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