connection.c 21.9 KB
Newer Older
A
Andy Grover 已提交
1
/*
2
 * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
A
Andy Grover 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#include <linux/kernel.h>
#include <linux/list.h>
35
#include <linux/slab.h>
36
#include <linux/export.h>
37 38
#include <net/ipv6.h>
#include <net/inet6_hashtables.h>
A
Andy Grover 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52

#include "rds.h"
#include "loop.h"

#define RDS_CONNECTION_HASH_BITS 12
#define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
#define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)

/* converting this to RCU is a chore for another day.. */
static DEFINE_SPINLOCK(rds_conn_lock);
static unsigned long rds_conn_count;
static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
static struct kmem_cache *rds_conn_slab;

53 54
static struct hlist_head *rds_conn_bucket(const struct in6_addr *laddr,
					  const struct in6_addr *faddr)
A
Andy Grover 已提交
55
{
56
	static u32 rds6_hash_secret __read_mostly;
57 58
	static u32 rds_hash_secret __read_mostly;

59
	u32 lhash, fhash, hash;
60 61

	net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret));
62 63 64 65 66
	net_get_random_once(&rds6_hash_secret, sizeof(rds6_hash_secret));

	lhash = (__force u32)laddr->s6_addr32[3];
	fhash = __ipv6_addr_jhash(faddr, rds6_hash_secret);
	hash = __inet6_ehashfn(lhash, 0, fhash, 0, rds_hash_secret);
67

A
Andy Grover 已提交
68 69 70 71 72 73 74 75
	return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
}

#define rds_conn_info_set(var, test, suffix) do {		\
	if (test)						\
		var |= RDS_INFO_CONNECTION_FLAG_##suffix;	\
} while (0)

76
/* rcu read lock must be held or the connection spinlock */
77 78
static struct rds_connection *rds_conn_lookup(struct net *net,
					      struct hlist_head *head,
79 80 81 82
					      const struct in6_addr *laddr,
					      const struct in6_addr *faddr,
					      struct rds_transport *trans,
					      int dev_if)
A
Andy Grover 已提交
83 84 85
{
	struct rds_connection *conn, *ret = NULL;

86
	hlist_for_each_entry_rcu(conn, head, c_hash_node) {
87 88 89 90 91
		if (ipv6_addr_equal(&conn->c_faddr, faddr) &&
		    ipv6_addr_equal(&conn->c_laddr, laddr) &&
		    conn->c_trans == trans &&
		    net == rds_conn_net(conn) &&
		    conn->c_dev_if == dev_if) {
A
Andy Grover 已提交
92 93 94 95
			ret = conn;
			break;
		}
	}
96 97
	rdsdebug("returning conn %p for %pI6c -> %pI6c\n", ret,
		 laddr, faddr);
A
Andy Grover 已提交
98 99 100 101 102 103 104 105 106
	return ret;
}

/*
 * This is called by transports as they're bringing down a connection.
 * It clears partial message state so that the transport can start sending
 * and receiving over this connection again in the future.  It is up to
 * the transport to have serialized this call with its send and recv.
 */
