]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ocfs2/cluster/tcp.c
WorkStruct: make allyesconfig
[net-next-2.6.git] / fs / ocfs2 / cluster / tcp.c
CommitLineData
98211489
ZB
1/* -*- mode: c; c-basic-offset: 8; -*-
2 *
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * Copyright (C) 2004 Oracle. All rights reserved.
6 *
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.
11 *
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.
16 *
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.
21 *
22 * ----
23 *
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.
27 *
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
32 * message they send.
33 *
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
39 * doing so.
40 *
41 * Any framing errors (bad magic, large payload lengths) close a connection.
42 *
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.
49 *
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.
54 */
55
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 <net/tcp.h>
62
63#include <asm/uaccess.h>
64
65#include "heartbeat.h"
66#include "tcp.h"
67#include "nodemanager.h"
68#define MLOG_MASK_PREFIX ML_TCP
69#include "masklog.h"
70#include "quorum.h"
71
72#include "tcp_internal.h"
73
74/*
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.
80 */
81
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)
86
87/*
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.
91 */
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); \
100} while (0)
101
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 , \
108 ##args); \
109} while (0)
110
34af946a 111static DEFINE_RWLOCK(o2net_handler_lock);
98211489
ZB
112static struct rb_root o2net_handler_tree = RB_ROOT;
113
114static struct o2net_node o2net_nodes[O2NM_MAX_NODES];
115
116/* XXX someday we'll need better accounting */
117static struct socket *o2net_listen_sock = NULL;
118
119/*
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.
126 */
127static struct workqueue_struct *o2net_wq;
128static struct work_struct o2net_listen_work;
129
130static struct o2hb_callback_func o2net_hb_up, o2net_hb_down;
131#define O2NET_HB_PRI 0x1
132
133static struct o2net_handshake *o2net_hand;
134static struct o2net_msg *o2net_keep_req, *o2net_keep_resp;
135
136static 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,};
141
142/* can't quite avoid *all* internal declarations :/ */
c4028958
DH
143static void o2net_sc_connect_completed(struct work_struct *work);
144static void o2net_rx_until_empty(struct work_struct *work);
145static void o2net_shutdown_sc(struct work_struct *work);
98211489 146static void o2net_listen_data_ready(struct sock *sk, int bytes);
c4028958 147static void o2net_sc_send_keep_req(struct work_struct *work);
98211489
ZB
148static void o2net_idle_timer(unsigned long data);
149static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
150
151static inline int o2net_sys_err_to_errno(enum o2net_system_error err)
152{
153 int trans;
154 BUG_ON(err >= O2NET_ERR_MAX);
155 trans = o2net_sys_err_translations[err];
156
157 /* Just in case we mess up the translation table above */
158 BUG_ON(err != O2NET_ERR_NONE && trans == 0);
159 return trans;
160}
161
162static struct o2net_node * o2net_nn_from_num(u8 node_num)
163{
164 BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes));
165 return &o2net_nodes[node_num];
166}
167
168static u8 o2net_num_from_nn(struct o2net_node *nn)
169{
170 BUG_ON(nn == NULL);
171 return nn - o2net_nodes;
172}
173
174/* ------------------------------------------------------------ */
175
176static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw)
177{
178 int ret = 0;
179
180 do {
181 if (!idr_pre_get(&nn->nn_status_idr, GFP_ATOMIC)) {
182 ret = -EAGAIN;
183 break;
184 }
185 spin_lock(&nn->nn_lock);
186 ret = idr_get_new(&nn->nn_status_idr, nsw, &nsw->ns_id);
187 if (ret == 0)
188 list_add_tail(&nsw->ns_node_item,
189 &nn->nn_status_list);
190 spin_unlock(&nn->nn_lock);
191 } while (ret == -EAGAIN);
192
193 if (ret == 0) {
194 init_waitqueue_head(&nsw->ns_wq);
195 nsw->ns_sys_status = O2NET_ERR_NONE;
196 nsw->ns_status = 0;
197 }
198
199 return ret;
200}
201
202static void o2net_complete_nsw_locked(struct o2net_node *nn,
203 struct o2net_status_wait *nsw,
204 enum o2net_system_error sys_status,
205 s32 status)
206{
207 assert_spin_locked(&nn->nn_lock);
208
209 if (!list_empty(&nsw->ns_node_item)) {
210 list_del_init(&nsw->ns_node_item);
211 nsw->ns_sys_status = sys_status;
212 nsw->ns_status = status;
213 idr_remove(&nn->nn_status_idr, nsw->ns_id);
214 wake_up(&nsw->ns_wq);
215 }
216}
217
218static void o2net_complete_nsw(struct o2net_node *nn,
219 struct o2net_status_wait *nsw,
220 u64 id, enum o2net_system_error sys_status,
221 s32 status)
222{
223 spin_lock(&nn->nn_lock);
224 if (nsw == NULL) {
225 if (id > INT_MAX)
226 goto out;
227
228 nsw = idr_find(&nn->nn_status_idr, id);
229 if (nsw == NULL)
230 goto out;
231 }
232
233 o2net_complete_nsw_locked(nn, nsw, sys_status, status);
234
235out:
236 spin_unlock(&nn->nn_lock);
237 return;
238}
239
240static void o2net_complete_nodes_nsw(struct o2net_node *nn)
241{
242 struct list_head *iter, *tmp;
243 unsigned int num_kills = 0;
244 struct o2net_status_wait *nsw;
245
246 assert_spin_locked(&nn->nn_lock);
247
248 list_for_each_safe(iter, tmp, &nn->nn_status_list) {
249 nsw = list_entry(iter, struct o2net_status_wait, ns_node_item);
250 o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
251 num_kills++;
252 }
253
254 mlog(0, "completed %d messages for node %u\n", num_kills,
255 o2net_num_from_nn(nn));
256}
257
258static int o2net_nsw_completed(struct o2net_node *nn,
259 struct o2net_status_wait *nsw)
260{
261 int completed;
262 spin_lock(&nn->nn_lock);
263 completed = list_empty(&nsw->ns_node_item);
264 spin_unlock(&nn->nn_lock);
265 return completed;
266}
267
268/* ------------------------------------------------------------ */
269
270static void sc_kref_release(struct kref *kref)
271{
272 struct o2net_sock_container *sc = container_of(kref,
273 struct o2net_sock_container, sc_kref);
274 sclog(sc, "releasing\n");
275
276 if (sc->sc_sock) {
277 sock_release(sc->sc_sock);
278 sc->sc_sock = NULL;
279 }
280
281 o2nm_node_put(sc->sc_node);
282 sc->sc_node = NULL;
283
284 kfree(sc);
285}
286
287static void sc_put(struct o2net_sock_container *sc)
288{
289 sclog(sc, "put\n");
290 kref_put(&sc->sc_kref, sc_kref_release);
291}
292static void sc_get(struct o2net_sock_container *sc)
293{
294 sclog(sc, "get\n");
295 kref_get(&sc->sc_kref);
296}
297static struct o2net_sock_container *sc_alloc(struct o2nm_node *node)
298{
299 struct o2net_sock_container *sc, *ret = NULL;
300 struct page *page = NULL;
301
302 page = alloc_page(GFP_NOFS);
303 sc = kcalloc(1, sizeof(*sc), GFP_NOFS);
304 if (sc == NULL || page == NULL)
305 goto out;
306
307 kref_init(&sc->sc_kref);
308 o2nm_node_get(node);
309 sc->sc_node = node;
310
c4028958
DH
311 INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed);
312 INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty);
313 INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc);
314 INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req);
98211489
ZB
315
316 init_timer(&sc->sc_idle_timeout);
317 sc->sc_idle_timeout.function = o2net_idle_timer;
318 sc->sc_idle_timeout.data = (unsigned long)sc;
319
320 sclog(sc, "alloced\n");
321
322 ret = sc;
323 sc->sc_page = page;
324 sc = NULL;
325 page = NULL;
326
327out:
328 if (page)
329 __free_page(page);
330 kfree(sc);
331
332 return ret;
333}
334
335/* ------------------------------------------------------------ */
336
337static void o2net_sc_queue_work(struct o2net_sock_container *sc,
338 struct work_struct *work)
339{
340 sc_get(sc);
341 if (!queue_work(o2net_wq, work))
342 sc_put(sc);
343}
344static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc,
c4028958 345 struct delayed_work *work,
98211489
ZB
346 int delay)
347{
348 sc_get(sc);
349 if (!queue_delayed_work(o2net_wq, work, delay))
350 sc_put(sc);
351}
352static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc,
c4028958 353 struct delayed_work *work)
98211489
ZB
354{
355 if (cancel_delayed_work(work))
356 sc_put(sc);
357}
358
359static void o2net_set_nn_state(struct o2net_node *nn,
360 struct o2net_sock_container *sc,
361 unsigned valid, int err)
362{
363 int was_valid = nn->nn_sc_valid;
364 int was_err = nn->nn_persistent_error;
365 struct o2net_sock_container *old_sc = nn->nn_sc;
366
367 assert_spin_locked(&nn->nn_lock);
368
369 /* the node num comparison and single connect/accept path should stop
370 * an non-null sc from being overwritten with another */
371 BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
372 mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
373 mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);
374
375 /* we won't reconnect after our valid conn goes away for
376 * this hb iteration.. here so it shows up in the logs */
377 if (was_valid && !valid && err == 0)
378 err = -ENOTCONN;
379
380 mlog(ML_CONN, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
381 o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
382 nn->nn_persistent_error, err);
383
384 nn->nn_sc = sc;
385 nn->nn_sc_valid = valid ? 1 : 0;
386 nn->nn_persistent_error = err;
387
388 /* mirrors o2net_tx_can_proceed() */
389 if (nn->nn_persistent_error || nn->nn_sc_valid)
390 wake_up(&nn->nn_sc_wq);
391
392 if (!was_err && nn->nn_persistent_error) {
393 o2quo_conn_err(o2net_num_from_nn(nn));
394 queue_delayed_work(o2net_wq, &nn->nn_still_up,
395 msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
396 }
397
398 if (was_valid && !valid) {
781ee3e2
SM
399 printk(KERN_INFO "o2net: no longer connected to "
400 SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
98211489
ZB
401 o2net_complete_nodes_nsw(nn);
402 }
403
404 if (!was_valid && valid) {
405 o2quo_conn_up(o2net_num_from_nn(nn));
406 /* this is a bit of a hack. we only try reconnecting
407 * when heartbeating starts until we get a connection.
408 * if that connection then dies we don't try reconnecting.
409 * the only way to start connecting again is to down
410 * heartbeat and bring it back up. */
411 cancel_delayed_work(&nn->nn_connect_expired);
781ee3e2
SM
412 printk(KERN_INFO "o2net: %s " SC_NODEF_FMT "\n",
413 o2nm_this_node() > sc->sc_node->nd_num ?
414 "connected to" : "accepted connection from",
415 SC_NODEF_ARGS(sc));
98211489
ZB
416 }
417
418 /* trigger the connecting worker func as long as we're not valid,
419 * it will back off if it shouldn't connect. This can be called
420 * from node config teardown and so needs to be careful about
421 * the work queue actually being up. */
422 if (!valid && o2net_wq) {
423 unsigned long delay;
424 /* delay if we're withing a RECONNECT_DELAY of the
425 * last attempt */
426 delay = (nn->nn_last_connect_attempt +
427 msecs_to_jiffies(O2NET_RECONNECT_DELAY_MS))
428 - jiffies;
429 if (delay > msecs_to_jiffies(O2NET_RECONNECT_DELAY_MS))
430 delay = 0;
431 mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay);
432 queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);
433 }
434
435 /* keep track of the nn's sc ref for the caller */
436 if ((old_sc == NULL) && sc)
437 sc_get(sc);
438 if (old_sc && (old_sc != sc)) {
439 o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
440 sc_put(old_sc);
441 }
442}
443
444/* see o2net_register_callbacks() */
445static void o2net_data_ready(struct sock *sk, int bytes)
446{
447 void (*ready)(struct sock *sk, int bytes);
448
449 read_lock(&sk->sk_callback_lock);
450 if (sk->sk_user_data) {
451 struct o2net_sock_container *sc = sk->sk_user_data;
452 sclog(sc, "data_ready hit\n");
453 do_gettimeofday(&sc->sc_tv_data_ready);
454 o2net_sc_queue_work(sc, &sc->sc_rx_work);
455 ready = sc->sc_data_ready;
456 } else {
457 ready = sk->sk_data_ready;
458 }
459 read_unlock(&sk->sk_callback_lock);
460
461 ready(sk, bytes);
462}
463
464/* see o2net_register_callbacks() */
465static void o2net_state_change(struct sock *sk)
466{
467 void (*state_change)(struct sock *sk);
468 struct o2net_sock_container *sc;
469
470 read_lock(&sk->sk_callback_lock);
471 sc = sk->sk_user_data;
472 if (sc == NULL) {
473 state_change = sk->sk_state_change;
474 goto out;
475 }
476
477 sclog(sc, "state_change to %d\n", sk->sk_state);
478
479 state_change = sc->sc_state_change;
480
481 switch(sk->sk_state) {
482 /* ignore connecting sockets as they make progress */
483 case TCP_SYN_SENT:
484 case TCP_SYN_RECV:
485 break;
486 case TCP_ESTABLISHED:
487 o2net_sc_queue_work(sc, &sc->sc_connect_work);
488 break;
489 default:
490 o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
491 break;
492 }
493out:
494 read_unlock(&sk->sk_callback_lock);
495 state_change(sk);
496}
497
498/*
499 * we register callbacks so we can queue work on events before calling
500 * the original callbacks. our callbacks our careful to test user_data
501 * to discover when they've reaced with o2net_unregister_callbacks().
502 */
503static void o2net_register_callbacks(struct sock *sk,
504 struct o2net_sock_container *sc)
505{
506 write_lock_bh(&sk->sk_callback_lock);
507
508 /* accepted sockets inherit the old listen socket data ready */
509 if (sk->sk_data_ready == o2net_listen_data_ready) {
510 sk->sk_data_ready = sk->sk_user_data;
511 sk->sk_user_data = NULL;
512 }
513
514 BUG_ON(sk->sk_user_data != NULL);
515 sk->sk_user_data = sc;
516 sc_get(sc);
517
518 sc->sc_data_ready = sk->sk_data_ready;
519 sc->sc_state_change = sk->sk_state_change;
520 sk->sk_data_ready = o2net_data_ready;
521 sk->sk_state_change = o2net_state_change;
522
523 write_unlock_bh(&sk->sk_callback_lock);
524}
525
526static int o2net_unregister_callbacks(struct sock *sk,
527 struct o2net_sock_container *sc)
528{
529 int ret = 0;
530
531 write_lock_bh(&sk->sk_callback_lock);
532 if (sk->sk_user_data == sc) {
533 ret = 1;
534 sk->sk_user_data = NULL;
535 sk->sk_data_ready = sc->sc_data_ready;
536 sk->sk_state_change = sc->sc_state_change;
537 }
538 write_unlock_bh(&sk->sk_callback_lock);
539
540 return ret;
541}
542
543/*
544 * this is a little helper that is called by callers who have seen a problem
545 * with an sc and want to detach it from the nn if someone already hasn't beat
546 * them to it. if an error is given then the shutdown will be persistent
547 * and pending transmits will be canceled.
548 */
549static void o2net_ensure_shutdown(struct o2net_node *nn,
550 struct o2net_sock_container *sc,
551 int err)
552{
553 spin_lock(&nn->nn_lock);
554 if (nn->nn_sc == sc)
555 o2net_set_nn_state(nn, NULL, 0, err);
556 spin_unlock(&nn->nn_lock);
557}
558
559/*
560 * This work queue function performs the blocking parts of socket shutdown. A
561 * few paths lead here. set_nn_state will trigger this callback if it sees an
562 * sc detached from the nn. state_change will also trigger this callback
563 * directly when it sees errors. In that case we need to call set_nn_state
564 * ourselves as state_change couldn't get the nn_lock and call set_nn_state
565 * itself.
566 */
c4028958 567static void o2net_shutdown_sc(struct work_struct *work)
98211489 568{
c4028958
DH
569 struct o2net_sock_container *sc =
570 container_of(work, struct o2net_sock_container,
571 sc_shutdown_work);
98211489
ZB
572 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
573
574 sclog(sc, "shutting down\n");
575
576 /* drop the callbacks ref and call shutdown only once */
577 if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
578 /* we shouldn't flush as we're in the thread, the
579 * races with pending sc work structs are harmless */
580 del_timer_sync(&sc->sc_idle_timeout);
581 o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
582 sc_put(sc);
583 sc->sc_sock->ops->shutdown(sc->sc_sock,
584 RCV_SHUTDOWN|SEND_SHUTDOWN);
585 }
586
587 /* not fatal so failed connects before the other guy has our
588 * heartbeat can be retried */
589 o2net_ensure_shutdown(nn, sc, 0);
590 sc_put(sc);
591}
592
593/* ------------------------------------------------------------ */
594
595static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
596 u32 key)
597{
598 int ret = memcmp(&nmh->nh_key, &key, sizeof(key));
599
600 if (ret == 0)
601 ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));
602
603 return ret;
604}
605
606static struct o2net_msg_handler *
607o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
608 struct rb_node **ret_parent)
609{
610 struct rb_node **p = &o2net_handler_tree.rb_node;
611 struct rb_node *parent = NULL;
612 struct o2net_msg_handler *nmh, *ret = NULL;
613 int cmp;
614
615 while (*p) {
616 parent = *p;
617 nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
618 cmp = o2net_handler_cmp(nmh, msg_type, key);
619
620 if (cmp < 0)
621 p = &(*p)->rb_left;
622 else if (cmp > 0)
623 p = &(*p)->rb_right;
624 else {
625 ret = nmh;
626 break;
627 }
628 }
629
630 if (ret_p != NULL)
631 *ret_p = p;
632 if (ret_parent != NULL)
633 *ret_parent = parent;
634
635 return ret;
636}
637
638static void o2net_handler_kref_release(struct kref *kref)
639{
640 struct o2net_msg_handler *nmh;
641 nmh = container_of(kref, struct o2net_msg_handler, nh_kref);
642
643 kfree(nmh);
644}
645
646static void o2net_handler_put(struct o2net_msg_handler *nmh)
647{
648 kref_put(&nmh->nh_kref, o2net_handler_kref_release);
649}
650
651/* max_len is protection for the handler func. incoming messages won't
652 * be given to the handler if their payload is longer than the max. */
653int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
654 o2net_msg_handler_func *func, void *data,
655 struct list_head *unreg_list)
656{
657 struct o2net_msg_handler *nmh = NULL;
658 struct rb_node **p, *parent;
659 int ret = 0;
660
661 if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
662 mlog(0, "max_len for message handler out of range: %u\n",
663 max_len);
664 ret = -EINVAL;
665 goto out;
666 }
667
668 if (!msg_type) {
669 mlog(0, "no message type provided: %u, %p\n", msg_type, func);
670 ret = -EINVAL;
671 goto out;
672
673 }
674 if (!func) {
675 mlog(0, "no message handler provided: %u, %p\n",
676 msg_type, func);
677 ret = -EINVAL;
678 goto out;
679 }
680
681 nmh = kcalloc(1, sizeof(struct o2net_msg_handler), GFP_NOFS);
682 if (nmh == NULL) {
683 ret = -ENOMEM;
684 goto out;
685 }
686
687 nmh->nh_func = func;
688 nmh->nh_func_data = data;
689 nmh->nh_msg_type = msg_type;
690 nmh->nh_max_len = max_len;
691 nmh->nh_key = key;
692 /* the tree and list get this ref.. they're both removed in
693 * unregister when this ref is dropped */
694 kref_init(&nmh->nh_kref);
695 INIT_LIST_HEAD(&nmh->nh_unregister_item);
696
697 write_lock(&o2net_handler_lock);
698 if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
699 ret = -EEXIST;
700 else {
701 rb_link_node(&nmh->nh_node, parent, p);
702 rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
703 list_add_tail(&nmh->nh_unregister_item, unreg_list);
704
705 mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
706 func, msg_type, key);
707 /* we've had some trouble with handlers seemingly vanishing. */
708 mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
709 &parent) == NULL,
710 "couldn't find handler we *just* registerd "
711 "for type %u key %08x\n", msg_type, key);
712 }
713 write_unlock(&o2net_handler_lock);
714 if (ret)
715 goto out;
716
717out:
718 if (ret)
719 kfree(nmh);
720
721 return ret;
722}
723EXPORT_SYMBOL_GPL(o2net_register_handler);
724
725void o2net_unregister_handler_list(struct list_head *list)
726{
727 struct list_head *pos, *n;
728 struct o2net_msg_handler *nmh;
729
730 write_lock(&o2net_handler_lock);
731 list_for_each_safe(pos, n, list) {
732 nmh = list_entry(pos, struct o2net_msg_handler,
733 nh_unregister_item);
734 mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
735 nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
736 rb_erase(&nmh->nh_node, &o2net_handler_tree);
737 list_del_init(&nmh->nh_unregister_item);
738 kref_put(&nmh->nh_kref, o2net_handler_kref_release);
739 }
740 write_unlock(&o2net_handler_lock);
741}
742EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
743
744static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
745{
746 struct o2net_msg_handler *nmh;
747
748 read_lock(&o2net_handler_lock);
749 nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
750 if (nmh)
751 kref_get(&nmh->nh_kref);
752 read_unlock(&o2net_handler_lock);
753
754 return nmh;
755}
756
757/* ------------------------------------------------------------ */
758
759static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
760{
761 int ret;
762 mm_segment_t oldfs;
763 struct kvec vec = {
764 .iov_len = len,
765 .iov_base = data,
766 };
767 struct msghdr msg = {
768 .msg_iovlen = 1,
769 .msg_iov = (struct iovec *)&vec,
770 .msg_flags = MSG_DONTWAIT,
771 };
772
773 oldfs = get_fs();
774 set_fs(get_ds());
775 ret = sock_recvmsg(sock, &msg, len, msg.msg_flags);
776 set_fs(oldfs);
777
778 return ret;
779}
780
781static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
782 size_t veclen, size_t total)
783{
784 int ret;
785 mm_segment_t oldfs;
786 struct msghdr msg = {
787 .msg_iov = (struct iovec *)vec,
788 .msg_iovlen = veclen,
789 };
790
791 if (sock == NULL) {
792 ret = -EINVAL;
793 goto out;
794 }
795
796 oldfs = get_fs();
797 set_fs(get_ds());
798 ret = sock_sendmsg(sock, &msg, total);
799 set_fs(oldfs);
800 if (ret != total) {
801 mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret,
802 total);
803 if (ret >= 0)
804 ret = -EPIPE; /* should be smarter, I bet */
805 goto out;
806 }
807
808 ret = 0;
809out:
810 if (ret < 0)
811 mlog(0, "returning error: %d\n", ret);
812 return ret;
813}
814
815static void o2net_sendpage(struct o2net_sock_container *sc,
816 void *kmalloced_virt,
817 size_t size)
818{
819 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
820 ssize_t ret;
821
822
823 ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
824 virt_to_page(kmalloced_virt),
825 (long)kmalloced_virt & ~PAGE_MASK,
826 size, MSG_DONTWAIT);
827 if (ret != size) {
828 mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
829 " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
830 o2net_ensure_shutdown(nn, sc, 0);
831 }
832}
833
834static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
835{
836 memset(msg, 0, sizeof(struct o2net_msg));
837 msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
838 msg->data_len = cpu_to_be16(data_len);
839 msg->msg_type = cpu_to_be16(msg_type);
840 msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
841 msg->status = 0;
842 msg->key = cpu_to_be32(key);
843}
844
845static int o2net_tx_can_proceed(struct o2net_node *nn,
846 struct o2net_sock_container **sc_ret,
847 int *error)
848{
849 int ret = 0;
850
851 spin_lock(&nn->nn_lock);
852 if (nn->nn_persistent_error) {
853 ret = 1;
854 *sc_ret = NULL;
855 *error = nn->nn_persistent_error;
856 } else if (nn->nn_sc_valid) {
857 kref_get(&nn->nn_sc->sc_kref);
858
859 ret = 1;
860 *sc_ret = nn->nn_sc;
861 *error = 0;
862 }
863 spin_unlock(&nn->nn_lock);
864
865 return ret;
866}
867
868int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
869 size_t caller_veclen, u8 target_node, int *status)
870{
871 int ret, error = 0;
872 struct o2net_msg *msg = NULL;
873 size_t veclen, caller_bytes = 0;
874 struct kvec *vec = NULL;
875 struct o2net_sock_container *sc = NULL;
876 struct o2net_node *nn = o2net_nn_from_num(target_node);
877 struct o2net_status_wait nsw = {
878 .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
879 };
880
881 if (o2net_wq == NULL) {
882 mlog(0, "attempt to tx without o2netd running\n");
883 ret = -ESRCH;
884 goto out;
885 }
886
887 if (caller_veclen == 0) {
888 mlog(0, "bad kvec array length\n");
889 ret = -EINVAL;
890 goto out;
891 }
892
893 caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
894 if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
895 mlog(0, "total payload len %zu too large\n", caller_bytes);
896 ret = -EINVAL;
897 goto out;
898 }
899
900 if (target_node == o2nm_this_node()) {
901 ret = -ELOOP;
902 goto out;
903 }
904
905 ret = wait_event_interruptible(nn->nn_sc_wq,
906 o2net_tx_can_proceed(nn, &sc, &error));
907 if (!ret && error)
908 ret = error;
909 if (ret)
910 goto out;
911
912 veclen = caller_veclen + 1;
913 vec = kmalloc(sizeof(struct kvec) * veclen, GFP_ATOMIC);
914 if (vec == NULL) {
915 mlog(0, "failed to %zu element kvec!\n", veclen);
916 ret = -ENOMEM;
917 goto out;
918 }
919
920 msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
921 if (!msg) {
922 mlog(0, "failed to allocate a o2net_msg!\n");
923 ret = -ENOMEM;
924 goto out;
925 }
926
927 o2net_init_msg(msg, caller_bytes, msg_type, key);
928
929 vec[0].iov_len = sizeof(struct o2net_msg);
930 vec[0].iov_base = msg;
931 memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
932
933 ret = o2net_prep_nsw(nn, &nsw);
934 if (ret)
935 goto out;
936
937 msg->msg_num = cpu_to_be32(nsw.ns_id);
938
939 /* finally, convert the message header to network byte-order
940 * and send */
941 ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
942 sizeof(struct o2net_msg) + caller_bytes);
943 msglog(msg, "sending returned %d\n", ret);
944 if (ret < 0) {
945 mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
946 goto out;
947 }
948
949 /* wait on other node's handler */
950 wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
951
952 /* Note that we avoid overwriting the callers status return
953 * variable if a system error was reported on the other
954 * side. Callers beware. */
955 ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
956 if (status && !ret)
957 *status = nsw.ns_status;
958
959 mlog(0, "woken, returning system status %d, user status %d\n",
960 ret, nsw.ns_status);
961out:
962 if (sc)
963 sc_put(sc);
964 if (vec)
965 kfree(vec);
966 if (msg)
967 kfree(msg);
968 o2net_complete_nsw(nn, &nsw, 0, 0, 0);
969 return ret;
970}
971EXPORT_SYMBOL_GPL(o2net_send_message_vec);
972
973int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
974 u8 target_node, int *status)
975{
976 struct kvec vec = {
977 .iov_base = data,
978 .iov_len = len,
979 };
980 return o2net_send_message_vec(msg_type, key, &vec, 1,
981 target_node, status);
982}
983EXPORT_SYMBOL_GPL(o2net_send_message);
984
985static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
986 enum o2net_system_error syserr, int err)
987{
988 struct kvec vec = {
989 .iov_base = hdr,
990 .iov_len = sizeof(struct o2net_msg),
991 };
992
993 BUG_ON(syserr >= O2NET_ERR_MAX);
994
995 /* leave other fields intact from the incoming message, msg_num
996 * in particular */
997 hdr->sys_status = cpu_to_be32(syserr);
998 hdr->status = cpu_to_be32(err);
999 hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic
1000 hdr->data_len = 0;
1001
1002 msglog(hdr, "about to send status magic %d\n", err);
1003 /* hdr has been in host byteorder this whole time */
1004 return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
1005}
1006
1007/* this returns -errno if the header was unknown or too large, etc.
1008 * after this is called the buffer us reused for the next message */
1009static int o2net_process_message(struct o2net_sock_container *sc,
1010 struct o2net_msg *hdr)
1011{
1012 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
1013 int ret = 0, handler_status;
1014 enum o2net_system_error syserr;
1015 struct o2net_msg_handler *nmh = NULL;
1016
1017 msglog(hdr, "processing message\n");
1018
1019 o2net_sc_postpone_idle(sc);
1020
1021 switch(be16_to_cpu(hdr->magic)) {
1022 case O2NET_MSG_STATUS_MAGIC:
1023 /* special type for returning message status */
1024 o2net_complete_nsw(nn, NULL,
1025 be32_to_cpu(hdr->msg_num),
1026 be32_to_cpu(hdr->sys_status),
1027 be32_to_cpu(hdr->status));
1028 goto out;
1029 case O2NET_MSG_KEEP_REQ_MAGIC:
1030 o2net_sendpage(sc, o2net_keep_resp,
1031 sizeof(*o2net_keep_resp));
1032 goto out;
1033 case O2NET_MSG_KEEP_RESP_MAGIC:
1034 goto out;
1035 case O2NET_MSG_MAGIC:
1036 break;
1037 default:
1038 msglog(hdr, "bad magic\n");
1039 ret = -EINVAL;
1040 goto out;
1041 break;
1042 }
1043
1044 /* find a handler for it */
1045 handler_status = 0;
1046 nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
1047 be32_to_cpu(hdr->key));
1048 if (!nmh) {
1049 mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
1050 be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
1051 syserr = O2NET_ERR_NO_HNDLR;
1052 goto out_respond;
1053 }
1054
1055 syserr = O2NET_ERR_NONE;
1056
1057 if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
1058 syserr = O2NET_ERR_OVERFLOW;
1059
1060 if (syserr != O2NET_ERR_NONE)
1061 goto out_respond;
1062
1063 do_gettimeofday(&sc->sc_tv_func_start);
1064 sc->sc_msg_key = be32_to_cpu(hdr->key);
1065 sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
1066 handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
1067 be16_to_cpu(hdr->data_len),
1068 nmh->nh_func_data);
1069 do_gettimeofday(&sc->sc_tv_func_stop);
1070
1071out_respond:
1072 /* this destroys the hdr, so don't use it after this */
1073 ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
1074 handler_status);
1075 hdr = NULL;
1076 mlog(0, "sending handler status %d, syserr %d returned %d\n",
1077 handler_status, syserr, ret);
1078
1079out:
1080 if (nmh)
1081 o2net_handler_put(nmh);
1082 return ret;
1083}
1084
1085static int o2net_check_handshake(struct o2net_sock_container *sc)
1086{
1087 struct o2net_handshake *hand = page_address(sc->sc_page);
1088 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
1089
1090 if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
1091 mlog(ML_NOTICE, SC_NODEF_FMT " advertised net protocol "
1092 "version %llu but %llu is required, disconnecting\n",
1093 SC_NODEF_ARGS(sc),
1094 (unsigned long long)be64_to_cpu(hand->protocol_version),
1095 O2NET_PROTOCOL_VERSION);
1096
1097 /* don't bother reconnecting if its the wrong version. */
1098 o2net_ensure_shutdown(nn, sc, -ENOTCONN);
1099 return -1;
1100 }
1101
1102 sc->sc_handshake_ok = 1;
1103
1104 spin_lock(&nn->nn_lock);
1105 /* set valid and queue the idle timers only if it hasn't been
1106 * shut down already */
1107 if (nn->nn_sc == sc) {
1108 o2net_sc_postpone_idle(sc);
1109 o2net_set_nn_state(nn, sc, 1, 0);
1110 }
1111 spin_unlock(&nn->nn_lock);
1112
1113 /* shift everything up as though it wasn't there */
1114 sc->sc_page_off -= sizeof(struct o2net_handshake);
1115 if (sc->sc_page_off)
1116 memmove(hand, hand + 1, sc->sc_page_off);
1117
1118 return 0;
1119}
1120
1121/* this demuxes the queued rx bytes into header or payload bits and calls
1122 * handlers as each full message is read off the socket. it returns -error,
1123 * == 0 eof, or > 0 for progress made.*/
1124static int o2net_advance_rx(struct o2net_sock_container *sc)
1125{
1126 struct o2net_msg *hdr;
1127 int ret = 0;
1128 void *data;
1129 size_t datalen;
1130
1131 sclog(sc, "receiving\n");
1132 do_gettimeofday(&sc->sc_tv_advance_start);
1133
1134 /* do we need more header? */
1135 if (sc->sc_page_off < sizeof(struct o2net_msg)) {
1136 data = page_address(sc->sc_page) + sc->sc_page_off;
1137 datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
1138 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
1139 if (ret > 0) {
1140 sc->sc_page_off += ret;
1141
1142 /* this working relies on the handshake being
1143 * smaller than the normal message header */
1144 if (sc->sc_page_off >= sizeof(struct o2net_handshake)&&
1145 !sc->sc_handshake_ok && o2net_check_handshake(sc)) {
1146 ret = -EPROTO;
1147 goto out;
1148 }
1149
1150 /* only swab incoming here.. we can
1151 * only get here once as we cross from
1152 * being under to over */
1153 if (sc->sc_page_off == sizeof(struct o2net_msg)) {
1154 hdr = page_address(sc->sc_page);
1155 if (be16_to_cpu(hdr->data_len) >
1156 O2NET_MAX_PAYLOAD_BYTES)
1157 ret = -EOVERFLOW;
1158 }
1159 }
1160 if (ret <= 0)
1161 goto out;
1162 }
1163
1164 if (sc->sc_page_off < sizeof(struct o2net_msg)) {
1165 /* oof, still don't have a header */
1166 goto out;
1167 }
1168
1169 /* this was swabbed above when we first read it */
1170 hdr = page_address(sc->sc_page);
1171
1172 msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
1173
1174 /* do we need more payload? */
1175 if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
1176 /* need more payload */
1177 data = page_address(sc->sc_page) + sc->sc_page_off;
1178 datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
1179 sc->sc_page_off;
1180 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
1181 if (ret > 0)
1182 sc->sc_page_off += ret;
1183 if (ret <= 0)
1184 goto out;
1185 }
1186
1187 if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
1188 /* we can only get here once, the first time we read
1189 * the payload.. so set ret to progress if the handler
1190 * works out. after calling this the message is toast */
1191 ret = o2net_process_message(sc, hdr);
1192 if (ret == 0)
1193 ret = 1;
1194 sc->sc_page_off = 0;
1195 }
1196
1197out:
1198 sclog(sc, "ret = %d\n", ret);
1199 do_gettimeofday(&sc->sc_tv_advance_stop);
1200 return ret;
1201}
1202
1203/* this work func is triggerd by data ready. it reads until it can read no
1204 * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing
1205 * our work the work struct will be marked and we'll be called again. */
c4028958 1206static void o2net_rx_until_empty(struct work_struct *work)
98211489 1207{
c4028958
DH
1208 struct o2net_sock_container *sc =
1209 container_of(work, struct o2net_sock_container, sc_rx_work);
98211489
ZB
1210 int ret;
1211
1212 do {
1213 ret = o2net_advance_rx(sc);
1214 } while (ret > 0);
1215
1216 if (ret <= 0 && ret != -EAGAIN) {
1217 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
1218 sclog(sc, "saw error %d, closing\n", ret);
1219 /* not permanent so read failed handshake can retry */
1220 o2net_ensure_shutdown(nn, sc, 0);
1221 }
1222
1223 sc_put(sc);
1224}
1225
1226static int o2net_set_nodelay(struct socket *sock)
1227{
1228 int ret, val = 1;
1229 mm_segment_t oldfs;
1230
1231 oldfs = get_fs();
1232 set_fs(KERNEL_DS);
1233
1234 /*
1235 * Dear unsuspecting programmer,
1236 *
1237 * Don't use sock_setsockopt() for SOL_TCP. It doesn't check its level
1238 * argument and assumes SOL_SOCKET so, say, your TCP_NODELAY will
1239 * silently turn into SO_DEBUG.
1240 *
1241 * Yours,
1242 * Keeper of hilariously fragile interfaces.
1243 */
1244 ret = sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY,
1245 (char __user *)&val, sizeof(val));
1246
1247 set_fs(oldfs);
1248 return ret;
1249}
1250
1251/* ------------------------------------------------------------ */
1252
1253/* called when a connect completes and after a sock is accepted. the
1254 * rx path will see the response and mark the sc valid */
c4028958 1255static void o2net_sc_connect_completed(struct work_struct *work)
98211489 1256{
c4028958
DH
1257 struct o2net_sock_container *sc =
1258 container_of(work, struct o2net_sock_container,
1259 sc_connect_work);
98211489
ZB
1260
1261 mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
1262 (unsigned long long)O2NET_PROTOCOL_VERSION,
1263 (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
1264
1265 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
1266 sc_put(sc);
1267}
1268
1269/* this is called as a work_struct func. */
c4028958 1270static void o2net_sc_send_keep_req(struct work_struct *work)
98211489 1271{
c4028958
DH
1272 struct o2net_sock_container *sc =
1273 container_of(work, struct o2net_sock_container,
1274 sc_keepalive_work.work);
98211489
ZB
1275
1276 o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
1277 sc_put(sc);
1278}
1279
1280/* socket shutdown does a del_timer_sync against this as it tears down.
1281 * we can't start this timer until we've got to the point in sc buildup
1282 * where shutdown is going to be involved */
1283static void o2net_idle_timer(unsigned long data)
1284{
1285 struct o2net_sock_container *sc = (struct o2net_sock_container *)data;
1286 struct timeval now;
1287
1288 do_gettimeofday(&now);
1289
781ee3e2 1290 printk(KERN_INFO "o2net: connection to " SC_NODEF_FMT " has been idle for 10 "
98211489
ZB
1291 "seconds, shutting it down.\n", SC_NODEF_ARGS(sc));
1292 mlog(ML_NOTICE, "here are some times that might help debug the "
1293 "situation: (tmr %ld.%ld now %ld.%ld dr %ld.%ld adv "
1294 "%ld.%ld:%ld.%ld func (%08x:%u) %ld.%ld:%ld.%ld)\n",
215c7f9f
MF
1295 sc->sc_tv_timer.tv_sec, (long) sc->sc_tv_timer.tv_usec,
1296 now.tv_sec, (long) now.tv_usec,
1297 sc->sc_tv_data_ready.tv_sec, (long) sc->sc_tv_data_ready.tv_usec,
1298 sc->sc_tv_advance_start.tv_sec,
1299 (long) sc->sc_tv_advance_start.tv_usec,
1300 sc->sc_tv_advance_stop.tv_sec,
1301 (long) sc->sc_tv_advance_stop.tv_usec,
98211489 1302 sc->sc_msg_key, sc->sc_msg_type,
215c7f9f
MF
1303 sc->sc_tv_func_start.tv_sec, (long) sc->sc_tv_func_start.tv_usec,
1304 sc->sc_tv_func_stop.tv_sec, (long) sc->sc_tv_func_stop.tv_usec);
98211489
ZB
1305
1306 o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
1307}
1308
1309static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
1310{
1311 o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
1312 o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
1313 O2NET_KEEPALIVE_DELAY_SECS * HZ);
1314 do_gettimeofday(&sc->sc_tv_timer);
1315 mod_timer(&sc->sc_idle_timeout,
1316 jiffies + (O2NET_IDLE_TIMEOUT_SECS * HZ));
1317}
1318
1319/* this work func is kicked whenever a path sets the nn state which doesn't
1320 * have valid set. This includes seeing hb come up, losing a connection,
1321 * having a connect attempt fail, etc. This centralizes the logic which decides
1322 * if a connect attempt should be made or if we should give up and all future
1323 * transmit attempts should fail */
c4028958 1324static void o2net_start_connect(struct work_struct *work)
98211489 1325{
c4028958
DH
1326 struct o2net_node *nn =
1327 container_of(work, struct o2net_node, nn_connect_work.work);
98211489 1328 struct o2net_sock_container *sc = NULL;
b7668c72 1329 struct o2nm_node *node = NULL, *mynode = NULL;
98211489
ZB
1330 struct socket *sock = NULL;
1331 struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
c4028958 1332 int ret = 0, stop;
98211489
ZB
1333
1334 /* if we're greater we initiate tx, otherwise we accept */
1335 if (o2nm_this_node() <= o2net_num_from_nn(nn))
1336 goto out;
1337
1338 /* watch for racing with tearing a node down */
1339 node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
1340 if (node == NULL) {
1341 ret = 0;
1342 goto out;
1343 }
1344
b7668c72
SM
1345 mynode = o2nm_get_node_by_num(o2nm_this_node());
1346 if (mynode == NULL) {
1347 ret = 0;
1348 goto out;
1349 }
1350
98211489
ZB
1351 spin_lock(&nn->nn_lock);
1352 /* see if we already have one pending or have given up */
c4028958 1353 stop = (nn->nn_sc || nn->nn_persistent_error);
98211489 1354 spin_unlock(&nn->nn_lock);
c4028958 1355 if (stop)
98211489
ZB
1356 goto out;
1357
1358 nn->nn_last_connect_attempt = jiffies;
1359
1360 sc = sc_alloc(node);
1361 if (sc == NULL) {
1362 mlog(0, "couldn't allocate sc\n");
1363 ret = -ENOMEM;
1364 goto out;
1365 }
1366
1367 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1368 if (ret < 0) {
1369 mlog(0, "can't create socket: %d\n", ret);
1370 goto out;
1371 }
1372 sc->sc_sock = sock; /* freed by sc_kref_release */
1373
1374 sock->sk->sk_allocation = GFP_ATOMIC;
1375
1376 myaddr.sin_family = AF_INET;
b7668c72 1377 myaddr.sin_addr.s_addr = (__force u32)mynode->nd_ipv4_address;
98211489
ZB
1378 myaddr.sin_port = (__force u16)htons(0); /* any port */
1379
1380 ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
1381 sizeof(myaddr));
1382 if (ret) {
b7668c72
SM
1383 mlog(ML_ERROR, "bind failed with %d at address %u.%u.%u.%u\n",
1384 ret, NIPQUAD(mynode->nd_ipv4_address));
98211489
ZB
1385 goto out;
1386 }
1387
1388 ret = o2net_set_nodelay(sc->sc_sock);
1389 if (ret) {
1390 mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
1391 goto out;
1392 }
1393
1394 o2net_register_callbacks(sc->sc_sock->sk, sc);
1395
1396 spin_lock(&nn->nn_lock);
1397 /* handshake completion will set nn->nn_sc_valid */
1398 o2net_set_nn_state(nn, sc, 0, 0);
1399 spin_unlock(&nn->nn_lock);
1400
1401 remoteaddr.sin_family = AF_INET;
1402 remoteaddr.sin_addr.s_addr = (__force u32)node->nd_ipv4_address;
1403 remoteaddr.sin_port = (__force u16)node->nd_ipv4_port;
1404
1405 ret = sc->sc_sock->ops->connect(sc->sc_sock,
1406 (struct sockaddr *)&remoteaddr,
1407 sizeof(remoteaddr),
1408 O_NONBLOCK);
1409 if (ret == -EINPROGRESS)
1410 ret = 0;
1411
1412out:
1413 if (ret) {
1414 mlog(ML_NOTICE, "connect attempt to " SC_NODEF_FMT " failed "
1415 "with errno %d\n", SC_NODEF_ARGS(sc), ret);
1416 /* 0 err so that another will be queued and attempted
1417 * from set_nn_state */
1418 if (sc)
1419 o2net_ensure_shutdown(nn, sc, 0);
1420 }
1421 if (sc)
1422 sc_put(sc);
1423 if (node)
1424 o2nm_node_put(node);
b7668c72
SM
1425 if (mynode)
1426 o2nm_node_put(mynode);
98211489
ZB
1427
1428 return;
1429}
1430
c4028958 1431static void o2net_connect_expired(struct work_struct *work)
98211489 1432{
c4028958
DH
1433 struct o2net_node *nn =
1434 container_of(work, struct o2net_node, nn_connect_expired.work);
98211489
ZB
1435
1436 spin_lock(&nn->nn_lock);
1437 if (!nn->nn_sc_valid) {
1438 mlog(ML_ERROR, "no connection established with node %u after "
1439 "%u seconds, giving up and returning errors.\n",
1440 o2net_num_from_nn(nn), O2NET_IDLE_TIMEOUT_SECS);
1441
1442 o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
1443 }
1444 spin_unlock(&nn->nn_lock);
1445}
1446
c4028958 1447static void o2net_still_up(struct work_struct *work)
98211489 1448{
c4028958
DH
1449 struct o2net_node *nn =
1450 container_of(work, struct o2net_node, nn_still_up.work);
98211489
ZB
1451
1452 o2quo_hb_still_up(o2net_num_from_nn(nn));
1453}
1454
1455/* ------------------------------------------------------------ */
1456
1457void o2net_disconnect_node(struct o2nm_node *node)
1458{
1459 struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
1460
1461 /* don't reconnect until it's heartbeating again */
1462 spin_lock(&nn->nn_lock);
1463 o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
1464 spin_unlock(&nn->nn_lock);
1465
1466 if (o2net_wq) {
1467 cancel_delayed_work(&nn->nn_connect_expired);
1468 cancel_delayed_work(&nn->nn_connect_work);
1469 cancel_delayed_work(&nn->nn_still_up);
1470 flush_workqueue(o2net_wq);
1471 }
1472}
1473
1474static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
1475 void *data)
1476{
1477 o2quo_hb_down(node_num);
1478
1479 if (node_num != o2nm_this_node())
1480 o2net_disconnect_node(node);
1481}
1482
1483static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
1484 void *data)
1485{
1486 struct o2net_node *nn = o2net_nn_from_num(node_num);
1487
1488 o2quo_hb_up(node_num);
1489
1490 /* ensure an immediate connect attempt */
1491 nn->nn_last_connect_attempt = jiffies -
1492 (msecs_to_jiffies(O2NET_RECONNECT_DELAY_MS) + 1);
1493
1494 if (node_num != o2nm_this_node()) {
1495 /* heartbeat doesn't work unless a local node number is
1496 * configured and doing so brings up the o2net_wq, so we can
1497 * use it.. */
1498 queue_delayed_work(o2net_wq, &nn->nn_connect_expired,
1499 O2NET_IDLE_TIMEOUT_SECS * HZ);
1500
1501 /* believe it or not, accept and node hearbeating testing
1502 * can succeed for this node before we got here.. so
1503 * only use set_nn_state to clear the persistent error
1504 * if that hasn't already happened */
1505 spin_lock(&nn->nn_lock);
1506 if (nn->nn_persistent_error)
1507 o2net_set_nn_state(nn, NULL, 0, 0);
1508 spin_unlock(&nn->nn_lock);
1509 }
1510}
1511
1512void o2net_unregister_hb_callbacks(void)
1513{
1514 int ret;
1515
1516 ret = o2hb_unregister_callback(&o2net_hb_up);
1517 if (ret < 0)
1518 mlog(ML_ERROR, "Status return %d unregistering heartbeat up "
1519 "callback!\n", ret);
1520
1521 ret = o2hb_unregister_callback(&o2net_hb_down);
1522 if (ret < 0)
1523 mlog(ML_ERROR, "Status return %d unregistering heartbeat down "
1524 "callback!\n", ret);
1525}
1526
1527int o2net_register_hb_callbacks(void)
1528{
1529 int ret;
1530
1531 o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
1532 o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
1533 o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
1534 o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
1535
1536 ret = o2hb_register_callback(&o2net_hb_up);
1537 if (ret == 0)
1538 ret = o2hb_register_callback(&o2net_hb_down);
1539
1540 if (ret)
1541 o2net_unregister_hb_callbacks();
1542
1543 return ret;
1544}
1545
1546/* ------------------------------------------------------------ */
1547
1548static int o2net_accept_one(struct socket *sock)
1549{
1550 int ret, slen;
1551 struct sockaddr_in sin;
1552 struct socket *new_sock = NULL;
1553 struct o2nm_node *node = NULL;
1554 struct o2net_sock_container *sc = NULL;
1555 struct o2net_node *nn;
1556
1557 BUG_ON(sock == NULL);
1558 ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
1559 sock->sk->sk_protocol, &new_sock);
1560 if (ret)
1561 goto out;
1562
1563 new_sock->type = sock->type;
1564 new_sock->ops = sock->ops;
1565 ret = sock->ops->accept(sock, new_sock, O_NONBLOCK);
1566 if (ret < 0)
1567 goto out;
1568
1569 new_sock->sk->sk_allocation = GFP_ATOMIC;
1570
1571 ret = o2net_set_nodelay(new_sock);
1572 if (ret) {
1573 mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
1574 goto out;
1575 }
1576
1577 slen = sizeof(sin);
1578 ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin,
1579 &slen, 1);
1580 if (ret < 0)
1581 goto out;
1582
1583 node = o2nm_get_node_by_ip((__force __be32)sin.sin_addr.s_addr);
1584 if (node == NULL) {
1585 mlog(ML_NOTICE, "attempt to connect from unknown node at "
1586 "%u.%u.%u.%u:%d\n", NIPQUAD(sin.sin_addr.s_addr),
1587 ntohs((__force __be16)sin.sin_port));
1588 ret = -EINVAL;
1589 goto out;
1590 }
1591
1592 if (o2nm_this_node() > node->nd_num) {
1593 mlog(ML_NOTICE, "unexpected connect attempted from a lower "
1594 "numbered node '%s' at " "%u.%u.%u.%u:%d with num %u\n",
1595 node->nd_name, NIPQUAD(sin.sin_addr.s_addr),
1596 ntohs((__force __be16)sin.sin_port), node->nd_num);
1597 ret = -EINVAL;
1598 goto out;
1599 }
1600
1601 /* this happens all the time when the other node sees our heartbeat
1602 * and tries to connect before we see their heartbeat */
1603 if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
1604 mlog(ML_CONN, "attempt to connect from node '%s' at "
1605 "%u.%u.%u.%u:%d but it isn't heartbeating\n",
1606 node->nd_name, NIPQUAD(sin.sin_addr.s_addr),
1607 ntohs((__force __be16)sin.sin_port));
1608 ret = -EINVAL;
1609 goto out;
1610 }
1611
1612 nn = o2net_nn_from_num(node->nd_num);
1613
1614 spin_lock(&nn->nn_lock);
1615 if (nn->nn_sc)
1616 ret = -EBUSY;
1617 else
1618 ret = 0;
1619 spin_unlock(&nn->nn_lock);
1620 if (ret) {
1621 mlog(ML_NOTICE, "attempt to connect from node '%s' at "
1622 "%u.%u.%u.%u:%d but it already has an open connection\n",
1623 node->nd_name, NIPQUAD(sin.sin_addr.s_addr),
1624 ntohs((__force __be16)sin.sin_port));
1625 goto out;
1626 }
1627
1628 sc = sc_alloc(node);
1629 if (sc == NULL) {
1630 ret = -ENOMEM;
1631 goto out;
1632 }
1633
1634 sc->sc_sock = new_sock;
1635 new_sock = NULL;
1636
1637 spin_lock(&nn->nn_lock);
1638 o2net_set_nn_state(nn, sc, 0, 0);
1639 spin_unlock(&nn->nn_lock);
1640
1641 o2net_register_callbacks(sc->sc_sock->sk, sc);
1642 o2net_sc_queue_work(sc, &sc->sc_rx_work);
1643
1644 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
1645
1646out:
1647 if (new_sock)
1648 sock_release(new_sock);
1649 if (node)
1650 o2nm_node_put(node);
1651 if (sc)
1652 sc_put(sc);
1653 return ret;
1654}
1655
c4028958 1656static void o2net_accept_many(struct work_struct *work)
98211489 1657{
c4028958 1658 struct socket *sock = o2net_listen_sock;
98211489
ZB
1659 while (o2net_accept_one(sock) == 0)
1660 cond_resched();
1661}
1662
1663static void o2net_listen_data_ready(struct sock *sk, int bytes)
1664{
1665 void (*ready)(struct sock *sk, int bytes);
1666
1667 read_lock(&sk->sk_callback_lock);
1668 ready = sk->sk_user_data;
1669 if (ready == NULL) { /* check for teardown race */
1670 ready = sk->sk_data_ready;
1671 goto out;
1672 }
1673
1674 /* ->sk_data_ready is also called for a newly established child socket
1675 * before it has been accepted and the acceptor has set up their
1676 * data_ready.. we only want to queue listen work for our listening
1677 * socket */
1678 if (sk->sk_state == TCP_LISTEN) {
1679 mlog(ML_TCP, "bytes: %d\n", bytes);
1680 queue_work(o2net_wq, &o2net_listen_work);
1681 }
1682
1683out:
1684 read_unlock(&sk->sk_callback_lock);
1685 ready(sk, bytes);
1686}
1687
1688static int o2net_open_listening_sock(__be16 port)
1689{
1690 struct socket *sock = NULL;
1691 int ret;
1692 struct sockaddr_in sin = {
1693 .sin_family = PF_INET,
1694 .sin_addr = { .s_addr = (__force u32)htonl(INADDR_ANY) },
1695 .sin_port = (__force u16)port,
1696 };
1697
1698 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1699 if (ret < 0) {
1700 mlog(ML_ERROR, "unable to create socket, ret=%d\n", ret);
1701 goto out;
1702 }
1703
1704 sock->sk->sk_allocation = GFP_ATOMIC;
1705
1706 write_lock_bh(&sock->sk->sk_callback_lock);
1707 sock->sk->sk_user_data = sock->sk->sk_data_ready;
1708 sock->sk->sk_data_ready = o2net_listen_data_ready;
1709 write_unlock_bh(&sock->sk->sk_callback_lock);
1710
1711 o2net_listen_sock = sock;
c4028958 1712 INIT_WORK(&o2net_listen_work, o2net_accept_many);
98211489
ZB
1713
1714 sock->sk->sk_reuse = 1;
1715 ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
1716 if (ret < 0) {
1717 mlog(ML_ERROR, "unable to bind socket to port %d, ret=%d\n",
1718 ntohs(port), ret);
1719 goto out;
1720 }
1721
1722 ret = sock->ops->listen(sock, 64);
1723 if (ret < 0) {
1724 mlog(ML_ERROR, "unable to listen on port %d, ret=%d\n",
1725 ntohs(port), ret);
1726 }
1727
1728out:
1729 if (ret) {
1730 o2net_listen_sock = NULL;
1731 if (sock)
1732 sock_release(sock);
1733 }
1734 return ret;
1735}
1736
1737/*
1738 * called from node manager when we should bring up our network listening
1739 * socket. node manager handles all the serialization to only call this
1740 * once and to match it with o2net_stop_listening(). note,
1741 * o2nm_this_node() doesn't work yet as we're being called while it
1742 * is being set up.
1743 */
1744int o2net_start_listening(struct o2nm_node *node)
1745{
1746 int ret = 0;
1747
1748 BUG_ON(o2net_wq != NULL);
1749 BUG_ON(o2net_listen_sock != NULL);
1750
1751 mlog(ML_KTHREAD, "starting o2net thread...\n");
1752 o2net_wq = create_singlethread_workqueue("o2net");
1753 if (o2net_wq == NULL) {
1754 mlog(ML_ERROR, "unable to launch o2net thread\n");
1755 return -ENOMEM; /* ? */
1756 }
1757
1758 ret = o2net_open_listening_sock(node->nd_ipv4_port);
1759 if (ret) {
1760 destroy_workqueue(o2net_wq);
1761 o2net_wq = NULL;
1762 } else
1763 o2quo_conn_up(node->nd_num);
1764
1765 return ret;
1766}
1767
1768/* again, o2nm_this_node() doesn't work here as we're involved in
1769 * tearing it down */
1770void o2net_stop_listening(struct o2nm_node *node)
1771{
1772 struct socket *sock = o2net_listen_sock;
1773 size_t i;
1774
1775 BUG_ON(o2net_wq == NULL);
1776 BUG_ON(o2net_listen_sock == NULL);
1777
1778 /* stop the listening socket from generating work */
1779 write_lock_bh(&sock->sk->sk_callback_lock);
1780 sock->sk->sk_data_ready = sock->sk->sk_user_data;
1781 sock->sk->sk_user_data = NULL;
1782 write_unlock_bh(&sock->sk->sk_callback_lock);
1783
1784 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
1785 struct o2nm_node *node = o2nm_get_node_by_num(i);
1786 if (node) {
1787 o2net_disconnect_node(node);
1788 o2nm_node_put(node);
1789 }
1790 }
1791
1792 /* finish all work and tear down the work queue */
1793 mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
1794 destroy_workqueue(o2net_wq);
1795 o2net_wq = NULL;
1796
1797 sock_release(o2net_listen_sock);
1798 o2net_listen_sock = NULL;
1799
1800 o2quo_conn_err(node->nd_num);
1801}
1802
1803/* ------------------------------------------------------------ */
1804
1805int o2net_init(void)
1806{
1807 unsigned long i;
1808
1809 o2quo_init();
1810
1811 o2net_hand = kcalloc(1, sizeof(struct o2net_handshake), GFP_KERNEL);
1812 o2net_keep_req = kcalloc(1, sizeof(struct o2net_msg), GFP_KERNEL);
1813 o2net_keep_resp = kcalloc(1, sizeof(struct o2net_msg), GFP_KERNEL);
1814 if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp) {
1815 kfree(o2net_hand);
1816 kfree(o2net_keep_req);
1817 kfree(o2net_keep_resp);
1818 return -ENOMEM;
1819 }
1820
1821 o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
1822 o2net_hand->connector_id = cpu_to_be64(1);
1823
1824 o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
1825 o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
1826
1827 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
1828 struct o2net_node *nn = o2net_nn_from_num(i);
1829
1830 spin_lock_init(&nn->nn_lock);
c4028958
DH
1831 INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
1832 INIT_DELAYED_WORK(&nn->nn_connect_expired,
1833 o2net_connect_expired);
1834 INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
98211489
ZB
1835 /* until we see hb from a node we'll return einval */
1836 nn->nn_persistent_error = -ENOTCONN;
1837 init_waitqueue_head(&nn->nn_sc_wq);
1838 idr_init(&nn->nn_status_idr);
1839 INIT_LIST_HEAD(&nn->nn_status_list);
1840 }
1841
1842 return 0;
1843}
1844
1845void o2net_exit(void)
1846{
1847 o2quo_exit();
1848 kfree(o2net_hand);
1849 kfree(o2net_keep_req);
1850 kfree(o2net_keep_resp);
1851}