1 // SPDX-License-Identifier: GPL-2.0
3 * Multipath support for RPC
5 * Copyright (c) 2015, 2016, Primary Data, Inc. All rights reserved.
7 * Trond Myklebust <trond.myklebust@primarydata.com>
10 #include <linux/atomic.h>
11 #include <linux/types.h>
12 #include <linux/kref.h>
13 #include <linux/list.h>
14 #include <linux/rcupdate.h>
15 #include <linux/rculist.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/sunrpc/xprt.h>
19 #include <linux/sunrpc/addr.h>
20 #include <linux/sunrpc/xprtmultipath.h>
24 typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct rpc_xprt_switch *xps,
25 const struct rpc_xprt *cur);
27 static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
28 static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
29 static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
30 static const struct rpc_xprt_iter_ops rpc_xprt_iter_listoffline;
32 static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
33 struct rpc_xprt *xprt)
35 if (unlikely(xprt_get(xprt) == NULL))
37 list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
39 if (xps->xps_nxprts == 0)
40 xps->xps_net = xprt->xprt_net;
46 * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
47 * @xps: pointer to struct rpc_xprt_switch
48 * @xprt: pointer to struct rpc_xprt
50 * Adds xprt to the end of the list of struct rpc_xprt in xps.
52 void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
53 struct rpc_xprt *xprt)
57 spin_lock(&xps->xps_lock);
58 if (xps->xps_net == xprt->xprt_net || xps->xps_net == NULL)
59 xprt_switch_add_xprt_locked(xps, xprt);
60 spin_unlock(&xps->xps_lock);
61 rpc_sysfs_xprt_setup(xps, xprt, GFP_KERNEL);
64 static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
65 struct rpc_xprt *xprt, bool offline)
67 if (unlikely(xprt == NULL))
69 if (!test_bit(XPRT_OFFLINE, &xprt->state) && offline)
72 if (xps->xps_nxprts == 0)
75 list_del_rcu(&xprt->xprt_switch);
79 * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
80 * @xps: pointer to struct rpc_xprt_switch
81 * @xprt: pointer to struct rpc_xprt
82 * @offline: indicates if the xprt that's being removed is in an offline state
84 * Removes xprt from the list of struct rpc_xprt in xps.
86 void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
87 struct rpc_xprt *xprt, bool offline)
89 spin_lock(&xps->xps_lock);
90 xprt_switch_remove_xprt_locked(xps, xprt, offline);
91 spin_unlock(&xps->xps_lock);
95 static DEFINE_IDA(rpc_xprtswitch_ids);
97 void xprt_multipath_cleanup_ids(void)
99 ida_destroy(&rpc_xprtswitch_ids);
102 static int xprt_switch_alloc_id(struct rpc_xprt_switch *xps, gfp_t gfp_flags)
106 id = ida_alloc(&rpc_xprtswitch_ids, gfp_flags);
114 static void xprt_switch_free_id(struct rpc_xprt_switch *xps)
116 ida_free(&rpc_xprtswitch_ids, xps->xps_id);
120 * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
121 * @xprt: pointer to struct rpc_xprt
122 * @gfp_flags: allocation flags
124 * On success, returns an initialised struct rpc_xprt_switch, containing
125 * the entry xprt. Returns NULL on failure.
127 struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
130 struct rpc_xprt_switch *xps;
132 xps = kmalloc(sizeof(*xps), gfp_flags);
134 spin_lock_init(&xps->xps_lock);
135 kref_init(&xps->xps_kref);
136 xprt_switch_alloc_id(xps, gfp_flags);
137 xps->xps_nxprts = xps->xps_nactive = 0;
138 atomic_long_set(&xps->xps_queuelen, 0);
140 INIT_LIST_HEAD(&xps->xps_xprt_list);
141 xps->xps_iter_ops = &rpc_xprt_iter_singular;
142 rpc_sysfs_xprt_switch_setup(xps, xprt, gfp_flags);
143 xprt_switch_add_xprt_locked(xps, xprt);
144 xps->xps_nunique_destaddr_xprts = 1;
145 rpc_sysfs_xprt_setup(xps, xprt, gfp_flags);
151 static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
153 spin_lock(&xps->xps_lock);
154 while (!list_empty(&xps->xps_xprt_list)) {
155 struct rpc_xprt *xprt;
157 xprt = list_first_entry(&xps->xps_xprt_list,
158 struct rpc_xprt, xprt_switch);
159 xprt_switch_remove_xprt_locked(xps, xprt, true);
160 spin_unlock(&xps->xps_lock);
162 spin_lock(&xps->xps_lock);
164 spin_unlock(&xps->xps_lock);
167 static void xprt_switch_free(struct kref *kref)
169 struct rpc_xprt_switch *xps = container_of(kref,
170 struct rpc_xprt_switch, xps_kref);
172 xprt_switch_free_entries(xps);
173 rpc_sysfs_xprt_switch_destroy(xps);
174 xprt_switch_free_id(xps);
175 kfree_rcu(xps, xps_rcu);
179 * xprt_switch_get - Return a reference to a rpc_xprt_switch
180 * @xps: pointer to struct rpc_xprt_switch
182 * Returns a reference to xps unless the refcount is already zero.
184 struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
186 if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
192 * xprt_switch_put - Release a reference to a rpc_xprt_switch
193 * @xps: pointer to struct rpc_xprt_switch
195 * Release the reference to xps, and free it once the refcount is zero.
197 void xprt_switch_put(struct rpc_xprt_switch *xps)
200 kref_put(&xps->xps_kref, xprt_switch_free);
204 * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
205 * @xps: pointer to struct rpc_xprt_switch
207 * Sets a round-robin default policy for iterators acting on xps.
209 void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
211 if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
212 WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
216 const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
218 if (xpi->xpi_ops != NULL)
220 return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
224 void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
229 void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
231 WRITE_ONCE(xpi->xpi_cursor, NULL);
235 bool xprt_is_active(const struct rpc_xprt *xprt)
237 return (kref_read(&xprt->kref) != 0 &&
238 !test_bit(XPRT_OFFLINE, &xprt->state));
242 struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
244 struct rpc_xprt *pos;
246 list_for_each_entry_rcu(pos, head, xprt_switch) {
247 if (xprt_is_active(pos))
254 struct rpc_xprt *xprt_switch_find_first_entry_offline(struct list_head *head)
256 struct rpc_xprt *pos;
258 list_for_each_entry_rcu(pos, head, xprt_switch) {
259 if (!xprt_is_active(pos))
266 struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
268 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
272 return xprt_switch_find_first_entry(&xps->xps_xprt_list);
276 struct rpc_xprt *_xprt_switch_find_current_entry(struct list_head *head,
277 const struct rpc_xprt *cur,
280 struct rpc_xprt *pos;
283 list_for_each_entry_rcu(pos, head, xprt_switch) {
286 if (found && ((find_active && xprt_is_active(pos)) ||
287 (!find_active && xprt_is_active(pos))))
294 struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
295 const struct rpc_xprt *cur)
297 return _xprt_switch_find_current_entry(head, cur, true);
301 struct rpc_xprt * _xprt_iter_current_entry(struct rpc_xprt_iter *xpi,
302 struct rpc_xprt *first_entry(struct list_head *head),
303 struct rpc_xprt *current_entry(struct list_head *head,
304 const struct rpc_xprt *cur))
306 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
307 struct list_head *head;
311 head = &xps->xps_xprt_list;
312 if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
313 return first_entry(head);
314 return current_entry(head, xpi->xpi_cursor);
318 struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
320 return _xprt_iter_current_entry(xpi, xprt_switch_find_first_entry,
321 xprt_switch_find_current_entry);
325 struct rpc_xprt *xprt_switch_find_current_entry_offline(struct list_head *head,
326 const struct rpc_xprt *cur)
328 return _xprt_switch_find_current_entry(head, cur, false);
332 struct rpc_xprt *xprt_iter_current_entry_offline(struct rpc_xprt_iter *xpi)
334 return _xprt_iter_current_entry(xpi,
335 xprt_switch_find_first_entry_offline,
336 xprt_switch_find_current_entry_offline);
339 bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
340 const struct sockaddr *sap)
342 struct list_head *head;
343 struct rpc_xprt *pos;
345 if (xps == NULL || sap == NULL)
348 head = &xps->xps_xprt_list;
349 list_for_each_entry_rcu(pos, head, xprt_switch) {
350 if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
351 pr_info("RPC: addr %s already in xprt switch\n",
352 pos->address_strings[RPC_DISPLAY_ADDR]);
360 struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
361 const struct rpc_xprt *cur, bool check_active)
363 struct rpc_xprt *pos, *prev = NULL;
366 list_for_each_entry_rcu(pos, head, xprt_switch) {
369 /* for request to return active transports return only
370 * active, for request to return offline transports
371 * return only offline
373 if (found && ((check_active && xprt_is_active(pos)) ||
374 (!check_active && !xprt_is_active(pos))))
382 struct rpc_xprt *xprt_switch_set_next_cursor(struct rpc_xprt_switch *xps,
383 struct rpc_xprt **cursor,
384 xprt_switch_find_xprt_t find_next)
386 struct rpc_xprt *pos, *old;
388 old = smp_load_acquire(cursor);
389 pos = find_next(xps, old);
390 smp_store_release(cursor, pos);
395 struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
396 xprt_switch_find_xprt_t find_next)
398 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
402 return xprt_switch_set_next_cursor(xps, &xpi->xpi_cursor, find_next);
406 struct rpc_xprt *__xprt_switch_find_next_entry_roundrobin(struct list_head *head,
407 const struct rpc_xprt *cur)
409 struct rpc_xprt *ret;
411 ret = xprt_switch_find_next_entry(head, cur, true);
414 return xprt_switch_find_first_entry(head);
418 struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct rpc_xprt_switch *xps,
419 const struct rpc_xprt *cur)
421 struct list_head *head = &xps->xps_xprt_list;
422 struct rpc_xprt *xprt;
423 unsigned int nactive;
426 unsigned long xprt_queuelen, xps_queuelen;
428 xprt = __xprt_switch_find_next_entry_roundrobin(head, cur);
431 xprt_queuelen = atomic_long_read(&xprt->queuelen);
432 xps_queuelen = atomic_long_read(&xps->xps_queuelen);
433 nactive = READ_ONCE(xps->xps_nactive);
434 /* Exit loop if xprt_queuelen <= average queue length */
435 if (xprt_queuelen * nactive <= xps_queuelen)
443 struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
445 return xprt_iter_next_entry_multiple(xpi,
446 xprt_switch_find_next_entry_roundrobin);
450 struct rpc_xprt *xprt_switch_find_next_entry_all(struct rpc_xprt_switch *xps,
451 const struct rpc_xprt *cur)
453 return xprt_switch_find_next_entry(&xps->xps_xprt_list, cur, true);
457 struct rpc_xprt *xprt_switch_find_next_entry_offline(struct rpc_xprt_switch *xps,
458 const struct rpc_xprt *cur)
460 return xprt_switch_find_next_entry(&xps->xps_xprt_list, cur, false);
464 struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
466 return xprt_iter_next_entry_multiple(xpi,
467 xprt_switch_find_next_entry_all);
471 struct rpc_xprt *xprt_iter_next_entry_offline(struct rpc_xprt_iter *xpi)
473 return xprt_iter_next_entry_multiple(xpi,
474 xprt_switch_find_next_entry_offline);
478 * xprt_iter_rewind - Resets the xprt iterator
479 * @xpi: pointer to rpc_xprt_iter
481 * Resets xpi to ensure that it points to the first entry in the list
484 void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
487 xprt_iter_ops(xpi)->xpi_rewind(xpi);
491 static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
492 struct rpc_xprt_switch *xps,
493 const struct rpc_xprt_iter_ops *ops)
495 rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
496 xpi->xpi_cursor = NULL;
501 * xprt_iter_init - Initialise an xprt iterator
502 * @xpi: pointer to rpc_xprt_iter
503 * @xps: pointer to rpc_xprt_switch
505 * Initialises the iterator to use the default iterator ops
506 * as set in xps. This function is mainly intended for internal
507 * use in the rpc_client.
509 void xprt_iter_init(struct rpc_xprt_iter *xpi,
510 struct rpc_xprt_switch *xps)
512 __xprt_iter_init(xpi, xps, NULL);
516 * xprt_iter_init_listall - Initialise an xprt iterator
517 * @xpi: pointer to rpc_xprt_iter
518 * @xps: pointer to rpc_xprt_switch
520 * Initialises the iterator to iterate once through the entire list
523 void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
524 struct rpc_xprt_switch *xps)
526 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
529 void xprt_iter_init_listoffline(struct rpc_xprt_iter *xpi,
530 struct rpc_xprt_switch *xps)
532 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listoffline);
536 * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
537 * @xpi: pointer to rpc_xprt_iter
538 * @newswitch: pointer to a new rpc_xprt_switch or NULL
540 * Swaps out the existing xpi->xpi_xpswitch with a new value.
542 struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
543 struct rpc_xprt_switch *newswitch)
545 struct rpc_xprt_switch __rcu *oldswitch;
547 /* Atomically swap out the old xpswitch */
548 oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
549 if (newswitch != NULL)
550 xprt_iter_rewind(xpi);
551 return rcu_dereference_protected(oldswitch, true);
555 * xprt_iter_destroy - Destroys the xprt iterator
556 * @xpi: pointer to rpc_xprt_iter
558 void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
560 xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
564 * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
565 * @xpi: pointer to rpc_xprt_iter
567 * Returns a pointer to the struct rpc_xprt that is currently
568 * pointed to by the cursor.
569 * Caller must be holding rcu_read_lock().
571 struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
573 WARN_ON_ONCE(!rcu_read_lock_held());
574 return xprt_iter_ops(xpi)->xpi_xprt(xpi);
578 struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
579 struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
581 struct rpc_xprt *ret;
588 } while (ret == NULL);
593 * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
594 * @xpi: pointer to rpc_xprt_iter
596 * Returns a reference to the struct rpc_xprt that is currently
597 * pointed to by the cursor.
599 struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
601 struct rpc_xprt *xprt;
604 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
610 * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
611 * @xpi: pointer to rpc_xprt_iter
613 * Returns a reference to the struct rpc_xprt that immediately follows the
614 * entry pointed to by the cursor.
616 struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
618 struct rpc_xprt *xprt;
621 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
626 /* Policy for always returning the first entry in the rpc_xprt_switch */
628 const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
629 .xpi_rewind = xprt_iter_no_rewind,
630 .xpi_xprt = xprt_iter_first_entry,
631 .xpi_next = xprt_iter_first_entry,
634 /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
636 const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
637 .xpi_rewind = xprt_iter_default_rewind,
638 .xpi_xprt = xprt_iter_current_entry,
639 .xpi_next = xprt_iter_next_entry_roundrobin,
642 /* Policy for once-through iteration of entries in the rpc_xprt_switch */
644 const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
645 .xpi_rewind = xprt_iter_default_rewind,
646 .xpi_xprt = xprt_iter_current_entry,
647 .xpi_next = xprt_iter_next_entry_all,
651 const struct rpc_xprt_iter_ops rpc_xprt_iter_listoffline = {
652 .xpi_rewind = xprt_iter_default_rewind,
653 .xpi_xprt = xprt_iter_current_entry_offline,
654 .xpi_next = xprt_iter_next_entry_offline,