1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2007 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 it's
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 wither 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 shouldbe 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/idr.h>
52 #include <linux/file.h>
53 #include <linux/sctp.h>
54 #include <net/sctp/user.h>
56 #include "dlm_internal.h"
61 #define NEEDED_RMEM (4*1024*1024)
69 static void cbuf_add(struct cbuf *cb, int n)
74 static int cbuf_data(struct cbuf *cb)
76 return ((cb->base + cb->len) & cb->mask);
79 static void cbuf_init(struct cbuf *cb, int size)
81 cb->base = cb->len = 0;
85 static void cbuf_eat(struct cbuf *cb, int n)
92 static bool cbuf_empty(struct cbuf *cb)
98 struct socket *sock; /* NULL if not connected */
99 uint32_t nodeid; /* So we know who we are in the list */
100 struct mutex sock_mutex;
102 #define CF_READ_PENDING 1
103 #define CF_WRITE_PENDING 2
104 #define CF_CONNECT_PENDING 3
105 #define CF_INIT_PENDING 4
106 #define CF_IS_OTHERCON 5
107 struct list_head writequeue; /* List of outgoing writequeue_entries */
108 spinlock_t writequeue_lock;
109 int (*rx_action) (struct connection *); /* What to do when active */
110 void (*connect_action) (struct connection *); /* What to do to connect */
111 struct page *rx_page;
114 #define MAX_CONNECT_RETRIES 3
116 struct connection *othercon;
117 struct work_struct rwork; /* Receive workqueue */
118 struct work_struct swork; /* Send workqueue */
120 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
122 /* An entry waiting to be sent */
123 struct writequeue_entry {
124 struct list_head list;
130 struct connection *con;
133 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
134 static int dlm_local_count;
137 static struct workqueue_struct *recv_workqueue;
138 static struct workqueue_struct *send_workqueue;
140 static DEFINE_IDR(connections_idr);
141 static DECLARE_MUTEX(connections_lock);
142 static int max_nodeid;
143 static struct kmem_cache *con_cache;
145 static void process_recv_sockets(struct work_struct *work);
146 static void process_send_sockets(struct work_struct *work);
149 * If 'allocation' is zero then we don't attempt to create a new
150 * connection structure for this node.
152 static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
154 struct connection *con = NULL;
158 con = idr_find(&connections_idr, nodeid);
162 r = idr_pre_get(&connections_idr, alloc);
166 con = kmem_cache_zalloc(con_cache, alloc);
170 r = idr_get_new_above(&connections_idr, con, nodeid, &n);
172 kmem_cache_free(con_cache, con);
177 idr_remove(&connections_idr, n);
178 kmem_cache_free(con_cache, con);
182 con->nodeid = nodeid;
183 mutex_init(&con->sock_mutex);
184 INIT_LIST_HEAD(&con->writequeue);
185 spin_lock_init(&con->writequeue_lock);
186 INIT_WORK(&con->swork, process_send_sockets);
187 INIT_WORK(&con->rwork, process_recv_sockets);
189 /* Setup action pointers for child sockets */
191 struct connection *zerocon = idr_find(&connections_idr, 0);
193 con->connect_action = zerocon->connect_action;
195 con->rx_action = zerocon->rx_action;
198 if (nodeid > max_nodeid)
204 static struct connection *nodeid2con(int nodeid, gfp_t allocation)
206 struct connection *con;
208 down(&connections_lock);
209 con = __nodeid2con(nodeid, allocation);
210 up(&connections_lock);
215 /* This is a bit drastic, but only called when things go wrong */
216 static struct connection *assoc2con(int assoc_id)
219 struct connection *con;
221 down(&connections_lock);
222 for (i=0; i<=max_nodeid; i++) {
223 con = __nodeid2con(i, 0);
224 if (con && con->sctp_assoc == assoc_id) {
225 up(&connections_lock);
229 up(&connections_lock);
233 static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
235 struct sockaddr_storage addr;
238 if (!dlm_local_count)
241 error = dlm_nodeid_to_addr(nodeid, &addr);
245 if (dlm_local_addr[0]->ss_family == AF_INET) {
246 struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
247 struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
248 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
250 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
251 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
252 memcpy(&ret6->sin6_addr, &in6->sin6_addr,
253 sizeof(in6->sin6_addr));
259 /* Data available on socket or listen socket received a connect */
260 static void lowcomms_data_ready(struct sock *sk, int count_unused)
262 struct connection *con = sock2con(sk);
263 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
264 queue_work(recv_workqueue, &con->rwork);
267 static void lowcomms_write_space(struct sock *sk)
269 struct connection *con = sock2con(sk);
271 if (con && !test_and_set_bit(CF_WRITE_PENDING, &con->flags))
272 queue_work(send_workqueue, &con->swork);
275 static inline void lowcomms_connect_sock(struct connection *con)
277 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
278 queue_work(send_workqueue, &con->swork);
281 static void lowcomms_state_change(struct sock *sk)
283 if (sk->sk_state == TCP_ESTABLISHED)
284 lowcomms_write_space(sk);
287 /* Make a socket active */
288 static int add_sock(struct socket *sock, struct connection *con)
292 /* Install a data_ready callback */
293 con->sock->sk->sk_data_ready = lowcomms_data_ready;
294 con->sock->sk->sk_write_space = lowcomms_write_space;
295 con->sock->sk->sk_state_change = lowcomms_state_change;
296 con->sock->sk->sk_user_data = con;
300 /* Add the port number to an IPv6 or 4 sockaddr and return the address
302 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
305 saddr->ss_family = dlm_local_addr[0]->ss_family;
306 if (saddr->ss_family == AF_INET) {
307 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
308 in4_addr->sin_port = cpu_to_be16(port);
309 *addr_len = sizeof(struct sockaddr_in);
310 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
312 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
313 in6_addr->sin6_port = cpu_to_be16(port);
314 *addr_len = sizeof(struct sockaddr_in6);
316 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
319 /* Close a remote connection and tidy up */
320 static void close_connection(struct connection *con, bool and_other)
322 mutex_lock(&con->sock_mutex);
325 sock_release(con->sock);
328 if (con->othercon && and_other) {
329 /* Will only re-enter once. */
330 close_connection(con->othercon, false);
333 __free_page(con->rx_page);
337 /* If we are an 'othercon' then NULL the pointer to us
338 from the parent and tidy ourself up */
339 if (test_bit(CF_IS_OTHERCON, &con->flags)) {
340 struct connection *parent = __nodeid2con(con->nodeid, 0);
341 parent->othercon = NULL;
342 kmem_cache_free(con_cache, con);
345 /* Parent connections get reused */
347 mutex_unlock(&con->sock_mutex);
351 /* We only send shutdown messages to nodes that are not part of the cluster */
352 static void sctp_send_shutdown(sctp_assoc_t associd)
354 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
355 struct msghdr outmessage;
356 struct cmsghdr *cmsg;
357 struct sctp_sndrcvinfo *sinfo;
359 struct connection *con;
361 con = nodeid2con(0,0);
364 outmessage.msg_name = NULL;
365 outmessage.msg_namelen = 0;
366 outmessage.msg_control = outcmsg;
367 outmessage.msg_controllen = sizeof(outcmsg);
368 outmessage.msg_flags = MSG_EOR;
370 cmsg = CMSG_FIRSTHDR(&outmessage);
371 cmsg->cmsg_level = IPPROTO_SCTP;
372 cmsg->cmsg_type = SCTP_SNDRCV;
373 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
374 outmessage.msg_controllen = cmsg->cmsg_len;
375 sinfo = CMSG_DATA(cmsg);
376 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
378 sinfo->sinfo_flags |= MSG_EOF;
379 sinfo->sinfo_assoc_id = associd;
381 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
384 log_print("send EOF to node failed: %d", ret);
387 /* INIT failed but we don't know which node...
388 restart INIT on all pending nodes */
389 static void sctp_init_failed(void)
392 struct connection *con;
394 down(&connections_lock);
395 for (i=1; i<=max_nodeid; i++) {
396 con = __nodeid2con(i, 0);
400 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
401 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
402 queue_work(send_workqueue, &con->swork);
406 up(&connections_lock);
409 /* Something happened to an association */
410 static void process_sctp_notification(struct connection *con,
411 struct msghdr *msg, char *buf)
413 union sctp_notification *sn = (union sctp_notification *)buf;
415 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
416 switch (sn->sn_assoc_change.sac_state) {
421 /* Check that the new node is in the lockspace */
422 struct sctp_prim prim;
426 struct connection *new_con;
428 sctp_peeloff_arg_t parg;
429 int parglen = sizeof(parg);
432 * We get this before any data for an association.
433 * We verify that the node is in the cluster and
434 * then peel off a socket for it.
436 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
437 log_print("COMM_UP for invalid assoc ID %d",
438 (int)sn->sn_assoc_change.sac_assoc_id);
442 memset(&prim, 0, sizeof(struct sctp_prim));
443 prim_len = sizeof(struct sctp_prim);
444 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
446 ret = kernel_getsockopt(con->sock,
452 log_print("getsockopt/sctp_primary_addr on "
453 "new assoc %d failed : %d",
454 (int)sn->sn_assoc_change.sac_assoc_id,
457 /* Retry INIT later */
458 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
460 clear_bit(CF_CONNECT_PENDING, &con->flags);
463 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
464 if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
466 unsigned char *b=(unsigned char *)&prim.ssp_addr;
467 log_print("reject connect from unknown addr");
468 for (i=0; i<sizeof(struct sockaddr_storage);i++)
469 printk("%02x ", b[i]);
471 sctp_send_shutdown(prim.ssp_assoc_id);
475 new_con = nodeid2con(nodeid, GFP_KERNEL);
479 /* Peel off a new sock */
480 parg.associd = sn->sn_assoc_change.sac_assoc_id;
481 ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
482 SCTP_SOCKOPT_PEELOFF,
483 (void *)&parg, &parglen);
485 log_print("Can't peel off a socket for "
486 "connection %d to node %d: err=%d\n",
487 parg.associd, nodeid, ret);
489 file = fget(parg.sd);
490 new_con->sock = SOCKET_I(file->f_dentry->d_inode);
491 add_sock(new_con->sock, new_con);
493 put_unused_fd(parg.sd);
495 log_print("got new/restarted association %d nodeid %d",
496 (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
498 /* Send any pending writes */
499 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
500 clear_bit(CF_INIT_PENDING, &con->flags);
501 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
502 queue_work(send_workqueue, &new_con->swork);
504 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
505 queue_work(recv_workqueue, &new_con->rwork);
510 case SCTP_SHUTDOWN_COMP:
512 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
519 /* We don't know which INIT failed, so clear the PENDING flags
520 * on them all. if assoc_id is zero then it will then try
523 case SCTP_CANT_STR_ASSOC:
525 log_print("Can't start SCTP association - retrying");
531 log_print("unexpected SCTP assoc change id=%d state=%d",
532 (int)sn->sn_assoc_change.sac_assoc_id,
533 sn->sn_assoc_change.sac_state);
538 /* Data received from remote end */
539 static int receive_from_sock(struct connection *con)
542 struct msghdr msg = {};
546 int call_again_soon = 0;
548 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
550 mutex_lock(&con->sock_mutex);
552 if (con->sock == NULL) {
557 if (con->rx_page == NULL) {
559 * This doesn't need to be atomic, but I think it should
560 * improve performance if it is.
562 con->rx_page = alloc_page(GFP_ATOMIC);
563 if (con->rx_page == NULL)
565 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
568 /* Only SCTP needs these really */
569 memset(&incmsg, 0, sizeof(incmsg));
570 msg.msg_control = incmsg;
571 msg.msg_controllen = sizeof(incmsg);
574 * iov[0] is the bit of the circular buffer between the current end
575 * point (cb.base + cb.len) and the end of the buffer.
577 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
578 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
583 * iov[1] is the bit of the circular buffer between the start of the
584 * buffer and the start of the currently used section (cb.base)
586 if (cbuf_data(&con->cb) >= con->cb.base) {
587 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
588 iov[1].iov_len = con->cb.base;
589 iov[1].iov_base = page_address(con->rx_page);
592 len = iov[0].iov_len + iov[1].iov_len;
594 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
595 MSG_DONTWAIT | MSG_NOSIGNAL);
599 /* Process SCTP notifications */
600 if (msg.msg_flags & MSG_NOTIFICATION) {
601 msg.msg_control = incmsg;
602 msg.msg_controllen = sizeof(incmsg);
604 process_sctp_notification(con, &msg,
605 page_address(con->rx_page) + con->cb.base);
606 mutex_unlock(&con->sock_mutex);
609 BUG_ON(con->nodeid == 0);
613 cbuf_add(&con->cb, ret);
614 ret = dlm_process_incoming_buffer(con->nodeid,
615 page_address(con->rx_page),
616 con->cb.base, con->cb.len,
618 if (ret == -EBADMSG) {
619 log_print("lowcomms: addr=%p, base=%u, len=%u, "
620 "iov_len=%u, iov_base[0]=%p, read=%d",
621 page_address(con->rx_page), con->cb.base, con->cb.len,
622 len, iov[0].iov_base, r);
626 cbuf_eat(&con->cb, ret);
628 if (cbuf_empty(&con->cb) && !call_again_soon) {
629 __free_page(con->rx_page);
635 mutex_unlock(&con->sock_mutex);
639 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
640 queue_work(recv_workqueue, &con->rwork);
641 mutex_unlock(&con->sock_mutex);
645 mutex_unlock(&con->sock_mutex);
646 if (ret != -EAGAIN) {
647 close_connection(con, false);
648 /* Reconnect when there is something to send */
650 /* Don't return success if we really got EOF */
657 /* Listening socket is busy, accept a connection */
658 static int tcp_accept_from_sock(struct connection *con)
661 struct sockaddr_storage peeraddr;
662 struct socket *newsock;
665 struct connection *newcon;
666 struct connection *addcon;
668 memset(&peeraddr, 0, sizeof(peeraddr));
669 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
670 IPPROTO_TCP, &newsock);
674 mutex_lock_nested(&con->sock_mutex, 0);
677 if (con->sock == NULL)
680 newsock->type = con->sock->type;
681 newsock->ops = con->sock->ops;
683 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
687 /* Get the connected socket's peer */
688 memset(&peeraddr, 0, sizeof(peeraddr));
689 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
691 result = -ECONNABORTED;
695 /* Get the new node's NODEID */
696 make_sockaddr(&peeraddr, 0, &len);
697 if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
698 log_print("connect from non cluster node");
699 sock_release(newsock);
700 mutex_unlock(&con->sock_mutex);
704 log_print("got connection from %d", nodeid);
706 /* Check to see if we already have a connection to this node. This
707 * could happen if the two nodes initiate a connection at roughly
708 * the same time and the connections cross on the wire.
709 * In this case we store the incoming one in "othercon"
711 newcon = nodeid2con(nodeid, GFP_KERNEL);
716 mutex_lock_nested(&newcon->sock_mutex, 1);
718 struct connection *othercon = newcon->othercon;
721 othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
723 log_print("failed to allocate incoming socket");
724 mutex_unlock(&newcon->sock_mutex);
728 othercon->nodeid = nodeid;
729 othercon->rx_action = receive_from_sock;
730 mutex_init(&othercon->sock_mutex);
731 INIT_WORK(&othercon->swork, process_send_sockets);
732 INIT_WORK(&othercon->rwork, process_recv_sockets);
733 set_bit(CF_IS_OTHERCON, &othercon->flags);
734 newcon->othercon = othercon;
735 othercon->sock = newsock;
736 newsock->sk->sk_user_data = othercon;
737 add_sock(newsock, othercon);
741 printk("Extra connection from node %d attempted\n", nodeid);
743 mutex_unlock(&newcon->sock_mutex);
748 newsock->sk->sk_user_data = newcon;
749 newcon->rx_action = receive_from_sock;
750 add_sock(newsock, newcon);
754 mutex_unlock(&newcon->sock_mutex);
757 * Add it to the active queue in case we got data
758 * beween processing the accept adding the socket
759 * to the read_sockets list
761 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
762 queue_work(recv_workqueue, &addcon->rwork);
763 mutex_unlock(&con->sock_mutex);
768 mutex_unlock(&con->sock_mutex);
769 sock_release(newsock);
771 if (result != -EAGAIN)
772 log_print("error accepting connection from node: %d", result);
776 static void free_entry(struct writequeue_entry *e)
778 __free_page(e->page);
782 /* Initiate an SCTP association.
783 This is a special case of send_to_sock() in that we don't yet have a
784 peeled-off socket for this association, so we use the listening socket
785 and add the primary IP address of the remote node.
787 static void sctp_init_assoc(struct connection *con)
789 struct sockaddr_storage rem_addr;
790 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
791 struct msghdr outmessage;
792 struct cmsghdr *cmsg;
793 struct sctp_sndrcvinfo *sinfo;
794 struct connection *base_con;
795 struct writequeue_entry *e;
801 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
804 if (con->retries++ > MAX_CONNECT_RETRIES)
807 log_print("Initiating association with node %d", con->nodeid);
809 if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
810 log_print("no address for nodeid %d", con->nodeid);
813 base_con = nodeid2con(0, 0);
814 BUG_ON(base_con == NULL);
816 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
818 outmessage.msg_name = &rem_addr;
819 outmessage.msg_namelen = addrlen;
820 outmessage.msg_control = outcmsg;
821 outmessage.msg_controllen = sizeof(outcmsg);
822 outmessage.msg_flags = MSG_EOR;
824 spin_lock(&con->writequeue_lock);
825 e = list_entry(con->writequeue.next, struct writequeue_entry,
828 BUG_ON((struct list_head *) e == &con->writequeue);
832 spin_unlock(&con->writequeue_lock);
835 /* Send the first block off the write queue */
836 iov[0].iov_base = page_address(e->page)+offset;
837 iov[0].iov_len = len;
839 cmsg = CMSG_FIRSTHDR(&outmessage);
840 cmsg->cmsg_level = IPPROTO_SCTP;
841 cmsg->cmsg_type = SCTP_SNDRCV;
842 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
843 sinfo = CMSG_DATA(cmsg);
844 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
845 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
846 outmessage.msg_controllen = cmsg->cmsg_len;
848 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
850 log_print("Send first packet to node %d failed: %d",
853 /* Try again later */
854 clear_bit(CF_CONNECT_PENDING, &con->flags);
855 clear_bit(CF_INIT_PENDING, &con->flags);
858 spin_lock(&con->writequeue_lock);
862 if (e->len == 0 && e->users == 0) {
867 spin_unlock(&con->writequeue_lock);
871 /* Connect a new socket to its peer */
872 static void tcp_connect_to_sock(struct connection *con)
874 int result = -EHOSTUNREACH;
875 struct sockaddr_storage saddr;
879 if (con->nodeid == 0) {
880 log_print("attempt to connect sock 0 foiled");
884 mutex_lock(&con->sock_mutex);
885 if (con->retries++ > MAX_CONNECT_RETRIES)
888 /* Some odd races can cause double-connects, ignore them */
894 /* Create a socket to communicate with */
895 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
900 memset(&saddr, 0, sizeof(saddr));
901 if (dlm_nodeid_to_addr(con->nodeid, &saddr))
904 sock->sk->sk_user_data = con;
905 con->rx_action = receive_from_sock;
906 con->connect_action = tcp_connect_to_sock;
909 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
911 log_print("connecting to %d", con->nodeid);
913 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
915 if (result == -EINPROGRESS)
922 sock_release(con->sock);
926 * Some errors are fatal and this list might need adjusting. For other
927 * errors we try again until the max number of retries is reached.
929 if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
930 result != -ENETDOWN && result != EINVAL
931 && result != -EPROTONOSUPPORT) {
932 lowcomms_connect_sock(con);
936 mutex_unlock(&con->sock_mutex);
940 static struct socket *tcp_create_listen_sock(struct connection *con,
941 struct sockaddr_storage *saddr)
943 struct socket *sock = NULL;
948 if (dlm_local_addr[0]->ss_family == AF_INET)
949 addr_len = sizeof(struct sockaddr_in);
951 addr_len = sizeof(struct sockaddr_in6);
953 /* Create a socket to communicate with */
954 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
957 log_print("Can't create listening comms socket");
961 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
962 (char *)&one, sizeof(one));
965 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
967 sock->sk->sk_user_data = con;
968 con->rx_action = tcp_accept_from_sock;
969 con->connect_action = tcp_connect_to_sock;
972 /* Bind to our port */
973 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
974 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
976 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
982 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
983 (char *)&one, sizeof(one));
985 log_print("Set keepalive failed: %d", result);
988 result = sock->ops->listen(sock, 5);
990 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1000 /* Get local addresses */
1001 static void init_local(void)
1003 struct sockaddr_storage sas, *addr;
1006 dlm_local_count = 0;
1007 for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
1008 if (dlm_our_addr(&sas, i))
1011 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1014 memcpy(addr, &sas, sizeof(*addr));
1015 dlm_local_addr[dlm_local_count++] = addr;
1019 /* Bind to an IP address. SCTP allows multiple address so it can do
1021 static int add_sctp_bind_addr(struct connection *sctp_con,
1022 struct sockaddr_storage *addr,
1023 int addr_len, int num)
1028 result = kernel_bind(sctp_con->sock,
1029 (struct sockaddr *) addr,
1032 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1033 SCTP_SOCKOPT_BINDX_ADD,
1034 (char *)addr, addr_len);
1037 log_print("Can't bind to port %d addr number %d",
1038 dlm_config.ci_tcp_port, num);
1043 /* Initialise SCTP socket and bind to all interfaces */
1044 static int sctp_listen_for_all(void)
1046 struct socket *sock = NULL;
1047 struct sockaddr_storage localaddr;
1048 struct sctp_event_subscribe subscribe;
1049 int result = -EINVAL, num = 1, i, addr_len;
1050 struct connection *con = nodeid2con(0, GFP_KERNEL);
1051 int bufsize = NEEDED_RMEM;
1056 log_print("Using SCTP for communications");
1058 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1059 IPPROTO_SCTP, &sock);
1061 log_print("Can't create comms socket, check SCTP is loaded");
1065 /* Listen for events */
1066 memset(&subscribe, 0, sizeof(subscribe));
1067 subscribe.sctp_data_io_event = 1;
1068 subscribe.sctp_association_event = 1;
1069 subscribe.sctp_send_failure_event = 1;
1070 subscribe.sctp_shutdown_event = 1;
1071 subscribe.sctp_partial_delivery_event = 1;
1073 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1074 (char *)&bufsize, sizeof(bufsize));
1076 log_print("Error increasing buffer space on socket %d", result);
1078 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
1079 (char *)&subscribe, sizeof(subscribe));
1081 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1083 goto create_delsock;
1086 /* Init con struct */
1087 sock->sk->sk_user_data = con;
1089 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1090 con->rx_action = receive_from_sock;
1091 con->connect_action = sctp_init_assoc;
1093 /* Bind to all interfaces. */
1094 for (i = 0; i < dlm_local_count; i++) {
1095 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1096 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1098 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1100 goto create_delsock;
1104 result = sock->ops->listen(sock, 5);
1106 log_print("Can't set socket listening");
1107 goto create_delsock;
1119 static int tcp_listen_for_all(void)
1121 struct socket *sock = NULL;
1122 struct connection *con = nodeid2con(0, GFP_KERNEL);
1123 int result = -EINVAL;
1128 /* We don't support multi-homed hosts */
1129 if (dlm_local_addr[1] != NULL) {
1130 log_print("TCP protocol can't handle multi-homed hosts, "
1135 log_print("Using TCP for communications");
1137 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1139 add_sock(sock, con);
1143 result = -EADDRINUSE;
1151 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1154 struct writequeue_entry *entry;
1156 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1160 entry->page = alloc_page(allocation);
1175 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1177 struct connection *con;
1178 struct writequeue_entry *e;
1182 con = nodeid2con(nodeid, allocation);
1186 spin_lock(&con->writequeue_lock);
1187 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1188 if ((&e->list == &con->writequeue) ||
1189 (PAGE_CACHE_SIZE - e->end < len)) {
1196 spin_unlock(&con->writequeue_lock);
1202 *ppc = page_address(e->page) + offset;
1206 e = new_writequeue_entry(con, allocation);
1208 spin_lock(&con->writequeue_lock);
1212 list_add_tail(&e->list, &con->writequeue);
1213 spin_unlock(&con->writequeue_lock);
1219 void dlm_lowcomms_commit_buffer(void *mh)
1221 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1222 struct connection *con = e->con;
1225 spin_lock(&con->writequeue_lock);
1229 e->len = e->end - e->offset;
1231 spin_unlock(&con->writequeue_lock);
1233 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1234 queue_work(send_workqueue, &con->swork);
1239 spin_unlock(&con->writequeue_lock);
1243 /* Send a message */
1244 static void send_to_sock(struct connection *con)
1247 ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
1248 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1249 struct writequeue_entry *e;
1252 mutex_lock(&con->sock_mutex);
1253 if (con->sock == NULL)
1256 sendpage = con->sock->ops->sendpage;
1258 spin_lock(&con->writequeue_lock);
1260 e = list_entry(con->writequeue.next, struct writequeue_entry,
1262 if ((struct list_head *) e == &con->writequeue)
1267 BUG_ON(len == 0 && e->users == 0);
1268 spin_unlock(&con->writequeue_lock);
1273 ret = sendpage(con->sock, e->page, offset, len,
1275 if (ret == -EAGAIN || ret == 0)
1280 /* Don't starve people filling buffers */
1284 spin_lock(&con->writequeue_lock);
1288 if (e->len == 0 && e->users == 0) {
1295 spin_unlock(&con->writequeue_lock);
1297 mutex_unlock(&con->sock_mutex);
1301 mutex_unlock(&con->sock_mutex);
1302 close_connection(con, false);
1303 lowcomms_connect_sock(con);
1307 mutex_unlock(&con->sock_mutex);
1308 if (!test_bit(CF_INIT_PENDING, &con->flags))
1309 lowcomms_connect_sock(con);
1313 static void clean_one_writequeue(struct connection *con)
1315 struct list_head *list;
1316 struct list_head *temp;
1318 spin_lock(&con->writequeue_lock);
1319 list_for_each_safe(list, temp, &con->writequeue) {
1320 struct writequeue_entry *e =
1321 list_entry(list, struct writequeue_entry, list);
1325 spin_unlock(&con->writequeue_lock);
1328 /* Called from recovery when it knows that a node has
1330 int dlm_lowcomms_close(int nodeid)
1332 struct connection *con;
1334 log_print("closing connection to node %d", nodeid);
1335 con = nodeid2con(nodeid, 0);
1337 clean_one_writequeue(con);
1338 close_connection(con, true);
1343 /* Receive workqueue function */
1344 static void process_recv_sockets(struct work_struct *work)
1346 struct connection *con = container_of(work, struct connection, rwork);
1349 clear_bit(CF_READ_PENDING, &con->flags);
1351 err = con->rx_action(con);
1355 /* Send workqueue function */
1356 static void process_send_sockets(struct work_struct *work)
1358 struct connection *con = container_of(work, struct connection, swork);
1360 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1361 con->connect_action(con);
1363 clear_bit(CF_WRITE_PENDING, &con->flags);
1368 /* Discard all entries on the write queues */
1369 static void clean_writequeues(void)
1373 for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
1374 struct connection *con = __nodeid2con(nodeid, 0);
1377 clean_one_writequeue(con);
1381 static void work_stop(void)
1383 destroy_workqueue(recv_workqueue);
1384 destroy_workqueue(send_workqueue);
1387 static int work_start(void)
1390 recv_workqueue = create_workqueue("dlm_recv");
1391 error = IS_ERR(recv_workqueue);
1393 log_print("can't start dlm_recv %d", error);
1397 send_workqueue = create_singlethread_workqueue("dlm_send");
1398 error = IS_ERR(send_workqueue);
1400 log_print("can't start dlm_send %d", error);
1401 destroy_workqueue(recv_workqueue);
1408 void dlm_lowcomms_stop(void)
1411 struct connection *con;
1413 /* Set all the flags to prevent any
1416 down(&connections_lock);
1417 for (i = 0; i <= max_nodeid; i++) {
1418 con = __nodeid2con(i, 0);
1422 con->sock->sk->sk_user_data = NULL;
1425 up(&connections_lock);
1429 down(&connections_lock);
1430 clean_writequeues();
1432 for (i = 0; i <= max_nodeid; i++) {
1433 con = __nodeid2con(i, 0);
1435 close_connection(con, true);
1436 kmem_cache_free(con_cache, con);
1440 up(&connections_lock);
1441 kmem_cache_destroy(con_cache);
1442 idr_init(&connections_idr);
1445 int dlm_lowcomms_start(void)
1447 int error = -EINVAL;
1448 struct connection *con;
1451 if (!dlm_local_count) {
1453 log_print("no local IP address has been set");
1458 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1459 __alignof__(struct connection), 0,
1464 /* Set some sysctl minima */
1465 if (sysctl_rmem_max < NEEDED_RMEM)
1466 sysctl_rmem_max = NEEDED_RMEM;
1468 /* Start listening */
1469 if (dlm_config.ci_protocol == 0)
1470 error = tcp_listen_for_all();
1472 error = sctp_listen_for_all();
1476 error = work_start();
1483 con = nodeid2con(0,0);
1485 close_connection(con, false);
1486 kmem_cache_free(con_cache, con);
1488 kmem_cache_destroy(con_cache);