tcp_listen.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/gfp.h>
  35. #include <linux/in.h>
  36. #include <net/tcp.h>
  37. #include "rds.h"
  38. #include "tcp.h"
  39. int rds_tcp_keepalive(struct socket *sock)
  40. {
  41. /* values below based on xs_udp_default_timeout */
  42. int keepidle = 5; /* send a probe 'keepidle' secs after last data */
  43. int keepcnt = 5; /* number of unack'ed probes before declaring dead */
  44. int keepalive = 1;
  45. int ret = 0;
  46. ret = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  47. (char *)&keepalive, sizeof(keepalive));
  48. if (ret < 0)
  49. goto bail;
  50. ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT,
  51. (char *)&keepcnt, sizeof(keepcnt));
  52. if (ret < 0)
  53. goto bail;
  54. ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE,
  55. (char *)&keepidle, sizeof(keepidle));
  56. if (ret < 0)
  57. goto bail;
  58. /* KEEPINTVL is the interval between successive probes. We follow
  59. * the model in xs_tcp_finish_connecting() and re-use keepidle.
  60. */
  61. ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL,
  62. (char *)&keepidle, sizeof(keepidle));
  63. bail:
  64. return ret;
  65. }
  66. /* rds_tcp_accept_one_path(): if accepting on cp_index > 0, make sure the
  67. * client's ipaddr < server's ipaddr. Otherwise, close the accepted
  68. * socket and force a reconneect from smaller -> larger ip addr. The reason
  69. * we special case cp_index 0 is to allow the rds probe ping itself to itself
  70. * get through efficiently.
  71. * Since reconnects are only initiated from the node with the numerically
  72. * smaller ip address, we recycle conns in RDS_CONN_ERROR on the passive side
  73. * by moving them to CONNECTING in this function.
  74. */
  75. static
  76. struct rds_tcp_connection *rds_tcp_accept_one_path(struct rds_connection *conn)
  77. {
  78. int i;
  79. int npaths = max_t(int, 1, conn->c_npaths);
  80. /* for mprds, all paths MUST be initiated by the peer
  81. * with the smaller address.
  82. */
  83. if (rds_addr_cmp(&conn->c_faddr, &conn->c_laddr) >= 0) {
  84. /* Make sure we initiate at least one path if this
  85. * has not already been done; rds_start_mprds() will
  86. * take care of additional paths, if necessary.
  87. */
  88. if (npaths == 1)
  89. rds_conn_path_connect_if_down(&conn->c_path[0]);
  90. return NULL;
  91. }
  92. for (i = 0; i < npaths; i++) {
  93. struct rds_conn_path *cp = &conn->c_path[i];
  94. if (rds_conn_path_transition(cp, RDS_CONN_DOWN,
  95. RDS_CONN_CONNECTING) ||
  96. rds_conn_path_transition(cp, RDS_CONN_ERROR,
  97. RDS_CONN_CONNECTING)) {
  98. return cp->cp_transport_data;
  99. }
  100. }
  101. return NULL;
  102. }
  103. void rds_tcp_set_linger(struct socket *sock)
  104. {
  105. struct linger no_linger = {
  106. .l_onoff = 1,
  107. .l_linger = 0,
  108. };
  109. kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
  110. (char *)&no_linger, sizeof(no_linger));
  111. }
  112. int rds_tcp_accept_one(struct socket *sock)
  113. {
  114. struct socket *new_sock = NULL;
  115. struct rds_connection *conn;
  116. int ret;
  117. struct inet_sock *inet;
  118. struct rds_tcp_connection *rs_tcp = NULL;
  119. int conn_state;
  120. struct rds_conn_path *cp;
  121. struct in6_addr *my_addr, *peer_addr;
  122. #if !IS_ENABLED(CONFIG_IPV6)
  123. struct in6_addr saddr, daddr;
  124. #endif
  125. int dev_if = 0;
  126. if (!sock) /* module unload or netns delete in progress */
  127. return -ENETUNREACH;
  128. ret = sock_create_lite(sock->sk->sk_family,
  129. sock->sk->sk_type, sock->sk->sk_protocol,
  130. &new_sock);
  131. if (ret)
  132. goto out;
  133. ret = sock->ops->accept(sock, new_sock, O_NONBLOCK, true);
  134. if (ret < 0)
  135. goto out;
  136. /* sock_create_lite() does not get a hold on the owner module so we
  137. * need to do it here. Note that sock_release() uses sock->ops to
  138. * determine if it needs to decrement the reference count. So set
  139. * sock->ops after calling accept() in case that fails. And there's
  140. * no need to do try_module_get() as the listener should have a hold
  141. * already.
  142. */
  143. new_sock->ops = sock->ops;
  144. __module_get(new_sock->ops->owner);
  145. ret = rds_tcp_keepalive(new_sock);
  146. if (ret < 0)
  147. goto out;
  148. rds_tcp_tune(new_sock);
  149. inet = inet_sk(new_sock->sk);
  150. #if IS_ENABLED(CONFIG_IPV6)
  151. my_addr = &new_sock->sk->sk_v6_rcv_saddr;
  152. peer_addr = &new_sock->sk->sk_v6_daddr;
  153. #else
  154. ipv6_addr_set_v4mapped(inet->inet_saddr, &saddr);
  155. ipv6_addr_set_v4mapped(inet->inet_daddr, &daddr);
  156. my_addr = &saddr;
  157. peer_addr = &daddr;
  158. #endif
  159. rdsdebug("accepted family %d tcp %pI6c:%u -> %pI6c:%u\n",
  160. sock->sk->sk_family,
  161. my_addr, ntohs(inet->inet_sport),
  162. peer_addr, ntohs(inet->inet_dport));
  163. #if IS_ENABLED(CONFIG_IPV6)
  164. /* sk_bound_dev_if is not set if the peer address is not link local
  165. * address. In this case, it happens that mcast_oif is set. So
  166. * just use it.
  167. */
  168. if ((ipv6_addr_type(my_addr) & IPV6_ADDR_LINKLOCAL) &&
  169. !(ipv6_addr_type(peer_addr) & IPV6_ADDR_LINKLOCAL)) {
  170. struct ipv6_pinfo *inet6;
  171. inet6 = inet6_sk(new_sock->sk);
  172. dev_if = inet6->mcast_oif;
  173. } else {
  174. dev_if = new_sock->sk->sk_bound_dev_if;
  175. }
  176. #endif
  177. conn = rds_conn_create(sock_net(sock->sk),
  178. my_addr, peer_addr,
  179. &rds_tcp_transport, GFP_KERNEL, dev_if);
  180. if (IS_ERR(conn)) {
  181. ret = PTR_ERR(conn);
  182. goto out;
  183. }
  184. /* An incoming SYN request came in, and TCP just accepted it.
  185. *
  186. * If the client reboots, this conn will need to be cleaned up.
  187. * rds_tcp_state_change() will do that cleanup
  188. */
  189. rs_tcp = rds_tcp_accept_one_path(conn);
  190. if (!rs_tcp)
  191. goto rst_nsk;
  192. mutex_lock(&rs_tcp->t_conn_path_lock);
  193. cp = rs_tcp->t_cpath;
  194. conn_state = rds_conn_path_state(cp);
  195. WARN_ON(conn_state == RDS_CONN_UP);
  196. if (conn_state != RDS_CONN_CONNECTING && conn_state != RDS_CONN_ERROR)
  197. goto rst_nsk;
  198. if (rs_tcp->t_sock) {
  199. /* Duelling SYN has been handled in rds_tcp_accept_one() */
  200. rds_tcp_reset_callbacks(new_sock, cp);
  201. /* rds_connect_path_complete() marks RDS_CONN_UP */
  202. rds_connect_path_complete(cp, RDS_CONN_RESETTING);
  203. } else {
  204. rds_tcp_set_callbacks(new_sock, cp);
  205. rds_connect_path_complete(cp, RDS_CONN_CONNECTING);
  206. }
  207. new_sock = NULL;
  208. ret = 0;
  209. if (conn->c_npaths == 0)
  210. rds_send_ping(cp->cp_conn, cp->cp_index);
  211. goto out;
  212. rst_nsk:
  213. /* reset the newly returned accept sock and bail.
  214. * It is safe to set linger on new_sock because the RDS connection
  215. * has not been brought up on new_sock, so no RDS-level data could
  216. * be pending on it. By setting linger, we achieve the side-effect
  217. * of avoiding TIME_WAIT state on new_sock.
  218. */
  219. rds_tcp_set_linger(new_sock);
  220. kernel_sock_shutdown(new_sock, SHUT_RDWR);
  221. ret = 0;
  222. out:
  223. if (rs_tcp)
  224. mutex_unlock(&rs_tcp->t_conn_path_lock);
  225. if (new_sock)
  226. sock_release(new_sock);
  227. return ret;
  228. }
  229. void rds_tcp_listen_data_ready(struct sock *sk)
  230. {
  231. void (*ready)(struct sock *sk);
  232. rdsdebug("listen data ready sk %p\n", sk);
  233. read_lock_bh(&sk->sk_callback_lock);
  234. ready = sk->sk_user_data;
  235. if (!ready) { /* check for teardown race */
  236. ready = sk->sk_data_ready;
  237. goto out;
  238. }
  239. /*
  240. * ->sk_data_ready is also called for a newly established child socket
  241. * before it has been accepted and the accepter has set up their
  242. * data_ready.. we only want to queue listen work for our listening
  243. * socket
  244. *
  245. * (*ready)() may be null if we are racing with netns delete, and
  246. * the listen socket is being torn down.
  247. */
  248. if (sk->sk_state == TCP_LISTEN)
  249. rds_tcp_accept_work(sk);
  250. else
  251. ready = rds_tcp_listen_sock_def_readable(sock_net(sk));
  252. out:
  253. read_unlock_bh(&sk->sk_callback_lock);
  254. if (ready)
  255. ready(sk);
  256. }
  257. struct socket *rds_tcp_listen_init(struct net *net, bool isv6)
  258. {
  259. struct socket *sock = NULL;
  260. struct sockaddr_storage ss;
  261. struct sockaddr_in6 *sin6;
  262. struct sockaddr_in *sin;
  263. int addr_len;
  264. int ret;
  265. ret = sock_create_kern(net, isv6 ? PF_INET6 : PF_INET, SOCK_STREAM,
  266. IPPROTO_TCP, &sock);
  267. if (ret < 0) {
  268. rdsdebug("could not create %s listener socket: %d\n",
  269. isv6 ? "IPv6" : "IPv4", ret);
  270. goto out;
  271. }
  272. sock->sk->sk_reuse = SK_CAN_REUSE;
  273. rds_tcp_nonagle(sock);
  274. write_lock_bh(&sock->sk->sk_callback_lock);
  275. sock->sk->sk_user_data = sock->sk->sk_data_ready;
  276. sock->sk->sk_data_ready = rds_tcp_listen_data_ready;
  277. write_unlock_bh(&sock->sk->sk_callback_lock);
  278. if (isv6) {
  279. sin6 = (struct sockaddr_in6 *)&ss;
  280. sin6->sin6_family = PF_INET6;
  281. sin6->sin6_addr = in6addr_any;
  282. sin6->sin6_port = (__force u16)htons(RDS_TCP_PORT);
  283. sin6->sin6_scope_id = 0;
  284. sin6->sin6_flowinfo = 0;
  285. addr_len = sizeof(*sin6);
  286. } else {
  287. sin = (struct sockaddr_in *)&ss;
  288. sin->sin_family = PF_INET;
  289. sin->sin_addr.s_addr = INADDR_ANY;
  290. sin->sin_port = (__force u16)htons(RDS_TCP_PORT);
  291. addr_len = sizeof(*sin);
  292. }
  293. ret = sock->ops->bind(sock, (struct sockaddr *)&ss, addr_len);
  294. if (ret < 0) {
  295. rdsdebug("could not bind %s listener socket: %d\n",
  296. isv6 ? "IPv6" : "IPv4", ret);
  297. goto out;
  298. }
  299. ret = sock->ops->listen(sock, 64);
  300. if (ret < 0)
  301. goto out;
  302. return sock;
  303. out:
  304. if (sock)
  305. sock_release(sock);
  306. return NULL;
  307. }
  308. void rds_tcp_listen_stop(struct socket *sock, struct work_struct *acceptor)
  309. {
  310. struct sock *sk;
  311. if (!sock)
  312. return;
  313. sk = sock->sk;
  314. /* serialize with and prevent further callbacks */
  315. lock_sock(sk);
  316. write_lock_bh(&sk->sk_callback_lock);
  317. if (sk->sk_user_data) {
  318. sk->sk_data_ready = sk->sk_user_data;
  319. sk->sk_user_data = NULL;
  320. }
  321. write_unlock_bh(&sk->sk_callback_lock);
  322. release_sock(sk);
  323. /* wait for accepts to stop and close the socket */
  324. flush_workqueue(rds_wq);
  325. flush_work(acceptor);
  326. sock_release(sock);
  327. }