1a246b010SChuck Lever /* 2a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 3a246b010SChuck Lever * 4a246b010SChuck Lever * Client-side transport implementation for sockets. 5a246b010SChuck Lever * 6113aa838SAlan Cox * TCP callback races fixes (C) 1998 Red Hat 7113aa838SAlan Cox * TCP send fixes (C) 1998 Red Hat 8a246b010SChuck Lever * TCP NFS related read + write fixes 9a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 10a246b010SChuck Lever * 11a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 12a246b010SChuck Lever * Fix behaviour when socket buffer is full. 13a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1455aa4f58SChuck Lever * 1555aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 168f9d5b1aSChuck Lever * 178f9d5b1aSChuck Lever * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 188f9d5b1aSChuck Lever * <gilles.quillard@bull.net> 19a246b010SChuck Lever */ 20a246b010SChuck Lever 21a246b010SChuck Lever #include <linux/types.h> 22a246b010SChuck Lever #include <linux/slab.h> 23bc25571eS\"Talpey, Thomas\ #include <linux/module.h> 24a246b010SChuck Lever #include <linux/capability.h> 25a246b010SChuck Lever #include <linux/pagemap.h> 26a246b010SChuck Lever #include <linux/errno.h> 27a246b010SChuck Lever #include <linux/socket.h> 28a246b010SChuck Lever #include <linux/in.h> 29a246b010SChuck Lever #include <linux/net.h> 30a246b010SChuck Lever #include <linux/mm.h> 31a246b010SChuck Lever #include <linux/udp.h> 32a246b010SChuck Lever #include <linux/tcp.h> 33a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 3402107148SChuck Lever #include <linux/sunrpc/sched.h> 3549c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 36a246b010SChuck Lever #include <linux/file.h> 3744b98efdSRicardo Labiaga #ifdef CONFIG_NFS_V4_1 3844b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 3944b98efdSRicardo Labiaga #endif 40a246b010SChuck Lever 41a246b010SChuck Lever #include <net/sock.h> 42a246b010SChuck Lever #include <net/checksum.h> 43a246b010SChuck Lever #include <net/udp.h> 44a246b010SChuck Lever #include <net/tcp.h> 45a246b010SChuck Lever 469903cd1cSChuck Lever /* 47c556b754SChuck Lever * xprtsock tunables 48c556b754SChuck Lever */ 49c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 50c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; 51c556b754SChuck Lever 52c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 53c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 54c556b754SChuck Lever 557d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 5625fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 577d1e8255STrond Myklebust 58c556b754SChuck Lever /* 59fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 60fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 61fbf76683SChuck Lever * directory become the union of all files registered there. 62fbf76683SChuck Lever * 63fbf76683SChuck Lever * We simply need to make sure that we don't collide with 64fbf76683SChuck Lever * someone else's file names! 65fbf76683SChuck Lever */ 66fbf76683SChuck Lever 67fbf76683SChuck Lever #ifdef RPC_DEBUG 68fbf76683SChuck Lever 69fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 70fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 71fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 72fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 73fbf76683SChuck Lever 74fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 75fbf76683SChuck Lever 76fbf76683SChuck Lever /* 77fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 78fbf76683SChuck Lever * socket buffers for existing UDP transports 79fbf76683SChuck Lever */ 80fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 81fbf76683SChuck Lever { 82fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_UDP, 83fbf76683SChuck Lever .procname = "udp_slot_table_entries", 84fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 85fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 86fbf76683SChuck Lever .mode = 0644, 87fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 88fbf76683SChuck Lever .strategy = &sysctl_intvec, 89fbf76683SChuck Lever .extra1 = &min_slot_table_size, 90fbf76683SChuck Lever .extra2 = &max_slot_table_size 91fbf76683SChuck Lever }, 92fbf76683SChuck Lever { 93fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_TCP, 94fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 95fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 96fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 97fbf76683SChuck Lever .mode = 0644, 98fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 99fbf76683SChuck Lever .strategy = &sysctl_intvec, 100fbf76683SChuck Lever .extra1 = &min_slot_table_size, 101fbf76683SChuck Lever .extra2 = &max_slot_table_size 102fbf76683SChuck Lever }, 103fbf76683SChuck Lever { 104fbf76683SChuck Lever .ctl_name = CTL_MIN_RESVPORT, 105fbf76683SChuck Lever .procname = "min_resvport", 106fbf76683SChuck Lever .data = &xprt_min_resvport, 107fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 108fbf76683SChuck Lever .mode = 0644, 109fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 110fbf76683SChuck Lever .strategy = &sysctl_intvec, 111fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 112fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 113fbf76683SChuck Lever }, 114fbf76683SChuck Lever { 115fbf76683SChuck Lever .ctl_name = CTL_MAX_RESVPORT, 116fbf76683SChuck Lever .procname = "max_resvport", 117fbf76683SChuck Lever .data = &xprt_max_resvport, 118fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 119fbf76683SChuck Lever .mode = 0644, 120fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 121fbf76683SChuck Lever .strategy = &sysctl_intvec, 122fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 123fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 124fbf76683SChuck Lever }, 125fbf76683SChuck Lever { 12625fe6142STrond Myklebust .procname = "tcp_fin_timeout", 12725fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 12825fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 12925fe6142STrond Myklebust .mode = 0644, 13025fe6142STrond Myklebust .proc_handler = &proc_dointvec_jiffies, 13125fe6142STrond Myklebust .strategy = sysctl_jiffies 13225fe6142STrond Myklebust }, 13325fe6142STrond Myklebust { 134fbf76683SChuck Lever .ctl_name = 0, 135fbf76683SChuck Lever }, 136fbf76683SChuck Lever }; 137fbf76683SChuck Lever 138fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 139fbf76683SChuck Lever { 140fbf76683SChuck Lever .ctl_name = CTL_SUNRPC, 141fbf76683SChuck Lever .procname = "sunrpc", 142fbf76683SChuck Lever .mode = 0555, 143fbf76683SChuck Lever .child = xs_tunables_table 144fbf76683SChuck Lever }, 145fbf76683SChuck Lever { 146fbf76683SChuck Lever .ctl_name = 0, 147fbf76683SChuck Lever }, 148fbf76683SChuck Lever }; 149fbf76683SChuck Lever 150fbf76683SChuck Lever #endif 151fbf76683SChuck Lever 152fbf76683SChuck Lever /* 15303bf4b70SChuck Lever * Time out for an RPC UDP socket connect. UDP socket connects are 15403bf4b70SChuck Lever * synchronous, but we set a timeout anyway in case of resource 15503bf4b70SChuck Lever * exhaustion on the local host. 15603bf4b70SChuck Lever */ 15703bf4b70SChuck Lever #define XS_UDP_CONN_TO (5U * HZ) 15803bf4b70SChuck Lever 15903bf4b70SChuck Lever /* 16003bf4b70SChuck Lever * Wait duration for an RPC TCP connection to be established. Solaris 16103bf4b70SChuck Lever * NFS over TCP uses 60 seconds, for example, which is in line with how 16203bf4b70SChuck Lever * long a server takes to reboot. 16303bf4b70SChuck Lever */ 16403bf4b70SChuck Lever #define XS_TCP_CONN_TO (60U * HZ) 16503bf4b70SChuck Lever 16603bf4b70SChuck Lever /* 16703bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16803bf4b70SChuck Lever */ 16903bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 17003bf4b70SChuck Lever 17103bf4b70SChuck Lever /* 17203bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 17303bf4b70SChuck Lever * kind of resource problem on the local host. 17403bf4b70SChuck Lever */ 17503bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17603bf4b70SChuck Lever 17703bf4b70SChuck Lever /* 17803bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17903bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 18003bf4b70SChuck Lever * 18103bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 18203bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 18303bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18403bf4b70SChuck Lever * increase over time if the server is down or not responding. 18503bf4b70SChuck Lever */ 18603bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18703bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 18803bf4b70SChuck Lever 18903bf4b70SChuck Lever /* 19003bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 19103bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 19203bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 19303bf4b70SChuck Lever */ 19403bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19503bf4b70SChuck Lever 196a246b010SChuck Lever #ifdef RPC_DEBUG 197a246b010SChuck Lever # undef RPC_DEBUG_DATA 1989903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 199a246b010SChuck Lever #endif 200a246b010SChuck Lever 201a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 2029903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 203a246b010SChuck Lever { 204a246b010SChuck Lever u8 *buf = (u8 *) packet; 205a246b010SChuck Lever int j; 206a246b010SChuck Lever 207a246b010SChuck Lever dprintk("RPC: %s\n", msg); 208a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 209a246b010SChuck Lever if (!(j & 31)) { 210a246b010SChuck Lever if (j) 211a246b010SChuck Lever dprintk("\n"); 212a246b010SChuck Lever dprintk("0x%04x ", j); 213a246b010SChuck Lever } 214a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 215a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 216a246b010SChuck Lever } 217a246b010SChuck Lever dprintk("\n"); 218a246b010SChuck Lever } 219a246b010SChuck Lever #else 2209903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 221a246b010SChuck Lever { 222a246b010SChuck Lever /* NOP */ 223a246b010SChuck Lever } 224a246b010SChuck Lever #endif 225a246b010SChuck Lever 226ffc2e518SChuck Lever struct sock_xprt { 227ffc2e518SChuck Lever struct rpc_xprt xprt; 228ee0ac0c2SChuck Lever 229ee0ac0c2SChuck Lever /* 230ee0ac0c2SChuck Lever * Network layer 231ee0ac0c2SChuck Lever */ 232ee0ac0c2SChuck Lever struct socket * sock; 233ee0ac0c2SChuck Lever struct sock * inet; 23451971139SChuck Lever 23551971139SChuck Lever /* 23651971139SChuck Lever * State of TCP reply receive 23751971139SChuck Lever */ 23851971139SChuck Lever __be32 tcp_fraghdr, 23951971139SChuck Lever tcp_xid; 24051971139SChuck Lever 24151971139SChuck Lever u32 tcp_offset, 24251971139SChuck Lever tcp_reclen; 24351971139SChuck Lever 24451971139SChuck Lever unsigned long tcp_copied, 24551971139SChuck Lever tcp_flags; 246c8475461SChuck Lever 247c8475461SChuck Lever /* 248c8475461SChuck Lever * Connection of transports 249c8475461SChuck Lever */ 25034161db6STrond Myklebust struct delayed_work connect_worker; 251d3bc9a1dSFrank van Maarseveen struct sockaddr_storage addr; 252c8475461SChuck Lever unsigned short port; 2537c6e066eSChuck Lever 2547c6e066eSChuck Lever /* 2557c6e066eSChuck Lever * UDP socket buffer size parameters 2567c6e066eSChuck Lever */ 2577c6e066eSChuck Lever size_t rcvsize, 2587c6e066eSChuck Lever sndsize; 259314dfd79SChuck Lever 260314dfd79SChuck Lever /* 261314dfd79SChuck Lever * Saved socket callback addresses 262314dfd79SChuck Lever */ 263314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 264314dfd79SChuck Lever void (*old_state_change)(struct sock *); 265314dfd79SChuck Lever void (*old_write_space)(struct sock *); 2662a9e1cfaSTrond Myklebust void (*old_error_report)(struct sock *); 267ffc2e518SChuck Lever }; 268ffc2e518SChuck Lever 269e136d092SChuck Lever /* 270e136d092SChuck Lever * TCP receive state flags 271e136d092SChuck Lever */ 272e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 273e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 274e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 275e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 276f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR (1UL << 4) 277f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR (1UL << 5) 27818dca02aSRicardo Labiaga 27918dca02aSRicardo Labiaga /* 28018dca02aSRicardo Labiaga * TCP RPC flags 28118dca02aSRicardo Labiaga */ 282f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY (1UL << 6) 283e136d092SChuck Lever 28495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 285edb267a6SChuck Lever { 28695392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 28795392c59SChuck Lever } 28895392c59SChuck Lever 28995392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 29095392c59SChuck Lever { 29195392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 29295392c59SChuck Lever } 29395392c59SChuck Lever 29495392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 29595392c59SChuck Lever { 29695392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 29795392c59SChuck Lever } 29895392c59SChuck Lever 299b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt, 300b454ae90SChuck Lever const char *protocol, 301b454ae90SChuck Lever const char *netid) 302edb267a6SChuck Lever { 30395392c59SChuck Lever struct sockaddr_in *addr = xs_addr_in(xprt); 304edb267a6SChuck Lever char *buf; 305edb267a6SChuck Lever 306edb267a6SChuck Lever buf = kzalloc(20, GFP_KERNEL); 307edb267a6SChuck Lever if (buf) { 308e0db4a78SDavid S. Miller snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr); 309edb267a6SChuck Lever } 310edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 311edb267a6SChuck Lever 312edb267a6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 313edb267a6SChuck Lever if (buf) { 314edb267a6SChuck Lever snprintf(buf, 8, "%u", 315edb267a6SChuck Lever ntohs(addr->sin_port)); 316edb267a6SChuck Lever } 317edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 318edb267a6SChuck Lever 319b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 320edb267a6SChuck Lever 321edb267a6SChuck Lever buf = kzalloc(48, GFP_KERNEL); 322edb267a6SChuck Lever if (buf) { 32321454aaaSHarvey Harrison snprintf(buf, 48, "addr=%pI4 port=%u proto=%s", 32421454aaaSHarvey Harrison &addr->sin_addr.s_addr, 325edb267a6SChuck Lever ntohs(addr->sin_port), 326b454ae90SChuck Lever protocol); 327edb267a6SChuck Lever } 328edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 329fbfe3cc6SChuck Lever 330fbfe3cc6SChuck Lever buf = kzalloc(10, GFP_KERNEL); 331fbfe3cc6SChuck Lever if (buf) { 332fbfe3cc6SChuck Lever snprintf(buf, 10, "%02x%02x%02x%02x", 333fbfe3cc6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 334fbfe3cc6SChuck Lever } 335fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 336fbfe3cc6SChuck Lever 337fbfe3cc6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 338fbfe3cc6SChuck Lever if (buf) { 339fbfe3cc6SChuck Lever snprintf(buf, 8, "%4hx", 340fbfe3cc6SChuck Lever ntohs(addr->sin_port)); 341fbfe3cc6SChuck Lever } 342fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 343756805e7SChuck Lever 344756805e7SChuck Lever buf = kzalloc(30, GFP_KERNEL); 345756805e7SChuck Lever if (buf) { 34621454aaaSHarvey Harrison snprintf(buf, 30, "%pI4.%u.%u", 34721454aaaSHarvey Harrison &addr->sin_addr.s_addr, 348756805e7SChuck Lever ntohs(addr->sin_port) >> 8, 349756805e7SChuck Lever ntohs(addr->sin_port) & 0xff); 350756805e7SChuck Lever } 351756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3524417c8c4S\"Talpey, Thomas\ 353b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 354edb267a6SChuck Lever } 355edb267a6SChuck Lever 356b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt, 357b454ae90SChuck Lever const char *protocol, 358b454ae90SChuck Lever const char *netid) 3594b6473fbSChuck Lever { 36095392c59SChuck Lever struct sockaddr_in6 *addr = xs_addr_in6(xprt); 3614b6473fbSChuck Lever char *buf; 3624b6473fbSChuck Lever 3634b6473fbSChuck Lever buf = kzalloc(40, GFP_KERNEL); 3644b6473fbSChuck Lever if (buf) { 3655b095d98SHarvey Harrison snprintf(buf, 40, "%pI6",&addr->sin6_addr); 3664b6473fbSChuck Lever } 3674b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 3684b6473fbSChuck Lever 3694b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3704b6473fbSChuck Lever if (buf) { 3714b6473fbSChuck Lever snprintf(buf, 8, "%u", 3724b6473fbSChuck Lever ntohs(addr->sin6_port)); 3734b6473fbSChuck Lever } 3744b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 3754b6473fbSChuck Lever 376b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 3774b6473fbSChuck Lever 3784b6473fbSChuck Lever buf = kzalloc(64, GFP_KERNEL); 3794b6473fbSChuck Lever if (buf) { 3805b095d98SHarvey Harrison snprintf(buf, 64, "addr=%pI6 port=%u proto=%s", 381fdb46ee7SHarvey Harrison &addr->sin6_addr, 3824b6473fbSChuck Lever ntohs(addr->sin6_port), 383b454ae90SChuck Lever protocol); 3844b6473fbSChuck Lever } 3854b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 3864b6473fbSChuck Lever 3874b6473fbSChuck Lever buf = kzalloc(36, GFP_KERNEL); 388b071195dSHarvey Harrison if (buf) 3894b7a4274SHarvey Harrison snprintf(buf, 36, "%pi6", &addr->sin6_addr); 390b071195dSHarvey Harrison 3914b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 3924b6473fbSChuck Lever 3934b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3944b6473fbSChuck Lever if (buf) { 3954b6473fbSChuck Lever snprintf(buf, 8, "%4hx", 3964b6473fbSChuck Lever ntohs(addr->sin6_port)); 3974b6473fbSChuck Lever } 3984b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 399756805e7SChuck Lever 400756805e7SChuck Lever buf = kzalloc(50, GFP_KERNEL); 401756805e7SChuck Lever if (buf) { 4025b095d98SHarvey Harrison snprintf(buf, 50, "%pI6.%u.%u", 403fdb46ee7SHarvey Harrison &addr->sin6_addr, 404756805e7SChuck Lever ntohs(addr->sin6_port) >> 8, 405756805e7SChuck Lever ntohs(addr->sin6_port) & 0xff); 406756805e7SChuck Lever } 407756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 4084417c8c4S\"Talpey, Thomas\ 409b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 410edb267a6SChuck Lever } 411edb267a6SChuck Lever 412edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 413edb267a6SChuck Lever { 41433e01dc7SChuck Lever unsigned int i; 41533e01dc7SChuck Lever 41633e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 41733e01dc7SChuck Lever switch (i) { 41833e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 41933e01dc7SChuck Lever case RPC_DISPLAY_NETID: 42033e01dc7SChuck Lever continue; 42133e01dc7SChuck Lever default: 42233e01dc7SChuck Lever kfree(xprt->address_strings[i]); 42333e01dc7SChuck Lever } 424edb267a6SChuck Lever } 425edb267a6SChuck Lever 426b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 427b4b5cc85SChuck Lever 42824c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 429b4b5cc85SChuck Lever { 430b4b5cc85SChuck Lever struct msghdr msg = { 431b4b5cc85SChuck Lever .msg_name = addr, 432b4b5cc85SChuck Lever .msg_namelen = addrlen, 43324c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 43424c5684bSTrond Myklebust }; 43524c5684bSTrond Myklebust struct kvec iov = { 43624c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 43724c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 438b4b5cc85SChuck Lever }; 439b4b5cc85SChuck Lever 44024c5684bSTrond Myklebust if (iov.iov_len != 0) 441b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 442b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 443b4b5cc85SChuck Lever } 444b4b5cc85SChuck Lever 44524c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 446b4b5cc85SChuck Lever { 44724c5684bSTrond Myklebust struct page **ppage; 44824c5684bSTrond Myklebust unsigned int remainder; 44924c5684bSTrond Myklebust int err, sent = 0; 450b4b5cc85SChuck Lever 45124c5684bSTrond Myklebust remainder = xdr->page_len - base; 45224c5684bSTrond Myklebust base += xdr->page_base; 45324c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 45424c5684bSTrond Myklebust base &= ~PAGE_MASK; 45524c5684bSTrond Myklebust for(;;) { 45624c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 45724c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 45824c5684bSTrond Myklebust 45924c5684bSTrond Myklebust remainder -= len; 46024c5684bSTrond Myklebust if (remainder != 0 || more) 46124c5684bSTrond Myklebust flags |= MSG_MORE; 46224c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 46324c5684bSTrond Myklebust if (remainder == 0 || err != len) 46424c5684bSTrond Myklebust break; 46524c5684bSTrond Myklebust sent += err; 46624c5684bSTrond Myklebust ppage++; 46724c5684bSTrond Myklebust base = 0; 46824c5684bSTrond Myklebust } 46924c5684bSTrond Myklebust if (sent == 0) 47024c5684bSTrond Myklebust return err; 47124c5684bSTrond Myklebust if (err > 0) 47224c5684bSTrond Myklebust sent += err; 47324c5684bSTrond Myklebust return sent; 474b4b5cc85SChuck Lever } 475b4b5cc85SChuck Lever 4769903cd1cSChuck Lever /** 4779903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 4789903cd1cSChuck Lever * @sock: socket to send on 4799903cd1cSChuck Lever * @addr: UDP only -- address of destination 4809903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 4819903cd1cSChuck Lever * @xdr: buffer containing this request 4829903cd1cSChuck Lever * @base: starting position in the buffer 4839903cd1cSChuck Lever * 484a246b010SChuck Lever */ 48524c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 486a246b010SChuck Lever { 48724c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 48824c5684bSTrond Myklebust int err, sent = 0; 489a246b010SChuck Lever 490262965f5SChuck Lever if (unlikely(!sock)) 491fba91afbSTrond Myklebust return -ENOTSOCK; 492262965f5SChuck Lever 493262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 49424c5684bSTrond Myklebust if (base != 0) { 49524c5684bSTrond Myklebust addr = NULL; 49624c5684bSTrond Myklebust addrlen = 0; 49724c5684bSTrond Myklebust } 498262965f5SChuck Lever 49924c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 50024c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 50124c5684bSTrond Myklebust remainder -= len; 50224c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 50324c5684bSTrond Myklebust if (remainder == 0 || err != len) 504a246b010SChuck Lever goto out; 50524c5684bSTrond Myklebust sent += err; 506a246b010SChuck Lever base = 0; 507a246b010SChuck Lever } else 50824c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 509a246b010SChuck Lever 51024c5684bSTrond Myklebust if (base < xdr->page_len) { 51124c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 51224c5684bSTrond Myklebust remainder -= len; 51324c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 51424c5684bSTrond Myklebust if (remainder == 0 || err != len) 515a246b010SChuck Lever goto out; 51624c5684bSTrond Myklebust sent += err; 517a246b010SChuck Lever base = 0; 51824c5684bSTrond Myklebust } else 51924c5684bSTrond Myklebust base -= xdr->page_len; 52024c5684bSTrond Myklebust 52124c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 52224c5684bSTrond Myklebust return sent; 52324c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 524a246b010SChuck Lever out: 52524c5684bSTrond Myklebust if (sent == 0) 52624c5684bSTrond Myklebust return err; 52724c5684bSTrond Myklebust if (err > 0) 52824c5684bSTrond Myklebust sent += err; 52924c5684bSTrond Myklebust return sent; 530a246b010SChuck Lever } 531a246b010SChuck Lever 532b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 533b6ddf64fSTrond Myklebust { 534b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 535b6ddf64fSTrond Myklebust 536b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 537b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 538b6ddf64fSTrond Myklebust } 539b6ddf64fSTrond Myklebust 5409903cd1cSChuck Lever /** 541262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 542262965f5SChuck Lever * @task: task to put to sleep 5439903cd1cSChuck Lever * 544a246b010SChuck Lever */ 5455e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 546a246b010SChuck Lever { 547262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 548262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 549ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 5505e3771ceSTrond Myklebust int ret = 0; 551a246b010SChuck Lever 55246121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 553262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 554262965f5SChuck Lever req->rq_slen); 555a246b010SChuck Lever 556262965f5SChuck Lever /* Protect against races with write_space */ 557262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 558a246b010SChuck Lever 559262965f5SChuck Lever /* Don't race with disconnect */ 560b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 561b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 5625e3771ceSTrond Myklebust ret = -EAGAIN; 563b6ddf64fSTrond Myklebust /* 564b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 565b6ddf64fSTrond Myklebust * window size 566b6ddf64fSTrond Myklebust */ 567b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 568b6ddf64fSTrond Myklebust transport->inet->sk_write_pending++; 569b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 570b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 571b6ddf64fSTrond Myklebust } 572b6ddf64fSTrond Myklebust } else { 573b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 5745e3771ceSTrond Myklebust ret = -ENOTCONN; 575b6ddf64fSTrond Myklebust } 576a246b010SChuck Lever 577262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5785e3771ceSTrond Myklebust return ret; 579a246b010SChuck Lever } 580a246b010SChuck Lever 5819903cd1cSChuck Lever /** 582262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5839903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5849903cd1cSChuck Lever * 5859903cd1cSChuck Lever * Return values: 5869903cd1cSChuck Lever * 0: The request has been sent 5879903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5889903cd1cSChuck Lever * complete the request 589262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 5909903cd1cSChuck Lever * other: Some other error occured, the request was not sent 5919903cd1cSChuck Lever */ 592262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 593a246b010SChuck Lever { 594a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 595a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 596ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 597262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 598262965f5SChuck Lever int status; 599262965f5SChuck Lever 600262965f5SChuck Lever xs_pktdump("packet data:", 601262965f5SChuck Lever req->rq_svec->iov_base, 602262965f5SChuck Lever req->rq_svec->iov_len); 603262965f5SChuck Lever 60401d37c42STrond Myklebust if (!xprt_bound(xprt)) 60501d37c42STrond Myklebust return -ENOTCONN; 606ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 60795392c59SChuck Lever xs_addr(xprt), 608ee0ac0c2SChuck Lever xprt->addrlen, xdr, 609ee0ac0c2SChuck Lever req->rq_bytes_sent); 610262965f5SChuck Lever 611262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 612262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 613262965f5SChuck Lever 6142199700fSTrond Myklebust if (status >= 0) { 6151321d8d9SChuck Lever task->tk_bytes_sent += status; 6162199700fSTrond Myklebust if (status >= req->rq_slen) 617262965f5SChuck Lever return 0; 618262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 619262965f5SChuck Lever status = -EAGAIN; 6202199700fSTrond Myklebust } 621c8485e4dSTrond Myklebust if (!transport->sock) 622c8485e4dSTrond Myklebust goto out; 623262965f5SChuck Lever 624262965f5SChuck Lever switch (status) { 625fba91afbSTrond Myklebust case -ENOTSOCK: 626fba91afbSTrond Myklebust status = -ENOTCONN; 627fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 628fba91afbSTrond Myklebust break; 629b6ddf64fSTrond Myklebust case -EAGAIN: 6305e3771ceSTrond Myklebust status = xs_nospace(task); 631b6ddf64fSTrond Myklebust break; 632c8485e4dSTrond Myklebust default: 633c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 634c8485e4dSTrond Myklebust -status); 635262965f5SChuck Lever case -ENETUNREACH: 636262965f5SChuck Lever case -EPIPE: 637262965f5SChuck Lever case -ECONNREFUSED: 638262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 639262965f5SChuck Lever * prompts ECONNREFUSED. */ 640b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 641262965f5SChuck Lever } 642c8485e4dSTrond Myklebust out: 643262965f5SChuck Lever return status; 644262965f5SChuck Lever } 645262965f5SChuck Lever 646e06799f9STrond Myklebust /** 647e06799f9STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 648e06799f9STrond Myklebust * @xprt: transport 649e06799f9STrond Myklebust * 650e06799f9STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 651e06799f9STrond Myklebust * equivalent of shutdown(SHUT_WR); 652e06799f9STrond Myklebust */ 653e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 654e06799f9STrond Myklebust { 655e06799f9STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 656e06799f9STrond Myklebust struct socket *sock = transport->sock; 657e06799f9STrond Myklebust 658e06799f9STrond Myklebust if (sock != NULL) 659e06799f9STrond Myklebust kernel_sock_shutdown(sock, SHUT_WR); 660e06799f9STrond Myklebust } 661e06799f9STrond Myklebust 662808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 663808012fbSChuck Lever { 664808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 665808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 666808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 667808012fbSChuck Lever } 668808012fbSChuck Lever 669262965f5SChuck Lever /** 670262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 671262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 672262965f5SChuck Lever * 673262965f5SChuck Lever * Return values: 674262965f5SChuck Lever * 0: The request has been sent 675262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 676262965f5SChuck Lever * complete the request 677262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 678262965f5SChuck Lever * other: Some other error occured, the request was not sent 679262965f5SChuck Lever * 680262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 681262965f5SChuck Lever * if sendmsg is not able to make progress? 682262965f5SChuck Lever */ 683262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 684262965f5SChuck Lever { 685262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 686262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 687ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 688262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 689b595bb15SChuck Lever int status; 690a246b010SChuck Lever 691808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 692262965f5SChuck Lever 693262965f5SChuck Lever xs_pktdump("packet data:", 694262965f5SChuck Lever req->rq_svec->iov_base, 695262965f5SChuck Lever req->rq_svec->iov_len); 696a246b010SChuck Lever 697a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 698a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 699262965f5SChuck Lever * called sendmsg(). */ 700a246b010SChuck Lever while (1) { 701ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 702ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 703a246b010SChuck Lever 704262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 705262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 706262965f5SChuck Lever 707262965f5SChuck Lever if (unlikely(status < 0)) 708a246b010SChuck Lever break; 709a246b010SChuck Lever 710a246b010SChuck Lever /* If we've sent the entire packet, immediately 711a246b010SChuck Lever * reset the count of bytes sent. */ 712262965f5SChuck Lever req->rq_bytes_sent += status; 713ef759a2eSChuck Lever task->tk_bytes_sent += status; 714262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 715a246b010SChuck Lever req->rq_bytes_sent = 0; 716a246b010SChuck Lever return 0; 717a246b010SChuck Lever } 718262965f5SChuck Lever 71906b4b681STrond Myklebust if (status != 0) 72006b4b681STrond Myklebust continue; 721a246b010SChuck Lever status = -EAGAIN; 722a246b010SChuck Lever break; 723a246b010SChuck Lever } 724c8485e4dSTrond Myklebust if (!transport->sock) 725c8485e4dSTrond Myklebust goto out; 726a246b010SChuck Lever 727262965f5SChuck Lever switch (status) { 728fba91afbSTrond Myklebust case -ENOTSOCK: 729fba91afbSTrond Myklebust status = -ENOTCONN; 730fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 731fba91afbSTrond Myklebust break; 732262965f5SChuck Lever case -EAGAIN: 7335e3771ceSTrond Myklebust status = xs_nospace(task); 734262965f5SChuck Lever break; 735262965f5SChuck Lever default: 736262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 737262965f5SChuck Lever -status); 738a246b010SChuck Lever case -ECONNRESET: 73955420c24STrond Myklebust case -EPIPE: 740e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 741a246b010SChuck Lever case -ECONNREFUSED: 742a246b010SChuck Lever case -ENOTCONN: 743a246b010SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 744a246b010SChuck Lever } 745c8485e4dSTrond Myklebust out: 746a246b010SChuck Lever return status; 747a246b010SChuck Lever } 748a246b010SChuck Lever 7499903cd1cSChuck Lever /** 750e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 751e0ab53deSTrond Myklebust * @xprt: transport 752e0ab53deSTrond Myklebust * @task: rpc task 753e0ab53deSTrond Myklebust * 754e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 755e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 756e0ab53deSTrond Myklebust * the server. 757e0ab53deSTrond Myklebust */ 758e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 759e0ab53deSTrond Myklebust { 760e0ab53deSTrond Myklebust struct rpc_rqst *req; 761e0ab53deSTrond Myklebust 762e0ab53deSTrond Myklebust if (task != xprt->snd_task) 763e0ab53deSTrond Myklebust return; 764e0ab53deSTrond Myklebust if (task == NULL) 765e0ab53deSTrond Myklebust goto out_release; 766e0ab53deSTrond Myklebust req = task->tk_rqstp; 767e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 768e0ab53deSTrond Myklebust goto out_release; 769e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 770e0ab53deSTrond Myklebust goto out_release; 771e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 772e0ab53deSTrond Myklebust out_release: 773e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 774e0ab53deSTrond Myklebust } 775e0ab53deSTrond Myklebust 7762a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7772a9e1cfaSTrond Myklebust { 7782a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7792a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7802a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7812a9e1cfaSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7822a9e1cfaSTrond Myklebust } 7832a9e1cfaSTrond Myklebust 7842a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7852a9e1cfaSTrond Myklebust { 7862a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7872a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7882a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7892a9e1cfaSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7902a9e1cfaSTrond Myklebust } 7912a9e1cfaSTrond Myklebust 792fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 793a246b010SChuck Lever { 794ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 795ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 796a246b010SChuck Lever 797fe315e76SChuck Lever if (sk == NULL) 798fe315e76SChuck Lever return; 7999903cd1cSChuck Lever 800a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 801ee0ac0c2SChuck Lever transport->inet = NULL; 802ee0ac0c2SChuck Lever transport->sock = NULL; 803a246b010SChuck Lever 804a246b010SChuck Lever sk->sk_user_data = NULL; 8052a9e1cfaSTrond Myklebust 8062a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 807a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 808a246b010SChuck Lever 809a246b010SChuck Lever sk->sk_no_check = 0; 810a246b010SChuck Lever 811a246b010SChuck Lever sock_release(sock); 812fe315e76SChuck Lever } 813fe315e76SChuck Lever 814fe315e76SChuck Lever /** 815fe315e76SChuck Lever * xs_close - close a socket 816fe315e76SChuck Lever * @xprt: transport 817fe315e76SChuck Lever * 818fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 819fe315e76SChuck Lever * on the server we want to save. 820f75e6745STrond Myklebust * 821f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 822f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 823fe315e76SChuck Lever */ 824fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 825fe315e76SChuck Lever { 826fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 827fe315e76SChuck Lever 828fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 829fe315e76SChuck Lever 830fe315e76SChuck Lever xs_reset_transport(transport); 831fe315e76SChuck Lever 832632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 8337d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 834632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 8353b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 836632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 83762da3b24STrond Myklebust xprt_disconnect_done(xprt); 838a246b010SChuck Lever } 839a246b010SChuck Lever 840f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt) 841f75e6745STrond Myklebust { 842f75e6745STrond Myklebust if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state)) 843f75e6745STrond Myklebust xs_close(xprt); 844f75e6745STrond Myklebust else 845f75e6745STrond Myklebust xs_tcp_shutdown(xprt); 846f75e6745STrond Myklebust } 847f75e6745STrond Myklebust 8489903cd1cSChuck Lever /** 8499903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8509903cd1cSChuck Lever * @xprt: doomed transport 8519903cd1cSChuck Lever * 8529903cd1cSChuck Lever */ 8539903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 854a246b010SChuck Lever { 855c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 856c8475461SChuck Lever 8579903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8589903cd1cSChuck Lever 859c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 860a246b010SChuck Lever 8619903cd1cSChuck Lever xs_close(xprt); 862edb267a6SChuck Lever xs_free_peer_addresses(xprt); 863a246b010SChuck Lever kfree(xprt->slot); 864c8541ecdSChuck Lever kfree(xprt); 865bc25571eS\"Talpey, Thomas\ module_put(THIS_MODULE); 866a246b010SChuck Lever } 867a246b010SChuck Lever 8689903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 8699903cd1cSChuck Lever { 8709903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 8719903cd1cSChuck Lever } 8729903cd1cSChuck Lever 8739903cd1cSChuck Lever /** 8749903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 8759903cd1cSChuck Lever * @sk: socket with data to read 8769903cd1cSChuck Lever * @len: how much data to read 8779903cd1cSChuck Lever * 878a246b010SChuck Lever */ 8799903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 880a246b010SChuck Lever { 881a246b010SChuck Lever struct rpc_task *task; 882a246b010SChuck Lever struct rpc_xprt *xprt; 883a246b010SChuck Lever struct rpc_rqst *rovr; 884a246b010SChuck Lever struct sk_buff *skb; 885a246b010SChuck Lever int err, repsize, copied; 886d8ed029dSAlexey Dobriyan u32 _xid; 887d8ed029dSAlexey Dobriyan __be32 *xp; 888a246b010SChuck Lever 889a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 8909903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 8919903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 892a246b010SChuck Lever goto out; 893a246b010SChuck Lever 894a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 895a246b010SChuck Lever goto out; 896a246b010SChuck Lever 897a246b010SChuck Lever if (xprt->shutdown) 898a246b010SChuck Lever goto dropit; 899a246b010SChuck Lever 900a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 901a246b010SChuck Lever if (repsize < 4) { 9029903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 903a246b010SChuck Lever goto dropit; 904a246b010SChuck Lever } 905a246b010SChuck Lever 906a246b010SChuck Lever /* Copy the XID from the skb... */ 907a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 908a246b010SChuck Lever sizeof(_xid), &_xid); 909a246b010SChuck Lever if (xp == NULL) 910a246b010SChuck Lever goto dropit; 911a246b010SChuck Lever 912a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 9134a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 914a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 915a246b010SChuck Lever if (!rovr) 916a246b010SChuck Lever goto out_unlock; 917a246b010SChuck Lever task = rovr->rq_task; 918a246b010SChuck Lever 919a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 920a246b010SChuck Lever copied = repsize; 921a246b010SChuck Lever 922a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 9231781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 9241781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 925a246b010SChuck Lever goto out_unlock; 9261781f7f5SHerbert Xu } 9271781f7f5SHerbert Xu 9281781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 929a246b010SChuck Lever 930a246b010SChuck Lever /* Something worked... */ 931adf30907SEric Dumazet dst_confirm(skb_dst(skb)); 932a246b010SChuck Lever 9331570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 9341570c1e4SChuck Lever xprt_update_rtt(task); 9351570c1e4SChuck Lever xprt_complete_rqst(task, copied); 936a246b010SChuck Lever 937a246b010SChuck Lever out_unlock: 9384a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 939a246b010SChuck Lever dropit: 940a246b010SChuck Lever skb_free_datagram(sk, skb); 941a246b010SChuck Lever out: 942a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 943a246b010SChuck Lever } 944a246b010SChuck Lever 945dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 946a246b010SChuck Lever { 94751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 948a246b010SChuck Lever size_t len, used; 949a246b010SChuck Lever char *p; 950a246b010SChuck Lever 95151971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 95251971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 9539d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 95451971139SChuck Lever transport->tcp_offset += used; 955a246b010SChuck Lever if (used != len) 956a246b010SChuck Lever return; 957808012fbSChuck Lever 95851971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 95951971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 960e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 961a246b010SChuck Lever else 962e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 96351971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 964808012fbSChuck Lever 965e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 96651971139SChuck Lever transport->tcp_offset = 0; 967808012fbSChuck Lever 968a246b010SChuck Lever /* Sanity check of the record length */ 96918dca02aSRicardo Labiaga if (unlikely(transport->tcp_reclen < 8)) { 9709903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 9713ebb067dSTrond Myklebust xprt_force_disconnect(xprt); 9729903cd1cSChuck Lever return; 973a246b010SChuck Lever } 974a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 97551971139SChuck Lever transport->tcp_reclen); 976a246b010SChuck Lever } 977a246b010SChuck Lever 97851971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 979a246b010SChuck Lever { 98051971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 981e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 98251971139SChuck Lever transport->tcp_offset = 0; 983e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 984e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 985e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 98651971139SChuck Lever transport->tcp_copied = 0; 987a246b010SChuck Lever } 988a246b010SChuck Lever } 989a246b010SChuck Lever } 990a246b010SChuck Lever 991dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 992a246b010SChuck Lever { 993a246b010SChuck Lever size_t len, used; 994a246b010SChuck Lever char *p; 995a246b010SChuck Lever 99651971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 997a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 99851971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 9999d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 100051971139SChuck Lever transport->tcp_offset += used; 1001a246b010SChuck Lever if (used != len) 1002a246b010SChuck Lever return; 1003e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 1004f4a2e418SRicardo Labiaga transport->tcp_flags |= TCP_RCV_READ_CALLDIR; 100551971139SChuck Lever transport->tcp_copied = 4; 100618dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 100718dca02aSRicardo Labiaga (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" 100818dca02aSRicardo Labiaga : "request with", 100951971139SChuck Lever ntohl(transport->tcp_xid)); 101051971139SChuck Lever xs_tcp_check_fraghdr(transport); 1011a246b010SChuck Lever } 1012a246b010SChuck Lever 101318dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 101418dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1015a246b010SChuck Lever { 101618dca02aSRicardo Labiaga size_t len, used; 101718dca02aSRicardo Labiaga u32 offset; 101818dca02aSRicardo Labiaga __be32 calldir; 101918dca02aSRicardo Labiaga 102018dca02aSRicardo Labiaga /* 102118dca02aSRicardo Labiaga * We want transport->tcp_offset to be 8 at the end of this routine 102218dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 102318dca02aSRicardo Labiaga * When this function is called for the first time, 102418dca02aSRicardo Labiaga * transport->tcp_offset is 4 (after having already read the xid). 102518dca02aSRicardo Labiaga */ 102618dca02aSRicardo Labiaga offset = transport->tcp_offset - sizeof(transport->tcp_xid); 102718dca02aSRicardo Labiaga len = sizeof(calldir) - offset; 102818dca02aSRicardo Labiaga dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); 102918dca02aSRicardo Labiaga used = xdr_skb_read_bits(desc, &calldir, len); 103018dca02aSRicardo Labiaga transport->tcp_offset += used; 103118dca02aSRicardo Labiaga if (used != len) 103218dca02aSRicardo Labiaga return; 1033f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; 1034f4a2e418SRicardo Labiaga transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 103518dca02aSRicardo Labiaga transport->tcp_flags |= TCP_RCV_COPY_DATA; 1036f4a2e418SRicardo Labiaga /* 1037f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1038f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1039f4a2e418SRicardo Labiaga */ 104018dca02aSRicardo Labiaga if (ntohl(calldir) == RPC_REPLY) 104118dca02aSRicardo Labiaga transport->tcp_flags |= TCP_RPC_REPLY; 104218dca02aSRicardo Labiaga else 104318dca02aSRicardo Labiaga transport->tcp_flags &= ~TCP_RPC_REPLY; 104418dca02aSRicardo Labiaga dprintk("RPC: reading %s CALL/REPLY flag %08x\n", 104518dca02aSRicardo Labiaga (transport->tcp_flags & TCP_RPC_REPLY) ? 104618dca02aSRicardo Labiaga "reply for" : "request with", calldir); 104718dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 104818dca02aSRicardo Labiaga } 104918dca02aSRicardo Labiaga 105044b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 105144b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 105244b98efdSRicardo Labiaga struct rpc_rqst *req) 1053a246b010SChuck Lever { 105444b98efdSRicardo Labiaga struct sock_xprt *transport = 105544b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1056a246b010SChuck Lever struct xdr_buf *rcvbuf; 1057a246b010SChuck Lever size_t len; 1058a246b010SChuck Lever ssize_t r; 1059a246b010SChuck Lever 1060a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1061f4a2e418SRicardo Labiaga 1062f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { 1063f4a2e418SRicardo Labiaga /* 1064f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1065f4a2e418SRicardo Labiaga */ 1066f4a2e418SRicardo Labiaga __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ? 1067f4a2e418SRicardo Labiaga htonl(RPC_REPLY) : 0; 1068f4a2e418SRicardo Labiaga 1069f4a2e418SRicardo Labiaga memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, 1070f4a2e418SRicardo Labiaga &calldir, sizeof(calldir)); 1071f4a2e418SRicardo Labiaga transport->tcp_copied += sizeof(calldir); 1072f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; 1073a246b010SChuck Lever } 1074a246b010SChuck Lever 1075a246b010SChuck Lever len = desc->count; 107651971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1077dd456471SChuck Lever struct xdr_skb_reader my_desc; 1078a246b010SChuck Lever 107951971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1080a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1081a246b010SChuck Lever my_desc.count = len; 108251971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10839d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1084a246b010SChuck Lever desc->count -= r; 1085a246b010SChuck Lever desc->offset += r; 1086a246b010SChuck Lever } else 108751971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10889d292316SChuck Lever desc, xdr_skb_read_bits); 1089a246b010SChuck Lever 1090a246b010SChuck Lever if (r > 0) { 109151971139SChuck Lever transport->tcp_copied += r; 109251971139SChuck Lever transport->tcp_offset += r; 1093a246b010SChuck Lever } 1094a246b010SChuck Lever if (r != len) { 1095a246b010SChuck Lever /* Error when copying to the receive buffer, 1096a246b010SChuck Lever * usually because we weren't able to allocate 1097a246b010SChuck Lever * additional buffer pages. All we can do now 1098e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1099a246b010SChuck Lever * will not receive any additional updates, 1100a246b010SChuck Lever * and time out. 1101a246b010SChuck Lever * Any remaining data from this record will 1102a246b010SChuck Lever * be discarded. 1103a246b010SChuck Lever */ 1104e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1105a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 110651971139SChuck Lever ntohl(transport->tcp_xid)); 110746121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 110846121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 110946121cf7SChuck Lever xprt, transport->tcp_copied, 111046121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 111144b98efdSRicardo Labiaga return; 1112a246b010SChuck Lever } 1113a246b010SChuck Lever 1114a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 111551971139SChuck Lever ntohl(transport->tcp_xid), r); 111646121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 111746121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 111846121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1119a246b010SChuck Lever 112051971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1121e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 112251971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1123e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1124e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1125a246b010SChuck Lever } 1126a246b010SChuck Lever 112744b98efdSRicardo Labiaga return; 112844b98efdSRicardo Labiaga } 112944b98efdSRicardo Labiaga 113044b98efdSRicardo Labiaga /* 113144b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 113244b98efdSRicardo Labiaga * tcp read code to read the data. 113344b98efdSRicardo Labiaga */ 113444b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 113544b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 113644b98efdSRicardo Labiaga { 113744b98efdSRicardo Labiaga struct sock_xprt *transport = 113844b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 113944b98efdSRicardo Labiaga struct rpc_rqst *req; 114044b98efdSRicardo Labiaga 114144b98efdSRicardo Labiaga dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); 114244b98efdSRicardo Labiaga 114344b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 114444b98efdSRicardo Labiaga spin_lock(&xprt->transport_lock); 114544b98efdSRicardo Labiaga req = xprt_lookup_rqst(xprt, transport->tcp_xid); 114644b98efdSRicardo Labiaga if (!req) { 114744b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 114844b98efdSRicardo Labiaga ntohl(transport->tcp_xid)); 114944b98efdSRicardo Labiaga spin_unlock(&xprt->transport_lock); 115044b98efdSRicardo Labiaga return -1; 115144b98efdSRicardo Labiaga } 115244b98efdSRicardo Labiaga 115344b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 115444b98efdSRicardo Labiaga 1155e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 115651971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 115744b98efdSRicardo Labiaga 11584a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 115944b98efdSRicardo Labiaga return 0; 116044b98efdSRicardo Labiaga } 116144b98efdSRicardo Labiaga 116244b98efdSRicardo Labiaga #if defined(CONFIG_NFS_V4_1) 116344b98efdSRicardo Labiaga /* 116444b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 116544b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 116644b98efdSRicardo Labiaga * queue. 116744b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 116844b98efdSRicardo Labiaga * connection and return -1. 116944b98efdSRicardo Labiaga */ 117044b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt, 117144b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 117244b98efdSRicardo Labiaga { 117344b98efdSRicardo Labiaga struct sock_xprt *transport = 117444b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 117544b98efdSRicardo Labiaga struct rpc_rqst *req; 117644b98efdSRicardo Labiaga 117744b98efdSRicardo Labiaga req = xprt_alloc_bc_request(xprt); 117844b98efdSRicardo Labiaga if (req == NULL) { 117944b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 118044b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 118144b98efdSRicardo Labiaga return -1; 118244b98efdSRicardo Labiaga } 118344b98efdSRicardo Labiaga 118444b98efdSRicardo Labiaga req->rq_xid = transport->tcp_xid; 118544b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 118644b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 118744b98efdSRicardo Labiaga 118844b98efdSRicardo Labiaga if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) { 118944b98efdSRicardo Labiaga struct svc_serv *bc_serv = xprt->bc_serv; 119044b98efdSRicardo Labiaga 119144b98efdSRicardo Labiaga /* 119244b98efdSRicardo Labiaga * Add callback request to callback list. The callback 119344b98efdSRicardo Labiaga * service sleeps on the sv_cb_waitq waiting for new 119444b98efdSRicardo Labiaga * requests. Wake it up after adding enqueing the 119544b98efdSRicardo Labiaga * request. 119644b98efdSRicardo Labiaga */ 119744b98efdSRicardo Labiaga dprintk("RPC: add callback request to list\n"); 119844b98efdSRicardo Labiaga spin_lock(&bc_serv->sv_cb_lock); 119944b98efdSRicardo Labiaga list_add(&req->rq_bc_list, &bc_serv->sv_cb_list); 120044b98efdSRicardo Labiaga spin_unlock(&bc_serv->sv_cb_lock); 120144b98efdSRicardo Labiaga wake_up(&bc_serv->sv_cb_waitq); 120244b98efdSRicardo Labiaga } 120344b98efdSRicardo Labiaga 120444b98efdSRicardo Labiaga req->rq_private_buf.len = transport->tcp_copied; 120544b98efdSRicardo Labiaga 120644b98efdSRicardo Labiaga return 0; 120744b98efdSRicardo Labiaga } 120844b98efdSRicardo Labiaga 120944b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 121044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 121144b98efdSRicardo Labiaga { 121244b98efdSRicardo Labiaga struct sock_xprt *transport = 121344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 121444b98efdSRicardo Labiaga 121544b98efdSRicardo Labiaga return (transport->tcp_flags & TCP_RPC_REPLY) ? 121644b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 121744b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 121844b98efdSRicardo Labiaga } 121944b98efdSRicardo Labiaga #else 122044b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 122144b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 122244b98efdSRicardo Labiaga { 122344b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 122444b98efdSRicardo Labiaga } 122544b98efdSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */ 122644b98efdSRicardo Labiaga 122744b98efdSRicardo Labiaga /* 122844b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 122944b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 123044b98efdSRicardo Labiaga */ 123144b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 123244b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 123344b98efdSRicardo Labiaga { 123444b98efdSRicardo Labiaga struct sock_xprt *transport = 123544b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 123644b98efdSRicardo Labiaga 123744b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 123851971139SChuck Lever xs_tcp_check_fraghdr(transport); 123944b98efdSRicardo Labiaga else { 124044b98efdSRicardo Labiaga /* 124144b98efdSRicardo Labiaga * The transport_lock protects the request handling. 124244b98efdSRicardo Labiaga * There's no need to hold it to update the tcp_flags. 124344b98efdSRicardo Labiaga */ 124444b98efdSRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 124544b98efdSRicardo Labiaga } 1246a246b010SChuck Lever } 1247a246b010SChuck Lever 1248dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1249a246b010SChuck Lever { 1250a246b010SChuck Lever size_t len; 1251a246b010SChuck Lever 125251971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1253a246b010SChuck Lever if (len > desc->count) 1254a246b010SChuck Lever len = desc->count; 1255a246b010SChuck Lever desc->count -= len; 1256a246b010SChuck Lever desc->offset += len; 125751971139SChuck Lever transport->tcp_offset += len; 1258a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 125951971139SChuck Lever xs_tcp_check_fraghdr(transport); 1260a246b010SChuck Lever } 1261a246b010SChuck Lever 12629903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1263a246b010SChuck Lever { 1264a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 126551971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1266dd456471SChuck Lever struct xdr_skb_reader desc = { 1267a246b010SChuck Lever .skb = skb, 1268a246b010SChuck Lever .offset = offset, 1269a246b010SChuck Lever .count = len, 1270a246b010SChuck Lever }; 1271a246b010SChuck Lever 12729903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1273a246b010SChuck Lever do { 1274a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1275a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1276e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 12779903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1278a246b010SChuck Lever continue; 1279a246b010SChuck Lever } 1280a246b010SChuck Lever /* Read in the xid if necessary */ 1281e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 128251971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1283a246b010SChuck Lever continue; 1284a246b010SChuck Lever } 128518dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1286f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { 128718dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 128818dca02aSRicardo Labiaga continue; 128918dca02aSRicardo Labiaga } 1290a246b010SChuck Lever /* Read in the request data */ 1291e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 129244b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1293a246b010SChuck Lever continue; 1294a246b010SChuck Lever } 1295a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 129651971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1297a246b010SChuck Lever } while (desc.count); 12989903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1299a246b010SChuck Lever return len - desc.count; 1300a246b010SChuck Lever } 1301a246b010SChuck Lever 13029903cd1cSChuck Lever /** 13039903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 13049903cd1cSChuck Lever * @sk: socket with data to read 13059903cd1cSChuck Lever * @bytes: how much data to read 13069903cd1cSChuck Lever * 13079903cd1cSChuck Lever */ 13089903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1309a246b010SChuck Lever { 1310a246b010SChuck Lever struct rpc_xprt *xprt; 1311a246b010SChuck Lever read_descriptor_t rd_desc; 1312ff2d7db8STrond Myklebust int read; 1313a246b010SChuck Lever 13149903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 131546121cf7SChuck Lever 131646121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 13179903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1318a246b010SChuck Lever goto out; 1319a246b010SChuck Lever if (xprt->shutdown) 1320a246b010SChuck Lever goto out; 1321a246b010SChuck Lever 13229903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1323a246b010SChuck Lever rd_desc.arg.data = xprt; 1324ff2d7db8STrond Myklebust do { 1325a246b010SChuck Lever rd_desc.count = 65536; 1326ff2d7db8STrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1327ff2d7db8STrond Myklebust } while (read > 0); 1328a246b010SChuck Lever out: 1329a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1330a246b010SChuck Lever } 1331a246b010SChuck Lever 13327d1e8255STrond Myklebust /* 13337d1e8255STrond Myklebust * Do the equivalent of linger/linger2 handling for dealing with 13347d1e8255STrond Myklebust * broken servers that don't close the socket in a timely 13357d1e8255STrond Myklebust * fashion 13367d1e8255STrond Myklebust */ 13377d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, 13387d1e8255STrond Myklebust unsigned long timeout) 13397d1e8255STrond Myklebust { 13407d1e8255STrond Myklebust struct sock_xprt *transport; 13417d1e8255STrond Myklebust 13427d1e8255STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 13437d1e8255STrond Myklebust return; 13447d1e8255STrond Myklebust set_bit(XPRT_CONNECTION_ABORT, &xprt->state); 13457d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 13467d1e8255STrond Myklebust queue_delayed_work(rpciod_workqueue, &transport->connect_worker, 13477d1e8255STrond Myklebust timeout); 13487d1e8255STrond Myklebust } 13497d1e8255STrond Myklebust 13507d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) 13517d1e8255STrond Myklebust { 13527d1e8255STrond Myklebust struct sock_xprt *transport; 13537d1e8255STrond Myklebust 13547d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 13557d1e8255STrond Myklebust 13567d1e8255STrond Myklebust if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || 13577d1e8255STrond Myklebust !cancel_delayed_work(&transport->connect_worker)) 13587d1e8255STrond Myklebust return; 13597d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 13607d1e8255STrond Myklebust xprt_clear_connecting(xprt); 13617d1e8255STrond Myklebust } 13627d1e8255STrond Myklebust 13637d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 13647d1e8255STrond Myklebust { 13657d1e8255STrond Myklebust smp_mb__before_clear_bit(); 13667d1e8255STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 13677d1e8255STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 13687d1e8255STrond Myklebust smp_mb__after_clear_bit(); 13697d1e8255STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 13707d1e8255STrond Myklebust xprt_disconnect_done(xprt); 13717d1e8255STrond Myklebust } 13727d1e8255STrond Myklebust 13739903cd1cSChuck Lever /** 13749903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 13759903cd1cSChuck Lever * @sk: socket whose state has changed 13769903cd1cSChuck Lever * 13779903cd1cSChuck Lever */ 13789903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1379a246b010SChuck Lever { 1380a246b010SChuck Lever struct rpc_xprt *xprt; 1381a246b010SChuck Lever 1382a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1383a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1384a246b010SChuck Lever goto out; 13859903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1386a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 1387a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1388a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1389a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 1390a246b010SChuck Lever 1391a246b010SChuck Lever switch (sk->sk_state) { 1392a246b010SChuck Lever case TCP_ESTABLISHED: 13934a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 1394a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 139551971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 139651971139SChuck Lever struct sock_xprt, xprt); 139751971139SChuck Lever 1398a246b010SChuck Lever /* Reset TCP record info */ 139951971139SChuck Lever transport->tcp_offset = 0; 140051971139SChuck Lever transport->tcp_reclen = 0; 140151971139SChuck Lever transport->tcp_copied = 0; 1402e136d092SChuck Lever transport->tcp_flags = 1403e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 140451971139SChuck Lever 14052a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1406a246b010SChuck Lever } 14074a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1408a246b010SChuck Lever break; 14093b948ae5STrond Myklebust case TCP_FIN_WAIT1: 14103b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 14117c1d71cfSTrond Myklebust xprt->connect_cookie++; 1412663b8858STrond Myklebust xprt->reestablish_timeout = 0; 14133b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 14143b948ae5STrond Myklebust smp_mb__before_clear_bit(); 14153b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1416ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 14173b948ae5STrond Myklebust smp_mb__after_clear_bit(); 141825fe6142STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); 1419a246b010SChuck Lever break; 1420632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 14213b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 142266af1e55STrond Myklebust xprt_force_disconnect(xprt); 1423663b8858STrond Myklebust case TCP_SYN_SENT: 14247c1d71cfSTrond Myklebust xprt->connect_cookie++; 1425663b8858STrond Myklebust case TCP_CLOSING: 1426663b8858STrond Myklebust /* 1427663b8858STrond Myklebust * If the server closed down the connection, make sure that 1428663b8858STrond Myklebust * we back off before reconnecting 1429663b8858STrond Myklebust */ 1430663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1431663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 14323b948ae5STrond Myklebust break; 14333b948ae5STrond Myklebust case TCP_LAST_ACK: 1434670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 143525fe6142STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); 14363b948ae5STrond Myklebust smp_mb__before_clear_bit(); 14373b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 14383b948ae5STrond Myklebust smp_mb__after_clear_bit(); 14393b948ae5STrond Myklebust break; 14403b948ae5STrond Myklebust case TCP_CLOSE: 14417d1e8255STrond Myklebust xs_tcp_cancel_linger_timeout(xprt); 14427d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1443a246b010SChuck Lever } 1444a246b010SChuck Lever out: 1445a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1446a246b010SChuck Lever } 1447a246b010SChuck Lever 14489903cd1cSChuck Lever /** 1449482f32e6STrond Myklebust * xs_error_report - callback mainly for catching socket errors 14502a9e1cfaSTrond Myklebust * @sk: socket 14512a9e1cfaSTrond Myklebust */ 1452482f32e6STrond Myklebust static void xs_error_report(struct sock *sk) 14532a9e1cfaSTrond Myklebust { 14542a9e1cfaSTrond Myklebust struct rpc_xprt *xprt; 14552a9e1cfaSTrond Myklebust 14562a9e1cfaSTrond Myklebust read_lock(&sk->sk_callback_lock); 14572a9e1cfaSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 14582a9e1cfaSTrond Myklebust goto out; 14592a9e1cfaSTrond Myklebust dprintk("RPC: %s client %p...\n" 14602a9e1cfaSTrond Myklebust "RPC: error %d\n", 14612a9e1cfaSTrond Myklebust __func__, xprt, sk->sk_err); 1462482f32e6STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 14632a9e1cfaSTrond Myklebust out: 14642a9e1cfaSTrond Myklebust read_unlock(&sk->sk_callback_lock); 14652a9e1cfaSTrond Myklebust } 14662a9e1cfaSTrond Myklebust 14671f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 14681f0fa154SIlpo Järvinen { 14691f0fa154SIlpo Järvinen struct socket *sock; 14701f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 14711f0fa154SIlpo Järvinen 14721f0fa154SIlpo Järvinen if (unlikely(!(sock = sk->sk_socket))) 14731f0fa154SIlpo Järvinen return; 14741f0fa154SIlpo Järvinen clear_bit(SOCK_NOSPACE, &sock->flags); 14751f0fa154SIlpo Järvinen 14761f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 14771f0fa154SIlpo Järvinen return; 14781f0fa154SIlpo Järvinen if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 14791f0fa154SIlpo Järvinen return; 14801f0fa154SIlpo Järvinen 14811f0fa154SIlpo Järvinen xprt_write_space(xprt); 14821f0fa154SIlpo Järvinen } 14831f0fa154SIlpo Järvinen 14842a9e1cfaSTrond Myklebust /** 1485c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1486c7b2cae8SChuck Lever * becomes available 14879903cd1cSChuck Lever * @sk: socket whose state has changed 14889903cd1cSChuck Lever * 1489a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1490a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1491c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1492a246b010SChuck Lever * with a bunch of small requests. 1493a246b010SChuck Lever */ 1494c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1495a246b010SChuck Lever { 1496a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1497c7b2cae8SChuck Lever 1498c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 14991f0fa154SIlpo Järvinen if (sock_writeable(sk)) 15001f0fa154SIlpo Järvinen xs_write_space(sk); 1501c7b2cae8SChuck Lever 1502c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1503c7b2cae8SChuck Lever } 1504c7b2cae8SChuck Lever 1505c7b2cae8SChuck Lever /** 1506c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1507c7b2cae8SChuck Lever * becomes available 1508c7b2cae8SChuck Lever * @sk: socket whose state has changed 1509c7b2cae8SChuck Lever * 1510c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1511c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1512c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1513c7b2cae8SChuck Lever * with a bunch of small requests. 1514c7b2cae8SChuck Lever */ 1515c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1516c7b2cae8SChuck Lever { 1517c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1518c7b2cae8SChuck Lever 1519c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 15201f0fa154SIlpo Järvinen if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 15211f0fa154SIlpo Järvinen xs_write_space(sk); 1522c7b2cae8SChuck Lever 1523a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1524a246b010SChuck Lever } 1525a246b010SChuck Lever 1526470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1527a246b010SChuck Lever { 1528ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1529ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1530a246b010SChuck Lever 15317c6e066eSChuck Lever if (transport->rcvsize) { 1532a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 15337c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1534a246b010SChuck Lever } 15357c6e066eSChuck Lever if (transport->sndsize) { 1536a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 15377c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1538a246b010SChuck Lever sk->sk_write_space(sk); 1539a246b010SChuck Lever } 1540a246b010SChuck Lever } 1541a246b010SChuck Lever 154243118c29SChuck Lever /** 1543470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 154443118c29SChuck Lever * @xprt: generic transport 1545470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1546470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 154743118c29SChuck Lever * 1548470056c2SChuck Lever * Set socket send and receive buffer size limits. 154943118c29SChuck Lever */ 1550470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 155143118c29SChuck Lever { 15527c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 15537c6e066eSChuck Lever 15547c6e066eSChuck Lever transport->sndsize = 0; 1555470056c2SChuck Lever if (sndsize) 15567c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 15577c6e066eSChuck Lever transport->rcvsize = 0; 1558470056c2SChuck Lever if (rcvsize) 15597c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1560470056c2SChuck Lever 1561470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 156243118c29SChuck Lever } 156343118c29SChuck Lever 156446c0ee8bSChuck Lever /** 156546c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 156646c0ee8bSChuck Lever * @task: task that timed out 156746c0ee8bSChuck Lever * 156846c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 156946c0ee8bSChuck Lever */ 157046c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 157146c0ee8bSChuck Lever { 157246c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 157346c0ee8bSChuck Lever } 157446c0ee8bSChuck Lever 1575b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1576b85d8806SChuck Lever { 1577b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1578b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1579b85d8806SChuck Lever return rand + xprt_min_resvport; 1580b85d8806SChuck Lever } 1581b85d8806SChuck Lever 158292200412SChuck Lever /** 158392200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 158492200412SChuck Lever * @xprt: generic transport 158592200412SChuck Lever * @port: new port number 158692200412SChuck Lever * 158792200412SChuck Lever */ 158892200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 158992200412SChuck Lever { 159095392c59SChuck Lever struct sockaddr *addr = xs_addr(xprt); 1591c4efcb1dSChuck Lever 159292200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1593c4efcb1dSChuck Lever 159420612005SChuck Lever switch (addr->sa_family) { 159520612005SChuck Lever case AF_INET: 159620612005SChuck Lever ((struct sockaddr_in *)addr)->sin_port = htons(port); 159720612005SChuck Lever break; 159820612005SChuck Lever case AF_INET6: 159920612005SChuck Lever ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); 160020612005SChuck Lever break; 160120612005SChuck Lever default: 160220612005SChuck Lever BUG(); 160320612005SChuck Lever } 160492200412SChuck Lever } 160592200412SChuck Lever 160667a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock) 160767a391d7STrond Myklebust { 160867a391d7STrond Myklebust unsigned short port = transport->port; 160967a391d7STrond Myklebust 161067a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 161167a391d7STrond Myklebust port = xs_get_random_port(); 161267a391d7STrond Myklebust return port; 161367a391d7STrond Myklebust } 161467a391d7STrond Myklebust 161567a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port) 161667a391d7STrond Myklebust { 161767a391d7STrond Myklebust if (transport->port != 0) 161867a391d7STrond Myklebust transport->port = 0; 161967a391d7STrond Myklebust if (!transport->xprt.resvport) 162067a391d7STrond Myklebust return 0; 162167a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 162267a391d7STrond Myklebust return xprt_max_resvport; 162367a391d7STrond Myklebust return --port; 162467a391d7STrond Myklebust } 162567a391d7STrond Myklebust 16267dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock) 1627a246b010SChuck Lever { 1628a246b010SChuck Lever struct sockaddr_in myaddr = { 1629a246b010SChuck Lever .sin_family = AF_INET, 1630a246b010SChuck Lever }; 1631d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 163267a391d7STrond Myklebust int err, nloop = 0; 163367a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 163467a391d7STrond Myklebust unsigned short last; 1635a246b010SChuck Lever 1636d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1637d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1638a246b010SChuck Lever do { 1639a246b010SChuck Lever myaddr.sin_port = htons(port); 1640e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1641a246b010SChuck Lever sizeof(myaddr)); 164267a391d7STrond Myklebust if (port == 0) 1643d3bc9a1dSFrank van Maarseveen break; 1644a246b010SChuck Lever if (err == 0) { 1645c8475461SChuck Lever transport->port = port; 1646d3bc9a1dSFrank van Maarseveen break; 1647a246b010SChuck Lever } 164867a391d7STrond Myklebust last = port; 164967a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 165067a391d7STrond Myklebust if (port > last) 165167a391d7STrond Myklebust nloop++; 165267a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 165321454aaaSHarvey Harrison dprintk("RPC: %s %pI4:%u: %s (%d)\n", 165421454aaaSHarvey Harrison __func__, &myaddr.sin_addr, 16557dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1656a246b010SChuck Lever return err; 1657a246b010SChuck Lever } 1658a246b010SChuck Lever 165990058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock) 166090058d37SChuck Lever { 166190058d37SChuck Lever struct sockaddr_in6 myaddr = { 166290058d37SChuck Lever .sin6_family = AF_INET6, 166390058d37SChuck Lever }; 166490058d37SChuck Lever struct sockaddr_in6 *sa; 166567a391d7STrond Myklebust int err, nloop = 0; 166667a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 166767a391d7STrond Myklebust unsigned short last; 166890058d37SChuck Lever 166990058d37SChuck Lever sa = (struct sockaddr_in6 *)&transport->addr; 167090058d37SChuck Lever myaddr.sin6_addr = sa->sin6_addr; 167190058d37SChuck Lever do { 167290058d37SChuck Lever myaddr.sin6_port = htons(port); 167390058d37SChuck Lever err = kernel_bind(sock, (struct sockaddr *) &myaddr, 167490058d37SChuck Lever sizeof(myaddr)); 167567a391d7STrond Myklebust if (port == 0) 167690058d37SChuck Lever break; 167790058d37SChuck Lever if (err == 0) { 167890058d37SChuck Lever transport->port = port; 167990058d37SChuck Lever break; 168090058d37SChuck Lever } 168167a391d7STrond Myklebust last = port; 168267a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 168367a391d7STrond Myklebust if (port > last) 168467a391d7STrond Myklebust nloop++; 168567a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 16865b095d98SHarvey Harrison dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", 1687fdb46ee7SHarvey Harrison &myaddr.sin6_addr, port, err ? "failed" : "ok", err); 1688a246b010SChuck Lever return err; 1689a246b010SChuck Lever } 1690a246b010SChuck Lever 1691ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1692ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1693ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1694ed07536eSPeter Zijlstra 16958945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1696ed07536eSPeter Zijlstra { 1697ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 16988945ee5eSChuck Lever 169902b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 17008945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 17018945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1702ed07536eSPeter Zijlstra } 17038945ee5eSChuck Lever 17048945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 17058945ee5eSChuck Lever { 17068945ee5eSChuck Lever struct sock *sk = sock->sk; 17078945ee5eSChuck Lever 1708f4921affSLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 17098945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 17108945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1711ed07536eSPeter Zijlstra } 1712ed07536eSPeter Zijlstra #else 17138945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 17148945ee5eSChuck Lever { 17158945ee5eSChuck Lever } 17168945ee5eSChuck Lever 17178945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1718ed07536eSPeter Zijlstra { 1719ed07536eSPeter Zijlstra } 1720ed07536eSPeter Zijlstra #endif 1721ed07536eSPeter Zijlstra 172216be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1723a246b010SChuck Lever { 172416be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1725edb267a6SChuck Lever 1726ee0ac0c2SChuck Lever if (!transport->inet) { 1727b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1728b0d93ad5SChuck Lever 1729b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1730b0d93ad5SChuck Lever 17312a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 17322a9e1cfaSTrond Myklebust 1733b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1734b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1735b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1736482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1737b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 1738b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 1739b0d93ad5SChuck Lever 1740b0d93ad5SChuck Lever xprt_set_connected(xprt); 1741b0d93ad5SChuck Lever 1742b0d93ad5SChuck Lever /* Reset to new socket */ 1743ee0ac0c2SChuck Lever transport->sock = sock; 1744ee0ac0c2SChuck Lever transport->inet = sk; 1745b0d93ad5SChuck Lever 1746b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1747b0d93ad5SChuck Lever } 1748470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 174916be2d20SChuck Lever } 175016be2d20SChuck Lever 1751a246b010SChuck Lever /** 17529c3d72deSChuck Lever * xs_udp_connect_worker4 - set up a UDP socket 1753a246b010SChuck Lever * @work: RPC transport to connect 1754a246b010SChuck Lever * 1755a246b010SChuck Lever * Invoked by a work queue tasklet. 1756a246b010SChuck Lever */ 17579c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work) 1758a246b010SChuck Lever { 1759a246b010SChuck Lever struct sock_xprt *transport = 1760a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1761a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1762a246b010SChuck Lever struct socket *sock = transport->sock; 1763a246b010SChuck Lever int err, status = -EIO; 1764a246b010SChuck Lever 176501d37c42STrond Myklebust if (xprt->shutdown) 17669903cd1cSChuck Lever goto out; 17679903cd1cSChuck Lever 1768a246b010SChuck Lever /* Start by resetting any existing state */ 1769fe315e76SChuck Lever xs_reset_transport(transport); 1770a246b010SChuck Lever 1771fe315e76SChuck Lever err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); 1772fe315e76SChuck Lever if (err < 0) { 17739903cd1cSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1774a246b010SChuck Lever goto out; 1775a246b010SChuck Lever } 17768945ee5eSChuck Lever xs_reclassify_socket4(sock); 1777a246b010SChuck Lever 17787dc753f0SChuck Lever if (xs_bind4(transport, sock)) { 17799903cd1cSChuck Lever sock_release(sock); 17809903cd1cSChuck Lever goto out; 1781a246b010SChuck Lever } 1782b0d93ad5SChuck Lever 1783b0d93ad5SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 1784b0d93ad5SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1785b0d93ad5SChuck Lever 178616be2d20SChuck Lever xs_udp_finish_connecting(xprt, sock); 1787a246b010SChuck Lever status = 0; 1788b0d93ad5SChuck Lever out: 1789b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 17907d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1791b0d93ad5SChuck Lever } 1792b0d93ad5SChuck Lever 179368e220bdSChuck Lever /** 179468e220bdSChuck Lever * xs_udp_connect_worker6 - set up a UDP socket 179568e220bdSChuck Lever * @work: RPC transport to connect 179668e220bdSChuck Lever * 179768e220bdSChuck Lever * Invoked by a work queue tasklet. 179868e220bdSChuck Lever */ 179968e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work) 180068e220bdSChuck Lever { 180168e220bdSChuck Lever struct sock_xprt *transport = 180268e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 180368e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 180468e220bdSChuck Lever struct socket *sock = transport->sock; 180568e220bdSChuck Lever int err, status = -EIO; 180668e220bdSChuck Lever 180701d37c42STrond Myklebust if (xprt->shutdown) 180868e220bdSChuck Lever goto out; 180968e220bdSChuck Lever 181068e220bdSChuck Lever /* Start by resetting any existing state */ 1811fe315e76SChuck Lever xs_reset_transport(transport); 181268e220bdSChuck Lever 1813fe315e76SChuck Lever err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); 1814fe315e76SChuck Lever if (err < 0) { 181568e220bdSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 181668e220bdSChuck Lever goto out; 181768e220bdSChuck Lever } 18188945ee5eSChuck Lever xs_reclassify_socket6(sock); 181968e220bdSChuck Lever 182068e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 182168e220bdSChuck Lever sock_release(sock); 182268e220bdSChuck Lever goto out; 182368e220bdSChuck Lever } 182468e220bdSChuck Lever 182568e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 182668e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 182768e220bdSChuck Lever 182868e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 1829a246b010SChuck Lever status = 0; 1830b0d93ad5SChuck Lever out: 1831b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 18327d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1833b0d93ad5SChuck Lever } 1834b0d93ad5SChuck Lever 18353167e12cSChuck Lever /* 18363167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 18373167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 18383167e12cSChuck Lever */ 183940d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 18403167e12cSChuck Lever { 18413167e12cSChuck Lever int result; 18423167e12cSChuck Lever struct sockaddr any; 18433167e12cSChuck Lever 18443167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 18453167e12cSChuck Lever 18463167e12cSChuck Lever /* 18473167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 18483167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 18493167e12cSChuck Lever */ 18503167e12cSChuck Lever memset(&any, 0, sizeof(any)); 18513167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1852ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 18537d1e8255STrond Myklebust if (!result) 18547d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 18557d1e8255STrond Myklebust else 18563167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 18573167e12cSChuck Lever result); 18583167e12cSChuck Lever } 18593167e12cSChuck Lever 186040d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 186140d2549dSTrond Myklebust { 186240d2549dSTrond Myklebust unsigned int state = transport->inet->sk_state; 186340d2549dSTrond Myklebust 186440d2549dSTrond Myklebust if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) 186540d2549dSTrond Myklebust return; 186640d2549dSTrond Myklebust if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) 186740d2549dSTrond Myklebust return; 186840d2549dSTrond Myklebust xs_abort_connection(xprt, transport); 186940d2549dSTrond Myklebust } 187040d2549dSTrond Myklebust 187116be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1872b0d93ad5SChuck Lever { 187316be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1874edb267a6SChuck Lever 1875ee0ac0c2SChuck Lever if (!transport->inet) { 1876b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1877b0d93ad5SChuck Lever 1878b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1879b0d93ad5SChuck Lever 18802a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 18812a9e1cfaSTrond Myklebust 1882b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1883b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1884b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1885b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1886482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1887b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 18883167e12cSChuck Lever 18893167e12cSChuck Lever /* socket options */ 18903167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 18913167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 18923167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 18933167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1894b0d93ad5SChuck Lever 1895b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1896b0d93ad5SChuck Lever 1897b0d93ad5SChuck Lever /* Reset to new socket */ 1898ee0ac0c2SChuck Lever transport->sock = sock; 1899ee0ac0c2SChuck Lever transport->inet = sk; 1900b0d93ad5SChuck Lever 1901b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1902b0d93ad5SChuck Lever } 1903b0d93ad5SChuck Lever 190401d37c42STrond Myklebust if (!xprt_bound(xprt)) 190501d37c42STrond Myklebust return -ENOTCONN; 190601d37c42STrond Myklebust 1907b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1908262ca07dSChuck Lever xprt->stat.connect_count++; 1909262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 191095392c59SChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 191116be2d20SChuck Lever } 191216be2d20SChuck Lever 191316be2d20SChuck Lever /** 1914b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 1915b61d59ffSTrond Myklebust * @xprt: RPC transport to connect 1916b61d59ffSTrond Myklebust * @transport: socket transport to connect 1917b61d59ffSTrond Myklebust * @create_sock: function to create a socket of the correct type 191816be2d20SChuck Lever * 191916be2d20SChuck Lever * Invoked by a work queue tasklet. 192016be2d20SChuck Lever */ 1921b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt, 1922b61d59ffSTrond Myklebust struct sock_xprt *transport, 1923b61d59ffSTrond Myklebust struct socket *(*create_sock)(struct rpc_xprt *, 1924b61d59ffSTrond Myklebust struct sock_xprt *)) 192516be2d20SChuck Lever { 192616be2d20SChuck Lever struct socket *sock = transport->sock; 1927b61d59ffSTrond Myklebust int status = -EIO; 192816be2d20SChuck Lever 192901d37c42STrond Myklebust if (xprt->shutdown) 193016be2d20SChuck Lever goto out; 193116be2d20SChuck Lever 193216be2d20SChuck Lever if (!sock) { 19337d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 1934b61d59ffSTrond Myklebust sock = create_sock(xprt, transport); 1935b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 1936b61d59ffSTrond Myklebust status = PTR_ERR(sock); 193716be2d20SChuck Lever goto out; 193816be2d20SChuck Lever } 19397d1e8255STrond Myklebust } else { 19407d1e8255STrond Myklebust int abort_and_exit; 19417d1e8255STrond Myklebust 19427d1e8255STrond Myklebust abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, 19437d1e8255STrond Myklebust &xprt->state); 194416be2d20SChuck Lever /* "close" the socket, preserving the local port */ 194540d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 194616be2d20SChuck Lever 19477d1e8255STrond Myklebust if (abort_and_exit) 19487d1e8255STrond Myklebust goto out_eagain; 19497d1e8255STrond Myklebust } 19507d1e8255STrond Myklebust 195116be2d20SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 195216be2d20SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 195316be2d20SChuck Lever 195416be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 1955a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 195646121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 195746121cf7SChuck Lever sock->sk->sk_state); 1958a246b010SChuck Lever switch (status) { 1959f75e6745STrond Myklebust default: 1960f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 1961f75e6745STrond Myklebust __func__, status); 1962f75e6745STrond Myklebust case -EADDRNOTAVAIL: 1963f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 1964f75e6745STrond Myklebust * and retry 1965f75e6745STrond Myklebust */ 1966f75e6745STrond Myklebust set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 1967f75e6745STrond Myklebust xprt_force_disconnect(xprt); 196888b5ed73STrond Myklebust break; 19698a2cec29STrond Myklebust case -ECONNREFUSED: 19708a2cec29STrond Myklebust case -ECONNRESET: 19718a2cec29STrond Myklebust case -ENETUNREACH: 19728a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 19732a491991STrond Myklebust case 0: 1974a246b010SChuck Lever case -EINPROGRESS: 1975a246b010SChuck Lever case -EALREADY: 19767d1e8255STrond Myklebust xprt_clear_connecting(xprt); 19777d1e8255STrond Myklebust return; 19788a2cec29STrond Myklebust } 19797d1e8255STrond Myklebust out_eagain: 19802a491991STrond Myklebust status = -EAGAIN; 1981a246b010SChuck Lever out: 19822226feb6SChuck Lever xprt_clear_connecting(xprt); 19837d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1984a246b010SChuck Lever } 1985a246b010SChuck Lever 1986b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt, 1987b61d59ffSTrond Myklebust struct sock_xprt *transport) 1988b61d59ffSTrond Myklebust { 1989b61d59ffSTrond Myklebust struct socket *sock; 1990b61d59ffSTrond Myklebust int err; 1991b61d59ffSTrond Myklebust 1992b61d59ffSTrond Myklebust /* start from scratch */ 1993b61d59ffSTrond Myklebust err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); 1994b61d59ffSTrond Myklebust if (err < 0) { 1995b61d59ffSTrond Myklebust dprintk("RPC: can't create TCP transport socket (%d).\n", 1996b61d59ffSTrond Myklebust -err); 1997b61d59ffSTrond Myklebust goto out_err; 1998b61d59ffSTrond Myklebust } 1999b61d59ffSTrond Myklebust xs_reclassify_socket4(sock); 2000b61d59ffSTrond Myklebust 2001b61d59ffSTrond Myklebust if (xs_bind4(transport, sock) < 0) { 2002b61d59ffSTrond Myklebust sock_release(sock); 2003b61d59ffSTrond Myklebust goto out_err; 2004b61d59ffSTrond Myklebust } 2005b61d59ffSTrond Myklebust return sock; 2006b61d59ffSTrond Myklebust out_err: 2007b61d59ffSTrond Myklebust return ERR_PTR(-EIO); 2008b61d59ffSTrond Myklebust } 2009b61d59ffSTrond Myklebust 2010b61d59ffSTrond Myklebust /** 2011a246b010SChuck Lever * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint 2012a246b010SChuck Lever * @work: RPC transport to connect 2013a246b010SChuck Lever * 2014a246b010SChuck Lever * Invoked by a work queue tasklet. 2015a246b010SChuck Lever */ 2016a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work) 2017a246b010SChuck Lever { 2018a246b010SChuck Lever struct sock_xprt *transport = 2019a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2020a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2021a246b010SChuck Lever 2022b61d59ffSTrond Myklebust xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4); 2023b61d59ffSTrond Myklebust } 2024a246b010SChuck Lever 2025b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt, 2026b61d59ffSTrond Myklebust struct sock_xprt *transport) 2027b61d59ffSTrond Myklebust { 2028b61d59ffSTrond Myklebust struct socket *sock; 2029b61d59ffSTrond Myklebust int err; 2030b61d59ffSTrond Myklebust 2031a246b010SChuck Lever /* start from scratch */ 2032b61d59ffSTrond Myklebust err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock); 2033b61d59ffSTrond Myklebust if (err < 0) { 2034b61d59ffSTrond Myklebust dprintk("RPC: can't create TCP transport socket (%d).\n", 2035b61d59ffSTrond Myklebust -err); 2036b61d59ffSTrond Myklebust goto out_err; 20379903cd1cSChuck Lever } 2038b61d59ffSTrond Myklebust xs_reclassify_socket6(sock); 20399903cd1cSChuck Lever 2040b61d59ffSTrond Myklebust if (xs_bind6(transport, sock) < 0) { 2041a246b010SChuck Lever sock_release(sock); 2042b61d59ffSTrond Myklebust goto out_err; 2043a246b010SChuck Lever } 2044b61d59ffSTrond Myklebust return sock; 2045b61d59ffSTrond Myklebust out_err: 2046b61d59ffSTrond Myklebust return ERR_PTR(-EIO); 2047a246b010SChuck Lever } 2048a246b010SChuck Lever 2049a246b010SChuck Lever /** 205068e220bdSChuck Lever * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint 205168e220bdSChuck Lever * @work: RPC transport to connect 205268e220bdSChuck Lever * 205368e220bdSChuck Lever * Invoked by a work queue tasklet. 205468e220bdSChuck Lever */ 205568e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work) 205668e220bdSChuck Lever { 205768e220bdSChuck Lever struct sock_xprt *transport = 205868e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 205968e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 206068e220bdSChuck Lever 2061b61d59ffSTrond Myklebust xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6); 206268e220bdSChuck Lever } 206368e220bdSChuck Lever 206468e220bdSChuck Lever /** 2065a246b010SChuck Lever * xs_connect - connect a socket to a remote endpoint 2066a246b010SChuck Lever * @task: address of RPC task that manages state of connect request 2067a246b010SChuck Lever * 2068a246b010SChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 206903bf4b70SChuck Lever * 207003bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 207103bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 207203bf4b70SChuck Lever * socket on a privileged port. 207303bf4b70SChuck Lever * 207403bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 207503bf4b70SChuck Lever * retry floods (hard mounts). 2076a246b010SChuck Lever */ 2077a246b010SChuck Lever static void xs_connect(struct rpc_task *task) 2078a246b010SChuck Lever { 2079a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 2080ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2081a246b010SChuck Lever 2082b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 2083b0d93ad5SChuck Lever return; 2084b0d93ad5SChuck Lever 2085ee0ac0c2SChuck Lever if (transport->sock != NULL) { 208646121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 208746121cf7SChuck Lever "seconds\n", 208803bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 2089c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2090c1384c9cSTrond Myklebust &transport->connect_worker, 209103bf4b70SChuck Lever xprt->reestablish_timeout); 209203bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 209303bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 209403bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 20959903cd1cSChuck Lever } else { 20969903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2097c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2098c1384c9cSTrond Myklebust &transport->connect_worker, 0); 2099a246b010SChuck Lever } 2100a246b010SChuck Lever } 2101a246b010SChuck Lever 2102e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task) 2103e06799f9STrond Myklebust { 2104e06799f9STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 2105e06799f9STrond Myklebust 2106e06799f9STrond Myklebust /* Exit if we need to wait for socket shutdown to complete */ 2107e06799f9STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 2108e06799f9STrond Myklebust return; 2109e06799f9STrond Myklebust xs_connect(task); 2110e06799f9STrond Myklebust } 2111e06799f9STrond Myklebust 2112262ca07dSChuck Lever /** 2113262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2114262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2115262ca07dSChuck Lever * @seq: output file 2116262ca07dSChuck Lever * 2117262ca07dSChuck Lever */ 2118262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2119262ca07dSChuck Lever { 2120c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2121c8475461SChuck Lever 2122262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 2123c8475461SChuck Lever transport->port, 2124262ca07dSChuck Lever xprt->stat.bind_count, 2125262ca07dSChuck Lever xprt->stat.sends, 2126262ca07dSChuck Lever xprt->stat.recvs, 2127262ca07dSChuck Lever xprt->stat.bad_xids, 2128262ca07dSChuck Lever xprt->stat.req_u, 2129262ca07dSChuck Lever xprt->stat.bklog_u); 2130262ca07dSChuck Lever } 2131262ca07dSChuck Lever 2132262ca07dSChuck Lever /** 2133262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2134262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2135262ca07dSChuck Lever * @seq: output file 2136262ca07dSChuck Lever * 2137262ca07dSChuck Lever */ 2138262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2139262ca07dSChuck Lever { 2140c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2141262ca07dSChuck Lever long idle_time = 0; 2142262ca07dSChuck Lever 2143262ca07dSChuck Lever if (xprt_connected(xprt)) 2144262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2145262ca07dSChuck Lever 2146262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 2147c8475461SChuck Lever transport->port, 2148262ca07dSChuck Lever xprt->stat.bind_count, 2149262ca07dSChuck Lever xprt->stat.connect_count, 2150262ca07dSChuck Lever xprt->stat.connect_time, 2151262ca07dSChuck Lever idle_time, 2152262ca07dSChuck Lever xprt->stat.sends, 2153262ca07dSChuck Lever xprt->stat.recvs, 2154262ca07dSChuck Lever xprt->stat.bad_xids, 2155262ca07dSChuck Lever xprt->stat.req_u, 2156262ca07dSChuck Lever xprt->stat.bklog_u); 2157262ca07dSChuck Lever } 2158262ca07dSChuck Lever 2159262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 216043118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 216112a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 216249e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 216345160d62SChuck Lever .rpcbind = rpcb_getport_async, 216492200412SChuck Lever .set_port = xs_set_port, 21659903cd1cSChuck Lever .connect = xs_connect, 216602107148SChuck Lever .buf_alloc = rpc_malloc, 216702107148SChuck Lever .buf_free = rpc_free, 2168262965f5SChuck Lever .send_request = xs_udp_send_request, 2169fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 217046c0ee8bSChuck Lever .timer = xs_udp_timer, 2171a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2172262965f5SChuck Lever .close = xs_close, 2173262965f5SChuck Lever .destroy = xs_destroy, 2174262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2175262965f5SChuck Lever }; 2176262965f5SChuck Lever 2177262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 217812a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 2179e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 218045160d62SChuck Lever .rpcbind = rpcb_getport_async, 218192200412SChuck Lever .set_port = xs_set_port, 2182e06799f9STrond Myklebust .connect = xs_tcp_connect, 218302107148SChuck Lever .buf_alloc = rpc_malloc, 218402107148SChuck Lever .buf_free = rpc_free, 2185262965f5SChuck Lever .send_request = xs_tcp_send_request, 2186fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 21870d90ba1cSRicardo Labiaga #if defined(CONFIG_NFS_V4_1) 21880d90ba1cSRicardo Labiaga .release_request = bc_release_request, 21890d90ba1cSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */ 2190f75e6745STrond Myklebust .close = xs_tcp_close, 21919903cd1cSChuck Lever .destroy = xs_destroy, 2192262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2193a246b010SChuck Lever }; 2194a246b010SChuck Lever 21953c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2196bc25571eS\"Talpey, Thomas\ unsigned int slot_table_size) 2197c8541ecdSChuck Lever { 2198c8541ecdSChuck Lever struct rpc_xprt *xprt; 2199ffc2e518SChuck Lever struct sock_xprt *new; 2200c8541ecdSChuck Lever 220196802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2202c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2203c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2204c8541ecdSChuck Lever } 2205c8541ecdSChuck Lever 2206ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 2207ffc2e518SChuck Lever if (new == NULL) { 220846121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 220946121cf7SChuck Lever "rpc_xprt\n"); 2210c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2211c8541ecdSChuck Lever } 2212ffc2e518SChuck Lever xprt = &new->xprt; 2213c8541ecdSChuck Lever 2214c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 2215c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 2216c8541ecdSChuck Lever if (xprt->slot == NULL) { 2217c8541ecdSChuck Lever kfree(xprt); 221846121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 221946121cf7SChuck Lever "table\n"); 2220c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2221c8541ecdSChuck Lever } 2222c8541ecdSChuck Lever 222396802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 222496802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2225d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2226d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 2227c8541ecdSChuck Lever 2228c8541ecdSChuck Lever return xprt; 2229c8541ecdSChuck Lever } 2230c8541ecdSChuck Lever 22312881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 22322881ae74STrond Myklebust .to_initval = 5 * HZ, 22332881ae74STrond Myklebust .to_maxval = 30 * HZ, 22342881ae74STrond Myklebust .to_increment = 5 * HZ, 22352881ae74STrond Myklebust .to_retries = 5, 22362881ae74STrond Myklebust }; 22372881ae74STrond Myklebust 22389903cd1cSChuck Lever /** 22399903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 224096802a09SFrank van Maarseveen * @args: rpc transport creation arguments 22419903cd1cSChuck Lever * 22429903cd1cSChuck Lever */ 2243483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2244a246b010SChuck Lever { 22458f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2246c8541ecdSChuck Lever struct rpc_xprt *xprt; 2247c8475461SChuck Lever struct sock_xprt *transport; 2248a246b010SChuck Lever 224996802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 2250c8541ecdSChuck Lever if (IS_ERR(xprt)) 2251c8541ecdSChuck Lever return xprt; 2252c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2253a246b010SChuck Lever 2254ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2255808012fbSChuck Lever xprt->tsh_size = 0; 2256a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2257a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2258a246b010SChuck Lever 225903bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 226003bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 226103bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 226203bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2263a246b010SChuck Lever 2264262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2265a246b010SChuck Lever 2266ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2267a246b010SChuck Lever 22688f9d5b1aSChuck Lever switch (addr->sa_family) { 22698f9d5b1aSChuck Lever case AF_INET: 22708f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 22718f9d5b1aSChuck Lever xprt_set_bound(xprt); 22728f9d5b1aSChuck Lever 22738f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 22748f9d5b1aSChuck Lever xs_udp_connect_worker4); 2275b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 22768f9d5b1aSChuck Lever break; 22778f9d5b1aSChuck Lever case AF_INET6: 22788f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 22798f9d5b1aSChuck Lever xprt_set_bound(xprt); 22808f9d5b1aSChuck Lever 22818f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 22828f9d5b1aSChuck Lever xs_udp_connect_worker6); 2283b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 22848f9d5b1aSChuck Lever break; 22858f9d5b1aSChuck Lever default: 22868f9d5b1aSChuck Lever kfree(xprt); 22878f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 22888f9d5b1aSChuck Lever } 22898f9d5b1aSChuck Lever 2290edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 22917559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2292edb267a6SChuck Lever 2293bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2294c8541ecdSChuck Lever return xprt; 2295bc25571eS\"Talpey, Thomas\ 2296bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2297bc25571eS\"Talpey, Thomas\ kfree(xprt); 2298bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2299a246b010SChuck Lever } 2300a246b010SChuck Lever 23012881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 23022881ae74STrond Myklebust .to_initval = 60 * HZ, 23032881ae74STrond Myklebust .to_maxval = 60 * HZ, 23042881ae74STrond Myklebust .to_retries = 2, 23052881ae74STrond Myklebust }; 23062881ae74STrond Myklebust 23079903cd1cSChuck Lever /** 23089903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 230996802a09SFrank van Maarseveen * @args: rpc transport creation arguments 23109903cd1cSChuck Lever * 23119903cd1cSChuck Lever */ 2312483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2313a246b010SChuck Lever { 23148f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2315c8541ecdSChuck Lever struct rpc_xprt *xprt; 2316c8475461SChuck Lever struct sock_xprt *transport; 2317a246b010SChuck Lever 231896802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 2319c8541ecdSChuck Lever if (IS_ERR(xprt)) 2320c8541ecdSChuck Lever return xprt; 2321c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2322a246b010SChuck Lever 2323ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2324808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2325808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2326a246b010SChuck Lever 232703bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 232803bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 232903bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 233003bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2331a246b010SChuck Lever 2332262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2333ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2334a246b010SChuck Lever 23358f9d5b1aSChuck Lever switch (addr->sa_family) { 23368f9d5b1aSChuck Lever case AF_INET: 23378f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 23388f9d5b1aSChuck Lever xprt_set_bound(xprt); 23398f9d5b1aSChuck Lever 23408f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); 2341b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 23428f9d5b1aSChuck Lever break; 23438f9d5b1aSChuck Lever case AF_INET6: 23448f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 23458f9d5b1aSChuck Lever xprt_set_bound(xprt); 23468f9d5b1aSChuck Lever 23478f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); 2348b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 23498f9d5b1aSChuck Lever break; 23508f9d5b1aSChuck Lever default: 23518f9d5b1aSChuck Lever kfree(xprt); 23528f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 23538f9d5b1aSChuck Lever } 23548f9d5b1aSChuck Lever 2355edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 23567559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2357edb267a6SChuck Lever 2358bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2359c8541ecdSChuck Lever return xprt; 2360bc25571eS\"Talpey, Thomas\ 2361bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2362bc25571eS\"Talpey, Thomas\ kfree(xprt); 2363bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2364a246b010SChuck Lever } 2365282b32e1SChuck Lever 2366bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 2367bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 2368bc25571eS\"Talpey, Thomas\ .name = "udp", 2369bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 23704fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_UDP, 2371bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 2372bc25571eS\"Talpey, Thomas\ }; 2373bc25571eS\"Talpey, Thomas\ 2374bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 2375bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 2376bc25571eS\"Talpey, Thomas\ .name = "tcp", 2377bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 23784fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_TCP, 2379bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 2380bc25571eS\"Talpey, Thomas\ }; 2381bc25571eS\"Talpey, Thomas\ 2382282b32e1SChuck Lever /** 2383bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 2384282b32e1SChuck Lever * 2385282b32e1SChuck Lever */ 2386282b32e1SChuck Lever int init_socket_xprt(void) 2387282b32e1SChuck Lever { 2388fbf76683SChuck Lever #ifdef RPC_DEBUG 23892b1bec5fSEric W. Biederman if (!sunrpc_table_header) 23900b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 2391fbf76683SChuck Lever #endif 2392fbf76683SChuck Lever 2393bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 2394bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 2395bc25571eS\"Talpey, Thomas\ 2396282b32e1SChuck Lever return 0; 2397282b32e1SChuck Lever } 2398282b32e1SChuck Lever 2399282b32e1SChuck Lever /** 2400bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 2401282b32e1SChuck Lever * 2402282b32e1SChuck Lever */ 2403282b32e1SChuck Lever void cleanup_socket_xprt(void) 2404282b32e1SChuck Lever { 2405fbf76683SChuck Lever #ifdef RPC_DEBUG 2406fbf76683SChuck Lever if (sunrpc_table_header) { 2407fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 2408fbf76683SChuck Lever sunrpc_table_header = NULL; 2409fbf76683SChuck Lever } 2410fbf76683SChuck Lever #endif 2411bc25571eS\"Talpey, Thomas\ 2412bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 2413bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 2414282b32e1SChuck Lever } 2415*cbf11071STrond Myklebust 2416*cbf11071STrond Myklebust static int param_set_uint_minmax(const char *val, struct kernel_param *kp, 2417*cbf11071STrond Myklebust unsigned int min, unsigned int max) 2418*cbf11071STrond Myklebust { 2419*cbf11071STrond Myklebust unsigned long num; 2420*cbf11071STrond Myklebust int ret; 2421*cbf11071STrond Myklebust 2422*cbf11071STrond Myklebust if (!val) 2423*cbf11071STrond Myklebust return -EINVAL; 2424*cbf11071STrond Myklebust ret = strict_strtoul(val, 0, &num); 2425*cbf11071STrond Myklebust if (ret == -EINVAL || num < min || num > max) 2426*cbf11071STrond Myklebust return -EINVAL; 2427*cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 2428*cbf11071STrond Myklebust return 0; 2429*cbf11071STrond Myklebust } 2430*cbf11071STrond Myklebust 2431*cbf11071STrond Myklebust static int param_set_portnr(const char *val, struct kernel_param *kp) 2432*cbf11071STrond Myklebust { 2433*cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 2434*cbf11071STrond Myklebust RPC_MIN_RESVPORT, 2435*cbf11071STrond Myklebust RPC_MAX_RESVPORT); 2436*cbf11071STrond Myklebust } 2437*cbf11071STrond Myklebust 2438*cbf11071STrond Myklebust static int param_get_portnr(char *buffer, struct kernel_param *kp) 2439*cbf11071STrond Myklebust { 2440*cbf11071STrond Myklebust return param_get_uint(buffer, kp); 2441*cbf11071STrond Myklebust } 2442*cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 2443*cbf11071STrond Myklebust __param_check(name, p, unsigned int); 2444*cbf11071STrond Myklebust 2445*cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 2446*cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 2447*cbf11071STrond Myklebust 2448*cbf11071STrond Myklebust static int param_set_slot_table_size(const char *val, struct kernel_param *kp) 2449*cbf11071STrond Myklebust { 2450*cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 2451*cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 2452*cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 2453*cbf11071STrond Myklebust } 2454*cbf11071STrond Myklebust 2455*cbf11071STrond Myklebust static int param_get_slot_table_size(char *buffer, struct kernel_param *kp) 2456*cbf11071STrond Myklebust { 2457*cbf11071STrond Myklebust return param_get_uint(buffer, kp); 2458*cbf11071STrond Myklebust } 2459*cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 2460*cbf11071STrond Myklebust __param_check(name, p, unsigned int); 2461*cbf11071STrond Myklebust 2462*cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 2463*cbf11071STrond Myklebust slot_table_size, 0644); 2464*cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 2465*cbf11071STrond Myklebust slot_table_size, 0644); 2466*cbf11071STrond Myklebust 2467