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
rds_conn_bucket(const struct in6_addr * laddr,const struct in6_addr * faddr)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 */
rds_conn_lookup(struct net * net,struct hlist_head * head,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,u8 tos,int dev_if)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 */
rds_conn_path_reset(struct rds_conn_path * cp)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
__rds_conn_path_init(struct rds_connection * conn,struct rds_conn_path * cp,bool is_outgoing)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 */
__rds_conn_create(struct net * net,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,gfp_t gfp,u8 tos,int is_outgoing,int dev_if)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;
243aced3ce5SRao Shoaib if (trans->t_prefer_loopback) {
244aced3ce5SRao Shoaib if (likely(is_outgoing)) {
245aced3ce5SRao Shoaib /* "outgoing" connection to local address.
246aced3ce5SRao Shoaib * Protocol says it wants the connection
247aced3ce5SRao Shoaib * handled by the loopback transport.
248aced3ce5SRao Shoaib * This is what TCP does.
24900e0f34cSAndy Grover */
25000e0f34cSAndy Grover trans = &rds_loop_transport;
251aced3ce5SRao Shoaib } else {
252aced3ce5SRao Shoaib /* No transport currently in use
253aced3ce5SRao Shoaib * should end up here, but if it
254aced3ce5SRao Shoaib * does, reset/destroy the connection.
255aced3ce5SRao Shoaib */
256*5f9562ebSHangyu Hua kfree(conn->c_path);
257aced3ce5SRao Shoaib kmem_cache_free(rds_conn_slab, conn);
258aced3ce5SRao Shoaib conn = ERR_PTR(-EOPNOTSUPP);
259aced3ce5SRao Shoaib goto out;
260aced3ce5SRao Shoaib }
26100e0f34cSAndy Grover }
26200e0f34cSAndy Grover }
26300e0f34cSAndy Grover
26400e0f34cSAndy Grover conn->c_trans = trans;
26500e0f34cSAndy Grover
2665916e2c1SSowmini Varadhan init_waitqueue_head(&conn->c_hs_waitq);
267840df162SSowmini Varadhan for (i = 0; i < npaths; i++) {
2685916e2c1SSowmini Varadhan __rds_conn_path_init(conn, &conn->c_path[i],
2695916e2c1SSowmini Varadhan is_outgoing);
2705916e2c1SSowmini Varadhan conn->c_path[i].cp_index = i;
2715916e2c1SSowmini Varadhan }
272ebeeb1adSSowmini Varadhan rcu_read_lock();
273ebeeb1adSSowmini Varadhan if (rds_destroy_pending(conn))
274ebeeb1adSSowmini Varadhan ret = -ENETDOWN;
275ebeeb1adSSowmini Varadhan else
276d4014d8cSSowmini Varadhan ret = trans->conn_alloc(conn, GFP_ATOMIC);
27700e0f34cSAndy Grover if (ret) {
278ebeeb1adSSowmini Varadhan rcu_read_unlock();
279840df162SSowmini Varadhan kfree(conn->c_path);
28000e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn);
28100e0f34cSAndy Grover conn = ERR_PTR(ret);
28200e0f34cSAndy Grover goto out;
28300e0f34cSAndy Grover }
28400e0f34cSAndy Grover
285eee2fa6aSKa-Cheong Poon rdsdebug("allocated conn %p for %pI6c -> %pI6c over %s %s\n",
286eee2fa6aSKa-Cheong Poon conn, laddr, faddr,
287eee2fa6aSKa-Cheong Poon strnlen(trans->t_name, sizeof(trans->t_name)) ?
288eee2fa6aSKa-Cheong Poon trans->t_name : "[unknown]", is_outgoing ? "(outgoing)" : "");
28900e0f34cSAndy Grover
290cb24405eSAndy Grover /*
291cb24405eSAndy Grover * Since we ran without holding the conn lock, someone could
292cb24405eSAndy Grover * have created the same conn (either normal or passive) in the
293cb24405eSAndy Grover * interim. We check while holding the lock. If we won, we complete
294cb24405eSAndy Grover * init and return our conn. If we lost, we rollback and return the
295cb24405eSAndy Grover * other one.
296cb24405eSAndy Grover */
29700e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags);
298cb24405eSAndy Grover if (parent) {
299cb24405eSAndy Grover /* Creating passive conn */
300cb24405eSAndy Grover if (parent->c_passive) {
3011c5113cfSSowmini Varadhan trans->conn_free(conn->c_path[0].cp_transport_data);
302840df162SSowmini Varadhan kfree(conn->c_path);
30300e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn);
304cb24405eSAndy Grover conn = parent->c_passive;
30500e0f34cSAndy Grover } else {
306cb24405eSAndy Grover parent->c_passive = conn;
30700e0f34cSAndy Grover rds_cong_add_conn(conn);
30800e0f34cSAndy Grover rds_conn_count++;
30900e0f34cSAndy Grover }
310cb24405eSAndy Grover } else {
311cb24405eSAndy Grover /* Creating normal conn */
312cb24405eSAndy Grover struct rds_connection *found;
31300e0f34cSAndy Grover
314eee2fa6aSKa-Cheong Poon found = rds_conn_lookup(net, head, laddr, faddr, trans,
3153eb45036SSantosh Shilimkar tos, dev_if);
316cb24405eSAndy Grover if (found) {
3171c5113cfSSowmini Varadhan struct rds_conn_path *cp;
3181c5113cfSSowmini Varadhan int i;
3191c5113cfSSowmini Varadhan
320840df162SSowmini Varadhan for (i = 0; i < npaths; i++) {
3211c5113cfSSowmini Varadhan cp = &conn->c_path[i];
32202105b2cSSowmini Varadhan /* The ->conn_alloc invocation may have
32302105b2cSSowmini Varadhan * allocated resource for all paths, so all
32402105b2cSSowmini Varadhan * of them may have to be freed here.
32502105b2cSSowmini Varadhan */
32602105b2cSSowmini Varadhan if (cp->cp_transport_data)
3271c5113cfSSowmini Varadhan trans->conn_free(cp->cp_transport_data);
3281c5113cfSSowmini Varadhan }
329840df162SSowmini Varadhan kfree(conn->c_path);
330cb24405eSAndy Grover kmem_cache_free(rds_conn_slab, conn);
331cb24405eSAndy Grover conn = found;
332cb24405eSAndy Grover } else {
333905dd418SSowmini Varadhan conn->c_my_gen_num = rds_gen_num;
334905dd418SSowmini Varadhan conn->c_peer_gen_num = 0;
335bcf50ef2SChris Mason hlist_add_head_rcu(&conn->c_hash_node, head);
336cb24405eSAndy Grover rds_cong_add_conn(conn);
337cb24405eSAndy Grover rds_conn_count++;
338cb24405eSAndy Grover }
339cb24405eSAndy Grover }
34000e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags);
341ebeeb1adSSowmini Varadhan rcu_read_unlock();
34200e0f34cSAndy Grover
34300e0f34cSAndy Grover out:
34400e0f34cSAndy Grover return conn;
34500e0f34cSAndy Grover }
34600e0f34cSAndy Grover
rds_conn_create(struct net * net,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,u8 tos,gfp_t gfp,int dev_if)347d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create(struct net *net,
348eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr,
349eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr,
3503eb45036SSantosh Shilimkar struct rds_transport *trans, u8 tos,
3513eb45036SSantosh Shilimkar gfp_t gfp, int dev_if)
35200e0f34cSAndy Grover {
3533eb45036SSantosh Shilimkar return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 0, dev_if);
35400e0f34cSAndy Grover }
355616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create);
35600e0f34cSAndy Grover
rds_conn_create_outgoing(struct net * net,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,u8 tos,gfp_t gfp,int dev_if)357d5a8ac28SSowmini Varadhan struct rds_connection *rds_conn_create_outgoing(struct net *net,
358eee2fa6aSKa-Cheong Poon const struct in6_addr *laddr,
359eee2fa6aSKa-Cheong Poon const struct in6_addr *faddr,
360eee2fa6aSKa-Cheong Poon struct rds_transport *trans,
3613eb45036SSantosh Shilimkar u8 tos, gfp_t gfp, int dev_if)
36200e0f34cSAndy Grover {
3633eb45036SSantosh Shilimkar return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 1, dev_if);
36400e0f34cSAndy Grover }
365616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
36600e0f34cSAndy Grover
rds_conn_shutdown(struct rds_conn_path * cp)367d769ef81SSowmini Varadhan void rds_conn_shutdown(struct rds_conn_path *cp)
3682dc39357SAndy Grover {
369d769ef81SSowmini Varadhan struct rds_connection *conn = cp->cp_conn;
370d769ef81SSowmini Varadhan
3712dc39357SAndy Grover /* shut it down unless it's down already */
372d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
3732dc39357SAndy Grover /*
3742dc39357SAndy Grover * Quiesce the connection mgmt handlers before we start tearing
3752dc39357SAndy Grover * things down. We don't hold the mutex for the entire
3762dc39357SAndy Grover * duration of the shutdown operation, else we may be
3772dc39357SAndy Grover * deadlocking with the CM handler. Instead, the CM event
3782dc39357SAndy Grover * handler is supposed to check for state DISCONNECTING
3792dc39357SAndy Grover */
380d769ef81SSowmini Varadhan mutex_lock(&cp->cp_cm_lock);
381d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_UP,
382d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING) &&
383d769ef81SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR,
384d769ef81SSowmini Varadhan RDS_CONN_DISCONNECTING)) {
385d769ef81SSowmini Varadhan rds_conn_path_error(cp,
386d769ef81SSowmini Varadhan "shutdown called in state %d\n",
387d769ef81SSowmini Varadhan atomic_read(&cp->cp_state));
388d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock);
3892dc39357SAndy Grover return;
3902dc39357SAndy Grover }
391d769ef81SSowmini Varadhan mutex_unlock(&cp->cp_cm_lock);
3922dc39357SAndy Grover
393d769ef81SSowmini Varadhan wait_event(cp->cp_waitq,
394d769ef81SSowmini Varadhan !test_bit(RDS_IN_XMIT, &cp->cp_flags));
395d769ef81SSowmini Varadhan wait_event(cp->cp_waitq,
396d769ef81SSowmini Varadhan !test_bit(RDS_RECV_REFILL, &cp->cp_flags));
3977e3f2952SChris Mason
398d769ef81SSowmini Varadhan conn->c_trans->conn_path_shutdown(cp);
399d769ef81SSowmini Varadhan rds_conn_path_reset(cp);
4002dc39357SAndy Grover
401d769ef81SSowmini Varadhan if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING,
402e97656d0SSowmini Varadhan RDS_CONN_DOWN) &&
403e97656d0SSowmini Varadhan !rds_conn_path_transition(cp, RDS_CONN_ERROR,
404d769ef81SSowmini Varadhan RDS_CONN_DOWN)) {
4052dc39357SAndy Grover /* This can happen - eg when we're in the middle of tearing
4062dc39357SAndy Grover * down the connection, and someone unloads the rds module.
407e97656d0SSowmini Varadhan * Quite reproducible with loopback connections.
4082dc39357SAndy Grover * Mostly harmless.
409e97656d0SSowmini Varadhan *
410e97656d0SSowmini Varadhan * Note that this also happens with rds-tcp because
411e97656d0SSowmini Varadhan * we could have triggered rds_conn_path_drop in irq
412e97656d0SSowmini Varadhan * mode from rds_tcp_state change on the receipt of
413e97656d0SSowmini Varadhan * a FIN, thus we need to recheck for RDS_CONN_ERROR
414e97656d0SSowmini Varadhan * here.
4152dc39357SAndy Grover */
416d769ef81SSowmini Varadhan rds_conn_path_error(cp, "%s: failed to transition "
417d769ef81SSowmini Varadhan "to state DOWN, current state "
418d769ef81SSowmini Varadhan "is %d\n", __func__,
419d769ef81SSowmini Varadhan atomic_read(&cp->cp_state));
4202dc39357SAndy Grover return;
4212dc39357SAndy Grover }
4222dc39357SAndy Grover }
4232dc39357SAndy Grover
4242dc39357SAndy Grover /* Then reconnect if it's still live.
4252dc39357SAndy Grover * The passive side of an IB loopback connection is never added
4262dc39357SAndy Grover * to the conn hash, so we never trigger a reconnect on this
4272dc39357SAndy Grover * conn - the reconnect is always triggered by the active peer. */
428d769ef81SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_conn_w);
429bcf50ef2SChris Mason rcu_read_lock();
430bcf50ef2SChris Mason if (!hlist_unhashed(&conn->c_hash_node)) {
431bcf50ef2SChris Mason rcu_read_unlock();
432d769ef81SSowmini Varadhan rds_queue_reconnect(cp);
433bcf50ef2SChris Mason } else {
434bcf50ef2SChris Mason rcu_read_unlock();
435bcf50ef2SChris Mason }
4362dc39357SAndy Grover }
4372dc39357SAndy Grover
4383ecc5693SSowmini Varadhan /* destroy a single rds_conn_path. rds_conn_destroy() iterates over
4393ecc5693SSowmini Varadhan * all paths using rds_conn_path_destroy()
4403ecc5693SSowmini Varadhan */
rds_conn_path_destroy(struct rds_conn_path * cp)4413ecc5693SSowmini Varadhan static void rds_conn_path_destroy(struct rds_conn_path *cp)
4423ecc5693SSowmini Varadhan {
4433ecc5693SSowmini Varadhan struct rds_message *rm, *rtmp;
4443ecc5693SSowmini Varadhan
44502105b2cSSowmini Varadhan if (!cp->cp_transport_data)
44602105b2cSSowmini Varadhan return;
44702105b2cSSowmini Varadhan
4483ecc5693SSowmini Varadhan /* make sure lingering queued work won't try to ref the conn */
4493ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_send_w);
4503ecc5693SSowmini Varadhan cancel_delayed_work_sync(&cp->cp_recv_w);
4513ecc5693SSowmini Varadhan
452aed20a53SSowmini Varadhan rds_conn_path_drop(cp, true);
453aed20a53SSowmini Varadhan flush_work(&cp->cp_down_w);
454aed20a53SSowmini Varadhan
4553ecc5693SSowmini Varadhan /* tear down queued messages */
4563ecc5693SSowmini Varadhan list_for_each_entry_safe(rm, rtmp,
4573ecc5693SSowmini Varadhan &cp->cp_send_queue,
4583ecc5693SSowmini Varadhan m_conn_item) {
4593ecc5693SSowmini Varadhan list_del_init(&rm->m_conn_item);
4603ecc5693SSowmini Varadhan BUG_ON(!list_empty(&rm->m_sock_item));
4613ecc5693SSowmini Varadhan rds_message_put(rm);
4623ecc5693SSowmini Varadhan }
4633ecc5693SSowmini Varadhan if (cp->cp_xmit_rm)
4643ecc5693SSowmini Varadhan rds_message_put(cp->cp_xmit_rm);
4653ecc5693SSowmini Varadhan
4663db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_send_w));
4673db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_recv_w));
4683db6e0d1SSowmini Varadhan WARN_ON(delayed_work_pending(&cp->cp_conn_w));
4693db6e0d1SSowmini Varadhan WARN_ON(work_pending(&cp->cp_down_w));
4703db6e0d1SSowmini Varadhan
4713ecc5693SSowmini Varadhan cp->cp_conn->c_trans->conn_free(cp->cp_transport_data);
4723ecc5693SSowmini Varadhan }
4733ecc5693SSowmini Varadhan
4742dc39357SAndy Grover /*
4752dc39357SAndy Grover * Stop and free a connection.
476ffcec0e1SZach Brown *
477ffcec0e1SZach Brown * This can only be used in very limited circumstances. It assumes that once
478ffcec0e1SZach Brown * the conn has been shutdown that no one else is referencing the connection.
479ffcec0e1SZach Brown * We can only ensure this in the rmmod path in the current code.
4802dc39357SAndy Grover */
rds_conn_destroy(struct rds_connection * conn)48100e0f34cSAndy Grover void rds_conn_destroy(struct rds_connection *conn)
48200e0f34cSAndy Grover {
483fe8ff6b5SZach Brown unsigned long flags;
48402105b2cSSowmini Varadhan int i;
48502105b2cSSowmini Varadhan struct rds_conn_path *cp;
486840df162SSowmini Varadhan int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
48700e0f34cSAndy Grover
48800e0f34cSAndy Grover rdsdebug("freeing conn %p for %pI4 -> "
48900e0f34cSAndy Grover "%pI4\n", conn, &conn->c_laddr,
49000e0f34cSAndy Grover &conn->c_faddr);
49100e0f34cSAndy Grover
492abf45439SChris Mason /* Ensure conn will not be scheduled for reconnect */
493abf45439SChris Mason spin_lock_irq(&rds_conn_lock);
494bcf50ef2SChris Mason hlist_del_init_rcu(&conn->c_hash_node);
495abf45439SChris Mason spin_unlock_irq(&rds_conn_lock);
496bcf50ef2SChris Mason synchronize_rcu();
497bcf50ef2SChris Mason
498ffcec0e1SZach Brown /* shut the connection down */
499840df162SSowmini Varadhan for (i = 0; i < npaths; i++) {
5003ecc5693SSowmini Varadhan cp = &conn->c_path[i];
5013ecc5693SSowmini Varadhan rds_conn_path_destroy(cp);
5023ecc5693SSowmini Varadhan BUG_ON(!list_empty(&cp->cp_retrans));
50300e0f34cSAndy Grover }
50400e0f34cSAndy Grover
50500e0f34cSAndy Grover /*
50600e0f34cSAndy Grover * The congestion maps aren't freed up here. They're
50700e0f34cSAndy Grover * freed by rds_cong_exit() after all the connections
50800e0f34cSAndy Grover * have been freed.
50900e0f34cSAndy Grover */
51000e0f34cSAndy Grover rds_cong_remove_conn(conn);
51100e0f34cSAndy Grover
512840df162SSowmini Varadhan kfree(conn->c_path);
51300e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn);
51400e0f34cSAndy Grover
515fe8ff6b5SZach Brown spin_lock_irqsave(&rds_conn_lock, flags);
51600e0f34cSAndy Grover rds_conn_count--;
517fe8ff6b5SZach Brown spin_unlock_irqrestore(&rds_conn_lock, flags);
51800e0f34cSAndy Grover }
519616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_destroy);
52000e0f34cSAndy Grover
__rds_inc_msg_cp(struct rds_incoming * inc,struct rds_info_iterator * iter,void * saddr,void * daddr,int flip,bool isv6)5211e2b44e7SKa-Cheong Poon static void __rds_inc_msg_cp(struct rds_incoming *inc,
5221e2b44e7SKa-Cheong Poon struct rds_info_iterator *iter,
523b7ff8b10SKa-Cheong Poon void *saddr, void *daddr, int flip, bool isv6)
5241e2b44e7SKa-Cheong Poon {
525e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
526b7ff8b10SKa-Cheong Poon if (isv6)
527b7ff8b10SKa-Cheong Poon rds6_inc_info_copy(inc, iter, saddr, daddr, flip);
528b7ff8b10SKa-Cheong Poon else
529e65d4d96SKa-Cheong Poon #endif
5301e2b44e7SKa-Cheong Poon rds_inc_info_copy(inc, iter, *(__be32 *)saddr,
5311e2b44e7SKa-Cheong Poon *(__be32 *)daddr, flip);
5321e2b44e7SKa-Cheong Poon }
5331e2b44e7SKa-Cheong Poon
rds_conn_message_info_cmn(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int want_send,bool isv6)5341e2b44e7SKa-Cheong Poon static void rds_conn_message_info_cmn(struct socket *sock, unsigned int len,
53500e0f34cSAndy Grover struct rds_info_iterator *iter,
53600e0f34cSAndy Grover struct rds_info_lengths *lens,
537b7ff8b10SKa-Cheong Poon int want_send, bool isv6)
53800e0f34cSAndy Grover {
53900e0f34cSAndy Grover struct hlist_head *head;
54000e0f34cSAndy Grover struct list_head *list;
54100e0f34cSAndy Grover struct rds_connection *conn;
54200e0f34cSAndy Grover struct rds_message *rm;
54300e0f34cSAndy Grover unsigned int total = 0;
544501dcccdSZach Brown unsigned long flags;
54500e0f34cSAndy Grover size_t i;
546992c9ec5SSowmini Varadhan int j;
54700e0f34cSAndy Grover
548b7ff8b10SKa-Cheong Poon if (isv6)
549b7ff8b10SKa-Cheong Poon len /= sizeof(struct rds6_info_message);
550b7ff8b10SKa-Cheong Poon else
55100e0f34cSAndy Grover len /= sizeof(struct rds_info_message);
55200e0f34cSAndy Grover
553bcf50ef2SChris Mason rcu_read_lock();
55400e0f34cSAndy Grover
55500e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
55600e0f34cSAndy Grover i++, head++) {
557b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) {
558992c9ec5SSowmini Varadhan struct rds_conn_path *cp;
559840df162SSowmini Varadhan int npaths;
56000e0f34cSAndy Grover
561b7ff8b10SKa-Cheong Poon if (!isv6 && conn->c_isv6)
562b7ff8b10SKa-Cheong Poon continue;
563b7ff8b10SKa-Cheong Poon
564840df162SSowmini Varadhan npaths = (conn->c_trans->t_mp_capable ?
565840df162SSowmini Varadhan RDS_MPATH_WORKERS : 1);
566840df162SSowmini Varadhan
567840df162SSowmini Varadhan for (j = 0; j < npaths; j++) {
568992c9ec5SSowmini Varadhan cp = &conn->c_path[j];
569992c9ec5SSowmini Varadhan if (want_send)
570992c9ec5SSowmini Varadhan list = &cp->cp_send_queue;
571992c9ec5SSowmini Varadhan else
572992c9ec5SSowmini Varadhan list = &cp->cp_retrans;
573992c9ec5SSowmini Varadhan
574992c9ec5SSowmini Varadhan spin_lock_irqsave(&cp->cp_lock, flags);
57500e0f34cSAndy Grover
57600e0f34cSAndy Grover /* XXX too lazy to maintain counts.. */
57700e0f34cSAndy Grover list_for_each_entry(rm, list, m_conn_item) {
57800e0f34cSAndy Grover total++;
57900e0f34cSAndy Grover if (total <= len)
5801e2b44e7SKa-Cheong Poon __rds_inc_msg_cp(&rm->m_inc,
581992c9ec5SSowmini Varadhan iter,
5821e2b44e7SKa-Cheong Poon &conn->c_laddr,
5831e2b44e7SKa-Cheong Poon &conn->c_faddr,
584b7ff8b10SKa-Cheong Poon 0, isv6);
58500e0f34cSAndy Grover }
58600e0f34cSAndy Grover
587992c9ec5SSowmini Varadhan spin_unlock_irqrestore(&cp->cp_lock, flags);
588992c9ec5SSowmini Varadhan }
58900e0f34cSAndy Grover }
59000e0f34cSAndy Grover }
591bcf50ef2SChris Mason rcu_read_unlock();
59200e0f34cSAndy Grover
59300e0f34cSAndy Grover lens->nr = total;
594b7ff8b10SKa-Cheong Poon if (isv6)
595b7ff8b10SKa-Cheong Poon lens->each = sizeof(struct rds6_info_message);
596b7ff8b10SKa-Cheong Poon else
59700e0f34cSAndy Grover lens->each = sizeof(struct rds_info_message);
59800e0f34cSAndy Grover }
59900e0f34cSAndy Grover
rds_conn_message_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int want_send)6001e2b44e7SKa-Cheong Poon static void rds_conn_message_info(struct socket *sock, unsigned int len,
6011e2b44e7SKa-Cheong Poon struct rds_info_iterator *iter,
6021e2b44e7SKa-Cheong Poon struct rds_info_lengths *lens,
6031e2b44e7SKa-Cheong Poon int want_send)
6041e2b44e7SKa-Cheong Poon {
605b7ff8b10SKa-Cheong Poon rds_conn_message_info_cmn(sock, len, iter, lens, want_send, false);
606b7ff8b10SKa-Cheong Poon }
607b7ff8b10SKa-Cheong Poon
608e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_message_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int want_send)609b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info(struct socket *sock, unsigned int len,
610b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter,
611b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens,
612b7ff8b10SKa-Cheong Poon int want_send)
613b7ff8b10SKa-Cheong Poon {
614b7ff8b10SKa-Cheong Poon rds_conn_message_info_cmn(sock, len, iter, lens, want_send, true);
6151e2b44e7SKa-Cheong Poon }
616e65d4d96SKa-Cheong Poon #endif
6171e2b44e7SKa-Cheong Poon
rds_conn_message_info_send(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)61800e0f34cSAndy Grover static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
61900e0f34cSAndy Grover struct rds_info_iterator *iter,
62000e0f34cSAndy Grover struct rds_info_lengths *lens)
62100e0f34cSAndy Grover {
62200e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 1);
62300e0f34cSAndy Grover }
62400e0f34cSAndy Grover
625e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_message_info_send(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)626b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info_send(struct socket *sock, unsigned int len,
627b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter,
628b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens)
629b7ff8b10SKa-Cheong Poon {
630b7ff8b10SKa-Cheong Poon rds6_conn_message_info(sock, len, iter, lens, 1);
631b7ff8b10SKa-Cheong Poon }
632e65d4d96SKa-Cheong Poon #endif
633b7ff8b10SKa-Cheong Poon
rds_conn_message_info_retrans(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)63400e0f34cSAndy Grover static void rds_conn_message_info_retrans(struct socket *sock,
63500e0f34cSAndy Grover unsigned int len,
63600e0f34cSAndy Grover struct rds_info_iterator *iter,
63700e0f34cSAndy Grover struct rds_info_lengths *lens)
63800e0f34cSAndy Grover {
63900e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 0);
64000e0f34cSAndy Grover }
64100e0f34cSAndy Grover
642e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_message_info_retrans(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)643b7ff8b10SKa-Cheong Poon static void rds6_conn_message_info_retrans(struct socket *sock,
644b7ff8b10SKa-Cheong Poon unsigned int len,
645b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter,
646b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens)
647b7ff8b10SKa-Cheong Poon {
648b7ff8b10SKa-Cheong Poon rds6_conn_message_info(sock, len, iter, lens, 0);
649b7ff8b10SKa-Cheong Poon }
650e65d4d96SKa-Cheong Poon #endif
651b7ff8b10SKa-Cheong Poon
rds_for_each_conn_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int (* visitor)(struct rds_connection *,void *),u64 * buffer,size_t item_len)65200e0f34cSAndy Grover void rds_for_each_conn_info(struct socket *sock, unsigned int len,
65300e0f34cSAndy Grover struct rds_info_iterator *iter,
65400e0f34cSAndy Grover struct rds_info_lengths *lens,
65500e0f34cSAndy Grover int (*visitor)(struct rds_connection *, void *),
656f1cb9d68SSalvatore Mesoraca u64 *buffer,
65700e0f34cSAndy Grover size_t item_len)
65800e0f34cSAndy Grover {
65900e0f34cSAndy Grover struct hlist_head *head;
66000e0f34cSAndy Grover struct rds_connection *conn;
66100e0f34cSAndy Grover size_t i;
66200e0f34cSAndy Grover
663bcf50ef2SChris Mason rcu_read_lock();
66400e0f34cSAndy Grover
66500e0f34cSAndy Grover lens->nr = 0;
66600e0f34cSAndy Grover lens->each = item_len;
66700e0f34cSAndy Grover
66800e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
66900e0f34cSAndy Grover i++, head++) {
670b67bfe0dSSasha Levin hlist_for_each_entry_rcu(conn, head, c_hash_node) {
67100e0f34cSAndy Grover
67200e0f34cSAndy Grover /* XXX no c_lock usage.. */
67300e0f34cSAndy Grover if (!visitor(conn, buffer))
67400e0f34cSAndy Grover continue;
67500e0f34cSAndy Grover
67600e0f34cSAndy Grover /* We copy as much as we can fit in the buffer,
67700e0f34cSAndy Grover * but we count all items so that the caller
67800e0f34cSAndy Grover * can resize the buffer. */
67900e0f34cSAndy Grover if (len >= item_len) {
68000e0f34cSAndy Grover rds_info_copy(iter, buffer, item_len);
68100e0f34cSAndy Grover len -= item_len;
68200e0f34cSAndy Grover }
68300e0f34cSAndy Grover lens->nr++;
68400e0f34cSAndy Grover }
68500e0f34cSAndy Grover }
686bcf50ef2SChris Mason rcu_read_unlock();
68700e0f34cSAndy Grover }
688616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
68900e0f34cSAndy Grover
rds_walk_conn_path_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int (* visitor)(struct rds_conn_path *,void *),u64 * buffer,size_t item_len)690bb789763SSantosh Shilimkar static void rds_walk_conn_path_info(struct socket *sock, unsigned int len,
691992c9ec5SSowmini Varadhan struct rds_info_iterator *iter,
692992c9ec5SSowmini Varadhan struct rds_info_lengths *lens,
693992c9ec5SSowmini Varadhan int (*visitor)(struct rds_conn_path *, void *),
694b2c9272aSSalvatore Mesoraca u64 *buffer,
695992c9ec5SSowmini Varadhan size_t item_len)
696992c9ec5SSowmini Varadhan {
697992c9ec5SSowmini Varadhan struct hlist_head *head;
698992c9ec5SSowmini Varadhan struct rds_connection *conn;
699992c9ec5SSowmini Varadhan size_t i;
700992c9ec5SSowmini Varadhan
701992c9ec5SSowmini Varadhan rcu_read_lock();
702992c9ec5SSowmini Varadhan
703992c9ec5SSowmini Varadhan lens->nr = 0;
704992c9ec5SSowmini Varadhan lens->each = item_len;
705992c9ec5SSowmini Varadhan
706992c9ec5SSowmini Varadhan for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
707992c9ec5SSowmini Varadhan i++, head++) {
708992c9ec5SSowmini Varadhan hlist_for_each_entry_rcu(conn, head, c_hash_node) {
709992c9ec5SSowmini Varadhan struct rds_conn_path *cp;
710992c9ec5SSowmini Varadhan
711eee2fa6aSKa-Cheong Poon /* XXX We only copy the information from the first
712eee2fa6aSKa-Cheong Poon * path for now. The problem is that if there are
713eee2fa6aSKa-Cheong Poon * more than one underlying paths, we cannot report
714eee2fa6aSKa-Cheong Poon * information of all of them using the existing
715eee2fa6aSKa-Cheong Poon * API. For example, there is only one next_tx_seq,
716eee2fa6aSKa-Cheong Poon * which path's next_tx_seq should we report? It is
717eee2fa6aSKa-Cheong Poon * a bug in the design of MPRDS.
718eee2fa6aSKa-Cheong Poon */
719eee2fa6aSKa-Cheong Poon cp = conn->c_path;
720992c9ec5SSowmini Varadhan
721992c9ec5SSowmini Varadhan /* XXX no cp_lock usage.. */
722992c9ec5SSowmini Varadhan if (!visitor(cp, buffer))
723992c9ec5SSowmini Varadhan continue;
724992c9ec5SSowmini Varadhan
725992c9ec5SSowmini Varadhan /* We copy as much as we can fit in the buffer,
726992c9ec5SSowmini Varadhan * but we count all items so that the caller
727992c9ec5SSowmini Varadhan * can resize the buffer.
728992c9ec5SSowmini Varadhan */
729992c9ec5SSowmini Varadhan if (len >= item_len) {
730992c9ec5SSowmini Varadhan rds_info_copy(iter, buffer, item_len);
731992c9ec5SSowmini Varadhan len -= item_len;
732992c9ec5SSowmini Varadhan }
733992c9ec5SSowmini Varadhan lens->nr++;
734992c9ec5SSowmini Varadhan }
735992c9ec5SSowmini Varadhan }
736992c9ec5SSowmini Varadhan rcu_read_unlock();
737992c9ec5SSowmini Varadhan }
738992c9ec5SSowmini Varadhan
rds_conn_info_visitor(struct rds_conn_path * cp,void * buffer)739992c9ec5SSowmini Varadhan static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
74000e0f34cSAndy Grover {
74100e0f34cSAndy Grover struct rds_info_connection *cinfo = buffer;
742eee2fa6aSKa-Cheong Poon struct rds_connection *conn = cp->cp_conn;
74300e0f34cSAndy Grover
7441e2b44e7SKa-Cheong Poon if (conn->c_isv6)
7451e2b44e7SKa-Cheong Poon return 0;
7461e2b44e7SKa-Cheong Poon
747992c9ec5SSowmini Varadhan cinfo->next_tx_seq = cp->cp_next_tx_seq;
748992c9ec5SSowmini Varadhan cinfo->next_rx_seq = cp->cp_next_rx_seq;
749eee2fa6aSKa-Cheong Poon cinfo->laddr = conn->c_laddr.s6_addr32[3];
750eee2fa6aSKa-Cheong Poon cinfo->faddr = conn->c_faddr.s6_addr32[3];
751fc640d4cSGerd Rausch cinfo->tos = conn->c_tos;
752eee2fa6aSKa-Cheong Poon strncpy(cinfo->transport, conn->c_trans->t_name,
75300e0f34cSAndy Grover sizeof(cinfo->transport));
75400e0f34cSAndy Grover cinfo->flags = 0;
75500e0f34cSAndy Grover
756992c9ec5SSowmini Varadhan rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
7570f4b1c7eSZach Brown SENDING);
75800e0f34cSAndy Grover /* XXX Future: return the state rather than these funky bits */
75900e0f34cSAndy Grover rds_conn_info_set(cinfo->flags,
760992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
76100e0f34cSAndy Grover CONNECTING);
76200e0f34cSAndy Grover rds_conn_info_set(cinfo->flags,
763992c9ec5SSowmini Varadhan atomic_read(&cp->cp_state) == RDS_CONN_UP,
76400e0f34cSAndy Grover CONNECTED);
76500e0f34cSAndy Grover return 1;
76600e0f34cSAndy Grover }
76700e0f34cSAndy Grover
768e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_info_visitor(struct rds_conn_path * cp,void * buffer)769b7ff8b10SKa-Cheong Poon static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
770b7ff8b10SKa-Cheong Poon {
771b7ff8b10SKa-Cheong Poon struct rds6_info_connection *cinfo6 = buffer;
772b7ff8b10SKa-Cheong Poon struct rds_connection *conn = cp->cp_conn;
773b7ff8b10SKa-Cheong Poon
774b7ff8b10SKa-Cheong Poon cinfo6->next_tx_seq = cp->cp_next_tx_seq;
775b7ff8b10SKa-Cheong Poon cinfo6->next_rx_seq = cp->cp_next_rx_seq;
776b7ff8b10SKa-Cheong Poon cinfo6->laddr = conn->c_laddr;
777b7ff8b10SKa-Cheong Poon cinfo6->faddr = conn->c_faddr;
778b7ff8b10SKa-Cheong Poon strncpy(cinfo6->transport, conn->c_trans->t_name,
779b7ff8b10SKa-Cheong Poon sizeof(cinfo6->transport));
780b7ff8b10SKa-Cheong Poon cinfo6->flags = 0;
781b7ff8b10SKa-Cheong Poon
782b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
783b7ff8b10SKa-Cheong Poon SENDING);
784b7ff8b10SKa-Cheong Poon /* XXX Future: return the state rather than these funky bits */
785b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags,
786b7ff8b10SKa-Cheong Poon atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
787b7ff8b10SKa-Cheong Poon CONNECTING);
788b7ff8b10SKa-Cheong Poon rds_conn_info_set(cinfo6->flags,
789b7ff8b10SKa-Cheong Poon atomic_read(&cp->cp_state) == RDS_CONN_UP,
790b7ff8b10SKa-Cheong Poon CONNECTED);
791b7ff8b10SKa-Cheong Poon /* Just return 1 as there is no error case. This is a helper function
792b7ff8b10SKa-Cheong Poon * for rds_walk_conn_path_info() and it wants a return value.
793b7ff8b10SKa-Cheong Poon */
794b7ff8b10SKa-Cheong Poon return 1;
795b7ff8b10SKa-Cheong Poon }
796e65d4d96SKa-Cheong Poon #endif
797b7ff8b10SKa-Cheong Poon
rds_conn_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)79800e0f34cSAndy Grover static void rds_conn_info(struct socket *sock, unsigned int len,
79900e0f34cSAndy Grover struct rds_info_iterator *iter,
80000e0f34cSAndy Grover struct rds_info_lengths *lens)
80100e0f34cSAndy Grover {
802b2c9272aSSalvatore Mesoraca u64 buffer[(sizeof(struct rds_info_connection) + 7) / 8];
803b2c9272aSSalvatore Mesoraca
804992c9ec5SSowmini Varadhan rds_walk_conn_path_info(sock, len, iter, lens,
80500e0f34cSAndy Grover rds_conn_info_visitor,
806b2c9272aSSalvatore Mesoraca buffer,
80700e0f34cSAndy Grover sizeof(struct rds_info_connection));
80800e0f34cSAndy Grover }
80900e0f34cSAndy Grover
810e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)811b7ff8b10SKa-Cheong Poon static void rds6_conn_info(struct socket *sock, unsigned int len,
812b7ff8b10SKa-Cheong Poon struct rds_info_iterator *iter,
813b7ff8b10SKa-Cheong Poon struct rds_info_lengths *lens)
814b7ff8b10SKa-Cheong Poon {
815b7ff8b10SKa-Cheong Poon u64 buffer[(sizeof(struct rds6_info_connection) + 7) / 8];
816b7ff8b10SKa-Cheong Poon
817b7ff8b10SKa-Cheong Poon rds_walk_conn_path_info(sock, len, iter, lens,
818b7ff8b10SKa-Cheong Poon rds6_conn_info_visitor,
819b7ff8b10SKa-Cheong Poon buffer,
820b7ff8b10SKa-Cheong Poon sizeof(struct rds6_info_connection));
821b7ff8b10SKa-Cheong Poon }
822e65d4d96SKa-Cheong Poon #endif
823b7ff8b10SKa-Cheong Poon
rds_conn_init(void)824ef87b7eaSZach Brown int rds_conn_init(void)
82500e0f34cSAndy Grover {
826c809195fSSowmini Varadhan int ret;
827c809195fSSowmini Varadhan
828c809195fSSowmini Varadhan ret = rds_loop_net_init(); /* register pernet callback */
829c809195fSSowmini Varadhan if (ret)
830c809195fSSowmini Varadhan return ret;
831c809195fSSowmini Varadhan
83200e0f34cSAndy Grover rds_conn_slab = kmem_cache_create("rds_connection",
83300e0f34cSAndy Grover sizeof(struct rds_connection),
83400e0f34cSAndy Grover 0, 0, NULL);
835c809195fSSowmini Varadhan if (!rds_conn_slab) {
836c809195fSSowmini Varadhan rds_loop_net_exit();
83700e0f34cSAndy Grover return -ENOMEM;
838c809195fSSowmini Varadhan }
83900e0f34cSAndy Grover
84000e0f34cSAndy Grover rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
84100e0f34cSAndy Grover rds_info_register_func(RDS_INFO_SEND_MESSAGES,
84200e0f34cSAndy Grover rds_conn_message_info_send);
84300e0f34cSAndy Grover rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
84400e0f34cSAndy Grover rds_conn_message_info_retrans);
845e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
846b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_CONNECTIONS, rds6_conn_info);
847b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_SEND_MESSAGES,
848b7ff8b10SKa-Cheong Poon rds6_conn_message_info_send);
849b7ff8b10SKa-Cheong Poon rds_info_register_func(RDS6_INFO_RETRANS_MESSAGES,
850b7ff8b10SKa-Cheong Poon rds6_conn_message_info_retrans);
851e65d4d96SKa-Cheong Poon #endif
85200e0f34cSAndy Grover return 0;
85300e0f34cSAndy Grover }
85400e0f34cSAndy Grover
rds_conn_exit(void)85500e0f34cSAndy Grover void rds_conn_exit(void)
85600e0f34cSAndy Grover {
857c809195fSSowmini Varadhan rds_loop_net_exit(); /* unregister pernet callback */
85800e0f34cSAndy Grover rds_loop_exit();
85900e0f34cSAndy Grover
86000e0f34cSAndy Grover WARN_ON(!hlist_empty(rds_conn_hash));
86100e0f34cSAndy Grover
86200e0f34cSAndy Grover kmem_cache_destroy(rds_conn_slab);
86300e0f34cSAndy Grover
86400e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
86500e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
86600e0f34cSAndy Grover rds_conn_message_info_send);
86700e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
86800e0f34cSAndy Grover rds_conn_message_info_retrans);
869e65d4d96SKa-Cheong Poon #if IS_ENABLED(CONFIG_IPV6)
870b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_CONNECTIONS, rds6_conn_info);
871b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_SEND_MESSAGES,
872b7ff8b10SKa-Cheong Poon rds6_conn_message_info_send);
873b7ff8b10SKa-Cheong Poon rds_info_deregister_func(RDS6_INFO_RETRANS_MESSAGES,
874b7ff8b10SKa-Cheong Poon rds6_conn_message_info_retrans);
875e65d4d96SKa-Cheong Poon #endif
87600e0f34cSAndy Grover }
87700e0f34cSAndy Grover
87800e0f34cSAndy Grover /*
87900e0f34cSAndy Grover * Force a disconnect
88000e0f34cSAndy Grover */
rds_conn_path_drop(struct rds_conn_path * cp,bool destroy)881aed20a53SSowmini Varadhan void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy)
8820cb43965SSowmini Varadhan {
8830cb43965SSowmini Varadhan atomic_set(&cp->cp_state, RDS_CONN_ERROR);
884aed20a53SSowmini Varadhan
8853db6e0d1SSowmini Varadhan rcu_read_lock();
886ebeeb1adSSowmini Varadhan if (!destroy && rds_destroy_pending(cp->cp_conn)) {
8873db6e0d1SSowmini Varadhan rcu_read_unlock();
888aed20a53SSowmini Varadhan return;
8893db6e0d1SSowmini Varadhan }
8900cb43965SSowmini Varadhan queue_work(rds_wq, &cp->cp_down_w);
8913db6e0d1SSowmini Varadhan rcu_read_unlock();
8920cb43965SSowmini Varadhan }
8930cb43965SSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_drop);
8940cb43965SSowmini Varadhan
rds_conn_drop(struct rds_connection * conn)89500e0f34cSAndy Grover void rds_conn_drop(struct rds_connection *conn)
89600e0f34cSAndy Grover {
8975916e2c1SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable);
898aed20a53SSowmini Varadhan rds_conn_path_drop(&conn->c_path[0], false);
89900e0f34cSAndy Grover }
900616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_drop);
90100e0f34cSAndy Grover
90200e0f34cSAndy Grover /*
903f3c6808dSZach Brown * If the connection is down, trigger a connect. We may have scheduled a
904f3c6808dSZach Brown * delayed reconnect however - in this case we should not interfere.
905f3c6808dSZach Brown */
rds_conn_path_connect_if_down(struct rds_conn_path * cp)9063c0a5900SSowmini Varadhan void rds_conn_path_connect_if_down(struct rds_conn_path *cp)
9073c0a5900SSowmini Varadhan {
9083db6e0d1SSowmini Varadhan rcu_read_lock();
909ebeeb1adSSowmini Varadhan if (rds_destroy_pending(cp->cp_conn)) {
9103db6e0d1SSowmini Varadhan rcu_read_unlock();
9113db6e0d1SSowmini Varadhan return;
9123db6e0d1SSowmini Varadhan }
9133c0a5900SSowmini Varadhan if (rds_conn_path_state(cp) == RDS_CONN_DOWN &&
9143c0a5900SSowmini Varadhan !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags))
9153c0a5900SSowmini Varadhan queue_delayed_work(rds_wq, &cp->cp_conn_w, 0);
9163db6e0d1SSowmini Varadhan rcu_read_unlock();
9173c0a5900SSowmini Varadhan }
9181a0e100fSSowmini Varadhan EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down);
9193c0a5900SSowmini Varadhan
9209ef845f8SRao Shoaib /* Check connectivity of all paths
9219ef845f8SRao Shoaib */
rds_check_all_paths(struct rds_connection * conn)9229ef845f8SRao Shoaib void rds_check_all_paths(struct rds_connection *conn)
9239ef845f8SRao Shoaib {
9249ef845f8SRao Shoaib int i = 0;
9259ef845f8SRao Shoaib
9269ef845f8SRao Shoaib do {
9279ef845f8SRao Shoaib rds_conn_path_connect_if_down(&conn->c_path[i]);
9289ef845f8SRao Shoaib } while (++i < conn->c_npaths);
9299ef845f8SRao Shoaib }
9309ef845f8SRao Shoaib
rds_conn_connect_if_down(struct rds_connection * conn)931f3c6808dSZach Brown void rds_conn_connect_if_down(struct rds_connection *conn)
932f3c6808dSZach Brown {
9333c0a5900SSowmini Varadhan WARN_ON(conn->c_trans->t_mp_capable);
9343c0a5900SSowmini Varadhan rds_conn_path_connect_if_down(&conn->c_path[0]);
935f3c6808dSZach Brown }
936f3c6808dSZach Brown EXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
937f3c6808dSZach Brown
938fb1b3dc4SSowmini Varadhan void
__rds_conn_path_error(struct rds_conn_path * cp,const char * fmt,...)939fb1b3dc4SSowmini Varadhan __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...)
940fb1b3dc4SSowmini Varadhan {
941fb1b3dc4SSowmini Varadhan va_list ap;
942fb1b3dc4SSowmini Varadhan
943fb1b3dc4SSowmini Varadhan va_start(ap, fmt);
944fb1b3dc4SSowmini Varadhan vprintk(fmt, ap);
945fb1b3dc4SSowmini Varadhan va_end(ap);
946fb1b3dc4SSowmini Varadhan
947aed20a53SSowmini Varadhan rds_conn_path_drop(cp, false);
948fb1b3dc4SSowmini Varadhan }
949