Commit | Line | Data |
---|---|---|
fdda387f PC |
1 | /****************************************************************************** |
2 | ******************************************************************************* | |
3 | ** | |
4 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
5e9ccc37 | 5 | ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved. |
fdda387f PC |
6 | ** |
7 | ** This copyrighted material is made available to anyone wishing to use, | |
8 | ** modify, copy, or redistribute it subject to the terms and conditions | |
9 | ** of the GNU General Public License v.2. | |
10 | ** | |
11 | ******************************************************************************* | |
12 | ******************************************************************************/ | |
13 | ||
14 | /* | |
15 | * lowcomms.c | |
16 | * | |
17 | * This is the "low-level" comms layer. | |
18 | * | |
19 | * It is responsible for sending/receiving messages | |
20 | * from other nodes in the cluster. | |
21 | * | |
22 | * Cluster nodes are referred to by their nodeids. nodeids are | |
23 | * simply 32 bit numbers to the locking module - if they need to | |
2cf12c0b | 24 | * be expanded for the cluster infrastructure then that is its |
fdda387f PC |
25 | * responsibility. It is this layer's |
26 | * responsibility to resolve these into IP address or | |
27 | * whatever it needs for inter-node communication. | |
28 | * | |
29 | * The comms level is two kernel threads that deal mainly with | |
30 | * the receiving of messages from other nodes and passing them | |
31 | * up to the mid-level comms layer (which understands the | |
32 | * message format) for execution by the locking core, and | |
33 | * a send thread which does all the setting up of connections | |
34 | * to remote nodes and the sending of data. Threads are not allowed | |
35 | * to send their own data because it may cause them to wait in times | |
36 | * of high load. Also, this way, the sending thread can collect together | |
37 | * messages bound for one node and send them in one block. | |
38 | * | |
2cf12c0b | 39 | * lowcomms will choose to use either TCP or SCTP as its transport layer |
6ed7257b | 40 | * depending on the configuration variable 'protocol'. This should be set |
2cf12c0b | 41 | * to 0 (default) for TCP or 1 for SCTP. It should be configured using a |
6ed7257b PC |
42 | * cluster-wide mechanism as it must be the same on all nodes of the cluster |
43 | * for the DLM to function. | |
fdda387f PC |
44 | * |
45 | */ | |
46 | ||
fdda387f PC |
47 | #include <asm/ioctls.h> |
48 | #include <net/sock.h> | |
49 | #include <net/tcp.h> | |
50 | #include <linux/pagemap.h> | |
6ed7257b | 51 | #include <linux/file.h> |
7a936ce7 | 52 | #include <linux/mutex.h> |
6ed7257b | 53 | #include <linux/sctp.h> |
5a0e3ad6 | 54 | #include <linux/slab.h> |
2f2d76cc | 55 | #include <net/sctp/sctp.h> |
6ed7257b | 56 | #include <net/sctp/user.h> |
44ad532b | 57 | #include <net/ipv6.h> |
fdda387f PC |
58 | |
59 | #include "dlm_internal.h" | |
60 | #include "lowcomms.h" | |
61 | #include "midcomms.h" | |
62 | #include "config.h" | |
63 | ||
6ed7257b | 64 | #define NEEDED_RMEM (4*1024*1024) |
5e9ccc37 | 65 | #define CONN_HASH_SIZE 32 |
6ed7257b | 66 | |
f92c8dd7 BP |
67 | /* Number of messages to send before rescheduling */ |
68 | #define MAX_SEND_MSG_COUNT 25 | |
69 | ||
fdda387f | 70 | struct cbuf { |
ac33d071 PC |
71 | unsigned int base; |
72 | unsigned int len; | |
73 | unsigned int mask; | |
fdda387f PC |
74 | }; |
75 | ||
ac33d071 PC |
76 | static void cbuf_add(struct cbuf *cb, int n) |
77 | { | |
78 | cb->len += n; | |
79 | } | |
fdda387f | 80 | |
ac33d071 PC |
81 | static int cbuf_data(struct cbuf *cb) |
82 | { | |
83 | return ((cb->base + cb->len) & cb->mask); | |
84 | } | |
85 | ||
86 | static void cbuf_init(struct cbuf *cb, int size) | |
87 | { | |
88 | cb->base = cb->len = 0; | |
89 | cb->mask = size-1; | |
90 | } | |
91 | ||
92 | static void cbuf_eat(struct cbuf *cb, int n) | |
93 | { | |
94 | cb->len -= n; | |
95 | cb->base += n; | |
96 | cb->base &= cb->mask; | |
97 | } | |
98 | ||
99 | static bool cbuf_empty(struct cbuf *cb) | |
100 | { | |
101 | return cb->len == 0; | |
102 | } | |
fdda387f | 103 | |
fdda387f PC |
104 | struct connection { |
105 | struct socket *sock; /* NULL if not connected */ | |
106 | uint32_t nodeid; /* So we know who we are in the list */ | |
f1f1c1cc | 107 | struct mutex sock_mutex; |
6ed7257b | 108 | unsigned long flags; |
fdda387f PC |
109 | #define CF_READ_PENDING 1 |
110 | #define CF_WRITE_PENDING 2 | |
111 | #define CF_CONNECT_PENDING 3 | |
6ed7257b PC |
112 | #define CF_INIT_PENDING 4 |
113 | #define CF_IS_OTHERCON 5 | |
063c4c99 | 114 | #define CF_CLOSE 6 |
b36930dd | 115 | #define CF_APP_LIMITED 7 |
ac33d071 | 116 | struct list_head writequeue; /* List of outgoing writequeue_entries */ |
fdda387f PC |
117 | spinlock_t writequeue_lock; |
118 | int (*rx_action) (struct connection *); /* What to do when active */ | |
6ed7257b | 119 | void (*connect_action) (struct connection *); /* What to do to connect */ |
fdda387f PC |
120 | struct page *rx_page; |
121 | struct cbuf cb; | |
122 | int retries; | |
fdda387f | 123 | #define MAX_CONNECT_RETRIES 3 |
6ed7257b | 124 | int sctp_assoc; |
5e9ccc37 | 125 | struct hlist_node list; |
fdda387f | 126 | struct connection *othercon; |
1d6e8131 PC |
127 | struct work_struct rwork; /* Receive workqueue */ |
128 | struct work_struct swork; /* Send workqueue */ | |
fdda387f PC |
129 | }; |
130 | #define sock2con(x) ((struct connection *)(x)->sk_user_data) | |
131 | ||
132 | /* An entry waiting to be sent */ | |
133 | struct writequeue_entry { | |
134 | struct list_head list; | |
135 | struct page *page; | |
136 | int offset; | |
137 | int len; | |
138 | int end; | |
139 | int users; | |
140 | struct connection *con; | |
141 | }; | |
142 | ||
36b71a8b DT |
143 | struct dlm_node_addr { |
144 | struct list_head list; | |
145 | int nodeid; | |
146 | int addr_count; | |
147 | struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT]; | |
148 | }; | |
149 | ||
150 | static LIST_HEAD(dlm_node_addrs); | |
151 | static DEFINE_SPINLOCK(dlm_node_addrs_spin); | |
152 | ||
6ed7257b PC |
153 | static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT]; |
154 | static int dlm_local_count; | |
513ef596 | 155 | static int dlm_allow_conn; |
fdda387f | 156 | |
1d6e8131 PC |
157 | /* Work queues */ |
158 | static struct workqueue_struct *recv_workqueue; | |
159 | static struct workqueue_struct *send_workqueue; | |
fdda387f | 160 | |
5e9ccc37 | 161 | static struct hlist_head connection_hash[CONN_HASH_SIZE]; |
7a936ce7 | 162 | static DEFINE_MUTEX(connections_lock); |
c80e7c83 | 163 | static struct kmem_cache *con_cache; |
fdda387f | 164 | |
1d6e8131 PC |
165 | static void process_recv_sockets(struct work_struct *work); |
166 | static void process_send_sockets(struct work_struct *work); | |
fdda387f | 167 | |
5e9ccc37 CC |
168 | |
169 | /* This is deliberately very simple because most clusters have simple | |
170 | sequential nodeids, so we should be able to go straight to a connection | |
171 | struct in the array */ | |
172 | static inline int nodeid_hash(int nodeid) | |
173 | { | |
174 | return nodeid & (CONN_HASH_SIZE-1); | |
175 | } | |
176 | ||
177 | static struct connection *__find_con(int nodeid) | |
178 | { | |
179 | int r; | |
5e9ccc37 CC |
180 | struct connection *con; |
181 | ||
182 | r = nodeid_hash(nodeid); | |
183 | ||
b67bfe0d | 184 | hlist_for_each_entry(con, &connection_hash[r], list) { |
5e9ccc37 CC |
185 | if (con->nodeid == nodeid) |
186 | return con; | |
187 | } | |
188 | return NULL; | |
189 | } | |
190 | ||
6ed7257b PC |
191 | /* |
192 | * If 'allocation' is zero then we don't attempt to create a new | |
193 | * connection structure for this node. | |
194 | */ | |
195 | static struct connection *__nodeid2con(int nodeid, gfp_t alloc) | |
fdda387f PC |
196 | { |
197 | struct connection *con = NULL; | |
6ed7257b | 198 | int r; |
fdda387f | 199 | |
5e9ccc37 | 200 | con = __find_con(nodeid); |
6ed7257b PC |
201 | if (con || !alloc) |
202 | return con; | |
fdda387f | 203 | |
6ed7257b PC |
204 | con = kmem_cache_zalloc(con_cache, alloc); |
205 | if (!con) | |
206 | return NULL; | |
fdda387f | 207 | |
5e9ccc37 CC |
208 | r = nodeid_hash(nodeid); |
209 | hlist_add_head(&con->list, &connection_hash[r]); | |
fdda387f | 210 | |
6ed7257b PC |
211 | con->nodeid = nodeid; |
212 | mutex_init(&con->sock_mutex); | |
213 | INIT_LIST_HEAD(&con->writequeue); | |
214 | spin_lock_init(&con->writequeue_lock); | |
215 | INIT_WORK(&con->swork, process_send_sockets); | |
216 | INIT_WORK(&con->rwork, process_recv_sockets); | |
fdda387f | 217 | |
6ed7257b PC |
218 | /* Setup action pointers for child sockets */ |
219 | if (con->nodeid) { | |
5e9ccc37 | 220 | struct connection *zerocon = __find_con(0); |
fdda387f | 221 | |
6ed7257b PC |
222 | con->connect_action = zerocon->connect_action; |
223 | if (!con->rx_action) | |
224 | con->rx_action = zerocon->rx_action; | |
fdda387f PC |
225 | } |
226 | ||
6ed7257b PC |
227 | return con; |
228 | } | |
229 | ||
5e9ccc37 CC |
230 | /* Loop round all connections */ |
231 | static void foreach_conn(void (*conn_func)(struct connection *c)) | |
232 | { | |
233 | int i; | |
b67bfe0d | 234 | struct hlist_node *n; |
5e9ccc37 CC |
235 | struct connection *con; |
236 | ||
237 | for (i = 0; i < CONN_HASH_SIZE; i++) { | |
b67bfe0d | 238 | hlist_for_each_entry_safe(con, n, &connection_hash[i], list) |
5e9ccc37 | 239 | conn_func(con); |
5e9ccc37 CC |
240 | } |
241 | } | |
242 | ||
6ed7257b PC |
243 | static struct connection *nodeid2con(int nodeid, gfp_t allocation) |
244 | { | |
245 | struct connection *con; | |
246 | ||
7a936ce7 | 247 | mutex_lock(&connections_lock); |
6ed7257b | 248 | con = __nodeid2con(nodeid, allocation); |
7a936ce7 | 249 | mutex_unlock(&connections_lock); |
6ed7257b | 250 | |
fdda387f PC |
251 | return con; |
252 | } | |
253 | ||
6ed7257b PC |
254 | /* This is a bit drastic, but only called when things go wrong */ |
255 | static struct connection *assoc2con(int assoc_id) | |
256 | { | |
257 | int i; | |
258 | struct connection *con; | |
259 | ||
7a936ce7 | 260 | mutex_lock(&connections_lock); |
5e9ccc37 CC |
261 | |
262 | for (i = 0 ; i < CONN_HASH_SIZE; i++) { | |
b67bfe0d | 263 | hlist_for_each_entry(con, &connection_hash[i], list) { |
f70cb33b | 264 | if (con->sctp_assoc == assoc_id) { |
5e9ccc37 CC |
265 | mutex_unlock(&connections_lock); |
266 | return con; | |
267 | } | |
6ed7257b PC |
268 | } |
269 | } | |
7a936ce7 | 270 | mutex_unlock(&connections_lock); |
6ed7257b PC |
271 | return NULL; |
272 | } | |
273 | ||
36b71a8b DT |
274 | static struct dlm_node_addr *find_node_addr(int nodeid) |
275 | { | |
276 | struct dlm_node_addr *na; | |
277 | ||
278 | list_for_each_entry(na, &dlm_node_addrs, list) { | |
279 | if (na->nodeid == nodeid) | |
280 | return na; | |
281 | } | |
282 | return NULL; | |
283 | } | |
284 | ||
285 | static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y) | |
6ed7257b | 286 | { |
36b71a8b DT |
287 | switch (x->ss_family) { |
288 | case AF_INET: { | |
289 | struct sockaddr_in *sinx = (struct sockaddr_in *)x; | |
290 | struct sockaddr_in *siny = (struct sockaddr_in *)y; | |
291 | if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr) | |
292 | return 0; | |
293 | if (sinx->sin_port != siny->sin_port) | |
294 | return 0; | |
295 | break; | |
296 | } | |
297 | case AF_INET6: { | |
298 | struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x; | |
299 | struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y; | |
300 | if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr)) | |
301 | return 0; | |
302 | if (sinx->sin6_port != siny->sin6_port) | |
303 | return 0; | |
304 | break; | |
305 | } | |
306 | default: | |
307 | return 0; | |
308 | } | |
309 | return 1; | |
310 | } | |
311 | ||
312 | static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out, | |
313 | struct sockaddr *sa_out) | |
314 | { | |
315 | struct sockaddr_storage sas; | |
316 | struct dlm_node_addr *na; | |
6ed7257b PC |
317 | |
318 | if (!dlm_local_count) | |
319 | return -1; | |
320 | ||
36b71a8b DT |
321 | spin_lock(&dlm_node_addrs_spin); |
322 | na = find_node_addr(nodeid); | |
323 | if (na && na->addr_count) | |
324 | memcpy(&sas, na->addr[0], sizeof(struct sockaddr_storage)); | |
325 | spin_unlock(&dlm_node_addrs_spin); | |
326 | ||
327 | if (!na) | |
328 | return -EEXIST; | |
329 | ||
330 | if (!na->addr_count) | |
331 | return -ENOENT; | |
332 | ||
333 | if (sas_out) | |
334 | memcpy(sas_out, &sas, sizeof(struct sockaddr_storage)); | |
335 | ||
336 | if (!sa_out) | |
337 | return 0; | |
6ed7257b PC |
338 | |
339 | if (dlm_local_addr[0]->ss_family == AF_INET) { | |
36b71a8b DT |
340 | struct sockaddr_in *in4 = (struct sockaddr_in *) &sas; |
341 | struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out; | |
6ed7257b PC |
342 | ret4->sin_addr.s_addr = in4->sin_addr.s_addr; |
343 | } else { | |
36b71a8b DT |
344 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas; |
345 | struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out; | |
4e3fd7a0 | 346 | ret6->sin6_addr = in6->sin6_addr; |
6ed7257b PC |
347 | } |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
36b71a8b DT |
352 | static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid) |
353 | { | |
354 | struct dlm_node_addr *na; | |
355 | int rv = -EEXIST; | |
356 | ||
357 | spin_lock(&dlm_node_addrs_spin); | |
358 | list_for_each_entry(na, &dlm_node_addrs, list) { | |
359 | if (!na->addr_count) | |
360 | continue; | |
361 | ||
362 | if (!addr_compare(na->addr[0], addr)) | |
363 | continue; | |
364 | ||
365 | *nodeid = na->nodeid; | |
366 | rv = 0; | |
367 | break; | |
368 | } | |
369 | spin_unlock(&dlm_node_addrs_spin); | |
370 | return rv; | |
371 | } | |
372 | ||
373 | int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len) | |
374 | { | |
375 | struct sockaddr_storage *new_addr; | |
376 | struct dlm_node_addr *new_node, *na; | |
377 | ||
378 | new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS); | |
379 | if (!new_node) | |
380 | return -ENOMEM; | |
381 | ||
382 | new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS); | |
383 | if (!new_addr) { | |
384 | kfree(new_node); | |
385 | return -ENOMEM; | |
386 | } | |
387 | ||
388 | memcpy(new_addr, addr, len); | |
389 | ||
390 | spin_lock(&dlm_node_addrs_spin); | |
391 | na = find_node_addr(nodeid); | |
392 | if (!na) { | |
393 | new_node->nodeid = nodeid; | |
394 | new_node->addr[0] = new_addr; | |
395 | new_node->addr_count = 1; | |
396 | list_add(&new_node->list, &dlm_node_addrs); | |
397 | spin_unlock(&dlm_node_addrs_spin); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | if (na->addr_count >= DLM_MAX_ADDR_COUNT) { | |
402 | spin_unlock(&dlm_node_addrs_spin); | |
403 | kfree(new_addr); | |
404 | kfree(new_node); | |
405 | return -ENOSPC; | |
406 | } | |
407 | ||
408 | na->addr[na->addr_count++] = new_addr; | |
409 | spin_unlock(&dlm_node_addrs_spin); | |
410 | kfree(new_node); | |
411 | return 0; | |
412 | } | |
413 | ||
fdda387f PC |
414 | /* Data available on socket or listen socket received a connect */ |
415 | static void lowcomms_data_ready(struct sock *sk, int count_unused) | |
416 | { | |
417 | struct connection *con = sock2con(sk); | |
afb853fb | 418 | if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags)) |
1d6e8131 | 419 | queue_work(recv_workqueue, &con->rwork); |
fdda387f PC |
420 | } |
421 | ||
422 | static void lowcomms_write_space(struct sock *sk) | |
423 | { | |
424 | struct connection *con = sock2con(sk); | |
425 | ||
b36930dd DM |
426 | if (!con) |
427 | return; | |
428 | ||
429 | clear_bit(SOCK_NOSPACE, &con->sock->flags); | |
430 | ||
431 | if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) { | |
432 | con->sock->sk->sk_write_pending--; | |
433 | clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags); | |
434 | } | |
435 | ||
436 | if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) | |
1d6e8131 | 437 | queue_work(send_workqueue, &con->swork); |
fdda387f PC |
438 | } |
439 | ||
440 | static inline void lowcomms_connect_sock(struct connection *con) | |
441 | { | |
063c4c99 LMB |
442 | if (test_bit(CF_CLOSE, &con->flags)) |
443 | return; | |
1d6e8131 PC |
444 | if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags)) |
445 | queue_work(send_workqueue, &con->swork); | |
fdda387f PC |
446 | } |
447 | ||
448 | static void lowcomms_state_change(struct sock *sk) | |
449 | { | |
ac33d071 | 450 | if (sk->sk_state == TCP_ESTABLISHED) |
fdda387f | 451 | lowcomms_write_space(sk); |
fdda387f PC |
452 | } |
453 | ||
391fbdc5 CC |
454 | int dlm_lowcomms_connect_node(int nodeid) |
455 | { | |
456 | struct connection *con; | |
457 | ||
04bedd79 DT |
458 | /* with sctp there's no connecting without sending */ |
459 | if (dlm_config.ci_protocol != 0) | |
460 | return 0; | |
461 | ||
391fbdc5 CC |
462 | if (nodeid == dlm_our_nodeid()) |
463 | return 0; | |
464 | ||
465 | con = nodeid2con(nodeid, GFP_NOFS); | |
466 | if (!con) | |
467 | return -ENOMEM; | |
468 | lowcomms_connect_sock(con); | |
469 | return 0; | |
470 | } | |
471 | ||
fdda387f | 472 | /* Make a socket active */ |
4dd40f0c | 473 | static void add_sock(struct socket *sock, struct connection *con) |
fdda387f PC |
474 | { |
475 | con->sock = sock; | |
476 | ||
477 | /* Install a data_ready callback */ | |
478 | con->sock->sk->sk_data_ready = lowcomms_data_ready; | |
479 | con->sock->sk->sk_write_space = lowcomms_write_space; | |
480 | con->sock->sk->sk_state_change = lowcomms_state_change; | |
6ed7257b | 481 | con->sock->sk->sk_user_data = con; |
d6d7b702 | 482 | con->sock->sk->sk_allocation = GFP_NOFS; |
fdda387f PC |
483 | } |
484 | ||
6ed7257b | 485 | /* Add the port number to an IPv6 or 4 sockaddr and return the address |
fdda387f PC |
486 | length */ |
487 | static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, | |
488 | int *addr_len) | |
489 | { | |
6ed7257b | 490 | saddr->ss_family = dlm_local_addr[0]->ss_family; |
ac33d071 | 491 | if (saddr->ss_family == AF_INET) { |
fdda387f PC |
492 | struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr; |
493 | in4_addr->sin_port = cpu_to_be16(port); | |
494 | *addr_len = sizeof(struct sockaddr_in); | |
6ed7257b | 495 | memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero)); |
ac33d071 | 496 | } else { |
fdda387f PC |
497 | struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr; |
498 | in6_addr->sin6_port = cpu_to_be16(port); | |
499 | *addr_len = sizeof(struct sockaddr_in6); | |
500 | } | |
01c8cab2 | 501 | memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len); |
fdda387f PC |
502 | } |
503 | ||
504 | /* Close a remote connection and tidy up */ | |
ac33d071 | 505 | static void close_connection(struct connection *con, bool and_other) |
fdda387f | 506 | { |
f1f1c1cc | 507 | mutex_lock(&con->sock_mutex); |
fdda387f PC |
508 | |
509 | if (con->sock) { | |
510 | sock_release(con->sock); | |
511 | con->sock = NULL; | |
512 | } | |
513 | if (con->othercon && and_other) { | |
ac33d071 PC |
514 | /* Will only re-enter once. */ |
515 | close_connection(con->othercon, false); | |
fdda387f PC |
516 | } |
517 | if (con->rx_page) { | |
518 | __free_page(con->rx_page); | |
519 | con->rx_page = NULL; | |
520 | } | |
9e5f2825 | 521 | |
61d96be0 PC |
522 | con->retries = 0; |
523 | mutex_unlock(&con->sock_mutex); | |
fdda387f PC |
524 | } |
525 | ||
6ed7257b PC |
526 | /* We only send shutdown messages to nodes that are not part of the cluster */ |
527 | static void sctp_send_shutdown(sctp_assoc_t associd) | |
528 | { | |
529 | static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; | |
530 | struct msghdr outmessage; | |
531 | struct cmsghdr *cmsg; | |
532 | struct sctp_sndrcvinfo *sinfo; | |
533 | int ret; | |
534 | struct connection *con; | |
535 | ||
536 | con = nodeid2con(0,0); | |
537 | BUG_ON(con == NULL); | |
538 | ||
539 | outmessage.msg_name = NULL; | |
540 | outmessage.msg_namelen = 0; | |
541 | outmessage.msg_control = outcmsg; | |
542 | outmessage.msg_controllen = sizeof(outcmsg); | |
543 | outmessage.msg_flags = MSG_EOR; | |
544 | ||
545 | cmsg = CMSG_FIRSTHDR(&outmessage); | |
546 | cmsg->cmsg_level = IPPROTO_SCTP; | |
547 | cmsg->cmsg_type = SCTP_SNDRCV; | |
548 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); | |
549 | outmessage.msg_controllen = cmsg->cmsg_len; | |
550 | sinfo = CMSG_DATA(cmsg); | |
551 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); | |
552 | ||
553 | sinfo->sinfo_flags |= MSG_EOF; | |
554 | sinfo->sinfo_assoc_id = associd; | |
555 | ||
556 | ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0); | |
557 | ||
558 | if (ret != 0) | |
559 | log_print("send EOF to node failed: %d", ret); | |
560 | } | |
561 | ||
5e9ccc37 CC |
562 | static void sctp_init_failed_foreach(struct connection *con) |
563 | { | |
564 | con->sctp_assoc = 0; | |
565 | if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) { | |
566 | if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) | |
567 | queue_work(send_workqueue, &con->swork); | |
568 | } | |
569 | } | |
570 | ||
6ed7257b PC |
571 | /* INIT failed but we don't know which node... |
572 | restart INIT on all pending nodes */ | |
573 | static void sctp_init_failed(void) | |
574 | { | |
7a936ce7 | 575 | mutex_lock(&connections_lock); |
5e9ccc37 CC |
576 | |
577 | foreach_conn(sctp_init_failed_foreach); | |
578 | ||
7a936ce7 | 579 | mutex_unlock(&connections_lock); |
6ed7257b PC |
580 | } |
581 | ||
582 | /* Something happened to an association */ | |
617e82e1 DT |
583 | static void process_sctp_notification(struct connection *con, |
584 | struct msghdr *msg, char *buf) | |
6ed7257b PC |
585 | { |
586 | union sctp_notification *sn = (union sctp_notification *)buf; | |
587 | ||
588 | if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) { | |
589 | switch (sn->sn_assoc_change.sac_state) { | |
590 | ||
591 | case SCTP_COMM_UP: | |
592 | case SCTP_RESTART: | |
593 | { | |
594 | /* Check that the new node is in the lockspace */ | |
595 | struct sctp_prim prim; | |
596 | int nodeid; | |
597 | int prim_len, ret; | |
598 | int addr_len; | |
599 | struct connection *new_con; | |
6ed7257b PC |
600 | |
601 | /* | |
602 | * We get this before any data for an association. | |
603 | * We verify that the node is in the cluster and | |
604 | * then peel off a socket for it. | |
605 | */ | |
606 | if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) { | |
607 | log_print("COMM_UP for invalid assoc ID %d", | |
617e82e1 | 608 | (int)sn->sn_assoc_change.sac_assoc_id); |
6ed7257b PC |
609 | sctp_init_failed(); |
610 | return; | |
611 | } | |
612 | memset(&prim, 0, sizeof(struct sctp_prim)); | |
613 | prim_len = sizeof(struct sctp_prim); | |
614 | prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id; | |
615 | ||
616 | ret = kernel_getsockopt(con->sock, | |
617 | IPPROTO_SCTP, | |
618 | SCTP_PRIMARY_ADDR, | |
619 | (char*)&prim, | |
620 | &prim_len); | |
621 | if (ret < 0) { | |
622 | log_print("getsockopt/sctp_primary_addr on " | |
623 | "new assoc %d failed : %d", | |
624 | (int)sn->sn_assoc_change.sac_assoc_id, | |
625 | ret); | |
626 | ||
627 | /* Retry INIT later */ | |
628 | new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id); | |
629 | if (new_con) | |
630 | clear_bit(CF_CONNECT_PENDING, &con->flags); | |
631 | return; | |
632 | } | |
633 | make_sockaddr(&prim.ssp_addr, 0, &addr_len); | |
36b71a8b | 634 | if (addr_to_nodeid(&prim.ssp_addr, &nodeid)) { |
6ed7257b PC |
635 | unsigned char *b=(unsigned char *)&prim.ssp_addr; |
636 | log_print("reject connect from unknown addr"); | |
bcaadf5c MY |
637 | print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE, |
638 | b, sizeof(struct sockaddr_storage)); | |
6ed7257b PC |
639 | sctp_send_shutdown(prim.ssp_assoc_id); |
640 | return; | |
641 | } | |
642 | ||
748285cc | 643 | new_con = nodeid2con(nodeid, GFP_NOFS); |
6ed7257b PC |
644 | if (!new_con) |
645 | return; | |
646 | ||
647 | /* Peel off a new sock */ | |
2f2d76cc BP |
648 | sctp_lock_sock(con->sock->sk); |
649 | ret = sctp_do_peeloff(con->sock->sk, | |
650 | sn->sn_assoc_change.sac_assoc_id, | |
651 | &new_con->sock); | |
652 | sctp_release_sock(con->sock->sk); | |
6861f350 | 653 | if (ret < 0) { |
617e82e1 | 654 | log_print("Can't peel off a socket for " |
6861f350 | 655 | "connection %d to node %d: err=%d", |
2f2d76cc BP |
656 | (int)sn->sn_assoc_change.sac_assoc_id, |
657 | nodeid, ret); | |
6861f350 | 658 | return; |
6ed7257b | 659 | } |
6ed7257b | 660 | add_sock(new_con->sock, new_con); |
6ed7257b | 661 | |
6861f350 DT |
662 | log_print("connecting to %d sctp association %d", |
663 | nodeid, (int)sn->sn_assoc_change.sac_assoc_id); | |
6ed7257b PC |
664 | |
665 | /* Send any pending writes */ | |
666 | clear_bit(CF_CONNECT_PENDING, &new_con->flags); | |
667 | clear_bit(CF_INIT_PENDING, &con->flags); | |
668 | if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) { | |
669 | queue_work(send_workqueue, &new_con->swork); | |
670 | } | |
671 | if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags)) | |
672 | queue_work(recv_workqueue, &new_con->rwork); | |
673 | } | |
674 | break; | |
675 | ||
676 | case SCTP_COMM_LOST: | |
677 | case SCTP_SHUTDOWN_COMP: | |
678 | { | |
679 | con = assoc2con(sn->sn_assoc_change.sac_assoc_id); | |
680 | if (con) { | |
681 | con->sctp_assoc = 0; | |
682 | } | |
683 | } | |
684 | break; | |
685 | ||
686 | /* We don't know which INIT failed, so clear the PENDING flags | |
687 | * on them all. if assoc_id is zero then it will then try | |
688 | * again */ | |
689 | ||
690 | case SCTP_CANT_STR_ASSOC: | |
691 | { | |
692 | log_print("Can't start SCTP association - retrying"); | |
693 | sctp_init_failed(); | |
694 | } | |
695 | break; | |
696 | ||
697 | default: | |
698 | log_print("unexpected SCTP assoc change id=%d state=%d", | |
699 | (int)sn->sn_assoc_change.sac_assoc_id, | |
700 | sn->sn_assoc_change.sac_state); | |
701 | } | |
702 | } | |
703 | } | |
704 | ||
fdda387f PC |
705 | /* Data received from remote end */ |
706 | static int receive_from_sock(struct connection *con) | |
707 | { | |
708 | int ret = 0; | |
58addbff AV |
709 | struct msghdr msg = {}; |
710 | struct kvec iov[2]; | |
fdda387f PC |
711 | unsigned len; |
712 | int r; | |
713 | int call_again_soon = 0; | |
58addbff | 714 | int nvec; |
6ed7257b | 715 | char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
fdda387f | 716 | |
f1f1c1cc | 717 | mutex_lock(&con->sock_mutex); |
fdda387f | 718 | |
a34fbc63 PC |
719 | if (con->sock == NULL) { |
720 | ret = -EAGAIN; | |
721 | goto out_close; | |
722 | } | |
723 | ||
fdda387f PC |
724 | if (con->rx_page == NULL) { |
725 | /* | |
726 | * This doesn't need to be atomic, but I think it should | |
727 | * improve performance if it is. | |
728 | */ | |
729 | con->rx_page = alloc_page(GFP_ATOMIC); | |
730 | if (con->rx_page == NULL) | |
731 | goto out_resched; | |
ac33d071 | 732 | cbuf_init(&con->cb, PAGE_CACHE_SIZE); |
fdda387f PC |
733 | } |
734 | ||
6ed7257b PC |
735 | /* Only SCTP needs these really */ |
736 | memset(&incmsg, 0, sizeof(incmsg)); | |
737 | msg.msg_control = incmsg; | |
738 | msg.msg_controllen = sizeof(incmsg); | |
739 | ||
fdda387f PC |
740 | /* |
741 | * iov[0] is the bit of the circular buffer between the current end | |
742 | * point (cb.base + cb.len) and the end of the buffer. | |
743 | */ | |
ac33d071 PC |
744 | iov[0].iov_len = con->cb.base - cbuf_data(&con->cb); |
745 | iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb); | |
89adc934 | 746 | iov[1].iov_len = 0; |
58addbff | 747 | nvec = 1; |
fdda387f PC |
748 | |
749 | /* | |
750 | * iov[1] is the bit of the circular buffer between the start of the | |
751 | * buffer and the start of the currently used section (cb.base) | |
752 | */ | |
ac33d071 PC |
753 | if (cbuf_data(&con->cb) >= con->cb.base) { |
754 | iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb); | |
fdda387f PC |
755 | iov[1].iov_len = con->cb.base; |
756 | iov[1].iov_base = page_address(con->rx_page); | |
58addbff | 757 | nvec = 2; |
fdda387f PC |
758 | } |
759 | len = iov[0].iov_len + iov[1].iov_len; | |
760 | ||
58addbff | 761 | r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len, |
fdda387f | 762 | MSG_DONTWAIT | MSG_NOSIGNAL); |
fdda387f PC |
763 | if (ret <= 0) |
764 | goto out_close; | |
bd44e2b0 | 765 | |
6ed7257b PC |
766 | /* Process SCTP notifications */ |
767 | if (msg.msg_flags & MSG_NOTIFICATION) { | |
6ed7257b PC |
768 | msg.msg_control = incmsg; |
769 | msg.msg_controllen = sizeof(incmsg); | |
770 | ||
771 | process_sctp_notification(con, &msg, | |
617e82e1 | 772 | page_address(con->rx_page) + con->cb.base); |
6ed7257b PC |
773 | mutex_unlock(&con->sock_mutex); |
774 | return 0; | |
775 | } | |
776 | BUG_ON(con->nodeid == 0); | |
777 | ||
fdda387f PC |
778 | if (ret == len) |
779 | call_again_soon = 1; | |
ac33d071 | 780 | cbuf_add(&con->cb, ret); |
fdda387f PC |
781 | ret = dlm_process_incoming_buffer(con->nodeid, |
782 | page_address(con->rx_page), | |
783 | con->cb.base, con->cb.len, | |
784 | PAGE_CACHE_SIZE); | |
785 | if (ret == -EBADMSG) { | |
617e82e1 DT |
786 | log_print("lowcomms: addr=%p, base=%u, len=%u, " |
787 | "iov_len=%u, iov_base[0]=%p, read=%d", | |
788 | page_address(con->rx_page), con->cb.base, con->cb.len, | |
789 | len, iov[0].iov_base, r); | |
fdda387f PC |
790 | } |
791 | if (ret < 0) | |
792 | goto out_close; | |
ac33d071 | 793 | cbuf_eat(&con->cb, ret); |
fdda387f | 794 | |
ac33d071 | 795 | if (cbuf_empty(&con->cb) && !call_again_soon) { |
fdda387f PC |
796 | __free_page(con->rx_page); |
797 | con->rx_page = NULL; | |
798 | } | |
799 | ||
fdda387f PC |
800 | if (call_again_soon) |
801 | goto out_resched; | |
f1f1c1cc | 802 | mutex_unlock(&con->sock_mutex); |
ac33d071 | 803 | return 0; |
fdda387f | 804 | |
ac33d071 | 805 | out_resched: |
1d6e8131 PC |
806 | if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) |
807 | queue_work(recv_workqueue, &con->rwork); | |
f1f1c1cc | 808 | mutex_unlock(&con->sock_mutex); |
bd44e2b0 | 809 | return -EAGAIN; |
fdda387f | 810 | |
ac33d071 | 811 | out_close: |
f1f1c1cc | 812 | mutex_unlock(&con->sock_mutex); |
9e5f2825 | 813 | if (ret != -EAGAIN) { |
ac33d071 | 814 | close_connection(con, false); |
fdda387f PC |
815 | /* Reconnect when there is something to send */ |
816 | } | |
a34fbc63 PC |
817 | /* Don't return success if we really got EOF */ |
818 | if (ret == 0) | |
819 | ret = -EAGAIN; | |
fdda387f | 820 | |
fdda387f PC |
821 | return ret; |
822 | } | |
823 | ||
824 | /* Listening socket is busy, accept a connection */ | |
6ed7257b | 825 | static int tcp_accept_from_sock(struct connection *con) |
fdda387f PC |
826 | { |
827 | int result; | |
828 | struct sockaddr_storage peeraddr; | |
829 | struct socket *newsock; | |
830 | int len; | |
831 | int nodeid; | |
832 | struct connection *newcon; | |
bd44e2b0 | 833 | struct connection *addcon; |
fdda387f | 834 | |
513ef596 DT |
835 | mutex_lock(&connections_lock); |
836 | if (!dlm_allow_conn) { | |
837 | mutex_unlock(&connections_lock); | |
838 | return -1; | |
839 | } | |
840 | mutex_unlock(&connections_lock); | |
841 | ||
fdda387f | 842 | memset(&peeraddr, 0, sizeof(peeraddr)); |
6ed7257b | 843 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, |
ac33d071 | 844 | IPPROTO_TCP, &newsock); |
fdda387f PC |
845 | if (result < 0) |
846 | return -ENOMEM; | |
847 | ||
f1f1c1cc | 848 | mutex_lock_nested(&con->sock_mutex, 0); |
fdda387f PC |
849 | |
850 | result = -ENOTCONN; | |
851 | if (con->sock == NULL) | |
852 | goto accept_err; | |
853 | ||
854 | newsock->type = con->sock->type; | |
855 | newsock->ops = con->sock->ops; | |
856 | ||
857 | result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK); | |
858 | if (result < 0) | |
859 | goto accept_err; | |
860 | ||
861 | /* Get the connected socket's peer */ | |
862 | memset(&peeraddr, 0, sizeof(peeraddr)); | |
863 | if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, | |
864 | &len, 2)) { | |
865 | result = -ECONNABORTED; | |
866 | goto accept_err; | |
867 | } | |
868 | ||
869 | /* Get the new node's NODEID */ | |
870 | make_sockaddr(&peeraddr, 0, &len); | |
36b71a8b | 871 | if (addr_to_nodeid(&peeraddr, &nodeid)) { |
bcaadf5c | 872 | unsigned char *b=(unsigned char *)&peeraddr; |
617e82e1 | 873 | log_print("connect from non cluster node"); |
bcaadf5c MY |
874 | print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE, |
875 | b, sizeof(struct sockaddr_storage)); | |
fdda387f | 876 | sock_release(newsock); |
f1f1c1cc | 877 | mutex_unlock(&con->sock_mutex); |
fdda387f PC |
878 | return -1; |
879 | } | |
880 | ||
881 | log_print("got connection from %d", nodeid); | |
882 | ||
883 | /* Check to see if we already have a connection to this node. This | |
884 | * could happen if the two nodes initiate a connection at roughly | |
885 | * the same time and the connections cross on the wire. | |
fdda387f PC |
886 | * In this case we store the incoming one in "othercon" |
887 | */ | |
748285cc | 888 | newcon = nodeid2con(nodeid, GFP_NOFS); |
fdda387f PC |
889 | if (!newcon) { |
890 | result = -ENOMEM; | |
891 | goto accept_err; | |
892 | } | |
f1f1c1cc | 893 | mutex_lock_nested(&newcon->sock_mutex, 1); |
fdda387f | 894 | if (newcon->sock) { |
ac33d071 | 895 | struct connection *othercon = newcon->othercon; |
fdda387f PC |
896 | |
897 | if (!othercon) { | |
748285cc | 898 | othercon = kmem_cache_zalloc(con_cache, GFP_NOFS); |
fdda387f | 899 | if (!othercon) { |
617e82e1 | 900 | log_print("failed to allocate incoming socket"); |
f1f1c1cc | 901 | mutex_unlock(&newcon->sock_mutex); |
fdda387f PC |
902 | result = -ENOMEM; |
903 | goto accept_err; | |
904 | } | |
fdda387f PC |
905 | othercon->nodeid = nodeid; |
906 | othercon->rx_action = receive_from_sock; | |
f1f1c1cc | 907 | mutex_init(&othercon->sock_mutex); |
1d6e8131 PC |
908 | INIT_WORK(&othercon->swork, process_send_sockets); |
909 | INIT_WORK(&othercon->rwork, process_recv_sockets); | |
fdda387f | 910 | set_bit(CF_IS_OTHERCON, &othercon->flags); |
61d96be0 PC |
911 | } |
912 | if (!othercon->sock) { | |
fdda387f | 913 | newcon->othercon = othercon; |
97d84836 PC |
914 | othercon->sock = newsock; |
915 | newsock->sk->sk_user_data = othercon; | |
916 | add_sock(newsock, othercon); | |
917 | addcon = othercon; | |
918 | } | |
919 | else { | |
920 | printk("Extra connection from node %d attempted\n", nodeid); | |
921 | result = -EAGAIN; | |
f4fadb23 | 922 | mutex_unlock(&newcon->sock_mutex); |
97d84836 | 923 | goto accept_err; |
fdda387f | 924 | } |
fdda387f PC |
925 | } |
926 | else { | |
927 | newsock->sk->sk_user_data = newcon; | |
928 | newcon->rx_action = receive_from_sock; | |
929 | add_sock(newsock, newcon); | |
bd44e2b0 | 930 | addcon = newcon; |
fdda387f PC |
931 | } |
932 | ||
f1f1c1cc | 933 | mutex_unlock(&newcon->sock_mutex); |
fdda387f PC |
934 | |
935 | /* | |
936 | * Add it to the active queue in case we got data | |
25985edc | 937 | * between processing the accept adding the socket |
fdda387f PC |
938 | * to the read_sockets list |
939 | */ | |
bd44e2b0 PC |
940 | if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) |
941 | queue_work(recv_workqueue, &addcon->rwork); | |
f1f1c1cc | 942 | mutex_unlock(&con->sock_mutex); |
fdda387f PC |
943 | |
944 | return 0; | |
945 | ||
ac33d071 | 946 | accept_err: |
f1f1c1cc | 947 | mutex_unlock(&con->sock_mutex); |
fdda387f PC |
948 | sock_release(newsock); |
949 | ||
950 | if (result != -EAGAIN) | |
617e82e1 | 951 | log_print("error accepting connection from node: %d", result); |
fdda387f PC |
952 | return result; |
953 | } | |
954 | ||
6ed7257b PC |
955 | static void free_entry(struct writequeue_entry *e) |
956 | { | |
957 | __free_page(e->page); | |
958 | kfree(e); | |
959 | } | |
960 | ||
961 | /* Initiate an SCTP association. | |
962 | This is a special case of send_to_sock() in that we don't yet have a | |
963 | peeled-off socket for this association, so we use the listening socket | |
964 | and add the primary IP address of the remote node. | |
965 | */ | |
966 | static void sctp_init_assoc(struct connection *con) | |
967 | { | |
968 | struct sockaddr_storage rem_addr; | |
969 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; | |
970 | struct msghdr outmessage; | |
971 | struct cmsghdr *cmsg; | |
972 | struct sctp_sndrcvinfo *sinfo; | |
973 | struct connection *base_con; | |
974 | struct writequeue_entry *e; | |
975 | int len, offset; | |
976 | int ret; | |
977 | int addrlen; | |
978 | struct kvec iov[1]; | |
979 | ||
980 | if (test_and_set_bit(CF_INIT_PENDING, &con->flags)) | |
981 | return; | |
982 | ||
983 | if (con->retries++ > MAX_CONNECT_RETRIES) | |
984 | return; | |
985 | ||
36b71a8b | 986 | if (nodeid_to_addr(con->nodeid, NULL, (struct sockaddr *)&rem_addr)) { |
6ed7257b PC |
987 | log_print("no address for nodeid %d", con->nodeid); |
988 | return; | |
989 | } | |
990 | base_con = nodeid2con(0, 0); | |
991 | BUG_ON(base_con == NULL); | |
992 | ||
993 | make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen); | |
994 | ||
995 | outmessage.msg_name = &rem_addr; | |
996 | outmessage.msg_namelen = addrlen; | |
997 | outmessage.msg_control = outcmsg; | |
998 | outmessage.msg_controllen = sizeof(outcmsg); | |
999 | outmessage.msg_flags = MSG_EOR; | |
1000 | ||
1001 | spin_lock(&con->writequeue_lock); | |
6ed7257b | 1002 | |
04bedd79 DT |
1003 | if (list_empty(&con->writequeue)) { |
1004 | spin_unlock(&con->writequeue_lock); | |
1005 | log_print("writequeue empty for nodeid %d", con->nodeid); | |
1006 | return; | |
1007 | } | |
6ed7257b | 1008 | |
04bedd79 | 1009 | e = list_first_entry(&con->writequeue, struct writequeue_entry, list); |
6ed7257b PC |
1010 | len = e->len; |
1011 | offset = e->offset; | |
1012 | spin_unlock(&con->writequeue_lock); | |
6ed7257b PC |
1013 | |
1014 | /* Send the first block off the write queue */ | |
1015 | iov[0].iov_base = page_address(e->page)+offset; | |
1016 | iov[0].iov_len = len; | |
1017 | ||
1018 | cmsg = CMSG_FIRSTHDR(&outmessage); | |
1019 | cmsg->cmsg_level = IPPROTO_SCTP; | |
1020 | cmsg->cmsg_type = SCTP_SNDRCV; | |
1021 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); | |
1022 | sinfo = CMSG_DATA(cmsg); | |
1023 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); | |
1024 | sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid()); | |
1025 | outmessage.msg_controllen = cmsg->cmsg_len; | |
1026 | ||
1027 | ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len); | |
1028 | if (ret < 0) { | |
617e82e1 DT |
1029 | log_print("Send first packet to node %d failed: %d", |
1030 | con->nodeid, ret); | |
6ed7257b PC |
1031 | |
1032 | /* Try again later */ | |
1033 | clear_bit(CF_CONNECT_PENDING, &con->flags); | |
1034 | clear_bit(CF_INIT_PENDING, &con->flags); | |
1035 | } | |
1036 | else { | |
1037 | spin_lock(&con->writequeue_lock); | |
1038 | e->offset += ret; | |
1039 | e->len -= ret; | |
1040 | ||
1041 | if (e->len == 0 && e->users == 0) { | |
1042 | list_del(&e->list); | |
6ed7257b PC |
1043 | free_entry(e); |
1044 | } | |
1045 | spin_unlock(&con->writequeue_lock); | |
1046 | } | |
1047 | } | |
1048 | ||
fdda387f | 1049 | /* Connect a new socket to its peer */ |
6ed7257b | 1050 | static void tcp_connect_to_sock(struct connection *con) |
fdda387f | 1051 | { |
6bd8feda | 1052 | struct sockaddr_storage saddr, src_addr; |
fdda387f | 1053 | int addr_len; |
a89d63a1 | 1054 | struct socket *sock = NULL; |
cb2d45da | 1055 | int one = 1; |
36b71a8b | 1056 | int result; |
fdda387f PC |
1057 | |
1058 | if (con->nodeid == 0) { | |
1059 | log_print("attempt to connect sock 0 foiled"); | |
ac33d071 | 1060 | return; |
fdda387f PC |
1061 | } |
1062 | ||
f1f1c1cc | 1063 | mutex_lock(&con->sock_mutex); |
fdda387f PC |
1064 | if (con->retries++ > MAX_CONNECT_RETRIES) |
1065 | goto out; | |
1066 | ||
1067 | /* Some odd races can cause double-connects, ignore them */ | |
36b71a8b | 1068 | if (con->sock) |
fdda387f | 1069 | goto out; |
fdda387f PC |
1070 | |
1071 | /* Create a socket to communicate with */ | |
6ed7257b | 1072 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, |
ac33d071 | 1073 | IPPROTO_TCP, &sock); |
fdda387f PC |
1074 | if (result < 0) |
1075 | goto out_err; | |
1076 | ||
1077 | memset(&saddr, 0, sizeof(saddr)); | |
36b71a8b DT |
1078 | result = nodeid_to_addr(con->nodeid, &saddr, NULL); |
1079 | if (result < 0) { | |
1080 | log_print("no address for nodeid %d", con->nodeid); | |
ac33d071 | 1081 | goto out_err; |
36b71a8b | 1082 | } |
fdda387f PC |
1083 | |
1084 | sock->sk->sk_user_data = con; | |
1085 | con->rx_action = receive_from_sock; | |
6ed7257b PC |
1086 | con->connect_action = tcp_connect_to_sock; |
1087 | add_sock(sock, con); | |
fdda387f | 1088 | |
6bd8feda LH |
1089 | /* Bind to our cluster-known address connecting to avoid |
1090 | routing problems */ | |
1091 | memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr)); | |
1092 | make_sockaddr(&src_addr, 0, &addr_len); | |
1093 | result = sock->ops->bind(sock, (struct sockaddr *) &src_addr, | |
1094 | addr_len); | |
1095 | if (result < 0) { | |
1096 | log_print("could not bind for connect: %d", result); | |
1097 | /* This *may* not indicate a critical error */ | |
1098 | } | |
1099 | ||
68c817a1 | 1100 | make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len); |
fdda387f | 1101 | |
fdda387f | 1102 | log_print("connecting to %d", con->nodeid); |
cb2d45da DT |
1103 | |
1104 | /* Turn off Nagle's algorithm */ | |
1105 | kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one, | |
1106 | sizeof(one)); | |
1107 | ||
36b71a8b | 1108 | result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len, |
ac33d071 | 1109 | O_NONBLOCK); |
fdda387f PC |
1110 | if (result == -EINPROGRESS) |
1111 | result = 0; | |
ac33d071 PC |
1112 | if (result == 0) |
1113 | goto out; | |
fdda387f | 1114 | |
ac33d071 | 1115 | out_err: |
fdda387f PC |
1116 | if (con->sock) { |
1117 | sock_release(con->sock); | |
1118 | con->sock = NULL; | |
a89d63a1 CD |
1119 | } else if (sock) { |
1120 | sock_release(sock); | |
fdda387f PC |
1121 | } |
1122 | /* | |
1123 | * Some errors are fatal and this list might need adjusting. For other | |
1124 | * errors we try again until the max number of retries is reached. | |
1125 | */ | |
36b71a8b DT |
1126 | if (result != -EHOSTUNREACH && |
1127 | result != -ENETUNREACH && | |
1128 | result != -ENETDOWN && | |
1129 | result != -EINVAL && | |
1130 | result != -EPROTONOSUPPORT) { | |
1131 | log_print("connect %d try %d error %d", con->nodeid, | |
1132 | con->retries, result); | |
1133 | mutex_unlock(&con->sock_mutex); | |
1134 | msleep(1000); | |
fdda387f | 1135 | lowcomms_connect_sock(con); |
36b71a8b | 1136 | return; |
fdda387f | 1137 | } |
ac33d071 | 1138 | out: |
f1f1c1cc | 1139 | mutex_unlock(&con->sock_mutex); |
ac33d071 | 1140 | return; |
fdda387f PC |
1141 | } |
1142 | ||
6ed7257b PC |
1143 | static struct socket *tcp_create_listen_sock(struct connection *con, |
1144 | struct sockaddr_storage *saddr) | |
fdda387f | 1145 | { |
ac33d071 | 1146 | struct socket *sock = NULL; |
fdda387f PC |
1147 | int result = 0; |
1148 | int one = 1; | |
1149 | int addr_len; | |
1150 | ||
6ed7257b | 1151 | if (dlm_local_addr[0]->ss_family == AF_INET) |
fdda387f PC |
1152 | addr_len = sizeof(struct sockaddr_in); |
1153 | else | |
1154 | addr_len = sizeof(struct sockaddr_in6); | |
1155 | ||
1156 | /* Create a socket to communicate with */ | |
617e82e1 DT |
1157 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, |
1158 | IPPROTO_TCP, &sock); | |
fdda387f | 1159 | if (result < 0) { |
617e82e1 | 1160 | log_print("Can't create listening comms socket"); |
fdda387f PC |
1161 | goto create_out; |
1162 | } | |
1163 | ||
cb2d45da DT |
1164 | /* Turn off Nagle's algorithm */ |
1165 | kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one, | |
1166 | sizeof(one)); | |
1167 | ||
6ed7257b PC |
1168 | result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
1169 | (char *)&one, sizeof(one)); | |
1170 | ||
fdda387f | 1171 | if (result < 0) { |
617e82e1 | 1172 | log_print("Failed to set SO_REUSEADDR on socket: %d", result); |
fdda387f | 1173 | } |
6ed7257b PC |
1174 | con->rx_action = tcp_accept_from_sock; |
1175 | con->connect_action = tcp_connect_to_sock; | |
fdda387f PC |
1176 | |
1177 | /* Bind to our port */ | |
68c817a1 | 1178 | make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len); |
fdda387f PC |
1179 | result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len); |
1180 | if (result < 0) { | |
617e82e1 | 1181 | log_print("Can't bind to port %d", dlm_config.ci_tcp_port); |
fdda387f PC |
1182 | sock_release(sock); |
1183 | sock = NULL; | |
1184 | con->sock = NULL; | |
1185 | goto create_out; | |
1186 | } | |
6ed7257b | 1187 | result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
ac33d071 | 1188 | (char *)&one, sizeof(one)); |
fdda387f | 1189 | if (result < 0) { |
617e82e1 | 1190 | log_print("Set keepalive failed: %d", result); |
fdda387f PC |
1191 | } |
1192 | ||
1193 | result = sock->ops->listen(sock, 5); | |
1194 | if (result < 0) { | |
617e82e1 | 1195 | log_print("Can't listen on port %d", dlm_config.ci_tcp_port); |
fdda387f PC |
1196 | sock_release(sock); |
1197 | sock = NULL; | |
1198 | goto create_out; | |
1199 | } | |
1200 | ||
ac33d071 | 1201 | create_out: |
fdda387f PC |
1202 | return sock; |
1203 | } | |
1204 | ||
6ed7257b PC |
1205 | /* Get local addresses */ |
1206 | static void init_local(void) | |
1207 | { | |
1208 | struct sockaddr_storage sas, *addr; | |
1209 | int i; | |
1210 | ||
30d3a237 | 1211 | dlm_local_count = 0; |
1b189b88 | 1212 | for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) { |
6ed7257b PC |
1213 | if (dlm_our_addr(&sas, i)) |
1214 | break; | |
1215 | ||
573c24c4 | 1216 | addr = kmalloc(sizeof(*addr), GFP_NOFS); |
6ed7257b PC |
1217 | if (!addr) |
1218 | break; | |
1219 | memcpy(addr, &sas, sizeof(*addr)); | |
1220 | dlm_local_addr[dlm_local_count++] = addr; | |
1221 | } | |
1222 | } | |
1223 | ||
617e82e1 DT |
1224 | /* Bind to an IP address. SCTP allows multiple address so it can do |
1225 | multi-homing */ | |
1226 | static int add_sctp_bind_addr(struct connection *sctp_con, | |
1227 | struct sockaddr_storage *addr, | |
1228 | int addr_len, int num) | |
6ed7257b PC |
1229 | { |
1230 | int result = 0; | |
1231 | ||
1232 | if (num == 1) | |
1233 | result = kernel_bind(sctp_con->sock, | |
1234 | (struct sockaddr *) addr, | |
1235 | addr_len); | |
1236 | else | |
1237 | result = kernel_setsockopt(sctp_con->sock, SOL_SCTP, | |
1238 | SCTP_SOCKOPT_BINDX_ADD, | |
1239 | (char *)addr, addr_len); | |
1240 | ||
1241 | if (result < 0) | |
1242 | log_print("Can't bind to port %d addr number %d", | |
1243 | dlm_config.ci_tcp_port, num); | |
1244 | ||
1245 | return result; | |
1246 | } | |
fdda387f | 1247 | |
6ed7257b PC |
1248 | /* Initialise SCTP socket and bind to all interfaces */ |
1249 | static int sctp_listen_for_all(void) | |
1250 | { | |
1251 | struct socket *sock = NULL; | |
1252 | struct sockaddr_storage localaddr; | |
1253 | struct sctp_event_subscribe subscribe; | |
1254 | int result = -EINVAL, num = 1, i, addr_len; | |
573c24c4 | 1255 | struct connection *con = nodeid2con(0, GFP_NOFS); |
6ed7257b PC |
1256 | int bufsize = NEEDED_RMEM; |
1257 | ||
1258 | if (!con) | |
1259 | return -ENOMEM; | |
1260 | ||
1261 | log_print("Using SCTP for communications"); | |
1262 | ||
1263 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET, | |
1264 | IPPROTO_SCTP, &sock); | |
1265 | if (result < 0) { | |
1266 | log_print("Can't create comms socket, check SCTP is loaded"); | |
1267 | goto out; | |
1268 | } | |
1269 | ||
1270 | /* Listen for events */ | |
1271 | memset(&subscribe, 0, sizeof(subscribe)); | |
1272 | subscribe.sctp_data_io_event = 1; | |
1273 | subscribe.sctp_association_event = 1; | |
1274 | subscribe.sctp_send_failure_event = 1; | |
1275 | subscribe.sctp_shutdown_event = 1; | |
1276 | subscribe.sctp_partial_delivery_event = 1; | |
1277 | ||
df61c952 | 1278 | result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE, |
6ed7257b PC |
1279 | (char *)&bufsize, sizeof(bufsize)); |
1280 | if (result) | |
617e82e1 | 1281 | log_print("Error increasing buffer space on socket %d", result); |
6ed7257b PC |
1282 | |
1283 | result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS, | |
617e82e1 | 1284 | (char *)&subscribe, sizeof(subscribe)); |
6ed7257b PC |
1285 | if (result < 0) { |
1286 | log_print("Failed to set SCTP_EVENTS on socket: result=%d", | |
1287 | result); | |
1288 | goto create_delsock; | |
1289 | } | |
1290 | ||
1291 | /* Init con struct */ | |
1292 | sock->sk->sk_user_data = con; | |
1293 | con->sock = sock; | |
1294 | con->sock->sk->sk_data_ready = lowcomms_data_ready; | |
1295 | con->rx_action = receive_from_sock; | |
1296 | con->connect_action = sctp_init_assoc; | |
1297 | ||
1298 | /* Bind to all interfaces. */ | |
1299 | for (i = 0; i < dlm_local_count; i++) { | |
1300 | memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr)); | |
1301 | make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len); | |
1302 | ||
1303 | result = add_sctp_bind_addr(con, &localaddr, addr_len, num); | |
1304 | if (result) | |
1305 | goto create_delsock; | |
1306 | ++num; | |
1307 | } | |
1308 | ||
1309 | result = sock->ops->listen(sock, 5); | |
1310 | if (result < 0) { | |
1311 | log_print("Can't set socket listening"); | |
1312 | goto create_delsock; | |
1313 | } | |
1314 | ||
1315 | return 0; | |
1316 | ||
1317 | create_delsock: | |
1318 | sock_release(sock); | |
1319 | con->sock = NULL; | |
1320 | out: | |
1321 | return result; | |
1322 | } | |
1323 | ||
1324 | static int tcp_listen_for_all(void) | |
fdda387f PC |
1325 | { |
1326 | struct socket *sock = NULL; | |
573c24c4 | 1327 | struct connection *con = nodeid2con(0, GFP_NOFS); |
fdda387f PC |
1328 | int result = -EINVAL; |
1329 | ||
6ed7257b PC |
1330 | if (!con) |
1331 | return -ENOMEM; | |
1332 | ||
fdda387f | 1333 | /* We don't support multi-homed hosts */ |
6ed7257b | 1334 | if (dlm_local_addr[1] != NULL) { |
617e82e1 DT |
1335 | log_print("TCP protocol can't handle multi-homed hosts, " |
1336 | "try SCTP"); | |
6ed7257b PC |
1337 | return -EINVAL; |
1338 | } | |
1339 | ||
1340 | log_print("Using TCP for communications"); | |
1341 | ||
6ed7257b | 1342 | sock = tcp_create_listen_sock(con, dlm_local_addr[0]); |
fdda387f PC |
1343 | if (sock) { |
1344 | add_sock(sock, con); | |
1345 | result = 0; | |
1346 | } | |
1347 | else { | |
1348 | result = -EADDRINUSE; | |
1349 | } | |
1350 | ||
1351 | return result; | |
1352 | } | |
1353 | ||
1354 | ||
1355 | ||
1356 | static struct writequeue_entry *new_writequeue_entry(struct connection *con, | |
1357 | gfp_t allocation) | |
1358 | { | |
1359 | struct writequeue_entry *entry; | |
1360 | ||
1361 | entry = kmalloc(sizeof(struct writequeue_entry), allocation); | |
1362 | if (!entry) | |
1363 | return NULL; | |
1364 | ||
1365 | entry->page = alloc_page(allocation); | |
1366 | if (!entry->page) { | |
1367 | kfree(entry); | |
1368 | return NULL; | |
1369 | } | |
1370 | ||
1371 | entry->offset = 0; | |
1372 | entry->len = 0; | |
1373 | entry->end = 0; | |
1374 | entry->users = 0; | |
1375 | entry->con = con; | |
1376 | ||
1377 | return entry; | |
1378 | } | |
1379 | ||
617e82e1 | 1380 | void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc) |
fdda387f PC |
1381 | { |
1382 | struct connection *con; | |
1383 | struct writequeue_entry *e; | |
1384 | int offset = 0; | |
fdda387f | 1385 | |
fdda387f PC |
1386 | con = nodeid2con(nodeid, allocation); |
1387 | if (!con) | |
1388 | return NULL; | |
1389 | ||
4edde74e | 1390 | spin_lock(&con->writequeue_lock); |
fdda387f | 1391 | e = list_entry(con->writequeue.prev, struct writequeue_entry, list); |
ac33d071 | 1392 | if ((&e->list == &con->writequeue) || |
fdda387f PC |
1393 | (PAGE_CACHE_SIZE - e->end < len)) { |
1394 | e = NULL; | |
1395 | } else { | |
1396 | offset = e->end; | |
1397 | e->end += len; | |
eeee2b5f | 1398 | e->users++; |
fdda387f PC |
1399 | } |
1400 | spin_unlock(&con->writequeue_lock); | |
1401 | ||
1402 | if (e) { | |
ac33d071 | 1403 | got_one: |
fdda387f PC |
1404 | *ppc = page_address(e->page) + offset; |
1405 | return e; | |
1406 | } | |
1407 | ||
1408 | e = new_writequeue_entry(con, allocation); | |
1409 | if (e) { | |
1410 | spin_lock(&con->writequeue_lock); | |
1411 | offset = e->end; | |
1412 | e->end += len; | |
eeee2b5f | 1413 | e->users++; |
fdda387f PC |
1414 | list_add_tail(&e->list, &con->writequeue); |
1415 | spin_unlock(&con->writequeue_lock); | |
1416 | goto got_one; | |
1417 | } | |
1418 | return NULL; | |
1419 | } | |
1420 | ||
1421 | void dlm_lowcomms_commit_buffer(void *mh) | |
1422 | { | |
1423 | struct writequeue_entry *e = (struct writequeue_entry *)mh; | |
1424 | struct connection *con = e->con; | |
1425 | int users; | |
1426 | ||
4edde74e | 1427 | spin_lock(&con->writequeue_lock); |
fdda387f PC |
1428 | users = --e->users; |
1429 | if (users) | |
1430 | goto out; | |
1431 | e->len = e->end - e->offset; | |
fdda387f PC |
1432 | spin_unlock(&con->writequeue_lock); |
1433 | ||
1d6e8131 PC |
1434 | if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) { |
1435 | queue_work(send_workqueue, &con->swork); | |
fdda387f PC |
1436 | } |
1437 | return; | |
1438 | ||
ac33d071 | 1439 | out: |
fdda387f PC |
1440 | spin_unlock(&con->writequeue_lock); |
1441 | return; | |
1442 | } | |
1443 | ||
fdda387f | 1444 | /* Send a message */ |
ac33d071 | 1445 | static void send_to_sock(struct connection *con) |
fdda387f PC |
1446 | { |
1447 | int ret = 0; | |
fdda387f PC |
1448 | const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
1449 | struct writequeue_entry *e; | |
1450 | int len, offset; | |
f92c8dd7 | 1451 | int count = 0; |
fdda387f | 1452 | |
f1f1c1cc | 1453 | mutex_lock(&con->sock_mutex); |
fdda387f PC |
1454 | if (con->sock == NULL) |
1455 | goto out_connect; | |
1456 | ||
fdda387f PC |
1457 | spin_lock(&con->writequeue_lock); |
1458 | for (;;) { | |
1459 | e = list_entry(con->writequeue.next, struct writequeue_entry, | |
1460 | list); | |
1461 | if ((struct list_head *) e == &con->writequeue) | |
1462 | break; | |
1463 | ||
1464 | len = e->len; | |
1465 | offset = e->offset; | |
1466 | BUG_ON(len == 0 && e->users == 0); | |
1467 | spin_unlock(&con->writequeue_lock); | |
1468 | ||
1469 | ret = 0; | |
1470 | if (len) { | |
1329e3f2 PB |
1471 | ret = kernel_sendpage(con->sock, e->page, offset, len, |
1472 | msg_flags); | |
d66f8277 | 1473 | if (ret == -EAGAIN || ret == 0) { |
b36930dd DM |
1474 | if (ret == -EAGAIN && |
1475 | test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) && | |
1476 | !test_and_set_bit(CF_APP_LIMITED, &con->flags)) { | |
1477 | /* Notify TCP that we're limited by the | |
1478 | * application window size. | |
1479 | */ | |
1480 | set_bit(SOCK_NOSPACE, &con->sock->flags); | |
1481 | con->sock->sk->sk_write_pending++; | |
1482 | } | |
d66f8277 | 1483 | cond_resched(); |
fdda387f | 1484 | goto out; |
9c5bef58 | 1485 | } else if (ret < 0) |
fdda387f | 1486 | goto send_error; |
d66f8277 | 1487 | } |
f92c8dd7 BP |
1488 | |
1489 | /* Don't starve people filling buffers */ | |
1490 | if (++count >= MAX_SEND_MSG_COUNT) { | |
ac33d071 | 1491 | cond_resched(); |
f92c8dd7 BP |
1492 | count = 0; |
1493 | } | |
fdda387f PC |
1494 | |
1495 | spin_lock(&con->writequeue_lock); | |
1496 | e->offset += ret; | |
1497 | e->len -= ret; | |
1498 | ||
1499 | if (e->len == 0 && e->users == 0) { | |
1500 | list_del(&e->list); | |
1501 | free_entry(e); | |
fdda387f PC |
1502 | } |
1503 | } | |
1504 | spin_unlock(&con->writequeue_lock); | |
ac33d071 | 1505 | out: |
f1f1c1cc | 1506 | mutex_unlock(&con->sock_mutex); |
ac33d071 | 1507 | return; |
fdda387f | 1508 | |
ac33d071 | 1509 | send_error: |
f1f1c1cc | 1510 | mutex_unlock(&con->sock_mutex); |
ac33d071 | 1511 | close_connection(con, false); |
fdda387f | 1512 | lowcomms_connect_sock(con); |
ac33d071 | 1513 | return; |
fdda387f | 1514 | |
ac33d071 | 1515 | out_connect: |
f1f1c1cc | 1516 | mutex_unlock(&con->sock_mutex); |
6ed7257b PC |
1517 | if (!test_bit(CF_INIT_PENDING, &con->flags)) |
1518 | lowcomms_connect_sock(con); | |
fdda387f PC |
1519 | } |
1520 | ||
1521 | static void clean_one_writequeue(struct connection *con) | |
1522 | { | |
5e9ccc37 | 1523 | struct writequeue_entry *e, *safe; |
fdda387f PC |
1524 | |
1525 | spin_lock(&con->writequeue_lock); | |
5e9ccc37 | 1526 | list_for_each_entry_safe(e, safe, &con->writequeue, list) { |
fdda387f PC |
1527 | list_del(&e->list); |
1528 | free_entry(e); | |
1529 | } | |
1530 | spin_unlock(&con->writequeue_lock); | |
1531 | } | |
1532 | ||
1533 | /* Called from recovery when it knows that a node has | |
1534 | left the cluster */ | |
1535 | int dlm_lowcomms_close(int nodeid) | |
1536 | { | |
1537 | struct connection *con; | |
36b71a8b | 1538 | struct dlm_node_addr *na; |
fdda387f | 1539 | |
fdda387f PC |
1540 | log_print("closing connection to node %d", nodeid); |
1541 | con = nodeid2con(nodeid, 0); | |
1542 | if (con) { | |
063c4c99 LMB |
1543 | clear_bit(CF_CONNECT_PENDING, &con->flags); |
1544 | clear_bit(CF_WRITE_PENDING, &con->flags); | |
1545 | set_bit(CF_CLOSE, &con->flags); | |
1546 | if (cancel_work_sync(&con->swork)) | |
1547 | log_print("canceled swork for node %d", nodeid); | |
1548 | if (cancel_work_sync(&con->rwork)) | |
1549 | log_print("canceled rwork for node %d", nodeid); | |
fdda387f | 1550 | clean_one_writequeue(con); |
ac33d071 | 1551 | close_connection(con, true); |
fdda387f | 1552 | } |
36b71a8b DT |
1553 | |
1554 | spin_lock(&dlm_node_addrs_spin); | |
1555 | na = find_node_addr(nodeid); | |
1556 | if (na) { | |
1557 | list_del(&na->list); | |
1558 | while (na->addr_count--) | |
1559 | kfree(na->addr[na->addr_count]); | |
1560 | kfree(na); | |
1561 | } | |
1562 | spin_unlock(&dlm_node_addrs_spin); | |
1563 | ||
fdda387f | 1564 | return 0; |
fdda387f PC |
1565 | } |
1566 | ||
6ed7257b | 1567 | /* Receive workqueue function */ |
1d6e8131 | 1568 | static void process_recv_sockets(struct work_struct *work) |
fdda387f | 1569 | { |
1d6e8131 PC |
1570 | struct connection *con = container_of(work, struct connection, rwork); |
1571 | int err; | |
fdda387f | 1572 | |
1d6e8131 PC |
1573 | clear_bit(CF_READ_PENDING, &con->flags); |
1574 | do { | |
1575 | err = con->rx_action(con); | |
1576 | } while (!err); | |
fdda387f PC |
1577 | } |
1578 | ||
6ed7257b | 1579 | /* Send workqueue function */ |
1d6e8131 | 1580 | static void process_send_sockets(struct work_struct *work) |
fdda387f | 1581 | { |
1d6e8131 | 1582 | struct connection *con = container_of(work, struct connection, swork); |
fdda387f | 1583 | |
1d6e8131 | 1584 | if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) { |
6ed7257b | 1585 | con->connect_action(con); |
063c4c99 | 1586 | set_bit(CF_WRITE_PENDING, &con->flags); |
fdda387f | 1587 | } |
063c4c99 LMB |
1588 | if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags)) |
1589 | send_to_sock(con); | |
fdda387f PC |
1590 | } |
1591 | ||
1592 | ||
1593 | /* Discard all entries on the write queues */ | |
1594 | static void clean_writequeues(void) | |
1595 | { | |
5e9ccc37 | 1596 | foreach_conn(clean_one_writequeue); |
fdda387f PC |
1597 | } |
1598 | ||
1d6e8131 | 1599 | static void work_stop(void) |
fdda387f | 1600 | { |
1d6e8131 PC |
1601 | destroy_workqueue(recv_workqueue); |
1602 | destroy_workqueue(send_workqueue); | |
fdda387f PC |
1603 | } |
1604 | ||
1d6e8131 | 1605 | static int work_start(void) |
fdda387f | 1606 | { |
e43f055a DT |
1607 | recv_workqueue = alloc_workqueue("dlm_recv", |
1608 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); | |
b9d41052 NK |
1609 | if (!recv_workqueue) { |
1610 | log_print("can't start dlm_recv"); | |
1611 | return -ENOMEM; | |
fdda387f | 1612 | } |
fdda387f | 1613 | |
e43f055a DT |
1614 | send_workqueue = alloc_workqueue("dlm_send", |
1615 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); | |
b9d41052 NK |
1616 | if (!send_workqueue) { |
1617 | log_print("can't start dlm_send"); | |
1d6e8131 | 1618 | destroy_workqueue(recv_workqueue); |
b9d41052 | 1619 | return -ENOMEM; |
fdda387f | 1620 | } |
fdda387f PC |
1621 | |
1622 | return 0; | |
1623 | } | |
1624 | ||
5e9ccc37 | 1625 | static void stop_conn(struct connection *con) |
fdda387f | 1626 | { |
5e9ccc37 | 1627 | con->flags |= 0x0F; |
391fbdc5 | 1628 | if (con->sock && con->sock->sk) |
5e9ccc37 CC |
1629 | con->sock->sk->sk_user_data = NULL; |
1630 | } | |
fdda387f | 1631 | |
5e9ccc37 CC |
1632 | static void free_conn(struct connection *con) |
1633 | { | |
1634 | close_connection(con, true); | |
1635 | if (con->othercon) | |
1636 | kmem_cache_free(con_cache, con->othercon); | |
1637 | hlist_del(&con->list); | |
1638 | kmem_cache_free(con_cache, con); | |
1639 | } | |
1640 | ||
1641 | void dlm_lowcomms_stop(void) | |
1642 | { | |
ac33d071 | 1643 | /* Set all the flags to prevent any |
fdda387f PC |
1644 | socket activity. |
1645 | */ | |
7a936ce7 | 1646 | mutex_lock(&connections_lock); |
513ef596 | 1647 | dlm_allow_conn = 0; |
5e9ccc37 | 1648 | foreach_conn(stop_conn); |
7a936ce7 | 1649 | mutex_unlock(&connections_lock); |
ac33d071 | 1650 | |
1d6e8131 | 1651 | work_stop(); |
6ed7257b | 1652 | |
7a936ce7 | 1653 | mutex_lock(&connections_lock); |
fdda387f PC |
1654 | clean_writequeues(); |
1655 | ||
5e9ccc37 CC |
1656 | foreach_conn(free_conn); |
1657 | ||
7a936ce7 | 1658 | mutex_unlock(&connections_lock); |
fdda387f PC |
1659 | kmem_cache_destroy(con_cache); |
1660 | } | |
1661 | ||
fdda387f PC |
1662 | int dlm_lowcomms_start(void) |
1663 | { | |
6ed7257b PC |
1664 | int error = -EINVAL; |
1665 | struct connection *con; | |
5e9ccc37 CC |
1666 | int i; |
1667 | ||
1668 | for (i = 0; i < CONN_HASH_SIZE; i++) | |
1669 | INIT_HLIST_HEAD(&connection_hash[i]); | |
fdda387f | 1670 | |
6ed7257b PC |
1671 | init_local(); |
1672 | if (!dlm_local_count) { | |
617e82e1 | 1673 | error = -ENOTCONN; |
fdda387f | 1674 | log_print("no local IP address has been set"); |
513ef596 | 1675 | goto fail; |
fdda387f PC |
1676 | } |
1677 | ||
6ed7257b | 1678 | error = -ENOMEM; |
fdda387f | 1679 | con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection), |
ac33d071 | 1680 | __alignof__(struct connection), 0, |
20c2df83 | 1681 | NULL); |
fdda387f | 1682 | if (!con_cache) |
513ef596 DT |
1683 | goto fail; |
1684 | ||
1685 | error = work_start(); | |
1686 | if (error) | |
1687 | goto fail_destroy; | |
1688 | ||
1689 | dlm_allow_conn = 1; | |
fdda387f | 1690 | |
fdda387f | 1691 | /* Start listening */ |
6ed7257b PC |
1692 | if (dlm_config.ci_protocol == 0) |
1693 | error = tcp_listen_for_all(); | |
1694 | else | |
1695 | error = sctp_listen_for_all(); | |
fdda387f PC |
1696 | if (error) |
1697 | goto fail_unlisten; | |
1698 | ||
fdda387f PC |
1699 | return 0; |
1700 | ||
ac33d071 | 1701 | fail_unlisten: |
513ef596 | 1702 | dlm_allow_conn = 0; |
6ed7257b PC |
1703 | con = nodeid2con(0,0); |
1704 | if (con) { | |
1705 | close_connection(con, false); | |
1706 | kmem_cache_free(con_cache, con); | |
1707 | } | |
513ef596 | 1708 | fail_destroy: |
fdda387f | 1709 | kmem_cache_destroy(con_cache); |
513ef596 | 1710 | fail: |
fdda387f PC |
1711 | return error; |
1712 | } | |
36b71a8b DT |
1713 | |
1714 | void dlm_lowcomms_exit(void) | |
1715 | { | |
1716 | struct dlm_node_addr *na, *safe; | |
1717 | ||
1718 | spin_lock(&dlm_node_addrs_spin); | |
1719 | list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) { | |
1720 | list_del(&na->list); | |
1721 | while (na->addr_count--) | |
1722 | kfree(na->addr[na->addr_count]); | |
1723 | kfree(na); | |
1724 | } | |
1725 | spin_unlock(&dlm_node_addrs_spin); | |
1726 | } |