connection.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 <linux/export.h>
  37. #include <net/inet_hashtables.h>
  38. #include "rds.h"
  39. #include "loop.h"
  40. #define RDS_CONNECTION_HASH_BITS 12
  41. #define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
  42. #define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
  43. /* converting this to RCU is a chore for another day.. */
  44. static DEFINE_SPINLOCK(rds_conn_lock);
  45. static unsigned long rds_conn_count;
  46. static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
  47. static struct kmem_cache *rds_conn_slab;
  48. static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
  49. {
  50. static u32 rds_hash_secret __read_mostly;
  51. unsigned long hash;
  52. net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret));
  53. /* Pass NULL, don't need struct net for hash */
  54. hash = __inet_ehashfn(be32_to_cpu(laddr), 0,
  55. be32_to_cpu(faddr), 0,
  56. rds_hash_secret);
  57. return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
  58. }
  59. #define rds_conn_info_set(var, test, suffix) do { \
  60. if (test) \
  61. var |= RDS_INFO_CONNECTION_FLAG_##suffix; \
  62. } while (0)
  63. /* rcu read lock must be held or the connection spinlock */
  64. static struct rds_connection *rds_conn_lookup(struct net *net,
  65. struct hlist_head *head,
  66. __be32 laddr, __be32 faddr,
  67. struct rds_transport *trans)
  68. {
  69. struct rds_connection *conn, *ret = NULL;
  70. hlist_for_each_entry_rcu(conn, head, c_hash_node) {
  71. if (conn->c_faddr == faddr && conn->c_laddr == laddr &&
  72. conn->c_trans == trans && net == rds_conn_net(conn)) {
  73. ret = conn;
  74. break;
  75. }
  76. }
  77. rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret,
  78. &laddr, &faddr);
  79. return ret;
  80. }
  81. /*
  82. * This is called by transports as they're bringing down a connection.
  83. * It clears partial message state so that the transport can start sending
  84. * and receiving over this connection again in the future. It is up to
  85. * the transport to have serialized this call with its send and recv.
  86. */
  87. static void rds_conn_path_reset(struct rds_conn_path *cp)
  88. {
  89. struct rds_connection *conn = cp->cp_conn;
  90. rdsdebug("connection %pI4 to %pI4 reset\n",
  91. &conn->c_laddr, &conn->c_faddr);
  92. rds_stats_inc(s_conn_reset);
  93. rds_send_path_reset(cp);
  94. cp->cp_flags = 0;
  95. /* Do not clear next_rx_seq here, else we cannot distinguish
  96. * retransmitted packets from new packets, and will hand all
  97. * of them to the application. That is not consistent with the
  98. * reliability guarantees of RDS. */
  99. }
  100. static void __rds_conn_path_init(struct rds_connection *conn,
  101. struct rds_conn_path *cp, bool is_outgoing)
  102. {
  103. spin_lock_init(&cp->cp_lock);
  104. cp->cp_next_tx_seq = 1;
  105. init_waitqueue_head(&cp->cp_waitq);
  106. INIT_LIST_HEAD(&cp->cp_send_queue);
  107. INIT_LIST_HEAD(&cp->cp_retrans);
  108. cp->cp_conn = conn;
  109. atomic_set(&cp->cp_state, RDS_CONN_DOWN);
  110. cp->cp_send_gen = 0;
  111. cp->cp_reconnect_jiffies = 0;
  112. INIT_DELAYED_WORK(&cp->cp_send_w, rds_send_worker);
  113. INIT_DELAYED_WORK(&cp->cp_recv_w, rds_recv_worker);
  114. INIT_DELAYED_WORK(&cp->cp_conn_w, rds_connect_worker);
  115. INIT_WORK(&cp->cp_down_w, rds_shutdown_worker);
  116. mutex_init(&cp->cp_cm_lock);
  117. cp->cp_flags = 0;
  118. }
  119. /*
  120. * There is only every one 'conn' for a given pair of addresses in the
  121. * system at a time. They contain messages to be retransmitted and so
  122. * span the lifetime of the actual underlying transport connections.
  123. *
  124. * For now they are not garbage collected once they're created. They
  125. * are torn down as the module is removed, if ever.
  126. */
  127. static struct rds_connection *__rds_conn_create(struct net *net,
  128. __be32 laddr, __be32 faddr,
  129. struct rds_transport *trans, gfp_t gfp,
  130. int is_outgoing)
  131. {
  132. struct rds_connection *conn, *parent = NULL;
  133. struct hlist_head *head = rds_conn_bucket(laddr, faddr);
  134. struct rds_transport *loop_trans;
  135. unsigned long flags;
  136. int ret, i;
  137. int npaths = (trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
  138. rcu_read_lock();
  139. conn = rds_conn_lookup(net, head, laddr, faddr, trans);
  140. if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
  141. laddr == faddr && !is_outgoing) {
  142. /* This is a looped back IB connection, and we're
  143. * called by the code handling the incoming connect.
  144. * We need a second connection object into which we
  145. * can stick the other QP. */
  146. parent = conn;
  147. conn = parent->c_passive;
  148. }
  149. rcu_read_unlock();
  150. if (conn)
  151. goto out;
  152. conn = kmem_cache_zalloc(rds_conn_slab, gfp);
  153. if (!conn) {
  154. conn = ERR_PTR(-ENOMEM);
  155. goto out;
  156. }
  157. conn->c_path = kcalloc(npaths, sizeof(struct rds_conn_path), gfp);
  158. if (!conn->c_path) {
  159. kmem_cache_free(rds_conn_slab, conn);
  160. conn = ERR_PTR(-ENOMEM);
  161. goto out;
  162. }
  163. INIT_HLIST_NODE(&conn->c_hash_node);
  164. conn->c_laddr = laddr;
  165. conn->c_faddr = faddr;
  166. rds_conn_net_set(conn, net);
  167. ret = rds_cong_get_maps(conn);
  168. if (ret) {
  169. kfree(conn->c_path);
  170. kmem_cache_free(rds_conn_slab, conn);
  171. conn = ERR_PTR(ret);
  172. goto out;
  173. }
  174. /*
  175. * This is where a connection becomes loopback. If *any* RDS sockets
  176. * can bind to the destination address then we'd rather the messages
  177. * flow through loopback rather than either transport.
  178. */
  179. loop_trans = rds_trans_get_preferred(net, faddr);
  180. if (loop_trans) {
  181. rds_trans_put(loop_trans);
  182. conn->c_loopback = 1;
  183. if (is_outgoing && trans->t_prefer_loopback) {
  184. /* "outgoing" connection - and the transport
  185. * says it wants the connection handled by the
  186. * loopback transport. This is what TCP does.
  187. */
  188. trans = &rds_loop_transport;
  189. }
  190. }
  191. conn->c_trans = trans;
  192. init_waitqueue_head(&conn->c_hs_waitq);
  193. for (i = 0; i < npaths; i++) {
  194. __rds_conn_path_init(conn, &conn->c_path[i],
  195. is_outgoing);
  196. conn->c_path[i].cp_index = i;
  197. }
  198. ret = trans->conn_alloc(conn, gfp);
  199. if (ret) {
  200. kfree(conn->c_path);
  201. kmem_cache_free(rds_conn_slab, conn);
  202. conn = ERR_PTR(ret);
  203. goto out;
  204. }
  205. rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
  206. conn, &laddr, &faddr,
  207. trans->t_name ? trans->t_name : "[unknown]",
  208. is_outgoing ? "(outgoing)" : "");
  209. /*
  210. * Since we ran without holding the conn lock, someone could
  211. * have created the same conn (either normal or passive) in the
  212. * interim. We check while holding the lock. If we won, we complete
  213. * init and return our conn. If we lost, we rollback and return the
  214. * other one.
  215. */
  216. spin_lock_irqsave(&rds_conn_lock, flags);
  217. if (parent) {
  218. /* Creating passive conn */
  219. if (parent->c_passive) {
  220. trans->conn_free(conn->c_path[0].cp_transport_data);
  221. kfree(conn->c_path);
  222. kmem_cache_free(rds_conn_slab, conn);
  223. conn = parent->c_passive;
  224. } else {
  225. parent->c_passive = conn;
  226. rds_cong_add_conn(conn);
  227. rds_conn_count++;
  228. }
  229. } else {
  230. /* Creating normal conn */
  231. struct rds_connection *found;
  232. found = rds_conn_lookup(net, head, laddr, faddr, trans);
  233. if (found) {
  234. struct rds_conn_path *cp;
  235. int i;
  236. for (i = 0; i < npaths; i++) {
  237. cp = &conn->c_path[i];
  238. /* The ->conn_alloc invocation may have
  239. * allocated resource for all paths, so all
  240. * of them may have to be freed here.
  241. */
  242. if (cp->cp_transport_data)
  243. trans->conn_free(cp->cp_transport_data);
  244. }
  245. kfree(conn->c_path);
  246. kmem_cache_free(rds_conn_slab, conn);
  247. conn = found;
  248. } else {
  249. conn->c_my_gen_num = rds_gen_num;
  250. conn->c_peer_gen_num = 0;
  251. hlist_add_head_rcu(&conn->c_hash_node, head);
  252. rds_cong_add_conn(conn);
  253. rds_conn_count++;
  254. }
  255. }
  256. spin_unlock_irqrestore(&rds_conn_lock, flags);
  257. out:
  258. return conn;
  259. }
  260. struct rds_connection *rds_conn_create(struct net *net,
  261. __be32 laddr, __be32 faddr,
  262. struct rds_transport *trans, gfp_t gfp)
  263. {
  264. return __rds_conn_create(net, laddr, faddr, trans, gfp, 0);
  265. }
  266. EXPORT_SYMBOL_GPL(rds_conn_create);
  267. struct rds_connection *rds_conn_create_outgoing(struct net *net,
  268. __be32 laddr, __be32 faddr,
  269. struct rds_transport *trans, gfp_t gfp)
  270. {
  271. return __rds_conn_create(net, laddr, faddr, trans, gfp, 1);
  272. }
  273. EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
  274. void rds_conn_shutdown(struct rds_conn_path *cp)
  275. {
  276. struct rds_connection *conn = cp->cp_conn;
  277. /* shut it down unless it's down already */
  278. if (!rds_conn_path_transition(cp, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
  279. /*
  280. * Quiesce the connection mgmt handlers before we start tearing
  281. * things down. We don't hold the mutex for the entire
  282. * duration of the shutdown operation, else we may be
  283. * deadlocking with the CM handler. Instead, the CM event
  284. * handler is supposed to check for state DISCONNECTING
  285. */
  286. mutex_lock(&cp->cp_cm_lock);
  287. if (!rds_conn_path_transition(cp, RDS_CONN_UP,
  288. RDS_CONN_DISCONNECTING) &&
  289. !rds_conn_path_transition(cp, RDS_CONN_ERROR,
  290. RDS_CONN_DISCONNECTING)) {
  291. rds_conn_path_error(cp,
  292. "shutdown called in state %d\n",
  293. atomic_read(&cp->cp_state));
  294. mutex_unlock(&cp->cp_cm_lock);
  295. return;
  296. }
  297. mutex_unlock(&cp->cp_cm_lock);
  298. wait_event(cp->cp_waitq,
  299. !test_bit(RDS_IN_XMIT, &cp->cp_flags));
  300. wait_event(cp->cp_waitq,
  301. !test_bit(RDS_RECV_REFILL, &cp->cp_flags));
  302. conn->c_trans->conn_path_shutdown(cp);
  303. rds_conn_path_reset(cp);
  304. if (!rds_conn_path_transition(cp, RDS_CONN_DISCONNECTING,
  305. RDS_CONN_DOWN) &&
  306. !rds_conn_path_transition(cp, RDS_CONN_ERROR,
  307. RDS_CONN_DOWN)) {
  308. /* This can happen - eg when we're in the middle of tearing
  309. * down the connection, and someone unloads the rds module.
  310. * Quite reproducible with loopback connections.
  311. * Mostly harmless.
  312. *
  313. * Note that this also happens with rds-tcp because
  314. * we could have triggered rds_conn_path_drop in irq
  315. * mode from rds_tcp_state change on the receipt of
  316. * a FIN, thus we need to recheck for RDS_CONN_ERROR
  317. * here.
  318. */
  319. rds_conn_path_error(cp, "%s: failed to transition "
  320. "to state DOWN, current state "
  321. "is %d\n", __func__,
  322. atomic_read(&cp->cp_state));
  323. return;
  324. }
  325. }
  326. /* Then reconnect if it's still live.
  327. * The passive side of an IB loopback connection is never added
  328. * to the conn hash, so we never trigger a reconnect on this
  329. * conn - the reconnect is always triggered by the active peer. */
  330. cancel_delayed_work_sync(&cp->cp_conn_w);
  331. if (conn->c_destroy_in_prog)
  332. return;
  333. rcu_read_lock();
  334. if (!hlist_unhashed(&conn->c_hash_node)) {
  335. rcu_read_unlock();
  336. rds_queue_reconnect(cp);
  337. } else {
  338. rcu_read_unlock();
  339. }
  340. }
  341. /* destroy a single rds_conn_path. rds_conn_destroy() iterates over
  342. * all paths using rds_conn_path_destroy()
  343. */
  344. static void rds_conn_path_destroy(struct rds_conn_path *cp)
  345. {
  346. struct rds_message *rm, *rtmp;
  347. if (!cp->cp_transport_data)
  348. return;
  349. /* make sure lingering queued work won't try to ref the conn */
  350. cancel_delayed_work_sync(&cp->cp_send_w);
  351. cancel_delayed_work_sync(&cp->cp_recv_w);
  352. rds_conn_path_drop(cp, true);
  353. flush_work(&cp->cp_down_w);
  354. /* tear down queued messages */
  355. list_for_each_entry_safe(rm, rtmp,
  356. &cp->cp_send_queue,
  357. m_conn_item) {
  358. list_del_init(&rm->m_conn_item);
  359. BUG_ON(!list_empty(&rm->m_sock_item));
  360. rds_message_put(rm);
  361. }
  362. if (cp->cp_xmit_rm)
  363. rds_message_put(cp->cp_xmit_rm);
  364. cp->cp_conn->c_trans->conn_free(cp->cp_transport_data);
  365. }
  366. /*
  367. * Stop and free a connection.
  368. *
  369. * This can only be used in very limited circumstances. It assumes that once
  370. * the conn has been shutdown that no one else is referencing the connection.
  371. * We can only ensure this in the rmmod path in the current code.
  372. */
  373. void rds_conn_destroy(struct rds_connection *conn)
  374. {
  375. unsigned long flags;
  376. int i;
  377. struct rds_conn_path *cp;
  378. int npaths = (conn->c_trans->t_mp_capable ? RDS_MPATH_WORKERS : 1);
  379. rdsdebug("freeing conn %p for %pI4 -> "
  380. "%pI4\n", conn, &conn->c_laddr,
  381. &conn->c_faddr);
  382. conn->c_destroy_in_prog = 1;
  383. /* Ensure conn will not be scheduled for reconnect */
  384. spin_lock_irq(&rds_conn_lock);
  385. hlist_del_init_rcu(&conn->c_hash_node);
  386. spin_unlock_irq(&rds_conn_lock);
  387. synchronize_rcu();
  388. /* shut the connection down */
  389. for (i = 0; i < npaths; i++) {
  390. cp = &conn->c_path[i];
  391. rds_conn_path_destroy(cp);
  392. BUG_ON(!list_empty(&cp->cp_retrans));
  393. }
  394. /*
  395. * The congestion maps aren't freed up here. They're
  396. * freed by rds_cong_exit() after all the connections
  397. * have been freed.
  398. */
  399. rds_cong_remove_conn(conn);
  400. kfree(conn->c_path);
  401. kmem_cache_free(rds_conn_slab, conn);
  402. spin_lock_irqsave(&rds_conn_lock, flags);
  403. rds_conn_count--;
  404. spin_unlock_irqrestore(&rds_conn_lock, flags);
  405. }
  406. EXPORT_SYMBOL_GPL(rds_conn_destroy);
  407. static void rds_conn_message_info(struct socket *sock, unsigned int len,
  408. struct rds_info_iterator *iter,
  409. struct rds_info_lengths *lens,
  410. int want_send)
  411. {
  412. struct hlist_head *head;
  413. struct list_head *list;
  414. struct rds_connection *conn;
  415. struct rds_message *rm;
  416. unsigned int total = 0;
  417. unsigned long flags;
  418. size_t i;
  419. int j;
  420. len /= sizeof(struct rds_info_message);
  421. rcu_read_lock();
  422. for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
  423. i++, head++) {
  424. hlist_for_each_entry_rcu(conn, head, c_hash_node) {
  425. struct rds_conn_path *cp;
  426. int npaths;
  427. npaths = (conn->c_trans->t_mp_capable ?
  428. RDS_MPATH_WORKERS : 1);
  429. for (j = 0; j < npaths; j++) {
  430. cp = &conn->c_path[j];
  431. if (want_send)
  432. list = &cp->cp_send_queue;
  433. else
  434. list = &cp->cp_retrans;
  435. spin_lock_irqsave(&cp->cp_lock, flags);
  436. /* XXX too lazy to maintain counts.. */
  437. list_for_each_entry(rm, list, m_conn_item) {
  438. total++;
  439. if (total <= len)
  440. rds_inc_info_copy(&rm->m_inc,
  441. iter,
  442. conn->c_laddr,
  443. conn->c_faddr,
  444. 0);
  445. }
  446. spin_unlock_irqrestore(&cp->cp_lock, flags);
  447. }
  448. }
  449. }
  450. rcu_read_unlock();
  451. lens->nr = total;
  452. lens->each = sizeof(struct rds_info_message);
  453. }
  454. static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
  455. struct rds_info_iterator *iter,
  456. struct rds_info_lengths *lens)
  457. {
  458. rds_conn_message_info(sock, len, iter, lens, 1);
  459. }
  460. static void rds_conn_message_info_retrans(struct socket *sock,
  461. unsigned int len,
  462. struct rds_info_iterator *iter,
  463. struct rds_info_lengths *lens)
  464. {
  465. rds_conn_message_info(sock, len, iter, lens, 0);
  466. }
  467. void rds_for_each_conn_info(struct socket *sock, unsigned int len,
  468. struct rds_info_iterator *iter,
  469. struct rds_info_lengths *lens,
  470. int (*visitor)(struct rds_connection *, void *),
  471. size_t item_len)
  472. {
  473. uint64_t buffer[(item_len + 7) / 8];
  474. struct hlist_head *head;
  475. struct rds_connection *conn;
  476. size_t i;
  477. rcu_read_lock();
  478. lens->nr = 0;
  479. lens->each = item_len;
  480. for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
  481. i++, head++) {
  482. hlist_for_each_entry_rcu(conn, head, c_hash_node) {
  483. /* XXX no c_lock usage.. */
  484. if (!visitor(conn, buffer))
  485. continue;
  486. /* We copy as much as we can fit in the buffer,
  487. * but we count all items so that the caller
  488. * can resize the buffer. */
  489. if (len >= item_len) {
  490. rds_info_copy(iter, buffer, item_len);
  491. len -= item_len;
  492. }
  493. lens->nr++;
  494. }
  495. }
  496. rcu_read_unlock();
  497. }
  498. EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
  499. static void rds_walk_conn_path_info(struct socket *sock, unsigned int len,
  500. struct rds_info_iterator *iter,
  501. struct rds_info_lengths *lens,
  502. int (*visitor)(struct rds_conn_path *, void *),
  503. size_t item_len)
  504. {
  505. u64 buffer[(item_len + 7) / 8];
  506. struct hlist_head *head;
  507. struct rds_connection *conn;
  508. size_t i;
  509. int j;
  510. rcu_read_lock();
  511. lens->nr = 0;
  512. lens->each = item_len;
  513. for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
  514. i++, head++) {
  515. hlist_for_each_entry_rcu(conn, head, c_hash_node) {
  516. struct rds_conn_path *cp;
  517. int npaths;
  518. npaths = (conn->c_trans->t_mp_capable ?
  519. RDS_MPATH_WORKERS : 1);
  520. for (j = 0; j < npaths; j++) {
  521. cp = &conn->c_path[j];
  522. /* XXX no cp_lock usage.. */
  523. if (!visitor(cp, buffer))
  524. continue;
  525. }
  526. /* We copy as much as we can fit in the buffer,
  527. * but we count all items so that the caller
  528. * can resize the buffer.
  529. */
  530. if (len >= item_len) {
  531. rds_info_copy(iter, buffer, item_len);
  532. len -= item_len;
  533. }
  534. lens->nr++;
  535. }
  536. }
  537. rcu_read_unlock();
  538. }
  539. static int rds_conn_info_visitor(struct rds_conn_path *cp, void *buffer)
  540. {
  541. struct rds_info_connection *cinfo = buffer;
  542. cinfo->next_tx_seq = cp->cp_next_tx_seq;
  543. cinfo->next_rx_seq = cp->cp_next_rx_seq;
  544. cinfo->laddr = cp->cp_conn->c_laddr;
  545. cinfo->faddr = cp->cp_conn->c_faddr;
  546. strncpy(cinfo->transport, cp->cp_conn->c_trans->t_name,
  547. sizeof(cinfo->transport));
  548. cinfo->flags = 0;
  549. rds_conn_info_set(cinfo->flags, test_bit(RDS_IN_XMIT, &cp->cp_flags),
  550. SENDING);
  551. /* XXX Future: return the state rather than these funky bits */
  552. rds_conn_info_set(cinfo->flags,
  553. atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING,
  554. CONNECTING);
  555. rds_conn_info_set(cinfo->flags,
  556. atomic_read(&cp->cp_state) == RDS_CONN_UP,
  557. CONNECTED);
  558. return 1;
  559. }
  560. static void rds_conn_info(struct socket *sock, unsigned int len,
  561. struct rds_info_iterator *iter,
  562. struct rds_info_lengths *lens)
  563. {
  564. rds_walk_conn_path_info(sock, len, iter, lens,
  565. rds_conn_info_visitor,
  566. sizeof(struct rds_info_connection));
  567. }
  568. int rds_conn_init(void)
  569. {
  570. rds_conn_slab = kmem_cache_create("rds_connection",
  571. sizeof(struct rds_connection),
  572. 0, 0, NULL);
  573. if (!rds_conn_slab)
  574. return -ENOMEM;
  575. rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
  576. rds_info_register_func(RDS_INFO_SEND_MESSAGES,
  577. rds_conn_message_info_send);
  578. rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
  579. rds_conn_message_info_retrans);
  580. return 0;
  581. }
  582. void rds_conn_exit(void)
  583. {
  584. rds_loop_exit();
  585. WARN_ON(!hlist_empty(rds_conn_hash));
  586. kmem_cache_destroy(rds_conn_slab);
  587. rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
  588. rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
  589. rds_conn_message_info_send);
  590. rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
  591. rds_conn_message_info_retrans);
  592. }
  593. /*
  594. * Force a disconnect
  595. */
  596. void rds_conn_path_drop(struct rds_conn_path *cp, bool destroy)
  597. {
  598. atomic_set(&cp->cp_state, RDS_CONN_ERROR);
  599. if (!destroy && cp->cp_conn->c_destroy_in_prog)
  600. return;
  601. queue_work(rds_wq, &cp->cp_down_w);
  602. }
  603. EXPORT_SYMBOL_GPL(rds_conn_path_drop);
  604. void rds_conn_drop(struct rds_connection *conn)
  605. {
  606. WARN_ON(conn->c_trans->t_mp_capable);
  607. rds_conn_path_drop(&conn->c_path[0], false);
  608. }
  609. EXPORT_SYMBOL_GPL(rds_conn_drop);
  610. /*
  611. * If the connection is down, trigger a connect. We may have scheduled a
  612. * delayed reconnect however - in this case we should not interfere.
  613. */
  614. void rds_conn_path_connect_if_down(struct rds_conn_path *cp)
  615. {
  616. if (rds_conn_path_state(cp) == RDS_CONN_DOWN &&
  617. !test_and_set_bit(RDS_RECONNECT_PENDING, &cp->cp_flags))
  618. queue_delayed_work(rds_wq, &cp->cp_conn_w, 0);
  619. }
  620. EXPORT_SYMBOL_GPL(rds_conn_path_connect_if_down);
  621. void rds_conn_connect_if_down(struct rds_connection *conn)
  622. {
  623. WARN_ON(conn->c_trans->t_mp_capable);
  624. rds_conn_path_connect_if_down(&conn->c_path[0]);
  625. }
  626. EXPORT_SYMBOL_GPL(rds_conn_connect_if_down);
  627. void
  628. __rds_conn_path_error(struct rds_conn_path *cp, const char *fmt, ...)
  629. {
  630. va_list ap;
  631. va_start(ap, fmt);
  632. vprintk(fmt, ap);
  633. va_end(ap);
  634. rds_conn_path_drop(cp, false);
  635. }