threads.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/random.h>
  35. #include <linux/export.h>
  36. #include "rds.h"
  37. /*
  38. * All of connection management is simplified by serializing it through
  39. * work queues that execute in a connection managing thread.
  40. *
  41. * TCP wants to send acks through sendpage() in response to data_ready(),
  42. * but it needs a process context to do so.
  43. *
  44. * The receive paths need to allocate but can't drop packets (!) so we have
  45. * a thread around to block allocating if the receive fast path sees an
  46. * allocation failure.
  47. */
  48. /* Grand Unified Theory of connection life cycle:
  49. * At any point in time, the connection can be in one of these states:
  50. * DOWN, CONNECTING, UP, DISCONNECTING, ERROR
  51. *
  52. * The following transitions are possible:
  53. * ANY -> ERROR
  54. * UP -> DISCONNECTING
  55. * ERROR -> DISCONNECTING
  56. * DISCONNECTING -> DOWN
  57. * DOWN -> CONNECTING
  58. * CONNECTING -> UP
  59. *
  60. * Transition to state DISCONNECTING/DOWN:
  61. * - Inside the shutdown worker; synchronizes with xmit path
  62. * through RDS_IN_XMIT, and with connection management callbacks
  63. * via c_cm_lock.
  64. *
  65. * For receive callbacks, we rely on the underlying transport
  66. * (TCP, IB/RDMA) to provide the necessary synchronisation.
  67. */
  68. struct workqueue_struct *rds_wq;
  69. EXPORT_SYMBOL_GPL(rds_wq);
  70. void rds_connect_complete(struct rds_connection *conn)
  71. {
  72. if (!rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_UP)) {
  73. printk(KERN_WARNING "%s: Cannot transition to state UP, "
  74. "current state is %d\n",
  75. __func__,
  76. atomic_read(&conn->c_state));
  77. atomic_set(&conn->c_state, RDS_CONN_ERROR);
  78. queue_work(rds_wq, &conn->c_down_w);
  79. return;
  80. }
  81. rdsdebug("conn %p for %pI4 to %pI4 complete\n",
  82. conn, &conn->c_laddr, &conn->c_faddr);
  83. conn->c_reconnect_jiffies = 0;
  84. set_bit(0, &conn->c_map_queued);
  85. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  86. queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
  87. }
  88. EXPORT_SYMBOL_GPL(rds_connect_complete);
  89. /*
  90. * This random exponential backoff is relied on to eventually resolve racing
  91. * connects.
  92. *
  93. * If connect attempts race then both parties drop both connections and come
  94. * here to wait for a random amount of time before trying again. Eventually
  95. * the backoff range will be so much greater than the time it takes to
  96. * establish a connection that one of the pair will establish the connection
  97. * before the other's random delay fires.
  98. *
  99. * Connection attempts that arrive while a connection is already established
  100. * are also considered to be racing connects. This lets a connection from
  101. * a rebooted machine replace an existing stale connection before the transport
  102. * notices that the connection has failed.
  103. *
  104. * We should *always* start with a random backoff; otherwise a broken connection
  105. * will always take several iterations to be re-established.
  106. */
  107. void rds_queue_reconnect(struct rds_connection *conn)
  108. {
  109. unsigned long rand;
  110. rdsdebug("conn %p for %pI4 to %pI4 reconnect jiffies %lu\n",
  111. conn, &conn->c_laddr, &conn->c_faddr,
  112. conn->c_reconnect_jiffies);
  113. set_bit(RDS_RECONNECT_PENDING, &conn->c_flags);
  114. if (conn->c_reconnect_jiffies == 0) {
  115. conn->c_reconnect_jiffies = rds_sysctl_reconnect_min_jiffies;
  116. queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
  117. return;
  118. }
  119. get_random_bytes(&rand, sizeof(rand));
  120. rdsdebug("%lu delay %lu ceil conn %p for %pI4 -> %pI4\n",
  121. rand % conn->c_reconnect_jiffies, conn->c_reconnect_jiffies,
  122. conn, &conn->c_laddr, &conn->c_faddr);
  123. queue_delayed_work(rds_wq, &conn->c_conn_w,
  124. rand % conn->c_reconnect_jiffies);
  125. conn->c_reconnect_jiffies = min(conn->c_reconnect_jiffies * 2,
  126. rds_sysctl_reconnect_max_jiffies);
  127. }
  128. void rds_connect_worker(struct work_struct *work)
  129. {
  130. struct rds_connection *conn = container_of(work, struct rds_connection, c_conn_w.work);
  131. int ret;
  132. clear_bit(RDS_RECONNECT_PENDING, &conn->c_flags);
  133. if (rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  134. ret = conn->c_trans->conn_connect(conn);
  135. rdsdebug("conn %p for %pI4 to %pI4 dispatched, ret %d\n",
  136. conn, &conn->c_laddr, &conn->c_faddr, ret);
  137. if (ret) {
  138. if (rds_conn_transition(conn, RDS_CONN_CONNECTING, RDS_CONN_DOWN))
  139. rds_queue_reconnect(conn);
  140. else
  141. rds_conn_error(conn, "RDS: connect failed\n");
  142. }
  143. }
  144. }
  145. void rds_send_worker(struct work_struct *work)
  146. {
  147. struct rds_connection *conn = container_of(work, struct rds_connection, c_send_w.work);
  148. int ret;
  149. if (rds_conn_state(conn) == RDS_CONN_UP) {
  150. ret = rds_send_xmit(conn);
  151. rdsdebug("conn %p ret %d\n", conn, ret);
  152. switch (ret) {
  153. case -EAGAIN:
  154. rds_stats_inc(s_send_immediate_retry);
  155. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  156. break;
  157. case -ENOMEM:
  158. rds_stats_inc(s_send_delayed_retry);
  159. queue_delayed_work(rds_wq, &conn->c_send_w, 2);
  160. default:
  161. break;
  162. }
  163. }
  164. }
  165. void rds_recv_worker(struct work_struct *work)
  166. {
  167. struct rds_connection *conn = container_of(work, struct rds_connection, c_recv_w.work);
  168. int ret;
  169. if (rds_conn_state(conn) == RDS_CONN_UP) {
  170. ret = conn->c_trans->recv(conn);
  171. rdsdebug("conn %p ret %d\n", conn, ret);
  172. switch (ret) {
  173. case -EAGAIN:
  174. rds_stats_inc(s_recv_immediate_retry);
  175. queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
  176. break;
  177. case -ENOMEM:
  178. rds_stats_inc(s_recv_delayed_retry);
  179. queue_delayed_work(rds_wq, &conn->c_recv_w, 2);
  180. default:
  181. break;
  182. }
  183. }
  184. }
  185. void rds_shutdown_worker(struct work_struct *work)
  186. {
  187. struct rds_connection *conn = container_of(work, struct rds_connection, c_down_w);
  188. rds_conn_shutdown(conn);
  189. }
  190. void rds_threads_exit(void)
  191. {
  192. destroy_workqueue(rds_wq);
  193. }
  194. int rds_threads_init(void)
  195. {
  196. rds_wq = create_singlethread_workqueue("krdsd");
  197. if (!rds_wq)
  198. return -ENOMEM;
  199. return 0;
  200. }