connection.c revision 0f4b1c7e89e699f588807a914ec6e6396c851a72
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>
3600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include <net/inet_hashtables.h>
3700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
3800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include "rds.h"
3900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#include "loop.h"
4000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
4100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define RDS_CONNECTION_HASH_BITS 12
4200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
4300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
4400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
4500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/* converting this to RCU is a chore for another day.. */
4600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic DEFINE_SPINLOCK(rds_conn_lock);
4700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic unsigned long rds_conn_count;
4800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
4900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct kmem_cache *rds_conn_slab;
5000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
5100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
5200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
5300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* Pass NULL, don't need struct net for hash */
5400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	unsigned long hash = inet_ehashfn(NULL,
5500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  be32_to_cpu(laddr), 0,
5600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  be32_to_cpu(faddr), 0);
5700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
5800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
5900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
6000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover#define rds_conn_info_set(var, test, suffix) do {		\
6100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (test)						\
6200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		var |= RDS_INFO_CONNECTION_FLAG_##suffix;	\
6300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover} while (0)
6400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
65bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason/* rcu read lock must be held or the connection spinlock */
6600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic struct rds_connection *rds_conn_lookup(struct hlist_head *head,
6700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					      __be32 laddr, __be32 faddr,
6800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					      struct rds_transport *trans)
6900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
7000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_connection *conn, *ret = NULL;
7100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_node *pos;
7200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
73bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	hlist_for_each_entry_rcu(conn, pos, 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 */
9100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid 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);
12000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	unsigned long flags;
12100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	int ret;
12200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
123bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason
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	 */
16700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (rds_trans_get_preferred(faddr)) {
16800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn->c_loopback = 1;
16900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		if (is_outgoing && trans->t_prefer_loopback) {
17000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* "outgoing" connection - and the transport
17100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * says it wants the connection handled by the
17200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * loopback transport. This is what TCP does.
17300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 */
17400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			trans = &rds_loop_transport;
17500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
17600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
17700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
17800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_trans = trans;
17900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
18000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	ret = trans->conn_alloc(conn, gfp);
18100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (ret) {
18200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		kmem_cache_free(rds_conn_slab, conn);
18300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		conn = ERR_PTR(ret);
18400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		goto out;
18500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
18600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
18700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	atomic_set(&conn->c_state, RDS_CONN_DOWN);
18800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_reconnect_jiffies = 0;
18900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker);
19000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker);
19100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_DELAYED_WORK(&conn->c_conn_w, rds_connect_worker);
19200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	INIT_WORK(&conn->c_down_w, rds_shutdown_worker);
19300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	mutex_init(&conn->c_cm_lock);
19400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_flags = 0;
19500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
19600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
19700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  conn, &laddr, &faddr,
19800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  trans->t_name ? trans->t_name : "[unknown]",
19900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	  is_outgoing ? "(outgoing)" : "");
20000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
201cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	/*
202cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * Since we ran without holding the conn lock, someone could
203cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * have created the same conn (either normal or passive) in the
204cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * interim. We check while holding the lock. If we won, we complete
205cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * init and return our conn. If we lost, we rollback and return the
206cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 * other one.
207cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	 */
20800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	spin_lock_irqsave(&rds_conn_lock, flags);
209cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover	if (parent) {
210cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		/* Creating passive conn */
211cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		if (parent->c_passive) {
212cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			trans->conn_free(conn->c_transport_data);
213cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			kmem_cache_free(rds_conn_slab, conn);
214cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			conn = parent->c_passive;
215cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		} else {
21600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			parent->c_passive = conn;
217cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_cong_add_conn(conn);
218cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_conn_count++;
219cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		}
22000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	} else {
221cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		/* Creating normal conn */
222cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		struct rds_connection *found;
223cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover
224cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		found = rds_conn_lookup(head, laddr, faddr, trans);
225cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		if (found) {
226cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			trans->conn_free(conn->c_transport_data);
227cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			kmem_cache_free(rds_conn_slab, conn);
228cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			conn = found;
229cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		} else {
230bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason			hlist_add_head_rcu(&conn->c_hash_node, head);
231cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_cong_add_conn(conn);
232cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover			rds_conn_count++;
233cb24405e67e56cbef51b5e4d0bb0a0fde167261fAndy Grover		}
23400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
23500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	spin_unlock_irqrestore(&rds_conn_lock, flags);
23600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
23700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverout:
23800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return conn;
23900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
24000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
24100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstruct rds_connection *rds_conn_create(__be32 laddr, __be32 faddr,
24200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_transport *trans, gfp_t gfp)
24300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
24400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return __rds_conn_create(laddr, faddr, trans, gfp, 0);
24500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
246616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_create);
24700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
24800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstruct rds_connection *rds_conn_create_outgoing(__be32 laddr, __be32 faddr,
24900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_transport *trans, gfp_t gfp)
25000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
25100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return __rds_conn_create(laddr, faddr, trans, gfp, 1);
25200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
253616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
25400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
2552dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grovervoid rds_conn_shutdown(struct rds_connection *conn)
2562dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover{
2572dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	/* shut it down unless it's down already */
2582dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
2592dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		/*
2602dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * Quiesce the connection mgmt handlers before we start tearing
2612dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * things down. We don't hold the mutex for the entire
2622dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * duration of the shutdown operation, else we may be
2632dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * deadlocking with the CM handler. Instead, the CM event
2642dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 * handler is supposed to check for state DISCONNECTING
2652dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 */
2662dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		mutex_lock(&conn->c_cm_lock);
2672dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING)
2682dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		 && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) {
2692dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			rds_conn_error(conn, "shutdown called in state %d\n",
2702dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover					atomic_read(&conn->c_state));
2712dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			mutex_unlock(&conn->c_cm_lock);
2722dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			return;
2732dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		}
2742dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		mutex_unlock(&conn->c_cm_lock);
2752dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
2760f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown		wait_event(conn->c_waitq,
2770f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown			   !test_bit(RDS_IN_XMIT, &conn->c_flags));
2787e3f2952eeb1a0fe2aa9882fd1705a88f9d89b35Chris Mason
2792dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		conn->c_trans->conn_shutdown(conn);
2802dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		rds_conn_reset(conn);
2812dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
2822dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) {
2832dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			/* This can happen - eg when we're in the middle of tearing
2842dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 * down the connection, and someone unloads the rds module.
2852dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 * Quite reproduceable with loopback connections.
2862dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 * Mostly harmless.
2872dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			 */
2882dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			rds_conn_error(conn,
2892dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				"%s: failed to transition to state DOWN, "
2902dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				"current state is %d\n",
2912dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				__func__,
2922dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover				atomic_read(&conn->c_state));
2932dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover			return;
2942dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		}
2952dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	}
2962dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
2972dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	/* Then reconnect if it's still live.
2982dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	 * The passive side of an IB loopback connection is never added
2992dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	 * to the conn hash, so we never trigger a reconnect on this
3002dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	 * conn - the reconnect is always triggered by the active peer. */
3012dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover	cancel_delayed_work_sync(&conn->c_conn_w);
302bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
303bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	if (!hlist_unhashed(&conn->c_hash_node)) {
304bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason		rcu_read_unlock();
3052dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover		rds_queue_reconnect(conn);
306bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	} else {
307bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason		rcu_read_unlock();
308bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	}
3092dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover}
3102dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover
3112dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover/*
3122dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover * Stop and free a connection.
3132dc393573430f853e56e25bf4b41c34ba2aa8fd6Andy Grover */
31400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_conn_destroy(struct rds_connection *conn)
31500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
31600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_message *rm, *rtmp;
31700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
31800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rdsdebug("freeing conn %p for %pI4 -> "
31900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 "%pI4\n", conn, &conn->c_laddr,
32000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		 &conn->c_faddr);
32100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
322abf454398c2ebafc629ebb8b149f5a752c79e919Chris Mason	/* Ensure conn will not be scheduled for reconnect */
323abf454398c2ebafc629ebb8b149f5a752c79e919Chris Mason	spin_lock_irq(&rds_conn_lock);
324bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	hlist_del_init_rcu(&conn->c_hash_node);
325abf454398c2ebafc629ebb8b149f5a752c79e919Chris Mason	spin_unlock_irq(&rds_conn_lock);
32600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
327bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	synchronize_rcu();
328bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason
329bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rds_conn_shutdown(conn);
33000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
33100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* tear down queued messages */
33200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	list_for_each_entry_safe(rm, rtmp,
33300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 &conn->c_send_queue,
33400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 m_conn_item) {
33500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		list_del_init(&rm->m_conn_item);
33600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		BUG_ON(!list_empty(&rm->m_sock_item));
33700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		rds_message_put(rm);
33800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
33900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	if (conn->c_xmit_rm)
34000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		rds_message_put(conn->c_xmit_rm);
34100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
34200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	conn->c_trans->conn_free(conn->c_transport_data);
34300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
34400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/*
34500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * The congestion maps aren't freed up here.  They're
34600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * freed by rds_cong_exit() after all the connections
34700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 * have been freed.
34800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	 */
34900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_cong_remove_conn(conn);
35000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
35100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	BUG_ON(!list_empty(&conn->c_retrans));
35200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	kmem_cache_free(rds_conn_slab, conn);
35300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
35400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_count--;
35500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
356616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_destroy);
35700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
35800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_message_info(struct socket *sock, unsigned int len,
35900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  struct rds_info_iterator *iter,
36000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  struct rds_info_lengths *lens,
36100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  int want_send)
36200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
36300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_head *head;
36400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_node *pos;
36500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct list_head *list;
36600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_connection *conn;
36700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_message *rm;
36800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	unsigned int total = 0;
369501dcccdb7a2335cde07d4acb56e636182d62944Zach Brown	unsigned long flags;
37000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	size_t i;
37100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
37200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	len /= sizeof(struct rds_info_message);
37300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
374bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
37500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
37600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
37700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	     i++, head++) {
378bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason		hlist_for_each_entry_rcu(conn, pos, head, c_hash_node) {
37900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			if (want_send)
38000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				list = &conn->c_send_queue;
38100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			else
38200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				list = &conn->c_retrans;
38300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
384501dcccdb7a2335cde07d4acb56e636182d62944Zach Brown			spin_lock_irqsave(&conn->c_lock, flags);
38500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
38600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* XXX too lazy to maintain counts.. */
38700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			list_for_each_entry(rm, list, m_conn_item) {
38800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				total++;
38900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				if (total <= len)
39000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					rds_inc_info_copy(&rm->m_inc, iter,
39100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover							  conn->c_laddr,
39200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover							  conn->c_faddr, 0);
39300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			}
39400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
395501dcccdb7a2335cde07d4acb56e636182d62944Zach Brown			spin_unlock_irqrestore(&conn->c_lock, flags);
39600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
39700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
398bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_unlock();
39900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
40000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->nr = total;
40100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->each = sizeof(struct rds_info_message);
40200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
40300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
40400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_message_info_send(struct socket *sock, unsigned int len,
40500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_info_iterator *iter,
40600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				       struct rds_info_lengths *lens)
40700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
40800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_message_info(sock, len, iter, lens, 1);
40900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
41000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
41100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_message_info_retrans(struct socket *sock,
41200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  unsigned int len,
41300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  struct rds_info_iterator *iter,
41400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  struct rds_info_lengths *lens)
41500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
41600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_message_info(sock, len, iter, lens, 0);
41700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
41800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
41900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_for_each_conn_info(struct socket *sock, unsigned int len,
42000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_iterator *iter,
42100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_lengths *lens,
42200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  int (*visitor)(struct rds_connection *, void *),
42300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  size_t item_len)
42400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
42500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	uint64_t buffer[(item_len + 7) / 8];
42600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_head *head;
42700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct hlist_node *pos;
42800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_connection *conn;
42900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	size_t i;
43000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
431bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_lock();
43200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
43300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->nr = 0;
43400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	lens->each = item_len;
43500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
43600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
43700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	     i++, head++) {
438bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason		hlist_for_each_entry_rcu(conn, pos, head, c_hash_node) {
43900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
44000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* XXX no c_lock usage.. */
44100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			if (!visitor(conn, buffer))
44200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				continue;
44300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
44400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			/* We copy as much as we can fit in the buffer,
44500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * but we count all items so that the caller
44600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			 * can resize the buffer. */
44700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			if (len >= item_len) {
44800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				rds_info_copy(iter, buffer, item_len);
44900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				len -= item_len;
45000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			}
45100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			lens->nr++;
45200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		}
45300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	}
454bcf50ef2ce3c5d8f2fe995259da16677898cb300Chris Mason	rcu_read_unlock();
45500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
456616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_for_each_conn_info);
45700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
45800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic int rds_conn_info_visitor(struct rds_connection *conn,
45900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				  void *buffer)
46000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
46100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	struct rds_info_connection *cinfo = buffer;
46200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
46300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->next_tx_seq = conn->c_next_tx_seq;
46400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->next_rx_seq = conn->c_next_rx_seq;
46500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->laddr = conn->c_laddr;
46600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->faddr = conn->c_faddr;
46700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	strncpy(cinfo->transport, conn->c_trans->t_name,
46800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		sizeof(cinfo->transport));
46900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	cinfo->flags = 0;
47000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
4710f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &conn->c_flags),
4720f4b1c7e89e699f588807a914ec6e6396c851a72Zach Brown			  SENDING);
47300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	/* XXX Future: return the state rather than these funky bits */
47400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_info_set(cinfo->flags,
47500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  atomic_read(&conn->c_state) == RDS_CONN_CONNECTING,
47600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  CONNECTING);
47700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_info_set(cinfo->flags,
47800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  atomic_read(&conn->c_state) == RDS_CONN_UP,
47900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  CONNECTED);
48000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return 1;
48100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
48200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
48300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverstatic void rds_conn_info(struct socket *sock, unsigned int len,
48400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_iterator *iter,
48500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			  struct rds_info_lengths *lens)
48600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
48700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_for_each_conn_info(sock, len, iter, lens,
48800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				rds_conn_info_visitor,
48900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				sizeof(struct rds_info_connection));
49000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
49100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
49200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Groverint __init rds_conn_init(void)
49300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
49400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_slab = kmem_cache_create("rds_connection",
49500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  sizeof(struct rds_connection),
49600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover					  0, 0, NULL);
4978690bfa17aea4c42da1bcf90a7af93d161eca624Andy Grover	if (!rds_conn_slab)
49800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover		return -ENOMEM;
49900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
50000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
50100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_register_func(RDS_INFO_SEND_MESSAGES,
50200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			       rds_conn_message_info_send);
50300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
50400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover			       rds_conn_message_info_retrans);
50500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
50600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	return 0;
50700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
50800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
50900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_conn_exit(void)
51000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
51100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_loop_exit();
51200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
51300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	WARN_ON(!hlist_empty(rds_conn_hash));
51400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
51500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	kmem_cache_destroy(rds_conn_slab);
51600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
51700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
51800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
51900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 rds_conn_message_info_send);
52000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
52100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover				 rds_conn_message_info_retrans);
52200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
52300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
52400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
52500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * Force a disconnect
52600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
52700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid rds_conn_drop(struct rds_connection *conn)
52800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
52900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	atomic_set(&conn->c_state, RDS_CONN_ERROR);
53000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	queue_work(rds_wq, &conn->c_down_w);
53100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
532616b757ae18fb8ec2dfe7ff9d3f589f82cb0eb9dAndy GroverEXPORT_SYMBOL_GPL(rds_conn_drop);
53300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
53400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover/*
535f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown * If the connection is down, trigger a connect. We may have scheduled a
536f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown * delayed reconnect however - in this case we should not interfere.
537f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown */
538f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brownvoid rds_conn_connect_if_down(struct rds_connection *conn)
539f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown{
540f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown	if (rds_conn_state(conn) == RDS_CONN_DOWN &&
541f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown	    !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
542f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown		queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
543f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown}
544f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach BrownEXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
545f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown
546f3c6808d3d8513db2b0543538fc35c25a60fe7a7Zach Brown/*
54700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover * An error occurred on the connection
54800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover */
54900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grovervoid
55000e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover__rds_conn_error(struct rds_connection *conn, const char *fmt, ...)
55100e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover{
55200e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	va_list ap;
55300e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
55400e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	va_start(ap, fmt);
55500e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	vprintk(fmt, ap);
55600e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	va_end(ap);
55700e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover
55800e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover	rds_conn_drop(conn);
55900e0f34c616603ba6500f41943cbf89eb4a8a5beAndy Grover}
560