tcp_connect.c 4.8 KB
Newer Older
A
Andy Grover 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * 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);
43
	struct rds_conn_path *cp;
A
Andy Grover 已提交
44 45
	struct rds_tcp_connection *tc;

46
	read_lock_bh(&sk->sk_callback_lock);
47 48
	cp = sk->sk_user_data;
	if (!cp) {
A
Andy Grover 已提交
49 50 51
		state_change = sk->sk_state_change;
		goto out;
	}
52
	tc = cp->cp_transport_data;
A
Andy Grover 已提交
53 54 55 56
	state_change = tc->t_orig_state_change;

	rdsdebug("sock %p state_change to %d\n", tc->t_sock, sk->sk_state);

57 58 59 60 61 62
	switch (sk->sk_state) {
	/* ignore connecting sockets as they make progress */
	case TCP_SYN_SENT:
	case TCP_SYN_RECV:
		break;
	case TCP_ESTABLISHED:
63
		rds_connect_path_complete(cp, RDS_CONN_CONNECTING);
64 65 66
		break;
	case TCP_CLOSE_WAIT:
	case TCP_CLOSE:
67
		rds_conn_path_drop(cp);
68 69
	default:
		break;
A
Andy Grover 已提交
70 71
	}
out:
72
	read_unlock_bh(&sk->sk_callback_lock);
A
Andy Grover 已提交
73 74 75
	state_change(sk);
}

76
int rds_tcp_conn_path_connect(struct rds_conn_path *cp)
A
Andy Grover 已提交
77 78 79 80
{
	struct socket *sock = NULL;
	struct sockaddr_in src, dest;
	int ret;
81 82
	struct rds_connection *conn = cp->cp_conn;
	struct rds_tcp_connection *tc = cp->cp_transport_data;
83

84 85 86 87 88 89
	/* 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;

90
	mutex_lock(&tc->t_conn_path_lock);
A
Andy Grover 已提交
91

92
	if (rds_conn_path_up(cp)) {
93
		mutex_unlock(&tc->t_conn_path_lock);
94 95
		return 0;
	}
96 97
	ret = sock_create_kern(rds_conn_net(conn), PF_INET,
			       SOCK_STREAM, IPPROTO_TCP, &sock);
A
Andy Grover 已提交
98 99 100 101 102 103 104 105 106 107 108
	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) {
109 110
		rdsdebug("bind failed with %d at address %pI4\n",
			 ret, &conn->c_laddr);
A
Andy Grover 已提交
111 112 113 114 115 116 117 118 119 120 121
		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
	 */
122
	rds_tcp_set_callbacks(sock, cp);
A
Andy Grover 已提交
123 124 125
	ret = sock->ops->connect(sock, (struct sockaddr *)&dest, sizeof(dest),
				 O_NONBLOCK);

126
	cp->cp_outgoing = 1;
127
	rdsdebug("connect to address %pI4 returned %d\n", &conn->c_faddr, ret);
A
Andy Grover 已提交
128 129
	if (ret == -EINPROGRESS)
		ret = 0;
130 131
	if (ret == 0) {
		rds_tcp_keepalive(sock);
132
		sock = NULL;
133
	} else {
134
		rds_tcp_restore_callbacks(sock, cp->cp_transport_data);
135
	}
A
Andy Grover 已提交
136 137

out:
138
	mutex_unlock(&tc->t_conn_path_lock);
A
Andy Grover 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152
	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.
 */
153
void rds_tcp_conn_path_shutdown(struct rds_conn_path *cp)
A
Andy Grover 已提交
154
{
155
	struct rds_tcp_connection *tc = cp->cp_transport_data;
A
Andy Grover 已提交
156 157
	struct socket *sock = tc->t_sock;

158 159
	rdsdebug("shutting down conn %p tc %p sock %p\n",
		 cp->cp_conn, tc, sock);
A
Andy Grover 已提交
160 161 162 163 164 165 166 167

	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);
168
	}
A
Andy Grover 已提交
169 170 171 172 173 174 175 176

	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;
}