tcp.c 19 KB

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