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/user.h>
58 #include "dlm_internal.h"
63 #define NEEDED_RMEM (4*1024*1024)
64 #define CONN_HASH_SIZE 32
72 static void cbuf_add(struct cbuf *cb, int n)
77 static int cbuf_data(struct cbuf *cb)
79 return ((cb->base + cb->len) & cb->mask);
82 static void cbuf_init(struct cbuf *cb, int size)
84 cb->base = cb->len = 0;
88 static void cbuf_eat(struct cbuf *cb, int n)
95 static bool cbuf_empty(struct cbuf *cb)
101 struct socket *sock; /* NULL if not connected */
102 uint32_t nodeid; /* So we know who we are in the list */
103 struct mutex sock_mutex;
105 #define CF_READ_PENDING 1
106 #define CF_WRITE_PENDING 2
107 #define CF_CONNECT_PENDING 3
108 #define CF_INIT_PENDING 4
109 #define CF_IS_OTHERCON 5
111 struct list_head writequeue; /* List of outgoing writequeue_entries */
112 spinlock_t writequeue_lock;
113 int (*rx_action) (struct connection *); /* What to do when active */
114 void (*connect_action) (struct connection *); /* What to do to connect */
115 struct page *rx_page;
118 #define MAX_CONNECT_RETRIES 3
120 struct hlist_node list;
121 struct connection *othercon;
122 struct work_struct rwork; /* Receive workqueue */
123 struct work_struct swork; /* Send workqueue */
125 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
127 /* An entry waiting to be sent */
128 struct writequeue_entry {
129 struct list_head list;
135 struct connection *con;
138 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
139 static int dlm_local_count;
142 static struct workqueue_struct *recv_workqueue;
143 static struct workqueue_struct *send_workqueue;
145 static struct hlist_head connection_hash[CONN_HASH_SIZE];
146 static DEFINE_MUTEX(connections_lock);
147 static struct kmem_cache *con_cache;
149 static void process_recv_sockets(struct work_struct *work);
150 static void process_send_sockets(struct work_struct *work);
153 /* This is deliberately very simple because most clusters have simple
154 sequential nodeids, so we should be able to go straight to a connection
155 struct in the array */
156 static inline int nodeid_hash(int nodeid)
158 return nodeid & (CONN_HASH_SIZE-1);
161 static struct connection *__find_con(int nodeid)
164 struct hlist_node *h;
165 struct connection *con;
167 r = nodeid_hash(nodeid);
169 hlist_for_each_entry(con, h, &connection_hash[r], list) {
170 if (con->nodeid == nodeid)
177 * If 'allocation' is zero then we don't attempt to create a new
178 * connection structure for this node.
180 static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
182 struct connection *con = NULL;
185 con = __find_con(nodeid);
189 con = kmem_cache_zalloc(con_cache, alloc);
193 r = nodeid_hash(nodeid);
194 hlist_add_head(&con->list, &connection_hash[r]);
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);
203 /* Setup action pointers for child sockets */
205 struct connection *zerocon = __find_con(0);
207 con->connect_action = zerocon->connect_action;
209 con->rx_action = zerocon->rx_action;
215 /* Loop round all connections */
216 static void foreach_conn(void (*conn_func)(struct connection *c))
219 struct hlist_node *h, *n;
220 struct connection *con;
222 for (i = 0; i < CONN_HASH_SIZE; i++) {
223 hlist_for_each_entry_safe(con, h, n, &connection_hash[i], list){
229 static struct connection *nodeid2con(int nodeid, gfp_t allocation)
231 struct connection *con;
233 mutex_lock(&connections_lock);
234 con = __nodeid2con(nodeid, allocation);
235 mutex_unlock(&connections_lock);
240 /* This is a bit drastic, but only called when things go wrong */
241 static struct connection *assoc2con(int assoc_id)
244 struct hlist_node *h;
245 struct connection *con;
247 mutex_lock(&connections_lock);
249 for (i = 0 ; i < CONN_HASH_SIZE; i++) {
250 hlist_for_each_entry(con, h, &connection_hash[i], list) {
251 if (con && con->sctp_assoc == assoc_id) {
252 mutex_unlock(&connections_lock);
257 mutex_unlock(&connections_lock);
261 static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
263 struct sockaddr_storage addr;
266 if (!dlm_local_count)
269 error = dlm_nodeid_to_addr(nodeid, &addr);
273 if (dlm_local_addr[0]->ss_family == AF_INET) {
274 struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
275 struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
276 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
278 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
279 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
280 ipv6_addr_copy(&ret6->sin6_addr, &in6->sin6_addr);
286 /* Data available on socket or listen socket received a connect */
287 static void lowcomms_data_ready(struct sock *sk, int count_unused)
289 struct connection *con = sock2con(sk);
290 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
291 queue_work(recv_workqueue, &con->rwork);
294 static void lowcomms_write_space(struct sock *sk)
296 struct connection *con = sock2con(sk);
298 if (con && !test_and_set_bit(CF_WRITE_PENDING, &con->flags))
299 queue_work(send_workqueue, &con->swork);
302 static inline void lowcomms_connect_sock(struct connection *con)
304 if (test_bit(CF_CLOSE, &con->flags))
306 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
307 queue_work(send_workqueue, &con->swork);
310 static void lowcomms_state_change(struct sock *sk)
312 if (sk->sk_state == TCP_ESTABLISHED)
313 lowcomms_write_space(sk);
316 int dlm_lowcomms_connect_node(int nodeid)
318 struct connection *con;
320 /* with sctp there's no connecting without sending */
321 if (dlm_config.ci_protocol != 0)
324 if (nodeid == dlm_our_nodeid())
327 con = nodeid2con(nodeid, GFP_NOFS);
330 lowcomms_connect_sock(con);
334 /* Make a socket active */
335 static int add_sock(struct socket *sock, struct connection *con)
339 /* Install a data_ready callback */
340 con->sock->sk->sk_data_ready = lowcomms_data_ready;
341 con->sock->sk->sk_write_space = lowcomms_write_space;
342 con->sock->sk->sk_state_change = lowcomms_state_change;
343 con->sock->sk->sk_user_data = con;
344 con->sock->sk->sk_allocation = GFP_NOFS;
348 /* Add the port number to an IPv6 or 4 sockaddr and return the address
350 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
353 saddr->ss_family = dlm_local_addr[0]->ss_family;
354 if (saddr->ss_family == AF_INET) {
355 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
356 in4_addr->sin_port = cpu_to_be16(port);
357 *addr_len = sizeof(struct sockaddr_in);
358 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
360 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
361 in6_addr->sin6_port = cpu_to_be16(port);
362 *addr_len = sizeof(struct sockaddr_in6);
364 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
367 /* Close a remote connection and tidy up */
368 static void close_connection(struct connection *con, bool and_other)
370 mutex_lock(&con->sock_mutex);
373 sock_release(con->sock);
376 if (con->othercon && and_other) {
377 /* Will only re-enter once. */
378 close_connection(con->othercon, false);
381 __free_page(con->rx_page);
386 mutex_unlock(&con->sock_mutex);
389 /* We only send shutdown messages to nodes that are not part of the cluster */
390 static void sctp_send_shutdown(sctp_assoc_t associd)
392 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
393 struct msghdr outmessage;
394 struct cmsghdr *cmsg;
395 struct sctp_sndrcvinfo *sinfo;
397 struct connection *con;
399 con = nodeid2con(0,0);
402 outmessage.msg_name = NULL;
403 outmessage.msg_namelen = 0;
404 outmessage.msg_control = outcmsg;
405 outmessage.msg_controllen = sizeof(outcmsg);
406 outmessage.msg_flags = MSG_EOR;
408 cmsg = CMSG_FIRSTHDR(&outmessage);
409 cmsg->cmsg_level = IPPROTO_SCTP;
410 cmsg->cmsg_type = SCTP_SNDRCV;
411 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
412 outmessage.msg_controllen = cmsg->cmsg_len;
413 sinfo = CMSG_DATA(cmsg);
414 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
416 sinfo->sinfo_flags |= MSG_EOF;
417 sinfo->sinfo_assoc_id = associd;
419 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
422 log_print("send EOF to node failed: %d", ret);
425 static void sctp_init_failed_foreach(struct connection *con)
428 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
429 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
430 queue_work(send_workqueue, &con->swork);
434 /* INIT failed but we don't know which node...
435 restart INIT on all pending nodes */
436 static void sctp_init_failed(void)
438 mutex_lock(&connections_lock);
440 foreach_conn(sctp_init_failed_foreach);
442 mutex_unlock(&connections_lock);
445 /* Something happened to an association */
446 static void process_sctp_notification(struct connection *con,
447 struct msghdr *msg, char *buf)
449 union sctp_notification *sn = (union sctp_notification *)buf;
451 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
452 switch (sn->sn_assoc_change.sac_state) {
457 /* Check that the new node is in the lockspace */
458 struct sctp_prim prim;
462 struct connection *new_con;
463 sctp_peeloff_arg_t parg;
464 int parglen = sizeof(parg);
468 * We get this before any data for an association.
469 * We verify that the node is in the cluster and
470 * then peel off a socket for it.
472 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
473 log_print("COMM_UP for invalid assoc ID %d",
474 (int)sn->sn_assoc_change.sac_assoc_id);
478 memset(&prim, 0, sizeof(struct sctp_prim));
479 prim_len = sizeof(struct sctp_prim);
480 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
482 ret = kernel_getsockopt(con->sock,
488 log_print("getsockopt/sctp_primary_addr on "
489 "new assoc %d failed : %d",
490 (int)sn->sn_assoc_change.sac_assoc_id,
493 /* Retry INIT later */
494 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
496 clear_bit(CF_CONNECT_PENDING, &con->flags);
499 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
500 if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
502 unsigned char *b=(unsigned char *)&prim.ssp_addr;
503 log_print("reject connect from unknown addr");
504 for (i=0; i<sizeof(struct sockaddr_storage);i++)
505 printk("%02x ", b[i]);
507 sctp_send_shutdown(prim.ssp_assoc_id);
511 new_con = nodeid2con(nodeid, GFP_NOFS);
515 /* Peel off a new sock */
516 parg.associd = sn->sn_assoc_change.sac_assoc_id;
517 ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
518 SCTP_SOCKOPT_PEELOFF,
519 (void *)&parg, &parglen);
521 log_print("Can't peel off a socket for "
522 "connection %d to node %d: err=%d",
523 parg.associd, nodeid, ret);
526 new_con->sock = sockfd_lookup(parg.sd, &err);
527 if (!new_con->sock) {
528 log_print("sockfd_lookup error %d", err);
531 add_sock(new_con->sock, new_con);
532 sockfd_put(new_con->sock);
534 log_print("connecting to %d sctp association %d",
535 nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
537 /* Send any pending writes */
538 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
539 clear_bit(CF_INIT_PENDING, &con->flags);
540 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
541 queue_work(send_workqueue, &new_con->swork);
543 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
544 queue_work(recv_workqueue, &new_con->rwork);
549 case SCTP_SHUTDOWN_COMP:
551 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
558 /* We don't know which INIT failed, so clear the PENDING flags
559 * on them all. if assoc_id is zero then it will then try
562 case SCTP_CANT_STR_ASSOC:
564 log_print("Can't start SCTP association - retrying");
570 log_print("unexpected SCTP assoc change id=%d state=%d",
571 (int)sn->sn_assoc_change.sac_assoc_id,
572 sn->sn_assoc_change.sac_state);
577 /* Data received from remote end */
578 static int receive_from_sock(struct connection *con)
581 struct msghdr msg = {};
585 int call_again_soon = 0;
587 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
589 mutex_lock(&con->sock_mutex);
591 if (con->sock == NULL) {
596 if (con->rx_page == NULL) {
598 * This doesn't need to be atomic, but I think it should
599 * improve performance if it is.
601 con->rx_page = alloc_page(GFP_ATOMIC);
602 if (con->rx_page == NULL)
604 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
607 /* Only SCTP needs these really */
608 memset(&incmsg, 0, sizeof(incmsg));
609 msg.msg_control = incmsg;
610 msg.msg_controllen = sizeof(incmsg);
613 * iov[0] is the bit of the circular buffer between the current end
614 * point (cb.base + cb.len) and the end of the buffer.
616 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
617 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
622 * iov[1] is the bit of the circular buffer between the start of the
623 * buffer and the start of the currently used section (cb.base)
625 if (cbuf_data(&con->cb) >= con->cb.base) {
626 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
627 iov[1].iov_len = con->cb.base;
628 iov[1].iov_base = page_address(con->rx_page);
631 len = iov[0].iov_len + iov[1].iov_len;
633 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
634 MSG_DONTWAIT | MSG_NOSIGNAL);
638 /* Process SCTP notifications */
639 if (msg.msg_flags & MSG_NOTIFICATION) {
640 msg.msg_control = incmsg;
641 msg.msg_controllen = sizeof(incmsg);
643 process_sctp_notification(con, &msg,
644 page_address(con->rx_page) + con->cb.base);
645 mutex_unlock(&con->sock_mutex);
648 BUG_ON(con->nodeid == 0);
652 cbuf_add(&con->cb, ret);
653 ret = dlm_process_incoming_buffer(con->nodeid,
654 page_address(con->rx_page),
655 con->cb.base, con->cb.len,
657 if (ret == -EBADMSG) {
658 log_print("lowcomms: addr=%p, base=%u, len=%u, "
659 "iov_len=%u, iov_base[0]=%p, read=%d",
660 page_address(con->rx_page), con->cb.base, con->cb.len,
661 len, iov[0].iov_base, r);
665 cbuf_eat(&con->cb, ret);
667 if (cbuf_empty(&con->cb) && !call_again_soon) {
668 __free_page(con->rx_page);
674 mutex_unlock(&con->sock_mutex);
678 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
679 queue_work(recv_workqueue, &con->rwork);
680 mutex_unlock(&con->sock_mutex);
684 mutex_unlock(&con->sock_mutex);
685 if (ret != -EAGAIN) {
686 close_connection(con, false);
687 /* Reconnect when there is something to send */
689 /* Don't return success if we really got EOF */
696 /* Listening socket is busy, accept a connection */
697 static int tcp_accept_from_sock(struct connection *con)
700 struct sockaddr_storage peeraddr;
701 struct socket *newsock;
704 struct connection *newcon;
705 struct connection *addcon;
707 memset(&peeraddr, 0, sizeof(peeraddr));
708 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
709 IPPROTO_TCP, &newsock);
713 mutex_lock_nested(&con->sock_mutex, 0);
716 if (con->sock == NULL)
719 newsock->type = con->sock->type;
720 newsock->ops = con->sock->ops;
722 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
726 /* Get the connected socket's peer */
727 memset(&peeraddr, 0, sizeof(peeraddr));
728 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
730 result = -ECONNABORTED;
734 /* Get the new node's NODEID */
735 make_sockaddr(&peeraddr, 0, &len);
736 if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
737 log_print("connect from non cluster node");
738 sock_release(newsock);
739 mutex_unlock(&con->sock_mutex);
743 log_print("got connection from %d", nodeid);
745 /* Check to see if we already have a connection to this node. This
746 * could happen if the two nodes initiate a connection at roughly
747 * the same time and the connections cross on the wire.
748 * In this case we store the incoming one in "othercon"
750 newcon = nodeid2con(nodeid, GFP_NOFS);
755 mutex_lock_nested(&newcon->sock_mutex, 1);
757 struct connection *othercon = newcon->othercon;
760 othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
762 log_print("failed to allocate incoming socket");
763 mutex_unlock(&newcon->sock_mutex);
767 othercon->nodeid = nodeid;
768 othercon->rx_action = receive_from_sock;
769 mutex_init(&othercon->sock_mutex);
770 INIT_WORK(&othercon->swork, process_send_sockets);
771 INIT_WORK(&othercon->rwork, process_recv_sockets);
772 set_bit(CF_IS_OTHERCON, &othercon->flags);
774 if (!othercon->sock) {
775 newcon->othercon = othercon;
776 othercon->sock = newsock;
777 newsock->sk->sk_user_data = othercon;
778 add_sock(newsock, othercon);
782 printk("Extra connection from node %d attempted\n", nodeid);
784 mutex_unlock(&newcon->sock_mutex);
789 newsock->sk->sk_user_data = newcon;
790 newcon->rx_action = receive_from_sock;
791 add_sock(newsock, newcon);
795 mutex_unlock(&newcon->sock_mutex);
798 * Add it to the active queue in case we got data
799 * beween processing the accept adding the socket
800 * to the read_sockets list
802 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
803 queue_work(recv_workqueue, &addcon->rwork);
804 mutex_unlock(&con->sock_mutex);
809 mutex_unlock(&con->sock_mutex);
810 sock_release(newsock);
812 if (result != -EAGAIN)
813 log_print("error accepting connection from node: %d", result);
817 static void free_entry(struct writequeue_entry *e)
819 __free_page(e->page);
823 /* Initiate an SCTP association.
824 This is a special case of send_to_sock() in that we don't yet have a
825 peeled-off socket for this association, so we use the listening socket
826 and add the primary IP address of the remote node.
828 static void sctp_init_assoc(struct connection *con)
830 struct sockaddr_storage rem_addr;
831 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
832 struct msghdr outmessage;
833 struct cmsghdr *cmsg;
834 struct sctp_sndrcvinfo *sinfo;
835 struct connection *base_con;
836 struct writequeue_entry *e;
842 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
845 if (con->retries++ > MAX_CONNECT_RETRIES)
848 if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
849 log_print("no address for nodeid %d", con->nodeid);
852 base_con = nodeid2con(0, 0);
853 BUG_ON(base_con == NULL);
855 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
857 outmessage.msg_name = &rem_addr;
858 outmessage.msg_namelen = addrlen;
859 outmessage.msg_control = outcmsg;
860 outmessage.msg_controllen = sizeof(outcmsg);
861 outmessage.msg_flags = MSG_EOR;
863 spin_lock(&con->writequeue_lock);
865 if (list_empty(&con->writequeue)) {
866 spin_unlock(&con->writequeue_lock);
867 log_print("writequeue empty for nodeid %d", con->nodeid);
871 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
874 spin_unlock(&con->writequeue_lock);
876 /* Send the first block off the write queue */
877 iov[0].iov_base = page_address(e->page)+offset;
878 iov[0].iov_len = len;
880 cmsg = CMSG_FIRSTHDR(&outmessage);
881 cmsg->cmsg_level = IPPROTO_SCTP;
882 cmsg->cmsg_type = SCTP_SNDRCV;
883 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
884 sinfo = CMSG_DATA(cmsg);
885 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
886 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
887 outmessage.msg_controllen = cmsg->cmsg_len;
889 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
891 log_print("Send first packet to node %d failed: %d",
894 /* Try again later */
895 clear_bit(CF_CONNECT_PENDING, &con->flags);
896 clear_bit(CF_INIT_PENDING, &con->flags);
899 spin_lock(&con->writequeue_lock);
903 if (e->len == 0 && e->users == 0) {
907 spin_unlock(&con->writequeue_lock);
911 /* Connect a new socket to its peer */
912 static void tcp_connect_to_sock(struct connection *con)
914 int result = -EHOSTUNREACH;
915 struct sockaddr_storage saddr, src_addr;
917 struct socket *sock = NULL;
919 if (con->nodeid == 0) {
920 log_print("attempt to connect sock 0 foiled");
924 mutex_lock(&con->sock_mutex);
925 if (con->retries++ > MAX_CONNECT_RETRIES)
928 /* Some odd races can cause double-connects, ignore them */
934 /* Create a socket to communicate with */
935 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
940 memset(&saddr, 0, sizeof(saddr));
941 if (dlm_nodeid_to_addr(con->nodeid, &saddr))
944 sock->sk->sk_user_data = con;
945 con->rx_action = receive_from_sock;
946 con->connect_action = tcp_connect_to_sock;
949 /* Bind to our cluster-known address connecting to avoid
951 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
952 make_sockaddr(&src_addr, 0, &addr_len);
953 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
956 log_print("could not bind for connect: %d", result);
957 /* This *may* not indicate a critical error */
960 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
962 log_print("connecting to %d", con->nodeid);
964 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
966 if (result == -EINPROGRESS)
973 sock_release(con->sock);
979 * Some errors are fatal and this list might need adjusting. For other
980 * errors we try again until the max number of retries is reached.
982 if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
983 result != -ENETDOWN && result != -EINVAL
984 && result != -EPROTONOSUPPORT) {
985 lowcomms_connect_sock(con);
989 mutex_unlock(&con->sock_mutex);
993 static struct socket *tcp_create_listen_sock(struct connection *con,
994 struct sockaddr_storage *saddr)
996 struct socket *sock = NULL;
1001 if (dlm_local_addr[0]->ss_family == AF_INET)
1002 addr_len = sizeof(struct sockaddr_in);
1004 addr_len = sizeof(struct sockaddr_in6);
1006 /* Create a socket to communicate with */
1007 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1008 IPPROTO_TCP, &sock);
1010 log_print("Can't create listening comms socket");
1014 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1015 (char *)&one, sizeof(one));
1018 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
1020 sock->sk->sk_user_data = con;
1021 con->rx_action = tcp_accept_from_sock;
1022 con->connect_action = tcp_connect_to_sock;
1025 /* Bind to our port */
1026 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1027 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1029 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1035 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1036 (char *)&one, sizeof(one));
1038 log_print("Set keepalive failed: %d", result);
1041 result = sock->ops->listen(sock, 5);
1043 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1053 /* Get local addresses */
1054 static void init_local(void)
1056 struct sockaddr_storage sas, *addr;
1059 dlm_local_count = 0;
1060 for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
1061 if (dlm_our_addr(&sas, i))
1064 addr = kmalloc(sizeof(*addr), GFP_NOFS);
1067 memcpy(addr, &sas, sizeof(*addr));
1068 dlm_local_addr[dlm_local_count++] = addr;
1072 /* Bind to an IP address. SCTP allows multiple address so it can do
1074 static int add_sctp_bind_addr(struct connection *sctp_con,
1075 struct sockaddr_storage *addr,
1076 int addr_len, int num)
1081 result = kernel_bind(sctp_con->sock,
1082 (struct sockaddr *) addr,
1085 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1086 SCTP_SOCKOPT_BINDX_ADD,
1087 (char *)addr, addr_len);
1090 log_print("Can't bind to port %d addr number %d",
1091 dlm_config.ci_tcp_port, num);
1096 /* Initialise SCTP socket and bind to all interfaces */
1097 static int sctp_listen_for_all(void)
1099 struct socket *sock = NULL;
1100 struct sockaddr_storage localaddr;
1101 struct sctp_event_subscribe subscribe;
1102 int result = -EINVAL, num = 1, i, addr_len;
1103 struct connection *con = nodeid2con(0, GFP_NOFS);
1104 int bufsize = NEEDED_RMEM;
1109 log_print("Using SCTP for communications");
1111 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1112 IPPROTO_SCTP, &sock);
1114 log_print("Can't create comms socket, check SCTP is loaded");
1118 /* Listen for events */
1119 memset(&subscribe, 0, sizeof(subscribe));
1120 subscribe.sctp_data_io_event = 1;
1121 subscribe.sctp_association_event = 1;
1122 subscribe.sctp_send_failure_event = 1;
1123 subscribe.sctp_shutdown_event = 1;
1124 subscribe.sctp_partial_delivery_event = 1;
1126 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
1127 (char *)&bufsize, sizeof(bufsize));
1129 log_print("Error increasing buffer space on socket %d", result);
1131 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
1132 (char *)&subscribe, sizeof(subscribe));
1134 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1136 goto create_delsock;
1139 /* Init con struct */
1140 sock->sk->sk_user_data = con;
1142 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1143 con->rx_action = receive_from_sock;
1144 con->connect_action = sctp_init_assoc;
1146 /* Bind to all interfaces. */
1147 for (i = 0; i < dlm_local_count; i++) {
1148 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1149 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1151 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1153 goto create_delsock;
1157 result = sock->ops->listen(sock, 5);
1159 log_print("Can't set socket listening");
1160 goto create_delsock;
1172 static int tcp_listen_for_all(void)
1174 struct socket *sock = NULL;
1175 struct connection *con = nodeid2con(0, GFP_NOFS);
1176 int result = -EINVAL;
1181 /* We don't support multi-homed hosts */
1182 if (dlm_local_addr[1] != NULL) {
1183 log_print("TCP protocol can't handle multi-homed hosts, "
1188 log_print("Using TCP for communications");
1190 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1192 add_sock(sock, con);
1196 result = -EADDRINUSE;
1204 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1207 struct writequeue_entry *entry;
1209 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1213 entry->page = alloc_page(allocation);
1228 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1230 struct connection *con;
1231 struct writequeue_entry *e;
1235 con = nodeid2con(nodeid, allocation);
1239 spin_lock(&con->writequeue_lock);
1240 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1241 if ((&e->list == &con->writequeue) ||
1242 (PAGE_CACHE_SIZE - e->end < len)) {
1249 spin_unlock(&con->writequeue_lock);
1253 *ppc = page_address(e->page) + offset;
1257 e = new_writequeue_entry(con, allocation);
1259 spin_lock(&con->writequeue_lock);
1263 list_add_tail(&e->list, &con->writequeue);
1264 spin_unlock(&con->writequeue_lock);
1270 void dlm_lowcomms_commit_buffer(void *mh)
1272 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1273 struct connection *con = e->con;
1276 spin_lock(&con->writequeue_lock);
1280 e->len = e->end - e->offset;
1281 spin_unlock(&con->writequeue_lock);
1283 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1284 queue_work(send_workqueue, &con->swork);
1289 spin_unlock(&con->writequeue_lock);
1293 /* Send a message */
1294 static void send_to_sock(struct connection *con)
1297 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1298 struct writequeue_entry *e;
1301 mutex_lock(&con->sock_mutex);
1302 if (con->sock == NULL)
1305 spin_lock(&con->writequeue_lock);
1307 e = list_entry(con->writequeue.next, struct writequeue_entry,
1309 if ((struct list_head *) e == &con->writequeue)
1314 BUG_ON(len == 0 && e->users == 0);
1315 spin_unlock(&con->writequeue_lock);
1319 ret = kernel_sendpage(con->sock, e->page, offset, len,
1321 if (ret == -EAGAIN || ret == 0) {
1328 /* Don't starve people filling buffers */
1331 spin_lock(&con->writequeue_lock);
1335 if (e->len == 0 && e->users == 0) {
1341 spin_unlock(&con->writequeue_lock);
1343 mutex_unlock(&con->sock_mutex);
1347 mutex_unlock(&con->sock_mutex);
1348 close_connection(con, false);
1349 lowcomms_connect_sock(con);
1353 mutex_unlock(&con->sock_mutex);
1354 if (!test_bit(CF_INIT_PENDING, &con->flags))
1355 lowcomms_connect_sock(con);
1359 static void clean_one_writequeue(struct connection *con)
1361 struct writequeue_entry *e, *safe;
1363 spin_lock(&con->writequeue_lock);
1364 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1368 spin_unlock(&con->writequeue_lock);
1371 /* Called from recovery when it knows that a node has
1373 int dlm_lowcomms_close(int nodeid)
1375 struct connection *con;
1377 log_print("closing connection to node %d", nodeid);
1378 con = nodeid2con(nodeid, 0);
1380 clear_bit(CF_CONNECT_PENDING, &con->flags);
1381 clear_bit(CF_WRITE_PENDING, &con->flags);
1382 set_bit(CF_CLOSE, &con->flags);
1383 if (cancel_work_sync(&con->swork))
1384 log_print("canceled swork for node %d", nodeid);
1385 if (cancel_work_sync(&con->rwork))
1386 log_print("canceled rwork for node %d", nodeid);
1387 clean_one_writequeue(con);
1388 close_connection(con, true);
1393 /* Receive workqueue function */
1394 static void process_recv_sockets(struct work_struct *work)
1396 struct connection *con = container_of(work, struct connection, rwork);
1399 clear_bit(CF_READ_PENDING, &con->flags);
1401 err = con->rx_action(con);
1405 /* Send workqueue function */
1406 static void process_send_sockets(struct work_struct *work)
1408 struct connection *con = container_of(work, struct connection, swork);
1410 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1411 con->connect_action(con);
1412 set_bit(CF_WRITE_PENDING, &con->flags);
1414 if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
1419 /* Discard all entries on the write queues */
1420 static void clean_writequeues(void)
1422 foreach_conn(clean_one_writequeue);
1425 static void work_stop(void)
1427 destroy_workqueue(recv_workqueue);
1428 destroy_workqueue(send_workqueue);
1431 static int work_start(void)
1434 recv_workqueue = create_workqueue("dlm_recv");
1435 error = IS_ERR(recv_workqueue);
1437 log_print("can't start dlm_recv %d", error);
1441 send_workqueue = create_singlethread_workqueue("dlm_send");
1442 error = IS_ERR(send_workqueue);
1444 log_print("can't start dlm_send %d", error);
1445 destroy_workqueue(recv_workqueue);
1452 static void stop_conn(struct connection *con)
1455 if (con->sock && con->sock->sk)
1456 con->sock->sk->sk_user_data = NULL;
1459 static void free_conn(struct connection *con)
1461 close_connection(con, true);
1463 kmem_cache_free(con_cache, con->othercon);
1464 hlist_del(&con->list);
1465 kmem_cache_free(con_cache, con);
1468 void dlm_lowcomms_stop(void)
1470 /* Set all the flags to prevent any
1473 mutex_lock(&connections_lock);
1474 foreach_conn(stop_conn);
1475 mutex_unlock(&connections_lock);
1479 mutex_lock(&connections_lock);
1480 clean_writequeues();
1482 foreach_conn(free_conn);
1484 mutex_unlock(&connections_lock);
1485 kmem_cache_destroy(con_cache);
1488 int dlm_lowcomms_start(void)
1490 int error = -EINVAL;
1491 struct connection *con;
1494 for (i = 0; i < CONN_HASH_SIZE; i++)
1495 INIT_HLIST_HEAD(&connection_hash[i]);
1498 if (!dlm_local_count) {
1500 log_print("no local IP address has been set");
1505 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1506 __alignof__(struct connection), 0,
1511 /* Start listening */
1512 if (dlm_config.ci_protocol == 0)
1513 error = tcp_listen_for_all();
1515 error = sctp_listen_for_all();
1519 error = work_start();
1526 con = nodeid2con(0,0);
1528 close_connection(con, false);
1529 kmem_cache_free(con_cache, con);
1531 kmem_cache_destroy(con_cache);