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
);
81 called when socket becomes writeable on connect
83 static void ctdb_node_connect_write(struct tevent_context
*ev
,
84 struct tevent_fd
*fde
,
85 uint16_t flags
, void *private_data
)
87 struct ctdb_node
*node
= talloc_get_type(private_data
,
89 struct ctdb_tcp_node
*tnode
= talloc_get_type(node
->private_data
,
90 struct ctdb_tcp_node
);
91 struct ctdb_context
*ctdb
= node
->ctdb
;
93 socklen_t len
= sizeof(error
);
96 talloc_free(tnode
->connect_te
);
97 tnode
->connect_te
= NULL
;
99 if (getsockopt(tnode
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) != 0 ||
101 ctdb_tcp_stop_connection(node
);
102 tnode
->connect_te
= tevent_add_timer(ctdb
->ev
, tnode
,
103 timeval_current_ofs(1, 0),
104 ctdb_tcp_node_connect
, node
);
108 talloc_free(tnode
->connect_fde
);
109 tnode
->connect_fde
= NULL
;
111 if (setsockopt(tnode
->fd
,IPPROTO_TCP
,TCP_NODELAY
,(char *)&one
,sizeof(one
)) == -1) {
112 DEBUG(DEBUG_WARNING
, ("Failed to set TCP_NODELAY on fd - %s\n",
115 if (setsockopt(tnode
->fd
,SOL_SOCKET
,SO_KEEPALIVE
,(char *)&one
,sizeof(one
)) == -1) {
116 DEBUG(DEBUG_WARNING
, ("Failed to set KEEPALIVE on fd - %s\n",
120 ctdb_queue_set_fd(tnode
->out_queue
, tnode
->fd
);
122 /* the queue subsystem now owns this fd */
128 called when we should try and establish a tcp connection to a node
130 void ctdb_tcp_node_connect(struct tevent_context
*ev
, struct tevent_timer
*te
,
131 struct timeval t
, void *private_data
)
133 struct ctdb_node
*node
= talloc_get_type(private_data
,
135 struct ctdb_tcp_node
*tnode
= talloc_get_type(node
->private_data
,
136 struct ctdb_tcp_node
);
137 struct ctdb_context
*ctdb
= node
->ctdb
;
138 ctdb_sock_addr sock_in
;
141 ctdb_sock_addr sock_out
;
144 ctdb_tcp_stop_connection(node
);
146 sock_out
= node
->address
;
148 tnode
->fd
= socket(sock_out
.sa
.sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
149 if (tnode
->fd
== -1) {
150 DEBUG(DEBUG_ERR
, (__location__
" Failed to create socket\n"));
154 ret
= set_blocking(tnode
->fd
, false);
158 " failed to set socket non-blocking (%s)\n",
165 set_close_on_exec(tnode
->fd
);
167 DEBUG(DEBUG_DEBUG
, (__location__
" Created TCP SOCKET FD:%d\n", tnode
->fd
));
169 /* Bind our side of the socketpair to the same address we use to listen
170 * on incoming CTDB traffic.
171 * We must specify this address to make sure that the address we expose to
172 * the remote side is actually routable in case CTDB traffic will run on
173 * a dedicated non-routeable network.
175 sock_in
= *ctdb
->address
;
177 /* AIX libs check to see if the socket address and length
178 arguments are consistent with each other on calls like
179 connect(). Can not get by with just sizeof(sock_in),
180 need sizeof(sock_in.ip).
182 switch (sock_in
.sa
.sa_family
) {
184 sock_in
.ip
.sin_port
= 0 /* Any port */;
185 sockin_size
= sizeof(sock_in
.ip
);
186 sockout_size
= sizeof(sock_out
.ip
);
189 sock_in
.ip6
.sin6_port
= 0 /* Any port */;
190 sockin_size
= sizeof(sock_in
.ip6
);
191 sockout_size
= sizeof(sock_out
.ip6
);
194 DEBUG(DEBUG_ERR
, (__location__
" unknown family %u\n",
195 sock_in
.sa
.sa_family
));
201 if (bind(tnode
->fd
, (struct sockaddr
*)&sock_in
, sockin_size
) == -1) {
202 DEBUG(DEBUG_ERR
, (__location__
" Failed to bind socket %s(%d)\n",
203 strerror(errno
), errno
));
209 if (connect(tnode
->fd
, (struct sockaddr
*)&sock_out
, sockout_size
) != 0 &&
210 errno
!= EINPROGRESS
) {
211 ctdb_tcp_stop_connection(node
);
212 tnode
->connect_te
= tevent_add_timer(ctdb
->ev
, tnode
,
213 timeval_current_ofs(1, 0),
214 ctdb_tcp_node_connect
, node
);
218 /* non-blocking connect - wait for write event */
219 tnode
->connect_fde
= tevent_add_fd(node
->ctdb
->ev
, tnode
, tnode
->fd
,
220 TEVENT_FD_WRITE
|TEVENT_FD_READ
,
221 ctdb_node_connect_write
, node
);
223 /* don't give it long to connect - retry in one second. This ensures
224 that we find a node is up quickly (tcp normally backs off a syn reply
225 delay by quite a lot) */
226 tnode
->connect_te
= tevent_add_timer(ctdb
->ev
, tnode
,
227 timeval_current_ofs(1, 0),
228 ctdb_tcp_node_connect
, node
);
232 called when we get contacted by another node
233 currently makes no attempt to check if the connection is really from a ctdb
236 static void ctdb_listen_event(struct tevent_context
*ev
, struct tevent_fd
*fde
,
237 uint16_t flags
, void *private_data
)
239 struct ctdb_context
*ctdb
= talloc_get_type(private_data
, struct ctdb_context
);
240 struct ctdb_tcp
*ctcp
= talloc_get_type(ctdb
->private_data
, struct ctdb_tcp
);
244 struct ctdb_incoming
*in
;
248 memset(&addr
, 0, sizeof(addr
));
250 fd
= accept(ctcp
->listen_fd
, (struct sockaddr
*)&addr
, &len
);
251 if (fd
== -1) return;
253 nodeid
= ctdb_ip_to_nodeid(ctdb
, &addr
);
256 DEBUG(DEBUG_ERR
, ("Refused connection from unknown node %s\n", ctdb_addr_to_str(&addr
)));
261 in
= talloc_zero(ctcp
, struct ctdb_incoming
);
265 ret
= set_blocking(in
->fd
, false);
269 " failed to set socket non-blocking (%s)\n",
276 set_close_on_exec(in
->fd
);
278 DEBUG(DEBUG_DEBUG
, (__location__
" Created SOCKET FD:%d to incoming ctdb connection\n", fd
));
280 if (setsockopt(in
->fd
,SOL_SOCKET
,SO_KEEPALIVE
,(char *)&one
,sizeof(one
)) == -1) {
281 DEBUG(DEBUG_WARNING
, ("Failed to set KEEPALIVE on fd - %s\n",
285 in
->queue
= ctdb_queue_setup(ctdb
, in
, in
->fd
, CTDB_TCP_ALIGNMENT
,
286 ctdb_tcp_read_cb
, in
, "ctdbd-%s", ctdb_addr_to_str(&addr
));
291 automatically find which address to listen on
293 static int ctdb_tcp_listen_automatic(struct ctdb_context
*ctdb
)
295 struct ctdb_tcp
*ctcp
= talloc_get_type(ctdb
->private_data
,
299 const char *lock_path
= CTDB_RUNDIR
"/.socket_lock";
303 struct tevent_fd
*fde
;
305 /* If there are no nodes, then it won't be possible to find
306 * the first one. Log a failure and short circuit the whole
309 if (ctdb
->num_nodes
== 0) {
310 DEBUG(DEBUG_CRIT
,("No nodes available to attempt bind to - is the nodes file empty?\n"));
314 /* in order to ensure that we don't get two nodes with the
315 same adddress, we must make the bind() and listen() calls
316 atomic. The SO_REUSEADDR setsockopt only prevents double
317 binds if the first socket is in LISTEN state */
318 lock_fd
= open(lock_path
, O_RDWR
|O_CREAT
, 0666);
320 DEBUG(DEBUG_CRIT
,("Unable to open %s\n", lock_path
));
324 lock
.l_type
= F_WRLCK
;
325 lock
.l_whence
= SEEK_SET
;
330 if (fcntl(lock_fd
, F_SETLKW
, &lock
) != 0) {
331 DEBUG(DEBUG_CRIT
,("Unable to lock %s\n", lock_path
));
336 for (i
=0; i
< ctdb
->num_nodes
; i
++) {
337 if (ctdb
->nodes
[i
]->flags
& NODE_FLAGS_DELETED
) {
340 sock
= ctdb
->nodes
[i
]->address
;
342 switch (sock
.sa
.sa_family
) {
344 sock_size
= sizeof(sock
.ip
);
347 sock_size
= sizeof(sock
.ip6
);
350 DEBUG(DEBUG_ERR
, (__location__
" unknown family %u\n",
355 ctcp
->listen_fd
= socket(sock
.sa
.sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
356 if (ctcp
->listen_fd
== -1) {
357 ctdb_set_error(ctdb
, "socket failed\n");
361 set_close_on_exec(ctcp
->listen_fd
);
363 if (setsockopt(ctcp
->listen_fd
,SOL_SOCKET
,SO_REUSEADDR
,
364 (char *)&one
,sizeof(one
)) == -1) {
365 DEBUG(DEBUG_WARNING
, ("Failed to set REUSEADDR on fd - %s\n",
369 if (bind(ctcp
->listen_fd
, (struct sockaddr
* )&sock
, sock_size
) == 0) {
373 if (errno
== EADDRNOTAVAIL
) {
374 DEBUG(DEBUG_DEBUG
,(__location__
" Failed to bind() to socket. %s(%d)\n",
375 strerror(errno
), errno
));
377 DEBUG(DEBUG_ERR
,(__location__
" Failed to bind() to socket. %s(%d)\n",
378 strerror(errno
), errno
));
381 close(ctcp
->listen_fd
);
382 ctcp
->listen_fd
= -1;
385 if (i
== ctdb
->num_nodes
) {
386 DEBUG(DEBUG_CRIT
,("Unable to bind to any of the node addresses - giving up\n"));
389 ctdb
->address
= talloc_memdup(ctdb
,
390 &ctdb
->nodes
[i
]->address
,
391 sizeof(ctdb_sock_addr
));
392 if (ctdb
->address
== NULL
) {
393 ctdb_set_error(ctdb
, "Out of memory at %s:%d",
398 ctdb
->name
= talloc_asprintf(ctdb
, "%s:%u",
399 ctdb_addr_to_str(ctdb
->address
),
400 ctdb_addr_to_port(ctdb
->address
));
401 if (ctdb
->name
== NULL
) {
402 ctdb_set_error(ctdb
, "Out of memory at %s:%d",
406 DEBUG(DEBUG_INFO
,("ctdb chose network address %s\n", ctdb
->name
));
408 if (listen(ctcp
->listen_fd
, 10) == -1) {
412 fde
= tevent_add_fd(ctdb
->ev
, ctcp
, ctcp
->listen_fd
, TEVENT_FD_READ
,
413 ctdb_listen_event
, ctdb
);
414 tevent_fd_set_auto_close(fde
);
422 if (ctcp
->listen_fd
!= -1) {
423 close(ctcp
->listen_fd
);
424 ctcp
->listen_fd
= -1;
431 listen on our own address
433 int ctdb_tcp_listen(struct ctdb_context
*ctdb
)
435 struct ctdb_tcp
*ctcp
= talloc_get_type(ctdb
->private_data
,
440 struct tevent_fd
*fde
;
442 /* we can either auto-bind to the first available address, or we can
443 use a specified address */
444 if (!ctdb
->address
) {
445 return ctdb_tcp_listen_automatic(ctdb
);
448 sock
= *ctdb
->address
;
450 switch (sock
.sa
.sa_family
) {
452 sock_size
= sizeof(sock
.ip
);
455 sock_size
= sizeof(sock
.ip6
);
458 DEBUG(DEBUG_ERR
, (__location__
" unknown family %u\n",
463 ctcp
->listen_fd
= socket(sock
.sa
.sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
464 if (ctcp
->listen_fd
== -1) {
465 ctdb_set_error(ctdb
, "socket failed\n");
469 set_close_on_exec(ctcp
->listen_fd
);
471 if (setsockopt(ctcp
->listen_fd
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&one
,sizeof(one
)) == -1) {
472 DEBUG(DEBUG_WARNING
, ("Failed to set REUSEADDR on fd - %s\n",
476 if (bind(ctcp
->listen_fd
, (struct sockaddr
* )&sock
, sock_size
) != 0) {
477 DEBUG(DEBUG_ERR
,(__location__
" Failed to bind() to socket. %s(%d)\n", strerror(errno
), errno
));
481 if (listen(ctcp
->listen_fd
, 10) == -1) {
485 fde
= tevent_add_fd(ctdb
->ev
, ctcp
, ctcp
->listen_fd
, TEVENT_FD_READ
,
486 ctdb_listen_event
, ctdb
);
487 tevent_fd_set_auto_close(fde
);
492 if (ctcp
->listen_fd
!= -1) {
493 close(ctcp
->listen_fd
);
495 ctcp
->listen_fd
= -1;