blob: e284d696c1fdc852f8eb92395ad03f1af31af844 [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
Alexander Aring92732372021-11-02 15:17:16 -040056#include <trace/events/dlm.h>
57
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010058#include "dlm_internal.h"
59#include "lowcomms.h"
60#include "midcomms.h"
Alexander Aring3af23262021-11-30 14:47:19 -050061#include "memory.h"
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010062#include "config.h"
63
64#define NEEDED_RMEM (4*1024*1024)
65
Bob Petersonf92c8dd2010-11-12 11:15:20 -060066/* Number of messages to send before rescheduling */
67#define MAX_SEND_MSG_COUNT 25
Alexander Aring055923b2020-07-27 09:13:38 -040068#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
Bob Petersonf92c8dd2010-11-12 11:15:20 -060069
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010070struct connection {
71 struct socket *sock; /* NULL if not connected */
72 uint32_t nodeid; /* So we know who we are in the list */
73 struct mutex sock_mutex;
74 unsigned long flags;
75#define CF_READ_PENDING 1
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +000076#define CF_WRITE_PENDING 2
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010077#define CF_INIT_PENDING 4
78#define CF_IS_OTHERCON 5
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -050079#define CF_CLOSE 6
David Millerb36930d2010-11-10 21:56:39 -080080#define CF_APP_LIMITED 7
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +000081#define CF_CLOSING 8
Alexander Aring055923b2020-07-27 09:13:38 -040082#define CF_SHUTDOWN 9
Alexander Aring19633c72020-11-02 20:04:20 -050083#define CF_CONNECTED 10
Alexander Aringba868d92021-05-21 15:08:37 -040084#define CF_RECONNECT 11
85#define CF_DELAY_CONNECT 12
Alexander Aring8aa31cb2021-05-21 15:08:39 -040086#define CF_EOF 13
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010087 struct list_head writequeue; /* List of outgoing writequeue_entries */
88 spinlock_t writequeue_lock;
Alexander Aring8aa31cb2021-05-21 15:08:39 -040089 atomic_t writequeue_cnt;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010090 int retries;
91#define MAX_CONNECT_RETRIES 3
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060092 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010093 struct connection *othercon;
Alexander Aringba868d92021-05-21 15:08:37 -040094 struct connection *sendcon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010095 struct work_struct rwork; /* Receive workqueue */
96 struct work_struct swork; /* Send workqueue */
Alexander Aring055923b2020-07-27 09:13:38 -040097 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
Alexander Aring4798cbb2020-09-24 10:31:26 -040098 unsigned char *rx_buf;
99 int rx_buflen;
100 int rx_leftover;
Alexander Aringa47666eb2020-08-27 15:02:49 -0400101 struct rcu_head rcu;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100102};
103#define sock2con(x) ((struct connection *)(x)->sk_user_data)
104
Alexander Aringd11ccd42020-11-02 20:04:25 -0500105struct listen_connection {
106 struct socket *sock;
107 struct work_struct rwork;
108};
109
Alexander Aringf0747ebf2021-03-01 17:05:16 -0500110#define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
111#define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
112
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100113/* An entry waiting to be sent */
114struct writequeue_entry {
115 struct list_head list;
116 struct page *page;
117 int offset;
118 int len;
119 int end;
120 int users;
Alexander Aring706474f2021-05-21 15:08:48 -0400121 bool dirty;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100122 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400123 struct list_head msgs;
124 struct kref ref;
125};
126
127struct dlm_msg {
128 struct writequeue_entry *entry;
Alexander Aring2874d1a2021-05-21 15:08:43 -0400129 struct dlm_msg *orig_msg;
130 bool retransmit;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400131 void *ppc;
132 int len;
133 int idx; /* new()/commit() idx exchange */
134
135 struct list_head list;
136 struct kref ref;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100137};
138
David Teigland36b71a82012-07-26 12:44:30 -0500139struct dlm_node_addr {
140 struct list_head list;
141 int nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500142 int mark;
David Teigland36b71a82012-07-26 12:44:30 -0500143 int addr_count;
Mike Christie98e1b602013-06-14 04:56:12 -0500144 int curr_addr_index;
David Teigland36b71a82012-07-26 12:44:30 -0500145 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
146};
147
Alexander Aringa66c0082021-07-16 16:22:40 -0400148struct dlm_proto_ops {
Alexander Aring8728a452021-07-16 16:22:43 -0400149 bool try_new_addr;
Alexander Aring2dc6b112021-07-16 16:22:41 -0400150 const char *name;
151 int proto;
152
Alexander Aring8728a452021-07-16 16:22:43 -0400153 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 Aring2dc6b112021-07-16 16:22:41 -0400157 int (*listen_validate)(void);
158 void (*listen_sockopts)(struct socket *sock);
159 int (*listen_bind)(struct socket *sock);
Alexander Aringa66c0082021-07-16 16:22:40 -0400160 /* 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 Petersoncc661fc2017-09-12 08:55:23 +0000166static 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 Teigland36b71a82012-07-26 12:44:30 -0500173static LIST_HEAD(dlm_node_addrs);
174static DEFINE_SPINLOCK(dlm_node_addrs_spin);
175
Alexander Aringd11ccd42020-11-02 20:04:25 -0500176static struct listen_connection listen_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100177static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
178static int dlm_local_count;
Alexander Aring51746162021-03-01 17:05:13 -0500179int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100180
181/* Work queues */
182static struct workqueue_struct *recv_workqueue;
183static struct workqueue_struct *send_workqueue;
184
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600185static struct hlist_head connection_hash[CONN_HASH_SIZE];
Alexander Aringa47666eb2020-08-27 15:02:49 -0400186static DEFINE_SPINLOCK(connections_lock);
187DEFINE_STATIC_SRCU(connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100188
Alexander Aringa66c0082021-07-16 16:22:40 -0400189static const struct dlm_proto_ops *dlm_proto_ops;
190
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100191static void process_recv_sockets(struct work_struct *work);
192static void process_send_sockets(struct work_struct *work);
193
Alexander Aring3af23262021-11-30 14:47:19 -0500194static void writequeue_entry_ctor(void *data)
195{
196 struct writequeue_entry *entry = data;
197
198 INIT_LIST_HEAD(&entry->msgs);
199}
200
201struct kmem_cache *dlm_lowcomms_writequeue_cache_create(void)
202{
203 return kmem_cache_create("dlm_writequeue", sizeof(struct writequeue_entry),
204 0, 0, writequeue_entry_ctor);
205}
206
Alexander Aringe4dc81e2021-11-30 14:47:20 -0500207struct kmem_cache *dlm_lowcomms_msg_cache_create(void)
208{
209 return kmem_cache_create("dlm_msg", sizeof(struct dlm_msg), 0, 0, NULL);
210}
211
Alexander Aring66d59552021-07-16 16:22:39 -0400212/* need to held writequeue_lock */
213static struct writequeue_entry *con_next_wq(struct connection *con)
214{
215 struct writequeue_entry *e;
216
217 if (list_empty(&con->writequeue))
218 return NULL;
219
220 e = list_first_entry(&con->writequeue, struct writequeue_entry,
221 list);
Alexander Aringbcbfea42021-11-30 14:47:15 -0500222 /* if len is zero nothing is to send, if there are users filling
223 * buffers we wait until the users are done so we can send more.
224 */
225 if (e->users || e->len == 0)
Alexander Aring66d59552021-07-16 16:22:39 -0400226 return NULL;
227
228 return e;
229}
230
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400231static struct connection *__find_con(int nodeid, int r)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600232{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600233 struct connection *con;
234
Alexander Aringa47666eb2020-08-27 15:02:49 -0400235 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400236 if (con->nodeid == nodeid)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600237 return con;
238 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400239
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600240 return NULL;
241}
242
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400243static bool tcp_eof_condition(struct connection *con)
244{
245 return atomic_read(&con->writequeue_cnt);
246}
247
Alexander Aring6cde2102020-11-02 20:04:21 -0500248static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100249{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400250 con->rx_buflen = dlm_config.ci_buffer_size;
251 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500252 if (!con->rx_buf)
253 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400254
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100255 con->nodeid = nodeid;
256 mutex_init(&con->sock_mutex);
257 INIT_LIST_HEAD(&con->writequeue);
258 spin_lock_init(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400259 atomic_set(&con->writequeue_cnt, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100260 INIT_WORK(&con->swork, process_send_sockets);
261 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400262 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100263
Alexander Aring6cde2102020-11-02 20:04:21 -0500264 return 0;
265}
266
267/*
268 * If 'allocation' is zero then we don't attempt to create a new
269 * connection structure for this node.
270 */
271static struct connection *nodeid2con(int nodeid, gfp_t alloc)
272{
273 struct connection *con, *tmp;
274 int r, ret;
275
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400276 r = nodeid_hash(nodeid);
277 con = __find_con(nodeid, r);
Alexander Aring6cde2102020-11-02 20:04:21 -0500278 if (con || !alloc)
279 return con;
280
281 con = kzalloc(sizeof(*con), alloc);
282 if (!con)
283 return NULL;
284
285 ret = dlm_con_init(con, nodeid);
286 if (ret) {
287 kfree(con);
288 return NULL;
289 }
290
Alexander Aringa47666eb2020-08-27 15:02:49 -0400291 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400292 /* Because multiple workqueues/threads calls this function it can
293 * race on multiple cpu's. Instead of locking hot path __find_con()
294 * we just check in rare cases of recently added nodes again
295 * under protection of connections_lock. If this is the case we
296 * abort our connection creation and return the existing connection.
297 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400298 tmp = __find_con(nodeid, r);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400299 if (tmp) {
300 spin_unlock(&connections_lock);
301 kfree(con->rx_buf);
302 kfree(con);
303 return tmp;
304 }
305
Alexander Aringa47666eb2020-08-27 15:02:49 -0400306 hlist_add_head_rcu(&con->list, &connection_hash[r]);
307 spin_unlock(&connections_lock);
308
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100309 return con;
310}
311
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600312/* Loop round all connections */
313static void foreach_conn(void (*conn_func)(struct connection *c))
314{
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400315 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600316 struct connection *con;
317
318 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400319 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600320 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600321 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100322}
323
David Teigland36b71a82012-07-26 12:44:30 -0500324static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100325{
David Teigland36b71a82012-07-26 12:44:30 -0500326 struct dlm_node_addr *na;
327
328 list_for_each_entry(na, &dlm_node_addrs, list) {
329 if (na->nodeid == nodeid)
330 return na;
331 }
332 return NULL;
333}
334
Alexander Aring40c6b832020-11-02 20:04:27 -0500335static int addr_compare(const struct sockaddr_storage *x,
336 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500337{
338 switch (x->ss_family) {
339 case AF_INET: {
340 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
341 struct sockaddr_in *siny = (struct sockaddr_in *)y;
342 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
343 return 0;
344 if (sinx->sin_port != siny->sin_port)
345 return 0;
346 break;
347 }
348 case AF_INET6: {
349 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
350 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
351 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
352 return 0;
353 if (sinx->sin6_port != siny->sin6_port)
354 return 0;
355 break;
356 }
357 default:
358 return 0;
359 }
360 return 1;
361}
362
363static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Alexander Aringe125fbe2021-03-01 17:05:09 -0500364 struct sockaddr *sa_out, bool try_new_addr,
365 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500366{
367 struct sockaddr_storage sas;
368 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100369
370 if (!dlm_local_count)
371 return -1;
372
David Teigland36b71a82012-07-26 12:44:30 -0500373 spin_lock(&dlm_node_addrs_spin);
374 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500375 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300376 memcpy(&sas, na->addr[na->curr_addr_index],
377 sizeof(struct sockaddr_storage));
378
Mike Christie98e1b602013-06-14 04:56:12 -0500379 if (try_new_addr) {
380 na->curr_addr_index++;
381 if (na->curr_addr_index == na->addr_count)
382 na->curr_addr_index = 0;
383 }
Mike Christie98e1b602013-06-14 04:56:12 -0500384 }
David Teigland36b71a82012-07-26 12:44:30 -0500385 spin_unlock(&dlm_node_addrs_spin);
386
387 if (!na)
388 return -EEXIST;
389
390 if (!na->addr_count)
391 return -ENOENT;
392
Alexander Aringe125fbe2021-03-01 17:05:09 -0500393 *mark = na->mark;
394
David Teigland36b71a82012-07-26 12:44:30 -0500395 if (sas_out)
396 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
397
398 if (!sa_out)
399 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100400
401 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500402 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
403 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100404 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
405 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500406 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
407 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000408 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100409 }
410
411 return 0;
412}
413
Alexander Aringe125fbe2021-03-01 17:05:09 -0500414static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
415 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500416{
417 struct dlm_node_addr *na;
418 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500419 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500420
421 spin_lock(&dlm_node_addrs_spin);
422 list_for_each_entry(na, &dlm_node_addrs, list) {
423 if (!na->addr_count)
424 continue;
425
Mike Christie98e1b602013-06-14 04:56:12 -0500426 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
427 if (addr_compare(na->addr[addr_i], addr)) {
428 *nodeid = na->nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500429 *mark = na->mark;
Mike Christie98e1b602013-06-14 04:56:12 -0500430 rv = 0;
431 goto unlock;
432 }
433 }
David Teigland36b71a82012-07-26 12:44:30 -0500434 }
Mike Christie98e1b602013-06-14 04:56:12 -0500435unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500436 spin_unlock(&dlm_node_addrs_spin);
437 return rv;
438}
439
Alexander Aring4f19d072020-11-02 20:04:28 -0500440/* caller need to held dlm_node_addrs_spin lock */
441static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
442 const struct sockaddr_storage *addr)
443{
444 int i;
445
446 for (i = 0; i < na->addr_count; i++) {
447 if (addr_compare(na->addr[i], addr))
448 return true;
449 }
450
451 return false;
452}
453
David Teigland36b71a82012-07-26 12:44:30 -0500454int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
455{
456 struct sockaddr_storage *new_addr;
457 struct dlm_node_addr *new_node, *na;
Alexander Aring4f19d072020-11-02 20:04:28 -0500458 bool ret;
David Teigland36b71a82012-07-26 12:44:30 -0500459
460 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
461 if (!new_node)
462 return -ENOMEM;
463
464 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
465 if (!new_addr) {
466 kfree(new_node);
467 return -ENOMEM;
468 }
469
470 memcpy(new_addr, addr, len);
471
472 spin_lock(&dlm_node_addrs_spin);
473 na = find_node_addr(nodeid);
474 if (!na) {
475 new_node->nodeid = nodeid;
476 new_node->addr[0] = new_addr;
477 new_node->addr_count = 1;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500478 new_node->mark = dlm_config.ci_mark;
David Teigland36b71a82012-07-26 12:44:30 -0500479 list_add(&new_node->list, &dlm_node_addrs);
480 spin_unlock(&dlm_node_addrs_spin);
481 return 0;
482 }
483
Alexander Aring4f19d072020-11-02 20:04:28 -0500484 ret = dlm_lowcomms_na_has_addr(na, addr);
485 if (ret) {
486 spin_unlock(&dlm_node_addrs_spin);
487 kfree(new_addr);
488 kfree(new_node);
489 return -EEXIST;
490 }
491
David Teigland36b71a82012-07-26 12:44:30 -0500492 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
493 spin_unlock(&dlm_node_addrs_spin);
494 kfree(new_addr);
495 kfree(new_node);
496 return -ENOSPC;
497 }
498
499 na->addr[na->addr_count++] = new_addr;
500 spin_unlock(&dlm_node_addrs_spin);
501 kfree(new_node);
502 return 0;
503}
504
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100505/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400506static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100507{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000508 struct connection *con;
509
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000510 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500511 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100512 queue_work(recv_workqueue, &con->rwork);
513}
514
Alexander Aringd11ccd42020-11-02 20:04:25 -0500515static void lowcomms_listen_data_ready(struct sock *sk)
516{
Alexander Aring9a4139a2021-06-02 09:45:18 -0400517 if (!dlm_allow_conn)
518 return;
519
Alexander Aringd11ccd42020-11-02 20:04:25 -0500520 queue_work(recv_workqueue, &listen_con.rwork);
521}
522
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100523static void lowcomms_write_space(struct sock *sk)
524{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000525 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100526
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000527 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800528 if (!con)
Alexander Aring92c44602021-11-15 08:57:06 -0500529 return;
David Millerb36930d2010-11-10 21:56:39 -0800530
Alexander Aring19633c72020-11-02 20:04:20 -0500531 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
532 log_print("successful connected to node %d", con->nodeid);
533 queue_work(send_workqueue, &con->swork);
Alexander Aring92c44602021-11-15 08:57:06 -0500534 return;
Alexander Aring19633c72020-11-02 20:04:20 -0500535 }
536
David Millerb36930d2010-11-10 21:56:39 -0800537 clear_bit(SOCK_NOSPACE, &con->sock->flags);
538
539 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
540 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800541 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800542 }
543
Bob Peterson01da24d2017-09-12 08:55:14 +0000544 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100545}
546
547static inline void lowcomms_connect_sock(struct connection *con)
548{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500549 if (test_bit(CF_CLOSE, &con->flags))
550 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000551 queue_work(send_workqueue, &con->swork);
552 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100553}
554
555static void lowcomms_state_change(struct sock *sk)
556{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300557 /* SCTP layer is not calling sk_data_ready when the connection
558 * is done, so we catch the signal through here. Also, it
559 * doesn't switch socket state when entering shutdown, so we
560 * skip the write in that case.
561 */
562 if (sk->sk_shutdown) {
563 if (sk->sk_shutdown == RCV_SHUTDOWN)
564 lowcomms_data_ready(sk);
565 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100566 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300567 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100568}
569
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500570int dlm_lowcomms_connect_node(int nodeid)
571{
572 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400573 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500574
575 if (nodeid == dlm_our_nodeid())
576 return 0;
577
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400578 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500579 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400580 if (!con) {
581 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500582 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400583 }
584
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500585 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400586 srcu_read_unlock(&connections_srcu, idx);
587
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500588 return 0;
589}
590
Alexander Aringe125fbe2021-03-01 17:05:09 -0500591int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
592{
593 struct dlm_node_addr *na;
594
595 spin_lock(&dlm_node_addrs_spin);
596 na = find_node_addr(nodeid);
597 if (!na) {
598 spin_unlock(&dlm_node_addrs_spin);
599 return -ENOENT;
600 }
601
602 na->mark = mark;
603 spin_unlock(&dlm_node_addrs_spin);
604
605 return 0;
606}
607
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500608static void lowcomms_error_report(struct sock *sk)
609{
Bob Petersonb81171c2016-02-05 14:39:02 -0500610 struct connection *con;
Bob Petersonb81171c2016-02-05 14:39:02 -0500611 void (*orig_report)(struct sock *) = NULL;
Alexander Aring4c3d9052021-11-15 08:57:05 -0500612 struct inet_sock *inet;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500613
Bob Petersonb81171c2016-02-05 14:39:02 -0500614 con = sock2con(sk);
615 if (con == NULL)
616 goto out;
617
Bob Petersoncc661fc2017-09-12 08:55:23 +0000618 orig_report = listen_sock.sk_error_report;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500619
Alexander Aring4c3d9052021-11-15 08:57:05 -0500620 inet = inet_sk(sk);
621 switch (sk->sk_family) {
622 case AF_INET:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500623 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500624 "sending to node %d at %pI4, dport %d, "
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500625 "sk_err=%d/%d\n", dlm_our_nodeid(),
Alexander Aring4c3d9052021-11-15 08:57:05 -0500626 con->nodeid, &inet->inet_daddr,
627 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500628 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500629 break;
Alexander Aring1b9beda2021-11-17 09:20:43 -0500630#if IS_ENABLED(CONFIG_IPV6)
Alexander Aring4c3d9052021-11-15 08:57:05 -0500631 case AF_INET6:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500632 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500633 "sending to node %d at %pI6c, "
634 "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
635 con->nodeid, &sk->sk_v6_daddr,
636 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500637 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500638 break;
Alexander Aring1b9beda2021-11-17 09:20:43 -0500639#endif
Alexander Aring4c3d9052021-11-15 08:57:05 -0500640 default:
641 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
642 "invalid socket family %d set, "
643 "sk_err=%d/%d\n", dlm_our_nodeid(),
644 sk->sk_family, sk->sk_err, sk->sk_err_soft);
645 goto out;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500646 }
Alexander Aringba868d92021-05-21 15:08:37 -0400647
648 /* below sendcon only handling */
649 if (test_bit(CF_IS_OTHERCON, &con->flags))
650 con = con->sendcon;
651
652 switch (sk->sk_err) {
653 case ECONNREFUSED:
654 set_bit(CF_DELAY_CONNECT, &con->flags);
655 break;
656 default:
657 break;
658 }
659
660 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
661 queue_work(send_workqueue, &con->swork);
662
Bob Petersonb81171c2016-02-05 14:39:02 -0500663out:
Bob Petersonb81171c2016-02-05 14:39:02 -0500664 if (orig_report)
665 orig_report(sk);
666}
667
668/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000669static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500670{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000671 struct sock *sk = sock->sk;
672
673 listen_sock.sk_data_ready = sk->sk_data_ready;
674 listen_sock.sk_state_change = sk->sk_state_change;
675 listen_sock.sk_write_space = sk->sk_write_space;
676 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500677}
678
Bob Petersoncc661fc2017-09-12 08:55:23 +0000679static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500680{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000681 struct sock *sk = sock->sk;
682
Alexander Aring92c44602021-11-15 08:57:06 -0500683 lock_sock(sk);
Bob Petersonb81171c2016-02-05 14:39:02 -0500684 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000685 sk->sk_data_ready = listen_sock.sk_data_ready;
686 sk->sk_state_change = listen_sock.sk_state_change;
687 sk->sk_write_space = listen_sock.sk_write_space;
688 sk->sk_error_report = listen_sock.sk_error_report;
Alexander Aring92c44602021-11-15 08:57:06 -0500689 release_sock(sk);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500690}
691
Alexander Aringd11ccd42020-11-02 20:04:25 -0500692static void add_listen_sock(struct socket *sock, struct listen_connection *con)
693{
694 struct sock *sk = sock->sk;
695
Alexander Aring92c44602021-11-15 08:57:06 -0500696 lock_sock(sk);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500697 save_listen_callbacks(sock);
698 con->sock = sock;
699
700 sk->sk_user_data = con;
701 sk->sk_allocation = GFP_NOFS;
702 /* Install a data_ready callback */
703 sk->sk_data_ready = lowcomms_listen_data_ready;
Alexander Aring92c44602021-11-15 08:57:06 -0500704 release_sock(sk);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500705}
706
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100707/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000708static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100709{
Bob Petersonb81171c2016-02-05 14:39:02 -0500710 struct sock *sk = sock->sk;
711
Alexander Aring92c44602021-11-15 08:57:06 -0500712 lock_sock(sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100713 con->sock = sock;
714
Bob Petersonb81171c2016-02-05 14:39:02 -0500715 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100716 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500717 sk->sk_data_ready = lowcomms_data_ready;
718 sk->sk_write_space = lowcomms_write_space;
719 sk->sk_state_change = lowcomms_state_change;
720 sk->sk_allocation = GFP_NOFS;
721 sk->sk_error_report = lowcomms_error_report;
Alexander Aring92c44602021-11-15 08:57:06 -0500722 release_sock(sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100723}
724
725/* Add the port number to an IPv6 or 4 sockaddr and return the address
726 length */
727static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
728 int *addr_len)
729{
730 saddr->ss_family = dlm_local_addr[0]->ss_family;
731 if (saddr->ss_family == AF_INET) {
732 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
733 in4_addr->sin_port = cpu_to_be16(port);
734 *addr_len = sizeof(struct sockaddr_in);
735 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
736 } else {
737 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
738 in6_addr->sin6_port = cpu_to_be16(port);
739 *addr_len = sizeof(struct sockaddr_in6);
740 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100741 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100742}
743
Alexander Aring706474f2021-05-21 15:08:48 -0400744static void dlm_page_release(struct kref *kref)
745{
746 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
747 ref);
748
749 __free_page(e->page);
Alexander Aring3af23262021-11-30 14:47:19 -0500750 dlm_free_writequeue(e);
Alexander Aring706474f2021-05-21 15:08:48 -0400751}
752
753static void dlm_msg_release(struct kref *kref)
754{
755 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
756
757 kref_put(&msg->entry->ref, dlm_page_release);
Alexander Aringe4dc81e2021-11-30 14:47:20 -0500758 dlm_free_msg(msg);
Alexander Aring706474f2021-05-21 15:08:48 -0400759}
760
761static void free_entry(struct writequeue_entry *e)
762{
763 struct dlm_msg *msg, *tmp;
764
765 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
766 if (msg->orig_msg) {
767 msg->orig_msg->retransmit = false;
768 kref_put(&msg->orig_msg->ref, dlm_msg_release);
769 }
770
771 list_del(&msg->list);
772 kref_put(&msg->ref, dlm_msg_release);
773 }
774
775 list_del(&e->list);
776 atomic_dec(&e->con->writequeue_cnt);
777 kref_put(&e->ref, dlm_page_release);
778}
779
Alexander Aringd11ccd42020-11-02 20:04:25 -0500780static void dlm_close_sock(struct socket **sock)
781{
782 if (*sock) {
783 restore_callbacks(*sock);
784 sock_release(*sock);
785 *sock = NULL;
786 }
787}
788
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100789/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300790static void close_connection(struct connection *con, bool and_other,
791 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100792{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000793 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
Alexander Aring706474f2021-05-21 15:08:48 -0400794 struct writequeue_entry *e;
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000795
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000796 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300797 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000798 clear_bit(CF_WRITE_PENDING, &con->flags);
799 }
800 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300801 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000802 clear_bit(CF_READ_PENDING, &con->flags);
803 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100804
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300805 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500806 dlm_close_sock(&con->sock);
807
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100808 if (con->othercon && and_other) {
809 /* Will only re-enter once. */
Alexander Aringc6aa00e32021-05-21 15:08:38 -0400810 close_connection(con->othercon, false, tx, rx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100811 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100812
Alexander Aring706474f2021-05-21 15:08:48 -0400813 /* if we send a writequeue entry only a half way, we drop the
814 * whole entry because reconnection and that we not start of the
815 * middle of a msg which will confuse the other end.
816 *
817 * we can always drop messages because retransmits, but what we
818 * cannot allow is to transmit half messages which may be processed
819 * at the other side.
820 *
821 * our policy is to start on a clean state when disconnects, we don't
822 * know what's send/received on transport layer in this case.
823 */
824 spin_lock(&con->writequeue_lock);
825 if (!list_empty(&con->writequeue)) {
826 e = list_first_entry(&con->writequeue, struct writequeue_entry,
827 list);
828 if (e->dirty)
829 free_entry(e);
830 }
831 spin_unlock(&con->writequeue_lock);
832
Alexander Aring4798cbb2020-09-24 10:31:26 -0400833 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100834 con->retries = 0;
Alexander Aring052849b2021-07-16 16:22:37 -0400835 clear_bit(CF_APP_LIMITED, &con->flags);
Alexander Aring19633c72020-11-02 20:04:20 -0500836 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400837 clear_bit(CF_DELAY_CONNECT, &con->flags);
838 clear_bit(CF_RECONNECT, &con->flags);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400839 clear_bit(CF_EOF, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100840 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000841 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100842}
843
Alexander Aring055923b2020-07-27 09:13:38 -0400844static void shutdown_connection(struct connection *con)
845{
846 int ret;
847
Alexander Aringeec054b2021-03-01 17:05:19 -0500848 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400849
850 mutex_lock(&con->sock_mutex);
851 /* nothing to shutdown */
852 if (!con->sock) {
853 mutex_unlock(&con->sock_mutex);
854 return;
855 }
856
857 set_bit(CF_SHUTDOWN, &con->flags);
858 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
859 mutex_unlock(&con->sock_mutex);
860 if (ret) {
861 log_print("Connection %p failed to shutdown: %d will force close",
862 con, ret);
863 goto force_close;
864 } else {
865 ret = wait_event_timeout(con->shutdown_wait,
866 !test_bit(CF_SHUTDOWN, &con->flags),
867 DLM_SHUTDOWN_WAIT_TIMEOUT);
868 if (ret == 0) {
869 log_print("Connection %p shutdown timed out, will force close",
870 con);
871 goto force_close;
872 }
873 }
874
875 return;
876
877force_close:
878 clear_bit(CF_SHUTDOWN, &con->flags);
879 close_connection(con, false, true, true);
880}
881
882static void dlm_tcp_shutdown(struct connection *con)
883{
884 if (con->othercon)
885 shutdown_connection(con->othercon);
886 shutdown_connection(con);
887}
888
Alexander Aring4798cbb2020-09-24 10:31:26 -0400889static int con_realloc_receive_buf(struct connection *con, int newlen)
890{
891 unsigned char *newbuf;
892
893 newbuf = kmalloc(newlen, GFP_NOFS);
894 if (!newbuf)
895 return -ENOMEM;
896
897 /* copy any leftover from last receive */
898 if (con->rx_leftover)
899 memmove(newbuf, con->rx_buf, con->rx_leftover);
900
901 /* swap to new buffer space */
902 kfree(con->rx_buf);
903 con->rx_buflen = newlen;
904 con->rx_buf = newbuf;
905
906 return 0;
907}
908
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100909/* Data received from remote end */
910static int receive_from_sock(struct connection *con)
911{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400912 struct msghdr msg;
913 struct kvec iov;
914 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100915
916 mutex_lock(&con->sock_mutex);
917
918 if (con->sock == NULL) {
919 ret = -EAGAIN;
920 goto out_close;
921 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400922
Alexander Aring4798cbb2020-09-24 10:31:26 -0400923 /* realloc if we get new buffer size to read out */
924 buflen = dlm_config.ci_buffer_size;
925 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
926 ret = con_realloc_receive_buf(con, buflen);
927 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100928 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100929 }
930
Alexander Aring62699b32021-07-16 16:22:45 -0400931 for (;;) {
932 /* calculate new buffer parameter regarding last receive and
933 * possible leftover bytes
934 */
935 iov.iov_base = con->rx_buf + con->rx_leftover;
936 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100937
Alexander Aring62699b32021-07-16 16:22:45 -0400938 memset(&msg, 0, sizeof(msg));
939 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
940 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
941 msg.msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -0400942 trace_dlm_recv(con->nodeid, ret);
Alexander Aring62699b32021-07-16 16:22:45 -0400943 if (ret == -EAGAIN)
944 break;
945 else if (ret <= 0)
946 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100947
Alexander Aring62699b32021-07-16 16:22:45 -0400948 /* new buflen according readed bytes and leftover from last receive */
949 buflen = ret + con->rx_leftover;
950 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
951 if (ret < 0)
952 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100953
Alexander Aring62699b32021-07-16 16:22:45 -0400954 /* calculate leftover bytes from process and put it into begin of
955 * the receive buffer, so next receive we have the full message
956 * at the start address of the receive buffer.
957 */
958 con->rx_leftover = buflen - ret;
959 if (con->rx_leftover) {
960 memmove(con->rx_buf, con->rx_buf + ret,
961 con->rx_leftover);
962 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100963 }
964
Alexander Aringb97f8522021-08-18 16:27:13 -0400965 dlm_midcomms_receive_done(con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100966 mutex_unlock(&con->sock_mutex);
967 return 0;
968
969out_resched:
970 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
971 queue_work(recv_workqueue, &con->rwork);
972 mutex_unlock(&con->sock_mutex);
973 return -EAGAIN;
974
975out_close:
Alexander Aringba868d92021-05-21 15:08:37 -0400976 if (ret == 0) {
Alexander Aringba868d92021-05-21 15:08:37 -0400977 log_print("connection %p got EOF from %d",
978 con, con->nodeid);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400979
Alexander Aringa66c0082021-07-16 16:22:40 -0400980 if (dlm_proto_ops->eof_condition &&
981 dlm_proto_ops->eof_condition(con)) {
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400982 set_bit(CF_EOF, &con->flags);
983 mutex_unlock(&con->sock_mutex);
984 } else {
985 mutex_unlock(&con->sock_mutex);
986 close_connection(con, false, true, false);
987
988 /* handling for tcp shutdown */
989 clear_bit(CF_SHUTDOWN, &con->flags);
990 wake_up(&con->shutdown_wait);
991 }
992
Alexander Aringba868d92021-05-21 15:08:37 -0400993 /* signal to breaking receive worker */
994 ret = -1;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400995 } else {
996 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100997 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100998 return ret;
999}
1000
1001/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -05001002static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001003{
1004 int result;
1005 struct sockaddr_storage peeraddr;
1006 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001007 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001008 int nodeid;
1009 struct connection *newcon;
1010 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -04001011 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001012
Alexander Aringd11ccd42020-11-02 20:04:25 -05001013 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001014 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001015
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001016 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001017 if (result < 0)
1018 goto accept_err;
1019
1020 /* Get the connected socket's peer */
1021 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001022 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
1023 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001024 result = -ECONNABORTED;
1025 goto accept_err;
1026 }
1027
1028 /* Get the new node's NODEID */
1029 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -05001030 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Alexander Aringfeae43f82022-01-04 09:09:47 -05001031 switch (peeraddr.ss_family) {
1032 case AF_INET: {
1033 struct sockaddr_in *sin = (struct sockaddr_in *)&peeraddr;
1034
1035 log_print("connect from non cluster IPv4 node %pI4",
1036 &sin->sin_addr);
1037 break;
1038 }
1039#if IS_ENABLED(CONFIG_IPV6)
1040 case AF_INET6: {
1041 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&peeraddr;
1042
1043 log_print("connect from non cluster IPv6 node %pI6c",
1044 &sin6->sin6_addr);
1045 break;
1046 }
1047#endif
1048 default:
1049 log_print("invalid family from non cluster node");
1050 break;
1051 }
1052
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001053 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001054 return -1;
1055 }
1056
1057 log_print("got connection from %d", nodeid);
1058
1059 /* Check to see if we already have a connection to this node. This
1060 * could happen if the two nodes initiate a connection at roughly
1061 * the same time and the connections cross on the wire.
1062 * In this case we store the incoming one in "othercon"
1063 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001064 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -05001065 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001066 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001067 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001068 result = -ENOMEM;
1069 goto accept_err;
1070 }
Alexander Aringd11ccd42020-11-02 20:04:25 -05001071
Alexander Aringe125fbe2021-03-01 17:05:09 -05001072 sock_set_mark(newsock->sk, mark);
1073
Alexander Aringd11ccd42020-11-02 20:04:25 -05001074 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001075 if (newcon->sock) {
1076 struct connection *othercon = newcon->othercon;
1077
1078 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001079 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001080 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -05001081 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001082 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001083 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001084 result = -ENOMEM;
1085 goto accept_err;
1086 }
Alexander Aring4798cbb2020-09-24 10:31:26 -04001087
Alexander Aring6cde2102020-11-02 20:04:21 -05001088 result = dlm_con_init(othercon, nodeid);
1089 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -04001090 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +08001091 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001092 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -04001093 goto accept_err;
1094 }
1095
Alexander Aringe9a470a2021-03-01 17:05:11 -05001096 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -04001097 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -05001098 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -04001099 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001100 } else {
1101 /* close other sock con if we have something new */
1102 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +01001103 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001104
Alexander Aringe9a470a2021-03-01 17:05:11 -05001105 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001106 add_sock(newsock, othercon);
1107 addcon = othercon;
1108 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001109 }
1110 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -04001111 /* accept copies the sk after we've saved the callbacks, so we
1112 don't want to save them a second time or comm errors will
1113 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001114 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001115 addcon = newcon;
1116 }
1117
Alexander Aringb30a6242021-03-01 17:05:10 -05001118 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001119 mutex_unlock(&newcon->sock_mutex);
1120
1121 /*
1122 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001123 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001124 * to the read_sockets list
1125 */
1126 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1127 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001128
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001129 srcu_read_unlock(&connections_srcu, idx);
1130
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001131 return 0;
1132
1133accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001134 if (newsock)
1135 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001136
1137 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001138 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001139 return result;
1140}
1141
Mike Christie5d689872013-06-14 04:56:13 -05001142/*
1143 * writequeue_entry_complete - try to delete and free write queue entry
1144 * @e: write queue entry to try to delete
1145 * @completed: bytes completed
1146 *
1147 * writequeue_lock must be held.
1148 */
1149static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1150{
1151 e->offset += completed;
1152 e->len -= completed;
Alexander Aring706474f2021-05-21 15:08:48 -04001153 /* signal that page was half way transmitted */
1154 e->dirty = true;
Mike Christie5d689872013-06-14 04:56:13 -05001155
Alexander Aring8f2dc782021-05-21 15:08:42 -04001156 if (e->len == 0 && e->users == 0)
Mike Christie5d689872013-06-14 04:56:13 -05001157 free_entry(e);
Mike Christie5d689872013-06-14 04:56:13 -05001158}
1159
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001160/*
1161 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1162 */
Alexander Aring13004e82020-11-02 20:04:24 -05001163static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001164{
1165 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001166 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001167 int i, addr_len, result = 0;
1168
1169 for (i = 0; i < dlm_local_count; i++) {
1170 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1171 make_sockaddr(&localaddr, port, &addr_len);
1172
1173 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001174 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001175 else
Alexander Aring13004e82020-11-02 20:04:24 -05001176 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001177
1178 if (result < 0) {
1179 log_print("Can't bind to %d addr number %d, %d.\n",
1180 port, i + 1, result);
1181 break;
1182 }
1183 }
1184 return result;
1185}
1186
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001187/* Get local addresses */
1188static void init_local(void)
1189{
1190 struct sockaddr_storage sas, *addr;
1191 int i;
1192
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001193 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001194 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001195 if (dlm_our_addr(&sas, i))
1196 break;
1197
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301198 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001199 if (!addr)
1200 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001201 dlm_local_addr[dlm_local_count++] = addr;
1202 }
1203}
1204
Alexander Aring043697f2020-08-27 15:02:50 -04001205static void deinit_local(void)
1206{
1207 int i;
1208
1209 for (i = 0; i < dlm_local_count; i++)
1210 kfree(dlm_local_addr[i]);
1211}
1212
Alexander Aringbe3b0402021-11-30 14:47:17 -05001213static struct writequeue_entry *new_writequeue_entry(struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001214{
1215 struct writequeue_entry *entry;
1216
Alexander Aring3af23262021-11-30 14:47:19 -05001217 entry = dlm_allocate_writequeue();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001218 if (!entry)
1219 return NULL;
1220
Alexander Aringbe3b0402021-11-30 14:47:17 -05001221 entry->page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001222 if (!entry->page) {
Alexander Aring3af23262021-11-30 14:47:19 -05001223 dlm_free_writequeue(entry);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001224 return NULL;
1225 }
1226
Alexander Aring3af23262021-11-30 14:47:19 -05001227 entry->offset = 0;
1228 entry->len = 0;
1229 entry->end = 0;
1230 entry->dirty = false;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001231 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001232 entry->users = 1;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001233 kref_init(&entry->ref);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001234 return entry;
1235}
1236
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001237static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
Alexander Aringbe3b0402021-11-30 14:47:17 -05001238 char **ppc, void (*cb)(void *data),
1239 void *data)
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001240{
1241 struct writequeue_entry *e;
1242
1243 spin_lock(&con->writequeue_lock);
1244 if (!list_empty(&con->writequeue)) {
1245 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1246 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
Alexander Aring8f2dc782021-05-21 15:08:42 -04001247 kref_get(&e->ref);
1248
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001249 *ppc = page_address(e->page) + e->end;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001250 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001251 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001252
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001253 e->end += len;
1254 e->users++;
Alexander Aringbe3b0402021-11-30 14:47:17 -05001255 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001256 }
1257 }
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001258
Alexander Aringbe3b0402021-11-30 14:47:17 -05001259 e = new_writequeue_entry(con);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001260 if (!e)
Alexander Aringbe3b0402021-11-30 14:47:17 -05001261 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001262
Alexander Aring8f2dc782021-05-21 15:08:42 -04001263 kref_get(&e->ref);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001264 *ppc = page_address(e->page);
1265 e->end += len;
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001266 atomic_inc(&con->writequeue_cnt);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001267 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001268 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001269
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001270 list_add_tail(&e->list, &con->writequeue);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001271
Alexander Aringbe3b0402021-11-30 14:47:17 -05001272out:
1273 spin_unlock(&con->writequeue_lock);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001274 return e;
1275};
1276
Alexander Aring2874d1a2021-05-21 15:08:43 -04001277static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1278 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001279 void (*cb)(void *data),
1280 void *data)
Alexander Aring2874d1a2021-05-21 15:08:43 -04001281{
1282 struct writequeue_entry *e;
1283 struct dlm_msg *msg;
1284
Alexander Aringe4dc81e2021-11-30 14:47:20 -05001285 msg = dlm_allocate_msg(allocation);
Alexander Aring2874d1a2021-05-21 15:08:43 -04001286 if (!msg)
1287 return NULL;
1288
1289 kref_init(&msg->ref);
1290
Alexander Aringbe3b0402021-11-30 14:47:17 -05001291 e = new_wq_entry(con, len, ppc, cb, data);
Alexander Aring2874d1a2021-05-21 15:08:43 -04001292 if (!e) {
Alexander Aringe4dc81e2021-11-30 14:47:20 -05001293 dlm_free_msg(msg);
Alexander Aring2874d1a2021-05-21 15:08:43 -04001294 return NULL;
1295 }
1296
Alexander Aringe4dc81e2021-11-30 14:47:20 -05001297 msg->retransmit = false;
1298 msg->orig_msg = NULL;
Alexander Aring2874d1a2021-05-21 15:08:43 -04001299 msg->ppc = *ppc;
1300 msg->len = len;
1301 msg->entry = e;
1302
1303 return msg;
1304}
1305
Alexander Aring8f2dc782021-05-21 15:08:42 -04001306struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001307 char **ppc, void (*cb)(void *data),
1308 void *data)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001309{
1310 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001311 struct dlm_msg *msg;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001312 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001313
Alexander Aringd10a0b82021-06-02 09:45:20 -04001314 if (len > DLM_MAX_SOCKET_BUFSIZE ||
Alexander Aringc45674f2021-03-01 17:05:14 -05001315 len < sizeof(struct dlm_header)) {
Alexander Aringd10a0b82021-06-02 09:45:20 -04001316 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001317 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001318 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001319 return NULL;
1320 }
1321
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001322 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001323 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001324 if (!con) {
1325 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001326 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001327 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001328
Alexander Aring5c16feb2021-11-02 15:17:19 -04001329 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001330 if (!msg) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001331 srcu_read_unlock(&connections_srcu, idx);
1332 return NULL;
1333 }
1334
Alexander Aring8f2dc782021-05-21 15:08:42 -04001335 /* we assume if successful commit must called */
1336 msg->idx = idx;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001337 return msg;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001338}
1339
Alexander Aring2874d1a2021-05-21 15:08:43 -04001340static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001341{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001342 struct writequeue_entry *e = msg->entry;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001343 struct connection *con = e->con;
1344 int users;
1345
1346 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001347 kref_get(&msg->ref);
1348 list_add(&msg->list, &e->msgs);
1349
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001350 users = --e->users;
1351 if (users)
1352 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001353
1354 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001355 spin_unlock(&con->writequeue_lock);
1356
Bob Peterson01da24d2017-09-12 08:55:14 +00001357 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001358 return;
1359
1360out:
1361 spin_unlock(&con->writequeue_lock);
1362 return;
1363}
1364
Alexander Aring2874d1a2021-05-21 15:08:43 -04001365void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1366{
1367 _dlm_lowcomms_commit_msg(msg);
1368 srcu_read_unlock(&connections_srcu, msg->idx);
1369}
1370
Alexander Aring8f2dc782021-05-21 15:08:42 -04001371void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1372{
1373 kref_put(&msg->ref, dlm_msg_release);
1374}
1375
Alexander Aring2874d1a2021-05-21 15:08:43 -04001376/* does not held connections_srcu, usage workqueue only */
1377int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1378{
1379 struct dlm_msg *msg_resend;
1380 char *ppc;
1381
1382 if (msg->retransmit)
1383 return 1;
1384
1385 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1386 GFP_ATOMIC, &ppc, NULL, NULL);
1387 if (!msg_resend)
1388 return -ENOMEM;
1389
1390 msg->retransmit = true;
1391 kref_get(&msg->ref);
1392 msg_resend->orig_msg = msg;
1393
1394 memcpy(ppc, msg->ppc, msg->len);
1395 _dlm_lowcomms_commit_msg(msg_resend);
1396 dlm_lowcomms_put_msg(msg_resend);
1397
1398 return 0;
1399}
1400
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001401/* Send a message */
1402static void send_to_sock(struct connection *con)
1403{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001404 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1405 struct writequeue_entry *e;
Alexander Aring66d59552021-07-16 16:22:39 -04001406 int len, offset, ret;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001407 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001408
1409 mutex_lock(&con->sock_mutex);
1410 if (con->sock == NULL)
1411 goto out_connect;
1412
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001413 spin_lock(&con->writequeue_lock);
1414 for (;;) {
Alexander Aring66d59552021-07-16 16:22:39 -04001415 e = con_next_wq(con);
1416 if (!e)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001417 break;
1418
1419 len = e->len;
1420 offset = e->offset;
1421 BUG_ON(len == 0 && e->users == 0);
1422 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001423
Alexander Aring66d59552021-07-16 16:22:39 -04001424 ret = kernel_sendpage(con->sock, e->page, offset, len,
1425 msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -04001426 trace_dlm_send(con->nodeid, ret);
Alexander Aring66d59552021-07-16 16:22:39 -04001427 if (ret == -EAGAIN || ret == 0) {
1428 if (ret == -EAGAIN &&
1429 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1430 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1431 /* Notify TCP that we're limited by the
1432 * application window size.
1433 */
1434 set_bit(SOCK_NOSPACE, &con->sock->flags);
1435 con->sock->sk->sk_write_pending++;
1436 }
1437 cond_resched();
1438 goto out;
1439 } else if (ret < 0)
1440 goto out;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001441
1442 /* Don't starve people filling buffers */
1443 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001444 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001445 count = 0;
1446 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001447
1448 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001449 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001450 }
1451 spin_unlock(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001452
1453 /* close if we got EOF */
1454 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1455 mutex_unlock(&con->sock_mutex);
1456 close_connection(con, false, false, true);
1457
1458 /* handling for tcp shutdown */
1459 clear_bit(CF_SHUTDOWN, &con->flags);
1460 wake_up(&con->shutdown_wait);
1461 } else {
1462 mutex_unlock(&con->sock_mutex);
1463 }
1464
1465 return;
1466
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001467out:
1468 mutex_unlock(&con->sock_mutex);
1469 return;
1470
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001471out_connect:
1472 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001473 queue_work(send_workqueue, &con->swork);
1474 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001475}
1476
1477static void clean_one_writequeue(struct connection *con)
1478{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001479 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001480
1481 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001482 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001483 free_entry(e);
1484 }
1485 spin_unlock(&con->writequeue_lock);
1486}
1487
1488/* Called from recovery when it knows that a node has
1489 left the cluster */
1490int dlm_lowcomms_close(int nodeid)
1491{
1492 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001493 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001494 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001495
1496 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001497 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001498 con = nodeid2con(nodeid, 0);
1499 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001500 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001501 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001502 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001503 if (con->othercon)
1504 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001505 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001506 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001507
1508 spin_lock(&dlm_node_addrs_spin);
1509 na = find_node_addr(nodeid);
1510 if (na) {
1511 list_del(&na->list);
1512 while (na->addr_count--)
1513 kfree(na->addr[na->addr_count]);
1514 kfree(na);
1515 }
1516 spin_unlock(&dlm_node_addrs_spin);
1517
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001518 return 0;
1519}
1520
1521/* Receive workqueue function */
1522static void process_recv_sockets(struct work_struct *work)
1523{
1524 struct connection *con = container_of(work, struct connection, rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001525
1526 clear_bit(CF_READ_PENDING, &con->flags);
Alexander Aring62699b32021-07-16 16:22:45 -04001527 receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001528}
1529
Alexander Aringd11ccd42020-11-02 20:04:25 -05001530static void process_listen_recv_socket(struct work_struct *work)
1531{
1532 accept_from_sock(&listen_con);
1533}
1534
Alexander Aring8728a452021-07-16 16:22:43 -04001535static void dlm_connect(struct connection *con)
1536{
1537 struct sockaddr_storage addr;
1538 int result, addr_len;
1539 struct socket *sock;
1540 unsigned int mark;
1541
1542 /* Some odd races can cause double-connects, ignore them */
1543 if (con->retries++ > MAX_CONNECT_RETRIES)
1544 return;
1545
1546 if (con->sock) {
1547 log_print("node %d already connected.", con->nodeid);
1548 return;
1549 }
1550
1551 memset(&addr, 0, sizeof(addr));
1552 result = nodeid_to_addr(con->nodeid, &addr, NULL,
1553 dlm_proto_ops->try_new_addr, &mark);
1554 if (result < 0) {
1555 log_print("no address for nodeid %d", con->nodeid);
1556 return;
1557 }
1558
1559 /* Create a socket to communicate with */
1560 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1561 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1562 if (result < 0)
1563 goto socket_err;
1564
1565 sock_set_mark(sock->sk, mark);
1566 dlm_proto_ops->sockopts(sock);
1567
1568 add_sock(sock, con);
1569
1570 result = dlm_proto_ops->bind(sock);
1571 if (result < 0)
1572 goto add_sock_err;
1573
1574 log_print_ratelimited("connecting to %d", con->nodeid);
1575 make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1576 result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr,
1577 addr_len);
1578 if (result < 0)
1579 goto add_sock_err;
1580
1581 return;
1582
1583add_sock_err:
1584 dlm_close_sock(&con->sock);
1585
1586socket_err:
1587 /*
1588 * Some errors are fatal and this list might need adjusting. For other
1589 * errors we try again until the max number of retries is reached.
1590 */
1591 if (result != -EHOSTUNREACH &&
1592 result != -ENETUNREACH &&
1593 result != -ENETDOWN &&
1594 result != -EINVAL &&
1595 result != -EPROTONOSUPPORT) {
1596 log_print("connect %d try %d error %d", con->nodeid,
1597 con->retries, result);
1598 msleep(1000);
1599 lowcomms_connect_sock(con);
1600 }
1601}
1602
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001603/* Send workqueue function */
1604static void process_send_sockets(struct work_struct *work)
1605{
1606 struct connection *con = container_of(work, struct connection, swork);
1607
Alexander Aring7443bc92021-05-21 15:08:36 -04001608 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1609
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001610 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001611
Alexander Aring489d8e52021-05-21 15:08:46 -04001612 if (test_and_clear_bit(CF_RECONNECT, &con->flags)) {
Alexander Aringba868d92021-05-21 15:08:37 -04001613 close_connection(con, false, false, true);
Alexander Aring489d8e52021-05-21 15:08:46 -04001614 dlm_midcomms_unack_msg_resend(con->nodeid);
1615 }
Alexander Aringba868d92021-05-21 15:08:37 -04001616
Alexander Aring8728a452021-07-16 16:22:43 -04001617 if (con->sock == NULL) {
Alexander Aringba868d92021-05-21 15:08:37 -04001618 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1619 msleep(1000);
Alexander Aring8728a452021-07-16 16:22:43 -04001620
1621 mutex_lock(&con->sock_mutex);
1622 dlm_connect(con);
1623 mutex_unlock(&con->sock_mutex);
Alexander Aringba868d92021-05-21 15:08:37 -04001624 }
Alexander Aring8728a452021-07-16 16:22:43 -04001625
Bob Peterson01da24d2017-09-12 08:55:14 +00001626 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001627 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001628}
1629
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001630static void work_stop(void)
1631{
Alexander Aringfcef0e62021-06-02 09:45:15 -04001632 if (recv_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001633 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001634 recv_workqueue = NULL;
1635 }
1636
1637 if (send_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001638 destroy_workqueue(send_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001639 send_workqueue = NULL;
1640 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001641}
1642
1643static int work_start(void)
1644{
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001645 recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001646 if (!recv_workqueue) {
1647 log_print("can't start dlm_recv");
1648 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001649 }
1650
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001651 send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001652 if (!send_workqueue) {
1653 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001654 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001655 recv_workqueue = NULL;
Namhyung Kimb9d41052010-12-13 13:42:24 -06001656 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001657 }
1658
1659 return 0;
1660}
1661
Alexander Aring9d232462021-03-01 17:05:20 -05001662static void shutdown_conn(struct connection *con)
1663{
Alexander Aringa66c0082021-07-16 16:22:40 -04001664 if (dlm_proto_ops->shutdown_action)
1665 dlm_proto_ops->shutdown_action(con);
Alexander Aring9d232462021-03-01 17:05:20 -05001666}
1667
1668void dlm_lowcomms_shutdown(void)
1669{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001670 int idx;
1671
Alexander Aring9d232462021-03-01 17:05:20 -05001672 /* Set all the flags to prevent any
1673 * socket activity.
1674 */
1675 dlm_allow_conn = 0;
1676
1677 if (recv_workqueue)
1678 flush_workqueue(recv_workqueue);
1679 if (send_workqueue)
1680 flush_workqueue(send_workqueue);
1681
1682 dlm_close_sock(&listen_con.sock);
1683
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001684 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001685 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001686 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001687}
1688
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001689static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001690{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001691 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001692 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001693 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001694 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001695 if (con->sock && con->sock->sk) {
Alexander Aring92c44602021-11-15 08:57:06 -05001696 lock_sock(con->sock->sk);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001697 con->sock->sk->sk_user_data = NULL;
Alexander Aring92c44602021-11-15 08:57:06 -05001698 release_sock(con->sock->sk);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001699 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001700 if (con->othercon && and_other)
1701 _stop_conn(con->othercon, false);
1702 mutex_unlock(&con->sock_mutex);
1703}
1704
1705static void stop_conn(struct connection *con)
1706{
1707 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001708}
1709
Alexander Aring4798cbb2020-09-24 10:31:26 -04001710static void connection_release(struct rcu_head *rcu)
1711{
1712 struct connection *con = container_of(rcu, struct connection, rcu);
1713
1714 kfree(con->rx_buf);
1715 kfree(con);
1716}
1717
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001718static void free_conn(struct connection *con)
1719{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001720 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001721 spin_lock(&connections_lock);
1722 hlist_del_rcu(&con->list);
1723 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001724 if (con->othercon) {
1725 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001726 call_srcu(&connections_srcu, &con->othercon->rcu,
1727 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001728 }
Alexander Aring0de98432020-08-27 15:02:52 -04001729 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001730 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001731}
1732
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001733static void work_flush(void)
1734{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001735 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001736 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001737 struct connection *con;
1738
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001739 do {
1740 ok = 1;
1741 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001742 if (recv_workqueue)
1743 flush_workqueue(recv_workqueue);
1744 if (send_workqueue)
1745 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001746 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001747 hlist_for_each_entry_rcu(con, &connection_hash[i],
1748 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001749 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001750 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1751 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001752 ok &= test_bit(CF_READ_PENDING,
1753 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001754 ok &= test_bit(CF_WRITE_PENDING,
1755 &con->othercon->flags);
1756 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001757 }
1758 }
1759 } while (!ok);
1760}
1761
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001762void dlm_lowcomms_stop(void)
1763{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001764 int idx;
1765
1766 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001767 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001768 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001769 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001770 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001771 deinit_local();
Alexander Aringa66c0082021-07-16 16:22:40 -04001772
1773 dlm_proto_ops = NULL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001774}
1775
Alexander Aring2dc6b112021-07-16 16:22:41 -04001776static int dlm_listen_for_all(void)
1777{
1778 struct socket *sock;
1779 int result;
1780
1781 log_print("Using %s for communications",
1782 dlm_proto_ops->name);
1783
Alexander Aring90d21fc2021-07-16 16:22:42 -04001784 result = dlm_proto_ops->listen_validate();
1785 if (result < 0)
1786 return result;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001787
1788 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1789 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1790 if (result < 0) {
Alexander Aringfe933672021-11-02 15:17:10 -04001791 log_print("Can't create comms socket: %d", result);
Alexander Aring2dc6b112021-07-16 16:22:41 -04001792 goto out;
1793 }
1794
1795 sock_set_mark(sock->sk, dlm_config.ci_mark);
1796 dlm_proto_ops->listen_sockopts(sock);
1797
1798 result = dlm_proto_ops->listen_bind(sock);
1799 if (result < 0)
1800 goto out;
1801
1802 save_listen_callbacks(sock);
1803 add_listen_sock(sock, &listen_con);
1804
1805 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1806 result = sock->ops->listen(sock, 5);
1807 if (result < 0) {
1808 dlm_close_sock(&listen_con.sock);
1809 goto out;
1810 }
1811
1812 return 0;
1813
1814out:
1815 sock_release(sock);
1816 return result;
1817}
1818
Alexander Aring8728a452021-07-16 16:22:43 -04001819static int dlm_tcp_bind(struct socket *sock)
1820{
1821 struct sockaddr_storage src_addr;
1822 int result, addr_len;
1823
1824 /* Bind to our cluster-known address connecting to avoid
1825 * routing problems.
1826 */
1827 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1828 make_sockaddr(&src_addr, 0, &addr_len);
1829
1830 result = sock->ops->bind(sock, (struct sockaddr *)&src_addr,
1831 addr_len);
1832 if (result < 0) {
1833 /* This *may* not indicate a critical error */
1834 log_print("could not bind for connect: %d", result);
1835 }
1836
1837 return 0;
1838}
1839
1840static int dlm_tcp_connect(struct connection *con, struct socket *sock,
1841 struct sockaddr *addr, int addr_len)
1842{
1843 int ret;
1844
1845 ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK);
1846 switch (ret) {
1847 case -EINPROGRESS:
1848 fallthrough;
1849 case 0:
1850 return 0;
1851 }
1852
1853 return ret;
1854}
1855
Alexander Aring2dc6b112021-07-16 16:22:41 -04001856static int dlm_tcp_listen_validate(void)
1857{
1858 /* We don't support multi-homed hosts */
1859 if (dlm_local_count > 1) {
1860 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1861 return -EINVAL;
1862 }
1863
1864 return 0;
1865}
1866
1867static void dlm_tcp_sockopts(struct socket *sock)
1868{
1869 /* Turn off Nagle's algorithm */
1870 tcp_sock_set_nodelay(sock->sk);
1871}
1872
1873static void dlm_tcp_listen_sockopts(struct socket *sock)
1874{
1875 dlm_tcp_sockopts(sock);
1876 sock_set_reuseaddr(sock->sk);
1877}
1878
1879static int dlm_tcp_listen_bind(struct socket *sock)
1880{
1881 int addr_len;
1882
1883 /* Bind to our port */
1884 make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1885 return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0],
1886 addr_len);
1887}
1888
Alexander Aringa66c0082021-07-16 16:22:40 -04001889static const struct dlm_proto_ops dlm_tcp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001890 .name = "TCP",
1891 .proto = IPPROTO_TCP,
Alexander Aring8728a452021-07-16 16:22:43 -04001892 .connect = dlm_tcp_connect,
1893 .sockopts = dlm_tcp_sockopts,
1894 .bind = dlm_tcp_bind,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001895 .listen_validate = dlm_tcp_listen_validate,
1896 .listen_sockopts = dlm_tcp_listen_sockopts,
1897 .listen_bind = dlm_tcp_listen_bind,
Alexander Aringa66c0082021-07-16 16:22:40 -04001898 .shutdown_action = dlm_tcp_shutdown,
1899 .eof_condition = tcp_eof_condition,
1900};
1901
Alexander Aring8728a452021-07-16 16:22:43 -04001902static int dlm_sctp_bind(struct socket *sock)
1903{
1904 return sctp_bind_addrs(sock, 0);
1905}
1906
1907static int dlm_sctp_connect(struct connection *con, struct socket *sock,
1908 struct sockaddr *addr, int addr_len)
1909{
1910 int ret;
1911
1912 /*
1913 * Make sock->ops->connect() function return in specified time,
1914 * since O_NONBLOCK argument in connect() function does not work here,
1915 * then, we should restore the default value of this attribute.
1916 */
1917 sock_set_sndtimeo(sock->sk, 5);
1918 ret = sock->ops->connect(sock, addr, addr_len, 0);
1919 sock_set_sndtimeo(sock->sk, 0);
1920 if (ret < 0)
1921 return ret;
1922
1923 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1924 log_print("successful connected to node %d", con->nodeid);
1925
1926 return 0;
1927}
1928
Alexander Aring90d21fc2021-07-16 16:22:42 -04001929static int dlm_sctp_listen_validate(void)
1930{
1931 if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1932 log_print("SCTP is not enabled by this kernel");
1933 return -EOPNOTSUPP;
1934 }
1935
1936 request_module("sctp");
1937 return 0;
1938}
1939
Alexander Aring2dc6b112021-07-16 16:22:41 -04001940static int dlm_sctp_bind_listen(struct socket *sock)
1941{
1942 return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
1943}
1944
1945static void dlm_sctp_sockopts(struct socket *sock)
1946{
1947 /* Turn off Nagle's algorithm */
1948 sctp_sock_set_nodelay(sock->sk);
1949 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1950}
1951
Alexander Aringa66c0082021-07-16 16:22:40 -04001952static const struct dlm_proto_ops dlm_sctp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001953 .name = "SCTP",
1954 .proto = IPPROTO_SCTP,
Alexander Aring8728a452021-07-16 16:22:43 -04001955 .try_new_addr = true,
1956 .connect = dlm_sctp_connect,
1957 .sockopts = dlm_sctp_sockopts,
1958 .bind = dlm_sctp_bind,
Alexander Aring90d21fc2021-07-16 16:22:42 -04001959 .listen_validate = dlm_sctp_listen_validate,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001960 .listen_sockopts = dlm_sctp_sockopts,
1961 .listen_bind = dlm_sctp_bind_listen,
Alexander Aringa66c0082021-07-16 16:22:40 -04001962};
1963
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001964int dlm_lowcomms_start(void)
1965{
1966 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001967 int i;
1968
1969 for (i = 0; i < CONN_HASH_SIZE; i++)
1970 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001971
1972 init_local();
1973 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001974 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001975 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001976 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001977 }
1978
Alexander Aringd11ccd42020-11-02 20:04:25 -05001979 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1980
David Teigland513ef592012-03-30 11:46:08 -05001981 error = work_start();
1982 if (error)
Alexander Aringfcef0e62021-06-02 09:45:15 -04001983 goto fail_local;
David Teigland513ef592012-03-30 11:46:08 -05001984
1985 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001986
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001987 /* Start listening */
Alexander Aringac7d5d02021-06-02 09:45:19 -04001988 switch (dlm_config.ci_protocol) {
1989 case DLM_PROTO_TCP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001990 dlm_proto_ops = &dlm_tcp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001991 break;
1992 case DLM_PROTO_SCTP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001993 dlm_proto_ops = &dlm_sctp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001994 break;
1995 default:
1996 log_print("Invalid protocol identifier %d set",
1997 dlm_config.ci_protocol);
1998 error = -EINVAL;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001999 goto fail_proto_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04002000 }
Alexander Aring2dc6b112021-07-16 16:22:41 -04002001
2002 error = dlm_listen_for_all();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002003 if (error)
Alexander Aring2dc6b112021-07-16 16:22:41 -04002004 goto fail_listen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002005
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002006 return 0;
2007
Alexander Aring2dc6b112021-07-16 16:22:41 -04002008fail_listen:
2009 dlm_proto_ops = NULL;
2010fail_proto_ops:
David Teigland513ef592012-03-30 11:46:08 -05002011 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05002012 dlm_close_sock(&listen_con.sock);
Alexander Aringfcef0e62021-06-02 09:45:15 -04002013 work_stop();
2014fail_local:
2015 deinit_local();
David Teigland513ef592012-03-30 11:46:08 -05002016fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002017 return error;
2018}
David Teigland36b71a82012-07-26 12:44:30 -05002019
2020void dlm_lowcomms_exit(void)
2021{
2022 struct dlm_node_addr *na, *safe;
2023
2024 spin_lock(&dlm_node_addrs_spin);
2025 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
2026 list_del(&na->list);
2027 while (na->addr_count--)
2028 kfree(na->addr[na->addr_count]);
2029 kfree(na);
2030 }
2031 spin_unlock(&dlm_node_addrs_spin);
2032}