blob: a11e80d178305849cbeae675f44d4d2ad894a524 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Multipath support for RPC
4 *
5 * Copyright (c) 2015, 2016, Primary Data, Inc. All rights reserved.
6 *
7 * Trond Myklebust <trond.myklebust@primarydata.com>
8 *
9 */
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>
21
22typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct rpc_xprt_switch *xps,
23 const struct rpc_xprt *cur);
24
25static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
26static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
27static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
28
29static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
30 struct rpc_xprt *xprt)
31{
32 if (unlikely(xprt_get(xprt) == NULL))
33 return;
34 list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
35 smp_wmb();
36 if (xps->xps_nxprts == 0)
37 xps->xps_net = xprt->xprt_net;
38 xps->xps_nxprts++;
39 xps->xps_nactive++;
40}
41
42/**
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
46 *
47 * Adds xprt to the end of the list of struct rpc_xprt in xps.
48 */
49void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
50 struct rpc_xprt *xprt)
51{
52 if (xprt == NULL)
53 return;
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);
58}
59
60static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
61 struct rpc_xprt *xprt)
62{
63 if (unlikely(xprt == NULL))
64 return;
65 xps->xps_nactive--;
66 xps->xps_nxprts--;
67 if (xps->xps_nxprts == 0)
68 xps->xps_net = NULL;
69 smp_wmb();
70 list_del_rcu(&xprt->xprt_switch);
71}
72
73/**
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
77 *
78 * Removes xprt from the list of struct rpc_xprt in xps.
79 */
80void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
81 struct rpc_xprt *xprt)
82{
83 spin_lock(&xps->xps_lock);
84 xprt_switch_remove_xprt_locked(xps, xprt);
85 spin_unlock(&xps->xps_lock);
86 xprt_put(xprt);
87}
88
89/**
90 * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
91 * @xprt: pointer to struct rpc_xprt
92 * @gfp_flags: allocation flags
93 *
94 * On success, returns an initialised struct rpc_xprt_switch, containing
95 * the entry xprt. Returns NULL on failure.
96 */
97struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
98 gfp_t gfp_flags)
99{
100 struct rpc_xprt_switch *xps;
101
102 xps = kmalloc(sizeof(*xps), gfp_flags);
103 if (xps != NULL) {
104 spin_lock_init(&xps->xps_lock);
105 kref_init(&xps->xps_kref);
106 xps->xps_nxprts = xps->xps_nactive = 0;
107 atomic_long_set(&xps->xps_queuelen, 0);
108 xps->xps_net = NULL;
109 INIT_LIST_HEAD(&xps->xps_xprt_list);
110 xps->xps_iter_ops = &rpc_xprt_iter_singular;
111 xprt_switch_add_xprt_locked(xps, xprt);
112 }
113
114 return xps;
115}
116
117static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
118{
119 spin_lock(&xps->xps_lock);
120 while (!list_empty(&xps->xps_xprt_list)) {
121 struct rpc_xprt *xprt;
122
123 xprt = list_first_entry(&xps->xps_xprt_list,
124 struct rpc_xprt, xprt_switch);
125 xprt_switch_remove_xprt_locked(xps, xprt);
126 spin_unlock(&xps->xps_lock);
127 xprt_put(xprt);
128 spin_lock(&xps->xps_lock);
129 }
130 spin_unlock(&xps->xps_lock);
131}
132
133static void xprt_switch_free(struct kref *kref)
134{
135 struct rpc_xprt_switch *xps = container_of(kref,
136 struct rpc_xprt_switch, xps_kref);
137
138 xprt_switch_free_entries(xps);
139 kfree_rcu(xps, xps_rcu);
140}
141
142/**
143 * xprt_switch_get - Return a reference to a rpc_xprt_switch
144 * @xps: pointer to struct rpc_xprt_switch
145 *
146 * Returns a reference to xps unless the refcount is already zero.
147 */
148struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
149{
150 if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
151 return xps;
152 return NULL;
153}
154
155/**
156 * xprt_switch_put - Release a reference to a rpc_xprt_switch
157 * @xps: pointer to struct rpc_xprt_switch
158 *
159 * Release the reference to xps, and free it once the refcount is zero.
160 */
161void xprt_switch_put(struct rpc_xprt_switch *xps)
162{
163 if (xps != NULL)
164 kref_put(&xps->xps_kref, xprt_switch_free);
165}
166
167/**
168 * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
169 * @xps: pointer to struct rpc_xprt_switch
170 *
171 * Sets a round-robin default policy for iterators acting on xps.
172 */
173void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
174{
175 if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
176 WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
177}
178
179static
180const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
181{
182 if (xpi->xpi_ops != NULL)
183 return xpi->xpi_ops;
184 return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
185}
186
187static
188void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
189{
190}
191
192static
193void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
194{
195 WRITE_ONCE(xpi->xpi_cursor, NULL);
196}
197
198static
199bool xprt_is_active(const struct rpc_xprt *xprt)
200{
201 return kref_read(&xprt->kref) != 0;
202}
203
204static
205struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
206{
207 struct rpc_xprt *pos;
208
209 list_for_each_entry_rcu(pos, head, xprt_switch) {
210 if (xprt_is_active(pos))
211 return pos;
212 }
213 return NULL;
214}
215
216static
217struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
218{
219 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
220
221 if (xps == NULL)
222 return NULL;
223 return xprt_switch_find_first_entry(&xps->xps_xprt_list);
224}
225
226static
227struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
228 const struct rpc_xprt *cur)
229{
230 struct rpc_xprt *pos;
231 bool found = false;
232
233 list_for_each_entry_rcu(pos, head, xprt_switch) {
234 if (cur == pos)
235 found = true;
236 if (found && xprt_is_active(pos))
237 return pos;
238 }
239 return NULL;
240}
241
242static
243struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
244{
245 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
246 struct list_head *head;
247
248 if (xps == NULL)
249 return NULL;
250 head = &xps->xps_xprt_list;
251 if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
252 return xprt_switch_find_first_entry(head);
253 return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
254}
255
256static
257bool __rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
258 const struct sockaddr *sap)
259{
260 struct list_head *head;
261 struct rpc_xprt *pos;
262
263 if (xps == NULL || sap == NULL)
264 return false;
265
266 head = &xps->xps_xprt_list;
267 list_for_each_entry_rcu(pos, head, xprt_switch) {
268 if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
269 pr_info("RPC: addr %s already in xprt switch\n",
270 pos->address_strings[RPC_DISPLAY_ADDR]);
271 return true;
272 }
273 }
274 return false;
275}
276
277bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
278 const struct sockaddr *sap)
279{
280 bool res;
281
282 rcu_read_lock();
283 res = __rpc_xprt_switch_has_addr(xps, sap);
284 rcu_read_unlock();
285
286 return res;
287}
288
289static
290struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
291 const struct rpc_xprt *cur)
292{
293 struct rpc_xprt *pos, *prev = NULL;
294 bool found = false;
295
296 list_for_each_entry_rcu(pos, head, xprt_switch) {
297 if (cur == prev)
298 found = true;
299 if (found && xprt_is_active(pos))
300 return pos;
301 prev = pos;
302 }
303 return NULL;
304}
305
306static
307struct rpc_xprt *xprt_switch_set_next_cursor(struct rpc_xprt_switch *xps,
308 struct rpc_xprt **cursor,
309 xprt_switch_find_xprt_t find_next)
310{
311 struct rpc_xprt *pos, *old;
312
313 old = smp_load_acquire(cursor);
314 pos = find_next(xps, old);
315 smp_store_release(cursor, pos);
316 return pos;
317}
318
319static
320struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
321 xprt_switch_find_xprt_t find_next)
322{
323 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
324
325 if (xps == NULL)
326 return NULL;
327 return xprt_switch_set_next_cursor(xps, &xpi->xpi_cursor, find_next);
328}
329
330static
331struct rpc_xprt *__xprt_switch_find_next_entry_roundrobin(struct list_head *head,
332 const struct rpc_xprt *cur)
333{
334 struct rpc_xprt *ret;
335
336 ret = xprt_switch_find_next_entry(head, cur);
337 if (ret != NULL)
338 return ret;
339 return xprt_switch_find_first_entry(head);
340}
341
342static
343struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct rpc_xprt_switch *xps,
344 const struct rpc_xprt *cur)
345{
346 struct list_head *head = &xps->xps_xprt_list;
347 struct rpc_xprt *xprt;
348 unsigned int nactive;
349
350 for (;;) {
351 unsigned long xprt_queuelen, xps_queuelen;
352
353 xprt = __xprt_switch_find_next_entry_roundrobin(head, cur);
354 if (!xprt)
355 break;
356 xprt_queuelen = atomic_long_read(&xprt->queuelen);
357 xps_queuelen = atomic_long_read(&xps->xps_queuelen);
358 nactive = READ_ONCE(xps->xps_nactive);
359 /* Exit loop if xprt_queuelen <= average queue length */
360 if (xprt_queuelen * nactive <= xps_queuelen)
361 break;
362 cur = xprt;
363 }
364 return xprt;
365}
366
367static
368struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
369{
370 return xprt_iter_next_entry_multiple(xpi,
371 xprt_switch_find_next_entry_roundrobin);
372}
373
374static
375struct rpc_xprt *xprt_switch_find_next_entry_all(struct rpc_xprt_switch *xps,
376 const struct rpc_xprt *cur)
377{
378 return xprt_switch_find_next_entry(&xps->xps_xprt_list, cur);
379}
380
381static
382struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
383{
384 return xprt_iter_next_entry_multiple(xpi,
385 xprt_switch_find_next_entry_all);
386}
387
388/*
389 * xprt_iter_rewind - Resets the xprt iterator
390 * @xpi: pointer to rpc_xprt_iter
391 *
392 * Resets xpi to ensure that it points to the first entry in the list
393 * of transports.
394 */
395static
396void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
397{
398 rcu_read_lock();
399 xprt_iter_ops(xpi)->xpi_rewind(xpi);
400 rcu_read_unlock();
401}
402
403static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
404 struct rpc_xprt_switch *xps,
405 const struct rpc_xprt_iter_ops *ops)
406{
407 rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
408 xpi->xpi_cursor = NULL;
409 xpi->xpi_ops = ops;
410}
411
412/**
413 * xprt_iter_init - Initialise an xprt iterator
414 * @xpi: pointer to rpc_xprt_iter
415 * @xps: pointer to rpc_xprt_switch
416 *
417 * Initialises the iterator to use the default iterator ops
418 * as set in xps. This function is mainly intended for internal
419 * use in the rpc_client.
420 */
421void xprt_iter_init(struct rpc_xprt_iter *xpi,
422 struct rpc_xprt_switch *xps)
423{
424 __xprt_iter_init(xpi, xps, NULL);
425}
426
427/**
428 * xprt_iter_init_listall - Initialise an xprt iterator
429 * @xpi: pointer to rpc_xprt_iter
430 * @xps: pointer to rpc_xprt_switch
431 *
432 * Initialises the iterator to iterate once through the entire list
433 * of entries in xps.
434 */
435void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
436 struct rpc_xprt_switch *xps)
437{
438 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
439}
440
441/**
442 * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
443 * @xpi: pointer to rpc_xprt_iter
444 * @newswitch: pointer to a new rpc_xprt_switch or NULL
445 *
446 * Swaps out the existing xpi->xpi_xpswitch with a new value.
447 */
448struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
449 struct rpc_xprt_switch *newswitch)
450{
451 struct rpc_xprt_switch __rcu *oldswitch;
452
453 /* Atomically swap out the old xpswitch */
454 oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
455 if (newswitch != NULL)
456 xprt_iter_rewind(xpi);
457 return rcu_dereference_protected(oldswitch, true);
458}
459
460/**
461 * xprt_iter_destroy - Destroys the xprt iterator
462 * @xpi: pointer to rpc_xprt_iter
463 */
464void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
465{
466 xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
467}
468
469/**
470 * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
471 * @xpi: pointer to rpc_xprt_iter
472 *
473 * Returns a pointer to the struct rpc_xprt that is currently
474 * pointed to by the cursor.
475 * Caller must be holding rcu_read_lock().
476 */
477struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
478{
479 WARN_ON_ONCE(!rcu_read_lock_held());
480 return xprt_iter_ops(xpi)->xpi_xprt(xpi);
481}
482
483static
484struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
485 struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
486{
487 struct rpc_xprt *ret;
488
489 do {
490 ret = fn(xpi);
491 if (ret == NULL)
492 break;
493 ret = xprt_get(ret);
494 } while (ret == NULL);
495 return ret;
496}
497
498/**
499 * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
500 * @xpi: pointer to rpc_xprt_iter
501 *
502 * Returns a reference to the struct rpc_xprt that is currently
503 * pointed to by the cursor.
504 */
505struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
506{
507 struct rpc_xprt *xprt;
508
509 rcu_read_lock();
510 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
511 rcu_read_unlock();
512 return xprt;
513}
514
515/**
516 * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
517 * @xpi: pointer to rpc_xprt_iter
518 *
519 * Returns a reference to the struct rpc_xprt that immediately follows the
520 * entry pointed to by the cursor.
521 */
522struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
523{
524 struct rpc_xprt *xprt;
525
526 rcu_read_lock();
527 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
528 rcu_read_unlock();
529 return xprt;
530}
531
532/* Policy for always returning the first entry in the rpc_xprt_switch */
533static
534const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
535 .xpi_rewind = xprt_iter_no_rewind,
536 .xpi_xprt = xprt_iter_first_entry,
537 .xpi_next = xprt_iter_first_entry,
538};
539
540/* Policy for round-robin iteration of entries in the rpc_xprt_switch */
541static
542const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
543 .xpi_rewind = xprt_iter_default_rewind,
544 .xpi_xprt = xprt_iter_current_entry,
545 .xpi_next = xprt_iter_next_entry_roundrobin,
546};
547
548/* Policy for once-through iteration of entries in the rpc_xprt_switch */
549static
550const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
551 .xpi_rewind = xprt_iter_default_rewind,
552 .xpi_xprt = xprt_iter_current_entry,
553 .xpi_next = xprt_iter_next_entry_all,
554};