1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
15 * This is the "low-level" comms layer.
17 * It is responsible for sending/receiving messages
18 * from other nodes in the cluster.
20 * Cluster nodes are referred to by their nodeids. nodeids are
21 * simply 32 bit numbers to the locking module - if they need to
22 * be expanded for the cluster infrastructure then that is its
23 * responsibility. It is this layer's
24 * responsibility to resolve these into IP address or
25 * whatever it needs for inter-node communication.
27 * The comms level is two kernel threads that deal mainly with
28 * the receiving of messages from other nodes and passing them
29 * up to the mid-level comms layer (which understands the
30 * message format) for execution by the locking core, and
31 * a send thread which does all the setting up of connections
32 * to remote nodes and the sending of data. Threads are not allowed
33 * to send their own data because it may cause them to wait in times
34 * of high load. Also, this way, the sending thread can collect together
35 * messages bound for one node and send them in one block.
37 * lowcomms will choose to use either TCP or SCTP as its transport layer
38 * depending on the configuration variable 'protocol'. This should be set
39 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
40 * cluster-wide mechanism as it must be the same on all nodes of the cluster
41 * for the DLM to function.
45 #include <asm/ioctls.h>
48 #include <linux/pagemap.h>
49 #include <linux/file.h>
50 #include <linux/mutex.h>
51 #include <linux/sctp.h>
52 #include <linux/slab.h>
53 #include <net/sctp/sctp.h>
56 #include <trace/events/dlm.h>
58 #include "dlm_internal.h"
64 #define NEEDED_RMEM (4*1024*1024)
67 struct socket *sock; /* NULL if not connected */
68 uint32_t nodeid; /* So we know who we are in the list */
69 /* this semaphore is used to allow parallel recv/send in read
70 * lock mode. When we release a sock we need to held the write lock.
72 * However this is locking code and not nice. When we remove the
73 * othercon handling we can look into other mechanism to synchronize
74 * io handling to call sock_release() at the right time.
76 struct rw_semaphore sock_lock;
78 #define CF_APP_LIMITED 0
79 #define CF_RECV_PENDING 1
80 #define CF_SEND_PENDING 2
81 #define CF_RECV_INTR 3
83 #define CF_IS_OTHERCON 5
84 struct list_head writequeue; /* List of outgoing writequeue_entries */
85 spinlock_t writequeue_lock;
87 struct hlist_node list;
88 /* due some connect()/accept() races we currently have this cross over
89 * connection attempt second connection for one node.
91 * There is a solution to avoid the race by introducing a connect
92 * rule as e.g. our_nodeid > nodeid_to_connect who is allowed to
93 * connect. Otherside can connect but will only be considered that
94 * the other side wants to have a reconnect.
96 * However changing to this behaviour will break backwards compatible.
97 * In a DLM protocol major version upgrade we should remove this!
99 struct connection *othercon;
100 struct work_struct rwork; /* receive worker */
101 struct work_struct swork; /* send worker */
102 unsigned char rx_leftover_buf[DLM_MAX_SOCKET_BUFSIZE];
107 struct sockaddr_storage addr[DLM_MAX_ADDR_COUNT];
108 spinlock_t addrs_lock;
111 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
113 struct listen_connection {
115 struct work_struct rwork;
118 #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
119 #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
121 /* An entry waiting to be sent */
122 struct writequeue_entry {
123 struct list_head list;
130 struct connection *con;
131 struct list_head msgs;
136 struct writequeue_entry *entry;
137 struct dlm_msg *orig_msg;
141 int idx; /* new()/commit() idx exchange */
143 struct list_head list;
147 struct processqueue_entry {
152 struct list_head list;
155 struct dlm_proto_ops {
160 int (*connect)(struct connection *con, struct socket *sock,
161 struct sockaddr *addr, int addr_len);
162 void (*sockopts)(struct socket *sock);
163 int (*bind)(struct socket *sock);
164 int (*listen_validate)(void);
165 void (*listen_sockopts)(struct socket *sock);
166 int (*listen_bind)(struct socket *sock);
169 static struct listen_sock_callbacks {
170 void (*sk_error_report)(struct sock *);
171 void (*sk_data_ready)(struct sock *);
172 void (*sk_state_change)(struct sock *);
173 void (*sk_write_space)(struct sock *);
176 static struct listen_connection listen_con;
177 static struct sockaddr_storage dlm_local_addr[DLM_MAX_ADDR_COUNT];
178 static int dlm_local_count;
181 static struct workqueue_struct *io_workqueue;
182 static struct workqueue_struct *process_workqueue;
184 static struct hlist_head connection_hash[CONN_HASH_SIZE];
185 static DEFINE_SPINLOCK(connections_lock);
186 DEFINE_STATIC_SRCU(connections_srcu);
188 static const struct dlm_proto_ops *dlm_proto_ops;
190 #define DLM_IO_SUCCESS 0
193 #define DLM_IO_RESCHED 3
195 static void process_recv_sockets(struct work_struct *work);
196 static void process_send_sockets(struct work_struct *work);
197 static void process_dlm_messages(struct work_struct *work);
199 static DECLARE_WORK(process_work, process_dlm_messages);
200 static DEFINE_SPINLOCK(processqueue_lock);
201 static bool process_dlm_messages_pending;
202 static LIST_HEAD(processqueue);
204 bool dlm_lowcomms_is_running(void)
206 return !!listen_con.sock;
209 static void lowcomms_queue_swork(struct connection *con)
211 assert_spin_locked(&con->writequeue_lock);
213 if (!test_bit(CF_IO_STOP, &con->flags) &&
214 !test_bit(CF_APP_LIMITED, &con->flags) &&
215 !test_and_set_bit(CF_SEND_PENDING, &con->flags))
216 queue_work(io_workqueue, &con->swork);
219 static void lowcomms_queue_rwork(struct connection *con)
221 #ifdef CONFIG_LOCKDEP
222 WARN_ON_ONCE(!lockdep_sock_is_held(con->sock->sk));
225 if (!test_bit(CF_IO_STOP, &con->flags) &&
226 !test_and_set_bit(CF_RECV_PENDING, &con->flags))
227 queue_work(io_workqueue, &con->rwork);
230 static void writequeue_entry_ctor(void *data)
232 struct writequeue_entry *entry = data;
234 INIT_LIST_HEAD(&entry->msgs);
237 struct kmem_cache *dlm_lowcomms_writequeue_cache_create(void)
239 return kmem_cache_create("dlm_writequeue", sizeof(struct writequeue_entry),
240 0, 0, writequeue_entry_ctor);
243 struct kmem_cache *dlm_lowcomms_msg_cache_create(void)
245 return kmem_cache_create("dlm_msg", sizeof(struct dlm_msg), 0, 0, NULL);
248 /* need to held writequeue_lock */
249 static struct writequeue_entry *con_next_wq(struct connection *con)
251 struct writequeue_entry *e;
253 e = list_first_entry_or_null(&con->writequeue, struct writequeue_entry,
255 /* if len is zero nothing is to send, if there are users filling
256 * buffers we wait until the users are done so we can send more.
258 if (!e || e->users || e->len == 0)
264 static struct connection *__find_con(int nodeid, int r)
266 struct connection *con;
268 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
269 if (con->nodeid == nodeid)
276 static void dlm_con_init(struct connection *con, int nodeid)
278 con->nodeid = nodeid;
279 init_rwsem(&con->sock_lock);
280 INIT_LIST_HEAD(&con->writequeue);
281 spin_lock_init(&con->writequeue_lock);
282 INIT_WORK(&con->swork, process_send_sockets);
283 INIT_WORK(&con->rwork, process_recv_sockets);
284 spin_lock_init(&con->addrs_lock);
288 * If 'allocation' is zero then we don't attempt to create a new
289 * connection structure for this node.
291 static struct connection *nodeid2con(int nodeid, gfp_t alloc)
293 struct connection *con, *tmp;
296 r = nodeid_hash(nodeid);
297 con = __find_con(nodeid, r);
301 con = kzalloc(sizeof(*con), alloc);
305 dlm_con_init(con, nodeid);
307 spin_lock(&connections_lock);
308 /* Because multiple workqueues/threads calls this function it can
309 * race on multiple cpu's. Instead of locking hot path __find_con()
310 * we just check in rare cases of recently added nodes again
311 * under protection of connections_lock. If this is the case we
312 * abort our connection creation and return the existing connection.
314 tmp = __find_con(nodeid, r);
316 spin_unlock(&connections_lock);
321 hlist_add_head_rcu(&con->list, &connection_hash[r]);
322 spin_unlock(&connections_lock);
327 static int addr_compare(const struct sockaddr_storage *x,
328 const struct sockaddr_storage *y)
330 switch (x->ss_family) {
332 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
333 struct sockaddr_in *siny = (struct sockaddr_in *)y;
334 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
336 if (sinx->sin_port != siny->sin_port)
341 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
342 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
343 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
345 if (sinx->sin6_port != siny->sin6_port)
355 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
356 struct sockaddr *sa_out, bool try_new_addr,
359 struct sockaddr_storage sas;
360 struct connection *con;
363 if (!dlm_local_count)
366 idx = srcu_read_lock(&connections_srcu);
367 con = nodeid2con(nodeid, 0);
369 srcu_read_unlock(&connections_srcu, idx);
373 spin_lock(&con->addrs_lock);
374 if (!con->addr_count) {
375 spin_unlock(&con->addrs_lock);
376 srcu_read_unlock(&connections_srcu, idx);
380 memcpy(&sas, &con->addr[con->curr_addr_index],
381 sizeof(struct sockaddr_storage));
384 con->curr_addr_index++;
385 if (con->curr_addr_index == con->addr_count)
386 con->curr_addr_index = 0;
390 spin_unlock(&con->addrs_lock);
393 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
396 srcu_read_unlock(&connections_srcu, idx);
400 if (dlm_local_addr[0].ss_family == AF_INET) {
401 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
402 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
403 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
405 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
406 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
407 ret6->sin6_addr = in6->sin6_addr;
410 srcu_read_unlock(&connections_srcu, idx);
414 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
417 struct connection *con;
420 idx = srcu_read_lock(&connections_srcu);
421 for (i = 0; i < CONN_HASH_SIZE; i++) {
422 hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
423 WARN_ON_ONCE(!con->addr_count);
425 spin_lock(&con->addrs_lock);
426 for (addr_i = 0; addr_i < con->addr_count; addr_i++) {
427 if (addr_compare(&con->addr[addr_i], addr)) {
428 *nodeid = con->nodeid;
430 spin_unlock(&con->addrs_lock);
431 srcu_read_unlock(&connections_srcu, idx);
435 spin_unlock(&con->addrs_lock);
438 srcu_read_unlock(&connections_srcu, idx);
443 static bool dlm_lowcomms_con_has_addr(const struct connection *con,
444 const struct sockaddr_storage *addr)
448 for (i = 0; i < con->addr_count; i++) {
449 if (addr_compare(&con->addr[i], addr))
456 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
458 struct connection *con;
461 idx = srcu_read_lock(&connections_srcu);
462 con = nodeid2con(nodeid, GFP_NOFS);
464 srcu_read_unlock(&connections_srcu, idx);
468 spin_lock(&con->addrs_lock);
469 if (!con->addr_count) {
470 memcpy(&con->addr[0], addr, sizeof(*addr));
472 con->mark = dlm_config.ci_mark;
473 spin_unlock(&con->addrs_lock);
474 srcu_read_unlock(&connections_srcu, idx);
478 ret = dlm_lowcomms_con_has_addr(con, addr);
480 spin_unlock(&con->addrs_lock);
481 srcu_read_unlock(&connections_srcu, idx);
485 if (con->addr_count >= DLM_MAX_ADDR_COUNT) {
486 spin_unlock(&con->addrs_lock);
487 srcu_read_unlock(&connections_srcu, idx);
491 memcpy(&con->addr[con->addr_count++], addr, sizeof(*addr));
492 srcu_read_unlock(&connections_srcu, idx);
493 spin_unlock(&con->addrs_lock);
497 /* Data available on socket or listen socket received a connect */
498 static void lowcomms_data_ready(struct sock *sk)
500 struct connection *con = sock2con(sk);
502 set_bit(CF_RECV_INTR, &con->flags);
503 lowcomms_queue_rwork(con);
506 static void lowcomms_write_space(struct sock *sk)
508 struct connection *con = sock2con(sk);
510 clear_bit(SOCK_NOSPACE, &con->sock->flags);
512 spin_lock_bh(&con->writequeue_lock);
513 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
514 con->sock->sk->sk_write_pending--;
515 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
518 lowcomms_queue_swork(con);
519 spin_unlock_bh(&con->writequeue_lock);
522 static void lowcomms_state_change(struct sock *sk)
524 /* SCTP layer is not calling sk_data_ready when the connection
525 * is done, so we catch the signal through here.
527 if (sk->sk_shutdown == RCV_SHUTDOWN)
528 lowcomms_data_ready(sk);
531 static void lowcomms_listen_data_ready(struct sock *sk)
533 queue_work(io_workqueue, &listen_con.rwork);
536 int dlm_lowcomms_connect_node(int nodeid)
538 struct connection *con;
541 if (nodeid == dlm_our_nodeid())
544 idx = srcu_read_lock(&connections_srcu);
545 con = nodeid2con(nodeid, 0);
546 if (WARN_ON_ONCE(!con)) {
547 srcu_read_unlock(&connections_srcu, idx);
551 down_read(&con->sock_lock);
553 spin_lock_bh(&con->writequeue_lock);
554 lowcomms_queue_swork(con);
555 spin_unlock_bh(&con->writequeue_lock);
557 up_read(&con->sock_lock);
558 srcu_read_unlock(&connections_srcu, idx);
564 int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
566 struct connection *con;
569 idx = srcu_read_lock(&connections_srcu);
570 con = nodeid2con(nodeid, 0);
572 srcu_read_unlock(&connections_srcu, idx);
576 spin_lock(&con->addrs_lock);
578 spin_unlock(&con->addrs_lock);
579 srcu_read_unlock(&connections_srcu, idx);
583 static void lowcomms_error_report(struct sock *sk)
585 struct connection *con = sock2con(sk);
586 struct inet_sock *inet;
589 switch (sk->sk_family) {
591 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
592 "sending to node %d at %pI4, dport %d, "
593 "sk_err=%d/%d\n", dlm_our_nodeid(),
594 con->nodeid, &inet->inet_daddr,
595 ntohs(inet->inet_dport), sk->sk_err,
598 #if IS_ENABLED(CONFIG_IPV6)
600 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
601 "sending to node %d at %pI6c, "
602 "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
603 con->nodeid, &sk->sk_v6_daddr,
604 ntohs(inet->inet_dport), sk->sk_err,
609 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
610 "invalid socket family %d set, "
611 "sk_err=%d/%d\n", dlm_our_nodeid(),
612 sk->sk_family, sk->sk_err, sk->sk_err_soft);
616 dlm_midcomms_unack_msg_resend(con->nodeid);
618 listen_sock.sk_error_report(sk);
621 static void restore_callbacks(struct sock *sk)
623 #ifdef CONFIG_LOCKDEP
624 WARN_ON_ONCE(!lockdep_sock_is_held(sk));
627 sk->sk_user_data = NULL;
628 sk->sk_data_ready = listen_sock.sk_data_ready;
629 sk->sk_state_change = listen_sock.sk_state_change;
630 sk->sk_write_space = listen_sock.sk_write_space;
631 sk->sk_error_report = listen_sock.sk_error_report;
634 /* Make a socket active */
635 static void add_sock(struct socket *sock, struct connection *con)
637 struct sock *sk = sock->sk;
642 sk->sk_user_data = con;
643 sk->sk_data_ready = lowcomms_data_ready;
644 sk->sk_write_space = lowcomms_write_space;
645 if (dlm_config.ci_protocol == DLM_PROTO_SCTP)
646 sk->sk_state_change = lowcomms_state_change;
647 sk->sk_allocation = GFP_NOFS;
648 sk->sk_error_report = lowcomms_error_report;
652 /* Add the port number to an IPv6 or 4 sockaddr and return the address
654 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
657 saddr->ss_family = dlm_local_addr[0].ss_family;
658 if (saddr->ss_family == AF_INET) {
659 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
660 in4_addr->sin_port = cpu_to_be16(port);
661 *addr_len = sizeof(struct sockaddr_in);
662 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
664 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
665 in6_addr->sin6_port = cpu_to_be16(port);
666 *addr_len = sizeof(struct sockaddr_in6);
668 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
671 static void dlm_page_release(struct kref *kref)
673 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
676 __free_page(e->page);
677 dlm_free_writequeue(e);
680 static void dlm_msg_release(struct kref *kref)
682 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
684 kref_put(&msg->entry->ref, dlm_page_release);
688 static void free_entry(struct writequeue_entry *e)
690 struct dlm_msg *msg, *tmp;
692 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
694 msg->orig_msg->retransmit = false;
695 kref_put(&msg->orig_msg->ref, dlm_msg_release);
698 list_del(&msg->list);
699 kref_put(&msg->ref, dlm_msg_release);
703 kref_put(&e->ref, dlm_page_release);
706 static void dlm_close_sock(struct socket **sock)
708 lock_sock((*sock)->sk);
709 restore_callbacks((*sock)->sk);
710 release_sock((*sock)->sk);
716 static void allow_connection_io(struct connection *con)
719 clear_bit(CF_IO_STOP, &con->othercon->flags);
720 clear_bit(CF_IO_STOP, &con->flags);
723 static void stop_connection_io(struct connection *con)
726 stop_connection_io(con->othercon);
728 down_write(&con->sock_lock);
730 lock_sock(con->sock->sk);
731 restore_callbacks(con->sock->sk);
733 spin_lock_bh(&con->writequeue_lock);
734 set_bit(CF_IO_STOP, &con->flags);
735 spin_unlock_bh(&con->writequeue_lock);
736 release_sock(con->sock->sk);
738 spin_lock_bh(&con->writequeue_lock);
739 set_bit(CF_IO_STOP, &con->flags);
740 spin_unlock_bh(&con->writequeue_lock);
742 up_write(&con->sock_lock);
744 cancel_work_sync(&con->swork);
745 cancel_work_sync(&con->rwork);
748 /* Close a remote connection and tidy up */
749 static void close_connection(struct connection *con, bool and_other)
751 struct writequeue_entry *e;
753 if (con->othercon && and_other)
754 close_connection(con->othercon, false);
756 down_write(&con->sock_lock);
758 up_write(&con->sock_lock);
762 dlm_close_sock(&con->sock);
764 /* if we send a writequeue entry only a half way, we drop the
765 * whole entry because reconnection and that we not start of the
766 * middle of a msg which will confuse the other end.
768 * we can always drop messages because retransmits, but what we
769 * cannot allow is to transmit half messages which may be processed
772 * our policy is to start on a clean state when disconnects, we don't
773 * know what's send/received on transport layer in this case.
775 spin_lock_bh(&con->writequeue_lock);
776 if (!list_empty(&con->writequeue)) {
777 e = list_first_entry(&con->writequeue, struct writequeue_entry,
782 spin_unlock_bh(&con->writequeue_lock);
784 con->rx_leftover = 0;
786 clear_bit(CF_APP_LIMITED, &con->flags);
787 clear_bit(CF_RECV_PENDING, &con->flags);
788 clear_bit(CF_SEND_PENDING, &con->flags);
789 up_write(&con->sock_lock);
792 static struct processqueue_entry *new_processqueue_entry(int nodeid,
795 struct processqueue_entry *pentry;
797 pentry = kmalloc(sizeof(*pentry), GFP_NOFS);
801 pentry->buf = kmalloc(buflen, GFP_NOFS);
807 pentry->nodeid = nodeid;
811 static void free_processqueue_entry(struct processqueue_entry *pentry)
817 struct dlm_processed_nodes {
820 struct list_head list;
823 static void add_processed_node(int nodeid, struct list_head *processed_nodes)
825 struct dlm_processed_nodes *n;
827 list_for_each_entry(n, processed_nodes, list) {
828 /* we already remembered this node */
829 if (n->nodeid == nodeid)
833 /* if it's fails in worst case we simple don't send an ack back.
834 * We try it next time.
836 n = kmalloc(sizeof(*n), GFP_NOFS);
841 list_add(&n->list, processed_nodes);
844 static void process_dlm_messages(struct work_struct *work)
846 struct dlm_processed_nodes *n, *n_tmp;
847 struct processqueue_entry *pentry;
848 LIST_HEAD(processed_nodes);
850 spin_lock(&processqueue_lock);
851 pentry = list_first_entry_or_null(&processqueue,
852 struct processqueue_entry, list);
853 if (WARN_ON_ONCE(!pentry)) {
854 spin_unlock(&processqueue_lock);
858 list_del(&pentry->list);
859 spin_unlock(&processqueue_lock);
862 dlm_process_incoming_buffer(pentry->nodeid, pentry->buf,
864 add_processed_node(pentry->nodeid, &processed_nodes);
865 free_processqueue_entry(pentry);
867 spin_lock(&processqueue_lock);
868 pentry = list_first_entry_or_null(&processqueue,
869 struct processqueue_entry, list);
871 process_dlm_messages_pending = false;
872 spin_unlock(&processqueue_lock);
876 list_del(&pentry->list);
877 spin_unlock(&processqueue_lock);
880 /* send ack back after we processed couple of messages */
881 list_for_each_entry_safe(n, n_tmp, &processed_nodes, list) {
883 dlm_midcomms_receive_done(n->nodeid);
888 /* Data received from remote end */
889 static int receive_from_sock(struct connection *con, int buflen)
891 struct processqueue_entry *pentry;
892 int ret, buflen_real;
896 pentry = new_processqueue_entry(con->nodeid, buflen);
898 return DLM_IO_RESCHED;
900 memcpy(pentry->buf, con->rx_leftover_buf, con->rx_leftover);
902 /* calculate new buffer parameter regarding last receive and
903 * possible leftover bytes
905 iov.iov_base = pentry->buf + con->rx_leftover;
906 iov.iov_len = buflen - con->rx_leftover;
908 memset(&msg, 0, sizeof(msg));
909 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
910 clear_bit(CF_RECV_INTR, &con->flags);
912 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
914 trace_dlm_recv(con->nodeid, ret);
915 if (ret == -EAGAIN) {
916 lock_sock(con->sock->sk);
917 if (test_and_clear_bit(CF_RECV_INTR, &con->flags)) {
918 release_sock(con->sock->sk);
922 clear_bit(CF_RECV_PENDING, &con->flags);
923 release_sock(con->sock->sk);
924 free_processqueue_entry(pentry);
926 } else if (ret == 0) {
927 /* close will clear CF_RECV_PENDING */
928 free_processqueue_entry(pentry);
930 } else if (ret < 0) {
931 free_processqueue_entry(pentry);
935 /* new buflen according readed bytes and leftover from last receive */
936 buflen_real = ret + con->rx_leftover;
937 ret = dlm_validate_incoming_buffer(con->nodeid, pentry->buf,
940 free_processqueue_entry(pentry);
944 pentry->buflen = ret;
946 /* calculate leftover bytes from process and put it into begin of
947 * the receive buffer, so next receive we have the full message
948 * at the start address of the receive buffer.
950 con->rx_leftover = buflen_real - ret;
951 memmove(con->rx_leftover_buf, pentry->buf + ret,
954 spin_lock(&processqueue_lock);
955 list_add_tail(&pentry->list, &processqueue);
956 if (!process_dlm_messages_pending) {
957 process_dlm_messages_pending = true;
958 queue_work(process_workqueue, &process_work);
960 spin_unlock(&processqueue_lock);
962 return DLM_IO_SUCCESS;
965 /* Listening socket is busy, accept a connection */
966 static int accept_from_sock(void)
968 struct sockaddr_storage peeraddr;
969 int len, idx, result, nodeid;
970 struct connection *newcon;
971 struct socket *newsock;
974 result = kernel_accept(listen_con.sock, &newsock, O_NONBLOCK);
975 if (result == -EAGAIN)
980 /* Get the connected socket's peer */
981 memset(&peeraddr, 0, sizeof(peeraddr));
982 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
984 result = -ECONNABORTED;
988 /* Get the new node's NODEID */
989 make_sockaddr(&peeraddr, 0, &len);
990 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
991 switch (peeraddr.ss_family) {
993 struct sockaddr_in *sin = (struct sockaddr_in *)&peeraddr;
995 log_print("connect from non cluster IPv4 node %pI4",
999 #if IS_ENABLED(CONFIG_IPV6)
1001 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&peeraddr;
1003 log_print("connect from non cluster IPv6 node %pI6c",
1009 log_print("invalid family from non cluster node");
1013 sock_release(newsock);
1017 log_print("got connection from %d", nodeid);
1019 /* Check to see if we already have a connection to this node. This
1020 * could happen if the two nodes initiate a connection at roughly
1021 * the same time and the connections cross on the wire.
1022 * In this case we store the incoming one in "othercon"
1024 idx = srcu_read_lock(&connections_srcu);
1025 newcon = nodeid2con(nodeid, 0);
1026 if (WARN_ON_ONCE(!newcon)) {
1027 srcu_read_unlock(&connections_srcu, idx);
1032 sock_set_mark(newsock->sk, mark);
1034 down_write(&newcon->sock_lock);
1036 struct connection *othercon = newcon->othercon;
1039 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
1041 log_print("failed to allocate incoming socket");
1042 up_write(&newcon->sock_lock);
1043 srcu_read_unlock(&connections_srcu, idx);
1048 dlm_con_init(othercon, nodeid);
1049 lockdep_set_subclass(&othercon->sock_lock, 1);
1050 newcon->othercon = othercon;
1051 set_bit(CF_IS_OTHERCON, &othercon->flags);
1053 /* close other sock con if we have something new */
1054 close_connection(othercon, false);
1057 down_write(&othercon->sock_lock);
1058 add_sock(newsock, othercon);
1060 /* check if we receved something while adding */
1061 lock_sock(othercon->sock->sk);
1062 lowcomms_queue_rwork(othercon);
1063 release_sock(othercon->sock->sk);
1064 up_write(&othercon->sock_lock);
1067 /* accept copies the sk after we've saved the callbacks, so we
1068 don't want to save them a second time or comm errors will
1069 result in calling sk_error_report recursively. */
1070 add_sock(newsock, newcon);
1072 /* check if we receved something while adding */
1073 lock_sock(newcon->sock->sk);
1074 lowcomms_queue_rwork(newcon);
1075 release_sock(newcon->sock->sk);
1077 up_write(&newcon->sock_lock);
1078 srcu_read_unlock(&connections_srcu, idx);
1080 return DLM_IO_SUCCESS;
1084 sock_release(newsock);
1090 * writequeue_entry_complete - try to delete and free write queue entry
1091 * @e: write queue entry to try to delete
1092 * @completed: bytes completed
1094 * writequeue_lock must be held.
1096 static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1098 e->offset += completed;
1099 e->len -= completed;
1100 /* signal that page was half way transmitted */
1103 if (e->len == 0 && e->users == 0)
1108 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1110 static int sctp_bind_addrs(struct socket *sock, uint16_t port)
1112 struct sockaddr_storage localaddr;
1113 struct sockaddr *addr = (struct sockaddr *)&localaddr;
1114 int i, addr_len, result = 0;
1116 for (i = 0; i < dlm_local_count; i++) {
1117 memcpy(&localaddr, &dlm_local_addr[i], sizeof(localaddr));
1118 make_sockaddr(&localaddr, port, &addr_len);
1121 result = kernel_bind(sock, addr, addr_len);
1123 result = sock_bind_add(sock->sk, addr, addr_len);
1126 log_print("Can't bind to %d addr number %d, %d.\n",
1127 port, i + 1, result);
1134 /* Get local addresses */
1135 static void init_local(void)
1137 struct sockaddr_storage sas;
1140 dlm_local_count = 0;
1141 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1142 if (dlm_our_addr(&sas, i))
1145 memcpy(&dlm_local_addr[dlm_local_count++], &sas, sizeof(sas));
1149 static struct writequeue_entry *new_writequeue_entry(struct connection *con)
1151 struct writequeue_entry *entry;
1153 entry = dlm_allocate_writequeue();
1157 entry->page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
1159 dlm_free_writequeue(entry);
1166 entry->dirty = false;
1169 kref_init(&entry->ref);
1173 static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
1174 char **ppc, void (*cb)(void *data),
1177 struct writequeue_entry *e;
1179 spin_lock_bh(&con->writequeue_lock);
1180 if (!list_empty(&con->writequeue)) {
1181 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1182 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
1185 *ppc = page_address(e->page) + e->end;
1195 e = new_writequeue_entry(con);
1200 *ppc = page_address(e->page);
1205 list_add_tail(&e->list, &con->writequeue);
1208 spin_unlock_bh(&con->writequeue_lock);
1212 static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1213 gfp_t allocation, char **ppc,
1214 void (*cb)(void *data),
1217 struct writequeue_entry *e;
1218 struct dlm_msg *msg;
1220 msg = dlm_allocate_msg(allocation);
1224 kref_init(&msg->ref);
1226 e = new_wq_entry(con, len, ppc, cb, data);
1232 msg->retransmit = false;
1233 msg->orig_msg = NULL;
1241 /* avoid false positive for nodes_srcu, unlock happens in
1242 * dlm_lowcomms_commit_msg which is a must call if success
1245 struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
1246 char **ppc, void (*cb)(void *data),
1249 struct connection *con;
1250 struct dlm_msg *msg;
1253 if (len > DLM_MAX_SOCKET_BUFSIZE ||
1254 len < sizeof(struct dlm_header)) {
1255 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
1256 log_print("failed to allocate a buffer of size %d", len);
1261 idx = srcu_read_lock(&connections_srcu);
1262 con = nodeid2con(nodeid, 0);
1263 if (WARN_ON_ONCE(!con)) {
1264 srcu_read_unlock(&connections_srcu, idx);
1268 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data);
1270 srcu_read_unlock(&connections_srcu, idx);
1274 /* for dlm_lowcomms_commit_msg() */
1275 kref_get(&msg->ref);
1276 /* we assume if successful commit must called */
1282 static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1284 struct writequeue_entry *e = msg->entry;
1285 struct connection *con = e->con;
1288 spin_lock_bh(&con->writequeue_lock);
1289 kref_get(&msg->ref);
1290 list_add(&msg->list, &e->msgs);
1296 e->len = DLM_WQ_LENGTH_BYTES(e);
1298 lowcomms_queue_swork(con);
1301 spin_unlock_bh(&con->writequeue_lock);
1305 /* avoid false positive for nodes_srcu, lock was happen in
1306 * dlm_lowcomms_new_msg
1309 void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1311 _dlm_lowcomms_commit_msg(msg);
1312 srcu_read_unlock(&connections_srcu, msg->idx);
1313 /* because dlm_lowcomms_new_msg() */
1314 kref_put(&msg->ref, dlm_msg_release);
1318 void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1320 kref_put(&msg->ref, dlm_msg_release);
1323 /* does not held connections_srcu, usage lowcomms_error_report only */
1324 int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1326 struct dlm_msg *msg_resend;
1329 if (msg->retransmit)
1332 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1333 GFP_ATOMIC, &ppc, NULL, NULL);
1337 msg->retransmit = true;
1338 kref_get(&msg->ref);
1339 msg_resend->orig_msg = msg;
1341 memcpy(ppc, msg->ppc, msg->len);
1342 _dlm_lowcomms_commit_msg(msg_resend);
1343 dlm_lowcomms_put_msg(msg_resend);
1348 /* Send a message */
1349 static int send_to_sock(struct connection *con)
1351 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1352 struct writequeue_entry *e;
1353 int len, offset, ret;
1355 spin_lock_bh(&con->writequeue_lock);
1356 e = con_next_wq(con);
1358 clear_bit(CF_SEND_PENDING, &con->flags);
1359 spin_unlock_bh(&con->writequeue_lock);
1365 WARN_ON_ONCE(len == 0 && e->users == 0);
1366 spin_unlock_bh(&con->writequeue_lock);
1368 ret = kernel_sendpage(con->sock, e->page, offset, len,
1370 trace_dlm_send(con->nodeid, ret);
1371 if (ret == -EAGAIN || ret == 0) {
1372 lock_sock(con->sock->sk);
1373 spin_lock_bh(&con->writequeue_lock);
1374 if (test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1375 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1376 /* Notify TCP that we're limited by the
1377 * application window size.
1379 set_bit(SOCK_NOSPACE, &con->sock->sk->sk_socket->flags);
1380 con->sock->sk->sk_write_pending++;
1382 clear_bit(CF_SEND_PENDING, &con->flags);
1383 spin_unlock_bh(&con->writequeue_lock);
1384 release_sock(con->sock->sk);
1386 /* wait for write_space() event */
1389 spin_unlock_bh(&con->writequeue_lock);
1390 release_sock(con->sock->sk);
1392 return DLM_IO_RESCHED;
1393 } else if (ret < 0) {
1397 spin_lock_bh(&con->writequeue_lock);
1398 writequeue_entry_complete(e, ret);
1399 spin_unlock_bh(&con->writequeue_lock);
1401 return DLM_IO_SUCCESS;
1404 static void clean_one_writequeue(struct connection *con)
1406 struct writequeue_entry *e, *safe;
1408 spin_lock_bh(&con->writequeue_lock);
1409 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1412 spin_unlock_bh(&con->writequeue_lock);
1415 static void connection_release(struct rcu_head *rcu)
1417 struct connection *con = container_of(rcu, struct connection, rcu);
1419 WARN_ON_ONCE(!list_empty(&con->writequeue));
1420 WARN_ON_ONCE(con->sock);
1424 /* Called from recovery when it knows that a node has
1426 int dlm_lowcomms_close(int nodeid)
1428 struct connection *con;
1431 log_print("closing connection to node %d", nodeid);
1433 idx = srcu_read_lock(&connections_srcu);
1434 con = nodeid2con(nodeid, 0);
1435 if (WARN_ON_ONCE(!con)) {
1436 srcu_read_unlock(&connections_srcu, idx);
1440 stop_connection_io(con);
1441 log_print("io handling for node: %d stopped", nodeid);
1442 close_connection(con, true);
1444 spin_lock(&connections_lock);
1445 hlist_del_rcu(&con->list);
1446 spin_unlock(&connections_lock);
1448 clean_one_writequeue(con);
1449 call_srcu(&connections_srcu, &con->rcu, connection_release);
1450 if (con->othercon) {
1451 clean_one_writequeue(con->othercon);
1453 call_srcu(&connections_srcu, &con->othercon->rcu, connection_release);
1455 srcu_read_unlock(&connections_srcu, idx);
1457 /* for debugging we print when we are done to compare with other
1458 * messages in between. This function need to be correctly synchronized
1461 log_print("closing connection to node %d done", nodeid);
1466 /* Receive worker function */
1467 static void process_recv_sockets(struct work_struct *work)
1469 struct connection *con = container_of(work, struct connection, rwork);
1472 down_read(&con->sock_lock);
1474 up_read(&con->sock_lock);
1478 buflen = READ_ONCE(dlm_config.ci_buffer_size);
1480 ret = receive_from_sock(con, buflen);
1481 } while (ret == DLM_IO_SUCCESS);
1482 up_read(&con->sock_lock);
1486 /* CF_RECV_PENDING cleared */
1489 close_connection(con, false);
1490 /* CF_RECV_PENDING cleared */
1492 case DLM_IO_RESCHED:
1494 queue_work(io_workqueue, &con->rwork);
1495 /* CF_RECV_PENDING not cleared */
1499 if (test_bit(CF_IS_OTHERCON, &con->flags)) {
1500 close_connection(con, false);
1502 spin_lock_bh(&con->writequeue_lock);
1503 lowcomms_queue_swork(con);
1504 spin_unlock_bh(&con->writequeue_lock);
1507 /* CF_RECV_PENDING cleared for othercon
1508 * we trigger send queue if not already done
1509 * and process_send_sockets will handle it
1519 static void process_listen_recv_socket(struct work_struct *work)
1523 if (WARN_ON_ONCE(!listen_con.sock))
1527 ret = accept_from_sock();
1528 } while (ret == DLM_IO_SUCCESS);
1531 log_print("critical error accepting connection: %d", ret);
1534 static int dlm_connect(struct connection *con)
1536 struct sockaddr_storage addr;
1537 int result, addr_len;
1538 struct socket *sock;
1541 memset(&addr, 0, sizeof(addr));
1542 result = nodeid_to_addr(con->nodeid, &addr, NULL,
1543 dlm_proto_ops->try_new_addr, &mark);
1545 log_print("no address for nodeid %d", con->nodeid);
1549 /* Create a socket to communicate with */
1550 result = sock_create_kern(&init_net, dlm_local_addr[0].ss_family,
1551 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1555 sock_set_mark(sock->sk, mark);
1556 dlm_proto_ops->sockopts(sock);
1558 result = dlm_proto_ops->bind(sock);
1564 add_sock(sock, con);
1566 log_print_ratelimited("connecting to %d", con->nodeid);
1567 make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1568 result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr,
1578 dlm_close_sock(&con->sock);
1586 /* Send worker function */
1587 static void process_send_sockets(struct work_struct *work)
1589 struct connection *con = container_of(work, struct connection, swork);
1592 WARN_ON_ONCE(test_bit(CF_IS_OTHERCON, &con->flags));
1594 down_read(&con->sock_lock);
1596 up_read(&con->sock_lock);
1597 down_write(&con->sock_lock);
1599 ret = dlm_connect(con);
1604 /* avoid spamming resched on connection
1605 * we might can switch to a state_change
1606 * event based mechanism if established
1611 /* CF_SEND_PENDING not cleared */
1612 up_write(&con->sock_lock);
1613 log_print("connect to node %d try %d error %d",
1614 con->nodeid, con->retries++, ret);
1616 /* For now we try forever to reconnect. In
1617 * future we should send a event to cluster
1618 * manager to fence itself after certain amount
1621 queue_work(io_workqueue, &con->swork);
1625 downgrade_write(&con->sock_lock);
1629 ret = send_to_sock(con);
1630 } while (ret == DLM_IO_SUCCESS);
1631 up_read(&con->sock_lock);
1635 /* CF_SEND_PENDING cleared */
1637 case DLM_IO_RESCHED:
1638 /* CF_SEND_PENDING not cleared */
1640 queue_work(io_workqueue, &con->swork);
1644 close_connection(con, false);
1646 /* CF_SEND_PENDING cleared */
1647 spin_lock_bh(&con->writequeue_lock);
1648 lowcomms_queue_swork(con);
1649 spin_unlock_bh(&con->writequeue_lock);
1658 static void work_stop(void)
1661 destroy_workqueue(io_workqueue);
1662 io_workqueue = NULL;
1665 if (process_workqueue) {
1666 destroy_workqueue(process_workqueue);
1667 process_workqueue = NULL;
1671 static int work_start(void)
1673 io_workqueue = alloc_workqueue("dlm_io", WQ_HIGHPRI | WQ_MEM_RECLAIM,
1675 if (!io_workqueue) {
1676 log_print("can't start dlm_io");
1680 /* ordered dlm message process queue,
1681 * should be converted to a tasklet
1683 process_workqueue = alloc_ordered_workqueue("dlm_process",
1684 WQ_HIGHPRI | WQ_MEM_RECLAIM);
1685 if (!process_workqueue) {
1686 log_print("can't start dlm_process");
1687 destroy_workqueue(io_workqueue);
1688 io_workqueue = NULL;
1695 void dlm_lowcomms_shutdown(void)
1697 /* stop lowcomms_listen_data_ready calls */
1698 lock_sock(listen_con.sock->sk);
1699 listen_con.sock->sk->sk_data_ready = listen_sock.sk_data_ready;
1700 release_sock(listen_con.sock->sk);
1702 cancel_work_sync(&listen_con.rwork);
1703 dlm_close_sock(&listen_con.sock);
1705 flush_workqueue(process_workqueue);
1708 void dlm_lowcomms_shutdown_node(int nodeid, bool force)
1710 struct connection *con;
1713 idx = srcu_read_lock(&connections_srcu);
1714 con = nodeid2con(nodeid, 0);
1715 if (WARN_ON_ONCE(!con)) {
1716 srcu_read_unlock(&connections_srcu, idx);
1720 flush_work(&con->swork);
1721 stop_connection_io(con);
1722 WARN_ON_ONCE(!force && !list_empty(&con->writequeue));
1723 close_connection(con, true);
1724 clean_one_writequeue(con);
1726 clean_one_writequeue(con->othercon);
1727 allow_connection_io(con);
1728 srcu_read_unlock(&connections_srcu, idx);
1731 void dlm_lowcomms_stop(void)
1734 dlm_proto_ops = NULL;
1737 static int dlm_listen_for_all(void)
1739 struct socket *sock;
1742 log_print("Using %s for communications",
1743 dlm_proto_ops->name);
1745 result = dlm_proto_ops->listen_validate();
1749 result = sock_create_kern(&init_net, dlm_local_addr[0].ss_family,
1750 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1752 log_print("Can't create comms socket: %d", result);
1756 sock_set_mark(sock->sk, dlm_config.ci_mark);
1757 dlm_proto_ops->listen_sockopts(sock);
1759 result = dlm_proto_ops->listen_bind(sock);
1763 lock_sock(sock->sk);
1764 listen_sock.sk_data_ready = sock->sk->sk_data_ready;
1765 listen_sock.sk_write_space = sock->sk->sk_write_space;
1766 listen_sock.sk_error_report = sock->sk->sk_error_report;
1767 listen_sock.sk_state_change = sock->sk->sk_state_change;
1769 listen_con.sock = sock;
1771 sock->sk->sk_allocation = GFP_NOFS;
1772 sock->sk->sk_data_ready = lowcomms_listen_data_ready;
1773 release_sock(sock->sk);
1775 result = sock->ops->listen(sock, 5);
1777 dlm_close_sock(&listen_con.sock);
1788 static int dlm_tcp_bind(struct socket *sock)
1790 struct sockaddr_storage src_addr;
1791 int result, addr_len;
1793 /* Bind to our cluster-known address connecting to avoid
1796 memcpy(&src_addr, &dlm_local_addr[0], sizeof(src_addr));
1797 make_sockaddr(&src_addr, 0, &addr_len);
1799 result = sock->ops->bind(sock, (struct sockaddr *)&src_addr,
1802 /* This *may* not indicate a critical error */
1803 log_print("could not bind for connect: %d", result);
1809 static int dlm_tcp_connect(struct connection *con, struct socket *sock,
1810 struct sockaddr *addr, int addr_len)
1812 return sock->ops->connect(sock, addr, addr_len, O_NONBLOCK);
1815 static int dlm_tcp_listen_validate(void)
1817 /* We don't support multi-homed hosts */
1818 if (dlm_local_count > 1) {
1819 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1826 static void dlm_tcp_sockopts(struct socket *sock)
1828 /* Turn off Nagle's algorithm */
1829 tcp_sock_set_nodelay(sock->sk);
1832 static void dlm_tcp_listen_sockopts(struct socket *sock)
1834 dlm_tcp_sockopts(sock);
1835 sock_set_reuseaddr(sock->sk);
1838 static int dlm_tcp_listen_bind(struct socket *sock)
1842 /* Bind to our port */
1843 make_sockaddr(&dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1844 return sock->ops->bind(sock, (struct sockaddr *)&dlm_local_addr[0],
1848 static const struct dlm_proto_ops dlm_tcp_ops = {
1850 .proto = IPPROTO_TCP,
1851 .connect = dlm_tcp_connect,
1852 .sockopts = dlm_tcp_sockopts,
1853 .bind = dlm_tcp_bind,
1854 .listen_validate = dlm_tcp_listen_validate,
1855 .listen_sockopts = dlm_tcp_listen_sockopts,
1856 .listen_bind = dlm_tcp_listen_bind,
1859 static int dlm_sctp_bind(struct socket *sock)
1861 return sctp_bind_addrs(sock, 0);
1864 static int dlm_sctp_connect(struct connection *con, struct socket *sock,
1865 struct sockaddr *addr, int addr_len)
1870 * Make sock->ops->connect() function return in specified time,
1871 * since O_NONBLOCK argument in connect() function does not work here,
1872 * then, we should restore the default value of this attribute.
1874 sock_set_sndtimeo(sock->sk, 5);
1875 ret = sock->ops->connect(sock, addr, addr_len, 0);
1876 sock_set_sndtimeo(sock->sk, 0);
1880 static int dlm_sctp_listen_validate(void)
1882 if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1883 log_print("SCTP is not enabled by this kernel");
1887 request_module("sctp");
1891 static int dlm_sctp_bind_listen(struct socket *sock)
1893 return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
1896 static void dlm_sctp_sockopts(struct socket *sock)
1898 /* Turn off Nagle's algorithm */
1899 sctp_sock_set_nodelay(sock->sk);
1900 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1903 static const struct dlm_proto_ops dlm_sctp_ops = {
1905 .proto = IPPROTO_SCTP,
1906 .try_new_addr = true,
1907 .connect = dlm_sctp_connect,
1908 .sockopts = dlm_sctp_sockopts,
1909 .bind = dlm_sctp_bind,
1910 .listen_validate = dlm_sctp_listen_validate,
1911 .listen_sockopts = dlm_sctp_sockopts,
1912 .listen_bind = dlm_sctp_bind_listen,
1915 int dlm_lowcomms_start(void)
1920 if (!dlm_local_count) {
1922 log_print("no local IP address has been set");
1926 error = work_start();
1930 /* Start listening */
1931 switch (dlm_config.ci_protocol) {
1933 dlm_proto_ops = &dlm_tcp_ops;
1935 case DLM_PROTO_SCTP:
1936 dlm_proto_ops = &dlm_sctp_ops;
1939 log_print("Invalid protocol identifier %d set",
1940 dlm_config.ci_protocol);
1942 goto fail_proto_ops;
1945 error = dlm_listen_for_all();
1952 dlm_proto_ops = NULL;
1959 void dlm_lowcomms_init(void)
1963 for (i = 0; i < CONN_HASH_SIZE; i++)
1964 INIT_HLIST_HEAD(&connection_hash[i]);
1966 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1969 void dlm_lowcomms_exit(void)
1971 struct connection *con;
1974 idx = srcu_read_lock(&connections_srcu);
1975 for (i = 0; i < CONN_HASH_SIZE; i++) {
1976 hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
1977 spin_lock(&connections_lock);
1978 hlist_del_rcu(&con->list);
1979 spin_unlock(&connections_lock);
1982 call_srcu(&connections_srcu, &con->othercon->rcu,
1983 connection_release);
1984 call_srcu(&connections_srcu, &con->rcu, connection_release);
1987 srcu_read_unlock(&connections_srcu, idx);