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 struct list_head writequeue; /* List of outgoing writequeue_entries */
83 spinlock_t writequeue_lock;
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 struct listen_connection {
102 struct work_struct rwork;
105 /* An entry waiting to be sent */
106 struct writequeue_entry {
107 struct list_head list;
113 struct connection *con;
116 struct dlm_node_addr {
117 struct list_head list;
122 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
125 static struct listen_sock_callbacks {
126 void (*sk_error_report)(struct sock *);
127 void (*sk_data_ready)(struct sock *);
128 void (*sk_state_change)(struct sock *);
129 void (*sk_write_space)(struct sock *);
132 static LIST_HEAD(dlm_node_addrs);
133 static DEFINE_SPINLOCK(dlm_node_addrs_spin);
135 static struct listen_connection listen_con;
136 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
137 static int dlm_local_count;
138 static int dlm_allow_conn;
141 static struct workqueue_struct *recv_workqueue;
142 static struct workqueue_struct *send_workqueue;
144 static struct hlist_head connection_hash[CONN_HASH_SIZE];
145 static DEFINE_SPINLOCK(connections_lock);
146 DEFINE_STATIC_SRCU(connections_srcu);
148 static void process_recv_sockets(struct work_struct *work);
149 static void process_send_sockets(struct work_struct *work);
151 static void sctp_connect_to_sock(struct connection *con);
152 static void tcp_connect_to_sock(struct connection *con);
153 static void dlm_tcp_shutdown(struct connection *con);
155 /* This is deliberately very simple because most clusters have simple
156 sequential nodeids, so we should be able to go straight to a connection
157 struct in the array */
158 static inline int nodeid_hash(int nodeid)
160 return nodeid & (CONN_HASH_SIZE-1);
163 static struct connection *__find_con(int nodeid)
166 struct connection *con;
168 r = nodeid_hash(nodeid);
170 idx = srcu_read_lock(&connections_srcu);
171 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
172 if (con->nodeid == nodeid) {
173 srcu_read_unlock(&connections_srcu, idx);
177 srcu_read_unlock(&connections_srcu, idx);
182 static int dlm_con_init(struct connection *con, int nodeid)
184 con->rx_buflen = dlm_config.ci_buffer_size;
185 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
189 con->nodeid = nodeid;
190 mutex_init(&con->sock_mutex);
191 INIT_LIST_HEAD(&con->writequeue);
192 spin_lock_init(&con->writequeue_lock);
193 INIT_WORK(&con->swork, process_send_sockets);
194 INIT_WORK(&con->rwork, process_recv_sockets);
195 init_waitqueue_head(&con->shutdown_wait);
197 if (dlm_config.ci_protocol == 0) {
198 con->connect_action = tcp_connect_to_sock;
199 con->shutdown_action = dlm_tcp_shutdown;
201 con->connect_action = sctp_connect_to_sock;
208 * If 'allocation' is zero then we don't attempt to create a new
209 * connection structure for this node.
211 static struct connection *nodeid2con(int nodeid, gfp_t alloc)
213 struct connection *con, *tmp;
216 con = __find_con(nodeid);
220 con = kzalloc(sizeof(*con), alloc);
224 ret = dlm_con_init(con, nodeid);
230 r = nodeid_hash(nodeid);
232 spin_lock(&connections_lock);
233 /* Because multiple workqueues/threads calls this function it can
234 * race on multiple cpu's. Instead of locking hot path __find_con()
235 * we just check in rare cases of recently added nodes again
236 * under protection of connections_lock. If this is the case we
237 * abort our connection creation and return the existing connection.
239 tmp = __find_con(nodeid);
241 spin_unlock(&connections_lock);
247 hlist_add_head_rcu(&con->list, &connection_hash[r]);
248 spin_unlock(&connections_lock);
253 /* Loop round all connections */
254 static void foreach_conn(void (*conn_func)(struct connection *c))
257 struct connection *con;
259 idx = srcu_read_lock(&connections_srcu);
260 for (i = 0; i < CONN_HASH_SIZE; i++) {
261 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
264 srcu_read_unlock(&connections_srcu, idx);
267 static struct dlm_node_addr *find_node_addr(int nodeid)
269 struct dlm_node_addr *na;
271 list_for_each_entry(na, &dlm_node_addrs, list) {
272 if (na->nodeid == nodeid)
278 static int addr_compare(const struct sockaddr_storage *x,
279 const struct sockaddr_storage *y)
281 switch (x->ss_family) {
283 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
284 struct sockaddr_in *siny = (struct sockaddr_in *)y;
285 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
287 if (sinx->sin_port != siny->sin_port)
292 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
293 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
294 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
296 if (sinx->sin6_port != siny->sin6_port)
306 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
307 struct sockaddr *sa_out, bool try_new_addr,
310 struct sockaddr_storage sas;
311 struct dlm_node_addr *na;
313 if (!dlm_local_count)
316 spin_lock(&dlm_node_addrs_spin);
317 na = find_node_addr(nodeid);
318 if (na && na->addr_count) {
319 memcpy(&sas, na->addr[na->curr_addr_index],
320 sizeof(struct sockaddr_storage));
323 na->curr_addr_index++;
324 if (na->curr_addr_index == na->addr_count)
325 na->curr_addr_index = 0;
328 spin_unlock(&dlm_node_addrs_spin);
339 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
344 if (dlm_local_addr[0]->ss_family == AF_INET) {
345 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
346 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
347 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
349 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
350 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
351 ret6->sin6_addr = in6->sin6_addr;
357 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
360 struct dlm_node_addr *na;
364 spin_lock(&dlm_node_addrs_spin);
365 list_for_each_entry(na, &dlm_node_addrs, list) {
369 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
370 if (addr_compare(na->addr[addr_i], addr)) {
371 *nodeid = na->nodeid;
379 spin_unlock(&dlm_node_addrs_spin);
383 /* caller need to held dlm_node_addrs_spin lock */
384 static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
385 const struct sockaddr_storage *addr)
389 for (i = 0; i < na->addr_count; i++) {
390 if (addr_compare(na->addr[i], addr))
397 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
399 struct sockaddr_storage *new_addr;
400 struct dlm_node_addr *new_node, *na;
403 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
407 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
413 memcpy(new_addr, addr, len);
415 spin_lock(&dlm_node_addrs_spin);
416 na = find_node_addr(nodeid);
418 new_node->nodeid = nodeid;
419 new_node->addr[0] = new_addr;
420 new_node->addr_count = 1;
421 new_node->mark = dlm_config.ci_mark;
422 list_add(&new_node->list, &dlm_node_addrs);
423 spin_unlock(&dlm_node_addrs_spin);
427 ret = dlm_lowcomms_na_has_addr(na, addr);
429 spin_unlock(&dlm_node_addrs_spin);
435 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
436 spin_unlock(&dlm_node_addrs_spin);
442 na->addr[na->addr_count++] = new_addr;
443 spin_unlock(&dlm_node_addrs_spin);
448 /* Data available on socket or listen socket received a connect */
449 static void lowcomms_data_ready(struct sock *sk)
451 struct connection *con;
453 read_lock_bh(&sk->sk_callback_lock);
455 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
456 queue_work(recv_workqueue, &con->rwork);
457 read_unlock_bh(&sk->sk_callback_lock);
460 static void lowcomms_listen_data_ready(struct sock *sk)
462 queue_work(recv_workqueue, &listen_con.rwork);
465 static void lowcomms_write_space(struct sock *sk)
467 struct connection *con;
469 read_lock_bh(&sk->sk_callback_lock);
474 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
475 log_print("successful connected to node %d", con->nodeid);
476 queue_work(send_workqueue, &con->swork);
480 clear_bit(SOCK_NOSPACE, &con->sock->flags);
482 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
483 con->sock->sk->sk_write_pending--;
484 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
487 queue_work(send_workqueue, &con->swork);
489 read_unlock_bh(&sk->sk_callback_lock);
492 static inline void lowcomms_connect_sock(struct connection *con)
494 if (test_bit(CF_CLOSE, &con->flags))
496 queue_work(send_workqueue, &con->swork);
500 static void lowcomms_state_change(struct sock *sk)
502 /* SCTP layer is not calling sk_data_ready when the connection
503 * is done, so we catch the signal through here. Also, it
504 * doesn't switch socket state when entering shutdown, so we
505 * skip the write in that case.
507 if (sk->sk_shutdown) {
508 if (sk->sk_shutdown == RCV_SHUTDOWN)
509 lowcomms_data_ready(sk);
510 } else if (sk->sk_state == TCP_ESTABLISHED) {
511 lowcomms_write_space(sk);
515 int dlm_lowcomms_connect_node(int nodeid)
517 struct connection *con;
519 if (nodeid == dlm_our_nodeid())
522 con = nodeid2con(nodeid, GFP_NOFS);
525 lowcomms_connect_sock(con);
529 int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
531 struct dlm_node_addr *na;
533 spin_lock(&dlm_node_addrs_spin);
534 na = find_node_addr(nodeid);
536 spin_unlock(&dlm_node_addrs_spin);
541 spin_unlock(&dlm_node_addrs_spin);
546 static void lowcomms_error_report(struct sock *sk)
548 struct connection *con;
549 struct sockaddr_storage saddr;
550 void (*orig_report)(struct sock *) = NULL;
552 read_lock_bh(&sk->sk_callback_lock);
557 orig_report = listen_sock.sk_error_report;
558 if (con->sock == NULL ||
559 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
560 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
561 "sending to node %d, port %d, "
562 "sk_err=%d/%d\n", dlm_our_nodeid(),
563 con->nodeid, dlm_config.ci_tcp_port,
564 sk->sk_err, sk->sk_err_soft);
565 } else if (saddr.ss_family == AF_INET) {
566 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
568 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
569 "sending to node %d at %pI4, port %d, "
570 "sk_err=%d/%d\n", dlm_our_nodeid(),
571 con->nodeid, &sin4->sin_addr.s_addr,
572 dlm_config.ci_tcp_port, sk->sk_err,
575 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
577 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
578 "sending to node %d at %u.%u.%u.%u, "
579 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
580 con->nodeid, sin6->sin6_addr.s6_addr32[0],
581 sin6->sin6_addr.s6_addr32[1],
582 sin6->sin6_addr.s6_addr32[2],
583 sin6->sin6_addr.s6_addr32[3],
584 dlm_config.ci_tcp_port, sk->sk_err,
588 read_unlock_bh(&sk->sk_callback_lock);
593 /* Note: sk_callback_lock must be locked before calling this function. */
594 static void save_listen_callbacks(struct socket *sock)
596 struct sock *sk = sock->sk;
598 listen_sock.sk_data_ready = sk->sk_data_ready;
599 listen_sock.sk_state_change = sk->sk_state_change;
600 listen_sock.sk_write_space = sk->sk_write_space;
601 listen_sock.sk_error_report = sk->sk_error_report;
604 static void restore_callbacks(struct socket *sock)
606 struct sock *sk = sock->sk;
608 write_lock_bh(&sk->sk_callback_lock);
609 sk->sk_user_data = NULL;
610 sk->sk_data_ready = listen_sock.sk_data_ready;
611 sk->sk_state_change = listen_sock.sk_state_change;
612 sk->sk_write_space = listen_sock.sk_write_space;
613 sk->sk_error_report = listen_sock.sk_error_report;
614 write_unlock_bh(&sk->sk_callback_lock);
617 static void add_listen_sock(struct socket *sock, struct listen_connection *con)
619 struct sock *sk = sock->sk;
621 write_lock_bh(&sk->sk_callback_lock);
622 save_listen_callbacks(sock);
625 sk->sk_user_data = con;
626 sk->sk_allocation = GFP_NOFS;
627 /* Install a data_ready callback */
628 sk->sk_data_ready = lowcomms_listen_data_ready;
629 write_unlock_bh(&sk->sk_callback_lock);
632 /* Make a socket active */
633 static void add_sock(struct socket *sock, struct connection *con)
635 struct sock *sk = sock->sk;
637 write_lock_bh(&sk->sk_callback_lock);
640 sk->sk_user_data = con;
641 /* Install a data_ready callback */
642 sk->sk_data_ready = lowcomms_data_ready;
643 sk->sk_write_space = lowcomms_write_space;
644 sk->sk_state_change = lowcomms_state_change;
645 sk->sk_allocation = GFP_NOFS;
646 sk->sk_error_report = lowcomms_error_report;
647 write_unlock_bh(&sk->sk_callback_lock);
650 /* Add the port number to an IPv6 or 4 sockaddr and return the address
652 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
655 saddr->ss_family = dlm_local_addr[0]->ss_family;
656 if (saddr->ss_family == AF_INET) {
657 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
658 in4_addr->sin_port = cpu_to_be16(port);
659 *addr_len = sizeof(struct sockaddr_in);
660 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
662 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
663 in6_addr->sin6_port = cpu_to_be16(port);
664 *addr_len = sizeof(struct sockaddr_in6);
666 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
669 static void dlm_close_sock(struct socket **sock)
672 restore_callbacks(*sock);
678 /* Close a remote connection and tidy up */
679 static void close_connection(struct connection *con, bool and_other,
682 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
684 if (tx && !closing && cancel_work_sync(&con->swork)) {
685 log_print("canceled swork for node %d", con->nodeid);
686 clear_bit(CF_WRITE_PENDING, &con->flags);
688 if (rx && !closing && cancel_work_sync(&con->rwork)) {
689 log_print("canceled rwork for node %d", con->nodeid);
690 clear_bit(CF_READ_PENDING, &con->flags);
693 mutex_lock(&con->sock_mutex);
694 dlm_close_sock(&con->sock);
696 if (con->othercon && and_other) {
697 /* Will only re-enter once. */
698 close_connection(con->othercon, false, true, true);
701 con->rx_leftover = 0;
703 clear_bit(CF_CONNECTED, &con->flags);
704 mutex_unlock(&con->sock_mutex);
705 clear_bit(CF_CLOSING, &con->flags);
708 static void shutdown_connection(struct connection *con)
712 if (cancel_work_sync(&con->swork)) {
713 log_print("canceled swork for node %d", con->nodeid);
714 clear_bit(CF_WRITE_PENDING, &con->flags);
717 mutex_lock(&con->sock_mutex);
718 /* nothing to shutdown */
720 mutex_unlock(&con->sock_mutex);
724 set_bit(CF_SHUTDOWN, &con->flags);
725 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
726 mutex_unlock(&con->sock_mutex);
728 log_print("Connection %p failed to shutdown: %d will force close",
732 ret = wait_event_timeout(con->shutdown_wait,
733 !test_bit(CF_SHUTDOWN, &con->flags),
734 DLM_SHUTDOWN_WAIT_TIMEOUT);
736 log_print("Connection %p shutdown timed out, will force close",
745 clear_bit(CF_SHUTDOWN, &con->flags);
746 close_connection(con, false, true, true);
749 static void dlm_tcp_shutdown(struct connection *con)
752 shutdown_connection(con->othercon);
753 shutdown_connection(con);
756 static int con_realloc_receive_buf(struct connection *con, int newlen)
758 unsigned char *newbuf;
760 newbuf = kmalloc(newlen, GFP_NOFS);
764 /* copy any leftover from last receive */
765 if (con->rx_leftover)
766 memmove(newbuf, con->rx_buf, con->rx_leftover);
768 /* swap to new buffer space */
770 con->rx_buflen = newlen;
771 con->rx_buf = newbuf;
776 /* Data received from remote end */
777 static int receive_from_sock(struct connection *con)
779 int call_again_soon = 0;
784 mutex_lock(&con->sock_mutex);
786 if (con->sock == NULL) {
791 /* realloc if we get new buffer size to read out */
792 buflen = dlm_config.ci_buffer_size;
793 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
794 ret = con_realloc_receive_buf(con, buflen);
799 /* calculate new buffer parameter regarding last receive and
800 * possible leftover bytes
802 iov.iov_base = con->rx_buf + con->rx_leftover;
803 iov.iov_len = con->rx_buflen - con->rx_leftover;
805 memset(&msg, 0, sizeof(msg));
806 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
807 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
811 else if (ret == iov.iov_len)
814 /* new buflen according readed bytes and leftover from last receive */
815 buflen = ret + con->rx_leftover;
816 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
820 /* calculate leftover bytes from process and put it into begin of
821 * the receive buffer, so next receive we have the full message
822 * at the start address of the receive buffer.
824 con->rx_leftover = buflen - ret;
825 if (con->rx_leftover) {
826 memmove(con->rx_buf, con->rx_buf + ret,
828 call_again_soon = true;
834 mutex_unlock(&con->sock_mutex);
838 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
839 queue_work(recv_workqueue, &con->rwork);
840 mutex_unlock(&con->sock_mutex);
844 mutex_unlock(&con->sock_mutex);
845 if (ret != -EAGAIN) {
846 /* Reconnect when there is something to send */
847 close_connection(con, false, true, false);
849 log_print("connection %p got EOF from %d",
851 /* handling for tcp shutdown */
852 clear_bit(CF_SHUTDOWN, &con->flags);
853 wake_up(&con->shutdown_wait);
854 /* signal to breaking receive worker */
861 /* Listening socket is busy, accept a connection */
862 static int accept_from_sock(struct listen_connection *con)
865 struct sockaddr_storage peeraddr;
866 struct socket *newsock;
869 struct connection *newcon;
870 struct connection *addcon;
873 if (!dlm_allow_conn) {
880 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
884 /* Get the connected socket's peer */
885 memset(&peeraddr, 0, sizeof(peeraddr));
886 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
888 result = -ECONNABORTED;
892 /* Get the new node's NODEID */
893 make_sockaddr(&peeraddr, 0, &len);
894 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
895 unsigned char *b=(unsigned char *)&peeraddr;
896 log_print("connect from non cluster node");
897 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
898 b, sizeof(struct sockaddr_storage));
899 sock_release(newsock);
903 log_print("got connection from %d", nodeid);
905 /* Check to see if we already have a connection to this node. This
906 * could happen if the two nodes initiate a connection at roughly
907 * the same time and the connections cross on the wire.
908 * In this case we store the incoming one in "othercon"
910 newcon = nodeid2con(nodeid, GFP_NOFS);
916 sock_set_mark(newsock->sk, mark);
918 mutex_lock(&newcon->sock_mutex);
920 struct connection *othercon = newcon->othercon;
923 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
925 log_print("failed to allocate incoming socket");
926 mutex_unlock(&newcon->sock_mutex);
931 result = dlm_con_init(othercon, nodeid);
937 lockdep_set_subclass(&othercon->sock_mutex, 1);
938 newcon->othercon = othercon;
940 /* close other sock con if we have something new */
941 close_connection(othercon, false, true, false);
944 mutex_lock(&othercon->sock_mutex);
945 add_sock(newsock, othercon);
947 mutex_unlock(&othercon->sock_mutex);
950 /* accept copies the sk after we've saved the callbacks, so we
951 don't want to save them a second time or comm errors will
952 result in calling sk_error_report recursively. */
953 add_sock(newsock, newcon);
957 set_bit(CF_CONNECTED, &addcon->flags);
958 mutex_unlock(&newcon->sock_mutex);
961 * Add it to the active queue in case we got data
962 * between processing the accept adding the socket
963 * to the read_sockets list
965 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
966 queue_work(recv_workqueue, &addcon->rwork);
972 sock_release(newsock);
974 if (result != -EAGAIN)
975 log_print("error accepting connection from node: %d", result);
979 static void free_entry(struct writequeue_entry *e)
981 __free_page(e->page);
986 * writequeue_entry_complete - try to delete and free write queue entry
987 * @e: write queue entry to try to delete
988 * @completed: bytes completed
990 * writequeue_lock must be held.
992 static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
994 e->offset += completed;
997 if (e->len == 0 && e->users == 0) {
1004 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1006 static int sctp_bind_addrs(struct socket *sock, uint16_t port)
1008 struct sockaddr_storage localaddr;
1009 struct sockaddr *addr = (struct sockaddr *)&localaddr;
1010 int i, addr_len, result = 0;
1012 for (i = 0; i < dlm_local_count; i++) {
1013 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1014 make_sockaddr(&localaddr, port, &addr_len);
1017 result = kernel_bind(sock, addr, addr_len);
1019 result = sock_bind_add(sock->sk, addr, addr_len);
1022 log_print("Can't bind to %d addr number %d, %d.\n",
1023 port, i + 1, result);
1030 /* Initiate an SCTP association.
1031 This is a special case of send_to_sock() in that we don't yet have a
1032 peeled-off socket for this association, so we use the listening socket
1033 and add the primary IP address of the remote node.
1035 static void sctp_connect_to_sock(struct connection *con)
1037 struct sockaddr_storage daddr;
1040 struct socket *sock;
1043 mutex_lock(&con->sock_mutex);
1045 /* Some odd races can cause double-connects, ignore them */
1046 if (con->retries++ > MAX_CONNECT_RETRIES)
1050 log_print("node %d already connected.", con->nodeid);
1054 memset(&daddr, 0, sizeof(daddr));
1055 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true, &mark);
1057 log_print("no address for nodeid %d", con->nodeid);
1061 /* Create a socket to communicate with */
1062 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1063 SOCK_STREAM, IPPROTO_SCTP, &sock);
1067 sock_set_mark(sock->sk, mark);
1069 add_sock(sock, con);
1071 /* Bind to all addresses. */
1072 if (sctp_bind_addrs(con->sock, 0))
1075 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
1077 log_print("connecting to %d", con->nodeid);
1079 /* Turn off Nagle's algorithm */
1080 sctp_sock_set_nodelay(sock->sk);
1083 * Make sock->ops->connect() function return in specified time,
1084 * since O_NONBLOCK argument in connect() function does not work here,
1085 * then, we should restore the default value of this attribute.
1087 sock_set_sndtimeo(sock->sk, 5);
1088 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
1090 sock_set_sndtimeo(sock->sk, 0);
1092 if (result == -EINPROGRESS)
1095 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1096 log_print("successful connected to node %d", con->nodeid);
1106 * Some errors are fatal and this list might need adjusting. For other
1107 * errors we try again until the max number of retries is reached.
1109 if (result != -EHOSTUNREACH &&
1110 result != -ENETUNREACH &&
1111 result != -ENETDOWN &&
1112 result != -EINVAL &&
1113 result != -EPROTONOSUPPORT) {
1114 log_print("connect %d try %d error %d", con->nodeid,
1115 con->retries, result);
1116 mutex_unlock(&con->sock_mutex);
1118 lowcomms_connect_sock(con);
1123 mutex_unlock(&con->sock_mutex);
1126 /* Connect a new socket to its peer */
1127 static void tcp_connect_to_sock(struct connection *con)
1129 struct sockaddr_storage saddr, src_addr;
1132 struct socket *sock = NULL;
1135 mutex_lock(&con->sock_mutex);
1136 if (con->retries++ > MAX_CONNECT_RETRIES)
1139 /* Some odd races can cause double-connects, ignore them */
1143 /* Create a socket to communicate with */
1144 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1145 SOCK_STREAM, IPPROTO_TCP, &sock);
1149 memset(&saddr, 0, sizeof(saddr));
1150 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false, &mark);
1152 log_print("no address for nodeid %d", con->nodeid);
1156 sock_set_mark(sock->sk, mark);
1158 add_sock(sock, con);
1160 /* Bind to our cluster-known address connecting to avoid
1162 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1163 make_sockaddr(&src_addr, 0, &addr_len);
1164 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1167 log_print("could not bind for connect: %d", result);
1168 /* This *may* not indicate a critical error */
1171 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1173 log_print("connecting to %d", con->nodeid);
1175 /* Turn off Nagle's algorithm */
1176 tcp_sock_set_nodelay(sock->sk);
1178 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1180 if (result == -EINPROGRESS)
1187 sock_release(con->sock);
1193 * Some errors are fatal and this list might need adjusting. For other
1194 * errors we try again until the max number of retries is reached.
1196 if (result != -EHOSTUNREACH &&
1197 result != -ENETUNREACH &&
1198 result != -ENETDOWN &&
1199 result != -EINVAL &&
1200 result != -EPROTONOSUPPORT) {
1201 log_print("connect %d try %d error %d", con->nodeid,
1202 con->retries, result);
1203 mutex_unlock(&con->sock_mutex);
1205 lowcomms_connect_sock(con);
1209 mutex_unlock(&con->sock_mutex);
1213 /* On error caller must run dlm_close_sock() for the
1214 * listen connection socket.
1216 static int tcp_create_listen_sock(struct listen_connection *con,
1217 struct sockaddr_storage *saddr)
1219 struct socket *sock = NULL;
1223 if (dlm_local_addr[0]->ss_family == AF_INET)
1224 addr_len = sizeof(struct sockaddr_in);
1226 addr_len = sizeof(struct sockaddr_in6);
1228 /* Create a socket to communicate with */
1229 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1230 SOCK_STREAM, IPPROTO_TCP, &sock);
1232 log_print("Can't create listening comms socket");
1236 sock_set_mark(sock->sk, dlm_config.ci_mark);
1238 /* Turn off Nagle's algorithm */
1239 tcp_sock_set_nodelay(sock->sk);
1241 sock_set_reuseaddr(sock->sk);
1243 add_listen_sock(sock, con);
1245 /* Bind to our port */
1246 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1247 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1249 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1252 sock_set_keepalive(sock->sk);
1254 result = sock->ops->listen(sock, 5);
1256 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1266 /* Get local addresses */
1267 static void init_local(void)
1269 struct sockaddr_storage sas, *addr;
1272 dlm_local_count = 0;
1273 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1274 if (dlm_our_addr(&sas, i))
1277 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
1280 dlm_local_addr[dlm_local_count++] = addr;
1284 static void deinit_local(void)
1288 for (i = 0; i < dlm_local_count; i++)
1289 kfree(dlm_local_addr[i]);
1292 /* Initialise SCTP socket and bind to all interfaces
1293 * On error caller must run dlm_close_sock() for the
1294 * listen connection socket.
1296 static int sctp_listen_for_all(struct listen_connection *con)
1298 struct socket *sock = NULL;
1299 int result = -EINVAL;
1301 log_print("Using SCTP for communications");
1303 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1304 SOCK_STREAM, IPPROTO_SCTP, &sock);
1306 log_print("Can't create comms socket, check SCTP is loaded");
1310 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1311 sock_set_mark(sock->sk, dlm_config.ci_mark);
1312 sctp_sock_set_nodelay(sock->sk);
1314 add_listen_sock(sock, con);
1316 /* Bind to all addresses. */
1317 result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port);
1321 result = sock->ops->listen(sock, 5);
1323 log_print("Can't set socket listening");
1333 static int tcp_listen_for_all(void)
1335 /* We don't support multi-homed hosts */
1336 if (dlm_local_count > 1) {
1337 log_print("TCP protocol can't handle multi-homed hosts, "
1342 log_print("Using TCP for communications");
1344 return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]);
1349 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1352 struct writequeue_entry *entry;
1354 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1358 entry->page = alloc_page(allocation);
1373 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1375 struct connection *con;
1376 struct writequeue_entry *e;
1379 if (len > LOWCOMMS_MAX_TX_BUFFER_LEN) {
1380 BUILD_BUG_ON(PAGE_SIZE < LOWCOMMS_MAX_TX_BUFFER_LEN);
1381 log_print("failed to allocate a buffer of size %d", len);
1385 con = nodeid2con(nodeid, allocation);
1389 spin_lock(&con->writequeue_lock);
1390 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1391 if ((&e->list == &con->writequeue) ||
1392 (PAGE_SIZE - e->end < len)) {
1399 spin_unlock(&con->writequeue_lock);
1403 *ppc = page_address(e->page) + offset;
1407 e = new_writequeue_entry(con, allocation);
1409 spin_lock(&con->writequeue_lock);
1413 list_add_tail(&e->list, &con->writequeue);
1414 spin_unlock(&con->writequeue_lock);
1420 void dlm_lowcomms_commit_buffer(void *mh)
1422 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1423 struct connection *con = e->con;
1426 spin_lock(&con->writequeue_lock);
1430 e->len = e->end - e->offset;
1431 spin_unlock(&con->writequeue_lock);
1433 queue_work(send_workqueue, &con->swork);
1437 spin_unlock(&con->writequeue_lock);
1441 /* Send a message */
1442 static void send_to_sock(struct connection *con)
1445 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1446 struct writequeue_entry *e;
1450 mutex_lock(&con->sock_mutex);
1451 if (con->sock == NULL)
1454 spin_lock(&con->writequeue_lock);
1456 e = list_entry(con->writequeue.next, struct writequeue_entry,
1458 if ((struct list_head *) e == &con->writequeue)
1463 BUG_ON(len == 0 && e->users == 0);
1464 spin_unlock(&con->writequeue_lock);
1468 ret = kernel_sendpage(con->sock, e->page, offset, len,
1470 if (ret == -EAGAIN || ret == 0) {
1471 if (ret == -EAGAIN &&
1472 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1473 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1474 /* Notify TCP that we're limited by the
1475 * application window size.
1477 set_bit(SOCK_NOSPACE, &con->sock->flags);
1478 con->sock->sk->sk_write_pending++;
1486 /* Don't starve people filling buffers */
1487 if (++count >= MAX_SEND_MSG_COUNT) {
1492 spin_lock(&con->writequeue_lock);
1493 writequeue_entry_complete(e, ret);
1495 spin_unlock(&con->writequeue_lock);
1497 mutex_unlock(&con->sock_mutex);
1501 mutex_unlock(&con->sock_mutex);
1502 close_connection(con, false, false, true);
1503 /* Requeue the send work. When the work daemon runs again, it will try
1504 a new connection, then call this function again. */
1505 queue_work(send_workqueue, &con->swork);
1509 mutex_unlock(&con->sock_mutex);
1510 queue_work(send_workqueue, &con->swork);
1514 static void clean_one_writequeue(struct connection *con)
1516 struct writequeue_entry *e, *safe;
1518 spin_lock(&con->writequeue_lock);
1519 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1523 spin_unlock(&con->writequeue_lock);
1526 /* Called from recovery when it knows that a node has
1528 int dlm_lowcomms_close(int nodeid)
1530 struct connection *con;
1531 struct dlm_node_addr *na;
1533 log_print("closing connection to node %d", nodeid);
1534 con = nodeid2con(nodeid, 0);
1536 set_bit(CF_CLOSE, &con->flags);
1537 close_connection(con, true, true, true);
1538 clean_one_writequeue(con);
1540 clean_one_writequeue(con->othercon);
1543 spin_lock(&dlm_node_addrs_spin);
1544 na = find_node_addr(nodeid);
1546 list_del(&na->list);
1547 while (na->addr_count--)
1548 kfree(na->addr[na->addr_count]);
1551 spin_unlock(&dlm_node_addrs_spin);
1556 /* Receive workqueue function */
1557 static void process_recv_sockets(struct work_struct *work)
1559 struct connection *con = container_of(work, struct connection, rwork);
1562 clear_bit(CF_READ_PENDING, &con->flags);
1564 err = receive_from_sock(con);
1568 static void process_listen_recv_socket(struct work_struct *work)
1570 accept_from_sock(&listen_con);
1573 /* Send workqueue function */
1574 static void process_send_sockets(struct work_struct *work)
1576 struct connection *con = container_of(work, struct connection, swork);
1578 clear_bit(CF_WRITE_PENDING, &con->flags);
1579 if (con->sock == NULL) /* not mutex protected so check it inside too */
1580 con->connect_action(con);
1581 if (!list_empty(&con->writequeue))
1585 static void work_stop(void)
1588 destroy_workqueue(recv_workqueue);
1590 destroy_workqueue(send_workqueue);
1593 static int work_start(void)
1595 recv_workqueue = alloc_workqueue("dlm_recv",
1596 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1597 if (!recv_workqueue) {
1598 log_print("can't start dlm_recv");
1602 send_workqueue = alloc_workqueue("dlm_send",
1603 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1604 if (!send_workqueue) {
1605 log_print("can't start dlm_send");
1606 destroy_workqueue(recv_workqueue);
1613 static void _stop_conn(struct connection *con, bool and_other)
1615 mutex_lock(&con->sock_mutex);
1616 set_bit(CF_CLOSE, &con->flags);
1617 set_bit(CF_READ_PENDING, &con->flags);
1618 set_bit(CF_WRITE_PENDING, &con->flags);
1619 if (con->sock && con->sock->sk) {
1620 write_lock_bh(&con->sock->sk->sk_callback_lock);
1621 con->sock->sk->sk_user_data = NULL;
1622 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1624 if (con->othercon && and_other)
1625 _stop_conn(con->othercon, false);
1626 mutex_unlock(&con->sock_mutex);
1629 static void stop_conn(struct connection *con)
1631 _stop_conn(con, true);
1634 static void shutdown_conn(struct connection *con)
1636 if (con->shutdown_action)
1637 con->shutdown_action(con);
1640 static void connection_release(struct rcu_head *rcu)
1642 struct connection *con = container_of(rcu, struct connection, rcu);
1648 static void free_conn(struct connection *con)
1650 close_connection(con, true, true, true);
1651 spin_lock(&connections_lock);
1652 hlist_del_rcu(&con->list);
1653 spin_unlock(&connections_lock);
1654 if (con->othercon) {
1655 clean_one_writequeue(con->othercon);
1656 call_srcu(&connections_srcu, &con->othercon->rcu,
1657 connection_release);
1659 clean_one_writequeue(con);
1660 call_srcu(&connections_srcu, &con->rcu, connection_release);
1663 static void work_flush(void)
1667 struct connection *con;
1671 foreach_conn(stop_conn);
1673 flush_workqueue(recv_workqueue);
1675 flush_workqueue(send_workqueue);
1676 idx = srcu_read_lock(&connections_srcu);
1677 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
1678 hlist_for_each_entry_rcu(con, &connection_hash[i],
1680 ok &= test_bit(CF_READ_PENDING, &con->flags);
1681 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1682 if (con->othercon) {
1683 ok &= test_bit(CF_READ_PENDING,
1684 &con->othercon->flags);
1685 ok &= test_bit(CF_WRITE_PENDING,
1686 &con->othercon->flags);
1690 srcu_read_unlock(&connections_srcu, idx);
1694 void dlm_lowcomms_stop(void)
1696 /* Set all the flags to prevent any
1702 flush_workqueue(recv_workqueue);
1704 flush_workqueue(send_workqueue);
1706 dlm_close_sock(&listen_con.sock);
1708 foreach_conn(shutdown_conn);
1710 foreach_conn(free_conn);
1715 int dlm_lowcomms_start(void)
1717 int error = -EINVAL;
1720 for (i = 0; i < CONN_HASH_SIZE; i++)
1721 INIT_HLIST_HEAD(&connection_hash[i]);
1724 if (!dlm_local_count) {
1726 log_print("no local IP address has been set");
1730 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1732 error = work_start();
1738 /* Start listening */
1739 if (dlm_config.ci_protocol == 0)
1740 error = tcp_listen_for_all();
1742 error = sctp_listen_for_all(&listen_con);
1750 dlm_close_sock(&listen_con.sock);
1755 void dlm_lowcomms_exit(void)
1757 struct dlm_node_addr *na, *safe;
1759 spin_lock(&dlm_node_addrs_spin);
1760 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1761 list_del(&na->list);
1762 while (na->addr_count--)
1763 kfree(na->addr[na->addr_count]);
1766 spin_unlock(&dlm_node_addrs_spin);