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/>.
22 #include "system/network.h"
23 #include "system/filesys.h"
28 #include "lib/util/debug.h"
29 #include "lib/util/time.h"
30 #include "lib/util/blocking.h"
32 #include "ctdb_private.h"
34 #include "common/system.h"
35 #include "common/common.h"
36 #include "common/logging.h"
41 stop any connecting (established or pending) to a node
43 void ctdb_tcp_stop_connection(struct ctdb_node *node)
45 struct ctdb_tcp_node *tnode = talloc_get_type(
46 node->private_data, struct ctdb_tcp_node);
48 ctdb_queue_set_fd(tnode->out_queue, -1);
49 talloc_free(tnode->connect_te);
50 talloc_free(tnode->connect_fde);
51 tnode->connect_fde = NULL;
52 tnode->connect_te = NULL;
53 if (tnode->fd != -1) {
61 called when a complete packet has come in - should not happen on this socket
62 unless the other side closes the connection with RST or FIN
64 void ctdb_tcp_tnode_cb(uint8_t *data, size_t cnt, void *private_data)
66 struct ctdb_node *node = talloc_get_type(private_data, struct ctdb_node);
67 struct ctdb_tcp_node *tnode = talloc_get_type(
68 node->private_data, struct ctdb_tcp_node);
71 node->ctdb->upcalls->node_dead(node);
74 ctdb_tcp_stop_connection(node);
75 tnode->connect_te = tevent_add_timer(node->ctdb->ev, tnode,
76 timeval_current_ofs(3, 0),
77 ctdb_tcp_node_connect, node);
82 called when socket becomes writeable on connect
84 static void ctdb_node_connect_write(struct tevent_context *ev,
85 struct tevent_fd *fde,
86 uint16_t flags, void *private_data)
88 struct ctdb_node *node = talloc_get_type(private_data,
90 struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
91 struct ctdb_tcp_node);
92 struct ctdb_context *ctdb = node->ctdb;
94 socklen_t len = sizeof(error);
97 talloc_free(tnode->connect_te);
98 tnode->connect_te = NULL;
100 if (getsockopt(tnode->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0 ||
102 ctdb_tcp_stop_connection(node);
103 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
104 timeval_current_ofs(1, 0),
105 ctdb_tcp_node_connect, node);
109 talloc_free(tnode->connect_fde);
110 tnode->connect_fde = NULL;
112 if (setsockopt(tnode->fd,IPPROTO_TCP,TCP_NODELAY,(char *)&one,sizeof(one)) == -1) {
113 DEBUG(DEBUG_WARNING, ("Failed to set TCP_NODELAY on fd - %s\n",
116 if (setsockopt(tnode->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
117 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
121 ctdb_queue_set_fd(tnode->out_queue, tnode->fd);
123 /* the queue subsystem now owns this fd */
126 /* tell the ctdb layer we are connected */
127 node->ctdb->upcalls->node_connected(node);
132 called when we should try and establish a tcp connection to a node
134 void ctdb_tcp_node_connect(struct tevent_context *ev, struct tevent_timer *te,
135 struct timeval t, void *private_data)
137 struct ctdb_node *node = talloc_get_type(private_data,
139 struct ctdb_tcp_node *tnode = talloc_get_type(node->private_data,
140 struct ctdb_tcp_node);
141 struct ctdb_context *ctdb = node->ctdb;
142 ctdb_sock_addr sock_in;
145 ctdb_sock_addr sock_out;
148 ctdb_tcp_stop_connection(node);
150 sock_out = node->address;
152 tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
153 if (tnode->fd == -1) {
154 DEBUG(DEBUG_ERR, (__location__ " Failed to create socket\n"));
158 ret = set_blocking(tnode->fd, false);
162 " failed to set socket non-blocking (%s)\n",
169 set_close_on_exec(tnode->fd);
171 DEBUG(DEBUG_DEBUG, (__location__ " Created TCP SOCKET FD:%d\n", tnode->fd));
173 /* Bind our side of the socketpair to the same address we use to listen
174 * on incoming CTDB traffic.
175 * We must specify this address to make sure that the address we expose to
176 * the remote side is actually routable in case CTDB traffic will run on
177 * a dedicated non-routeable network.
179 sock_in = *ctdb->address;
181 /* AIX libs check to see if the socket address and length
182 arguments are consistent with each other on calls like
183 connect(). Can not get by with just sizeof(sock_in),
184 need sizeof(sock_in.ip).
186 switch (sock_in.sa.sa_family) {
188 sock_in.ip.sin_port = 0 /* Any port */;
189 sockin_size = sizeof(sock_in.ip);
190 sockout_size = sizeof(sock_out.ip);
193 sock_in.ip6.sin6_port = 0 /* Any port */;
194 sockin_size = sizeof(sock_in.ip6);
195 sockout_size = sizeof(sock_out.ip6);
198 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
199 sock_in.sa.sa_family));
205 if (bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size) == -1) {
206 DEBUG(DEBUG_ERR, (__location__ " Failed to bind socket %s(%d)\n",
207 strerror(errno), errno));
213 if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
214 errno != EINPROGRESS) {
215 ctdb_tcp_stop_connection(node);
216 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
217 timeval_current_ofs(1, 0),
218 ctdb_tcp_node_connect, node);
222 /* non-blocking connect - wait for write event */
223 tnode->connect_fde = tevent_add_fd(node->ctdb->ev, tnode, tnode->fd,
224 TEVENT_FD_WRITE|TEVENT_FD_READ,
225 ctdb_node_connect_write, node);
227 /* don't give it long to connect - retry in one second. This ensures
228 that we find a node is up quickly (tcp normally backs off a syn reply
229 delay by quite a lot) */
230 tnode->connect_te = tevent_add_timer(ctdb->ev, tnode,
231 timeval_current_ofs(1, 0),
232 ctdb_tcp_node_connect, node);
236 called when we get contacted by another node
237 currently makes no attempt to check if the connection is really from a ctdb
240 static void ctdb_listen_event(struct tevent_context *ev, struct tevent_fd *fde,
241 uint16_t flags, void *private_data)
243 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
244 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data, struct ctdb_tcp);
248 struct ctdb_incoming *in;
252 memset(&addr, 0, sizeof(addr));
254 fd = accept(ctcp->listen_fd, (struct sockaddr *)&addr, &len);
255 if (fd == -1) return;
256 smb_set_close_on_exec(fd);
258 nodeid = ctdb_ip_to_nodeid(ctdb, &addr);
261 DEBUG(DEBUG_ERR, ("Refused connection from unknown node %s\n", ctdb_addr_to_str(&addr)));
266 in = talloc_zero(ctcp, struct ctdb_incoming);
270 ret = set_blocking(in->fd, false);
274 " failed to set socket non-blocking (%s)\n",
281 set_close_on_exec(in->fd);
283 DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d to incoming ctdb connection\n", fd));
285 if (setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one)) == -1) {
286 DEBUG(DEBUG_WARNING, ("Failed to set KEEPALIVE on fd - %s\n",
290 in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT,
291 ctdb_tcp_read_cb, in, "ctdbd-%s", ctdb_addr_to_str(&addr));
296 automatically find which address to listen on
298 static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
300 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
304 const char *lock_path = CTDB_RUNDIR "/.socket_lock";
308 struct tevent_fd *fde;
310 /* If there are no nodes, then it won't be possible to find
311 * the first one. Log a failure and short circuit the whole
314 if (ctdb->num_nodes == 0) {
315 DEBUG(DEBUG_CRIT,("No nodes available to attempt bind to - is the nodes file empty?\n"));
319 /* in order to ensure that we don't get two nodes with the
320 same adddress, we must make the bind() and listen() calls
321 atomic. The SO_REUSEADDR setsockopt only prevents double
322 binds if the first socket is in LISTEN state */
323 lock_fd = open(lock_path, O_RDWR|O_CREAT, 0666);
325 DEBUG(DEBUG_CRIT,("Unable to open %s\n", lock_path));
329 lock.l_type = F_WRLCK;
330 lock.l_whence = SEEK_SET;
335 if (fcntl(lock_fd, F_SETLKW, &lock) != 0) {
336 DEBUG(DEBUG_CRIT,("Unable to lock %s\n", lock_path));
341 for (i=0; i < ctdb->num_nodes; i++) {
342 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
345 sock = ctdb->nodes[i]->address;
347 switch (sock.sa.sa_family) {
349 sock_size = sizeof(sock.ip);
352 sock_size = sizeof(sock.ip6);
355 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
360 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
361 if (ctcp->listen_fd == -1) {
362 ctdb_set_error(ctdb, "socket failed\n");
366 set_close_on_exec(ctcp->listen_fd);
368 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,
369 (char *)&one,sizeof(one)) == -1) {
370 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
374 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
378 if (errno == EADDRNOTAVAIL) {
379 DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
380 strerror(errno), errno));
382 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
383 strerror(errno), errno));
386 close(ctcp->listen_fd);
387 ctcp->listen_fd = -1;
390 if (i == ctdb->num_nodes) {
391 DEBUG(DEBUG_CRIT,("Unable to bind to any of the node addresses - giving up\n"));
394 ctdb->address = talloc_memdup(ctdb,
395 &ctdb->nodes[i]->address,
396 sizeof(ctdb_sock_addr));
397 if (ctdb->address == NULL) {
398 ctdb_set_error(ctdb, "Out of memory at %s:%d",
403 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
404 ctdb_addr_to_str(ctdb->address),
405 ctdb_addr_to_port(ctdb->address));
406 if (ctdb->name == NULL) {
407 ctdb_set_error(ctdb, "Out of memory at %s:%d",
411 DEBUG(DEBUG_INFO,("ctdb chose network address %s\n", ctdb->name));
413 if (listen(ctcp->listen_fd, 10) == -1) {
417 fde = tevent_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, TEVENT_FD_READ,
418 ctdb_listen_event, ctdb);
419 tevent_fd_set_auto_close(fde);
427 if (ctcp->listen_fd != -1) {
428 close(ctcp->listen_fd);
429 ctcp->listen_fd = -1;
436 listen on our own address
438 int ctdb_tcp_listen(struct ctdb_context *ctdb)
440 struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
445 struct tevent_fd *fde;
447 /* we can either auto-bind to the first available address, or we can
448 use a specified address */
449 if (!ctdb->address) {
450 return ctdb_tcp_listen_automatic(ctdb);
453 sock = *ctdb->address;
455 switch (sock.sa.sa_family) {
457 sock_size = sizeof(sock.ip);
460 sock_size = sizeof(sock.ip6);
463 DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
468 ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
469 if (ctcp->listen_fd == -1) {
470 ctdb_set_error(ctdb, "socket failed\n");
474 set_close_on_exec(ctcp->listen_fd);
476 if (setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one)) == -1) {
477 DEBUG(DEBUG_WARNING, ("Failed to set REUSEADDR on fd - %s\n",
481 if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
482 DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
486 if (listen(ctcp->listen_fd, 10) == -1) {
490 fde = tevent_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, TEVENT_FD_READ,
491 ctdb_listen_event, ctdb);
492 tevent_fd_set_auto_close(fde);
497 if (ctcp->listen_fd != -1) {
498 close(ctcp->listen_fd);
500 ctcp->listen_fd = -1;