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> 35bc3b2d7fSPaul Gortmaker #include <linux/export.h> 3600e0f34cSAndy Grover 3700e0f34cSAndy Grover #include "rds.h" 3800e0f34cSAndy Grover 3900e0f34cSAndy Grover /* 4000e0f34cSAndy Grover * All of connection management is simplified by serializing it through 4100e0f34cSAndy Grover * work queues that execute in a connection managing thread. 4200e0f34cSAndy Grover * 4300e0f34cSAndy Grover * TCP wants to send acks through sendpage() in response to data_ready(), 4400e0f34cSAndy Grover * but it needs a process context to do so. 4500e0f34cSAndy Grover * 4600e0f34cSAndy Grover * The receive paths need to allocate but can't drop packets (!) so we have 4700e0f34cSAndy Grover * a thread around to block allocating if the receive fast path sees an 4800e0f34cSAndy Grover * allocation failure. 4900e0f34cSAndy Grover */ 5000e0f34cSAndy Grover 5100e0f34cSAndy Grover /* Grand Unified Theory of connection life cycle: 5200e0f34cSAndy Grover * At any point in time, the connection can be in one of these states: 5300e0f34cSAndy Grover * DOWN, CONNECTING, UP, DISCONNECTING, ERROR 5400e0f34cSAndy Grover * 5500e0f34cSAndy Grover * The following transitions are possible: 5600e0f34cSAndy Grover * ANY -> ERROR 5700e0f34cSAndy Grover * UP -> DISCONNECTING 5800e0f34cSAndy Grover * ERROR -> DISCONNECTING 5900e0f34cSAndy Grover * DISCONNECTING -> DOWN 6000e0f34cSAndy Grover * DOWN -> CONNECTING 6100e0f34cSAndy Grover * CONNECTING -> UP 6200e0f34cSAndy Grover * 6300e0f34cSAndy Grover * Transition to state DISCONNECTING/DOWN: 6400e0f34cSAndy Grover * - Inside the shutdown worker; synchronizes with xmit path 650f4b1c7eSZach Brown * through RDS_IN_XMIT, and with connection management callbacks 6600e0f34cSAndy Grover * via c_cm_lock. 6700e0f34cSAndy Grover * 6800e0f34cSAndy Grover * For receive callbacks, we rely on the underlying transport 6900e0f34cSAndy Grover * (TCP, IB/RDMA) to provide the necessary synchronisation. 7000e0f34cSAndy Grover */ 7100e0f34cSAndy Grover struct workqueue_struct *rds_wq; 72616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_wq); 7300e0f34cSAndy Grover 749c79440eSSowmini Varadhan void rds_connect_path_complete(struct rds_connection *conn, int curr) 7500e0f34cSAndy Grover { 769c79440eSSowmini Varadhan if (!rds_conn_transition(conn, curr, RDS_CONN_UP)) { 7700e0f34cSAndy Grover printk(KERN_WARNING "%s: Cannot transition to state UP, " 7800e0f34cSAndy Grover "current state is %d\n", 7900e0f34cSAndy Grover __func__, 8000e0f34cSAndy Grover atomic_read(&conn->c_state)); 81310886ddSHerton R. Krzesinski rds_conn_drop(conn); 8200e0f34cSAndy Grover return; 8300e0f34cSAndy Grover } 8400e0f34cSAndy Grover 8500e0f34cSAndy Grover rdsdebug("conn %p for %pI4 to %pI4 complete\n", 8600e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr); 8700e0f34cSAndy Grover 8800e0f34cSAndy Grover conn->c_reconnect_jiffies = 0; 8900e0f34cSAndy Grover set_bit(0, &conn->c_map_queued); 9000e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_send_w, 0); 9100e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_recv_w, 0); 9200e0f34cSAndy Grover } 939c79440eSSowmini Varadhan EXPORT_SYMBOL_GPL(rds_connect_path_complete); 949c79440eSSowmini Varadhan 959c79440eSSowmini Varadhan void rds_connect_complete(struct rds_connection *conn) 969c79440eSSowmini Varadhan { 979c79440eSSowmini Varadhan rds_connect_path_complete(conn, RDS_CONN_CONNECTING); 989c79440eSSowmini Varadhan } 99616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_connect_complete); 10000e0f34cSAndy Grover 10100e0f34cSAndy Grover /* 10200e0f34cSAndy Grover * This random exponential backoff is relied on to eventually resolve racing 10300e0f34cSAndy Grover * connects. 10400e0f34cSAndy Grover * 10500e0f34cSAndy Grover * If connect attempts race then both parties drop both connections and come 10600e0f34cSAndy Grover * here to wait for a random amount of time before trying again. Eventually 10700e0f34cSAndy Grover * the backoff range will be so much greater than the time it takes to 10800e0f34cSAndy Grover * establish a connection that one of the pair will establish the connection 10900e0f34cSAndy Grover * before the other's random delay fires. 11000e0f34cSAndy Grover * 11100e0f34cSAndy Grover * Connection attempts that arrive while a connection is already established 11200e0f34cSAndy Grover * are also considered to be racing connects. This lets a connection from 11300e0f34cSAndy Grover * a rebooted machine replace an existing stale connection before the transport 11400e0f34cSAndy Grover * notices that the connection has failed. 11500e0f34cSAndy Grover * 11600e0f34cSAndy Grover * We should *always* start with a random backoff; otherwise a broken connection 11700e0f34cSAndy Grover * will always take several iterations to be re-established. 11800e0f34cSAndy Grover */ 1192dc39357SAndy Grover void rds_queue_reconnect(struct rds_connection *conn) 12000e0f34cSAndy Grover { 12100e0f34cSAndy Grover unsigned long rand; 12200e0f34cSAndy Grover 12300e0f34cSAndy Grover rdsdebug("conn %p for %pI4 to %pI4 reconnect jiffies %lu\n", 12400e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr, 12500e0f34cSAndy Grover conn->c_reconnect_jiffies); 12600e0f34cSAndy Grover 12700e0f34cSAndy Grover set_bit(RDS_RECONNECT_PENDING, &conn->c_flags); 12800e0f34cSAndy Grover if (conn->c_reconnect_jiffies == 0) { 12900e0f34cSAndy Grover conn->c_reconnect_jiffies = rds_sysctl_reconnect_min_jiffies; 13000e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_conn_w, 0); 13100e0f34cSAndy Grover return; 13200e0f34cSAndy Grover } 13300e0f34cSAndy Grover 13400e0f34cSAndy Grover get_random_bytes(&rand, sizeof(rand)); 13500e0f34cSAndy Grover rdsdebug("%lu delay %lu ceil conn %p for %pI4 -> %pI4\n", 13600e0f34cSAndy Grover rand % conn->c_reconnect_jiffies, conn->c_reconnect_jiffies, 13700e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr); 13800e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_conn_w, 13900e0f34cSAndy Grover rand % conn->c_reconnect_jiffies); 14000e0f34cSAndy Grover 14100e0f34cSAndy Grover conn->c_reconnect_jiffies = min(conn->c_reconnect_jiffies * 2, 14200e0f34cSAndy Grover rds_sysctl_reconnect_max_jiffies); 14300e0f34cSAndy Grover } 14400e0f34cSAndy Grover 14500e0f34cSAndy Grover void rds_connect_worker(struct work_struct *work) 14600e0f34cSAndy Grover { 14700e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_conn_w.work); 14800e0f34cSAndy Grover int ret; 14900e0f34cSAndy Grover 15000e0f34cSAndy Grover clear_bit(RDS_RECONNECT_PENDING, &conn->c_flags); 15100e0f34cSAndy Grover if (rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) { 15200e0f34cSAndy Grover ret = conn->c_trans->conn_connect(conn); 15300e0f34cSAndy Grover rdsdebug("conn %p for %pI4 to %pI4 dispatched, ret %d\n", 15400e0f34cSAndy Grover conn, &conn->c_laddr, &conn->c_faddr, ret); 15500e0f34cSAndy Grover 15600e0f34cSAndy Grover if (ret) { 15700e0f34cSAndy Grover if (rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_DOWN)) 15800e0f34cSAndy Grover rds_queue_reconnect(conn); 15900e0f34cSAndy Grover else 16000e0f34cSAndy Grover rds_conn_error(conn, "RDS: connect failed\n"); 16100e0f34cSAndy Grover } 16200e0f34cSAndy Grover } 16300e0f34cSAndy Grover } 16400e0f34cSAndy Grover 16500e0f34cSAndy Grover void rds_send_worker(struct work_struct *work) 16600e0f34cSAndy Grover { 16700e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_send_w.work); 16800e0f34cSAndy Grover int ret; 16900e0f34cSAndy Grover 17000e0f34cSAndy Grover if (rds_conn_state(conn) == RDS_CONN_UP) { 171db6526dcSSantosh Shilimkar clear_bit(RDS_LL_SEND_FULL, &conn->c_flags); 17200e0f34cSAndy Grover ret = rds_send_xmit(conn); 173db6526dcSSantosh Shilimkar cond_resched(); 17400e0f34cSAndy Grover rdsdebug("conn %p ret %d\n", conn, ret); 17500e0f34cSAndy Grover switch (ret) { 17600e0f34cSAndy Grover case -EAGAIN: 17700e0f34cSAndy Grover rds_stats_inc(s_send_immediate_retry); 17800e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_send_w, 0); 17900e0f34cSAndy Grover break; 18000e0f34cSAndy Grover case -ENOMEM: 18100e0f34cSAndy Grover rds_stats_inc(s_send_delayed_retry); 18200e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_send_w, 2); 18300e0f34cSAndy Grover default: 18400e0f34cSAndy Grover break; 18500e0f34cSAndy Grover } 18600e0f34cSAndy Grover } 18700e0f34cSAndy Grover } 18800e0f34cSAndy Grover 18900e0f34cSAndy Grover void rds_recv_worker(struct work_struct *work) 19000e0f34cSAndy Grover { 19100e0f34cSAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_recv_w.work); 19200e0f34cSAndy Grover int ret; 19300e0f34cSAndy Grover 19400e0f34cSAndy Grover if (rds_conn_state(conn) == RDS_CONN_UP) { 19500e0f34cSAndy Grover ret = conn->c_trans->recv(conn); 19600e0f34cSAndy Grover rdsdebug("conn %p ret %d\n", conn, ret); 19700e0f34cSAndy Grover switch (ret) { 19800e0f34cSAndy Grover case -EAGAIN: 19900e0f34cSAndy Grover rds_stats_inc(s_recv_immediate_retry); 20000e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_recv_w, 0); 20100e0f34cSAndy Grover break; 20200e0f34cSAndy Grover case -ENOMEM: 20300e0f34cSAndy Grover rds_stats_inc(s_recv_delayed_retry); 20400e0f34cSAndy Grover queue_delayed_work(rds_wq, &conn->c_recv_w, 2); 20500e0f34cSAndy Grover default: 20600e0f34cSAndy Grover break; 20700e0f34cSAndy Grover } 20800e0f34cSAndy Grover } 20900e0f34cSAndy Grover } 21000e0f34cSAndy Grover 2112dc39357SAndy Grover void rds_shutdown_worker(struct work_struct *work) 2122dc39357SAndy Grover { 2132dc39357SAndy Grover struct rds_connection *conn = container_of(work, struct rds_connection, c_down_w); 2142dc39357SAndy Grover 2152dc39357SAndy Grover rds_conn_shutdown(conn); 2162dc39357SAndy Grover } 2172dc39357SAndy Grover 21800e0f34cSAndy Grover void rds_threads_exit(void) 21900e0f34cSAndy Grover { 22000e0f34cSAndy Grover destroy_workqueue(rds_wq); 22100e0f34cSAndy Grover } 22200e0f34cSAndy Grover 223ef87b7eaSZach Brown int rds_threads_init(void) 22400e0f34cSAndy Grover { 22580c51be5SZach Brown rds_wq = create_singlethread_workqueue("krdsd"); 2268690bfa1SAndy Grover if (!rds_wq) 22700e0f34cSAndy Grover return -ENOMEM; 22800e0f34cSAndy Grover 22900e0f34cSAndy Grover return 0; 23000e0f34cSAndy Grover } 231