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 52*7d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 53*7d1e8255STrond Myklebust 54c556b754SChuck Lever /* 55fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 56fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 57fbf76683SChuck Lever * directory become the union of all files registered there. 58fbf76683SChuck Lever * 59fbf76683SChuck Lever * We simply need to make sure that we don't collide with 60fbf76683SChuck Lever * someone else's file names! 61fbf76683SChuck Lever */ 62fbf76683SChuck Lever 63fbf76683SChuck Lever #ifdef RPC_DEBUG 64fbf76683SChuck Lever 65fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 66fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 67fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 68fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 69fbf76683SChuck Lever 70fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 71fbf76683SChuck Lever 72fbf76683SChuck Lever /* 73fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 74fbf76683SChuck Lever * socket buffers for existing UDP transports 75fbf76683SChuck Lever */ 76fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 77fbf76683SChuck Lever { 78fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_UDP, 79fbf76683SChuck Lever .procname = "udp_slot_table_entries", 80fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 81fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 82fbf76683SChuck Lever .mode = 0644, 83fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 84fbf76683SChuck Lever .strategy = &sysctl_intvec, 85fbf76683SChuck Lever .extra1 = &min_slot_table_size, 86fbf76683SChuck Lever .extra2 = &max_slot_table_size 87fbf76683SChuck Lever }, 88fbf76683SChuck Lever { 89fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_TCP, 90fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 91fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 92fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 93fbf76683SChuck Lever .mode = 0644, 94fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 95fbf76683SChuck Lever .strategy = &sysctl_intvec, 96fbf76683SChuck Lever .extra1 = &min_slot_table_size, 97fbf76683SChuck Lever .extra2 = &max_slot_table_size 98fbf76683SChuck Lever }, 99fbf76683SChuck Lever { 100fbf76683SChuck Lever .ctl_name = CTL_MIN_RESVPORT, 101fbf76683SChuck Lever .procname = "min_resvport", 102fbf76683SChuck Lever .data = &xprt_min_resvport, 103fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 104fbf76683SChuck Lever .mode = 0644, 105fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 106fbf76683SChuck Lever .strategy = &sysctl_intvec, 107fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 108fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 109fbf76683SChuck Lever }, 110fbf76683SChuck Lever { 111fbf76683SChuck Lever .ctl_name = CTL_MAX_RESVPORT, 112fbf76683SChuck Lever .procname = "max_resvport", 113fbf76683SChuck Lever .data = &xprt_max_resvport, 114fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 115fbf76683SChuck Lever .mode = 0644, 116fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 117fbf76683SChuck Lever .strategy = &sysctl_intvec, 118fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 119fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 120fbf76683SChuck Lever }, 121fbf76683SChuck Lever { 122fbf76683SChuck Lever .ctl_name = 0, 123fbf76683SChuck Lever }, 124fbf76683SChuck Lever }; 125fbf76683SChuck Lever 126fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 127fbf76683SChuck Lever { 128fbf76683SChuck Lever .ctl_name = CTL_SUNRPC, 129fbf76683SChuck Lever .procname = "sunrpc", 130fbf76683SChuck Lever .mode = 0555, 131fbf76683SChuck Lever .child = xs_tunables_table 132fbf76683SChuck Lever }, 133fbf76683SChuck Lever { 134fbf76683SChuck Lever .ctl_name = 0, 135fbf76683SChuck Lever }, 136fbf76683SChuck Lever }; 137fbf76683SChuck Lever 138fbf76683SChuck Lever #endif 139fbf76683SChuck Lever 140fbf76683SChuck Lever /* 14103bf4b70SChuck Lever * Time out for an RPC UDP socket connect. UDP socket connects are 14203bf4b70SChuck Lever * synchronous, but we set a timeout anyway in case of resource 14303bf4b70SChuck Lever * exhaustion on the local host. 14403bf4b70SChuck Lever */ 14503bf4b70SChuck Lever #define XS_UDP_CONN_TO (5U * HZ) 14603bf4b70SChuck Lever 14703bf4b70SChuck Lever /* 14803bf4b70SChuck Lever * Wait duration for an RPC TCP connection to be established. Solaris 14903bf4b70SChuck Lever * NFS over TCP uses 60 seconds, for example, which is in line with how 15003bf4b70SChuck Lever * long a server takes to reboot. 15103bf4b70SChuck Lever */ 15203bf4b70SChuck Lever #define XS_TCP_CONN_TO (60U * HZ) 15303bf4b70SChuck Lever 15403bf4b70SChuck Lever /* 15503bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 15603bf4b70SChuck Lever */ 15703bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 15803bf4b70SChuck Lever 15903bf4b70SChuck Lever /* 16003bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16103bf4b70SChuck Lever * kind of resource problem on the local host. 16203bf4b70SChuck Lever */ 16303bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 16403bf4b70SChuck Lever 16503bf4b70SChuck Lever /* 16603bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 16703bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 16803bf4b70SChuck Lever * 16903bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17003bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17103bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17203bf4b70SChuck Lever * increase over time if the server is down or not responding. 17303bf4b70SChuck Lever */ 17403bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 17503bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 17603bf4b70SChuck Lever 17703bf4b70SChuck Lever /* 17803bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 17903bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18003bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18103bf4b70SChuck Lever */ 18203bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18303bf4b70SChuck Lever 184a246b010SChuck Lever #ifdef RPC_DEBUG 185a246b010SChuck Lever # undef RPC_DEBUG_DATA 1869903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 187a246b010SChuck Lever #endif 188a246b010SChuck Lever 189a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1909903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 191a246b010SChuck Lever { 192a246b010SChuck Lever u8 *buf = (u8 *) packet; 193a246b010SChuck Lever int j; 194a246b010SChuck Lever 195a246b010SChuck Lever dprintk("RPC: %s\n", msg); 196a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 197a246b010SChuck Lever if (!(j & 31)) { 198a246b010SChuck Lever if (j) 199a246b010SChuck Lever dprintk("\n"); 200a246b010SChuck Lever dprintk("0x%04x ", j); 201a246b010SChuck Lever } 202a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 203a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 204a246b010SChuck Lever } 205a246b010SChuck Lever dprintk("\n"); 206a246b010SChuck Lever } 207a246b010SChuck Lever #else 2089903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 209a246b010SChuck Lever { 210a246b010SChuck Lever /* NOP */ 211a246b010SChuck Lever } 212a246b010SChuck Lever #endif 213a246b010SChuck Lever 214ffc2e518SChuck Lever struct sock_xprt { 215ffc2e518SChuck Lever struct rpc_xprt xprt; 216ee0ac0c2SChuck Lever 217ee0ac0c2SChuck Lever /* 218ee0ac0c2SChuck Lever * Network layer 219ee0ac0c2SChuck Lever */ 220ee0ac0c2SChuck Lever struct socket * sock; 221ee0ac0c2SChuck Lever struct sock * inet; 22251971139SChuck Lever 22351971139SChuck Lever /* 22451971139SChuck Lever * State of TCP reply receive 22551971139SChuck Lever */ 22651971139SChuck Lever __be32 tcp_fraghdr, 22751971139SChuck Lever tcp_xid; 22851971139SChuck Lever 22951971139SChuck Lever u32 tcp_offset, 23051971139SChuck Lever tcp_reclen; 23151971139SChuck Lever 23251971139SChuck Lever unsigned long tcp_copied, 23351971139SChuck Lever tcp_flags; 234c8475461SChuck Lever 235c8475461SChuck Lever /* 236c8475461SChuck Lever * Connection of transports 237c8475461SChuck Lever */ 23834161db6STrond Myklebust struct delayed_work connect_worker; 239d3bc9a1dSFrank van Maarseveen struct sockaddr_storage addr; 240c8475461SChuck Lever unsigned short port; 2417c6e066eSChuck Lever 2427c6e066eSChuck Lever /* 2437c6e066eSChuck Lever * UDP socket buffer size parameters 2447c6e066eSChuck Lever */ 2457c6e066eSChuck Lever size_t rcvsize, 2467c6e066eSChuck Lever sndsize; 247314dfd79SChuck Lever 248314dfd79SChuck Lever /* 249314dfd79SChuck Lever * Saved socket callback addresses 250314dfd79SChuck Lever */ 251314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 252314dfd79SChuck Lever void (*old_state_change)(struct sock *); 253314dfd79SChuck Lever void (*old_write_space)(struct sock *); 2542a9e1cfaSTrond Myklebust void (*old_error_report)(struct sock *); 255ffc2e518SChuck Lever }; 256ffc2e518SChuck Lever 257e136d092SChuck Lever /* 258e136d092SChuck Lever * TCP receive state flags 259e136d092SChuck Lever */ 260e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 261e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 262e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 263e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 264e136d092SChuck Lever 26595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 266edb267a6SChuck Lever { 26795392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 26895392c59SChuck Lever } 26995392c59SChuck Lever 27095392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 27195392c59SChuck Lever { 27295392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 27395392c59SChuck Lever } 27495392c59SChuck Lever 27595392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 27695392c59SChuck Lever { 27795392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 27895392c59SChuck Lever } 27995392c59SChuck Lever 280b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt, 281b454ae90SChuck Lever const char *protocol, 282b454ae90SChuck Lever const char *netid) 283edb267a6SChuck Lever { 28495392c59SChuck Lever struct sockaddr_in *addr = xs_addr_in(xprt); 285edb267a6SChuck Lever char *buf; 286edb267a6SChuck Lever 287edb267a6SChuck Lever buf = kzalloc(20, GFP_KERNEL); 288edb267a6SChuck Lever if (buf) { 289e0db4a78SDavid S. Miller snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr); 290edb267a6SChuck Lever } 291edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 292edb267a6SChuck Lever 293edb267a6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 294edb267a6SChuck Lever if (buf) { 295edb267a6SChuck Lever snprintf(buf, 8, "%u", 296edb267a6SChuck Lever ntohs(addr->sin_port)); 297edb267a6SChuck Lever } 298edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 299edb267a6SChuck Lever 300b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 301edb267a6SChuck Lever 302edb267a6SChuck Lever buf = kzalloc(48, GFP_KERNEL); 303edb267a6SChuck Lever if (buf) { 30421454aaaSHarvey Harrison snprintf(buf, 48, "addr=%pI4 port=%u proto=%s", 30521454aaaSHarvey Harrison &addr->sin_addr.s_addr, 306edb267a6SChuck Lever ntohs(addr->sin_port), 307b454ae90SChuck Lever protocol); 308edb267a6SChuck Lever } 309edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 310fbfe3cc6SChuck Lever 311fbfe3cc6SChuck Lever buf = kzalloc(10, GFP_KERNEL); 312fbfe3cc6SChuck Lever if (buf) { 313fbfe3cc6SChuck Lever snprintf(buf, 10, "%02x%02x%02x%02x", 314fbfe3cc6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 315fbfe3cc6SChuck Lever } 316fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 317fbfe3cc6SChuck Lever 318fbfe3cc6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 319fbfe3cc6SChuck Lever if (buf) { 320fbfe3cc6SChuck Lever snprintf(buf, 8, "%4hx", 321fbfe3cc6SChuck Lever ntohs(addr->sin_port)); 322fbfe3cc6SChuck Lever } 323fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 324756805e7SChuck Lever 325756805e7SChuck Lever buf = kzalloc(30, GFP_KERNEL); 326756805e7SChuck Lever if (buf) { 32721454aaaSHarvey Harrison snprintf(buf, 30, "%pI4.%u.%u", 32821454aaaSHarvey Harrison &addr->sin_addr.s_addr, 329756805e7SChuck Lever ntohs(addr->sin_port) >> 8, 330756805e7SChuck Lever ntohs(addr->sin_port) & 0xff); 331756805e7SChuck Lever } 332756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3334417c8c4S\"Talpey, Thomas\ 334b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 335edb267a6SChuck Lever } 336edb267a6SChuck Lever 337b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt, 338b454ae90SChuck Lever const char *protocol, 339b454ae90SChuck Lever const char *netid) 3404b6473fbSChuck Lever { 34195392c59SChuck Lever struct sockaddr_in6 *addr = xs_addr_in6(xprt); 3424b6473fbSChuck Lever char *buf; 3434b6473fbSChuck Lever 3444b6473fbSChuck Lever buf = kzalloc(40, GFP_KERNEL); 3454b6473fbSChuck Lever if (buf) { 3465b095d98SHarvey Harrison snprintf(buf, 40, "%pI6",&addr->sin6_addr); 3474b6473fbSChuck Lever } 3484b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 3494b6473fbSChuck Lever 3504b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3514b6473fbSChuck Lever if (buf) { 3524b6473fbSChuck Lever snprintf(buf, 8, "%u", 3534b6473fbSChuck Lever ntohs(addr->sin6_port)); 3544b6473fbSChuck Lever } 3554b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 3564b6473fbSChuck Lever 357b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 3584b6473fbSChuck Lever 3594b6473fbSChuck Lever buf = kzalloc(64, GFP_KERNEL); 3604b6473fbSChuck Lever if (buf) { 3615b095d98SHarvey Harrison snprintf(buf, 64, "addr=%pI6 port=%u proto=%s", 362fdb46ee7SHarvey Harrison &addr->sin6_addr, 3634b6473fbSChuck Lever ntohs(addr->sin6_port), 364b454ae90SChuck Lever protocol); 3654b6473fbSChuck Lever } 3664b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 3674b6473fbSChuck Lever 3684b6473fbSChuck Lever buf = kzalloc(36, GFP_KERNEL); 369b071195dSHarvey Harrison if (buf) 3704b7a4274SHarvey Harrison snprintf(buf, 36, "%pi6", &addr->sin6_addr); 371b071195dSHarvey Harrison 3724b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 3734b6473fbSChuck Lever 3744b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3754b6473fbSChuck Lever if (buf) { 3764b6473fbSChuck Lever snprintf(buf, 8, "%4hx", 3774b6473fbSChuck Lever ntohs(addr->sin6_port)); 3784b6473fbSChuck Lever } 3794b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 380756805e7SChuck Lever 381756805e7SChuck Lever buf = kzalloc(50, GFP_KERNEL); 382756805e7SChuck Lever if (buf) { 3835b095d98SHarvey Harrison snprintf(buf, 50, "%pI6.%u.%u", 384fdb46ee7SHarvey Harrison &addr->sin6_addr, 385756805e7SChuck Lever ntohs(addr->sin6_port) >> 8, 386756805e7SChuck Lever ntohs(addr->sin6_port) & 0xff); 387756805e7SChuck Lever } 388756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3894417c8c4S\"Talpey, Thomas\ 390b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 391edb267a6SChuck Lever } 392edb267a6SChuck Lever 393edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 394edb267a6SChuck Lever { 39533e01dc7SChuck Lever unsigned int i; 39633e01dc7SChuck Lever 39733e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 39833e01dc7SChuck Lever switch (i) { 39933e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 40033e01dc7SChuck Lever case RPC_DISPLAY_NETID: 40133e01dc7SChuck Lever continue; 40233e01dc7SChuck Lever default: 40333e01dc7SChuck Lever kfree(xprt->address_strings[i]); 40433e01dc7SChuck Lever } 405edb267a6SChuck Lever } 406edb267a6SChuck Lever 407b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 408b4b5cc85SChuck Lever 40924c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 410b4b5cc85SChuck Lever { 411b4b5cc85SChuck Lever struct msghdr msg = { 412b4b5cc85SChuck Lever .msg_name = addr, 413b4b5cc85SChuck Lever .msg_namelen = addrlen, 41424c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 41524c5684bSTrond Myklebust }; 41624c5684bSTrond Myklebust struct kvec iov = { 41724c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 41824c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 419b4b5cc85SChuck Lever }; 420b4b5cc85SChuck Lever 42124c5684bSTrond Myklebust if (iov.iov_len != 0) 422b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 423b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 424b4b5cc85SChuck Lever } 425b4b5cc85SChuck Lever 42624c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 427b4b5cc85SChuck Lever { 42824c5684bSTrond Myklebust struct page **ppage; 42924c5684bSTrond Myklebust unsigned int remainder; 43024c5684bSTrond Myklebust int err, sent = 0; 431b4b5cc85SChuck Lever 43224c5684bSTrond Myklebust remainder = xdr->page_len - base; 43324c5684bSTrond Myklebust base += xdr->page_base; 43424c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 43524c5684bSTrond Myklebust base &= ~PAGE_MASK; 43624c5684bSTrond Myklebust for(;;) { 43724c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 43824c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 43924c5684bSTrond Myklebust 44024c5684bSTrond Myklebust remainder -= len; 44124c5684bSTrond Myklebust if (remainder != 0 || more) 44224c5684bSTrond Myklebust flags |= MSG_MORE; 44324c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 44424c5684bSTrond Myklebust if (remainder == 0 || err != len) 44524c5684bSTrond Myklebust break; 44624c5684bSTrond Myklebust sent += err; 44724c5684bSTrond Myklebust ppage++; 44824c5684bSTrond Myklebust base = 0; 44924c5684bSTrond Myklebust } 45024c5684bSTrond Myklebust if (sent == 0) 45124c5684bSTrond Myklebust return err; 45224c5684bSTrond Myklebust if (err > 0) 45324c5684bSTrond Myklebust sent += err; 45424c5684bSTrond Myklebust return sent; 455b4b5cc85SChuck Lever } 456b4b5cc85SChuck Lever 4579903cd1cSChuck Lever /** 4589903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 4599903cd1cSChuck Lever * @sock: socket to send on 4609903cd1cSChuck Lever * @addr: UDP only -- address of destination 4619903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 4629903cd1cSChuck Lever * @xdr: buffer containing this request 4639903cd1cSChuck Lever * @base: starting position in the buffer 4649903cd1cSChuck Lever * 465a246b010SChuck Lever */ 46624c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 467a246b010SChuck Lever { 46824c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 46924c5684bSTrond Myklebust int err, sent = 0; 470a246b010SChuck Lever 471262965f5SChuck Lever if (unlikely(!sock)) 472fba91afbSTrond Myklebust return -ENOTSOCK; 473262965f5SChuck Lever 474262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 47524c5684bSTrond Myklebust if (base != 0) { 47624c5684bSTrond Myklebust addr = NULL; 47724c5684bSTrond Myklebust addrlen = 0; 47824c5684bSTrond Myklebust } 479262965f5SChuck Lever 48024c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 48124c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 48224c5684bSTrond Myklebust remainder -= len; 48324c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 48424c5684bSTrond Myklebust if (remainder == 0 || err != len) 485a246b010SChuck Lever goto out; 48624c5684bSTrond Myklebust sent += err; 487a246b010SChuck Lever base = 0; 488a246b010SChuck Lever } else 48924c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 490a246b010SChuck Lever 49124c5684bSTrond Myklebust if (base < xdr->page_len) { 49224c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 49324c5684bSTrond Myklebust remainder -= len; 49424c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 49524c5684bSTrond Myklebust if (remainder == 0 || err != len) 496a246b010SChuck Lever goto out; 49724c5684bSTrond Myklebust sent += err; 498a246b010SChuck Lever base = 0; 49924c5684bSTrond Myklebust } else 50024c5684bSTrond Myklebust base -= xdr->page_len; 50124c5684bSTrond Myklebust 50224c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 50324c5684bSTrond Myklebust return sent; 50424c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 505a246b010SChuck Lever out: 50624c5684bSTrond Myklebust if (sent == 0) 50724c5684bSTrond Myklebust return err; 50824c5684bSTrond Myklebust if (err > 0) 50924c5684bSTrond Myklebust sent += err; 51024c5684bSTrond Myklebust return sent; 511a246b010SChuck Lever } 512a246b010SChuck Lever 513b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 514b6ddf64fSTrond Myklebust { 515b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 516b6ddf64fSTrond Myklebust 517b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 518b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 519b6ddf64fSTrond Myklebust } 520b6ddf64fSTrond Myklebust 5219903cd1cSChuck Lever /** 522262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 523262965f5SChuck Lever * @task: task to put to sleep 5249903cd1cSChuck Lever * 525a246b010SChuck Lever */ 5265e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 527a246b010SChuck Lever { 528262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 529262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 530ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 5315e3771ceSTrond Myklebust int ret = 0; 532a246b010SChuck Lever 53346121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 534262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 535262965f5SChuck Lever req->rq_slen); 536a246b010SChuck Lever 537262965f5SChuck Lever /* Protect against races with write_space */ 538262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 539a246b010SChuck Lever 540262965f5SChuck Lever /* Don't race with disconnect */ 541b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 542b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 5435e3771ceSTrond Myklebust ret = -EAGAIN; 544b6ddf64fSTrond Myklebust /* 545b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 546b6ddf64fSTrond Myklebust * window size 547b6ddf64fSTrond Myklebust */ 548b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 549b6ddf64fSTrond Myklebust transport->inet->sk_write_pending++; 550b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 551b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 552b6ddf64fSTrond Myklebust } 553b6ddf64fSTrond Myklebust } else { 554b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 5555e3771ceSTrond Myklebust ret = -ENOTCONN; 556b6ddf64fSTrond Myklebust } 557a246b010SChuck Lever 558262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5595e3771ceSTrond Myklebust return ret; 560a246b010SChuck Lever } 561a246b010SChuck Lever 5629903cd1cSChuck Lever /** 563262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5649903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5659903cd1cSChuck Lever * 5669903cd1cSChuck Lever * Return values: 5679903cd1cSChuck Lever * 0: The request has been sent 5689903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5699903cd1cSChuck Lever * complete the request 570262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 5719903cd1cSChuck Lever * other: Some other error occured, the request was not sent 5729903cd1cSChuck Lever */ 573262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 574a246b010SChuck Lever { 575a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 576a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 577ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 578262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 579262965f5SChuck Lever int status; 580262965f5SChuck Lever 581262965f5SChuck Lever xs_pktdump("packet data:", 582262965f5SChuck Lever req->rq_svec->iov_base, 583262965f5SChuck Lever req->rq_svec->iov_len); 584262965f5SChuck Lever 58501d37c42STrond Myklebust if (!xprt_bound(xprt)) 58601d37c42STrond Myklebust return -ENOTCONN; 587ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 58895392c59SChuck Lever xs_addr(xprt), 589ee0ac0c2SChuck Lever xprt->addrlen, xdr, 590ee0ac0c2SChuck Lever req->rq_bytes_sent); 591262965f5SChuck Lever 592262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 593262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 594262965f5SChuck Lever 5952199700fSTrond Myklebust if (status >= 0) { 5961321d8d9SChuck Lever task->tk_bytes_sent += status; 5972199700fSTrond Myklebust if (status >= req->rq_slen) 598262965f5SChuck Lever return 0; 599262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 600262965f5SChuck Lever status = -EAGAIN; 6012199700fSTrond Myklebust } 602c8485e4dSTrond Myklebust if (!transport->sock) 603c8485e4dSTrond Myklebust goto out; 604262965f5SChuck Lever 605262965f5SChuck Lever switch (status) { 606fba91afbSTrond Myklebust case -ENOTSOCK: 607fba91afbSTrond Myklebust status = -ENOTCONN; 608fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 609fba91afbSTrond Myklebust break; 610b6ddf64fSTrond Myklebust case -EAGAIN: 6115e3771ceSTrond Myklebust status = xs_nospace(task); 612b6ddf64fSTrond Myklebust break; 613c8485e4dSTrond Myklebust default: 614c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 615c8485e4dSTrond Myklebust -status); 616262965f5SChuck Lever case -ENETUNREACH: 617262965f5SChuck Lever case -EPIPE: 618262965f5SChuck Lever case -ECONNREFUSED: 619262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 620262965f5SChuck Lever * prompts ECONNREFUSED. */ 621b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 622262965f5SChuck Lever } 623c8485e4dSTrond Myklebust out: 624262965f5SChuck Lever return status; 625262965f5SChuck Lever } 626262965f5SChuck Lever 627e06799f9STrond Myklebust /** 628e06799f9STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 629e06799f9STrond Myklebust * @xprt: transport 630e06799f9STrond Myklebust * 631e06799f9STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 632e06799f9STrond Myklebust * equivalent of shutdown(SHUT_WR); 633e06799f9STrond Myklebust */ 634e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 635e06799f9STrond Myklebust { 636e06799f9STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 637e06799f9STrond Myklebust struct socket *sock = transport->sock; 638e06799f9STrond Myklebust 639e06799f9STrond Myklebust if (sock != NULL) 640e06799f9STrond Myklebust kernel_sock_shutdown(sock, SHUT_WR); 641e06799f9STrond Myklebust } 642e06799f9STrond Myklebust 643808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 644808012fbSChuck Lever { 645808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 646808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 647808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 648808012fbSChuck Lever } 649808012fbSChuck Lever 650262965f5SChuck Lever /** 651262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 652262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 653262965f5SChuck Lever * 654262965f5SChuck Lever * Return values: 655262965f5SChuck Lever * 0: The request has been sent 656262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 657262965f5SChuck Lever * complete the request 658262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 659262965f5SChuck Lever * other: Some other error occured, the request was not sent 660262965f5SChuck Lever * 661262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 662262965f5SChuck Lever * if sendmsg is not able to make progress? 663262965f5SChuck Lever */ 664262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 665262965f5SChuck Lever { 666262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 667262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 668ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 669262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 670b595bb15SChuck Lever int status; 671a246b010SChuck Lever 672808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 673262965f5SChuck Lever 674262965f5SChuck Lever xs_pktdump("packet data:", 675262965f5SChuck Lever req->rq_svec->iov_base, 676262965f5SChuck Lever req->rq_svec->iov_len); 677a246b010SChuck Lever 678a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 679a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 680262965f5SChuck Lever * called sendmsg(). */ 681a246b010SChuck Lever while (1) { 682ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 683ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 684a246b010SChuck Lever 685262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 686262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 687262965f5SChuck Lever 688262965f5SChuck Lever if (unlikely(status < 0)) 689a246b010SChuck Lever break; 690a246b010SChuck Lever 691a246b010SChuck Lever /* If we've sent the entire packet, immediately 692a246b010SChuck Lever * reset the count of bytes sent. */ 693262965f5SChuck Lever req->rq_bytes_sent += status; 694ef759a2eSChuck Lever task->tk_bytes_sent += status; 695262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 696a246b010SChuck Lever req->rq_bytes_sent = 0; 697a246b010SChuck Lever return 0; 698a246b010SChuck Lever } 699262965f5SChuck Lever 70006b4b681STrond Myklebust if (status != 0) 70106b4b681STrond Myklebust continue; 702a246b010SChuck Lever status = -EAGAIN; 703a246b010SChuck Lever break; 704a246b010SChuck Lever } 705c8485e4dSTrond Myklebust if (!transport->sock) 706c8485e4dSTrond Myklebust goto out; 707a246b010SChuck Lever 708262965f5SChuck Lever switch (status) { 709fba91afbSTrond Myklebust case -ENOTSOCK: 710fba91afbSTrond Myklebust status = -ENOTCONN; 711fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 712fba91afbSTrond Myklebust break; 713262965f5SChuck Lever case -EAGAIN: 7145e3771ceSTrond Myklebust status = xs_nospace(task); 715262965f5SChuck Lever break; 716c8485e4dSTrond Myklebust default: 717c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 718c8485e4dSTrond Myklebust -status); 719262965f5SChuck Lever case -ECONNRESET: 7202a9e1cfaSTrond Myklebust xs_tcp_shutdown(xprt); 7212a9e1cfaSTrond Myklebust case -ECONNREFUSED: 722262965f5SChuck Lever case -ENOTCONN: 723262965f5SChuck Lever case -EPIPE: 724b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 725a246b010SChuck Lever } 726c8485e4dSTrond Myklebust out: 727a246b010SChuck Lever return status; 728a246b010SChuck Lever } 729a246b010SChuck Lever 7309903cd1cSChuck Lever /** 731e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 732e0ab53deSTrond Myklebust * @xprt: transport 733e0ab53deSTrond Myklebust * @task: rpc task 734e0ab53deSTrond Myklebust * 735e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 736e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 737e0ab53deSTrond Myklebust * the server. 738e0ab53deSTrond Myklebust */ 739e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 740e0ab53deSTrond Myklebust { 741e0ab53deSTrond Myklebust struct rpc_rqst *req; 742e0ab53deSTrond Myklebust 743e0ab53deSTrond Myklebust if (task != xprt->snd_task) 744e0ab53deSTrond Myklebust return; 745e0ab53deSTrond Myklebust if (task == NULL) 746e0ab53deSTrond Myklebust goto out_release; 747e0ab53deSTrond Myklebust req = task->tk_rqstp; 748e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 749e0ab53deSTrond Myklebust goto out_release; 750e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 751e0ab53deSTrond Myklebust goto out_release; 752e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 753e0ab53deSTrond Myklebust out_release: 754e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 755e0ab53deSTrond Myklebust } 756e0ab53deSTrond Myklebust 7572a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7582a9e1cfaSTrond Myklebust { 7592a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7602a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7612a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7622a9e1cfaSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7632a9e1cfaSTrond Myklebust } 7642a9e1cfaSTrond Myklebust 7652a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7662a9e1cfaSTrond Myklebust { 7672a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7682a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7692a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7702a9e1cfaSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7712a9e1cfaSTrond Myklebust } 7722a9e1cfaSTrond Myklebust 773fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 774a246b010SChuck Lever { 775ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 776ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 777a246b010SChuck Lever 778fe315e76SChuck Lever if (sk == NULL) 779fe315e76SChuck Lever return; 7809903cd1cSChuck Lever 781a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 782ee0ac0c2SChuck Lever transport->inet = NULL; 783ee0ac0c2SChuck Lever transport->sock = NULL; 784a246b010SChuck Lever 785a246b010SChuck Lever sk->sk_user_data = NULL; 7862a9e1cfaSTrond Myklebust 7872a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 788a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 789a246b010SChuck Lever 790a246b010SChuck Lever sk->sk_no_check = 0; 791a246b010SChuck Lever 792a246b010SChuck Lever sock_release(sock); 793fe315e76SChuck Lever } 794fe315e76SChuck Lever 795fe315e76SChuck Lever /** 796fe315e76SChuck Lever * xs_close - close a socket 797fe315e76SChuck Lever * @xprt: transport 798fe315e76SChuck Lever * 799fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 800fe315e76SChuck Lever * on the server we want to save. 801fe315e76SChuck Lever */ 802fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 803fe315e76SChuck Lever { 804fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 805fe315e76SChuck Lever 806fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 807fe315e76SChuck Lever 808fe315e76SChuck Lever xs_reset_transport(transport); 809fe315e76SChuck Lever 810632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 811*7d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 812632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 8133b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 814632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 81562da3b24STrond Myklebust xprt_disconnect_done(xprt); 816a246b010SChuck Lever } 817a246b010SChuck Lever 8189903cd1cSChuck Lever /** 8199903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8209903cd1cSChuck Lever * @xprt: doomed transport 8219903cd1cSChuck Lever * 8229903cd1cSChuck Lever */ 8239903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 824a246b010SChuck Lever { 825c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 826c8475461SChuck Lever 8279903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8289903cd1cSChuck Lever 829c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 830a246b010SChuck Lever 8319903cd1cSChuck Lever xs_close(xprt); 832edb267a6SChuck Lever xs_free_peer_addresses(xprt); 833a246b010SChuck Lever kfree(xprt->slot); 834c8541ecdSChuck Lever kfree(xprt); 835bc25571eS\"Talpey, Thomas\ module_put(THIS_MODULE); 836a246b010SChuck Lever } 837a246b010SChuck Lever 8389903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 8399903cd1cSChuck Lever { 8409903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 8419903cd1cSChuck Lever } 8429903cd1cSChuck Lever 8439903cd1cSChuck Lever /** 8449903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 8459903cd1cSChuck Lever * @sk: socket with data to read 8469903cd1cSChuck Lever * @len: how much data to read 8479903cd1cSChuck Lever * 848a246b010SChuck Lever */ 8499903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 850a246b010SChuck Lever { 851a246b010SChuck Lever struct rpc_task *task; 852a246b010SChuck Lever struct rpc_xprt *xprt; 853a246b010SChuck Lever struct rpc_rqst *rovr; 854a246b010SChuck Lever struct sk_buff *skb; 855a246b010SChuck Lever int err, repsize, copied; 856d8ed029dSAlexey Dobriyan u32 _xid; 857d8ed029dSAlexey Dobriyan __be32 *xp; 858a246b010SChuck Lever 859a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 8609903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 8619903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 862a246b010SChuck Lever goto out; 863a246b010SChuck Lever 864a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 865a246b010SChuck Lever goto out; 866a246b010SChuck Lever 867a246b010SChuck Lever if (xprt->shutdown) 868a246b010SChuck Lever goto dropit; 869a246b010SChuck Lever 870a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 871a246b010SChuck Lever if (repsize < 4) { 8729903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 873a246b010SChuck Lever goto dropit; 874a246b010SChuck Lever } 875a246b010SChuck Lever 876a246b010SChuck Lever /* Copy the XID from the skb... */ 877a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 878a246b010SChuck Lever sizeof(_xid), &_xid); 879a246b010SChuck Lever if (xp == NULL) 880a246b010SChuck Lever goto dropit; 881a246b010SChuck Lever 882a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 8834a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 884a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 885a246b010SChuck Lever if (!rovr) 886a246b010SChuck Lever goto out_unlock; 887a246b010SChuck Lever task = rovr->rq_task; 888a246b010SChuck Lever 889a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 890a246b010SChuck Lever copied = repsize; 891a246b010SChuck Lever 892a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 8931781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 8941781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 895a246b010SChuck Lever goto out_unlock; 8961781f7f5SHerbert Xu } 8971781f7f5SHerbert Xu 8981781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 899a246b010SChuck Lever 900a246b010SChuck Lever /* Something worked... */ 901a246b010SChuck Lever dst_confirm(skb->dst); 902a246b010SChuck Lever 9031570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 9041570c1e4SChuck Lever xprt_update_rtt(task); 9051570c1e4SChuck Lever xprt_complete_rqst(task, copied); 906a246b010SChuck Lever 907a246b010SChuck Lever out_unlock: 9084a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 909a246b010SChuck Lever dropit: 910a246b010SChuck Lever skb_free_datagram(sk, skb); 911a246b010SChuck Lever out: 912a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 913a246b010SChuck Lever } 914a246b010SChuck Lever 915dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 916a246b010SChuck Lever { 91751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 918a246b010SChuck Lever size_t len, used; 919a246b010SChuck Lever char *p; 920a246b010SChuck Lever 92151971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 92251971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 9239d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 92451971139SChuck Lever transport->tcp_offset += used; 925a246b010SChuck Lever if (used != len) 926a246b010SChuck Lever return; 927808012fbSChuck Lever 92851971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 92951971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 930e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 931a246b010SChuck Lever else 932e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 93351971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 934808012fbSChuck Lever 935e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 93651971139SChuck Lever transport->tcp_offset = 0; 937808012fbSChuck Lever 938a246b010SChuck Lever /* Sanity check of the record length */ 93951971139SChuck Lever if (unlikely(transport->tcp_reclen < 4)) { 9409903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 9413ebb067dSTrond Myklebust xprt_force_disconnect(xprt); 9429903cd1cSChuck Lever return; 943a246b010SChuck Lever } 944a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 94551971139SChuck Lever transport->tcp_reclen); 946a246b010SChuck Lever } 947a246b010SChuck Lever 94851971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 949a246b010SChuck Lever { 95051971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 951e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 95251971139SChuck Lever transport->tcp_offset = 0; 953e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 954e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 955e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 95651971139SChuck Lever transport->tcp_copied = 0; 957a246b010SChuck Lever } 958a246b010SChuck Lever } 959a246b010SChuck Lever } 960a246b010SChuck Lever 961dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 962a246b010SChuck Lever { 963a246b010SChuck Lever size_t len, used; 964a246b010SChuck Lever char *p; 965a246b010SChuck Lever 96651971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 967a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 96851971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 9699d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 97051971139SChuck Lever transport->tcp_offset += used; 971a246b010SChuck Lever if (used != len) 972a246b010SChuck Lever return; 973e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 974e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_DATA; 97551971139SChuck Lever transport->tcp_copied = 4; 976a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 97751971139SChuck Lever ntohl(transport->tcp_xid)); 97851971139SChuck Lever xs_tcp_check_fraghdr(transport); 979a246b010SChuck Lever } 980a246b010SChuck Lever 981dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 982a246b010SChuck Lever { 98351971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 984a246b010SChuck Lever struct rpc_rqst *req; 985a246b010SChuck Lever struct xdr_buf *rcvbuf; 986a246b010SChuck Lever size_t len; 987a246b010SChuck Lever ssize_t r; 988a246b010SChuck Lever 989a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 9904a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 99151971139SChuck Lever req = xprt_lookup_rqst(xprt, transport->tcp_xid); 992a246b010SChuck Lever if (!req) { 993e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 994a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 99551971139SChuck Lever ntohl(transport->tcp_xid)); 9964a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 997a246b010SChuck Lever return; 998a246b010SChuck Lever } 999a246b010SChuck Lever 1000a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1001a246b010SChuck Lever len = desc->count; 100251971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1003dd456471SChuck Lever struct xdr_skb_reader my_desc; 1004a246b010SChuck Lever 100551971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1006a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1007a246b010SChuck Lever my_desc.count = len; 100851971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10099d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1010a246b010SChuck Lever desc->count -= r; 1011a246b010SChuck Lever desc->offset += r; 1012a246b010SChuck Lever } else 101351971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10149d292316SChuck Lever desc, xdr_skb_read_bits); 1015a246b010SChuck Lever 1016a246b010SChuck Lever if (r > 0) { 101751971139SChuck Lever transport->tcp_copied += r; 101851971139SChuck Lever transport->tcp_offset += r; 1019a246b010SChuck Lever } 1020a246b010SChuck Lever if (r != len) { 1021a246b010SChuck Lever /* Error when copying to the receive buffer, 1022a246b010SChuck Lever * usually because we weren't able to allocate 1023a246b010SChuck Lever * additional buffer pages. All we can do now 1024e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1025a246b010SChuck Lever * will not receive any additional updates, 1026a246b010SChuck Lever * and time out. 1027a246b010SChuck Lever * Any remaining data from this record will 1028a246b010SChuck Lever * be discarded. 1029a246b010SChuck Lever */ 1030e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1031a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 103251971139SChuck Lever ntohl(transport->tcp_xid)); 103346121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 103446121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 103546121cf7SChuck Lever xprt, transport->tcp_copied, 103646121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1037a246b010SChuck Lever goto out; 1038a246b010SChuck Lever } 1039a246b010SChuck Lever 1040a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 104151971139SChuck Lever ntohl(transport->tcp_xid), r); 104246121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 104346121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 104446121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1045a246b010SChuck Lever 104651971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1047e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 104851971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1049e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1050e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1051a246b010SChuck Lever } 1052a246b010SChuck Lever 1053a246b010SChuck Lever out: 1054e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 105551971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 10564a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 105751971139SChuck Lever xs_tcp_check_fraghdr(transport); 1058a246b010SChuck Lever } 1059a246b010SChuck Lever 1060dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1061a246b010SChuck Lever { 1062a246b010SChuck Lever size_t len; 1063a246b010SChuck Lever 106451971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1065a246b010SChuck Lever if (len > desc->count) 1066a246b010SChuck Lever len = desc->count; 1067a246b010SChuck Lever desc->count -= len; 1068a246b010SChuck Lever desc->offset += len; 106951971139SChuck Lever transport->tcp_offset += len; 1070a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 107151971139SChuck Lever xs_tcp_check_fraghdr(transport); 1072a246b010SChuck Lever } 1073a246b010SChuck Lever 10749903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1075a246b010SChuck Lever { 1076a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 107751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1078dd456471SChuck Lever struct xdr_skb_reader desc = { 1079a246b010SChuck Lever .skb = skb, 1080a246b010SChuck Lever .offset = offset, 1081a246b010SChuck Lever .count = len, 1082a246b010SChuck Lever }; 1083a246b010SChuck Lever 10849903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1085a246b010SChuck Lever do { 1086a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1087a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1088e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 10899903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1090a246b010SChuck Lever continue; 1091a246b010SChuck Lever } 1092a246b010SChuck Lever /* Read in the xid if necessary */ 1093e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 109451971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1095a246b010SChuck Lever continue; 1096a246b010SChuck Lever } 1097a246b010SChuck Lever /* Read in the request data */ 1098e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 10999903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 1100a246b010SChuck Lever continue; 1101a246b010SChuck Lever } 1102a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 110351971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1104a246b010SChuck Lever } while (desc.count); 11059903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1106a246b010SChuck Lever return len - desc.count; 1107a246b010SChuck Lever } 1108a246b010SChuck Lever 11099903cd1cSChuck Lever /** 11109903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 11119903cd1cSChuck Lever * @sk: socket with data to read 11129903cd1cSChuck Lever * @bytes: how much data to read 11139903cd1cSChuck Lever * 11149903cd1cSChuck Lever */ 11159903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1116a246b010SChuck Lever { 1117a246b010SChuck Lever struct rpc_xprt *xprt; 1118a246b010SChuck Lever read_descriptor_t rd_desc; 1119ff2d7db8STrond Myklebust int read; 1120a246b010SChuck Lever 11219903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 112246121cf7SChuck Lever 112346121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 11249903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1125a246b010SChuck Lever goto out; 1126a246b010SChuck Lever if (xprt->shutdown) 1127a246b010SChuck Lever goto out; 1128a246b010SChuck Lever 11299903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1130a246b010SChuck Lever rd_desc.arg.data = xprt; 1131ff2d7db8STrond Myklebust do { 1132a246b010SChuck Lever rd_desc.count = 65536; 1133ff2d7db8STrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1134ff2d7db8STrond Myklebust } while (read > 0); 1135a246b010SChuck Lever out: 1136a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1137a246b010SChuck Lever } 1138a246b010SChuck Lever 1139*7d1e8255STrond Myklebust /* 1140*7d1e8255STrond Myklebust * Do the equivalent of linger/linger2 handling for dealing with 1141*7d1e8255STrond Myklebust * broken servers that don't close the socket in a timely 1142*7d1e8255STrond Myklebust * fashion 1143*7d1e8255STrond Myklebust */ 1144*7d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, 1145*7d1e8255STrond Myklebust unsigned long timeout) 1146*7d1e8255STrond Myklebust { 1147*7d1e8255STrond Myklebust struct sock_xprt *transport; 1148*7d1e8255STrond Myklebust 1149*7d1e8255STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 1150*7d1e8255STrond Myklebust return; 1151*7d1e8255STrond Myklebust set_bit(XPRT_CONNECTION_ABORT, &xprt->state); 1152*7d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1153*7d1e8255STrond Myklebust queue_delayed_work(rpciod_workqueue, &transport->connect_worker, 1154*7d1e8255STrond Myklebust timeout); 1155*7d1e8255STrond Myklebust } 1156*7d1e8255STrond Myklebust 1157*7d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) 1158*7d1e8255STrond Myklebust { 1159*7d1e8255STrond Myklebust struct sock_xprt *transport; 1160*7d1e8255STrond Myklebust 1161*7d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1162*7d1e8255STrond Myklebust 1163*7d1e8255STrond Myklebust if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || 1164*7d1e8255STrond Myklebust !cancel_delayed_work(&transport->connect_worker)) 1165*7d1e8255STrond Myklebust return; 1166*7d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 1167*7d1e8255STrond Myklebust xprt_clear_connecting(xprt); 1168*7d1e8255STrond Myklebust } 1169*7d1e8255STrond Myklebust 1170*7d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 1171*7d1e8255STrond Myklebust { 1172*7d1e8255STrond Myklebust smp_mb__before_clear_bit(); 1173*7d1e8255STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1174*7d1e8255STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 1175*7d1e8255STrond Myklebust smp_mb__after_clear_bit(); 1176*7d1e8255STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 1177*7d1e8255STrond Myklebust xprt_disconnect_done(xprt); 1178*7d1e8255STrond Myklebust } 1179*7d1e8255STrond Myklebust 11809903cd1cSChuck Lever /** 11819903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 11829903cd1cSChuck Lever * @sk: socket whose state has changed 11839903cd1cSChuck Lever * 11849903cd1cSChuck Lever */ 11859903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1186a246b010SChuck Lever { 1187a246b010SChuck Lever struct rpc_xprt *xprt; 1188a246b010SChuck Lever 1189a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1190a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1191a246b010SChuck Lever goto out; 11929903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1193a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 1194a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1195a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1196a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 1197a246b010SChuck Lever 1198a246b010SChuck Lever switch (sk->sk_state) { 1199a246b010SChuck Lever case TCP_ESTABLISHED: 12004a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 1201a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 120251971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 120351971139SChuck Lever struct sock_xprt, xprt); 120451971139SChuck Lever 1205a246b010SChuck Lever /* Reset TCP record info */ 120651971139SChuck Lever transport->tcp_offset = 0; 120751971139SChuck Lever transport->tcp_reclen = 0; 120851971139SChuck Lever transport->tcp_copied = 0; 1209e136d092SChuck Lever transport->tcp_flags = 1210e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 121151971139SChuck Lever 12122a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1213a246b010SChuck Lever } 12144a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1215a246b010SChuck Lever break; 12163b948ae5STrond Myklebust case TCP_FIN_WAIT1: 12173b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 12187c1d71cfSTrond Myklebust xprt->connect_cookie++; 1219663b8858STrond Myklebust xprt->reestablish_timeout = 0; 12203b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 12213b948ae5STrond Myklebust smp_mb__before_clear_bit(); 12223b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1223ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 12243b948ae5STrond Myklebust smp_mb__after_clear_bit(); 1225*7d1e8255STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, XS_TCP_LINGER_TO); 1226a246b010SChuck Lever break; 1227632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 12283b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 122966af1e55STrond Myklebust xprt_force_disconnect(xprt); 1230663b8858STrond Myklebust case TCP_SYN_SENT: 12317c1d71cfSTrond Myklebust xprt->connect_cookie++; 1232663b8858STrond Myklebust case TCP_CLOSING: 1233663b8858STrond Myklebust /* 1234663b8858STrond Myklebust * If the server closed down the connection, make sure that 1235663b8858STrond Myklebust * we back off before reconnecting 1236663b8858STrond Myklebust */ 1237663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1238663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 12393b948ae5STrond Myklebust break; 12403b948ae5STrond Myklebust case TCP_LAST_ACK: 1241670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 1242*7d1e8255STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, XS_TCP_LINGER_TO); 12433b948ae5STrond Myklebust smp_mb__before_clear_bit(); 12443b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 12453b948ae5STrond Myklebust smp_mb__after_clear_bit(); 12463b948ae5STrond Myklebust break; 12473b948ae5STrond Myklebust case TCP_CLOSE: 1248*7d1e8255STrond Myklebust xs_tcp_cancel_linger_timeout(xprt); 1249*7d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1250a246b010SChuck Lever } 1251a246b010SChuck Lever out: 1252a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1253a246b010SChuck Lever } 1254a246b010SChuck Lever 12559903cd1cSChuck Lever /** 1256482f32e6STrond Myklebust * xs_error_report - callback mainly for catching socket errors 12572a9e1cfaSTrond Myklebust * @sk: socket 12582a9e1cfaSTrond Myklebust */ 1259482f32e6STrond Myklebust static void xs_error_report(struct sock *sk) 12602a9e1cfaSTrond Myklebust { 12612a9e1cfaSTrond Myklebust struct rpc_xprt *xprt; 12622a9e1cfaSTrond Myklebust 12632a9e1cfaSTrond Myklebust read_lock(&sk->sk_callback_lock); 12642a9e1cfaSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12652a9e1cfaSTrond Myklebust goto out; 12662a9e1cfaSTrond Myklebust dprintk("RPC: %s client %p...\n" 12672a9e1cfaSTrond Myklebust "RPC: error %d\n", 12682a9e1cfaSTrond Myklebust __func__, xprt, sk->sk_err); 1269482f32e6STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 12702a9e1cfaSTrond Myklebust out: 12712a9e1cfaSTrond Myklebust read_unlock(&sk->sk_callback_lock); 12722a9e1cfaSTrond Myklebust } 12732a9e1cfaSTrond Myklebust 12742a9e1cfaSTrond Myklebust /** 1275c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1276c7b2cae8SChuck Lever * becomes available 12779903cd1cSChuck Lever * @sk: socket whose state has changed 12789903cd1cSChuck Lever * 1279a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1280a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1281c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1282a246b010SChuck Lever * with a bunch of small requests. 1283a246b010SChuck Lever */ 1284c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1285a246b010SChuck Lever { 1286a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1287c7b2cae8SChuck Lever 1288c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 1289c7b2cae8SChuck Lever if (sock_writeable(sk)) { 1290c7b2cae8SChuck Lever struct socket *sock; 1291c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1292c7b2cae8SChuck Lever 1293c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1294a246b010SChuck Lever goto out; 1295b6ddf64fSTrond Myklebust clear_bit(SOCK_NOSPACE, &sock->flags); 1296b6ddf64fSTrond Myklebust 1297c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1298c7b2cae8SChuck Lever goto out; 1299b6ddf64fSTrond Myklebust if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 1300a246b010SChuck Lever goto out; 1301a246b010SChuck Lever 1302c7b2cae8SChuck Lever xprt_write_space(xprt); 1303a246b010SChuck Lever } 1304a246b010SChuck Lever 1305c7b2cae8SChuck Lever out: 1306c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1307c7b2cae8SChuck Lever } 1308c7b2cae8SChuck Lever 1309c7b2cae8SChuck Lever /** 1310c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1311c7b2cae8SChuck Lever * becomes available 1312c7b2cae8SChuck Lever * @sk: socket whose state has changed 1313c7b2cae8SChuck Lever * 1314c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1315c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1316c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1317c7b2cae8SChuck Lever * with a bunch of small requests. 1318c7b2cae8SChuck Lever */ 1319c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1320c7b2cae8SChuck Lever { 1321c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1322c7b2cae8SChuck Lever 1323c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 1324c7b2cae8SChuck Lever if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 1325c7b2cae8SChuck Lever struct socket *sock; 1326c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1327c7b2cae8SChuck Lever 1328c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1329c7b2cae8SChuck Lever goto out; 1330b6ddf64fSTrond Myklebust clear_bit(SOCK_NOSPACE, &sock->flags); 1331b6ddf64fSTrond Myklebust 1332c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1333c7b2cae8SChuck Lever goto out; 1334b6ddf64fSTrond Myklebust if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 1335a246b010SChuck Lever goto out; 1336a246b010SChuck Lever 1337c7b2cae8SChuck Lever xprt_write_space(xprt); 1338c7b2cae8SChuck Lever } 1339c7b2cae8SChuck Lever 1340a246b010SChuck Lever out: 1341a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1342a246b010SChuck Lever } 1343a246b010SChuck Lever 1344470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1345a246b010SChuck Lever { 1346ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1347ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1348a246b010SChuck Lever 13497c6e066eSChuck Lever if (transport->rcvsize) { 1350a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 13517c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1352a246b010SChuck Lever } 13537c6e066eSChuck Lever if (transport->sndsize) { 1354a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 13557c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1356a246b010SChuck Lever sk->sk_write_space(sk); 1357a246b010SChuck Lever } 1358a246b010SChuck Lever } 1359a246b010SChuck Lever 136043118c29SChuck Lever /** 1361470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 136243118c29SChuck Lever * @xprt: generic transport 1363470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1364470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 136543118c29SChuck Lever * 1366470056c2SChuck Lever * Set socket send and receive buffer size limits. 136743118c29SChuck Lever */ 1368470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 136943118c29SChuck Lever { 13707c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 13717c6e066eSChuck Lever 13727c6e066eSChuck Lever transport->sndsize = 0; 1373470056c2SChuck Lever if (sndsize) 13747c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 13757c6e066eSChuck Lever transport->rcvsize = 0; 1376470056c2SChuck Lever if (rcvsize) 13777c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1378470056c2SChuck Lever 1379470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 138043118c29SChuck Lever } 138143118c29SChuck Lever 138246c0ee8bSChuck Lever /** 138346c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 138446c0ee8bSChuck Lever * @task: task that timed out 138546c0ee8bSChuck Lever * 138646c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 138746c0ee8bSChuck Lever */ 138846c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 138946c0ee8bSChuck Lever { 139046c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 139146c0ee8bSChuck Lever } 139246c0ee8bSChuck Lever 1393b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1394b85d8806SChuck Lever { 1395b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1396b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1397b85d8806SChuck Lever return rand + xprt_min_resvport; 1398b85d8806SChuck Lever } 1399b85d8806SChuck Lever 140092200412SChuck Lever /** 140192200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 140292200412SChuck Lever * @xprt: generic transport 140392200412SChuck Lever * @port: new port number 140492200412SChuck Lever * 140592200412SChuck Lever */ 140692200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 140792200412SChuck Lever { 140895392c59SChuck Lever struct sockaddr *addr = xs_addr(xprt); 1409c4efcb1dSChuck Lever 141092200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1411c4efcb1dSChuck Lever 141220612005SChuck Lever switch (addr->sa_family) { 141320612005SChuck Lever case AF_INET: 141420612005SChuck Lever ((struct sockaddr_in *)addr)->sin_port = htons(port); 141520612005SChuck Lever break; 141620612005SChuck Lever case AF_INET6: 141720612005SChuck Lever ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); 141820612005SChuck Lever break; 141920612005SChuck Lever default: 142020612005SChuck Lever BUG(); 142120612005SChuck Lever } 142292200412SChuck Lever } 142392200412SChuck Lever 142467a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock) 142567a391d7STrond Myklebust { 142667a391d7STrond Myklebust unsigned short port = transport->port; 142767a391d7STrond Myklebust 142867a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 142967a391d7STrond Myklebust port = xs_get_random_port(); 143067a391d7STrond Myklebust return port; 143167a391d7STrond Myklebust } 143267a391d7STrond Myklebust 143367a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port) 143467a391d7STrond Myklebust { 143567a391d7STrond Myklebust if (transport->port != 0) 143667a391d7STrond Myklebust transport->port = 0; 143767a391d7STrond Myklebust if (!transport->xprt.resvport) 143867a391d7STrond Myklebust return 0; 143967a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 144067a391d7STrond Myklebust return xprt_max_resvport; 144167a391d7STrond Myklebust return --port; 144267a391d7STrond Myklebust } 144367a391d7STrond Myklebust 14447dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock) 1445a246b010SChuck Lever { 1446a246b010SChuck Lever struct sockaddr_in myaddr = { 1447a246b010SChuck Lever .sin_family = AF_INET, 1448a246b010SChuck Lever }; 1449d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 145067a391d7STrond Myklebust int err, nloop = 0; 145167a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 145267a391d7STrond Myklebust unsigned short last; 1453a246b010SChuck Lever 1454d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1455d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1456a246b010SChuck Lever do { 1457a246b010SChuck Lever myaddr.sin_port = htons(port); 1458e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1459a246b010SChuck Lever sizeof(myaddr)); 146067a391d7STrond Myklebust if (port == 0) 1461d3bc9a1dSFrank van Maarseveen break; 1462a246b010SChuck Lever if (err == 0) { 1463c8475461SChuck Lever transport->port = port; 1464d3bc9a1dSFrank van Maarseveen break; 1465a246b010SChuck Lever } 146667a391d7STrond Myklebust last = port; 146767a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 146867a391d7STrond Myklebust if (port > last) 146967a391d7STrond Myklebust nloop++; 147067a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 147121454aaaSHarvey Harrison dprintk("RPC: %s %pI4:%u: %s (%d)\n", 147221454aaaSHarvey Harrison __func__, &myaddr.sin_addr, 14737dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1474a246b010SChuck Lever return err; 1475a246b010SChuck Lever } 1476a246b010SChuck Lever 147790058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock) 147890058d37SChuck Lever { 147990058d37SChuck Lever struct sockaddr_in6 myaddr = { 148090058d37SChuck Lever .sin6_family = AF_INET6, 148190058d37SChuck Lever }; 148290058d37SChuck Lever struct sockaddr_in6 *sa; 148367a391d7STrond Myklebust int err, nloop = 0; 148467a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 148567a391d7STrond Myklebust unsigned short last; 148690058d37SChuck Lever 148790058d37SChuck Lever sa = (struct sockaddr_in6 *)&transport->addr; 148890058d37SChuck Lever myaddr.sin6_addr = sa->sin6_addr; 148990058d37SChuck Lever do { 149090058d37SChuck Lever myaddr.sin6_port = htons(port); 149190058d37SChuck Lever err = kernel_bind(sock, (struct sockaddr *) &myaddr, 149290058d37SChuck Lever sizeof(myaddr)); 149367a391d7STrond Myklebust if (port == 0) 149490058d37SChuck Lever break; 149590058d37SChuck Lever if (err == 0) { 149690058d37SChuck Lever transport->port = port; 149790058d37SChuck Lever break; 149890058d37SChuck Lever } 149967a391d7STrond Myklebust last = port; 150067a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 150167a391d7STrond Myklebust if (port > last) 150267a391d7STrond Myklebust nloop++; 150367a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 15045b095d98SHarvey Harrison dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", 1505fdb46ee7SHarvey Harrison &myaddr.sin6_addr, port, err ? "failed" : "ok", err); 1506a246b010SChuck Lever return err; 1507a246b010SChuck Lever } 1508a246b010SChuck Lever 1509ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1510ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1511ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1512ed07536eSPeter Zijlstra 15138945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1514ed07536eSPeter Zijlstra { 1515ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 15168945ee5eSChuck Lever 151702b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 15188945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 15198945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1520ed07536eSPeter Zijlstra } 15218945ee5eSChuck Lever 15228945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 15238945ee5eSChuck Lever { 15248945ee5eSChuck Lever struct sock *sk = sock->sk; 15258945ee5eSChuck Lever 1526f4921affSLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 15278945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 15288945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1529ed07536eSPeter Zijlstra } 1530ed07536eSPeter Zijlstra #else 15318945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 15328945ee5eSChuck Lever { 15338945ee5eSChuck Lever } 15348945ee5eSChuck Lever 15358945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1536ed07536eSPeter Zijlstra { 1537ed07536eSPeter Zijlstra } 1538ed07536eSPeter Zijlstra #endif 1539ed07536eSPeter Zijlstra 154016be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1541a246b010SChuck Lever { 154216be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1543edb267a6SChuck Lever 1544ee0ac0c2SChuck Lever if (!transport->inet) { 1545b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1546b0d93ad5SChuck Lever 1547b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1548b0d93ad5SChuck Lever 15492a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 15502a9e1cfaSTrond Myklebust 1551b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1552b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1553b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1554482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1555b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 1556b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 1557b0d93ad5SChuck Lever 1558b0d93ad5SChuck Lever xprt_set_connected(xprt); 1559b0d93ad5SChuck Lever 1560b0d93ad5SChuck Lever /* Reset to new socket */ 1561ee0ac0c2SChuck Lever transport->sock = sock; 1562ee0ac0c2SChuck Lever transport->inet = sk; 1563b0d93ad5SChuck Lever 1564b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1565b0d93ad5SChuck Lever } 1566470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 156716be2d20SChuck Lever } 156816be2d20SChuck Lever 1569a246b010SChuck Lever /** 15709c3d72deSChuck Lever * xs_udp_connect_worker4 - set up a UDP socket 1571a246b010SChuck Lever * @work: RPC transport to connect 1572a246b010SChuck Lever * 1573a246b010SChuck Lever * Invoked by a work queue tasklet. 1574a246b010SChuck Lever */ 15759c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work) 1576a246b010SChuck Lever { 1577a246b010SChuck Lever struct sock_xprt *transport = 1578a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1579a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1580a246b010SChuck Lever struct socket *sock = transport->sock; 1581a246b010SChuck Lever int err, status = -EIO; 1582a246b010SChuck Lever 158301d37c42STrond Myklebust if (xprt->shutdown) 15849903cd1cSChuck Lever goto out; 15859903cd1cSChuck Lever 1586a246b010SChuck Lever /* Start by resetting any existing state */ 1587fe315e76SChuck Lever xs_reset_transport(transport); 1588a246b010SChuck Lever 1589fe315e76SChuck Lever err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); 1590fe315e76SChuck Lever if (err < 0) { 15919903cd1cSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1592a246b010SChuck Lever goto out; 1593a246b010SChuck Lever } 15948945ee5eSChuck Lever xs_reclassify_socket4(sock); 1595a246b010SChuck Lever 15967dc753f0SChuck Lever if (xs_bind4(transport, sock)) { 15979903cd1cSChuck Lever sock_release(sock); 15989903cd1cSChuck Lever goto out; 1599a246b010SChuck Lever } 1600b0d93ad5SChuck Lever 1601b0d93ad5SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 1602b0d93ad5SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1603b0d93ad5SChuck Lever 160416be2d20SChuck Lever xs_udp_finish_connecting(xprt, sock); 1605a246b010SChuck Lever status = 0; 1606b0d93ad5SChuck Lever out: 1607b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1608*7d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1609b0d93ad5SChuck Lever } 1610b0d93ad5SChuck Lever 161168e220bdSChuck Lever /** 161268e220bdSChuck Lever * xs_udp_connect_worker6 - set up a UDP socket 161368e220bdSChuck Lever * @work: RPC transport to connect 161468e220bdSChuck Lever * 161568e220bdSChuck Lever * Invoked by a work queue tasklet. 161668e220bdSChuck Lever */ 161768e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work) 161868e220bdSChuck Lever { 161968e220bdSChuck Lever struct sock_xprt *transport = 162068e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 162168e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 162268e220bdSChuck Lever struct socket *sock = transport->sock; 162368e220bdSChuck Lever int err, status = -EIO; 162468e220bdSChuck Lever 162501d37c42STrond Myklebust if (xprt->shutdown) 162668e220bdSChuck Lever goto out; 162768e220bdSChuck Lever 162868e220bdSChuck Lever /* Start by resetting any existing state */ 1629fe315e76SChuck Lever xs_reset_transport(transport); 163068e220bdSChuck Lever 1631fe315e76SChuck Lever err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); 1632fe315e76SChuck Lever if (err < 0) { 163368e220bdSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 163468e220bdSChuck Lever goto out; 163568e220bdSChuck Lever } 16368945ee5eSChuck Lever xs_reclassify_socket6(sock); 163768e220bdSChuck Lever 163868e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 163968e220bdSChuck Lever sock_release(sock); 164068e220bdSChuck Lever goto out; 164168e220bdSChuck Lever } 164268e220bdSChuck Lever 164368e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 164468e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 164568e220bdSChuck Lever 164668e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 1647a246b010SChuck Lever status = 0; 1648b0d93ad5SChuck Lever out: 1649b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1650*7d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1651b0d93ad5SChuck Lever } 1652b0d93ad5SChuck Lever 16533167e12cSChuck Lever /* 16543167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 16553167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 16563167e12cSChuck Lever */ 165740d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 16583167e12cSChuck Lever { 16593167e12cSChuck Lever int result; 16603167e12cSChuck Lever struct sockaddr any; 16613167e12cSChuck Lever 16623167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 16633167e12cSChuck Lever 16643167e12cSChuck Lever /* 16653167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 16663167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 16673167e12cSChuck Lever */ 16683167e12cSChuck Lever memset(&any, 0, sizeof(any)); 16693167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1670ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 1671*7d1e8255STrond Myklebust if (!result) 1672*7d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1673*7d1e8255STrond Myklebust else 16743167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 16753167e12cSChuck Lever result); 16763167e12cSChuck Lever } 16773167e12cSChuck Lever 167840d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 167940d2549dSTrond Myklebust { 168040d2549dSTrond Myklebust unsigned int state = transport->inet->sk_state; 168140d2549dSTrond Myklebust 168240d2549dSTrond Myklebust if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) 168340d2549dSTrond Myklebust return; 168440d2549dSTrond Myklebust if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) 168540d2549dSTrond Myklebust return; 168640d2549dSTrond Myklebust xs_abort_connection(xprt, transport); 168740d2549dSTrond Myklebust } 168840d2549dSTrond Myklebust 168916be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1690b0d93ad5SChuck Lever { 169116be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1692edb267a6SChuck Lever 1693ee0ac0c2SChuck Lever if (!transport->inet) { 1694b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1695b0d93ad5SChuck Lever 1696b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1697b0d93ad5SChuck Lever 16982a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 16992a9e1cfaSTrond Myklebust 1700b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1701b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1702b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1703b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1704482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1705b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 17063167e12cSChuck Lever 17073167e12cSChuck Lever /* socket options */ 17083167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 17093167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 17103167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 17113167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1712b0d93ad5SChuck Lever 1713b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1714b0d93ad5SChuck Lever 1715b0d93ad5SChuck Lever /* Reset to new socket */ 1716ee0ac0c2SChuck Lever transport->sock = sock; 1717ee0ac0c2SChuck Lever transport->inet = sk; 1718b0d93ad5SChuck Lever 1719b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1720b0d93ad5SChuck Lever } 1721b0d93ad5SChuck Lever 172201d37c42STrond Myklebust if (!xprt_bound(xprt)) 172301d37c42STrond Myklebust return -ENOTCONN; 172401d37c42STrond Myklebust 1725b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1726262ca07dSChuck Lever xprt->stat.connect_count++; 1727262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 172895392c59SChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 172916be2d20SChuck Lever } 173016be2d20SChuck Lever 173116be2d20SChuck Lever /** 17329c3d72deSChuck Lever * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint 173316be2d20SChuck Lever * @work: RPC transport to connect 173416be2d20SChuck Lever * 173516be2d20SChuck Lever * Invoked by a work queue tasklet. 173616be2d20SChuck Lever */ 17379c3d72deSChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work) 173816be2d20SChuck Lever { 173916be2d20SChuck Lever struct sock_xprt *transport = 174016be2d20SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 174116be2d20SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 174216be2d20SChuck Lever struct socket *sock = transport->sock; 174316be2d20SChuck Lever int err, status = -EIO; 174416be2d20SChuck Lever 174501d37c42STrond Myklebust if (xprt->shutdown) 174616be2d20SChuck Lever goto out; 174716be2d20SChuck Lever 174816be2d20SChuck Lever if (!sock) { 1749*7d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 175016be2d20SChuck Lever /* start from scratch */ 175116be2d20SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 175216be2d20SChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 175316be2d20SChuck Lever goto out; 175416be2d20SChuck Lever } 17558945ee5eSChuck Lever xs_reclassify_socket4(sock); 175616be2d20SChuck Lever 175716be2d20SChuck Lever if (xs_bind4(transport, sock) < 0) { 175816be2d20SChuck Lever sock_release(sock); 175916be2d20SChuck Lever goto out; 176016be2d20SChuck Lever } 1761*7d1e8255STrond Myklebust } else { 1762*7d1e8255STrond Myklebust int abort_and_exit; 1763*7d1e8255STrond Myklebust 1764*7d1e8255STrond Myklebust abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, 1765*7d1e8255STrond Myklebust &xprt->state); 176616be2d20SChuck Lever /* "close" the socket, preserving the local port */ 176740d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 176816be2d20SChuck Lever 1769*7d1e8255STrond Myklebust if (abort_and_exit) 1770*7d1e8255STrond Myklebust goto out_eagain; 1771*7d1e8255STrond Myklebust } 1772*7d1e8255STrond Myklebust 177316be2d20SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 177416be2d20SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 177516be2d20SChuck Lever 177616be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 1777a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 177846121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 177946121cf7SChuck Lever sock->sk->sk_state); 1780a246b010SChuck Lever switch (status) { 17818a2cec29STrond Myklebust case -ECONNREFUSED: 17828a2cec29STrond Myklebust case -ECONNRESET: 17838a2cec29STrond Myklebust case -ENETUNREACH: 17848a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 17852a491991STrond Myklebust case 0: 1786a246b010SChuck Lever case -EINPROGRESS: 1787a246b010SChuck Lever case -EALREADY: 1788*7d1e8255STrond Myklebust xprt_clear_connecting(xprt); 1789*7d1e8255STrond Myklebust return; 17908a2cec29STrond Myklebust } 17913167e12cSChuck Lever /* get rid of existing socket, and retry */ 1792e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 17932a491991STrond Myklebust printk("%s: connect returned unhandled error %d\n", 17942a491991STrond Myklebust __func__, status); 1795*7d1e8255STrond Myklebust out_eagain: 17962a491991STrond Myklebust status = -EAGAIN; 1797a246b010SChuck Lever out: 17982226feb6SChuck Lever xprt_clear_connecting(xprt); 1799*7d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1800a246b010SChuck Lever } 1801a246b010SChuck Lever 18029903cd1cSChuck Lever /** 180368e220bdSChuck Lever * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint 180468e220bdSChuck Lever * @work: RPC transport to connect 180568e220bdSChuck Lever * 180668e220bdSChuck Lever * Invoked by a work queue tasklet. 180768e220bdSChuck Lever */ 180868e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work) 180968e220bdSChuck Lever { 181068e220bdSChuck Lever struct sock_xprt *transport = 181168e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 181268e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 181368e220bdSChuck Lever struct socket *sock = transport->sock; 181468e220bdSChuck Lever int err, status = -EIO; 181568e220bdSChuck Lever 181601d37c42STrond Myklebust if (xprt->shutdown) 181768e220bdSChuck Lever goto out; 181868e220bdSChuck Lever 181968e220bdSChuck Lever if (!sock) { 1820*7d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 182168e220bdSChuck Lever /* start from scratch */ 182268e220bdSChuck Lever if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 182368e220bdSChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 182468e220bdSChuck Lever goto out; 182568e220bdSChuck Lever } 18268945ee5eSChuck Lever xs_reclassify_socket6(sock); 182768e220bdSChuck Lever 182868e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 182968e220bdSChuck Lever sock_release(sock); 183068e220bdSChuck Lever goto out; 183168e220bdSChuck Lever } 1832*7d1e8255STrond Myklebust } else { 1833*7d1e8255STrond Myklebust int abort_and_exit; 1834*7d1e8255STrond Myklebust 1835*7d1e8255STrond Myklebust abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, 1836*7d1e8255STrond Myklebust &xprt->state); 183768e220bdSChuck Lever /* "close" the socket, preserving the local port */ 183840d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 183968e220bdSChuck Lever 1840*7d1e8255STrond Myklebust if (abort_and_exit) 1841*7d1e8255STrond Myklebust goto out_eagain; 1842*7d1e8255STrond Myklebust } 1843*7d1e8255STrond Myklebust 184468e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 184568e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 184668e220bdSChuck Lever 184768e220bdSChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 184868e220bdSChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 184968e220bdSChuck Lever xprt, -status, xprt_connected(xprt), sock->sk->sk_state); 185068e220bdSChuck Lever switch (status) { 18518a2cec29STrond Myklebust case -ECONNREFUSED: 18528a2cec29STrond Myklebust case -ECONNRESET: 18538a2cec29STrond Myklebust case -ENETUNREACH: 18548a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 18552a491991STrond Myklebust case 0: 185668e220bdSChuck Lever case -EINPROGRESS: 185768e220bdSChuck Lever case -EALREADY: 1858*7d1e8255STrond Myklebust xprt_clear_connecting(xprt); 1859*7d1e8255STrond Myklebust return; 18608a2cec29STrond Myklebust } 186168e220bdSChuck Lever /* get rid of existing socket, and retry */ 1862e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 18632a491991STrond Myklebust printk("%s: connect returned unhandled error %d\n", 18642a491991STrond Myklebust __func__, status); 1865*7d1e8255STrond Myklebust out_eagain: 18662a491991STrond Myklebust status = -EAGAIN; 186768e220bdSChuck Lever out: 186868e220bdSChuck Lever xprt_clear_connecting(xprt); 1869*7d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 187068e220bdSChuck Lever } 187168e220bdSChuck Lever 187268e220bdSChuck Lever /** 18739903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 18749903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 18759903cd1cSChuck Lever * 18769903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 187703bf4b70SChuck Lever * 187803bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 187903bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 188003bf4b70SChuck Lever * socket on a privileged port. 188103bf4b70SChuck Lever * 188203bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 188303bf4b70SChuck Lever * retry floods (hard mounts). 18849903cd1cSChuck Lever */ 18859903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 1886a246b010SChuck Lever { 1887a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1888ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1889a246b010SChuck Lever 1890b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1891b0d93ad5SChuck Lever return; 1892b0d93ad5SChuck Lever 1893ee0ac0c2SChuck Lever if (transport->sock != NULL) { 189446121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 189546121cf7SChuck Lever "seconds\n", 189603bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 1897c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1898c1384c9cSTrond Myklebust &transport->connect_worker, 189903bf4b70SChuck Lever xprt->reestablish_timeout); 190003bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 190103bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 190203bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 19039903cd1cSChuck Lever } else { 19049903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 1905c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1906c1384c9cSTrond Myklebust &transport->connect_worker, 0); 1907a246b010SChuck Lever } 1908a246b010SChuck Lever } 1909a246b010SChuck Lever 1910e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task) 1911e06799f9STrond Myklebust { 1912e06799f9STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1913e06799f9STrond Myklebust 1914e06799f9STrond Myklebust /* Exit if we need to wait for socket shutdown to complete */ 1915e06799f9STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 1916e06799f9STrond Myklebust return; 1917e06799f9STrond Myklebust xs_connect(task); 1918e06799f9STrond Myklebust } 1919e06799f9STrond Myklebust 1920262ca07dSChuck Lever /** 1921262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 1922262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1923262ca07dSChuck Lever * @seq: output file 1924262ca07dSChuck Lever * 1925262ca07dSChuck Lever */ 1926262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1927262ca07dSChuck Lever { 1928c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1929c8475461SChuck Lever 1930262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 1931c8475461SChuck Lever transport->port, 1932262ca07dSChuck Lever xprt->stat.bind_count, 1933262ca07dSChuck Lever xprt->stat.sends, 1934262ca07dSChuck Lever xprt->stat.recvs, 1935262ca07dSChuck Lever xprt->stat.bad_xids, 1936262ca07dSChuck Lever xprt->stat.req_u, 1937262ca07dSChuck Lever xprt->stat.bklog_u); 1938262ca07dSChuck Lever } 1939262ca07dSChuck Lever 1940262ca07dSChuck Lever /** 1941262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 1942262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1943262ca07dSChuck Lever * @seq: output file 1944262ca07dSChuck Lever * 1945262ca07dSChuck Lever */ 1946262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1947262ca07dSChuck Lever { 1948c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1949262ca07dSChuck Lever long idle_time = 0; 1950262ca07dSChuck Lever 1951262ca07dSChuck Lever if (xprt_connected(xprt)) 1952262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 1953262ca07dSChuck Lever 1954262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 1955c8475461SChuck Lever transport->port, 1956262ca07dSChuck Lever xprt->stat.bind_count, 1957262ca07dSChuck Lever xprt->stat.connect_count, 1958262ca07dSChuck Lever xprt->stat.connect_time, 1959262ca07dSChuck Lever idle_time, 1960262ca07dSChuck Lever xprt->stat.sends, 1961262ca07dSChuck Lever xprt->stat.recvs, 1962262ca07dSChuck Lever xprt->stat.bad_xids, 1963262ca07dSChuck Lever xprt->stat.req_u, 1964262ca07dSChuck Lever xprt->stat.bklog_u); 1965262ca07dSChuck Lever } 1966262ca07dSChuck Lever 1967262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 196843118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 196912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 197049e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 197145160d62SChuck Lever .rpcbind = rpcb_getport_async, 197292200412SChuck Lever .set_port = xs_set_port, 19739903cd1cSChuck Lever .connect = xs_connect, 197402107148SChuck Lever .buf_alloc = rpc_malloc, 197502107148SChuck Lever .buf_free = rpc_free, 1976262965f5SChuck Lever .send_request = xs_udp_send_request, 1977fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 197846c0ee8bSChuck Lever .timer = xs_udp_timer, 1979a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 1980262965f5SChuck Lever .close = xs_close, 1981262965f5SChuck Lever .destroy = xs_destroy, 1982262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 1983262965f5SChuck Lever }; 1984262965f5SChuck Lever 1985262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 198612a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 1987e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 198845160d62SChuck Lever .rpcbind = rpcb_getport_async, 198992200412SChuck Lever .set_port = xs_set_port, 1990e06799f9STrond Myklebust .connect = xs_tcp_connect, 199102107148SChuck Lever .buf_alloc = rpc_malloc, 199202107148SChuck Lever .buf_free = rpc_free, 1993262965f5SChuck Lever .send_request = xs_tcp_send_request, 1994fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 1995e06799f9STrond Myklebust .close = xs_tcp_shutdown, 19969903cd1cSChuck Lever .destroy = xs_destroy, 1997262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 1998a246b010SChuck Lever }; 1999a246b010SChuck Lever 20003c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2001bc25571eS\"Talpey, Thomas\ unsigned int slot_table_size) 2002c8541ecdSChuck Lever { 2003c8541ecdSChuck Lever struct rpc_xprt *xprt; 2004ffc2e518SChuck Lever struct sock_xprt *new; 2005c8541ecdSChuck Lever 200696802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2007c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2008c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2009c8541ecdSChuck Lever } 2010c8541ecdSChuck Lever 2011ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 2012ffc2e518SChuck Lever if (new == NULL) { 201346121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 201446121cf7SChuck Lever "rpc_xprt\n"); 2015c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2016c8541ecdSChuck Lever } 2017ffc2e518SChuck Lever xprt = &new->xprt; 2018c8541ecdSChuck Lever 2019c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 2020c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 2021c8541ecdSChuck Lever if (xprt->slot == NULL) { 2022c8541ecdSChuck Lever kfree(xprt); 202346121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 202446121cf7SChuck Lever "table\n"); 2025c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2026c8541ecdSChuck Lever } 2027c8541ecdSChuck Lever 202896802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 202996802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2030d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2031d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 2032c8541ecdSChuck Lever 2033c8541ecdSChuck Lever return xprt; 2034c8541ecdSChuck Lever } 2035c8541ecdSChuck Lever 20362881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 20372881ae74STrond Myklebust .to_initval = 5 * HZ, 20382881ae74STrond Myklebust .to_maxval = 30 * HZ, 20392881ae74STrond Myklebust .to_increment = 5 * HZ, 20402881ae74STrond Myklebust .to_retries = 5, 20412881ae74STrond Myklebust }; 20422881ae74STrond Myklebust 20439903cd1cSChuck Lever /** 20449903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 204596802a09SFrank van Maarseveen * @args: rpc transport creation arguments 20469903cd1cSChuck Lever * 20479903cd1cSChuck Lever */ 2048483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2049a246b010SChuck Lever { 20508f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2051c8541ecdSChuck Lever struct rpc_xprt *xprt; 2052c8475461SChuck Lever struct sock_xprt *transport; 2053a246b010SChuck Lever 205496802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 2055c8541ecdSChuck Lever if (IS_ERR(xprt)) 2056c8541ecdSChuck Lever return xprt; 2057c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2058a246b010SChuck Lever 2059ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2060808012fbSChuck Lever xprt->tsh_size = 0; 2061a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2062a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2063a246b010SChuck Lever 206403bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 206503bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 206603bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 206703bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2068a246b010SChuck Lever 2069262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2070a246b010SChuck Lever 2071ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_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, 20798f9d5b1aSChuck Lever xs_udp_connect_worker4); 2080b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 20818f9d5b1aSChuck Lever break; 20828f9d5b1aSChuck Lever case AF_INET6: 20838f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 20848f9d5b1aSChuck Lever xprt_set_bound(xprt); 20858f9d5b1aSChuck Lever 20868f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 20878f9d5b1aSChuck Lever xs_udp_connect_worker6); 2088b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 20898f9d5b1aSChuck Lever break; 20908f9d5b1aSChuck Lever default: 20918f9d5b1aSChuck Lever kfree(xprt); 20928f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 20938f9d5b1aSChuck Lever } 20948f9d5b1aSChuck Lever 2095edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 20967559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2097edb267a6SChuck Lever 2098bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2099c8541ecdSChuck Lever return xprt; 2100bc25571eS\"Talpey, Thomas\ 2101bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2102bc25571eS\"Talpey, Thomas\ kfree(xprt); 2103bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2104a246b010SChuck Lever } 2105a246b010SChuck Lever 21062881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 21072881ae74STrond Myklebust .to_initval = 60 * HZ, 21082881ae74STrond Myklebust .to_maxval = 60 * HZ, 21092881ae74STrond Myklebust .to_retries = 2, 21102881ae74STrond Myklebust }; 21112881ae74STrond Myklebust 21129903cd1cSChuck Lever /** 21139903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 211496802a09SFrank van Maarseveen * @args: rpc transport creation arguments 21159903cd1cSChuck Lever * 21169903cd1cSChuck Lever */ 2117483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2118a246b010SChuck Lever { 21198f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2120c8541ecdSChuck Lever struct rpc_xprt *xprt; 2121c8475461SChuck Lever struct sock_xprt *transport; 2122a246b010SChuck Lever 212396802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 2124c8541ecdSChuck Lever if (IS_ERR(xprt)) 2125c8541ecdSChuck Lever return xprt; 2126c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2127a246b010SChuck Lever 2128ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2129808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2130808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2131a246b010SChuck Lever 213203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 213303bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 213403bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 213503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2136a246b010SChuck Lever 2137262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2138ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2139a246b010SChuck Lever 21408f9d5b1aSChuck Lever switch (addr->sa_family) { 21418f9d5b1aSChuck Lever case AF_INET: 21428f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 21438f9d5b1aSChuck Lever xprt_set_bound(xprt); 21448f9d5b1aSChuck Lever 21458f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); 2146b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 21478f9d5b1aSChuck Lever break; 21488f9d5b1aSChuck Lever case AF_INET6: 21498f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 21508f9d5b1aSChuck Lever xprt_set_bound(xprt); 21518f9d5b1aSChuck Lever 21528f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); 2153b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 21548f9d5b1aSChuck Lever break; 21558f9d5b1aSChuck Lever default: 21568f9d5b1aSChuck Lever kfree(xprt); 21578f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 21588f9d5b1aSChuck Lever } 21598f9d5b1aSChuck Lever 2160edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 21617559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2162edb267a6SChuck Lever 2163bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2164c8541ecdSChuck Lever return xprt; 2165bc25571eS\"Talpey, Thomas\ 2166bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2167bc25571eS\"Talpey, Thomas\ kfree(xprt); 2168bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2169a246b010SChuck Lever } 2170282b32e1SChuck Lever 2171bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 2172bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 2173bc25571eS\"Talpey, Thomas\ .name = "udp", 2174bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21754fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_UDP, 2176bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 2177bc25571eS\"Talpey, Thomas\ }; 2178bc25571eS\"Talpey, Thomas\ 2179bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 2180bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 2181bc25571eS\"Talpey, Thomas\ .name = "tcp", 2182bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21834fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_TCP, 2184bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 2185bc25571eS\"Talpey, Thomas\ }; 2186bc25571eS\"Talpey, Thomas\ 2187282b32e1SChuck Lever /** 2188bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 2189282b32e1SChuck Lever * 2190282b32e1SChuck Lever */ 2191282b32e1SChuck Lever int init_socket_xprt(void) 2192282b32e1SChuck Lever { 2193fbf76683SChuck Lever #ifdef RPC_DEBUG 21942b1bec5fSEric W. Biederman if (!sunrpc_table_header) 21950b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 2196fbf76683SChuck Lever #endif 2197fbf76683SChuck Lever 2198bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 2199bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 2200bc25571eS\"Talpey, Thomas\ 2201282b32e1SChuck Lever return 0; 2202282b32e1SChuck Lever } 2203282b32e1SChuck Lever 2204282b32e1SChuck Lever /** 2205bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 2206282b32e1SChuck Lever * 2207282b32e1SChuck Lever */ 2208282b32e1SChuck Lever void cleanup_socket_xprt(void) 2209282b32e1SChuck Lever { 2210fbf76683SChuck Lever #ifdef RPC_DEBUG 2211fbf76683SChuck Lever if (sunrpc_table_header) { 2212fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 2213fbf76683SChuck Lever sunrpc_table_header = NULL; 2214fbf76683SChuck Lever } 2215fbf76683SChuck Lever #endif 2216bc25571eS\"Talpey, Thomas\ 2217bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 2218bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 2219282b32e1SChuck Lever } 2220