1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 ** This copyrighted material is made available to anyone wishing to use,
8 ** modify, copy, or redistribute it subject to the terms and conditions
9 ** of the GNU General Public License v.2.
11 *******************************************************************************
12 ******************************************************************************/
17 * This is the "low-level" comms layer.
19 * It is responsible for sending/receiving messages
20 * from other nodes in the cluster.
22 * Cluster nodes are referred to by their nodeids. nodeids are
23 * simply 32 bit numbers to the locking module - if they need to
24 * be expanded for the cluster infrastructure then that is its
25 * responsibility. It is this layer's
26 * responsibility to resolve these into IP address or
27 * whatever it needs for inter-node communication.
29 * The comms level is two kernel threads that deal mainly with
30 * the receiving of messages from other nodes and passing them
31 * up to the mid-level comms layer (which understands the
32 * message format) for execution by the locking core, and
33 * a send thread which does all the setting up of connections
34 * to remote nodes and the sending of data. Threads are not allowed
35 * to send their own data because it may cause them to wait in times
36 * of high load. Also, this way, the sending thread can collect together
37 * messages bound for one node and send them in one block.
39 * lowcomms will choose to use either TCP or SCTP as its transport layer
40 * depending on the configuration variable 'protocol'. This should be set
41 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
42 * cluster-wide mechanism as it must be the same on all nodes of the cluster
43 * for the DLM to function.
47 #include <asm/ioctls.h>
50 #include <linux/pagemap.h>
51 #include <linux/file.h>
52 #include <linux/mutex.h>
53 #include <linux/sctp.h>
54 #include <linux/slab.h>
55 #include <linux/sctp.h>
56 #include <net/sctp/sctp.h>
59 #include "dlm_internal.h"
64 #define NEEDED_RMEM (4*1024*1024)
65 #define CONN_HASH_SIZE 32
67 /* Number of messages to send before rescheduling */
68 #define MAX_SEND_MSG_COUNT 25
76 static void cbuf_add(struct cbuf
*cb
, int n
)
81 static int cbuf_data(struct cbuf
*cb
)
83 return ((cb
->base
+ cb
->len
) & cb
->mask
);
86 static void cbuf_init(struct cbuf
*cb
, int size
)
88 cb
->base
= cb
->len
= 0;
92 static void cbuf_eat(struct cbuf
*cb
, int n
)
99 static bool cbuf_empty(struct cbuf
*cb
)
105 struct socket
*sock
; /* NULL if not connected */
106 uint32_t nodeid
; /* So we know who we are in the list */
107 struct mutex sock_mutex
;
109 #define CF_READ_PENDING 1
110 #define CF_WRITE_PENDING 2
111 #define CF_CONNECT_PENDING 3
112 #define CF_INIT_PENDING 4
113 #define CF_IS_OTHERCON 5
115 #define CF_APP_LIMITED 7
116 struct list_head writequeue
; /* List of outgoing writequeue_entries */
117 spinlock_t writequeue_lock
;
118 int (*rx_action
) (struct connection
*); /* What to do when active */
119 void (*connect_action
) (struct connection
*); /* What to do to connect */
120 struct page
*rx_page
;
123 #define MAX_CONNECT_RETRIES 3
125 struct hlist_node list
;
126 struct connection
*othercon
;
127 struct work_struct rwork
; /* Receive workqueue */
128 struct work_struct swork
; /* Send workqueue */
130 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
132 /* An entry waiting to be sent */
133 struct writequeue_entry
{
134 struct list_head list
;
140 struct connection
*con
;
143 struct dlm_node_addr
{
144 struct list_head list
;
147 struct sockaddr_storage
*addr
[DLM_MAX_ADDR_COUNT
];
150 static LIST_HEAD(dlm_node_addrs
);
151 static DEFINE_SPINLOCK(dlm_node_addrs_spin
);
153 static struct sockaddr_storage
*dlm_local_addr
[DLM_MAX_ADDR_COUNT
];
154 static int dlm_local_count
;
155 static int dlm_allow_conn
;
158 static struct workqueue_struct
*recv_workqueue
;
159 static struct workqueue_struct
*send_workqueue
;
161 static struct hlist_head connection_hash
[CONN_HASH_SIZE
];
162 static DEFINE_MUTEX(connections_lock
);
163 static struct kmem_cache
*con_cache
;
165 static void process_recv_sockets(struct work_struct
*work
);
166 static void process_send_sockets(struct work_struct
*work
);
169 /* This is deliberately very simple because most clusters have simple
170 sequential nodeids, so we should be able to go straight to a connection
171 struct in the array */
172 static inline int nodeid_hash(int nodeid
)
174 return nodeid
& (CONN_HASH_SIZE
-1);
177 static struct connection
*__find_con(int nodeid
)
180 struct connection
*con
;
182 r
= nodeid_hash(nodeid
);
184 hlist_for_each_entry(con
, &connection_hash
[r
], list
) {
185 if (con
->nodeid
== nodeid
)
192 * If 'allocation' is zero then we don't attempt to create a new
193 * connection structure for this node.
195 static struct connection
*__nodeid2con(int nodeid
, gfp_t alloc
)
197 struct connection
*con
= NULL
;
200 con
= __find_con(nodeid
);
204 con
= kmem_cache_zalloc(con_cache
, alloc
);
208 r
= nodeid_hash(nodeid
);
209 hlist_add_head(&con
->list
, &connection_hash
[r
]);
211 con
->nodeid
= nodeid
;
212 mutex_init(&con
->sock_mutex
);
213 INIT_LIST_HEAD(&con
->writequeue
);
214 spin_lock_init(&con
->writequeue_lock
);
215 INIT_WORK(&con
->swork
, process_send_sockets
);
216 INIT_WORK(&con
->rwork
, process_recv_sockets
);
218 /* Setup action pointers for child sockets */
220 struct connection
*zerocon
= __find_con(0);
222 con
->connect_action
= zerocon
->connect_action
;
224 con
->rx_action
= zerocon
->rx_action
;
230 /* Loop round all connections */
231 static void foreach_conn(void (*conn_func
)(struct connection
*c
))
234 struct hlist_node
*n
;
235 struct connection
*con
;
237 for (i
= 0; i
< CONN_HASH_SIZE
; i
++) {
238 hlist_for_each_entry_safe(con
, n
, &connection_hash
[i
], list
)
243 static struct connection
*nodeid2con(int nodeid
, gfp_t allocation
)
245 struct connection
*con
;
247 mutex_lock(&connections_lock
);
248 con
= __nodeid2con(nodeid
, allocation
);
249 mutex_unlock(&connections_lock
);
254 /* This is a bit drastic, but only called when things go wrong */
255 static struct connection
*assoc2con(int assoc_id
)
258 struct connection
*con
;
260 mutex_lock(&connections_lock
);
262 for (i
= 0 ; i
< CONN_HASH_SIZE
; i
++) {
263 hlist_for_each_entry(con
, &connection_hash
[i
], list
) {
264 if (con
->sctp_assoc
== assoc_id
) {
265 mutex_unlock(&connections_lock
);
270 mutex_unlock(&connections_lock
);
274 static struct dlm_node_addr
*find_node_addr(int nodeid
)
276 struct dlm_node_addr
*na
;
278 list_for_each_entry(na
, &dlm_node_addrs
, list
) {
279 if (na
->nodeid
== nodeid
)
285 static int addr_compare(struct sockaddr_storage
*x
, struct sockaddr_storage
*y
)
287 switch (x
->ss_family
) {
289 struct sockaddr_in
*sinx
= (struct sockaddr_in
*)x
;
290 struct sockaddr_in
*siny
= (struct sockaddr_in
*)y
;
291 if (sinx
->sin_addr
.s_addr
!= siny
->sin_addr
.s_addr
)
293 if (sinx
->sin_port
!= siny
->sin_port
)
298 struct sockaddr_in6
*sinx
= (struct sockaddr_in6
*)x
;
299 struct sockaddr_in6
*siny
= (struct sockaddr_in6
*)y
;
300 if (!ipv6_addr_equal(&sinx
->sin6_addr
, &siny
->sin6_addr
))
302 if (sinx
->sin6_port
!= siny
->sin6_port
)
312 static int nodeid_to_addr(int nodeid
, struct sockaddr_storage
*sas_out
,
313 struct sockaddr
*sa_out
)
315 struct sockaddr_storage sas
;
316 struct dlm_node_addr
*na
;
318 if (!dlm_local_count
)
321 spin_lock(&dlm_node_addrs_spin
);
322 na
= find_node_addr(nodeid
);
323 if (na
&& na
->addr_count
)
324 memcpy(&sas
, na
->addr
[0], sizeof(struct sockaddr_storage
));
325 spin_unlock(&dlm_node_addrs_spin
);
334 memcpy(sas_out
, &sas
, sizeof(struct sockaddr_storage
));
339 if (dlm_local_addr
[0]->ss_family
== AF_INET
) {
340 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &sas
;
341 struct sockaddr_in
*ret4
= (struct sockaddr_in
*) sa_out
;
342 ret4
->sin_addr
.s_addr
= in4
->sin_addr
.s_addr
;
344 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*) &sas
;
345 struct sockaddr_in6
*ret6
= (struct sockaddr_in6
*) sa_out
;
346 ret6
->sin6_addr
= in6
->sin6_addr
;
352 static int addr_to_nodeid(struct sockaddr_storage
*addr
, int *nodeid
)
354 struct dlm_node_addr
*na
;
357 spin_lock(&dlm_node_addrs_spin
);
358 list_for_each_entry(na
, &dlm_node_addrs
, list
) {
362 if (!addr_compare(na
->addr
[0], addr
))
365 *nodeid
= na
->nodeid
;
369 spin_unlock(&dlm_node_addrs_spin
);
373 int dlm_lowcomms_addr(int nodeid
, struct sockaddr_storage
*addr
, int len
)
375 struct sockaddr_storage
*new_addr
;
376 struct dlm_node_addr
*new_node
, *na
;
378 new_node
= kzalloc(sizeof(struct dlm_node_addr
), GFP_NOFS
);
382 new_addr
= kzalloc(sizeof(struct sockaddr_storage
), GFP_NOFS
);
388 memcpy(new_addr
, addr
, len
);
390 spin_lock(&dlm_node_addrs_spin
);
391 na
= find_node_addr(nodeid
);
393 new_node
->nodeid
= nodeid
;
394 new_node
->addr
[0] = new_addr
;
395 new_node
->addr_count
= 1;
396 list_add(&new_node
->list
, &dlm_node_addrs
);
397 spin_unlock(&dlm_node_addrs_spin
);
401 if (na
->addr_count
>= DLM_MAX_ADDR_COUNT
) {
402 spin_unlock(&dlm_node_addrs_spin
);
408 na
->addr
[na
->addr_count
++] = new_addr
;
409 spin_unlock(&dlm_node_addrs_spin
);
414 /* Data available on socket or listen socket received a connect */
415 static void lowcomms_data_ready(struct sock
*sk
, int count_unused
)
417 struct connection
*con
= sock2con(sk
);
418 if (con
&& !test_and_set_bit(CF_READ_PENDING
, &con
->flags
))
419 queue_work(recv_workqueue
, &con
->rwork
);
422 static void lowcomms_write_space(struct sock
*sk
)
424 struct connection
*con
= sock2con(sk
);
429 clear_bit(SOCK_NOSPACE
, &con
->sock
->flags
);
431 if (test_and_clear_bit(CF_APP_LIMITED
, &con
->flags
)) {
432 con
->sock
->sk
->sk_write_pending
--;
433 clear_bit(SOCK_ASYNC_NOSPACE
, &con
->sock
->flags
);
436 if (!test_and_set_bit(CF_WRITE_PENDING
, &con
->flags
))
437 queue_work(send_workqueue
, &con
->swork
);
440 static inline void lowcomms_connect_sock(struct connection
*con
)
442 if (test_bit(CF_CLOSE
, &con
->flags
))
444 if (!test_and_set_bit(CF_CONNECT_PENDING
, &con
->flags
))
445 queue_work(send_workqueue
, &con
->swork
);
448 static void lowcomms_state_change(struct sock
*sk
)
450 if (sk
->sk_state
== TCP_ESTABLISHED
)
451 lowcomms_write_space(sk
);
454 int dlm_lowcomms_connect_node(int nodeid
)
456 struct connection
*con
;
458 /* with sctp there's no connecting without sending */
459 if (dlm_config
.ci_protocol
!= 0)
462 if (nodeid
== dlm_our_nodeid())
465 con
= nodeid2con(nodeid
, GFP_NOFS
);
468 lowcomms_connect_sock(con
);
472 /* Make a socket active */
473 static void add_sock(struct socket
*sock
, struct connection
*con
)
477 /* Install a data_ready callback */
478 con
->sock
->sk
->sk_data_ready
= lowcomms_data_ready
;
479 con
->sock
->sk
->sk_write_space
= lowcomms_write_space
;
480 con
->sock
->sk
->sk_state_change
= lowcomms_state_change
;
481 con
->sock
->sk
->sk_user_data
= con
;
482 con
->sock
->sk
->sk_allocation
= GFP_NOFS
;
485 /* Add the port number to an IPv6 or 4 sockaddr and return the address
487 static void make_sockaddr(struct sockaddr_storage
*saddr
, uint16_t port
,
490 saddr
->ss_family
= dlm_local_addr
[0]->ss_family
;
491 if (saddr
->ss_family
== AF_INET
) {
492 struct sockaddr_in
*in4_addr
= (struct sockaddr_in
*)saddr
;
493 in4_addr
->sin_port
= cpu_to_be16(port
);
494 *addr_len
= sizeof(struct sockaddr_in
);
495 memset(&in4_addr
->sin_zero
, 0, sizeof(in4_addr
->sin_zero
));
497 struct sockaddr_in6
*in6_addr
= (struct sockaddr_in6
*)saddr
;
498 in6_addr
->sin6_port
= cpu_to_be16(port
);
499 *addr_len
= sizeof(struct sockaddr_in6
);
501 memset((char *)saddr
+ *addr_len
, 0, sizeof(struct sockaddr_storage
) - *addr_len
);
504 /* Close a remote connection and tidy up */
505 static void close_connection(struct connection
*con
, bool and_other
)
507 mutex_lock(&con
->sock_mutex
);
510 sock_release(con
->sock
);
513 if (con
->othercon
&& and_other
) {
514 /* Will only re-enter once. */
515 close_connection(con
->othercon
, false);
518 __free_page(con
->rx_page
);
523 mutex_unlock(&con
->sock_mutex
);
526 /* We only send shutdown messages to nodes that are not part of the cluster */
527 static void sctp_send_shutdown(sctp_assoc_t associd
)
529 static char outcmsg
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
530 struct msghdr outmessage
;
531 struct cmsghdr
*cmsg
;
532 struct sctp_sndrcvinfo
*sinfo
;
534 struct connection
*con
;
536 con
= nodeid2con(0,0);
539 outmessage
.msg_name
= NULL
;
540 outmessage
.msg_namelen
= 0;
541 outmessage
.msg_control
= outcmsg
;
542 outmessage
.msg_controllen
= sizeof(outcmsg
);
543 outmessage
.msg_flags
= MSG_EOR
;
545 cmsg
= CMSG_FIRSTHDR(&outmessage
);
546 cmsg
->cmsg_level
= IPPROTO_SCTP
;
547 cmsg
->cmsg_type
= SCTP_SNDRCV
;
548 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
549 outmessage
.msg_controllen
= cmsg
->cmsg_len
;
550 sinfo
= CMSG_DATA(cmsg
);
551 memset(sinfo
, 0x00, sizeof(struct sctp_sndrcvinfo
));
553 sinfo
->sinfo_flags
|= MSG_EOF
;
554 sinfo
->sinfo_assoc_id
= associd
;
556 ret
= kernel_sendmsg(con
->sock
, &outmessage
, NULL
, 0, 0);
559 log_print("send EOF to node failed: %d", ret
);
562 static void sctp_init_failed_foreach(struct connection
*con
)
565 if (test_and_clear_bit(CF_CONNECT_PENDING
, &con
->flags
)) {
566 if (!test_and_set_bit(CF_WRITE_PENDING
, &con
->flags
))
567 queue_work(send_workqueue
, &con
->swork
);
571 /* INIT failed but we don't know which node...
572 restart INIT on all pending nodes */
573 static void sctp_init_failed(void)
575 mutex_lock(&connections_lock
);
577 foreach_conn(sctp_init_failed_foreach
);
579 mutex_unlock(&connections_lock
);
582 /* Something happened to an association */
583 static void process_sctp_notification(struct connection
*con
,
584 struct msghdr
*msg
, char *buf
)
586 union sctp_notification
*sn
= (union sctp_notification
*)buf
;
588 if (sn
->sn_header
.sn_type
== SCTP_ASSOC_CHANGE
) {
589 switch (sn
->sn_assoc_change
.sac_state
) {
594 /* Check that the new node is in the lockspace */
595 struct sctp_prim prim
;
599 struct connection
*new_con
;
602 * We get this before any data for an association.
603 * We verify that the node is in the cluster and
604 * then peel off a socket for it.
606 if ((int)sn
->sn_assoc_change
.sac_assoc_id
<= 0) {
607 log_print("COMM_UP for invalid assoc ID %d",
608 (int)sn
->sn_assoc_change
.sac_assoc_id
);
612 memset(&prim
, 0, sizeof(struct sctp_prim
));
613 prim_len
= sizeof(struct sctp_prim
);
614 prim
.ssp_assoc_id
= sn
->sn_assoc_change
.sac_assoc_id
;
616 ret
= kernel_getsockopt(con
->sock
,
622 log_print("getsockopt/sctp_primary_addr on "
623 "new assoc %d failed : %d",
624 (int)sn
->sn_assoc_change
.sac_assoc_id
,
627 /* Retry INIT later */
628 new_con
= assoc2con(sn
->sn_assoc_change
.sac_assoc_id
);
630 clear_bit(CF_CONNECT_PENDING
, &con
->flags
);
633 make_sockaddr(&prim
.ssp_addr
, 0, &addr_len
);
634 if (addr_to_nodeid(&prim
.ssp_addr
, &nodeid
)) {
635 unsigned char *b
=(unsigned char *)&prim
.ssp_addr
;
636 log_print("reject connect from unknown addr");
637 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE
,
638 b
, sizeof(struct sockaddr_storage
));
639 sctp_send_shutdown(prim
.ssp_assoc_id
);
643 new_con
= nodeid2con(nodeid
, GFP_NOFS
);
647 /* Peel off a new sock */
648 sctp_lock_sock(con
->sock
->sk
);
649 ret
= sctp_do_peeloff(con
->sock
->sk
,
650 sn
->sn_assoc_change
.sac_assoc_id
,
652 sctp_release_sock(con
->sock
->sk
);
654 log_print("Can't peel off a socket for "
655 "connection %d to node %d: err=%d",
656 (int)sn
->sn_assoc_change
.sac_assoc_id
,
660 add_sock(new_con
->sock
, new_con
);
662 log_print("connecting to %d sctp association %d",
663 nodeid
, (int)sn
->sn_assoc_change
.sac_assoc_id
);
665 /* Send any pending writes */
666 clear_bit(CF_CONNECT_PENDING
, &new_con
->flags
);
667 clear_bit(CF_INIT_PENDING
, &con
->flags
);
668 if (!test_and_set_bit(CF_WRITE_PENDING
, &new_con
->flags
)) {
669 queue_work(send_workqueue
, &new_con
->swork
);
671 if (!test_and_set_bit(CF_READ_PENDING
, &new_con
->flags
))
672 queue_work(recv_workqueue
, &new_con
->rwork
);
677 case SCTP_SHUTDOWN_COMP
:
679 con
= assoc2con(sn
->sn_assoc_change
.sac_assoc_id
);
686 /* We don't know which INIT failed, so clear the PENDING flags
687 * on them all. if assoc_id is zero then it will then try
690 case SCTP_CANT_STR_ASSOC
:
692 log_print("Can't start SCTP association - retrying");
698 log_print("unexpected SCTP assoc change id=%d state=%d",
699 (int)sn
->sn_assoc_change
.sac_assoc_id
,
700 sn
->sn_assoc_change
.sac_state
);
705 /* Data received from remote end */
706 static int receive_from_sock(struct connection
*con
)
709 struct msghdr msg
= {};
713 int call_again_soon
= 0;
715 char incmsg
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
717 mutex_lock(&con
->sock_mutex
);
719 if (con
->sock
== NULL
) {
724 if (con
->rx_page
== NULL
) {
726 * This doesn't need to be atomic, but I think it should
727 * improve performance if it is.
729 con
->rx_page
= alloc_page(GFP_ATOMIC
);
730 if (con
->rx_page
== NULL
)
732 cbuf_init(&con
->cb
, PAGE_CACHE_SIZE
);
735 /* Only SCTP needs these really */
736 memset(&incmsg
, 0, sizeof(incmsg
));
737 msg
.msg_control
= incmsg
;
738 msg
.msg_controllen
= sizeof(incmsg
);
741 * iov[0] is the bit of the circular buffer between the current end
742 * point (cb.base + cb.len) and the end of the buffer.
744 iov
[0].iov_len
= con
->cb
.base
- cbuf_data(&con
->cb
);
745 iov
[0].iov_base
= page_address(con
->rx_page
) + cbuf_data(&con
->cb
);
750 * iov[1] is the bit of the circular buffer between the start of the
751 * buffer and the start of the currently used section (cb.base)
753 if (cbuf_data(&con
->cb
) >= con
->cb
.base
) {
754 iov
[0].iov_len
= PAGE_CACHE_SIZE
- cbuf_data(&con
->cb
);
755 iov
[1].iov_len
= con
->cb
.base
;
756 iov
[1].iov_base
= page_address(con
->rx_page
);
759 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
761 r
= ret
= kernel_recvmsg(con
->sock
, &msg
, iov
, nvec
, len
,
762 MSG_DONTWAIT
| MSG_NOSIGNAL
);
766 /* Process SCTP notifications */
767 if (msg
.msg_flags
& MSG_NOTIFICATION
) {
768 msg
.msg_control
= incmsg
;
769 msg
.msg_controllen
= sizeof(incmsg
);
771 process_sctp_notification(con
, &msg
,
772 page_address(con
->rx_page
) + con
->cb
.base
);
773 mutex_unlock(&con
->sock_mutex
);
776 BUG_ON(con
->nodeid
== 0);
780 cbuf_add(&con
->cb
, ret
);
781 ret
= dlm_process_incoming_buffer(con
->nodeid
,
782 page_address(con
->rx_page
),
783 con
->cb
.base
, con
->cb
.len
,
785 if (ret
== -EBADMSG
) {
786 log_print("lowcomms: addr=%p, base=%u, len=%u, "
787 "iov_len=%u, iov_base[0]=%p, read=%d",
788 page_address(con
->rx_page
), con
->cb
.base
, con
->cb
.len
,
789 len
, iov
[0].iov_base
, r
);
793 cbuf_eat(&con
->cb
, ret
);
795 if (cbuf_empty(&con
->cb
) && !call_again_soon
) {
796 __free_page(con
->rx_page
);
802 mutex_unlock(&con
->sock_mutex
);
806 if (!test_and_set_bit(CF_READ_PENDING
, &con
->flags
))
807 queue_work(recv_workqueue
, &con
->rwork
);
808 mutex_unlock(&con
->sock_mutex
);
812 mutex_unlock(&con
->sock_mutex
);
813 if (ret
!= -EAGAIN
) {
814 close_connection(con
, false);
815 /* Reconnect when there is something to send */
817 /* Don't return success if we really got EOF */
824 /* Listening socket is busy, accept a connection */
825 static int tcp_accept_from_sock(struct connection
*con
)
828 struct sockaddr_storage peeraddr
;
829 struct socket
*newsock
;
832 struct connection
*newcon
;
833 struct connection
*addcon
;
835 mutex_lock(&connections_lock
);
836 if (!dlm_allow_conn
) {
837 mutex_unlock(&connections_lock
);
840 mutex_unlock(&connections_lock
);
842 memset(&peeraddr
, 0, sizeof(peeraddr
));
843 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_STREAM
,
844 IPPROTO_TCP
, &newsock
);
848 mutex_lock_nested(&con
->sock_mutex
, 0);
851 if (con
->sock
== NULL
)
854 newsock
->type
= con
->sock
->type
;
855 newsock
->ops
= con
->sock
->ops
;
857 result
= con
->sock
->ops
->accept(con
->sock
, newsock
, O_NONBLOCK
);
861 /* Get the connected socket's peer */
862 memset(&peeraddr
, 0, sizeof(peeraddr
));
863 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&peeraddr
,
865 result
= -ECONNABORTED
;
869 /* Get the new node's NODEID */
870 make_sockaddr(&peeraddr
, 0, &len
);
871 if (addr_to_nodeid(&peeraddr
, &nodeid
)) {
872 unsigned char *b
=(unsigned char *)&peeraddr
;
873 log_print("connect from non cluster node");
874 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE
,
875 b
, sizeof(struct sockaddr_storage
));
876 sock_release(newsock
);
877 mutex_unlock(&con
->sock_mutex
);
881 log_print("got connection from %d", nodeid
);
883 /* Check to see if we already have a connection to this node. This
884 * could happen if the two nodes initiate a connection at roughly
885 * the same time and the connections cross on the wire.
886 * In this case we store the incoming one in "othercon"
888 newcon
= nodeid2con(nodeid
, GFP_NOFS
);
893 mutex_lock_nested(&newcon
->sock_mutex
, 1);
895 struct connection
*othercon
= newcon
->othercon
;
898 othercon
= kmem_cache_zalloc(con_cache
, GFP_NOFS
);
900 log_print("failed to allocate incoming socket");
901 mutex_unlock(&newcon
->sock_mutex
);
905 othercon
->nodeid
= nodeid
;
906 othercon
->rx_action
= receive_from_sock
;
907 mutex_init(&othercon
->sock_mutex
);
908 INIT_WORK(&othercon
->swork
, process_send_sockets
);
909 INIT_WORK(&othercon
->rwork
, process_recv_sockets
);
910 set_bit(CF_IS_OTHERCON
, &othercon
->flags
);
912 if (!othercon
->sock
) {
913 newcon
->othercon
= othercon
;
914 othercon
->sock
= newsock
;
915 newsock
->sk
->sk_user_data
= othercon
;
916 add_sock(newsock
, othercon
);
920 printk("Extra connection from node %d attempted\n", nodeid
);
922 mutex_unlock(&newcon
->sock_mutex
);
927 newsock
->sk
->sk_user_data
= newcon
;
928 newcon
->rx_action
= receive_from_sock
;
929 add_sock(newsock
, newcon
);
933 mutex_unlock(&newcon
->sock_mutex
);
936 * Add it to the active queue in case we got data
937 * between processing the accept adding the socket
938 * to the read_sockets list
940 if (!test_and_set_bit(CF_READ_PENDING
, &addcon
->flags
))
941 queue_work(recv_workqueue
, &addcon
->rwork
);
942 mutex_unlock(&con
->sock_mutex
);
947 mutex_unlock(&con
->sock_mutex
);
948 sock_release(newsock
);
950 if (result
!= -EAGAIN
)
951 log_print("error accepting connection from node: %d", result
);
955 static void free_entry(struct writequeue_entry
*e
)
957 __free_page(e
->page
);
961 /* Initiate an SCTP association.
962 This is a special case of send_to_sock() in that we don't yet have a
963 peeled-off socket for this association, so we use the listening socket
964 and add the primary IP address of the remote node.
966 static void sctp_init_assoc(struct connection
*con
)
968 struct sockaddr_storage rem_addr
;
969 char outcmsg
[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo
))];
970 struct msghdr outmessage
;
971 struct cmsghdr
*cmsg
;
972 struct sctp_sndrcvinfo
*sinfo
;
973 struct connection
*base_con
;
974 struct writequeue_entry
*e
;
980 if (test_and_set_bit(CF_INIT_PENDING
, &con
->flags
))
983 if (con
->retries
++ > MAX_CONNECT_RETRIES
)
986 if (nodeid_to_addr(con
->nodeid
, NULL
, (struct sockaddr
*)&rem_addr
)) {
987 log_print("no address for nodeid %d", con
->nodeid
);
990 base_con
= nodeid2con(0, 0);
991 BUG_ON(base_con
== NULL
);
993 make_sockaddr(&rem_addr
, dlm_config
.ci_tcp_port
, &addrlen
);
995 outmessage
.msg_name
= &rem_addr
;
996 outmessage
.msg_namelen
= addrlen
;
997 outmessage
.msg_control
= outcmsg
;
998 outmessage
.msg_controllen
= sizeof(outcmsg
);
999 outmessage
.msg_flags
= MSG_EOR
;
1001 spin_lock(&con
->writequeue_lock
);
1003 if (list_empty(&con
->writequeue
)) {
1004 spin_unlock(&con
->writequeue_lock
);
1005 log_print("writequeue empty for nodeid %d", con
->nodeid
);
1009 e
= list_first_entry(&con
->writequeue
, struct writequeue_entry
, list
);
1012 spin_unlock(&con
->writequeue_lock
);
1014 /* Send the first block off the write queue */
1015 iov
[0].iov_base
= page_address(e
->page
)+offset
;
1016 iov
[0].iov_len
= len
;
1018 cmsg
= CMSG_FIRSTHDR(&outmessage
);
1019 cmsg
->cmsg_level
= IPPROTO_SCTP
;
1020 cmsg
->cmsg_type
= SCTP_SNDRCV
;
1021 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct sctp_sndrcvinfo
));
1022 sinfo
= CMSG_DATA(cmsg
);
1023 memset(sinfo
, 0x00, sizeof(struct sctp_sndrcvinfo
));
1024 sinfo
->sinfo_ppid
= cpu_to_le32(dlm_our_nodeid());
1025 outmessage
.msg_controllen
= cmsg
->cmsg_len
;
1027 ret
= kernel_sendmsg(base_con
->sock
, &outmessage
, iov
, 1, len
);
1029 log_print("Send first packet to node %d failed: %d",
1032 /* Try again later */
1033 clear_bit(CF_CONNECT_PENDING
, &con
->flags
);
1034 clear_bit(CF_INIT_PENDING
, &con
->flags
);
1037 spin_lock(&con
->writequeue_lock
);
1041 if (e
->len
== 0 && e
->users
== 0) {
1045 spin_unlock(&con
->writequeue_lock
);
1049 /* Connect a new socket to its peer */
1050 static void tcp_connect_to_sock(struct connection
*con
)
1052 struct sockaddr_storage saddr
, src_addr
;
1054 struct socket
*sock
= NULL
;
1058 if (con
->nodeid
== 0) {
1059 log_print("attempt to connect sock 0 foiled");
1063 mutex_lock(&con
->sock_mutex
);
1064 if (con
->retries
++ > MAX_CONNECT_RETRIES
)
1067 /* Some odd races can cause double-connects, ignore them */
1071 /* Create a socket to communicate with */
1072 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_STREAM
,
1073 IPPROTO_TCP
, &sock
);
1077 memset(&saddr
, 0, sizeof(saddr
));
1078 result
= nodeid_to_addr(con
->nodeid
, &saddr
, NULL
);
1080 log_print("no address for nodeid %d", con
->nodeid
);
1084 sock
->sk
->sk_user_data
= con
;
1085 con
->rx_action
= receive_from_sock
;
1086 con
->connect_action
= tcp_connect_to_sock
;
1087 add_sock(sock
, con
);
1089 /* Bind to our cluster-known address connecting to avoid
1091 memcpy(&src_addr
, dlm_local_addr
[0], sizeof(src_addr
));
1092 make_sockaddr(&src_addr
, 0, &addr_len
);
1093 result
= sock
->ops
->bind(sock
, (struct sockaddr
*) &src_addr
,
1096 log_print("could not bind for connect: %d", result
);
1097 /* This *may* not indicate a critical error */
1100 make_sockaddr(&saddr
, dlm_config
.ci_tcp_port
, &addr_len
);
1102 log_print("connecting to %d", con
->nodeid
);
1104 /* Turn off Nagle's algorithm */
1105 kernel_setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&one
,
1108 result
= sock
->ops
->connect(sock
, (struct sockaddr
*)&saddr
, addr_len
,
1110 if (result
== -EINPROGRESS
)
1117 sock_release(con
->sock
);
1123 * Some errors are fatal and this list might need adjusting. For other
1124 * errors we try again until the max number of retries is reached.
1126 if (result
!= -EHOSTUNREACH
&&
1127 result
!= -ENETUNREACH
&&
1128 result
!= -ENETDOWN
&&
1129 result
!= -EINVAL
&&
1130 result
!= -EPROTONOSUPPORT
) {
1131 log_print("connect %d try %d error %d", con
->nodeid
,
1132 con
->retries
, result
);
1133 mutex_unlock(&con
->sock_mutex
);
1135 lowcomms_connect_sock(con
);
1139 mutex_unlock(&con
->sock_mutex
);
1143 static struct socket
*tcp_create_listen_sock(struct connection
*con
,
1144 struct sockaddr_storage
*saddr
)
1146 struct socket
*sock
= NULL
;
1151 if (dlm_local_addr
[0]->ss_family
== AF_INET
)
1152 addr_len
= sizeof(struct sockaddr_in
);
1154 addr_len
= sizeof(struct sockaddr_in6
);
1156 /* Create a socket to communicate with */
1157 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_STREAM
,
1158 IPPROTO_TCP
, &sock
);
1160 log_print("Can't create listening comms socket");
1164 /* Turn off Nagle's algorithm */
1165 kernel_setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&one
,
1168 result
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
,
1169 (char *)&one
, sizeof(one
));
1172 log_print("Failed to set SO_REUSEADDR on socket: %d", result
);
1174 con
->rx_action
= tcp_accept_from_sock
;
1175 con
->connect_action
= tcp_connect_to_sock
;
1177 /* Bind to our port */
1178 make_sockaddr(saddr
, dlm_config
.ci_tcp_port
, &addr_len
);
1179 result
= sock
->ops
->bind(sock
, (struct sockaddr
*) saddr
, addr_len
);
1181 log_print("Can't bind to port %d", dlm_config
.ci_tcp_port
);
1187 result
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
1188 (char *)&one
, sizeof(one
));
1190 log_print("Set keepalive failed: %d", result
);
1193 result
= sock
->ops
->listen(sock
, 5);
1195 log_print("Can't listen on port %d", dlm_config
.ci_tcp_port
);
1205 /* Get local addresses */
1206 static void init_local(void)
1208 struct sockaddr_storage sas
, *addr
;
1211 dlm_local_count
= 0;
1212 for (i
= 0; i
< DLM_MAX_ADDR_COUNT
; i
++) {
1213 if (dlm_our_addr(&sas
, i
))
1216 addr
= kmalloc(sizeof(*addr
), GFP_NOFS
);
1219 memcpy(addr
, &sas
, sizeof(*addr
));
1220 dlm_local_addr
[dlm_local_count
++] = addr
;
1224 /* Bind to an IP address. SCTP allows multiple address so it can do
1226 static int add_sctp_bind_addr(struct connection
*sctp_con
,
1227 struct sockaddr_storage
*addr
,
1228 int addr_len
, int num
)
1233 result
= kernel_bind(sctp_con
->sock
,
1234 (struct sockaddr
*) addr
,
1237 result
= kernel_setsockopt(sctp_con
->sock
, SOL_SCTP
,
1238 SCTP_SOCKOPT_BINDX_ADD
,
1239 (char *)addr
, addr_len
);
1242 log_print("Can't bind to port %d addr number %d",
1243 dlm_config
.ci_tcp_port
, num
);
1248 /* Initialise SCTP socket and bind to all interfaces */
1249 static int sctp_listen_for_all(void)
1251 struct socket
*sock
= NULL
;
1252 struct sockaddr_storage localaddr
;
1253 struct sctp_event_subscribe subscribe
;
1254 int result
= -EINVAL
, num
= 1, i
, addr_len
;
1255 struct connection
*con
= nodeid2con(0, GFP_NOFS
);
1256 int bufsize
= NEEDED_RMEM
;
1261 log_print("Using SCTP for communications");
1263 result
= sock_create_kern(dlm_local_addr
[0]->ss_family
, SOCK_SEQPACKET
,
1264 IPPROTO_SCTP
, &sock
);
1266 log_print("Can't create comms socket, check SCTP is loaded");
1270 /* Listen for events */
1271 memset(&subscribe
, 0, sizeof(subscribe
));
1272 subscribe
.sctp_data_io_event
= 1;
1273 subscribe
.sctp_association_event
= 1;
1274 subscribe
.sctp_send_failure_event
= 1;
1275 subscribe
.sctp_shutdown_event
= 1;
1276 subscribe
.sctp_partial_delivery_event
= 1;
1278 result
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
1279 (char *)&bufsize
, sizeof(bufsize
));
1281 log_print("Error increasing buffer space on socket %d", result
);
1283 result
= kernel_setsockopt(sock
, SOL_SCTP
, SCTP_EVENTS
,
1284 (char *)&subscribe
, sizeof(subscribe
));
1286 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1288 goto create_delsock
;
1291 /* Init con struct */
1292 sock
->sk
->sk_user_data
= con
;
1294 con
->sock
->sk
->sk_data_ready
= lowcomms_data_ready
;
1295 con
->rx_action
= receive_from_sock
;
1296 con
->connect_action
= sctp_init_assoc
;
1298 /* Bind to all interfaces. */
1299 for (i
= 0; i
< dlm_local_count
; i
++) {
1300 memcpy(&localaddr
, dlm_local_addr
[i
], sizeof(localaddr
));
1301 make_sockaddr(&localaddr
, dlm_config
.ci_tcp_port
, &addr_len
);
1303 result
= add_sctp_bind_addr(con
, &localaddr
, addr_len
, num
);
1305 goto create_delsock
;
1309 result
= sock
->ops
->listen(sock
, 5);
1311 log_print("Can't set socket listening");
1312 goto create_delsock
;
1324 static int tcp_listen_for_all(void)
1326 struct socket
*sock
= NULL
;
1327 struct connection
*con
= nodeid2con(0, GFP_NOFS
);
1328 int result
= -EINVAL
;
1333 /* We don't support multi-homed hosts */
1334 if (dlm_local_addr
[1] != NULL
) {
1335 log_print("TCP protocol can't handle multi-homed hosts, "
1340 log_print("Using TCP for communications");
1342 sock
= tcp_create_listen_sock(con
, dlm_local_addr
[0]);
1344 add_sock(sock
, con
);
1348 result
= -EADDRINUSE
;
1356 static struct writequeue_entry
*new_writequeue_entry(struct connection
*con
,
1359 struct writequeue_entry
*entry
;
1361 entry
= kmalloc(sizeof(struct writequeue_entry
), allocation
);
1365 entry
->page
= alloc_page(allocation
);
1380 void *dlm_lowcomms_get_buffer(int nodeid
, int len
, gfp_t allocation
, char **ppc
)
1382 struct connection
*con
;
1383 struct writequeue_entry
*e
;
1386 con
= nodeid2con(nodeid
, allocation
);
1390 spin_lock(&con
->writequeue_lock
);
1391 e
= list_entry(con
->writequeue
.prev
, struct writequeue_entry
, list
);
1392 if ((&e
->list
== &con
->writequeue
) ||
1393 (PAGE_CACHE_SIZE
- e
->end
< len
)) {
1400 spin_unlock(&con
->writequeue_lock
);
1404 *ppc
= page_address(e
->page
) + offset
;
1408 e
= new_writequeue_entry(con
, allocation
);
1410 spin_lock(&con
->writequeue_lock
);
1414 list_add_tail(&e
->list
, &con
->writequeue
);
1415 spin_unlock(&con
->writequeue_lock
);
1421 void dlm_lowcomms_commit_buffer(void *mh
)
1423 struct writequeue_entry
*e
= (struct writequeue_entry
*)mh
;
1424 struct connection
*con
= e
->con
;
1427 spin_lock(&con
->writequeue_lock
);
1431 e
->len
= e
->end
- e
->offset
;
1432 spin_unlock(&con
->writequeue_lock
);
1434 if (!test_and_set_bit(CF_WRITE_PENDING
, &con
->flags
)) {
1435 queue_work(send_workqueue
, &con
->swork
);
1440 spin_unlock(&con
->writequeue_lock
);
1444 /* Send a message */
1445 static void send_to_sock(struct connection
*con
)
1448 const int msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
;
1449 struct writequeue_entry
*e
;
1453 mutex_lock(&con
->sock_mutex
);
1454 if (con
->sock
== NULL
)
1457 spin_lock(&con
->writequeue_lock
);
1459 e
= list_entry(con
->writequeue
.next
, struct writequeue_entry
,
1461 if ((struct list_head
*) e
== &con
->writequeue
)
1466 BUG_ON(len
== 0 && e
->users
== 0);
1467 spin_unlock(&con
->writequeue_lock
);
1471 ret
= kernel_sendpage(con
->sock
, e
->page
, offset
, len
,
1473 if (ret
== -EAGAIN
|| ret
== 0) {
1474 if (ret
== -EAGAIN
&&
1475 test_bit(SOCK_ASYNC_NOSPACE
, &con
->sock
->flags
) &&
1476 !test_and_set_bit(CF_APP_LIMITED
, &con
->flags
)) {
1477 /* Notify TCP that we're limited by the
1478 * application window size.
1480 set_bit(SOCK_NOSPACE
, &con
->sock
->flags
);
1481 con
->sock
->sk
->sk_write_pending
++;
1489 /* Don't starve people filling buffers */
1490 if (++count
>= MAX_SEND_MSG_COUNT
) {
1495 spin_lock(&con
->writequeue_lock
);
1499 if (e
->len
== 0 && e
->users
== 0) {
1504 spin_unlock(&con
->writequeue_lock
);
1506 mutex_unlock(&con
->sock_mutex
);
1510 mutex_unlock(&con
->sock_mutex
);
1511 close_connection(con
, false);
1512 lowcomms_connect_sock(con
);
1516 mutex_unlock(&con
->sock_mutex
);
1517 if (!test_bit(CF_INIT_PENDING
, &con
->flags
))
1518 lowcomms_connect_sock(con
);
1521 static void clean_one_writequeue(struct connection
*con
)
1523 struct writequeue_entry
*e
, *safe
;
1525 spin_lock(&con
->writequeue_lock
);
1526 list_for_each_entry_safe(e
, safe
, &con
->writequeue
, list
) {
1530 spin_unlock(&con
->writequeue_lock
);
1533 /* Called from recovery when it knows that a node has
1535 int dlm_lowcomms_close(int nodeid
)
1537 struct connection
*con
;
1538 struct dlm_node_addr
*na
;
1540 log_print("closing connection to node %d", nodeid
);
1541 con
= nodeid2con(nodeid
, 0);
1543 clear_bit(CF_CONNECT_PENDING
, &con
->flags
);
1544 clear_bit(CF_WRITE_PENDING
, &con
->flags
);
1545 set_bit(CF_CLOSE
, &con
->flags
);
1546 if (cancel_work_sync(&con
->swork
))
1547 log_print("canceled swork for node %d", nodeid
);
1548 if (cancel_work_sync(&con
->rwork
))
1549 log_print("canceled rwork for node %d", nodeid
);
1550 clean_one_writequeue(con
);
1551 close_connection(con
, true);
1554 spin_lock(&dlm_node_addrs_spin
);
1555 na
= find_node_addr(nodeid
);
1557 list_del(&na
->list
);
1558 while (na
->addr_count
--)
1559 kfree(na
->addr
[na
->addr_count
]);
1562 spin_unlock(&dlm_node_addrs_spin
);
1567 /* Receive workqueue function */
1568 static void process_recv_sockets(struct work_struct
*work
)
1570 struct connection
*con
= container_of(work
, struct connection
, rwork
);
1573 clear_bit(CF_READ_PENDING
, &con
->flags
);
1575 err
= con
->rx_action(con
);
1579 /* Send workqueue function */
1580 static void process_send_sockets(struct work_struct
*work
)
1582 struct connection
*con
= container_of(work
, struct connection
, swork
);
1584 if (test_and_clear_bit(CF_CONNECT_PENDING
, &con
->flags
)) {
1585 con
->connect_action(con
);
1586 set_bit(CF_WRITE_PENDING
, &con
->flags
);
1588 if (test_and_clear_bit(CF_WRITE_PENDING
, &con
->flags
))
1593 /* Discard all entries on the write queues */
1594 static void clean_writequeues(void)
1596 foreach_conn(clean_one_writequeue
);
1599 static void work_stop(void)
1601 destroy_workqueue(recv_workqueue
);
1602 destroy_workqueue(send_workqueue
);
1605 static int work_start(void)
1607 recv_workqueue
= alloc_workqueue("dlm_recv",
1608 WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
1609 if (!recv_workqueue
) {
1610 log_print("can't start dlm_recv");
1614 send_workqueue
= alloc_workqueue("dlm_send",
1615 WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1);
1616 if (!send_workqueue
) {
1617 log_print("can't start dlm_send");
1618 destroy_workqueue(recv_workqueue
);
1625 static void stop_conn(struct connection
*con
)
1628 if (con
->sock
&& con
->sock
->sk
)
1629 con
->sock
->sk
->sk_user_data
= NULL
;
1632 static void free_conn(struct connection
*con
)
1634 close_connection(con
, true);
1636 kmem_cache_free(con_cache
, con
->othercon
);
1637 hlist_del(&con
->list
);
1638 kmem_cache_free(con_cache
, con
);
1641 void dlm_lowcomms_stop(void)
1643 /* Set all the flags to prevent any
1646 mutex_lock(&connections_lock
);
1648 foreach_conn(stop_conn
);
1649 mutex_unlock(&connections_lock
);
1653 mutex_lock(&connections_lock
);
1654 clean_writequeues();
1656 foreach_conn(free_conn
);
1658 mutex_unlock(&connections_lock
);
1659 kmem_cache_destroy(con_cache
);
1662 int dlm_lowcomms_start(void)
1664 int error
= -EINVAL
;
1665 struct connection
*con
;
1668 for (i
= 0; i
< CONN_HASH_SIZE
; i
++)
1669 INIT_HLIST_HEAD(&connection_hash
[i
]);
1672 if (!dlm_local_count
) {
1674 log_print("no local IP address has been set");
1679 con_cache
= kmem_cache_create("dlm_conn", sizeof(struct connection
),
1680 __alignof__(struct connection
), 0,
1685 error
= work_start();
1691 /* Start listening */
1692 if (dlm_config
.ci_protocol
== 0)
1693 error
= tcp_listen_for_all();
1695 error
= sctp_listen_for_all();
1703 con
= nodeid2con(0,0);
1705 close_connection(con
, false);
1706 kmem_cache_free(con_cache
, con
);
1709 kmem_cache_destroy(con_cache
);
1714 void dlm_lowcomms_exit(void)
1716 struct dlm_node_addr
*na
, *safe
;
1718 spin_lock(&dlm_node_addrs_spin
);
1719 list_for_each_entry_safe(na
, safe
, &dlm_node_addrs
, list
) {
1720 list_del(&na
->list
);
1721 while (na
->addr_count
--)
1722 kfree(na
->addr
[na
->addr_count
]);
1725 spin_unlock(&dlm_node_addrs_spin
);