server: allow multiple interfaces comma separated in public_addresses
[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
26 /* location of daemon socket */
27 #define CTDB_PATH       "/tmp/ctdb.socket"
28
29 /* default ctdb port number */
30 #define CTDB_PORT 4379
31
32 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
33 #define CTDB_DS_ALIGNMENT 8
34
35
36 #define CTDB_NULL_FUNC      0xFF000001
37 #define CTDB_FETCH_FUNC     0xFF000002
38
39
40 /*
41   recovery daemon memdump reply address
42  */
43 struct rd_memdump_reply {
44         uint32_t pnn;
45         uint64_t srvid;
46 };
47
48 /*
49   description for a TAKEOVER_RUN message reply address
50  */
51 struct takeover_run_reply {
52         uint32_t pnn;
53         uint64_t srvid;
54 };
55
56 /*
57  * pid of the ctdbd daemon
58  */
59 extern pid_t ctdbd_pid;
60
61 /*
62   a tcp connection description
63  */
64 struct ctdb_tcp_connection {
65         ctdb_sock_addr src_addr;
66         ctdb_sock_addr dst_addr;
67 };
68
69 /* the wire representation for a tcp tickle array */
70 struct ctdb_tcp_wire_array {
71         uint32_t num;
72         struct ctdb_tcp_connection connections[1];
73 };      
74
75 /* the list of tcp tickles used by get/set tcp tickle list */
76 struct ctdb_control_tcp_tickle_list {
77         ctdb_sock_addr addr;
78         struct ctdb_tcp_wire_array tickles;
79 };
80
81 /*
82   array of tcp connections
83  */
84 struct ctdb_tcp_array {
85         uint32_t num;
86         struct ctdb_tcp_connection *connections;
87 };      
88
89
90 /* all tunable variables go in here */
91 struct ctdb_tunable {
92         uint32_t max_redirect_count;
93         uint32_t seqnum_interval; /* unit is ms */
94         uint32_t control_timeout;
95         uint32_t traverse_timeout;
96         uint32_t keepalive_interval;
97         uint32_t keepalive_limit;
98         uint32_t max_lacount;
99         uint32_t recover_timeout;
100         uint32_t recover_interval;
101         uint32_t election_timeout;
102         uint32_t takeover_timeout;
103         uint32_t monitor_interval;
104         uint32_t tickle_update_interval;
105         uint32_t script_timeout;
106         uint32_t script_timeout_count; /* allow dodgy scripts to hang this many times in a row before we mark the node unhealthy */
107         uint32_t script_unhealthy_on_timeout; /* obsolete */
108         uint32_t recovery_grace_period;
109         uint32_t recovery_ban_period;
110         uint32_t database_hash_size;
111         uint32_t database_max_dead;
112         uint32_t rerecovery_timeout;
113         uint32_t enable_bans;
114         uint32_t deterministic_public_ips;
115         uint32_t reclock_ping_period;
116         uint32_t no_ip_failback;
117         uint32_t verbose_memory_names;
118         uint32_t recd_ping_timeout;
119         uint32_t recd_ping_failcount;
120         uint32_t log_latency_ms;
121         uint32_t reclock_latency_ms;
122         uint32_t recovery_drop_all_ips;
123         uint32_t verify_recovery_lock;
124         uint32_t vacuum_default_interval;
125         uint32_t vacuum_max_run_time;
126         uint32_t repack_limit;
127         uint32_t vacuum_limit;
128         uint32_t vacuum_min_interval;
129         uint32_t vacuum_max_interval;
130         uint32_t max_queue_depth_drop_msg;
131         uint32_t use_status_events_for_monitoring;
132         uint32_t allow_unhealthy_db_read;
133 };
134
135 /*
136   an installed ctdb remote call
137 */
138 struct ctdb_registered_call {
139         struct ctdb_registered_call *next, *prev;
140         uint32_t id;
141         ctdb_fn_t fn;
142 };
143
144 /*
145   this address structure might need to be generalised later for some
146   transports
147 */
148 struct ctdb_address {
149         const char *address;
150         int port;
151 };
152
153 /*
154   check that a pnn is valid
155  */
156 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
157
158
159 /* called from the queue code when a packet comes in. Called with data==NULL
160    on error */
161 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
162                                    void *private_data);
163
164 /* used for callbacks in ctdb_control requests */
165 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
166                                            int32_t status, TDB_DATA data, 
167                                            const char *errormsg,
168                                            void *private_data);
169 /*
170   structure describing a connected client in the daemon
171  */
172 struct ctdb_client {
173         struct ctdb_context *ctdb;
174         int fd;
175         struct ctdb_queue *queue;
176         uint32_t client_id;
177         pid_t pid;
178         struct ctdb_tcp_list *tcp_list;
179         uint32_t db_id;
180         uint32_t num_persistent_updates;
181         struct ctdb_client_notify_list *notify;
182 };
183
184
185 /* state associated with a public ip address */
186 struct ctdb_vnn {
187         struct ctdb_vnn *prev, *next;
188
189         const char *iface;
190         const char **ifaces;
191         ctdb_sock_addr public_address;
192         uint8_t public_netmask_bits;
193
194         /* the node number that is serving this public address, if any. 
195            If no node serves this ip it is set to -1 */
196         int32_t pnn;
197
198         /* List of clients to tickle for this public address */
199         struct ctdb_tcp_array *tcp_array;
200
201         /* whether we need to update the other nodes with changes to our list
202            of connected clients */
203         bool tcp_update_needed;
204
205         /* a context to hang sending gratious arp events off */
206         TALLOC_CTX *takeover_ctx;
207
208         struct ctdb_kill_tcp *killtcp;
209 };
210
211 /*
212   state associated with one node
213 */
214 struct ctdb_node {
215         struct ctdb_context *ctdb;
216         struct ctdb_address address;
217         const char *name; /* for debug messages */
218         void *private_data; /* private to transport */
219         uint32_t pnn;
220 #define NODE_FLAGS_DISCONNECTED         0x00000001 /* node isn't connected */
221 #define NODE_FLAGS_UNHEALTHY            0x00000002 /* monitoring says node is unhealthy */
222 #define NODE_FLAGS_PERMANENTLY_DISABLED 0x00000004 /* administrator has disabled node */
223 #define NODE_FLAGS_BANNED               0x00000008 /* recovery daemon has banned the node */
224 #define NODE_FLAGS_DELETED              0x00000010 /* this node has been deleted */
225 #define NODE_FLAGS_STOPPED              0x00000020 /* this node has been stopped */
226 #define NODE_FLAGS_DISABLED             (NODE_FLAGS_UNHEALTHY|NODE_FLAGS_PERMANENTLY_DISABLED)
227 #define NODE_FLAGS_INACTIVE             (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)
228         uint32_t flags;
229
230         /* used by the dead node monitoring */
231         uint32_t dead_count;
232         uint32_t rx_cnt;
233         uint32_t tx_cnt;
234
235         /* used to track node capabilities, is only valid/tracked inside the
236            recovery daemon.
237         */
238         uint32_t capabilities;
239
240         /* a list of controls pending to this node, so we can time them out quickly
241            if the node becomes disconnected */
242         struct daemon_control_state *pending_controls;
243
244         /* used by the recovery daemon when distributing ip addresses 
245            across the nodes.  it needs to know which public ip's can be handled
246            by each node.
247         */
248         struct ctdb_all_public_ips *public_ips;
249         /* used by the recovery dameon to track when a node should be banned */
250         struct ctdb_banning_state *ban_state; 
251 };
252
253 /*
254   transport specific methods
255 */
256 struct ctdb_methods {
257         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
258         int (*start)(struct ctdb_context *); /* start the transport */
259         int (*add_node)(struct ctdb_node *); /* setup a new node */     
260         int (*connect_node)(struct ctdb_node *); /* connect to node */
261         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
262         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
263         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
264         void (*restart)(struct ctdb_node *); /* stop and restart the connection */
265 };
266
267 /*
268   transport calls up to the ctdb layer
269 */
270 struct ctdb_upcalls {
271         /* recv_pkt is called when a packet comes in */
272         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
273
274         /* node_dead is called when an attempt to send to a node fails */
275         void (*node_dead)(struct ctdb_node *);
276
277         /* node_connected is called when a connection to a node is established */
278         void (*node_connected)(struct ctdb_node *);
279 };
280
281 /* list of message handlers - needs to be changed to a more efficient data
282    structure so we can find a message handler given a srvid quickly */
283 struct ctdb_message_list {
284         struct ctdb_context *ctdb;
285         struct ctdb_message_list *next, *prev;
286         uint64_t srvid;
287         ctdb_message_fn_t message_handler;
288         void *message_private;
289 };
290
291 /* additional data required for the daemon mode */
292 struct ctdb_daemon_data {
293         int sd;
294         char *name;
295         struct ctdb_queue *queue;
296 };
297
298 /*
299   ctdb status information
300  */
301 struct ctdb_statistics {
302         uint32_t num_clients;
303         uint32_t frozen;
304         uint32_t recovering;
305         uint32_t client_packets_sent;
306         uint32_t client_packets_recv;
307         uint32_t node_packets_sent;
308         uint32_t node_packets_recv;
309         uint32_t keepalive_packets_sent;
310         uint32_t keepalive_packets_recv;
311         struct {
312                 uint32_t req_call;
313                 uint32_t reply_call;
314                 uint32_t req_dmaster;
315                 uint32_t reply_dmaster;
316                 uint32_t reply_error;
317                 uint32_t req_message;
318                 uint32_t req_control;
319                 uint32_t reply_control;
320         } node;
321         struct {
322                 uint32_t req_call;
323                 uint32_t req_message;
324                 uint32_t req_control;
325         } client;
326         struct {
327                 uint32_t call;
328                 uint32_t control;
329                 uint32_t traverse;
330         } timeouts;
331         struct {
332                 double ctdbd;
333                 double recd;
334         } reclock;
335         uint32_t total_calls;
336         uint32_t pending_calls;
337         uint32_t lockwait_calls;
338         uint32_t pending_lockwait_calls;
339         uint32_t childwrite_calls;
340         uint32_t pending_childwrite_calls;
341         uint32_t memory_used;
342         uint32_t __last_counter; /* hack for control_statistics_all */
343         uint32_t max_hop_count;
344         double max_call_latency;
345         double max_lockwait_latency;
346         double max_childwrite_latency;
347 };
348
349
350 #define INVALID_GENERATION 1
351 /* table that contains the mapping between a hash value and lmaster
352  */
353 struct ctdb_vnn_map {
354         uint32_t generation;
355         uint32_t size;
356         uint32_t *map;
357 };
358
359 /* 
360    a wire representation of the vnn map
361  */
362 struct ctdb_vnn_map_wire {
363         uint32_t generation;
364         uint32_t size;
365         uint32_t map[1];
366 };
367
368 /* a structure that contains the elements required for the write record
369    control
370 */
371 struct ctdb_write_record {
372         uint32_t dbid;
373         uint32_t keylen;
374         uint32_t datalen;
375         unsigned char blob[1];
376 };
377
378 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
379
380 #define CTDB_MONITORING_ACTIVE          0
381 #define CTDB_MONITORING_DISABLED        1
382
383 /* The different capabilities of the ctdb daemon. */
384 #define CTDB_CAP_RECMASTER              0x00000001
385 #define CTDB_CAP_LMASTER                0x00000002
386 /* This capability is set if CTDB_LVS_PUBLIC_IP is set */
387 #define CTDB_CAP_LVS                    0x00000004
388 /* This capability is set if NATGW is enabled */
389 #define CTDB_CAP_NATGW                  0x00000008
390
391 #define NUM_DB_PRIORITIES 3
392 /* main state of the ctdb daemon */
393 struct ctdb_context {
394         struct event_context *ev;
395         struct timeval ctdbd_start_time;
396         struct timeval last_recovery_started;
397         struct timeval last_recovery_finished;
398         uint32_t recovery_mode;
399         TALLOC_CTX *tickle_update_context;
400         TALLOC_CTX *keepalive_ctx;
401         struct ctdb_tunable tunable;
402         enum ctdb_freeze_mode freeze_mode[NUM_DB_PRIORITIES+1];
403         struct ctdb_freeze_handle *freeze_handles[NUM_DB_PRIORITIES+1];
404         bool freeze_transaction_started;
405         uint32_t freeze_transaction_id;
406         struct ctdb_address address;
407         const char *name;
408         const char *db_directory;
409         const char *db_directory_persistent;
410         const char *db_directory_state;
411         struct tdb_wrap *db_persistent_health;
412         uint32_t db_persistent_startup_generation;
413         uint64_t db_persistent_check_errors;
414         uint64_t max_persistent_check_errors;
415         const char *transport;
416         char *recovery_lock_file;
417         int recovery_lock_fd;
418         uint32_t pnn; /* our own pnn */
419         uint32_t num_nodes;
420         uint32_t num_connected;
421         unsigned flags;
422         uint32_t capabilities;
423         struct idr_context *idr;
424         uint16_t idr_cnt;
425         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
426         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
427         struct ctdb_vnn *single_ip_vnn; /* a structure for the single ip */
428         char *err_msg;
429         const struct ctdb_methods *methods; /* transport methods */
430         const struct ctdb_upcalls *upcalls; /* transport upcalls */
431         void *private_data; /* private to transport */
432         struct ctdb_db_context *db_list;
433         struct ctdb_message_list *message_list;
434         struct ctdb_daemon_data daemon;
435         struct ctdb_statistics statistics;
436         struct ctdb_vnn_map *vnn_map;
437         uint32_t num_clients;
438         uint32_t recovery_master;
439         struct ctdb_call_state *pending_calls;
440         struct ctdb_client_ip *client_ip_list;
441         bool do_checkpublicip;
442         struct _trbt_tree_t *server_ids;        
443         const char *event_script_dir;
444         const char *notification_script;
445         const char *default_public_interface;
446         pid_t ctdbd_pid;
447         pid_t recoverd_pid;
448         pid_t syslogd_pid;
449         bool done_startup;
450         const char *node_ip;
451         struct ctdb_monitor_state *monitor;
452         struct ctdb_log_state *log;
453         int start_as_disabled;
454         int start_as_stopped;
455         bool valgrinding;
456         uint32_t event_script_timeouts; /* counting how many consecutive times an eventscript has timedout */
457         uint32_t *recd_ping_count;
458         TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
459
460         TALLOC_CTX *event_script_ctx;
461
462         struct ctdb_event_script_state *current_monitor;
463         struct ctdb_scripts_wire *last_status[CTDB_EVENT_MAX];
464
465         TALLOC_CTX *banning_ctx;
466
467         /* mapping from pid to ctdb_client * */
468         struct ctdb_client_pid_list *client_pids;
469 };
470
471 struct ctdb_db_context {
472         struct ctdb_db_context *next, *prev;
473         struct ctdb_context *ctdb;
474         uint32_t db_id;
475         uint32_t priority;
476         bool persistent;
477         const char *db_name;
478         const char *db_path;
479         struct tdb_wrap *ltdb;
480         struct ctdb_registered_call *calls; /* list of registered calls */
481         uint32_t seqnum;
482         struct timed_event *seqnum_update;
483         struct ctdb_traverse_local_handle *traverse;
484         bool transaction_active;
485         struct ctdb_vacuum_handle *vacuum_handle;
486         char *unhealthy_reason;
487 };
488
489
490 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
491           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
492           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
493           return -1; }} while (0)
494
495 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
496           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
497           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
498           return; }} while (0)
499
500 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
501           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
502           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
503           return NULL; }} while (0)
504
505 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
506           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
507           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
508           }} while (0)
509
510 /*
511   the extended header for records in the ltdb
512 */
513 struct ctdb_ltdb_header {
514         uint64_t rsn;
515         uint32_t dmaster;
516         uint32_t laccessor;
517         uint32_t lacount;
518 };
519
520 enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0, 
521                     CTDB_CONTROL_STATISTICS              = 1, 
522                     /* #2 removed */
523                     CTDB_CONTROL_PING                    = 3,
524                     CTDB_CONTROL_GETDBPATH               = 4,
525                     CTDB_CONTROL_GETVNNMAP               = 5,
526                     CTDB_CONTROL_SETVNNMAP               = 6,
527                     CTDB_CONTROL_GET_DEBUG               = 7,
528                     CTDB_CONTROL_SET_DEBUG               = 8,
529                     CTDB_CONTROL_GET_DBMAP               = 9,
530                     CTDB_CONTROL_GET_NODEMAPv4           = 10, /* obsolete */
531                     CTDB_CONTROL_SET_DMASTER             = 11,
532                     /* #12 removed */
533                     CTDB_CONTROL_PULL_DB                 = 13,
534                     CTDB_CONTROL_PUSH_DB                 = 14,
535                     CTDB_CONTROL_GET_RECMODE             = 15,
536                     CTDB_CONTROL_SET_RECMODE             = 16,
537                     CTDB_CONTROL_STATISTICS_RESET        = 17,
538                     CTDB_CONTROL_DB_ATTACH               = 18,
539                     CTDB_CONTROL_SET_CALL                = 19,
540                     CTDB_CONTROL_TRAVERSE_START          = 20,
541                     CTDB_CONTROL_TRAVERSE_ALL            = 21,
542                     CTDB_CONTROL_TRAVERSE_DATA           = 22,
543                     CTDB_CONTROL_REGISTER_SRVID          = 23,
544                     CTDB_CONTROL_DEREGISTER_SRVID        = 24,
545                     CTDB_CONTROL_GET_DBNAME              = 25,
546                     CTDB_CONTROL_ENABLE_SEQNUM           = 26,
547                     CTDB_CONTROL_UPDATE_SEQNUM           = 27,
548                     /* #28 removed */
549                     CTDB_CONTROL_DUMP_MEMORY             = 29,
550                     CTDB_CONTROL_GET_PID                 = 30,
551                     CTDB_CONTROL_GET_RECMASTER           = 31,
552                     CTDB_CONTROL_SET_RECMASTER           = 32,
553                     CTDB_CONTROL_FREEZE                  = 33,
554                     CTDB_CONTROL_THAW                    = 34,
555                     CTDB_CONTROL_GET_PNN                 = 35,
556                     CTDB_CONTROL_SHUTDOWN                = 36,
557                     CTDB_CONTROL_GET_MONMODE             = 37,
558                     /* #38 removed */
559                     /* #39 removed */
560                     /* #40 removed */
561                     /* #41 removed */
562                     CTDB_CONTROL_TAKEOVER_IPv4           = 42, /* obsolete */
563                     CTDB_CONTROL_RELEASE_IPv4            = 43, /* obsolete */
564                     CTDB_CONTROL_TCP_CLIENT              = 44,
565                     CTDB_CONTROL_TCP_ADD                 = 45,
566                     CTDB_CONTROL_TCP_REMOVE              = 46,
567                     CTDB_CONTROL_STARTUP                 = 47,
568                     CTDB_CONTROL_SET_TUNABLE             = 48,
569                     CTDB_CONTROL_GET_TUNABLE             = 49,
570                     CTDB_CONTROL_LIST_TUNABLES           = 50,
571                     CTDB_CONTROL_GET_PUBLIC_IPSv4        = 51, /* obsolete */
572                     CTDB_CONTROL_MODIFY_FLAGS            = 52,
573                     CTDB_CONTROL_GET_ALL_TUNABLES        = 53,
574                     CTDB_CONTROL_KILL_TCP                = 54,
575                     CTDB_CONTROL_GET_TCP_TICKLE_LIST     = 55,
576                     CTDB_CONTROL_SET_TCP_TICKLE_LIST     = 56,
577                     CTDB_CONTROL_REGISTER_SERVER_ID      = 57,
578                     CTDB_CONTROL_UNREGISTER_SERVER_ID    = 58,
579                     CTDB_CONTROL_CHECK_SERVER_ID         = 59,
580                     CTDB_CONTROL_GET_SERVER_ID_LIST      = 60,
581                     CTDB_CONTROL_DB_ATTACH_PERSISTENT    = 61,
582                     CTDB_CONTROL_PERSISTENT_STORE        = 62,
583                     CTDB_CONTROL_UPDATE_RECORD           = 63,
584                     CTDB_CONTROL_SEND_GRATIOUS_ARP       = 64,
585                     CTDB_CONTROL_TRANSACTION_START       = 65,
586                     CTDB_CONTROL_TRANSACTION_COMMIT      = 66,
587                     CTDB_CONTROL_WIPE_DATABASE           = 67,
588                     /* #68 removed */
589                     CTDB_CONTROL_UPTIME                  = 69,
590                     CTDB_CONTROL_START_RECOVERY          = 70,
591                     CTDB_CONTROL_END_RECOVERY            = 71,
592                     CTDB_CONTROL_RELOAD_NODES_FILE       = 72,
593                     /* #73 removed */
594                     CTDB_CONTROL_TRY_DELETE_RECORDS      = 74,
595                     CTDB_CONTROL_ENABLE_MONITOR          = 75,
596                     CTDB_CONTROL_DISABLE_MONITOR         = 76,
597                     CTDB_CONTROL_ADD_PUBLIC_IP           = 77,
598                     CTDB_CONTROL_DEL_PUBLIC_IP           = 78,
599                     CTDB_CONTROL_RUN_EVENTSCRIPTS        = 79,
600                     CTDB_CONTROL_GET_CAPABILITIES        = 80,
601                     CTDB_CONTROL_START_PERSISTENT_UPDATE = 81,
602                     CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE= 82,
603                     CTDB_CONTROL_TRANS2_COMMIT           = 83,
604                     CTDB_CONTROL_TRANS2_FINISHED         = 84,
605                     CTDB_CONTROL_TRANS2_ERROR            = 85,
606                     CTDB_CONTROL_TRANS2_COMMIT_RETRY     = 86,
607                     CTDB_CONTROL_RECD_PING               = 87,
608                     CTDB_CONTROL_RELEASE_IP              = 88,
609                     CTDB_CONTROL_TAKEOVER_IP             = 89,
610                     CTDB_CONTROL_GET_PUBLIC_IPS          = 90,
611                     CTDB_CONTROL_GET_NODEMAP             = 91,
612                     CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS = 96,
613                     CTDB_CONTROL_TRAVERSE_KILL           = 97,
614                     CTDB_CONTROL_RECD_RECLOCK_LATENCY    = 98,
615                     CTDB_CONTROL_GET_RECLOCK_FILE        = 99,
616                     CTDB_CONTROL_SET_RECLOCK_FILE        = 100,
617                     CTDB_CONTROL_STOP_NODE               = 101,
618                     CTDB_CONTROL_CONTINUE_NODE           = 102,
619                     CTDB_CONTROL_SET_NATGWSTATE          = 103,
620                     CTDB_CONTROL_SET_LMASTERROLE         = 104,
621                     CTDB_CONTROL_SET_RECMASTERROLE       = 105,
622                     CTDB_CONTROL_ENABLE_SCRIPT           = 107,
623                     CTDB_CONTROL_DISABLE_SCRIPT          = 108,
624                     CTDB_CONTROL_SET_BAN_STATE           = 109,
625                     CTDB_CONTROL_GET_BAN_STATE           = 110,
626                     CTDB_CONTROL_SET_DB_PRIORITY         = 111,
627                     CTDB_CONTROL_GET_DB_PRIORITY         = 112,
628                     CTDB_CONTROL_TRANSACTION_CANCEL      = 113,
629                     CTDB_CONTROL_REGISTER_NOTIFY         = 114,
630                     CTDB_CONTROL_DEREGISTER_NOTIFY       = 115,
631                     CTDB_CONTROL_TRANS2_ACTIVE           = 116,
632                     CTDB_CONTROL_GET_LOG                 = 117,
633                     CTDB_CONTROL_CLEAR_LOG               = 118,
634                     CTDB_CONTROL_TRANS3_COMMIT           = 119,
635                     CTDB_CONTROL_GET_DB_SEQNUM           = 120,
636                     CTDB_CONTROL_DB_SET_HEALTHY          = 121,
637                     CTDB_CONTROL_DB_GET_HEALTH           = 122,
638 };      
639
640 /*
641   structure passed in set_call control
642  */
643 struct ctdb_control_set_call {
644         uint32_t db_id;
645         ctdb_fn_t fn;
646         uint32_t id;
647 };
648
649 /*
650   struct for kill_tcp control
651  */
652 struct ctdb_control_killtcp {
653         ctdb_sock_addr src_addr;
654         ctdb_sock_addr dst_addr;
655 };
656
657 /*
658   struct holding a ctdb_sock_addr and an interface name,
659   used to add/remove public addresses
660  */
661 struct ctdb_control_ip_iface {
662         ctdb_sock_addr addr;
663         uint32_t mask;
664         uint32_t len;
665         char iface[1];
666 };
667
668 /*
669   struct holding a ctdb_sock_addr and an interface name,
670   used for send_gratious_arp
671  */
672 struct ctdb_control_gratious_arp {
673         ctdb_sock_addr addr;
674         uint32_t mask;
675         uint32_t len;
676         char iface[1];
677 };
678
679 /*
680   struct for tcp_add and tcp_remove controls
681  */
682 struct ctdb_control_tcp_vnn {
683         ctdb_sock_addr src;
684         ctdb_sock_addr dest;
685 };
686
687 /*
688   persistent store control - update this record on all other nodes
689  */
690 struct ctdb_control_persistent_store {
691         uint32_t db_id;
692         uint32_t len;
693         uint8_t  data[1];
694 };
695
696 /*
697   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
698  */
699 struct ctdb_node_flag_change {
700         uint32_t pnn;
701         uint32_t new_flags;
702         uint32_t old_flags;
703 };
704
705 /*
706   struct for admin setting a ban
707  */
708 struct ctdb_ban_info {
709         uint32_t pnn;
710         uint32_t ban_time;
711 };
712
713 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
714
715 #define CTDB_LMASTER_ANY        0xffffffff
716
717 /*
718   state of a in-progress ctdb call
719 */
720 struct ctdb_call_state {
721         struct ctdb_call_state *next, *prev;
722         enum call_state state;
723         uint32_t reqid;
724         struct ctdb_req_call *c;
725         struct ctdb_db_context *ctdb_db;
726         const char *errmsg;
727         struct ctdb_call *call;
728         uint32_t generation;
729         struct {
730                 void (*fn)(struct ctdb_call_state *);
731                 void *private_data;
732         } async;
733 };
734
735
736 /* used for fetch_lock */
737 struct ctdb_fetch_handle {
738         struct ctdb_db_context *ctdb_db;
739         TDB_DATA key;
740         TDB_DATA *data;
741         struct ctdb_ltdb_header header;
742 };
743
744 /*
745   operation IDs
746 */
747 enum ctdb_operation {
748         CTDB_REQ_CALL           = 0,
749         CTDB_REPLY_CALL         = 1,
750         CTDB_REQ_DMASTER        = 2,
751         CTDB_REPLY_DMASTER      = 3,
752         CTDB_REPLY_ERROR        = 4,
753         CTDB_REQ_MESSAGE        = 5,
754         /* #6 removed */
755         CTDB_REQ_CONTROL        = 7,
756         CTDB_REPLY_CONTROL      = 8,
757         CTDB_REQ_KEEPALIVE      = 9,
758 };
759
760 #define CTDB_MAGIC 0x43544442 /* CTDB */
761 #define CTDB_VERSION 1
762
763 /*
764   packet structures
765 */
766 struct ctdb_req_header {
767         uint32_t length;
768         uint32_t ctdb_magic;
769         uint32_t ctdb_version;
770         uint32_t generation;
771         uint32_t operation;
772         uint32_t destnode;
773         uint32_t srcnode;
774         uint32_t reqid;
775 };
776
777 struct ctdb_req_call {
778         struct ctdb_req_header hdr;
779         uint32_t flags;
780         uint32_t db_id;
781         uint32_t callid;
782         uint32_t hopcount;
783         uint32_t keylen;
784         uint32_t calldatalen;
785         uint8_t data[1]; /* key[] followed by calldata[] */
786 };
787
788 struct ctdb_reply_call {
789         struct ctdb_req_header hdr;
790         uint32_t status;
791         uint32_t datalen;
792         uint8_t  data[1];
793 };
794
795 struct ctdb_reply_error {
796         struct ctdb_req_header hdr;
797         uint32_t status;
798         uint32_t msglen;
799         uint8_t  msg[1];
800 };
801
802 struct ctdb_req_dmaster {
803         struct ctdb_req_header hdr;
804         uint32_t db_id;
805         uint64_t rsn;
806         uint32_t dmaster;
807         uint32_t keylen;
808         uint32_t datalen;
809         uint8_t  data[1];
810 };
811
812 struct ctdb_reply_dmaster {
813         struct ctdb_req_header hdr;
814         uint32_t db_id;
815         uint64_t rsn;
816         uint32_t keylen;
817         uint32_t datalen;
818         uint8_t  data[1];
819 };
820
821 struct ctdb_req_message {
822         struct ctdb_req_header hdr;
823         uint64_t srvid;
824         uint32_t datalen;
825         uint8_t data[1];
826 };
827
828 struct ctdb_req_getdbpath {
829         struct ctdb_req_header hdr;
830         uint32_t db_id;
831 };
832
833 struct ctdb_reply_getdbpath {
834         struct ctdb_req_header hdr;
835         uint32_t datalen;
836         uint8_t data[1];
837 };
838
839 struct ctdb_req_control {
840         struct ctdb_req_header hdr;
841         uint32_t opcode;
842         uint64_t srvid;
843         uint32_t client_id;
844 #define CTDB_CTRL_FLAG_NOREPLY   1
845         uint32_t flags;
846         uint32_t datalen;
847         uint8_t data[1];
848 };
849
850 struct ctdb_reply_control {
851         struct ctdb_req_header hdr;
852         int32_t  status;
853         uint32_t datalen;
854         uint32_t errorlen;
855         uint8_t data[1];
856 };
857
858 struct ctdb_req_keepalive {
859         struct ctdb_req_header hdr;
860 };
861
862
863 /* types of failures possible from TRANS2_COMMIT */
864 enum ctdb_trans2_commit_error {
865         CTDB_TRANS2_COMMIT_SUCCESS=0, /* all nodes committed successfully */
866         CTDB_TRANS2_COMMIT_TIMEOUT=1, /* at least one node timed out */
867         CTDB_TRANS2_COMMIT_ALLFAIL=2, /* all nodes failed the commit */
868         CTDB_TRANS2_COMMIT_SOMEFAIL=3 /* some nodes failed the commit, some allowed it */
869 };
870
871 /* internal prototypes */
872 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
873 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
874 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
875 int ctdb_parse_address(struct ctdb_context *ctdb,
876                        TALLOC_CTX *mem_ctx, const char *str,
877                        struct ctdb_address *address);
878 bool ctdb_same_ip(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
879 bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
880 uint32_t ctdb_hash(const TDB_DATA *key);
881 uint32_t ctdb_hash_string(const char *str);
882 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
883 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
884 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
885 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
886 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
887 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
888
889 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
890 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
891                     TDB_DATA key, struct ctdb_ltdb_header *header, 
892                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
893 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
894                     struct ctdb_ltdb_header *header, TDB_DATA data);
895 int32_t ctdb_control_start_persistent_update(struct ctdb_context *ctdb, 
896                         struct ctdb_req_control *c,
897                         TDB_DATA recdata);
898 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context *ctdb, 
899                         struct ctdb_req_control *c,
900                         TDB_DATA recdata);
901 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
902 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode);
903 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
904                            TDB_DATA key, struct ctdb_req_header *hdr,
905                            void (*recv_pkt)(void *, struct ctdb_req_header *),
906                            void *recv_context, bool ignore_generation);
907 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
908                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
909                                  struct ctdb_req_header *hdr, TDB_DATA *data,
910                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
911                                  void *recv_context, bool ignore_generation);
912 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
913
914 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
915                                              struct ctdb_call *call,
916                                              struct ctdb_ltdb_header *header,
917                                              TDB_DATA *data);
918
919
920 int ctdbd_start(struct ctdb_context *ctdb);
921 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
922 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
923
924 /*
925   queue a packet for sending
926 */
927 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
928
929 /*
930   setup the fd used by the queue
931  */
932 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
933
934 /*
935   setup a packet queue on a socket
936  */
937 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
938                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
939                                     
940                                     ctdb_queue_cb_fn_t callback,
941                                     void *private_data);
942
943 /*
944   allocate a packet for use in client<->daemon communication
945  */
946 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
947                                             TALLOC_CTX *mem_ctx, 
948                                             enum ctdb_operation operation, 
949                                             size_t length, size_t slength,
950                                             const char *type);
951 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
952         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
953
954 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
955                                                  TALLOC_CTX *mem_ctx, 
956                                                  enum ctdb_operation operation, 
957                                                  size_t length, size_t slength,
958                                                  const char *type);
959 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
960         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
961
962 int ctdb_queue_length(struct ctdb_queue *queue);
963
964 /*
965   lock a record in the ltdb, given a key
966  */
967 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
968
969 /*
970   unlock a record in the ltdb, given a key
971  */
972 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
973
974
975 /*
976   make a ctdb call to the local daemon - async send. Called from client context.
977
978   This constructs a ctdb_call request and queues it for processing. 
979   This call never blocks.
980 */
981 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
982                                               struct ctdb_call *call);
983
984 /*
985   make a recv call to the local ctdb daemon - called from client context
986
987   This is called when the program wants to wait for a ctdb_call to complete and get the 
988   results. This call will block unless the call has already completed.
989 */
990 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
991
992 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
993                              ctdb_message_fn_t handler,
994                              void *private_data);
995
996 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
997                              uint64_t srvid, TDB_DATA data);
998
999 /*
1000   send a ctdb message
1001 */
1002 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
1003                              uint64_t srvid, TDB_DATA data);
1004
1005
1006 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
1007                                       TDB_DATA key,
1008                                       void (*callback)(void *), void *private_data);
1009
1010 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
1011                                               struct ctdb_call *call);
1012
1013 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
1014
1015 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
1016                                                      struct ctdb_call *call, 
1017                                                      struct ctdb_ltdb_header *header);
1018
1019 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
1020                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx, TDB_DATA *data,
1021                     uint32_t caller);
1022
1023 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
1024
1025 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
1026
1027 int ctdb_socket_connect(struct ctdb_context *ctdb);
1028
1029 void ctdb_latency(struct ctdb_db_context *ctdb_db, const char *name, double *latency, struct timeval t);
1030 void ctdb_reclock_latency(struct ctdb_context *ctdb, const char *name, double *latency, double l);
1031
1032 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
1033 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
1034 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
1035
1036 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
1037 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
1038
1039 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
1040                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
1041                              TDB_DATA data,
1042                              ctdb_control_callback_fn_t callback,
1043                              void *private_data);
1044
1045 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
1046                                TDB_DATA *outdata, uint64_t tdb_flags, bool persistent);
1047
1048 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
1049                          ctdb_fn_t fn, int id);
1050
1051 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
1052                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
1053                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
1054                  struct timeval *timeout, char **errormsg);
1055 int ctdb_control_recv(struct ctdb_context *ctdb, 
1056                 struct ctdb_client_control_state *state, 
1057                 TALLOC_CTX *mem_ctx,
1058                 TDB_DATA *outdata, int32_t *status, char **errormsg);
1059
1060 struct ctdb_client_control_state *
1061 ctdb_control_send(struct ctdb_context *ctdb, 
1062                 uint32_t destnode, uint64_t srvid, 
1063                 uint32_t opcode, uint32_t flags, TDB_DATA data, 
1064                 TALLOC_CTX *mem_ctx,
1065                 struct timeval *timeout,
1066                 char **errormsg);
1067
1068
1069
1070
1071 #define CHECK_CONTROL_DATA_SIZE(size) do { \
1072  if (indata.dsize != size) { \
1073          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
1074                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
1075          return -1; \
1076  } \
1077  } while (0)
1078
1079 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1080 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1081 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1082 int ctdb_control_getnodemapv4(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1083 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1084 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1085
1086
1087 struct ctdb_traverse_start {
1088         uint32_t db_id;
1089         uint32_t reqid;
1090         uint64_t srvid;
1091 };
1092
1093 /*
1094   structure used to pass record data between the child and parent
1095  */
1096 struct ctdb_rec_data {
1097         uint32_t length;
1098         uint32_t reqid;
1099         uint32_t keylen;
1100         uint32_t datalen;
1101         uint8_t  data[1];
1102 };
1103                                    
1104
1105 /* structure used for pulldb control */
1106 struct ctdb_control_pulldb {
1107         uint32_t db_id;
1108         uint32_t lmaster;
1109 };
1110
1111 /* structure used for sending lists of records */
1112 struct ctdb_marshall_buffer {
1113         uint32_t db_id;
1114         uint32_t count;
1115         uint8_t data[1];
1116 };
1117
1118 /* set dmaster control structure */
1119 struct ctdb_control_set_dmaster {
1120         uint32_t db_id;
1121         uint32_t dmaster;
1122 };
1123
1124 /*
1125   structure for setting a tunable
1126  */
1127 struct ctdb_control_set_tunable {
1128         uint32_t value;
1129         uint32_t length;
1130         uint8_t  name[1];
1131 };
1132
1133 /*
1134   structure for getting a tunable
1135  */
1136 struct ctdb_control_get_tunable {
1137         uint32_t length;
1138         uint8_t  name[1];
1139 };
1140
1141 /*
1142   structure for listing tunables
1143  */
1144 struct ctdb_control_list_tunable {
1145         uint32_t length;
1146         /* returns a : separated list of tunable names */
1147         uint8_t  data[1];
1148 };
1149
1150
1151 /* table that contains a list of all nodes a ctdb knows about and their 
1152    status
1153  */
1154 struct ctdb_node_and_flags {
1155         uint32_t pnn;
1156         uint32_t flags;
1157         ctdb_sock_addr addr;
1158 };
1159
1160 struct ctdb_node_map {
1161         uint32_t num;
1162         struct ctdb_node_and_flags nodes[1];
1163 };
1164
1165 struct ctdb_node_and_flagsv4 {
1166         uint32_t pnn;
1167         uint32_t flags;
1168         struct sockaddr_in sin;
1169 };
1170
1171 struct ctdb_node_mapv4 {
1172         uint32_t num;
1173         struct ctdb_node_and_flagsv4 nodes[1];
1174 };
1175
1176 struct ctdb_control_wipe_database {
1177         uint32_t db_id;
1178         uint32_t transaction_id;
1179 };
1180
1181 /*
1182   state of a in-progress ctdb call in client
1183 */
1184 struct ctdb_client_call_state {
1185         enum call_state state;
1186         uint32_t reqid;
1187         struct ctdb_db_context *ctdb_db;
1188         struct ctdb_call *call;
1189         struct {
1190                 void (*fn)(struct ctdb_client_call_state *);
1191                 void *private_data;
1192         } async;
1193 };
1194
1195
1196 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
1197                                     TDB_DATA *outdata, uint32_t srcnode, uint32_t client_id);
1198 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
1199 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
1200 int32_t ctdb_control_traverse_kill(struct ctdb_context *ctdb, TDB_DATA indata, 
1201                                     TDB_DATA *outdata, uint32_t srcnode);
1202
1203 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
1204
1205 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
1206 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
1207 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
1208
1209 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
1210 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
1211
1212 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
1213                                            TDB_DATA key, struct ctdb_ltdb_header *, TDB_DATA data);
1214
1215 struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
1216                                               uint32_t *reqid,
1217                                               struct ctdb_ltdb_header *header,
1218                                               TDB_DATA *key, TDB_DATA *data);
1219
1220 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1221 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
1222 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
1223
1224 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
1225                                  struct ctdb_req_control *c,
1226                                  TDB_DATA indata, bool *async_reply,
1227                                  const char **errormsg);
1228 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
1229                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
1230
1231 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1232 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority);
1233
1234 int ctdb_start_recoverd(struct ctdb_context *ctdb);
1235 void ctdb_stop_recoverd(struct ctdb_context *ctdb);
1236
1237 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
1238
1239 void ctdb_disable_monitoring(struct ctdb_context *ctdb);
1240 void ctdb_enable_monitoring(struct ctdb_context *ctdb);
1241 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
1242 void ctdb_start_monitoring(struct ctdb_context *ctdb);
1243 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
1244 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
1245 void ctdb_start_keepalive(struct ctdb_context *ctdb);
1246 void ctdb_stop_keepalive(struct ctdb_context *ctdb);
1247 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA data, bool *async_reply);
1248
1249
1250 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
1251 void ctdb_call_resend_all(struct ctdb_context *ctdb);
1252 void ctdb_node_dead(struct ctdb_node *node);
1253 void ctdb_node_connected(struct ctdb_node *node);
1254 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
1255 void ctdb_high_priority(struct ctdb_context *ctdb);
1256 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
1257                                  struct ctdb_req_control *c,
1258                                  TDB_DATA indata, 
1259                                  bool *async_reply);
1260 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb, 
1261                                  struct ctdb_req_control *c,
1262                                  TDB_DATA indata, 
1263                                  bool *async_reply);
1264 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
1265                                  struct ctdb_req_control *c,
1266                                  TDB_DATA indata, 
1267                                  bool *async_reply);
1268 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb, 
1269                                  struct ctdb_req_control *c,
1270                                  TDB_DATA indata, 
1271                                  bool *async_reply);
1272 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
1273                                  struct ctdb_req_control *c,
1274                                  bool *async_reply);
1275 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
1276                                  struct ctdb_req_control *c,
1277                                  bool *async_reply);
1278
1279 struct ctdb_public_ipv4 {
1280         uint32_t pnn;
1281         struct sockaddr_in sin;
1282 };
1283
1284 struct ctdb_public_ip {
1285         uint32_t pnn;
1286         ctdb_sock_addr addr;
1287 };
1288 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1289                           uint32_t destnode, struct ctdb_public_ip *ip);
1290 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1291                          uint32_t destnode, struct ctdb_public_ip *ip);
1292
1293 struct ctdb_all_public_ipsv4 {
1294         uint32_t num;
1295         struct ctdb_public_ipv4 ips[1];
1296 };
1297
1298 struct ctdb_all_public_ips {
1299         uint32_t num;
1300         struct ctdb_public_ip ips[1];
1301 };
1302 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1303 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1304 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb, 
1305                         struct timeval timeout, uint32_t destnode, 
1306                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1307 int ctdb_ctrl_get_public_ipsv4(struct ctdb_context *ctdb, 
1308                         struct timeval timeout, uint32_t destnode, 
1309                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1310
1311
1312 /* from takeover/system.c */
1313 uint32_t uint16_checksum(uint16_t *data, size_t n);
1314 int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface);
1315 bool ctdb_sys_have_ip(ctdb_sock_addr *addr);
1316 int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, 
1317                       const ctdb_sock_addr *src,
1318                       uint32_t seq, uint32_t ack, int rst);
1319
1320 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
1321 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1322                               const char *iface,
1323                               const char *ip);
1324 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
1325 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir);
1326 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script);
1327 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
1328
1329 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
1330                                 TDB_DATA indata);
1331 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata);
1332 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
1333 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
1334 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
1335 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata);
1336 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1337 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata);
1338
1339 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
1340 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call);
1341 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
1342                            const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
1343 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
1344                                TALLOC_CTX *mem_ctx,
1345                                void (*callback)(struct ctdb_context *, int, void *),
1346                                void *private_data,
1347                                bool from_user,
1348                                enum ctdb_eventscript_call call,
1349                                const char *fmt, ...) PRINTF_ATTRIBUTE(7,8);
1350 void ctdb_release_all_ips(struct ctdb_context *ctdb);
1351
1352 void set_nonblocking(int fd);
1353 void set_close_on_exec(int fd);
1354
1355 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
1356
1357 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1358
1359 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1360                                  TDB_DATA *outdata);
1361 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1362 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1363 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1364 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1365 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1366
1367 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1368
1369 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
1370
1371 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb, 
1372                                struct timeval timeout, 
1373                                uint32_t destnode,
1374                                struct ctdb_tunable *tunables);
1375
1376 int ctdb_start_freeze(struct ctdb_context *ctdb, uint32_t priority);
1377
1378 bool parse_ip_mask(const char *s, const char *iface, ctdb_sock_addr *addr, unsigned *mask);
1379 bool parse_ip_port(const char *s, ctdb_sock_addr *addr);
1380 bool parse_ip(const char *s, const char *iface, unsigned port, ctdb_sock_addr *addr);
1381 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin);
1382  
1383
1384 int ctdb_sys_open_capture_socket(const char *iface, void **private_data);
1385 int ctdb_sys_close_capture_socket(void *private_data);
1386 int ctdb_sys_read_tcp_packet(int s, void *private_data, ctdb_sock_addr *src, ctdb_sock_addr *dst, uint32_t *ack_seq, uint32_t *seq);
1387
1388 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb, 
1389                       struct timeval timeout, 
1390                       uint32_t destnode,
1391                       struct ctdb_control_killtcp *killtcp);
1392
1393 int ctdb_ctrl_add_public_ip(struct ctdb_context *ctdb, 
1394                       struct timeval timeout, 
1395                       uint32_t destnode,
1396                       struct ctdb_control_ip_iface *pub);
1397
1398 int ctdb_ctrl_del_public_ip(struct ctdb_context *ctdb, 
1399                       struct timeval timeout, 
1400                       uint32_t destnode,
1401                       struct ctdb_control_ip_iface *pub);
1402
1403 int ctdb_ctrl_gratious_arp(struct ctdb_context *ctdb, 
1404                       struct timeval timeout, 
1405                       uint32_t destnode,
1406                       ctdb_sock_addr *addr,
1407                       const char *ifname);
1408
1409 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb, 
1410                       struct timeval timeout, 
1411                       uint32_t destnode,
1412                       TALLOC_CTX *mem_ctx,
1413                       ctdb_sock_addr *addr,
1414                       struct ctdb_control_tcp_tickle_list **list);
1415
1416
1417 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
1418                       uint32_t client_id,
1419                       TDB_DATA indata);
1420 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
1421                       TDB_DATA indata);
1422 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
1423                       TDB_DATA indata);
1424 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, 
1425                       TDB_DATA *outdata);
1426 int32_t ctdb_control_uptime(struct ctdb_context *ctdb, 
1427                       TDB_DATA *outdata);
1428
1429 int ctdb_attach_databases(struct ctdb_context *ctdb);
1430
1431 int32_t ctdb_control_persistent_store(struct ctdb_context *ctdb, 
1432                                       struct ctdb_req_control *c, 
1433                                       TDB_DATA recdata, bool *async_reply);
1434 int32_t ctdb_control_update_record(struct ctdb_context *ctdb, 
1435                                    struct ctdb_req_control *c, TDB_DATA recdata, 
1436                                    bool *async_reply);
1437 int32_t ctdb_control_trans2_commit(struct ctdb_context *ctdb, 
1438                                    struct ctdb_req_control *c, 
1439                                    TDB_DATA recdata, bool *async_reply);
1440
1441 int32_t ctdb_control_trans3_commit(struct ctdb_context *ctdb,
1442                                    struct ctdb_req_control *c,
1443                                    TDB_DATA recdata, bool *async_reply);
1444
1445 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
1446 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
1447 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb);
1448 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
1449 int32_t ctdb_control_db_set_healthy(struct ctdb_context *ctdb, TDB_DATA indata);
1450 int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
1451                                    TDB_DATA indata,
1452                                    TDB_DATA *outdata);
1453
1454
1455 int ctdb_vacuum(struct ctdb_context *ctdb, int argc, const char **argv);
1456 int ctdb_repack(struct ctdb_context *ctdb, int argc, const char **argv);
1457
1458 void ctdb_block_signal(int signum);
1459 void ctdb_unblock_signal(int signum);
1460 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb);
1461 int ctdb_set_child_logging(struct ctdb_context *ctdb);
1462 void ctdb_lockdown_memory(struct ctdb_context *ctdb);
1463
1464 typedef void (*client_async_callback)(struct ctdb_context *ctdb, uint32_t node_pnn, int32_t res, TDB_DATA outdata, void *callback_data);
1465
1466 struct client_async_data {
1467         enum ctdb_controls opcode;
1468         bool dont_log_errors;
1469         uint32_t count;
1470         uint32_t fail_count;
1471         client_async_callback callback;
1472         client_async_callback fail_callback;
1473         void *callback_data;
1474 };
1475 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state);
1476 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data);
1477 int ctdb_client_async_control(struct ctdb_context *ctdb,
1478                                 enum ctdb_controls opcode,
1479                                 uint32_t *nodes,
1480                                 uint64_t srvid,
1481                                 struct timeval timeout,
1482                                 bool dont_log_errors,
1483                                 TDB_DATA data,
1484                                 client_async_callback client_callback,
1485                                 client_async_callback fail_callback,
1486                                 void *callback_data);
1487
1488 void ctdb_load_nodes_file(struct ctdb_context *ctdb);
1489
1490 int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);
1491
1492 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata);
1493 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata);
1494
1495 int32_t ctdb_control_trans2_finished(struct ctdb_context *ctdb, 
1496                                      struct ctdb_req_control *c);
1497 int32_t ctdb_control_trans2_error(struct ctdb_context *ctdb, 
1498                                   struct ctdb_req_control *c);
1499 int32_t ctdb_control_trans2_active(struct ctdb_context *ctdb,
1500                                    struct ctdb_req_control *c,
1501                                    uint32_t db_id);
1502
1503 char *ctdb_addr_to_str(ctdb_sock_addr *addr);
1504 unsigned ctdb_addr_to_port(ctdb_sock_addr *addr);
1505 void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip);
1506
1507 int32_t ctdb_control_recd_ping(struct ctdb_context *ctdb);
1508 int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata);
1509
1510 extern int script_log_level;
1511
1512 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
1513                                              uint32_t call_type,
1514                                              TDB_DATA *outdata);
1515
1516 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len);
1517 int ctdb_ctrl_report_recd_lock_latency(struct ctdb_context *ctdb, struct timeval timeout, double latency);
1518
1519 int32_t ctdb_control_stop_node(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1520 int32_t ctdb_control_continue_node(struct ctdb_context *ctdb);
1521
1522 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db);
1523
1524 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1525 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1526
1527 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata);
1528 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata);
1529 int32_t ctdb_control_set_db_priority(struct ctdb_context *ctdb, TDB_DATA indata);
1530 void ctdb_ban_self(struct ctdb_context *ctdb);
1531
1532 int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1533
1534 int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1535
1536 int start_syslog_daemon(struct ctdb_context *ctdb);
1537
1538 /* Where to send the log messages back to */
1539 struct ctdb_get_log_addr {
1540         uint32_t pnn;
1541         uint64_t srvid;
1542         int32_t level;
1543 };
1544
1545 extern int log_ringbuf_size;
1546
1547 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr);
1548 int32_t ctdb_control_clear_log(struct ctdb_context *ctdb);
1549
1550
1551 struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
1552                                               struct ctdb_context *ctdb,
1553                                               void (*logfn)(const char *, uint16_t, void *),
1554                                               void *logfn_private, pid_t *pid);
1555
1556 int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid);
1557 struct ctdb_client *ctdb_find_client_by_pid(struct ctdb_context *ctdb, pid_t pid);
1558
1559 int32_t ctdb_control_get_db_seqnum(struct ctdb_context *ctdb,
1560                                    TDB_DATA indata,
1561                                    TDB_DATA *outdata);
1562
1563 int ctdb_load_persistent_health(struct ctdb_context *ctdb,
1564                                 struct ctdb_db_context *ctdb_db);
1565 int ctdb_update_persistent_health(struct ctdb_context *ctdb,
1566                                   struct ctdb_db_context *ctdb_db,
1567                                   const char *reason,/* NULL means healthy */
1568                                   int num_healthy_nodes);
1569 int ctdb_recheck_persistent_health(struct ctdb_context *ctdb);
1570
1571 void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event);
1572
1573 void ctdb_fault_setup(void);
1574 #endif