new version 1.3.2
[sahlberg/ctdb.git] / 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 "lib/tdb/include/tdb.h"
22 #include "lib/tevent/tevent.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26 #include "ctdb_tcp.h"
27
28 static int tnode_destructor(struct ctdb_tcp_node *tnode)
29 {
30   //    struct ctdb_node *node = talloc_find_parent_bytype(tnode, struct ctdb_node);
31
32         if (tnode->fd != -1) {
33                 close(tnode->fd);
34                 tnode->fd = -1;
35         }
36
37         return 0;
38 }
39
40 /*
41   initialise tcp portion of a ctdb node 
42 */
43 static int ctdb_tcp_add_node(struct ctdb_node *node)
44 {
45         struct ctdb_tcp_node *tnode;
46         tnode = talloc_zero(node, struct ctdb_tcp_node);
47         CTDB_NO_MEMORY(node->ctdb, tnode);
48
49         tnode->fd = -1;
50         node->private_data = tnode;
51         talloc_set_destructor(tnode, tnode_destructor);
52
53         tnode->out_queue = ctdb_queue_setup(node->ctdb, node, tnode->fd, CTDB_TCP_ALIGNMENT,
54                                             ctdb_tcp_tnode_cb, node, "to-node-%s", node->name);
55         
56         return 0;
57 }
58
59 /*
60   initialise transport structures
61 */
62 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
63 {
64         int i;
65
66         /* listen on our own address */
67         if (ctdb_tcp_listen(ctdb) != 0) {
68                 DEBUG(DEBUG_CRIT, (__location__ " Failed to start listening on the CTDB socket\n"));
69                 exit(1);
70         }
71
72         for (i=0; i < ctdb->num_nodes; i++) {
73                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
74                         continue;
75                 }
76                 if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
77                         DEBUG(DEBUG_CRIT, ("methods->add_node failed at %d\n", i));
78                         return -1;
79                 }
80         }
81         
82         return 0;
83 }
84
85 /*
86   start the protocol going
87 */
88 static int ctdb_tcp_connect_node(struct ctdb_node *node)
89 {
90         struct ctdb_context *ctdb = node->ctdb;
91         struct ctdb_tcp_node *tnode = talloc_get_type(
92                 node->private_data, struct ctdb_tcp_node);
93
94         /* startup connection to the other server - will happen on
95            next event loop */
96         if (!ctdb_same_address(&ctdb->address, &node->address)) {
97                 tnode->connect_te = event_add_timed(ctdb->ev, tnode, 
98                                                     timeval_zero(), 
99                                                     ctdb_tcp_node_connect, node);
100         }
101
102         return 0;
103 }
104
105 /*
106   shutdown and try to restart a connection to a node after it has been
107   disconnected
108 */
109 static void ctdb_tcp_restart(struct ctdb_node *node)
110 {
111         struct ctdb_tcp_node *tnode = talloc_get_type(
112                 node->private_data, struct ctdb_tcp_node);
113
114         DEBUG(DEBUG_NOTICE,("Tearing down connection to dead node :%d\n", node->pnn));
115
116         ctdb_tcp_stop_connection(node);
117
118         tnode->connect_te = event_add_timed(node->ctdb->ev, tnode, timeval_zero(), 
119                                             ctdb_tcp_node_connect, node);
120 }
121
122
123 /*
124   shutdown the transport
125 */
126 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
127 {
128         struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
129                                                 struct ctdb_tcp);
130         talloc_free(ctcp);
131         ctdb->private_data = NULL;
132 }
133
134 /*
135   start the transport
136 */
137 static int ctdb_tcp_start(struct ctdb_context *ctdb)
138 {
139         int i;
140
141         for (i=0; i < ctdb->num_nodes; i++) {
142                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
143                         continue;
144                 }
145                 ctdb_tcp_connect_node(ctdb->nodes[i]);
146         }
147
148         return 0;
149 }
150
151
152 /*
153   transport packet allocator - allows transport to control memory for packets
154 */
155 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
156 {
157         /* tcp transport needs to round to 8 byte alignment to ensure
158            that we can use a length header and 64 bit elements in
159            structures */
160         size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
161         return talloc_size(mem_ctx, size);
162 }
163
164
165 static const struct ctdb_methods ctdb_tcp_methods = {
166         .initialise   = ctdb_tcp_initialise,
167         .start        = ctdb_tcp_start,
168         .queue_pkt    = ctdb_tcp_queue_pkt,
169         .add_node     = ctdb_tcp_add_node,
170         .connect_node = ctdb_tcp_connect_node,
171         .allocate_pkt = ctdb_tcp_allocate_pkt,
172         .shutdown     = ctdb_tcp_shutdown,
173         .restart      = ctdb_tcp_restart,
174 };
175
176 static int tcp_ctcp_destructor(struct ctdb_tcp *ctcp)
177 {
178         ctcp->ctdb->private_data = NULL;
179         ctcp->ctdb->methods = NULL;
180         
181         return 0;
182 }
183
184                 
185 /*
186   initialise tcp portion of ctdb 
187 */
188 int ctdb_tcp_init(struct ctdb_context *ctdb)
189 {
190         struct ctdb_tcp *ctcp;
191         ctcp = talloc_zero(ctdb, struct ctdb_tcp);
192         CTDB_NO_MEMORY(ctdb, ctcp);
193
194         ctcp->listen_fd = -1;
195         ctcp->ctdb      = ctdb;
196         ctdb->private_data = ctcp;
197         ctdb->methods = &ctdb_tcp_methods;
198
199         talloc_set_destructor(ctcp, tcp_ctcp_destructor);
200         return 0;
201 }
202