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/mutex.h>
54 #include <linux/sctp.h>
55 #include <net/sctp/user.h>
57 #include "dlm_internal.h"
62 #define NEEDED_RMEM (4*1024*1024)
70 static void cbuf_add(struct cbuf
*cb
, int n
)
75 static int cbuf_data(struct cbuf
*cb
)
77 return ((cb
->base
+ cb
->len
) & cb
->mask
);
80 static void cbuf_init(struct cbuf
*cb
, int size
)
82 cb
->base
= cb
->len
= 0;
86 static void cbuf_eat(struct cbuf
*cb
, int n
)
93 static bool cbuf_empty(struct cbuf
*cb
)
99 struct socket
*sock
; /* NULL if not connected */
100 uint32_t nodeid
; /* So we know who we are in the list */
101 struct mutex sock_mutex
;
103 #define CF_READ_PENDING 1
104 #define CF_WRITE_PENDING 2
105 #define CF_CONNECT_PENDING 3
106 #define CF_INIT_PENDING 4
107 #define CF_IS_OTHERCON 5
108 struct list_head writequeue
; /* List of outgoing writequeue_entries */
109 spinlock_t writequeue_lock
;
110 int (*rx_action
) (struct connection
*); /* What to do when active */
111 void (*connect_action
) (struct connection
*); /* What to do to connect */
112 struct page
*rx_page
;
115 #define MAX_CONNECT_RETRIES 3
117 struct connection
*othercon
;
118 struct work_struct rwork
; /* Receive workqueue */
119 struct work_struct swork
; /* Send workqueue */
121 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
123 /* An entry waiting to be sent */
124 struct writequeue_entry
{
125 struct list_head list
;
131 struct connection
*con
;
134 static struct sockaddr_storage
*dlm_local_addr
[DLM_MAX_ADDR_COUNT
];
135 static int dlm_local_count
;
138 static struct workqueue_struct
*recv_workqueue
;
139 static struct workqueue_struct
*send_workqueue
;
141 static DEFINE_IDR(connections_idr
);
142 static DEFINE_MUTEX(connections_lock
);
143 static int max_nodeid
;
144 static struct kmem_cache
*con_cache
;
146 static void process_recv_sockets(struct work_struct
*work
);
147 static void process_send_sockets(struct work_struct
*work
);
150 * If 'allocation' is zero then we don't attempt to create a new
151 * connection structure for this node.
153 static struct connection
*__nodeid2con(int nodeid
, gfp_t alloc
)
155 struct connection
*con
= NULL
;
159 con
= idr_find(&connections_idr
, nodeid
);
163 r
= idr_pre_get(&connections_idr
, alloc
);
167 con
= kmem_cache_zalloc(con_cache
, alloc
);
171 r
= idr_get_new_above(&connections_idr
, con
, nodeid
, &n
);
173 kmem_cache_free(con_cache
, con
);
178 idr_remove(&connections_idr
, n
);
179 kmem_cache_free(con_cache
, con
);
183 con
->nodeid
= nodeid
;
184 mutex_init(&con
->sock_mutex
);
185 INIT_LIST_HEAD(&con
->writequeue
);
186 spin_lock_init(&con
->writequeue_lock
);
187 INIT_WORK(&con
->swork
, process_send_sockets
);
188 INIT_WORK(&con
->rwork
, process_recv_sockets
);
190 /* Setup action pointers for child sockets */
192 struct connection
*zerocon
= idr_find(&connections_idr
, 0);
194 con
->connect_action
= zerocon
->connect_action
;
196 con
->rx_action
= zerocon
->rx_action
;
199 if (nodeid
> max_nodeid
)
205 static struct connection
*nodeid2con(int nodeid
, gfp_t allocation
)
207 struct connection
*con
;
209 mutex_lock(&connections_lock
);
210 con
= __nodeid2con(nodeid
, allocation
);
211 mutex_unlock(&connections_lock
);
216 /* This is a bit drastic, but only called when things go wrong */
217 static struct connection
*assoc2con(int assoc_id
)
220 struct connection
*con
;
222 mutex_lock(&connections_lock
);
223 for (i
=0; i
<=max_nodeid
; i
++) {
224 con
= __nodeid2con(i
, 0);
225 if (con
&& con
->sctp_assoc
== assoc_id
) {
226 mutex_unlock(&connections_lock
);
230 mutex_unlock(&connections_lock
);
234 static int nodeid_to_addr(int nodeid
, struct sockaddr
*retaddr
)
236 struct sockaddr_storage addr
;
239 if (!dlm_local_count
)
242 error
= dlm_nodeid_to_addr(nodeid
, &addr
);
246 if (dlm_local_addr
[0]->ss_family
== AF_INET
) {
247 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &addr
;
248 struct sockaddr_in
*ret4
= (struct sockaddr_in
*) retaddr
;
249 ret4
->sin_addr
.s_addr
= in4
->sin_addr
.s_addr
;
251 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*) &addr
;
252 struct sockaddr_in6
*ret6
= (struct sockaddr_in6
*) retaddr
;
253 memcpy(&ret6
->sin6_addr
, &in6
->sin6_addr
,
254 sizeof(in6
->sin6_addr
));
260 /* Data available on socket or listen socket received a connect */
261 static void lowcomms_data_ready(struct sock
*sk
, int count_unused
)
263 struct connection
*con
= sock2con(sk
);
264 if (con
&& !test_and_set_bit(CF_READ_PENDING
, &con
->flags
))
265 queue_work(recv_workqueue
, &con
->rwork
);
268 static void lowcomms_write_space(struct sock
*sk
)
270 struct connection
*con
= sock2con(sk
);
272 if (con
&& !test_and_set_bit(CF_WRITE_PENDING
, &con
->flags
))
273 queue_work(send_workqueue
, &con
->swork
);
276 static inline void lowcomms_connect_sock(struct connection
*con
)
278 if (!test_and_set_bit(CF_CONNECT_PENDING
, &con
->flags
))
279 queue_work(send_workqueue
, &con
->swork
);
282 static void lowcomms_state_change(struct sock
*sk
)
284 if (sk
->sk_state
== TCP_ESTABLISHED
)
285 lowcomms_write_space(sk
);
288 /* Make a socket active */
289 static int add_sock(struct socket
*sock
, struct connection
*con
)
293 /* Install a data_ready callback */
294 con
->sock
->sk
->sk_data_ready
= lowcomms_data_ready
;
295 con
->sock
->sk
->sk_write_space
= lowcomms_write_space
;
296 con
->sock
->sk
->sk_state_change
= lowcomms_state_change
;
297 con
->sock
->sk
->sk_user_data
= con
;
298 con
->sock
->sk
->sk_allocation
= GFP_NOFS
;
302 /* Add the port number to an IPv6 or 4 sockaddr and return the address
304 static void make_sockaddr(struct sockaddr_storage
*saddr
, uint16_t port
,
307 saddr
->ss_family
= dlm_local_addr
[0]->ss_family
;
308 if (saddr
->ss_family
== AF_INET
) {
309 struct sockaddr_in
*in4_addr
= (struct sockaddr_in
*)saddr
;
310 in4_addr
->sin_port
= cpu_to_be16(port
);
311 *addr_len
= sizeof(struct sockaddr_in
);
312 memset(&in4_addr
->sin_zero
, 0, sizeof(in4_addr
->sin_zero
));
314 struct sockaddr_in6
*in6_addr
= (struct sockaddr_in6
*)saddr
;
315 in6_addr
->sin6_port
= cpu_to_be16(port
);
316 *addr_len
= sizeof(struct sockaddr_in6
);
318 memset((char *)saddr
+ *addr_len
, 0, sizeof(struct sockaddr_storage
) - *addr_len
);
321 /* Close a remote connection and tidy up */
322 static void close_connection(struct connection
*con
, bool and_other
)
324 mutex_lock(&con
->sock_mutex
);
327 sock_release(con
->sock
);
330 if (con
->othercon
&& and_other
) {
331 /* Will only re-enter once. */
332 close_connection(con
->othercon
, false);
335 __free_page(con
->rx_page
);
340 mutex_unlock(&con
->sock_mutex
);
343 /* We only send shutdown messages to nodes that are not part of the cluster */
344 static void sctp_send_shutdown(sctp_assoc_t associd
)
346 static char outcmsg
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
347 struct msghdr outmessage
;
348 struct cmsghdr
*cmsg
;
349 struct sctp_sndrcvinfo
*sinfo
;
351 struct connection
*con
;
353 con
= nodeid2con(0,0);
356 outmessage
.msg_name
= NULL
;
357 outmessage
.msg_namelen
= 0;
358 outmessage
.msg_control
= outcmsg
;
359 outmessage
.msg_controllen
= sizeof(outcmsg
);
360 outmessage
.msg_flags
= MSG_EOR
;
362 cmsg
= CMSG_FIRSTHDR(&outmessage
);
363 cmsg
->cmsg_level
= IPPROTO_SCTP
;
364 cmsg
->cmsg_type
= SCTP_SNDRCV
;
365 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
366 outmessage
.msg_controllen
= cmsg
->cmsg_len
;
367 sinfo
= CMSG_DATA(cmsg
);
368 memset(sinfo
, 0x00, sizeof(struct sctp_sndrcvinfo
));
370 sinfo
->sinfo_flags
|= MSG_EOF
;
371 sinfo
->sinfo_assoc_id
= associd
;
373 ret
= kernel_sendmsg(con
->sock
, &outmessage
, NULL
, 0, 0);
376 log_print("send EOF to node failed: %d", ret
);
379 /* INIT failed but we don't know which node...
380 restart INIT on all pending nodes */
381 static void sctp_init_failed(void)
384 struct connection
*con
;
386 mutex_lock(&connections_lock
);
387 for (i
=1; i
<=max_nodeid
; i
++) {
388 con
= __nodeid2con(i
, 0);
392 if (test_and_clear_bit(CF_CONNECT_PENDING
, &con
->flags
)) {
393 if (!test_and_set_bit(CF_WRITE_PENDING
, &con
->flags
)) {
394 queue_work(send_workqueue
, &con
->swork
);
398 mutex_unlock(&connections_lock
);
401 /* Something happened to an association */
402 static void process_sctp_notification(struct connection
*con
,
403 struct msghdr
*msg
, char *buf
)
405 union sctp_notification
*sn
= (union sctp_notification
*)buf
;
407 if (sn
->sn_header
.sn_type
== SCTP_ASSOC_CHANGE
) {
408 switch (sn
->sn_assoc_change
.sac_state
) {
413 /* Check that the new node is in the lockspace */
414 struct sctp_prim prim
;
418 struct connection
*new_con
;
420 sctp_peeloff_arg_t parg
;
421 int parglen
= sizeof(parg
);
424 * We get this before any data for an association.
425 * We verify that the node is in the cluster and
426 * then peel off a socket for it.
428 if ((int)sn
->sn_assoc_change
.sac_assoc_id
<= 0) {
429 log_print("COMM_UP for invalid assoc ID %d",
430 (int)sn
->sn_assoc_change
.sac_assoc_id
);
434 memset(&prim
, 0, sizeof(struct sctp_prim
));
435 prim_len
= sizeof(struct sctp_prim
);
436 prim
.ssp_assoc_id
= sn
->sn_assoc_change
.sac_assoc_id
;
438 ret
= kernel_getsockopt(con
->sock
,
444 log_print("getsockopt/sctp_primary_addr on "
445 "new assoc %d failed : %d",
446 (int)sn
->sn_assoc_change
.sac_assoc_id
,
449 /* Retry INIT later */
450 new_con
= assoc2con(sn
->sn_assoc_change
.sac_assoc_id
);
452 clear_bit(CF_CONNECT_PENDING
, &con
->flags
);
455 make_sockaddr(&prim
.ssp_addr
, 0, &addr_len
);
456 if (dlm_addr_to_nodeid(&prim
.ssp_addr
, &nodeid
)) {
458 unsigned char *b
=(unsigned char *)&prim
.ssp_addr
;
459 log_print("reject connect from unknown addr");
460 for (i
=0; i
<sizeof(struct sockaddr_storage
);i
++)
461 printk("%02x ", b
[i
]);
463 sctp_send_shutdown(prim
.ssp_assoc_id
);
467 new_con
= nodeid2con(nodeid
, GFP_KERNEL
);
471 /* Peel off a new sock */
472 parg
.associd
= sn
->sn_assoc_change
.sac_assoc_id
;
473 ret
= kernel_getsockopt(con
->sock
, IPPROTO_SCTP
,
474 SCTP_SOCKOPT_PEELOFF
,
475 (void *)&parg
, &parglen
);
477 log_print("Can't peel off a socket for "
478 "connection %d to node %d: err=%d\n",
479 parg
.associd
, nodeid
, ret
);
481 file
= fget(parg
.sd
);
482 new_con
->sock
= SOCKET_I(file
->f_dentry
->d_inode
);
483 add_sock(new_con
->sock
, new_con
);
485 put_unused_fd(parg
.sd
);
487 log_print("got new/restarted association %d nodeid %d",
488 (int)sn
->sn_assoc_change
.sac_assoc_id
, nodeid
);
490 /* Send any pending writes */
491 clear_bit(CF_CONNECT_PENDING
, &new_con
->flags
);
492 clear_bit(CF_INIT_PENDING
, &con
->flags
);
493 if (!test_and_set_bit(CF_WRITE_PENDING
, &new_con
->flags
)) {
494 queue_work(send_workqueue
, &new_con
->swork
);
496 if (!test_and_set_bit(CF_READ_PENDING
, &new_con
->flags
))
497 queue_work(recv_workqueue
, &new_con
->rwork
);
502 case SCTP_SHUTDOWN_COMP
:
504 con
= assoc2con(sn
->sn_assoc_change
.sac_assoc_id
);
511 /* We don't know which INIT failed, so clear the PENDING flags
512 * on them all. if assoc_id is zero then it will then try
515 case SCTP_CANT_STR_ASSOC
:
517 log_print("Can't start SCTP association - retrying");
523 log_print("unexpected SCTP assoc change id=%d state=%d",
524 (int)sn
->sn_assoc_change
.sac_assoc_id
,
525 sn
->sn_assoc_change
.sac_state
);
530 /* Data received from remote end */
531 static int receive_from_sock(struct connection
*con
)
534 struct msghdr msg
= {};
538 int call_again_soon
= 0;
540 char incmsg
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
542 mutex_lock(&con
->sock_mutex
);
544 if (con
->sock
== NULL
) {
549 if (con
->rx_page
== NULL
) {
551 * This doesn't need to be atomic, but I think it should
552 * improve performance if it is.
554 con
->rx_page
= alloc_page(GFP_ATOMIC
);
555 if (con
->rx_page
== NULL
)
557 cbuf_init(&con
->cb
, PAGE_CACHE_SIZE
);
560 /* Only SCTP needs these really */
561 memset(&incmsg
, 0, sizeof(incmsg
));
562 msg
.msg_control
= incmsg
;
563 msg
.msg_controllen
= sizeof(incmsg
);
566 * iov[0] is the bit of the circular buffer between the current end
567 * point (cb.base + cb.len) and the end of the buffer.
569 iov
[0].iov_len
= con
->cb
.base
- cbuf_data(&con
->cb
);
570 iov
[0].iov_base
= page_address(con
->rx_page
) + cbuf_data(&con
->cb
);
575 * iov[1] is the bit of the circular buffer between the start of the
576 * buffer and the start of the currently used section (cb.base)
578 if (cbuf_data(&con
->cb
) >= con
->cb
.base
) {
579 iov
[0].iov_len
= PAGE_CACHE_SIZE
- cbuf_data(&con
->cb
);
580 iov
[1].iov_len
= con
->cb
.base
;
581 iov
[1].iov_base
= page_address(con
->rx_page
);
584 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
586 r
= ret
= kernel_recvmsg(con
->sock
, &msg
, iov
, nvec
, len
,
587 MSG_DONTWAIT
| MSG_NOSIGNAL
);
591 /* Process SCTP notifications */
592 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
593 msg
.msg_control
= incmsg
;
594 msg
.msg_controllen
= sizeof(incmsg
);
596 process_sctp_notification(con
, &msg
,
597 page_address(con
->rx_page
) + con
->cb
.base
);
598 mutex_unlock(&con
->sock_mutex
);
601 BUG_ON(con
->nodeid
== 0);
605 cbuf_add(&con
->cb
, ret
);
606 ret
= dlm_process_incoming_buffer(con
->nodeid
,
607 page_address(con
->rx_page
),
608 con
->cb
.base
, con
->cb
.len
,
610 if (ret
== -EBADMSG
) {
611 log_print("lowcomms: addr=%p, base=%u, len=%u, "
612 "iov_len=%u, iov_base[0]=%p, read=%d",
613 page_address(con
->rx_page
), con
->cb
.base
, con
->cb
.len
,
614 len
, iov
[0].iov_base
, r
);
618 cbuf_eat(&con
->cb
, ret
);
620 if (cbuf_empty(&con
->cb
) && !call_again_soon
) {
621 __free_page(con
->rx_page
);
627 mutex_unlock(&con
->sock_mutex
);
631 if (!test_and_set_bit(CF_READ_PENDING
, &con
->flags
))
632 queue_work(recv_workqueue
, &con
->rwork
);
633 mutex_unlock(&con
->sock_mutex
);
637 mutex_unlock(&con
->sock_mutex
);
638 if (ret
!= -EAGAIN
) {
639 close_connection(con
, false);
640 /* Reconnect when there is something to send */
642 /* Don't return success if we really got EOF */
649 /* Listening socket is busy, accept a connection */
650 static int tcp_accept_from_sock(struct connection
*con
)
653 struct sockaddr_storage peeraddr
;
654 struct socket
*newsock
;
657 struct connection
*newcon
;
658 struct connection
*addcon
;
660 memset(&peeraddr
, 0, sizeof(peeraddr
));
661 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_STREAM
,
662 IPPROTO_TCP
, &newsock
);
666 mutex_lock_nested(&con
->sock_mutex
, 0);
669 if (con
->sock
== NULL
)
672 newsock
->type
= con
->sock
->type
;
673 newsock
->ops
= con
->sock
->ops
;
675 result
= con
->sock
->ops
->accept(con
->sock
, newsock
, O_NONBLOCK
);
679 /* Get the connected socket's peer */
680 memset(&peeraddr
, 0, sizeof(peeraddr
));
681 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&peeraddr
,
683 result
= -ECONNABORTED
;
687 /* Get the new node's NODEID */
688 make_sockaddr(&peeraddr
, 0, &len
);
689 if (dlm_addr_to_nodeid(&peeraddr
, &nodeid
)) {
690 log_print("connect from non cluster node");
691 sock_release(newsock
);
692 mutex_unlock(&con
->sock_mutex
);
696 log_print("got connection from %d", nodeid
);
698 /* Check to see if we already have a connection to this node. This
699 * could happen if the two nodes initiate a connection at roughly
700 * the same time and the connections cross on the wire.
701 * In this case we store the incoming one in "othercon"
703 newcon
= nodeid2con(nodeid
, GFP_KERNEL
);
708 mutex_lock_nested(&newcon
->sock_mutex
, 1);
710 struct connection
*othercon
= newcon
->othercon
;
713 othercon
= kmem_cache_zalloc(con_cache
, GFP_KERNEL
);
715 log_print("failed to allocate incoming socket");
716 mutex_unlock(&newcon
->sock_mutex
);
720 othercon
->nodeid
= nodeid
;
721 othercon
->rx_action
= receive_from_sock
;
722 mutex_init(&othercon
->sock_mutex
);
723 INIT_WORK(&othercon
->swork
, process_send_sockets
);
724 INIT_WORK(&othercon
->rwork
, process_recv_sockets
);
725 set_bit(CF_IS_OTHERCON
, &othercon
->flags
);
727 if (!othercon
->sock
) {
728 newcon
->othercon
= othercon
;
729 othercon
->sock
= newsock
;
730 newsock
->sk
->sk_user_data
= othercon
;
731 add_sock(newsock
, othercon
);
735 printk("Extra connection from node %d attempted\n", nodeid
);
737 mutex_unlock(&newcon
->sock_mutex
);
742 newsock
->sk
->sk_user_data
= newcon
;
743 newcon
->rx_action
= receive_from_sock
;
744 add_sock(newsock
, newcon
);
748 mutex_unlock(&newcon
->sock_mutex
);
751 * Add it to the active queue in case we got data
752 * beween processing the accept adding the socket
753 * to the read_sockets list
755 if (!test_and_set_bit(CF_READ_PENDING
, &addcon
->flags
))
756 queue_work(recv_workqueue
, &addcon
->rwork
);
757 mutex_unlock(&con
->sock_mutex
);
762 mutex_unlock(&con
->sock_mutex
);
763 sock_release(newsock
);
765 if (result
!= -EAGAIN
)
766 log_print("error accepting connection from node: %d", result
);
770 static void free_entry(struct writequeue_entry
*e
)
772 __free_page(e
->page
);
776 /* Initiate an SCTP association.
777 This is a special case of send_to_sock() in that we don't yet have a
778 peeled-off socket for this association, so we use the listening socket
779 and add the primary IP address of the remote node.
781 static void sctp_init_assoc(struct connection
*con
)
783 struct sockaddr_storage rem_addr
;
784 char outcmsg
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
785 struct msghdr outmessage
;
786 struct cmsghdr
*cmsg
;
787 struct sctp_sndrcvinfo
*sinfo
;
788 struct connection
*base_con
;
789 struct writequeue_entry
*e
;
795 if (test_and_set_bit(CF_INIT_PENDING
, &con
->flags
))
798 if (con
->retries
++ > MAX_CONNECT_RETRIES
)
801 log_print("Initiating association with node %d", con
->nodeid
);
803 if (nodeid_to_addr(con
->nodeid
, (struct sockaddr
*)&rem_addr
)) {
804 log_print("no address for nodeid %d", con
->nodeid
);
807 base_con
= nodeid2con(0, 0);
808 BUG_ON(base_con
== NULL
);
810 make_sockaddr(&rem_addr
, dlm_config
.ci_tcp_port
, &addrlen
);
812 outmessage
.msg_name
= &rem_addr
;
813 outmessage
.msg_namelen
= addrlen
;
814 outmessage
.msg_control
= outcmsg
;
815 outmessage
.msg_controllen
= sizeof(outcmsg
);
816 outmessage
.msg_flags
= MSG_EOR
;
818 spin_lock(&con
->writequeue_lock
);
819 e
= list_entry(con
->writequeue
.next
, struct writequeue_entry
,
822 BUG_ON((struct list_head
*) e
== &con
->writequeue
);
826 spin_unlock(&con
->writequeue_lock
);
828 /* Send the first block off the write queue */
829 iov
[0].iov_base
= page_address(e
->page
)+offset
;
830 iov
[0].iov_len
= len
;
832 cmsg
= CMSG_FIRSTHDR(&outmessage
);
833 cmsg
->cmsg_level
= IPPROTO_SCTP
;
834 cmsg
->cmsg_type
= SCTP_SNDRCV
;
835 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
836 sinfo
= CMSG_DATA(cmsg
);
837 memset(sinfo
, 0x00, sizeof(struct sctp_sndrcvinfo
));
838 sinfo
->sinfo_ppid
= cpu_to_le32(dlm_our_nodeid());
839 outmessage
.msg_controllen
= cmsg
->cmsg_len
;
841 ret
= kernel_sendmsg(base_con
->sock
, &outmessage
, iov
, 1, len
);
843 log_print("Send first packet to node %d failed: %d",
846 /* Try again later */
847 clear_bit(CF_CONNECT_PENDING
, &con
->flags
);
848 clear_bit(CF_INIT_PENDING
, &con
->flags
);
851 spin_lock(&con
->writequeue_lock
);
855 if (e
->len
== 0 && e
->users
== 0) {
859 spin_unlock(&con
->writequeue_lock
);
863 /* Connect a new socket to its peer */
864 static void tcp_connect_to_sock(struct connection
*con
)
866 int result
= -EHOSTUNREACH
;
867 struct sockaddr_storage saddr
, src_addr
;
871 if (con
->nodeid
== 0) {
872 log_print("attempt to connect sock 0 foiled");
876 mutex_lock(&con
->sock_mutex
);
877 if (con
->retries
++ > MAX_CONNECT_RETRIES
)
880 /* Some odd races can cause double-connects, ignore them */
886 /* Create a socket to communicate with */
887 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_STREAM
,
892 memset(&saddr
, 0, sizeof(saddr
));
893 if (dlm_nodeid_to_addr(con
->nodeid
, &saddr
)) {
898 sock
->sk
->sk_user_data
= con
;
899 con
->rx_action
= receive_from_sock
;
900 con
->connect_action
= tcp_connect_to_sock
;
903 /* Bind to our cluster-known address connecting to avoid
905 memcpy(&src_addr
, dlm_local_addr
[0], sizeof(src_addr
));
906 make_sockaddr(&src_addr
, 0, &addr_len
);
907 result
= sock
->ops
->bind(sock
, (struct sockaddr
*) &src_addr
,
910 log_print("could not bind for connect: %d", result
);
911 /* This *may* not indicate a critical error */
914 make_sockaddr(&saddr
, dlm_config
.ci_tcp_port
, &addr_len
);
916 log_print("connecting to %d", con
->nodeid
);
918 sock
->ops
->connect(sock
, (struct sockaddr
*)&saddr
, addr_len
,
920 if (result
== -EINPROGRESS
)
927 sock_release(con
->sock
);
931 * Some errors are fatal and this list might need adjusting. For other
932 * errors we try again until the max number of retries is reached.
934 if (result
!= -EHOSTUNREACH
&& result
!= -ENETUNREACH
&&
935 result
!= -ENETDOWN
&& result
!= -EINVAL
936 && result
!= -EPROTONOSUPPORT
) {
937 lowcomms_connect_sock(con
);
941 mutex_unlock(&con
->sock_mutex
);
945 static struct socket
*tcp_create_listen_sock(struct connection
*con
,
946 struct sockaddr_storage
*saddr
)
948 struct socket
*sock
= NULL
;
953 if (dlm_local_addr
[0]->ss_family
== AF_INET
)
954 addr_len
= sizeof(struct sockaddr_in
);
956 addr_len
= sizeof(struct sockaddr_in6
);
958 /* Create a socket to communicate with */
959 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_STREAM
,
962 log_print("Can't create listening comms socket");
966 result
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
,
967 (char *)&one
, sizeof(one
));
970 log_print("Failed to set SO_REUSEADDR on socket: %d", result
);
972 sock
->sk
->sk_user_data
= con
;
973 con
->rx_action
= tcp_accept_from_sock
;
974 con
->connect_action
= tcp_connect_to_sock
;
977 /* Bind to our port */
978 make_sockaddr(saddr
, dlm_config
.ci_tcp_port
, &addr_len
);
979 result
= sock
->ops
->bind(sock
, (struct sockaddr
*) saddr
, addr_len
);
981 log_print("Can't bind to port %d", dlm_config
.ci_tcp_port
);
987 result
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
988 (char *)&one
, sizeof(one
));
990 log_print("Set keepalive failed: %d", result
);
993 result
= sock
->ops
->listen(sock
, 5);
995 log_print("Can't listen on port %d", dlm_config
.ci_tcp_port
);
1005 /* Get local addresses */
1006 static void init_local(void)
1008 struct sockaddr_storage sas
, *addr
;
1011 dlm_local_count
= 0;
1012 for (i
= 0; i
< DLM_MAX_ADDR_COUNT
- 1; i
++) {
1013 if (dlm_our_addr(&sas
, i
))
1016 addr
= kmalloc(sizeof(*addr
), GFP_KERNEL
);
1019 memcpy(addr
, &sas
, sizeof(*addr
));
1020 dlm_local_addr
[dlm_local_count
++] = addr
;
1024 /* Bind to an IP address. SCTP allows multiple address so it can do
1026 static int add_sctp_bind_addr(struct connection
*sctp_con
,
1027 struct sockaddr_storage
*addr
,
1028 int addr_len
, int num
)
1033 result
= kernel_bind(sctp_con
->sock
,
1034 (struct sockaddr
*) addr
,
1037 result
= kernel_setsockopt(sctp_con
->sock
, SOL_SCTP
,
1038 SCTP_SOCKOPT_BINDX_ADD
,
1039 (char *)addr
, addr_len
);
1042 log_print("Can't bind to port %d addr number %d",
1043 dlm_config
.ci_tcp_port
, num
);
1048 /* Initialise SCTP socket and bind to all interfaces */
1049 static int sctp_listen_for_all(void)
1051 struct socket
*sock
= NULL
;
1052 struct sockaddr_storage localaddr
;
1053 struct sctp_event_subscribe subscribe
;
1054 int result
= -EINVAL
, num
= 1, i
, addr_len
;
1055 struct connection
*con
= nodeid2con(0, GFP_KERNEL
);
1056 int bufsize
= NEEDED_RMEM
;
1061 log_print("Using SCTP for communications");
1063 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_SEQPACKET
,
1064 IPPROTO_SCTP
, &sock
);
1066 log_print("Can't create comms socket, check SCTP is loaded");
1070 /* Listen for events */
1071 memset(&subscribe
, 0, sizeof(subscribe
));
1072 subscribe
.sctp_data_io_event
= 1;
1073 subscribe
.sctp_association_event
= 1;
1074 subscribe
.sctp_send_failure_event
= 1;
1075 subscribe
.sctp_shutdown_event
= 1;
1076 subscribe
.sctp_partial_delivery_event
= 1;
1078 result
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
1079 (char *)&bufsize
, sizeof(bufsize
));
1081 log_print("Error increasing buffer space on socket %d", result
);
1083 result
= kernel_setsockopt(sock
, SOL_SCTP
, SCTP_EVENTS
,
1084 (char *)&subscribe
, sizeof(subscribe
));
1086 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1088 goto create_delsock
;
1091 /* Init con struct */
1092 sock
->sk
->sk_user_data
= con
;
1094 con
->sock
->sk
->sk_data_ready
= lowcomms_data_ready
;
1095 con
->rx_action
= receive_from_sock
;
1096 con
->connect_action
= sctp_init_assoc
;
1098 /* Bind to all interfaces. */
1099 for (i
= 0; i
< dlm_local_count
; i
++) {
1100 memcpy(&localaddr
, dlm_local_addr
[i
], sizeof(localaddr
));
1101 make_sockaddr(&localaddr
, dlm_config
.ci_tcp_port
, &addr_len
);
1103 result
= add_sctp_bind_addr(con
, &localaddr
, addr_len
, num
);
1105 goto create_delsock
;
1109 result
= sock
->ops
->listen(sock
, 5);
1111 log_print("Can't set socket listening");
1112 goto create_delsock
;
1124 static int tcp_listen_for_all(void)
1126 struct socket
*sock
= NULL
;
1127 struct connection
*con
= nodeid2con(0, GFP_KERNEL
);
1128 int result
= -EINVAL
;
1133 /* We don't support multi-homed hosts */
1134 if (dlm_local_addr
[1] != NULL
) {
1135 log_print("TCP protocol can't handle multi-homed hosts, "
1140 log_print("Using TCP for communications");
1142 sock
= tcp_create_listen_sock(con
, dlm_local_addr
[0]);
1144 add_sock(sock
, con
);
1148 result
= -EADDRINUSE
;
1156 static struct writequeue_entry
*new_writequeue_entry(struct connection
*con
,
1159 struct writequeue_entry
*entry
;
1161 entry
= kmalloc(sizeof(struct writequeue_entry
), allocation
);
1165 entry
->page
= alloc_page(allocation
);
1180 void *dlm_lowcomms_get_buffer(int nodeid
, int len
, gfp_t allocation
, char **ppc
)
1182 struct connection
*con
;
1183 struct writequeue_entry
*e
;
1187 con
= nodeid2con(nodeid
, allocation
);
1191 spin_lock(&con
->writequeue_lock
);
1192 e
= list_entry(con
->writequeue
.prev
, struct writequeue_entry
, list
);
1193 if ((&e
->list
== &con
->writequeue
) ||
1194 (PAGE_CACHE_SIZE
- e
->end
< len
)) {
1201 spin_unlock(&con
->writequeue_lock
);
1205 *ppc
= page_address(e
->page
) + offset
;
1209 e
= new_writequeue_entry(con
, allocation
);
1211 spin_lock(&con
->writequeue_lock
);
1215 list_add_tail(&e
->list
, &con
->writequeue
);
1216 spin_unlock(&con
->writequeue_lock
);
1222 void dlm_lowcomms_commit_buffer(void *mh
)
1224 struct writequeue_entry
*e
= (struct writequeue_entry
*)mh
;
1225 struct connection
*con
= e
->con
;
1228 spin_lock(&con
->writequeue_lock
);
1232 e
->len
= e
->end
- e
->offset
;
1233 spin_unlock(&con
->writequeue_lock
);
1235 if (!test_and_set_bit(CF_WRITE_PENDING
, &con
->flags
)) {
1236 queue_work(send_workqueue
, &con
->swork
);
1241 spin_unlock(&con
->writequeue_lock
);
1245 /* Send a message */
1246 static void send_to_sock(struct connection
*con
)
1249 ssize_t(*sendpage
) (struct socket
*, struct page
*, int, size_t, int);
1250 const int msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
;
1251 struct writequeue_entry
*e
;
1254 mutex_lock(&con
->sock_mutex
);
1255 if (con
->sock
== NULL
)
1258 sendpage
= con
->sock
->ops
->sendpage
;
1260 spin_lock(&con
->writequeue_lock
);
1262 e
= list_entry(con
->writequeue
.next
, struct writequeue_entry
,
1264 if ((struct list_head
*) e
== &con
->writequeue
)
1269 BUG_ON(len
== 0 && e
->users
== 0);
1270 spin_unlock(&con
->writequeue_lock
);
1274 ret
= sendpage(con
->sock
, e
->page
, offset
, len
,
1276 if (ret
== -EAGAIN
|| ret
== 0) {
1283 /* Don't starve people filling buffers */
1286 spin_lock(&con
->writequeue_lock
);
1290 if (e
->len
== 0 && e
->users
== 0) {
1296 spin_unlock(&con
->writequeue_lock
);
1298 mutex_unlock(&con
->sock_mutex
);
1302 mutex_unlock(&con
->sock_mutex
);
1303 close_connection(con
, false);
1304 lowcomms_connect_sock(con
);
1308 mutex_unlock(&con
->sock_mutex
);
1309 if (!test_bit(CF_INIT_PENDING
, &con
->flags
))
1310 lowcomms_connect_sock(con
);
1314 static void clean_one_writequeue(struct connection
*con
)
1316 struct list_head
*list
;
1317 struct list_head
*temp
;
1319 spin_lock(&con
->writequeue_lock
);
1320 list_for_each_safe(list
, temp
, &con
->writequeue
) {
1321 struct writequeue_entry
*e
=
1322 list_entry(list
, struct writequeue_entry
, list
);
1326 spin_unlock(&con
->writequeue_lock
);
1329 /* Called from recovery when it knows that a node has
1331 int dlm_lowcomms_close(int nodeid
)
1333 struct connection
*con
;
1335 log_print("closing connection to node %d", nodeid
);
1336 con
= nodeid2con(nodeid
, 0);
1338 clean_one_writequeue(con
);
1339 close_connection(con
, true);
1344 /* Receive workqueue function */
1345 static void process_recv_sockets(struct work_struct
*work
)
1347 struct connection
*con
= container_of(work
, struct connection
, rwork
);
1350 clear_bit(CF_READ_PENDING
, &con
->flags
);
1352 err
= con
->rx_action(con
);
1356 /* Send workqueue function */
1357 static void process_send_sockets(struct work_struct
*work
)
1359 struct connection
*con
= container_of(work
, struct connection
, swork
);
1361 if (test_and_clear_bit(CF_CONNECT_PENDING
, &con
->flags
)) {
1362 con
->connect_action(con
);
1364 clear_bit(CF_WRITE_PENDING
, &con
->flags
);
1369 /* Discard all entries on the write queues */
1370 static void clean_writequeues(void)
1374 for (nodeid
= 1; nodeid
<= max_nodeid
; nodeid
++) {
1375 struct connection
*con
= __nodeid2con(nodeid
, 0);
1378 clean_one_writequeue(con
);
1382 static void work_stop(void)
1384 destroy_workqueue(recv_workqueue
);
1385 destroy_workqueue(send_workqueue
);
1388 static int work_start(void)
1391 recv_workqueue
= create_workqueue("dlm_recv");
1392 error
= IS_ERR(recv_workqueue
);
1394 log_print("can't start dlm_recv %d", error
);
1398 send_workqueue
= create_singlethread_workqueue("dlm_send");
1399 error
= IS_ERR(send_workqueue
);
1401 log_print("can't start dlm_send %d", error
);
1402 destroy_workqueue(recv_workqueue
);
1409 void dlm_lowcomms_stop(void)
1412 struct connection
*con
;
1414 /* Set all the flags to prevent any
1417 mutex_lock(&connections_lock
);
1418 for (i
= 0; i
<= max_nodeid
; i
++) {
1419 con
= __nodeid2con(i
, 0);
1423 con
->sock
->sk
->sk_user_data
= NULL
;
1426 mutex_unlock(&connections_lock
);
1430 mutex_lock(&connections_lock
);
1431 clean_writequeues();
1433 for (i
= 0; i
<= max_nodeid
; i
++) {
1434 con
= __nodeid2con(i
, 0);
1436 close_connection(con
, true);
1438 kmem_cache_free(con_cache
, con
->othercon
);
1439 kmem_cache_free(con_cache
, con
);
1443 mutex_unlock(&connections_lock
);
1444 kmem_cache_destroy(con_cache
);
1445 idr_init(&connections_idr
);
1448 int dlm_lowcomms_start(void)
1450 int error
= -EINVAL
;
1451 struct connection
*con
;
1454 if (!dlm_local_count
) {
1456 log_print("no local IP address has been set");
1461 con_cache
= kmem_cache_create("dlm_conn", sizeof(struct connection
),
1462 __alignof__(struct connection
), 0,
1467 /* Start listening */
1468 if (dlm_config
.ci_protocol
== 0)
1469 error
= tcp_listen_for_all();
1471 error
= sctp_listen_for_all();
1475 error
= work_start();
1482 con
= nodeid2con(0,0);
1484 close_connection(con
, false);
1485 kmem_cache_free(con_cache
, con
);
1487 kmem_cache_destroy(con_cache
);