100e0f34cSAndy Grover /* 200e0f34cSAndy Grover * Copyright (c) 2006 Oracle. All rights reserved. 300e0f34cSAndy Grover * 400e0f34cSAndy Grover * This software is available to you under a choice of one of two 500e0f34cSAndy Grover * licenses. You may choose to be licensed under the terms of the GNU 600e0f34cSAndy Grover * General Public License (GPL) Version 2, available from the file 700e0f34cSAndy Grover * COPYING in the main directory of this source tree, or the 800e0f34cSAndy Grover * OpenIB.org BSD license below: 900e0f34cSAndy Grover * 1000e0f34cSAndy Grover * Redistribution and use in source and binary forms, with or 1100e0f34cSAndy Grover * without modification, are permitted provided that the following 1200e0f34cSAndy Grover * conditions are met: 1300e0f34cSAndy Grover * 1400e0f34cSAndy Grover * - Redistributions of source code must retain the above 1500e0f34cSAndy Grover * copyright notice, this list of conditions and the following 1600e0f34cSAndy Grover * disclaimer. 1700e0f34cSAndy Grover * 1800e0f34cSAndy Grover * - Redistributions in binary form must reproduce the above 1900e0f34cSAndy Grover * copyright notice, this list of conditions and the following 2000e0f34cSAndy Grover * disclaimer in the documentation and/or other materials 2100e0f34cSAndy Grover * provided with the distribution. 2200e0f34cSAndy Grover * 2300e0f34cSAndy Grover * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2400e0f34cSAndy Grover * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2500e0f34cSAndy Grover * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2600e0f34cSAndy Grover * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2700e0f34cSAndy Grover * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2800e0f34cSAndy Grover * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2900e0f34cSAndy Grover * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3000e0f34cSAndy Grover * SOFTWARE. 3100e0f34cSAndy Grover * 3200e0f34cSAndy Grover */ 3300e0f34cSAndy Grover #include <linux/kernel.h> 3400e0f34cSAndy Grover #include <linux/list.h> 355a0e3ad6STejun Heo #include <linux/slab.h> 3600e0f34cSAndy Grover #include <net/inet_hashtables.h> 3700e0f34cSAndy Grover 3800e0f34cSAndy Grover #include "rds.h" 3900e0f34cSAndy Grover #include "loop.h" 4000e0f34cSAndy Grover 4100e0f34cSAndy Grover #define RDS_CONNECTION_HASH_BITS 12 4200e0f34cSAndy Grover #define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS) 4300e0f34cSAndy Grover #define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1) 4400e0f34cSAndy Grover 4500e0f34cSAndy Grover /* converting this to RCU is a chore for another day.. */ 4600e0f34cSAndy Grover static DEFINE_SPINLOCK(rds_conn_lock); 4700e0f34cSAndy Grover static unsigned long rds_conn_count; 4800e0f34cSAndy Grover static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES]; 4900e0f34cSAndy Grover static struct kmem_cache *rds_conn_slab; 5000e0f34cSAndy Grover 5100e0f34cSAndy Grover static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr) 5200e0f34cSAndy Grover { 5300e0f34cSAndy Grover /* Pass NULL, don't need struct net for hash */ 5400e0f34cSAndy Grover unsigned long hash = inet_ehashfn(NULL, 5500e0f34cSAndy Grover be32_to_cpu(laddr), 0, 5600e0f34cSAndy Grover be32_to_cpu(faddr), 0); 5700e0f34cSAndy Grover return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK]; 5800e0f34cSAndy Grover } 5900e0f34cSAndy Grover 6000e0f34cSAndy Grover #define rds_conn_info_set(var, test, suffix) do { \ 6100e0f34cSAndy Grover if (test) \ 6200e0f34cSAndy Grover var |= RDS_INFO_CONNECTION_FLAG_##suffix; \ 6300e0f34cSAndy Grover } while (0) 6400e0f34cSAndy Grover 6500e0f34cSAndy Grover static struct rds_connection *rds_conn_lookup(struct hlist_head *head, 6600e0f34cSAndy Grover __be32 laddr, __be32 faddr, 6700e0f34cSAndy Grover struct rds_transport *trans) 6800e0f34cSAndy Grover { 6900e0f34cSAndy Grover struct rds_connection *conn, *ret = NULL; 7000e0f34cSAndy Grover struct hlist_node *pos; 7100e0f34cSAndy Grover 7200e0f34cSAndy Grover hlist_for_each_entry(conn, pos, head, c_hash_node) { 7300e0f34cSAndy Grover if (conn->c_faddr == faddr && conn->c_laddr == laddr && 7400e0f34cSAndy Grover conn->c_trans == trans) { 7500e0f34cSAndy Grover ret = conn; 7600e0f34cSAndy Grover break; 7700e0f34cSAndy Grover } 7800e0f34cSAndy Grover } 7900e0f34cSAndy Grover rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret, 8000e0f34cSAndy Grover &laddr, &faddr); 8100e0f34cSAndy Grover return ret; 8200e0f34cSAndy Grover } 8300e0f34cSAndy Grover 8400e0f34cSAndy Grover /* 8500e0f34cSAndy Grover * This is called by transports as they're bringing down a connection. 8600e0f34cSAndy Grover * It clears partial message state so that the transport can start sending 8700e0f34cSAndy Grover * and receiving over this connection again in the future. It is up to 8800e0f34cSAndy Grover * the transport to have serialized this call with its send and recv. 8900e0f34cSAndy Grover */ 9000e0f34cSAndy Grover void rds_conn_reset(struct rds_connection *conn) 9100e0f34cSAndy Grover { 9200e0f34cSAndy Grover rdsdebug("connection %pI4 to %pI4 reset\n", 9300e0f34cSAndy Grover &conn->c_laddr, &conn->c_faddr); 9400e0f34cSAndy Grover 9500e0f34cSAndy Grover rds_stats_inc(s_conn_reset); 9600e0f34cSAndy Grover rds_send_reset(conn); 9700e0f34cSAndy Grover conn->c_flags = 0; 9800e0f34cSAndy Grover 9900e0f34cSAndy Grover /* Do not clear next_rx_seq here, else we cannot distinguish 10000e0f34cSAndy Grover * retransmitted packets from new packets, and will hand all 10100e0f34cSAndy Grover * of them to the application. That is not consistent with the 10200e0f34cSAndy Grover * reliability guarantees of RDS. */ 10300e0f34cSAndy Grover } 10400e0f34cSAndy Grover 10500e0f34cSAndy Grover /* 10600e0f34cSAndy Grover * There is only every one 'conn' for a given pair of addresses in the 10700e0f34cSAndy Grover * system at a time. They contain messages to be retransmitted and so 10800e0f34cSAndy Grover * span the lifetime of the actual underlying transport connections. 10900e0f34cSAndy Grover * 11000e0f34cSAndy Grover * For now they are not garbage collected once they're created. They 11100e0f34cSAndy Grover * are torn down as the module is removed, if ever. 11200e0f34cSAndy Grover */ 11300e0f34cSAndy Grover static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr, 11400e0f34cSAndy Grover struct rds_transport *trans, gfp_t gfp, 11500e0f34cSAndy Grover int is_outgoing) 11600e0f34cSAndy Grover { 117cb24405eSAndy Grover struct rds_connection *conn, *parent = NULL; 11800e0f34cSAndy Grover struct hlist_head *head = rds_conn_bucket(laddr, faddr); 11900e0f34cSAndy Grover unsigned long flags; 12000e0f34cSAndy Grover int ret; 12100e0f34cSAndy Grover 12200e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 12300e0f34cSAndy Grover conn = rds_conn_lookup(head, laddr, faddr, trans); 124f64f9e71SJoe Perches if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport && 125f64f9e71SJoe Perches !is_outgoing) { 12600e0f34cSAndy Grover /* This is a looped back IB connection, and we're 12700e0f34cSAndy Grover * called by the code handling the incoming connect. 12800e0f34cSAndy Grover * We need a second connection object into which we 12900e0f34cSAndy Grover * can stick the other QP. */ 13000e0f34cSAndy Grover parent = conn; 13100e0f34cSAndy Grover conn = parent->c_passive; 13200e0f34cSAndy Grover } 13300e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 13400e0f34cSAndy Grover if (conn) 13500e0f34cSAndy Grover goto out; 13600e0f34cSAndy Grover 13705a178ecSWei Yongjun conn = kmem_cache_zalloc(rds_conn_slab, gfp); 1388690bfa1SAndy Grover if (!conn) { 13900e0f34cSAndy Grover conn = ERR_PTR(-ENOMEM); 14000e0f34cSAndy Grover goto out; 14100e0f34cSAndy Grover } 14200e0f34cSAndy Grover 14300e0f34cSAndy Grover INIT_HLIST_NODE(&conn->c_hash_node); 14400e0f34cSAndy Grover conn->c_laddr = laddr; 14500e0f34cSAndy Grover conn->c_faddr = faddr; 14600e0f34cSAndy Grover spin_lock_init(&conn->c_lock); 14700e0f34cSAndy Grover conn->c_next_tx_seq = 1; 14800e0f34cSAndy Grover 149049ee3f5SAndy Grover spin_lock_init(&conn->c_send_lock); 1509e29db0eSChris Mason atomic_set(&conn->c_send_generation, 1); 1517e3f2952SChris Mason atomic_set(&conn->c_senders, 0); 15200e0f34cSAndy Grover INIT_LIST_HEAD(&conn->c_send_queue); 15300e0f34cSAndy Grover INIT_LIST_HEAD(&conn->c_retrans); 15400e0f34cSAndy Grover 15500e0f34cSAndy Grover ret = rds_cong_get_maps(conn); 15600e0f34cSAndy Grover if (ret) { 15700e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 15800e0f34cSAndy Grover conn = ERR_PTR(ret); 15900e0f34cSAndy Grover goto out; 16000e0f34cSAndy Grover } 16100e0f34cSAndy Grover 16200e0f34cSAndy Grover /* 16300e0f34cSAndy Grover * This is where a connection becomes loopback. If *any* RDS sockets 16400e0f34cSAndy Grover * can bind to the destination address then we'd rather the messages 16500e0f34cSAndy Grover * flow through loopback rather than either transport. 16600e0f34cSAndy Grover */ 16700e0f34cSAndy Grover if (rds_trans_get_preferred(faddr)) { 16800e0f34cSAndy Grover conn->c_loopback = 1; 16900e0f34cSAndy Grover if (is_outgoing && trans->t_prefer_loopback) { 17000e0f34cSAndy Grover /* "outgoing" connection - and the transport 17100e0f34cSAndy Grover * says it wants the connection handled by the 17200e0f34cSAndy Grover * loopback transport. This is what TCP does. 17300e0f34cSAndy Grover */ 17400e0f34cSAndy Grover trans = &rds_loop_transport; 17500e0f34cSAndy Grover } 17600e0f34cSAndy Grover } 17700e0f34cSAndy Grover 17800e0f34cSAndy Grover conn->c_trans = trans; 17900e0f34cSAndy Grover 18000e0f34cSAndy Grover ret = trans->conn_alloc(conn, gfp); 18100e0f34cSAndy Grover if (ret) { 18200e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 18300e0f34cSAndy Grover conn = ERR_PTR(ret); 18400e0f34cSAndy Grover goto out; 18500e0f34cSAndy Grover } 18600e0f34cSAndy Grover 18700e0f34cSAndy Grover atomic_set(&conn->c_state, RDS_CONN_DOWN); 18800e0f34cSAndy Grover conn->c_reconnect_jiffies = 0; 18900e0f34cSAndy Grover INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker); 19000e0f34cSAndy Grover INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker); 19100e0f34cSAndy Grover INIT_DELAYED_WORK(&conn->c_conn_w, rds_connect_worker); 19200e0f34cSAndy Grover INIT_WORK(&conn->c_down_w, rds_shutdown_worker); 19300e0f34cSAndy Grover mutex_init(&conn->c_cm_lock); 19400e0f34cSAndy Grover conn->c_flags = 0; 19500e0f34cSAndy Grover 19600e0f34cSAndy Grover rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n", 19700e0f34cSAndy Grover conn, &laddr, &faddr, 19800e0f34cSAndy Grover trans->t_name ? trans->t_name : "[unknown]", 19900e0f34cSAndy Grover is_outgoing ? "(outgoing)" : ""); 20000e0f34cSAndy Grover 201cb24405eSAndy Grover /* 202cb24405eSAndy Grover * Since we ran without holding the conn lock, someone could 203cb24405eSAndy Grover * have created the same conn (either normal or passive) in the 204cb24405eSAndy Grover * interim. We check while holding the lock. If we won, we complete 205cb24405eSAndy Grover * init and return our conn. If we lost, we rollback and return the 206cb24405eSAndy Grover * other one. 207cb24405eSAndy Grover */ 20800e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 209cb24405eSAndy Grover if (parent) { 210cb24405eSAndy Grover /* Creating passive conn */ 211cb24405eSAndy Grover if (parent->c_passive) { 21200e0f34cSAndy Grover trans->conn_free(conn->c_transport_data); 21300e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 214cb24405eSAndy Grover conn = parent->c_passive; 21500e0f34cSAndy Grover } else { 216cb24405eSAndy Grover parent->c_passive = conn; 21700e0f34cSAndy Grover rds_cong_add_conn(conn); 21800e0f34cSAndy Grover rds_conn_count++; 21900e0f34cSAndy Grover } 220cb24405eSAndy Grover } else { 221cb24405eSAndy Grover /* Creating normal conn */ 222cb24405eSAndy Grover struct rds_connection *found; 22300e0f34cSAndy Grover 224cb24405eSAndy Grover found = rds_conn_lookup(head, laddr, faddr, trans); 225cb24405eSAndy Grover if (found) { 226cb24405eSAndy Grover trans->conn_free(conn->c_transport_data); 227cb24405eSAndy Grover kmem_cache_free(rds_conn_slab, conn); 228cb24405eSAndy Grover conn = found; 229cb24405eSAndy Grover } else { 230cb24405eSAndy Grover hlist_add_head(&conn->c_hash_node, head); 231cb24405eSAndy Grover rds_cong_add_conn(conn); 232cb24405eSAndy Grover rds_conn_count++; 233cb24405eSAndy Grover } 234cb24405eSAndy Grover } 23500e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 23600e0f34cSAndy Grover 23700e0f34cSAndy Grover out: 23800e0f34cSAndy Grover return conn; 23900e0f34cSAndy Grover } 24000e0f34cSAndy Grover 24100e0f34cSAndy Grover struct rds_connection *rds_conn_create(__be32 laddr, __be32 faddr, 24200e0f34cSAndy Grover struct rds_transport *trans, gfp_t gfp) 24300e0f34cSAndy Grover { 24400e0f34cSAndy Grover return __rds_conn_create(laddr, faddr, trans, gfp, 0); 24500e0f34cSAndy Grover } 246616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create); 24700e0f34cSAndy Grover 24800e0f34cSAndy Grover struct rds_connection *rds_conn_create_outgoing(__be32 laddr, __be32 faddr, 24900e0f34cSAndy Grover struct rds_transport *trans, gfp_t gfp) 25000e0f34cSAndy Grover { 25100e0f34cSAndy Grover return __rds_conn_create(laddr, faddr, trans, gfp, 1); 25200e0f34cSAndy Grover } 253616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_create_outgoing); 25400e0f34cSAndy Grover 2552dc39357SAndy Grover void rds_conn_shutdown(struct rds_connection *conn) 2562dc39357SAndy Grover { 2572dc39357SAndy Grover /* shut it down unless it's down already */ 2582dc39357SAndy Grover if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) { 2592dc39357SAndy Grover /* 2602dc39357SAndy Grover * Quiesce the connection mgmt handlers before we start tearing 2612dc39357SAndy Grover * things down. We don't hold the mutex for the entire 2622dc39357SAndy Grover * duration of the shutdown operation, else we may be 2632dc39357SAndy Grover * deadlocking with the CM handler. Instead, the CM event 2642dc39357SAndy Grover * handler is supposed to check for state DISCONNECTING 2652dc39357SAndy Grover */ 2662dc39357SAndy Grover mutex_lock(&conn->c_cm_lock); 2672dc39357SAndy Grover if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING) 2682dc39357SAndy Grover && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) { 2692dc39357SAndy Grover rds_conn_error(conn, "shutdown called in state %d\n", 2702dc39357SAndy Grover atomic_read(&conn->c_state)); 2712dc39357SAndy Grover mutex_unlock(&conn->c_cm_lock); 2722dc39357SAndy Grover return; 2732dc39357SAndy Grover } 2742dc39357SAndy Grover mutex_unlock(&conn->c_cm_lock); 2752dc39357SAndy Grover 276049ee3f5SAndy Grover /* verify everybody's out of rds_send_xmit() */ 277049ee3f5SAndy Grover spin_lock_irq(&conn->c_send_lock); 278049ee3f5SAndy Grover spin_unlock_irq(&conn->c_send_lock); 279049ee3f5SAndy Grover 2807e3f2952SChris Mason while(atomic_read(&conn->c_senders)) { 2817e3f2952SChris Mason schedule_timeout(1); 2827e3f2952SChris Mason spin_lock_irq(&conn->c_send_lock); 2837e3f2952SChris Mason spin_unlock_irq(&conn->c_send_lock); 2847e3f2952SChris Mason } 2857e3f2952SChris Mason 2862dc39357SAndy Grover conn->c_trans->conn_shutdown(conn); 2872dc39357SAndy Grover rds_conn_reset(conn); 2882dc39357SAndy Grover 2892dc39357SAndy Grover if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) { 2902dc39357SAndy Grover /* This can happen - eg when we're in the middle of tearing 2912dc39357SAndy Grover * down the connection, and someone unloads the rds module. 2922dc39357SAndy Grover * Quite reproduceable with loopback connections. 2932dc39357SAndy Grover * Mostly harmless. 2942dc39357SAndy Grover */ 2952dc39357SAndy Grover rds_conn_error(conn, 2962dc39357SAndy Grover "%s: failed to transition to state DOWN, " 2972dc39357SAndy Grover "current state is %d\n", 2982dc39357SAndy Grover __func__, 2992dc39357SAndy Grover atomic_read(&conn->c_state)); 3002dc39357SAndy Grover return; 3012dc39357SAndy Grover } 3022dc39357SAndy Grover } 3032dc39357SAndy Grover 3042dc39357SAndy Grover /* Then reconnect if it's still live. 3052dc39357SAndy Grover * The passive side of an IB loopback connection is never added 3062dc39357SAndy Grover * to the conn hash, so we never trigger a reconnect on this 3072dc39357SAndy Grover * conn - the reconnect is always triggered by the active peer. */ 3082dc39357SAndy Grover cancel_delayed_work_sync(&conn->c_conn_w); 3092dc39357SAndy Grover if (!hlist_unhashed(&conn->c_hash_node)) 3102dc39357SAndy Grover rds_queue_reconnect(conn); 3112dc39357SAndy Grover } 3122dc39357SAndy Grover 3132dc39357SAndy Grover /* 3142dc39357SAndy Grover * Stop and free a connection. 3152dc39357SAndy Grover */ 31600e0f34cSAndy Grover void rds_conn_destroy(struct rds_connection *conn) 31700e0f34cSAndy Grover { 31800e0f34cSAndy Grover struct rds_message *rm, *rtmp; 31900e0f34cSAndy Grover 32000e0f34cSAndy Grover rdsdebug("freeing conn %p for %pI4 -> " 32100e0f34cSAndy Grover "%pI4\n", conn, &conn->c_laddr, 32200e0f34cSAndy Grover &conn->c_faddr); 32300e0f34cSAndy Grover 324*abf45439SChris Mason /* Ensure conn will not be scheduled for reconnect */ 325*abf45439SChris Mason spin_lock_irq(&rds_conn_lock); 32600e0f34cSAndy Grover hlist_del_init(&conn->c_hash_node); 327*abf45439SChris Mason spin_unlock_irq(&rds_conn_lock); 32800e0f34cSAndy Grover 32900e0f34cSAndy Grover /* wait for the rds thread to shut it down */ 33000e0f34cSAndy Grover atomic_set(&conn->c_state, RDS_CONN_ERROR); 33100e0f34cSAndy Grover cancel_delayed_work(&conn->c_conn_w); 33200e0f34cSAndy Grover queue_work(rds_wq, &conn->c_down_w); 33300e0f34cSAndy Grover flush_workqueue(rds_wq); 33400e0f34cSAndy Grover 33500e0f34cSAndy Grover /* tear down queued messages */ 33600e0f34cSAndy Grover list_for_each_entry_safe(rm, rtmp, 33700e0f34cSAndy Grover &conn->c_send_queue, 33800e0f34cSAndy Grover m_conn_item) { 33900e0f34cSAndy Grover list_del_init(&rm->m_conn_item); 34000e0f34cSAndy Grover BUG_ON(!list_empty(&rm->m_sock_item)); 34100e0f34cSAndy Grover rds_message_put(rm); 34200e0f34cSAndy Grover } 34300e0f34cSAndy Grover if (conn->c_xmit_rm) 34400e0f34cSAndy Grover rds_message_put(conn->c_xmit_rm); 34500e0f34cSAndy Grover 34600e0f34cSAndy Grover conn->c_trans->conn_free(conn->c_transport_data); 34700e0f34cSAndy Grover 34800e0f34cSAndy Grover /* 34900e0f34cSAndy Grover * The congestion maps aren't freed up here. They're 35000e0f34cSAndy Grover * freed by rds_cong_exit() after all the connections 35100e0f34cSAndy Grover * have been freed. 35200e0f34cSAndy Grover */ 35300e0f34cSAndy Grover rds_cong_remove_conn(conn); 35400e0f34cSAndy Grover 35500e0f34cSAndy Grover BUG_ON(!list_empty(&conn->c_retrans)); 35600e0f34cSAndy Grover kmem_cache_free(rds_conn_slab, conn); 35700e0f34cSAndy Grover 35800e0f34cSAndy Grover rds_conn_count--; 35900e0f34cSAndy Grover } 360616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_destroy); 36100e0f34cSAndy Grover 36200e0f34cSAndy Grover static void rds_conn_message_info(struct socket *sock, unsigned int len, 36300e0f34cSAndy Grover struct rds_info_iterator *iter, 36400e0f34cSAndy Grover struct rds_info_lengths *lens, 36500e0f34cSAndy Grover int want_send) 36600e0f34cSAndy Grover { 36700e0f34cSAndy Grover struct hlist_head *head; 36800e0f34cSAndy Grover struct hlist_node *pos; 36900e0f34cSAndy Grover struct list_head *list; 37000e0f34cSAndy Grover struct rds_connection *conn; 37100e0f34cSAndy Grover struct rds_message *rm; 37200e0f34cSAndy Grover unsigned long flags; 37300e0f34cSAndy Grover unsigned int total = 0; 37400e0f34cSAndy Grover size_t i; 37500e0f34cSAndy Grover 37600e0f34cSAndy Grover len /= sizeof(struct rds_info_message); 37700e0f34cSAndy Grover 37800e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 37900e0f34cSAndy Grover 38000e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 38100e0f34cSAndy Grover i++, head++) { 38200e0f34cSAndy Grover hlist_for_each_entry(conn, pos, head, c_hash_node) { 38300e0f34cSAndy Grover if (want_send) 38400e0f34cSAndy Grover list = &conn->c_send_queue; 38500e0f34cSAndy Grover else 38600e0f34cSAndy Grover list = &conn->c_retrans; 38700e0f34cSAndy Grover 38800e0f34cSAndy Grover spin_lock(&conn->c_lock); 38900e0f34cSAndy Grover 39000e0f34cSAndy Grover /* XXX too lazy to maintain counts.. */ 39100e0f34cSAndy Grover list_for_each_entry(rm, list, m_conn_item) { 39200e0f34cSAndy Grover total++; 39300e0f34cSAndy Grover if (total <= len) 39400e0f34cSAndy Grover rds_inc_info_copy(&rm->m_inc, iter, 39500e0f34cSAndy Grover conn->c_laddr, 39600e0f34cSAndy Grover conn->c_faddr, 0); 39700e0f34cSAndy Grover } 39800e0f34cSAndy Grover 39900e0f34cSAndy Grover spin_unlock(&conn->c_lock); 40000e0f34cSAndy Grover } 40100e0f34cSAndy Grover } 40200e0f34cSAndy Grover 40300e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 40400e0f34cSAndy Grover 40500e0f34cSAndy Grover lens->nr = total; 40600e0f34cSAndy Grover lens->each = sizeof(struct rds_info_message); 40700e0f34cSAndy Grover } 40800e0f34cSAndy Grover 40900e0f34cSAndy Grover static void rds_conn_message_info_send(struct socket *sock, unsigned int len, 41000e0f34cSAndy Grover struct rds_info_iterator *iter, 41100e0f34cSAndy Grover struct rds_info_lengths *lens) 41200e0f34cSAndy Grover { 41300e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 1); 41400e0f34cSAndy Grover } 41500e0f34cSAndy Grover 41600e0f34cSAndy Grover static void rds_conn_message_info_retrans(struct socket *sock, 41700e0f34cSAndy Grover unsigned int len, 41800e0f34cSAndy Grover struct rds_info_iterator *iter, 41900e0f34cSAndy Grover struct rds_info_lengths *lens) 42000e0f34cSAndy Grover { 42100e0f34cSAndy Grover rds_conn_message_info(sock, len, iter, lens, 0); 42200e0f34cSAndy Grover } 42300e0f34cSAndy Grover 42400e0f34cSAndy Grover void rds_for_each_conn_info(struct socket *sock, unsigned int len, 42500e0f34cSAndy Grover struct rds_info_iterator *iter, 42600e0f34cSAndy Grover struct rds_info_lengths *lens, 42700e0f34cSAndy Grover int (*visitor)(struct rds_connection *, void *), 42800e0f34cSAndy Grover size_t item_len) 42900e0f34cSAndy Grover { 43000e0f34cSAndy Grover uint64_t buffer[(item_len + 7) / 8]; 43100e0f34cSAndy Grover struct hlist_head *head; 43200e0f34cSAndy Grover struct hlist_node *pos; 43300e0f34cSAndy Grover struct hlist_node *tmp; 43400e0f34cSAndy Grover struct rds_connection *conn; 43500e0f34cSAndy Grover unsigned long flags; 43600e0f34cSAndy Grover size_t i; 43700e0f34cSAndy Grover 43800e0f34cSAndy Grover spin_lock_irqsave(&rds_conn_lock, flags); 43900e0f34cSAndy Grover 44000e0f34cSAndy Grover lens->nr = 0; 44100e0f34cSAndy Grover lens->each = item_len; 44200e0f34cSAndy Grover 44300e0f34cSAndy Grover for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash); 44400e0f34cSAndy Grover i++, head++) { 44500e0f34cSAndy Grover hlist_for_each_entry_safe(conn, pos, tmp, head, c_hash_node) { 44600e0f34cSAndy Grover 44700e0f34cSAndy Grover /* XXX no c_lock usage.. */ 44800e0f34cSAndy Grover if (!visitor(conn, buffer)) 44900e0f34cSAndy Grover continue; 45000e0f34cSAndy Grover 45100e0f34cSAndy Grover /* We copy as much as we can fit in the buffer, 45200e0f34cSAndy Grover * but we count all items so that the caller 45300e0f34cSAndy Grover * can resize the buffer. */ 45400e0f34cSAndy Grover if (len >= item_len) { 45500e0f34cSAndy Grover rds_info_copy(iter, buffer, item_len); 45600e0f34cSAndy Grover len -= item_len; 45700e0f34cSAndy Grover } 45800e0f34cSAndy Grover lens->nr++; 45900e0f34cSAndy Grover } 46000e0f34cSAndy Grover } 46100e0f34cSAndy Grover 46200e0f34cSAndy Grover spin_unlock_irqrestore(&rds_conn_lock, flags); 46300e0f34cSAndy Grover } 464616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_for_each_conn_info); 46500e0f34cSAndy Grover 46600e0f34cSAndy Grover static int rds_conn_info_visitor(struct rds_connection *conn, 46700e0f34cSAndy Grover void *buffer) 46800e0f34cSAndy Grover { 46900e0f34cSAndy Grover struct rds_info_connection *cinfo = buffer; 47000e0f34cSAndy Grover 47100e0f34cSAndy Grover cinfo->next_tx_seq = conn->c_next_tx_seq; 47200e0f34cSAndy Grover cinfo->next_rx_seq = conn->c_next_rx_seq; 47300e0f34cSAndy Grover cinfo->laddr = conn->c_laddr; 47400e0f34cSAndy Grover cinfo->faddr = conn->c_faddr; 47500e0f34cSAndy Grover strncpy(cinfo->transport, conn->c_trans->t_name, 47600e0f34cSAndy Grover sizeof(cinfo->transport)); 47700e0f34cSAndy Grover cinfo->flags = 0; 47800e0f34cSAndy Grover 47900e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 480049ee3f5SAndy Grover spin_is_locked(&conn->c_send_lock), SENDING); 48100e0f34cSAndy Grover /* XXX Future: return the state rather than these funky bits */ 48200e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 48300e0f34cSAndy Grover atomic_read(&conn->c_state) == RDS_CONN_CONNECTING, 48400e0f34cSAndy Grover CONNECTING); 48500e0f34cSAndy Grover rds_conn_info_set(cinfo->flags, 48600e0f34cSAndy Grover atomic_read(&conn->c_state) == RDS_CONN_UP, 48700e0f34cSAndy Grover CONNECTED); 48800e0f34cSAndy Grover return 1; 48900e0f34cSAndy Grover } 49000e0f34cSAndy Grover 49100e0f34cSAndy Grover static void rds_conn_info(struct socket *sock, unsigned int len, 49200e0f34cSAndy Grover struct rds_info_iterator *iter, 49300e0f34cSAndy Grover struct rds_info_lengths *lens) 49400e0f34cSAndy Grover { 49500e0f34cSAndy Grover rds_for_each_conn_info(sock, len, iter, lens, 49600e0f34cSAndy Grover rds_conn_info_visitor, 49700e0f34cSAndy Grover sizeof(struct rds_info_connection)); 49800e0f34cSAndy Grover } 49900e0f34cSAndy Grover 50000e0f34cSAndy Grover int __init rds_conn_init(void) 50100e0f34cSAndy Grover { 50200e0f34cSAndy Grover rds_conn_slab = kmem_cache_create("rds_connection", 50300e0f34cSAndy Grover sizeof(struct rds_connection), 50400e0f34cSAndy Grover 0, 0, NULL); 5058690bfa1SAndy Grover if (!rds_conn_slab) 50600e0f34cSAndy Grover return -ENOMEM; 50700e0f34cSAndy Grover 50800e0f34cSAndy Grover rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info); 50900e0f34cSAndy Grover rds_info_register_func(RDS_INFO_SEND_MESSAGES, 51000e0f34cSAndy Grover rds_conn_message_info_send); 51100e0f34cSAndy Grover rds_info_register_func(RDS_INFO_RETRANS_MESSAGES, 51200e0f34cSAndy Grover rds_conn_message_info_retrans); 51300e0f34cSAndy Grover 51400e0f34cSAndy Grover return 0; 51500e0f34cSAndy Grover } 51600e0f34cSAndy Grover 51700e0f34cSAndy Grover void rds_conn_exit(void) 51800e0f34cSAndy Grover { 51900e0f34cSAndy Grover rds_loop_exit(); 52000e0f34cSAndy Grover 52100e0f34cSAndy Grover WARN_ON(!hlist_empty(rds_conn_hash)); 52200e0f34cSAndy Grover 52300e0f34cSAndy Grover kmem_cache_destroy(rds_conn_slab); 52400e0f34cSAndy Grover 52500e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info); 52600e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_SEND_MESSAGES, 52700e0f34cSAndy Grover rds_conn_message_info_send); 52800e0f34cSAndy Grover rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES, 52900e0f34cSAndy Grover rds_conn_message_info_retrans); 53000e0f34cSAndy Grover } 53100e0f34cSAndy Grover 53200e0f34cSAndy Grover /* 53300e0f34cSAndy Grover * Force a disconnect 53400e0f34cSAndy Grover */ 53500e0f34cSAndy Grover void rds_conn_drop(struct rds_connection *conn) 53600e0f34cSAndy Grover { 53700e0f34cSAndy Grover atomic_set(&conn->c_state, RDS_CONN_ERROR); 53800e0f34cSAndy Grover queue_work(rds_wq, &conn->c_down_w); 53900e0f34cSAndy Grover } 540616b757aSAndy Grover EXPORT_SYMBOL_GPL(rds_conn_drop); 54100e0f34cSAndy Grover 54200e0f34cSAndy Grover /* 54300e0f34cSAndy Grover * An error occurred on the connection 54400e0f34cSAndy Grover */ 54500e0f34cSAndy Grover void 54600e0f34cSAndy Grover __rds_conn_error(struct rds_connection *conn, const char *fmt, ...) 54700e0f34cSAndy Grover { 54800e0f34cSAndy Grover va_list ap; 54900e0f34cSAndy Grover 55000e0f34cSAndy Grover va_start(ap, fmt); 55100e0f34cSAndy Grover vprintk(fmt, ap); 55200e0f34cSAndy Grover va_end(ap); 55300e0f34cSAndy Grover 55400e0f34cSAndy Grover rds_conn_drop(conn); 55500e0f34cSAndy Grover } 556