107
static void rds_conn_path_reset(struct rds_conn_path *cp)
A
Andy Grover 已提交
108
{
109 110
	struct rds_connection *conn = cp->cp_conn;

111 112
	rdsdebug("connection %pI6c to %pI6c reset\n",
		 &conn->c_laddr, &conn->c_faddr);
A
Andy Grover 已提交
113 114

	rds_stats_inc(s_conn_reset);
115 116
	rds_send_path_reset(cp);
	cp->cp_flags = 0;
A
Andy Grover 已提交
117 118 119 120 121 122 123

	/* Do not clear next_rx_seq here, else we cannot distinguish
	 * retransmitted packets from new packets, and will hand all
	 * of them to the application. That is not consistent with the
	 * reliability guarantees of RDS. */
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
static void __rds_conn_path_init(struct rds_connection *conn,
				 struct rds_conn_path *cp, bool is_outgoing)
{
	spin_lock_init(&cp->cp_lock);
	cp->cp_next_tx_seq = 1;
	init_waitqueue_head(&cp->cp_waitq);
	INIT_LIST_HEAD(&cp->cp_send_queue);
	INIT_LIST_HEAD(&cp->cp_retrans);

	cp->cp_conn = conn;
	atomic_set(&cp->cp_state, RDS_CONN_DOWN);
	cp->cp_send_gen = 0;
	cp->cp_reconnect_jiffies = 0;
	INIT_DELAYED_WORK(&cp->cp_send_w, rds_send_worker);
	INIT_DELAYED_WORK(&cp->cp_recv_w, rds_recv_worker);
	INIT_DELAYED_WORK(&cp->cp_conn_w, rds_connect_worker);
	INIT_WORK(&cp->cp_down_w, rds_shutdown_worker);
	mutex_init(&cp->cp_cm_lock);
	cp->cp_flags = 0;
}

A
Andy Grover 已提交
145 146 147 148 149 150 151 152
/*
 * There is only every one 'conn' for a given pair of addresses in the
 * system at a time.  They contain messages to be retransmitted and so
 * span the lifetime of the actual underlying transport connections.
 *
 * For now they are not garbage collected once they're created.  They
 * are torn down as the module is removed, if ever.
 */
153
static struct rds_connection *__rds_conn_create(struct net *net,
154 155 156 157 158 159
						const struct in6_addr *laddr,
						const struct in6_addr *faddr,
						struct rds_transport *trans,
						gfp_t gfp,
						int is_outgoing,
						int dev_if)
A
Andy Grover 已提交
160
{
161
	struct rds_connection *conn, *parent = NULL;
A
Andy Grover 已提交
162
	struct hlist_head *head = rds_conn_bucket(laddr, faddr);
163
	struct rds_transport *loop_trans;
A
Andy Grover 已提交
164
	unsigned long flags;
165
	int ret, i;
166
	int npaths = (trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
A
Andy Grover 已提交
167

168
	rcu_read_lock();
169 170 171 172 173 174
	conn = rds_conn_lookup(net, head, laddr, faddr, trans, dev_if);
	if (conn &&
	    conn->c_loopback &&
	    conn->c_trans != &rds_loop_transport &&
	    ipv6_addr_equal(laddr, faddr) &&
	    !is_outgoing) {
A
Andy Grover 已提交
175 176 177 178 179 180 181
		/* This is a looped back IB connection, and we're
		 * called by the code handling the incoming connect.
		 * We need a second connection object into which we
		 * can stick the other QP. */
		parent = conn;
		conn = parent->c_passive;
	}
182
	rcu_read_unlock();
A
Andy Grover 已提交
183 184 185
	if (conn)
		goto out;

186
	conn = kmem_cache_zalloc(rds_conn_slab, gfp);
187
	if (!conn) {
A
Andy Grover 已提交
188 189 190
		conn = ERR_PTR(-ENOMEM);
		goto out;
	}
191 192 193 194 195 196
	conn->c_path = kcalloc(npaths, sizeof(struct rds_conn_path), gfp);
	if (!conn->c_path) {
		kmem_cache_free(rds_conn_slab, conn);
		conn = ERR_PTR(-ENOMEM);
		goto out;
	}
A
Andy Grover 已提交
197 198

	INIT_HLIST_NODE(&conn->c_hash_node);
199 200 201 202
	conn->c_laddr = *laddr;
	conn->c_isv6 = !ipv6_addr_v4mapped(laddr);
	conn->c_faddr = *faddr;
	conn->c_dev_if = dev_if;
A
Andy Grover 已提交
203

204
	rds_conn_net_set(conn, net);
A
Andy Grover 已提交
205 206 207

	ret = rds_cong_get_maps(conn);
	if (ret) {
208
		kfree(conn->c_path);
A
Andy Grover 已提交
209 210 211 212 213 214 215 216 217 218
		kmem_cache_free(rds_conn_slab, conn);
		conn = ERR_PTR(ret);
		goto out;
	}

	/*
	 * This is where a connection becomes loopback.  If *any* RDS sockets
	 * can bind to the destination address then we'd rather the messages
	 * flow through loopback rather than either transport.
	 */
219
	loop_trans = rds_trans_get_preferred(net, faddr, conn->c_dev_if);
220 221
	if (loop_trans) {
		rds_trans_put(loop_trans);
A
Andy Grover 已提交
222 223 224 225 226 227 228 229 230 231 232 233
		conn->c_loopback = 1;
		if (is_outgoing && trans->t_prefer_loopback) {
			/* "outgoing" connection - and the transport
			 * says it wants the connection handled by the
			 * loopback transport. This is what TCP does.
			 */
			trans = &rds_loop_transport;
		}
	}

	conn->c_trans = trans;

234
	init_waitqueue_head(&conn->c_hs_waitq);
235
	for (i = 0; i < npaths; i++) {
236 237 238 239
		__rds_conn_path_init(conn, &conn->c_path[i],
				     is_outgoing);
		conn->c_path[i].cp_index = i;
	}
240 241 242 243
	rcu_read_lock();
	if (rds_destroy_pending(conn))
		ret = -ENETDOWN;
	else
244
		ret = trans->conn_alloc(conn, GFP_ATOMIC);
A
Andy Grover 已提交
245
	if (ret) {
246
		rcu_read_unlock();
247
		kfree(conn->c_path);
A
Andy Grover 已提交
248 249 250 251 252
		kmem_cache_free(rds_conn_slab, conn);
		conn = ERR_PTR(ret);
		goto out;
	}

253 254 255 256
	rdsdebug("allocated conn %p for %pI6c -> %pI6c over %s %s\n",
		 conn, laddr, faddr,
		 strnlen(trans->t_name, sizeof(trans->t_name)) ?
		 trans->t_name : "[unknown]", is_outgoing ? "(outgoing)" : "");
A
Andy Grover 已提交
257

258 259 260 261 262 263 264
	/*
	 * Since we ran without holding the conn lock, someone could
	 * have created the same conn (either normal or passive) in the
	 * interim. We check while holding the lock. If we won, we complete
	 * init and return our conn. If we lost, we rollback and return the
	 * other one.
	 */
A
Andy Grover 已提交
265
	spin_lock_irqsave(&rds_conn_lock, flags);
266 267 268
	if (parent) {
		/* Creating passive conn */
		if (parent->c_passive) {
269
			trans->conn_free(conn->c_path[0].cp_transport_data);
270
			kfree(conn->c_path);
271 272 273
			kmem_cache_free(rds_conn_slab, conn);
			conn = parent->c_passive;
		} else {
A
Andy Grover 已提交
274
			parent->c_passive = conn;
275 276 277
			rds_cong_add_conn(conn);
			rds_conn_count++;
		}
A
Andy Grover 已提交
278
	} else {
279 280 281
		/* Creating normal conn */
		struct rds_connection *found;

282 283
		found = rds_conn_lookup(net, head, laddr, faddr, trans,
					dev_if);
284
		if (found) {
285 286 287
			struct rds_conn_path *cp;
			int i;

288
			for (i = 0; i < npaths; i++) {
289
				cp = &conn->c_path[i];
290 291 292 293 294 295
				/* The ->conn_alloc invocation may have
				 * allocated resource for all paths, so all
				 * of them may have to be freed here.
				 */
				if (cp->cp_transport_data)
					trans->conn_free(cp->cp_transport_data);
296
			}
297
			kfree(conn->c_path);
298 299 300
			kmem_cache_free(rds_conn_slab, conn);
			conn = found;
		} else {
301 302
			conn->c_my_gen_num = rds_gen_num;
			conn->c_peer_gen_num = 0;
303
			hlist_add_head_rcu(&conn->c_hash_node, head);
304 305 306
			rds_cong_add_conn(conn);
			rds_conn_count++;
		}
A
Andy Grover 已提交
307 308
	}
	spin_unlock_irqrestore(&rds_conn_lock, flags);
309
	rcu_read_unlock();
A
Andy Grover 已提交
310 311 312 313 314

out:
	return conn;
}

315
struct rds_connection *rds_conn_create(struct net *net,
316 317 318 319
				       const struct in6_addr *laddr,
				       const struct in6_addr *faddr,
				       struct rds_transport *trans, gfp_t gfp,
				       int dev_if)
A
Andy Grover 已提交
320
{
321
	return __rds_conn_create(net, laddr, faddr, trans, gfp, 0, dev_if);
A
Andy Grover 已提交
322
}
A
Andy Grover 已提交
323
EXPORT_SYMBOL_GPL(rds_conn_create);
A
Andy Grover 已提交
324

325
struct rds_connection *rds_conn_create_outgoing(struct net *net,
326 327 328 329
						const struct in6_addr *laddr,
						const struct in6_addr *faddr,
						struct rds_transport *trans,
						gfp_t gfp, int dev_if)
A
Andy Grover 已提交
330
{
331
	return __rds_conn_create(net, laddr, faddr, trans, gfp, 1, dev_if);
A
Andy Grover 已提交
332
}
A
Andy Grover 已提交
333
EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
A
Andy Grover 已提交
334

335
void rds_conn_shutdown(struct rds_conn_path *cp)
336
{
337 338
	struct rds_connection *conn = cp->cp_conn;

339
	/* shut it down unless it's down already */
340
	if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
341 342 343 344 345 346 347
		/*
		 * Quiesce the connection mgmt handlers before we start tearing
		 * things down. We don't hold the mutex for the entire
		 * duration of the shutdown operation, else we may be
		 * deadlocking with the CM handler. Instead, the CM event
		 * handler is supposed to check for state DISCONNECTING
		 */
348 349 350 351 352 353 354 355 356
		mutex_lock(&cp->cp_cm_lock);
		if (!rds_conn_path_transition(cp, RDS_CONN_UP,
					      RDS_CONN_DISCONNECTING) &&
		    !rds_conn_path_transition(cp, RDS_CONN_ERROR,
					      RDS_CONN_DISCONNECTING)) {
			rds_conn_path_error(cp,
					    "shutdown called in state %d\n",
					    atomic_read(&cp->cp_state));
			mutex_unlock(&cp->cp_cm_lock);
357 358
			return;
		}
359
		mutex_unlock(&cp->cp_cm_lock);
360

361 362 363 364
		wait_event(cp->cp_waitq,
			   !test_bit(RDS_IN_XMIT, &cp->cp_flags));
		wait_event(cp->cp_waitq,
			   !test_bit(RDS_RECV_REFILL, &cp->cp_flags));
365

366
		conn->c_trans->conn_path_shutdown(cp);
367
		rds_conn_path_reset(cp);
368

369
		if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING,
370 371
					      RDS_CONN_DOWN) &&
		    !rds_conn_path_transition(cp, RDS_CONN_ERROR,
372
					      RDS_CONN_DOWN)) {
373 374
			/* This can happen - eg when we're in the middle of tearing
			 * down the connection, and someone unloads the rds module.
375
			 * Quite reproducible with loopback connections.
376
			 * Mostly harmless.
377 378 379 380 381 382
			 *
			 * Note that this also happens with rds-tcp because
			 * we could have triggered rds_conn_path_drop in irq
			 * mode from rds_tcp_state change on the receipt of
			 * a FIN, thus we need to recheck for RDS_CONN_ERROR
			 * here.
383
			 */
384 385 386 387
			rds_conn_path_error(cp, "%s: failed to transition "
					    "to state DOWN, current state "
					    "is %d\n", __func__,
					    atomic_read(&cp->cp_state));
388 389 390 391 392 393 394 395
			return;
		}
	}

	/* Then reconnect if it's still live.
	 * The passive side of an IB loopback connection is never added
	 * to the conn hash, so we never trigger a reconnect on this
	 * conn - the reconnect is always triggered by the active peer. */
