connection.c 26 KB

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