connection.c 25 KB

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