tcp_connect.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2006, 2017 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/in.h>
  35. #include <net/tcp.h>
  36. #include "rds.h"
  37. #include "tcp.h"
  38. void rds_tcp_state_change(struct sock *sk)
  39. {
  40. void (*state_change)(struct sock *sk);
  41. struct rds_conn_path *cp;
  42. struct rds_tcp_connection *tc;
  43. read_lock_bh(&sk->sk_callback_lock);
  44. cp = sk->sk_user_data;
  45. if (!cp) {
  46. state_change = sk->sk_state_change;
  47. goto out;
  48. }
  49. tc = cp->cp_transport_data;
  50. state_change = tc->t_orig_state_change;
  51. rdsdebug("sock %p state_change to %d\n", tc->t_sock, sk->sk_state);
  52. switch (sk->sk_state) {
  53. /* ignore connecting sockets as they make progress */
  54. case TCP_SYN_SENT:
  55. case TCP_SYN_RECV:
  56. break;
  57. case TCP_ESTABLISHED:
  58. /* Force the peer to reconnect so that we have the
  59. * TCP ports going from <smaller-ip>.<transient> to
  60. * <larger-ip>.<RDS_TCP_PORT>. We avoid marking the
  61. * RDS connection as RDS_CONN_UP until the reconnect,
  62. * to avoid RDS datagram loss.
  63. */
  64. if (rds_addr_cmp(&cp->cp_conn->c_laddr,
  65. &cp->cp_conn->c_faddr) >= 0 &&
  66. rds_conn_path_transition(cp, RDS_CONN_CONNECTING,
  67. RDS_CONN_ERROR)) {
  68. rds_conn_path_drop(cp, false);
  69. } else {
  70. rds_connect_path_complete(cp, RDS_CONN_CONNECTING);
  71. }
  72. break;
  73. case TCP_CLOSING:
  74. case TCP_TIME_WAIT:
  75. if (wq_has_sleeper(&tc->t_recv_done_waitq))
  76. wake_up(&tc->t_recv_done_waitq);
  77. break;
  78. case TCP_CLOSE_WAIT:
  79. case TCP_LAST_ACK:
  80. case TCP_CLOSE:
  81. if (wq_has_sleeper(&tc->t_recv_done_waitq))
  82. wake_up(&tc->t_recv_done_waitq);
  83. rds_conn_path_drop(cp, false);
  84. break;
  85. default:
  86. break;
  87. }
  88. out:
  89. read_unlock_bh(&sk->sk_callback_lock);
  90. state_change(sk);
  91. }
  92. int rds_tcp_conn_path_connect(struct rds_conn_path *cp)
  93. {
  94. struct socket *sock = NULL;
  95. struct sockaddr_in6 sin6;
  96. struct sockaddr_in sin;
  97. struct sockaddr *addr;
  98. int port_low, port_high, port;
  99. int port_groups, groups_left;
  100. int addrlen;
  101. bool isv6;
  102. int ret;
  103. struct rds_connection *conn = cp->cp_conn;
  104. struct rds_tcp_connection *tc = cp->cp_transport_data;
  105. /* for multipath rds,we only trigger the connection after
  106. * the handshake probe has determined the number of paths.
  107. */
  108. if (cp->cp_index > 0 && cp->cp_conn->c_npaths < 2)
  109. return -EAGAIN;
  110. mutex_lock(&tc->t_conn_path_lock);
  111. if (rds_conn_path_up(cp)) {
  112. mutex_unlock(&tc->t_conn_path_lock);
  113. return 0;
  114. }
  115. if (ipv6_addr_v4mapped(&conn->c_laddr)) {
  116. ret = sock_create_kern(rds_conn_net(conn), PF_INET,
  117. SOCK_STREAM, IPPROTO_TCP, &sock);
  118. isv6 = false;
  119. } else {
  120. ret = sock_create_kern(rds_conn_net(conn), PF_INET6,
  121. SOCK_STREAM, IPPROTO_TCP, &sock);
  122. isv6 = true;
  123. }
  124. if (ret < 0)
  125. goto out;
  126. if (!rds_tcp_tune(sock)) {
  127. ret = -EINVAL;
  128. goto out;
  129. }
  130. if (isv6) {
  131. sin6.sin6_family = AF_INET6;
  132. sin6.sin6_addr = conn->c_laddr;
  133. sin6.sin6_port = 0;
  134. sin6.sin6_flowinfo = 0;
  135. sin6.sin6_scope_id = conn->c_dev_if;
  136. addr = (struct sockaddr *)&sin6;
  137. addrlen = sizeof(sin6);
  138. } else {
  139. sin.sin_family = AF_INET;
  140. sin.sin_addr.s_addr = conn->c_laddr.s6_addr32[3];
  141. sin.sin_port = 0;
  142. addr = (struct sockaddr *)&sin;
  143. addrlen = sizeof(sin);
  144. }
  145. /* encode cp->cp_index in lowest bits of source-port */
  146. inet_get_local_port_range(rds_conn_net(conn), &port_low, &port_high);
  147. port_low = ALIGN(port_low, RDS_MPATH_WORKERS);
  148. port_groups = (port_high - port_low + 1) / RDS_MPATH_WORKERS;
  149. ret = -EADDRINUSE;
  150. groups_left = port_groups;
  151. while (groups_left-- > 0 && ret) {
  152. if (++tc->t_client_port_group >= port_groups)
  153. tc->t_client_port_group = 0;
  154. port = port_low +
  155. tc->t_client_port_group * RDS_MPATH_WORKERS +
  156. cp->cp_index;
  157. if (isv6)
  158. sin6.sin6_port = htons(port);
  159. else
  160. sin.sin_port = htons(port);
  161. ret = kernel_bind(sock, (struct sockaddr_unsized *)addr,
  162. addrlen);
  163. }
  164. if (ret) {
  165. rdsdebug("bind failed with %d at address %pI6c\n",
  166. ret, &conn->c_laddr);
  167. goto out;
  168. }
  169. if (isv6) {
  170. sin6.sin6_family = AF_INET6;
  171. sin6.sin6_addr = conn->c_faddr;
  172. sin6.sin6_port = htons(RDS_TCP_PORT);
  173. sin6.sin6_flowinfo = 0;
  174. sin6.sin6_scope_id = conn->c_dev_if;
  175. addr = (struct sockaddr *)&sin6;
  176. addrlen = sizeof(sin6);
  177. } else {
  178. sin.sin_family = AF_INET;
  179. sin.sin_addr.s_addr = conn->c_faddr.s6_addr32[3];
  180. sin.sin_port = htons(RDS_TCP_PORT);
  181. addr = (struct sockaddr *)&sin;
  182. addrlen = sizeof(sin);
  183. }
  184. /*
  185. * once we call connect() we can start getting callbacks and they
  186. * own the socket
  187. */
  188. rds_tcp_set_callbacks(sock, cp);
  189. ret = kernel_connect(sock, (struct sockaddr_unsized *)addr, addrlen, O_NONBLOCK);
  190. rdsdebug("connect to address %pI6c returned %d\n", &conn->c_faddr, ret);
  191. if (ret == -EINPROGRESS)
  192. ret = 0;
  193. if (ret == 0) {
  194. rds_tcp_keepalive(sock);
  195. sock = NULL;
  196. } else {
  197. rds_tcp_restore_callbacks(sock, cp->cp_transport_data);
  198. }
  199. out:
  200. mutex_unlock(&tc->t_conn_path_lock);
  201. if (sock)
  202. sock_release(sock);
  203. return ret;
  204. }
  205. /*
  206. * Before killing the tcp socket this needs to serialize with callbacks. The
  207. * caller has already grabbed the sending sem so we're serialized with other
  208. * senders.
  209. *
  210. * TCP calls the callbacks with the sock lock so we hold it while we reset the
  211. * callbacks to those set by TCP. Our callbacks won't execute again once we
  212. * hold the sock lock.
  213. */
  214. void rds_tcp_conn_path_shutdown(struct rds_conn_path *cp)
  215. {
  216. struct rds_tcp_connection *tc = cp->cp_transport_data;
  217. struct socket *sock = tc->t_sock;
  218. struct sock *sk;
  219. unsigned int rounds;
  220. rdsdebug("shutting down conn %p tc %p sock %p\n",
  221. cp->cp_conn, tc, sock);
  222. if (sock) {
  223. sk = sock->sk;
  224. if (rds_destroy_pending(cp->cp_conn))
  225. sock_no_linger(sk);
  226. sock->ops->shutdown(sock, SHUT_WR);
  227. /* after sending FIN,
  228. * wait until we processed all incoming messages
  229. * and we're sure that there won't be any more:
  230. * i.e. state CLOSING, TIME_WAIT, CLOSE_WAIT,
  231. * LAST_ACK, or CLOSE (RFC 793).
  232. *
  233. * Give up waiting after 5 seconds and allow messages
  234. * to theoretically get dropped, if the TCP transition
  235. * didn't happen.
  236. */
  237. rounds = 0;
  238. do {
  239. /* we need to ensure messages are dequeued here
  240. * since "rds_recv_worker" only dispatches messages
  241. * while the connection is still in RDS_CONN_UP
  242. * and there is no guarantee that "rds_tcp_data_ready"
  243. * was called nor that "sk_data_ready" still points to
  244. * it.
  245. */
  246. rds_tcp_recv_path(cp);
  247. } while (!wait_event_timeout(tc->t_recv_done_waitq,
  248. (sk->sk_state == TCP_CLOSING ||
  249. sk->sk_state == TCP_TIME_WAIT ||
  250. sk->sk_state == TCP_CLOSE_WAIT ||
  251. sk->sk_state == TCP_LAST_ACK ||
  252. sk->sk_state == TCP_CLOSE) &&
  253. skb_queue_empty_lockless(&sk->sk_receive_queue),
  254. msecs_to_jiffies(100)) &&
  255. ++rounds < 50);
  256. lock_sock(sk);
  257. /* discard messages that the peer received already */
  258. tc->t_last_seen_una = rds_tcp_snd_una(tc);
  259. rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc),
  260. rds_tcp_is_acked);
  261. rds_tcp_restore_callbacks(sock, tc); /* tc->tc_sock = NULL */
  262. release_sock(sk);
  263. sock_release(sock);
  264. }
  265. if (tc->t_tinc) {
  266. rds_inc_put(&tc->t_tinc->ti_inc);
  267. tc->t_tinc = NULL;
  268. }
  269. tc->t_tinc_hdr_rem = sizeof(struct rds_header);
  270. tc->t_tinc_data_rem = 0;
  271. }