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; 36331303d6cSTrond Myklebust if (more) 36424c5684bSTrond Myklebust flags |= MSG_MORE; 36531303d6cSTrond Myklebust if (remainder != 0) 36631303d6cSTrond Myklebust flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE; 367a6b31d18STrond Myklebust err = do_sendpage(sock, *ppage, base, len, flags); 36824c5684bSTrond Myklebust if (remainder == 0 || err != len) 36924c5684bSTrond Myklebust break; 370f279cd00SJason Baron *sent_p += err; 37124c5684bSTrond Myklebust ppage++; 37224c5684bSTrond Myklebust base = 0; 37324c5684bSTrond Myklebust } 374f279cd00SJason Baron if (err > 0) { 375f279cd00SJason Baron *sent_p += err; 376f279cd00SJason Baron err = 0; 377f279cd00SJason Baron } 37824c5684bSTrond Myklebust return err; 379b4b5cc85SChuck Lever } 380b4b5cc85SChuck Lever 3819903cd1cSChuck Lever /** 3829903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 3839903cd1cSChuck Lever * @sock: socket to send on 3849903cd1cSChuck Lever * @addr: UDP only -- address of destination 3859903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 3869903cd1cSChuck Lever * @xdr: buffer containing this request 3879903cd1cSChuck Lever * @base: starting position in the buffer 388a6b31d18STrond Myklebust * @zerocopy: true if it is safe to use sendpage() 389f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 3909903cd1cSChuck Lever * 391a246b010SChuck Lever */ 392f279cd00SJason 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) 393a246b010SChuck Lever { 39424c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 395f279cd00SJason Baron int err = 0; 396f279cd00SJason Baron int sent = 0; 397a246b010SChuck Lever 398262965f5SChuck Lever if (unlikely(!sock)) 399fba91afbSTrond Myklebust return -ENOTSOCK; 400262965f5SChuck Lever 40124c5684bSTrond Myklebust if (base != 0) { 40224c5684bSTrond Myklebust addr = NULL; 40324c5684bSTrond Myklebust addrlen = 0; 40424c5684bSTrond Myklebust } 405262965f5SChuck Lever 40624c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 40724c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 40824c5684bSTrond Myklebust remainder -= len; 40924c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 41024c5684bSTrond Myklebust if (remainder == 0 || err != len) 411a246b010SChuck Lever goto out; 412f279cd00SJason Baron *sent_p += err; 413a246b010SChuck Lever base = 0; 414a246b010SChuck Lever } else 41524c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 416a246b010SChuck Lever 41724c5684bSTrond Myklebust if (base < xdr->page_len) { 41824c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 41924c5684bSTrond Myklebust remainder -= len; 420f279cd00SJason Baron err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); 421f279cd00SJason Baron *sent_p += sent; 422f279cd00SJason Baron if (remainder == 0 || sent != len) 423a246b010SChuck Lever goto out; 424a246b010SChuck Lever base = 0; 42524c5684bSTrond Myklebust } else 42624c5684bSTrond Myklebust base -= xdr->page_len; 42724c5684bSTrond Myklebust 42824c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 429f279cd00SJason Baron return 0; 43024c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 431a246b010SChuck Lever out: 432f279cd00SJason Baron if (err > 0) { 433f279cd00SJason Baron *sent_p += err; 434f279cd00SJason Baron err = 0; 435f279cd00SJason Baron } 43624c5684bSTrond Myklebust return err; 437a246b010SChuck Lever } 438a246b010SChuck Lever 439b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 440b6ddf64fSTrond Myklebust { 441b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 442b6ddf64fSTrond Myklebust 443b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 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)) { 46813331a55STrond Myklebust /* wait for more buffer space */ 46906ea0bfeSTrond Myklebust sk->sk_write_pending++; 470b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 47113331a55STrond Myklebust } else 4725e3771ceSTrond Myklebust ret = -ENOTCONN; 473a246b010SChuck Lever 474262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 47506ea0bfeSTrond Myklebust 47606ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 47706ea0bfeSTrond Myklebust sk->sk_write_space(sk); 4785e3771ceSTrond Myklebust return ret; 479a246b010SChuck Lever } 480a246b010SChuck Lever 48161677eeeSChuck Lever /* 48261677eeeSChuck Lever * Construct a stream transport record marker in @buf. 48361677eeeSChuck Lever */ 48461677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 48561677eeeSChuck Lever { 48661677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 48761677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 48861677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 48961677eeeSChuck Lever } 49061677eeeSChuck Lever 4919903cd1cSChuck Lever /** 492176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 493176e21eeSChuck Lever * @task: RPC task that manages the state of an RPC request 494176e21eeSChuck Lever * 495176e21eeSChuck Lever * Return values: 496176e21eeSChuck Lever * 0: The request has been sent 497176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 498176e21eeSChuck Lever * complete the request 499176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 500176e21eeSChuck Lever * other: Some other error occured, the request was not sent 501176e21eeSChuck Lever */ 502176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task) 503176e21eeSChuck Lever { 504176e21eeSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 505176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 506176e21eeSChuck Lever struct sock_xprt *transport = 507176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 508176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 509176e21eeSChuck Lever int status; 510f279cd00SJason Baron int sent = 0; 511176e21eeSChuck Lever 512176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 513176e21eeSChuck Lever 514176e21eeSChuck Lever xs_pktdump("packet data:", 515176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 516176e21eeSChuck Lever 517f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent, 518f279cd00SJason Baron true, &sent); 519176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 520176e21eeSChuck Lever __func__, xdr->len - req->rq_bytes_sent, status); 521743c69e7SNeilBrown 522743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 523743c69e7SNeilBrown status = -ENOBUFS; 524743c69e7SNeilBrown 525f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 526f279cd00SJason Baron req->rq_bytes_sent += sent; 527f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 528176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 529176e21eeSChuck Lever req->rq_bytes_sent = 0; 530176e21eeSChuck Lever return 0; 531176e21eeSChuck Lever } 532176e21eeSChuck Lever status = -EAGAIN; 533176e21eeSChuck Lever } 534176e21eeSChuck Lever 535176e21eeSChuck Lever switch (status) { 5363601c4a9STrond Myklebust case -ENOBUFS: 537b5872f0cSTrond Myklebust break; 538176e21eeSChuck Lever case -EAGAIN: 539176e21eeSChuck Lever status = xs_nospace(task); 540176e21eeSChuck Lever break; 541176e21eeSChuck Lever default: 542176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 543176e21eeSChuck Lever -status); 544176e21eeSChuck Lever case -EPIPE: 545176e21eeSChuck Lever xs_close(xprt); 546176e21eeSChuck Lever status = -ENOTCONN; 547176e21eeSChuck Lever } 548176e21eeSChuck Lever 549176e21eeSChuck Lever return status; 550176e21eeSChuck Lever } 551176e21eeSChuck Lever 552176e21eeSChuck Lever /** 553262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5549903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5559903cd1cSChuck Lever * 5569903cd1cSChuck Lever * Return values: 5579903cd1cSChuck Lever * 0: The request has been sent 5589903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5599903cd1cSChuck Lever * complete the request 560262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 56125985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 5629903cd1cSChuck Lever */ 563262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 564a246b010SChuck Lever { 565a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 566a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 567ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 568262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 569f279cd00SJason Baron int sent = 0; 570262965f5SChuck Lever int status; 571262965f5SChuck Lever 572262965f5SChuck Lever xs_pktdump("packet data:", 573262965f5SChuck Lever req->rq_svec->iov_base, 574262965f5SChuck Lever req->rq_svec->iov_len); 575262965f5SChuck Lever 57601d37c42STrond Myklebust if (!xprt_bound(xprt)) 57701d37c42STrond Myklebust return -ENOTCONN; 578f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 579f279cd00SJason Baron xdr, req->rq_bytes_sent, true, &sent); 580262965f5SChuck Lever 581262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 582262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 583262965f5SChuck Lever 5843dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 5853dedbb5cSJason Baron if (status == -EPERM) 5863dedbb5cSJason Baron goto process_status; 5873dedbb5cSJason Baron 588743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 589743c69e7SNeilBrown status = -ENOBUFS; 590743c69e7SNeilBrown 591f279cd00SJason Baron if (sent > 0 || status == 0) { 592f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 593f279cd00SJason Baron if (sent >= req->rq_slen) 594262965f5SChuck Lever return 0; 595262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 596262965f5SChuck Lever status = -EAGAIN; 5972199700fSTrond Myklebust } 598262965f5SChuck Lever 5993dedbb5cSJason Baron process_status: 600262965f5SChuck Lever switch (status) { 601fba91afbSTrond Myklebust case -ENOTSOCK: 602fba91afbSTrond Myklebust status = -ENOTCONN; 603fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 604fba91afbSTrond Myklebust break; 605b6ddf64fSTrond Myklebust case -EAGAIN: 6065e3771ceSTrond Myklebust status = xs_nospace(task); 607b6ddf64fSTrond Myklebust break; 608262965f5SChuck Lever case -ENETUNREACH: 6093601c4a9STrond Myklebust case -ENOBUFS: 610262965f5SChuck Lever case -EPIPE: 611262965f5SChuck Lever case -ECONNREFUSED: 6123dedbb5cSJason Baron case -EPERM: 613262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 614262965f5SChuck Lever * prompts ECONNREFUSED. */ 61513331a55STrond Myklebust break; 61613331a55STrond Myklebust default: 61713331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 61813331a55STrond Myklebust -status); 619262965f5SChuck Lever } 6205fe46e9dSBian Naimeng 621262965f5SChuck Lever return status; 622262965f5SChuck Lever } 623262965f5SChuck Lever 624e06799f9STrond Myklebust /** 625262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 626262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 627262965f5SChuck Lever * 628262965f5SChuck Lever * Return values: 629262965f5SChuck Lever * 0: The request has been sent 630262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 631262965f5SChuck Lever * complete the request 632262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 63325985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 634262965f5SChuck Lever * 635262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 636262965f5SChuck Lever * if sendmsg is not able to make progress? 637262965f5SChuck Lever */ 638262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 639262965f5SChuck Lever { 640262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 641262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 642ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 643262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 644a6b31d18STrond Myklebust bool zerocopy = true; 645b595bb15SChuck Lever int status; 646f279cd00SJason Baron int sent; 647a246b010SChuck Lever 64861677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 649262965f5SChuck Lever 650262965f5SChuck Lever xs_pktdump("packet data:", 651262965f5SChuck Lever req->rq_svec->iov_base, 652262965f5SChuck Lever req->rq_svec->iov_len); 653a6b31d18STrond Myklebust /* Don't use zero copy if this is a resend. If the RPC call 654a6b31d18STrond Myklebust * completes while the socket holds a reference to the pages, 655a6b31d18STrond Myklebust * then we may end up resending corrupted data. 656a6b31d18STrond Myklebust */ 657a6b31d18STrond Myklebust if (task->tk_flags & RPC_TASK_SENT) 658a6b31d18STrond Myklebust zerocopy = false; 659a246b010SChuck Lever 660a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 661a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 662262965f5SChuck Lever * called sendmsg(). */ 663a246b010SChuck Lever while (1) { 664f279cd00SJason Baron sent = 0; 665f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 666f279cd00SJason Baron req->rq_bytes_sent, zerocopy, &sent); 667a246b010SChuck Lever 668262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 669262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 670262965f5SChuck Lever 671a246b010SChuck Lever /* If we've sent the entire packet, immediately 672a246b010SChuck Lever * reset the count of bytes sent. */ 673f279cd00SJason Baron req->rq_bytes_sent += sent; 674f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 675262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 676a246b010SChuck Lever req->rq_bytes_sent = 0; 677a246b010SChuck Lever return 0; 678a246b010SChuck Lever } 679262965f5SChuck Lever 680f580dd04STrond Myklebust if (status < 0) 681f580dd04STrond Myklebust break; 682f580dd04STrond Myklebust if (sent == 0) { 683a246b010SChuck Lever status = -EAGAIN; 684a246b010SChuck Lever break; 685a246b010SChuck Lever } 686f580dd04STrond Myklebust } 687743c69e7SNeilBrown if (status == -EAGAIN && sk_stream_is_writeable(transport->inet)) 688743c69e7SNeilBrown status = -ENOBUFS; 689a246b010SChuck Lever 690262965f5SChuck Lever switch (status) { 691fba91afbSTrond Myklebust case -ENOTSOCK: 692fba91afbSTrond Myklebust status = -ENOTCONN; 693fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 694fba91afbSTrond Myklebust break; 695262965f5SChuck Lever case -EAGAIN: 6965e3771ceSTrond Myklebust status = xs_nospace(task); 697262965f5SChuck Lever break; 698262965f5SChuck Lever case -ECONNRESET: 699262965f5SChuck Lever case -ECONNREFUSED: 700262965f5SChuck Lever case -ENOTCONN: 7013913c78cSTrond Myklebust case -EADDRINUSE: 702b5872f0cSTrond Myklebust case -ENOBUFS: 703b9d2bb2eSTrond Myklebust case -EPIPE: 70413331a55STrond Myklebust break; 70513331a55STrond Myklebust default: 70613331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 70713331a55STrond Myklebust -status); 708a246b010SChuck Lever } 7095fe46e9dSBian Naimeng 710a246b010SChuck Lever return status; 711a246b010SChuck Lever } 712a246b010SChuck Lever 7139903cd1cSChuck Lever /** 714e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 715e0ab53deSTrond Myklebust * @xprt: transport 716e0ab53deSTrond Myklebust * @task: rpc task 717e0ab53deSTrond Myklebust * 718e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 719e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 720e0ab53deSTrond Myklebust * the server. 721e0ab53deSTrond Myklebust */ 722e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 723e0ab53deSTrond Myklebust { 724e0ab53deSTrond Myklebust struct rpc_rqst *req; 725e0ab53deSTrond Myklebust 726e0ab53deSTrond Myklebust if (task != xprt->snd_task) 727e0ab53deSTrond Myklebust return; 728e0ab53deSTrond Myklebust if (task == NULL) 729e0ab53deSTrond Myklebust goto out_release; 730e0ab53deSTrond Myklebust req = task->tk_rqstp; 73143cedbf0STrond Myklebust if (req == NULL) 73243cedbf0STrond Myklebust goto out_release; 733e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 734e0ab53deSTrond Myklebust goto out_release; 735e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 736e0ab53deSTrond Myklebust goto out_release; 737a4f0835cSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 738e0ab53deSTrond Myklebust out_release: 739e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 740e0ab53deSTrond Myklebust } 741e0ab53deSTrond Myklebust 7422a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7432a9e1cfaSTrond Myklebust { 7442a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7452a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7462a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7472118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7482a9e1cfaSTrond Myklebust } 7492a9e1cfaSTrond Myklebust 7502a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7512a9e1cfaSTrond Myklebust { 7522a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7532a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7542a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7552118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7562118071dSTrond Myklebust } 7572118071dSTrond Myklebust 758b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 759b70ae915STrond Myklebust { 760b70ae915STrond Myklebust smp_mb__before_atomic(); 761b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 762b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 763b70ae915STrond Myklebust smp_mb__after_atomic(); 764b70ae915STrond Myklebust } 765b70ae915STrond Myklebust 766b70ae915STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 767b70ae915STrond Myklebust { 768b70ae915STrond Myklebust xs_sock_reset_connection_flags(xprt); 769b70ae915STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 770b70ae915STrond Myklebust xprt_disconnect_done(xprt); 771b70ae915STrond Myklebust } 772b70ae915STrond Myklebust 7732118071dSTrond Myklebust /** 7742118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 7752118071dSTrond Myklebust * @sk: socket 7762118071dSTrond Myklebust * 7772118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 7782118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 7792118071dSTrond Myklebust */ 7802118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 7812118071dSTrond Myklebust { 7822118071dSTrond Myklebust struct rpc_xprt *xprt; 7832118071dSTrond Myklebust int err; 7842118071dSTrond Myklebust 7852118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 7862118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 7872118071dSTrond Myklebust goto out; 7882118071dSTrond Myklebust 7892118071dSTrond Myklebust err = -sk->sk_err; 7902118071dSTrond Myklebust if (err == 0) 7912118071dSTrond Myklebust goto out; 792b70ae915STrond Myklebust /* Is this a reset event? */ 793b70ae915STrond Myklebust if (sk->sk_state == TCP_CLOSE) 794b70ae915STrond Myklebust xs_sock_mark_closed(xprt); 7952118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 7962118071dSTrond Myklebust xprt, -err); 797e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 7982118071dSTrond Myklebust xprt_wake_pending_tasks(xprt, err); 7992118071dSTrond Myklebust out: 8002118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 8012a9e1cfaSTrond Myklebust } 8022a9e1cfaSTrond Myklebust 803fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 804a246b010SChuck Lever { 805ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 806ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 8076cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 808a246b010SChuck Lever 809fe315e76SChuck Lever if (sk == NULL) 810fe315e76SChuck Lever return; 8119903cd1cSChuck Lever 812264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 813264d1df3SJeff Layton sk_clear_memalloc(sk); 814264d1df3SJeff Layton 81509939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 81609939204STrond Myklebust 817edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 818a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 819ee0ac0c2SChuck Lever transport->inet = NULL; 820ee0ac0c2SChuck Lever transport->sock = NULL; 821a246b010SChuck Lever 822a246b010SChuck Lever sk->sk_user_data = NULL; 8232a9e1cfaSTrond Myklebust 8242a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 8250c78789eSTrond Myklebust xprt_clear_connected(xprt); 826a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 8276cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 828edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 829a246b010SChuck Lever 8306cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 831a246b010SChuck Lever sock_release(sock); 832fe315e76SChuck Lever } 833fe315e76SChuck Lever 834fe315e76SChuck Lever /** 835fe315e76SChuck Lever * xs_close - close a socket 836fe315e76SChuck Lever * @xprt: transport 837fe315e76SChuck Lever * 838fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 839fe315e76SChuck Lever * on the server we want to save. 840f75e6745STrond Myklebust * 841f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 842f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 843fe315e76SChuck Lever */ 844fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 845fe315e76SChuck Lever { 846fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 847fe315e76SChuck Lever 848fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 849fe315e76SChuck Lever 850fe315e76SChuck Lever xs_reset_transport(transport); 85161d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 852fe315e76SChuck Lever 85362da3b24STrond Myklebust xprt_disconnect_done(xprt); 854a246b010SChuck Lever } 855a246b010SChuck Lever 8564a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 8574a068258SChuck Lever { 8584a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 8594a068258SChuck Lever xprt); 8604a068258SChuck Lever xprt_disconnect_done(xprt); 8614a068258SChuck Lever } 8624a068258SChuck Lever 863315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 864315f3812SKinglong Mee { 865315f3812SKinglong Mee xs_free_peer_addresses(xprt); 866315f3812SKinglong Mee xprt_free(xprt); 867315f3812SKinglong Mee } 868315f3812SKinglong Mee 8699903cd1cSChuck Lever /** 8709903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8719903cd1cSChuck Lever * @xprt: doomed transport 8729903cd1cSChuck Lever * 8739903cd1cSChuck Lever */ 8749903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 875a246b010SChuck Lever { 87603c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 87703c78827STrond Myklebust struct sock_xprt, xprt); 8789903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8799903cd1cSChuck Lever 88003c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 881a1311d87STrond Myklebust xs_close(xprt); 882edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 883315f3812SKinglong Mee xs_xprt_free(xprt); 884a1311d87STrond Myklebust module_put(THIS_MODULE); 885a246b010SChuck Lever } 886a246b010SChuck Lever 887176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) 888176e21eeSChuck Lever { 889176e21eeSChuck Lever struct xdr_skb_reader desc = { 890176e21eeSChuck Lever .skb = skb, 891176e21eeSChuck Lever .offset = sizeof(rpc_fraghdr), 892176e21eeSChuck Lever .count = skb->len - sizeof(rpc_fraghdr), 893176e21eeSChuck Lever }; 894176e21eeSChuck Lever 895176e21eeSChuck Lever if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) 896176e21eeSChuck Lever return -1; 897176e21eeSChuck Lever if (desc.count) 898176e21eeSChuck Lever return -1; 899176e21eeSChuck Lever return 0; 900176e21eeSChuck Lever } 901176e21eeSChuck Lever 902176e21eeSChuck Lever /** 903a2648094STrond Myklebust * xs_local_data_read_skb 904a2648094STrond Myklebust * @xprt: transport 905a2648094STrond Myklebust * @sk: socket 906a2648094STrond Myklebust * @skb: skbuff 907176e21eeSChuck Lever * 908176e21eeSChuck Lever * Currently this assumes we can read the whole reply in a single gulp. 909176e21eeSChuck Lever */ 910a2648094STrond Myklebust static void xs_local_data_read_skb(struct rpc_xprt *xprt, 911a2648094STrond Myklebust struct sock *sk, 912a2648094STrond Myklebust struct sk_buff *skb) 913176e21eeSChuck Lever { 914176e21eeSChuck Lever struct rpc_task *task; 915176e21eeSChuck Lever struct rpc_rqst *rovr; 916a2648094STrond Myklebust int repsize, copied; 917176e21eeSChuck Lever u32 _xid; 918176e21eeSChuck Lever __be32 *xp; 919176e21eeSChuck Lever 920176e21eeSChuck Lever repsize = skb->len - sizeof(rpc_fraghdr); 921176e21eeSChuck Lever if (repsize < 4) { 922176e21eeSChuck Lever dprintk("RPC: impossible RPC reply size %d\n", repsize); 923a2648094STrond Myklebust return; 924176e21eeSChuck Lever } 925176e21eeSChuck Lever 926176e21eeSChuck Lever /* Copy the XID from the skb... */ 927176e21eeSChuck Lever xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); 928176e21eeSChuck Lever if (xp == NULL) 929a2648094STrond Myklebust return; 930176e21eeSChuck Lever 931176e21eeSChuck Lever /* Look up and lock the request corresponding to the given XID */ 932a2648094STrond Myklebust spin_lock_bh(&xprt->transport_lock); 933176e21eeSChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 934176e21eeSChuck Lever if (!rovr) 935176e21eeSChuck Lever goto out_unlock; 936176e21eeSChuck Lever task = rovr->rq_task; 937176e21eeSChuck Lever 938176e21eeSChuck Lever copied = rovr->rq_private_buf.buflen; 939176e21eeSChuck Lever if (copied > repsize) 940176e21eeSChuck Lever copied = repsize; 941176e21eeSChuck Lever 942176e21eeSChuck Lever if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { 943176e21eeSChuck Lever dprintk("RPC: sk_buff copy failed\n"); 944176e21eeSChuck Lever goto out_unlock; 945176e21eeSChuck Lever } 946176e21eeSChuck Lever 947176e21eeSChuck Lever xprt_complete_rqst(task, copied); 948176e21eeSChuck Lever 949176e21eeSChuck Lever out_unlock: 950a2648094STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 951a2648094STrond Myklebust } 952a2648094STrond Myklebust 953a2648094STrond Myklebust static void xs_local_data_receive(struct sock_xprt *transport) 954a2648094STrond Myklebust { 955a2648094STrond Myklebust struct sk_buff *skb; 956a2648094STrond Myklebust struct sock *sk; 957a2648094STrond Myklebust int err; 958a2648094STrond Myklebust 959a2648094STrond Myklebust mutex_lock(&transport->recv_mutex); 960a2648094STrond Myklebust sk = transport->inet; 961a2648094STrond Myklebust if (sk == NULL) 962a2648094STrond Myklebust goto out; 963a2648094STrond Myklebust for (;;) { 964a2648094STrond Myklebust skb = skb_recv_datagram(sk, 0, 1, &err); 965a2648094STrond Myklebust if (skb == NULL) 966a2648094STrond Myklebust break; 967a2648094STrond Myklebust xs_local_data_read_skb(&transport->xprt, sk, skb); 968176e21eeSChuck Lever skb_free_datagram(sk, skb); 969a2648094STrond Myklebust } 970176e21eeSChuck Lever out: 971a2648094STrond Myklebust mutex_unlock(&transport->recv_mutex); 972a2648094STrond Myklebust } 973a2648094STrond Myklebust 974a2648094STrond Myklebust static void xs_local_data_receive_workfn(struct work_struct *work) 975a2648094STrond Myklebust { 976a2648094STrond Myklebust struct sock_xprt *transport = 977a2648094STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 978a2648094STrond Myklebust xs_local_data_receive(transport); 979176e21eeSChuck Lever } 980176e21eeSChuck Lever 9819903cd1cSChuck Lever /** 982f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 983f9b2ee71STrond Myklebust * @xprt: transport 984f9b2ee71STrond Myklebust * @sk: socket 985f9b2ee71STrond Myklebust * @skb: skbuff 9869903cd1cSChuck Lever * 987a246b010SChuck Lever */ 988f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 989f9b2ee71STrond Myklebust struct sock *sk, 990f9b2ee71STrond Myklebust struct sk_buff *skb) 991a246b010SChuck Lever { 992a246b010SChuck Lever struct rpc_task *task; 993a246b010SChuck Lever struct rpc_rqst *rovr; 994f9b2ee71STrond Myklebust int repsize, copied; 995d8ed029dSAlexey Dobriyan u32 _xid; 996d8ed029dSAlexey Dobriyan __be32 *xp; 997a246b010SChuck Lever 9981da8c681SWillem de Bruijn repsize = skb->len; 999a246b010SChuck Lever if (repsize < 4) { 10009903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1001f9b2ee71STrond Myklebust return; 1002a246b010SChuck Lever } 1003a246b010SChuck Lever 1004a246b010SChuck Lever /* Copy the XID from the skb... */ 10051da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1006a246b010SChuck Lever if (xp == NULL) 1007f9b2ee71STrond Myklebust return; 1008a246b010SChuck Lever 1009a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 1010f9b2ee71STrond Myklebust spin_lock_bh(&xprt->transport_lock); 1011a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1012a246b010SChuck Lever if (!rovr) 1013a246b010SChuck Lever goto out_unlock; 1014a246b010SChuck Lever task = rovr->rq_task; 1015a246b010SChuck Lever 1016a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1017a246b010SChuck Lever copied = repsize; 1018a246b010SChuck Lever 1019a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 10201781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 10211781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 1022a246b010SChuck Lever goto out_unlock; 10231781f7f5SHerbert Xu } 10241781f7f5SHerbert Xu 10251781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 1026a246b010SChuck Lever 10276a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 10281570c1e4SChuck Lever xprt_complete_rqst(task, copied); 1029a246b010SChuck Lever 1030a246b010SChuck Lever out_unlock: 1031f9b2ee71STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 1032f9b2ee71STrond Myklebust } 1033f9b2ee71STrond Myklebust 1034f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1035f9b2ee71STrond Myklebust { 1036f9b2ee71STrond Myklebust struct sk_buff *skb; 1037f9b2ee71STrond Myklebust struct sock *sk; 1038f9b2ee71STrond Myklebust int err; 1039f9b2ee71STrond Myklebust 1040f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1041f9b2ee71STrond Myklebust sk = transport->inet; 1042f9b2ee71STrond Myklebust if (sk == NULL) 1043f9b2ee71STrond Myklebust goto out; 1044f9b2ee71STrond Myklebust for (;;) { 1045f9b2ee71STrond Myklebust skb = skb_recv_datagram(sk, 0, 1, &err); 1046f9b2ee71STrond Myklebust if (skb == NULL) 1047f9b2ee71STrond Myklebust break; 1048f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1049a246b010SChuck Lever skb_free_datagram(sk, skb); 1050f9b2ee71STrond Myklebust } 1051a246b010SChuck Lever out: 1052f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1053f9b2ee71STrond Myklebust } 1054f9b2ee71STrond Myklebust 1055f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1056f9b2ee71STrond Myklebust { 1057f9b2ee71STrond Myklebust struct sock_xprt *transport = 1058f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1059f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1060f9b2ee71STrond Myklebust } 1061f9b2ee71STrond Myklebust 1062f9b2ee71STrond Myklebust /** 1063f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1064f9b2ee71STrond Myklebust * @sk: socket with data to read 1065f9b2ee71STrond Myklebust * 1066f9b2ee71STrond Myklebust */ 1067f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1068f9b2ee71STrond Myklebust { 1069f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1070f9b2ee71STrond Myklebust 1071f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1072f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1073f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1074f9b2ee71STrond Myklebust if (xprt != NULL) { 1075f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1076f9b2ee71STrond Myklebust struct sock_xprt, xprt); 1077f9b2ee71STrond Myklebust queue_work(rpciod_workqueue, &transport->recv_worker); 1078f9b2ee71STrond Myklebust } 1079f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1080a246b010SChuck Lever } 1081a246b010SChuck Lever 1082a519fc7aSTrond Myklebust /* 1083a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1084a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1085a519fc7aSTrond Myklebust */ 1086a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1087a519fc7aSTrond Myklebust { 1088a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1089a519fc7aSTrond Myklebust } 1090a519fc7aSTrond Myklebust 1091dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1092a246b010SChuck Lever { 109351971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1094a246b010SChuck Lever size_t len, used; 1095a246b010SChuck Lever char *p; 1096a246b010SChuck Lever 109751971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 109851971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 10999d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 110051971139SChuck Lever transport->tcp_offset += used; 1101a246b010SChuck Lever if (used != len) 1102a246b010SChuck Lever return; 1103808012fbSChuck Lever 110451971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 110551971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 1106e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 1107a246b010SChuck Lever else 1108e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 110951971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 1110808012fbSChuck Lever 1111e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 111251971139SChuck Lever transport->tcp_offset = 0; 1113808012fbSChuck Lever 1114a246b010SChuck Lever /* Sanity check of the record length */ 111518dca02aSRicardo Labiaga if (unlikely(transport->tcp_reclen < 8)) { 11169903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 1117a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 11189903cd1cSChuck Lever return; 1119a246b010SChuck Lever } 1120a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 112151971139SChuck Lever transport->tcp_reclen); 1122a246b010SChuck Lever } 1123a246b010SChuck Lever 112451971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 1125a246b010SChuck Lever { 112651971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 1127e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 112851971139SChuck Lever transport->tcp_offset = 0; 1129e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 1130e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1131e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 113251971139SChuck Lever transport->tcp_copied = 0; 1133a246b010SChuck Lever } 1134a246b010SChuck Lever } 1135a246b010SChuck Lever } 1136a246b010SChuck Lever 1137dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1138a246b010SChuck Lever { 1139a246b010SChuck Lever size_t len, used; 1140a246b010SChuck Lever char *p; 1141a246b010SChuck Lever 114251971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 1143a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 114451971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 11459d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 114651971139SChuck Lever transport->tcp_offset += used; 1147a246b010SChuck Lever if (used != len) 1148a246b010SChuck Lever return; 1149e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 1150f4a2e418SRicardo Labiaga transport->tcp_flags |= TCP_RCV_READ_CALLDIR; 115151971139SChuck Lever transport->tcp_copied = 4; 115218dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 115318dca02aSRicardo Labiaga (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" 115418dca02aSRicardo Labiaga : "request with", 115551971139SChuck Lever ntohl(transport->tcp_xid)); 115651971139SChuck Lever xs_tcp_check_fraghdr(transport); 1157a246b010SChuck Lever } 1158a246b010SChuck Lever 115918dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 116018dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1161a246b010SChuck Lever { 116218dca02aSRicardo Labiaga size_t len, used; 116318dca02aSRicardo Labiaga u32 offset; 1164b76ce561STrond Myklebust char *p; 116518dca02aSRicardo Labiaga 116618dca02aSRicardo Labiaga /* 116718dca02aSRicardo Labiaga * We want transport->tcp_offset to be 8 at the end of this routine 116818dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 116918dca02aSRicardo Labiaga * When this function is called for the first time, 117018dca02aSRicardo Labiaga * transport->tcp_offset is 4 (after having already read the xid). 117118dca02aSRicardo Labiaga */ 117218dca02aSRicardo Labiaga offset = transport->tcp_offset - sizeof(transport->tcp_xid); 1173b76ce561STrond Myklebust len = sizeof(transport->tcp_calldir) - offset; 117418dca02aSRicardo Labiaga dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); 1175b76ce561STrond Myklebust p = ((char *) &transport->tcp_calldir) + offset; 1176b76ce561STrond Myklebust used = xdr_skb_read_bits(desc, p, len); 117718dca02aSRicardo Labiaga transport->tcp_offset += used; 117818dca02aSRicardo Labiaga if (used != len) 117918dca02aSRicardo Labiaga return; 1180f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; 1181f4a2e418SRicardo Labiaga /* 1182f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1183f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1184f4a2e418SRicardo Labiaga */ 1185b76ce561STrond Myklebust switch (ntohl(transport->tcp_calldir)) { 1186b76ce561STrond Myklebust case RPC_REPLY: 1187b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1188b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 118918dca02aSRicardo Labiaga transport->tcp_flags |= TCP_RPC_REPLY; 1190b76ce561STrond Myklebust break; 1191b76ce561STrond Myklebust case RPC_CALL: 1192b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1193b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 119418dca02aSRicardo Labiaga transport->tcp_flags &= ~TCP_RPC_REPLY; 1195b76ce561STrond Myklebust break; 1196b76ce561STrond Myklebust default: 1197b76ce561STrond Myklebust dprintk("RPC: invalid request message type\n"); 1198a519fc7aSTrond Myklebust xs_tcp_force_close(&transport->xprt); 1199b76ce561STrond Myklebust } 120018dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 120118dca02aSRicardo Labiaga } 120218dca02aSRicardo Labiaga 120344b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 120444b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 120544b98efdSRicardo Labiaga struct rpc_rqst *req) 1206a246b010SChuck Lever { 120744b98efdSRicardo Labiaga struct sock_xprt *transport = 120844b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1209a246b010SChuck Lever struct xdr_buf *rcvbuf; 1210a246b010SChuck Lever size_t len; 1211a246b010SChuck Lever ssize_t r; 1212a246b010SChuck Lever 1213a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1214f4a2e418SRicardo Labiaga 1215f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { 1216f4a2e418SRicardo Labiaga /* 1217f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1218f4a2e418SRicardo Labiaga */ 1219f4a2e418SRicardo Labiaga memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, 1220b76ce561STrond Myklebust &transport->tcp_calldir, 1221b76ce561STrond Myklebust sizeof(transport->tcp_calldir)); 1222b76ce561STrond Myklebust transport->tcp_copied += sizeof(transport->tcp_calldir); 1223f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; 1224a246b010SChuck Lever } 1225a246b010SChuck Lever 1226a246b010SChuck Lever len = desc->count; 122751971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1228dd456471SChuck Lever struct xdr_skb_reader my_desc; 1229a246b010SChuck Lever 123051971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1231a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1232a246b010SChuck Lever my_desc.count = len; 123351971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 12349d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1235a246b010SChuck Lever desc->count -= r; 1236a246b010SChuck Lever desc->offset += r; 1237a246b010SChuck Lever } else 123851971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 12399d292316SChuck Lever desc, xdr_skb_read_bits); 1240a246b010SChuck Lever 1241a246b010SChuck Lever if (r > 0) { 124251971139SChuck Lever transport->tcp_copied += r; 124351971139SChuck Lever transport->tcp_offset += r; 1244a246b010SChuck Lever } 1245a246b010SChuck Lever if (r != len) { 1246a246b010SChuck Lever /* Error when copying to the receive buffer, 1247a246b010SChuck Lever * usually because we weren't able to allocate 1248a246b010SChuck Lever * additional buffer pages. All we can do now 1249e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1250a246b010SChuck Lever * will not receive any additional updates, 1251a246b010SChuck Lever * and time out. 1252a246b010SChuck Lever * Any remaining data from this record will 1253a246b010SChuck Lever * be discarded. 1254a246b010SChuck Lever */ 1255e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1256a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 125751971139SChuck Lever ntohl(transport->tcp_xid)); 125846121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 125946121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 126046121cf7SChuck Lever xprt, transport->tcp_copied, 126146121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 126244b98efdSRicardo Labiaga return; 1263a246b010SChuck Lever } 1264a246b010SChuck Lever 1265a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 126651971139SChuck Lever ntohl(transport->tcp_xid), r); 126746121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 126846121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 126946121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1270a246b010SChuck Lever 127151971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1272e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 127351971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1274e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1275e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1276a246b010SChuck Lever } 127744b98efdSRicardo Labiaga } 127844b98efdSRicardo Labiaga 127944b98efdSRicardo Labiaga /* 128044b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 128144b98efdSRicardo Labiaga * tcp read code to read the data. 128244b98efdSRicardo Labiaga */ 128344b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 128444b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 128544b98efdSRicardo Labiaga { 128644b98efdSRicardo Labiaga struct sock_xprt *transport = 128744b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 128844b98efdSRicardo Labiaga struct rpc_rqst *req; 128944b98efdSRicardo Labiaga 129044b98efdSRicardo Labiaga dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); 129144b98efdSRicardo Labiaga 129244b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 1293edc1b01cSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 129444b98efdSRicardo Labiaga req = xprt_lookup_rqst(xprt, transport->tcp_xid); 129544b98efdSRicardo Labiaga if (!req) { 129644b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 129744b98efdSRicardo Labiaga ntohl(transport->tcp_xid)); 1298edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 129944b98efdSRicardo Labiaga return -1; 130044b98efdSRicardo Labiaga } 130144b98efdSRicardo Labiaga 130244b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 130344b98efdSRicardo Labiaga 1304e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 130551971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 130644b98efdSRicardo Labiaga 1307edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 130844b98efdSRicardo Labiaga return 0; 130944b98efdSRicardo Labiaga } 131044b98efdSRicardo Labiaga 13119e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 131244b98efdSRicardo Labiaga /* 131344b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 131444b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 131544b98efdSRicardo Labiaga * queue. 131644b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 131744b98efdSRicardo Labiaga * connection and return -1. 131844b98efdSRicardo Labiaga */ 13192ea24497STrond Myklebust static int xs_tcp_read_callback(struct rpc_xprt *xprt, 132044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 132144b98efdSRicardo Labiaga { 132244b98efdSRicardo Labiaga struct sock_xprt *transport = 132344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 132444b98efdSRicardo Labiaga struct rpc_rqst *req; 132544b98efdSRicardo Labiaga 13262ea24497STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 1327edc1b01cSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 13282ea24497STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->tcp_xid); 132944b98efdSRicardo Labiaga if (req == NULL) { 1330edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 133144b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 133244b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 133344b98efdSRicardo Labiaga return -1; 133444b98efdSRicardo Labiaga } 133544b98efdSRicardo Labiaga 133644b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 133744b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 133844b98efdSRicardo Labiaga 13392ea24497STrond Myklebust if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 13402ea24497STrond Myklebust xprt_complete_bc_request(req, transport->tcp_copied); 1341edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 134244b98efdSRicardo Labiaga 134344b98efdSRicardo Labiaga return 0; 134444b98efdSRicardo Labiaga } 134544b98efdSRicardo Labiaga 134644b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 134744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 134844b98efdSRicardo Labiaga { 134944b98efdSRicardo Labiaga struct sock_xprt *transport = 135044b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 135144b98efdSRicardo Labiaga 135244b98efdSRicardo Labiaga return (transport->tcp_flags & TCP_RPC_REPLY) ? 135344b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 135444b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 135544b98efdSRicardo Labiaga } 135676566773SChuck Lever 135776566773SChuck Lever static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net) 135876566773SChuck Lever { 135976566773SChuck Lever int ret; 136076566773SChuck Lever 136176566773SChuck Lever ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0, 136276566773SChuck Lever SVC_SOCK_ANONYMOUS); 136376566773SChuck Lever if (ret < 0) 136476566773SChuck Lever return ret; 136576566773SChuck Lever return 0; 136676566773SChuck Lever } 136744b98efdSRicardo Labiaga #else 136844b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 136944b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 137044b98efdSRicardo Labiaga { 137144b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 137244b98efdSRicardo Labiaga } 13739e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 137444b98efdSRicardo Labiaga 137544b98efdSRicardo Labiaga /* 137644b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 137744b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 137844b98efdSRicardo Labiaga */ 137944b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 138044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 138144b98efdSRicardo Labiaga { 138244b98efdSRicardo Labiaga struct sock_xprt *transport = 138344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 138444b98efdSRicardo Labiaga 138544b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 138651971139SChuck Lever xs_tcp_check_fraghdr(transport); 138744b98efdSRicardo Labiaga else { 138844b98efdSRicardo Labiaga /* 138944b98efdSRicardo Labiaga * The transport_lock protects the request handling. 139044b98efdSRicardo Labiaga * There's no need to hold it to update the tcp_flags. 139144b98efdSRicardo Labiaga */ 139244b98efdSRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 139344b98efdSRicardo Labiaga } 1394a246b010SChuck Lever } 1395a246b010SChuck Lever 1396dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1397a246b010SChuck Lever { 1398a246b010SChuck Lever size_t len; 1399a246b010SChuck Lever 140051971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1401a246b010SChuck Lever if (len > desc->count) 1402a246b010SChuck Lever len = desc->count; 1403a246b010SChuck Lever desc->count -= len; 1404a246b010SChuck Lever desc->offset += len; 140551971139SChuck Lever transport->tcp_offset += len; 1406a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 140751971139SChuck Lever xs_tcp_check_fraghdr(transport); 1408a246b010SChuck Lever } 1409a246b010SChuck Lever 14109903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1411a246b010SChuck Lever { 1412a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 141351971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1414dd456471SChuck Lever struct xdr_skb_reader desc = { 1415a246b010SChuck Lever .skb = skb, 1416a246b010SChuck Lever .offset = offset, 1417a246b010SChuck Lever .count = len, 1418a246b010SChuck Lever }; 1419a246b010SChuck Lever 14209903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1421a246b010SChuck Lever do { 14221a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 1423a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1424a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1425e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 14269903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1427a246b010SChuck Lever continue; 1428a246b010SChuck Lever } 1429a246b010SChuck Lever /* Read in the xid if necessary */ 1430e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 143151971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1432a246b010SChuck Lever continue; 1433a246b010SChuck Lever } 143418dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1435f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { 143618dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 143718dca02aSRicardo Labiaga continue; 143818dca02aSRicardo Labiaga } 1439a246b010SChuck Lever /* Read in the request data */ 1440e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 144144b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1442a246b010SChuck Lever continue; 1443a246b010SChuck Lever } 1444a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 144551971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1446a246b010SChuck Lever } while (desc.count); 14471a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 14489903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1449a246b010SChuck Lever return len - desc.count; 1450a246b010SChuck Lever } 1451a246b010SChuck Lever 145266d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport) 145366d7a56aSTrond Myklebust { 145466d7a56aSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 145566d7a56aSTrond Myklebust struct sock *sk; 145666d7a56aSTrond Myklebust read_descriptor_t rd_desc = { 145766d7a56aSTrond Myklebust .count = 2*1024*1024, 145866d7a56aSTrond Myklebust .arg.data = xprt, 145966d7a56aSTrond Myklebust }; 146066d7a56aSTrond Myklebust unsigned long total = 0; 146166d7a56aSTrond Myklebust int read = 0; 146266d7a56aSTrond Myklebust 1463edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 146466d7a56aSTrond Myklebust sk = transport->inet; 1465edc1b01cSTrond Myklebust if (sk == NULL) 1466edc1b01cSTrond Myklebust goto out; 146766d7a56aSTrond Myklebust 146866d7a56aSTrond Myklebust /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 146966d7a56aSTrond Myklebust for (;;) { 1470edc1b01cSTrond Myklebust lock_sock(sk); 147166d7a56aSTrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1472edc1b01cSTrond Myklebust release_sock(sk); 147366d7a56aSTrond Myklebust if (read <= 0) 147466d7a56aSTrond Myklebust break; 147566d7a56aSTrond Myklebust total += read; 147666d7a56aSTrond Myklebust rd_desc.count = 65536; 147766d7a56aSTrond Myklebust } 1478edc1b01cSTrond Myklebust out: 1479edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 148066d7a56aSTrond Myklebust trace_xs_tcp_data_ready(xprt, read, total); 148166d7a56aSTrond Myklebust } 148266d7a56aSTrond Myklebust 1483edc1b01cSTrond Myklebust static void xs_tcp_data_receive_workfn(struct work_struct *work) 1484edc1b01cSTrond Myklebust { 1485edc1b01cSTrond Myklebust struct sock_xprt *transport = 1486edc1b01cSTrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1487edc1b01cSTrond Myklebust xs_tcp_data_receive(transport); 1488edc1b01cSTrond Myklebust } 1489edc1b01cSTrond Myklebust 14909903cd1cSChuck Lever /** 14919903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 14929903cd1cSChuck Lever * @sk: socket with data to read 14939903cd1cSChuck Lever * 14949903cd1cSChuck Lever */ 1495676d2369SDavid S. Miller static void xs_tcp_data_ready(struct sock *sk) 1496a246b010SChuck Lever { 149766d7a56aSTrond Myklebust struct sock_xprt *transport; 1498a246b010SChuck Lever struct rpc_xprt *xprt; 1499a246b010SChuck Lever 15009903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 150146121cf7SChuck Lever 1502f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 150366d7a56aSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1504a246b010SChuck Lever goto out; 150566d7a56aSTrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 150666d7a56aSTrond Myklebust 150761d0a8e6SNeil Brown /* Any data means we had a useful conversation, so 150861d0a8e6SNeil Brown * the we don't need to delay the next reconnect 150961d0a8e6SNeil Brown */ 151061d0a8e6SNeil Brown if (xprt->reestablish_timeout) 151161d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1512edc1b01cSTrond Myklebust queue_work(rpciod_workqueue, &transport->recv_worker); 151361d0a8e6SNeil Brown 1514a246b010SChuck Lever out: 1515f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1516a246b010SChuck Lever } 1517a246b010SChuck Lever 15189903cd1cSChuck Lever /** 15199903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 15209903cd1cSChuck Lever * @sk: socket whose state has changed 15219903cd1cSChuck Lever * 15229903cd1cSChuck Lever */ 15239903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1524a246b010SChuck Lever { 1525a246b010SChuck Lever struct rpc_xprt *xprt; 15260fdea1e8STrond Myklebust struct sock_xprt *transport; 1527a246b010SChuck Lever 1528f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1529a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1530a246b010SChuck Lever goto out; 15319903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1532669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1533a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1534a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1535669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1536669502ffSAndy Chittenden sk->sk_shutdown); 1537a246b010SChuck Lever 15380fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 153940b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1540a246b010SChuck Lever switch (sk->sk_state) { 1541a246b010SChuck Lever case TCP_ESTABLISHED: 1542f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1543a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 154451971139SChuck Lever 1545a246b010SChuck Lever /* Reset TCP record info */ 154651971139SChuck Lever transport->tcp_offset = 0; 154751971139SChuck Lever transport->tcp_reclen = 0; 154851971139SChuck Lever transport->tcp_copied = 0; 1549e136d092SChuck Lever transport->tcp_flags = 1550e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 15518b71798cSTrond Myklebust xprt->connect_cookie++; 15520fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15530fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 155451971139SChuck Lever 15552a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1556a246b010SChuck Lever } 1557f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1558a246b010SChuck Lever break; 15593b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15603b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15617c1d71cfSTrond Myklebust xprt->connect_cookie++; 1562663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15633b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15644e857c58SPeter Zijlstra smp_mb__before_atomic(); 15653b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1566ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15674e857c58SPeter Zijlstra smp_mb__after_atomic(); 1568a246b010SChuck Lever break; 1569632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15703b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15717c1d71cfSTrond Myklebust xprt->connect_cookie++; 1572d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1573a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1574663b8858STrond Myklebust case TCP_CLOSING: 1575663b8858STrond Myklebust /* 1576663b8858STrond Myklebust * If the server closed down the connection, make sure that 1577663b8858STrond Myklebust * we back off before reconnecting 1578663b8858STrond Myklebust */ 1579663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1580663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15813b948ae5STrond Myklebust break; 15823b948ae5STrond Myklebust case TCP_LAST_ACK: 1583670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15844e857c58SPeter Zijlstra smp_mb__before_atomic(); 15853b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15864e857c58SPeter Zijlstra smp_mb__after_atomic(); 15873b948ae5STrond Myklebust break; 15883b948ae5STrond Myklebust case TCP_CLOSE: 15890fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15900fdea1e8STrond Myklebust &transport->sock_state)) 15910fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15927d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1593a246b010SChuck Lever } 1594a246b010SChuck Lever out: 1595f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1596a246b010SChuck Lever } 1597a246b010SChuck Lever 15981f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15991f0fa154SIlpo Järvinen { 160013331a55STrond Myklebust struct socket_wq *wq; 16011f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 16021f0fa154SIlpo Järvinen 160313331a55STrond Myklebust if (!sk->sk_socket) 16041f0fa154SIlpo Järvinen return; 160513331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16061f0fa154SIlpo Järvinen 16071f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 16081f0fa154SIlpo Järvinen return; 160913331a55STrond Myklebust rcu_read_lock(); 161013331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 161113331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 161213331a55STrond Myklebust goto out; 16131f0fa154SIlpo Järvinen 16141f0fa154SIlpo Järvinen xprt_write_space(xprt); 161513331a55STrond Myklebust out: 161613331a55STrond Myklebust rcu_read_unlock(); 16171f0fa154SIlpo Järvinen } 16181f0fa154SIlpo Järvinen 16192a9e1cfaSTrond Myklebust /** 1620c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1621c7b2cae8SChuck Lever * becomes available 16229903cd1cSChuck Lever * @sk: socket whose state has changed 16239903cd1cSChuck Lever * 1624a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1625a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1626c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1627a246b010SChuck Lever * with a bunch of small requests. 1628a246b010SChuck Lever */ 1629c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1630a246b010SChuck Lever { 1631f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1632c7b2cae8SChuck Lever 1633c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16341f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16351f0fa154SIlpo Järvinen xs_write_space(sk); 1636c7b2cae8SChuck Lever 1637f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1638c7b2cae8SChuck Lever } 1639c7b2cae8SChuck Lever 1640c7b2cae8SChuck Lever /** 1641c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1642c7b2cae8SChuck Lever * becomes available 1643c7b2cae8SChuck Lever * @sk: socket whose state has changed 1644c7b2cae8SChuck Lever * 1645c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1646c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1647c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1648c7b2cae8SChuck Lever * with a bunch of small requests. 1649c7b2cae8SChuck Lever */ 1650c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1651c7b2cae8SChuck Lever { 1652f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1653c7b2cae8SChuck Lever 1654c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 165564dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16561f0fa154SIlpo Järvinen xs_write_space(sk); 1657c7b2cae8SChuck Lever 1658f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1659a246b010SChuck Lever } 1660a246b010SChuck Lever 1661470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1662a246b010SChuck Lever { 1663ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1664ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1665a246b010SChuck Lever 16667c6e066eSChuck Lever if (transport->rcvsize) { 1667a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16687c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1669a246b010SChuck Lever } 16707c6e066eSChuck Lever if (transport->sndsize) { 1671a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16727c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1673a246b010SChuck Lever sk->sk_write_space(sk); 1674a246b010SChuck Lever } 1675a246b010SChuck Lever } 1676a246b010SChuck Lever 167743118c29SChuck Lever /** 1678470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 167943118c29SChuck Lever * @xprt: generic transport 1680470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1681470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 168243118c29SChuck Lever * 1683470056c2SChuck Lever * Set socket send and receive buffer size limits. 168443118c29SChuck Lever */ 1685470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 168643118c29SChuck Lever { 16877c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16887c6e066eSChuck Lever 16897c6e066eSChuck Lever transport->sndsize = 0; 1690470056c2SChuck Lever if (sndsize) 16917c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16927c6e066eSChuck Lever transport->rcvsize = 0; 1693470056c2SChuck Lever if (rcvsize) 16947c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1695470056c2SChuck Lever 1696470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 169743118c29SChuck Lever } 169843118c29SChuck Lever 169946c0ee8bSChuck Lever /** 170046c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 170146c0ee8bSChuck Lever * @task: task that timed out 170246c0ee8bSChuck Lever * 170346c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 170446c0ee8bSChuck Lever */ 17056a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 170646c0ee8bSChuck Lever { 17076a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 170846c0ee8bSChuck Lever } 170946c0ee8bSChuck Lever 1710b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1711b85d8806SChuck Lever { 1712b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 171363862b5bSAruna-Hewapathirane unsigned short rand = (unsigned short) prandom_u32() % range; 1714b85d8806SChuck Lever return rand + xprt_min_resvport; 1715b85d8806SChuck Lever } 1716b85d8806SChuck Lever 171792200412SChuck Lever /** 17184dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17194dda9c8aSTrond Myklebust * @sock: socket 17204dda9c8aSTrond Myklebust * 17214dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17224dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17234dda9c8aSTrond Myklebust */ 17244dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17254dda9c8aSTrond Myklebust { 1726402e23b4STrond Myklebust int opt = 1; 17274dda9c8aSTrond Myklebust 1728402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1729402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17304dda9c8aSTrond Myklebust } 17314dda9c8aSTrond Myklebust 17324dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17334dda9c8aSTrond Myklebust { 17344dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17354dda9c8aSTrond Myklebust int buflen; 17364dda9c8aSTrond Myklebust unsigned short port = 0; 17374dda9c8aSTrond Myklebust 17384dda9c8aSTrond Myklebust if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0) 17394dda9c8aSTrond Myklebust goto out; 17404dda9c8aSTrond Myklebust switch (buf.ss_family) { 17414dda9c8aSTrond Myklebust case AF_INET6: 17424dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17434dda9c8aSTrond Myklebust break; 17444dda9c8aSTrond Myklebust case AF_INET: 17454dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17464dda9c8aSTrond Myklebust } 17474dda9c8aSTrond Myklebust out: 17484dda9c8aSTrond Myklebust return port; 17494dda9c8aSTrond Myklebust } 17504dda9c8aSTrond Myklebust 17514dda9c8aSTrond Myklebust /** 175292200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 175392200412SChuck Lever * @xprt: generic transport 175492200412SChuck Lever * @port: new port number 175592200412SChuck Lever * 175692200412SChuck Lever */ 175792200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 175892200412SChuck Lever { 175992200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1760c4efcb1dSChuck Lever 17619dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17629dc3b095SChuck Lever xs_update_peer_port(xprt); 176392200412SChuck Lever } 176492200412SChuck Lever 17654dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17664dda9c8aSTrond Myklebust { 17674dda9c8aSTrond Myklebust if (transport->srcport == 0) 17684dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17694dda9c8aSTrond Myklebust } 17704dda9c8aSTrond Myklebust 17715d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport) 177267a391d7STrond Myklebust { 1773fbfffbd5SChuck Lever unsigned short port = transport->srcport; 177467a391d7STrond Myklebust 177567a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 177667a391d7STrond Myklebust port = xs_get_random_port(); 177767a391d7STrond Myklebust return port; 177867a391d7STrond Myklebust } 177967a391d7STrond Myklebust 1780baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 178167a391d7STrond Myklebust { 1782fbfffbd5SChuck Lever if (transport->srcport != 0) 1783fbfffbd5SChuck Lever transport->srcport = 0; 178467a391d7STrond Myklebust if (!transport->xprt.resvport) 178567a391d7STrond Myklebust return 0; 178667a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 178767a391d7STrond Myklebust return xprt_max_resvport; 178867a391d7STrond Myklebust return --port; 178967a391d7STrond Myklebust } 1790beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1791a246b010SChuck Lever { 1792beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 179367a391d7STrond Myklebust int err, nloop = 0; 17945d4ec932SPavel Emelyanov unsigned short port = xs_get_srcport(transport); 179567a391d7STrond Myklebust unsigned short last; 1796a246b010SChuck Lever 17970f7a622cSChris Perl /* 17980f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17990f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 18000f7a622cSChris Perl * port selection happen implicitly when the socket is used 18010f7a622cSChris Perl * (for example at connect time). 18020f7a622cSChris Perl * 18030f7a622cSChris Perl * This ensures that we can continue to establish TCP 18040f7a622cSChris Perl * connections even when all local ephemeral ports are already 18050f7a622cSChris Perl * a part of some TCP connection. This makes no difference 18060f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 18070f7a622cSChris Perl * 18080f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 18090f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 18100f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 18110f7a622cSChris Perl */ 18120f7a622cSChris Perl if (port == 0) 18130f7a622cSChris Perl return 0; 18140f7a622cSChris Perl 1815beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1816a246b010SChuck Lever do { 1817beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1818e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1819beb59b68SPavel Emelyanov transport->xprt.addrlen); 1820a246b010SChuck Lever if (err == 0) { 1821fbfffbd5SChuck Lever transport->srcport = port; 1822d3bc9a1dSFrank van Maarseveen break; 1823a246b010SChuck Lever } 182467a391d7STrond Myklebust last = port; 1825baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 182667a391d7STrond Myklebust if (port > last) 182767a391d7STrond Myklebust nloop++; 182867a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1829beb59b68SPavel Emelyanov 18304232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1831beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1832beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1833beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1834beb59b68SPavel Emelyanov else 1835beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1836beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18377dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1838a246b010SChuck Lever return err; 1839a246b010SChuck Lever } 1840a246b010SChuck Lever 1841176e21eeSChuck Lever /* 1842176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1843176e21eeSChuck Lever */ 1844176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1845176e21eeSChuck Lever { 1846fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1847176e21eeSChuck Lever } 1848176e21eeSChuck Lever 1849176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1850176e21eeSChuck Lever { 1851176e21eeSChuck Lever } 1852a246b010SChuck Lever 1853ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1854ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1855ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1856ed07536eSPeter Zijlstra 1857176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1858176e21eeSChuck Lever { 1859176e21eeSChuck Lever struct sock *sk = sock->sk; 1860176e21eeSChuck Lever 1861176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1862176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1863176e21eeSChuck Lever } 1864176e21eeSChuck Lever 18658945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1866ed07536eSPeter Zijlstra { 1867ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18688945ee5eSChuck Lever 18698945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 18708945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1871ed07536eSPeter Zijlstra } 18728945ee5eSChuck Lever 18738945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18748945ee5eSChuck Lever { 18758945ee5eSChuck Lever struct sock *sk = sock->sk; 18768945ee5eSChuck Lever 18778945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 18788945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1879ed07536eSPeter Zijlstra } 18806bc9638aSPavel Emelyanov 18816bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18826bc9638aSPavel Emelyanov { 1883*fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18841b7a1819SWeston Andros Adamson return; 18851b7a1819SWeston Andros Adamson 18864232e863SChuck Lever switch (family) { 1887176e21eeSChuck Lever case AF_LOCAL: 1888176e21eeSChuck Lever xs_reclassify_socketu(sock); 1889176e21eeSChuck Lever break; 18904232e863SChuck Lever case AF_INET: 18916bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18924232e863SChuck Lever break; 18934232e863SChuck Lever case AF_INET6: 18946bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18954232e863SChuck Lever break; 18964232e863SChuck Lever } 18976bc9638aSPavel Emelyanov } 1898ed07536eSPeter Zijlstra #else 18996bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19006bc9638aSPavel Emelyanov { 19016bc9638aSPavel Emelyanov } 1902ed07536eSPeter Zijlstra #endif 1903ed07536eSPeter Zijlstra 190493dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 190593dc41bdSNeilBrown { 190693dc41bdSNeilBrown } 190793dc41bdSNeilBrown 19086bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 19094dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 19104dda9c8aSTrond Myklebust int protocol, bool reuseport) 191122f79326SPavel Emelyanov { 191222f79326SPavel Emelyanov struct socket *sock; 191322f79326SPavel Emelyanov int err; 191422f79326SPavel Emelyanov 19156bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 191622f79326SPavel Emelyanov if (err < 0) { 191722f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 191822f79326SPavel Emelyanov protocol, -err); 191922f79326SPavel Emelyanov goto out; 192022f79326SPavel Emelyanov } 19216bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 192222f79326SPavel Emelyanov 19234dda9c8aSTrond Myklebust if (reuseport) 19244dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19254dda9c8aSTrond Myklebust 19264cea288aSBen Hutchings err = xs_bind(transport, sock); 19274cea288aSBen Hutchings if (err) { 192822f79326SPavel Emelyanov sock_release(sock); 192922f79326SPavel Emelyanov goto out; 193022f79326SPavel Emelyanov } 193122f79326SPavel Emelyanov 193222f79326SPavel Emelyanov return sock; 193322f79326SPavel Emelyanov out: 193422f79326SPavel Emelyanov return ERR_PTR(err); 193522f79326SPavel Emelyanov } 193622f79326SPavel Emelyanov 1937176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1938176e21eeSChuck Lever struct socket *sock) 1939176e21eeSChuck Lever { 1940176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1941176e21eeSChuck Lever xprt); 1942176e21eeSChuck Lever 1943176e21eeSChuck Lever if (!transport->inet) { 1944176e21eeSChuck Lever struct sock *sk = sock->sk; 1945176e21eeSChuck Lever 1946176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1947176e21eeSChuck Lever 1948176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1949176e21eeSChuck Lever 1950176e21eeSChuck Lever sk->sk_user_data = xprt; 1951a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1952176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 19532118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1954c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 1955176e21eeSChuck Lever 1956176e21eeSChuck Lever xprt_clear_connected(xprt); 1957176e21eeSChuck Lever 1958176e21eeSChuck Lever /* Reset to new socket */ 1959176e21eeSChuck Lever transport->sock = sock; 1960176e21eeSChuck Lever transport->inet = sk; 1961176e21eeSChuck Lever 1962176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1963176e21eeSChuck Lever } 1964176e21eeSChuck Lever 1965176e21eeSChuck Lever /* Tell the socket layer to start connecting... */ 1966176e21eeSChuck Lever xprt->stat.connect_count++; 1967176e21eeSChuck Lever xprt->stat.connect_start = jiffies; 1968176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1969176e21eeSChuck Lever } 1970176e21eeSChuck Lever 1971176e21eeSChuck Lever /** 1972176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1973176e21eeSChuck Lever * @transport: socket transport to connect 1974176e21eeSChuck Lever */ 1975dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1976176e21eeSChuck Lever { 1977176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1978176e21eeSChuck Lever struct socket *sock; 1979176e21eeSChuck Lever int status = -EIO; 1980176e21eeSChuck Lever 1981176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1982176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1983176e21eeSChuck Lever if (status < 0) { 1984176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1985176e21eeSChuck Lever "transport socket (%d).\n", -status); 1986176e21eeSChuck Lever goto out; 1987176e21eeSChuck Lever } 1988d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1989176e21eeSChuck Lever 1990176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1991176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1992176e21eeSChuck Lever 1993176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 199440b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1995176e21eeSChuck Lever switch (status) { 1996176e21eeSChuck Lever case 0: 1997176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1998176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1999176e21eeSChuck Lever xprt_set_connected(xprt); 20003601c4a9STrond Myklebust case -ENOBUFS: 2001176e21eeSChuck Lever break; 2002176e21eeSChuck Lever case -ENOENT: 2003176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2004176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2005176e21eeSChuck Lever break; 20064a20a988STrond Myklebust case -ECONNREFUSED: 20074a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20084a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20094a20a988STrond Myklebust break; 2010176e21eeSChuck Lever default: 2011176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2012176e21eeSChuck Lever __func__, -status, 2013176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2014176e21eeSChuck Lever } 2015176e21eeSChuck Lever 2016176e21eeSChuck Lever out: 2017176e21eeSChuck Lever xprt_clear_connecting(xprt); 2018176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2019dc107402SJ. Bruce Fields return status; 2020dc107402SJ. Bruce Fields } 2021dc107402SJ. Bruce Fields 2022b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2023dc107402SJ. Bruce Fields { 2024dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2025dc107402SJ. Bruce Fields int ret; 2026dc107402SJ. Bruce Fields 2027dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2028dc107402SJ. Bruce Fields /* 2029dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2030dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2031dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2032dc107402SJ. Bruce Fields * 2033dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2034dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2035dc107402SJ. Bruce Fields * connect. 2036dc107402SJ. Bruce Fields */ 2037dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2038dc107402SJ. Bruce Fields return; 2039dc107402SJ. Bruce Fields } 2040dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2041dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2042dc107402SJ. Bruce Fields msleep_interruptible(15000); 2043176e21eeSChuck Lever } 2044176e21eeSChuck Lever 20453c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2046d6e971d8SJeff Layton /* 2047d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2048d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2049d6e971d8SJeff Layton * races with xs_reset_transport. 2050d6e971d8SJeff Layton */ 2051a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2052a564b8f0SMel Gorman { 2053a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2054a564b8f0SMel Gorman xprt); 2055a564b8f0SMel Gorman 2056d6e971d8SJeff Layton /* 2057d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2058d6e971d8SJeff Layton * reconnecting process will get it for us. 2059d6e971d8SJeff Layton */ 2060d6e971d8SJeff Layton if (!transport->inet) 2061d6e971d8SJeff Layton return; 20628e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2063a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2064a564b8f0SMel Gorman } 2065a564b8f0SMel Gorman 2066a564b8f0SMel Gorman /** 2067d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2068a564b8f0SMel Gorman * @xprt: transport to tag 2069a564b8f0SMel Gorman * 20708e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20718e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2072a564b8f0SMel Gorman */ 2073d67fa4d8SJeff Layton static int 2074d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2075a564b8f0SMel Gorman { 2076d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2077a564b8f0SMel Gorman 2078d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2079d6e971d8SJeff Layton return 0; 2080d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2081d6e971d8SJeff Layton return -ERESTARTSYS; 2082d6e971d8SJeff Layton if (xs->inet) 2083d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2084d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 20858e228133SJeff Layton return 0; 2086a564b8f0SMel Gorman } 2087a564b8f0SMel Gorman 20888e228133SJeff Layton /** 2089d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20908e228133SJeff Layton * @xprt: transport to tag 20918e228133SJeff Layton * 20928e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 20938e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 20948e228133SJeff Layton */ 2095d67fa4d8SJeff Layton static void 2096d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 20978e228133SJeff Layton { 2098d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 20998e228133SJeff Layton 2100d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2101d6e971d8SJeff Layton return; 2102d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2103d6e971d8SJeff Layton return; 2104d6e971d8SJeff Layton if (xs->inet) 2105d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2106d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2107a564b8f0SMel Gorman } 2108a564b8f0SMel Gorman #else 2109a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2110a564b8f0SMel Gorman { 2111a564b8f0SMel Gorman } 2112d67fa4d8SJeff Layton 2113d67fa4d8SJeff Layton static int 2114d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2115d67fa4d8SJeff Layton { 2116d67fa4d8SJeff Layton return -EINVAL; 2117d67fa4d8SJeff Layton } 2118d67fa4d8SJeff Layton 2119d67fa4d8SJeff Layton static void 2120d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2121d67fa4d8SJeff Layton { 2122d67fa4d8SJeff Layton } 2123a564b8f0SMel Gorman #endif 2124a564b8f0SMel Gorman 212516be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2126a246b010SChuck Lever { 212716be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2128edb267a6SChuck Lever 2129ee0ac0c2SChuck Lever if (!transport->inet) { 2130b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2131b0d93ad5SChuck Lever 2132b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2133b0d93ad5SChuck Lever 21342a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21352a9e1cfaSTrond Myklebust 2136b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2137f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2138b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2139c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2140b0d93ad5SChuck Lever 2141b0d93ad5SChuck Lever xprt_set_connected(xprt); 2142b0d93ad5SChuck Lever 2143b0d93ad5SChuck Lever /* Reset to new socket */ 2144ee0ac0c2SChuck Lever transport->sock = sock; 2145ee0ac0c2SChuck Lever transport->inet = sk; 2146b0d93ad5SChuck Lever 2147a564b8f0SMel Gorman xs_set_memalloc(xprt); 2148a564b8f0SMel Gorman 2149b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2150b0d93ad5SChuck Lever } 2151470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 215216be2d20SChuck Lever } 215316be2d20SChuck Lever 21548c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2155a246b010SChuck Lever { 2156a246b010SChuck Lever struct sock_xprt *transport = 2157a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2158a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2159a246b010SChuck Lever struct socket *sock = transport->sock; 2160b65c0310SPavel Emelyanov int status = -EIO; 2161a246b010SChuck Lever 21628c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21634dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21644dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2165b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2166a246b010SChuck Lever goto out; 216768e220bdSChuck Lever 2168c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2169c740eff8SChuck Lever "%s (port %s)\n", xprt, 2170c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2171c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2172c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 217368e220bdSChuck Lever 217468e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 217540b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2176a246b010SChuck Lever status = 0; 2177b0d93ad5SChuck Lever out: 2178718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 2179b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 21807d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2181b0d93ad5SChuck Lever } 2182b0d93ad5SChuck Lever 21834876cc77STrond Myklebust /** 21844876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21854876cc77STrond Myklebust * @xprt: transport 21864876cc77STrond Myklebust * 21874876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 21884876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 21894876cc77STrond Myklebust */ 21904876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 21914876cc77STrond Myklebust { 21924876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21934876cc77STrond Myklebust struct socket *sock = transport->sock; 21944876cc77STrond Myklebust 21954876cc77STrond Myklebust if (sock == NULL) 21964876cc77STrond Myklebust return; 21974876cc77STrond Myklebust if (xprt_connected(xprt)) { 21984876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 21994876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22004876cc77STrond Myklebust } else 22014876cc77STrond Myklebust xs_reset_transport(transport); 22024876cc77STrond Myklebust } 22034876cc77STrond Myklebust 220416be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2205b0d93ad5SChuck Lever { 220616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2207fe19a96bSTrond Myklebust int ret = -ENOTCONN; 2208edb267a6SChuck Lever 2209ee0ac0c2SChuck Lever if (!transport->inet) { 2210b0d93ad5SChuck Lever struct sock *sk = sock->sk; 22117f260e85STrond Myklebust unsigned int keepidle = xprt->timeout->to_initval / HZ; 22127f260e85STrond Myklebust unsigned int keepcnt = xprt->timeout->to_retries + 1; 22137f260e85STrond Myklebust unsigned int opt_on = 1; 2214775f06abSTrond Myklebust unsigned int timeo; 22157f260e85STrond Myklebust 22167f260e85STrond Myklebust /* TCP Keepalive options */ 22177f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22187f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22197f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22207f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22217f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22227f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22237f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22247f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2225b0d93ad5SChuck Lever 2226775f06abSTrond Myklebust /* TCP user timeout (see RFC5482) */ 2227775f06abSTrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 2228775f06abSTrond Myklebust (xprt->timeout->to_retries + 1); 2229775f06abSTrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 2230775f06abSTrond Myklebust (char *)&timeo, sizeof(timeo)); 2231775f06abSTrond Myklebust 2232b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2233b0d93ad5SChuck Lever 22342a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 22352a9e1cfaSTrond Myklebust 2236b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2237b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 2238b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2239b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 22402118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2241c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 22423167e12cSChuck Lever 22433167e12cSChuck Lever /* socket options */ 22443167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 22453167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2246b0d93ad5SChuck Lever 2247b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2248b0d93ad5SChuck Lever 2249b0d93ad5SChuck Lever /* Reset to new socket */ 2250ee0ac0c2SChuck Lever transport->sock = sock; 2251ee0ac0c2SChuck Lever transport->inet = sk; 2252b0d93ad5SChuck Lever 2253b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2254b0d93ad5SChuck Lever } 2255b0d93ad5SChuck Lever 225601d37c42STrond Myklebust if (!xprt_bound(xprt)) 2257fe19a96bSTrond Myklebust goto out; 225801d37c42STrond Myklebust 2259a564b8f0SMel Gorman xs_set_memalloc(xprt); 2260a564b8f0SMel Gorman 2261b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 2262262ca07dSChuck Lever xprt->stat.connect_count++; 2263262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 22640fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2265fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2266fe19a96bSTrond Myklebust switch (ret) { 2267fe19a96bSTrond Myklebust case 0: 22684dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2269fe19a96bSTrond Myklebust case -EINPROGRESS: 2270fe19a96bSTrond Myklebust /* SYN_SENT! */ 2271fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2272fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2273fe19a96bSTrond Myklebust } 2274fe19a96bSTrond Myklebust out: 2275fe19a96bSTrond Myklebust return ret; 227616be2d20SChuck Lever } 227716be2d20SChuck Lever 227816be2d20SChuck Lever /** 2279b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 228016be2d20SChuck Lever * 228116be2d20SChuck Lever * Invoked by a work queue tasklet. 228216be2d20SChuck Lever */ 2283cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 228416be2d20SChuck Lever { 2285cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2286cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 228716be2d20SChuck Lever struct socket *sock = transport->sock; 2288a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2289b61d59ffSTrond Myklebust int status = -EIO; 229016be2d20SChuck Lever 229116be2d20SChuck Lever if (!sock) { 2292cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 22934dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 22944dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2295b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2296b61d59ffSTrond Myklebust status = PTR_ERR(sock); 229716be2d20SChuck Lever goto out; 229816be2d20SChuck Lever } 22997d1e8255STrond Myklebust } 23007d1e8255STrond Myklebust 2301c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2302c740eff8SChuck Lever "%s (port %s)\n", xprt, 2303c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2304c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2305c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 230616be2d20SChuck Lever 230716be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 230840b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2309a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 231046121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 231146121cf7SChuck Lever sock->sk->sk_state); 2312a246b010SChuck Lever switch (status) { 2313f75e6745STrond Myklebust default: 2314f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2315f75e6745STrond Myklebust __func__, status); 2316f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2317f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2318f75e6745STrond Myklebust * and retry 2319f75e6745STrond Myklebust */ 2320a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 232188b5ed73STrond Myklebust break; 23222a491991STrond Myklebust case 0: 2323a246b010SChuck Lever case -EINPROGRESS: 2324a246b010SChuck Lever case -EALREADY: 2325718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 23267d1e8255STrond Myklebust return; 23279fcfe0c8STrond Myklebust case -EINVAL: 23289fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 23299fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 23309fcfe0c8STrond Myklebust */ 23313ed5e2a2STrond Myklebust case -ECONNREFUSED: 23323ed5e2a2STrond Myklebust case -ECONNRESET: 23333ed5e2a2STrond Myklebust case -ENETUNREACH: 23343913c78cSTrond Myklebust case -EADDRINUSE: 23353601c4a9STrond Myklebust case -ENOBUFS: 23363ed5e2a2STrond Myklebust /* retry with existing socket, after a delay */ 23374efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 23389fcfe0c8STrond Myklebust goto out; 23398a2cec29STrond Myklebust } 23402a491991STrond Myklebust status = -EAGAIN; 2341a246b010SChuck Lever out: 2342718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 23432226feb6SChuck Lever xprt_clear_connecting(xprt); 23447d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2345a246b010SChuck Lever } 2346a246b010SChuck Lever 234768e220bdSChuck Lever /** 23489903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 23491b092092STrond Myklebust * @xprt: pointer to transport structure 23509903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 23519903cd1cSChuck Lever * 23529903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 235303bf4b70SChuck Lever * 235403bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 235503bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 235603bf4b70SChuck Lever * socket on a privileged port. 235703bf4b70SChuck Lever * 235803bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 235903bf4b70SChuck Lever * retry floods (hard mounts). 23609903cd1cSChuck Lever */ 23611b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2362a246b010SChuck Lever { 2363ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2364a246b010SChuck Lever 2365718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2366718ba5b8STrond Myklebust 236799b1a4c3STrond Myklebust if (transport->sock != NULL) { 236846121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 236946121cf7SChuck Lever "seconds\n", 237003bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 237199b1a4c3STrond Myklebust 237299b1a4c3STrond Myklebust /* Start by resetting any existing state */ 237399b1a4c3STrond Myklebust xs_reset_transport(transport); 237499b1a4c3STrond Myklebust 2375c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2376c1384c9cSTrond Myklebust &transport->connect_worker, 237703bf4b70SChuck Lever xprt->reestablish_timeout); 237803bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 237961d0a8e6SNeil Brown if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 238061d0a8e6SNeil Brown xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 238103bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 238203bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 23839903cd1cSChuck Lever } else { 23849903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2385c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2386c1384c9cSTrond Myklebust &transport->connect_worker, 0); 2387a246b010SChuck Lever } 2388a246b010SChuck Lever } 2389a246b010SChuck Lever 2390262ca07dSChuck Lever /** 2391176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2392176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2393176e21eeSChuck Lever * @seq: output file 2394176e21eeSChuck Lever * 2395176e21eeSChuck Lever */ 2396176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2397176e21eeSChuck Lever { 2398176e21eeSChuck Lever long idle_time = 0; 2399176e21eeSChuck Lever 2400176e21eeSChuck Lever if (xprt_connected(xprt)) 2401176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2402176e21eeSChuck Lever 2403176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 240415a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2405176e21eeSChuck Lever xprt->stat.bind_count, 2406176e21eeSChuck Lever xprt->stat.connect_count, 2407176e21eeSChuck Lever xprt->stat.connect_time, 2408176e21eeSChuck Lever idle_time, 2409176e21eeSChuck Lever xprt->stat.sends, 2410176e21eeSChuck Lever xprt->stat.recvs, 2411176e21eeSChuck Lever xprt->stat.bad_xids, 2412176e21eeSChuck Lever xprt->stat.req_u, 241315a45206SAndy Adamson xprt->stat.bklog_u, 241415a45206SAndy Adamson xprt->stat.max_slots, 241515a45206SAndy Adamson xprt->stat.sending_u, 241615a45206SAndy Adamson xprt->stat.pending_u); 2417176e21eeSChuck Lever } 2418176e21eeSChuck Lever 2419176e21eeSChuck Lever /** 2420262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2421262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2422262ca07dSChuck Lever * @seq: output file 2423262ca07dSChuck Lever * 2424262ca07dSChuck Lever */ 2425262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2426262ca07dSChuck Lever { 2427c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2428c8475461SChuck Lever 242915a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 243015a45206SAndy Adamson "%lu %llu %llu\n", 2431fbfffbd5SChuck Lever transport->srcport, 2432262ca07dSChuck Lever xprt->stat.bind_count, 2433262ca07dSChuck Lever xprt->stat.sends, 2434262ca07dSChuck Lever xprt->stat.recvs, 2435262ca07dSChuck Lever xprt->stat.bad_xids, 2436262ca07dSChuck Lever xprt->stat.req_u, 243715a45206SAndy Adamson xprt->stat.bklog_u, 243815a45206SAndy Adamson xprt->stat.max_slots, 243915a45206SAndy Adamson xprt->stat.sending_u, 244015a45206SAndy Adamson xprt->stat.pending_u); 2441262ca07dSChuck Lever } 2442262ca07dSChuck Lever 2443262ca07dSChuck Lever /** 2444262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2445262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2446262ca07dSChuck Lever * @seq: output file 2447262ca07dSChuck Lever * 2448262ca07dSChuck Lever */ 2449262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2450262ca07dSChuck Lever { 2451c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2452262ca07dSChuck Lever long idle_time = 0; 2453262ca07dSChuck Lever 2454262ca07dSChuck Lever if (xprt_connected(xprt)) 2455262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2456262ca07dSChuck Lever 245715a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 245815a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2459fbfffbd5SChuck Lever transport->srcport, 2460262ca07dSChuck Lever xprt->stat.bind_count, 2461262ca07dSChuck Lever xprt->stat.connect_count, 2462262ca07dSChuck Lever xprt->stat.connect_time, 2463262ca07dSChuck Lever idle_time, 2464262ca07dSChuck Lever xprt->stat.sends, 2465262ca07dSChuck Lever xprt->stat.recvs, 2466262ca07dSChuck Lever xprt->stat.bad_xids, 2467262ca07dSChuck Lever xprt->stat.req_u, 246815a45206SAndy Adamson xprt->stat.bklog_u, 246915a45206SAndy Adamson xprt->stat.max_slots, 247015a45206SAndy Adamson xprt->stat.sending_u, 247115a45206SAndy Adamson xprt->stat.pending_u); 2472262ca07dSChuck Lever } 2473262ca07dSChuck Lever 24744cfc7e60SRahul Iyer /* 24754cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 24764cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 24774cfc7e60SRahul Iyer * to use the server side send routines. 24784cfc7e60SRahul Iyer */ 24795a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size) 24804cfc7e60SRahul Iyer { 24814cfc7e60SRahul Iyer struct page *page; 24824cfc7e60SRahul Iyer struct rpc_buffer *buf; 24834cfc7e60SRahul Iyer 2484b8a13d03SWeston Andros Adamson WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer)); 2485b8a13d03SWeston Andros Adamson if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) 2486b8a13d03SWeston Andros Adamson return NULL; 24874cfc7e60SRahul Iyer 2488b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 24894cfc7e60SRahul Iyer if (!page) 24904cfc7e60SRahul Iyer return NULL; 24914cfc7e60SRahul Iyer 24924cfc7e60SRahul Iyer buf = page_address(page); 24934cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 24944cfc7e60SRahul Iyer 24954cfc7e60SRahul Iyer return buf->data; 24964cfc7e60SRahul Iyer } 24974cfc7e60SRahul Iyer 24984cfc7e60SRahul Iyer /* 24994cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 25004cfc7e60SRahul Iyer */ 25015a51f13aSH Hartley Sweeten static void bc_free(void *buffer) 25024cfc7e60SRahul Iyer { 25034cfc7e60SRahul Iyer struct rpc_buffer *buf; 25044cfc7e60SRahul Iyer 25054cfc7e60SRahul Iyer if (!buffer) 25064cfc7e60SRahul Iyer return; 25074cfc7e60SRahul Iyer 25084cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 25094cfc7e60SRahul Iyer free_page((unsigned long)buf); 25104cfc7e60SRahul Iyer } 25114cfc7e60SRahul Iyer 25124cfc7e60SRahul Iyer /* 25134cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 25144cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 25154cfc7e60SRahul Iyer */ 25164cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 25174cfc7e60SRahul Iyer { 25184cfc7e60SRahul Iyer int len; 25194cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 25204cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 25214cfc7e60SRahul Iyer struct sock_xprt *transport = 25224cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 25234cfc7e60SRahul Iyer struct socket *sock = transport->sock; 25244cfc7e60SRahul Iyer unsigned long headoff; 25254cfc7e60SRahul Iyer unsigned long tailoff; 25264cfc7e60SRahul Iyer 252761677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 25284cfc7e60SRahul Iyer 25294cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 25304cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 25314cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 25324cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 25334cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 25344cfc7e60SRahul Iyer 25354cfc7e60SRahul Iyer if (len != xbufp->len) { 25364cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 25374cfc7e60SRahul Iyer len = -EAGAIN; 25384cfc7e60SRahul Iyer } 25394cfc7e60SRahul Iyer 25404cfc7e60SRahul Iyer return len; 25414cfc7e60SRahul Iyer } 25424cfc7e60SRahul Iyer 25434cfc7e60SRahul Iyer /* 25444cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 25454cfc7e60SRahul Iyer */ 25464cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task) 25474cfc7e60SRahul Iyer { 25484cfc7e60SRahul Iyer struct rpc_rqst *req = task->tk_rqstp; 25494cfc7e60SRahul Iyer struct svc_xprt *xprt; 25507fc56136SAndrzej Hajda int len; 25514cfc7e60SRahul Iyer 25524cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 25534cfc7e60SRahul Iyer /* 25544cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 25554cfc7e60SRahul Iyer */ 25564cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 25574cfc7e60SRahul Iyer 25584cfc7e60SRahul Iyer /* 25594cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 25604cfc7e60SRahul Iyer * with the fore channel 25614cfc7e60SRahul Iyer */ 25624cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) { 25634cfc7e60SRahul Iyer rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); 25644cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) 25654cfc7e60SRahul Iyer return -EAGAIN; 25664cfc7e60SRahul Iyer rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); 25674cfc7e60SRahul Iyer } 25684cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 25694cfc7e60SRahul Iyer len = -ENOTCONN; 25704cfc7e60SRahul Iyer else 25714cfc7e60SRahul Iyer len = bc_sendto(req); 25724cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 25734cfc7e60SRahul Iyer 25744cfc7e60SRahul Iyer if (len > 0) 25754cfc7e60SRahul Iyer len = 0; 25764cfc7e60SRahul Iyer 25774cfc7e60SRahul Iyer return len; 25784cfc7e60SRahul Iyer } 25794cfc7e60SRahul Iyer 25804cfc7e60SRahul Iyer /* 25814cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 25824cfc7e60SRahul Iyer */ 25834cfc7e60SRahul Iyer 25844cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 25854cfc7e60SRahul Iyer { 25864cfc7e60SRahul Iyer } 25874cfc7e60SRahul Iyer 25884cfc7e60SRahul Iyer /* 25894cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 25904cfc7e60SRahul Iyer * initiated, we do nothing 25914cfc7e60SRahul Iyer */ 25924cfc7e60SRahul Iyer 25934cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 25944cfc7e60SRahul Iyer { 259547f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 259647f72efaSKinglong Mee 259747f72efaSKinglong Mee xs_xprt_free(xprt); 259847f72efaSKinglong Mee module_put(THIS_MODULE); 25994cfc7e60SRahul Iyer } 26004cfc7e60SRahul Iyer 2601176e21eeSChuck Lever static struct rpc_xprt_ops xs_local_ops = { 2602176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 2603176e21eeSChuck Lever .release_xprt = xs_tcp_release_xprt, 2604f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2605176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2606176e21eeSChuck Lever .set_port = xs_local_set_port, 2607dc107402SJ. Bruce Fields .connect = xs_local_connect, 2608176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2609176e21eeSChuck Lever .buf_free = rpc_free, 2610176e21eeSChuck Lever .send_request = xs_local_send_request, 2611176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2612176e21eeSChuck Lever .close = xs_close, 2613a1311d87STrond Myklebust .destroy = xs_destroy, 2614176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2615d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2616d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2617176e21eeSChuck Lever }; 2618176e21eeSChuck Lever 2619262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 262043118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 262112a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 262249e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2623f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 262445160d62SChuck Lever .rpcbind = rpcb_getport_async, 262592200412SChuck Lever .set_port = xs_set_port, 26269903cd1cSChuck Lever .connect = xs_connect, 262702107148SChuck Lever .buf_alloc = rpc_malloc, 262802107148SChuck Lever .buf_free = rpc_free, 2629262965f5SChuck Lever .send_request = xs_udp_send_request, 2630fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 263146c0ee8bSChuck Lever .timer = xs_udp_timer, 2632a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2633262965f5SChuck Lever .close = xs_close, 2634262965f5SChuck Lever .destroy = xs_destroy, 2635262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2636d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2637d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26384a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2639262965f5SChuck Lever }; 2640262965f5SChuck Lever 2641262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 264212a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 2643e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 2644f39c1bfbSTrond Myklebust .alloc_slot = xprt_lock_and_alloc_slot, 264545160d62SChuck Lever .rpcbind = rpcb_getport_async, 264692200412SChuck Lever .set_port = xs_set_port, 26470b9e7943STrond Myklebust .connect = xs_connect, 264802107148SChuck Lever .buf_alloc = rpc_malloc, 264902107148SChuck Lever .buf_free = rpc_free, 2650262965f5SChuck Lever .send_request = xs_tcp_send_request, 2651fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2652c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 26539903cd1cSChuck Lever .destroy = xs_destroy, 2654262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2655d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2656d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26574a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 265842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 265942e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 266076566773SChuck Lever .bc_up = xs_tcp_bc_up, 266142e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 266242e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 266342e5c3e2SChuck Lever #endif 2664a246b010SChuck Lever }; 2665a246b010SChuck Lever 26664cfc7e60SRahul Iyer /* 26674cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 26684cfc7e60SRahul Iyer */ 26694cfc7e60SRahul Iyer 26704cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = { 26714cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 26724cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 267384e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 26744cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 26754cfc7e60SRahul Iyer .buf_free = bc_free, 26764cfc7e60SRahul Iyer .send_request = bc_send_request, 26774cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 26784cfc7e60SRahul Iyer .close = bc_close, 26794cfc7e60SRahul Iyer .destroy = bc_destroy, 26804cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2681d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2682d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26834a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 26844cfc7e60SRahul Iyer }; 26854cfc7e60SRahul Iyer 268692476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 268792476850SChuck Lever { 268892476850SChuck Lever static const struct sockaddr_in sin = { 268992476850SChuck Lever .sin_family = AF_INET, 269092476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 269192476850SChuck Lever }; 269292476850SChuck Lever static const struct sockaddr_in6 sin6 = { 269392476850SChuck Lever .sin6_family = AF_INET6, 269492476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 269592476850SChuck Lever }; 269692476850SChuck Lever 269792476850SChuck Lever switch (family) { 2698176e21eeSChuck Lever case AF_LOCAL: 2699176e21eeSChuck Lever break; 270092476850SChuck Lever case AF_INET: 270192476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 270292476850SChuck Lever break; 270392476850SChuck Lever case AF_INET6: 270492476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 270592476850SChuck Lever break; 270692476850SChuck Lever default: 270792476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 270892476850SChuck Lever return -EAFNOSUPPORT; 270992476850SChuck Lever } 271092476850SChuck Lever return 0; 271192476850SChuck Lever } 271292476850SChuck Lever 27133c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2714d9ba131dSTrond Myklebust unsigned int slot_table_size, 2715d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2716c8541ecdSChuck Lever { 2717c8541ecdSChuck Lever struct rpc_xprt *xprt; 2718ffc2e518SChuck Lever struct sock_xprt *new; 2719c8541ecdSChuck Lever 272096802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2721c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2722c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2723c8541ecdSChuck Lever } 2724c8541ecdSChuck Lever 2725d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2726d9ba131dSTrond Myklebust max_slot_table_size); 2727bd1722d4SPavel Emelyanov if (xprt == NULL) { 272846121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 272946121cf7SChuck Lever "rpc_xprt\n"); 2730c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2731c8541ecdSChuck Lever } 2732c8541ecdSChuck Lever 2733bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2734edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 273596802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 273696802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2737d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2738fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 273992476850SChuck Lever else { 274092476850SChuck Lever int err; 274192476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 274292476850SChuck Lever (struct sockaddr *)&new->srcaddr); 27432aa13531SStanislav Kinsbursky if (err != 0) { 27442aa13531SStanislav Kinsbursky xprt_free(xprt); 274592476850SChuck Lever return ERR_PTR(err); 274692476850SChuck Lever } 27472aa13531SStanislav Kinsbursky } 2748c8541ecdSChuck Lever 2749c8541ecdSChuck Lever return xprt; 2750c8541ecdSChuck Lever } 2751c8541ecdSChuck Lever 2752176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2753176e21eeSChuck Lever .to_initval = 10 * HZ, 2754176e21eeSChuck Lever .to_maxval = 10 * HZ, 2755176e21eeSChuck Lever .to_retries = 2, 2756176e21eeSChuck Lever }; 2757176e21eeSChuck Lever 2758176e21eeSChuck Lever /** 2759176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2760176e21eeSChuck Lever * @args: rpc transport creation arguments 2761176e21eeSChuck Lever * 2762176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2763176e21eeSChuck Lever */ 2764176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2765176e21eeSChuck Lever { 2766176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2767176e21eeSChuck Lever struct sock_xprt *transport; 2768176e21eeSChuck Lever struct rpc_xprt *xprt; 2769176e21eeSChuck Lever struct rpc_xprt *ret; 2770176e21eeSChuck Lever 2771d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2772d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2773176e21eeSChuck Lever if (IS_ERR(xprt)) 2774176e21eeSChuck Lever return xprt; 2775176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2776176e21eeSChuck Lever 2777176e21eeSChuck Lever xprt->prot = 0; 2778176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2779176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2780176e21eeSChuck Lever 2781176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2782176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2783176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2784176e21eeSChuck Lever 2785176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2786176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2787176e21eeSChuck Lever 2788a2648094STrond Myklebust INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn); 278993dc41bdSNeilBrown INIT_DELAYED_WORK(&transport->connect_worker, 279093dc41bdSNeilBrown xs_dummy_setup_socket); 279193dc41bdSNeilBrown 2792176e21eeSChuck Lever switch (sun->sun_family) { 2793176e21eeSChuck Lever case AF_LOCAL: 2794176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2795176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2796176e21eeSChuck Lever sun->sun_path); 2797176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2798176e21eeSChuck Lever goto out_err; 2799176e21eeSChuck Lever } 2800176e21eeSChuck Lever xprt_set_bound(xprt); 2801176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 28027073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 28037073ea87SJ. Bruce Fields if (ret) 28047073ea87SJ. Bruce Fields goto out_err; 2805176e21eeSChuck Lever break; 2806176e21eeSChuck Lever default: 2807176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2808176e21eeSChuck Lever goto out_err; 2809176e21eeSChuck Lever } 2810176e21eeSChuck Lever 2811176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2812176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2813176e21eeSChuck Lever 2814176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2815176e21eeSChuck Lever return xprt; 2816176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2817176e21eeSChuck Lever out_err: 2818315f3812SKinglong Mee xs_xprt_free(xprt); 2819176e21eeSChuck Lever return ret; 2820176e21eeSChuck Lever } 2821176e21eeSChuck Lever 28222881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 28232881ae74STrond Myklebust .to_initval = 5 * HZ, 28242881ae74STrond Myklebust .to_maxval = 30 * HZ, 28252881ae74STrond Myklebust .to_increment = 5 * HZ, 28262881ae74STrond Myklebust .to_retries = 5, 28272881ae74STrond Myklebust }; 28282881ae74STrond Myklebust 28299903cd1cSChuck Lever /** 28309903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 283196802a09SFrank van Maarseveen * @args: rpc transport creation arguments 28329903cd1cSChuck Lever * 28339903cd1cSChuck Lever */ 2834483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2835a246b010SChuck Lever { 28368f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2837c8541ecdSChuck Lever struct rpc_xprt *xprt; 2838c8475461SChuck Lever struct sock_xprt *transport; 28390a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2840a246b010SChuck Lever 2841d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2842d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2843c8541ecdSChuck Lever if (IS_ERR(xprt)) 2844c8541ecdSChuck Lever return xprt; 2845c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2846a246b010SChuck Lever 2847ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2848808012fbSChuck Lever xprt->tsh_size = 0; 2849a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2850a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2851a246b010SChuck Lever 285203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 285303bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 285403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2855a246b010SChuck Lever 2856262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2857a246b010SChuck Lever 2858ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2859a246b010SChuck Lever 2860f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 2861edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2862edc1b01cSTrond Myklebust 28638f9d5b1aSChuck Lever switch (addr->sa_family) { 28648f9d5b1aSChuck Lever case AF_INET: 28658f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 28668f9d5b1aSChuck Lever xprt_set_bound(xprt); 28678f9d5b1aSChuck Lever 28689dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 28698f9d5b1aSChuck Lever break; 28708f9d5b1aSChuck Lever case AF_INET6: 28718f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 28728f9d5b1aSChuck Lever xprt_set_bound(xprt); 28738f9d5b1aSChuck Lever 28749dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 28758f9d5b1aSChuck Lever break; 28768f9d5b1aSChuck Lever default: 28770a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 28780a68b0beSJ. Bruce Fields goto out_err; 28798f9d5b1aSChuck Lever } 28808f9d5b1aSChuck Lever 2881c740eff8SChuck Lever if (xprt_bound(xprt)) 2882c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2883c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2884c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2885c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2886c740eff8SChuck Lever else 2887c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2888c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2889c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2890edb267a6SChuck Lever 2891bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2892c8541ecdSChuck Lever return xprt; 28930a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 28940a68b0beSJ. Bruce Fields out_err: 2895315f3812SKinglong Mee xs_xprt_free(xprt); 28960a68b0beSJ. Bruce Fields return ret; 2897a246b010SChuck Lever } 2898a246b010SChuck Lever 28992881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 29002881ae74STrond Myklebust .to_initval = 60 * HZ, 29012881ae74STrond Myklebust .to_maxval = 60 * HZ, 29022881ae74STrond Myklebust .to_retries = 2, 29032881ae74STrond Myklebust }; 29042881ae74STrond Myklebust 29059903cd1cSChuck Lever /** 29069903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 290796802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29089903cd1cSChuck Lever * 29099903cd1cSChuck Lever */ 2910483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2911a246b010SChuck Lever { 29128f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2913c8541ecdSChuck Lever struct rpc_xprt *xprt; 2914c8475461SChuck Lever struct sock_xprt *transport; 29150a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2916b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2917b7993cebSTrond Myklebust 2918b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2919b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2920a246b010SChuck Lever 2921d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2922b7993cebSTrond Myklebust max_slot_table_size); 2923c8541ecdSChuck Lever if (IS_ERR(xprt)) 2924c8541ecdSChuck Lever return xprt; 2925c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2926a246b010SChuck Lever 2927ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2928808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2929808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2930a246b010SChuck Lever 293103bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 293203bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 293303bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2934a246b010SChuck Lever 2935262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2936ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2937a246b010SChuck Lever 2938edc1b01cSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn); 2939edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 2940edc1b01cSTrond Myklebust 29418f9d5b1aSChuck Lever switch (addr->sa_family) { 29428f9d5b1aSChuck Lever case AF_INET: 29438f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 29448f9d5b1aSChuck Lever xprt_set_bound(xprt); 29458f9d5b1aSChuck Lever 29469dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 29478f9d5b1aSChuck Lever break; 29488f9d5b1aSChuck Lever case AF_INET6: 29498f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 29508f9d5b1aSChuck Lever xprt_set_bound(xprt); 29518f9d5b1aSChuck Lever 29529dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 29538f9d5b1aSChuck Lever break; 29548f9d5b1aSChuck Lever default: 29550a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 29560a68b0beSJ. Bruce Fields goto out_err; 29578f9d5b1aSChuck Lever } 29588f9d5b1aSChuck Lever 2959c740eff8SChuck Lever if (xprt_bound(xprt)) 2960c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2961c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2962c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2963c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2964c740eff8SChuck Lever else 2965c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2966c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2967c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2968c740eff8SChuck Lever 2969bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2970c8541ecdSChuck Lever return xprt; 29710a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 29720a68b0beSJ. Bruce Fields out_err: 2973315f3812SKinglong Mee xs_xprt_free(xprt); 29740a68b0beSJ. Bruce Fields return ret; 2975a246b010SChuck Lever } 2976282b32e1SChuck Lever 2977f300babaSAlexandros Batsakis /** 2978f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 2979f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 2980f300babaSAlexandros Batsakis * 2981f300babaSAlexandros Batsakis */ 2982f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 2983f300babaSAlexandros Batsakis { 2984f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 2985f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 2986f300babaSAlexandros Batsakis struct sock_xprt *transport; 2987f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 29880a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2989f300babaSAlexandros Batsakis 2990d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2991d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 2992f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 2993f300babaSAlexandros Batsakis return xprt; 2994f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 2995f300babaSAlexandros Batsakis 2996f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 2997f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2998f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2999f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3000f300babaSAlexandros Batsakis 3001f300babaSAlexandros Batsakis /* backchannel */ 3002f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3003f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3004f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3005f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3006f300babaSAlexandros Batsakis 3007f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3008f300babaSAlexandros Batsakis 3009f300babaSAlexandros Batsakis switch (addr->sa_family) { 3010f300babaSAlexandros Batsakis case AF_INET: 3011f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3012f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3013f300babaSAlexandros Batsakis break; 3014f300babaSAlexandros Batsakis case AF_INET6: 3015f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3016f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3017f300babaSAlexandros Batsakis break; 3018f300babaSAlexandros Batsakis default: 30190a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30200a68b0beSJ. Bruce Fields goto out_err; 3021f300babaSAlexandros Batsakis } 3022f300babaSAlexandros Batsakis 3023f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3024f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3025f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3026f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3027f300babaSAlexandros Batsakis 3028f300babaSAlexandros Batsakis /* 302999de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 303028303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 303128303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 303228303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 303399de8ea9SJ. Bruce Fields */ 303499de8ea9SJ. Bruce Fields xprt_get(xprt); 303599de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 303699de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 303799de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 303899de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 303999de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 304099de8ea9SJ. Bruce Fields 304199de8ea9SJ. Bruce Fields /* 3042f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3043f300babaSAlexandros Batsakis * the xprt status to connected 3044f300babaSAlexandros Batsakis */ 3045f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3046f300babaSAlexandros Batsakis 3047f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3048f300babaSAlexandros Batsakis return xprt; 3049642aab58SKinglong Mee 3050642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 305199de8ea9SJ. Bruce Fields xprt_put(xprt); 30520a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30530a68b0beSJ. Bruce Fields out_err: 3054315f3812SKinglong Mee xs_xprt_free(xprt); 30550a68b0beSJ. Bruce Fields return ret; 3056f300babaSAlexandros Batsakis } 3057f300babaSAlexandros Batsakis 3058176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3059176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3060176e21eeSChuck Lever .name = "named UNIX socket", 3061176e21eeSChuck Lever .owner = THIS_MODULE, 3062176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3063176e21eeSChuck Lever .setup = xs_setup_local, 3064176e21eeSChuck Lever }; 3065176e21eeSChuck Lever 3066bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3067bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3068bc25571eS\"Talpey, Thomas\ .name = "udp", 3069bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3070f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3071bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3072bc25571eS\"Talpey, Thomas\ }; 3073bc25571eS\"Talpey, Thomas\ 3074bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3075bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3076bc25571eS\"Talpey, Thomas\ .name = "tcp", 3077bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3078f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3079bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3080bc25571eS\"Talpey, Thomas\ }; 3081bc25571eS\"Talpey, Thomas\ 3082f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3083f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3084f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3085f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3086f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3087f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3088f300babaSAlexandros Batsakis }; 3089f300babaSAlexandros Batsakis 3090282b32e1SChuck Lever /** 3091bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3092282b32e1SChuck Lever * 3093282b32e1SChuck Lever */ 3094282b32e1SChuck Lever int init_socket_xprt(void) 3095282b32e1SChuck Lever { 3096f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 30972b1bec5fSEric W. Biederman if (!sunrpc_table_header) 30980b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3099fbf76683SChuck Lever #endif 3100fbf76683SChuck Lever 3101176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3102bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3103bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3104f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3105bc25571eS\"Talpey, Thomas\ 3106282b32e1SChuck Lever return 0; 3107282b32e1SChuck Lever } 3108282b32e1SChuck Lever 3109282b32e1SChuck Lever /** 3110bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3111282b32e1SChuck Lever * 3112282b32e1SChuck Lever */ 3113282b32e1SChuck Lever void cleanup_socket_xprt(void) 3114282b32e1SChuck Lever { 3115f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3116fbf76683SChuck Lever if (sunrpc_table_header) { 3117fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3118fbf76683SChuck Lever sunrpc_table_header = NULL; 3119fbf76683SChuck Lever } 3120fbf76683SChuck Lever #endif 3121bc25571eS\"Talpey, Thomas\ 3122176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3123bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3124bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3125f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3126282b32e1SChuck Lever } 3127cbf11071STrond Myklebust 31289bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 31299bbb9e5aSRusty Russell const struct kernel_param *kp, 3130cbf11071STrond Myklebust unsigned int min, unsigned int max) 3131cbf11071STrond Myklebust { 313200cfaa94SDaniel Walter unsigned int num; 3133cbf11071STrond Myklebust int ret; 3134cbf11071STrond Myklebust 3135cbf11071STrond Myklebust if (!val) 3136cbf11071STrond Myklebust return -EINVAL; 313700cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 3138cbf11071STrond Myklebust if (ret == -EINVAL || num < min || num > max) 3139cbf11071STrond Myklebust return -EINVAL; 3140cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3141cbf11071STrond Myklebust return 0; 3142cbf11071STrond Myklebust } 3143cbf11071STrond Myklebust 31449bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3145cbf11071STrond Myklebust { 3146cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3147cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3148cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3149cbf11071STrond Myklebust } 3150cbf11071STrond Myklebust 31519c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 31529bbb9e5aSRusty Russell .set = param_set_portnr, 31539bbb9e5aSRusty Russell .get = param_get_uint, 31549bbb9e5aSRusty Russell }; 31559bbb9e5aSRusty Russell 3156cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3157cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3158cbf11071STrond Myklebust 3159cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3160cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3161cbf11071STrond Myklebust 31629bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 31639bbb9e5aSRusty Russell const struct kernel_param *kp) 3164cbf11071STrond Myklebust { 3165cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3166cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3167cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3168cbf11071STrond Myklebust } 3169cbf11071STrond Myklebust 31709c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 31719bbb9e5aSRusty Russell .set = param_set_slot_table_size, 31729bbb9e5aSRusty Russell .get = param_get_uint, 31739bbb9e5aSRusty Russell }; 31749bbb9e5aSRusty Russell 3175cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3176cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3177cbf11071STrond Myklebust 3178d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3179d9ba131dSTrond Myklebust const struct kernel_param *kp) 3180d9ba131dSTrond Myklebust { 3181d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3182d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3183d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3184d9ba131dSTrond Myklebust } 3185d9ba131dSTrond Myklebust 31869c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3187d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3188d9ba131dSTrond Myklebust .get = param_get_uint, 3189d9ba131dSTrond Myklebust }; 3190d9ba131dSTrond Myklebust 3191d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3192d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3193d9ba131dSTrond Myklebust 3194cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3195cbf11071STrond Myklebust slot_table_size, 0644); 3196d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3197d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3198cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3199cbf11071STrond Myklebust slot_table_size, 0644); 3200cbf11071STrond Myklebust 3201