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 #define CF_CONNECTED 10
82 #define CF_RECONNECT 11
83 #define CF_DELAY_CONNECT 12
85 struct list_head writequeue; /* List of outgoing writequeue_entries */
86 spinlock_t writequeue_lock;
87 atomic_t writequeue_cnt;
88 void (*connect_action) (struct connection *); /* What to do to connect */
89 void (*shutdown_action)(struct connection *con); /* What to do to shutdown */
90 bool (*eof_condition)(struct connection *con); /* What to do to eof check */
92 #define MAX_CONNECT_RETRIES 3
93 struct hlist_node list;
94 struct connection *othercon;
95 struct connection *sendcon;
96 struct work_struct rwork; /* Receive workqueue */
97 struct work_struct swork; /* Send workqueue */
98 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
99 unsigned char *rx_buf;
104 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
106 struct listen_connection {
108 struct work_struct rwork;
111 #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
112 #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
114 /* An entry waiting to be sent */
115 struct writequeue_entry {
116 struct list_head list;
122 int idx; /* get()/commit() idx exchange */
123 struct connection *con;
126 struct dlm_node_addr {
127 struct list_head list;
132 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
135 static struct listen_sock_callbacks {
136 void (*sk_error_report)(struct sock *);
137 void (*sk_data_ready)(struct sock *);
138 void (*sk_state_change)(struct sock *);
139 void (*sk_write_space)(struct sock *);
142 static LIST_HEAD(dlm_node_addrs);
143 static DEFINE_SPINLOCK(dlm_node_addrs_spin);
145 static struct listen_connection listen_con;
146 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
147 static int dlm_local_count;
151 static struct workqueue_struct *recv_workqueue;
152 static struct workqueue_struct *send_workqueue;
154 static struct hlist_head connection_hash[CONN_HASH_SIZE];
155 static DEFINE_SPINLOCK(connections_lock);
156 DEFINE_STATIC_SRCU(connections_srcu);
158 static void process_recv_sockets(struct work_struct *work);
159 static void process_send_sockets(struct work_struct *work);
161 static void sctp_connect_to_sock(struct connection *con);
162 static void tcp_connect_to_sock(struct connection *con);
163 static void dlm_tcp_shutdown(struct connection *con);
165 /* This is deliberately very simple because most clusters have simple
166 sequential nodeids, so we should be able to go straight to a connection
167 struct in the array */
168 static inline int nodeid_hash(int nodeid)
170 return nodeid & (CONN_HASH_SIZE-1);
173 static struct connection *__find_con(int nodeid, int r)
175 struct connection *con;
177 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
178 if (con->nodeid == nodeid)
185 static bool tcp_eof_condition(struct connection *con)
187 return atomic_read(&con->writequeue_cnt);
190 static int dlm_con_init(struct connection *con, int nodeid)
192 con->rx_buflen = dlm_config.ci_buffer_size;
193 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
197 con->nodeid = nodeid;
198 mutex_init(&con->sock_mutex);
199 INIT_LIST_HEAD(&con->writequeue);
200 spin_lock_init(&con->writequeue_lock);
201 atomic_set(&con->writequeue_cnt, 0);
202 INIT_WORK(&con->swork, process_send_sockets);
203 INIT_WORK(&con->rwork, process_recv_sockets);
204 init_waitqueue_head(&con->shutdown_wait);
206 if (dlm_config.ci_protocol == 0) {
207 con->connect_action = tcp_connect_to_sock;
208 con->shutdown_action = dlm_tcp_shutdown;
209 con->eof_condition = tcp_eof_condition;
211 con->connect_action = sctp_connect_to_sock;
218 * If 'allocation' is zero then we don't attempt to create a new
219 * connection structure for this node.
221 static struct connection *nodeid2con(int nodeid, gfp_t alloc)
223 struct connection *con, *tmp;
226 r = nodeid_hash(nodeid);
227 con = __find_con(nodeid, r);
231 con = kzalloc(sizeof(*con), alloc);
235 ret = dlm_con_init(con, nodeid);
241 spin_lock(&connections_lock);
242 /* Because multiple workqueues/threads calls this function it can
243 * race on multiple cpu's. Instead of locking hot path __find_con()
244 * we just check in rare cases of recently added nodes again
245 * under protection of connections_lock. If this is the case we
246 * abort our connection creation and return the existing connection.
248 tmp = __find_con(nodeid, r);
250 spin_unlock(&connections_lock);
256 hlist_add_head_rcu(&con->list, &connection_hash[r]);
257 spin_unlock(&connections_lock);
262 /* Loop round all connections */
263 static void foreach_conn(void (*conn_func)(struct connection *c))
266 struct connection *con;
268 for (i = 0; i < CONN_HASH_SIZE; i++) {
269 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
274 static struct dlm_node_addr *find_node_addr(int nodeid)
276 struct dlm_node_addr *na;
278 list_for_each_entry(na, &dlm_node_addrs, list) {
279 if (na->nodeid == nodeid)
285 static int addr_compare(const struct sockaddr_storage *x,
286 const struct sockaddr_storage *y)
288 switch (x->ss_family) {
290 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
291 struct sockaddr_in *siny = (struct sockaddr_in *)y;
292 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
294 if (sinx->sin_port != siny->sin_port)
299 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
300 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
301 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
303 if (sinx->sin6_port != siny->sin6_port)
313 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
314 struct sockaddr *sa_out, bool try_new_addr,
317 struct sockaddr_storage sas;
318 struct dlm_node_addr *na;
320 if (!dlm_local_count)
323 spin_lock(&dlm_node_addrs_spin);
324 na = find_node_addr(nodeid);
325 if (na && na->addr_count) {
326 memcpy(&sas, na->addr[na->curr_addr_index],
327 sizeof(struct sockaddr_storage));
330 na->curr_addr_index++;
331 if (na->curr_addr_index == na->addr_count)
332 na->curr_addr_index = 0;
335 spin_unlock(&dlm_node_addrs_spin);
346 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
351 if (dlm_local_addr[0]->ss_family == AF_INET) {
352 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
353 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
354 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
356 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
357 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
358 ret6->sin6_addr = in6->sin6_addr;
364 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
367 struct dlm_node_addr *na;
371 spin_lock(&dlm_node_addrs_spin);
372 list_for_each_entry(na, &dlm_node_addrs, list) {
376 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
377 if (addr_compare(na->addr[addr_i], addr)) {
378 *nodeid = na->nodeid;
386 spin_unlock(&dlm_node_addrs_spin);
390 /* caller need to held dlm_node_addrs_spin lock */
391 static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
392 const struct sockaddr_storage *addr)
396 for (i = 0; i < na->addr_count; i++) {
397 if (addr_compare(na->addr[i], addr))
404 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
406 struct sockaddr_storage *new_addr;
407 struct dlm_node_addr *new_node, *na;
410 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
414 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
420 memcpy(new_addr, addr, len);
422 spin_lock(&dlm_node_addrs_spin);
423 na = find_node_addr(nodeid);
425 new_node->nodeid = nodeid;
426 new_node->addr[0] = new_addr;
427 new_node->addr_count = 1;
428 new_node->mark = dlm_config.ci_mark;
429 list_add(&new_node->list, &dlm_node_addrs);
430 spin_unlock(&dlm_node_addrs_spin);
434 ret = dlm_lowcomms_na_has_addr(na, addr);
436 spin_unlock(&dlm_node_addrs_spin);
442 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
443 spin_unlock(&dlm_node_addrs_spin);
449 na->addr[na->addr_count++] = new_addr;
450 spin_unlock(&dlm_node_addrs_spin);
455 /* Data available on socket or listen socket received a connect */
456 static void lowcomms_data_ready(struct sock *sk)
458 struct connection *con;
460 read_lock_bh(&sk->sk_callback_lock);
462 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
463 queue_work(recv_workqueue, &con->rwork);
464 read_unlock_bh(&sk->sk_callback_lock);
467 static void lowcomms_listen_data_ready(struct sock *sk)
469 queue_work(recv_workqueue, &listen_con.rwork);
472 static void lowcomms_write_space(struct sock *sk)
474 struct connection *con;
476 read_lock_bh(&sk->sk_callback_lock);
481 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
482 log_print("successful connected to node %d", con->nodeid);
483 queue_work(send_workqueue, &con->swork);
487 clear_bit(SOCK_NOSPACE, &con->sock->flags);
489 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
490 con->sock->sk->sk_write_pending--;
491 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
494 queue_work(send_workqueue, &con->swork);
496 read_unlock_bh(&sk->sk_callback_lock);
499 static inline void lowcomms_connect_sock(struct connection *con)
501 if (test_bit(CF_CLOSE, &con->flags))
503 queue_work(send_workqueue, &con->swork);
507 static void lowcomms_state_change(struct sock *sk)
509 /* SCTP layer is not calling sk_data_ready when the connection
510 * is done, so we catch the signal through here. Also, it
511 * doesn't switch socket state when entering shutdown, so we
512 * skip the write in that case.
514 if (sk->sk_shutdown) {
515 if (sk->sk_shutdown == RCV_SHUTDOWN)
516 lowcomms_data_ready(sk);
517 } else if (sk->sk_state == TCP_ESTABLISHED) {
518 lowcomms_write_space(sk);
522 int dlm_lowcomms_connect_node(int nodeid)
524 struct connection *con;
527 if (nodeid == dlm_our_nodeid())
530 idx = srcu_read_lock(&connections_srcu);
531 con = nodeid2con(nodeid, GFP_NOFS);
533 srcu_read_unlock(&connections_srcu, idx);
537 lowcomms_connect_sock(con);
538 srcu_read_unlock(&connections_srcu, idx);
543 int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
545 struct dlm_node_addr *na;
547 spin_lock(&dlm_node_addrs_spin);
548 na = find_node_addr(nodeid);
550 spin_unlock(&dlm_node_addrs_spin);
555 spin_unlock(&dlm_node_addrs_spin);
560 static void lowcomms_error_report(struct sock *sk)
562 struct connection *con;
563 struct sockaddr_storage saddr;
564 void (*orig_report)(struct sock *) = NULL;
566 read_lock_bh(&sk->sk_callback_lock);
571 orig_report = listen_sock.sk_error_report;
572 if (con->sock == NULL ||
573 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
574 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
575 "sending to node %d, port %d, "
576 "sk_err=%d/%d\n", dlm_our_nodeid(),
577 con->nodeid, dlm_config.ci_tcp_port,
578 sk->sk_err, sk->sk_err_soft);
579 } else if (saddr.ss_family == AF_INET) {
580 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
582 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
583 "sending to node %d at %pI4, port %d, "
584 "sk_err=%d/%d\n", dlm_our_nodeid(),
585 con->nodeid, &sin4->sin_addr.s_addr,
586 dlm_config.ci_tcp_port, sk->sk_err,
589 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
591 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
592 "sending to node %d at %u.%u.%u.%u, "
593 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
594 con->nodeid, sin6->sin6_addr.s6_addr32[0],
595 sin6->sin6_addr.s6_addr32[1],
596 sin6->sin6_addr.s6_addr32[2],
597 sin6->sin6_addr.s6_addr32[3],
598 dlm_config.ci_tcp_port, sk->sk_err,
602 /* below sendcon only handling */
603 if (test_bit(CF_IS_OTHERCON, &con->flags))
606 switch (sk->sk_err) {
608 set_bit(CF_DELAY_CONNECT, &con->flags);
614 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
615 queue_work(send_workqueue, &con->swork);
618 read_unlock_bh(&sk->sk_callback_lock);
623 /* Note: sk_callback_lock must be locked before calling this function. */
624 static void save_listen_callbacks(struct socket *sock)
626 struct sock *sk = sock->sk;
628 listen_sock.sk_data_ready = sk->sk_data_ready;
629 listen_sock.sk_state_change = sk->sk_state_change;
630 listen_sock.sk_write_space = sk->sk_write_space;
631 listen_sock.sk_error_report = sk->sk_error_report;
634 static void restore_callbacks(struct socket *sock)
636 struct sock *sk = sock->sk;
638 write_lock_bh(&sk->sk_callback_lock);
639 sk->sk_user_data = NULL;
640 sk->sk_data_ready = listen_sock.sk_data_ready;
641 sk->sk_state_change = listen_sock.sk_state_change;
642 sk->sk_write_space = listen_sock.sk_write_space;
643 sk->sk_error_report = listen_sock.sk_error_report;
644 write_unlock_bh(&sk->sk_callback_lock);
647 static void add_listen_sock(struct socket *sock, struct listen_connection *con)
649 struct sock *sk = sock->sk;
651 write_lock_bh(&sk->sk_callback_lock);
652 save_listen_callbacks(sock);
655 sk->sk_user_data = con;
656 sk->sk_allocation = GFP_NOFS;
657 /* Install a data_ready callback */
658 sk->sk_data_ready = lowcomms_listen_data_ready;
659 write_unlock_bh(&sk->sk_callback_lock);
662 /* Make a socket active */
663 static void add_sock(struct socket *sock, struct connection *con)
665 struct sock *sk = sock->sk;
667 write_lock_bh(&sk->sk_callback_lock);
670 sk->sk_user_data = con;
671 /* Install a data_ready callback */
672 sk->sk_data_ready = lowcomms_data_ready;
673 sk->sk_write_space = lowcomms_write_space;
674 sk->sk_state_change = lowcomms_state_change;
675 sk->sk_allocation = GFP_NOFS;
676 sk->sk_error_report = lowcomms_error_report;
677 write_unlock_bh(&sk->sk_callback_lock);
680 /* Add the port number to an IPv6 or 4 sockaddr and return the address
682 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
685 saddr->ss_family = dlm_local_addr[0]->ss_family;
686 if (saddr->ss_family == AF_INET) {
687 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
688 in4_addr->sin_port = cpu_to_be16(port);
689 *addr_len = sizeof(struct sockaddr_in);
690 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
692 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
693 in6_addr->sin6_port = cpu_to_be16(port);
694 *addr_len = sizeof(struct sockaddr_in6);
696 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
699 static void dlm_close_sock(struct socket **sock)
702 restore_callbacks(*sock);
708 /* Close a remote connection and tidy up */
709 static void close_connection(struct connection *con, bool and_other,
712 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
714 if (tx && !closing && cancel_work_sync(&con->swork)) {
715 log_print("canceled swork for node %d", con->nodeid);
716 clear_bit(CF_WRITE_PENDING, &con->flags);
718 if (rx && !closing && cancel_work_sync(&con->rwork)) {
719 log_print("canceled rwork for node %d", con->nodeid);
720 clear_bit(CF_READ_PENDING, &con->flags);
723 mutex_lock(&con->sock_mutex);
724 dlm_close_sock(&con->sock);
726 if (con->othercon && and_other) {
727 /* Will only re-enter once. */
728 close_connection(con->othercon, false, tx, rx);
731 con->rx_leftover = 0;
733 clear_bit(CF_CONNECTED, &con->flags);
734 clear_bit(CF_DELAY_CONNECT, &con->flags);
735 clear_bit(CF_RECONNECT, &con->flags);
736 clear_bit(CF_EOF, &con->flags);
737 mutex_unlock(&con->sock_mutex);
738 clear_bit(CF_CLOSING, &con->flags);
741 static void shutdown_connection(struct connection *con)
745 flush_work(&con->swork);
747 mutex_lock(&con->sock_mutex);
748 /* nothing to shutdown */
750 mutex_unlock(&con->sock_mutex);
754 set_bit(CF_SHUTDOWN, &con->flags);
755 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
756 mutex_unlock(&con->sock_mutex);
758 log_print("Connection %p failed to shutdown: %d will force close",
762 ret = wait_event_timeout(con->shutdown_wait,
763 !test_bit(CF_SHUTDOWN, &con->flags),
764 DLM_SHUTDOWN_WAIT_TIMEOUT);
766 log_print("Connection %p shutdown timed out, will force close",
775 clear_bit(CF_SHUTDOWN, &con->flags);
776 close_connection(con, false, true, true);
779 static void dlm_tcp_shutdown(struct connection *con)
782 shutdown_connection(con->othercon);
783 shutdown_connection(con);
786 static int con_realloc_receive_buf(struct connection *con, int newlen)
788 unsigned char *newbuf;
790 newbuf = kmalloc(newlen, GFP_NOFS);
794 /* copy any leftover from last receive */
795 if (con->rx_leftover)
796 memmove(newbuf, con->rx_buf, con->rx_leftover);
798 /* swap to new buffer space */
800 con->rx_buflen = newlen;
801 con->rx_buf = newbuf;
806 /* Data received from remote end */
807 static int receive_from_sock(struct connection *con)
809 int call_again_soon = 0;
814 mutex_lock(&con->sock_mutex);
816 if (con->sock == NULL) {
821 /* realloc if we get new buffer size to read out */
822 buflen = dlm_config.ci_buffer_size;
823 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
824 ret = con_realloc_receive_buf(con, buflen);
829 /* calculate new buffer parameter regarding last receive and
830 * possible leftover bytes
832 iov.iov_base = con->rx_buf + con->rx_leftover;
833 iov.iov_len = con->rx_buflen - con->rx_leftover;
835 memset(&msg, 0, sizeof(msg));
836 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
837 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
841 else if (ret == iov.iov_len)
844 /* new buflen according readed bytes and leftover from last receive */
845 buflen = ret + con->rx_leftover;
846 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
850 /* calculate leftover bytes from process and put it into begin of
851 * the receive buffer, so next receive we have the full message
852 * at the start address of the receive buffer.
854 con->rx_leftover = buflen - ret;
855 if (con->rx_leftover) {
856 memmove(con->rx_buf, con->rx_buf + ret,
858 call_again_soon = true;
864 mutex_unlock(&con->sock_mutex);
868 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
869 queue_work(recv_workqueue, &con->rwork);
870 mutex_unlock(&con->sock_mutex);
875 log_print("connection %p got EOF from %d",
878 if (con->eof_condition && con->eof_condition(con)) {
879 set_bit(CF_EOF, &con->flags);
880 mutex_unlock(&con->sock_mutex);
882 mutex_unlock(&con->sock_mutex);
883 close_connection(con, false, true, false);
885 /* handling for tcp shutdown */
886 clear_bit(CF_SHUTDOWN, &con->flags);
887 wake_up(&con->shutdown_wait);
890 /* signal to breaking receive worker */
893 mutex_unlock(&con->sock_mutex);
898 /* Listening socket is busy, accept a connection */
899 static int accept_from_sock(struct listen_connection *con)
902 struct sockaddr_storage peeraddr;
903 struct socket *newsock;
906 struct connection *newcon;
907 struct connection *addcon;
910 if (!dlm_allow_conn) {
917 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
921 /* Get the connected socket's peer */
922 memset(&peeraddr, 0, sizeof(peeraddr));
923 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
925 result = -ECONNABORTED;
929 /* Get the new node's NODEID */
930 make_sockaddr(&peeraddr, 0, &len);
931 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
932 unsigned char *b=(unsigned char *)&peeraddr;
933 log_print("connect from non cluster node");
934 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
935 b, sizeof(struct sockaddr_storage));
936 sock_release(newsock);
940 log_print("got connection from %d", nodeid);
942 /* Check to see if we already have a connection to this node. This
943 * could happen if the two nodes initiate a connection at roughly
944 * the same time and the connections cross on the wire.
945 * In this case we store the incoming one in "othercon"
947 idx = srcu_read_lock(&connections_srcu);
948 newcon = nodeid2con(nodeid, GFP_NOFS);
950 srcu_read_unlock(&connections_srcu, idx);
955 sock_set_mark(newsock->sk, mark);
957 mutex_lock(&newcon->sock_mutex);
959 struct connection *othercon = newcon->othercon;
962 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
964 log_print("failed to allocate incoming socket");
965 mutex_unlock(&newcon->sock_mutex);
966 srcu_read_unlock(&connections_srcu, idx);
971 result = dlm_con_init(othercon, nodeid);
974 mutex_unlock(&newcon->sock_mutex);
975 srcu_read_unlock(&connections_srcu, idx);
979 lockdep_set_subclass(&othercon->sock_mutex, 1);
980 newcon->othercon = othercon;
981 othercon->sendcon = newcon;
983 /* close other sock con if we have something new */
984 close_connection(othercon, false, true, false);
987 mutex_lock(&othercon->sock_mutex);
988 add_sock(newsock, othercon);
990 mutex_unlock(&othercon->sock_mutex);
993 /* accept copies the sk after we've saved the callbacks, so we
994 don't want to save them a second time or comm errors will
995 result in calling sk_error_report recursively. */
996 add_sock(newsock, newcon);
1000 set_bit(CF_CONNECTED, &addcon->flags);
1001 mutex_unlock(&newcon->sock_mutex);
1004 * Add it to the active queue in case we got data
1005 * between processing the accept adding the socket
1006 * to the read_sockets list
1008 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1009 queue_work(recv_workqueue, &addcon->rwork);
1011 srcu_read_unlock(&connections_srcu, idx);
1017 sock_release(newsock);
1019 if (result != -EAGAIN)
1020 log_print("error accepting connection from node: %d", result);
1024 static void free_entry(struct writequeue_entry *e)
1026 __free_page(e->page);
1031 * writequeue_entry_complete - try to delete and free write queue entry
1032 * @e: write queue entry to try to delete
1033 * @completed: bytes completed
1035 * writequeue_lock must be held.
1037 static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1039 e->offset += completed;
1040 e->len -= completed;
1042 if (e->len == 0 && e->users == 0) {
1044 atomic_dec(&e->con->writequeue_cnt);
1050 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1052 static int sctp_bind_addrs(struct socket *sock, uint16_t port)
1054 struct sockaddr_storage localaddr;
1055 struct sockaddr *addr = (struct sockaddr *)&localaddr;
1056 int i, addr_len, result = 0;
1058 for (i = 0; i < dlm_local_count; i++) {
1059 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1060 make_sockaddr(&localaddr, port, &addr_len);
1063 result = kernel_bind(sock, addr, addr_len);
1065 result = sock_bind_add(sock->sk, addr, addr_len);
1068 log_print("Can't bind to %d addr number %d, %d.\n",
1069 port, i + 1, result);
1076 /* Initiate an SCTP association.
1077 This is a special case of send_to_sock() in that we don't yet have a
1078 peeled-off socket for this association, so we use the listening socket
1079 and add the primary IP address of the remote node.
1081 static void sctp_connect_to_sock(struct connection *con)
1083 struct sockaddr_storage daddr;
1086 struct socket *sock;
1089 mutex_lock(&con->sock_mutex);
1091 /* Some odd races can cause double-connects, ignore them */
1092 if (con->retries++ > MAX_CONNECT_RETRIES)
1096 log_print("node %d already connected.", con->nodeid);
1100 memset(&daddr, 0, sizeof(daddr));
1101 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true, &mark);
1103 log_print("no address for nodeid %d", con->nodeid);
1107 /* Create a socket to communicate with */
1108 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1109 SOCK_STREAM, IPPROTO_SCTP, &sock);
1113 sock_set_mark(sock->sk, mark);
1115 add_sock(sock, con);
1117 /* Bind to all addresses. */
1118 if (sctp_bind_addrs(con->sock, 0))
1121 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
1123 log_print("connecting to %d", con->nodeid);
1125 /* Turn off Nagle's algorithm */
1126 sctp_sock_set_nodelay(sock->sk);
1129 * Make sock->ops->connect() function return in specified time,
1130 * since O_NONBLOCK argument in connect() function does not work here,
1131 * then, we should restore the default value of this attribute.
1133 sock_set_sndtimeo(sock->sk, 5);
1134 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
1136 sock_set_sndtimeo(sock->sk, 0);
1138 if (result == -EINPROGRESS)
1141 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1142 log_print("successful connected to node %d", con->nodeid);
1152 * Some errors are fatal and this list might need adjusting. For other
1153 * errors we try again until the max number of retries is reached.
1155 if (result != -EHOSTUNREACH &&
1156 result != -ENETUNREACH &&
1157 result != -ENETDOWN &&
1158 result != -EINVAL &&
1159 result != -EPROTONOSUPPORT) {
1160 log_print("connect %d try %d error %d", con->nodeid,
1161 con->retries, result);
1162 mutex_unlock(&con->sock_mutex);
1164 lowcomms_connect_sock(con);
1169 mutex_unlock(&con->sock_mutex);
1172 /* Connect a new socket to its peer */
1173 static void tcp_connect_to_sock(struct connection *con)
1175 struct sockaddr_storage saddr, src_addr;
1178 struct socket *sock = NULL;
1181 mutex_lock(&con->sock_mutex);
1182 if (con->retries++ > MAX_CONNECT_RETRIES)
1185 /* Some odd races can cause double-connects, ignore them */
1189 /* Create a socket to communicate with */
1190 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1191 SOCK_STREAM, IPPROTO_TCP, &sock);
1195 memset(&saddr, 0, sizeof(saddr));
1196 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false, &mark);
1198 log_print("no address for nodeid %d", con->nodeid);
1202 sock_set_mark(sock->sk, mark);
1204 add_sock(sock, con);
1206 /* Bind to our cluster-known address connecting to avoid
1208 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1209 make_sockaddr(&src_addr, 0, &addr_len);
1210 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1213 log_print("could not bind for connect: %d", result);
1214 /* This *may* not indicate a critical error */
1217 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1219 log_print("connecting to %d", con->nodeid);
1221 /* Turn off Nagle's algorithm */
1222 tcp_sock_set_nodelay(sock->sk);
1224 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1226 if (result == -EINPROGRESS)
1233 sock_release(con->sock);
1239 * Some errors are fatal and this list might need adjusting. For other
1240 * errors we try again until the max number of retries is reached.
1242 if (result != -EHOSTUNREACH &&
1243 result != -ENETUNREACH &&
1244 result != -ENETDOWN &&
1245 result != -EINVAL &&
1246 result != -EPROTONOSUPPORT) {
1247 log_print("connect %d try %d error %d", con->nodeid,
1248 con->retries, result);
1249 mutex_unlock(&con->sock_mutex);
1251 lowcomms_connect_sock(con);
1255 mutex_unlock(&con->sock_mutex);
1259 /* On error caller must run dlm_close_sock() for the
1260 * listen connection socket.
1262 static int tcp_create_listen_sock(struct listen_connection *con,
1263 struct sockaddr_storage *saddr)
1265 struct socket *sock = NULL;
1269 if (dlm_local_addr[0]->ss_family == AF_INET)
1270 addr_len = sizeof(struct sockaddr_in);
1272 addr_len = sizeof(struct sockaddr_in6);
1274 /* Create a socket to communicate with */
1275 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1276 SOCK_STREAM, IPPROTO_TCP, &sock);
1278 log_print("Can't create listening comms socket");
1282 sock_set_mark(sock->sk, dlm_config.ci_mark);
1284 /* Turn off Nagle's algorithm */
1285 tcp_sock_set_nodelay(sock->sk);
1287 sock_set_reuseaddr(sock->sk);
1289 add_listen_sock(sock, con);
1291 /* Bind to our port */
1292 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1293 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1295 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1298 sock_set_keepalive(sock->sk);
1300 result = sock->ops->listen(sock, 5);
1302 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1312 /* Get local addresses */
1313 static void init_local(void)
1315 struct sockaddr_storage sas, *addr;
1318 dlm_local_count = 0;
1319 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1320 if (dlm_our_addr(&sas, i))
1323 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
1326 dlm_local_addr[dlm_local_count++] = addr;
1330 static void deinit_local(void)
1334 for (i = 0; i < dlm_local_count; i++)
1335 kfree(dlm_local_addr[i]);
1338 /* Initialise SCTP socket and bind to all interfaces
1339 * On error caller must run dlm_close_sock() for the
1340 * listen connection socket.
1342 static int sctp_listen_for_all(struct listen_connection *con)
1344 struct socket *sock = NULL;
1345 int result = -EINVAL;
1347 log_print("Using SCTP for communications");
1349 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1350 SOCK_STREAM, IPPROTO_SCTP, &sock);
1352 log_print("Can't create comms socket, check SCTP is loaded");
1356 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1357 sock_set_mark(sock->sk, dlm_config.ci_mark);
1358 sctp_sock_set_nodelay(sock->sk);
1360 add_listen_sock(sock, con);
1362 /* Bind to all addresses. */
1363 result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port);
1367 result = sock->ops->listen(sock, 5);
1369 log_print("Can't set socket listening");
1379 static int tcp_listen_for_all(void)
1381 /* We don't support multi-homed hosts */
1382 if (dlm_local_count > 1) {
1383 log_print("TCP protocol can't handle multi-homed hosts, "
1388 log_print("Using TCP for communications");
1390 return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]);
1395 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1398 struct writequeue_entry *entry;
1400 entry = kzalloc(sizeof(*entry), allocation);
1404 entry->page = alloc_page(allocation | __GFP_ZERO);
1416 static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
1417 gfp_t allocation, char **ppc)
1419 struct writequeue_entry *e;
1421 spin_lock(&con->writequeue_lock);
1422 if (!list_empty(&con->writequeue)) {
1423 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1424 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
1425 *ppc = page_address(e->page) + e->end;
1428 spin_unlock(&con->writequeue_lock);
1433 spin_unlock(&con->writequeue_lock);
1435 e = new_writequeue_entry(con, allocation);
1439 *ppc = page_address(e->page);
1441 atomic_inc(&con->writequeue_cnt);
1443 spin_lock(&con->writequeue_lock);
1444 list_add_tail(&e->list, &con->writequeue);
1445 spin_unlock(&con->writequeue_lock);
1450 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1452 struct writequeue_entry *e;
1453 struct connection *con;
1456 if (len > DEFAULT_BUFFER_SIZE ||
1457 len < sizeof(struct dlm_header)) {
1458 BUILD_BUG_ON(PAGE_SIZE < DEFAULT_BUFFER_SIZE);
1459 log_print("failed to allocate a buffer of size %d", len);
1464 idx = srcu_read_lock(&connections_srcu);
1465 con = nodeid2con(nodeid, allocation);
1467 srcu_read_unlock(&connections_srcu, idx);
1471 e = new_wq_entry(con, len, allocation, ppc);
1473 srcu_read_unlock(&connections_srcu, idx);
1477 /* we assume if successful commit must called */
1483 void dlm_lowcomms_commit_buffer(void *mh)
1485 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1486 struct connection *con = e->con;
1489 spin_lock(&con->writequeue_lock);
1494 e->len = DLM_WQ_LENGTH_BYTES(e);
1495 spin_unlock(&con->writequeue_lock);
1497 queue_work(send_workqueue, &con->swork);
1498 srcu_read_unlock(&connections_srcu, e->idx);
1502 spin_unlock(&con->writequeue_lock);
1503 srcu_read_unlock(&connections_srcu, e->idx);
1507 /* Send a message */
1508 static void send_to_sock(struct connection *con)
1511 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1512 struct writequeue_entry *e;
1516 mutex_lock(&con->sock_mutex);
1517 if (con->sock == NULL)
1520 spin_lock(&con->writequeue_lock);
1522 if (list_empty(&con->writequeue))
1525 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
1528 BUG_ON(len == 0 && e->users == 0);
1529 spin_unlock(&con->writequeue_lock);
1533 ret = kernel_sendpage(con->sock, e->page, offset, len,
1535 if (ret == -EAGAIN || ret == 0) {
1536 if (ret == -EAGAIN &&
1537 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1538 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1539 /* Notify TCP that we're limited by the
1540 * application window size.
1542 set_bit(SOCK_NOSPACE, &con->sock->flags);
1543 con->sock->sk->sk_write_pending++;
1551 /* Don't starve people filling buffers */
1552 if (++count >= MAX_SEND_MSG_COUNT) {
1557 spin_lock(&con->writequeue_lock);
1558 writequeue_entry_complete(e, ret);
1560 spin_unlock(&con->writequeue_lock);
1562 /* close if we got EOF */
1563 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1564 mutex_unlock(&con->sock_mutex);
1565 close_connection(con, false, false, true);
1567 /* handling for tcp shutdown */
1568 clear_bit(CF_SHUTDOWN, &con->flags);
1569 wake_up(&con->shutdown_wait);
1571 mutex_unlock(&con->sock_mutex);
1577 mutex_unlock(&con->sock_mutex);
1581 mutex_unlock(&con->sock_mutex);
1582 queue_work(send_workqueue, &con->swork);
1586 static void clean_one_writequeue(struct connection *con)
1588 struct writequeue_entry *e, *safe;
1590 spin_lock(&con->writequeue_lock);
1591 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1595 spin_unlock(&con->writequeue_lock);
1598 /* Called from recovery when it knows that a node has
1600 int dlm_lowcomms_close(int nodeid)
1602 struct connection *con;
1603 struct dlm_node_addr *na;
1606 log_print("closing connection to node %d", nodeid);
1607 idx = srcu_read_lock(&connections_srcu);
1608 con = nodeid2con(nodeid, 0);
1610 set_bit(CF_CLOSE, &con->flags);
1611 close_connection(con, true, true, true);
1612 clean_one_writequeue(con);
1614 clean_one_writequeue(con->othercon);
1616 srcu_read_unlock(&connections_srcu, idx);
1618 spin_lock(&dlm_node_addrs_spin);
1619 na = find_node_addr(nodeid);
1621 list_del(&na->list);
1622 while (na->addr_count--)
1623 kfree(na->addr[na->addr_count]);
1626 spin_unlock(&dlm_node_addrs_spin);
1631 /* Receive workqueue function */
1632 static void process_recv_sockets(struct work_struct *work)
1634 struct connection *con = container_of(work, struct connection, rwork);
1637 clear_bit(CF_READ_PENDING, &con->flags);
1639 err = receive_from_sock(con);
1643 static void process_listen_recv_socket(struct work_struct *work)
1645 accept_from_sock(&listen_con);
1648 /* Send workqueue function */
1649 static void process_send_sockets(struct work_struct *work)
1651 struct connection *con = container_of(work, struct connection, swork);
1653 clear_bit(CF_WRITE_PENDING, &con->flags);
1655 if (test_and_clear_bit(CF_RECONNECT, &con->flags))
1656 close_connection(con, false, false, true);
1658 if (con->sock == NULL) { /* not mutex protected so check it inside too */
1659 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1661 con->connect_action(con);
1663 if (!list_empty(&con->writequeue))
1667 static void work_stop(void)
1669 if (recv_workqueue) {
1670 destroy_workqueue(recv_workqueue);
1671 recv_workqueue = NULL;
1674 if (send_workqueue) {
1675 destroy_workqueue(send_workqueue);
1676 send_workqueue = NULL;
1680 static int work_start(void)
1682 recv_workqueue = alloc_workqueue("dlm_recv",
1683 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1684 if (!recv_workqueue) {
1685 log_print("can't start dlm_recv");
1689 send_workqueue = alloc_workqueue("dlm_send",
1690 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1691 if (!send_workqueue) {
1692 log_print("can't start dlm_send");
1693 destroy_workqueue(recv_workqueue);
1694 recv_workqueue = NULL;
1701 static void shutdown_conn(struct connection *con)
1703 if (con->shutdown_action)
1704 con->shutdown_action(con);
1707 void dlm_lowcomms_shutdown(void)
1711 /* Set all the flags to prevent any
1717 flush_workqueue(recv_workqueue);
1719 flush_workqueue(send_workqueue);
1721 dlm_close_sock(&listen_con.sock);
1723 idx = srcu_read_lock(&connections_srcu);
1724 foreach_conn(shutdown_conn);
1725 srcu_read_unlock(&connections_srcu, idx);
1728 static void _stop_conn(struct connection *con, bool and_other)
1730 mutex_lock(&con->sock_mutex);
1731 set_bit(CF_CLOSE, &con->flags);
1732 set_bit(CF_READ_PENDING, &con->flags);
1733 set_bit(CF_WRITE_PENDING, &con->flags);
1734 if (con->sock && con->sock->sk) {
1735 write_lock_bh(&con->sock->sk->sk_callback_lock);
1736 con->sock->sk->sk_user_data = NULL;
1737 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1739 if (con->othercon && and_other)
1740 _stop_conn(con->othercon, false);
1741 mutex_unlock(&con->sock_mutex);
1744 static void stop_conn(struct connection *con)
1746 _stop_conn(con, true);
1749 static void connection_release(struct rcu_head *rcu)
1751 struct connection *con = container_of(rcu, struct connection, rcu);
1757 static void free_conn(struct connection *con)
1759 close_connection(con, true, true, true);
1760 spin_lock(&connections_lock);
1761 hlist_del_rcu(&con->list);
1762 spin_unlock(&connections_lock);
1763 if (con->othercon) {
1764 clean_one_writequeue(con->othercon);
1765 call_srcu(&connections_srcu, &con->othercon->rcu,
1766 connection_release);
1768 clean_one_writequeue(con);
1769 call_srcu(&connections_srcu, &con->rcu, connection_release);
1772 static void work_flush(void)
1776 struct connection *con;
1780 foreach_conn(stop_conn);
1782 flush_workqueue(recv_workqueue);
1784 flush_workqueue(send_workqueue);
1785 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
1786 hlist_for_each_entry_rcu(con, &connection_hash[i],
1788 ok &= test_bit(CF_READ_PENDING, &con->flags);
1789 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1790 if (con->othercon) {
1791 ok &= test_bit(CF_READ_PENDING,
1792 &con->othercon->flags);
1793 ok &= test_bit(CF_WRITE_PENDING,
1794 &con->othercon->flags);
1801 void dlm_lowcomms_stop(void)
1805 idx = srcu_read_lock(&connections_srcu);
1807 foreach_conn(free_conn);
1808 srcu_read_unlock(&connections_srcu, idx);
1813 int dlm_lowcomms_start(void)
1815 int error = -EINVAL;
1818 for (i = 0; i < CONN_HASH_SIZE; i++)
1819 INIT_HLIST_HEAD(&connection_hash[i]);
1822 if (!dlm_local_count) {
1824 log_print("no local IP address has been set");
1828 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1830 error = work_start();
1836 /* Start listening */
1837 if (dlm_config.ci_protocol == 0)
1838 error = tcp_listen_for_all();
1840 error = sctp_listen_for_all(&listen_con);
1848 dlm_close_sock(&listen_con.sock);
1856 void dlm_lowcomms_exit(void)
1858 struct dlm_node_addr *na, *safe;
1860 spin_lock(&dlm_node_addrs_spin);
1861 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1862 list_del(&na->list);
1863 while (na->addr_count--)
1864 kfree(na->addr[na->addr_count]);
1867 spin_unlock(&dlm_node_addrs_spin);