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