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