Thomas Gleixner | 2522fe4 | 2019-05-28 09:57:20 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 2 | /****************************************************************************** |
| 3 | ******************************************************************************* |
| 4 | ** |
| 5 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 6 | ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved. |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 7 | ** |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 8 | ** |
| 9 | ******************************************************************************* |
| 10 | ******************************************************************************/ |
| 11 | |
| 12 | /* |
| 13 | * lowcomms.c |
| 14 | * |
| 15 | * This is the "low-level" comms layer. |
| 16 | * |
| 17 | * It is responsible for sending/receiving messages |
| 18 | * from other nodes in the cluster. |
| 19 | * |
| 20 | * Cluster nodes are referred to by their nodeids. nodeids are |
| 21 | * simply 32 bit numbers to the locking module - if they need to |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 22 | * be expanded for the cluster infrastructure then that is its |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 23 | * responsibility. It is this layer's |
| 24 | * responsibility to resolve these into IP address or |
| 25 | * whatever it needs for inter-node communication. |
| 26 | * |
| 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. |
| 36 | * |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 37 | * lowcomms will choose to use either TCP or SCTP as its transport layer |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 38 | * depending on the configuration variable 'protocol'. This should be set |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 39 | * to 0 (default) for TCP or 1 for SCTP. It should be configured using a |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 40 | * cluster-wide mechanism as it must be the same on all nodes of the cluster |
| 41 | * for the DLM to function. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | #include <asm/ioctls.h> |
| 46 | #include <net/sock.h> |
| 47 | #include <net/tcp.h> |
| 48 | #include <linux/pagemap.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 49 | #include <linux/file.h> |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 50 | #include <linux/mutex.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 51 | #include <linux/sctp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
Benjamin Poirier | 2f2d76c | 2012-03-08 05:55:59 +0000 | [diff] [blame] | 53 | #include <net/sctp/sctp.h> |
Joe Perches | 44ad532 | 2009-01-22 13:24:49 -0800 | [diff] [blame] | 54 | #include <net/ipv6.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 55 | |
Alexander Aring | 9273237 | 2021-11-02 15:17:16 -0400 | [diff] [blame] | 56 | #include <trace/events/dlm.h> |
| 57 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 58 | #include "dlm_internal.h" |
| 59 | #include "lowcomms.h" |
| 60 | #include "midcomms.h" |
| 61 | #include "config.h" |
| 62 | |
| 63 | #define NEEDED_RMEM (4*1024*1024) |
| 64 | |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 65 | /* Number of messages to send before rescheduling */ |
| 66 | #define MAX_SEND_MSG_COUNT 25 |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 67 | #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000) |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 68 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 69 | struct connection { |
| 70 | struct socket *sock; /* NULL if not connected */ |
| 71 | uint32_t nodeid; /* So we know who we are in the list */ |
| 72 | struct mutex sock_mutex; |
| 73 | unsigned long flags; |
| 74 | #define CF_READ_PENDING 1 |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 75 | #define CF_WRITE_PENDING 2 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 76 | #define CF_INIT_PENDING 4 |
| 77 | #define CF_IS_OTHERCON 5 |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 78 | #define CF_CLOSE 6 |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 79 | #define CF_APP_LIMITED 7 |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 80 | #define CF_CLOSING 8 |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 81 | #define CF_SHUTDOWN 9 |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 82 | #define CF_CONNECTED 10 |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 83 | #define CF_RECONNECT 11 |
| 84 | #define CF_DELAY_CONNECT 12 |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 85 | #define CF_EOF 13 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 86 | struct list_head writequeue; /* List of outgoing writequeue_entries */ |
| 87 | spinlock_t writequeue_lock; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 88 | atomic_t writequeue_cnt; |
Alexander Aring | c51b022 | 2021-07-16 16:22:44 -0400 | [diff] [blame] | 89 | struct mutex wq_alloc; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 90 | int retries; |
| 91 | #define MAX_CONNECT_RETRIES 3 |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 92 | struct hlist_node list; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 93 | struct connection *othercon; |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 94 | struct connection *sendcon; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 95 | struct work_struct rwork; /* Receive workqueue */ |
| 96 | struct work_struct swork; /* Send workqueue */ |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 97 | wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */ |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 98 | unsigned char *rx_buf; |
| 99 | int rx_buflen; |
| 100 | int rx_leftover; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 101 | struct rcu_head rcu; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 102 | }; |
| 103 | #define sock2con(x) ((struct connection *)(x)->sk_user_data) |
| 104 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 105 | struct listen_connection { |
| 106 | struct socket *sock; |
| 107 | struct work_struct rwork; |
| 108 | }; |
| 109 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 110 | #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end) |
| 111 | #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset) |
| 112 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 113 | /* An entry waiting to be sent */ |
| 114 | struct writequeue_entry { |
| 115 | struct list_head list; |
| 116 | struct page *page; |
| 117 | int offset; |
| 118 | int len; |
| 119 | int end; |
| 120 | int users; |
Alexander Aring | 706474f | 2021-05-21 15:08:48 -0400 | [diff] [blame] | 121 | bool dirty; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 122 | struct connection *con; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 123 | struct list_head msgs; |
| 124 | struct kref ref; |
| 125 | }; |
| 126 | |
| 127 | struct dlm_msg { |
| 128 | struct writequeue_entry *entry; |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 129 | struct dlm_msg *orig_msg; |
| 130 | bool retransmit; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 131 | void *ppc; |
| 132 | int len; |
| 133 | int idx; /* new()/commit() idx exchange */ |
| 134 | |
| 135 | struct list_head list; |
| 136 | struct kref ref; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 139 | struct dlm_node_addr { |
| 140 | struct list_head list; |
| 141 | int nodeid; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 142 | int mark; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 143 | int addr_count; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 144 | int curr_addr_index; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 145 | struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT]; |
| 146 | }; |
| 147 | |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 148 | struct dlm_proto_ops { |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 149 | bool try_new_addr; |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 150 | const char *name; |
| 151 | int proto; |
| 152 | |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 153 | int (*connect)(struct connection *con, struct socket *sock, |
| 154 | struct sockaddr *addr, int addr_len); |
| 155 | void (*sockopts)(struct socket *sock); |
| 156 | int (*bind)(struct socket *sock); |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 157 | int (*listen_validate)(void); |
| 158 | void (*listen_sockopts)(struct socket *sock); |
| 159 | int (*listen_bind)(struct socket *sock); |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 160 | /* What to do to shutdown */ |
| 161 | void (*shutdown_action)(struct connection *con); |
| 162 | /* What to do to eof check */ |
| 163 | bool (*eof_condition)(struct connection *con); |
| 164 | }; |
| 165 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 166 | static struct listen_sock_callbacks { |
| 167 | void (*sk_error_report)(struct sock *); |
| 168 | void (*sk_data_ready)(struct sock *); |
| 169 | void (*sk_state_change)(struct sock *); |
| 170 | void (*sk_write_space)(struct sock *); |
| 171 | } listen_sock; |
| 172 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 173 | static LIST_HEAD(dlm_node_addrs); |
| 174 | static DEFINE_SPINLOCK(dlm_node_addrs_spin); |
| 175 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 176 | static struct listen_connection listen_con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 177 | static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT]; |
| 178 | static int dlm_local_count; |
Alexander Aring | 5174616 | 2021-03-01 17:05:13 -0500 | [diff] [blame] | 179 | int dlm_allow_conn; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 180 | |
| 181 | /* Work queues */ |
| 182 | static struct workqueue_struct *recv_workqueue; |
| 183 | static struct workqueue_struct *send_workqueue; |
| 184 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 185 | static struct hlist_head connection_hash[CONN_HASH_SIZE]; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 186 | static DEFINE_SPINLOCK(connections_lock); |
| 187 | DEFINE_STATIC_SRCU(connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 188 | |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 189 | static const struct dlm_proto_ops *dlm_proto_ops; |
| 190 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 191 | static void process_recv_sockets(struct work_struct *work); |
| 192 | static void process_send_sockets(struct work_struct *work); |
| 193 | |
Alexander Aring | 66d5955 | 2021-07-16 16:22:39 -0400 | [diff] [blame] | 194 | /* need to held writequeue_lock */ |
| 195 | static struct writequeue_entry *con_next_wq(struct connection *con) |
| 196 | { |
| 197 | struct writequeue_entry *e; |
| 198 | |
| 199 | if (list_empty(&con->writequeue)) |
| 200 | return NULL; |
| 201 | |
| 202 | e = list_first_entry(&con->writequeue, struct writequeue_entry, |
| 203 | list); |
| 204 | if (e->len == 0) |
| 205 | return NULL; |
| 206 | |
| 207 | return e; |
| 208 | } |
| 209 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 210 | static struct connection *__find_con(int nodeid, int r) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 211 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 212 | struct connection *con; |
| 213 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 214 | hlist_for_each_entry_rcu(con, &connection_hash[r], list) { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 215 | if (con->nodeid == nodeid) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 216 | return con; |
| 217 | } |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 218 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 222 | static bool tcp_eof_condition(struct connection *con) |
| 223 | { |
| 224 | return atomic_read(&con->writequeue_cnt); |
| 225 | } |
| 226 | |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 227 | static int dlm_con_init(struct connection *con, int nodeid) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 228 | { |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 229 | con->rx_buflen = dlm_config.ci_buffer_size; |
| 230 | con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS); |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 231 | if (!con->rx_buf) |
| 232 | return -ENOMEM; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 233 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 234 | con->nodeid = nodeid; |
| 235 | mutex_init(&con->sock_mutex); |
| 236 | INIT_LIST_HEAD(&con->writequeue); |
| 237 | spin_lock_init(&con->writequeue_lock); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 238 | atomic_set(&con->writequeue_cnt, 0); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 239 | INIT_WORK(&con->swork, process_send_sockets); |
| 240 | INIT_WORK(&con->rwork, process_recv_sockets); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 241 | init_waitqueue_head(&con->shutdown_wait); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 242 | |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * If 'allocation' is zero then we don't attempt to create a new |
| 248 | * connection structure for this node. |
| 249 | */ |
| 250 | static struct connection *nodeid2con(int nodeid, gfp_t alloc) |
| 251 | { |
| 252 | struct connection *con, *tmp; |
| 253 | int r, ret; |
| 254 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 255 | r = nodeid_hash(nodeid); |
| 256 | con = __find_con(nodeid, r); |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 257 | if (con || !alloc) |
| 258 | return con; |
| 259 | |
| 260 | con = kzalloc(sizeof(*con), alloc); |
| 261 | if (!con) |
| 262 | return NULL; |
| 263 | |
| 264 | ret = dlm_con_init(con, nodeid); |
| 265 | if (ret) { |
| 266 | kfree(con); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Alexander Aring | c51b022 | 2021-07-16 16:22:44 -0400 | [diff] [blame] | 270 | mutex_init(&con->wq_alloc); |
| 271 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 272 | spin_lock(&connections_lock); |
Alexander Aring | 4f2b30f | 2020-09-30 18:37:29 -0400 | [diff] [blame] | 273 | /* Because multiple workqueues/threads calls this function it can |
| 274 | * race on multiple cpu's. Instead of locking hot path __find_con() |
| 275 | * we just check in rare cases of recently added nodes again |
| 276 | * under protection of connections_lock. If this is the case we |
| 277 | * abort our connection creation and return the existing connection. |
| 278 | */ |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 279 | tmp = __find_con(nodeid, r); |
Alexander Aring | 4f2b30f | 2020-09-30 18:37:29 -0400 | [diff] [blame] | 280 | if (tmp) { |
| 281 | spin_unlock(&connections_lock); |
| 282 | kfree(con->rx_buf); |
| 283 | kfree(con); |
| 284 | return tmp; |
| 285 | } |
| 286 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 287 | hlist_add_head_rcu(&con->list, &connection_hash[r]); |
| 288 | spin_unlock(&connections_lock); |
| 289 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 290 | return con; |
| 291 | } |
| 292 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 293 | /* Loop round all connections */ |
| 294 | static void foreach_conn(void (*conn_func)(struct connection *c)) |
| 295 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 296 | int i; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 297 | struct connection *con; |
| 298 | |
| 299 | for (i = 0; i < CONN_HASH_SIZE; i++) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 300 | hlist_for_each_entry_rcu(con, &connection_hash[i], list) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 301 | conn_func(con); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 302 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 303 | } |
| 304 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 305 | static struct dlm_node_addr *find_node_addr(int nodeid) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 306 | { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 307 | struct dlm_node_addr *na; |
| 308 | |
| 309 | list_for_each_entry(na, &dlm_node_addrs, list) { |
| 310 | if (na->nodeid == nodeid) |
| 311 | return na; |
| 312 | } |
| 313 | return NULL; |
| 314 | } |
| 315 | |
Alexander Aring | 40c6b83 | 2020-11-02 20:04:27 -0500 | [diff] [blame] | 316 | static int addr_compare(const struct sockaddr_storage *x, |
| 317 | const struct sockaddr_storage *y) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 318 | { |
| 319 | switch (x->ss_family) { |
| 320 | case AF_INET: { |
| 321 | struct sockaddr_in *sinx = (struct sockaddr_in *)x; |
| 322 | struct sockaddr_in *siny = (struct sockaddr_in *)y; |
| 323 | if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr) |
| 324 | return 0; |
| 325 | if (sinx->sin_port != siny->sin_port) |
| 326 | return 0; |
| 327 | break; |
| 328 | } |
| 329 | case AF_INET6: { |
| 330 | struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x; |
| 331 | struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y; |
| 332 | if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr)) |
| 333 | return 0; |
| 334 | if (sinx->sin6_port != siny->sin6_port) |
| 335 | return 0; |
| 336 | break; |
| 337 | } |
| 338 | default: |
| 339 | return 0; |
| 340 | } |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out, |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 345 | struct sockaddr *sa_out, bool try_new_addr, |
| 346 | unsigned int *mark) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 347 | { |
| 348 | struct sockaddr_storage sas; |
| 349 | struct dlm_node_addr *na; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 350 | |
| 351 | if (!dlm_local_count) |
| 352 | return -1; |
| 353 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 354 | spin_lock(&dlm_node_addrs_spin); |
| 355 | na = find_node_addr(nodeid); |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 356 | if (na && na->addr_count) { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 357 | memcpy(&sas, na->addr[na->curr_addr_index], |
| 358 | sizeof(struct sockaddr_storage)); |
| 359 | |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 360 | if (try_new_addr) { |
| 361 | na->curr_addr_index++; |
| 362 | if (na->curr_addr_index == na->addr_count) |
| 363 | na->curr_addr_index = 0; |
| 364 | } |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 365 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 366 | spin_unlock(&dlm_node_addrs_spin); |
| 367 | |
| 368 | if (!na) |
| 369 | return -EEXIST; |
| 370 | |
| 371 | if (!na->addr_count) |
| 372 | return -ENOENT; |
| 373 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 374 | *mark = na->mark; |
| 375 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 376 | if (sas_out) |
| 377 | memcpy(sas_out, &sas, sizeof(struct sockaddr_storage)); |
| 378 | |
| 379 | if (!sa_out) |
| 380 | return 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 381 | |
| 382 | if (dlm_local_addr[0]->ss_family == AF_INET) { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 383 | struct sockaddr_in *in4 = (struct sockaddr_in *) &sas; |
| 384 | struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 385 | ret4->sin_addr.s_addr = in4->sin_addr.s_addr; |
| 386 | } else { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 387 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas; |
| 388 | struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 389 | ret6->sin6_addr = in6->sin6_addr; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 395 | static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid, |
| 396 | unsigned int *mark) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 397 | { |
| 398 | struct dlm_node_addr *na; |
| 399 | int rv = -EEXIST; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 400 | int addr_i; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 401 | |
| 402 | spin_lock(&dlm_node_addrs_spin); |
| 403 | list_for_each_entry(na, &dlm_node_addrs, list) { |
| 404 | if (!na->addr_count) |
| 405 | continue; |
| 406 | |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 407 | for (addr_i = 0; addr_i < na->addr_count; addr_i++) { |
| 408 | if (addr_compare(na->addr[addr_i], addr)) { |
| 409 | *nodeid = na->nodeid; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 410 | *mark = na->mark; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 411 | rv = 0; |
| 412 | goto unlock; |
| 413 | } |
| 414 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 415 | } |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 416 | unlock: |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 417 | spin_unlock(&dlm_node_addrs_spin); |
| 418 | return rv; |
| 419 | } |
| 420 | |
Alexander Aring | 4f19d07 | 2020-11-02 20:04:28 -0500 | [diff] [blame] | 421 | /* caller need to held dlm_node_addrs_spin lock */ |
| 422 | static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na, |
| 423 | const struct sockaddr_storage *addr) |
| 424 | { |
| 425 | int i; |
| 426 | |
| 427 | for (i = 0; i < na->addr_count; i++) { |
| 428 | if (addr_compare(na->addr[i], addr)) |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | return false; |
| 433 | } |
| 434 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 435 | int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len) |
| 436 | { |
| 437 | struct sockaddr_storage *new_addr; |
| 438 | struct dlm_node_addr *new_node, *na; |
Alexander Aring | 4f19d07 | 2020-11-02 20:04:28 -0500 | [diff] [blame] | 439 | bool ret; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 440 | |
| 441 | new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS); |
| 442 | if (!new_node) |
| 443 | return -ENOMEM; |
| 444 | |
| 445 | new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS); |
| 446 | if (!new_addr) { |
| 447 | kfree(new_node); |
| 448 | return -ENOMEM; |
| 449 | } |
| 450 | |
| 451 | memcpy(new_addr, addr, len); |
| 452 | |
| 453 | spin_lock(&dlm_node_addrs_spin); |
| 454 | na = find_node_addr(nodeid); |
| 455 | if (!na) { |
| 456 | new_node->nodeid = nodeid; |
| 457 | new_node->addr[0] = new_addr; |
| 458 | new_node->addr_count = 1; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 459 | new_node->mark = dlm_config.ci_mark; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 460 | list_add(&new_node->list, &dlm_node_addrs); |
| 461 | spin_unlock(&dlm_node_addrs_spin); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Alexander Aring | 4f19d07 | 2020-11-02 20:04:28 -0500 | [diff] [blame] | 465 | ret = dlm_lowcomms_na_has_addr(na, addr); |
| 466 | if (ret) { |
| 467 | spin_unlock(&dlm_node_addrs_spin); |
| 468 | kfree(new_addr); |
| 469 | kfree(new_node); |
| 470 | return -EEXIST; |
| 471 | } |
| 472 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 473 | if (na->addr_count >= DLM_MAX_ADDR_COUNT) { |
| 474 | spin_unlock(&dlm_node_addrs_spin); |
| 475 | kfree(new_addr); |
| 476 | kfree(new_node); |
| 477 | return -ENOSPC; |
| 478 | } |
| 479 | |
| 480 | na->addr[na->addr_count++] = new_addr; |
| 481 | spin_unlock(&dlm_node_addrs_spin); |
| 482 | kfree(new_node); |
| 483 | return 0; |
| 484 | } |
| 485 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 486 | /* Data available on socket or listen socket received a connect */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 487 | static void lowcomms_data_ready(struct sock *sk) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 488 | { |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 489 | struct connection *con; |
| 490 | |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 491 | con = sock2con(sk); |
Patrick Caulfield | afb853f | 2007-06-01 10:07:26 -0500 | [diff] [blame] | 492 | if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 493 | queue_work(recv_workqueue, &con->rwork); |
| 494 | } |
| 495 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 496 | static void lowcomms_listen_data_ready(struct sock *sk) |
| 497 | { |
Alexander Aring | 9a4139a | 2021-06-02 09:45:18 -0400 | [diff] [blame] | 498 | if (!dlm_allow_conn) |
| 499 | return; |
| 500 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 501 | queue_work(recv_workqueue, &listen_con.rwork); |
| 502 | } |
| 503 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 504 | static void lowcomms_write_space(struct sock *sk) |
| 505 | { |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 506 | struct connection *con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 507 | |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 508 | con = sock2con(sk); |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 509 | if (!con) |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 510 | return; |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 511 | |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 512 | if (!test_and_set_bit(CF_CONNECTED, &con->flags)) { |
| 513 | log_print("successful connected to node %d", con->nodeid); |
| 514 | queue_work(send_workqueue, &con->swork); |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 515 | return; |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 516 | } |
| 517 | |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 518 | clear_bit(SOCK_NOSPACE, &con->sock->flags); |
| 519 | |
| 520 | if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) { |
| 521 | con->sock->sk->sk_write_pending--; |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 522 | clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags); |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 525 | queue_work(send_workqueue, &con->swork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static inline void lowcomms_connect_sock(struct connection *con) |
| 529 | { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 530 | if (test_bit(CF_CLOSE, &con->flags)) |
| 531 | return; |
Bob Peterson | 61d9102b | 2017-09-12 08:55:04 +0000 | [diff] [blame] | 532 | queue_work(send_workqueue, &con->swork); |
| 533 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void lowcomms_state_change(struct sock *sk) |
| 537 | { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 538 | /* SCTP layer is not calling sk_data_ready when the connection |
| 539 | * is done, so we catch the signal through here. Also, it |
| 540 | * doesn't switch socket state when entering shutdown, so we |
| 541 | * skip the write in that case. |
| 542 | */ |
| 543 | if (sk->sk_shutdown) { |
| 544 | if (sk->sk_shutdown == RCV_SHUTDOWN) |
| 545 | lowcomms_data_ready(sk); |
| 546 | } else if (sk->sk_state == TCP_ESTABLISHED) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 547 | lowcomms_write_space(sk); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 548 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 549 | } |
| 550 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 551 | int dlm_lowcomms_connect_node(int nodeid) |
| 552 | { |
| 553 | struct connection *con; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 554 | int idx; |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 555 | |
| 556 | if (nodeid == dlm_our_nodeid()) |
| 557 | return 0; |
| 558 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 559 | idx = srcu_read_lock(&connections_srcu); |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 560 | con = nodeid2con(nodeid, GFP_NOFS); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 561 | if (!con) { |
| 562 | srcu_read_unlock(&connections_srcu, idx); |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 563 | return -ENOMEM; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 564 | } |
| 565 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 566 | lowcomms_connect_sock(con); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 567 | srcu_read_unlock(&connections_srcu, idx); |
| 568 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 572 | int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark) |
| 573 | { |
| 574 | struct dlm_node_addr *na; |
| 575 | |
| 576 | spin_lock(&dlm_node_addrs_spin); |
| 577 | na = find_node_addr(nodeid); |
| 578 | if (!na) { |
| 579 | spin_unlock(&dlm_node_addrs_spin); |
| 580 | return -ENOENT; |
| 581 | } |
| 582 | |
| 583 | na->mark = mark; |
| 584 | spin_unlock(&dlm_node_addrs_spin); |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 589 | static void lowcomms_error_report(struct sock *sk) |
| 590 | { |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 591 | struct connection *con; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 592 | void (*orig_report)(struct sock *) = NULL; |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 593 | struct inet_sock *inet; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 594 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 595 | con = sock2con(sk); |
| 596 | if (con == NULL) |
| 597 | goto out; |
| 598 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 599 | orig_report = listen_sock.sk_error_report; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 600 | |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 601 | inet = inet_sk(sk); |
| 602 | switch (sk->sk_family) { |
| 603 | case AF_INET: |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 604 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 605 | "sending to node %d at %pI4, dport %d, " |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 606 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 607 | con->nodeid, &inet->inet_daddr, |
| 608 | ntohs(inet->inet_dport), sk->sk_err, |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 609 | sk->sk_err_soft); |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 610 | break; |
Alexander Aring | 1b9beda | 2021-11-17 09:20:43 -0500 | [diff] [blame^] | 611 | #if IS_ENABLED(CONFIG_IPV6) |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 612 | case AF_INET6: |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 613 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 614 | "sending to node %d at %pI6c, " |
| 615 | "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(), |
| 616 | con->nodeid, &sk->sk_v6_daddr, |
| 617 | ntohs(inet->inet_dport), sk->sk_err, |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 618 | sk->sk_err_soft); |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 619 | break; |
Alexander Aring | 1b9beda | 2021-11-17 09:20:43 -0500 | [diff] [blame^] | 620 | #endif |
Alexander Aring | 4c3d905 | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 621 | default: |
| 622 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
| 623 | "invalid socket family %d set, " |
| 624 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
| 625 | sk->sk_family, sk->sk_err, sk->sk_err_soft); |
| 626 | goto out; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 627 | } |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 628 | |
| 629 | /* below sendcon only handling */ |
| 630 | if (test_bit(CF_IS_OTHERCON, &con->flags)) |
| 631 | con = con->sendcon; |
| 632 | |
| 633 | switch (sk->sk_err) { |
| 634 | case ECONNREFUSED: |
| 635 | set_bit(CF_DELAY_CONNECT, &con->flags); |
| 636 | break; |
| 637 | default: |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | if (!test_and_set_bit(CF_RECONNECT, &con->flags)) |
| 642 | queue_work(send_workqueue, &con->swork); |
| 643 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 644 | out: |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 645 | if (orig_report) |
| 646 | orig_report(sk); |
| 647 | } |
| 648 | |
| 649 | /* Note: sk_callback_lock must be locked before calling this function. */ |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 650 | static void save_listen_callbacks(struct socket *sock) |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 651 | { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 652 | struct sock *sk = sock->sk; |
| 653 | |
| 654 | listen_sock.sk_data_ready = sk->sk_data_ready; |
| 655 | listen_sock.sk_state_change = sk->sk_state_change; |
| 656 | listen_sock.sk_write_space = sk->sk_write_space; |
| 657 | listen_sock.sk_error_report = sk->sk_error_report; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 660 | static void restore_callbacks(struct socket *sock) |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 661 | { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 662 | struct sock *sk = sock->sk; |
| 663 | |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 664 | lock_sock(sk); |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 665 | sk->sk_user_data = NULL; |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 666 | sk->sk_data_ready = listen_sock.sk_data_ready; |
| 667 | sk->sk_state_change = listen_sock.sk_state_change; |
| 668 | sk->sk_write_space = listen_sock.sk_write_space; |
| 669 | sk->sk_error_report = listen_sock.sk_error_report; |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 670 | release_sock(sk); |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 671 | } |
| 672 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 673 | static void add_listen_sock(struct socket *sock, struct listen_connection *con) |
| 674 | { |
| 675 | struct sock *sk = sock->sk; |
| 676 | |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 677 | lock_sock(sk); |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 678 | save_listen_callbacks(sock); |
| 679 | con->sock = sock; |
| 680 | |
| 681 | sk->sk_user_data = con; |
| 682 | sk->sk_allocation = GFP_NOFS; |
| 683 | /* Install a data_ready callback */ |
| 684 | sk->sk_data_ready = lowcomms_listen_data_ready; |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 685 | release_sock(sk); |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 686 | } |
| 687 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 688 | /* Make a socket active */ |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 689 | static void add_sock(struct socket *sock, struct connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 690 | { |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 691 | struct sock *sk = sock->sk; |
| 692 | |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 693 | lock_sock(sk); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 694 | con->sock = sock; |
| 695 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 696 | sk->sk_user_data = con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 697 | /* Install a data_ready callback */ |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 698 | sk->sk_data_ready = lowcomms_data_ready; |
| 699 | sk->sk_write_space = lowcomms_write_space; |
| 700 | sk->sk_state_change = lowcomms_state_change; |
| 701 | sk->sk_allocation = GFP_NOFS; |
| 702 | sk->sk_error_report = lowcomms_error_report; |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 703 | release_sock(sk); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* Add the port number to an IPv6 or 4 sockaddr and return the address |
| 707 | length */ |
| 708 | static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, |
| 709 | int *addr_len) |
| 710 | { |
| 711 | saddr->ss_family = dlm_local_addr[0]->ss_family; |
| 712 | if (saddr->ss_family == AF_INET) { |
| 713 | struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr; |
| 714 | in4_addr->sin_port = cpu_to_be16(port); |
| 715 | *addr_len = sizeof(struct sockaddr_in); |
| 716 | memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero)); |
| 717 | } else { |
| 718 | struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr; |
| 719 | in6_addr->sin6_port = cpu_to_be16(port); |
| 720 | *addr_len = sizeof(struct sockaddr_in6); |
| 721 | } |
Patrick Caulfield | 01c8cab | 2007-07-17 16:53:15 +0100 | [diff] [blame] | 722 | memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Alexander Aring | 706474f | 2021-05-21 15:08:48 -0400 | [diff] [blame] | 725 | static void dlm_page_release(struct kref *kref) |
| 726 | { |
| 727 | struct writequeue_entry *e = container_of(kref, struct writequeue_entry, |
| 728 | ref); |
| 729 | |
| 730 | __free_page(e->page); |
| 731 | kfree(e); |
| 732 | } |
| 733 | |
| 734 | static void dlm_msg_release(struct kref *kref) |
| 735 | { |
| 736 | struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref); |
| 737 | |
| 738 | kref_put(&msg->entry->ref, dlm_page_release); |
| 739 | kfree(msg); |
| 740 | } |
| 741 | |
| 742 | static void free_entry(struct writequeue_entry *e) |
| 743 | { |
| 744 | struct dlm_msg *msg, *tmp; |
| 745 | |
| 746 | list_for_each_entry_safe(msg, tmp, &e->msgs, list) { |
| 747 | if (msg->orig_msg) { |
| 748 | msg->orig_msg->retransmit = false; |
| 749 | kref_put(&msg->orig_msg->ref, dlm_msg_release); |
| 750 | } |
| 751 | |
| 752 | list_del(&msg->list); |
| 753 | kref_put(&msg->ref, dlm_msg_release); |
| 754 | } |
| 755 | |
| 756 | list_del(&e->list); |
| 757 | atomic_dec(&e->con->writequeue_cnt); |
| 758 | kref_put(&e->ref, dlm_page_release); |
| 759 | } |
| 760 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 761 | static void dlm_close_sock(struct socket **sock) |
| 762 | { |
| 763 | if (*sock) { |
| 764 | restore_callbacks(*sock); |
| 765 | sock_release(*sock); |
| 766 | *sock = NULL; |
| 767 | } |
| 768 | } |
| 769 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 770 | /* Close a remote connection and tidy up */ |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 771 | static void close_connection(struct connection *con, bool and_other, |
| 772 | bool tx, bool rx) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 773 | { |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 774 | bool closing = test_and_set_bit(CF_CLOSING, &con->flags); |
Alexander Aring | 706474f | 2021-05-21 15:08:48 -0400 | [diff] [blame] | 775 | struct writequeue_entry *e; |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 776 | |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 777 | if (tx && !closing && cancel_work_sync(&con->swork)) { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 778 | log_print("canceled swork for node %d", con->nodeid); |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 779 | clear_bit(CF_WRITE_PENDING, &con->flags); |
| 780 | } |
| 781 | if (rx && !closing && cancel_work_sync(&con->rwork)) { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 782 | log_print("canceled rwork for node %d", con->nodeid); |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 783 | clear_bit(CF_READ_PENDING, &con->flags); |
| 784 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 785 | |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 786 | mutex_lock(&con->sock_mutex); |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 787 | dlm_close_sock(&con->sock); |
| 788 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 789 | if (con->othercon && and_other) { |
| 790 | /* Will only re-enter once. */ |
Alexander Aring | c6aa00e3 | 2021-05-21 15:08:38 -0400 | [diff] [blame] | 791 | close_connection(con->othercon, false, tx, rx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 792 | } |
Patrick Caulfield | 9e5f282 | 2007-08-02 14:58:14 +0100 | [diff] [blame] | 793 | |
Alexander Aring | 706474f | 2021-05-21 15:08:48 -0400 | [diff] [blame] | 794 | /* if we send a writequeue entry only a half way, we drop the |
| 795 | * whole entry because reconnection and that we not start of the |
| 796 | * middle of a msg which will confuse the other end. |
| 797 | * |
| 798 | * we can always drop messages because retransmits, but what we |
| 799 | * cannot allow is to transmit half messages which may be processed |
| 800 | * at the other side. |
| 801 | * |
| 802 | * our policy is to start on a clean state when disconnects, we don't |
| 803 | * know what's send/received on transport layer in this case. |
| 804 | */ |
| 805 | spin_lock(&con->writequeue_lock); |
| 806 | if (!list_empty(&con->writequeue)) { |
| 807 | e = list_first_entry(&con->writequeue, struct writequeue_entry, |
| 808 | list); |
| 809 | if (e->dirty) |
| 810 | free_entry(e); |
| 811 | } |
| 812 | spin_unlock(&con->writequeue_lock); |
| 813 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 814 | con->rx_leftover = 0; |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 815 | con->retries = 0; |
Alexander Aring | 052849b | 2021-07-16 16:22:37 -0400 | [diff] [blame] | 816 | clear_bit(CF_APP_LIMITED, &con->flags); |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 817 | clear_bit(CF_CONNECTED, &con->flags); |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 818 | clear_bit(CF_DELAY_CONNECT, &con->flags); |
| 819 | clear_bit(CF_RECONNECT, &con->flags); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 820 | clear_bit(CF_EOF, &con->flags); |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 821 | mutex_unlock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 822 | clear_bit(CF_CLOSING, &con->flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 825 | static void shutdown_connection(struct connection *con) |
| 826 | { |
| 827 | int ret; |
| 828 | |
Alexander Aring | eec054b | 2021-03-01 17:05:19 -0500 | [diff] [blame] | 829 | flush_work(&con->swork); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 830 | |
| 831 | mutex_lock(&con->sock_mutex); |
| 832 | /* nothing to shutdown */ |
| 833 | if (!con->sock) { |
| 834 | mutex_unlock(&con->sock_mutex); |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | set_bit(CF_SHUTDOWN, &con->flags); |
| 839 | ret = kernel_sock_shutdown(con->sock, SHUT_WR); |
| 840 | mutex_unlock(&con->sock_mutex); |
| 841 | if (ret) { |
| 842 | log_print("Connection %p failed to shutdown: %d will force close", |
| 843 | con, ret); |
| 844 | goto force_close; |
| 845 | } else { |
| 846 | ret = wait_event_timeout(con->shutdown_wait, |
| 847 | !test_bit(CF_SHUTDOWN, &con->flags), |
| 848 | DLM_SHUTDOWN_WAIT_TIMEOUT); |
| 849 | if (ret == 0) { |
| 850 | log_print("Connection %p shutdown timed out, will force close", |
| 851 | con); |
| 852 | goto force_close; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | return; |
| 857 | |
| 858 | force_close: |
| 859 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 860 | close_connection(con, false, true, true); |
| 861 | } |
| 862 | |
| 863 | static void dlm_tcp_shutdown(struct connection *con) |
| 864 | { |
| 865 | if (con->othercon) |
| 866 | shutdown_connection(con->othercon); |
| 867 | shutdown_connection(con); |
| 868 | } |
| 869 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 870 | static int con_realloc_receive_buf(struct connection *con, int newlen) |
| 871 | { |
| 872 | unsigned char *newbuf; |
| 873 | |
| 874 | newbuf = kmalloc(newlen, GFP_NOFS); |
| 875 | if (!newbuf) |
| 876 | return -ENOMEM; |
| 877 | |
| 878 | /* copy any leftover from last receive */ |
| 879 | if (con->rx_leftover) |
| 880 | memmove(newbuf, con->rx_buf, con->rx_leftover); |
| 881 | |
| 882 | /* swap to new buffer space */ |
| 883 | kfree(con->rx_buf); |
| 884 | con->rx_buflen = newlen; |
| 885 | con->rx_buf = newbuf; |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 890 | /* Data received from remote end */ |
| 891 | static int receive_from_sock(struct connection *con) |
| 892 | { |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 893 | struct msghdr msg; |
| 894 | struct kvec iov; |
| 895 | int ret, buflen; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 896 | |
| 897 | mutex_lock(&con->sock_mutex); |
| 898 | |
| 899 | if (con->sock == NULL) { |
| 900 | ret = -EAGAIN; |
| 901 | goto out_close; |
| 902 | } |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 903 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 904 | /* realloc if we get new buffer size to read out */ |
| 905 | buflen = dlm_config.ci_buffer_size; |
| 906 | if (con->rx_buflen != buflen && con->rx_leftover <= buflen) { |
| 907 | ret = con_realloc_receive_buf(con, buflen); |
| 908 | if (ret < 0) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 909 | goto out_resched; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Alexander Aring | 62699b3 | 2021-07-16 16:22:45 -0400 | [diff] [blame] | 912 | for (;;) { |
| 913 | /* calculate new buffer parameter regarding last receive and |
| 914 | * possible leftover bytes |
| 915 | */ |
| 916 | iov.iov_base = con->rx_buf + con->rx_leftover; |
| 917 | iov.iov_len = con->rx_buflen - con->rx_leftover; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 918 | |
Alexander Aring | 62699b3 | 2021-07-16 16:22:45 -0400 | [diff] [blame] | 919 | memset(&msg, 0, sizeof(msg)); |
| 920 | msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 921 | ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len, |
| 922 | msg.msg_flags); |
Alexander Aring | 9273237 | 2021-11-02 15:17:16 -0400 | [diff] [blame] | 923 | trace_dlm_recv(con->nodeid, ret); |
Alexander Aring | 62699b3 | 2021-07-16 16:22:45 -0400 | [diff] [blame] | 924 | if (ret == -EAGAIN) |
| 925 | break; |
| 926 | else if (ret <= 0) |
| 927 | goto out_close; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 928 | |
Alexander Aring | 62699b3 | 2021-07-16 16:22:45 -0400 | [diff] [blame] | 929 | /* new buflen according readed bytes and leftover from last receive */ |
| 930 | buflen = ret + con->rx_leftover; |
| 931 | ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen); |
| 932 | if (ret < 0) |
| 933 | goto out_close; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 934 | |
Alexander Aring | 62699b3 | 2021-07-16 16:22:45 -0400 | [diff] [blame] | 935 | /* calculate leftover bytes from process and put it into begin of |
| 936 | * the receive buffer, so next receive we have the full message |
| 937 | * at the start address of the receive buffer. |
| 938 | */ |
| 939 | con->rx_leftover = buflen - ret; |
| 940 | if (con->rx_leftover) { |
| 941 | memmove(con->rx_buf, con->rx_buf + ret, |
| 942 | con->rx_leftover); |
| 943 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 944 | } |
| 945 | |
Alexander Aring | b97f852 | 2021-08-18 16:27:13 -0400 | [diff] [blame] | 946 | dlm_midcomms_receive_done(con->nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 947 | mutex_unlock(&con->sock_mutex); |
| 948 | return 0; |
| 949 | |
| 950 | out_resched: |
| 951 | if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) |
| 952 | queue_work(recv_workqueue, &con->rwork); |
| 953 | mutex_unlock(&con->sock_mutex); |
| 954 | return -EAGAIN; |
| 955 | |
| 956 | out_close: |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 957 | if (ret == 0) { |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 958 | log_print("connection %p got EOF from %d", |
| 959 | con, con->nodeid); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 960 | |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 961 | if (dlm_proto_ops->eof_condition && |
| 962 | dlm_proto_ops->eof_condition(con)) { |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 963 | set_bit(CF_EOF, &con->flags); |
| 964 | mutex_unlock(&con->sock_mutex); |
| 965 | } else { |
| 966 | mutex_unlock(&con->sock_mutex); |
| 967 | close_connection(con, false, true, false); |
| 968 | |
| 969 | /* handling for tcp shutdown */ |
| 970 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 971 | wake_up(&con->shutdown_wait); |
| 972 | } |
| 973 | |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 974 | /* signal to breaking receive worker */ |
| 975 | ret = -1; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 976 | } else { |
| 977 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 978 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | /* Listening socket is busy, accept a connection */ |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 983 | static int accept_from_sock(struct listen_connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 984 | { |
| 985 | int result; |
| 986 | struct sockaddr_storage peeraddr; |
| 987 | struct socket *newsock; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 988 | int len, idx; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 989 | int nodeid; |
| 990 | struct connection *newcon; |
| 991 | struct connection *addcon; |
Alexander Aring | 3f78cd7 | 2020-09-24 10:31:23 -0400 | [diff] [blame] | 992 | unsigned int mark; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 993 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 994 | if (!con->sock) |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 995 | return -ENOTCONN; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 996 | |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 997 | result = kernel_accept(con->sock, &newsock, O_NONBLOCK); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 998 | if (result < 0) |
| 999 | goto accept_err; |
| 1000 | |
| 1001 | /* Get the connected socket's peer */ |
| 1002 | memset(&peeraddr, 0, sizeof(peeraddr)); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1003 | len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2); |
| 1004 | if (len < 0) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1005 | result = -ECONNABORTED; |
| 1006 | goto accept_err; |
| 1007 | } |
| 1008 | |
| 1009 | /* Get the new node's NODEID */ |
| 1010 | make_sockaddr(&peeraddr, 0, &len); |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 1011 | if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) { |
Masatake YAMATO | bcaadf5 | 2011-07-04 12:25:51 +0900 | [diff] [blame] | 1012 | unsigned char *b=(unsigned char *)&peeraddr; |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1013 | log_print("connect from non cluster node"); |
Masatake YAMATO | bcaadf5 | 2011-07-04 12:25:51 +0900 | [diff] [blame] | 1014 | print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE, |
| 1015 | b, sizeof(struct sockaddr_storage)); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1016 | sock_release(newsock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1017 | return -1; |
| 1018 | } |
| 1019 | |
| 1020 | log_print("got connection from %d", nodeid); |
| 1021 | |
| 1022 | /* Check to see if we already have a connection to this node. This |
| 1023 | * could happen if the two nodes initiate a connection at roughly |
| 1024 | * the same time and the connections cross on the wire. |
| 1025 | * In this case we store the incoming one in "othercon" |
| 1026 | */ |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1027 | idx = srcu_read_lock(&connections_srcu); |
David Teigland | 748285c | 2009-05-15 10:50:57 -0500 | [diff] [blame] | 1028 | newcon = nodeid2con(nodeid, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1029 | if (!newcon) { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1030 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1031 | result = -ENOMEM; |
| 1032 | goto accept_err; |
| 1033 | } |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1034 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 1035 | sock_set_mark(newsock->sk, mark); |
| 1036 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1037 | mutex_lock(&newcon->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1038 | if (newcon->sock) { |
| 1039 | struct connection *othercon = newcon->othercon; |
| 1040 | |
| 1041 | if (!othercon) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1042 | othercon = kzalloc(sizeof(*othercon), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1043 | if (!othercon) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1044 | log_print("failed to allocate incoming socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1045 | mutex_unlock(&newcon->sock_mutex); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1046 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1047 | result = -ENOMEM; |
| 1048 | goto accept_err; |
| 1049 | } |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1050 | |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 1051 | result = dlm_con_init(othercon, nodeid); |
| 1052 | if (result < 0) { |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1053 | kfree(othercon); |
Yang Yingliang | 2fd8db2 | 2021-03-27 16:37:04 +0800 | [diff] [blame] | 1054 | mutex_unlock(&newcon->sock_mutex); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1055 | srcu_read_unlock(&connections_srcu, idx); |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1056 | goto accept_err; |
| 1057 | } |
| 1058 | |
Alexander Aring | e9a470a | 2021-03-01 17:05:11 -0500 | [diff] [blame] | 1059 | lockdep_set_subclass(&othercon->sock_mutex, 1); |
Alexander Aring | 7443bc9 | 2021-05-21 15:08:36 -0400 | [diff] [blame] | 1060 | set_bit(CF_IS_OTHERCON, &othercon->flags); |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 1061 | newcon->othercon = othercon; |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1062 | othercon->sendcon = newcon; |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 1063 | } else { |
| 1064 | /* close other sock con if we have something new */ |
| 1065 | close_connection(othercon, false, true, false); |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 1066 | } |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 1067 | |
Alexander Aring | e9a470a | 2021-03-01 17:05:11 -0500 | [diff] [blame] | 1068 | mutex_lock(&othercon->sock_mutex); |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 1069 | add_sock(newsock, othercon); |
| 1070 | addcon = othercon; |
| 1071 | mutex_unlock(&othercon->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1072 | } |
| 1073 | else { |
Bob Peterson | 3735b4b | 2016-09-23 14:23:26 -0400 | [diff] [blame] | 1074 | /* accept copies the sk after we've saved the callbacks, so we |
| 1075 | don't want to save them a second time or comm errors will |
| 1076 | result in calling sk_error_report recursively. */ |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1077 | add_sock(newsock, newcon); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1078 | addcon = newcon; |
| 1079 | } |
| 1080 | |
Alexander Aring | b30a624 | 2021-03-01 17:05:10 -0500 | [diff] [blame] | 1081 | set_bit(CF_CONNECTED, &addcon->flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1082 | mutex_unlock(&newcon->sock_mutex); |
| 1083 | |
| 1084 | /* |
| 1085 | * Add it to the active queue in case we got data |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1086 | * between processing the accept adding the socket |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1087 | * to the read_sockets list |
| 1088 | */ |
| 1089 | if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) |
| 1090 | queue_work(recv_workqueue, &addcon->rwork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1091 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1092 | srcu_read_unlock(&connections_srcu, idx); |
| 1093 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1094 | return 0; |
| 1095 | |
| 1096 | accept_err: |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 1097 | if (newsock) |
| 1098 | sock_release(newsock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1099 | |
| 1100 | if (result != -EAGAIN) |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1101 | log_print("error accepting connection from node: %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1102 | return result; |
| 1103 | } |
| 1104 | |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1105 | /* |
| 1106 | * writequeue_entry_complete - try to delete and free write queue entry |
| 1107 | * @e: write queue entry to try to delete |
| 1108 | * @completed: bytes completed |
| 1109 | * |
| 1110 | * writequeue_lock must be held. |
| 1111 | */ |
| 1112 | static void writequeue_entry_complete(struct writequeue_entry *e, int completed) |
| 1113 | { |
| 1114 | e->offset += completed; |
| 1115 | e->len -= completed; |
Alexander Aring | 706474f | 2021-05-21 15:08:48 -0400 | [diff] [blame] | 1116 | /* signal that page was half way transmitted */ |
| 1117 | e->dirty = true; |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1118 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1119 | if (e->len == 0 && e->users == 0) |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1120 | free_entry(e); |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1121 | } |
| 1122 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1123 | /* |
| 1124 | * sctp_bind_addrs - bind a SCTP socket to all our addresses |
| 1125 | */ |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1126 | static int sctp_bind_addrs(struct socket *sock, uint16_t port) |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1127 | { |
| 1128 | struct sockaddr_storage localaddr; |
Christoph Hellwig | c0425a4 | 2020-05-29 14:09:42 +0200 | [diff] [blame] | 1129 | struct sockaddr *addr = (struct sockaddr *)&localaddr; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1130 | int i, addr_len, result = 0; |
| 1131 | |
| 1132 | for (i = 0; i < dlm_local_count; i++) { |
| 1133 | memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr)); |
| 1134 | make_sockaddr(&localaddr, port, &addr_len); |
| 1135 | |
| 1136 | if (!i) |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1137 | result = kernel_bind(sock, addr, addr_len); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1138 | else |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1139 | result = sock_bind_add(sock->sk, addr, addr_len); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1140 | |
| 1141 | if (result < 0) { |
| 1142 | log_print("Can't bind to %d addr number %d, %d.\n", |
| 1143 | port, i + 1, result); |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | return result; |
| 1148 | } |
| 1149 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1150 | /* Get local addresses */ |
| 1151 | static void init_local(void) |
| 1152 | { |
| 1153 | struct sockaddr_storage sas, *addr; |
| 1154 | int i; |
| 1155 | |
Patrick Caulfield | 30d3a23 | 2007-04-23 16:26:21 +0100 | [diff] [blame] | 1156 | dlm_local_count = 0; |
David Teigland | 1b189b8 | 2012-03-21 09:18:34 -0500 | [diff] [blame] | 1157 | for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1158 | if (dlm_our_addr(&sas, i)) |
| 1159 | break; |
| 1160 | |
Amitoj Kaur Chawla | 5c93f56 | 2016-06-23 10:22:01 +0530 | [diff] [blame] | 1161 | addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1162 | if (!addr) |
| 1163 | break; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1164 | dlm_local_addr[dlm_local_count++] = addr; |
| 1165 | } |
| 1166 | } |
| 1167 | |
Alexander Aring | 043697f | 2020-08-27 15:02:50 -0400 | [diff] [blame] | 1168 | static void deinit_local(void) |
| 1169 | { |
| 1170 | int i; |
| 1171 | |
| 1172 | for (i = 0; i < dlm_local_count; i++) |
| 1173 | kfree(dlm_local_addr[i]); |
| 1174 | } |
| 1175 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1176 | static struct writequeue_entry *new_writequeue_entry(struct connection *con, |
| 1177 | gfp_t allocation) |
| 1178 | { |
| 1179 | struct writequeue_entry *entry; |
| 1180 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1181 | entry = kzalloc(sizeof(*entry), allocation); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1182 | if (!entry) |
| 1183 | return NULL; |
| 1184 | |
Alexander Aring | e1a7cbc | 2021-03-01 17:05:15 -0500 | [diff] [blame] | 1185 | entry->page = alloc_page(allocation | __GFP_ZERO); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1186 | if (!entry->page) { |
| 1187 | kfree(entry); |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1191 | entry->con = con; |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1192 | entry->users = 1; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1193 | kref_init(&entry->ref); |
| 1194 | INIT_LIST_HEAD(&entry->msgs); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1195 | |
| 1196 | return entry; |
| 1197 | } |
| 1198 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1199 | static struct writequeue_entry *new_wq_entry(struct connection *con, int len, |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1200 | gfp_t allocation, char **ppc, |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1201 | void (*cb)(void *data), void *data) |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1202 | { |
| 1203 | struct writequeue_entry *e; |
| 1204 | |
| 1205 | spin_lock(&con->writequeue_lock); |
| 1206 | if (!list_empty(&con->writequeue)) { |
| 1207 | e = list_last_entry(&con->writequeue, struct writequeue_entry, list); |
| 1208 | if (DLM_WQ_REMAIN_BYTES(e) >= len) { |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1209 | kref_get(&e->ref); |
| 1210 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1211 | *ppc = page_address(e->page) + e->end; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1212 | if (cb) |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1213 | cb(data); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1214 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1215 | e->end += len; |
| 1216 | e->users++; |
| 1217 | spin_unlock(&con->writequeue_lock); |
| 1218 | |
| 1219 | return e; |
| 1220 | } |
| 1221 | } |
| 1222 | spin_unlock(&con->writequeue_lock); |
| 1223 | |
| 1224 | e = new_writequeue_entry(con, allocation); |
| 1225 | if (!e) |
| 1226 | return NULL; |
| 1227 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1228 | kref_get(&e->ref); |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1229 | *ppc = page_address(e->page); |
| 1230 | e->end += len; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 1231 | atomic_inc(&con->writequeue_cnt); |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1232 | |
| 1233 | spin_lock(&con->writequeue_lock); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1234 | if (cb) |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1235 | cb(data); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1236 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1237 | list_add_tail(&e->list, &con->writequeue); |
| 1238 | spin_unlock(&con->writequeue_lock); |
| 1239 | |
| 1240 | return e; |
| 1241 | }; |
| 1242 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1243 | static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len, |
| 1244 | gfp_t allocation, char **ppc, |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1245 | void (*cb)(void *data), |
| 1246 | void *data) |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1247 | { |
| 1248 | struct writequeue_entry *e; |
| 1249 | struct dlm_msg *msg; |
Alexander Aring | c51b022 | 2021-07-16 16:22:44 -0400 | [diff] [blame] | 1250 | bool sleepable; |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1251 | |
| 1252 | msg = kzalloc(sizeof(*msg), allocation); |
| 1253 | if (!msg) |
| 1254 | return NULL; |
| 1255 | |
Alexander Aring | c51b022 | 2021-07-16 16:22:44 -0400 | [diff] [blame] | 1256 | /* this mutex is being used as a wait to avoid multiple "fast" |
| 1257 | * new writequeue page list entry allocs in new_wq_entry in |
| 1258 | * normal operation which is sleepable context. Without it |
| 1259 | * we could end in multiple writequeue entries with one |
| 1260 | * dlm message because multiple callers were waiting at |
| 1261 | * the writequeue_lock in new_wq_entry(). |
| 1262 | */ |
| 1263 | sleepable = gfpflags_normal_context(allocation); |
| 1264 | if (sleepable) |
| 1265 | mutex_lock(&con->wq_alloc); |
| 1266 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1267 | kref_init(&msg->ref); |
| 1268 | |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1269 | e = new_wq_entry(con, len, allocation, ppc, cb, data); |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1270 | if (!e) { |
Alexander Aring | c51b022 | 2021-07-16 16:22:44 -0400 | [diff] [blame] | 1271 | if (sleepable) |
| 1272 | mutex_unlock(&con->wq_alloc); |
| 1273 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1274 | kfree(msg); |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
Alexander Aring | c51b022 | 2021-07-16 16:22:44 -0400 | [diff] [blame] | 1278 | if (sleepable) |
| 1279 | mutex_unlock(&con->wq_alloc); |
| 1280 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1281 | msg->ppc = *ppc; |
| 1282 | msg->len = len; |
| 1283 | msg->entry = e; |
| 1284 | |
| 1285 | return msg; |
| 1286 | } |
| 1287 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1288 | struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation, |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1289 | char **ppc, void (*cb)(void *data), |
| 1290 | void *data) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1291 | { |
| 1292 | struct connection *con; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1293 | struct dlm_msg *msg; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1294 | int idx; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1295 | |
Alexander Aring | d10a0b8 | 2021-06-02 09:45:20 -0400 | [diff] [blame] | 1296 | if (len > DLM_MAX_SOCKET_BUFSIZE || |
Alexander Aring | c45674f | 2021-03-01 17:05:14 -0500 | [diff] [blame] | 1297 | len < sizeof(struct dlm_header)) { |
Alexander Aring | d10a0b8 | 2021-06-02 09:45:20 -0400 | [diff] [blame] | 1298 | BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE); |
Alexander Aring | 692f51c | 2020-11-02 20:04:18 -0500 | [diff] [blame] | 1299 | log_print("failed to allocate a buffer of size %d", len); |
Alexander Aring | c45674f | 2021-03-01 17:05:14 -0500 | [diff] [blame] | 1300 | WARN_ON(1); |
Alexander Aring | 692f51c | 2020-11-02 20:04:18 -0500 | [diff] [blame] | 1301 | return NULL; |
| 1302 | } |
| 1303 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1304 | idx = srcu_read_lock(&connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1305 | con = nodeid2con(nodeid, allocation); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1306 | if (!con) { |
| 1307 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1308 | return NULL; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1309 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1310 | |
Alexander Aring | 5c16feb | 2021-11-02 15:17:19 -0400 | [diff] [blame] | 1311 | msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1312 | if (!msg) { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1313 | srcu_read_unlock(&connections_srcu, idx); |
| 1314 | return NULL; |
| 1315 | } |
| 1316 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1317 | /* we assume if successful commit must called */ |
| 1318 | msg->idx = idx; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1319 | return msg; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1322 | static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1323 | { |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1324 | struct writequeue_entry *e = msg->entry; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1325 | struct connection *con = e->con; |
| 1326 | int users; |
| 1327 | |
| 1328 | spin_lock(&con->writequeue_lock); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1329 | kref_get(&msg->ref); |
| 1330 | list_add(&msg->list, &e->msgs); |
| 1331 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1332 | users = --e->users; |
| 1333 | if (users) |
| 1334 | goto out; |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1335 | |
| 1336 | e->len = DLM_WQ_LENGTH_BYTES(e); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1337 | spin_unlock(&con->writequeue_lock); |
| 1338 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1339 | queue_work(send_workqueue, &con->swork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1340 | return; |
| 1341 | |
| 1342 | out: |
| 1343 | spin_unlock(&con->writequeue_lock); |
| 1344 | return; |
| 1345 | } |
| 1346 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1347 | void dlm_lowcomms_commit_msg(struct dlm_msg *msg) |
| 1348 | { |
| 1349 | _dlm_lowcomms_commit_msg(msg); |
| 1350 | srcu_read_unlock(&connections_srcu, msg->idx); |
| 1351 | } |
| 1352 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame] | 1353 | void dlm_lowcomms_put_msg(struct dlm_msg *msg) |
| 1354 | { |
| 1355 | kref_put(&msg->ref, dlm_msg_release); |
| 1356 | } |
| 1357 | |
Alexander Aring | 2874d1a | 2021-05-21 15:08:43 -0400 | [diff] [blame] | 1358 | /* does not held connections_srcu, usage workqueue only */ |
| 1359 | int dlm_lowcomms_resend_msg(struct dlm_msg *msg) |
| 1360 | { |
| 1361 | struct dlm_msg *msg_resend; |
| 1362 | char *ppc; |
| 1363 | |
| 1364 | if (msg->retransmit) |
| 1365 | return 1; |
| 1366 | |
| 1367 | msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len, |
| 1368 | GFP_ATOMIC, &ppc, NULL, NULL); |
| 1369 | if (!msg_resend) |
| 1370 | return -ENOMEM; |
| 1371 | |
| 1372 | msg->retransmit = true; |
| 1373 | kref_get(&msg->ref); |
| 1374 | msg_resend->orig_msg = msg; |
| 1375 | |
| 1376 | memcpy(ppc, msg->ppc, msg->len); |
| 1377 | _dlm_lowcomms_commit_msg(msg_resend); |
| 1378 | dlm_lowcomms_put_msg(msg_resend); |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1383 | /* Send a message */ |
| 1384 | static void send_to_sock(struct connection *con) |
| 1385 | { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1386 | const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 1387 | struct writequeue_entry *e; |
Alexander Aring | 66d5955 | 2021-07-16 16:22:39 -0400 | [diff] [blame] | 1388 | int len, offset, ret; |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1389 | int count = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1390 | |
| 1391 | mutex_lock(&con->sock_mutex); |
| 1392 | if (con->sock == NULL) |
| 1393 | goto out_connect; |
| 1394 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1395 | spin_lock(&con->writequeue_lock); |
| 1396 | for (;;) { |
Alexander Aring | 66d5955 | 2021-07-16 16:22:39 -0400 | [diff] [blame] | 1397 | e = con_next_wq(con); |
| 1398 | if (!e) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1399 | break; |
| 1400 | |
| 1401 | len = e->len; |
| 1402 | offset = e->offset; |
| 1403 | BUG_ON(len == 0 && e->users == 0); |
| 1404 | spin_unlock(&con->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1405 | |
Alexander Aring | 66d5955 | 2021-07-16 16:22:39 -0400 | [diff] [blame] | 1406 | ret = kernel_sendpage(con->sock, e->page, offset, len, |
| 1407 | msg_flags); |
Alexander Aring | 9273237 | 2021-11-02 15:17:16 -0400 | [diff] [blame] | 1408 | trace_dlm_send(con->nodeid, ret); |
Alexander Aring | 66d5955 | 2021-07-16 16:22:39 -0400 | [diff] [blame] | 1409 | if (ret == -EAGAIN || ret == 0) { |
| 1410 | if (ret == -EAGAIN && |
| 1411 | test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) && |
| 1412 | !test_and_set_bit(CF_APP_LIMITED, &con->flags)) { |
| 1413 | /* Notify TCP that we're limited by the |
| 1414 | * application window size. |
| 1415 | */ |
| 1416 | set_bit(SOCK_NOSPACE, &con->sock->flags); |
| 1417 | con->sock->sk->sk_write_pending++; |
| 1418 | } |
| 1419 | cond_resched(); |
| 1420 | goto out; |
| 1421 | } else if (ret < 0) |
| 1422 | goto out; |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1423 | |
| 1424 | /* Don't starve people filling buffers */ |
| 1425 | if (++count >= MAX_SEND_MSG_COUNT) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1426 | cond_resched(); |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1427 | count = 0; |
| 1428 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1429 | |
| 1430 | spin_lock(&con->writequeue_lock); |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1431 | writequeue_entry_complete(e, ret); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1432 | } |
| 1433 | spin_unlock(&con->writequeue_lock); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 1434 | |
| 1435 | /* close if we got EOF */ |
| 1436 | if (test_and_clear_bit(CF_EOF, &con->flags)) { |
| 1437 | mutex_unlock(&con->sock_mutex); |
| 1438 | close_connection(con, false, false, true); |
| 1439 | |
| 1440 | /* handling for tcp shutdown */ |
| 1441 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 1442 | wake_up(&con->shutdown_wait); |
| 1443 | } else { |
| 1444 | mutex_unlock(&con->sock_mutex); |
| 1445 | } |
| 1446 | |
| 1447 | return; |
| 1448 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1449 | out: |
| 1450 | mutex_unlock(&con->sock_mutex); |
| 1451 | return; |
| 1452 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1453 | out_connect: |
| 1454 | mutex_unlock(&con->sock_mutex); |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1455 | queue_work(send_workqueue, &con->swork); |
| 1456 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static void clean_one_writequeue(struct connection *con) |
| 1460 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1461 | struct writequeue_entry *e, *safe; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1462 | |
| 1463 | spin_lock(&con->writequeue_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1464 | list_for_each_entry_safe(e, safe, &con->writequeue, list) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1465 | free_entry(e); |
| 1466 | } |
| 1467 | spin_unlock(&con->writequeue_lock); |
| 1468 | } |
| 1469 | |
| 1470 | /* Called from recovery when it knows that a node has |
| 1471 | left the cluster */ |
| 1472 | int dlm_lowcomms_close(int nodeid) |
| 1473 | { |
| 1474 | struct connection *con; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1475 | struct dlm_node_addr *na; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1476 | int idx; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1477 | |
| 1478 | log_print("closing connection to node %d", nodeid); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1479 | idx = srcu_read_lock(&connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1480 | con = nodeid2con(nodeid, 0); |
| 1481 | if (con) { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1482 | set_bit(CF_CLOSE, &con->flags); |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 1483 | close_connection(con, true, true, true); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1484 | clean_one_writequeue(con); |
Alexander Aring | 53a5eda | 2020-11-02 20:04:19 -0500 | [diff] [blame] | 1485 | if (con->othercon) |
| 1486 | clean_one_writequeue(con->othercon); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1487 | } |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1488 | srcu_read_unlock(&connections_srcu, idx); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1489 | |
| 1490 | spin_lock(&dlm_node_addrs_spin); |
| 1491 | na = find_node_addr(nodeid); |
| 1492 | if (na) { |
| 1493 | list_del(&na->list); |
| 1494 | while (na->addr_count--) |
| 1495 | kfree(na->addr[na->addr_count]); |
| 1496 | kfree(na); |
| 1497 | } |
| 1498 | spin_unlock(&dlm_node_addrs_spin); |
| 1499 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | /* Receive workqueue function */ |
| 1504 | static void process_recv_sockets(struct work_struct *work) |
| 1505 | { |
| 1506 | struct connection *con = container_of(work, struct connection, rwork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1507 | |
| 1508 | clear_bit(CF_READ_PENDING, &con->flags); |
Alexander Aring | 62699b3 | 2021-07-16 16:22:45 -0400 | [diff] [blame] | 1509 | receive_from_sock(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1512 | static void process_listen_recv_socket(struct work_struct *work) |
| 1513 | { |
| 1514 | accept_from_sock(&listen_con); |
| 1515 | } |
| 1516 | |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1517 | static void dlm_connect(struct connection *con) |
| 1518 | { |
| 1519 | struct sockaddr_storage addr; |
| 1520 | int result, addr_len; |
| 1521 | struct socket *sock; |
| 1522 | unsigned int mark; |
| 1523 | |
| 1524 | /* Some odd races can cause double-connects, ignore them */ |
| 1525 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 1526 | return; |
| 1527 | |
| 1528 | if (con->sock) { |
| 1529 | log_print("node %d already connected.", con->nodeid); |
| 1530 | return; |
| 1531 | } |
| 1532 | |
| 1533 | memset(&addr, 0, sizeof(addr)); |
| 1534 | result = nodeid_to_addr(con->nodeid, &addr, NULL, |
| 1535 | dlm_proto_ops->try_new_addr, &mark); |
| 1536 | if (result < 0) { |
| 1537 | log_print("no address for nodeid %d", con->nodeid); |
| 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | /* Create a socket to communicate with */ |
| 1542 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1543 | SOCK_STREAM, dlm_proto_ops->proto, &sock); |
| 1544 | if (result < 0) |
| 1545 | goto socket_err; |
| 1546 | |
| 1547 | sock_set_mark(sock->sk, mark); |
| 1548 | dlm_proto_ops->sockopts(sock); |
| 1549 | |
| 1550 | add_sock(sock, con); |
| 1551 | |
| 1552 | result = dlm_proto_ops->bind(sock); |
| 1553 | if (result < 0) |
| 1554 | goto add_sock_err; |
| 1555 | |
| 1556 | log_print_ratelimited("connecting to %d", con->nodeid); |
| 1557 | make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len); |
| 1558 | result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr, |
| 1559 | addr_len); |
| 1560 | if (result < 0) |
| 1561 | goto add_sock_err; |
| 1562 | |
| 1563 | return; |
| 1564 | |
| 1565 | add_sock_err: |
| 1566 | dlm_close_sock(&con->sock); |
| 1567 | |
| 1568 | socket_err: |
| 1569 | /* |
| 1570 | * Some errors are fatal and this list might need adjusting. For other |
| 1571 | * errors we try again until the max number of retries is reached. |
| 1572 | */ |
| 1573 | if (result != -EHOSTUNREACH && |
| 1574 | result != -ENETUNREACH && |
| 1575 | result != -ENETDOWN && |
| 1576 | result != -EINVAL && |
| 1577 | result != -EPROTONOSUPPORT) { |
| 1578 | log_print("connect %d try %d error %d", con->nodeid, |
| 1579 | con->retries, result); |
| 1580 | msleep(1000); |
| 1581 | lowcomms_connect_sock(con); |
| 1582 | } |
| 1583 | } |
| 1584 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1585 | /* Send workqueue function */ |
| 1586 | static void process_send_sockets(struct work_struct *work) |
| 1587 | { |
| 1588 | struct connection *con = container_of(work, struct connection, swork); |
| 1589 | |
Alexander Aring | 7443bc9 | 2021-05-21 15:08:36 -0400 | [diff] [blame] | 1590 | WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags)); |
| 1591 | |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1592 | clear_bit(CF_WRITE_PENDING, &con->flags); |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1593 | |
Alexander Aring | 489d8e5 | 2021-05-21 15:08:46 -0400 | [diff] [blame] | 1594 | if (test_and_clear_bit(CF_RECONNECT, &con->flags)) { |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1595 | close_connection(con, false, false, true); |
Alexander Aring | 489d8e5 | 2021-05-21 15:08:46 -0400 | [diff] [blame] | 1596 | dlm_midcomms_unack_msg_resend(con->nodeid); |
| 1597 | } |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1598 | |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1599 | if (con->sock == NULL) { |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1600 | if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags)) |
| 1601 | msleep(1000); |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1602 | |
| 1603 | mutex_lock(&con->sock_mutex); |
| 1604 | dlm_connect(con); |
| 1605 | mutex_unlock(&con->sock_mutex); |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1606 | } |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1607 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1608 | if (!list_empty(&con->writequeue)) |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1609 | send_to_sock(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1612 | static void work_stop(void) |
| 1613 | { |
Alexander Aring | fcef0e6 | 2021-06-02 09:45:15 -0400 | [diff] [blame] | 1614 | if (recv_workqueue) { |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1615 | destroy_workqueue(recv_workqueue); |
Alexander Aring | fcef0e6 | 2021-06-02 09:45:15 -0400 | [diff] [blame] | 1616 | recv_workqueue = NULL; |
| 1617 | } |
| 1618 | |
| 1619 | if (send_workqueue) { |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1620 | destroy_workqueue(send_workqueue); |
Alexander Aring | fcef0e6 | 2021-06-02 09:45:15 -0400 | [diff] [blame] | 1621 | send_workqueue = NULL; |
| 1622 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | static int work_start(void) |
| 1626 | { |
Alexander Aring | 6c6a1cc | 2021-06-02 09:45:17 -0400 | [diff] [blame] | 1627 | recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1628 | if (!recv_workqueue) { |
| 1629 | log_print("can't start dlm_recv"); |
| 1630 | return -ENOMEM; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1631 | } |
| 1632 | |
Alexander Aring | 6c6a1cc | 2021-06-02 09:45:17 -0400 | [diff] [blame] | 1633 | send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1634 | if (!send_workqueue) { |
| 1635 | log_print("can't start dlm_send"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1636 | destroy_workqueue(recv_workqueue); |
Alexander Aring | fcef0e6 | 2021-06-02 09:45:15 -0400 | [diff] [blame] | 1637 | recv_workqueue = NULL; |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1638 | return -ENOMEM; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1644 | static void shutdown_conn(struct connection *con) |
| 1645 | { |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1646 | if (dlm_proto_ops->shutdown_action) |
| 1647 | dlm_proto_ops->shutdown_action(con); |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | void dlm_lowcomms_shutdown(void) |
| 1651 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1652 | int idx; |
| 1653 | |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1654 | /* Set all the flags to prevent any |
| 1655 | * socket activity. |
| 1656 | */ |
| 1657 | dlm_allow_conn = 0; |
| 1658 | |
| 1659 | if (recv_workqueue) |
| 1660 | flush_workqueue(recv_workqueue); |
| 1661 | if (send_workqueue) |
| 1662 | flush_workqueue(send_workqueue); |
| 1663 | |
| 1664 | dlm_close_sock(&listen_con.sock); |
| 1665 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1666 | idx = srcu_read_lock(&connections_srcu); |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1667 | foreach_conn(shutdown_conn); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1668 | srcu_read_unlock(&connections_srcu, idx); |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1669 | } |
| 1670 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1671 | static void _stop_conn(struct connection *con, bool and_other) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1672 | { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1673 | mutex_lock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | 173a31f | 2017-09-12 09:01:24 +0000 | [diff] [blame] | 1674 | set_bit(CF_CLOSE, &con->flags); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1675 | set_bit(CF_READ_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1676 | set_bit(CF_WRITE_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1677 | if (con->sock && con->sock->sk) { |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 1678 | lock_sock(con->sock->sk); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1679 | con->sock->sk->sk_user_data = NULL; |
Alexander Aring | 92c4460 | 2021-11-15 08:57:06 -0500 | [diff] [blame] | 1680 | release_sock(con->sock->sk); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1681 | } |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1682 | if (con->othercon && and_other) |
| 1683 | _stop_conn(con->othercon, false); |
| 1684 | mutex_unlock(&con->sock_mutex); |
| 1685 | } |
| 1686 | |
| 1687 | static void stop_conn(struct connection *con) |
| 1688 | { |
| 1689 | _stop_conn(con, true); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1690 | } |
| 1691 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1692 | static void connection_release(struct rcu_head *rcu) |
| 1693 | { |
| 1694 | struct connection *con = container_of(rcu, struct connection, rcu); |
| 1695 | |
| 1696 | kfree(con->rx_buf); |
| 1697 | kfree(con); |
| 1698 | } |
| 1699 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1700 | static void free_conn(struct connection *con) |
| 1701 | { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 1702 | close_connection(con, true, true, true); |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1703 | spin_lock(&connections_lock); |
| 1704 | hlist_del_rcu(&con->list); |
| 1705 | spin_unlock(&connections_lock); |
Alexander Aring | 948c47e | 2020-08-27 15:02:53 -0400 | [diff] [blame] | 1706 | if (con->othercon) { |
| 1707 | clean_one_writequeue(con->othercon); |
Alexander Aring | 5cbec20 | 2020-11-02 20:04:16 -0500 | [diff] [blame] | 1708 | call_srcu(&connections_srcu, &con->othercon->rcu, |
| 1709 | connection_release); |
Alexander Aring | 948c47e | 2020-08-27 15:02:53 -0400 | [diff] [blame] | 1710 | } |
Alexander Aring | 0de9843 | 2020-08-27 15:02:52 -0400 | [diff] [blame] | 1711 | clean_one_writequeue(con); |
Alexander Aring | 5cbec20 | 2020-11-02 20:04:16 -0500 | [diff] [blame] | 1712 | call_srcu(&connections_srcu, &con->rcu, connection_release); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1713 | } |
| 1714 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1715 | static void work_flush(void) |
| 1716 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1717 | int ok; |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1718 | int i; |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1719 | struct connection *con; |
| 1720 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1721 | do { |
| 1722 | ok = 1; |
| 1723 | foreach_conn(stop_conn); |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1724 | if (recv_workqueue) |
| 1725 | flush_workqueue(recv_workqueue); |
| 1726 | if (send_workqueue) |
| 1727 | flush_workqueue(send_workqueue); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1728 | for (i = 0; i < CONN_HASH_SIZE && ok; i++) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1729 | hlist_for_each_entry_rcu(con, &connection_hash[i], |
| 1730 | list) { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1731 | ok &= test_bit(CF_READ_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1732 | ok &= test_bit(CF_WRITE_PENDING, &con->flags); |
| 1733 | if (con->othercon) { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1734 | ok &= test_bit(CF_READ_PENDING, |
| 1735 | &con->othercon->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1736 | ok &= test_bit(CF_WRITE_PENDING, |
| 1737 | &con->othercon->flags); |
| 1738 | } |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | } while (!ok); |
| 1742 | } |
| 1743 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1744 | void dlm_lowcomms_stop(void) |
| 1745 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1746 | int idx; |
| 1747 | |
| 1748 | idx = srcu_read_lock(&connections_srcu); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1749 | work_flush(); |
Marcelo Ricardo Leitner | 3a8db79 | 2016-10-08 10:14:37 -0300 | [diff] [blame] | 1750 | foreach_conn(free_conn); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1751 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1752 | work_stop(); |
Alexander Aring | 043697f | 2020-08-27 15:02:50 -0400 | [diff] [blame] | 1753 | deinit_local(); |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1754 | |
| 1755 | dlm_proto_ops = NULL; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1756 | } |
| 1757 | |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1758 | static int dlm_listen_for_all(void) |
| 1759 | { |
| 1760 | struct socket *sock; |
| 1761 | int result; |
| 1762 | |
| 1763 | log_print("Using %s for communications", |
| 1764 | dlm_proto_ops->name); |
| 1765 | |
Alexander Aring | 90d21fc | 2021-07-16 16:22:42 -0400 | [diff] [blame] | 1766 | result = dlm_proto_ops->listen_validate(); |
| 1767 | if (result < 0) |
| 1768 | return result; |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1769 | |
| 1770 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1771 | SOCK_STREAM, dlm_proto_ops->proto, &sock); |
| 1772 | if (result < 0) { |
Alexander Aring | fe93367 | 2021-11-02 15:17:10 -0400 | [diff] [blame] | 1773 | log_print("Can't create comms socket: %d", result); |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1774 | goto out; |
| 1775 | } |
| 1776 | |
| 1777 | sock_set_mark(sock->sk, dlm_config.ci_mark); |
| 1778 | dlm_proto_ops->listen_sockopts(sock); |
| 1779 | |
| 1780 | result = dlm_proto_ops->listen_bind(sock); |
| 1781 | if (result < 0) |
| 1782 | goto out; |
| 1783 | |
| 1784 | save_listen_callbacks(sock); |
| 1785 | add_listen_sock(sock, &listen_con); |
| 1786 | |
| 1787 | INIT_WORK(&listen_con.rwork, process_listen_recv_socket); |
| 1788 | result = sock->ops->listen(sock, 5); |
| 1789 | if (result < 0) { |
| 1790 | dlm_close_sock(&listen_con.sock); |
| 1791 | goto out; |
| 1792 | } |
| 1793 | |
| 1794 | return 0; |
| 1795 | |
| 1796 | out: |
| 1797 | sock_release(sock); |
| 1798 | return result; |
| 1799 | } |
| 1800 | |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1801 | static int dlm_tcp_bind(struct socket *sock) |
| 1802 | { |
| 1803 | struct sockaddr_storage src_addr; |
| 1804 | int result, addr_len; |
| 1805 | |
| 1806 | /* Bind to our cluster-known address connecting to avoid |
| 1807 | * routing problems. |
| 1808 | */ |
| 1809 | memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr)); |
| 1810 | make_sockaddr(&src_addr, 0, &addr_len); |
| 1811 | |
| 1812 | result = sock->ops->bind(sock, (struct sockaddr *)&src_addr, |
| 1813 | addr_len); |
| 1814 | if (result < 0) { |
| 1815 | /* This *may* not indicate a critical error */ |
| 1816 | log_print("could not bind for connect: %d", result); |
| 1817 | } |
| 1818 | |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | static int dlm_tcp_connect(struct connection *con, struct socket *sock, |
| 1823 | struct sockaddr *addr, int addr_len) |
| 1824 | { |
| 1825 | int ret; |
| 1826 | |
| 1827 | ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK); |
| 1828 | switch (ret) { |
| 1829 | case -EINPROGRESS: |
| 1830 | fallthrough; |
| 1831 | case 0: |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
| 1835 | return ret; |
| 1836 | } |
| 1837 | |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1838 | static int dlm_tcp_listen_validate(void) |
| 1839 | { |
| 1840 | /* We don't support multi-homed hosts */ |
| 1841 | if (dlm_local_count > 1) { |
| 1842 | log_print("TCP protocol can't handle multi-homed hosts, try SCTP"); |
| 1843 | return -EINVAL; |
| 1844 | } |
| 1845 | |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
| 1849 | static void dlm_tcp_sockopts(struct socket *sock) |
| 1850 | { |
| 1851 | /* Turn off Nagle's algorithm */ |
| 1852 | tcp_sock_set_nodelay(sock->sk); |
| 1853 | } |
| 1854 | |
| 1855 | static void dlm_tcp_listen_sockopts(struct socket *sock) |
| 1856 | { |
| 1857 | dlm_tcp_sockopts(sock); |
| 1858 | sock_set_reuseaddr(sock->sk); |
| 1859 | } |
| 1860 | |
| 1861 | static int dlm_tcp_listen_bind(struct socket *sock) |
| 1862 | { |
| 1863 | int addr_len; |
| 1864 | |
| 1865 | /* Bind to our port */ |
| 1866 | make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len); |
| 1867 | return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0], |
| 1868 | addr_len); |
| 1869 | } |
| 1870 | |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1871 | static const struct dlm_proto_ops dlm_tcp_ops = { |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1872 | .name = "TCP", |
| 1873 | .proto = IPPROTO_TCP, |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1874 | .connect = dlm_tcp_connect, |
| 1875 | .sockopts = dlm_tcp_sockopts, |
| 1876 | .bind = dlm_tcp_bind, |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1877 | .listen_validate = dlm_tcp_listen_validate, |
| 1878 | .listen_sockopts = dlm_tcp_listen_sockopts, |
| 1879 | .listen_bind = dlm_tcp_listen_bind, |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1880 | .shutdown_action = dlm_tcp_shutdown, |
| 1881 | .eof_condition = tcp_eof_condition, |
| 1882 | }; |
| 1883 | |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1884 | static int dlm_sctp_bind(struct socket *sock) |
| 1885 | { |
| 1886 | return sctp_bind_addrs(sock, 0); |
| 1887 | } |
| 1888 | |
| 1889 | static int dlm_sctp_connect(struct connection *con, struct socket *sock, |
| 1890 | struct sockaddr *addr, int addr_len) |
| 1891 | { |
| 1892 | int ret; |
| 1893 | |
| 1894 | /* |
| 1895 | * Make sock->ops->connect() function return in specified time, |
| 1896 | * since O_NONBLOCK argument in connect() function does not work here, |
| 1897 | * then, we should restore the default value of this attribute. |
| 1898 | */ |
| 1899 | sock_set_sndtimeo(sock->sk, 5); |
| 1900 | ret = sock->ops->connect(sock, addr, addr_len, 0); |
| 1901 | sock_set_sndtimeo(sock->sk, 0); |
| 1902 | if (ret < 0) |
| 1903 | return ret; |
| 1904 | |
| 1905 | if (!test_and_set_bit(CF_CONNECTED, &con->flags)) |
| 1906 | log_print("successful connected to node %d", con->nodeid); |
| 1907 | |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
Alexander Aring | 90d21fc | 2021-07-16 16:22:42 -0400 | [diff] [blame] | 1911 | static int dlm_sctp_listen_validate(void) |
| 1912 | { |
| 1913 | if (!IS_ENABLED(CONFIG_IP_SCTP)) { |
| 1914 | log_print("SCTP is not enabled by this kernel"); |
| 1915 | return -EOPNOTSUPP; |
| 1916 | } |
| 1917 | |
| 1918 | request_module("sctp"); |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1922 | static int dlm_sctp_bind_listen(struct socket *sock) |
| 1923 | { |
| 1924 | return sctp_bind_addrs(sock, dlm_config.ci_tcp_port); |
| 1925 | } |
| 1926 | |
| 1927 | static void dlm_sctp_sockopts(struct socket *sock) |
| 1928 | { |
| 1929 | /* Turn off Nagle's algorithm */ |
| 1930 | sctp_sock_set_nodelay(sock->sk); |
| 1931 | sock_set_rcvbuf(sock->sk, NEEDED_RMEM); |
| 1932 | } |
| 1933 | |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1934 | static const struct dlm_proto_ops dlm_sctp_ops = { |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1935 | .name = "SCTP", |
| 1936 | .proto = IPPROTO_SCTP, |
Alexander Aring | 8728a45 | 2021-07-16 16:22:43 -0400 | [diff] [blame] | 1937 | .try_new_addr = true, |
| 1938 | .connect = dlm_sctp_connect, |
| 1939 | .sockopts = dlm_sctp_sockopts, |
| 1940 | .bind = dlm_sctp_bind, |
Alexander Aring | 90d21fc | 2021-07-16 16:22:42 -0400 | [diff] [blame] | 1941 | .listen_validate = dlm_sctp_listen_validate, |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1942 | .listen_sockopts = dlm_sctp_sockopts, |
| 1943 | .listen_bind = dlm_sctp_bind_listen, |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1944 | }; |
| 1945 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1946 | int dlm_lowcomms_start(void) |
| 1947 | { |
| 1948 | int error = -EINVAL; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1949 | int i; |
| 1950 | |
| 1951 | for (i = 0; i < CONN_HASH_SIZE; i++) |
| 1952 | INIT_HLIST_HEAD(&connection_hash[i]); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1953 | |
| 1954 | init_local(); |
| 1955 | if (!dlm_local_count) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1956 | error = -ENOTCONN; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1957 | log_print("no local IP address has been set"); |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1958 | goto fail; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1961 | INIT_WORK(&listen_con.rwork, process_listen_recv_socket); |
| 1962 | |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1963 | error = work_start(); |
| 1964 | if (error) |
Alexander Aring | fcef0e6 | 2021-06-02 09:45:15 -0400 | [diff] [blame] | 1965 | goto fail_local; |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1966 | |
| 1967 | dlm_allow_conn = 1; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1968 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1969 | /* Start listening */ |
Alexander Aring | ac7d5d0 | 2021-06-02 09:45:19 -0400 | [diff] [blame] | 1970 | switch (dlm_config.ci_protocol) { |
| 1971 | case DLM_PROTO_TCP: |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1972 | dlm_proto_ops = &dlm_tcp_ops; |
Alexander Aring | ac7d5d0 | 2021-06-02 09:45:19 -0400 | [diff] [blame] | 1973 | break; |
| 1974 | case DLM_PROTO_SCTP: |
Alexander Aring | a66c008 | 2021-07-16 16:22:40 -0400 | [diff] [blame] | 1975 | dlm_proto_ops = &dlm_sctp_ops; |
Alexander Aring | ac7d5d0 | 2021-06-02 09:45:19 -0400 | [diff] [blame] | 1976 | break; |
| 1977 | default: |
| 1978 | log_print("Invalid protocol identifier %d set", |
| 1979 | dlm_config.ci_protocol); |
| 1980 | error = -EINVAL; |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1981 | goto fail_proto_ops; |
Alexander Aring | ac7d5d0 | 2021-06-02 09:45:19 -0400 | [diff] [blame] | 1982 | } |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1983 | |
| 1984 | error = dlm_listen_for_all(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1985 | if (error) |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1986 | goto fail_listen; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1987 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1988 | return 0; |
| 1989 | |
Alexander Aring | 2dc6b11 | 2021-07-16 16:22:41 -0400 | [diff] [blame] | 1990 | fail_listen: |
| 1991 | dlm_proto_ops = NULL; |
| 1992 | fail_proto_ops: |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1993 | dlm_allow_conn = 0; |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1994 | dlm_close_sock(&listen_con.sock); |
Alexander Aring | fcef0e6 | 2021-06-02 09:45:15 -0400 | [diff] [blame] | 1995 | work_stop(); |
| 1996 | fail_local: |
| 1997 | deinit_local(); |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1998 | fail: |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1999 | return error; |
| 2000 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 2001 | |
| 2002 | void dlm_lowcomms_exit(void) |
| 2003 | { |
| 2004 | struct dlm_node_addr *na, *safe; |
| 2005 | |
| 2006 | spin_lock(&dlm_node_addrs_spin); |
| 2007 | list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) { |
| 2008 | list_del(&na->list); |
| 2009 | while (na->addr_count--) |
| 2010 | kfree(na->addr[na->addr_count]); |
| 2011 | kfree(na); |
| 2012 | } |
| 2013 | spin_unlock(&dlm_node_addrs_spin); |
| 2014 | } |