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 527d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 5325fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 547d1e8255STrond Myklebust 55c556b754SChuck Lever /* 56fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 57fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 58fbf76683SChuck Lever * directory become the union of all files registered there. 59fbf76683SChuck Lever * 60fbf76683SChuck Lever * We simply need to make sure that we don't collide with 61fbf76683SChuck Lever * someone else's file names! 62fbf76683SChuck Lever */ 63fbf76683SChuck Lever 64fbf76683SChuck Lever #ifdef RPC_DEBUG 65fbf76683SChuck Lever 66fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 67fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 68fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 69fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 70fbf76683SChuck Lever 71fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 72fbf76683SChuck Lever 73fbf76683SChuck Lever /* 74fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 75fbf76683SChuck Lever * socket buffers for existing UDP transports 76fbf76683SChuck Lever */ 77fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 78fbf76683SChuck Lever { 79fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_UDP, 80fbf76683SChuck Lever .procname = "udp_slot_table_entries", 81fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 82fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 83fbf76683SChuck Lever .mode = 0644, 84fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 85fbf76683SChuck Lever .strategy = &sysctl_intvec, 86fbf76683SChuck Lever .extra1 = &min_slot_table_size, 87fbf76683SChuck Lever .extra2 = &max_slot_table_size 88fbf76683SChuck Lever }, 89fbf76683SChuck Lever { 90fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_TCP, 91fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 92fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 93fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 94fbf76683SChuck Lever .mode = 0644, 95fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 96fbf76683SChuck Lever .strategy = &sysctl_intvec, 97fbf76683SChuck Lever .extra1 = &min_slot_table_size, 98fbf76683SChuck Lever .extra2 = &max_slot_table_size 99fbf76683SChuck Lever }, 100fbf76683SChuck Lever { 101fbf76683SChuck Lever .ctl_name = CTL_MIN_RESVPORT, 102fbf76683SChuck Lever .procname = "min_resvport", 103fbf76683SChuck Lever .data = &xprt_min_resvport, 104fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 105fbf76683SChuck Lever .mode = 0644, 106fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 107fbf76683SChuck Lever .strategy = &sysctl_intvec, 108fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 109fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 110fbf76683SChuck Lever }, 111fbf76683SChuck Lever { 112fbf76683SChuck Lever .ctl_name = CTL_MAX_RESVPORT, 113fbf76683SChuck Lever .procname = "max_resvport", 114fbf76683SChuck Lever .data = &xprt_max_resvport, 115fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 116fbf76683SChuck Lever .mode = 0644, 117fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 118fbf76683SChuck Lever .strategy = &sysctl_intvec, 119fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 120fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 121fbf76683SChuck Lever }, 122fbf76683SChuck Lever { 12325fe6142STrond Myklebust .procname = "tcp_fin_timeout", 12425fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 12525fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 12625fe6142STrond Myklebust .mode = 0644, 12725fe6142STrond Myklebust .proc_handler = &proc_dointvec_jiffies, 12825fe6142STrond Myklebust .strategy = sysctl_jiffies 12925fe6142STrond Myklebust }, 13025fe6142STrond Myklebust { 131fbf76683SChuck Lever .ctl_name = 0, 132fbf76683SChuck Lever }, 133fbf76683SChuck Lever }; 134fbf76683SChuck Lever 135fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 136fbf76683SChuck Lever { 137fbf76683SChuck Lever .ctl_name = CTL_SUNRPC, 138fbf76683SChuck Lever .procname = "sunrpc", 139fbf76683SChuck Lever .mode = 0555, 140fbf76683SChuck Lever .child = xs_tunables_table 141fbf76683SChuck Lever }, 142fbf76683SChuck Lever { 143fbf76683SChuck Lever .ctl_name = 0, 144fbf76683SChuck Lever }, 145fbf76683SChuck Lever }; 146fbf76683SChuck Lever 147fbf76683SChuck Lever #endif 148fbf76683SChuck Lever 149fbf76683SChuck Lever /* 15003bf4b70SChuck Lever * Time out for an RPC UDP socket connect. UDP socket connects are 15103bf4b70SChuck Lever * synchronous, but we set a timeout anyway in case of resource 15203bf4b70SChuck Lever * exhaustion on the local host. 15303bf4b70SChuck Lever */ 15403bf4b70SChuck Lever #define XS_UDP_CONN_TO (5U * HZ) 15503bf4b70SChuck Lever 15603bf4b70SChuck Lever /* 15703bf4b70SChuck Lever * Wait duration for an RPC TCP connection to be established. Solaris 15803bf4b70SChuck Lever * NFS over TCP uses 60 seconds, for example, which is in line with how 15903bf4b70SChuck Lever * long a server takes to reboot. 16003bf4b70SChuck Lever */ 16103bf4b70SChuck Lever #define XS_TCP_CONN_TO (60U * HZ) 16203bf4b70SChuck Lever 16303bf4b70SChuck Lever /* 16403bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16503bf4b70SChuck Lever */ 16603bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16703bf4b70SChuck Lever 16803bf4b70SChuck Lever /* 16903bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 17003bf4b70SChuck Lever * kind of resource problem on the local host. 17103bf4b70SChuck Lever */ 17203bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17303bf4b70SChuck Lever 17403bf4b70SChuck Lever /* 17503bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17603bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17703bf4b70SChuck Lever * 17803bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17903bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 18003bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18103bf4b70SChuck Lever * increase over time if the server is down or not responding. 18203bf4b70SChuck Lever */ 18303bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18403bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 18503bf4b70SChuck Lever 18603bf4b70SChuck Lever /* 18703bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18803bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18903bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 19003bf4b70SChuck Lever */ 19103bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19203bf4b70SChuck Lever 193a246b010SChuck Lever #ifdef RPC_DEBUG 194a246b010SChuck Lever # undef RPC_DEBUG_DATA 1959903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 196a246b010SChuck Lever #endif 197a246b010SChuck Lever 198a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1999903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 200a246b010SChuck Lever { 201a246b010SChuck Lever u8 *buf = (u8 *) packet; 202a246b010SChuck Lever int j; 203a246b010SChuck Lever 204a246b010SChuck Lever dprintk("RPC: %s\n", msg); 205a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 206a246b010SChuck Lever if (!(j & 31)) { 207a246b010SChuck Lever if (j) 208a246b010SChuck Lever dprintk("\n"); 209a246b010SChuck Lever dprintk("0x%04x ", j); 210a246b010SChuck Lever } 211a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 212a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 213a246b010SChuck Lever } 214a246b010SChuck Lever dprintk("\n"); 215a246b010SChuck Lever } 216a246b010SChuck Lever #else 2179903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 218a246b010SChuck Lever { 219a246b010SChuck Lever /* NOP */ 220a246b010SChuck Lever } 221a246b010SChuck Lever #endif 222a246b010SChuck Lever 223ffc2e518SChuck Lever struct sock_xprt { 224ffc2e518SChuck Lever struct rpc_xprt xprt; 225ee0ac0c2SChuck Lever 226ee0ac0c2SChuck Lever /* 227ee0ac0c2SChuck Lever * Network layer 228ee0ac0c2SChuck Lever */ 229ee0ac0c2SChuck Lever struct socket * sock; 230ee0ac0c2SChuck Lever struct sock * inet; 23151971139SChuck Lever 23251971139SChuck Lever /* 23351971139SChuck Lever * State of TCP reply receive 23451971139SChuck Lever */ 23551971139SChuck Lever __be32 tcp_fraghdr, 23651971139SChuck Lever tcp_xid; 23751971139SChuck Lever 23851971139SChuck Lever u32 tcp_offset, 23951971139SChuck Lever tcp_reclen; 24051971139SChuck Lever 24151971139SChuck Lever unsigned long tcp_copied, 24251971139SChuck Lever tcp_flags; 243c8475461SChuck Lever 244c8475461SChuck Lever /* 245c8475461SChuck Lever * Connection of transports 246c8475461SChuck Lever */ 24734161db6STrond Myklebust struct delayed_work connect_worker; 248d3bc9a1dSFrank van Maarseveen struct sockaddr_storage addr; 249c8475461SChuck Lever unsigned short port; 2507c6e066eSChuck Lever 2517c6e066eSChuck Lever /* 2527c6e066eSChuck Lever * UDP socket buffer size parameters 2537c6e066eSChuck Lever */ 2547c6e066eSChuck Lever size_t rcvsize, 2557c6e066eSChuck Lever sndsize; 256314dfd79SChuck Lever 257314dfd79SChuck Lever /* 258314dfd79SChuck Lever * Saved socket callback addresses 259314dfd79SChuck Lever */ 260314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 261314dfd79SChuck Lever void (*old_state_change)(struct sock *); 262314dfd79SChuck Lever void (*old_write_space)(struct sock *); 2632a9e1cfaSTrond Myklebust void (*old_error_report)(struct sock *); 264ffc2e518SChuck Lever }; 265ffc2e518SChuck Lever 266e136d092SChuck Lever /* 267e136d092SChuck Lever * TCP receive state flags 268e136d092SChuck Lever */ 269e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 270e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 271e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 272e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 273e136d092SChuck Lever 27495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 275edb267a6SChuck Lever { 27695392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 27795392c59SChuck Lever } 27895392c59SChuck Lever 27995392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 28095392c59SChuck Lever { 28195392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 28295392c59SChuck Lever } 28395392c59SChuck Lever 28495392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 28595392c59SChuck Lever { 28695392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 28795392c59SChuck Lever } 28895392c59SChuck Lever 289b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt, 290b454ae90SChuck Lever const char *protocol, 291b454ae90SChuck Lever const char *netid) 292edb267a6SChuck Lever { 29395392c59SChuck Lever struct sockaddr_in *addr = xs_addr_in(xprt); 294edb267a6SChuck Lever char *buf; 295edb267a6SChuck Lever 296edb267a6SChuck Lever buf = kzalloc(20, GFP_KERNEL); 297edb267a6SChuck Lever if (buf) { 298e0db4a78SDavid S. Miller snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr); 299edb267a6SChuck Lever } 300edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 301edb267a6SChuck Lever 302edb267a6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 303edb267a6SChuck Lever if (buf) { 304edb267a6SChuck Lever snprintf(buf, 8, "%u", 305edb267a6SChuck Lever ntohs(addr->sin_port)); 306edb267a6SChuck Lever } 307edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 308edb267a6SChuck Lever 309b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 310edb267a6SChuck Lever 311edb267a6SChuck Lever buf = kzalloc(48, GFP_KERNEL); 312edb267a6SChuck Lever if (buf) { 31321454aaaSHarvey Harrison snprintf(buf, 48, "addr=%pI4 port=%u proto=%s", 31421454aaaSHarvey Harrison &addr->sin_addr.s_addr, 315edb267a6SChuck Lever ntohs(addr->sin_port), 316b454ae90SChuck Lever protocol); 317edb267a6SChuck Lever } 318edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 319fbfe3cc6SChuck Lever 320fbfe3cc6SChuck Lever buf = kzalloc(10, GFP_KERNEL); 321fbfe3cc6SChuck Lever if (buf) { 322fbfe3cc6SChuck Lever snprintf(buf, 10, "%02x%02x%02x%02x", 323fbfe3cc6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 324fbfe3cc6SChuck Lever } 325fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 326fbfe3cc6SChuck Lever 327fbfe3cc6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 328fbfe3cc6SChuck Lever if (buf) { 329fbfe3cc6SChuck Lever snprintf(buf, 8, "%4hx", 330fbfe3cc6SChuck Lever ntohs(addr->sin_port)); 331fbfe3cc6SChuck Lever } 332fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 333756805e7SChuck Lever 334756805e7SChuck Lever buf = kzalloc(30, GFP_KERNEL); 335756805e7SChuck Lever if (buf) { 33621454aaaSHarvey Harrison snprintf(buf, 30, "%pI4.%u.%u", 33721454aaaSHarvey Harrison &addr->sin_addr.s_addr, 338756805e7SChuck Lever ntohs(addr->sin_port) >> 8, 339756805e7SChuck Lever ntohs(addr->sin_port) & 0xff); 340756805e7SChuck Lever } 341756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3424417c8c4S\"Talpey, Thomas\ 343b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 344edb267a6SChuck Lever } 345edb267a6SChuck Lever 346b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt, 347b454ae90SChuck Lever const char *protocol, 348b454ae90SChuck Lever const char *netid) 3494b6473fbSChuck Lever { 35095392c59SChuck Lever struct sockaddr_in6 *addr = xs_addr_in6(xprt); 3514b6473fbSChuck Lever char *buf; 3524b6473fbSChuck Lever 3534b6473fbSChuck Lever buf = kzalloc(40, GFP_KERNEL); 3544b6473fbSChuck Lever if (buf) { 3555b095d98SHarvey Harrison snprintf(buf, 40, "%pI6",&addr->sin6_addr); 3564b6473fbSChuck Lever } 3574b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 3584b6473fbSChuck Lever 3594b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3604b6473fbSChuck Lever if (buf) { 3614b6473fbSChuck Lever snprintf(buf, 8, "%u", 3624b6473fbSChuck Lever ntohs(addr->sin6_port)); 3634b6473fbSChuck Lever } 3644b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 3654b6473fbSChuck Lever 366b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 3674b6473fbSChuck Lever 3684b6473fbSChuck Lever buf = kzalloc(64, GFP_KERNEL); 3694b6473fbSChuck Lever if (buf) { 3705b095d98SHarvey Harrison snprintf(buf, 64, "addr=%pI6 port=%u proto=%s", 371fdb46ee7SHarvey Harrison &addr->sin6_addr, 3724b6473fbSChuck Lever ntohs(addr->sin6_port), 373b454ae90SChuck Lever protocol); 3744b6473fbSChuck Lever } 3754b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 3764b6473fbSChuck Lever 3774b6473fbSChuck Lever buf = kzalloc(36, GFP_KERNEL); 378b071195dSHarvey Harrison if (buf) 3794b7a4274SHarvey Harrison snprintf(buf, 36, "%pi6", &addr->sin6_addr); 380b071195dSHarvey Harrison 3814b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 3824b6473fbSChuck Lever 3834b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3844b6473fbSChuck Lever if (buf) { 3854b6473fbSChuck Lever snprintf(buf, 8, "%4hx", 3864b6473fbSChuck Lever ntohs(addr->sin6_port)); 3874b6473fbSChuck Lever } 3884b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 389756805e7SChuck Lever 390756805e7SChuck Lever buf = kzalloc(50, GFP_KERNEL); 391756805e7SChuck Lever if (buf) { 3925b095d98SHarvey Harrison snprintf(buf, 50, "%pI6.%u.%u", 393fdb46ee7SHarvey Harrison &addr->sin6_addr, 394756805e7SChuck Lever ntohs(addr->sin6_port) >> 8, 395756805e7SChuck Lever ntohs(addr->sin6_port) & 0xff); 396756805e7SChuck Lever } 397756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 3984417c8c4S\"Talpey, Thomas\ 399b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 400edb267a6SChuck Lever } 401edb267a6SChuck Lever 402edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 403edb267a6SChuck Lever { 40433e01dc7SChuck Lever unsigned int i; 40533e01dc7SChuck Lever 40633e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 40733e01dc7SChuck Lever switch (i) { 40833e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 40933e01dc7SChuck Lever case RPC_DISPLAY_NETID: 41033e01dc7SChuck Lever continue; 41133e01dc7SChuck Lever default: 41233e01dc7SChuck Lever kfree(xprt->address_strings[i]); 41333e01dc7SChuck Lever } 414edb267a6SChuck Lever } 415edb267a6SChuck Lever 416b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 417b4b5cc85SChuck Lever 41824c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 419b4b5cc85SChuck Lever { 420b4b5cc85SChuck Lever struct msghdr msg = { 421b4b5cc85SChuck Lever .msg_name = addr, 422b4b5cc85SChuck Lever .msg_namelen = addrlen, 42324c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 42424c5684bSTrond Myklebust }; 42524c5684bSTrond Myklebust struct kvec iov = { 42624c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 42724c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 428b4b5cc85SChuck Lever }; 429b4b5cc85SChuck Lever 43024c5684bSTrond Myklebust if (iov.iov_len != 0) 431b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 432b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 433b4b5cc85SChuck Lever } 434b4b5cc85SChuck Lever 43524c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 436b4b5cc85SChuck Lever { 43724c5684bSTrond Myklebust struct page **ppage; 43824c5684bSTrond Myklebust unsigned int remainder; 43924c5684bSTrond Myklebust int err, sent = 0; 440b4b5cc85SChuck Lever 44124c5684bSTrond Myklebust remainder = xdr->page_len - base; 44224c5684bSTrond Myklebust base += xdr->page_base; 44324c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 44424c5684bSTrond Myklebust base &= ~PAGE_MASK; 44524c5684bSTrond Myklebust for(;;) { 44624c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 44724c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 44824c5684bSTrond Myklebust 44924c5684bSTrond Myklebust remainder -= len; 45024c5684bSTrond Myklebust if (remainder != 0 || more) 45124c5684bSTrond Myklebust flags |= MSG_MORE; 45224c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 45324c5684bSTrond Myklebust if (remainder == 0 || err != len) 45424c5684bSTrond Myklebust break; 45524c5684bSTrond Myklebust sent += err; 45624c5684bSTrond Myklebust ppage++; 45724c5684bSTrond Myklebust base = 0; 45824c5684bSTrond Myklebust } 45924c5684bSTrond Myklebust if (sent == 0) 46024c5684bSTrond Myklebust return err; 46124c5684bSTrond Myklebust if (err > 0) 46224c5684bSTrond Myklebust sent += err; 46324c5684bSTrond Myklebust return sent; 464b4b5cc85SChuck Lever } 465b4b5cc85SChuck Lever 4669903cd1cSChuck Lever /** 4679903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 4689903cd1cSChuck Lever * @sock: socket to send on 4699903cd1cSChuck Lever * @addr: UDP only -- address of destination 4709903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 4719903cd1cSChuck Lever * @xdr: buffer containing this request 4729903cd1cSChuck Lever * @base: starting position in the buffer 4739903cd1cSChuck Lever * 474a246b010SChuck Lever */ 47524c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 476a246b010SChuck Lever { 47724c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 47824c5684bSTrond Myklebust int err, sent = 0; 479a246b010SChuck Lever 480262965f5SChuck Lever if (unlikely(!sock)) 481fba91afbSTrond Myklebust return -ENOTSOCK; 482262965f5SChuck Lever 483262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 48424c5684bSTrond Myklebust if (base != 0) { 48524c5684bSTrond Myklebust addr = NULL; 48624c5684bSTrond Myklebust addrlen = 0; 48724c5684bSTrond Myklebust } 488262965f5SChuck Lever 48924c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 49024c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 49124c5684bSTrond Myklebust remainder -= len; 49224c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 49324c5684bSTrond Myklebust if (remainder == 0 || err != len) 494a246b010SChuck Lever goto out; 49524c5684bSTrond Myklebust sent += err; 496a246b010SChuck Lever base = 0; 497a246b010SChuck Lever } else 49824c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 499a246b010SChuck Lever 50024c5684bSTrond Myklebust if (base < xdr->page_len) { 50124c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 50224c5684bSTrond Myklebust remainder -= len; 50324c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 50424c5684bSTrond Myklebust if (remainder == 0 || err != len) 505a246b010SChuck Lever goto out; 50624c5684bSTrond Myklebust sent += err; 507a246b010SChuck Lever base = 0; 50824c5684bSTrond Myklebust } else 50924c5684bSTrond Myklebust base -= xdr->page_len; 51024c5684bSTrond Myklebust 51124c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 51224c5684bSTrond Myklebust return sent; 51324c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 514a246b010SChuck Lever out: 51524c5684bSTrond Myklebust if (sent == 0) 51624c5684bSTrond Myklebust return err; 51724c5684bSTrond Myklebust if (err > 0) 51824c5684bSTrond Myklebust sent += err; 51924c5684bSTrond Myklebust return sent; 520a246b010SChuck Lever } 521a246b010SChuck Lever 522b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 523b6ddf64fSTrond Myklebust { 524b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 525b6ddf64fSTrond Myklebust 526b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 527b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 528b6ddf64fSTrond Myklebust } 529b6ddf64fSTrond Myklebust 5309903cd1cSChuck Lever /** 531262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 532262965f5SChuck Lever * @task: task to put to sleep 5339903cd1cSChuck Lever * 534a246b010SChuck Lever */ 5355e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 536a246b010SChuck Lever { 537262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 538262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 539ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 5405e3771ceSTrond Myklebust int ret = 0; 541a246b010SChuck Lever 54246121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 543262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 544262965f5SChuck Lever req->rq_slen); 545a246b010SChuck Lever 546262965f5SChuck Lever /* Protect against races with write_space */ 547262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 548a246b010SChuck Lever 549262965f5SChuck Lever /* Don't race with disconnect */ 550b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 551b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 5525e3771ceSTrond Myklebust ret = -EAGAIN; 553b6ddf64fSTrond Myklebust /* 554b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 555b6ddf64fSTrond Myklebust * window size 556b6ddf64fSTrond Myklebust */ 557b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 558b6ddf64fSTrond Myklebust transport->inet->sk_write_pending++; 559b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 560b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 561b6ddf64fSTrond Myklebust } 562b6ddf64fSTrond Myklebust } else { 563b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 5645e3771ceSTrond Myklebust ret = -ENOTCONN; 565b6ddf64fSTrond Myklebust } 566a246b010SChuck Lever 567262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5685e3771ceSTrond Myklebust return ret; 569a246b010SChuck Lever } 570a246b010SChuck Lever 5719903cd1cSChuck Lever /** 572262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5739903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5749903cd1cSChuck Lever * 5759903cd1cSChuck Lever * Return values: 5769903cd1cSChuck Lever * 0: The request has been sent 5779903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5789903cd1cSChuck Lever * complete the request 579262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 5809903cd1cSChuck Lever * other: Some other error occured, the request was not sent 5819903cd1cSChuck Lever */ 582262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 583a246b010SChuck Lever { 584a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 585a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 586ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 587262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 588262965f5SChuck Lever int status; 589262965f5SChuck Lever 590262965f5SChuck Lever xs_pktdump("packet data:", 591262965f5SChuck Lever req->rq_svec->iov_base, 592262965f5SChuck Lever req->rq_svec->iov_len); 593262965f5SChuck Lever 59401d37c42STrond Myklebust if (!xprt_bound(xprt)) 59501d37c42STrond Myklebust return -ENOTCONN; 596ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 59795392c59SChuck Lever xs_addr(xprt), 598ee0ac0c2SChuck Lever xprt->addrlen, xdr, 599ee0ac0c2SChuck Lever req->rq_bytes_sent); 600262965f5SChuck Lever 601262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 602262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 603262965f5SChuck Lever 6042199700fSTrond Myklebust if (status >= 0) { 6051321d8d9SChuck Lever task->tk_bytes_sent += status; 6062199700fSTrond Myklebust if (status >= req->rq_slen) 607262965f5SChuck Lever return 0; 608262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 609262965f5SChuck Lever status = -EAGAIN; 6102199700fSTrond Myklebust } 611c8485e4dSTrond Myklebust if (!transport->sock) 612c8485e4dSTrond Myklebust goto out; 613262965f5SChuck Lever 614262965f5SChuck Lever switch (status) { 615fba91afbSTrond Myklebust case -ENOTSOCK: 616fba91afbSTrond Myklebust status = -ENOTCONN; 617fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 618fba91afbSTrond Myklebust break; 619b6ddf64fSTrond Myklebust case -EAGAIN: 6205e3771ceSTrond Myklebust status = xs_nospace(task); 621b6ddf64fSTrond Myklebust break; 622c8485e4dSTrond Myklebust default: 623c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 624c8485e4dSTrond Myklebust -status); 625262965f5SChuck Lever case -ENETUNREACH: 626262965f5SChuck Lever case -EPIPE: 627262965f5SChuck Lever case -ECONNREFUSED: 628262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 629262965f5SChuck Lever * prompts ECONNREFUSED. */ 630b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 631262965f5SChuck Lever } 632c8485e4dSTrond Myklebust out: 633262965f5SChuck Lever return status; 634262965f5SChuck Lever } 635262965f5SChuck Lever 636e06799f9STrond Myklebust /** 637e06799f9STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 638e06799f9STrond Myklebust * @xprt: transport 639e06799f9STrond Myklebust * 640e06799f9STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 641e06799f9STrond Myklebust * equivalent of shutdown(SHUT_WR); 642e06799f9STrond Myklebust */ 643e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 644e06799f9STrond Myklebust { 645e06799f9STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 646e06799f9STrond Myklebust struct socket *sock = transport->sock; 647e06799f9STrond Myklebust 648e06799f9STrond Myklebust if (sock != NULL) 649e06799f9STrond Myklebust kernel_sock_shutdown(sock, SHUT_WR); 650e06799f9STrond Myklebust } 651e06799f9STrond Myklebust 652808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 653808012fbSChuck Lever { 654808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 655808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 656808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 657808012fbSChuck Lever } 658808012fbSChuck Lever 659262965f5SChuck Lever /** 660262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 661262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 662262965f5SChuck Lever * 663262965f5SChuck Lever * Return values: 664262965f5SChuck Lever * 0: The request has been sent 665262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 666262965f5SChuck Lever * complete the request 667262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 668262965f5SChuck Lever * other: Some other error occured, the request was not sent 669262965f5SChuck Lever * 670262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 671262965f5SChuck Lever * if sendmsg is not able to make progress? 672262965f5SChuck Lever */ 673262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 674262965f5SChuck Lever { 675262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 676262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 677ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 678262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 679b595bb15SChuck Lever int status; 680a246b010SChuck Lever 681808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 682262965f5SChuck Lever 683262965f5SChuck Lever xs_pktdump("packet data:", 684262965f5SChuck Lever req->rq_svec->iov_base, 685262965f5SChuck Lever req->rq_svec->iov_len); 686a246b010SChuck Lever 687a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 688a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 689262965f5SChuck Lever * called sendmsg(). */ 690a246b010SChuck Lever while (1) { 691ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 692ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 693a246b010SChuck Lever 694262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 695262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 696262965f5SChuck Lever 697262965f5SChuck Lever if (unlikely(status < 0)) 698a246b010SChuck Lever break; 699a246b010SChuck Lever 700a246b010SChuck Lever /* If we've sent the entire packet, immediately 701a246b010SChuck Lever * reset the count of bytes sent. */ 702262965f5SChuck Lever req->rq_bytes_sent += status; 703ef759a2eSChuck Lever task->tk_bytes_sent += status; 704262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 705a246b010SChuck Lever req->rq_bytes_sent = 0; 706a246b010SChuck Lever return 0; 707a246b010SChuck Lever } 708262965f5SChuck Lever 70906b4b681STrond Myklebust if (status != 0) 71006b4b681STrond Myklebust continue; 711a246b010SChuck Lever status = -EAGAIN; 712a246b010SChuck Lever break; 713a246b010SChuck Lever } 714c8485e4dSTrond Myklebust if (!transport->sock) 715c8485e4dSTrond Myklebust goto out; 716a246b010SChuck Lever 717262965f5SChuck Lever switch (status) { 718fba91afbSTrond Myklebust case -ENOTSOCK: 719fba91afbSTrond Myklebust status = -ENOTCONN; 720fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 721fba91afbSTrond Myklebust break; 722262965f5SChuck Lever case -EAGAIN: 7235e3771ceSTrond Myklebust status = xs_nospace(task); 724262965f5SChuck Lever break; 725262965f5SChuck Lever default: 726262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 727262965f5SChuck Lever -status); 728a246b010SChuck Lever case -ECONNRESET: 72955420c24STrond Myklebust case -EPIPE: 730e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 731a246b010SChuck Lever case -ECONNREFUSED: 732a246b010SChuck Lever case -ENOTCONN: 733a246b010SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 734a246b010SChuck Lever } 735c8485e4dSTrond Myklebust out: 736a246b010SChuck Lever return status; 737a246b010SChuck Lever } 738a246b010SChuck Lever 7399903cd1cSChuck Lever /** 740e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 741e0ab53deSTrond Myklebust * @xprt: transport 742e0ab53deSTrond Myklebust * @task: rpc task 743e0ab53deSTrond Myklebust * 744e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 745e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 746e0ab53deSTrond Myklebust * the server. 747e0ab53deSTrond Myklebust */ 748e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 749e0ab53deSTrond Myklebust { 750e0ab53deSTrond Myklebust struct rpc_rqst *req; 751e0ab53deSTrond Myklebust 752e0ab53deSTrond Myklebust if (task != xprt->snd_task) 753e0ab53deSTrond Myklebust return; 754e0ab53deSTrond Myklebust if (task == NULL) 755e0ab53deSTrond Myklebust goto out_release; 756e0ab53deSTrond Myklebust req = task->tk_rqstp; 757e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 758e0ab53deSTrond Myklebust goto out_release; 759e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 760e0ab53deSTrond Myklebust goto out_release; 761e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 762e0ab53deSTrond Myklebust out_release: 763e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 764e0ab53deSTrond Myklebust } 765e0ab53deSTrond Myklebust 7662a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7672a9e1cfaSTrond Myklebust { 7682a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7692a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7702a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7712a9e1cfaSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7722a9e1cfaSTrond Myklebust } 7732a9e1cfaSTrond Myklebust 7742a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7752a9e1cfaSTrond Myklebust { 7762a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7772a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7782a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7792a9e1cfaSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7802a9e1cfaSTrond Myklebust } 7812a9e1cfaSTrond Myklebust 782fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 783a246b010SChuck Lever { 784ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 785ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 786a246b010SChuck Lever 787fe315e76SChuck Lever if (sk == NULL) 788fe315e76SChuck Lever return; 7899903cd1cSChuck Lever 790a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 791ee0ac0c2SChuck Lever transport->inet = NULL; 792ee0ac0c2SChuck Lever transport->sock = NULL; 793a246b010SChuck Lever 794a246b010SChuck Lever sk->sk_user_data = NULL; 7952a9e1cfaSTrond Myklebust 7962a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 797a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 798a246b010SChuck Lever 799a246b010SChuck Lever sk->sk_no_check = 0; 800a246b010SChuck Lever 801a246b010SChuck Lever sock_release(sock); 802fe315e76SChuck Lever } 803fe315e76SChuck Lever 804fe315e76SChuck Lever /** 805fe315e76SChuck Lever * xs_close - close a socket 806fe315e76SChuck Lever * @xprt: transport 807fe315e76SChuck Lever * 808fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 809fe315e76SChuck Lever * on the server we want to save. 810f75e6745STrond Myklebust * 811f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 812f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 813fe315e76SChuck Lever */ 814fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 815fe315e76SChuck Lever { 816fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 817fe315e76SChuck Lever 818fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 819fe315e76SChuck Lever 820fe315e76SChuck Lever xs_reset_transport(transport); 821fe315e76SChuck Lever 822632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 8237d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 824632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 8253b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 826632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 82762da3b24STrond Myklebust xprt_disconnect_done(xprt); 828a246b010SChuck Lever } 829a246b010SChuck Lever 830f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt) 831f75e6745STrond Myklebust { 832f75e6745STrond Myklebust if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state)) 833f75e6745STrond Myklebust xs_close(xprt); 834f75e6745STrond Myklebust else 835f75e6745STrond Myklebust xs_tcp_shutdown(xprt); 836f75e6745STrond Myklebust } 837f75e6745STrond Myklebust 8389903cd1cSChuck Lever /** 8399903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8409903cd1cSChuck Lever * @xprt: doomed transport 8419903cd1cSChuck Lever * 8429903cd1cSChuck Lever */ 8439903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 844a246b010SChuck Lever { 845c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 846c8475461SChuck Lever 8479903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8489903cd1cSChuck Lever 849c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 850a246b010SChuck Lever 8519903cd1cSChuck Lever xs_close(xprt); 852edb267a6SChuck Lever xs_free_peer_addresses(xprt); 853a246b010SChuck Lever kfree(xprt->slot); 854c8541ecdSChuck Lever kfree(xprt); 855bc25571eS\"Talpey, Thomas\ module_put(THIS_MODULE); 856a246b010SChuck Lever } 857a246b010SChuck Lever 8589903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 8599903cd1cSChuck Lever { 8609903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 8619903cd1cSChuck Lever } 8629903cd1cSChuck Lever 8639903cd1cSChuck Lever /** 8649903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 8659903cd1cSChuck Lever * @sk: socket with data to read 8669903cd1cSChuck Lever * @len: how much data to read 8679903cd1cSChuck Lever * 868a246b010SChuck Lever */ 8699903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 870a246b010SChuck Lever { 871a246b010SChuck Lever struct rpc_task *task; 872a246b010SChuck Lever struct rpc_xprt *xprt; 873a246b010SChuck Lever struct rpc_rqst *rovr; 874a246b010SChuck Lever struct sk_buff *skb; 875a246b010SChuck Lever int err, repsize, copied; 876d8ed029dSAlexey Dobriyan u32 _xid; 877d8ed029dSAlexey Dobriyan __be32 *xp; 878a246b010SChuck Lever 879a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 8809903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 8819903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 882a246b010SChuck Lever goto out; 883a246b010SChuck Lever 884a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 885a246b010SChuck Lever goto out; 886a246b010SChuck Lever 887a246b010SChuck Lever if (xprt->shutdown) 888a246b010SChuck Lever goto dropit; 889a246b010SChuck Lever 890a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 891a246b010SChuck Lever if (repsize < 4) { 8929903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 893a246b010SChuck Lever goto dropit; 894a246b010SChuck Lever } 895a246b010SChuck Lever 896a246b010SChuck Lever /* Copy the XID from the skb... */ 897a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 898a246b010SChuck Lever sizeof(_xid), &_xid); 899a246b010SChuck Lever if (xp == NULL) 900a246b010SChuck Lever goto dropit; 901a246b010SChuck Lever 902a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 9034a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 904a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 905a246b010SChuck Lever if (!rovr) 906a246b010SChuck Lever goto out_unlock; 907a246b010SChuck Lever task = rovr->rq_task; 908a246b010SChuck Lever 909a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 910a246b010SChuck Lever copied = repsize; 911a246b010SChuck Lever 912a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 9131781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 9141781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 915a246b010SChuck Lever goto out_unlock; 9161781f7f5SHerbert Xu } 9171781f7f5SHerbert Xu 9181781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 919a246b010SChuck Lever 920a246b010SChuck Lever /* Something worked... */ 921*adf30907SEric Dumazet dst_confirm(skb_dst(skb)); 922a246b010SChuck Lever 9231570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 9241570c1e4SChuck Lever xprt_update_rtt(task); 9251570c1e4SChuck Lever xprt_complete_rqst(task, copied); 926a246b010SChuck Lever 927a246b010SChuck Lever out_unlock: 9284a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 929a246b010SChuck Lever dropit: 930a246b010SChuck Lever skb_free_datagram(sk, skb); 931a246b010SChuck Lever out: 932a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 933a246b010SChuck Lever } 934a246b010SChuck Lever 935dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 936a246b010SChuck Lever { 93751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 938a246b010SChuck Lever size_t len, used; 939a246b010SChuck Lever char *p; 940a246b010SChuck Lever 94151971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 94251971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 9439d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 94451971139SChuck Lever transport->tcp_offset += used; 945a246b010SChuck Lever if (used != len) 946a246b010SChuck Lever return; 947808012fbSChuck Lever 94851971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 94951971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 950e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 951a246b010SChuck Lever else 952e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 95351971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 954808012fbSChuck Lever 955e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 95651971139SChuck Lever transport->tcp_offset = 0; 957808012fbSChuck Lever 958a246b010SChuck Lever /* Sanity check of the record length */ 95951971139SChuck Lever if (unlikely(transport->tcp_reclen < 4)) { 9609903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 9613ebb067dSTrond Myklebust xprt_force_disconnect(xprt); 9629903cd1cSChuck Lever return; 963a246b010SChuck Lever } 964a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 96551971139SChuck Lever transport->tcp_reclen); 966a246b010SChuck Lever } 967a246b010SChuck Lever 96851971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 969a246b010SChuck Lever { 97051971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 971e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 97251971139SChuck Lever transport->tcp_offset = 0; 973e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 974e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 975e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 97651971139SChuck Lever transport->tcp_copied = 0; 977a246b010SChuck Lever } 978a246b010SChuck Lever } 979a246b010SChuck Lever } 980a246b010SChuck Lever 981dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 982a246b010SChuck Lever { 983a246b010SChuck Lever size_t len, used; 984a246b010SChuck Lever char *p; 985a246b010SChuck Lever 98651971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 987a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 98851971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 9899d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 99051971139SChuck Lever transport->tcp_offset += used; 991a246b010SChuck Lever if (used != len) 992a246b010SChuck Lever return; 993e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 994e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_DATA; 99551971139SChuck Lever transport->tcp_copied = 4; 996a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 99751971139SChuck Lever ntohl(transport->tcp_xid)); 99851971139SChuck Lever xs_tcp_check_fraghdr(transport); 999a246b010SChuck Lever } 1000a246b010SChuck Lever 1001dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1002a246b010SChuck Lever { 100351971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1004a246b010SChuck Lever struct rpc_rqst *req; 1005a246b010SChuck Lever struct xdr_buf *rcvbuf; 1006a246b010SChuck Lever size_t len; 1007a246b010SChuck Lever ssize_t r; 1008a246b010SChuck Lever 1009a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 10104a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 101151971139SChuck Lever req = xprt_lookup_rqst(xprt, transport->tcp_xid); 1012a246b010SChuck Lever if (!req) { 1013e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1014a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 101551971139SChuck Lever ntohl(transport->tcp_xid)); 10164a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 1017a246b010SChuck Lever return; 1018a246b010SChuck Lever } 1019a246b010SChuck Lever 1020a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1021a246b010SChuck Lever len = desc->count; 102251971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1023dd456471SChuck Lever struct xdr_skb_reader my_desc; 1024a246b010SChuck Lever 102551971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1026a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1027a246b010SChuck Lever my_desc.count = len; 102851971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10299d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1030a246b010SChuck Lever desc->count -= r; 1031a246b010SChuck Lever desc->offset += r; 1032a246b010SChuck Lever } else 103351971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 10349d292316SChuck Lever desc, xdr_skb_read_bits); 1035a246b010SChuck Lever 1036a246b010SChuck Lever if (r > 0) { 103751971139SChuck Lever transport->tcp_copied += r; 103851971139SChuck Lever transport->tcp_offset += r; 1039a246b010SChuck Lever } 1040a246b010SChuck Lever if (r != len) { 1041a246b010SChuck Lever /* Error when copying to the receive buffer, 1042a246b010SChuck Lever * usually because we weren't able to allocate 1043a246b010SChuck Lever * additional buffer pages. All we can do now 1044e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1045a246b010SChuck Lever * will not receive any additional updates, 1046a246b010SChuck Lever * and time out. 1047a246b010SChuck Lever * Any remaining data from this record will 1048a246b010SChuck Lever * be discarded. 1049a246b010SChuck Lever */ 1050e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1051a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 105251971139SChuck Lever ntohl(transport->tcp_xid)); 105346121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 105446121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 105546121cf7SChuck Lever xprt, transport->tcp_copied, 105646121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1057a246b010SChuck Lever goto out; 1058a246b010SChuck Lever } 1059a246b010SChuck Lever 1060a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 106151971139SChuck Lever ntohl(transport->tcp_xid), r); 106246121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 106346121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 106446121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1065a246b010SChuck Lever 106651971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1067e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 106851971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1069e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1070e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1071a246b010SChuck Lever } 1072a246b010SChuck Lever 1073a246b010SChuck Lever out: 1074e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 107551971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 10764a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 107751971139SChuck Lever xs_tcp_check_fraghdr(transport); 1078a246b010SChuck Lever } 1079a246b010SChuck Lever 1080dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1081a246b010SChuck Lever { 1082a246b010SChuck Lever size_t len; 1083a246b010SChuck Lever 108451971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1085a246b010SChuck Lever if (len > desc->count) 1086a246b010SChuck Lever len = desc->count; 1087a246b010SChuck Lever desc->count -= len; 1088a246b010SChuck Lever desc->offset += len; 108951971139SChuck Lever transport->tcp_offset += len; 1090a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 109151971139SChuck Lever xs_tcp_check_fraghdr(transport); 1092a246b010SChuck Lever } 1093a246b010SChuck Lever 10949903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1095a246b010SChuck Lever { 1096a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 109751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1098dd456471SChuck Lever struct xdr_skb_reader desc = { 1099a246b010SChuck Lever .skb = skb, 1100a246b010SChuck Lever .offset = offset, 1101a246b010SChuck Lever .count = len, 1102a246b010SChuck Lever }; 1103a246b010SChuck Lever 11049903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1105a246b010SChuck Lever do { 1106a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1107a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1108e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 11099903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1110a246b010SChuck Lever continue; 1111a246b010SChuck Lever } 1112a246b010SChuck Lever /* Read in the xid if necessary */ 1113e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 111451971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1115a246b010SChuck Lever continue; 1116a246b010SChuck Lever } 1117a246b010SChuck Lever /* Read in the request data */ 1118e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 11199903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 1120a246b010SChuck Lever continue; 1121a246b010SChuck Lever } 1122a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 112351971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1124a246b010SChuck Lever } while (desc.count); 11259903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1126a246b010SChuck Lever return len - desc.count; 1127a246b010SChuck Lever } 1128a246b010SChuck Lever 11299903cd1cSChuck Lever /** 11309903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 11319903cd1cSChuck Lever * @sk: socket with data to read 11329903cd1cSChuck Lever * @bytes: how much data to read 11339903cd1cSChuck Lever * 11349903cd1cSChuck Lever */ 11359903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1136a246b010SChuck Lever { 1137a246b010SChuck Lever struct rpc_xprt *xprt; 1138a246b010SChuck Lever read_descriptor_t rd_desc; 1139ff2d7db8STrond Myklebust int read; 1140a246b010SChuck Lever 11419903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 114246121cf7SChuck Lever 114346121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 11449903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1145a246b010SChuck Lever goto out; 1146a246b010SChuck Lever if (xprt->shutdown) 1147a246b010SChuck Lever goto out; 1148a246b010SChuck Lever 11499903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1150a246b010SChuck Lever rd_desc.arg.data = xprt; 1151ff2d7db8STrond Myklebust do { 1152a246b010SChuck Lever rd_desc.count = 65536; 1153ff2d7db8STrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1154ff2d7db8STrond Myklebust } while (read > 0); 1155a246b010SChuck Lever out: 1156a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1157a246b010SChuck Lever } 1158a246b010SChuck Lever 11597d1e8255STrond Myklebust /* 11607d1e8255STrond Myklebust * Do the equivalent of linger/linger2 handling for dealing with 11617d1e8255STrond Myklebust * broken servers that don't close the socket in a timely 11627d1e8255STrond Myklebust * fashion 11637d1e8255STrond Myklebust */ 11647d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, 11657d1e8255STrond Myklebust unsigned long timeout) 11667d1e8255STrond Myklebust { 11677d1e8255STrond Myklebust struct sock_xprt *transport; 11687d1e8255STrond Myklebust 11697d1e8255STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 11707d1e8255STrond Myklebust return; 11717d1e8255STrond Myklebust set_bit(XPRT_CONNECTION_ABORT, &xprt->state); 11727d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 11737d1e8255STrond Myklebust queue_delayed_work(rpciod_workqueue, &transport->connect_worker, 11747d1e8255STrond Myklebust timeout); 11757d1e8255STrond Myklebust } 11767d1e8255STrond Myklebust 11777d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) 11787d1e8255STrond Myklebust { 11797d1e8255STrond Myklebust struct sock_xprt *transport; 11807d1e8255STrond Myklebust 11817d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 11827d1e8255STrond Myklebust 11837d1e8255STrond Myklebust if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || 11847d1e8255STrond Myklebust !cancel_delayed_work(&transport->connect_worker)) 11857d1e8255STrond Myklebust return; 11867d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 11877d1e8255STrond Myklebust xprt_clear_connecting(xprt); 11887d1e8255STrond Myklebust } 11897d1e8255STrond Myklebust 11907d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 11917d1e8255STrond Myklebust { 11927d1e8255STrond Myklebust smp_mb__before_clear_bit(); 11937d1e8255STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 11947d1e8255STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 11957d1e8255STrond Myklebust smp_mb__after_clear_bit(); 11967d1e8255STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 11977d1e8255STrond Myklebust xprt_disconnect_done(xprt); 11987d1e8255STrond Myklebust } 11997d1e8255STrond Myklebust 12009903cd1cSChuck Lever /** 12019903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 12029903cd1cSChuck Lever * @sk: socket whose state has changed 12039903cd1cSChuck Lever * 12049903cd1cSChuck Lever */ 12059903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1206a246b010SChuck Lever { 1207a246b010SChuck Lever struct rpc_xprt *xprt; 1208a246b010SChuck Lever 1209a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1210a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1211a246b010SChuck Lever goto out; 12129903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1213a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 1214a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1215a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1216a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 1217a246b010SChuck Lever 1218a246b010SChuck Lever switch (sk->sk_state) { 1219a246b010SChuck Lever case TCP_ESTABLISHED: 12204a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 1221a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 122251971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 122351971139SChuck Lever struct sock_xprt, xprt); 122451971139SChuck Lever 1225a246b010SChuck Lever /* Reset TCP record info */ 122651971139SChuck Lever transport->tcp_offset = 0; 122751971139SChuck Lever transport->tcp_reclen = 0; 122851971139SChuck Lever transport->tcp_copied = 0; 1229e136d092SChuck Lever transport->tcp_flags = 1230e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 123151971139SChuck Lever 12322a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1233a246b010SChuck Lever } 12344a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1235a246b010SChuck Lever break; 12363b948ae5STrond Myklebust case TCP_FIN_WAIT1: 12373b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 12387c1d71cfSTrond Myklebust xprt->connect_cookie++; 1239663b8858STrond Myklebust xprt->reestablish_timeout = 0; 12403b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 12413b948ae5STrond Myklebust smp_mb__before_clear_bit(); 12423b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1243ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 12443b948ae5STrond Myklebust smp_mb__after_clear_bit(); 124525fe6142STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); 1246a246b010SChuck Lever break; 1247632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 12483b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 124966af1e55STrond Myklebust xprt_force_disconnect(xprt); 1250663b8858STrond Myklebust case TCP_SYN_SENT: 12517c1d71cfSTrond Myklebust xprt->connect_cookie++; 1252663b8858STrond Myklebust case TCP_CLOSING: 1253663b8858STrond Myklebust /* 1254663b8858STrond Myklebust * If the server closed down the connection, make sure that 1255663b8858STrond Myklebust * we back off before reconnecting 1256663b8858STrond Myklebust */ 1257663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1258663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 12593b948ae5STrond Myklebust break; 12603b948ae5STrond Myklebust case TCP_LAST_ACK: 1261670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 126225fe6142STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); 12633b948ae5STrond Myklebust smp_mb__before_clear_bit(); 12643b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 12653b948ae5STrond Myklebust smp_mb__after_clear_bit(); 12663b948ae5STrond Myklebust break; 12673b948ae5STrond Myklebust case TCP_CLOSE: 12687d1e8255STrond Myklebust xs_tcp_cancel_linger_timeout(xprt); 12697d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1270a246b010SChuck Lever } 1271a246b010SChuck Lever out: 1272a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1273a246b010SChuck Lever } 1274a246b010SChuck Lever 12759903cd1cSChuck Lever /** 1276482f32e6STrond Myklebust * xs_error_report - callback mainly for catching socket errors 12772a9e1cfaSTrond Myklebust * @sk: socket 12782a9e1cfaSTrond Myklebust */ 1279482f32e6STrond Myklebust static void xs_error_report(struct sock *sk) 12802a9e1cfaSTrond Myklebust { 12812a9e1cfaSTrond Myklebust struct rpc_xprt *xprt; 12822a9e1cfaSTrond Myklebust 12832a9e1cfaSTrond Myklebust read_lock(&sk->sk_callback_lock); 12842a9e1cfaSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12852a9e1cfaSTrond Myklebust goto out; 12862a9e1cfaSTrond Myklebust dprintk("RPC: %s client %p...\n" 12872a9e1cfaSTrond Myklebust "RPC: error %d\n", 12882a9e1cfaSTrond Myklebust __func__, xprt, sk->sk_err); 1289482f32e6STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 12902a9e1cfaSTrond Myklebust out: 12912a9e1cfaSTrond Myklebust read_unlock(&sk->sk_callback_lock); 12922a9e1cfaSTrond Myklebust } 12932a9e1cfaSTrond Myklebust 12941f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 12951f0fa154SIlpo Järvinen { 12961f0fa154SIlpo Järvinen struct socket *sock; 12971f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 12981f0fa154SIlpo Järvinen 12991f0fa154SIlpo Järvinen if (unlikely(!(sock = sk->sk_socket))) 13001f0fa154SIlpo Järvinen return; 13011f0fa154SIlpo Järvinen clear_bit(SOCK_NOSPACE, &sock->flags); 13021f0fa154SIlpo Järvinen 13031f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 13041f0fa154SIlpo Järvinen return; 13051f0fa154SIlpo Järvinen if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 13061f0fa154SIlpo Järvinen return; 13071f0fa154SIlpo Järvinen 13081f0fa154SIlpo Järvinen xprt_write_space(xprt); 13091f0fa154SIlpo Järvinen } 13101f0fa154SIlpo Järvinen 13112a9e1cfaSTrond Myklebust /** 1312c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1313c7b2cae8SChuck Lever * becomes available 13149903cd1cSChuck Lever * @sk: socket whose state has changed 13159903cd1cSChuck Lever * 1316a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1317a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1318c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1319a246b010SChuck Lever * with a bunch of small requests. 1320a246b010SChuck Lever */ 1321c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1322a246b010SChuck Lever { 1323a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1324c7b2cae8SChuck Lever 1325c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 13261f0fa154SIlpo Järvinen if (sock_writeable(sk)) 13271f0fa154SIlpo Järvinen xs_write_space(sk); 1328c7b2cae8SChuck Lever 1329c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1330c7b2cae8SChuck Lever } 1331c7b2cae8SChuck Lever 1332c7b2cae8SChuck Lever /** 1333c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1334c7b2cae8SChuck Lever * becomes available 1335c7b2cae8SChuck Lever * @sk: socket whose state has changed 1336c7b2cae8SChuck Lever * 1337c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1338c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1339c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1340c7b2cae8SChuck Lever * with a bunch of small requests. 1341c7b2cae8SChuck Lever */ 1342c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1343c7b2cae8SChuck Lever { 1344c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1345c7b2cae8SChuck Lever 1346c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 13471f0fa154SIlpo Järvinen if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 13481f0fa154SIlpo Järvinen xs_write_space(sk); 1349c7b2cae8SChuck Lever 1350a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1351a246b010SChuck Lever } 1352a246b010SChuck Lever 1353470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1354a246b010SChuck Lever { 1355ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1356ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1357a246b010SChuck Lever 13587c6e066eSChuck Lever if (transport->rcvsize) { 1359a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 13607c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1361a246b010SChuck Lever } 13627c6e066eSChuck Lever if (transport->sndsize) { 1363a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 13647c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1365a246b010SChuck Lever sk->sk_write_space(sk); 1366a246b010SChuck Lever } 1367a246b010SChuck Lever } 1368a246b010SChuck Lever 136943118c29SChuck Lever /** 1370470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 137143118c29SChuck Lever * @xprt: generic transport 1372470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1373470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 137443118c29SChuck Lever * 1375470056c2SChuck Lever * Set socket send and receive buffer size limits. 137643118c29SChuck Lever */ 1377470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 137843118c29SChuck Lever { 13797c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 13807c6e066eSChuck Lever 13817c6e066eSChuck Lever transport->sndsize = 0; 1382470056c2SChuck Lever if (sndsize) 13837c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 13847c6e066eSChuck Lever transport->rcvsize = 0; 1385470056c2SChuck Lever if (rcvsize) 13867c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1387470056c2SChuck Lever 1388470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 138943118c29SChuck Lever } 139043118c29SChuck Lever 139146c0ee8bSChuck Lever /** 139246c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 139346c0ee8bSChuck Lever * @task: task that timed out 139446c0ee8bSChuck Lever * 139546c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 139646c0ee8bSChuck Lever */ 139746c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 139846c0ee8bSChuck Lever { 139946c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 140046c0ee8bSChuck Lever } 140146c0ee8bSChuck Lever 1402b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1403b85d8806SChuck Lever { 1404b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1405b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1406b85d8806SChuck Lever return rand + xprt_min_resvport; 1407b85d8806SChuck Lever } 1408b85d8806SChuck Lever 140992200412SChuck Lever /** 141092200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 141192200412SChuck Lever * @xprt: generic transport 141292200412SChuck Lever * @port: new port number 141392200412SChuck Lever * 141492200412SChuck Lever */ 141592200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 141692200412SChuck Lever { 141795392c59SChuck Lever struct sockaddr *addr = xs_addr(xprt); 1418c4efcb1dSChuck Lever 141992200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1420c4efcb1dSChuck Lever 142120612005SChuck Lever switch (addr->sa_family) { 142220612005SChuck Lever case AF_INET: 142320612005SChuck Lever ((struct sockaddr_in *)addr)->sin_port = htons(port); 142420612005SChuck Lever break; 142520612005SChuck Lever case AF_INET6: 142620612005SChuck Lever ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); 142720612005SChuck Lever break; 142820612005SChuck Lever default: 142920612005SChuck Lever BUG(); 143020612005SChuck Lever } 143192200412SChuck Lever } 143292200412SChuck Lever 143367a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock) 143467a391d7STrond Myklebust { 143567a391d7STrond Myklebust unsigned short port = transport->port; 143667a391d7STrond Myklebust 143767a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 143867a391d7STrond Myklebust port = xs_get_random_port(); 143967a391d7STrond Myklebust return port; 144067a391d7STrond Myklebust } 144167a391d7STrond Myklebust 144267a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port) 144367a391d7STrond Myklebust { 144467a391d7STrond Myklebust if (transport->port != 0) 144567a391d7STrond Myklebust transport->port = 0; 144667a391d7STrond Myklebust if (!transport->xprt.resvport) 144767a391d7STrond Myklebust return 0; 144867a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 144967a391d7STrond Myklebust return xprt_max_resvport; 145067a391d7STrond Myklebust return --port; 145167a391d7STrond Myklebust } 145267a391d7STrond Myklebust 14537dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock) 1454a246b010SChuck Lever { 1455a246b010SChuck Lever struct sockaddr_in myaddr = { 1456a246b010SChuck Lever .sin_family = AF_INET, 1457a246b010SChuck Lever }; 1458d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 145967a391d7STrond Myklebust int err, nloop = 0; 146067a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 146167a391d7STrond Myklebust unsigned short last; 1462a246b010SChuck Lever 1463d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1464d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1465a246b010SChuck Lever do { 1466a246b010SChuck Lever myaddr.sin_port = htons(port); 1467e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1468a246b010SChuck Lever sizeof(myaddr)); 146967a391d7STrond Myklebust if (port == 0) 1470d3bc9a1dSFrank van Maarseveen break; 1471a246b010SChuck Lever if (err == 0) { 1472c8475461SChuck Lever transport->port = port; 1473d3bc9a1dSFrank van Maarseveen break; 1474a246b010SChuck Lever } 147567a391d7STrond Myklebust last = port; 147667a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 147767a391d7STrond Myklebust if (port > last) 147867a391d7STrond Myklebust nloop++; 147967a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 148021454aaaSHarvey Harrison dprintk("RPC: %s %pI4:%u: %s (%d)\n", 148121454aaaSHarvey Harrison __func__, &myaddr.sin_addr, 14827dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1483a246b010SChuck Lever return err; 1484a246b010SChuck Lever } 1485a246b010SChuck Lever 148690058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock) 148790058d37SChuck Lever { 148890058d37SChuck Lever struct sockaddr_in6 myaddr = { 148990058d37SChuck Lever .sin6_family = AF_INET6, 149090058d37SChuck Lever }; 149190058d37SChuck Lever struct sockaddr_in6 *sa; 149267a391d7STrond Myklebust int err, nloop = 0; 149367a391d7STrond Myklebust unsigned short port = xs_get_srcport(transport, sock); 149467a391d7STrond Myklebust unsigned short last; 149590058d37SChuck Lever 149690058d37SChuck Lever sa = (struct sockaddr_in6 *)&transport->addr; 149790058d37SChuck Lever myaddr.sin6_addr = sa->sin6_addr; 149890058d37SChuck Lever do { 149990058d37SChuck Lever myaddr.sin6_port = htons(port); 150090058d37SChuck Lever err = kernel_bind(sock, (struct sockaddr *) &myaddr, 150190058d37SChuck Lever sizeof(myaddr)); 150267a391d7STrond Myklebust if (port == 0) 150390058d37SChuck Lever break; 150490058d37SChuck Lever if (err == 0) { 150590058d37SChuck Lever transport->port = port; 150690058d37SChuck Lever break; 150790058d37SChuck Lever } 150867a391d7STrond Myklebust last = port; 150967a391d7STrond Myklebust port = xs_next_srcport(transport, sock, port); 151067a391d7STrond Myklebust if (port > last) 151167a391d7STrond Myklebust nloop++; 151267a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 15135b095d98SHarvey Harrison dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", 1514fdb46ee7SHarvey Harrison &myaddr.sin6_addr, port, err ? "failed" : "ok", err); 1515a246b010SChuck Lever return err; 1516a246b010SChuck Lever } 1517a246b010SChuck Lever 1518ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1519ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1520ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1521ed07536eSPeter Zijlstra 15228945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1523ed07536eSPeter Zijlstra { 1524ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 15258945ee5eSChuck Lever 152602b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 15278945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 15288945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1529ed07536eSPeter Zijlstra } 15308945ee5eSChuck Lever 15318945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 15328945ee5eSChuck Lever { 15338945ee5eSChuck Lever struct sock *sk = sock->sk; 15348945ee5eSChuck Lever 1535f4921affSLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 15368945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 15378945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1538ed07536eSPeter Zijlstra } 1539ed07536eSPeter Zijlstra #else 15408945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 15418945ee5eSChuck Lever { 15428945ee5eSChuck Lever } 15438945ee5eSChuck Lever 15448945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1545ed07536eSPeter Zijlstra { 1546ed07536eSPeter Zijlstra } 1547ed07536eSPeter Zijlstra #endif 1548ed07536eSPeter Zijlstra 154916be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1550a246b010SChuck Lever { 155116be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1552edb267a6SChuck Lever 1553ee0ac0c2SChuck Lever if (!transport->inet) { 1554b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1555b0d93ad5SChuck Lever 1556b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1557b0d93ad5SChuck Lever 15582a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 15592a9e1cfaSTrond Myklebust 1560b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1561b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1562b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1563482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1564b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 1565b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 1566b0d93ad5SChuck Lever 1567b0d93ad5SChuck Lever xprt_set_connected(xprt); 1568b0d93ad5SChuck Lever 1569b0d93ad5SChuck Lever /* Reset to new socket */ 1570ee0ac0c2SChuck Lever transport->sock = sock; 1571ee0ac0c2SChuck Lever transport->inet = sk; 1572b0d93ad5SChuck Lever 1573b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1574b0d93ad5SChuck Lever } 1575470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 157616be2d20SChuck Lever } 157716be2d20SChuck Lever 1578a246b010SChuck Lever /** 15799c3d72deSChuck Lever * xs_udp_connect_worker4 - set up a UDP socket 1580a246b010SChuck Lever * @work: RPC transport to connect 1581a246b010SChuck Lever * 1582a246b010SChuck Lever * Invoked by a work queue tasklet. 1583a246b010SChuck Lever */ 15849c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work) 1585a246b010SChuck Lever { 1586a246b010SChuck Lever struct sock_xprt *transport = 1587a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1588a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1589a246b010SChuck Lever struct socket *sock = transport->sock; 1590a246b010SChuck Lever int err, status = -EIO; 1591a246b010SChuck Lever 159201d37c42STrond Myklebust if (xprt->shutdown) 15939903cd1cSChuck Lever goto out; 15949903cd1cSChuck Lever 1595a246b010SChuck Lever /* Start by resetting any existing state */ 1596fe315e76SChuck Lever xs_reset_transport(transport); 1597a246b010SChuck Lever 1598fe315e76SChuck Lever err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); 1599fe315e76SChuck Lever if (err < 0) { 16009903cd1cSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1601a246b010SChuck Lever goto out; 1602a246b010SChuck Lever } 16038945ee5eSChuck Lever xs_reclassify_socket4(sock); 1604a246b010SChuck Lever 16057dc753f0SChuck Lever if (xs_bind4(transport, sock)) { 16069903cd1cSChuck Lever sock_release(sock); 16079903cd1cSChuck Lever goto out; 1608a246b010SChuck Lever } 1609b0d93ad5SChuck Lever 1610b0d93ad5SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 1611b0d93ad5SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1612b0d93ad5SChuck Lever 161316be2d20SChuck Lever xs_udp_finish_connecting(xprt, sock); 1614a246b010SChuck Lever status = 0; 1615b0d93ad5SChuck Lever out: 1616b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 16177d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1618b0d93ad5SChuck Lever } 1619b0d93ad5SChuck Lever 162068e220bdSChuck Lever /** 162168e220bdSChuck Lever * xs_udp_connect_worker6 - set up a UDP socket 162268e220bdSChuck Lever * @work: RPC transport to connect 162368e220bdSChuck Lever * 162468e220bdSChuck Lever * Invoked by a work queue tasklet. 162568e220bdSChuck Lever */ 162668e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work) 162768e220bdSChuck Lever { 162868e220bdSChuck Lever struct sock_xprt *transport = 162968e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 163068e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 163168e220bdSChuck Lever struct socket *sock = transport->sock; 163268e220bdSChuck Lever int err, status = -EIO; 163368e220bdSChuck Lever 163401d37c42STrond Myklebust if (xprt->shutdown) 163568e220bdSChuck Lever goto out; 163668e220bdSChuck Lever 163768e220bdSChuck Lever /* Start by resetting any existing state */ 1638fe315e76SChuck Lever xs_reset_transport(transport); 163968e220bdSChuck Lever 1640fe315e76SChuck Lever err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); 1641fe315e76SChuck Lever if (err < 0) { 164268e220bdSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 164368e220bdSChuck Lever goto out; 164468e220bdSChuck Lever } 16458945ee5eSChuck Lever xs_reclassify_socket6(sock); 164668e220bdSChuck Lever 164768e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 164868e220bdSChuck Lever sock_release(sock); 164968e220bdSChuck Lever goto out; 165068e220bdSChuck Lever } 165168e220bdSChuck Lever 165268e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 165368e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 165468e220bdSChuck Lever 165568e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 1656a246b010SChuck Lever status = 0; 1657b0d93ad5SChuck Lever out: 1658b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 16597d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1660b0d93ad5SChuck Lever } 1661b0d93ad5SChuck Lever 16623167e12cSChuck Lever /* 16633167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 16643167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 16653167e12cSChuck Lever */ 166640d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 16673167e12cSChuck Lever { 16683167e12cSChuck Lever int result; 16693167e12cSChuck Lever struct sockaddr any; 16703167e12cSChuck Lever 16713167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 16723167e12cSChuck Lever 16733167e12cSChuck Lever /* 16743167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 16753167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 16763167e12cSChuck Lever */ 16773167e12cSChuck Lever memset(&any, 0, sizeof(any)); 16783167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1679ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 16807d1e8255STrond Myklebust if (!result) 16817d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 16827d1e8255STrond Myklebust else 16833167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 16843167e12cSChuck Lever result); 16853167e12cSChuck Lever } 16863167e12cSChuck Lever 168740d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) 168840d2549dSTrond Myklebust { 168940d2549dSTrond Myklebust unsigned int state = transport->inet->sk_state; 169040d2549dSTrond Myklebust 169140d2549dSTrond Myklebust if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) 169240d2549dSTrond Myklebust return; 169340d2549dSTrond Myklebust if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) 169440d2549dSTrond Myklebust return; 169540d2549dSTrond Myklebust xs_abort_connection(xprt, transport); 169640d2549dSTrond Myklebust } 169740d2549dSTrond Myklebust 169816be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1699b0d93ad5SChuck Lever { 170016be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1701edb267a6SChuck Lever 1702ee0ac0c2SChuck Lever if (!transport->inet) { 1703b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1704b0d93ad5SChuck Lever 1705b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1706b0d93ad5SChuck Lever 17072a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 17082a9e1cfaSTrond Myklebust 1709b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1710b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1711b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1712b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1713482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1714b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 17153167e12cSChuck Lever 17163167e12cSChuck Lever /* socket options */ 17173167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 17183167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 17193167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 17203167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1721b0d93ad5SChuck Lever 1722b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1723b0d93ad5SChuck Lever 1724b0d93ad5SChuck Lever /* Reset to new socket */ 1725ee0ac0c2SChuck Lever transport->sock = sock; 1726ee0ac0c2SChuck Lever transport->inet = sk; 1727b0d93ad5SChuck Lever 1728b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1729b0d93ad5SChuck Lever } 1730b0d93ad5SChuck Lever 173101d37c42STrond Myklebust if (!xprt_bound(xprt)) 173201d37c42STrond Myklebust return -ENOTCONN; 173301d37c42STrond Myklebust 1734b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1735262ca07dSChuck Lever xprt->stat.connect_count++; 1736262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 173795392c59SChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 173816be2d20SChuck Lever } 173916be2d20SChuck Lever 174016be2d20SChuck Lever /** 1741b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 1742b61d59ffSTrond Myklebust * @xprt: RPC transport to connect 1743b61d59ffSTrond Myklebust * @transport: socket transport to connect 1744b61d59ffSTrond Myklebust * @create_sock: function to create a socket of the correct type 174516be2d20SChuck Lever * 174616be2d20SChuck Lever * Invoked by a work queue tasklet. 174716be2d20SChuck Lever */ 1748b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt, 1749b61d59ffSTrond Myklebust struct sock_xprt *transport, 1750b61d59ffSTrond Myklebust struct socket *(*create_sock)(struct rpc_xprt *, 1751b61d59ffSTrond Myklebust struct sock_xprt *)) 175216be2d20SChuck Lever { 175316be2d20SChuck Lever struct socket *sock = transport->sock; 1754b61d59ffSTrond Myklebust int status = -EIO; 175516be2d20SChuck Lever 175601d37c42STrond Myklebust if (xprt->shutdown) 175716be2d20SChuck Lever goto out; 175816be2d20SChuck Lever 175916be2d20SChuck Lever if (!sock) { 17607d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 1761b61d59ffSTrond Myklebust sock = create_sock(xprt, transport); 1762b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 1763b61d59ffSTrond Myklebust status = PTR_ERR(sock); 176416be2d20SChuck Lever goto out; 176516be2d20SChuck Lever } 17667d1e8255STrond Myklebust } else { 17677d1e8255STrond Myklebust int abort_and_exit; 17687d1e8255STrond Myklebust 17697d1e8255STrond Myklebust abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, 17707d1e8255STrond Myklebust &xprt->state); 177116be2d20SChuck Lever /* "close" the socket, preserving the local port */ 177240d2549dSTrond Myklebust xs_tcp_reuse_connection(xprt, transport); 177316be2d20SChuck Lever 17747d1e8255STrond Myklebust if (abort_and_exit) 17757d1e8255STrond Myklebust goto out_eagain; 17767d1e8255STrond Myklebust } 17777d1e8255STrond Myklebust 177816be2d20SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 177916be2d20SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 178016be2d20SChuck Lever 178116be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 1782a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 178346121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 178446121cf7SChuck Lever sock->sk->sk_state); 1785a246b010SChuck Lever switch (status) { 1786f75e6745STrond Myklebust default: 1787f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 1788f75e6745STrond Myklebust __func__, status); 1789f75e6745STrond Myklebust case -EADDRNOTAVAIL: 1790f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 1791f75e6745STrond Myklebust * and retry 1792f75e6745STrond Myklebust */ 1793f75e6745STrond Myklebust set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 1794f75e6745STrond Myklebust xprt_force_disconnect(xprt); 17958a2cec29STrond Myklebust case -ECONNREFUSED: 17968a2cec29STrond Myklebust case -ECONNRESET: 17978a2cec29STrond Myklebust case -ENETUNREACH: 17988a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 17992a491991STrond Myklebust case 0: 1800a246b010SChuck Lever case -EINPROGRESS: 1801a246b010SChuck Lever case -EALREADY: 18027d1e8255STrond Myklebust xprt_clear_connecting(xprt); 18037d1e8255STrond Myklebust return; 18048a2cec29STrond Myklebust } 18057d1e8255STrond Myklebust out_eagain: 18062a491991STrond Myklebust status = -EAGAIN; 1807a246b010SChuck Lever out: 18082226feb6SChuck Lever xprt_clear_connecting(xprt); 18097d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 1810a246b010SChuck Lever } 1811a246b010SChuck Lever 1812b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt, 1813b61d59ffSTrond Myklebust struct sock_xprt *transport) 1814b61d59ffSTrond Myklebust { 1815b61d59ffSTrond Myklebust struct socket *sock; 1816b61d59ffSTrond Myklebust int err; 1817b61d59ffSTrond Myklebust 1818b61d59ffSTrond Myklebust /* start from scratch */ 1819b61d59ffSTrond Myklebust err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); 1820b61d59ffSTrond Myklebust if (err < 0) { 1821b61d59ffSTrond Myklebust dprintk("RPC: can't create TCP transport socket (%d).\n", 1822b61d59ffSTrond Myklebust -err); 1823b61d59ffSTrond Myklebust goto out_err; 1824b61d59ffSTrond Myklebust } 1825b61d59ffSTrond Myklebust xs_reclassify_socket4(sock); 1826b61d59ffSTrond Myklebust 1827b61d59ffSTrond Myklebust if (xs_bind4(transport, sock) < 0) { 1828b61d59ffSTrond Myklebust sock_release(sock); 1829b61d59ffSTrond Myklebust goto out_err; 1830b61d59ffSTrond Myklebust } 1831b61d59ffSTrond Myklebust return sock; 1832b61d59ffSTrond Myklebust out_err: 1833b61d59ffSTrond Myklebust return ERR_PTR(-EIO); 1834b61d59ffSTrond Myklebust } 1835b61d59ffSTrond Myklebust 1836b61d59ffSTrond Myklebust /** 1837a246b010SChuck Lever * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint 1838a246b010SChuck Lever * @work: RPC transport to connect 1839a246b010SChuck Lever * 1840a246b010SChuck Lever * Invoked by a work queue tasklet. 1841a246b010SChuck Lever */ 1842a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work) 1843a246b010SChuck Lever { 1844a246b010SChuck Lever struct sock_xprt *transport = 1845a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1846a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1847a246b010SChuck Lever 1848b61d59ffSTrond Myklebust xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4); 1849b61d59ffSTrond Myklebust } 1850a246b010SChuck Lever 1851b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt, 1852b61d59ffSTrond Myklebust struct sock_xprt *transport) 1853b61d59ffSTrond Myklebust { 1854b61d59ffSTrond Myklebust struct socket *sock; 1855b61d59ffSTrond Myklebust int err; 1856b61d59ffSTrond Myklebust 1857a246b010SChuck Lever /* start from scratch */ 1858b61d59ffSTrond Myklebust err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock); 1859b61d59ffSTrond Myklebust if (err < 0) { 1860b61d59ffSTrond Myklebust dprintk("RPC: can't create TCP transport socket (%d).\n", 1861b61d59ffSTrond Myklebust -err); 1862b61d59ffSTrond Myklebust goto out_err; 18639903cd1cSChuck Lever } 1864b61d59ffSTrond Myklebust xs_reclassify_socket6(sock); 18659903cd1cSChuck Lever 1866b61d59ffSTrond Myklebust if (xs_bind6(transport, sock) < 0) { 1867a246b010SChuck Lever sock_release(sock); 1868b61d59ffSTrond Myklebust goto out_err; 1869a246b010SChuck Lever } 1870b61d59ffSTrond Myklebust return sock; 1871b61d59ffSTrond Myklebust out_err: 1872b61d59ffSTrond Myklebust return ERR_PTR(-EIO); 1873a246b010SChuck Lever } 1874a246b010SChuck Lever 1875a246b010SChuck Lever /** 187668e220bdSChuck Lever * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint 187768e220bdSChuck Lever * @work: RPC transport to connect 187868e220bdSChuck Lever * 187968e220bdSChuck Lever * Invoked by a work queue tasklet. 188068e220bdSChuck Lever */ 188168e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work) 188268e220bdSChuck Lever { 188368e220bdSChuck Lever struct sock_xprt *transport = 188468e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 188568e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 188668e220bdSChuck Lever 1887b61d59ffSTrond Myklebust xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6); 188868e220bdSChuck Lever } 188968e220bdSChuck Lever 189068e220bdSChuck Lever /** 1891a246b010SChuck Lever * xs_connect - connect a socket to a remote endpoint 1892a246b010SChuck Lever * @task: address of RPC task that manages state of connect request 1893a246b010SChuck Lever * 1894a246b010SChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 189503bf4b70SChuck Lever * 189603bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 189703bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 189803bf4b70SChuck Lever * socket on a privileged port. 189903bf4b70SChuck Lever * 190003bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 190103bf4b70SChuck Lever * retry floods (hard mounts). 1902a246b010SChuck Lever */ 1903a246b010SChuck Lever static void xs_connect(struct rpc_task *task) 1904a246b010SChuck Lever { 1905a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1906ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1907a246b010SChuck Lever 1908b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1909b0d93ad5SChuck Lever return; 1910b0d93ad5SChuck Lever 1911ee0ac0c2SChuck Lever if (transport->sock != NULL) { 191246121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 191346121cf7SChuck Lever "seconds\n", 191403bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 1915c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1916c1384c9cSTrond Myklebust &transport->connect_worker, 191703bf4b70SChuck Lever xprt->reestablish_timeout); 191803bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 191903bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 192003bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 19219903cd1cSChuck Lever } else { 19229903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 1923c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1924c1384c9cSTrond Myklebust &transport->connect_worker, 0); 1925a246b010SChuck Lever } 1926a246b010SChuck Lever } 1927a246b010SChuck Lever 1928e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task) 1929e06799f9STrond Myklebust { 1930e06799f9STrond Myklebust struct rpc_xprt *xprt = task->tk_xprt; 1931e06799f9STrond Myklebust 1932e06799f9STrond Myklebust /* Exit if we need to wait for socket shutdown to complete */ 1933e06799f9STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 1934e06799f9STrond Myklebust return; 1935e06799f9STrond Myklebust xs_connect(task); 1936e06799f9STrond Myklebust } 1937e06799f9STrond Myklebust 1938262ca07dSChuck Lever /** 1939262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 1940262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1941262ca07dSChuck Lever * @seq: output file 1942262ca07dSChuck Lever * 1943262ca07dSChuck Lever */ 1944262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1945262ca07dSChuck Lever { 1946c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1947c8475461SChuck Lever 1948262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 1949c8475461SChuck Lever transport->port, 1950262ca07dSChuck Lever xprt->stat.bind_count, 1951262ca07dSChuck Lever xprt->stat.sends, 1952262ca07dSChuck Lever xprt->stat.recvs, 1953262ca07dSChuck Lever xprt->stat.bad_xids, 1954262ca07dSChuck Lever xprt->stat.req_u, 1955262ca07dSChuck Lever xprt->stat.bklog_u); 1956262ca07dSChuck Lever } 1957262ca07dSChuck Lever 1958262ca07dSChuck Lever /** 1959262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 1960262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1961262ca07dSChuck Lever * @seq: output file 1962262ca07dSChuck Lever * 1963262ca07dSChuck Lever */ 1964262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1965262ca07dSChuck Lever { 1966c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1967262ca07dSChuck Lever long idle_time = 0; 1968262ca07dSChuck Lever 1969262ca07dSChuck Lever if (xprt_connected(xprt)) 1970262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 1971262ca07dSChuck Lever 1972262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 1973c8475461SChuck Lever transport->port, 1974262ca07dSChuck Lever xprt->stat.bind_count, 1975262ca07dSChuck Lever xprt->stat.connect_count, 1976262ca07dSChuck Lever xprt->stat.connect_time, 1977262ca07dSChuck Lever idle_time, 1978262ca07dSChuck Lever xprt->stat.sends, 1979262ca07dSChuck Lever xprt->stat.recvs, 1980262ca07dSChuck Lever xprt->stat.bad_xids, 1981262ca07dSChuck Lever xprt->stat.req_u, 1982262ca07dSChuck Lever xprt->stat.bklog_u); 1983262ca07dSChuck Lever } 1984262ca07dSChuck Lever 1985262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 198643118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 198712a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 198849e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 198945160d62SChuck Lever .rpcbind = rpcb_getport_async, 199092200412SChuck Lever .set_port = xs_set_port, 19919903cd1cSChuck Lever .connect = xs_connect, 199202107148SChuck Lever .buf_alloc = rpc_malloc, 199302107148SChuck Lever .buf_free = rpc_free, 1994262965f5SChuck Lever .send_request = xs_udp_send_request, 1995fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 199646c0ee8bSChuck Lever .timer = xs_udp_timer, 1997a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 1998262965f5SChuck Lever .close = xs_close, 1999262965f5SChuck Lever .destroy = xs_destroy, 2000262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2001262965f5SChuck Lever }; 2002262965f5SChuck Lever 2003262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 200412a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 2005e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 200645160d62SChuck Lever .rpcbind = rpcb_getport_async, 200792200412SChuck Lever .set_port = xs_set_port, 2008e06799f9STrond Myklebust .connect = xs_tcp_connect, 200902107148SChuck Lever .buf_alloc = rpc_malloc, 201002107148SChuck Lever .buf_free = rpc_free, 2011262965f5SChuck Lever .send_request = xs_tcp_send_request, 2012fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2013f75e6745STrond Myklebust .close = xs_tcp_close, 20149903cd1cSChuck Lever .destroy = xs_destroy, 2015262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2016a246b010SChuck Lever }; 2017a246b010SChuck Lever 20183c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2019bc25571eS\"Talpey, Thomas\ unsigned int slot_table_size) 2020c8541ecdSChuck Lever { 2021c8541ecdSChuck Lever struct rpc_xprt *xprt; 2022ffc2e518SChuck Lever struct sock_xprt *new; 2023c8541ecdSChuck Lever 202496802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2025c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2026c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2027c8541ecdSChuck Lever } 2028c8541ecdSChuck Lever 2029ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 2030ffc2e518SChuck Lever if (new == NULL) { 203146121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 203246121cf7SChuck Lever "rpc_xprt\n"); 2033c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2034c8541ecdSChuck Lever } 2035ffc2e518SChuck Lever xprt = &new->xprt; 2036c8541ecdSChuck Lever 2037c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 2038c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 2039c8541ecdSChuck Lever if (xprt->slot == NULL) { 2040c8541ecdSChuck Lever kfree(xprt); 204146121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 204246121cf7SChuck Lever "table\n"); 2043c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2044c8541ecdSChuck Lever } 2045c8541ecdSChuck Lever 204696802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 204796802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2048d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2049d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 2050c8541ecdSChuck Lever 2051c8541ecdSChuck Lever return xprt; 2052c8541ecdSChuck Lever } 2053c8541ecdSChuck Lever 20542881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 20552881ae74STrond Myklebust .to_initval = 5 * HZ, 20562881ae74STrond Myklebust .to_maxval = 30 * HZ, 20572881ae74STrond Myklebust .to_increment = 5 * HZ, 20582881ae74STrond Myklebust .to_retries = 5, 20592881ae74STrond Myklebust }; 20602881ae74STrond Myklebust 20619903cd1cSChuck Lever /** 20629903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 206396802a09SFrank van Maarseveen * @args: rpc transport creation arguments 20649903cd1cSChuck Lever * 20659903cd1cSChuck Lever */ 2066483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2067a246b010SChuck Lever { 20688f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2069c8541ecdSChuck Lever struct rpc_xprt *xprt; 2070c8475461SChuck Lever struct sock_xprt *transport; 2071a246b010SChuck Lever 207296802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 2073c8541ecdSChuck Lever if (IS_ERR(xprt)) 2074c8541ecdSChuck Lever return xprt; 2075c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2076a246b010SChuck Lever 2077ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2078808012fbSChuck Lever xprt->tsh_size = 0; 2079a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2080a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2081a246b010SChuck Lever 208203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 208303bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 208403bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 208503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2086a246b010SChuck Lever 2087262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2088a246b010SChuck Lever 2089ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2090a246b010SChuck Lever 20918f9d5b1aSChuck Lever switch (addr->sa_family) { 20928f9d5b1aSChuck Lever case AF_INET: 20938f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 20948f9d5b1aSChuck Lever xprt_set_bound(xprt); 20958f9d5b1aSChuck Lever 20968f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 20978f9d5b1aSChuck Lever xs_udp_connect_worker4); 2098b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 20998f9d5b1aSChuck Lever break; 21008f9d5b1aSChuck Lever case AF_INET6: 21018f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 21028f9d5b1aSChuck Lever xprt_set_bound(xprt); 21038f9d5b1aSChuck Lever 21048f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 21058f9d5b1aSChuck Lever xs_udp_connect_worker6); 2106b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 21078f9d5b1aSChuck Lever break; 21088f9d5b1aSChuck Lever default: 21098f9d5b1aSChuck Lever kfree(xprt); 21108f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 21118f9d5b1aSChuck Lever } 21128f9d5b1aSChuck Lever 2113edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 21147559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2115edb267a6SChuck Lever 2116bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2117c8541ecdSChuck Lever return xprt; 2118bc25571eS\"Talpey, Thomas\ 2119bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2120bc25571eS\"Talpey, Thomas\ kfree(xprt); 2121bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2122a246b010SChuck Lever } 2123a246b010SChuck Lever 21242881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 21252881ae74STrond Myklebust .to_initval = 60 * HZ, 21262881ae74STrond Myklebust .to_maxval = 60 * HZ, 21272881ae74STrond Myklebust .to_retries = 2, 21282881ae74STrond Myklebust }; 21292881ae74STrond Myklebust 21309903cd1cSChuck Lever /** 21319903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 213296802a09SFrank van Maarseveen * @args: rpc transport creation arguments 21339903cd1cSChuck Lever * 21349903cd1cSChuck Lever */ 2135483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2136a246b010SChuck Lever { 21378f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2138c8541ecdSChuck Lever struct rpc_xprt *xprt; 2139c8475461SChuck Lever struct sock_xprt *transport; 2140a246b010SChuck Lever 214196802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 2142c8541ecdSChuck Lever if (IS_ERR(xprt)) 2143c8541ecdSChuck Lever return xprt; 2144c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2145a246b010SChuck Lever 2146ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2147808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2148808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2149a246b010SChuck Lever 215003bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 215103bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 215203bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 215303bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2154a246b010SChuck Lever 2155262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2156ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2157a246b010SChuck Lever 21588f9d5b1aSChuck Lever switch (addr->sa_family) { 21598f9d5b1aSChuck Lever case AF_INET: 21608f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 21618f9d5b1aSChuck Lever xprt_set_bound(xprt); 21628f9d5b1aSChuck Lever 21638f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); 2164b454ae90SChuck Lever xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 21658f9d5b1aSChuck Lever break; 21668f9d5b1aSChuck Lever case AF_INET6: 21678f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 21688f9d5b1aSChuck Lever xprt_set_bound(xprt); 21698f9d5b1aSChuck Lever 21708f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); 2171b454ae90SChuck Lever xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 21728f9d5b1aSChuck Lever break; 21738f9d5b1aSChuck Lever default: 21748f9d5b1aSChuck Lever kfree(xprt); 21758f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 21768f9d5b1aSChuck Lever } 21778f9d5b1aSChuck Lever 2178edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 21797559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 2180edb267a6SChuck Lever 2181bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2182c8541ecdSChuck Lever return xprt; 2183bc25571eS\"Talpey, Thomas\ 2184bc25571eS\"Talpey, Thomas\ kfree(xprt->slot); 2185bc25571eS\"Talpey, Thomas\ kfree(xprt); 2186bc25571eS\"Talpey, Thomas\ return ERR_PTR(-EINVAL); 2187a246b010SChuck Lever } 2188282b32e1SChuck Lever 2189bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 2190bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 2191bc25571eS\"Talpey, Thomas\ .name = "udp", 2192bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 21934fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_UDP, 2194bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 2195bc25571eS\"Talpey, Thomas\ }; 2196bc25571eS\"Talpey, Thomas\ 2197bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 2198bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 2199bc25571eS\"Talpey, Thomas\ .name = "tcp", 2200bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 22014fa016ebS\"Talpey, Thomas\ .ident = IPPROTO_TCP, 2202bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 2203bc25571eS\"Talpey, Thomas\ }; 2204bc25571eS\"Talpey, Thomas\ 2205282b32e1SChuck Lever /** 2206bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 2207282b32e1SChuck Lever * 2208282b32e1SChuck Lever */ 2209282b32e1SChuck Lever int init_socket_xprt(void) 2210282b32e1SChuck Lever { 2211fbf76683SChuck Lever #ifdef RPC_DEBUG 22122b1bec5fSEric W. Biederman if (!sunrpc_table_header) 22130b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 2214fbf76683SChuck Lever #endif 2215fbf76683SChuck Lever 2216bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 2217bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 2218bc25571eS\"Talpey, Thomas\ 2219282b32e1SChuck Lever return 0; 2220282b32e1SChuck Lever } 2221282b32e1SChuck Lever 2222282b32e1SChuck Lever /** 2223bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 2224282b32e1SChuck Lever * 2225282b32e1SChuck Lever */ 2226282b32e1SChuck Lever void cleanup_socket_xprt(void) 2227282b32e1SChuck Lever { 2228fbf76683SChuck Lever #ifdef RPC_DEBUG 2229fbf76683SChuck Lever if (sunrpc_table_header) { 2230fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 2231fbf76683SChuck Lever sunrpc_table_header = NULL; 2232fbf76683SChuck Lever } 2233fbf76683SChuck Lever #endif 2234bc25571eS\"Talpey, Thomas\ 2235bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 2236bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 2237282b32e1SChuck Lever } 2238