ctdb-tcp: Move incoming fd and queue into struct ctdb_tcp_node
[samba.git] / ctdb / tcp / tcp_init.c
blobd4b42b0d0f2d5c17d94968fdb4b87e7fb20b1fe8
1 /*
2 ctdb over TCP
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/util/time.h"
28 #include "lib/util/debug.h"
30 #include "ctdb_private.h"
32 #include "common/common.h"
33 #include "common/logging.h"
35 #include "ctdb_tcp.h"
37 static int tnode_destructor(struct ctdb_tcp_node *tnode)
39 // struct ctdb_node *node = talloc_find_parent_bytype(tnode, struct ctdb_node);
41 if (tnode->out_fd != -1) {
42 close(tnode->out_fd);
43 tnode->out_fd = -1;
46 if (tnode->in_fd != -1) {
47 close(tnode->in_fd);
48 tnode->in_fd = -1;
51 return 0;
55 initialise tcp portion of a ctdb node
57 static int ctdb_tcp_add_node(struct ctdb_node *node)
59 struct ctdb_tcp_node *tnode;
60 tnode = talloc_zero(node, struct ctdb_tcp_node);
61 CTDB_NO_MEMORY(node->ctdb, tnode);
63 tnode->out_fd = -1;
64 tnode->in_fd = -1;
65 tnode->ctdb = node->ctdb;
67 node->private_data = tnode;
68 talloc_set_destructor(tnode, tnode_destructor);
70 tnode->out_queue = ctdb_queue_setup(node->ctdb,
71 node,
72 tnode->out_fd,
73 CTDB_TCP_ALIGNMENT,
74 ctdb_tcp_tnode_cb,
75 node,
76 "to-node-%s",
77 node->name);
79 return 0;
83 initialise transport structures
85 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
87 unsigned int i;
89 /* listen on our own address */
90 if (ctdb_tcp_listen(ctdb) != 0) {
91 DEBUG(DEBUG_CRIT, (__location__ " Failed to start listening on the CTDB socket\n"));
92 exit(1);
95 for (i=0; i < ctdb->num_nodes; i++) {
96 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
97 continue;
99 if (ctdb_tcp_add_node(ctdb->nodes[i]) != 0) {
100 DEBUG(DEBUG_CRIT, ("methods->add_node failed at %d\n", i));
101 return -1;
105 return 0;
109 start the protocol going
111 static int ctdb_tcp_connect_node(struct ctdb_node *node)
113 struct ctdb_context *ctdb = node->ctdb;
114 struct ctdb_tcp_node *tnode = talloc_get_type(
115 node->private_data, struct ctdb_tcp_node);
117 /* startup connection to the other server - will happen on
118 next event loop */
119 if (!ctdb_same_address(ctdb->address, &node->address)) {
120 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
121 timeval_zero(),
122 ctdb_tcp_node_connect,
123 node);
126 return 0;
130 shutdown and try to restart a connection to a node after it has been
131 disconnected
133 static void ctdb_tcp_restart(struct ctdb_node *node)
135 struct ctdb_tcp_node *tnode = talloc_get_type(
136 node->private_data, struct ctdb_tcp_node);
138 DEBUG(DEBUG_NOTICE,("Tearing down connection to dead node :%d\n", node->pnn));
140 ctdb_tcp_stop_connection(node);
142 tnode->connect_te = tevent_add_timer(node->ctdb->ev, tnode,
143 timeval_zero(),
144 ctdb_tcp_node_connect, node);
149 shutdown the transport
151 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
153 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
154 struct ctdb_tcp);
155 talloc_free(ctcp);
156 ctdb->private_data = NULL;
160 start the transport
162 static int ctdb_tcp_start(struct ctdb_context *ctdb)
164 unsigned int i;
166 for (i=0; i < ctdb->num_nodes; i++) {
167 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
168 continue;
170 ctdb_tcp_connect_node(ctdb->nodes[i]);
173 return 0;
178 transport packet allocator - allows transport to control memory for packets
180 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
182 /* tcp transport needs to round to 8 byte alignment to ensure
183 that we can use a length header and 64 bit elements in
184 structures */
185 size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
186 return talloc_size(mem_ctx, size);
190 static const struct ctdb_methods ctdb_tcp_methods = {
191 .initialise = ctdb_tcp_initialise,
192 .start = ctdb_tcp_start,
193 .queue_pkt = ctdb_tcp_queue_pkt,
194 .add_node = ctdb_tcp_add_node,
195 .connect_node = ctdb_tcp_connect_node,
196 .allocate_pkt = ctdb_tcp_allocate_pkt,
197 .shutdown = ctdb_tcp_shutdown,
198 .restart = ctdb_tcp_restart,
201 static int tcp_ctcp_destructor(struct ctdb_tcp *ctcp)
203 ctcp->ctdb->private_data = NULL;
204 ctcp->ctdb->methods = NULL;
206 return 0;
211 initialise tcp portion of ctdb
213 int ctdb_tcp_init(struct ctdb_context *ctdb)
215 struct ctdb_tcp *ctcp;
216 ctcp = talloc_zero(ctdb, struct ctdb_tcp);
217 CTDB_NO_MEMORY(ctdb, ctcp);
219 ctcp->listen_fd = -1;
220 ctcp->ctdb = ctdb;
221 ctdb->private_data = ctcp;
222 ctdb->methods = &ctdb_tcp_methods;
224 talloc_set_destructor(ctcp, tcp_ctcp_destructor);
225 return 0;