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 <linux/net.h> 62#include <net/tcp.h> 63 64#include <asm/uaccess.h> 65 66#include "heartbeat.h" 67#include "tcp.h" 68#include "nodemanager.h" 69#define MLOG_MASK_PREFIX ML_TCP 70#include "masklog.h" 71#include "quorum.h" 72 73#include "tcp_internal.h" 74 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) 79 80/* 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. 84 */ 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); \ 93} while (0) 94 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 , \ 101 ##args); \ 102} while (0) 103 104static DEFINE_RWLOCK(o2net_handler_lock); 105static struct rb_root o2net_handler_tree = RB_ROOT; 106 107static struct o2net_node o2net_nodes[O2NM_MAX_NODES]; 108 109static struct socket *o2net_listen_sock = NULL; 110 111/* 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. 118 */ 119static struct workqueue_struct *o2net_wq; 120static struct work_struct o2net_listen_work; 121 122static struct o2hb_callback_func o2net_hb_up, o2net_hb_down; 123#define O2NET_HB_PRI 0x1 124 125static struct o2net_handshake *o2net_hand; 126static struct o2net_msg *o2net_keep_req, *o2net_keep_resp; 127 128static 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,}; 133 134/* can't quite avoid *all* internal declarations :/ */ 135static void o2net_sc_connect_completed(struct work_struct *work); 136static void o2net_rx_until_empty(struct work_struct *work); 137static void o2net_shutdown_sc(struct work_struct *work); 138static void o2net_listen_data_ready(struct sock *sk, int bytes); 139static void o2net_sc_send_keep_req(struct work_struct *work); 140static void o2net_idle_timer(unsigned long data); 141static void o2net_sc_postpone_idle(struct o2net_sock_container *sc); 142static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc); 143 144#ifdef CONFIG_DEBUG_FS 145static void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype, 146 u32 msgkey, struct task_struct *task, u8 node) 147{ 148 INIT_LIST_HEAD(&nst->st_net_debug_item); 149 nst->st_task = task; 150 nst->st_msg_type = msgtype; 151 nst->st_msg_key = msgkey; 152 nst->st_node = node; 153} 154 155static void o2net_set_nst_sock_time(struct o2net_send_tracking *nst) 156{ 157 do_gettimeofday(&nst->st_sock_time); 158} 159 160static void o2net_set_nst_send_time(struct o2net_send_tracking *nst) 161{ 162 do_gettimeofday(&nst->st_send_time); 163} 164 165static void o2net_set_nst_status_time(struct o2net_send_tracking *nst) 166{ 167 do_gettimeofday(&nst->st_status_time); 168} 169 170static void o2net_set_nst_sock_container(struct o2net_send_tracking *nst, 171 struct o2net_sock_container *sc) 172{ 173 nst->st_sc = sc; 174} 175 176static void o2net_set_nst_msg_id(struct o2net_send_tracking *nst, u32 msg_id) 177{ 178 nst->st_id = msg_id; 179} 180 181#else /* CONFIG_DEBUG_FS */ 182 183static inline void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype, 184 u32 msgkey, struct task_struct *task, u8 node) 185{ 186} 187 188static inline void o2net_set_nst_sock_time(struct o2net_send_tracking *nst) 189{ 190} 191 192static inline void o2net_set_nst_send_time(struct o2net_send_tracking *nst) 193{ 194} 195 196static inline void o2net_set_nst_status_time(struct o2net_send_tracking *nst) 197{ 198} 199 200static inline void o2net_set_nst_sock_container(struct o2net_send_tracking *nst, 201 struct o2net_sock_container *sc) 202{ 203} 204 205static inline void o2net_set_nst_msg_id(struct o2net_send_tracking *nst, 206 u32 msg_id) 207{ 208} 209 210#endif /* CONFIG_DEBUG_FS */ 211 212static inline int o2net_reconnect_delay(void) 213{ 214 return o2nm_single_cluster->cl_reconnect_delay_ms; 215} 216 217static inline int o2net_keepalive_delay(void) 218{ 219 return o2nm_single_cluster->cl_keepalive_delay_ms; 220} 221 222static inline int o2net_idle_timeout(void) 223{ 224 return o2nm_single_cluster->cl_idle_timeout_ms; 225} 226 227static inline int o2net_sys_err_to_errno(enum o2net_system_error err) 228{ 229 int trans; 230 BUG_ON(err >= O2NET_ERR_MAX); 231 trans = o2net_sys_err_translations[err]; 232 233 /* Just in case we mess up the translation table above */ 234 BUG_ON(err != O2NET_ERR_NONE && trans == 0); 235 return trans; 236} 237 238static struct o2net_node * o2net_nn_from_num(u8 node_num) 239{ 240 BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes)); 241 return &o2net_nodes[node_num]; 242} 243 244static u8 o2net_num_from_nn(struct o2net_node *nn) 245{ 246 BUG_ON(nn == NULL); 247 return nn - o2net_nodes; 248} 249 250/* ------------------------------------------------------------ */ 251 252static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw) 253{ 254 int ret = 0; 255 256 do { 257 if (!idr_pre_get(&nn->nn_status_idr, GFP_ATOMIC)) { 258 ret = -EAGAIN; 259 break; 260 } 261 spin_lock(&nn->nn_lock); 262 ret = idr_get_new(&nn->nn_status_idr, nsw, &nsw->ns_id); 263 if (ret == 0) 264 list_add_tail(&nsw->ns_node_item, 265 &nn->nn_status_list); 266 spin_unlock(&nn->nn_lock); 267 } while (ret == -EAGAIN); 268 269 if (ret == 0) { 270 init_waitqueue_head(&nsw->ns_wq); 271 nsw->ns_sys_status = O2NET_ERR_NONE; 272 nsw->ns_status = 0; 273 } 274 275 return ret; 276} 277 278static void o2net_complete_nsw_locked(struct o2net_node *nn, 279 struct o2net_status_wait *nsw, 280 enum o2net_system_error sys_status, 281 s32 status) 282{ 283 assert_spin_locked(&nn->nn_lock); 284 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); 291 } 292} 293 294static void o2net_complete_nsw(struct o2net_node *nn, 295 struct o2net_status_wait *nsw, 296 u64 id, enum o2net_system_error sys_status, 297 s32 status) 298{ 299 spin_lock(&nn->nn_lock); 300 if (nsw == NULL) { 301 if (id > INT_MAX) 302 goto out; 303 304 nsw = idr_find(&nn->nn_status_idr, id); 305 if (nsw == NULL) 306 goto out; 307 } 308 309 o2net_complete_nsw_locked(nn, nsw, sys_status, status); 310 311out: 312 spin_unlock(&nn->nn_lock); 313 return; 314} 315 316static void o2net_complete_nodes_nsw(struct o2net_node *nn) 317{ 318 struct o2net_status_wait *nsw, *tmp; 319 unsigned int num_kills = 0; 320 321 assert_spin_locked(&nn->nn_lock); 322 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); 325 num_kills++; 326 } 327 328 mlog(0, "completed %d messages for node %u\n", num_kills, 329 o2net_num_from_nn(nn)); 330} 331 332static int o2net_nsw_completed(struct o2net_node *nn, 333 struct o2net_status_wait *nsw) 334{ 335 int completed; 336 spin_lock(&nn->nn_lock); 337 completed = list_empty(&nsw->ns_node_item); 338 spin_unlock(&nn->nn_lock); 339 return completed; 340} 341 342/* ------------------------------------------------------------ */ 343 344static void sc_kref_release(struct kref *kref) 345{ 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)); 349 350 sclog(sc, "releasing\n"); 351 352 if (sc->sc_sock) { 353 sock_release(sc->sc_sock); 354 sc->sc_sock = NULL; 355 } 356 357 o2nm_node_put(sc->sc_node); 358 sc->sc_node = NULL; 359 360 o2net_debug_del_sc(sc); 361 kfree(sc); 362} 363 364static void sc_put(struct o2net_sock_container *sc) 365{ 366 sclog(sc, "put\n"); 367 kref_put(&sc->sc_kref, sc_kref_release); 368} 369static void sc_get(struct o2net_sock_container *sc) 370{ 371 sclog(sc, "get\n"); 372 kref_get(&sc->sc_kref); 373} 374static struct o2net_sock_container *sc_alloc(struct o2nm_node *node) 375{ 376 struct o2net_sock_container *sc, *ret = NULL; 377 struct page *page = NULL; 378 379 page = alloc_page(GFP_NOFS); 380 sc = kzalloc(sizeof(*sc), GFP_NOFS); 381 if (sc == NULL || page == NULL) 382 goto out; 383 384 kref_init(&sc->sc_kref); 385 o2nm_node_get(node); 386 sc->sc_node = node; 387 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); 392 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; 396 397 sclog(sc, "alloced\n"); 398 399 ret = sc; 400 sc->sc_page = page; 401 o2net_debug_add_sc(sc); 402 sc = NULL; 403 page = NULL; 404 405out: 406 if (page) 407 __free_page(page); 408 kfree(sc); 409 410 return ret; 411} 412 413/* ------------------------------------------------------------ */ 414 415static void o2net_sc_queue_work(struct o2net_sock_container *sc, 416 struct work_struct *work) 417{ 418 sc_get(sc); 419 if (!queue_work(o2net_wq, work)) 420 sc_put(sc); 421} 422static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc, 423 struct delayed_work *work, 424 int delay) 425{ 426 sc_get(sc); 427 if (!queue_delayed_work(o2net_wq, work, delay)) 428 sc_put(sc); 429} 430static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc, 431 struct delayed_work *work) 432{ 433 if (cancel_delayed_work(work)) 434 sc_put(sc); 435} 436 437static atomic_t o2net_connected_peers = ATOMIC_INIT(0); 438 439int o2net_num_connected_peers(void) 440{ 441 return atomic_read(&o2net_connected_peers); 442} 443 444static void o2net_set_nn_state(struct o2net_node *nn, 445 struct o2net_sock_container *sc, 446 unsigned valid, int err) 447{ 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; 451 452 assert_spin_locked(&nn->nn_lock); 453 454 if (old_sc && !sc) 455 atomic_dec(&o2net_connected_peers); 456 else if (!old_sc && sc) 457 atomic_inc(&o2net_connected_peers); 458 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); 464 465 if (was_valid && !valid && err == 0) 466 err = -ENOTCONN; 467 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); 471 472 nn->nn_sc = sc; 473 nn->nn_sc_valid = valid ? 1 : 0; 474 nn->nn_persistent_error = err; 475 476 /* mirrors o2net_tx_can_proceed() */ 477 if (nn->nn_persistent_error || nn->nn_sc_valid) 478 wake_up(&nn->nn_sc_wq); 479 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)); 484 } 485 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); 490 } 491 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", 498 SC_NODEF_ARGS(sc)); 499 } 500 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) { 506 unsigned long delay; 507 /* delay if we're withing a RECONNECT_DELAY of the 508 * last attempt */ 509 delay = (nn->nn_last_connect_attempt + 510 msecs_to_jiffies(o2net_reconnect_delay())) 511 - jiffies; 512 if (delay > msecs_to_jiffies(o2net_reconnect_delay())) 513 delay = 0; 514 mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay); 515 queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay); 516 517 /* 518 * Delay the expired work after idle timeout. 519 * 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. 525 */ 526 delay += msecs_to_jiffies(o2net_idle_timeout()); 527 queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay); 528 } 529 530 /* keep track of the nn's sc ref for the caller */ 531 if ((old_sc == NULL) && sc) 532 sc_get(sc); 533 if (old_sc && (old_sc != sc)) { 534 o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work); 535 sc_put(old_sc); 536 } 537} 538 539/* see o2net_register_callbacks() */ 540static void o2net_data_ready(struct sock *sk, int bytes) 541{ 542 void (*ready)(struct sock *sk, int bytes); 543 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; 551 } else { 552 ready = sk->sk_data_ready; 553 } 554 read_unlock(&sk->sk_callback_lock); 555 556 ready(sk, bytes); 557} 558 559/* see o2net_register_callbacks() */ 560static void o2net_state_change(struct sock *sk) 561{ 562 void (*state_change)(struct sock *sk); 563 struct o2net_sock_container *sc; 564 565 read_lock(&sk->sk_callback_lock); 566 sc = sk->sk_user_data; 567 if (sc == NULL) { 568 state_change = sk->sk_state_change; 569 goto out; 570 } 571 572 sclog(sc, "state_change to %d\n", sk->sk_state); 573 574 state_change = sc->sc_state_change; 575 576 switch(sk->sk_state) { 577 /* ignore connecting sockets as they make progress */ 578 case TCP_SYN_SENT: 579 case TCP_SYN_RECV: 580 break; 581 case TCP_ESTABLISHED: 582 o2net_sc_queue_work(sc, &sc->sc_connect_work); 583 break; 584 default: 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); 589 break; 590 } 591out: 592 read_unlock(&sk->sk_callback_lock); 593 state_change(sk); 594} 595 596/* 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(). 600 */ 601static void o2net_register_callbacks(struct sock *sk, 602 struct o2net_sock_container *sc) 603{ 604 write_lock_bh(&sk->sk_callback_lock); 605 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; 610 } 611 612 BUG_ON(sk->sk_user_data != NULL); 613 sk->sk_user_data = sc; 614 sc_get(sc); 615 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; 620 621 mutex_init(&sc->sc_send_lock); 622 623 write_unlock_bh(&sk->sk_callback_lock); 624} 625 626static int o2net_unregister_callbacks(struct sock *sk, 627 struct o2net_sock_container *sc) 628{ 629 int ret = 0; 630 631 write_lock_bh(&sk->sk_callback_lock); 632 if (sk->sk_user_data == sc) { 633 ret = 1; 634 sk->sk_user_data = NULL; 635 sk->sk_data_ready = sc->sc_data_ready; 636 sk->sk_state_change = sc->sc_state_change; 637 } 638 write_unlock_bh(&sk->sk_callback_lock); 639 640 return ret; 641} 642 643/* 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. 648 */ 649static void o2net_ensure_shutdown(struct o2net_node *nn, 650 struct o2net_sock_container *sc, 651 int err) 652{ 653 spin_lock(&nn->nn_lock); 654 if (nn->nn_sc == sc) 655 o2net_set_nn_state(nn, NULL, 0, err); 656 spin_unlock(&nn->nn_lock); 657} 658 659/* 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 665 * itself. 666 */ 667static void o2net_shutdown_sc(struct work_struct *work) 668{ 669 struct o2net_sock_container *sc = 670 container_of(work, struct o2net_sock_container, 671 sc_shutdown_work); 672 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 673 674 sclog(sc, "shutting down\n"); 675 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); 682 sc_put(sc); 683 kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR); 684 } 685 686 /* not fatal so failed connects before the other guy has our 687 * heartbeat can be retried */ 688 o2net_ensure_shutdown(nn, sc, 0); 689 sc_put(sc); 690} 691 692/* ------------------------------------------------------------ */ 693 694static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type, 695 u32 key) 696{ 697 int ret = memcmp(&nmh->nh_key, &key, sizeof(key)); 698 699 if (ret == 0) 700 ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type)); 701 702 return ret; 703} 704 705static struct o2net_msg_handler * 706o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p, 707 struct rb_node **ret_parent) 708{ 709 struct rb_node **p = &o2net_handler_tree.rb_node; 710 struct rb_node *parent = NULL; 711 struct o2net_msg_handler *nmh, *ret = NULL; 712 int cmp; 713 714 while (*p) { 715 parent = *p; 716 nmh = rb_entry(parent, struct o2net_msg_handler, nh_node); 717 cmp = o2net_handler_cmp(nmh, msg_type, key); 718 719 if (cmp < 0) 720 p = &(*p)->rb_left; 721 else if (cmp > 0) 722 p = &(*p)->rb_right; 723 else { 724 ret = nmh; 725 break; 726 } 727 } 728 729 if (ret_p != NULL) 730 *ret_p = p; 731 if (ret_parent != NULL) 732 *ret_parent = parent; 733 734 return ret; 735} 736 737static void o2net_handler_kref_release(struct kref *kref) 738{ 739 struct o2net_msg_handler *nmh; 740 nmh = container_of(kref, struct o2net_msg_handler, nh_kref); 741 742 kfree(nmh); 743} 744 745static void o2net_handler_put(struct o2net_msg_handler *nmh) 746{ 747 kref_put(&nmh->nh_kref, o2net_handler_kref_release); 748} 749 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. */ 752int 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) 756{ 757 struct o2net_msg_handler *nmh = NULL; 758 struct rb_node **p, *parent; 759 int ret = 0; 760 761 if (max_len > O2NET_MAX_PAYLOAD_BYTES) { 762 mlog(0, "max_len for message handler out of range: %u\n", 763 max_len); 764 ret = -EINVAL; 765 goto out; 766 } 767 768 if (!msg_type) { 769 mlog(0, "no message type provided: %u, %p\n", msg_type, func); 770 ret = -EINVAL; 771 goto out; 772 773 } 774 if (!func) { 775 mlog(0, "no message handler provided: %u, %p\n", 776 msg_type, func); 777 ret = -EINVAL; 778 goto out; 779 } 780 781 nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS); 782 if (nmh == NULL) { 783 ret = -ENOMEM; 784 goto out; 785 } 786 787 nmh->nh_func = func; 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; 792 nmh->nh_key = key; 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); 797 798 write_lock(&o2net_handler_lock); 799 if (o2net_handler_tree_lookup(msg_type, key, &p, &parent)) 800 ret = -EEXIST; 801 else { 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); 805 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, 810 &parent) == NULL, 811 "couldn't find handler we *just* registerd " 812 "for type %u key %08x\n", msg_type, key); 813 } 814 write_unlock(&o2net_handler_lock); 815 if (ret) 816 goto out; 817 818out: 819 if (ret) 820 kfree(nmh); 821 822 return ret; 823} 824EXPORT_SYMBOL_GPL(o2net_register_handler); 825 826void o2net_unregister_handler_list(struct list_head *list) 827{ 828 struct o2net_msg_handler *nmh, *n; 829 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); 837 } 838 write_unlock(&o2net_handler_lock); 839} 840EXPORT_SYMBOL_GPL(o2net_unregister_handler_list); 841 842static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key) 843{ 844 struct o2net_msg_handler *nmh; 845 846 read_lock(&o2net_handler_lock); 847 nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL); 848 if (nmh) 849 kref_get(&nmh->nh_kref); 850 read_unlock(&o2net_handler_lock); 851 852 return nmh; 853} 854 855/* ------------------------------------------------------------ */ 856 857static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len) 858{ 859 int ret; 860 mm_segment_t oldfs; 861 struct kvec vec = { 862 .iov_len = len, 863 .iov_base = data, 864 }; 865 struct msghdr msg = { 866 .msg_iovlen = 1, 867 .msg_iov = (struct iovec *)&vec, 868 .msg_flags = MSG_DONTWAIT, 869 }; 870 871 oldfs = get_fs(); 872 set_fs(get_ds()); 873 ret = sock_recvmsg(sock, &msg, len, msg.msg_flags); 874 set_fs(oldfs); 875 876 return ret; 877} 878 879static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec, 880 size_t veclen, size_t total) 881{ 882 int ret; 883 mm_segment_t oldfs; 884 struct msghdr msg = { 885 .msg_iov = (struct iovec *)vec, 886 .msg_iovlen = veclen, 887 }; 888 889 if (sock == NULL) { 890 ret = -EINVAL; 891 goto out; 892 } 893 894 oldfs = get_fs(); 895 set_fs(get_ds()); 896 ret = sock_sendmsg(sock, &msg, total); 897 set_fs(oldfs); 898 if (ret != total) { 899 mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, 900 total); 901 if (ret >= 0) 902 ret = -EPIPE; /* should be smarter, I bet */ 903 goto out; 904 } 905 906 ret = 0; 907out: 908 if (ret < 0) 909 mlog(0, "returning error: %d\n", ret); 910 return ret; 911} 912 913static void o2net_sendpage(struct o2net_sock_container *sc, 914 void *kmalloced_virt, 915 size_t size) 916{ 917 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 918 ssize_t ret; 919 920 while (1) { 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, 925 size, MSG_DONTWAIT); 926 mutex_unlock(&sc->sc_send_lock); 927 if (ret == size) 928 break; 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)); 932 cond_resched(); 933 continue; 934 } 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); 938 break; 939 } 940} 941 942static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key) 943{ 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); 949 msg->status = 0; 950 msg->key = cpu_to_be32(key); 951} 952 953static int o2net_tx_can_proceed(struct o2net_node *nn, 954 struct o2net_sock_container **sc_ret, 955 int *error) 956{ 957 int ret = 0; 958 959 spin_lock(&nn->nn_lock); 960 if (nn->nn_persistent_error) { 961 ret = 1; 962 *sc_ret = NULL; 963 *error = nn->nn_persistent_error; 964 } else if (nn->nn_sc_valid) { 965 kref_get(&nn->nn_sc->sc_kref); 966 967 ret = 1; 968 *sc_ret = nn->nn_sc; 969 *error = 0; 970 } 971 spin_unlock(&nn->nn_lock); 972 973 return ret; 974} 975 976int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec, 977 size_t caller_veclen, u8 target_node, int *status) 978{ 979 int ret = 0; 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), 987 }; 988 struct o2net_send_tracking nst; 989 990 o2net_init_nst(&nst, msg_type, key, current, target_node); 991 992 if (o2net_wq == NULL) { 993 mlog(0, "attempt to tx without o2netd running\n"); 994 ret = -ESRCH; 995 goto out; 996 } 997 998 if (caller_veclen == 0) { 999 mlog(0, "bad kvec array length\n"); 1000 ret = -EINVAL; 1001 goto out; 1002 } 1003 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); 1007 ret = -EINVAL; 1008 goto out; 1009 } 1010 1011 if (target_node == o2nm_this_node()) { 1012 ret = -ELOOP; 1013 goto out; 1014 } 1015 1016 o2net_debug_add_nst(&nst); 1017 1018 o2net_set_nst_sock_time(&nst); 1019 1020 wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret)); 1021 if (ret) 1022 goto out; 1023 1024 o2net_set_nst_sock_container(&nst, sc); 1025 1026 veclen = caller_veclen + 1; 1027 vec = kmalloc(sizeof(struct kvec) * veclen, GFP_ATOMIC); 1028 if (vec == NULL) { 1029 mlog(0, "failed to %zu element kvec!\n", veclen); 1030 ret = -ENOMEM; 1031 goto out; 1032 } 1033 1034 msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC); 1035 if (!msg) { 1036 mlog(0, "failed to allocate a o2net_msg!\n"); 1037 ret = -ENOMEM; 1038 goto out; 1039 } 1040 1041 o2net_init_msg(msg, caller_bytes, msg_type, key); 1042 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)); 1046 1047 ret = o2net_prep_nsw(nn, &nsw); 1048 if (ret) 1049 goto out; 1050 1051 msg->msg_num = cpu_to_be32(nsw.ns_id); 1052 o2net_set_nst_msg_id(&nst, nsw.ns_id); 1053 1054 o2net_set_nst_send_time(&nst); 1055 1056 /* finally, convert the message header to network byte-order 1057 * and send */ 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); 1063 if (ret < 0) { 1064 mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret); 1065 goto out; 1066 } 1067 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)); 1071 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); 1076 if (status && !ret) 1077 *status = nsw.ns_status; 1078 1079 mlog(0, "woken, returning system status %d, user status %d\n", 1080 ret, nsw.ns_status); 1081out: 1082 o2net_debug_del_nst(&nst); /* must be before dropping sc and node */ 1083 if (sc) 1084 sc_put(sc); 1085 if (vec) 1086 kfree(vec); 1087 if (msg) 1088 kfree(msg); 1089 o2net_complete_nsw(nn, &nsw, 0, 0, 0); 1090 return ret; 1091} 1092EXPORT_SYMBOL_GPL(o2net_send_message_vec); 1093 1094int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len, 1095 u8 target_node, int *status) 1096{ 1097 struct kvec vec = { 1098 .iov_base = data, 1099 .iov_len = len, 1100 }; 1101 return o2net_send_message_vec(msg_type, key, &vec, 1, 1102 target_node, status); 1103} 1104EXPORT_SYMBOL_GPL(o2net_send_message); 1105 1106static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr, 1107 enum o2net_system_error syserr, int err) 1108{ 1109 struct kvec vec = { 1110 .iov_base = hdr, 1111 .iov_len = sizeof(struct o2net_msg), 1112 }; 1113 1114 BUG_ON(syserr >= O2NET_ERR_MAX); 1115 1116 /* leave other fields intact from the incoming message, msg_num 1117 * in particular */ 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 1121 hdr->data_len = 0; 1122 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)); 1126} 1127 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 */ 1130static int o2net_process_message(struct o2net_sock_container *sc, 1131 struct o2net_msg *hdr) 1132{ 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; 1138 1139 msglog(hdr, "processing message\n"); 1140 1141 o2net_sc_postpone_idle(sc); 1142 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)); 1150 goto out; 1151 case O2NET_MSG_KEEP_REQ_MAGIC: 1152 o2net_sendpage(sc, o2net_keep_resp, 1153 sizeof(*o2net_keep_resp)); 1154 goto out; 1155 case O2NET_MSG_KEEP_RESP_MAGIC: 1156 goto out; 1157 case O2NET_MSG_MAGIC: 1158 break; 1159 default: 1160 msglog(hdr, "bad magic\n"); 1161 ret = -EINVAL; 1162 goto out; 1163 break; 1164 } 1165 1166 /* find a handler for it */ 1167 handler_status = 0; 1168 nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type), 1169 be32_to_cpu(hdr->key)); 1170 if (!nmh) { 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; 1174 goto out_respond; 1175 } 1176 1177 syserr = O2NET_ERR_NONE; 1178 1179 if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len) 1180 syserr = O2NET_ERR_OVERFLOW; 1181 1182 if (syserr != O2NET_ERR_NONE) 1183 goto out_respond; 1184 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); 1192 1193out_respond: 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, 1197 handler_status); 1198 mutex_unlock(&sc->sc_send_lock); 1199 hdr = NULL; 1200 mlog(0, "sending handler status %d, syserr %d returned %d\n", 1201 handler_status, syserr, ret); 1202 1203 if (nmh) { 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, 1207 ret_data); 1208 } 1209 1210out: 1211 if (nmh) 1212 o2net_handler_put(nmh); 1213 return ret; 1214} 1215 1216static int o2net_check_handshake(struct o2net_sock_container *sc) 1217{ 1218 struct o2net_handshake *hand = page_address(sc->sc_page); 1219 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 1220 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", 1224 SC_NODEF_ARGS(sc), 1225 (unsigned long long)be64_to_cpu(hand->protocol_version), 1226 O2NET_PROTOCOL_VERSION); 1227 1228 /* don't bother reconnecting if its the wrong version. */ 1229 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1230 return -1; 1231 } 1232 1233 /* 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. 1237 */ 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", 1242 SC_NODEF_ARGS(sc), 1243 be32_to_cpu(hand->o2net_idle_timeout_ms), 1244 o2net_idle_timeout()); 1245 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1246 return -1; 1247 } 1248 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", 1253 SC_NODEF_ARGS(sc), 1254 be32_to_cpu(hand->o2net_keepalive_delay_ms), 1255 o2net_keepalive_delay()); 1256 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1257 return -1; 1258 } 1259 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", 1264 SC_NODEF_ARGS(sc), 1265 be32_to_cpu(hand->o2hb_heartbeat_timeout_ms), 1266 O2HB_MAX_WRITE_TIMEOUT_MS); 1267 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1268 return -1; 1269 } 1270 1271 sc->sc_handshake_ok = 1; 1272 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); 1280 } 1281 spin_unlock(&nn->nn_lock); 1282 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); 1287 1288 return 0; 1289} 1290 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.*/ 1294static int o2net_advance_rx(struct o2net_sock_container *sc) 1295{ 1296 struct o2net_msg *hdr; 1297 int ret = 0; 1298 void *data; 1299 size_t datalen; 1300 1301 sclog(sc, "receiving\n"); 1302 do_gettimeofday(&sc->sc_tv_advance_start); 1303 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); 1309 if (ret > 0) 1310 sc->sc_page_off += ret; 1311 } 1312 1313 if (sc->sc_page_off == sizeof(struct o2net_handshake)) { 1314 o2net_check_handshake(sc); 1315 if (unlikely(sc->sc_handshake_ok == 0)) 1316 ret = -EPROTO; 1317 } 1318 goto out; 1319 } 1320 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); 1326 if (ret > 0) { 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) 1335 ret = -EOVERFLOW; 1336 } 1337 } 1338 if (ret <= 0) 1339 goto out; 1340 } 1341 1342 if (sc->sc_page_off < sizeof(struct o2net_msg)) { 1343 /* oof, still don't have a header */ 1344 goto out; 1345 } 1346 1347 /* this was swabbed above when we first read it */ 1348 hdr = page_address(sc->sc_page); 1349 1350 msglog(hdr, "at page_off %zu\n", sc->sc_page_off); 1351 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)) - 1357 sc->sc_page_off; 1358 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); 1359 if (ret > 0) 1360 sc->sc_page_off += ret; 1361 if (ret <= 0) 1362 goto out; 1363 } 1364 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); 1370 if (ret == 0) 1371 ret = 1; 1372 sc->sc_page_off = 0; 1373 } 1374 1375out: 1376 sclog(sc, "ret = %d\n", ret); 1377 do_gettimeofday(&sc->sc_tv_advance_stop); 1378 return ret; 1379} 1380 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. */ 1384static void o2net_rx_until_empty(struct work_struct *work) 1385{ 1386 struct o2net_sock_container *sc = 1387 container_of(work, struct o2net_sock_container, sc_rx_work); 1388 int ret; 1389 1390 do { 1391 ret = o2net_advance_rx(sc); 1392 } while (ret > 0); 1393 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); 1399 } 1400 1401 sc_put(sc); 1402} 1403 1404static int o2net_set_nodelay(struct socket *sock) 1405{ 1406 int ret, val = 1; 1407 mm_segment_t oldfs; 1408 1409 oldfs = get_fs(); 1410 set_fs(KERNEL_DS); 1411 1412 /* 1413 * Dear unsuspecting programmer, 1414 * 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. 1418 * 1419 * Yours, 1420 * Keeper of hilariously fragile interfaces. 1421 */ 1422 ret = sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY, 1423 (char __user *)&val, sizeof(val)); 1424 1425 set_fs(oldfs); 1426 return ret; 1427} 1428 1429static void o2net_initialize_handshake(void) 1430{ 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()); 1438} 1439 1440/* ------------------------------------------------------------ */ 1441 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 */ 1444static void o2net_sc_connect_completed(struct work_struct *work) 1445{ 1446 struct o2net_sock_container *sc = 1447 container_of(work, struct o2net_sock_container, 1448 sc_connect_work); 1449 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)); 1453 1454 o2net_initialize_handshake(); 1455 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand)); 1456 sc_put(sc); 1457} 1458 1459/* this is called as a work_struct func. */ 1460static void o2net_sc_send_keep_req(struct work_struct *work) 1461{ 1462 struct o2net_sock_container *sc = 1463 container_of(work, struct o2net_sock_container, 1464 sc_keepalive_work.work); 1465 1466 o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req)); 1467 sc_put(sc); 1468} 1469 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 */ 1473static void o2net_idle_timer(unsigned long data) 1474{ 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); 1477 struct timeval now; 1478 1479 do_gettimeofday(&now); 1480 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); 1498 1499 /* 1500 * Initialize the nn_timeout so that the next connection attempt 1501 * will continue in o2net_start_connect. 1502 */ 1503 atomic_set(&nn->nn_timeout, 1); 1504 1505 o2net_sc_queue_work(sc, &sc->sc_shutdown_work); 1506} 1507 1508static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc) 1509{ 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())); 1516} 1517 1518static void o2net_sc_postpone_idle(struct o2net_sock_container *sc) 1519{ 1520 /* Only push out an existing timer */ 1521 if (timer_pending(&sc->sc_idle_timeout)) 1522 o2net_sc_reset_idle_timer(sc); 1523} 1524 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 */ 1530static void o2net_start_connect(struct work_struct *work) 1531{ 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, }; 1538 int ret = 0, stop; 1539 unsigned int timeout; 1540 1541 /* if we're greater we initiate tx, otherwise we accept */ 1542 if (o2nm_this_node() <= o2net_num_from_nn(nn)) 1543 goto out; 1544 1545 /* watch for racing with tearing a node down */ 1546 node = o2nm_get_node_by_num(o2net_num_from_nn(nn)); 1547 if (node == NULL) { 1548 ret = 0; 1549 goto out; 1550 } 1551 1552 mynode = o2nm_get_node_by_num(o2nm_this_node()); 1553 if (mynode == NULL) { 1554 ret = 0; 1555 goto out; 1556 } 1557 1558 spin_lock(&nn->nn_lock); 1559 /* 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. 1565 */ 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); 1571 if (stop) 1572 goto out; 1573 1574 nn->nn_last_connect_attempt = jiffies; 1575 1576 sc = sc_alloc(node); 1577 if (sc == NULL) { 1578 mlog(0, "couldn't allocate sc\n"); 1579 ret = -ENOMEM; 1580 goto out; 1581 } 1582 1583 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); 1584 if (ret < 0) { 1585 mlog(0, "can't create socket: %d\n", ret); 1586 goto out; 1587 } 1588 sc->sc_sock = sock; /* freed by sc_kref_release */ 1589 1590 sock->sk->sk_allocation = GFP_ATOMIC; 1591 1592 myaddr.sin_family = AF_INET; 1593 myaddr.sin_addr.s_addr = mynode->nd_ipv4_address; 1594 myaddr.sin_port = htons(0); /* any port */ 1595 1596 ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr, 1597 sizeof(myaddr)); 1598 if (ret) { 1599 mlog(ML_ERROR, "bind failed with %d at address %pI4\n", 1600 ret, &mynode->nd_ipv4_address); 1601 goto out; 1602 } 1603 1604 ret = o2net_set_nodelay(sc->sc_sock); 1605 if (ret) { 1606 mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret); 1607 goto out; 1608 } 1609 1610 o2net_register_callbacks(sc->sc_sock->sk, sc); 1611 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); 1616 1617 remoteaddr.sin_family = AF_INET; 1618 remoteaddr.sin_addr.s_addr = node->nd_ipv4_address; 1619 remoteaddr.sin_port = node->nd_ipv4_port; 1620 1621 ret = sc->sc_sock->ops->connect(sc->sc_sock, 1622 (struct sockaddr *)&remoteaddr, 1623 sizeof(remoteaddr), 1624 O_NONBLOCK); 1625 if (ret == -EINPROGRESS) 1626 ret = 0; 1627 1628out: 1629 if (ret) { 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 */ 1634 if (sc) 1635 o2net_ensure_shutdown(nn, sc, 0); 1636 } 1637 if (sc) 1638 sc_put(sc); 1639 if (node) 1640 o2nm_node_put(node); 1641 if (mynode) 1642 o2nm_node_put(mynode); 1643 1644 return; 1645} 1646 1647static void o2net_connect_expired(struct work_struct *work) 1648{ 1649 struct o2net_node *nn = 1650 container_of(work, struct o2net_node, nn_connect_expired.work); 1651 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); 1659 1660 o2net_set_nn_state(nn, NULL, 0, -ENOTCONN); 1661 } 1662 spin_unlock(&nn->nn_lock); 1663} 1664 1665static void o2net_still_up(struct work_struct *work) 1666{ 1667 struct o2net_node *nn = 1668 container_of(work, struct o2net_node, nn_still_up.work); 1669 1670 o2quo_hb_still_up(o2net_num_from_nn(nn)); 1671} 1672 1673/* ------------------------------------------------------------ */ 1674 1675void o2net_disconnect_node(struct o2nm_node *node) 1676{ 1677 struct o2net_node *nn = o2net_nn_from_num(node->nd_num); 1678 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); 1684 1685 if (o2net_wq) { 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); 1690 } 1691} 1692 1693static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num, 1694 void *data) 1695{ 1696 o2quo_hb_down(node_num); 1697 1698 if (node_num != o2nm_this_node()) 1699 o2net_disconnect_node(node); 1700 1701 BUG_ON(atomic_read(&o2net_connected_peers) < 0); 1702} 1703 1704static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num, 1705 void *data) 1706{ 1707 struct o2net_node *nn = o2net_nn_from_num(node_num); 1708 1709 o2quo_hb_up(node_num); 1710 1711 /* ensure an immediate connect attempt */ 1712 nn->nn_last_connect_attempt = jiffies - 1713 (msecs_to_jiffies(o2net_reconnect_delay()) + 1); 1714 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); 1725 } 1726} 1727 1728void o2net_unregister_hb_callbacks(void) 1729{ 1730 o2hb_unregister_callback(NULL, &o2net_hb_up); 1731 o2hb_unregister_callback(NULL, &o2net_hb_down); 1732} 1733 1734int o2net_register_hb_callbacks(void) 1735{ 1736 int ret; 1737 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); 1742 1743 ret = o2hb_register_callback(NULL, &o2net_hb_up); 1744 if (ret == 0) 1745 ret = o2hb_register_callback(NULL, &o2net_hb_down); 1746 1747 if (ret) 1748 o2net_unregister_hb_callbacks(); 1749 1750 return ret; 1751} 1752 1753/* ------------------------------------------------------------ */ 1754 1755static int o2net_accept_one(struct socket *sock) 1756{ 1757 int ret, slen; 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; 1764 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); 1768 if (ret) 1769 goto out; 1770 1771 new_sock->type = sock->type; 1772 new_sock->ops = sock->ops; 1773 ret = sock->ops->accept(sock, new_sock, O_NONBLOCK); 1774 if (ret < 0) 1775 goto out; 1776 1777 new_sock->sk->sk_allocation = GFP_ATOMIC; 1778 1779 ret = o2net_set_nodelay(new_sock); 1780 if (ret) { 1781 mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret); 1782 goto out; 1783 } 1784 1785 slen = sizeof(sin); 1786 ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin, 1787 &slen, 1); 1788 if (ret < 0) 1789 goto out; 1790 1791 node = o2nm_get_node_by_ip(sin.sin_addr.s_addr); 1792 if (node == NULL) { 1793 mlog(ML_NOTICE, "attempt to connect from unknown node at %pI4:%d\n", 1794 &sin.sin_addr.s_addr, ntohs(sin.sin_port)); 1795 ret = -EINVAL; 1796 goto out; 1797 } 1798 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)); 1808 ret = -EINVAL; 1809 goto out; 1810 } 1811 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)); 1819 ret = -EINVAL; 1820 goto out; 1821 } 1822 1823 nn = o2net_nn_from_num(node->nd_num); 1824 1825 spin_lock(&nn->nn_lock); 1826 if (nn->nn_sc) 1827 ret = -EBUSY; 1828 else 1829 ret = 0; 1830 spin_unlock(&nn->nn_lock); 1831 if (ret) { 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)); 1836 goto out; 1837 } 1838 1839 sc = sc_alloc(node); 1840 if (sc == NULL) { 1841 ret = -ENOMEM; 1842 goto out; 1843 } 1844 1845 sc->sc_sock = new_sock; 1846 new_sock = NULL; 1847 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); 1852 1853 o2net_register_callbacks(sc->sc_sock->sk, sc); 1854 o2net_sc_queue_work(sc, &sc->sc_rx_work); 1855 1856 o2net_initialize_handshake(); 1857 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand)); 1858 1859out: 1860 if (new_sock) 1861 sock_release(new_sock); 1862 if (node) 1863 o2nm_node_put(node); 1864 if (local_node) 1865 o2nm_node_put(local_node); 1866 if (sc) 1867 sc_put(sc); 1868 return ret; 1869} 1870 1871static void o2net_accept_many(struct work_struct *work) 1872{ 1873 struct socket *sock = o2net_listen_sock; 1874 while (o2net_accept_one(sock) == 0) 1875 cond_resched(); 1876} 1877 1878static void o2net_listen_data_ready(struct sock *sk, int bytes) 1879{ 1880 void (*ready)(struct sock *sk, int bytes); 1881 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; 1886 goto out; 1887 } 1888 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 1892 * socket */ 1893 if (sk->sk_state == TCP_LISTEN) { 1894 mlog(ML_TCP, "bytes: %d\n", bytes); 1895 queue_work(o2net_wq, &o2net_listen_work); 1896 } 1897 1898out: 1899 read_unlock(&sk->sk_callback_lock); 1900 ready(sk, bytes); 1901} 1902 1903static int o2net_open_listening_sock(__be32 addr, __be16 port) 1904{ 1905 struct socket *sock = NULL; 1906 int ret; 1907 struct sockaddr_in sin = { 1908 .sin_family = PF_INET, 1909 .sin_addr = { .s_addr = addr }, 1910 .sin_port = port, 1911 }; 1912 1913 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); 1914 if (ret < 0) { 1915 mlog(ML_ERROR, "unable to create socket, ret=%d\n", ret); 1916 goto out; 1917 } 1918 1919 sock->sk->sk_allocation = GFP_ATOMIC; 1920 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); 1925 1926 o2net_listen_sock = sock; 1927 INIT_WORK(&o2net_listen_work, o2net_accept_many); 1928 1929 sock->sk->sk_reuse = 1; 1930 ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin)); 1931 if (ret < 0) { 1932 mlog(ML_ERROR, "unable to bind socket at %pI4:%u, " 1933 "ret=%d\n", &addr, ntohs(port), ret); 1934 goto out; 1935 } 1936 1937 ret = sock->ops->listen(sock, 64); 1938 if (ret < 0) { 1939 mlog(ML_ERROR, "unable to listen on %pI4:%u, ret=%d\n", 1940 &addr, ntohs(port), ret); 1941 } 1942 1943out: 1944 if (ret) { 1945 o2net_listen_sock = NULL; 1946 if (sock) 1947 sock_release(sock); 1948 } 1949 return ret; 1950} 1951 1952/* 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 1957 * is being set up. 1958 */ 1959int o2net_start_listening(struct o2nm_node *node) 1960{ 1961 int ret = 0; 1962 1963 BUG_ON(o2net_wq != NULL); 1964 BUG_ON(o2net_listen_sock != NULL); 1965 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; /* ? */ 1971 } 1972 1973 ret = o2net_open_listening_sock(node->nd_ipv4_address, 1974 node->nd_ipv4_port); 1975 if (ret) { 1976 destroy_workqueue(o2net_wq); 1977 o2net_wq = NULL; 1978 } else 1979 o2quo_conn_up(node->nd_num); 1980 1981 return ret; 1982} 1983 1984/* again, o2nm_this_node() doesn't work here as we're involved in 1985 * tearing it down */ 1986void o2net_stop_listening(struct o2nm_node *node) 1987{ 1988 struct socket *sock = o2net_listen_sock; 1989 size_t i; 1990 1991 BUG_ON(o2net_wq == NULL); 1992 BUG_ON(o2net_listen_sock == NULL); 1993 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); 1999 2000 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) { 2001 struct o2nm_node *node = o2nm_get_node_by_num(i); 2002 if (node) { 2003 o2net_disconnect_node(node); 2004 o2nm_node_put(node); 2005 } 2006 } 2007 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); 2011 o2net_wq = NULL; 2012 2013 sock_release(o2net_listen_sock); 2014 o2net_listen_sock = NULL; 2015 2016 o2quo_conn_err(node->nd_num); 2017} 2018 2019/* ------------------------------------------------------------ */ 2020 2021int o2net_init(void) 2022{ 2023 unsigned long i; 2024 2025 o2quo_init(); 2026 2027 if (o2net_debugfs_init()) 2028 return -ENOMEM; 2029 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) { 2034 kfree(o2net_hand); 2035 kfree(o2net_keep_req); 2036 kfree(o2net_keep_resp); 2037 return -ENOMEM; 2038 } 2039 2040 o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION); 2041 o2net_hand->connector_id = cpu_to_be64(1); 2042 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); 2045 2046 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) { 2047 struct o2net_node *nn = o2net_nn_from_num(i); 2048 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); 2060 } 2061 2062 return 0; 2063} 2064 2065void o2net_exit(void) 2066{ 2067 o2quo_exit(); 2068 kfree(o2net_hand); 2069 kfree(o2net_keep_req); 2070 kfree(o2net_keep_resp); 2071 o2net_debugfs_exit(); 2072} 2073