connection.c 19 KB

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