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/types.h>
11 #include <linux/kref.h>
12 #include <linux/list.h>
13 #include <linux/rcupdate.h>
14 #include <linux/rculist.h>
15 #include <linux/slab.h>
16 #include <asm/cmpxchg.h>
17 #include <linux/spinlock.h>
18 #include <linux/sunrpc/xprt.h>
19 #include <linux/sunrpc/addr.h>
20 #include <linux/sunrpc/xprtmultipath.h>
22 typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct rpc_xprt_switch *xps,
23 const struct rpc_xprt *cur);
25 static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
26 static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
27 static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
29 static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
30 struct rpc_xprt *xprt)
32 if (unlikely(xprt_get(xprt) == NULL))
34 list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
36 if (xps->xps_nxprts == 0)
37 xps->xps_net = xprt->xprt_net;
43 * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
44 * @xps: pointer to struct rpc_xprt_switch
45 * @xprt: pointer to struct rpc_xprt
47 * Adds xprt to the end of the list of struct rpc_xprt in xps.
49 void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
50 struct rpc_xprt *xprt)
54 spin_lock(&xps->xps_lock);
55 if (xps->xps_net == xprt->xprt_net || xps->xps_net == NULL)
56 xprt_switch_add_xprt_locked(xps, xprt);
57 spin_unlock(&xps->xps_lock);
60 static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
61 struct rpc_xprt *xprt)
63 if (unlikely(xprt == NULL))
67 if (xps->xps_nxprts == 0)
70 list_del_rcu(&xprt->xprt_switch);
74 * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
75 * @xps: pointer to struct rpc_xprt_switch
76 * @xprt: pointer to struct rpc_xprt
78 * Removes xprt from the list of struct rpc_xprt in xps.
80 void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
81 struct rpc_xprt *xprt)
83 spin_lock(&xps->xps_lock);
84 xprt_switch_remove_xprt_locked(xps, xprt);
85 spin_unlock(&xps->xps_lock);
89 static DEFINE_IDA(rpc_xprtswitch_ids);
91 void xprt_multipath_cleanup_ids(void)
93 ida_destroy(&rpc_xprtswitch_ids);
96 static int xprt_switch_alloc_id(struct rpc_xprt_switch *xps, gfp_t gfp_flags)
100 id = ida_simple_get(&rpc_xprtswitch_ids, 0, 0, gfp_flags);
108 static void xprt_switch_free_id(struct rpc_xprt_switch *xps)
110 ida_simple_remove(&rpc_xprtswitch_ids, xps->xps_id);
114 * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
115 * @xprt: pointer to struct rpc_xprt
116 * @gfp_flags: allocation flags
118 * On success, returns an initialised struct rpc_xprt_switch, containing
119 * the entry xprt. Returns NULL on failure.
121 struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
124 struct rpc_xprt_switch *xps;
126 xps = kmalloc(sizeof(*xps), gfp_flags);
128 spin_lock_init(&xps->xps_lock);
129 kref_init(&xps->xps_kref);
130 xprt_switch_alloc_id(xps, gfp_flags);
131 xps->xps_nxprts = xps->xps_nactive = 0;
132 atomic_long_set(&xps->xps_queuelen, 0);
134 INIT_LIST_HEAD(&xps->xps_xprt_list);
135 xps->xps_iter_ops = &rpc_xprt_iter_singular;
136 xprt_switch_add_xprt_locked(xps, xprt);
142 static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
144 spin_lock(&xps->xps_lock);
145 while (!list_empty(&xps->xps_xprt_list)) {
146 struct rpc_xprt *xprt;
148 xprt = list_first_entry(&xps->xps_xprt_list,
149 struct rpc_xprt, xprt_switch);
150 xprt_switch_remove_xprt_locked(xps, xprt);
151 spin_unlock(&xps->xps_lock);
153 spin_lock(&xps->xps_lock);
155 spin_unlock(&xps->xps_lock);
158 static void xprt_switch_free(struct kref *kref)
160 struct rpc_xprt_switch *xps = container_of(kref,
161 struct rpc_xprt_switch, xps_kref);
163 xprt_switch_free_entries(xps);
164 xprt_switch_free_id(xps);
165 kfree_rcu(xps, xps_rcu);
169 * xprt_switch_get - Return a reference to a rpc_xprt_switch
170 * @xps: pointer to struct rpc_xprt_switch
172 * Returns a reference to xps unless the refcount is already zero.
174 struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
176 if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
182 * xprt_switch_put - Release a reference to a rpc_xprt_switch
183 * @xps: pointer to struct rpc_xprt_switch
185 * Release the reference to xps, and free it once the refcount is zero.
187 void xprt_switch_put(struct rpc_xprt_switch *xps)
190 kref_put(&xps->xps_kref, xprt_switch_free);
194 * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
195 * @xps: pointer to struct rpc_xprt_switch
197 * Sets a round-robin default policy for iterators acting on xps.
199 void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
201 if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
202 WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
206 const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
208 if (xpi->xpi_ops != NULL)
210 return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
214 void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
219 void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
221 WRITE_ONCE(xpi->xpi_cursor, NULL);
225 bool xprt_is_active(const struct rpc_xprt *xprt)
227 return kref_read(&xprt->kref) != 0;
231 struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
233 struct rpc_xprt *pos;
235 list_for_each_entry_rcu(pos, head, xprt_switch) {
236 if (xprt_is_active(pos))
243 struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
245 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
249 return xprt_switch_find_first_entry(&xps->xps_xprt_list);
253 struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
254 const struct rpc_xprt *cur)
256 struct rpc_xprt *pos;
259 list_for_each_entry_rcu(pos, head, xprt_switch) {
262 if (found && xprt_is_active(pos))
269 struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
271 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
272 struct list_head *head;
276 head = &xps->xps_xprt_list;
277 if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
278 return xprt_switch_find_first_entry(head);
279 return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
282 bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
283 const struct sockaddr *sap)
285 struct list_head *head;
286 struct rpc_xprt *pos;
288 if (xps == NULL || sap == NULL)
291 head = &xps->xps_xprt_list;
292 list_for_each_entry_rcu(pos, head, xprt_switch) {
293 if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
294 pr_info("RPC: addr %s already in xprt switch\n",
295 pos->address_strings[RPC_DISPLAY_ADDR]);
303 struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
304 const struct rpc_xprt *cur)
306 struct rpc_xprt *pos, *prev = NULL;
309 list_for_each_entry_rcu(pos, head, xprt_switch) {
312 if (found && xprt_is_active(pos))
320 struct rpc_xprt *xprt_switch_set_next_cursor(struct rpc_xprt_switch *xps,
321 struct rpc_xprt **cursor,
322 xprt_switch_find_xprt_t find_next)
324 struct rpc_xprt *pos, *old;
326 old = smp_load_acquire(cursor);
327 pos = find_next(xps, old);
328 smp_store_release(cursor, pos);
333 struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
334 xprt_switch_find_xprt_t find_next)
336 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
340 return xprt_switch_set_next_cursor(xps, &xpi->xpi_cursor, find_next);
344 struct rpc_xprt *__xprt_switch_find_next_entry_roundrobin(struct list_head *head,
345 const struct rpc_xprt *cur)
347 struct rpc_xprt *ret;
349 ret = xprt_switch_find_next_entry(head, cur);
352 return xprt_switch_find_first_entry(head);
356 struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct rpc_xprt_switch *xps,
357 const struct rpc_xprt *cur)
359 struct list_head *head = &xps->xps_xprt_list;
360 struct rpc_xprt *xprt;
361 unsigned int nactive;
364 unsigned long xprt_queuelen, xps_queuelen;
366 xprt = __xprt_switch_find_next_entry_roundrobin(head, cur);
369 xprt_queuelen = atomic_long_read(&xprt->queuelen);
370 xps_queuelen = atomic_long_read(&xps->xps_queuelen);
371 nactive = READ_ONCE(xps->xps_nactive);
372 /* Exit loop if xprt_queuelen <= average queue length */
373 if (xprt_queuelen * nactive <= xps_queuelen)
381 struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
383 return xprt_iter_next_entry_multiple(xpi,
384 xprt_switch_find_next_entry_roundrobin);
388 struct rpc_xprt *xprt_switch_find_next_entry_all(struct rpc_xprt_switch *xps,
389 const struct rpc_xprt *cur)
391 return xprt_switch_find_next_entry(&xps->xps_xprt_list, cur);
395 struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
397 return xprt_iter_next_entry_multiple(xpi,
398 xprt_switch_find_next_entry_all);
402 * xprt_iter_rewind - Resets the xprt iterator
403 * @xpi: pointer to rpc_xprt_iter
405 * Resets xpi to ensure that it points to the first entry in the list
409 void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
412 xprt_iter_ops(xpi)->xpi_rewind(xpi);
416 static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
417 struct rpc_xprt_switch *xps,
418 const struct rpc_xprt_iter_ops *ops)
420 rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
421 xpi->xpi_cursor = NULL;
426 * xprt_iter_init - Initialise an xprt iterator
427 * @xpi: pointer to rpc_xprt_iter
428 * @xps: pointer to rpc_xprt_switch
430 * Initialises the iterator to use the default iterator ops
431 * as set in xps. This function is mainly intended for internal
432 * use in the rpc_client.
434 void xprt_iter_init(struct rpc_xprt_iter *xpi,
435 struct rpc_xprt_switch *xps)
437 __xprt_iter_init(xpi, xps, NULL);
441 * xprt_iter_init_listall - Initialise an xprt iterator
442 * @xpi: pointer to rpc_xprt_iter
443 * @xps: pointer to rpc_xprt_switch
445 * Initialises the iterator to iterate once through the entire list
448 void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
449 struct rpc_xprt_switch *xps)
451 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
455 * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
456 * @xpi: pointer to rpc_xprt_iter
457 * @newswitch: pointer to a new rpc_xprt_switch or NULL
459 * Swaps out the existing xpi->xpi_xpswitch with a new value.
461 struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
462 struct rpc_xprt_switch *newswitch)
464 struct rpc_xprt_switch __rcu *oldswitch;
466 /* Atomically swap out the old xpswitch */
467 oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
468 if (newswitch != NULL)
469 xprt_iter_rewind(xpi);
470 return rcu_dereference_protected(oldswitch, true);
474 * xprt_iter_destroy - Destroys the xprt iterator
475 * @xpi: pointer to rpc_xprt_iter
477 void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
479 xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
483 * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
484 * @xpi: pointer to rpc_xprt_iter
486 * Returns a pointer to the struct rpc_xprt that is currently
487 * pointed to by the cursor.
488 * Caller must be holding rcu_read_lock().
490 struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
492 WARN_ON_ONCE(!rcu_read_lock_held());
493 return xprt_iter_ops(xpi)->xpi_xprt(xpi);
497 struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
498 struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
500 struct rpc_xprt *ret;
507 } while (ret == NULL);
512 * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
513 * @xpi: pointer to rpc_xprt_iter
515 * Returns a reference to the struct rpc_xprt that is currently
516 * pointed to by the cursor.
518 struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
520 struct rpc_xprt *xprt;
523 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
529 * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
530 * @xpi: pointer to rpc_xprt_iter
532 * Returns a reference to the struct rpc_xprt that immediately follows the
533 * entry pointed to by the cursor.
535 struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
537 struct rpc_xprt *xprt;
540 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
545 /* Policy for always returning the first entry in the rpc_xprt_switch */
547 const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
548 .xpi_rewind = xprt_iter_no_rewind,
549 .xpi_xprt = xprt_iter_first_entry,
550 .xpi_next = xprt_iter_first_entry,
553 /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
555 const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
556 .xpi_rewind = xprt_iter_default_rewind,
557 .xpi_xprt = xprt_iter_current_entry,
558 .xpi_next = xprt_iter_next_entry_roundrobin,
561 /* Policy for once-through iteration of entries in the rpc_xprt_switch */
563 const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
564 .xpi_rewind = xprt_iter_default_rewind,
565 .xpi_xprt = xprt_iter_current_entry,
566 .xpi_next = xprt_iter_next_entry_all,