100e0f34cSAndy Grover /* 200e0f34cSAndy Grover * Copyright (c) 2006 Oracle. All rights reserved. 300e0f34cSAndy Grover * 400e0f34cSAndy Grover * This software is available to you under a choice of one of two 500e0f34cSAndy Grover * licenses. You may choose to be licensed under the terms of the GNU 600e0f34cSAndy Grover * General Public License (GPL) Version 2, available from the file 700e0f34cSAndy Grover * COPYING in the main directory of this source tree, or the 800e0f34cSAndy Grover * OpenIB.org BSD license below: 900e0f34cSAndy Grover * 1000e0f34cSAndy Grover * Redistribution and use in source and binary forms, with or 1100e0f34cSAndy Grover * without modification, are permitted provided that the following 1200e0f34cSAndy Grover * conditions are met: 1300e0f34cSAndy Grover * 1400e0f34cSAndy Grover * - Redistributions of source code must retain the above 1500e0f34cSAndy Grover * copyright notice, this list of conditions and the following 1600e0f34cSAndy Grover * disclaimer. 1700e0f34cSAndy Grover * 1800e0f34cSAndy Grover * - Redistributions in binary form must reproduce the above 1900e0f34cSAndy Grover * copyright notice, this list of conditions and the following 2000e0f34cSAndy Grover * disclaimer in the documentation and/or other materials 2100e0f34cSAndy Grover * provided with the distribution. 2200e0f34cSAndy Grover * 2300e0f34cSAndy Grover * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2400e0f34cSAndy Grover * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2500e0f34cSAndy Grover * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2600e0f34cSAndy Grover * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2700e0f34cSAndy Grover * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2800e0f34cSAndy Grover * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2900e0f34cSAndy Grover * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3000e0f34cSAndy Grover * SOFTWARE. 3100e0f34cSAndy Grover * 3200e0f34cSAndy Grover */ 3300e0f34cSAndy Grover #include <linux/kernel.h> 3400e0f34cSAndy Grover #include <linux/random.h> 3500e0f34cSAndy Grover 3600e0f34cSAndy Grover #include "rds.h" 3700e0f34cSAndy Grover 3800e0f34cSAndy Grover /* 3900e0f34cSAndy Grover * All of connection management is simplified by serializing it through 4000e0f34cSAndy Grover * work queues that execute in a connection managing thread. 4100e0f34cSAndy Grover * 4200e0f34cSAndy Grover * TCP wants to send acks through sendpage() in response to data_ready(), 4300e0f34cSAndy Grover * but it needs a process context to do so. 4400e0f34cSAndy Grover * 4500e0f34cSAndy Grover * The receive paths need to allocate but can't drop packets (!) so we have 4600e0f34cSAndy Grover * a thread around to block allocating if the receive fast path sees an 4700e0f34cSAndy Grover * allocation failure. 4800e0f34cSAndy Grover */ 4900e0f34cSAndy Grover 5000e0f34cSAndy Grover /* Grand Unified Theory of connection life cycle: 5100e0f34cSAndy Grover * At any point in time, the connection can be in one of these states: 5200e0f34cSAndy Grover * DOWN, CONNECTING, UP, DISCONNECTING, ERROR 5300e0f34cSAndy Grover * 5400e0f34cSAndy Grover * The following transitions are possible: 5500e0f34cSAndy Grover * ANY -> ERROR 5600e0f34cSAndy Grover * UP -> DISCONNECTING 5700e0f34cSAndy Grover * ERROR -> DISCONNECTING 5800e0f34cSAndy Grover * DISCONNECTING -> DOWN 5900e0f34cSAndy Grover * DOWN -> CONNECTING 6000e0f34cSAndy Grover * CONNECTING -> UP 6100e0f34cSAndy Grover * 6200e0f34cSAndy Grover * Transition to state DISCONNECTING/DOWN: 6300e0f34cSAndy Grover * - Inside the shutdown worker; synchronizes with xmit path 6400e0f34cSAndy Grover * through c_send_lock, and with connection management callbacks 6500e0f34cSAndy Grover * via c_cm_lock. 6600e0f34cSAndy Grover * 6700e0f34cSAndy Grover * For receive callbacks, we rely on the underlying transport 6800e0f34cSAndy Grover * (TCP, IB/RDMA) to provide the necessary synchronisation. 6900e0f34cSAndy Grover */ 7000e0f34cSAndy Grover struct workqueue_struct *rds_wq; 7100e0f34cSAndy Grover 7200e0f34cSAndy Grover void rds_connect_complete(struct rds_connection *conn) 7300e0f34cSAndy Grover { 7400e0f34cSAndy Grover if (!rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_UP)) { 7500e0f34cSAndy Grover printk(KERN_WARNING "%s: Cannot transition to state UP, " 7600e0f34cSAndy Grover "current state is %d\n", 7700e0f34cSAndy Grover __func__, 7800e0f34cSAndy Grover atomic_read(&conn->c_state)); 7900e0f34cSAndy Grover atomic_set(&conn->c_state, RDS_CONN_ERROR); 8000e0f34cSAndy Grover queue_work(rds_wq, &conn->c_down_w); 8100e0f34cSAndy Grover return; 8200e0f34cSAndy Grover } 8300e0f34cSAndy Grover 8400e0f34cSAndy Grover rdsdebug("conn %p for %pI4 to %pI4 complete\n", 8500e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr); 8600e0f34cSAndy Grover 8700e0f34cSAndy Grover conn->c_reconnect_jiffies = 0; 8800e0f34cSAndy Grover set_bit(0, &conn->c_map_queued); 8900e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_send_w, 0); 9000e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_recv_w, 0); 9100e0f34cSAndy Grover } 9200e0f34cSAndy Grover 9300e0f34cSAndy Grover /* 9400e0f34cSAndy Grover * This random exponential backoff is relied on to eventually resolve racing 9500e0f34cSAndy Grover * connects. 9600e0f34cSAndy Grover * 9700e0f34cSAndy Grover * If connect attempts race then both parties drop both connections and come 9800e0f34cSAndy Grover * here to wait for a random amount of time before trying again. Eventually 9900e0f34cSAndy Grover * the backoff range will be so much greater than the time it takes to 10000e0f34cSAndy Grover * establish a connection that one of the pair will establish the connection 10100e0f34cSAndy Grover * before the other's random delay fires. 10200e0f34cSAndy Grover * 10300e0f34cSAndy Grover * Connection attempts that arrive while a connection is already established 10400e0f34cSAndy Grover * are also considered to be racing connects. This lets a connection from 10500e0f34cSAndy Grover * a rebooted machine replace an existing stale connection before the transport 10600e0f34cSAndy Grover * notices that the connection has failed. 10700e0f34cSAndy Grover * 10800e0f34cSAndy Grover * We should *always* start with a random backoff; otherwise a broken connection 10900e0f34cSAndy Grover * will always take several iterations to be re-established. 11000e0f34cSAndy Grover */ 11100e0f34cSAndy Grover static void rds_queue_reconnect(struct rds_connection *conn) 11200e0f34cSAndy Grover { 11300e0f34cSAndy Grover unsigned long rand; 11400e0f34cSAndy Grover 11500e0f34cSAndy Grover rdsdebug("conn %p for %pI4 to %pI4 reconnect jiffies %lu\n", 11600e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr, 11700e0f34cSAndy Grover conn->c_reconnect_jiffies); 11800e0f34cSAndy Grover 11900e0f34cSAndy Grover set_bit(RDS_RECONNECT_PENDING, &conn->c_flags); 12000e0f34cSAndy Grover if (conn->c_reconnect_jiffies == 0) { 12100e0f34cSAndy Grover conn->c_reconnect_jiffies = rds_sysctl_reconnect_min_jiffies; 12200e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_conn_w, 0); 12300e0f34cSAndy Grover return; 12400e0f34cSAndy Grover } 12500e0f34cSAndy Grover 12600e0f34cSAndy Grover get_random_bytes(&rand, sizeof(rand)); 12700e0f34cSAndy Grover rdsdebug("%lu delay %lu ceil conn %p for %pI4 -> %pI4\n", 12800e0f34cSAndy Grover rand % conn->c_reconnect_jiffies, conn->c_reconnect_jiffies, 12900e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr); 13000e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_conn_w, 13100e0f34cSAndy Grover rand % conn->c_reconnect_jiffies); 13200e0f34cSAndy Grover 13300e0f34cSAndy Grover conn->c_reconnect_jiffies = min(conn->c_reconnect_jiffies * 2, 13400e0f34cSAndy Grover rds_sysctl_reconnect_max_jiffies); 13500e0f34cSAndy Grover } 13600e0f34cSAndy Grover 13700e0f34cSAndy Grover void rds_connect_worker(struct work_struct *work) 13800e0f34cSAndy Grover { 13900e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_conn_w.work); 14000e0f34cSAndy Grover int ret; 14100e0f34cSAndy Grover 14200e0f34cSAndy Grover clear_bit(RDS_RECONNECT_PENDING, &conn->c_flags); 14300e0f34cSAndy Grover if (rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) { 14400e0f34cSAndy Grover ret = conn->c_trans->conn_connect(conn); 14500e0f34cSAndy Grover rdsdebug("conn %p for %pI4 to %pI4 dispatched, ret %d\n", 14600e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr, ret); 14700e0f34cSAndy Grover 14800e0f34cSAndy Grover if (ret) { 14900e0f34cSAndy Grover if (rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_DOWN)) 15000e0f34cSAndy Grover rds_queue_reconnect(conn); 15100e0f34cSAndy Grover else 15200e0f34cSAndy Grover rds_conn_error(conn, "RDS: connect failed\n"); 15300e0f34cSAndy Grover } 15400e0f34cSAndy Grover } 15500e0f34cSAndy Grover } 15600e0f34cSAndy Grover 15700e0f34cSAndy Grover void rds_shutdown_worker(struct work_struct *work) 15800e0f34cSAndy Grover { 15900e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_down_w); 16000e0f34cSAndy Grover 16100e0f34cSAndy Grover /* shut it down unless it's down already */ 16200e0f34cSAndy Grover if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) { 16300e0f34cSAndy Grover /* 16400e0f34cSAndy Grover * Quiesce the connection mgmt handlers before we start tearing 16500e0f34cSAndy Grover * things down. We don't hold the mutex for the entire 16600e0f34cSAndy Grover * duration of the shutdown operation, else we may be 16700e0f34cSAndy Grover * deadlocking with the CM handler. Instead, the CM event 16800e0f34cSAndy Grover * handler is supposed to check for state DISCONNECTING 16900e0f34cSAndy Grover */ 17000e0f34cSAndy Grover mutex_lock(&conn->c_cm_lock); 17100e0f34cSAndy Grover if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING) 17200e0f34cSAndy Grover && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) { 17300e0f34cSAndy Grover rds_conn_error(conn, "shutdown called in state %d\n", 17400e0f34cSAndy Grover atomic_read(&conn->c_state)); 17500e0f34cSAndy Grover mutex_unlock(&conn->c_cm_lock); 17600e0f34cSAndy Grover return; 17700e0f34cSAndy Grover } 17800e0f34cSAndy Grover mutex_unlock(&conn->c_cm_lock); 17900e0f34cSAndy Grover 18000e0f34cSAndy Grover mutex_lock(&conn->c_send_lock); 18100e0f34cSAndy Grover conn->c_trans->conn_shutdown(conn); 18200e0f34cSAndy Grover rds_conn_reset(conn); 18300e0f34cSAndy Grover mutex_unlock(&conn->c_send_lock); 18400e0f34cSAndy Grover 18500e0f34cSAndy Grover if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) { 18600e0f34cSAndy Grover /* This can happen - eg when we're in the middle of tearing 18700e0f34cSAndy Grover * down the connection, and someone unloads the rds module. 18800e0f34cSAndy Grover * Quite reproduceable with loopback connections. 18900e0f34cSAndy Grover * Mostly harmless. 19000e0f34cSAndy Grover */ 19100e0f34cSAndy Grover rds_conn_error(conn, 19200e0f34cSAndy Grover "%s: failed to transition to state DOWN, " 19300e0f34cSAndy Grover "current state is %d\n", 19400e0f34cSAndy Grover __func__, 19500e0f34cSAndy Grover atomic_read(&conn->c_state)); 19600e0f34cSAndy Grover return; 19700e0f34cSAndy Grover } 19800e0f34cSAndy Grover } 19900e0f34cSAndy Grover 20000e0f34cSAndy Grover /* Then reconnect if it's still live. 20100e0f34cSAndy Grover * The passive side of an IB loopback connection is never added 20200e0f34cSAndy Grover * to the conn hash, so we never trigger a reconnect on this 20300e0f34cSAndy Grover * conn - the reconnect is always triggered by the active peer. */ 20400e0f34cSAndy Grover cancel_delayed_work(&conn->c_conn_w); 20500e0f34cSAndy Grover if (!hlist_unhashed(&conn->c_hash_node)) 20600e0f34cSAndy Grover rds_queue_reconnect(conn); 20700e0f34cSAndy Grover } 20800e0f34cSAndy Grover 20900e0f34cSAndy Grover void rds_send_worker(struct work_struct *work) 21000e0f34cSAndy Grover { 21100e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_send_w.work); 21200e0f34cSAndy Grover int ret; 21300e0f34cSAndy Grover 21400e0f34cSAndy Grover if (rds_conn_state(conn) == RDS_CONN_UP) { 21500e0f34cSAndy Grover ret = rds_send_xmit(conn); 21600e0f34cSAndy Grover rdsdebug("conn %p ret %d\n", conn, ret); 21700e0f34cSAndy Grover switch (ret) { 21800e0f34cSAndy Grover case -EAGAIN: 21900e0f34cSAndy Grover rds_stats_inc(s_send_immediate_retry); 22000e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_send_w, 0); 22100e0f34cSAndy Grover break; 22200e0f34cSAndy Grover case -ENOMEM: 22300e0f34cSAndy Grover rds_stats_inc(s_send_delayed_retry); 22400e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_send_w, 2); 22500e0f34cSAndy Grover default: 22600e0f34cSAndy Grover break; 22700e0f34cSAndy Grover } 22800e0f34cSAndy Grover } 22900e0f34cSAndy Grover } 23000e0f34cSAndy Grover 23100e0f34cSAndy Grover void rds_recv_worker(struct work_struct *work) 23200e0f34cSAndy Grover { 23300e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_recv_w.work); 23400e0f34cSAndy Grover int ret; 23500e0f34cSAndy Grover 23600e0f34cSAndy Grover if (rds_conn_state(conn) == RDS_CONN_UP) { 23700e0f34cSAndy Grover ret = conn->c_trans->recv(conn); 23800e0f34cSAndy Grover rdsdebug("conn %p ret %d\n", conn, ret); 23900e0f34cSAndy Grover switch (ret) { 24000e0f34cSAndy Grover case -EAGAIN: 24100e0f34cSAndy Grover rds_stats_inc(s_recv_immediate_retry); 24200e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_recv_w, 0); 24300e0f34cSAndy Grover break; 24400e0f34cSAndy Grover case -ENOMEM: 24500e0f34cSAndy Grover rds_stats_inc(s_recv_delayed_retry); 24600e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_recv_w, 2); 24700e0f34cSAndy Grover default: 24800e0f34cSAndy Grover break; 24900e0f34cSAndy Grover } 25000e0f34cSAndy Grover } 25100e0f34cSAndy Grover } 25200e0f34cSAndy Grover 25300e0f34cSAndy Grover void rds_threads_exit(void) 25400e0f34cSAndy Grover { 25500e0f34cSAndy Grover destroy_workqueue(rds_wq); 25600e0f34cSAndy Grover } 25700e0f34cSAndy Grover 25800e0f34cSAndy Grover int __init rds_threads_init(void) 25900e0f34cSAndy Grover { 26000e0f34cSAndy Grover rds_wq = create_singlethread_workqueue("krdsd"); 26100e0f34cSAndy Grover if (rds_wq == NULL) 26200e0f34cSAndy Grover return -ENOMEM; 26300e0f34cSAndy Grover 26400e0f34cSAndy Grover return 0; 26500e0f34cSAndy Grover } 266