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"
29 stop any connecting (established or pending) to a node
31 void ctdb_tcp_stop_connection(struct ctdb_node
*node
)
33 struct ctdb_tcp_node
*tnode
= talloc_get_type(
34 node
->private_data
, struct ctdb_tcp_node
);
36 ctdb_queue_set_fd(tnode
->out_queue
, -1);
37 talloc_free(tnode
->connect_te
);
38 talloc_free(tnode
->connect_fde
);
39 tnode
->connect_fde
= NULL
;
40 tnode
->connect_te
= NULL
;
41 if (tnode
->fd
!= -1) {
49 called when a complete packet has come in - should not happen on this socket
50 unless the other side closes the connection with RST or FIN
52 void ctdb_tcp_tnode_cb(uint8_t *data
, size_t cnt
, void *private_data
)
54 struct ctdb_node
*node
= talloc_get_type(private_data
, struct ctdb_node
);
55 struct ctdb_tcp_node
*tnode
= talloc_get_type(
56 node
->private_data
, struct ctdb_tcp_node
);
59 node
->ctdb
->upcalls
->node_dead(node
);
62 ctdb_tcp_stop_connection(node
);
63 tnode
->connect_te
= event_add_timed(node
->ctdb
->ev
, tnode
,
64 timeval_current_ofs(3, 0),
65 ctdb_tcp_node_connect
, node
);
69 called when socket becomes writeable on connect
71 static void ctdb_node_connect_write(struct event_context
*ev
, struct fd_event
*fde
,
72 uint16_t flags
, void *private_data
)
74 struct ctdb_node
*node
= talloc_get_type(private_data
,
76 struct ctdb_tcp_node
*tnode
= talloc_get_type(node
->private_data
,
77 struct ctdb_tcp_node
);
78 struct ctdb_context
*ctdb
= node
->ctdb
;
80 socklen_t len
= sizeof(error
);
83 talloc_free(tnode
->connect_te
);
84 tnode
->connect_te
= NULL
;
86 if (getsockopt(tnode
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
) != 0 ||
88 ctdb_tcp_stop_connection(node
);
89 tnode
->connect_te
= event_add_timed(ctdb
->ev
, tnode
,
90 timeval_current_ofs(1, 0),
91 ctdb_tcp_node_connect
, node
);
95 talloc_free(tnode
->connect_fde
);
96 tnode
->connect_fde
= NULL
;
98 if (setsockopt(tnode
->fd
,IPPROTO_TCP
,TCP_NODELAY
,(char *)&one
,sizeof(one
)) == -1) {
99 DEBUG(DEBUG_WARNING
, ("Failed to set TCP_NODELAY on fd - %s\n",
102 if (setsockopt(tnode
->fd
,SOL_SOCKET
,SO_KEEPALIVE
,(char *)&one
,sizeof(one
)) == -1) {
103 DEBUG(DEBUG_WARNING
, ("Failed to set KEEPALIVE on fd - %s\n",
107 ctdb_queue_set_fd(tnode
->out_queue
, tnode
->fd
);
109 /* the queue subsystem now owns this fd */
115 called when we should try and establish a tcp connection to a node
117 void ctdb_tcp_node_connect(struct event_context
*ev
, struct timed_event
*te
,
118 struct timeval t
, void *private_data
)
120 struct ctdb_node
*node
= talloc_get_type(private_data
,
122 struct ctdb_tcp_node
*tnode
= talloc_get_type(node
->private_data
,
123 struct ctdb_tcp_node
);
124 struct ctdb_context
*ctdb
= node
->ctdb
;
125 ctdb_sock_addr sock_in
;
128 ctdb_sock_addr sock_out
;
130 ctdb_tcp_stop_connection(node
);
132 sock_out
= node
->address
;
134 tnode
->fd
= socket(sock_out
.sa
.sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
135 if (tnode
->fd
== -1) {
136 DEBUG(DEBUG_ERR
, (__location__
"Failed to create socket\n"));
139 set_nonblocking(tnode
->fd
);
140 set_close_on_exec(tnode
->fd
);
142 DEBUG(DEBUG_DEBUG
, (__location__
" Created TCP SOCKET FD:%d\n", tnode
->fd
));
144 /* Bind our side of the socketpair to the same address we use to listen
145 * on incoming CTDB traffic.
146 * We must specify this address to make sure that the address we expose to
147 * the remote side is actually routable in case CTDB traffic will run on
148 * a dedicated non-routeable network.
150 sock_in
= *ctdb
->address
;
152 /* AIX libs check to see if the socket address and length
153 arguments are consistent with each other on calls like
154 connect(). Can not get by with just sizeof(sock_in),
155 need sizeof(sock_in.ip).
157 switch (sock_in
.sa
.sa_family
) {
159 sock_in
.ip
.sin_port
= 0 /* Any port */;
160 sockin_size
= sizeof(sock_in
.ip
);
161 sockout_size
= sizeof(sock_out
.ip
);
164 sock_in
.ip6
.sin6_port
= 0 /* Any port */;
165 sockin_size
= sizeof(sock_in
.ip6
);
166 sockout_size
= sizeof(sock_out
.ip6
);
169 DEBUG(DEBUG_ERR
, (__location__
" unknown family %u\n",
170 sock_in
.sa
.sa_family
));
175 if (bind(tnode
->fd
, (struct sockaddr
*)&sock_in
, sockin_size
) == -1) {
176 DEBUG(DEBUG_ERR
, (__location__
"Failed to bind socket %s(%d)\n",
177 strerror(errno
), errno
));
182 if (connect(tnode
->fd
, (struct sockaddr
*)&sock_out
, sockout_size
) != 0 &&
183 errno
!= EINPROGRESS
) {
184 ctdb_tcp_stop_connection(node
);
185 tnode
->connect_te
= event_add_timed(ctdb
->ev
, tnode
,
186 timeval_current_ofs(1, 0),
187 ctdb_tcp_node_connect
, node
);
191 /* non-blocking connect - wait for write event */
192 tnode
->connect_fde
= event_add_fd(node
->ctdb
->ev
, tnode
, tnode
->fd
,
193 EVENT_FD_WRITE
|EVENT_FD_READ
,
194 ctdb_node_connect_write
, node
);
196 /* don't give it long to connect - retry in one second. This ensures
197 that we find a node is up quickly (tcp normally backs off a syn reply
198 delay by quite a lot) */
199 tnode
->connect_te
= event_add_timed(ctdb
->ev
, tnode
, timeval_current_ofs(1, 0),
200 ctdb_tcp_node_connect
, node
);
204 called when we get contacted by another node
205 currently makes no attempt to check if the connection is really from a ctdb
208 static void ctdb_listen_event(struct event_context
*ev
, struct fd_event
*fde
,
209 uint16_t flags
, void *private_data
)
211 struct ctdb_context
*ctdb
= talloc_get_type(private_data
, struct ctdb_context
);
212 struct ctdb_tcp
*ctcp
= talloc_get_type(ctdb
->private_data
, struct ctdb_tcp
);
216 struct ctdb_incoming
*in
;
219 memset(&addr
, 0, sizeof(addr
));
221 fd
= accept(ctcp
->listen_fd
, (struct sockaddr
*)&addr
, &len
);
222 if (fd
== -1) return;
224 nodeid
= ctdb_ip_to_nodeid(ctdb
, &addr
);
227 DEBUG(DEBUG_ERR
, ("Refused connection from unknown node %s\n", ctdb_addr_to_str(&addr
)));
232 in
= talloc_zero(ctcp
, struct ctdb_incoming
);
236 set_nonblocking(in
->fd
);
237 set_close_on_exec(in
->fd
);
239 DEBUG(DEBUG_DEBUG
, (__location__
" Created SOCKET FD:%d to incoming ctdb connection\n", fd
));
241 if (setsockopt(in
->fd
,SOL_SOCKET
,SO_KEEPALIVE
,(char *)&one
,sizeof(one
)) == -1) {
242 DEBUG(DEBUG_WARNING
, ("Failed to set KEEPALIVE on fd - %s\n",
246 in
->queue
= ctdb_queue_setup(ctdb
, in
, in
->fd
, CTDB_TCP_ALIGNMENT
,
247 ctdb_tcp_read_cb
, in
, "ctdbd-%s", ctdb_addr_to_str(&addr
));
252 automatically find which address to listen on
254 static int ctdb_tcp_listen_automatic(struct ctdb_context
*ctdb
)
256 struct ctdb_tcp
*ctcp
= talloc_get_type(ctdb
->private_data
,
260 const char *lock_path
= CTDB_RUNDIR
"/.socket_lock";
264 struct tevent_fd
*fde
;
266 /* If there are no nodes, then it won't be possible to find
267 * the first one. Log a failure and short circuit the whole
270 if (ctdb
->num_nodes
== 0) {
271 DEBUG(DEBUG_CRIT
,("No nodes available to attempt bind to - is the nodes file empty?\n"));
275 /* in order to ensure that we don't get two nodes with the
276 same adddress, we must make the bind() and listen() calls
277 atomic. The SO_REUSEADDR setsockopt only prevents double
278 binds if the first socket is in LISTEN state */
279 lock_fd
= open(lock_path
, O_RDWR
|O_CREAT
, 0666);
281 DEBUG(DEBUG_CRIT
,("Unable to open %s\n", lock_path
));
285 lock
.l_type
= F_WRLCK
;
286 lock
.l_whence
= SEEK_SET
;
291 if (fcntl(lock_fd
, F_SETLKW
, &lock
) != 0) {
292 DEBUG(DEBUG_CRIT
,("Unable to lock %s\n", lock_path
));
297 for (i
=0; i
< ctdb
->num_nodes
; i
++) {
298 if (ctdb
->nodes
[i
]->flags
& NODE_FLAGS_DELETED
) {
301 sock
= ctdb
->nodes
[i
]->address
;
303 switch (sock
.sa
.sa_family
) {
305 sock_size
= sizeof(sock
.ip
);
308 sock_size
= sizeof(sock
.ip6
);
311 DEBUG(DEBUG_ERR
, (__location__
" unknown family %u\n",
316 ctcp
->listen_fd
= socket(sock
.sa
.sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
317 if (ctcp
->listen_fd
== -1) {
318 ctdb_set_error(ctdb
, "socket failed\n");
322 set_close_on_exec(ctcp
->listen_fd
);
324 if (setsockopt(ctcp
->listen_fd
,SOL_SOCKET
,SO_REUSEADDR
,
325 (char *)&one
,sizeof(one
)) == -1) {
326 DEBUG(DEBUG_WARNING
, ("Failed to set REUSEADDR on fd - %s\n",
330 if (bind(ctcp
->listen_fd
, (struct sockaddr
* )&sock
, sock_size
) == 0) {
334 if (errno
== EADDRNOTAVAIL
) {
335 DEBUG(DEBUG_DEBUG
,(__location__
" Failed to bind() to socket. %s(%d)\n",
336 strerror(errno
), errno
));
338 DEBUG(DEBUG_ERR
,(__location__
" Failed to bind() to socket. %s(%d)\n",
339 strerror(errno
), errno
));
343 if (i
== ctdb
->num_nodes
) {
344 DEBUG(DEBUG_CRIT
,("Unable to bind to any of the node addresses - giving up\n"));
347 ctdb
->address
= talloc_memdup(ctdb
,
348 &ctdb
->nodes
[i
]->address
,
349 sizeof(ctdb_sock_addr
));
350 CTDB_NO_MEMORY(ctdb
, ctdb
->address
);
351 ctdb
->name
= talloc_asprintf(ctdb
, "%s:%u",
352 ctdb_addr_to_str(ctdb
->address
),
353 ctdb_addr_to_port(ctdb
->address
));
354 DEBUG(DEBUG_INFO
,("ctdb chose network address %s\n", ctdb
->name
));
356 if (listen(ctcp
->listen_fd
, 10) == -1) {
360 fde
= event_add_fd(ctdb
->ev
, ctcp
, ctcp
->listen_fd
, EVENT_FD_READ
,
361 ctdb_listen_event
, ctdb
);
362 tevent_fd_set_auto_close(fde
);
370 if (ctcp
->listen_fd
!= -1) {
371 close(ctcp
->listen_fd
);
372 ctcp
->listen_fd
= -1;
379 listen on our own address
381 int ctdb_tcp_listen(struct ctdb_context
*ctdb
)
383 struct ctdb_tcp
*ctcp
= talloc_get_type(ctdb
->private_data
,
388 struct tevent_fd
*fde
;
390 /* we can either auto-bind to the first available address, or we can
391 use a specified address */
392 if (!ctdb
->address
) {
393 return ctdb_tcp_listen_automatic(ctdb
);
396 sock
= *ctdb
->address
;
398 switch (sock
.sa
.sa_family
) {
400 sock_size
= sizeof(sock
.ip
);
403 sock_size
= sizeof(sock
.ip6
);
406 DEBUG(DEBUG_ERR
, (__location__
" unknown family %u\n",
411 ctcp
->listen_fd
= socket(sock
.sa
.sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
412 if (ctcp
->listen_fd
== -1) {
413 ctdb_set_error(ctdb
, "socket failed\n");
417 set_close_on_exec(ctcp
->listen_fd
);
419 if (setsockopt(ctcp
->listen_fd
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&one
,sizeof(one
)) == -1) {
420 DEBUG(DEBUG_WARNING
, ("Failed to set REUSEADDR on fd - %s\n",
424 if (bind(ctcp
->listen_fd
, (struct sockaddr
* )&sock
, sock_size
) != 0) {
425 DEBUG(DEBUG_ERR
,(__location__
" Failed to bind() to socket. %s(%d)\n", strerror(errno
), errno
));
429 if (listen(ctcp
->listen_fd
, 10) == -1) {
433 fde
= event_add_fd(ctdb
->ev
, ctcp
, ctcp
->listen_fd
, EVENT_FD_READ
,
434 ctdb_listen_event
, ctdb
);
435 tevent_fd_set_auto_close(fde
);
440 if (ctcp
->listen_fd
!= -1) {
441 close(ctcp
->listen_fd
);
443 ctcp
->listen_fd
= -1;