connection.c 16 KB

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