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