396
	cancel_delayed_work_sync(&cp->cp_conn_w);
397 398 399
	rcu_read_lock();
	if (!hlist_unhashed(&conn->c_hash_node)) {
		rcu_read_unlock();
400
		rds_queue_reconnect(cp);
401 402 403
	} else {
		rcu_read_unlock();
	}
404 405
}

406 407 408 409 410 411 412
/* destroy a single rds_conn_path. rds_conn_destroy() iterates over
 * all paths using rds_conn_path_destroy()
 */
static void rds_conn_path_destroy(struct rds_conn_path *cp)
{
	struct rds_message *rm, *rtmp;

413 414 415
	if (!cp->cp_transport_data)
		return;

416 417 418 419
	/* make sure lingering queued work won't try to ref the conn */
	cancel_delayed_work_sync(&cp->cp_send_w);
	cancel_delayed_work_sync(&cp->cp_recv_w);

420 421 422
	rds_conn_path_drop(cp, true);
	flush_work(&cp->cp_down_w);

423 424 425 426 427 428 429 430 431 432 433
	/* tear down queued messages */
	list_for_each_entry_safe(rm, rtmp,
				 &cp->cp_send_queue,
				 m_conn_item) {
		list_del_init(&rm->m_conn_item);
		BUG_ON(!list_empty(&rm->m_sock_item));
		rds_message_put(rm);
	}
	if (cp->cp_xmit_rm)
		rds_message_put(cp->cp_xmit_rm);

434 435 436 437 438
	WARN_ON(delayed_work_pending(&cp->cp_send_w));
	WARN_ON(delayed_work_pending(&cp->cp_recv_w));
	WARN_ON(delayed_work_pending(&cp->cp_conn_w));
	WARN_ON(work_pending(&cp->cp_down_w));

439 440 441
	cp->cp_conn->c_trans->conn_free(cp->cp_transport_data);
}

