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