blob: a99ac69f77acc806d8de714516f30757d84856d1 [file] [log] [blame]
Andy Grover00e0f342009-02-24 15:30:23 +00001/*
2 * Copyright (c) 2006 Oracle. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
34#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040036#include <linux/export.h>
Andy Grover00e0f342009-02-24 15:30:23 +000037#include <net/inet_hashtables.h>
38
Sowmini Varadhan0cb43962016-06-13 09:44:26 -070039#include "rds_single_path.h"
Andy Grover00e0f342009-02-24 15:30:23 +000040#include "rds.h"
41#include "loop.h"
Andy Grover00e0f342009-02-24 15:30:23 +000042
43#define RDS_CONNECTION_HASH_BITS 12
44#define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
45#define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
46
47/* converting this to RCU is a chore for another day.. */
48static DEFINE_SPINLOCK(rds_conn_lock);
49static unsigned long rds_conn_count;
50static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
51static struct kmem_cache *rds_conn_slab;
52
53static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
54{
Hannes Frederic Sowa1bbdcee2013-10-19 21:48:57 +020055 static u32 rds_hash_secret __read_mostly;
56
57 unsigned long hash;
58
59 net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret));
60
Andy Grover00e0f342009-02-24 15:30:23 +000061 /* Pass NULL, don't need struct net for hash */
Hannes Frederic Sowa1bbdcee2013-10-19 21:48:57 +020062 hash = __inet_ehashfn(be32_to_cpu(laddr), 0,
63 be32_to_cpu(faddr), 0,
64 rds_hash_secret);
Andy Grover00e0f342009-02-24 15:30:23 +000065 return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
66}
67
68#define rds_conn_info_set(var, test, suffix) do { \
69 if (test) \
70 var |= RDS_INFO_CONNECTION_FLAG_##suffix; \
71} while (0)
72
Chris Masonbcf50ef2010-05-11 15:15:15 -070073/* rcu read lock must be held or the connection spinlock */
Sowmini Varadhan8f384c02015-09-03 16:24:52 -040074static struct rds_connection *rds_conn_lookup(struct net *net,
75 struct hlist_head *head,
Andy Grover00e0f342009-02-24 15:30:23 +000076 __be32 laddr, __be32 faddr,
77 struct rds_transport *trans)
78{
79 struct rds_connection *conn, *ret = NULL;
Andy Grover00e0f342009-02-24 15:30:23 +000080
Sasha Levinb67bfe02013-02-27 17:06:00 -080081 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
Andy Grover00e0f342009-02-24 15:30:23 +000082 if (conn->c_faddr == faddr && conn->c_laddr == laddr &&
Sowmini Varadhan8f384c02015-09-03 16:24:52 -040083 conn->c_trans == trans && net == rds_conn_net(conn)) {
Andy Grover00e0f342009-02-24 15:30:23 +000084 ret = conn;
85 break;
86 }
87 }
88 rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret,
89 &laddr, &faddr);
90 return ret;
91}
92
93/*
94 * This is called by transports as they're bringing down a connection.
95 * It clears partial message state so that the transport can start sending
96 * and receiving over this connection again in the future. It is up to
97 * the transport to have serialized this call with its send and recv.
98 */
stephen hemmingerff51bf82010-10-19 08:08:33 +000099static void rds_conn_reset(struct rds_connection *conn)
Andy Grover00e0f342009-02-24 15:30:23 +0000100{
101 rdsdebug("connection %pI4 to %pI4 reset\n",
102 &conn->c_laddr, &conn->c_faddr);
103
104 rds_stats_inc(s_conn_reset);
105 rds_send_reset(conn);
106 conn->c_flags = 0;
107
108 /* Do not clear next_rx_seq here, else we cannot distinguish
109 * retransmitted packets from new packets, and will hand all
110 * of them to the application. That is not consistent with the
111 * reliability guarantees of RDS. */
112}
113
Sowmini Varadhan1c5113c2016-06-13 09:44:40 -0700114static void __rds_conn_path_init(struct rds_connection *conn,
115 struct rds_conn_path *cp, bool is_outgoing)
116{
117 spin_lock_init(&cp->cp_lock);
118 cp->cp_next_tx_seq = 1;
119 init_waitqueue_head(&cp->cp_waitq);
120 INIT_LIST_HEAD(&cp->cp_send_queue);
121 INIT_LIST_HEAD(&cp->cp_retrans);
122
123 cp->cp_conn = conn;
124 atomic_set(&cp->cp_state, RDS_CONN_DOWN);
125 cp->cp_send_gen = 0;
126 /* cp_outgoing is per-path. So we can only set it here
127 * for the single-path transports.
128 */
129 if (!conn->c_trans->t_mp_capable)
130 cp->cp_outgoing = (is_outgoing ? 1 : 0);
131 cp->cp_reconnect_jiffies = 0;
132 INIT_DELAYED_WORK(&cp->cp_send_w, rds_send_worker);
133 INIT_DELAYED_WORK(&cp->cp_recv_w, rds_recv_worker);
134 INIT_DELAYED_WORK(&cp->cp_conn_w, rds_connect_worker);
135 INIT_WORK(&cp->cp_down_w, rds_shutdown_worker);
136 mutex_init(&cp->cp_cm_lock);
137 cp->cp_flags = 0;
138}
139
Andy Grover00e0f342009-02-24 15:30:23 +0000140/*
141 * There is only every one 'conn' for a given pair of addresses in the
142 * system at a time. They contain messages to be retransmitted and so
143 * span the lifetime of the actual underlying transport connections.
144 *
145 * For now they are not garbage collected once they're created. They
146 * are torn down as the module is removed, if ever.
147 */
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400148static struct rds_connection *__rds_conn_create(struct net *net,
149 __be32 laddr, __be32 faddr,
Andy Grover00e0f342009-02-24 15:30:23 +0000150 struct rds_transport *trans, gfp_t gfp,
151 int is_outgoing)
152{
Andy Grovercb244052009-07-17 13:13:36 +0000153 struct rds_connection *conn, *parent = NULL;
Andy Grover00e0f342009-02-24 15:30:23 +0000154 struct hlist_head *head = rds_conn_bucket(laddr, faddr);
Zach Brown5adb5bc2010-07-23 10:32:31 -0700155 struct rds_transport *loop_trans;
Andy Grover00e0f342009-02-24 15:30:23 +0000156 unsigned long flags;
157 int ret;
158
Chris Masonbcf50ef2010-05-11 15:15:15 -0700159 rcu_read_lock();
Sowmini Varadhan8f384c02015-09-03 16:24:52 -0400160 conn = rds_conn_lookup(net, head, laddr, faddr, trans);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800161 if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
Sowmini Varadhan1789b2c072015-04-08 12:33:46 -0400162 laddr == faddr && !is_outgoing) {
Andy Grover00e0f342009-02-24 15:30:23 +0000163 /* This is a looped back IB connection, and we're
164 * called by the code handling the incoming connect.
165 * We need a second connection object into which we
166 * can stick the other QP. */
167 parent = conn;
168 conn = parent->c_passive;
169 }
Chris Masonbcf50ef2010-05-11 15:15:15 -0700170 rcu_read_unlock();
Andy Grover00e0f342009-02-24 15:30:23 +0000171 if (conn)
172 goto out;
173
Wei Yongjun05a178e2009-04-09 14:09:44 +0000174 conn = kmem_cache_zalloc(rds_conn_slab, gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -0800175 if (!conn) {
Andy Grover00e0f342009-02-24 15:30:23 +0000176 conn = ERR_PTR(-ENOMEM);
177 goto out;
178 }
179
Andy Grover00e0f342009-02-24 15:30:23 +0000180 INIT_HLIST_NODE(&conn->c_hash_node);
Andy Grover00e0f342009-02-24 15:30:23 +0000181 conn->c_laddr = laddr;
182 conn->c_faddr = faddr;
Andy Grover00e0f342009-02-24 15:30:23 +0000183
Sowmini Varadhan1c5113c2016-06-13 09:44:40 -0700184 rds_conn_net_set(conn, net);
Andy Grover00e0f342009-02-24 15:30:23 +0000185
186 ret = rds_cong_get_maps(conn);
187 if (ret) {
188 kmem_cache_free(rds_conn_slab, conn);
189 conn = ERR_PTR(ret);
190 goto out;
191 }
192
193 /*
194 * This is where a connection becomes loopback. If *any* RDS sockets
195 * can bind to the destination address then we'd rather the messages
196 * flow through loopback rather than either transport.
197 */
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400198 loop_trans = rds_trans_get_preferred(net, faddr);
Zach Brown5adb5bc2010-07-23 10:32:31 -0700199 if (loop_trans) {
200 rds_trans_put(loop_trans);
Andy Grover00e0f342009-02-24 15:30:23 +0000201 conn->c_loopback = 1;
202 if (is_outgoing && trans->t_prefer_loopback) {
203 /* "outgoing" connection - and the transport
204 * says it wants the connection handled by the
205 * loopback transport. This is what TCP does.
206 */
207 trans = &rds_loop_transport;
208 }
209 }
210
211 conn->c_trans = trans;
212
213 ret = trans->conn_alloc(conn, gfp);
214 if (ret) {
215 kmem_cache_free(rds_conn_slab, conn);
216 conn = ERR_PTR(ret);
217 goto out;
218 }
219
Andy Grover00e0f342009-02-24 15:30:23 +0000220 rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
221 conn, &laddr, &faddr,
222 trans->t_name ? trans->t_name : "[unknown]",
223 is_outgoing ? "(outgoing)" : "");
224
Andy Grovercb244052009-07-17 13:13:36 +0000225 /*
226 * Since we ran without holding the conn lock, someone could
227 * have created the same conn (either normal or passive) in the
228 * interim. We check while holding the lock. If we won, we complete
229 * init and return our conn. If we lost, we rollback and return the
230 * other one.
231 */
Andy Grover00e0f342009-02-24 15:30:23 +0000232 spin_lock_irqsave(&rds_conn_lock, flags);
Andy Grovercb244052009-07-17 13:13:36 +0000233 if (parent) {
234 /* Creating passive conn */
235 if (parent->c_passive) {
Sowmini Varadhan1c5113c2016-06-13 09:44:40 -0700236 trans->conn_free(conn->c_path[0].cp_transport_data);
Andy Grovercb244052009-07-17 13:13:36 +0000237 kmem_cache_free(rds_conn_slab, conn);
238 conn = parent->c_passive;
239 } else {
Andy Grover00e0f342009-02-24 15:30:23 +0000240 parent->c_passive = conn;
Andy Grovercb244052009-07-17 13:13:36 +0000241 rds_cong_add_conn(conn);
242 rds_conn_count++;
243 }
Andy Grover00e0f342009-02-24 15:30:23 +0000244 } else {
Andy Grovercb244052009-07-17 13:13:36 +0000245 /* Creating normal conn */
246 struct rds_connection *found;
Andy Grover00e0f342009-02-24 15:30:23 +0000247
Sowmini Varadhan3b20fc32015-09-30 16:54:07 -0400248 found = rds_conn_lookup(net, head, laddr, faddr, trans);
Andy Grovercb244052009-07-17 13:13:36 +0000249 if (found) {
Sowmini Varadhan1c5113c2016-06-13 09:44:40 -0700250 struct rds_conn_path *cp;
251 int i;
252
253 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
254 cp = &conn->c_path[i];
255 trans->conn_free(cp->cp_transport_data);
256 if (!trans->t_mp_capable)
257 break;
258 }
Andy Grovercb244052009-07-17 13:13:36 +0000259 kmem_cache_free(rds_conn_slab, conn);
260 conn = found;
261 } else {
Sowmini Varadhan1c5113c2016-06-13 09:44:40 -0700262 int i;
263
264 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
265 __rds_conn_path_init(conn, &conn->c_path[i],
266 is_outgoing);
267 conn->c_path[i].cp_index = i;
268 }
269
Sowmini Varadhan3b20fc32015-09-30 16:54:07 -0400270 hlist_add_head_rcu(&conn->c_hash_node, head);
Andy Grovercb244052009-07-17 13:13:36 +0000271 rds_cong_add_conn(conn);
272 rds_conn_count++;
273 }
274 }
Andy Grover00e0f342009-02-24 15:30:23 +0000275 spin_unlock_irqrestore(&rds_conn_lock, flags);
276
277out:
278 return conn;
279}
280
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400281struct rds_connection *rds_conn_create(struct net *net,
282 __be32 laddr, __be32 faddr,
Andy Grover00e0f342009-02-24 15:30:23 +0000283 struct rds_transport *trans, gfp_t gfp)
284{
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400285 return __rds_conn_create(net, laddr, faddr, trans, gfp, 0);
Andy Grover00e0f342009-02-24 15:30:23 +0000286}
Andy Grover616b7572009-08-21 12:28:32 +0000287EXPORT_SYMBOL_GPL(rds_conn_create);
Andy Grover00e0f342009-02-24 15:30:23 +0000288
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400289struct rds_connection *rds_conn_create_outgoing(struct net *net,
290 __be32 laddr, __be32 faddr,
Andy Grover00e0f342009-02-24 15:30:23 +0000291 struct rds_transport *trans, gfp_t gfp)
292{
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400293 return __rds_conn_create(net, laddr, faddr, trans, gfp, 1);
Andy Grover00e0f342009-02-24 15:30:23 +0000294}
Andy Grover616b7572009-08-21 12:28:32 +0000295EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
Andy Grover00e0f342009-02-24 15:30:23 +0000296
Andy Grover2dc39352010-06-11 13:49:13 -0700297void rds_conn_shutdown(struct rds_connection *conn)
298{
299 /* shut it down unless it's down already */
300 if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
301 /*
302 * Quiesce the connection mgmt handlers before we start tearing
303 * things down. We don't hold the mutex for the entire
304 * duration of the shutdown operation, else we may be
305 * deadlocking with the CM handler. Instead, the CM event
306 * handler is supposed to check for state DISCONNECTING
307 */
308 mutex_lock(&conn->c_cm_lock);
309 if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING)
310 && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) {
311 rds_conn_error(conn, "shutdown called in state %d\n",
312 atomic_read(&conn->c_state));
313 mutex_unlock(&conn->c_cm_lock);
314 return;
315 }
316 mutex_unlock(&conn->c_cm_lock);
317
Zach Brown0f4b1c72010-06-04 14:41:41 -0700318 wait_event(conn->c_waitq,
319 !test_bit(RDS_IN_XMIT, &conn->c_flags));
santosh.shilimkar@oracle.com73ce4312015-08-22 15:45:26 -0700320 wait_event(conn->c_waitq,
321 !test_bit(RDS_RECV_REFILL, &conn->c_flags));
Chris Mason7e3f2952010-05-11 15:11:11 -0700322
Andy Grover2dc39352010-06-11 13:49:13 -0700323 conn->c_trans->conn_shutdown(conn);
324 rds_conn_reset(conn);
Andy Grover2dc39352010-06-11 13:49:13 -0700325
326 if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) {
327 /* This can happen - eg when we're in the middle of tearing
328 * down the connection, and someone unloads the rds module.
329 * Quite reproduceable with loopback connections.
330 * Mostly harmless.
331 */
332 rds_conn_error(conn,
333 "%s: failed to transition to state DOWN, "
334 "current state is %d\n",
335 __func__,
336 atomic_read(&conn->c_state));
337 return;
338 }
339 }
340
341 /* Then reconnect if it's still live.
342 * The passive side of an IB loopback connection is never added
343 * to the conn hash, so we never trigger a reconnect on this
344 * conn - the reconnect is always triggered by the active peer. */
345 cancel_delayed_work_sync(&conn->c_conn_w);
Chris Masonbcf50ef2010-05-11 15:15:15 -0700346 rcu_read_lock();
347 if (!hlist_unhashed(&conn->c_hash_node)) {
348 rcu_read_unlock();
Sowmini Varadhan3b20fc32015-09-30 16:54:07 -0400349 if (conn->c_trans->t_type != RDS_TRANS_TCP ||
Sowmini Varadhan0cb43962016-06-13 09:44:26 -0700350 conn->c_path[0].cp_outgoing == 1)
351 rds_queue_reconnect(&conn->c_path[0]);
Chris Masonbcf50ef2010-05-11 15:15:15 -0700352 } else {
353 rcu_read_unlock();
354 }
Andy Grover2dc39352010-06-11 13:49:13 -0700355}
356
357/*
358 * Stop and free a connection.
Zach Brownffcec0e2010-07-23 10:36:58 -0700359 *
360 * This can only be used in very limited circumstances. It assumes that once
361 * the conn has been shutdown that no one else is referencing the connection.
362 * We can only ensure this in the rmmod path in the current code.
Andy Grover2dc39352010-06-11 13:49:13 -0700363 */
Andy Grover00e0f342009-02-24 15:30:23 +0000364void rds_conn_destroy(struct rds_connection *conn)
365{
366 struct rds_message *rm, *rtmp;
Zach Brownfe8ff6b2010-07-23 10:30:45 -0700367 unsigned long flags;
Andy Grover00e0f342009-02-24 15:30:23 +0000368
369 rdsdebug("freeing conn %p for %pI4 -> "
370 "%pI4\n", conn, &conn->c_laddr,
371 &conn->c_faddr);
372
Chris Masonabf45432010-05-11 15:14:52 -0700373 /* Ensure conn will not be scheduled for reconnect */
374 spin_lock_irq(&rds_conn_lock);
Chris Masonbcf50ef2010-05-11 15:15:15 -0700375 hlist_del_init_rcu(&conn->c_hash_node);
Chris Masonabf45432010-05-11 15:14:52 -0700376 spin_unlock_irq(&rds_conn_lock);
Chris Masonbcf50ef2010-05-11 15:15:15 -0700377 synchronize_rcu();
378
Zach Brownffcec0e2010-07-23 10:36:58 -0700379 /* shut the connection down */
380 rds_conn_drop(conn);
381 flush_work(&conn->c_down_w);
Andy Grover00e0f342009-02-24 15:30:23 +0000382
Zach Brown45180712010-07-23 10:37:33 -0700383 /* make sure lingering queued work won't try to ref the conn */
384 cancel_delayed_work_sync(&conn->c_send_w);
385 cancel_delayed_work_sync(&conn->c_recv_w);
386
Andy Grover00e0f342009-02-24 15:30:23 +0000387 /* tear down queued messages */
388 list_for_each_entry_safe(rm, rtmp,
389 &conn->c_send_queue,
390 m_conn_item) {
391 list_del_init(&rm->m_conn_item);
392 BUG_ON(!list_empty(&rm->m_sock_item));
393 rds_message_put(rm);
394 }
395 if (conn->c_xmit_rm)
396 rds_message_put(conn->c_xmit_rm);
397
398 conn->c_trans->conn_free(conn->c_transport_data);
399
400 /*
401 * The congestion maps aren't freed up here. They're
402 * freed by rds_cong_exit() after all the connections
403 * have been freed.
404 */
405 rds_cong_remove_conn(conn);
406
407 BUG_ON(!list_empty(&conn->c_retrans));
408 kmem_cache_free(rds_conn_slab, conn);
409
Zach Brownfe8ff6b2010-07-23 10:30:45 -0700410 spin_lock_irqsave(&rds_conn_lock, flags);
Andy Grover00e0f342009-02-24 15:30:23 +0000411 rds_conn_count--;
Zach Brownfe8ff6b2010-07-23 10:30:45 -0700412 spin_unlock_irqrestore(&rds_conn_lock, flags);
Andy Grover00e0f342009-02-24 15:30:23 +0000413}
Andy Grover616b7572009-08-21 12:28:32 +0000414EXPORT_SYMBOL_GPL(rds_conn_destroy);
Andy Grover00e0f342009-02-24 15:30:23 +0000415
416static void rds_conn_message_info(struct socket *sock, unsigned int len,
417 struct rds_info_iterator *iter,
418 struct rds_info_lengths *lens,
419 int want_send)
420{
421 struct hlist_head *head;
Andy Grover00e0f342009-02-24 15:30:23 +0000422 struct list_head *list;
423 struct rds_connection *conn;
424 struct rds_message *rm;
Andy Grover00e0f342009-02-24 15:30:23 +0000425 unsigned int total = 0;
Zach Brown501dccc2010-06-04 14:25:27 -0700426 unsigned long flags;
Andy Grover00e0f342009-02-24 15:30:23 +0000427 size_t i;
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700428 int j;
Andy Grover00e0f342009-02-24 15:30:23 +0000429
430 len /= sizeof(struct rds_info_message);
431
Chris Masonbcf50ef2010-05-11 15:15:15 -0700432 rcu_read_lock();
Andy Grover00e0f342009-02-24 15:30:23 +0000433
434 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
435 i++, head++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800436 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700437 struct rds_conn_path *cp;
Andy Grover00e0f342009-02-24 15:30:23 +0000438
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700439 for (j = 0; j < RDS_MPATH_WORKERS; j++) {
440 cp = &conn->c_path[j];
441 if (want_send)
442 list = &cp->cp_send_queue;
443 else
444 list = &cp->cp_retrans;
Andy Grover00e0f342009-02-24 15:30:23 +0000445
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700446 spin_lock_irqsave(&cp->cp_lock, flags);
447
448 /* XXX too lazy to maintain counts.. */
449 list_for_each_entry(rm, list, m_conn_item) {
450 total++;
451 if (total <= len)
452 rds_inc_info_copy(&rm->m_inc,
453 iter,
454 conn->c_laddr,
455 conn->c_faddr,
456 0);
457 }
458
459 spin_unlock_irqrestore(&cp->cp_lock, flags);
460 if (!conn->c_trans->t_mp_capable)
461 break;
Andy Grover00e0f342009-02-24 15:30:23 +0000462 }
Andy Grover00e0f342009-02-24 15:30:23 +0000463 }
464 }
Chris Masonbcf50ef2010-05-11 15:15:15 -0700465 rcu_read_unlock();
Andy Grover00e0f342009-02-24 15:30:23 +0000466
467 lens->nr = total;
468 lens->each = sizeof(struct rds_info_message);
469}
470
471static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
472 struct rds_info_iterator *iter,
473 struct rds_info_lengths *lens)
474{
475 rds_conn_message_info(sock, len, iter, lens, 1);
476}
477
478static void rds_conn_message_info_retrans(struct socket *sock,
479 unsigned int len,
480 struct rds_info_iterator *iter,
481 struct rds_info_lengths *lens)
482{
483 rds_conn_message_info(sock, len, iter, lens, 0);
484}
485
486void rds_for_each_conn_info(struct socket *sock, unsigned int len,
487 struct rds_info_iterator *iter,
488 struct rds_info_lengths *lens,
489 int (*visitor)(struct rds_connection *, void *),
490 size_t item_len)
491{
492 uint64_t buffer[(item_len + 7) / 8];
493 struct hlist_head *head;
Andy Grover00e0f342009-02-24 15:30:23 +0000494 struct rds_connection *conn;
Andy Grover00e0f342009-02-24 15:30:23 +0000495 size_t i;
496
Chris Masonbcf50ef2010-05-11 15:15:15 -0700497 rcu_read_lock();
Andy Grover00e0f342009-02-24 15:30:23 +0000498
499 lens->nr = 0;
500 lens->each = item_len;
501
502 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
503 i++, head++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800504 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
Andy Grover00e0f342009-02-24 15:30:23 +0000505
506 /* XXX no c_lock usage.. */
507 if (!visitor(conn, buffer))
508 continue;
509
510 /* We copy as much as we can fit in the buffer,
511 * but we count all items so that the caller
512 * can resize the buffer. */
513 if (len >= item_len) {
514 rds_info_copy(iter, buffer, item_len);
515 len -= item_len;
516 }
517 lens->nr++;
518 }
519 }
Chris Masonbcf50ef2010-05-11 15:15:15 -0700520 rcu_read_unlock();
Andy Grover00e0f342009-02-24 15:30:23 +0000521}
Andy Grover616b7572009-08-21 12:28:32 +0000522EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
Andy Grover00e0f342009-02-24 15:30:23 +0000523
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700524void rds_walk_conn_path_info(struct socket *sock, unsigned int len,
525 struct rds_info_iterator *iter,
526 struct rds_info_lengths *lens,
527 int (*visitor)(struct rds_conn_path *, void *),
528 size_t item_len)
529{
530 u64 buffer[(item_len + 7) / 8];
531 struct hlist_head *head;
532 struct rds_connection *conn;
533 size_t i;
534 int j;
535
536 rcu_read_lock();
537
538 lens->nr = 0;
539 lens->each = item_len;
540
541 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
542 i++, head++) {
543 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
544 struct rds_conn_path *cp;
545
546 for (j = 0; j < RDS_MPATH_WORKERS; j++) {
547 cp = &conn->c_path[j];
548
549 /* XXX no cp_lock usage.. */
550 if (!visitor(cp, buffer))
551 continue;
552 if (!conn->c_trans->t_mp_capable)
553 break;
554 }
555
556 /* We copy as much as we can fit in the buffer,
557 * but we count all items so that the caller
558 * can resize the buffer.
559 */
560 if (len >= item_len) {
561 rds_info_copy(iter, buffer, item_len);
562 len -= item_len;
563 }
564 lens->nr++;
565 }
566 }
567 rcu_read_unlock();
568}
569
570static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
Andy Grover00e0f342009-02-24 15:30:23 +0000571{
572 struct rds_info_connection *cinfo = buffer;
573
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700574 cinfo->next_tx_seq = cp->cp_next_tx_seq;
575 cinfo->next_rx_seq = cp->cp_next_rx_seq;
576 cinfo->laddr = cp->cp_conn->c_laddr;
577 cinfo->faddr = cp->cp_conn->c_faddr;
578 strncpy(cinfo->transport, cp->cp_conn->c_trans->t_name,
Andy Grover00e0f342009-02-24 15:30:23 +0000579 sizeof(cinfo->transport));
580 cinfo->flags = 0;
581
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700582 rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
Zach Brown0f4b1c72010-06-04 14:41:41 -0700583 SENDING);
Andy Grover00e0f342009-02-24 15:30:23 +0000584 /* XXX Future: return the state rather than these funky bits */
585 rds_conn_info_set(cinfo->flags,
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700586 atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
Andy Grover00e0f342009-02-24 15:30:23 +0000587 CONNECTING);
588 rds_conn_info_set(cinfo->flags,
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700589 atomic_read(&cp->cp_state) == RDS_CONN_UP,
Andy Grover00e0f342009-02-24 15:30:23 +0000590 CONNECTED);
591 return 1;
592}
593
594static void rds_conn_info(struct socket *sock, unsigned int len,
595 struct rds_info_iterator *iter,
596 struct rds_info_lengths *lens)
597{
Sowmini Varadhan992c9ec2016-06-13 09:44:38 -0700598 rds_walk_conn_path_info(sock, len, iter, lens,
Andy Grover00e0f342009-02-24 15:30:23 +0000599 rds_conn_info_visitor,
600 sizeof(struct rds_info_connection));
601}
602
Zach Brownef87b7e2010-07-09 12:26:20 -0700603int rds_conn_init(void)
Andy Grover00e0f342009-02-24 15:30:23 +0000604{
605 rds_conn_slab = kmem_cache_create("rds_connection",
606 sizeof(struct rds_connection),
607 0, 0, NULL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800608 if (!rds_conn_slab)
Andy Grover00e0f342009-02-24 15:30:23 +0000609 return -ENOMEM;
610
611 rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
612 rds_info_register_func(RDS_INFO_SEND_MESSAGES,
613 rds_conn_message_info_send);
614 rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
615 rds_conn_message_info_retrans);
616
617 return 0;
618}
619
620void rds_conn_exit(void)
621{
622 rds_loop_exit();
623
624 WARN_ON(!hlist_empty(rds_conn_hash));
625
626 kmem_cache_destroy(rds_conn_slab);
627
628 rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
629 rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
630 rds_conn_message_info_send);
631 rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
632 rds_conn_message_info_retrans);
633}
634
635/*
636 * Force a disconnect
637 */
Sowmini Varadhan0cb43962016-06-13 09:44:26 -0700638void rds_conn_path_drop(struct rds_conn_path *cp)
639{
640 atomic_set(&cp->cp_state, RDS_CONN_ERROR);
641 queue_work(rds_wq, &cp->cp_down_w);
642}
643EXPORT_SYMBOL_GPL(rds_conn_path_drop);
644
Andy Grover00e0f342009-02-24 15:30:23 +0000645void rds_conn_drop(struct rds_connection *conn)
646{
Sowmini Varadhan0cb43962016-06-13 09:44:26 -0700647 rds_conn_path_drop(&conn->c_path[0]);
Andy Grover00e0f342009-02-24 15:30:23 +0000648}
Andy Grover616b7572009-08-21 12:28:32 +0000649EXPORT_SYMBOL_GPL(rds_conn_drop);
Andy Grover00e0f342009-02-24 15:30:23 +0000650
651/*
Zach Brownf3c68082010-05-24 13:14:36 -0700652 * If the connection is down, trigger a connect. We may have scheduled a
653 * delayed reconnect however - in this case we should not interfere.
654 */
Sowmini Varadhan3c0a5902016-06-13 09:44:37 -0700655void rds_conn_path_connect_if_down(struct rds_conn_path *cp)
656{
657 if (rds_conn_path_state(cp) == RDS_CONN_DOWN &&
658 !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags))
659 queue_delayed_work(rds_wq, &cp->cp_conn_w, 0);
660}
661
Zach Brownf3c68082010-05-24 13:14:36 -0700662void rds_conn_connect_if_down(struct rds_connection *conn)
663{
Sowmini Varadhan3c0a5902016-06-13 09:44:37 -0700664 WARN_ON(conn->c_trans->t_mp_capable);
665 rds_conn_path_connect_if_down(&conn->c_path[0]);
Zach Brownf3c68082010-05-24 13:14:36 -0700666}
667EXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
668
669/*
Andy Grover00e0f342009-02-24 15:30:23 +0000670 * An error occurred on the connection
671 */
672void
673__rds_conn_error(struct rds_connection *conn, const char *fmt, ...)
674{
675 va_list ap;
676
677 va_start(ap, fmt);
678 vprintk(fmt, ap);
679 va_end(ap);
680
681 rds_conn_drop(conn);
682}
Sowmini Varadhanfb1b3dc2016-06-13 09:44:39 -0700683
684void
685__rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...)
686{
687 va_list ap;
688
689 va_start(ap, fmt);
690 vprintk(fmt, ap);
691 va_end(ap);
692
693 rds_conn_path_drop(cp);
694}