442 443
/*
 * Stop and free a connection.
444 445 446 447
 *
 * This can only be used in very limited circumstances.  It assumes that once
 * the conn has been shutdown that no one else is referencing the connection.
 * We can only ensure this in the rmmod path in the current code.
448
 */
A
Andy Grover 已提交
449 450
void rds_conn_destroy(struct rds_connection *conn)
{
451
	unsigned long flags;
452 453
	int i;
	struct rds_conn_path *cp;
454
	int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
A
Andy Grover 已提交
455 456 457 458 459

	rdsdebug("freeing conn %p for %pI4 -> "
		 "%pI4\n", conn, &conn->c_laddr,
		 &conn->c_faddr);

460 461
	/* Ensure conn will not be scheduled for reconnect */
	spin_lock_irq(&rds_conn_lock);
462
	hlist_del_init_rcu(&conn->c_hash_node);
463
	spin_unlock_irq(&rds_conn_lock);
464 465
	synchronize_rcu();

466
	/* shut the connection down */
467
	for (i = 0; i < npaths; i++) {
468 469 470
		cp = &conn->c_path[i];
		rds_conn_path_destroy(cp);
		BUG_ON(!list_empty(&cp->cp_retrans));
A
Andy Grover 已提交
471 472 473 474 475 476 477 478 479
	}

	/*
	 * The congestion maps aren't freed up here.  They're
	 * freed by rds_cong_exit() after all the connections
	 * have been freed.
	 */
	rds_cong_remove_conn(conn);

480
	kfree(conn->c_path);
A
Andy Grover 已提交
481 482
	kmem_cache_free(rds_conn_slab, conn);

483
	spin_lock_irqsave(&rds_conn_lock, flags);
A
Andy Grover 已提交
484
	rds_conn_count--;
485
	spin_unlock_irqrestore(&rds_conn_lock, flags);
A
Andy Grover 已提交
486
}
A
Andy Grover 已提交
487
EXPORT_SYMBOL_GPL(rds_conn_destroy);
A
Andy Grover 已提交
488 489 490 491 492 493 494 495 496 497 498

