100e0f34cSAndy Grover /* 21e2b44e7SKa-Cheong Poon * Copyright (c) 2006, 2018 Oracle and/or its affiliates. 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> 37eee2fa6aSKa-Cheong Poon #include <net/ipv6.h> 38eee2fa6aSKa-Cheong Poon #include <net/inet6_hashtables.h> 391e2b44e7SKa-Cheong Poon #include <net/addrconf.h> 4000e0f34cSAndy Grover 4100e0f34cSAndy Grover #include "rds.h" 4200e0f34cSAndy Grover #include "loop.h" 4300e0f34cSAndy Grover 4400e0f34cSAndy Grover #define RDS_CONNECTION_HASH_BITS 12 4500e0f34cSAndy Grover #define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS) 4600e0f34cSAndy Grover #define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1) 4700e0f34cSAndy Grover 4800e0f34cSAndy Grover /* converting this to RCU is a chore for another day.. */ 4900e0f34cSAndy Grover static DEFINE_SPINLOCK(rds_conn_lock); 5000e0f34cSAndy Grover static unsigned long rds_conn_count; 5100e0f34cSAndy Grover static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES]; 5200e0f34cSAndy Grover static struct kmem_cache *rds_conn_slab; 5300e0f34cSAndy Grover 54eee2fa6aSKa-Cheong Poon static struct hlist_head *rds_conn_bucket(const struct in6_addr *laddr, 55eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr) 5600e0f34cSAndy Grover { 57eee2fa6aSKa-Cheong Poon static u32 rds6_hash_secret __read_mostly; 581bbdceefSHannes Frederic Sowa static u32 rds_hash_secret __read_mostly; 591bbdceefSHannes Frederic Sowa 60eee2fa6aSKa-Cheong Poon u32 lhash, fhash, hash; 611bbdceefSHannes Frederic Sowa 621bbdceefSHannes Frederic Sowa net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret)); 63eee2fa6aSKa-Cheong Poon net_get_random_once(&rds6_hash_secret, sizeof(rds6_hash_secret)); 641bbdceefSHannes Frederic Sowa 65eee2fa6aSKa-Cheong Poon lhash = (__force u32)laddr->s6_addr32[3]; 66e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 67eee2fa6aSKa-Cheong Poon fhash = __ipv6_addr_jhash(faddr, rds6_hash_secret); 68e65d4d96SKa-Cheong Poon #else 69e65d4d96SKa-Cheong Poon fhash = (__force u32)faddr->s6_addr32[3]; 70e65d4d96SKa-Cheong Poon #endif 71e65d4d96SKa-Cheong Poon hash = __inet_ehashfn(lhash, 0, fhash, 0, rds_hash_secret); 72eee2fa6aSKa-Cheong Poon 7300e0f34cSAndy Grover return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK]; 7400e0f34cSAndy Grover } 7500e0f34cSAndy Grover 7600e0f34cSAndy Grover #define rds_conn_info_set(var, test, suffix) do { \ 7700e0f34cSAndy Grover if (test) \ 7800e0f34cSAndy Grover var |= RDS_INFO_CONNECTION_FLAG_##suffix; \ 7900e0f34cSAndy Grover } while (0) 8000e0f34cSAndy Grover 81bcf50ef2SChris Mason /* rcu read lock must be held or the connection spinlock */ 828f384c01SSowmini Varadhan static struct rds_connection *rds_conn_lookup(struct net *net, 838f384c01SSowmini Varadhan struct hlist_head *head, 84eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr, 85eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr, 86eee2fa6aSKa-Cheong Poon struct rds_transport *trans, 873eb45036SSantosh Shilimkar u8 tos, int dev_if) 8800e0f34cSAndy Grover { 8900e0f34cSAndy Grover struct rds_connection *conn, *ret = NULL; 9000e0f34cSAndy Grover 91b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 92eee2fa6aSKa-Cheong Poon if (ipv6_addr_equal(&conn->c_faddr, faddr) && 93eee2fa6aSKa-Cheong Poon ipv6_addr_equal(&conn->c_laddr, laddr) && 94eee2fa6aSKa-Cheong Poon conn->c_trans == trans && 953eb45036SSantosh Shilimkar conn->c_tos == tos && 96eee2fa6aSKa-Cheong Poon net == rds_conn_net(conn) && 97eee2fa6aSKa-Cheong Poon conn->c_dev_if == dev_if) { 9800e0f34cSAndy Grover ret = conn; 9900e0f34cSAndy Grover break; 10000e0f34cSAndy Grover } 10100e0f34cSAndy Grover } 102eee2fa6aSKa-Cheong Poon rdsdebug("returning conn %p for %pI6c -> %pI6c\n", ret, 103eee2fa6aSKa-Cheong Poon laddr, faddr); 10400e0f34cSAndy Grover return ret; 10500e0f34cSAndy Grover } 10600e0f34cSAndy Grover 10700e0f34cSAndy Grover /* 10800e0f34cSAndy Grover * This is called by transports as they're bringing down a connection. 10900e0f34cSAndy Grover * It clears partial message state so that the transport can start sending 11000e0f34cSAndy Grover * and receiving over this connection again in the future. It is up to 11100e0f34cSAndy Grover * the transport to have serialized this call with its send and recv. 11200e0f34cSAndy Grover */ 113d769ef81SSowmini Varadhan static void rds_conn_path_reset(struct rds_conn_path *cp) 11400e0f34cSAndy Grover { 115d769ef81SSowmini Varadhan struct rds_connection *conn = cp->cp_conn; 116d769ef81SSowmini Varadhan 117eee2fa6aSKa-Cheong Poon rdsdebug("connection %pI6c to %pI6c reset\n", 11800e0f34cSAndy Grover &conn->c_laddr, &conn->c_faddr); 11900e0f34cSAndy Grover 12000e0f34cSAndy Grover rds_stats_inc(s_conn_reset); 121d769ef81SSowmini Varadhan rds_send_path_reset(cp); 122d769ef81SSowmini Varadhan cp->cp_flags = 0; 12300e0f34cSAndy Grover 12400e0f34cSAndy Grover /* Do not clear next_rx_seq here, else we cannot distinguish 12500e0f34cSAndy Grover * retransmitted packets from new packets, and will hand all 12600e0f34cSAndy Grover * of them to the application. That is not consistent with the 12700e0f34cSAndy Grover * reliability guarantees of RDS. */ 12800e0f34cSAndy Grover } 12900e0f34cSAndy Grover 1301c5113cfSSowmini Varadhan static void __rds_conn_path_init(struct rds_connection *conn, 1311c5113cfSSowmini Varadhan struct rds_conn_path *cp, bool is_outgoing) 1321c5113cfSSowmini Varadhan { 1331c5113cfSSowmini Varadhan spin_lock_init(&cp->cp_lock); 1341c5113cfSSowmini Varadhan cp->cp_next_tx_seq = 1; 1351c5113cfSSowmini Varadhan init_waitqueue_head(&cp->cp_waitq); 1361c5113cfSSowmini Varadhan INIT_LIST_HEAD(&cp->cp_send_queue); 1371c5113cfSSowmini Varadhan INIT_LIST_HEAD(&cp->cp_retrans); 1381c5113cfSSowmini Varadhan 1391c5113cfSSowmini Varadhan cp->cp_conn = conn; 1401c5113cfSSowmini Varadhan atomic_set(&cp->cp_state, RDS_CONN_DOWN); 1411c5113cfSSowmini Varadhan cp->cp_send_gen = 0; 1421c5113cfSSowmini Varadhan cp->cp_reconnect_jiffies = 0; 143cdc306a5SSantosh Shilimkar cp->cp_conn->c_proposed_version = RDS_PROTOCOL_VERSION; 1441c5113cfSSowmini Varadhan INIT_DELAYED_WORK(&cp->cp_send_w, rds_send_worker); 1451c5113cfSSowmini Varadhan INIT_DELAYED_WORK(&cp->cp_recv_w, rds_recv_worker); 1461c5113cfSSowmini Varadhan INIT_DELAYED_WORK(&cp->cp_conn_w, rds_connect_worker); 1471c5113cfSSowmini Varadhan INIT_WORK(&cp->cp_down_w, rds_shutdown_worker); 1481c5113cfSSowmini Varadhan mutex_init(&cp->cp_cm_lock); 1491c5113cfSSowmini Varadhan cp->cp_flags = 0; 1501c5113cfSSowmini Varadhan } 1511c5113cfSSowmini Varadhan 15200e0f34cSAndy Grover /* 15300e0f34cSAndy Grover * There is only every one 'conn' for a given pair of addresses in the 15400e0f34cSAndy Grover * system at a time. They contain messages to be retransmitted and so 15500e0f34cSAndy Grover * span the lifetime of the actual underlying transport connections. 15600e0f34cSAndy Grover * 15700e0f34cSAndy Grover * For now they are not garbage collected once they're created. They 15800e0f34cSAndy Grover * are torn down as the module is removed, if ever. 15900e0f34cSAndy Grover */ 160d5a8ac28SSowmini Varadhan static struct rds_connection *__rds_conn_create(struct net *net, 161eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr, 162eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr, 163eee2fa6aSKa-Cheong Poon struct rds_transport *trans, 1643eb45036SSantosh Shilimkar gfp_t gfp, u8 tos, 165eee2fa6aSKa-Cheong Poon int is_outgoing, 166eee2fa6aSKa-Cheong Poon int dev_if) 16700e0f34cSAndy Grover { 168cb24405eSAndy Grover struct rds_connection *conn, *parent = NULL; 16900e0f34cSAndy Grover struct hlist_head *head = rds_conn_bucket(laddr, faddr); 1705adb5bc6SZach Brown struct rds_transport *loop_trans; 17100e0f34cSAndy Grover unsigned long flags; 1725916e2c1SSowmini Varadhan int ret, i; 173840df162SSowmini Varadhan int npaths = (trans->t_mp_capable ? RDS_MPATH_WORKERS : 1); 17400e0f34cSAndy Grover 175bcf50ef2SChris Mason rcu_read_lock(); 1763eb45036SSantosh Shilimkar conn = rds_conn_lookup(net, head, laddr, faddr, trans, tos, dev_if); 177eee2fa6aSKa-Cheong Poon if (conn && 178eee2fa6aSKa-Cheong Poon conn->c_loopback && 179eee2fa6aSKa-Cheong Poon conn->c_trans != &rds_loop_transport && 180eee2fa6aSKa-Cheong Poon ipv6_addr_equal(laddr, faddr) && 181eee2fa6aSKa-Cheong Poon !is_outgoing) { 18200e0f34cSAndy Grover /* This is a looped back IB connection, and we're 18300e0f34cSAndy Grover * called by the code handling the incoming connect. 18400e0f34cSAndy Grover * We need a second connection object into which we 18500e0f34cSAndy Grover * can stick the other QP. */ 18600e0f34cSAndy Grover parent = conn; 18700e0f34cSAndy Grover conn = parent->c_passive; 18800e0f34cSAndy Grover } 189bcf50ef2SChris Mason rcu_read_unlock(); 19000e0f34cSAndy Grover if (conn) 19100e0f34cSAndy Grover goto out; 19200e0f34cSAndy Grover 19305a178ecSWei Yongjun conn = kmem_cache_zalloc(rds_conn_slab, gfp); 1948690bfa1SAndy Grover if (!conn) { 19500e0f34cSAndy Grover conn = ERR_PTR(-ENOMEM); 19600e0f34cSAndy Grover goto out; 19700e0f34cSAndy Grover } 198840df162SSowmini Varadhan conn->c_path = kcalloc(npaths, sizeof(struct rds_conn_path), gfp); 199840df162SSowmini Varadhan if (!conn->c_path) { 200840df162SSowmini Varadhan kmem_cache_free(rds_conn_slab, conn); 201840df162SSowmini Varadhan conn = ERR_PTR(-ENOMEM); 202840df162SSowmini Varadhan goto out; 203840df162SSowmini Varadhan } 20400e0f34cSAndy Grover 20500e0f34cSAndy Grover INIT_HLIST_NODE(&conn->c_hash_node); 206eee2fa6aSKa-Cheong Poon conn->c_laddr = *laddr; 207eee2fa6aSKa-Cheong Poon conn->c_isv6 = !ipv6_addr_v4mapped(laddr); 208eee2fa6aSKa-Cheong Poon conn->c_faddr = *faddr; 209eee2fa6aSKa-Cheong Poon conn->c_dev_if = dev_if; 2103eb45036SSantosh Shilimkar conn->c_tos = tos; 211e65d4d96SKa-Cheong Poon 212e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 2131e2b44e7SKa-Cheong Poon /* If the local address is link local, set c_bound_if to be the 2141e2b44e7SKa-Cheong Poon * index used for this connection. Otherwise, set it to 0 as 2151e2b44e7SKa-Cheong Poon * the socket is not bound to an interface. c_bound_if is used 2161e2b44e7SKa-Cheong Poon * to look up a socket when a packet is received 2171e2b44e7SKa-Cheong Poon */ 2181e2b44e7SKa-Cheong Poon if (ipv6_addr_type(laddr) & IPV6_ADDR_LINKLOCAL) 2191e2b44e7SKa-Cheong Poon conn->c_bound_if = dev_if; 2201e2b44e7SKa-Cheong Poon else 221e65d4d96SKa-Cheong Poon #endif 2221e2b44e7SKa-Cheong Poon conn->c_bound_if = 0; 22300e0f34cSAndy Grover 2241c5113cfSSowmini Varadhan rds_conn_net_set(conn, net); 22500e0f34cSAndy Grover 22600e0f34cSAndy Grover ret = rds_cong_get_maps(conn); 22700e0f34cSAndy Grover if (ret) { 228840df162SSowmini Varadhan kfree(conn->c_path); 22900e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 23000e0f34cSAndy Grover conn = ERR_PTR(ret); 23100e0f34cSAndy Grover goto out; 23200e0f34cSAndy Grover } 23300e0f34cSAndy Grover 23400e0f34cSAndy Grover /* 23500e0f34cSAndy Grover * This is where a connection becomes loopback. If *any* RDS sockets 23600e0f34cSAndy Grover * can bind to the destination address then we'd rather the messages 23700e0f34cSAndy Grover * flow through loopback rather than either transport. 23800e0f34cSAndy Grover */ 239eee2fa6aSKa-Cheong Poon loop_trans = rds_trans_get_preferred(net, faddr, conn->c_dev_if); 2405adb5bc6SZach Brown if (loop_trans) { 2415adb5bc6SZach Brown rds_trans_put(loop_trans); 24200e0f34cSAndy Grover conn->c_loopback = 1; 243*aced3ce5SRao Shoaib if (trans->t_prefer_loopback) { 244*aced3ce5SRao Shoaib if (likely(is_outgoing)) { 245*aced3ce5SRao Shoaib /* "outgoing" connection to local address. 246*aced3ce5SRao Shoaib * Protocol says it wants the connection 247*aced3ce5SRao Shoaib * handled by the loopback transport. 248*aced3ce5SRao Shoaib * This is what TCP does. 24900e0f34cSAndy Grover */ 25000e0f34cSAndy Grover trans = &rds_loop_transport; 251*aced3ce5SRao Shoaib } else { 252*aced3ce5SRao Shoaib /* No transport currently in use 253*aced3ce5SRao Shoaib * should end up here, but if it 254*aced3ce5SRao Shoaib * does, reset/destroy the connection. 255*aced3ce5SRao Shoaib */ 256*aced3ce5SRao Shoaib kmem_cache_free(rds_conn_slab, conn); 257*aced3ce5SRao Shoaib conn = ERR_PTR(-EOPNOTSUPP); 258*aced3ce5SRao Shoaib goto out; 259*aced3ce5SRao Shoaib } 26000e0f34cSAndy Grover } 26100e0f34cSAndy Grover } 26200e0f34cSAndy Grover 26300e0f34cSAndy Grover conn->c_trans = trans; 26400e0f34cSAndy Grover 2655916e2c1SSowmini Varadhan init_waitqueue_head(&conn->c_hs_waitq); 266840df162SSowmini Varadhan for (i = 0; i < npaths; i++) { 2675916e2c1SSowmini Varadhan __rds_conn_path_init(conn, &conn->c_path[i], 2685916e2c1SSowmini Varadhan is_outgoing); 2695916e2c1SSowmini Varadhan conn->c_path[i].cp_index = i; 2705916e2c1SSowmini Varadhan } 271ebeeb1adSSowmini Varadhan rcu_read_lock(); 272ebeeb1adSSowmini Varadhan if (rds_destroy_pending(conn)) 273ebeeb1adSSowmini Varadhan ret = -ENETDOWN; 274ebeeb1adSSowmini Varadhan else 275d4014d8cSSowmini Varadhan ret = trans->conn_alloc(conn, GFP_ATOMIC); 27600e0f34cSAndy Grover if (ret) { 277ebeeb1adSSowmini Varadhan rcu_read_unlock(); 278840df162SSowmini Varadhan kfree(conn->c_path); 27900e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 28000e0f34cSAndy Grover conn = ERR_PTR(ret); 28100e0f34cSAndy Grover goto out; 28200e0f34cSAndy Grover } 28300e0f34cSAndy Grover 284eee2fa6aSKa-Cheong Poon rdsdebug("allocated conn %p for %pI6c -> %pI6c over %s %s\n", 285eee2fa6aSKa-Cheong Poon conn, laddr, faddr, 286eee2fa6aSKa-Cheong Poon strnlen(trans->t_name, sizeof(trans->t_name)) ? 287eee2fa6aSKa-Cheong Poon trans->t_name : "[unknown]", is_outgoing ? "(outgoing)" : ""); 28800e0f34cSAndy Grover 289cb24405eSAndy Grover /* 290cb24405eSAndy Grover * Since we ran without holding the conn lock, someone could 291cb24405eSAndy Grover * have created the same conn (either normal or passive) in the 292cb24405eSAndy Grover * interim. We check while holding the lock. If we won, we complete 293cb24405eSAndy Grover * init and return our conn. If we lost, we rollback and return the 294cb24405eSAndy Grover * other one. 295cb24405eSAndy Grover */ 29600e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 297cb24405eSAndy Grover if (parent) { 298cb24405eSAndy Grover /* Creating passive conn */ 299cb24405eSAndy Grover if (parent->c_passive) { 3001c5113cfSSowmini Varadhan trans->conn_free(conn->c_path[0].cp_transport_data); 301840df162SSowmini Varadhan kfree(conn->c_path); 30200e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 303cb24405eSAndy Grover conn = parent->c_passive; 30400e0f34cSAndy Grover } else { 305cb24405eSAndy Grover parent->c_passive = conn; 30600e0f34cSAndy Grover rds_cong_add_conn(conn); 30700e0f34cSAndy Grover rds_conn_count++; 30800e0f34cSAndy Grover } 309cb24405eSAndy Grover } else { 310cb24405eSAndy Grover /* Creating normal conn */ 311cb24405eSAndy Grover struct rds_connection *found; 31200e0f34cSAndy Grover 313eee2fa6aSKa-Cheong Poon found = rds_conn_lookup(net, head, laddr, faddr, trans, 3143eb45036SSantosh Shilimkar tos, dev_if); 315cb24405eSAndy Grover if (found) { 3161c5113cfSSowmini Varadhan struct rds_conn_path *cp; 3171c5113cfSSowmini Varadhan int i; 3181c5113cfSSowmini Varadhan 319840df162SSowmini Varadhan for (i = 0; i < npaths; i++) { 3201c5113cfSSowmini Varadhan cp = &conn->c_path[i]; 32102105b2cSSowmini Varadhan /* The ->conn_alloc invocation may have 32202105b2cSSowmini Varadhan * allocated resource for all paths, so all 32302105b2cSSowmini Varadhan * of them may have to be freed here. 32402105b2cSSowmini Varadhan */ 32502105b2cSSowmini Varadhan if (cp->cp_transport_data) 3261c5113cfSSowmini Varadhan trans->conn_free(cp->cp_transport_data); 3271c5113cfSSowmini Varadhan } 328840df162SSowmini Varadhan kfree(conn->c_path); 329cb24405eSAndy Grover kmem_cache_free(rds_conn_slab, conn); 330cb24405eSAndy Grover conn = found; 331cb24405eSAndy Grover } else { 332905dd418SSowmini Varadhan conn->c_my_gen_num = rds_gen_num; 333905dd418SSowmini Varadhan conn->c_peer_gen_num = 0; 334bcf50ef2SChris Mason hlist_add_head_rcu(&conn->c_hash_node, head); 335cb24405eSAndy Grover rds_cong_add_conn(conn); 336cb24405eSAndy Grover rds_conn_count++; 337cb24405eSAndy Grover } 338cb24405eSAndy Grover } 33900e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 340ebeeb1adSSowmini Varadhan rcu_read_unlock(); 34100e0f34cSAndy Grover 34200e0f34cSAndy Grover out: 34300e0f34cSAndy Grover return conn; 34400e0f34cSAndy Grover } 34500e0f34cSAndy Grover 346d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create(struct net *net, 347eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr, 348eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr, 3493eb45036SSantosh Shilimkar struct rds_transport *trans, u8 tos, 3503eb45036SSantosh Shilimkar gfp_t gfp, int dev_if) 35100e0f34cSAndy Grover { 3523eb45036SSantosh Shilimkar return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 0, dev_if); 35300e0f34cSAndy Grover } 354616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create); 35500e0f34cSAndy Grover 356d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create_outgoing(struct net *net, 357eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr, 358eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr, 359eee2fa6aSKa-Cheong Poon struct rds_transport *trans, 3603eb45036SSantosh Shilimkar u8 tos, gfp_t gfp, int dev_if) 36100e0f34cSAndy Grover { 3623eb45036SSantosh Shilimkar return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 1, dev_if); 36300e0f34cSAndy Grover } 364616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create_outgoing); 36500e0f34cSAndy Grover 366d769ef81SSowmini Varadhan void rds_conn_shutdown(struct rds_conn_path *cp) 3672dc39357SAndy Grover { 368d769ef81SSowmini Varadhan struct rds_connection *conn = cp->cp_conn; 369d769ef81SSowmini Varadhan 3702dc39357SAndy Grover /* shut it down unless it's down already */ 371d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) { 3722dc39357SAndy Grover /* 3732dc39357SAndy Grover * Quiesce the connection mgmt handlers before we start tearing 3742dc39357SAndy Grover * things down. We don't hold the mutex for the entire 3752dc39357SAndy Grover * duration of the shutdown operation, else we may be 3762dc39357SAndy Grover * deadlocking with the CM handler. Instead, the CM event 3772dc39357SAndy Grover * handler is supposed to check for state DISCONNECTING 3782dc39357SAndy Grover */ 379d769ef81SSowmini Varadhan mutex_lock(&cp->cp_cm_lock); 380d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_UP, 381d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING) && 382d769ef81SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR, 383d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING)) { 384d769ef81SSowmini Varadhan rds_conn_path_error(cp, 385d769ef81SSowmini Varadhan "shutdown called in state %d\n", 386d769ef81SSowmini Varadhan atomic_read(&cp->cp_state)); 387d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock); 3882dc39357SAndy Grover return; 3892dc39357SAndy Grover } 390d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock); 3912dc39357SAndy Grover 392d769ef81SSowmini Varadhan wait_event(cp->cp_waitq, 393d769ef81SSowmini Varadhan !test_bit(RDS_IN_XMIT, &cp->cp_flags)); 394d769ef81SSowmini Varadhan wait_event(cp->cp_waitq, 395d769ef81SSowmini Varadhan !test_bit(RDS_RECV_REFILL, &cp->cp_flags)); 3967e3f2952SChris Mason 397d769ef81SSowmini Varadhan conn->c_trans->conn_path_shutdown(cp); 398d769ef81SSowmini Varadhan rds_conn_path_reset(cp); 3992dc39357SAndy Grover 400d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING, 401e97656d0SSowmini Varadhan RDS_CONN_DOWN) && 402e97656d0SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR, 403d769ef81SSowmini Varadhan RDS_CONN_DOWN)) { 4042dc39357SAndy Grover /* This can happen - eg when we're in the middle of tearing 4052dc39357SAndy Grover * down the connection, and someone unloads the rds module. 406e97656d0SSowmini Varadhan * Quite reproducible with loopback connections. 4072dc39357SAndy Grover * Mostly harmless. 408e97656d0SSowmini Varadhan * 409e97656d0SSowmini Varadhan * Note that this also happens with rds-tcp because 410e97656d0SSowmini Varadhan * we could have triggered rds_conn_path_drop in irq 411e97656d0SSowmini Varadhan * mode from rds_tcp_state change on the receipt of 412e97656d0SSowmini Varadhan * a FIN, thus we need to recheck for RDS_CONN_ERROR 413e97656d0SSowmini Varadhan * here. 4142dc39357SAndy Grover */ 415d769ef81SSowmini Varadhan rds_conn_path_error(cp, "%s: failed to transition " 416d769ef81SSowmini Varadhan "to state DOWN, current state " 417d769ef81SSowmini Varadhan "is %d\n", __func__, 418d769ef81SSowmini Varadhan atomic_read(&cp->cp_state)); 4192dc39357SAndy Grover return; 4202dc39357SAndy Grover } 4212dc39357SAndy Grover } 4222dc39357SAndy Grover 4232dc39357SAndy Grover /* Then reconnect if it's still live. 4242dc39357SAndy Grover * The passive side of an IB loopback connection is never added 4252dc39357SAndy Grover * to the conn hash, so we never trigger a reconnect on this 4262dc39357SAndy Grover * conn - the reconnect is always triggered by the active peer. */ 427d769ef81SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_conn_w); 428bcf50ef2SChris Mason rcu_read_lock(); 429bcf50ef2SChris Mason if (!hlist_unhashed(&conn->c_hash_node)) { 430bcf50ef2SChris Mason rcu_read_unlock(); 431d769ef81SSowmini Varadhan rds_queue_reconnect(cp); 432bcf50ef2SChris Mason } else { 433bcf50ef2SChris Mason rcu_read_unlock(); 434bcf50ef2SChris Mason } 4352dc39357SAndy Grover } 4362dc39357SAndy Grover 4373ecc5693SSowmini Varadhan /* destroy a single rds_conn_path. rds_conn_destroy() iterates over 4383ecc5693SSowmini Varadhan * all paths using rds_conn_path_destroy() 4393ecc5693SSowmini Varadhan */ 4403ecc5693SSowmini Varadhan static void rds_conn_path_destroy(struct rds_conn_path *cp) 4413ecc5693SSowmini Varadhan { 4423ecc5693SSowmini Varadhan struct rds_message *rm, *rtmp; 4433ecc5693SSowmini Varadhan 44402105b2cSSowmini Varadhan if (!cp->cp_transport_data) 44502105b2cSSowmini Varadhan return; 44602105b2cSSowmini Varadhan 4473ecc5693SSowmini Varadhan /* make sure lingering queued work won't try to ref the conn */ 4483ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_send_w); 4493ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_recv_w); 4503ecc5693SSowmini Varadhan 451aed20a53SSowmini Varadhan rds_conn_path_drop(cp, true); 452aed20a53SSowmini Varadhan flush_work(&cp->cp_down_w); 453aed20a53SSowmini Varadhan 4543ecc5693SSowmini Varadhan /* tear down queued messages */ 4553ecc5693SSowmini Varadhan list_for_each_entry_safe(rm, rtmp, 4563ecc5693SSowmini Varadhan &cp->cp_send_queue, 4573ecc5693SSowmini Varadhan m_conn_item) { 4583ecc5693SSowmini Varadhan list_del_init(&rm->m_conn_item); 4593ecc5693SSowmini Varadhan BUG_ON(!list_empty(&rm->m_sock_item)); 4603ecc5693SSowmini Varadhan rds_message_put(rm); 4613ecc5693SSowmini Varadhan } 4623ecc5693SSowmini Varadhan if (cp->cp_xmit_rm) 4633ecc5693SSowmini Varadhan rds_message_put(cp->cp_xmit_rm); 4643ecc5693SSowmini Varadhan 4653db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_send_w)); 4663db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_recv_w)); 4673db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_conn_w)); 4683db6e0d1SSowmini Varadhan WARN_ON(work_pending(&cp->cp_down_w)); 4693db6e0d1SSowmini Varadhan 4703ecc5693SSowmini Varadhan cp->cp_conn->c_trans->conn_free(cp->cp_transport_data); 4713ecc5693SSowmini Varadhan } 4723ecc5693SSowmini Varadhan 4732dc39357SAndy Grover /* 4742dc39357SAndy Grover * Stop and free a connection. 475ffcec0e1SZach Brown * 476ffcec0e1SZach Brown * This can only be used in very limited circumstances. It assumes that once 477ffcec0e1SZach Brown * the conn has been shutdown that no one else is referencing the connection. 478ffcec0e1SZach Brown * We can only ensure this in the rmmod path in the current code. 4792dc39357SAndy Grover */ 48000e0f34cSAndy Grover void rds_conn_destroy(struct rds_connection *conn) 48100e0f34cSAndy Grover { 482fe8ff6b5SZach Brown unsigned long flags; 48302105b2cSSowmini Varadhan int i; 48402105b2cSSowmini Varadhan struct rds_conn_path *cp; 485840df162SSowmini Varadhan int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1); 48600e0f34cSAndy Grover 48700e0f34cSAndy Grover rdsdebug("freeing conn %p for %pI4 -> " 48800e0f34cSAndy Grover "%pI4\n", conn, &conn->c_laddr, 48900e0f34cSAndy Grover &conn->c_faddr); 49000e0f34cSAndy Grover 491abf45439SChris Mason /* Ensure conn will not be scheduled for reconnect */ 492abf45439SChris Mason spin_lock_irq(&rds_conn_lock); 493bcf50ef2SChris Mason hlist_del_init_rcu(&conn->c_hash_node); 494abf45439SChris Mason spin_unlock_irq(&rds_conn_lock); 495bcf50ef2SChris Mason synchronize_rcu(); 496bcf50ef2SChris Mason 497ffcec0e1SZach Brown /* shut the connection down */ 498840df162SSowmini Varadhan for (i = 0; i < npaths; i++) { 4993ecc5693SSowmini Varadhan cp = &conn->c_path[i]; 5003ecc5693SSowmini Varadhan rds_conn_path_destroy(cp); 5013ecc5693SSowmini Varadhan BUG_ON(!list_empty(&cp->cp_retrans)); 50200e0f34cSAndy Grover } 50300e0f34cSAndy Grover 50400e0f34cSAndy Grover /* 50500e0f34cSAndy Grover * The congestion maps aren't freed up here. They're 50600e0f34cSAndy Grover * freed by rds_cong_exit() after all the connections 50700e0f34cSAndy Grover * have been freed. 50800e0f34cSAndy Grover */ 50900e0f34cSAndy Grover rds_cong_remove_conn(conn); 51000e0f34cSAndy Grover 511840df162SSowmini Varadhan kfree(conn->c_path); 51200e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 51300e0f34cSAndy Grover 514fe8ff6b5SZach Brown spin_lock_irqsave(&rds_conn_lock, flags); 51500e0f34cSAndy Grover rds_conn_count--; 516fe8ff6b5SZach Brown spin_unlock_irqrestore(&rds_conn_lock, flags); 51700e0f34cSAndy Grover } 518616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_destroy); 51900e0f34cSAndy Grover 5201e2b44e7SKa-Cheong Poon static void __rds_inc_msg_cp(struct rds_incoming *inc, 5211e2b44e7SKa-Cheong Poon struct rds_info_iterator *iter, 522b7ff8b10SKa-Cheong Poon void *saddr, void *daddr, int flip, bool isv6) 5231e2b44e7SKa-Cheong Poon { 524e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 525b7ff8b10SKa-Cheong Poon if (isv6) 526b7ff8b10SKa-Cheong Poon rds6_inc_info_copy(inc, iter, saddr, daddr, flip); 527b7ff8b10SKa-Cheong Poon else 528e65d4d96SKa-Cheong Poon #endif 5291e2b44e7SKa-Cheong Poon rds_inc_info_copy(inc, iter, *(__be32 *)saddr, 5301e2b44e7SKa-Cheong Poon *(__be32 *)daddr, flip); 5311e2b44e7SKa-Cheong Poon } 5321e2b44e7SKa-Cheong Poon 5331e2b44e7SKa-Cheong Poon static void rds_conn_message_info_cmn(struct socket *sock, unsigned int len, 53400e0f34cSAndy Grover struct rds_info_iterator *iter, 53500e0f34cSAndy Grover struct rds_info_lengths *lens, 536b7ff8b10SKa-Cheong Poon int want_send, bool isv6) 53700e0f34cSAndy Grover { 53800e0f34cSAndy Grover struct hlist_head *head; 53900e0f34cSAndy Grover struct list_head *list; 54000e0f34cSAndy Grover struct rds_connection *conn; 54100e0f34cSAndy Grover struct rds_message *rm; 54200e0f34cSAndy Grover unsigned int total = 0; 543501dcccdSZach Brown unsigned long flags; 54400e0f34cSAndy Grover size_t i; 545992c9ec5SSowmini Varadhan int j; 54600e0f34cSAndy Grover 547b7ff8b10SKa-Cheong Poon if (isv6) 548b7ff8b10SKa-Cheong Poon len /= sizeof(struct rds6_info_message); 549b7ff8b10SKa-Cheong Poon else 55000e0f34cSAndy Grover len /= sizeof(struct rds_info_message); 55100e0f34cSAndy Grover 552bcf50ef2SChris Mason rcu_read_lock(); 55300e0f34cSAndy Grover 55400e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 55500e0f34cSAndy Grover i++, head++) { 556b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 557992c9ec5SSowmini Varadhan struct rds_conn_path *cp; 558840df162SSowmini Varadhan int npaths; 55900e0f34cSAndy Grover 560b7ff8b10SKa-Cheong Poon if (!isv6 && conn->c_isv6) 561b7ff8b10SKa-Cheong Poon continue; 562b7ff8b10SKa-Cheong Poon 563840df162SSowmini Varadhan npaths = (conn->c_trans->t_mp_capable ? 564840df162SSowmini Varadhan RDS_MPATH_WORKERS : 1); 565840df162SSowmini Varadhan 566840df162SSowmini Varadhan for (j = 0; j < npaths; j++) { 567992c9ec5SSowmini Varadhan cp = &conn->c_path[j]; 568992c9ec5SSowmini Varadhan if (want_send) 569992c9ec5SSowmini Varadhan list = &cp->cp_send_queue; 570992c9ec5SSowmini Varadhan else 571992c9ec5SSowmini Varadhan list = &cp->cp_retrans; 572992c9ec5SSowmini Varadhan 573992c9ec5SSowmini Varadhan spin_lock_irqsave(&cp->cp_lock, flags); 57400e0f34cSAndy Grover 57500e0f34cSAndy Grover /* XXX too lazy to maintain counts.. */ 57600e0f34cSAndy Grover list_for_each_entry(rm, list, m_conn_item) { 57700e0f34cSAndy Grover total++; 57800e0f34cSAndy Grover if (total <= len) 5791e2b44e7SKa-Cheong Poon __rds_inc_msg_cp(&rm->m_inc, 580992c9ec5SSowmini Varadhan iter, 5811e2b44e7SKa-Cheong Poon &conn->c_laddr, 5821e2b44e7SKa-Cheong Poon &conn->c_faddr, 583b7ff8b10SKa-Cheong Poon 0, isv6); 58400e0f34cSAndy Grover } 58500e0f34cSAndy Grover 586992c9ec5SSowmini Varadhan spin_unlock_irqrestore(&cp->cp_lock, flags); 587992c9ec5SSowmini Varadhan } 58800e0f34cSAndy Grover } 58900e0f34cSAndy Grover } 590bcf50ef2SChris Mason rcu_read_unlock(); 59100e0f34cSAndy Grover 59200e0f34cSAndy Grover lens->nr = total; 593b7ff8b10SKa-Cheong Poon if (isv6) 594b7ff8b10SKa-Cheong Poon lens->each = sizeof(struct rds6_info_message); 595b7ff8b10SKa-Cheong Poon else 59600e0f34cSAndy Grover lens->each = sizeof(struct rds_info_message); 59700e0f34cSAndy Grover } 59800e0f34cSAndy Grover 5991e2b44e7SKa-Cheong Poon static void rds_conn_message_info(struct socket *sock, unsigned int len, 6001e2b44e7SKa-Cheong Poon struct rds_info_iterator *iter, 6011e2b44e7SKa-Cheong Poon struct rds_info_lengths *lens, 6021e2b44e7SKa-Cheong Poon int want_send) 6031e2b44e7SKa-Cheong Poon { 604b7ff8b10SKa-Cheong Poon rds_conn_message_info_cmn(sock, len, iter, lens, want_send, false); 605b7ff8b10SKa-Cheong Poon } 606b7ff8b10SKa-Cheong Poon 607e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 608b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info(struct socket *sock, unsigned int len, 609b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 610b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens, 611b7ff8b10SKa-Cheong Poon int want_send) 612b7ff8b10SKa-Cheong Poon { 613b7ff8b10SKa-Cheong Poon rds_conn_message_info_cmn(sock, len, iter, lens, want_send, true); 6141e2b44e7SKa-Cheong Poon } 615e65d4d96SKa-Cheong Poon #endif 6161e2b44e7SKa-Cheong Poon 61700e0f34cSAndy Grover static void rds_conn_message_info_send(struct socket *sock, unsigned int len, 61800e0f34cSAndy Grover struct rds_info_iterator *iter, 61900e0f34cSAndy Grover struct rds_info_lengths *lens) 62000e0f34cSAndy Grover { 62100e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 1); 62200e0f34cSAndy Grover } 62300e0f34cSAndy Grover 624e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 625b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info_send(struct socket *sock, unsigned int len, 626b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 627b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens) 628b7ff8b10SKa-Cheong Poon { 629b7ff8b10SKa-Cheong Poon rds6_conn_message_info(sock, len, iter, lens, 1); 630b7ff8b10SKa-Cheong Poon } 631e65d4d96SKa-Cheong Poon #endif 632b7ff8b10SKa-Cheong Poon 63300e0f34cSAndy Grover static void rds_conn_message_info_retrans(struct socket *sock, 63400e0f34cSAndy Grover unsigned int len, 63500e0f34cSAndy Grover struct rds_info_iterator *iter, 63600e0f34cSAndy Grover struct rds_info_lengths *lens) 63700e0f34cSAndy Grover { 63800e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 0); 63900e0f34cSAndy Grover } 64000e0f34cSAndy Grover 641e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 642b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info_retrans(struct socket *sock, 643b7ff8b10SKa-Cheong Poon unsigned int len, 644b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 645b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens) 646b7ff8b10SKa-Cheong Poon { 647b7ff8b10SKa-Cheong Poon rds6_conn_message_info(sock, len, iter, lens, 0); 648b7ff8b10SKa-Cheong Poon } 649e65d4d96SKa-Cheong Poon #endif 650b7ff8b10SKa-Cheong Poon 65100e0f34cSAndy Grover void rds_for_each_conn_info(struct socket *sock, unsigned int len, 65200e0f34cSAndy Grover struct rds_info_iterator *iter, 65300e0f34cSAndy Grover struct rds_info_lengths *lens, 65400e0f34cSAndy Grover int (*visitor)(struct rds_connection *, void *), 655f1cb9d68SSalvatore Mesoraca u64 *buffer, 65600e0f34cSAndy Grover size_t item_len) 65700e0f34cSAndy Grover { 65800e0f34cSAndy Grover struct hlist_head *head; 65900e0f34cSAndy Grover struct rds_connection *conn; 66000e0f34cSAndy Grover size_t i; 66100e0f34cSAndy Grover 662bcf50ef2SChris Mason rcu_read_lock(); 66300e0f34cSAndy Grover 66400e0f34cSAndy Grover lens->nr = 0; 66500e0f34cSAndy Grover lens->each = item_len; 66600e0f34cSAndy Grover 66700e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 66800e0f34cSAndy Grover i++, head++) { 669b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 67000e0f34cSAndy Grover 67100e0f34cSAndy Grover /* XXX no c_lock usage.. */ 67200e0f34cSAndy Grover if (!visitor(conn, buffer)) 67300e0f34cSAndy Grover continue; 67400e0f34cSAndy Grover 67500e0f34cSAndy Grover /* We copy as much as we can fit in the buffer, 67600e0f34cSAndy Grover * but we count all items so that the caller 67700e0f34cSAndy Grover * can resize the buffer. */ 67800e0f34cSAndy Grover if (len >= item_len) { 67900e0f34cSAndy Grover rds_info_copy(iter, buffer, item_len); 68000e0f34cSAndy Grover len -= item_len; 68100e0f34cSAndy Grover } 68200e0f34cSAndy Grover lens->nr++; 68300e0f34cSAndy Grover } 68400e0f34cSAndy Grover } 685bcf50ef2SChris Mason rcu_read_unlock(); 68600e0f34cSAndy Grover } 687616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_for_each_conn_info); 68800e0f34cSAndy Grover 689bb789763SSantosh Shilimkar static void rds_walk_conn_path_info(struct socket *sock, unsigned int len, 690992c9ec5SSowmini Varadhan struct rds_info_iterator *iter, 691992c9ec5SSowmini Varadhan struct rds_info_lengths *lens, 692992c9ec5SSowmini Varadhan int (*visitor)(struct rds_conn_path *, void *), 693b2c9272aSSalvatore Mesoraca u64 *buffer, 694992c9ec5SSowmini Varadhan size_t item_len) 695992c9ec5SSowmini Varadhan { 696992c9ec5SSowmini Varadhan struct hlist_head *head; 697992c9ec5SSowmini Varadhan struct rds_connection *conn; 698992c9ec5SSowmini Varadhan size_t i; 699992c9ec5SSowmini Varadhan 700992c9ec5SSowmini Varadhan rcu_read_lock(); 701992c9ec5SSowmini Varadhan 702992c9ec5SSowmini Varadhan lens->nr = 0; 703992c9ec5SSowmini Varadhan lens->each = item_len; 704992c9ec5SSowmini Varadhan 705992c9ec5SSowmini Varadhan for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 706992c9ec5SSowmini Varadhan i++, head++) { 707992c9ec5SSowmini Varadhan hlist_for_each_entry_rcu(conn, head, c_hash_node) { 708992c9ec5SSowmini Varadhan struct rds_conn_path *cp; 709992c9ec5SSowmini Varadhan 710eee2fa6aSKa-Cheong Poon /* XXX We only copy the information from the first 711eee2fa6aSKa-Cheong Poon * path for now. The problem is that if there are 712eee2fa6aSKa-Cheong Poon * more than one underlying paths, we cannot report 713eee2fa6aSKa-Cheong Poon * information of all of them using the existing 714eee2fa6aSKa-Cheong Poon * API. For example, there is only one next_tx_seq, 715eee2fa6aSKa-Cheong Poon * which path's next_tx_seq should we report? It is 716eee2fa6aSKa-Cheong Poon * a bug in the design of MPRDS. 717eee2fa6aSKa-Cheong Poon */ 718eee2fa6aSKa-Cheong Poon cp = conn->c_path; 719992c9ec5SSowmini Varadhan 720992c9ec5SSowmini Varadhan /* XXX no cp_lock usage.. */ 721992c9ec5SSowmini Varadhan if (!visitor(cp, buffer)) 722992c9ec5SSowmini Varadhan continue; 723992c9ec5SSowmini Varadhan 724992c9ec5SSowmini Varadhan /* We copy as much as we can fit in the buffer, 725992c9ec5SSowmini Varadhan * but we count all items so that the caller 726992c9ec5SSowmini Varadhan * can resize the buffer. 727992c9ec5SSowmini Varadhan */ 728992c9ec5SSowmini Varadhan if (len >= item_len) { 729992c9ec5SSowmini Varadhan rds_info_copy(iter, buffer, item_len); 730992c9ec5SSowmini Varadhan len -= item_len; 731992c9ec5SSowmini Varadhan } 732992c9ec5SSowmini Varadhan lens->nr++; 733992c9ec5SSowmini Varadhan } 734992c9ec5SSowmini Varadhan } 735992c9ec5SSowmini Varadhan rcu_read_unlock(); 736992c9ec5SSowmini Varadhan } 737992c9ec5SSowmini Varadhan 738992c9ec5SSowmini Varadhan static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer) 73900e0f34cSAndy Grover { 74000e0f34cSAndy Grover struct rds_info_connection *cinfo = buffer; 741eee2fa6aSKa-Cheong Poon struct rds_connection *conn = cp->cp_conn; 74200e0f34cSAndy Grover 7431e2b44e7SKa-Cheong Poon if (conn->c_isv6) 7441e2b44e7SKa-Cheong Poon return 0; 7451e2b44e7SKa-Cheong Poon 746992c9ec5SSowmini Varadhan cinfo->next_tx_seq = cp->cp_next_tx_seq; 747992c9ec5SSowmini Varadhan cinfo->next_rx_seq = cp->cp_next_rx_seq; 748eee2fa6aSKa-Cheong Poon cinfo->laddr = conn->c_laddr.s6_addr32[3]; 749eee2fa6aSKa-Cheong Poon cinfo->faddr = conn->c_faddr.s6_addr32[3]; 750fc640d4cSGerd Rausch cinfo->tos = conn->c_tos; 751eee2fa6aSKa-Cheong Poon strncpy(cinfo->transport, conn->c_trans->t_name, 75200e0f34cSAndy Grover sizeof(cinfo->transport)); 75300e0f34cSAndy Grover cinfo->flags = 0; 75400e0f34cSAndy Grover 755992c9ec5SSowmini Varadhan rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags), 7560f4b1c7eSZach Brown SENDING); 75700e0f34cSAndy Grover /* XXX Future: return the state rather than these funky bits */ 75800e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 759992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING, 76000e0f34cSAndy Grover CONNECTING); 76100e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 762992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_UP, 76300e0f34cSAndy Grover CONNECTED); 76400e0f34cSAndy Grover return 1; 76500e0f34cSAndy Grover } 76600e0f34cSAndy Grover 767e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 768b7ff8b10SKa-Cheong Poon static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer) 769b7ff8b10SKa-Cheong Poon { 770b7ff8b10SKa-Cheong Poon struct rds6_info_connection *cinfo6 = buffer; 771b7ff8b10SKa-Cheong Poon struct rds_connection *conn = cp->cp_conn; 772b7ff8b10SKa-Cheong Poon 773b7ff8b10SKa-Cheong Poon cinfo6->next_tx_seq = cp->cp_next_tx_seq; 774b7ff8b10SKa-Cheong Poon cinfo6->next_rx_seq = cp->cp_next_rx_seq; 775b7ff8b10SKa-Cheong Poon cinfo6->laddr = conn->c_laddr; 776b7ff8b10SKa-Cheong Poon cinfo6->faddr = conn->c_faddr; 777b7ff8b10SKa-Cheong Poon strncpy(cinfo6->transport, conn->c_trans->t_name, 778b7ff8b10SKa-Cheong Poon sizeof(cinfo6->transport)); 779b7ff8b10SKa-Cheong Poon cinfo6->flags = 0; 780b7ff8b10SKa-Cheong Poon 781b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags), 782b7ff8b10SKa-Cheong Poon SENDING); 783b7ff8b10SKa-Cheong Poon /* XXX Future: return the state rather than these funky bits */ 784b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, 785b7ff8b10SKa-Cheong Poon atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING, 786b7ff8b10SKa-Cheong Poon CONNECTING); 787b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, 788b7ff8b10SKa-Cheong Poon atomic_read(&cp->cp_state) == RDS_CONN_UP, 789b7ff8b10SKa-Cheong Poon CONNECTED); 790b7ff8b10SKa-Cheong Poon /* Just return 1 as there is no error case. This is a helper function 791b7ff8b10SKa-Cheong Poon * for rds_walk_conn_path_info() and it wants a return value. 792b7ff8b10SKa-Cheong Poon */ 793b7ff8b10SKa-Cheong Poon return 1; 794b7ff8b10SKa-Cheong Poon } 795e65d4d96SKa-Cheong Poon #endif 796b7ff8b10SKa-Cheong Poon 79700e0f34cSAndy Grover static void rds_conn_info(struct socket *sock, unsigned int len, 79800e0f34cSAndy Grover struct rds_info_iterator *iter, 79900e0f34cSAndy Grover struct rds_info_lengths *lens) 80000e0f34cSAndy Grover { 801b2c9272aSSalvatore Mesoraca u64 buffer[(sizeof(struct rds_info_connection) + 7) / 8]; 802b2c9272aSSalvatore Mesoraca 803992c9ec5SSowmini Varadhan rds_walk_conn_path_info(sock, len, iter, lens, 80400e0f34cSAndy Grover rds_conn_info_visitor, 805b2c9272aSSalvatore Mesoraca buffer, 80600e0f34cSAndy Grover sizeof(struct rds_info_connection)); 80700e0f34cSAndy Grover } 80800e0f34cSAndy Grover 809e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 810b7ff8b10SKa-Cheong Poon static void rds6_conn_info(struct socket *sock, unsigned int len, 811b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 812b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens) 813b7ff8b10SKa-Cheong Poon { 814b7ff8b10SKa-Cheong Poon u64 buffer[(sizeof(struct rds6_info_connection) + 7) / 8]; 815b7ff8b10SKa-Cheong Poon 816b7ff8b10SKa-Cheong Poon rds_walk_conn_path_info(sock, len, iter, lens, 817b7ff8b10SKa-Cheong Poon rds6_conn_info_visitor, 818b7ff8b10SKa-Cheong Poon buffer, 819b7ff8b10SKa-Cheong Poon sizeof(struct rds6_info_connection)); 820b7ff8b10SKa-Cheong Poon } 821e65d4d96SKa-Cheong Poon #endif 822b7ff8b10SKa-Cheong Poon 823ef87b7eaSZach Brown int rds_conn_init(void) 82400e0f34cSAndy Grover { 825c809195fSSowmini Varadhan int ret; 826c809195fSSowmini Varadhan 827c809195fSSowmini Varadhan ret = rds_loop_net_init(); /* register pernet callback */ 828c809195fSSowmini Varadhan if (ret) 829c809195fSSowmini Varadhan return ret; 830c809195fSSowmini Varadhan 83100e0f34cSAndy Grover rds_conn_slab = kmem_cache_create("rds_connection", 83200e0f34cSAndy Grover sizeof(struct rds_connection), 83300e0f34cSAndy Grover 0, 0, NULL); 834c809195fSSowmini Varadhan if (!rds_conn_slab) { 835c809195fSSowmini Varadhan rds_loop_net_exit(); 83600e0f34cSAndy Grover return -ENOMEM; 837c809195fSSowmini Varadhan } 83800e0f34cSAndy Grover 83900e0f34cSAndy Grover rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info); 84000e0f34cSAndy Grover rds_info_register_func(RDS_INFO_SEND_MESSAGES, 84100e0f34cSAndy Grover rds_conn_message_info_send); 84200e0f34cSAndy Grover rds_info_register_func(RDS_INFO_RETRANS_MESSAGES, 84300e0f34cSAndy Grover rds_conn_message_info_retrans); 844e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 845b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_CONNECTIONS, rds6_conn_info); 846b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_SEND_MESSAGES, 847b7ff8b10SKa-Cheong Poon rds6_conn_message_info_send); 848b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_RETRANS_MESSAGES, 849b7ff8b10SKa-Cheong Poon rds6_conn_message_info_retrans); 850e65d4d96SKa-Cheong Poon #endif 85100e0f34cSAndy Grover return 0; 85200e0f34cSAndy Grover } 85300e0f34cSAndy Grover 85400e0f34cSAndy Grover void rds_conn_exit(void) 85500e0f34cSAndy Grover { 856c809195fSSowmini Varadhan rds_loop_net_exit(); /* unregister pernet callback */ 85700e0f34cSAndy Grover rds_loop_exit(); 85800e0f34cSAndy Grover 85900e0f34cSAndy Grover WARN_ON(!hlist_empty(rds_conn_hash)); 86000e0f34cSAndy Grover 86100e0f34cSAndy Grover kmem_cache_destroy(rds_conn_slab); 86200e0f34cSAndy Grover 86300e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info); 86400e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_SEND_MESSAGES, 86500e0f34cSAndy Grover rds_conn_message_info_send); 86600e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES, 86700e0f34cSAndy Grover rds_conn_message_info_retrans); 868e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 869b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_CONNECTIONS, rds6_conn_info); 870b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_SEND_MESSAGES, 871b7ff8b10SKa-Cheong Poon rds6_conn_message_info_send); 872b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_RETRANS_MESSAGES, 873b7ff8b10SKa-Cheong Poon rds6_conn_message_info_retrans); 874e65d4d96SKa-Cheong Poon #endif 87500e0f34cSAndy Grover } 87600e0f34cSAndy Grover 87700e0f34cSAndy Grover /* 87800e0f34cSAndy Grover * Force a disconnect 87900e0f34cSAndy Grover */ 880aed20a53SSowmini Varadhan void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy) 8810cb43965SSowmini Varadhan { 8820cb43965SSowmini Varadhan atomic_set(&cp->cp_state, RDS_CONN_ERROR); 883aed20a53SSowmini Varadhan 8843db6e0d1SSowmini Varadhan rcu_read_lock(); 885ebeeb1adSSowmini Varadhan if (!destroy && rds_destroy_pending(cp->cp_conn)) { 8863db6e0d1SSowmini Varadhan rcu_read_unlock(); 887aed20a53SSowmini Varadhan return; 8883db6e0d1SSowmini Varadhan } 8890cb43965SSowmini Varadhan queue_work(rds_wq, &cp->cp_down_w); 8903db6e0d1SSowmini Varadhan rcu_read_unlock(); 8910cb43965SSowmini Varadhan } 8920cb43965SSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_drop); 8930cb43965SSowmini Varadhan 89400e0f34cSAndy Grover void rds_conn_drop(struct rds_connection *conn) 89500e0f34cSAndy Grover { 8965916e2c1SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable); 897aed20a53SSowmini Varadhan rds_conn_path_drop(&conn->c_path[0], false); 89800e0f34cSAndy Grover } 899616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_drop); 90000e0f34cSAndy Grover 90100e0f34cSAndy Grover /* 902f3c6808dSZach Brown * If the connection is down, trigger a connect. We may have scheduled a 903f3c6808dSZach Brown * delayed reconnect however - in this case we should not interfere. 904f3c6808dSZach Brown */ 9053c0a5900SSowmini Varadhan void rds_conn_path_connect_if_down(struct rds_conn_path *cp) 9063c0a5900SSowmini Varadhan { 9073db6e0d1SSowmini Varadhan rcu_read_lock(); 908ebeeb1adSSowmini Varadhan if (rds_destroy_pending(cp->cp_conn)) { 9093db6e0d1SSowmini Varadhan rcu_read_unlock(); 9103db6e0d1SSowmini Varadhan return; 9113db6e0d1SSowmini Varadhan } 9123c0a5900SSowmini Varadhan if (rds_conn_path_state(cp) == RDS_CONN_DOWN && 9133c0a5900SSowmini Varadhan !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags)) 9143c0a5900SSowmini Varadhan queue_delayed_work(rds_wq, &cp->cp_conn_w, 0); 9153db6e0d1SSowmini Varadhan rcu_read_unlock(); 9163c0a5900SSowmini Varadhan } 9171a0e100fSSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down); 9183c0a5900SSowmini Varadhan 9199ef845f8SRao Shoaib /* Check connectivity of all paths 9209ef845f8SRao Shoaib */ 9219ef845f8SRao Shoaib void rds_check_all_paths(struct rds_connection *conn) 9229ef845f8SRao Shoaib { 9239ef845f8SRao Shoaib int i = 0; 9249ef845f8SRao Shoaib 9259ef845f8SRao Shoaib do { 9269ef845f8SRao Shoaib rds_conn_path_connect_if_down(&conn->c_path[i]); 9279ef845f8SRao Shoaib } while (++i < conn->c_npaths); 9289ef845f8SRao Shoaib } 9299ef845f8SRao Shoaib 930f3c6808dSZach Brown void rds_conn_connect_if_down(struct rds_connection *conn) 931f3c6808dSZach Brown { 9323c0a5900SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable); 9333c0a5900SSowmini Varadhan rds_conn_path_connect_if_down(&conn->c_path[0]); 934f3c6808dSZach Brown } 935f3c6808dSZach Brown EXPORT_SYMBOL_GPL(rds_conn_connect_if_down); 936f3c6808dSZach Brown 937fb1b3dc4SSowmini Varadhan void 938fb1b3dc4SSowmini Varadhan __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...) 939fb1b3dc4SSowmini Varadhan { 940fb1b3dc4SSowmini Varadhan va_list ap; 941fb1b3dc4SSowmini Varadhan 942fb1b3dc4SSowmini Varadhan va_start(ap, fmt); 943fb1b3dc4SSowmini Varadhan vprintk(fmt, ap); 944fb1b3dc4SSowmini Varadhan va_end(ap); 945fb1b3dc4SSowmini Varadhan 946aed20a53SSowmini Varadhan rds_conn_path_drop(cp, false); 947fb1b3dc4SSowmini Varadhan } 948