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 */ 524262965f5SChuck Lever static void 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); 529a246b010SChuck Lever 53046121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 531262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 532262965f5SChuck Lever req->rq_slen); 533a246b010SChuck Lever 534262965f5SChuck Lever /* Protect against races with write_space */ 535262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 536a246b010SChuck Lever 537262965f5SChuck Lever /* Don't race with disconnect */ 538b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 539b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 540b6ddf64fSTrond Myklebust /* 541b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 542b6ddf64fSTrond Myklebust * window size 543b6ddf64fSTrond Myklebust */ 544b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 545b6ddf64fSTrond Myklebust transport->inet->sk_write_pending++; 546b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 547b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 548b6ddf64fSTrond Myklebust } 549b6ddf64fSTrond Myklebust } else { 550b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 551262965f5SChuck Lever task->tk_status = -ENOTCONN; 552b6ddf64fSTrond Myklebust } 553a246b010SChuck Lever 554262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 555a246b010SChuck Lever } 556a246b010SChuck Lever 5579903cd1cSChuck Lever /** 558262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5599903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5609903cd1cSChuck Lever * 5619903cd1cSChuck Lever * Return values: 5629903cd1cSChuck Lever * 0: The request has been sent 5639903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5649903cd1cSChuck Lever * complete the request 565262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 5669903cd1cSChuck Lever * other: Some other error occured, the request was not sent 5679903cd1cSChuck Lever */ 568262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 569a246b010SChuck Lever { 570a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 571a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 572ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 573262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 574262965f5SChuck Lever int status; 575262965f5SChuck Lever 576262965f5SChuck Lever xs_pktdump("packet data:", 577262965f5SChuck Lever req->rq_svec->iov_base, 578262965f5SChuck Lever req->rq_svec->iov_len); 579262965f5SChuck Lever 58001d37c42STrond Myklebust if (!xprt_bound(xprt)) 58101d37c42STrond Myklebust return -ENOTCONN; 582ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 58395392c59SChuck Lever xs_addr(xprt), 584ee0ac0c2SChuck Lever xprt->addrlen, xdr, 585ee0ac0c2SChuck Lever req->rq_bytes_sent); 586262965f5SChuck Lever 587262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 588262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 589262965f5SChuck Lever 5902199700fSTrond Myklebust if (status >= 0) { 5911321d8d9SChuck Lever task->tk_bytes_sent += status; 5922199700fSTrond Myklebust if (status >= req->rq_slen) 593262965f5SChuck Lever return 0; 594262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 595262965f5SChuck Lever status = -EAGAIN; 5962199700fSTrond Myklebust } 597c8485e4dSTrond Myklebust if (!transport->sock) 598c8485e4dSTrond Myklebust goto out; 599262965f5SChuck Lever 600262965f5SChuck Lever switch (status) { 601fba91afbSTrond Myklebust case -ENOTSOCK: 602fba91afbSTrond Myklebust status = -ENOTCONN; 603fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 604fba91afbSTrond Myklebust break; 605b6ddf64fSTrond Myklebust case -EAGAIN: 606b6ddf64fSTrond Myklebust xs_nospace(task); 607b6ddf64fSTrond Myklebust break; 608c8485e4dSTrond Myklebust default: 609c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 610c8485e4dSTrond Myklebust -status); 611262965f5SChuck Lever case -ENETUNREACH: 612262965f5SChuck Lever case -EPIPE: 613262965f5SChuck Lever case -ECONNREFUSED: 614262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 615262965f5SChuck Lever * prompts ECONNREFUSED. */ 616b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 617262965f5SChuck Lever } 618c8485e4dSTrond Myklebust out: 619262965f5SChuck Lever return status; 620262965f5SChuck Lever } 621262965f5SChuck Lever 622e06799f9STrond Myklebust /** 623e06799f9STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 624e06799f9STrond Myklebust * @xprt: transport 625e06799f9STrond Myklebust * 626e06799f9STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 627e06799f9STrond Myklebust * equivalent of shutdown(SHUT_WR); 628e06799f9STrond Myklebust */ 629e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 630e06799f9STrond Myklebust { 631e06799f9STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 632e06799f9STrond Myklebust struct socket *sock = transport->sock; 633e06799f9STrond Myklebust 634e06799f9STrond Myklebust if (sock != NULL) 635e06799f9STrond Myklebust kernel_sock_shutdown(sock, SHUT_WR); 636e06799f9STrond Myklebust } 637e06799f9STrond Myklebust 638808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 639808012fbSChuck Lever { 640808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 641808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 642808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 643808012fbSChuck Lever } 644808012fbSChuck Lever 645262965f5SChuck Lever /** 646262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 647262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 648262965f5SChuck Lever * 649262965f5SChuck Lever * Return values: 650262965f5SChuck Lever * 0: The request has been sent 651262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 652262965f5SChuck Lever * complete the request 653262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 654262965f5SChuck Lever * other: Some other error occured, the request was not sent 655262965f5SChuck Lever * 656262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 657262965f5SChuck Lever * if sendmsg is not able to make progress? 658262965f5SChuck Lever */ 659262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 660262965f5SChuck Lever { 661262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 662262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 663ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 664262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 665b595bb15SChuck Lever int status; 666a246b010SChuck Lever 667808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 668262965f5SChuck Lever 669262965f5SChuck Lever xs_pktdump("packet data:", 670262965f5SChuck Lever req->rq_svec->iov_base, 671262965f5SChuck Lever req->rq_svec->iov_len); 672a246b010SChuck Lever 673a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 674a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 675262965f5SChuck Lever * called sendmsg(). */ 676a246b010SChuck Lever while (1) { 677ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 678ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 679a246b010SChuck Lever 680262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 681262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 682262965f5SChuck Lever 683262965f5SChuck Lever if (unlikely(status < 0)) 684a246b010SChuck Lever break; 685a246b010SChuck Lever 686a246b010SChuck Lever /* If we've sent the entire packet, immediately 687a246b010SChuck Lever * reset the count of bytes sent. */ 688262965f5SChuck Lever req->rq_bytes_sent += status; 689ef759a2eSChuck Lever task->tk_bytes_sent += status; 690262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 691a246b010SChuck Lever req->rq_bytes_sent = 0; 692a246b010SChuck Lever return 0; 693a246b010SChuck Lever } 694262965f5SChuck Lever 69506b4b681STrond Myklebust if (status != 0) 69606b4b681STrond Myklebust continue; 697a246b010SChuck Lever status = -EAGAIN; 698a246b010SChuck Lever break; 699a246b010SChuck Lever } 700c8485e4dSTrond Myklebust if (!transport->sock) 701c8485e4dSTrond Myklebust goto out; 702a246b010SChuck Lever 703262965f5SChuck Lever switch (status) { 704fba91afbSTrond Myklebust case -ENOTSOCK: 705fba91afbSTrond Myklebust status = -ENOTCONN; 706fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 707fba91afbSTrond Myklebust break; 708262965f5SChuck Lever case -EAGAIN: 709262965f5SChuck Lever xs_nospace(task); 710262965f5SChuck Lever break; 711c8485e4dSTrond Myklebust default: 712c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 713c8485e4dSTrond Myklebust -status); 714262965f5SChuck Lever case -ECONNRESET: 7152a9e1cfaSTrond Myklebust xs_tcp_shutdown(xprt); 7162a9e1cfaSTrond Myklebust case -ECONNREFUSED: 717262965f5SChuck Lever case -ENOTCONN: 718262965f5SChuck Lever case -EPIPE: 719b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 720a246b010SChuck Lever } 721c8485e4dSTrond Myklebust out: 722a246b010SChuck Lever return status; 723a246b010SChuck Lever } 724a246b010SChuck Lever 7259903cd1cSChuck Lever /** 726e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 727e0ab53deSTrond Myklebust * @xprt: transport 728e0ab53deSTrond Myklebust * @task: rpc task 729e0ab53deSTrond Myklebust * 730e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 731e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 732e0ab53deSTrond Myklebust * the server. 733e0ab53deSTrond Myklebust */ 734e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 735e0ab53deSTrond Myklebust { 736e0ab53deSTrond Myklebust struct rpc_rqst *req; 737e0ab53deSTrond Myklebust 738e0ab53deSTrond Myklebust if (task != xprt->snd_task) 739e0ab53deSTrond Myklebust return; 740e0ab53deSTrond Myklebust if (task == NULL) 741e0ab53deSTrond Myklebust goto out_release; 742e0ab53deSTrond Myklebust req = task->tk_rqstp; 743e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 744e0ab53deSTrond Myklebust goto out_release; 745e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 746e0ab53deSTrond Myklebust goto out_release; 747e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 748e0ab53deSTrond Myklebust out_release: 749e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 750e0ab53deSTrond Myklebust } 751e0ab53deSTrond Myklebust 7522a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7532a9e1cfaSTrond Myklebust { 7542a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7552a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7562a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7572a9e1cfaSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7582a9e1cfaSTrond Myklebust } 7592a9e1cfaSTrond Myklebust 7602a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7612a9e1cfaSTrond Myklebust { 7622a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7632a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7642a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7652a9e1cfaSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7662a9e1cfaSTrond Myklebust } 7672a9e1cfaSTrond Myklebust 768fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 769a246b010SChuck Lever { 770ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 771ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 772a246b010SChuck Lever 773fe315e76SChuck Lever if (sk == NULL) 774fe315e76SChuck Lever return; 7759903cd1cSChuck Lever 776a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 777ee0ac0c2SChuck Lever transport->inet = NULL; 778ee0ac0c2SChuck Lever transport->sock = NULL; 779a246b010SChuck Lever 780a246b010SChuck Lever sk->sk_user_data = NULL; 7812a9e1cfaSTrond Myklebust 7822a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 783a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 784a246b010SChuck Lever 785a246b010SChuck Lever sk->sk_no_check = 0; 786a246b010SChuck Lever 787a246b010SChuck Lever sock_release(sock); 788fe315e76SChuck Lever } 789fe315e76SChuck Lever 790fe315e76SChuck Lever /** 791fe315e76SChuck Lever * xs_close - close a socket 792fe315e76SChuck Lever * @xprt: transport 793fe315e76SChuck Lever * 794fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 795fe315e76SChuck Lever * on the server we want to save. 796fe315e76SChuck Lever */ 797fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 798fe315e76SChuck Lever { 799fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 800fe315e76SChuck Lever 801fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 802fe315e76SChuck Lever 803fe315e76SChuck Lever xs_reset_transport(transport); 804fe315e76SChuck Lever 805632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 806632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 8073b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 808632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 80962da3b24STrond Myklebust xprt_disconnect_done(xprt); 810a246b010SChuck Lever } 811a246b010SChuck Lever 8129903cd1cSChuck Lever /** 8139903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8149903cd1cSChuck Lever * @xprt: doomed transport 8159903cd1cSChuck Lever * 8169903cd1cSChuck Lever */ 8179903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 818a246b010SChuck Lever { 819c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 820c8475461SChuck Lever 8219903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8229903cd1cSChuck Lever 823c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 824a246b010SChuck Lever 8259903cd1cSChuck Lever xs_close(xprt); 826edb267a6SChuck Lever xs_free_peer_addresses(xprt); 827a246b010SChuck Lever kfree(xprt->slot); 828c8541ecdSChuck Lever kfree(xprt); 829bc25571eS\"Talpey, Thomas\ module_put(THIS_MODULE); 830a246b010SChuck Lever } 831a246b010SChuck Lever 8329903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 8339903cd1cSChuck Lever { 8349903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 8359903cd1cSChuck Lever } 8369903cd1cSChuck Lever 8379903cd1cSChuck Lever /** 8389903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 8399903cd1cSChuck Lever * @sk: socket with data to read 8409903cd1cSChuck Lever * @len: how much data to read 8419903cd1cSChuck Lever * 842a246b010SChuck Lever */ 8439903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 844a246b010SChuck Lever { 845a246b010SChuck Lever struct rpc_task *task; 846a246b010SChuck Lever struct rpc_xprt *xprt; 847a246b010SChuck Lever struct rpc_rqst *rovr; 848a246b010SChuck Lever struct sk_buff *skb; 849a246b010SChuck Lever int err, repsize, copied; 850d8ed029dSAlexey Dobriyan u32 _xid; 851d8ed029dSAlexey Dobriyan __be32 *xp; 852a246b010SChuck Lever 853a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 8549903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 8559903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 856a246b010SChuck Lever goto out; 857a246b010SChuck Lever 858a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 859a246b010SChuck Lever goto out; 860a246b010SChuck Lever 861a246b010SChuck Lever if (xprt->shutdown) 862a246b010SChuck Lever goto dropit; 863a246b010SChuck Lever 864a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 865a246b010SChuck Lever if (repsize < 4) { 8669903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 867a246b010SChuck Lever goto dropit; 868a246b010SChuck Lever } 869a246b010SChuck Lever 870a246b010SChuck Lever /* Copy the XID from the skb... */ 871a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 872a246b010SChuck Lever sizeof(_xid), &_xid); 873a246b010SChuck Lever if (xp == NULL) 874a246b010SChuck Lever goto dropit; 875a246b010SChuck Lever 876a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 8774a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 878a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 879a246b010SChuck Lever if (!rovr) 880a246b010SChuck Lever goto out_unlock; 881a246b010SChuck Lever task = rovr->rq_task; 882a246b010SChuck Lever 883a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 884a246b010SChuck Lever copied = repsize; 885a246b010SChuck Lever 886a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 8871781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 8881781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 889a246b010SChuck Lever goto out_unlock; 8901781f7f5SHerbert Xu } 8911781f7f5SHerbert Xu 8921781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 893a246b010SChuck Lever 894a246b010SChuck Lever /* Something worked... */ 895a246b010SChuck Lever dst_confirm(skb->dst); 896a246b010SChuck Lever 8971570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 8981570c1e4SChuck Lever xprt_update_rtt(task); 8991570c1e4SChuck Lever xprt_complete_rqst(task, copied); 900a246b010SChuck Lever 901a246b010SChuck Lever out_unlock: 9024a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 903a246b010SChuck Lever dropit: 904a246b010SChuck Lever skb_free_datagram(sk, skb); 905a246b010SChuck Lever out: 906a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 907a246b010SChuck Lever } 908a246b010SChuck Lever 909dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 910a246b010SChuck Lever { 91151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 912a246b010SChuck Lever size_t len, used; 913a246b010SChuck Lever char *p; 914a246b010SChuck Lever 91551971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 91651971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 9179d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 91851971139SChuck Lever transport->tcp_offset += used; 919a246b010SChuck Lever if (used != len) 920a246b010SChuck Lever return; 921808012fbSChuck Lever 92251971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 92351971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 924e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 925a246b010SChuck Lever else 926e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 92751971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 928808012fbSChuck Lever 929e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 93051971139SChuck Lever transport->tcp_offset = 0; 931808012fbSChuck Lever 932a246b010SChuck Lever /* Sanity check of the record length */ 93351971139SChuck Lever if (unlikely(transport->tcp_reclen < 4)) { 9349903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 9353ebb067dSTrond Myklebust xprt_force_disconnect(xprt); 9369903cd1cSChuck Lever return; 937a246b010SChuck Lever } 938a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 93951971139SChuck Lever transport->tcp_reclen); 940a246b010SChuck Lever } 941a246b010SChuck Lever 94251971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 943a246b010SChuck Lever { 94451971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 945e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 94651971139SChuck Lever transport->tcp_offset = 0; 947e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 948e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 949e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 95051971139SChuck Lever transport->tcp_copied = 0; 951a246b010SChuck Lever } 952a246b010SChuck Lever } 953a246b010SChuck Lever } 954a246b010SChuck Lever 955dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 956a246b010SChuck Lever { 957a246b010SChuck Lever size_t len, used; 958a246b010SChuck Lever char *p; 959a246b010SChuck Lever 96051971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 961a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 96251971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 9639d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 96451971139SChuck Lever transport->tcp_offset += used; 965a246b010SChuck Lever if (used != len) 966a246b010SChuck Lever return; 967e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 968e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_DATA; 96951971139SChuck Lever transport->tcp_copied = 4; 970a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 97151971139SChuck Lever ntohl(transport->tcp_xid)); 97251971139SChuck Lever xs_tcp_check_fraghdr(transport); 973a246b010SChuck Lever } 974a246b010SChuck Lever 975dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 976a246b010SChuck Lever { 97751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 978a246b010SChuck Lever struct rpc_rqst *req; 979a246b010SChuck Lever struct xdr_buf *rcvbuf; 980a246b010SChuck Lever size_t len; 981a246b010SChuck Lever ssize_t r; 982a246b010SChuck Lever 983a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 9844a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 98551971139SChuck Lever req = xprt_lookup_rqst(xprt, transport->tcp_xid); 986a246b010SChuck Lever if (!req) { 987e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 988a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 98951971139SChuck Lever ntohl(transport->tcp_xid)); 9904a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 991a246b010SChuck Lever return; 992a246b010SChuck Lever } 993a246b010SChuck Lever 994a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 995a246b010SChuck Lever len = desc->count; 99651971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 997dd456471SChuck Lever struct xdr_skb_reader my_desc; 998a246b010SChuck Lever 99951971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1000a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1001a246b010SChuck Lever my_desc.count = len; 100251971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10039d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1004a246b010SChuck Lever desc->count -= r; 1005a246b010SChuck Lever desc->offset += r; 1006a246b010SChuck Lever } else 100751971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10089d292316SChuck Lever desc, xdr_skb_read_bits); 1009a246b010SChuck Lever 1010a246b010SChuck Lever if (r > 0) { 101151971139SChuck Lever transport->tcp_copied += r; 101251971139SChuck Lever transport->tcp_offset += r; 1013a246b010SChuck Lever } 1014a246b010SChuck Lever if (r != len) { 1015a246b010SChuck Lever /* Error when copying to the receive buffer, 1016a246b010SChuck Lever * usually because we weren't able to allocate 1017a246b010SChuck Lever * additional buffer pages. All we can do now 1018e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1019a246b010SChuck Lever * will not receive any additional updates, 1020a246b010SChuck Lever * and time out. 1021a246b010SChuck Lever * Any remaining data from this record will 1022a246b010SChuck Lever * be discarded. 1023a246b010SChuck Lever */ 1024e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1025a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 102651971139SChuck Lever ntohl(transport->tcp_xid)); 102746121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 102846121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 102946121cf7SChuck Lever xprt, transport->tcp_copied, 103046121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1031a246b010SChuck Lever goto out; 1032a246b010SChuck Lever } 1033a246b010SChuck Lever 1034a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 103551971139SChuck Lever ntohl(transport->tcp_xid), r); 103646121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 103746121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 103846121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1039a246b010SChuck Lever 104051971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1041e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 104251971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1043e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1044e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1045a246b010SChuck Lever } 1046a246b010SChuck Lever 1047a246b010SChuck Lever out: 1048e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 104951971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 10504a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 105151971139SChuck Lever xs_tcp_check_fraghdr(transport); 1052a246b010SChuck Lever } 1053a246b010SChuck Lever 1054dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1055a246b010SChuck Lever { 1056a246b010SChuck Lever size_t len; 1057a246b010SChuck Lever 105851971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1059a246b010SChuck Lever if (len > desc->count) 1060a246b010SChuck Lever len = desc->count; 1061a246b010SChuck Lever desc->count -= len; 1062a246b010SChuck Lever desc->offset += len; 106351971139SChuck Lever transport->tcp_offset += len; 1064a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 106551971139SChuck Lever xs_tcp_check_fraghdr(transport); 1066a246b010SChuck Lever } 1067a246b010SChuck Lever 10689903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1069a246b010SChuck Lever { 1070a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 107151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1072dd456471SChuck Lever struct xdr_skb_reader desc = { 1073a246b010SChuck Lever .skb = skb, 1074a246b010SChuck Lever .offset = offset, 1075a246b010SChuck Lever .count = len, 1076a246b010SChuck Lever }; 1077a246b010SChuck Lever 10789903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1079a246b010SChuck Lever do { 1080a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1081a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1082e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 10839903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1084a246b010SChuck Lever continue; 1085a246b010SChuck Lever } 1086a246b010SChuck Lever /* Read in the xid if necessary */ 1087e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 108851971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1089a246b010SChuck Lever continue; 1090a246b010SChuck Lever } 1091a246b010SChuck Lever /* Read in the request data */ 1092e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 10939903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 1094a246b010SChuck Lever continue; 1095a246b010SChuck Lever } 1096a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 109751971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1098a246b010SChuck Lever } while (desc.count); 10999903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1100a246b010SChuck Lever return len - desc.count; 1101a246b010SChuck Lever } 1102a246b010SChuck Lever 11039903cd1cSChuck Lever /** 11049903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 11059903cd1cSChuck Lever * @sk: socket with data to read 11069903cd1cSChuck Lever * @bytes: how much data to read 11079903cd1cSChuck Lever * 11089903cd1cSChuck Lever */ 11099903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1110a246b010SChuck Lever { 1111a246b010SChuck Lever struct rpc_xprt *xprt; 1112a246b010SChuck Lever read_descriptor_t rd_desc; 1113ff2d7db8STrond Myklebust int read; 1114a246b010SChuck Lever 11159903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 111646121cf7SChuck Lever 111746121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 11189903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1119a246b010SChuck Lever goto out; 1120a246b010SChuck Lever if (xprt->shutdown) 1121a246b010SChuck Lever goto out; 1122a246b010SChuck Lever 11239903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1124a246b010SChuck Lever rd_desc.arg.data = xprt; 1125ff2d7db8STrond Myklebust do { 1126a246b010SChuck Lever rd_desc.count = 65536; 1127ff2d7db8STrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1128ff2d7db8STrond Myklebust } while (read > 0); 1129a246b010SChuck Lever out: 1130a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1131a246b010SChuck Lever } 1132a246b010SChuck Lever 11339903cd1cSChuck Lever /** 11349903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 11359903cd1cSChuck Lever * @sk: socket whose state has changed 11369903cd1cSChuck Lever * 11379903cd1cSChuck Lever */ 11389903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1139a246b010SChuck Lever { 1140a246b010SChuck Lever struct rpc_xprt *xprt; 1141a246b010SChuck Lever 1142a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1143a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1144a246b010SChuck Lever goto out; 11459903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1146a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 1147a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1148a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1149a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 1150a246b010SChuck Lever 1151a246b010SChuck Lever switch (sk->sk_state) { 1152a246b010SChuck Lever case TCP_ESTABLISHED: 11534a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 1154a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 115551971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 115651971139SChuck Lever struct sock_xprt, xprt); 115751971139SChuck Lever 1158a246b010SChuck Lever /* Reset TCP record info */ 115951971139SChuck Lever transport->tcp_offset = 0; 116051971139SChuck Lever transport->tcp_reclen = 0; 116151971139SChuck Lever transport->tcp_copied = 0; 1162e136d092SChuck Lever transport->tcp_flags = 1163e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 116451971139SChuck Lever 11652a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1166a246b010SChuck Lever } 11674a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1168a246b010SChuck Lever break; 11693b948ae5STrond Myklebust case TCP_FIN_WAIT1: 11703b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 11717c1d71cfSTrond Myklebust xprt->connect_cookie++; 1172663b8858STrond Myklebust xprt->reestablish_timeout = 0; 11733b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 11743b948ae5STrond Myklebust smp_mb__before_clear_bit(); 11753b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1176ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 11773b948ae5STrond Myklebust smp_mb__after_clear_bit(); 1178a246b010SChuck Lever break; 1179632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 11803b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 118166af1e55STrond Myklebust xprt_force_disconnect(xprt); 1182663b8858STrond Myklebust case TCP_SYN_SENT: 11837c1d71cfSTrond Myklebust xprt->connect_cookie++; 1184663b8858STrond Myklebust case TCP_CLOSING: 1185663b8858STrond Myklebust /* 1186663b8858STrond Myklebust * If the server closed down the connection, make sure that 1187663b8858STrond Myklebust * we back off before reconnecting 1188663b8858STrond Myklebust */ 1189663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1190663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 11913b948ae5STrond Myklebust break; 11923b948ae5STrond Myklebust case TCP_LAST_ACK: 1193670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 11943b948ae5STrond Myklebust smp_mb__before_clear_bit(); 11953b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 11963b948ae5STrond Myklebust smp_mb__after_clear_bit(); 11973b948ae5STrond Myklebust break; 11983b948ae5STrond Myklebust case TCP_CLOSE: 11993b948ae5STrond Myklebust smp_mb__before_clear_bit(); 1200ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 12013b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 12023b948ae5STrond Myklebust smp_mb__after_clear_bit(); 12033b948ae5STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 120462da3b24STrond Myklebust xprt_disconnect_done(xprt); 1205a246b010SChuck Lever } 1206a246b010SChuck Lever out: 1207a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1208a246b010SChuck Lever } 1209a246b010SChuck Lever 12109903cd1cSChuck Lever /** 1211482f32e6STrond Myklebust * xs_error_report - callback mainly for catching socket errors 12122a9e1cfaSTrond Myklebust * @sk: socket 12132a9e1cfaSTrond Myklebust */ 1214482f32e6STrond Myklebust static void xs_error_report(struct sock *sk) 12152a9e1cfaSTrond Myklebust { 12162a9e1cfaSTrond Myklebust struct rpc_xprt *xprt; 12172a9e1cfaSTrond Myklebust 12182a9e1cfaSTrond Myklebust read_lock(&sk->sk_callback_lock); 12192a9e1cfaSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12202a9e1cfaSTrond Myklebust goto out; 12212a9e1cfaSTrond Myklebust dprintk("RPC: %s client %p...\n" 12222a9e1cfaSTrond Myklebust "RPC: error %d\n", 12232a9e1cfaSTrond Myklebust __func__, xprt, sk->sk_err); 1224482f32e6STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 12252a9e1cfaSTrond Myklebust out: 12262a9e1cfaSTrond Myklebust read_unlock(&sk->sk_callback_lock); 12272a9e1cfaSTrond Myklebust } 12282a9e1cfaSTrond Myklebust 12292a9e1cfaSTrond Myklebust /** 1230c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1231c7b2cae8SChuck Lever * becomes available 12329903cd1cSChuck Lever * @sk: socket whose state has changed 12339903cd1cSChuck Lever * 1234a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1235a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1236c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1237a246b010SChuck Lever * with a bunch of small requests. 1238a246b010SChuck Lever */ 1239c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1240a246b010SChuck Lever { 1241a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1242c7b2cae8SChuck Lever 1243c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 1244c7b2cae8SChuck Lever if (sock_writeable(sk)) { 1245c7b2cae8SChuck Lever struct socket *sock; 1246c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1247c7b2cae8SChuck Lever 1248c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1249a246b010SChuck Lever goto out; 1250b6ddf64fSTrond Myklebust clear_bit(SOCK_NOSPACE, &sock->flags); 1251b6ddf64fSTrond Myklebust 1252c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1253c7b2cae8SChuck Lever goto out; 1254b6ddf64fSTrond Myklebust if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 1255a246b010SChuck Lever goto out; 1256a246b010SChuck Lever 1257c7b2cae8SChuck Lever xprt_write_space(xprt); 1258a246b010SChuck Lever } 1259a246b010SChuck Lever 1260c7b2cae8SChuck Lever out: 1261c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1262c7b2cae8SChuck Lever } 1263c7b2cae8SChuck Lever 1264c7b2cae8SChuck Lever /** 1265c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1266c7b2cae8SChuck Lever * becomes available 1267c7b2cae8SChuck Lever * @sk: socket whose state has changed 1268c7b2cae8SChuck Lever * 1269c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1270c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1271c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1272c7b2cae8SChuck Lever * with a bunch of small requests. 1273c7b2cae8SChuck Lever */ 1274c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1275c7b2cae8SChuck Lever { 1276c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1277c7b2cae8SChuck Lever 1278c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 1279c7b2cae8SChuck Lever if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 1280c7b2cae8SChuck Lever struct socket *sock; 1281c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1282c7b2cae8SChuck Lever 1283c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1284c7b2cae8SChuck Lever goto out; 1285b6ddf64fSTrond Myklebust clear_bit(SOCK_NOSPACE, &sock->flags); 1286b6ddf64fSTrond Myklebust 1287c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1288c7b2cae8SChuck Lever goto out; 1289b6ddf64fSTrond Myklebust if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 1290a246b010SChuck Lever goto out; 1291a246b010SChuck Lever 1292c7b2cae8SChuck Lever xprt_write_space(xprt); 1293c7b2cae8SChuck Lever } 1294c7b2cae8SChuck Lever 1295a246b010SChuck Lever out: 1296a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1297a246b010SChuck Lever } 1298a246b010SChuck Lever 1299470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1300a246b010SChuck Lever { 1301ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1302ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1303a246b010SChuck Lever 13047c6e066eSChuck Lever if (transport->rcvsize) { 1305a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 13067c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1307a246b010SChuck Lever } 13087c6e066eSChuck Lever if (transport->sndsize) { 1309a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 13107c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1311a246b010SChuck Lever sk->sk_write_space(sk); 1312a246b010SChuck Lever } 1313a246b010SChuck Lever } 1314a246b010SChuck Lever 131543118c29SChuck Lever /** 1316470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 131743118c29SChuck Lever * @xprt: generic transport 1318470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1319470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 132043118c29SChuck Lever * 1321470056c2SChuck Lever * Set socket send and receive buffer size limits. 132243118c29SChuck Lever */ 1323470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 132443118c29SChuck Lever { 13257c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 13267c6e066eSChuck Lever 13277c6e066eSChuck Lever transport->sndsize = 0; 1328470056c2SChuck Lever if (sndsize) 13297c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 13307c6e066eSChuck Lever transport->rcvsize = 0; 1331470056c2SChuck Lever if (rcvsize) 13327c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1333470056c2SChuck Lever 1334470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 133543118c29SChuck Lever } 133643118c29SChuck Lever 133746c0ee8bSChuck Lever /** 133846c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 133946c0ee8bSChuck Lever * @task: task that timed out 134046c0ee8bSChuck Lever * 134146c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 134246c0ee8bSChuck Lever */ 134346c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 134446c0ee8bSChuck Lever { 134546c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 134646c0ee8bSChuck Lever } 134746c0ee8bSChuck Lever 1348b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1349b85d8806SChuck Lever { 1350b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1351b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1352b85d8806SChuck Lever return rand + xprt_min_resvport; 1353b85d8806SChuck Lever } 1354b85d8806SChuck Lever 135592200412SChuck Lever /** 135692200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 135792200412SChuck Lever * @xprt: generic transport 135892200412SChuck Lever * @port: new port number 135992200412SChuck Lever * 136092200412SChuck Lever */ 136192200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 136292200412SChuck Lever { 136395392c59SChuck Lever struct sockaddr *addr = xs_addr(xprt); 1364c4efcb1dSChuck Lever 136592200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1366c4efcb1dSChuck Lever 136720612005SChuck Lever switch (addr->sa_family) { 136820612005SChuck Lever case AF_INET: 136920612005SChuck Lever ((struct sockaddr_in *)addr)->sin_port = htons(port); 137020612005SChuck Lever break; 137120612005SChuck Lever case AF_INET6: 137220612005SChuck Lever ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); 137320612005SChuck Lever break; 137420612005SChuck Lever default: 137520612005SChuck Lever BUG(); 137620612005SChuck Lever } 137792200412SChuck Lever } 137892200412SChuck Lever 137967a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock) 138067a391d7STrond Myklebust { 138167a391d7STrond Myklebust unsigned short port = transport->port; 138267a391d7STrond Myklebust 138367a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 138467a391d7STrond Myklebust port = xs_get_random_port(); 138567a391d7STrond Myklebust return port; 138667a391d7STrond Myklebust } 138767a391d7STrond Myklebust 138867a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port) 138967a391d7STrond Myklebust { 139067a391d7STrond Myklebust if (transport->port != 0) 139167a391d7STrond Myklebust transport->port = 0; 139267a391d7STrond Myklebust if (!transport->xprt.resvport) 139367a391d7STrond Myklebust return 0; 139467a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 139567a391d7STrond Myklebust return xprt_max_resvport; 139667a391d7STrond Myklebust return --port; 139767a391d7STrond Myklebust } 139867a391d7STrond Myklebust 13997dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock) 1400a246b010SChuck Lever { 1401a246b010SChuck Lever struct sockaddr_in myaddr = { 1402a246b010SChuck Lever .sin_family = AF_INET, 1403a246b010SChuck Lever }; 1404d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 140567a391d7STrond Myklebust int err, nloop = 0; 140667a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 140767a391d7STrond Myklebust unsigned short last; 1408a246b010SChuck Lever 1409d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1410d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1411a246b010SChuck Lever do { 1412a246b010SChuck Lever myaddr.sin_port = htons(port); 1413e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1414a246b010SChuck Lever sizeof(myaddr)); 141567a391d7STrond Myklebust if (port == 0) 1416d3bc9a1dSFrank van Maarseveen break; 1417a246b010SChuck Lever if (err == 0) { 1418c8475461SChuck Lever transport->port = port; 1419d3bc9a1dSFrank van Maarseveen break; 1420a246b010SChuck Lever } 142167a391d7STrond Myklebust last = port; 142267a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 142367a391d7STrond Myklebust if (port > last) 142467a391d7STrond Myklebust nloop++; 142567a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 142621454aaaSHarvey Harrison dprintk("RPC: %s %pI4:%u: %s (%d)\n", 142721454aaaSHarvey Harrison __func__, &myaddr.sin_addr, 14287dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1429a246b010SChuck Lever return err; 1430a246b010SChuck Lever } 1431a246b010SChuck Lever 143290058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock) 143390058d37SChuck Lever { 143490058d37SChuck Lever struct sockaddr_in6 myaddr = { 143590058d37SChuck Lever .sin6_family = AF_INET6, 143690058d37SChuck Lever }; 143790058d37SChuck Lever struct sockaddr_in6 *sa; 143867a391d7STrond Myklebust int err, nloop = 0; 143967a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 144067a391d7STrond Myklebust unsigned short last; 144190058d37SChuck Lever 144290058d37SChuck Lever sa = (struct sockaddr_in6 *)&transport->addr; 144390058d37SChuck Lever myaddr.sin6_addr = sa->sin6_addr; 144490058d37SChuck Lever do { 144590058d37SChuck Lever myaddr.sin6_port = htons(port); 144690058d37SChuck Lever err = kernel_bind(sock, (struct sockaddr *) &myaddr, 144790058d37SChuck Lever sizeof(myaddr)); 144867a391d7STrond Myklebust if (port == 0) 144990058d37SChuck Lever break; 145090058d37SChuck Lever if (err == 0) { 145190058d37SChuck Lever transport->port = port; 145290058d37SChuck Lever break; 145390058d37SChuck Lever } 145467a391d7STrond Myklebust last = port; 145567a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 145667a391d7STrond Myklebust if (port > last) 145767a391d7STrond Myklebust nloop++; 145867a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 14595b095d98SHarvey Harrison dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", 1460fdb46ee7SHarvey Harrison &myaddr.sin6_addr, port, err ? "failed" : "ok", err); 1461a246b010SChuck Lever return err; 1462a246b010SChuck Lever } 1463a246b010SChuck Lever 1464ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1465ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1466ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1467ed07536eSPeter Zijlstra 14688945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1469ed07536eSPeter Zijlstra { 1470ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 14718945ee5eSChuck Lever 147202b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 14738945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 14748945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1475ed07536eSPeter Zijlstra } 14768945ee5eSChuck Lever 14778945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 14788945ee5eSChuck Lever { 14798945ee5eSChuck Lever struct sock *sk = sock->sk; 14808945ee5eSChuck Lever 1481f4921affSLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 14828945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 14838945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1484ed07536eSPeter Zijlstra } 1485ed07536eSPeter Zijlstra #else 14868945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 14878945ee5eSChuck Lever { 14888945ee5eSChuck Lever } 14898945ee5eSChuck Lever 14908945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1491ed07536eSPeter Zijlstra { 1492ed07536eSPeter Zijlstra } 1493ed07536eSPeter Zijlstra #endif 1494ed07536eSPeter Zijlstra 149516be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1496a246b010SChuck Lever { 149716be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1498edb267a6SChuck Lever 1499ee0ac0c2SChuck Lever if (!transport->inet) { 1500b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1501b0d93ad5SChuck Lever 1502b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1503b0d93ad5SChuck Lever 15042a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 15052a9e1cfaSTrond Myklebust 1506b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1507b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1508b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1509482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1510b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 1511b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 1512b0d93ad5SChuck Lever 1513b0d93ad5SChuck Lever xprt_set_connected(xprt); 1514b0d93ad5SChuck Lever 1515b0d93ad5SChuck Lever /* Reset to new socket */ 1516ee0ac0c2SChuck Lever transport->sock = sock; 1517ee0ac0c2SChuck Lever transport->inet = sk; 1518b0d93ad5SChuck Lever 1519b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1520b0d93ad5SChuck Lever } 1521470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 152216be2d20SChuck Lever } 152316be2d20SChuck Lever 1524a246b010SChuck Lever /** 15259c3d72deSChuck Lever * xs_udp_connect_worker4 - set up a UDP socket 1526a246b010SChuck Lever * @work: RPC transport to connect 1527a246b010SChuck Lever * 1528a246b010SChuck Lever * Invoked by a work queue tasklet. 1529a246b010SChuck Lever */ 15309c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work) 1531a246b010SChuck Lever { 1532a246b010SChuck Lever struct sock_xprt *transport = 1533a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1534a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1535a246b010SChuck Lever struct socket *sock = transport->sock; 1536a246b010SChuck Lever int err, status = -EIO; 1537a246b010SChuck Lever 153801d37c42STrond Myklebust if (xprt->shutdown) 15399903cd1cSChuck Lever goto out; 15409903cd1cSChuck Lever 1541a246b010SChuck Lever /* Start by resetting any existing state */ 1542fe315e76SChuck Lever xs_reset_transport(transport); 1543a246b010SChuck Lever 1544fe315e76SChuck Lever err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); 1545fe315e76SChuck Lever if (err < 0) { 15469903cd1cSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1547a246b010SChuck Lever goto out; 1548a246b010SChuck Lever } 15498945ee5eSChuck Lever xs_reclassify_socket4(sock); 1550a246b010SChuck Lever 15517dc753f0SChuck Lever if (xs_bind4(transport, sock)) { 15529903cd1cSChuck Lever sock_release(sock); 15539903cd1cSChuck Lever goto out; 1554a246b010SChuck Lever } 1555b0d93ad5SChuck Lever 1556b0d93ad5SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 1557b0d93ad5SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1558b0d93ad5SChuck Lever 155916be2d20SChuck Lever xs_udp_finish_connecting(xprt, sock); 1560a246b010SChuck Lever status = 0; 1561b0d93ad5SChuck Lever out: 1562b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 1563b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1564b0d93ad5SChuck Lever } 1565b0d93ad5SChuck Lever 156668e220bdSChuck Lever /** 156768e220bdSChuck Lever * xs_udp_connect_worker6 - set up a UDP socket 156868e220bdSChuck Lever * @work: RPC transport to connect 156968e220bdSChuck Lever * 157068e220bdSChuck Lever * Invoked by a work queue tasklet. 157168e220bdSChuck Lever */ 157268e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work) 157368e220bdSChuck Lever { 157468e220bdSChuck Lever struct sock_xprt *transport = 157568e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 157668e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 157768e220bdSChuck Lever struct socket *sock = transport->sock; 157868e220bdSChuck Lever int err, status = -EIO; 157968e220bdSChuck Lever 158001d37c42STrond Myklebust if (xprt->shutdown) 158168e220bdSChuck Lever goto out; 158268e220bdSChuck Lever 158368e220bdSChuck Lever /* Start by resetting any existing state */ 1584fe315e76SChuck Lever xs_reset_transport(transport); 158568e220bdSChuck Lever 1586fe315e76SChuck Lever err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); 1587fe315e76SChuck Lever if (err < 0) { 158868e220bdSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 158968e220bdSChuck Lever goto out; 159068e220bdSChuck Lever } 15918945ee5eSChuck Lever xs_reclassify_socket6(sock); 159268e220bdSChuck Lever 159368e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 159468e220bdSChuck Lever sock_release(sock); 159568e220bdSChuck Lever goto out; 159668e220bdSChuck Lever } 159768e220bdSChuck Lever 159868e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 159968e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 160068e220bdSChuck Lever 160168e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 1602a246b010SChuck Lever status = 0; 1603b0d93ad5SChuck Lever out: 1604b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 1605b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1606b0d93ad5SChuck Lever } 1607b0d93ad5SChuck Lever 16083167e12cSChuck Lever /* 16093167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 16103167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 16113167e12cSChuck Lever */ 161240d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 16133167e12cSChuck Lever { 16143167e12cSChuck Lever int result; 16153167e12cSChuck Lever struct sockaddr any; 16163167e12cSChuck Lever 16173167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 16183167e12cSChuck Lever 16193167e12cSChuck Lever /* 16203167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 16213167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 16223167e12cSChuck Lever */ 16233167e12cSChuck Lever memset(&any, 0, sizeof(any)); 16243167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1625ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 16263167e12cSChuck Lever if (result) 16273167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 16283167e12cSChuck Lever result); 16293167e12cSChuck Lever } 16303167e12cSChuck Lever 163140d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 163240d2549dSTrond Myklebust { 163340d2549dSTrond Myklebust unsigned int state = transport->inet->sk_state; 163440d2549dSTrond Myklebust 163540d2549dSTrond Myklebust if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) 163640d2549dSTrond Myklebust return; 163740d2549dSTrond Myklebust if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) 163840d2549dSTrond Myklebust return; 163940d2549dSTrond Myklebust xs_abort_connection(xprt, transport); 164040d2549dSTrond Myklebust } 164140d2549dSTrond Myklebust 164216be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1643b0d93ad5SChuck Lever { 164416be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1645edb267a6SChuck Lever 1646ee0ac0c2SChuck Lever if (!transport->inet) { 1647b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1648b0d93ad5SChuck Lever 1649b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1650b0d93ad5SChuck Lever 16512a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 16522a9e1cfaSTrond Myklebust 1653b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1654b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1655b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1656b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1657482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1658b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 16593167e12cSChuck Lever 16603167e12cSChuck Lever /* socket options */ 16613167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 16623167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 16633167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 16643167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1665b0d93ad5SChuck Lever 1666b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1667b0d93ad5SChuck Lever 1668b0d93ad5SChuck Lever /* Reset to new socket */ 1669ee0ac0c2SChuck Lever transport->sock = sock; 1670ee0ac0c2SChuck Lever transport->inet = sk; 1671b0d93ad5SChuck Lever 1672b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1673b0d93ad5SChuck Lever } 1674b0d93ad5SChuck Lever 167501d37c42STrond Myklebust if (!xprt_bound(xprt)) 167601d37c42STrond Myklebust return -ENOTCONN; 167701d37c42STrond Myklebust 1678b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1679262ca07dSChuck Lever xprt->stat.connect_count++; 1680262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 168195392c59SChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 168216be2d20SChuck Lever } 168316be2d20SChuck Lever 168416be2d20SChuck Lever /** 16859c3d72deSChuck Lever * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint 168616be2d20SChuck Lever * @work: RPC transport to connect 168716be2d20SChuck Lever * 168816be2d20SChuck Lever * Invoked by a work queue tasklet. 168916be2d20SChuck Lever */ 16909c3d72deSChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work) 169116be2d20SChuck Lever { 169216be2d20SChuck Lever struct sock_xprt *transport = 169316be2d20SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 169416be2d20SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 169516be2d20SChuck Lever struct socket *sock = transport->sock; 169616be2d20SChuck Lever int err, status = -EIO; 169716be2d20SChuck Lever 169801d37c42STrond Myklebust if (xprt->shutdown) 169916be2d20SChuck Lever goto out; 170016be2d20SChuck Lever 170116be2d20SChuck Lever if (!sock) { 170216be2d20SChuck Lever /* start from scratch */ 170316be2d20SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 170416be2d20SChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 170516be2d20SChuck Lever goto out; 170616be2d20SChuck Lever } 17078945ee5eSChuck Lever xs_reclassify_socket4(sock); 170816be2d20SChuck Lever 170916be2d20SChuck Lever if (xs_bind4(transport, sock) < 0) { 171016be2d20SChuck Lever sock_release(sock); 171116be2d20SChuck Lever goto out; 171216be2d20SChuck Lever } 171316be2d20SChuck Lever } else 171416be2d20SChuck Lever /* "close" the socket, preserving the local port */ 171540d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 171616be2d20SChuck Lever 171716be2d20SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 171816be2d20SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 171916be2d20SChuck Lever 172016be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 1721a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 172246121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 172346121cf7SChuck Lever sock->sk->sk_state); 1724a246b010SChuck Lever switch (status) { 1725*8a2cec29STrond Myklebust case -ECONNREFUSED: 1726*8a2cec29STrond Myklebust case -ECONNRESET: 1727*8a2cec29STrond Myklebust case -ENETUNREACH: 1728*8a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 17292a491991STrond Myklebust case 0: 1730a246b010SChuck Lever case -EINPROGRESS: 1731a246b010SChuck Lever case -EALREADY: 1732a246b010SChuck Lever goto out_clear; 1733*8a2cec29STrond Myklebust } 17343167e12cSChuck Lever /* get rid of existing socket, and retry */ 1735e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 17362a491991STrond Myklebust printk("%s: connect returned unhandled error %d\n", 17372a491991STrond Myklebust __func__, status); 17382a491991STrond Myklebust status = -EAGAIN; 1739a246b010SChuck Lever out: 174044fbac22SChuck Lever xprt_wake_pending_tasks(xprt, status); 1741a246b010SChuck Lever out_clear: 17422226feb6SChuck Lever xprt_clear_connecting(xprt); 1743a246b010SChuck Lever } 1744a246b010SChuck Lever 17459903cd1cSChuck Lever /** 174668e220bdSChuck Lever * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint 174768e220bdSChuck Lever * @work: RPC transport to connect 174868e220bdSChuck Lever * 174968e220bdSChuck Lever * Invoked by a work queue tasklet. 175068e220bdSChuck Lever */ 175168e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work) 175268e220bdSChuck Lever { 175368e220bdSChuck Lever struct sock_xprt *transport = 175468e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 175568e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 175668e220bdSChuck Lever struct socket *sock = transport->sock; 175768e220bdSChuck Lever int err, status = -EIO; 175868e220bdSChuck Lever 175901d37c42STrond Myklebust if (xprt->shutdown) 176068e220bdSChuck Lever goto out; 176168e220bdSChuck Lever 176268e220bdSChuck Lever if (!sock) { 176368e220bdSChuck Lever /* start from scratch */ 176468e220bdSChuck Lever if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 176568e220bdSChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 176668e220bdSChuck Lever goto out; 176768e220bdSChuck Lever } 17688945ee5eSChuck Lever xs_reclassify_socket6(sock); 176968e220bdSChuck Lever 177068e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 177168e220bdSChuck Lever sock_release(sock); 177268e220bdSChuck Lever goto out; 177368e220bdSChuck Lever } 177468e220bdSChuck Lever } else 177568e220bdSChuck Lever /* "close" the socket, preserving the local port */ 177640d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 177768e220bdSChuck Lever 177868e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 177968e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 178068e220bdSChuck Lever 178168e220bdSChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 178268e220bdSChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 178368e220bdSChuck Lever xprt, -status, xprt_connected(xprt), sock->sk->sk_state); 178468e220bdSChuck Lever switch (status) { 1785*8a2cec29STrond Myklebust case -ECONNREFUSED: 1786*8a2cec29STrond Myklebust case -ECONNRESET: 1787*8a2cec29STrond Myklebust case -ENETUNREACH: 1788*8a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 17892a491991STrond Myklebust case 0: 179068e220bdSChuck Lever case -EINPROGRESS: 179168e220bdSChuck Lever case -EALREADY: 179268e220bdSChuck Lever goto out_clear; 1793*8a2cec29STrond Myklebust } 179468e220bdSChuck Lever /* get rid of existing socket, and retry */ 1795e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 17962a491991STrond Myklebust printk("%s: connect returned unhandled error %d\n", 17972a491991STrond Myklebust __func__, status); 17982a491991STrond Myklebust status = -EAGAIN; 179968e220bdSChuck Lever out: 180068e220bdSChuck Lever xprt_wake_pending_tasks(xprt, status); 180168e220bdSChuck Lever out_clear: 180268e220bdSChuck Lever xprt_clear_connecting(xprt); 180368e220bdSChuck Lever } 180468e220bdSChuck Lever 180568e220bdSChuck Lever /** 18069903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 18079903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 18089903cd1cSChuck Lever * 18099903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 181003bf4b70SChuck Lever * 181103bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 181203bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 181303bf4b70SChuck Lever * socket on a privileged port. 181403bf4b70SChuck Lever * 181503bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 181603bf4b70SChuck Lever * retry floods (hard mounts). 18179903cd1cSChuck Lever */ 18189903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 1819a246b010SChuck Lever { 1820a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1821ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1822a246b010SChuck Lever 1823b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1824b0d93ad5SChuck Lever return; 1825b0d93ad5SChuck Lever 1826ee0ac0c2SChuck Lever if (transport->sock != NULL) { 182746121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 182846121cf7SChuck Lever "seconds\n", 182903bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 1830c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1831c1384c9cSTrond Myklebust &transport->connect_worker, 183203bf4b70SChuck Lever xprt->reestablish_timeout); 183303bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 183403bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 183503bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 18369903cd1cSChuck Lever } else { 18379903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 1838c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1839c1384c9cSTrond Myklebust &transport->connect_worker, 0); 1840a246b010SChuck Lever } 1841a246b010SChuck Lever } 1842a246b010SChuck Lever 1843e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task) 1844e06799f9STrond Myklebust { 1845e06799f9STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1846e06799f9STrond Myklebust 1847e06799f9STrond Myklebust /* Exit if we need to wait for socket shutdown to complete */ 1848e06799f9STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 1849e06799f9STrond Myklebust return; 1850e06799f9STrond Myklebust xs_connect(task); 1851e06799f9STrond Myklebust } 1852e06799f9STrond Myklebust 1853262ca07dSChuck Lever /** 1854262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 1855262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1856262ca07dSChuck Lever * @seq: output file 1857262ca07dSChuck Lever * 1858262ca07dSChuck Lever */ 1859262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1860262ca07dSChuck Lever { 1861c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1862c8475461SChuck Lever 1863262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 1864c8475461SChuck Lever transport->port, 1865262ca07dSChuck Lever xprt->stat.bind_count, 1866262ca07dSChuck Lever xprt->stat.sends, 1867262ca07dSChuck Lever xprt->stat.recvs, 1868262ca07dSChuck Lever xprt->stat.bad_xids, 1869262ca07dSChuck Lever xprt->stat.req_u, 1870262ca07dSChuck Lever xprt->stat.bklog_u); 1871262ca07dSChuck Lever } 1872262ca07dSChuck Lever 1873262ca07dSChuck Lever /** 1874262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 1875262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1876262ca07dSChuck Lever * @seq: output file 1877262ca07dSChuck Lever * 1878262ca07dSChuck Lever */ 1879262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1880262ca07dSChuck Lever { 1881c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1882262ca07dSChuck Lever long idle_time = 0; 1883262ca07dSChuck Lever 1884262ca07dSChuck Lever if (xprt_connected(xprt)) 1885262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 1886262ca07dSChuck Lever 1887262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 1888c8475461SChuck Lever transport->port, 1889262ca07dSChuck Lever xprt->stat.bind_count, 1890262ca07dSChuck Lever xprt->stat.connect_count, 1891262ca07dSChuck Lever xprt->stat.connect_time, 1892262ca07dSChuck Lever idle_time, 1893262ca07dSChuck Lever xprt->stat.sends, 1894262ca07dSChuck Lever xprt->stat.recvs, 1895262ca07dSChuck Lever xprt->stat.bad_xids, 1896262ca07dSChuck Lever xprt->stat.req_u, 1897262ca07dSChuck Lever xprt->stat.bklog_u); 1898262ca07dSChuck Lever } 1899262ca07dSChuck Lever 1900262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 190143118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 190212a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 190349e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 190445160d62SChuck Lever .rpcbind = rpcb_getport_async, 190592200412SChuck Lever .set_port = xs_set_port, 19069903cd1cSChuck Lever .connect = xs_connect, 190702107148SChuck Lever .buf_alloc = rpc_malloc, 190802107148SChuck Lever .buf_free = rpc_free, 1909262965f5SChuck Lever .send_request = xs_udp_send_request, 1910fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 191146c0ee8bSChuck Lever .timer = xs_udp_timer, 1912a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 1913262965f5SChuck Lever .close = xs_close, 1914262965f5SChuck Lever .destroy = xs_destroy, 1915262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 1916262965f5SChuck Lever }; 1917262965f5SChuck Lever 1918262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 191912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 1920e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 192145160d62SChuck Lever .rpcbind = rpcb_getport_async, 192292200412SChuck Lever .set_port = xs_set_port, 1923e06799f9STrond Myklebust .connect = xs_tcp_connect, 192402107148SChuck Lever .buf_alloc = rpc_malloc, 192502107148SChuck Lever .buf_free = rpc_free, 1926262965f5SChuck Lever .send_request = xs_tcp_send_request, 1927fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 1928e06799f9STrond Myklebust .close = xs_tcp_shutdown, 19299903cd1cSChuck Lever .destroy = xs_destroy, 1930262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 1931a246b010SChuck Lever }; 1932a246b010SChuck Lever 19333c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 1934bc25571eS\"Talpey, Thomas\ unsigned int slot_table_size) 1935c8541ecdSChuck Lever { 1936c8541ecdSChuck Lever struct rpc_xprt *xprt; 1937ffc2e518SChuck Lever struct sock_xprt *new; 1938c8541ecdSChuck Lever 193996802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 1940c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 1941c8541ecdSChuck Lever return ERR_PTR(-EBADF); 1942c8541ecdSChuck Lever } 1943c8541ecdSChuck Lever 1944ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 1945ffc2e518SChuck Lever if (new == NULL) { 194646121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 194746121cf7SChuck Lever "rpc_xprt\n"); 1948c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1949c8541ecdSChuck Lever } 1950ffc2e518SChuck Lever xprt = &new->xprt; 1951c8541ecdSChuck Lever 1952c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 1953c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 1954c8541ecdSChuck Lever if (xprt->slot == NULL) { 1955c8541ecdSChuck Lever kfree(xprt); 195646121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 195746121cf7SChuck Lever "table\n"); 1958c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1959c8541ecdSChuck Lever } 1960c8541ecdSChuck Lever 196196802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 196296802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 1963d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 1964d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 1965c8541ecdSChuck Lever 1966c8541ecdSChuck Lever return xprt; 1967c8541ecdSChuck Lever } 1968c8541ecdSChuck Lever 19692881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 19702881ae74STrond Myklebust .to_initval = 5 * HZ, 19712881ae74STrond Myklebust .to_maxval = 30 * HZ, 19722881ae74STrond Myklebust .to_increment = 5 * HZ, 19732881ae74STrond Myklebust .to_retries = 5, 19742881ae74STrond Myklebust }; 19752881ae74STrond Myklebust 19769903cd1cSChuck Lever /** 19779903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 197896802a09SFrank van Maarseveen * @args: rpc transport creation arguments 19799903cd1cSChuck Lever * 19809903cd1cSChuck Lever */ 1981483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 1982a246b010SChuck Lever { 19838f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 1984c8541ecdSChuck Lever struct rpc_xprt *xprt; 1985c8475461SChuck Lever struct sock_xprt *transport; 1986a246b010SChuck Lever 198796802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 1988c8541ecdSChuck Lever if (IS_ERR(xprt)) 1989c8541ecdSChuck Lever return xprt; 1990c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 1991a246b010SChuck Lever 1992ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 1993808012fbSChuck Lever xprt->tsh_size = 0; 1994a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 1995a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 1996a246b010SChuck Lever 199703bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 199803bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 199903bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 200003bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2001a246b010SChuck Lever 2002262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2003a246b010SChuck Lever 2004ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2005a246b010SChuck Lever 20068f9d5b1aSChuck Lever switch (addr->sa_family) { 20078f9d5b1aSChuck Lever case AF_INET: 20088f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 20098f9d5b1aSChuck Lever xprt_set_bound(xprt); 20108f9d5b1aSChuck Lever 20118f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 20128f9d5b1aSChuck Lever xs_udp_connect_worker4); 2013b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 20148f9d5b1aSChuck Lever break; 20158f9d5b1aSChuck Lever case AF_INET6: 20168f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 20178f9d5b1aSChuck Lever xprt_set_bound(xprt); 20188f9d5b1aSChuck Lever 20198f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 20208f9d5b1aSChuck Lever xs_udp_connect_worker6); 2021b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 20228f9d5b1aSChuck Lever break; 20238f9d5b1aSChuck Lever default: 20248f9d5b1aSChuck Lever kfree(xprt); 20258f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 20268f9d5b1aSChuck Lever } 20278f9d5b1aSChuck Lever 2028edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 20297559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2030edb267a6SChuck Lever 2031bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2032c8541ecdSChuck Lever return xprt; 2033bc25571eS\"Talpey, Thomas\ 2034bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2035bc25571eS\"Talpey, Thomas\ kfree(xprt); 2036bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2037a246b010SChuck Lever } 2038a246b010SChuck Lever 20392881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 20402881ae74STrond Myklebust .to_initval = 60 * HZ, 20412881ae74STrond Myklebust .to_maxval = 60 * HZ, 20422881ae74STrond Myklebust .to_retries = 2, 20432881ae74STrond Myklebust }; 20442881ae74STrond Myklebust 20459903cd1cSChuck Lever /** 20469903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 204796802a09SFrank van Maarseveen * @args: rpc transport creation arguments 20489903cd1cSChuck Lever * 20499903cd1cSChuck Lever */ 2050483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2051a246b010SChuck Lever { 20528f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2053c8541ecdSChuck Lever struct rpc_xprt *xprt; 2054c8475461SChuck Lever struct sock_xprt *transport; 2055a246b010SChuck Lever 205696802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 2057c8541ecdSChuck Lever if (IS_ERR(xprt)) 2058c8541ecdSChuck Lever return xprt; 2059c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2060a246b010SChuck Lever 2061ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2062808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2063808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2064a246b010SChuck Lever 206503bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 206603bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 206703bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 206803bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2069a246b010SChuck Lever 2070262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2071ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2072a246b010SChuck Lever 20738f9d5b1aSChuck Lever switch (addr->sa_family) { 20748f9d5b1aSChuck Lever case AF_INET: 20758f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 20768f9d5b1aSChuck Lever xprt_set_bound(xprt); 20778f9d5b1aSChuck Lever 20788f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); 2079b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 20808f9d5b1aSChuck Lever break; 20818f9d5b1aSChuck Lever case AF_INET6: 20828f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 20838f9d5b1aSChuck Lever xprt_set_bound(xprt); 20848f9d5b1aSChuck Lever 20858f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); 2086b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 20878f9d5b1aSChuck Lever break; 20888f9d5b1aSChuck Lever default: 20898f9d5b1aSChuck Lever kfree(xprt); 20908f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 20918f9d5b1aSChuck Lever } 20928f9d5b1aSChuck Lever 2093edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 20947559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2095edb267a6SChuck Lever 2096bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2097c8541ecdSChuck Lever return xprt; 2098bc25571eS\"Talpey, Thomas\ 2099bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2100bc25571eS\"Talpey, Thomas\ kfree(xprt); 2101bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2102a246b010SChuck Lever } 2103282b32e1SChuck Lever 2104bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 2105bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 2106bc25571eS\"Talpey, Thomas\ .name = "udp", 2107bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21084fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_UDP, 2109bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 2110bc25571eS\"Talpey, Thomas\ }; 2111bc25571eS\"Talpey, Thomas\ 2112bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 2113bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 2114bc25571eS\"Talpey, Thomas\ .name = "tcp", 2115bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21164fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_TCP, 2117bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 2118bc25571eS\"Talpey, Thomas\ }; 2119bc25571eS\"Talpey, Thomas\ 2120282b32e1SChuck Lever /** 2121bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 2122282b32e1SChuck Lever * 2123282b32e1SChuck Lever */ 2124282b32e1SChuck Lever int init_socket_xprt(void) 2125282b32e1SChuck Lever { 2126fbf76683SChuck Lever #ifdef RPC_DEBUG 21272b1bec5fSEric W. Biederman if (!sunrpc_table_header) 21280b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 2129fbf76683SChuck Lever #endif 2130fbf76683SChuck Lever 2131bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 2132bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 2133bc25571eS\"Talpey, Thomas\ 2134282b32e1SChuck Lever return 0; 2135282b32e1SChuck Lever } 2136282b32e1SChuck Lever 2137282b32e1SChuck Lever /** 2138bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 2139282b32e1SChuck Lever * 2140282b32e1SChuck Lever */ 2141282b32e1SChuck Lever void cleanup_socket_xprt(void) 2142282b32e1SChuck Lever { 2143fbf76683SChuck Lever #ifdef RPC_DEBUG 2144fbf76683SChuck Lever if (sunrpc_table_header) { 2145fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 2146fbf76683SChuck Lever sunrpc_table_header = NULL; 2147fbf76683SChuck Lever } 2148fbf76683SChuck Lever #endif 2149bc25571eS\"Talpey, Thomas\ 2150bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 2151bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 2152282b32e1SChuck Lever } 2153