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