xref: /openbmc/linux/net/rds/threads.c (revision bc3b2d7f)
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 
7400e0f34cSAndy Grover void rds_connect_complete(struct rds_connection *conn)
7500e0f34cSAndy Grover {
7600e0f34cSAndy Grover 	if (!rds_conn_transition(conn, RDS_CONN_CONNECTING, 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));
8100e0f34cSAndy Grover 		atomic_set(&conn->c_state, RDS_CONN_ERROR);
8200e0f34cSAndy Grover 		queue_work(rds_wq, &conn->c_down_w);
8300e0f34cSAndy Grover 		return;
8400e0f34cSAndy Grover 	}
8500e0f34cSAndy Grover 
8600e0f34cSAndy Grover 	rdsdebug("conn %p for %pI4 to %pI4 complete\n",
8700e0f34cSAndy Grover 	  conn, &conn->c_laddr, &conn->c_faddr);
8800e0f34cSAndy Grover 
8900e0f34cSAndy Grover 	conn->c_reconnect_jiffies = 0;
9000e0f34cSAndy Grover 	set_bit(0, &conn->c_map_queued);
9100e0f34cSAndy Grover 	queue_delayed_work(rds_wq, &conn->c_send_w, 0);
9200e0f34cSAndy Grover 	queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
9300e0f34cSAndy Grover }
94616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_connect_complete);
9500e0f34cSAndy Grover 
9600e0f34cSAndy Grover /*
9700e0f34cSAndy Grover  * This random exponential backoff is relied on to eventually resolve racing
9800e0f34cSAndy Grover  * connects.
9900e0f34cSAndy Grover  *
10000e0f34cSAndy Grover  * If connect attempts race then both parties drop both connections and come
10100e0f34cSAndy Grover  * here to wait for a random amount of time before trying again.  Eventually
10200e0f34cSAndy Grover  * the backoff range will be so much greater than the time it takes to
10300e0f34cSAndy Grover  * establish a connection that one of the pair will establish the connection
10400e0f34cSAndy Grover  * before the other's random delay fires.
10500e0f34cSAndy Grover  *
10600e0f34cSAndy Grover  * Connection attempts that arrive while a connection is already established
10700e0f34cSAndy Grover  * are also considered to be racing connects.  This lets a connection from
10800e0f34cSAndy Grover  * a rebooted machine replace an existing stale connection before the transport
10900e0f34cSAndy Grover  * notices that the connection has failed.
11000e0f34cSAndy Grover  *
11100e0f34cSAndy Grover  * We should *always* start with a random backoff; otherwise a broken connection
11200e0f34cSAndy Grover  * will always take several iterations to be re-established.
11300e0f34cSAndy Grover  */
1142dc39357SAndy Grover void rds_queue_reconnect(struct rds_connection *conn)
11500e0f34cSAndy Grover {
11600e0f34cSAndy Grover 	unsigned long rand;
11700e0f34cSAndy Grover 
11800e0f34cSAndy Grover 	rdsdebug("conn %p for %pI4 to %pI4 reconnect jiffies %lu\n",
11900e0f34cSAndy Grover 	  conn, &conn->c_laddr, &conn->c_faddr,
12000e0f34cSAndy Grover 	  conn->c_reconnect_jiffies);
12100e0f34cSAndy Grover 
12200e0f34cSAndy Grover 	set_bit(RDS_RECONNECT_PENDING, &conn->c_flags);
12300e0f34cSAndy Grover 	if (conn->c_reconnect_jiffies == 0) {
12400e0f34cSAndy Grover 		conn->c_reconnect_jiffies = rds_sysctl_reconnect_min_jiffies;
12500e0f34cSAndy Grover 		queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
12600e0f34cSAndy Grover 		return;
12700e0f34cSAndy Grover 	}
12800e0f34cSAndy Grover 
12900e0f34cSAndy Grover 	get_random_bytes(&rand, sizeof(rand));
13000e0f34cSAndy Grover 	rdsdebug("%lu delay %lu ceil conn %p for %pI4 -> %pI4\n",
13100e0f34cSAndy Grover 		 rand % conn->c_reconnect_jiffies, conn->c_reconnect_jiffies,
13200e0f34cSAndy Grover 		 conn, &conn->c_laddr, &conn->c_faddr);
13300e0f34cSAndy Grover 	queue_delayed_work(rds_wq, &conn->c_conn_w,
13400e0f34cSAndy Grover 			   rand % conn->c_reconnect_jiffies);
13500e0f34cSAndy Grover 
13600e0f34cSAndy Grover 	conn->c_reconnect_jiffies = min(conn->c_reconnect_jiffies * 2,
13700e0f34cSAndy Grover 					rds_sysctl_reconnect_max_jiffies);
13800e0f34cSAndy Grover }
13900e0f34cSAndy Grover 
14000e0f34cSAndy Grover void rds_connect_worker(struct work_struct *work)
14100e0f34cSAndy Grover {
14200e0f34cSAndy Grover 	struct rds_connection *conn = container_of(work, struct rds_connection, c_conn_w.work);
14300e0f34cSAndy Grover 	int ret;
14400e0f34cSAndy Grover 
14500e0f34cSAndy Grover 	clear_bit(RDS_RECONNECT_PENDING, &conn->c_flags);
14600e0f34cSAndy Grover 	if (rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
14700e0f34cSAndy Grover 		ret = conn->c_trans->conn_connect(conn);
14800e0f34cSAndy Grover 		rdsdebug("conn %p for %pI4 to %pI4 dispatched, ret %d\n",
14900e0f34cSAndy Grover 			conn, &conn->c_laddr, &conn->c_faddr, ret);
15000e0f34cSAndy Grover 
15100e0f34cSAndy Grover 		if (ret) {
15200e0f34cSAndy Grover 			if (rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_DOWN))
15300e0f34cSAndy Grover 				rds_queue_reconnect(conn);
15400e0f34cSAndy Grover 			else
15500e0f34cSAndy Grover 				rds_conn_error(conn, "RDS: connect failed\n");
15600e0f34cSAndy Grover 		}
15700e0f34cSAndy Grover 	}
15800e0f34cSAndy Grover }
15900e0f34cSAndy Grover 
16000e0f34cSAndy Grover void rds_send_worker(struct work_struct *work)
16100e0f34cSAndy Grover {
16200e0f34cSAndy Grover 	struct rds_connection *conn = container_of(work, struct rds_connection, c_send_w.work);
16300e0f34cSAndy Grover 	int ret;
16400e0f34cSAndy Grover 
16500e0f34cSAndy Grover 	if (rds_conn_state(conn) == RDS_CONN_UP) {
16600e0f34cSAndy Grover 		ret = rds_send_xmit(conn);
16700e0f34cSAndy Grover 		rdsdebug("conn %p ret %d\n", conn, ret);
16800e0f34cSAndy Grover 		switch (ret) {
16900e0f34cSAndy Grover 		case -EAGAIN:
17000e0f34cSAndy Grover 			rds_stats_inc(s_send_immediate_retry);
17100e0f34cSAndy Grover 			queue_delayed_work(rds_wq, &conn->c_send_w, 0);
17200e0f34cSAndy Grover 			break;
17300e0f34cSAndy Grover 		case -ENOMEM:
17400e0f34cSAndy Grover 			rds_stats_inc(s_send_delayed_retry);
17500e0f34cSAndy Grover 			queue_delayed_work(rds_wq, &conn->c_send_w, 2);
17600e0f34cSAndy Grover 		default:
17700e0f34cSAndy Grover 			break;
17800e0f34cSAndy Grover 		}
17900e0f34cSAndy Grover 	}
18000e0f34cSAndy Grover }
18100e0f34cSAndy Grover 
18200e0f34cSAndy Grover void rds_recv_worker(struct work_struct *work)
18300e0f34cSAndy Grover {
18400e0f34cSAndy Grover 	struct rds_connection *conn = container_of(work, struct rds_connection, c_recv_w.work);
18500e0f34cSAndy Grover 	int ret;
18600e0f34cSAndy Grover 
18700e0f34cSAndy Grover 	if (rds_conn_state(conn) == RDS_CONN_UP) {
18800e0f34cSAndy Grover 		ret = conn->c_trans->recv(conn);
18900e0f34cSAndy Grover 		rdsdebug("conn %p ret %d\n", conn, ret);
19000e0f34cSAndy Grover 		switch (ret) {
19100e0f34cSAndy Grover 		case -EAGAIN:
19200e0f34cSAndy Grover 			rds_stats_inc(s_recv_immediate_retry);
19300e0f34cSAndy Grover 			queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
19400e0f34cSAndy Grover 			break;
19500e0f34cSAndy Grover 		case -ENOMEM:
19600e0f34cSAndy Grover 			rds_stats_inc(s_recv_delayed_retry);
19700e0f34cSAndy Grover 			queue_delayed_work(rds_wq, &conn->c_recv_w, 2);
19800e0f34cSAndy Grover 		default:
19900e0f34cSAndy Grover 			break;
20000e0f34cSAndy Grover 		}
20100e0f34cSAndy Grover 	}
20200e0f34cSAndy Grover }
20300e0f34cSAndy Grover 
2042dc39357SAndy Grover void rds_shutdown_worker(struct work_struct *work)
2052dc39357SAndy Grover {
2062dc39357SAndy Grover 	struct rds_connection *conn = container_of(work, struct rds_connection, c_down_w);
2072dc39357SAndy Grover 
2082dc39357SAndy Grover 	rds_conn_shutdown(conn);
2092dc39357SAndy Grover }
2102dc39357SAndy Grover 
21100e0f34cSAndy Grover void rds_threads_exit(void)
21200e0f34cSAndy Grover {
21300e0f34cSAndy Grover 	destroy_workqueue(rds_wq);
21400e0f34cSAndy Grover }
21500e0f34cSAndy Grover 
216ef87b7eaSZach Brown int rds_threads_init(void)
21700e0f34cSAndy Grover {
21880c51be5SZach Brown 	rds_wq = create_singlethread_workqueue("krdsd");
2198690bfa1SAndy Grover 	if (!rds_wq)
22000e0f34cSAndy Grover 		return -ENOMEM;
22100e0f34cSAndy Grover 
22200e0f34cSAndy Grover 	return 0;
22300e0f34cSAndy Grover }
224