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