static void rds_conn_message_info(struct socket *sock, unsigned int len,
				  struct rds_info_iterator *iter,
				  struct rds_info_lengths *lens,
				  int want_send)
{
	struct hlist_head *head;
	struct list_head *list;
	struct rds_connection *conn;
	struct rds_message *rm;
	unsigned int total = 0;
499
	unsigned long flags;
A
Andy Grover 已提交
500
	size_t i;
501
	int j;
A
Andy Grover 已提交
502 503 504

	len /= sizeof(struct rds_info_message);

505
	rcu_read_lock();
A
Andy Grover 已提交
506 507 508

	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
	     i++, head++) {
509
		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
510
			struct rds_conn_path *cp;
511 512 513 514
			int npaths;

			npaths = (conn->c_trans->t_mp_capable ?
				 RDS_MPATH_WORKERS : 1);
515

516
			for (j = 0; j < npaths; j++) {
517 518 519 520 521 522 523 524 525 526
				cp = &conn->c_path[j];
				if (want_send)
					list = &cp->cp_send_queue;
				else
					list = &cp->cp_retrans;

				spin_lock_irqsave(&cp->cp_lock, flags);

				/* XXX too lazy to maintain counts.. */
				list_for_each_entry(rm, list, m_conn_item) {
527 528 529
					__be32 laddr;
					__be32 faddr;

530
					total++;
531 532
					laddr = conn->c_laddr.s6_addr32[3];
					faddr = conn->c_faddr.s6_addr32[3];
533 534 535
					if (total <= len)
						rds_inc_info_copy(&rm->m_inc,
								  iter,
536 537
								  laddr,
								  faddr,
538 539 540 541
								  0);
				}

				spin_unlock_irqrestore(&cp->cp_lock, flags);
A
Andy Grover 已提交
542 543 544
			}
		}
	}
