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/>.
21 #include "lib/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"
30 initialise tcp portion of a ctdb node
32 static int ctdb_tcp_add_node(struct ctdb_node *node)
34 struct ctdb_tcp *ctcp = talloc_get_type(node->ctdb->private_data,
36 struct ctdb_tcp_node *tnode;
37 tnode = talloc_zero(ctcp, struct ctdb_tcp_node);
38 CTDB_NO_MEMORY(node->ctdb, tnode);
41 node->private_data = tnode;
43 tnode->out_queue = ctdb_queue_setup(node->ctdb, ctcp, tnode->fd, CTDB_TCP_ALIGNMENT,
44 ctdb_tcp_tnode_cb, node);
50 initialise transport structures
52 static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
56 /* listen on our own address */
57 if (ctdb_tcp_listen(ctdb) != 0) return -1;
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));
70 start the protocol going
72 static int ctdb_tcp_start(struct ctdb_context *ctdb)
76 /* startup connections to the other servers - will happen on
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);
92 shutdown and try to restart a connection to a node after it has been
95 static void ctdb_tcp_restart(struct ctdb_node *node)
97 struct ctdb_tcp_node *tnode = talloc_get_type(
98 node->private_data, struct ctdb_tcp_node);
100 DEBUG(0,("Tearing down connection to dead node :%d\n", node->pnn));
103 ctdb_queue_set_fd(tnode->out_queue, -1);
105 event_add_timed(node->ctdb->ev, tnode, timeval_zero(),
106 ctdb_tcp_node_connect, node);
111 shutdown the transport
113 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
115 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
118 ctdb->private_data = NULL;
123 transport packet allocator - allows transport to control memory for packets
125 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
127 /* tcp transport needs to round to 8 byte alignment to ensure
128 that we can use a length header and 64 bit elements in
130 size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
131 return talloc_size(mem_ctx, size);
135 static const struct ctdb_methods ctdb_tcp_methods = {
136 .initialise = ctdb_tcp_initialise,
137 .start = ctdb_tcp_start,
138 .queue_pkt = ctdb_tcp_queue_pkt,
139 .add_node = ctdb_tcp_add_node,
140 .allocate_pkt = ctdb_tcp_allocate_pkt,
141 .shutdown = ctdb_tcp_shutdown,
142 .restart = ctdb_tcp_restart,
146 initialise tcp portion of ctdb
148 int ctdb_tcp_init(struct ctdb_context *ctdb)
150 struct ctdb_tcp *ctcp;
151 ctcp = talloc_zero(ctdb, struct ctdb_tcp);
152 CTDB_NO_MEMORY(ctdb, ctcp);
154 ctcp->listen_fd = -1;
155 ctdb->private_data = ctcp;
156 ctdb->methods = &ctdb_tcp_methods;