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