update a comment
[sahlberg/ctdb.git] / include / ctdb_private.h
1 /* 
2    ctdb database library
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 #ifndef _CTDB_PRIVATE_H
21 #define _CTDB_PRIVATE_H
22
23 #include "ctdb.h"
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26
27 /* location of daemon socket */
28 #define CTDB_PATH       "/tmp/ctdb.socket"
29
30 /* default ctdb port number */
31 #define CTDB_PORT 4379
32
33 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
34 #define CTDB_DS_ALIGNMENT 8
35
36
37 #define CTDB_NULL_FUNC      0xFF000001
38 #define CTDB_FETCH_FUNC     0xFF000002
39
40 /*
41   a tcp connection description
42  */
43 struct ctdb_tcp_connection {
44         struct sockaddr_in saddr;
45         struct sockaddr_in daddr;
46 };
47
48 /* the wire representation for a tcp tickle array */
49 struct ctdb_tcp_wire_array {
50         uint32_t num;
51         struct ctdb_tcp_connection connections[1];
52 };      
53
54 /* the list of tcp tickles used by get/set tcp tickle list */
55 struct ctdb_control_tcp_tickle_list {
56         struct sockaddr_in ip;
57         struct ctdb_tcp_wire_array tickles;
58 };
59
60 /*
61   array of tcp connections
62  */
63 struct ctdb_tcp_array {
64         uint32_t num;
65         struct ctdb_tcp_connection *connections;
66 };      
67
68
69 /* all tunable variables go in here */
70 struct ctdb_tunable {
71         uint32_t max_redirect_count;
72         uint32_t seqnum_frequency;
73         uint32_t control_timeout;
74         uint32_t traverse_timeout;
75         uint32_t keepalive_interval;
76         uint32_t keepalive_limit;
77         uint32_t max_lacount;
78         uint32_t recover_timeout;
79         uint32_t recover_interval;
80         uint32_t election_timeout;
81         uint32_t takeover_timeout;
82         uint32_t monitor_interval;
83         uint32_t tickle_update_interval;
84         uint32_t script_timeout;
85         uint32_t recovery_grace_period;
86         uint32_t recovery_ban_period;
87         uint32_t database_hash_size;
88         uint32_t rerecovery_timeout;
89 };
90
91 /*
92   an installed ctdb remote call
93 */
94 struct ctdb_registered_call {
95         struct ctdb_registered_call *next, *prev;
96         uint32_t id;
97         ctdb_fn_t fn;
98 };
99
100 /*
101   this address structure might need to be generalised later for some
102   transports
103 */
104 struct ctdb_address {
105         const char *address;
106         int port;
107 };
108
109 /*
110   check that a pnn is valid
111  */
112 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
113
114
115 /* called from the queue code when a packet comes in. Called with data==NULL
116    on error */
117 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
118                                    void *private_data);
119
120 /* used for callbacks in ctdb_control requests */
121 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
122                                            int32_t status, TDB_DATA data, 
123                                            const char *errormsg,
124                                            void *private_data);
125
126 /*
127   structure describing a connected client in the daemon
128  */
129 struct ctdb_client {
130         struct ctdb_context *ctdb;
131         int fd;
132         struct ctdb_queue *queue;
133         uint32_t client_id;
134         pid_t pid;
135         struct ctdb_tcp_list *tcp_list;
136 };
137
138
139 /* state associated with a public ip address */
140 struct ctdb_vnn {
141         struct ctdb_vnn *prev, *next;
142
143         const char *iface;
144         const char *public_address;
145         uint8_t public_netmask_bits;
146
147         /* the node number that is serving this public address, if any. 
148            If no node serves this ip it is set to -1 */
149         int32_t pnn;
150
151         /* List of clients to tickle for this public address */
152         struct ctdb_tcp_array *tcp_array;
153
154         /* whether we need to update the other nodes with changes to our list
155            of connected clients */
156         bool tcp_update_needed;
157
158         /* a context to hang sending gratious arp events off */
159         TALLOC_CTX *takeover_ctx;
160
161         struct ctdb_kill_tcp *killtcp;
162 };
163
164 /*
165   state associated with one node
166 */
167 struct ctdb_node {
168         struct ctdb_context *ctdb;
169         struct ctdb_address address;
170         const char *name; /* for debug messages */
171         void *private_data; /* private to transport */
172         uint32_t pnn;
173 #define NODE_FLAGS_DISCONNECTED         0x00000001 /* node isn't connected */
174 #define NODE_FLAGS_UNHEALTHY            0x00000002 /* monitoring says node is unhealthy */
175 #define NODE_FLAGS_PERMANENTLY_DISABLED 0x00000004 /* administrator has disabled node */
176 #define NODE_FLAGS_BANNED               0x00000008 /* recovery daemon has banned the node */
177 #define NODE_FLAGS_DISABLED             (NODE_FLAGS_UNHEALTHY|NODE_FLAGS_PERMANENTLY_DISABLED)
178 #define NODE_FLAGS_INACTIVE             (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_BANNED)
179         uint32_t flags;
180
181         /* used by the dead node monitoring */
182         uint32_t dead_count;
183         uint32_t rx_cnt;
184         uint32_t tx_cnt;
185
186         /* a list of controls pending to this node, so we can time them out quickly
187            if the node becomes disconnected */
188         struct daemon_control_state *pending_controls;
189
190         /* used by the recovery daemon when distributing ip addresses 
191            across the nodes.  it needs to know which public ip's can be handled
192            by each node.
193         */
194         struct ctdb_all_public_ips *public_ips;
195 };
196
197 /*
198   transport specific methods
199 */
200 struct ctdb_methods {
201         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
202         int (*start)(struct ctdb_context *); /* start protocol processing */    
203         int (*add_node)(struct ctdb_node *); /* setup a new node */     
204         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
205         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
206         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
207 };
208
209 /*
210   transport calls up to the ctdb layer
211 */
212 struct ctdb_upcalls {
213         /* recv_pkt is called when a packet comes in */
214         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
215
216         /* node_dead is called when an attempt to send to a node fails */
217         void (*node_dead)(struct ctdb_node *);
218
219         /* node_connected is called when a connection to a node is established */
220         void (*node_connected)(struct ctdb_node *);
221 };
222
223 /* list of message handlers - needs to be changed to a more efficient data
224    structure so we can find a message handler given a srvid quickly */
225 struct ctdb_message_list {
226         struct ctdb_context *ctdb;
227         struct ctdb_message_list *next, *prev;
228         uint64_t srvid;
229         ctdb_message_fn_t message_handler;
230         void *message_private;
231 };
232
233 /* additional data required for the daemon mode */
234 struct ctdb_daemon_data {
235         int sd;
236         char *name;
237         struct ctdb_queue *queue;
238 };
239
240 /*
241   ctdb status information
242  */
243 struct ctdb_statistics {
244         uint32_t num_clients;
245         uint32_t frozen;
246         uint32_t recovering;
247         uint32_t client_packets_sent;
248         uint32_t client_packets_recv;
249         uint32_t node_packets_sent;
250         uint32_t node_packets_recv;
251         uint32_t keepalive_packets_sent;
252         uint32_t keepalive_packets_recv;
253         struct {
254                 uint32_t req_call;
255                 uint32_t reply_call;
256                 uint32_t req_dmaster;
257                 uint32_t reply_dmaster;
258                 uint32_t reply_error;
259                 uint32_t req_message;
260                 uint32_t req_control;
261                 uint32_t reply_control;
262         } node;
263         struct {
264                 uint32_t req_call;
265                 uint32_t req_message;
266                 uint32_t req_control;
267         } client;
268         struct {
269                 uint32_t call;
270                 uint32_t control;
271                 uint32_t traverse;
272         } timeouts;
273         uint32_t total_calls;
274         uint32_t pending_calls;
275         uint32_t lockwait_calls;
276         uint32_t pending_lockwait_calls;
277         uint32_t memory_used;
278         uint32_t __last_counter; /* hack for control_statistics_all */
279         uint32_t max_hop_count;
280         double max_call_latency;
281         double max_lockwait_latency;
282 };
283
284
285 #define INVALID_GENERATION 1
286 /* table that contains the mapping between a hash value and lmaster
287  */
288 struct ctdb_vnn_map {
289         uint32_t generation;
290         uint32_t size;
291         uint32_t *map;
292 };
293
294 /* 
295    a wire representation of the vnn map
296  */
297 struct ctdb_vnn_map_wire {
298         uint32_t generation;
299         uint32_t size;
300         uint32_t map[1];
301 };
302
303 /* a structure that contains the elements required for the write record
304    control
305 */
306 struct ctdb_write_record {
307         uint32_t dbid;
308         uint32_t keylen;
309         uint32_t datalen;
310         unsigned char blob[1];
311 };
312
313 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
314
315 #define CTDB_MONITORING_ACTIVE          0
316 #define CTDB_MONITORING_DISABLED        1
317
318 /* main state of the ctdb daemon */
319 struct ctdb_context {
320         struct event_context *ev;
321         uint32_t recovery_mode;
322         uint32_t monitoring_mode;
323         TALLOC_CTX *monitor_context;
324         TALLOC_CTX *tickle_update_context;
325         struct ctdb_tunable tunable;
326         enum ctdb_freeze_mode freeze_mode;
327         struct ctdb_freeze_handle *freeze_handle;
328         struct ctdb_address address;
329         const char *name;
330         const char *db_directory;
331         const char *transport;
332         const char *logfile;
333         char *node_list_file;
334         char *recovery_lock_file;
335         int recovery_lock_fd;
336         uint32_t pnn; /* our own pnn */
337         uint32_t num_nodes;
338         uint32_t num_connected;
339         unsigned flags;
340         struct idr_context *idr;
341         uint16_t idr_cnt;
342         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
343         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
344         char *err_msg;
345         const struct ctdb_methods *methods; /* transport methods */
346         const struct ctdb_upcalls *upcalls; /* transport upcalls */
347         void *private_data; /* private to transport */
348         struct ctdb_db_context *db_list;
349         struct ctdb_message_list *message_list;
350         struct ctdb_daemon_data daemon;
351         struct ctdb_statistics statistics;
352         struct ctdb_vnn_map *vnn_map;
353         uint32_t num_clients;
354         uint32_t recovery_master;
355         struct ctdb_call_state *pending_calls;
356         struct ctdb_client_ip *client_ip_list;
357         bool do_setsched;
358         void *saved_scheduler_param;
359         struct _trbt_tree_t *server_ids;        
360         const char *event_script_dir;
361 };
362
363 struct ctdb_db_context {
364         struct ctdb_db_context *next, *prev;
365         struct ctdb_context *ctdb;
366         uint32_t db_id;
367         const char *db_name;
368         const char *db_path;
369         struct tdb_wrap *ltdb;
370         struct ctdb_registered_call *calls; /* list of registered calls */
371         uint32_t seqnum;
372         struct timed_event *te;
373 };
374
375
376 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
377           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
378           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
379           return -1; }} while (0)
380
381 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
382           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
383           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
384           }} while (0)
385
386 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
387           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
388           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
389           return NULL; }} while (0)
390
391 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
392           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
393           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
394           }} while (0)
395
396 /*
397   the extended header for records in the ltdb
398 */
399 struct ctdb_ltdb_header {
400         uint64_t rsn;
401         uint32_t dmaster;
402         uint32_t laccessor;
403         uint32_t lacount;
404 };
405
406 enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0, 
407                     CTDB_CONTROL_STATISTICS              = 1, 
408                     /* #2 removed */
409                     CTDB_CONTROL_PING                    = 3,
410                     CTDB_CONTROL_GETDBPATH               = 4,
411                     CTDB_CONTROL_GETVNNMAP               = 5,
412                     CTDB_CONTROL_SETVNNMAP               = 6,
413                     CTDB_CONTROL_GET_DEBUG               = 7,
414                     CTDB_CONTROL_SET_DEBUG               = 8,
415                     CTDB_CONTROL_GET_DBMAP               = 9,
416                     CTDB_CONTROL_GET_NODEMAP             = 10,
417                     CTDB_CONTROL_SET_DMASTER             = 11,
418                     /* #12 removed */
419                     CTDB_CONTROL_PULL_DB                 = 13,
420                     CTDB_CONTROL_PUSH_DB                 = 14,
421                     CTDB_CONTROL_GET_RECMODE             = 15,
422                     CTDB_CONTROL_SET_RECMODE             = 16,
423                     CTDB_CONTROL_STATISTICS_RESET        = 17,
424                     CTDB_CONTROL_DB_ATTACH               = 18,
425                     CTDB_CONTROL_SET_CALL                = 19,
426                     CTDB_CONTROL_TRAVERSE_START          = 20,
427                     CTDB_CONTROL_TRAVERSE_ALL            = 21,
428                     CTDB_CONTROL_TRAVERSE_DATA           = 22,
429                     CTDB_CONTROL_REGISTER_SRVID          = 23,
430                     CTDB_CONTROL_DEREGISTER_SRVID        = 24,
431                     CTDB_CONTROL_GET_DBNAME              = 25,
432                     CTDB_CONTROL_ENABLE_SEQNUM           = 26,
433                     CTDB_CONTROL_UPDATE_SEQNUM           = 27,
434                     /* #28 removed */
435                     CTDB_CONTROL_DUMP_MEMORY             = 29,
436                     CTDB_CONTROL_GET_PID                 = 30,
437                     CTDB_CONTROL_GET_RECMASTER           = 31,
438                     CTDB_CONTROL_SET_RECMASTER           = 32,
439                     CTDB_CONTROL_FREEZE                  = 33,
440                     CTDB_CONTROL_THAW                    = 34,
441                     CTDB_CONTROL_GET_PNN                 = 35,
442                     CTDB_CONTROL_SHUTDOWN                = 36,
443                     CTDB_CONTROL_GET_MONMODE             = 37,
444                     CTDB_CONTROL_SET_MONMODE             = 38,
445                     CTDB_CONTROL_MAX_RSN                 = 39,
446                     CTDB_CONTROL_SET_RSN_NONEMPTY        = 40,
447                     CTDB_CONTROL_DELETE_LOW_RSN          = 41,
448                     CTDB_CONTROL_TAKEOVER_IP             = 42,
449                     CTDB_CONTROL_RELEASE_IP              = 43,
450                     CTDB_CONTROL_TCP_CLIENT              = 44,
451                     CTDB_CONTROL_TCP_ADD                 = 45,
452                     CTDB_CONTROL_TCP_REMOVE              = 46,
453                     CTDB_CONTROL_STARTUP                 = 47,
454                     CTDB_CONTROL_SET_TUNABLE             = 48,
455                     CTDB_CONTROL_GET_TUNABLE             = 49,
456                     CTDB_CONTROL_LIST_TUNABLES           = 50,
457                     CTDB_CONTROL_GET_PUBLIC_IPS          = 51,
458                     CTDB_CONTROL_MODIFY_FLAGS            = 52,
459                     CTDB_CONTROL_GET_ALL_TUNABLES        = 53,
460                     CTDB_CONTROL_KILL_TCP                = 54,
461                     CTDB_CONTROL_GET_TCP_TICKLE_LIST     = 55,
462                     CTDB_CONTROL_SET_TCP_TICKLE_LIST     = 56,
463                     CTDB_CONTROL_REGISTER_SERVER_ID      = 57,
464                     CTDB_CONTROL_UNREGISTER_SERVER_ID    = 58,
465                     CTDB_CONTROL_CHECK_SERVER_ID         = 59,
466                     CTDB_CONTROL_GET_SERVER_ID_LIST      = 60,
467 };      
468
469 /*
470   structure passed in ctdb_control_set_rsn_nonempty
471  */
472 struct ctdb_control_set_rsn_nonempty {
473         uint32_t db_id;
474         uint64_t rsn;
475 };
476
477 /*
478   structure passed in ctdb_control_delete_low_rsn
479  */
480 struct ctdb_control_delete_low_rsn {
481         uint32_t db_id;
482         uint64_t rsn;
483 };
484
485 /*
486   structure passed in set_call control
487  */
488 struct ctdb_control_set_call {
489         uint32_t db_id;
490         ctdb_fn_t fn;
491         uint32_t id;
492 };
493
494 /*
495   struct for tcp_client control
496  */
497 struct ctdb_control_tcp {
498         struct sockaddr_in src;
499         struct sockaddr_in dest;
500 };
501
502 /*
503   struct for kill_tcp control
504  */
505 struct ctdb_control_killtcp {
506         struct sockaddr_in src;
507         struct sockaddr_in dst;
508 };
509
510 /*
511   struct for tcp_add and tcp_remove controls
512  */
513 struct ctdb_control_tcp_vnn {
514         struct sockaddr_in src;
515         struct sockaddr_in dest;
516 };
517
518 /*
519   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
520  */
521 struct ctdb_node_flag_change {
522         uint32_t pnn;
523         uint32_t new_flags;
524         uint32_t old_flags;
525 };
526
527 /*
528   structure to change flags on a node
529  */
530 struct ctdb_node_modflags {
531         uint32_t set;
532         uint32_t clear;
533 };
534
535 /*
536   struct for admin setting a ban
537  */
538 struct ctdb_ban_info {
539         uint32_t pnn;
540         uint32_t ban_time;
541 };
542
543 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
544
545 #define CTDB_LMASTER_ANY        0xffffffff
546
547 /*
548   state of a in-progress ctdb call
549 */
550 struct ctdb_call_state {
551         struct ctdb_call_state *next, *prev;
552         enum call_state state;
553         uint32_t reqid;
554         struct ctdb_req_call *c;
555         struct ctdb_db_context *ctdb_db;
556         const char *errmsg;
557         struct ctdb_call call;
558         uint32_t generation;
559         struct {
560                 void (*fn)(struct ctdb_call_state *);
561                 void *private_data;
562         } async;
563 };
564
565
566 /* used for fetch_lock */
567 struct ctdb_fetch_handle {
568         struct ctdb_db_context *ctdb_db;
569         TDB_DATA key;
570         TDB_DATA *data;
571         struct ctdb_ltdb_header header;
572 };
573
574 /*
575   operation IDs
576 */
577 enum ctdb_operation {
578         CTDB_REQ_CALL           = 0,
579         CTDB_REPLY_CALL         = 1,
580         CTDB_REQ_DMASTER        = 2,
581         CTDB_REPLY_DMASTER      = 3,
582         CTDB_REPLY_ERROR        = 4,
583         CTDB_REQ_MESSAGE        = 5,
584         /* #6 removed */
585         CTDB_REQ_CONTROL        = 7,
586         CTDB_REPLY_CONTROL      = 8,
587         CTDB_REQ_KEEPALIVE      = 9,
588 };
589
590 #define CTDB_MAGIC 0x43544442 /* CTDB */
591 #define CTDB_VERSION 1
592
593 /*
594   packet structures
595 */
596 struct ctdb_req_header {
597         uint32_t length;
598         uint32_t ctdb_magic;
599         uint32_t ctdb_version;
600         uint32_t generation;
601         uint32_t operation;
602         uint32_t destnode;
603         uint32_t srcnode;
604         uint32_t reqid;
605 };
606
607 struct ctdb_req_call {
608         struct ctdb_req_header hdr;
609         uint32_t flags;
610         uint32_t db_id;
611         uint32_t callid;
612         uint32_t hopcount;
613         uint32_t keylen;
614         uint32_t calldatalen;
615         uint8_t data[1]; /* key[] followed by calldata[] */
616 };
617
618 struct ctdb_reply_call {
619         struct ctdb_req_header hdr;
620         uint32_t status;
621         uint32_t datalen;
622         uint8_t  data[1];
623 };
624
625 struct ctdb_reply_error {
626         struct ctdb_req_header hdr;
627         uint32_t status;
628         uint32_t msglen;
629         uint8_t  msg[1];
630 };
631
632 struct ctdb_req_dmaster {
633         struct ctdb_req_header hdr;
634         uint32_t db_id;
635         uint64_t rsn;
636         uint32_t dmaster;
637         uint32_t keylen;
638         uint32_t datalen;
639         uint8_t  data[1];
640 };
641
642 struct ctdb_reply_dmaster {
643         struct ctdb_req_header hdr;
644         uint32_t db_id;
645         uint64_t rsn;
646         uint32_t keylen;
647         uint32_t datalen;
648         uint8_t  data[1];
649 };
650
651 struct ctdb_req_message {
652         struct ctdb_req_header hdr;
653         uint64_t srvid;
654         uint32_t datalen;
655         uint8_t data[1];
656 };
657
658 struct ctdb_req_getdbpath {
659         struct ctdb_req_header hdr;
660         uint32_t db_id;
661 };
662
663 struct ctdb_reply_getdbpath {
664         struct ctdb_req_header hdr;
665         uint32_t datalen;
666         uint8_t data[1];
667 };
668
669 struct ctdb_req_control {
670         struct ctdb_req_header hdr;
671         uint32_t opcode;
672         uint64_t srvid;
673         uint32_t client_id;
674 #define CTDB_CTRL_FLAG_NOREPLY   1
675         uint32_t flags;
676         uint32_t datalen;
677         uint8_t data[1];
678 };
679
680 struct ctdb_reply_control {
681         struct ctdb_req_header hdr;
682         int32_t  status;
683         uint32_t datalen;
684         uint32_t errorlen;
685         uint8_t data[1];
686 };
687
688 struct ctdb_req_keepalive {
689         struct ctdb_req_header hdr;
690 };
691
692 /* internal prototypes */
693 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
694 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
695 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
696 int ctdb_parse_address(struct ctdb_context *ctdb,
697                        TALLOC_CTX *mem_ctx, const char *str,
698                        struct ctdb_address *address);
699 uint32_t ctdb_hash(const TDB_DATA *key);
700 uint32_t ctdb_hash_string(const char *str);
701 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
702 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
703 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
704 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
705 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
706 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
707
708 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
709 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
710                     TDB_DATA key, struct ctdb_ltdb_header *header, 
711                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
712 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
713                     struct ctdb_ltdb_header *header, TDB_DATA data);
714 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
715 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
716                            TDB_DATA key, struct ctdb_req_header *hdr,
717                            void (*recv_pkt)(void *, struct ctdb_req_header *),
718                            void *recv_context, bool ignore_generation);
719 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
720                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
721                                  struct ctdb_req_header *hdr, TDB_DATA *data,
722                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
723                                  void *recv_context, bool ignore_generation);
724 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
725
726 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
727                                              struct ctdb_call *call,
728                                              struct ctdb_ltdb_header *header,
729                                              TDB_DATA *data);
730
731
732 int ctdbd_start(struct ctdb_context *ctdb);
733 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
734 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
735
736 /*
737   queue a packet for sending
738 */
739 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
740
741 /*
742   setup the fd used by the queue
743  */
744 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
745
746 /*
747   setup a packet queue on a socket
748  */
749 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
750                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
751                                     
752                                     ctdb_queue_cb_fn_t callback,
753                                     void *private_data);
754
755 /*
756   allocate a packet for use in client<->daemon communication
757  */
758 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
759                                             TALLOC_CTX *mem_ctx, 
760                                             enum ctdb_operation operation, 
761                                             size_t length, size_t slength,
762                                             const char *type);
763 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
764         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
765
766 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
767                                                  TALLOC_CTX *mem_ctx, 
768                                                  enum ctdb_operation operation, 
769                                                  size_t length, size_t slength,
770                                                  const char *type);
771 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
772         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
773
774
775 /*
776   lock a record in the ltdb, given a key
777  */
778 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
779
780 /*
781   unlock a record in the ltdb, given a key
782  */
783 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
784
785
786 /*
787   make a ctdb call to the local daemon - async send. Called from client context.
788
789   This constructs a ctdb_call request and queues it for processing. 
790   This call never blocks.
791 */
792 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
793                                               struct ctdb_call *call);
794
795 /*
796   make a recv call to the local ctdb daemon - called from client context
797
798   This is called when the program wants to wait for a ctdb_call to complete and get the 
799   results. This call will block unless the call has already completed.
800 */
801 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
802
803 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
804                              ctdb_message_fn_t handler,
805                              void *private_data);
806
807 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
808                              uint64_t srvid, TDB_DATA data);
809
810 /*
811   send a ctdb message
812 */
813 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
814                              uint64_t srvid, TDB_DATA data);
815
816
817 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
818                                       TDB_DATA key,
819                                       void (*callback)(void *), void *private_data);
820
821 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
822                                               struct ctdb_call *call);
823
824 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
825
826 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
827                                                      struct ctdb_call *call, 
828                                                      struct ctdb_ltdb_header *header);
829
830 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
831                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx, TDB_DATA *data,
832                     uint32_t caller);
833
834 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
835
836 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
837
838 int ctdb_socket_connect(struct ctdb_context *ctdb);
839
840 void ctdb_latency(double *latency, struct timeval t);
841
842 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
843 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
844 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
845
846 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
847 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
848
849 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
850                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
851                              TDB_DATA data,
852                              ctdb_control_callback_fn_t callback,
853                              void *private_data);
854
855 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
856                                TDB_DATA *outdata);
857
858 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
859                          ctdb_fn_t fn, int id);
860
861 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
862                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
863                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
864                  struct timeval *timeout, char **errormsg);
865 int ctdb_control_recv(struct ctdb_context *ctdb, 
866                 struct ctdb_client_control_state *state, 
867                 TALLOC_CTX *mem_ctx,
868                 TDB_DATA *outdata, int32_t *status, char **errormsg);
869
870 struct ctdb_client_control_state *
871 ctdb_control_send(struct ctdb_context *ctdb, 
872                 uint32_t destnode, uint64_t srvid, 
873                 uint32_t opcode, uint32_t flags, TDB_DATA data, 
874                 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
875                 struct timeval *timeout,
876                 char **errormsg);
877
878
879
880
881 #define CHECK_CONTROL_DATA_SIZE(size) do { \
882  if (indata.dsize != size) { \
883          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
884                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
885          return -1; \
886  } \
887  } while (0)
888
889 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
890 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
891 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
892 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
893 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
894
895
896 struct ctdb_traverse_start {
897         uint32_t db_id;
898         uint32_t reqid;
899         uint64_t srvid;
900 };
901
902 /*
903   structure used to pass record data between the child and parent
904  */
905 struct ctdb_rec_data {
906         uint32_t length;
907         uint32_t reqid;
908         uint32_t keylen;
909         uint32_t datalen;
910         uint8_t  data[1];
911 };
912                                    
913
914 /* structure used for pulldb control */
915 struct ctdb_control_pulldb {
916         uint32_t db_id;
917         uint32_t lmaster;
918 };
919
920 /* structure used for pulldb control */
921 struct ctdb_control_pulldb_reply {
922         uint32_t db_id;
923         uint32_t count;
924         uint8_t data[1];
925 };
926
927 /* set dmaster control structure */
928 struct ctdb_control_set_dmaster {
929         uint32_t db_id;
930         uint32_t dmaster;
931 };
932
933 /*
934   structure for setting a tunable
935  */
936 struct ctdb_control_set_tunable {
937         uint32_t value;
938         uint32_t length;
939         uint8_t  name[1];
940 };
941
942 /*
943   structure for getting a tunable
944  */
945 struct ctdb_control_get_tunable {
946         uint32_t length;
947         uint8_t  name[1];
948 };
949
950 /*
951   structure for listing tunables
952  */
953 struct ctdb_control_list_tunable {
954         uint32_t length;
955         /* returns a : separated list of tunable names */
956         uint8_t  data[1];
957 };
958
959
960 /* table that contains a list of all nodes a ctdb knows about and their 
961    status
962  */
963 struct ctdb_node_and_flags {
964         uint32_t pnn;
965         uint32_t flags;
966         struct sockaddr_in sin;
967
968 };
969
970 struct ctdb_node_map {
971         uint32_t num;
972         struct ctdb_node_and_flags nodes[1];
973 };
974
975 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
976                                     TDB_DATA *outdata, uint32_t srcnode);
977 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
978 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
979
980 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
981
982 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
983 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
984 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
985
986 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
987 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
988 int32_t ctdb_ltdb_set_seqnum_frequency(struct ctdb_context *ctdb, uint32_t frequency);
989
990 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data);
991
992 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
993 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
994 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
995
996 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
997                                  struct ctdb_req_control *c,
998                                  TDB_DATA indata, bool *async_reply,
999                                  const char **errormsg);
1000 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
1001                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
1002
1003 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1004 int32_t ctdb_control_thaw(struct ctdb_context *ctdb);
1005
1006 int ctdb_start_recoverd(struct ctdb_context *ctdb);
1007
1008 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
1009
1010 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
1011 void ctdb_start_monitoring(struct ctdb_context *ctdb);
1012 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
1013 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
1014
1015 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
1016 void ctdb_call_resend_all(struct ctdb_context *ctdb);
1017 void ctdb_node_dead(struct ctdb_node *node);
1018 void ctdb_node_connected(struct ctdb_node *node);
1019 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
1020 int32_t ctdb_control_max_rsn(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1021 int32_t ctdb_control_set_rsn_nonempty(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1022 int32_t ctdb_control_delete_low_rsn(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1023 int ctdb_ctrl_get_max_rsn(struct ctdb_context *ctdb, struct timeval timeout, 
1024                           uint32_t destnode, uint32_t db_id, uint64_t *max_rsn);
1025 int ctdb_ctrl_set_rsn_nonempty(struct ctdb_context *ctdb, struct timeval timeout, 
1026                                uint32_t destnode, uint32_t db_id, uint64_t rsn);
1027 int ctdb_ctrl_delete_low_rsn(struct ctdb_context *ctdb, struct timeval timeout, 
1028                              uint32_t destnode, uint32_t db_id, uint64_t rsn);
1029 void ctdb_set_scheduler(struct ctdb_context *ctdb);
1030 void ctdb_restore_scheduler(struct ctdb_context *ctdb);
1031 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
1032                                  struct ctdb_req_control *c,
1033                                  TDB_DATA indata, 
1034                                  bool *async_reply);
1035 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
1036                                  struct ctdb_req_control *c,
1037                                  TDB_DATA indata, 
1038                                  bool *async_reply);
1039
1040 struct ctdb_public_ip {
1041         uint32_t pnn;
1042         struct sockaddr_in sin;
1043 };
1044 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1045                           uint32_t destnode, struct ctdb_public_ip *ip);
1046 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1047                          uint32_t destnode, struct ctdb_public_ip *ip);
1048
1049 struct ctdb_all_public_ips {
1050         uint32_t num;
1051         struct ctdb_public_ip ips[1];
1052 };
1053 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1054 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb, 
1055                         struct timeval timeout, uint32_t destnode, 
1056                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1057
1058
1059 /* from takeover/system.c */
1060 int ctdb_sys_send_arp(const struct sockaddr_in *saddr, const char *iface);
1061 bool ctdb_sys_have_ip(const char *ip, bool *is_loopback, TALLOC_CTX *mem_ctx, char **ifname);
1062 int ctdb_sys_send_tcp(int fd,
1063                       const struct sockaddr_in *dest, 
1064                       const struct sockaddr_in *src,
1065                       uint32_t seq, uint32_t ack, int rst);
1066
1067 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
1068 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
1069 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir);
1070 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
1071
1072 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
1073                                 TDB_DATA indata);
1074 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata);
1075 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
1076 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
1077 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
1078 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1079 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata);
1080
1081 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
1082 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1083 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
1084                                struct timeval timeout,
1085                                TALLOC_CTX *mem_ctx,
1086                                void (*callback)(struct ctdb_context *, int, void *),
1087                                void *private_data,
1088                                const char *fmt, ...) PRINTF_ATTRIBUTE(6,7);
1089 void ctdb_release_all_ips(struct ctdb_context *ctdb);
1090
1091 void set_nonblocking(int fd);
1092 void set_close_on_exec(int fd);
1093
1094 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
1095
1096 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1097
1098 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1099                                  TDB_DATA *outdata);
1100 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1101 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1102
1103 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1104
1105 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
1106
1107 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb, 
1108                                struct timeval timeout, 
1109                                uint32_t destnode,
1110                                struct ctdb_tunable *tunables);
1111
1112 void ctdb_start_freeze(struct ctdb_context *ctdb);
1113
1114 bool parse_ip_port(const char *s, struct sockaddr_in *ip);
1115
1116 int ctdb_sys_open_capture_socket(const char *iface, void **private_data);
1117 int ctdb_sys_close_capture_socket(void *private_data);
1118 int ctdb_sys_open_sending_socket(void);
1119 int ctdb_sys_read_tcp_packet(int s, void *private_data, struct sockaddr_in *src, struct sockaddr_in *dst,
1120                              uint32_t *ack_seq, uint32_t *seq);
1121
1122 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb, 
1123                       struct timeval timeout, 
1124                       uint32_t destnode,
1125                       struct ctdb_control_killtcp *killtcp);
1126
1127 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb, 
1128                       struct timeval timeout, 
1129                       uint32_t destnode,
1130                       TALLOC_CTX *mem_ctx,
1131                       struct sockaddr_in *ip,
1132                       struct ctdb_control_tcp_tickle_list **list);
1133
1134
1135 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
1136                       uint32_t client_id,
1137                       TDB_DATA indata);
1138 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
1139                       TDB_DATA indata);
1140 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
1141                       TDB_DATA indata);
1142 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, 
1143                       TDB_DATA *outdata);
1144
1145 #endif