ctdb-daemon: Check packet generation against database generation
[obnox/samba/samba-obnox.git] / ctdb / server / ctdb_server.c
1 /* 
2    ctdb main protocol code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program 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
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "tdb.h"
22 #include "lib/util/dlinklist.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26
27 /*
28   choose the transport we will use
29 */
30 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
31 {
32         ctdb->transport = talloc_strdup(ctdb, transport);
33         CTDB_NO_MEMORY(ctdb, ctdb->transport);
34
35         return 0;
36 }
37
38 /*
39   Check whether an ip is a valid node ip
40   Returns the node id for this ip address or -1
41 */
42 int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const ctdb_sock_addr *nodeip)
43 {
44         int nodeid;
45
46         for (nodeid=0;nodeid<ctdb->num_nodes;nodeid++) {
47                 if (ctdb->nodes[nodeid]->flags & NODE_FLAGS_DELETED) {
48                         continue;
49                 }
50                 if (ctdb_same_ip(&ctdb->nodes[nodeid]->address, nodeip)) {
51                         return nodeid;
52                 }
53         }
54
55         return -1;
56 }
57
58 /*
59   choose the recovery lock file
60 */
61 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file)
62 {
63         if (ctdb->recovery_lock_file != NULL) {
64                 talloc_free(ctdb->recovery_lock_file);
65                 ctdb->recovery_lock_file = NULL;
66         }
67
68         if (file == NULL) {
69                 DEBUG(DEBUG_ALERT,("Recovery lock file set to \"\". Disabling recovery lock checking\n"));
70                 return 0;
71         }
72
73         ctdb->recovery_lock_file = talloc_strdup(ctdb, file);
74         CTDB_NO_MEMORY(ctdb, ctdb->recovery_lock_file);
75
76         return 0;
77 }
78
79 /* Load a nodes list file into a nodes array */
80 static int convert_node_map_to_list(struct ctdb_context *ctdb,
81                                     TALLOC_CTX *mem_ctx,
82                                     struct ctdb_node_map *node_map,
83                                     struct ctdb_node ***nodes,
84                                     uint32_t *num_nodes)
85 {
86         int i;
87
88         *nodes = talloc_zero_array(mem_ctx,
89                                         struct ctdb_node *, node_map->num);
90         CTDB_NO_MEMORY(ctdb, *nodes);
91         *num_nodes = node_map->num;
92
93         for (i = 0; i < node_map->num; i++) {
94                 struct ctdb_node *node;
95
96                 node = talloc_zero(*nodes, struct ctdb_node);
97                 CTDB_NO_MEMORY(ctdb, node);
98                 (*nodes)[i] = node;
99
100                 node->address = node_map->nodes[i].addr;
101                 node->name = talloc_asprintf(node, "%s:%u",
102                                              ctdb_addr_to_str(&node->address),
103                                              ctdb_addr_to_port(&node->address));
104
105                 node->flags = node_map->nodes[i].flags;
106                 if (!(node->flags & NODE_FLAGS_DELETED)) {
107                         node->flags = NODE_FLAGS_UNHEALTHY;
108                 }
109                 node->flags |= NODE_FLAGS_DISCONNECTED;
110
111                 node->pnn = i;
112                 node->ctdb = ctdb;
113                 node->dead_count = 0;
114         }
115
116         return 0;
117 }
118
119 /* Load the nodes list from a file */
120 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
121 {
122         struct ctdb_node_map *node_map;
123         int ret;
124
125         node_map = ctdb_read_nodes_file(ctdb, ctdb->nodes_file);
126         if (node_map == NULL) {
127                 goto fail;
128         }
129
130         TALLOC_FREE(ctdb->nodes);
131         ret = convert_node_map_to_list(ctdb, ctdb, node_map,
132                                        &ctdb->nodes, &ctdb->num_nodes);
133         if (ret == -1) {
134                 goto fail;
135         }
136
137         talloc_free(node_map);
138         return;
139
140 fail:
141         DEBUG(DEBUG_ERR, ("Failed to load nodes file \"%s\"\n",
142                           ctdb->nodes_file));
143         talloc_free(node_map);
144         exit(1);
145 }
146
147 /*
148   setup the local node address
149 */
150 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
151 {
152         ctdb->address = talloc(ctdb, ctdb_sock_addr);
153         CTDB_NO_MEMORY(ctdb, ctdb->address);
154
155         if (ctdb_parse_address(ctdb, address, ctdb->address) != 0) {
156                 return -1;
157         }
158
159         ctdb->name = talloc_asprintf(ctdb, "%s:%u",
160                                      ctdb_addr_to_str(ctdb->address),
161                                      ctdb_addr_to_port(ctdb->address));
162         return 0;
163 }
164
165
166 /*
167   return the number of active nodes
168 */
169 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb)
170 {
171         int i;
172         uint32_t count=0;
173         for (i=0; i < ctdb->num_nodes; i++) {
174                 if (!(ctdb->nodes[i]->flags & NODE_FLAGS_INACTIVE)) {
175                         count++;
176                 }
177         }
178         return count;
179 }
180
181
182 /*
183   called when we need to process a packet. This can be a requeued packet
184   after a lockwait, or a real packet from another node
185 */
186 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
187 {
188         TALLOC_CTX *tmp_ctx;
189
190         /* place the packet as a child of the tmp_ctx. We then use
191            talloc_free() below to free it. If any of the calls want
192            to keep it, then they will steal it somewhere else, and the
193            talloc_free() will only free the tmp_ctx */
194         tmp_ctx = talloc_new(ctdb);
195         talloc_steal(tmp_ctx, hdr);
196
197         DEBUG(DEBUG_DEBUG,(__location__ " ctdb request %u of type %u length %u from "
198                  "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
199                  hdr->srcnode, hdr->destnode));
200
201         switch (hdr->operation) {
202         case CTDB_REQ_CALL:
203         case CTDB_REPLY_CALL:
204         case CTDB_REQ_DMASTER:
205         case CTDB_REPLY_DMASTER:
206                 /* we dont allow these calls when banned */
207                 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_BANNED) {
208                         DEBUG(DEBUG_DEBUG,(__location__ " ctdb operation %u"
209                                 " request %u"
210                                 " length %u from node %u to %u while node"
211                                 " is banned\n",
212                                  hdr->operation, hdr->reqid,
213                                  hdr->length, 
214                                  hdr->srcnode, hdr->destnode));
215                         goto done;
216                 }
217
218                 /* Push the check for generation in the handlers for these
219                  * operations.  Check database generation instead of global
220                  * generation.  Since the database context is not available
221                  * here, push the check in the operations.
222                  */
223         }
224
225         switch (hdr->operation) {
226         case CTDB_REQ_CALL:
227                 CTDB_INCREMENT_STAT(ctdb, node.req_call);
228                 ctdb_request_call(ctdb, hdr);
229                 break;
230
231         case CTDB_REPLY_CALL:
232                 CTDB_INCREMENT_STAT(ctdb, node.reply_call);
233                 ctdb_reply_call(ctdb, hdr);
234                 break;
235
236         case CTDB_REPLY_ERROR:
237                 CTDB_INCREMENT_STAT(ctdb, node.reply_error);
238                 ctdb_reply_error(ctdb, hdr);
239                 break;
240
241         case CTDB_REQ_DMASTER:
242                 CTDB_INCREMENT_STAT(ctdb, node.req_dmaster);
243                 ctdb_request_dmaster(ctdb, hdr);
244                 break;
245
246         case CTDB_REPLY_DMASTER:
247                 CTDB_INCREMENT_STAT(ctdb, node.reply_dmaster);
248                 ctdb_reply_dmaster(ctdb, hdr);
249                 break;
250
251         case CTDB_REQ_MESSAGE:
252                 CTDB_INCREMENT_STAT(ctdb, node.req_message);
253                 ctdb_request_message(ctdb, hdr);
254                 break;
255
256         case CTDB_REQ_CONTROL:
257                 CTDB_INCREMENT_STAT(ctdb, node.req_control);
258                 ctdb_request_control(ctdb, hdr);
259                 break;
260
261         case CTDB_REPLY_CONTROL:
262                 CTDB_INCREMENT_STAT(ctdb, node.reply_control);
263                 ctdb_reply_control(ctdb, hdr);
264                 break;
265
266         case CTDB_REQ_KEEPALIVE:
267                 CTDB_INCREMENT_STAT(ctdb, keepalive_packets_recv);
268                 break;
269
270         default:
271                 DEBUG(DEBUG_CRIT,("%s: Packet with unknown operation %u\n", 
272                          __location__, hdr->operation));
273                 break;
274         }
275
276 done:
277         talloc_free(tmp_ctx);
278 }
279
280
281 /*
282   called by the transport layer when a node is dead
283 */
284 void ctdb_node_dead(struct ctdb_node *node)
285 {
286         if (node->flags & NODE_FLAGS_DISCONNECTED) {
287                 DEBUG(DEBUG_INFO,("%s: node %s is already marked disconnected: %u connected\n", 
288                          node->ctdb->name, node->name, 
289                          node->ctdb->num_connected));
290                 return;
291         }
292         node->ctdb->num_connected--;
293         node->flags |= NODE_FLAGS_DISCONNECTED | NODE_FLAGS_UNHEALTHY;
294         node->rx_cnt = 0;
295         node->dead_count = 0;
296
297         DEBUG(DEBUG_NOTICE,("%s: node %s is dead: %u connected\n", 
298                  node->ctdb->name, node->name, node->ctdb->num_connected));
299         ctdb_daemon_cancel_controls(node->ctdb, node);
300
301         if (node->ctdb->methods == NULL) {
302                 DEBUG(DEBUG_ERR,(__location__ " Can not restart transport while shutting down daemon.\n"));
303                 return;
304         }
305
306         node->ctdb->methods->restart(node);
307 }
308
309 /*
310   called by the transport layer when a node is connected
311 */
312 void ctdb_node_connected(struct ctdb_node *node)
313 {
314         if (!(node->flags & NODE_FLAGS_DISCONNECTED)) {
315                 DEBUG(DEBUG_INFO,("%s: node %s is already marked connected: %u connected\n", 
316                          node->ctdb->name, node->name, 
317                          node->ctdb->num_connected));
318                 return;
319         }
320         node->ctdb->num_connected++;
321         node->dead_count = 0;
322         node->flags &= ~NODE_FLAGS_DISCONNECTED;
323         node->flags |= NODE_FLAGS_UNHEALTHY;
324         DEBUG(DEBUG_NOTICE,
325               ("%s: connected to %s - %u connected\n", 
326                node->ctdb->name, node->name, node->ctdb->num_connected));
327 }
328
329 struct queue_next {
330         struct ctdb_context *ctdb;
331         struct ctdb_req_header *hdr;
332 };
333
334
335 /*
336   triggered when a deferred packet is due
337  */
338 static void queue_next_trigger(struct event_context *ev, struct timed_event *te, 
339                                struct timeval t, void *private_data)
340 {
341         struct queue_next *q = talloc_get_type(private_data, struct queue_next);
342         ctdb_input_pkt(q->ctdb, q->hdr);
343         talloc_free(q);
344 }       
345
346 /*
347   defer a packet, so it is processed on the next event loop
348   this is used for sending packets to ourselves
349  */
350 static void ctdb_defer_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
351 {
352         struct queue_next *q;
353         q = talloc(ctdb, struct queue_next);
354         if (q == NULL) {
355                 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate deferred packet\n"));
356                 return;
357         }
358         q->ctdb = ctdb;
359         q->hdr = talloc_memdup(ctdb, hdr, hdr->length);
360         if (q->hdr == NULL) {
361                 DEBUG(DEBUG_ERR,("Error copying deferred packet to self\n"));
362                 return;
363         }
364 #if 0
365         /* use this to put packets directly into our recv function */
366         ctdb_input_pkt(q->ctdb, q->hdr);
367 #else
368         event_add_timed(ctdb->ev, q, timeval_zero(), queue_next_trigger, q);
369 #endif
370 }
371
372
373 /*
374   broadcast a packet to all nodes
375 */
376 static void ctdb_broadcast_packet_all(struct ctdb_context *ctdb, 
377                                       struct ctdb_req_header *hdr)
378 {
379         int i;
380         for (i=0; i < ctdb->num_nodes; i++) {
381                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
382                         continue;
383                 }
384                 hdr->destnode = ctdb->nodes[i]->pnn;
385                 ctdb_queue_packet(ctdb, hdr);
386         }
387 }
388
389 /*
390   broadcast a packet to all nodes in the current vnnmap
391 */
392 static void ctdb_broadcast_packet_vnnmap(struct ctdb_context *ctdb, 
393                                          struct ctdb_req_header *hdr)
394 {
395         int i;
396         for (i=0;i<ctdb->vnn_map->size;i++) {
397                 hdr->destnode = ctdb->vnn_map->map[i];
398                 ctdb_queue_packet(ctdb, hdr);
399         }
400 }
401
402 /*
403   broadcast a packet to all connected nodes
404 */
405 static void ctdb_broadcast_packet_connected(struct ctdb_context *ctdb, 
406                                             struct ctdb_req_header *hdr)
407 {
408         int i;
409         for (i=0; i < ctdb->num_nodes; i++) {
410                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
411                         continue;
412                 }
413                 if (!(ctdb->nodes[i]->flags & NODE_FLAGS_DISCONNECTED)) {
414                         hdr->destnode = ctdb->nodes[i]->pnn;
415                         ctdb_queue_packet(ctdb, hdr);
416                 }
417         }
418 }
419
420 /*
421   queue a packet or die
422 */
423 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
424 {
425         struct ctdb_node *node;
426
427         switch (hdr->destnode) {
428         case CTDB_BROADCAST_ALL:
429                 ctdb_broadcast_packet_all(ctdb, hdr);
430                 return;
431         case CTDB_BROADCAST_VNNMAP:
432                 ctdb_broadcast_packet_vnnmap(ctdb, hdr);
433                 return;
434         case CTDB_BROADCAST_CONNECTED:
435                 ctdb_broadcast_packet_connected(ctdb, hdr);
436                 return;
437         }
438
439         CTDB_INCREMENT_STAT(ctdb, node_packets_sent);
440
441         if (!ctdb_validate_pnn(ctdb, hdr->destnode)) {
442                 DEBUG(DEBUG_CRIT,(__location__ " cant send to node %u that does not exist\n", 
443                          hdr->destnode));
444                 return;
445         }
446
447         node = ctdb->nodes[hdr->destnode];
448
449         if (node->flags & NODE_FLAGS_DELETED) {
450                 DEBUG(DEBUG_ERR, (__location__ " Can not queue packet to DELETED node %d\n", hdr->destnode));
451                 return;
452         }
453
454         if (node->pnn == ctdb->pnn) {
455                 ctdb_defer_packet(ctdb, hdr);
456                 return;
457         }
458
459         if (ctdb->methods == NULL) {
460                 DEBUG(DEBUG_ALERT, (__location__ " Can not queue packet. "
461                                     "Transport is DOWN\n"));
462                 return;
463         }
464
465         node->tx_cnt++;
466         if (ctdb->methods->queue_pkt(node, (uint8_t *)hdr, hdr->length) != 0) {
467                 ctdb_fatal(ctdb, "Unable to queue packet\n");
468         }
469 }
470
471
472
473
474 /*
475   a valgrind hack to allow us to get opcode specific backtraces
476   very ugly, and relies on no compiler optimisation!
477 */
478 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode)
479 {
480         switch (opcode) {
481 #define DO_OP(x) case x: ctdb_queue_packet(ctdb, hdr); break
482                 DO_OP(1);
483                 DO_OP(2);
484                 DO_OP(3);
485                 DO_OP(4);
486                 DO_OP(5);
487                 DO_OP(6);
488                 DO_OP(7);
489                 DO_OP(8);
490                 DO_OP(9);
491                 DO_OP(10);
492                 DO_OP(11);
493                 DO_OP(12);
494                 DO_OP(13);
495                 DO_OP(14);
496                 DO_OP(15);
497                 DO_OP(16);
498                 DO_OP(17);
499                 DO_OP(18);
500                 DO_OP(19);
501                 DO_OP(20);
502                 DO_OP(21);
503                 DO_OP(22);
504                 DO_OP(23);
505                 DO_OP(24);
506                 DO_OP(25);
507                 DO_OP(26);
508                 DO_OP(27);
509                 DO_OP(28);
510                 DO_OP(29);
511                 DO_OP(30);
512                 DO_OP(31);
513                 DO_OP(32);
514                 DO_OP(33);
515                 DO_OP(34);
516                 DO_OP(35);
517                 DO_OP(36);
518                 DO_OP(37);
519                 DO_OP(38);
520                 DO_OP(39);
521                 DO_OP(40);
522                 DO_OP(41);
523                 DO_OP(42);
524                 DO_OP(43);
525                 DO_OP(44);
526                 DO_OP(45);
527                 DO_OP(46);
528                 DO_OP(47);
529                 DO_OP(48);
530                 DO_OP(49);
531                 DO_OP(50);
532                 DO_OP(51);
533                 DO_OP(52);
534                 DO_OP(53);
535                 DO_OP(54);
536                 DO_OP(55);
537                 DO_OP(56);
538                 DO_OP(57);
539                 DO_OP(58);
540                 DO_OP(59);
541                 DO_OP(60);
542                 DO_OP(61);
543                 DO_OP(62);
544                 DO_OP(63);
545                 DO_OP(64);
546                 DO_OP(65);
547                 DO_OP(66);
548                 DO_OP(67);
549                 DO_OP(68);
550                 DO_OP(69);
551                 DO_OP(70);
552                 DO_OP(71);
553                 DO_OP(72);
554                 DO_OP(73);
555                 DO_OP(74);
556                 DO_OP(75);
557                 DO_OP(76);
558                 DO_OP(77);
559                 DO_OP(78);
560                 DO_OP(79);
561                 DO_OP(80);
562                 DO_OP(81);
563                 DO_OP(82);
564                 DO_OP(83);
565                 DO_OP(84);
566                 DO_OP(85);
567                 DO_OP(86);
568                 DO_OP(87);
569                 DO_OP(88);
570                 DO_OP(89);
571                 DO_OP(90);
572                 DO_OP(91);
573                 DO_OP(92);
574                 DO_OP(93);
575                 DO_OP(94);
576                 DO_OP(95);
577                 DO_OP(96);
578                 DO_OP(97);
579                 DO_OP(98);
580                 DO_OP(99);
581                 DO_OP(100);
582         default: 
583                 ctdb_queue_packet(ctdb, hdr);
584                 break;
585         }
586 }