1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
15 * This is the "low-level" comms layer.
17 * It is responsible for sending/receiving messages
18 * from other nodes in the cluster.
20 * Cluster nodes are referred to by their nodeids. nodeids are
21 * simply 32 bit numbers to the locking module - if they need to
22 * be expanded for the cluster infrastructure then that is its
23 * responsibility. It is this layer's
24 * responsibility to resolve these into IP address or
25 * whatever it needs for inter-node communication.
27 * The comms level is two kernel threads that deal mainly with
28 * the receiving of messages from other nodes and passing them
29 * up to the mid-level comms layer (which understands the
30 * message format) for execution by the locking core, and
31 * a send thread which does all the setting up of connections
32 * to remote nodes and the sending of data. Threads are not allowed
33 * to send their own data because it may cause them to wait in times
34 * of high load. Also, this way, the sending thread can collect together
35 * messages bound for one node and send them in one block.
37 * lowcomms will choose to use either TCP or SCTP as its transport layer
38 * depending on the configuration variable 'protocol'. This should be set
39 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
40 * cluster-wide mechanism as it must be the same on all nodes of the cluster
41 * for the DLM to function.
45 #include <asm/ioctls.h>
48 #include <linux/pagemap.h>
49 #include <linux/file.h>
50 #include <linux/mutex.h>
51 #include <linux/sctp.h>
52 #include <linux/slab.h>
53 #include <net/sctp/sctp.h>
56 #include "dlm_internal.h"
61 #define NEEDED_RMEM (4*1024*1024)
62 #define CONN_HASH_SIZE 32
64 /* Number of messages to send before rescheduling */
65 #define MAX_SEND_MSG_COUNT 25
66 #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
69 struct socket *sock; /* NULL if not connected */
70 uint32_t nodeid; /* So we know who we are in the list */
71 struct mutex sock_mutex;
73 #define CF_READ_PENDING 1
74 #define CF_WRITE_PENDING 2
75 #define CF_INIT_PENDING 4
76 #define CF_IS_OTHERCON 5
78 #define CF_APP_LIMITED 7
81 struct list_head writequeue; /* List of outgoing writequeue_entries */
82 spinlock_t writequeue_lock;
83 int (*rx_action) (struct connection *); /* What to do when active */
84 void (*connect_action) (struct connection *); /* What to do to connect */
85 void (*shutdown_action)(struct connection *con); /* What to do to shutdown */
87 #define MAX_CONNECT_RETRIES 3
88 struct hlist_node list;
89 struct connection *othercon;
90 struct work_struct rwork; /* Receive workqueue */
91 struct work_struct swork; /* Send workqueue */
92 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
93 unsigned char *rx_buf;
98 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
100 /* An entry waiting to be sent */
101 struct writequeue_entry {
102 struct list_head list;
108 struct connection *con;
111 struct dlm_node_addr {
112 struct list_head list;
116 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
119 static struct listen_sock_callbacks {
120 void (*sk_error_report)(struct sock *);
121 void (*sk_data_ready)(struct sock *);
122 void (*sk_state_change)(struct sock *);
123 void (*sk_write_space)(struct sock *);
126 static LIST_HEAD(dlm_node_addrs);
127 static DEFINE_SPINLOCK(dlm_node_addrs_spin);
129 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
130 static int dlm_local_count;
131 static int dlm_allow_conn;
134 static struct workqueue_struct *recv_workqueue;
135 static struct workqueue_struct *send_workqueue;
137 static struct hlist_head connection_hash[CONN_HASH_SIZE];
138 static DEFINE_SPINLOCK(connections_lock);
139 DEFINE_STATIC_SRCU(connections_srcu);
141 static void process_recv_sockets(struct work_struct *work);
142 static void process_send_sockets(struct work_struct *work);
145 /* This is deliberately very simple because most clusters have simple
146 sequential nodeids, so we should be able to go straight to a connection
147 struct in the array */
148 static inline int nodeid_hash(int nodeid)
150 return nodeid & (CONN_HASH_SIZE-1);
153 static struct connection *__find_con(int nodeid)
156 struct connection *con;
158 r = nodeid_hash(nodeid);
160 idx = srcu_read_lock(&connections_srcu);
161 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
162 if (con->nodeid == nodeid) {
163 srcu_read_unlock(&connections_srcu, idx);
167 srcu_read_unlock(&connections_srcu, idx);
173 * If 'allocation' is zero then we don't attempt to create a new
174 * connection structure for this node.
176 static struct connection *nodeid2con(int nodeid, gfp_t alloc)
178 struct connection *con = NULL;
181 con = __find_con(nodeid);
185 con = kzalloc(sizeof(*con), alloc);
189 con->rx_buflen = dlm_config.ci_buffer_size;
190 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
196 con->nodeid = nodeid;
197 mutex_init(&con->sock_mutex);
198 INIT_LIST_HEAD(&con->writequeue);
199 spin_lock_init(&con->writequeue_lock);
200 INIT_WORK(&con->swork, process_send_sockets);
201 INIT_WORK(&con->rwork, process_recv_sockets);
202 init_waitqueue_head(&con->shutdown_wait);
204 /* Setup action pointers for child sockets */
206 struct connection *zerocon = __find_con(0);
208 con->connect_action = zerocon->connect_action;
210 con->rx_action = zerocon->rx_action;
213 r = nodeid_hash(nodeid);
215 spin_lock(&connections_lock);
216 hlist_add_head_rcu(&con->list, &connection_hash[r]);
217 spin_unlock(&connections_lock);
222 /* Loop round all connections */
223 static void foreach_conn(void (*conn_func)(struct connection *c))
226 struct connection *con;
228 idx = srcu_read_lock(&connections_srcu);
229 for (i = 0; i < CONN_HASH_SIZE; i++) {
230 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
233 srcu_read_unlock(&connections_srcu, idx);
236 static struct dlm_node_addr *find_node_addr(int nodeid)
238 struct dlm_node_addr *na;
240 list_for_each_entry(na, &dlm_node_addrs, list) {
241 if (na->nodeid == nodeid)
247 static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
249 switch (x->ss_family) {
251 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
252 struct sockaddr_in *siny = (struct sockaddr_in *)y;
253 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
255 if (sinx->sin_port != siny->sin_port)
260 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
261 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
262 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
264 if (sinx->sin6_port != siny->sin6_port)
274 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
275 struct sockaddr *sa_out, bool try_new_addr)
277 struct sockaddr_storage sas;
278 struct dlm_node_addr *na;
280 if (!dlm_local_count)
283 spin_lock(&dlm_node_addrs_spin);
284 na = find_node_addr(nodeid);
285 if (na && na->addr_count) {
286 memcpy(&sas, na->addr[na->curr_addr_index],
287 sizeof(struct sockaddr_storage));
290 na->curr_addr_index++;
291 if (na->curr_addr_index == na->addr_count)
292 na->curr_addr_index = 0;
295 spin_unlock(&dlm_node_addrs_spin);
304 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
309 if (dlm_local_addr[0]->ss_family == AF_INET) {
310 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
311 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
312 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
314 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
315 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
316 ret6->sin6_addr = in6->sin6_addr;
322 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
324 struct dlm_node_addr *na;
328 spin_lock(&dlm_node_addrs_spin);
329 list_for_each_entry(na, &dlm_node_addrs, list) {
333 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
334 if (addr_compare(na->addr[addr_i], addr)) {
335 *nodeid = na->nodeid;
342 spin_unlock(&dlm_node_addrs_spin);
346 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
348 struct sockaddr_storage *new_addr;
349 struct dlm_node_addr *new_node, *na;
351 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
355 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
361 memcpy(new_addr, addr, len);
363 spin_lock(&dlm_node_addrs_spin);
364 na = find_node_addr(nodeid);
366 new_node->nodeid = nodeid;
367 new_node->addr[0] = new_addr;
368 new_node->addr_count = 1;
369 list_add(&new_node->list, &dlm_node_addrs);
370 spin_unlock(&dlm_node_addrs_spin);
374 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
375 spin_unlock(&dlm_node_addrs_spin);
381 na->addr[na->addr_count++] = new_addr;
382 spin_unlock(&dlm_node_addrs_spin);
387 /* Data available on socket or listen socket received a connect */
388 static void lowcomms_data_ready(struct sock *sk)
390 struct connection *con;
392 read_lock_bh(&sk->sk_callback_lock);
394 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
395 queue_work(recv_workqueue, &con->rwork);
396 read_unlock_bh(&sk->sk_callback_lock);
399 static void lowcomms_write_space(struct sock *sk)
401 struct connection *con;
403 read_lock_bh(&sk->sk_callback_lock);
408 clear_bit(SOCK_NOSPACE, &con->sock->flags);
410 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
411 con->sock->sk->sk_write_pending--;
412 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
415 queue_work(send_workqueue, &con->swork);
417 read_unlock_bh(&sk->sk_callback_lock);
420 static inline void lowcomms_connect_sock(struct connection *con)
422 if (test_bit(CF_CLOSE, &con->flags))
424 queue_work(send_workqueue, &con->swork);
428 static void lowcomms_state_change(struct sock *sk)
430 /* SCTP layer is not calling sk_data_ready when the connection
431 * is done, so we catch the signal through here. Also, it
432 * doesn't switch socket state when entering shutdown, so we
433 * skip the write in that case.
435 if (sk->sk_shutdown) {
436 if (sk->sk_shutdown == RCV_SHUTDOWN)
437 lowcomms_data_ready(sk);
438 } else if (sk->sk_state == TCP_ESTABLISHED) {
439 lowcomms_write_space(sk);
443 int dlm_lowcomms_connect_node(int nodeid)
445 struct connection *con;
447 if (nodeid == dlm_our_nodeid())
450 con = nodeid2con(nodeid, GFP_NOFS);
453 lowcomms_connect_sock(con);
457 static void lowcomms_error_report(struct sock *sk)
459 struct connection *con;
460 struct sockaddr_storage saddr;
461 void (*orig_report)(struct sock *) = NULL;
463 read_lock_bh(&sk->sk_callback_lock);
468 orig_report = listen_sock.sk_error_report;
469 if (con->sock == NULL ||
470 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
471 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
472 "sending to node %d, port %d, "
473 "sk_err=%d/%d\n", dlm_our_nodeid(),
474 con->nodeid, dlm_config.ci_tcp_port,
475 sk->sk_err, sk->sk_err_soft);
476 } else if (saddr.ss_family == AF_INET) {
477 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
479 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
480 "sending to node %d at %pI4, port %d, "
481 "sk_err=%d/%d\n", dlm_our_nodeid(),
482 con->nodeid, &sin4->sin_addr.s_addr,
483 dlm_config.ci_tcp_port, sk->sk_err,
486 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
488 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
489 "sending to node %d at %u.%u.%u.%u, "
490 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
491 con->nodeid, sin6->sin6_addr.s6_addr32[0],
492 sin6->sin6_addr.s6_addr32[1],
493 sin6->sin6_addr.s6_addr32[2],
494 sin6->sin6_addr.s6_addr32[3],
495 dlm_config.ci_tcp_port, sk->sk_err,
499 read_unlock_bh(&sk->sk_callback_lock);
504 /* Note: sk_callback_lock must be locked before calling this function. */
505 static void save_listen_callbacks(struct socket *sock)
507 struct sock *sk = sock->sk;
509 listen_sock.sk_data_ready = sk->sk_data_ready;
510 listen_sock.sk_state_change = sk->sk_state_change;
511 listen_sock.sk_write_space = sk->sk_write_space;
512 listen_sock.sk_error_report = sk->sk_error_report;
515 static void restore_callbacks(struct socket *sock)
517 struct sock *sk = sock->sk;
519 write_lock_bh(&sk->sk_callback_lock);
520 sk->sk_user_data = NULL;
521 sk->sk_data_ready = listen_sock.sk_data_ready;
522 sk->sk_state_change = listen_sock.sk_state_change;
523 sk->sk_write_space = listen_sock.sk_write_space;
524 sk->sk_error_report = listen_sock.sk_error_report;
525 write_unlock_bh(&sk->sk_callback_lock);
528 /* Make a socket active */
529 static void add_sock(struct socket *sock, struct connection *con)
531 struct sock *sk = sock->sk;
533 write_lock_bh(&sk->sk_callback_lock);
536 sk->sk_user_data = con;
537 /* Install a data_ready callback */
538 sk->sk_data_ready = lowcomms_data_ready;
539 sk->sk_write_space = lowcomms_write_space;
540 sk->sk_state_change = lowcomms_state_change;
541 sk->sk_allocation = GFP_NOFS;
542 sk->sk_error_report = lowcomms_error_report;
543 write_unlock_bh(&sk->sk_callback_lock);
546 /* Add the port number to an IPv6 or 4 sockaddr and return the address
548 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
551 saddr->ss_family = dlm_local_addr[0]->ss_family;
552 if (saddr->ss_family == AF_INET) {
553 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
554 in4_addr->sin_port = cpu_to_be16(port);
555 *addr_len = sizeof(struct sockaddr_in);
556 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
558 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
559 in6_addr->sin6_port = cpu_to_be16(port);
560 *addr_len = sizeof(struct sockaddr_in6);
562 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
565 /* Close a remote connection and tidy up */
566 static void close_connection(struct connection *con, bool and_other,
569 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
571 if (tx && !closing && cancel_work_sync(&con->swork)) {
572 log_print("canceled swork for node %d", con->nodeid);
573 clear_bit(CF_WRITE_PENDING, &con->flags);
575 if (rx && !closing && cancel_work_sync(&con->rwork)) {
576 log_print("canceled rwork for node %d", con->nodeid);
577 clear_bit(CF_READ_PENDING, &con->flags);
580 mutex_lock(&con->sock_mutex);
582 restore_callbacks(con->sock);
583 sock_release(con->sock);
586 if (con->othercon && and_other) {
587 /* Will only re-enter once. */
588 close_connection(con->othercon, false, true, true);
591 con->rx_leftover = 0;
593 mutex_unlock(&con->sock_mutex);
594 clear_bit(CF_CLOSING, &con->flags);
597 static void shutdown_connection(struct connection *con)
601 if (cancel_work_sync(&con->swork)) {
602 log_print("canceled swork for node %d", con->nodeid);
603 clear_bit(CF_WRITE_PENDING, &con->flags);
606 mutex_lock(&con->sock_mutex);
607 /* nothing to shutdown */
609 mutex_unlock(&con->sock_mutex);
613 set_bit(CF_SHUTDOWN, &con->flags);
614 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
615 mutex_unlock(&con->sock_mutex);
617 log_print("Connection %p failed to shutdown: %d will force close",
621 ret = wait_event_timeout(con->shutdown_wait,
622 !test_bit(CF_SHUTDOWN, &con->flags),
623 DLM_SHUTDOWN_WAIT_TIMEOUT);
625 log_print("Connection %p shutdown timed out, will force close",
634 clear_bit(CF_SHUTDOWN, &con->flags);
635 close_connection(con, false, true, true);
638 static void dlm_tcp_shutdown(struct connection *con)
641 shutdown_connection(con->othercon);
642 shutdown_connection(con);
645 static int con_realloc_receive_buf(struct connection *con, int newlen)
647 unsigned char *newbuf;
649 newbuf = kmalloc(newlen, GFP_NOFS);
653 /* copy any leftover from last receive */
654 if (con->rx_leftover)
655 memmove(newbuf, con->rx_buf, con->rx_leftover);
657 /* swap to new buffer space */
659 con->rx_buflen = newlen;
660 con->rx_buf = newbuf;
665 /* Data received from remote end */
666 static int receive_from_sock(struct connection *con)
668 int call_again_soon = 0;
673 mutex_lock(&con->sock_mutex);
675 if (con->sock == NULL) {
680 if (con->nodeid == 0) {
685 /* realloc if we get new buffer size to read out */
686 buflen = dlm_config.ci_buffer_size;
687 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
688 ret = con_realloc_receive_buf(con, buflen);
693 /* calculate new buffer parameter regarding last receive and
694 * possible leftover bytes
696 iov.iov_base = con->rx_buf + con->rx_leftover;
697 iov.iov_len = con->rx_buflen - con->rx_leftover;
699 memset(&msg, 0, sizeof(msg));
700 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
701 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
705 else if (ret == iov.iov_len)
708 /* new buflen according readed bytes and leftover from last receive */
709 buflen = ret + con->rx_leftover;
710 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
714 /* calculate leftover bytes from process and put it into begin of
715 * the receive buffer, so next receive we have the full message
716 * at the start address of the receive buffer.
718 con->rx_leftover = buflen - ret;
719 if (con->rx_leftover) {
720 memmove(con->rx_buf, con->rx_buf + ret,
722 call_again_soon = true;
728 mutex_unlock(&con->sock_mutex);
732 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
733 queue_work(recv_workqueue, &con->rwork);
734 mutex_unlock(&con->sock_mutex);
738 mutex_unlock(&con->sock_mutex);
739 if (ret != -EAGAIN) {
740 /* Reconnect when there is something to send */
741 close_connection(con, false, true, false);
743 log_print("connection %p got EOF from %d",
745 /* handling for tcp shutdown */
746 clear_bit(CF_SHUTDOWN, &con->flags);
747 wake_up(&con->shutdown_wait);
748 /* signal to breaking receive worker */
755 /* Listening socket is busy, accept a connection */
756 static int accept_from_sock(struct connection *con)
759 struct sockaddr_storage peeraddr;
760 struct socket *newsock;
763 struct connection *newcon;
764 struct connection *addcon;
767 if (!dlm_allow_conn) {
771 mutex_lock_nested(&con->sock_mutex, 0);
774 mutex_unlock(&con->sock_mutex);
778 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
782 /* Get the connected socket's peer */
783 memset(&peeraddr, 0, sizeof(peeraddr));
784 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
786 result = -ECONNABORTED;
790 /* Get the new node's NODEID */
791 make_sockaddr(&peeraddr, 0, &len);
792 if (addr_to_nodeid(&peeraddr, &nodeid)) {
793 unsigned char *b=(unsigned char *)&peeraddr;
794 log_print("connect from non cluster node");
795 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
796 b, sizeof(struct sockaddr_storage));
797 sock_release(newsock);
798 mutex_unlock(&con->sock_mutex);
802 dlm_comm_mark(nodeid, &mark);
803 sock_set_mark(newsock->sk, mark);
805 log_print("got connection from %d", nodeid);
807 /* Check to see if we already have a connection to this node. This
808 * could happen if the two nodes initiate a connection at roughly
809 * the same time and the connections cross on the wire.
810 * In this case we store the incoming one in "othercon"
812 newcon = nodeid2con(nodeid, GFP_NOFS);
817 mutex_lock_nested(&newcon->sock_mutex, 1);
819 struct connection *othercon = newcon->othercon;
822 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
824 log_print("failed to allocate incoming socket");
825 mutex_unlock(&newcon->sock_mutex);
830 othercon->rx_buflen = dlm_config.ci_buffer_size;
831 othercon->rx_buf = kmalloc(othercon->rx_buflen, GFP_NOFS);
832 if (!othercon->rx_buf) {
833 mutex_unlock(&newcon->sock_mutex);
835 log_print("failed to allocate incoming socket receive buffer");
840 othercon->nodeid = nodeid;
841 othercon->rx_action = receive_from_sock;
842 mutex_init(&othercon->sock_mutex);
843 INIT_LIST_HEAD(&othercon->writequeue);
844 spin_lock_init(&othercon->writequeue_lock);
845 INIT_WORK(&othercon->swork, process_send_sockets);
846 INIT_WORK(&othercon->rwork, process_recv_sockets);
847 init_waitqueue_head(&othercon->shutdown_wait);
848 set_bit(CF_IS_OTHERCON, &othercon->flags);
850 /* close other sock con if we have something new */
851 close_connection(othercon, false, true, false);
854 mutex_lock_nested(&othercon->sock_mutex, 2);
855 newcon->othercon = othercon;
856 add_sock(newsock, othercon);
858 mutex_unlock(&othercon->sock_mutex);
861 newcon->rx_action = receive_from_sock;
862 /* accept copies the sk after we've saved the callbacks, so we
863 don't want to save them a second time or comm errors will
864 result in calling sk_error_report recursively. */
865 add_sock(newsock, newcon);
869 mutex_unlock(&newcon->sock_mutex);
872 * Add it to the active queue in case we got data
873 * between processing the accept adding the socket
874 * to the read_sockets list
876 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
877 queue_work(recv_workqueue, &addcon->rwork);
878 mutex_unlock(&con->sock_mutex);
883 mutex_unlock(&con->sock_mutex);
885 sock_release(newsock);
887 if (result != -EAGAIN)
888 log_print("error accepting connection from node: %d", result);
892 static void free_entry(struct writequeue_entry *e)
894 __free_page(e->page);
899 * writequeue_entry_complete - try to delete and free write queue entry
900 * @e: write queue entry to try to delete
901 * @completed: bytes completed
903 * writequeue_lock must be held.
905 static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
907 e->offset += completed;
910 if (e->len == 0 && e->users == 0) {
917 * sctp_bind_addrs - bind a SCTP socket to all our addresses
919 static int sctp_bind_addrs(struct connection *con, uint16_t port)
921 struct sockaddr_storage localaddr;
922 struct sockaddr *addr = (struct sockaddr *)&localaddr;
923 int i, addr_len, result = 0;
925 for (i = 0; i < dlm_local_count; i++) {
926 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
927 make_sockaddr(&localaddr, port, &addr_len);
930 result = kernel_bind(con->sock, addr, addr_len);
932 result = sock_bind_add(con->sock->sk, addr, addr_len);
935 log_print("Can't bind to %d addr number %d, %d.\n",
936 port, i + 1, result);
943 /* Initiate an SCTP association.
944 This is a special case of send_to_sock() in that we don't yet have a
945 peeled-off socket for this association, so we use the listening socket
946 and add the primary IP address of the remote node.
948 static void sctp_connect_to_sock(struct connection *con)
950 struct sockaddr_storage daddr;
956 if (con->nodeid == 0) {
957 log_print("attempt to connect sock 0 foiled");
961 dlm_comm_mark(con->nodeid, &mark);
963 mutex_lock(&con->sock_mutex);
965 /* Some odd races can cause double-connects, ignore them */
966 if (con->retries++ > MAX_CONNECT_RETRIES)
970 log_print("node %d already connected.", con->nodeid);
974 memset(&daddr, 0, sizeof(daddr));
975 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true);
977 log_print("no address for nodeid %d", con->nodeid);
981 /* Create a socket to communicate with */
982 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
983 SOCK_STREAM, IPPROTO_SCTP, &sock);
987 sock_set_mark(sock->sk, mark);
989 con->rx_action = receive_from_sock;
990 con->connect_action = sctp_connect_to_sock;
993 /* Bind to all addresses. */
994 if (sctp_bind_addrs(con, 0))
997 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
999 log_print("connecting to %d", con->nodeid);
1001 /* Turn off Nagle's algorithm */
1002 sctp_sock_set_nodelay(sock->sk);
1005 * Make sock->ops->connect() function return in specified time,
1006 * since O_NONBLOCK argument in connect() function does not work here,
1007 * then, we should restore the default value of this attribute.
1009 sock_set_sndtimeo(sock->sk, 5);
1010 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
1012 sock_set_sndtimeo(sock->sk, 0);
1014 if (result == -EINPROGRESS)
1025 * Some errors are fatal and this list might need adjusting. For other
1026 * errors we try again until the max number of retries is reached.
1028 if (result != -EHOSTUNREACH &&
1029 result != -ENETUNREACH &&
1030 result != -ENETDOWN &&
1031 result != -EINVAL &&
1032 result != -EPROTONOSUPPORT) {
1033 log_print("connect %d try %d error %d", con->nodeid,
1034 con->retries, result);
1035 mutex_unlock(&con->sock_mutex);
1037 lowcomms_connect_sock(con);
1042 mutex_unlock(&con->sock_mutex);
1045 /* Connect a new socket to its peer */
1046 static void tcp_connect_to_sock(struct connection *con)
1048 struct sockaddr_storage saddr, src_addr;
1050 struct socket *sock = NULL;
1054 if (con->nodeid == 0) {
1055 log_print("attempt to connect sock 0 foiled");
1059 dlm_comm_mark(con->nodeid, &mark);
1061 mutex_lock(&con->sock_mutex);
1062 if (con->retries++ > MAX_CONNECT_RETRIES)
1065 /* Some odd races can cause double-connects, ignore them */
1069 /* Create a socket to communicate with */
1070 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1071 SOCK_STREAM, IPPROTO_TCP, &sock);
1075 sock_set_mark(sock->sk, mark);
1077 memset(&saddr, 0, sizeof(saddr));
1078 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
1080 log_print("no address for nodeid %d", con->nodeid);
1084 con->rx_action = receive_from_sock;
1085 con->connect_action = tcp_connect_to_sock;
1086 con->shutdown_action = dlm_tcp_shutdown;
1087 add_sock(sock, con);
1089 /* Bind to our cluster-known address connecting to avoid
1091 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1092 make_sockaddr(&src_addr, 0, &addr_len);
1093 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1096 log_print("could not bind for connect: %d", result);
1097 /* This *may* not indicate a critical error */
1100 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1102 log_print("connecting to %d", con->nodeid);
1104 /* Turn off Nagle's algorithm */
1105 tcp_sock_set_nodelay(sock->sk);
1107 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1109 if (result == -EINPROGRESS)
1116 sock_release(con->sock);
1122 * Some errors are fatal and this list might need adjusting. For other
1123 * errors we try again until the max number of retries is reached.
1125 if (result != -EHOSTUNREACH &&
1126 result != -ENETUNREACH &&
1127 result != -ENETDOWN &&
1128 result != -EINVAL &&
1129 result != -EPROTONOSUPPORT) {
1130 log_print("connect %d try %d error %d", con->nodeid,
1131 con->retries, result);
1132 mutex_unlock(&con->sock_mutex);
1134 lowcomms_connect_sock(con);
1138 mutex_unlock(&con->sock_mutex);
1142 static struct socket *tcp_create_listen_sock(struct connection *con,
1143 struct sockaddr_storage *saddr)
1145 struct socket *sock = NULL;
1149 if (dlm_local_addr[0]->ss_family == AF_INET)
1150 addr_len = sizeof(struct sockaddr_in);
1152 addr_len = sizeof(struct sockaddr_in6);
1154 /* Create a socket to communicate with */
1155 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1156 SOCK_STREAM, IPPROTO_TCP, &sock);
1158 log_print("Can't create listening comms socket");
1162 sock_set_mark(sock->sk, dlm_config.ci_mark);
1164 /* Turn off Nagle's algorithm */
1165 tcp_sock_set_nodelay(sock->sk);
1167 sock_set_reuseaddr(sock->sk);
1169 write_lock_bh(&sock->sk->sk_callback_lock);
1170 sock->sk->sk_user_data = con;
1171 save_listen_callbacks(sock);
1172 con->rx_action = accept_from_sock;
1173 con->connect_action = tcp_connect_to_sock;
1174 write_unlock_bh(&sock->sk->sk_callback_lock);
1176 /* Bind to our port */
1177 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1178 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1180 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1186 sock_set_keepalive(sock->sk);
1188 result = sock->ops->listen(sock, 5);
1190 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1200 /* Get local addresses */
1201 static void init_local(void)
1203 struct sockaddr_storage sas, *addr;
1206 dlm_local_count = 0;
1207 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1208 if (dlm_our_addr(&sas, i))
1211 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
1214 dlm_local_addr[dlm_local_count++] = addr;
1218 static void deinit_local(void)
1222 for (i = 0; i < dlm_local_count; i++)
1223 kfree(dlm_local_addr[i]);
1226 /* Initialise SCTP socket and bind to all interfaces */
1227 static int sctp_listen_for_all(void)
1229 struct socket *sock = NULL;
1230 int result = -EINVAL;
1231 struct connection *con = nodeid2con(0, GFP_NOFS);
1236 log_print("Using SCTP for communications");
1238 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1239 SOCK_STREAM, IPPROTO_SCTP, &sock);
1241 log_print("Can't create comms socket, check SCTP is loaded");
1245 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1246 sock_set_mark(sock->sk, dlm_config.ci_mark);
1247 sctp_sock_set_nodelay(sock->sk);
1249 write_lock_bh(&sock->sk->sk_callback_lock);
1250 /* Init con struct */
1251 sock->sk->sk_user_data = con;
1252 save_listen_callbacks(sock);
1254 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1255 con->rx_action = accept_from_sock;
1256 con->connect_action = sctp_connect_to_sock;
1258 write_unlock_bh(&sock->sk->sk_callback_lock);
1260 /* Bind to all addresses. */
1261 if (sctp_bind_addrs(con, dlm_config.ci_tcp_port))
1262 goto create_delsock;
1264 result = sock->ops->listen(sock, 5);
1266 log_print("Can't set socket listening");
1267 goto create_delsock;
1279 static int tcp_listen_for_all(void)
1281 struct socket *sock = NULL;
1282 struct connection *con = nodeid2con(0, GFP_NOFS);
1283 int result = -EINVAL;
1288 /* We don't support multi-homed hosts */
1289 if (dlm_local_addr[1] != NULL) {
1290 log_print("TCP protocol can't handle multi-homed hosts, "
1295 log_print("Using TCP for communications");
1297 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1299 add_sock(sock, con);
1303 result = -EADDRINUSE;
1311 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1314 struct writequeue_entry *entry;
1316 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1320 entry->page = alloc_page(allocation);
1335 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1337 struct connection *con;
1338 struct writequeue_entry *e;
1341 con = nodeid2con(nodeid, allocation);
1345 spin_lock(&con->writequeue_lock);
1346 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1347 if ((&e->list == &con->writequeue) ||
1348 (PAGE_SIZE - e->end < len)) {
1355 spin_unlock(&con->writequeue_lock);
1359 *ppc = page_address(e->page) + offset;
1363 e = new_writequeue_entry(con, allocation);
1365 spin_lock(&con->writequeue_lock);
1369 list_add_tail(&e->list, &con->writequeue);
1370 spin_unlock(&con->writequeue_lock);
1376 void dlm_lowcomms_commit_buffer(void *mh)
1378 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1379 struct connection *con = e->con;
1382 spin_lock(&con->writequeue_lock);
1386 e->len = e->end - e->offset;
1387 spin_unlock(&con->writequeue_lock);
1389 queue_work(send_workqueue, &con->swork);
1393 spin_unlock(&con->writequeue_lock);
1397 /* Send a message */
1398 static void send_to_sock(struct connection *con)
1401 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1402 struct writequeue_entry *e;
1406 mutex_lock(&con->sock_mutex);
1407 if (con->sock == NULL)
1410 spin_lock(&con->writequeue_lock);
1412 e = list_entry(con->writequeue.next, struct writequeue_entry,
1414 if ((struct list_head *) e == &con->writequeue)
1419 BUG_ON(len == 0 && e->users == 0);
1420 spin_unlock(&con->writequeue_lock);
1424 ret = kernel_sendpage(con->sock, e->page, offset, len,
1426 if (ret == -EAGAIN || ret == 0) {
1427 if (ret == -EAGAIN &&
1428 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1429 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1430 /* Notify TCP that we're limited by the
1431 * application window size.
1433 set_bit(SOCK_NOSPACE, &con->sock->flags);
1434 con->sock->sk->sk_write_pending++;
1442 /* Don't starve people filling buffers */
1443 if (++count >= MAX_SEND_MSG_COUNT) {
1448 spin_lock(&con->writequeue_lock);
1449 writequeue_entry_complete(e, ret);
1451 spin_unlock(&con->writequeue_lock);
1453 mutex_unlock(&con->sock_mutex);
1457 mutex_unlock(&con->sock_mutex);
1458 close_connection(con, false, false, true);
1459 /* Requeue the send work. When the work daemon runs again, it will try
1460 a new connection, then call this function again. */
1461 queue_work(send_workqueue, &con->swork);
1465 mutex_unlock(&con->sock_mutex);
1466 queue_work(send_workqueue, &con->swork);
1470 static void clean_one_writequeue(struct connection *con)
1472 struct writequeue_entry *e, *safe;
1474 spin_lock(&con->writequeue_lock);
1475 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1479 spin_unlock(&con->writequeue_lock);
1482 /* Called from recovery when it knows that a node has
1484 int dlm_lowcomms_close(int nodeid)
1486 struct connection *con;
1487 struct dlm_node_addr *na;
1489 log_print("closing connection to node %d", nodeid);
1490 con = nodeid2con(nodeid, 0);
1492 set_bit(CF_CLOSE, &con->flags);
1493 close_connection(con, true, true, true);
1494 clean_one_writequeue(con);
1497 spin_lock(&dlm_node_addrs_spin);
1498 na = find_node_addr(nodeid);
1500 list_del(&na->list);
1501 while (na->addr_count--)
1502 kfree(na->addr[na->addr_count]);
1505 spin_unlock(&dlm_node_addrs_spin);
1510 /* Receive workqueue function */
1511 static void process_recv_sockets(struct work_struct *work)
1513 struct connection *con = container_of(work, struct connection, rwork);
1516 clear_bit(CF_READ_PENDING, &con->flags);
1518 err = con->rx_action(con);
1522 /* Send workqueue function */
1523 static void process_send_sockets(struct work_struct *work)
1525 struct connection *con = container_of(work, struct connection, swork);
1527 clear_bit(CF_WRITE_PENDING, &con->flags);
1528 if (con->sock == NULL) /* not mutex protected so check it inside too */
1529 con->connect_action(con);
1530 if (!list_empty(&con->writequeue))
1534 static void work_stop(void)
1537 destroy_workqueue(recv_workqueue);
1539 destroy_workqueue(send_workqueue);
1542 static int work_start(void)
1544 recv_workqueue = alloc_workqueue("dlm_recv",
1545 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1546 if (!recv_workqueue) {
1547 log_print("can't start dlm_recv");
1551 send_workqueue = alloc_workqueue("dlm_send",
1552 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1553 if (!send_workqueue) {
1554 log_print("can't start dlm_send");
1555 destroy_workqueue(recv_workqueue);
1562 static void _stop_conn(struct connection *con, bool and_other)
1564 mutex_lock(&con->sock_mutex);
1565 set_bit(CF_CLOSE, &con->flags);
1566 set_bit(CF_READ_PENDING, &con->flags);
1567 set_bit(CF_WRITE_PENDING, &con->flags);
1568 if (con->sock && con->sock->sk) {
1569 write_lock_bh(&con->sock->sk->sk_callback_lock);
1570 con->sock->sk->sk_user_data = NULL;
1571 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1573 if (con->othercon && and_other)
1574 _stop_conn(con->othercon, false);
1575 mutex_unlock(&con->sock_mutex);
1578 static void stop_conn(struct connection *con)
1580 _stop_conn(con, true);
1583 static void shutdown_conn(struct connection *con)
1585 if (con->shutdown_action)
1586 con->shutdown_action(con);
1589 static void connection_release(struct rcu_head *rcu)
1591 struct connection *con = container_of(rcu, struct connection, rcu);
1597 static void free_conn(struct connection *con)
1599 close_connection(con, true, true, true);
1600 spin_lock(&connections_lock);
1601 hlist_del_rcu(&con->list);
1602 spin_unlock(&connections_lock);
1603 if (con->othercon) {
1604 clean_one_writequeue(con->othercon);
1605 call_rcu(&con->othercon->rcu, connection_release);
1607 clean_one_writequeue(con);
1608 call_rcu(&con->rcu, connection_release);
1611 static void work_flush(void)
1615 struct connection *con;
1619 foreach_conn(stop_conn);
1621 flush_workqueue(recv_workqueue);
1623 flush_workqueue(send_workqueue);
1624 idx = srcu_read_lock(&connections_srcu);
1625 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
1626 hlist_for_each_entry_rcu(con, &connection_hash[i],
1628 ok &= test_bit(CF_READ_PENDING, &con->flags);
1629 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1630 if (con->othercon) {
1631 ok &= test_bit(CF_READ_PENDING,
1632 &con->othercon->flags);
1633 ok &= test_bit(CF_WRITE_PENDING,
1634 &con->othercon->flags);
1638 srcu_read_unlock(&connections_srcu, idx);
1642 void dlm_lowcomms_stop(void)
1644 /* Set all the flags to prevent any
1650 flush_workqueue(recv_workqueue);
1652 flush_workqueue(send_workqueue);
1654 foreach_conn(shutdown_conn);
1656 foreach_conn(free_conn);
1661 int dlm_lowcomms_start(void)
1663 int error = -EINVAL;
1664 struct connection *con;
1667 for (i = 0; i < CONN_HASH_SIZE; i++)
1668 INIT_HLIST_HEAD(&connection_hash[i]);
1671 if (!dlm_local_count) {
1673 log_print("no local IP address has been set");
1677 error = work_start();
1683 /* Start listening */
1684 if (dlm_config.ci_protocol == 0)
1685 error = tcp_listen_for_all();
1687 error = sctp_listen_for_all();
1695 con = nodeid2con(0,0);
1702 void dlm_lowcomms_exit(void)
1704 struct dlm_node_addr *na, *safe;
1706 spin_lock(&dlm_node_addrs_spin);
1707 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1708 list_del(&na->list);
1709 while (na->addr_count--)
1710 kfree(na->addr[na->addr_count]);
1713 spin_unlock(&dlm_node_addrs_spin);