connection.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/list.h>
  35. #include <net/inet_hashtables.h>
  36. #include "rds.h"
  37. #include "loop.h"
  38. #include "rdma.h"
  39. #define RDS_CONNECTION_HASH_BITS 12
  40. #define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
  41. #define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
  42. /* converting this to RCU is a chore for another day.. */
  43. static DEFINE_SPINLOCK(rds_conn_lock);
  44. static unsigned long rds_conn_count;
  45. static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
  46. static struct kmem_cache *rds_conn_slab;
  47. static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
  48. {
  49. /* Pass NULL, don't need struct net for hash */
  50. unsigned long hash = inet_ehashfn(NULL,
  51. be32_to_cpu(laddr), 0,
  52. be32_to_cpu(faddr), 0);
  53. return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
  54. }
  55. #define rds_conn_info_set(var, test, suffix) do { \
  56. if (test) \
  57. var |= RDS_INFO_CONNECTION_FLAG_##suffix; \
  58. } while (0)
  59. static inline int rds_conn_is_sending(struct rds_connection *conn)
  60. {
  61. int ret = 0;
  62. if (!mutex_trylock(&conn->c_send_lock))
  63. ret = 1;
  64. else
  65. mutex_unlock(&conn->c_send_lock);
  66. return ret;
  67. }
  68. static struct rds_connection *rds_conn_lookup(struct hlist_head *head,
  69. __be32 laddr, __be32 faddr,
  70. struct rds_transport *trans)
  71. {
  72. struct rds_connection *conn, *ret = NULL;
  73. struct hlist_node *pos;
  74. hlist_for_each_entry(conn, pos, head, c_hash_node) {
  75. if (conn->c_faddr == faddr && conn->c_laddr == laddr &&
  76. conn->c_trans == trans) {
  77. ret = conn;
  78. break;
  79. }
  80. }
  81. rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret,
  82. &laddr, &faddr);
  83. return ret;
  84. }
  85. /*
  86. * This is called by transports as they're bringing down a connection.
  87. * It clears partial message state so that the transport can start sending
  88. * and receiving over this connection again in the future. It is up to
  89. * the transport to have serialized this call with its send and recv.
  90. */
  91. void rds_conn_reset(struct rds_connection *conn)
  92. {
  93. rdsdebug("connection %pI4 to %pI4 reset\n",
  94. &conn->c_laddr, &conn->c_faddr);
  95. rds_stats_inc(s_conn_reset);
  96. rds_send_reset(conn);
  97. conn->c_flags = 0;
  98. /* Do not clear next_rx_seq here, else we cannot distinguish
  99. * retransmitted packets from new packets, and will hand all
  100. * of them to the application. That is not consistent with the
  101. * reliability guarantees of RDS. */
  102. }
  103. /*
  104. * There is only every one 'conn' for a given pair of addresses in the
  105. * system at a time. They contain messages to be retransmitted and so
  106. * span the lifetime of the actual underlying transport connections.
  107. *
  108. * For now they are not garbage collected once they're created. They
  109. * are torn down as the module is removed, if ever.
  110. */
  111. static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
  112. struct rds_transport *trans, gfp_t gfp,
  113. int is_outgoing)
  114. {
  115. struct rds_connection *conn, *parent = NULL;
  116. struct hlist_head *head = rds_conn_bucket(laddr, faddr);
  117. unsigned long flags;
  118. int ret;
  119. spin_lock_irqsave(&rds_conn_lock, flags);
  120. conn = rds_conn_lookup(head, laddr, faddr, trans);
  121. if (conn
  122. && conn->c_loopback
  123. && conn->c_trans != &rds_loop_transport
  124. && !is_outgoing) {
  125. /* This is a looped back IB connection, and we're
  126. * called by the code handling the incoming connect.
  127. * We need a second connection object into which we
  128. * can stick the other QP. */
  129. parent = conn;
  130. conn = parent->c_passive;
  131. }
  132. spin_unlock_irqrestore(&rds_conn_lock, flags);
  133. if (conn)
  134. goto out;
  135. conn = kmem_cache_zalloc(rds_conn_slab, gfp);
  136. if (conn == NULL) {
  137. conn = ERR_PTR(-ENOMEM);
  138. goto out;
  139. }
  140. INIT_HLIST_NODE(&conn->c_hash_node);
  141. conn->c_laddr = laddr;
  142. conn->c_faddr = faddr;
  143. spin_lock_init(&conn->c_lock);
  144. conn->c_next_tx_seq = 1;
  145. mutex_init(&conn->c_send_lock);
  146. INIT_LIST_HEAD(&conn->c_send_queue);
  147. INIT_LIST_HEAD(&conn->c_retrans);
  148. ret = rds_cong_get_maps(conn);
  149. if (ret) {
  150. kmem_cache_free(rds_conn_slab, conn);
  151. conn = ERR_PTR(ret);
  152. goto out;
  153. }
  154. /*
  155. * This is where a connection becomes loopback. If *any* RDS sockets
  156. * can bind to the destination address then we'd rather the messages
  157. * flow through loopback rather than either transport.
  158. */
  159. if (rds_trans_get_preferred(faddr)) {
  160. conn->c_loopback = 1;
  161. if (is_outgoing && trans->t_prefer_loopback) {
  162. /* "outgoing" connection - and the transport
  163. * says it wants the connection handled by the
  164. * loopback transport. This is what TCP does.
  165. */
  166. trans = &rds_loop_transport;
  167. }
  168. }
  169. conn->c_trans = trans;
  170. ret = trans->conn_alloc(conn, gfp);
  171. if (ret) {
  172. kmem_cache_free(rds_conn_slab, conn);
  173. conn = ERR_PTR(ret);
  174. goto out;
  175. }
  176. atomic_set(&conn->c_state, RDS_CONN_DOWN);
  177. conn->c_reconnect_jiffies = 0;
  178. INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker);
  179. INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker);
  180. INIT_DELAYED_WORK(&conn->c_conn_w, rds_connect_worker);
  181. INIT_WORK(&conn->c_down_w, rds_shutdown_worker);
  182. mutex_init(&conn->c_cm_lock);
  183. conn->c_flags = 0;
  184. rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
  185. conn, &laddr, &faddr,
  186. trans->t_name ? trans->t_name : "[unknown]",
  187. is_outgoing ? "(outgoing)" : "");
  188. /*
  189. * Since we ran without holding the conn lock, someone could
  190. * have created the same conn (either normal or passive) in the
  191. * interim. We check while holding the lock. If we won, we complete
  192. * init and return our conn. If we lost, we rollback and return the
  193. * other one.
  194. */
  195. spin_lock_irqsave(&rds_conn_lock, flags);
  196. if (parent) {
  197. /* Creating passive conn */
  198. if (parent->c_passive) {
  199. trans->conn_free(conn->c_transport_data);
  200. kmem_cache_free(rds_conn_slab, conn);
  201. conn = parent->c_passive;
  202. } else {
  203. parent->c_passive = conn;
  204. rds_cong_add_conn(conn);
  205. rds_conn_count++;
  206. }
  207. } else {
  208. /* Creating normal conn */
  209. struct rds_connection *found;
  210. found = rds_conn_lookup(head, laddr, faddr, trans);
  211. if (found) {
  212. trans->conn_free(conn->c_transport_data);
  213. kmem_cache_free(rds_conn_slab, conn);
  214. conn = found;
  215. } else {
  216. hlist_add_head(&conn->c_hash_node, head);
  217. rds_cong_add_conn(conn);
  218. rds_conn_count++;
  219. }
  220. }
  221. spin_unlock_irqrestore(&rds_conn_lock, flags);
  222. out:
  223. return conn;
  224. }
  225. struct rds_connection *rds_conn_create(__be32 laddr, __be32 faddr,
  226. struct rds_transport *trans, gfp_t gfp)
  227. {
  228. return __rds_conn_create(laddr, faddr, trans, gfp, 0);
  229. }
  230. EXPORT_SYMBOL_GPL(rds_conn_create);
  231. struct rds_connection *rds_conn_create_outgoing(__be32 laddr, __be32 faddr,
  232. struct rds_transport *trans, gfp_t gfp)
  233. {
  234. return __rds_conn_create(laddr, faddr, trans, gfp, 1);
  235. }
  236. EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
  237. void rds_conn_destroy(struct rds_connection *conn)
  238. {
  239. struct rds_message *rm, *rtmp;
  240. rdsdebug("freeing conn %p for %pI4 -> "
  241. "%pI4\n", conn, &conn->c_laddr,
  242. &conn->c_faddr);
  243. hlist_del_init(&conn->c_hash_node);
  244. /* wait for the rds thread to shut it down */
  245. atomic_set(&conn->c_state, RDS_CONN_ERROR);
  246. cancel_delayed_work(&conn->c_conn_w);
  247. queue_work(rds_wq, &conn->c_down_w);
  248. flush_workqueue(rds_wq);
  249. /* tear down queued messages */
  250. list_for_each_entry_safe(rm, rtmp,
  251. &conn->c_send_queue,
  252. m_conn_item) {
  253. list_del_init(&rm->m_conn_item);
  254. BUG_ON(!list_empty(&rm->m_sock_item));
  255. rds_message_put(rm);
  256. }
  257. if (conn->c_xmit_rm)
  258. rds_message_put(conn->c_xmit_rm);
  259. conn->c_trans->conn_free(conn->c_transport_data);
  260. /*
  261. * The congestion maps aren't freed up here. They're
  262. * freed by rds_cong_exit() after all the connections
  263. * have been freed.
  264. */
  265. rds_cong_remove_conn(conn);
  266. BUG_ON(!list_empty(&conn->c_retrans));
  267. kmem_cache_free(rds_conn_slab, conn);
  268. rds_conn_count--;
  269. }
  270. EXPORT_SYMBOL_GPL(rds_conn_destroy);
  271. static void rds_conn_message_info(struct socket *sock, unsigned int len,
  272. struct rds_info_iterator *iter,
  273. struct rds_info_lengths *lens,
  274. int want_send)
  275. {
  276. struct hlist_head *head;
  277. struct hlist_node *pos;
  278. struct list_head *list;
  279. struct rds_connection *conn;
  280. struct rds_message *rm;
  281. unsigned long flags;
  282. unsigned int total = 0;
  283. size_t i;
  284. len /= sizeof(struct rds_info_message);
  285. spin_lock_irqsave(&rds_conn_lock, flags);
  286. for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
  287. i++, head++) {
  288. hlist_for_each_entry(conn, pos, head, c_hash_node) {
  289. if (want_send)
  290. list = &conn->c_send_queue;
  291. else
  292. list = &conn->c_retrans;
  293. spin_lock(&conn->c_lock);
  294. /* XXX too lazy to maintain counts.. */
  295. list_for_each_entry(rm, list, m_conn_item) {
  296. total++;
  297. if (total <= len)
  298. rds_inc_info_copy(&rm->m_inc, iter,
  299. conn->c_laddr,
  300. conn->c_faddr, 0);
  301. }
  302. spin_unlock(&conn->c_lock);
  303. }
  304. }
  305. spin_unlock_irqrestore(&rds_conn_lock, flags);
  306. lens->nr = total;
  307. lens->each = sizeof(struct rds_info_message);
  308. }
  309. static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
  310. struct rds_info_iterator *iter,
  311. struct rds_info_lengths *lens)
  312. {
  313. rds_conn_message_info(sock, len, iter, lens, 1);
  314. }
  315. static void rds_conn_message_info_retrans(struct socket *sock,
  316. unsigned int len,
  317. struct rds_info_iterator *iter,
  318. struct rds_info_lengths *lens)
  319. {
  320. rds_conn_message_info(sock, len, iter, lens, 0);
  321. }
  322. void rds_for_each_conn_info(struct socket *sock, unsigned int len,
  323. struct rds_info_iterator *iter,
  324. struct rds_info_lengths *lens,
  325. int (*visitor)(struct rds_connection *, void *),
  326. size_t item_len)
  327. {
  328. uint64_t buffer[(item_len + 7) / 8];
  329. struct hlist_head *head;
  330. struct hlist_node *pos;
  331. struct hlist_node *tmp;
  332. struct rds_connection *conn;
  333. unsigned long flags;
  334. size_t i;
  335. spin_lock_irqsave(&rds_conn_lock, flags);
  336. lens->nr = 0;
  337. lens->each = item_len;
  338. for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
  339. i++, head++) {
  340. hlist_for_each_entry_safe(conn, pos, tmp, head, c_hash_node) {
  341. /* XXX no c_lock usage.. */
  342. if (!visitor(conn, buffer))
  343. continue;
  344. /* We copy as much as we can fit in the buffer,
  345. * but we count all items so that the caller
  346. * can resize the buffer. */
  347. if (len >= item_len) {
  348. rds_info_copy(iter, buffer, item_len);
  349. len -= item_len;
  350. }
  351. lens->nr++;
  352. }
  353. }
  354. spin_unlock_irqrestore(&rds_conn_lock, flags);
  355. }
  356. EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
  357. static int rds_conn_info_visitor(struct rds_connection *conn,
  358. void *buffer)
  359. {
  360. struct rds_info_connection *cinfo = buffer;
  361. cinfo->next_tx_seq = conn->c_next_tx_seq;
  362. cinfo->next_rx_seq = conn->c_next_rx_seq;
  363. cinfo->laddr = conn->c_laddr;
  364. cinfo->faddr = conn->c_faddr;
  365. strncpy(cinfo->transport, conn->c_trans->t_name,
  366. sizeof(cinfo->transport));
  367. cinfo->flags = 0;
  368. rds_conn_info_set(cinfo->flags,
  369. rds_conn_is_sending(conn), SENDING);
  370. /* XXX Future: return the state rather than these funky bits */
  371. rds_conn_info_set(cinfo->flags,
  372. atomic_read(&conn->c_state) == RDS_CONN_CONNECTING,
  373. CONNECTING);
  374. rds_conn_info_set(cinfo->flags,
  375. atomic_read(&conn->c_state) == RDS_CONN_UP,
  376. CONNECTED);
  377. return 1;
  378. }
  379. static void rds_conn_info(struct socket *sock, unsigned int len,
  380. struct rds_info_iterator *iter,
  381. struct rds_info_lengths *lens)
  382. {
  383. rds_for_each_conn_info(sock, len, iter, lens,
  384. rds_conn_info_visitor,
  385. sizeof(struct rds_info_connection));
  386. }
  387. int __init rds_conn_init(void)
  388. {
  389. rds_conn_slab = kmem_cache_create("rds_connection",
  390. sizeof(struct rds_connection),
  391. 0, 0, NULL);
  392. if (rds_conn_slab == NULL)
  393. return -ENOMEM;
  394. rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
  395. rds_info_register_func(RDS_INFO_SEND_MESSAGES,
  396. rds_conn_message_info_send);
  397. rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
  398. rds_conn_message_info_retrans);
  399. return 0;
  400. }
  401. void rds_conn_exit(void)
  402. {
  403. rds_loop_exit();
  404. WARN_ON(!hlist_empty(rds_conn_hash));
  405. kmem_cache_destroy(rds_conn_slab);
  406. rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
  407. rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
  408. rds_conn_message_info_send);
  409. rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
  410. rds_conn_message_info_retrans);
  411. }
  412. /*
  413. * Force a disconnect
  414. */
  415. void rds_conn_drop(struct rds_connection *conn)
  416. {
  417. atomic_set(&conn->c_state, RDS_CONN_ERROR);
  418. queue_work(rds_wq, &conn->c_down_w);
  419. }
  420. EXPORT_SYMBOL_GPL(rds_conn_drop);
  421. /*
  422. * An error occurred on the connection
  423. */
  424. void
  425. __rds_conn_error(struct rds_connection *conn, const char *fmt, ...)
  426. {
  427. va_list ap;
  428. va_start(ap, fmt);
  429. vprintk(fmt, ap);
  430. va_end(ap);
  431. rds_conn_drop(conn);
  432. }