545
	rcu_read_unlock();
A
Andy Grover 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

	lens->nr = total;
	lens->each = sizeof(struct rds_info_message);
}

static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
				       struct rds_info_iterator *iter,
				       struct rds_info_lengths *lens)
{
	rds_conn_message_info(sock, len, iter, lens, 1);
}

static void rds_conn_message_info_retrans(struct socket *sock,
					  unsigned int len,
					  struct rds_info_iterator *iter,
					  struct rds_info_lengths *lens)
{
	rds_conn_message_info(sock, len, iter, lens, 0);
}

void 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 *),
570
			  u64 *buffer,
A
Andy Grover 已提交
571 572 573 574 575 576
			  size_t item_len)
{
	struct hlist_head *head;
	struct rds_connection *conn;
	size_t i;

577
	rcu_read_lock();
A
Andy Grover 已提交
578 579 580 581 582 583

	lens->nr = 0;
	lens->each = item_len;

	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
	     i++, head++) {
584
		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
A
Andy Grover 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

			/* XXX no c_lock usage.. */
			if (!visitor(conn, buffer))
				continue;

			/* We copy as much as we can fit in the buffer,
			 * but we count all items so that the caller
			 * can resize the buffer. */
			if (len >= item_len) {
				rds_info_copy(iter, buffer, item_len);
				len -= item_len;
			}
			lens->nr++;
		}
	}
600
	rcu_read_unlock();
A
Andy Grover 已提交
601
}
A
Andy Grover 已提交
602
EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
A
Andy Grover 已提交
603

604 605 606 607
static void 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 *),
608
				    u64 *buffer,
609
				    size_t item_len)
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
{
	struct hlist_head *head;
	struct rds_connection *conn;
	size_t i;

	rcu_read_lock();

	lens->nr = 0;
	lens->each = item_len;

	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
	     i++, head++) {
		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
			struct rds_conn_path *cp;

625 626 627 628 629 630 631 632 633
			/* XXX We only copy the information from the first
			 * path for now.  The problem is that if there are
			 * more than one underlying paths, we cannot report
			 * information of all of them using the existing
			 * API.  For example, there is only one next_tx_seq,
			 * which path's next_tx_seq should we report?  It is
			 * a bug in the design of MPRDS.
			 */
			cp = conn->c_path;
634

635 636 637
			/* XXX no cp_lock usage.. */
			if (!visitor(cp, buffer))
				continue;
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

			/* We copy as much as we can fit in the buffer,
			 * but we count all items so that the caller
			 * can resize the buffer.
			 */
			if (len >= item_len) {
				rds_info_copy(iter, buffer, item_len);
				len -= item_len;
			}
			lens->nr++;
		}
	}
	rcu_read_unlock();
}

static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
A
Andy Grover 已提交
654 655
{
	struct rds_info_connection *cinfo = buffer;
656
	struct rds_connection *conn = cp->cp_conn;
A
Andy Grover 已提交
657

658 659
	cinfo->next_tx_seq = cp->cp_next_tx_seq;
	cinfo->next_rx_seq = cp->cp_next_rx_seq;
660 661 662
	cinfo->laddr = conn->c_laddr.s6_addr32[3];
	cinfo->faddr = conn->c_faddr.s6_addr32[3];
	strncpy(cinfo->transport, conn->c_trans->t_name,
A
Andy Grover 已提交
663 664 665
		sizeof(cinfo->transport));
	cinfo->flags = 0;

666
	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
667
			  SENDING);
