2 ctdb_message protocol code
4 Copyright (C) Andrew Tridgell 2007
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21 protocol design and packet details
24 #include "lib/events/events.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28 #include "../include/ctdb_private.h"
29 #include "lib/util/dlinklist.h"
32 this dispatches the messages to the registered ctdb message handler
34 static int ctdb_dispatch_message(struct ctdb_context *ctdb, uint32_t srvid, TDB_DATA data)
36 struct ctdb_message_list *ml;
38 /* XXX we need a must faster way of finding the matching srvid
40 for (ml=ctdb->message_list;ml;ml=ml->next) {
41 if (ml->srvid == srvid || ml->srvid == CTDB_SRVID_ALL) break;
44 DEBUG(1,(__location__ " daemon vnn:%d no msg handler for srvid=%u\n",
45 ctdb_get_vnn(ctdb), srvid));
46 /* no registered message handler */
50 ml->message_handler(ctdb, srvid, data, ml->message_private);
56 called when a CTDB_REQ_MESSAGE packet comes in
58 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
60 struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
63 data.dptr = &c->data[0];
64 data.dsize = c->datalen;
66 ctdb_dispatch_message(ctdb, c->srvid, data);
70 this local messaging handler is ugly, but is needed to prevent
71 recursion in ctdb_send_message() when the destination node is the
72 same as the source node
74 struct ctdb_local_message {
75 struct ctdb_context *ctdb;
80 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
81 struct timeval t, void *private_data)
83 struct ctdb_local_message *m = talloc_get_type(private_data,
84 struct ctdb_local_message);
87 res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
89 DEBUG(0, (__location__ " Failed to dispatch message for srvid=%u\n", m->srvid));
94 static int ctdb_local_message(struct ctdb_context *ctdb, uint32_t srvid, TDB_DATA data)
96 struct ctdb_local_message *m;
97 m = talloc(ctdb, struct ctdb_local_message);
98 CTDB_NO_MEMORY(ctdb, m);
103 m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
104 if (m->data.dptr == NULL) {
109 /* this needs to be done as an event to prevent recursion */
110 event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
117 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
118 uint32_t srvid, TDB_DATA data)
120 struct ctdb_req_message *r;
123 /* see if this is a message to ourselves */
124 if (vnn == ctdb->vnn && !(ctdb->flags & CTDB_FLAG_SELF_CONNECT)) {
125 return ctdb_local_message(ctdb, srvid, data);
128 len = offsetof(struct ctdb_req_message, data) + data.dsize;
129 r = ctdb->methods->allocate_pkt(ctdb, len);
130 CTDB_NO_MEMORY(ctdb, r);
131 talloc_set_name_const(r, "req_message packet");
134 r->hdr.ctdb_magic = CTDB_MAGIC;
135 r->hdr.ctdb_version = CTDB_VERSION;
136 r->hdr.operation = CTDB_REQ_MESSAGE;
137 r->hdr.destnode = vnn;
138 r->hdr.srcnode = ctdb->vnn;
141 r->datalen = data.dsize;
142 memcpy(&r->data[0], data.dptr, data.dsize);
144 ctdb_queue_packet(ctdb, &r->hdr);
152 when a client goes away, we need to remove its srvid handler from the list
154 static int message_handler_destructor(struct ctdb_message_list *m)
156 DLIST_REMOVE(m->ctdb->message_list, m);
161 setup handler for receipt of ctdb messages from ctdb_send_message()
163 int ctdb_register_message_handler(struct ctdb_context *ctdb,
166 ctdb_message_fn_t handler,
169 struct ctdb_message_list *m;
171 m = talloc(mem_ctx, struct ctdb_message_list);
172 CTDB_NO_MEMORY(ctdb, m);
176 m->message_handler = handler;
177 m->message_private = private_data;
179 DLIST_ADD(ctdb->message_list, m);
181 talloc_set_destructor(m, message_handler_destructor);