100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * Copyright (c) 2006 Oracle.  All rights reserved.
300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * This software is available to you under a choice of one of two
500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * licenses.  You may choose to be licensed under the terms of the GNU
600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * General Public License (GPL) Version 2, available from the file
700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * COPYING in the main directory of this source tree, or the
800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * OpenIB.org BSD license below:
900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
1000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *     Redistribution and use in source and binary forms, with or
1100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *     without modification, are permitted provided that the following
1200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *     conditions are met:
1300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
1400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *      - Redistributions of source code must retain the above
1500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *        copyright notice, this list of conditions and the following
1600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *        disclaimer.
1700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
1800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *      - Redistributions in binary form must reproduce the above
1900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *        copyright notice, this list of conditions and the following
2000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *        disclaimer in the documentation and/or other materials
2100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *        provided with the distribution.
2200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
2300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * SOFTWARE.
3100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
3200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
3300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include <linux/kernel.h>
3400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include <linux/list.h>
355a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
36bc3b2d7fb9b014d75ebb79ba371a763dbab5e8cfPaul Gortmaker#include <linux/export.h>
3700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include <net/inet_hashtables.h>
3800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
3900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include "rds.h"
4000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include "loop.h"
4100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
4200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define RDS_CONNECTION_HASH_BITS 12
4300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
4400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
4500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
4600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/* converting this to RCU is a chore for another day.. */
4700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic DEFINE_SPINLOCK(rds_conn_lock);
4800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic unsigned long rds_conn_count;
4900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
5000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct kmem_cache *rds_conn_slab;
5100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
5200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
5300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
5400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* Pass NULL, don't need struct net for hash */
5500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	unsigned long hash = inet_ehashfn(NULL,
5600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  be32_to_cpu(laddr), 0,
5700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  be32_to_cpu(faddr), 0);
5800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
5900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
6000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
6100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define rds_conn_info_set(var, test, suffix) do {		\
6200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (test)						\
6300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		var |= RDS_INFO_CONNECTION_FLAG_##suffix;	\
6400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover} while (0)
6500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
66bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason/* rcu read lock must be held or the connection spinlock */
6700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct rds_connection *rds_conn_lookup(struct hlist_head *head,
6800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					      __be32 laddr, __be32 faddr,
6900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					      struct rds_transport *trans)
7000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
7100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_connection *conn, *ret = NULL;
7200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
73b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin	hlist_for_each_entry_rcu(conn, head, c_hash_node) {
7400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		if (conn->c_faddr == faddr && conn->c_laddr == laddr &&
7500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				conn->c_trans == trans) {
7600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			ret = conn;
7700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			break;
7800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
7900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
8000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret,
8100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 &laddr, &faddr);
8200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return ret;
8300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
8400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
8500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
8600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * This is called by transports as they're bringing down a connection.
8700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * It clears partial message state so that the transport can start sending
8800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * and receiving over this connection again in the future.  It is up to
8900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * the transport to have serialized this call with its send and recv.
9000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
91ff51bf841587c75b58d25ed77263158619784dd3stephen hemmingerstatic void rds_conn_reset(struct rds_connection *conn)
9200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
9300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rdsdebug("connection %pI4 to %pI4 reset\n",
9400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  &conn->c_laddr, &conn->c_faddr);
9500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
9600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_stats_inc(s_conn_reset);
9700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_send_reset(conn);
9800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_flags = 0;
9900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
10000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* Do not clear next_rx_seq here, else we cannot distinguish
10100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * retransmitted packets from new packets, and will hand all
10200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * of them to the application. That is not consistent with the
10300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * reliability guarantees of RDS. */
10400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
10500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
10600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
10700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * There is only every one 'conn' for a given pair of addresses in the
10800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * system at a time.  They contain messages to be retransmitted and so
10900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * span the lifetime of the actual underlying transport connections.
11000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover *
11100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * For now they are not garbage collected once they're created.  They
11200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * are torn down as the module is removed, if ever.
11300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
11400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
11500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_transport *trans, gfp_t gfp,
11600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       int is_outgoing)
11700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
118cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	struct rds_connection *conn, *parent = NULL;
11900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_head *head = rds_conn_bucket(laddr, faddr);
1205adb5bc65f93e52341c3fc9d03d4030dd375e256Zach Brown	struct rds_transport *loop_trans;
12100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	unsigned long flags;
12200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	int ret;
12300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
124bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
12500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn = rds_conn_lookup(head, laddr, faddr, trans);
126f64f9e719261a87818dd192a3a2352e5b20fbd0fJoe Perches	if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
127f64f9e719261a87818dd192a3a2352e5b20fbd0fJoe Perches	    !is_outgoing) {
12800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		/* This is a looped back IB connection, and we're
12900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 * called by the code handling the incoming connect.
13000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 * We need a second connection object into which we
13100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 * can stick the other QP. */
13200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		parent = conn;
13300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn = parent->c_passive;
13400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
135bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_unlock();
13600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (conn)
13700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		goto out;
13800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
13905a178ecdc7396b78dfbb5d8bda65108b37b8672Wei Yongjun	conn = kmem_cache_zalloc(rds_conn_slab, gfp);
1408690bfa17aea4c42da1bcf90a7af93d161eca624Andy Grover	if (!conn) {
14100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn = ERR_PTR(-ENOMEM);
14200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		goto out;
14300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
14400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
14500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_HLIST_NODE(&conn->c_hash_node);
14600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_laddr = laddr;
14700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_faddr = faddr;
14800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	spin_lock_init(&conn->c_lock);
14900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_next_tx_seq = 1;
15000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
1510f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown	init_waitqueue_head(&conn->c_waitq);
15200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_LIST_HEAD(&conn->c_send_queue);
15300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_LIST_HEAD(&conn->c_retrans);
15400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
15500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	ret = rds_cong_get_maps(conn);
15600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (ret) {
15700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		kmem_cache_free(rds_conn_slab, conn);
15800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn = ERR_PTR(ret);
15900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		goto out;
16000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
16100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
16200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/*
16300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * This is where a connection becomes loopback.  If *any* RDS sockets
16400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * can bind to the destination address then we'd rather the messages
16500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * flow through loopback rather than either transport.
16600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 */
1675adb5bc65f93e52341c3fc9d03d4030dd375e256Zach Brown	loop_trans = rds_trans_get_preferred(faddr);
1685adb5bc65f93e52341c3fc9d03d4030dd375e256Zach Brown	if (loop_trans) {
1695adb5bc65f93e52341c3fc9d03d4030dd375e256Zach Brown		rds_trans_put(loop_trans);
17000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn->c_loopback = 1;
17100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		if (is_outgoing && trans->t_prefer_loopback) {
17200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* "outgoing" connection - and the transport
17300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * says it wants the connection handled by the
17400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * loopback transport. This is what TCP does.
17500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 */
17600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			trans = &rds_loop_transport;
17700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
17800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
17900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
18000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_trans = trans;
18100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
18200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	ret = trans->conn_alloc(conn, gfp);
18300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (ret) {
18400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		kmem_cache_free(rds_conn_slab, conn);
18500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn = ERR_PTR(ret);
18600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		goto out;
18700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
18800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
18900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	atomic_set(&conn->c_state, RDS_CONN_DOWN);
19000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_reconnect_jiffies = 0;
19100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker);
19200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker);
19300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_DELAYED_WORK(&conn->c_conn_w, rds_connect_worker);
19400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_WORK(&conn->c_down_w, rds_shutdown_worker);
19500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	mutex_init(&conn->c_cm_lock);
19600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_flags = 0;
19700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
19800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
19900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  conn, &laddr, &faddr,
20000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  trans->t_name ? trans->t_name : "[unknown]",
20100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  is_outgoing ? "(outgoing)" : "");
20200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
203cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	/*
204cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * Since we ran without holding the conn lock, someone could
205cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * have created the same conn (either normal or passive) in the
206cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * interim. We check while holding the lock. If we won, we complete
207cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * init and return our conn. If we lost, we rollback and return the
208cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * other one.
209cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 */
21000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	spin_lock_irqsave(&rds_conn_lock, flags);
211cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	if (parent) {
212cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		/* Creating passive conn */
213cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		if (parent->c_passive) {
214cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			trans->conn_free(conn->c_transport_data);
215cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			kmem_cache_free(rds_conn_slab, conn);
216cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			conn = parent->c_passive;
217cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		} else {
21800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			parent->c_passive = conn;
219cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_cong_add_conn(conn);
220cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_conn_count++;
221cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		}
22200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	} else {
223cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		/* Creating normal conn */
224cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		struct rds_connection *found;
225cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover
226cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		found = rds_conn_lookup(head, laddr, faddr, trans);
227cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		if (found) {
228cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			trans->conn_free(conn->c_transport_data);
229cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			kmem_cache_free(rds_conn_slab, conn);
230cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			conn = found;
231cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		} else {
232bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason			hlist_add_head_rcu(&conn->c_hash_node, head);
233cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_cong_add_conn(conn);
234cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_conn_count++;
235cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		}
23600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
23700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	spin_unlock_irqrestore(&rds_conn_lock, flags);
23800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
23900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverout:
24000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return conn;
24100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
24200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
24300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstruct rds_connection *rds_conn_create(__be32 laddr, __be32 faddr,
24400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_transport *trans, gfp_t gfp)
24500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
24600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return __rds_conn_create(laddr, faddr, trans, gfp, 0);
24700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
248616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_create);
24900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
25000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstruct rds_connection *rds_conn_create_outgoing(__be32 laddr, __be32 faddr,
25100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_transport *trans, gfp_t gfp)
25200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
25300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return __rds_conn_create(laddr, faddr, trans, gfp, 1);
25400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
255616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
25600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
2572dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grovervoid rds_conn_shutdown(struct rds_connection *conn)
2582dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover{
2592dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	/* shut it down unless it's down already */
2602dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
2612dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		/*
2622dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * Quiesce the connection mgmt handlers before we start tearing
2632dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * things down. We don't hold the mutex for the entire
2642dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * duration of the shutdown operation, else we may be
2652dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * deadlocking with the CM handler. Instead, the CM event
2662dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * handler is supposed to check for state DISCONNECTING
2672dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 */
2682dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		mutex_lock(&conn->c_cm_lock);
2692dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING)
2702dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) {
2712dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			rds_conn_error(conn, "shutdown called in state %d\n",
2722dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover					atomic_read(&conn->c_state));
2732dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			mutex_unlock(&conn->c_cm_lock);
2742dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			return;
2752dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		}
2762dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		mutex_unlock(&conn->c_cm_lock);
2772dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
2780f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown		wait_event(conn->c_waitq,
2790f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown			   !test_bit(RDS_IN_XMIT, &conn->c_flags));
2807e3f2952eeb1a0fe2aa9882fd1705a88f9d89b35Chris Mason
2812dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		conn->c_trans->conn_shutdown(conn);
2822dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		rds_conn_reset(conn);
2832dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
2842dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) {
2852dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			/* This can happen - eg when we're in the middle of tearing
2862dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 * down the connection, and someone unloads the rds module.
2872dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 * Quite reproduceable with loopback connections.
2882dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 * Mostly harmless.
2892dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 */
2902dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			rds_conn_error(conn,
2912dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				"%s: failed to transition to state DOWN, "
2922dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				"current state is %d\n",
2932dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				__func__,
2942dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				atomic_read(&conn->c_state));
2952dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			return;
2962dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		}
2972dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	}
2982dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
2992dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	/* Then reconnect if it's still live.
3002dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	 * The passive side of an IB loopback connection is never added
3012dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	 * to the conn hash, so we never trigger a reconnect on this
3022dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	 * conn - the reconnect is always triggered by the active peer. */
3032dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	cancel_delayed_work_sync(&conn->c_conn_w);
304bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
305bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	if (!hlist_unhashed(&conn->c_hash_node)) {
306bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason		rcu_read_unlock();
3072dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		rds_queue_reconnect(conn);
308bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	} else {
309bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason		rcu_read_unlock();
310bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	}
3112dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover}
3122dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
3132dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover/*
3142dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover * Stop and free a connection.
315ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown *
316ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown * This can only be used in very limited circumstances.  It assumes that once
317ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown * the conn has been shutdown that no one else is referencing the connection.
318ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown * We can only ensure this in the rmmod path in the current code.
3192dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover */
32000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_conn_destroy(struct rds_connection *conn)
32100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
32200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_message *rm, *rtmp;
323fe8ff6b58f040dd52d2db45972db8e0301847f1cZach Brown	unsigned long flags;
32400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
32500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rdsdebug("freeing conn %p for %pI4 -> "
32600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 "%pI4\n", conn, &conn->c_laddr,
32700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 &conn->c_faddr);
32800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
329abf454398c2ebafc629ebb8b149f5a752c79e919Chris Mason	/* Ensure conn will not be scheduled for reconnect */
330abf454398c2ebafc629ebb8b149f5a752c79e919Chris Mason	spin_lock_irq(&rds_conn_lock);
331bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	hlist_del_init_rcu(&conn->c_hash_node);
332abf454398c2ebafc629ebb8b149f5a752c79e919Chris Mason	spin_unlock_irq(&rds_conn_lock);
333bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	synchronize_rcu();
334bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason
335ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown	/* shut the connection down */
336ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown	rds_conn_drop(conn);
337ffcec0e110c198717eb0f6ac000c1e5397db9451Zach Brown	flush_work(&conn->c_down_w);
33800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
3394518071ac1bcb76c64a55a3fddb39fb3d39add41Zach Brown	/* make sure lingering queued work won't try to ref the conn */
3404518071ac1bcb76c64a55a3fddb39fb3d39add41Zach Brown	cancel_delayed_work_sync(&conn->c_send_w);
3414518071ac1bcb76c64a55a3fddb39fb3d39add41Zach Brown	cancel_delayed_work_sync(&conn->c_recv_w);
3424518071ac1bcb76c64a55a3fddb39fb3d39add41Zach Brown
34300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* tear down queued messages */
34400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	list_for_each_entry_safe(rm, rtmp,
34500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 &conn->c_send_queue,
34600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 m_conn_item) {
34700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		list_del_init(&rm->m_conn_item);
34800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		BUG_ON(!list_empty(&rm->m_sock_item));
34900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		rds_message_put(rm);
35000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
35100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (conn->c_xmit_rm)
35200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		rds_message_put(conn->c_xmit_rm);
35300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
35400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_trans->conn_free(conn->c_transport_data);
35500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
35600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/*
35700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * The congestion maps aren't freed up here.  They're
35800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * freed by rds_cong_exit() after all the connections
35900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * have been freed.
36000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 */
36100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_cong_remove_conn(conn);
36200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
36300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	BUG_ON(!list_empty(&conn->c_retrans));
36400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	kmem_cache_free(rds_conn_slab, conn);
36500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
366fe8ff6b58f040dd52d2db45972db8e0301847f1cZach Brown	spin_lock_irqsave(&rds_conn_lock, flags);
36700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_count--;
368fe8ff6b58f040dd52d2db45972db8e0301847f1cZach Brown	spin_unlock_irqrestore(&rds_conn_lock, flags);
36900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
370616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_destroy);
37100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
37200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_message_info(struct socket *sock, unsigned int len,
37300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  struct rds_info_iterator *iter,
37400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  struct rds_info_lengths *lens,
37500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  int want_send)
37600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
37700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_head *head;
37800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct list_head *list;
37900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_connection *conn;
38000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_message *rm;
38100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	unsigned int total = 0;
382501dcccdb7a2335cde07d4acb56e636182d62944Zach Brown	unsigned long flags;
38300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	size_t i;
38400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
38500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	len /= sizeof(struct rds_info_message);
38600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
387bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
38800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
38900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
39000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	     i++, head++) {
391b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
39200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			if (want_send)
39300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				list = &conn->c_send_queue;
39400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			else
39500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				list = &conn->c_retrans;
39600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
397501dcccdb7a2335cde07d4acb56e636182d62944Zach Brown			spin_lock_irqsave(&conn->c_lock, flags);
39800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
39900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* XXX too lazy to maintain counts.. */
40000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			list_for_each_entry(rm, list, m_conn_item) {
40100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				total++;
40200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				if (total <= len)
40300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					rds_inc_info_copy(&rm->m_inc, iter,
40400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover							  conn->c_laddr,
40500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover							  conn->c_faddr, 0);
40600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			}
40700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
408501dcccdb7a2335cde07d4acb56e636182d62944Zach Brown			spin_unlock_irqrestore(&conn->c_lock, flags);
40900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
41000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
411bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_unlock();
41200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
41300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->nr = total;
41400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->each = sizeof(struct rds_info_message);
41500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
41600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
41700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_message_info_send(struct socket *sock, unsigned int len,
41800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_info_iterator *iter,
41900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_info_lengths *lens)
42000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
42100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_message_info(sock, len, iter, lens, 1);
42200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
42300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
42400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_message_info_retrans(struct socket *sock,
42500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  unsigned int len,
42600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  struct rds_info_iterator *iter,
42700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  struct rds_info_lengths *lens)
42800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
42900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_message_info(sock, len, iter, lens, 0);
43000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
43100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
43200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_for_each_conn_info(struct socket *sock, unsigned int len,
43300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_iterator *iter,
43400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_lengths *lens,
43500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  int (*visitor)(struct rds_connection *, void *),
43600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  size_t item_len)
43700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
43800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	uint64_t buffer[(item_len + 7) / 8];
43900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_head *head;
44000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_connection *conn;
44100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	size_t i;
44200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
443bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
44400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
44500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->nr = 0;
44600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->each = item_len;
44700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
44800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
44900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	     i++, head++) {
450b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
45100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
45200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* XXX no c_lock usage.. */
45300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			if (!visitor(conn, buffer))
45400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				continue;
45500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
45600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* We copy as much as we can fit in the buffer,
45700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * but we count all items so that the caller
45800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * can resize the buffer. */
45900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			if (len >= item_len) {
46000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				rds_info_copy(iter, buffer, item_len);
46100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				len -= item_len;
46200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			}
46300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			lens->nr++;
46400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
46500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
466bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_unlock();
46700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
468616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_for_each_conn_info);
46900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
47000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic int rds_conn_info_visitor(struct rds_connection *conn,
47100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  void *buffer)
47200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
47300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_info_connection *cinfo = buffer;
47400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
47500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->next_tx_seq = conn->c_next_tx_seq;
47600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->next_rx_seq = conn->c_next_rx_seq;
47700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->laddr = conn->c_laddr;
47800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->faddr = conn->c_faddr;
47900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	strncpy(cinfo->transport, conn->c_trans->t_name,
48000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		sizeof(cinfo->transport));
48100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->flags = 0;
48200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
4830f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &conn->c_flags),
4840f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown			  SENDING);
48500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* XXX Future: return the state rather than these funky bits */
48600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_info_set(cinfo->flags,
48700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  atomic_read(&conn->c_state) == RDS_CONN_CONNECTING,
48800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  CONNECTING);
48900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_info_set(cinfo->flags,
49000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  atomic_read(&conn->c_state) == RDS_CONN_UP,
49100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  CONNECTED);
49200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return 1;
49300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
49400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
49500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_info(struct socket *sock, unsigned int len,
49600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_iterator *iter,
49700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_lengths *lens)
49800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
49900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_for_each_conn_info(sock, len, iter, lens,
50000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				rds_conn_info_visitor,
50100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				sizeof(struct rds_info_connection));
50200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
50300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
504ef87b7ea39a91906218a262686bcb8bad8b6b46eZach Brownint rds_conn_init(void)
50500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
50600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_slab = kmem_cache_create("rds_connection",
50700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  sizeof(struct rds_connection),
50800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  0, 0, NULL);
5098690bfa17aea4c42da1bcf90a7af93d161eca624Andy Grover	if (!rds_conn_slab)
51000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		return -ENOMEM;
51100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
51200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
51300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_register_func(RDS_INFO_SEND_MESSAGES,
51400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			       rds_conn_message_info_send);
51500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
51600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			       rds_conn_message_info_retrans);
51700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
51800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return 0;
51900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
52000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
52100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_conn_exit(void)
52200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
52300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_loop_exit();
52400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
52500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	WARN_ON(!hlist_empty(rds_conn_hash));
52600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
52700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	kmem_cache_destroy(rds_conn_slab);
52800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
52900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
53000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
53100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 rds_conn_message_info_send);
53200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
53300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 rds_conn_message_info_retrans);
53400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
53500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
53600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
53700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * Force a disconnect
53800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
53900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_conn_drop(struct rds_connection *conn)
54000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
54100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	atomic_set(&conn->c_state, RDS_CONN_ERROR);
54200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	queue_work(rds_wq, &conn->c_down_w);
54300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
544616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_drop);
54500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
54600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
547f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown * If the connection is down, trigger a connect. We may have scheduled a
548f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown * delayed reconnect however - in this case we should not interfere.
549f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown */
550f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brownvoid rds_conn_connect_if_down(struct rds_connection *conn)
551f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown{
552f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown	if (rds_conn_state(conn) == RDS_CONN_DOWN &&
553f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown	    !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
554f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown		queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
555f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown}
556f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach BrownEXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
557f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown
558f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown/*
55900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * An error occurred on the connection
56000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
56100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid
56200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover__rds_conn_error(struct rds_connection *conn, const char *fmt, ...)
56300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
56400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	va_list ap;
56500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
56600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	va_start(ap, fmt);
56700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	vprintk(fmt, ap);
56800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	va_end(ap);
56900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
57000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_drop(conn);
57100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
572