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) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 62 | #define CONN_HASH_SIZE 32 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 63 | |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 64 | /* Number of messages to send before rescheduling */ |
| 65 | #define MAX_SEND_MSG_COUNT 25 |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 66 | #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000) |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 67 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 68 | struct connection { |
| 69 | struct socket *sock; /* NULL if not connected */ |
| 70 | uint32_t nodeid; /* So we know who we are in the list */ |
| 71 | struct mutex sock_mutex; |
| 72 | unsigned long flags; |
| 73 | #define CF_READ_PENDING 1 |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 74 | #define CF_WRITE_PENDING 2 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 75 | #define CF_INIT_PENDING 4 |
| 76 | #define CF_IS_OTHERCON 5 |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 77 | #define CF_CLOSE 6 |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 78 | #define CF_APP_LIMITED 7 |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 79 | #define CF_CLOSING 8 |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 80 | #define CF_SHUTDOWN 9 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 81 | struct list_head writequeue; /* List of outgoing writequeue_entries */ |
| 82 | spinlock_t writequeue_lock; |
| 83 | int (*rx_action) (struct connection *); /* What to do when active */ |
| 84 | void (*connect_action) (struct connection *); /* What to do to connect */ |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 85 | void (*shutdown_action)(struct connection *con); /* What to do to shutdown */ |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 86 | int retries; |
| 87 | #define MAX_CONNECT_RETRIES 3 |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 88 | struct hlist_node list; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 89 | struct connection *othercon; |
| 90 | struct work_struct rwork; /* Receive workqueue */ |
| 91 | struct work_struct swork; /* Send workqueue */ |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 92 | wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */ |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 93 | unsigned char *rx_buf; |
| 94 | int rx_buflen; |
| 95 | int rx_leftover; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 96 | struct rcu_head rcu; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 97 | }; |
| 98 | #define sock2con(x) ((struct connection *)(x)->sk_user_data) |
| 99 | |
| 100 | /* An entry waiting to be sent */ |
| 101 | struct writequeue_entry { |
| 102 | struct list_head list; |
| 103 | struct page *page; |
| 104 | int offset; |
| 105 | int len; |
| 106 | int end; |
| 107 | int users; |
| 108 | struct connection *con; |
| 109 | }; |
| 110 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 111 | struct dlm_node_addr { |
| 112 | struct list_head list; |
| 113 | int nodeid; |
| 114 | int addr_count; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 115 | int curr_addr_index; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 116 | struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT]; |
| 117 | }; |
| 118 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 119 | static struct listen_sock_callbacks { |
| 120 | void (*sk_error_report)(struct sock *); |
| 121 | void (*sk_data_ready)(struct sock *); |
| 122 | void (*sk_state_change)(struct sock *); |
| 123 | void (*sk_write_space)(struct sock *); |
| 124 | } listen_sock; |
| 125 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 126 | static LIST_HEAD(dlm_node_addrs); |
| 127 | static DEFINE_SPINLOCK(dlm_node_addrs_spin); |
| 128 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 129 | static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT]; |
| 130 | static int dlm_local_count; |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 131 | static int dlm_allow_conn; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 132 | |
| 133 | /* Work queues */ |
| 134 | static struct workqueue_struct *recv_workqueue; |
| 135 | static struct workqueue_struct *send_workqueue; |
| 136 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 137 | static struct hlist_head connection_hash[CONN_HASH_SIZE]; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 138 | static DEFINE_SPINLOCK(connections_lock); |
| 139 | DEFINE_STATIC_SRCU(connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 140 | |
| 141 | static void process_recv_sockets(struct work_struct *work); |
| 142 | static void process_send_sockets(struct work_struct *work); |
| 143 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 144 | |
| 145 | /* This is deliberately very simple because most clusters have simple |
| 146 | sequential nodeids, so we should be able to go straight to a connection |
| 147 | struct in the array */ |
| 148 | static inline int nodeid_hash(int nodeid) |
| 149 | { |
| 150 | return nodeid & (CONN_HASH_SIZE-1); |
| 151 | } |
| 152 | |
| 153 | static struct connection *__find_con(int nodeid) |
| 154 | { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 155 | int r, idx; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 156 | struct connection *con; |
| 157 | |
| 158 | r = nodeid_hash(nodeid); |
| 159 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 160 | idx = srcu_read_lock(&connections_srcu); |
| 161 | hlist_for_each_entry_rcu(con, &connection_hash[r], list) { |
| 162 | if (con->nodeid == nodeid) { |
| 163 | srcu_read_unlock(&connections_srcu, idx); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 164 | return con; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 165 | } |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 166 | } |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 167 | srcu_read_unlock(&connections_srcu, idx); |
| 168 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
| 171 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 172 | /* |
| 173 | * If 'allocation' is zero then we don't attempt to create a new |
| 174 | * connection structure for this node. |
| 175 | */ |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 176 | static struct connection *nodeid2con(int nodeid, gfp_t alloc) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 177 | { |
Alexander Aring | 4f2b30f | 2020-09-30 18:37:29 -0400 | [diff] [blame] | 178 | struct connection *con, *tmp; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 179 | int r; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 180 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 181 | con = __find_con(nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 182 | if (con || !alloc) |
| 183 | return con; |
| 184 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 185 | con = kzalloc(sizeof(*con), alloc); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 186 | if (!con) |
| 187 | return NULL; |
| 188 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 189 | con->rx_buflen = dlm_config.ci_buffer_size; |
| 190 | con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS); |
| 191 | if (!con->rx_buf) { |
| 192 | kfree(con); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 196 | con->nodeid = nodeid; |
| 197 | mutex_init(&con->sock_mutex); |
| 198 | INIT_LIST_HEAD(&con->writequeue); |
| 199 | spin_lock_init(&con->writequeue_lock); |
| 200 | INIT_WORK(&con->swork, process_send_sockets); |
| 201 | INIT_WORK(&con->rwork, process_recv_sockets); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 202 | init_waitqueue_head(&con->shutdown_wait); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 203 | |
| 204 | /* Setup action pointers for child sockets */ |
| 205 | if (con->nodeid) { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 206 | struct connection *zerocon = __find_con(0); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 207 | |
| 208 | con->connect_action = zerocon->connect_action; |
| 209 | if (!con->rx_action) |
| 210 | con->rx_action = zerocon->rx_action; |
| 211 | } |
| 212 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 213 | r = nodeid_hash(nodeid); |
| 214 | |
| 215 | spin_lock(&connections_lock); |
Alexander Aring | 4f2b30f | 2020-09-30 18:37:29 -0400 | [diff] [blame] | 216 | /* Because multiple workqueues/threads calls this function it can |
| 217 | * race on multiple cpu's. Instead of locking hot path __find_con() |
| 218 | * we just check in rare cases of recently added nodes again |
| 219 | * under protection of connections_lock. If this is the case we |
| 220 | * abort our connection creation and return the existing connection. |
| 221 | */ |
| 222 | tmp = __find_con(nodeid); |
| 223 | if (tmp) { |
| 224 | spin_unlock(&connections_lock); |
| 225 | kfree(con->rx_buf); |
| 226 | kfree(con); |
| 227 | return tmp; |
| 228 | } |
| 229 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 230 | hlist_add_head_rcu(&con->list, &connection_hash[r]); |
| 231 | spin_unlock(&connections_lock); |
| 232 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 233 | return con; |
| 234 | } |
| 235 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 236 | /* Loop round all connections */ |
| 237 | static void foreach_conn(void (*conn_func)(struct connection *c)) |
| 238 | { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 239 | int i, idx; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 240 | struct connection *con; |
| 241 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 242 | idx = srcu_read_lock(&connections_srcu); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 243 | for (i = 0; i < CONN_HASH_SIZE; i++) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 244 | hlist_for_each_entry_rcu(con, &connection_hash[i], list) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 245 | conn_func(con); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 246 | } |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 247 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 248 | } |
| 249 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 250 | static struct dlm_node_addr *find_node_addr(int nodeid) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 251 | { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 252 | struct dlm_node_addr *na; |
| 253 | |
| 254 | list_for_each_entry(na, &dlm_node_addrs, list) { |
| 255 | if (na->nodeid == nodeid) |
| 256 | return na; |
| 257 | } |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y) |
| 262 | { |
| 263 | switch (x->ss_family) { |
| 264 | case AF_INET: { |
| 265 | struct sockaddr_in *sinx = (struct sockaddr_in *)x; |
| 266 | struct sockaddr_in *siny = (struct sockaddr_in *)y; |
| 267 | if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr) |
| 268 | return 0; |
| 269 | if (sinx->sin_port != siny->sin_port) |
| 270 | return 0; |
| 271 | break; |
| 272 | } |
| 273 | case AF_INET6: { |
| 274 | struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x; |
| 275 | struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y; |
| 276 | if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr)) |
| 277 | return 0; |
| 278 | if (sinx->sin6_port != siny->sin6_port) |
| 279 | return 0; |
| 280 | break; |
| 281 | } |
| 282 | default: |
| 283 | return 0; |
| 284 | } |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out, |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 289 | struct sockaddr *sa_out, bool try_new_addr) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 290 | { |
| 291 | struct sockaddr_storage sas; |
| 292 | struct dlm_node_addr *na; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 293 | |
| 294 | if (!dlm_local_count) |
| 295 | return -1; |
| 296 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 297 | spin_lock(&dlm_node_addrs_spin); |
| 298 | na = find_node_addr(nodeid); |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 299 | if (na && na->addr_count) { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 300 | memcpy(&sas, na->addr[na->curr_addr_index], |
| 301 | sizeof(struct sockaddr_storage)); |
| 302 | |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 303 | if (try_new_addr) { |
| 304 | na->curr_addr_index++; |
| 305 | if (na->curr_addr_index == na->addr_count) |
| 306 | na->curr_addr_index = 0; |
| 307 | } |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 308 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 309 | spin_unlock(&dlm_node_addrs_spin); |
| 310 | |
| 311 | if (!na) |
| 312 | return -EEXIST; |
| 313 | |
| 314 | if (!na->addr_count) |
| 315 | return -ENOENT; |
| 316 | |
| 317 | if (sas_out) |
| 318 | memcpy(sas_out, &sas, sizeof(struct sockaddr_storage)); |
| 319 | |
| 320 | if (!sa_out) |
| 321 | return 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 322 | |
| 323 | if (dlm_local_addr[0]->ss_family == AF_INET) { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 324 | struct sockaddr_in *in4 = (struct sockaddr_in *) &sas; |
| 325 | struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 326 | ret4->sin_addr.s_addr = in4->sin_addr.s_addr; |
| 327 | } else { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 328 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas; |
| 329 | struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 330 | ret6->sin6_addr = in6->sin6_addr; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 336 | static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid) |
| 337 | { |
| 338 | struct dlm_node_addr *na; |
| 339 | int rv = -EEXIST; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 340 | int addr_i; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 341 | |
| 342 | spin_lock(&dlm_node_addrs_spin); |
| 343 | list_for_each_entry(na, &dlm_node_addrs, list) { |
| 344 | if (!na->addr_count) |
| 345 | continue; |
| 346 | |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 347 | for (addr_i = 0; addr_i < na->addr_count; addr_i++) { |
| 348 | if (addr_compare(na->addr[addr_i], addr)) { |
| 349 | *nodeid = na->nodeid; |
| 350 | rv = 0; |
| 351 | goto unlock; |
| 352 | } |
| 353 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 354 | } |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 355 | unlock: |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 356 | spin_unlock(&dlm_node_addrs_spin); |
| 357 | return rv; |
| 358 | } |
| 359 | |
| 360 | int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len) |
| 361 | { |
| 362 | struct sockaddr_storage *new_addr; |
| 363 | struct dlm_node_addr *new_node, *na; |
| 364 | |
| 365 | new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS); |
| 366 | if (!new_node) |
| 367 | return -ENOMEM; |
| 368 | |
| 369 | new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS); |
| 370 | if (!new_addr) { |
| 371 | kfree(new_node); |
| 372 | return -ENOMEM; |
| 373 | } |
| 374 | |
| 375 | memcpy(new_addr, addr, len); |
| 376 | |
| 377 | spin_lock(&dlm_node_addrs_spin); |
| 378 | na = find_node_addr(nodeid); |
| 379 | if (!na) { |
| 380 | new_node->nodeid = nodeid; |
| 381 | new_node->addr[0] = new_addr; |
| 382 | new_node->addr_count = 1; |
| 383 | list_add(&new_node->list, &dlm_node_addrs); |
| 384 | spin_unlock(&dlm_node_addrs_spin); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | if (na->addr_count >= DLM_MAX_ADDR_COUNT) { |
| 389 | spin_unlock(&dlm_node_addrs_spin); |
| 390 | kfree(new_addr); |
| 391 | kfree(new_node); |
| 392 | return -ENOSPC; |
| 393 | } |
| 394 | |
| 395 | na->addr[na->addr_count++] = new_addr; |
| 396 | spin_unlock(&dlm_node_addrs_spin); |
| 397 | kfree(new_node); |
| 398 | return 0; |
| 399 | } |
| 400 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 401 | /* Data available on socket or listen socket received a connect */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 402 | static void lowcomms_data_ready(struct sock *sk) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 403 | { |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 404 | struct connection *con; |
| 405 | |
| 406 | read_lock_bh(&sk->sk_callback_lock); |
| 407 | con = sock2con(sk); |
Patrick Caulfield | afb853f | 2007-06-01 10:07:26 -0500 | [diff] [blame] | 408 | if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 409 | queue_work(recv_workqueue, &con->rwork); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 410 | read_unlock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void lowcomms_write_space(struct sock *sk) |
| 414 | { |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 415 | struct connection *con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 416 | |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 417 | read_lock_bh(&sk->sk_callback_lock); |
| 418 | con = sock2con(sk); |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 419 | if (!con) |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 420 | goto out; |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 421 | |
| 422 | clear_bit(SOCK_NOSPACE, &con->sock->flags); |
| 423 | |
| 424 | if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) { |
| 425 | con->sock->sk->sk_write_pending--; |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 426 | clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags); |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 429 | queue_work(send_workqueue, &con->swork); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 430 | out: |
| 431 | read_unlock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static inline void lowcomms_connect_sock(struct connection *con) |
| 435 | { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 436 | if (test_bit(CF_CLOSE, &con->flags)) |
| 437 | return; |
Bob Peterson | 61d9102b | 2017-09-12 08:55:04 +0000 | [diff] [blame] | 438 | queue_work(send_workqueue, &con->swork); |
| 439 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void lowcomms_state_change(struct sock *sk) |
| 443 | { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 444 | /* SCTP layer is not calling sk_data_ready when the connection |
| 445 | * is done, so we catch the signal through here. Also, it |
| 446 | * doesn't switch socket state when entering shutdown, so we |
| 447 | * skip the write in that case. |
| 448 | */ |
| 449 | if (sk->sk_shutdown) { |
| 450 | if (sk->sk_shutdown == RCV_SHUTDOWN) |
| 451 | lowcomms_data_ready(sk); |
| 452 | } else if (sk->sk_state == TCP_ESTABLISHED) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 453 | lowcomms_write_space(sk); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 454 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 457 | int dlm_lowcomms_connect_node(int nodeid) |
| 458 | { |
| 459 | struct connection *con; |
| 460 | |
| 461 | if (nodeid == dlm_our_nodeid()) |
| 462 | return 0; |
| 463 | |
| 464 | con = nodeid2con(nodeid, GFP_NOFS); |
| 465 | if (!con) |
| 466 | return -ENOMEM; |
| 467 | lowcomms_connect_sock(con); |
| 468 | return 0; |
| 469 | } |
| 470 | |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 471 | static void lowcomms_error_report(struct sock *sk) |
| 472 | { |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 473 | struct connection *con; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 474 | void (*orig_report)(struct sock *) = NULL; |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 475 | struct inet_sock *inet; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 476 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 477 | read_lock_bh(&sk->sk_callback_lock); |
| 478 | con = sock2con(sk); |
| 479 | if (con == NULL) |
| 480 | goto out; |
| 481 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 482 | orig_report = listen_sock.sk_error_report; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 483 | |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 484 | inet = inet_sk(sk); |
| 485 | switch (sk->sk_family) { |
| 486 | case AF_INET: |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 487 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 488 | "sending to node %d at %pI4, dport %d, " |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 489 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 490 | con->nodeid, &inet->inet_daddr, |
| 491 | ntohs(inet->inet_dport), sk->sk_err, |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 492 | sk->sk_err_soft); |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 493 | break; |
Alexander Aring | 43220a6 | 2021-11-17 09:20:43 -0500 | [diff] [blame] | 494 | #if IS_ENABLED(CONFIG_IPV6) |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 495 | case AF_INET6: |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 496 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 497 | "sending to node %d at %pI6c, " |
| 498 | "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(), |
| 499 | con->nodeid, &sk->sk_v6_daddr, |
| 500 | ntohs(inet->inet_dport), sk->sk_err, |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 501 | sk->sk_err_soft); |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 502 | break; |
Alexander Aring | 43220a6 | 2021-11-17 09:20:43 -0500 | [diff] [blame] | 503 | #endif |
Alexander Aring | 55917db | 2021-11-15 08:57:05 -0500 | [diff] [blame] | 504 | default: |
| 505 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
| 506 | "invalid socket family %d set, " |
| 507 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
| 508 | sk->sk_family, sk->sk_err, sk->sk_err_soft); |
| 509 | goto out; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 510 | } |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 511 | out: |
| 512 | read_unlock_bh(&sk->sk_callback_lock); |
| 513 | if (orig_report) |
| 514 | orig_report(sk); |
| 515 | } |
| 516 | |
| 517 | /* Note: sk_callback_lock must be locked before calling this function. */ |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 518 | static void save_listen_callbacks(struct socket *sock) |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 519 | { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 520 | struct sock *sk = sock->sk; |
| 521 | |
| 522 | listen_sock.sk_data_ready = sk->sk_data_ready; |
| 523 | listen_sock.sk_state_change = sk->sk_state_change; |
| 524 | listen_sock.sk_write_space = sk->sk_write_space; |
| 525 | listen_sock.sk_error_report = sk->sk_error_report; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 528 | static void restore_callbacks(struct socket *sock) |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 529 | { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 530 | struct sock *sk = sock->sk; |
| 531 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 532 | write_lock_bh(&sk->sk_callback_lock); |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 533 | sk->sk_user_data = NULL; |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 534 | sk->sk_data_ready = listen_sock.sk_data_ready; |
| 535 | sk->sk_state_change = listen_sock.sk_state_change; |
| 536 | sk->sk_write_space = listen_sock.sk_write_space; |
| 537 | sk->sk_error_report = listen_sock.sk_error_report; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 538 | write_unlock_bh(&sk->sk_callback_lock); |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 539 | } |
| 540 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 541 | /* Make a socket active */ |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 542 | static void add_sock(struct socket *sock, struct connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 543 | { |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 544 | struct sock *sk = sock->sk; |
| 545 | |
| 546 | write_lock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 547 | con->sock = sock; |
| 548 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 549 | sk->sk_user_data = con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 550 | /* Install a data_ready callback */ |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 551 | sk->sk_data_ready = lowcomms_data_ready; |
| 552 | sk->sk_write_space = lowcomms_write_space; |
| 553 | sk->sk_state_change = lowcomms_state_change; |
| 554 | sk->sk_allocation = GFP_NOFS; |
| 555 | sk->sk_error_report = lowcomms_error_report; |
| 556 | write_unlock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* Add the port number to an IPv6 or 4 sockaddr and return the address |
| 560 | length */ |
| 561 | static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, |
| 562 | int *addr_len) |
| 563 | { |
| 564 | saddr->ss_family = dlm_local_addr[0]->ss_family; |
| 565 | if (saddr->ss_family == AF_INET) { |
| 566 | struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr; |
| 567 | in4_addr->sin_port = cpu_to_be16(port); |
| 568 | *addr_len = sizeof(struct sockaddr_in); |
| 569 | memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero)); |
| 570 | } else { |
| 571 | struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr; |
| 572 | in6_addr->sin6_port = cpu_to_be16(port); |
| 573 | *addr_len = sizeof(struct sockaddr_in6); |
| 574 | } |
Patrick Caulfield | 01c8cab | 2007-07-17 16:53:15 +0100 | [diff] [blame] | 575 | memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* Close a remote connection and tidy up */ |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 579 | static void close_connection(struct connection *con, bool and_other, |
| 580 | bool tx, bool rx) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 581 | { |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 582 | bool closing = test_and_set_bit(CF_CLOSING, &con->flags); |
| 583 | |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 584 | if (tx && !closing && cancel_work_sync(&con->swork)) { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 585 | log_print("canceled swork for node %d", con->nodeid); |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 586 | clear_bit(CF_WRITE_PENDING, &con->flags); |
| 587 | } |
| 588 | if (rx && !closing && cancel_work_sync(&con->rwork)) { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 589 | log_print("canceled rwork for node %d", con->nodeid); |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 590 | clear_bit(CF_READ_PENDING, &con->flags); |
| 591 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 592 | |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 593 | mutex_lock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 594 | if (con->sock) { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 595 | restore_callbacks(con->sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 596 | sock_release(con->sock); |
| 597 | con->sock = NULL; |
| 598 | } |
| 599 | if (con->othercon && and_other) { |
| 600 | /* Will only re-enter once. */ |
Alexander Aring | 7425fe5 | 2021-05-21 15:08:38 -0400 | [diff] [blame] | 601 | close_connection(con->othercon, false, tx, rx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 602 | } |
Patrick Caulfield | 9e5f282 | 2007-08-02 14:58:14 +0100 | [diff] [blame] | 603 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 604 | con->rx_leftover = 0; |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 605 | con->retries = 0; |
| 606 | mutex_unlock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 607 | clear_bit(CF_CLOSING, &con->flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 610 | static void shutdown_connection(struct connection *con) |
| 611 | { |
| 612 | int ret; |
| 613 | |
Alexander Aring | a407b58 | 2021-03-01 17:05:19 -0500 | [diff] [blame] | 614 | flush_work(&con->swork); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 615 | |
| 616 | mutex_lock(&con->sock_mutex); |
| 617 | /* nothing to shutdown */ |
| 618 | if (!con->sock) { |
| 619 | mutex_unlock(&con->sock_mutex); |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | set_bit(CF_SHUTDOWN, &con->flags); |
| 624 | ret = kernel_sock_shutdown(con->sock, SHUT_WR); |
| 625 | mutex_unlock(&con->sock_mutex); |
| 626 | if (ret) { |
| 627 | log_print("Connection %p failed to shutdown: %d will force close", |
| 628 | con, ret); |
| 629 | goto force_close; |
| 630 | } else { |
| 631 | ret = wait_event_timeout(con->shutdown_wait, |
| 632 | !test_bit(CF_SHUTDOWN, &con->flags), |
| 633 | DLM_SHUTDOWN_WAIT_TIMEOUT); |
| 634 | if (ret == 0) { |
| 635 | log_print("Connection %p shutdown timed out, will force close", |
| 636 | con); |
| 637 | goto force_close; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return; |
| 642 | |
| 643 | force_close: |
| 644 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 645 | close_connection(con, false, true, true); |
| 646 | } |
| 647 | |
| 648 | static void dlm_tcp_shutdown(struct connection *con) |
| 649 | { |
| 650 | if (con->othercon) |
| 651 | shutdown_connection(con->othercon); |
| 652 | shutdown_connection(con); |
| 653 | } |
| 654 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 655 | static int con_realloc_receive_buf(struct connection *con, int newlen) |
| 656 | { |
| 657 | unsigned char *newbuf; |
| 658 | |
| 659 | newbuf = kmalloc(newlen, GFP_NOFS); |
| 660 | if (!newbuf) |
| 661 | return -ENOMEM; |
| 662 | |
| 663 | /* copy any leftover from last receive */ |
| 664 | if (con->rx_leftover) |
| 665 | memmove(newbuf, con->rx_buf, con->rx_leftover); |
| 666 | |
| 667 | /* swap to new buffer space */ |
| 668 | kfree(con->rx_buf); |
| 669 | con->rx_buflen = newlen; |
| 670 | con->rx_buf = newbuf; |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 675 | /* Data received from remote end */ |
| 676 | static int receive_from_sock(struct connection *con) |
| 677 | { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 678 | int call_again_soon = 0; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 679 | struct msghdr msg; |
| 680 | struct kvec iov; |
| 681 | int ret, buflen; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 682 | |
| 683 | mutex_lock(&con->sock_mutex); |
| 684 | |
| 685 | if (con->sock == NULL) { |
| 686 | ret = -EAGAIN; |
| 687 | goto out_close; |
| 688 | } |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 689 | |
Marcelo Ricardo Leitner | acee4e5 | 2015-08-11 19:22:24 -0300 | [diff] [blame] | 690 | if (con->nodeid == 0) { |
| 691 | ret = -EINVAL; |
| 692 | goto out_close; |
| 693 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 694 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 695 | /* realloc if we get new buffer size to read out */ |
| 696 | buflen = dlm_config.ci_buffer_size; |
| 697 | if (con->rx_buflen != buflen && con->rx_leftover <= buflen) { |
| 698 | ret = con_realloc_receive_buf(con, buflen); |
| 699 | if (ret < 0) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 700 | goto out_resched; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 703 | /* calculate new buffer parameter regarding last receive and |
| 704 | * possible leftover bytes |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 705 | */ |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 706 | iov.iov_base = con->rx_buf + con->rx_leftover; |
| 707 | iov.iov_len = con->rx_buflen - con->rx_leftover; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 708 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 709 | memset(&msg, 0, sizeof(msg)); |
| 710 | msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 711 | ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len, |
| 712 | msg.msg_flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 713 | if (ret <= 0) |
| 714 | goto out_close; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 715 | else if (ret == iov.iov_len) |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 716 | call_again_soon = 1; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 717 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 718 | /* new buflen according readed bytes and leftover from last receive */ |
| 719 | buflen = ret + con->rx_leftover; |
| 720 | ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen); |
| 721 | if (ret < 0) |
| 722 | goto out_close; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 723 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 724 | /* calculate leftover bytes from process and put it into begin of |
| 725 | * the receive buffer, so next receive we have the full message |
| 726 | * at the start address of the receive buffer. |
| 727 | */ |
| 728 | con->rx_leftover = buflen - ret; |
| 729 | if (con->rx_leftover) { |
| 730 | memmove(con->rx_buf, con->rx_buf + ret, |
| 731 | con->rx_leftover); |
| 732 | call_again_soon = true; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (call_again_soon) |
| 736 | goto out_resched; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 737 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 738 | mutex_unlock(&con->sock_mutex); |
| 739 | return 0; |
| 740 | |
| 741 | out_resched: |
| 742 | if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) |
| 743 | queue_work(recv_workqueue, &con->rwork); |
| 744 | mutex_unlock(&con->sock_mutex); |
| 745 | return -EAGAIN; |
| 746 | |
| 747 | out_close: |
| 748 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 9e5f282 | 2007-08-02 14:58:14 +0100 | [diff] [blame] | 749 | if (ret != -EAGAIN) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 750 | /* Reconnect when there is something to send */ |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 751 | close_connection(con, false, true, false); |
| 752 | if (ret == 0) { |
| 753 | log_print("connection %p got EOF from %d", |
| 754 | con, con->nodeid); |
| 755 | /* handling for tcp shutdown */ |
| 756 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 757 | wake_up(&con->shutdown_wait); |
| 758 | /* signal to breaking receive worker */ |
| 759 | ret = -1; |
| 760 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 761 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | /* Listening socket is busy, accept a connection */ |
Christoph Hellwig | 0774dc7 | 2020-05-27 20:22:28 +0200 | [diff] [blame] | 766 | static int accept_from_sock(struct connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 767 | { |
| 768 | int result; |
| 769 | struct sockaddr_storage peeraddr; |
| 770 | struct socket *newsock; |
| 771 | int len; |
| 772 | int nodeid; |
| 773 | struct connection *newcon; |
| 774 | struct connection *addcon; |
Alexander Aring | 3f78cd7 | 2020-09-24 10:31:23 -0400 | [diff] [blame] | 775 | unsigned int mark; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 776 | |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 777 | if (!dlm_allow_conn) { |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 778 | return -1; |
| 779 | } |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 780 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 781 | mutex_lock_nested(&con->sock_mutex, 0); |
| 782 | |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 783 | if (!con->sock) { |
| 784 | mutex_unlock(&con->sock_mutex); |
| 785 | return -ENOTCONN; |
| 786 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 787 | |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 788 | result = kernel_accept(con->sock, &newsock, O_NONBLOCK); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 789 | if (result < 0) |
| 790 | goto accept_err; |
| 791 | |
| 792 | /* Get the connected socket's peer */ |
| 793 | memset(&peeraddr, 0, sizeof(peeraddr)); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 794 | len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2); |
| 795 | if (len < 0) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 796 | result = -ECONNABORTED; |
| 797 | goto accept_err; |
| 798 | } |
| 799 | |
| 800 | /* Get the new node's NODEID */ |
| 801 | make_sockaddr(&peeraddr, 0, &len); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 802 | if (addr_to_nodeid(&peeraddr, &nodeid)) { |
Masatake YAMATO | bcaadf5 | 2011-07-04 12:25:51 +0900 | [diff] [blame] | 803 | unsigned char *b=(unsigned char *)&peeraddr; |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 804 | log_print("connect from non cluster node"); |
Masatake YAMATO | bcaadf5 | 2011-07-04 12:25:51 +0900 | [diff] [blame] | 805 | print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE, |
| 806 | b, sizeof(struct sockaddr_storage)); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 807 | sock_release(newsock); |
| 808 | mutex_unlock(&con->sock_mutex); |
| 809 | return -1; |
| 810 | } |
| 811 | |
Alexander Aring | 3f78cd7 | 2020-09-24 10:31:23 -0400 | [diff] [blame] | 812 | dlm_comm_mark(nodeid, &mark); |
| 813 | sock_set_mark(newsock->sk, mark); |
| 814 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 815 | log_print("got connection from %d", nodeid); |
| 816 | |
| 817 | /* Check to see if we already have a connection to this node. This |
| 818 | * could happen if the two nodes initiate a connection at roughly |
| 819 | * the same time and the connections cross on the wire. |
| 820 | * In this case we store the incoming one in "othercon" |
| 821 | */ |
David Teigland | 748285c | 2009-05-15 10:50:57 -0500 | [diff] [blame] | 822 | newcon = nodeid2con(nodeid, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 823 | if (!newcon) { |
| 824 | result = -ENOMEM; |
| 825 | goto accept_err; |
| 826 | } |
| 827 | mutex_lock_nested(&newcon->sock_mutex, 1); |
| 828 | if (newcon->sock) { |
| 829 | struct connection *othercon = newcon->othercon; |
| 830 | |
| 831 | if (!othercon) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 832 | othercon = kzalloc(sizeof(*othercon), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 833 | if (!othercon) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 834 | log_print("failed to allocate incoming socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 835 | mutex_unlock(&newcon->sock_mutex); |
| 836 | result = -ENOMEM; |
| 837 | goto accept_err; |
| 838 | } |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 839 | |
| 840 | othercon->rx_buflen = dlm_config.ci_buffer_size; |
| 841 | othercon->rx_buf = kmalloc(othercon->rx_buflen, GFP_NOFS); |
| 842 | if (!othercon->rx_buf) { |
| 843 | mutex_unlock(&newcon->sock_mutex); |
| 844 | kfree(othercon); |
| 845 | log_print("failed to allocate incoming socket receive buffer"); |
| 846 | result = -ENOMEM; |
| 847 | goto accept_err; |
| 848 | } |
| 849 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 850 | othercon->nodeid = nodeid; |
| 851 | othercon->rx_action = receive_from_sock; |
| 852 | mutex_init(&othercon->sock_mutex); |
tsutomu.owa@toshiba.co.jp | 26b4109 | 2017-09-12 09:02:10 +0000 | [diff] [blame] | 853 | INIT_LIST_HEAD(&othercon->writequeue); |
| 854 | spin_lock_init(&othercon->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 855 | INIT_WORK(&othercon->swork, process_send_sockets); |
| 856 | INIT_WORK(&othercon->rwork, process_recv_sockets); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 857 | init_waitqueue_head(&othercon->shutdown_wait); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 858 | set_bit(CF_IS_OTHERCON, &othercon->flags); |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 859 | } else { |
| 860 | /* close other sock con if we have something new */ |
| 861 | close_connection(othercon, false, true, false); |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 862 | } |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 863 | |
tsutomu.owa@toshiba.co.jp | c735582 | 2017-09-12 08:56:00 +0000 | [diff] [blame] | 864 | mutex_lock_nested(&othercon->sock_mutex, 2); |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 865 | newcon->othercon = othercon; |
| 866 | add_sock(newsock, othercon); |
| 867 | addcon = othercon; |
| 868 | mutex_unlock(&othercon->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 869 | } |
| 870 | else { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 871 | newcon->rx_action = receive_from_sock; |
Bob Peterson | 3735b4b | 2016-09-23 14:23:26 -0400 | [diff] [blame] | 872 | /* accept copies the sk after we've saved the callbacks, so we |
| 873 | don't want to save them a second time or comm errors will |
| 874 | result in calling sk_error_report recursively. */ |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 875 | add_sock(newsock, newcon); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 876 | addcon = newcon; |
| 877 | } |
| 878 | |
| 879 | mutex_unlock(&newcon->sock_mutex); |
| 880 | |
| 881 | /* |
| 882 | * Add it to the active queue in case we got data |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 883 | * between processing the accept adding the socket |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 884 | * to the read_sockets list |
| 885 | */ |
| 886 | if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) |
| 887 | queue_work(recv_workqueue, &addcon->rwork); |
| 888 | mutex_unlock(&con->sock_mutex); |
| 889 | |
| 890 | return 0; |
| 891 | |
| 892 | accept_err: |
| 893 | mutex_unlock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 894 | if (newsock) |
| 895 | sock_release(newsock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 896 | |
| 897 | if (result != -EAGAIN) |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 898 | log_print("error accepting connection from node: %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 899 | return result; |
| 900 | } |
| 901 | |
| 902 | static void free_entry(struct writequeue_entry *e) |
| 903 | { |
| 904 | __free_page(e->page); |
| 905 | kfree(e); |
| 906 | } |
| 907 | |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 908 | /* |
| 909 | * writequeue_entry_complete - try to delete and free write queue entry |
| 910 | * @e: write queue entry to try to delete |
| 911 | * @completed: bytes completed |
| 912 | * |
| 913 | * writequeue_lock must be held. |
| 914 | */ |
| 915 | static void writequeue_entry_complete(struct writequeue_entry *e, int completed) |
| 916 | { |
| 917 | e->offset += completed; |
| 918 | e->len -= completed; |
| 919 | |
| 920 | if (e->len == 0 && e->users == 0) { |
| 921 | list_del(&e->list); |
| 922 | free_entry(e); |
| 923 | } |
| 924 | } |
| 925 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 926 | /* |
| 927 | * sctp_bind_addrs - bind a SCTP socket to all our addresses |
| 928 | */ |
| 929 | static int sctp_bind_addrs(struct connection *con, uint16_t port) |
| 930 | { |
| 931 | struct sockaddr_storage localaddr; |
Christoph Hellwig | c0425a4 | 2020-05-29 14:09:42 +0200 | [diff] [blame] | 932 | struct sockaddr *addr = (struct sockaddr *)&localaddr; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 933 | int i, addr_len, result = 0; |
| 934 | |
| 935 | for (i = 0; i < dlm_local_count; i++) { |
| 936 | memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr)); |
| 937 | make_sockaddr(&localaddr, port, &addr_len); |
| 938 | |
| 939 | if (!i) |
Christoph Hellwig | c0425a4 | 2020-05-29 14:09:42 +0200 | [diff] [blame] | 940 | result = kernel_bind(con->sock, addr, addr_len); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 941 | else |
Christoph Hellwig | c0425a4 | 2020-05-29 14:09:42 +0200 | [diff] [blame] | 942 | result = sock_bind_add(con->sock->sk, addr, addr_len); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 943 | |
| 944 | if (result < 0) { |
| 945 | log_print("Can't bind to %d addr number %d, %d.\n", |
| 946 | port, i + 1, result); |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | return result; |
| 951 | } |
| 952 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 953 | /* Initiate an SCTP association. |
| 954 | This is a special case of send_to_sock() in that we don't yet have a |
| 955 | peeled-off socket for this association, so we use the listening socket |
| 956 | and add the primary IP address of the remote node. |
| 957 | */ |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 958 | static void sctp_connect_to_sock(struct connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 959 | { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 960 | struct sockaddr_storage daddr; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 961 | int result; |
| 962 | int addr_len; |
| 963 | struct socket *sock; |
Alexander Aring | 9c9f168 | 2020-06-26 13:26:50 -0400 | [diff] [blame] | 964 | unsigned int mark; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 965 | |
| 966 | if (con->nodeid == 0) { |
| 967 | log_print("attempt to connect sock 0 foiled"); |
| 968 | return; |
| 969 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 970 | |
Alexander Aring | 3f78cd7 | 2020-09-24 10:31:23 -0400 | [diff] [blame] | 971 | dlm_comm_mark(con->nodeid, &mark); |
Alexander Aring | 0461e0d | 2020-09-24 10:31:22 -0400 | [diff] [blame] | 972 | |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 973 | mutex_lock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 974 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 975 | /* Some odd races can cause double-connects, ignore them */ |
| 976 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 977 | goto out; |
| 978 | |
| 979 | if (con->sock) { |
| 980 | log_print("node %d already connected.", con->nodeid); |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | memset(&daddr, 0, sizeof(daddr)); |
| 985 | result = nodeid_to_addr(con->nodeid, &daddr, NULL, true); |
| 986 | if (result < 0) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 987 | log_print("no address for nodeid %d", con->nodeid); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 988 | goto out; |
David Teigland | 04bedd7 | 2009-09-18 14:31:47 -0500 | [diff] [blame] | 989 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 990 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 991 | /* Create a socket to communicate with */ |
| 992 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 993 | SOCK_STREAM, IPPROTO_SCTP, &sock); |
| 994 | if (result < 0) |
| 995 | goto socket_err; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 996 | |
Alexander Aring | 9c9f168 | 2020-06-26 13:26:50 -0400 | [diff] [blame] | 997 | sock_set_mark(sock->sk, mark); |
| 998 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 999 | con->rx_action = receive_from_sock; |
| 1000 | con->connect_action = sctp_connect_to_sock; |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1001 | add_sock(sock, con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1002 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1003 | /* Bind to all addresses. */ |
| 1004 | if (sctp_bind_addrs(con, 0)) |
| 1005 | goto bind_err; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 1006 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1007 | make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1008 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1009 | log_print("connecting to %d", con->nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1010 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1011 | /* Turn off Nagle's algorithm */ |
Christoph Hellwig | 40ef92c | 2020-05-29 14:09:40 +0200 | [diff] [blame] | 1012 | sctp_sock_set_nodelay(sock->sk); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1013 | |
Gang He | f706d83 | 2018-05-02 10:28:35 -0500 | [diff] [blame] | 1014 | /* |
| 1015 | * Make sock->ops->connect() function return in specified time, |
| 1016 | * since O_NONBLOCK argument in connect() function does not work here, |
| 1017 | * then, we should restore the default value of this attribute. |
| 1018 | */ |
Christoph Hellwig | 76ee078 | 2020-05-28 07:12:12 +0200 | [diff] [blame] | 1019 | sock_set_sndtimeo(sock->sk, 5); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1020 | result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len, |
Gang He | da3627c | 2018-05-29 11:09:22 +0800 | [diff] [blame] | 1021 | 0); |
Christoph Hellwig | 76ee078 | 2020-05-28 07:12:12 +0200 | [diff] [blame] | 1022 | sock_set_sndtimeo(sock->sk, 0); |
Gang He | f706d83 | 2018-05-02 10:28:35 -0500 | [diff] [blame] | 1023 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1024 | if (result == -EINPROGRESS) |
| 1025 | result = 0; |
| 1026 | if (result == 0) |
| 1027 | goto out; |
| 1028 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1029 | bind_err: |
| 1030 | con->sock = NULL; |
| 1031 | sock_release(sock); |
| 1032 | |
| 1033 | socket_err: |
| 1034 | /* |
| 1035 | * Some errors are fatal and this list might need adjusting. For other |
| 1036 | * errors we try again until the max number of retries is reached. |
| 1037 | */ |
| 1038 | if (result != -EHOSTUNREACH && |
| 1039 | result != -ENETUNREACH && |
| 1040 | result != -ENETDOWN && |
| 1041 | result != -EINVAL && |
| 1042 | result != -EPROTONOSUPPORT) { |
| 1043 | log_print("connect %d try %d error %d", con->nodeid, |
| 1044 | con->retries, result); |
| 1045 | mutex_unlock(&con->sock_mutex); |
| 1046 | msleep(1000); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1047 | lowcomms_connect_sock(con); |
| 1048 | return; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1049 | } |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1050 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1051 | out: |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1052 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* Connect a new socket to its peer */ |
| 1056 | static void tcp_connect_to_sock(struct connection *con) |
| 1057 | { |
Lon Hohberger | 6bd8fed | 2007-10-25 18:51:54 -0400 | [diff] [blame] | 1058 | struct sockaddr_storage saddr, src_addr; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1059 | int addr_len; |
Casey Dahlin | a89d63a | 2009-07-14 12:17:51 -0500 | [diff] [blame] | 1060 | struct socket *sock = NULL; |
Alexander Aring | 9c9f168 | 2020-06-26 13:26:50 -0400 | [diff] [blame] | 1061 | unsigned int mark; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1062 | int result; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1063 | |
| 1064 | if (con->nodeid == 0) { |
| 1065 | log_print("attempt to connect sock 0 foiled"); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
Alexander Aring | 3f78cd7 | 2020-09-24 10:31:23 -0400 | [diff] [blame] | 1069 | dlm_comm_mark(con->nodeid, &mark); |
Alexander Aring | 0461e0d | 2020-09-24 10:31:22 -0400 | [diff] [blame] | 1070 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1071 | mutex_lock(&con->sock_mutex); |
| 1072 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 1073 | goto out; |
| 1074 | |
| 1075 | /* Some odd races can cause double-connects, ignore them */ |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1076 | if (con->sock) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1077 | goto out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1078 | |
| 1079 | /* Create a socket to communicate with */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 1080 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1081 | SOCK_STREAM, IPPROTO_TCP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1082 | if (result < 0) |
| 1083 | goto out_err; |
| 1084 | |
Alexander Aring | 9c9f168 | 2020-06-26 13:26:50 -0400 | [diff] [blame] | 1085 | sock_set_mark(sock->sk, mark); |
| 1086 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1087 | memset(&saddr, 0, sizeof(saddr)); |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 1088 | result = nodeid_to_addr(con->nodeid, &saddr, NULL, false); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1089 | if (result < 0) { |
| 1090 | log_print("no address for nodeid %d", con->nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1091 | goto out_err; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1092 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1093 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1094 | con->rx_action = receive_from_sock; |
| 1095 | con->connect_action = tcp_connect_to_sock; |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 1096 | con->shutdown_action = dlm_tcp_shutdown; |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1097 | add_sock(sock, con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1098 | |
Lon Hohberger | 6bd8fed | 2007-10-25 18:51:54 -0400 | [diff] [blame] | 1099 | /* Bind to our cluster-known address connecting to avoid |
| 1100 | routing problems */ |
| 1101 | memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr)); |
| 1102 | make_sockaddr(&src_addr, 0, &addr_len); |
| 1103 | result = sock->ops->bind(sock, (struct sockaddr *) &src_addr, |
| 1104 | addr_len); |
| 1105 | if (result < 0) { |
| 1106 | log_print("could not bind for connect: %d", result); |
| 1107 | /* This *may* not indicate a critical error */ |
| 1108 | } |
| 1109 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1110 | make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len); |
| 1111 | |
| 1112 | log_print("connecting to %d", con->nodeid); |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1113 | |
| 1114 | /* Turn off Nagle's algorithm */ |
Christoph Hellwig | 12abc5e | 2020-05-28 07:12:19 +0200 | [diff] [blame] | 1115 | tcp_sock_set_nodelay(sock->sk); |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1116 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1117 | result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len, |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1118 | O_NONBLOCK); |
| 1119 | if (result == -EINPROGRESS) |
| 1120 | result = 0; |
| 1121 | if (result == 0) |
| 1122 | goto out; |
| 1123 | |
| 1124 | out_err: |
| 1125 | if (con->sock) { |
| 1126 | sock_release(con->sock); |
| 1127 | con->sock = NULL; |
Casey Dahlin | a89d63a | 2009-07-14 12:17:51 -0500 | [diff] [blame] | 1128 | } else if (sock) { |
| 1129 | sock_release(sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1130 | } |
| 1131 | /* |
| 1132 | * Some errors are fatal and this list might need adjusting. For other |
| 1133 | * errors we try again until the max number of retries is reached. |
| 1134 | */ |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1135 | if (result != -EHOSTUNREACH && |
| 1136 | result != -ENETUNREACH && |
| 1137 | result != -ENETDOWN && |
| 1138 | result != -EINVAL && |
| 1139 | result != -EPROTONOSUPPORT) { |
| 1140 | log_print("connect %d try %d error %d", con->nodeid, |
| 1141 | con->retries, result); |
| 1142 | mutex_unlock(&con->sock_mutex); |
| 1143 | msleep(1000); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1144 | lowcomms_connect_sock(con); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1145 | return; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1146 | } |
| 1147 | out: |
| 1148 | mutex_unlock(&con->sock_mutex); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | static struct socket *tcp_create_listen_sock(struct connection *con, |
| 1153 | struct sockaddr_storage *saddr) |
| 1154 | { |
| 1155 | struct socket *sock = NULL; |
| 1156 | int result = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1157 | int addr_len; |
| 1158 | |
| 1159 | if (dlm_local_addr[0]->ss_family == AF_INET) |
| 1160 | addr_len = sizeof(struct sockaddr_in); |
| 1161 | else |
| 1162 | addr_len = sizeof(struct sockaddr_in6); |
| 1163 | |
| 1164 | /* Create a socket to communicate with */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 1165 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1166 | SOCK_STREAM, IPPROTO_TCP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1167 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1168 | log_print("Can't create listening comms socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1169 | goto create_out; |
| 1170 | } |
| 1171 | |
Alexander Aring | a5b7ab6 | 2020-06-26 13:26:49 -0400 | [diff] [blame] | 1172 | sock_set_mark(sock->sk, dlm_config.ci_mark); |
| 1173 | |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1174 | /* Turn off Nagle's algorithm */ |
Christoph Hellwig | 12abc5e | 2020-05-28 07:12:19 +0200 | [diff] [blame] | 1175 | tcp_sock_set_nodelay(sock->sk); |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1176 | |
Christoph Hellwig | b58f0e8 | 2020-05-28 07:12:09 +0200 | [diff] [blame] | 1177 | sock_set_reuseaddr(sock->sk); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1178 | |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1179 | write_lock_bh(&sock->sk->sk_callback_lock); |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 1180 | sock->sk->sk_user_data = con; |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 1181 | save_listen_callbacks(sock); |
Christoph Hellwig | 0774dc7 | 2020-05-27 20:22:28 +0200 | [diff] [blame] | 1182 | con->rx_action = accept_from_sock; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1183 | con->connect_action = tcp_connect_to_sock; |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1184 | write_unlock_bh(&sock->sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1185 | |
| 1186 | /* Bind to our port */ |
| 1187 | make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len); |
| 1188 | result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len); |
| 1189 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1190 | log_print("Can't bind to port %d", dlm_config.ci_tcp_port); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1191 | sock_release(sock); |
| 1192 | sock = NULL; |
| 1193 | con->sock = NULL; |
| 1194 | goto create_out; |
| 1195 | } |
Christoph Hellwig | ce3d954 | 2020-05-28 07:12:15 +0200 | [diff] [blame] | 1196 | sock_set_keepalive(sock->sk); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1197 | |
| 1198 | result = sock->ops->listen(sock, 5); |
| 1199 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1200 | log_print("Can't listen on port %d", dlm_config.ci_tcp_port); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1201 | sock_release(sock); |
| 1202 | sock = NULL; |
| 1203 | goto create_out; |
| 1204 | } |
| 1205 | |
| 1206 | create_out: |
| 1207 | return sock; |
| 1208 | } |
| 1209 | |
| 1210 | /* Get local addresses */ |
| 1211 | static void init_local(void) |
| 1212 | { |
| 1213 | struct sockaddr_storage sas, *addr; |
| 1214 | int i; |
| 1215 | |
Patrick Caulfield | 30d3a23 | 2007-04-23 16:26:21 +0100 | [diff] [blame] | 1216 | dlm_local_count = 0; |
David Teigland | 1b189b8 | 2012-03-21 09:18:34 -0500 | [diff] [blame] | 1217 | for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1218 | if (dlm_our_addr(&sas, i)) |
| 1219 | break; |
| 1220 | |
Amitoj Kaur Chawla | 5c93f56 | 2016-06-23 10:22:01 +0530 | [diff] [blame] | 1221 | addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1222 | if (!addr) |
| 1223 | break; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1224 | dlm_local_addr[dlm_local_count++] = addr; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Alexander Aring | 043697f | 2020-08-27 15:02:50 -0400 | [diff] [blame] | 1228 | static void deinit_local(void) |
| 1229 | { |
| 1230 | int i; |
| 1231 | |
| 1232 | for (i = 0; i < dlm_local_count; i++) |
| 1233 | kfree(dlm_local_addr[i]); |
| 1234 | } |
| 1235 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1236 | /* Initialise SCTP socket and bind to all interfaces */ |
| 1237 | static int sctp_listen_for_all(void) |
| 1238 | { |
| 1239 | struct socket *sock = NULL; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1240 | int result = -EINVAL; |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 1241 | struct connection *con = nodeid2con(0, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1242 | |
| 1243 | if (!con) |
| 1244 | return -ENOMEM; |
| 1245 | |
| 1246 | log_print("Using SCTP for communications"); |
| 1247 | |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 1248 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1249 | SOCK_STREAM, IPPROTO_SCTP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1250 | if (result < 0) { |
| 1251 | log_print("Can't create comms socket, check SCTP is loaded"); |
| 1252 | goto out; |
| 1253 | } |
| 1254 | |
Christoph Hellwig | 26cfabf | 2020-05-28 07:12:16 +0200 | [diff] [blame] | 1255 | sock_set_rcvbuf(sock->sk, NEEDED_RMEM); |
Alexander Aring | a5b7ab6 | 2020-06-26 13:26:49 -0400 | [diff] [blame] | 1256 | sock_set_mark(sock->sk, dlm_config.ci_mark); |
Christoph Hellwig | 40ef92c | 2020-05-29 14:09:40 +0200 | [diff] [blame] | 1257 | sctp_sock_set_nodelay(sock->sk); |
Mike Christie | 86e92ad | 2013-06-14 04:56:14 -0500 | [diff] [blame] | 1258 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 1259 | write_lock_bh(&sock->sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1260 | /* Init con struct */ |
| 1261 | sock->sk->sk_user_data = con; |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 1262 | save_listen_callbacks(sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1263 | con->sock = sock; |
| 1264 | con->sock->sk->sk_data_ready = lowcomms_data_ready; |
Christoph Hellwig | 0774dc7 | 2020-05-27 20:22:28 +0200 | [diff] [blame] | 1265 | con->rx_action = accept_from_sock; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1266 | con->connect_action = sctp_connect_to_sock; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1267 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 1268 | write_unlock_bh(&sock->sk->sk_callback_lock); |
| 1269 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1270 | /* Bind to all addresses. */ |
| 1271 | if (sctp_bind_addrs(con, dlm_config.ci_tcp_port)) |
| 1272 | goto create_delsock; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1273 | |
| 1274 | result = sock->ops->listen(sock, 5); |
| 1275 | if (result < 0) { |
| 1276 | log_print("Can't set socket listening"); |
| 1277 | goto create_delsock; |
| 1278 | } |
| 1279 | |
| 1280 | return 0; |
| 1281 | |
| 1282 | create_delsock: |
| 1283 | sock_release(sock); |
| 1284 | con->sock = NULL; |
| 1285 | out: |
| 1286 | return result; |
| 1287 | } |
| 1288 | |
| 1289 | static int tcp_listen_for_all(void) |
| 1290 | { |
| 1291 | struct socket *sock = NULL; |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 1292 | struct connection *con = nodeid2con(0, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1293 | int result = -EINVAL; |
| 1294 | |
| 1295 | if (!con) |
| 1296 | return -ENOMEM; |
| 1297 | |
| 1298 | /* We don't support multi-homed hosts */ |
| 1299 | if (dlm_local_addr[1] != NULL) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1300 | log_print("TCP protocol can't handle multi-homed hosts, " |
| 1301 | "try SCTP"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | |
| 1305 | log_print("Using TCP for communications"); |
| 1306 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1307 | sock = tcp_create_listen_sock(con, dlm_local_addr[0]); |
| 1308 | if (sock) { |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1309 | add_sock(sock, con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1310 | result = 0; |
| 1311 | } |
| 1312 | else { |
| 1313 | result = -EADDRINUSE; |
| 1314 | } |
| 1315 | |
| 1316 | return result; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | static struct writequeue_entry *new_writequeue_entry(struct connection *con, |
| 1322 | gfp_t allocation) |
| 1323 | { |
| 1324 | struct writequeue_entry *entry; |
| 1325 | |
| 1326 | entry = kmalloc(sizeof(struct writequeue_entry), allocation); |
| 1327 | if (!entry) |
| 1328 | return NULL; |
| 1329 | |
| 1330 | entry->page = alloc_page(allocation); |
| 1331 | if (!entry->page) { |
| 1332 | kfree(entry); |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | |
| 1336 | entry->offset = 0; |
| 1337 | entry->len = 0; |
| 1338 | entry->end = 0; |
| 1339 | entry->users = 0; |
| 1340 | entry->con = con; |
| 1341 | |
| 1342 | return entry; |
| 1343 | } |
| 1344 | |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1345 | void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1346 | { |
| 1347 | struct connection *con; |
| 1348 | struct writequeue_entry *e; |
| 1349 | int offset = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1350 | |
| 1351 | con = nodeid2con(nodeid, allocation); |
| 1352 | if (!con) |
| 1353 | return NULL; |
| 1354 | |
| 1355 | spin_lock(&con->writequeue_lock); |
| 1356 | e = list_entry(con->writequeue.prev, struct writequeue_entry, list); |
| 1357 | if ((&e->list == &con->writequeue) || |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1358 | (PAGE_SIZE - e->end < len)) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1359 | e = NULL; |
| 1360 | } else { |
| 1361 | offset = e->end; |
| 1362 | e->end += len; |
Wei Yongjun | eeee2b5 | 2012-10-18 22:57:19 +0800 | [diff] [blame] | 1363 | e->users++; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1364 | } |
| 1365 | spin_unlock(&con->writequeue_lock); |
| 1366 | |
| 1367 | if (e) { |
| 1368 | got_one: |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1369 | *ppc = page_address(e->page) + offset; |
| 1370 | return e; |
| 1371 | } |
| 1372 | |
| 1373 | e = new_writequeue_entry(con, allocation); |
| 1374 | if (e) { |
| 1375 | spin_lock(&con->writequeue_lock); |
| 1376 | offset = e->end; |
| 1377 | e->end += len; |
Wei Yongjun | eeee2b5 | 2012-10-18 22:57:19 +0800 | [diff] [blame] | 1378 | e->users++; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1379 | list_add_tail(&e->list, &con->writequeue); |
| 1380 | spin_unlock(&con->writequeue_lock); |
| 1381 | goto got_one; |
| 1382 | } |
| 1383 | return NULL; |
| 1384 | } |
| 1385 | |
| 1386 | void dlm_lowcomms_commit_buffer(void *mh) |
| 1387 | { |
| 1388 | struct writequeue_entry *e = (struct writequeue_entry *)mh; |
| 1389 | struct connection *con = e->con; |
| 1390 | int users; |
| 1391 | |
| 1392 | spin_lock(&con->writequeue_lock); |
| 1393 | users = --e->users; |
| 1394 | if (users) |
| 1395 | goto out; |
| 1396 | e->len = e->end - e->offset; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1397 | spin_unlock(&con->writequeue_lock); |
| 1398 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1399 | queue_work(send_workqueue, &con->swork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1400 | return; |
| 1401 | |
| 1402 | out: |
| 1403 | spin_unlock(&con->writequeue_lock); |
| 1404 | return; |
| 1405 | } |
| 1406 | |
| 1407 | /* Send a message */ |
| 1408 | static void send_to_sock(struct connection *con) |
| 1409 | { |
| 1410 | int ret = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1411 | const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 1412 | struct writequeue_entry *e; |
| 1413 | int len, offset; |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1414 | int count = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1415 | |
| 1416 | mutex_lock(&con->sock_mutex); |
| 1417 | if (con->sock == NULL) |
| 1418 | goto out_connect; |
| 1419 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1420 | spin_lock(&con->writequeue_lock); |
| 1421 | for (;;) { |
| 1422 | e = list_entry(con->writequeue.next, struct writequeue_entry, |
| 1423 | list); |
| 1424 | if ((struct list_head *) e == &con->writequeue) |
| 1425 | break; |
| 1426 | |
| 1427 | len = e->len; |
| 1428 | offset = e->offset; |
| 1429 | BUG_ON(len == 0 && e->users == 0); |
| 1430 | spin_unlock(&con->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1431 | |
| 1432 | ret = 0; |
| 1433 | if (len) { |
Paolo Bonzini | 1329e3f | 2009-08-24 13:18:04 -0500 | [diff] [blame] | 1434 | ret = kernel_sendpage(con->sock, e->page, offset, len, |
| 1435 | msg_flags); |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1436 | if (ret == -EAGAIN || ret == 0) { |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 1437 | if (ret == -EAGAIN && |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 1438 | test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) && |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 1439 | !test_and_set_bit(CF_APP_LIMITED, &con->flags)) { |
| 1440 | /* Notify TCP that we're limited by the |
| 1441 | * application window size. |
| 1442 | */ |
| 1443 | set_bit(SOCK_NOSPACE, &con->sock->flags); |
| 1444 | con->sock->sk->sk_write_pending++; |
| 1445 | } |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1446 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1447 | goto out; |
Ying Xue | 9c5bef5 | 2012-08-13 14:29:55 +0800 | [diff] [blame] | 1448 | } else if (ret < 0) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1449 | goto send_error; |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1450 | } |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1451 | |
| 1452 | /* Don't starve people filling buffers */ |
| 1453 | if (++count >= MAX_SEND_MSG_COUNT) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1454 | cond_resched(); |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1455 | count = 0; |
| 1456 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1457 | |
| 1458 | spin_lock(&con->writequeue_lock); |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1459 | writequeue_entry_complete(e, ret); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1460 | } |
| 1461 | spin_unlock(&con->writequeue_lock); |
| 1462 | out: |
| 1463 | mutex_unlock(&con->sock_mutex); |
| 1464 | return; |
| 1465 | |
| 1466 | send_error: |
| 1467 | mutex_unlock(&con->sock_mutex); |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 1468 | close_connection(con, false, false, true); |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1469 | /* Requeue the send work. When the work daemon runs again, it will try |
| 1470 | a new connection, then call this function again. */ |
| 1471 | queue_work(send_workqueue, &con->swork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1472 | return; |
| 1473 | |
| 1474 | out_connect: |
| 1475 | mutex_unlock(&con->sock_mutex); |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1476 | queue_work(send_workqueue, &con->swork); |
| 1477 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | static void clean_one_writequeue(struct connection *con) |
| 1481 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1482 | struct writequeue_entry *e, *safe; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1483 | |
| 1484 | spin_lock(&con->writequeue_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1485 | list_for_each_entry_safe(e, safe, &con->writequeue, list) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1486 | list_del(&e->list); |
| 1487 | free_entry(e); |
| 1488 | } |
| 1489 | spin_unlock(&con->writequeue_lock); |
| 1490 | } |
| 1491 | |
| 1492 | /* Called from recovery when it knows that a node has |
| 1493 | left the cluster */ |
| 1494 | int dlm_lowcomms_close(int nodeid) |
| 1495 | { |
| 1496 | struct connection *con; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1497 | struct dlm_node_addr *na; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1498 | |
| 1499 | log_print("closing connection to node %d", nodeid); |
| 1500 | con = nodeid2con(nodeid, 0); |
| 1501 | if (con) { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1502 | set_bit(CF_CLOSE, &con->flags); |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 1503 | close_connection(con, true, true, true); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1504 | clean_one_writequeue(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1505 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1506 | |
| 1507 | spin_lock(&dlm_node_addrs_spin); |
| 1508 | na = find_node_addr(nodeid); |
| 1509 | if (na) { |
| 1510 | list_del(&na->list); |
| 1511 | while (na->addr_count--) |
| 1512 | kfree(na->addr[na->addr_count]); |
| 1513 | kfree(na); |
| 1514 | } |
| 1515 | spin_unlock(&dlm_node_addrs_spin); |
| 1516 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | /* Receive workqueue function */ |
| 1521 | static void process_recv_sockets(struct work_struct *work) |
| 1522 | { |
| 1523 | struct connection *con = container_of(work, struct connection, rwork); |
| 1524 | int err; |
| 1525 | |
| 1526 | clear_bit(CF_READ_PENDING, &con->flags); |
| 1527 | do { |
| 1528 | err = con->rx_action(con); |
| 1529 | } while (!err); |
| 1530 | } |
| 1531 | |
| 1532 | /* Send workqueue function */ |
| 1533 | static void process_send_sockets(struct work_struct *work) |
| 1534 | { |
| 1535 | struct connection *con = container_of(work, struct connection, swork); |
| 1536 | |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1537 | clear_bit(CF_WRITE_PENDING, &con->flags); |
Bob Peterson | 61d9102b | 2017-09-12 08:55:04 +0000 | [diff] [blame] | 1538 | if (con->sock == NULL) /* not mutex protected so check it inside too */ |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1539 | con->connect_action(con); |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1540 | if (!list_empty(&con->writequeue)) |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1541 | send_to_sock(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1544 | static void work_stop(void) |
| 1545 | { |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1546 | if (recv_workqueue) |
| 1547 | destroy_workqueue(recv_workqueue); |
| 1548 | if (send_workqueue) |
| 1549 | destroy_workqueue(send_workqueue); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | static int work_start(void) |
| 1553 | { |
David Teigland | e43f055 | 2011-03-10 13:22:34 -0600 | [diff] [blame] | 1554 | recv_workqueue = alloc_workqueue("dlm_recv", |
| 1555 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1556 | if (!recv_workqueue) { |
| 1557 | log_print("can't start dlm_recv"); |
| 1558 | return -ENOMEM; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1559 | } |
| 1560 | |
David Teigland | e43f055 | 2011-03-10 13:22:34 -0600 | [diff] [blame] | 1561 | send_workqueue = alloc_workqueue("dlm_send", |
| 1562 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1563 | if (!send_workqueue) { |
| 1564 | log_print("can't start dlm_send"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1565 | destroy_workqueue(recv_workqueue); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1566 | return -ENOMEM; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1572 | static void _stop_conn(struct connection *con, bool and_other) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1573 | { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1574 | mutex_lock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | 173a31f | 2017-09-12 09:01:24 +0000 | [diff] [blame] | 1575 | set_bit(CF_CLOSE, &con->flags); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1576 | set_bit(CF_READ_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1577 | set_bit(CF_WRITE_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1578 | if (con->sock && con->sock->sk) { |
| 1579 | write_lock_bh(&con->sock->sk->sk_callback_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1580 | con->sock->sk->sk_user_data = NULL; |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1581 | write_unlock_bh(&con->sock->sk->sk_callback_lock); |
| 1582 | } |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1583 | if (con->othercon && and_other) |
| 1584 | _stop_conn(con->othercon, false); |
| 1585 | mutex_unlock(&con->sock_mutex); |
| 1586 | } |
| 1587 | |
| 1588 | static void stop_conn(struct connection *con) |
| 1589 | { |
| 1590 | _stop_conn(con, true); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1591 | } |
| 1592 | |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 1593 | static void shutdown_conn(struct connection *con) |
| 1594 | { |
| 1595 | if (con->shutdown_action) |
| 1596 | con->shutdown_action(con); |
| 1597 | } |
| 1598 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1599 | static void connection_release(struct rcu_head *rcu) |
| 1600 | { |
| 1601 | struct connection *con = container_of(rcu, struct connection, rcu); |
| 1602 | |
| 1603 | kfree(con->rx_buf); |
| 1604 | kfree(con); |
| 1605 | } |
| 1606 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1607 | static void free_conn(struct connection *con) |
| 1608 | { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 1609 | close_connection(con, true, true, true); |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1610 | spin_lock(&connections_lock); |
| 1611 | hlist_del_rcu(&con->list); |
| 1612 | spin_unlock(&connections_lock); |
Alexander Aring | 948c47e | 2020-08-27 15:02:53 -0400 | [diff] [blame] | 1613 | if (con->othercon) { |
| 1614 | clean_one_writequeue(con->othercon); |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1615 | call_rcu(&con->othercon->rcu, connection_release); |
Alexander Aring | 948c47e | 2020-08-27 15:02:53 -0400 | [diff] [blame] | 1616 | } |
Alexander Aring | 0de9843 | 2020-08-27 15:02:52 -0400 | [diff] [blame] | 1617 | clean_one_writequeue(con); |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1618 | call_rcu(&con->rcu, connection_release); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1619 | } |
| 1620 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1621 | static void work_flush(void) |
| 1622 | { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1623 | int ok, idx; |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1624 | int i; |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1625 | struct connection *con; |
| 1626 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1627 | do { |
| 1628 | ok = 1; |
| 1629 | foreach_conn(stop_conn); |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1630 | if (recv_workqueue) |
| 1631 | flush_workqueue(recv_workqueue); |
| 1632 | if (send_workqueue) |
| 1633 | flush_workqueue(send_workqueue); |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1634 | idx = srcu_read_lock(&connections_srcu); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1635 | for (i = 0; i < CONN_HASH_SIZE && ok; i++) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1636 | hlist_for_each_entry_rcu(con, &connection_hash[i], |
| 1637 | list) { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1638 | ok &= test_bit(CF_READ_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1639 | ok &= test_bit(CF_WRITE_PENDING, &con->flags); |
| 1640 | if (con->othercon) { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1641 | ok &= test_bit(CF_READ_PENDING, |
| 1642 | &con->othercon->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1643 | ok &= test_bit(CF_WRITE_PENDING, |
| 1644 | &con->othercon->flags); |
| 1645 | } |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1648 | srcu_read_unlock(&connections_srcu, idx); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1649 | } while (!ok); |
| 1650 | } |
| 1651 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1652 | void dlm_lowcomms_stop(void) |
| 1653 | { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1654 | /* Set all the flags to prevent any |
| 1655 | socket activity. |
| 1656 | */ |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1657 | dlm_allow_conn = 0; |
Alexander Aring | aa7ab1e | 2020-08-27 15:02:48 -0400 | [diff] [blame] | 1658 | |
| 1659 | if (recv_workqueue) |
| 1660 | flush_workqueue(recv_workqueue); |
| 1661 | if (send_workqueue) |
| 1662 | flush_workqueue(send_workqueue); |
| 1663 | |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 1664 | foreach_conn(shutdown_conn); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1665 | work_flush(); |
Marcelo Ricardo Leitner | 3a8db79 | 2016-10-08 10:14:37 -0300 | [diff] [blame] | 1666 | foreach_conn(free_conn); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1667 | work_stop(); |
Alexander Aring | 043697f | 2020-08-27 15:02:50 -0400 | [diff] [blame] | 1668 | deinit_local(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | int dlm_lowcomms_start(void) |
| 1672 | { |
| 1673 | int error = -EINVAL; |
| 1674 | struct connection *con; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1675 | int i; |
| 1676 | |
| 1677 | for (i = 0; i < CONN_HASH_SIZE; i++) |
| 1678 | INIT_HLIST_HEAD(&connection_hash[i]); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1679 | |
| 1680 | init_local(); |
| 1681 | if (!dlm_local_count) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1682 | error = -ENOTCONN; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1683 | log_print("no local IP address has been set"); |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1684 | goto fail; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1687 | error = work_start(); |
| 1688 | if (error) |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1689 | goto fail; |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1690 | |
| 1691 | dlm_allow_conn = 1; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1692 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1693 | /* Start listening */ |
| 1694 | if (dlm_config.ci_protocol == 0) |
| 1695 | error = tcp_listen_for_all(); |
| 1696 | else |
| 1697 | error = sctp_listen_for_all(); |
| 1698 | if (error) |
| 1699 | goto fail_unlisten; |
| 1700 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1701 | return 0; |
| 1702 | |
| 1703 | fail_unlisten: |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1704 | dlm_allow_conn = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1705 | con = nodeid2con(0,0); |
Alexander Aring | 7ae0451 | 2020-08-27 15:02:54 -0400 | [diff] [blame] | 1706 | if (con) |
| 1707 | free_conn(con); |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1708 | fail: |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1709 | return error; |
| 1710 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1711 | |
| 1712 | void dlm_lowcomms_exit(void) |
| 1713 | { |
| 1714 | struct dlm_node_addr *na, *safe; |
| 1715 | |
| 1716 | spin_lock(&dlm_node_addrs_spin); |
| 1717 | list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) { |
| 1718 | list_del(&na->list); |
| 1719 | while (na->addr_count--) |
| 1720 | kfree(na->addr[na->addr_count]); |
| 1721 | kfree(na); |
| 1722 | } |
| 1723 | spin_unlock(&dlm_node_addrs_spin); |
| 1724 | } |