1a246b010SChuck Lever /* 2a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 3a246b010SChuck Lever * 4a246b010SChuck Lever * Client-side transport implementation for sockets. 5a246b010SChuck Lever * 6a246b010SChuck Lever * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com> 7a246b010SChuck Lever * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com> 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> 23a246b010SChuck Lever #include <linux/capability.h> 24a246b010SChuck Lever #include <linux/pagemap.h> 25a246b010SChuck Lever #include <linux/errno.h> 26a246b010SChuck Lever #include <linux/socket.h> 27a246b010SChuck Lever #include <linux/in.h> 28a246b010SChuck Lever #include <linux/net.h> 29a246b010SChuck Lever #include <linux/mm.h> 30a246b010SChuck Lever #include <linux/udp.h> 31a246b010SChuck Lever #include <linux/tcp.h> 32a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 3302107148SChuck Lever #include <linux/sunrpc/sched.h> 34a246b010SChuck Lever #include <linux/file.h> 35a246b010SChuck Lever 36a246b010SChuck Lever #include <net/sock.h> 37a246b010SChuck Lever #include <net/checksum.h> 38a246b010SChuck Lever #include <net/udp.h> 39a246b010SChuck Lever #include <net/tcp.h> 40a246b010SChuck Lever 419903cd1cSChuck Lever /* 42c556b754SChuck Lever * xprtsock tunables 43c556b754SChuck Lever */ 44c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 45c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; 46c556b754SChuck Lever 47c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 48c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 49c556b754SChuck Lever 50c556b754SChuck Lever /* 51fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 52fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 53fbf76683SChuck Lever * directory become the union of all files registered there. 54fbf76683SChuck Lever * 55fbf76683SChuck Lever * We simply need to make sure that we don't collide with 56fbf76683SChuck Lever * someone else's file names! 57fbf76683SChuck Lever */ 58fbf76683SChuck Lever 59fbf76683SChuck Lever #ifdef RPC_DEBUG 60fbf76683SChuck Lever 61fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 62fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 63fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 64fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 65fbf76683SChuck Lever 66fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 67fbf76683SChuck Lever 68fbf76683SChuck Lever /* 69fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 70fbf76683SChuck Lever * socket buffers for existing UDP transports 71fbf76683SChuck Lever */ 72fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 73fbf76683SChuck Lever { 74fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_UDP, 75fbf76683SChuck Lever .procname = "udp_slot_table_entries", 76fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 77fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 78fbf76683SChuck Lever .mode = 0644, 79fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 80fbf76683SChuck Lever .strategy = &sysctl_intvec, 81fbf76683SChuck Lever .extra1 = &min_slot_table_size, 82fbf76683SChuck Lever .extra2 = &max_slot_table_size 83fbf76683SChuck Lever }, 84fbf76683SChuck Lever { 85fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_TCP, 86fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 87fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 88fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 89fbf76683SChuck Lever .mode = 0644, 90fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 91fbf76683SChuck Lever .strategy = &sysctl_intvec, 92fbf76683SChuck Lever .extra1 = &min_slot_table_size, 93fbf76683SChuck Lever .extra2 = &max_slot_table_size 94fbf76683SChuck Lever }, 95fbf76683SChuck Lever { 96fbf76683SChuck Lever .ctl_name = CTL_MIN_RESVPORT, 97fbf76683SChuck Lever .procname = "min_resvport", 98fbf76683SChuck Lever .data = &xprt_min_resvport, 99fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 100fbf76683SChuck Lever .mode = 0644, 101fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 102fbf76683SChuck Lever .strategy = &sysctl_intvec, 103fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 104fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 105fbf76683SChuck Lever }, 106fbf76683SChuck Lever { 107fbf76683SChuck Lever .ctl_name = CTL_MAX_RESVPORT, 108fbf76683SChuck Lever .procname = "max_resvport", 109fbf76683SChuck Lever .data = &xprt_max_resvport, 110fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 111fbf76683SChuck Lever .mode = 0644, 112fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 113fbf76683SChuck Lever .strategy = &sysctl_intvec, 114fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 115fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 116fbf76683SChuck Lever }, 117fbf76683SChuck Lever { 118fbf76683SChuck Lever .ctl_name = 0, 119fbf76683SChuck Lever }, 120fbf76683SChuck Lever }; 121fbf76683SChuck Lever 122fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 123fbf76683SChuck Lever { 124fbf76683SChuck Lever .ctl_name = CTL_SUNRPC, 125fbf76683SChuck Lever .procname = "sunrpc", 126fbf76683SChuck Lever .mode = 0555, 127fbf76683SChuck Lever .child = xs_tunables_table 128fbf76683SChuck Lever }, 129fbf76683SChuck Lever { 130fbf76683SChuck Lever .ctl_name = 0, 131fbf76683SChuck Lever }, 132fbf76683SChuck Lever }; 133fbf76683SChuck Lever 134fbf76683SChuck Lever #endif 135fbf76683SChuck Lever 136fbf76683SChuck Lever /* 137262965f5SChuck Lever * How many times to try sending a request on a socket before waiting 138262965f5SChuck Lever * for the socket buffer to clear. 139262965f5SChuck Lever */ 140262965f5SChuck Lever #define XS_SENDMSG_RETRY (10U) 141262965f5SChuck Lever 14203bf4b70SChuck Lever /* 14303bf4b70SChuck Lever * Time out for an RPC UDP socket connect. UDP socket connects are 14403bf4b70SChuck Lever * synchronous, but we set a timeout anyway in case of resource 14503bf4b70SChuck Lever * exhaustion on the local host. 14603bf4b70SChuck Lever */ 14703bf4b70SChuck Lever #define XS_UDP_CONN_TO (5U * HZ) 14803bf4b70SChuck Lever 14903bf4b70SChuck Lever /* 15003bf4b70SChuck Lever * Wait duration for an RPC TCP connection to be established. Solaris 15103bf4b70SChuck Lever * NFS over TCP uses 60 seconds, for example, which is in line with how 15203bf4b70SChuck Lever * long a server takes to reboot. 15303bf4b70SChuck Lever */ 15403bf4b70SChuck Lever #define XS_TCP_CONN_TO (60U * HZ) 15503bf4b70SChuck Lever 15603bf4b70SChuck Lever /* 15703bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 15803bf4b70SChuck Lever */ 15903bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16003bf4b70SChuck Lever 16103bf4b70SChuck Lever /* 16203bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16303bf4b70SChuck Lever * kind of resource problem on the local host. 16403bf4b70SChuck Lever */ 16503bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 16603bf4b70SChuck Lever 16703bf4b70SChuck Lever /* 16803bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 16903bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17003bf4b70SChuck Lever * 17103bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17203bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17303bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17403bf4b70SChuck Lever * increase over time if the server is down or not responding. 17503bf4b70SChuck Lever */ 17603bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 17703bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 17803bf4b70SChuck Lever 17903bf4b70SChuck Lever /* 18003bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18103bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18203bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18303bf4b70SChuck Lever */ 18403bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18503bf4b70SChuck Lever 186a246b010SChuck Lever #ifdef RPC_DEBUG 187a246b010SChuck Lever # undef RPC_DEBUG_DATA 1889903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 189a246b010SChuck Lever #endif 190a246b010SChuck Lever 191a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1929903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 193a246b010SChuck Lever { 194a246b010SChuck Lever u8 *buf = (u8 *) packet; 195a246b010SChuck Lever int j; 196a246b010SChuck Lever 197a246b010SChuck Lever dprintk("RPC: %s\n", msg); 198a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 199a246b010SChuck Lever if (!(j & 31)) { 200a246b010SChuck Lever if (j) 201a246b010SChuck Lever dprintk("\n"); 202a246b010SChuck Lever dprintk("0x%04x ", j); 203a246b010SChuck Lever } 204a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 205a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 206a246b010SChuck Lever } 207a246b010SChuck Lever dprintk("\n"); 208a246b010SChuck Lever } 209a246b010SChuck Lever #else 2109903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 211a246b010SChuck Lever { 212a246b010SChuck Lever /* NOP */ 213a246b010SChuck Lever } 214a246b010SChuck Lever #endif 215a246b010SChuck Lever 216ffc2e518SChuck Lever struct sock_xprt { 217ffc2e518SChuck Lever struct rpc_xprt xprt; 218ee0ac0c2SChuck Lever 219ee0ac0c2SChuck Lever /* 220ee0ac0c2SChuck Lever * Network layer 221ee0ac0c2SChuck Lever */ 222ee0ac0c2SChuck Lever struct socket * sock; 223ee0ac0c2SChuck Lever struct sock * inet; 22451971139SChuck Lever 22551971139SChuck Lever /* 22651971139SChuck Lever * State of TCP reply receive 22751971139SChuck Lever */ 22851971139SChuck Lever __be32 tcp_fraghdr, 22951971139SChuck Lever tcp_xid; 23051971139SChuck Lever 23151971139SChuck Lever u32 tcp_offset, 23251971139SChuck Lever tcp_reclen; 23351971139SChuck Lever 23451971139SChuck Lever unsigned long tcp_copied, 23551971139SChuck Lever tcp_flags; 236c8475461SChuck Lever 237c8475461SChuck Lever /* 238c8475461SChuck Lever * Connection of transports 239c8475461SChuck Lever */ 24034161db6STrond Myklebust struct delayed_work connect_worker; 241d3bc9a1dSFrank van Maarseveen struct sockaddr_storage addr; 242c8475461SChuck Lever unsigned short port; 2437c6e066eSChuck Lever 2447c6e066eSChuck Lever /* 2457c6e066eSChuck Lever * UDP socket buffer size parameters 2467c6e066eSChuck Lever */ 2477c6e066eSChuck Lever size_t rcvsize, 2487c6e066eSChuck Lever sndsize; 249314dfd79SChuck Lever 250314dfd79SChuck Lever /* 251314dfd79SChuck Lever * Saved socket callback addresses 252314dfd79SChuck Lever */ 253314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 254314dfd79SChuck Lever void (*old_state_change)(struct sock *); 255314dfd79SChuck Lever void (*old_write_space)(struct sock *); 256ffc2e518SChuck Lever }; 257ffc2e518SChuck Lever 258e136d092SChuck Lever /* 259e136d092SChuck Lever * TCP receive state flags 260e136d092SChuck Lever */ 261e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 262e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 263e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 264e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 265e136d092SChuck Lever 26695392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 26795392c59SChuck Lever { 26895392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 26995392c59SChuck Lever } 27095392c59SChuck Lever 27195392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 27295392c59SChuck Lever { 27395392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 27495392c59SChuck Lever } 27595392c59SChuck Lever 27695392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 27795392c59SChuck Lever { 27895392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 27995392c59SChuck Lever } 28095392c59SChuck Lever 281ba10f2c2SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt) 282edb267a6SChuck Lever { 28395392c59SChuck Lever struct sockaddr_in *addr = xs_addr_in(xprt); 284edb267a6SChuck Lever char *buf; 285edb267a6SChuck Lever 286edb267a6SChuck Lever buf = kzalloc(20, GFP_KERNEL); 287edb267a6SChuck Lever if (buf) { 288bda243dfSChuck Lever snprintf(buf, 20, NIPQUAD_FMT, 289edb267a6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 290edb267a6SChuck Lever } 291edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 292edb267a6SChuck Lever 293edb267a6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 294edb267a6SChuck Lever if (buf) { 295edb267a6SChuck Lever snprintf(buf, 8, "%u", 296edb267a6SChuck Lever ntohs(addr->sin_port)); 297edb267a6SChuck Lever } 298edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 299edb267a6SChuck Lever 3000c43b3d8SChuck Lever buf = kzalloc(8, GFP_KERNEL); 3010c43b3d8SChuck Lever if (buf) { 302edb267a6SChuck Lever if (xprt->prot == IPPROTO_UDP) 3030c43b3d8SChuck Lever snprintf(buf, 8, "udp"); 304edb267a6SChuck Lever else 3050c43b3d8SChuck Lever snprintf(buf, 8, "tcp"); 3060c43b3d8SChuck Lever } 3070c43b3d8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = buf; 308edb267a6SChuck Lever 309edb267a6SChuck Lever buf = kzalloc(48, GFP_KERNEL); 310edb267a6SChuck Lever if (buf) { 311bda243dfSChuck Lever snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s", 312edb267a6SChuck Lever NIPQUAD(addr->sin_addr.s_addr), 313edb267a6SChuck Lever ntohs(addr->sin_port), 314edb267a6SChuck Lever xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); 315edb267a6SChuck Lever } 316edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 317fbfe3cc6SChuck Lever 318fbfe3cc6SChuck Lever buf = kzalloc(10, GFP_KERNEL); 319fbfe3cc6SChuck Lever if (buf) { 320fbfe3cc6SChuck Lever snprintf(buf, 10, "%02x%02x%02x%02x", 321fbfe3cc6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 322fbfe3cc6SChuck Lever } 323fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 324fbfe3cc6SChuck Lever 325fbfe3cc6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 326fbfe3cc6SChuck Lever if (buf) { 327fbfe3cc6SChuck Lever snprintf(buf, 8, "%4hx", 328fbfe3cc6SChuck Lever ntohs(addr->sin_port)); 329fbfe3cc6SChuck Lever } 330fbfe3cc6SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 331*756805e7SChuck Lever 332*756805e7SChuck Lever buf = kzalloc(30, GFP_KERNEL); 333*756805e7SChuck Lever if (buf) { 334*756805e7SChuck Lever snprintf(buf, 30, NIPQUAD_FMT".%u.%u", 335*756805e7SChuck Lever NIPQUAD(addr->sin_addr.s_addr), 336*756805e7SChuck Lever ntohs(addr->sin_port) >> 8, 337*756805e7SChuck Lever ntohs(addr->sin_port) & 0xff); 338*756805e7SChuck Lever } 339*756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 340edb267a6SChuck Lever } 341edb267a6SChuck Lever 3424b6473fbSChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt) 3434b6473fbSChuck Lever { 34495392c59SChuck Lever struct sockaddr_in6 *addr = xs_addr_in6(xprt); 3454b6473fbSChuck Lever char *buf; 3464b6473fbSChuck Lever 3474b6473fbSChuck Lever buf = kzalloc(40, GFP_KERNEL); 3484b6473fbSChuck Lever if (buf) { 3494b6473fbSChuck Lever snprintf(buf, 40, NIP6_FMT, 3504b6473fbSChuck Lever NIP6(addr->sin6_addr)); 3514b6473fbSChuck Lever } 3524b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 3534b6473fbSChuck Lever 3544b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3554b6473fbSChuck Lever if (buf) { 3564b6473fbSChuck Lever snprintf(buf, 8, "%u", 3574b6473fbSChuck Lever ntohs(addr->sin6_port)); 3584b6473fbSChuck Lever } 3594b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 3604b6473fbSChuck Lever 3614b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3624b6473fbSChuck Lever if (buf) { 3634b6473fbSChuck Lever if (xprt->prot == IPPROTO_UDP) 3644b6473fbSChuck Lever snprintf(buf, 8, "udp"); 3654b6473fbSChuck Lever else 3664b6473fbSChuck Lever snprintf(buf, 8, "tcp"); 3674b6473fbSChuck Lever } 3684b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = buf; 3694b6473fbSChuck Lever 3704b6473fbSChuck Lever buf = kzalloc(64, GFP_KERNEL); 3714b6473fbSChuck Lever if (buf) { 3724b6473fbSChuck Lever snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s", 3734b6473fbSChuck Lever NIP6(addr->sin6_addr), 3744b6473fbSChuck Lever ntohs(addr->sin6_port), 3754b6473fbSChuck Lever xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); 3764b6473fbSChuck Lever } 3774b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 3784b6473fbSChuck Lever 3794b6473fbSChuck Lever buf = kzalloc(36, GFP_KERNEL); 3804b6473fbSChuck Lever if (buf) { 3814b6473fbSChuck Lever snprintf(buf, 36, NIP6_SEQFMT, 3824b6473fbSChuck Lever NIP6(addr->sin6_addr)); 3834b6473fbSChuck Lever } 3844b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; 3854b6473fbSChuck Lever 3864b6473fbSChuck Lever buf = kzalloc(8, GFP_KERNEL); 3874b6473fbSChuck Lever if (buf) { 3884b6473fbSChuck Lever snprintf(buf, 8, "%4hx", 3894b6473fbSChuck Lever ntohs(addr->sin6_port)); 3904b6473fbSChuck Lever } 3914b6473fbSChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; 392*756805e7SChuck Lever 393*756805e7SChuck Lever buf = kzalloc(50, GFP_KERNEL); 394*756805e7SChuck Lever if (buf) { 395*756805e7SChuck Lever snprintf(buf, 50, NIP6_FMT".%u.%u", 396*756805e7SChuck Lever NIP6(addr->sin6_addr), 397*756805e7SChuck Lever ntohs(addr->sin6_port) >> 8, 398*756805e7SChuck Lever ntohs(addr->sin6_port) & 0xff); 399*756805e7SChuck Lever } 400*756805e7SChuck Lever xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; 4014b6473fbSChuck Lever } 4024b6473fbSChuck Lever 403edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 404edb267a6SChuck Lever { 4050c43b3d8SChuck Lever int i; 4060c43b3d8SChuck Lever 4070c43b3d8SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 4080c43b3d8SChuck Lever kfree(xprt->address_strings[i]); 409edb267a6SChuck Lever } 410edb267a6SChuck Lever 411b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 412b4b5cc85SChuck Lever 41324c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 414b4b5cc85SChuck Lever { 415b4b5cc85SChuck Lever struct msghdr msg = { 416b4b5cc85SChuck Lever .msg_name = addr, 417b4b5cc85SChuck Lever .msg_namelen = addrlen, 41824c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 41924c5684bSTrond Myklebust }; 42024c5684bSTrond Myklebust struct kvec iov = { 42124c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 42224c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 423b4b5cc85SChuck Lever }; 424b4b5cc85SChuck Lever 42524c5684bSTrond Myklebust if (iov.iov_len != 0) 426b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 427b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 428b4b5cc85SChuck Lever } 429b4b5cc85SChuck Lever 43024c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 431b4b5cc85SChuck Lever { 43224c5684bSTrond Myklebust struct page **ppage; 43324c5684bSTrond Myklebust unsigned int remainder; 43424c5684bSTrond Myklebust int err, sent = 0; 435b4b5cc85SChuck Lever 43624c5684bSTrond Myklebust remainder = xdr->page_len - base; 43724c5684bSTrond Myklebust base += xdr->page_base; 43824c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 43924c5684bSTrond Myklebust base &= ~PAGE_MASK; 44024c5684bSTrond Myklebust for(;;) { 44124c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 44224c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 44324c5684bSTrond Myklebust 44424c5684bSTrond Myklebust remainder -= len; 44524c5684bSTrond Myklebust if (remainder != 0 || more) 44624c5684bSTrond Myklebust flags |= MSG_MORE; 44724c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 44824c5684bSTrond Myklebust if (remainder == 0 || err != len) 44924c5684bSTrond Myklebust break; 45024c5684bSTrond Myklebust sent += err; 45124c5684bSTrond Myklebust ppage++; 45224c5684bSTrond Myklebust base = 0; 45324c5684bSTrond Myklebust } 45424c5684bSTrond Myklebust if (sent == 0) 45524c5684bSTrond Myklebust return err; 45624c5684bSTrond Myklebust if (err > 0) 45724c5684bSTrond Myklebust sent += err; 45824c5684bSTrond Myklebust return sent; 459b4b5cc85SChuck Lever } 460b4b5cc85SChuck Lever 4619903cd1cSChuck Lever /** 4629903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 4639903cd1cSChuck Lever * @sock: socket to send on 4649903cd1cSChuck Lever * @addr: UDP only -- address of destination 4659903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 4669903cd1cSChuck Lever * @xdr: buffer containing this request 4679903cd1cSChuck Lever * @base: starting position in the buffer 4689903cd1cSChuck Lever * 469a246b010SChuck Lever */ 47024c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 471a246b010SChuck Lever { 47224c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 47324c5684bSTrond Myklebust int err, sent = 0; 474a246b010SChuck Lever 475262965f5SChuck Lever if (unlikely(!sock)) 476262965f5SChuck Lever return -ENOTCONN; 477262965f5SChuck Lever 478262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 47924c5684bSTrond Myklebust if (base != 0) { 48024c5684bSTrond Myklebust addr = NULL; 48124c5684bSTrond Myklebust addrlen = 0; 48224c5684bSTrond Myklebust } 483262965f5SChuck Lever 48424c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 48524c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 48624c5684bSTrond Myklebust remainder -= len; 48724c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 48824c5684bSTrond Myklebust if (remainder == 0 || err != len) 489a246b010SChuck Lever goto out; 49024c5684bSTrond Myklebust sent += err; 491a246b010SChuck Lever base = 0; 492a246b010SChuck Lever } else 49324c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 494a246b010SChuck Lever 49524c5684bSTrond Myklebust if (base < xdr->page_len) { 49624c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 49724c5684bSTrond Myklebust remainder -= len; 49824c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 49924c5684bSTrond Myklebust if (remainder == 0 || err != len) 500a246b010SChuck Lever goto out; 50124c5684bSTrond Myklebust sent += err; 502a246b010SChuck Lever base = 0; 50324c5684bSTrond Myklebust } else 50424c5684bSTrond Myklebust base -= xdr->page_len; 50524c5684bSTrond Myklebust 50624c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 50724c5684bSTrond Myklebust return sent; 50824c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 509a246b010SChuck Lever out: 51024c5684bSTrond Myklebust if (sent == 0) 51124c5684bSTrond Myklebust return err; 51224c5684bSTrond Myklebust if (err > 0) 51324c5684bSTrond Myklebust sent += err; 51424c5684bSTrond Myklebust return sent; 515a246b010SChuck Lever } 516a246b010SChuck Lever 5179903cd1cSChuck Lever /** 518262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 519262965f5SChuck Lever * @task: task to put to sleep 5209903cd1cSChuck Lever * 521a246b010SChuck Lever */ 522262965f5SChuck Lever static void xs_nospace(struct rpc_task *task) 523a246b010SChuck Lever { 524262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 525262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 526ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 527a246b010SChuck Lever 52846121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 529262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 530262965f5SChuck Lever req->rq_slen); 531a246b010SChuck Lever 532ee0ac0c2SChuck Lever if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 533262965f5SChuck Lever /* Protect against races with write_space */ 534262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 535a246b010SChuck Lever 536262965f5SChuck Lever /* Don't race with disconnect */ 537262965f5SChuck Lever if (!xprt_connected(xprt)) 538262965f5SChuck Lever task->tk_status = -ENOTCONN; 539ee0ac0c2SChuck Lever else if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 540262965f5SChuck Lever xprt_wait_for_buffer_space(task); 541a246b010SChuck Lever 542262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 543262965f5SChuck Lever } else 544262965f5SChuck Lever /* Keep holding the socket if it is blocked */ 545262965f5SChuck Lever rpc_delay(task, HZ>>4); 546a246b010SChuck Lever } 547a246b010SChuck Lever 5489903cd1cSChuck Lever /** 549262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5509903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5519903cd1cSChuck Lever * 5529903cd1cSChuck Lever * Return values: 5539903cd1cSChuck Lever * 0: The request has been sent 5549903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5559903cd1cSChuck Lever * complete the request 556262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 5579903cd1cSChuck Lever * other: Some other error occured, the request was not sent 5589903cd1cSChuck Lever */ 559262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 560a246b010SChuck Lever { 561a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 562a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 563ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 564262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 565262965f5SChuck Lever int status; 566262965f5SChuck Lever 567262965f5SChuck Lever xs_pktdump("packet data:", 568262965f5SChuck Lever req->rq_svec->iov_base, 569262965f5SChuck Lever req->rq_svec->iov_len); 570262965f5SChuck Lever 571262965f5SChuck Lever req->rq_xtime = jiffies; 572ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 57395392c59SChuck Lever xs_addr(xprt), 574ee0ac0c2SChuck Lever xprt->addrlen, xdr, 575ee0ac0c2SChuck Lever req->rq_bytes_sent); 576262965f5SChuck Lever 577262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 578262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 579262965f5SChuck Lever 580262965f5SChuck Lever if (likely(status >= (int) req->rq_slen)) 581262965f5SChuck Lever return 0; 582262965f5SChuck Lever 583262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 584262965f5SChuck Lever if (status > 0) 585262965f5SChuck Lever status = -EAGAIN; 586262965f5SChuck Lever 587262965f5SChuck Lever switch (status) { 588262965f5SChuck Lever case -ENETUNREACH: 589262965f5SChuck Lever case -EPIPE: 590262965f5SChuck Lever case -ECONNREFUSED: 591262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 592262965f5SChuck Lever * prompts ECONNREFUSED. */ 593262965f5SChuck Lever break; 594262965f5SChuck Lever case -EAGAIN: 595262965f5SChuck Lever xs_nospace(task); 596262965f5SChuck Lever break; 597262965f5SChuck Lever default: 598262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 599262965f5SChuck Lever -status); 600262965f5SChuck Lever break; 601262965f5SChuck Lever } 602262965f5SChuck Lever 603262965f5SChuck Lever return status; 604262965f5SChuck Lever } 605262965f5SChuck Lever 606808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 607808012fbSChuck Lever { 608808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 609808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 610808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 611808012fbSChuck Lever } 612808012fbSChuck Lever 613262965f5SChuck Lever /** 614262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 615262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 616262965f5SChuck Lever * 617262965f5SChuck Lever * Return values: 618262965f5SChuck Lever * 0: The request has been sent 619262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 620262965f5SChuck Lever * complete the request 621262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 622262965f5SChuck Lever * other: Some other error occured, the request was not sent 623262965f5SChuck Lever * 624262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 625262965f5SChuck Lever * if sendmsg is not able to make progress? 626262965f5SChuck Lever */ 627262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 628262965f5SChuck Lever { 629262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 630262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 631ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 632262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 633b595bb15SChuck Lever int status; 634b595bb15SChuck Lever unsigned int retry = 0; 635a246b010SChuck Lever 636808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 637262965f5SChuck Lever 638262965f5SChuck Lever xs_pktdump("packet data:", 639262965f5SChuck Lever req->rq_svec->iov_base, 640262965f5SChuck Lever req->rq_svec->iov_len); 641a246b010SChuck Lever 642a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 643a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 644262965f5SChuck Lever * called sendmsg(). */ 645a246b010SChuck Lever while (1) { 646a246b010SChuck Lever req->rq_xtime = jiffies; 647ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 648ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 649a246b010SChuck Lever 650262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 651262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 652262965f5SChuck Lever 653262965f5SChuck Lever if (unlikely(status < 0)) 654a246b010SChuck Lever break; 655a246b010SChuck Lever 656a246b010SChuck Lever /* If we've sent the entire packet, immediately 657a246b010SChuck Lever * reset the count of bytes sent. */ 658262965f5SChuck Lever req->rq_bytes_sent += status; 659ef759a2eSChuck Lever task->tk_bytes_sent += status; 660262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 661a246b010SChuck Lever req->rq_bytes_sent = 0; 662a246b010SChuck Lever return 0; 663a246b010SChuck Lever } 664262965f5SChuck Lever 665a246b010SChuck Lever status = -EAGAIN; 666262965f5SChuck Lever if (retry++ > XS_SENDMSG_RETRY) 667a246b010SChuck Lever break; 668a246b010SChuck Lever } 669a246b010SChuck Lever 670262965f5SChuck Lever switch (status) { 671262965f5SChuck Lever case -EAGAIN: 672262965f5SChuck Lever xs_nospace(task); 673262965f5SChuck Lever break; 674262965f5SChuck Lever case -ECONNREFUSED: 675262965f5SChuck Lever case -ECONNRESET: 676262965f5SChuck Lever case -ENOTCONN: 677262965f5SChuck Lever case -EPIPE: 678262965f5SChuck Lever status = -ENOTCONN; 679262965f5SChuck Lever break; 680262965f5SChuck Lever default: 681262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 682262965f5SChuck Lever -status); 68343118c29SChuck Lever xprt_disconnect(xprt); 684a246b010SChuck Lever break; 685a246b010SChuck Lever } 686a246b010SChuck Lever 687a246b010SChuck Lever return status; 688a246b010SChuck Lever } 689a246b010SChuck Lever 6909903cd1cSChuck Lever /** 691e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 692e0ab53deSTrond Myklebust * @xprt: transport 693e0ab53deSTrond Myklebust * @task: rpc task 694e0ab53deSTrond Myklebust * 695e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 696e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 697e0ab53deSTrond Myklebust * the server. 698e0ab53deSTrond Myklebust */ 699e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 700e0ab53deSTrond Myklebust { 701e0ab53deSTrond Myklebust struct rpc_rqst *req; 702e0ab53deSTrond Myklebust 703e0ab53deSTrond Myklebust if (task != xprt->snd_task) 704e0ab53deSTrond Myklebust return; 705e0ab53deSTrond Myklebust if (task == NULL) 706e0ab53deSTrond Myklebust goto out_release; 707e0ab53deSTrond Myklebust req = task->tk_rqstp; 708e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 709e0ab53deSTrond Myklebust goto out_release; 710e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 711e0ab53deSTrond Myklebust goto out_release; 712e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 713e0ab53deSTrond Myklebust out_release: 714e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 715e0ab53deSTrond Myklebust } 716e0ab53deSTrond Myklebust 717e0ab53deSTrond Myklebust /** 7189903cd1cSChuck Lever * xs_close - close a socket 7199903cd1cSChuck Lever * @xprt: transport 7209903cd1cSChuck Lever * 7213167e12cSChuck Lever * This is used when all requests are complete; ie, no DRC state remains 7223167e12cSChuck Lever * on the server we want to save. 723a246b010SChuck Lever */ 7249903cd1cSChuck Lever static void xs_close(struct rpc_xprt *xprt) 725a246b010SChuck Lever { 726ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 727ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 728ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 729a246b010SChuck Lever 730a246b010SChuck Lever if (!sk) 731632e3bdcSTrond Myklebust goto clear_close_wait; 732a246b010SChuck Lever 7339903cd1cSChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 7349903cd1cSChuck Lever 735a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 736ee0ac0c2SChuck Lever transport->inet = NULL; 737ee0ac0c2SChuck Lever transport->sock = NULL; 738a246b010SChuck Lever 739a246b010SChuck Lever sk->sk_user_data = NULL; 740314dfd79SChuck Lever sk->sk_data_ready = transport->old_data_ready; 741314dfd79SChuck Lever sk->sk_state_change = transport->old_state_change; 742314dfd79SChuck Lever sk->sk_write_space = transport->old_write_space; 743a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 744a246b010SChuck Lever 745a246b010SChuck Lever sk->sk_no_check = 0; 746a246b010SChuck Lever 747a246b010SChuck Lever sock_release(sock); 748632e3bdcSTrond Myklebust clear_close_wait: 749632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 750632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 751632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 752a246b010SChuck Lever } 753a246b010SChuck Lever 7549903cd1cSChuck Lever /** 7559903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 7569903cd1cSChuck Lever * @xprt: doomed transport 7579903cd1cSChuck Lever * 7589903cd1cSChuck Lever */ 7599903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 760a246b010SChuck Lever { 761c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 762c8475461SChuck Lever 7639903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 7649903cd1cSChuck Lever 765c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 766a246b010SChuck Lever 767a246b010SChuck Lever xprt_disconnect(xprt); 7689903cd1cSChuck Lever xs_close(xprt); 769edb267a6SChuck Lever xs_free_peer_addresses(xprt); 770a246b010SChuck Lever kfree(xprt->slot); 771c8541ecdSChuck Lever kfree(xprt); 772a246b010SChuck Lever } 773a246b010SChuck Lever 7749903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 7759903cd1cSChuck Lever { 7769903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 7779903cd1cSChuck Lever } 7789903cd1cSChuck Lever 7799903cd1cSChuck Lever /** 7809903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 7819903cd1cSChuck Lever * @sk: socket with data to read 7829903cd1cSChuck Lever * @len: how much data to read 7839903cd1cSChuck Lever * 784a246b010SChuck Lever */ 7859903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 786a246b010SChuck Lever { 787a246b010SChuck Lever struct rpc_task *task; 788a246b010SChuck Lever struct rpc_xprt *xprt; 789a246b010SChuck Lever struct rpc_rqst *rovr; 790a246b010SChuck Lever struct sk_buff *skb; 791a246b010SChuck Lever int err, repsize, copied; 792d8ed029dSAlexey Dobriyan u32 _xid; 793d8ed029dSAlexey Dobriyan __be32 *xp; 794a246b010SChuck Lever 795a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 7969903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 7979903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 798a246b010SChuck Lever goto out; 799a246b010SChuck Lever 800a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 801a246b010SChuck Lever goto out; 802a246b010SChuck Lever 803a246b010SChuck Lever if (xprt->shutdown) 804a246b010SChuck Lever goto dropit; 805a246b010SChuck Lever 806a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 807a246b010SChuck Lever if (repsize < 4) { 8089903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 809a246b010SChuck Lever goto dropit; 810a246b010SChuck Lever } 811a246b010SChuck Lever 812a246b010SChuck Lever /* Copy the XID from the skb... */ 813a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 814a246b010SChuck Lever sizeof(_xid), &_xid); 815a246b010SChuck Lever if (xp == NULL) 816a246b010SChuck Lever goto dropit; 817a246b010SChuck Lever 818a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 8194a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 820a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 821a246b010SChuck Lever if (!rovr) 822a246b010SChuck Lever goto out_unlock; 823a246b010SChuck Lever task = rovr->rq_task; 824a246b010SChuck Lever 825a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 826a246b010SChuck Lever copied = repsize; 827a246b010SChuck Lever 828a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 829a246b010SChuck Lever if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) 830a246b010SChuck Lever goto out_unlock; 831a246b010SChuck Lever 832a246b010SChuck Lever /* Something worked... */ 833a246b010SChuck Lever dst_confirm(skb->dst); 834a246b010SChuck Lever 8351570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 8361570c1e4SChuck Lever xprt_update_rtt(task); 8371570c1e4SChuck Lever xprt_complete_rqst(task, copied); 838a246b010SChuck Lever 839a246b010SChuck Lever out_unlock: 8404a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 841a246b010SChuck Lever dropit: 842a246b010SChuck Lever skb_free_datagram(sk, skb); 843a246b010SChuck Lever out: 844a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 845a246b010SChuck Lever } 846a246b010SChuck Lever 847dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 848a246b010SChuck Lever { 84951971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 850a246b010SChuck Lever size_t len, used; 851a246b010SChuck Lever char *p; 852a246b010SChuck Lever 85351971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 85451971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 8559d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 85651971139SChuck Lever transport->tcp_offset += used; 857a246b010SChuck Lever if (used != len) 858a246b010SChuck Lever return; 859808012fbSChuck Lever 86051971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 86151971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 862e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 863a246b010SChuck Lever else 864e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 86551971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 866808012fbSChuck Lever 867e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 86851971139SChuck Lever transport->tcp_offset = 0; 869808012fbSChuck Lever 870a246b010SChuck Lever /* Sanity check of the record length */ 87151971139SChuck Lever if (unlikely(transport->tcp_reclen < 4)) { 8729903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 873a246b010SChuck Lever xprt_disconnect(xprt); 8749903cd1cSChuck Lever return; 875a246b010SChuck Lever } 876a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 87751971139SChuck Lever transport->tcp_reclen); 878a246b010SChuck Lever } 879a246b010SChuck Lever 88051971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 881a246b010SChuck Lever { 88251971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 883e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 88451971139SChuck Lever transport->tcp_offset = 0; 885e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 886e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 887e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 88851971139SChuck Lever transport->tcp_copied = 0; 889a246b010SChuck Lever } 890a246b010SChuck Lever } 891a246b010SChuck Lever } 892a246b010SChuck Lever 893dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 894a246b010SChuck Lever { 895a246b010SChuck Lever size_t len, used; 896a246b010SChuck Lever char *p; 897a246b010SChuck Lever 89851971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 899a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 90051971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 9019d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 90251971139SChuck Lever transport->tcp_offset += used; 903a246b010SChuck Lever if (used != len) 904a246b010SChuck Lever return; 905e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 906e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_DATA; 90751971139SChuck Lever transport->tcp_copied = 4; 908a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 90951971139SChuck Lever ntohl(transport->tcp_xid)); 91051971139SChuck Lever xs_tcp_check_fraghdr(transport); 911a246b010SChuck Lever } 912a246b010SChuck Lever 913dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 914a246b010SChuck Lever { 91551971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 916a246b010SChuck Lever struct rpc_rqst *req; 917a246b010SChuck Lever struct xdr_buf *rcvbuf; 918a246b010SChuck Lever size_t len; 919a246b010SChuck Lever ssize_t r; 920a246b010SChuck Lever 921a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 9224a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 92351971139SChuck Lever req = xprt_lookup_rqst(xprt, transport->tcp_xid); 924a246b010SChuck Lever if (!req) { 925e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 926a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 92751971139SChuck Lever ntohl(transport->tcp_xid)); 9284a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 929a246b010SChuck Lever return; 930a246b010SChuck Lever } 931a246b010SChuck Lever 932a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 933a246b010SChuck Lever len = desc->count; 93451971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 935dd456471SChuck Lever struct xdr_skb_reader my_desc; 936a246b010SChuck Lever 93751971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 938a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 939a246b010SChuck Lever my_desc.count = len; 94051971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 9419d292316SChuck Lever &my_desc, xdr_skb_read_bits); 942a246b010SChuck Lever desc->count -= r; 943a246b010SChuck Lever desc->offset += r; 944a246b010SChuck Lever } else 94551971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 9469d292316SChuck Lever desc, xdr_skb_read_bits); 947a246b010SChuck Lever 948a246b010SChuck Lever if (r > 0) { 94951971139SChuck Lever transport->tcp_copied += r; 95051971139SChuck Lever transport->tcp_offset += r; 951a246b010SChuck Lever } 952a246b010SChuck Lever if (r != len) { 953a246b010SChuck Lever /* Error when copying to the receive buffer, 954a246b010SChuck Lever * usually because we weren't able to allocate 955a246b010SChuck Lever * additional buffer pages. All we can do now 956e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 957a246b010SChuck Lever * will not receive any additional updates, 958a246b010SChuck Lever * and time out. 959a246b010SChuck Lever * Any remaining data from this record will 960a246b010SChuck Lever * be discarded. 961a246b010SChuck Lever */ 962e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 963a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 96451971139SChuck Lever ntohl(transport->tcp_xid)); 96546121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 96646121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 96746121cf7SChuck Lever xprt, transport->tcp_copied, 96846121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 969a246b010SChuck Lever goto out; 970a246b010SChuck Lever } 971a246b010SChuck Lever 972a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 97351971139SChuck Lever ntohl(transport->tcp_xid), r); 97446121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 97546121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 97646121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 977a246b010SChuck Lever 97851971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 979e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 98051971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 981e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 982e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 983a246b010SChuck Lever } 984a246b010SChuck Lever 985a246b010SChuck Lever out: 986e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 98751971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 9884a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 98951971139SChuck Lever xs_tcp_check_fraghdr(transport); 990a246b010SChuck Lever } 991a246b010SChuck Lever 992dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 993a246b010SChuck Lever { 994a246b010SChuck Lever size_t len; 995a246b010SChuck Lever 99651971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 997a246b010SChuck Lever if (len > desc->count) 998a246b010SChuck Lever len = desc->count; 999a246b010SChuck Lever desc->count -= len; 1000a246b010SChuck Lever desc->offset += len; 100151971139SChuck Lever transport->tcp_offset += len; 1002a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 100351971139SChuck Lever xs_tcp_check_fraghdr(transport); 1004a246b010SChuck Lever } 1005a246b010SChuck Lever 10069903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1007a246b010SChuck Lever { 1008a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 100951971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1010dd456471SChuck Lever struct xdr_skb_reader desc = { 1011a246b010SChuck Lever .skb = skb, 1012a246b010SChuck Lever .offset = offset, 1013a246b010SChuck Lever .count = len, 1014a246b010SChuck Lever }; 1015a246b010SChuck Lever 10169903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1017a246b010SChuck Lever do { 1018a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1019a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1020e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 10219903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1022a246b010SChuck Lever continue; 1023a246b010SChuck Lever } 1024a246b010SChuck Lever /* Read in the xid if necessary */ 1025e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 102651971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1027a246b010SChuck Lever continue; 1028a246b010SChuck Lever } 1029a246b010SChuck Lever /* Read in the request data */ 1030e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 10319903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 1032a246b010SChuck Lever continue; 1033a246b010SChuck Lever } 1034a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 103551971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1036a246b010SChuck Lever } while (desc.count); 10379903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1038a246b010SChuck Lever return len - desc.count; 1039a246b010SChuck Lever } 1040a246b010SChuck Lever 10419903cd1cSChuck Lever /** 10429903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 10439903cd1cSChuck Lever * @sk: socket with data to read 10449903cd1cSChuck Lever * @bytes: how much data to read 10459903cd1cSChuck Lever * 10469903cd1cSChuck Lever */ 10479903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1048a246b010SChuck Lever { 1049a246b010SChuck Lever struct rpc_xprt *xprt; 1050a246b010SChuck Lever read_descriptor_t rd_desc; 1051a246b010SChuck Lever 10529903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 105346121cf7SChuck Lever 105446121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 10559903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1056a246b010SChuck Lever goto out; 1057a246b010SChuck Lever if (xprt->shutdown) 1058a246b010SChuck Lever goto out; 1059a246b010SChuck Lever 10609903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1061a246b010SChuck Lever rd_desc.arg.data = xprt; 1062a246b010SChuck Lever rd_desc.count = 65536; 10639903cd1cSChuck Lever tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1064a246b010SChuck Lever out: 1065a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1066a246b010SChuck Lever } 1067a246b010SChuck Lever 10689903cd1cSChuck Lever /** 10699903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 10709903cd1cSChuck Lever * @sk: socket whose state has changed 10719903cd1cSChuck Lever * 10729903cd1cSChuck Lever */ 10739903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1074a246b010SChuck Lever { 1075a246b010SChuck Lever struct rpc_xprt *xprt; 1076a246b010SChuck Lever 1077a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1078a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1079a246b010SChuck Lever goto out; 10809903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1081a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 1082a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1083a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1084a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 1085a246b010SChuck Lever 1086a246b010SChuck Lever switch (sk->sk_state) { 1087a246b010SChuck Lever case TCP_ESTABLISHED: 10884a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 1089a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 109051971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 109151971139SChuck Lever struct sock_xprt, xprt); 109251971139SChuck Lever 1093a246b010SChuck Lever /* Reset TCP record info */ 109451971139SChuck Lever transport->tcp_offset = 0; 109551971139SChuck Lever transport->tcp_reclen = 0; 109651971139SChuck Lever transport->tcp_copied = 0; 1097e136d092SChuck Lever transport->tcp_flags = 1098e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 109951971139SChuck Lever 110003bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 110144fbac22SChuck Lever xprt_wake_pending_tasks(xprt, 0); 1102a246b010SChuck Lever } 11034a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1104a246b010SChuck Lever break; 1105a246b010SChuck Lever case TCP_SYN_SENT: 1106a246b010SChuck Lever case TCP_SYN_RECV: 1107a246b010SChuck Lever break; 1108632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 1109632e3bdcSTrond Myklebust /* Try to schedule an autoclose RPC calls */ 1110632e3bdcSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 1111632e3bdcSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 1112c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 1113a246b010SChuck Lever default: 1114a246b010SChuck Lever xprt_disconnect(xprt); 1115a246b010SChuck Lever } 1116a246b010SChuck Lever out: 1117a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1118a246b010SChuck Lever } 1119a246b010SChuck Lever 11209903cd1cSChuck Lever /** 1121c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1122c7b2cae8SChuck Lever * becomes available 11239903cd1cSChuck Lever * @sk: socket whose state has changed 11249903cd1cSChuck Lever * 1125a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1126a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1127c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1128a246b010SChuck Lever * with a bunch of small requests. 1129a246b010SChuck Lever */ 1130c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1131a246b010SChuck Lever { 1132a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1133c7b2cae8SChuck Lever 1134c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 1135c7b2cae8SChuck Lever if (sock_writeable(sk)) { 1136c7b2cae8SChuck Lever struct socket *sock; 1137c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1138c7b2cae8SChuck Lever 1139c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1140a246b010SChuck Lever goto out; 1141c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1142c7b2cae8SChuck Lever goto out; 1143c7b2cae8SChuck Lever if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) 1144a246b010SChuck Lever goto out; 1145a246b010SChuck Lever 1146c7b2cae8SChuck Lever xprt_write_space(xprt); 1147a246b010SChuck Lever } 1148a246b010SChuck Lever 1149c7b2cae8SChuck Lever out: 1150c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1151c7b2cae8SChuck Lever } 1152c7b2cae8SChuck Lever 1153c7b2cae8SChuck Lever /** 1154c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1155c7b2cae8SChuck Lever * becomes available 1156c7b2cae8SChuck Lever * @sk: socket whose state has changed 1157c7b2cae8SChuck Lever * 1158c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1159c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1160c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1161c7b2cae8SChuck Lever * with a bunch of small requests. 1162c7b2cae8SChuck Lever */ 1163c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1164c7b2cae8SChuck Lever { 1165c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1166c7b2cae8SChuck Lever 1167c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 1168c7b2cae8SChuck Lever if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 1169c7b2cae8SChuck Lever struct socket *sock; 1170c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1171c7b2cae8SChuck Lever 1172c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1173c7b2cae8SChuck Lever goto out; 1174c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1175c7b2cae8SChuck Lever goto out; 1176c7b2cae8SChuck Lever if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) 1177a246b010SChuck Lever goto out; 1178a246b010SChuck Lever 1179c7b2cae8SChuck Lever xprt_write_space(xprt); 1180c7b2cae8SChuck Lever } 1181c7b2cae8SChuck Lever 1182a246b010SChuck Lever out: 1183a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1184a246b010SChuck Lever } 1185a246b010SChuck Lever 1186470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1187a246b010SChuck Lever { 1188ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1189ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1190a246b010SChuck Lever 11917c6e066eSChuck Lever if (transport->rcvsize) { 1192a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 11937c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1194a246b010SChuck Lever } 11957c6e066eSChuck Lever if (transport->sndsize) { 1196a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 11977c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1198a246b010SChuck Lever sk->sk_write_space(sk); 1199a246b010SChuck Lever } 1200a246b010SChuck Lever } 1201a246b010SChuck Lever 120243118c29SChuck Lever /** 1203470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 120443118c29SChuck Lever * @xprt: generic transport 1205470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1206470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 120743118c29SChuck Lever * 1208470056c2SChuck Lever * Set socket send and receive buffer size limits. 120943118c29SChuck Lever */ 1210470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 121143118c29SChuck Lever { 12127c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 12137c6e066eSChuck Lever 12147c6e066eSChuck Lever transport->sndsize = 0; 1215470056c2SChuck Lever if (sndsize) 12167c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 12177c6e066eSChuck Lever transport->rcvsize = 0; 1218470056c2SChuck Lever if (rcvsize) 12197c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1220470056c2SChuck Lever 1221470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 122243118c29SChuck Lever } 122343118c29SChuck Lever 122446c0ee8bSChuck Lever /** 122546c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 122646c0ee8bSChuck Lever * @task: task that timed out 122746c0ee8bSChuck Lever * 122846c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 122946c0ee8bSChuck Lever */ 123046c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 123146c0ee8bSChuck Lever { 123246c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 123346c0ee8bSChuck Lever } 123446c0ee8bSChuck Lever 1235b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1236b85d8806SChuck Lever { 1237b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1238b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1239b85d8806SChuck Lever return rand + xprt_min_resvport; 1240b85d8806SChuck Lever } 1241b85d8806SChuck Lever 124292200412SChuck Lever /** 124392200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 124492200412SChuck Lever * @xprt: generic transport 124592200412SChuck Lever * @port: new port number 124692200412SChuck Lever * 124792200412SChuck Lever */ 124892200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 124992200412SChuck Lever { 125095392c59SChuck Lever struct sockaddr *addr = xs_addr(xprt); 1251c4efcb1dSChuck Lever 125292200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1253c4efcb1dSChuck Lever 125420612005SChuck Lever switch (addr->sa_family) { 125520612005SChuck Lever case AF_INET: 125620612005SChuck Lever ((struct sockaddr_in *)addr)->sin_port = htons(port); 125720612005SChuck Lever break; 125820612005SChuck Lever case AF_INET6: 125920612005SChuck Lever ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); 126020612005SChuck Lever break; 126120612005SChuck Lever default: 126220612005SChuck Lever BUG(); 126320612005SChuck Lever } 126492200412SChuck Lever } 126592200412SChuck Lever 12667dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock) 1267a246b010SChuck Lever { 1268a246b010SChuck Lever struct sockaddr_in myaddr = { 1269a246b010SChuck Lever .sin_family = AF_INET, 1270a246b010SChuck Lever }; 1271d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 1272529b33c6SChuck Lever int err; 1273c8475461SChuck Lever unsigned short port = transport->port; 1274a246b010SChuck Lever 1275d3bc9a1dSFrank van Maarseveen if (!transport->xprt.resvport) 1276d3bc9a1dSFrank van Maarseveen port = 0; 1277d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1278d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1279a246b010SChuck Lever do { 1280a246b010SChuck Lever myaddr.sin_port = htons(port); 1281e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1282a246b010SChuck Lever sizeof(myaddr)); 1283d3bc9a1dSFrank van Maarseveen if (!transport->xprt.resvport) 1284d3bc9a1dSFrank van Maarseveen break; 1285a246b010SChuck Lever if (err == 0) { 1286c8475461SChuck Lever transport->port = port; 1287d3bc9a1dSFrank van Maarseveen break; 1288a246b010SChuck Lever } 1289529b33c6SChuck Lever if (port <= xprt_min_resvport) 1290529b33c6SChuck Lever port = xprt_max_resvport; 1291529b33c6SChuck Lever else 1292529b33c6SChuck Lever port--; 1293c8475461SChuck Lever } while (err == -EADDRINUSE && port != transport->port); 12947dc753f0SChuck Lever dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n", 12957dc753f0SChuck Lever __FUNCTION__, NIPQUAD(myaddr.sin_addr), 12967dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1297a246b010SChuck Lever return err; 1298a246b010SChuck Lever } 1299a246b010SChuck Lever 130090058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock) 130190058d37SChuck Lever { 130290058d37SChuck Lever struct sockaddr_in6 myaddr = { 130390058d37SChuck Lever .sin6_family = AF_INET6, 130490058d37SChuck Lever }; 130590058d37SChuck Lever struct sockaddr_in6 *sa; 130690058d37SChuck Lever int err; 130790058d37SChuck Lever unsigned short port = transport->port; 130890058d37SChuck Lever 130990058d37SChuck Lever if (!transport->xprt.resvport) 131090058d37SChuck Lever port = 0; 131190058d37SChuck Lever sa = (struct sockaddr_in6 *)&transport->addr; 131290058d37SChuck Lever myaddr.sin6_addr = sa->sin6_addr; 131390058d37SChuck Lever do { 131490058d37SChuck Lever myaddr.sin6_port = htons(port); 131590058d37SChuck Lever err = kernel_bind(sock, (struct sockaddr *) &myaddr, 131690058d37SChuck Lever sizeof(myaddr)); 131790058d37SChuck Lever if (!transport->xprt.resvport) 131890058d37SChuck Lever break; 131990058d37SChuck Lever if (err == 0) { 132090058d37SChuck Lever transport->port = port; 132190058d37SChuck Lever break; 132290058d37SChuck Lever } 132390058d37SChuck Lever if (port <= xprt_min_resvport) 132490058d37SChuck Lever port = xprt_max_resvport; 132590058d37SChuck Lever else 132690058d37SChuck Lever port--; 132790058d37SChuck Lever } while (err == -EADDRINUSE && port != transport->port); 132890058d37SChuck Lever dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n", 132990058d37SChuck Lever NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err); 133090058d37SChuck Lever return err; 133190058d37SChuck Lever } 133290058d37SChuck Lever 1333ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1334ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1335ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1336ed07536eSPeter Zijlstra 13378945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1338ed07536eSPeter Zijlstra { 1339ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 13408945ee5eSChuck Lever 1341ed07536eSPeter Zijlstra BUG_ON(sk->sk_lock.owner != NULL); 13428945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 13438945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1344ed07536eSPeter Zijlstra } 13458945ee5eSChuck Lever 13468945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 13478945ee5eSChuck Lever { 13488945ee5eSChuck Lever struct sock *sk = sock->sk; 13498945ee5eSChuck Lever 13508945ee5eSChuck Lever BUG_ON(sk->sk_lock.owner != NULL); 13518945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 13528945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1353ed07536eSPeter Zijlstra } 1354ed07536eSPeter Zijlstra #else 13558945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 13568945ee5eSChuck Lever { 13578945ee5eSChuck Lever } 13588945ee5eSChuck Lever 13598945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1360ed07536eSPeter Zijlstra { 1361ed07536eSPeter Zijlstra } 1362ed07536eSPeter Zijlstra #endif 1363ed07536eSPeter Zijlstra 136416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 136516be2d20SChuck Lever { 136616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 136716be2d20SChuck Lever 136816be2d20SChuck Lever if (!transport->inet) { 136916be2d20SChuck Lever struct sock *sk = sock->sk; 137016be2d20SChuck Lever 137116be2d20SChuck Lever write_lock_bh(&sk->sk_callback_lock); 137216be2d20SChuck Lever 137316be2d20SChuck Lever sk->sk_user_data = xprt; 137416be2d20SChuck Lever transport->old_data_ready = sk->sk_data_ready; 137516be2d20SChuck Lever transport->old_state_change = sk->sk_state_change; 137616be2d20SChuck Lever transport->old_write_space = sk->sk_write_space; 137716be2d20SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 137816be2d20SChuck Lever sk->sk_write_space = xs_udp_write_space; 137916be2d20SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 138016be2d20SChuck Lever sk->sk_allocation = GFP_ATOMIC; 138116be2d20SChuck Lever 138216be2d20SChuck Lever xprt_set_connected(xprt); 138316be2d20SChuck Lever 138416be2d20SChuck Lever /* Reset to new socket */ 138516be2d20SChuck Lever transport->sock = sock; 138616be2d20SChuck Lever transport->inet = sk; 138716be2d20SChuck Lever 138816be2d20SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 138916be2d20SChuck Lever } 139016be2d20SChuck Lever xs_udp_do_set_buffer_size(xprt); 139116be2d20SChuck Lever } 139216be2d20SChuck Lever 13939903cd1cSChuck Lever /** 13949c3d72deSChuck Lever * xs_udp_connect_worker4 - set up a UDP socket 139565f27f38SDavid Howells * @work: RPC transport to connect 13969903cd1cSChuck Lever * 13979903cd1cSChuck Lever * Invoked by a work queue tasklet. 1398a246b010SChuck Lever */ 13999c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work) 1400a246b010SChuck Lever { 140134161db6STrond Myklebust struct sock_xprt *transport = 140234161db6STrond Myklebust container_of(work, struct sock_xprt, connect_worker.work); 1403c8475461SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1404ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1405b0d93ad5SChuck Lever int err, status = -EIO; 1406a246b010SChuck Lever 1407ec739ef0SChuck Lever if (xprt->shutdown || !xprt_bound(xprt)) 1408a246b010SChuck Lever goto out; 1409a246b010SChuck Lever 1410b0d93ad5SChuck Lever /* Start by resetting any existing state */ 14119903cd1cSChuck Lever xs_close(xprt); 1412b0d93ad5SChuck Lever 1413b0d93ad5SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { 1414b0d93ad5SChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1415a246b010SChuck Lever goto out; 1416a246b010SChuck Lever } 14178945ee5eSChuck Lever xs_reclassify_socket4(sock); 1418a246b010SChuck Lever 14197dc753f0SChuck Lever if (xs_bind4(transport, sock)) { 1420b0d93ad5SChuck Lever sock_release(sock); 1421b0d93ad5SChuck Lever goto out; 1422b0d93ad5SChuck Lever } 1423b0d93ad5SChuck Lever 1424edb267a6SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 14257559c7a2SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1426edb267a6SChuck Lever 142716be2d20SChuck Lever xs_udp_finish_connecting(xprt, sock); 1428a246b010SChuck Lever status = 0; 1429b0d93ad5SChuck Lever out: 1430b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 1431b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1432b0d93ad5SChuck Lever } 1433b0d93ad5SChuck Lever 143468e220bdSChuck Lever /** 143568e220bdSChuck Lever * xs_udp_connect_worker6 - set up a UDP socket 143668e220bdSChuck Lever * @work: RPC transport to connect 143768e220bdSChuck Lever * 143868e220bdSChuck Lever * Invoked by a work queue tasklet. 143968e220bdSChuck Lever */ 144068e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work) 144168e220bdSChuck Lever { 144268e220bdSChuck Lever struct sock_xprt *transport = 144368e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 144468e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 144568e220bdSChuck Lever struct socket *sock = transport->sock; 144668e220bdSChuck Lever int err, status = -EIO; 144768e220bdSChuck Lever 144868e220bdSChuck Lever if (xprt->shutdown || !xprt_bound(xprt)) 144968e220bdSChuck Lever goto out; 145068e220bdSChuck Lever 145168e220bdSChuck Lever /* Start by resetting any existing state */ 145268e220bdSChuck Lever xs_close(xprt); 145368e220bdSChuck Lever 145468e220bdSChuck Lever if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { 145568e220bdSChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 145668e220bdSChuck Lever goto out; 145768e220bdSChuck Lever } 14588945ee5eSChuck Lever xs_reclassify_socket6(sock); 145968e220bdSChuck Lever 146068e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 146168e220bdSChuck Lever sock_release(sock); 146268e220bdSChuck Lever goto out; 146368e220bdSChuck Lever } 146468e220bdSChuck Lever 146568e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 146668e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 146768e220bdSChuck Lever 146868e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 146968e220bdSChuck Lever status = 0; 147068e220bdSChuck Lever out: 147168e220bdSChuck Lever xprt_wake_pending_tasks(xprt, status); 147268e220bdSChuck Lever xprt_clear_connecting(xprt); 147368e220bdSChuck Lever } 147468e220bdSChuck Lever 14753167e12cSChuck Lever /* 14763167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 14773167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 14783167e12cSChuck Lever */ 14793167e12cSChuck Lever static void xs_tcp_reuse_connection(struct rpc_xprt *xprt) 14803167e12cSChuck Lever { 14813167e12cSChuck Lever int result; 1482ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 14833167e12cSChuck Lever struct sockaddr any; 14843167e12cSChuck Lever 14853167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 14863167e12cSChuck Lever 14873167e12cSChuck Lever /* 14883167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 14893167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 14903167e12cSChuck Lever */ 14913167e12cSChuck Lever memset(&any, 0, sizeof(any)); 14923167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1493ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 14943167e12cSChuck Lever if (result) 14953167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 14963167e12cSChuck Lever result); 14973167e12cSChuck Lever } 14983167e12cSChuck Lever 149916be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1500b0d93ad5SChuck Lever { 150116be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1502edb267a6SChuck Lever 1503ee0ac0c2SChuck Lever if (!transport->inet) { 1504b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1505b0d93ad5SChuck Lever 1506b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1507b0d93ad5SChuck Lever 1508b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1509314dfd79SChuck Lever transport->old_data_ready = sk->sk_data_ready; 1510314dfd79SChuck Lever transport->old_state_change = sk->sk_state_change; 1511314dfd79SChuck Lever transport->old_write_space = sk->sk_write_space; 1512b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1513b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1514b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1515b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 15163167e12cSChuck Lever 15173167e12cSChuck Lever /* socket options */ 15183167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 15193167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 15203167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 15213167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1522b0d93ad5SChuck Lever 1523b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1524b0d93ad5SChuck Lever 1525b0d93ad5SChuck Lever /* Reset to new socket */ 1526ee0ac0c2SChuck Lever transport->sock = sock; 1527ee0ac0c2SChuck Lever transport->inet = sk; 1528b0d93ad5SChuck Lever 1529b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1530b0d93ad5SChuck Lever } 1531b0d93ad5SChuck Lever 1532b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1533262ca07dSChuck Lever xprt->stat.connect_count++; 1534262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 153595392c59SChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 153616be2d20SChuck Lever } 153716be2d20SChuck Lever 153816be2d20SChuck Lever /** 15399c3d72deSChuck Lever * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint 154016be2d20SChuck Lever * @work: RPC transport to connect 154116be2d20SChuck Lever * 154216be2d20SChuck Lever * Invoked by a work queue tasklet. 154316be2d20SChuck Lever */ 15449c3d72deSChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work) 154516be2d20SChuck Lever { 154616be2d20SChuck Lever struct sock_xprt *transport = 154716be2d20SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 154816be2d20SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 154916be2d20SChuck Lever struct socket *sock = transport->sock; 155016be2d20SChuck Lever int err, status = -EIO; 155116be2d20SChuck Lever 155216be2d20SChuck Lever if (xprt->shutdown || !xprt_bound(xprt)) 155316be2d20SChuck Lever goto out; 155416be2d20SChuck Lever 155516be2d20SChuck Lever if (!sock) { 155616be2d20SChuck Lever /* start from scratch */ 155716be2d20SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 155816be2d20SChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 155916be2d20SChuck Lever goto out; 156016be2d20SChuck Lever } 15618945ee5eSChuck Lever xs_reclassify_socket4(sock); 156216be2d20SChuck Lever 156316be2d20SChuck Lever if (xs_bind4(transport, sock) < 0) { 156416be2d20SChuck Lever sock_release(sock); 156516be2d20SChuck Lever goto out; 156616be2d20SChuck Lever } 156716be2d20SChuck Lever } else 156816be2d20SChuck Lever /* "close" the socket, preserving the local port */ 156916be2d20SChuck Lever xs_tcp_reuse_connection(xprt); 157016be2d20SChuck Lever 157116be2d20SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 157216be2d20SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 157316be2d20SChuck Lever 157416be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 1575a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 157646121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 157746121cf7SChuck Lever sock->sk->sk_state); 1578a246b010SChuck Lever if (status < 0) { 1579a246b010SChuck Lever switch (status) { 1580a246b010SChuck Lever case -EINPROGRESS: 1581a246b010SChuck Lever case -EALREADY: 1582a246b010SChuck Lever goto out_clear; 15833167e12cSChuck Lever case -ECONNREFUSED: 15843167e12cSChuck Lever case -ECONNRESET: 15853167e12cSChuck Lever /* retry with existing socket, after a delay */ 15863167e12cSChuck Lever break; 15873167e12cSChuck Lever default: 15883167e12cSChuck Lever /* get rid of existing socket, and retry */ 15893167e12cSChuck Lever xs_close(xprt); 15903167e12cSChuck Lever break; 1591a246b010SChuck Lever } 1592a246b010SChuck Lever } 1593a246b010SChuck Lever out: 159444fbac22SChuck Lever xprt_wake_pending_tasks(xprt, status); 1595a246b010SChuck Lever out_clear: 15962226feb6SChuck Lever xprt_clear_connecting(xprt); 1597a246b010SChuck Lever } 1598a246b010SChuck Lever 15999903cd1cSChuck Lever /** 160068e220bdSChuck Lever * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint 160168e220bdSChuck Lever * @work: RPC transport to connect 160268e220bdSChuck Lever * 160368e220bdSChuck Lever * Invoked by a work queue tasklet. 160468e220bdSChuck Lever */ 160568e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work) 160668e220bdSChuck Lever { 160768e220bdSChuck Lever struct sock_xprt *transport = 160868e220bdSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 160968e220bdSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 161068e220bdSChuck Lever struct socket *sock = transport->sock; 161168e220bdSChuck Lever int err, status = -EIO; 161268e220bdSChuck Lever 161368e220bdSChuck Lever if (xprt->shutdown || !xprt_bound(xprt)) 161468e220bdSChuck Lever goto out; 161568e220bdSChuck Lever 161668e220bdSChuck Lever if (!sock) { 161768e220bdSChuck Lever /* start from scratch */ 161868e220bdSChuck Lever if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 161968e220bdSChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 162068e220bdSChuck Lever goto out; 162168e220bdSChuck Lever } 16228945ee5eSChuck Lever xs_reclassify_socket6(sock); 162368e220bdSChuck Lever 162468e220bdSChuck Lever if (xs_bind6(transport, sock) < 0) { 162568e220bdSChuck Lever sock_release(sock); 162668e220bdSChuck Lever goto out; 162768e220bdSChuck Lever } 162868e220bdSChuck Lever } else 162968e220bdSChuck Lever /* "close" the socket, preserving the local port */ 163068e220bdSChuck Lever xs_tcp_reuse_connection(xprt); 163168e220bdSChuck Lever 163268e220bdSChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 163368e220bdSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 163468e220bdSChuck Lever 163568e220bdSChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 163668e220bdSChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 163768e220bdSChuck Lever xprt, -status, xprt_connected(xprt), sock->sk->sk_state); 163868e220bdSChuck Lever if (status < 0) { 163968e220bdSChuck Lever switch (status) { 164068e220bdSChuck Lever case -EINPROGRESS: 164168e220bdSChuck Lever case -EALREADY: 164268e220bdSChuck Lever goto out_clear; 164368e220bdSChuck Lever case -ECONNREFUSED: 164468e220bdSChuck Lever case -ECONNRESET: 164568e220bdSChuck Lever /* retry with existing socket, after a delay */ 164668e220bdSChuck Lever break; 164768e220bdSChuck Lever default: 164868e220bdSChuck Lever /* get rid of existing socket, and retry */ 164968e220bdSChuck Lever xs_close(xprt); 165068e220bdSChuck Lever break; 165168e220bdSChuck Lever } 165268e220bdSChuck Lever } 165368e220bdSChuck Lever out: 165468e220bdSChuck Lever xprt_wake_pending_tasks(xprt, status); 165568e220bdSChuck Lever out_clear: 165668e220bdSChuck Lever xprt_clear_connecting(xprt); 165768e220bdSChuck Lever } 165868e220bdSChuck Lever 165968e220bdSChuck Lever /** 16609903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 16619903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 16629903cd1cSChuck Lever * 16639903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 166403bf4b70SChuck Lever * 166503bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 166603bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 166703bf4b70SChuck Lever * socket on a privileged port. 166803bf4b70SChuck Lever * 166903bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 167003bf4b70SChuck Lever * retry floods (hard mounts). 16719903cd1cSChuck Lever */ 16729903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 1673a246b010SChuck Lever { 1674a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1675ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1676a246b010SChuck Lever 1677b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1678b0d93ad5SChuck Lever return; 1679b0d93ad5SChuck Lever 1680ee0ac0c2SChuck Lever if (transport->sock != NULL) { 168146121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 168246121cf7SChuck Lever "seconds\n", 168303bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 1684c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1685c1384c9cSTrond Myklebust &transport->connect_worker, 168603bf4b70SChuck Lever xprt->reestablish_timeout); 168703bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 168803bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 168903bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 16909903cd1cSChuck Lever } else { 16919903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 1692c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1693c1384c9cSTrond Myklebust &transport->connect_worker, 0); 1694a246b010SChuck Lever } 1695a246b010SChuck Lever } 1696a246b010SChuck Lever 1697262ca07dSChuck Lever /** 1698262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 1699262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1700262ca07dSChuck Lever * @seq: output file 1701262ca07dSChuck Lever * 1702262ca07dSChuck Lever */ 1703262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1704262ca07dSChuck Lever { 1705c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1706c8475461SChuck Lever 1707262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 1708c8475461SChuck Lever transport->port, 1709262ca07dSChuck Lever xprt->stat.bind_count, 1710262ca07dSChuck Lever xprt->stat.sends, 1711262ca07dSChuck Lever xprt->stat.recvs, 1712262ca07dSChuck Lever xprt->stat.bad_xids, 1713262ca07dSChuck Lever xprt->stat.req_u, 1714262ca07dSChuck Lever xprt->stat.bklog_u); 1715262ca07dSChuck Lever } 1716262ca07dSChuck Lever 1717262ca07dSChuck Lever /** 1718262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 1719262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1720262ca07dSChuck Lever * @seq: output file 1721262ca07dSChuck Lever * 1722262ca07dSChuck Lever */ 1723262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1724262ca07dSChuck Lever { 1725c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1726262ca07dSChuck Lever long idle_time = 0; 1727262ca07dSChuck Lever 1728262ca07dSChuck Lever if (xprt_connected(xprt)) 1729262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 1730262ca07dSChuck Lever 1731262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 1732c8475461SChuck Lever transport->port, 1733262ca07dSChuck Lever xprt->stat.bind_count, 1734262ca07dSChuck Lever xprt->stat.connect_count, 1735262ca07dSChuck Lever xprt->stat.connect_time, 1736262ca07dSChuck Lever idle_time, 1737262ca07dSChuck Lever xprt->stat.sends, 1738262ca07dSChuck Lever xprt->stat.recvs, 1739262ca07dSChuck Lever xprt->stat.bad_xids, 1740262ca07dSChuck Lever xprt->stat.req_u, 1741262ca07dSChuck Lever xprt->stat.bklog_u); 1742262ca07dSChuck Lever } 1743262ca07dSChuck Lever 1744262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 174543118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 174612a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 174749e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 174845160d62SChuck Lever .rpcbind = rpcb_getport_async, 174992200412SChuck Lever .set_port = xs_set_port, 17509903cd1cSChuck Lever .connect = xs_connect, 175102107148SChuck Lever .buf_alloc = rpc_malloc, 175202107148SChuck Lever .buf_free = rpc_free, 1753262965f5SChuck Lever .send_request = xs_udp_send_request, 1754fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 175546c0ee8bSChuck Lever .timer = xs_udp_timer, 1756a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 1757262965f5SChuck Lever .close = xs_close, 1758262965f5SChuck Lever .destroy = xs_destroy, 1759262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 1760262965f5SChuck Lever }; 1761262965f5SChuck Lever 1762262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 176312a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 1764e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 176545160d62SChuck Lever .rpcbind = rpcb_getport_async, 176692200412SChuck Lever .set_port = xs_set_port, 1767262965f5SChuck Lever .connect = xs_connect, 176802107148SChuck Lever .buf_alloc = rpc_malloc, 176902107148SChuck Lever .buf_free = rpc_free, 1770262965f5SChuck Lever .send_request = xs_tcp_send_request, 1771fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 17729903cd1cSChuck Lever .close = xs_close, 17739903cd1cSChuck Lever .destroy = xs_destroy, 1774262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 1775a246b010SChuck Lever }; 1776a246b010SChuck Lever 177796802a09SFrank van Maarseveen static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args, unsigned int slot_table_size) 1778c8541ecdSChuck Lever { 1779c8541ecdSChuck Lever struct rpc_xprt *xprt; 1780ffc2e518SChuck Lever struct sock_xprt *new; 1781c8541ecdSChuck Lever 178296802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 1783c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 1784c8541ecdSChuck Lever return ERR_PTR(-EBADF); 1785c8541ecdSChuck Lever } 1786c8541ecdSChuck Lever 1787ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 1788ffc2e518SChuck Lever if (new == NULL) { 178946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 179046121cf7SChuck Lever "rpc_xprt\n"); 1791c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1792c8541ecdSChuck Lever } 1793ffc2e518SChuck Lever xprt = &new->xprt; 1794c8541ecdSChuck Lever 1795c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 1796c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 1797c8541ecdSChuck Lever if (xprt->slot == NULL) { 1798c8541ecdSChuck Lever kfree(xprt); 179946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 180046121cf7SChuck Lever "table\n"); 1801c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1802c8541ecdSChuck Lever } 1803c8541ecdSChuck Lever 180496802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 180596802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 1806d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 1807d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 1808c8475461SChuck Lever new->port = xs_get_random_port(); 1809c8541ecdSChuck Lever 1810c8541ecdSChuck Lever return xprt; 1811c8541ecdSChuck Lever } 1812c8541ecdSChuck Lever 18139903cd1cSChuck Lever /** 18149903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 181596802a09SFrank van Maarseveen * @args: rpc transport creation arguments 18169903cd1cSChuck Lever * 18179903cd1cSChuck Lever */ 181896802a09SFrank van Maarseveen struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args) 1819a246b010SChuck Lever { 18208f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 1821c8541ecdSChuck Lever struct rpc_xprt *xprt; 1822c8475461SChuck Lever struct sock_xprt *transport; 1823a246b010SChuck Lever 182496802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 1825c8541ecdSChuck Lever if (IS_ERR(xprt)) 1826c8541ecdSChuck Lever return xprt; 1827c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 1828a246b010SChuck Lever 1829ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 1830808012fbSChuck Lever xprt->tsh_size = 0; 1831a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 1832a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 1833a246b010SChuck Lever 183403bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 183503bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 183603bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 183703bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 1838a246b010SChuck Lever 1839262965f5SChuck Lever xprt->ops = &xs_udp_ops; 1840a246b010SChuck Lever 184196802a09SFrank van Maarseveen if (args->timeout) 184296802a09SFrank van Maarseveen xprt->timeout = *args->timeout; 1843a246b010SChuck Lever else 18449903cd1cSChuck Lever xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); 1845a246b010SChuck Lever 18468f9d5b1aSChuck Lever switch (addr->sa_family) { 18478f9d5b1aSChuck Lever case AF_INET: 18488f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 18498f9d5b1aSChuck Lever xprt_set_bound(xprt); 18508f9d5b1aSChuck Lever 18518f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 18528f9d5b1aSChuck Lever xs_udp_connect_worker4); 1853ba10f2c2SChuck Lever xs_format_ipv4_peer_addresses(xprt); 18548f9d5b1aSChuck Lever break; 18558f9d5b1aSChuck Lever case AF_INET6: 18568f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 18578f9d5b1aSChuck Lever xprt_set_bound(xprt); 18588f9d5b1aSChuck Lever 18598f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 18608f9d5b1aSChuck Lever xs_udp_connect_worker6); 18618f9d5b1aSChuck Lever xs_format_ipv6_peer_addresses(xprt); 18628f9d5b1aSChuck Lever break; 18638f9d5b1aSChuck Lever default: 18648f9d5b1aSChuck Lever kfree(xprt); 18658f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 18668f9d5b1aSChuck Lever } 18678f9d5b1aSChuck Lever 1868edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 18697559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 1870edb267a6SChuck Lever 1871c8541ecdSChuck Lever return xprt; 1872a246b010SChuck Lever } 1873a246b010SChuck Lever 18749903cd1cSChuck Lever /** 18759903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 187696802a09SFrank van Maarseveen * @args: rpc transport creation arguments 18779903cd1cSChuck Lever * 18789903cd1cSChuck Lever */ 187996802a09SFrank van Maarseveen struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args) 1880a246b010SChuck Lever { 18818f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 1882c8541ecdSChuck Lever struct rpc_xprt *xprt; 1883c8475461SChuck Lever struct sock_xprt *transport; 1884a246b010SChuck Lever 188596802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 1886c8541ecdSChuck Lever if (IS_ERR(xprt)) 1887c8541ecdSChuck Lever return xprt; 1888c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 1889a246b010SChuck Lever 1890ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 1891808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 1892808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 1893a246b010SChuck Lever 189403bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 189503bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 189603bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 189703bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 1898a246b010SChuck Lever 1899262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 1900a246b010SChuck Lever 190196802a09SFrank van Maarseveen if (args->timeout) 190296802a09SFrank van Maarseveen xprt->timeout = *args->timeout; 1903a246b010SChuck Lever else 19049903cd1cSChuck Lever xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); 1905a246b010SChuck Lever 19068f9d5b1aSChuck Lever switch (addr->sa_family) { 19078f9d5b1aSChuck Lever case AF_INET: 19088f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 19098f9d5b1aSChuck Lever xprt_set_bound(xprt); 19108f9d5b1aSChuck Lever 19118f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); 1912ba10f2c2SChuck Lever xs_format_ipv4_peer_addresses(xprt); 19138f9d5b1aSChuck Lever break; 19148f9d5b1aSChuck Lever case AF_INET6: 19158f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 19168f9d5b1aSChuck Lever xprt_set_bound(xprt); 19178f9d5b1aSChuck Lever 19188f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); 19198f9d5b1aSChuck Lever xs_format_ipv6_peer_addresses(xprt); 19208f9d5b1aSChuck Lever break; 19218f9d5b1aSChuck Lever default: 19228f9d5b1aSChuck Lever kfree(xprt); 19238f9d5b1aSChuck Lever return ERR_PTR(-EAFNOSUPPORT); 19248f9d5b1aSChuck Lever } 19258f9d5b1aSChuck Lever 1926edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 19277559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 1928edb267a6SChuck Lever 1929c8541ecdSChuck Lever return xprt; 1930a246b010SChuck Lever } 1931282b32e1SChuck Lever 1932282b32e1SChuck Lever /** 1933fbf76683SChuck Lever * init_socket_xprt - set up xprtsock's sysctls 1934282b32e1SChuck Lever * 1935282b32e1SChuck Lever */ 1936282b32e1SChuck Lever int init_socket_xprt(void) 1937282b32e1SChuck Lever { 1938fbf76683SChuck Lever #ifdef RPC_DEBUG 19392b1bec5fSEric W. Biederman if (!sunrpc_table_header) 19400b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 1941fbf76683SChuck Lever #endif 1942fbf76683SChuck Lever 1943282b32e1SChuck Lever return 0; 1944282b32e1SChuck Lever } 1945282b32e1SChuck Lever 1946282b32e1SChuck Lever /** 1947fbf76683SChuck Lever * cleanup_socket_xprt - remove xprtsock's sysctls 1948282b32e1SChuck Lever * 1949282b32e1SChuck Lever */ 1950282b32e1SChuck Lever void cleanup_socket_xprt(void) 1951282b32e1SChuck Lever { 1952fbf76683SChuck Lever #ifdef RPC_DEBUG 1953fbf76683SChuck Lever if (sunrpc_table_header) { 1954fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 1955fbf76683SChuck Lever sunrpc_table_header = NULL; 1956fbf76683SChuck Lever } 1957fbf76683SChuck Lever #endif 1958282b32e1SChuck Lever } 1959