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