1 /* 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 */ 4 5 /* 6 * This file contains code imported from the OFED rds source file connection.c 7 * Oracle elects to have and use the contents of connection.c under and governed 8 * by the OpenIB.org BSD license (see below for full license text). However, 9 * the following notice accompanied the original version of this file: 10 */ 11 12 /* 13 * Copyright (c) 2006 Oracle. All rights reserved. 14 * 15 * This software is available to you under a choice of one of two 16 * licenses. You may choose to be licensed under the terms of the GNU 17 * General Public License (GPL) Version 2, available from the file 18 * COPYING in the main directory of this source tree, or the 19 * OpenIB.org BSD license below: 20 * 21 * Redistribution and use in source and binary forms, with or 22 * without modification, are permitted provided that the following 23 * conditions are met: 24 * 25 * - Redistributions of source code must retain the above 26 * copyright notice, this list of conditions and the following 27 * disclaimer. 28 * 29 * - Redistributions in binary form must reproduce the above 30 * copyright notice, this list of conditions and the following 31 * disclaimer in the documentation and/or other materials 32 * provided with the distribution. 33 * 34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 37 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 38 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 39 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 40 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 41 * SOFTWARE. 42 * 43 */ 44 #include <sys/types.h> 45 #include <sys/kmem.h> 46 #include <sys/rds.h> 47 48 #include <sys/ib/clients/rdsv3/rdsv3.h> 49 #include <sys/ib/clients/rdsv3/loop.h> 50 #include <sys/ib/clients/rdsv3/rdsv3_debug.h> 51 52 /* converting this to RCU is a chore for another day.. */ 53 static krwlock_t rdsv3_conn_lock; 54 static unsigned long rdsv3_conn_count; 55 struct avl_tree rdsv3_conn_hash; 56 static struct kmem_cache *rdsv3_conn_slab = NULL; 57 58 #define rdsv3_conn_info_set(var, test, suffix) do { \ 59 if (test) \ 60 var |= RDS_INFO_CONNECTION_FLAG_##suffix; \ 61 } while (0) 62 63 64 static struct rdsv3_connection * 65 rdsv3_conn_lookup(uint32_be_t laddr, uint32_be_t faddr, avl_index_t *pos) 66 { 67 struct rdsv3_connection *conn; 68 struct rdsv3_conn_info_s conn_info; 69 avl_index_t place = 0; 70 71 conn_info.c_laddr = laddr; 72 conn_info.c_faddr = faddr; 73 74 conn = avl_find(&rdsv3_conn_hash, &conn_info, &place); 75 76 RDSV3_DPRINTF5("rdsv3_conn_lookup", 77 "returning conn %p for %u.%u.%u.%u -> %u.%u.%u.%u", 78 conn, NIPQUAD(laddr), NIPQUAD(faddr)); 79 80 if (pos != NULL) 81 *pos = place; 82 83 return (conn); 84 } 85 86 /* 87 * This is called by transports as they're bringing down a connection. 88 * It clears partial message state so that the transport can start sending 89 * and receiving over this connection again in the future. It is up to 90 * the transport to have serialized this call with its send and recv. 91 */ 92 void 93 rdsv3_conn_reset(struct rdsv3_connection *conn) 94 { 95 RDSV3_DPRINTF2("rdsv3_conn_reset", 96 "connection %u.%u.%u.%u to %u.%u.%u.%u reset", 97 NIPQUAD(conn->c_laddr), NIPQUAD(conn->c_faddr)); 98 99 rdsv3_stats_inc(s_conn_reset); 100 rdsv3_send_reset(conn); 101 conn->c_flags = 0; 102 103 /* 104 * Do not clear next_rx_seq here, else we cannot distinguish 105 * retransmitted packets from new packets, and will hand all 106 * of them to the application. That is not consistent with the 107 * reliability guarantees of RDS. 108 */ 109 } 110 111 /* 112 * There is only every one 'conn' for a given pair of addresses in the 113 * system at a time. They contain messages to be retransmitted and so 114 * span the lifetime of the actual underlying transport connections. 115 * 116 * For now they are not garbage collected once they're created. They 117 * are torn down as the module is removed, if ever. 118 */ 119 static struct rdsv3_connection * 120 __rdsv3_conn_create(uint32_be_t laddr, uint32_be_t faddr, 121 struct rdsv3_transport *trans, int gfp, int is_outgoing) 122 { 123 struct rdsv3_connection *conn, *parent = NULL; 124 avl_index_t pos; 125 int ret; 126 127 rw_enter(&rdsv3_conn_lock, RW_READER); 128 conn = rdsv3_conn_lookup(laddr, faddr, &pos); 129 if (conn && 130 conn->c_loopback && 131 conn->c_trans != &rdsv3_loop_transport && 132 !is_outgoing) { 133 /* 134 * This is a looped back IB connection, and we're 135 * called by the code handling the incoming connect. 136 * We need a second connection object into which we 137 * can stick the other QP. 138 */ 139 parent = conn; 140 conn = parent->c_passive; 141 } 142 rw_exit(&rdsv3_conn_lock); 143 if (conn) 144 goto out; 145 146 RDSV3_DPRINTF2("__rdsv3_conn_create", "Enter(%x -> %x)", 147 ntohl(laddr), ntohl(faddr)); 148 149 conn = kmem_cache_alloc(rdsv3_conn_slab, gfp); 150 if (!conn) { 151 conn = ERR_PTR(-ENOMEM); 152 goto out; 153 } 154 155 /* see rdsv3_conn_constructor */ 156 conn->c_laddr = laddr; 157 conn->c_faddr = faddr; 158 159 /* 160 * We don't allow sockets to send messages without binding. 161 * So, the IP address will already be there in the bind array. 162 * Mostly, this is a readonly operation. 163 * For now, passing GLOBAL_ZONEID. 164 */ 165 conn->c_bucketp = rdsv3_find_ip_bucket(ntohl(laddr), GLOBAL_ZONEID); 166 167 ret = rdsv3_cong_get_maps(conn); 168 if (ret) { 169 kmem_cache_free(rdsv3_conn_slab, conn); 170 conn = ERR_PTR(ret); 171 goto out; 172 } 173 174 /* 175 * This is where a connection becomes loopback. If *any* RDS sockets 176 * can bind to the destination address then we'd rather the messages 177 * flow through loopback rather than either transport. 178 */ 179 if (rdsv3_trans_get_preferred(faddr)) { 180 conn->c_loopback = 1; 181 if (is_outgoing && trans->t_prefer_loopback) { 182 /* 183 * "outgoing" connection - and the transport 184 * says it wants the connection handled by the 185 * loopback transport. This is what TCP does. 186 */ 187 trans = &rdsv3_loop_transport; 188 } 189 } 190 191 conn->c_trans = trans; 192 193 ret = trans->conn_alloc(conn, gfp); 194 if (ret) { 195 kmem_cache_free(rdsv3_conn_slab, conn); 196 conn = ERR_PTR(ret); 197 goto out; 198 } 199 200 conn->c_state = RDSV3_CONN_DOWN; 201 conn->c_reconnect_jiffies = 0; 202 RDSV3_INIT_DELAYED_WORK(&conn->c_send_w, rdsv3_send_worker); 203 RDSV3_INIT_DELAYED_WORK(&conn->c_recv_w, rdsv3_recv_worker); 204 RDSV3_INIT_DELAYED_WORK(&conn->c_conn_w, rdsv3_connect_worker); 205 RDSV3_INIT_DELAYED_WORK(&conn->c_reap_w, rdsv3_reaper_worker); 206 RDSV3_INIT_WORK(&conn->c_down_w, rdsv3_shutdown_worker); 207 mutex_init(&conn->c_cm_lock, NULL, MUTEX_DRIVER, NULL); 208 conn->c_flags = 0; 209 210 RDSV3_DPRINTF2("__rdsv3_conn_create", 211 "allocated conn %p for %u.%u.%u.%u -> %u.%u.%u.%u over %s %s", 212 conn, NIPQUAD(laddr), NIPQUAD(faddr), 213 trans->t_name ? trans->t_name : "[unknown]", 214 is_outgoing ? "(outgoing)" : ""); 215 216 /* 217 * Since we ran without holding the conn lock, someone could 218 * have created the same conn (either normal or passive) in the 219 * interim. We check while holding the lock. If we won, we complete 220 * init and return our conn. If we lost, we rollback and return the 221 * other one. 222 */ 223 rw_enter(&rdsv3_conn_lock, RW_WRITER); 224 if (parent) { 225 /* Creating passive conn */ 226 if (parent->c_passive) { 227 trans->conn_free(conn->c_transport_data); 228 kmem_cache_free(rdsv3_conn_slab, conn); 229 conn = parent->c_passive; 230 } else { 231 parent->c_passive = conn; 232 rdsv3_cong_add_conn(conn); 233 rdsv3_conn_count++; 234 } 235 } else { 236 /* Creating normal conn */ 237 struct rdsv3_connection *found; 238 239 found = rdsv3_conn_lookup(laddr, faddr, &pos); 240 if (found) { 241 trans->conn_free(conn->c_transport_data); 242 kmem_cache_free(rdsv3_conn_slab, conn); 243 conn = found; 244 } else { 245 avl_insert(&rdsv3_conn_hash, conn, pos); 246 rdsv3_cong_add_conn(conn); 247 rdsv3_queue_delayed_work(rdsv3_wq, &conn->c_reap_w, 248 RDSV3_REAPER_WAIT_JIFFIES); 249 rdsv3_conn_count++; 250 } 251 } 252 253 rw_exit(&rdsv3_conn_lock); 254 255 RDSV3_DPRINTF2("__rdsv3_conn_create", "Return(conn: %p)", conn); 256 257 out: 258 return (conn); 259 } 260 261 struct rdsv3_connection * 262 rdsv3_conn_create(uint32_be_t laddr, uint32_be_t faddr, 263 struct rdsv3_transport *trans, int gfp) 264 { 265 return (__rdsv3_conn_create(laddr, faddr, trans, gfp, 0)); 266 } 267 268 struct rdsv3_connection * 269 rdsv3_conn_create_outgoing(uint32_be_t laddr, uint32_be_t faddr, 270 struct rdsv3_transport *trans, int gfp) 271 { 272 return (__rdsv3_conn_create(laddr, faddr, trans, gfp, 1)); 273 } 274 275 extern struct avl_tree rdsv3_conn_hash; 276 277 void 278 rdsv3_conn_shutdown(struct rdsv3_connection *conn) 279 { 280 RDSV3_DPRINTF2("rdsv3_conn_shutdown", "Enter(conn: %p)", conn); 281 282 /* shut it down unless it's down already */ 283 if (!rdsv3_conn_transition(conn, RDSV3_CONN_DOWN, RDSV3_CONN_DOWN)) { 284 /* 285 * Quiesce the connection mgmt handlers before we start tearing 286 * things down. We don't hold the mutex for the entire 287 * duration of the shutdown operation, else we may be 288 * deadlocking with the CM handler. Instead, the CM event 289 * handler is supposed to check for state DISCONNECTING 290 */ 291 mutex_enter(&conn->c_cm_lock); 292 if (!rdsv3_conn_transition(conn, RDSV3_CONN_UP, 293 RDSV3_CONN_DISCONNECTING) && 294 !rdsv3_conn_transition(conn, RDSV3_CONN_ERROR, 295 RDSV3_CONN_DISCONNECTING)) { 296 RDSV3_DPRINTF2("rdsv3_conn_shutdown", 297 "shutdown called in state %d", 298 atomic_get(&conn->c_state)); 299 rdsv3_conn_drop(conn); 300 mutex_exit(&conn->c_cm_lock); 301 return; 302 } 303 mutex_exit(&conn->c_cm_lock); 304 305 /* verify everybody's out of rds_send_xmit() */ 306 mutex_enter(&conn->c_send_lock); 307 while (atomic_get(&conn->c_senders)) { 308 mutex_exit(&conn->c_send_lock); 309 delay(1); 310 mutex_enter(&conn->c_send_lock); 311 } 312 313 conn->c_trans->conn_shutdown(conn); 314 rdsv3_conn_reset(conn); 315 mutex_exit(&conn->c_send_lock); 316 317 if (!rdsv3_conn_transition(conn, RDSV3_CONN_DISCONNECTING, 318 RDSV3_CONN_DOWN)) { 319 /* 320 * This can happen - eg when we're in the middle of 321 * tearing down the connection, and someone unloads 322 * the rds module. 323 * Quite reproduceable with loopback connections. 324 * Mostly harmless. 325 */ 326 #ifndef __lock_lint 327 RDSV3_DPRINTF2("rdsv3_conn_shutdown", 328 "failed to transition to state DOWN, " 329 "current statis is: %d", 330 atomic_get(&conn->c_state)); 331 rdsv3_conn_drop(conn); 332 #endif 333 return; 334 } 335 } 336 337 /* 338 * Then reconnect if it's still live. 339 * The passive side of an IB loopback connection is never added 340 * to the conn hash, so we never trigger a reconnect on this 341 * conn - the reconnect is always triggered by the active peer. 342 */ 343 rdsv3_cancel_delayed_work(&conn->c_conn_w); 344 345 { 346 struct rdsv3_conn_info_s conn_info; 347 348 conn_info.c_laddr = conn->c_laddr; 349 conn_info.c_faddr = conn->c_faddr; 350 if (avl_find(&rdsv3_conn_hash, &conn_info, NULL) == conn) 351 rdsv3_queue_reconnect(conn); 352 } 353 RDSV3_DPRINTF2("rdsv3_conn_shutdown", "Exit"); 354 } 355 356 /* 357 * Stop and free a connection. 358 */ 359 void 360 rdsv3_conn_destroy(struct rdsv3_connection *conn) 361 { 362 struct rdsv3_message *rm, *rtmp; 363 list_t to_be_dropped; 364 365 RDSV3_DPRINTF4("rdsv3_conn_destroy", 366 "freeing conn %p for %u.%u.%u.%u -> %u.%u.%u.%u", 367 conn, NIPQUAD(conn->c_laddr), NIPQUAD(conn->c_faddr)); 368 369 avl_remove(&rdsv3_conn_hash, conn); 370 371 rdsv3_cancel_delayed_work(&conn->c_reap_w); 372 rdsv3_cancel_delayed_work(&conn->c_send_w); 373 rdsv3_cancel_delayed_work(&conn->c_recv_w); 374 375 rdsv3_conn_shutdown(conn); 376 377 /* tear down queued messages */ 378 379 list_create(&to_be_dropped, sizeof (struct rdsv3_message), 380 offsetof(struct rdsv3_message, m_conn_item)); 381 382 RDSV3_FOR_EACH_LIST_NODE_SAFE(rm, rtmp, &conn->c_retrans, m_conn_item) { 383 list_remove_node(&rm->m_conn_item); 384 list_insert_tail(&to_be_dropped, rm); 385 } 386 387 RDSV3_FOR_EACH_LIST_NODE_SAFE(rm, rtmp, &conn->c_send_queue, 388 m_conn_item) { 389 list_remove_node(&rm->m_conn_item); 390 list_insert_tail(&to_be_dropped, rm); 391 } 392 393 RDSV3_FOR_EACH_LIST_NODE_SAFE(rm, rtmp, &to_be_dropped, m_conn_item) { 394 clear_bit(RDSV3_MSG_ON_CONN, &rm->m_flags); 395 list_remove_node(&rm->m_conn_item); 396 rdsv3_message_put(rm); 397 } 398 399 if (conn->c_xmit_rm) 400 rdsv3_message_put(conn->c_xmit_rm); 401 402 conn->c_trans->conn_free(conn->c_transport_data); 403 404 /* 405 * The congestion maps aren't freed up here. They're 406 * freed by rdsv3_cong_exit() after all the connections 407 * have been freed. 408 */ 409 rdsv3_cong_remove_conn(conn); 410 411 ASSERT(list_is_empty(&conn->c_retrans)); 412 kmem_cache_free(rdsv3_conn_slab, conn); 413 414 rdsv3_conn_count--; 415 } 416 417 /* ARGSUSED */ 418 static void 419 rdsv3_conn_message_info(struct rsock *sock, unsigned int len, 420 struct rdsv3_info_iterator *iter, 421 struct rdsv3_info_lengths *lens, 422 int want_send) 423 { 424 struct list *list; 425 struct rdsv3_connection *conn; 426 struct rdsv3_message *rm; 427 unsigned int total = 0; 428 429 RDSV3_DPRINTF4("rdsv3_conn_message_info", "Enter"); 430 431 len /= sizeof (struct rds_info_message); 432 433 rw_enter(&rdsv3_conn_lock, RW_READER); 434 435 if (avl_is_empty(&rdsv3_conn_hash)) { 436 /* no connections */ 437 rw_exit(&rdsv3_conn_lock); 438 return; 439 } 440 441 conn = (struct rdsv3_connection *)avl_first(&rdsv3_conn_hash); 442 443 do { 444 if (want_send) 445 list = &conn->c_send_queue; 446 else 447 list = &conn->c_retrans; 448 449 mutex_enter(&conn->c_lock); 450 451 /* XXX too lazy to maintain counts.. */ 452 RDSV3_FOR_EACH_LIST_NODE(rm, list, m_conn_item) { 453 total++; 454 if (total <= len) 455 rdsv3_inc_info_copy(&rm->m_inc, iter, 456 conn->c_laddr, conn->c_faddr, 0); 457 } 458 459 mutex_exit(&conn->c_lock); 460 461 conn = AVL_NEXT(&rdsv3_conn_hash, conn); 462 } while (conn != NULL); 463 rw_exit(&rdsv3_conn_lock); 464 465 lens->nr = total; 466 lens->each = sizeof (struct rds_info_message); 467 468 RDSV3_DPRINTF4("rdsv3_conn_message_info", "Return"); 469 } 470 471 static void 472 rdsv3_conn_message_info_send(struct rsock *sock, unsigned int len, 473 struct rdsv3_info_iterator *iter, 474 struct rdsv3_info_lengths *lens) 475 { 476 rdsv3_conn_message_info(sock, len, iter, lens, 1); 477 } 478 479 static void 480 rdsv3_conn_message_info_retrans(struct rsock *sock, 481 unsigned int len, 482 struct rdsv3_info_iterator *iter, 483 struct rdsv3_info_lengths *lens) 484 { 485 rdsv3_conn_message_info(sock, len, iter, lens, 0); 486 } 487 488 /* ARGSUSED */ 489 void 490 rdsv3_for_each_conn_info(struct rsock *sock, unsigned int len, 491 struct rdsv3_info_iterator *iter, 492 struct rdsv3_info_lengths *lens, 493 int (*visitor)(struct rdsv3_connection *, void *), 494 size_t item_len) 495 { 496 uint8_t *buffer; 497 struct rdsv3_connection *conn; 498 499 rw_enter(&rdsv3_conn_lock, RW_READER); 500 501 lens->nr = 0; 502 lens->each = item_len; 503 504 if (avl_is_empty(&rdsv3_conn_hash)) { 505 /* no connections */ 506 rw_exit(&rdsv3_conn_lock); 507 return; 508 } 509 510 /* allocate a little extra as this can get cast to a uint64_t */ 511 buffer = kmem_zalloc(item_len + 8, KM_SLEEP); 512 513 conn = (struct rdsv3_connection *)avl_first(&rdsv3_conn_hash); 514 515 do { 516 /* XXX no c_lock usage.. */ 517 if (visitor(conn, buffer)) { 518 /* 519 * We copy as much as we can fit in the buffer, 520 * but we count all items so that the caller 521 * can resize the buffer. 522 */ 523 if (len >= item_len) { 524 RDSV3_DPRINTF4("rdsv3_for_each_conn_info", 525 "buffer: %p iter: %p bytes: %d", buffer, 526 iter->addr + iter->offset, item_len); 527 rdsv3_info_copy(iter, buffer, item_len); 528 len -= item_len; 529 } 530 lens->nr++; 531 } 532 conn = AVL_NEXT(&rdsv3_conn_hash, conn); 533 } while (conn != NULL); 534 rw_exit(&rdsv3_conn_lock); 535 536 kmem_free(buffer, item_len + 8); 537 } 538 539 static int 540 rdsv3_conn_info_visitor(struct rdsv3_connection *conn, void *buffer) 541 { 542 struct rds_info_connection *cinfo = buffer; 543 544 cinfo->next_tx_seq = conn->c_next_tx_seq; 545 cinfo->next_rx_seq = conn->c_next_rx_seq; 546 cinfo->laddr = conn->c_laddr; 547 cinfo->faddr = conn->c_faddr; 548 (void) strncpy((char *)cinfo->transport, conn->c_trans->t_name, 549 sizeof (cinfo->transport)); 550 cinfo->flags = 0; 551 552 rdsv3_conn_info_set(cinfo->flags, 553 MUTEX_HELD(&conn->c_send_lock), SENDING); 554 555 /* XXX Future: return the state rather than these funky bits */ 556 rdsv3_conn_info_set(cinfo->flags, 557 atomic_get(&conn->c_state) == RDSV3_CONN_CONNECTING, 558 CONNECTING); 559 rdsv3_conn_info_set(cinfo->flags, 560 atomic_get(&conn->c_state) == RDSV3_CONN_UP, 561 CONNECTED); 562 return (1); 563 } 564 565 static void 566 rdsv3_conn_info(struct rsock *sock, unsigned int len, 567 struct rdsv3_info_iterator *iter, struct rdsv3_info_lengths *lens) 568 { 569 rdsv3_for_each_conn_info(sock, len, iter, lens, 570 rdsv3_conn_info_visitor, sizeof (struct rds_info_connection)); 571 } 572 573 int 574 rdsv3_conn_init() 575 { 576 RDSV3_DPRINTF4("rdsv3_conn_init", "Enter"); 577 578 rdsv3_conn_slab = kmem_cache_create("rdsv3_connection", 579 sizeof (struct rdsv3_connection), 0, rdsv3_conn_constructor, 580 rdsv3_conn_destructor, NULL, NULL, NULL, 0); 581 if (!rdsv3_conn_slab) { 582 RDSV3_DPRINTF2("rdsv3_conn_init", 583 "kmem_cache_create(rdsv3_conn_slab) failed"); 584 return (-ENOMEM); 585 } 586 587 avl_create(&rdsv3_conn_hash, rdsv3_conn_compare, 588 sizeof (struct rdsv3_connection), offsetof(struct rdsv3_connection, 589 c_hash_node)); 590 591 rw_init(&rdsv3_conn_lock, NULL, RW_DRIVER, NULL); 592 593 rdsv3_loop_init(); 594 595 rdsv3_info_register_func(RDS_INFO_CONNECTIONS, rdsv3_conn_info); 596 rdsv3_info_register_func(RDS_INFO_SEND_MESSAGES, 597 rdsv3_conn_message_info_send); 598 rdsv3_info_register_func(RDS_INFO_RETRANS_MESSAGES, 599 rdsv3_conn_message_info_retrans); 600 601 RDSV3_DPRINTF4("rdsv3_conn_init", "Return"); 602 603 return (0); 604 } 605 606 void 607 rdsv3_conn_exit() 608 { 609 RDSV3_DPRINTF4("rdsv3_conn_exit", "Enter"); 610 611 rdsv3_loop_exit(); 612 613 rw_destroy(&rdsv3_conn_lock); 614 avl_destroy(&rdsv3_conn_hash); 615 616 ASSERT(rdsv3_conn_slab); 617 kmem_cache_destroy(rdsv3_conn_slab); 618 619 RDSV3_DPRINTF4("rdsv3_conn_exit", "Return"); 620 } 621 622 /* 623 * Force a disconnect 624 */ 625 void 626 rdsv3_conn_drop(struct rdsv3_connection *conn) 627 { 628 conn->c_state = RDSV3_CONN_ERROR; 629 rdsv3_queue_work(rdsv3_wq, &conn->c_down_w); 630 } 631