1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2a246b010SChuck Lever /* 3a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 4a246b010SChuck Lever * 5a246b010SChuck Lever * Client-side transport implementation for sockets. 6a246b010SChuck Lever * 7113aa838SAlan Cox * TCP callback races fixes (C) 1998 Red Hat 8113aa838SAlan Cox * TCP send fixes (C) 1998 Red Hat 9a246b010SChuck Lever * TCP NFS related read + write fixes 10a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 11a246b010SChuck Lever * 12a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 13a246b010SChuck Lever * Fix behaviour when socket buffer is full. 14a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1555aa4f58SChuck Lever * 1655aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 178f9d5b1aSChuck Lever * 188f9d5b1aSChuck Lever * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 198f9d5b1aSChuck Lever * <gilles.quillard@bull.net> 20a246b010SChuck Lever */ 21a246b010SChuck Lever 22a246b010SChuck Lever #include <linux/types.h> 23176e21eeSChuck Lever #include <linux/string.h> 24a246b010SChuck Lever #include <linux/slab.h> 25bc25571eS\"Talpey, Thomas\ #include <linux/module.h> 26a246b010SChuck Lever #include <linux/capability.h> 27a246b010SChuck Lever #include <linux/pagemap.h> 28a246b010SChuck Lever #include <linux/errno.h> 29a246b010SChuck Lever #include <linux/socket.h> 30a246b010SChuck Lever #include <linux/in.h> 31a246b010SChuck Lever #include <linux/net.h> 32a246b010SChuck Lever #include <linux/mm.h> 33176e21eeSChuck Lever #include <linux/un.h> 34a246b010SChuck Lever #include <linux/udp.h> 35a246b010SChuck Lever #include <linux/tcp.h> 36a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 375976687aSJeff Layton #include <linux/sunrpc/addr.h> 3802107148SChuck Lever #include <linux/sunrpc/sched.h> 394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h> 4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 41a246b010SChuck Lever #include <linux/file.h> 429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 4444b98efdSRicardo Labiaga #endif 45a246b010SChuck Lever 46a246b010SChuck Lever #include <net/sock.h> 47a246b010SChuck Lever #include <net/checksum.h> 48a246b010SChuck Lever #include <net/udp.h> 49a246b010SChuck Lever #include <net/tcp.h> 50a246b010SChuck Lever 5140b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 5240b5ea0cSTrond Myklebust 534cfc7e60SRahul Iyer #include "sunrpc.h" 54176e21eeSChuck Lever 553d188805STrond Myklebust #define RPC_TCP_READ_CHUNK_SZ (3*512*1024) 563d188805STrond Myklebust 57176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 587196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 597196dbb0STrond Myklebust struct socket *sock); 60176e21eeSChuck Lever 619903cd1cSChuck Lever /* 62c556b754SChuck Lever * xprtsock tunables 63c556b754SChuck Lever */ 6409acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 6509acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 6609acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 67c556b754SChuck Lever 6809acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 6909acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 70c556b754SChuck Lever 7154c09874STrond Myklebust #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 7254c09874STrond Myklebust 737d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 7425fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 757d1e8255STrond Myklebust 76c556b754SChuck Lever /* 77fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 78fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 79fbf76683SChuck Lever * directory become the union of all files registered there. 80fbf76683SChuck Lever * 81fbf76683SChuck Lever * We simply need to make sure that we don't collide with 82fbf76683SChuck Lever * someone else's file names! 83fbf76683SChuck Lever */ 84fbf76683SChuck Lever 85fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 86fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 87d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 88fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 89fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 90fbf76683SChuck Lever 91fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 92fbf76683SChuck Lever 93fbf76683SChuck Lever /* 94fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 95fbf76683SChuck Lever * socket buffers for existing UDP transports 96fbf76683SChuck Lever */ 97fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 98fbf76683SChuck Lever { 99fbf76683SChuck Lever .procname = "udp_slot_table_entries", 100fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 101fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 102fbf76683SChuck Lever .mode = 0644, 1036d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 104fbf76683SChuck Lever .extra1 = &min_slot_table_size, 105fbf76683SChuck Lever .extra2 = &max_slot_table_size 106fbf76683SChuck Lever }, 107fbf76683SChuck Lever { 108fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 109fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 110fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 111fbf76683SChuck Lever .mode = 0644, 1126d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 113fbf76683SChuck Lever .extra1 = &min_slot_table_size, 114fbf76683SChuck Lever .extra2 = &max_slot_table_size 115fbf76683SChuck Lever }, 116fbf76683SChuck Lever { 117d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 118d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 119d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 120d9ba131dSTrond Myklebust .mode = 0644, 121d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 122d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 123d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 124d9ba131dSTrond Myklebust }, 125d9ba131dSTrond Myklebust { 126fbf76683SChuck Lever .procname = "min_resvport", 127fbf76683SChuck Lever .data = &xprt_min_resvport, 128fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 129fbf76683SChuck Lever .mode = 0644, 1306d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 131fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 132e08ea3a9SFrank Sorenson .extra2 = &xprt_max_resvport 133fbf76683SChuck Lever }, 134fbf76683SChuck Lever { 135fbf76683SChuck Lever .procname = "max_resvport", 136fbf76683SChuck Lever .data = &xprt_max_resvport, 137fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 138fbf76683SChuck Lever .mode = 0644, 1396d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 140e08ea3a9SFrank Sorenson .extra1 = &xprt_min_resvport, 141fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 142fbf76683SChuck Lever }, 143fbf76683SChuck Lever { 14425fe6142STrond Myklebust .procname = "tcp_fin_timeout", 14525fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 14625fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 14725fe6142STrond Myklebust .mode = 0644, 1486d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 14925fe6142STrond Myklebust }, 150f8572d8fSEric W. Biederman { }, 151fbf76683SChuck Lever }; 152fbf76683SChuck Lever 153fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = { 154fbf76683SChuck Lever { 155fbf76683SChuck Lever .procname = "sunrpc", 156fbf76683SChuck Lever .mode = 0555, 157fbf76683SChuck Lever .child = xs_tunables_table 158fbf76683SChuck Lever }, 159f8572d8fSEric W. Biederman { }, 160fbf76683SChuck Lever }; 161fbf76683SChuck Lever 162fbf76683SChuck Lever #endif 163fbf76683SChuck Lever 164fbf76683SChuck Lever /* 16503bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16603bf4b70SChuck Lever */ 16703bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16803bf4b70SChuck Lever 16903bf4b70SChuck Lever /* 17003bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 17103bf4b70SChuck Lever * kind of resource problem on the local host. 17203bf4b70SChuck Lever */ 17303bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17403bf4b70SChuck Lever 17503bf4b70SChuck Lever /* 17603bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17703bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17803bf4b70SChuck Lever * 17903bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 18003bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 18103bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18203bf4b70SChuck Lever * increase over time if the server is down or not responding. 18303bf4b70SChuck Lever */ 18403bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18503bf4b70SChuck Lever 18603bf4b70SChuck Lever /* 18703bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18803bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18903bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 19003bf4b70SChuck Lever */ 19103bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19203bf4b70SChuck Lever 193f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 194a246b010SChuck Lever # undef RPC_DEBUG_DATA 1959903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 196a246b010SChuck Lever #endif 197a246b010SChuck Lever 198a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1999903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 200a246b010SChuck Lever { 201a246b010SChuck Lever u8 *buf = (u8 *) packet; 202a246b010SChuck Lever int j; 203a246b010SChuck Lever 204a246b010SChuck Lever dprintk("RPC: %s\n", msg); 205a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 206a246b010SChuck Lever if (!(j & 31)) { 207a246b010SChuck Lever if (j) 208a246b010SChuck Lever dprintk("\n"); 209a246b010SChuck Lever dprintk("0x%04x ", j); 210a246b010SChuck Lever } 211a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 212a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 213a246b010SChuck Lever } 214a246b010SChuck Lever dprintk("\n"); 215a246b010SChuck Lever } 216a246b010SChuck Lever #else 2179903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 218a246b010SChuck Lever { 219a246b010SChuck Lever /* NOP */ 220a246b010SChuck Lever } 221a246b010SChuck Lever #endif 222a246b010SChuck Lever 2232118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2242118071dSTrond Myklebust { 2252118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2262118071dSTrond Myklebust } 2272118071dSTrond Myklebust 22895392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 229edb267a6SChuck Lever { 23095392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 23195392c59SChuck Lever } 23295392c59SChuck Lever 233176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 234176e21eeSChuck Lever { 235176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 236176e21eeSChuck Lever } 237176e21eeSChuck Lever 23895392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 23995392c59SChuck Lever { 24095392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 24195392c59SChuck Lever } 24295392c59SChuck Lever 24395392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 24495392c59SChuck Lever { 24595392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 24695392c59SChuck Lever } 24795392c59SChuck Lever 248c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 249c877b849SChuck Lever { 250c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2519dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2529dc3b095SChuck Lever struct sockaddr_in *sin; 253176e21eeSChuck Lever struct sockaddr_un *sun; 254c877b849SChuck Lever char buf[128]; 255c877b849SChuck Lever 2569dc3b095SChuck Lever switch (sap->sa_family) { 257176e21eeSChuck Lever case AF_LOCAL: 258176e21eeSChuck Lever sun = xs_addr_un(xprt); 259176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 260176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 261176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 262176e21eeSChuck Lever break; 2639dc3b095SChuck Lever case AF_INET: 264176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 265176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 266176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2679dc3b095SChuck Lever sin = xs_addr_in(xprt); 268fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2699dc3b095SChuck Lever break; 2709dc3b095SChuck Lever case AF_INET6: 271176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 272176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 273176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2749dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 275fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2769dc3b095SChuck Lever break; 2779dc3b095SChuck Lever default: 2789dc3b095SChuck Lever BUG(); 2799dc3b095SChuck Lever } 280176e21eeSChuck Lever 2819dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2829dc3b095SChuck Lever } 2839dc3b095SChuck Lever 2849dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2859dc3b095SChuck Lever { 2869dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2879dc3b095SChuck Lever char buf[128]; 2889dc3b095SChuck Lever 28981160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 290c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 291c877b849SChuck Lever 29281160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 293c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 294c877b849SChuck Lever } 295c877b849SChuck Lever 2969dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 297b454ae90SChuck Lever const char *protocol, 298b454ae90SChuck Lever const char *netid) 299edb267a6SChuck Lever { 300b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 301b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 302c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3039dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 304edb267a6SChuck Lever } 305edb267a6SChuck Lever 3069dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3074b6473fbSChuck Lever { 3089dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3099dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3104b6473fbSChuck Lever 3119dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 312edb267a6SChuck Lever } 313edb267a6SChuck Lever 314edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 315edb267a6SChuck Lever { 31633e01dc7SChuck Lever unsigned int i; 31733e01dc7SChuck Lever 31833e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 31933e01dc7SChuck Lever switch (i) { 32033e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 32133e01dc7SChuck Lever case RPC_DISPLAY_NETID: 32233e01dc7SChuck Lever continue; 32333e01dc7SChuck Lever default: 32433e01dc7SChuck Lever kfree(xprt->address_strings[i]); 32533e01dc7SChuck Lever } 326edb267a6SChuck Lever } 327edb267a6SChuck Lever 328b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 329b4b5cc85SChuck Lever 33024c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 331b4b5cc85SChuck Lever { 332b4b5cc85SChuck Lever struct msghdr msg = { 333b4b5cc85SChuck Lever .msg_name = addr, 334b4b5cc85SChuck Lever .msg_namelen = addrlen, 33524c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 33624c5684bSTrond Myklebust }; 33724c5684bSTrond Myklebust struct kvec iov = { 33824c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 33924c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 340b4b5cc85SChuck Lever }; 341b4b5cc85SChuck Lever 34224c5684bSTrond Myklebust if (iov.iov_len != 0) 343b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 344b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 345b4b5cc85SChuck Lever } 346b4b5cc85SChuck Lever 347f279cd00SJason Baron static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p) 348b4b5cc85SChuck Lever { 349a6b31d18STrond Myklebust ssize_t (*do_sendpage)(struct socket *sock, struct page *page, 350a6b31d18STrond Myklebust int offset, size_t size, int flags); 35124c5684bSTrond Myklebust struct page **ppage; 35224c5684bSTrond Myklebust unsigned int remainder; 353f279cd00SJason Baron int err; 354b4b5cc85SChuck Lever 35524c5684bSTrond Myklebust remainder = xdr->page_len - base; 35624c5684bSTrond Myklebust base += xdr->page_base; 35724c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 35824c5684bSTrond Myklebust base &= ~PAGE_MASK; 359a6b31d18STrond Myklebust do_sendpage = sock->ops->sendpage; 360a6b31d18STrond Myklebust if (!zerocopy) 361a6b31d18STrond Myklebust do_sendpage = sock_no_sendpage; 36224c5684bSTrond Myklebust for(;;) { 36324c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 36424c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 36524c5684bSTrond Myklebust 36624c5684bSTrond Myklebust remainder -= len; 36731303d6cSTrond Myklebust if (more) 36824c5684bSTrond Myklebust flags |= MSG_MORE; 36931303d6cSTrond Myklebust if (remainder != 0) 37031303d6cSTrond Myklebust flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE; 371a6b31d18STrond Myklebust err = do_sendpage(sock, *ppage, base, len, flags); 37224c5684bSTrond Myklebust if (remainder == 0 || err != len) 37324c5684bSTrond Myklebust break; 374f279cd00SJason Baron *sent_p += err; 37524c5684bSTrond Myklebust ppage++; 37624c5684bSTrond Myklebust base = 0; 37724c5684bSTrond Myklebust } 378f279cd00SJason Baron if (err > 0) { 379f279cd00SJason Baron *sent_p += err; 380f279cd00SJason Baron err = 0; 381f279cd00SJason Baron } 38224c5684bSTrond Myklebust return err; 383b4b5cc85SChuck Lever } 384b4b5cc85SChuck Lever 3859903cd1cSChuck Lever /** 3869903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 3879903cd1cSChuck Lever * @sock: socket to send on 3889903cd1cSChuck Lever * @addr: UDP only -- address of destination 3899903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 3909903cd1cSChuck Lever * @xdr: buffer containing this request 3919903cd1cSChuck Lever * @base: starting position in the buffer 392a6b31d18STrond Myklebust * @zerocopy: true if it is safe to use sendpage() 393f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 3949903cd1cSChuck Lever * 395a246b010SChuck Lever */ 396f279cd00SJason 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) 397a246b010SChuck Lever { 39824c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 399f279cd00SJason Baron int err = 0; 400f279cd00SJason Baron int sent = 0; 401a246b010SChuck Lever 402262965f5SChuck Lever if (unlikely(!sock)) 403fba91afbSTrond Myklebust return -ENOTSOCK; 404262965f5SChuck Lever 40524c5684bSTrond Myklebust if (base != 0) { 40624c5684bSTrond Myklebust addr = NULL; 40724c5684bSTrond Myklebust addrlen = 0; 40824c5684bSTrond Myklebust } 409262965f5SChuck Lever 41024c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 41124c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 41224c5684bSTrond Myklebust remainder -= len; 41324c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 41424c5684bSTrond Myklebust if (remainder == 0 || err != len) 415a246b010SChuck Lever goto out; 416f279cd00SJason Baron *sent_p += err; 417a246b010SChuck Lever base = 0; 418a246b010SChuck Lever } else 41924c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 420a246b010SChuck Lever 42124c5684bSTrond Myklebust if (base < xdr->page_len) { 42224c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 42324c5684bSTrond Myklebust remainder -= len; 424f279cd00SJason Baron err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); 425f279cd00SJason Baron *sent_p += sent; 426f279cd00SJason Baron if (remainder == 0 || sent != len) 427a246b010SChuck Lever goto out; 428a246b010SChuck Lever base = 0; 42924c5684bSTrond Myklebust } else 43024c5684bSTrond Myklebust base -= xdr->page_len; 43124c5684bSTrond Myklebust 43224c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 433f279cd00SJason Baron return 0; 43424c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 435a246b010SChuck Lever out: 436f279cd00SJason Baron if (err > 0) { 437f279cd00SJason Baron *sent_p += err; 438f279cd00SJason Baron err = 0; 439f279cd00SJason Baron } 44024c5684bSTrond Myklebust return err; 441a246b010SChuck Lever } 442a246b010SChuck Lever 443b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 444b6ddf64fSTrond Myklebust { 445b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 446b6ddf64fSTrond Myklebust 447b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 448b6ddf64fSTrond Myklebust } 449b6ddf64fSTrond Myklebust 4509903cd1cSChuck Lever /** 451262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 452262965f5SChuck Lever * @task: task to put to sleep 4539903cd1cSChuck Lever * 454a246b010SChuck Lever */ 4555e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 456a246b010SChuck Lever { 457262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 458262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 459ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 46006ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 46124ca9a84STrond Myklebust int ret = -EAGAIN; 462a246b010SChuck Lever 46346121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 4646c7a64e5STrond Myklebust task->tk_pid, req->rq_slen - transport->xmit.offset, 465262965f5SChuck Lever req->rq_slen); 466a246b010SChuck Lever 467262965f5SChuck Lever /* Protect against races with write_space */ 468262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 469a246b010SChuck Lever 470262965f5SChuck Lever /* Don't race with disconnect */ 471b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 47213331a55STrond Myklebust /* wait for more buffer space */ 47306ea0bfeSTrond Myklebust sk->sk_write_pending++; 474b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 47513331a55STrond Myklebust } else 4765e3771ceSTrond Myklebust ret = -ENOTCONN; 477a246b010SChuck Lever 478262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 47906ea0bfeSTrond Myklebust 48006ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 481d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 482d48f9ce7SDavid Vrabel struct socket_wq *wq; 483d48f9ce7SDavid Vrabel 484d48f9ce7SDavid Vrabel rcu_read_lock(); 485d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 486d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 487d48f9ce7SDavid Vrabel rcu_read_unlock(); 488d48f9ce7SDavid Vrabel 48906ea0bfeSTrond Myklebust sk->sk_write_space(sk); 490d48f9ce7SDavid Vrabel } 4915e3771ceSTrond Myklebust return ret; 492a246b010SChuck Lever } 493a246b010SChuck Lever 49461677eeeSChuck Lever /* 4954cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 4964cd34e7cSTrond Myklebust * could complete transmission. 4974cd34e7cSTrond Myklebust */ 4984cd34e7cSTrond Myklebust static bool 4994cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 5004cd34e7cSTrond Myklebust { 5014cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 5024cd34e7cSTrond Myklebust } 5034cd34e7cSTrond Myklebust 5044cd34e7cSTrond Myklebust /* 50561677eeeSChuck Lever * Construct a stream transport record marker in @buf. 50661677eeeSChuck Lever */ 50761677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 50861677eeeSChuck Lever { 50961677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 51061677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 51161677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 51261677eeeSChuck Lever } 51361677eeeSChuck Lever 5149903cd1cSChuck Lever /** 515176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 516176e21eeSChuck Lever * @task: RPC task that manages the state of an RPC request 517176e21eeSChuck Lever * 518176e21eeSChuck Lever * Return values: 519176e21eeSChuck Lever * 0: The request has been sent 520176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 521176e21eeSChuck Lever * complete the request 522176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 523176e21eeSChuck Lever * other: Some other error occured, the request was not sent 524176e21eeSChuck Lever */ 525176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task) 526176e21eeSChuck Lever { 527176e21eeSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 528176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 529176e21eeSChuck Lever struct sock_xprt *transport = 530176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 531176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 532176e21eeSChuck Lever int status; 533f279cd00SJason Baron int sent = 0; 534176e21eeSChuck Lever 5354cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 5364cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 5374cd34e7cSTrond Myklebust xs_close(xprt); 5384cd34e7cSTrond Myklebust return -ENOTCONN; 5394cd34e7cSTrond Myklebust } 5404cd34e7cSTrond Myklebust 541176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 542176e21eeSChuck Lever 543176e21eeSChuck Lever xs_pktdump("packet data:", 544176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 545176e21eeSChuck Lever 54678215759SChuck Lever req->rq_xtime = ktime_get(); 5476c7a64e5STrond Myklebust status = xs_sendpages(transport->sock, NULL, 0, xdr, 5486c7a64e5STrond Myklebust transport->xmit.offset, 549f279cd00SJason Baron true, &sent); 550176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 5516c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 552743c69e7SNeilBrown 553743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 554743c69e7SNeilBrown status = -ENOBUFS; 555743c69e7SNeilBrown 556f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 5576c7a64e5STrond Myklebust transport->xmit.offset += sent; 5586c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 559176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 5606c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 561176e21eeSChuck Lever req->rq_bytes_sent = 0; 5626c7a64e5STrond Myklebust transport->xmit.offset = 0; 563176e21eeSChuck Lever return 0; 564176e21eeSChuck Lever } 565176e21eeSChuck Lever status = -EAGAIN; 566176e21eeSChuck Lever } 567176e21eeSChuck Lever 568176e21eeSChuck Lever switch (status) { 5693601c4a9STrond Myklebust case -ENOBUFS: 570b5872f0cSTrond Myklebust break; 571176e21eeSChuck Lever case -EAGAIN: 572176e21eeSChuck Lever status = xs_nospace(task); 573176e21eeSChuck Lever break; 574176e21eeSChuck Lever default: 575176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 576176e21eeSChuck Lever -status); 577e9d47639SGustavo A. R. Silva /* fall through */ 578176e21eeSChuck Lever case -EPIPE: 579176e21eeSChuck Lever xs_close(xprt); 580176e21eeSChuck Lever status = -ENOTCONN; 581176e21eeSChuck Lever } 582176e21eeSChuck Lever 583176e21eeSChuck Lever return status; 584176e21eeSChuck Lever } 585176e21eeSChuck Lever 586176e21eeSChuck Lever /** 587262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5889903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5899903cd1cSChuck Lever * 5909903cd1cSChuck Lever * Return values: 5919903cd1cSChuck Lever * 0: The request has been sent 5929903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5939903cd1cSChuck Lever * complete the request 594262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 59525985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 5969903cd1cSChuck Lever */ 597262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 598a246b010SChuck Lever { 599a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 600a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 601ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 602262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 603f279cd00SJason Baron int sent = 0; 604262965f5SChuck Lever int status; 605262965f5SChuck Lever 606262965f5SChuck Lever xs_pktdump("packet data:", 607262965f5SChuck Lever req->rq_svec->iov_base, 608262965f5SChuck Lever req->rq_svec->iov_len); 609262965f5SChuck Lever 61001d37c42STrond Myklebust if (!xprt_bound(xprt)) 61101d37c42STrond Myklebust return -ENOTCONN; 61278215759SChuck Lever req->rq_xtime = ktime_get(); 613f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 6146c7a64e5STrond Myklebust xdr, 0, true, &sent); 615262965f5SChuck Lever 616262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 6176c7a64e5STrond Myklebust xdr->len, status); 618262965f5SChuck Lever 6193dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 6203dedbb5cSJason Baron if (status == -EPERM) 6213dedbb5cSJason Baron goto process_status; 6223dedbb5cSJason Baron 623743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 624743c69e7SNeilBrown status = -ENOBUFS; 625743c69e7SNeilBrown 626f279cd00SJason Baron if (sent > 0 || status == 0) { 627f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 628f279cd00SJason Baron if (sent >= req->rq_slen) 629262965f5SChuck Lever return 0; 630262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 631262965f5SChuck Lever status = -EAGAIN; 6322199700fSTrond Myklebust } 633262965f5SChuck Lever 6343dedbb5cSJason Baron process_status: 635262965f5SChuck Lever switch (status) { 636fba91afbSTrond Myklebust case -ENOTSOCK: 637fba91afbSTrond Myklebust status = -ENOTCONN; 638fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 639fba91afbSTrond Myklebust break; 640b6ddf64fSTrond Myklebust case -EAGAIN: 6415e3771ceSTrond Myklebust status = xs_nospace(task); 642b6ddf64fSTrond Myklebust break; 643262965f5SChuck Lever case -ENETUNREACH: 6443601c4a9STrond Myklebust case -ENOBUFS: 645262965f5SChuck Lever case -EPIPE: 646262965f5SChuck Lever case -ECONNREFUSED: 6473dedbb5cSJason Baron case -EPERM: 648262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 649262965f5SChuck Lever * prompts ECONNREFUSED. */ 65013331a55STrond Myklebust break; 65113331a55STrond Myklebust default: 65213331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 65313331a55STrond Myklebust -status); 654262965f5SChuck Lever } 6555fe46e9dSBian Naimeng 656262965f5SChuck Lever return status; 657262965f5SChuck Lever } 658262965f5SChuck Lever 659e06799f9STrond Myklebust /** 660262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 661262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 662262965f5SChuck Lever * 663262965f5SChuck Lever * Return values: 664262965f5SChuck Lever * 0: The request has been sent 665262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 666262965f5SChuck Lever * complete the request 667262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 66825985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 669262965f5SChuck Lever * 670262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 671262965f5SChuck Lever * if sendmsg is not able to make progress? 672262965f5SChuck Lever */ 673262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 674262965f5SChuck Lever { 675262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 676262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 677ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 678262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 679a6b31d18STrond Myklebust bool zerocopy = true; 6809ffadfbcSTrond Myklebust bool vm_wait = false; 681b595bb15SChuck Lever int status; 682f279cd00SJason Baron int sent; 683a246b010SChuck Lever 6844cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 6854cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 6864cd34e7cSTrond Myklebust if (transport->sock != NULL) 6874cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 6884cd34e7cSTrond Myklebust return -ENOTCONN; 6894cd34e7cSTrond Myklebust } 6904cd34e7cSTrond Myklebust 69161677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 692262965f5SChuck Lever 693262965f5SChuck Lever xs_pktdump("packet data:", 694262965f5SChuck Lever req->rq_svec->iov_base, 695262965f5SChuck Lever req->rq_svec->iov_len); 696a6b31d18STrond Myklebust /* Don't use zero copy if this is a resend. If the RPC call 697a6b31d18STrond Myklebust * completes while the socket holds a reference to the pages, 698a6b31d18STrond Myklebust * then we may end up resending corrupted data. 699a6b31d18STrond Myklebust */ 700a6b31d18STrond Myklebust if (task->tk_flags & RPC_TASK_SENT) 701a6b31d18STrond Myklebust zerocopy = false; 702a246b010SChuck Lever 7037196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 7047196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 7057196dbb0STrond Myklebust 706a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 707a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 708262965f5SChuck Lever * called sendmsg(). */ 70978215759SChuck Lever req->rq_xtime = ktime_get(); 710a246b010SChuck Lever while (1) { 711f279cd00SJason Baron sent = 0; 712f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 7136c7a64e5STrond Myklebust transport->xmit.offset, 7146c7a64e5STrond Myklebust zerocopy, &sent); 715a246b010SChuck Lever 716262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 7176c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 718262965f5SChuck Lever 719a246b010SChuck Lever /* If we've sent the entire packet, immediately 720a246b010SChuck Lever * reset the count of bytes sent. */ 7216c7a64e5STrond Myklebust transport->xmit.offset += sent; 7226c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 723262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 7246c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 725a246b010SChuck Lever req->rq_bytes_sent = 0; 7266c7a64e5STrond Myklebust transport->xmit.offset = 0; 727a246b010SChuck Lever return 0; 728a246b010SChuck Lever } 729262965f5SChuck Lever 7309ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 7319ffadfbcSTrond Myklebust 7329ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 7339ffadfbcSTrond Myklebust /* 7349ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 7359ffadfbcSTrond Myklebust * socket send buffer limits. 7369ffadfbcSTrond Myklebust */ 7379ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 7389ffadfbcSTrond Myklebust break; 7399ffadfbcSTrond Myklebust /* 7409ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 7419ffadfbcSTrond Myklebust */ 7429ffadfbcSTrond Myklebust if (sent == 0) { 7439ffadfbcSTrond Myklebust status = -ENOBUFS; 7449ffadfbcSTrond Myklebust if (vm_wait) 7459ffadfbcSTrond Myklebust break; 7469ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 7479ffadfbcSTrond Myklebust * socket send buffer limit 7489ffadfbcSTrond Myklebust */ 7499ffadfbcSTrond Myklebust vm_wait = true; 7509ffadfbcSTrond Myklebust } 7519ffadfbcSTrond Myklebust continue; 7529ffadfbcSTrond Myklebust } 753f580dd04STrond Myklebust if (status < 0) 754f580dd04STrond Myklebust break; 7559ffadfbcSTrond Myklebust vm_wait = false; 756a246b010SChuck Lever } 757a246b010SChuck Lever 758262965f5SChuck Lever switch (status) { 759fba91afbSTrond Myklebust case -ENOTSOCK: 760fba91afbSTrond Myklebust status = -ENOTCONN; 761fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 762fba91afbSTrond Myklebust break; 763262965f5SChuck Lever case -EAGAIN: 7645e3771ceSTrond Myklebust status = xs_nospace(task); 765262965f5SChuck Lever break; 766262965f5SChuck Lever case -ECONNRESET: 767262965f5SChuck Lever case -ECONNREFUSED: 768262965f5SChuck Lever case -ENOTCONN: 7693913c78cSTrond Myklebust case -EADDRINUSE: 770b5872f0cSTrond Myklebust case -ENOBUFS: 771b9d2bb2eSTrond Myklebust case -EPIPE: 77213331a55STrond Myklebust break; 77313331a55STrond Myklebust default: 77413331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 77513331a55STrond Myklebust -status); 776a246b010SChuck Lever } 7775fe46e9dSBian Naimeng 778a246b010SChuck Lever return status; 779a246b010SChuck Lever } 780a246b010SChuck Lever 7812a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7822a9e1cfaSTrond Myklebust { 7832a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7842a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7852a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7862118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7872a9e1cfaSTrond Myklebust } 7882a9e1cfaSTrond Myklebust 7892a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7902a9e1cfaSTrond Myklebust { 7912a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7922a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7932a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7942118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7952118071dSTrond Myklebust } 7962118071dSTrond Myklebust 79742d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 79842d42a5bSTrond Myklebust { 79942d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 80042d42a5bSTrond Myklebust 80142d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 80242d42a5bSTrond Myklebust } 80342d42a5bSTrond Myklebust 804b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 805b70ae915STrond Myklebust { 806b70ae915STrond Myklebust smp_mb__before_atomic(); 807b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 808b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 80942d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 810b70ae915STrond Myklebust smp_mb__after_atomic(); 811b70ae915STrond Myklebust } 812b70ae915STrond Myklebust 8132118071dSTrond Myklebust /** 8142118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 8152118071dSTrond Myklebust * @sk: socket 8162118071dSTrond Myklebust * 8172118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 8182118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 8192118071dSTrond Myklebust */ 8202118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 8212118071dSTrond Myklebust { 8222118071dSTrond Myklebust struct rpc_xprt *xprt; 8232118071dSTrond Myklebust int err; 8242118071dSTrond Myklebust 8252118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 8262118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 8272118071dSTrond Myklebust goto out; 8282118071dSTrond Myklebust 8292118071dSTrond Myklebust err = -sk->sk_err; 8302118071dSTrond Myklebust if (err == 0) 8312118071dSTrond Myklebust goto out; 8322118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 8332118071dSTrond Myklebust xprt, -err); 834e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 8352118071dSTrond Myklebust xprt_wake_pending_tasks(xprt, err); 8362118071dSTrond Myklebust out: 8372118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 8382a9e1cfaSTrond Myklebust } 8392a9e1cfaSTrond Myklebust 840fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 841a246b010SChuck Lever { 842ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 843ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 8446cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 845a246b010SChuck Lever 846fe315e76SChuck Lever if (sk == NULL) 847fe315e76SChuck Lever return; 8489903cd1cSChuck Lever 849264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 850264d1df3SJeff Layton sk_clear_memalloc(sk); 851264d1df3SJeff Layton 85209939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 85309939204STrond Myklebust 854edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 855a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 856ee0ac0c2SChuck Lever transport->inet = NULL; 857ee0ac0c2SChuck Lever transport->sock = NULL; 858a246b010SChuck Lever 859a246b010SChuck Lever sk->sk_user_data = NULL; 8602a9e1cfaSTrond Myklebust 8612a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 8620c78789eSTrond Myklebust xprt_clear_connected(xprt); 863a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 8646cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 865edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 866a246b010SChuck Lever 8676cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 868a246b010SChuck Lever sock_release(sock); 869fe315e76SChuck Lever } 870fe315e76SChuck Lever 871fe315e76SChuck Lever /** 872fe315e76SChuck Lever * xs_close - close a socket 873fe315e76SChuck Lever * @xprt: transport 874fe315e76SChuck Lever * 875fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 876fe315e76SChuck Lever * on the server we want to save. 877f75e6745STrond Myklebust * 878f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 879f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 880fe315e76SChuck Lever */ 881fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 882fe315e76SChuck Lever { 883fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 884fe315e76SChuck Lever 885fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 886fe315e76SChuck Lever 887fe315e76SChuck Lever xs_reset_transport(transport); 88861d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 889fe315e76SChuck Lever 89062da3b24STrond Myklebust xprt_disconnect_done(xprt); 891a246b010SChuck Lever } 892a246b010SChuck Lever 8934a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 8944a068258SChuck Lever { 8954a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 8964a068258SChuck Lever xprt); 8974a068258SChuck Lever xprt_disconnect_done(xprt); 8984a068258SChuck Lever } 8994a068258SChuck Lever 900315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 901315f3812SKinglong Mee { 902315f3812SKinglong Mee xs_free_peer_addresses(xprt); 903315f3812SKinglong Mee xprt_free(xprt); 904315f3812SKinglong Mee } 905315f3812SKinglong Mee 9069903cd1cSChuck Lever /** 9079903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 9089903cd1cSChuck Lever * @xprt: doomed transport 9099903cd1cSChuck Lever * 9109903cd1cSChuck Lever */ 9119903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 912a246b010SChuck Lever { 91303c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 91403c78827STrond Myklebust struct sock_xprt, xprt); 9159903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 9169903cd1cSChuck Lever 91703c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 918a1311d87STrond Myklebust xs_close(xprt); 919edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 920315f3812SKinglong Mee xs_xprt_free(xprt); 921a1311d87STrond Myklebust module_put(THIS_MODULE); 922a246b010SChuck Lever } 923a246b010SChuck Lever 924176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) 925176e21eeSChuck Lever { 926176e21eeSChuck Lever struct xdr_skb_reader desc = { 927176e21eeSChuck Lever .skb = skb, 928176e21eeSChuck Lever .offset = sizeof(rpc_fraghdr), 929176e21eeSChuck Lever .count = skb->len - sizeof(rpc_fraghdr), 930176e21eeSChuck Lever }; 931176e21eeSChuck Lever 932176e21eeSChuck Lever if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) 933176e21eeSChuck Lever return -1; 934176e21eeSChuck Lever if (desc.count) 935176e21eeSChuck Lever return -1; 936176e21eeSChuck Lever return 0; 937176e21eeSChuck Lever } 938176e21eeSChuck Lever 939176e21eeSChuck Lever /** 940a2648094STrond Myklebust * xs_local_data_read_skb 941a2648094STrond Myklebust * @xprt: transport 942a2648094STrond Myklebust * @sk: socket 943a2648094STrond Myklebust * @skb: skbuff 944176e21eeSChuck Lever * 945176e21eeSChuck Lever * Currently this assumes we can read the whole reply in a single gulp. 946176e21eeSChuck Lever */ 947a2648094STrond Myklebust static void xs_local_data_read_skb(struct rpc_xprt *xprt, 948a2648094STrond Myklebust struct sock *sk, 949a2648094STrond Myklebust struct sk_buff *skb) 950176e21eeSChuck Lever { 951176e21eeSChuck Lever struct rpc_task *task; 952176e21eeSChuck Lever struct rpc_rqst *rovr; 953a2648094STrond Myklebust int repsize, copied; 954176e21eeSChuck Lever u32 _xid; 955176e21eeSChuck Lever __be32 *xp; 956176e21eeSChuck Lever 957176e21eeSChuck Lever repsize = skb->len - sizeof(rpc_fraghdr); 958176e21eeSChuck Lever if (repsize < 4) { 959176e21eeSChuck Lever dprintk("RPC: impossible RPC reply size %d\n", repsize); 960a2648094STrond Myklebust return; 961176e21eeSChuck Lever } 962176e21eeSChuck Lever 963176e21eeSChuck Lever /* Copy the XID from the skb... */ 964176e21eeSChuck Lever xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); 965176e21eeSChuck Lever if (xp == NULL) 966a2648094STrond Myklebust return; 967176e21eeSChuck Lever 968176e21eeSChuck Lever /* Look up and lock the request corresponding to the given XID */ 969*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 970176e21eeSChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 971176e21eeSChuck Lever if (!rovr) 972176e21eeSChuck Lever goto out_unlock; 973729749bbSTrond Myklebust xprt_pin_rqst(rovr); 974*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 975176e21eeSChuck Lever task = rovr->rq_task; 976176e21eeSChuck Lever 977176e21eeSChuck Lever copied = rovr->rq_private_buf.buflen; 978176e21eeSChuck Lever if (copied > repsize) 979176e21eeSChuck Lever copied = repsize; 980176e21eeSChuck Lever 981176e21eeSChuck Lever if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { 982176e21eeSChuck Lever dprintk("RPC: sk_buff copy failed\n"); 983*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 984729749bbSTrond Myklebust goto out_unpin; 985176e21eeSChuck Lever } 986176e21eeSChuck Lever 987*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 988176e21eeSChuck Lever xprt_complete_rqst(task, copied); 989729749bbSTrond Myklebust out_unpin: 990729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 991176e21eeSChuck Lever out_unlock: 992*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 993a2648094STrond Myklebust } 994a2648094STrond Myklebust 995a2648094STrond Myklebust static void xs_local_data_receive(struct sock_xprt *transport) 996a2648094STrond Myklebust { 997a2648094STrond Myklebust struct sk_buff *skb; 998a2648094STrond Myklebust struct sock *sk; 999a2648094STrond Myklebust int err; 1000a2648094STrond Myklebust 10010af3442aSTrond Myklebust restart: 1002a2648094STrond Myklebust mutex_lock(&transport->recv_mutex); 1003a2648094STrond Myklebust sk = transport->inet; 1004a2648094STrond Myklebust if (sk == NULL) 1005a2648094STrond Myklebust goto out; 1006a2648094STrond Myklebust for (;;) { 1007a2648094STrond Myklebust skb = skb_recv_datagram(sk, 0, 1, &err); 100842d42a5bSTrond Myklebust if (skb != NULL) { 1009a2648094STrond Myklebust xs_local_data_read_skb(&transport->xprt, sk, skb); 1010176e21eeSChuck Lever skb_free_datagram(sk, skb); 101142d42a5bSTrond Myklebust continue; 101242d42a5bSTrond Myklebust } 101342d42a5bSTrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 101442d42a5bSTrond Myklebust break; 10150af3442aSTrond Myklebust if (need_resched()) { 10160af3442aSTrond Myklebust mutex_unlock(&transport->recv_mutex); 10170af3442aSTrond Myklebust cond_resched(); 10180af3442aSTrond Myklebust goto restart; 10190af3442aSTrond Myklebust } 1020a2648094STrond Myklebust } 1021176e21eeSChuck Lever out: 1022a2648094STrond Myklebust mutex_unlock(&transport->recv_mutex); 1023a2648094STrond Myklebust } 1024a2648094STrond Myklebust 1025a2648094STrond Myklebust static void xs_local_data_receive_workfn(struct work_struct *work) 1026a2648094STrond Myklebust { 1027a2648094STrond Myklebust struct sock_xprt *transport = 1028a2648094STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1029a2648094STrond Myklebust xs_local_data_receive(transport); 1030176e21eeSChuck Lever } 1031176e21eeSChuck Lever 10329903cd1cSChuck Lever /** 1033f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1034f9b2ee71STrond Myklebust * @xprt: transport 1035f9b2ee71STrond Myklebust * @sk: socket 1036f9b2ee71STrond Myklebust * @skb: skbuff 10379903cd1cSChuck Lever * 1038a246b010SChuck Lever */ 1039f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1040f9b2ee71STrond Myklebust struct sock *sk, 1041f9b2ee71STrond Myklebust struct sk_buff *skb) 1042a246b010SChuck Lever { 1043a246b010SChuck Lever struct rpc_task *task; 1044a246b010SChuck Lever struct rpc_rqst *rovr; 1045f9b2ee71STrond Myklebust int repsize, copied; 1046d8ed029dSAlexey Dobriyan u32 _xid; 1047d8ed029dSAlexey Dobriyan __be32 *xp; 1048a246b010SChuck Lever 10491da8c681SWillem de Bruijn repsize = skb->len; 1050a246b010SChuck Lever if (repsize < 4) { 10519903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1052f9b2ee71STrond Myklebust return; 1053a246b010SChuck Lever } 1054a246b010SChuck Lever 1055a246b010SChuck Lever /* Copy the XID from the skb... */ 10561da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1057a246b010SChuck Lever if (xp == NULL) 1058f9b2ee71STrond Myklebust return; 1059a246b010SChuck Lever 1060a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 1061*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1062a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1063a246b010SChuck Lever if (!rovr) 1064a246b010SChuck Lever goto out_unlock; 1065729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1066ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 1067*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1068a246b010SChuck Lever task = rovr->rq_task; 1069a246b010SChuck Lever 1070a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1071a246b010SChuck Lever copied = repsize; 1072a246b010SChuck Lever 1073a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 10741781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 1075*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 10760afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1077729749bbSTrond Myklebust goto out_unpin; 10781781f7f5SHerbert Xu } 10791781f7f5SHerbert Xu 1080a246b010SChuck Lever 1081729749bbSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 10826a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1083ce7c252aSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 1084*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 10851570c1e4SChuck Lever xprt_complete_rqst(task, copied); 10860afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1087729749bbSTrond Myklebust out_unpin: 1088729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1089a246b010SChuck Lever out_unlock: 1090*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1091f9b2ee71STrond Myklebust } 1092f9b2ee71STrond Myklebust 1093f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1094f9b2ee71STrond Myklebust { 1095f9b2ee71STrond Myklebust struct sk_buff *skb; 1096f9b2ee71STrond Myklebust struct sock *sk; 1097f9b2ee71STrond Myklebust int err; 1098f9b2ee71STrond Myklebust 10990af3442aSTrond Myklebust restart: 1100f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1101f9b2ee71STrond Myklebust sk = transport->inet; 1102f9b2ee71STrond Myklebust if (sk == NULL) 1103f9b2ee71STrond Myklebust goto out; 1104f9b2ee71STrond Myklebust for (;;) { 11057c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 110642d42a5bSTrond Myklebust if (skb != NULL) { 1107f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1108850cbaddSPaolo Abeni consume_skb(skb); 110942d42a5bSTrond Myklebust continue; 111042d42a5bSTrond Myklebust } 111142d42a5bSTrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 111242d42a5bSTrond Myklebust break; 11130af3442aSTrond Myklebust if (need_resched()) { 11140af3442aSTrond Myklebust mutex_unlock(&transport->recv_mutex); 11150af3442aSTrond Myklebust cond_resched(); 11160af3442aSTrond Myklebust goto restart; 11170af3442aSTrond Myklebust } 1118f9b2ee71STrond Myklebust } 1119a246b010SChuck Lever out: 1120f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1121f9b2ee71STrond Myklebust } 1122f9b2ee71STrond Myklebust 1123f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1124f9b2ee71STrond Myklebust { 1125f9b2ee71STrond Myklebust struct sock_xprt *transport = 1126f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1127f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1128f9b2ee71STrond Myklebust } 1129f9b2ee71STrond Myklebust 1130f9b2ee71STrond Myklebust /** 1131f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1132f9b2ee71STrond Myklebust * @sk: socket with data to read 1133f9b2ee71STrond Myklebust * 1134f9b2ee71STrond Myklebust */ 1135f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1136f9b2ee71STrond Myklebust { 1137f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1138f9b2ee71STrond Myklebust 1139f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1140f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1141f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1142f9b2ee71STrond Myklebust if (xprt != NULL) { 1143f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1144f9b2ee71STrond Myklebust struct sock_xprt, xprt); 11455157b956STrond Myklebust transport->old_data_ready(sk); 11465157b956STrond Myklebust /* Any data means we had a useful conversation, so 11475157b956STrond Myklebust * then we don't need to delay the next reconnect 11485157b956STrond Myklebust */ 11495157b956STrond Myklebust if (xprt->reestablish_timeout) 11505157b956STrond Myklebust xprt->reestablish_timeout = 0; 115142d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 115240a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1153f9b2ee71STrond Myklebust } 1154f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1155a246b010SChuck Lever } 1156a246b010SChuck Lever 1157a519fc7aSTrond Myklebust /* 1158a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1159a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1160a519fc7aSTrond Myklebust */ 1161a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1162a519fc7aSTrond Myklebust { 1163a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1164a519fc7aSTrond Myklebust } 1165a519fc7aSTrond Myklebust 1166dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1167a246b010SChuck Lever { 116851971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1169a246b010SChuck Lever size_t len, used; 1170a246b010SChuck Lever char *p; 1171a246b010SChuck Lever 1172d1109aa5STrond Myklebust p = ((char *) &transport->recv.fraghdr) + transport->recv.offset; 1173d1109aa5STrond Myklebust len = sizeof(transport->recv.fraghdr) - transport->recv.offset; 11749d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 1175d1109aa5STrond Myklebust transport->recv.offset += used; 1176a246b010SChuck Lever if (used != len) 1177a246b010SChuck Lever return; 1178808012fbSChuck Lever 1179d1109aa5STrond Myklebust transport->recv.len = ntohl(transport->recv.fraghdr); 1180d1109aa5STrond Myklebust if (transport->recv.len & RPC_LAST_STREAM_FRAGMENT) 1181d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_LAST_FRAG; 1182a246b010SChuck Lever else 1183d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_LAST_FRAG; 1184d1109aa5STrond Myklebust transport->recv.len &= RPC_FRAGMENT_SIZE_MASK; 1185808012fbSChuck Lever 1186d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_FRAGHDR; 1187d1109aa5STrond Myklebust transport->recv.offset = 0; 1188808012fbSChuck Lever 1189a246b010SChuck Lever /* Sanity check of the record length */ 1190d1109aa5STrond Myklebust if (unlikely(transport->recv.len < 8)) { 11919903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 1192a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 11939903cd1cSChuck Lever return; 1194a246b010SChuck Lever } 1195a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 1196d1109aa5STrond Myklebust transport->recv.len); 1197a246b010SChuck Lever } 1198a246b010SChuck Lever 119951971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 1200a246b010SChuck Lever { 1201d1109aa5STrond Myklebust if (transport->recv.offset == transport->recv.len) { 1202d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_FRAGHDR; 1203d1109aa5STrond Myklebust transport->recv.offset = 0; 1204d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_LAST_FRAG) { 1205d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1206d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_XID; 1207d1109aa5STrond Myklebust transport->recv.copied = 0; 1208a246b010SChuck Lever } 1209a246b010SChuck Lever } 1210a246b010SChuck Lever } 1211a246b010SChuck Lever 1212dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1213a246b010SChuck Lever { 1214a246b010SChuck Lever size_t len, used; 1215a246b010SChuck Lever char *p; 1216a246b010SChuck Lever 1217d1109aa5STrond Myklebust len = sizeof(transport->recv.xid) - transport->recv.offset; 12185b5e0928SAlexey Dobriyan dprintk("RPC: reading XID (%zu bytes)\n", len); 1219d1109aa5STrond Myklebust p = ((char *) &transport->recv.xid) + transport->recv.offset; 12209d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 1221d1109aa5STrond Myklebust transport->recv.offset += used; 1222a246b010SChuck Lever if (used != len) 1223a246b010SChuck Lever return; 1224d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_XID; 1225d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_READ_CALLDIR; 1226d1109aa5STrond Myklebust transport->recv.copied = 4; 122718dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 1228d1109aa5STrond Myklebust (transport->recv.flags & TCP_RPC_REPLY) ? "reply for" 122918dca02aSRicardo Labiaga : "request with", 1230d1109aa5STrond Myklebust ntohl(transport->recv.xid)); 123151971139SChuck Lever xs_tcp_check_fraghdr(transport); 1232a246b010SChuck Lever } 1233a246b010SChuck Lever 123418dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 123518dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1236a246b010SChuck Lever { 123718dca02aSRicardo Labiaga size_t len, used; 123818dca02aSRicardo Labiaga u32 offset; 1239b76ce561STrond Myklebust char *p; 124018dca02aSRicardo Labiaga 124118dca02aSRicardo Labiaga /* 1242d1109aa5STrond Myklebust * We want transport->recv.offset to be 8 at the end of this routine 124318dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 124418dca02aSRicardo Labiaga * When this function is called for the first time, 1245d1109aa5STrond Myklebust * transport->recv.offset is 4 (after having already read the xid). 124618dca02aSRicardo Labiaga */ 1247d1109aa5STrond Myklebust offset = transport->recv.offset - sizeof(transport->recv.xid); 1248d1109aa5STrond Myklebust len = sizeof(transport->recv.calldir) - offset; 12495b5e0928SAlexey Dobriyan dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len); 1250d1109aa5STrond Myklebust p = ((char *) &transport->recv.calldir) + offset; 1251b76ce561STrond Myklebust used = xdr_skb_read_bits(desc, p, len); 1252d1109aa5STrond Myklebust transport->recv.offset += used; 125318dca02aSRicardo Labiaga if (used != len) 125418dca02aSRicardo Labiaga return; 1255d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_READ_CALLDIR; 1256f4a2e418SRicardo Labiaga /* 1257f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1258f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1259f4a2e418SRicardo Labiaga */ 1260d1109aa5STrond Myklebust switch (ntohl(transport->recv.calldir)) { 1261b76ce561STrond Myklebust case RPC_REPLY: 1262d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_CALLDIR; 1263d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_DATA; 1264d1109aa5STrond Myklebust transport->recv.flags |= TCP_RPC_REPLY; 1265b76ce561STrond Myklebust break; 1266b76ce561STrond Myklebust case RPC_CALL: 1267d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_CALLDIR; 1268d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_DATA; 1269d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RPC_REPLY; 1270b76ce561STrond Myklebust break; 1271b76ce561STrond Myklebust default: 1272b76ce561STrond Myklebust dprintk("RPC: invalid request message type\n"); 1273a519fc7aSTrond Myklebust xs_tcp_force_close(&transport->xprt); 1274b76ce561STrond Myklebust } 127518dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 127618dca02aSRicardo Labiaga } 127718dca02aSRicardo Labiaga 127844b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 127944b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 128044b98efdSRicardo Labiaga struct rpc_rqst *req) 1281a246b010SChuck Lever { 128244b98efdSRicardo Labiaga struct sock_xprt *transport = 128344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1284a246b010SChuck Lever struct xdr_buf *rcvbuf; 1285a246b010SChuck Lever size_t len; 1286a246b010SChuck Lever ssize_t r; 1287a246b010SChuck Lever 1288a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1289f4a2e418SRicardo Labiaga 1290d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_CALLDIR) { 1291f4a2e418SRicardo Labiaga /* 1292f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1293f4a2e418SRicardo Labiaga */ 1294d1109aa5STrond Myklebust memcpy(rcvbuf->head[0].iov_base + transport->recv.copied, 1295d1109aa5STrond Myklebust &transport->recv.calldir, 1296d1109aa5STrond Myklebust sizeof(transport->recv.calldir)); 1297d1109aa5STrond Myklebust transport->recv.copied += sizeof(transport->recv.calldir); 1298d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_CALLDIR; 1299a246b010SChuck Lever } 1300a246b010SChuck Lever 1301a246b010SChuck Lever len = desc->count; 1302d1109aa5STrond Myklebust if (len > transport->recv.len - transport->recv.offset) 1303d1109aa5STrond Myklebust desc->count = transport->recv.len - transport->recv.offset; 1304d1109aa5STrond Myklebust r = xdr_partial_copy_from_skb(rcvbuf, transport->recv.copied, 13059d292316SChuck Lever desc, xdr_skb_read_bits); 1306a246b010SChuck Lever 1307040249dfSTrond Myklebust if (desc->count) { 1308a246b010SChuck Lever /* Error when copying to the receive buffer, 1309a246b010SChuck Lever * usually because we weren't able to allocate 1310a246b010SChuck Lever * additional buffer pages. All we can do now 1311e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1312a246b010SChuck Lever * will not receive any additional updates, 1313a246b010SChuck Lever * and time out. 1314a246b010SChuck Lever * Any remaining data from this record will 1315a246b010SChuck Lever * be discarded. 1316a246b010SChuck Lever */ 1317d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1318a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 1319d1109aa5STrond Myklebust ntohl(transport->recv.xid)); 1320d1109aa5STrond Myklebust dprintk("RPC: xprt = %p, recv.copied = %lu, " 1321d1109aa5STrond Myklebust "recv.offset = %u, recv.len = %u\n", 1322d1109aa5STrond Myklebust xprt, transport->recv.copied, 1323d1109aa5STrond Myklebust transport->recv.offset, transport->recv.len); 132444b98efdSRicardo Labiaga return; 1325a246b010SChuck Lever } 1326a246b010SChuck Lever 1327d1109aa5STrond Myklebust transport->recv.copied += r; 1328d1109aa5STrond Myklebust transport->recv.offset += r; 1329040249dfSTrond Myklebust desc->count = len - r; 1330040249dfSTrond Myklebust 13315b5e0928SAlexey Dobriyan dprintk("RPC: XID %08x read %zd bytes\n", 1332d1109aa5STrond Myklebust ntohl(transport->recv.xid), r); 1333d1109aa5STrond Myklebust dprintk("RPC: xprt = %p, recv.copied = %lu, recv.offset = %u, " 1334d1109aa5STrond Myklebust "recv.len = %u\n", xprt, transport->recv.copied, 1335d1109aa5STrond Myklebust transport->recv.offset, transport->recv.len); 1336a246b010SChuck Lever 1337d1109aa5STrond Myklebust if (transport->recv.copied == req->rq_private_buf.buflen) 1338d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1339d1109aa5STrond Myklebust else if (transport->recv.offset == transport->recv.len) { 1340d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_LAST_FRAG) 1341d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1342a246b010SChuck Lever } 134344b98efdSRicardo Labiaga } 134444b98efdSRicardo Labiaga 134544b98efdSRicardo Labiaga /* 134644b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 134744b98efdSRicardo Labiaga * tcp read code to read the data. 134844b98efdSRicardo Labiaga */ 134944b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 135044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 135144b98efdSRicardo Labiaga { 135244b98efdSRicardo Labiaga struct sock_xprt *transport = 135344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 135444b98efdSRicardo Labiaga struct rpc_rqst *req; 135544b98efdSRicardo Labiaga 1356d1109aa5STrond Myklebust dprintk("RPC: read reply XID %08x\n", ntohl(transport->recv.xid)); 135744b98efdSRicardo Labiaga 135844b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 1359*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1360d1109aa5STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 136144b98efdSRicardo Labiaga if (!req) { 136244b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 1363d1109aa5STrond Myklebust ntohl(transport->recv.xid)); 1364*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 136544b98efdSRicardo Labiaga return -1; 136644b98efdSRicardo Labiaga } 1367729749bbSTrond Myklebust xprt_pin_rqst(req); 1368*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 136944b98efdSRicardo Labiaga 137044b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 137144b98efdSRicardo Labiaga 1372*75c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1373d1109aa5STrond Myklebust if (!(transport->recv.flags & TCP_RCV_COPY_DATA)) 1374d1109aa5STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 1375729749bbSTrond Myklebust xprt_unpin_rqst(req); 1376*75c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 137744b98efdSRicardo Labiaga return 0; 137844b98efdSRicardo Labiaga } 137944b98efdSRicardo Labiaga 13809e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 138144b98efdSRicardo Labiaga /* 138244b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 138344b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 138444b98efdSRicardo Labiaga * queue. 138544b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 138644b98efdSRicardo Labiaga * connection and return -1. 138744b98efdSRicardo Labiaga */ 13882ea24497STrond Myklebust static int xs_tcp_read_callback(struct rpc_xprt *xprt, 138944b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 139044b98efdSRicardo Labiaga { 139144b98efdSRicardo Labiaga struct sock_xprt *transport = 139244b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 139344b98efdSRicardo Labiaga struct rpc_rqst *req; 139444b98efdSRicardo Labiaga 1395c89091c8STrond Myklebust /* Look up the request corresponding to the given XID */ 1396d1109aa5STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 139744b98efdSRicardo Labiaga if (req == NULL) { 139844b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 139944b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 140044b98efdSRicardo Labiaga return -1; 140144b98efdSRicardo Labiaga } 140244b98efdSRicardo Labiaga 140344b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 140444b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 140544b98efdSRicardo Labiaga 1406d1109aa5STrond Myklebust if (!(transport->recv.flags & TCP_RCV_COPY_DATA)) 1407d1109aa5STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 140844b98efdSRicardo Labiaga 140944b98efdSRicardo Labiaga return 0; 141044b98efdSRicardo Labiaga } 141144b98efdSRicardo Labiaga 141244b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 141344b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 141444b98efdSRicardo Labiaga { 141544b98efdSRicardo Labiaga struct sock_xprt *transport = 141644b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 141744b98efdSRicardo Labiaga 1418d1109aa5STrond Myklebust return (transport->recv.flags & TCP_RPC_REPLY) ? 141944b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 142044b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 142144b98efdSRicardo Labiaga } 142276566773SChuck Lever 142376566773SChuck Lever static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net) 142476566773SChuck Lever { 142576566773SChuck Lever int ret; 142676566773SChuck Lever 142776566773SChuck Lever ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0, 142876566773SChuck Lever SVC_SOCK_ANONYMOUS); 142976566773SChuck Lever if (ret < 0) 143076566773SChuck Lever return ret; 143176566773SChuck Lever return 0; 143276566773SChuck Lever } 14336b26cc8cSChuck Lever 14346b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14356b26cc8cSChuck Lever { 14366b26cc8cSChuck Lever return PAGE_SIZE; 14376b26cc8cSChuck Lever } 143844b98efdSRicardo Labiaga #else 143944b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 144044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 144144b98efdSRicardo Labiaga { 144244b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 144344b98efdSRicardo Labiaga } 14449e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 144544b98efdSRicardo Labiaga 144644b98efdSRicardo Labiaga /* 144744b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 144844b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 144944b98efdSRicardo Labiaga */ 145044b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 145144b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 145244b98efdSRicardo Labiaga { 145344b98efdSRicardo Labiaga struct sock_xprt *transport = 145444b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 145544b98efdSRicardo Labiaga 145644b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 145751971139SChuck Lever xs_tcp_check_fraghdr(transport); 145844b98efdSRicardo Labiaga else { 145944b98efdSRicardo Labiaga /* 146044b98efdSRicardo Labiaga * The transport_lock protects the request handling. 1461d1109aa5STrond Myklebust * There's no need to hold it to update the recv.flags. 146244b98efdSRicardo Labiaga */ 1463d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 146444b98efdSRicardo Labiaga } 1465a246b010SChuck Lever } 1466a246b010SChuck Lever 1467dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1468a246b010SChuck Lever { 1469a246b010SChuck Lever size_t len; 1470a246b010SChuck Lever 1471d1109aa5STrond Myklebust len = transport->recv.len - transport->recv.offset; 1472a246b010SChuck Lever if (len > desc->count) 1473a246b010SChuck Lever len = desc->count; 1474a246b010SChuck Lever desc->count -= len; 1475a246b010SChuck Lever desc->offset += len; 1476d1109aa5STrond Myklebust transport->recv.offset += len; 14775b5e0928SAlexey Dobriyan dprintk("RPC: discarded %zu bytes\n", len); 147851971139SChuck Lever xs_tcp_check_fraghdr(transport); 1479a246b010SChuck Lever } 1480a246b010SChuck Lever 14819903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1482a246b010SChuck Lever { 1483a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 148451971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1485dd456471SChuck Lever struct xdr_skb_reader desc = { 1486a246b010SChuck Lever .skb = skb, 1487a246b010SChuck Lever .offset = offset, 1488a246b010SChuck Lever .count = len, 1489a246b010SChuck Lever }; 14903d188805STrond Myklebust size_t ret; 1491a246b010SChuck Lever 14929903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1493a246b010SChuck Lever do { 14941a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 1495a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1496a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1497d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_FRAGHDR) { 14989903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1499a246b010SChuck Lever continue; 1500a246b010SChuck Lever } 1501a246b010SChuck Lever /* Read in the xid if necessary */ 1502d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_XID) { 150351971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1504a246b010SChuck Lever continue; 1505a246b010SChuck Lever } 150618dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1507d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_READ_CALLDIR) { 150818dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 150918dca02aSRicardo Labiaga continue; 151018dca02aSRicardo Labiaga } 1511a246b010SChuck Lever /* Read in the request data */ 1512d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_DATA) { 151344b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1514a246b010SChuck Lever continue; 1515a246b010SChuck Lever } 1516a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 151751971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1518a246b010SChuck Lever } while (desc.count); 15193d188805STrond Myklebust ret = len - desc.count; 15203d188805STrond Myklebust if (ret < rd_desc->count) 15213d188805STrond Myklebust rd_desc->count -= ret; 15223d188805STrond Myklebust else 15233d188805STrond Myklebust rd_desc->count = 0; 15241a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 15259903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 15263d188805STrond Myklebust return ret; 1527a246b010SChuck Lever } 1528a246b010SChuck Lever 152966d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport) 153066d7a56aSTrond Myklebust { 153166d7a56aSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 153266d7a56aSTrond Myklebust struct sock *sk; 153366d7a56aSTrond Myklebust read_descriptor_t rd_desc = { 153466d7a56aSTrond Myklebust .arg.data = xprt, 153566d7a56aSTrond Myklebust }; 153666d7a56aSTrond Myklebust unsigned long total = 0; 153766d7a56aSTrond Myklebust int read = 0; 153866d7a56aSTrond Myklebust 15390af3442aSTrond Myklebust restart: 1540edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 154166d7a56aSTrond Myklebust sk = transport->inet; 1542edc1b01cSTrond Myklebust if (sk == NULL) 1543edc1b01cSTrond Myklebust goto out; 154466d7a56aSTrond Myklebust 154566d7a56aSTrond Myklebust /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 15460af3442aSTrond Myklebust for (;;) { 15473d188805STrond Myklebust rd_desc.count = RPC_TCP_READ_CHUNK_SZ; 1548edc1b01cSTrond Myklebust lock_sock(sk); 154966d7a56aSTrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 15503d188805STrond Myklebust if (rd_desc.count != 0 || read < 0) { 155142d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 1552edc1b01cSTrond Myklebust release_sock(sk); 155366d7a56aSTrond Myklebust break; 15548d6f97d6STrond Myklebust } 155542d42a5bSTrond Myklebust release_sock(sk); 155666d7a56aSTrond Myklebust total += read; 15570af3442aSTrond Myklebust if (need_resched()) { 15580af3442aSTrond Myklebust mutex_unlock(&transport->recv_mutex); 15590af3442aSTrond Myklebust cond_resched(); 15600af3442aSTrond Myklebust goto restart; 15610af3442aSTrond Myklebust } 156266d7a56aSTrond Myklebust } 15638d6f97d6STrond Myklebust if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 15648d6f97d6STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1565edc1b01cSTrond Myklebust out: 1566edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 156766d7a56aSTrond Myklebust trace_xs_tcp_data_ready(xprt, read, total); 156866d7a56aSTrond Myklebust } 156966d7a56aSTrond Myklebust 1570edc1b01cSTrond Myklebust static void xs_tcp_data_receive_workfn(struct work_struct *work) 1571edc1b01cSTrond Myklebust { 1572edc1b01cSTrond Myklebust struct sock_xprt *transport = 1573edc1b01cSTrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1574edc1b01cSTrond Myklebust xs_tcp_data_receive(transport); 1575edc1b01cSTrond Myklebust } 1576edc1b01cSTrond Myklebust 15779903cd1cSChuck Lever /** 15789903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 15799903cd1cSChuck Lever * @sk: socket whose state has changed 15809903cd1cSChuck Lever * 15819903cd1cSChuck Lever */ 15829903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1583a246b010SChuck Lever { 1584a246b010SChuck Lever struct rpc_xprt *xprt; 15850fdea1e8STrond Myklebust struct sock_xprt *transport; 1586a246b010SChuck Lever 1587f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1588a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1589a246b010SChuck Lever goto out; 15909903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1591669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1592a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1593a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1594669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1595669502ffSAndy Chittenden sk->sk_shutdown); 1596a246b010SChuck Lever 15970fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 159840b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1599a246b010SChuck Lever switch (sk->sk_state) { 1600a246b010SChuck Lever case TCP_ESTABLISHED: 1601f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1602a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 16038b71798cSTrond Myklebust xprt->connect_cookie++; 16040fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 16050fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 160651971139SChuck Lever 16072a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1608a246b010SChuck Lever } 1609f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1610a246b010SChuck Lever break; 16113b948ae5STrond Myklebust case TCP_FIN_WAIT1: 16123b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 16137c1d71cfSTrond Myklebust xprt->connect_cookie++; 1614663b8858STrond Myklebust xprt->reestablish_timeout = 0; 16153b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 16164e857c58SPeter Zijlstra smp_mb__before_atomic(); 16173b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1618ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 16194e857c58SPeter Zijlstra smp_mb__after_atomic(); 1620a246b010SChuck Lever break; 1621632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 16223b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 16237c1d71cfSTrond Myklebust xprt->connect_cookie++; 1624d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1625a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1626e9d47639SGustavo A. R. Silva /* fall through */ 1627663b8858STrond Myklebust case TCP_CLOSING: 1628663b8858STrond Myklebust /* 1629663b8858STrond Myklebust * If the server closed down the connection, make sure that 1630663b8858STrond Myklebust * we back off before reconnecting 1631663b8858STrond Myklebust */ 1632663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1633663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 16343b948ae5STrond Myklebust break; 16353b948ae5STrond Myklebust case TCP_LAST_ACK: 1636670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 16374e857c58SPeter Zijlstra smp_mb__before_atomic(); 16383b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 16394e857c58SPeter Zijlstra smp_mb__after_atomic(); 16403b948ae5STrond Myklebust break; 16413b948ae5STrond Myklebust case TCP_CLOSE: 16420fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 16430fdea1e8STrond Myklebust &transport->sock_state)) 16440fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 16459b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 16463ffbc1d6SNeilBrown if (sk->sk_err) 16473ffbc1d6SNeilBrown xprt_wake_pending_tasks(xprt, -sk->sk_err); 16489b30889cSTrond Myklebust /* Trigger the socket release */ 16499b30889cSTrond Myklebust xs_tcp_force_close(xprt); 1650a246b010SChuck Lever } 1651a246b010SChuck Lever out: 1652f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1653a246b010SChuck Lever } 1654a246b010SChuck Lever 16551f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 16561f0fa154SIlpo Järvinen { 165713331a55STrond Myklebust struct socket_wq *wq; 16581f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 16591f0fa154SIlpo Järvinen 166013331a55STrond Myklebust if (!sk->sk_socket) 16611f0fa154SIlpo Järvinen return; 166213331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16631f0fa154SIlpo Järvinen 16641f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 16651f0fa154SIlpo Järvinen return; 166613331a55STrond Myklebust rcu_read_lock(); 166713331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 166813331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 166913331a55STrond Myklebust goto out; 16701f0fa154SIlpo Järvinen 16711f0fa154SIlpo Järvinen xprt_write_space(xprt); 167213331a55STrond Myklebust out: 167313331a55STrond Myklebust rcu_read_unlock(); 16741f0fa154SIlpo Järvinen } 16751f0fa154SIlpo Järvinen 16762a9e1cfaSTrond Myklebust /** 1677c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1678c7b2cae8SChuck Lever * becomes available 16799903cd1cSChuck Lever * @sk: socket whose state has changed 16809903cd1cSChuck Lever * 1681a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1682a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1683c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1684a246b010SChuck Lever * with a bunch of small requests. 1685a246b010SChuck Lever */ 1686c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1687a246b010SChuck Lever { 1688f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1689c7b2cae8SChuck Lever 1690c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16911f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16921f0fa154SIlpo Järvinen xs_write_space(sk); 1693c7b2cae8SChuck Lever 1694f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1695c7b2cae8SChuck Lever } 1696c7b2cae8SChuck Lever 1697c7b2cae8SChuck Lever /** 1698c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1699c7b2cae8SChuck Lever * becomes available 1700c7b2cae8SChuck Lever * @sk: socket whose state has changed 1701c7b2cae8SChuck Lever * 1702c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1703c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1704c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1705c7b2cae8SChuck Lever * with a bunch of small requests. 1706c7b2cae8SChuck Lever */ 1707c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1708c7b2cae8SChuck Lever { 1709f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1710c7b2cae8SChuck Lever 1711c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 171264dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 17131f0fa154SIlpo Järvinen xs_write_space(sk); 1714c7b2cae8SChuck Lever 1715f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1716a246b010SChuck Lever } 1717a246b010SChuck Lever 1718470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1719a246b010SChuck Lever { 1720ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1721ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1722a246b010SChuck Lever 17237c6e066eSChuck Lever if (transport->rcvsize) { 1724a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 17257c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1726a246b010SChuck Lever } 17277c6e066eSChuck Lever if (transport->sndsize) { 1728a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 17297c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1730a246b010SChuck Lever sk->sk_write_space(sk); 1731a246b010SChuck Lever } 1732a246b010SChuck Lever } 1733a246b010SChuck Lever 173443118c29SChuck Lever /** 1735470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 173643118c29SChuck Lever * @xprt: generic transport 1737470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1738470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 173943118c29SChuck Lever * 1740470056c2SChuck Lever * Set socket send and receive buffer size limits. 174143118c29SChuck Lever */ 1742470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 174343118c29SChuck Lever { 17447c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 17457c6e066eSChuck Lever 17467c6e066eSChuck Lever transport->sndsize = 0; 1747470056c2SChuck Lever if (sndsize) 17487c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 17497c6e066eSChuck Lever transport->rcvsize = 0; 1750470056c2SChuck Lever if (rcvsize) 17517c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1752470056c2SChuck Lever 1753470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 175443118c29SChuck Lever } 175543118c29SChuck Lever 175646c0ee8bSChuck Lever /** 175746c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 175846c0ee8bSChuck Lever * @task: task that timed out 175946c0ee8bSChuck Lever * 176046c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 176146c0ee8bSChuck Lever */ 17626a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 176346c0ee8bSChuck Lever { 1764b977b644SChuck Lever spin_lock_bh(&xprt->transport_lock); 17656a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1766b977b644SChuck Lever spin_unlock_bh(&xprt->transport_lock); 176746c0ee8bSChuck Lever } 176846c0ee8bSChuck Lever 1769b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1770b85d8806SChuck Lever { 17715d71899aSFrank Sorenson unsigned short range = xprt_max_resvport - xprt_min_resvport + 1; 177263862b5bSAruna-Hewapathirane unsigned short rand = (unsigned short) prandom_u32() % range; 1773b85d8806SChuck Lever return rand + xprt_min_resvport; 1774b85d8806SChuck Lever } 1775b85d8806SChuck Lever 177692200412SChuck Lever /** 17774dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17784dda9c8aSTrond Myklebust * @sock: socket 17794dda9c8aSTrond Myklebust * 17804dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17814dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17824dda9c8aSTrond Myklebust */ 17834dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17844dda9c8aSTrond Myklebust { 1785402e23b4STrond Myklebust int opt = 1; 17864dda9c8aSTrond Myklebust 1787402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1788402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17894dda9c8aSTrond Myklebust } 17904dda9c8aSTrond Myklebust 17914dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17924dda9c8aSTrond Myklebust { 17934dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17944dda9c8aSTrond Myklebust unsigned short port = 0; 17954dda9c8aSTrond Myklebust 17969b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 17974dda9c8aSTrond Myklebust goto out; 17984dda9c8aSTrond Myklebust switch (buf.ss_family) { 17994dda9c8aSTrond Myklebust case AF_INET6: 18004dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 18014dda9c8aSTrond Myklebust break; 18024dda9c8aSTrond Myklebust case AF_INET: 18034dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 18044dda9c8aSTrond Myklebust } 18054dda9c8aSTrond Myklebust out: 18064dda9c8aSTrond Myklebust return port; 18074dda9c8aSTrond Myklebust } 18084dda9c8aSTrond Myklebust 18094dda9c8aSTrond Myklebust /** 181092200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 181192200412SChuck Lever * @xprt: generic transport 181292200412SChuck Lever * @port: new port number 181392200412SChuck Lever * 181492200412SChuck Lever */ 181592200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 181692200412SChuck Lever { 181792200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1818c4efcb1dSChuck Lever 18199dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 18209dc3b095SChuck Lever xs_update_peer_port(xprt); 182192200412SChuck Lever } 182292200412SChuck Lever 18234dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 18244dda9c8aSTrond Myklebust { 18254dda9c8aSTrond Myklebust if (transport->srcport == 0) 18264dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 18274dda9c8aSTrond Myklebust } 18284dda9c8aSTrond Myklebust 18295d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport) 183067a391d7STrond Myklebust { 1831fbfffbd5SChuck Lever unsigned short port = transport->srcport; 183267a391d7STrond Myklebust 183367a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 183467a391d7STrond Myklebust port = xs_get_random_port(); 183567a391d7STrond Myklebust return port; 183667a391d7STrond Myklebust } 183767a391d7STrond Myklebust 1838baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 183967a391d7STrond Myklebust { 1840fbfffbd5SChuck Lever if (transport->srcport != 0) 1841fbfffbd5SChuck Lever transport->srcport = 0; 184267a391d7STrond Myklebust if (!transport->xprt.resvport) 184367a391d7STrond Myklebust return 0; 184467a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 184567a391d7STrond Myklebust return xprt_max_resvport; 184667a391d7STrond Myklebust return --port; 184767a391d7STrond Myklebust } 1848beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1849a246b010SChuck Lever { 1850beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 185167a391d7STrond Myklebust int err, nloop = 0; 18525d4ec932SPavel Emelyanov unsigned short port = xs_get_srcport(transport); 185367a391d7STrond Myklebust unsigned short last; 1854a246b010SChuck Lever 18550f7a622cSChris Perl /* 18560f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 18570f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 18580f7a622cSChris Perl * port selection happen implicitly when the socket is used 18590f7a622cSChris Perl * (for example at connect time). 18600f7a622cSChris Perl * 18610f7a622cSChris Perl * This ensures that we can continue to establish TCP 18620f7a622cSChris Perl * connections even when all local ephemeral ports are already 18630f7a622cSChris Perl * a part of some TCP connection. This makes no difference 18640f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 18650f7a622cSChris Perl * 18660f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 18670f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 18680f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 18690f7a622cSChris Perl */ 18700f7a622cSChris Perl if (port == 0) 18710f7a622cSChris Perl return 0; 18720f7a622cSChris Perl 1873beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1874a246b010SChuck Lever do { 1875beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1876e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1877beb59b68SPavel Emelyanov transport->xprt.addrlen); 1878a246b010SChuck Lever if (err == 0) { 1879fbfffbd5SChuck Lever transport->srcport = port; 1880d3bc9a1dSFrank van Maarseveen break; 1881a246b010SChuck Lever } 188267a391d7STrond Myklebust last = port; 1883baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 188467a391d7STrond Myklebust if (port > last) 188567a391d7STrond Myklebust nloop++; 188667a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1887beb59b68SPavel Emelyanov 18884232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1889beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1890beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1891beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1892beb59b68SPavel Emelyanov else 1893beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1894beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18957dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1896a246b010SChuck Lever return err; 1897a246b010SChuck Lever } 1898a246b010SChuck Lever 1899176e21eeSChuck Lever /* 1900176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1901176e21eeSChuck Lever */ 1902176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1903176e21eeSChuck Lever { 1904fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1905176e21eeSChuck Lever } 1906176e21eeSChuck Lever 1907176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1908176e21eeSChuck Lever { 1909176e21eeSChuck Lever } 1910a246b010SChuck Lever 1911ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1912ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1913ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1914ed07536eSPeter Zijlstra 1915176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1916176e21eeSChuck Lever { 1917176e21eeSChuck Lever struct sock *sk = sock->sk; 1918176e21eeSChuck Lever 1919176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1920176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1921176e21eeSChuck Lever } 1922176e21eeSChuck Lever 19238945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1924ed07536eSPeter Zijlstra { 1925ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 19268945ee5eSChuck Lever 19278945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 19288945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1929ed07536eSPeter Zijlstra } 19308945ee5eSChuck Lever 19318945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 19328945ee5eSChuck Lever { 19338945ee5eSChuck Lever struct sock *sk = sock->sk; 19348945ee5eSChuck Lever 19358945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 19368945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1937ed07536eSPeter Zijlstra } 19386bc9638aSPavel Emelyanov 19396bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19406bc9638aSPavel Emelyanov { 1941fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 19421b7a1819SWeston Andros Adamson return; 19431b7a1819SWeston Andros Adamson 19444232e863SChuck Lever switch (family) { 1945176e21eeSChuck Lever case AF_LOCAL: 1946176e21eeSChuck Lever xs_reclassify_socketu(sock); 1947176e21eeSChuck Lever break; 19484232e863SChuck Lever case AF_INET: 19496bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 19504232e863SChuck Lever break; 19514232e863SChuck Lever case AF_INET6: 19526bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 19534232e863SChuck Lever break; 19544232e863SChuck Lever } 19556bc9638aSPavel Emelyanov } 1956ed07536eSPeter Zijlstra #else 19576bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19586bc9638aSPavel Emelyanov { 19596bc9638aSPavel Emelyanov } 1960ed07536eSPeter Zijlstra #endif 1961ed07536eSPeter Zijlstra 196293dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 196393dc41bdSNeilBrown { 196493dc41bdSNeilBrown } 196593dc41bdSNeilBrown 19666bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 19674dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 19684dda9c8aSTrond Myklebust int protocol, bool reuseport) 196922f79326SPavel Emelyanov { 197022f79326SPavel Emelyanov struct socket *sock; 197122f79326SPavel Emelyanov int err; 197222f79326SPavel Emelyanov 19736bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 197422f79326SPavel Emelyanov if (err < 0) { 197522f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 197622f79326SPavel Emelyanov protocol, -err); 197722f79326SPavel Emelyanov goto out; 197822f79326SPavel Emelyanov } 19796bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 198022f79326SPavel Emelyanov 19814dda9c8aSTrond Myklebust if (reuseport) 19824dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19834dda9c8aSTrond Myklebust 19844cea288aSBen Hutchings err = xs_bind(transport, sock); 19854cea288aSBen Hutchings if (err) { 198622f79326SPavel Emelyanov sock_release(sock); 198722f79326SPavel Emelyanov goto out; 198822f79326SPavel Emelyanov } 198922f79326SPavel Emelyanov 199022f79326SPavel Emelyanov return sock; 199122f79326SPavel Emelyanov out: 199222f79326SPavel Emelyanov return ERR_PTR(err); 199322f79326SPavel Emelyanov } 199422f79326SPavel Emelyanov 1995176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1996176e21eeSChuck Lever struct socket *sock) 1997176e21eeSChuck Lever { 1998176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1999176e21eeSChuck Lever xprt); 2000176e21eeSChuck Lever 2001176e21eeSChuck Lever if (!transport->inet) { 2002176e21eeSChuck Lever struct sock *sk = sock->sk; 2003176e21eeSChuck Lever 2004176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 2005176e21eeSChuck Lever 2006176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 2007176e21eeSChuck Lever 2008176e21eeSChuck Lever sk->sk_user_data = xprt; 2009a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 2010176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 2011b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 20122118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2013c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2014176e21eeSChuck Lever 2015176e21eeSChuck Lever xprt_clear_connected(xprt); 2016176e21eeSChuck Lever 2017176e21eeSChuck Lever /* Reset to new socket */ 2018176e21eeSChuck Lever transport->sock = sock; 2019176e21eeSChuck Lever transport->inet = sk; 2020176e21eeSChuck Lever 2021176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2022176e21eeSChuck Lever } 2023176e21eeSChuck Lever 20246c7a64e5STrond Myklebust transport->xmit.offset = 0; 20256c7a64e5STrond Myklebust 2026176e21eeSChuck Lever /* Tell the socket layer to start connecting... */ 2027176e21eeSChuck Lever xprt->stat.connect_count++; 2028176e21eeSChuck Lever xprt->stat.connect_start = jiffies; 2029176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 2030176e21eeSChuck Lever } 2031176e21eeSChuck Lever 2032176e21eeSChuck Lever /** 2033176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 2034176e21eeSChuck Lever * @transport: socket transport to connect 2035176e21eeSChuck Lever */ 2036dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 2037176e21eeSChuck Lever { 2038176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2039176e21eeSChuck Lever struct socket *sock; 2040176e21eeSChuck Lever int status = -EIO; 2041176e21eeSChuck Lever 2042176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 2043176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 2044176e21eeSChuck Lever if (status < 0) { 2045176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 2046176e21eeSChuck Lever "transport socket (%d).\n", -status); 2047176e21eeSChuck Lever goto out; 2048176e21eeSChuck Lever } 2049d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 2050176e21eeSChuck Lever 2051176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 2052176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2053176e21eeSChuck Lever 2054176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 205540b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2056176e21eeSChuck Lever switch (status) { 2057176e21eeSChuck Lever case 0: 2058176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 2059176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2060176e21eeSChuck Lever xprt_set_connected(xprt); 20613601c4a9STrond Myklebust case -ENOBUFS: 2062176e21eeSChuck Lever break; 2063176e21eeSChuck Lever case -ENOENT: 2064176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2065176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2066176e21eeSChuck Lever break; 20674a20a988STrond Myklebust case -ECONNREFUSED: 20684a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20694a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20704a20a988STrond Myklebust break; 2071176e21eeSChuck Lever default: 2072176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2073176e21eeSChuck Lever __func__, -status, 2074176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2075176e21eeSChuck Lever } 2076176e21eeSChuck Lever 2077176e21eeSChuck Lever out: 2078176e21eeSChuck Lever xprt_clear_connecting(xprt); 2079176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2080dc107402SJ. Bruce Fields return status; 2081dc107402SJ. Bruce Fields } 2082dc107402SJ. Bruce Fields 2083b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2084dc107402SJ. Bruce Fields { 2085dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2086dc107402SJ. Bruce Fields int ret; 2087dc107402SJ. Bruce Fields 2088dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2089dc107402SJ. Bruce Fields /* 2090dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2091dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2092dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2093dc107402SJ. Bruce Fields * 2094dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2095dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2096dc107402SJ. Bruce Fields * connect. 2097dc107402SJ. Bruce Fields */ 2098dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2099dc107402SJ. Bruce Fields return; 2100dc107402SJ. Bruce Fields } 2101dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2102dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2103dc107402SJ. Bruce Fields msleep_interruptible(15000); 2104176e21eeSChuck Lever } 2105176e21eeSChuck Lever 21063c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2107d6e971d8SJeff Layton /* 2108d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2109d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2110d6e971d8SJeff Layton * races with xs_reset_transport. 2111d6e971d8SJeff Layton */ 2112a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2113a564b8f0SMel Gorman { 2114a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2115a564b8f0SMel Gorman xprt); 2116a564b8f0SMel Gorman 2117d6e971d8SJeff Layton /* 2118d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2119d6e971d8SJeff Layton * reconnecting process will get it for us. 2120d6e971d8SJeff Layton */ 2121d6e971d8SJeff Layton if (!transport->inet) 2122d6e971d8SJeff Layton return; 21238e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2124a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2125a564b8f0SMel Gorman } 2126a564b8f0SMel Gorman 2127a564b8f0SMel Gorman /** 2128d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2129a564b8f0SMel Gorman * @xprt: transport to tag 2130a564b8f0SMel Gorman * 21318e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 21328e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2133a564b8f0SMel Gorman */ 2134d67fa4d8SJeff Layton static int 2135d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2136a564b8f0SMel Gorman { 2137d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2138a564b8f0SMel Gorman 2139d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2140d6e971d8SJeff Layton return 0; 2141d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2142d6e971d8SJeff Layton return -ERESTARTSYS; 2143d6e971d8SJeff Layton if (xs->inet) 2144d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2145d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 21468e228133SJeff Layton return 0; 2147a564b8f0SMel Gorman } 2148a564b8f0SMel Gorman 21498e228133SJeff Layton /** 2150d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 21518e228133SJeff Layton * @xprt: transport to tag 21528e228133SJeff Layton * 21538e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 21548e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 21558e228133SJeff Layton */ 2156d67fa4d8SJeff Layton static void 2157d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21588e228133SJeff Layton { 2159d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21608e228133SJeff Layton 2161d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2162d6e971d8SJeff Layton return; 2163d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2164d6e971d8SJeff Layton return; 2165d6e971d8SJeff Layton if (xs->inet) 2166d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2167d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2168a564b8f0SMel Gorman } 2169a564b8f0SMel Gorman #else 2170a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2171a564b8f0SMel Gorman { 2172a564b8f0SMel Gorman } 2173d67fa4d8SJeff Layton 2174d67fa4d8SJeff Layton static int 2175d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2176d67fa4d8SJeff Layton { 2177d67fa4d8SJeff Layton return -EINVAL; 2178d67fa4d8SJeff Layton } 2179d67fa4d8SJeff Layton 2180d67fa4d8SJeff Layton static void 2181d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2182d67fa4d8SJeff Layton { 2183d67fa4d8SJeff Layton } 2184a564b8f0SMel Gorman #endif 2185a564b8f0SMel Gorman 218616be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2187a246b010SChuck Lever { 218816be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2189edb267a6SChuck Lever 2190ee0ac0c2SChuck Lever if (!transport->inet) { 2191b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2192b0d93ad5SChuck Lever 2193b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2194b0d93ad5SChuck Lever 21952a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21962a9e1cfaSTrond Myklebust 2197b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2198f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2199b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2200b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2201c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2202b0d93ad5SChuck Lever 2203b0d93ad5SChuck Lever xprt_set_connected(xprt); 2204b0d93ad5SChuck Lever 2205b0d93ad5SChuck Lever /* Reset to new socket */ 2206ee0ac0c2SChuck Lever transport->sock = sock; 2207ee0ac0c2SChuck Lever transport->inet = sk; 2208b0d93ad5SChuck Lever 2209a564b8f0SMel Gorman xs_set_memalloc(xprt); 2210a564b8f0SMel Gorman 2211b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2212b0d93ad5SChuck Lever } 2213470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 221402910177STrond Myklebust 221502910177STrond Myklebust xprt->stat.connect_start = jiffies; 221616be2d20SChuck Lever } 221716be2d20SChuck Lever 22188c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2219a246b010SChuck Lever { 2220a246b010SChuck Lever struct sock_xprt *transport = 2221a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2222a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2223d099b8afSColin Ian King struct socket *sock; 2224b65c0310SPavel Emelyanov int status = -EIO; 2225a246b010SChuck Lever 22268c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 22274dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 22284dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2229b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2230a246b010SChuck Lever goto out; 223168e220bdSChuck Lever 2232c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2233c740eff8SChuck Lever "%s (port %s)\n", xprt, 2234c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2235c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2236c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 223768e220bdSChuck Lever 223868e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 223940b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2240a246b010SChuck Lever status = 0; 2241b0d93ad5SChuck Lever out: 2242718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 2243b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 22447d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2245b0d93ad5SChuck Lever } 2246b0d93ad5SChuck Lever 22474876cc77STrond Myklebust /** 22484876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 22494876cc77STrond Myklebust * @xprt: transport 22504876cc77STrond Myklebust * 22514876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 22524876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 22534876cc77STrond Myklebust */ 22544876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 22554876cc77STrond Myklebust { 22564876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22574876cc77STrond Myklebust struct socket *sock = transport->sock; 22589b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22594876cc77STrond Myklebust 22604876cc77STrond Myklebust if (sock == NULL) 22614876cc77STrond Myklebust return; 22629b30889cSTrond Myklebust switch (skst) { 22639b30889cSTrond Myklebust default: 22644876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22654876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22669b30889cSTrond Myklebust break; 22679b30889cSTrond Myklebust case TCP_CLOSE: 22689b30889cSTrond Myklebust case TCP_TIME_WAIT: 22694876cc77STrond Myklebust xs_reset_transport(transport); 22704876cc77STrond Myklebust } 22719b30889cSTrond Myklebust } 22724876cc77STrond Myklebust 22738d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22748d1b8c62STrond Myklebust struct socket *sock) 2275b0d93ad5SChuck Lever { 227616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22777196dbb0STrond Myklebust unsigned int keepidle; 22787196dbb0STrond Myklebust unsigned int keepcnt; 22797f260e85STrond Myklebust unsigned int opt_on = 1; 2280775f06abSTrond Myklebust unsigned int timeo; 22817f260e85STrond Myklebust 22827196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22837196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22847196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22857196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22867196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22877196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22887196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22897f260e85STrond Myklebust 22907f260e85STrond Myklebust /* TCP Keepalive options */ 22917f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22927f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22937f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22947f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22957f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22967f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22977f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22987f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2299b0d93ad5SChuck Lever 23008d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 23018d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 23028d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 23038d1b8c62STrond Myklebust } 23048d1b8c62STrond Myklebust 23057196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 23067196dbb0STrond Myklebust unsigned long connect_timeout, 23077196dbb0STrond Myklebust unsigned long reconnect_timeout) 23087196dbb0STrond Myklebust { 23097196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23107196dbb0STrond Myklebust struct rpc_timeout to; 23117196dbb0STrond Myklebust unsigned long initval; 23127196dbb0STrond Myklebust 23137196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 23147196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 23157196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 23167196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 23177196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 23187196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 23197196dbb0STrond Myklebust /* Arbitrary lower limit */ 23207196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 23217196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 23227196dbb0STrond Myklebust to.to_initval = initval; 23237196dbb0STrond Myklebust to.to_maxval = initval; 23247196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 23257196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 23267196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 23277196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 23287196dbb0STrond Myklebust } 23297196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 23307196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 23317196dbb0STrond Myklebust } 23327196dbb0STrond Myklebust 23338d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 23348d1b8c62STrond Myklebust { 23358d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23368d1b8c62STrond Myklebust int ret = -ENOTCONN; 23378d1b8c62STrond Myklebust 23388d1b8c62STrond Myklebust if (!transport->inet) { 23398d1b8c62STrond Myklebust struct sock *sk = sock->sk; 23408d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 23418d1b8c62STrond Myklebust 2342d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2343d88e4d82SNeilBrown * connections such as NFS mounts. 2344d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2345d88e4d82SNeilBrown * Individual applications, which have specific 2346d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2347d88e4d82SNeilBrown * MAY override this as appropriate. 2348d88e4d82SNeilBrown */ 2349d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2350d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2351d88e4d82SNeilBrown 23528d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2353775f06abSTrond Myklebust 2354b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2355b0d93ad5SChuck Lever 23562a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23572a9e1cfaSTrond Myklebust 2358b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23595157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2360b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2361b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2362b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 23632118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2364c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 23653167e12cSChuck Lever 23663167e12cSChuck Lever /* socket options */ 23673167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 23683167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2369b0d93ad5SChuck Lever 2370b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2371b0d93ad5SChuck Lever 2372b0d93ad5SChuck Lever /* Reset to new socket */ 2373ee0ac0c2SChuck Lever transport->sock = sock; 2374ee0ac0c2SChuck Lever transport->inet = sk; 2375b0d93ad5SChuck Lever 2376b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2377b0d93ad5SChuck Lever } 2378b0d93ad5SChuck Lever 237901d37c42STrond Myklebust if (!xprt_bound(xprt)) 2380fe19a96bSTrond Myklebust goto out; 238101d37c42STrond Myklebust 2382a564b8f0SMel Gorman xs_set_memalloc(xprt); 2383a564b8f0SMel Gorman 2384e1806c7bSTrond Myklebust /* Reset TCP record info */ 2385e1806c7bSTrond Myklebust transport->recv.offset = 0; 2386e1806c7bSTrond Myklebust transport->recv.len = 0; 2387e1806c7bSTrond Myklebust transport->recv.copied = 0; 2388e1806c7bSTrond Myklebust transport->recv.flags = TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 23896c7a64e5STrond Myklebust transport->xmit.offset = 0; 2390e1806c7bSTrond Myklebust 2391b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 2392262ca07dSChuck Lever xprt->stat.connect_count++; 2393262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 23940fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2395fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2396fe19a96bSTrond Myklebust switch (ret) { 2397fe19a96bSTrond Myklebust case 0: 23984dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2399e9d47639SGustavo A. R. Silva /* fall through */ 2400fe19a96bSTrond Myklebust case -EINPROGRESS: 2401fe19a96bSTrond Myklebust /* SYN_SENT! */ 2402fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2403fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 24041f4c17a0STrond Myklebust break; 24051f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 24061f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 24071f4c17a0STrond Myklebust transport->srcport = 0; 2408fe19a96bSTrond Myklebust } 2409fe19a96bSTrond Myklebust out: 2410fe19a96bSTrond Myklebust return ret; 241116be2d20SChuck Lever } 241216be2d20SChuck Lever 241316be2d20SChuck Lever /** 2414b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 241516be2d20SChuck Lever * 241616be2d20SChuck Lever * Invoked by a work queue tasklet. 241716be2d20SChuck Lever */ 2418cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 241916be2d20SChuck Lever { 2420cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2421cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 242216be2d20SChuck Lever struct socket *sock = transport->sock; 2423a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2424b61d59ffSTrond Myklebust int status = -EIO; 242516be2d20SChuck Lever 242616be2d20SChuck Lever if (!sock) { 2427cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 24284dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 24294dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2430b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2431b61d59ffSTrond Myklebust status = PTR_ERR(sock); 243216be2d20SChuck Lever goto out; 243316be2d20SChuck Lever } 24347d1e8255STrond Myklebust } 24357d1e8255STrond Myklebust 2436c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2437c740eff8SChuck Lever "%s (port %s)\n", xprt, 2438c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2439c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2440c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 244116be2d20SChuck Lever 244216be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 244340b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2444a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 244546121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 244646121cf7SChuck Lever sock->sk->sk_state); 2447a246b010SChuck Lever switch (status) { 2448f75e6745STrond Myklebust default: 2449f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2450f75e6745STrond Myklebust __func__, status); 2451e9d47639SGustavo A. R. Silva /* fall through */ 2452f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2453f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2454f75e6745STrond Myklebust * and retry 2455f75e6745STrond Myklebust */ 2456a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 245788b5ed73STrond Myklebust break; 24582a491991STrond Myklebust case 0: 2459a246b010SChuck Lever case -EINPROGRESS: 2460a246b010SChuck Lever case -EALREADY: 2461718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 24627d1e8255STrond Myklebust return; 24639fcfe0c8STrond Myklebust case -EINVAL: 24649fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24659fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24669fcfe0c8STrond Myklebust */ 24673ed5e2a2STrond Myklebust case -ECONNREFUSED: 24683ed5e2a2STrond Myklebust case -ECONNRESET: 2469eb5b46faSTrond Myklebust case -ENETDOWN: 24703ed5e2a2STrond Myklebust case -ENETUNREACH: 24714ba161a7STrond Myklebust case -EHOSTUNREACH: 24723913c78cSTrond Myklebust case -EADDRINUSE: 24733601c4a9STrond Myklebust case -ENOBUFS: 24746ea44adcSNeilBrown /* 24756ea44adcSNeilBrown * xs_tcp_force_close() wakes tasks with -EIO. 24766ea44adcSNeilBrown * We need to wake them first to ensure the 24776ea44adcSNeilBrown * correct error code. 24786ea44adcSNeilBrown */ 24796ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24804efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 24819fcfe0c8STrond Myklebust goto out; 24828a2cec29STrond Myklebust } 24832a491991STrond Myklebust status = -EAGAIN; 2484a246b010SChuck Lever out: 2485718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 24862226feb6SChuck Lever xprt_clear_connecting(xprt); 24877d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2488a246b010SChuck Lever } 2489a246b010SChuck Lever 249002910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 249102910177STrond Myklebust { 249202910177STrond Myklebust unsigned long start, now = jiffies; 249302910177STrond Myklebust 249402910177STrond Myklebust start = xprt->stat.connect_start + xprt->reestablish_timeout; 249502910177STrond Myklebust if (time_after(start, now)) 249602910177STrond Myklebust return start - now; 249702910177STrond Myklebust return 0; 249802910177STrond Myklebust } 249902910177STrond Myklebust 25003851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt) 25013851f1cdSTrond Myklebust { 25023851f1cdSTrond Myklebust xprt->reestablish_timeout <<= 1; 25033851f1cdSTrond Myklebust if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 25043851f1cdSTrond Myklebust xprt->reestablish_timeout = xprt->max_reconnect_timeout; 25053851f1cdSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 25063851f1cdSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 25073851f1cdSTrond Myklebust } 25083851f1cdSTrond Myklebust 250968e220bdSChuck Lever /** 25109903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 25111b092092STrond Myklebust * @xprt: pointer to transport structure 25129903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 25139903cd1cSChuck Lever * 25149903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 251503bf4b70SChuck Lever * 251603bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 251703bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 251803bf4b70SChuck Lever * socket on a privileged port. 251903bf4b70SChuck Lever * 252003bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 252103bf4b70SChuck Lever * retry floods (hard mounts). 25229903cd1cSChuck Lever */ 25231b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2524a246b010SChuck Lever { 2525ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 252602910177STrond Myklebust unsigned long delay = 0; 2527a246b010SChuck Lever 2528718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2529718ba5b8STrond Myklebust 253099b1a4c3STrond Myklebust if (transport->sock != NULL) { 253146121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 253246121cf7SChuck Lever "seconds\n", 253303bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 253499b1a4c3STrond Myklebust 253599b1a4c3STrond Myklebust /* Start by resetting any existing state */ 253699b1a4c3STrond Myklebust xs_reset_transport(transport); 253799b1a4c3STrond Myklebust 253802910177STrond Myklebust delay = xs_reconnect_delay(xprt); 25393851f1cdSTrond Myklebust xs_reconnect_backoff(xprt); 254002910177STrond Myklebust 254102910177STrond Myklebust } else 25429903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 254302910177STrond Myklebust 254440a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 254502910177STrond Myklebust &transport->connect_worker, 254602910177STrond Myklebust delay); 2547a246b010SChuck Lever } 2548a246b010SChuck Lever 2549262ca07dSChuck Lever /** 2550176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2551176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2552176e21eeSChuck Lever * @seq: output file 2553176e21eeSChuck Lever * 2554176e21eeSChuck Lever */ 2555176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2556176e21eeSChuck Lever { 2557176e21eeSChuck Lever long idle_time = 0; 2558176e21eeSChuck Lever 2559176e21eeSChuck Lever if (xprt_connected(xprt)) 2560176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2561176e21eeSChuck Lever 2562176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 256315a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2564176e21eeSChuck Lever xprt->stat.bind_count, 2565176e21eeSChuck Lever xprt->stat.connect_count, 2566176e21eeSChuck Lever xprt->stat.connect_time, 2567176e21eeSChuck Lever idle_time, 2568176e21eeSChuck Lever xprt->stat.sends, 2569176e21eeSChuck Lever xprt->stat.recvs, 2570176e21eeSChuck Lever xprt->stat.bad_xids, 2571176e21eeSChuck Lever xprt->stat.req_u, 257215a45206SAndy Adamson xprt->stat.bklog_u, 257315a45206SAndy Adamson xprt->stat.max_slots, 257415a45206SAndy Adamson xprt->stat.sending_u, 257515a45206SAndy Adamson xprt->stat.pending_u); 2576176e21eeSChuck Lever } 2577176e21eeSChuck Lever 2578176e21eeSChuck Lever /** 2579262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2580262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2581262ca07dSChuck Lever * @seq: output file 2582262ca07dSChuck Lever * 2583262ca07dSChuck Lever */ 2584262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2585262ca07dSChuck Lever { 2586c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2587c8475461SChuck Lever 258815a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 258915a45206SAndy Adamson "%lu %llu %llu\n", 2590fbfffbd5SChuck Lever transport->srcport, 2591262ca07dSChuck Lever xprt->stat.bind_count, 2592262ca07dSChuck Lever xprt->stat.sends, 2593262ca07dSChuck Lever xprt->stat.recvs, 2594262ca07dSChuck Lever xprt->stat.bad_xids, 2595262ca07dSChuck Lever xprt->stat.req_u, 259615a45206SAndy Adamson xprt->stat.bklog_u, 259715a45206SAndy Adamson xprt->stat.max_slots, 259815a45206SAndy Adamson xprt->stat.sending_u, 259915a45206SAndy Adamson xprt->stat.pending_u); 2600262ca07dSChuck Lever } 2601262ca07dSChuck Lever 2602262ca07dSChuck Lever /** 2603262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2604262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2605262ca07dSChuck Lever * @seq: output file 2606262ca07dSChuck Lever * 2607262ca07dSChuck Lever */ 2608262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2609262ca07dSChuck Lever { 2610c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2611262ca07dSChuck Lever long idle_time = 0; 2612262ca07dSChuck Lever 2613262ca07dSChuck Lever if (xprt_connected(xprt)) 2614262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2615262ca07dSChuck Lever 261615a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 261715a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2618fbfffbd5SChuck Lever transport->srcport, 2619262ca07dSChuck Lever xprt->stat.bind_count, 2620262ca07dSChuck Lever xprt->stat.connect_count, 2621262ca07dSChuck Lever xprt->stat.connect_time, 2622262ca07dSChuck Lever idle_time, 2623262ca07dSChuck Lever xprt->stat.sends, 2624262ca07dSChuck Lever xprt->stat.recvs, 2625262ca07dSChuck Lever xprt->stat.bad_xids, 2626262ca07dSChuck Lever xprt->stat.req_u, 262715a45206SAndy Adamson xprt->stat.bklog_u, 262815a45206SAndy Adamson xprt->stat.max_slots, 262915a45206SAndy Adamson xprt->stat.sending_u, 263015a45206SAndy Adamson xprt->stat.pending_u); 2631262ca07dSChuck Lever } 2632262ca07dSChuck Lever 26334cfc7e60SRahul Iyer /* 26344cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 26354cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 26364cfc7e60SRahul Iyer * to use the server side send routines. 26374cfc7e60SRahul Iyer */ 26385fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 26394cfc7e60SRahul Iyer { 26405fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 26415fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 26424cfc7e60SRahul Iyer struct page *page; 26434cfc7e60SRahul Iyer struct rpc_buffer *buf; 26444cfc7e60SRahul Iyer 26455fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 26465fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 26475fe6eaa1SChuck Lever size); 26485fe6eaa1SChuck Lever return -EINVAL; 26495fe6eaa1SChuck Lever } 26504cfc7e60SRahul Iyer 2651b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 26524cfc7e60SRahul Iyer if (!page) 26535fe6eaa1SChuck Lever return -ENOMEM; 26544cfc7e60SRahul Iyer 26554cfc7e60SRahul Iyer buf = page_address(page); 26564cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 26574cfc7e60SRahul Iyer 26585fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 265918e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 26605fe6eaa1SChuck Lever return 0; 26614cfc7e60SRahul Iyer } 26624cfc7e60SRahul Iyer 26634cfc7e60SRahul Iyer /* 26644cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 26654cfc7e60SRahul Iyer */ 26663435c74aSChuck Lever static void bc_free(struct rpc_task *task) 26674cfc7e60SRahul Iyer { 26683435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 26694cfc7e60SRahul Iyer struct rpc_buffer *buf; 26704cfc7e60SRahul Iyer 26714cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 26724cfc7e60SRahul Iyer free_page((unsigned long)buf); 26734cfc7e60SRahul Iyer } 26744cfc7e60SRahul Iyer 26754cfc7e60SRahul Iyer /* 26764cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 26774cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 26784cfc7e60SRahul Iyer */ 26794cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 26804cfc7e60SRahul Iyer { 26814cfc7e60SRahul Iyer int len; 26824cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 26834cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 26844cfc7e60SRahul Iyer struct sock_xprt *transport = 26854cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 26864cfc7e60SRahul Iyer struct socket *sock = transport->sock; 26874cfc7e60SRahul Iyer unsigned long headoff; 26884cfc7e60SRahul Iyer unsigned long tailoff; 26894cfc7e60SRahul Iyer 269061677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 26914cfc7e60SRahul Iyer 26924cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26934cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 26944cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 26954cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 26964cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 26974cfc7e60SRahul Iyer 26984cfc7e60SRahul Iyer if (len != xbufp->len) { 26994cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 27004cfc7e60SRahul Iyer len = -EAGAIN; 27014cfc7e60SRahul Iyer } 27024cfc7e60SRahul Iyer 27034cfc7e60SRahul Iyer return len; 27044cfc7e60SRahul Iyer } 27054cfc7e60SRahul Iyer 27064cfc7e60SRahul Iyer /* 27074cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 27084cfc7e60SRahul Iyer */ 27094cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task) 27104cfc7e60SRahul Iyer { 27114cfc7e60SRahul Iyer struct rpc_rqst *req = task->tk_rqstp; 27124cfc7e60SRahul Iyer struct svc_xprt *xprt; 27137fc56136SAndrzej Hajda int len; 27144cfc7e60SRahul Iyer 27154cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 27164cfc7e60SRahul Iyer /* 27174cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 27184cfc7e60SRahul Iyer */ 27194cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 27204cfc7e60SRahul Iyer 27214cfc7e60SRahul Iyer /* 27224cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 27234cfc7e60SRahul Iyer * with the fore channel 27244cfc7e60SRahul Iyer */ 27254cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) { 27264cfc7e60SRahul Iyer rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); 27274cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) 27284cfc7e60SRahul Iyer return -EAGAIN; 27294cfc7e60SRahul Iyer rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); 27304cfc7e60SRahul Iyer } 27314cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 27324cfc7e60SRahul Iyer len = -ENOTCONN; 27334cfc7e60SRahul Iyer else 27344cfc7e60SRahul Iyer len = bc_sendto(req); 27354cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 27364cfc7e60SRahul Iyer 27374cfc7e60SRahul Iyer if (len > 0) 27384cfc7e60SRahul Iyer len = 0; 27394cfc7e60SRahul Iyer 27404cfc7e60SRahul Iyer return len; 27414cfc7e60SRahul Iyer } 27424cfc7e60SRahul Iyer 27434cfc7e60SRahul Iyer /* 27444cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 27454cfc7e60SRahul Iyer */ 27464cfc7e60SRahul Iyer 27474cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 27484cfc7e60SRahul Iyer { 27494cfc7e60SRahul Iyer } 27504cfc7e60SRahul Iyer 27514cfc7e60SRahul Iyer /* 27524cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 27534cfc7e60SRahul Iyer * initiated, we do nothing 27544cfc7e60SRahul Iyer */ 27554cfc7e60SRahul Iyer 27564cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 27574cfc7e60SRahul Iyer { 275847f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 275947f72efaSKinglong Mee 276047f72efaSKinglong Mee xs_xprt_free(xprt); 276147f72efaSKinglong Mee module_put(THIS_MODULE); 27624cfc7e60SRahul Iyer } 27634cfc7e60SRahul Iyer 2764d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2765176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 27664cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2767f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2768a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2769176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2770176e21eeSChuck Lever .set_port = xs_local_set_port, 2771dc107402SJ. Bruce Fields .connect = xs_local_connect, 2772176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2773176e21eeSChuck Lever .buf_free = rpc_free, 2774176e21eeSChuck Lever .send_request = xs_local_send_request, 2775176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2776176e21eeSChuck Lever .close = xs_close, 2777a1311d87STrond Myklebust .destroy = xs_destroy, 2778176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2779d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2780d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2781176e21eeSChuck Lever }; 2782176e21eeSChuck Lever 2783d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 278443118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 278512a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 278649e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2787f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2788a9cde23aSChuck Lever .free_slot = xprt_free_slot, 278945160d62SChuck Lever .rpcbind = rpcb_getport_async, 279092200412SChuck Lever .set_port = xs_set_port, 27919903cd1cSChuck Lever .connect = xs_connect, 279202107148SChuck Lever .buf_alloc = rpc_malloc, 279302107148SChuck Lever .buf_free = rpc_free, 2794262965f5SChuck Lever .send_request = xs_udp_send_request, 2795fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 279646c0ee8bSChuck Lever .timer = xs_udp_timer, 2797a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2798262965f5SChuck Lever .close = xs_close, 2799262965f5SChuck Lever .destroy = xs_destroy, 2800262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2801d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2802d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28034a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2804262965f5SChuck Lever }; 2805262965f5SChuck Lever 2806d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 280712a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 28084cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2809f39c1bfbSTrond Myklebust .alloc_slot = xprt_lock_and_alloc_slot, 2810a9cde23aSChuck Lever .free_slot = xprt_free_slot, 281145160d62SChuck Lever .rpcbind = rpcb_getport_async, 281292200412SChuck Lever .set_port = xs_set_port, 28130b9e7943STrond Myklebust .connect = xs_connect, 281402107148SChuck Lever .buf_alloc = rpc_malloc, 281502107148SChuck Lever .buf_free = rpc_free, 2816262965f5SChuck Lever .send_request = xs_tcp_send_request, 2817fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2818c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 28199903cd1cSChuck Lever .destroy = xs_destroy, 28207196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2821262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2822d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2823d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28244a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 282542e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 282642e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 282776566773SChuck Lever .bc_up = xs_tcp_bc_up, 28286b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 282942e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 283042e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 283142e5c3e2SChuck Lever #endif 2832a246b010SChuck Lever }; 2833a246b010SChuck Lever 28344cfc7e60SRahul Iyer /* 28354cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 28364cfc7e60SRahul Iyer */ 28374cfc7e60SRahul Iyer 2838d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 28394cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 28404cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 284184e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2842a9cde23aSChuck Lever .free_slot = xprt_free_slot, 28434cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 28444cfc7e60SRahul Iyer .buf_free = bc_free, 28454cfc7e60SRahul Iyer .send_request = bc_send_request, 28464cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 28474cfc7e60SRahul Iyer .close = bc_close, 28484cfc7e60SRahul Iyer .destroy = bc_destroy, 28494cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2850d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2851d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28524a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 28534cfc7e60SRahul Iyer }; 28544cfc7e60SRahul Iyer 285592476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 285692476850SChuck Lever { 285792476850SChuck Lever static const struct sockaddr_in sin = { 285892476850SChuck Lever .sin_family = AF_INET, 285992476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 286092476850SChuck Lever }; 286192476850SChuck Lever static const struct sockaddr_in6 sin6 = { 286292476850SChuck Lever .sin6_family = AF_INET6, 286392476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 286492476850SChuck Lever }; 286592476850SChuck Lever 286692476850SChuck Lever switch (family) { 2867176e21eeSChuck Lever case AF_LOCAL: 2868176e21eeSChuck Lever break; 286992476850SChuck Lever case AF_INET: 287092476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 287192476850SChuck Lever break; 287292476850SChuck Lever case AF_INET6: 287392476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 287492476850SChuck Lever break; 287592476850SChuck Lever default: 287692476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 287792476850SChuck Lever return -EAFNOSUPPORT; 287892476850SChuck Lever } 287992476850SChuck Lever return 0; 288092476850SChuck Lever } 288192476850SChuck Lever 28823c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2883d9ba131dSTrond Myklebust unsigned int slot_table_size, 2884d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2885c8541ecdSChuck Lever { 2886c8541ecdSChuck Lever struct rpc_xprt *xprt; 2887ffc2e518SChuck Lever struct sock_xprt *new; 2888c8541ecdSChuck Lever 288996802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2890c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2891c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2892c8541ecdSChuck Lever } 2893c8541ecdSChuck Lever 2894d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2895d9ba131dSTrond Myklebust max_slot_table_size); 2896bd1722d4SPavel Emelyanov if (xprt == NULL) { 289746121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 289846121cf7SChuck Lever "rpc_xprt\n"); 2899c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2900c8541ecdSChuck Lever } 2901c8541ecdSChuck Lever 2902bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2903edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 290496802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 290596802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2906d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2907fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 290892476850SChuck Lever else { 290992476850SChuck Lever int err; 291092476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 291192476850SChuck Lever (struct sockaddr *)&new->srcaddr); 29122aa13531SStanislav Kinsbursky if (err != 0) { 29132aa13531SStanislav Kinsbursky xprt_free(xprt); 291492476850SChuck Lever return ERR_PTR(err); 291592476850SChuck Lever } 29162aa13531SStanislav Kinsbursky } 2917c8541ecdSChuck Lever 2918c8541ecdSChuck Lever return xprt; 2919c8541ecdSChuck Lever } 2920c8541ecdSChuck Lever 2921176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2922176e21eeSChuck Lever .to_initval = 10 * HZ, 2923176e21eeSChuck Lever .to_maxval = 10 * HZ, 2924176e21eeSChuck Lever .to_retries = 2, 2925176e21eeSChuck Lever }; 2926176e21eeSChuck Lever 2927176e21eeSChuck Lever /** 2928176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2929176e21eeSChuck Lever * @args: rpc transport creation arguments 2930176e21eeSChuck Lever * 2931176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2932176e21eeSChuck Lever */ 2933176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2934176e21eeSChuck Lever { 2935176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2936176e21eeSChuck Lever struct sock_xprt *transport; 2937176e21eeSChuck Lever struct rpc_xprt *xprt; 2938176e21eeSChuck Lever struct rpc_xprt *ret; 2939176e21eeSChuck Lever 2940d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2941d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2942176e21eeSChuck Lever if (IS_ERR(xprt)) 2943176e21eeSChuck Lever return xprt; 2944176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2945176e21eeSChuck Lever 2946176e21eeSChuck Lever xprt->prot = 0; 2947176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2948176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2949176e21eeSChuck Lever 2950176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2951176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2952176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2953176e21eeSChuck Lever 2954176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2955176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2956176e21eeSChuck Lever 2957a2648094STrond Myklebust INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn); 295893dc41bdSNeilBrown INIT_DELAYED_WORK(&transport->connect_worker, 295993dc41bdSNeilBrown xs_dummy_setup_socket); 296093dc41bdSNeilBrown 2961176e21eeSChuck Lever switch (sun->sun_family) { 2962176e21eeSChuck Lever case AF_LOCAL: 2963176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2964176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2965176e21eeSChuck Lever sun->sun_path); 2966176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2967176e21eeSChuck Lever goto out_err; 2968176e21eeSChuck Lever } 2969176e21eeSChuck Lever xprt_set_bound(xprt); 2970176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 29717073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 29727073ea87SJ. Bruce Fields if (ret) 29737073ea87SJ. Bruce Fields goto out_err; 2974176e21eeSChuck Lever break; 2975176e21eeSChuck Lever default: 2976176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2977176e21eeSChuck Lever goto out_err; 2978176e21eeSChuck Lever } 2979176e21eeSChuck Lever 2980176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2981176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2982176e21eeSChuck Lever 2983176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2984176e21eeSChuck Lever return xprt; 2985176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2986176e21eeSChuck Lever out_err: 2987315f3812SKinglong Mee xs_xprt_free(xprt); 2988176e21eeSChuck Lever return ret; 2989176e21eeSChuck Lever } 2990176e21eeSChuck Lever 29912881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 29922881ae74STrond Myklebust .to_initval = 5 * HZ, 29932881ae74STrond Myklebust .to_maxval = 30 * HZ, 29942881ae74STrond Myklebust .to_increment = 5 * HZ, 29952881ae74STrond Myklebust .to_retries = 5, 29962881ae74STrond Myklebust }; 29972881ae74STrond Myklebust 29989903cd1cSChuck Lever /** 29999903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 300096802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30019903cd1cSChuck Lever * 30029903cd1cSChuck Lever */ 3003483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 3004a246b010SChuck Lever { 30058f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3006c8541ecdSChuck Lever struct rpc_xprt *xprt; 3007c8475461SChuck Lever struct sock_xprt *transport; 30080a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3009a246b010SChuck Lever 3010d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 3011d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 3012c8541ecdSChuck Lever if (IS_ERR(xprt)) 3013c8541ecdSChuck Lever return xprt; 3014c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3015a246b010SChuck Lever 3016ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 3017808012fbSChuck Lever xprt->tsh_size = 0; 3018a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 3019a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3020a246b010SChuck Lever 302103bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 302203bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 302303bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3024a246b010SChuck Lever 3025262965f5SChuck Lever xprt->ops = &xs_udp_ops; 3026a246b010SChuck Lever 3027ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 3028a246b010SChuck Lever 3029f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 3030edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3031edc1b01cSTrond Myklebust 30328f9d5b1aSChuck Lever switch (addr->sa_family) { 30338f9d5b1aSChuck Lever case AF_INET: 30348f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30358f9d5b1aSChuck Lever xprt_set_bound(xprt); 30368f9d5b1aSChuck Lever 30379dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 30388f9d5b1aSChuck Lever break; 30398f9d5b1aSChuck Lever case AF_INET6: 30408f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30418f9d5b1aSChuck Lever xprt_set_bound(xprt); 30428f9d5b1aSChuck Lever 30439dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 30448f9d5b1aSChuck Lever break; 30458f9d5b1aSChuck Lever default: 30460a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30470a68b0beSJ. Bruce Fields goto out_err; 30488f9d5b1aSChuck Lever } 30498f9d5b1aSChuck Lever 3050c740eff8SChuck Lever if (xprt_bound(xprt)) 3051c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3052c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3053c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3054c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3055c740eff8SChuck Lever else 3056c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3057c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3058c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3059edb267a6SChuck Lever 3060bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3061c8541ecdSChuck Lever return xprt; 30620a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30630a68b0beSJ. Bruce Fields out_err: 3064315f3812SKinglong Mee xs_xprt_free(xprt); 30650a68b0beSJ. Bruce Fields return ret; 3066a246b010SChuck Lever } 3067a246b010SChuck Lever 30682881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 30692881ae74STrond Myklebust .to_initval = 60 * HZ, 30702881ae74STrond Myklebust .to_maxval = 60 * HZ, 30712881ae74STrond Myklebust .to_retries = 2, 30722881ae74STrond Myklebust }; 30732881ae74STrond Myklebust 30749903cd1cSChuck Lever /** 30759903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 307696802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30779903cd1cSChuck Lever * 30789903cd1cSChuck Lever */ 3079483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3080a246b010SChuck Lever { 30818f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3082c8541ecdSChuck Lever struct rpc_xprt *xprt; 3083c8475461SChuck Lever struct sock_xprt *transport; 30840a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3085b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3086b7993cebSTrond Myklebust 3087b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3088b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3089a246b010SChuck Lever 3090d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3091b7993cebSTrond Myklebust max_slot_table_size); 3092c8541ecdSChuck Lever if (IS_ERR(xprt)) 3093c8541ecdSChuck Lever return xprt; 3094c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3095a246b010SChuck Lever 3096ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3097808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3098808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3099a246b010SChuck Lever 310003bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 310103bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 310203bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3103a246b010SChuck Lever 3104262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3105ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3106a246b010SChuck Lever 31073851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 31087196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 31097196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 31103851f1cdSTrond Myklebust 3111edc1b01cSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn); 3112edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3113edc1b01cSTrond Myklebust 31148f9d5b1aSChuck Lever switch (addr->sa_family) { 31158f9d5b1aSChuck Lever case AF_INET: 31168f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 31178f9d5b1aSChuck Lever xprt_set_bound(xprt); 31188f9d5b1aSChuck Lever 31199dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 31208f9d5b1aSChuck Lever break; 31218f9d5b1aSChuck Lever case AF_INET6: 31228f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 31238f9d5b1aSChuck Lever xprt_set_bound(xprt); 31248f9d5b1aSChuck Lever 31259dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 31268f9d5b1aSChuck Lever break; 31278f9d5b1aSChuck Lever default: 31280a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31290a68b0beSJ. Bruce Fields goto out_err; 31308f9d5b1aSChuck Lever } 31318f9d5b1aSChuck Lever 3132c740eff8SChuck Lever if (xprt_bound(xprt)) 3133c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3134c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3135c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3136c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3137c740eff8SChuck Lever else 3138c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3139c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3140c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3141c740eff8SChuck Lever 3142bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3143c8541ecdSChuck Lever return xprt; 31440a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31450a68b0beSJ. Bruce Fields out_err: 3146315f3812SKinglong Mee xs_xprt_free(xprt); 31470a68b0beSJ. Bruce Fields return ret; 3148a246b010SChuck Lever } 3149282b32e1SChuck Lever 3150f300babaSAlexandros Batsakis /** 3151f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3152f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3153f300babaSAlexandros Batsakis * 3154f300babaSAlexandros Batsakis */ 3155f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3156f300babaSAlexandros Batsakis { 3157f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3158f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3159f300babaSAlexandros Batsakis struct sock_xprt *transport; 3160f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 31610a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3162f300babaSAlexandros Batsakis 3163d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3164d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3165f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3166f300babaSAlexandros Batsakis return xprt; 3167f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3168f300babaSAlexandros Batsakis 3169f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3170f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3171f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3172f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3173f300babaSAlexandros Batsakis 3174f300babaSAlexandros Batsakis /* backchannel */ 3175f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3176f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3177f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3178f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3179f300babaSAlexandros Batsakis 3180f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3181f300babaSAlexandros Batsakis 3182f300babaSAlexandros Batsakis switch (addr->sa_family) { 3183f300babaSAlexandros Batsakis case AF_INET: 3184f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3185f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3186f300babaSAlexandros Batsakis break; 3187f300babaSAlexandros Batsakis case AF_INET6: 3188f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3189f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3190f300babaSAlexandros Batsakis break; 3191f300babaSAlexandros Batsakis default: 31920a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31930a68b0beSJ. Bruce Fields goto out_err; 3194f300babaSAlexandros Batsakis } 3195f300babaSAlexandros Batsakis 3196f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3197f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3198f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3199f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3200f300babaSAlexandros Batsakis 3201f300babaSAlexandros Batsakis /* 320299de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 320328303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 320428303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 320528303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 320699de8ea9SJ. Bruce Fields */ 320799de8ea9SJ. Bruce Fields xprt_get(xprt); 320899de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 320999de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 321099de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 321199de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 321299de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 321399de8ea9SJ. Bruce Fields 321499de8ea9SJ. Bruce Fields /* 3215f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3216f300babaSAlexandros Batsakis * the xprt status to connected 3217f300babaSAlexandros Batsakis */ 3218f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3219f300babaSAlexandros Batsakis 3220f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3221f300babaSAlexandros Batsakis return xprt; 3222642aab58SKinglong Mee 3223642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 322439a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 322599de8ea9SJ. Bruce Fields xprt_put(xprt); 32260a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 32270a68b0beSJ. Bruce Fields out_err: 3228315f3812SKinglong Mee xs_xprt_free(xprt); 32290a68b0beSJ. Bruce Fields return ret; 3230f300babaSAlexandros Batsakis } 3231f300babaSAlexandros Batsakis 3232176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3233176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3234176e21eeSChuck Lever .name = "named UNIX socket", 3235176e21eeSChuck Lever .owner = THIS_MODULE, 3236176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3237176e21eeSChuck Lever .setup = xs_setup_local, 3238176e21eeSChuck Lever }; 3239176e21eeSChuck Lever 3240bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3241bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3242bc25571eS\"Talpey, Thomas\ .name = "udp", 3243bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3244f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3245bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3246bc25571eS\"Talpey, Thomas\ }; 3247bc25571eS\"Talpey, Thomas\ 3248bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3249bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3250bc25571eS\"Talpey, Thomas\ .name = "tcp", 3251bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3252f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3253bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3254bc25571eS\"Talpey, Thomas\ }; 3255bc25571eS\"Talpey, Thomas\ 3256f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3257f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3258f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3259f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3260f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3261f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3262f300babaSAlexandros Batsakis }; 3263f300babaSAlexandros Batsakis 3264282b32e1SChuck Lever /** 3265bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3266282b32e1SChuck Lever * 3267282b32e1SChuck Lever */ 3268282b32e1SChuck Lever int init_socket_xprt(void) 3269282b32e1SChuck Lever { 3270f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 32712b1bec5fSEric W. Biederman if (!sunrpc_table_header) 32720b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3273fbf76683SChuck Lever #endif 3274fbf76683SChuck Lever 3275176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3276bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3277bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3278f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3279bc25571eS\"Talpey, Thomas\ 3280282b32e1SChuck Lever return 0; 3281282b32e1SChuck Lever } 3282282b32e1SChuck Lever 3283282b32e1SChuck Lever /** 3284bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3285282b32e1SChuck Lever * 3286282b32e1SChuck Lever */ 3287282b32e1SChuck Lever void cleanup_socket_xprt(void) 3288282b32e1SChuck Lever { 3289f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3290fbf76683SChuck Lever if (sunrpc_table_header) { 3291fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3292fbf76683SChuck Lever sunrpc_table_header = NULL; 3293fbf76683SChuck Lever } 3294fbf76683SChuck Lever #endif 3295bc25571eS\"Talpey, Thomas\ 3296176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3297bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3298bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3299f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3300282b32e1SChuck Lever } 3301cbf11071STrond Myklebust 33029bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 33039bbb9e5aSRusty Russell const struct kernel_param *kp, 3304cbf11071STrond Myklebust unsigned int min, unsigned int max) 3305cbf11071STrond Myklebust { 330600cfaa94SDaniel Walter unsigned int num; 3307cbf11071STrond Myklebust int ret; 3308cbf11071STrond Myklebust 3309cbf11071STrond Myklebust if (!val) 3310cbf11071STrond Myklebust return -EINVAL; 331100cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 33129761a246SDan Carpenter if (ret) 33139761a246SDan Carpenter return ret; 33149761a246SDan Carpenter if (num < min || num > max) 3315cbf11071STrond Myklebust return -EINVAL; 3316cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3317cbf11071STrond Myklebust return 0; 3318cbf11071STrond Myklebust } 3319cbf11071STrond Myklebust 33209bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3321cbf11071STrond Myklebust { 3322ffb6ca33SFrank Sorenson if (kp->arg == &xprt_min_resvport) 3323cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3324cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3325ffb6ca33SFrank Sorenson xprt_max_resvport); 3326ffb6ca33SFrank Sorenson return param_set_uint_minmax(val, kp, 3327ffb6ca33SFrank Sorenson xprt_min_resvport, 3328cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3329cbf11071STrond Myklebust } 3330cbf11071STrond Myklebust 33319c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 33329bbb9e5aSRusty Russell .set = param_set_portnr, 33339bbb9e5aSRusty Russell .get = param_get_uint, 33349bbb9e5aSRusty Russell }; 33359bbb9e5aSRusty Russell 3336cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3337cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3338cbf11071STrond Myklebust 3339cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3340cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3341cbf11071STrond Myklebust 33429bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 33439bbb9e5aSRusty Russell const struct kernel_param *kp) 3344cbf11071STrond Myklebust { 3345cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3346cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3347cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3348cbf11071STrond Myklebust } 3349cbf11071STrond Myklebust 33509c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 33519bbb9e5aSRusty Russell .set = param_set_slot_table_size, 33529bbb9e5aSRusty Russell .get = param_get_uint, 33539bbb9e5aSRusty Russell }; 33549bbb9e5aSRusty Russell 3355cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3356cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3357cbf11071STrond Myklebust 3358d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3359d9ba131dSTrond Myklebust const struct kernel_param *kp) 3360d9ba131dSTrond Myklebust { 3361d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3362d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3363d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3364d9ba131dSTrond Myklebust } 3365d9ba131dSTrond Myklebust 33669c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3367d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3368d9ba131dSTrond Myklebust .get = param_get_uint, 3369d9ba131dSTrond Myklebust }; 3370d9ba131dSTrond Myklebust 3371d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3372d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3373d9ba131dSTrond Myklebust 3374cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3375cbf11071STrond Myklebust slot_table_size, 0644); 3376d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3377d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3378cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3379cbf11071STrond Myklebust slot_table_size, 0644); 3380