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