]>
Commit | Line | Data |
---|---|---|
2522fe45 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fdda387f PC |
2 | /****************************************************************************** |
3 | ******************************************************************************* | |
4 | ** | |
5 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
5e9ccc37 | 6 | ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved. |
fdda387f | 7 | ** |
fdda387f PC |
8 | ** |
9 | ******************************************************************************* | |
10 | ******************************************************************************/ | |
11 | ||
12 | /* | |
13 | * lowcomms.c | |
14 | * | |
15 | * This is the "low-level" comms layer. | |
16 | * | |
17 | * It is responsible for sending/receiving messages | |
18 | * from other nodes in the cluster. | |
19 | * | |
20 | * Cluster nodes are referred to by their nodeids. nodeids are | |
21 | * simply 32 bit numbers to the locking module - if they need to | |
2cf12c0b | 22 | * be expanded for the cluster infrastructure then that is its |
fdda387f PC |
23 | * responsibility. It is this layer's |
24 | * responsibility to resolve these into IP address or | |
25 | * whatever it needs for inter-node communication. | |
26 | * | |
27 | * The comms level is two kernel threads that deal mainly with | |
28 | * the receiving of messages from other nodes and passing them | |
29 | * up to the mid-level comms layer (which understands the | |
30 | * message format) for execution by the locking core, and | |
31 | * a send thread which does all the setting up of connections | |
32 | * to remote nodes and the sending of data. Threads are not allowed | |
33 | * to send their own data because it may cause them to wait in times | |
34 | * of high load. Also, this way, the sending thread can collect together | |
35 | * messages bound for one node and send them in one block. | |
36 | * | |
2cf12c0b | 37 | * lowcomms will choose to use either TCP or SCTP as its transport layer |
6ed7257b | 38 | * depending on the configuration variable 'protocol'. This should be set |
2cf12c0b | 39 | * to 0 (default) for TCP or 1 for SCTP. It should be configured using a |
6ed7257b PC |
40 | * cluster-wide mechanism as it must be the same on all nodes of the cluster |
41 | * for the DLM to function. | |
fdda387f PC |
42 | * |
43 | */ | |
44 | ||
fdda387f PC |
45 | #include <asm/ioctls.h> |
46 | #include <net/sock.h> | |
47 | #include <net/tcp.h> | |
48 | #include <linux/pagemap.h> | |
6ed7257b | 49 | #include <linux/file.h> |
7a936ce7 | 50 | #include <linux/mutex.h> |
6ed7257b | 51 | #include <linux/sctp.h> |
5a0e3ad6 | 52 | #include <linux/slab.h> |
2f2d76cc | 53 | #include <net/sctp/sctp.h> |
44ad532b | 54 | #include <net/ipv6.h> |
fdda387f | 55 | |
92732376 AA |
56 | #include <trace/events/dlm.h> |
57 | ||
fdda387f PC |
58 | #include "dlm_internal.h" |
59 | #include "lowcomms.h" | |
60 | #include "midcomms.h" | |
3af2326c | 61 | #include "memory.h" |
fdda387f PC |
62 | #include "config.h" |
63 | ||
6ed7257b PC |
64 | #define NEEDED_RMEM (4*1024*1024) |
65 | ||
f92c8dd7 BP |
66 | /* Number of messages to send before rescheduling */ |
67 | #define MAX_SEND_MSG_COUNT 25 | |
055923bf | 68 | #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000) |
f92c8dd7 | 69 | |
fdda387f PC |
70 | struct connection { |
71 | struct socket *sock; /* NULL if not connected */ | |
72 | uint32_t nodeid; /* So we know who we are in the list */ | |
f1f1c1cc | 73 | struct mutex sock_mutex; |
6ed7257b | 74 | unsigned long flags; |
fdda387f | 75 | #define CF_READ_PENDING 1 |
8a4abb08 | 76 | #define CF_WRITE_PENDING 2 |
6ed7257b PC |
77 | #define CF_INIT_PENDING 4 |
78 | #define CF_IS_OTHERCON 5 | |
063c4c99 | 79 | #define CF_CLOSE 6 |
b36930dd | 80 | #define CF_APP_LIMITED 7 |
b2a66629 | 81 | #define CF_CLOSING 8 |
055923bf | 82 | #define CF_SHUTDOWN 9 |
19633c7e | 83 | #define CF_CONNECTED 10 |
ba868d9d AA |
84 | #define CF_RECONNECT 11 |
85 | #define CF_DELAY_CONNECT 12 | |
8aa31cbf | 86 | #define CF_EOF 13 |
ac33d071 | 87 | struct list_head writequeue; /* List of outgoing writequeue_entries */ |
fdda387f | 88 | spinlock_t writequeue_lock; |
8aa31cbf | 89 | atomic_t writequeue_cnt; |
fdda387f | 90 | int retries; |
fdda387f | 91 | #define MAX_CONNECT_RETRIES 3 |
5e9ccc37 | 92 | struct hlist_node list; |
fdda387f | 93 | struct connection *othercon; |
ba868d9d | 94 | struct connection *sendcon; |
1d6e8131 PC |
95 | struct work_struct rwork; /* Receive workqueue */ |
96 | struct work_struct swork; /* Send workqueue */ | |
055923bf | 97 | wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */ |
4798cbbf AA |
98 | unsigned char *rx_buf; |
99 | int rx_buflen; | |
100 | int rx_leftover; | |
a47666eb | 101 | struct rcu_head rcu; |
fdda387f PC |
102 | }; |
103 | #define sock2con(x) ((struct connection *)(x)->sk_user_data) | |
104 | ||
d11ccd45 AA |
105 | struct listen_connection { |
106 | struct socket *sock; | |
107 | struct work_struct rwork; | |
108 | }; | |
109 | ||
f0747ebf AA |
110 | #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end) |
111 | #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset) | |
112 | ||
fdda387f PC |
113 | /* An entry waiting to be sent */ |
114 | struct writequeue_entry { | |
115 | struct list_head list; | |
116 | struct page *page; | |
117 | int offset; | |
118 | int len; | |
119 | int end; | |
120 | int users; | |
706474fb | 121 | bool dirty; |
fdda387f | 122 | struct connection *con; |
8f2dc78d AA |
123 | struct list_head msgs; |
124 | struct kref ref; | |
125 | }; | |
126 | ||
127 | struct dlm_msg { | |
128 | struct writequeue_entry *entry; | |
2874d1a6 AA |
129 | struct dlm_msg *orig_msg; |
130 | bool retransmit; | |
8f2dc78d AA |
131 | void *ppc; |
132 | int len; | |
133 | int idx; /* new()/commit() idx exchange */ | |
134 | ||
135 | struct list_head list; | |
136 | struct kref ref; | |
fdda387f PC |
137 | }; |
138 | ||
36b71a8b DT |
139 | struct dlm_node_addr { |
140 | struct list_head list; | |
141 | int nodeid; | |
e125fbeb | 142 | int mark; |
36b71a8b | 143 | int addr_count; |
98e1b60e | 144 | int curr_addr_index; |
36b71a8b DT |
145 | struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT]; |
146 | }; | |
147 | ||
a66c008c | 148 | struct dlm_proto_ops { |
8728a455 | 149 | bool try_new_addr; |
2dc6b115 AA |
150 | const char *name; |
151 | int proto; | |
152 | ||
8728a455 AA |
153 | int (*connect)(struct connection *con, struct socket *sock, |
154 | struct sockaddr *addr, int addr_len); | |
155 | void (*sockopts)(struct socket *sock); | |
156 | int (*bind)(struct socket *sock); | |
2dc6b115 AA |
157 | int (*listen_validate)(void); |
158 | void (*listen_sockopts)(struct socket *sock); | |
159 | int (*listen_bind)(struct socket *sock); | |
a66c008c AA |
160 | /* What to do to shutdown */ |
161 | void (*shutdown_action)(struct connection *con); | |
162 | /* What to do to eof check */ | |
163 | bool (*eof_condition)(struct connection *con); | |
164 | }; | |
165 | ||
cc661fc9 BP |
166 | static struct listen_sock_callbacks { |
167 | void (*sk_error_report)(struct sock *); | |
168 | void (*sk_data_ready)(struct sock *); | |
169 | void (*sk_state_change)(struct sock *); | |
170 | void (*sk_write_space)(struct sock *); | |
171 | } listen_sock; | |
172 | ||
36b71a8b DT |
173 | static LIST_HEAD(dlm_node_addrs); |
174 | static DEFINE_SPINLOCK(dlm_node_addrs_spin); | |
175 | ||
d11ccd45 | 176 | static struct listen_connection listen_con; |
6ed7257b PC |
177 | static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT]; |
178 | static int dlm_local_count; | |
51746163 | 179 | int dlm_allow_conn; |
fdda387f | 180 | |
1d6e8131 PC |
181 | /* Work queues */ |
182 | static struct workqueue_struct *recv_workqueue; | |
183 | static struct workqueue_struct *send_workqueue; | |
fdda387f | 184 | |
5e9ccc37 | 185 | static struct hlist_head connection_hash[CONN_HASH_SIZE]; |
a47666eb AA |
186 | static DEFINE_SPINLOCK(connections_lock); |
187 | DEFINE_STATIC_SRCU(connections_srcu); | |
fdda387f | 188 | |
a66c008c AA |
189 | static const struct dlm_proto_ops *dlm_proto_ops; |
190 | ||
1d6e8131 PC |
191 | static void process_recv_sockets(struct work_struct *work); |
192 | static void process_send_sockets(struct work_struct *work); | |
fdda387f | 193 | |
3af2326c AA |
194 | static void writequeue_entry_ctor(void *data) |
195 | { | |
196 | struct writequeue_entry *entry = data; | |
197 | ||
198 | INIT_LIST_HEAD(&entry->msgs); | |
199 | } | |
200 | ||
201 | struct 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 | ||
e4dc81ed AA |
207 | struct 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 | ||
66d5955a AA |
212 | /* need to held writequeue_lock */ |
213 | static 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); | |
bcbfea41 AA |
222 | /* 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) | |
66d5955a AA |
226 | return NULL; |
227 | ||
228 | return e; | |
229 | } | |
230 | ||
b38bc9c2 | 231 | static struct connection *__find_con(int nodeid, int r) |
5e9ccc37 | 232 | { |
5e9ccc37 CC |
233 | struct connection *con; |
234 | ||
a47666eb | 235 | hlist_for_each_entry_rcu(con, &connection_hash[r], list) { |
b38bc9c2 | 236 | if (con->nodeid == nodeid) |
5e9ccc37 CC |
237 | return con; |
238 | } | |
a47666eb | 239 | |
5e9ccc37 CC |
240 | return NULL; |
241 | } | |
242 | ||
8aa31cbf AA |
243 | static bool tcp_eof_condition(struct connection *con) |
244 | { | |
245 | return atomic_read(&con->writequeue_cnt); | |
246 | } | |
247 | ||
6cde210a | 248 | static int dlm_con_init(struct connection *con, int nodeid) |
fdda387f | 249 | { |
4798cbbf AA |
250 | con->rx_buflen = dlm_config.ci_buffer_size; |
251 | con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS); | |
6cde210a AA |
252 | if (!con->rx_buf) |
253 | return -ENOMEM; | |
4798cbbf | 254 | |
6ed7257b PC |
255 | con->nodeid = nodeid; |
256 | mutex_init(&con->sock_mutex); | |
257 | INIT_LIST_HEAD(&con->writequeue); | |
258 | spin_lock_init(&con->writequeue_lock); | |
8aa31cbf | 259 | atomic_set(&con->writequeue_cnt, 0); |
6ed7257b PC |
260 | INIT_WORK(&con->swork, process_send_sockets); |
261 | INIT_WORK(&con->rwork, process_recv_sockets); | |
055923bf | 262 | init_waitqueue_head(&con->shutdown_wait); |
fdda387f | 263 | |
6cde210a AA |
264 | 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 | */ | |
271 | static struct connection *nodeid2con(int nodeid, gfp_t alloc) | |
272 | { | |
273 | struct connection *con, *tmp; | |
274 | int r, ret; | |
275 | ||
b38bc9c2 AA |
276 | r = nodeid_hash(nodeid); |
277 | con = __find_con(nodeid, r); | |
6cde210a AA |
278 | 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 | ||
a47666eb | 291 | spin_lock(&connections_lock); |
4f2b30fd AA |
292 | /* 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 | */ | |
b38bc9c2 | 298 | tmp = __find_con(nodeid, r); |
4f2b30fd AA |
299 | if (tmp) { |
300 | spin_unlock(&connections_lock); | |
301 | kfree(con->rx_buf); | |
302 | kfree(con); | |
303 | return tmp; | |
304 | } | |
305 | ||
a47666eb AA |
306 | hlist_add_head_rcu(&con->list, &connection_hash[r]); |
307 | spin_unlock(&connections_lock); | |
308 | ||
6ed7257b PC |
309 | return con; |
310 | } | |
311 | ||
5e9ccc37 CC |
312 | /* Loop round all connections */ |
313 | static void foreach_conn(void (*conn_func)(struct connection *c)) | |
314 | { | |
b38bc9c2 | 315 | int i; |
5e9ccc37 CC |
316 | struct connection *con; |
317 | ||
318 | for (i = 0; i < CONN_HASH_SIZE; i++) { | |
a47666eb | 319 | hlist_for_each_entry_rcu(con, &connection_hash[i], list) |
5e9ccc37 | 320 | conn_func(con); |
5e9ccc37 | 321 | } |
fdda387f PC |
322 | } |
323 | ||
36b71a8b DT |
324 | static struct dlm_node_addr *find_node_addr(int nodeid) |
325 | { | |
326 | 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 | ||
40c6b83e AA |
335 | static int addr_compare(const struct sockaddr_storage *x, |
336 | const struct sockaddr_storage *y) | |
6ed7257b | 337 | { |
36b71a8b DT |
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 | ||
363 | static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out, | |
e125fbeb AA |
364 | struct sockaddr *sa_out, bool try_new_addr, |
365 | unsigned int *mark) | |
36b71a8b DT |
366 | { |
367 | struct sockaddr_storage sas; | |
368 | struct dlm_node_addr *na; | |
6ed7257b PC |
369 | |
370 | if (!dlm_local_count) | |
371 | return -1; | |
372 | ||
36b71a8b DT |
373 | spin_lock(&dlm_node_addrs_spin); |
374 | na = find_node_addr(nodeid); | |
98e1b60e | 375 | if (na && na->addr_count) { |
ee44b4bc MRL |
376 | memcpy(&sas, na->addr[na->curr_addr_index], |
377 | sizeof(struct sockaddr_storage)); | |
378 | ||
98e1b60e MC |
379 | 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 | } | |
98e1b60e | 384 | } |
36b71a8b DT |
385 | spin_unlock(&dlm_node_addrs_spin); |
386 | ||
387 | if (!na) | |
388 | return -EEXIST; | |
389 | ||
390 | if (!na->addr_count) | |
391 | return -ENOENT; | |
392 | ||
e125fbeb AA |
393 | *mark = na->mark; |
394 | ||
36b71a8b DT |
395 | if (sas_out) |
396 | memcpy(sas_out, &sas, sizeof(struct sockaddr_storage)); | |
397 | ||
398 | if (!sa_out) | |
399 | return 0; | |
6ed7257b PC |
400 | |
401 | if (dlm_local_addr[0]->ss_family == AF_INET) { | |
36b71a8b DT |
402 | struct sockaddr_in *in4 = (struct sockaddr_in *) &sas; |
403 | struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out; | |
6ed7257b PC |
404 | ret4->sin_addr.s_addr = in4->sin_addr.s_addr; |
405 | } else { | |
36b71a8b DT |
406 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas; |
407 | struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out; | |
4e3fd7a0 | 408 | ret6->sin6_addr = in6->sin6_addr; |
6ed7257b PC |
409 | } |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
e125fbeb AA |
414 | static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid, |
415 | unsigned int *mark) | |
36b71a8b DT |
416 | { |
417 | struct dlm_node_addr *na; | |
418 | int rv = -EEXIST; | |
98e1b60e | 419 | int addr_i; |
36b71a8b DT |
420 | |
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 | ||
98e1b60e MC |
426 | for (addr_i = 0; addr_i < na->addr_count; addr_i++) { |
427 | if (addr_compare(na->addr[addr_i], addr)) { | |
428 | *nodeid = na->nodeid; | |
e125fbeb | 429 | *mark = na->mark; |
98e1b60e MC |
430 | rv = 0; |
431 | goto unlock; | |
432 | } | |
433 | } | |
36b71a8b | 434 | } |
98e1b60e | 435 | unlock: |
36b71a8b DT |
436 | spin_unlock(&dlm_node_addrs_spin); |
437 | return rv; | |
438 | } | |
439 | ||
4f19d071 AA |
440 | /* caller need to held dlm_node_addrs_spin lock */ |
441 | static 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 | ||
36b71a8b DT |
454 | int 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; | |
4f19d071 | 458 | bool ret; |
36b71a8b DT |
459 | |
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; | |
e125fbeb | 478 | new_node->mark = dlm_config.ci_mark; |
36b71a8b DT |
479 | list_add(&new_node->list, &dlm_node_addrs); |
480 | spin_unlock(&dlm_node_addrs_spin); | |
481 | return 0; | |
482 | } | |
483 | ||
4f19d071 AA |
484 | 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 | ||
36b71a8b DT |
492 | 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 | ||
fdda387f | 505 | /* Data available on socket or listen socket received a connect */ |
676d2369 | 506 | static void lowcomms_data_ready(struct sock *sk) |
fdda387f | 507 | { |
93eaadeb | 508 | struct connection *con; |
509 | ||
93eaadeb | 510 | con = sock2con(sk); |
afb853fb | 511 | if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags)) |
1d6e8131 | 512 | queue_work(recv_workqueue, &con->rwork); |
fdda387f PC |
513 | } |
514 | ||
d11ccd45 AA |
515 | static void lowcomms_listen_data_ready(struct sock *sk) |
516 | { | |
9a4139a7 AA |
517 | if (!dlm_allow_conn) |
518 | return; | |
519 | ||
d11ccd45 AA |
520 | queue_work(recv_workqueue, &listen_con.rwork); |
521 | } | |
522 | ||
fdda387f PC |
523 | static void lowcomms_write_space(struct sock *sk) |
524 | { | |
93eaadeb | 525 | struct connection *con; |
fdda387f | 526 | |
93eaadeb | 527 | con = sock2con(sk); |
b36930dd | 528 | if (!con) |
92c44605 | 529 | return; |
b36930dd | 530 | |
19633c7e | 531 | if (!test_and_set_bit(CF_CONNECTED, &con->flags)) { |
dfc020f3 | 532 | log_print("connected to node %d", con->nodeid); |
19633c7e | 533 | queue_work(send_workqueue, &con->swork); |
92c44605 | 534 | return; |
19633c7e AA |
535 | } |
536 | ||
b36930dd DM |
537 | 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--; | |
9cd3e072 | 541 | clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags); |
b36930dd DM |
542 | } |
543 | ||
01da24d3 | 544 | queue_work(send_workqueue, &con->swork); |
fdda387f PC |
545 | } |
546 | ||
547 | static inline void lowcomms_connect_sock(struct connection *con) | |
548 | { | |
063c4c99 LMB |
549 | if (test_bit(CF_CLOSE, &con->flags)) |
550 | return; | |
61d9102b BP |
551 | queue_work(send_workqueue, &con->swork); |
552 | cond_resched(); | |
fdda387f PC |
553 | } |
554 | ||
555 | static void lowcomms_state_change(struct sock *sk) | |
556 | { | |
ee44b4bc MRL |
557 | /* 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) { | |
fdda387f | 566 | lowcomms_write_space(sk); |
ee44b4bc | 567 | } |
fdda387f PC |
568 | } |
569 | ||
391fbdc5 CC |
570 | int dlm_lowcomms_connect_node(int nodeid) |
571 | { | |
572 | struct connection *con; | |
b38bc9c2 | 573 | int idx; |
391fbdc5 CC |
574 | |
575 | if (nodeid == dlm_our_nodeid()) | |
576 | return 0; | |
577 | ||
b38bc9c2 | 578 | idx = srcu_read_lock(&connections_srcu); |
391fbdc5 | 579 | con = nodeid2con(nodeid, GFP_NOFS); |
b38bc9c2 AA |
580 | if (!con) { |
581 | srcu_read_unlock(&connections_srcu, idx); | |
391fbdc5 | 582 | return -ENOMEM; |
b38bc9c2 AA |
583 | } |
584 | ||
391fbdc5 | 585 | lowcomms_connect_sock(con); |
b38bc9c2 AA |
586 | srcu_read_unlock(&connections_srcu, idx); |
587 | ||
391fbdc5 CC |
588 | return 0; |
589 | } | |
590 | ||
e125fbeb AA |
591 | int 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 | ||
b3a5bbfd BP |
608 | static void lowcomms_error_report(struct sock *sk) |
609 | { | |
b81171cb | 610 | struct connection *con; |
b81171cb | 611 | void (*orig_report)(struct sock *) = NULL; |
4c3d9057 | 612 | struct inet_sock *inet; |
b3a5bbfd | 613 | |
b81171cb BP |
614 | con = sock2con(sk); |
615 | if (con == NULL) | |
616 | goto out; | |
617 | ||
cc661fc9 | 618 | orig_report = listen_sock.sk_error_report; |
b3a5bbfd | 619 | |
4c3d9057 AA |
620 | inet = inet_sk(sk); |
621 | switch (sk->sk_family) { | |
622 | case AF_INET: | |
b3a5bbfd | 623 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
4c3d9057 | 624 | "sending to node %d at %pI4, dport %d, " |
b3a5bbfd | 625 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
4c3d9057 AA |
626 | con->nodeid, &inet->inet_daddr, |
627 | ntohs(inet->inet_dport), sk->sk_err, | |
b3a5bbfd | 628 | sk->sk_err_soft); |
4c3d9057 | 629 | break; |
1b9beda8 | 630 | #if IS_ENABLED(CONFIG_IPV6) |
4c3d9057 | 631 | case AF_INET6: |
b3a5bbfd | 632 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
4c3d9057 AA |
633 | "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, | |
b3a5bbfd | 637 | sk->sk_err_soft); |
4c3d9057 | 638 | break; |
1b9beda8 | 639 | #endif |
4c3d9057 AA |
640 | 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; | |
b3a5bbfd | 646 | } |
ba868d9d AA |
647 | |
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 | ||
b81171cb | 663 | out: |
b81171cb BP |
664 | if (orig_report) |
665 | orig_report(sk); | |
666 | } | |
667 | ||
668 | /* Note: sk_callback_lock must be locked before calling this function. */ | |
cc661fc9 | 669 | static void save_listen_callbacks(struct socket *sock) |
b81171cb | 670 | { |
cc661fc9 BP |
671 | 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; | |
b81171cb BP |
677 | } |
678 | ||
cc661fc9 | 679 | static void restore_callbacks(struct socket *sock) |
b81171cb | 680 | { |
cc661fc9 BP |
681 | struct sock *sk = sock->sk; |
682 | ||
92c44605 | 683 | lock_sock(sk); |
b81171cb | 684 | sk->sk_user_data = NULL; |
cc661fc9 BP |
685 | 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; | |
92c44605 | 689 | release_sock(sk); |
b3a5bbfd BP |
690 | } |
691 | ||
d11ccd45 AA |
692 | static void add_listen_sock(struct socket *sock, struct listen_connection *con) |
693 | { | |
694 | struct sock *sk = sock->sk; | |
695 | ||
92c44605 | 696 | lock_sock(sk); |
d11ccd45 AA |
697 | 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; | |
92c44605 | 704 | release_sock(sk); |
d11ccd45 AA |
705 | } |
706 | ||
fdda387f | 707 | /* Make a socket active */ |
988419a9 | 708 | static void add_sock(struct socket *sock, struct connection *con) |
fdda387f | 709 | { |
b81171cb BP |
710 | struct sock *sk = sock->sk; |
711 | ||
92c44605 | 712 | lock_sock(sk); |
fdda387f PC |
713 | con->sock = sock; |
714 | ||
b81171cb | 715 | sk->sk_user_data = con; |
fdda387f | 716 | /* Install a data_ready callback */ |
b81171cb BP |
717 | 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; | |
92c44605 | 722 | release_sock(sk); |
fdda387f PC |
723 | } |
724 | ||
6ed7257b | 725 | /* Add the port number to an IPv6 or 4 sockaddr and return the address |
fdda387f PC |
726 | length */ |
727 | static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, | |
728 | int *addr_len) | |
729 | { | |
6ed7257b | 730 | saddr->ss_family = dlm_local_addr[0]->ss_family; |
ac33d071 | 731 | if (saddr->ss_family == AF_INET) { |
fdda387f PC |
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); | |
6ed7257b | 735 | memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero)); |
ac33d071 | 736 | } else { |
fdda387f PC |
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 | } | |
01c8cab2 | 741 | memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len); |
fdda387f PC |
742 | } |
743 | ||
706474fb AA |
744 | static 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); | |
3af2326c | 750 | dlm_free_writequeue(e); |
706474fb AA |
751 | } |
752 | ||
753 | static 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); | |
e4dc81ed | 758 | dlm_free_msg(msg); |
706474fb AA |
759 | } |
760 | ||
761 | static 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 | ||
d11ccd45 AA |
780 | static 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 | ||
fdda387f | 789 | /* Close a remote connection and tidy up */ |
0d737a8c MRL |
790 | static void close_connection(struct connection *con, bool and_other, |
791 | bool tx, bool rx) | |
fdda387f | 792 | { |
b2a66629 | 793 | bool closing = test_and_set_bit(CF_CLOSING, &con->flags); |
706474fb | 794 | struct writequeue_entry *e; |
b2a66629 | 795 | |
0aa18464 | 796 | if (tx && !closing && cancel_work_sync(&con->swork)) { |
0d737a8c | 797 | log_print("canceled swork for node %d", con->nodeid); |
0aa18464 | 798 | clear_bit(CF_WRITE_PENDING, &con->flags); |
799 | } | |
800 | if (rx && !closing && cancel_work_sync(&con->rwork)) { | |
0d737a8c | 801 | log_print("canceled rwork for node %d", con->nodeid); |
0aa18464 | 802 | clear_bit(CF_READ_PENDING, &con->flags); |
803 | } | |
fdda387f | 804 | |
0d737a8c | 805 | mutex_lock(&con->sock_mutex); |
d11ccd45 AA |
806 | dlm_close_sock(&con->sock); |
807 | ||
fdda387f | 808 | if (con->othercon && and_other) { |
ac33d071 | 809 | /* Will only re-enter once. */ |
c6aa00e3 | 810 | close_connection(con->othercon, false, tx, rx); |
fdda387f | 811 | } |
9e5f2825 | 812 | |
706474fb AA |
813 | /* 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 | ||
4798cbbf | 833 | con->rx_leftover = 0; |
61d96be0 | 834 | con->retries = 0; |
052849be | 835 | clear_bit(CF_APP_LIMITED, &con->flags); |
19633c7e | 836 | clear_bit(CF_CONNECTED, &con->flags); |
ba868d9d AA |
837 | clear_bit(CF_DELAY_CONNECT, &con->flags); |
838 | clear_bit(CF_RECONNECT, &con->flags); | |
8aa31cbf | 839 | clear_bit(CF_EOF, &con->flags); |
61d96be0 | 840 | mutex_unlock(&con->sock_mutex); |
b2a66629 | 841 | clear_bit(CF_CLOSING, &con->flags); |
fdda387f PC |
842 | } |
843 | ||
055923bf AA |
844 | static void shutdown_connection(struct connection *con) |
845 | { | |
846 | int ret; | |
847 | ||
eec054b5 | 848 | flush_work(&con->swork); |
055923bf AA |
849 | |
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 | ||
877 | force_close: | |
878 | clear_bit(CF_SHUTDOWN, &con->flags); | |
879 | close_connection(con, false, true, true); | |
880 | } | |
881 | ||
882 | static void dlm_tcp_shutdown(struct connection *con) | |
883 | { | |
884 | if (con->othercon) | |
885 | shutdown_connection(con->othercon); | |
886 | shutdown_connection(con); | |
887 | } | |
888 | ||
4798cbbf AA |
889 | static 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 | ||
fdda387f PC |
909 | /* Data received from remote end */ |
910 | static int receive_from_sock(struct connection *con) | |
911 | { | |
4798cbbf AA |
912 | struct msghdr msg; |
913 | struct kvec iov; | |
914 | int ret, buflen; | |
fdda387f | 915 | |
f1f1c1cc | 916 | mutex_lock(&con->sock_mutex); |
fdda387f | 917 | |
a34fbc63 PC |
918 | if (con->sock == NULL) { |
919 | ret = -EAGAIN; | |
920 | goto out_close; | |
921 | } | |
4798cbbf | 922 | |
4798cbbf AA |
923 | /* 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) | |
fdda387f | 928 | goto out_resched; |
fdda387f PC |
929 | } |
930 | ||
62699b3f AA |
931 | 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; | |
937 | ||
938 | 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); | |
92732376 | 942 | trace_dlm_recv(con->nodeid, ret); |
62699b3f AA |
943 | if (ret == -EAGAIN) |
944 | break; | |
945 | else if (ret <= 0) | |
946 | goto out_close; | |
fdda387f | 947 | |
62699b3f AA |
948 | /* 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; | |
953 | ||
954 | /* 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 | } | |
fdda387f PC |
963 | } |
964 | ||
b97f8525 | 965 | dlm_midcomms_receive_done(con->nodeid); |
f1f1c1cc | 966 | mutex_unlock(&con->sock_mutex); |
ac33d071 | 967 | return 0; |
fdda387f | 968 | |
ac33d071 | 969 | out_resched: |
1d6e8131 PC |
970 | if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) |
971 | queue_work(recv_workqueue, &con->rwork); | |
f1f1c1cc | 972 | mutex_unlock(&con->sock_mutex); |
bd44e2b0 | 973 | return -EAGAIN; |
fdda387f | 974 | |
ac33d071 | 975 | out_close: |
ba868d9d | 976 | if (ret == 0) { |
ba868d9d AA |
977 | log_print("connection %p got EOF from %d", |
978 | con, con->nodeid); | |
8aa31cbf | 979 | |
a66c008c AA |
980 | if (dlm_proto_ops->eof_condition && |
981 | dlm_proto_ops->eof_condition(con)) { | |
8aa31cbf AA |
982 | 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 | ||
ba868d9d AA |
993 | /* signal to breaking receive worker */ |
994 | ret = -1; | |
8aa31cbf AA |
995 | } else { |
996 | mutex_unlock(&con->sock_mutex); | |
fdda387f | 997 | } |
fdda387f PC |
998 | return ret; |
999 | } | |
1000 | ||
1001 | /* Listening socket is busy, accept a connection */ | |
d11ccd45 | 1002 | static int accept_from_sock(struct listen_connection *con) |
fdda387f PC |
1003 | { |
1004 | int result; | |
1005 | struct sockaddr_storage peeraddr; | |
1006 | struct socket *newsock; | |
b38bc9c2 | 1007 | int len, idx; |
fdda387f PC |
1008 | int nodeid; |
1009 | struct connection *newcon; | |
bd44e2b0 | 1010 | struct connection *addcon; |
3f78cd7d | 1011 | unsigned int mark; |
fdda387f | 1012 | |
d11ccd45 | 1013 | if (!con->sock) |
3421fb15 | 1014 | return -ENOTCONN; |
fdda387f | 1015 | |
3421fb15 | 1016 | result = kernel_accept(con->sock, &newsock, O_NONBLOCK); |
fdda387f PC |
1017 | if (result < 0) |
1018 | goto accept_err; | |
1019 | ||
1020 | /* Get the connected socket's peer */ | |
1021 | memset(&peeraddr, 0, sizeof(peeraddr)); | |
9b2c45d4 DV |
1022 | len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2); |
1023 | if (len < 0) { | |
fdda387f PC |
1024 | result = -ECONNABORTED; |
1025 | goto accept_err; | |
1026 | } | |
1027 | ||
1028 | /* Get the new node's NODEID */ | |
1029 | make_sockaddr(&peeraddr, 0, &len); | |
e125fbeb | 1030 | if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) { |
feae43f8 AA |
1031 | 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 | ||
fdda387f | 1053 | sock_release(newsock); |
fdda387f PC |
1054 | 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. | |
fdda387f PC |
1062 | * In this case we store the incoming one in "othercon" |
1063 | */ | |
b38bc9c2 | 1064 | idx = srcu_read_lock(&connections_srcu); |
748285cc | 1065 | newcon = nodeid2con(nodeid, GFP_NOFS); |
fdda387f | 1066 | if (!newcon) { |
b38bc9c2 | 1067 | srcu_read_unlock(&connections_srcu, idx); |
fdda387f PC |
1068 | result = -ENOMEM; |
1069 | goto accept_err; | |
1070 | } | |
d11ccd45 | 1071 | |
e125fbeb AA |
1072 | sock_set_mark(newsock->sk, mark); |
1073 | ||
d11ccd45 | 1074 | mutex_lock(&newcon->sock_mutex); |
fdda387f | 1075 | if (newcon->sock) { |
ac33d071 | 1076 | struct connection *othercon = newcon->othercon; |
fdda387f PC |
1077 | |
1078 | if (!othercon) { | |
a47666eb | 1079 | othercon = kzalloc(sizeof(*othercon), GFP_NOFS); |
fdda387f | 1080 | if (!othercon) { |
617e82e1 | 1081 | log_print("failed to allocate incoming socket"); |
f1f1c1cc | 1082 | mutex_unlock(&newcon->sock_mutex); |
b38bc9c2 | 1083 | srcu_read_unlock(&connections_srcu, idx); |
fdda387f PC |
1084 | result = -ENOMEM; |
1085 | goto accept_err; | |
1086 | } | |
4798cbbf | 1087 | |
6cde210a AA |
1088 | result = dlm_con_init(othercon, nodeid); |
1089 | if (result < 0) { | |
4798cbbf | 1090 | kfree(othercon); |
2fd8db2d | 1091 | mutex_unlock(&newcon->sock_mutex); |
b38bc9c2 | 1092 | srcu_read_unlock(&connections_srcu, idx); |
4798cbbf AA |
1093 | goto accept_err; |
1094 | } | |
1095 | ||
e9a470ac | 1096 | lockdep_set_subclass(&othercon->sock_mutex, 1); |
7443bc96 | 1097 | set_bit(CF_IS_OTHERCON, &othercon->flags); |
6cde210a | 1098 | newcon->othercon = othercon; |
ba868d9d | 1099 | othercon->sendcon = newcon; |
ba3ab3ca AA |
1100 | } else { |
1101 | /* close other sock con if we have something new */ | |
1102 | close_connection(othercon, false, true, false); | |
61d96be0 | 1103 | } |
ba3ab3ca | 1104 | |
e9a470ac | 1105 | mutex_lock(&othercon->sock_mutex); |
ba3ab3ca AA |
1106 | add_sock(newsock, othercon); |
1107 | addcon = othercon; | |
1108 | mutex_unlock(&othercon->sock_mutex); | |
fdda387f PC |
1109 | } |
1110 | else { | |
3735b4b9 BP |
1111 | /* 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. */ | |
988419a9 | 1114 | add_sock(newsock, newcon); |
bd44e2b0 | 1115 | addcon = newcon; |
fdda387f PC |
1116 | } |
1117 | ||
b30a624f | 1118 | set_bit(CF_CONNECTED, &addcon->flags); |
f1f1c1cc | 1119 | mutex_unlock(&newcon->sock_mutex); |
fdda387f PC |
1120 | |
1121 | /* | |
1122 | * Add it to the active queue in case we got data | |
25985edc | 1123 | * between processing the accept adding the socket |
fdda387f PC |
1124 | * to the read_sockets list |
1125 | */ | |
bd44e2b0 PC |
1126 | if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) |
1127 | queue_work(recv_workqueue, &addcon->rwork); | |
fdda387f | 1128 | |
b38bc9c2 AA |
1129 | srcu_read_unlock(&connections_srcu, idx); |
1130 | ||
fdda387f PC |
1131 | return 0; |
1132 | ||
ac33d071 | 1133 | accept_err: |
3421fb15 | 1134 | if (newsock) |
1135 | sock_release(newsock); | |
fdda387f PC |
1136 | |
1137 | if (result != -EAGAIN) | |
617e82e1 | 1138 | log_print("error accepting connection from node: %d", result); |
fdda387f PC |
1139 | return result; |
1140 | } | |
1141 | ||
5d689871 MC |
1142 | /* |
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 | */ | |
1149 | static void writequeue_entry_complete(struct writequeue_entry *e, int completed) | |
1150 | { | |
1151 | e->offset += completed; | |
1152 | e->len -= completed; | |
706474fb AA |
1153 | /* signal that page was half way transmitted */ |
1154 | e->dirty = true; | |
5d689871 | 1155 | |
8f2dc78d | 1156 | if (e->len == 0 && e->users == 0) |
5d689871 | 1157 | free_entry(e); |
5d689871 MC |
1158 | } |
1159 | ||
ee44b4bc MRL |
1160 | /* |
1161 | * sctp_bind_addrs - bind a SCTP socket to all our addresses | |
1162 | */ | |
13004e8a | 1163 | static int sctp_bind_addrs(struct socket *sock, uint16_t port) |
ee44b4bc MRL |
1164 | { |
1165 | struct sockaddr_storage localaddr; | |
c0425a42 | 1166 | struct sockaddr *addr = (struct sockaddr *)&localaddr; |
ee44b4bc MRL |
1167 | 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) | |
13004e8a | 1174 | result = kernel_bind(sock, addr, addr_len); |
ee44b4bc | 1175 | else |
13004e8a | 1176 | result = sock_bind_add(sock->sk, addr, addr_len); |
ee44b4bc MRL |
1177 | |
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 | ||
6ed7257b PC |
1187 | /* Get local addresses */ |
1188 | static void init_local(void) | |
1189 | { | |
1190 | struct sockaddr_storage sas, *addr; | |
1191 | int i; | |
1192 | ||
30d3a237 | 1193 | dlm_local_count = 0; |
1b189b88 | 1194 | for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) { |
6ed7257b PC |
1195 | if (dlm_our_addr(&sas, i)) |
1196 | break; | |
1197 | ||
5c93f56f | 1198 | addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS); |
6ed7257b PC |
1199 | if (!addr) |
1200 | break; | |
6ed7257b PC |
1201 | dlm_local_addr[dlm_local_count++] = addr; |
1202 | } | |
1203 | } | |
1204 | ||
043697f0 AA |
1205 | static 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 | ||
be3b0400 | 1213 | static struct writequeue_entry *new_writequeue_entry(struct connection *con) |
fdda387f PC |
1214 | { |
1215 | struct writequeue_entry *entry; | |
1216 | ||
3af2326c | 1217 | entry = dlm_allocate_writequeue(); |
fdda387f PC |
1218 | if (!entry) |
1219 | return NULL; | |
1220 | ||
be3b0400 | 1221 | entry->page = alloc_page(GFP_ATOMIC | __GFP_ZERO); |
fdda387f | 1222 | if (!entry->page) { |
3af2326c | 1223 | dlm_free_writequeue(entry); |
fdda387f PC |
1224 | return NULL; |
1225 | } | |
1226 | ||
3af2326c AA |
1227 | entry->offset = 0; |
1228 | entry->len = 0; | |
1229 | entry->end = 0; | |
1230 | entry->dirty = false; | |
fdda387f | 1231 | entry->con = con; |
f0747ebf | 1232 | entry->users = 1; |
8f2dc78d | 1233 | kref_init(&entry->ref); |
fdda387f PC |
1234 | return entry; |
1235 | } | |
1236 | ||
f0747ebf | 1237 | static struct writequeue_entry *new_wq_entry(struct connection *con, int len, |
be3b0400 AA |
1238 | char **ppc, void (*cb)(void *data), |
1239 | void *data) | |
f0747ebf AA |
1240 | { |
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) { | |
8f2dc78d AA |
1247 | kref_get(&e->ref); |
1248 | ||
f0747ebf | 1249 | *ppc = page_address(e->page) + e->end; |
8f2dc78d | 1250 | if (cb) |
5c16febb | 1251 | cb(data); |
8f2dc78d | 1252 | |
f0747ebf AA |
1253 | e->end += len; |
1254 | e->users++; | |
be3b0400 | 1255 | goto out; |
f0747ebf AA |
1256 | } |
1257 | } | |
f0747ebf | 1258 | |
be3b0400 | 1259 | e = new_writequeue_entry(con); |
f0747ebf | 1260 | if (!e) |
be3b0400 | 1261 | goto out; |
f0747ebf | 1262 | |
8f2dc78d | 1263 | kref_get(&e->ref); |
f0747ebf AA |
1264 | *ppc = page_address(e->page); |
1265 | e->end += len; | |
8aa31cbf | 1266 | atomic_inc(&con->writequeue_cnt); |
8f2dc78d | 1267 | if (cb) |
5c16febb | 1268 | cb(data); |
8f2dc78d | 1269 | |
f0747ebf | 1270 | list_add_tail(&e->list, &con->writequeue); |
f0747ebf | 1271 | |
be3b0400 AA |
1272 | out: |
1273 | spin_unlock(&con->writequeue_lock); | |
f0747ebf AA |
1274 | return e; |
1275 | }; | |
1276 | ||
2874d1a6 AA |
1277 | static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len, |
1278 | gfp_t allocation, char **ppc, | |
5c16febb AA |
1279 | void (*cb)(void *data), |
1280 | void *data) | |
2874d1a6 AA |
1281 | { |
1282 | struct writequeue_entry *e; | |
1283 | struct dlm_msg *msg; | |
1284 | ||
e4dc81ed | 1285 | msg = dlm_allocate_msg(allocation); |
2874d1a6 AA |
1286 | if (!msg) |
1287 | return NULL; | |
1288 | ||
1289 | kref_init(&msg->ref); | |
1290 | ||
be3b0400 | 1291 | e = new_wq_entry(con, len, ppc, cb, data); |
2874d1a6 | 1292 | if (!e) { |
e4dc81ed | 1293 | dlm_free_msg(msg); |
2874d1a6 AA |
1294 | return NULL; |
1295 | } | |
1296 | ||
e4dc81ed AA |
1297 | msg->retransmit = false; |
1298 | msg->orig_msg = NULL; | |
2874d1a6 AA |
1299 | msg->ppc = *ppc; |
1300 | msg->len = len; | |
1301 | msg->entry = e; | |
1302 | ||
1303 | return msg; | |
1304 | } | |
1305 | ||
a8449f23 AA |
1306 | /* avoid false positive for nodes_srcu, unlock happens in |
1307 | * dlm_lowcomms_commit_msg which is a must call if success | |
1308 | */ | |
1309 | #ifndef __CHECKER__ | |
8f2dc78d | 1310 | struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation, |
5c16febb AA |
1311 | char **ppc, void (*cb)(void *data), |
1312 | void *data) | |
fdda387f PC |
1313 | { |
1314 | struct connection *con; | |
8f2dc78d | 1315 | struct dlm_msg *msg; |
b38bc9c2 | 1316 | int idx; |
fdda387f | 1317 | |
d10a0b88 | 1318 | if (len > DLM_MAX_SOCKET_BUFSIZE || |
c45674fb | 1319 | len < sizeof(struct dlm_header)) { |
d10a0b88 | 1320 | BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE); |
692f51c8 | 1321 | log_print("failed to allocate a buffer of size %d", len); |
c45674fb | 1322 | WARN_ON(1); |
692f51c8 AA |
1323 | return NULL; |
1324 | } | |
1325 | ||
b38bc9c2 | 1326 | idx = srcu_read_lock(&connections_srcu); |
fdda387f | 1327 | con = nodeid2con(nodeid, allocation); |
b38bc9c2 AA |
1328 | if (!con) { |
1329 | srcu_read_unlock(&connections_srcu, idx); | |
fdda387f | 1330 | return NULL; |
b38bc9c2 AA |
1331 | } |
1332 | ||
5c16febb | 1333 | msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data); |
8f2dc78d AA |
1334 | if (!msg) { |
1335 | srcu_read_unlock(&connections_srcu, idx); | |
1336 | return NULL; | |
1337 | } | |
1338 | ||
b38bc9c2 | 1339 | /* we assume if successful commit must called */ |
8f2dc78d | 1340 | msg->idx = idx; |
8f2dc78d | 1341 | return msg; |
fdda387f | 1342 | } |
a8449f23 | 1343 | #endif |
fdda387f | 1344 | |
2874d1a6 | 1345 | static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg) |
fdda387f | 1346 | { |
8f2dc78d | 1347 | struct writequeue_entry *e = msg->entry; |
fdda387f PC |
1348 | struct connection *con = e->con; |
1349 | int users; | |
1350 | ||
4edde74e | 1351 | spin_lock(&con->writequeue_lock); |
8f2dc78d AA |
1352 | kref_get(&msg->ref); |
1353 | list_add(&msg->list, &e->msgs); | |
1354 | ||
fdda387f PC |
1355 | users = --e->users; |
1356 | if (users) | |
1357 | goto out; | |
f0747ebf AA |
1358 | |
1359 | e->len = DLM_WQ_LENGTH_BYTES(e); | |
fdda387f PC |
1360 | spin_unlock(&con->writequeue_lock); |
1361 | ||
01da24d3 | 1362 | queue_work(send_workqueue, &con->swork); |
fdda387f PC |
1363 | return; |
1364 | ||
ac33d071 | 1365 | out: |
fdda387f PC |
1366 | spin_unlock(&con->writequeue_lock); |
1367 | return; | |
1368 | } | |
1369 | ||
a8449f23 AA |
1370 | /* avoid false positive for nodes_srcu, lock was happen in |
1371 | * dlm_lowcomms_new_msg | |
1372 | */ | |
1373 | #ifndef __CHECKER__ | |
2874d1a6 AA |
1374 | void dlm_lowcomms_commit_msg(struct dlm_msg *msg) |
1375 | { | |
1376 | _dlm_lowcomms_commit_msg(msg); | |
1377 | srcu_read_unlock(&connections_srcu, msg->idx); | |
1378 | } | |
a8449f23 | 1379 | #endif |
2874d1a6 | 1380 | |
8f2dc78d AA |
1381 | void dlm_lowcomms_put_msg(struct dlm_msg *msg) |
1382 | { | |
1383 | kref_put(&msg->ref, dlm_msg_release); | |
1384 | } | |
1385 | ||
2874d1a6 AA |
1386 | /* does not held connections_srcu, usage workqueue only */ |
1387 | int dlm_lowcomms_resend_msg(struct dlm_msg *msg) | |
1388 | { | |
1389 | struct dlm_msg *msg_resend; | |
1390 | char *ppc; | |
1391 | ||
1392 | if (msg->retransmit) | |
1393 | return 1; | |
1394 | ||
1395 | msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len, | |
1396 | GFP_ATOMIC, &ppc, NULL, NULL); | |
1397 | if (!msg_resend) | |
1398 | return -ENOMEM; | |
1399 | ||
1400 | msg->retransmit = true; | |
1401 | kref_get(&msg->ref); | |
1402 | msg_resend->orig_msg = msg; | |
1403 | ||
1404 | memcpy(ppc, msg->ppc, msg->len); | |
1405 | _dlm_lowcomms_commit_msg(msg_resend); | |
1406 | dlm_lowcomms_put_msg(msg_resend); | |
1407 | ||
1408 | return 0; | |
1409 | } | |
1410 | ||
fdda387f | 1411 | /* Send a message */ |
ac33d071 | 1412 | static void send_to_sock(struct connection *con) |
fdda387f | 1413 | { |
fdda387f PC |
1414 | const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
1415 | struct writequeue_entry *e; | |
66d5955a | 1416 | int len, offset, ret; |
f92c8dd7 | 1417 | int count = 0; |
fdda387f | 1418 | |
f1f1c1cc | 1419 | mutex_lock(&con->sock_mutex); |
fdda387f PC |
1420 | if (con->sock == NULL) |
1421 | goto out_connect; | |
1422 | ||
fdda387f PC |
1423 | spin_lock(&con->writequeue_lock); |
1424 | for (;;) { | |
66d5955a AA |
1425 | e = con_next_wq(con); |
1426 | if (!e) | |
fdda387f PC |
1427 | break; |
1428 | ||
1429 | len = e->len; | |
1430 | offset = e->offset; | |
1431 | BUG_ON(len == 0 && e->users == 0); | |
1432 | spin_unlock(&con->writequeue_lock); | |
1433 | ||
66d5955a AA |
1434 | ret = kernel_sendpage(con->sock, e->page, offset, len, |
1435 | msg_flags); | |
92732376 | 1436 | trace_dlm_send(con->nodeid, ret); |
66d5955a AA |
1437 | if (ret == -EAGAIN || ret == 0) { |
1438 | if (ret == -EAGAIN && | |
1439 | test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) && | |
1440 | !test_and_set_bit(CF_APP_LIMITED, &con->flags)) { | |
1441 | /* Notify TCP that we're limited by the | |
1442 | * application window size. | |
1443 | */ | |
1444 | set_bit(SOCK_NOSPACE, &con->sock->flags); | |
1445 | con->sock->sk->sk_write_pending++; | |
1446 | } | |
1447 | cond_resched(); | |
1448 | goto out; | |
1449 | } else if (ret < 0) | |
1450 | goto out; | |
f92c8dd7 BP |
1451 | |
1452 | /* Don't starve people filling buffers */ | |
1453 | if (++count >= MAX_SEND_MSG_COUNT) { | |
ac33d071 | 1454 | cond_resched(); |
f92c8dd7 BP |
1455 | count = 0; |
1456 | } | |
fdda387f PC |
1457 | |
1458 | spin_lock(&con->writequeue_lock); | |
5d689871 | 1459 | writequeue_entry_complete(e, ret); |
fdda387f PC |
1460 | } |
1461 | spin_unlock(&con->writequeue_lock); | |
8aa31cbf AA |
1462 | |
1463 | /* close if we got EOF */ | |
1464 | if (test_and_clear_bit(CF_EOF, &con->flags)) { | |
1465 | mutex_unlock(&con->sock_mutex); | |
1466 | close_connection(con, false, false, true); | |
1467 | ||
1468 | /* handling for tcp shutdown */ | |
1469 | clear_bit(CF_SHUTDOWN, &con->flags); | |
1470 | wake_up(&con->shutdown_wait); | |
1471 | } else { | |
1472 | mutex_unlock(&con->sock_mutex); | |
1473 | } | |
1474 | ||
1475 | return; | |
1476 | ||
ac33d071 | 1477 | out: |
f1f1c1cc | 1478 | mutex_unlock(&con->sock_mutex); |
ac33d071 | 1479 | return; |
fdda387f | 1480 | |
ac33d071 | 1481 | out_connect: |
f1f1c1cc | 1482 | mutex_unlock(&con->sock_mutex); |
01da24d3 BP |
1483 | queue_work(send_workqueue, &con->swork); |
1484 | cond_resched(); | |
fdda387f PC |
1485 | } |
1486 | ||
1487 | static void clean_one_writequeue(struct connection *con) | |
1488 | { | |
5e9ccc37 | 1489 | struct writequeue_entry *e, *safe; |
fdda387f PC |
1490 | |
1491 | spin_lock(&con->writequeue_lock); | |
5e9ccc37 | 1492 | list_for_each_entry_safe(e, safe, &con->writequeue, list) { |
fdda387f PC |
1493 | free_entry(e); |
1494 | } | |
1495 | spin_unlock(&con->writequeue_lock); | |
1496 | } | |
1497 | ||
1498 | /* Called from recovery when it knows that a node has | |
1499 | left the cluster */ | |
1500 | int dlm_lowcomms_close(int nodeid) | |
1501 | { | |
1502 | struct connection *con; | |
36b71a8b | 1503 | struct dlm_node_addr *na; |
b38bc9c2 | 1504 | int idx; |
fdda387f | 1505 | |
fdda387f | 1506 | log_print("closing connection to node %d", nodeid); |
b38bc9c2 | 1507 | idx = srcu_read_lock(&connections_srcu); |
fdda387f PC |
1508 | con = nodeid2con(nodeid, 0); |
1509 | if (con) { | |
063c4c99 | 1510 | set_bit(CF_CLOSE, &con->flags); |
0d737a8c | 1511 | close_connection(con, true, true, true); |
fdda387f | 1512 | clean_one_writequeue(con); |
53a5edaa AA |
1513 | if (con->othercon) |
1514 | clean_one_writequeue(con->othercon); | |
fdda387f | 1515 | } |
b38bc9c2 | 1516 | srcu_read_unlock(&connections_srcu, idx); |
36b71a8b DT |
1517 | |
1518 | spin_lock(&dlm_node_addrs_spin); | |
1519 | na = find_node_addr(nodeid); | |
1520 | if (na) { | |
1521 | list_del(&na->list); | |
1522 | while (na->addr_count--) | |
1523 | kfree(na->addr[na->addr_count]); | |
1524 | kfree(na); | |
1525 | } | |
1526 | spin_unlock(&dlm_node_addrs_spin); | |
1527 | ||
fdda387f | 1528 | return 0; |
fdda387f PC |
1529 | } |
1530 | ||
6ed7257b | 1531 | /* Receive workqueue function */ |
1d6e8131 | 1532 | static void process_recv_sockets(struct work_struct *work) |
fdda387f | 1533 | { |
1d6e8131 | 1534 | struct connection *con = container_of(work, struct connection, rwork); |
fdda387f | 1535 | |
1d6e8131 | 1536 | clear_bit(CF_READ_PENDING, &con->flags); |
62699b3f | 1537 | receive_from_sock(con); |
fdda387f PC |
1538 | } |
1539 | ||
d11ccd45 AA |
1540 | static void process_listen_recv_socket(struct work_struct *work) |
1541 | { | |
1542 | accept_from_sock(&listen_con); | |
1543 | } | |
1544 | ||
8728a455 AA |
1545 | static void dlm_connect(struct connection *con) |
1546 | { | |
1547 | struct sockaddr_storage addr; | |
1548 | int result, addr_len; | |
1549 | struct socket *sock; | |
1550 | unsigned int mark; | |
1551 | ||
1552 | /* Some odd races can cause double-connects, ignore them */ | |
1553 | if (con->retries++ > MAX_CONNECT_RETRIES) | |
1554 | return; | |
1555 | ||
1556 | if (con->sock) { | |
1557 | log_print("node %d already connected.", con->nodeid); | |
1558 | return; | |
1559 | } | |
1560 | ||
1561 | memset(&addr, 0, sizeof(addr)); | |
1562 | result = nodeid_to_addr(con->nodeid, &addr, NULL, | |
1563 | dlm_proto_ops->try_new_addr, &mark); | |
1564 | if (result < 0) { | |
1565 | log_print("no address for nodeid %d", con->nodeid); | |
1566 | return; | |
1567 | } | |
1568 | ||
1569 | /* Create a socket to communicate with */ | |
1570 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, | |
1571 | SOCK_STREAM, dlm_proto_ops->proto, &sock); | |
1572 | if (result < 0) | |
1573 | goto socket_err; | |
1574 | ||
1575 | sock_set_mark(sock->sk, mark); | |
1576 | dlm_proto_ops->sockopts(sock); | |
1577 | ||
1578 | add_sock(sock, con); | |
1579 | ||
1580 | result = dlm_proto_ops->bind(sock); | |
1581 | if (result < 0) | |
1582 | goto add_sock_err; | |
1583 | ||
1584 | log_print_ratelimited("connecting to %d", con->nodeid); | |
1585 | make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len); | |
1586 | result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr, | |
1587 | addr_len); | |
1588 | if (result < 0) | |
1589 | goto add_sock_err; | |
1590 | ||
1591 | return; | |
1592 | ||
1593 | add_sock_err: | |
1594 | dlm_close_sock(&con->sock); | |
1595 | ||
1596 | socket_err: | |
1597 | /* | |
1598 | * Some errors are fatal and this list might need adjusting. For other | |
1599 | * errors we try again until the max number of retries is reached. | |
1600 | */ | |
1601 | if (result != -EHOSTUNREACH && | |
1602 | result != -ENETUNREACH && | |
1603 | result != -ENETDOWN && | |
1604 | result != -EINVAL && | |
1605 | result != -EPROTONOSUPPORT) { | |
1606 | log_print("connect %d try %d error %d", con->nodeid, | |
1607 | con->retries, result); | |
1608 | msleep(1000); | |
1609 | lowcomms_connect_sock(con); | |
1610 | } | |
1611 | } | |
1612 | ||
6ed7257b | 1613 | /* Send workqueue function */ |
1d6e8131 | 1614 | static void process_send_sockets(struct work_struct *work) |
fdda387f | 1615 | { |
1d6e8131 | 1616 | struct connection *con = container_of(work, struct connection, swork); |
fdda387f | 1617 | |
7443bc96 AA |
1618 | WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags)); |
1619 | ||
8a4abb08 | 1620 | clear_bit(CF_WRITE_PENDING, &con->flags); |
ba868d9d | 1621 | |
489d8e55 | 1622 | if (test_and_clear_bit(CF_RECONNECT, &con->flags)) { |
ba868d9d | 1623 | close_connection(con, false, false, true); |
489d8e55 AA |
1624 | dlm_midcomms_unack_msg_resend(con->nodeid); |
1625 | } | |
ba868d9d | 1626 | |
8728a455 | 1627 | if (con->sock == NULL) { |
ba868d9d AA |
1628 | if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags)) |
1629 | msleep(1000); | |
8728a455 AA |
1630 | |
1631 | mutex_lock(&con->sock_mutex); | |
1632 | dlm_connect(con); | |
1633 | mutex_unlock(&con->sock_mutex); | |
ba868d9d | 1634 | } |
8728a455 | 1635 | |
01da24d3 | 1636 | if (!list_empty(&con->writequeue)) |
063c4c99 | 1637 | send_to_sock(con); |
fdda387f PC |
1638 | } |
1639 | ||
1d6e8131 | 1640 | static void work_stop(void) |
fdda387f | 1641 | { |
fcef0e6c | 1642 | if (recv_workqueue) { |
b355516f | 1643 | destroy_workqueue(recv_workqueue); |
fcef0e6c AA |
1644 | recv_workqueue = NULL; |
1645 | } | |
1646 | ||
1647 | if (send_workqueue) { | |
b355516f | 1648 | destroy_workqueue(send_workqueue); |
fcef0e6c AA |
1649 | send_workqueue = NULL; |
1650 | } | |
fdda387f PC |
1651 | } |
1652 | ||
1d6e8131 | 1653 | static int work_start(void) |
fdda387f | 1654 | { |
6c6a1cc6 | 1655 | recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM); |
b9d41052 NK |
1656 | if (!recv_workqueue) { |
1657 | log_print("can't start dlm_recv"); | |
1658 | return -ENOMEM; | |
fdda387f | 1659 | } |
fdda387f | 1660 | |
6c6a1cc6 | 1661 | send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM); |
b9d41052 NK |
1662 | if (!send_workqueue) { |
1663 | log_print("can't start dlm_send"); | |
1d6e8131 | 1664 | destroy_workqueue(recv_workqueue); |
fcef0e6c | 1665 | recv_workqueue = NULL; |
b9d41052 | 1666 | return -ENOMEM; |
fdda387f | 1667 | } |
fdda387f PC |
1668 | |
1669 | return 0; | |
1670 | } | |
1671 | ||
9d232469 AA |
1672 | static void shutdown_conn(struct connection *con) |
1673 | { | |
a66c008c AA |
1674 | if (dlm_proto_ops->shutdown_action) |
1675 | dlm_proto_ops->shutdown_action(con); | |
9d232469 AA |
1676 | } |
1677 | ||
1678 | void dlm_lowcomms_shutdown(void) | |
1679 | { | |
b38bc9c2 AA |
1680 | int idx; |
1681 | ||
9d232469 AA |
1682 | /* Set all the flags to prevent any |
1683 | * socket activity. | |
1684 | */ | |
1685 | dlm_allow_conn = 0; | |
1686 | ||
1687 | if (recv_workqueue) | |
1688 | flush_workqueue(recv_workqueue); | |
1689 | if (send_workqueue) | |
1690 | flush_workqueue(send_workqueue); | |
1691 | ||
1692 | dlm_close_sock(&listen_con.sock); | |
1693 | ||
b38bc9c2 | 1694 | idx = srcu_read_lock(&connections_srcu); |
9d232469 | 1695 | foreach_conn(shutdown_conn); |
b38bc9c2 | 1696 | srcu_read_unlock(&connections_srcu, idx); |
9d232469 AA |
1697 | } |
1698 | ||
f0fb83cb | 1699 | static void _stop_conn(struct connection *con, bool and_other) |
fdda387f | 1700 | { |
f0fb83cb | 1701 | mutex_lock(&con->sock_mutex); |
173a31fe | 1702 | set_bit(CF_CLOSE, &con->flags); |
f0fb83cb | 1703 | set_bit(CF_READ_PENDING, &con->flags); |
8a4abb08 | 1704 | set_bit(CF_WRITE_PENDING, &con->flags); |
93eaadeb | 1705 | if (con->sock && con->sock->sk) { |
92c44605 | 1706 | lock_sock(con->sock->sk); |
5e9ccc37 | 1707 | con->sock->sk->sk_user_data = NULL; |
92c44605 | 1708 | release_sock(con->sock->sk); |
93eaadeb | 1709 | } |
f0fb83cb | 1710 | if (con->othercon && and_other) |
1711 | _stop_conn(con->othercon, false); | |
1712 | mutex_unlock(&con->sock_mutex); | |
1713 | } | |
1714 | ||
1715 | static void stop_conn(struct connection *con) | |
1716 | { | |
1717 | _stop_conn(con, true); | |
5e9ccc37 | 1718 | } |
fdda387f | 1719 | |
4798cbbf AA |
1720 | static void connection_release(struct rcu_head *rcu) |
1721 | { | |
1722 | struct connection *con = container_of(rcu, struct connection, rcu); | |
1723 | ||
1724 | kfree(con->rx_buf); | |
1725 | kfree(con); | |
1726 | } | |
1727 | ||
5e9ccc37 CC |
1728 | static void free_conn(struct connection *con) |
1729 | { | |
0d737a8c | 1730 | close_connection(con, true, true, true); |
a47666eb AA |
1731 | spin_lock(&connections_lock); |
1732 | hlist_del_rcu(&con->list); | |
1733 | spin_unlock(&connections_lock); | |
948c47e9 AA |
1734 | if (con->othercon) { |
1735 | clean_one_writequeue(con->othercon); | |
5cbec208 AA |
1736 | call_srcu(&connections_srcu, &con->othercon->rcu, |
1737 | connection_release); | |
948c47e9 | 1738 | } |
0de98432 | 1739 | clean_one_writequeue(con); |
5cbec208 | 1740 | call_srcu(&connections_srcu, &con->rcu, connection_release); |
5e9ccc37 CC |
1741 | } |
1742 | ||
f0fb83cb | 1743 | static void work_flush(void) |
1744 | { | |
b38bc9c2 | 1745 | int ok; |
f0fb83cb | 1746 | int i; |
f0fb83cb | 1747 | struct connection *con; |
1748 | ||
f0fb83cb | 1749 | do { |
1750 | ok = 1; | |
1751 | foreach_conn(stop_conn); | |
b355516f DW |
1752 | if (recv_workqueue) |
1753 | flush_workqueue(recv_workqueue); | |
1754 | if (send_workqueue) | |
1755 | flush_workqueue(send_workqueue); | |
f0fb83cb | 1756 | for (i = 0; i < CONN_HASH_SIZE && ok; i++) { |
a47666eb AA |
1757 | hlist_for_each_entry_rcu(con, &connection_hash[i], |
1758 | list) { | |
f0fb83cb | 1759 | ok &= test_bit(CF_READ_PENDING, &con->flags); |
8a4abb08 | 1760 | ok &= test_bit(CF_WRITE_PENDING, &con->flags); |
1761 | if (con->othercon) { | |
f0fb83cb | 1762 | ok &= test_bit(CF_READ_PENDING, |
1763 | &con->othercon->flags); | |
8a4abb08 | 1764 | ok &= test_bit(CF_WRITE_PENDING, |
1765 | &con->othercon->flags); | |
1766 | } | |
f0fb83cb | 1767 | } |
1768 | } | |
1769 | } while (!ok); | |
1770 | } | |
1771 | ||
5e9ccc37 CC |
1772 | void dlm_lowcomms_stop(void) |
1773 | { | |
b38bc9c2 AA |
1774 | int idx; |
1775 | ||
1776 | idx = srcu_read_lock(&connections_srcu); | |
f0fb83cb | 1777 | work_flush(); |
3a8db798 | 1778 | foreach_conn(free_conn); |
b38bc9c2 | 1779 | srcu_read_unlock(&connections_srcu, idx); |
1d6e8131 | 1780 | work_stop(); |
043697f0 | 1781 | deinit_local(); |
a66c008c AA |
1782 | |
1783 | dlm_proto_ops = NULL; | |
fdda387f PC |
1784 | } |
1785 | ||
2dc6b115 AA |
1786 | static int dlm_listen_for_all(void) |
1787 | { | |
1788 | struct socket *sock; | |
1789 | int result; | |
1790 | ||
1791 | log_print("Using %s for communications", | |
1792 | dlm_proto_ops->name); | |
1793 | ||
90d21fc0 AA |
1794 | result = dlm_proto_ops->listen_validate(); |
1795 | if (result < 0) | |
1796 | return result; | |
2dc6b115 AA |
1797 | |
1798 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, | |
1799 | SOCK_STREAM, dlm_proto_ops->proto, &sock); | |
1800 | if (result < 0) { | |
fe933675 | 1801 | log_print("Can't create comms socket: %d", result); |
1f4f1084 | 1802 | return result; |
2dc6b115 AA |
1803 | } |
1804 | ||
1805 | sock_set_mark(sock->sk, dlm_config.ci_mark); | |
1806 | dlm_proto_ops->listen_sockopts(sock); | |
1807 | ||
1808 | result = dlm_proto_ops->listen_bind(sock); | |
1809 | if (result < 0) | |
1810 | goto out; | |
1811 | ||
1812 | save_listen_callbacks(sock); | |
1813 | add_listen_sock(sock, &listen_con); | |
1814 | ||
1815 | INIT_WORK(&listen_con.rwork, process_listen_recv_socket); | |
1816 | result = sock->ops->listen(sock, 5); | |
1817 | if (result < 0) { | |
1818 | dlm_close_sock(&listen_con.sock); | |
1819 | goto out; | |
1820 | } | |
1821 | ||
1822 | return 0; | |
1823 | ||
1824 | out: | |
1825 | sock_release(sock); | |
1826 | return result; | |
1827 | } | |
1828 | ||
8728a455 AA |
1829 | static int dlm_tcp_bind(struct socket *sock) |
1830 | { | |
1831 | struct sockaddr_storage src_addr; | |
1832 | int result, addr_len; | |
1833 | ||
1834 | /* Bind to our cluster-known address connecting to avoid | |
1835 | * routing problems. | |
1836 | */ | |
1837 | memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr)); | |
1838 | make_sockaddr(&src_addr, 0, &addr_len); | |
1839 | ||
1840 | result = sock->ops->bind(sock, (struct sockaddr *)&src_addr, | |
1841 | addr_len); | |
1842 | if (result < 0) { | |
1843 | /* This *may* not indicate a critical error */ | |
1844 | log_print("could not bind for connect: %d", result); | |
1845 | } | |
1846 | ||
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | static int dlm_tcp_connect(struct connection *con, struct socket *sock, | |
1851 | struct sockaddr *addr, int addr_len) | |
1852 | { | |
1853 | int ret; | |
1854 | ||
1855 | ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK); | |
1856 | switch (ret) { | |
1857 | case -EINPROGRESS: | |
1858 | fallthrough; | |
1859 | case 0: | |
1860 | return 0; | |
1861 | } | |
1862 | ||
1863 | return ret; | |
1864 | } | |
1865 | ||
2dc6b115 AA |
1866 | static int dlm_tcp_listen_validate(void) |
1867 | { | |
1868 | /* We don't support multi-homed hosts */ | |
1869 | if (dlm_local_count > 1) { | |
1870 | log_print("TCP protocol can't handle multi-homed hosts, try SCTP"); | |
1871 | return -EINVAL; | |
1872 | } | |
1873 | ||
1874 | return 0; | |
1875 | } | |
1876 | ||
1877 | static void dlm_tcp_sockopts(struct socket *sock) | |
1878 | { | |
1879 | /* Turn off Nagle's algorithm */ | |
1880 | tcp_sock_set_nodelay(sock->sk); | |
1881 | } | |
1882 | ||
1883 | static void dlm_tcp_listen_sockopts(struct socket *sock) | |
1884 | { | |
1885 | dlm_tcp_sockopts(sock); | |
1886 | sock_set_reuseaddr(sock->sk); | |
1887 | } | |
1888 | ||
1889 | static int dlm_tcp_listen_bind(struct socket *sock) | |
1890 | { | |
1891 | int addr_len; | |
1892 | ||
1893 | /* Bind to our port */ | |
1894 | make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len); | |
1895 | return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0], | |
1896 | addr_len); | |
1897 | } | |
1898 | ||
a66c008c | 1899 | static const struct dlm_proto_ops dlm_tcp_ops = { |
2dc6b115 AA |
1900 | .name = "TCP", |
1901 | .proto = IPPROTO_TCP, | |
8728a455 AA |
1902 | .connect = dlm_tcp_connect, |
1903 | .sockopts = dlm_tcp_sockopts, | |
1904 | .bind = dlm_tcp_bind, | |
2dc6b115 AA |
1905 | .listen_validate = dlm_tcp_listen_validate, |
1906 | .listen_sockopts = dlm_tcp_listen_sockopts, | |
1907 | .listen_bind = dlm_tcp_listen_bind, | |
a66c008c AA |
1908 | .shutdown_action = dlm_tcp_shutdown, |
1909 | .eof_condition = tcp_eof_condition, | |
1910 | }; | |
1911 | ||
8728a455 AA |
1912 | static int dlm_sctp_bind(struct socket *sock) |
1913 | { | |
1914 | return sctp_bind_addrs(sock, 0); | |
1915 | } | |
1916 | ||
1917 | static int dlm_sctp_connect(struct connection *con, struct socket *sock, | |
1918 | struct sockaddr *addr, int addr_len) | |
1919 | { | |
1920 | int ret; | |
1921 | ||
1922 | /* | |
1923 | * Make sock->ops->connect() function return in specified time, | |
1924 | * since O_NONBLOCK argument in connect() function does not work here, | |
1925 | * then, we should restore the default value of this attribute. | |
1926 | */ | |
1927 | sock_set_sndtimeo(sock->sk, 5); | |
1928 | ret = sock->ops->connect(sock, addr, addr_len, 0); | |
1929 | sock_set_sndtimeo(sock->sk, 0); | |
1930 | if (ret < 0) | |
1931 | return ret; | |
1932 | ||
1933 | if (!test_and_set_bit(CF_CONNECTED, &con->flags)) | |
dfc020f3 | 1934 | log_print("connected to node %d", con->nodeid); |
8728a455 AA |
1935 | |
1936 | return 0; | |
1937 | } | |
1938 | ||
90d21fc0 AA |
1939 | static int dlm_sctp_listen_validate(void) |
1940 | { | |
1941 | if (!IS_ENABLED(CONFIG_IP_SCTP)) { | |
1942 | log_print("SCTP is not enabled by this kernel"); | |
1943 | return -EOPNOTSUPP; | |
1944 | } | |
1945 | ||
1946 | request_module("sctp"); | |
1947 | return 0; | |
1948 | } | |
1949 | ||
2dc6b115 AA |
1950 | static int dlm_sctp_bind_listen(struct socket *sock) |
1951 | { | |
1952 | return sctp_bind_addrs(sock, dlm_config.ci_tcp_port); | |
1953 | } | |
1954 | ||
1955 | static void dlm_sctp_sockopts(struct socket *sock) | |
1956 | { | |
1957 | /* Turn off Nagle's algorithm */ | |
1958 | sctp_sock_set_nodelay(sock->sk); | |
1959 | sock_set_rcvbuf(sock->sk, NEEDED_RMEM); | |
1960 | } | |
1961 | ||
a66c008c | 1962 | static const struct dlm_proto_ops dlm_sctp_ops = { |
2dc6b115 AA |
1963 | .name = "SCTP", |
1964 | .proto = IPPROTO_SCTP, | |
8728a455 AA |
1965 | .try_new_addr = true, |
1966 | .connect = dlm_sctp_connect, | |
1967 | .sockopts = dlm_sctp_sockopts, | |
1968 | .bind = dlm_sctp_bind, | |
90d21fc0 | 1969 | .listen_validate = dlm_sctp_listen_validate, |
2dc6b115 AA |
1970 | .listen_sockopts = dlm_sctp_sockopts, |
1971 | .listen_bind = dlm_sctp_bind_listen, | |
a66c008c AA |
1972 | }; |
1973 | ||
fdda387f PC |
1974 | int dlm_lowcomms_start(void) |
1975 | { | |
6ed7257b | 1976 | int error = -EINVAL; |
5e9ccc37 CC |
1977 | int i; |
1978 | ||
1979 | for (i = 0; i < CONN_HASH_SIZE; i++) | |
1980 | INIT_HLIST_HEAD(&connection_hash[i]); | |
fdda387f | 1981 | |
6ed7257b PC |
1982 | init_local(); |
1983 | if (!dlm_local_count) { | |
617e82e1 | 1984 | error = -ENOTCONN; |
fdda387f | 1985 | log_print("no local IP address has been set"); |
513ef596 | 1986 | goto fail; |
fdda387f PC |
1987 | } |
1988 | ||
d11ccd45 AA |
1989 | INIT_WORK(&listen_con.rwork, process_listen_recv_socket); |
1990 | ||
513ef596 DT |
1991 | error = work_start(); |
1992 | if (error) | |
fcef0e6c | 1993 | goto fail_local; |
513ef596 DT |
1994 | |
1995 | dlm_allow_conn = 1; | |
fdda387f | 1996 | |
fdda387f | 1997 | /* Start listening */ |
ac7d5d03 AA |
1998 | switch (dlm_config.ci_protocol) { |
1999 | case DLM_PROTO_TCP: | |
a66c008c | 2000 | dlm_proto_ops = &dlm_tcp_ops; |
ac7d5d03 AA |
2001 | break; |
2002 | case DLM_PROTO_SCTP: | |
a66c008c | 2003 | dlm_proto_ops = &dlm_sctp_ops; |
ac7d5d03 AA |
2004 | break; |
2005 | default: | |
2006 | log_print("Invalid protocol identifier %d set", | |
2007 | dlm_config.ci_protocol); | |
2008 | error = -EINVAL; | |
2dc6b115 | 2009 | goto fail_proto_ops; |
ac7d5d03 | 2010 | } |
2dc6b115 AA |
2011 | |
2012 | error = dlm_listen_for_all(); | |
fdda387f | 2013 | if (error) |
2dc6b115 | 2014 | goto fail_listen; |
fdda387f | 2015 | |
fdda387f PC |
2016 | return 0; |
2017 | ||
2dc6b115 AA |
2018 | fail_listen: |
2019 | dlm_proto_ops = NULL; | |
2020 | fail_proto_ops: | |
513ef596 | 2021 | dlm_allow_conn = 0; |
d11ccd45 | 2022 | dlm_close_sock(&listen_con.sock); |
fcef0e6c AA |
2023 | work_stop(); |
2024 | fail_local: | |
2025 | deinit_local(); | |
513ef596 | 2026 | fail: |
fdda387f PC |
2027 | return error; |
2028 | } | |
36b71a8b DT |
2029 | |
2030 | void dlm_lowcomms_exit(void) | |
2031 | { | |
2032 | struct dlm_node_addr *na, *safe; | |
2033 | ||
2034 | spin_lock(&dlm_node_addrs_spin); | |
2035 | list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) { | |
2036 | list_del(&na->list); | |
2037 | while (na->addr_count--) | |
2038 | kfree(na->addr[na->addr_count]); | |
2039 | kfree(na); | |
2040 | } | |
2041 | spin_unlock(&dlm_node_addrs_spin); | |
2042 | } |