2009-08-21 20:28:31 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is available to you under a choice of one of two
|
|
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
|
|
* General Public License (GPL) Version 2, available from the file
|
|
|
|
* COPYING in the main directory of this source tree, or the
|
|
|
|
* OpenIB.org BSD license below:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <net/tcp.h>
|
|
|
|
|
|
|
|
#include "rds.h"
|
|
|
|
#include "tcp.h"
|
|
|
|
|
|
|
|
void rds_tcp_state_change(struct sock *sk)
|
|
|
|
{
|
|
|
|
void (*state_change)(struct sock *sk);
|
2016-07-01 07:11:14 +08:00
|
|
|
struct rds_conn_path *cp;
|
2009-08-21 20:28:31 +08:00
|
|
|
struct rds_tcp_connection *tc;
|
|
|
|
|
2016-05-18 08:44:08 +08:00
|
|
|
read_lock_bh(&sk->sk_callback_lock);
|
2016-07-01 07:11:14 +08:00
|
|
|
cp = sk->sk_user_data;
|
|
|
|
if (!cp) {
|
2009-08-21 20:28:31 +08:00
|
|
|
state_change = sk->sk_state_change;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-07-01 07:11:14 +08:00
|
|
|
tc = cp->cp_transport_data;
|
2009-08-21 20:28:31 +08:00
|
|
|
state_change = tc->t_orig_state_change;
|
|
|
|
|
|
|
|
rdsdebug("sock %p state_change to %d\n", tc->t_sock, sk->sk_state);
|
|
|
|
|
2016-06-18 23:46:31 +08:00
|
|
|
switch (sk->sk_state) {
|
|
|
|
/* ignore connecting sockets as they make progress */
|
|
|
|
case TCP_SYN_SENT:
|
|
|
|
case TCP_SYN_RECV:
|
|
|
|
break;
|
|
|
|
case TCP_ESTABLISHED:
|
RDS: TCP: Force every connection to be initiated by numerically smaller IP address
When 2 RDS peers initiate an RDS-TCP connection simultaneously,
there is a potential for "duelling syns" on either/both sides.
See commit 241b271952eb ("RDS-TCP: Reset tcp callbacks if re-using an
outgoing socket in rds_tcp_accept_one()") for a description of this
condition, and the arbitration logic which ensures that the
numerically large IP address in the TCP connection is bound to the
RDS_TCP_PORT ("canonical ordering").
The rds_connection should not be marked as RDS_CONN_UP until the
arbitration logic has converged for the following reason. The sender
may start transmitting RDS datagrams as soon as RDS_CONN_UP is set,
and since the sender removes all datagrams from the rds_connection's
cp_retrans queue based on TCP acks. If the TCP ack was sent from
a tcp socket that got reset as part of duel aribitration (but
before data was delivered to the receivers RDS socket layer),
the sender may end up prematurely freeing the datagram, and
the datagram is no longer reliably deliverable.
This patch remedies that condition by making sure that, upon
receipt of 3WH completion state change notification of TCP_ESTABLISHED
in rds_tcp_state_change, we mark the rds_connection as RDS_CONN_UP
if, and only if, the IP addresses and ports for the connection are
canonically ordered. In all other cases, rds_tcp_state_change will
force an rds_conn_path_drop(), and rds_queue_reconnect() on
both peers will restart the connection to ensure canonical ordering.
A side-effect of enforcing this condition in rds_tcp_state_change()
is that rds_tcp_accept_one_path() can now be refactored for simplicity.
It is also no longer possible to encounter an RDS_CONN_UP connection in
the arbitration logic in rds_tcp_accept_one().
Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com>
Acked-by: Santosh Shilimkar <santosh.shilimkar@oracle.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-11-17 05:29:50 +08:00
|
|
|
/* Force the peer to reconnect so that we have the
|
|
|
|
* TCP ports going from <smaller-ip>.<transient> to
|
|
|
|
* <larger-ip>.<RDS_TCP_PORT>. We avoid marking the
|
|
|
|
* RDS connection as RDS_CONN_UP until the reconnect,
|
|
|
|
* to avoid RDS datagram loss.
|
|
|
|
*/
|
|
|
|
if (cp->cp_conn->c_laddr > cp->cp_conn->c_faddr &&
|
|
|
|
rds_conn_path_transition(cp, RDS_CONN_CONNECTING,
|
|
|
|
RDS_CONN_ERROR)) {
|
|
|
|
rds_conn_path_drop(cp);
|
|
|
|
} else {
|
|
|
|
rds_connect_path_complete(cp, RDS_CONN_CONNECTING);
|
|
|
|
}
|
2016-06-18 23:46:31 +08:00
|
|
|
break;
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
case TCP_CLOSE:
|
2016-07-01 07:11:14 +08:00
|
|
|
rds_conn_path_drop(cp);
|
2016-06-18 23:46:31 +08:00
|
|
|
default:
|
|
|
|
break;
|
2009-08-21 20:28:31 +08:00
|
|
|
}
|
|
|
|
out:
|
2016-05-18 08:44:08 +08:00
|
|
|
read_unlock_bh(&sk->sk_callback_lock);
|
2009-08-21 20:28:31 +08:00
|
|
|
state_change(sk);
|
|
|
|
}
|
|
|
|
|
2016-07-01 07:11:16 +08:00
|
|
|
int rds_tcp_conn_path_connect(struct rds_conn_path *cp)
|
2009-08-21 20:28:31 +08:00
|
|
|
{
|
|
|
|
struct socket *sock = NULL;
|
|
|
|
struct sockaddr_in src, dest;
|
|
|
|
int ret;
|
2016-07-01 07:11:16 +08:00
|
|
|
struct rds_connection *conn = cp->cp_conn;
|
|
|
|
struct rds_tcp_connection *tc = cp->cp_transport_data;
|
2016-05-03 02:24:52 +08:00
|
|
|
|
2016-07-14 18:51:03 +08:00
|
|
|
/* for multipath rds,we only trigger the connection after
|
|
|
|
* the handshake probe has determined the number of paths.
|
|
|
|
*/
|
|
|
|
if (cp->cp_index > 0 && cp->cp_conn->c_npaths < 2)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
2016-07-01 07:11:12 +08:00
|
|
|
mutex_lock(&tc->t_conn_path_lock);
|
2009-08-21 20:28:31 +08:00
|
|
|
|
2016-07-01 07:11:16 +08:00
|
|
|
if (rds_conn_path_up(cp)) {
|
2016-07-01 07:11:12 +08:00
|
|
|
mutex_unlock(&tc->t_conn_path_lock);
|
2016-05-03 02:24:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-05 13:43:25 +08:00
|
|
|
ret = sock_create_kern(rds_conn_net(conn), PF_INET,
|
|
|
|
SOCK_STREAM, IPPROTO_TCP, &sock);
|
2009-08-21 20:28:31 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rds_tcp_tune(sock);
|
|
|
|
|
|
|
|
src.sin_family = AF_INET;
|
|
|
|
src.sin_addr.s_addr = (__force u32)conn->c_laddr;
|
|
|
|
src.sin_port = (__force u16)htons(0);
|
|
|
|
|
|
|
|
ret = sock->ops->bind(sock, (struct sockaddr *)&src, sizeof(src));
|
|
|
|
if (ret) {
|
2010-02-02 20:43:59 +08:00
|
|
|
rdsdebug("bind failed with %d at address %pI4\n",
|
|
|
|
ret, &conn->c_laddr);
|
2009-08-21 20:28:31 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest.sin_family = AF_INET;
|
|
|
|
dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
|
|
|
|
dest.sin_port = (__force u16)htons(RDS_TCP_PORT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* once we call connect() we can start getting callbacks and they
|
|
|
|
* own the socket
|
|
|
|
*/
|
2016-07-01 07:11:14 +08:00
|
|
|
rds_tcp_set_callbacks(sock, cp);
|
2009-08-21 20:28:31 +08:00
|
|
|
ret = sock->ops->connect(sock, (struct sockaddr *)&dest, sizeof(dest),
|
|
|
|
O_NONBLOCK);
|
|
|
|
|
2016-07-01 07:11:16 +08:00
|
|
|
cp->cp_outgoing = 1;
|
2010-02-02 20:43:59 +08:00
|
|
|
rdsdebug("connect to address %pI4 returned %d\n", &conn->c_faddr, ret);
|
2009-08-21 20:28:31 +08:00
|
|
|
if (ret == -EINPROGRESS)
|
|
|
|
ret = 0;
|
2015-08-05 13:43:26 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
rds_tcp_keepalive(sock);
|
2014-10-02 05:49:53 +08:00
|
|
|
sock = NULL;
|
2015-08-05 13:43:26 +08:00
|
|
|
} else {
|
2016-07-01 07:11:16 +08:00
|
|
|
rds_tcp_restore_callbacks(sock, cp->cp_transport_data);
|
2015-08-05 13:43:26 +08:00
|
|
|
}
|
2009-08-21 20:28:31 +08:00
|
|
|
|
|
|
|
out:
|
2016-07-01 07:11:12 +08:00
|
|
|
mutex_unlock(&tc->t_conn_path_lock);
|
2009-08-21 20:28:31 +08:00
|
|
|
if (sock)
|
|
|
|
sock_release(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before killing the tcp socket this needs to serialize with callbacks. The
|
|
|
|
* caller has already grabbed the sending sem so we're serialized with other
|
|
|
|
* senders.
|
|
|
|
*
|
|
|
|
* TCP calls the callbacks with the sock lock so we hold it while we reset the
|
|
|
|
* callbacks to those set by TCP. Our callbacks won't execute again once we
|
|
|
|
* hold the sock lock.
|
|
|
|
*/
|
2016-07-01 07:11:10 +08:00
|
|
|
void rds_tcp_conn_path_shutdown(struct rds_conn_path *cp)
|
2009-08-21 20:28:31 +08:00
|
|
|
{
|
2016-07-01 07:11:10 +08:00
|
|
|
struct rds_tcp_connection *tc = cp->cp_transport_data;
|
2009-08-21 20:28:31 +08:00
|
|
|
struct socket *sock = tc->t_sock;
|
|
|
|
|
2016-07-01 07:11:10 +08:00
|
|
|
rdsdebug("shutting down conn %p tc %p sock %p\n",
|
|
|
|
cp->cp_conn, tc, sock);
|
2009-08-21 20:28:31 +08:00
|
|
|
|
|
|
|
if (sock) {
|
|
|
|
sock->ops->shutdown(sock, RCV_SHUTDOWN | SEND_SHUTDOWN);
|
|
|
|
lock_sock(sock->sk);
|
|
|
|
rds_tcp_restore_callbacks(sock, tc); /* tc->tc_sock = NULL */
|
|
|
|
|
|
|
|
release_sock(sock->sk);
|
|
|
|
sock_release(sock);
|
2010-05-14 18:58:26 +08:00
|
|
|
}
|
2009-08-21 20:28:31 +08:00
|
|
|
|
|
|
|
if (tc->t_tinc) {
|
|
|
|
rds_inc_put(&tc->t_tinc->ti_inc);
|
|
|
|
tc->t_tinc = NULL;
|
|
|
|
}
|
|
|
|
tc->t_tinc_hdr_rem = sizeof(struct rds_header);
|
|
|
|
tc->t_tinc_data_rem = 0;
|
|
|
|
}
|