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, *tmp;
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 /* Because multiple workqueues/threads calls this function it can
217 * race on multiple cpu's. Instead of locking hot path __find_con()
218 * we just check in rare cases of recently added nodes again
219 * under protection of connections_lock. If this is the case we
220 * abort our connection creation and return the existing connection.
222 tmp = __find_con(nodeid);
224 spin_unlock(&connections_lock);
230 hlist_add_head_rcu(&con->list, &connection_hash[r]);
231 spin_unlock(&connections_lock);
236 /* Loop round all connections */
237 static void foreach_conn(void (*conn_func)(struct connection *c))
240 struct connection *con;
242 idx = srcu_read_lock(&connections_srcu);
243 for (i = 0; i < CONN_HASH_SIZE; i++) {
244 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
247 srcu_read_unlock(&connections_srcu, idx);
250 static struct dlm_node_addr *find_node_addr(int nodeid)
252 struct dlm_node_addr *na;
254 list_for_each_entry(na, &dlm_node_addrs, list) {
255 if (na->nodeid == nodeid)
261 static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
263 switch (x->ss_family) {
265 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
266 struct sockaddr_in *siny = (struct sockaddr_in *)y;
267 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
269 if (sinx->sin_port != siny->sin_port)
274 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
275 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
276 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
278 if (sinx->sin6_port != siny->sin6_port)
288 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
289 struct sockaddr *sa_out, bool try_new_addr)
291 struct sockaddr_storage sas;
292 struct dlm_node_addr *na;
294 if (!dlm_local_count)
297 spin_lock(&dlm_node_addrs_spin);
298 na = find_node_addr(nodeid);
299 if (na && na->addr_count) {
300 memcpy(&sas, na->addr[na->curr_addr_index],
301 sizeof(struct sockaddr_storage));
304 na->curr_addr_index++;
305 if (na->curr_addr_index == na->addr_count)
306 na->curr_addr_index = 0;
309 spin_unlock(&dlm_node_addrs_spin);
318 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
323 if (dlm_local_addr[0]->ss_family == AF_INET) {
324 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
325 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
326 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
328 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
329 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
330 ret6->sin6_addr = in6->sin6_addr;
336 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
338 struct dlm_node_addr *na;
342 spin_lock(&dlm_node_addrs_spin);
343 list_for_each_entry(na, &dlm_node_addrs, list) {
347 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
348 if (addr_compare(na->addr[addr_i], addr)) {
349 *nodeid = na->nodeid;
356 spin_unlock(&dlm_node_addrs_spin);
360 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
362 struct sockaddr_storage *new_addr;
363 struct dlm_node_addr *new_node, *na;
365 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
369 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
375 memcpy(new_addr, addr, len);
377 spin_lock(&dlm_node_addrs_spin);
378 na = find_node_addr(nodeid);
380 new_node->nodeid = nodeid;
381 new_node->addr[0] = new_addr;
382 new_node->addr_count = 1;
383 list_add(&new_node->list, &dlm_node_addrs);
384 spin_unlock(&dlm_node_addrs_spin);
388 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
389 spin_unlock(&dlm_node_addrs_spin);
395 na->addr[na->addr_count++] = new_addr;
396 spin_unlock(&dlm_node_addrs_spin);
401 /* Data available on socket or listen socket received a connect */
402 static void lowcomms_data_ready(struct sock *sk)
404 struct connection *con;
406 read_lock_bh(&sk->sk_callback_lock);
408 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
409 queue_work(recv_workqueue, &con->rwork);
410 read_unlock_bh(&sk->sk_callback_lock);
413 static void lowcomms_write_space(struct sock *sk)
415 struct connection *con;
417 read_lock_bh(&sk->sk_callback_lock);
422 clear_bit(SOCK_NOSPACE, &con->sock->flags);
424 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
425 con->sock->sk->sk_write_pending--;
426 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
429 queue_work(send_workqueue, &con->swork);
431 read_unlock_bh(&sk->sk_callback_lock);
434 static inline void lowcomms_connect_sock(struct connection *con)
436 if (test_bit(CF_CLOSE, &con->flags))
438 queue_work(send_workqueue, &con->swork);
442 static void lowcomms_state_change(struct sock *sk)
444 /* SCTP layer is not calling sk_data_ready when the connection
445 * is done, so we catch the signal through here. Also, it
446 * doesn't switch socket state when entering shutdown, so we
447 * skip the write in that case.
449 if (sk->sk_shutdown) {
450 if (sk->sk_shutdown == RCV_SHUTDOWN)
451 lowcomms_data_ready(sk);
452 } else if (sk->sk_state == TCP_ESTABLISHED) {
453 lowcomms_write_space(sk);
457 int dlm_lowcomms_connect_node(int nodeid)
459 struct connection *con;
461 if (nodeid == dlm_our_nodeid())
464 con = nodeid2con(nodeid, GFP_NOFS);
467 lowcomms_connect_sock(con);
471 static void lowcomms_error_report(struct sock *sk)
473 struct connection *con;
474 struct sockaddr_storage saddr;
475 void (*orig_report)(struct sock *) = NULL;
477 read_lock_bh(&sk->sk_callback_lock);
482 orig_report = listen_sock.sk_error_report;
483 if (con->sock == NULL ||
484 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
485 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
486 "sending to node %d, port %d, "
487 "sk_err=%d/%d\n", dlm_our_nodeid(),
488 con->nodeid, dlm_config.ci_tcp_port,
489 sk->sk_err, sk->sk_err_soft);
490 } else if (saddr.ss_family == AF_INET) {
491 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
493 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
494 "sending to node %d at %pI4, port %d, "
495 "sk_err=%d/%d\n", dlm_our_nodeid(),
496 con->nodeid, &sin4->sin_addr.s_addr,
497 dlm_config.ci_tcp_port, sk->sk_err,
500 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
502 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
503 "sending to node %d at %u.%u.%u.%u, "
504 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
505 con->nodeid, sin6->sin6_addr.s6_addr32[0],
506 sin6->sin6_addr.s6_addr32[1],
507 sin6->sin6_addr.s6_addr32[2],
508 sin6->sin6_addr.s6_addr32[3],
509 dlm_config.ci_tcp_port, sk->sk_err,
513 read_unlock_bh(&sk->sk_callback_lock);
518 /* Note: sk_callback_lock must be locked before calling this function. */
519 static void save_listen_callbacks(struct socket *sock)
521 struct sock *sk = sock->sk;
523 listen_sock.sk_data_ready = sk->sk_data_ready;
524 listen_sock.sk_state_change = sk->sk_state_change;
525 listen_sock.sk_write_space = sk->sk_write_space;
526 listen_sock.sk_error_report = sk->sk_error_report;
529 static void restore_callbacks(struct socket *sock)
531 struct sock *sk = sock->sk;
533 write_lock_bh(&sk->sk_callback_lock);
534 sk->sk_user_data = NULL;
535 sk->sk_data_ready = listen_sock.sk_data_ready;
536 sk->sk_state_change = listen_sock.sk_state_change;
537 sk->sk_write_space = listen_sock.sk_write_space;
538 sk->sk_error_report = listen_sock.sk_error_report;
539 write_unlock_bh(&sk->sk_callback_lock);
542 /* Make a socket active */
543 static void add_sock(struct socket *sock, struct connection *con)
545 struct sock *sk = sock->sk;
547 write_lock_bh(&sk->sk_callback_lock);
550 sk->sk_user_data = con;
551 /* Install a data_ready callback */
552 sk->sk_data_ready = lowcomms_data_ready;
553 sk->sk_write_space = lowcomms_write_space;
554 sk->sk_state_change = lowcomms_state_change;
555 sk->sk_allocation = GFP_NOFS;
556 sk->sk_error_report = lowcomms_error_report;
557 write_unlock_bh(&sk->sk_callback_lock);
560 /* Add the port number to an IPv6 or 4 sockaddr and return the address
562 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
565 saddr->ss_family = dlm_local_addr[0]->ss_family;
566 if (saddr->ss_family == AF_INET) {
567 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
568 in4_addr->sin_port = cpu_to_be16(port);
569 *addr_len = sizeof(struct sockaddr_in);
570 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
572 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
573 in6_addr->sin6_port = cpu_to_be16(port);
574 *addr_len = sizeof(struct sockaddr_in6);
576 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
579 /* Close a remote connection and tidy up */
580 static void close_connection(struct connection *con, bool and_other,
583 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
585 if (tx && !closing && cancel_work_sync(&con->swork)) {
586 log_print("canceled swork for node %d", con->nodeid);
587 clear_bit(CF_WRITE_PENDING, &con->flags);
589 if (rx && !closing && cancel_work_sync(&con->rwork)) {
590 log_print("canceled rwork for node %d", con->nodeid);
591 clear_bit(CF_READ_PENDING, &con->flags);
594 mutex_lock(&con->sock_mutex);
596 restore_callbacks(con->sock);
597 sock_release(con->sock);
600 if (con->othercon && and_other) {
601 /* Will only re-enter once. */
602 close_connection(con->othercon, false, true, true);
605 con->rx_leftover = 0;
607 mutex_unlock(&con->sock_mutex);
608 clear_bit(CF_CLOSING, &con->flags);
611 static void shutdown_connection(struct connection *con)
615 if (cancel_work_sync(&con->swork)) {
616 log_print("canceled swork for node %d", con->nodeid);
617 clear_bit(CF_WRITE_PENDING, &con->flags);
620 mutex_lock(&con->sock_mutex);
621 /* nothing to shutdown */
623 mutex_unlock(&con->sock_mutex);
627 set_bit(CF_SHUTDOWN, &con->flags);
628 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
629 mutex_unlock(&con->sock_mutex);
631 log_print("Connection %p failed to shutdown: %d will force close",
635 ret = wait_event_timeout(con->shutdown_wait,
636 !test_bit(CF_SHUTDOWN, &con->flags),
637 DLM_SHUTDOWN_WAIT_TIMEOUT);
639 log_print("Connection %p shutdown timed out, will force close",
648 clear_bit(CF_SHUTDOWN, &con->flags);
649 close_connection(con, false, true, true);
652 static void dlm_tcp_shutdown(struct connection *con)
655 shutdown_connection(con->othercon);
656 shutdown_connection(con);
659 static int con_realloc_receive_buf(struct connection *con, int newlen)
661 unsigned char *newbuf;
663 newbuf = kmalloc(newlen, GFP_NOFS);
667 /* copy any leftover from last receive */
668 if (con->rx_leftover)
669 memmove(newbuf, con->rx_buf, con->rx_leftover);
671 /* swap to new buffer space */
673 con->rx_buflen = newlen;
674 con->rx_buf = newbuf;
679 /* Data received from remote end */
680 static int receive_from_sock(struct connection *con)
682 int call_again_soon = 0;
687 mutex_lock(&con->sock_mutex);
689 if (con->sock == NULL) {
694 if (con->nodeid == 0) {
699 /* realloc if we get new buffer size to read out */
700 buflen = dlm_config.ci_buffer_size;
701 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
702 ret = con_realloc_receive_buf(con, buflen);
707 /* calculate new buffer parameter regarding last receive and
708 * possible leftover bytes
710 iov.iov_base = con->rx_buf + con->rx_leftover;
711 iov.iov_len = con->rx_buflen - con->rx_leftover;
713 memset(&msg, 0, sizeof(msg));
714 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
715 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
719 else if (ret == iov.iov_len)
722 /* new buflen according readed bytes and leftover from last receive */
723 buflen = ret + con->rx_leftover;
724 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
728 /* calculate leftover bytes from process and put it into begin of
729 * the receive buffer, so next receive we have the full message
730 * at the start address of the receive buffer.
732 con->rx_leftover = buflen - ret;
733 if (con->rx_leftover) {
734 memmove(con->rx_buf, con->rx_buf + ret,
736 call_again_soon = true;
742 mutex_unlock(&con->sock_mutex);
746 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
747 queue_work(recv_workqueue, &con->rwork);
748 mutex_unlock(&con->sock_mutex);
752 mutex_unlock(&con->sock_mutex);
753 if (ret != -EAGAIN) {
754 /* Reconnect when there is something to send */
755 close_connection(con, false, true, false);
757 log_print("connection %p got EOF from %d",
759 /* handling for tcp shutdown */
760 clear_bit(CF_SHUTDOWN, &con->flags);
761 wake_up(&con->shutdown_wait);
762 /* signal to breaking receive worker */
769 /* Listening socket is busy, accept a connection */
770 static int accept_from_sock(struct connection *con)
773 struct sockaddr_storage peeraddr;
774 struct socket *newsock;
777 struct connection *newcon;
778 struct connection *addcon;
781 if (!dlm_allow_conn) {
785 mutex_lock_nested(&con->sock_mutex, 0);
788 mutex_unlock(&con->sock_mutex);
792 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
796 /* Get the connected socket's peer */
797 memset(&peeraddr, 0, sizeof(peeraddr));
798 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
800 result = -ECONNABORTED;
804 /* Get the new node's NODEID */
805 make_sockaddr(&peeraddr, 0, &len);
806 if (addr_to_nodeid(&peeraddr, &nodeid)) {
807 unsigned char *b=(unsigned char *)&peeraddr;
808 log_print("connect from non cluster node");
809 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
810 b, sizeof(struct sockaddr_storage));
811 sock_release(newsock);
812 mutex_unlock(&con->sock_mutex);
816 dlm_comm_mark(nodeid, &mark);
817 sock_set_mark(newsock->sk, mark);
819 log_print("got connection from %d", nodeid);
821 /* Check to see if we already have a connection to this node. This
822 * could happen if the two nodes initiate a connection at roughly
823 * the same time and the connections cross on the wire.
824 * In this case we store the incoming one in "othercon"
826 newcon = nodeid2con(nodeid, GFP_NOFS);
831 mutex_lock_nested(&newcon->sock_mutex, 1);
833 struct connection *othercon = newcon->othercon;
836 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
838 log_print("failed to allocate incoming socket");
839 mutex_unlock(&newcon->sock_mutex);
844 othercon->rx_buflen = dlm_config.ci_buffer_size;
845 othercon->rx_buf = kmalloc(othercon->rx_buflen, GFP_NOFS);
846 if (!othercon->rx_buf) {
847 mutex_unlock(&newcon->sock_mutex);
849 log_print("failed to allocate incoming socket receive buffer");
854 othercon->nodeid = nodeid;
855 othercon->rx_action = receive_from_sock;
856 mutex_init(&othercon->sock_mutex);
857 INIT_LIST_HEAD(&othercon->writequeue);
858 spin_lock_init(&othercon->writequeue_lock);
859 INIT_WORK(&othercon->swork, process_send_sockets);
860 INIT_WORK(&othercon->rwork, process_recv_sockets);
861 init_waitqueue_head(&othercon->shutdown_wait);
862 set_bit(CF_IS_OTHERCON, &othercon->flags);
864 /* close other sock con if we have something new */
865 close_connection(othercon, false, true, false);
868 mutex_lock_nested(&othercon->sock_mutex, 2);
869 newcon->othercon = othercon;
870 add_sock(newsock, othercon);
872 mutex_unlock(&othercon->sock_mutex);
875 newcon->rx_action = receive_from_sock;
876 /* accept copies the sk after we've saved the callbacks, so we
877 don't want to save them a second time or comm errors will
878 result in calling sk_error_report recursively. */
879 add_sock(newsock, newcon);
883 mutex_unlock(&newcon->sock_mutex);
886 * Add it to the active queue in case we got data
887 * between processing the accept adding the socket
888 * to the read_sockets list
890 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
891 queue_work(recv_workqueue, &addcon->rwork);
892 mutex_unlock(&con->sock_mutex);
897 mutex_unlock(&con->sock_mutex);
899 sock_release(newsock);
901 if (result != -EAGAIN)
902 log_print("error accepting connection from node: %d", result);
906 static void free_entry(struct writequeue_entry *e)
908 __free_page(e->page);
913 * writequeue_entry_complete - try to delete and free write queue entry
914 * @e: write queue entry to try to delete
915 * @completed: bytes completed
917 * writequeue_lock must be held.
919 static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
921 e->offset += completed;
924 if (e->len == 0 && e->users == 0) {
931 * sctp_bind_addrs - bind a SCTP socket to all our addresses
933 static int sctp_bind_addrs(struct connection *con, uint16_t port)
935 struct sockaddr_storage localaddr;
936 struct sockaddr *addr = (struct sockaddr *)&localaddr;
937 int i, addr_len, result = 0;
939 for (i = 0; i < dlm_local_count; i++) {
940 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
941 make_sockaddr(&localaddr, port, &addr_len);
944 result = kernel_bind(con->sock, addr, addr_len);
946 result = sock_bind_add(con->sock->sk, addr, addr_len);
949 log_print("Can't bind to %d addr number %d, %d.\n",
950 port, i + 1, result);
957 /* Initiate an SCTP association.
958 This is a special case of send_to_sock() in that we don't yet have a
959 peeled-off socket for this association, so we use the listening socket
960 and add the primary IP address of the remote node.
962 static void sctp_connect_to_sock(struct connection *con)
964 struct sockaddr_storage daddr;
970 if (con->nodeid == 0) {
971 log_print("attempt to connect sock 0 foiled");
975 dlm_comm_mark(con->nodeid, &mark);
977 mutex_lock(&con->sock_mutex);
979 /* Some odd races can cause double-connects, ignore them */
980 if (con->retries++ > MAX_CONNECT_RETRIES)
984 log_print("node %d already connected.", con->nodeid);
988 memset(&daddr, 0, sizeof(daddr));
989 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true);
991 log_print("no address for nodeid %d", con->nodeid);
995 /* Create a socket to communicate with */
996 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
997 SOCK_STREAM, IPPROTO_SCTP, &sock);
1001 sock_set_mark(sock->sk, mark);
1003 con->rx_action = receive_from_sock;
1004 con->connect_action = sctp_connect_to_sock;
1005 add_sock(sock, con);
1007 /* Bind to all addresses. */
1008 if (sctp_bind_addrs(con, 0))
1011 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
1013 log_print("connecting to %d", con->nodeid);
1015 /* Turn off Nagle's algorithm */
1016 sctp_sock_set_nodelay(sock->sk);
1019 * Make sock->ops->connect() function return in specified time,
1020 * since O_NONBLOCK argument in connect() function does not work here,
1021 * then, we should restore the default value of this attribute.
1023 sock_set_sndtimeo(sock->sk, 5);
1024 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
1026 sock_set_sndtimeo(sock->sk, 0);
1028 if (result == -EINPROGRESS)
1039 * Some errors are fatal and this list might need adjusting. For other
1040 * errors we try again until the max number of retries is reached.
1042 if (result != -EHOSTUNREACH &&
1043 result != -ENETUNREACH &&
1044 result != -ENETDOWN &&
1045 result != -EINVAL &&
1046 result != -EPROTONOSUPPORT) {
1047 log_print("connect %d try %d error %d", con->nodeid,
1048 con->retries, result);
1049 mutex_unlock(&con->sock_mutex);
1051 lowcomms_connect_sock(con);
1056 mutex_unlock(&con->sock_mutex);
1059 /* Connect a new socket to its peer */
1060 static void tcp_connect_to_sock(struct connection *con)
1062 struct sockaddr_storage saddr, src_addr;
1064 struct socket *sock = NULL;
1068 if (con->nodeid == 0) {
1069 log_print("attempt to connect sock 0 foiled");
1073 dlm_comm_mark(con->nodeid, &mark);
1075 mutex_lock(&con->sock_mutex);
1076 if (con->retries++ > MAX_CONNECT_RETRIES)
1079 /* Some odd races can cause double-connects, ignore them */
1083 /* Create a socket to communicate with */
1084 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1085 SOCK_STREAM, IPPROTO_TCP, &sock);
1089 sock_set_mark(sock->sk, mark);
1091 memset(&saddr, 0, sizeof(saddr));
1092 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
1094 log_print("no address for nodeid %d", con->nodeid);
1098 con->rx_action = receive_from_sock;
1099 con->connect_action = tcp_connect_to_sock;
1100 con->shutdown_action = dlm_tcp_shutdown;
1101 add_sock(sock, con);
1103 /* Bind to our cluster-known address connecting to avoid
1105 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1106 make_sockaddr(&src_addr, 0, &addr_len);
1107 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1110 log_print("could not bind for connect: %d", result);
1111 /* This *may* not indicate a critical error */
1114 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1116 log_print("connecting to %d", con->nodeid);
1118 /* Turn off Nagle's algorithm */
1119 tcp_sock_set_nodelay(sock->sk);
1121 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1123 if (result == -EINPROGRESS)
1130 sock_release(con->sock);
1136 * Some errors are fatal and this list might need adjusting. For other
1137 * errors we try again until the max number of retries is reached.
1139 if (result != -EHOSTUNREACH &&
1140 result != -ENETUNREACH &&
1141 result != -ENETDOWN &&
1142 result != -EINVAL &&
1143 result != -EPROTONOSUPPORT) {
1144 log_print("connect %d try %d error %d", con->nodeid,
1145 con->retries, result);
1146 mutex_unlock(&con->sock_mutex);
1148 lowcomms_connect_sock(con);
1152 mutex_unlock(&con->sock_mutex);
1156 static struct socket *tcp_create_listen_sock(struct connection *con,
1157 struct sockaddr_storage *saddr)
1159 struct socket *sock = NULL;
1163 if (dlm_local_addr[0]->ss_family == AF_INET)
1164 addr_len = sizeof(struct sockaddr_in);
1166 addr_len = sizeof(struct sockaddr_in6);
1168 /* Create a socket to communicate with */
1169 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1170 SOCK_STREAM, IPPROTO_TCP, &sock);
1172 log_print("Can't create listening comms socket");
1176 sock_set_mark(sock->sk, dlm_config.ci_mark);
1178 /* Turn off Nagle's algorithm */
1179 tcp_sock_set_nodelay(sock->sk);
1181 sock_set_reuseaddr(sock->sk);
1183 write_lock_bh(&sock->sk->sk_callback_lock);
1184 sock->sk->sk_user_data = con;
1185 save_listen_callbacks(sock);
1186 con->rx_action = accept_from_sock;
1187 con->connect_action = tcp_connect_to_sock;
1188 write_unlock_bh(&sock->sk->sk_callback_lock);
1190 /* Bind to our port */
1191 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1192 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1194 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1200 sock_set_keepalive(sock->sk);
1202 result = sock->ops->listen(sock, 5);
1204 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1214 /* Get local addresses */
1215 static void init_local(void)
1217 struct sockaddr_storage sas, *addr;
1220 dlm_local_count = 0;
1221 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1222 if (dlm_our_addr(&sas, i))
1225 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
1228 dlm_local_addr[dlm_local_count++] = addr;
1232 static void deinit_local(void)
1236 for (i = 0; i < dlm_local_count; i++)
1237 kfree(dlm_local_addr[i]);
1240 /* Initialise SCTP socket and bind to all interfaces */
1241 static int sctp_listen_for_all(void)
1243 struct socket *sock = NULL;
1244 int result = -EINVAL;
1245 struct connection *con = nodeid2con(0, GFP_NOFS);
1250 log_print("Using SCTP for communications");
1252 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1253 SOCK_STREAM, IPPROTO_SCTP, &sock);
1255 log_print("Can't create comms socket, check SCTP is loaded");
1259 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1260 sock_set_mark(sock->sk, dlm_config.ci_mark);
1261 sctp_sock_set_nodelay(sock->sk);
1263 write_lock_bh(&sock->sk->sk_callback_lock);
1264 /* Init con struct */
1265 sock->sk->sk_user_data = con;
1266 save_listen_callbacks(sock);
1268 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1269 con->rx_action = accept_from_sock;
1270 con->connect_action = sctp_connect_to_sock;
1272 write_unlock_bh(&sock->sk->sk_callback_lock);
1274 /* Bind to all addresses. */
1275 if (sctp_bind_addrs(con, dlm_config.ci_tcp_port))
1276 goto create_delsock;
1278 result = sock->ops->listen(sock, 5);
1280 log_print("Can't set socket listening");
1281 goto create_delsock;
1293 static int tcp_listen_for_all(void)
1295 struct socket *sock = NULL;
1296 struct connection *con = nodeid2con(0, GFP_NOFS);
1297 int result = -EINVAL;
1302 /* We don't support multi-homed hosts */
1303 if (dlm_local_addr[1] != NULL) {
1304 log_print("TCP protocol can't handle multi-homed hosts, "
1309 log_print("Using TCP for communications");
1311 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1313 add_sock(sock, con);
1317 result = -EADDRINUSE;
1325 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1328 struct writequeue_entry *entry;
1330 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1334 entry->page = alloc_page(allocation);
1349 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1351 struct connection *con;
1352 struct writequeue_entry *e;
1355 con = nodeid2con(nodeid, allocation);
1359 spin_lock(&con->writequeue_lock);
1360 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1361 if ((&e->list == &con->writequeue) ||
1362 (PAGE_SIZE - e->end < len)) {
1369 spin_unlock(&con->writequeue_lock);
1373 *ppc = page_address(e->page) + offset;
1377 e = new_writequeue_entry(con, allocation);
1379 spin_lock(&con->writequeue_lock);
1383 list_add_tail(&e->list, &con->writequeue);
1384 spin_unlock(&con->writequeue_lock);
1390 void dlm_lowcomms_commit_buffer(void *mh)
1392 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1393 struct connection *con = e->con;
1396 spin_lock(&con->writequeue_lock);
1400 e->len = e->end - e->offset;
1401 spin_unlock(&con->writequeue_lock);
1403 queue_work(send_workqueue, &con->swork);
1407 spin_unlock(&con->writequeue_lock);
1411 /* Send a message */
1412 static void send_to_sock(struct connection *con)
1415 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1416 struct writequeue_entry *e;
1420 mutex_lock(&con->sock_mutex);
1421 if (con->sock == NULL)
1424 spin_lock(&con->writequeue_lock);
1426 e = list_entry(con->writequeue.next, struct writequeue_entry,
1428 if ((struct list_head *) e == &con->writequeue)
1433 BUG_ON(len == 0 && e->users == 0);
1434 spin_unlock(&con->writequeue_lock);
1438 ret = kernel_sendpage(con->sock, e->page, offset, len,
1440 if (ret == -EAGAIN || ret == 0) {
1441 if (ret == -EAGAIN &&
1442 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1443 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1444 /* Notify TCP that we're limited by the
1445 * application window size.
1447 set_bit(SOCK_NOSPACE, &con->sock->flags);
1448 con->sock->sk->sk_write_pending++;
1456 /* Don't starve people filling buffers */
1457 if (++count >= MAX_SEND_MSG_COUNT) {
1462 spin_lock(&con->writequeue_lock);
1463 writequeue_entry_complete(e, ret);
1465 spin_unlock(&con->writequeue_lock);
1467 mutex_unlock(&con->sock_mutex);
1471 mutex_unlock(&con->sock_mutex);
1472 close_connection(con, false, false, true);
1473 /* Requeue the send work. When the work daemon runs again, it will try
1474 a new connection, then call this function again. */
1475 queue_work(send_workqueue, &con->swork);
1479 mutex_unlock(&con->sock_mutex);
1480 queue_work(send_workqueue, &con->swork);
1484 static void clean_one_writequeue(struct connection *con)
1486 struct writequeue_entry *e, *safe;
1488 spin_lock(&con->writequeue_lock);
1489 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1493 spin_unlock(&con->writequeue_lock);
1496 /* Called from recovery when it knows that a node has
1498 int dlm_lowcomms_close(int nodeid)
1500 struct connection *con;
1501 struct dlm_node_addr *na;
1503 log_print("closing connection to node %d", nodeid);
1504 con = nodeid2con(nodeid, 0);
1506 set_bit(CF_CLOSE, &con->flags);
1507 close_connection(con, true, true, true);
1508 clean_one_writequeue(con);
1511 spin_lock(&dlm_node_addrs_spin);
1512 na = find_node_addr(nodeid);
1514 list_del(&na->list);
1515 while (na->addr_count--)
1516 kfree(na->addr[na->addr_count]);
1519 spin_unlock(&dlm_node_addrs_spin);
1524 /* Receive workqueue function */
1525 static void process_recv_sockets(struct work_struct *work)
1527 struct connection *con = container_of(work, struct connection, rwork);
1530 clear_bit(CF_READ_PENDING, &con->flags);
1532 err = con->rx_action(con);
1536 /* Send workqueue function */
1537 static void process_send_sockets(struct work_struct *work)
1539 struct connection *con = container_of(work, struct connection, swork);
1541 clear_bit(CF_WRITE_PENDING, &con->flags);
1542 if (con->sock == NULL) /* not mutex protected so check it inside too */
1543 con->connect_action(con);
1544 if (!list_empty(&con->writequeue))
1548 static void work_stop(void)
1551 destroy_workqueue(recv_workqueue);
1553 destroy_workqueue(send_workqueue);
1556 static int work_start(void)
1558 recv_workqueue = alloc_workqueue("dlm_recv",
1559 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1560 if (!recv_workqueue) {
1561 log_print("can't start dlm_recv");
1565 send_workqueue = alloc_workqueue("dlm_send",
1566 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1567 if (!send_workqueue) {
1568 log_print("can't start dlm_send");
1569 destroy_workqueue(recv_workqueue);
1576 static void _stop_conn(struct connection *con, bool and_other)
1578 mutex_lock(&con->sock_mutex);
1579 set_bit(CF_CLOSE, &con->flags);
1580 set_bit(CF_READ_PENDING, &con->flags);
1581 set_bit(CF_WRITE_PENDING, &con->flags);
1582 if (con->sock && con->sock->sk) {
1583 write_lock_bh(&con->sock->sk->sk_callback_lock);
1584 con->sock->sk->sk_user_data = NULL;
1585 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1587 if (con->othercon && and_other)
1588 _stop_conn(con->othercon, false);
1589 mutex_unlock(&con->sock_mutex);
1592 static void stop_conn(struct connection *con)
1594 _stop_conn(con, true);
1597 static void shutdown_conn(struct connection *con)
1599 if (con->shutdown_action)
1600 con->shutdown_action(con);
1603 static void connection_release(struct rcu_head *rcu)
1605 struct connection *con = container_of(rcu, struct connection, rcu);
1611 static void free_conn(struct connection *con)
1613 close_connection(con, true, true, true);
1614 spin_lock(&connections_lock);
1615 hlist_del_rcu(&con->list);
1616 spin_unlock(&connections_lock);
1617 if (con->othercon) {
1618 clean_one_writequeue(con->othercon);
1619 call_rcu(&con->othercon->rcu, connection_release);
1621 clean_one_writequeue(con);
1622 call_rcu(&con->rcu, connection_release);
1625 static void work_flush(void)
1629 struct connection *con;
1633 foreach_conn(stop_conn);
1635 flush_workqueue(recv_workqueue);
1637 flush_workqueue(send_workqueue);
1638 idx = srcu_read_lock(&connections_srcu);
1639 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
1640 hlist_for_each_entry_rcu(con, &connection_hash[i],
1642 ok &= test_bit(CF_READ_PENDING, &con->flags);
1643 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1644 if (con->othercon) {
1645 ok &= test_bit(CF_READ_PENDING,
1646 &con->othercon->flags);
1647 ok &= test_bit(CF_WRITE_PENDING,
1648 &con->othercon->flags);
1652 srcu_read_unlock(&connections_srcu, idx);
1656 void dlm_lowcomms_stop(void)
1658 /* Set all the flags to prevent any
1664 flush_workqueue(recv_workqueue);
1666 flush_workqueue(send_workqueue);
1668 foreach_conn(shutdown_conn);
1670 foreach_conn(free_conn);
1675 int dlm_lowcomms_start(void)
1677 int error = -EINVAL;
1678 struct connection *con;
1681 for (i = 0; i < CONN_HASH_SIZE; i++)
1682 INIT_HLIST_HEAD(&connection_hash[i]);
1685 if (!dlm_local_count) {
1687 log_print("no local IP address has been set");
1691 error = work_start();
1697 /* Start listening */
1698 if (dlm_config.ci_protocol == 0)
1699 error = tcp_listen_for_all();
1701 error = sctp_listen_for_all();
1709 con = nodeid2con(0,0);
1716 void dlm_lowcomms_exit(void)
1718 struct dlm_node_addr *na, *safe;
1720 spin_lock(&dlm_node_addrs_spin);
1721 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1722 list_del(&na->list);
1723 while (na->addr_count--)
1724 kfree(na->addr[na->addr_count]);
1727 spin_unlock(&dlm_node_addrs_spin);