1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Copyright (C) 2004 Oracle. All rights reserved. 5 * 6 * ---- 7 * 8 * Callers for this were originally written against a very simple synchronous 9 * API. This implementation reflects those simple callers. Some day I'm sure 10 * we'll need to move to a more robust posting/callback mechanism. 11 * 12 * Transmit calls pass in kernel virtual addresses and block copying this into 13 * the socket's tx buffers via a usual blocking sendmsg. They'll block waiting 14 * for a failed socket to timeout. TX callers can also pass in a pointer to an 15 * 'int' which gets filled with an errno off the wire in response to the 16 * message they send. 17 * 18 * Handlers for unsolicited messages are registered. Each socket has a page 19 * that incoming data is copied into. First the header, then the data. 20 * Handlers are called from only one thread with a reference to this per-socket 21 * page. This page is destroyed after the handler call, so it can't be 22 * referenced beyond the call. Handlers may block but are discouraged from 23 * doing so. 24 * 25 * Any framing errors (bad magic, large payload lengths) close a connection. 26 * 27 * Our sock_container holds the state we associate with a socket. It's current 28 * framing state is held there as well as the refcounting we do around when it 29 * is safe to tear down the socket. The socket is only finally torn down from 30 * the container when the container loses all of its references -- so as long 31 * as you hold a ref on the container you can trust that the socket is valid 32 * for use with kernel socket APIs. 33 * 34 * Connections are initiated between a pair of nodes when the node with the 35 * higher node number gets a heartbeat callback which indicates that the lower 36 * numbered node has started heartbeating. The lower numbered node is passive 37 * and only accepts the connection if the higher numbered node is heartbeating. 38 */ 39 40 #include <linux/kernel.h> 41 #include <linux/completion.h> 42 #include <linux/mutex.h> 43 #include <linux/sched/mm.h> 44 #include <linux/jiffies.h> 45 #include <linux/slab.h> 46 #include <linux/idr.h> 47 #include <linux/kref.h> 48 #include <linux/net.h> 49 #include <linux/export.h> 50 #include <net/tcp.h> 51 #include <trace/events/sock.h> 52 53 #include <linux/uaccess.h> 54 55 #include "heartbeat.h" 56 #include "tcp.h" 57 #include "nodemanager.h" 58 #define MLOG_MASK_PREFIX ML_TCP 59 #include "masklog.h" 60 #include "quorum.h" 61 62 #include "tcp_internal.h" 63 64 #define SC_NODEF_FMT "node %s (num %u) at %pI4:%u" 65 #define SC_NODEF_ARGS(sc) sc->sc_node->nd_name, sc->sc_node->nd_num, \ 66 &sc->sc_node->nd_ipv4_address, \ 67 ntohs(sc->sc_node->nd_ipv4_port) 68 69 /* 70 * In the following two log macros, the whitespace after the ',' just 71 * before ##args is intentional. Otherwise, gcc 2.95 will eat the 72 * previous token if args expands to nothing. 73 */ 74 #define msglog(hdr, fmt, args...) do { \ 75 typeof(hdr) __hdr = (hdr); \ 76 mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d " \ 77 "key %08x num %u] " fmt, \ 78 be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), \ 79 be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status), \ 80 be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key), \ 81 be32_to_cpu(__hdr->msg_num) , ##args); \ 82 } while (0) 83 84 #define sclog(sc, fmt, args...) do { \ 85 typeof(sc) __sc = (sc); \ 86 mlog(ML_SOCKET, "[sc %p refs %d sock %p node %u page %p " \ 87 "pg_off %zu] " fmt, __sc, \ 88 kref_read(&__sc->sc_kref), __sc->sc_sock, \ 89 __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off , \ 90 ##args); \ 91 } while (0) 92 93 static DEFINE_RWLOCK(o2net_handler_lock); 94 static struct rb_root o2net_handler_tree = RB_ROOT; 95 96 static struct o2net_node o2net_nodes[O2NM_MAX_NODES]; 97 98 /* XXX someday we'll need better accounting */ 99 static struct socket *o2net_listen_sock; 100 101 /* 102 * listen work is only queued by the listening socket callbacks on the 103 * o2net_wq. teardown detaches the callbacks before destroying the workqueue. 104 * quorum work is queued as sock containers are shutdown.. stop_listening 105 * tears down all the node's sock containers, preventing future shutdowns 106 * and queued quorum work, before canceling delayed quorum work and 107 * destroying the work queue. Handler teardown can also race local listener 108 * shutdown, so keep a waitable destroying pointer until the old ordered 109 * queue has finished draining. 110 */ 111 static struct workqueue_struct *o2net_wq; 112 static struct workqueue_struct *o2net_wq_destroying; 113 static DEFINE_MUTEX(o2net_wq_mutex); 114 static DECLARE_COMPLETION(o2net_wq_destroyed); 115 /* Heartbeat callbacks stay registered across local-node off/on. */ 116 static bool o2net_listening; 117 static struct work_struct o2net_listen_work; 118 119 static struct o2hb_callback_func o2net_hb_up, o2net_hb_down; 120 #define O2NET_HB_PRI 0x1 121 122 static struct o2net_handshake *o2net_hand; 123 static struct o2net_msg *o2net_keep_req, *o2net_keep_resp; 124 125 static int o2net_sys_err_translations[O2NET_ERR_MAX] = 126 {[O2NET_ERR_NONE] = 0, 127 [O2NET_ERR_NO_HNDLR] = -ENOPROTOOPT, 128 [O2NET_ERR_OVERFLOW] = -EOVERFLOW, 129 [O2NET_ERR_DIED] = -EHOSTDOWN,}; 130 131 /* can't quite avoid *all* internal declarations :/ */ 132 static void o2net_sc_connect_completed(struct work_struct *work); 133 static void o2net_rx_until_empty(struct work_struct *work); 134 static void o2net_shutdown_sc(struct work_struct *work); 135 static void o2net_listen_data_ready(struct sock *sk); 136 static void o2net_sc_send_keep_req(struct work_struct *work); 137 static void o2net_idle_timer(struct timer_list *t); 138 static void o2net_sc_postpone_idle(struct o2net_sock_container *sc); 139 static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc); 140 141 #ifdef CONFIG_DEBUG_FS 142 static void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype, 143 u32 msgkey, struct task_struct *task, u8 node) 144 { 145 INIT_LIST_HEAD(&nst->st_net_debug_item); 146 nst->st_task = task; 147 nst->st_msg_type = msgtype; 148 nst->st_msg_key = msgkey; 149 nst->st_node = node; 150 } 151 152 static inline void o2net_set_nst_sock_time(struct o2net_send_tracking *nst) 153 { 154 nst->st_sock_time = ktime_get(); 155 } 156 157 static inline void o2net_set_nst_send_time(struct o2net_send_tracking *nst) 158 { 159 nst->st_send_time = ktime_get(); 160 } 161 162 static inline void o2net_set_nst_status_time(struct o2net_send_tracking *nst) 163 { 164 nst->st_status_time = ktime_get(); 165 } 166 167 static inline void o2net_set_nst_sock_container(struct o2net_send_tracking *nst, 168 struct o2net_sock_container *sc) 169 { 170 nst->st_sc = sc; 171 } 172 173 static inline void o2net_set_nst_msg_id(struct o2net_send_tracking *nst, 174 u32 msg_id) 175 { 176 nst->st_id = msg_id; 177 } 178 179 static inline void o2net_set_sock_timer(struct o2net_sock_container *sc) 180 { 181 sc->sc_tv_timer = ktime_get(); 182 } 183 184 static inline void o2net_set_data_ready_time(struct o2net_sock_container *sc) 185 { 186 sc->sc_tv_data_ready = ktime_get(); 187 } 188 189 static inline void o2net_set_advance_start_time(struct o2net_sock_container *sc) 190 { 191 sc->sc_tv_advance_start = ktime_get(); 192 } 193 194 static inline void o2net_set_advance_stop_time(struct o2net_sock_container *sc) 195 { 196 sc->sc_tv_advance_stop = ktime_get(); 197 } 198 199 static inline void o2net_set_func_start_time(struct o2net_sock_container *sc) 200 { 201 sc->sc_tv_func_start = ktime_get(); 202 } 203 204 static inline void o2net_set_func_stop_time(struct o2net_sock_container *sc) 205 { 206 sc->sc_tv_func_stop = ktime_get(); 207 } 208 209 #else /* CONFIG_DEBUG_FS */ 210 # define o2net_init_nst(a, b, c, d, e) 211 # define o2net_set_nst_sock_time(a) 212 # define o2net_set_nst_send_time(a) 213 # define o2net_set_nst_status_time(a) 214 # define o2net_set_nst_sock_container(a, b) 215 # define o2net_set_nst_msg_id(a, b) 216 # define o2net_set_sock_timer(a) 217 # define o2net_set_data_ready_time(a) 218 # define o2net_set_advance_start_time(a) 219 # define o2net_set_advance_stop_time(a) 220 # define o2net_set_func_start_time(a) 221 # define o2net_set_func_stop_time(a) 222 #endif /* CONFIG_DEBUG_FS */ 223 224 #ifdef CONFIG_OCFS2_FS_STATS 225 static ktime_t o2net_get_func_run_time(struct o2net_sock_container *sc) 226 { 227 return ktime_sub(sc->sc_tv_func_stop, sc->sc_tv_func_start); 228 } 229 230 static void o2net_update_send_stats(struct o2net_send_tracking *nst, 231 struct o2net_sock_container *sc) 232 { 233 sc->sc_tv_status_total = ktime_add(sc->sc_tv_status_total, 234 ktime_sub(ktime_get(), 235 nst->st_status_time)); 236 sc->sc_tv_send_total = ktime_add(sc->sc_tv_send_total, 237 ktime_sub(nst->st_status_time, 238 nst->st_send_time)); 239 sc->sc_tv_acquiry_total = ktime_add(sc->sc_tv_acquiry_total, 240 ktime_sub(nst->st_send_time, 241 nst->st_sock_time)); 242 sc->sc_send_count++; 243 } 244 245 static void o2net_update_recv_stats(struct o2net_sock_container *sc) 246 { 247 sc->sc_tv_process_total = ktime_add(sc->sc_tv_process_total, 248 o2net_get_func_run_time(sc)); 249 sc->sc_recv_count++; 250 } 251 252 #else 253 254 # define o2net_update_send_stats(a, b) 255 256 # define o2net_update_recv_stats(sc) 257 258 #endif /* CONFIG_OCFS2_FS_STATS */ 259 260 static inline unsigned int o2net_reconnect_delay(void) 261 { 262 return o2nm_single_cluster->cl_reconnect_delay_ms; 263 } 264 265 static inline unsigned int o2net_keepalive_delay(void) 266 { 267 return o2nm_single_cluster->cl_keepalive_delay_ms; 268 } 269 270 static inline unsigned int o2net_idle_timeout(void) 271 { 272 return o2nm_single_cluster->cl_idle_timeout_ms; 273 } 274 275 static inline int o2net_sys_err_to_errno(enum o2net_system_error err) 276 { 277 int trans; 278 BUG_ON(err >= O2NET_ERR_MAX); 279 trans = o2net_sys_err_translations[err]; 280 281 /* Just in case we mess up the translation table above */ 282 BUG_ON(err != O2NET_ERR_NONE && trans == 0); 283 return trans; 284 } 285 286 static struct o2net_node * o2net_nn_from_num(u8 node_num) 287 { 288 BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes)); 289 return &o2net_nodes[node_num]; 290 } 291 292 static u8 o2net_num_from_nn(struct o2net_node *nn) 293 { 294 BUG_ON(nn == NULL); 295 return nn - o2net_nodes; 296 } 297 298 /* ------------------------------------------------------------ */ 299 300 static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw) 301 { 302 int ret; 303 304 spin_lock(&nn->nn_lock); 305 ret = idr_alloc(&nn->nn_status_idr, nsw, 0, 0, GFP_ATOMIC); 306 if (ret >= 0) { 307 nsw->ns_id = ret; 308 list_add_tail(&nsw->ns_node_item, &nn->nn_status_list); 309 } 310 spin_unlock(&nn->nn_lock); 311 if (ret < 0) 312 return ret; 313 314 init_waitqueue_head(&nsw->ns_wq); 315 nsw->ns_sys_status = O2NET_ERR_NONE; 316 nsw->ns_status = 0; 317 return 0; 318 } 319 320 static void o2net_complete_nsw_locked(struct o2net_node *nn, 321 struct o2net_status_wait *nsw, 322 enum o2net_system_error sys_status, 323 s32 status) 324 { 325 assert_spin_locked(&nn->nn_lock); 326 327 if (!list_empty(&nsw->ns_node_item)) { 328 list_del_init(&nsw->ns_node_item); 329 nsw->ns_sys_status = sys_status; 330 nsw->ns_status = status; 331 idr_remove(&nn->nn_status_idr, nsw->ns_id); 332 wake_up(&nsw->ns_wq); 333 } 334 } 335 336 static void o2net_complete_nsw(struct o2net_node *nn, 337 struct o2net_status_wait *nsw, 338 u64 id, enum o2net_system_error sys_status, 339 s32 status) 340 { 341 spin_lock(&nn->nn_lock); 342 if (nsw == NULL) { 343 if (id > INT_MAX) 344 goto out; 345 346 nsw = idr_find(&nn->nn_status_idr, id); 347 if (nsw == NULL) 348 goto out; 349 } 350 351 o2net_complete_nsw_locked(nn, nsw, sys_status, status); 352 353 out: 354 spin_unlock(&nn->nn_lock); 355 return; 356 } 357 358 static void o2net_complete_nodes_nsw(struct o2net_node *nn) 359 { 360 struct o2net_status_wait *nsw, *tmp; 361 unsigned int num_kills = 0; 362 363 assert_spin_locked(&nn->nn_lock); 364 365 list_for_each_entry_safe(nsw, tmp, &nn->nn_status_list, ns_node_item) { 366 o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0); 367 num_kills++; 368 } 369 370 mlog(0, "completed %d messages for node %u\n", num_kills, 371 o2net_num_from_nn(nn)); 372 } 373 374 static int o2net_nsw_completed(struct o2net_node *nn, 375 struct o2net_status_wait *nsw) 376 { 377 int completed; 378 spin_lock(&nn->nn_lock); 379 completed = list_empty(&nsw->ns_node_item); 380 spin_unlock(&nn->nn_lock); 381 return completed; 382 } 383 384 /* ------------------------------------------------------------ */ 385 386 static void sc_kref_release(struct kref *kref) 387 { 388 struct o2net_sock_container *sc = container_of(kref, 389 struct o2net_sock_container, sc_kref); 390 BUG_ON(timer_pending(&sc->sc_idle_timeout)); 391 392 sclog(sc, "releasing\n"); 393 394 if (sc->sc_sock) { 395 sock_release(sc->sc_sock); 396 sc->sc_sock = NULL; 397 } 398 399 o2nm_undepend_item(&sc->sc_node->nd_item); 400 o2nm_node_put(sc->sc_node); 401 sc->sc_node = NULL; 402 403 o2net_debug_del_sc(sc); 404 405 if (sc->sc_page) 406 __free_page(sc->sc_page); 407 kfree(sc); 408 } 409 410 static void sc_put(struct o2net_sock_container *sc) 411 { 412 sclog(sc, "put\n"); 413 kref_put(&sc->sc_kref, sc_kref_release); 414 } 415 static void sc_get(struct o2net_sock_container *sc) 416 { 417 sclog(sc, "get\n"); 418 kref_get(&sc->sc_kref); 419 } 420 static struct o2net_sock_container *sc_alloc(struct o2nm_node *node) 421 { 422 struct o2net_sock_container *sc, *ret = NULL; 423 struct page *page = NULL; 424 int status = 0; 425 426 page = alloc_page(GFP_NOFS); 427 sc = kzalloc_obj(*sc, GFP_NOFS); 428 if (sc == NULL || page == NULL) 429 goto out; 430 431 kref_init(&sc->sc_kref); 432 o2nm_node_get(node); 433 sc->sc_node = node; 434 435 /* pin the node item of the remote node */ 436 status = o2nm_depend_item(&node->nd_item); 437 if (status) { 438 mlog_errno(status); 439 o2nm_node_put(node); 440 goto out; 441 } 442 INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed); 443 INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty); 444 INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc); 445 INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req); 446 447 timer_setup(&sc->sc_idle_timeout, o2net_idle_timer, 0); 448 449 sclog(sc, "alloced\n"); 450 451 ret = sc; 452 sc->sc_page = page; 453 o2net_debug_add_sc(sc); 454 sc = NULL; 455 page = NULL; 456 457 out: 458 if (page) 459 __free_page(page); 460 kfree(sc); 461 462 return ret; 463 } 464 465 /* ------------------------------------------------------------ */ 466 467 static void o2net_sc_queue_work(struct o2net_sock_container *sc, 468 struct work_struct *work) 469 { 470 sc_get(sc); 471 if (!queue_work(o2net_wq, work)) 472 sc_put(sc); 473 } 474 static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc, 475 struct delayed_work *work, 476 int delay) 477 { 478 sc_get(sc); 479 if (!queue_delayed_work(o2net_wq, work, delay)) 480 sc_put(sc); 481 } 482 static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc, 483 struct delayed_work *work) 484 { 485 if (cancel_delayed_work(work)) 486 sc_put(sc); 487 } 488 489 static atomic_t o2net_connected_peers = ATOMIC_INIT(0); 490 491 int o2net_num_connected_peers(void) 492 { 493 return atomic_read(&o2net_connected_peers); 494 } 495 496 static void o2net_set_nn_state(struct o2net_node *nn, 497 struct o2net_sock_container *sc, 498 unsigned valid, int err) 499 { 500 int was_valid = nn->nn_sc_valid; 501 int was_err = nn->nn_persistent_error; 502 struct o2net_sock_container *old_sc = nn->nn_sc; 503 504 assert_spin_locked(&nn->nn_lock); 505 506 if (old_sc && !sc) 507 atomic_dec(&o2net_connected_peers); 508 else if (!old_sc && sc) 509 atomic_inc(&o2net_connected_peers); 510 511 /* the node num comparison and single connect/accept path should stop 512 * an non-null sc from being overwritten with another */ 513 BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc); 514 mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid); 515 mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc); 516 517 if (was_valid && !valid && err == 0) 518 err = -ENOTCONN; 519 520 mlog(ML_CONN, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n", 521 o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid, 522 nn->nn_persistent_error, err); 523 524 nn->nn_sc = sc; 525 nn->nn_sc_valid = valid ? 1 : 0; 526 nn->nn_persistent_error = err; 527 528 /* mirrors o2net_tx_can_proceed() */ 529 if (nn->nn_persistent_error || nn->nn_sc_valid) 530 wake_up(&nn->nn_sc_wq); 531 532 if (was_valid && !was_err && nn->nn_persistent_error) { 533 o2quo_conn_err(o2net_num_from_nn(nn)); 534 queue_delayed_work(o2net_wq, &nn->nn_still_up, 535 msecs_to_jiffies(O2NET_QUORUM_DELAY_MS)); 536 } 537 538 if (was_valid && !valid) { 539 if (old_sc) 540 printk(KERN_NOTICE "o2net: No longer connected to " 541 SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc)); 542 o2net_complete_nodes_nsw(nn); 543 } 544 545 if (!was_valid && valid) { 546 o2quo_conn_up(o2net_num_from_nn(nn)); 547 cancel_delayed_work(&nn->nn_connect_expired); 548 printk(KERN_NOTICE "o2net: %s " SC_NODEF_FMT "\n", 549 o2nm_this_node() > sc->sc_node->nd_num ? 550 "Connected to" : "Accepted connection from", 551 SC_NODEF_ARGS(sc)); 552 } 553 554 /* trigger the connecting worker func as long as we're not valid, 555 * it will back off if it shouldn't connect. This can be called 556 * from node config teardown and so needs to be careful about 557 * the work queue actually being up. */ 558 if (!valid && o2net_wq) { 559 unsigned long delay; 560 /* delay if we're within a RECONNECT_DELAY of the 561 * last attempt */ 562 delay = (nn->nn_last_connect_attempt + 563 msecs_to_jiffies(o2net_reconnect_delay())) 564 - jiffies; 565 if (delay > msecs_to_jiffies(o2net_reconnect_delay())) 566 delay = 0; 567 mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay); 568 queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay); 569 570 /* 571 * Delay the expired work after idle timeout. 572 * 573 * We might have lots of failed connection attempts that run 574 * through here but we only cancel the connect_expired work when 575 * a connection attempt succeeds. So only the first enqueue of 576 * the connect_expired work will do anything. The rest will see 577 * that it's already queued and do nothing. 578 */ 579 delay += msecs_to_jiffies(o2net_idle_timeout()); 580 queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay); 581 } 582 583 /* keep track of the nn's sc ref for the caller */ 584 if ((old_sc == NULL) && sc) 585 sc_get(sc); 586 if (old_sc && (old_sc != sc)) { 587 o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work); 588 sc_put(old_sc); 589 } 590 } 591 592 /* see o2net_register_callbacks() */ 593 static void o2net_data_ready(struct sock *sk) 594 { 595 void (*ready)(struct sock *sk); 596 struct o2net_sock_container *sc; 597 598 trace_sk_data_ready(sk); 599 600 read_lock_bh(&sk->sk_callback_lock); 601 sc = sk->sk_user_data; 602 if (sc) { 603 sclog(sc, "data_ready hit\n"); 604 o2net_set_data_ready_time(sc); 605 o2net_sc_queue_work(sc, &sc->sc_rx_work); 606 ready = sc->sc_data_ready; 607 } else { 608 ready = sk->sk_data_ready; 609 } 610 read_unlock_bh(&sk->sk_callback_lock); 611 612 ready(sk); 613 } 614 615 /* see o2net_register_callbacks() */ 616 static void o2net_state_change(struct sock *sk) 617 { 618 void (*state_change)(struct sock *sk); 619 struct o2net_sock_container *sc; 620 621 read_lock_bh(&sk->sk_callback_lock); 622 sc = sk->sk_user_data; 623 if (sc == NULL) { 624 state_change = sk->sk_state_change; 625 goto out; 626 } 627 628 sclog(sc, "state_change to %d\n", sk->sk_state); 629 630 state_change = sc->sc_state_change; 631 632 switch(sk->sk_state) { 633 /* ignore connecting sockets as they make progress */ 634 case TCP_SYN_SENT: 635 case TCP_SYN_RECV: 636 break; 637 case TCP_ESTABLISHED: 638 o2net_sc_queue_work(sc, &sc->sc_connect_work); 639 break; 640 default: 641 printk(KERN_INFO "o2net: Connection to " SC_NODEF_FMT 642 " shutdown, state %d\n", 643 SC_NODEF_ARGS(sc), sk->sk_state); 644 o2net_sc_queue_work(sc, &sc->sc_shutdown_work); 645 break; 646 } 647 out: 648 read_unlock_bh(&sk->sk_callback_lock); 649 state_change(sk); 650 } 651 652 /* 653 * we register callbacks so we can queue work on events before calling 654 * the original callbacks. our callbacks our careful to test user_data 655 * to discover when they've reaced with o2net_unregister_callbacks(). 656 */ 657 static void o2net_register_callbacks(struct sock *sk, 658 struct o2net_sock_container *sc) 659 { 660 write_lock_bh(&sk->sk_callback_lock); 661 662 /* accepted sockets inherit the old listen socket data ready */ 663 if (sk->sk_data_ready == o2net_listen_data_ready) { 664 sk->sk_data_ready = sk->sk_user_data; 665 sk->sk_user_data = NULL; 666 } 667 668 BUG_ON(sk->sk_user_data != NULL); 669 sk->sk_user_data = sc; 670 sc_get(sc); 671 672 sc->sc_data_ready = sk->sk_data_ready; 673 sc->sc_state_change = sk->sk_state_change; 674 sk->sk_data_ready = o2net_data_ready; 675 sk->sk_state_change = o2net_state_change; 676 677 mutex_init(&sc->sc_send_lock); 678 679 write_unlock_bh(&sk->sk_callback_lock); 680 } 681 682 static int o2net_unregister_callbacks(struct sock *sk, 683 struct o2net_sock_container *sc) 684 { 685 int ret = 0; 686 687 write_lock_bh(&sk->sk_callback_lock); 688 if (sk->sk_user_data == sc) { 689 ret = 1; 690 sk->sk_user_data = NULL; 691 sk->sk_data_ready = sc->sc_data_ready; 692 sk->sk_state_change = sc->sc_state_change; 693 } 694 write_unlock_bh(&sk->sk_callback_lock); 695 696 return ret; 697 } 698 699 /* 700 * this is a little helper that is called by callers who have seen a problem 701 * with an sc and want to detach it from the nn if someone already hasn't beat 702 * them to it. if an error is given then the shutdown will be persistent 703 * and pending transmits will be canceled. 704 */ 705 static void o2net_ensure_shutdown(struct o2net_node *nn, 706 struct o2net_sock_container *sc, 707 int err) 708 { 709 spin_lock(&nn->nn_lock); 710 if (nn->nn_sc == sc) 711 o2net_set_nn_state(nn, NULL, 0, err); 712 spin_unlock(&nn->nn_lock); 713 } 714 715 /* 716 * This work queue function performs the blocking parts of socket shutdown. A 717 * few paths lead here. set_nn_state will trigger this callback if it sees an 718 * sc detached from the nn. state_change will also trigger this callback 719 * directly when it sees errors. In that case we need to call set_nn_state 720 * ourselves as state_change couldn't get the nn_lock and call set_nn_state 721 * itself. 722 */ 723 static void o2net_shutdown_sc(struct work_struct *work) 724 { 725 struct o2net_sock_container *sc = 726 container_of(work, struct o2net_sock_container, 727 sc_shutdown_work); 728 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 729 730 sclog(sc, "shutting down\n"); 731 732 /* drop the callbacks ref and call shutdown only once */ 733 if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) { 734 /* we shouldn't flush as we're in the thread, the 735 * races with pending sc work structs are harmless */ 736 timer_delete_sync(&sc->sc_idle_timeout); 737 o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work); 738 sc_put(sc); 739 kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR); 740 } 741 742 /* not fatal so failed connects before the other guy has our 743 * heartbeat can be retried */ 744 o2net_ensure_shutdown(nn, sc, 0); 745 sc_put(sc); 746 } 747 748 /* ------------------------------------------------------------ */ 749 750 static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type, 751 u32 key) 752 { 753 int ret = memcmp(&nmh->nh_key, &key, sizeof(key)); 754 755 if (ret == 0) 756 ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type)); 757 758 return ret; 759 } 760 761 static struct o2net_msg_handler * 762 o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p, 763 struct rb_node **ret_parent) 764 { 765 struct rb_node **p = &o2net_handler_tree.rb_node; 766 struct rb_node *parent = NULL; 767 struct o2net_msg_handler *nmh, *ret = NULL; 768 int cmp; 769 770 while (*p) { 771 parent = *p; 772 nmh = rb_entry(parent, struct o2net_msg_handler, nh_node); 773 cmp = o2net_handler_cmp(nmh, msg_type, key); 774 775 if (cmp < 0) 776 p = &(*p)->rb_left; 777 else if (cmp > 0) 778 p = &(*p)->rb_right; 779 else { 780 ret = nmh; 781 break; 782 } 783 } 784 785 if (ret_p != NULL) 786 *ret_p = p; 787 if (ret_parent != NULL) 788 *ret_parent = parent; 789 790 return ret; 791 } 792 793 static void o2net_handler_kref_release(struct kref *kref) 794 { 795 struct o2net_msg_handler *nmh; 796 nmh = container_of(kref, struct o2net_msg_handler, nh_kref); 797 798 kfree(nmh); 799 } 800 801 static void o2net_handler_put(struct o2net_msg_handler *nmh) 802 { 803 kref_put(&nmh->nh_kref, o2net_handler_kref_release); 804 } 805 806 /* max_len is protection for the handler func. incoming messages won't 807 * be given to the handler if their payload is longer than the max. */ 808 int o2net_register_handler(u32 msg_type, u32 key, u32 max_len, 809 o2net_msg_handler_func *func, void *data, 810 o2net_post_msg_handler_func *post_func, 811 struct list_head *unreg_list) 812 { 813 struct o2net_msg_handler *nmh = NULL; 814 struct rb_node **p, *parent; 815 int ret = 0; 816 817 if (max_len > O2NET_MAX_PAYLOAD_BYTES) { 818 mlog(0, "max_len for message handler out of range: %u\n", 819 max_len); 820 ret = -EINVAL; 821 goto out; 822 } 823 824 if (!msg_type) { 825 mlog(0, "no message type provided: %u, %p\n", msg_type, func); 826 ret = -EINVAL; 827 goto out; 828 829 } 830 if (!func) { 831 mlog(0, "no message handler provided: %u, %p\n", 832 msg_type, func); 833 ret = -EINVAL; 834 goto out; 835 } 836 837 nmh = kzalloc_obj(struct o2net_msg_handler, GFP_NOFS); 838 if (nmh == NULL) { 839 ret = -ENOMEM; 840 goto out; 841 } 842 843 nmh->nh_func = func; 844 nmh->nh_func_data = data; 845 nmh->nh_post_func = post_func; 846 nmh->nh_msg_type = msg_type; 847 nmh->nh_max_len = max_len; 848 nmh->nh_key = key; 849 /* the tree and list get this ref.. they're both removed in 850 * unregister when this ref is dropped */ 851 kref_init(&nmh->nh_kref); 852 INIT_LIST_HEAD(&nmh->nh_unregister_item); 853 854 write_lock(&o2net_handler_lock); 855 if (o2net_handler_tree_lookup(msg_type, key, &p, &parent)) 856 ret = -EEXIST; 857 else { 858 rb_link_node(&nmh->nh_node, parent, p); 859 rb_insert_color(&nmh->nh_node, &o2net_handler_tree); 860 list_add_tail(&nmh->nh_unregister_item, unreg_list); 861 862 mlog(ML_TCP, "registered handler func %p type %u key %08x\n", 863 func, msg_type, key); 864 /* we've had some trouble with handlers seemingly vanishing. */ 865 mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p, 866 &parent) == NULL, 867 "couldn't find handler we *just* registered " 868 "for type %u key %08x\n", msg_type, key); 869 } 870 write_unlock(&o2net_handler_lock); 871 872 out: 873 if (ret) 874 kfree(nmh); 875 876 return ret; 877 } 878 EXPORT_SYMBOL_GPL(o2net_register_handler); 879 880 void o2net_unregister_handler_list(struct list_head *list) 881 { 882 struct o2net_msg_handler *nmh, *n; 883 884 write_lock(&o2net_handler_lock); 885 list_for_each_entry_safe(nmh, n, list, nh_unregister_item) { 886 mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n", 887 nmh->nh_func, nmh->nh_msg_type, nmh->nh_key); 888 rb_erase(&nmh->nh_node, &o2net_handler_tree); 889 list_del_init(&nmh->nh_unregister_item); 890 kref_put(&nmh->nh_kref, o2net_handler_kref_release); 891 } 892 write_unlock(&o2net_handler_lock); 893 } 894 EXPORT_SYMBOL_GPL(o2net_unregister_handler_list); 895 896 static void o2net_flush_wq(void) 897 { 898 mutex_lock(&o2net_wq_mutex); 899 if (o2net_wq_destroying) { 900 mutex_unlock(&o2net_wq_mutex); 901 wait_for_completion(&o2net_wq_destroyed); 902 return; 903 } 904 905 if (o2net_wq) 906 flush_workqueue(o2net_wq); 907 mutex_unlock(&o2net_wq_mutex); 908 } 909 910 void o2net_unregister_and_flush_handler_list(struct list_head *list) 911 { 912 o2net_unregister_handler_list(list); 913 o2net_flush_wq(); 914 } 915 EXPORT_SYMBOL_GPL(o2net_unregister_and_flush_handler_list); 916 917 static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key) 918 { 919 struct o2net_msg_handler *nmh; 920 921 read_lock(&o2net_handler_lock); 922 nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL); 923 if (nmh) 924 kref_get(&nmh->nh_kref); 925 read_unlock(&o2net_handler_lock); 926 927 return nmh; 928 } 929 930 /* ------------------------------------------------------------ */ 931 932 static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len) 933 { 934 struct kvec vec = { .iov_len = len, .iov_base = data, }; 935 struct msghdr msg = { .msg_flags = MSG_DONTWAIT, }; 936 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, len); 937 return sock_recvmsg(sock, &msg, MSG_DONTWAIT); 938 } 939 940 static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec, 941 size_t veclen, size_t total) 942 { 943 int ret; 944 struct msghdr msg = {.msg_flags = 0,}; 945 946 if (sock == NULL) { 947 ret = -EINVAL; 948 goto out; 949 } 950 951 ret = kernel_sendmsg(sock, &msg, vec, veclen, total); 952 if (likely(ret == total)) 953 return 0; 954 mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, total); 955 if (ret >= 0) 956 ret = -EPIPE; /* should be smarter, I bet */ 957 out: 958 mlog(0, "returning error: %d\n", ret); 959 return ret; 960 } 961 962 static void o2net_sendpage(struct o2net_sock_container *sc, 963 void *virt, size_t size) 964 { 965 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 966 struct msghdr msg = {}; 967 struct bio_vec bv; 968 ssize_t ret; 969 970 bvec_set_virt(&bv, virt, size); 971 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, size); 972 973 while (1) { 974 msg.msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES; 975 mutex_lock(&sc->sc_send_lock); 976 ret = sock_sendmsg(sc->sc_sock, &msg); 977 mutex_unlock(&sc->sc_send_lock); 978 979 if (ret == size) 980 break; 981 if (ret == (ssize_t)-EAGAIN) { 982 mlog(0, "sendpage of size %zu to " SC_NODEF_FMT 983 " returned EAGAIN\n", size, SC_NODEF_ARGS(sc)); 984 cond_resched(); 985 continue; 986 } 987 mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT 988 " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret); 989 o2net_ensure_shutdown(nn, sc, 0); 990 break; 991 } 992 } 993 994 static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key) 995 { 996 memset(msg, 0, sizeof(struct o2net_msg)); 997 msg->magic = cpu_to_be16(O2NET_MSG_MAGIC); 998 msg->data_len = cpu_to_be16(data_len); 999 msg->msg_type = cpu_to_be16(msg_type); 1000 msg->sys_status = cpu_to_be32(O2NET_ERR_NONE); 1001 msg->status = 0; 1002 msg->key = cpu_to_be32(key); 1003 } 1004 1005 static int o2net_tx_can_proceed(struct o2net_node *nn, 1006 struct o2net_sock_container **sc_ret, 1007 int *error) 1008 { 1009 int ret = 0; 1010 1011 spin_lock(&nn->nn_lock); 1012 if (nn->nn_persistent_error) { 1013 ret = 1; 1014 *sc_ret = NULL; 1015 *error = nn->nn_persistent_error; 1016 } else if (nn->nn_sc_valid) { 1017 kref_get(&nn->nn_sc->sc_kref); 1018 1019 ret = 1; 1020 *sc_ret = nn->nn_sc; 1021 *error = 0; 1022 } 1023 spin_unlock(&nn->nn_lock); 1024 1025 return ret; 1026 } 1027 1028 /* Get a map of all nodes to which this node is currently connected to */ 1029 void o2net_fill_node_map(unsigned long *map, unsigned int bits) 1030 { 1031 struct o2net_sock_container *sc; 1032 int node, ret; 1033 1034 bitmap_zero(map, bits); 1035 for (node = 0; node < O2NM_MAX_NODES; ++node) { 1036 if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret)) 1037 continue; 1038 if (!ret) { 1039 set_bit(node, map); 1040 sc_put(sc); 1041 } 1042 } 1043 } 1044 EXPORT_SYMBOL_GPL(o2net_fill_node_map); 1045 1046 int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec, 1047 size_t caller_veclen, u8 target_node, int *status) 1048 { 1049 int ret = 0; 1050 struct o2net_msg *msg = NULL; 1051 size_t veclen, caller_bytes = 0; 1052 struct kvec *vec = NULL; 1053 struct o2net_sock_container *sc = NULL; 1054 struct o2net_node *nn = o2net_nn_from_num(target_node); 1055 struct o2net_status_wait nsw = { 1056 .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item), 1057 }; 1058 struct o2net_send_tracking nst; 1059 1060 o2net_init_nst(&nst, msg_type, key, current, target_node); 1061 1062 if (o2net_wq == NULL) { 1063 mlog(0, "attempt to tx without o2netd running\n"); 1064 ret = -ESRCH; 1065 goto out; 1066 } 1067 1068 if (caller_veclen == 0) { 1069 mlog(0, "bad kvec array length\n"); 1070 ret = -EINVAL; 1071 goto out; 1072 } 1073 1074 caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen); 1075 if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) { 1076 mlog(0, "total payload len %zu too large\n", caller_bytes); 1077 ret = -EINVAL; 1078 goto out; 1079 } 1080 1081 if (target_node == o2nm_this_node()) { 1082 ret = -ELOOP; 1083 goto out; 1084 } 1085 1086 o2net_debug_add_nst(&nst); 1087 1088 o2net_set_nst_sock_time(&nst); 1089 1090 wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret)); 1091 if (ret) 1092 goto out; 1093 1094 o2net_set_nst_sock_container(&nst, sc); 1095 1096 veclen = caller_veclen + 1; 1097 vec = kmalloc_objs(struct kvec, veclen, GFP_ATOMIC); 1098 if (vec == NULL) { 1099 mlog(0, "failed to %zu element kvec!\n", veclen); 1100 ret = -ENOMEM; 1101 goto out; 1102 } 1103 1104 msg = kmalloc_obj(struct o2net_msg, GFP_ATOMIC); 1105 if (!msg) { 1106 mlog(0, "failed to allocate a o2net_msg!\n"); 1107 ret = -ENOMEM; 1108 goto out; 1109 } 1110 1111 o2net_init_msg(msg, caller_bytes, msg_type, key); 1112 1113 vec[0].iov_len = sizeof(struct o2net_msg); 1114 vec[0].iov_base = msg; 1115 memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec)); 1116 1117 ret = o2net_prep_nsw(nn, &nsw); 1118 if (ret) 1119 goto out; 1120 1121 msg->msg_num = cpu_to_be32(nsw.ns_id); 1122 o2net_set_nst_msg_id(&nst, nsw.ns_id); 1123 1124 o2net_set_nst_send_time(&nst); 1125 1126 /* finally, convert the message header to network byte-order 1127 * and send */ 1128 mutex_lock(&sc->sc_send_lock); 1129 ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen, 1130 sizeof(struct o2net_msg) + caller_bytes); 1131 mutex_unlock(&sc->sc_send_lock); 1132 msglog(msg, "sending returned %d\n", ret); 1133 if (ret < 0) { 1134 mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret); 1135 goto out; 1136 } 1137 1138 /* wait on other node's handler */ 1139 o2net_set_nst_status_time(&nst); 1140 wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw)); 1141 1142 o2net_update_send_stats(&nst, sc); 1143 1144 /* Note that we avoid overwriting the callers status return 1145 * variable if a system error was reported on the other 1146 * side. Callers beware. */ 1147 ret = o2net_sys_err_to_errno(nsw.ns_sys_status); 1148 if (status && !ret) 1149 *status = nsw.ns_status; 1150 1151 mlog(0, "woken, returning system status %d, user status %d\n", 1152 ret, nsw.ns_status); 1153 out: 1154 o2net_debug_del_nst(&nst); /* must be before dropping sc and node */ 1155 if (sc) 1156 sc_put(sc); 1157 kfree(vec); 1158 kfree(msg); 1159 o2net_complete_nsw(nn, &nsw, 0, 0, 0); 1160 return ret; 1161 } 1162 EXPORT_SYMBOL_GPL(o2net_send_message_vec); 1163 1164 int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len, 1165 u8 target_node, int *status) 1166 { 1167 struct kvec vec = { 1168 .iov_base = data, 1169 .iov_len = len, 1170 }; 1171 return o2net_send_message_vec(msg_type, key, &vec, 1, 1172 target_node, status); 1173 } 1174 EXPORT_SYMBOL_GPL(o2net_send_message); 1175 1176 static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr, 1177 enum o2net_system_error syserr, int err) 1178 { 1179 struct kvec vec = { 1180 .iov_base = hdr, 1181 .iov_len = sizeof(struct o2net_msg), 1182 }; 1183 1184 BUG_ON(syserr >= O2NET_ERR_MAX); 1185 1186 /* leave other fields intact from the incoming message, msg_num 1187 * in particular */ 1188 hdr->sys_status = cpu_to_be32(syserr); 1189 hdr->status = cpu_to_be32(err); 1190 hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC); // twiddle the magic 1191 hdr->data_len = 0; 1192 1193 msglog(hdr, "about to send status magic %d\n", err); 1194 /* hdr has been in host byteorder this whole time */ 1195 return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg)); 1196 } 1197 1198 /* this returns -errno if the header was unknown or too large, etc. 1199 * after this is called the buffer us reused for the next message */ 1200 static int o2net_process_message(struct o2net_sock_container *sc, 1201 struct o2net_msg *hdr) 1202 { 1203 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 1204 int ret = 0, handler_status; 1205 enum o2net_system_error syserr; 1206 struct o2net_msg_handler *nmh = NULL; 1207 void *ret_data = NULL; 1208 1209 msglog(hdr, "processing message\n"); 1210 1211 o2net_sc_postpone_idle(sc); 1212 1213 switch(be16_to_cpu(hdr->magic)) { 1214 case O2NET_MSG_STATUS_MAGIC: 1215 /* special type for returning message status */ 1216 o2net_complete_nsw(nn, NULL, 1217 be32_to_cpu(hdr->msg_num), 1218 be32_to_cpu(hdr->sys_status), 1219 be32_to_cpu(hdr->status)); 1220 goto out; 1221 case O2NET_MSG_KEEP_REQ_MAGIC: 1222 o2net_sendpage(sc, o2net_keep_resp, 1223 sizeof(*o2net_keep_resp)); 1224 goto out; 1225 case O2NET_MSG_KEEP_RESP_MAGIC: 1226 goto out; 1227 case O2NET_MSG_MAGIC: 1228 break; 1229 default: 1230 msglog(hdr, "bad magic\n"); 1231 ret = -EINVAL; 1232 goto out; 1233 } 1234 1235 /* find a handler for it */ 1236 handler_status = 0; 1237 nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type), 1238 be32_to_cpu(hdr->key)); 1239 if (!nmh) { 1240 mlog(ML_TCP, "couldn't find handler for type %u key %08x\n", 1241 be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key)); 1242 syserr = O2NET_ERR_NO_HNDLR; 1243 goto out_respond; 1244 } 1245 1246 syserr = O2NET_ERR_NONE; 1247 1248 if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len) 1249 syserr = O2NET_ERR_OVERFLOW; 1250 1251 if (syserr != O2NET_ERR_NONE) 1252 goto out_respond; 1253 1254 o2net_set_func_start_time(sc); 1255 sc->sc_msg_key = be32_to_cpu(hdr->key); 1256 sc->sc_msg_type = be16_to_cpu(hdr->msg_type); 1257 handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) + 1258 be16_to_cpu(hdr->data_len), 1259 nmh->nh_func_data, &ret_data); 1260 o2net_set_func_stop_time(sc); 1261 1262 o2net_update_recv_stats(sc); 1263 1264 out_respond: 1265 /* this destroys the hdr, so don't use it after this */ 1266 mutex_lock(&sc->sc_send_lock); 1267 ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr, 1268 handler_status); 1269 mutex_unlock(&sc->sc_send_lock); 1270 hdr = NULL; 1271 mlog(0, "sending handler status %d, syserr %d returned %d\n", 1272 handler_status, syserr, ret); 1273 1274 if (nmh) { 1275 BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL); 1276 if (nmh->nh_post_func) 1277 (nmh->nh_post_func)(handler_status, nmh->nh_func_data, 1278 ret_data); 1279 } 1280 1281 out: 1282 if (nmh) 1283 o2net_handler_put(nmh); 1284 return ret; 1285 } 1286 1287 static int o2net_check_handshake(struct o2net_sock_container *sc) 1288 { 1289 struct o2net_handshake *hand = page_address(sc->sc_page); 1290 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 1291 1292 if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) { 1293 printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net " 1294 "protocol version %llu but %llu is required. " 1295 "Disconnecting.\n", SC_NODEF_ARGS(sc), 1296 (unsigned long long)be64_to_cpu(hand->protocol_version), 1297 O2NET_PROTOCOL_VERSION); 1298 1299 /* don't bother reconnecting if its the wrong version. */ 1300 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1301 return -1; 1302 } 1303 1304 /* 1305 * Ensure timeouts are consistent with other nodes, otherwise 1306 * we can end up with one node thinking that the other must be down, 1307 * but isn't. This can ultimately cause corruption. 1308 */ 1309 if (be32_to_cpu(hand->o2net_idle_timeout_ms) != 1310 o2net_idle_timeout()) { 1311 printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network " 1312 "idle timeout of %u ms, but we use %u ms locally. " 1313 "Disconnecting.\n", SC_NODEF_ARGS(sc), 1314 be32_to_cpu(hand->o2net_idle_timeout_ms), 1315 o2net_idle_timeout()); 1316 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1317 return -1; 1318 } 1319 1320 if (be32_to_cpu(hand->o2net_keepalive_delay_ms) != 1321 o2net_keepalive_delay()) { 1322 printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive " 1323 "delay of %u ms, but we use %u ms locally. " 1324 "Disconnecting.\n", SC_NODEF_ARGS(sc), 1325 be32_to_cpu(hand->o2net_keepalive_delay_ms), 1326 o2net_keepalive_delay()); 1327 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1328 return -1; 1329 } 1330 1331 if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) != 1332 O2HB_MAX_WRITE_TIMEOUT_MS) { 1333 printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat " 1334 "timeout of %u ms, but we use %u ms locally. " 1335 "Disconnecting.\n", SC_NODEF_ARGS(sc), 1336 be32_to_cpu(hand->o2hb_heartbeat_timeout_ms), 1337 O2HB_MAX_WRITE_TIMEOUT_MS); 1338 o2net_ensure_shutdown(nn, sc, -ENOTCONN); 1339 return -1; 1340 } 1341 1342 sc->sc_handshake_ok = 1; 1343 1344 spin_lock(&nn->nn_lock); 1345 /* set valid and queue the idle timers only if it hasn't been 1346 * shut down already */ 1347 if (nn->nn_sc == sc) { 1348 o2net_sc_reset_idle_timer(sc); 1349 atomic_set(&nn->nn_timeout, 0); 1350 o2net_set_nn_state(nn, sc, 1, 0); 1351 } 1352 spin_unlock(&nn->nn_lock); 1353 1354 /* shift everything up as though it wasn't there */ 1355 sc->sc_page_off -= sizeof(struct o2net_handshake); 1356 if (sc->sc_page_off) 1357 memmove(hand, hand + 1, sc->sc_page_off); 1358 1359 return 0; 1360 } 1361 1362 /* this demuxes the queued rx bytes into header or payload bits and calls 1363 * handlers as each full message is read off the socket. it returns -error, 1364 * == 0 eof, or > 0 for progress made.*/ 1365 static int o2net_advance_rx(struct o2net_sock_container *sc) 1366 { 1367 struct o2net_msg *hdr; 1368 int ret = 0; 1369 void *data; 1370 size_t datalen; 1371 1372 sclog(sc, "receiving\n"); 1373 o2net_set_advance_start_time(sc); 1374 1375 if (unlikely(sc->sc_handshake_ok == 0)) { 1376 if(sc->sc_page_off < sizeof(struct o2net_handshake)) { 1377 data = page_address(sc->sc_page) + sc->sc_page_off; 1378 datalen = sizeof(struct o2net_handshake) - sc->sc_page_off; 1379 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); 1380 if (ret > 0) 1381 sc->sc_page_off += ret; 1382 } 1383 1384 if (sc->sc_page_off == sizeof(struct o2net_handshake)) { 1385 o2net_check_handshake(sc); 1386 if (unlikely(sc->sc_handshake_ok == 0)) 1387 ret = -EPROTO; 1388 } 1389 goto out; 1390 } 1391 1392 /* do we need more header? */ 1393 if (sc->sc_page_off < sizeof(struct o2net_msg)) { 1394 data = page_address(sc->sc_page) + sc->sc_page_off; 1395 datalen = sizeof(struct o2net_msg) - sc->sc_page_off; 1396 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); 1397 if (ret > 0) { 1398 sc->sc_page_off += ret; 1399 /* only swab incoming here.. we can 1400 * only get here once as we cross from 1401 * being under to over */ 1402 if (sc->sc_page_off == sizeof(struct o2net_msg)) { 1403 hdr = page_address(sc->sc_page); 1404 if (be16_to_cpu(hdr->data_len) > 1405 O2NET_MAX_PAYLOAD_BYTES) 1406 ret = -EOVERFLOW; 1407 } 1408 } 1409 if (ret <= 0) 1410 goto out; 1411 } 1412 1413 if (sc->sc_page_off < sizeof(struct o2net_msg)) { 1414 /* oof, still don't have a header */ 1415 goto out; 1416 } 1417 1418 /* this was swabbed above when we first read it */ 1419 hdr = page_address(sc->sc_page); 1420 1421 msglog(hdr, "at page_off %zu\n", sc->sc_page_off); 1422 1423 /* do we need more payload? */ 1424 if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) { 1425 /* need more payload */ 1426 data = page_address(sc->sc_page) + sc->sc_page_off; 1427 datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) - 1428 sc->sc_page_off; 1429 ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen); 1430 if (ret > 0) 1431 sc->sc_page_off += ret; 1432 if (ret <= 0) 1433 goto out; 1434 } 1435 1436 if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) { 1437 /* we can only get here once, the first time we read 1438 * the payload.. so set ret to progress if the handler 1439 * works out. after calling this the message is toast */ 1440 ret = o2net_process_message(sc, hdr); 1441 if (ret == 0) 1442 ret = 1; 1443 sc->sc_page_off = 0; 1444 } 1445 1446 out: 1447 sclog(sc, "ret = %d\n", ret); 1448 o2net_set_advance_stop_time(sc); 1449 return ret; 1450 } 1451 1452 /* this work func is triggered by data ready. it reads until it can read no 1453 * more. it interprets 0, eof, as fatal. if data_ready hits while we're doing 1454 * our work the work struct will be marked and we'll be called again. */ 1455 static void o2net_rx_until_empty(struct work_struct *work) 1456 { 1457 struct o2net_sock_container *sc = 1458 container_of(work, struct o2net_sock_container, sc_rx_work); 1459 int ret; 1460 1461 do { 1462 ret = o2net_advance_rx(sc); 1463 } while (ret > 0); 1464 1465 if (ret <= 0 && ret != -EAGAIN) { 1466 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 1467 sclog(sc, "saw error %d, closing\n", ret); 1468 /* not permanent so read failed handshake can retry */ 1469 o2net_ensure_shutdown(nn, sc, 0); 1470 } 1471 1472 sc_put(sc); 1473 } 1474 1475 static void o2net_initialize_handshake(void) 1476 { 1477 o2net_hand->o2hb_heartbeat_timeout_ms = cpu_to_be32( 1478 O2HB_MAX_WRITE_TIMEOUT_MS); 1479 o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout()); 1480 o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32( 1481 o2net_keepalive_delay()); 1482 o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32( 1483 o2net_reconnect_delay()); 1484 } 1485 1486 /* ------------------------------------------------------------ */ 1487 1488 /* called when a connect completes and after a sock is accepted. the 1489 * rx path will see the response and mark the sc valid */ 1490 static void o2net_sc_connect_completed(struct work_struct *work) 1491 { 1492 struct o2net_sock_container *sc = 1493 container_of(work, struct o2net_sock_container, 1494 sc_connect_work); 1495 1496 mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n", 1497 (unsigned long long)O2NET_PROTOCOL_VERSION, 1498 (unsigned long long)be64_to_cpu(o2net_hand->connector_id)); 1499 1500 o2net_initialize_handshake(); 1501 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand)); 1502 sc_put(sc); 1503 } 1504 1505 /* this is called as a work_struct func. */ 1506 static void o2net_sc_send_keep_req(struct work_struct *work) 1507 { 1508 struct o2net_sock_container *sc = 1509 container_of(work, struct o2net_sock_container, 1510 sc_keepalive_work.work); 1511 1512 o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req)); 1513 sc_put(sc); 1514 } 1515 1516 /* socket shutdown does a timer_delete_sync against this as it tears down. 1517 * we can't start this timer until we've got to the point in sc buildup 1518 * where shutdown is going to be involved */ 1519 static void o2net_idle_timer(struct timer_list *t) 1520 { 1521 struct o2net_sock_container *sc = timer_container_of(sc, t, 1522 sc_idle_timeout); 1523 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 1524 #ifdef CONFIG_DEBUG_FS 1525 unsigned long msecs = ktime_to_ms(ktime_get()) - 1526 ktime_to_ms(sc->sc_tv_timer); 1527 #else 1528 unsigned long msecs = o2net_idle_timeout(); 1529 #endif 1530 1531 printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been " 1532 "idle for %lu.%lu secs.\n", 1533 SC_NODEF_ARGS(sc), msecs / 1000, msecs % 1000); 1534 1535 /* idle timerout happen, don't shutdown the connection, but 1536 * make fence decision. Maybe the connection can recover before 1537 * the decision is made. 1538 */ 1539 atomic_set(&nn->nn_timeout, 1); 1540 o2quo_conn_err(o2net_num_from_nn(nn)); 1541 queue_delayed_work(o2net_wq, &nn->nn_still_up, 1542 msecs_to_jiffies(O2NET_QUORUM_DELAY_MS)); 1543 1544 o2net_sc_reset_idle_timer(sc); 1545 1546 } 1547 1548 static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc) 1549 { 1550 o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work); 1551 o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work, 1552 msecs_to_jiffies(o2net_keepalive_delay())); 1553 o2net_set_sock_timer(sc); 1554 mod_timer(&sc->sc_idle_timeout, 1555 jiffies + msecs_to_jiffies(o2net_idle_timeout())); 1556 } 1557 1558 static void o2net_sc_postpone_idle(struct o2net_sock_container *sc) 1559 { 1560 struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num); 1561 1562 /* clear fence decision since the connection recover from timeout*/ 1563 if (atomic_read(&nn->nn_timeout)) { 1564 o2quo_conn_up(o2net_num_from_nn(nn)); 1565 cancel_delayed_work(&nn->nn_still_up); 1566 atomic_set(&nn->nn_timeout, 0); 1567 } 1568 1569 /* Only push out an existing timer */ 1570 if (timer_pending(&sc->sc_idle_timeout)) 1571 o2net_sc_reset_idle_timer(sc); 1572 } 1573 1574 /* this work func is kicked whenever a path sets the nn state which doesn't 1575 * have valid set. This includes seeing hb come up, losing a connection, 1576 * having a connect attempt fail, etc. This centralizes the logic which decides 1577 * if a connect attempt should be made or if we should give up and all future 1578 * transmit attempts should fail */ 1579 static void o2net_start_connect(struct work_struct *work) 1580 { 1581 struct o2net_node *nn = 1582 container_of(work, struct o2net_node, nn_connect_work.work); 1583 struct o2net_sock_container *sc = NULL; 1584 struct o2nm_node *node = NULL, *mynode = NULL; 1585 struct socket *sock = NULL; 1586 struct sockaddr_in myaddr = {0, }, remoteaddr = {0, }; 1587 int ret = 0, stop; 1588 unsigned int timeout; 1589 unsigned int nofs_flag; 1590 1591 /* 1592 * sock_create allocates the sock with GFP_KERNEL. We must 1593 * prevent the filesystem from being reentered by memory reclaim. 1594 */ 1595 nofs_flag = memalloc_nofs_save(); 1596 /* if we're greater we initiate tx, otherwise we accept */ 1597 if (o2nm_this_node() <= o2net_num_from_nn(nn)) 1598 goto out; 1599 1600 /* watch for racing with tearing a node down */ 1601 node = o2nm_get_node_by_num(o2net_num_from_nn(nn)); 1602 if (node == NULL) 1603 goto out; 1604 1605 mynode = o2nm_get_node_by_num(o2nm_this_node()); 1606 if (mynode == NULL) 1607 goto out; 1608 1609 spin_lock(&nn->nn_lock); 1610 /* 1611 * see if we already have one pending or have given up. 1612 * For nn_timeout, it is set when we close the connection 1613 * because of the idle time out. So it means that we have 1614 * at least connected to that node successfully once, 1615 * now try to connect to it again. 1616 */ 1617 timeout = atomic_read(&nn->nn_timeout); 1618 stop = (nn->nn_sc || 1619 (nn->nn_persistent_error && 1620 (nn->nn_persistent_error != -ENOTCONN || timeout == 0))); 1621 spin_unlock(&nn->nn_lock); 1622 if (stop) 1623 goto out; 1624 1625 nn->nn_last_connect_attempt = jiffies; 1626 1627 sc = sc_alloc(node); 1628 if (sc == NULL) { 1629 mlog(0, "couldn't allocate sc\n"); 1630 ret = -ENOMEM; 1631 goto out; 1632 } 1633 1634 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); 1635 if (ret < 0) { 1636 mlog(0, "can't create socket: %d\n", ret); 1637 goto out; 1638 } 1639 sc->sc_sock = sock; /* freed by sc_kref_release */ 1640 1641 sock->sk->sk_allocation = GFP_ATOMIC; 1642 sock->sk->sk_use_task_frag = false; 1643 1644 myaddr.sin_family = AF_INET; 1645 myaddr.sin_addr.s_addr = mynode->nd_ipv4_address; 1646 myaddr.sin_port = htons(0); /* any port */ 1647 1648 ret = sock->ops->bind(sock, (struct sockaddr_unsized *)&myaddr, 1649 sizeof(myaddr)); 1650 if (ret) { 1651 mlog(ML_ERROR, "bind failed with %d at address %pI4\n", 1652 ret, &mynode->nd_ipv4_address); 1653 goto out; 1654 } 1655 1656 tcp_sock_set_nodelay(sc->sc_sock->sk); 1657 tcp_sock_set_user_timeout(sock->sk, O2NET_TCP_USER_TIMEOUT); 1658 1659 o2net_register_callbacks(sc->sc_sock->sk, sc); 1660 1661 spin_lock(&nn->nn_lock); 1662 /* handshake completion will set nn->nn_sc_valid */ 1663 o2net_set_nn_state(nn, sc, 0, 0); 1664 spin_unlock(&nn->nn_lock); 1665 1666 remoteaddr.sin_family = AF_INET; 1667 remoteaddr.sin_addr.s_addr = node->nd_ipv4_address; 1668 remoteaddr.sin_port = node->nd_ipv4_port; 1669 1670 ret = sc->sc_sock->ops->connect(sc->sc_sock, 1671 (struct sockaddr_unsized *)&remoteaddr, 1672 sizeof(remoteaddr), 1673 O_NONBLOCK); 1674 if (ret == -EINPROGRESS) 1675 ret = 0; 1676 1677 out: 1678 if (ret && sc) { 1679 printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT 1680 " failed with errno %d\n", SC_NODEF_ARGS(sc), ret); 1681 /* 0 err so that another will be queued and attempted 1682 * from set_nn_state */ 1683 o2net_ensure_shutdown(nn, sc, 0); 1684 } 1685 if (sc) 1686 sc_put(sc); 1687 if (node) 1688 o2nm_node_put(node); 1689 if (mynode) 1690 o2nm_node_put(mynode); 1691 1692 memalloc_nofs_restore(nofs_flag); 1693 return; 1694 } 1695 1696 static void o2net_connect_expired(struct work_struct *work) 1697 { 1698 struct o2net_node *nn = 1699 container_of(work, struct o2net_node, nn_connect_expired.work); 1700 1701 spin_lock(&nn->nn_lock); 1702 if (!nn->nn_sc_valid) { 1703 printk(KERN_NOTICE "o2net: No connection established with " 1704 "node %u after %u.%u seconds, check network and" 1705 " cluster configuration.\n", 1706 o2net_num_from_nn(nn), 1707 o2net_idle_timeout() / 1000, 1708 o2net_idle_timeout() % 1000); 1709 1710 o2net_set_nn_state(nn, NULL, 0, 0); 1711 } 1712 spin_unlock(&nn->nn_lock); 1713 } 1714 1715 static void o2net_still_up(struct work_struct *work) 1716 { 1717 struct o2net_node *nn = 1718 container_of(work, struct o2net_node, nn_still_up.work); 1719 1720 o2quo_hb_still_up(o2net_num_from_nn(nn)); 1721 } 1722 1723 /* ------------------------------------------------------------ */ 1724 1725 static void o2net_hb_node_up(struct o2net_node *nn) 1726 { 1727 /* ensure an immediate connect attempt */ 1728 nn->nn_last_connect_attempt = jiffies - 1729 (msecs_to_jiffies(o2net_reconnect_delay()) + 1); 1730 1731 spin_lock(&nn->nn_lock); 1732 atomic_set(&nn->nn_timeout, 0); 1733 if (nn->nn_persistent_error) 1734 o2net_set_nn_state(nn, NULL, 0, 0); 1735 spin_unlock(&nn->nn_lock); 1736 } 1737 1738 void o2net_disconnect_node(struct o2nm_node *node) 1739 { 1740 struct o2net_node *nn = o2net_nn_from_num(node->nd_num); 1741 1742 /* don't reconnect until it's heartbeating again */ 1743 spin_lock(&nn->nn_lock); 1744 atomic_set(&nn->nn_timeout, 0); 1745 o2net_set_nn_state(nn, NULL, 0, -ENOTCONN); 1746 spin_unlock(&nn->nn_lock); 1747 1748 cancel_delayed_work(&nn->nn_connect_expired); 1749 cancel_delayed_work(&nn->nn_connect_work); 1750 cancel_delayed_work(&nn->nn_still_up); 1751 o2net_flush_wq(); 1752 } 1753 1754 static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num, 1755 void *data) 1756 { 1757 u8 this_node; 1758 1759 o2quo_hb_down(node_num); 1760 1761 if (!node) 1762 goto out; 1763 1764 this_node = o2nm_this_node(); 1765 if (!READ_ONCE(o2net_listening) || this_node == O2NM_MAX_NODES) 1766 goto out; 1767 1768 if (node_num != this_node) 1769 o2net_disconnect_node(node); 1770 1771 out: 1772 BUG_ON(atomic_read(&o2net_connected_peers) < 0); 1773 } 1774 1775 static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num, 1776 void *data) 1777 { 1778 u8 this_node; 1779 1780 o2quo_hb_up(node_num); 1781 1782 BUG_ON(!node); 1783 1784 this_node = o2nm_this_node(); 1785 if (!READ_ONCE(o2net_listening) || this_node == O2NM_MAX_NODES) 1786 return; 1787 1788 if (node_num != this_node) 1789 o2net_hb_node_up(o2net_nn_from_num(node_num)); 1790 } 1791 1792 void o2net_unregister_hb_callbacks(void) 1793 { 1794 o2hb_unregister_callback(NULL, &o2net_hb_up); 1795 o2hb_unregister_callback(NULL, &o2net_hb_down); 1796 } 1797 1798 /* 1799 * Delay heartbeat-driven network work until the local node is fully published 1800 * through o2nm_this_node(), then replay the nodes that are already live while 1801 * callback delivery stays blocked. 1802 */ 1803 void o2net_complete_start_listening(struct o2nm_node *node) 1804 { 1805 unsigned long live_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)]; 1806 unsigned long node_num; 1807 u8 local_node; 1808 1809 local_node = o2nm_this_node(); 1810 if (WARN_ON_ONCE(local_node == O2NM_MAX_NODES)) 1811 return; 1812 if (WARN_ON_ONCE(local_node != node->nd_num)) 1813 return; 1814 if (WARN_ON_ONCE(!o2net_wq)) 1815 return; 1816 1817 o2hb_callback_read_lock(); 1818 WRITE_ONCE(o2net_listening, true); 1819 o2hb_fill_node_map_locked(live_nodes, O2NM_MAX_NODES); 1820 for_each_set_bit(node_num, live_nodes, O2NM_MAX_NODES) { 1821 if (node_num == local_node) 1822 continue; 1823 1824 o2net_hb_node_up(o2net_nn_from_num(node_num)); 1825 } 1826 o2hb_callback_read_unlock(); 1827 } 1828 1829 int o2net_register_hb_callbacks(void) 1830 { 1831 int ret; 1832 1833 o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB, 1834 o2net_hb_node_down_cb, NULL, O2NET_HB_PRI); 1835 o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB, 1836 o2net_hb_node_up_cb, NULL, O2NET_HB_PRI); 1837 1838 ret = o2hb_register_callback(NULL, &o2net_hb_up); 1839 if (ret == 0) 1840 ret = o2hb_register_callback(NULL, &o2net_hb_down); 1841 1842 if (ret) 1843 o2net_unregister_hb_callbacks(); 1844 1845 return ret; 1846 } 1847 1848 /* ------------------------------------------------------------ */ 1849 1850 static int o2net_accept_one(struct socket *sock, int *more) 1851 { 1852 int ret; 1853 struct sockaddr_in sin; 1854 struct socket *new_sock = NULL; 1855 struct o2nm_node *node = NULL; 1856 struct o2nm_node *local_node = NULL; 1857 struct o2net_sock_container *sc = NULL; 1858 struct proto_accept_arg arg = { 1859 .flags = O_NONBLOCK, 1860 }; 1861 struct o2net_node *nn; 1862 unsigned int nofs_flag; 1863 1864 /* 1865 * sock_create_lite allocates the sock with GFP_KERNEL. We must 1866 * prevent the filesystem from being reentered by memory reclaim. 1867 */ 1868 nofs_flag = memalloc_nofs_save(); 1869 1870 BUG_ON(sock == NULL); 1871 *more = 0; 1872 ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type, 1873 sock->sk->sk_protocol, &new_sock); 1874 if (ret) 1875 goto out; 1876 1877 new_sock->type = sock->type; 1878 new_sock->ops = sock->ops; 1879 ret = sock->ops->accept(sock, new_sock, &arg); 1880 if (ret < 0) 1881 goto out; 1882 1883 *more = 1; 1884 new_sock->sk->sk_allocation = GFP_ATOMIC; 1885 1886 tcp_sock_set_nodelay(new_sock->sk); 1887 tcp_sock_set_user_timeout(new_sock->sk, O2NET_TCP_USER_TIMEOUT); 1888 1889 ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin, 1); 1890 if (ret < 0) 1891 goto out; 1892 1893 node = o2nm_get_node_by_ip(sin.sin_addr.s_addr); 1894 if (node == NULL) { 1895 printk(KERN_NOTICE "o2net: Attempt to connect from unknown " 1896 "node at %pI4:%d\n", &sin.sin_addr.s_addr, 1897 ntohs(sin.sin_port)); 1898 ret = -EINVAL; 1899 goto out; 1900 } 1901 1902 if (o2nm_this_node() >= node->nd_num) { 1903 local_node = o2nm_get_node_by_num(o2nm_this_node()); 1904 if (local_node) 1905 printk(KERN_NOTICE "o2net: Unexpected connect attempt " 1906 "seen at node '%s' (%u, %pI4:%d) from " 1907 "node '%s' (%u, %pI4:%d)\n", 1908 local_node->nd_name, local_node->nd_num, 1909 &(local_node->nd_ipv4_address), 1910 ntohs(local_node->nd_ipv4_port), 1911 node->nd_name, 1912 node->nd_num, &sin.sin_addr.s_addr, 1913 ntohs(sin.sin_port)); 1914 ret = -EINVAL; 1915 goto out; 1916 } 1917 1918 /* this happens all the time when the other node sees our heartbeat 1919 * and tries to connect before we see their heartbeat */ 1920 if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) { 1921 mlog(ML_CONN, "attempt to connect from node '%s' at " 1922 "%pI4:%d but it isn't heartbeating\n", 1923 node->nd_name, &sin.sin_addr.s_addr, 1924 ntohs(sin.sin_port)); 1925 ret = -EINVAL; 1926 goto out; 1927 } 1928 1929 nn = o2net_nn_from_num(node->nd_num); 1930 1931 spin_lock(&nn->nn_lock); 1932 if (nn->nn_sc) 1933 ret = -EBUSY; 1934 else 1935 ret = 0; 1936 spin_unlock(&nn->nn_lock); 1937 if (ret) { 1938 printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' " 1939 "at %pI4:%d but it already has an open connection\n", 1940 node->nd_name, &sin.sin_addr.s_addr, 1941 ntohs(sin.sin_port)); 1942 goto out; 1943 } 1944 1945 sc = sc_alloc(node); 1946 if (sc == NULL) { 1947 ret = -ENOMEM; 1948 goto out; 1949 } 1950 1951 sc->sc_sock = new_sock; 1952 new_sock = NULL; 1953 1954 spin_lock(&nn->nn_lock); 1955 atomic_set(&nn->nn_timeout, 0); 1956 o2net_set_nn_state(nn, sc, 0, 0); 1957 spin_unlock(&nn->nn_lock); 1958 1959 o2net_register_callbacks(sc->sc_sock->sk, sc); 1960 o2net_sc_queue_work(sc, &sc->sc_rx_work); 1961 1962 o2net_initialize_handshake(); 1963 o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand)); 1964 1965 out: 1966 if (new_sock) 1967 sock_release(new_sock); 1968 if (node) 1969 o2nm_node_put(node); 1970 if (local_node) 1971 o2nm_node_put(local_node); 1972 if (sc) 1973 sc_put(sc); 1974 1975 memalloc_nofs_restore(nofs_flag); 1976 return ret; 1977 } 1978 1979 /* 1980 * This function is invoked in response to one or more 1981 * pending accepts at softIRQ level. We must drain the 1982 * entire que before returning. 1983 */ 1984 1985 static void o2net_accept_many(struct work_struct *work) 1986 { 1987 struct socket *sock = o2net_listen_sock; 1988 int more; 1989 1990 /* 1991 * It is critical to note that due to interrupt moderation 1992 * at the network driver level, we can't assume to get a 1993 * softIRQ for every single conn since tcp SYN packets 1994 * can arrive back-to-back, and therefore many pending 1995 * accepts may result in just 1 softIRQ. If we terminate 1996 * the o2net_accept_one() loop upon seeing an err, what happens 1997 * to the rest of the conns in the queue? If no new SYN 1998 * arrives for hours, no softIRQ will be delivered, 1999 * and the connections will just sit in the queue. 2000 */ 2001 2002 for (;;) { 2003 o2net_accept_one(sock, &more); 2004 if (!more) 2005 break; 2006 cond_resched(); 2007 } 2008 } 2009 2010 static void o2net_listen_data_ready(struct sock *sk) 2011 { 2012 void (*ready)(struct sock *sk); 2013 2014 trace_sk_data_ready(sk); 2015 2016 read_lock_bh(&sk->sk_callback_lock); 2017 ready = sk->sk_user_data; 2018 if (ready == NULL) { /* check for teardown race */ 2019 ready = sk->sk_data_ready; 2020 goto out; 2021 } 2022 2023 /* This callback may called twice when a new connection 2024 * is being established as a child socket inherits everything 2025 * from a parent LISTEN socket, including the data_ready cb of 2026 * the parent. This leads to a hazard. In o2net_accept_one() 2027 * we are still initializing the child socket but have not 2028 * changed the inherited data_ready callback yet when 2029 * data starts arriving. 2030 * We avoid this hazard by checking the state. 2031 * For the listening socket, the state will be TCP_LISTEN; for the new 2032 * socket, will be TCP_ESTABLISHED. Also, in this case, 2033 * sk->sk_user_data is not a valid function pointer. 2034 */ 2035 2036 if (sk->sk_state == TCP_LISTEN) { 2037 queue_work(o2net_wq, &o2net_listen_work); 2038 } else { 2039 ready = NULL; 2040 } 2041 2042 out: 2043 read_unlock_bh(&sk->sk_callback_lock); 2044 if (ready != NULL) 2045 ready(sk); 2046 } 2047 2048 static int o2net_open_listening_sock(__be32 addr, __be16 port) 2049 { 2050 struct socket *sock = NULL; 2051 int ret; 2052 struct sockaddr_in sin = { 2053 .sin_family = PF_INET, 2054 .sin_addr = { .s_addr = addr }, 2055 .sin_port = port, 2056 }; 2057 2058 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); 2059 if (ret < 0) { 2060 printk(KERN_ERR "o2net: Error %d while creating socket\n", ret); 2061 goto out; 2062 } 2063 2064 sock->sk->sk_allocation = GFP_ATOMIC; 2065 2066 write_lock_bh(&sock->sk->sk_callback_lock); 2067 sock->sk->sk_user_data = sock->sk->sk_data_ready; 2068 sock->sk->sk_data_ready = o2net_listen_data_ready; 2069 write_unlock_bh(&sock->sk->sk_callback_lock); 2070 2071 o2net_listen_sock = sock; 2072 INIT_WORK(&o2net_listen_work, o2net_accept_many); 2073 2074 sock->sk->sk_reuse = SK_CAN_REUSE; 2075 ret = sock->ops->bind(sock, (struct sockaddr_unsized *)&sin, sizeof(sin)); 2076 if (ret < 0) { 2077 printk(KERN_ERR "o2net: Error %d while binding socket at " 2078 "%pI4:%u\n", ret, &addr, ntohs(port)); 2079 goto out; 2080 } 2081 2082 ret = sock->ops->listen(sock, 64); 2083 if (ret < 0) 2084 printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n", 2085 ret, &addr, ntohs(port)); 2086 2087 out: 2088 if (ret) { 2089 o2net_listen_sock = NULL; 2090 if (sock) 2091 sock_release(sock); 2092 } 2093 return ret; 2094 } 2095 2096 static void o2net_destroy_wq(void) 2097 { 2098 struct workqueue_struct *wq; 2099 2100 mutex_lock(&o2net_wq_mutex); 2101 if (o2net_wq_destroying) { 2102 mutex_unlock(&o2net_wq_mutex); 2103 wait_for_completion(&o2net_wq_destroyed); 2104 return; 2105 } 2106 2107 wq = o2net_wq; 2108 if (!wq) { 2109 mutex_unlock(&o2net_wq_mutex); 2110 return; 2111 } 2112 2113 reinit_completion(&o2net_wq_destroyed); 2114 o2net_wq_destroying = wq; 2115 mutex_unlock(&o2net_wq_mutex); 2116 2117 destroy_workqueue(wq); 2118 2119 mutex_lock(&o2net_wq_mutex); 2120 o2net_wq = NULL; 2121 o2net_wq_destroying = NULL; 2122 complete_all(&o2net_wq_destroyed); 2123 mutex_unlock(&o2net_wq_mutex); 2124 } 2125 2126 /* 2127 * called from node manager when we should bring up our network listening 2128 * socket. node manager handles all the serialization to only call this 2129 * once and to match it with o2net_stop_listening(). note, 2130 * o2nm_this_node() doesn't work yet as we're being called while it 2131 * is being set up. 2132 */ 2133 int o2net_start_listening(struct o2nm_node *node) 2134 { 2135 int ret = 0; 2136 struct workqueue_struct *wq; 2137 2138 if (WARN_ON_ONCE(READ_ONCE(o2net_listening))) 2139 return -EBUSY; 2140 2141 mutex_lock(&o2net_wq_mutex); 2142 if (o2net_wq_destroying) { 2143 mutex_unlock(&o2net_wq_mutex); 2144 return -EBUSY; 2145 } 2146 if (WARN_ON_ONCE(o2net_wq)) { 2147 mutex_unlock(&o2net_wq_mutex); 2148 return -EBUSY; 2149 } 2150 mutex_unlock(&o2net_wq_mutex); 2151 2152 BUG_ON(o2net_listen_sock != NULL); 2153 2154 mlog(ML_KTHREAD, "starting o2net thread...\n"); 2155 wq = alloc_ordered_workqueue("o2net", WQ_MEM_RECLAIM); 2156 if (!wq) { 2157 mlog(ML_ERROR, "unable to launch o2net thread\n"); 2158 return -ENOMEM; /* ? */ 2159 } 2160 2161 mutex_lock(&o2net_wq_mutex); 2162 if (unlikely(o2net_wq_destroying || o2net_wq)) { 2163 mutex_unlock(&o2net_wq_mutex); 2164 destroy_workqueue(wq); 2165 return -EBUSY; 2166 } 2167 o2net_wq = wq; 2168 mutex_unlock(&o2net_wq_mutex); 2169 2170 ret = o2net_open_listening_sock(node->nd_ipv4_address, 2171 node->nd_ipv4_port); 2172 if (ret) { 2173 o2net_destroy_wq(); 2174 } else 2175 o2quo_conn_up(node->nd_num); 2176 2177 return ret; 2178 } 2179 2180 /* again, o2nm_this_node() doesn't work here as we're involved in 2181 * tearing it down */ 2182 void o2net_stop_listening(struct o2nm_node *node) 2183 { 2184 struct socket *sock = o2net_listen_sock; 2185 size_t i; 2186 2187 BUG_ON(o2net_wq == NULL); 2188 BUG_ON(o2net_listen_sock == NULL); 2189 2190 WRITE_ONCE(o2net_listening, false); 2191 o2hb_synchronize_callbacks(); 2192 2193 /* stop the listening socket from generating work */ 2194 write_lock_bh(&sock->sk->sk_callback_lock); 2195 sock->sk->sk_data_ready = sock->sk->sk_user_data; 2196 sock->sk->sk_user_data = NULL; 2197 write_unlock_bh(&sock->sk->sk_callback_lock); 2198 2199 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) { 2200 struct o2nm_node *node = o2nm_get_node_by_num(i); 2201 if (node) { 2202 o2net_disconnect_node(node); 2203 o2nm_node_put(node); 2204 } 2205 } 2206 2207 /* finish all work and tear down the work queue */ 2208 mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n"); 2209 o2net_destroy_wq(); 2210 2211 sock_release(o2net_listen_sock); 2212 o2net_listen_sock = NULL; 2213 2214 o2quo_conn_err(node->nd_num); 2215 } 2216 2217 /* ------------------------------------------------------------ */ 2218 2219 int o2net_init(void) 2220 { 2221 struct folio *folio; 2222 void *p; 2223 unsigned long i; 2224 2225 o2quo_init(); 2226 o2net_debugfs_init(); 2227 2228 folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, 0); 2229 if (!folio) 2230 goto out; 2231 2232 p = folio_address(folio); 2233 o2net_hand = p; 2234 p += sizeof(struct o2net_handshake); 2235 o2net_keep_req = p; 2236 p += sizeof(struct o2net_msg); 2237 o2net_keep_resp = p; 2238 2239 o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION); 2240 o2net_hand->connector_id = cpu_to_be64(1); 2241 2242 o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC); 2243 o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC); 2244 2245 for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) { 2246 struct o2net_node *nn = o2net_nn_from_num(i); 2247 2248 atomic_set(&nn->nn_timeout, 0); 2249 spin_lock_init(&nn->nn_lock); 2250 INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect); 2251 INIT_DELAYED_WORK(&nn->nn_connect_expired, 2252 o2net_connect_expired); 2253 INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up); 2254 /* until we see hb from a node we'll return einval */ 2255 nn->nn_persistent_error = -ENOTCONN; 2256 init_waitqueue_head(&nn->nn_sc_wq); 2257 idr_init(&nn->nn_status_idr); 2258 INIT_LIST_HEAD(&nn->nn_status_list); 2259 } 2260 2261 return 0; 2262 2263 out: 2264 o2net_debugfs_exit(); 2265 o2quo_exit(); 2266 return -ENOMEM; 2267 } 2268 2269 void o2net_exit(void) 2270 { 2271 o2quo_exit(); 2272 o2net_debugfs_exit(); 2273 folio_put(virt_to_folio(o2net_hand)); 2274 } 2275