tcp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/slab.h>
  35. #include <linux/in.h>
  36. #include <linux/module.h>
  37. #include <net/tcp.h>
  38. #include <net/net_namespace.h>
  39. #include <net/netns/generic.h>
  40. #include "rds.h"
  41. #include "tcp.h"
  42. /* only for info exporting */
  43. static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
  44. static LIST_HEAD(rds_tcp_tc_list);
  45. static unsigned int rds_tcp_tc_count;
  46. /* Track rds_tcp_connection structs so they can be cleaned up */
  47. static DEFINE_SPINLOCK(rds_tcp_conn_lock);
  48. static LIST_HEAD(rds_tcp_conn_list);
  49. static atomic_t rds_tcp_unloading = ATOMIC_INIT(0);
  50. static struct kmem_cache *rds_tcp_conn_slab;
  51. static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
  52. void __user *buffer, size_t *lenp,
  53. loff_t *fpos);
  54. static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
  55. static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
  56. static struct ctl_table rds_tcp_sysctl_table[] = {
  57. #define RDS_TCP_SNDBUF 0
  58. {
  59. .procname = "rds_tcp_sndbuf",
  60. /* data is per-net pointer */
  61. .maxlen = sizeof(int),
  62. .mode = 0644,
  63. .proc_handler = rds_tcp_skbuf_handler,
  64. .extra1 = &rds_tcp_min_sndbuf,
  65. },
  66. #define RDS_TCP_RCVBUF 1
  67. {
  68. .procname = "rds_tcp_rcvbuf",
  69. /* data is per-net pointer */
  70. .maxlen = sizeof(int),
  71. .mode = 0644,
  72. .proc_handler = rds_tcp_skbuf_handler,
  73. .extra1 = &rds_tcp_min_rcvbuf,
  74. },
  75. { }
  76. };
  77. /* doing it this way avoids calling tcp_sk() */
  78. void rds_tcp_nonagle(struct socket *sock)
  79. {
  80. int val = 1;
  81. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (void *)&val,
  82. sizeof(val));
  83. }
  84. u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
  85. {
  86. /* seq# of the last byte of data in tcp send buffer */
  87. return tcp_sk(tc->t_sock->sk)->write_seq;
  88. }
  89. u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
  90. {
  91. return tcp_sk(tc->t_sock->sk)->snd_una;
  92. }
  93. void rds_tcp_restore_callbacks(struct socket *sock,
  94. struct rds_tcp_connection *tc)
  95. {
  96. rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
  97. write_lock_bh(&sock->sk->sk_callback_lock);
  98. /* done under the callback_lock to serialize with write_space */
  99. spin_lock(&rds_tcp_tc_list_lock);
  100. list_del_init(&tc->t_list_item);
  101. rds_tcp_tc_count--;
  102. spin_unlock(&rds_tcp_tc_list_lock);
  103. tc->t_sock = NULL;
  104. sock->sk->sk_write_space = tc->t_orig_write_space;
  105. sock->sk->sk_data_ready = tc->t_orig_data_ready;
  106. sock->sk->sk_state_change = tc->t_orig_state_change;
  107. sock->sk->sk_user_data = NULL;
  108. write_unlock_bh(&sock->sk->sk_callback_lock);
  109. }
  110. /*
  111. * rds_tcp_reset_callbacks() switches the to the new sock and
  112. * returns the existing tc->t_sock.
  113. *
  114. * The only functions that set tc->t_sock are rds_tcp_set_callbacks
  115. * and rds_tcp_reset_callbacks. Send and receive trust that
  116. * it is set. The absence of RDS_CONN_UP bit protects those paths
  117. * from being called while it isn't set.
  118. */
  119. void rds_tcp_reset_callbacks(struct socket *sock,
  120. struct rds_conn_path *cp)
  121. {
  122. struct rds_tcp_connection *tc = cp->cp_transport_data;
  123. struct socket *osock = tc->t_sock;
  124. if (!osock)
  125. goto newsock;
  126. /* Need to resolve a duelling SYN between peers.
  127. * We have an outstanding SYN to this peer, which may
  128. * potentially have transitioned to the RDS_CONN_UP state,
  129. * so we must quiesce any send threads before resetting
  130. * cp_transport_data. We quiesce these threads by setting
  131. * cp_state to something other than RDS_CONN_UP, and then
  132. * waiting for any existing threads in rds_send_xmit to
  133. * complete release_in_xmit(). (Subsequent threads entering
  134. * rds_send_xmit() will bail on !rds_conn_up().
  135. *
  136. * However an incoming syn-ack at this point would end up
  137. * marking the conn as RDS_CONN_UP, and would again permit
  138. * rds_send_xmi() threads through, so ideally we would
  139. * synchronize on RDS_CONN_UP after lock_sock(), but cannot
  140. * do that: waiting on !RDS_IN_XMIT after lock_sock() may
  141. * end up deadlocking with tcp_sendmsg(), and the RDS_IN_XMIT
  142. * would not get set. As a result, we set c_state to
  143. * RDS_CONN_RESETTTING, to ensure that rds_tcp_state_change
  144. * cannot mark rds_conn_path_up() in the window before lock_sock()
  145. */
  146. atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
  147. wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
  148. lock_sock(osock->sk);
  149. /* reset receive side state for rds_tcp_data_recv() for osock */
  150. cancel_delayed_work_sync(&cp->cp_send_w);
  151. cancel_delayed_work_sync(&cp->cp_recv_w);
  152. if (tc->t_tinc) {
  153. rds_inc_put(&tc->t_tinc->ti_inc);
  154. tc->t_tinc = NULL;
  155. }
  156. tc->t_tinc_hdr_rem = sizeof(struct rds_header);
  157. tc->t_tinc_data_rem = 0;
  158. rds_tcp_restore_callbacks(osock, tc);
  159. release_sock(osock->sk);
  160. sock_release(osock);
  161. newsock:
  162. rds_send_path_reset(cp);
  163. lock_sock(sock->sk);
  164. rds_tcp_set_callbacks(sock, cp);
  165. release_sock(sock->sk);
  166. }
  167. /* Add tc to rds_tcp_tc_list and set tc->t_sock. See comments
  168. * above rds_tcp_reset_callbacks for notes about synchronization
  169. * with data path
  170. */
  171. void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
  172. {
  173. struct rds_tcp_connection *tc = cp->cp_transport_data;
  174. rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
  175. write_lock_bh(&sock->sk->sk_callback_lock);
  176. /* done under the callback_lock to serialize with write_space */
  177. spin_lock(&rds_tcp_tc_list_lock);
  178. list_add_tail(&tc->t_list_item, &rds_tcp_tc_list);
  179. rds_tcp_tc_count++;
  180. spin_unlock(&rds_tcp_tc_list_lock);
  181. /* accepted sockets need our listen data ready undone */
  182. if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
  183. sock->sk->sk_data_ready = sock->sk->sk_user_data;
  184. tc->t_sock = sock;
  185. tc->t_cpath = cp;
  186. tc->t_orig_data_ready = sock->sk->sk_data_ready;
  187. tc->t_orig_write_space = sock->sk->sk_write_space;
  188. tc->t_orig_state_change = sock->sk->sk_state_change;
  189. sock->sk->sk_user_data = cp;
  190. sock->sk->sk_data_ready = rds_tcp_data_ready;
  191. sock->sk->sk_write_space = rds_tcp_write_space;
  192. sock->sk->sk_state_change = rds_tcp_state_change;
  193. write_unlock_bh(&sock->sk->sk_callback_lock);
  194. }
  195. static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
  196. struct rds_info_iterator *iter,
  197. struct rds_info_lengths *lens)
  198. {
  199. struct rds_info_tcp_socket tsinfo;
  200. struct rds_tcp_connection *tc;
  201. unsigned long flags;
  202. struct sockaddr_in sin;
  203. struct socket *sock;
  204. spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
  205. if (len / sizeof(tsinfo) < rds_tcp_tc_count)
  206. goto out;
  207. list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
  208. sock = tc->t_sock;
  209. if (sock) {
  210. sock->ops->getname(sock, (struct sockaddr *)&sin, 0);
  211. tsinfo.local_addr = sin.sin_addr.s_addr;
  212. tsinfo.local_port = sin.sin_port;
  213. sock->ops->getname(sock, (struct sockaddr *)&sin, 1);
  214. tsinfo.peer_addr = sin.sin_addr.s_addr;
  215. tsinfo.peer_port = sin.sin_port;
  216. }
  217. tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
  218. tsinfo.data_rem = tc->t_tinc_data_rem;
  219. tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
  220. tsinfo.last_expected_una = tc->t_last_expected_una;
  221. tsinfo.last_seen_una = tc->t_last_seen_una;
  222. rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
  223. }
  224. out:
  225. lens->nr = rds_tcp_tc_count;
  226. lens->each = sizeof(tsinfo);
  227. spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
  228. }
  229. static int rds_tcp_laddr_check(struct net *net, __be32 addr)
  230. {
  231. if (inet_addr_type(net, addr) == RTN_LOCAL)
  232. return 0;
  233. return -EADDRNOTAVAIL;
  234. }
  235. static void rds_tcp_conn_free(void *arg)
  236. {
  237. struct rds_tcp_connection *tc = arg;
  238. rdsdebug("freeing tc %p\n", tc);
  239. spin_lock_bh(&rds_tcp_conn_lock);
  240. if (!tc->t_tcp_node_detached)
  241. list_del(&tc->t_tcp_node);
  242. spin_unlock_bh(&rds_tcp_conn_lock);
  243. kmem_cache_free(rds_tcp_conn_slab, tc);
  244. }
  245. static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  246. {
  247. struct rds_tcp_connection *tc;
  248. int i, j;
  249. int ret = 0;
  250. for (i = 0; i < RDS_MPATH_WORKERS; i++) {
  251. tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
  252. if (!tc) {
  253. ret = -ENOMEM;
  254. goto fail;
  255. }
  256. mutex_init(&tc->t_conn_path_lock);
  257. tc->t_sock = NULL;
  258. tc->t_tinc = NULL;
  259. tc->t_tinc_hdr_rem = sizeof(struct rds_header);
  260. tc->t_tinc_data_rem = 0;
  261. conn->c_path[i].cp_transport_data = tc;
  262. tc->t_cpath = &conn->c_path[i];
  263. tc->t_tcp_node_detached = true;
  264. rdsdebug("rds_conn_path [%d] tc %p\n", i,
  265. conn->c_path[i].cp_transport_data);
  266. }
  267. spin_lock_bh(&rds_tcp_conn_lock);
  268. for (i = 0; i < RDS_MPATH_WORKERS; i++) {
  269. tc = conn->c_path[i].cp_transport_data;
  270. tc->t_tcp_node_detached = false;
  271. list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
  272. }
  273. spin_unlock_bh(&rds_tcp_conn_lock);
  274. fail:
  275. if (ret) {
  276. for (j = 0; j < i; j++)
  277. rds_tcp_conn_free(conn->c_path[j].cp_transport_data);
  278. }
  279. return ret;
  280. }
  281. static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
  282. {
  283. struct rds_tcp_connection *tc, *_tc;
  284. list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
  285. if (tc->t_cpath->cp_conn == conn)
  286. return true;
  287. }
  288. return false;
  289. }
  290. static void rds_tcp_set_unloading(void)
  291. {
  292. atomic_set(&rds_tcp_unloading, 1);
  293. }
  294. static bool rds_tcp_is_unloading(struct rds_connection *conn)
  295. {
  296. return atomic_read(&rds_tcp_unloading) != 0;
  297. }
  298. static void rds_tcp_destroy_conns(void)
  299. {
  300. struct rds_tcp_connection *tc, *_tc;
  301. LIST_HEAD(tmp_list);
  302. /* avoid calling conn_destroy with irqs off */
  303. spin_lock_irq(&rds_tcp_conn_lock);
  304. list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
  305. if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
  306. list_move_tail(&tc->t_tcp_node, &tmp_list);
  307. }
  308. spin_unlock_irq(&rds_tcp_conn_lock);
  309. list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
  310. rds_conn_destroy(tc->t_cpath->cp_conn);
  311. }
  312. static void rds_tcp_exit(void);
  313. struct rds_transport rds_tcp_transport = {
  314. .laddr_check = rds_tcp_laddr_check,
  315. .xmit_path_prepare = rds_tcp_xmit_path_prepare,
  316. .xmit_path_complete = rds_tcp_xmit_path_complete,
  317. .xmit = rds_tcp_xmit,
  318. .recv_path = rds_tcp_recv_path,
  319. .conn_alloc = rds_tcp_conn_alloc,
  320. .conn_free = rds_tcp_conn_free,
  321. .conn_path_connect = rds_tcp_conn_path_connect,
  322. .conn_path_shutdown = rds_tcp_conn_path_shutdown,
  323. .inc_copy_to_user = rds_tcp_inc_copy_to_user,
  324. .inc_free = rds_tcp_inc_free,
  325. .stats_info_copy = rds_tcp_stats_info_copy,
  326. .exit = rds_tcp_exit,
  327. .t_owner = THIS_MODULE,
  328. .t_name = "tcp",
  329. .t_type = RDS_TRANS_TCP,
  330. .t_prefer_loopback = 1,
  331. .t_mp_capable = 1,
  332. .t_unloading = rds_tcp_is_unloading,
  333. };
  334. static unsigned int rds_tcp_netid;
  335. /* per-network namespace private data for this module */
  336. struct rds_tcp_net {
  337. struct socket *rds_tcp_listen_sock;
  338. struct work_struct rds_tcp_accept_w;
  339. struct ctl_table_header *rds_tcp_sysctl;
  340. struct ctl_table *ctl_table;
  341. int sndbuf_size;
  342. int rcvbuf_size;
  343. };
  344. /* All module specific customizations to the RDS-TCP socket should be done in
  345. * rds_tcp_tune() and applied after socket creation.
  346. */
  347. void rds_tcp_tune(struct socket *sock)
  348. {
  349. struct sock *sk = sock->sk;
  350. struct net *net = sock_net(sk);
  351. struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
  352. rds_tcp_nonagle(sock);
  353. lock_sock(sk);
  354. if (rtn->sndbuf_size > 0) {
  355. sk->sk_sndbuf = rtn->sndbuf_size;
  356. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  357. }
  358. if (rtn->rcvbuf_size > 0) {
  359. sk->sk_sndbuf = rtn->rcvbuf_size;
  360. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  361. }
  362. release_sock(sk);
  363. }
  364. static void rds_tcp_accept_worker(struct work_struct *work)
  365. {
  366. struct rds_tcp_net *rtn = container_of(work,
  367. struct rds_tcp_net,
  368. rds_tcp_accept_w);
  369. while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
  370. cond_resched();
  371. }
  372. void rds_tcp_accept_work(struct sock *sk)
  373. {
  374. struct net *net = sock_net(sk);
  375. struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
  376. queue_work(rds_wq, &rtn->rds_tcp_accept_w);
  377. }
  378. static __net_init int rds_tcp_init_net(struct net *net)
  379. {
  380. struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
  381. struct ctl_table *tbl;
  382. int err = 0;
  383. memset(rtn, 0, sizeof(*rtn));
  384. /* {snd, rcv}buf_size default to 0, which implies we let the
  385. * stack pick the value, and permit auto-tuning of buffer size.
  386. */
  387. if (net == &init_net) {
  388. tbl = rds_tcp_sysctl_table;
  389. } else {
  390. tbl = kmemdup(rds_tcp_sysctl_table,
  391. sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
  392. if (!tbl) {
  393. pr_warn("could not set allocate syctl table\n");
  394. return -ENOMEM;
  395. }
  396. rtn->ctl_table = tbl;
  397. }
  398. tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
  399. tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
  400. rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
  401. if (!rtn->rds_tcp_sysctl) {
  402. pr_warn("could not register sysctl\n");
  403. err = -ENOMEM;
  404. goto fail;
  405. }
  406. rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
  407. if (!rtn->rds_tcp_listen_sock) {
  408. pr_warn("could not set up listen sock\n");
  409. unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
  410. rtn->rds_tcp_sysctl = NULL;
  411. err = -EAFNOSUPPORT;
  412. goto fail;
  413. }
  414. INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
  415. return 0;
  416. fail:
  417. if (net != &init_net)
  418. kfree(tbl);
  419. return err;
  420. }
  421. static void __net_exit rds_tcp_exit_net(struct net *net)
  422. {
  423. struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
  424. if (rtn->rds_tcp_sysctl)
  425. unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
  426. if (net != &init_net && rtn->ctl_table)
  427. kfree(rtn->ctl_table);
  428. /* If rds_tcp_exit_net() is called as a result of netns deletion,
  429. * the rds_tcp_kill_sock() device notifier would already have cleaned
  430. * up the listen socket, thus there is no work to do in this function.
  431. *
  432. * If rds_tcp_exit_net() is called as a result of module unload,
  433. * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
  434. * we do need to clean up the listen socket here.
  435. */
  436. if (rtn->rds_tcp_listen_sock) {
  437. struct socket *lsock = rtn->rds_tcp_listen_sock;
  438. rtn->rds_tcp_listen_sock = NULL;
  439. rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
  440. }
  441. }
  442. static struct pernet_operations rds_tcp_net_ops = {
  443. .init = rds_tcp_init_net,
  444. .exit = rds_tcp_exit_net,
  445. .id = &rds_tcp_netid,
  446. .size = sizeof(struct rds_tcp_net),
  447. };
  448. static void rds_tcp_kill_sock(struct net *net)
  449. {
  450. struct rds_tcp_connection *tc, *_tc;
  451. LIST_HEAD(tmp_list);
  452. struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
  453. struct socket *lsock = rtn->rds_tcp_listen_sock;
  454. rtn->rds_tcp_listen_sock = NULL;
  455. rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
  456. spin_lock_bh(&rds_tcp_conn_lock);
  457. list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
  458. struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
  459. if (net != c_net || !tc->t_sock)
  460. continue;
  461. if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
  462. list_move_tail(&tc->t_tcp_node, &tmp_list);
  463. } else {
  464. list_del(&tc->t_tcp_node);
  465. tc->t_tcp_node_detached = true;
  466. }
  467. }
  468. spin_unlock_bh(&rds_tcp_conn_lock);
  469. list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
  470. rds_conn_destroy(tc->t_cpath->cp_conn);
  471. }
  472. void *rds_tcp_listen_sock_def_readable(struct net *net)
  473. {
  474. struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
  475. struct socket *lsock = rtn->rds_tcp_listen_sock;
  476. if (!lsock)
  477. return NULL;
  478. return lsock->sk->sk_user_data;
  479. }
  480. static int rds_tcp_dev_event(struct notifier_block *this,
  481. unsigned long event, void *ptr)
  482. {
  483. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  484. /* rds-tcp registers as a pernet subys, so the ->exit will only
  485. * get invoked after network acitivity has quiesced. We need to
  486. * clean up all sockets to quiesce network activity, and use
  487. * the unregistration of the per-net loopback device as a trigger
  488. * to start that cleanup.
  489. */
  490. if (event == NETDEV_UNREGISTER_FINAL &&
  491. dev->ifindex == LOOPBACK_IFINDEX)
  492. rds_tcp_kill_sock(dev_net(dev));
  493. return NOTIFY_DONE;
  494. }
  495. static struct notifier_block rds_tcp_dev_notifier = {
  496. .notifier_call = rds_tcp_dev_event,
  497. .priority = -10, /* must be called after other network notifiers */
  498. };
  499. /* when sysctl is used to modify some kernel socket parameters,this
  500. * function resets the RDS connections in that netns so that we can
  501. * restart with new parameters. The assumption is that such reset
  502. * events are few and far-between.
  503. */
  504. static void rds_tcp_sysctl_reset(struct net *net)
  505. {
  506. struct rds_tcp_connection *tc, *_tc;
  507. spin_lock_bh(&rds_tcp_conn_lock);
  508. list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
  509. struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
  510. if (net != c_net || !tc->t_sock)
  511. continue;
  512. /* reconnect with new parameters */
  513. rds_conn_path_drop(tc->t_cpath, false);
  514. }
  515. spin_unlock_bh(&rds_tcp_conn_lock);
  516. }
  517. static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
  518. void __user *buffer, size_t *lenp,
  519. loff_t *fpos)
  520. {
  521. struct net *net = current->nsproxy->net_ns;
  522. int err;
  523. err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
  524. if (err < 0) {
  525. pr_warn("Invalid input. Must be >= %d\n",
  526. *(int *)(ctl->extra1));
  527. return err;
  528. }
  529. if (write)
  530. rds_tcp_sysctl_reset(net);
  531. return 0;
  532. }
  533. static void rds_tcp_exit(void)
  534. {
  535. rds_tcp_set_unloading();
  536. synchronize_rcu();
  537. rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
  538. unregister_pernet_subsys(&rds_tcp_net_ops);
  539. if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
  540. pr_warn("could not unregister rds_tcp_dev_notifier\n");
  541. rds_tcp_destroy_conns();
  542. rds_trans_unregister(&rds_tcp_transport);
  543. rds_tcp_recv_exit();
  544. kmem_cache_destroy(rds_tcp_conn_slab);
  545. }
  546. module_exit(rds_tcp_exit);
  547. static int rds_tcp_init(void)
  548. {
  549. int ret;
  550. rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
  551. sizeof(struct rds_tcp_connection),
  552. 0, 0, NULL);
  553. if (!rds_tcp_conn_slab) {
  554. ret = -ENOMEM;
  555. goto out;
  556. }
  557. ret = rds_tcp_recv_init();
  558. if (ret)
  559. goto out_slab;
  560. ret = register_pernet_subsys(&rds_tcp_net_ops);
  561. if (ret)
  562. goto out_recv;
  563. ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
  564. if (ret) {
  565. pr_warn("could not register rds_tcp_dev_notifier\n");
  566. goto out_pernet;
  567. }
  568. rds_trans_register(&rds_tcp_transport);
  569. rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
  570. goto out;
  571. out_pernet:
  572. unregister_pernet_subsys(&rds_tcp_net_ops);
  573. out_recv:
  574. rds_tcp_recv_exit();
  575. out_slab:
  576. kmem_cache_destroy(rds_tcp_conn_slab);
  577. out:
  578. return ret;
  579. }
  580. module_init(rds_tcp_init);
  581. MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
  582. MODULE_DESCRIPTION("RDS: TCP transport");
  583. MODULE_LICENSE("Dual BSD/GPL");