tcp.c 19 KB

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