tcp.c 20 KB

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