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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 see http://wiki.samba.org/index.php/Samba_%26_Clustering for
22 protocol design and packet details
25 #include "lib/events/events.h"
26 #include "lib/tdb/include/tdb.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "../include/ctdb_private.h"
30 #include "lib/util/dlinklist.h"
33 this dispatches the messages to the registered ctdb message handler
35 static int ctdb_dispatch_message(struct ctdb_context *ctdb, uint32_t srvid, TDB_DATA data)
37 struct ctdb_message_list *ml;
39 /* XXX we need a must faster way of finding the matching srvid
41 for (ml=ctdb->message_list;ml;ml=ml->next) {
42 if (ml->srvid == srvid || ml->srvid == CTDB_SRVID_ALL) break;
45 DEBUG(1,(__location__ " daemon vnn:%d no msg handler for srvid=%u\n",
46 ctdb_get_vnn(ctdb), srvid));
47 /* no registered message handler */
51 ml->message_handler(ctdb, srvid, data, ml->message_private);
57 called when a CTDB_REQ_MESSAGE packet comes in
59 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
61 struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
64 data.dptr = &c->data[0];
65 data.dsize = c->datalen;
67 ctdb_dispatch_message(ctdb, c->srvid, data);
71 this local messaging handler is ugly, but is needed to prevent
72 recursion in ctdb_send_message() when the destination node is the
73 same as the source node
75 struct ctdb_local_message {
76 struct ctdb_context *ctdb;
81 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
82 struct timeval t, void *private_data)
84 struct ctdb_local_message *m = talloc_get_type(private_data,
85 struct ctdb_local_message);
88 res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
90 DEBUG(0, (__location__ " Failed to dispatch message for srvid=%u\n", m->srvid));
95 static int ctdb_local_message(struct ctdb_context *ctdb, uint32_t srvid, TDB_DATA data)
97 struct ctdb_local_message *m;
98 m = talloc(ctdb, struct ctdb_local_message);
99 CTDB_NO_MEMORY(ctdb, m);
104 m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
105 if (m->data.dptr == NULL) {
110 /* this needs to be done as an event to prevent recursion */
111 event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
118 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
119 uint32_t srvid, TDB_DATA data)
121 struct ctdb_req_message *r;
124 /* see if this is a message to ourselves */
125 if (vnn == ctdb->vnn && !(ctdb->flags & CTDB_FLAG_SELF_CONNECT)) {
126 return ctdb_local_message(ctdb, srvid, data);
129 len = offsetof(struct ctdb_req_message, data) + data.dsize;
130 r = ctdb->methods->allocate_pkt(ctdb, len);
131 CTDB_NO_MEMORY(ctdb, r);
132 talloc_set_name_const(r, "req_message packet");
135 r->hdr.ctdb_magic = CTDB_MAGIC;
136 r->hdr.ctdb_version = CTDB_VERSION;
137 r->hdr.operation = CTDB_REQ_MESSAGE;
138 r->hdr.destnode = vnn;
139 r->hdr.srcnode = ctdb->vnn;
142 r->datalen = data.dsize;
143 memcpy(&r->data[0], data.dptr, data.dsize);
145 ctdb_queue_packet(ctdb, &r->hdr);
153 when a client goes away, we need to remove its srvid handler from the list
155 static int message_handler_destructor(struct ctdb_message_list *m)
157 DLIST_REMOVE(m->ctdb->message_list, m);
162 setup handler for receipt of ctdb messages from ctdb_send_message()
164 int ctdb_register_message_handler(struct ctdb_context *ctdb,
167 ctdb_message_fn_t handler,
170 struct ctdb_message_list *m;
172 m = talloc(mem_ctx, struct ctdb_message_list);
173 CTDB_NO_MEMORY(ctdb, m);
177 m->message_handler = handler;
178 m->message_private = private_data;
180 DLIST_ADD(ctdb->message_list, m);
182 talloc_set_destructor(m, message_handler_destructor);