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> 37a246b010SChuck Lever 38a246b010SChuck Lever #include <net/sock.h> 39a246b010SChuck Lever #include <net/checksum.h> 40a246b010SChuck Lever #include <net/udp.h> 41a246b010SChuck Lever #include <net/tcp.h> 42a246b010SChuck Lever 439903cd1cSChuck Lever /* 44c556b754SChuck Lever * xprtsock tunables 45c556b754SChuck Lever */ 46c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 47c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; 48c556b754SChuck Lever 49c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 50c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 51c556b754SChuck Lever 52c556b754SChuck Lever /* 53fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 54fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 55fbf76683SChuck Lever * directory become the union of all files registered there. 56fbf76683SChuck Lever * 57fbf76683SChuck Lever * We simply need to make sure that we don't collide with 58fbf76683SChuck Lever * someone else's file names! 59fbf76683SChuck Lever */ 60fbf76683SChuck Lever 61fbf76683SChuck Lever #ifdef RPC_DEBUG 62fbf76683SChuck Lever 63fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 64fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 65fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 66fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 67fbf76683SChuck Lever 68fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 69fbf76683SChuck Lever 70fbf76683SChuck Lever /* 71fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 72fbf76683SChuck Lever * socket buffers for existing UDP transports 73fbf76683SChuck Lever */ 74fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 75fbf76683SChuck Lever { 76fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_UDP, 77fbf76683SChuck Lever .procname = "udp_slot_table_entries", 78fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 79fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 80fbf76683SChuck Lever .mode = 0644, 81fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 82fbf76683SChuck Lever .strategy = &sysctl_intvec, 83fbf76683SChuck Lever .extra1 = &min_slot_table_size, 84fbf76683SChuck Lever .extra2 = &max_slot_table_size 85fbf76683SChuck Lever }, 86fbf76683SChuck Lever { 87fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_TCP, 88fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 89fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 90fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 91fbf76683SChuck Lever .mode = 0644, 92fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 93fbf76683SChuck Lever .strategy = &sysctl_intvec, 94fbf76683SChuck Lever .extra1 = &min_slot_table_size, 95fbf76683SChuck Lever .extra2 = &max_slot_table_size 96fbf76683SChuck Lever }, 97fbf76683SChuck Lever { 98fbf76683SChuck Lever .ctl_name = CTL_MIN_RESVPORT, 99fbf76683SChuck Lever .procname = "min_resvport", 100fbf76683SChuck Lever .data = &xprt_min_resvport, 101fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 102fbf76683SChuck Lever .mode = 0644, 103fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 104fbf76683SChuck Lever .strategy = &sysctl_intvec, 105fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 106fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 107fbf76683SChuck Lever }, 108fbf76683SChuck Lever { 109fbf76683SChuck Lever .ctl_name = CTL_MAX_RESVPORT, 110fbf76683SChuck Lever .procname = "max_resvport", 111fbf76683SChuck Lever .data = &xprt_max_resvport, 112fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 113fbf76683SChuck Lever .mode = 0644, 114fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 115fbf76683SChuck Lever .strategy = &sysctl_intvec, 116fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 117fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 118fbf76683SChuck Lever }, 119fbf76683SChuck Lever { 120fbf76683SChuck Lever .ctl_name = 0, 121fbf76683SChuck Lever }, 122fbf76683SChuck Lever }; 123fbf76683SChuck Lever 124fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 125fbf76683SChuck Lever { 126fbf76683SChuck Lever .ctl_name = CTL_SUNRPC, 127fbf76683SChuck Lever .procname = "sunrpc", 128fbf76683SChuck Lever .mode = 0555, 129fbf76683SChuck Lever .child = xs_tunables_table 130fbf76683SChuck Lever }, 131fbf76683SChuck Lever { 132fbf76683SChuck Lever .ctl_name = 0, 133fbf76683SChuck Lever }, 134fbf76683SChuck Lever }; 135fbf76683SChuck Lever 136fbf76683SChuck Lever #endif 137fbf76683SChuck Lever 138fbf76683SChuck Lever /* 13903bf4b70SChuck Lever * Time out for an RPC UDP socket connect. UDP socket connects are 14003bf4b70SChuck Lever * synchronous, but we set a timeout anyway in case of resource 14103bf4b70SChuck Lever * exhaustion on the local host. 14203bf4b70SChuck Lever */ 14303bf4b70SChuck Lever #define XS_UDP_CONN_TO (5U * HZ) 14403bf4b70SChuck Lever 14503bf4b70SChuck Lever /* 14603bf4b70SChuck Lever * Wait duration for an RPC TCP connection to be established. Solaris 14703bf4b70SChuck Lever * NFS over TCP uses 60 seconds, for example, which is in line with how 14803bf4b70SChuck Lever * long a server takes to reboot. 14903bf4b70SChuck Lever */ 15003bf4b70SChuck Lever #define XS_TCP_CONN_TO (60U * HZ) 15103bf4b70SChuck Lever 15203bf4b70SChuck Lever /* 15303bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 15403bf4b70SChuck Lever */ 15503bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 15603bf4b70SChuck Lever 15703bf4b70SChuck Lever /* 15803bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 15903bf4b70SChuck Lever * kind of resource problem on the local host. 16003bf4b70SChuck Lever */ 16103bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 16203bf4b70SChuck Lever 16303bf4b70SChuck Lever /* 16403bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 16503bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 16603bf4b70SChuck Lever * 16703bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 16803bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 16903bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17003bf4b70SChuck Lever * increase over time if the server is down or not responding. 17103bf4b70SChuck Lever */ 17203bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 17303bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 17403bf4b70SChuck Lever 17503bf4b70SChuck Lever /* 17603bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 17703bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 17803bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 17903bf4b70SChuck Lever */ 18003bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18103bf4b70SChuck Lever 182a246b010SChuck Lever #ifdef RPC_DEBUG 183a246b010SChuck Lever # undef RPC_DEBUG_DATA 1849903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 185a246b010SChuck Lever #endif 186a246b010SChuck Lever 187a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1889903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 189a246b010SChuck Lever { 190a246b010SChuck Lever u8 *buf = (u8 *) packet; 191a246b010SChuck Lever int j; 192a246b010SChuck Lever 193a246b010SChuck Lever dprintk("RPC: %s\n", msg); 194a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 195a246b010SChuck Lever if (!(j & 31)) { 196a246b010SChuck Lever if (j) 197a246b010SChuck Lever dprintk("\n"); 198a246b010SChuck Lever dprintk("0x%04x ", j); 199a246b010SChuck Lever } 200a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 201a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 202a246b010SChuck Lever } 203a246b010SChuck Lever dprintk("\n"); 204a246b010SChuck Lever } 205a246b010SChuck Lever #else 2069903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 207a246b010SChuck Lever { 208a246b010SChuck Lever /* NOP */ 209a246b010SChuck Lever } 210a246b010SChuck Lever #endif 211a246b010SChuck Lever 212ffc2e518SChuck Lever struct sock_xprt { 213ffc2e518SChuck Lever struct rpc_xprt xprt; 214ee0ac0c2SChuck Lever 215ee0ac0c2SChuck Lever /* 216ee0ac0c2SChuck Lever * Network layer 217ee0ac0c2SChuck Lever */ 218ee0ac0c2SChuck Lever struct socket * sock; 219ee0ac0c2SChuck Lever struct sock * inet; 22051971139SChuck Lever 22151971139SChuck Lever /* 22251971139SChuck Lever * State of TCP reply receive 22351971139SChuck Lever */ 22451971139SChuck Lever __be32 tcp_fraghdr, 22551971139SChuck Lever tcp_xid; 22651971139SChuck Lever 22751971139SChuck Lever u32 tcp_offset, 22851971139SChuck Lever tcp_reclen; 22951971139SChuck Lever 23051971139SChuck Lever unsigned long tcp_copied, 23151971139SChuck Lever tcp_flags; 232c8475461SChuck Lever 233c8475461SChuck Lever /* 234c8475461SChuck Lever * Connection of transports 235c8475461SChuck Lever */ 23634161db6STrond Myklebust struct delayed_work connect_worker; 237d3bc9a1dSFrank van Maarseveen struct sockaddr_storage addr; 238c8475461SChuck Lever unsigned short port; 2397c6e066eSChuck Lever 2407c6e066eSChuck Lever /* 2417c6e066eSChuck Lever * UDP socket buffer size parameters 2427c6e066eSChuck Lever */ 2437c6e066eSChuck Lever size_t rcvsize, 2447c6e066eSChuck Lever sndsize; 245314dfd79SChuck Lever 246314dfd79SChuck Lever /* 247314dfd79SChuck Lever * Saved socket callback addresses 248314dfd79SChuck Lever */ 249314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 250314dfd79SChuck Lever void (*old_state_change)(struct sock *); 251314dfd79SChuck Lever void (*old_write_space)(struct sock *); 2522a9e1cfaSTrond Myklebust void (*old_error_report)(struct sock *); 253ffc2e518SChuck Lever }; 254ffc2e518SChuck Lever 255e136d092SChuck Lever /* 256e136d092SChuck Lever * TCP receive state flags 257e136d092SChuck Lever */ 258e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 259e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 260e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 261e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 262e136d092SChuck Lever 26395392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 264edb267a6SChuck Lever { 26595392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 26695392c59SChuck Lever } 26795392c59SChuck Lever 26895392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 26995392c59SChuck Lever { 27095392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 27195392c59SChuck Lever } 27295392c59SChuck Lever 27395392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 27495392c59SChuck Lever { 27595392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 27695392c59SChuck Lever } 27795392c59SChuck Lever 278b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt, 279b454ae90SChuck Lever const char *protocol, 280b454ae90SChuck Lever const char *netid) 281edb267a6SChuck Lever { 28295392c59SChuck Lever struct sockaddr_in *addr = xs_addr_in(xprt); 283edb267a6SChuck Lever char *buf; 284edb267a6SChuck Lever 285edb267a6SChuck Lever buf = kzalloc(20, GFP_KERNEL); 286edb267a6SChuck Lever if (buf) { 287e0db4a78SDavid S. Miller snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr); 288edb267a6SChuck Lever } 289edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 290edb267a6SChuck Lever 291edb267a6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 292edb267a6SChuck Lever if (buf) { 293edb267a6SChuck Lever snprintf(buf, 8, "%u", 294edb267a6SChuck Lever ntohs(addr->sin_port)); 295edb267a6SChuck Lever } 296edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 297edb267a6SChuck Lever 298b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 299edb267a6SChuck Lever 300edb267a6SChuck Lever buf = kzalloc(48, GFP_KERNEL); 301edb267a6SChuck Lever if (buf) { 30221454aaaSHarvey Harrison snprintf(buf, 48, "addr=%pI4 port=%u proto=%s", 30321454aaaSHarvey Harrison &addr->sin_addr.s_addr, 304edb267a6SChuck Lever ntohs(addr->sin_port), 305b454ae90SChuck Lever protocol); 306edb267a6SChuck Lever } 307edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 308fbfe3cc6SChuck Lever 309fbfe3cc6SChuck Lever buf = kzalloc(10, GFP_KERNEL); 310fbfe3cc6SChuck Lever if (buf) { 311fbfe3cc6SChuck Lever snprintf(buf, 10, "%02x%02x%02x%02x", 312fbfe3cc6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 313fbfe3cc6SChuck Lever } 314fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 315fbfe3cc6SChuck Lever 316fbfe3cc6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 317fbfe3cc6SChuck Lever if (buf) { 318fbfe3cc6SChuck Lever snprintf(buf, 8, "%4hx", 319fbfe3cc6SChuck Lever ntohs(addr->sin_port)); 320fbfe3cc6SChuck Lever } 321fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 322756805e7SChuck Lever 323756805e7SChuck Lever buf = kzalloc(30, GFP_KERNEL); 324756805e7SChuck Lever if (buf) { 32521454aaaSHarvey Harrison snprintf(buf, 30, "%pI4.%u.%u", 32621454aaaSHarvey Harrison &addr->sin_addr.s_addr, 327756805e7SChuck Lever ntohs(addr->sin_port) >> 8, 328756805e7SChuck Lever ntohs(addr->sin_port) & 0xff); 329756805e7SChuck Lever } 330756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3314417c8c4S\"Talpey, Thomas\ 332b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 333edb267a6SChuck Lever } 334edb267a6SChuck Lever 335b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt, 336b454ae90SChuck Lever const char *protocol, 337b454ae90SChuck Lever const char *netid) 3384b6473fbSChuck Lever { 33995392c59SChuck Lever struct sockaddr_in6 *addr = xs_addr_in6(xprt); 3404b6473fbSChuck Lever char *buf; 3414b6473fbSChuck Lever 3424b6473fbSChuck Lever buf = kzalloc(40, GFP_KERNEL); 3434b6473fbSChuck Lever if (buf) { 3445b095d98SHarvey Harrison snprintf(buf, 40, "%pI6",&addr->sin6_addr); 3454b6473fbSChuck Lever } 3464b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 3474b6473fbSChuck Lever 3484b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3494b6473fbSChuck Lever if (buf) { 3504b6473fbSChuck Lever snprintf(buf, 8, "%u", 3514b6473fbSChuck Lever ntohs(addr->sin6_port)); 3524b6473fbSChuck Lever } 3534b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 3544b6473fbSChuck Lever 355b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 3564b6473fbSChuck Lever 3574b6473fbSChuck Lever buf = kzalloc(64, GFP_KERNEL); 3584b6473fbSChuck Lever if (buf) { 3595b095d98SHarvey Harrison snprintf(buf, 64, "addr=%pI6 port=%u proto=%s", 360fdb46ee7SHarvey Harrison &addr->sin6_addr, 3614b6473fbSChuck Lever ntohs(addr->sin6_port), 362b454ae90SChuck Lever protocol); 3634b6473fbSChuck Lever } 3644b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 3654b6473fbSChuck Lever 3664b6473fbSChuck Lever buf = kzalloc(36, GFP_KERNEL); 367b071195dSHarvey Harrison if (buf) 3684b7a4274SHarvey Harrison snprintf(buf, 36, "%pi6", &addr->sin6_addr); 369b071195dSHarvey Harrison 3704b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 3714b6473fbSChuck Lever 3724b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3734b6473fbSChuck Lever if (buf) { 3744b6473fbSChuck Lever snprintf(buf, 8, "%4hx", 3754b6473fbSChuck Lever ntohs(addr->sin6_port)); 3764b6473fbSChuck Lever } 3774b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 378756805e7SChuck Lever 379756805e7SChuck Lever buf = kzalloc(50, GFP_KERNEL); 380756805e7SChuck Lever if (buf) { 3815b095d98SHarvey Harrison snprintf(buf, 50, "%pI6.%u.%u", 382fdb46ee7SHarvey Harrison &addr->sin6_addr, 383756805e7SChuck Lever ntohs(addr->sin6_port) >> 8, 384756805e7SChuck Lever ntohs(addr->sin6_port) & 0xff); 385756805e7SChuck Lever } 386756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3874417c8c4S\"Talpey, Thomas\ 388b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 389edb267a6SChuck Lever } 390edb267a6SChuck Lever 391edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 392edb267a6SChuck Lever { 39333e01dc7SChuck Lever unsigned int i; 39433e01dc7SChuck Lever 39533e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 39633e01dc7SChuck Lever switch (i) { 39733e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 39833e01dc7SChuck Lever case RPC_DISPLAY_NETID: 39933e01dc7SChuck Lever continue; 40033e01dc7SChuck Lever default: 40133e01dc7SChuck Lever kfree(xprt->address_strings[i]); 40233e01dc7SChuck Lever } 403edb267a6SChuck Lever } 404edb267a6SChuck Lever 405b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 406b4b5cc85SChuck Lever 40724c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 408b4b5cc85SChuck Lever { 409b4b5cc85SChuck Lever struct msghdr msg = { 410b4b5cc85SChuck Lever .msg_name = addr, 411b4b5cc85SChuck Lever .msg_namelen = addrlen, 41224c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 41324c5684bSTrond Myklebust }; 41424c5684bSTrond Myklebust struct kvec iov = { 41524c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 41624c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 417b4b5cc85SChuck Lever }; 418b4b5cc85SChuck Lever 41924c5684bSTrond Myklebust if (iov.iov_len != 0) 420b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 421b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 422b4b5cc85SChuck Lever } 423b4b5cc85SChuck Lever 42424c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 425b4b5cc85SChuck Lever { 42624c5684bSTrond Myklebust struct page **ppage; 42724c5684bSTrond Myklebust unsigned int remainder; 42824c5684bSTrond Myklebust int err, sent = 0; 429b4b5cc85SChuck Lever 43024c5684bSTrond Myklebust remainder = xdr->page_len - base; 43124c5684bSTrond Myklebust base += xdr->page_base; 43224c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 43324c5684bSTrond Myklebust base &= ~PAGE_MASK; 43424c5684bSTrond Myklebust for(;;) { 43524c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 43624c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 43724c5684bSTrond Myklebust 43824c5684bSTrond Myklebust remainder -= len; 43924c5684bSTrond Myklebust if (remainder != 0 || more) 44024c5684bSTrond Myklebust flags |= MSG_MORE; 44124c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 44224c5684bSTrond Myklebust if (remainder == 0 || err != len) 44324c5684bSTrond Myklebust break; 44424c5684bSTrond Myklebust sent += err; 44524c5684bSTrond Myklebust ppage++; 44624c5684bSTrond Myklebust base = 0; 44724c5684bSTrond Myklebust } 44824c5684bSTrond Myklebust if (sent == 0) 44924c5684bSTrond Myklebust return err; 45024c5684bSTrond Myklebust if (err > 0) 45124c5684bSTrond Myklebust sent += err; 45224c5684bSTrond Myklebust return sent; 453b4b5cc85SChuck Lever } 454b4b5cc85SChuck Lever 4559903cd1cSChuck Lever /** 4569903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 4579903cd1cSChuck Lever * @sock: socket to send on 4589903cd1cSChuck Lever * @addr: UDP only -- address of destination 4599903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 4609903cd1cSChuck Lever * @xdr: buffer containing this request 4619903cd1cSChuck Lever * @base: starting position in the buffer 4629903cd1cSChuck Lever * 463a246b010SChuck Lever */ 46424c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 465a246b010SChuck Lever { 46624c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 46724c5684bSTrond Myklebust int err, sent = 0; 468a246b010SChuck Lever 469262965f5SChuck Lever if (unlikely(!sock)) 470fba91afbSTrond Myklebust return -ENOTSOCK; 471262965f5SChuck Lever 472262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 47324c5684bSTrond Myklebust if (base != 0) { 47424c5684bSTrond Myklebust addr = NULL; 47524c5684bSTrond Myklebust addrlen = 0; 47624c5684bSTrond Myklebust } 477262965f5SChuck Lever 47824c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 47924c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 48024c5684bSTrond Myklebust remainder -= len; 48124c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 48224c5684bSTrond Myklebust if (remainder == 0 || err != len) 483a246b010SChuck Lever goto out; 48424c5684bSTrond Myklebust sent += err; 485a246b010SChuck Lever base = 0; 486a246b010SChuck Lever } else 48724c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 488a246b010SChuck Lever 48924c5684bSTrond Myklebust if (base < xdr->page_len) { 49024c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 49124c5684bSTrond Myklebust remainder -= len; 49224c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 49324c5684bSTrond Myklebust if (remainder == 0 || err != len) 494a246b010SChuck Lever goto out; 49524c5684bSTrond Myklebust sent += err; 496a246b010SChuck Lever base = 0; 49724c5684bSTrond Myklebust } else 49824c5684bSTrond Myklebust base -= xdr->page_len; 49924c5684bSTrond Myklebust 50024c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 50124c5684bSTrond Myklebust return sent; 50224c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 503a246b010SChuck Lever out: 50424c5684bSTrond Myklebust if (sent == 0) 50524c5684bSTrond Myklebust return err; 50624c5684bSTrond Myklebust if (err > 0) 50724c5684bSTrond Myklebust sent += err; 50824c5684bSTrond Myklebust return sent; 509a246b010SChuck Lever } 510a246b010SChuck Lever 511b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 512b6ddf64fSTrond Myklebust { 513b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 514b6ddf64fSTrond Myklebust 515b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 516b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 517b6ddf64fSTrond Myklebust } 518b6ddf64fSTrond Myklebust 5199903cd1cSChuck Lever /** 520262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 521262965f5SChuck Lever * @task: task to put to sleep 5229903cd1cSChuck Lever * 523a246b010SChuck Lever */ 524*5e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 525a246b010SChuck Lever { 526262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 527262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 528ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 529*5e3771ceSTrond Myklebust int ret = 0; 530a246b010SChuck Lever 53146121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 532262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 533262965f5SChuck Lever req->rq_slen); 534a246b010SChuck Lever 535262965f5SChuck Lever /* Protect against races with write_space */ 536262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 537a246b010SChuck Lever 538262965f5SChuck Lever /* Don't race with disconnect */ 539b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 540b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 541*5e3771ceSTrond Myklebust ret = -EAGAIN; 542b6ddf64fSTrond Myklebust /* 543b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 544b6ddf64fSTrond Myklebust * window size 545b6ddf64fSTrond Myklebust */ 546b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 547b6ddf64fSTrond Myklebust transport->inet->sk_write_pending++; 548b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 549b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 550b6ddf64fSTrond Myklebust } 551b6ddf64fSTrond Myklebust } else { 552b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 553*5e3771ceSTrond Myklebust ret = -ENOTCONN; 554b6ddf64fSTrond Myklebust } 555a246b010SChuck Lever 556262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 557*5e3771ceSTrond Myklebust return ret; 558a246b010SChuck Lever } 559a246b010SChuck Lever 5609903cd1cSChuck Lever /** 561262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5629903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5639903cd1cSChuck Lever * 5649903cd1cSChuck Lever * Return values: 5659903cd1cSChuck Lever * 0: The request has been sent 5669903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5679903cd1cSChuck Lever * complete the request 568262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 5699903cd1cSChuck Lever * other: Some other error occured, the request was not sent 5709903cd1cSChuck Lever */ 571262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 572a246b010SChuck Lever { 573a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 574a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 575ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 576262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 577262965f5SChuck Lever int status; 578262965f5SChuck Lever 579262965f5SChuck Lever xs_pktdump("packet data:", 580262965f5SChuck Lever req->rq_svec->iov_base, 581262965f5SChuck Lever req->rq_svec->iov_len); 582262965f5SChuck Lever 58301d37c42STrond Myklebust if (!xprt_bound(xprt)) 58401d37c42STrond Myklebust return -ENOTCONN; 585ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 58695392c59SChuck Lever xs_addr(xprt), 587ee0ac0c2SChuck Lever xprt->addrlen, xdr, 588ee0ac0c2SChuck Lever req->rq_bytes_sent); 589262965f5SChuck Lever 590262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 591262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 592262965f5SChuck Lever 5932199700fSTrond Myklebust if (status >= 0) { 5941321d8d9SChuck Lever task->tk_bytes_sent += status; 5952199700fSTrond Myklebust if (status >= req->rq_slen) 596262965f5SChuck Lever return 0; 597262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 598262965f5SChuck Lever status = -EAGAIN; 5992199700fSTrond Myklebust } 600c8485e4dSTrond Myklebust if (!transport->sock) 601c8485e4dSTrond Myklebust goto out; 602262965f5SChuck Lever 603262965f5SChuck Lever switch (status) { 604fba91afbSTrond Myklebust case -ENOTSOCK: 605fba91afbSTrond Myklebust status = -ENOTCONN; 606fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 607fba91afbSTrond Myklebust break; 608b6ddf64fSTrond Myklebust case -EAGAIN: 609*5e3771ceSTrond Myklebust status = xs_nospace(task); 610b6ddf64fSTrond Myklebust break; 611c8485e4dSTrond Myklebust default: 612c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 613c8485e4dSTrond Myklebust -status); 614262965f5SChuck Lever case -ENETUNREACH: 615262965f5SChuck Lever case -EPIPE: 616262965f5SChuck Lever case -ECONNREFUSED: 617262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 618262965f5SChuck Lever * prompts ECONNREFUSED. */ 619b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 620262965f5SChuck Lever } 621c8485e4dSTrond Myklebust out: 622262965f5SChuck Lever return status; 623262965f5SChuck Lever } 624262965f5SChuck Lever 625e06799f9STrond Myklebust /** 626e06799f9STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 627e06799f9STrond Myklebust * @xprt: transport 628e06799f9STrond Myklebust * 629e06799f9STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 630e06799f9STrond Myklebust * equivalent of shutdown(SHUT_WR); 631e06799f9STrond Myklebust */ 632e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 633e06799f9STrond Myklebust { 634e06799f9STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 635e06799f9STrond Myklebust struct socket *sock = transport->sock; 636e06799f9STrond Myklebust 637e06799f9STrond Myklebust if (sock != NULL) 638e06799f9STrond Myklebust kernel_sock_shutdown(sock, SHUT_WR); 639e06799f9STrond Myklebust } 640e06799f9STrond Myklebust 641808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 642808012fbSChuck Lever { 643808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 644808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 645808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 646808012fbSChuck Lever } 647808012fbSChuck Lever 648262965f5SChuck Lever /** 649262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 650262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 651262965f5SChuck Lever * 652262965f5SChuck Lever * Return values: 653262965f5SChuck Lever * 0: The request has been sent 654262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 655262965f5SChuck Lever * complete the request 656262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 657262965f5SChuck Lever * other: Some other error occured, the request was not sent 658262965f5SChuck Lever * 659262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 660262965f5SChuck Lever * if sendmsg is not able to make progress? 661262965f5SChuck Lever */ 662262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 663262965f5SChuck Lever { 664262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 665262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 666ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 667262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 668b595bb15SChuck Lever int status; 669a246b010SChuck Lever 670808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 671262965f5SChuck Lever 672262965f5SChuck Lever xs_pktdump("packet data:", 673262965f5SChuck Lever req->rq_svec->iov_base, 674262965f5SChuck Lever req->rq_svec->iov_len); 675a246b010SChuck Lever 676a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 677a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 678262965f5SChuck Lever * called sendmsg(). */ 679a246b010SChuck Lever while (1) { 680ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 681ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 682a246b010SChuck Lever 683262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 684262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 685262965f5SChuck Lever 686262965f5SChuck Lever if (unlikely(status < 0)) 687a246b010SChuck Lever break; 688a246b010SChuck Lever 689a246b010SChuck Lever /* If we've sent the entire packet, immediately 690a246b010SChuck Lever * reset the count of bytes sent. */ 691262965f5SChuck Lever req->rq_bytes_sent += status; 692ef759a2eSChuck Lever task->tk_bytes_sent += status; 693262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 694a246b010SChuck Lever req->rq_bytes_sent = 0; 695a246b010SChuck Lever return 0; 696a246b010SChuck Lever } 697262965f5SChuck Lever 69806b4b681STrond Myklebust if (status != 0) 69906b4b681STrond Myklebust continue; 700a246b010SChuck Lever status = -EAGAIN; 701a246b010SChuck Lever break; 702a246b010SChuck Lever } 703c8485e4dSTrond Myklebust if (!transport->sock) 704c8485e4dSTrond Myklebust goto out; 705a246b010SChuck Lever 706262965f5SChuck Lever switch (status) { 707fba91afbSTrond Myklebust case -ENOTSOCK: 708fba91afbSTrond Myklebust status = -ENOTCONN; 709fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 710fba91afbSTrond Myklebust break; 711262965f5SChuck Lever case -EAGAIN: 712*5e3771ceSTrond Myklebust status = xs_nospace(task); 713262965f5SChuck Lever break; 714c8485e4dSTrond Myklebust default: 715c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 716c8485e4dSTrond Myklebust -status); 717262965f5SChuck Lever case -ECONNRESET: 7182a9e1cfaSTrond Myklebust xs_tcp_shutdown(xprt); 7192a9e1cfaSTrond Myklebust case -ECONNREFUSED: 720262965f5SChuck Lever case -ENOTCONN: 721262965f5SChuck Lever case -EPIPE: 722b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 723a246b010SChuck Lever } 724c8485e4dSTrond Myklebust out: 725a246b010SChuck Lever return status; 726a246b010SChuck Lever } 727a246b010SChuck Lever 7289903cd1cSChuck Lever /** 729e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 730e0ab53deSTrond Myklebust * @xprt: transport 731e0ab53deSTrond Myklebust * @task: rpc task 732e0ab53deSTrond Myklebust * 733e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 734e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 735e0ab53deSTrond Myklebust * the server. 736e0ab53deSTrond Myklebust */ 737e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 738e0ab53deSTrond Myklebust { 739e0ab53deSTrond Myklebust struct rpc_rqst *req; 740e0ab53deSTrond Myklebust 741e0ab53deSTrond Myklebust if (task != xprt->snd_task) 742e0ab53deSTrond Myklebust return; 743e0ab53deSTrond Myklebust if (task == NULL) 744e0ab53deSTrond Myklebust goto out_release; 745e0ab53deSTrond Myklebust req = task->tk_rqstp; 746e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 747e0ab53deSTrond Myklebust goto out_release; 748e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 749e0ab53deSTrond Myklebust goto out_release; 750e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 751e0ab53deSTrond Myklebust out_release: 752e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 753e0ab53deSTrond Myklebust } 754e0ab53deSTrond Myklebust 7552a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7562a9e1cfaSTrond Myklebust { 7572a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7582a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7592a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7602a9e1cfaSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7612a9e1cfaSTrond Myklebust } 7622a9e1cfaSTrond Myklebust 7632a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7642a9e1cfaSTrond Myklebust { 7652a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7662a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7672a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7682a9e1cfaSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7692a9e1cfaSTrond Myklebust } 7702a9e1cfaSTrond Myklebust 771fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 772a246b010SChuck Lever { 773ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 774ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 775a246b010SChuck Lever 776fe315e76SChuck Lever if (sk == NULL) 777fe315e76SChuck Lever return; 7789903cd1cSChuck Lever 779a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 780ee0ac0c2SChuck Lever transport->inet = NULL; 781ee0ac0c2SChuck Lever transport->sock = NULL; 782a246b010SChuck Lever 783a246b010SChuck Lever sk->sk_user_data = NULL; 7842a9e1cfaSTrond Myklebust 7852a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 786a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 787a246b010SChuck Lever 788a246b010SChuck Lever sk->sk_no_check = 0; 789a246b010SChuck Lever 790a246b010SChuck Lever sock_release(sock); 791fe315e76SChuck Lever } 792fe315e76SChuck Lever 793fe315e76SChuck Lever /** 794fe315e76SChuck Lever * xs_close - close a socket 795fe315e76SChuck Lever * @xprt: transport 796fe315e76SChuck Lever * 797fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 798fe315e76SChuck Lever * on the server we want to save. 799fe315e76SChuck Lever */ 800fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 801fe315e76SChuck Lever { 802fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 803fe315e76SChuck Lever 804fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 805fe315e76SChuck Lever 806fe315e76SChuck Lever xs_reset_transport(transport); 807fe315e76SChuck Lever 808632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 809632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 8103b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 811632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 81262da3b24STrond Myklebust xprt_disconnect_done(xprt); 813a246b010SChuck Lever } 814a246b010SChuck Lever 8159903cd1cSChuck Lever /** 8169903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8179903cd1cSChuck Lever * @xprt: doomed transport 8189903cd1cSChuck Lever * 8199903cd1cSChuck Lever */ 8209903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 821a246b010SChuck Lever { 822c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 823c8475461SChuck Lever 8249903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8259903cd1cSChuck Lever 826c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 827a246b010SChuck Lever 8289903cd1cSChuck Lever xs_close(xprt); 829edb267a6SChuck Lever xs_free_peer_addresses(xprt); 830a246b010SChuck Lever kfree(xprt->slot); 831c8541ecdSChuck Lever kfree(xprt); 832bc25571eS\"Talpey, Thomas\ module_put(THIS_MODULE); 833a246b010SChuck Lever } 834a246b010SChuck Lever 8359903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 8369903cd1cSChuck Lever { 8379903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 8389903cd1cSChuck Lever } 8399903cd1cSChuck Lever 8409903cd1cSChuck Lever /** 8419903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 8429903cd1cSChuck Lever * @sk: socket with data to read 8439903cd1cSChuck Lever * @len: how much data to read 8449903cd1cSChuck Lever * 845a246b010SChuck Lever */ 8469903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 847a246b010SChuck Lever { 848a246b010SChuck Lever struct rpc_task *task; 849a246b010SChuck Lever struct rpc_xprt *xprt; 850a246b010SChuck Lever struct rpc_rqst *rovr; 851a246b010SChuck Lever struct sk_buff *skb; 852a246b010SChuck Lever int err, repsize, copied; 853d8ed029dSAlexey Dobriyan u32 _xid; 854d8ed029dSAlexey Dobriyan __be32 *xp; 855a246b010SChuck Lever 856a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 8579903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 8589903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 859a246b010SChuck Lever goto out; 860a246b010SChuck Lever 861a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 862a246b010SChuck Lever goto out; 863a246b010SChuck Lever 864a246b010SChuck Lever if (xprt->shutdown) 865a246b010SChuck Lever goto dropit; 866a246b010SChuck Lever 867a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 868a246b010SChuck Lever if (repsize < 4) { 8699903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 870a246b010SChuck Lever goto dropit; 871a246b010SChuck Lever } 872a246b010SChuck Lever 873a246b010SChuck Lever /* Copy the XID from the skb... */ 874a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 875a246b010SChuck Lever sizeof(_xid), &_xid); 876a246b010SChuck Lever if (xp == NULL) 877a246b010SChuck Lever goto dropit; 878a246b010SChuck Lever 879a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 8804a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 881a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 882a246b010SChuck Lever if (!rovr) 883a246b010SChuck Lever goto out_unlock; 884a246b010SChuck Lever task = rovr->rq_task; 885a246b010SChuck Lever 886a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 887a246b010SChuck Lever copied = repsize; 888a246b010SChuck Lever 889a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 8901781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 8911781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 892a246b010SChuck Lever goto out_unlock; 8931781f7f5SHerbert Xu } 8941781f7f5SHerbert Xu 8951781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 896a246b010SChuck Lever 897a246b010SChuck Lever /* Something worked... */ 898a246b010SChuck Lever dst_confirm(skb->dst); 899a246b010SChuck Lever 9001570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 9011570c1e4SChuck Lever xprt_update_rtt(task); 9021570c1e4SChuck Lever xprt_complete_rqst(task, copied); 903a246b010SChuck Lever 904a246b010SChuck Lever out_unlock: 9054a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 906a246b010SChuck Lever dropit: 907a246b010SChuck Lever skb_free_datagram(sk, skb); 908a246b010SChuck Lever out: 909a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 910a246b010SChuck Lever } 911a246b010SChuck Lever 912dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 913a246b010SChuck Lever { 91451971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 915a246b010SChuck Lever size_t len, used; 916a246b010SChuck Lever char *p; 917a246b010SChuck Lever 91851971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 91951971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 9209d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 92151971139SChuck Lever transport->tcp_offset += used; 922a246b010SChuck Lever if (used != len) 923a246b010SChuck Lever return; 924808012fbSChuck Lever 92551971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 92651971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 927e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 928a246b010SChuck Lever else 929e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 93051971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 931808012fbSChuck Lever 932e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 93351971139SChuck Lever transport->tcp_offset = 0; 934808012fbSChuck Lever 935a246b010SChuck Lever /* Sanity check of the record length */ 93651971139SChuck Lever if (unlikely(transport->tcp_reclen < 4)) { 9379903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 9383ebb067dSTrond Myklebust xprt_force_disconnect(xprt); 9399903cd1cSChuck Lever return; 940a246b010SChuck Lever } 941a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 94251971139SChuck Lever transport->tcp_reclen); 943a246b010SChuck Lever } 944a246b010SChuck Lever 94551971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 946a246b010SChuck Lever { 94751971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 948e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 94951971139SChuck Lever transport->tcp_offset = 0; 950e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 951e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 952e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 95351971139SChuck Lever transport->tcp_copied = 0; 954a246b010SChuck Lever } 955a246b010SChuck Lever } 956a246b010SChuck Lever } 957a246b010SChuck Lever 958dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 959a246b010SChuck Lever { 960a246b010SChuck Lever size_t len, used; 961a246b010SChuck Lever char *p; 962a246b010SChuck Lever 96351971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 964a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 96551971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 9669d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 96751971139SChuck Lever transport->tcp_offset += used; 968a246b010SChuck Lever if (used != len) 969a246b010SChuck Lever return; 970e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 971e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_DATA; 97251971139SChuck Lever transport->tcp_copied = 4; 973a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 97451971139SChuck Lever ntohl(transport->tcp_xid)); 97551971139SChuck Lever xs_tcp_check_fraghdr(transport); 976a246b010SChuck Lever } 977a246b010SChuck Lever 978dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 979a246b010SChuck Lever { 98051971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 981a246b010SChuck Lever struct rpc_rqst *req; 982a246b010SChuck Lever struct xdr_buf *rcvbuf; 983a246b010SChuck Lever size_t len; 984a246b010SChuck Lever ssize_t r; 985a246b010SChuck Lever 986a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 9874a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 98851971139SChuck Lever req = xprt_lookup_rqst(xprt, transport->tcp_xid); 989a246b010SChuck Lever if (!req) { 990e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 991a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 99251971139SChuck Lever ntohl(transport->tcp_xid)); 9934a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 994a246b010SChuck Lever return; 995a246b010SChuck Lever } 996a246b010SChuck Lever 997a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 998a246b010SChuck Lever len = desc->count; 99951971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1000dd456471SChuck Lever struct xdr_skb_reader my_desc; 1001a246b010SChuck Lever 100251971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1003a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1004a246b010SChuck Lever my_desc.count = len; 100551971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10069d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1007a246b010SChuck Lever desc->count -= r; 1008a246b010SChuck Lever desc->offset += r; 1009a246b010SChuck Lever } else 101051971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10119d292316SChuck Lever desc, xdr_skb_read_bits); 1012a246b010SChuck Lever 1013a246b010SChuck Lever if (r > 0) { 101451971139SChuck Lever transport->tcp_copied += r; 101551971139SChuck Lever transport->tcp_offset += r; 1016a246b010SChuck Lever } 1017a246b010SChuck Lever if (r != len) { 1018a246b010SChuck Lever /* Error when copying to the receive buffer, 1019a246b010SChuck Lever * usually because we weren't able to allocate 1020a246b010SChuck Lever * additional buffer pages. All we can do now 1021e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1022a246b010SChuck Lever * will not receive any additional updates, 1023a246b010SChuck Lever * and time out. 1024a246b010SChuck Lever * Any remaining data from this record will 1025a246b010SChuck Lever * be discarded. 1026a246b010SChuck Lever */ 1027e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1028a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 102951971139SChuck Lever ntohl(transport->tcp_xid)); 103046121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 103146121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 103246121cf7SChuck Lever xprt, transport->tcp_copied, 103346121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1034a246b010SChuck Lever goto out; 1035a246b010SChuck Lever } 1036a246b010SChuck Lever 1037a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 103851971139SChuck Lever ntohl(transport->tcp_xid), r); 103946121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 104046121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 104146121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1042a246b010SChuck Lever 104351971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1044e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 104551971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1046e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1047e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1048a246b010SChuck Lever } 1049a246b010SChuck Lever 1050a246b010SChuck Lever out: 1051e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 105251971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 10534a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 105451971139SChuck Lever xs_tcp_check_fraghdr(transport); 1055a246b010SChuck Lever } 1056a246b010SChuck Lever 1057dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1058a246b010SChuck Lever { 1059a246b010SChuck Lever size_t len; 1060a246b010SChuck Lever 106151971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1062a246b010SChuck Lever if (len > desc->count) 1063a246b010SChuck Lever len = desc->count; 1064a246b010SChuck Lever desc->count -= len; 1065a246b010SChuck Lever desc->offset += len; 106651971139SChuck Lever transport->tcp_offset += len; 1067a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 106851971139SChuck Lever xs_tcp_check_fraghdr(transport); 1069a246b010SChuck Lever } 1070a246b010SChuck Lever 10719903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1072a246b010SChuck Lever { 1073a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 107451971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1075dd456471SChuck Lever struct xdr_skb_reader desc = { 1076a246b010SChuck Lever .skb = skb, 1077a246b010SChuck Lever .offset = offset, 1078a246b010SChuck Lever .count = len, 1079a246b010SChuck Lever }; 1080a246b010SChuck Lever 10819903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1082a246b010SChuck Lever do { 1083a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1084a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1085e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 10869903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1087a246b010SChuck Lever continue; 1088a246b010SChuck Lever } 1089a246b010SChuck Lever /* Read in the xid if necessary */ 1090e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 109151971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1092a246b010SChuck Lever continue; 1093a246b010SChuck Lever } 1094a246b010SChuck Lever /* Read in the request data */ 1095e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 10969903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 1097a246b010SChuck Lever continue; 1098a246b010SChuck Lever } 1099a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 110051971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1101a246b010SChuck Lever } while (desc.count); 11029903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1103a246b010SChuck Lever return len - desc.count; 1104a246b010SChuck Lever } 1105a246b010SChuck Lever 11069903cd1cSChuck Lever /** 11079903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 11089903cd1cSChuck Lever * @sk: socket with data to read 11099903cd1cSChuck Lever * @bytes: how much data to read 11109903cd1cSChuck Lever * 11119903cd1cSChuck Lever */ 11129903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1113a246b010SChuck Lever { 1114a246b010SChuck Lever struct rpc_xprt *xprt; 1115a246b010SChuck Lever read_descriptor_t rd_desc; 1116ff2d7db8STrond Myklebust int read; 1117a246b010SChuck Lever 11189903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 111946121cf7SChuck Lever 112046121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 11219903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1122a246b010SChuck Lever goto out; 1123a246b010SChuck Lever if (xprt->shutdown) 1124a246b010SChuck Lever goto out; 1125a246b010SChuck Lever 11269903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1127a246b010SChuck Lever rd_desc.arg.data = xprt; 1128ff2d7db8STrond Myklebust do { 1129a246b010SChuck Lever rd_desc.count = 65536; 1130ff2d7db8STrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1131ff2d7db8STrond Myklebust } while (read > 0); 1132a246b010SChuck Lever out: 1133a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1134a246b010SChuck Lever } 1135a246b010SChuck Lever 11369903cd1cSChuck Lever /** 11379903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 11389903cd1cSChuck Lever * @sk: socket whose state has changed 11399903cd1cSChuck Lever * 11409903cd1cSChuck Lever */ 11419903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1142a246b010SChuck Lever { 1143a246b010SChuck Lever struct rpc_xprt *xprt; 1144a246b010SChuck Lever 1145a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1146a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1147a246b010SChuck Lever goto out; 11489903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1149a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 1150a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1151a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1152a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 1153a246b010SChuck Lever 1154a246b010SChuck Lever switch (sk->sk_state) { 1155a246b010SChuck Lever case TCP_ESTABLISHED: 11564a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 1157a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 115851971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 115951971139SChuck Lever struct sock_xprt, xprt); 116051971139SChuck Lever 1161a246b010SChuck Lever /* Reset TCP record info */ 116251971139SChuck Lever transport->tcp_offset = 0; 116351971139SChuck Lever transport->tcp_reclen = 0; 116451971139SChuck Lever transport->tcp_copied = 0; 1165e136d092SChuck Lever transport->tcp_flags = 1166e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 116751971139SChuck Lever 11682a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1169a246b010SChuck Lever } 11704a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1171a246b010SChuck Lever break; 11723b948ae5STrond Myklebust case TCP_FIN_WAIT1: 11733b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 11747c1d71cfSTrond Myklebust xprt->connect_cookie++; 1175663b8858STrond Myklebust xprt->reestablish_timeout = 0; 11763b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 11773b948ae5STrond Myklebust smp_mb__before_clear_bit(); 11783b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1179ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 11803b948ae5STrond Myklebust smp_mb__after_clear_bit(); 1181a246b010SChuck Lever break; 1182632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 11833b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 118466af1e55STrond Myklebust xprt_force_disconnect(xprt); 1185663b8858STrond Myklebust case TCP_SYN_SENT: 11867c1d71cfSTrond Myklebust xprt->connect_cookie++; 1187663b8858STrond Myklebust case TCP_CLOSING: 1188663b8858STrond Myklebust /* 1189663b8858STrond Myklebust * If the server closed down the connection, make sure that 1190663b8858STrond Myklebust * we back off before reconnecting 1191663b8858STrond Myklebust */ 1192663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1193663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 11943b948ae5STrond Myklebust break; 11953b948ae5STrond Myklebust case TCP_LAST_ACK: 1196670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 11973b948ae5STrond Myklebust smp_mb__before_clear_bit(); 11983b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 11993b948ae5STrond Myklebust smp_mb__after_clear_bit(); 12003b948ae5STrond Myklebust break; 12013b948ae5STrond Myklebust case TCP_CLOSE: 12023b948ae5STrond Myklebust smp_mb__before_clear_bit(); 1203ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 12043b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 12053b948ae5STrond Myklebust smp_mb__after_clear_bit(); 12063b948ae5STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 120762da3b24STrond Myklebust xprt_disconnect_done(xprt); 1208a246b010SChuck Lever } 1209a246b010SChuck Lever out: 1210a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1211a246b010SChuck Lever } 1212a246b010SChuck Lever 12139903cd1cSChuck Lever /** 1214482f32e6STrond Myklebust * xs_error_report - callback mainly for catching socket errors 12152a9e1cfaSTrond Myklebust * @sk: socket 12162a9e1cfaSTrond Myklebust */ 1217482f32e6STrond Myklebust static void xs_error_report(struct sock *sk) 12182a9e1cfaSTrond Myklebust { 12192a9e1cfaSTrond Myklebust struct rpc_xprt *xprt; 12202a9e1cfaSTrond Myklebust 12212a9e1cfaSTrond Myklebust read_lock(&sk->sk_callback_lock); 12222a9e1cfaSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12232a9e1cfaSTrond Myklebust goto out; 12242a9e1cfaSTrond Myklebust dprintk("RPC: %s client %p...\n" 12252a9e1cfaSTrond Myklebust "RPC: error %d\n", 12262a9e1cfaSTrond Myklebust __func__, xprt, sk->sk_err); 1227482f32e6STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 12282a9e1cfaSTrond Myklebust out: 12292a9e1cfaSTrond Myklebust read_unlock(&sk->sk_callback_lock); 12302a9e1cfaSTrond Myklebust } 12312a9e1cfaSTrond Myklebust 12322a9e1cfaSTrond Myklebust /** 1233c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1234c7b2cae8SChuck Lever * becomes available 12359903cd1cSChuck Lever * @sk: socket whose state has changed 12369903cd1cSChuck Lever * 1237a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1238a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1239c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1240a246b010SChuck Lever * with a bunch of small requests. 1241a246b010SChuck Lever */ 1242c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1243a246b010SChuck Lever { 1244a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1245c7b2cae8SChuck Lever 1246c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 1247c7b2cae8SChuck Lever if (sock_writeable(sk)) { 1248c7b2cae8SChuck Lever struct socket *sock; 1249c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1250c7b2cae8SChuck Lever 1251c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1252a246b010SChuck Lever goto out; 1253b6ddf64fSTrond Myklebust clear_bit(SOCK_NOSPACE, &sock->flags); 1254b6ddf64fSTrond Myklebust 1255c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1256c7b2cae8SChuck Lever goto out; 1257b6ddf64fSTrond Myklebust if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 1258a246b010SChuck Lever goto out; 1259a246b010SChuck Lever 1260c7b2cae8SChuck Lever xprt_write_space(xprt); 1261a246b010SChuck Lever } 1262a246b010SChuck Lever 1263c7b2cae8SChuck Lever out: 1264c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1265c7b2cae8SChuck Lever } 1266c7b2cae8SChuck Lever 1267c7b2cae8SChuck Lever /** 1268c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1269c7b2cae8SChuck Lever * becomes available 1270c7b2cae8SChuck Lever * @sk: socket whose state has changed 1271c7b2cae8SChuck Lever * 1272c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1273c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1274c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1275c7b2cae8SChuck Lever * with a bunch of small requests. 1276c7b2cae8SChuck Lever */ 1277c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1278c7b2cae8SChuck Lever { 1279c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1280c7b2cae8SChuck Lever 1281c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 1282c7b2cae8SChuck Lever if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 1283c7b2cae8SChuck Lever struct socket *sock; 1284c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1285c7b2cae8SChuck Lever 1286c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1287c7b2cae8SChuck Lever goto out; 1288b6ddf64fSTrond Myklebust clear_bit(SOCK_NOSPACE, &sock->flags); 1289b6ddf64fSTrond Myklebust 1290c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1291c7b2cae8SChuck Lever goto out; 1292b6ddf64fSTrond Myklebust if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 1293a246b010SChuck Lever goto out; 1294a246b010SChuck Lever 1295c7b2cae8SChuck Lever xprt_write_space(xprt); 1296c7b2cae8SChuck Lever } 1297c7b2cae8SChuck Lever 1298a246b010SChuck Lever out: 1299a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1300a246b010SChuck Lever } 1301a246b010SChuck Lever 1302470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1303a246b010SChuck Lever { 1304ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1305ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1306a246b010SChuck Lever 13077c6e066eSChuck Lever if (transport->rcvsize) { 1308a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 13097c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1310a246b010SChuck Lever } 13117c6e066eSChuck Lever if (transport->sndsize) { 1312a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 13137c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1314a246b010SChuck Lever sk->sk_write_space(sk); 1315a246b010SChuck Lever } 1316a246b010SChuck Lever } 1317a246b010SChuck Lever 131843118c29SChuck Lever /** 1319470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 132043118c29SChuck Lever * @xprt: generic transport 1321470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1322470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 132343118c29SChuck Lever * 1324470056c2SChuck Lever * Set socket send and receive buffer size limits. 132543118c29SChuck Lever */ 1326470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 132743118c29SChuck Lever { 13287c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 13297c6e066eSChuck Lever 13307c6e066eSChuck Lever transport->sndsize = 0; 1331470056c2SChuck Lever if (sndsize) 13327c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 13337c6e066eSChuck Lever transport->rcvsize = 0; 1334470056c2SChuck Lever if (rcvsize) 13357c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1336470056c2SChuck Lever 1337470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 133843118c29SChuck Lever } 133943118c29SChuck Lever 134046c0ee8bSChuck Lever /** 134146c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 134246c0ee8bSChuck Lever * @task: task that timed out 134346c0ee8bSChuck Lever * 134446c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 134546c0ee8bSChuck Lever */ 134646c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 134746c0ee8bSChuck Lever { 134846c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 134946c0ee8bSChuck Lever } 135046c0ee8bSChuck Lever 1351b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1352b85d8806SChuck Lever { 1353b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1354b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1355b85d8806SChuck Lever return rand + xprt_min_resvport; 1356b85d8806SChuck Lever } 1357b85d8806SChuck Lever 135892200412SChuck Lever /** 135992200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 136092200412SChuck Lever * @xprt: generic transport 136192200412SChuck Lever * @port: new port number 136292200412SChuck Lever * 136392200412SChuck Lever */ 136492200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 136592200412SChuck Lever { 136695392c59SChuck Lever struct sockaddr *addr = xs_addr(xprt); 1367c4efcb1dSChuck Lever 136892200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1369c4efcb1dSChuck Lever 137020612005SChuck Lever switch (addr->sa_family) { 137120612005SChuck Lever case AF_INET: 137220612005SChuck Lever ((struct sockaddr_in *)addr)->sin_port = htons(port); 137320612005SChuck Lever break; 137420612005SChuck Lever case AF_INET6: 137520612005SChuck Lever ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); 137620612005SChuck Lever break; 137720612005SChuck Lever default: 137820612005SChuck Lever BUG(); 137920612005SChuck Lever } 138092200412SChuck Lever } 138192200412SChuck Lever 138267a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock) 138367a391d7STrond Myklebust { 138467a391d7STrond Myklebust unsigned short port = transport->port; 138567a391d7STrond Myklebust 138667a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 138767a391d7STrond Myklebust port = xs_get_random_port(); 138867a391d7STrond Myklebust return port; 138967a391d7STrond Myklebust } 139067a391d7STrond Myklebust 139167a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port) 139267a391d7STrond Myklebust { 139367a391d7STrond Myklebust if (transport->port != 0) 139467a391d7STrond Myklebust transport->port = 0; 139567a391d7STrond Myklebust if (!transport->xprt.resvport) 139667a391d7STrond Myklebust return 0; 139767a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 139867a391d7STrond Myklebust return xprt_max_resvport; 139967a391d7STrond Myklebust return --port; 140067a391d7STrond Myklebust } 140167a391d7STrond Myklebust 14027dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock) 1403a246b010SChuck Lever { 1404a246b010SChuck Lever struct sockaddr_in myaddr = { 1405a246b010SChuck Lever .sin_family = AF_INET, 1406a246b010SChuck Lever }; 1407d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 140867a391d7STrond Myklebust int err, nloop = 0; 140967a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 141067a391d7STrond Myklebust unsigned short last; 1411a246b010SChuck Lever 1412d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1413d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1414a246b010SChuck Lever do { 1415a246b010SChuck Lever myaddr.sin_port = htons(port); 1416e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1417a246b010SChuck Lever sizeof(myaddr)); 141867a391d7STrond Myklebust if (port == 0) 1419d3bc9a1dSFrank van Maarseveen break; 1420a246b010SChuck Lever if (err == 0) { 1421c8475461SChuck Lever transport->port = port; 1422d3bc9a1dSFrank van Maarseveen break; 1423a246b010SChuck Lever } 142467a391d7STrond Myklebust last = port; 142567a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 142667a391d7STrond Myklebust if (port > last) 142767a391d7STrond Myklebust nloop++; 142867a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 142921454aaaSHarvey Harrison dprintk("RPC: %s %pI4:%u: %s (%d)\n", 143021454aaaSHarvey Harrison __func__, &myaddr.sin_addr, 14317dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1432a246b010SChuck Lever return err; 1433a246b010SChuck Lever } 1434a246b010SChuck Lever 143590058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock) 143690058d37SChuck Lever { 143790058d37SChuck Lever struct sockaddr_in6 myaddr = { 143890058d37SChuck Lever .sin6_family = AF_INET6, 143990058d37SChuck Lever }; 144090058d37SChuck Lever struct sockaddr_in6 *sa; 144167a391d7STrond Myklebust int err, nloop = 0; 144267a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 144367a391d7STrond Myklebust unsigned short last; 144490058d37SChuck Lever 144590058d37SChuck Lever sa = (struct sockaddr_in6 *)&transport->addr; 144690058d37SChuck Lever myaddr.sin6_addr = sa->sin6_addr; 144790058d37SChuck Lever do { 144890058d37SChuck Lever myaddr.sin6_port = htons(port); 144990058d37SChuck Lever err = kernel_bind(sock, (struct sockaddr *) &myaddr, 145090058d37SChuck Lever sizeof(myaddr)); 145167a391d7STrond Myklebust if (port == 0) 145290058d37SChuck Lever break; 145390058d37SChuck Lever if (err == 0) { 145490058d37SChuck Lever transport->port = port; 145590058d37SChuck Lever break; 145690058d37SChuck Lever } 145767a391d7STrond Myklebust last = port; 145867a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 145967a391d7STrond Myklebust if (port > last) 146067a391d7STrond Myklebust nloop++; 146167a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 14625b095d98SHarvey Harrison dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", 1463fdb46ee7SHarvey Harrison &myaddr.sin6_addr, port, err ? "failed" : "ok", err); 1464a246b010SChuck Lever return err; 1465a246b010SChuck Lever } 1466a246b010SChuck Lever 1467ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1468ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1469ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1470ed07536eSPeter Zijlstra 14718945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1472ed07536eSPeter Zijlstra { 1473ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 14748945ee5eSChuck Lever 147502b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 14768945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 14778945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1478ed07536eSPeter Zijlstra } 14798945ee5eSChuck Lever 14808945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 14818945ee5eSChuck Lever { 14828945ee5eSChuck Lever struct sock *sk = sock->sk; 14838945ee5eSChuck Lever 1484f4921affSLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 14858945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 14868945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1487ed07536eSPeter Zijlstra } 1488ed07536eSPeter Zijlstra #else 14898945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 14908945ee5eSChuck Lever { 14918945ee5eSChuck Lever } 14928945ee5eSChuck Lever 14938945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1494ed07536eSPeter Zijlstra { 1495ed07536eSPeter Zijlstra } 1496ed07536eSPeter Zijlstra #endif 1497ed07536eSPeter Zijlstra 149816be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1499a246b010SChuck Lever { 150016be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1501edb267a6SChuck Lever 1502ee0ac0c2SChuck Lever if (!transport->inet) { 1503b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1504b0d93ad5SChuck Lever 1505b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1506b0d93ad5SChuck Lever 15072a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 15082a9e1cfaSTrond Myklebust 1509b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1510b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1511b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1512482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1513b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 1514b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 1515b0d93ad5SChuck Lever 1516b0d93ad5SChuck Lever xprt_set_connected(xprt); 1517b0d93ad5SChuck Lever 1518b0d93ad5SChuck Lever /* Reset to new socket */ 1519ee0ac0c2SChuck Lever transport->sock = sock; 1520ee0ac0c2SChuck Lever transport->inet = sk; 1521b0d93ad5SChuck Lever 1522b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1523b0d93ad5SChuck Lever } 1524470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 152516be2d20SChuck Lever } 152616be2d20SChuck Lever 1527a246b010SChuck Lever /** 15289c3d72deSChuck Lever * xs_udp_connect_worker4 - set up a UDP socket 1529a246b010SChuck Lever * @work: RPC transport to connect 1530a246b010SChuck Lever * 1531a246b010SChuck Lever * Invoked by a work queue tasklet. 1532a246b010SChuck Lever */ 15339c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work) 1534a246b010SChuck Lever { 1535a246b010SChuck Lever struct sock_xprt *transport = 1536a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1537a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1538a246b010SChuck Lever struct socket *sock = transport->sock; 1539a246b010SChuck Lever int err, status = -EIO; 1540a246b010SChuck Lever 154101d37c42STrond Myklebust if (xprt->shutdown) 15429903cd1cSChuck Lever goto out; 15439903cd1cSChuck Lever 1544a246b010SChuck Lever /* Start by resetting any existing state */ 1545fe315e76SChuck Lever xs_reset_transport(transport); 1546a246b010SChuck Lever 1547fe315e76SChuck Lever err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); 1548fe315e76SChuck Lever if (err < 0) { 15499903cd1cSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1550a246b010SChuck Lever goto out; 1551a246b010SChuck Lever } 15528945ee5eSChuck Lever xs_reclassify_socket4(sock); 1553a246b010SChuck Lever 15547dc753f0SChuck Lever if (xs_bind4(transport, sock)) { 15559903cd1cSChuck Lever sock_release(sock); 15569903cd1cSChuck Lever goto out; 1557a246b010SChuck Lever } 1558b0d93ad5SChuck Lever 1559b0d93ad5SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 1560b0d93ad5SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1561b0d93ad5SChuck Lever 156216be2d20SChuck Lever xs_udp_finish_connecting(xprt, sock); 1563a246b010SChuck Lever status = 0; 1564b0d93ad5SChuck Lever out: 1565b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 1566b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1567b0d93ad5SChuck Lever } 1568b0d93ad5SChuck Lever 156968e220bdSChuck Lever /** 157068e220bdSChuck Lever * xs_udp_connect_worker6 - set up a UDP socket 157168e220bdSChuck Lever * @work: RPC transport to connect 157268e220bdSChuck Lever * 157368e220bdSChuck Lever * Invoked by a work queue tasklet. 157468e220bdSChuck Lever */ 157568e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work) 157668e220bdSChuck Lever { 157768e220bdSChuck Lever struct sock_xprt *transport = 157868e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 157968e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 158068e220bdSChuck Lever struct socket *sock = transport->sock; 158168e220bdSChuck Lever int err, status = -EIO; 158268e220bdSChuck Lever 158301d37c42STrond Myklebust if (xprt->shutdown) 158468e220bdSChuck Lever goto out; 158568e220bdSChuck Lever 158668e220bdSChuck Lever /* Start by resetting any existing state */ 1587fe315e76SChuck Lever xs_reset_transport(transport); 158868e220bdSChuck Lever 1589fe315e76SChuck Lever err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); 1590fe315e76SChuck Lever if (err < 0) { 159168e220bdSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 159268e220bdSChuck Lever goto out; 159368e220bdSChuck Lever } 15948945ee5eSChuck Lever xs_reclassify_socket6(sock); 159568e220bdSChuck Lever 159668e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 159768e220bdSChuck Lever sock_release(sock); 159868e220bdSChuck Lever goto out; 159968e220bdSChuck Lever } 160068e220bdSChuck Lever 160168e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 160268e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 160368e220bdSChuck Lever 160468e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 1605a246b010SChuck Lever status = 0; 1606b0d93ad5SChuck Lever out: 1607b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 1608b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1609b0d93ad5SChuck Lever } 1610b0d93ad5SChuck Lever 16113167e12cSChuck Lever /* 16123167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 16133167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 16143167e12cSChuck Lever */ 161540d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 16163167e12cSChuck Lever { 16173167e12cSChuck Lever int result; 16183167e12cSChuck Lever struct sockaddr any; 16193167e12cSChuck Lever 16203167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 16213167e12cSChuck Lever 16223167e12cSChuck Lever /* 16233167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 16243167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 16253167e12cSChuck Lever */ 16263167e12cSChuck Lever memset(&any, 0, sizeof(any)); 16273167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1628ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 16293167e12cSChuck Lever if (result) 16303167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 16313167e12cSChuck Lever result); 16323167e12cSChuck Lever } 16333167e12cSChuck Lever 163440d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 163540d2549dSTrond Myklebust { 163640d2549dSTrond Myklebust unsigned int state = transport->inet->sk_state; 163740d2549dSTrond Myklebust 163840d2549dSTrond Myklebust if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) 163940d2549dSTrond Myklebust return; 164040d2549dSTrond Myklebust if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) 164140d2549dSTrond Myklebust return; 164240d2549dSTrond Myklebust xs_abort_connection(xprt, transport); 164340d2549dSTrond Myklebust } 164440d2549dSTrond Myklebust 164516be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1646b0d93ad5SChuck Lever { 164716be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1648edb267a6SChuck Lever 1649ee0ac0c2SChuck Lever if (!transport->inet) { 1650b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1651b0d93ad5SChuck Lever 1652b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1653b0d93ad5SChuck Lever 16542a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 16552a9e1cfaSTrond Myklebust 1656b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1657b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1658b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1659b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1660482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1661b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 16623167e12cSChuck Lever 16633167e12cSChuck Lever /* socket options */ 16643167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 16653167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 16663167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 16673167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1668b0d93ad5SChuck Lever 1669b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1670b0d93ad5SChuck Lever 1671b0d93ad5SChuck Lever /* Reset to new socket */ 1672ee0ac0c2SChuck Lever transport->sock = sock; 1673ee0ac0c2SChuck Lever transport->inet = sk; 1674b0d93ad5SChuck Lever 1675b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1676b0d93ad5SChuck Lever } 1677b0d93ad5SChuck Lever 167801d37c42STrond Myklebust if (!xprt_bound(xprt)) 167901d37c42STrond Myklebust return -ENOTCONN; 168001d37c42STrond Myklebust 1681b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1682262ca07dSChuck Lever xprt->stat.connect_count++; 1683262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 168495392c59SChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 168516be2d20SChuck Lever } 168616be2d20SChuck Lever 168716be2d20SChuck Lever /** 16889c3d72deSChuck Lever * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint 168916be2d20SChuck Lever * @work: RPC transport to connect 169016be2d20SChuck Lever * 169116be2d20SChuck Lever * Invoked by a work queue tasklet. 169216be2d20SChuck Lever */ 16939c3d72deSChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work) 169416be2d20SChuck Lever { 169516be2d20SChuck Lever struct sock_xprt *transport = 169616be2d20SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 169716be2d20SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 169816be2d20SChuck Lever struct socket *sock = transport->sock; 169916be2d20SChuck Lever int err, status = -EIO; 170016be2d20SChuck Lever 170101d37c42STrond Myklebust if (xprt->shutdown) 170216be2d20SChuck Lever goto out; 170316be2d20SChuck Lever 170416be2d20SChuck Lever if (!sock) { 170516be2d20SChuck Lever /* start from scratch */ 170616be2d20SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 170716be2d20SChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 170816be2d20SChuck Lever goto out; 170916be2d20SChuck Lever } 17108945ee5eSChuck Lever xs_reclassify_socket4(sock); 171116be2d20SChuck Lever 171216be2d20SChuck Lever if (xs_bind4(transport, sock) < 0) { 171316be2d20SChuck Lever sock_release(sock); 171416be2d20SChuck Lever goto out; 171516be2d20SChuck Lever } 171616be2d20SChuck Lever } else 171716be2d20SChuck Lever /* "close" the socket, preserving the local port */ 171840d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 171916be2d20SChuck Lever 172016be2d20SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 172116be2d20SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 172216be2d20SChuck Lever 172316be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 1724a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 172546121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 172646121cf7SChuck Lever sock->sk->sk_state); 1727a246b010SChuck Lever switch (status) { 17288a2cec29STrond Myklebust case -ECONNREFUSED: 17298a2cec29STrond Myklebust case -ECONNRESET: 17308a2cec29STrond Myklebust case -ENETUNREACH: 17318a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 17322a491991STrond Myklebust case 0: 1733a246b010SChuck Lever case -EINPROGRESS: 1734a246b010SChuck Lever case -EALREADY: 1735a246b010SChuck Lever goto out_clear; 17368a2cec29STrond Myklebust } 17373167e12cSChuck Lever /* get rid of existing socket, and retry */ 1738e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 17392a491991STrond Myklebust printk("%s: connect returned unhandled error %d\n", 17402a491991STrond Myklebust __func__, status); 17412a491991STrond Myklebust status = -EAGAIN; 1742a246b010SChuck Lever out: 174344fbac22SChuck Lever xprt_wake_pending_tasks(xprt, status); 1744a246b010SChuck Lever out_clear: 17452226feb6SChuck Lever xprt_clear_connecting(xprt); 1746a246b010SChuck Lever } 1747a246b010SChuck Lever 17489903cd1cSChuck Lever /** 174968e220bdSChuck Lever * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint 175068e220bdSChuck Lever * @work: RPC transport to connect 175168e220bdSChuck Lever * 175268e220bdSChuck Lever * Invoked by a work queue tasklet. 175368e220bdSChuck Lever */ 175468e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work) 175568e220bdSChuck Lever { 175668e220bdSChuck Lever struct sock_xprt *transport = 175768e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 175868e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 175968e220bdSChuck Lever struct socket *sock = transport->sock; 176068e220bdSChuck Lever int err, status = -EIO; 176168e220bdSChuck Lever 176201d37c42STrond Myklebust if (xprt->shutdown) 176368e220bdSChuck Lever goto out; 176468e220bdSChuck Lever 176568e220bdSChuck Lever if (!sock) { 176668e220bdSChuck Lever /* start from scratch */ 176768e220bdSChuck Lever if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 176868e220bdSChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 176968e220bdSChuck Lever goto out; 177068e220bdSChuck Lever } 17718945ee5eSChuck Lever xs_reclassify_socket6(sock); 177268e220bdSChuck Lever 177368e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 177468e220bdSChuck Lever sock_release(sock); 177568e220bdSChuck Lever goto out; 177668e220bdSChuck Lever } 177768e220bdSChuck Lever } else 177868e220bdSChuck Lever /* "close" the socket, preserving the local port */ 177940d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 178068e220bdSChuck Lever 178168e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 178268e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 178368e220bdSChuck Lever 178468e220bdSChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 178568e220bdSChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 178668e220bdSChuck Lever xprt, -status, xprt_connected(xprt), sock->sk->sk_state); 178768e220bdSChuck Lever switch (status) { 17888a2cec29STrond Myklebust case -ECONNREFUSED: 17898a2cec29STrond Myklebust case -ECONNRESET: 17908a2cec29STrond Myklebust case -ENETUNREACH: 17918a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 17922a491991STrond Myklebust case 0: 179368e220bdSChuck Lever case -EINPROGRESS: 179468e220bdSChuck Lever case -EALREADY: 179568e220bdSChuck Lever goto out_clear; 17968a2cec29STrond Myklebust } 179768e220bdSChuck Lever /* get rid of existing socket, and retry */ 1798e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 17992a491991STrond Myklebust printk("%s: connect returned unhandled error %d\n", 18002a491991STrond Myklebust __func__, status); 18012a491991STrond Myklebust status = -EAGAIN; 180268e220bdSChuck Lever out: 180368e220bdSChuck Lever xprt_wake_pending_tasks(xprt, status); 180468e220bdSChuck Lever out_clear: 180568e220bdSChuck Lever xprt_clear_connecting(xprt); 180668e220bdSChuck Lever } 180768e220bdSChuck Lever 180868e220bdSChuck Lever /** 18099903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 18109903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 18119903cd1cSChuck Lever * 18129903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 181303bf4b70SChuck Lever * 181403bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 181503bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 181603bf4b70SChuck Lever * socket on a privileged port. 181703bf4b70SChuck Lever * 181803bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 181903bf4b70SChuck Lever * retry floods (hard mounts). 18209903cd1cSChuck Lever */ 18219903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 1822a246b010SChuck Lever { 1823a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1824ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1825a246b010SChuck Lever 1826b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1827b0d93ad5SChuck Lever return; 1828b0d93ad5SChuck Lever 1829ee0ac0c2SChuck Lever if (transport->sock != NULL) { 183046121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 183146121cf7SChuck Lever "seconds\n", 183203bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 1833c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1834c1384c9cSTrond Myklebust &transport->connect_worker, 183503bf4b70SChuck Lever xprt->reestablish_timeout); 183603bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 183703bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 183803bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 18399903cd1cSChuck Lever } else { 18409903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 1841c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1842c1384c9cSTrond Myklebust &transport->connect_worker, 0); 1843a246b010SChuck Lever } 1844a246b010SChuck Lever } 1845a246b010SChuck Lever 1846e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task) 1847e06799f9STrond Myklebust { 1848e06799f9STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1849e06799f9STrond Myklebust 1850e06799f9STrond Myklebust /* Exit if we need to wait for socket shutdown to complete */ 1851e06799f9STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 1852e06799f9STrond Myklebust return; 1853e06799f9STrond Myklebust xs_connect(task); 1854e06799f9STrond Myklebust } 1855e06799f9STrond Myklebust 1856262ca07dSChuck Lever /** 1857262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 1858262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1859262ca07dSChuck Lever * @seq: output file 1860262ca07dSChuck Lever * 1861262ca07dSChuck Lever */ 1862262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1863262ca07dSChuck Lever { 1864c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1865c8475461SChuck Lever 1866262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 1867c8475461SChuck Lever transport->port, 1868262ca07dSChuck Lever xprt->stat.bind_count, 1869262ca07dSChuck Lever xprt->stat.sends, 1870262ca07dSChuck Lever xprt->stat.recvs, 1871262ca07dSChuck Lever xprt->stat.bad_xids, 1872262ca07dSChuck Lever xprt->stat.req_u, 1873262ca07dSChuck Lever xprt->stat.bklog_u); 1874262ca07dSChuck Lever } 1875262ca07dSChuck Lever 1876262ca07dSChuck Lever /** 1877262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 1878262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1879262ca07dSChuck Lever * @seq: output file 1880262ca07dSChuck Lever * 1881262ca07dSChuck Lever */ 1882262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1883262ca07dSChuck Lever { 1884c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1885262ca07dSChuck Lever long idle_time = 0; 1886262ca07dSChuck Lever 1887262ca07dSChuck Lever if (xprt_connected(xprt)) 1888262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 1889262ca07dSChuck Lever 1890262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 1891c8475461SChuck Lever transport->port, 1892262ca07dSChuck Lever xprt->stat.bind_count, 1893262ca07dSChuck Lever xprt->stat.connect_count, 1894262ca07dSChuck Lever xprt->stat.connect_time, 1895262ca07dSChuck Lever idle_time, 1896262ca07dSChuck Lever xprt->stat.sends, 1897262ca07dSChuck Lever xprt->stat.recvs, 1898262ca07dSChuck Lever xprt->stat.bad_xids, 1899262ca07dSChuck Lever xprt->stat.req_u, 1900262ca07dSChuck Lever xprt->stat.bklog_u); 1901262ca07dSChuck Lever } 1902262ca07dSChuck Lever 1903262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 190443118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 190512a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 190649e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 190745160d62SChuck Lever .rpcbind = rpcb_getport_async, 190892200412SChuck Lever .set_port = xs_set_port, 19099903cd1cSChuck Lever .connect = xs_connect, 191002107148SChuck Lever .buf_alloc = rpc_malloc, 191102107148SChuck Lever .buf_free = rpc_free, 1912262965f5SChuck Lever .send_request = xs_udp_send_request, 1913fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 191446c0ee8bSChuck Lever .timer = xs_udp_timer, 1915a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 1916262965f5SChuck Lever .close = xs_close, 1917262965f5SChuck Lever .destroy = xs_destroy, 1918262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 1919262965f5SChuck Lever }; 1920262965f5SChuck Lever 1921262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 192212a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 1923e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 192445160d62SChuck Lever .rpcbind = rpcb_getport_async, 192592200412SChuck Lever .set_port = xs_set_port, 1926e06799f9STrond Myklebust .connect = xs_tcp_connect, 192702107148SChuck Lever .buf_alloc = rpc_malloc, 192802107148SChuck Lever .buf_free = rpc_free, 1929262965f5SChuck Lever .send_request = xs_tcp_send_request, 1930fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 1931e06799f9STrond Myklebust .close = xs_tcp_shutdown, 19329903cd1cSChuck Lever .destroy = xs_destroy, 1933262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 1934a246b010SChuck Lever }; 1935a246b010SChuck Lever 19363c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 1937bc25571eS\"Talpey, Thomas\ unsigned int slot_table_size) 1938c8541ecdSChuck Lever { 1939c8541ecdSChuck Lever struct rpc_xprt *xprt; 1940ffc2e518SChuck Lever struct sock_xprt *new; 1941c8541ecdSChuck Lever 194296802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 1943c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 1944c8541ecdSChuck Lever return ERR_PTR(-EBADF); 1945c8541ecdSChuck Lever } 1946c8541ecdSChuck Lever 1947ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 1948ffc2e518SChuck Lever if (new == NULL) { 194946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 195046121cf7SChuck Lever "rpc_xprt\n"); 1951c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1952c8541ecdSChuck Lever } 1953ffc2e518SChuck Lever xprt = &new->xprt; 1954c8541ecdSChuck Lever 1955c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 1956c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 1957c8541ecdSChuck Lever if (xprt->slot == NULL) { 1958c8541ecdSChuck Lever kfree(xprt); 195946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 196046121cf7SChuck Lever "table\n"); 1961c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1962c8541ecdSChuck Lever } 1963c8541ecdSChuck Lever 196496802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 196596802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 1966d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 1967d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 1968c8541ecdSChuck Lever 1969c8541ecdSChuck Lever return xprt; 1970c8541ecdSChuck Lever } 1971c8541ecdSChuck Lever 19722881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 19732881ae74STrond Myklebust .to_initval = 5 * HZ, 19742881ae74STrond Myklebust .to_maxval = 30 * HZ, 19752881ae74STrond Myklebust .to_increment = 5 * HZ, 19762881ae74STrond Myklebust .to_retries = 5, 19772881ae74STrond Myklebust }; 19782881ae74STrond Myklebust 19799903cd1cSChuck Lever /** 19809903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 198196802a09SFrank van Maarseveen * @args: rpc transport creation arguments 19829903cd1cSChuck Lever * 19839903cd1cSChuck Lever */ 1984483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 1985a246b010SChuck Lever { 19868f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 1987c8541ecdSChuck Lever struct rpc_xprt *xprt; 1988c8475461SChuck Lever struct sock_xprt *transport; 1989a246b010SChuck Lever 199096802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 1991c8541ecdSChuck Lever if (IS_ERR(xprt)) 1992c8541ecdSChuck Lever return xprt; 1993c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 1994a246b010SChuck Lever 1995ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 1996808012fbSChuck Lever xprt->tsh_size = 0; 1997a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 1998a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 1999a246b010SChuck Lever 200003bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 200103bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 200203bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 200303bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2004a246b010SChuck Lever 2005262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2006a246b010SChuck Lever 2007ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2008a246b010SChuck Lever 20098f9d5b1aSChuck Lever switch (addr->sa_family) { 20108f9d5b1aSChuck Lever case AF_INET: 20118f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 20128f9d5b1aSChuck Lever xprt_set_bound(xprt); 20138f9d5b1aSChuck Lever 20148f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 20158f9d5b1aSChuck Lever xs_udp_connect_worker4); 2016b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 20178f9d5b1aSChuck Lever break; 20188f9d5b1aSChuck Lever case AF_INET6: 20198f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 20208f9d5b1aSChuck Lever xprt_set_bound(xprt); 20218f9d5b1aSChuck Lever 20228f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 20238f9d5b1aSChuck Lever xs_udp_connect_worker6); 2024b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 20258f9d5b1aSChuck Lever break; 20268f9d5b1aSChuck Lever default: 20278f9d5b1aSChuck Lever kfree(xprt); 20288f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 20298f9d5b1aSChuck Lever } 20308f9d5b1aSChuck Lever 2031edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 20327559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2033edb267a6SChuck Lever 2034bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2035c8541ecdSChuck Lever return xprt; 2036bc25571eS\"Talpey, Thomas\ 2037bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2038bc25571eS\"Talpey, Thomas\ kfree(xprt); 2039bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2040a246b010SChuck Lever } 2041a246b010SChuck Lever 20422881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 20432881ae74STrond Myklebust .to_initval = 60 * HZ, 20442881ae74STrond Myklebust .to_maxval = 60 * HZ, 20452881ae74STrond Myklebust .to_retries = 2, 20462881ae74STrond Myklebust }; 20472881ae74STrond Myklebust 20489903cd1cSChuck Lever /** 20499903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 205096802a09SFrank van Maarseveen * @args: rpc transport creation arguments 20519903cd1cSChuck Lever * 20529903cd1cSChuck Lever */ 2053483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2054a246b010SChuck Lever { 20558f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2056c8541ecdSChuck Lever struct rpc_xprt *xprt; 2057c8475461SChuck Lever struct sock_xprt *transport; 2058a246b010SChuck Lever 205996802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 2060c8541ecdSChuck Lever if (IS_ERR(xprt)) 2061c8541ecdSChuck Lever return xprt; 2062c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2063a246b010SChuck Lever 2064ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2065808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2066808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2067a246b010SChuck Lever 206803bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 206903bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 207003bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 207103bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2072a246b010SChuck Lever 2073262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2074ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2075a246b010SChuck Lever 20768f9d5b1aSChuck Lever switch (addr->sa_family) { 20778f9d5b1aSChuck Lever case AF_INET: 20788f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 20798f9d5b1aSChuck Lever xprt_set_bound(xprt); 20808f9d5b1aSChuck Lever 20818f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); 2082b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 20838f9d5b1aSChuck Lever break; 20848f9d5b1aSChuck Lever case AF_INET6: 20858f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 20868f9d5b1aSChuck Lever xprt_set_bound(xprt); 20878f9d5b1aSChuck Lever 20888f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); 2089b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 20908f9d5b1aSChuck Lever break; 20918f9d5b1aSChuck Lever default: 20928f9d5b1aSChuck Lever kfree(xprt); 20938f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 20948f9d5b1aSChuck Lever } 20958f9d5b1aSChuck Lever 2096edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 20977559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2098edb267a6SChuck Lever 2099bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2100c8541ecdSChuck Lever return xprt; 2101bc25571eS\"Talpey, Thomas\ 2102bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2103bc25571eS\"Talpey, Thomas\ kfree(xprt); 2104bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2105a246b010SChuck Lever } 2106282b32e1SChuck Lever 2107bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 2108bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 2109bc25571eS\"Talpey, Thomas\ .name = "udp", 2110bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21114fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_UDP, 2112bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 2113bc25571eS\"Talpey, Thomas\ }; 2114bc25571eS\"Talpey, Thomas\ 2115bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 2116bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 2117bc25571eS\"Talpey, Thomas\ .name = "tcp", 2118bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21194fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_TCP, 2120bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 2121bc25571eS\"Talpey, Thomas\ }; 2122bc25571eS\"Talpey, Thomas\ 2123282b32e1SChuck Lever /** 2124bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 2125282b32e1SChuck Lever * 2126282b32e1SChuck Lever */ 2127282b32e1SChuck Lever int init_socket_xprt(void) 2128282b32e1SChuck Lever { 2129fbf76683SChuck Lever #ifdef RPC_DEBUG 21302b1bec5fSEric W. Biederman if (!sunrpc_table_header) 21310b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 2132fbf76683SChuck Lever #endif 2133fbf76683SChuck Lever 2134bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 2135bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 2136bc25571eS\"Talpey, Thomas\ 2137282b32e1SChuck Lever return 0; 2138282b32e1SChuck Lever } 2139282b32e1SChuck Lever 2140282b32e1SChuck Lever /** 2141bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 2142282b32e1SChuck Lever * 2143282b32e1SChuck Lever */ 2144282b32e1SChuck Lever void cleanup_socket_xprt(void) 2145282b32e1SChuck Lever { 2146fbf76683SChuck Lever #ifdef RPC_DEBUG 2147fbf76683SChuck Lever if (sunrpc_table_header) { 2148fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 2149fbf76683SChuck Lever sunrpc_table_header = NULL; 2150fbf76683SChuck Lever } 2151fbf76683SChuck Lever #endif 2152bc25571eS\"Talpey, Thomas\ 2153bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 2154bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 2155282b32e1SChuck Lever } 2156