xprtmultipath.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // 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. typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct list_head *head,
  22. const struct rpc_xprt *cur);
  23. static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
  24. static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
  25. static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
  26. static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
  27. struct rpc_xprt *xprt)
  28. {
  29. if (unlikely(xprt_get(xprt) == NULL))
  30. return;
  31. list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
  32. smp_wmb();
  33. if (xps->xps_nxprts == 0)
  34. xps->xps_net = xprt->xprt_net;
  35. xps->xps_nxprts++;
  36. }
  37. /**
  38. * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
  39. * @xps: pointer to struct rpc_xprt_switch
  40. * @xprt: pointer to struct rpc_xprt
  41. *
  42. * Adds xprt to the end of the list of struct rpc_xprt in xps.
  43. */
  44. void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
  45. struct rpc_xprt *xprt)
  46. {
  47. if (xprt == NULL)
  48. return;
  49. spin_lock(&xps->xps_lock);
  50. if ((xps->xps_net == xprt->xprt_net || xps->xps_net == NULL) &&
  51. !rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
  52. xprt_switch_add_xprt_locked(xps, xprt);
  53. spin_unlock(&xps->xps_lock);
  54. }
  55. static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
  56. struct rpc_xprt *xprt)
  57. {
  58. if (unlikely(xprt == NULL))
  59. return;
  60. xps->xps_nxprts--;
  61. if (xps->xps_nxprts == 0)
  62. xps->xps_net = NULL;
  63. smp_wmb();
  64. list_del_rcu(&xprt->xprt_switch);
  65. }
  66. /**
  67. * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
  68. * @xps: pointer to struct rpc_xprt_switch
  69. * @xprt: pointer to struct rpc_xprt
  70. *
  71. * Removes xprt from the list of struct rpc_xprt in xps.
  72. */
  73. void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
  74. struct rpc_xprt *xprt)
  75. {
  76. spin_lock(&xps->xps_lock);
  77. xprt_switch_remove_xprt_locked(xps, xprt);
  78. spin_unlock(&xps->xps_lock);
  79. xprt_put(xprt);
  80. }
  81. /**
  82. * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
  83. * @xprt: pointer to struct rpc_xprt
  84. * @gfp_flags: allocation flags
  85. *
  86. * On success, returns an initialised struct rpc_xprt_switch, containing
  87. * the entry xprt. Returns NULL on failure.
  88. */
  89. struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
  90. gfp_t gfp_flags)
  91. {
  92. struct rpc_xprt_switch *xps;
  93. xps = kmalloc(sizeof(*xps), gfp_flags);
  94. if (xps != NULL) {
  95. spin_lock_init(&xps->xps_lock);
  96. kref_init(&xps->xps_kref);
  97. xps->xps_nxprts = 0;
  98. INIT_LIST_HEAD(&xps->xps_xprt_list);
  99. xps->xps_iter_ops = &rpc_xprt_iter_singular;
  100. xprt_switch_add_xprt_locked(xps, xprt);
  101. }
  102. return xps;
  103. }
  104. static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
  105. {
  106. spin_lock(&xps->xps_lock);
  107. while (!list_empty(&xps->xps_xprt_list)) {
  108. struct rpc_xprt *xprt;
  109. xprt = list_first_entry(&xps->xps_xprt_list,
  110. struct rpc_xprt, xprt_switch);
  111. xprt_switch_remove_xprt_locked(xps, xprt);
  112. spin_unlock(&xps->xps_lock);
  113. xprt_put(xprt);
  114. spin_lock(&xps->xps_lock);
  115. }
  116. spin_unlock(&xps->xps_lock);
  117. }
  118. static void xprt_switch_free(struct kref *kref)
  119. {
  120. struct rpc_xprt_switch *xps = container_of(kref,
  121. struct rpc_xprt_switch, xps_kref);
  122. xprt_switch_free_entries(xps);
  123. kfree_rcu(xps, xps_rcu);
  124. }
  125. /**
  126. * xprt_switch_get - Return a reference to a rpc_xprt_switch
  127. * @xps: pointer to struct rpc_xprt_switch
  128. *
  129. * Returns a reference to xps unless the refcount is already zero.
  130. */
  131. struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
  132. {
  133. if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
  134. return xps;
  135. return NULL;
  136. }
  137. /**
  138. * xprt_switch_put - Release a reference to a rpc_xprt_switch
  139. * @xps: pointer to struct rpc_xprt_switch
  140. *
  141. * Release the reference to xps, and free it once the refcount is zero.
  142. */
  143. void xprt_switch_put(struct rpc_xprt_switch *xps)
  144. {
  145. if (xps != NULL)
  146. kref_put(&xps->xps_kref, xprt_switch_free);
  147. }
  148. /**
  149. * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
  150. * @xps: pointer to struct rpc_xprt_switch
  151. *
  152. * Sets a round-robin default policy for iterators acting on xps.
  153. */
  154. void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
  155. {
  156. if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
  157. WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
  158. }
  159. static
  160. const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
  161. {
  162. if (xpi->xpi_ops != NULL)
  163. return xpi->xpi_ops;
  164. return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
  165. }
  166. static
  167. void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
  168. {
  169. }
  170. static
  171. void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
  172. {
  173. WRITE_ONCE(xpi->xpi_cursor, NULL);
  174. }
  175. static
  176. struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
  177. {
  178. return list_first_or_null_rcu(head, struct rpc_xprt, xprt_switch);
  179. }
  180. static
  181. struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
  182. {
  183. struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
  184. if (xps == NULL)
  185. return NULL;
  186. return xprt_switch_find_first_entry(&xps->xps_xprt_list);
  187. }
  188. static
  189. struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
  190. const struct rpc_xprt *cur)
  191. {
  192. struct rpc_xprt *pos;
  193. list_for_each_entry_rcu(pos, head, xprt_switch) {
  194. if (cur == pos)
  195. return pos;
  196. }
  197. return NULL;
  198. }
  199. static
  200. struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
  201. {
  202. struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
  203. struct list_head *head;
  204. if (xps == NULL)
  205. return NULL;
  206. head = &xps->xps_xprt_list;
  207. if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
  208. return xprt_switch_find_first_entry(head);
  209. return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
  210. }
  211. bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
  212. const struct sockaddr *sap)
  213. {
  214. struct list_head *head;
  215. struct rpc_xprt *pos;
  216. if (xps == NULL || sap == NULL)
  217. return false;
  218. head = &xps->xps_xprt_list;
  219. list_for_each_entry_rcu(pos, head, xprt_switch) {
  220. if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
  221. pr_info("RPC: addr %s already in xprt switch\n",
  222. pos->address_strings[RPC_DISPLAY_ADDR]);
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. static
  229. struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
  230. const struct rpc_xprt *cur)
  231. {
  232. struct rpc_xprt *pos, *prev = NULL;
  233. list_for_each_entry_rcu(pos, head, xprt_switch) {
  234. if (cur == prev)
  235. return pos;
  236. prev = pos;
  237. }
  238. return NULL;
  239. }
  240. static
  241. struct rpc_xprt *xprt_switch_set_next_cursor(struct list_head *head,
  242. struct rpc_xprt **cursor,
  243. xprt_switch_find_xprt_t find_next)
  244. {
  245. struct rpc_xprt *cur, *pos, *old;
  246. cur = READ_ONCE(*cursor);
  247. for (;;) {
  248. old = cur;
  249. pos = find_next(head, old);
  250. if (pos == NULL)
  251. break;
  252. cur = cmpxchg_relaxed(cursor, old, pos);
  253. if (cur == old)
  254. break;
  255. }
  256. return pos;
  257. }
  258. static
  259. struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
  260. xprt_switch_find_xprt_t find_next)
  261. {
  262. struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
  263. if (xps == NULL)
  264. return NULL;
  265. return xprt_switch_set_next_cursor(&xps->xps_xprt_list,
  266. &xpi->xpi_cursor,
  267. find_next);
  268. }
  269. static
  270. struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct list_head *head,
  271. const struct rpc_xprt *cur)
  272. {
  273. struct rpc_xprt *ret;
  274. ret = xprt_switch_find_next_entry(head, cur);
  275. if (ret != NULL)
  276. return ret;
  277. return xprt_switch_find_first_entry(head);
  278. }
  279. static
  280. struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
  281. {
  282. return xprt_iter_next_entry_multiple(xpi,
  283. xprt_switch_find_next_entry_roundrobin);
  284. }
  285. static
  286. struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
  287. {
  288. return xprt_iter_next_entry_multiple(xpi, xprt_switch_find_next_entry);
  289. }
  290. /*
  291. * xprt_iter_rewind - Resets the xprt iterator
  292. * @xpi: pointer to rpc_xprt_iter
  293. *
  294. * Resets xpi to ensure that it points to the first entry in the list
  295. * of transports.
  296. */
  297. static
  298. void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
  299. {
  300. rcu_read_lock();
  301. xprt_iter_ops(xpi)->xpi_rewind(xpi);
  302. rcu_read_unlock();
  303. }
  304. static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
  305. struct rpc_xprt_switch *xps,
  306. const struct rpc_xprt_iter_ops *ops)
  307. {
  308. rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
  309. xpi->xpi_cursor = NULL;
  310. xpi->xpi_ops = ops;
  311. }
  312. /**
  313. * xprt_iter_init - Initialise an xprt iterator
  314. * @xpi: pointer to rpc_xprt_iter
  315. * @xps: pointer to rpc_xprt_switch
  316. *
  317. * Initialises the iterator to use the default iterator ops
  318. * as set in xps. This function is mainly intended for internal
  319. * use in the rpc_client.
  320. */
  321. void xprt_iter_init(struct rpc_xprt_iter *xpi,
  322. struct rpc_xprt_switch *xps)
  323. {
  324. __xprt_iter_init(xpi, xps, NULL);
  325. }
  326. /**
  327. * xprt_iter_init_listall - Initialise an xprt iterator
  328. * @xpi: pointer to rpc_xprt_iter
  329. * @xps: pointer to rpc_xprt_switch
  330. *
  331. * Initialises the iterator to iterate once through the entire list
  332. * of entries in xps.
  333. */
  334. void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
  335. struct rpc_xprt_switch *xps)
  336. {
  337. __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
  338. }
  339. /**
  340. * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
  341. * @xpi: pointer to rpc_xprt_iter
  342. * @xps: pointer to a new rpc_xprt_switch or NULL
  343. *
  344. * Swaps out the existing xpi->xpi_xpswitch with a new value.
  345. */
  346. struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
  347. struct rpc_xprt_switch *newswitch)
  348. {
  349. struct rpc_xprt_switch __rcu *oldswitch;
  350. /* Atomically swap out the old xpswitch */
  351. oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
  352. if (newswitch != NULL)
  353. xprt_iter_rewind(xpi);
  354. return rcu_dereference_protected(oldswitch, true);
  355. }
  356. /**
  357. * xprt_iter_destroy - Destroys the xprt iterator
  358. * @xpi pointer to rpc_xprt_iter
  359. */
  360. void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
  361. {
  362. xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
  363. }
  364. /**
  365. * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
  366. * @xpi: pointer to rpc_xprt_iter
  367. *
  368. * Returns a pointer to the struct rpc_xprt that is currently
  369. * pointed to by the cursor.
  370. * Caller must be holding rcu_read_lock().
  371. */
  372. struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
  373. {
  374. WARN_ON_ONCE(!rcu_read_lock_held());
  375. return xprt_iter_ops(xpi)->xpi_xprt(xpi);
  376. }
  377. static
  378. struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
  379. struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
  380. {
  381. struct rpc_xprt *ret;
  382. do {
  383. ret = fn(xpi);
  384. if (ret == NULL)
  385. break;
  386. ret = xprt_get(ret);
  387. } while (ret == NULL);
  388. return ret;
  389. }
  390. /**
  391. * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
  392. * @xpi: pointer to rpc_xprt_iter
  393. *
  394. * Returns a reference to the struct rpc_xprt that is currently
  395. * pointed to by the cursor.
  396. */
  397. struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
  398. {
  399. struct rpc_xprt *xprt;
  400. rcu_read_lock();
  401. xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
  402. rcu_read_unlock();
  403. return xprt;
  404. }
  405. /**
  406. * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
  407. * @xpi: pointer to rpc_xprt_iter
  408. *
  409. * Returns a reference to the struct rpc_xprt that immediately follows the
  410. * entry pointed to by the cursor.
  411. */
  412. struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
  413. {
  414. struct rpc_xprt *xprt;
  415. rcu_read_lock();
  416. xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
  417. rcu_read_unlock();
  418. return xprt;
  419. }
  420. /* Policy for always returning the first entry in the rpc_xprt_switch */
  421. static
  422. const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
  423. .xpi_rewind = xprt_iter_no_rewind,
  424. .xpi_xprt = xprt_iter_first_entry,
  425. .xpi_next = xprt_iter_first_entry,
  426. };
  427. /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
  428. static
  429. const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
  430. .xpi_rewind = xprt_iter_default_rewind,
  431. .xpi_xprt = xprt_iter_current_entry,
  432. .xpi_next = xprt_iter_next_entry_roundrobin,
  433. };
  434. /* Policy for once-through iteration of entries in the rpc_xprt_switch */
  435. static
  436. const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
  437. .xpi_rewind = xprt_iter_default_rewind,
  438. .xpi_xprt = xprt_iter_current_entry,
  439. .xpi_next = xprt_iter_next_entry_all,
  440. };