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 "../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);
93 shutdown the transport
95 static void ctdb_tcp_shutdown(struct ctdb_context *ctdb)
97 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
100 ctdb->private_data = NULL;
105 transport packet allocator - allows transport to control memory for packets
107 static void *ctdb_tcp_allocate_pkt(TALLOC_CTX *mem_ctx, size_t size)
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
112 size = (size+(CTDB_TCP_ALIGNMENT-1)) & ~(CTDB_TCP_ALIGNMENT-1);
113 return talloc_size(mem_ctx, size);
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,
127 initialise tcp portion of ctdb
129 int ctdb_tcp_init(struct ctdb_context *ctdb)
131 struct ctdb_tcp *ctcp;
132 ctcp = talloc_zero(ctdb, struct ctdb_tcp);
133 CTDB_NO_MEMORY(ctdb, ctcp);
135 ctcp->listen_fd = -1;
136 ctdb->private_data = ctcp;
137 ctdb->methods = &ctdb_tcp_methods;