1 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
5 * Copyright (C) 2004 Oracle. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 021110-1307, USA.
24 * Callers for this were originally written against a very simple synchronus
25 * API. This implementation reflects those simple callers. Some day I'm sure
26 * we'll need to move to a more robust posting/callback mechanism.
28 * Transmit calls pass in kernel virtual addresses and block copying this into
29 * the socket's tx buffers via a usual blocking sendmsg. They'll block waiting
30 * for a failed socket to timeout. TX callers can also pass in a poniter to an
31 * 'int' which gets filled with an errno off the wire in response to the
34 * Handlers for unsolicited messages are registered. Each socket has a page
35 * that incoming data is copied into. First the header, then the data.
36 * Handlers are called from only one thread with a reference to this per-socket
37 * page. This page is destroyed after the handler call, so it can't be
38 * referenced beyond the call. Handlers may block but are discouraged from
41 * Any framing errors (bad magic, large payload lengths) close a connection.
43 * Our sock_container holds the state we associate with a socket. It's current
44 * framing state is held there as well as the refcounting we do around when it
45 * is safe to tear down the socket. The socket is only finally torn down from
46 * the container when the container loses all of its references -- so as long
47 * as you hold a ref on the container you can trust that the socket is valid
48 * for use with kernel socket APIs.
50 * Connections are initiated between a pair of nodes when the node with the
51 * higher node number gets a heartbeat callback which indicates that the lower
52 * numbered node has started heartbeating. The lower numbered node is passive
53 * and only accepts the connection if the higher numbered node is heartbeating.
56 #include <linux/kernel.h>
57 #include <linux/jiffies.h>
58 #include <linux/slab.h>
59 #include <linux/idr.h>
60 #include <linux/kref.h>
63 #include <asm/uaccess.h>
65 #include "heartbeat.h"
67 #include "nodemanager.h"
68 #define MLOG_MASK_PREFIX ML_TCP
72 #include "tcp_internal.h"
75 * The linux network stack isn't sparse endian clean.. It has macros like
76 * ntohs() which perform the endian checks and structs like sockaddr_in
77 * which aren't annotated. So __force is found here to get the build
78 * clean. When they emerge from the dark ages and annotate the code
79 * we can remove these.
82 #define SC_NODEF_FMT "node %s (num %u) at %u.%u.%u.%u:%u"
83 #define SC_NODEF_ARGS(sc) sc->sc_node->nd_name, sc->sc_node->nd_num, \
84 NIPQUAD(sc->sc_node->nd_ipv4_address), \
85 ntohs(sc->sc_node->nd_ipv4_port)
88 * In the following two log macros, the whitespace after the ',' just
89 * before ##args is intentional. Otherwise, gcc 2.95 will eat the
90 * previous token if args expands to nothing.
92 #define msglog(hdr, fmt, args...) do { \
93 typeof(hdr) __hdr = (hdr); \
94 mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d " \
95 "key %08x num %u] " fmt, \
96 be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), \
97 be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status), \
98 be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key), \
99 be32_to_cpu(__hdr->msg_num) , ##args); \
102 #define sclog(sc, fmt, args...) do { \
103 typeof(sc) __sc = (sc); \
104 mlog(ML_SOCKET, "[sc %p refs %d sock %p node %u page %p " \
105 "pg_off %zu] " fmt, __sc, \
106 atomic_read(&__sc->sc_kref.refcount), __sc->sc_sock, \
107 __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off , \
111 static DEFINE_RWLOCK(o2net_handler_lock);
112 static struct rb_root o2net_handler_tree = RB_ROOT;
114 static struct o2net_node o2net_nodes[O2NM_MAX_NODES];
116 /* XXX someday we'll need better accounting */
117 static struct socket *o2net_listen_sock = NULL;
120 * listen work is only queued by the listening socket callbacks on the
121 * o2net_wq. teardown detaches the callbacks before destroying the workqueue.
122 * quorum work is queued as sock containers are shutdown.. stop_listening
123 * tears down all the node's sock containers, preventing future shutdowns
124 * and queued quroum work, before canceling delayed quorum work and
125 * destroying the work queue.
127 static struct workqueue_struct *o2net_wq;
128 static struct work_struct o2net_listen_work;
130 static struct o2hb_callback_func o2net_hb_up, o2net_hb_down;
131 #define O2NET_HB_PRI 0x1
133 static struct o2net_handshake *o2net_hand;
134 static struct o2net_msg *o2net_keep_req, *o2net_keep_resp;
136 static int o2net_sys_err_translations[O2NET_ERR_MAX] =
137 {[O2NET_ERR_NONE] = 0,
138 [O2NET_ERR_NO_HNDLR] = -ENOPROTOOPT,
139 [O2NET_ERR_OVERFLOW] = -EOVERFLOW,
140 [O2NET_ERR_DIED] = -EHOSTDOWN,};
142 /* can't quite avoid *all* internal declarations :/ */
143 static void o2net_sc_connect_completed(struct work_struct *work);
144 static void o2net_rx_until_empty(struct work_struct *work);
145 static void o2net_shutdown_sc(struct work_struct *work);
146 static void o2net_listen_data_ready(struct sock *sk, int bytes);
147 static void o2net_sc_send_keep_req(struct work_struct *work);
148 static void o2net_idle_timer(unsigned long data);
149 static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
150 static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc);
153 * FIXME: These should use to_o2nm_cluster_from_node(), but we end up
154 * losing our parent link to the cluster during shutdown. This can be
155 * solved by adding a pre-removal callback to configfs, or passing
156 * around the cluster with the node. -jeffm
158 static inline int o2net_reconnect_delay(struct o2nm_node *node)
160 return o2nm_single_cluster->cl_reconnect_delay_ms;
163 static inline int o2net_keepalive_delay(struct o2nm_node *node)
165 return o2nm_single_cluster->cl_keepalive_delay_ms;
168 static inline int o2net_idle_timeout(struct o2nm_node *node)
170 return o2nm_single_cluster->cl_idle_timeout_ms;
173 static inline int o2net_sys_err_to_errno(enum o2net_system_error err)
176 BUG_ON(err >= O2NET_ERR_MAX);
177 trans = o2net_sys_err_translations[err];
179 /* Just in case we mess up the translation table above */
180 BUG_ON(err != O2NET_ERR_NONE && trans == 0);
184 static struct o2net_node * o2net_nn_from_num(u8 node_num)
186 BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes));
187 return &o2net_nodes[node_num];
190 static u8 o2net_num_from_nn(struct o2net_node *nn)
193 return nn - o2net_nodes;
196 /* ------------------------------------------------------------ */
198 static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw)
203 if (!idr_pre_get(&nn->nn_status_idr, GFP_ATOMIC)) {
207 spin_lock(&nn->nn_lock);
208 ret = idr_get_new(&nn->nn_status_idr, nsw, &nsw->ns_id);
210 list_add_tail(&nsw->ns_node_item,
211 &nn->nn_status_list);
212 spin_unlock(&nn->nn_lock);
213 } while (ret == -EAGAIN);
216 init_waitqueue_head(&nsw->ns_wq);
217 nsw->ns_sys_status = O2NET_ERR_NONE;
224 static void o2net_complete_nsw_locked(struct o2net_node *nn,
225 struct o2net_status_wait *nsw,
226 enum o2net_system_error sys_status,
229 assert_spin_locked(&nn->nn_lock);
231 if (!list_empty(&nsw->ns_node_item)) {
232 list_del_init(&nsw->ns_node_item);
233 nsw->ns_sys_status = sys_status;
234 nsw->ns_status = status;
235 idr_remove(&nn->nn_status_idr, nsw->ns_id);
236 wake_up(&nsw->ns_wq);
240 static void o2net_complete_nsw(struct o2net_node *nn,
241 struct o2net_status_wait *nsw,
242 u64 id, enum o2net_system_error sys_status,
245 spin_lock(&nn->nn_lock);
250 nsw = idr_find(&nn->nn_status_idr, id);
255 o2net_complete_nsw_locked(nn, nsw, sys_status, status);
258 spin_unlock(&nn->nn_lock);
262 static void o2net_complete_nodes_nsw(struct o2net_node *nn)
264 struct list_head *iter, *tmp;
265 unsigned int num_kills = 0;
266 struct o2net_status_wait *nsw;
268 assert_spin_locked(&nn->nn_lock);
270 list_for_each_safe(iter, tmp, &nn->nn_status_list) {
271 nsw = list_entry(iter, struct o2net_status_wait, ns_node_item);
272 o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
276 mlog(0, "completed %d messages for node %u\n", num_kills,
277 o2net_num_from_nn(nn));
280 static int o2net_nsw_completed(struct o2net_node *nn,
281 struct o2net_status_wait *nsw)
284 spin_lock(&nn->nn_lock);
285 completed = list_empty(&nsw->ns_node_item);
286 spin_unlock(&nn->nn_lock);
290 /* ------------------------------------------------------------ */
292 static void sc_kref_release(struct kref *kref)
294 struct o2net_sock_container *sc = container_of(kref,
295 struct o2net_sock_container, sc_kref);
296 BUG_ON(timer_pending(&sc->sc_idle_timeout));
298 sclog(sc, "releasing\n");
301 sock_release(sc->sc_sock);
305 o2nm_node_put(sc->sc_node);
311 static void sc_put(struct o2net_sock_container *sc)
314 kref_put(&sc->sc_kref, sc_kref_release);
316 static void sc_get(struct o2net_sock_container *sc)
319 kref_get(&sc->sc_kref);
321 static struct o2net_sock_container *sc_alloc(struct o2nm_node *node)
323 struct o2net_sock_container *sc, *ret = NULL;
324 struct page *page = NULL;
326 page = alloc_page(GFP_NOFS);
327 sc = kcalloc(1, sizeof(*sc), GFP_NOFS);
328 if (sc == NULL || page == NULL)
331 kref_init(&sc->sc_kref);
335 INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed);
336 INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty);
337 INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc);
338 INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req);
340 init_timer(&sc->sc_idle_timeout);
341 sc->sc_idle_timeout.function = o2net_idle_timer;
342 sc->sc_idle_timeout.data = (unsigned long)sc;
344 sclog(sc, "alloced\n");
359 /* ------------------------------------------------------------ */
361 static void o2net_sc_queue_work(struct o2net_sock_container *sc,
362 struct work_struct *work)
365 if (!queue_work(o2net_wq, work))
368 static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc,
369 struct delayed_work *work,
373 if (!queue_delayed_work(o2net_wq, work, delay))
376 static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc,
377 struct delayed_work *work)
379 if (cancel_delayed_work(work))
383 static void o2net_set_nn_state(struct o2net_node *nn,
384 struct o2net_sock_container *sc,
385 unsigned valid, int err)
387 int was_valid = nn->nn_sc_valid;
388 int was_err = nn->nn_persistent_error;
389 struct o2net_sock_container *old_sc = nn->nn_sc;
391 assert_spin_locked(&nn->nn_lock);
393 /* the node num comparison and single connect/accept path should stop
394 * an non-null sc from being overwritten with another */
395 BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
396 mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
397 mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);
399 /* we won't reconnect after our valid conn goes away for
400 * this hb iteration.. here so it shows up in the logs */
401 if (was_valid && !valid && err == 0)
404 mlog(ML_CONN, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
405 o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
406 nn->nn_persistent_error, err);
409 nn->nn_sc_valid = valid ? 1 : 0;
410 nn->nn_persistent_error = err;
412 /* mirrors o2net_tx_can_proceed() */
413 if (nn->nn_persistent_error || nn->nn_sc_valid)
414 wake_up(&nn->nn_sc_wq);
416 if (!was_err && nn->nn_persistent_error) {
417 o2quo_conn_err(o2net_num_from_nn(nn));
418 queue_delayed_work(o2net_wq, &nn->nn_still_up,
419 msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
422 if (was_valid && !valid) {
423 printk(KERN_INFO "o2net: no longer connected to "
424 SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
425 o2net_complete_nodes_nsw(nn);
428 if (!was_valid && valid) {
429 o2quo_conn_up(o2net_num_from_nn(nn));
430 /* this is a bit of a hack. we only try reconnecting
431 * when heartbeating starts until we get a connection.
432 * if that connection then dies we don't try reconnecting.
433 * the only way to start connecting again is to down
434 * heartbeat and bring it back up. */
435 cancel_delayed_work(&nn->nn_connect_expired);
436 printk(KERN_INFO "o2net: %s " SC_NODEF_FMT "\n",
437 o2nm_this_node() > sc->sc_node->nd_num ?
438 "connected to" : "accepted connection from",
442 /* trigger the connecting worker func as long as we're not valid,
443 * it will back off if it shouldn't connect. This can be called
444 * from node config teardown and so needs to be careful about
445 * the work queue actually being up. */
446 if (!valid && o2net_wq) {
448 /* delay if we're withing a RECONNECT_DELAY of the
450 delay = (nn->nn_last_connect_attempt +
451 msecs_to_jiffies(o2net_reconnect_delay(sc->sc_node)))
453 if (delay > msecs_to_jiffies(o2net_reconnect_delay(sc->sc_node)))
455 mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay);
456 queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);
459 /* keep track of the nn's sc ref for the caller */
460 if ((old_sc == NULL) && sc)
462 if (old_sc && (old_sc != sc)) {
463 o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
468 /* see o2net_register_callbacks() */
469 static void o2net_data_ready(struct sock *sk, int bytes)
471 void (*ready)(struct sock *sk, int bytes);
473 read_lock(&sk->sk_callback_lock);
474 if (sk->sk_user_data) {
475 struct o2net_sock_container *sc = sk->sk_user_data;
476 sclog(sc, "data_ready hit\n");
477 do_gettimeofday(&sc->sc_tv_data_ready);
478 o2net_sc_queue_work(sc, &sc->sc_rx_work);
479 ready = sc->sc_data_ready;
481 ready = sk->sk_data_ready;
483 read_unlock(&sk->sk_callback_lock);
488 /* see o2net_register_callbacks() */
489 static void o2net_state_change(struct sock *sk)
491 void (*state_change)(struct sock *sk);
492 struct o2net_sock_container *sc;
494 read_lock(&sk->sk_callback_lock);
495 sc = sk->sk_user_data;
497 state_change = sk->sk_state_change;
501 sclog(sc, "state_change to %d\n", sk->sk_state);
503 state_change = sc->sc_state_change;
505 switch(sk->sk_state) {
506 /* ignore connecting sockets as they make progress */
510 case TCP_ESTABLISHED:
511 o2net_sc_queue_work(sc, &sc->sc_connect_work);
514 o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
518 read_unlock(&sk->sk_callback_lock);
523 * we register callbacks so we can queue work on events before calling
524 * the original callbacks. our callbacks our careful to test user_data
525 * to discover when they've reaced with o2net_unregister_callbacks().
527 static void o2net_register_callbacks(struct sock *sk,
528 struct o2net_sock_container *sc)
530 write_lock_bh(&sk->sk_callback_lock);
532 /* accepted sockets inherit the old listen socket data ready */
533 if (sk->sk_data_ready == o2net_listen_data_ready) {
534 sk->sk_data_ready = sk->sk_user_data;
535 sk->sk_user_data = NULL;
538 BUG_ON(sk->sk_user_data != NULL);
539 sk->sk_user_data = sc;
542 sc->sc_data_ready = sk->sk_data_ready;
543 sc->sc_state_change = sk->sk_state_change;
544 sk->sk_data_ready = o2net_data_ready;
545 sk->sk_state_change = o2net_state_change;
547 write_unlock_bh(&sk->sk_callback_lock);
550 static int o2net_unregister_callbacks(struct sock *sk,
551 struct o2net_sock_container *sc)
555 write_lock_bh(&sk->sk_callback_lock);
556 if (sk->sk_user_data == sc) {
558 sk->sk_user_data = NULL;
559 sk->sk_data_ready = sc->sc_data_ready;
560 sk->sk_state_change = sc->sc_state_change;
562 write_unlock_bh(&sk->sk_callback_lock);
568 * this is a little helper that is called by callers who have seen a problem
569 * with an sc and want to detach it from the nn if someone already hasn't beat
570 * them to it. if an error is given then the shutdown will be persistent
571 * and pending transmits will be canceled.
573 static void o2net_ensure_shutdown(struct o2net_node *nn,
574 struct o2net_sock_container *sc,
577 spin_lock(&nn->nn_lock);
579 o2net_set_nn_state(nn, NULL, 0, err);
580 spin_unlock(&nn->nn_lock);
584 * This work queue function performs the blocking parts of socket shutdown. A
585 * few paths lead here. set_nn_state will trigger this callback if it sees an
586 * sc detached from the nn. state_change will also trigger this callback
587 * directly when it sees errors. In that case we need to call set_nn_state
588 * ourselves as state_change couldn't get the nn_lock and call set_nn_state
591 static void o2net_shutdown_sc(struct work_struct *work)
593 struct o2net_sock_container *sc =
594 container_of(work, struct o2net_sock_container,
596 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
598 sclog(sc, "shutting down\n");
600 /* drop the callbacks ref and call shutdown only once */
601 if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
602 /* we shouldn't flush as we're in the thread, the
603 * races with pending sc work structs are harmless */
604 del_timer_sync(&sc->sc_idle_timeout);
605 o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
607 sc->sc_sock->ops->shutdown(sc->sc_sock,
608 RCV_SHUTDOWN|SEND_SHUTDOWN);
611 /* not fatal so failed connects before the other guy has our
612 * heartbeat can be retried */
613 o2net_ensure_shutdown(nn, sc, 0);
617 /* ------------------------------------------------------------ */
619 static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
622 int ret = memcmp(&nmh->nh_key, &key, sizeof(key));
625 ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));
630 static struct o2net_msg_handler *
631 o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
632 struct rb_node **ret_parent)
634 struct rb_node **p = &o2net_handler_tree.rb_node;
635 struct rb_node *parent = NULL;
636 struct o2net_msg_handler *nmh, *ret = NULL;
641 nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
642 cmp = o2net_handler_cmp(nmh, msg_type, key);
656 if (ret_parent != NULL)
657 *ret_parent = parent;
662 static void o2net_handler_kref_release(struct kref *kref)
664 struct o2net_msg_handler *nmh;
665 nmh = container_of(kref, struct o2net_msg_handler, nh_kref);
670 static void o2net_handler_put(struct o2net_msg_handler *nmh)
672 kref_put(&nmh->nh_kref, o2net_handler_kref_release);
675 /* max_len is protection for the handler func. incoming messages won't
676 * be given to the handler if their payload is longer than the max. */
677 int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
678 o2net_msg_handler_func *func, void *data,
679 struct list_head *unreg_list)
681 struct o2net_msg_handler *nmh = NULL;
682 struct rb_node **p, *parent;
685 if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
686 mlog(0, "max_len for message handler out of range: %u\n",
693 mlog(0, "no message type provided: %u, %p\n", msg_type, func);
699 mlog(0, "no message handler provided: %u, %p\n",
705 nmh = kcalloc(1, sizeof(struct o2net_msg_handler), GFP_NOFS);
712 nmh->nh_func_data = data;
713 nmh->nh_msg_type = msg_type;
714 nmh->nh_max_len = max_len;
716 /* the tree and list get this ref.. they're both removed in
717 * unregister when this ref is dropped */
718 kref_init(&nmh->nh_kref);
719 INIT_LIST_HEAD(&nmh->nh_unregister_item);
721 write_lock(&o2net_handler_lock);
722 if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
725 rb_link_node(&nmh->nh_node, parent, p);
726 rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
727 list_add_tail(&nmh->nh_unregister_item, unreg_list);
729 mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
730 func, msg_type, key);
731 /* we've had some trouble with handlers seemingly vanishing. */
732 mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
734 "couldn't find handler we *just* registerd "
735 "for type %u key %08x\n", msg_type, key);
737 write_unlock(&o2net_handler_lock);
747 EXPORT_SYMBOL_GPL(o2net_register_handler);
749 void o2net_unregister_handler_list(struct list_head *list)
751 struct list_head *pos, *n;
752 struct o2net_msg_handler *nmh;
754 write_lock(&o2net_handler_lock);
755 list_for_each_safe(pos, n, list) {
756 nmh = list_entry(pos, struct o2net_msg_handler,
758 mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
759 nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
760 rb_erase(&nmh->nh_node, &o2net_handler_tree);
761 list_del_init(&nmh->nh_unregister_item);
762 kref_put(&nmh->nh_kref, o2net_handler_kref_release);
764 write_unlock(&o2net_handler_lock);
766 EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
768 static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
770 struct o2net_msg_handler *nmh;
772 read_lock(&o2net_handler_lock);
773 nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
775 kref_get(&nmh->nh_kref);
776 read_unlock(&o2net_handler_lock);
781 /* ------------------------------------------------------------ */
783 static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
791 struct msghdr msg = {
793 .msg_iov = (struct iovec *)&vec,
794 .msg_flags = MSG_DONTWAIT,
799 ret = sock_recvmsg(sock, &msg, len, msg.msg_flags);
805 static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
806 size_t veclen, size_t total)
810 struct msghdr msg = {
811 .msg_iov = (struct iovec *)vec,
812 .msg_iovlen = veclen,
822 ret = sock_sendmsg(sock, &msg, total);
825 mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret,
828 ret = -EPIPE; /* should be smarter, I bet */
835 mlog(0, "returning error: %d\n", ret);
839 static void o2net_sendpage(struct o2net_sock_container *sc,
840 void *kmalloced_virt,
843 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
847 ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
848 virt_to_page(kmalloced_virt),
849 (long)kmalloced_virt & ~PAGE_MASK,
852 mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
853 " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
854 o2net_ensure_shutdown(nn, sc, 0);
858 static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
860 memset(msg, 0, sizeof(struct o2net_msg));
861 msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
862 msg->data_len = cpu_to_be16(data_len);
863 msg->msg_type = cpu_to_be16(msg_type);
864 msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
866 msg->key = cpu_to_be32(key);
869 static int o2net_tx_can_proceed(struct o2net_node *nn,
870 struct o2net_sock_container **sc_ret,
875 spin_lock(&nn->nn_lock);
876 if (nn->nn_persistent_error) {
879 *error = nn->nn_persistent_error;
880 } else if (nn->nn_sc_valid) {
881 kref_get(&nn->nn_sc->sc_kref);
887 spin_unlock(&nn->nn_lock);
892 int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
893 size_t caller_veclen, u8 target_node, int *status)
896 struct o2net_msg *msg = NULL;
897 size_t veclen, caller_bytes = 0;
898 struct kvec *vec = NULL;
899 struct o2net_sock_container *sc = NULL;
900 struct o2net_node *nn = o2net_nn_from_num(target_node);
901 struct o2net_status_wait nsw = {
902 .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
905 if (o2net_wq == NULL) {
906 mlog(0, "attempt to tx without o2netd running\n");
911 if (caller_veclen == 0) {
912 mlog(0, "bad kvec array length\n");
917 caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
918 if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
919 mlog(0, "total payload len %zu too large\n", caller_bytes);
924 if (target_node == o2nm_this_node()) {
929 ret = wait_event_interruptible(nn->nn_sc_wq,
930 o2net_tx_can_proceed(nn, &sc, &error));
936 veclen = caller_veclen + 1;
937 vec = kmalloc(sizeof(struct kvec) * veclen, GFP_ATOMIC);
939 mlog(0, "failed to %zu element kvec!\n", veclen);
944 msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
946 mlog(0, "failed to allocate a o2net_msg!\n");
951 o2net_init_msg(msg, caller_bytes, msg_type, key);
953 vec[0].iov_len = sizeof(struct o2net_msg);
954 vec[0].iov_base = msg;
955 memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
957 ret = o2net_prep_nsw(nn, &nsw);
961 msg->msg_num = cpu_to_be32(nsw.ns_id);
963 /* finally, convert the message header to network byte-order
965 ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
966 sizeof(struct o2net_msg) + caller_bytes);
967 msglog(msg, "sending returned %d\n", ret);
969 mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
973 /* wait on other node's handler */
974 wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
976 /* Note that we avoid overwriting the callers status return
977 * variable if a system error was reported on the other
978 * side. Callers beware. */
979 ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
981 *status = nsw.ns_status;
983 mlog(0, "woken, returning system status %d, user status %d\n",
992 o2net_complete_nsw(nn, &nsw, 0, 0, 0);
995 EXPORT_SYMBOL_GPL(o2net_send_message_vec);
997 int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
998 u8 target_node, int *status)
1004 return o2net_send_message_vec(msg_type, key, &vec, 1,
1005 target_node, status);
1007 EXPORT_SYMBOL_GPL(o2net_send_message);
1009 static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
1010 enum o2net_system_error syserr, int err)
1014 .iov_len = sizeof(struct o2net_msg),
1017 BUG_ON(syserr >= O2NET_ERR_MAX);
1019 /* leave other fields intact from the incoming message, msg_num
1021 hdr->sys_status = cpu_to_be32(syserr);
1022 hdr->status = cpu_to_be32(err);
1023 hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic
1026 msglog(hdr, "about to send status magic %d\n", err);
1027 /* hdr has been in host byteorder this whole time */
1028 return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
1031 /* this returns -errno if the header was unknown or too large, etc.
1032 * after this is called the buffer us reused for the next message */
1033 static int o2net_process_message(struct o2net_sock_container *sc,
1034 struct o2net_msg *hdr)
1036 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
1037 int ret = 0, handler_status;
1038 enum o2net_system_error syserr;
1039 struct o2net_msg_handler *nmh = NULL;
1041 msglog(hdr, "processing message\n");
1043 o2net_sc_postpone_idle(sc);
1045 switch(be16_to_cpu(hdr->magic)) {
1046 case O2NET_MSG_STATUS_MAGIC:
1047 /* special type for returning message status */
1048 o2net_complete_nsw(nn, NULL,
1049 be32_to_cpu(hdr->msg_num),
1050 be32_to_cpu(hdr->sys_status),
1051 be32_to_cpu(hdr->status));
1053 case O2NET_MSG_KEEP_REQ_MAGIC:
1054 o2net_sendpage(sc, o2net_keep_resp,
1055 sizeof(*o2net_keep_resp));
1057 case O2NET_MSG_KEEP_RESP_MAGIC:
1059 case O2NET_MSG_MAGIC:
1062 msglog(hdr, "bad magic\n");
1068 /* find a handler for it */
1070 nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
1071 be32_to_cpu(hdr->key));
1073 mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
1074 be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
1075 syserr = O2NET_ERR_NO_HNDLR;
1079 syserr = O2NET_ERR_NONE;
1081 if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
1082 syserr = O2NET_ERR_OVERFLOW;
1084 if (syserr != O2NET_ERR_NONE)
1087 do_gettimeofday(&sc->sc_tv_func_start);
1088 sc->sc_msg_key = be32_to_cpu(hdr->key);
1089 sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
1090 handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
1091 be16_to_cpu(hdr->data_len),
1093 do_gettimeofday(&sc->sc_tv_func_stop);
1096 /* this destroys the hdr, so don't use it after this */
1097 ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
1100 mlog(0, "sending handler status %d, syserr %d returned %d\n",
1101 handler_status, syserr, ret);
1105 o2net_handler_put(nmh);
1109 static int o2net_check_handshake(struct o2net_sock_container *sc)
1111 struct o2net_handshake *hand = page_address(sc->sc_page);
1112 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
1114 if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
1115 mlog(ML_NOTICE, SC_NODEF_FMT " advertised net protocol "
1116 "version %llu but %llu is required, disconnecting\n",
1118 (unsigned long long)be64_to_cpu(hand->protocol_version),
1119 O2NET_PROTOCOL_VERSION);
1121 /* don't bother reconnecting if its the wrong version. */
1122 o2net_ensure_shutdown(nn, sc, -ENOTCONN);
1126 sc->sc_handshake_ok = 1;
1128 spin_lock(&nn->nn_lock);
1129 /* set valid and queue the idle timers only if it hasn't been
1130 * shut down already */
1131 if (nn->nn_sc == sc) {
1132 o2net_sc_reset_idle_timer(sc);
1133 o2net_set_nn_state(nn, sc, 1, 0);
1135 spin_unlock(&nn->nn_lock);
1137 /* shift everything up as though it wasn't there */
1138 sc->sc_page_off -= sizeof(struct o2net_handshake);
1139 if (sc->sc_page_off)
1140 memmove(hand, hand + 1, sc->sc_page_off);
1145 /* this demuxes the queued rx bytes into header or payload bits and calls
1146 * handlers as each full message is read off the socket. it returns -error,
1147 * == 0 eof, or > 0 for progress made.*/
1148 static int o2net_advance_rx(struct o2net_sock_container *sc)
1150 struct o2net_msg *hdr;
1155 sclog(sc, "receiving\n");
1156 do_gettimeofday(&sc->sc_tv_advance_start);
1158 /* do we need more header? */
1159 if (sc->sc_page_off < sizeof(struct o2net_msg)) {
1160 data = page_address(sc->sc_page) + sc->sc_page_off;
1161 datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
1162 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
1164 sc->sc_page_off += ret;
1166 /* this working relies on the handshake being
1167 * smaller than the normal message header */
1168 if (sc->sc_page_off >= sizeof(struct o2net_handshake)&&
1169 !sc->sc_handshake_ok && o2net_check_handshake(sc)) {
1174 /* only swab incoming here.. we can
1175 * only get here once as we cross from
1176 * being under to over */
1177 if (sc->sc_page_off == sizeof(struct o2net_msg)) {
1178 hdr = page_address(sc->sc_page);
1179 if (be16_to_cpu(hdr->data_len) >
1180 O2NET_MAX_PAYLOAD_BYTES)
1188 if (sc->sc_page_off < sizeof(struct o2net_msg)) {
1189 /* oof, still don't have a header */
1193 /* this was swabbed above when we first read it */
1194 hdr = page_address(sc->sc_page);
1196 msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
1198 /* do we need more payload? */
1199 if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
1200 /* need more payload */
1201 data = page_address(sc->sc_page) + sc->sc_page_off;
1202 datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
1204 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
1206 sc->sc_page_off += ret;
1211 if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
1212 /* we can only get here once, the first time we read
1213 * the payload.. so set ret to progress if the handler
1214 * works out. after calling this the message is toast */
1215 ret = o2net_process_message(sc, hdr);
1218 sc->sc_page_off = 0;
1222 sclog(sc, "ret = %d\n", ret);
1223 do_gettimeofday(&sc->sc_tv_advance_stop);
1227 /* this work func is triggerd by data ready. it reads until it can read no
1228 * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
1229 * our work the work struct will be marked and we'll be called again. */
1230 static void o2net_rx_until_empty(struct work_struct *work)
1232 struct o2net_sock_container *sc =
1233 container_of(work, struct o2net_sock_container, sc_rx_work);
1237 ret = o2net_advance_rx(sc);
1240 if (ret <= 0 && ret != -EAGAIN) {
1241 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
1242 sclog(sc, "saw error %d, closing\n", ret);
1243 /* not permanent so read failed handshake can retry */
1244 o2net_ensure_shutdown(nn, sc, 0);
1250 static int o2net_set_nodelay(struct socket *sock)
1259 * Dear unsuspecting programmer,
1261 * Don't use sock_setsockopt() for SOL_TCP. It doesn't check its level
1262 * argument and assumes SOL_SOCKET so, say, your TCP_NODELAY will
1263 * silently turn into SO_DEBUG.
1266 * Keeper of hilariously fragile interfaces.
1268 ret = sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY,
1269 (char __user *)&val, sizeof(val));
1275 /* ------------------------------------------------------------ */
1277 /* called when a connect completes and after a sock is accepted. the
1278 * rx path will see the response and mark the sc valid */
1279 static void o2net_sc_connect_completed(struct work_struct *work)
1281 struct o2net_sock_container *sc =
1282 container_of(work, struct o2net_sock_container,
1285 mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
1286 (unsigned long long)O2NET_PROTOCOL_VERSION,
1287 (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
1289 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
1293 /* this is called as a work_struct func. */
1294 static void o2net_sc_send_keep_req(struct work_struct *work)
1296 struct o2net_sock_container *sc =
1297 container_of(work, struct o2net_sock_container,
1298 sc_keepalive_work.work);
1300 o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
1304 /* socket shutdown does a del_timer_sync against this as it tears down.
1305 * we can't start this timer until we've got to the point in sc buildup
1306 * where shutdown is going to be involved */
1307 static void o2net_idle_timer(unsigned long data)
1309 struct o2net_sock_container *sc = (struct o2net_sock_container *)data;
1312 do_gettimeofday(&now);
1314 printk(KERN_INFO "o2net: connection to " SC_NODEF_FMT " has been idle for %u.%u "
1315 "seconds, shutting it down.\n", SC_NODEF_ARGS(sc),
1316 o2net_idle_timeout(sc->sc_node) / 1000,
1317 o2net_idle_timeout(sc->sc_node) % 1000);
1318 mlog(ML_NOTICE, "here are some times that might help debug the "
1319 "situation: (tmr %ld.%ld now %ld.%ld dr %ld.%ld adv "
1320 "%ld.%ld:%ld.%ld func (%08x:%u) %ld.%ld:%ld.%ld)\n",
1321 sc->sc_tv_timer.tv_sec, (long) sc->sc_tv_timer.tv_usec,
1322 now.tv_sec, (long) now.tv_usec,
1323 sc->sc_tv_data_ready.tv_sec, (long) sc->sc_tv_data_ready.tv_usec,
1324 sc->sc_tv_advance_start.tv_sec,
1325 (long) sc->sc_tv_advance_start.tv_usec,
1326 sc->sc_tv_advance_stop.tv_sec,
1327 (long) sc->sc_tv_advance_stop.tv_usec,
1328 sc->sc_msg_key, sc->sc_msg_type,
1329 sc->sc_tv_func_start.tv_sec, (long) sc->sc_tv_func_start.tv_usec,
1330 sc->sc_tv_func_stop.tv_sec, (long) sc->sc_tv_func_stop.tv_usec);
1332 o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
1335 static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
1337 o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
1338 o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
1339 msecs_to_jiffies(o2net_keepalive_delay(sc->sc_node)));
1340 do_gettimeofday(&sc->sc_tv_timer);
1341 mod_timer(&sc->sc_idle_timeout,
1342 jiffies + msecs_to_jiffies(o2net_idle_timeout(sc->sc_node)));
1345 static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
1347 /* Only push out an existing timer */
1348 if (timer_pending(&sc->sc_idle_timeout))
1349 o2net_sc_reset_idle_timer(sc);
1352 /* this work func is kicked whenever a path sets the nn state which doesn't
1353 * have valid set. This includes seeing hb come up, losing a connection,
1354 * having a connect attempt fail, etc. This centralizes the logic which decides
1355 * if a connect attempt should be made or if we should give up and all future
1356 * transmit attempts should fail */
1357 static void o2net_start_connect(struct work_struct *work)
1359 struct o2net_node *nn =
1360 container_of(work, struct o2net_node, nn_connect_work.work);
1361 struct o2net_sock_container *sc = NULL;
1362 struct o2nm_node *node = NULL, *mynode = NULL;
1363 struct socket *sock = NULL;
1364 struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
1367 /* if we're greater we initiate tx, otherwise we accept */
1368 if (o2nm_this_node() <= o2net_num_from_nn(nn))
1371 /* watch for racing with tearing a node down */
1372 node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
1378 mynode = o2nm_get_node_by_num(o2nm_this_node());
1379 if (mynode == NULL) {
1384 spin_lock(&nn->nn_lock);
1385 /* see if we already have one pending or have given up */
1386 stop = (nn->nn_sc || nn->nn_persistent_error);
1387 spin_unlock(&nn->nn_lock);
1391 nn->nn_last_connect_attempt = jiffies;
1393 sc = sc_alloc(node);
1395 mlog(0, "couldn't allocate sc\n");
1400 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1402 mlog(0, "can't create socket: %d\n", ret);
1405 sc->sc_sock = sock; /* freed by sc_kref_release */
1407 sock->sk->sk_allocation = GFP_ATOMIC;
1409 myaddr.sin_family = AF_INET;
1410 myaddr.sin_addr.s_addr = (__force u32)mynode->nd_ipv4_address;
1411 myaddr.sin_port = (__force u16)htons(0); /* any port */
1413 ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
1416 mlog(ML_ERROR, "bind failed with %d at address %u.%u.%u.%u\n",
1417 ret, NIPQUAD(mynode->nd_ipv4_address));
1421 ret = o2net_set_nodelay(sc->sc_sock);
1423 mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
1427 o2net_register_callbacks(sc->sc_sock->sk, sc);
1429 spin_lock(&nn->nn_lock);
1430 /* handshake completion will set nn->nn_sc_valid */
1431 o2net_set_nn_state(nn, sc, 0, 0);
1432 spin_unlock(&nn->nn_lock);
1434 remoteaddr.sin_family = AF_INET;
1435 remoteaddr.sin_addr.s_addr = (__force u32)node->nd_ipv4_address;
1436 remoteaddr.sin_port = (__force u16)node->nd_ipv4_port;
1438 ret = sc->sc_sock->ops->connect(sc->sc_sock,
1439 (struct sockaddr *)&remoteaddr,
1442 if (ret == -EINPROGRESS)
1447 mlog(ML_NOTICE, "connect attempt to " SC_NODEF_FMT " failed "
1448 "with errno %d\n", SC_NODEF_ARGS(sc), ret);
1449 /* 0 err so that another will be queued and attempted
1450 * from set_nn_state */
1452 o2net_ensure_shutdown(nn, sc, 0);
1457 o2nm_node_put(node);
1459 o2nm_node_put(mynode);
1464 static void o2net_connect_expired(struct work_struct *work)
1466 struct o2net_node *nn =
1467 container_of(work, struct o2net_node, nn_connect_expired.work);
1469 spin_lock(&nn->nn_lock);
1470 if (!nn->nn_sc_valid) {
1471 struct o2nm_node *node = nn->nn_sc->sc_node;
1472 mlog(ML_ERROR, "no connection established with node %u after "
1473 "%u.%u seconds, giving up and returning errors.\n",
1474 o2net_num_from_nn(nn),
1475 o2net_idle_timeout(node) / 1000,
1476 o2net_idle_timeout(node) % 1000);
1478 o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
1480 spin_unlock(&nn->nn_lock);
1483 static void o2net_still_up(struct work_struct *work)
1485 struct o2net_node *nn =
1486 container_of(work, struct o2net_node, nn_still_up.work);
1488 o2quo_hb_still_up(o2net_num_from_nn(nn));
1491 /* ------------------------------------------------------------ */
1493 void o2net_disconnect_node(struct o2nm_node *node)
1495 struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
1497 /* don't reconnect until it's heartbeating again */
1498 spin_lock(&nn->nn_lock);
1499 o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
1500 spin_unlock(&nn->nn_lock);
1503 cancel_delayed_work(&nn->nn_connect_expired);
1504 cancel_delayed_work(&nn->nn_connect_work);
1505 cancel_delayed_work(&nn->nn_still_up);
1506 flush_workqueue(o2net_wq);
1510 static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
1513 o2quo_hb_down(node_num);
1515 if (node_num != o2nm_this_node())
1516 o2net_disconnect_node(node);
1519 static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
1522 struct o2net_node *nn = o2net_nn_from_num(node_num);
1524 o2quo_hb_up(node_num);
1526 /* ensure an immediate connect attempt */
1527 nn->nn_last_connect_attempt = jiffies -
1528 (msecs_to_jiffies(o2net_reconnect_delay(node)) + 1);
1530 if (node_num != o2nm_this_node()) {
1531 /* heartbeat doesn't work unless a local node number is
1532 * configured and doing so brings up the o2net_wq, so we can
1534 queue_delayed_work(o2net_wq, &nn->nn_connect_expired,
1535 msecs_to_jiffies(o2net_idle_timeout(node)));
1537 /* believe it or not, accept and node hearbeating testing
1538 * can succeed for this node before we got here.. so
1539 * only use set_nn_state to clear the persistent error
1540 * if that hasn't already happened */
1541 spin_lock(&nn->nn_lock);
1542 if (nn->nn_persistent_error)
1543 o2net_set_nn_state(nn, NULL, 0, 0);
1544 spin_unlock(&nn->nn_lock);
1548 void o2net_unregister_hb_callbacks(void)
1552 ret = o2hb_unregister_callback(&o2net_hb_up);
1554 mlog(ML_ERROR, "Status return %d unregistering heartbeat up "
1555 "callback!\n", ret);
1557 ret = o2hb_unregister_callback(&o2net_hb_down);
1559 mlog(ML_ERROR, "Status return %d unregistering heartbeat down "
1560 "callback!\n", ret);
1563 int o2net_register_hb_callbacks(void)
1567 o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
1568 o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
1569 o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
1570 o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
1572 ret = o2hb_register_callback(&o2net_hb_up);
1574 ret = o2hb_register_callback(&o2net_hb_down);
1577 o2net_unregister_hb_callbacks();
1582 /* ------------------------------------------------------------ */
1584 static int o2net_accept_one(struct socket *sock)
1587 struct sockaddr_in sin;
1588 struct socket *new_sock = NULL;
1589 struct o2nm_node *node = NULL;
1590 struct o2net_sock_container *sc = NULL;
1591 struct o2net_node *nn;
1593 BUG_ON(sock == NULL);
1594 ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
1595 sock->sk->sk_protocol, &new_sock);
1599 new_sock->type = sock->type;
1600 new_sock->ops = sock->ops;
1601 ret = sock->ops->accept(sock, new_sock, O_NONBLOCK);
1605 new_sock->sk->sk_allocation = GFP_ATOMIC;
1607 ret = o2net_set_nodelay(new_sock);
1609 mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
1614 ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin,
1619 node = o2nm_get_node_by_ip((__force __be32)sin.sin_addr.s_addr);
1621 mlog(ML_NOTICE, "attempt to connect from unknown node at "
1622 "%u.%u.%u.%u:%d\n", NIPQUAD(sin.sin_addr.s_addr),
1623 ntohs((__force __be16)sin.sin_port));
1628 if (o2nm_this_node() > node->nd_num) {
1629 mlog(ML_NOTICE, "unexpected connect attempted from a lower "
1630 "numbered node '%s' at " "%u.%u.%u.%u:%d with num %u\n",
1631 node->nd_name, NIPQUAD(sin.sin_addr.s_addr),
1632 ntohs((__force __be16)sin.sin_port), node->nd_num);
1637 /* this happens all the time when the other node sees our heartbeat
1638 * and tries to connect before we see their heartbeat */
1639 if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
1640 mlog(ML_CONN, "attempt to connect from node '%s' at "
1641 "%u.%u.%u.%u:%d but it isn't heartbeating\n",
1642 node->nd_name, NIPQUAD(sin.sin_addr.s_addr),
1643 ntohs((__force __be16)sin.sin_port));
1648 nn = o2net_nn_from_num(node->nd_num);
1650 spin_lock(&nn->nn_lock);
1655 spin_unlock(&nn->nn_lock);
1657 mlog(ML_NOTICE, "attempt to connect from node '%s' at "
1658 "%u.%u.%u.%u:%d but it already has an open connection\n",
1659 node->nd_name, NIPQUAD(sin.sin_addr.s_addr),
1660 ntohs((__force __be16)sin.sin_port));
1664 sc = sc_alloc(node);
1670 sc->sc_sock = new_sock;
1673 spin_lock(&nn->nn_lock);
1674 o2net_set_nn_state(nn, sc, 0, 0);
1675 spin_unlock(&nn->nn_lock);
1677 o2net_register_callbacks(sc->sc_sock->sk, sc);
1678 o2net_sc_queue_work(sc, &sc->sc_rx_work);
1680 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
1684 sock_release(new_sock);
1686 o2nm_node_put(node);
1692 static void o2net_accept_many(struct work_struct *work)
1694 struct socket *sock = o2net_listen_sock;
1695 while (o2net_accept_one(sock) == 0)
1699 static void o2net_listen_data_ready(struct sock *sk, int bytes)
1701 void (*ready)(struct sock *sk, int bytes);
1703 read_lock(&sk->sk_callback_lock);
1704 ready = sk->sk_user_data;
1705 if (ready == NULL) { /* check for teardown race */
1706 ready = sk->sk_data_ready;
1710 /* ->sk_data_ready is also called for a newly established child socket
1711 * before it has been accepted and the acceptor has set up their
1712 * data_ready.. we only want to queue listen work for our listening
1714 if (sk->sk_state == TCP_LISTEN) {
1715 mlog(ML_TCP, "bytes: %d\n", bytes);
1716 queue_work(o2net_wq, &o2net_listen_work);
1720 read_unlock(&sk->sk_callback_lock);
1724 static int o2net_open_listening_sock(__be16 port)
1726 struct socket *sock = NULL;
1728 struct sockaddr_in sin = {
1729 .sin_family = PF_INET,
1730 .sin_addr = { .s_addr = (__force u32)htonl(INADDR_ANY) },
1731 .sin_port = (__force u16)port,
1734 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1736 mlog(ML_ERROR, "unable to create socket, ret=%d\n", ret);
1740 sock->sk->sk_allocation = GFP_ATOMIC;
1742 write_lock_bh(&sock->sk->sk_callback_lock);
1743 sock->sk->sk_user_data = sock->sk->sk_data_ready;
1744 sock->sk->sk_data_ready = o2net_listen_data_ready;
1745 write_unlock_bh(&sock->sk->sk_callback_lock);
1747 o2net_listen_sock = sock;
1748 INIT_WORK(&o2net_listen_work, o2net_accept_many);
1750 sock->sk->sk_reuse = 1;
1751 ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
1753 mlog(ML_ERROR, "unable to bind socket to port %d, ret=%d\n",
1758 ret = sock->ops->listen(sock, 64);
1760 mlog(ML_ERROR, "unable to listen on port %d, ret=%d\n",
1766 o2net_listen_sock = NULL;
1774 * called from node manager when we should bring up our network listening
1775 * socket. node manager handles all the serialization to only call this
1776 * once and to match it with o2net_stop_listening(). note,
1777 * o2nm_this_node() doesn't work yet as we're being called while it
1780 int o2net_start_listening(struct o2nm_node *node)
1784 BUG_ON(o2net_wq != NULL);
1785 BUG_ON(o2net_listen_sock != NULL);
1787 mlog(ML_KTHREAD, "starting o2net thread...\n");
1788 o2net_wq = create_singlethread_workqueue("o2net");
1789 if (o2net_wq == NULL) {
1790 mlog(ML_ERROR, "unable to launch o2net thread\n");
1791 return -ENOMEM; /* ? */
1794 ret = o2net_open_listening_sock(node->nd_ipv4_port);
1796 destroy_workqueue(o2net_wq);
1799 o2quo_conn_up(node->nd_num);
1804 /* again, o2nm_this_node() doesn't work here as we're involved in
1805 * tearing it down */
1806 void o2net_stop_listening(struct o2nm_node *node)
1808 struct socket *sock = o2net_listen_sock;
1811 BUG_ON(o2net_wq == NULL);
1812 BUG_ON(o2net_listen_sock == NULL);
1814 /* stop the listening socket from generating work */
1815 write_lock_bh(&sock->sk->sk_callback_lock);
1816 sock->sk->sk_data_ready = sock->sk->sk_user_data;
1817 sock->sk->sk_user_data = NULL;
1818 write_unlock_bh(&sock->sk->sk_callback_lock);
1820 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
1821 struct o2nm_node *node = o2nm_get_node_by_num(i);
1823 o2net_disconnect_node(node);
1824 o2nm_node_put(node);
1828 /* finish all work and tear down the work queue */
1829 mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
1830 destroy_workqueue(o2net_wq);
1833 sock_release(o2net_listen_sock);
1834 o2net_listen_sock = NULL;
1836 o2quo_conn_err(node->nd_num);
1839 /* ------------------------------------------------------------ */
1841 int o2net_init(void)
1847 o2net_hand = kcalloc(1, sizeof(struct o2net_handshake), GFP_KERNEL);
1848 o2net_keep_req = kcalloc(1, sizeof(struct o2net_msg), GFP_KERNEL);
1849 o2net_keep_resp = kcalloc(1, sizeof(struct o2net_msg), GFP_KERNEL);
1850 if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp) {
1852 kfree(o2net_keep_req);
1853 kfree(o2net_keep_resp);
1857 o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
1858 o2net_hand->connector_id = cpu_to_be64(1);
1860 o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
1861 o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
1863 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
1864 struct o2net_node *nn = o2net_nn_from_num(i);
1866 spin_lock_init(&nn->nn_lock);
1867 INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
1868 INIT_DELAYED_WORK(&nn->nn_connect_expired,
1869 o2net_connect_expired);
1870 INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
1871 /* until we see hb from a node we'll return einval */
1872 nn->nn_persistent_error = -ENOTCONN;
1873 init_waitqueue_head(&nn->nn_sc_wq);
1874 idr_init(&nn->nn_status_idr);
1875 INIT_LIST_HEAD(&nn->nn_status_list);
1881 void o2net_exit(void)
1885 kfree(o2net_keep_req);
1886 kfree(o2net_keep_resp);