/*1* Copyright (c) 2006 Oracle. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*31*/32#include <linux/kernel.h>33#include <linux/in.h>34#include <net/tcp.h>3536#include "rds.h"37#include "tcp.h"3839void rds_tcp_state_change(struct sock *sk)40{41void (*state_change)(struct sock *sk);42struct rds_connection *conn;43struct rds_tcp_connection *tc;4445read_lock_bh(&sk->sk_callback_lock);46conn = sk->sk_user_data;47if (!conn) {48state_change = sk->sk_state_change;49goto out;50}51tc = conn->c_transport_data;52state_change = tc->t_orig_state_change;5354rdsdebug("sock %p state_change to %d\n", tc->t_sock, sk->sk_state);5556switch(sk->sk_state) {57/* ignore connecting sockets as they make progress */58case TCP_SYN_SENT:59case TCP_SYN_RECV:60break;61case TCP_ESTABLISHED:62rds_connect_complete(conn);63break;64case TCP_CLOSE:65rds_conn_drop(conn);66default:67break;68}69out:70read_unlock_bh(&sk->sk_callback_lock);71state_change(sk);72}7374int rds_tcp_conn_connect(struct rds_connection *conn)75{76struct socket *sock = NULL;77struct sockaddr_in src, dest;78int ret;7980ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);81if (ret < 0)82goto out;8384rds_tcp_tune(sock);8586src.sin_family = AF_INET;87src.sin_addr.s_addr = (__force u32)conn->c_laddr;88src.sin_port = (__force u16)htons(0);8990ret = sock->ops->bind(sock, (struct sockaddr *)&src, sizeof(src));91if (ret) {92rdsdebug("bind failed with %d at address %pI4\n",93ret, &conn->c_laddr);94goto out;95}9697dest.sin_family = AF_INET;98dest.sin_addr.s_addr = (__force u32)conn->c_faddr;99dest.sin_port = (__force u16)htons(RDS_TCP_PORT);100101/*102* once we call connect() we can start getting callbacks and they103* own the socket104*/105rds_tcp_set_callbacks(sock, conn);106ret = sock->ops->connect(sock, (struct sockaddr *)&dest, sizeof(dest),107O_NONBLOCK);108sock = NULL;109110rdsdebug("connect to address %pI4 returned %d\n", &conn->c_faddr, ret);111if (ret == -EINPROGRESS)112ret = 0;113114out:115if (sock)116sock_release(sock);117return ret;118}119120/*121* Before killing the tcp socket this needs to serialize with callbacks. The122* caller has already grabbed the sending sem so we're serialized with other123* senders.124*125* TCP calls the callbacks with the sock lock so we hold it while we reset the126* callbacks to those set by TCP. Our callbacks won't execute again once we127* hold the sock lock.128*/129void rds_tcp_conn_shutdown(struct rds_connection *conn)130{131struct rds_tcp_connection *tc = conn->c_transport_data;132struct socket *sock = tc->t_sock;133134rdsdebug("shutting down conn %p tc %p sock %p\n", conn, tc, sock);135136if (sock) {137sock->ops->shutdown(sock, RCV_SHUTDOWN | SEND_SHUTDOWN);138lock_sock(sock->sk);139rds_tcp_restore_callbacks(sock, tc); /* tc->tc_sock = NULL */140141release_sock(sock->sk);142sock_release(sock);143}144145if (tc->t_tinc) {146rds_inc_put(&tc->t_tinc->ti_inc);147tc->t_tinc = NULL;148}149tc->t_tinc_hdr_rem = sizeof(struct rds_header);150tc->t_tinc_data_rem = 0;151}152153154