2 * Multipath support for RPC
4 * Copyright (c) 2015, 2016, Primary Data, Inc. All rights reserved.
6 * Trond Myklebust <trond.myklebust@primarydata.com>
9 #include <linux/types.h>
10 #include <linux/kref.h>
11 #include <linux/list.h>
12 #include <linux/rcupdate.h>
13 #include <linux/rculist.h>
14 #include <linux/slab.h>
15 #include <asm/cmpxchg.h>
16 #include <linux/spinlock.h>
17 #include <linux/sunrpc/xprt.h>
18 #include <linux/sunrpc/addr.h>
19 #include <linux/sunrpc/xprtmultipath.h>
21 typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct list_head *head,
22 const struct rpc_xprt *cur);
24 static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
25 static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
26 static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
28 static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
29 struct rpc_xprt *xprt)
31 if (unlikely(xprt_get(xprt) == NULL))
33 list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
35 if (xps->xps_nxprts == 0)
36 xps->xps_net = xprt->xprt_net;
41 * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
42 * @xps: pointer to struct rpc_xprt_switch
43 * @xprt: pointer to struct rpc_xprt
45 * Adds xprt to the end of the list of struct rpc_xprt in xps.
47 void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
48 struct rpc_xprt *xprt)
52 spin_lock(&xps->xps_lock);
53 if ((xps->xps_net == xprt->xprt_net || xps->xps_net == NULL) &&
54 !rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
55 xprt_switch_add_xprt_locked(xps, xprt);
56 spin_unlock(&xps->xps_lock);
59 static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
60 struct rpc_xprt *xprt)
62 if (unlikely(xprt == NULL))
65 if (xps->xps_nxprts == 0)
68 list_del_rcu(&xprt->xprt_switch);
72 * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
73 * @xps: pointer to struct rpc_xprt_switch
74 * @xprt: pointer to struct rpc_xprt
76 * Removes xprt from the list of struct rpc_xprt in xps.
78 void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
79 struct rpc_xprt *xprt)
81 spin_lock(&xps->xps_lock);
82 xprt_switch_remove_xprt_locked(xps, xprt);
83 spin_unlock(&xps->xps_lock);
88 * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
89 * @xprt: pointer to struct rpc_xprt
90 * @gfp_flags: allocation flags
92 * On success, returns an initialised struct rpc_xprt_switch, containing
93 * the entry xprt. Returns NULL on failure.
95 struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
98 struct rpc_xprt_switch *xps;
100 xps = kmalloc(sizeof(*xps), gfp_flags);
102 spin_lock_init(&xps->xps_lock);
103 kref_init(&xps->xps_kref);
105 INIT_LIST_HEAD(&xps->xps_xprt_list);
106 xps->xps_iter_ops = &rpc_xprt_iter_singular;
107 xprt_switch_add_xprt_locked(xps, xprt);
113 static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
115 spin_lock(&xps->xps_lock);
116 while (!list_empty(&xps->xps_xprt_list)) {
117 struct rpc_xprt *xprt;
119 xprt = list_first_entry(&xps->xps_xprt_list,
120 struct rpc_xprt, xprt_switch);
121 xprt_switch_remove_xprt_locked(xps, xprt);
122 spin_unlock(&xps->xps_lock);
124 spin_lock(&xps->xps_lock);
126 spin_unlock(&xps->xps_lock);
129 static void xprt_switch_free(struct kref *kref)
131 struct rpc_xprt_switch *xps = container_of(kref,
132 struct rpc_xprt_switch, xps_kref);
134 xprt_switch_free_entries(xps);
135 kfree_rcu(xps, xps_rcu);
139 * xprt_switch_get - Return a reference to a rpc_xprt_switch
140 * @xps: pointer to struct rpc_xprt_switch
142 * Returns a reference to xps unless the refcount is already zero.
144 struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
146 if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
152 * xprt_switch_put - Release a reference to a rpc_xprt_switch
153 * @xps: pointer to struct rpc_xprt_switch
155 * Release the reference to xps, and free it once the refcount is zero.
157 void xprt_switch_put(struct rpc_xprt_switch *xps)
160 kref_put(&xps->xps_kref, xprt_switch_free);
164 * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
165 * @xps: pointer to struct rpc_xprt_switch
167 * Sets a round-robin default policy for iterators acting on xps.
169 void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
171 if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
172 WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
176 const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
178 if (xpi->xpi_ops != NULL)
180 return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
184 void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
189 void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
191 WRITE_ONCE(xpi->xpi_cursor, NULL);
195 struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
197 return list_first_or_null_rcu(head, struct rpc_xprt, xprt_switch);
201 struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
203 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
207 return xprt_switch_find_first_entry(&xps->xps_xprt_list);
211 struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
212 const struct rpc_xprt *cur)
214 struct rpc_xprt *pos;
216 list_for_each_entry_rcu(pos, head, xprt_switch) {
224 struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
226 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
227 struct list_head *head;
231 head = &xps->xps_xprt_list;
232 if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
233 return xprt_switch_find_first_entry(head);
234 return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
237 bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
238 const struct sockaddr *sap)
240 struct list_head *head;
241 struct rpc_xprt *pos;
243 if (xps == NULL || sap == NULL)
246 head = &xps->xps_xprt_list;
247 list_for_each_entry_rcu(pos, head, xprt_switch) {
248 if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
249 pr_info("RPC: addr %s already in xprt switch\n",
250 pos->address_strings[RPC_DISPLAY_ADDR]);
258 struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
259 const struct rpc_xprt *cur)
261 struct rpc_xprt *pos, *prev = NULL;
263 list_for_each_entry_rcu(pos, head, xprt_switch) {
272 struct rpc_xprt *xprt_switch_set_next_cursor(struct list_head *head,
273 struct rpc_xprt **cursor,
274 xprt_switch_find_xprt_t find_next)
276 struct rpc_xprt *cur, *pos, *old;
278 cur = READ_ONCE(*cursor);
281 pos = find_next(head, old);
284 cur = cmpxchg_relaxed(cursor, old, pos);
292 struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
293 xprt_switch_find_xprt_t find_next)
295 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
299 return xprt_switch_set_next_cursor(&xps->xps_xprt_list,
305 struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct list_head *head,
306 const struct rpc_xprt *cur)
308 struct rpc_xprt *ret;
310 ret = xprt_switch_find_next_entry(head, cur);
313 return xprt_switch_find_first_entry(head);
317 struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
319 return xprt_iter_next_entry_multiple(xpi,
320 xprt_switch_find_next_entry_roundrobin);
324 struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
326 return xprt_iter_next_entry_multiple(xpi, xprt_switch_find_next_entry);
330 * xprt_iter_rewind - Resets the xprt iterator
331 * @xpi: pointer to rpc_xprt_iter
333 * Resets xpi to ensure that it points to the first entry in the list
337 void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
340 xprt_iter_ops(xpi)->xpi_rewind(xpi);
344 static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
345 struct rpc_xprt_switch *xps,
346 const struct rpc_xprt_iter_ops *ops)
348 rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
349 xpi->xpi_cursor = NULL;
354 * xprt_iter_init - Initialise an xprt iterator
355 * @xpi: pointer to rpc_xprt_iter
356 * @xps: pointer to rpc_xprt_switch
358 * Initialises the iterator to use the default iterator ops
359 * as set in xps. This function is mainly intended for internal
360 * use in the rpc_client.
362 void xprt_iter_init(struct rpc_xprt_iter *xpi,
363 struct rpc_xprt_switch *xps)
365 __xprt_iter_init(xpi, xps, NULL);
369 * xprt_iter_init_listall - Initialise an xprt iterator
370 * @xpi: pointer to rpc_xprt_iter
371 * @xps: pointer to rpc_xprt_switch
373 * Initialises the iterator to iterate once through the entire list
376 void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
377 struct rpc_xprt_switch *xps)
379 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
383 * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
384 * @xpi: pointer to rpc_xprt_iter
385 * @xps: pointer to a new rpc_xprt_switch or NULL
387 * Swaps out the existing xpi->xpi_xpswitch with a new value.
389 struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
390 struct rpc_xprt_switch *newswitch)
392 struct rpc_xprt_switch __rcu *oldswitch;
394 /* Atomically swap out the old xpswitch */
395 oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
396 if (newswitch != NULL)
397 xprt_iter_rewind(xpi);
398 return rcu_dereference_protected(oldswitch, true);
402 * xprt_iter_destroy - Destroys the xprt iterator
403 * @xpi pointer to rpc_xprt_iter
405 void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
407 xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
411 * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
412 * @xpi: pointer to rpc_xprt_iter
414 * Returns a pointer to the struct rpc_xprt that is currently
415 * pointed to by the cursor.
416 * Caller must be holding rcu_read_lock().
418 struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
420 WARN_ON_ONCE(!rcu_read_lock_held());
421 return xprt_iter_ops(xpi)->xpi_xprt(xpi);
425 struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
426 struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
428 struct rpc_xprt *ret;
435 } while (ret == NULL);
440 * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
441 * @xpi: pointer to rpc_xprt_iter
443 * Returns a reference to the struct rpc_xprt that is currently
444 * pointed to by the cursor.
446 struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
448 struct rpc_xprt *xprt;
451 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
457 * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
458 * @xpi: pointer to rpc_xprt_iter
460 * Returns a reference to the struct rpc_xprt that immediately follows the
461 * entry pointed to by the cursor.
463 struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
465 struct rpc_xprt *xprt;
468 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
473 /* Policy for always returning the first entry in the rpc_xprt_switch */
475 const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
476 .xpi_rewind = xprt_iter_no_rewind,
477 .xpi_xprt = xprt_iter_first_entry,
478 .xpi_next = xprt_iter_first_entry,
481 /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
483 const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
484 .xpi_rewind = xprt_iter_default_rewind,
485 .xpi_xprt = xprt_iter_current_entry,
486 .xpi_next = xprt_iter_next_entry_roundrobin,
489 /* Policy for once-through iteration of entries in the rpc_xprt_switch */
491 const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
492 .xpi_rewind = xprt_iter_default_rewind,
493 .xpi_xprt = xprt_iter_current_entry,
494 .xpi_next = xprt_iter_next_entry_all,