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 <linux/sctp.h>
56 #include <net/sctp/sctp.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 */
131 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
133 /* An entry waiting to be sent */
134 struct writequeue_entry {
135 struct list_head list;
141 struct connection *con;
144 struct dlm_node_addr {
145 struct list_head list;
149 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
152 static LIST_HEAD(dlm_node_addrs);
153 static DEFINE_SPINLOCK(dlm_node_addrs_spin);
155 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
156 static int dlm_local_count;
157 static int dlm_allow_conn;
160 static struct workqueue_struct *recv_workqueue;
161 static struct workqueue_struct *send_workqueue;
163 static struct hlist_head connection_hash[CONN_HASH_SIZE];
164 static DEFINE_MUTEX(connections_lock);
165 static struct kmem_cache *con_cache;
167 static void process_recv_sockets(struct work_struct *work);
168 static void process_send_sockets(struct work_struct *work);
171 /* This is deliberately very simple because most clusters have simple
172 sequential nodeids, so we should be able to go straight to a connection
173 struct in the array */
174 static inline int nodeid_hash(int nodeid)
176 return nodeid & (CONN_HASH_SIZE-1);
179 static struct connection *__find_con(int nodeid)
182 struct connection *con;
184 r = nodeid_hash(nodeid);
186 hlist_for_each_entry(con, &connection_hash[r], list) {
187 if (con->nodeid == nodeid)
194 * If 'allocation' is zero then we don't attempt to create a new
195 * connection structure for this node.
197 static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
199 struct connection *con = NULL;
202 con = __find_con(nodeid);
206 con = kmem_cache_zalloc(con_cache, alloc);
210 r = nodeid_hash(nodeid);
211 hlist_add_head(&con->list, &connection_hash[r]);
213 con->nodeid = nodeid;
214 mutex_init(&con->sock_mutex);
215 INIT_LIST_HEAD(&con->writequeue);
216 spin_lock_init(&con->writequeue_lock);
217 INIT_WORK(&con->swork, process_send_sockets);
218 INIT_WORK(&con->rwork, process_recv_sockets);
220 /* Setup action pointers for child sockets */
222 struct connection *zerocon = __find_con(0);
224 con->connect_action = zerocon->connect_action;
226 con->rx_action = zerocon->rx_action;
232 /* Loop round all connections */
233 static void foreach_conn(void (*conn_func)(struct connection *c))
236 struct hlist_node *n;
237 struct connection *con;
239 for (i = 0; i < CONN_HASH_SIZE; i++) {
240 hlist_for_each_entry_safe(con, 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 connection *con;
262 mutex_lock(&connections_lock);
264 for (i = 0 ; i < CONN_HASH_SIZE; i++) {
265 hlist_for_each_entry(con, &connection_hash[i], list) {
266 if (con->sctp_assoc == assoc_id) {
267 mutex_unlock(&connections_lock);
272 mutex_unlock(&connections_lock);
276 static struct dlm_node_addr *find_node_addr(int nodeid)
278 struct dlm_node_addr *na;
280 list_for_each_entry(na, &dlm_node_addrs, list) {
281 if (na->nodeid == nodeid)
287 static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
289 switch (x->ss_family) {
291 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
292 struct sockaddr_in *siny = (struct sockaddr_in *)y;
293 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
295 if (sinx->sin_port != siny->sin_port)
300 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
301 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
302 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
304 if (sinx->sin6_port != siny->sin6_port)
314 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
315 struct sockaddr *sa_out, bool try_new_addr)
317 struct sockaddr_storage sas;
318 struct dlm_node_addr *na;
320 if (!dlm_local_count)
323 spin_lock(&dlm_node_addrs_spin);
324 na = find_node_addr(nodeid);
325 if (na && na->addr_count) {
327 na->curr_addr_index++;
328 if (na->curr_addr_index == na->addr_count)
329 na->curr_addr_index = 0;
332 memcpy(&sas, na->addr[na->curr_addr_index ],
333 sizeof(struct sockaddr_storage));
335 spin_unlock(&dlm_node_addrs_spin);
344 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
349 if (dlm_local_addr[0]->ss_family == AF_INET) {
350 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
351 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
352 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
354 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
355 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
356 ret6->sin6_addr = in6->sin6_addr;
362 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
364 struct dlm_node_addr *na;
368 spin_lock(&dlm_node_addrs_spin);
369 list_for_each_entry(na, &dlm_node_addrs, list) {
373 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
374 if (addr_compare(na->addr[addr_i], addr)) {
375 *nodeid = na->nodeid;
382 spin_unlock(&dlm_node_addrs_spin);
386 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
388 struct sockaddr_storage *new_addr;
389 struct dlm_node_addr *new_node, *na;
391 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
395 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
401 memcpy(new_addr, addr, len);
403 spin_lock(&dlm_node_addrs_spin);
404 na = find_node_addr(nodeid);
406 new_node->nodeid = nodeid;
407 new_node->addr[0] = new_addr;
408 new_node->addr_count = 1;
409 list_add(&new_node->list, &dlm_node_addrs);
410 spin_unlock(&dlm_node_addrs_spin);
414 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
415 spin_unlock(&dlm_node_addrs_spin);
421 na->addr[na->addr_count++] = new_addr;
422 spin_unlock(&dlm_node_addrs_spin);
427 /* Data available on socket or listen socket received a connect */
428 static void lowcomms_data_ready(struct sock *sk, int count_unused)
430 struct connection *con = sock2con(sk);
431 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
432 queue_work(recv_workqueue, &con->rwork);
435 static void lowcomms_write_space(struct sock *sk)
437 struct connection *con = sock2con(sk);
442 clear_bit(SOCK_NOSPACE, &con->sock->flags);
444 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
445 con->sock->sk->sk_write_pending--;
446 clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags);
449 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
450 queue_work(send_workqueue, &con->swork);
453 static inline void lowcomms_connect_sock(struct connection *con)
455 if (test_bit(CF_CLOSE, &con->flags))
457 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
458 queue_work(send_workqueue, &con->swork);
461 static void lowcomms_state_change(struct sock *sk)
463 if (sk->sk_state == TCP_ESTABLISHED)
464 lowcomms_write_space(sk);
467 int dlm_lowcomms_connect_node(int nodeid)
469 struct connection *con;
471 /* with sctp there's no connecting without sending */
472 if (dlm_config.ci_protocol != 0)
475 if (nodeid == dlm_our_nodeid())
478 con = nodeid2con(nodeid, GFP_NOFS);
481 lowcomms_connect_sock(con);
485 /* Make a socket active */
486 static void add_sock(struct socket *sock, struct connection *con)
490 /* Install a data_ready callback */
491 con->sock->sk->sk_data_ready = lowcomms_data_ready;
492 con->sock->sk->sk_write_space = lowcomms_write_space;
493 con->sock->sk->sk_state_change = lowcomms_state_change;
494 con->sock->sk->sk_user_data = con;
495 con->sock->sk->sk_allocation = GFP_NOFS;
498 /* Add the port number to an IPv6 or 4 sockaddr and return the address
500 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
503 saddr->ss_family = dlm_local_addr[0]->ss_family;
504 if (saddr->ss_family == AF_INET) {
505 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
506 in4_addr->sin_port = cpu_to_be16(port);
507 *addr_len = sizeof(struct sockaddr_in);
508 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
510 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
511 in6_addr->sin6_port = cpu_to_be16(port);
512 *addr_len = sizeof(struct sockaddr_in6);
514 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
517 /* Close a remote connection and tidy up */
518 static void close_connection(struct connection *con, bool and_other)
520 mutex_lock(&con->sock_mutex);
523 sock_release(con->sock);
526 if (con->othercon && and_other) {
527 /* Will only re-enter once. */
528 close_connection(con->othercon, false);
531 __free_page(con->rx_page);
536 mutex_unlock(&con->sock_mutex);
539 /* We only send shutdown messages to nodes that are not part of the cluster */
540 static void sctp_send_shutdown(sctp_assoc_t associd)
542 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
543 struct msghdr outmessage;
544 struct cmsghdr *cmsg;
545 struct sctp_sndrcvinfo *sinfo;
547 struct connection *con;
549 con = nodeid2con(0,0);
552 outmessage.msg_name = NULL;
553 outmessage.msg_namelen = 0;
554 outmessage.msg_control = outcmsg;
555 outmessage.msg_controllen = sizeof(outcmsg);
556 outmessage.msg_flags = MSG_EOR;
558 cmsg = CMSG_FIRSTHDR(&outmessage);
559 cmsg->cmsg_level = IPPROTO_SCTP;
560 cmsg->cmsg_type = SCTP_SNDRCV;
561 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
562 outmessage.msg_controllen = cmsg->cmsg_len;
563 sinfo = CMSG_DATA(cmsg);
564 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
566 sinfo->sinfo_flags |= MSG_EOF;
567 sinfo->sinfo_assoc_id = associd;
569 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
572 log_print("send EOF to node failed: %d", ret);
575 static void sctp_init_failed_foreach(struct connection *con)
579 * Don't try to recover base con and handle race where the
580 * other node's assoc init creates a assoc and we get that
581 * notification, then we get a notification that our attempt
582 * failed due. This happens when we are still trying the primary
583 * address, but the other node has already tried secondary addrs
584 * and found one that worked.
586 if (!con->nodeid || con->sctp_assoc)
589 log_print("Retrying SCTP association init for node %d\n", con->nodeid);
591 con->try_new_addr = true;
593 if (test_and_clear_bit(CF_INIT_PENDING, &con->flags)) {
594 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
595 queue_work(send_workqueue, &con->swork);
599 /* INIT failed but we don't know which node...
600 restart INIT on all pending nodes */
601 static void sctp_init_failed(void)
603 mutex_lock(&connections_lock);
605 foreach_conn(sctp_init_failed_foreach);
607 mutex_unlock(&connections_lock);
610 /* Something happened to an association */
611 static void process_sctp_notification(struct connection *con,
612 struct msghdr *msg, char *buf)
614 union sctp_notification *sn = (union sctp_notification *)buf;
616 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
617 switch (sn->sn_assoc_change.sac_state) {
622 /* Check that the new node is in the lockspace */
623 struct sctp_prim prim;
627 struct connection *new_con;
630 * We get this before any data for an association.
631 * We verify that the node is in the cluster and
632 * then peel off a socket for it.
634 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
635 log_print("COMM_UP for invalid assoc ID %d",
636 (int)sn->sn_assoc_change.sac_assoc_id);
640 memset(&prim, 0, sizeof(struct sctp_prim));
641 prim_len = sizeof(struct sctp_prim);
642 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
644 ret = kernel_getsockopt(con->sock,
650 log_print("getsockopt/sctp_primary_addr on "
651 "new assoc %d failed : %d",
652 (int)sn->sn_assoc_change.sac_assoc_id,
655 /* Retry INIT later */
656 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
658 clear_bit(CF_CONNECT_PENDING, &con->flags);
661 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
662 if (addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
663 unsigned char *b=(unsigned char *)&prim.ssp_addr;
664 log_print("reject connect from unknown addr");
665 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
666 b, sizeof(struct sockaddr_storage));
667 sctp_send_shutdown(prim.ssp_assoc_id);
671 new_con = nodeid2con(nodeid, GFP_NOFS);
675 /* Peel off a new sock */
676 sctp_lock_sock(con->sock->sk);
677 ret = sctp_do_peeloff(con->sock->sk,
678 sn->sn_assoc_change.sac_assoc_id,
680 sctp_release_sock(con->sock->sk);
682 log_print("Can't peel off a socket for "
683 "connection %d to node %d: err=%d",
684 (int)sn->sn_assoc_change.sac_assoc_id,
688 add_sock(new_con->sock, new_con);
690 log_print("connecting to %d sctp association %d",
691 nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
693 new_con->sctp_assoc = sn->sn_assoc_change.sac_assoc_id;
694 new_con->try_new_addr = false;
695 /* Send any pending writes */
696 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
697 clear_bit(CF_INIT_PENDING, &new_con->flags);
698 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
699 queue_work(send_workqueue, &new_con->swork);
701 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
702 queue_work(recv_workqueue, &new_con->rwork);
707 case SCTP_SHUTDOWN_COMP:
709 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
716 /* We don't know which INIT failed, so clear the PENDING flags
717 * on them all. if assoc_id is zero then it will then try
720 case SCTP_CANT_STR_ASSOC:
722 log_print("Can't start SCTP association - retrying");
728 log_print("unexpected SCTP assoc change id=%d state=%d",
729 (int)sn->sn_assoc_change.sac_assoc_id,
730 sn->sn_assoc_change.sac_state);
735 /* Data received from remote end */
736 static int receive_from_sock(struct connection *con)
739 struct msghdr msg = {};
743 int call_again_soon = 0;
745 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
747 mutex_lock(&con->sock_mutex);
749 if (con->sock == NULL) {
754 if (con->rx_page == NULL) {
756 * This doesn't need to be atomic, but I think it should
757 * improve performance if it is.
759 con->rx_page = alloc_page(GFP_ATOMIC);
760 if (con->rx_page == NULL)
762 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
765 /* Only SCTP needs these really */
766 memset(&incmsg, 0, sizeof(incmsg));
767 msg.msg_control = incmsg;
768 msg.msg_controllen = sizeof(incmsg);
771 * iov[0] is the bit of the circular buffer between the current end
772 * point (cb.base + cb.len) and the end of the buffer.
774 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
775 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
780 * iov[1] is the bit of the circular buffer between the start of the
781 * buffer and the start of the currently used section (cb.base)
783 if (cbuf_data(&con->cb) >= con->cb.base) {
784 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
785 iov[1].iov_len = con->cb.base;
786 iov[1].iov_base = page_address(con->rx_page);
789 len = iov[0].iov_len + iov[1].iov_len;
791 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
792 MSG_DONTWAIT | MSG_NOSIGNAL);
796 /* Process SCTP notifications */
797 if (msg.msg_flags & MSG_NOTIFICATION) {
798 msg.msg_control = incmsg;
799 msg.msg_controllen = sizeof(incmsg);
801 process_sctp_notification(con, &msg,
802 page_address(con->rx_page) + con->cb.base);
803 mutex_unlock(&con->sock_mutex);
806 BUG_ON(con->nodeid == 0);
810 cbuf_add(&con->cb, ret);
811 ret = dlm_process_incoming_buffer(con->nodeid,
812 page_address(con->rx_page),
813 con->cb.base, con->cb.len,
815 if (ret == -EBADMSG) {
816 log_print("lowcomms: addr=%p, base=%u, len=%u, "
817 "iov_len=%u, iov_base[0]=%p, read=%d",
818 page_address(con->rx_page), con->cb.base, con->cb.len,
819 len, iov[0].iov_base, r);
823 cbuf_eat(&con->cb, ret);
825 if (cbuf_empty(&con->cb) && !call_again_soon) {
826 __free_page(con->rx_page);
832 mutex_unlock(&con->sock_mutex);
836 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
837 queue_work(recv_workqueue, &con->rwork);
838 mutex_unlock(&con->sock_mutex);
842 mutex_unlock(&con->sock_mutex);
843 if (ret != -EAGAIN) {
844 close_connection(con, false);
845 /* Reconnect when there is something to send */
847 /* Don't return success if we really got EOF */
854 /* Listening socket is busy, accept a connection */
855 static int tcp_accept_from_sock(struct connection *con)
858 struct sockaddr_storage peeraddr;
859 struct socket *newsock;
862 struct connection *newcon;
863 struct connection *addcon;
865 mutex_lock(&connections_lock);
866 if (!dlm_allow_conn) {
867 mutex_unlock(&connections_lock);
870 mutex_unlock(&connections_lock);
872 memset(&peeraddr, 0, sizeof(peeraddr));
873 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
874 IPPROTO_TCP, &newsock);
878 mutex_lock_nested(&con->sock_mutex, 0);
881 if (con->sock == NULL)
884 newsock->type = con->sock->type;
885 newsock->ops = con->sock->ops;
887 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
891 /* Get the connected socket's peer */
892 memset(&peeraddr, 0, sizeof(peeraddr));
893 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
895 result = -ECONNABORTED;
899 /* Get the new node's NODEID */
900 make_sockaddr(&peeraddr, 0, &len);
901 if (addr_to_nodeid(&peeraddr, &nodeid)) {
902 unsigned char *b=(unsigned char *)&peeraddr;
903 log_print("connect from non cluster node");
904 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
905 b, sizeof(struct sockaddr_storage));
906 sock_release(newsock);
907 mutex_unlock(&con->sock_mutex);
911 log_print("got connection from %d", nodeid);
913 /* Check to see if we already have a connection to this node. This
914 * could happen if the two nodes initiate a connection at roughly
915 * the same time and the connections cross on the wire.
916 * In this case we store the incoming one in "othercon"
918 newcon = nodeid2con(nodeid, GFP_NOFS);
923 mutex_lock_nested(&newcon->sock_mutex, 1);
925 struct connection *othercon = newcon->othercon;
928 othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
930 log_print("failed to allocate incoming socket");
931 mutex_unlock(&newcon->sock_mutex);
935 othercon->nodeid = nodeid;
936 othercon->rx_action = receive_from_sock;
937 mutex_init(&othercon->sock_mutex);
938 INIT_WORK(&othercon->swork, process_send_sockets);
939 INIT_WORK(&othercon->rwork, process_recv_sockets);
940 set_bit(CF_IS_OTHERCON, &othercon->flags);
942 if (!othercon->sock) {
943 newcon->othercon = othercon;
944 othercon->sock = newsock;
945 newsock->sk->sk_user_data = othercon;
946 add_sock(newsock, othercon);
950 printk("Extra connection from node %d attempted\n", nodeid);
952 mutex_unlock(&newcon->sock_mutex);
957 newsock->sk->sk_user_data = newcon;
958 newcon->rx_action = receive_from_sock;
959 add_sock(newsock, newcon);
963 mutex_unlock(&newcon->sock_mutex);
966 * Add it to the active queue in case we got data
967 * between processing the accept adding the socket
968 * to the read_sockets list
970 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
971 queue_work(recv_workqueue, &addcon->rwork);
972 mutex_unlock(&con->sock_mutex);
977 mutex_unlock(&con->sock_mutex);
978 sock_release(newsock);
980 if (result != -EAGAIN)
981 log_print("error accepting connection from node: %d", result);
985 static void free_entry(struct writequeue_entry *e)
987 __free_page(e->page);
991 /* Initiate an SCTP association.
992 This is a special case of send_to_sock() in that we don't yet have a
993 peeled-off socket for this association, so we use the listening socket
994 and add the primary IP address of the remote node.
996 static void sctp_init_assoc(struct connection *con)
998 struct sockaddr_storage rem_addr;
999 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1000 struct msghdr outmessage;
1001 struct cmsghdr *cmsg;
1002 struct sctp_sndrcvinfo *sinfo;
1003 struct connection *base_con;
1004 struct writequeue_entry *e;
1010 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
1013 if (con->retries++ > MAX_CONNECT_RETRIES)
1016 if (nodeid_to_addr(con->nodeid, NULL, (struct sockaddr *)&rem_addr,
1017 con->try_new_addr)) {
1018 log_print("no address for nodeid %d", con->nodeid);
1021 base_con = nodeid2con(0, 0);
1022 BUG_ON(base_con == NULL);
1024 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
1026 outmessage.msg_name = &rem_addr;
1027 outmessage.msg_namelen = addrlen;
1028 outmessage.msg_control = outcmsg;
1029 outmessage.msg_controllen = sizeof(outcmsg);
1030 outmessage.msg_flags = MSG_EOR;
1032 spin_lock(&con->writequeue_lock);
1034 if (list_empty(&con->writequeue)) {
1035 spin_unlock(&con->writequeue_lock);
1036 log_print("writequeue empty for nodeid %d", con->nodeid);
1040 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
1043 spin_unlock(&con->writequeue_lock);
1045 /* Send the first block off the write queue */
1046 iov[0].iov_base = page_address(e->page)+offset;
1047 iov[0].iov_len = len;
1049 if (rem_addr.ss_family == AF_INET) {
1050 struct sockaddr_in *sin = (struct sockaddr_in *)&rem_addr;
1051 log_print("Trying to connect to %pI4", &sin->sin_addr.s_addr);
1053 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&rem_addr;
1054 log_print("Trying to connect to %pI6", &sin6->sin6_addr);
1057 cmsg = CMSG_FIRSTHDR(&outmessage);
1058 cmsg->cmsg_level = IPPROTO_SCTP;
1059 cmsg->cmsg_type = SCTP_SNDRCV;
1060 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1061 sinfo = CMSG_DATA(cmsg);
1062 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
1063 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
1064 outmessage.msg_controllen = cmsg->cmsg_len;
1065 sinfo->sinfo_flags |= SCTP_ADDR_OVER;
1067 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
1069 log_print("Send first packet to node %d failed: %d",
1072 /* Try again later */
1073 clear_bit(CF_CONNECT_PENDING, &con->flags);
1074 clear_bit(CF_INIT_PENDING, &con->flags);
1077 spin_lock(&con->writequeue_lock);
1081 if (e->len == 0 && e->users == 0) {
1085 spin_unlock(&con->writequeue_lock);
1089 /* Connect a new socket to its peer */
1090 static void tcp_connect_to_sock(struct connection *con)
1092 struct sockaddr_storage saddr, src_addr;
1094 struct socket *sock = NULL;
1098 if (con->nodeid == 0) {
1099 log_print("attempt to connect sock 0 foiled");
1103 mutex_lock(&con->sock_mutex);
1104 if (con->retries++ > MAX_CONNECT_RETRIES)
1107 /* Some odd races can cause double-connects, ignore them */
1111 /* Create a socket to communicate with */
1112 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1113 IPPROTO_TCP, &sock);
1117 memset(&saddr, 0, sizeof(saddr));
1118 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
1120 log_print("no address for nodeid %d", con->nodeid);
1124 sock->sk->sk_user_data = con;
1125 con->rx_action = receive_from_sock;
1126 con->connect_action = tcp_connect_to_sock;
1127 add_sock(sock, con);
1129 /* Bind to our cluster-known address connecting to avoid
1131 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1132 make_sockaddr(&src_addr, 0, &addr_len);
1133 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1136 log_print("could not bind for connect: %d", result);
1137 /* This *may* not indicate a critical error */
1140 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1142 log_print("connecting to %d", con->nodeid);
1144 /* Turn off Nagle's algorithm */
1145 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1148 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1150 if (result == -EINPROGRESS)
1157 sock_release(con->sock);
1163 * Some errors are fatal and this list might need adjusting. For other
1164 * errors we try again until the max number of retries is reached.
1166 if (result != -EHOSTUNREACH &&
1167 result != -ENETUNREACH &&
1168 result != -ENETDOWN &&
1169 result != -EINVAL &&
1170 result != -EPROTONOSUPPORT) {
1171 log_print("connect %d try %d error %d", con->nodeid,
1172 con->retries, result);
1173 mutex_unlock(&con->sock_mutex);
1175 lowcomms_connect_sock(con);
1179 mutex_unlock(&con->sock_mutex);
1183 static struct socket *tcp_create_listen_sock(struct connection *con,
1184 struct sockaddr_storage *saddr)
1186 struct socket *sock = NULL;
1191 if (dlm_local_addr[0]->ss_family == AF_INET)
1192 addr_len = sizeof(struct sockaddr_in);
1194 addr_len = sizeof(struct sockaddr_in6);
1196 /* Create a socket to communicate with */
1197 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1198 IPPROTO_TCP, &sock);
1200 log_print("Can't create listening comms socket");
1204 /* Turn off Nagle's algorithm */
1205 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1208 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1209 (char *)&one, sizeof(one));
1212 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
1214 con->rx_action = tcp_accept_from_sock;
1215 con->connect_action = tcp_connect_to_sock;
1217 /* Bind to our port */
1218 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1219 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1221 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1227 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1228 (char *)&one, sizeof(one));
1230 log_print("Set keepalive failed: %d", result);
1233 result = sock->ops->listen(sock, 5);
1235 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1245 /* Get local addresses */
1246 static void init_local(void)
1248 struct sockaddr_storage sas, *addr;
1251 dlm_local_count = 0;
1252 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1253 if (dlm_our_addr(&sas, i))
1256 addr = kmalloc(sizeof(*addr), GFP_NOFS);
1259 memcpy(addr, &sas, sizeof(*addr));
1260 dlm_local_addr[dlm_local_count++] = addr;
1264 /* Bind to an IP address. SCTP allows multiple address so it can do
1266 static int add_sctp_bind_addr(struct connection *sctp_con,
1267 struct sockaddr_storage *addr,
1268 int addr_len, int num)
1273 result = kernel_bind(sctp_con->sock,
1274 (struct sockaddr *) addr,
1277 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1278 SCTP_SOCKOPT_BINDX_ADD,
1279 (char *)addr, addr_len);
1282 log_print("Can't bind to port %d addr number %d",
1283 dlm_config.ci_tcp_port, num);
1288 /* Initialise SCTP socket and bind to all interfaces */
1289 static int sctp_listen_for_all(void)
1291 struct socket *sock = NULL;
1292 struct sockaddr_storage localaddr;
1293 struct sctp_event_subscribe subscribe;
1294 int result = -EINVAL, num = 1, i, addr_len;
1295 struct connection *con = nodeid2con(0, GFP_NOFS);
1296 int bufsize = NEEDED_RMEM;
1301 log_print("Using SCTP for communications");
1303 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1304 IPPROTO_SCTP, &sock);
1306 log_print("Can't create comms socket, check SCTP is loaded");
1310 /* Listen for events */
1311 memset(&subscribe, 0, sizeof(subscribe));
1312 subscribe.sctp_data_io_event = 1;
1313 subscribe.sctp_association_event = 1;
1314 subscribe.sctp_send_failure_event = 1;
1315 subscribe.sctp_shutdown_event = 1;
1316 subscribe.sctp_partial_delivery_event = 1;
1318 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
1319 (char *)&bufsize, sizeof(bufsize));
1321 log_print("Error increasing buffer space on socket %d", result);
1323 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
1324 (char *)&subscribe, sizeof(subscribe));
1326 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1328 goto create_delsock;
1331 /* Init con struct */
1332 sock->sk->sk_user_data = con;
1334 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1335 con->rx_action = receive_from_sock;
1336 con->connect_action = sctp_init_assoc;
1338 /* Bind to all interfaces. */
1339 for (i = 0; i < dlm_local_count; i++) {
1340 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1341 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1343 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1345 goto create_delsock;
1349 result = sock->ops->listen(sock, 5);
1351 log_print("Can't set socket listening");
1352 goto create_delsock;
1364 static int tcp_listen_for_all(void)
1366 struct socket *sock = NULL;
1367 struct connection *con = nodeid2con(0, GFP_NOFS);
1368 int result = -EINVAL;
1373 /* We don't support multi-homed hosts */
1374 if (dlm_local_addr[1] != NULL) {
1375 log_print("TCP protocol can't handle multi-homed hosts, "
1380 log_print("Using TCP for communications");
1382 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1384 add_sock(sock, con);
1388 result = -EADDRINUSE;
1396 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1399 struct writequeue_entry *entry;
1401 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1405 entry->page = alloc_page(allocation);
1420 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1422 struct connection *con;
1423 struct writequeue_entry *e;
1426 con = nodeid2con(nodeid, allocation);
1430 spin_lock(&con->writequeue_lock);
1431 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1432 if ((&e->list == &con->writequeue) ||
1433 (PAGE_CACHE_SIZE - e->end < len)) {
1440 spin_unlock(&con->writequeue_lock);
1444 *ppc = page_address(e->page) + offset;
1448 e = new_writequeue_entry(con, allocation);
1450 spin_lock(&con->writequeue_lock);
1454 list_add_tail(&e->list, &con->writequeue);
1455 spin_unlock(&con->writequeue_lock);
1461 void dlm_lowcomms_commit_buffer(void *mh)
1463 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1464 struct connection *con = e->con;
1467 spin_lock(&con->writequeue_lock);
1471 e->len = e->end - e->offset;
1472 spin_unlock(&con->writequeue_lock);
1474 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1475 queue_work(send_workqueue, &con->swork);
1480 spin_unlock(&con->writequeue_lock);
1484 /* Send a message */
1485 static void send_to_sock(struct connection *con)
1488 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1489 struct writequeue_entry *e;
1493 mutex_lock(&con->sock_mutex);
1494 if (con->sock == NULL)
1497 spin_lock(&con->writequeue_lock);
1499 e = list_entry(con->writequeue.next, struct writequeue_entry,
1501 if ((struct list_head *) e == &con->writequeue)
1506 BUG_ON(len == 0 && e->users == 0);
1507 spin_unlock(&con->writequeue_lock);
1511 ret = kernel_sendpage(con->sock, e->page, offset, len,
1513 if (ret == -EAGAIN || ret == 0) {
1514 if (ret == -EAGAIN &&
1515 test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
1516 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1517 /* Notify TCP that we're limited by the
1518 * application window size.
1520 set_bit(SOCK_NOSPACE, &con->sock->flags);
1521 con->sock->sk->sk_write_pending++;
1529 /* Don't starve people filling buffers */
1530 if (++count >= MAX_SEND_MSG_COUNT) {
1535 spin_lock(&con->writequeue_lock);
1539 if (e->len == 0 && e->users == 0) {
1544 spin_unlock(&con->writequeue_lock);
1546 mutex_unlock(&con->sock_mutex);
1550 mutex_unlock(&con->sock_mutex);
1551 close_connection(con, false);
1552 lowcomms_connect_sock(con);
1556 mutex_unlock(&con->sock_mutex);
1557 if (!test_bit(CF_INIT_PENDING, &con->flags))
1558 lowcomms_connect_sock(con);
1561 static void clean_one_writequeue(struct connection *con)
1563 struct writequeue_entry *e, *safe;
1565 spin_lock(&con->writequeue_lock);
1566 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1570 spin_unlock(&con->writequeue_lock);
1573 /* Called from recovery when it knows that a node has
1575 int dlm_lowcomms_close(int nodeid)
1577 struct connection *con;
1578 struct dlm_node_addr *na;
1580 log_print("closing connection to node %d", nodeid);
1581 con = nodeid2con(nodeid, 0);
1583 clear_bit(CF_CONNECT_PENDING, &con->flags);
1584 clear_bit(CF_WRITE_PENDING, &con->flags);
1585 set_bit(CF_CLOSE, &con->flags);
1586 if (cancel_work_sync(&con->swork))
1587 log_print("canceled swork for node %d", nodeid);
1588 if (cancel_work_sync(&con->rwork))
1589 log_print("canceled rwork for node %d", nodeid);
1590 clean_one_writequeue(con);
1591 close_connection(con, true);
1594 spin_lock(&dlm_node_addrs_spin);
1595 na = find_node_addr(nodeid);
1597 list_del(&na->list);
1598 while (na->addr_count--)
1599 kfree(na->addr[na->addr_count]);
1602 spin_unlock(&dlm_node_addrs_spin);
1607 /* Receive workqueue function */
1608 static void process_recv_sockets(struct work_struct *work)
1610 struct connection *con = container_of(work, struct connection, rwork);
1613 clear_bit(CF_READ_PENDING, &con->flags);
1615 err = con->rx_action(con);
1619 /* Send workqueue function */
1620 static void process_send_sockets(struct work_struct *work)
1622 struct connection *con = container_of(work, struct connection, swork);
1624 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1625 con->connect_action(con);
1626 set_bit(CF_WRITE_PENDING, &con->flags);
1628 if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
1633 /* Discard all entries on the write queues */
1634 static void clean_writequeues(void)
1636 foreach_conn(clean_one_writequeue);
1639 static void work_stop(void)
1641 destroy_workqueue(recv_workqueue);
1642 destroy_workqueue(send_workqueue);
1645 static int work_start(void)
1647 recv_workqueue = alloc_workqueue("dlm_recv",
1648 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1649 if (!recv_workqueue) {
1650 log_print("can't start dlm_recv");
1654 send_workqueue = alloc_workqueue("dlm_send",
1655 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1656 if (!send_workqueue) {
1657 log_print("can't start dlm_send");
1658 destroy_workqueue(recv_workqueue);
1665 static void stop_conn(struct connection *con)
1668 if (con->sock && con->sock->sk)
1669 con->sock->sk->sk_user_data = NULL;
1672 static void free_conn(struct connection *con)
1674 close_connection(con, true);
1676 kmem_cache_free(con_cache, con->othercon);
1677 hlist_del(&con->list);
1678 kmem_cache_free(con_cache, con);
1681 void dlm_lowcomms_stop(void)
1683 /* Set all the flags to prevent any
1686 mutex_lock(&connections_lock);
1688 foreach_conn(stop_conn);
1689 mutex_unlock(&connections_lock);
1693 mutex_lock(&connections_lock);
1694 clean_writequeues();
1696 foreach_conn(free_conn);
1698 mutex_unlock(&connections_lock);
1699 kmem_cache_destroy(con_cache);
1702 int dlm_lowcomms_start(void)
1704 int error = -EINVAL;
1705 struct connection *con;
1708 for (i = 0; i < CONN_HASH_SIZE; i++)
1709 INIT_HLIST_HEAD(&connection_hash[i]);
1712 if (!dlm_local_count) {
1714 log_print("no local IP address has been set");
1719 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1720 __alignof__(struct connection), 0,
1725 error = work_start();
1731 /* Start listening */
1732 if (dlm_config.ci_protocol == 0)
1733 error = tcp_listen_for_all();
1735 error = sctp_listen_for_all();
1743 con = nodeid2con(0,0);
1745 close_connection(con, false);
1746 kmem_cache_free(con_cache, con);
1749 kmem_cache_destroy(con_cache);
1754 void dlm_lowcomms_exit(void)
1756 struct dlm_node_addr *na, *safe;
1758 spin_lock(&dlm_node_addrs_spin);
1759 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1760 list_del(&na->list);
1761 while (na->addr_count--)
1762 kfree(na->addr[na->addr_count]);
1765 spin_unlock(&dlm_node_addrs_spin);