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
rds_conn_bucket(const struct in6_addr * laddr,const struct in6_addr * faddr)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 */
rds_conn_lookup(struct net * net,struct hlist_head * head,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,u8 tos,int dev_if)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 */
rds_conn_path_reset(struct rds_conn_path * cp)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
__rds_conn_path_init(struct rds_connection * conn,struct rds_conn_path * cp,bool is_outgoing)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 */
__rds_conn_create(struct net * net,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,gfp_t gfp,u8 tos,int is_outgoing,int dev_if)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 /* The transport may just have been swapped for loopback; size the
280 * set of paths - which is also what rds_conn_destroy() tears down
281 * again - by the transport the connection actually uses.
282 */
283 npaths = (trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
284
285 init_waitqueue_head(&conn->c_hs_waitq);
286 for (i = 0; i < npaths; i++) {
287 __rds_conn_path_init(conn, &conn->c_path[i],
288 is_outgoing);
289 conn->c_path[i].cp_index = i;
290 conn->c_path[i].cp_wq =
291 alloc_ordered_workqueue("krds_cp_wq#%lu/%d", 0,
292 rds_conn_count, i);
293 if (!conn->c_path[i].cp_wq)
294 conn->c_path[i].cp_wq = rds_wq;
295 }
296 rcu_read_lock();
297 if (rds_destroy_pending(conn))
298 ret = -ENETDOWN;
299 else
300 ret = trans->conn_alloc(conn, GFP_ATOMIC);
301 if (ret) {
302 rcu_read_unlock();
303 free_cp = conn->c_path;
304 kmem_cache_free(rds_conn_slab, conn);
305 conn = ERR_PTR(ret);
306 goto out;
307 }
308
309 rdsdebug("allocated conn %p for %pI6c -> %pI6c over %s %s\n",
310 conn, laddr, faddr,
311 strnlen(trans->t_name, sizeof(trans->t_name)) ?
312 trans->t_name : "[unknown]", is_outgoing ? "(outgoing)" : "");
313
314 /*
315 * Since we ran without holding the conn lock, someone could
316 * have created the same conn (either normal or passive) in the
317 * interim. We check while holding the lock. If we won, we complete
318 * init and return our conn. If we lost, we rollback and return the
319 * other one.
320 */
321 spin_lock_irqsave(&rds_conn_lock, flags);
322 if (parent) {
323 /* Creating passive conn */
324 if (parent->c_passive) {
325 trans->conn_free(conn->c_path[0].cp_transport_data);
326 free_cp = conn->c_path;
327 kmem_cache_free(rds_conn_slab, conn);
328 conn = parent->c_passive;
329 } else {
330 parent->c_passive = conn;
331 rds_cong_add_conn(conn);
332 rds_conn_count++;
333 }
334 } else {
335 /* Creating normal conn */
336 struct rds_connection *found;
337
338 found = rds_conn_lookup(net, head, laddr, faddr, trans,
339 tos, dev_if);
340 if (found) {
341 struct rds_conn_path *cp;
342 int i;
343
344 for (i = 0; i < npaths; i++) {
345 cp = &conn->c_path[i];
346 /* The ->conn_alloc invocation may have
347 * allocated resource for all paths, so all
348 * of them may have to be freed here.
349 */
350 if (cp->cp_transport_data)
351 trans->conn_free(cp->cp_transport_data);
352 }
353 free_cp = conn->c_path;
354 kmem_cache_free(rds_conn_slab, conn);
355 conn = found;
356 } else {
357 conn->c_my_gen_num = rds_gen_num;
358 conn->c_peer_gen_num = 0;
359 hlist_add_head_rcu(&conn->c_hash_node, head);
360 rds_cong_add_conn(conn);
361 rds_conn_count++;
362 }
363 }
364 spin_unlock_irqrestore(&rds_conn_lock, flags);
365 rcu_read_unlock();
366
367 out:
368 if (free_cp) {
369 for (i = 0; i < npaths; i++)
370 if (free_cp[i].cp_wq != rds_wq)
371 destroy_workqueue(free_cp[i].cp_wq);
372 kfree(free_cp);
373 }
374
375 return conn;
376 }
377
rds_conn_create(struct net * net,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,u8 tos,gfp_t gfp,int dev_if)378 struct rds_connection *rds_conn_create(struct net *net,
379 const struct in6_addr *laddr,
380 const struct in6_addr *faddr,
381 struct rds_transport *trans, u8 tos,
382 gfp_t gfp, int dev_if)
383 {
384 return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 0, dev_if);
385 }
386 EXPORT_SYMBOL_GPL(rds_conn_create);
387
rds_conn_create_outgoing(struct net * net,const struct in6_addr * laddr,const struct in6_addr * faddr,struct rds_transport * trans,u8 tos,gfp_t gfp,int dev_if)388 struct rds_connection *rds_conn_create_outgoing(struct net *net,
389 const struct in6_addr *laddr,
390 const struct in6_addr *faddr,
391 struct rds_transport *trans,
392 u8 tos, gfp_t gfp, int dev_if)
393 {
394 return __rds_conn_create(net, laddr, faddr, trans, gfp, tos, 1, dev_if);
395 }
396 EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
397
rds_conn_shutdown(struct rds_conn_path * cp)398 void rds_conn_shutdown(struct rds_conn_path *cp)
399 {
400 struct rds_connection *conn = cp->cp_conn;
401
402 /* shut it down unless it's down already */
403 if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
404 /*
405 * Quiesce the connection mgmt handlers before we start tearing
406 * things down. We don't hold the mutex for the entire
407 * duration of the shutdown operation, else we may be
408 * deadlocking with the CM handler. Instead, the CM event
409 * handler is supposed to check for state DISCONNECTING
410 */
411 mutex_lock(&cp->cp_cm_lock);
412 if (!rds_conn_path_transition(cp, RDS_CONN_UP,
413 RDS_CONN_DISCONNECTING) &&
414 !rds_conn_path_transition(cp, RDS_CONN_ERROR,
415 RDS_CONN_DISCONNECTING) &&
416 !rds_conn_path_transition(cp, RDS_CONN_RESETTING,
417 RDS_CONN_DISCONNECTING)) {
418 rds_conn_path_error(cp,
419 "shutdown called in state %d\n",
420 atomic_read(&cp->cp_state));
421 mutex_unlock(&cp->cp_cm_lock);
422 return;
423 }
424 mutex_unlock(&cp->cp_cm_lock);
425
426 /* Quiesce the transmit and receive-refill paths by
427 * acquiring their bit locks, not merely waiting for
428 * them to be released: with a plain wait, either path
429 * can re-take its lock the instant after we sample it
430 * clear and then run concurrently with the transport
431 * shutdown and the path reset below. Holding both
432 * locks across the teardown makes that structurally
433 * impossible.
434 */
435 wait_event(cp->cp_waitq,
436 !test_and_set_bit_lock(RDS_IN_XMIT, &cp->cp_flags));
437 wait_event(cp->cp_waitq,
438 !test_and_set_bit(RDS_RECV_REFILL, &cp->cp_flags));
439
440 conn->c_trans->conn_path_shutdown(cp);
441 rds_conn_path_reset(cp);
442
443 /* Release the two locks and wake any waiter (e.g.
444 * rds_tcp_reset_callbacks()) that blocked on them while
445 * we held them. The unlock orders the transport's ring
446 * re-initialization and the path reset above before
447 * either bit is seen clear. rds_conn_path_reset() leaves
448 * both bits alone: ownership ends here, not inside the
449 * reset.
450 */
451 clear_bit_unlock(RDS_IN_XMIT, &cp->cp_flags);
452 clear_bit_unlock(RDS_RECV_REFILL, &cp->cp_flags);
453 wake_up_all(&cp->cp_waitq);
454
455 if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING,
456 RDS_CONN_DOWN)) {
457 /* The path was dropped again while we tore it
458 * down: by a socket state-change callback in
459 * irq context on receipt of a FIN, or by an
460 * accept that claimed the path just before a
461 * drop put it back to RDS_CONN_ERROR and then
462 * installed a fresh socket on it. Unless a
463 * pending destroy suppressed it, the drop also
464 * queued another shutdown pass, and that pass
465 * must run, because it is what tears down
466 * whatever attached to the path after the
467 * transport shutdown above sampled its state.
468 * Consuming the RDS_CONN_ERROR here would turn
469 * that pass into a no-op: leave the state
470 * alone, and let the pass finish the job.
471 *
472 * Quiesce the reconnect timer before bailing
473 * out, though. When a pending destroy did
474 * suppress the queue, no later pass runs, and
475 * rds_conn_path_destroy() is about to flush
476 * cp_down_w and free the path: it must not
477 * find cp_conn_w still armed. A successor
478 * pass, when there is one, re-arms the
479 * reconnect from its own tail.
480 */
481 cancel_delayed_work_sync(&cp->cp_conn_w);
482 clear_bit(RDS_RECONNECT_PENDING, &cp->cp_flags);
483
484 if (rds_conn_path_state(cp) == RDS_CONN_ERROR)
485 return;
486 /* No current cp_state writer leaves a
487 * DISCONNECTING path in any state but
488 * RDS_CONN_ERROR; report loudly if one ever
489 * does.
490 */
491 rds_conn_path_error(cp, "%s: failed to transition "
492 "to state DOWN, current state "
493 "is %d\n", __func__,
494 atomic_read(&cp->cp_state));
495 return;
496 }
497 }
498
499 /* Then reconnect if it's still live.
500 * The passive side of an IB loopback connection is never added
501 * to the conn hash, so we never trigger a reconnect on this
502 * conn - the reconnect is always triggered by the active peer. */
503 cancel_delayed_work_sync(&cp->cp_conn_w);
504
505 clear_bit(RDS_RECONNECT_PENDING, &cp->cp_flags);
506 rcu_read_lock();
507 if (!hlist_unhashed(&conn->c_hash_node)) {
508 rcu_read_unlock();
509 if (conn->c_trans->t_mp_capable &&
510 cp->cp_index == 0)
511 rds_send_ping(conn, 0);
512 rds_queue_reconnect(cp);
513 } else {
514 rcu_read_unlock();
515 }
516
517 /* we do not hold the socket lock here but it is safe because
518 * fan-out is disabled when calling conn_slots_available()
519 */
520 if (conn->c_trans->conn_slots_available)
521 conn->c_trans->conn_slots_available(conn, false);
522 }
523
524 /* destroy a single rds_conn_path. rds_conn_destroy() iterates over
525 * all paths using rds_conn_path_destroy()
526 */
rds_conn_path_destroy(struct rds_conn_path * cp)527 static void rds_conn_path_destroy(struct rds_conn_path *cp)
528 {
529 struct rds_message *rm, *rtmp;
530
531 if (!cp->cp_transport_data)
532 return;
533
534 /* make sure lingering queued work won't try to ref the conn */
535 cancel_delayed_work_sync(&cp->cp_send_w);
536 cancel_delayed_work_sync(&cp->cp_recv_w);
537
538 rds_conn_path_drop(cp, true);
539 flush_work(&cp->cp_down_w);
540
541 /* tear down queued messages */
542 list_for_each_entry_safe(rm, rtmp,
543 &cp->cp_send_queue,
544 m_conn_item) {
545 list_del_init(&rm->m_conn_item);
546 BUG_ON(!list_empty(&rm->m_sock_item));
547 rds_message_put(rm);
548 }
549 if (cp->cp_xmit_rm)
550 rds_message_put(cp->cp_xmit_rm);
551
552 WARN_ON(delayed_work_pending(&cp->cp_send_w));
553 WARN_ON(delayed_work_pending(&cp->cp_recv_w));
554 WARN_ON(delayed_work_pending(&cp->cp_conn_w));
555 WARN_ON(work_pending(&cp->cp_down_w));
556
557 if (cp->cp_wq != rds_wq) {
558 destroy_workqueue(cp->cp_wq);
559 cp->cp_wq = NULL;
560 }
561
562 cp->cp_conn->c_trans->conn_free(cp->cp_transport_data);
563 }
564
565 /*
566 * Stop and free a connection.
567 *
568 * This can only be used in very limited circumstances. It assumes that once
569 * the conn has been shutdown that no one else is referencing the connection.
570 * We can only ensure this in the rmmod path in the current code.
571 */
rds_conn_destroy(struct rds_connection * conn)572 void rds_conn_destroy(struct rds_connection *conn)
573 {
574 unsigned long flags;
575 int i;
576 struct rds_conn_path *cp;
577 int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
578
579 rdsdebug("freeing conn %p for %pI4 -> "
580 "%pI4\n", conn, &conn->c_laddr,
581 &conn->c_faddr);
582
583 /* Ensure conn will not be scheduled for reconnect */
584 spin_lock_irq(&rds_conn_lock);
585 hlist_del_init_rcu(&conn->c_hash_node);
586 spin_unlock_irq(&rds_conn_lock);
587 synchronize_rcu();
588
589 /* shut the connection down */
590 for (i = 0; i < npaths; i++) {
591 cp = &conn->c_path[i];
592 rds_conn_path_destroy(cp);
593 BUG_ON(!list_empty(&cp->cp_retrans));
594 }
595
596 /*
597 * The congestion maps aren't freed up here. They're
598 * freed by rds_cong_exit() after all the connections
599 * have been freed.
600 */
601 rds_cong_remove_conn(conn);
602
603 kfree(conn->c_path);
604 kmem_cache_free(rds_conn_slab, conn);
605
606 spin_lock_irqsave(&rds_conn_lock, flags);
607 rds_conn_count--;
608 spin_unlock_irqrestore(&rds_conn_lock, flags);
609 }
610 EXPORT_SYMBOL_GPL(rds_conn_destroy);
611
__rds_inc_msg_cp(struct rds_incoming * inc,struct rds_info_iterator * iter,void * saddr,void * daddr,int flip,bool isv6)612 static void __rds_inc_msg_cp(struct rds_incoming *inc,
613 struct rds_info_iterator *iter,
614 void *saddr, void *daddr, int flip, bool isv6)
615 {
616 #if IS_ENABLED(CONFIG_IPV6)
617 if (isv6)
618 rds6_inc_info_copy(inc, iter, saddr, daddr, flip);
619 else
620 #endif
621 rds_inc_info_copy(inc, iter, *(__be32 *)saddr,
622 *(__be32 *)daddr, flip);
623 }
624
rds_conn_message_info_cmn(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int want_send,bool isv6)625 static void rds_conn_message_info_cmn(struct socket *sock, unsigned int len,
626 struct rds_info_iterator *iter,
627 struct rds_info_lengths *lens,
628 int want_send, bool isv6)
629 {
630 struct net *net = sock_net(sock->sk);
631 struct hlist_head *head;
632 struct list_head *list;
633 struct rds_connection *conn;
634 struct rds_message *rm;
635 unsigned int total = 0;
636 unsigned long flags;
637 size_t i;
638 int j;
639
640 if (isv6)
641 len /= sizeof(struct rds6_info_message);
642 else
643 len /= sizeof(struct rds_info_message);
644
645 rcu_read_lock();
646
647 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
648 i++, head++) {
649 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
650 struct rds_conn_path *cp;
651 int npaths;
652
653 /* Only show connections in the caller's netns. */
654 if (!net_eq(rds_conn_net(conn), net))
655 continue;
656 if (!isv6 && conn->c_isv6)
657 continue;
658
659 npaths = (conn->c_trans->t_mp_capable ?
660 RDS_MPATH_WORKERS : 1);
661
662 for (j = 0; j < npaths; j++) {
663 cp = &conn->c_path[j];
664 if (want_send)
665 list = &cp->cp_send_queue;
666 else
667 list = &cp->cp_retrans;
668
669 spin_lock_irqsave(&cp->cp_lock, flags);
670
671 /* XXX too lazy to maintain counts.. */
672 list_for_each_entry(rm, list, m_conn_item) {
673 total++;
674 if (total <= len)
675 __rds_inc_msg_cp(&rm->m_inc,
676 iter,
677 &conn->c_laddr,
678 &conn->c_faddr,
679 0, isv6);
680 }
681
682 spin_unlock_irqrestore(&cp->cp_lock, flags);
683 }
684 }
685 }
686 rcu_read_unlock();
687
688 lens->nr = total;
689 if (isv6)
690 lens->each = sizeof(struct rds6_info_message);
691 else
692 lens->each = sizeof(struct rds_info_message);
693 }
694
rds_conn_message_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int want_send)695 static void rds_conn_message_info(struct socket *sock, unsigned int len,
696 struct rds_info_iterator *iter,
697 struct rds_info_lengths *lens,
698 int want_send)
699 {
700 rds_conn_message_info_cmn(sock, len, iter, lens, want_send, false);
701 }
702
703 #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_message_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int want_send)704 static void rds6_conn_message_info(struct socket *sock, unsigned int len,
705 struct rds_info_iterator *iter,
706 struct rds_info_lengths *lens,
707 int want_send)
708 {
709 rds_conn_message_info_cmn(sock, len, iter, lens, want_send, true);
710 }
711 #endif
712
rds_conn_message_info_send(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)713 static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
714 struct rds_info_iterator *iter,
715 struct rds_info_lengths *lens)
716 {
717 rds_conn_message_info(sock, len, iter, lens, 1);
718 }
719
720 #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_message_info_send(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)721 static void rds6_conn_message_info_send(struct socket *sock, unsigned int len,
722 struct rds_info_iterator *iter,
723 struct rds_info_lengths *lens)
724 {
725 rds6_conn_message_info(sock, len, iter, lens, 1);
726 }
727 #endif
728
rds_conn_message_info_retrans(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)729 static void rds_conn_message_info_retrans(struct socket *sock,
730 unsigned int len,
731 struct rds_info_iterator *iter,
732 struct rds_info_lengths *lens)
733 {
734 rds_conn_message_info(sock, len, iter, lens, 0);
735 }
736
737 #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_message_info_retrans(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)738 static void rds6_conn_message_info_retrans(struct socket *sock,
739 unsigned int len,
740 struct rds_info_iterator *iter,
741 struct rds_info_lengths *lens)
742 {
743 rds6_conn_message_info(sock, len, iter, lens, 0);
744 }
745 #endif
746
rds_for_each_conn_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int (* visitor)(struct rds_connection *,void *),u64 * buffer,size_t item_len)747 void rds_for_each_conn_info(struct socket *sock, unsigned int len,
748 struct rds_info_iterator *iter,
749 struct rds_info_lengths *lens,
750 int (*visitor)(struct rds_connection *, void *),
751 u64 *buffer,
752 size_t item_len)
753 {
754 struct net *net = sock_net(sock->sk);
755 struct hlist_head *head;
756 struct rds_connection *conn;
757 size_t i;
758
759 rcu_read_lock();
760
761 lens->nr = 0;
762 lens->each = item_len;
763
764 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
765 i++, head++) {
766 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
767 /* Only show connections in the caller's netns. */
768 if (!net_eq(rds_conn_net(conn), net))
769 continue;
770
771 /* Zero the per-item buffer before handing it to the
772 * visitor so any field the visitor does not write -
773 * including implicit alignment padding - cannot leak
774 * stack contents to user space via rds_info_copy().
775 */
776 memset(buffer, 0, item_len);
777
778 /* XXX no c_lock usage.. */
779 if (!visitor(conn, buffer))
780 continue;
781
782 /* We copy as much as we can fit in the buffer,
783 * but we count all items so that the caller
784 * can resize the buffer. */
785 if (len >= item_len) {
786 rds_info_copy(iter, buffer, item_len);
787 len -= item_len;
788 }
789 lens->nr++;
790 }
791 }
792 rcu_read_unlock();
793 }
794 EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
795
rds_walk_conn_path_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens,int (* visitor)(struct rds_conn_path *,void *),u64 * buffer,size_t item_len)796 static void rds_walk_conn_path_info(struct socket *sock, unsigned int len,
797 struct rds_info_iterator *iter,
798 struct rds_info_lengths *lens,
799 int (*visitor)(struct rds_conn_path *, void *),
800 u64 *buffer,
801 size_t item_len)
802 {
803 struct net *net = sock_net(sock->sk);
804 struct hlist_head *head;
805 struct rds_connection *conn;
806 size_t i;
807
808 rcu_read_lock();
809
810 lens->nr = 0;
811 lens->each = item_len;
812
813 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
814 i++, head++) {
815 hlist_for_each_entry_rcu(conn, head, c_hash_node) {
816 struct rds_conn_path *cp;
817
818 /* Only show connections in the caller's netns. */
819 if (!net_eq(rds_conn_net(conn), net))
820 continue;
821
822 /* XXX We only copy the information from the first
823 * path for now. The problem is that if there are
824 * more than one underlying paths, we cannot report
825 * information of all of them using the existing
826 * API. For example, there is only one next_tx_seq,
827 * which path's next_tx_seq should we report? It is
828 * a bug in the design of MPRDS.
829 */
830 cp = conn->c_path;
831
832 /* Zero the per-item buffer for the same reason as
833 * rds_for_each_conn_info(): any byte the visitor
834 * does not write (including alignment padding) must
835 * not leak stack contents via rds_info_copy().
836 */
837 memset(buffer, 0, item_len);
838
839 /* XXX no cp_lock usage.. */
840 if (!visitor(cp, buffer))
841 continue;
842
843 /* We copy as much as we can fit in the buffer,
844 * but we count all items so that the caller
845 * can resize the buffer.
846 */
847 if (len >= item_len) {
848 rds_info_copy(iter, buffer, item_len);
849 len -= item_len;
850 }
851 lens->nr++;
852 }
853 }
854 rcu_read_unlock();
855 }
856
rds_conn_info_visitor(struct rds_conn_path * cp,void * buffer)857 static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
858 {
859 struct rds_info_connection *cinfo = buffer;
860 struct rds_connection *conn = cp->cp_conn;
861
862 if (conn->c_isv6)
863 return 0;
864
865 cinfo->next_tx_seq = cp->cp_next_tx_seq;
866 cinfo->next_rx_seq = cp->cp_next_rx_seq;
867 cinfo->laddr = conn->c_laddr.s6_addr32[3];
868 cinfo->faddr = conn->c_faddr.s6_addr32[3];
869 cinfo->tos = conn->c_tos;
870 strscpy_pad(cinfo->transport, conn->c_trans->t_name);
871 cinfo->flags = 0;
872
873 rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
874 SENDING);
875 /* XXX Future: return the state rather than these funky bits */
876 rds_conn_info_set(cinfo->flags,
877 atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
878 CONNECTING);
879 rds_conn_info_set(cinfo->flags,
880 atomic_read(&cp->cp_state) == RDS_CONN_UP,
881 CONNECTED);
882 return 1;
883 }
884
885 #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_info_visitor(struct rds_conn_path * cp,void * buffer)886 static int rds6_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
887 {
888 struct rds6_info_connection *cinfo6 = buffer;
889 struct rds_connection *conn = cp->cp_conn;
890
891 cinfo6->next_tx_seq = cp->cp_next_tx_seq;
892 cinfo6->next_rx_seq = cp->cp_next_rx_seq;
893 cinfo6->laddr = conn->c_laddr;
894 cinfo6->faddr = conn->c_faddr;
895 strscpy_pad(cinfo6->transport, conn->c_trans->t_name);
896 cinfo6->flags = 0;
897
898 rds_conn_info_set(cinfo6->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
899 SENDING);
900 /* XXX Future: return the state rather than these funky bits */
901 rds_conn_info_set(cinfo6->flags,
902 atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
903 CONNECTING);
904 rds_conn_info_set(cinfo6->flags,
905 atomic_read(&cp->cp_state) == RDS_CONN_UP,
906 CONNECTED);
907 /* Just return 1 as there is no error case. This is a helper function
908 * for rds_walk_conn_path_info() and it wants a return value.
909 */
910 return 1;
911 }
912 #endif
913
rds_conn_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)914 static void rds_conn_info(struct socket *sock, unsigned int len,
915 struct rds_info_iterator *iter,
916 struct rds_info_lengths *lens)
917 {
918 u64 buffer[(sizeof(struct rds_info_connection) + 7) / 8];
919
920 rds_walk_conn_path_info(sock, len, iter, lens,
921 rds_conn_info_visitor,
922 buffer,
923 sizeof(struct rds_info_connection));
924 }
925
926 #if IS_ENABLED(CONFIG_IPV6)
rds6_conn_info(struct socket * sock,unsigned int len,struct rds_info_iterator * iter,struct rds_info_lengths * lens)927 static void rds6_conn_info(struct socket *sock, unsigned int len,
928 struct rds_info_iterator *iter,
929 struct rds_info_lengths *lens)
930 {
931 u64 buffer[(sizeof(struct rds6_info_connection) + 7) / 8];
932
933 rds_walk_conn_path_info(sock, len, iter, lens,
934 rds6_conn_info_visitor,
935 buffer,
936 sizeof(struct rds6_info_connection));
937 }
938 #endif
939
rds_conn_init(void)940 int rds_conn_init(void)
941 {
942 int ret;
943
944 ret = rds_loop_net_init(); /* register pernet callback */
945 if (ret)
946 return ret;
947
948 rds_conn_slab = KMEM_CACHE(rds_connection, 0);
949 if (!rds_conn_slab) {
950 rds_loop_net_exit();
951 return -ENOMEM;
952 }
953
954 rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
955 rds_info_register_func(RDS_INFO_SEND_MESSAGES,
956 rds_conn_message_info_send);
957 rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
958 rds_conn_message_info_retrans);
959 #if IS_ENABLED(CONFIG_IPV6)
960 rds_info_register_func(RDS6_INFO_CONNECTIONS, rds6_conn_info);
961 rds_info_register_func(RDS6_INFO_SEND_MESSAGES,
962 rds6_conn_message_info_send);
963 rds_info_register_func(RDS6_INFO_RETRANS_MESSAGES,
964 rds6_conn_message_info_retrans);
965 #endif
966 return 0;
967 }
968
rds_conn_exit(void)969 void rds_conn_exit(void)
970 {
971 rds_loop_net_exit(); /* unregister pernet callback */
972 rds_loop_exit();
973
974 WARN_ON(!hlist_empty(rds_conn_hash));
975
976 kmem_cache_destroy(rds_conn_slab);
977
978 rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
979 rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
980 rds_conn_message_info_send);
981 rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
982 rds_conn_message_info_retrans);
983 #if IS_ENABLED(CONFIG_IPV6)
984 rds_info_deregister_func(RDS6_INFO_CONNECTIONS, rds6_conn_info);
985 rds_info_deregister_func(RDS6_INFO_SEND_MESSAGES,
986 rds6_conn_message_info_send);
987 rds_info_deregister_func(RDS6_INFO_RETRANS_MESSAGES,
988 rds6_conn_message_info_retrans);
989 #endif
990 }
991
992 /*
993 * Force a disconnect
994 */
rds_conn_path_drop(struct rds_conn_path * cp,bool destroy)995 void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy)
996 {
997 atomic_set(&cp->cp_state, RDS_CONN_ERROR);
998
999 rcu_read_lock();
1000 if (!destroy && rds_destroy_pending(cp->cp_conn)) {
1001 rcu_read_unlock();
1002 return;
1003 }
1004 queue_work(cp->cp_wq, &cp->cp_down_w);
1005 rcu_read_unlock();
1006 }
1007 EXPORT_SYMBOL_GPL(rds_conn_path_drop);
1008
rds_conn_drop(struct rds_connection * conn)1009 void rds_conn_drop(struct rds_connection *conn)
1010 {
1011 WARN_ON(conn->c_trans->t_mp_capable);
1012 rds_conn_path_drop(&conn->c_path[0], false);
1013 }
1014 EXPORT_SYMBOL_GPL(rds_conn_drop);
1015
1016 /*
1017 * If the connection is down, trigger a connect. We may have scheduled a
1018 * delayed reconnect however - in this case we should not interfere.
1019 */
rds_conn_path_connect_if_down(struct rds_conn_path * cp)1020 void rds_conn_path_connect_if_down(struct rds_conn_path *cp)
1021 {
1022 rcu_read_lock();
1023 if (rds_destroy_pending(cp->cp_conn)) {
1024 rcu_read_unlock();
1025 return;
1026 }
1027 if (rds_conn_path_state(cp) == RDS_CONN_DOWN &&
1028 !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags))
1029 queue_delayed_work(cp->cp_wq, &cp->cp_conn_w, 0);
1030 rcu_read_unlock();
1031 }
1032 EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down);
1033
1034 /* Check connectivity of all paths
1035 */
rds_check_all_paths(struct rds_connection * conn)1036 void rds_check_all_paths(struct rds_connection *conn)
1037 {
1038 int i = 0;
1039
1040 do {
1041 rds_conn_path_connect_if_down(&conn->c_path[i]);
1042 } while (++i < conn->c_npaths);
1043 }
1044
rds_conn_connect_if_down(struct rds_connection * conn)1045 void rds_conn_connect_if_down(struct rds_connection *conn)
1046 {
1047 WARN_ON(conn->c_trans->t_mp_capable);
1048 rds_conn_path_connect_if_down(&conn->c_path[0]);
1049 }
1050 EXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
1051
1052 void
__rds_conn_path_error(struct rds_conn_path * cp,const char * fmt,...)1053 __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...)
1054 {
1055 va_list ap;
1056
1057 va_start(ap, fmt);
1058 vprintk(fmt, ap);
1059 va_end(ap);
1060
1061 rds_conn_path_drop(cp, false);
1062 }
1063