1a246b010SChuck Lever /* 2a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 3a246b010SChuck Lever * 4a246b010SChuck Lever * Client-side transport implementation for sockets. 5a246b010SChuck Lever * 6113aa838SAlan Cox * TCP callback races fixes (C) 1998 Red Hat 7113aa838SAlan Cox * TCP send fixes (C) 1998 Red Hat 8a246b010SChuck Lever * TCP NFS related read + write fixes 9a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 10a246b010SChuck Lever * 11a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 12a246b010SChuck Lever * Fix behaviour when socket buffer is full. 13a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1455aa4f58SChuck Lever * 1555aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 168f9d5b1aSChuck Lever * 178f9d5b1aSChuck Lever * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 188f9d5b1aSChuck Lever * <gilles.quillard@bull.net> 19a246b010SChuck Lever */ 20a246b010SChuck Lever 21a246b010SChuck Lever #include <linux/types.h> 22176e21eeSChuck Lever #include <linux/string.h> 23a246b010SChuck Lever #include <linux/slab.h> 24bc25571eS\"Talpey, Thomas\ #include <linux/module.h> 25a246b010SChuck Lever #include <linux/capability.h> 26a246b010SChuck Lever #include <linux/pagemap.h> 27a246b010SChuck Lever #include <linux/errno.h> 28a246b010SChuck Lever #include <linux/socket.h> 29a246b010SChuck Lever #include <linux/in.h> 30a246b010SChuck Lever #include <linux/net.h> 31a246b010SChuck Lever #include <linux/mm.h> 32176e21eeSChuck Lever #include <linux/un.h> 33a246b010SChuck Lever #include <linux/udp.h> 34a246b010SChuck Lever #include <linux/tcp.h> 35a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 365976687aSJeff Layton #include <linux/sunrpc/addr.h> 3702107148SChuck Lever #include <linux/sunrpc/sched.h> 384cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h> 3949c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 40a246b010SChuck Lever #include <linux/file.h> 419e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 4244b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 4344b98efdSRicardo Labiaga #endif 44a246b010SChuck Lever 45a246b010SChuck Lever #include <net/sock.h> 46a246b010SChuck Lever #include <net/checksum.h> 47a246b010SChuck Lever #include <net/udp.h> 48a246b010SChuck Lever #include <net/tcp.h> 49a246b010SChuck Lever 5040b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 5140b5ea0cSTrond Myklebust 524cfc7e60SRahul Iyer #include "sunrpc.h" 53176e21eeSChuck Lever 54176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 55176e21eeSChuck Lever 569903cd1cSChuck Lever /* 57c556b754SChuck Lever * xprtsock tunables 58c556b754SChuck Lever */ 5909acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 6009acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 6109acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 62c556b754SChuck Lever 6309acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 6409acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 65c556b754SChuck Lever 6654c09874STrond Myklebust #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 6754c09874STrond Myklebust 687d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 6925fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 707d1e8255STrond Myklebust 71c556b754SChuck Lever /* 72fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 73fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 74fbf76683SChuck Lever * directory become the union of all files registered there. 75fbf76683SChuck Lever * 76fbf76683SChuck Lever * We simply need to make sure that we don't collide with 77fbf76683SChuck Lever * someone else's file names! 78fbf76683SChuck Lever */ 79fbf76683SChuck Lever 80fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 81fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 82d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 83fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 84fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 85fbf76683SChuck Lever 86fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 87fbf76683SChuck Lever 88fbf76683SChuck Lever /* 89fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 90fbf76683SChuck Lever * socket buffers for existing UDP transports 91fbf76683SChuck Lever */ 92fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 93fbf76683SChuck Lever { 94fbf76683SChuck Lever .procname = "udp_slot_table_entries", 95fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 96fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 97fbf76683SChuck Lever .mode = 0644, 986d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 99fbf76683SChuck Lever .extra1 = &min_slot_table_size, 100fbf76683SChuck Lever .extra2 = &max_slot_table_size 101fbf76683SChuck Lever }, 102fbf76683SChuck Lever { 103fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 104fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 105fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 106fbf76683SChuck Lever .mode = 0644, 1076d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 108fbf76683SChuck Lever .extra1 = &min_slot_table_size, 109fbf76683SChuck Lever .extra2 = &max_slot_table_size 110fbf76683SChuck Lever }, 111fbf76683SChuck Lever { 112d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 113d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 114d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 115d9ba131dSTrond Myklebust .mode = 0644, 116d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 117d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 118d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 119d9ba131dSTrond Myklebust }, 120d9ba131dSTrond Myklebust { 121fbf76683SChuck Lever .procname = "min_resvport", 122fbf76683SChuck Lever .data = &xprt_min_resvport, 123fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 124fbf76683SChuck Lever .mode = 0644, 1256d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 126fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 127fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 128fbf76683SChuck Lever }, 129fbf76683SChuck Lever { 130fbf76683SChuck Lever .procname = "max_resvport", 131fbf76683SChuck Lever .data = &xprt_max_resvport, 132fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 133fbf76683SChuck Lever .mode = 0644, 1346d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 135fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 136fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 137fbf76683SChuck Lever }, 138fbf76683SChuck Lever { 13925fe6142STrond Myklebust .procname = "tcp_fin_timeout", 14025fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 14125fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 14225fe6142STrond Myklebust .mode = 0644, 1436d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 14425fe6142STrond Myklebust }, 145f8572d8fSEric W. Biederman { }, 146fbf76683SChuck Lever }; 147fbf76683SChuck Lever 148fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = { 149fbf76683SChuck Lever { 150fbf76683SChuck Lever .procname = "sunrpc", 151fbf76683SChuck Lever .mode = 0555, 152fbf76683SChuck Lever .child = xs_tunables_table 153fbf76683SChuck Lever }, 154f8572d8fSEric W. Biederman { }, 155fbf76683SChuck Lever }; 156fbf76683SChuck Lever 157fbf76683SChuck Lever #endif 158fbf76683SChuck Lever 159fbf76683SChuck Lever /* 16003bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16103bf4b70SChuck Lever */ 16203bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16303bf4b70SChuck Lever 16403bf4b70SChuck Lever /* 16503bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16603bf4b70SChuck Lever * kind of resource problem on the local host. 16703bf4b70SChuck Lever */ 16803bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 16903bf4b70SChuck Lever 17003bf4b70SChuck Lever /* 17103bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17203bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17303bf4b70SChuck Lever * 17403bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17503bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17603bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17703bf4b70SChuck Lever * increase over time if the server is down or not responding. 17803bf4b70SChuck Lever */ 17903bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18003bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 18103bf4b70SChuck Lever 18203bf4b70SChuck Lever /* 18303bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18403bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18503bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18603bf4b70SChuck Lever */ 18703bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18803bf4b70SChuck Lever 189f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 190a246b010SChuck Lever # undef RPC_DEBUG_DATA 1919903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 192a246b010SChuck Lever #endif 193a246b010SChuck Lever 194a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1959903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 196a246b010SChuck Lever { 197a246b010SChuck Lever u8 *buf = (u8 *) packet; 198a246b010SChuck Lever int j; 199a246b010SChuck Lever 200a246b010SChuck Lever dprintk("RPC: %s\n", msg); 201a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 202a246b010SChuck Lever if (!(j & 31)) { 203a246b010SChuck Lever if (j) 204a246b010SChuck Lever dprintk("\n"); 205a246b010SChuck Lever dprintk("0x%04x ", j); 206a246b010SChuck Lever } 207a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 208a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 209a246b010SChuck Lever } 210a246b010SChuck Lever dprintk("\n"); 211a246b010SChuck Lever } 212a246b010SChuck Lever #else 2139903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 214a246b010SChuck Lever { 215a246b010SChuck Lever /* NOP */ 216a246b010SChuck Lever } 217a246b010SChuck Lever #endif 218a246b010SChuck Lever 2192118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2202118071dSTrond Myklebust { 2212118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2222118071dSTrond Myklebust } 2232118071dSTrond Myklebust 22495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 225edb267a6SChuck Lever { 22695392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 22795392c59SChuck Lever } 22895392c59SChuck Lever 229176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 230176e21eeSChuck Lever { 231176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 232176e21eeSChuck Lever } 233176e21eeSChuck Lever 23495392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 23595392c59SChuck Lever { 23695392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 23795392c59SChuck Lever } 23895392c59SChuck Lever 23995392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 24095392c59SChuck Lever { 24195392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 24295392c59SChuck Lever } 24395392c59SChuck Lever 244c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 245c877b849SChuck Lever { 246c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2479dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2489dc3b095SChuck Lever struct sockaddr_in *sin; 249176e21eeSChuck Lever struct sockaddr_un *sun; 250c877b849SChuck Lever char buf[128]; 251c877b849SChuck Lever 2529dc3b095SChuck Lever switch (sap->sa_family) { 253176e21eeSChuck Lever case AF_LOCAL: 254176e21eeSChuck Lever sun = xs_addr_un(xprt); 255176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 256176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 257176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 258176e21eeSChuck Lever break; 2599dc3b095SChuck Lever case AF_INET: 260176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 261176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 262176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2639dc3b095SChuck Lever sin = xs_addr_in(xprt); 264fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2659dc3b095SChuck Lever break; 2669dc3b095SChuck Lever case AF_INET6: 267176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 268176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 269176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2709dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 271fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2729dc3b095SChuck Lever break; 2739dc3b095SChuck Lever default: 2749dc3b095SChuck Lever BUG(); 2759dc3b095SChuck Lever } 276176e21eeSChuck Lever 2779dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2789dc3b095SChuck Lever } 2799dc3b095SChuck Lever 2809dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2819dc3b095SChuck Lever { 2829dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2839dc3b095SChuck Lever char buf[128]; 2849dc3b095SChuck Lever 28581160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 286c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 287c877b849SChuck Lever 28881160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 289c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 290c877b849SChuck Lever } 291c877b849SChuck Lever 2929dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 293b454ae90SChuck Lever const char *protocol, 294b454ae90SChuck Lever const char *netid) 295edb267a6SChuck Lever { 296b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 297b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 298c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 2999dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 300edb267a6SChuck Lever } 301edb267a6SChuck Lever 3029dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3034b6473fbSChuck Lever { 3049dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3059dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3064b6473fbSChuck Lever 3079dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 308edb267a6SChuck Lever } 309edb267a6SChuck Lever 310edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 311edb267a6SChuck Lever { 31233e01dc7SChuck Lever unsigned int i; 31333e01dc7SChuck Lever 31433e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 31533e01dc7SChuck Lever switch (i) { 31633e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 31733e01dc7SChuck Lever case RPC_DISPLAY_NETID: 31833e01dc7SChuck Lever continue; 31933e01dc7SChuck Lever default: 32033e01dc7SChuck Lever kfree(xprt->address_strings[i]); 32133e01dc7SChuck Lever } 322edb267a6SChuck Lever } 323edb267a6SChuck Lever 324b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 325b4b5cc85SChuck Lever 32624c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 327b4b5cc85SChuck Lever { 328b4b5cc85SChuck Lever struct msghdr msg = { 329b4b5cc85SChuck Lever .msg_name = addr, 330b4b5cc85SChuck Lever .msg_namelen = addrlen, 33124c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 33224c5684bSTrond Myklebust }; 33324c5684bSTrond Myklebust struct kvec iov = { 33424c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 33524c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 336b4b5cc85SChuck Lever }; 337b4b5cc85SChuck Lever 33824c5684bSTrond Myklebust if (iov.iov_len != 0) 339b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 340b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 341b4b5cc85SChuck Lever } 342b4b5cc85SChuck Lever 343f279cd00SJason Baron static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p) 344b4b5cc85SChuck Lever { 345a6b31d18STrond Myklebust ssize_t (*do_sendpage)(struct socket *sock, struct page *page, 346a6b31d18STrond Myklebust int offset, size_t size, int flags); 34724c5684bSTrond Myklebust struct page **ppage; 34824c5684bSTrond Myklebust unsigned int remainder; 349f279cd00SJason Baron int err; 350b4b5cc85SChuck Lever 35124c5684bSTrond Myklebust remainder = xdr->page_len - base; 35224c5684bSTrond Myklebust base += xdr->page_base; 35324c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 35424c5684bSTrond Myklebust base &= ~PAGE_MASK; 355a6b31d18STrond Myklebust do_sendpage = sock->ops->sendpage; 356a6b31d18STrond Myklebust if (!zerocopy) 357a6b31d18STrond Myklebust do_sendpage = sock_no_sendpage; 35824c5684bSTrond Myklebust for(;;) { 35924c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 36024c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 36124c5684bSTrond Myklebust 36224c5684bSTrond Myklebust remainder -= len; 36324c5684bSTrond Myklebust if (remainder != 0 || more) 36424c5684bSTrond Myklebust flags |= MSG_MORE; 365a6b31d18STrond Myklebust err = do_sendpage(sock, *ppage, base, len, flags); 36624c5684bSTrond Myklebust if (remainder == 0 || err != len) 36724c5684bSTrond Myklebust break; 368f279cd00SJason Baron *sent_p += err; 36924c5684bSTrond Myklebust ppage++; 37024c5684bSTrond Myklebust base = 0; 37124c5684bSTrond Myklebust } 372f279cd00SJason Baron if (err > 0) { 373f279cd00SJason Baron *sent_p += err; 374f279cd00SJason Baron err = 0; 375f279cd00SJason Baron } 37624c5684bSTrond Myklebust return err; 377b4b5cc85SChuck Lever } 378b4b5cc85SChuck Lever 3799903cd1cSChuck Lever /** 3809903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 3819903cd1cSChuck Lever * @sock: socket to send on 3829903cd1cSChuck Lever * @addr: UDP only -- address of destination 3839903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 3849903cd1cSChuck Lever * @xdr: buffer containing this request 3859903cd1cSChuck Lever * @base: starting position in the buffer 386a6b31d18STrond Myklebust * @zerocopy: true if it is safe to use sendpage() 387f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 3889903cd1cSChuck Lever * 389a246b010SChuck Lever */ 390f279cd00SJason Baron static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p) 391a246b010SChuck Lever { 39224c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 393f279cd00SJason Baron int err = 0; 394f279cd00SJason Baron int sent = 0; 395a246b010SChuck Lever 396262965f5SChuck Lever if (unlikely(!sock)) 397fba91afbSTrond Myklebust return -ENOTSOCK; 398262965f5SChuck Lever 399262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 40024c5684bSTrond Myklebust if (base != 0) { 40124c5684bSTrond Myklebust addr = NULL; 40224c5684bSTrond Myklebust addrlen = 0; 40324c5684bSTrond Myklebust } 404262965f5SChuck Lever 40524c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 40624c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 40724c5684bSTrond Myklebust remainder -= len; 40824c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 40924c5684bSTrond Myklebust if (remainder == 0 || err != len) 410a246b010SChuck Lever goto out; 411f279cd00SJason Baron *sent_p += err; 412a246b010SChuck Lever base = 0; 413a246b010SChuck Lever } else 41424c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 415a246b010SChuck Lever 41624c5684bSTrond Myklebust if (base < xdr->page_len) { 41724c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 41824c5684bSTrond Myklebust remainder -= len; 419f279cd00SJason Baron err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); 420f279cd00SJason Baron *sent_p += sent; 421f279cd00SJason Baron if (remainder == 0 || sent != len) 422a246b010SChuck Lever goto out; 423a246b010SChuck Lever base = 0; 42424c5684bSTrond Myklebust } else 42524c5684bSTrond Myklebust base -= xdr->page_len; 42624c5684bSTrond Myklebust 42724c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 428f279cd00SJason Baron return 0; 42924c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 430a246b010SChuck Lever out: 431f279cd00SJason Baron if (err > 0) { 432f279cd00SJason Baron *sent_p += err; 433f279cd00SJason Baron err = 0; 434f279cd00SJason Baron } 43524c5684bSTrond Myklebust return err; 436a246b010SChuck Lever } 437a246b010SChuck Lever 438b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 439b6ddf64fSTrond Myklebust { 440b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 441b6ddf64fSTrond Myklebust 442b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 443b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 444b6ddf64fSTrond Myklebust } 445b6ddf64fSTrond Myklebust 4469903cd1cSChuck Lever /** 447262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 448262965f5SChuck Lever * @task: task to put to sleep 4499903cd1cSChuck Lever * 450a246b010SChuck Lever */ 4515e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 452a246b010SChuck Lever { 453262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 454262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 455ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 45606ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 45724ca9a84STrond Myklebust int ret = -EAGAIN; 458a246b010SChuck Lever 45946121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 460262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 461262965f5SChuck Lever req->rq_slen); 462a246b010SChuck Lever 463262965f5SChuck Lever /* Protect against races with write_space */ 464262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 465a246b010SChuck Lever 466262965f5SChuck Lever /* Don't race with disconnect */ 467b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 468b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 469b6ddf64fSTrond Myklebust /* 470b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 471b6ddf64fSTrond Myklebust * window size 472b6ddf64fSTrond Myklebust */ 473b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 47406ea0bfeSTrond Myklebust sk->sk_write_pending++; 475b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 476b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 477b6ddf64fSTrond Myklebust } 478b6ddf64fSTrond Myklebust } else { 479b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 4805e3771ceSTrond Myklebust ret = -ENOTCONN; 481b6ddf64fSTrond Myklebust } 482a246b010SChuck Lever 483262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 48406ea0bfeSTrond Myklebust 48506ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 48606ea0bfeSTrond Myklebust sk->sk_write_space(sk); 4875e3771ceSTrond Myklebust return ret; 488a246b010SChuck Lever } 489a246b010SChuck Lever 49061677eeeSChuck Lever /* 49161677eeeSChuck Lever * Construct a stream transport record marker in @buf. 49261677eeeSChuck Lever */ 49361677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 49461677eeeSChuck Lever { 49561677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 49661677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 49761677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 49861677eeeSChuck Lever } 49961677eeeSChuck Lever 5009903cd1cSChuck Lever /** 501176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 502176e21eeSChuck Lever * @task: RPC task that manages the state of an RPC request 503176e21eeSChuck Lever * 504176e21eeSChuck Lever * Return values: 505176e21eeSChuck Lever * 0: The request has been sent 506176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 507176e21eeSChuck Lever * complete the request 508176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 509176e21eeSChuck Lever * other: Some other error occured, the request was not sent 510176e21eeSChuck Lever */ 511176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task) 512176e21eeSChuck Lever { 513176e21eeSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 514176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 515176e21eeSChuck Lever struct sock_xprt *transport = 516176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 517176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 518176e21eeSChuck Lever int status; 519f279cd00SJason Baron int sent = 0; 520176e21eeSChuck Lever 521176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 522176e21eeSChuck Lever 523176e21eeSChuck Lever xs_pktdump("packet data:", 524176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 525176e21eeSChuck Lever 526f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent, 527f279cd00SJason Baron true, &sent); 528176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 529176e21eeSChuck Lever __func__, xdr->len - req->rq_bytes_sent, status); 530743c69e7SNeilBrown 531743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 532743c69e7SNeilBrown status = -ENOBUFS; 533743c69e7SNeilBrown 534f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 535f279cd00SJason Baron req->rq_bytes_sent += sent; 536f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 537176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 538176e21eeSChuck Lever req->rq_bytes_sent = 0; 539176e21eeSChuck Lever return 0; 540176e21eeSChuck Lever } 541176e21eeSChuck Lever status = -EAGAIN; 542176e21eeSChuck Lever } 543176e21eeSChuck Lever 544176e21eeSChuck Lever switch (status) { 5453601c4a9STrond Myklebust case -ENOBUFS: 546b5872f0cSTrond Myklebust break; 547176e21eeSChuck Lever case -EAGAIN: 548176e21eeSChuck Lever status = xs_nospace(task); 549176e21eeSChuck Lever break; 550176e21eeSChuck Lever default: 551176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 552176e21eeSChuck Lever -status); 553176e21eeSChuck Lever case -EPIPE: 554176e21eeSChuck Lever xs_close(xprt); 555176e21eeSChuck Lever status = -ENOTCONN; 556176e21eeSChuck Lever } 557176e21eeSChuck Lever 558176e21eeSChuck Lever return status; 559176e21eeSChuck Lever } 560176e21eeSChuck Lever 561176e21eeSChuck Lever /** 562262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5639903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5649903cd1cSChuck Lever * 5659903cd1cSChuck Lever * Return values: 5669903cd1cSChuck Lever * 0: The request has been sent 5679903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5689903cd1cSChuck Lever * complete the request 569262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 57025985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 5719903cd1cSChuck Lever */ 572262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 573a246b010SChuck Lever { 574a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 575a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 576ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 577262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 578f279cd00SJason Baron int sent = 0; 579262965f5SChuck Lever int status; 580262965f5SChuck Lever 581262965f5SChuck Lever xs_pktdump("packet data:", 582262965f5SChuck Lever req->rq_svec->iov_base, 583262965f5SChuck Lever req->rq_svec->iov_len); 584262965f5SChuck Lever 58501d37c42STrond Myklebust if (!xprt_bound(xprt)) 58601d37c42STrond Myklebust return -ENOTCONN; 587f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 588f279cd00SJason Baron xdr, req->rq_bytes_sent, true, &sent); 589262965f5SChuck Lever 590262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 591262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 592262965f5SChuck Lever 5933dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 5943dedbb5cSJason Baron if (status == -EPERM) 5953dedbb5cSJason Baron goto process_status; 5963dedbb5cSJason Baron 597743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 598743c69e7SNeilBrown status = -ENOBUFS; 599743c69e7SNeilBrown 600f279cd00SJason Baron if (sent > 0 || status == 0) { 601f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 602f279cd00SJason Baron if (sent >= req->rq_slen) 603262965f5SChuck Lever return 0; 604262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 605262965f5SChuck Lever status = -EAGAIN; 6062199700fSTrond Myklebust } 607262965f5SChuck Lever 6083dedbb5cSJason Baron process_status: 609262965f5SChuck Lever switch (status) { 610fba91afbSTrond Myklebust case -ENOTSOCK: 611fba91afbSTrond Myklebust status = -ENOTCONN; 612fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 613fba91afbSTrond Myklebust break; 614b6ddf64fSTrond Myklebust case -EAGAIN: 6155e3771ceSTrond Myklebust status = xs_nospace(task); 616b6ddf64fSTrond Myklebust break; 617c8485e4dSTrond Myklebust default: 618c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 619c8485e4dSTrond Myklebust -status); 620262965f5SChuck Lever case -ENETUNREACH: 6213601c4a9STrond Myklebust case -ENOBUFS: 622262965f5SChuck Lever case -EPIPE: 623262965f5SChuck Lever case -ECONNREFUSED: 6243dedbb5cSJason Baron case -EPERM: 625262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 626262965f5SChuck Lever * prompts ECONNREFUSED. */ 627b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 628262965f5SChuck Lever } 6295fe46e9dSBian Naimeng 630262965f5SChuck Lever return status; 631262965f5SChuck Lever } 632262965f5SChuck Lever 633e06799f9STrond Myklebust /** 634262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 635262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 636262965f5SChuck Lever * 637262965f5SChuck Lever * Return values: 638262965f5SChuck Lever * 0: The request has been sent 639262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 640262965f5SChuck Lever * complete the request 641262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 64225985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 643262965f5SChuck Lever * 644262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 645262965f5SChuck Lever * if sendmsg is not able to make progress? 646262965f5SChuck Lever */ 647262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 648262965f5SChuck Lever { 649262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 650262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 651ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 652262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 653a6b31d18STrond Myklebust bool zerocopy = true; 654b595bb15SChuck Lever int status; 655f279cd00SJason Baron int sent; 656a246b010SChuck Lever 65761677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 658262965f5SChuck Lever 659262965f5SChuck Lever xs_pktdump("packet data:", 660262965f5SChuck Lever req->rq_svec->iov_base, 661262965f5SChuck Lever req->rq_svec->iov_len); 662a6b31d18STrond Myklebust /* Don't use zero copy if this is a resend. If the RPC call 663a6b31d18STrond Myklebust * completes while the socket holds a reference to the pages, 664a6b31d18STrond Myklebust * then we may end up resending corrupted data. 665a6b31d18STrond Myklebust */ 666a6b31d18STrond Myklebust if (task->tk_flags & RPC_TASK_SENT) 667a6b31d18STrond Myklebust zerocopy = false; 668a246b010SChuck Lever 669a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 670a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 671262965f5SChuck Lever * called sendmsg(). */ 672a246b010SChuck Lever while (1) { 673f279cd00SJason Baron sent = 0; 674f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 675f279cd00SJason Baron req->rq_bytes_sent, zerocopy, &sent); 676a246b010SChuck Lever 677262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 678262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 679262965f5SChuck Lever 680a246b010SChuck Lever /* If we've sent the entire packet, immediately 681a246b010SChuck Lever * reset the count of bytes sent. */ 682f279cd00SJason Baron req->rq_bytes_sent += sent; 683f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 684262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 685a246b010SChuck Lever req->rq_bytes_sent = 0; 686a246b010SChuck Lever return 0; 687a246b010SChuck Lever } 688262965f5SChuck Lever 689f580dd04STrond Myklebust if (status < 0) 690f580dd04STrond Myklebust break; 691f580dd04STrond Myklebust if (sent == 0) { 692a246b010SChuck Lever status = -EAGAIN; 693a246b010SChuck Lever break; 694a246b010SChuck Lever } 695f580dd04STrond Myklebust } 696743c69e7SNeilBrown if (status == -EAGAIN && sk_stream_is_writeable(transport->inet)) 697743c69e7SNeilBrown status = -ENOBUFS; 698a246b010SChuck Lever 699262965f5SChuck Lever switch (status) { 700fba91afbSTrond Myklebust case -ENOTSOCK: 701fba91afbSTrond Myklebust status = -ENOTCONN; 702fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 703fba91afbSTrond Myklebust break; 704262965f5SChuck Lever case -EAGAIN: 7055e3771ceSTrond Myklebust status = xs_nospace(task); 706262965f5SChuck Lever break; 707262965f5SChuck Lever default: 708262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 709262965f5SChuck Lever -status); 710a246b010SChuck Lever case -ECONNRESET: 711a246b010SChuck Lever case -ECONNREFUSED: 712a246b010SChuck Lever case -ENOTCONN: 7133913c78cSTrond Myklebust case -EADDRINUSE: 714b5872f0cSTrond Myklebust case -ENOBUFS: 715b9d2bb2eSTrond Myklebust case -EPIPE: 716a246b010SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 717a246b010SChuck Lever } 7185fe46e9dSBian Naimeng 719a246b010SChuck Lever return status; 720a246b010SChuck Lever } 721a246b010SChuck Lever 7229903cd1cSChuck Lever /** 723e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 724e0ab53deSTrond Myklebust * @xprt: transport 725e0ab53deSTrond Myklebust * @task: rpc task 726e0ab53deSTrond Myklebust * 727e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 728e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 729e0ab53deSTrond Myklebust * the server. 730e0ab53deSTrond Myklebust */ 731e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 732e0ab53deSTrond Myklebust { 733e0ab53deSTrond Myklebust struct rpc_rqst *req; 734e0ab53deSTrond Myklebust 735e0ab53deSTrond Myklebust if (task != xprt->snd_task) 736e0ab53deSTrond Myklebust return; 737e0ab53deSTrond Myklebust if (task == NULL) 738e0ab53deSTrond Myklebust goto out_release; 739e0ab53deSTrond Myklebust req = task->tk_rqstp; 74043cedbf0STrond Myklebust if (req == NULL) 74143cedbf0STrond Myklebust goto out_release; 742e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 743e0ab53deSTrond Myklebust goto out_release; 744e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 745e0ab53deSTrond Myklebust goto out_release; 746a4f0835cSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 747e0ab53deSTrond Myklebust out_release: 748e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 749e0ab53deSTrond Myklebust } 750e0ab53deSTrond Myklebust 7512a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7522a9e1cfaSTrond Myklebust { 7532a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7542a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7552a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7562118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7572a9e1cfaSTrond Myklebust } 7582a9e1cfaSTrond Myklebust 7592a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7602a9e1cfaSTrond Myklebust { 7612a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7622a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7632a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7642118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7652118071dSTrond Myklebust } 7662118071dSTrond Myklebust 767b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 768b70ae915STrond Myklebust { 769b70ae915STrond Myklebust smp_mb__before_atomic(); 770b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 771b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 772b70ae915STrond Myklebust smp_mb__after_atomic(); 773b70ae915STrond Myklebust } 774b70ae915STrond Myklebust 775b70ae915STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 776b70ae915STrond Myklebust { 777b70ae915STrond Myklebust xs_sock_reset_connection_flags(xprt); 778b70ae915STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 779b70ae915STrond Myklebust xprt_disconnect_done(xprt); 780b70ae915STrond Myklebust } 781b70ae915STrond Myklebust 7822118071dSTrond Myklebust /** 7832118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 7842118071dSTrond Myklebust * @sk: socket 7852118071dSTrond Myklebust * 7862118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 7872118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 7882118071dSTrond Myklebust */ 7892118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 7902118071dSTrond Myklebust { 7912118071dSTrond Myklebust struct rpc_xprt *xprt; 7922118071dSTrond Myklebust int err; 7932118071dSTrond Myklebust 7942118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 7952118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 7962118071dSTrond Myklebust goto out; 7972118071dSTrond Myklebust 7982118071dSTrond Myklebust err = -sk->sk_err; 7992118071dSTrond Myklebust if (err == 0) 8002118071dSTrond Myklebust goto out; 801b70ae915STrond Myklebust /* Is this a reset event? */ 802b70ae915STrond Myklebust if (sk->sk_state == TCP_CLOSE) 803b70ae915STrond Myklebust xs_sock_mark_closed(xprt); 8042118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 8052118071dSTrond Myklebust xprt, -err); 806e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 8072118071dSTrond Myklebust xprt_wake_pending_tasks(xprt, err); 8082118071dSTrond Myklebust out: 8092118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 8102a9e1cfaSTrond Myklebust } 8112a9e1cfaSTrond Myklebust 812fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 813a246b010SChuck Lever { 814ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 815ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 8166cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 817a246b010SChuck Lever 818fe315e76SChuck Lever if (sk == NULL) 819fe315e76SChuck Lever return; 8209903cd1cSChuck Lever 821264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 822264d1df3SJeff Layton sk_clear_memalloc(sk); 823264d1df3SJeff Layton 82409939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 82509939204STrond Myklebust 826a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 827ee0ac0c2SChuck Lever transport->inet = NULL; 828ee0ac0c2SChuck Lever transport->sock = NULL; 829a246b010SChuck Lever 830a246b010SChuck Lever sk->sk_user_data = NULL; 8312a9e1cfaSTrond Myklebust 8322a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 8330c78789eSTrond Myklebust xprt_clear_connected(xprt); 834a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 8356cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 836a246b010SChuck Lever 8376cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 838a246b010SChuck Lever sock_release(sock); 839fe315e76SChuck Lever } 840fe315e76SChuck Lever 841fe315e76SChuck Lever /** 842fe315e76SChuck Lever * xs_close - close a socket 843fe315e76SChuck Lever * @xprt: transport 844fe315e76SChuck Lever * 845fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 846fe315e76SChuck Lever * on the server we want to save. 847f75e6745STrond Myklebust * 848f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 849f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 850fe315e76SChuck Lever */ 851fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 852fe315e76SChuck Lever { 853fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 854fe315e76SChuck Lever 855fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 856fe315e76SChuck Lever 857fe315e76SChuck Lever xs_reset_transport(transport); 85861d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 859fe315e76SChuck Lever 86062da3b24STrond Myklebust xprt_disconnect_done(xprt); 861a246b010SChuck Lever } 862a246b010SChuck Lever 8634a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 8644a068258SChuck Lever { 8654a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 8664a068258SChuck Lever xprt); 8674a068258SChuck Lever xprt_disconnect_done(xprt); 8684a068258SChuck Lever } 8694a068258SChuck Lever 870315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 871315f3812SKinglong Mee { 872315f3812SKinglong Mee xs_free_peer_addresses(xprt); 873315f3812SKinglong Mee xprt_free(xprt); 874315f3812SKinglong Mee } 875315f3812SKinglong Mee 8769903cd1cSChuck Lever /** 8779903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8789903cd1cSChuck Lever * @xprt: doomed transport 8799903cd1cSChuck Lever * 8809903cd1cSChuck Lever */ 8819903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 882a246b010SChuck Lever { 88303c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 88403c78827STrond Myklebust struct sock_xprt, xprt); 8859903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8869903cd1cSChuck Lever 88703c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 888a1311d87STrond Myklebust xs_close(xprt); 889315f3812SKinglong Mee xs_xprt_free(xprt); 890a1311d87STrond Myklebust module_put(THIS_MODULE); 891a246b010SChuck Lever } 892a246b010SChuck Lever 893176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) 894176e21eeSChuck Lever { 895176e21eeSChuck Lever struct xdr_skb_reader desc = { 896176e21eeSChuck Lever .skb = skb, 897176e21eeSChuck Lever .offset = sizeof(rpc_fraghdr), 898176e21eeSChuck Lever .count = skb->len - sizeof(rpc_fraghdr), 899176e21eeSChuck Lever }; 900176e21eeSChuck Lever 901176e21eeSChuck Lever if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) 902176e21eeSChuck Lever return -1; 903176e21eeSChuck Lever if (desc.count) 904176e21eeSChuck Lever return -1; 905176e21eeSChuck Lever return 0; 906176e21eeSChuck Lever } 907176e21eeSChuck Lever 908176e21eeSChuck Lever /** 909176e21eeSChuck Lever * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets 910176e21eeSChuck Lever * @sk: socket with data to read 911176e21eeSChuck Lever * 912176e21eeSChuck Lever * Currently this assumes we can read the whole reply in a single gulp. 913176e21eeSChuck Lever */ 914676d2369SDavid S. Miller static void xs_local_data_ready(struct sock *sk) 915176e21eeSChuck Lever { 916176e21eeSChuck Lever struct rpc_task *task; 917176e21eeSChuck Lever struct rpc_xprt *xprt; 918176e21eeSChuck Lever struct rpc_rqst *rovr; 919176e21eeSChuck Lever struct sk_buff *skb; 920176e21eeSChuck Lever int err, repsize, copied; 921176e21eeSChuck Lever u32 _xid; 922176e21eeSChuck Lever __be32 *xp; 923176e21eeSChuck Lever 924176e21eeSChuck Lever read_lock_bh(&sk->sk_callback_lock); 925176e21eeSChuck Lever dprintk("RPC: %s...\n", __func__); 926176e21eeSChuck Lever xprt = xprt_from_sock(sk); 927176e21eeSChuck Lever if (xprt == NULL) 928176e21eeSChuck Lever goto out; 929176e21eeSChuck Lever 930176e21eeSChuck Lever skb = skb_recv_datagram(sk, 0, 1, &err); 931176e21eeSChuck Lever if (skb == NULL) 932176e21eeSChuck Lever goto out; 933176e21eeSChuck Lever 934176e21eeSChuck Lever repsize = skb->len - sizeof(rpc_fraghdr); 935176e21eeSChuck Lever if (repsize < 4) { 936176e21eeSChuck Lever dprintk("RPC: impossible RPC reply size %d\n", repsize); 937176e21eeSChuck Lever goto dropit; 938176e21eeSChuck Lever } 939176e21eeSChuck Lever 940176e21eeSChuck Lever /* Copy the XID from the skb... */ 941176e21eeSChuck Lever xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); 942176e21eeSChuck Lever if (xp == NULL) 943176e21eeSChuck Lever goto dropit; 944176e21eeSChuck Lever 945176e21eeSChuck Lever /* Look up and lock the request corresponding to the given XID */ 946176e21eeSChuck Lever spin_lock(&xprt->transport_lock); 947176e21eeSChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 948176e21eeSChuck Lever if (!rovr) 949176e21eeSChuck Lever goto out_unlock; 950176e21eeSChuck Lever task = rovr->rq_task; 951176e21eeSChuck Lever 952176e21eeSChuck Lever copied = rovr->rq_private_buf.buflen; 953176e21eeSChuck Lever if (copied > repsize) 954176e21eeSChuck Lever copied = repsize; 955176e21eeSChuck Lever 956176e21eeSChuck Lever if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { 957176e21eeSChuck Lever dprintk("RPC: sk_buff copy failed\n"); 958176e21eeSChuck Lever goto out_unlock; 959176e21eeSChuck Lever } 960176e21eeSChuck Lever 961176e21eeSChuck Lever xprt_complete_rqst(task, copied); 962176e21eeSChuck Lever 963176e21eeSChuck Lever out_unlock: 964176e21eeSChuck Lever spin_unlock(&xprt->transport_lock); 965176e21eeSChuck Lever dropit: 966176e21eeSChuck Lever skb_free_datagram(sk, skb); 967176e21eeSChuck Lever out: 968176e21eeSChuck Lever read_unlock_bh(&sk->sk_callback_lock); 969176e21eeSChuck Lever } 970176e21eeSChuck Lever 9719903cd1cSChuck Lever /** 9729903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 9739903cd1cSChuck Lever * @sk: socket with data to read 9749903cd1cSChuck Lever * 975a246b010SChuck Lever */ 976676d2369SDavid S. Miller static void xs_udp_data_ready(struct sock *sk) 977a246b010SChuck Lever { 978a246b010SChuck Lever struct rpc_task *task; 979a246b010SChuck Lever struct rpc_xprt *xprt; 980a246b010SChuck Lever struct rpc_rqst *rovr; 981a246b010SChuck Lever struct sk_buff *skb; 982a246b010SChuck Lever int err, repsize, copied; 983d8ed029dSAlexey Dobriyan u32 _xid; 984d8ed029dSAlexey Dobriyan __be32 *xp; 985a246b010SChuck Lever 986f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 9879903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 9889903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 989a246b010SChuck Lever goto out; 990a246b010SChuck Lever 991a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 992a246b010SChuck Lever goto out; 993a246b010SChuck Lever 994a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 995a246b010SChuck Lever if (repsize < 4) { 9969903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 997a246b010SChuck Lever goto dropit; 998a246b010SChuck Lever } 999a246b010SChuck Lever 1000a246b010SChuck Lever /* Copy the XID from the skb... */ 1001a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 1002a246b010SChuck Lever sizeof(_xid), &_xid); 1003a246b010SChuck Lever if (xp == NULL) 1004a246b010SChuck Lever goto dropit; 1005a246b010SChuck Lever 1006a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 10074a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 1008a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1009a246b010SChuck Lever if (!rovr) 1010a246b010SChuck Lever goto out_unlock; 1011a246b010SChuck Lever task = rovr->rq_task; 1012a246b010SChuck Lever 1013a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1014a246b010SChuck Lever copied = repsize; 1015a246b010SChuck Lever 1016a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 10171781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 10181781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 1019a246b010SChuck Lever goto out_unlock; 10201781f7f5SHerbert Xu } 10211781f7f5SHerbert Xu 10221781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 1023a246b010SChuck Lever 10246a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 10251570c1e4SChuck Lever xprt_complete_rqst(task, copied); 1026a246b010SChuck Lever 1027a246b010SChuck Lever out_unlock: 10284a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 1029a246b010SChuck Lever dropit: 1030a246b010SChuck Lever skb_free_datagram(sk, skb); 1031a246b010SChuck Lever out: 1032f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1033a246b010SChuck Lever } 1034a246b010SChuck Lever 1035a519fc7aSTrond Myklebust /* 1036a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1037a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1038a519fc7aSTrond Myklebust */ 1039a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1040a519fc7aSTrond Myklebust { 1041a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1042a519fc7aSTrond Myklebust } 1043a519fc7aSTrond Myklebust 1044dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1045a246b010SChuck Lever { 104651971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1047a246b010SChuck Lever size_t len, used; 1048a246b010SChuck Lever char *p; 1049a246b010SChuck Lever 105051971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 105151971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 10529d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 105351971139SChuck Lever transport->tcp_offset += used; 1054a246b010SChuck Lever if (used != len) 1055a246b010SChuck Lever return; 1056808012fbSChuck Lever 105751971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 105851971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 1059e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 1060a246b010SChuck Lever else 1061e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 106251971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 1063808012fbSChuck Lever 1064e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 106551971139SChuck Lever transport->tcp_offset = 0; 1066808012fbSChuck Lever 1067a246b010SChuck Lever /* Sanity check of the record length */ 106818dca02aSRicardo Labiaga if (unlikely(transport->tcp_reclen < 8)) { 10699903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 1070a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 10719903cd1cSChuck Lever return; 1072a246b010SChuck Lever } 1073a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 107451971139SChuck Lever transport->tcp_reclen); 1075a246b010SChuck Lever } 1076a246b010SChuck Lever 107751971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 1078a246b010SChuck Lever { 107951971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 1080e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 108151971139SChuck Lever transport->tcp_offset = 0; 1082e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 1083e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1084e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 108551971139SChuck Lever transport->tcp_copied = 0; 1086a246b010SChuck Lever } 1087a246b010SChuck Lever } 1088a246b010SChuck Lever } 1089a246b010SChuck Lever 1090dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1091a246b010SChuck Lever { 1092a246b010SChuck Lever size_t len, used; 1093a246b010SChuck Lever char *p; 1094a246b010SChuck Lever 109551971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 1096a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 109751971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 10989d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 109951971139SChuck Lever transport->tcp_offset += used; 1100a246b010SChuck Lever if (used != len) 1101a246b010SChuck Lever return; 1102e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 1103f4a2e418SRicardo Labiaga transport->tcp_flags |= TCP_RCV_READ_CALLDIR; 110451971139SChuck Lever transport->tcp_copied = 4; 110518dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 110618dca02aSRicardo Labiaga (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" 110718dca02aSRicardo Labiaga : "request with", 110851971139SChuck Lever ntohl(transport->tcp_xid)); 110951971139SChuck Lever xs_tcp_check_fraghdr(transport); 1110a246b010SChuck Lever } 1111a246b010SChuck Lever 111218dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 111318dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1114a246b010SChuck Lever { 111518dca02aSRicardo Labiaga size_t len, used; 111618dca02aSRicardo Labiaga u32 offset; 1117b76ce561STrond Myklebust char *p; 111818dca02aSRicardo Labiaga 111918dca02aSRicardo Labiaga /* 112018dca02aSRicardo Labiaga * We want transport->tcp_offset to be 8 at the end of this routine 112118dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 112218dca02aSRicardo Labiaga * When this function is called for the first time, 112318dca02aSRicardo Labiaga * transport->tcp_offset is 4 (after having already read the xid). 112418dca02aSRicardo Labiaga */ 112518dca02aSRicardo Labiaga offset = transport->tcp_offset - sizeof(transport->tcp_xid); 1126b76ce561STrond Myklebust len = sizeof(transport->tcp_calldir) - offset; 112718dca02aSRicardo Labiaga dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); 1128b76ce561STrond Myklebust p = ((char *) &transport->tcp_calldir) + offset; 1129b76ce561STrond Myklebust used = xdr_skb_read_bits(desc, p, len); 113018dca02aSRicardo Labiaga transport->tcp_offset += used; 113118dca02aSRicardo Labiaga if (used != len) 113218dca02aSRicardo Labiaga return; 1133f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; 1134f4a2e418SRicardo Labiaga /* 1135f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1136f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1137f4a2e418SRicardo Labiaga */ 1138b76ce561STrond Myklebust switch (ntohl(transport->tcp_calldir)) { 1139b76ce561STrond Myklebust case RPC_REPLY: 1140b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1141b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 114218dca02aSRicardo Labiaga transport->tcp_flags |= TCP_RPC_REPLY; 1143b76ce561STrond Myklebust break; 1144b76ce561STrond Myklebust case RPC_CALL: 1145b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1146b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 114718dca02aSRicardo Labiaga transport->tcp_flags &= ~TCP_RPC_REPLY; 1148b76ce561STrond Myklebust break; 1149b76ce561STrond Myklebust default: 1150b76ce561STrond Myklebust dprintk("RPC: invalid request message type\n"); 1151a519fc7aSTrond Myklebust xs_tcp_force_close(&transport->xprt); 1152b76ce561STrond Myklebust } 115318dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 115418dca02aSRicardo Labiaga } 115518dca02aSRicardo Labiaga 115644b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 115744b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 115844b98efdSRicardo Labiaga struct rpc_rqst *req) 1159a246b010SChuck Lever { 116044b98efdSRicardo Labiaga struct sock_xprt *transport = 116144b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1162a246b010SChuck Lever struct xdr_buf *rcvbuf; 1163a246b010SChuck Lever size_t len; 1164a246b010SChuck Lever ssize_t r; 1165a246b010SChuck Lever 1166a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1167f4a2e418SRicardo Labiaga 1168f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { 1169f4a2e418SRicardo Labiaga /* 1170f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1171f4a2e418SRicardo Labiaga */ 1172f4a2e418SRicardo Labiaga memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, 1173b76ce561STrond Myklebust &transport->tcp_calldir, 1174b76ce561STrond Myklebust sizeof(transport->tcp_calldir)); 1175b76ce561STrond Myklebust transport->tcp_copied += sizeof(transport->tcp_calldir); 1176f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; 1177a246b010SChuck Lever } 1178a246b010SChuck Lever 1179a246b010SChuck Lever len = desc->count; 118051971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1181dd456471SChuck Lever struct xdr_skb_reader my_desc; 1182a246b010SChuck Lever 118351971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1184a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1185a246b010SChuck Lever my_desc.count = len; 118651971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 11879d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1188a246b010SChuck Lever desc->count -= r; 1189a246b010SChuck Lever desc->offset += r; 1190a246b010SChuck Lever } else 119151971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 11929d292316SChuck Lever desc, xdr_skb_read_bits); 1193a246b010SChuck Lever 1194a246b010SChuck Lever if (r > 0) { 119551971139SChuck Lever transport->tcp_copied += r; 119651971139SChuck Lever transport->tcp_offset += r; 1197a246b010SChuck Lever } 1198a246b010SChuck Lever if (r != len) { 1199a246b010SChuck Lever /* Error when copying to the receive buffer, 1200a246b010SChuck Lever * usually because we weren't able to allocate 1201a246b010SChuck Lever * additional buffer pages. All we can do now 1202e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1203a246b010SChuck Lever * will not receive any additional updates, 1204a246b010SChuck Lever * and time out. 1205a246b010SChuck Lever * Any remaining data from this record will 1206a246b010SChuck Lever * be discarded. 1207a246b010SChuck Lever */ 1208e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1209a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 121051971139SChuck Lever ntohl(transport->tcp_xid)); 121146121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 121246121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 121346121cf7SChuck Lever xprt, transport->tcp_copied, 121446121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 121544b98efdSRicardo Labiaga return; 1216a246b010SChuck Lever } 1217a246b010SChuck Lever 1218a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 121951971139SChuck Lever ntohl(transport->tcp_xid), r); 122046121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 122146121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 122246121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1223a246b010SChuck Lever 122451971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1225e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 122651971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1227e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1228e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1229a246b010SChuck Lever } 123044b98efdSRicardo Labiaga } 123144b98efdSRicardo Labiaga 123244b98efdSRicardo Labiaga /* 123344b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 123444b98efdSRicardo Labiaga * tcp read code to read the data. 123544b98efdSRicardo Labiaga */ 123644b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 123744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 123844b98efdSRicardo Labiaga { 123944b98efdSRicardo Labiaga struct sock_xprt *transport = 124044b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 124144b98efdSRicardo Labiaga struct rpc_rqst *req; 124244b98efdSRicardo Labiaga 124344b98efdSRicardo Labiaga dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); 124444b98efdSRicardo Labiaga 124544b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 124644b98efdSRicardo Labiaga spin_lock(&xprt->transport_lock); 124744b98efdSRicardo Labiaga req = xprt_lookup_rqst(xprt, transport->tcp_xid); 124844b98efdSRicardo Labiaga if (!req) { 124944b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 125044b98efdSRicardo Labiaga ntohl(transport->tcp_xid)); 125144b98efdSRicardo Labiaga spin_unlock(&xprt->transport_lock); 125244b98efdSRicardo Labiaga return -1; 125344b98efdSRicardo Labiaga } 125444b98efdSRicardo Labiaga 125544b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 125644b98efdSRicardo Labiaga 1257e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 125851971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 125944b98efdSRicardo Labiaga 12604a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 126144b98efdSRicardo Labiaga return 0; 126244b98efdSRicardo Labiaga } 126344b98efdSRicardo Labiaga 12649e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 126544b98efdSRicardo Labiaga /* 126644b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 126744b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 126844b98efdSRicardo Labiaga * queue. 126944b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 127044b98efdSRicardo Labiaga * connection and return -1. 127144b98efdSRicardo Labiaga */ 12722ea24497STrond Myklebust static int xs_tcp_read_callback(struct rpc_xprt *xprt, 127344b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 127444b98efdSRicardo Labiaga { 127544b98efdSRicardo Labiaga struct sock_xprt *transport = 127644b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 127744b98efdSRicardo Labiaga struct rpc_rqst *req; 127844b98efdSRicardo Labiaga 12792ea24497STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 12802ea24497STrond Myklebust spin_lock(&xprt->transport_lock); 12812ea24497STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->tcp_xid); 128244b98efdSRicardo Labiaga if (req == NULL) { 12832ea24497STrond Myklebust spin_unlock(&xprt->transport_lock); 128444b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 128544b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 128644b98efdSRicardo Labiaga return -1; 128744b98efdSRicardo Labiaga } 128844b98efdSRicardo Labiaga 128944b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 129044b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 129144b98efdSRicardo Labiaga 12922ea24497STrond Myklebust if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 12932ea24497STrond Myklebust xprt_complete_bc_request(req, transport->tcp_copied); 12942ea24497STrond Myklebust spin_unlock(&xprt->transport_lock); 129544b98efdSRicardo Labiaga 129644b98efdSRicardo Labiaga return 0; 129744b98efdSRicardo Labiaga } 129844b98efdSRicardo Labiaga 129944b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 130044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 130144b98efdSRicardo Labiaga { 130244b98efdSRicardo Labiaga struct sock_xprt *transport = 130344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 130444b98efdSRicardo Labiaga 130544b98efdSRicardo Labiaga return (transport->tcp_flags & TCP_RPC_REPLY) ? 130644b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 130744b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 130844b98efdSRicardo Labiaga } 130944b98efdSRicardo Labiaga #else 131044b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 131144b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 131244b98efdSRicardo Labiaga { 131344b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 131444b98efdSRicardo Labiaga } 13159e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 131644b98efdSRicardo Labiaga 131744b98efdSRicardo Labiaga /* 131844b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 131944b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 132044b98efdSRicardo Labiaga */ 132144b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 132244b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 132344b98efdSRicardo Labiaga { 132444b98efdSRicardo Labiaga struct sock_xprt *transport = 132544b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 132644b98efdSRicardo Labiaga 132744b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 132851971139SChuck Lever xs_tcp_check_fraghdr(transport); 132944b98efdSRicardo Labiaga else { 133044b98efdSRicardo Labiaga /* 133144b98efdSRicardo Labiaga * The transport_lock protects the request handling. 133244b98efdSRicardo Labiaga * There's no need to hold it to update the tcp_flags. 133344b98efdSRicardo Labiaga */ 133444b98efdSRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 133544b98efdSRicardo Labiaga } 1336a246b010SChuck Lever } 1337a246b010SChuck Lever 1338dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1339a246b010SChuck Lever { 1340a246b010SChuck Lever size_t len; 1341a246b010SChuck Lever 134251971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1343a246b010SChuck Lever if (len > desc->count) 1344a246b010SChuck Lever len = desc->count; 1345a246b010SChuck Lever desc->count -= len; 1346a246b010SChuck Lever desc->offset += len; 134751971139SChuck Lever transport->tcp_offset += len; 1348a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 134951971139SChuck Lever xs_tcp_check_fraghdr(transport); 1350a246b010SChuck Lever } 1351a246b010SChuck Lever 13529903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1353a246b010SChuck Lever { 1354a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 135551971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1356dd456471SChuck Lever struct xdr_skb_reader desc = { 1357a246b010SChuck Lever .skb = skb, 1358a246b010SChuck Lever .offset = offset, 1359a246b010SChuck Lever .count = len, 1360a246b010SChuck Lever }; 1361a246b010SChuck Lever 13629903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1363a246b010SChuck Lever do { 13641a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 1365a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1366a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1367e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 13689903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1369a246b010SChuck Lever continue; 1370a246b010SChuck Lever } 1371a246b010SChuck Lever /* Read in the xid if necessary */ 1372e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 137351971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1374a246b010SChuck Lever continue; 1375a246b010SChuck Lever } 137618dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1377f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { 137818dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 137918dca02aSRicardo Labiaga continue; 138018dca02aSRicardo Labiaga } 1381a246b010SChuck Lever /* Read in the request data */ 1382e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 138344b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1384a246b010SChuck Lever continue; 1385a246b010SChuck Lever } 1386a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 138751971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1388a246b010SChuck Lever } while (desc.count); 13891a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 13909903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1391a246b010SChuck Lever return len - desc.count; 1392a246b010SChuck Lever } 1393a246b010SChuck Lever 1394*66d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport) 1395*66d7a56aSTrond Myklebust { 1396*66d7a56aSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1397*66d7a56aSTrond Myklebust struct sock *sk; 1398*66d7a56aSTrond Myklebust read_descriptor_t rd_desc = { 1399*66d7a56aSTrond Myklebust .count = 2*1024*1024, 1400*66d7a56aSTrond Myklebust .arg.data = xprt, 1401*66d7a56aSTrond Myklebust }; 1402*66d7a56aSTrond Myklebust unsigned long total = 0; 1403*66d7a56aSTrond Myklebust int read = 0; 1404*66d7a56aSTrond Myklebust 1405*66d7a56aSTrond Myklebust sk = transport->inet; 1406*66d7a56aSTrond Myklebust 1407*66d7a56aSTrond Myklebust /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1408*66d7a56aSTrond Myklebust for (;;) { 1409*66d7a56aSTrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1410*66d7a56aSTrond Myklebust if (read <= 0) 1411*66d7a56aSTrond Myklebust break; 1412*66d7a56aSTrond Myklebust total += read; 1413*66d7a56aSTrond Myklebust rd_desc.count = 65536; 1414*66d7a56aSTrond Myklebust } 1415*66d7a56aSTrond Myklebust trace_xs_tcp_data_ready(xprt, read, total); 1416*66d7a56aSTrond Myklebust } 1417*66d7a56aSTrond Myklebust 14189903cd1cSChuck Lever /** 14199903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 14209903cd1cSChuck Lever * @sk: socket with data to read 14219903cd1cSChuck Lever * 14229903cd1cSChuck Lever */ 1423676d2369SDavid S. Miller static void xs_tcp_data_ready(struct sock *sk) 1424a246b010SChuck Lever { 1425*66d7a56aSTrond Myklebust struct sock_xprt *transport; 1426a246b010SChuck Lever struct rpc_xprt *xprt; 1427a246b010SChuck Lever 14289903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 142946121cf7SChuck Lever 1430f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1431*66d7a56aSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1432a246b010SChuck Lever goto out; 1433*66d7a56aSTrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1434*66d7a56aSTrond Myklebust 143561d0a8e6SNeil Brown /* Any data means we had a useful conversation, so 143661d0a8e6SNeil Brown * the we don't need to delay the next reconnect 143761d0a8e6SNeil Brown */ 143861d0a8e6SNeil Brown if (xprt->reestablish_timeout) 143961d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 144061d0a8e6SNeil Brown 1441*66d7a56aSTrond Myklebust xs_tcp_data_receive(transport); 1442a246b010SChuck Lever out: 1443f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1444a246b010SChuck Lever } 1445a246b010SChuck Lever 14469903cd1cSChuck Lever /** 14479903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14489903cd1cSChuck Lever * @sk: socket whose state has changed 14499903cd1cSChuck Lever * 14509903cd1cSChuck Lever */ 14519903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1452a246b010SChuck Lever { 1453a246b010SChuck Lever struct rpc_xprt *xprt; 14540fdea1e8STrond Myklebust struct sock_xprt *transport; 1455a246b010SChuck Lever 1456f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1457a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1458a246b010SChuck Lever goto out; 14599903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1460669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1461a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1462a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1463669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1464669502ffSAndy Chittenden sk->sk_shutdown); 1465a246b010SChuck Lever 14660fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 146740b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1468a246b010SChuck Lever switch (sk->sk_state) { 1469a246b010SChuck Lever case TCP_ESTABLISHED: 1470f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1471a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 147251971139SChuck Lever 1473a246b010SChuck Lever /* Reset TCP record info */ 147451971139SChuck Lever transport->tcp_offset = 0; 147551971139SChuck Lever transport->tcp_reclen = 0; 147651971139SChuck Lever transport->tcp_copied = 0; 1477e136d092SChuck Lever transport->tcp_flags = 1478e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 14798b71798cSTrond Myklebust xprt->connect_cookie++; 14800fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 14810fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 148251971139SChuck Lever 14832a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1484a246b010SChuck Lever } 1485f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1486a246b010SChuck Lever break; 14873b948ae5STrond Myklebust case TCP_FIN_WAIT1: 14883b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 14897c1d71cfSTrond Myklebust xprt->connect_cookie++; 1490663b8858STrond Myklebust xprt->reestablish_timeout = 0; 14913b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 14924e857c58SPeter Zijlstra smp_mb__before_atomic(); 14933b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1494ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 14954e857c58SPeter Zijlstra smp_mb__after_atomic(); 1496a246b010SChuck Lever break; 1497632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 14983b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 14997c1d71cfSTrond Myklebust xprt->connect_cookie++; 1500d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1501a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1502663b8858STrond Myklebust case TCP_CLOSING: 1503663b8858STrond Myklebust /* 1504663b8858STrond Myklebust * If the server closed down the connection, make sure that 1505663b8858STrond Myklebust * we back off before reconnecting 1506663b8858STrond Myklebust */ 1507663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1508663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15093b948ae5STrond Myklebust break; 15103b948ae5STrond Myklebust case TCP_LAST_ACK: 1511670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15124e857c58SPeter Zijlstra smp_mb__before_atomic(); 15133b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15144e857c58SPeter Zijlstra smp_mb__after_atomic(); 15153b948ae5STrond Myklebust break; 15163b948ae5STrond Myklebust case TCP_CLOSE: 15170fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15180fdea1e8STrond Myklebust &transport->sock_state)) 15190fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15207d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1521a246b010SChuck Lever } 1522a246b010SChuck Lever out: 1523f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1524a246b010SChuck Lever } 1525a246b010SChuck Lever 15261f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15271f0fa154SIlpo Järvinen { 15281f0fa154SIlpo Järvinen struct socket *sock; 15291f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15301f0fa154SIlpo Järvinen 15311f0fa154SIlpo Järvinen if (unlikely(!(sock = sk->sk_socket))) 15321f0fa154SIlpo Järvinen return; 15331f0fa154SIlpo Järvinen clear_bit(SOCK_NOSPACE, &sock->flags); 15341f0fa154SIlpo Järvinen 15351f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15361f0fa154SIlpo Järvinen return; 15371f0fa154SIlpo Järvinen if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 15381f0fa154SIlpo Järvinen return; 15391f0fa154SIlpo Järvinen 15401f0fa154SIlpo Järvinen xprt_write_space(xprt); 15411f0fa154SIlpo Järvinen } 15421f0fa154SIlpo Järvinen 15432a9e1cfaSTrond Myklebust /** 1544c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1545c7b2cae8SChuck Lever * becomes available 15469903cd1cSChuck Lever * @sk: socket whose state has changed 15479903cd1cSChuck Lever * 1548a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1549a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1550c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1551a246b010SChuck Lever * with a bunch of small requests. 1552a246b010SChuck Lever */ 1553c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1554a246b010SChuck Lever { 1555f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1556c7b2cae8SChuck Lever 1557c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 15581f0fa154SIlpo Järvinen if (sock_writeable(sk)) 15591f0fa154SIlpo Järvinen xs_write_space(sk); 1560c7b2cae8SChuck Lever 1561f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1562c7b2cae8SChuck Lever } 1563c7b2cae8SChuck Lever 1564c7b2cae8SChuck Lever /** 1565c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1566c7b2cae8SChuck Lever * becomes available 1567c7b2cae8SChuck Lever * @sk: socket whose state has changed 1568c7b2cae8SChuck Lever * 1569c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1570c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1571c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1572c7b2cae8SChuck Lever * with a bunch of small requests. 1573c7b2cae8SChuck Lever */ 1574c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1575c7b2cae8SChuck Lever { 1576f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1577c7b2cae8SChuck Lever 1578c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 157964dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 15801f0fa154SIlpo Järvinen xs_write_space(sk); 1581c7b2cae8SChuck Lever 1582f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1583a246b010SChuck Lever } 1584a246b010SChuck Lever 1585470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1586a246b010SChuck Lever { 1587ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1588ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1589a246b010SChuck Lever 15907c6e066eSChuck Lever if (transport->rcvsize) { 1591a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 15927c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1593a246b010SChuck Lever } 15947c6e066eSChuck Lever if (transport->sndsize) { 1595a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 15967c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1597a246b010SChuck Lever sk->sk_write_space(sk); 1598a246b010SChuck Lever } 1599a246b010SChuck Lever } 1600a246b010SChuck Lever 160143118c29SChuck Lever /** 1602470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 160343118c29SChuck Lever * @xprt: generic transport 1604470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1605470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 160643118c29SChuck Lever * 1607470056c2SChuck Lever * Set socket send and receive buffer size limits. 160843118c29SChuck Lever */ 1609470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 161043118c29SChuck Lever { 16117c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16127c6e066eSChuck Lever 16137c6e066eSChuck Lever transport->sndsize = 0; 1614470056c2SChuck Lever if (sndsize) 16157c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16167c6e066eSChuck Lever transport->rcvsize = 0; 1617470056c2SChuck Lever if (rcvsize) 16187c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1619470056c2SChuck Lever 1620470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 162143118c29SChuck Lever } 162243118c29SChuck Lever 162346c0ee8bSChuck Lever /** 162446c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 162546c0ee8bSChuck Lever * @task: task that timed out 162646c0ee8bSChuck Lever * 162746c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 162846c0ee8bSChuck Lever */ 16296a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 163046c0ee8bSChuck Lever { 16316a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 163246c0ee8bSChuck Lever } 163346c0ee8bSChuck Lever 1634b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1635b85d8806SChuck Lever { 1636b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 163763862b5bSAruna-Hewapathirane unsigned short rand = (unsigned short) prandom_u32() % range; 1638b85d8806SChuck Lever return rand + xprt_min_resvport; 1639b85d8806SChuck Lever } 1640b85d8806SChuck Lever 164192200412SChuck Lever /** 16424dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 16434dda9c8aSTrond Myklebust * @sock: socket 16444dda9c8aSTrond Myklebust * 16454dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 16464dda9c8aSTrond Myklebust * same port, and it must be called before binding. 16474dda9c8aSTrond Myklebust */ 16484dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 16494dda9c8aSTrond Myklebust { 1650402e23b4STrond Myklebust int opt = 1; 16514dda9c8aSTrond Myklebust 1652402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1653402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 16544dda9c8aSTrond Myklebust } 16554dda9c8aSTrond Myklebust 16564dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 16574dda9c8aSTrond Myklebust { 16584dda9c8aSTrond Myklebust struct sockaddr_storage buf; 16594dda9c8aSTrond Myklebust int buflen; 16604dda9c8aSTrond Myklebust unsigned short port = 0; 16614dda9c8aSTrond Myklebust 16624dda9c8aSTrond Myklebust if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0) 16634dda9c8aSTrond Myklebust goto out; 16644dda9c8aSTrond Myklebust switch (buf.ss_family) { 16654dda9c8aSTrond Myklebust case AF_INET6: 16664dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 16674dda9c8aSTrond Myklebust break; 16684dda9c8aSTrond Myklebust case AF_INET: 16694dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 16704dda9c8aSTrond Myklebust } 16714dda9c8aSTrond Myklebust out: 16724dda9c8aSTrond Myklebust return port; 16734dda9c8aSTrond Myklebust } 16744dda9c8aSTrond Myklebust 16754dda9c8aSTrond Myklebust /** 167692200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 167792200412SChuck Lever * @xprt: generic transport 167892200412SChuck Lever * @port: new port number 167992200412SChuck Lever * 168092200412SChuck Lever */ 168192200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 168292200412SChuck Lever { 168392200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1684c4efcb1dSChuck Lever 16859dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 16869dc3b095SChuck Lever xs_update_peer_port(xprt); 168792200412SChuck Lever } 168892200412SChuck Lever 16894dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 16904dda9c8aSTrond Myklebust { 16914dda9c8aSTrond Myklebust if (transport->srcport == 0) 16924dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 16934dda9c8aSTrond Myklebust } 16944dda9c8aSTrond Myklebust 16955d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport) 169667a391d7STrond Myklebust { 1697fbfffbd5SChuck Lever unsigned short port = transport->srcport; 169867a391d7STrond Myklebust 169967a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 170067a391d7STrond Myklebust port = xs_get_random_port(); 170167a391d7STrond Myklebust return port; 170267a391d7STrond Myklebust } 170367a391d7STrond Myklebust 1704baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 170567a391d7STrond Myklebust { 1706fbfffbd5SChuck Lever if (transport->srcport != 0) 1707fbfffbd5SChuck Lever transport->srcport = 0; 170867a391d7STrond Myklebust if (!transport->xprt.resvport) 170967a391d7STrond Myklebust return 0; 171067a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 171167a391d7STrond Myklebust return xprt_max_resvport; 171267a391d7STrond Myklebust return --port; 171367a391d7STrond Myklebust } 1714beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1715a246b010SChuck Lever { 1716beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 171767a391d7STrond Myklebust int err, nloop = 0; 17185d4ec932SPavel Emelyanov unsigned short port = xs_get_srcport(transport); 171967a391d7STrond Myklebust unsigned short last; 1720a246b010SChuck Lever 17210f7a622cSChris Perl /* 17220f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17230f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17240f7a622cSChris Perl * port selection happen implicitly when the socket is used 17250f7a622cSChris Perl * (for example at connect time). 17260f7a622cSChris Perl * 17270f7a622cSChris Perl * This ensures that we can continue to establish TCP 17280f7a622cSChris Perl * connections even when all local ephemeral ports are already 17290f7a622cSChris Perl * a part of some TCP connection. This makes no difference 17300f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 17310f7a622cSChris Perl * 17320f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17330f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17340f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17350f7a622cSChris Perl */ 17360f7a622cSChris Perl if (port == 0) 17370f7a622cSChris Perl return 0; 17380f7a622cSChris Perl 1739beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1740a246b010SChuck Lever do { 1741beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1742e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1743beb59b68SPavel Emelyanov transport->xprt.addrlen); 1744a246b010SChuck Lever if (err == 0) { 1745fbfffbd5SChuck Lever transport->srcport = port; 1746d3bc9a1dSFrank van Maarseveen break; 1747a246b010SChuck Lever } 174867a391d7STrond Myklebust last = port; 1749baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 175067a391d7STrond Myklebust if (port > last) 175167a391d7STrond Myklebust nloop++; 175267a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1753beb59b68SPavel Emelyanov 17544232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1755beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1756beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1757beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1758beb59b68SPavel Emelyanov else 1759beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1760beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 17617dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1762a246b010SChuck Lever return err; 1763a246b010SChuck Lever } 1764a246b010SChuck Lever 1765176e21eeSChuck Lever /* 1766176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1767176e21eeSChuck Lever */ 1768176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1769176e21eeSChuck Lever { 17703dc0da27STrond Myklebust rcu_read_lock(); 17713dc0da27STrond Myklebust xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt)); 17723dc0da27STrond Myklebust rcu_read_unlock(); 1773176e21eeSChuck Lever } 1774176e21eeSChuck Lever 1775176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1776176e21eeSChuck Lever { 1777176e21eeSChuck Lever } 1778a246b010SChuck Lever 1779ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1780ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1781ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1782ed07536eSPeter Zijlstra 1783176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1784176e21eeSChuck Lever { 1785176e21eeSChuck Lever struct sock *sk = sock->sk; 1786176e21eeSChuck Lever 1787176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1788176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1789176e21eeSChuck Lever } 1790176e21eeSChuck Lever 17918945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1792ed07536eSPeter Zijlstra { 1793ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 17948945ee5eSChuck Lever 17958945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 17968945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1797ed07536eSPeter Zijlstra } 17988945ee5eSChuck Lever 17998945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18008945ee5eSChuck Lever { 18018945ee5eSChuck Lever struct sock *sk = sock->sk; 18028945ee5eSChuck Lever 18038945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 18048945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1805ed07536eSPeter Zijlstra } 18066bc9638aSPavel Emelyanov 18076bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18086bc9638aSPavel Emelyanov { 18091b7a1819SWeston Andros Adamson WARN_ON_ONCE(sock_owned_by_user(sock->sk)); 18101b7a1819SWeston Andros Adamson if (sock_owned_by_user(sock->sk)) 18111b7a1819SWeston Andros Adamson return; 18121b7a1819SWeston Andros Adamson 18134232e863SChuck Lever switch (family) { 1814176e21eeSChuck Lever case AF_LOCAL: 1815176e21eeSChuck Lever xs_reclassify_socketu(sock); 1816176e21eeSChuck Lever break; 18174232e863SChuck Lever case AF_INET: 18186bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18194232e863SChuck Lever break; 18204232e863SChuck Lever case AF_INET6: 18216bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18224232e863SChuck Lever break; 18234232e863SChuck Lever } 18246bc9638aSPavel Emelyanov } 1825ed07536eSPeter Zijlstra #else 1826176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1827176e21eeSChuck Lever { 1828176e21eeSChuck Lever } 1829176e21eeSChuck Lever 18308945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 18318945ee5eSChuck Lever { 18328945ee5eSChuck Lever } 18338945ee5eSChuck Lever 18348945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1835ed07536eSPeter Zijlstra { 1836ed07536eSPeter Zijlstra } 18376bc9638aSPavel Emelyanov 18386bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18396bc9638aSPavel Emelyanov { 18406bc9638aSPavel Emelyanov } 1841ed07536eSPeter Zijlstra #endif 1842ed07536eSPeter Zijlstra 184393dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 184493dc41bdSNeilBrown { 184593dc41bdSNeilBrown } 184693dc41bdSNeilBrown 18476bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18484dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18494dda9c8aSTrond Myklebust int protocol, bool reuseport) 185022f79326SPavel Emelyanov { 185122f79326SPavel Emelyanov struct socket *sock; 185222f79326SPavel Emelyanov int err; 185322f79326SPavel Emelyanov 18546bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 185522f79326SPavel Emelyanov if (err < 0) { 185622f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 185722f79326SPavel Emelyanov protocol, -err); 185822f79326SPavel Emelyanov goto out; 185922f79326SPavel Emelyanov } 18606bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 186122f79326SPavel Emelyanov 18624dda9c8aSTrond Myklebust if (reuseport) 18634dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 18644dda9c8aSTrond Myklebust 18654cea288aSBen Hutchings err = xs_bind(transport, sock); 18664cea288aSBen Hutchings if (err) { 186722f79326SPavel Emelyanov sock_release(sock); 186822f79326SPavel Emelyanov goto out; 186922f79326SPavel Emelyanov } 187022f79326SPavel Emelyanov 187122f79326SPavel Emelyanov return sock; 187222f79326SPavel Emelyanov out: 187322f79326SPavel Emelyanov return ERR_PTR(err); 187422f79326SPavel Emelyanov } 187522f79326SPavel Emelyanov 1876176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1877176e21eeSChuck Lever struct socket *sock) 1878176e21eeSChuck Lever { 1879176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1880176e21eeSChuck Lever xprt); 1881176e21eeSChuck Lever 1882176e21eeSChuck Lever if (!transport->inet) { 1883176e21eeSChuck Lever struct sock *sk = sock->sk; 1884176e21eeSChuck Lever 1885176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1886176e21eeSChuck Lever 1887176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1888176e21eeSChuck Lever 1889176e21eeSChuck Lever sk->sk_user_data = xprt; 1890176e21eeSChuck Lever sk->sk_data_ready = xs_local_data_ready; 1891176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 18922118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1893c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 1894176e21eeSChuck Lever 1895176e21eeSChuck Lever xprt_clear_connected(xprt); 1896176e21eeSChuck Lever 1897176e21eeSChuck Lever /* Reset to new socket */ 1898176e21eeSChuck Lever transport->sock = sock; 1899176e21eeSChuck Lever transport->inet = sk; 1900176e21eeSChuck Lever 1901176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1902176e21eeSChuck Lever } 1903176e21eeSChuck Lever 1904176e21eeSChuck Lever /* Tell the socket layer to start connecting... */ 1905176e21eeSChuck Lever xprt->stat.connect_count++; 1906176e21eeSChuck Lever xprt->stat.connect_start = jiffies; 1907176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1908176e21eeSChuck Lever } 1909176e21eeSChuck Lever 1910176e21eeSChuck Lever /** 1911176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1912176e21eeSChuck Lever * @transport: socket transport to connect 1913176e21eeSChuck Lever */ 1914dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1915176e21eeSChuck Lever { 1916176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1917176e21eeSChuck Lever struct socket *sock; 1918176e21eeSChuck Lever int status = -EIO; 1919176e21eeSChuck Lever 1920176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1921176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1922176e21eeSChuck Lever if (status < 0) { 1923176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1924176e21eeSChuck Lever "transport socket (%d).\n", -status); 1925176e21eeSChuck Lever goto out; 1926176e21eeSChuck Lever } 1927176e21eeSChuck Lever xs_reclassify_socketu(sock); 1928176e21eeSChuck Lever 1929176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1930176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1931176e21eeSChuck Lever 1932176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 193340b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1934176e21eeSChuck Lever switch (status) { 1935176e21eeSChuck Lever case 0: 1936176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1937176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1938176e21eeSChuck Lever xprt_set_connected(xprt); 19393601c4a9STrond Myklebust case -ENOBUFS: 1940176e21eeSChuck Lever break; 1941176e21eeSChuck Lever case -ENOENT: 1942176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 1943176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1944176e21eeSChuck Lever break; 19454a20a988STrond Myklebust case -ECONNREFUSED: 19464a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 19474a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19484a20a988STrond Myklebust break; 1949176e21eeSChuck Lever default: 1950176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1951176e21eeSChuck Lever __func__, -status, 1952176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 1953176e21eeSChuck Lever } 1954176e21eeSChuck Lever 1955176e21eeSChuck Lever out: 1956176e21eeSChuck Lever xprt_clear_connecting(xprt); 1957176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 1958dc107402SJ. Bruce Fields return status; 1959dc107402SJ. Bruce Fields } 1960dc107402SJ. Bruce Fields 1961b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 1962dc107402SJ. Bruce Fields { 1963dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1964dc107402SJ. Bruce Fields int ret; 1965dc107402SJ. Bruce Fields 1966dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 1967dc107402SJ. Bruce Fields /* 1968dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 1969dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 1970dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 1971dc107402SJ. Bruce Fields * 1972dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 1973dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 1974dc107402SJ. Bruce Fields * connect. 1975dc107402SJ. Bruce Fields */ 1976dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 1977dc107402SJ. Bruce Fields return; 1978dc107402SJ. Bruce Fields } 1979dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 1980dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 1981dc107402SJ. Bruce Fields msleep_interruptible(15000); 1982176e21eeSChuck Lever } 1983176e21eeSChuck Lever 19843c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 1985d6e971d8SJeff Layton /* 1986d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 1987d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 1988d6e971d8SJeff Layton * races with xs_reset_transport. 1989d6e971d8SJeff Layton */ 1990a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 1991a564b8f0SMel Gorman { 1992a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1993a564b8f0SMel Gorman xprt); 1994a564b8f0SMel Gorman 1995d6e971d8SJeff Layton /* 1996d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 1997d6e971d8SJeff Layton * reconnecting process will get it for us. 1998d6e971d8SJeff Layton */ 1999d6e971d8SJeff Layton if (!transport->inet) 2000d6e971d8SJeff Layton return; 20018e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2002a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2003a564b8f0SMel Gorman } 2004a564b8f0SMel Gorman 2005a564b8f0SMel Gorman /** 2006d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2007a564b8f0SMel Gorman * @xprt: transport to tag 2008a564b8f0SMel Gorman * 20098e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20108e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2011a564b8f0SMel Gorman */ 2012d67fa4d8SJeff Layton static int 2013d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2014a564b8f0SMel Gorman { 2015d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2016a564b8f0SMel Gorman 2017d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2018d6e971d8SJeff Layton return 0; 2019d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2020d6e971d8SJeff Layton return -ERESTARTSYS; 2021d6e971d8SJeff Layton if (xs->inet) 2022d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2023d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 20248e228133SJeff Layton return 0; 2025a564b8f0SMel Gorman } 2026a564b8f0SMel Gorman 20278e228133SJeff Layton /** 2028d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20298e228133SJeff Layton * @xprt: transport to tag 20308e228133SJeff Layton * 20318e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 20328e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 20338e228133SJeff Layton */ 2034d67fa4d8SJeff Layton static void 2035d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 20368e228133SJeff Layton { 2037d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 20388e228133SJeff Layton 2039d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2040d6e971d8SJeff Layton return; 2041d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2042d6e971d8SJeff Layton return; 2043d6e971d8SJeff Layton if (xs->inet) 2044d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2045d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2046a564b8f0SMel Gorman } 2047a564b8f0SMel Gorman #else 2048a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2049a564b8f0SMel Gorman { 2050a564b8f0SMel Gorman } 2051d67fa4d8SJeff Layton 2052d67fa4d8SJeff Layton static int 2053d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2054d67fa4d8SJeff Layton { 2055d67fa4d8SJeff Layton return -EINVAL; 2056d67fa4d8SJeff Layton } 2057d67fa4d8SJeff Layton 2058d67fa4d8SJeff Layton static void 2059d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2060d67fa4d8SJeff Layton { 2061d67fa4d8SJeff Layton } 2062a564b8f0SMel Gorman #endif 2063a564b8f0SMel Gorman 206416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2065a246b010SChuck Lever { 206616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2067edb267a6SChuck Lever 2068ee0ac0c2SChuck Lever if (!transport->inet) { 2069b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2070b0d93ad5SChuck Lever 2071b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2072b0d93ad5SChuck Lever 20732a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 20742a9e1cfaSTrond Myklebust 2075b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2076b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 2077b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2078c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2079b0d93ad5SChuck Lever 2080b0d93ad5SChuck Lever xprt_set_connected(xprt); 2081b0d93ad5SChuck Lever 2082b0d93ad5SChuck Lever /* Reset to new socket */ 2083ee0ac0c2SChuck Lever transport->sock = sock; 2084ee0ac0c2SChuck Lever transport->inet = sk; 2085b0d93ad5SChuck Lever 2086a564b8f0SMel Gorman xs_set_memalloc(xprt); 2087a564b8f0SMel Gorman 2088b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2089b0d93ad5SChuck Lever } 2090470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 209116be2d20SChuck Lever } 209216be2d20SChuck Lever 20938c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2094a246b010SChuck Lever { 2095a246b010SChuck Lever struct sock_xprt *transport = 2096a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2097a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2098a246b010SChuck Lever struct socket *sock = transport->sock; 2099b65c0310SPavel Emelyanov int status = -EIO; 2100a246b010SChuck Lever 21018c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21024dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21034dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2104b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2105a246b010SChuck Lever goto out; 210668e220bdSChuck Lever 2107c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2108c740eff8SChuck Lever "%s (port %s)\n", xprt, 2109c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2110c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2111c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 211268e220bdSChuck Lever 211368e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 211440b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2115a246b010SChuck Lever status = 0; 2116b0d93ad5SChuck Lever out: 2117718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 2118b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 21197d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2120b0d93ad5SChuck Lever } 2121b0d93ad5SChuck Lever 21224876cc77STrond Myklebust /** 21234876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21244876cc77STrond Myklebust * @xprt: transport 21254876cc77STrond Myklebust * 21264876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 21274876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 21284876cc77STrond Myklebust */ 21294876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 21304876cc77STrond Myklebust { 21314876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21324876cc77STrond Myklebust struct socket *sock = transport->sock; 21334876cc77STrond Myklebust 21344876cc77STrond Myklebust if (sock == NULL) 21354876cc77STrond Myklebust return; 21364876cc77STrond Myklebust if (xprt_connected(xprt)) { 21374876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 21384876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 21394876cc77STrond Myklebust } else 21404876cc77STrond Myklebust xs_reset_transport(transport); 21414876cc77STrond Myklebust } 21424876cc77STrond Myklebust 214316be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2144b0d93ad5SChuck Lever { 214516be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2146fe19a96bSTrond Myklebust int ret = -ENOTCONN; 2147edb267a6SChuck Lever 2148ee0ac0c2SChuck Lever if (!transport->inet) { 2149b0d93ad5SChuck Lever struct sock *sk = sock->sk; 21507f260e85STrond Myklebust unsigned int keepidle = xprt->timeout->to_initval / HZ; 21517f260e85STrond Myklebust unsigned int keepcnt = xprt->timeout->to_retries + 1; 21527f260e85STrond Myklebust unsigned int opt_on = 1; 2153775f06abSTrond Myklebust unsigned int timeo; 21547f260e85STrond Myklebust 21557f260e85STrond Myklebust /* TCP Keepalive options */ 21567f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 21577f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 21587f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 21597f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 21607f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 21617f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 21627f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 21637f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2164b0d93ad5SChuck Lever 2165775f06abSTrond Myklebust /* TCP user timeout (see RFC5482) */ 2166775f06abSTrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 2167775f06abSTrond Myklebust (xprt->timeout->to_retries + 1); 2168775f06abSTrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 2169775f06abSTrond Myklebust (char *)&timeo, sizeof(timeo)); 2170775f06abSTrond Myklebust 2171b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2172b0d93ad5SChuck Lever 21732a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21742a9e1cfaSTrond Myklebust 2175b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2176b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 2177b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2178b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 21792118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2180c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 21813167e12cSChuck Lever 21823167e12cSChuck Lever /* socket options */ 21833167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 21843167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2185b0d93ad5SChuck Lever 2186b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2187b0d93ad5SChuck Lever 2188b0d93ad5SChuck Lever /* Reset to new socket */ 2189ee0ac0c2SChuck Lever transport->sock = sock; 2190ee0ac0c2SChuck Lever transport->inet = sk; 2191b0d93ad5SChuck Lever 2192b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2193b0d93ad5SChuck Lever } 2194b0d93ad5SChuck Lever 219501d37c42STrond Myklebust if (!xprt_bound(xprt)) 2196fe19a96bSTrond Myklebust goto out; 219701d37c42STrond Myklebust 2198a564b8f0SMel Gorman xs_set_memalloc(xprt); 2199a564b8f0SMel Gorman 2200b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 2201262ca07dSChuck Lever xprt->stat.connect_count++; 2202262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 22030fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2204fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2205fe19a96bSTrond Myklebust switch (ret) { 2206fe19a96bSTrond Myklebust case 0: 22074dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2208fe19a96bSTrond Myklebust case -EINPROGRESS: 2209fe19a96bSTrond Myklebust /* SYN_SENT! */ 2210fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2211fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2212fe19a96bSTrond Myklebust } 2213fe19a96bSTrond Myklebust out: 2214fe19a96bSTrond Myklebust return ret; 221516be2d20SChuck Lever } 221616be2d20SChuck Lever 221716be2d20SChuck Lever /** 2218b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 221916be2d20SChuck Lever * 222016be2d20SChuck Lever * Invoked by a work queue tasklet. 222116be2d20SChuck Lever */ 2222cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 222316be2d20SChuck Lever { 2224cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2225cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 222616be2d20SChuck Lever struct socket *sock = transport->sock; 2227a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2228b61d59ffSTrond Myklebust int status = -EIO; 222916be2d20SChuck Lever 223016be2d20SChuck Lever if (!sock) { 2231cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 22324dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 22334dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2234b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2235b61d59ffSTrond Myklebust status = PTR_ERR(sock); 223616be2d20SChuck Lever goto out; 223716be2d20SChuck Lever } 22387d1e8255STrond Myklebust } 22397d1e8255STrond Myklebust 2240c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2241c740eff8SChuck Lever "%s (port %s)\n", xprt, 2242c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2243c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2244c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 224516be2d20SChuck Lever 224616be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 224740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2248a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 224946121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 225046121cf7SChuck Lever sock->sk->sk_state); 2251a246b010SChuck Lever switch (status) { 2252f75e6745STrond Myklebust default: 2253f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2254f75e6745STrond Myklebust __func__, status); 2255f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2256f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2257f75e6745STrond Myklebust * and retry 2258f75e6745STrond Myklebust */ 2259a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 226088b5ed73STrond Myklebust break; 22612a491991STrond Myklebust case 0: 2262a246b010SChuck Lever case -EINPROGRESS: 2263a246b010SChuck Lever case -EALREADY: 2264718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 22657d1e8255STrond Myklebust return; 22669fcfe0c8STrond Myklebust case -EINVAL: 22679fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 22689fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 22699fcfe0c8STrond Myklebust */ 22703ed5e2a2STrond Myklebust case -ECONNREFUSED: 22713ed5e2a2STrond Myklebust case -ECONNRESET: 22723ed5e2a2STrond Myklebust case -ENETUNREACH: 22733913c78cSTrond Myklebust case -EADDRINUSE: 22743601c4a9STrond Myklebust case -ENOBUFS: 22753ed5e2a2STrond Myklebust /* retry with existing socket, after a delay */ 22764efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 22779fcfe0c8STrond Myklebust goto out; 22788a2cec29STrond Myklebust } 22792a491991STrond Myklebust status = -EAGAIN; 2280a246b010SChuck Lever out: 2281718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 22822226feb6SChuck Lever xprt_clear_connecting(xprt); 22837d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2284a246b010SChuck Lever } 2285a246b010SChuck Lever 228668e220bdSChuck Lever /** 22879903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 22881b092092STrond Myklebust * @xprt: pointer to transport structure 22899903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 22909903cd1cSChuck Lever * 22919903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 229203bf4b70SChuck Lever * 229303bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 229403bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 229503bf4b70SChuck Lever * socket on a privileged port. 229603bf4b70SChuck Lever * 229703bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 229803bf4b70SChuck Lever * retry floods (hard mounts). 22999903cd1cSChuck Lever */ 23001b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2301a246b010SChuck Lever { 2302ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2303a246b010SChuck Lever 2304718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2305718ba5b8STrond Myklebust 230699b1a4c3STrond Myklebust if (transport->sock != NULL) { 230746121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 230846121cf7SChuck Lever "seconds\n", 230903bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 231099b1a4c3STrond Myklebust 231199b1a4c3STrond Myklebust /* Start by resetting any existing state */ 231299b1a4c3STrond Myklebust xs_reset_transport(transport); 231399b1a4c3STrond Myklebust 2314c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2315c1384c9cSTrond Myklebust &transport->connect_worker, 231603bf4b70SChuck Lever xprt->reestablish_timeout); 231703bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 231861d0a8e6SNeil Brown if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 231961d0a8e6SNeil Brown xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 232003bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 232103bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 23229903cd1cSChuck Lever } else { 23239903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2324c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2325c1384c9cSTrond Myklebust &transport->connect_worker, 0); 2326a246b010SChuck Lever } 2327a246b010SChuck Lever } 2328a246b010SChuck Lever 2329262ca07dSChuck Lever /** 2330176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2331176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2332176e21eeSChuck Lever * @seq: output file 2333176e21eeSChuck Lever * 2334176e21eeSChuck Lever */ 2335176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2336176e21eeSChuck Lever { 2337176e21eeSChuck Lever long idle_time = 0; 2338176e21eeSChuck Lever 2339176e21eeSChuck Lever if (xprt_connected(xprt)) 2340176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2341176e21eeSChuck Lever 2342176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 234315a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2344176e21eeSChuck Lever xprt->stat.bind_count, 2345176e21eeSChuck Lever xprt->stat.connect_count, 2346176e21eeSChuck Lever xprt->stat.connect_time, 2347176e21eeSChuck Lever idle_time, 2348176e21eeSChuck Lever xprt->stat.sends, 2349176e21eeSChuck Lever xprt->stat.recvs, 2350176e21eeSChuck Lever xprt->stat.bad_xids, 2351176e21eeSChuck Lever xprt->stat.req_u, 235215a45206SAndy Adamson xprt->stat.bklog_u, 235315a45206SAndy Adamson xprt->stat.max_slots, 235415a45206SAndy Adamson xprt->stat.sending_u, 235515a45206SAndy Adamson xprt->stat.pending_u); 2356176e21eeSChuck Lever } 2357176e21eeSChuck Lever 2358176e21eeSChuck Lever /** 2359262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2360262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2361262ca07dSChuck Lever * @seq: output file 2362262ca07dSChuck Lever * 2363262ca07dSChuck Lever */ 2364262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2365262ca07dSChuck Lever { 2366c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2367c8475461SChuck Lever 236815a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 236915a45206SAndy Adamson "%lu %llu %llu\n", 2370fbfffbd5SChuck Lever transport->srcport, 2371262ca07dSChuck Lever xprt->stat.bind_count, 2372262ca07dSChuck Lever xprt->stat.sends, 2373262ca07dSChuck Lever xprt->stat.recvs, 2374262ca07dSChuck Lever xprt->stat.bad_xids, 2375262ca07dSChuck Lever xprt->stat.req_u, 237615a45206SAndy Adamson xprt->stat.bklog_u, 237715a45206SAndy Adamson xprt->stat.max_slots, 237815a45206SAndy Adamson xprt->stat.sending_u, 237915a45206SAndy Adamson xprt->stat.pending_u); 2380262ca07dSChuck Lever } 2381262ca07dSChuck Lever 2382262ca07dSChuck Lever /** 2383262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2384262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2385262ca07dSChuck Lever * @seq: output file 2386262ca07dSChuck Lever * 2387262ca07dSChuck Lever */ 2388262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2389262ca07dSChuck Lever { 2390c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2391262ca07dSChuck Lever long idle_time = 0; 2392262ca07dSChuck Lever 2393262ca07dSChuck Lever if (xprt_connected(xprt)) 2394262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2395262ca07dSChuck Lever 239615a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 239715a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2398fbfffbd5SChuck Lever transport->srcport, 2399262ca07dSChuck Lever xprt->stat.bind_count, 2400262ca07dSChuck Lever xprt->stat.connect_count, 2401262ca07dSChuck Lever xprt->stat.connect_time, 2402262ca07dSChuck Lever idle_time, 2403262ca07dSChuck Lever xprt->stat.sends, 2404262ca07dSChuck Lever xprt->stat.recvs, 2405262ca07dSChuck Lever xprt->stat.bad_xids, 2406262ca07dSChuck Lever xprt->stat.req_u, 240715a45206SAndy Adamson xprt->stat.bklog_u, 240815a45206SAndy Adamson xprt->stat.max_slots, 240915a45206SAndy Adamson xprt->stat.sending_u, 241015a45206SAndy Adamson xprt->stat.pending_u); 2411262ca07dSChuck Lever } 2412262ca07dSChuck Lever 24134cfc7e60SRahul Iyer /* 24144cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 24154cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 24164cfc7e60SRahul Iyer * to use the server side send routines. 24174cfc7e60SRahul Iyer */ 24185a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size) 24194cfc7e60SRahul Iyer { 24204cfc7e60SRahul Iyer struct page *page; 24214cfc7e60SRahul Iyer struct rpc_buffer *buf; 24224cfc7e60SRahul Iyer 2423b8a13d03SWeston Andros Adamson WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer)); 2424b8a13d03SWeston Andros Adamson if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) 2425b8a13d03SWeston Andros Adamson return NULL; 24264cfc7e60SRahul Iyer 2427b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 24284cfc7e60SRahul Iyer if (!page) 24294cfc7e60SRahul Iyer return NULL; 24304cfc7e60SRahul Iyer 24314cfc7e60SRahul Iyer buf = page_address(page); 24324cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 24334cfc7e60SRahul Iyer 24344cfc7e60SRahul Iyer return buf->data; 24354cfc7e60SRahul Iyer } 24364cfc7e60SRahul Iyer 24374cfc7e60SRahul Iyer /* 24384cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 24394cfc7e60SRahul Iyer */ 24405a51f13aSH Hartley Sweeten static void bc_free(void *buffer) 24414cfc7e60SRahul Iyer { 24424cfc7e60SRahul Iyer struct rpc_buffer *buf; 24434cfc7e60SRahul Iyer 24444cfc7e60SRahul Iyer if (!buffer) 24454cfc7e60SRahul Iyer return; 24464cfc7e60SRahul Iyer 24474cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 24484cfc7e60SRahul Iyer free_page((unsigned long)buf); 24494cfc7e60SRahul Iyer } 24504cfc7e60SRahul Iyer 24514cfc7e60SRahul Iyer /* 24524cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 24534cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 24544cfc7e60SRahul Iyer */ 24554cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 24564cfc7e60SRahul Iyer { 24574cfc7e60SRahul Iyer int len; 24584cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 24594cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 24604cfc7e60SRahul Iyer struct sock_xprt *transport = 24614cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 24624cfc7e60SRahul Iyer struct socket *sock = transport->sock; 24634cfc7e60SRahul Iyer unsigned long headoff; 24644cfc7e60SRahul Iyer unsigned long tailoff; 24654cfc7e60SRahul Iyer 246661677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 24674cfc7e60SRahul Iyer 24684cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 24694cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 24704cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 24714cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 24724cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 24734cfc7e60SRahul Iyer 24744cfc7e60SRahul Iyer if (len != xbufp->len) { 24754cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 24764cfc7e60SRahul Iyer len = -EAGAIN; 24774cfc7e60SRahul Iyer } 24784cfc7e60SRahul Iyer 24794cfc7e60SRahul Iyer return len; 24804cfc7e60SRahul Iyer } 24814cfc7e60SRahul Iyer 24824cfc7e60SRahul Iyer /* 24834cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 24844cfc7e60SRahul Iyer */ 24854cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task) 24864cfc7e60SRahul Iyer { 24874cfc7e60SRahul Iyer struct rpc_rqst *req = task->tk_rqstp; 24884cfc7e60SRahul Iyer struct svc_xprt *xprt; 24894cfc7e60SRahul Iyer u32 len; 24904cfc7e60SRahul Iyer 24914cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 24924cfc7e60SRahul Iyer /* 24934cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 24944cfc7e60SRahul Iyer */ 24954cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 24964cfc7e60SRahul Iyer 24974cfc7e60SRahul Iyer /* 24984cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 24994cfc7e60SRahul Iyer * with the fore channel 25004cfc7e60SRahul Iyer */ 25014cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) { 25024cfc7e60SRahul Iyer rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); 25034cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) 25044cfc7e60SRahul Iyer return -EAGAIN; 25054cfc7e60SRahul Iyer rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); 25064cfc7e60SRahul Iyer } 25074cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 25084cfc7e60SRahul Iyer len = -ENOTCONN; 25094cfc7e60SRahul Iyer else 25104cfc7e60SRahul Iyer len = bc_sendto(req); 25114cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 25124cfc7e60SRahul Iyer 25134cfc7e60SRahul Iyer if (len > 0) 25144cfc7e60SRahul Iyer len = 0; 25154cfc7e60SRahul Iyer 25164cfc7e60SRahul Iyer return len; 25174cfc7e60SRahul Iyer } 25184cfc7e60SRahul Iyer 25194cfc7e60SRahul Iyer /* 25204cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 25214cfc7e60SRahul Iyer */ 25224cfc7e60SRahul Iyer 25234cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 25244cfc7e60SRahul Iyer { 25254cfc7e60SRahul Iyer } 25264cfc7e60SRahul Iyer 25274cfc7e60SRahul Iyer /* 25284cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 25294cfc7e60SRahul Iyer * initiated, we do nothing 25304cfc7e60SRahul Iyer */ 25314cfc7e60SRahul Iyer 25324cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 25334cfc7e60SRahul Iyer { 253447f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 253547f72efaSKinglong Mee 253647f72efaSKinglong Mee xs_xprt_free(xprt); 253747f72efaSKinglong Mee module_put(THIS_MODULE); 25384cfc7e60SRahul Iyer } 25394cfc7e60SRahul Iyer 2540176e21eeSChuck Lever static struct rpc_xprt_ops xs_local_ops = { 2541176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 2542176e21eeSChuck Lever .release_xprt = xs_tcp_release_xprt, 2543f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2544176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2545176e21eeSChuck Lever .set_port = xs_local_set_port, 2546dc107402SJ. Bruce Fields .connect = xs_local_connect, 2547176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2548176e21eeSChuck Lever .buf_free = rpc_free, 2549176e21eeSChuck Lever .send_request = xs_local_send_request, 2550176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2551176e21eeSChuck Lever .close = xs_close, 2552a1311d87STrond Myklebust .destroy = xs_destroy, 2553176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2554d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2555d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2556176e21eeSChuck Lever }; 2557176e21eeSChuck Lever 2558262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 255943118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 256012a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 256149e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2562f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 256345160d62SChuck Lever .rpcbind = rpcb_getport_async, 256492200412SChuck Lever .set_port = xs_set_port, 25659903cd1cSChuck Lever .connect = xs_connect, 256602107148SChuck Lever .buf_alloc = rpc_malloc, 256702107148SChuck Lever .buf_free = rpc_free, 2568262965f5SChuck Lever .send_request = xs_udp_send_request, 2569fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 257046c0ee8bSChuck Lever .timer = xs_udp_timer, 2571a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2572262965f5SChuck Lever .close = xs_close, 2573262965f5SChuck Lever .destroy = xs_destroy, 2574262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2575d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2576d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 25774a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2578262965f5SChuck Lever }; 2579262965f5SChuck Lever 2580262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 258112a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 2582e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 2583f39c1bfbSTrond Myklebust .alloc_slot = xprt_lock_and_alloc_slot, 258445160d62SChuck Lever .rpcbind = rpcb_getport_async, 258592200412SChuck Lever .set_port = xs_set_port, 25860b9e7943STrond Myklebust .connect = xs_connect, 258702107148SChuck Lever .buf_alloc = rpc_malloc, 258802107148SChuck Lever .buf_free = rpc_free, 2589262965f5SChuck Lever .send_request = xs_tcp_send_request, 2590fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2591c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 25929903cd1cSChuck Lever .destroy = xs_destroy, 2593262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2594d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2595d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 25964a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2597a246b010SChuck Lever }; 2598a246b010SChuck Lever 25994cfc7e60SRahul Iyer /* 26004cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 26014cfc7e60SRahul Iyer */ 26024cfc7e60SRahul Iyer 26034cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = { 26044cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 26054cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 260684e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 26074cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 26084cfc7e60SRahul Iyer .buf_free = bc_free, 26094cfc7e60SRahul Iyer .send_request = bc_send_request, 26104cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 26114cfc7e60SRahul Iyer .close = bc_close, 26124cfc7e60SRahul Iyer .destroy = bc_destroy, 26134cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2614d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2615d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26164a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 26174cfc7e60SRahul Iyer }; 26184cfc7e60SRahul Iyer 261992476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 262092476850SChuck Lever { 262192476850SChuck Lever static const struct sockaddr_in sin = { 262292476850SChuck Lever .sin_family = AF_INET, 262392476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 262492476850SChuck Lever }; 262592476850SChuck Lever static const struct sockaddr_in6 sin6 = { 262692476850SChuck Lever .sin6_family = AF_INET6, 262792476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 262892476850SChuck Lever }; 262992476850SChuck Lever 263092476850SChuck Lever switch (family) { 2631176e21eeSChuck Lever case AF_LOCAL: 2632176e21eeSChuck Lever break; 263392476850SChuck Lever case AF_INET: 263492476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 263592476850SChuck Lever break; 263692476850SChuck Lever case AF_INET6: 263792476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 263892476850SChuck Lever break; 263992476850SChuck Lever default: 264092476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 264192476850SChuck Lever return -EAFNOSUPPORT; 264292476850SChuck Lever } 264392476850SChuck Lever return 0; 264492476850SChuck Lever } 264592476850SChuck Lever 26463c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2647d9ba131dSTrond Myklebust unsigned int slot_table_size, 2648d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2649c8541ecdSChuck Lever { 2650c8541ecdSChuck Lever struct rpc_xprt *xprt; 2651ffc2e518SChuck Lever struct sock_xprt *new; 2652c8541ecdSChuck Lever 265396802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2654c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2655c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2656c8541ecdSChuck Lever } 2657c8541ecdSChuck Lever 2658d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2659d9ba131dSTrond Myklebust max_slot_table_size); 2660bd1722d4SPavel Emelyanov if (xprt == NULL) { 266146121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 266246121cf7SChuck Lever "rpc_xprt\n"); 2663c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2664c8541ecdSChuck Lever } 2665c8541ecdSChuck Lever 2666bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 266796802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 266896802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2669d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2670fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 267192476850SChuck Lever else { 267292476850SChuck Lever int err; 267392476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 267492476850SChuck Lever (struct sockaddr *)&new->srcaddr); 26752aa13531SStanislav Kinsbursky if (err != 0) { 26762aa13531SStanislav Kinsbursky xprt_free(xprt); 267792476850SChuck Lever return ERR_PTR(err); 267892476850SChuck Lever } 26792aa13531SStanislav Kinsbursky } 2680c8541ecdSChuck Lever 2681c8541ecdSChuck Lever return xprt; 2682c8541ecdSChuck Lever } 2683c8541ecdSChuck Lever 2684176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2685176e21eeSChuck Lever .to_initval = 10 * HZ, 2686176e21eeSChuck Lever .to_maxval = 10 * HZ, 2687176e21eeSChuck Lever .to_retries = 2, 2688176e21eeSChuck Lever }; 2689176e21eeSChuck Lever 2690176e21eeSChuck Lever /** 2691176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2692176e21eeSChuck Lever * @args: rpc transport creation arguments 2693176e21eeSChuck Lever * 2694176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2695176e21eeSChuck Lever */ 2696176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2697176e21eeSChuck Lever { 2698176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2699176e21eeSChuck Lever struct sock_xprt *transport; 2700176e21eeSChuck Lever struct rpc_xprt *xprt; 2701176e21eeSChuck Lever struct rpc_xprt *ret; 2702176e21eeSChuck Lever 2703d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2704d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2705176e21eeSChuck Lever if (IS_ERR(xprt)) 2706176e21eeSChuck Lever return xprt; 2707176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2708176e21eeSChuck Lever 2709176e21eeSChuck Lever xprt->prot = 0; 2710176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2711176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2712176e21eeSChuck Lever 2713176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2714176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2715176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2716176e21eeSChuck Lever 2717176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2718176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2719176e21eeSChuck Lever 272093dc41bdSNeilBrown INIT_DELAYED_WORK(&transport->connect_worker, 272193dc41bdSNeilBrown xs_dummy_setup_socket); 272293dc41bdSNeilBrown 2723176e21eeSChuck Lever switch (sun->sun_family) { 2724176e21eeSChuck Lever case AF_LOCAL: 2725176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2726176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2727176e21eeSChuck Lever sun->sun_path); 2728176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2729176e21eeSChuck Lever goto out_err; 2730176e21eeSChuck Lever } 2731176e21eeSChuck Lever xprt_set_bound(xprt); 2732176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 27337073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 27347073ea87SJ. Bruce Fields if (ret) 27357073ea87SJ. Bruce Fields goto out_err; 2736176e21eeSChuck Lever break; 2737176e21eeSChuck Lever default: 2738176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2739176e21eeSChuck Lever goto out_err; 2740176e21eeSChuck Lever } 2741176e21eeSChuck Lever 2742176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2743176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2744176e21eeSChuck Lever 2745176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2746176e21eeSChuck Lever return xprt; 2747176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2748176e21eeSChuck Lever out_err: 2749315f3812SKinglong Mee xs_xprt_free(xprt); 2750176e21eeSChuck Lever return ret; 2751176e21eeSChuck Lever } 2752176e21eeSChuck Lever 27532881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 27542881ae74STrond Myklebust .to_initval = 5 * HZ, 27552881ae74STrond Myklebust .to_maxval = 30 * HZ, 27562881ae74STrond Myklebust .to_increment = 5 * HZ, 27572881ae74STrond Myklebust .to_retries = 5, 27582881ae74STrond Myklebust }; 27592881ae74STrond Myklebust 27609903cd1cSChuck Lever /** 27619903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 276296802a09SFrank van Maarseveen * @args: rpc transport creation arguments 27639903cd1cSChuck Lever * 27649903cd1cSChuck Lever */ 2765483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2766a246b010SChuck Lever { 27678f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2768c8541ecdSChuck Lever struct rpc_xprt *xprt; 2769c8475461SChuck Lever struct sock_xprt *transport; 27700a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2771a246b010SChuck Lever 2772d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2773d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2774c8541ecdSChuck Lever if (IS_ERR(xprt)) 2775c8541ecdSChuck Lever return xprt; 2776c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2777a246b010SChuck Lever 2778ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2779808012fbSChuck Lever xprt->tsh_size = 0; 2780a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2781a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2782a246b010SChuck Lever 278303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 278403bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 278503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2786a246b010SChuck Lever 2787262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2788a246b010SChuck Lever 2789ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2790a246b010SChuck Lever 27918f9d5b1aSChuck Lever switch (addr->sa_family) { 27928f9d5b1aSChuck Lever case AF_INET: 27938f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 27948f9d5b1aSChuck Lever xprt_set_bound(xprt); 27958f9d5b1aSChuck Lever 27968f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 27978c14ff2aSPavel Emelyanov xs_udp_setup_socket); 27989dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 27998f9d5b1aSChuck Lever break; 28008f9d5b1aSChuck Lever case AF_INET6: 28018f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 28028f9d5b1aSChuck Lever xprt_set_bound(xprt); 28038f9d5b1aSChuck Lever 28048f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 28058c14ff2aSPavel Emelyanov xs_udp_setup_socket); 28069dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 28078f9d5b1aSChuck Lever break; 28088f9d5b1aSChuck Lever default: 28090a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 28100a68b0beSJ. Bruce Fields goto out_err; 28118f9d5b1aSChuck Lever } 28128f9d5b1aSChuck Lever 2813c740eff8SChuck Lever if (xprt_bound(xprt)) 2814c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2815c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2816c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2817c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2818c740eff8SChuck Lever else 2819c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2820c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2821c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2822edb267a6SChuck Lever 2823bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2824c8541ecdSChuck Lever return xprt; 28250a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 28260a68b0beSJ. Bruce Fields out_err: 2827315f3812SKinglong Mee xs_xprt_free(xprt); 28280a68b0beSJ. Bruce Fields return ret; 2829a246b010SChuck Lever } 2830a246b010SChuck Lever 28312881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 28322881ae74STrond Myklebust .to_initval = 60 * HZ, 28332881ae74STrond Myklebust .to_maxval = 60 * HZ, 28342881ae74STrond Myklebust .to_retries = 2, 28352881ae74STrond Myklebust }; 28362881ae74STrond Myklebust 28379903cd1cSChuck Lever /** 28389903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 283996802a09SFrank van Maarseveen * @args: rpc transport creation arguments 28409903cd1cSChuck Lever * 28419903cd1cSChuck Lever */ 2842483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2843a246b010SChuck Lever { 28448f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2845c8541ecdSChuck Lever struct rpc_xprt *xprt; 2846c8475461SChuck Lever struct sock_xprt *transport; 28470a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2848b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2849b7993cebSTrond Myklebust 2850b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2851b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2852a246b010SChuck Lever 2853d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2854b7993cebSTrond Myklebust max_slot_table_size); 2855c8541ecdSChuck Lever if (IS_ERR(xprt)) 2856c8541ecdSChuck Lever return xprt; 2857c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2858a246b010SChuck Lever 2859ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2860808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2861808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2862a246b010SChuck Lever 286303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 286403bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 286503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2866a246b010SChuck Lever 2867262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2868ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2869a246b010SChuck Lever 28708f9d5b1aSChuck Lever switch (addr->sa_family) { 28718f9d5b1aSChuck Lever case AF_INET: 28728f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 28738f9d5b1aSChuck Lever xprt_set_bound(xprt); 28748f9d5b1aSChuck Lever 28759dc3b095SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 2876cdd518d5SPavel Emelyanov xs_tcp_setup_socket); 28779dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 28788f9d5b1aSChuck Lever break; 28798f9d5b1aSChuck Lever case AF_INET6: 28808f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 28818f9d5b1aSChuck Lever xprt_set_bound(xprt); 28828f9d5b1aSChuck Lever 28839dc3b095SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 2884cdd518d5SPavel Emelyanov xs_tcp_setup_socket); 28859dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 28868f9d5b1aSChuck Lever break; 28878f9d5b1aSChuck Lever default: 28880a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 28890a68b0beSJ. Bruce Fields goto out_err; 28908f9d5b1aSChuck Lever } 28918f9d5b1aSChuck Lever 2892c740eff8SChuck Lever if (xprt_bound(xprt)) 2893c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2894c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2895c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2896c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2897c740eff8SChuck Lever else 2898c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2899c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2900c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2901c740eff8SChuck Lever 2902bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2903c8541ecdSChuck Lever return xprt; 29040a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 29050a68b0beSJ. Bruce Fields out_err: 2906315f3812SKinglong Mee xs_xprt_free(xprt); 29070a68b0beSJ. Bruce Fields return ret; 2908a246b010SChuck Lever } 2909282b32e1SChuck Lever 2910f300babaSAlexandros Batsakis /** 2911f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 2912f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 2913f300babaSAlexandros Batsakis * 2914f300babaSAlexandros Batsakis */ 2915f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 2916f300babaSAlexandros Batsakis { 2917f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 2918f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 2919f300babaSAlexandros Batsakis struct sock_xprt *transport; 2920f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 29210a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2922f300babaSAlexandros Batsakis 2923d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2924d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 2925f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 2926f300babaSAlexandros Batsakis return xprt; 2927f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 2928f300babaSAlexandros Batsakis 2929f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 2930f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2931f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2932f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 2933f300babaSAlexandros Batsakis 2934f300babaSAlexandros Batsakis /* backchannel */ 2935f300babaSAlexandros Batsakis xprt_set_bound(xprt); 2936f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 2937f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 2938f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 2939f300babaSAlexandros Batsakis 2940f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 2941f300babaSAlexandros Batsakis 2942f300babaSAlexandros Batsakis switch (addr->sa_family) { 2943f300babaSAlexandros Batsakis case AF_INET: 2944f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 2945f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 2946f300babaSAlexandros Batsakis break; 2947f300babaSAlexandros Batsakis case AF_INET6: 2948f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 2949f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 2950f300babaSAlexandros Batsakis break; 2951f300babaSAlexandros Batsakis default: 29520a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 29530a68b0beSJ. Bruce Fields goto out_err; 2954f300babaSAlexandros Batsakis } 2955f300babaSAlexandros Batsakis 2956f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2957f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 2958f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 2959f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 2960f300babaSAlexandros Batsakis 2961f300babaSAlexandros Batsakis /* 296299de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 296328303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 296428303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 296528303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 296699de8ea9SJ. Bruce Fields */ 296799de8ea9SJ. Bruce Fields xprt_get(xprt); 296899de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 296999de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 297099de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 297199de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 297299de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 297399de8ea9SJ. Bruce Fields 297499de8ea9SJ. Bruce Fields /* 2975f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 2976f300babaSAlexandros Batsakis * the xprt status to connected 2977f300babaSAlexandros Batsakis */ 2978f300babaSAlexandros Batsakis xprt_set_connected(xprt); 2979f300babaSAlexandros Batsakis 2980f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 2981f300babaSAlexandros Batsakis return xprt; 2982642aab58SKinglong Mee 2983642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 298499de8ea9SJ. Bruce Fields xprt_put(xprt); 29850a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 29860a68b0beSJ. Bruce Fields out_err: 2987315f3812SKinglong Mee xs_xprt_free(xprt); 29880a68b0beSJ. Bruce Fields return ret; 2989f300babaSAlexandros Batsakis } 2990f300babaSAlexandros Batsakis 2991176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 2992176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 2993176e21eeSChuck Lever .name = "named UNIX socket", 2994176e21eeSChuck Lever .owner = THIS_MODULE, 2995176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 2996176e21eeSChuck Lever .setup = xs_setup_local, 2997176e21eeSChuck Lever }; 2998176e21eeSChuck Lever 2999bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3000bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3001bc25571eS\"Talpey, Thomas\ .name = "udp", 3002bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3003f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3004bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3005bc25571eS\"Talpey, Thomas\ }; 3006bc25571eS\"Talpey, Thomas\ 3007bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3008bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3009bc25571eS\"Talpey, Thomas\ .name = "tcp", 3010bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3011f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3012bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3013bc25571eS\"Talpey, Thomas\ }; 3014bc25571eS\"Talpey, Thomas\ 3015f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3016f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3017f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3018f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3019f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3020f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3021f300babaSAlexandros Batsakis }; 3022f300babaSAlexandros Batsakis 3023282b32e1SChuck Lever /** 3024bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3025282b32e1SChuck Lever * 3026282b32e1SChuck Lever */ 3027282b32e1SChuck Lever int init_socket_xprt(void) 3028282b32e1SChuck Lever { 3029f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 30302b1bec5fSEric W. Biederman if (!sunrpc_table_header) 30310b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3032fbf76683SChuck Lever #endif 3033fbf76683SChuck Lever 3034176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3035bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3036bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3037f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3038bc25571eS\"Talpey, Thomas\ 3039282b32e1SChuck Lever return 0; 3040282b32e1SChuck Lever } 3041282b32e1SChuck Lever 3042282b32e1SChuck Lever /** 3043bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3044282b32e1SChuck Lever * 3045282b32e1SChuck Lever */ 3046282b32e1SChuck Lever void cleanup_socket_xprt(void) 3047282b32e1SChuck Lever { 3048f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3049fbf76683SChuck Lever if (sunrpc_table_header) { 3050fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3051fbf76683SChuck Lever sunrpc_table_header = NULL; 3052fbf76683SChuck Lever } 3053fbf76683SChuck Lever #endif 3054bc25571eS\"Talpey, Thomas\ 3055176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3056bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3057bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3058f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3059282b32e1SChuck Lever } 3060cbf11071STrond Myklebust 30619bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 30629bbb9e5aSRusty Russell const struct kernel_param *kp, 3063cbf11071STrond Myklebust unsigned int min, unsigned int max) 3064cbf11071STrond Myklebust { 306500cfaa94SDaniel Walter unsigned int num; 3066cbf11071STrond Myklebust int ret; 3067cbf11071STrond Myklebust 3068cbf11071STrond Myklebust if (!val) 3069cbf11071STrond Myklebust return -EINVAL; 307000cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 3071cbf11071STrond Myklebust if (ret == -EINVAL || num < min || num > max) 3072cbf11071STrond Myklebust return -EINVAL; 3073cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3074cbf11071STrond Myklebust return 0; 3075cbf11071STrond Myklebust } 3076cbf11071STrond Myklebust 30779bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3078cbf11071STrond Myklebust { 3079cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3080cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3081cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3082cbf11071STrond Myklebust } 3083cbf11071STrond Myklebust 30849c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 30859bbb9e5aSRusty Russell .set = param_set_portnr, 30869bbb9e5aSRusty Russell .get = param_get_uint, 30879bbb9e5aSRusty Russell }; 30889bbb9e5aSRusty Russell 3089cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3090cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3091cbf11071STrond Myklebust 3092cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3093cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3094cbf11071STrond Myklebust 30959bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 30969bbb9e5aSRusty Russell const struct kernel_param *kp) 3097cbf11071STrond Myklebust { 3098cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3099cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3100cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3101cbf11071STrond Myklebust } 3102cbf11071STrond Myklebust 31039c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 31049bbb9e5aSRusty Russell .set = param_set_slot_table_size, 31059bbb9e5aSRusty Russell .get = param_get_uint, 31069bbb9e5aSRusty Russell }; 31079bbb9e5aSRusty Russell 3108cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3109cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3110cbf11071STrond Myklebust 3111d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3112d9ba131dSTrond Myklebust const struct kernel_param *kp) 3113d9ba131dSTrond Myklebust { 3114d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3115d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3116d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3117d9ba131dSTrond Myklebust } 3118d9ba131dSTrond Myklebust 31199c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3120d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3121d9ba131dSTrond Myklebust .get = param_get_uint, 3122d9ba131dSTrond Myklebust }; 3123d9ba131dSTrond Myklebust 3124d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3125d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3126d9ba131dSTrond Myklebust 3127cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3128cbf11071STrond Myklebust slot_table_size, 0644); 3129d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3130d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3131cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3132cbf11071STrond Myklebust slot_table_size, 0644); 3133cbf11071STrond Myklebust 3134