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