A
Andy Grover 已提交
668 669
	/* XXX Future: return the state rather than these funky bits */
	rds_conn_info_set(cinfo->flags,
670
			  atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
A
Andy Grover 已提交
671 672
			  CONNECTING);
	rds_conn_info_set(cinfo->flags,
673
			  atomic_read(&cp->cp_state) == RDS_CONN_UP,
A
Andy Grover 已提交
674 675 676 677 678 679 680 681
			  CONNECTED);
	return 1;
}

static void rds_conn_info(struct socket *sock, unsigned int len,
			  struct rds_info_iterator *iter,
			  struct rds_info_lengths *lens)
{
682 683
	u64 buffer[(sizeof(struct rds_info_connection) + 7) / 8];

684
	rds_walk_conn_path_info(sock, len, iter, lens,
A
Andy Grover 已提交
685
				rds_conn_info_visitor,
686
				buffer,
A
Andy Grover 已提交
687 688 689
				sizeof(struct rds_info_connection));
}

690
int rds_conn_init(void)
A
Andy Grover 已提交
691
{
692 693 694 695 696 697
	int ret;

	ret = rds_loop_net_init(); /* register pernet callback */
	if (ret)
		return ret;

A
Andy Grover 已提交
698 699 700
	rds_conn_slab = kmem_cache_create("rds_connection",
					  sizeof(struct rds_connection),
					  0, 0, NULL);
701 702
	if (!rds_conn_slab) {
		rds_loop_net_exit();
A
Andy Grover 已提交
703
		return -ENOMEM;
704
	}
A
Andy Grover 已提交
705 706 707 708 709 710 711 712 713 714 715 716

	rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
	rds_info_register_func(RDS_INFO_SEND_MESSAGES,
			       rds_conn_message_info_send);
	rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
			       rds_conn_message_info_retrans);

	return 0;
}

void rds_conn_exit(void)
{
717
	rds_loop_net_exit(); /* unregister pernet callback */
A
Andy Grover 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	rds_loop_exit();

	WARN_ON(!hlist_empty(rds_conn_hash));

	kmem_cache_destroy(rds_conn_slab);

	rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
	rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
				 rds_conn_message_info_send);
	rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
				 rds_conn_message_info_retrans);
}

/*
 * Force a disconnect
 */
734
void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy)
735 736
{
	atomic_set(&cp->cp_state, RDS_CONN_ERROR);
737

738
	rcu_read_lock();
739
	if (!destroy && rds_destroy_pending(cp->cp_conn)) {
740
		rcu_read_unlock();
741
		return;
742
	}
743
	queue_work(rds_wq, &cp->cp_down_w);
744
	rcu_read_unlock();
745 746 747
}
EXPORT_SYMBOL_GPL(rds_conn_path_drop);

A
Andy Grover 已提交
748 749
void rds_conn_drop(struct rds_connection *conn)
{
750
	WARN_ON(conn->c_trans->t_mp_capable);
751
	rds_conn_path_drop(&conn->c_path[0], false);
A
Andy Grover 已提交
752
}
A
Andy Grover 已提交
753
EXPORT_SYMBOL_GPL(rds_conn_drop);
A
Andy Grover 已提交
754

755 756 757 758
/*
 * If the connection is down, trigger a connect. We may have scheduled a
 * delayed reconnect however - in this case we should not interfere.
 */
759 760
void rds_conn_path_connect_if_down(struct rds_conn_path *cp)
{
761
	rcu_read_lock();
762
	if (rds_destroy_pending(cp->cp_conn)) {
763 764 765
		rcu_read_unlock();
		return;
	}
766 767 768
	if (rds_conn_path_state(cp) == RDS_CONN_DOWN &&
	    !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags))
		queue_delayed_work(rds_wq, &cp->cp_conn_w, 0);
769
	rcu_read_unlock();
770
}
771
EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down);
772

773 774
void rds_conn_connect_if_down(struct rds_connection *conn)
{
775 776
	WARN_ON(conn->c_trans->t_mp_capable);
	rds_conn_path_connect_if_down(&conn->c_path[0]);
777 778 779
}
EXPORT_SYMBOL_GPL(rds_conn_connect_if_down);

780 781 782 783 784 785 786 787 788
void
__rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vprintk(fmt, ap);
	va_end(ap);

789
	rds_conn_path_drop(cp, false);
790
}