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; 24300e0f34cSAndy Grover if (is_outgoing && trans->t_prefer_loopback) { 24400e0f34cSAndy Grover /* "outgoing" connection - and the transport 24500e0f34cSAndy Grover * says it wants the connection handled by the 24600e0f34cSAndy Grover * loopback transport. This is what TCP does. 24700e0f34cSAndy Grover */ 24800e0f34cSAndy Grover trans = &rds_loop_transport; 24900e0f34cSAndy Grover } 25000e0f34cSAndy Grover } 25100e0f34cSAndy Grover 25200e0f34cSAndy Grover conn->c_trans = trans; 25300e0f34cSAndy Grover 2545916e2c1SSowmini Varadhan init_waitqueue_head(&conn->c_hs_waitq); 255840df162SSowmini Varadhan for (i = 0; i < npaths; i++) { 2565916e2c1SSowmini Varadhan __rds_conn_path_init(conn, &conn->c_path[i], 2575916e2c1SSowmini Varadhan is_outgoing); 2585916e2c1SSowmini Varadhan conn->c_path[i].cp_index = i; 2595916e2c1SSowmini Varadhan } 260ebeeb1adSSowmini Varadhan rcu_read_lock(); 261ebeeb1adSSowmini Varadhan if (rds_destroy_pending(conn)) 262ebeeb1adSSowmini Varadhan ret = -ENETDOWN; 263ebeeb1adSSowmini Varadhan else 264d4014d8cSSowmini Varadhan ret = trans->conn_alloc(conn, GFP_ATOMIC); 26500e0f34cSAndy Grover if (ret) { 266ebeeb1adSSowmini Varadhan rcu_read_unlock(); 267840df162SSowmini Varadhan kfree(conn->c_path); 26800e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 26900e0f34cSAndy Grover conn = ERR_PTR(ret); 27000e0f34cSAndy Grover goto out; 27100e0f34cSAndy Grover } 27200e0f34cSAndy Grover 273eee2fa6aSKa-Cheong Poon rdsdebug("allocated conn %p for %pI6c -> %pI6c over %s %s\n", 274eee2fa6aSKa-Cheong Poon conn, laddr, faddr, 275eee2fa6aSKa-Cheong Poon strnlen(trans->t_name, sizeof(trans->t_name)) ? 276eee2fa6aSKa-Cheong Poon trans->t_name : "[unknown]", is_outgoing ? "(outgoing)" : ""); 27700e0f34cSAndy Grover 278cb24405eSAndy Grover /* 279cb24405eSAndy Grover * Since we ran without holding the conn lock, someone could 280cb24405eSAndy Grover * have created the same conn (either normal or passive) in the 281cb24405eSAndy Grover * interim. We check while holding the lock. If we won, we complete 282cb24405eSAndy Grover * init and return our conn. If we lost, we rollback and return the 283cb24405eSAndy Grover * other one. 284cb24405eSAndy Grover */ 28500e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 286cb24405eSAndy Grover if (parent) { 287cb24405eSAndy Grover /* Creating passive conn */ 288cb24405eSAndy Grover if (parent->c_passive) { 2891c5113cfSSowmini Varadhan trans->conn_free(conn->c_path[0].cp_transport_data); 290840df162SSowmini Varadhan kfree(conn->c_path); 29100e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 292cb24405eSAndy Grover conn = parent->c_passive; 29300e0f34cSAndy Grover } else { 294cb24405eSAndy Grover parent->c_passive = conn; 29500e0f34cSAndy Grover rds_cong_add_conn(conn); 29600e0f34cSAndy Grover rds_conn_count++; 29700e0f34cSAndy Grover } 298cb24405eSAndy Grover } else { 299cb24405eSAndy Grover /* Creating normal conn */ 300cb24405eSAndy Grover struct rds_connection *found; 30100e0f34cSAndy Grover 302eee2fa6aSKa-Cheong Poon found = rds_conn_lookup(net, head, laddr, faddr, trans, 3033eb45036SSantosh Shilimkar tos, dev_if); 304cb24405eSAndy Grover if (found) { 3051c5113cfSSowmini Varadhan struct rds_conn_path *cp; 3061c5113cfSSowmini Varadhan int i; 3071c5113cfSSowmini Varadhan 308840df162SSowmini Varadhan for (i = 0; i < npaths; i++) { 3091c5113cfSSowmini Varadhan cp = &conn->c_path[i]; 31002105b2cSSowmini Varadhan /* The ->conn_alloc invocation may have 31102105b2cSSowmini Varadhan * allocated resource for all paths, so all 31202105b2cSSowmini Varadhan * of them may have to be freed here. 31302105b2cSSowmini Varadhan */ 31402105b2cSSowmini Varadhan if (cp->cp_transport_data) 3151c5113cfSSowmini Varadhan trans->conn_free(cp->cp_transport_data); 3161c5113cfSSowmini Varadhan } 317840df162SSowmini Varadhan kfree(conn->c_path); 318cb24405eSAndy Grover kmem_cache_free(rds_conn_slab, conn); 319cb24405eSAndy Grover conn = found; 320cb24405eSAndy Grover } else { 321905dd418SSowmini Varadhan conn->c_my_gen_num = rds_gen_num; 322905dd418SSowmini Varadhan conn->c_peer_gen_num = 0; 323bcf50ef2SChris Mason hlist_add_head_rcu(&conn->c_hash_node, head); 324cb24405eSAndy Grover rds_cong_add_conn(conn); 325cb24405eSAndy Grover rds_conn_count++; 326cb24405eSAndy Grover } 327cb24405eSAndy Grover } 32800e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 329ebeeb1adSSowmini Varadhan rcu_read_unlock(); 33000e0f34cSAndy Grover 33100e0f34cSAndy Grover out: 33200e0f34cSAndy Grover return conn; 33300e0f34cSAndy Grover } 33400e0f34cSAndy Grover 335d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create(struct net *net, 336eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr, 337eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr, 3383eb45036SSantosh Shilimkar struct rds_transport *trans, u8 tos, 3393eb45036SSantosh Shilimkar gfp_t gfp, int dev_if) 34000e0f34cSAndy Grover { 3413eb45036SSantosh Shilimkar return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 0, dev_if); 34200e0f34cSAndy Grover } 343616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create); 34400e0f34cSAndy Grover 345d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create_outgoing(struct net *net, 346eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr, 347eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr, 348eee2fa6aSKa-Cheong Poon struct rds_transport *trans, 3493eb45036SSantosh Shilimkar u8 tos, gfp_t gfp, int dev_if) 35000e0f34cSAndy Grover { 3513eb45036SSantosh Shilimkar return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 1, dev_if); 35200e0f34cSAndy Grover } 353616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create_outgoing); 35400e0f34cSAndy Grover 355d769ef81SSowmini Varadhan void rds_conn_shutdown(struct rds_conn_path *cp) 3562dc39357SAndy Grover { 357d769ef81SSowmini Varadhan struct rds_connection *conn = cp->cp_conn; 358d769ef81SSowmini Varadhan 3592dc39357SAndy Grover /* shut it down unless it's down already */ 360d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) { 3612dc39357SAndy Grover /* 3622dc39357SAndy Grover * Quiesce the connection mgmt handlers before we start tearing 3632dc39357SAndy Grover * things down. We don't hold the mutex for the entire 3642dc39357SAndy Grover * duration of the shutdown operation, else we may be 3652dc39357SAndy Grover * deadlocking with the CM handler. Instead, the CM event 3662dc39357SAndy Grover * handler is supposed to check for state DISCONNECTING 3672dc39357SAndy Grover */ 368d769ef81SSowmini Varadhan mutex_lock(&cp->cp_cm_lock); 369d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_UP, 370d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING) && 371d769ef81SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR, 372d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING)) { 373d769ef81SSowmini Varadhan rds_conn_path_error(cp, 374d769ef81SSowmini Varadhan "shutdown called in state %d\n", 375d769ef81SSowmini Varadhan atomic_read(&cp->cp_state)); 376d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock); 3772dc39357SAndy Grover return; 3782dc39357SAndy Grover } 379d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock); 3802dc39357SAndy Grover 381d769ef81SSowmini Varadhan wait_event(cp->cp_waitq, 382d769ef81SSowmini Varadhan !test_bit(RDS_IN_XMIT, &cp->cp_flags)); 383d769ef81SSowmini Varadhan wait_event(cp->cp_waitq, 384d769ef81SSowmini Varadhan !test_bit(RDS_RECV_REFILL, &cp->cp_flags)); 3857e3f2952SChris Mason 386d769ef81SSowmini Varadhan conn->c_trans->conn_path_shutdown(cp); 387d769ef81SSowmini Varadhan rds_conn_path_reset(cp); 3882dc39357SAndy Grover 389d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING, 390e97656d0SSowmini Varadhan RDS_CONN_DOWN) && 391e97656d0SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR, 392d769ef81SSowmini Varadhan RDS_CONN_DOWN)) { 3932dc39357SAndy Grover /* This can happen - eg when we're in the middle of tearing 3942dc39357SAndy Grover * down the connection, and someone unloads the rds module. 395e97656d0SSowmini Varadhan * Quite reproducible with loopback connections. 3962dc39357SAndy Grover * Mostly harmless. 397e97656d0SSowmini Varadhan * 398e97656d0SSowmini Varadhan * Note that this also happens with rds-tcp because 399e97656d0SSowmini Varadhan * we could have triggered rds_conn_path_drop in irq 400e97656d0SSowmini Varadhan * mode from rds_tcp_state change on the receipt of 401e97656d0SSowmini Varadhan * a FIN, thus we need to recheck for RDS_CONN_ERROR 402e97656d0SSowmini Varadhan * here. 4032dc39357SAndy Grover */ 404d769ef81SSowmini Varadhan rds_conn_path_error(cp, "%s: failed to transition " 405d769ef81SSowmini Varadhan "to state DOWN, current state " 406d769ef81SSowmini Varadhan "is %d\n", __func__, 407d769ef81SSowmini Varadhan atomic_read(&cp->cp_state)); 4082dc39357SAndy Grover return; 4092dc39357SAndy Grover } 4102dc39357SAndy Grover } 4112dc39357SAndy Grover 4122dc39357SAndy Grover /* Then reconnect if it's still live. 4132dc39357SAndy Grover * The passive side of an IB loopback connection is never added 4142dc39357SAndy Grover * to the conn hash, so we never trigger a reconnect on this 4152dc39357SAndy Grover * conn - the reconnect is always triggered by the active peer. */ 416d769ef81SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_conn_w); 417bcf50ef2SChris Mason rcu_read_lock(); 418bcf50ef2SChris Mason if (!hlist_unhashed(&conn->c_hash_node)) { 419bcf50ef2SChris Mason rcu_read_unlock(); 420d769ef81SSowmini Varadhan rds_queue_reconnect(cp); 421bcf50ef2SChris Mason } else { 422bcf50ef2SChris Mason rcu_read_unlock(); 423bcf50ef2SChris Mason } 4242dc39357SAndy Grover } 4252dc39357SAndy Grover 4263ecc5693SSowmini Varadhan /* destroy a single rds_conn_path. rds_conn_destroy() iterates over 4273ecc5693SSowmini Varadhan * all paths using rds_conn_path_destroy() 4283ecc5693SSowmini Varadhan */ 4293ecc5693SSowmini Varadhan static void rds_conn_path_destroy(struct rds_conn_path *cp) 4303ecc5693SSowmini Varadhan { 4313ecc5693SSowmini Varadhan struct rds_message *rm, *rtmp; 4323ecc5693SSowmini Varadhan 43302105b2cSSowmini Varadhan if (!cp->cp_transport_data) 43402105b2cSSowmini Varadhan return; 43502105b2cSSowmini Varadhan 4363ecc5693SSowmini Varadhan /* make sure lingering queued work won't try to ref the conn */ 4373ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_send_w); 4383ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_recv_w); 4393ecc5693SSowmini Varadhan 440aed20a53SSowmini Varadhan rds_conn_path_drop(cp, true); 441aed20a53SSowmini Varadhan flush_work(&cp->cp_down_w); 442aed20a53SSowmini Varadhan 4433ecc5693SSowmini Varadhan /* tear down queued messages */ 4443ecc5693SSowmini Varadhan list_for_each_entry_safe(rm, rtmp, 4453ecc5693SSowmini Varadhan &cp->cp_send_queue, 4463ecc5693SSowmini Varadhan m_conn_item) { 4473ecc5693SSowmini Varadhan list_del_init(&rm->m_conn_item); 4483ecc5693SSowmini Varadhan BUG_ON(!list_empty(&rm->m_sock_item)); 4493ecc5693SSowmini Varadhan rds_message_put(rm); 4503ecc5693SSowmini Varadhan } 4513ecc5693SSowmini Varadhan if (cp->cp_xmit_rm) 4523ecc5693SSowmini Varadhan rds_message_put(cp->cp_xmit_rm); 4533ecc5693SSowmini Varadhan 4543db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_send_w)); 4553db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_recv_w)); 4563db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_conn_w)); 4573db6e0d1SSowmini Varadhan WARN_ON(work_pending(&cp->cp_down_w)); 4583db6e0d1SSowmini Varadhan 4593ecc5693SSowmini Varadhan cp->cp_conn->c_trans->conn_free(cp->cp_transport_data); 4603ecc5693SSowmini Varadhan } 4613ecc5693SSowmini Varadhan 4622dc39357SAndy Grover /* 4632dc39357SAndy Grover * Stop and free a connection. 464ffcec0e1SZach Brown * 465ffcec0e1SZach Brown * This can only be used in very limited circumstances. It assumes that once 466ffcec0e1SZach Brown * the conn has been shutdown that no one else is referencing the connection. 467ffcec0e1SZach Brown * We can only ensure this in the rmmod path in the current code. 4682dc39357SAndy Grover */ 46900e0f34cSAndy Grover void rds_conn_destroy(struct rds_connection *conn) 47000e0f34cSAndy Grover { 471fe8ff6b5SZach Brown unsigned long flags; 47202105b2cSSowmini Varadhan int i; 47302105b2cSSowmini Varadhan struct rds_conn_path *cp; 474840df162SSowmini Varadhan int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1); 47500e0f34cSAndy Grover 47600e0f34cSAndy Grover rdsdebug("freeing conn %p for %pI4 -> " 47700e0f34cSAndy Grover "%pI4\n", conn, &conn->c_laddr, 47800e0f34cSAndy Grover &conn->c_faddr); 47900e0f34cSAndy Grover 480abf45439SChris Mason /* Ensure conn will not be scheduled for reconnect */ 481abf45439SChris Mason spin_lock_irq(&rds_conn_lock); 482bcf50ef2SChris Mason hlist_del_init_rcu(&conn->c_hash_node); 483abf45439SChris Mason spin_unlock_irq(&rds_conn_lock); 484bcf50ef2SChris Mason synchronize_rcu(); 485bcf50ef2SChris Mason 486ffcec0e1SZach Brown /* shut the connection down */ 487840df162SSowmini Varadhan for (i = 0; i < npaths; i++) { 4883ecc5693SSowmini Varadhan cp = &conn->c_path[i]; 4893ecc5693SSowmini Varadhan rds_conn_path_destroy(cp); 4903ecc5693SSowmini Varadhan BUG_ON(!list_empty(&cp->cp_retrans)); 49100e0f34cSAndy Grover } 49200e0f34cSAndy Grover 49300e0f34cSAndy Grover /* 49400e0f34cSAndy Grover * The congestion maps aren't freed up here. They're 49500e0f34cSAndy Grover * freed by rds_cong_exit() after all the connections 49600e0f34cSAndy Grover * have been freed. 49700e0f34cSAndy Grover */ 49800e0f34cSAndy Grover rds_cong_remove_conn(conn); 49900e0f34cSAndy Grover 500840df162SSowmini Varadhan kfree(conn->c_path); 50100e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 50200e0f34cSAndy Grover 503fe8ff6b5SZach Brown spin_lock_irqsave(&rds_conn_lock, flags); 50400e0f34cSAndy Grover rds_conn_count--; 505fe8ff6b5SZach Brown spin_unlock_irqrestore(&rds_conn_lock, flags); 50600e0f34cSAndy Grover } 507616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_destroy); 50800e0f34cSAndy Grover 5091e2b44e7SKa-Cheong Poon static void __rds_inc_msg_cp(struct rds_incoming *inc, 5101e2b44e7SKa-Cheong Poon struct rds_info_iterator *iter, 511b7ff8b10SKa-Cheong Poon void *saddr, void *daddr, int flip, bool isv6) 5121e2b44e7SKa-Cheong Poon { 513e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 514b7ff8b10SKa-Cheong Poon if (isv6) 515b7ff8b10SKa-Cheong Poon rds6_inc_info_copy(inc, iter, saddr, daddr, flip); 516b7ff8b10SKa-Cheong Poon else 517e65d4d96SKa-Cheong Poon #endif 5181e2b44e7SKa-Cheong Poon rds_inc_info_copy(inc, iter, *(__be32 *)saddr, 5191e2b44e7SKa-Cheong Poon *(__be32 *)daddr, flip); 5201e2b44e7SKa-Cheong Poon } 5211e2b44e7SKa-Cheong Poon 5221e2b44e7SKa-Cheong Poon static void rds_conn_message_info_cmn(struct socket *sock, unsigned int len, 52300e0f34cSAndy Grover struct rds_info_iterator *iter, 52400e0f34cSAndy Grover struct rds_info_lengths *lens, 525b7ff8b10SKa-Cheong Poon int want_send, bool isv6) 52600e0f34cSAndy Grover { 52700e0f34cSAndy Grover struct hlist_head *head; 52800e0f34cSAndy Grover struct list_head *list; 52900e0f34cSAndy Grover struct rds_connection *conn; 53000e0f34cSAndy Grover struct rds_message *rm; 53100e0f34cSAndy Grover unsigned int total = 0; 532501dcccdSZach Brown unsigned long flags; 53300e0f34cSAndy Grover size_t i; 534992c9ec5SSowmini Varadhan int j; 53500e0f34cSAndy Grover 536b7ff8b10SKa-Cheong Poon if (isv6) 537b7ff8b10SKa-Cheong Poon len /= sizeof(struct rds6_info_message); 538b7ff8b10SKa-Cheong Poon else 53900e0f34cSAndy Grover len /= sizeof(struct rds_info_message); 54000e0f34cSAndy Grover 541bcf50ef2SChris Mason rcu_read_lock(); 54200e0f34cSAndy Grover 54300e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 54400e0f34cSAndy Grover i++, head++) { 545b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 546992c9ec5SSowmini Varadhan struct rds_conn_path *cp; 547840df162SSowmini Varadhan int npaths; 54800e0f34cSAndy Grover 549b7ff8b10SKa-Cheong Poon if (!isv6 && conn->c_isv6) 550b7ff8b10SKa-Cheong Poon continue; 551b7ff8b10SKa-Cheong Poon 552840df162SSowmini Varadhan npaths = (conn->c_trans->t_mp_capable ? 553840df162SSowmini Varadhan RDS_MPATH_WORKERS : 1); 554840df162SSowmini Varadhan 555840df162SSowmini Varadhan for (j = 0; j < npaths; j++) { 556992c9ec5SSowmini Varadhan cp = &conn->c_path[j]; 557992c9ec5SSowmini Varadhan if (want_send) 558992c9ec5SSowmini Varadhan list = &cp->cp_send_queue; 559992c9ec5SSowmini Varadhan else 560992c9ec5SSowmini Varadhan list = &cp->cp_retrans; 561992c9ec5SSowmini Varadhan 562992c9ec5SSowmini Varadhan spin_lock_irqsave(&cp->cp_lock, flags); 56300e0f34cSAndy Grover 56400e0f34cSAndy Grover /* XXX too lazy to maintain counts.. */ 56500e0f34cSAndy Grover list_for_each_entry(rm, list, m_conn_item) { 56600e0f34cSAndy Grover total++; 56700e0f34cSAndy Grover if (total <= len) 5681e2b44e7SKa-Cheong Poon __rds_inc_msg_cp(&rm->m_inc, 569992c9ec5SSowmini Varadhan iter, 5701e2b44e7SKa-Cheong Poon &conn->c_laddr, 5711e2b44e7SKa-Cheong Poon &conn->c_faddr, 572b7ff8b10SKa-Cheong Poon 0, isv6); 57300e0f34cSAndy Grover } 57400e0f34cSAndy Grover 575992c9ec5SSowmini Varadhan spin_unlock_irqrestore(&cp->cp_lock, flags); 576992c9ec5SSowmini Varadhan } 57700e0f34cSAndy Grover } 57800e0f34cSAndy Grover } 579bcf50ef2SChris Mason rcu_read_unlock(); 58000e0f34cSAndy Grover 58100e0f34cSAndy Grover lens->nr = total; 582b7ff8b10SKa-Cheong Poon if (isv6) 583b7ff8b10SKa-Cheong Poon lens->each = sizeof(struct rds6_info_message); 584b7ff8b10SKa-Cheong Poon else 58500e0f34cSAndy Grover lens->each = sizeof(struct rds_info_message); 58600e0f34cSAndy Grover } 58700e0f34cSAndy Grover 5881e2b44e7SKa-Cheong Poon static void rds_conn_message_info(struct socket *sock, unsigned int len, 5891e2b44e7SKa-Cheong Poon struct rds_info_iterator *iter, 5901e2b44e7SKa-Cheong Poon struct rds_info_lengths *lens, 5911e2b44e7SKa-Cheong Poon int want_send) 5921e2b44e7SKa-Cheong Poon { 593b7ff8b10SKa-Cheong Poon rds_conn_message_info_cmn(sock, len, iter, lens, want_send, false); 594b7ff8b10SKa-Cheong Poon } 595b7ff8b10SKa-Cheong Poon 596e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 597b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info(struct socket *sock, unsigned int len, 598b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 599b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens, 600b7ff8b10SKa-Cheong Poon int want_send) 601b7ff8b10SKa-Cheong Poon { 602b7ff8b10SKa-Cheong Poon rds_conn_message_info_cmn(sock, len, iter, lens, want_send, true); 6031e2b44e7SKa-Cheong Poon } 604e65d4d96SKa-Cheong Poon #endif 6051e2b44e7SKa-Cheong Poon 60600e0f34cSAndy Grover static void rds_conn_message_info_send(struct socket *sock, unsigned int len, 60700e0f34cSAndy Grover struct rds_info_iterator *iter, 60800e0f34cSAndy Grover struct rds_info_lengths *lens) 60900e0f34cSAndy Grover { 61000e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 1); 61100e0f34cSAndy Grover } 61200e0f34cSAndy Grover 613e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 614b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info_send(struct socket *sock, unsigned int len, 615b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 616b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens) 617b7ff8b10SKa-Cheong Poon { 618b7ff8b10SKa-Cheong Poon rds6_conn_message_info(sock, len, iter, lens, 1); 619b7ff8b10SKa-Cheong Poon } 620e65d4d96SKa-Cheong Poon #endif 621b7ff8b10SKa-Cheong Poon 62200e0f34cSAndy Grover static void rds_conn_message_info_retrans(struct socket *sock, 62300e0f34cSAndy Grover unsigned int len, 62400e0f34cSAndy Grover struct rds_info_iterator *iter, 62500e0f34cSAndy Grover struct rds_info_lengths *lens) 62600e0f34cSAndy Grover { 62700e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 0); 62800e0f34cSAndy Grover } 62900e0f34cSAndy Grover 630e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 631b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info_retrans(struct socket *sock, 632b7ff8b10SKa-Cheong Poon unsigned int len, 633b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 634b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens) 635b7ff8b10SKa-Cheong Poon { 636b7ff8b10SKa-Cheong Poon rds6_conn_message_info(sock, len, iter, lens, 0); 637b7ff8b10SKa-Cheong Poon } 638e65d4d96SKa-Cheong Poon #endif 639b7ff8b10SKa-Cheong Poon 64000e0f34cSAndy Grover void rds_for_each_conn_info(struct socket *sock, unsigned int len, 64100e0f34cSAndy Grover struct rds_info_iterator *iter, 64200e0f34cSAndy Grover struct rds_info_lengths *lens, 64300e0f34cSAndy Grover int (*visitor)(struct rds_connection *, void *), 644f1cb9d68SSalvatore Mesoraca u64 *buffer, 64500e0f34cSAndy Grover size_t item_len) 64600e0f34cSAndy Grover { 64700e0f34cSAndy Grover struct hlist_head *head; 64800e0f34cSAndy Grover struct rds_connection *conn; 64900e0f34cSAndy Grover size_t i; 65000e0f34cSAndy Grover 651bcf50ef2SChris Mason rcu_read_lock(); 65200e0f34cSAndy Grover 65300e0f34cSAndy Grover lens->nr = 0; 65400e0f34cSAndy Grover lens->each = item_len; 65500e0f34cSAndy Grover 65600e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 65700e0f34cSAndy Grover i++, head++) { 658b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) { 65900e0f34cSAndy Grover 66000e0f34cSAndy Grover /* XXX no c_lock usage.. */ 66100e0f34cSAndy Grover if (!visitor(conn, buffer)) 66200e0f34cSAndy Grover continue; 66300e0f34cSAndy Grover 66400e0f34cSAndy Grover /* We copy as much as we can fit in the buffer, 66500e0f34cSAndy Grover * but we count all items so that the caller 66600e0f34cSAndy Grover * can resize the buffer. */ 66700e0f34cSAndy Grover if (len >= item_len) { 66800e0f34cSAndy Grover rds_info_copy(iter, buffer, item_len); 66900e0f34cSAndy Grover len -= item_len; 67000e0f34cSAndy Grover } 67100e0f34cSAndy Grover lens->nr++; 67200e0f34cSAndy Grover } 67300e0f34cSAndy Grover } 674bcf50ef2SChris Mason rcu_read_unlock(); 67500e0f34cSAndy Grover } 676616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_for_each_conn_info); 67700e0f34cSAndy Grover 678bb789763SSantosh Shilimkar static void rds_walk_conn_path_info(struct socket *sock, unsigned int len, 679992c9ec5SSowmini Varadhan struct rds_info_iterator *iter, 680992c9ec5SSowmini Varadhan struct rds_info_lengths *lens, 681992c9ec5SSowmini Varadhan int (*visitor)(struct rds_conn_path *, void *), 682b2c9272aSSalvatore Mesoraca u64 *buffer, 683992c9ec5SSowmini Varadhan size_t item_len) 684992c9ec5SSowmini Varadhan { 685992c9ec5SSowmini Varadhan struct hlist_head *head; 686992c9ec5SSowmini Varadhan struct rds_connection *conn; 687992c9ec5SSowmini Varadhan size_t i; 688992c9ec5SSowmini Varadhan 689992c9ec5SSowmini Varadhan rcu_read_lock(); 690992c9ec5SSowmini Varadhan 691992c9ec5SSowmini Varadhan lens->nr = 0; 692992c9ec5SSowmini Varadhan lens->each = item_len; 693992c9ec5SSowmini Varadhan 694992c9ec5SSowmini Varadhan for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 695992c9ec5SSowmini Varadhan i++, head++) { 696992c9ec5SSowmini Varadhan hlist_for_each_entry_rcu(conn, head, c_hash_node) { 697992c9ec5SSowmini Varadhan struct rds_conn_path *cp; 698992c9ec5SSowmini Varadhan 699eee2fa6aSKa-Cheong Poon /* XXX We only copy the information from the first 700eee2fa6aSKa-Cheong Poon * path for now. The problem is that if there are 701eee2fa6aSKa-Cheong Poon * more than one underlying paths, we cannot report 702eee2fa6aSKa-Cheong Poon * information of all of them using the existing 703eee2fa6aSKa-Cheong Poon * API. For example, there is only one next_tx_seq, 704eee2fa6aSKa-Cheong Poon * which path's next_tx_seq should we report? It is 705eee2fa6aSKa-Cheong Poon * a bug in the design of MPRDS. 706eee2fa6aSKa-Cheong Poon */ 707eee2fa6aSKa-Cheong Poon cp = conn->c_path; 708992c9ec5SSowmini Varadhan 709992c9ec5SSowmini Varadhan /* XXX no cp_lock usage.. */ 710992c9ec5SSowmini Varadhan if (!visitor(cp, buffer)) 711992c9ec5SSowmini Varadhan continue; 712992c9ec5SSowmini Varadhan 713992c9ec5SSowmini Varadhan /* We copy as much as we can fit in the buffer, 714992c9ec5SSowmini Varadhan * but we count all items so that the caller 715992c9ec5SSowmini Varadhan * can resize the buffer. 716992c9ec5SSowmini Varadhan */ 717992c9ec5SSowmini Varadhan if (len >= item_len) { 718992c9ec5SSowmini Varadhan rds_info_copy(iter, buffer, item_len); 719992c9ec5SSowmini Varadhan len -= item_len; 720992c9ec5SSowmini Varadhan } 721992c9ec5SSowmini Varadhan lens->nr++; 722992c9ec5SSowmini Varadhan } 723992c9ec5SSowmini Varadhan } 724992c9ec5SSowmini Varadhan rcu_read_unlock(); 725992c9ec5SSowmini Varadhan } 726992c9ec5SSowmini Varadhan 727992c9ec5SSowmini Varadhan static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer) 72800e0f34cSAndy Grover { 72900e0f34cSAndy Grover struct rds_info_connection *cinfo = buffer; 730eee2fa6aSKa-Cheong Poon struct rds_connection *conn = cp->cp_conn; 73100e0f34cSAndy Grover 7321e2b44e7SKa-Cheong Poon if (conn->c_isv6) 7331e2b44e7SKa-Cheong Poon return 0; 7341e2b44e7SKa-Cheong Poon 735992c9ec5SSowmini Varadhan cinfo->next_tx_seq = cp->cp_next_tx_seq; 736992c9ec5SSowmini Varadhan cinfo->next_rx_seq = cp->cp_next_rx_seq; 737eee2fa6aSKa-Cheong Poon cinfo->laddr = conn->c_laddr.s6_addr32[3]; 738eee2fa6aSKa-Cheong Poon cinfo->faddr = conn->c_faddr.s6_addr32[3]; 739*fc640d4cSGerd Rausch cinfo->tos = conn->c_tos; 740eee2fa6aSKa-Cheong Poon strncpy(cinfo->transport, conn->c_trans->t_name, 74100e0f34cSAndy Grover sizeof(cinfo->transport)); 74200e0f34cSAndy Grover cinfo->flags = 0; 74300e0f34cSAndy Grover 744992c9ec5SSowmini Varadhan rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags), 7450f4b1c7eSZach Brown SENDING); 74600e0f34cSAndy Grover /* XXX Future: return the state rather than these funky bits */ 74700e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 748992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING, 74900e0f34cSAndy Grover CONNECTING); 75000e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 751992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_UP, 75200e0f34cSAndy Grover CONNECTED); 75300e0f34cSAndy Grover return 1; 75400e0f34cSAndy Grover } 75500e0f34cSAndy Grover 756e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 757b7ff8b10SKa-Cheong Poon static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer) 758b7ff8b10SKa-Cheong Poon { 759b7ff8b10SKa-Cheong Poon struct rds6_info_connection *cinfo6 = buffer; 760b7ff8b10SKa-Cheong Poon struct rds_connection *conn = cp->cp_conn; 761b7ff8b10SKa-Cheong Poon 762b7ff8b10SKa-Cheong Poon cinfo6->next_tx_seq = cp->cp_next_tx_seq; 763b7ff8b10SKa-Cheong Poon cinfo6->next_rx_seq = cp->cp_next_rx_seq; 764b7ff8b10SKa-Cheong Poon cinfo6->laddr = conn->c_laddr; 765b7ff8b10SKa-Cheong Poon cinfo6->faddr = conn->c_faddr; 766b7ff8b10SKa-Cheong Poon strncpy(cinfo6->transport, conn->c_trans->t_name, 767b7ff8b10SKa-Cheong Poon sizeof(cinfo6->transport)); 768b7ff8b10SKa-Cheong Poon cinfo6->flags = 0; 769b7ff8b10SKa-Cheong Poon 770b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags), 771b7ff8b10SKa-Cheong Poon SENDING); 772b7ff8b10SKa-Cheong Poon /* XXX Future: return the state rather than these funky bits */ 773b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, 774b7ff8b10SKa-Cheong Poon atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING, 775b7ff8b10SKa-Cheong Poon CONNECTING); 776b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, 777b7ff8b10SKa-Cheong Poon atomic_read(&cp->cp_state) == RDS_CONN_UP, 778b7ff8b10SKa-Cheong Poon CONNECTED); 779b7ff8b10SKa-Cheong Poon /* Just return 1 as there is no error case. This is a helper function 780b7ff8b10SKa-Cheong Poon * for rds_walk_conn_path_info() and it wants a return value. 781b7ff8b10SKa-Cheong Poon */ 782b7ff8b10SKa-Cheong Poon return 1; 783b7ff8b10SKa-Cheong Poon } 784e65d4d96SKa-Cheong Poon #endif 785b7ff8b10SKa-Cheong Poon 78600e0f34cSAndy Grover static void rds_conn_info(struct socket *sock, unsigned int len, 78700e0f34cSAndy Grover struct rds_info_iterator *iter, 78800e0f34cSAndy Grover struct rds_info_lengths *lens) 78900e0f34cSAndy Grover { 790b2c9272aSSalvatore Mesoraca u64 buffer[(sizeof(struct rds_info_connection) + 7) / 8]; 791b2c9272aSSalvatore Mesoraca 792992c9ec5SSowmini Varadhan rds_walk_conn_path_info(sock, len, iter, lens, 79300e0f34cSAndy Grover rds_conn_info_visitor, 794b2c9272aSSalvatore Mesoraca buffer, 79500e0f34cSAndy Grover sizeof(struct rds_info_connection)); 79600e0f34cSAndy Grover } 79700e0f34cSAndy Grover 798e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 799b7ff8b10SKa-Cheong Poon static void rds6_conn_info(struct socket *sock, unsigned int len, 800b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter, 801b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens) 802b7ff8b10SKa-Cheong Poon { 803b7ff8b10SKa-Cheong Poon u64 buffer[(sizeof(struct rds6_info_connection) + 7) / 8]; 804b7ff8b10SKa-Cheong Poon 805b7ff8b10SKa-Cheong Poon rds_walk_conn_path_info(sock, len, iter, lens, 806b7ff8b10SKa-Cheong Poon rds6_conn_info_visitor, 807b7ff8b10SKa-Cheong Poon buffer, 808b7ff8b10SKa-Cheong Poon sizeof(struct rds6_info_connection)); 809b7ff8b10SKa-Cheong Poon } 810e65d4d96SKa-Cheong Poon #endif 811b7ff8b10SKa-Cheong Poon 812ef87b7eaSZach Brown int rds_conn_init(void) 81300e0f34cSAndy Grover { 814c809195fSSowmini Varadhan int ret; 815c809195fSSowmini Varadhan 816c809195fSSowmini Varadhan ret = rds_loop_net_init(); /* register pernet callback */ 817c809195fSSowmini Varadhan if (ret) 818c809195fSSowmini Varadhan return ret; 819c809195fSSowmini Varadhan 82000e0f34cSAndy Grover rds_conn_slab = kmem_cache_create("rds_connection", 82100e0f34cSAndy Grover sizeof(struct rds_connection), 82200e0f34cSAndy Grover 0, 0, NULL); 823c809195fSSowmini Varadhan if (!rds_conn_slab) { 824c809195fSSowmini Varadhan rds_loop_net_exit(); 82500e0f34cSAndy Grover return -ENOMEM; 826c809195fSSowmini Varadhan } 82700e0f34cSAndy Grover 82800e0f34cSAndy Grover rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info); 82900e0f34cSAndy Grover rds_info_register_func(RDS_INFO_SEND_MESSAGES, 83000e0f34cSAndy Grover rds_conn_message_info_send); 83100e0f34cSAndy Grover rds_info_register_func(RDS_INFO_RETRANS_MESSAGES, 83200e0f34cSAndy Grover rds_conn_message_info_retrans); 833e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 834b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_CONNECTIONS, rds6_conn_info); 835b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_SEND_MESSAGES, 836b7ff8b10SKa-Cheong Poon rds6_conn_message_info_send); 837b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_RETRANS_MESSAGES, 838b7ff8b10SKa-Cheong Poon rds6_conn_message_info_retrans); 839e65d4d96SKa-Cheong Poon #endif 84000e0f34cSAndy Grover return 0; 84100e0f34cSAndy Grover } 84200e0f34cSAndy Grover 84300e0f34cSAndy Grover void rds_conn_exit(void) 84400e0f34cSAndy Grover { 845c809195fSSowmini Varadhan rds_loop_net_exit(); /* unregister pernet callback */ 84600e0f34cSAndy Grover rds_loop_exit(); 84700e0f34cSAndy Grover 84800e0f34cSAndy Grover WARN_ON(!hlist_empty(rds_conn_hash)); 84900e0f34cSAndy Grover 85000e0f34cSAndy Grover kmem_cache_destroy(rds_conn_slab); 85100e0f34cSAndy Grover 85200e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info); 85300e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_SEND_MESSAGES, 85400e0f34cSAndy Grover rds_conn_message_info_send); 85500e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES, 85600e0f34cSAndy Grover rds_conn_message_info_retrans); 857e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6) 858b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_CONNECTIONS, rds6_conn_info); 859b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_SEND_MESSAGES, 860b7ff8b10SKa-Cheong Poon rds6_conn_message_info_send); 861b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_RETRANS_MESSAGES, 862b7ff8b10SKa-Cheong Poon rds6_conn_message_info_retrans); 863e65d4d96SKa-Cheong Poon #endif 86400e0f34cSAndy Grover } 86500e0f34cSAndy Grover 86600e0f34cSAndy Grover /* 86700e0f34cSAndy Grover * Force a disconnect 86800e0f34cSAndy Grover */ 869aed20a53SSowmini Varadhan void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy) 8700cb43965SSowmini Varadhan { 8710cb43965SSowmini Varadhan atomic_set(&cp->cp_state, RDS_CONN_ERROR); 872aed20a53SSowmini Varadhan 8733db6e0d1SSowmini Varadhan rcu_read_lock(); 874ebeeb1adSSowmini Varadhan if (!destroy && rds_destroy_pending(cp->cp_conn)) { 8753db6e0d1SSowmini Varadhan rcu_read_unlock(); 876aed20a53SSowmini Varadhan return; 8773db6e0d1SSowmini Varadhan } 8780cb43965SSowmini Varadhan queue_work(rds_wq, &cp->cp_down_w); 8793db6e0d1SSowmini Varadhan rcu_read_unlock(); 8800cb43965SSowmini Varadhan } 8810cb43965SSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_drop); 8820cb43965SSowmini Varadhan 88300e0f34cSAndy Grover void rds_conn_drop(struct rds_connection *conn) 88400e0f34cSAndy Grover { 8855916e2c1SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable); 886aed20a53SSowmini Varadhan rds_conn_path_drop(&conn->c_path[0], false); 88700e0f34cSAndy Grover } 888616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_drop); 88900e0f34cSAndy Grover 89000e0f34cSAndy Grover /* 891f3c6808dSZach Brown * If the connection is down, trigger a connect. We may have scheduled a 892f3c6808dSZach Brown * delayed reconnect however - in this case we should not interfere. 893f3c6808dSZach Brown */ 8943c0a5900SSowmini Varadhan void rds_conn_path_connect_if_down(struct rds_conn_path *cp) 8953c0a5900SSowmini Varadhan { 8963db6e0d1SSowmini Varadhan rcu_read_lock(); 897ebeeb1adSSowmini Varadhan if (rds_destroy_pending(cp->cp_conn)) { 8983db6e0d1SSowmini Varadhan rcu_read_unlock(); 8993db6e0d1SSowmini Varadhan return; 9003db6e0d1SSowmini Varadhan } 9013c0a5900SSowmini Varadhan if (rds_conn_path_state(cp) == RDS_CONN_DOWN && 9023c0a5900SSowmini Varadhan !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags)) 9033c0a5900SSowmini Varadhan queue_delayed_work(rds_wq, &cp->cp_conn_w, 0); 9043db6e0d1SSowmini Varadhan rcu_read_unlock(); 9053c0a5900SSowmini Varadhan } 9061a0e100fSSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down); 9073c0a5900SSowmini Varadhan 908f3c6808dSZach Brown void rds_conn_connect_if_down(struct rds_connection *conn) 909f3c6808dSZach Brown { 9103c0a5900SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable); 9113c0a5900SSowmini Varadhan rds_conn_path_connect_if_down(&conn->c_path[0]); 912f3c6808dSZach Brown } 913f3c6808dSZach Brown EXPORT_SYMBOL_GPL(rds_conn_connect_if_down); 914f3c6808dSZach Brown 915fb1b3dc4SSowmini Varadhan void 916fb1b3dc4SSowmini Varadhan __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...) 917fb1b3dc4SSowmini Varadhan { 918fb1b3dc4SSowmini Varadhan va_list ap; 919fb1b3dc4SSowmini Varadhan 920fb1b3dc4SSowmini Varadhan va_start(ap, fmt); 921fb1b3dc4SSowmini Varadhan vprintk(fmt, ap); 922fb1b3dc4SSowmini Varadhan va_end(ap); 923fb1b3dc4SSowmini Varadhan 924aed20a53SSowmini Varadhan rds_conn_path_drop(cp, false); 925fb1b3dc4SSowmini Varadhan } 926