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>
61 #include <linux/net.h>
64 #include <asm/uaccess.h>
66 #include "heartbeat.h"
68 #include "nodemanager.h"
69 #define MLOG_MASK_PREFIX ML_TCP
73 #include "tcp_internal.h"
75 #define SC_NODEF_FMT "node %s (num %u) at %pI4:%u"
76 #define SC_NODEF_ARGS(sc) sc->sc_node->nd_name, sc->sc_node->nd_num, \
77 &sc->sc_node->nd_ipv4_address, \
78 ntohs(sc->sc_node->nd_ipv4_port)
81 * In the following two log macros, the whitespace after the ',' just
82 * before ##args is intentional. Otherwise, gcc 2.95 will eat the
83 * previous token if args expands to nothing.
85 #define msglog(hdr, fmt, args...) do { \
86 typeof(hdr) __hdr = (hdr); \
87 mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d " \
88 "key %08x num %u] " fmt, \
89 be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), \
90 be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status), \
91 be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key), \
92 be32_to_cpu(__hdr->msg_num) , ##args); \
95 #define sclog(sc, fmt, args...) do { \
96 typeof(sc) __sc = (sc); \
97 mlog(ML_SOCKET, "[sc %p refs %d sock %p node %u page %p " \
98 "pg_off %zu] " fmt, __sc, \
99 atomic_read(&__sc->sc_kref.refcount), __sc->sc_sock, \
100 __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off , \
104 static DEFINE_RWLOCK(o2net_handler_lock
);
105 static struct rb_root o2net_handler_tree
= RB_ROOT
;
107 static struct o2net_node o2net_nodes
[O2NM_MAX_NODES
];
109 static struct socket
*o2net_listen_sock
= NULL
;
112 * listen work is only queued by the listening socket callbacks on the
113 * o2net_wq. teardown detaches the callbacks before destroying the workqueue.
114 * quorum work is queued as sock containers are shutdown.. stop_listening
115 * tears down all the node's sock containers, preventing future shutdowns
116 * and queued quroum work, before canceling delayed quorum work and
117 * destroying the work queue.
119 static struct workqueue_struct
*o2net_wq
;
120 static struct work_struct o2net_listen_work
;
122 static struct o2hb_callback_func o2net_hb_up
, o2net_hb_down
;
123 #define O2NET_HB_PRI 0x1
125 static struct o2net_handshake
*o2net_hand
;
126 static struct o2net_msg
*o2net_keep_req
, *o2net_keep_resp
;
128 static int o2net_sys_err_translations
[O2NET_ERR_MAX
] =
129 {[O2NET_ERR_NONE
] = 0,
130 [O2NET_ERR_NO_HNDLR
] = -ENOPROTOOPT
,
131 [O2NET_ERR_OVERFLOW
] = -EOVERFLOW
,
132 [O2NET_ERR_DIED
] = -EHOSTDOWN
,};
134 /* can't quite avoid *all* internal declarations :/ */
135 static void o2net_sc_connect_completed(struct work_struct
*work
);
136 static void o2net_rx_until_empty(struct work_struct
*work
);
137 static void o2net_shutdown_sc(struct work_struct
*work
);
138 static void o2net_listen_data_ready(struct sock
*sk
, int bytes
);
139 static void o2net_sc_send_keep_req(struct work_struct
*work
);
140 static void o2net_idle_timer(unsigned long data
);
141 static void o2net_sc_postpone_idle(struct o2net_sock_container
*sc
);
142 static void o2net_sc_reset_idle_timer(struct o2net_sock_container
*sc
);
144 #ifdef CONFIG_DEBUG_FS
145 static void o2net_init_nst(struct o2net_send_tracking
*nst
, u32 msgtype
,
146 u32 msgkey
, struct task_struct
*task
, u8 node
)
148 INIT_LIST_HEAD(&nst
->st_net_debug_item
);
150 nst
->st_msg_type
= msgtype
;
151 nst
->st_msg_key
= msgkey
;
155 static void o2net_set_nst_sock_time(struct o2net_send_tracking
*nst
)
157 do_gettimeofday(&nst
->st_sock_time
);
160 static void o2net_set_nst_send_time(struct o2net_send_tracking
*nst
)
162 do_gettimeofday(&nst
->st_send_time
);
165 static void o2net_set_nst_status_time(struct o2net_send_tracking
*nst
)
167 do_gettimeofday(&nst
->st_status_time
);
170 static void o2net_set_nst_sock_container(struct o2net_send_tracking
*nst
,
171 struct o2net_sock_container
*sc
)
176 static void o2net_set_nst_msg_id(struct o2net_send_tracking
*nst
, u32 msg_id
)
181 #else /* CONFIG_DEBUG_FS */
183 static inline void o2net_init_nst(struct o2net_send_tracking
*nst
, u32 msgtype
,
184 u32 msgkey
, struct task_struct
*task
, u8 node
)
188 static inline void o2net_set_nst_sock_time(struct o2net_send_tracking
*nst
)
192 static inline void o2net_set_nst_send_time(struct o2net_send_tracking
*nst
)
196 static inline void o2net_set_nst_status_time(struct o2net_send_tracking
*nst
)
200 static inline void o2net_set_nst_sock_container(struct o2net_send_tracking
*nst
,
201 struct o2net_sock_container
*sc
)
205 static inline void o2net_set_nst_msg_id(struct o2net_send_tracking
*nst
,
210 #endif /* CONFIG_DEBUG_FS */
212 static inline int o2net_reconnect_delay(void)
214 return o2nm_single_cluster
->cl_reconnect_delay_ms
;
217 static inline int o2net_keepalive_delay(void)
219 return o2nm_single_cluster
->cl_keepalive_delay_ms
;
222 static inline int o2net_idle_timeout(void)
224 return o2nm_single_cluster
->cl_idle_timeout_ms
;
227 static inline int o2net_sys_err_to_errno(enum o2net_system_error err
)
230 BUG_ON(err
>= O2NET_ERR_MAX
);
231 trans
= o2net_sys_err_translations
[err
];
233 /* Just in case we mess up the translation table above */
234 BUG_ON(err
!= O2NET_ERR_NONE
&& trans
== 0);
238 static struct o2net_node
* o2net_nn_from_num(u8 node_num
)
240 BUG_ON(node_num
>= ARRAY_SIZE(o2net_nodes
));
241 return &o2net_nodes
[node_num
];
244 static u8
o2net_num_from_nn(struct o2net_node
*nn
)
247 return nn
- o2net_nodes
;
250 /* ------------------------------------------------------------ */
252 static int o2net_prep_nsw(struct o2net_node
*nn
, struct o2net_status_wait
*nsw
)
257 if (!idr_pre_get(&nn
->nn_status_idr
, GFP_ATOMIC
)) {
261 spin_lock(&nn
->nn_lock
);
262 ret
= idr_get_new(&nn
->nn_status_idr
, nsw
, &nsw
->ns_id
);
264 list_add_tail(&nsw
->ns_node_item
,
265 &nn
->nn_status_list
);
266 spin_unlock(&nn
->nn_lock
);
267 } while (ret
== -EAGAIN
);
270 init_waitqueue_head(&nsw
->ns_wq
);
271 nsw
->ns_sys_status
= O2NET_ERR_NONE
;
278 static void o2net_complete_nsw_locked(struct o2net_node
*nn
,
279 struct o2net_status_wait
*nsw
,
280 enum o2net_system_error sys_status
,
283 assert_spin_locked(&nn
->nn_lock
);
285 if (!list_empty(&nsw
->ns_node_item
)) {
286 list_del_init(&nsw
->ns_node_item
);
287 nsw
->ns_sys_status
= sys_status
;
288 nsw
->ns_status
= status
;
289 idr_remove(&nn
->nn_status_idr
, nsw
->ns_id
);
290 wake_up(&nsw
->ns_wq
);
294 static void o2net_complete_nsw(struct o2net_node
*nn
,
295 struct o2net_status_wait
*nsw
,
296 u64 id
, enum o2net_system_error sys_status
,
299 spin_lock(&nn
->nn_lock
);
304 nsw
= idr_find(&nn
->nn_status_idr
, id
);
309 o2net_complete_nsw_locked(nn
, nsw
, sys_status
, status
);
312 spin_unlock(&nn
->nn_lock
);
316 static void o2net_complete_nodes_nsw(struct o2net_node
*nn
)
318 struct o2net_status_wait
*nsw
, *tmp
;
319 unsigned int num_kills
= 0;
321 assert_spin_locked(&nn
->nn_lock
);
323 list_for_each_entry_safe(nsw
, tmp
, &nn
->nn_status_list
, ns_node_item
) {
324 o2net_complete_nsw_locked(nn
, nsw
, O2NET_ERR_DIED
, 0);
328 mlog(0, "completed %d messages for node %u\n", num_kills
,
329 o2net_num_from_nn(nn
));
332 static int o2net_nsw_completed(struct o2net_node
*nn
,
333 struct o2net_status_wait
*nsw
)
336 spin_lock(&nn
->nn_lock
);
337 completed
= list_empty(&nsw
->ns_node_item
);
338 spin_unlock(&nn
->nn_lock
);
342 /* ------------------------------------------------------------ */
344 static void sc_kref_release(struct kref
*kref
)
346 struct o2net_sock_container
*sc
= container_of(kref
,
347 struct o2net_sock_container
, sc_kref
);
348 BUG_ON(timer_pending(&sc
->sc_idle_timeout
));
350 sclog(sc
, "releasing\n");
353 sock_release(sc
->sc_sock
);
357 o2nm_node_put(sc
->sc_node
);
360 o2net_debug_del_sc(sc
);
364 static void sc_put(struct o2net_sock_container
*sc
)
367 kref_put(&sc
->sc_kref
, sc_kref_release
);
369 static void sc_get(struct o2net_sock_container
*sc
)
372 kref_get(&sc
->sc_kref
);
374 static struct o2net_sock_container
*sc_alloc(struct o2nm_node
*node
)
376 struct o2net_sock_container
*sc
, *ret
= NULL
;
377 struct page
*page
= NULL
;
379 page
= alloc_page(GFP_NOFS
);
380 sc
= kzalloc(sizeof(*sc
), GFP_NOFS
);
381 if (sc
== NULL
|| page
== NULL
)
384 kref_init(&sc
->sc_kref
);
388 INIT_WORK(&sc
->sc_connect_work
, o2net_sc_connect_completed
);
389 INIT_WORK(&sc
->sc_rx_work
, o2net_rx_until_empty
);
390 INIT_WORK(&sc
->sc_shutdown_work
, o2net_shutdown_sc
);
391 INIT_DELAYED_WORK(&sc
->sc_keepalive_work
, o2net_sc_send_keep_req
);
393 init_timer(&sc
->sc_idle_timeout
);
394 sc
->sc_idle_timeout
.function
= o2net_idle_timer
;
395 sc
->sc_idle_timeout
.data
= (unsigned long)sc
;
397 sclog(sc
, "alloced\n");
401 o2net_debug_add_sc(sc
);
413 /* ------------------------------------------------------------ */
415 static void o2net_sc_queue_work(struct o2net_sock_container
*sc
,
416 struct work_struct
*work
)
419 if (!queue_work(o2net_wq
, work
))
422 static void o2net_sc_queue_delayed_work(struct o2net_sock_container
*sc
,
423 struct delayed_work
*work
,
427 if (!queue_delayed_work(o2net_wq
, work
, delay
))
430 static void o2net_sc_cancel_delayed_work(struct o2net_sock_container
*sc
,
431 struct delayed_work
*work
)
433 if (cancel_delayed_work(work
))
437 static atomic_t o2net_connected_peers
= ATOMIC_INIT(0);
439 int o2net_num_connected_peers(void)
441 return atomic_read(&o2net_connected_peers
);
444 static void o2net_set_nn_state(struct o2net_node
*nn
,
445 struct o2net_sock_container
*sc
,
446 unsigned valid
, int err
)
448 int was_valid
= nn
->nn_sc_valid
;
449 int was_err
= nn
->nn_persistent_error
;
450 struct o2net_sock_container
*old_sc
= nn
->nn_sc
;
452 assert_spin_locked(&nn
->nn_lock
);
455 atomic_dec(&o2net_connected_peers
);
456 else if (!old_sc
&& sc
)
457 atomic_inc(&o2net_connected_peers
);
459 /* the node num comparison and single connect/accept path should stop
460 * an non-null sc from being overwritten with another */
461 BUG_ON(sc
&& nn
->nn_sc
&& nn
->nn_sc
!= sc
);
462 mlog_bug_on_msg(err
&& valid
, "err %d valid %u\n", err
, valid
);
463 mlog_bug_on_msg(valid
&& !sc
, "valid %u sc %p\n", valid
, sc
);
465 if (was_valid
&& !valid
&& err
== 0)
468 mlog(ML_CONN
, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
469 o2net_num_from_nn(nn
), nn
->nn_sc
, sc
, nn
->nn_sc_valid
, valid
,
470 nn
->nn_persistent_error
, err
);
473 nn
->nn_sc_valid
= valid
? 1 : 0;
474 nn
->nn_persistent_error
= err
;
476 /* mirrors o2net_tx_can_proceed() */
477 if (nn
->nn_persistent_error
|| nn
->nn_sc_valid
)
478 wake_up(&nn
->nn_sc_wq
);
480 if (!was_err
&& nn
->nn_persistent_error
) {
481 o2quo_conn_err(o2net_num_from_nn(nn
));
482 queue_delayed_work(o2net_wq
, &nn
->nn_still_up
,
483 msecs_to_jiffies(O2NET_QUORUM_DELAY_MS
));
486 if (was_valid
&& !valid
) {
487 printk(KERN_NOTICE
"o2net: no longer connected to "
488 SC_NODEF_FMT
"\n", SC_NODEF_ARGS(old_sc
));
489 o2net_complete_nodes_nsw(nn
);
492 if (!was_valid
&& valid
) {
493 o2quo_conn_up(o2net_num_from_nn(nn
));
494 cancel_delayed_work(&nn
->nn_connect_expired
);
495 printk(KERN_NOTICE
"o2net: %s " SC_NODEF_FMT
"\n",
496 o2nm_this_node() > sc
->sc_node
->nd_num
?
497 "connected to" : "accepted connection from",
501 /* trigger the connecting worker func as long as we're not valid,
502 * it will back off if it shouldn't connect. This can be called
503 * from node config teardown and so needs to be careful about
504 * the work queue actually being up. */
505 if (!valid
&& o2net_wq
) {
507 /* delay if we're withing a RECONNECT_DELAY of the
509 delay
= (nn
->nn_last_connect_attempt
+
510 msecs_to_jiffies(o2net_reconnect_delay()))
512 if (delay
> msecs_to_jiffies(o2net_reconnect_delay()))
514 mlog(ML_CONN
, "queueing conn attempt in %lu jiffies\n", delay
);
515 queue_delayed_work(o2net_wq
, &nn
->nn_connect_work
, delay
);
518 * Delay the expired work after idle timeout.
520 * We might have lots of failed connection attempts that run
521 * through here but we only cancel the connect_expired work when
522 * a connection attempt succeeds. So only the first enqueue of
523 * the connect_expired work will do anything. The rest will see
524 * that it's already queued and do nothing.
526 delay
+= msecs_to_jiffies(o2net_idle_timeout());
527 queue_delayed_work(o2net_wq
, &nn
->nn_connect_expired
, delay
);
530 /* keep track of the nn's sc ref for the caller */
531 if ((old_sc
== NULL
) && sc
)
533 if (old_sc
&& (old_sc
!= sc
)) {
534 o2net_sc_queue_work(old_sc
, &old_sc
->sc_shutdown_work
);
539 /* see o2net_register_callbacks() */
540 static void o2net_data_ready(struct sock
*sk
, int bytes
)
542 void (*ready
)(struct sock
*sk
, int bytes
);
544 read_lock(&sk
->sk_callback_lock
);
545 if (sk
->sk_user_data
) {
546 struct o2net_sock_container
*sc
= sk
->sk_user_data
;
547 sclog(sc
, "data_ready hit\n");
548 do_gettimeofday(&sc
->sc_tv_data_ready
);
549 o2net_sc_queue_work(sc
, &sc
->sc_rx_work
);
550 ready
= sc
->sc_data_ready
;
552 ready
= sk
->sk_data_ready
;
554 read_unlock(&sk
->sk_callback_lock
);
559 /* see o2net_register_callbacks() */
560 static void o2net_state_change(struct sock
*sk
)
562 void (*state_change
)(struct sock
*sk
);
563 struct o2net_sock_container
*sc
;
565 read_lock(&sk
->sk_callback_lock
);
566 sc
= sk
->sk_user_data
;
568 state_change
= sk
->sk_state_change
;
572 sclog(sc
, "state_change to %d\n", sk
->sk_state
);
574 state_change
= sc
->sc_state_change
;
576 switch(sk
->sk_state
) {
577 /* ignore connecting sockets as they make progress */
581 case TCP_ESTABLISHED
:
582 o2net_sc_queue_work(sc
, &sc
->sc_connect_work
);
585 printk(KERN_INFO
"o2net: connection to " SC_NODEF_FMT
586 " shutdown, state %d\n",
587 SC_NODEF_ARGS(sc
), sk
->sk_state
);
588 o2net_sc_queue_work(sc
, &sc
->sc_shutdown_work
);
592 read_unlock(&sk
->sk_callback_lock
);
597 * we register callbacks so we can queue work on events before calling
598 * the original callbacks. our callbacks our careful to test user_data
599 * to discover when they've reaced with o2net_unregister_callbacks().
601 static void o2net_register_callbacks(struct sock
*sk
,
602 struct o2net_sock_container
*sc
)
604 write_lock_bh(&sk
->sk_callback_lock
);
606 /* accepted sockets inherit the old listen socket data ready */
607 if (sk
->sk_data_ready
== o2net_listen_data_ready
) {
608 sk
->sk_data_ready
= sk
->sk_user_data
;
609 sk
->sk_user_data
= NULL
;
612 BUG_ON(sk
->sk_user_data
!= NULL
);
613 sk
->sk_user_data
= sc
;
616 sc
->sc_data_ready
= sk
->sk_data_ready
;
617 sc
->sc_state_change
= sk
->sk_state_change
;
618 sk
->sk_data_ready
= o2net_data_ready
;
619 sk
->sk_state_change
= o2net_state_change
;
621 mutex_init(&sc
->sc_send_lock
);
623 write_unlock_bh(&sk
->sk_callback_lock
);
626 static int o2net_unregister_callbacks(struct sock
*sk
,
627 struct o2net_sock_container
*sc
)
631 write_lock_bh(&sk
->sk_callback_lock
);
632 if (sk
->sk_user_data
== sc
) {
634 sk
->sk_user_data
= NULL
;
635 sk
->sk_data_ready
= sc
->sc_data_ready
;
636 sk
->sk_state_change
= sc
->sc_state_change
;
638 write_unlock_bh(&sk
->sk_callback_lock
);
644 * this is a little helper that is called by callers who have seen a problem
645 * with an sc and want to detach it from the nn if someone already hasn't beat
646 * them to it. if an error is given then the shutdown will be persistent
647 * and pending transmits will be canceled.
649 static void o2net_ensure_shutdown(struct o2net_node
*nn
,
650 struct o2net_sock_container
*sc
,
653 spin_lock(&nn
->nn_lock
);
655 o2net_set_nn_state(nn
, NULL
, 0, err
);
656 spin_unlock(&nn
->nn_lock
);
660 * This work queue function performs the blocking parts of socket shutdown. A
661 * few paths lead here. set_nn_state will trigger this callback if it sees an
662 * sc detached from the nn. state_change will also trigger this callback
663 * directly when it sees errors. In that case we need to call set_nn_state
664 * ourselves as state_change couldn't get the nn_lock and call set_nn_state
667 static void o2net_shutdown_sc(struct work_struct
*work
)
669 struct o2net_sock_container
*sc
=
670 container_of(work
, struct o2net_sock_container
,
672 struct o2net_node
*nn
= o2net_nn_from_num(sc
->sc_node
->nd_num
);
674 sclog(sc
, "shutting down\n");
676 /* drop the callbacks ref and call shutdown only once */
677 if (o2net_unregister_callbacks(sc
->sc_sock
->sk
, sc
)) {
678 /* we shouldn't flush as we're in the thread, the
679 * races with pending sc work structs are harmless */
680 del_timer_sync(&sc
->sc_idle_timeout
);
681 o2net_sc_cancel_delayed_work(sc
, &sc
->sc_keepalive_work
);
683 kernel_sock_shutdown(sc
->sc_sock
, SHUT_RDWR
);
686 /* not fatal so failed connects before the other guy has our
687 * heartbeat can be retried */
688 o2net_ensure_shutdown(nn
, sc
, 0);
692 /* ------------------------------------------------------------ */
694 static int o2net_handler_cmp(struct o2net_msg_handler
*nmh
, u32 msg_type
,
697 int ret
= memcmp(&nmh
->nh_key
, &key
, sizeof(key
));
700 ret
= memcmp(&nmh
->nh_msg_type
, &msg_type
, sizeof(msg_type
));
705 static struct o2net_msg_handler
*
706 o2net_handler_tree_lookup(u32 msg_type
, u32 key
, struct rb_node
***ret_p
,
707 struct rb_node
**ret_parent
)
709 struct rb_node
**p
= &o2net_handler_tree
.rb_node
;
710 struct rb_node
*parent
= NULL
;
711 struct o2net_msg_handler
*nmh
, *ret
= NULL
;
716 nmh
= rb_entry(parent
, struct o2net_msg_handler
, nh_node
);
717 cmp
= o2net_handler_cmp(nmh
, msg_type
, key
);
731 if (ret_parent
!= NULL
)
732 *ret_parent
= parent
;
737 static void o2net_handler_kref_release(struct kref
*kref
)
739 struct o2net_msg_handler
*nmh
;
740 nmh
= container_of(kref
, struct o2net_msg_handler
, nh_kref
);
745 static void o2net_handler_put(struct o2net_msg_handler
*nmh
)
747 kref_put(&nmh
->nh_kref
, o2net_handler_kref_release
);
750 /* max_len is protection for the handler func. incoming messages won't
751 * be given to the handler if their payload is longer than the max. */
752 int o2net_register_handler(u32 msg_type
, u32 key
, u32 max_len
,
753 o2net_msg_handler_func
*func
, void *data
,
754 o2net_post_msg_handler_func
*post_func
,
755 struct list_head
*unreg_list
)
757 struct o2net_msg_handler
*nmh
= NULL
;
758 struct rb_node
**p
, *parent
;
761 if (max_len
> O2NET_MAX_PAYLOAD_BYTES
) {
762 mlog(0, "max_len for message handler out of range: %u\n",
769 mlog(0, "no message type provided: %u, %p\n", msg_type
, func
);
775 mlog(0, "no message handler provided: %u, %p\n",
781 nmh
= kzalloc(sizeof(struct o2net_msg_handler
), GFP_NOFS
);
788 nmh
->nh_func_data
= data
;
789 nmh
->nh_post_func
= post_func
;
790 nmh
->nh_msg_type
= msg_type
;
791 nmh
->nh_max_len
= max_len
;
793 /* the tree and list get this ref.. they're both removed in
794 * unregister when this ref is dropped */
795 kref_init(&nmh
->nh_kref
);
796 INIT_LIST_HEAD(&nmh
->nh_unregister_item
);
798 write_lock(&o2net_handler_lock
);
799 if (o2net_handler_tree_lookup(msg_type
, key
, &p
, &parent
))
802 rb_link_node(&nmh
->nh_node
, parent
, p
);
803 rb_insert_color(&nmh
->nh_node
, &o2net_handler_tree
);
804 list_add_tail(&nmh
->nh_unregister_item
, unreg_list
);
806 mlog(ML_TCP
, "registered handler func %p type %u key %08x\n",
807 func
, msg_type
, key
);
808 /* we've had some trouble with handlers seemingly vanishing. */
809 mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type
, key
, &p
,
811 "couldn't find handler we *just* registerd "
812 "for type %u key %08x\n", msg_type
, key
);
814 write_unlock(&o2net_handler_lock
);
824 EXPORT_SYMBOL_GPL(o2net_register_handler
);
826 void o2net_unregister_handler_list(struct list_head
*list
)
828 struct o2net_msg_handler
*nmh
, *n
;
830 write_lock(&o2net_handler_lock
);
831 list_for_each_entry_safe(nmh
, n
, list
, nh_unregister_item
) {
832 mlog(ML_TCP
, "unregistering handler func %p type %u key %08x\n",
833 nmh
->nh_func
, nmh
->nh_msg_type
, nmh
->nh_key
);
834 rb_erase(&nmh
->nh_node
, &o2net_handler_tree
);
835 list_del_init(&nmh
->nh_unregister_item
);
836 kref_put(&nmh
->nh_kref
, o2net_handler_kref_release
);
838 write_unlock(&o2net_handler_lock
);
840 EXPORT_SYMBOL_GPL(o2net_unregister_handler_list
);
842 static struct o2net_msg_handler
*o2net_handler_get(u32 msg_type
, u32 key
)
844 struct o2net_msg_handler
*nmh
;
846 read_lock(&o2net_handler_lock
);
847 nmh
= o2net_handler_tree_lookup(msg_type
, key
, NULL
, NULL
);
849 kref_get(&nmh
->nh_kref
);
850 read_unlock(&o2net_handler_lock
);
855 /* ------------------------------------------------------------ */
857 static int o2net_recv_tcp_msg(struct socket
*sock
, void *data
, size_t len
)
865 struct msghdr msg
= {
867 .msg_iov
= (struct iovec
*)&vec
,
868 .msg_flags
= MSG_DONTWAIT
,
873 ret
= sock_recvmsg(sock
, &msg
, len
, msg
.msg_flags
);
879 static int o2net_send_tcp_msg(struct socket
*sock
, struct kvec
*vec
,
880 size_t veclen
, size_t total
)
884 struct msghdr msg
= {
885 .msg_iov
= (struct iovec
*)vec
,
886 .msg_iovlen
= veclen
,
896 ret
= sock_sendmsg(sock
, &msg
, total
);
899 mlog(ML_ERROR
, "sendmsg returned %d instead of %zu\n", ret
,
902 ret
= -EPIPE
; /* should be smarter, I bet */
909 mlog(0, "returning error: %d\n", ret
);
913 static void o2net_sendpage(struct o2net_sock_container
*sc
,
914 void *kmalloced_virt
,
917 struct o2net_node
*nn
= o2net_nn_from_num(sc
->sc_node
->nd_num
);
921 mutex_lock(&sc
->sc_send_lock
);
922 ret
= sc
->sc_sock
->ops
->sendpage(sc
->sc_sock
,
923 virt_to_page(kmalloced_virt
),
924 (long)kmalloced_virt
& ~PAGE_MASK
,
926 mutex_unlock(&sc
->sc_send_lock
);
929 if (ret
== (ssize_t
)-EAGAIN
) {
930 mlog(0, "sendpage of size %zu to " SC_NODEF_FMT
931 " returned EAGAIN\n", size
, SC_NODEF_ARGS(sc
));
935 mlog(ML_ERROR
, "sendpage of size %zu to " SC_NODEF_FMT
936 " failed with %zd\n", size
, SC_NODEF_ARGS(sc
), ret
);
937 o2net_ensure_shutdown(nn
, sc
, 0);
942 static void o2net_init_msg(struct o2net_msg
*msg
, u16 data_len
, u16 msg_type
, u32 key
)
944 memset(msg
, 0, sizeof(struct o2net_msg
));
945 msg
->magic
= cpu_to_be16(O2NET_MSG_MAGIC
);
946 msg
->data_len
= cpu_to_be16(data_len
);
947 msg
->msg_type
= cpu_to_be16(msg_type
);
948 msg
->sys_status
= cpu_to_be32(O2NET_ERR_NONE
);
950 msg
->key
= cpu_to_be32(key
);
953 static int o2net_tx_can_proceed(struct o2net_node
*nn
,
954 struct o2net_sock_container
**sc_ret
,
959 spin_lock(&nn
->nn_lock
);
960 if (nn
->nn_persistent_error
) {
963 *error
= nn
->nn_persistent_error
;
964 } else if (nn
->nn_sc_valid
) {
965 kref_get(&nn
->nn_sc
->sc_kref
);
971 spin_unlock(&nn
->nn_lock
);
976 int o2net_send_message_vec(u32 msg_type
, u32 key
, struct kvec
*caller_vec
,
977 size_t caller_veclen
, u8 target_node
, int *status
)
980 struct o2net_msg
*msg
= NULL
;
981 size_t veclen
, caller_bytes
= 0;
982 struct kvec
*vec
= NULL
;
983 struct o2net_sock_container
*sc
= NULL
;
984 struct o2net_node
*nn
= o2net_nn_from_num(target_node
);
985 struct o2net_status_wait nsw
= {
986 .ns_node_item
= LIST_HEAD_INIT(nsw
.ns_node_item
),
988 struct o2net_send_tracking nst
;
990 o2net_init_nst(&nst
, msg_type
, key
, current
, target_node
);
992 if (o2net_wq
== NULL
) {
993 mlog(0, "attempt to tx without o2netd running\n");
998 if (caller_veclen
== 0) {
999 mlog(0, "bad kvec array length\n");
1004 caller_bytes
= iov_length((struct iovec
*)caller_vec
, caller_veclen
);
1005 if (caller_bytes
> O2NET_MAX_PAYLOAD_BYTES
) {
1006 mlog(0, "total payload len %zu too large\n", caller_bytes
);
1011 if (target_node
== o2nm_this_node()) {
1016 o2net_debug_add_nst(&nst
);
1018 o2net_set_nst_sock_time(&nst
);
1020 wait_event(nn
->nn_sc_wq
, o2net_tx_can_proceed(nn
, &sc
, &ret
));
1024 o2net_set_nst_sock_container(&nst
, sc
);
1026 veclen
= caller_veclen
+ 1;
1027 vec
= kmalloc(sizeof(struct kvec
) * veclen
, GFP_ATOMIC
);
1029 mlog(0, "failed to %zu element kvec!\n", veclen
);
1034 msg
= kmalloc(sizeof(struct o2net_msg
), GFP_ATOMIC
);
1036 mlog(0, "failed to allocate a o2net_msg!\n");
1041 o2net_init_msg(msg
, caller_bytes
, msg_type
, key
);
1043 vec
[0].iov_len
= sizeof(struct o2net_msg
);
1044 vec
[0].iov_base
= msg
;
1045 memcpy(&vec
[1], caller_vec
, caller_veclen
* sizeof(struct kvec
));
1047 ret
= o2net_prep_nsw(nn
, &nsw
);
1051 msg
->msg_num
= cpu_to_be32(nsw
.ns_id
);
1052 o2net_set_nst_msg_id(&nst
, nsw
.ns_id
);
1054 o2net_set_nst_send_time(&nst
);
1056 /* finally, convert the message header to network byte-order
1058 mutex_lock(&sc
->sc_send_lock
);
1059 ret
= o2net_send_tcp_msg(sc
->sc_sock
, vec
, veclen
,
1060 sizeof(struct o2net_msg
) + caller_bytes
);
1061 mutex_unlock(&sc
->sc_send_lock
);
1062 msglog(msg
, "sending returned %d\n", ret
);
1064 mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret
);
1068 /* wait on other node's handler */
1069 o2net_set_nst_status_time(&nst
);
1070 wait_event(nsw
.ns_wq
, o2net_nsw_completed(nn
, &nsw
));
1072 /* Note that we avoid overwriting the callers status return
1073 * variable if a system error was reported on the other
1074 * side. Callers beware. */
1075 ret
= o2net_sys_err_to_errno(nsw
.ns_sys_status
);
1077 *status
= nsw
.ns_status
;
1079 mlog(0, "woken, returning system status %d, user status %d\n",
1080 ret
, nsw
.ns_status
);
1082 o2net_debug_del_nst(&nst
); /* must be before dropping sc and node */
1089 o2net_complete_nsw(nn
, &nsw
, 0, 0, 0);
1092 EXPORT_SYMBOL_GPL(o2net_send_message_vec
);
1094 int o2net_send_message(u32 msg_type
, u32 key
, void *data
, u32 len
,
1095 u8 target_node
, int *status
)
1101 return o2net_send_message_vec(msg_type
, key
, &vec
, 1,
1102 target_node
, status
);
1104 EXPORT_SYMBOL_GPL(o2net_send_message
);
1106 static int o2net_send_status_magic(struct socket
*sock
, struct o2net_msg
*hdr
,
1107 enum o2net_system_error syserr
, int err
)
1111 .iov_len
= sizeof(struct o2net_msg
),
1114 BUG_ON(syserr
>= O2NET_ERR_MAX
);
1116 /* leave other fields intact from the incoming message, msg_num
1118 hdr
->sys_status
= cpu_to_be32(syserr
);
1119 hdr
->status
= cpu_to_be32(err
);
1120 hdr
->magic
= cpu_to_be16(O2NET_MSG_STATUS_MAGIC
); // twiddle the magic
1123 msglog(hdr
, "about to send status magic %d\n", err
);
1124 /* hdr has been in host byteorder this whole time */
1125 return o2net_send_tcp_msg(sock
, &vec
, 1, sizeof(struct o2net_msg
));
1128 /* this returns -errno if the header was unknown or too large, etc.
1129 * after this is called the buffer us reused for the next message */
1130 static int o2net_process_message(struct o2net_sock_container
*sc
,
1131 struct o2net_msg
*hdr
)
1133 struct o2net_node
*nn
= o2net_nn_from_num(sc
->sc_node
->nd_num
);
1134 int ret
= 0, handler_status
;
1135 enum o2net_system_error syserr
;
1136 struct o2net_msg_handler
*nmh
= NULL
;
1137 void *ret_data
= NULL
;
1139 msglog(hdr
, "processing message\n");
1141 o2net_sc_postpone_idle(sc
);
1143 switch(be16_to_cpu(hdr
->magic
)) {
1144 case O2NET_MSG_STATUS_MAGIC
:
1145 /* special type for returning message status */
1146 o2net_complete_nsw(nn
, NULL
,
1147 be32_to_cpu(hdr
->msg_num
),
1148 be32_to_cpu(hdr
->sys_status
),
1149 be32_to_cpu(hdr
->status
));
1151 case O2NET_MSG_KEEP_REQ_MAGIC
:
1152 o2net_sendpage(sc
, o2net_keep_resp
,
1153 sizeof(*o2net_keep_resp
));
1155 case O2NET_MSG_KEEP_RESP_MAGIC
:
1157 case O2NET_MSG_MAGIC
:
1160 msglog(hdr
, "bad magic\n");
1166 /* find a handler for it */
1168 nmh
= o2net_handler_get(be16_to_cpu(hdr
->msg_type
),
1169 be32_to_cpu(hdr
->key
));
1171 mlog(ML_TCP
, "couldn't find handler for type %u key %08x\n",
1172 be16_to_cpu(hdr
->msg_type
), be32_to_cpu(hdr
->key
));
1173 syserr
= O2NET_ERR_NO_HNDLR
;
1177 syserr
= O2NET_ERR_NONE
;
1179 if (be16_to_cpu(hdr
->data_len
) > nmh
->nh_max_len
)
1180 syserr
= O2NET_ERR_OVERFLOW
;
1182 if (syserr
!= O2NET_ERR_NONE
)
1185 do_gettimeofday(&sc
->sc_tv_func_start
);
1186 sc
->sc_msg_key
= be32_to_cpu(hdr
->key
);
1187 sc
->sc_msg_type
= be16_to_cpu(hdr
->msg_type
);
1188 handler_status
= (nmh
->nh_func
)(hdr
, sizeof(struct o2net_msg
) +
1189 be16_to_cpu(hdr
->data_len
),
1190 nmh
->nh_func_data
, &ret_data
);
1191 do_gettimeofday(&sc
->sc_tv_func_stop
);
1194 /* this destroys the hdr, so don't use it after this */
1195 mutex_lock(&sc
->sc_send_lock
);
1196 ret
= o2net_send_status_magic(sc
->sc_sock
, hdr
, syserr
,
1198 mutex_unlock(&sc
->sc_send_lock
);
1200 mlog(0, "sending handler status %d, syserr %d returned %d\n",
1201 handler_status
, syserr
, ret
);
1204 BUG_ON(ret_data
!= NULL
&& nmh
->nh_post_func
== NULL
);
1205 if (nmh
->nh_post_func
)
1206 (nmh
->nh_post_func
)(handler_status
, nmh
->nh_func_data
,
1212 o2net_handler_put(nmh
);
1216 static int o2net_check_handshake(struct o2net_sock_container
*sc
)
1218 struct o2net_handshake
*hand
= page_address(sc
->sc_page
);
1219 struct o2net_node
*nn
= o2net_nn_from_num(sc
->sc_node
->nd_num
);
1221 if (hand
->protocol_version
!= cpu_to_be64(O2NET_PROTOCOL_VERSION
)) {
1222 mlog(ML_NOTICE
, SC_NODEF_FMT
" advertised net protocol "
1223 "version %llu but %llu is required, disconnecting\n",
1225 (unsigned long long)be64_to_cpu(hand
->protocol_version
),
1226 O2NET_PROTOCOL_VERSION
);
1228 /* don't bother reconnecting if its the wrong version. */
1229 o2net_ensure_shutdown(nn
, sc
, -ENOTCONN
);
1234 * Ensure timeouts are consistent with other nodes, otherwise
1235 * we can end up with one node thinking that the other must be down,
1236 * but isn't. This can ultimately cause corruption.
1238 if (be32_to_cpu(hand
->o2net_idle_timeout_ms
) !=
1239 o2net_idle_timeout()) {
1240 mlog(ML_NOTICE
, SC_NODEF_FMT
" uses a network idle timeout of "
1241 "%u ms, but we use %u ms locally. disconnecting\n",
1243 be32_to_cpu(hand
->o2net_idle_timeout_ms
),
1244 o2net_idle_timeout());
1245 o2net_ensure_shutdown(nn
, sc
, -ENOTCONN
);
1249 if (be32_to_cpu(hand
->o2net_keepalive_delay_ms
) !=
1250 o2net_keepalive_delay()) {
1251 mlog(ML_NOTICE
, SC_NODEF_FMT
" uses a keepalive delay of "
1252 "%u ms, but we use %u ms locally. disconnecting\n",
1254 be32_to_cpu(hand
->o2net_keepalive_delay_ms
),
1255 o2net_keepalive_delay());
1256 o2net_ensure_shutdown(nn
, sc
, -ENOTCONN
);
1260 if (be32_to_cpu(hand
->o2hb_heartbeat_timeout_ms
) !=
1261 O2HB_MAX_WRITE_TIMEOUT_MS
) {
1262 mlog(ML_NOTICE
, SC_NODEF_FMT
" uses a heartbeat timeout of "
1263 "%u ms, but we use %u ms locally. disconnecting\n",
1265 be32_to_cpu(hand
->o2hb_heartbeat_timeout_ms
),
1266 O2HB_MAX_WRITE_TIMEOUT_MS
);
1267 o2net_ensure_shutdown(nn
, sc
, -ENOTCONN
);
1271 sc
->sc_handshake_ok
= 1;
1273 spin_lock(&nn
->nn_lock
);
1274 /* set valid and queue the idle timers only if it hasn't been
1275 * shut down already */
1276 if (nn
->nn_sc
== sc
) {
1277 o2net_sc_reset_idle_timer(sc
);
1278 atomic_set(&nn
->nn_timeout
, 0);
1279 o2net_set_nn_state(nn
, sc
, 1, 0);
1281 spin_unlock(&nn
->nn_lock
);
1283 /* shift everything up as though it wasn't there */
1284 sc
->sc_page_off
-= sizeof(struct o2net_handshake
);
1285 if (sc
->sc_page_off
)
1286 memmove(hand
, hand
+ 1, sc
->sc_page_off
);
1291 /* this demuxes the queued rx bytes into header or payload bits and calls
1292 * handlers as each full message is read off the socket. it returns -error,
1293 * == 0 eof, or > 0 for progress made.*/
1294 static int o2net_advance_rx(struct o2net_sock_container
*sc
)
1296 struct o2net_msg
*hdr
;
1301 sclog(sc
, "receiving\n");
1302 do_gettimeofday(&sc
->sc_tv_advance_start
);
1304 if (unlikely(sc
->sc_handshake_ok
== 0)) {
1305 if(sc
->sc_page_off
< sizeof(struct o2net_handshake
)) {
1306 data
= page_address(sc
->sc_page
) + sc
->sc_page_off
;
1307 datalen
= sizeof(struct o2net_handshake
) - sc
->sc_page_off
;
1308 ret
= o2net_recv_tcp_msg(sc
->sc_sock
, data
, datalen
);
1310 sc
->sc_page_off
+= ret
;
1313 if (sc
->sc_page_off
== sizeof(struct o2net_handshake
)) {
1314 o2net_check_handshake(sc
);
1315 if (unlikely(sc
->sc_handshake_ok
== 0))
1321 /* do we need more header? */
1322 if (sc
->sc_page_off
< sizeof(struct o2net_msg
)) {
1323 data
= page_address(sc
->sc_page
) + sc
->sc_page_off
;
1324 datalen
= sizeof(struct o2net_msg
) - sc
->sc_page_off
;
1325 ret
= o2net_recv_tcp_msg(sc
->sc_sock
, data
, datalen
);
1327 sc
->sc_page_off
+= ret
;
1328 /* only swab incoming here.. we can
1329 * only get here once as we cross from
1330 * being under to over */
1331 if (sc
->sc_page_off
== sizeof(struct o2net_msg
)) {
1332 hdr
= page_address(sc
->sc_page
);
1333 if (be16_to_cpu(hdr
->data_len
) >
1334 O2NET_MAX_PAYLOAD_BYTES
)
1342 if (sc
->sc_page_off
< sizeof(struct o2net_msg
)) {
1343 /* oof, still don't have a header */
1347 /* this was swabbed above when we first read it */
1348 hdr
= page_address(sc
->sc_page
);
1350 msglog(hdr
, "at page_off %zu\n", sc
->sc_page_off
);
1352 /* do we need more payload? */
1353 if (sc
->sc_page_off
- sizeof(struct o2net_msg
) < be16_to_cpu(hdr
->data_len
)) {
1354 /* need more payload */
1355 data
= page_address(sc
->sc_page
) + sc
->sc_page_off
;
1356 datalen
= (sizeof(struct o2net_msg
) + be16_to_cpu(hdr
->data_len
)) -
1358 ret
= o2net_recv_tcp_msg(sc
->sc_sock
, data
, datalen
);
1360 sc
->sc_page_off
+= ret
;
1365 if (sc
->sc_page_off
- sizeof(struct o2net_msg
) == be16_to_cpu(hdr
->data_len
)) {
1366 /* we can only get here once, the first time we read
1367 * the payload.. so set ret to progress if the handler
1368 * works out. after calling this the message is toast */
1369 ret
= o2net_process_message(sc
, hdr
);
1372 sc
->sc_page_off
= 0;
1376 sclog(sc
, "ret = %d\n", ret
);
1377 do_gettimeofday(&sc
->sc_tv_advance_stop
);
1381 /* this work func is triggerd by data ready. it reads until it can read no
1382 * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
1383 * our work the work struct will be marked and we'll be called again. */
1384 static void o2net_rx_until_empty(struct work_struct
*work
)
1386 struct o2net_sock_container
*sc
=
1387 container_of(work
, struct o2net_sock_container
, sc_rx_work
);
1391 ret
= o2net_advance_rx(sc
);
1394 if (ret
<= 0 && ret
!= -EAGAIN
) {
1395 struct o2net_node
*nn
= o2net_nn_from_num(sc
->sc_node
->nd_num
);
1396 sclog(sc
, "saw error %d, closing\n", ret
);
1397 /* not permanent so read failed handshake can retry */
1398 o2net_ensure_shutdown(nn
, sc
, 0);
1404 static int o2net_set_nodelay(struct socket
*sock
)
1413 * Dear unsuspecting programmer,
1415 * Don't use sock_setsockopt() for SOL_TCP. It doesn't check its level
1416 * argument and assumes SOL_SOCKET so, say, your TCP_NODELAY will
1417 * silently turn into SO_DEBUG.
1420 * Keeper of hilariously fragile interfaces.
1422 ret
= sock
->ops
->setsockopt(sock
, SOL_TCP
, TCP_NODELAY
,
1423 (char __user
*)&val
, sizeof(val
));
1429 static void o2net_initialize_handshake(void)
1431 o2net_hand
->o2hb_heartbeat_timeout_ms
= cpu_to_be32(
1432 O2HB_MAX_WRITE_TIMEOUT_MS
);
1433 o2net_hand
->o2net_idle_timeout_ms
= cpu_to_be32(o2net_idle_timeout());
1434 o2net_hand
->o2net_keepalive_delay_ms
= cpu_to_be32(
1435 o2net_keepalive_delay());
1436 o2net_hand
->o2net_reconnect_delay_ms
= cpu_to_be32(
1437 o2net_reconnect_delay());
1440 /* ------------------------------------------------------------ */
1442 /* called when a connect completes and after a sock is accepted. the
1443 * rx path will see the response and mark the sc valid */
1444 static void o2net_sc_connect_completed(struct work_struct
*work
)
1446 struct o2net_sock_container
*sc
=
1447 container_of(work
, struct o2net_sock_container
,
1450 mlog(ML_MSG
, "sc sending handshake with ver %llu id %llx\n",
1451 (unsigned long long)O2NET_PROTOCOL_VERSION
,
1452 (unsigned long long)be64_to_cpu(o2net_hand
->connector_id
));
1454 o2net_initialize_handshake();
1455 o2net_sendpage(sc
, o2net_hand
, sizeof(*o2net_hand
));
1459 /* this is called as a work_struct func. */
1460 static void o2net_sc_send_keep_req(struct work_struct
*work
)
1462 struct o2net_sock_container
*sc
=
1463 container_of(work
, struct o2net_sock_container
,
1464 sc_keepalive_work
.work
);
1466 o2net_sendpage(sc
, o2net_keep_req
, sizeof(*o2net_keep_req
));
1470 /* socket shutdown does a del_timer_sync against this as it tears down.
1471 * we can't start this timer until we've got to the point in sc buildup
1472 * where shutdown is going to be involved */
1473 static void o2net_idle_timer(unsigned long data
)
1475 struct o2net_sock_container
*sc
= (struct o2net_sock_container
*)data
;
1476 struct o2net_node
*nn
= o2net_nn_from_num(sc
->sc_node
->nd_num
);
1479 do_gettimeofday(&now
);
1481 printk(KERN_NOTICE
"o2net: connection to " SC_NODEF_FMT
" has been idle for %u.%u "
1482 "seconds, shutting it down.\n", SC_NODEF_ARGS(sc
),
1483 o2net_idle_timeout() / 1000,
1484 o2net_idle_timeout() % 1000);
1485 mlog(ML_NOTICE
, "here are some times that might help debug the "
1486 "situation: (tmr %ld.%ld now %ld.%ld dr %ld.%ld adv "
1487 "%ld.%ld:%ld.%ld func (%08x:%u) %ld.%ld:%ld.%ld)\n",
1488 sc
->sc_tv_timer
.tv_sec
, (long) sc
->sc_tv_timer
.tv_usec
,
1489 now
.tv_sec
, (long) now
.tv_usec
,
1490 sc
->sc_tv_data_ready
.tv_sec
, (long) sc
->sc_tv_data_ready
.tv_usec
,
1491 sc
->sc_tv_advance_start
.tv_sec
,
1492 (long) sc
->sc_tv_advance_start
.tv_usec
,
1493 sc
->sc_tv_advance_stop
.tv_sec
,
1494 (long) sc
->sc_tv_advance_stop
.tv_usec
,
1495 sc
->sc_msg_key
, sc
->sc_msg_type
,
1496 sc
->sc_tv_func_start
.tv_sec
, (long) sc
->sc_tv_func_start
.tv_usec
,
1497 sc
->sc_tv_func_stop
.tv_sec
, (long) sc
->sc_tv_func_stop
.tv_usec
);
1500 * Initialize the nn_timeout so that the next connection attempt
1501 * will continue in o2net_start_connect.
1503 atomic_set(&nn
->nn_timeout
, 1);
1505 o2net_sc_queue_work(sc
, &sc
->sc_shutdown_work
);
1508 static void o2net_sc_reset_idle_timer(struct o2net_sock_container
*sc
)
1510 o2net_sc_cancel_delayed_work(sc
, &sc
->sc_keepalive_work
);
1511 o2net_sc_queue_delayed_work(sc
, &sc
->sc_keepalive_work
,
1512 msecs_to_jiffies(o2net_keepalive_delay()));
1513 do_gettimeofday(&sc
->sc_tv_timer
);
1514 mod_timer(&sc
->sc_idle_timeout
,
1515 jiffies
+ msecs_to_jiffies(o2net_idle_timeout()));
1518 static void o2net_sc_postpone_idle(struct o2net_sock_container
*sc
)
1520 /* Only push out an existing timer */
1521 if (timer_pending(&sc
->sc_idle_timeout
))
1522 o2net_sc_reset_idle_timer(sc
);
1525 /* this work func is kicked whenever a path sets the nn state which doesn't
1526 * have valid set. This includes seeing hb come up, losing a connection,
1527 * having a connect attempt fail, etc. This centralizes the logic which decides
1528 * if a connect attempt should be made or if we should give up and all future
1529 * transmit attempts should fail */
1530 static void o2net_start_connect(struct work_struct
*work
)
1532 struct o2net_node
*nn
=
1533 container_of(work
, struct o2net_node
, nn_connect_work
.work
);
1534 struct o2net_sock_container
*sc
= NULL
;
1535 struct o2nm_node
*node
= NULL
, *mynode
= NULL
;
1536 struct socket
*sock
= NULL
;
1537 struct sockaddr_in myaddr
= {0, }, remoteaddr
= {0, };
1539 unsigned int timeout
;
1541 /* if we're greater we initiate tx, otherwise we accept */
1542 if (o2nm_this_node() <= o2net_num_from_nn(nn
))
1545 /* watch for racing with tearing a node down */
1546 node
= o2nm_get_node_by_num(o2net_num_from_nn(nn
));
1552 mynode
= o2nm_get_node_by_num(o2nm_this_node());
1553 if (mynode
== NULL
) {
1558 spin_lock(&nn
->nn_lock
);
1560 * see if we already have one pending or have given up.
1561 * For nn_timeout, it is set when we close the connection
1562 * because of the idle time out. So it means that we have
1563 * at least connected to that node successfully once,
1564 * now try to connect to it again.
1566 timeout
= atomic_read(&nn
->nn_timeout
);
1567 stop
= (nn
->nn_sc
||
1568 (nn
->nn_persistent_error
&&
1569 (nn
->nn_persistent_error
!= -ENOTCONN
|| timeout
== 0)));
1570 spin_unlock(&nn
->nn_lock
);
1574 nn
->nn_last_connect_attempt
= jiffies
;
1576 sc
= sc_alloc(node
);
1578 mlog(0, "couldn't allocate sc\n");
1583 ret
= sock_create(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1585 mlog(0, "can't create socket: %d\n", ret
);
1588 sc
->sc_sock
= sock
; /* freed by sc_kref_release */
1590 sock
->sk
->sk_allocation
= GFP_ATOMIC
;
1592 myaddr
.sin_family
= AF_INET
;
1593 myaddr
.sin_addr
.s_addr
= mynode
->nd_ipv4_address
;
1594 myaddr
.sin_port
= htons(0); /* any port */
1596 ret
= sock
->ops
->bind(sock
, (struct sockaddr
*)&myaddr
,
1599 mlog(ML_ERROR
, "bind failed with %d at address %pI4\n",
1600 ret
, &mynode
->nd_ipv4_address
);
1604 ret
= o2net_set_nodelay(sc
->sc_sock
);
1606 mlog(ML_ERROR
, "setting TCP_NODELAY failed with %d\n", ret
);
1610 o2net_register_callbacks(sc
->sc_sock
->sk
, sc
);
1612 spin_lock(&nn
->nn_lock
);
1613 /* handshake completion will set nn->nn_sc_valid */
1614 o2net_set_nn_state(nn
, sc
, 0, 0);
1615 spin_unlock(&nn
->nn_lock
);
1617 remoteaddr
.sin_family
= AF_INET
;
1618 remoteaddr
.sin_addr
.s_addr
= node
->nd_ipv4_address
;
1619 remoteaddr
.sin_port
= node
->nd_ipv4_port
;
1621 ret
= sc
->sc_sock
->ops
->connect(sc
->sc_sock
,
1622 (struct sockaddr
*)&remoteaddr
,
1625 if (ret
== -EINPROGRESS
)
1630 mlog(ML_NOTICE
, "connect attempt to " SC_NODEF_FMT
" failed "
1631 "with errno %d\n", SC_NODEF_ARGS(sc
), ret
);
1632 /* 0 err so that another will be queued and attempted
1633 * from set_nn_state */
1635 o2net_ensure_shutdown(nn
, sc
, 0);
1640 o2nm_node_put(node
);
1642 o2nm_node_put(mynode
);
1647 static void o2net_connect_expired(struct work_struct
*work
)
1649 struct o2net_node
*nn
=
1650 container_of(work
, struct o2net_node
, nn_connect_expired
.work
);
1652 spin_lock(&nn
->nn_lock
);
1653 if (!nn
->nn_sc_valid
) {
1654 mlog(ML_ERROR
, "no connection established with node %u after "
1655 "%u.%u seconds, giving up and returning errors.\n",
1656 o2net_num_from_nn(nn
),
1657 o2net_idle_timeout() / 1000,
1658 o2net_idle_timeout() % 1000);
1660 o2net_set_nn_state(nn
, NULL
, 0, -ENOTCONN
);
1662 spin_unlock(&nn
->nn_lock
);
1665 static void o2net_still_up(struct work_struct
*work
)
1667 struct o2net_node
*nn
=
1668 container_of(work
, struct o2net_node
, nn_still_up
.work
);
1670 o2quo_hb_still_up(o2net_num_from_nn(nn
));
1673 /* ------------------------------------------------------------ */
1675 void o2net_disconnect_node(struct o2nm_node
*node
)
1677 struct o2net_node
*nn
= o2net_nn_from_num(node
->nd_num
);
1679 /* don't reconnect until it's heartbeating again */
1680 spin_lock(&nn
->nn_lock
);
1681 atomic_set(&nn
->nn_timeout
, 0);
1682 o2net_set_nn_state(nn
, NULL
, 0, -ENOTCONN
);
1683 spin_unlock(&nn
->nn_lock
);
1686 cancel_delayed_work(&nn
->nn_connect_expired
);
1687 cancel_delayed_work(&nn
->nn_connect_work
);
1688 cancel_delayed_work(&nn
->nn_still_up
);
1689 flush_workqueue(o2net_wq
);
1693 static void o2net_hb_node_down_cb(struct o2nm_node
*node
, int node_num
,
1696 o2quo_hb_down(node_num
);
1698 if (node_num
!= o2nm_this_node())
1699 o2net_disconnect_node(node
);
1701 BUG_ON(atomic_read(&o2net_connected_peers
) < 0);
1704 static void o2net_hb_node_up_cb(struct o2nm_node
*node
, int node_num
,
1707 struct o2net_node
*nn
= o2net_nn_from_num(node_num
);
1709 o2quo_hb_up(node_num
);
1711 /* ensure an immediate connect attempt */
1712 nn
->nn_last_connect_attempt
= jiffies
-
1713 (msecs_to_jiffies(o2net_reconnect_delay()) + 1);
1715 if (node_num
!= o2nm_this_node()) {
1716 /* believe it or not, accept and node hearbeating testing
1717 * can succeed for this node before we got here.. so
1718 * only use set_nn_state to clear the persistent error
1719 * if that hasn't already happened */
1720 spin_lock(&nn
->nn_lock
);
1721 atomic_set(&nn
->nn_timeout
, 0);
1722 if (nn
->nn_persistent_error
)
1723 o2net_set_nn_state(nn
, NULL
, 0, 0);
1724 spin_unlock(&nn
->nn_lock
);
1728 void o2net_unregister_hb_callbacks(void)
1730 o2hb_unregister_callback(NULL
, &o2net_hb_up
);
1731 o2hb_unregister_callback(NULL
, &o2net_hb_down
);
1734 int o2net_register_hb_callbacks(void)
1738 o2hb_setup_callback(&o2net_hb_down
, O2HB_NODE_DOWN_CB
,
1739 o2net_hb_node_down_cb
, NULL
, O2NET_HB_PRI
);
1740 o2hb_setup_callback(&o2net_hb_up
, O2HB_NODE_UP_CB
,
1741 o2net_hb_node_up_cb
, NULL
, O2NET_HB_PRI
);
1743 ret
= o2hb_register_callback(NULL
, &o2net_hb_up
);
1745 ret
= o2hb_register_callback(NULL
, &o2net_hb_down
);
1748 o2net_unregister_hb_callbacks();
1753 /* ------------------------------------------------------------ */
1755 static int o2net_accept_one(struct socket
*sock
)
1758 struct sockaddr_in sin
;
1759 struct socket
*new_sock
= NULL
;
1760 struct o2nm_node
*node
= NULL
;
1761 struct o2nm_node
*local_node
= NULL
;
1762 struct o2net_sock_container
*sc
= NULL
;
1763 struct o2net_node
*nn
;
1765 BUG_ON(sock
== NULL
);
1766 ret
= sock_create_lite(sock
->sk
->sk_family
, sock
->sk
->sk_type
,
1767 sock
->sk
->sk_protocol
, &new_sock
);
1771 new_sock
->type
= sock
->type
;
1772 new_sock
->ops
= sock
->ops
;
1773 ret
= sock
->ops
->accept(sock
, new_sock
, O_NONBLOCK
);
1777 new_sock
->sk
->sk_allocation
= GFP_ATOMIC
;
1779 ret
= o2net_set_nodelay(new_sock
);
1781 mlog(ML_ERROR
, "setting TCP_NODELAY failed with %d\n", ret
);
1786 ret
= new_sock
->ops
->getname(new_sock
, (struct sockaddr
*) &sin
,
1791 node
= o2nm_get_node_by_ip(sin
.sin_addr
.s_addr
);
1793 mlog(ML_NOTICE
, "attempt to connect from unknown node at %pI4:%d\n",
1794 &sin
.sin_addr
.s_addr
, ntohs(sin
.sin_port
));
1799 if (o2nm_this_node() >= node
->nd_num
) {
1800 local_node
= o2nm_get_node_by_num(o2nm_this_node());
1801 mlog(ML_NOTICE
, "unexpected connect attempt seen at node '%s' ("
1802 "%u, %pI4:%d) from node '%s' (%u, %pI4:%d)\n",
1803 local_node
->nd_name
, local_node
->nd_num
,
1804 &(local_node
->nd_ipv4_address
),
1805 ntohs(local_node
->nd_ipv4_port
),
1806 node
->nd_name
, node
->nd_num
, &sin
.sin_addr
.s_addr
,
1807 ntohs(sin
.sin_port
));
1812 /* this happens all the time when the other node sees our heartbeat
1813 * and tries to connect before we see their heartbeat */
1814 if (!o2hb_check_node_heartbeating_from_callback(node
->nd_num
)) {
1815 mlog(ML_CONN
, "attempt to connect from node '%s' at "
1816 "%pI4:%d but it isn't heartbeating\n",
1817 node
->nd_name
, &sin
.sin_addr
.s_addr
,
1818 ntohs(sin
.sin_port
));
1823 nn
= o2net_nn_from_num(node
->nd_num
);
1825 spin_lock(&nn
->nn_lock
);
1830 spin_unlock(&nn
->nn_lock
);
1832 mlog(ML_NOTICE
, "attempt to connect from node '%s' at "
1833 "%pI4:%d but it already has an open connection\n",
1834 node
->nd_name
, &sin
.sin_addr
.s_addr
,
1835 ntohs(sin
.sin_port
));
1839 sc
= sc_alloc(node
);
1845 sc
->sc_sock
= new_sock
;
1848 spin_lock(&nn
->nn_lock
);
1849 atomic_set(&nn
->nn_timeout
, 0);
1850 o2net_set_nn_state(nn
, sc
, 0, 0);
1851 spin_unlock(&nn
->nn_lock
);
1853 o2net_register_callbacks(sc
->sc_sock
->sk
, sc
);
1854 o2net_sc_queue_work(sc
, &sc
->sc_rx_work
);
1856 o2net_initialize_handshake();
1857 o2net_sendpage(sc
, o2net_hand
, sizeof(*o2net_hand
));
1861 sock_release(new_sock
);
1863 o2nm_node_put(node
);
1865 o2nm_node_put(local_node
);
1871 static void o2net_accept_many(struct work_struct
*work
)
1873 struct socket
*sock
= o2net_listen_sock
;
1874 while (o2net_accept_one(sock
) == 0)
1878 static void o2net_listen_data_ready(struct sock
*sk
, int bytes
)
1880 void (*ready
)(struct sock
*sk
, int bytes
);
1882 read_lock(&sk
->sk_callback_lock
);
1883 ready
= sk
->sk_user_data
;
1884 if (ready
== NULL
) { /* check for teardown race */
1885 ready
= sk
->sk_data_ready
;
1889 /* ->sk_data_ready is also called for a newly established child socket
1890 * before it has been accepted and the acceptor has set up their
1891 * data_ready.. we only want to queue listen work for our listening
1893 if (sk
->sk_state
== TCP_LISTEN
) {
1894 mlog(ML_TCP
, "bytes: %d\n", bytes
);
1895 queue_work(o2net_wq
, &o2net_listen_work
);
1899 read_unlock(&sk
->sk_callback_lock
);
1903 static int o2net_open_listening_sock(__be32 addr
, __be16 port
)
1905 struct socket
*sock
= NULL
;
1907 struct sockaddr_in sin
= {
1908 .sin_family
= PF_INET
,
1909 .sin_addr
= { .s_addr
= addr
},
1913 ret
= sock_create(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1915 mlog(ML_ERROR
, "unable to create socket, ret=%d\n", ret
);
1919 sock
->sk
->sk_allocation
= GFP_ATOMIC
;
1921 write_lock_bh(&sock
->sk
->sk_callback_lock
);
1922 sock
->sk
->sk_user_data
= sock
->sk
->sk_data_ready
;
1923 sock
->sk
->sk_data_ready
= o2net_listen_data_ready
;
1924 write_unlock_bh(&sock
->sk
->sk_callback_lock
);
1926 o2net_listen_sock
= sock
;
1927 INIT_WORK(&o2net_listen_work
, o2net_accept_many
);
1929 sock
->sk
->sk_reuse
= 1;
1930 ret
= sock
->ops
->bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
1932 mlog(ML_ERROR
, "unable to bind socket at %pI4:%u, "
1933 "ret=%d\n", &addr
, ntohs(port
), ret
);
1937 ret
= sock
->ops
->listen(sock
, 64);
1939 mlog(ML_ERROR
, "unable to listen on %pI4:%u, ret=%d\n",
1940 &addr
, ntohs(port
), ret
);
1945 o2net_listen_sock
= NULL
;
1953 * called from node manager when we should bring up our network listening
1954 * socket. node manager handles all the serialization to only call this
1955 * once and to match it with o2net_stop_listening(). note,
1956 * o2nm_this_node() doesn't work yet as we're being called while it
1959 int o2net_start_listening(struct o2nm_node
*node
)
1963 BUG_ON(o2net_wq
!= NULL
);
1964 BUG_ON(o2net_listen_sock
!= NULL
);
1966 mlog(ML_KTHREAD
, "starting o2net thread...\n");
1967 o2net_wq
= create_singlethread_workqueue("o2net");
1968 if (o2net_wq
== NULL
) {
1969 mlog(ML_ERROR
, "unable to launch o2net thread\n");
1970 return -ENOMEM
; /* ? */
1973 ret
= o2net_open_listening_sock(node
->nd_ipv4_address
,
1974 node
->nd_ipv4_port
);
1976 destroy_workqueue(o2net_wq
);
1979 o2quo_conn_up(node
->nd_num
);
1984 /* again, o2nm_this_node() doesn't work here as we're involved in
1985 * tearing it down */
1986 void o2net_stop_listening(struct o2nm_node
*node
)
1988 struct socket
*sock
= o2net_listen_sock
;
1991 BUG_ON(o2net_wq
== NULL
);
1992 BUG_ON(o2net_listen_sock
== NULL
);
1994 /* stop the listening socket from generating work */
1995 write_lock_bh(&sock
->sk
->sk_callback_lock
);
1996 sock
->sk
->sk_data_ready
= sock
->sk
->sk_user_data
;
1997 sock
->sk
->sk_user_data
= NULL
;
1998 write_unlock_bh(&sock
->sk
->sk_callback_lock
);
2000 for (i
= 0; i
< ARRAY_SIZE(o2net_nodes
); i
++) {
2001 struct o2nm_node
*node
= o2nm_get_node_by_num(i
);
2003 o2net_disconnect_node(node
);
2004 o2nm_node_put(node
);
2008 /* finish all work and tear down the work queue */
2009 mlog(ML_KTHREAD
, "waiting for o2net thread to exit....\n");
2010 destroy_workqueue(o2net_wq
);
2013 sock_release(o2net_listen_sock
);
2014 o2net_listen_sock
= NULL
;
2016 o2quo_conn_err(node
->nd_num
);
2019 /* ------------------------------------------------------------ */
2021 int o2net_init(void)
2027 if (o2net_debugfs_init())
2030 o2net_hand
= kzalloc(sizeof(struct o2net_handshake
), GFP_KERNEL
);
2031 o2net_keep_req
= kzalloc(sizeof(struct o2net_msg
), GFP_KERNEL
);
2032 o2net_keep_resp
= kzalloc(sizeof(struct o2net_msg
), GFP_KERNEL
);
2033 if (!o2net_hand
|| !o2net_keep_req
|| !o2net_keep_resp
) {
2035 kfree(o2net_keep_req
);
2036 kfree(o2net_keep_resp
);
2040 o2net_hand
->protocol_version
= cpu_to_be64(O2NET_PROTOCOL_VERSION
);
2041 o2net_hand
->connector_id
= cpu_to_be64(1);
2043 o2net_keep_req
->magic
= cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC
);
2044 o2net_keep_resp
->magic
= cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC
);
2046 for (i
= 0; i
< ARRAY_SIZE(o2net_nodes
); i
++) {
2047 struct o2net_node
*nn
= o2net_nn_from_num(i
);
2049 atomic_set(&nn
->nn_timeout
, 0);
2050 spin_lock_init(&nn
->nn_lock
);
2051 INIT_DELAYED_WORK(&nn
->nn_connect_work
, o2net_start_connect
);
2052 INIT_DELAYED_WORK(&nn
->nn_connect_expired
,
2053 o2net_connect_expired
);
2054 INIT_DELAYED_WORK(&nn
->nn_still_up
, o2net_still_up
);
2055 /* until we see hb from a node we'll return einval */
2056 nn
->nn_persistent_error
= -ENOTCONN
;
2057 init_waitqueue_head(&nn
->nn_sc_wq
);
2058 idr_init(&nn
->nn_status_idr
);
2059 INIT_LIST_HEAD(&nn
->nn_status_list
);
2065 void o2net_exit(void)
2069 kfree(o2net_keep_req
);
2070 kfree(o2net_keep_resp
);
2071 o2net_debugfs_exit();