connection.c 16 KB

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