xref: /linux/net/rds/connection.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 /*
2  * Copyright (c) 2006, 2018 Oracle and/or its affiliates. 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>
35 #include <linux/slab.h>
36 #include <linux/export.h>
37 #include <net/ipv6.h>
38 #include <net/inet6_hashtables.h>
39 #include <net/addrconf.h>
40 
41 #include "rds.h"
42 #include "loop.h"
43 
44 #define RDS_CONNECTION_HASH_BITS 12
45 #define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
46 #define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
47 
48 /* converting this to RCU is a chore for another day.. */
49 static DEFINE_SPINLOCK(rds_conn_lock);
50 static unsigned long rds_conn_count;
51 static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
52 static struct kmem_cache *rds_conn_slab;
53 
54 static struct hlist_head *rds_conn_bucket(const struct in6_addr *laddr,
55 					  const struct in6_addr *faddr)
56 {
57 	static u32 rds6_hash_secret __read_mostly;
58 	static u32 rds_hash_secret __read_mostly;
59 
60 	__be32 lhash, fhash;
61 	u32 hash;
62 
63 	net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret));
64 	net_get_random_once(&rds6_hash_secret, sizeof(rds6_hash_secret));
65 
66 	lhash = laddr->s6_addr32[3];
67 #if IS_ENABLED(CONFIG_IPV6)
68 	fhash = (__force __be32)__ipv6_addr_jhash(faddr, rds6_hash_secret);
69 #else
70 	fhash = faddr->s6_addr32[3];
71 #endif
72 	hash = __inet_ehashfn(lhash, 0, fhash, 0, rds_hash_secret);
73 
74 	return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
75 }
76 
77 #define rds_conn_info_set(var, test, suffix) do {		\
78 	if (test)						\
79 		var |= RDS_INFO_CONNECTION_FLAG_##suffix;	\
80 } while (0)
81 
82 /* rcu read lock must be held or the connection spinlock */
83 static struct rds_connection *rds_conn_lookup(struct net *net,
84 					      struct hlist_head *head,
85 					      const struct in6_addr *laddr,
86 					      const struct in6_addr *faddr,
87 					      struct rds_transport *trans,
88 					      u8 tos, int dev_if)
89 {
90 	struct rds_connection *conn, *ret = NULL;
91 
92 	hlist_for_each_entry_rcu(conn, head, c_hash_node) {
93 		if (ipv6_addr_equal(&conn->c_faddr, faddr) &&
94 		    ipv6_addr_equal(&conn->c_laddr, laddr) &&
95 		    conn->c_trans == trans &&
96 		    conn->c_tos == tos &&
97 		    net == rds_conn_net(conn) &&
98 		    conn->c_dev_if == dev_if) {
99 			ret = conn;
100 			break;
101 		}
102 	}
103 	rdsdebug("returning conn %p for %pI6c -> %pI6c\n", ret,
104 		 laddr, faddr);
105 	return ret;
106 }
107 
108 /*
109  * This is called by rds_conn_shutdown() once the transport has brought
110  * a path down.  It clears partial message state so that the transport
111  * can start sending and receiving over this path again in the future.
112  * The caller owns RDS_IN_XMIT and RDS_RECV_REFILL across this call,
113  * which is what serializes it against the send and receive-refill
114  * paths.
115  */
116 static void rds_conn_path_reset(struct rds_conn_path *cp)
117 {
118 	struct rds_connection *conn = cp->cp_conn;
119 
120 	rdsdebug("connection %pI6c to %pI6c reset\n",
121 		 &conn->c_laddr, &conn->c_faddr);
122 
123 	rds_stats_inc(s_conn_reset);
124 	rds_send_path_reset(cp);
125 
126 	/* Clear the bits the reset is responsible for individually: a
127 	 * blanket cp_flags = 0 is a plain store that can clobber a
128 	 * concurrent atomic read-modify-write on the same word.
129 	 * RDS_IN_XMIT and RDS_RECV_REFILL are held as locks by the
130 	 * caller, rds_conn_shutdown(), which releases them once the
131 	 * teardown is complete.
132 	 */
133 	clear_bit(RDS_LL_SEND_FULL, &cp->cp_flags);
134 	clear_bit(RDS_RECONNECT_PENDING, &cp->cp_flags);
135 
136 	/* Do not clear next_rx_seq here, else we cannot distinguish
137 	 * retransmitted packets from new packets, and will hand all
138 	 * of them to the application. That is not consistent with the
139 	 * reliability guarantees of RDS. */
140 }
141 
142 static void __rds_conn_path_init(struct rds_connection *conn,
143 				 struct rds_conn_path *cp, bool is_outgoing)
144 {
145 	spin_lock_init(&cp->cp_lock);
146 	cp->cp_next_tx_seq = 1;
147 	init_waitqueue_head(&cp->cp_waitq);
148 	INIT_LIST_HEAD(&cp->cp_send_queue);
149 	INIT_LIST_HEAD(&cp->cp_retrans);
150 
151 	cp->cp_conn = conn;
152 	atomic_set(&cp->cp_state, RDS_CONN_DOWN);
153 	cp->cp_send_gen = 0;
154 	cp->cp_reconnect_jiffies = 0;
155 	cp->cp_conn->c_proposed_version = RDS_PROTOCOL_VERSION;
156 	INIT_DELAYED_WORK(&cp->cp_send_w, rds_send_worker);
157 	INIT_DELAYED_WORK(&cp->cp_recv_w, rds_recv_worker);
158 	INIT_DELAYED_WORK(&cp->cp_conn_w, rds_connect_worker);
159 	INIT_WORK(&cp->cp_down_w, rds_shutdown_worker);
160 	mutex_init(&cp->cp_cm_lock);
161 	cp->cp_flags = 0;
162 }
163 
164 /*
165  * There is only every one 'conn' for a given pair of addresses in the
166  * system at a time.  They contain messages to be retransmitted and so
167  * span the lifetime of the actual underlying transport connections.
168  *
169  * For now they are not garbage collected once they're created.  They
170  * are torn down as the module is removed, if ever.
171  */
172 static struct rds_connection *__rds_conn_create(struct net *net,
173 						const struct in6_addr *laddr,
174 						const struct in6_addr *faddr,
175 						struct rds_transport *trans,
176 						gfp_t gfp, u8 tos,
177 						int is_outgoing,
178 						int dev_if)
179 {
180 	struct rds_connection *conn, *parent = NULL;
181 	struct hlist_head *head = rds_conn_bucket(laddr, faddr);
182 	struct rds_transport *loop_trans;
183 	struct rds_conn_path *free_cp = NULL;
184 	unsigned long flags;
185 	int ret, i;
186 	int npaths = (trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
187 
188 	rcu_read_lock();
189 	conn = rds_conn_lookup(net, head, laddr, faddr, trans, tos, dev_if);
190 	if (conn &&
191 	    conn->c_loopback &&
192 	    conn->c_trans != &rds_loop_transport &&
193 	    ipv6_addr_equal(laddr, faddr) &&
194 	    !is_outgoing) {
195 		/* This is a looped back IB connection, and we're
196 		 * called by the code handling the incoming connect.
197 		 * We need a second connection object into which we
198 		 * can stick the other QP. */
199 		parent = conn;
200 		conn = parent->c_passive;
201 	}
202 	rcu_read_unlock();
203 	if (conn)
204 		goto out;
205 
206 	conn = kmem_cache_zalloc(rds_conn_slab, gfp);
207 	if (!conn) {
208 		conn = ERR_PTR(-ENOMEM);
209 		goto out;
210 	}
211 	conn->c_path = kzalloc_objs(struct rds_conn_path, npaths, gfp);
212 	if (!conn->c_path) {
213 		kmem_cache_free(rds_conn_slab, conn);
214 		conn = ERR_PTR(-ENOMEM);
215 		goto out;
216 	}
217 
218 	INIT_HLIST_NODE(&conn->c_hash_node);
219 	conn->c_laddr = *laddr;
220 	conn->c_isv6 = !ipv6_addr_v4mapped(laddr);
221 	conn->c_faddr = *faddr;
222 	conn->c_dev_if = dev_if;
223 	conn->c_tos = tos;
224 
225 #if IS_ENABLED(CONFIG_IPV6)
226 	/* If the local address is link local, set c_bound_if to be the
227 	 * index used for this connection.  Otherwise, set it to 0 as
228 	 * the socket is not bound to an interface.  c_bound_if is used
229 	 * to look up a socket when a packet is received
230 	 */
231 	if (ipv6_addr_type(laddr) & IPV6_ADDR_LINKLOCAL)
232 		conn->c_bound_if = dev_if;
233 	else
234 #endif
235 		conn->c_bound_if = 0;
236 
237 	rds_conn_net_set(conn, net);
238 
239 	ret = rds_cong_get_maps(conn);
240 	if (ret) {
241 		kfree(conn->c_path);
242 		kmem_cache_free(rds_conn_slab, conn);
243 		conn = ERR_PTR(ret);
244 		goto out;
245 	}
246 
247 	/*
248 	 * This is where a connection becomes loopback.  If *any* RDS sockets
249 	 * can bind to the destination address then we'd rather the messages
250 	 * flow through loopback rather than either transport.
251 	 */
252 	loop_trans = rds_trans_get_preferred(net, faddr, conn->c_dev_if);
253 	if (loop_trans) {
254 		rds_trans_put(loop_trans);
255 		conn->c_loopback = 1;
256 		if (trans->t_prefer_loopback) {
257 			if (likely(is_outgoing)) {
258 				/* "outgoing" connection to local address.
259 				 * Protocol says it wants the connection
260 				 * handled by the loopback transport.
261 				 * This is what TCP does.
262 				 */
263 				trans = &rds_loop_transport;
264 			} else {
265 				/* No transport currently in use
266 				 * should end up here, but if it
267 				 * does, reset/destroy the connection.
268 				 */
269 				kfree(conn->c_path);
270 				kmem_cache_free(rds_conn_slab, conn);
271 				conn = ERR_PTR(-EOPNOTSUPP);
272 				goto out;
273 			}
274 		}
275 	}
276 
277 	conn->c_trans = trans;
278 
279 	init_waitqueue_head(&conn->c_hs_waitq);
280 	for (i = 0; i < npaths; i++) {
281 		__rds_conn_path_init(conn, &conn->c_path[i],
282 				     is_outgoing);
283 		conn->c_path[i].cp_index = i;
284 		conn->c_path[i].cp_wq =
285 			alloc_ordered_workqueue("krds_cp_wq#%lu/%d", 0,
286 						rds_conn_count, i);
287 		if (!conn->c_path[i].cp_wq)
288 			conn->c_path[i].cp_wq = rds_wq;
289 	}
290 	rcu_read_lock();
291 	if (rds_destroy_pending(conn))
292 		ret = -ENETDOWN;
293 	else
294 		ret = trans->conn_alloc(conn, GFP_ATOMIC);
295 	if (ret) {
296 		rcu_read_unlock();
297 		free_cp = conn->c_path;
298 		kmem_cache_free(rds_conn_slab, conn);
299 		conn = ERR_PTR(ret);
300 		goto out;
301 	}
302 
303 	rdsdebug("allocated conn %p for %pI6c -> %pI6c over %s %s\n",
304 		 conn, laddr, faddr,
305 		 strnlen(trans->t_name, sizeof(trans->t_name)) ?
306 		 trans->t_name : "[unknown]", is_outgoing ? "(outgoing)" : "");
307 
308 	/*
309 	 * Since we ran without holding the conn lock, someone could
310 	 * have created the same conn (either normal or passive) in the
311 	 * interim. We check while holding the lock. If we won, we complete
312 	 * init and return our conn. If we lost, we rollback and return the
313 	 * other one.
314 	 */
315 	spin_lock_irqsave(&rds_conn_lock, flags);
316 	if (parent) {
317 		/* Creating passive conn */
318 		if (parent->c_passive) {
319 			trans->conn_free(conn->c_path[0].cp_transport_data);
320 			free_cp = conn->c_path;
321 			kmem_cache_free(rds_conn_slab, conn);
322 			conn = parent->c_passive;
323 		} else {
324 			parent->c_passive = conn;
325 			rds_cong_add_conn(conn);
326 			rds_conn_count++;
327 		}
328 	} else {
329 		/* Creating normal conn */
330 		struct rds_connection *found;
331 
332 		found = rds_conn_lookup(net, head, laddr, faddr, trans,
333 					tos, dev_if);
334 		if (found) {
335 			struct rds_conn_path *cp;
336 			int i;
337 
338 			for (i = 0; i < npaths; i++) {
339 				cp = &conn->c_path[i];
340 				/* The ->conn_alloc invocation may have
341 				 * allocated resource for all paths, so all
342 				 * of them may have to be freed here.
343 				 */
344 				if (cp->cp_transport_data)
345 					trans->conn_free(cp->cp_transport_data);
346 			}
347 			free_cp = conn->c_path;
348 			kmem_cache_free(rds_conn_slab, conn);
349 			conn = found;
350 		} else {
351 			conn->c_my_gen_num = rds_gen_num;
352 			conn->c_peer_gen_num = 0;
353 			hlist_add_head_rcu(&conn->c_hash_node, head);
354 			rds_cong_add_conn(conn);
355 			rds_conn_count++;
356 		}
357 	}
358 	spin_unlock_irqrestore(&rds_conn_lock, flags);
359 	rcu_read_unlock();
360 
361 out:
362 	if (free_cp) {
363 		for (i = 0; i < npaths; i++)
364 			if (free_cp[i].cp_wq != rds_wq)
365 				destroy_workqueue(free_cp[i].cp_wq);
366 		kfree(free_cp);
367 	}
368 
369 	return conn;
370 }
371 
372 struct rds_connection *rds_conn_create(struct net *net,
373 				       const struct in6_addr *laddr,
374 				       const struct in6_addr *faddr,
375 				       struct rds_transport *trans, u8 tos,
376 				       gfp_t gfp, int dev_if)
377 {
378 	return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 0, dev_if);
379 }
380 EXPORT_SYMBOL_GPL(rds_conn_create);
381 
382 struct rds_connection *rds_conn_create_outgoing(struct net *net,
383 						const struct in6_addr *laddr,
384 						const struct in6_addr *faddr,
385 						struct rds_transport *trans,
386 						u8 tos, gfp_t gfp, int dev_if)
387 {
388 	return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 1, dev_if);
389 }
390 EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
391 
392 void rds_conn_shutdown(struct rds_conn_path *cp)
393 {
394 	struct rds_connection *conn = cp->cp_conn;
395 
396 	/* shut it down unless it's down already */
397 	if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
398 		/*
399 		 * Quiesce the connection mgmt handlers before we start tearing
400 		 * things down. We don't hold the mutex for the entire
401 		 * duration of the shutdown operation, else we may be
402 		 * deadlocking with the CM handler. Instead, the CM event
403 		 * handler is supposed to check for state DISCONNECTING
404 		 */
405 		mutex_lock(&cp->cp_cm_lock);
406 		if (!rds_conn_path_transition(cp, RDS_CONN_UP,
407 					      RDS_CONN_DISCONNECTING) &&
408 		    !rds_conn_path_transition(cp, RDS_CONN_ERROR,
409 					      RDS_CONN_DISCONNECTING) &&
410 		    !rds_conn_path_transition(cp, RDS_CONN_RESETTING,
411 					      RDS_CONN_DISCONNECTING)) {
412 			rds_conn_path_error(cp,
413 					    "shutdown called in state %d\n",
414 					    atomic_read(&cp->cp_state));
415 			mutex_unlock(&cp->cp_cm_lock);
416 			return;
417 		}
418 		mutex_unlock(&cp->cp_cm_lock);
419 
420 		/* Quiesce the transmit and receive-refill paths by
421 		 * acquiring their bit locks, not merely waiting for
422 		 * them to be released: with a plain wait, either path
423 		 * can re-take its lock the instant after we sample it
424 		 * clear and then run concurrently with the transport
425 		 * shutdown and the path reset below.  Holding both
426 		 * locks across the teardown makes that structurally
427 		 * impossible.
428 		 */
429 		wait_event(cp->cp_waitq,
430 			   !test_and_set_bit_lock(RDS_IN_XMIT, &cp->cp_flags));
431 		wait_event(cp->cp_waitq,
432 			   !test_and_set_bit(RDS_RECV_REFILL, &cp->cp_flags));
433 
434 		conn->c_trans->conn_path_shutdown(cp);
435 		rds_conn_path_reset(cp);
436 
437 		/* Release the two locks and wake any waiter (e.g.
438 		 * rds_tcp_reset_callbacks()) that blocked on them while
439 		 * we held them.  The unlock orders the transport's ring
440 		 * re-initialization and the path reset above before
441 		 * either bit is seen clear.  rds_conn_path_reset() leaves
442 		 * both bits alone: ownership ends here, not inside the
443 		 * reset.
444 		 */
445 		clear_bit_unlock(RDS_IN_XMIT, &cp->cp_flags);
446 		clear_bit_unlock(RDS_RECV_REFILL, &cp->cp_flags);
447 		wake_up_all(&cp->cp_waitq);
448 
449 		if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING,
450 					      RDS_CONN_DOWN)) {
451 			/* The path was dropped again while we tore it
452 			 * down: by a socket state-change callback in
453 			 * irq context on receipt of a FIN, or by an
454 			 * accept that claimed the path just before a
455 			 * drop put it back to RDS_CONN_ERROR and then
456 			 * installed a fresh socket on it.  Unless a
457 			 * pending destroy suppressed it, the drop also
458 			 * queued another shutdown pass, and that pass
459 			 * must run, because it is what tears down
460 			 * whatever attached to the path after the
461 			 * transport shutdown above sampled its state.
462 			 * Consuming the RDS_CONN_ERROR here would turn
463 			 * that pass into a no-op: leave the state
464 			 * alone, and let the pass finish the job.
465 			 *
466 			 * Quiesce the reconnect timer before bailing
467 			 * out, though.  When a pending destroy did
468 			 * suppress the queue, no later pass runs, and
469 			 * rds_conn_path_destroy() is about to flush
470 			 * cp_down_w and free the path: it must not
471 			 * find cp_conn_w still armed.  A successor
472 			 * pass, when there is one, re-arms the
473 			 * reconnect from its own tail.
474 			 */
475 			cancel_delayed_work_sync(&cp->cp_conn_w);
476 			clear_bit(RDS_RECONNECT_PENDING, &cp->cp_flags);
477 
478 			if (rds_conn_path_state(cp) == RDS_CONN_ERROR)
479 				return;
480 			/* No current cp_state writer leaves a
481 			 * DISCONNECTING path in any state but
482 			 * RDS_CONN_ERROR; report loudly if one ever
483 			 * does.
484 			 */
485 			rds_conn_path_error(cp, "%s: failed to transition "
486 					    "to state DOWN, current state "
487 					    "is %d\n", __func__,
488 					    atomic_read(&cp->cp_state));
489 			return;
490 		}
491 	}
492 
493 	/* Then reconnect if it's still live.
494 	 * The passive side of an IB loopback connection is never added
495 	 * to the conn hash, so we never trigger a reconnect on this
496 	 * conn - the reconnect is always triggered by the active peer. */
497 	cancel_delayed_work_sync(&cp->cp_conn_w);
498 
499 	clear_bit(RDS_RECONNECT_PENDING, &cp->cp_flags);
500 	rcu_read_lock();
501 	if (!hlist_unhashed(&conn->c_hash_node)) {
502 		rcu_read_unlock();
503 		if (conn->c_trans->t_mp_capable &&
504 		    cp->cp_index == 0)
505 			rds_send_ping(conn, 0);
506 		rds_queue_reconnect(cp);
507 	} else {
508 		rcu_read_unlock();
509 	}
510 
511 	/* we do not hold the socket lock here but it is safe because
512 	 * fan-out is disabled when calling conn_slots_available()
513 	 */
514 	if (conn->c_trans->conn_slots_available)
515 		conn->c_trans->conn_slots_available(conn, false);
516 }
517 
518 /* destroy a single rds_conn_path. rds_conn_destroy() iterates over
519  * all paths using rds_conn_path_destroy()
520  */
521 static void rds_conn_path_destroy(struct rds_conn_path *cp)
522 {
523 	struct rds_message *rm, *rtmp;
524 
525 	if (!cp->cp_transport_data)
526 		return;
527 
528 	/* make sure lingering queued work won't try to ref the conn */
529 	cancel_delayed_work_sync(&cp->cp_send_w);
530 	cancel_delayed_work_sync(&cp->cp_recv_w);
531 
532 	rds_conn_path_drop(cp, true);
533 	flush_work(&cp->cp_down_w);
534 
535 	/* tear down queued messages */
536 	list_for_each_entry_safe(rm, rtmp,
537 				 &cp->cp_send_queue,
538 				 m_conn_item) {
539 		list_del_init(&rm->m_conn_item);
540 		BUG_ON(!list_empty(&rm->m_sock_item));
541 		rds_message_put(rm);
542 	}
543 	if (cp->cp_xmit_rm)
544 		rds_message_put(cp->cp_xmit_rm);
545 
546 	WARN_ON(delayed_work_pending(&cp->cp_send_w));
547 	WARN_ON(delayed_work_pending(&cp->cp_recv_w));
548 	WARN_ON(delayed_work_pending(&cp->cp_conn_w));
549 	WARN_ON(work_pending(&cp->cp_down_w));
550 
551 	if (cp->cp_wq != rds_wq) {
552 		destroy_workqueue(cp->cp_wq);
553 		cp->cp_wq = NULL;
554 	}
555 
556 	cp->cp_conn->c_trans->conn_free(cp->cp_transport_data);
557 }
558 
559 /*
560  * Stop and free a connection.
561  *
562  * This can only be used in very limited circumstances.  It assumes that once
563  * the conn has been shutdown that no one else is referencing the connection.
564  * We can only ensure this in the rmmod path in the current code.
565  */
566 void rds_conn_destroy(struct rds_connection *conn)
567 {
568 	unsigned long flags;
569 	int i;
570 	struct rds_conn_path *cp;
571 	int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
572 
573 	rdsdebug("freeing conn %p for %pI4 -> "
574 		 "%pI4\n", conn, &conn->c_laddr,
575 		 &conn->c_faddr);
576 
577 	/* Ensure conn will not be scheduled for reconnect */
578 	spin_lock_irq(&rds_conn_lock);
579 	hlist_del_init_rcu(&conn->c_hash_node);
580 	spin_unlock_irq(&rds_conn_lock);
581 	synchronize_rcu();
582 
583 	/* shut the connection down */
584 	for (i = 0; i < npaths; i++) {
585 		cp = &conn->c_path[i];
586 		rds_conn_path_destroy(cp);
587 		BUG_ON(!list_empty(&cp->cp_retrans));
588 	}
589 
590 	/*
591 	 * The congestion maps aren't freed up here.  They're
592 	 * freed by rds_cong_exit() after all the connections
593 	 * have been freed.
594 	 */
595 	rds_cong_remove_conn(conn);
596 
597 	kfree(conn->c_path);
598 	kmem_cache_free(rds_conn_slab, conn);
599 
600 	spin_lock_irqsave(&rds_conn_lock, flags);
601 	rds_conn_count--;
602 	spin_unlock_irqrestore(&rds_conn_lock, flags);
603 }
604 EXPORT_SYMBOL_GPL(rds_conn_destroy);
605 
606 static void __rds_inc_msg_cp(struct rds_incoming *inc,
607 			     struct rds_info_iterator *iter,
608 			     void *saddr, void *daddr, int flip, bool isv6)
609 {
610 #if IS_ENABLED(CONFIG_IPV6)
611 	if (isv6)
612 		rds6_inc_info_copy(inc, iter, saddr, daddr, flip);
613 	else
614 #endif
615 		rds_inc_info_copy(inc, iter, *(__be32 *)saddr,
616 				  *(__be32 *)daddr, flip);
617 }
618 
619 static void rds_conn_message_info_cmn(struct socket *sock, unsigned int len,
620 				      struct rds_info_iterator *iter,
621 				      struct rds_info_lengths *lens,
622 				      int want_send, bool isv6)
623 {
624 	struct net *net = sock_net(sock->sk);
625 	struct hlist_head *head;
626 	struct list_head *list;
627 	struct rds_connection *conn;
628 	struct rds_message *rm;
629 	unsigned int total = 0;
630 	unsigned long flags;
631 	size_t i;
632 	int j;
633 
634 	if (isv6)
635 		len /= sizeof(struct rds6_info_message);
636 	else
637 		len /= sizeof(struct rds_info_message);
638 
639 	rcu_read_lock();
640 
641 	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
642 	     i++, head++) {
643 		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
644 			struct rds_conn_path *cp;
645 			int npaths;
646 
647 			/* Only show connections in the caller's netns. */
648 			if (!net_eq(rds_conn_net(conn), net))
649 				continue;
650 			if (!isv6 && conn->c_isv6)
651 				continue;
652 
653 			npaths = (conn->c_trans->t_mp_capable ?
654 				 RDS_MPATH_WORKERS : 1);
655 
656 			for (j = 0; j < npaths; j++) {
657 				cp = &conn->c_path[j];
658 				if (want_send)
659 					list = &cp->cp_send_queue;
660 				else
661 					list = &cp->cp_retrans;
662 
663 				spin_lock_irqsave(&cp->cp_lock, flags);
664 
665 				/* XXX too lazy to maintain counts.. */
666 				list_for_each_entry(rm, list, m_conn_item) {
667 					total++;
668 					if (total <= len)
669 						__rds_inc_msg_cp(&rm->m_inc,
670 								 iter,
671 								 &conn->c_laddr,
672 								 &conn->c_faddr,
673 								 0, isv6);
674 				}
675 
676 				spin_unlock_irqrestore(&cp->cp_lock, flags);
677 			}
678 		}
679 	}
680 	rcu_read_unlock();
681 
682 	lens->nr = total;
683 	if (isv6)
684 		lens->each = sizeof(struct rds6_info_message);
685 	else
686 		lens->each = sizeof(struct rds_info_message);
687 }
688 
689 static void rds_conn_message_info(struct socket *sock, unsigned int len,
690 				  struct rds_info_iterator *iter,
691 				  struct rds_info_lengths *lens,
692 				  int want_send)
693 {
694 	rds_conn_message_info_cmn(sock, len, iter, lens, want_send, false);
695 }
696 
697 #if IS_ENABLED(CONFIG_IPV6)
698 static void rds6_conn_message_info(struct socket *sock, unsigned int len,
699 				   struct rds_info_iterator *iter,
700 				   struct rds_info_lengths *lens,
701 				   int want_send)
702 {
703 	rds_conn_message_info_cmn(sock, len, iter, lens, want_send, true);
704 }
705 #endif
706 
707 static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
708 				       struct rds_info_iterator *iter,
709 				       struct rds_info_lengths *lens)
710 {
711 	rds_conn_message_info(sock, len, iter, lens, 1);
712 }
713 
714 #if IS_ENABLED(CONFIG_IPV6)
715 static void rds6_conn_message_info_send(struct socket *sock, unsigned int len,
716 					struct rds_info_iterator *iter,
717 					struct rds_info_lengths *lens)
718 {
719 	rds6_conn_message_info(sock, len, iter, lens, 1);
720 }
721 #endif
722 
723 static void rds_conn_message_info_retrans(struct socket *sock,
724 					  unsigned int len,
725 					  struct rds_info_iterator *iter,
726 					  struct rds_info_lengths *lens)
727 {
728 	rds_conn_message_info(sock, len, iter, lens, 0);
729 }
730 
731 #if IS_ENABLED(CONFIG_IPV6)
732 static void rds6_conn_message_info_retrans(struct socket *sock,
733 					   unsigned int len,
734 					   struct rds_info_iterator *iter,
735 					   struct rds_info_lengths *lens)
736 {
737 	rds6_conn_message_info(sock, len, iter, lens, 0);
738 }
739 #endif
740 
741 void rds_for_each_conn_info(struct socket *sock, unsigned int len,
742 			  struct rds_info_iterator *iter,
743 			  struct rds_info_lengths *lens,
744 			  int (*visitor)(struct rds_connection *, void *),
745 			  u64 *buffer,
746 			  size_t item_len)
747 {
748 	struct net *net = sock_net(sock->sk);
749 	struct hlist_head *head;
750 	struct rds_connection *conn;
751 	size_t i;
752 
753 	rcu_read_lock();
754 
755 	lens->nr = 0;
756 	lens->each = item_len;
757 
758 	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
759 	     i++, head++) {
760 		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
761 			/* Only show connections in the caller's netns. */
762 			if (!net_eq(rds_conn_net(conn), net))
763 				continue;
764 
765 			/* Zero the per-item buffer before handing it to the
766 			 * visitor so any field the visitor does not write -
767 			 * including implicit alignment padding - cannot leak
768 			 * stack contents to user space via rds_info_copy().
769 			 */
770 			memset(buffer, 0, item_len);
771 
772 			/* XXX no c_lock usage.. */
773 			if (!visitor(conn, buffer))
774 				continue;
775 
776 			/* We copy as much as we can fit in the buffer,
777 			 * but we count all items so that the caller
778 			 * can resize the buffer. */
779 			if (len >= item_len) {
780 				rds_info_copy(iter, buffer, item_len);
781 				len -= item_len;
782 			}
783 			lens->nr++;
784 		}
785 	}
786 	rcu_read_unlock();
787 }
788 EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
789 
790 static void rds_walk_conn_path_info(struct socket *sock, unsigned int len,
791 				    struct rds_info_iterator *iter,
792 				    struct rds_info_lengths *lens,
793 				    int (*visitor)(struct rds_conn_path *, void *),
794 				    u64 *buffer,
795 				    size_t item_len)
796 {
797 	struct net *net = sock_net(sock->sk);
798 	struct hlist_head *head;
799 	struct rds_connection *conn;
800 	size_t i;
801 
802 	rcu_read_lock();
803 
804 	lens->nr = 0;
805 	lens->each = item_len;
806 
807 	for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
808 	     i++, head++) {
809 		hlist_for_each_entry_rcu(conn, head, c_hash_node) {
810 			struct rds_conn_path *cp;
811 
812 			/* Only show connections in the caller's netns. */
813 			if (!net_eq(rds_conn_net(conn), net))
814 				continue;
815 
816 			/* XXX We only copy the information from the first
817 			 * path for now.  The problem is that if there are
818 			 * more than one underlying paths, we cannot report
819 			 * information of all of them using the existing
820 			 * API.  For example, there is only one next_tx_seq,
821 			 * which path's next_tx_seq should we report?  It is
822 			 * a bug in the design of MPRDS.
823 			 */
824 			cp = conn->c_path;
825 
826 			/* Zero the per-item buffer for the same reason as
827 			 * rds_for_each_conn_info(): any byte the visitor
828 			 * does not write (including alignment padding) must
829 			 * not leak stack contents via rds_info_copy().
830 			 */
831 			memset(buffer, 0, item_len);
832 
833 			/* XXX no cp_lock usage.. */
834 			if (!visitor(cp, buffer))
835 				continue;
836 
837 			/* We copy as much as we can fit in the buffer,
838 			 * but we count all items so that the caller
839 			 * can resize the buffer.
840 			 */
841 			if (len >= item_len) {
842 				rds_info_copy(iter, buffer, item_len);
843 				len -= item_len;
844 			}
845 			lens->nr++;
846 		}
847 	}
848 	rcu_read_unlock();
849 }
850 
851 static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
852 {
853 	struct rds_info_connection *cinfo = buffer;
854 	struct rds_connection *conn = cp->cp_conn;
855 
856 	if (conn->c_isv6)
857 		return 0;
858 
859 	cinfo->next_tx_seq = cp->cp_next_tx_seq;
860 	cinfo->next_rx_seq = cp->cp_next_rx_seq;
861 	cinfo->laddr = conn->c_laddr.s6_addr32[3];
862 	cinfo->faddr = conn->c_faddr.s6_addr32[3];
863 	cinfo->tos = conn->c_tos;
864 	strscpy_pad(cinfo->transport, conn->c_trans->t_name);
865 	cinfo->flags = 0;
866 
867 	rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
868 			  SENDING);
869 	/* XXX Future: return the state rather than these funky bits */
870 	rds_conn_info_set(cinfo->flags,
871 			  atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
872 			  CONNECTING);
873 	rds_conn_info_set(cinfo->flags,
874 			  atomic_read(&cp->cp_state) == RDS_CONN_UP,
875 			  CONNECTED);
876 	return 1;
877 }
878 
879 #if IS_ENABLED(CONFIG_IPV6)
880 static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
881 {
882 	struct rds6_info_connection *cinfo6 = buffer;
883 	struct rds_connection *conn = cp->cp_conn;
884 
885 	cinfo6->next_tx_seq = cp->cp_next_tx_seq;
886 	cinfo6->next_rx_seq = cp->cp_next_rx_seq;
887 	cinfo6->laddr = conn->c_laddr;
888 	cinfo6->faddr = conn->c_faddr;
889 	strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
890 	cinfo6->flags = 0;
891 
892 	rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
893 			  SENDING);
894 	/* XXX Future: return the state rather than these funky bits */
895 	rds_conn_info_set(cinfo6->flags,
896 			  atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
897 			  CONNECTING);
898 	rds_conn_info_set(cinfo6->flags,
899 			  atomic_read(&cp->cp_state) == RDS_CONN_UP,
900 			  CONNECTED);
901 	/* Just return 1 as there is no error case. This is a helper function
902 	 * for rds_walk_conn_path_info() and it wants a return value.
903 	 */
904 	return 1;
905 }
906 #endif
907 
908 static void rds_conn_info(struct socket *sock, unsigned int len,
909 			  struct rds_info_iterator *iter,
910 			  struct rds_info_lengths *lens)
911 {
912 	u64 buffer[(sizeof(struct rds_info_connection) + 7) / 8];
913 
914 	rds_walk_conn_path_info(sock, len, iter, lens,
915 				rds_conn_info_visitor,
916 				buffer,
917 				sizeof(struct rds_info_connection));
918 }
919 
920 #if IS_ENABLED(CONFIG_IPV6)
921 static void rds6_conn_info(struct socket *sock, unsigned int len,
922 			   struct rds_info_iterator *iter,
923 			   struct rds_info_lengths *lens)
924 {
925 	u64 buffer[(sizeof(struct rds6_info_connection) + 7) / 8];
926 
927 	rds_walk_conn_path_info(sock, len, iter, lens,
928 				rds6_conn_info_visitor,
929 				buffer,
930 				sizeof(struct rds6_info_connection));
931 }
932 #endif
933 
934 int rds_conn_init(void)
935 {
936 	int ret;
937 
938 	ret = rds_loop_net_init(); /* register pernet callback */
939 	if (ret)
940 		return ret;
941 
942 	rds_conn_slab = KMEM_CACHE(rds_connection, 0);
943 	if (!rds_conn_slab) {
944 		rds_loop_net_exit();
945 		return -ENOMEM;
946 	}
947 
948 	rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
949 	rds_info_register_func(RDS_INFO_SEND_MESSAGES,
950 			       rds_conn_message_info_send);
951 	rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
952 			       rds_conn_message_info_retrans);
953 #if IS_ENABLED(CONFIG_IPV6)
954 	rds_info_register_func(RDS6_INFO_CONNECTIONS, rds6_conn_info);
955 	rds_info_register_func(RDS6_INFO_SEND_MESSAGES,
956 			       rds6_conn_message_info_send);
957 	rds_info_register_func(RDS6_INFO_RETRANS_MESSAGES,
958 			       rds6_conn_message_info_retrans);
959 #endif
960 	return 0;
961 }
962 
963 void rds_conn_exit(void)
964 {
965 	rds_loop_net_exit(); /* unregister pernet callback */
966 	rds_loop_exit();
967 
968 	WARN_ON(!hlist_empty(rds_conn_hash));
969 
970 	kmem_cache_destroy(rds_conn_slab);
971 
972 	rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
973 	rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
974 				 rds_conn_message_info_send);
975 	rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
976 				 rds_conn_message_info_retrans);
977 #if IS_ENABLED(CONFIG_IPV6)
978 	rds_info_deregister_func(RDS6_INFO_CONNECTIONS, rds6_conn_info);
979 	rds_info_deregister_func(RDS6_INFO_SEND_MESSAGES,
980 				 rds6_conn_message_info_send);
981 	rds_info_deregister_func(RDS6_INFO_RETRANS_MESSAGES,
982 				 rds6_conn_message_info_retrans);
983 #endif
984 }
985 
986 /*
987  * Force a disconnect
988  */
989 void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy)
990 {
991 	atomic_set(&cp->cp_state, RDS_CONN_ERROR);
992 
993 	rcu_read_lock();
994 	if (!destroy && rds_destroy_pending(cp->cp_conn)) {
995 		rcu_read_unlock();
996 		return;
997 	}
998 	queue_work(cp->cp_wq, &cp->cp_down_w);
999 	rcu_read_unlock();
1000 }
1001 EXPORT_SYMBOL_GPL(rds_conn_path_drop);
1002 
1003 void rds_conn_drop(struct rds_connection *conn)
1004 {
1005 	WARN_ON(conn->c_trans->t_mp_capable);
1006 	rds_conn_path_drop(&conn->c_path[0], false);
1007 }
1008 EXPORT_SYMBOL_GPL(rds_conn_drop);
1009 
1010 /*
1011  * If the connection is down, trigger a connect. We may have scheduled a
1012  * delayed reconnect however - in this case we should not interfere.
1013  */
1014 void rds_conn_path_connect_if_down(struct rds_conn_path *cp)
1015 {
1016 	rcu_read_lock();
1017 	if (rds_destroy_pending(cp->cp_conn)) {
1018 		rcu_read_unlock();
1019 		return;
1020 	}
1021 	if (rds_conn_path_state(cp) == RDS_CONN_DOWN &&
1022 	    !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags))
1023 		queue_delayed_work(cp->cp_wq, &cp->cp_conn_w, 0);
1024 	rcu_read_unlock();
1025 }
1026 EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down);
1027 
1028 /* Check connectivity of all paths
1029  */
1030 void rds_check_all_paths(struct rds_connection *conn)
1031 {
1032 	int i = 0;
1033 
1034 	do {
1035 		rds_conn_path_connect_if_down(&conn->c_path[i]);
1036 	} while (++i < conn->c_npaths);
1037 }
1038 
1039 void rds_conn_connect_if_down(struct rds_connection *conn)
1040 {
1041 	WARN_ON(conn->c_trans->t_mp_capable);
1042 	rds_conn_path_connect_if_down(&conn->c_path[0]);
1043 }
1044 EXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
1045 
1046 void
1047 __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...)
1048 {
1049 	va_list ap;
1050 
1051 	va_start(ap, fmt);
1052 	vprintk(fmt, ap);
1053 	va_end(ap);
1054 
1055 	rds_conn_path_drop(cp, false);
1056 }
1057