Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[kai/samba.git] / source4 / cluster / ctdb / tcp / tcp_init.c
1 /* 
2    ctdb over TCP
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "../tdb/include/tdb.h"
22 #include "lib/events/events.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26 #include "ctdb_tcp.h"
27
28
29 /*
30   initialise tcp portion of a ctdb node 
31 */
32 static int ctdb_tcp_add_node(struct ctdb_node *node)
33 {
34         struct ctdb_tcp *ctcp = talloc_get_type(node->ctdb->private_data,
35                                                 struct ctdb_tcp);
36         struct ctdb_tcp_node *tnode;
37         tnode = talloc_zero(ctcp, struct ctdb_tcp_node);
38         CTDB_NO_MEMORY(node->ctdb, tnode);
39
40         tnode->fd = -1;
41         node->private_data = tnode;
42
43         tnode->out_queue = ctdb_queue_setup(node->ctdb, ctcp, tnode->fd, CTDB_TCP_ALIGNMENT,
44                                         ctdb_tcp_tnode_cb, node);
45         
46         return 0;
47 }
48
49 /*
50   initialise transport structures
51 */
52 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
53 {
54         int i;
55
56         /* listen on our own address */
57         if (ctdb_tcp_listen(ctdb) != 0) return -1;
58
59         for (i=0; i<ctdb->num_nodes; i++) {
60                 if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
61                         DEBUG(0, ("methods->add_node failed at %d\n", i));
62                         return -1;
63                 }
64         }
65         
66         return 0;
67 }
68
69 /*
70   start the protocol going
71 */
72 static int ctdb_tcp_start(struct ctdb_context *ctdb)
73 {
74         int i;
75
76         /* startup connections to the other servers - will happen on
77            next event loop */
78         for (i=0;i<ctdb->num_nodes;i++) {
79                 struct ctdb_node *node = *(ctdb->nodes + i);
80                 struct ctdb_tcp_node *tnode = talloc_get_type(
81                         node->private_data, struct ctdb_tcp_node);
82                 if (!ctdb_same_address(&ctdb->address, &node->address)) {
83                         event_add_timed(ctdb->ev, tnode, timeval_zero(), 
84                                         ctdb_tcp_node_connect, node);
85                 }
86         }
87
88         return 0;
89 }
90
91
92 /*
93   shutdown the transport
94 */
95 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
96 {
97         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
98                                                 struct ctdb_tcp);
99         talloc_free(ctcp);
100         ctdb->private_data = NULL;
101 }
102
103
104 /*
105   transport packet allocator - allows transport to control memory for packets
106 */
107 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
108 {
109         /* tcp transport needs to round to 8 byte alignment to ensure
110            that we can use a length header and 64 bit elements in
111            structures */
112         size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
113         return talloc_size(mem_ctx, size);
114 }
115
116
117 static const struct ctdb_methods ctdb_tcp_methods = {
118         .initialise   = ctdb_tcp_initialise,
119         .start        = ctdb_tcp_start,
120         .queue_pkt    = ctdb_tcp_queue_pkt,
121         .add_node     = ctdb_tcp_add_node,
122         .allocate_pkt = ctdb_tcp_allocate_pkt,
123         .shutdown     = ctdb_tcp_shutdown,
124 };
125
126 /*
127   initialise tcp portion of ctdb 
128 */
129 int ctdb_tcp_init(struct ctdb_context *ctdb)
130 {
131         struct ctdb_tcp *ctcp;
132         ctcp = talloc_zero(ctdb, struct ctdb_tcp);
133         CTDB_NO_MEMORY(ctdb, ctcp);
134
135         ctcp->listen_fd = -1;
136         ctdb->private_data = ctcp;
137         ctdb->methods = &ctdb_tcp_methods;
138         return 0;
139 }
140