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 list_head *head,
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;
42 * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
43 * @xps: pointer to struct rpc_xprt_switch
44 * @xprt: pointer to struct rpc_xprt
46 * Adds xprt to the end of the list of struct rpc_xprt in xps.
48 void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
49 struct rpc_xprt *xprt)
53 spin_lock(&xps->xps_lock);
54 if ((xps->xps_net == xprt->xprt_net || xps->xps_net == NULL) &&
55 !rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
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))
66 if (xps->xps_nxprts == 0)
69 list_del_rcu(&xprt->xprt_switch);
73 * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
74 * @xps: pointer to struct rpc_xprt_switch
75 * @xprt: pointer to struct rpc_xprt
77 * Removes xprt from the list of struct rpc_xprt in xps.
79 void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
80 struct rpc_xprt *xprt)
82 spin_lock(&xps->xps_lock);
83 xprt_switch_remove_xprt_locked(xps, xprt);
84 spin_unlock(&xps->xps_lock);
89 * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
90 * @xprt: pointer to struct rpc_xprt
91 * @gfp_flags: allocation flags
93 * On success, returns an initialised struct rpc_xprt_switch, containing
94 * the entry xprt. Returns NULL on failure.
96 struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
99 struct rpc_xprt_switch *xps;
101 xps = kmalloc(sizeof(*xps), gfp_flags);
103 spin_lock_init(&xps->xps_lock);
104 kref_init(&xps->xps_kref);
106 INIT_LIST_HEAD(&xps->xps_xprt_list);
107 xps->xps_iter_ops = &rpc_xprt_iter_singular;
108 xprt_switch_add_xprt_locked(xps, xprt);
114 static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
116 spin_lock(&xps->xps_lock);
117 while (!list_empty(&xps->xps_xprt_list)) {
118 struct rpc_xprt *xprt;
120 xprt = list_first_entry(&xps->xps_xprt_list,
121 struct rpc_xprt, xprt_switch);
122 xprt_switch_remove_xprt_locked(xps, xprt);
123 spin_unlock(&xps->xps_lock);
125 spin_lock(&xps->xps_lock);
127 spin_unlock(&xps->xps_lock);
130 static void xprt_switch_free(struct kref *kref)
132 struct rpc_xprt_switch *xps = container_of(kref,
133 struct rpc_xprt_switch, xps_kref);
135 xprt_switch_free_entries(xps);
136 kfree_rcu(xps, xps_rcu);
140 * xprt_switch_get - Return a reference to a rpc_xprt_switch
141 * @xps: pointer to struct rpc_xprt_switch
143 * Returns a reference to xps unless the refcount is already zero.
145 struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
147 if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
153 * xprt_switch_put - Release a reference to a rpc_xprt_switch
154 * @xps: pointer to struct rpc_xprt_switch
156 * Release the reference to xps, and free it once the refcount is zero.
158 void xprt_switch_put(struct rpc_xprt_switch *xps)
161 kref_put(&xps->xps_kref, xprt_switch_free);
165 * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
166 * @xps: pointer to struct rpc_xprt_switch
168 * Sets a round-robin default policy for iterators acting on xps.
170 void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
172 if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
173 WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
177 const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
179 if (xpi->xpi_ops != NULL)
181 return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
185 void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
190 void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
192 WRITE_ONCE(xpi->xpi_cursor, NULL);
196 struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
198 return list_first_or_null_rcu(head, struct rpc_xprt, xprt_switch);
202 struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
204 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
208 return xprt_switch_find_first_entry(&xps->xps_xprt_list);
212 struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
213 const struct rpc_xprt *cur)
215 struct rpc_xprt *pos;
217 list_for_each_entry_rcu(pos, head, xprt_switch) {
225 struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
227 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
228 struct list_head *head;
232 head = &xps->xps_xprt_list;
233 if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
234 return xprt_switch_find_first_entry(head);
235 return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
239 bool __rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
240 const struct sockaddr *sap)
242 struct list_head *head;
243 struct rpc_xprt *pos;
245 if (xps == NULL || sap == NULL)
248 head = &xps->xps_xprt_list;
249 list_for_each_entry_rcu(pos, head, xprt_switch) {
250 if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
251 pr_info("RPC: addr %s already in xprt switch\n",
252 pos->address_strings[RPC_DISPLAY_ADDR]);
259 bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
260 const struct sockaddr *sap)
265 res = __rpc_xprt_switch_has_addr(xps, sap);
272 struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
273 const struct rpc_xprt *cur)
275 struct rpc_xprt *pos, *prev = NULL;
277 list_for_each_entry_rcu(pos, head, xprt_switch) {
286 struct rpc_xprt *xprt_switch_set_next_cursor(struct list_head *head,
287 struct rpc_xprt **cursor,
288 xprt_switch_find_xprt_t find_next)
290 struct rpc_xprt *cur, *pos, *old;
292 cur = READ_ONCE(*cursor);
295 pos = find_next(head, old);
298 cur = cmpxchg_relaxed(cursor, old, pos);
306 struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
307 xprt_switch_find_xprt_t find_next)
309 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
313 return xprt_switch_set_next_cursor(&xps->xps_xprt_list,
319 struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct list_head *head,
320 const struct rpc_xprt *cur)
322 struct rpc_xprt *ret;
324 ret = xprt_switch_find_next_entry(head, cur);
327 return xprt_switch_find_first_entry(head);
331 struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
333 return xprt_iter_next_entry_multiple(xpi,
334 xprt_switch_find_next_entry_roundrobin);
338 struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
340 return xprt_iter_next_entry_multiple(xpi, xprt_switch_find_next_entry);
344 * xprt_iter_rewind - Resets the xprt iterator
345 * @xpi: pointer to rpc_xprt_iter
347 * Resets xpi to ensure that it points to the first entry in the list
351 void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
354 xprt_iter_ops(xpi)->xpi_rewind(xpi);
358 static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
359 struct rpc_xprt_switch *xps,
360 const struct rpc_xprt_iter_ops *ops)
362 rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
363 xpi->xpi_cursor = NULL;
368 * xprt_iter_init - Initialise an xprt iterator
369 * @xpi: pointer to rpc_xprt_iter
370 * @xps: pointer to rpc_xprt_switch
372 * Initialises the iterator to use the default iterator ops
373 * as set in xps. This function is mainly intended for internal
374 * use in the rpc_client.
376 void xprt_iter_init(struct rpc_xprt_iter *xpi,
377 struct rpc_xprt_switch *xps)
379 __xprt_iter_init(xpi, xps, NULL);
383 * xprt_iter_init_listall - Initialise an xprt iterator
384 * @xpi: pointer to rpc_xprt_iter
385 * @xps: pointer to rpc_xprt_switch
387 * Initialises the iterator to iterate once through the entire list
390 void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
391 struct rpc_xprt_switch *xps)
393 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
397 * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
398 * @xpi: pointer to rpc_xprt_iter
399 * @xps: pointer to a new rpc_xprt_switch or NULL
401 * Swaps out the existing xpi->xpi_xpswitch with a new value.
403 struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
404 struct rpc_xprt_switch *newswitch)
406 struct rpc_xprt_switch __rcu *oldswitch;
408 /* Atomically swap out the old xpswitch */
409 oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
410 if (newswitch != NULL)
411 xprt_iter_rewind(xpi);
412 return rcu_dereference_protected(oldswitch, true);
416 * xprt_iter_destroy - Destroys the xprt iterator
417 * @xpi pointer to rpc_xprt_iter
419 void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
421 xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
425 * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
426 * @xpi: pointer to rpc_xprt_iter
428 * Returns a pointer to the struct rpc_xprt that is currently
429 * pointed to by the cursor.
430 * Caller must be holding rcu_read_lock().
432 struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
434 WARN_ON_ONCE(!rcu_read_lock_held());
435 return xprt_iter_ops(xpi)->xpi_xprt(xpi);
439 struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
440 struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
442 struct rpc_xprt *ret;
449 } while (ret == NULL);
454 * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
455 * @xpi: pointer to rpc_xprt_iter
457 * Returns a reference to the struct rpc_xprt that is currently
458 * pointed to by the cursor.
460 struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
462 struct rpc_xprt *xprt;
465 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
471 * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
472 * @xpi: pointer to rpc_xprt_iter
474 * Returns a reference to the struct rpc_xprt that immediately follows the
475 * entry pointed to by the cursor.
477 struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
479 struct rpc_xprt *xprt;
482 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
487 /* Policy for always returning the first entry in the rpc_xprt_switch */
489 const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
490 .xpi_rewind = xprt_iter_no_rewind,
491 .xpi_xprt = xprt_iter_first_entry,
492 .xpi_next = xprt_iter_first_entry,
495 /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
497 const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
498 .xpi_rewind = xprt_iter_default_rewind,
499 .xpi_xprt = xprt_iter_current_entry,
500 .xpi_next = xprt_iter_next_entry_roundrobin,
503 /* Policy for once-through iteration of entries in the rpc_xprt_switch */
505 const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
506 .xpi_rewind = xprt_iter_default_rewind,
507 .xpi_xprt = xprt_iter_current_entry,
508 .xpi_next = xprt_iter_next_entry_all,