1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
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.
11 *******************************************************************************
12 ******************************************************************************/
17 * This is the "low-level" comms layer.
19 * It is responsible for sending/receiving messages
20 * from other nodes in the cluster.
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.
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.
39 * I don't see any problem with the recv thread executing the locking
40 * code on behalf of remote processes as the locking code is
41 * short, efficient and never waits.
46 #include <asm/ioctls.h>
49 #include <linux/pagemap.h>
51 #include "dlm_internal.h"
62 #define NODE_INCREMENT 32
63 static void cbuf_add(struct cbuf *cb, int n)
68 static int cbuf_data(struct cbuf *cb)
70 return ((cb->base + cb->len) & cb->mask);
73 static void cbuf_init(struct cbuf *cb, int size)
75 cb->base = cb->len = 0;
79 static void cbuf_eat(struct cbuf *cb, int n)
86 static bool cbuf_empty(struct cbuf *cb)
91 /* Maximum number of incoming messages to process before
92 doing a cond_resched()
94 #define MAX_RX_MSG_COUNT 25
97 struct socket *sock; /* NULL if not connected */
98 uint32_t nodeid; /* So we know who we are in the list */
99 struct mutex sock_mutex;
100 unsigned long flags; /* bit 1,2 = We are on the read/write lists */
101 #define CF_READ_PENDING 1
102 #define CF_WRITE_PENDING 2
103 #define CF_CONNECT_PENDING 3
104 #define CF_IS_OTHERCON 4
105 struct list_head writequeue; /* List of outgoing writequeue_entries */
106 struct list_head listenlist; /* List of allocated listening sockets */
107 spinlock_t writequeue_lock;
108 int (*rx_action) (struct connection *); /* What to do when active */
109 struct page *rx_page;
112 #define MAX_CONNECT_RETRIES 3
113 struct connection *othercon;
114 struct work_struct rwork; /* Receive workqueue */
115 struct work_struct swork; /* Send workqueue */
117 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
119 /* An entry waiting to be sent */
120 struct writequeue_entry {
121 struct list_head list;
127 struct connection *con;
130 static struct sockaddr_storage dlm_local_addr;
133 static struct workqueue_struct *recv_workqueue;
134 static struct workqueue_struct *send_workqueue;
136 /* An array of pointers to connections, indexed by NODEID */
137 static struct connection **connections;
138 static DECLARE_MUTEX(connections_lock);
139 static struct kmem_cache *con_cache;
140 static int conn_array_size;
142 static void process_recv_sockets(struct work_struct *work);
143 static void process_send_sockets(struct work_struct *work);
145 static struct connection *nodeid2con(int nodeid, gfp_t allocation)
147 struct connection *con = NULL;
149 down(&connections_lock);
150 if (nodeid >= conn_array_size) {
151 int new_size = nodeid + NODE_INCREMENT;
152 struct connection **new_conns;
154 new_conns = kzalloc(sizeof(struct connection *) *
155 new_size, allocation);
159 memcpy(new_conns, connections, sizeof(struct connection *) * conn_array_size);
160 conn_array_size = new_size;
162 connections = new_conns;
166 con = connections[nodeid];
167 if (con == NULL && allocation) {
168 con = kmem_cache_zalloc(con_cache, allocation);
172 con->nodeid = nodeid;
173 mutex_init(&con->sock_mutex);
174 INIT_LIST_HEAD(&con->writequeue);
175 spin_lock_init(&con->writequeue_lock);
176 INIT_WORK(&con->swork, process_send_sockets);
177 INIT_WORK(&con->rwork, process_recv_sockets);
179 connections[nodeid] = con;
183 up(&connections_lock);
187 /* Data available on socket or listen socket received a connect */
188 static void lowcomms_data_ready(struct sock *sk, int count_unused)
190 struct connection *con = sock2con(sk);
192 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
193 queue_work(recv_workqueue, &con->rwork);
196 static void lowcomms_write_space(struct sock *sk)
198 struct connection *con = sock2con(sk);
200 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
201 queue_work(send_workqueue, &con->swork);
204 static inline void lowcomms_connect_sock(struct connection *con)
206 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
207 queue_work(send_workqueue, &con->swork);
210 static void lowcomms_state_change(struct sock *sk)
212 if (sk->sk_state == TCP_ESTABLISHED)
213 lowcomms_write_space(sk);
216 /* Make a socket active */
217 static int add_sock(struct socket *sock, struct connection *con)
221 /* Install a data_ready callback */
222 con->sock->sk->sk_data_ready = lowcomms_data_ready;
223 con->sock->sk->sk_write_space = lowcomms_write_space;
224 con->sock->sk->sk_state_change = lowcomms_state_change;
229 /* Add the port number to an IP6 or 4 sockaddr and return the address
231 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
234 saddr->ss_family = dlm_local_addr.ss_family;
235 if (saddr->ss_family == AF_INET) {
236 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
237 in4_addr->sin_port = cpu_to_be16(port);
238 *addr_len = sizeof(struct sockaddr_in);
240 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
241 in6_addr->sin6_port = cpu_to_be16(port);
242 *addr_len = sizeof(struct sockaddr_in6);
246 /* Close a remote connection and tidy up */
247 static void close_connection(struct connection *con, bool and_other)
249 mutex_lock(&con->sock_mutex);
252 sock_release(con->sock);
255 if (con->othercon && and_other) {
256 /* Will only re-enter once. */
257 close_connection(con->othercon, false);
260 __free_page(con->rx_page);
264 mutex_unlock(&con->sock_mutex);
267 /* Data received from remote end */
268 static int receive_from_sock(struct connection *con)
276 int call_again_soon = 0;
278 mutex_lock(&con->sock_mutex);
280 if (con->sock == NULL) {
285 if (con->rx_page == NULL) {
287 * This doesn't need to be atomic, but I think it should
288 * improve performance if it is.
290 con->rx_page = alloc_page(GFP_ATOMIC);
291 if (con->rx_page == NULL)
293 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
296 msg.msg_control = NULL;
297 msg.msg_controllen = 0;
305 * iov[0] is the bit of the circular buffer between the current end
306 * point (cb.base + cb.len) and the end of the buffer.
308 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
309 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
313 * iov[1] is the bit of the circular buffer between the start of the
314 * buffer and the start of the currently used section (cb.base)
316 if (cbuf_data(&con->cb) >= con->cb.base) {
317 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
318 iov[1].iov_len = con->cb.base;
319 iov[1].iov_base = page_address(con->rx_page);
322 len = iov[0].iov_len + iov[1].iov_len;
326 r = ret = sock_recvmsg(con->sock, &msg, len,
327 MSG_DONTWAIT | MSG_NOSIGNAL);
337 cbuf_add(&con->cb, ret);
338 ret = dlm_process_incoming_buffer(con->nodeid,
339 page_address(con->rx_page),
340 con->cb.base, con->cb.len,
342 if (ret == -EBADMSG) {
343 printk(KERN_INFO "dlm: lowcomms: addr=%p, base=%u, len=%u, "
344 "iov_len=%u, iov_base[0]=%p, read=%d\n",
345 page_address(con->rx_page), con->cb.base, con->cb.len,
346 len, iov[0].iov_base, r);
350 cbuf_eat(&con->cb, ret);
352 if (cbuf_empty(&con->cb) && !call_again_soon) {
353 __free_page(con->rx_page);
359 mutex_unlock(&con->sock_mutex);
363 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
364 queue_work(recv_workqueue, &con->rwork);
365 mutex_unlock(&con->sock_mutex);
369 mutex_unlock(&con->sock_mutex);
370 if (ret != -EAGAIN && !test_bit(CF_IS_OTHERCON, &con->flags)) {
371 close_connection(con, false);
372 /* Reconnect when there is something to send */
374 /* Don't return success if we really got EOF */
381 /* Listening socket is busy, accept a connection */
382 static int accept_from_sock(struct connection *con)
385 struct sockaddr_storage peeraddr;
386 struct socket *newsock;
389 struct connection *newcon;
390 struct connection *addcon;
392 memset(&peeraddr, 0, sizeof(peeraddr));
393 result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
394 IPPROTO_TCP, &newsock);
398 mutex_lock_nested(&con->sock_mutex, 0);
401 if (con->sock == NULL)
404 newsock->type = con->sock->type;
405 newsock->ops = con->sock->ops;
407 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
411 /* Get the connected socket's peer */
412 memset(&peeraddr, 0, sizeof(peeraddr));
413 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
415 result = -ECONNABORTED;
419 /* Get the new node's NODEID */
420 make_sockaddr(&peeraddr, 0, &len);
421 if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
422 printk("dlm: connect from non cluster node\n");
423 sock_release(newsock);
424 mutex_unlock(&con->sock_mutex);
428 log_print("got connection from %d", nodeid);
430 /* Check to see if we already have a connection to this node. This
431 * could happen if the two nodes initiate a connection at roughly
432 * the same time and the connections cross on the wire.
434 * In this case we store the incoming one in "othercon"
436 newcon = nodeid2con(nodeid, GFP_KERNEL);
441 mutex_lock_nested(&newcon->sock_mutex, 1);
443 struct connection *othercon = newcon->othercon;
446 othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
448 printk("dlm: failed to allocate incoming socket\n");
449 mutex_unlock(&newcon->sock_mutex);
453 othercon->nodeid = nodeid;
454 othercon->rx_action = receive_from_sock;
455 mutex_init(&othercon->sock_mutex);
456 INIT_WORK(&othercon->swork, process_send_sockets);
457 INIT_WORK(&othercon->rwork, process_recv_sockets);
458 set_bit(CF_IS_OTHERCON, &othercon->flags);
459 newcon->othercon = othercon;
461 othercon->sock = newsock;
462 newsock->sk->sk_user_data = othercon;
463 add_sock(newsock, othercon);
467 newsock->sk->sk_user_data = newcon;
468 newcon->rx_action = receive_from_sock;
469 add_sock(newsock, newcon);
473 mutex_unlock(&newcon->sock_mutex);
476 * Add it to the active queue in case we got data
477 * beween processing the accept adding the socket
478 * to the read_sockets list
480 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
481 queue_work(recv_workqueue, &addcon->rwork);
482 mutex_unlock(&con->sock_mutex);
487 mutex_unlock(&con->sock_mutex);
488 sock_release(newsock);
490 if (result != -EAGAIN)
491 printk("dlm: error accepting connection from node: %d\n", result);
495 /* Connect a new socket to its peer */
496 static void connect_to_sock(struct connection *con)
498 int result = -EHOSTUNREACH;
499 struct sockaddr_storage saddr;
503 if (con->nodeid == 0) {
504 log_print("attempt to connect sock 0 foiled");
508 mutex_lock(&con->sock_mutex);
509 if (con->retries++ > MAX_CONNECT_RETRIES)
512 /* Some odd races can cause double-connects, ignore them */
518 /* Create a socket to communicate with */
519 result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
524 memset(&saddr, 0, sizeof(saddr));
525 if (dlm_nodeid_to_addr(con->nodeid, &saddr))
528 sock->sk->sk_user_data = con;
529 con->rx_action = receive_from_sock;
531 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
535 log_print("connecting to %d", con->nodeid);
537 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
539 if (result == -EINPROGRESS)
546 sock_release(con->sock);
550 * Some errors are fatal and this list might need adjusting. For other
551 * errors we try again until the max number of retries is reached.
553 if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
554 result != -ENETDOWN && result != EINVAL
555 && result != -EPROTONOSUPPORT) {
556 lowcomms_connect_sock(con);
560 mutex_unlock(&con->sock_mutex);
564 static struct socket *create_listen_sock(struct connection *con,
565 struct sockaddr_storage *saddr)
567 struct socket *sock = NULL;
573 if (dlm_local_addr.ss_family == AF_INET)
574 addr_len = sizeof(struct sockaddr_in);
576 addr_len = sizeof(struct sockaddr_in6);
578 /* Create a socket to communicate with */
579 result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM, IPPROTO_TCP, &sock);
581 printk("dlm: Can't create listening comms socket\n");
587 result = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
588 (char *)&one, sizeof(one));
591 printk("dlm: Failed to set SO_REUSEADDR on socket: result=%d\n",
594 sock->sk->sk_user_data = con;
595 con->rx_action = accept_from_sock;
598 /* Bind to our port */
599 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
600 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
602 printk("dlm: Can't bind to port %d\n", dlm_config.ci_tcp_port);
612 result = sock_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
613 (char *)&one, sizeof(one));
616 printk("dlm: Set keepalive failed: %d\n", result);
619 result = sock->ops->listen(sock, 5);
621 printk("dlm: Can't listen on port %d\n", dlm_config.ci_tcp_port);
632 /* Listen on all interfaces */
633 static int listen_for_all(void)
635 struct socket *sock = NULL;
636 struct connection *con = nodeid2con(0, GFP_KERNEL);
637 int result = -EINVAL;
639 /* We don't support multi-homed hosts */
640 set_bit(CF_IS_OTHERCON, &con->flags);
642 sock = create_listen_sock(con, &dlm_local_addr);
648 result = -EADDRINUSE;
656 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
659 struct writequeue_entry *entry;
661 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
665 entry->page = alloc_page(allocation);
680 void *dlm_lowcomms_get_buffer(int nodeid, int len,
681 gfp_t allocation, char **ppc)
683 struct connection *con;
684 struct writequeue_entry *e;
688 con = nodeid2con(nodeid, allocation);
692 spin_lock(&con->writequeue_lock);
693 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
694 if ((&e->list == &con->writequeue) ||
695 (PAGE_CACHE_SIZE - e->end < len)) {
702 spin_unlock(&con->writequeue_lock);
708 *ppc = page_address(e->page) + offset;
712 e = new_writequeue_entry(con, allocation);
714 spin_lock(&con->writequeue_lock);
718 list_add_tail(&e->list, &con->writequeue);
719 spin_unlock(&con->writequeue_lock);
725 void dlm_lowcomms_commit_buffer(void *mh)
727 struct writequeue_entry *e = (struct writequeue_entry *)mh;
728 struct connection *con = e->con;
731 spin_lock(&con->writequeue_lock);
735 e->len = e->end - e->offset;
737 spin_unlock(&con->writequeue_lock);
739 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
740 queue_work(send_workqueue, &con->swork);
745 spin_unlock(&con->writequeue_lock);
749 static void free_entry(struct writequeue_entry *e)
751 __free_page(e->page);
756 static void send_to_sock(struct connection *con)
759 ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
760 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
761 struct writequeue_entry *e;
764 mutex_lock(&con->sock_mutex);
765 if (con->sock == NULL)
768 sendpage = con->sock->ops->sendpage;
770 spin_lock(&con->writequeue_lock);
772 e = list_entry(con->writequeue.next, struct writequeue_entry,
774 if ((struct list_head *) e == &con->writequeue)
779 BUG_ON(len == 0 && e->users == 0);
780 spin_unlock(&con->writequeue_lock);
785 ret = sendpage(con->sock, e->page, offset, len,
787 if (ret == -EAGAIN || ret == 0)
793 /* Don't starve people filling buffers */
797 spin_lock(&con->writequeue_lock);
801 if (e->len == 0 && e->users == 0) {
808 spin_unlock(&con->writequeue_lock);
810 mutex_unlock(&con->sock_mutex);
814 mutex_unlock(&con->sock_mutex);
815 close_connection(con, false);
816 lowcomms_connect_sock(con);
820 mutex_unlock(&con->sock_mutex);
821 connect_to_sock(con);
825 static void clean_one_writequeue(struct connection *con)
827 struct list_head *list;
828 struct list_head *temp;
830 spin_lock(&con->writequeue_lock);
831 list_for_each_safe(list, temp, &con->writequeue) {
832 struct writequeue_entry *e =
833 list_entry(list, struct writequeue_entry, list);
837 spin_unlock(&con->writequeue_lock);
840 /* Called from recovery when it knows that a node has
842 int dlm_lowcomms_close(int nodeid)
844 struct connection *con;
849 log_print("closing connection to node %d", nodeid);
850 con = nodeid2con(nodeid, 0);
852 clean_one_writequeue(con);
853 close_connection(con, true);
861 /* Look for activity on active sockets */
862 static void process_recv_sockets(struct work_struct *work)
864 struct connection *con = container_of(work, struct connection, rwork);
867 clear_bit(CF_READ_PENDING, &con->flags);
869 err = con->rx_action(con);
874 static void process_send_sockets(struct work_struct *work)
876 struct connection *con = container_of(work, struct connection, swork);
878 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
879 connect_to_sock(con);
882 clear_bit(CF_WRITE_PENDING, &con->flags);
887 /* Discard all entries on the write queues */
888 static void clean_writequeues(void)
892 for (nodeid = 1; nodeid < conn_array_size; nodeid++) {
893 struct connection *con = nodeid2con(nodeid, 0);
896 clean_one_writequeue(con);
900 static void work_stop(void)
902 destroy_workqueue(recv_workqueue);
903 destroy_workqueue(send_workqueue);
906 static int work_start(void)
909 recv_workqueue = create_workqueue("dlm_recv");
910 error = IS_ERR(recv_workqueue);
912 log_print("can't start dlm_recv %d", error);
916 send_workqueue = create_singlethread_workqueue("dlm_send");
917 error = IS_ERR(send_workqueue);
919 log_print("can't start dlm_send %d", error);
920 destroy_workqueue(recv_workqueue);
927 void dlm_lowcomms_stop(void)
931 /* Set all the flags to prevent any
934 for (i = 0; i < conn_array_size; i++) {
936 connections[i]->flags |= 0xFF;
942 for (i = 0; i < conn_array_size; i++) {
943 if (connections[i]) {
944 close_connection(connections[i], true);
945 if (connections[i]->othercon)
946 kmem_cache_free(con_cache, connections[i]->othercon);
947 kmem_cache_free(con_cache, connections[i]);
954 kmem_cache_destroy(con_cache);
957 /* This is quite likely to sleep... */
958 int dlm_lowcomms_start(void)
963 connections = kzalloc(sizeof(struct connection *) *
964 NODE_INCREMENT, GFP_KERNEL);
968 conn_array_size = NODE_INCREMENT;
970 if (dlm_our_addr(&dlm_local_addr, 0)) {
971 log_print("no local IP address has been set");
974 if (!dlm_our_addr(&dlm_local_addr, 1)) {
975 log_print("This dlm comms module does not support multi-homed clustering");
979 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
980 __alignof__(struct connection), 0,
986 /* Start listening */
987 error = listen_for_all();
991 error = work_start();
998 close_connection(connections[0], false);
999 kmem_cache_free(con_cache, connections[0]);
1000 kmem_cache_destroy(con_cache);
1010 * Overrides for Emacs so that we follow Linus's tabbing style.
1011 * Emacs will notice this stuff at the end of the file and automatically
1012 * adjust the settings for this buffer only. This must remain at the end
1014 * ---------------------------------------------------------------------------
1016 * c-file-style: "linux"