100e0f34cSAndy Grover /* 200e0f34cSAndy Grover * Copyright (c) 2006 Oracle. All rights reserved. 300e0f34cSAndy Grover * 400e0f34cSAndy Grover * This software is available to you under a choice of one of two 500e0f34cSAndy Grover * licenses. You may choose to be licensed under the terms of the GNU 600e0f34cSAndy Grover * General Public License (GPL) Version 2, available from the file 700e0f34cSAndy Grover * COPYING in the main directory of this source tree, or the 800e0f34cSAndy Grover * OpenIB.org BSD license below: 900e0f34cSAndy Grover * 1000e0f34cSAndy Grover * Redistribution and use in source and binary forms, with or 1100e0f34cSAndy Grover * without modification, are permitted provided that the following 1200e0f34cSAndy Grover * conditions are met: 1300e0f34cSAndy Grover * 1400e0f34cSAndy Grover * - Redistributions of source code must retain the above 1500e0f34cSAndy Grover * copyright notice, this list of conditions and the following 1600e0f34cSAndy Grover * disclaimer. 1700e0f34cSAndy Grover * 1800e0f34cSAndy Grover * - Redistributions in binary form must reproduce the above 1900e0f34cSAndy Grover * copyright notice, this list of conditions and the following 2000e0f34cSAndy Grover * disclaimer in the documentation and/or other materials 2100e0f34cSAndy Grover * provided with the distribution. 2200e0f34cSAndy Grover * 2300e0f34cSAndy Grover * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2400e0f34cSAndy Grover * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2500e0f34cSAndy Grover * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2600e0f34cSAndy Grover * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2700e0f34cSAndy Grover * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2800e0f34cSAndy Grover * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2900e0f34cSAndy Grover * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3000e0f34cSAndy Grover * SOFTWARE. 3100e0f34cSAndy Grover * 3200e0f34cSAndy Grover */ 3300e0f34cSAndy Grover #include <linux/kernel.h> 3400e0f34cSAndy Grover #include <linux/list.h> 355a0e3ad6STejun Heo #include <linux/slab.h> 36bc3b2d7fSPaul Gortmaker #include <linux/export.h> 3700e0f34cSAndy Grover #include <net/inet_hashtables.h> 3800e0f34cSAndy Grover 3900e0f34cSAndy Grover #include "rds.h" 4000e0f34cSAndy Grover #include "loop.h" 4100e0f34cSAndy Grover 4200e0f34cSAndy Grover #define RDS_CONNECTION_HASH_BITS 12 4300e0f34cSAndy Grover #define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS) 4400e0f34cSAndy Grover #define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1) 4500e0f34cSAndy Grover 4600e0f34cSAndy Grover /* converting this to RCU is a chore for another day.. */ 4700e0f34cSAndy Grover static DEFINE_SPINLOCK(rds_conn_lock); 4800e0f34cSAndy Grover static unsigned long rds_conn_count; 4900e0f34cSAndy Grover static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES]; 5000e0f34cSAndy Grover static struct kmem_cache *rds_conn_slab; 5100e0f34cSAndy Grover 5200e0f34cSAndy Grover static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr) 5300e0f34cSAndy Grover { 541bbdceefSHannes Frederic Sowa static u32 rds_hash_secret __read_mostly; 551bbdceefSHannes Frederic Sowa 561bbdceefSHannes Frederic Sowa unsigned long hash; 571bbdceefSHannes Frederic Sowa 581bbdceefSHannes Frederic Sowa net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret)); 591bbdceefSHannes Frederic Sowa 6000e0f34cSAndy Grover /* Pass NULL, don't need struct net for hash */ 611bbdceefSHannes Frederic Sowa hash = __inet_ehashfn(be32_to_cpu(laddr), 0, 6265cd8033SHannes Frederic Sowa be32_to_cpu(faddr), 0, 631bbdceefSHannes Frederic Sowa rds_hash_secret); 6400e0f34cSAndy Grover return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK]; 6500e0f34cSAndy Grover } 6600e0f34cSAndy Grover 6700e0f34cSAndy Grover #define rds_conn_info_set(var, test, suffix) do { \ 6800e0f34cSAndy Grover if (test) \ 6900e0f34cSAndy Grover var |= RDS_INFO_CONNECTION_FLAG_##suffix; \ 7000e0f34cSAndy Grover } while (0) 7100e0f34cSAndy Grover 72bcf50ef2SChris Mason /* rcu read lock must be held or the connection spinlock */ 738f384c01SSowmini Varadhan static struct rds_connection *rds_conn_lookup(struct net *net, 748f384c01SSowmini Varadhan struct hlist_head *head, 7500e0f34cSAndy Grover __be32 laddr, __be32 faddr, 7600e0f34cSAndy Grover struct rds_transport *trans) 7700e0f34cSAndy Grover { 7800e0f34cSAndy Grover struct rds_connection *conn, *ret = NULL; 7900e0f34cSAndy Grover 80b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 8100e0f34cSAndy Grover if (conn->c_faddr == faddr && conn->c_laddr == laddr && 828f384c01SSowmini Varadhan conn->c_trans == trans && net == rds_conn_net(conn)) { 8300e0f34cSAndy Grover ret = conn; 8400e0f34cSAndy Grover break; 8500e0f34cSAndy Grover } 8600e0f34cSAndy Grover } 8700e0f34cSAndy Grover rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret, 8800e0f34cSAndy Grover &laddr, &faddr); 8900e0f34cSAndy Grover return ret; 9000e0f34cSAndy Grover } 9100e0f34cSAndy Grover 9200e0f34cSAndy Grover /* 9300e0f34cSAndy Grover * This is called by transports as they're bringing down a connection. 9400e0f34cSAndy Grover * It clears partial message state so that the transport can start sending 9500e0f34cSAndy Grover * and receiving over this connection again in the future. It is up to 9600e0f34cSAndy Grover * the transport to have serialized this call with its send and recv. 9700e0f34cSAndy Grover */ 98d769ef81SSowmini Varadhan static void rds_conn_path_reset(struct rds_conn_path *cp) 9900e0f34cSAndy Grover { 100d769ef81SSowmini Varadhan struct rds_connection *conn = cp->cp_conn; 101d769ef81SSowmini Varadhan 10200e0f34cSAndy Grover rdsdebug("connection %pI4 to %pI4 reset\n", 10300e0f34cSAndy Grover &conn->c_laddr, &conn->c_faddr); 10400e0f34cSAndy Grover 10500e0f34cSAndy Grover rds_stats_inc(s_conn_reset); 106d769ef81SSowmini Varadhan rds_send_path_reset(cp); 107d769ef81SSowmini Varadhan cp->cp_flags = 0; 10800e0f34cSAndy Grover 10900e0f34cSAndy Grover /* Do not clear next_rx_seq here, else we cannot distinguish 11000e0f34cSAndy Grover * retransmitted packets from new packets, and will hand all 11100e0f34cSAndy Grover * of them to the application. That is not consistent with the 11200e0f34cSAndy Grover * reliability guarantees of RDS. */ 11300e0f34cSAndy Grover } 11400e0f34cSAndy Grover 1151c5113cfSSowmini Varadhan static void __rds_conn_path_init(struct rds_connection *conn, 1161c5113cfSSowmini Varadhan struct rds_conn_path *cp, bool is_outgoing) 1171c5113cfSSowmini Varadhan { 1181c5113cfSSowmini Varadhan spin_lock_init(&cp->cp_lock); 1191c5113cfSSowmini Varadhan cp->cp_next_tx_seq = 1; 1201c5113cfSSowmini Varadhan init_waitqueue_head(&cp->cp_waitq); 1211c5113cfSSowmini Varadhan INIT_LIST_HEAD(&cp->cp_send_queue); 1221c5113cfSSowmini Varadhan INIT_LIST_HEAD(&cp->cp_retrans); 1231c5113cfSSowmini Varadhan 1241c5113cfSSowmini Varadhan cp->cp_conn = conn; 1251c5113cfSSowmini Varadhan atomic_set(&cp->cp_state, RDS_CONN_DOWN); 1261c5113cfSSowmini Varadhan cp->cp_send_gen = 0; 1271c5113cfSSowmini Varadhan /* cp_outgoing is per-path. So we can only set it here 1281c5113cfSSowmini Varadhan * for the single-path transports. 1291c5113cfSSowmini Varadhan */ 1301c5113cfSSowmini Varadhan if (!conn->c_trans->t_mp_capable) 1311c5113cfSSowmini Varadhan cp->cp_outgoing = (is_outgoing ? 1 : 0); 1321c5113cfSSowmini Varadhan cp->cp_reconnect_jiffies = 0; 1331c5113cfSSowmini Varadhan INIT_DELAYED_WORK(&cp->cp_send_w, rds_send_worker); 1341c5113cfSSowmini Varadhan INIT_DELAYED_WORK(&cp->cp_recv_w, rds_recv_worker); 1351c5113cfSSowmini Varadhan INIT_DELAYED_WORK(&cp->cp_conn_w, rds_connect_worker); 1361c5113cfSSowmini Varadhan INIT_WORK(&cp->cp_down_w, rds_shutdown_worker); 1371c5113cfSSowmini Varadhan mutex_init(&cp->cp_cm_lock); 1381c5113cfSSowmini Varadhan cp->cp_flags = 0; 1391c5113cfSSowmini Varadhan } 1401c5113cfSSowmini Varadhan 14100e0f34cSAndy Grover /* 14200e0f34cSAndy Grover * There is only every one 'conn' for a given pair of addresses in the 14300e0f34cSAndy Grover * system at a time. They contain messages to be retransmitted and so 14400e0f34cSAndy Grover * span the lifetime of the actual underlying transport connections. 14500e0f34cSAndy Grover * 14600e0f34cSAndy Grover * For now they are not garbage collected once they're created. They 14700e0f34cSAndy Grover * are torn down as the module is removed, if ever. 14800e0f34cSAndy Grover */ 149d5a8ac28SSowmini Varadhan static struct rds_connection *__rds_conn_create(struct net *net, 150d5a8ac28SSowmini Varadhan __be32 laddr, __be32 faddr, 15100e0f34cSAndy Grover struct rds_transport *trans, gfp_t gfp, 15200e0f34cSAndy Grover int is_outgoing) 15300e0f34cSAndy Grover { 154cb24405eSAndy Grover struct rds_connection *conn, *parent = NULL; 15500e0f34cSAndy Grover struct hlist_head *head = rds_conn_bucket(laddr, faddr); 1565adb5bc6SZach Brown struct rds_transport *loop_trans; 15700e0f34cSAndy Grover unsigned long flags; 158*5916e2c1SSowmini Varadhan int ret, i; 15900e0f34cSAndy Grover 160bcf50ef2SChris Mason rcu_read_lock(); 1618f384c01SSowmini Varadhan conn = rds_conn_lookup(net, head, laddr, faddr, trans); 162f64f9e71SJoe Perches if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport && 1631789b2c0SSowmini Varadhan laddr == faddr && !is_outgoing) { 16400e0f34cSAndy Grover /* This is a looped back IB connection, and we're 16500e0f34cSAndy Grover * called by the code handling the incoming connect. 16600e0f34cSAndy Grover * We need a second connection object into which we 16700e0f34cSAndy Grover * can stick the other QP. */ 16800e0f34cSAndy Grover parent = conn; 16900e0f34cSAndy Grover conn = parent->c_passive; 17000e0f34cSAndy Grover } 171bcf50ef2SChris Mason rcu_read_unlock(); 17200e0f34cSAndy Grover if (conn) 17300e0f34cSAndy Grover goto out; 17400e0f34cSAndy Grover 17505a178ecSWei Yongjun conn = kmem_cache_zalloc(rds_conn_slab, gfp); 1768690bfa1SAndy Grover if (!conn) { 17700e0f34cSAndy Grover conn = ERR_PTR(-ENOMEM); 17800e0f34cSAndy Grover goto out; 17900e0f34cSAndy Grover } 18000e0f34cSAndy Grover 18100e0f34cSAndy Grover INIT_HLIST_NODE(&conn->c_hash_node); 18200e0f34cSAndy Grover conn->c_laddr = laddr; 18300e0f34cSAndy Grover conn->c_faddr = faddr; 18400e0f34cSAndy Grover 1851c5113cfSSowmini Varadhan rds_conn_net_set(conn, net); 18600e0f34cSAndy Grover 18700e0f34cSAndy Grover ret = rds_cong_get_maps(conn); 18800e0f34cSAndy Grover if (ret) { 18900e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 19000e0f34cSAndy Grover conn = ERR_PTR(ret); 19100e0f34cSAndy Grover goto out; 19200e0f34cSAndy Grover } 19300e0f34cSAndy Grover 19400e0f34cSAndy Grover /* 19500e0f34cSAndy Grover * This is where a connection becomes loopback. If *any* RDS sockets 19600e0f34cSAndy Grover * can bind to the destination address then we'd rather the messages 19700e0f34cSAndy Grover * flow through loopback rather than either transport. 19800e0f34cSAndy Grover */ 199d5a8ac28SSowmini Varadhan loop_trans = rds_trans_get_preferred(net, faddr); 2005adb5bc6SZach Brown if (loop_trans) { 2015adb5bc6SZach Brown rds_trans_put(loop_trans); 20200e0f34cSAndy Grover conn->c_loopback = 1; 20300e0f34cSAndy Grover if (is_outgoing && trans->t_prefer_loopback) { 20400e0f34cSAndy Grover /* "outgoing" connection - and the transport 20500e0f34cSAndy Grover * says it wants the connection handled by the 20600e0f34cSAndy Grover * loopback transport. This is what TCP does. 20700e0f34cSAndy Grover */ 20800e0f34cSAndy Grover trans = &rds_loop_transport; 20900e0f34cSAndy Grover } 21000e0f34cSAndy Grover } 21100e0f34cSAndy Grover 21200e0f34cSAndy Grover conn->c_trans = trans; 21300e0f34cSAndy Grover 214*5916e2c1SSowmini Varadhan init_waitqueue_head(&conn->c_hs_waitq); 215*5916e2c1SSowmini Varadhan for (i = 0; i < RDS_MPATH_WORKERS; i++) { 216*5916e2c1SSowmini Varadhan __rds_conn_path_init(conn, &conn->c_path[i], 217*5916e2c1SSowmini Varadhan is_outgoing); 218*5916e2c1SSowmini Varadhan conn->c_path[i].cp_index = i; 219*5916e2c1SSowmini Varadhan } 22000e0f34cSAndy Grover ret = trans->conn_alloc(conn, gfp); 22100e0f34cSAndy Grover if (ret) { 22200e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 22300e0f34cSAndy Grover conn = ERR_PTR(ret); 22400e0f34cSAndy Grover goto out; 22500e0f34cSAndy Grover } 22600e0f34cSAndy Grover 22700e0f34cSAndy Grover rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n", 22800e0f34cSAndy Grover conn, &laddr, &faddr, 22900e0f34cSAndy Grover trans->t_name ? trans->t_name : "[unknown]", 23000e0f34cSAndy Grover is_outgoing ? "(outgoing)" : ""); 23100e0f34cSAndy Grover 232cb24405eSAndy Grover /* 233cb24405eSAndy Grover * Since we ran without holding the conn lock, someone could 234cb24405eSAndy Grover * have created the same conn (either normal or passive) in the 235cb24405eSAndy Grover * interim. We check while holding the lock. If we won, we complete 236cb24405eSAndy Grover * init and return our conn. If we lost, we rollback and return the 237cb24405eSAndy Grover * other one. 238cb24405eSAndy Grover */ 23900e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 240cb24405eSAndy Grover if (parent) { 241cb24405eSAndy Grover /* Creating passive conn */ 242cb24405eSAndy Grover if (parent->c_passive) { 2431c5113cfSSowmini Varadhan trans->conn_free(conn->c_path[0].cp_transport_data); 24400e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 245cb24405eSAndy Grover conn = parent->c_passive; 24600e0f34cSAndy Grover } else { 247cb24405eSAndy Grover parent->c_passive = conn; 24800e0f34cSAndy Grover rds_cong_add_conn(conn); 24900e0f34cSAndy Grover rds_conn_count++; 25000e0f34cSAndy Grover } 251cb24405eSAndy Grover } else { 252cb24405eSAndy Grover /* Creating normal conn */ 253cb24405eSAndy Grover struct rds_connection *found; 25400e0f34cSAndy Grover 2558f384c01SSowmini Varadhan found = rds_conn_lookup(net, head, laddr, faddr, trans); 256cb24405eSAndy Grover if (found) { 2571c5113cfSSowmini Varadhan struct rds_conn_path *cp; 2581c5113cfSSowmini Varadhan int i; 2591c5113cfSSowmini Varadhan 2601c5113cfSSowmini Varadhan for (i = 0; i < RDS_MPATH_WORKERS; i++) { 2611c5113cfSSowmini Varadhan cp = &conn->c_path[i]; 26202105b2cSSowmini Varadhan /* The ->conn_alloc invocation may have 26302105b2cSSowmini Varadhan * allocated resource for all paths, so all 26402105b2cSSowmini Varadhan * of them may have to be freed here. 26502105b2cSSowmini Varadhan */ 26602105b2cSSowmini Varadhan if (cp->cp_transport_data) 2671c5113cfSSowmini Varadhan trans->conn_free(cp->cp_transport_data); 2681c5113cfSSowmini Varadhan } 269cb24405eSAndy Grover kmem_cache_free(rds_conn_slab, conn); 270cb24405eSAndy Grover conn = found; 271cb24405eSAndy Grover } else { 272bcf50ef2SChris Mason hlist_add_head_rcu(&conn->c_hash_node, head); 273cb24405eSAndy Grover rds_cong_add_conn(conn); 274cb24405eSAndy Grover rds_conn_count++; 275cb24405eSAndy Grover } 276cb24405eSAndy Grover } 27700e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 27800e0f34cSAndy Grover 27900e0f34cSAndy Grover out: 28000e0f34cSAndy Grover return conn; 28100e0f34cSAndy Grover } 28200e0f34cSAndy Grover 283d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create(struct net *net, 284d5a8ac28SSowmini Varadhan __be32 laddr, __be32 faddr, 28500e0f34cSAndy Grover struct rds_transport *trans, gfp_t gfp) 28600e0f34cSAndy Grover { 287d5a8ac28SSowmini Varadhan return __rds_conn_create(net, laddr, faddr, trans, gfp, 0); 28800e0f34cSAndy Grover } 289616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create); 29000e0f34cSAndy Grover 291d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create_outgoing(struct net *net, 292d5a8ac28SSowmini Varadhan __be32 laddr, __be32 faddr, 29300e0f34cSAndy Grover struct rds_transport *trans, gfp_t gfp) 29400e0f34cSAndy Grover { 295d5a8ac28SSowmini Varadhan return __rds_conn_create(net, laddr, faddr, trans, gfp, 1); 29600e0f34cSAndy Grover } 297616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create_outgoing); 29800e0f34cSAndy Grover 299d769ef81SSowmini Varadhan void rds_conn_shutdown(struct rds_conn_path *cp) 3002dc39357SAndy Grover { 301d769ef81SSowmini Varadhan struct rds_connection *conn = cp->cp_conn; 302d769ef81SSowmini Varadhan 3032dc39357SAndy Grover /* shut it down unless it's down already */ 304d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) { 3052dc39357SAndy Grover /* 3062dc39357SAndy Grover * Quiesce the connection mgmt handlers before we start tearing 3072dc39357SAndy Grover * things down. We don't hold the mutex for the entire 3082dc39357SAndy Grover * duration of the shutdown operation, else we may be 3092dc39357SAndy Grover * deadlocking with the CM handler. Instead, the CM event 3102dc39357SAndy Grover * handler is supposed to check for state DISCONNECTING 3112dc39357SAndy Grover */ 312d769ef81SSowmini Varadhan mutex_lock(&cp->cp_cm_lock); 313d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_UP, 314d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING) && 315d769ef81SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR, 316d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING)) { 317d769ef81SSowmini Varadhan rds_conn_path_error(cp, 318d769ef81SSowmini Varadhan "shutdown called in state %d\n", 319d769ef81SSowmini Varadhan atomic_read(&cp->cp_state)); 320d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock); 3212dc39357SAndy Grover return; 3222dc39357SAndy Grover } 323d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock); 3242dc39357SAndy Grover 325d769ef81SSowmini Varadhan wait_event(cp->cp_waitq, 326d769ef81SSowmini Varadhan !test_bit(RDS_IN_XMIT, &cp->cp_flags)); 327d769ef81SSowmini Varadhan wait_event(cp->cp_waitq, 328d769ef81SSowmini Varadhan !test_bit(RDS_RECV_REFILL, &cp->cp_flags)); 3297e3f2952SChris Mason 330d769ef81SSowmini Varadhan conn->c_trans->conn_path_shutdown(cp); 331d769ef81SSowmini Varadhan rds_conn_path_reset(cp); 3322dc39357SAndy Grover 333d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING, 334d769ef81SSowmini Varadhan RDS_CONN_DOWN)) { 3352dc39357SAndy Grover /* This can happen - eg when we're in the middle of tearing 3362dc39357SAndy Grover * down the connection, and someone unloads the rds module. 3372dc39357SAndy Grover * Quite reproduceable with loopback connections. 3382dc39357SAndy Grover * Mostly harmless. 3392dc39357SAndy Grover */ 340d769ef81SSowmini Varadhan rds_conn_path_error(cp, "%s: failed to transition " 341d769ef81SSowmini Varadhan "to state DOWN, current state " 342d769ef81SSowmini Varadhan "is %d\n", __func__, 343d769ef81SSowmini Varadhan atomic_read(&cp->cp_state)); 3442dc39357SAndy Grover return; 3452dc39357SAndy Grover } 3462dc39357SAndy Grover } 3472dc39357SAndy Grover 3482dc39357SAndy Grover /* Then reconnect if it's still live. 3492dc39357SAndy Grover * The passive side of an IB loopback connection is never added 3502dc39357SAndy Grover * to the conn hash, so we never trigger a reconnect on this 3512dc39357SAndy Grover * conn - the reconnect is always triggered by the active peer. */ 352d769ef81SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_conn_w); 353bcf50ef2SChris Mason rcu_read_lock(); 354bcf50ef2SChris Mason if (!hlist_unhashed(&conn->c_hash_node)) { 355bcf50ef2SChris Mason rcu_read_unlock(); 356d769ef81SSowmini Varadhan rds_queue_reconnect(cp); 357bcf50ef2SChris Mason } else { 358bcf50ef2SChris Mason rcu_read_unlock(); 359bcf50ef2SChris Mason } 3602dc39357SAndy Grover } 3612dc39357SAndy Grover 3623ecc5693SSowmini Varadhan /* destroy a single rds_conn_path. rds_conn_destroy() iterates over 3633ecc5693SSowmini Varadhan * all paths using rds_conn_path_destroy() 3643ecc5693SSowmini Varadhan */ 3653ecc5693SSowmini Varadhan static void rds_conn_path_destroy(struct rds_conn_path *cp) 3663ecc5693SSowmini Varadhan { 3673ecc5693SSowmini Varadhan struct rds_message *rm, *rtmp; 3683ecc5693SSowmini Varadhan 36902105b2cSSowmini Varadhan if (!cp->cp_transport_data) 37002105b2cSSowmini Varadhan return; 37102105b2cSSowmini Varadhan 3723ecc5693SSowmini Varadhan rds_conn_path_drop(cp); 3733ecc5693SSowmini Varadhan flush_work(&cp->cp_down_w); 3743ecc5693SSowmini Varadhan 3753ecc5693SSowmini Varadhan /* make sure lingering queued work won't try to ref the conn */ 3763ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_send_w); 3773ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_recv_w); 3783ecc5693SSowmini Varadhan 3793ecc5693SSowmini Varadhan /* tear down queued messages */ 3803ecc5693SSowmini Varadhan list_for_each_entry_safe(rm, rtmp, 3813ecc5693SSowmini Varadhan &cp->cp_send_queue, 3823ecc5693SSowmini Varadhan m_conn_item) { 3833ecc5693SSowmini Varadhan list_del_init(&rm->m_conn_item); 3843ecc5693SSowmini Varadhan BUG_ON(!list_empty(&rm->m_sock_item)); 3853ecc5693SSowmini Varadhan rds_message_put(rm); 3863ecc5693SSowmini Varadhan } 3873ecc5693SSowmini Varadhan if (cp->cp_xmit_rm) 3883ecc5693SSowmini Varadhan rds_message_put(cp->cp_xmit_rm); 3893ecc5693SSowmini Varadhan 3903ecc5693SSowmini Varadhan cp->cp_conn->c_trans->conn_free(cp->cp_transport_data); 3913ecc5693SSowmini Varadhan } 3923ecc5693SSowmini Varadhan 3932dc39357SAndy Grover /* 3942dc39357SAndy Grover * Stop and free a connection. 395ffcec0e1SZach Brown * 396ffcec0e1SZach Brown * This can only be used in very limited circumstances. It assumes that once 397ffcec0e1SZach Brown * the conn has been shutdown that no one else is referencing the connection. 398ffcec0e1SZach Brown * We can only ensure this in the rmmod path in the current code. 3992dc39357SAndy Grover */ 40000e0f34cSAndy Grover void rds_conn_destroy(struct rds_connection *conn) 40100e0f34cSAndy Grover { 402fe8ff6b5SZach Brown unsigned long flags; 40302105b2cSSowmini Varadhan int i; 40402105b2cSSowmini Varadhan struct rds_conn_path *cp; 40500e0f34cSAndy Grover 40600e0f34cSAndy Grover rdsdebug("freeing conn %p for %pI4 -> " 40700e0f34cSAndy Grover "%pI4\n", conn, &conn->c_laddr, 40800e0f34cSAndy Grover &conn->c_faddr); 40900e0f34cSAndy Grover 410abf45439SChris Mason /* Ensure conn will not be scheduled for reconnect */ 411abf45439SChris Mason spin_lock_irq(&rds_conn_lock); 412bcf50ef2SChris Mason hlist_del_init_rcu(&conn->c_hash_node); 413abf45439SChris Mason spin_unlock_irq(&rds_conn_lock); 414bcf50ef2SChris Mason synchronize_rcu(); 415bcf50ef2SChris Mason 416ffcec0e1SZach Brown /* shut the connection down */ 4173ecc5693SSowmini Varadhan for (i = 0; i < RDS_MPATH_WORKERS; i++) { 4183ecc5693SSowmini Varadhan cp = &conn->c_path[i]; 4193ecc5693SSowmini Varadhan rds_conn_path_destroy(cp); 4203ecc5693SSowmini Varadhan BUG_ON(!list_empty(&cp->cp_retrans)); 42100e0f34cSAndy Grover } 42200e0f34cSAndy Grover 42300e0f34cSAndy Grover /* 42400e0f34cSAndy Grover * The congestion maps aren't freed up here. They're 42500e0f34cSAndy Grover * freed by rds_cong_exit() after all the connections 42600e0f34cSAndy Grover * have been freed. 42700e0f34cSAndy Grover */ 42800e0f34cSAndy Grover rds_cong_remove_conn(conn); 42900e0f34cSAndy Grover 43000e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 43100e0f34cSAndy Grover 432fe8ff6b5SZach Brown spin_lock_irqsave(&rds_conn_lock, flags); 43300e0f34cSAndy Grover rds_conn_count--; 434fe8ff6b5SZach Brown spin_unlock_irqrestore(&rds_conn_lock, flags); 43500e0f34cSAndy Grover } 436616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_destroy); 43700e0f34cSAndy Grover 43800e0f34cSAndy Grover static void rds_conn_message_info(struct socket *sock, unsigned int len, 43900e0f34cSAndy Grover struct rds_info_iterator *iter, 44000e0f34cSAndy Grover struct rds_info_lengths *lens, 44100e0f34cSAndy Grover int want_send) 44200e0f34cSAndy Grover { 44300e0f34cSAndy Grover struct hlist_head *head; 44400e0f34cSAndy Grover struct list_head *list; 44500e0f34cSAndy Grover struct rds_connection *conn; 44600e0f34cSAndy Grover struct rds_message *rm; 44700e0f34cSAndy Grover unsigned int total = 0; 448501dcccdSZach Brown unsigned long flags; 44900e0f34cSAndy Grover size_t i; 450992c9ec5SSowmini Varadhan int j; 45100e0f34cSAndy Grover 45200e0f34cSAndy Grover len /= sizeof(struct rds_info_message); 45300e0f34cSAndy Grover 454bcf50ef2SChris Mason rcu_read_lock(); 45500e0f34cSAndy Grover 45600e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 45700e0f34cSAndy Grover i++, head++) { 458b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 459992c9ec5SSowmini Varadhan struct rds_conn_path *cp; 46000e0f34cSAndy Grover 461992c9ec5SSowmini Varadhan for (j = 0; j < RDS_MPATH_WORKERS; j++) { 462992c9ec5SSowmini Varadhan cp = &conn->c_path[j]; 463992c9ec5SSowmini Varadhan if (want_send) 464992c9ec5SSowmini Varadhan list = &cp->cp_send_queue; 465992c9ec5SSowmini Varadhan else 466992c9ec5SSowmini Varadhan list = &cp->cp_retrans; 467992c9ec5SSowmini Varadhan 468992c9ec5SSowmini Varadhan spin_lock_irqsave(&cp->cp_lock, flags); 46900e0f34cSAndy Grover 47000e0f34cSAndy Grover /* XXX too lazy to maintain counts.. */ 47100e0f34cSAndy Grover list_for_each_entry(rm, list, m_conn_item) { 47200e0f34cSAndy Grover total++; 47300e0f34cSAndy Grover if (total <= len) 474992c9ec5SSowmini Varadhan rds_inc_info_copy(&rm->m_inc, 475992c9ec5SSowmini Varadhan iter, 47600e0f34cSAndy Grover conn->c_laddr, 477992c9ec5SSowmini Varadhan conn->c_faddr, 478992c9ec5SSowmini Varadhan 0); 47900e0f34cSAndy Grover } 48000e0f34cSAndy Grover 481992c9ec5SSowmini Varadhan spin_unlock_irqrestore(&cp->cp_lock, flags); 482992c9ec5SSowmini Varadhan if (!conn->c_trans->t_mp_capable) 483992c9ec5SSowmini Varadhan break; 484992c9ec5SSowmini Varadhan } 48500e0f34cSAndy Grover } 48600e0f34cSAndy Grover } 487bcf50ef2SChris Mason rcu_read_unlock(); 48800e0f34cSAndy Grover 48900e0f34cSAndy Grover lens->nr = total; 49000e0f34cSAndy Grover lens->each = sizeof(struct rds_info_message); 49100e0f34cSAndy Grover } 49200e0f34cSAndy Grover 49300e0f34cSAndy Grover static void rds_conn_message_info_send(struct socket *sock, unsigned int len, 49400e0f34cSAndy Grover struct rds_info_iterator *iter, 49500e0f34cSAndy Grover struct rds_info_lengths *lens) 49600e0f34cSAndy Grover { 49700e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 1); 49800e0f34cSAndy Grover } 49900e0f34cSAndy Grover 50000e0f34cSAndy Grover static void rds_conn_message_info_retrans(struct socket *sock, 50100e0f34cSAndy Grover unsigned int len, 50200e0f34cSAndy Grover struct rds_info_iterator *iter, 50300e0f34cSAndy Grover struct rds_info_lengths *lens) 50400e0f34cSAndy Grover { 50500e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 0); 50600e0f34cSAndy Grover } 50700e0f34cSAndy Grover 50800e0f34cSAndy Grover void rds_for_each_conn_info(struct socket *sock, unsigned int len, 50900e0f34cSAndy Grover struct rds_info_iterator *iter, 51000e0f34cSAndy Grover struct rds_info_lengths *lens, 51100e0f34cSAndy Grover int (*visitor)(struct rds_connection *, void *), 51200e0f34cSAndy Grover size_t item_len) 51300e0f34cSAndy Grover { 51400e0f34cSAndy Grover uint64_t buffer[(item_len + 7) / 8]; 51500e0f34cSAndy Grover struct hlist_head *head; 51600e0f34cSAndy Grover struct rds_connection *conn; 51700e0f34cSAndy Grover size_t i; 51800e0f34cSAndy Grover 519bcf50ef2SChris Mason rcu_read_lock(); 52000e0f34cSAndy Grover 52100e0f34cSAndy Grover lens->nr = 0; 52200e0f34cSAndy Grover lens->each = item_len; 52300e0f34cSAndy Grover 52400e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 52500e0f34cSAndy Grover i++, head++) { 526b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 52700e0f34cSAndy Grover 52800e0f34cSAndy Grover /* XXX no c_lock usage.. */ 52900e0f34cSAndy Grover if (!visitor(conn, buffer)) 53000e0f34cSAndy Grover continue; 53100e0f34cSAndy Grover 53200e0f34cSAndy Grover /* We copy as much as we can fit in the buffer, 53300e0f34cSAndy Grover * but we count all items so that the caller 53400e0f34cSAndy Grover * can resize the buffer. */ 53500e0f34cSAndy Grover if (len >= item_len) { 53600e0f34cSAndy Grover rds_info_copy(iter, buffer, item_len); 53700e0f34cSAndy Grover len -= item_len; 53800e0f34cSAndy Grover } 53900e0f34cSAndy Grover lens->nr++; 54000e0f34cSAndy Grover } 54100e0f34cSAndy Grover } 542bcf50ef2SChris Mason rcu_read_unlock(); 54300e0f34cSAndy Grover } 544616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_for_each_conn_info); 54500e0f34cSAndy Grover 546992c9ec5SSowmini Varadhan void rds_walk_conn_path_info(struct socket *sock, unsigned int len, 547992c9ec5SSowmini Varadhan struct rds_info_iterator *iter, 548992c9ec5SSowmini Varadhan struct rds_info_lengths *lens, 549992c9ec5SSowmini Varadhan int (*visitor)(struct rds_conn_path *, void *), 550992c9ec5SSowmini Varadhan size_t item_len) 551992c9ec5SSowmini Varadhan { 552992c9ec5SSowmini Varadhan u64 buffer[(item_len + 7) / 8]; 553992c9ec5SSowmini Varadhan struct hlist_head *head; 554992c9ec5SSowmini Varadhan struct rds_connection *conn; 555992c9ec5SSowmini Varadhan size_t i; 556992c9ec5SSowmini Varadhan int j; 557992c9ec5SSowmini Varadhan 558992c9ec5SSowmini Varadhan rcu_read_lock(); 559992c9ec5SSowmini Varadhan 560992c9ec5SSowmini Varadhan lens->nr = 0; 561992c9ec5SSowmini Varadhan lens->each = item_len; 562992c9ec5SSowmini Varadhan 563992c9ec5SSowmini Varadhan for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 564992c9ec5SSowmini Varadhan i++, head++) { 565992c9ec5SSowmini Varadhan hlist_for_each_entry_rcu(conn, head, c_hash_node) { 566992c9ec5SSowmini Varadhan struct rds_conn_path *cp; 567992c9ec5SSowmini Varadhan 568992c9ec5SSowmini Varadhan for (j = 0; j < RDS_MPATH_WORKERS; j++) { 569992c9ec5SSowmini Varadhan cp = &conn->c_path[j]; 570992c9ec5SSowmini Varadhan 571992c9ec5SSowmini Varadhan /* XXX no cp_lock usage.. */ 572992c9ec5SSowmini Varadhan if (!visitor(cp, buffer)) 573992c9ec5SSowmini Varadhan continue; 574992c9ec5SSowmini Varadhan if (!conn->c_trans->t_mp_capable) 575992c9ec5SSowmini Varadhan break; 576992c9ec5SSowmini Varadhan } 577992c9ec5SSowmini Varadhan 578992c9ec5SSowmini Varadhan /* We copy as much as we can fit in the buffer, 579992c9ec5SSowmini Varadhan * but we count all items so that the caller 580992c9ec5SSowmini Varadhan * can resize the buffer. 581992c9ec5SSowmini Varadhan */ 582992c9ec5SSowmini Varadhan if (len >= item_len) { 583992c9ec5SSowmini Varadhan rds_info_copy(iter, buffer, item_len); 584992c9ec5SSowmini Varadhan len -= item_len; 585992c9ec5SSowmini Varadhan } 586992c9ec5SSowmini Varadhan lens->nr++; 587992c9ec5SSowmini Varadhan } 588992c9ec5SSowmini Varadhan } 589992c9ec5SSowmini Varadhan rcu_read_unlock(); 590992c9ec5SSowmini Varadhan } 591992c9ec5SSowmini Varadhan 592992c9ec5SSowmini Varadhan static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer) 59300e0f34cSAndy Grover { 59400e0f34cSAndy Grover struct rds_info_connection *cinfo = buffer; 59500e0f34cSAndy Grover 596992c9ec5SSowmini Varadhan cinfo->next_tx_seq = cp->cp_next_tx_seq; 597992c9ec5SSowmini Varadhan cinfo->next_rx_seq = cp->cp_next_rx_seq; 598992c9ec5SSowmini Varadhan cinfo->laddr = cp->cp_conn->c_laddr; 599992c9ec5SSowmini Varadhan cinfo->faddr = cp->cp_conn->c_faddr; 600992c9ec5SSowmini Varadhan strncpy(cinfo->transport, cp->cp_conn->c_trans->t_name, 60100e0f34cSAndy Grover sizeof(cinfo->transport)); 60200e0f34cSAndy Grover cinfo->flags = 0; 60300e0f34cSAndy Grover 604992c9ec5SSowmini Varadhan rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags), 6050f4b1c7eSZach Brown SENDING); 60600e0f34cSAndy Grover /* XXX Future: return the state rather than these funky bits */ 60700e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 608992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING, 60900e0f34cSAndy Grover CONNECTING); 61000e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 611992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_UP, 61200e0f34cSAndy Grover CONNECTED); 61300e0f34cSAndy Grover return 1; 61400e0f34cSAndy Grover } 61500e0f34cSAndy Grover 61600e0f34cSAndy Grover static void rds_conn_info(struct socket *sock, unsigned int len, 61700e0f34cSAndy Grover struct rds_info_iterator *iter, 61800e0f34cSAndy Grover struct rds_info_lengths *lens) 61900e0f34cSAndy Grover { 620992c9ec5SSowmini Varadhan rds_walk_conn_path_info(sock, len, iter, lens, 62100e0f34cSAndy Grover rds_conn_info_visitor, 62200e0f34cSAndy Grover sizeof(struct rds_info_connection)); 62300e0f34cSAndy Grover } 62400e0f34cSAndy Grover 625ef87b7eaSZach Brown int rds_conn_init(void) 62600e0f34cSAndy Grover { 62700e0f34cSAndy Grover rds_conn_slab = kmem_cache_create("rds_connection", 62800e0f34cSAndy Grover sizeof(struct rds_connection), 62900e0f34cSAndy Grover 0, 0, NULL); 6308690bfa1SAndy Grover if (!rds_conn_slab) 63100e0f34cSAndy Grover return -ENOMEM; 63200e0f34cSAndy Grover 63300e0f34cSAndy Grover rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info); 63400e0f34cSAndy Grover rds_info_register_func(RDS_INFO_SEND_MESSAGES, 63500e0f34cSAndy Grover rds_conn_message_info_send); 63600e0f34cSAndy Grover rds_info_register_func(RDS_INFO_RETRANS_MESSAGES, 63700e0f34cSAndy Grover rds_conn_message_info_retrans); 63800e0f34cSAndy Grover 63900e0f34cSAndy Grover return 0; 64000e0f34cSAndy Grover } 64100e0f34cSAndy Grover 64200e0f34cSAndy Grover void rds_conn_exit(void) 64300e0f34cSAndy Grover { 64400e0f34cSAndy Grover rds_loop_exit(); 64500e0f34cSAndy Grover 64600e0f34cSAndy Grover WARN_ON(!hlist_empty(rds_conn_hash)); 64700e0f34cSAndy Grover 64800e0f34cSAndy Grover kmem_cache_destroy(rds_conn_slab); 64900e0f34cSAndy Grover 65000e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info); 65100e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_SEND_MESSAGES, 65200e0f34cSAndy Grover rds_conn_message_info_send); 65300e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES, 65400e0f34cSAndy Grover rds_conn_message_info_retrans); 65500e0f34cSAndy Grover } 65600e0f34cSAndy Grover 65700e0f34cSAndy Grover /* 65800e0f34cSAndy Grover * Force a disconnect 65900e0f34cSAndy Grover */ 6600cb43965SSowmini Varadhan void rds_conn_path_drop(struct rds_conn_path *cp) 6610cb43965SSowmini Varadhan { 6620cb43965SSowmini Varadhan atomic_set(&cp->cp_state, RDS_CONN_ERROR); 6630cb43965SSowmini Varadhan queue_work(rds_wq, &cp->cp_down_w); 6640cb43965SSowmini Varadhan } 6650cb43965SSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_drop); 6660cb43965SSowmini Varadhan 66700e0f34cSAndy Grover void rds_conn_drop(struct rds_connection *conn) 66800e0f34cSAndy Grover { 669*5916e2c1SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable); 6700cb43965SSowmini Varadhan rds_conn_path_drop(&conn->c_path[0]); 67100e0f34cSAndy Grover } 672616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_drop); 67300e0f34cSAndy Grover 67400e0f34cSAndy Grover /* 675f3c6808dSZach Brown * If the connection is down, trigger a connect. We may have scheduled a 676f3c6808dSZach Brown * delayed reconnect however - in this case we should not interfere. 677f3c6808dSZach Brown */ 6783c0a5900SSowmini Varadhan void rds_conn_path_connect_if_down(struct rds_conn_path *cp) 6793c0a5900SSowmini Varadhan { 6803c0a5900SSowmini Varadhan if (rds_conn_path_state(cp) == RDS_CONN_DOWN && 6813c0a5900SSowmini Varadhan !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags)) 6823c0a5900SSowmini Varadhan queue_delayed_work(rds_wq, &cp->cp_conn_w, 0); 6833c0a5900SSowmini Varadhan } 6843c0a5900SSowmini Varadhan 685f3c6808dSZach Brown void rds_conn_connect_if_down(struct rds_connection *conn) 686f3c6808dSZach Brown { 6873c0a5900SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable); 6883c0a5900SSowmini Varadhan rds_conn_path_connect_if_down(&conn->c_path[0]); 689f3c6808dSZach Brown } 690f3c6808dSZach Brown EXPORT_SYMBOL_GPL(rds_conn_connect_if_down); 691f3c6808dSZach Brown 692f3c6808dSZach Brown /* 69300e0f34cSAndy Grover * An error occurred on the connection 69400e0f34cSAndy Grover */ 69500e0f34cSAndy Grover void 69600e0f34cSAndy Grover __rds_conn_error(struct rds_connection *conn, const char *fmt, ...) 69700e0f34cSAndy Grover { 69800e0f34cSAndy Grover va_list ap; 69900e0f34cSAndy Grover 70000e0f34cSAndy Grover va_start(ap, fmt); 70100e0f34cSAndy Grover vprintk(fmt, ap); 70200e0f34cSAndy Grover va_end(ap); 70300e0f34cSAndy Grover 70400e0f34cSAndy Grover rds_conn_drop(conn); 70500e0f34cSAndy Grover } 706fb1b3dc4SSowmini Varadhan 707fb1b3dc4SSowmini Varadhan void 708fb1b3dc4SSowmini Varadhan __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...) 709fb1b3dc4SSowmini Varadhan { 710fb1b3dc4SSowmini Varadhan va_list ap; 711fb1b3dc4SSowmini Varadhan 712fb1b3dc4SSowmini Varadhan va_start(ap, fmt); 713fb1b3dc4SSowmini Varadhan vprintk(fmt, ap); 714fb1b3dc4SSowmini Varadhan va_end(ap); 715fb1b3dc4SSowmini Varadhan 716fb1b3dc4SSowmini Varadhan rds_conn_path_drop(cp); 717fb1b3dc4SSowmini Varadhan } 718