1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 ** This copyrighted material is made available to anyone wishing to use,
8 ** modify, copy, or redistribute it subject to the terms and conditions
9 ** of the GNU General Public License v.2.
11 *******************************************************************************
12 ******************************************************************************/
17 * This is the "low-level" comms layer.
19 * It is responsible for sending/receiving messages
20 * from other nodes in the cluster.
22 * Cluster nodes are referred to by their nodeids. nodeids are
23 * simply 32 bit numbers to the locking module - if they need to
24 * be expanded for the cluster infrastructure then that is its
25 * responsibility. It is this layer's
26 * responsibility to resolve these into IP address or
27 * whatever it needs for inter-node communication.
29 * The comms level is two kernel threads that deal mainly with
30 * the receiving of messages from other nodes and passing them
31 * up to the mid-level comms layer (which understands the
32 * message format) for execution by the locking core, and
33 * a send thread which does all the setting up of connections
34 * to remote nodes and the sending of data. Threads are not allowed
35 * to send their own data because it may cause them to wait in times
36 * of high load. Also, this way, the sending thread can collect together
37 * messages bound for one node and send them in one block.
39 * lowcomms will choose to use either TCP or SCTP as its transport layer
40 * depending on the configuration variable 'protocol'. This should be set
41 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
42 * cluster-wide mechanism as it must be the same on all nodes of the cluster
43 * for the DLM to function.
47 #include <asm/ioctls.h>
50 #include <linux/pagemap.h>
51 #include <linux/file.h>
52 #include <linux/mutex.h>
53 #include <linux/sctp.h>
54 #include <linux/slab.h>
55 #include <net/sctp/sctp.h>
56 #include <net/sctp/user.h>
59 #include "dlm_internal.h"
64 #define NEEDED_RMEM (4*1024*1024)
65 #define CONN_HASH_SIZE 32
67 /* Number of messages to send before rescheduling */
68 #define MAX_SEND_MSG_COUNT 25
76 static void cbuf_add(struct cbuf *cb, int n)
81 static int cbuf_data(struct cbuf *cb)
83 return ((cb->base + cb->len) & cb->mask);
86 static void cbuf_init(struct cbuf *cb, int size)
88 cb->base = cb->len = 0;
92 static void cbuf_eat(struct cbuf *cb, int n)
99 static bool cbuf_empty(struct cbuf *cb)
105 struct socket *sock; /* NULL if not connected */
106 uint32_t nodeid; /* So we know who we are in the list */
107 struct mutex sock_mutex;
109 #define CF_READ_PENDING 1
110 #define CF_WRITE_PENDING 2
111 #define CF_CONNECT_PENDING 3
112 #define CF_INIT_PENDING 4
113 #define CF_IS_OTHERCON 5
115 #define CF_APP_LIMITED 7
116 struct list_head writequeue; /* List of outgoing writequeue_entries */
117 spinlock_t writequeue_lock;
118 int (*rx_action) (struct connection *); /* What to do when active */
119 void (*connect_action) (struct connection *); /* What to do to connect */
120 struct page *rx_page;
123 #define MAX_CONNECT_RETRIES 3
125 struct hlist_node list;
126 struct connection *othercon;
127 struct work_struct rwork; /* Receive workqueue */
128 struct work_struct swork; /* Send workqueue */
130 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
132 /* An entry waiting to be sent */
133 struct writequeue_entry {
134 struct list_head list;
140 struct connection *con;
143 struct dlm_node_addr {
144 struct list_head list;
147 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
150 static LIST_HEAD(dlm_node_addrs);
151 static DEFINE_SPINLOCK(dlm_node_addrs_spin);
153 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
154 static int dlm_local_count;
155 static int dlm_allow_conn;
158 static struct workqueue_struct *recv_workqueue;
159 static struct workqueue_struct *send_workqueue;
161 static struct hlist_head connection_hash[CONN_HASH_SIZE];
162 static DEFINE_MUTEX(connections_lock);
163 static struct kmem_cache *con_cache;
165 static void process_recv_sockets(struct work_struct *work);
166 static void process_send_sockets(struct work_struct *work);
169 /* This is deliberately very simple because most clusters have simple
170 sequential nodeids, so we should be able to go straight to a connection
171 struct in the array */
172 static inline int nodeid_hash(int nodeid)
174 return nodeid & (CONN_HASH_SIZE-1);
177 static struct connection *__find_con(int nodeid)
180 struct hlist_node *h;
181 struct connection *con;
183 r = nodeid_hash(nodeid);
185 hlist_for_each_entry(con, h, &connection_hash[r], list) {
186 if (con->nodeid == nodeid)
193 * If 'allocation' is zero then we don't attempt to create a new
194 * connection structure for this node.
196 static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
198 struct connection *con = NULL;
201 con = __find_con(nodeid);
205 con = kmem_cache_zalloc(con_cache, alloc);
209 r = nodeid_hash(nodeid);
210 hlist_add_head(&con->list, &connection_hash[r]);
212 con->nodeid = nodeid;
213 mutex_init(&con->sock_mutex);
214 INIT_LIST_HEAD(&con->writequeue);
215 spin_lock_init(&con->writequeue_lock);
216 INIT_WORK(&con->swork, process_send_sockets);
217 INIT_WORK(&con->rwork, process_recv_sockets);
219 /* Setup action pointers for child sockets */
221 struct connection *zerocon = __find_con(0);
223 con->connect_action = zerocon->connect_action;
225 con->rx_action = zerocon->rx_action;
231 /* Loop round all connections */
232 static void foreach_conn(void (*conn_func)(struct connection *c))
235 struct hlist_node *h, *n;
236 struct connection *con;
238 for (i = 0; i < CONN_HASH_SIZE; i++) {
239 hlist_for_each_entry_safe(con, h, n, &connection_hash[i], list){
245 static struct connection *nodeid2con(int nodeid, gfp_t allocation)
247 struct connection *con;
249 mutex_lock(&connections_lock);
250 con = __nodeid2con(nodeid, allocation);
251 mutex_unlock(&connections_lock);
256 /* This is a bit drastic, but only called when things go wrong */
257 static struct connection *assoc2con(int assoc_id)
260 struct hlist_node *h;
261 struct connection *con;
263 mutex_lock(&connections_lock);
265 for (i = 0 ; i < CONN_HASH_SIZE; i++) {
266 hlist_for_each_entry(con, h, &connection_hash[i], list) {
267 if (con->sctp_assoc == assoc_id) {
268 mutex_unlock(&connections_lock);
273 mutex_unlock(&connections_lock);
277 static struct dlm_node_addr *find_node_addr(int nodeid)
279 struct dlm_node_addr *na;
281 list_for_each_entry(na, &dlm_node_addrs, list) {
282 if (na->nodeid == nodeid)
288 static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
290 switch (x->ss_family) {
292 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
293 struct sockaddr_in *siny = (struct sockaddr_in *)y;
294 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
296 if (sinx->sin_port != siny->sin_port)
301 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
302 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
303 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
305 if (sinx->sin6_port != siny->sin6_port)
315 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
316 struct sockaddr *sa_out)
318 struct sockaddr_storage sas;
319 struct dlm_node_addr *na;
321 if (!dlm_local_count)
324 spin_lock(&dlm_node_addrs_spin);
325 na = find_node_addr(nodeid);
326 if (na && na->addr_count)
327 memcpy(&sas, na->addr[0], sizeof(struct sockaddr_storage));
328 spin_unlock(&dlm_node_addrs_spin);
337 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
342 if (dlm_local_addr[0]->ss_family == AF_INET) {
343 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
344 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
345 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
347 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
348 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
349 ret6->sin6_addr = in6->sin6_addr;
355 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
357 struct dlm_node_addr *na;
360 spin_lock(&dlm_node_addrs_spin);
361 list_for_each_entry(na, &dlm_node_addrs, list) {
365 if (!addr_compare(na->addr[0], addr))
368 *nodeid = na->nodeid;
372 spin_unlock(&dlm_node_addrs_spin);
376 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
378 struct sockaddr_storage *new_addr;
379 struct dlm_node_addr *new_node, *na;
381 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
385 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
391 memcpy(new_addr, addr, len);
393 spin_lock(&dlm_node_addrs_spin);
394 na = find_node_addr(nodeid);
396 new_node->nodeid = nodeid;
397 new_node->addr[0] = new_addr;
398 new_node->addr_count = 1;
399 list_add(&new_node->list, &dlm_node_addrs);
400 spin_unlock(&dlm_node_addrs_spin);
404 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
405 spin_unlock(&dlm_node_addrs_spin);
411 na->addr[na->addr_count++] = new_addr;
412 spin_unlock(&dlm_node_addrs_spin);
417 /* Data available on socket or listen socket received a connect */
418 static void lowcomms_data_ready(struct sock *sk, int count_unused)
420 struct connection *con = sock2con(sk);
421 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
422 queue_work(recv_workqueue, &con->rwork);
425 static void lowcomms_write_space(struct sock *sk)
427 struct connection *con = sock2con(sk);
432 clear_bit(SOCK_NOSPACE, &con->sock->flags);
434 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
435 con->sock->sk->sk_write_pending--;
436 clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags);
439 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
440 queue_work(send_workqueue, &con->swork);
443 static inline void lowcomms_connect_sock(struct connection *con)
445 if (test_bit(CF_CLOSE, &con->flags))
447 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
448 queue_work(send_workqueue, &con->swork);
451 static void lowcomms_state_change(struct sock *sk)
453 if (sk->sk_state == TCP_ESTABLISHED)
454 lowcomms_write_space(sk);
457 int dlm_lowcomms_connect_node(int nodeid)
459 struct connection *con;
461 /* with sctp there's no connecting without sending */
462 if (dlm_config.ci_protocol != 0)
465 if (nodeid == dlm_our_nodeid())
468 con = nodeid2con(nodeid, GFP_NOFS);
471 lowcomms_connect_sock(con);
475 /* Make a socket active */
476 static void add_sock(struct socket *sock, struct connection *con)
480 /* Install a data_ready callback */
481 con->sock->sk->sk_data_ready = lowcomms_data_ready;
482 con->sock->sk->sk_write_space = lowcomms_write_space;
483 con->sock->sk->sk_state_change = lowcomms_state_change;
484 con->sock->sk->sk_user_data = con;
485 con->sock->sk->sk_allocation = GFP_NOFS;
488 /* Add the port number to an IPv6 or 4 sockaddr and return the address
490 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
493 saddr->ss_family = dlm_local_addr[0]->ss_family;
494 if (saddr->ss_family == AF_INET) {
495 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
496 in4_addr->sin_port = cpu_to_be16(port);
497 *addr_len = sizeof(struct sockaddr_in);
498 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
500 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
501 in6_addr->sin6_port = cpu_to_be16(port);
502 *addr_len = sizeof(struct sockaddr_in6);
504 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
507 /* Close a remote connection and tidy up */
508 static void close_connection(struct connection *con, bool and_other)
510 mutex_lock(&con->sock_mutex);
513 sock_release(con->sock);
516 if (con->othercon && and_other) {
517 /* Will only re-enter once. */
518 close_connection(con->othercon, false);
521 __free_page(con->rx_page);
526 mutex_unlock(&con->sock_mutex);
529 /* We only send shutdown messages to nodes that are not part of the cluster */
530 static void sctp_send_shutdown(sctp_assoc_t associd)
532 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
533 struct msghdr outmessage;
534 struct cmsghdr *cmsg;
535 struct sctp_sndrcvinfo *sinfo;
537 struct connection *con;
539 con = nodeid2con(0,0);
542 outmessage.msg_name = NULL;
543 outmessage.msg_namelen = 0;
544 outmessage.msg_control = outcmsg;
545 outmessage.msg_controllen = sizeof(outcmsg);
546 outmessage.msg_flags = MSG_EOR;
548 cmsg = CMSG_FIRSTHDR(&outmessage);
549 cmsg->cmsg_level = IPPROTO_SCTP;
550 cmsg->cmsg_type = SCTP_SNDRCV;
551 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
552 outmessage.msg_controllen = cmsg->cmsg_len;
553 sinfo = CMSG_DATA(cmsg);
554 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
556 sinfo->sinfo_flags |= MSG_EOF;
557 sinfo->sinfo_assoc_id = associd;
559 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
562 log_print("send EOF to node failed: %d", ret);
565 static void sctp_init_failed_foreach(struct connection *con)
568 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
569 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
570 queue_work(send_workqueue, &con->swork);
574 /* INIT failed but we don't know which node...
575 restart INIT on all pending nodes */
576 static void sctp_init_failed(void)
578 mutex_lock(&connections_lock);
580 foreach_conn(sctp_init_failed_foreach);
582 mutex_unlock(&connections_lock);
585 /* Something happened to an association */
586 static void process_sctp_notification(struct connection *con,
587 struct msghdr *msg, char *buf)
589 union sctp_notification *sn = (union sctp_notification *)buf;
591 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
592 switch (sn->sn_assoc_change.sac_state) {
597 /* Check that the new node is in the lockspace */
598 struct sctp_prim prim;
602 struct connection *new_con;
605 * We get this before any data for an association.
606 * We verify that the node is in the cluster and
607 * then peel off a socket for it.
609 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
610 log_print("COMM_UP for invalid assoc ID %d",
611 (int)sn->sn_assoc_change.sac_assoc_id);
615 memset(&prim, 0, sizeof(struct sctp_prim));
616 prim_len = sizeof(struct sctp_prim);
617 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
619 ret = kernel_getsockopt(con->sock,
625 log_print("getsockopt/sctp_primary_addr on "
626 "new assoc %d failed : %d",
627 (int)sn->sn_assoc_change.sac_assoc_id,
630 /* Retry INIT later */
631 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
633 clear_bit(CF_CONNECT_PENDING, &con->flags);
636 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
637 if (addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
638 unsigned char *b=(unsigned char *)&prim.ssp_addr;
639 log_print("reject connect from unknown addr");
640 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
641 b, sizeof(struct sockaddr_storage));
642 sctp_send_shutdown(prim.ssp_assoc_id);
646 new_con = nodeid2con(nodeid, GFP_NOFS);
650 /* Peel off a new sock */
651 sctp_lock_sock(con->sock->sk);
652 ret = sctp_do_peeloff(con->sock->sk,
653 sn->sn_assoc_change.sac_assoc_id,
655 sctp_release_sock(con->sock->sk);
657 log_print("Can't peel off a socket for "
658 "connection %d to node %d: err=%d",
659 (int)sn->sn_assoc_change.sac_assoc_id,
663 add_sock(new_con->sock, new_con);
665 log_print("connecting to %d sctp association %d",
666 nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
668 /* Send any pending writes */
669 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
670 clear_bit(CF_INIT_PENDING, &con->flags);
671 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
672 queue_work(send_workqueue, &new_con->swork);
674 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
675 queue_work(recv_workqueue, &new_con->rwork);
680 case SCTP_SHUTDOWN_COMP:
682 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
689 /* We don't know which INIT failed, so clear the PENDING flags
690 * on them all. if assoc_id is zero then it will then try
693 case SCTP_CANT_STR_ASSOC:
695 log_print("Can't start SCTP association - retrying");
701 log_print("unexpected SCTP assoc change id=%d state=%d",
702 (int)sn->sn_assoc_change.sac_assoc_id,
703 sn->sn_assoc_change.sac_state);
708 /* Data received from remote end */
709 static int receive_from_sock(struct connection *con)
712 struct msghdr msg = {};
716 int call_again_soon = 0;
718 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
720 mutex_lock(&con->sock_mutex);
722 if (con->sock == NULL) {
727 if (con->rx_page == NULL) {
729 * This doesn't need to be atomic, but I think it should
730 * improve performance if it is.
732 con->rx_page = alloc_page(GFP_ATOMIC);
733 if (con->rx_page == NULL)
735 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
738 /* Only SCTP needs these really */
739 memset(&incmsg, 0, sizeof(incmsg));
740 msg.msg_control = incmsg;
741 msg.msg_controllen = sizeof(incmsg);
744 * iov[0] is the bit of the circular buffer between the current end
745 * point (cb.base + cb.len) and the end of the buffer.
747 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
748 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
753 * iov[1] is the bit of the circular buffer between the start of the
754 * buffer and the start of the currently used section (cb.base)
756 if (cbuf_data(&con->cb) >= con->cb.base) {
757 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
758 iov[1].iov_len = con->cb.base;
759 iov[1].iov_base = page_address(con->rx_page);
762 len = iov[0].iov_len + iov[1].iov_len;
764 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
765 MSG_DONTWAIT | MSG_NOSIGNAL);
769 /* Process SCTP notifications */
770 if (msg.msg_flags & MSG_NOTIFICATION) {
771 msg.msg_control = incmsg;
772 msg.msg_controllen = sizeof(incmsg);
774 process_sctp_notification(con, &msg,
775 page_address(con->rx_page) + con->cb.base);
776 mutex_unlock(&con->sock_mutex);
779 BUG_ON(con->nodeid == 0);
783 cbuf_add(&con->cb, ret);
784 ret = dlm_process_incoming_buffer(con->nodeid,
785 page_address(con->rx_page),
786 con->cb.base, con->cb.len,
788 if (ret == -EBADMSG) {
789 log_print("lowcomms: addr=%p, base=%u, len=%u, "
790 "iov_len=%u, iov_base[0]=%p, read=%d",
791 page_address(con->rx_page), con->cb.base, con->cb.len,
792 len, iov[0].iov_base, r);
796 cbuf_eat(&con->cb, ret);
798 if (cbuf_empty(&con->cb) && !call_again_soon) {
799 __free_page(con->rx_page);
805 mutex_unlock(&con->sock_mutex);
809 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
810 queue_work(recv_workqueue, &con->rwork);
811 mutex_unlock(&con->sock_mutex);
815 mutex_unlock(&con->sock_mutex);
816 if (ret != -EAGAIN) {
817 close_connection(con, false);
818 /* Reconnect when there is something to send */
820 /* Don't return success if we really got EOF */
827 /* Listening socket is busy, accept a connection */
828 static int tcp_accept_from_sock(struct connection *con)
831 struct sockaddr_storage peeraddr;
832 struct socket *newsock;
835 struct connection *newcon;
836 struct connection *addcon;
838 mutex_lock(&connections_lock);
839 if (!dlm_allow_conn) {
840 mutex_unlock(&connections_lock);
843 mutex_unlock(&connections_lock);
845 memset(&peeraddr, 0, sizeof(peeraddr));
846 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
847 IPPROTO_TCP, &newsock);
851 mutex_lock_nested(&con->sock_mutex, 0);
854 if (con->sock == NULL)
857 newsock->type = con->sock->type;
858 newsock->ops = con->sock->ops;
860 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
864 /* Get the connected socket's peer */
865 memset(&peeraddr, 0, sizeof(peeraddr));
866 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
868 result = -ECONNABORTED;
872 /* Get the new node's NODEID */
873 make_sockaddr(&peeraddr, 0, &len);
874 if (addr_to_nodeid(&peeraddr, &nodeid)) {
875 unsigned char *b=(unsigned char *)&peeraddr;
876 log_print("connect from non cluster node");
877 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
878 b, sizeof(struct sockaddr_storage));
879 sock_release(newsock);
880 mutex_unlock(&con->sock_mutex);
884 log_print("got connection from %d", nodeid);
886 /* Check to see if we already have a connection to this node. This
887 * could happen if the two nodes initiate a connection at roughly
888 * the same time and the connections cross on the wire.
889 * In this case we store the incoming one in "othercon"
891 newcon = nodeid2con(nodeid, GFP_NOFS);
896 mutex_lock_nested(&newcon->sock_mutex, 1);
898 struct connection *othercon = newcon->othercon;
901 othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
903 log_print("failed to allocate incoming socket");
904 mutex_unlock(&newcon->sock_mutex);
908 othercon->nodeid = nodeid;
909 othercon->rx_action = receive_from_sock;
910 mutex_init(&othercon->sock_mutex);
911 INIT_WORK(&othercon->swork, process_send_sockets);
912 INIT_WORK(&othercon->rwork, process_recv_sockets);
913 set_bit(CF_IS_OTHERCON, &othercon->flags);
915 if (!othercon->sock) {
916 newcon->othercon = othercon;
917 othercon->sock = newsock;
918 newsock->sk->sk_user_data = othercon;
919 add_sock(newsock, othercon);
923 printk("Extra connection from node %d attempted\n", nodeid);
925 mutex_unlock(&newcon->sock_mutex);
930 newsock->sk->sk_user_data = newcon;
931 newcon->rx_action = receive_from_sock;
932 add_sock(newsock, newcon);
936 mutex_unlock(&newcon->sock_mutex);
939 * Add it to the active queue in case we got data
940 * between processing the accept adding the socket
941 * to the read_sockets list
943 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
944 queue_work(recv_workqueue, &addcon->rwork);
945 mutex_unlock(&con->sock_mutex);
950 mutex_unlock(&con->sock_mutex);
951 sock_release(newsock);
953 if (result != -EAGAIN)
954 log_print("error accepting connection from node: %d", result);
958 static void free_entry(struct writequeue_entry *e)
960 __free_page(e->page);
964 /* Initiate an SCTP association.
965 This is a special case of send_to_sock() in that we don't yet have a
966 peeled-off socket for this association, so we use the listening socket
967 and add the primary IP address of the remote node.
969 static void sctp_init_assoc(struct connection *con)
971 struct sockaddr_storage rem_addr;
972 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
973 struct msghdr outmessage;
974 struct cmsghdr *cmsg;
975 struct sctp_sndrcvinfo *sinfo;
976 struct connection *base_con;
977 struct writequeue_entry *e;
983 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
986 if (con->retries++ > MAX_CONNECT_RETRIES)
989 if (nodeid_to_addr(con->nodeid, NULL, (struct sockaddr *)&rem_addr)) {
990 log_print("no address for nodeid %d", con->nodeid);
993 base_con = nodeid2con(0, 0);
994 BUG_ON(base_con == NULL);
996 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
998 outmessage.msg_name = &rem_addr;
999 outmessage.msg_namelen = addrlen;
1000 outmessage.msg_control = outcmsg;
1001 outmessage.msg_controllen = sizeof(outcmsg);
1002 outmessage.msg_flags = MSG_EOR;
1004 spin_lock(&con->writequeue_lock);
1006 if (list_empty(&con->writequeue)) {
1007 spin_unlock(&con->writequeue_lock);
1008 log_print("writequeue empty for nodeid %d", con->nodeid);
1012 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
1015 spin_unlock(&con->writequeue_lock);
1017 /* Send the first block off the write queue */
1018 iov[0].iov_base = page_address(e->page)+offset;
1019 iov[0].iov_len = len;
1021 cmsg = CMSG_FIRSTHDR(&outmessage);
1022 cmsg->cmsg_level = IPPROTO_SCTP;
1023 cmsg->cmsg_type = SCTP_SNDRCV;
1024 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1025 sinfo = CMSG_DATA(cmsg);
1026 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
1027 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
1028 outmessage.msg_controllen = cmsg->cmsg_len;
1030 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
1032 log_print("Send first packet to node %d failed: %d",
1035 /* Try again later */
1036 clear_bit(CF_CONNECT_PENDING, &con->flags);
1037 clear_bit(CF_INIT_PENDING, &con->flags);
1040 spin_lock(&con->writequeue_lock);
1044 if (e->len == 0 && e->users == 0) {
1048 spin_unlock(&con->writequeue_lock);
1052 /* Connect a new socket to its peer */
1053 static void tcp_connect_to_sock(struct connection *con)
1055 struct sockaddr_storage saddr, src_addr;
1057 struct socket *sock = NULL;
1061 if (con->nodeid == 0) {
1062 log_print("attempt to connect sock 0 foiled");
1066 mutex_lock(&con->sock_mutex);
1067 if (con->retries++ > MAX_CONNECT_RETRIES)
1070 /* Some odd races can cause double-connects, ignore them */
1074 /* Create a socket to communicate with */
1075 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1076 IPPROTO_TCP, &sock);
1080 memset(&saddr, 0, sizeof(saddr));
1081 result = nodeid_to_addr(con->nodeid, &saddr, NULL);
1083 log_print("no address for nodeid %d", con->nodeid);
1087 sock->sk->sk_user_data = con;
1088 con->rx_action = receive_from_sock;
1089 con->connect_action = tcp_connect_to_sock;
1090 add_sock(sock, con);
1092 /* Bind to our cluster-known address connecting to avoid
1094 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1095 make_sockaddr(&src_addr, 0, &addr_len);
1096 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1099 log_print("could not bind for connect: %d", result);
1100 /* This *may* not indicate a critical error */
1103 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1105 log_print("connecting to %d", con->nodeid);
1107 /* Turn off Nagle's algorithm */
1108 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1111 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1113 if (result == -EINPROGRESS)
1120 sock_release(con->sock);
1126 * Some errors are fatal and this list might need adjusting. For other
1127 * errors we try again until the max number of retries is reached.
1129 if (result != -EHOSTUNREACH &&
1130 result != -ENETUNREACH &&
1131 result != -ENETDOWN &&
1132 result != -EINVAL &&
1133 result != -EPROTONOSUPPORT) {
1134 log_print("connect %d try %d error %d", con->nodeid,
1135 con->retries, result);
1136 mutex_unlock(&con->sock_mutex);
1138 lowcomms_connect_sock(con);
1142 mutex_unlock(&con->sock_mutex);
1146 static struct socket *tcp_create_listen_sock(struct connection *con,
1147 struct sockaddr_storage *saddr)
1149 struct socket *sock = NULL;
1154 if (dlm_local_addr[0]->ss_family == AF_INET)
1155 addr_len = sizeof(struct sockaddr_in);
1157 addr_len = sizeof(struct sockaddr_in6);
1159 /* Create a socket to communicate with */
1160 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1161 IPPROTO_TCP, &sock);
1163 log_print("Can't create listening comms socket");
1167 /* Turn off Nagle's algorithm */
1168 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1171 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1172 (char *)&one, sizeof(one));
1175 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
1177 con->rx_action = tcp_accept_from_sock;
1178 con->connect_action = tcp_connect_to_sock;
1180 /* Bind to our port */
1181 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1182 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1184 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1190 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1191 (char *)&one, sizeof(one));
1193 log_print("Set keepalive failed: %d", result);
1196 result = sock->ops->listen(sock, 5);
1198 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1208 /* Get local addresses */
1209 static void init_local(void)
1211 struct sockaddr_storage sas, *addr;
1214 dlm_local_count = 0;
1215 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1216 if (dlm_our_addr(&sas, i))
1219 addr = kmalloc(sizeof(*addr), GFP_NOFS);
1222 memcpy(addr, &sas, sizeof(*addr));
1223 dlm_local_addr[dlm_local_count++] = addr;
1227 /* Bind to an IP address. SCTP allows multiple address so it can do
1229 static int add_sctp_bind_addr(struct connection *sctp_con,
1230 struct sockaddr_storage *addr,
1231 int addr_len, int num)
1236 result = kernel_bind(sctp_con->sock,
1237 (struct sockaddr *) addr,
1240 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1241 SCTP_SOCKOPT_BINDX_ADD,
1242 (char *)addr, addr_len);
1245 log_print("Can't bind to port %d addr number %d",
1246 dlm_config.ci_tcp_port, num);
1251 /* Initialise SCTP socket and bind to all interfaces */
1252 static int sctp_listen_for_all(void)
1254 struct socket *sock = NULL;
1255 struct sockaddr_storage localaddr;
1256 struct sctp_event_subscribe subscribe;
1257 int result = -EINVAL, num = 1, i, addr_len;
1258 struct connection *con = nodeid2con(0, GFP_NOFS);
1259 int bufsize = NEEDED_RMEM;
1264 log_print("Using SCTP for communications");
1266 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1267 IPPROTO_SCTP, &sock);
1269 log_print("Can't create comms socket, check SCTP is loaded");
1273 /* Listen for events */
1274 memset(&subscribe, 0, sizeof(subscribe));
1275 subscribe.sctp_data_io_event = 1;
1276 subscribe.sctp_association_event = 1;
1277 subscribe.sctp_send_failure_event = 1;
1278 subscribe.sctp_shutdown_event = 1;
1279 subscribe.sctp_partial_delivery_event = 1;
1281 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
1282 (char *)&bufsize, sizeof(bufsize));
1284 log_print("Error increasing buffer space on socket %d", result);
1286 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
1287 (char *)&subscribe, sizeof(subscribe));
1289 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1291 goto create_delsock;
1294 /* Init con struct */
1295 sock->sk->sk_user_data = con;
1297 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1298 con->rx_action = receive_from_sock;
1299 con->connect_action = sctp_init_assoc;
1301 /* Bind to all interfaces. */
1302 for (i = 0; i < dlm_local_count; i++) {
1303 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1304 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1306 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1308 goto create_delsock;
1312 result = sock->ops->listen(sock, 5);
1314 log_print("Can't set socket listening");
1315 goto create_delsock;
1327 static int tcp_listen_for_all(void)
1329 struct socket *sock = NULL;
1330 struct connection *con = nodeid2con(0, GFP_NOFS);
1331 int result = -EINVAL;
1336 /* We don't support multi-homed hosts */
1337 if (dlm_local_addr[1] != NULL) {
1338 log_print("TCP protocol can't handle multi-homed hosts, "
1343 log_print("Using TCP for communications");
1345 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1347 add_sock(sock, con);
1351 result = -EADDRINUSE;
1359 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1362 struct writequeue_entry *entry;
1364 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1368 entry->page = alloc_page(allocation);
1383 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1385 struct connection *con;
1386 struct writequeue_entry *e;
1390 con = nodeid2con(nodeid, allocation);
1394 spin_lock(&con->writequeue_lock);
1395 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1396 if ((&e->list == &con->writequeue) ||
1397 (PAGE_CACHE_SIZE - e->end < len)) {
1404 spin_unlock(&con->writequeue_lock);
1408 *ppc = page_address(e->page) + offset;
1412 e = new_writequeue_entry(con, allocation);
1414 spin_lock(&con->writequeue_lock);
1418 list_add_tail(&e->list, &con->writequeue);
1419 spin_unlock(&con->writequeue_lock);
1425 void dlm_lowcomms_commit_buffer(void *mh)
1427 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1428 struct connection *con = e->con;
1431 spin_lock(&con->writequeue_lock);
1435 e->len = e->end - e->offset;
1436 spin_unlock(&con->writequeue_lock);
1438 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1439 queue_work(send_workqueue, &con->swork);
1444 spin_unlock(&con->writequeue_lock);
1448 /* Send a message */
1449 static void send_to_sock(struct connection *con)
1452 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1453 struct writequeue_entry *e;
1457 mutex_lock(&con->sock_mutex);
1458 if (con->sock == NULL)
1461 spin_lock(&con->writequeue_lock);
1463 e = list_entry(con->writequeue.next, struct writequeue_entry,
1465 if ((struct list_head *) e == &con->writequeue)
1470 BUG_ON(len == 0 && e->users == 0);
1471 spin_unlock(&con->writequeue_lock);
1475 ret = kernel_sendpage(con->sock, e->page, offset, len,
1477 if (ret == -EAGAIN || ret == 0) {
1478 if (ret == -EAGAIN &&
1479 test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
1480 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1481 /* Notify TCP that we're limited by the
1482 * application window size.
1484 set_bit(SOCK_NOSPACE, &con->sock->flags);
1485 con->sock->sk->sk_write_pending++;
1493 /* Don't starve people filling buffers */
1494 if (++count >= MAX_SEND_MSG_COUNT) {
1499 spin_lock(&con->writequeue_lock);
1503 if (e->len == 0 && e->users == 0) {
1508 spin_unlock(&con->writequeue_lock);
1510 mutex_unlock(&con->sock_mutex);
1514 mutex_unlock(&con->sock_mutex);
1515 close_connection(con, false);
1516 lowcomms_connect_sock(con);
1520 mutex_unlock(&con->sock_mutex);
1521 if (!test_bit(CF_INIT_PENDING, &con->flags))
1522 lowcomms_connect_sock(con);
1525 static void clean_one_writequeue(struct connection *con)
1527 struct writequeue_entry *e, *safe;
1529 spin_lock(&con->writequeue_lock);
1530 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1534 spin_unlock(&con->writequeue_lock);
1537 /* Called from recovery when it knows that a node has
1539 int dlm_lowcomms_close(int nodeid)
1541 struct connection *con;
1542 struct dlm_node_addr *na;
1544 log_print("closing connection to node %d", nodeid);
1545 con = nodeid2con(nodeid, 0);
1547 clear_bit(CF_CONNECT_PENDING, &con->flags);
1548 clear_bit(CF_WRITE_PENDING, &con->flags);
1549 set_bit(CF_CLOSE, &con->flags);
1550 if (cancel_work_sync(&con->swork))
1551 log_print("canceled swork for node %d", nodeid);
1552 if (cancel_work_sync(&con->rwork))
1553 log_print("canceled rwork for node %d", nodeid);
1554 clean_one_writequeue(con);
1555 close_connection(con, true);
1558 spin_lock(&dlm_node_addrs_spin);
1559 na = find_node_addr(nodeid);
1561 list_del(&na->list);
1562 while (na->addr_count--)
1563 kfree(na->addr[na->addr_count]);
1566 spin_unlock(&dlm_node_addrs_spin);
1571 /* Receive workqueue function */
1572 static void process_recv_sockets(struct work_struct *work)
1574 struct connection *con = container_of(work, struct connection, rwork);
1577 clear_bit(CF_READ_PENDING, &con->flags);
1579 err = con->rx_action(con);
1583 /* Send workqueue function */
1584 static void process_send_sockets(struct work_struct *work)
1586 struct connection *con = container_of(work, struct connection, swork);
1588 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1589 con->connect_action(con);
1590 set_bit(CF_WRITE_PENDING, &con->flags);
1592 if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
1597 /* Discard all entries on the write queues */
1598 static void clean_writequeues(void)
1600 foreach_conn(clean_one_writequeue);
1603 static void work_stop(void)
1605 destroy_workqueue(recv_workqueue);
1606 destroy_workqueue(send_workqueue);
1609 static int work_start(void)
1611 recv_workqueue = alloc_workqueue("dlm_recv",
1612 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1613 if (!recv_workqueue) {
1614 log_print("can't start dlm_recv");
1618 send_workqueue = alloc_workqueue("dlm_send",
1619 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1620 if (!send_workqueue) {
1621 log_print("can't start dlm_send");
1622 destroy_workqueue(recv_workqueue);
1629 static void stop_conn(struct connection *con)
1632 if (con->sock && con->sock->sk)
1633 con->sock->sk->sk_user_data = NULL;
1636 static void free_conn(struct connection *con)
1638 close_connection(con, true);
1640 kmem_cache_free(con_cache, con->othercon);
1641 hlist_del(&con->list);
1642 kmem_cache_free(con_cache, con);
1645 void dlm_lowcomms_stop(void)
1647 /* Set all the flags to prevent any
1650 mutex_lock(&connections_lock);
1652 foreach_conn(stop_conn);
1653 mutex_unlock(&connections_lock);
1657 mutex_lock(&connections_lock);
1658 clean_writequeues();
1660 foreach_conn(free_conn);
1662 mutex_unlock(&connections_lock);
1663 kmem_cache_destroy(con_cache);
1666 int dlm_lowcomms_start(void)
1668 int error = -EINVAL;
1669 struct connection *con;
1672 for (i = 0; i < CONN_HASH_SIZE; i++)
1673 INIT_HLIST_HEAD(&connection_hash[i]);
1676 if (!dlm_local_count) {
1678 log_print("no local IP address has been set");
1683 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1684 __alignof__(struct connection), 0,
1689 error = work_start();
1695 /* Start listening */
1696 if (dlm_config.ci_protocol == 0)
1697 error = tcp_listen_for_all();
1699 error = sctp_listen_for_all();
1707 con = nodeid2con(0,0);
1709 close_connection(con, false);
1710 kmem_cache_free(con_cache, con);
1713 kmem_cache_destroy(con_cache);
1718 void dlm_lowcomms_exit(void)
1720 struct dlm_node_addr *na, *safe;
1722 spin_lock(&dlm_node_addrs_spin);
1723 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1724 list_del(&na->list);
1725 while (na->addr_count--)
1726 kfree(na->addr[na->addr_count]);
1729 spin_unlock(&dlm_node_addrs_spin);