blob: 8f715c620e1f86942487cd08db847d32f7807f4d [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06006** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01007**
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01008**
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 Perches2cf12c02009-01-22 13:26:47 -080022 * be expanded for the cluster infrastructure then that is its
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010023 * 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 Perches2cf12c02009-01-22 13:26:47 -080037 * lowcomms will choose to use either TCP or SCTP as its transport layer
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010038 * depending on the configuration variable 'protocol'. This should be set
Joe Perches2cf12c02009-01-22 13:26:47 -080039 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010040 * 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 Caulfield6ed7257b2007-04-17 15:39:57 +010049#include <linux/file.h>
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -050050#include <linux/mutex.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010051#include <linux/sctp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +000053#include <net/sctp/sctp.h>
Joe Perches44ad5322009-01-22 13:24:49 -080054#include <net/ipv6.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010055
56#include "dlm_internal.h"
57#include "lowcomms.h"
58#include "midcomms.h"
59#include "config.h"
60
61#define NEEDED_RMEM (4*1024*1024)
62
Bob Petersonf92c8dd2010-11-12 11:15:20 -060063/* Number of messages to send before rescheduling */
64#define MAX_SEND_MSG_COUNT 25
Alexander Aring055923b2020-07-27 09:13:38 -040065#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
Bob Petersonf92c8dd2010-11-12 11:15:20 -060066
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010067struct connection {
68 struct socket *sock; /* NULL if not connected */
69 uint32_t nodeid; /* So we know who we are in the list */
70 struct mutex sock_mutex;
71 unsigned long flags;
72#define CF_READ_PENDING 1
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +000073#define CF_WRITE_PENDING 2
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010074#define CF_INIT_PENDING 4
75#define CF_IS_OTHERCON 5
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -050076#define CF_CLOSE 6
David Millerb36930d2010-11-10 21:56:39 -080077#define CF_APP_LIMITED 7
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +000078#define CF_CLOSING 8
Alexander Aring055923b2020-07-27 09:13:38 -040079#define CF_SHUTDOWN 9
Alexander Aring19633c72020-11-02 20:04:20 -050080#define CF_CONNECTED 10
Alexander Aringba868d92021-05-21 15:08:37 -040081#define CF_RECONNECT 11
82#define CF_DELAY_CONNECT 12
Alexander Aring8aa31cb2021-05-21 15:08:39 -040083#define CF_EOF 13
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010084 struct list_head writequeue; /* List of outgoing writequeue_entries */
85 spinlock_t writequeue_lock;
Alexander Aring8aa31cb2021-05-21 15:08:39 -040086 atomic_t writequeue_cnt;
Alexander Aringc51b0222021-07-16 16:22:44 -040087 struct mutex wq_alloc;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010088 int retries;
89#define MAX_CONNECT_RETRIES 3
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060090 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010091 struct connection *othercon;
Alexander Aringba868d92021-05-21 15:08:37 -040092 struct connection *sendcon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010093 struct work_struct rwork; /* Receive workqueue */
94 struct work_struct swork; /* Send workqueue */
Alexander Aring055923b2020-07-27 09:13:38 -040095 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
Alexander Aring4798cbb2020-09-24 10:31:26 -040096 unsigned char *rx_buf;
97 int rx_buflen;
98 int rx_leftover;
Alexander Aringa47666eb2020-08-27 15:02:49 -040099 struct rcu_head rcu;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100100};
101#define sock2con(x) ((struct connection *)(x)->sk_user_data)
102
Alexander Aringd11ccd42020-11-02 20:04:25 -0500103struct listen_connection {
104 struct socket *sock;
105 struct work_struct rwork;
106};
107
Alexander Aringf0747ebf2021-03-01 17:05:16 -0500108#define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
109#define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
110
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100111/* An entry waiting to be sent */
112struct writequeue_entry {
113 struct list_head list;
114 struct page *page;
115 int offset;
116 int len;
117 int end;
118 int users;
Alexander Aring706474f2021-05-21 15:08:48 -0400119 bool dirty;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100120 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400121 struct list_head msgs;
122 struct kref ref;
123};
124
125struct dlm_msg {
126 struct writequeue_entry *entry;
Alexander Aring2874d1a2021-05-21 15:08:43 -0400127 struct dlm_msg *orig_msg;
128 bool retransmit;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400129 void *ppc;
130 int len;
131 int idx; /* new()/commit() idx exchange */
132
133 struct list_head list;
134 struct kref ref;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100135};
136
David Teigland36b71a82012-07-26 12:44:30 -0500137struct dlm_node_addr {
138 struct list_head list;
139 int nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500140 int mark;
David Teigland36b71a82012-07-26 12:44:30 -0500141 int addr_count;
Mike Christie98e1b602013-06-14 04:56:12 -0500142 int curr_addr_index;
David Teigland36b71a82012-07-26 12:44:30 -0500143 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
144};
145
Alexander Aringa66c0082021-07-16 16:22:40 -0400146struct dlm_proto_ops {
Alexander Aring8728a452021-07-16 16:22:43 -0400147 bool try_new_addr;
Alexander Aring2dc6b112021-07-16 16:22:41 -0400148 const char *name;
149 int proto;
150
Alexander Aring8728a452021-07-16 16:22:43 -0400151 int (*connect)(struct connection *con, struct socket *sock,
152 struct sockaddr *addr, int addr_len);
153 void (*sockopts)(struct socket *sock);
154 int (*bind)(struct socket *sock);
Alexander Aring2dc6b112021-07-16 16:22:41 -0400155 int (*listen_validate)(void);
156 void (*listen_sockopts)(struct socket *sock);
157 int (*listen_bind)(struct socket *sock);
Alexander Aringa66c0082021-07-16 16:22:40 -0400158 /* What to do to shutdown */
159 void (*shutdown_action)(struct connection *con);
160 /* What to do to eof check */
161 bool (*eof_condition)(struct connection *con);
162};
163
Bob Petersoncc661fc2017-09-12 08:55:23 +0000164static struct listen_sock_callbacks {
165 void (*sk_error_report)(struct sock *);
166 void (*sk_data_ready)(struct sock *);
167 void (*sk_state_change)(struct sock *);
168 void (*sk_write_space)(struct sock *);
169} listen_sock;
170
David Teigland36b71a82012-07-26 12:44:30 -0500171static LIST_HEAD(dlm_node_addrs);
172static DEFINE_SPINLOCK(dlm_node_addrs_spin);
173
Alexander Aringd11ccd42020-11-02 20:04:25 -0500174static struct listen_connection listen_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100175static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
176static int dlm_local_count;
Alexander Aring51746162021-03-01 17:05:13 -0500177int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100178
179/* Work queues */
180static struct workqueue_struct *recv_workqueue;
181static struct workqueue_struct *send_workqueue;
182
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600183static struct hlist_head connection_hash[CONN_HASH_SIZE];
Alexander Aringa47666eb2020-08-27 15:02:49 -0400184static DEFINE_SPINLOCK(connections_lock);
185DEFINE_STATIC_SRCU(connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100186
Alexander Aringa66c0082021-07-16 16:22:40 -0400187static const struct dlm_proto_ops *dlm_proto_ops;
188
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100189static void process_recv_sockets(struct work_struct *work);
190static void process_send_sockets(struct work_struct *work);
191
Alexander Aring66d59552021-07-16 16:22:39 -0400192/* need to held writequeue_lock */
193static struct writequeue_entry *con_next_wq(struct connection *con)
194{
195 struct writequeue_entry *e;
196
197 if (list_empty(&con->writequeue))
198 return NULL;
199
200 e = list_first_entry(&con->writequeue, struct writequeue_entry,
201 list);
202 if (e->len == 0)
203 return NULL;
204
205 return e;
206}
207
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400208static struct connection *__find_con(int nodeid, int r)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600209{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600210 struct connection *con;
211
Alexander Aringa47666eb2020-08-27 15:02:49 -0400212 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400213 if (con->nodeid == nodeid)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600214 return con;
215 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400216
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600217 return NULL;
218}
219
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400220static bool tcp_eof_condition(struct connection *con)
221{
222 return atomic_read(&con->writequeue_cnt);
223}
224
Alexander Aring6cde2102020-11-02 20:04:21 -0500225static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100226{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400227 con->rx_buflen = dlm_config.ci_buffer_size;
228 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500229 if (!con->rx_buf)
230 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400231
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100232 con->nodeid = nodeid;
233 mutex_init(&con->sock_mutex);
234 INIT_LIST_HEAD(&con->writequeue);
235 spin_lock_init(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400236 atomic_set(&con->writequeue_cnt, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100237 INIT_WORK(&con->swork, process_send_sockets);
238 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400239 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100240
Alexander Aring6cde2102020-11-02 20:04:21 -0500241 return 0;
242}
243
244/*
245 * If 'allocation' is zero then we don't attempt to create a new
246 * connection structure for this node.
247 */
248static struct connection *nodeid2con(int nodeid, gfp_t alloc)
249{
250 struct connection *con, *tmp;
251 int r, ret;
252
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400253 r = nodeid_hash(nodeid);
254 con = __find_con(nodeid, r);
Alexander Aring6cde2102020-11-02 20:04:21 -0500255 if (con || !alloc)
256 return con;
257
258 con = kzalloc(sizeof(*con), alloc);
259 if (!con)
260 return NULL;
261
262 ret = dlm_con_init(con, nodeid);
263 if (ret) {
264 kfree(con);
265 return NULL;
266 }
267
Alexander Aringc51b0222021-07-16 16:22:44 -0400268 mutex_init(&con->wq_alloc);
269
Alexander Aringa47666eb2020-08-27 15:02:49 -0400270 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400271 /* Because multiple workqueues/threads calls this function it can
272 * race on multiple cpu's. Instead of locking hot path __find_con()
273 * we just check in rare cases of recently added nodes again
274 * under protection of connections_lock. If this is the case we
275 * abort our connection creation and return the existing connection.
276 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400277 tmp = __find_con(nodeid, r);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400278 if (tmp) {
279 spin_unlock(&connections_lock);
280 kfree(con->rx_buf);
281 kfree(con);
282 return tmp;
283 }
284
Alexander Aringa47666eb2020-08-27 15:02:49 -0400285 hlist_add_head_rcu(&con->list, &connection_hash[r]);
286 spin_unlock(&connections_lock);
287
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100288 return con;
289}
290
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600291/* Loop round all connections */
292static void foreach_conn(void (*conn_func)(struct connection *c))
293{
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400294 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600295 struct connection *con;
296
297 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400298 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600299 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600300 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100301}
302
David Teigland36b71a82012-07-26 12:44:30 -0500303static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100304{
David Teigland36b71a82012-07-26 12:44:30 -0500305 struct dlm_node_addr *na;
306
307 list_for_each_entry(na, &dlm_node_addrs, list) {
308 if (na->nodeid == nodeid)
309 return na;
310 }
311 return NULL;
312}
313
Alexander Aring40c6b832020-11-02 20:04:27 -0500314static int addr_compare(const struct sockaddr_storage *x,
315 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500316{
317 switch (x->ss_family) {
318 case AF_INET: {
319 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
320 struct sockaddr_in *siny = (struct sockaddr_in *)y;
321 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
322 return 0;
323 if (sinx->sin_port != siny->sin_port)
324 return 0;
325 break;
326 }
327 case AF_INET6: {
328 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
329 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
330 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
331 return 0;
332 if (sinx->sin6_port != siny->sin6_port)
333 return 0;
334 break;
335 }
336 default:
337 return 0;
338 }
339 return 1;
340}
341
342static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Alexander Aringe125fbe2021-03-01 17:05:09 -0500343 struct sockaddr *sa_out, bool try_new_addr,
344 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500345{
346 struct sockaddr_storage sas;
347 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100348
349 if (!dlm_local_count)
350 return -1;
351
David Teigland36b71a82012-07-26 12:44:30 -0500352 spin_lock(&dlm_node_addrs_spin);
353 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500354 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300355 memcpy(&sas, na->addr[na->curr_addr_index],
356 sizeof(struct sockaddr_storage));
357
Mike Christie98e1b602013-06-14 04:56:12 -0500358 if (try_new_addr) {
359 na->curr_addr_index++;
360 if (na->curr_addr_index == na->addr_count)
361 na->curr_addr_index = 0;
362 }
Mike Christie98e1b602013-06-14 04:56:12 -0500363 }
David Teigland36b71a82012-07-26 12:44:30 -0500364 spin_unlock(&dlm_node_addrs_spin);
365
366 if (!na)
367 return -EEXIST;
368
369 if (!na->addr_count)
370 return -ENOENT;
371
Alexander Aringe125fbe2021-03-01 17:05:09 -0500372 *mark = na->mark;
373
David Teigland36b71a82012-07-26 12:44:30 -0500374 if (sas_out)
375 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
376
377 if (!sa_out)
378 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100379
380 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500381 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
382 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100383 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
384 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500385 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
386 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000387 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100388 }
389
390 return 0;
391}
392
Alexander Aringe125fbe2021-03-01 17:05:09 -0500393static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
394 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500395{
396 struct dlm_node_addr *na;
397 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500398 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500399
400 spin_lock(&dlm_node_addrs_spin);
401 list_for_each_entry(na, &dlm_node_addrs, list) {
402 if (!na->addr_count)
403 continue;
404
Mike Christie98e1b602013-06-14 04:56:12 -0500405 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
406 if (addr_compare(na->addr[addr_i], addr)) {
407 *nodeid = na->nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500408 *mark = na->mark;
Mike Christie98e1b602013-06-14 04:56:12 -0500409 rv = 0;
410 goto unlock;
411 }
412 }
David Teigland36b71a82012-07-26 12:44:30 -0500413 }
Mike Christie98e1b602013-06-14 04:56:12 -0500414unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500415 spin_unlock(&dlm_node_addrs_spin);
416 return rv;
417}
418
Alexander Aring4f19d072020-11-02 20:04:28 -0500419/* caller need to held dlm_node_addrs_spin lock */
420static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
421 const struct sockaddr_storage *addr)
422{
423 int i;
424
425 for (i = 0; i < na->addr_count; i++) {
426 if (addr_compare(na->addr[i], addr))
427 return true;
428 }
429
430 return false;
431}
432
David Teigland36b71a82012-07-26 12:44:30 -0500433int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
434{
435 struct sockaddr_storage *new_addr;
436 struct dlm_node_addr *new_node, *na;
Alexander Aring4f19d072020-11-02 20:04:28 -0500437 bool ret;
David Teigland36b71a82012-07-26 12:44:30 -0500438
439 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
440 if (!new_node)
441 return -ENOMEM;
442
443 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
444 if (!new_addr) {
445 kfree(new_node);
446 return -ENOMEM;
447 }
448
449 memcpy(new_addr, addr, len);
450
451 spin_lock(&dlm_node_addrs_spin);
452 na = find_node_addr(nodeid);
453 if (!na) {
454 new_node->nodeid = nodeid;
455 new_node->addr[0] = new_addr;
456 new_node->addr_count = 1;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500457 new_node->mark = dlm_config.ci_mark;
David Teigland36b71a82012-07-26 12:44:30 -0500458 list_add(&new_node->list, &dlm_node_addrs);
459 spin_unlock(&dlm_node_addrs_spin);
460 return 0;
461 }
462
Alexander Aring4f19d072020-11-02 20:04:28 -0500463 ret = dlm_lowcomms_na_has_addr(na, addr);
464 if (ret) {
465 spin_unlock(&dlm_node_addrs_spin);
466 kfree(new_addr);
467 kfree(new_node);
468 return -EEXIST;
469 }
470
David Teigland36b71a82012-07-26 12:44:30 -0500471 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
472 spin_unlock(&dlm_node_addrs_spin);
473 kfree(new_addr);
474 kfree(new_node);
475 return -ENOSPC;
476 }
477
478 na->addr[na->addr_count++] = new_addr;
479 spin_unlock(&dlm_node_addrs_spin);
480 kfree(new_node);
481 return 0;
482}
483
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100484/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400485static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100486{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000487 struct connection *con;
488
489 read_lock_bh(&sk->sk_callback_lock);
490 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500491 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100492 queue_work(recv_workqueue, &con->rwork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000493 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100494}
495
Alexander Aringd11ccd42020-11-02 20:04:25 -0500496static void lowcomms_listen_data_ready(struct sock *sk)
497{
Alexander Aring9a4139a2021-06-02 09:45:18 -0400498 if (!dlm_allow_conn)
499 return;
500
Alexander Aringd11ccd42020-11-02 20:04:25 -0500501 queue_work(recv_workqueue, &listen_con.rwork);
502}
503
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100504static void lowcomms_write_space(struct sock *sk)
505{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000506 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100507
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000508 read_lock_bh(&sk->sk_callback_lock);
509 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800510 if (!con)
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000511 goto out;
David Millerb36930d2010-11-10 21:56:39 -0800512
Alexander Aring19633c72020-11-02 20:04:20 -0500513 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
514 log_print("successful connected to node %d", con->nodeid);
515 queue_work(send_workqueue, &con->swork);
516 goto out;
517 }
518
David Millerb36930d2010-11-10 21:56:39 -0800519 clear_bit(SOCK_NOSPACE, &con->sock->flags);
520
521 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
522 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800523 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800524 }
525
Bob Peterson01da24d2017-09-12 08:55:14 +0000526 queue_work(send_workqueue, &con->swork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000527out:
528 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100529}
530
531static inline void lowcomms_connect_sock(struct connection *con)
532{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500533 if (test_bit(CF_CLOSE, &con->flags))
534 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000535 queue_work(send_workqueue, &con->swork);
536 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100537}
538
539static void lowcomms_state_change(struct sock *sk)
540{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300541 /* 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 Caulfield6ed7257b2007-04-17 15:39:57 +0100550 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300551 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100552}
553
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500554int dlm_lowcomms_connect_node(int nodeid)
555{
556 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400557 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500558
559 if (nodeid == dlm_our_nodeid())
560 return 0;
561
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400562 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500563 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400564 if (!con) {
565 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500566 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400567 }
568
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500569 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400570 srcu_read_unlock(&connections_srcu, idx);
571
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500572 return 0;
573}
574
Alexander Aringe125fbe2021-03-01 17:05:09 -0500575int 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 Petersonb3a5bbf2015-08-27 09:34:47 -0500592static void lowcomms_error_report(struct sock *sk)
593{
Bob Petersonb81171c2016-02-05 14:39:02 -0500594 struct connection *con;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500595 struct sockaddr_storage saddr;
Bob Petersonb81171c2016-02-05 14:39:02 -0500596 void (*orig_report)(struct sock *) = NULL;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500597
Bob Petersonb81171c2016-02-05 14:39:02 -0500598 read_lock_bh(&sk->sk_callback_lock);
599 con = sock2con(sk);
600 if (con == NULL)
601 goto out;
602
Bob Petersoncc661fc2017-09-12 08:55:23 +0000603 orig_report = listen_sock.sk_error_report;
Alexander Aringfeb704b2021-07-16 16:22:34 -0400604 if (kernel_getpeername(sk->sk_socket, (struct sockaddr *)&saddr) < 0) {
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500605 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
606 "sending to node %d, port %d, "
607 "sk_err=%d/%d\n", dlm_our_nodeid(),
608 con->nodeid, dlm_config.ci_tcp_port,
609 sk->sk_err, sk->sk_err_soft);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500610 } else if (saddr.ss_family == AF_INET) {
611 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
612
613 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
614 "sending to node %d at %pI4, port %d, "
615 "sk_err=%d/%d\n", dlm_our_nodeid(),
616 con->nodeid, &sin4->sin_addr.s_addr,
617 dlm_config.ci_tcp_port, sk->sk_err,
618 sk->sk_err_soft);
619 } else {
620 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
621
622 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
623 "sending to node %d at %u.%u.%u.%u, "
624 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
625 con->nodeid, sin6->sin6_addr.s6_addr32[0],
626 sin6->sin6_addr.s6_addr32[1],
627 sin6->sin6_addr.s6_addr32[2],
628 sin6->sin6_addr.s6_addr32[3],
629 dlm_config.ci_tcp_port, sk->sk_err,
630 sk->sk_err_soft);
631 }
Alexander Aringba868d92021-05-21 15:08:37 -0400632
633 /* below sendcon only handling */
634 if (test_bit(CF_IS_OTHERCON, &con->flags))
635 con = con->sendcon;
636
637 switch (sk->sk_err) {
638 case ECONNREFUSED:
639 set_bit(CF_DELAY_CONNECT, &con->flags);
640 break;
641 default:
642 break;
643 }
644
645 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
646 queue_work(send_workqueue, &con->swork);
647
Bob Petersonb81171c2016-02-05 14:39:02 -0500648out:
649 read_unlock_bh(&sk->sk_callback_lock);
650 if (orig_report)
651 orig_report(sk);
652}
653
654/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000655static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500656{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000657 struct sock *sk = sock->sk;
658
659 listen_sock.sk_data_ready = sk->sk_data_ready;
660 listen_sock.sk_state_change = sk->sk_state_change;
661 listen_sock.sk_write_space = sk->sk_write_space;
662 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500663}
664
Bob Petersoncc661fc2017-09-12 08:55:23 +0000665static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500666{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000667 struct sock *sk = sock->sk;
668
Bob Petersonb81171c2016-02-05 14:39:02 -0500669 write_lock_bh(&sk->sk_callback_lock);
Bob Petersonb81171c2016-02-05 14:39:02 -0500670 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000671 sk->sk_data_ready = listen_sock.sk_data_ready;
672 sk->sk_state_change = listen_sock.sk_state_change;
673 sk->sk_write_space = listen_sock.sk_write_space;
674 sk->sk_error_report = listen_sock.sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500675 write_unlock_bh(&sk->sk_callback_lock);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500676}
677
Alexander Aringd11ccd42020-11-02 20:04:25 -0500678static void add_listen_sock(struct socket *sock, struct listen_connection *con)
679{
680 struct sock *sk = sock->sk;
681
682 write_lock_bh(&sk->sk_callback_lock);
683 save_listen_callbacks(sock);
684 con->sock = sock;
685
686 sk->sk_user_data = con;
687 sk->sk_allocation = GFP_NOFS;
688 /* Install a data_ready callback */
689 sk->sk_data_ready = lowcomms_listen_data_ready;
690 write_unlock_bh(&sk->sk_callback_lock);
691}
692
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100693/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000694static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100695{
Bob Petersonb81171c2016-02-05 14:39:02 -0500696 struct sock *sk = sock->sk;
697
698 write_lock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100699 con->sock = sock;
700
Bob Petersonb81171c2016-02-05 14:39:02 -0500701 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100702 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500703 sk->sk_data_ready = lowcomms_data_ready;
704 sk->sk_write_space = lowcomms_write_space;
705 sk->sk_state_change = lowcomms_state_change;
706 sk->sk_allocation = GFP_NOFS;
707 sk->sk_error_report = lowcomms_error_report;
708 write_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100709}
710
711/* Add the port number to an IPv6 or 4 sockaddr and return the address
712 length */
713static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
714 int *addr_len)
715{
716 saddr->ss_family = dlm_local_addr[0]->ss_family;
717 if (saddr->ss_family == AF_INET) {
718 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
719 in4_addr->sin_port = cpu_to_be16(port);
720 *addr_len = sizeof(struct sockaddr_in);
721 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
722 } else {
723 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
724 in6_addr->sin6_port = cpu_to_be16(port);
725 *addr_len = sizeof(struct sockaddr_in6);
726 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100727 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100728}
729
Alexander Aring706474f2021-05-21 15:08:48 -0400730static void dlm_page_release(struct kref *kref)
731{
732 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
733 ref);
734
735 __free_page(e->page);
736 kfree(e);
737}
738
739static void dlm_msg_release(struct kref *kref)
740{
741 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
742
743 kref_put(&msg->entry->ref, dlm_page_release);
744 kfree(msg);
745}
746
747static void free_entry(struct writequeue_entry *e)
748{
749 struct dlm_msg *msg, *tmp;
750
751 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
752 if (msg->orig_msg) {
753 msg->orig_msg->retransmit = false;
754 kref_put(&msg->orig_msg->ref, dlm_msg_release);
755 }
756
757 list_del(&msg->list);
758 kref_put(&msg->ref, dlm_msg_release);
759 }
760
761 list_del(&e->list);
762 atomic_dec(&e->con->writequeue_cnt);
763 kref_put(&e->ref, dlm_page_release);
764}
765
Alexander Aringd11ccd42020-11-02 20:04:25 -0500766static void dlm_close_sock(struct socket **sock)
767{
768 if (*sock) {
769 restore_callbacks(*sock);
770 sock_release(*sock);
771 *sock = NULL;
772 }
773}
774
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100775/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300776static void close_connection(struct connection *con, bool and_other,
777 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100778{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000779 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
Alexander Aring706474f2021-05-21 15:08:48 -0400780 struct writequeue_entry *e;
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000781
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000782 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300783 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000784 clear_bit(CF_WRITE_PENDING, &con->flags);
785 }
786 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300787 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000788 clear_bit(CF_READ_PENDING, &con->flags);
789 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100790
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300791 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500792 dlm_close_sock(&con->sock);
793
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100794 if (con->othercon && and_other) {
795 /* Will only re-enter once. */
Alexander Aringc6aa00e32021-05-21 15:08:38 -0400796 close_connection(con->othercon, false, tx, rx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100797 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100798
Alexander Aring706474f2021-05-21 15:08:48 -0400799 /* if we send a writequeue entry only a half way, we drop the
800 * whole entry because reconnection and that we not start of the
801 * middle of a msg which will confuse the other end.
802 *
803 * we can always drop messages because retransmits, but what we
804 * cannot allow is to transmit half messages which may be processed
805 * at the other side.
806 *
807 * our policy is to start on a clean state when disconnects, we don't
808 * know what's send/received on transport layer in this case.
809 */
810 spin_lock(&con->writequeue_lock);
811 if (!list_empty(&con->writequeue)) {
812 e = list_first_entry(&con->writequeue, struct writequeue_entry,
813 list);
814 if (e->dirty)
815 free_entry(e);
816 }
817 spin_unlock(&con->writequeue_lock);
818
Alexander Aring4798cbb2020-09-24 10:31:26 -0400819 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100820 con->retries = 0;
Alexander Aring052849b2021-07-16 16:22:37 -0400821 clear_bit(CF_APP_LIMITED, &con->flags);
Alexander Aring19633c72020-11-02 20:04:20 -0500822 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400823 clear_bit(CF_DELAY_CONNECT, &con->flags);
824 clear_bit(CF_RECONNECT, &con->flags);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400825 clear_bit(CF_EOF, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100826 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000827 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100828}
829
Alexander Aring055923b2020-07-27 09:13:38 -0400830static void shutdown_connection(struct connection *con)
831{
832 int ret;
833
Alexander Aringeec054b2021-03-01 17:05:19 -0500834 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400835
836 mutex_lock(&con->sock_mutex);
837 /* nothing to shutdown */
838 if (!con->sock) {
839 mutex_unlock(&con->sock_mutex);
840 return;
841 }
842
843 set_bit(CF_SHUTDOWN, &con->flags);
844 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
845 mutex_unlock(&con->sock_mutex);
846 if (ret) {
847 log_print("Connection %p failed to shutdown: %d will force close",
848 con, ret);
849 goto force_close;
850 } else {
851 ret = wait_event_timeout(con->shutdown_wait,
852 !test_bit(CF_SHUTDOWN, &con->flags),
853 DLM_SHUTDOWN_WAIT_TIMEOUT);
854 if (ret == 0) {
855 log_print("Connection %p shutdown timed out, will force close",
856 con);
857 goto force_close;
858 }
859 }
860
861 return;
862
863force_close:
864 clear_bit(CF_SHUTDOWN, &con->flags);
865 close_connection(con, false, true, true);
866}
867
868static void dlm_tcp_shutdown(struct connection *con)
869{
870 if (con->othercon)
871 shutdown_connection(con->othercon);
872 shutdown_connection(con);
873}
874
Alexander Aring4798cbb2020-09-24 10:31:26 -0400875static int con_realloc_receive_buf(struct connection *con, int newlen)
876{
877 unsigned char *newbuf;
878
879 newbuf = kmalloc(newlen, GFP_NOFS);
880 if (!newbuf)
881 return -ENOMEM;
882
883 /* copy any leftover from last receive */
884 if (con->rx_leftover)
885 memmove(newbuf, con->rx_buf, con->rx_leftover);
886
887 /* swap to new buffer space */
888 kfree(con->rx_buf);
889 con->rx_buflen = newlen;
890 con->rx_buf = newbuf;
891
892 return 0;
893}
894
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100895/* Data received from remote end */
896static int receive_from_sock(struct connection *con)
897{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400898 struct msghdr msg;
899 struct kvec iov;
900 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100901
902 mutex_lock(&con->sock_mutex);
903
904 if (con->sock == NULL) {
905 ret = -EAGAIN;
906 goto out_close;
907 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400908
Alexander Aring4798cbb2020-09-24 10:31:26 -0400909 /* realloc if we get new buffer size to read out */
910 buflen = dlm_config.ci_buffer_size;
911 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
912 ret = con_realloc_receive_buf(con, buflen);
913 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100914 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100915 }
916
Alexander Aring62699b32021-07-16 16:22:45 -0400917 for (;;) {
918 /* calculate new buffer parameter regarding last receive and
919 * possible leftover bytes
920 */
921 iov.iov_base = con->rx_buf + con->rx_leftover;
922 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100923
Alexander Aring62699b32021-07-16 16:22:45 -0400924 memset(&msg, 0, sizeof(msg));
925 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
926 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
927 msg.msg_flags);
928 if (ret == -EAGAIN)
929 break;
930 else if (ret <= 0)
931 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100932
Alexander Aring62699b32021-07-16 16:22:45 -0400933 /* new buflen according readed bytes and leftover from last receive */
934 buflen = ret + con->rx_leftover;
935 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
936 if (ret < 0)
937 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100938
Alexander Aring62699b32021-07-16 16:22:45 -0400939 /* calculate leftover bytes from process and put it into begin of
940 * the receive buffer, so next receive we have the full message
941 * at the start address of the receive buffer.
942 */
943 con->rx_leftover = buflen - ret;
944 if (con->rx_leftover) {
945 memmove(con->rx_buf, con->rx_buf + ret,
946 con->rx_leftover);
947 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100948 }
949
Alexander Aringb97f8522021-08-18 16:27:13 -0400950 dlm_midcomms_receive_done(con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100951 mutex_unlock(&con->sock_mutex);
952 return 0;
953
954out_resched:
955 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
956 queue_work(recv_workqueue, &con->rwork);
957 mutex_unlock(&con->sock_mutex);
958 return -EAGAIN;
959
960out_close:
Alexander Aringba868d92021-05-21 15:08:37 -0400961 if (ret == 0) {
Alexander Aringba868d92021-05-21 15:08:37 -0400962 log_print("connection %p got EOF from %d",
963 con, con->nodeid);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400964
Alexander Aringa66c0082021-07-16 16:22:40 -0400965 if (dlm_proto_ops->eof_condition &&
966 dlm_proto_ops->eof_condition(con)) {
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400967 set_bit(CF_EOF, &con->flags);
968 mutex_unlock(&con->sock_mutex);
969 } else {
970 mutex_unlock(&con->sock_mutex);
971 close_connection(con, false, true, false);
972
973 /* handling for tcp shutdown */
974 clear_bit(CF_SHUTDOWN, &con->flags);
975 wake_up(&con->shutdown_wait);
976 }
977
Alexander Aringba868d92021-05-21 15:08:37 -0400978 /* signal to breaking receive worker */
979 ret = -1;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400980 } else {
981 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100982 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100983 return ret;
984}
985
986/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500987static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100988{
989 int result;
990 struct sockaddr_storage peeraddr;
991 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400992 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100993 int nodeid;
994 struct connection *newcon;
995 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400996 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100997
Alexander Aringd11ccd42020-11-02 20:04:25 -0500998 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000999 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001000
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001001 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001002 if (result < 0)
1003 goto accept_err;
1004
1005 /* Get the connected socket's peer */
1006 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001007 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
1008 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001009 result = -ECONNABORTED;
1010 goto accept_err;
1011 }
1012
1013 /* Get the new node's NODEID */
1014 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -05001015 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001016 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -05001017 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001018 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
1019 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001020 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001021 return -1;
1022 }
1023
1024 log_print("got connection from %d", nodeid);
1025
1026 /* Check to see if we already have a connection to this node. This
1027 * could happen if the two nodes initiate a connection at roughly
1028 * the same time and the connections cross on the wire.
1029 * In this case we store the incoming one in "othercon"
1030 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001031 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -05001032 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001033 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001034 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001035 result = -ENOMEM;
1036 goto accept_err;
1037 }
Alexander Aringd11ccd42020-11-02 20:04:25 -05001038
Alexander Aringe125fbe2021-03-01 17:05:09 -05001039 sock_set_mark(newsock->sk, mark);
1040
Alexander Aringd11ccd42020-11-02 20:04:25 -05001041 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001042 if (newcon->sock) {
1043 struct connection *othercon = newcon->othercon;
1044
1045 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001046 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001047 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -05001048 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001049 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001050 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001051 result = -ENOMEM;
1052 goto accept_err;
1053 }
Alexander Aring4798cbb2020-09-24 10:31:26 -04001054
Alexander Aring6cde2102020-11-02 20:04:21 -05001055 result = dlm_con_init(othercon, nodeid);
1056 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -04001057 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +08001058 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001059 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -04001060 goto accept_err;
1061 }
1062
Alexander Aringe9a470a2021-03-01 17:05:11 -05001063 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -04001064 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -05001065 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -04001066 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001067 } else {
1068 /* close other sock con if we have something new */
1069 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +01001070 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001071
Alexander Aringe9a470a2021-03-01 17:05:11 -05001072 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001073 add_sock(newsock, othercon);
1074 addcon = othercon;
1075 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001076 }
1077 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -04001078 /* accept copies the sk after we've saved the callbacks, so we
1079 don't want to save them a second time or comm errors will
1080 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001081 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001082 addcon = newcon;
1083 }
1084
Alexander Aringb30a6242021-03-01 17:05:10 -05001085 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001086 mutex_unlock(&newcon->sock_mutex);
1087
1088 /*
1089 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001090 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001091 * to the read_sockets list
1092 */
1093 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1094 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001095
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001096 srcu_read_unlock(&connections_srcu, idx);
1097
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001098 return 0;
1099
1100accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001101 if (newsock)
1102 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001103
1104 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001105 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001106 return result;
1107}
1108
Mike Christie5d689872013-06-14 04:56:13 -05001109/*
1110 * writequeue_entry_complete - try to delete and free write queue entry
1111 * @e: write queue entry to try to delete
1112 * @completed: bytes completed
1113 *
1114 * writequeue_lock must be held.
1115 */
1116static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1117{
1118 e->offset += completed;
1119 e->len -= completed;
Alexander Aring706474f2021-05-21 15:08:48 -04001120 /* signal that page was half way transmitted */
1121 e->dirty = true;
Mike Christie5d689872013-06-14 04:56:13 -05001122
Alexander Aring8f2dc782021-05-21 15:08:42 -04001123 if (e->len == 0 && e->users == 0)
Mike Christie5d689872013-06-14 04:56:13 -05001124 free_entry(e);
Mike Christie5d689872013-06-14 04:56:13 -05001125}
1126
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001127/*
1128 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1129 */
Alexander Aring13004e82020-11-02 20:04:24 -05001130static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001131{
1132 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001133 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001134 int i, addr_len, result = 0;
1135
1136 for (i = 0; i < dlm_local_count; i++) {
1137 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1138 make_sockaddr(&localaddr, port, &addr_len);
1139
1140 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001141 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001142 else
Alexander Aring13004e82020-11-02 20:04:24 -05001143 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001144
1145 if (result < 0) {
1146 log_print("Can't bind to %d addr number %d, %d.\n",
1147 port, i + 1, result);
1148 break;
1149 }
1150 }
1151 return result;
1152}
1153
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001154/* Get local addresses */
1155static void init_local(void)
1156{
1157 struct sockaddr_storage sas, *addr;
1158 int i;
1159
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001160 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001161 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001162 if (dlm_our_addr(&sas, i))
1163 break;
1164
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301165 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001166 if (!addr)
1167 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001168 dlm_local_addr[dlm_local_count++] = addr;
1169 }
1170}
1171
Alexander Aring043697f2020-08-27 15:02:50 -04001172static void deinit_local(void)
1173{
1174 int i;
1175
1176 for (i = 0; i < dlm_local_count; i++)
1177 kfree(dlm_local_addr[i]);
1178}
1179
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001180static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1181 gfp_t allocation)
1182{
1183 struct writequeue_entry *entry;
1184
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001185 entry = kzalloc(sizeof(*entry), allocation);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001186 if (!entry)
1187 return NULL;
1188
Alexander Aringe1a7cbc2021-03-01 17:05:15 -05001189 entry->page = alloc_page(allocation | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001190 if (!entry->page) {
1191 kfree(entry);
1192 return NULL;
1193 }
1194
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001195 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001196 entry->users = 1;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001197 kref_init(&entry->ref);
1198 INIT_LIST_HEAD(&entry->msgs);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001199
1200 return entry;
1201}
1202
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001203static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
Alexander Aring8f2dc782021-05-21 15:08:42 -04001204 gfp_t allocation, char **ppc,
1205 void (*cb)(struct dlm_mhandle *mh),
1206 struct dlm_mhandle *mh)
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001207{
1208 struct writequeue_entry *e;
1209
1210 spin_lock(&con->writequeue_lock);
1211 if (!list_empty(&con->writequeue)) {
1212 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1213 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
Alexander Aring8f2dc782021-05-21 15:08:42 -04001214 kref_get(&e->ref);
1215
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001216 *ppc = page_address(e->page) + e->end;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001217 if (cb)
1218 cb(mh);
1219
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001220 e->end += len;
1221 e->users++;
1222 spin_unlock(&con->writequeue_lock);
1223
1224 return e;
1225 }
1226 }
1227 spin_unlock(&con->writequeue_lock);
1228
1229 e = new_writequeue_entry(con, allocation);
1230 if (!e)
1231 return NULL;
1232
Alexander Aring8f2dc782021-05-21 15:08:42 -04001233 kref_get(&e->ref);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001234 *ppc = page_address(e->page);
1235 e->end += len;
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001236 atomic_inc(&con->writequeue_cnt);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001237
1238 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001239 if (cb)
1240 cb(mh);
1241
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001242 list_add_tail(&e->list, &con->writequeue);
1243 spin_unlock(&con->writequeue_lock);
1244
1245 return e;
1246};
1247
Alexander Aring2874d1a2021-05-21 15:08:43 -04001248static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1249 gfp_t allocation, char **ppc,
1250 void (*cb)(struct dlm_mhandle *mh),
1251 struct dlm_mhandle *mh)
1252{
1253 struct writequeue_entry *e;
1254 struct dlm_msg *msg;
Alexander Aringc51b0222021-07-16 16:22:44 -04001255 bool sleepable;
Alexander Aring2874d1a2021-05-21 15:08:43 -04001256
1257 msg = kzalloc(sizeof(*msg), allocation);
1258 if (!msg)
1259 return NULL;
1260
Alexander Aringc51b0222021-07-16 16:22:44 -04001261 /* this mutex is being used as a wait to avoid multiple "fast"
1262 * new writequeue page list entry allocs in new_wq_entry in
1263 * normal operation which is sleepable context. Without it
1264 * we could end in multiple writequeue entries with one
1265 * dlm message because multiple callers were waiting at
1266 * the writequeue_lock in new_wq_entry().
1267 */
1268 sleepable = gfpflags_normal_context(allocation);
1269 if (sleepable)
1270 mutex_lock(&con->wq_alloc);
1271
Alexander Aring2874d1a2021-05-21 15:08:43 -04001272 kref_init(&msg->ref);
1273
1274 e = new_wq_entry(con, len, allocation, ppc, cb, mh);
1275 if (!e) {
Alexander Aringc51b0222021-07-16 16:22:44 -04001276 if (sleepable)
1277 mutex_unlock(&con->wq_alloc);
1278
Alexander Aring2874d1a2021-05-21 15:08:43 -04001279 kfree(msg);
1280 return NULL;
1281 }
1282
Alexander Aringc51b0222021-07-16 16:22:44 -04001283 if (sleepable)
1284 mutex_unlock(&con->wq_alloc);
1285
Alexander Aring2874d1a2021-05-21 15:08:43 -04001286 msg->ppc = *ppc;
1287 msg->len = len;
1288 msg->entry = e;
1289
1290 return msg;
1291}
1292
Alexander Aring8f2dc782021-05-21 15:08:42 -04001293struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
1294 char **ppc, void (*cb)(struct dlm_mhandle *mh),
1295 struct dlm_mhandle *mh)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001296{
1297 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001298 struct dlm_msg *msg;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001299 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001300
Alexander Aringd10a0b82021-06-02 09:45:20 -04001301 if (len > DLM_MAX_SOCKET_BUFSIZE ||
Alexander Aringc45674f2021-03-01 17:05:14 -05001302 len < sizeof(struct dlm_header)) {
Alexander Aringd10a0b82021-06-02 09:45:20 -04001303 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001304 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001305 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001306 return NULL;
1307 }
1308
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001309 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001310 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001311 if (!con) {
1312 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001313 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001314 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001315
Alexander Aring2874d1a2021-05-21 15:08:43 -04001316 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, mh);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001317 if (!msg) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001318 srcu_read_unlock(&connections_srcu, idx);
1319 return NULL;
1320 }
1321
Alexander Aring8f2dc782021-05-21 15:08:42 -04001322 /* we assume if successful commit must called */
1323 msg->idx = idx;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001324 return msg;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001325}
1326
Alexander Aring2874d1a2021-05-21 15:08:43 -04001327static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001328{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001329 struct writequeue_entry *e = msg->entry;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001330 struct connection *con = e->con;
1331 int users;
1332
1333 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001334 kref_get(&msg->ref);
1335 list_add(&msg->list, &e->msgs);
1336
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001337 users = --e->users;
1338 if (users)
1339 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001340
1341 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001342 spin_unlock(&con->writequeue_lock);
1343
Bob Peterson01da24d2017-09-12 08:55:14 +00001344 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001345 return;
1346
1347out:
1348 spin_unlock(&con->writequeue_lock);
1349 return;
1350}
1351
Alexander Aring2874d1a2021-05-21 15:08:43 -04001352void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1353{
1354 _dlm_lowcomms_commit_msg(msg);
1355 srcu_read_unlock(&connections_srcu, msg->idx);
1356}
1357
Alexander Aring8f2dc782021-05-21 15:08:42 -04001358void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1359{
1360 kref_put(&msg->ref, dlm_msg_release);
1361}
1362
Alexander Aring2874d1a2021-05-21 15:08:43 -04001363/* does not held connections_srcu, usage workqueue only */
1364int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1365{
1366 struct dlm_msg *msg_resend;
1367 char *ppc;
1368
1369 if (msg->retransmit)
1370 return 1;
1371
1372 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1373 GFP_ATOMIC, &ppc, NULL, NULL);
1374 if (!msg_resend)
1375 return -ENOMEM;
1376
1377 msg->retransmit = true;
1378 kref_get(&msg->ref);
1379 msg_resend->orig_msg = msg;
1380
1381 memcpy(ppc, msg->ppc, msg->len);
1382 _dlm_lowcomms_commit_msg(msg_resend);
1383 dlm_lowcomms_put_msg(msg_resend);
1384
1385 return 0;
1386}
1387
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001388/* Send a message */
1389static void send_to_sock(struct connection *con)
1390{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001391 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1392 struct writequeue_entry *e;
Alexander Aring66d59552021-07-16 16:22:39 -04001393 int len, offset, ret;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001394 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001395
1396 mutex_lock(&con->sock_mutex);
1397 if (con->sock == NULL)
1398 goto out_connect;
1399
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001400 spin_lock(&con->writequeue_lock);
1401 for (;;) {
Alexander Aring66d59552021-07-16 16:22:39 -04001402 e = con_next_wq(con);
1403 if (!e)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001404 break;
1405
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001406 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001407 len = e->len;
1408 offset = e->offset;
1409 BUG_ON(len == 0 && e->users == 0);
1410 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001411
Alexander Aring66d59552021-07-16 16:22:39 -04001412 ret = kernel_sendpage(con->sock, e->page, offset, len,
1413 msg_flags);
1414 if (ret == -EAGAIN || ret == 0) {
1415 if (ret == -EAGAIN &&
1416 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1417 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1418 /* Notify TCP that we're limited by the
1419 * application window size.
1420 */
1421 set_bit(SOCK_NOSPACE, &con->sock->flags);
1422 con->sock->sk->sk_write_pending++;
1423 }
1424 cond_resched();
1425 goto out;
1426 } else if (ret < 0)
1427 goto out;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001428
1429 /* Don't starve people filling buffers */
1430 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001431 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001432 count = 0;
1433 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001434
1435 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001436 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001437 }
1438 spin_unlock(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001439
1440 /* close if we got EOF */
1441 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1442 mutex_unlock(&con->sock_mutex);
1443 close_connection(con, false, false, true);
1444
1445 /* handling for tcp shutdown */
1446 clear_bit(CF_SHUTDOWN, &con->flags);
1447 wake_up(&con->shutdown_wait);
1448 } else {
1449 mutex_unlock(&con->sock_mutex);
1450 }
1451
1452 return;
1453
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001454out:
1455 mutex_unlock(&con->sock_mutex);
1456 return;
1457
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001458out_connect:
1459 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001460 queue_work(send_workqueue, &con->swork);
1461 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001462}
1463
1464static void clean_one_writequeue(struct connection *con)
1465{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001466 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001467
1468 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001469 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001470 free_entry(e);
1471 }
1472 spin_unlock(&con->writequeue_lock);
1473}
1474
1475/* Called from recovery when it knows that a node has
1476 left the cluster */
1477int dlm_lowcomms_close(int nodeid)
1478{
1479 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001480 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001481 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001482
1483 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001484 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001485 con = nodeid2con(nodeid, 0);
1486 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001487 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001488 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001489 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001490 if (con->othercon)
1491 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001492 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001493 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001494
1495 spin_lock(&dlm_node_addrs_spin);
1496 na = find_node_addr(nodeid);
1497 if (na) {
1498 list_del(&na->list);
1499 while (na->addr_count--)
1500 kfree(na->addr[na->addr_count]);
1501 kfree(na);
1502 }
1503 spin_unlock(&dlm_node_addrs_spin);
1504
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001505 return 0;
1506}
1507
1508/* Receive workqueue function */
1509static void process_recv_sockets(struct work_struct *work)
1510{
1511 struct connection *con = container_of(work, struct connection, rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001512
1513 clear_bit(CF_READ_PENDING, &con->flags);
Alexander Aring62699b32021-07-16 16:22:45 -04001514 receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001515}
1516
Alexander Aringd11ccd42020-11-02 20:04:25 -05001517static void process_listen_recv_socket(struct work_struct *work)
1518{
1519 accept_from_sock(&listen_con);
1520}
1521
Alexander Aring8728a452021-07-16 16:22:43 -04001522static void dlm_connect(struct connection *con)
1523{
1524 struct sockaddr_storage addr;
1525 int result, addr_len;
1526 struct socket *sock;
1527 unsigned int mark;
1528
1529 /* Some odd races can cause double-connects, ignore them */
1530 if (con->retries++ > MAX_CONNECT_RETRIES)
1531 return;
1532
1533 if (con->sock) {
1534 log_print("node %d already connected.", con->nodeid);
1535 return;
1536 }
1537
1538 memset(&addr, 0, sizeof(addr));
1539 result = nodeid_to_addr(con->nodeid, &addr, NULL,
1540 dlm_proto_ops->try_new_addr, &mark);
1541 if (result < 0) {
1542 log_print("no address for nodeid %d", con->nodeid);
1543 return;
1544 }
1545
1546 /* Create a socket to communicate with */
1547 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1548 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1549 if (result < 0)
1550 goto socket_err;
1551
1552 sock_set_mark(sock->sk, mark);
1553 dlm_proto_ops->sockopts(sock);
1554
1555 add_sock(sock, con);
1556
1557 result = dlm_proto_ops->bind(sock);
1558 if (result < 0)
1559 goto add_sock_err;
1560
1561 log_print_ratelimited("connecting to %d", con->nodeid);
1562 make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1563 result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr,
1564 addr_len);
1565 if (result < 0)
1566 goto add_sock_err;
1567
1568 return;
1569
1570add_sock_err:
1571 dlm_close_sock(&con->sock);
1572
1573socket_err:
1574 /*
1575 * Some errors are fatal and this list might need adjusting. For other
1576 * errors we try again until the max number of retries is reached.
1577 */
1578 if (result != -EHOSTUNREACH &&
1579 result != -ENETUNREACH &&
1580 result != -ENETDOWN &&
1581 result != -EINVAL &&
1582 result != -EPROTONOSUPPORT) {
1583 log_print("connect %d try %d error %d", con->nodeid,
1584 con->retries, result);
1585 msleep(1000);
1586 lowcomms_connect_sock(con);
1587 }
1588}
1589
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001590/* Send workqueue function */
1591static void process_send_sockets(struct work_struct *work)
1592{
1593 struct connection *con = container_of(work, struct connection, swork);
1594
Alexander Aring7443bc92021-05-21 15:08:36 -04001595 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1596
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001597 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001598
Alexander Aring489d8e52021-05-21 15:08:46 -04001599 if (test_and_clear_bit(CF_RECONNECT, &con->flags)) {
Alexander Aringba868d92021-05-21 15:08:37 -04001600 close_connection(con, false, false, true);
Alexander Aring489d8e52021-05-21 15:08:46 -04001601 dlm_midcomms_unack_msg_resend(con->nodeid);
1602 }
Alexander Aringba868d92021-05-21 15:08:37 -04001603
Alexander Aring8728a452021-07-16 16:22:43 -04001604 if (con->sock == NULL) {
Alexander Aringba868d92021-05-21 15:08:37 -04001605 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1606 msleep(1000);
Alexander Aring8728a452021-07-16 16:22:43 -04001607
1608 mutex_lock(&con->sock_mutex);
1609 dlm_connect(con);
1610 mutex_unlock(&con->sock_mutex);
Alexander Aringba868d92021-05-21 15:08:37 -04001611 }
Alexander Aring8728a452021-07-16 16:22:43 -04001612
Bob Peterson01da24d2017-09-12 08:55:14 +00001613 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001614 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001615}
1616
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001617static void work_stop(void)
1618{
Alexander Aringfcef0e62021-06-02 09:45:15 -04001619 if (recv_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001620 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001621 recv_workqueue = NULL;
1622 }
1623
1624 if (send_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001625 destroy_workqueue(send_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001626 send_workqueue = NULL;
1627 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001628}
1629
1630static int work_start(void)
1631{
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001632 recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001633 if (!recv_workqueue) {
1634 log_print("can't start dlm_recv");
1635 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001636 }
1637
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001638 send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001639 if (!send_workqueue) {
1640 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001641 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001642 recv_workqueue = NULL;
Namhyung Kimb9d41052010-12-13 13:42:24 -06001643 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001644 }
1645
1646 return 0;
1647}
1648
Alexander Aring9d232462021-03-01 17:05:20 -05001649static void shutdown_conn(struct connection *con)
1650{
Alexander Aringa66c0082021-07-16 16:22:40 -04001651 if (dlm_proto_ops->shutdown_action)
1652 dlm_proto_ops->shutdown_action(con);
Alexander Aring9d232462021-03-01 17:05:20 -05001653}
1654
1655void dlm_lowcomms_shutdown(void)
1656{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001657 int idx;
1658
Alexander Aring9d232462021-03-01 17:05:20 -05001659 /* Set all the flags to prevent any
1660 * socket activity.
1661 */
1662 dlm_allow_conn = 0;
1663
1664 if (recv_workqueue)
1665 flush_workqueue(recv_workqueue);
1666 if (send_workqueue)
1667 flush_workqueue(send_workqueue);
1668
1669 dlm_close_sock(&listen_con.sock);
1670
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001671 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001672 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001673 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001674}
1675
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001676static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001677{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001678 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001679 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001680 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001681 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001682 if (con->sock && con->sock->sk) {
1683 write_lock_bh(&con->sock->sk->sk_callback_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001684 con->sock->sk->sk_user_data = NULL;
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001685 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1686 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001687 if (con->othercon && and_other)
1688 _stop_conn(con->othercon, false);
1689 mutex_unlock(&con->sock_mutex);
1690}
1691
1692static void stop_conn(struct connection *con)
1693{
1694 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001695}
1696
Alexander Aring4798cbb2020-09-24 10:31:26 -04001697static void connection_release(struct rcu_head *rcu)
1698{
1699 struct connection *con = container_of(rcu, struct connection, rcu);
1700
1701 kfree(con->rx_buf);
1702 kfree(con);
1703}
1704
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001705static void free_conn(struct connection *con)
1706{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001707 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001708 spin_lock(&connections_lock);
1709 hlist_del_rcu(&con->list);
1710 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001711 if (con->othercon) {
1712 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001713 call_srcu(&connections_srcu, &con->othercon->rcu,
1714 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001715 }
Alexander Aring0de98432020-08-27 15:02:52 -04001716 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001717 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001718}
1719
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001720static void work_flush(void)
1721{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001722 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001723 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001724 struct connection *con;
1725
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001726 do {
1727 ok = 1;
1728 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001729 if (recv_workqueue)
1730 flush_workqueue(recv_workqueue);
1731 if (send_workqueue)
1732 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001733 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001734 hlist_for_each_entry_rcu(con, &connection_hash[i],
1735 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001736 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001737 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1738 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001739 ok &= test_bit(CF_READ_PENDING,
1740 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001741 ok &= test_bit(CF_WRITE_PENDING,
1742 &con->othercon->flags);
1743 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001744 }
1745 }
1746 } while (!ok);
1747}
1748
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001749void dlm_lowcomms_stop(void)
1750{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001751 int idx;
1752
1753 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001754 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001755 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001756 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001757 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001758 deinit_local();
Alexander Aringa66c0082021-07-16 16:22:40 -04001759
1760 dlm_proto_ops = NULL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001761}
1762
Alexander Aring2dc6b112021-07-16 16:22:41 -04001763static int dlm_listen_for_all(void)
1764{
1765 struct socket *sock;
1766 int result;
1767
1768 log_print("Using %s for communications",
1769 dlm_proto_ops->name);
1770
Alexander Aring90d21fc2021-07-16 16:22:42 -04001771 result = dlm_proto_ops->listen_validate();
1772 if (result < 0)
1773 return result;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001774
1775 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1776 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1777 if (result < 0) {
1778 log_print("Can't create comms socket, check SCTP is loaded");
1779 goto out;
1780 }
1781
1782 sock_set_mark(sock->sk, dlm_config.ci_mark);
1783 dlm_proto_ops->listen_sockopts(sock);
1784
1785 result = dlm_proto_ops->listen_bind(sock);
1786 if (result < 0)
1787 goto out;
1788
1789 save_listen_callbacks(sock);
1790 add_listen_sock(sock, &listen_con);
1791
1792 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1793 result = sock->ops->listen(sock, 5);
1794 if (result < 0) {
1795 dlm_close_sock(&listen_con.sock);
1796 goto out;
1797 }
1798
1799 return 0;
1800
1801out:
1802 sock_release(sock);
1803 return result;
1804}
1805
Alexander Aring8728a452021-07-16 16:22:43 -04001806static int dlm_tcp_bind(struct socket *sock)
1807{
1808 struct sockaddr_storage src_addr;
1809 int result, addr_len;
1810
1811 /* Bind to our cluster-known address connecting to avoid
1812 * routing problems.
1813 */
1814 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1815 make_sockaddr(&src_addr, 0, &addr_len);
1816
1817 result = sock->ops->bind(sock, (struct sockaddr *)&src_addr,
1818 addr_len);
1819 if (result < 0) {
1820 /* This *may* not indicate a critical error */
1821 log_print("could not bind for connect: %d", result);
1822 }
1823
1824 return 0;
1825}
1826
1827static int dlm_tcp_connect(struct connection *con, struct socket *sock,
1828 struct sockaddr *addr, int addr_len)
1829{
1830 int ret;
1831
1832 ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK);
1833 switch (ret) {
1834 case -EINPROGRESS:
1835 fallthrough;
1836 case 0:
1837 return 0;
1838 }
1839
1840 return ret;
1841}
1842
Alexander Aring2dc6b112021-07-16 16:22:41 -04001843static int dlm_tcp_listen_validate(void)
1844{
1845 /* We don't support multi-homed hosts */
1846 if (dlm_local_count > 1) {
1847 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1848 return -EINVAL;
1849 }
1850
1851 return 0;
1852}
1853
1854static void dlm_tcp_sockopts(struct socket *sock)
1855{
1856 /* Turn off Nagle's algorithm */
1857 tcp_sock_set_nodelay(sock->sk);
1858}
1859
1860static void dlm_tcp_listen_sockopts(struct socket *sock)
1861{
1862 dlm_tcp_sockopts(sock);
1863 sock_set_reuseaddr(sock->sk);
1864}
1865
1866static int dlm_tcp_listen_bind(struct socket *sock)
1867{
1868 int addr_len;
1869
1870 /* Bind to our port */
1871 make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1872 return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0],
1873 addr_len);
1874}
1875
Alexander Aringa66c0082021-07-16 16:22:40 -04001876static const struct dlm_proto_ops dlm_tcp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001877 .name = "TCP",
1878 .proto = IPPROTO_TCP,
Alexander Aring8728a452021-07-16 16:22:43 -04001879 .connect = dlm_tcp_connect,
1880 .sockopts = dlm_tcp_sockopts,
1881 .bind = dlm_tcp_bind,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001882 .listen_validate = dlm_tcp_listen_validate,
1883 .listen_sockopts = dlm_tcp_listen_sockopts,
1884 .listen_bind = dlm_tcp_listen_bind,
Alexander Aringa66c0082021-07-16 16:22:40 -04001885 .shutdown_action = dlm_tcp_shutdown,
1886 .eof_condition = tcp_eof_condition,
1887};
1888
Alexander Aring8728a452021-07-16 16:22:43 -04001889static int dlm_sctp_bind(struct socket *sock)
1890{
1891 return sctp_bind_addrs(sock, 0);
1892}
1893
1894static int dlm_sctp_connect(struct connection *con, struct socket *sock,
1895 struct sockaddr *addr, int addr_len)
1896{
1897 int ret;
1898
1899 /*
1900 * Make sock->ops->connect() function return in specified time,
1901 * since O_NONBLOCK argument in connect() function does not work here,
1902 * then, we should restore the default value of this attribute.
1903 */
1904 sock_set_sndtimeo(sock->sk, 5);
1905 ret = sock->ops->connect(sock, addr, addr_len, 0);
1906 sock_set_sndtimeo(sock->sk, 0);
1907 if (ret < 0)
1908 return ret;
1909
1910 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1911 log_print("successful connected to node %d", con->nodeid);
1912
1913 return 0;
1914}
1915
Alexander Aring90d21fc2021-07-16 16:22:42 -04001916static int dlm_sctp_listen_validate(void)
1917{
1918 if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1919 log_print("SCTP is not enabled by this kernel");
1920 return -EOPNOTSUPP;
1921 }
1922
1923 request_module("sctp");
1924 return 0;
1925}
1926
Alexander Aring2dc6b112021-07-16 16:22:41 -04001927static int dlm_sctp_bind_listen(struct socket *sock)
1928{
1929 return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
1930}
1931
1932static void dlm_sctp_sockopts(struct socket *sock)
1933{
1934 /* Turn off Nagle's algorithm */
1935 sctp_sock_set_nodelay(sock->sk);
1936 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1937}
1938
Alexander Aringa66c0082021-07-16 16:22:40 -04001939static const struct dlm_proto_ops dlm_sctp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001940 .name = "SCTP",
1941 .proto = IPPROTO_SCTP,
Alexander Aring8728a452021-07-16 16:22:43 -04001942 .try_new_addr = true,
1943 .connect = dlm_sctp_connect,
1944 .sockopts = dlm_sctp_sockopts,
1945 .bind = dlm_sctp_bind,
Alexander Aring90d21fc2021-07-16 16:22:42 -04001946 .listen_validate = dlm_sctp_listen_validate,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001947 .listen_sockopts = dlm_sctp_sockopts,
1948 .listen_bind = dlm_sctp_bind_listen,
Alexander Aringa66c0082021-07-16 16:22:40 -04001949};
1950
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001951int dlm_lowcomms_start(void)
1952{
1953 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001954 int i;
1955
1956 for (i = 0; i < CONN_HASH_SIZE; i++)
1957 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001958
1959 init_local();
1960 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001961 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001962 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001963 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001964 }
1965
Alexander Aringd11ccd42020-11-02 20:04:25 -05001966 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1967
David Teigland513ef592012-03-30 11:46:08 -05001968 error = work_start();
1969 if (error)
Alexander Aringfcef0e62021-06-02 09:45:15 -04001970 goto fail_local;
David Teigland513ef592012-03-30 11:46:08 -05001971
1972 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001973
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001974 /* Start listening */
Alexander Aringac7d5d02021-06-02 09:45:19 -04001975 switch (dlm_config.ci_protocol) {
1976 case DLM_PROTO_TCP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001977 dlm_proto_ops = &dlm_tcp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001978 break;
1979 case DLM_PROTO_SCTP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001980 dlm_proto_ops = &dlm_sctp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001981 break;
1982 default:
1983 log_print("Invalid protocol identifier %d set",
1984 dlm_config.ci_protocol);
1985 error = -EINVAL;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001986 goto fail_proto_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001987 }
Alexander Aring2dc6b112021-07-16 16:22:41 -04001988
1989 error = dlm_listen_for_all();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001990 if (error)
Alexander Aring2dc6b112021-07-16 16:22:41 -04001991 goto fail_listen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001992
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001993 return 0;
1994
Alexander Aring2dc6b112021-07-16 16:22:41 -04001995fail_listen:
1996 dlm_proto_ops = NULL;
1997fail_proto_ops:
David Teigland513ef592012-03-30 11:46:08 -05001998 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001999 dlm_close_sock(&listen_con.sock);
Alexander Aringfcef0e62021-06-02 09:45:15 -04002000 work_stop();
2001fail_local:
2002 deinit_local();
David Teigland513ef592012-03-30 11:46:08 -05002003fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002004 return error;
2005}
David Teigland36b71a82012-07-26 12:44:30 -05002006
2007void dlm_lowcomms_exit(void)
2008{
2009 struct dlm_node_addr *na, *safe;
2010
2011 spin_lock(&dlm_node_addrs_spin);
2012 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
2013 list_del(&na->list);
2014 while (na->addr_count--)
2015 kfree(na->addr[na->addr_count]);
2016 kfree(na);
2017 }
2018 spin_unlock(&dlm_node_addrs_spin);
2019}