r23795: more v2->v3 conversion
[samba.git] / source4 / cluster / ctdb / common / ctdb_message.c
1 /* 
2    ctdb_message protocol code
3
4    Copyright (C) Andrew Tridgell  2007
5
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.
10
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.
15
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
19 */
20 /*
21   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
22   protocol design and packet details
23 */
24 #include "includes.h"
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"
31
32 /*
33   this dispatches the messages to the registered ctdb message handler
34 */
35 static int ctdb_dispatch_message(struct ctdb_context *ctdb, uint32_t srvid, TDB_DATA data)
36 {
37         struct ctdb_message_list *ml;
38
39         /* XXX we need a must faster way of finding the matching srvid
40            - maybe a tree? */
41         for (ml=ctdb->message_list;ml;ml=ml->next) {
42                 if (ml->srvid == srvid || ml->srvid == CTDB_SRVID_ALL) break;
43         }
44         if (ml == NULL) {
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 */
48                 return -1;
49         }
50
51         ml->message_handler(ctdb, srvid, data, ml->message_private);
52         return 0;
53 }
54
55
56 /*
57   called when a CTDB_REQ_MESSAGE packet comes in
58 */
59 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
60 {
61         struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
62         TDB_DATA data;
63
64         data.dptr = &c->data[0];
65         data.dsize = c->datalen;
66
67         ctdb_dispatch_message(ctdb, c->srvid, data);
68 }
69
70 /*
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
74  */
75 struct ctdb_local_message {
76         struct ctdb_context *ctdb;
77         uint32_t srvid;
78         TDB_DATA data;
79 };
80
81 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te, 
82                                        struct timeval t, void *private_data)
83 {
84         struct ctdb_local_message *m = talloc_get_type(private_data, 
85                                                        struct ctdb_local_message);
86         int res;
87
88         res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
89         if (res != 0) {
90                 DEBUG(0, (__location__ " Failed to dispatch message for srvid=%u\n", m->srvid));
91         }
92         talloc_free(m);
93 }
94
95 static int ctdb_local_message(struct ctdb_context *ctdb, uint32_t srvid, TDB_DATA data)
96 {
97         struct ctdb_local_message *m;
98         m = talloc(ctdb, struct ctdb_local_message);
99         CTDB_NO_MEMORY(ctdb, m);
100
101         m->ctdb = ctdb;
102         m->srvid = srvid;
103         m->data  = data;
104         m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
105         if (m->data.dptr == NULL) {
106                 talloc_free(m);
107                 return -1;
108         }
109
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);
112         return 0;
113 }
114
115 /*
116   send a ctdb message
117 */
118 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
119                              uint32_t srvid, TDB_DATA data)
120 {
121         struct ctdb_req_message *r;
122         int len;
123
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);
127         }
128
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");
133
134         r->hdr.length    = len;
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;
140         r->hdr.reqid     = 0;
141         r->srvid         = srvid;
142         r->datalen       = data.dsize;
143         memcpy(&r->data[0], data.dptr, data.dsize);
144         
145         ctdb_queue_packet(ctdb, &r->hdr);
146
147         talloc_free(r);
148         return 0;
149 }
150
151
152 /*
153   when a client goes away, we need to remove its srvid handler from the list
154  */
155 static int message_handler_destructor(struct ctdb_message_list *m)
156 {
157         DLIST_REMOVE(m->ctdb->message_list, m);
158         return 0;
159 }
160
161 /*
162   setup handler for receipt of ctdb messages from ctdb_send_message()
163 */
164 int ctdb_register_message_handler(struct ctdb_context *ctdb, 
165                                   TALLOC_CTX *mem_ctx,
166                                   uint32_t srvid,
167                                   ctdb_message_fn_t handler,
168                                   void *private_data)
169 {
170         struct ctdb_message_list *m;
171
172         m = talloc(mem_ctx, struct ctdb_message_list);
173         CTDB_NO_MEMORY(ctdb, m);
174
175         m->ctdb            = ctdb;
176         m->srvid           = srvid;
177         m->message_handler = handler;
178         m->message_private = private_data;
179         
180         DLIST_ADD(ctdb->message_list, m);
181
182         talloc_set_destructor(m, message_handler_destructor);
183
184         return 0;
185 }