xprtmultipath.c 12 KB

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