4 Copyright (C) Andrew Tridgell 2006
5 Copyright (C) Ronnie Sahlberg 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26 #include "common/system.h"
30 stop any connecting (established or pending) to a node
32 void ctdb_tcp_stop_connection(struct ctdb_node *node)
34 struct ctdb_tcp_node *tnode = talloc_get_type(
35 node->private_data, struct ctdb_tcp_node);
37 ctdb_queue_set_fd(tnode->out_queue, -1);
38 talloc_free(tnode->connect_te);
39 talloc_free(tnode->connect_fde);
40 tnode->connect_fde = NULL;
41 tnode->connect_te = NULL;
42 if (tnode->fd != -1) {
50 called when a complete packet has come in - should not happen on this socket
51 unless the other side closes the connection with RST or FIN
53 void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
55 struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
56 struct ctdb_tcp_node *tnode = talloc_get_type(
57 node->private_data, struct ctdb_tcp_node);
60 node->ctdb->upcalls->node_dead(node);
63 ctdb_tcp_stop_connection(node);
64 tnode->connect_te = event_add_timed(node->ctdb->ev, tnode,
65 timeval_current_ofs(3, 0),
66 ctdb_tcp_node_connect, node);
70 called when socket becomes writeable on connect
72 static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *fde,
73 uint16_t flags, void *private_data)
75 struct ctdb_node *node = talloc_get_type(private_data,
77 struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
78 struct ctdb_tcp_node);
79 struct ctdb_context *ctdb = node->ctdb;
81 socklen_t len = sizeof(error);
84 talloc_free(tnode->connect_te);
85 tnode->connect_te = NULL;
87 if (getsockopt(tnode->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 ||
89 ctdb_tcp_stop_connection(node);
90 tnode->connect_te = event_add_timed(ctdb->ev, tnode,
91 timeval_current_ofs(1, 0),
92 ctdb_tcp_node_connect, node);
96 talloc_free(tnode->connect_fde);
97 tnode->connect_fde = NULL;
99 if (setsockopt(tnode->fd,IPPROTO_TCP,TCP_NODELAY,(char *)&one,sizeof(one)) == -1) {
100 DEBUG(DEBUG_WARNING, ("Failed to set TCP_NODELAY on fd - %s\n",
103 if (setsockopt(tnode->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
104 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
108 ctdb_queue_set_fd(tnode->out_queue, tnode->fd);
110 /* the queue subsystem now owns this fd */
116 called when we should try and establish a tcp connection to a node
118 void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
119 struct timeval t, void *private_data)
121 struct ctdb_node *node = talloc_get_type(private_data,
123 struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
124 struct ctdb_tcp_node);
125 struct ctdb_context *ctdb = node->ctdb;
126 ctdb_sock_addr sock_in;
129 ctdb_sock_addr sock_out;
131 ctdb_tcp_stop_connection(node);
133 sock_out = node->address;
135 tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
136 if (tnode->fd == -1) {
137 DEBUG(DEBUG_ERR, (__location__ "Failed to create socket\n"));
140 set_nonblocking(tnode->fd);
141 set_close_on_exec(tnode->fd);
143 DEBUG(DEBUG_DEBUG, (__location__ " Created TCP SOCKET FD:%d\n", tnode->fd));
145 /* Bind our side of the socketpair to the same address we use to listen
146 * on incoming CTDB traffic.
147 * We must specify this address to make sure that the address we expose to
148 * the remote side is actually routable in case CTDB traffic will run on
149 * a dedicated non-routeable network.
151 sock_in = *ctdb->address;
153 /* AIX libs check to see if the socket address and length
154 arguments are consistent with each other on calls like
155 connect(). Can not get by with just sizeof(sock_in),
156 need sizeof(sock_in.ip).
158 switch (sock_in.sa.sa_family) {
160 sock_in.ip.sin_port = 0 /* Any port */;
161 sockin_size = sizeof(sock_in.ip);
162 sockout_size = sizeof(sock_out.ip);
165 sock_in.ip6.sin6_port = 0 /* Any port */;
166 sockin_size = sizeof(sock_in.ip6);
167 sockout_size = sizeof(sock_out.ip6);
170 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
171 sock_in.sa.sa_family));
176 if (bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size) == -1) {
177 DEBUG(DEBUG_ERR, (__location__ "Failed to bind socket %s(%d)\n",
178 strerror(errno), errno));
183 if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
184 errno != EINPROGRESS) {
185 ctdb_tcp_stop_connection(node);
186 tnode->connect_te = event_add_timed(ctdb->ev, tnode,
187 timeval_current_ofs(1, 0),
188 ctdb_tcp_node_connect, node);
192 /* non-blocking connect - wait for write event */
193 tnode->connect_fde = event_add_fd(node->ctdb->ev, tnode, tnode->fd,
194 EVENT_FD_WRITE|EVENT_FD_READ,
195 ctdb_node_connect_write, node);
197 /* don't give it long to connect - retry in one second. This ensures
198 that we find a node is up quickly (tcp normally backs off a syn reply
199 delay by quite a lot) */
200 tnode->connect_te = event_add_timed(ctdb->ev, tnode, timeval_current_ofs(1, 0),
201 ctdb_tcp_node_connect, node);
205 called when we get contacted by another node
206 currently makes no attempt to check if the connection is really from a ctdb
209 static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
210 uint16_t flags, void *private_data)
212 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
213 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
217 struct ctdb_incoming *in;
220 memset(&addr, 0, sizeof(addr));
222 fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
223 if (fd == -1) return;
225 nodeid = ctdb_ip_to_nodeid(ctdb, &addr);
228 DEBUG(DEBUG_ERR, ("Refused connection from unknown node %s\n", ctdb_addr_to_str(&addr)));
233 in = talloc_zero(ctcp, struct ctdb_incoming);
237 set_nonblocking(in->fd);
238 set_close_on_exec(in->fd);
240 DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d to incoming ctdb connection\n", fd));
242 if (setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
243 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
247 in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT,
248 ctdb_tcp_read_cb, in, "ctdbd-%s", ctdb_addr_to_str(&addr));
253 automatically find which address to listen on
255 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
257 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
261 const char *lock_path = CTDB_RUNDIR "/.socket_lock";
265 struct tevent_fd *fde;
267 /* If there are no nodes, then it won't be possible to find
268 * the first one. Log a failure and short circuit the whole
271 if (ctdb->num_nodes == 0) {
272 DEBUG(DEBUG_CRIT,("No nodes available to attempt bind to - is the nodes file empty?\n"));
276 /* in order to ensure that we don't get two nodes with the
277 same adddress, we must make the bind() and listen() calls
278 atomic. The SO_REUSEADDR setsockopt only prevents double
279 binds if the first socket is in LISTEN state */
280 lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
282 DEBUG(DEBUG_CRIT,("Unable to open %s\n", lock_path));
286 lock.l_type = F_WRLCK;
287 lock.l_whence = SEEK_SET;
292 if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
293 DEBUG(DEBUG_CRIT,("Unable to lock %s\n", lock_path));
298 for (i=0; i < ctdb->num_nodes; i++) {
299 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
302 sock = ctdb->nodes[i]->address;
304 switch (sock.sa.sa_family) {
306 sock_size = sizeof(sock.ip);
309 sock_size = sizeof(sock.ip6);
312 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
317 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
318 if (ctcp->listen_fd == -1) {
319 ctdb_set_error(ctdb, "socket failed\n");
323 set_close_on_exec(ctcp->listen_fd);
325 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,
326 (char *)&one,sizeof(one)) == -1) {
327 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
331 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
335 if (errno == EADDRNOTAVAIL) {
336 DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
337 strerror(errno), errno));
339 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
340 strerror(errno), errno));
343 close(ctcp->listen_fd);
344 ctcp->listen_fd = -1;
347 if (i == ctdb->num_nodes) {
348 DEBUG(DEBUG_CRIT,("Unable to bind to any of the node addresses - giving up\n"));
351 ctdb->address = talloc_memdup(ctdb,
352 &ctdb->nodes[i]->address,
353 sizeof(ctdb_sock_addr));
354 if (ctdb->address == NULL) {
355 ctdb_set_error(ctdb, "Out of memory at %s:%d",
360 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
361 ctdb_addr_to_str(ctdb->address),
362 ctdb_addr_to_port(ctdb->address));
363 if (ctdb->name == NULL) {
364 ctdb_set_error(ctdb, "Out of memory at %s:%d",
368 DEBUG(DEBUG_INFO,("ctdb chose network address %s\n", ctdb->name));
370 if (listen(ctcp->listen_fd, 10) == -1) {
374 fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
375 ctdb_listen_event, ctdb);
376 tevent_fd_set_auto_close(fde);
384 if (ctcp->listen_fd != -1) {
385 close(ctcp->listen_fd);
386 ctcp->listen_fd = -1;
393 listen on our own address
395 int ctdb_tcp_listen(struct ctdb_context *ctdb)
397 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
402 struct tevent_fd *fde;
404 /* we can either auto-bind to the first available address, or we can
405 use a specified address */
406 if (!ctdb->address) {
407 return ctdb_tcp_listen_automatic(ctdb);
410 sock = *ctdb->address;
412 switch (sock.sa.sa_family) {
414 sock_size = sizeof(sock.ip);
417 sock_size = sizeof(sock.ip6);
420 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
425 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
426 if (ctcp->listen_fd == -1) {
427 ctdb_set_error(ctdb, "socket failed\n");
431 set_close_on_exec(ctcp->listen_fd);
433 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one)) == -1) {
434 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
438 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
439 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
443 if (listen(ctcp->listen_fd, 10) == -1) {
447 fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
448 ctdb_listen_event, ctdb);
449 tevent_fd_set_auto_close(fde);
454 if (ctcp->listen_fd != -1) {
455 close(ctcp->listen_fd);
457 ctcp->listen_fd = -1;