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 4439903cd1cSChuck Lever /** 444*c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 44550f484e2STrond Myklebust * @req: pointer to RPC request 4469903cd1cSChuck Lever * 447a246b010SChuck Lever */ 448*c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req) 449a246b010SChuck Lever { 450262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 451ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 45206ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 45324ca9a84STrond Myklebust int ret = -EAGAIN; 454a246b010SChuck Lever 45546121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 456*c544577dSTrond Myklebust req->rq_task->tk_pid, 457*c544577dSTrond Myklebust req->rq_slen - transport->xmit.offset, 458262965f5SChuck Lever req->rq_slen); 459a246b010SChuck Lever 460262965f5SChuck Lever /* Protect against races with write_space */ 461262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 462a246b010SChuck Lever 463262965f5SChuck Lever /* Don't race with disconnect */ 464b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 46513331a55STrond Myklebust /* wait for more buffer space */ 46606ea0bfeSTrond Myklebust sk->sk_write_pending++; 467*c544577dSTrond Myklebust xprt_wait_for_buffer_space(xprt); 46813331a55STrond Myklebust } else 4695e3771ceSTrond Myklebust ret = -ENOTCONN; 470a246b010SChuck Lever 471262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 47206ea0bfeSTrond Myklebust 47306ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 474d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 475d48f9ce7SDavid Vrabel struct socket_wq *wq; 476d48f9ce7SDavid Vrabel 477d48f9ce7SDavid Vrabel rcu_read_lock(); 478d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 479d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 480d48f9ce7SDavid Vrabel rcu_read_unlock(); 481d48f9ce7SDavid Vrabel 48206ea0bfeSTrond Myklebust sk->sk_write_space(sk); 483d48f9ce7SDavid Vrabel } 4845e3771ceSTrond Myklebust return ret; 485a246b010SChuck Lever } 486a246b010SChuck Lever 48761677eeeSChuck Lever /* 4884cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 4894cd34e7cSTrond Myklebust * could complete transmission. 4904cd34e7cSTrond Myklebust */ 4914cd34e7cSTrond Myklebust static bool 4924cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 4934cd34e7cSTrond Myklebust { 4944cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 4954cd34e7cSTrond Myklebust } 4964cd34e7cSTrond Myklebust 4974cd34e7cSTrond Myklebust /* 49861677eeeSChuck Lever * Construct a stream transport record marker in @buf. 49961677eeeSChuck Lever */ 50061677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 50161677eeeSChuck Lever { 50261677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 50361677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 50461677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 50561677eeeSChuck Lever } 50661677eeeSChuck Lever 5079903cd1cSChuck Lever /** 508176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 50950f484e2STrond Myklebust * @req: pointer to RPC request 510176e21eeSChuck Lever * @task: RPC task that manages the state of an RPC request 511176e21eeSChuck Lever * 512176e21eeSChuck Lever * Return values: 513176e21eeSChuck Lever * 0: The request has been sent 514176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 515176e21eeSChuck Lever * complete the request 516176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 517176e21eeSChuck Lever * other: Some other error occured, the request was not sent 518176e21eeSChuck Lever */ 51950f484e2STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req, struct rpc_task *task) 520176e21eeSChuck Lever { 521176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 522176e21eeSChuck Lever struct sock_xprt *transport = 523176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 524176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 525176e21eeSChuck Lever int status; 526f279cd00SJason Baron int sent = 0; 527176e21eeSChuck Lever 5284cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 5294cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 5304cd34e7cSTrond Myklebust xs_close(xprt); 5314cd34e7cSTrond Myklebust return -ENOTCONN; 5324cd34e7cSTrond Myklebust } 5334cd34e7cSTrond Myklebust 534176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 535176e21eeSChuck Lever 536176e21eeSChuck Lever xs_pktdump("packet data:", 537176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 538176e21eeSChuck Lever 53978215759SChuck Lever req->rq_xtime = ktime_get(); 5406c7a64e5STrond Myklebust status = xs_sendpages(transport->sock, NULL, 0, xdr, 5416c7a64e5STrond Myklebust transport->xmit.offset, 542f279cd00SJason Baron true, &sent); 543176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 5446c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 545743c69e7SNeilBrown 546743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 547743c69e7SNeilBrown status = -ENOBUFS; 548743c69e7SNeilBrown 549f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 5506c7a64e5STrond Myklebust transport->xmit.offset += sent; 5516c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 552176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 5536c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 554176e21eeSChuck Lever req->rq_bytes_sent = 0; 5556c7a64e5STrond Myklebust transport->xmit.offset = 0; 556176e21eeSChuck Lever return 0; 557176e21eeSChuck Lever } 558176e21eeSChuck Lever status = -EAGAIN; 559176e21eeSChuck Lever } 560176e21eeSChuck Lever 561176e21eeSChuck Lever switch (status) { 5623601c4a9STrond Myklebust case -ENOBUFS: 563b5872f0cSTrond Myklebust break; 564176e21eeSChuck Lever case -EAGAIN: 565*c544577dSTrond Myklebust status = xs_nospace(req); 566176e21eeSChuck Lever break; 567176e21eeSChuck Lever default: 568176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 569176e21eeSChuck Lever -status); 570e9d47639SGustavo A. R. Silva /* fall through */ 571176e21eeSChuck Lever case -EPIPE: 572176e21eeSChuck Lever xs_close(xprt); 573176e21eeSChuck Lever status = -ENOTCONN; 574176e21eeSChuck Lever } 575176e21eeSChuck Lever 576176e21eeSChuck Lever return status; 577176e21eeSChuck Lever } 578176e21eeSChuck Lever 579176e21eeSChuck Lever /** 580262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 58150f484e2STrond Myklebust * @req: pointer to RPC request 5829903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5839903cd1cSChuck Lever * 5849903cd1cSChuck Lever * Return values: 5859903cd1cSChuck Lever * 0: The request has been sent 5869903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5879903cd1cSChuck Lever * complete the request 588262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 58925985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 5909903cd1cSChuck Lever */ 59150f484e2STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req, struct rpc_task *task) 592a246b010SChuck Lever { 593a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 594ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 595262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 596f279cd00SJason Baron int sent = 0; 597262965f5SChuck Lever int status; 598262965f5SChuck Lever 599262965f5SChuck Lever xs_pktdump("packet data:", 600262965f5SChuck Lever req->rq_svec->iov_base, 601262965f5SChuck Lever req->rq_svec->iov_len); 602262965f5SChuck Lever 60301d37c42STrond Myklebust if (!xprt_bound(xprt)) 60401d37c42STrond Myklebust return -ENOTCONN; 60575891f50STrond Myklebust 60675891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 60775891f50STrond Myklebust return -EBADSLT; 60875891f50STrond Myklebust 60978215759SChuck Lever req->rq_xtime = ktime_get(); 610f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 6116c7a64e5STrond Myklebust xdr, 0, true, &sent); 612262965f5SChuck Lever 613262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 6146c7a64e5STrond Myklebust xdr->len, status); 615262965f5SChuck Lever 6163dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 6173dedbb5cSJason Baron if (status == -EPERM) 6183dedbb5cSJason Baron goto process_status; 6193dedbb5cSJason Baron 620743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 621743c69e7SNeilBrown status = -ENOBUFS; 622743c69e7SNeilBrown 623f279cd00SJason Baron if (sent > 0 || status == 0) { 624f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 625f279cd00SJason Baron if (sent >= req->rq_slen) 626262965f5SChuck Lever return 0; 627262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 628262965f5SChuck Lever status = -EAGAIN; 6292199700fSTrond Myklebust } 630262965f5SChuck Lever 6313dedbb5cSJason Baron process_status: 632262965f5SChuck Lever switch (status) { 633fba91afbSTrond Myklebust case -ENOTSOCK: 634fba91afbSTrond Myklebust status = -ENOTCONN; 635fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 636fba91afbSTrond Myklebust break; 637b6ddf64fSTrond Myklebust case -EAGAIN: 638*c544577dSTrond Myklebust status = xs_nospace(req); 639b6ddf64fSTrond Myklebust break; 640262965f5SChuck Lever case -ENETUNREACH: 6413601c4a9STrond Myklebust case -ENOBUFS: 642262965f5SChuck Lever case -EPIPE: 643262965f5SChuck Lever case -ECONNREFUSED: 6443dedbb5cSJason Baron case -EPERM: 645262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 646262965f5SChuck Lever * prompts ECONNREFUSED. */ 64713331a55STrond Myklebust break; 64813331a55STrond Myklebust default: 64913331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 65013331a55STrond Myklebust -status); 651262965f5SChuck Lever } 6525fe46e9dSBian Naimeng 653262965f5SChuck Lever return status; 654262965f5SChuck Lever } 655262965f5SChuck Lever 656e06799f9STrond Myklebust /** 657262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 65850f484e2STrond Myklebust * @req: pointer to RPC request 659262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 660262965f5SChuck Lever * 661262965f5SChuck Lever * Return values: 662262965f5SChuck Lever * 0: The request has been sent 663262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 664262965f5SChuck Lever * complete the request 665262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 66625985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 667262965f5SChuck Lever * 668262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 669262965f5SChuck Lever * if sendmsg is not able to make progress? 670262965f5SChuck Lever */ 67150f484e2STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req, struct rpc_task *task) 672262965f5SChuck Lever { 673262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 674ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 675262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 676a6b31d18STrond Myklebust bool zerocopy = true; 6779ffadfbcSTrond Myklebust bool vm_wait = false; 678b595bb15SChuck Lever int status; 679f279cd00SJason Baron int sent; 680a246b010SChuck Lever 6814cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 6824cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 6834cd34e7cSTrond Myklebust if (transport->sock != NULL) 6844cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 6854cd34e7cSTrond Myklebust return -ENOTCONN; 6864cd34e7cSTrond Myklebust } 6874cd34e7cSTrond Myklebust 68861677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 689262965f5SChuck Lever 690262965f5SChuck Lever xs_pktdump("packet data:", 691262965f5SChuck Lever req->rq_svec->iov_base, 692262965f5SChuck Lever req->rq_svec->iov_len); 693a6b31d18STrond Myklebust /* Don't use zero copy if this is a resend. If the RPC call 694a6b31d18STrond Myklebust * completes while the socket holds a reference to the pages, 695a6b31d18STrond Myklebust * then we may end up resending corrupted data. 696a6b31d18STrond Myklebust */ 69750f484e2STrond Myklebust if (req->rq_task->tk_flags & RPC_TASK_SENT) 698a6b31d18STrond Myklebust zerocopy = false; 699a246b010SChuck Lever 7007196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 7017196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 7027196dbb0STrond Myklebust 703a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 704a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 705262965f5SChuck Lever * called sendmsg(). */ 70678215759SChuck Lever req->rq_xtime = ktime_get(); 707a246b010SChuck Lever while (1) { 708f279cd00SJason Baron sent = 0; 709f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 7106c7a64e5STrond Myklebust transport->xmit.offset, 7116c7a64e5STrond Myklebust zerocopy, &sent); 712a246b010SChuck Lever 713262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 7146c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 715262965f5SChuck Lever 716a246b010SChuck Lever /* If we've sent the entire packet, immediately 717a246b010SChuck Lever * reset the count of bytes sent. */ 7186c7a64e5STrond Myklebust transport->xmit.offset += sent; 7196c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 720262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 7216c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 722a246b010SChuck Lever req->rq_bytes_sent = 0; 7236c7a64e5STrond Myklebust transport->xmit.offset = 0; 724a246b010SChuck Lever return 0; 725a246b010SChuck Lever } 726262965f5SChuck Lever 7279ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 7289ffadfbcSTrond Myklebust 7299ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 7309ffadfbcSTrond Myklebust /* 7319ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 7329ffadfbcSTrond Myklebust * socket send buffer limits. 7339ffadfbcSTrond Myklebust */ 7349ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 7359ffadfbcSTrond Myklebust break; 7369ffadfbcSTrond Myklebust /* 7379ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 7389ffadfbcSTrond Myklebust */ 7399ffadfbcSTrond Myklebust if (sent == 0) { 7409ffadfbcSTrond Myklebust status = -ENOBUFS; 7419ffadfbcSTrond Myklebust if (vm_wait) 7429ffadfbcSTrond Myklebust break; 7439ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 7449ffadfbcSTrond Myklebust * socket send buffer limit 7459ffadfbcSTrond Myklebust */ 7469ffadfbcSTrond Myklebust vm_wait = true; 7479ffadfbcSTrond Myklebust } 7489ffadfbcSTrond Myklebust continue; 7499ffadfbcSTrond Myklebust } 750f580dd04STrond Myklebust if (status < 0) 751f580dd04STrond Myklebust break; 7529ffadfbcSTrond Myklebust vm_wait = false; 753a246b010SChuck Lever } 754a246b010SChuck Lever 755262965f5SChuck Lever switch (status) { 756fba91afbSTrond Myklebust case -ENOTSOCK: 757fba91afbSTrond Myklebust status = -ENOTCONN; 758fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 759fba91afbSTrond Myklebust break; 760262965f5SChuck Lever case -EAGAIN: 761*c544577dSTrond Myklebust status = xs_nospace(req); 762262965f5SChuck Lever break; 763262965f5SChuck Lever case -ECONNRESET: 764262965f5SChuck Lever case -ECONNREFUSED: 765262965f5SChuck Lever case -ENOTCONN: 7663913c78cSTrond Myklebust case -EADDRINUSE: 767b5872f0cSTrond Myklebust case -ENOBUFS: 768b9d2bb2eSTrond Myklebust case -EPIPE: 76913331a55STrond Myklebust break; 77013331a55STrond Myklebust default: 77113331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 77213331a55STrond Myklebust -status); 773a246b010SChuck Lever } 7745fe46e9dSBian Naimeng 775a246b010SChuck Lever return status; 776a246b010SChuck Lever } 777a246b010SChuck Lever 7782a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7792a9e1cfaSTrond Myklebust { 7802a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7812a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7822a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7832118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7842a9e1cfaSTrond Myklebust } 7852a9e1cfaSTrond Myklebust 7862a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7872a9e1cfaSTrond Myklebust { 7882a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7892a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7902a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7912118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7922118071dSTrond Myklebust } 7932118071dSTrond Myklebust 79442d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 79542d42a5bSTrond Myklebust { 79642d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 79742d42a5bSTrond Myklebust 79842d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 79942d42a5bSTrond Myklebust } 80042d42a5bSTrond Myklebust 801b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 802b70ae915STrond Myklebust { 803b70ae915STrond Myklebust smp_mb__before_atomic(); 804b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 805b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 80642d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 807b70ae915STrond Myklebust smp_mb__after_atomic(); 808b70ae915STrond Myklebust } 809b70ae915STrond Myklebust 8102118071dSTrond Myklebust /** 8112118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 8122118071dSTrond Myklebust * @sk: socket 8132118071dSTrond Myklebust * 8142118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 8152118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 8162118071dSTrond Myklebust */ 8172118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 8182118071dSTrond Myklebust { 8192118071dSTrond Myklebust struct rpc_xprt *xprt; 8202118071dSTrond Myklebust int err; 8212118071dSTrond Myklebust 8222118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 8232118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 8242118071dSTrond Myklebust goto out; 8252118071dSTrond Myklebust 8262118071dSTrond Myklebust err = -sk->sk_err; 8272118071dSTrond Myklebust if (err == 0) 8282118071dSTrond Myklebust goto out; 8292118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 8302118071dSTrond Myklebust xprt, -err); 831e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 8322118071dSTrond Myklebust xprt_wake_pending_tasks(xprt, err); 8332118071dSTrond Myklebust out: 8342118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 8352a9e1cfaSTrond Myklebust } 8362a9e1cfaSTrond Myklebust 837fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 838a246b010SChuck Lever { 839ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 840ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 8416cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 842a246b010SChuck Lever 843fe315e76SChuck Lever if (sk == NULL) 844fe315e76SChuck Lever return; 8459903cd1cSChuck Lever 846264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 847264d1df3SJeff Layton sk_clear_memalloc(sk); 848264d1df3SJeff Layton 84909939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 85009939204STrond Myklebust 851edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 852a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 853ee0ac0c2SChuck Lever transport->inet = NULL; 854ee0ac0c2SChuck Lever transport->sock = NULL; 855a246b010SChuck Lever 856a246b010SChuck Lever sk->sk_user_data = NULL; 8572a9e1cfaSTrond Myklebust 8582a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 8590c78789eSTrond Myklebust xprt_clear_connected(xprt); 860a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 8616cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 862edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 863a246b010SChuck Lever 8646cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 865a246b010SChuck Lever sock_release(sock); 866fe315e76SChuck Lever } 867fe315e76SChuck Lever 868fe315e76SChuck Lever /** 869fe315e76SChuck Lever * xs_close - close a socket 870fe315e76SChuck Lever * @xprt: transport 871fe315e76SChuck Lever * 872fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 873fe315e76SChuck Lever * on the server we want to save. 874f75e6745STrond Myklebust * 875f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 876f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 877fe315e76SChuck Lever */ 878fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 879fe315e76SChuck Lever { 880fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 881fe315e76SChuck Lever 882fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 883fe315e76SChuck Lever 884fe315e76SChuck Lever xs_reset_transport(transport); 88561d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 886fe315e76SChuck Lever 88762da3b24STrond Myklebust xprt_disconnect_done(xprt); 888a246b010SChuck Lever } 889a246b010SChuck Lever 8904a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 8914a068258SChuck Lever { 8924a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 8934a068258SChuck Lever xprt); 8944a068258SChuck Lever xprt_disconnect_done(xprt); 8954a068258SChuck Lever } 8964a068258SChuck Lever 897315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 898315f3812SKinglong Mee { 899315f3812SKinglong Mee xs_free_peer_addresses(xprt); 900315f3812SKinglong Mee xprt_free(xprt); 901315f3812SKinglong Mee } 902315f3812SKinglong Mee 9039903cd1cSChuck Lever /** 9049903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 9059903cd1cSChuck Lever * @xprt: doomed transport 9069903cd1cSChuck Lever * 9079903cd1cSChuck Lever */ 9089903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 909a246b010SChuck Lever { 91003c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 91103c78827STrond Myklebust struct sock_xprt, xprt); 9129903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 9139903cd1cSChuck Lever 91403c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 915a1311d87STrond Myklebust xs_close(xprt); 916edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 917315f3812SKinglong Mee xs_xprt_free(xprt); 918a1311d87STrond Myklebust module_put(THIS_MODULE); 919a246b010SChuck Lever } 920a246b010SChuck Lever 921176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) 922176e21eeSChuck Lever { 923176e21eeSChuck Lever struct xdr_skb_reader desc = { 924176e21eeSChuck Lever .skb = skb, 925176e21eeSChuck Lever .offset = sizeof(rpc_fraghdr), 926176e21eeSChuck Lever .count = skb->len - sizeof(rpc_fraghdr), 927176e21eeSChuck Lever }; 928176e21eeSChuck Lever 929176e21eeSChuck Lever if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) 930176e21eeSChuck Lever return -1; 931176e21eeSChuck Lever if (desc.count) 932176e21eeSChuck Lever return -1; 933176e21eeSChuck Lever return 0; 934176e21eeSChuck Lever } 935176e21eeSChuck Lever 936176e21eeSChuck Lever /** 937a2648094STrond Myklebust * xs_local_data_read_skb 938a2648094STrond Myklebust * @xprt: transport 939a2648094STrond Myklebust * @sk: socket 940a2648094STrond Myklebust * @skb: skbuff 941176e21eeSChuck Lever * 942176e21eeSChuck Lever * Currently this assumes we can read the whole reply in a single gulp. 943176e21eeSChuck Lever */ 944a2648094STrond Myklebust static void xs_local_data_read_skb(struct rpc_xprt *xprt, 945a2648094STrond Myklebust struct sock *sk, 946a2648094STrond Myklebust struct sk_buff *skb) 947176e21eeSChuck Lever { 948176e21eeSChuck Lever struct rpc_task *task; 949176e21eeSChuck Lever struct rpc_rqst *rovr; 950a2648094STrond Myklebust int repsize, copied; 951176e21eeSChuck Lever u32 _xid; 952176e21eeSChuck Lever __be32 *xp; 953176e21eeSChuck Lever 954176e21eeSChuck Lever repsize = skb->len - sizeof(rpc_fraghdr); 955176e21eeSChuck Lever if (repsize < 4) { 956176e21eeSChuck Lever dprintk("RPC: impossible RPC reply size %d\n", repsize); 957a2648094STrond Myklebust return; 958176e21eeSChuck Lever } 959176e21eeSChuck Lever 960176e21eeSChuck Lever /* Copy the XID from the skb... */ 961176e21eeSChuck Lever xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); 962176e21eeSChuck Lever if (xp == NULL) 963a2648094STrond Myklebust return; 964176e21eeSChuck Lever 965176e21eeSChuck Lever /* Look up and lock the request corresponding to the given XID */ 96675c84151STrond Myklebust spin_lock(&xprt->queue_lock); 967176e21eeSChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 968176e21eeSChuck Lever if (!rovr) 969176e21eeSChuck Lever goto out_unlock; 970729749bbSTrond Myklebust xprt_pin_rqst(rovr); 97175c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 972176e21eeSChuck Lever task = rovr->rq_task; 973176e21eeSChuck Lever 974176e21eeSChuck Lever copied = rovr->rq_private_buf.buflen; 975176e21eeSChuck Lever if (copied > repsize) 976176e21eeSChuck Lever copied = repsize; 977176e21eeSChuck Lever 978176e21eeSChuck Lever if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { 979176e21eeSChuck Lever dprintk("RPC: sk_buff copy failed\n"); 98075c84151STrond Myklebust spin_lock(&xprt->queue_lock); 981729749bbSTrond Myklebust goto out_unpin; 982176e21eeSChuck Lever } 983176e21eeSChuck Lever 98475c84151STrond Myklebust spin_lock(&xprt->queue_lock); 985176e21eeSChuck Lever xprt_complete_rqst(task, copied); 986729749bbSTrond Myklebust out_unpin: 987729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 988176e21eeSChuck Lever out_unlock: 98975c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 990a2648094STrond Myklebust } 991a2648094STrond Myklebust 992a2648094STrond Myklebust static void xs_local_data_receive(struct sock_xprt *transport) 993a2648094STrond Myklebust { 994a2648094STrond Myklebust struct sk_buff *skb; 995a2648094STrond Myklebust struct sock *sk; 996a2648094STrond Myklebust int err; 997a2648094STrond Myklebust 9980af3442aSTrond Myklebust restart: 999a2648094STrond Myklebust mutex_lock(&transport->recv_mutex); 1000a2648094STrond Myklebust sk = transport->inet; 1001a2648094STrond Myklebust if (sk == NULL) 1002a2648094STrond Myklebust goto out; 1003a2648094STrond Myklebust for (;;) { 1004a2648094STrond Myklebust skb = skb_recv_datagram(sk, 0, 1, &err); 100542d42a5bSTrond Myklebust if (skb != NULL) { 1006a2648094STrond Myklebust xs_local_data_read_skb(&transport->xprt, sk, skb); 1007176e21eeSChuck Lever skb_free_datagram(sk, skb); 100842d42a5bSTrond Myklebust continue; 100942d42a5bSTrond Myklebust } 101042d42a5bSTrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 101142d42a5bSTrond Myklebust break; 10120af3442aSTrond Myklebust if (need_resched()) { 10130af3442aSTrond Myklebust mutex_unlock(&transport->recv_mutex); 10140af3442aSTrond Myklebust cond_resched(); 10150af3442aSTrond Myklebust goto restart; 10160af3442aSTrond Myklebust } 1017a2648094STrond Myklebust } 1018176e21eeSChuck Lever out: 1019a2648094STrond Myklebust mutex_unlock(&transport->recv_mutex); 1020a2648094STrond Myklebust } 1021a2648094STrond Myklebust 1022a2648094STrond Myklebust static void xs_local_data_receive_workfn(struct work_struct *work) 1023a2648094STrond Myklebust { 1024a2648094STrond Myklebust struct sock_xprt *transport = 1025a2648094STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1026a2648094STrond Myklebust xs_local_data_receive(transport); 1027176e21eeSChuck Lever } 1028176e21eeSChuck Lever 10299903cd1cSChuck Lever /** 1030f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1031f9b2ee71STrond Myklebust * @xprt: transport 1032f9b2ee71STrond Myklebust * @sk: socket 1033f9b2ee71STrond Myklebust * @skb: skbuff 10349903cd1cSChuck Lever * 1035a246b010SChuck Lever */ 1036f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1037f9b2ee71STrond Myklebust struct sock *sk, 1038f9b2ee71STrond Myklebust struct sk_buff *skb) 1039a246b010SChuck Lever { 1040a246b010SChuck Lever struct rpc_task *task; 1041a246b010SChuck Lever struct rpc_rqst *rovr; 1042f9b2ee71STrond Myklebust int repsize, copied; 1043d8ed029dSAlexey Dobriyan u32 _xid; 1044d8ed029dSAlexey Dobriyan __be32 *xp; 1045a246b010SChuck Lever 10461da8c681SWillem de Bruijn repsize = skb->len; 1047a246b010SChuck Lever if (repsize < 4) { 10489903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1049f9b2ee71STrond Myklebust return; 1050a246b010SChuck Lever } 1051a246b010SChuck Lever 1052a246b010SChuck Lever /* Copy the XID from the skb... */ 10531da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1054a246b010SChuck Lever if (xp == NULL) 1055f9b2ee71STrond Myklebust return; 1056a246b010SChuck Lever 1057a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 105875c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1059a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1060a246b010SChuck Lever if (!rovr) 1061a246b010SChuck Lever goto out_unlock; 1062729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1063ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 106475c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1065a246b010SChuck Lever task = rovr->rq_task; 1066a246b010SChuck Lever 1067a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1068a246b010SChuck Lever copied = repsize; 1069a246b010SChuck Lever 1070a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 10711781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 107275c84151STrond Myklebust spin_lock(&xprt->queue_lock); 10730afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1074729749bbSTrond Myklebust goto out_unpin; 10751781f7f5SHerbert Xu } 10761781f7f5SHerbert Xu 1077a246b010SChuck Lever 1078729749bbSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 10796a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1080ce7c252aSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 108175c84151STrond Myklebust spin_lock(&xprt->queue_lock); 10821570c1e4SChuck Lever xprt_complete_rqst(task, copied); 10830afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1084729749bbSTrond Myklebust out_unpin: 1085729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1086a246b010SChuck Lever out_unlock: 108775c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1088f9b2ee71STrond Myklebust } 1089f9b2ee71STrond Myklebust 1090f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1091f9b2ee71STrond Myklebust { 1092f9b2ee71STrond Myklebust struct sk_buff *skb; 1093f9b2ee71STrond Myklebust struct sock *sk; 1094f9b2ee71STrond Myklebust int err; 1095f9b2ee71STrond Myklebust 10960af3442aSTrond Myklebust restart: 1097f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1098f9b2ee71STrond Myklebust sk = transport->inet; 1099f9b2ee71STrond Myklebust if (sk == NULL) 1100f9b2ee71STrond Myklebust goto out; 1101f9b2ee71STrond Myklebust for (;;) { 11027c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 110342d42a5bSTrond Myklebust if (skb != NULL) { 1104f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1105850cbaddSPaolo Abeni consume_skb(skb); 110642d42a5bSTrond Myklebust continue; 110742d42a5bSTrond Myklebust } 110842d42a5bSTrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 110942d42a5bSTrond Myklebust break; 11100af3442aSTrond Myklebust if (need_resched()) { 11110af3442aSTrond Myklebust mutex_unlock(&transport->recv_mutex); 11120af3442aSTrond Myklebust cond_resched(); 11130af3442aSTrond Myklebust goto restart; 11140af3442aSTrond Myklebust } 1115f9b2ee71STrond Myklebust } 1116a246b010SChuck Lever out: 1117f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1118f9b2ee71STrond Myklebust } 1119f9b2ee71STrond Myklebust 1120f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1121f9b2ee71STrond Myklebust { 1122f9b2ee71STrond Myklebust struct sock_xprt *transport = 1123f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1124f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1125f9b2ee71STrond Myklebust } 1126f9b2ee71STrond Myklebust 1127f9b2ee71STrond Myklebust /** 1128f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1129f9b2ee71STrond Myklebust * @sk: socket with data to read 1130f9b2ee71STrond Myklebust * 1131f9b2ee71STrond Myklebust */ 1132f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1133f9b2ee71STrond Myklebust { 1134f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1135f9b2ee71STrond Myklebust 1136f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1137f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1138f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1139f9b2ee71STrond Myklebust if (xprt != NULL) { 1140f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1141f9b2ee71STrond Myklebust struct sock_xprt, xprt); 11425157b956STrond Myklebust transport->old_data_ready(sk); 11435157b956STrond Myklebust /* Any data means we had a useful conversation, so 11445157b956STrond Myklebust * then we don't need to delay the next reconnect 11455157b956STrond Myklebust */ 11465157b956STrond Myklebust if (xprt->reestablish_timeout) 11475157b956STrond Myklebust xprt->reestablish_timeout = 0; 114842d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 114940a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1150f9b2ee71STrond Myklebust } 1151f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1152a246b010SChuck Lever } 1153a246b010SChuck Lever 1154a519fc7aSTrond Myklebust /* 1155a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1156a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1157a519fc7aSTrond Myklebust */ 1158a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1159a519fc7aSTrond Myklebust { 1160a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1161a519fc7aSTrond Myklebust } 1162a519fc7aSTrond Myklebust 1163dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1164a246b010SChuck Lever { 116551971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1166a246b010SChuck Lever size_t len, used; 1167a246b010SChuck Lever char *p; 1168a246b010SChuck Lever 1169d1109aa5STrond Myklebust p = ((char *) &transport->recv.fraghdr) + transport->recv.offset; 1170d1109aa5STrond Myklebust len = sizeof(transport->recv.fraghdr) - transport->recv.offset; 11719d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 1172d1109aa5STrond Myklebust transport->recv.offset += used; 1173a246b010SChuck Lever if (used != len) 1174a246b010SChuck Lever return; 1175808012fbSChuck Lever 1176d1109aa5STrond Myklebust transport->recv.len = ntohl(transport->recv.fraghdr); 1177d1109aa5STrond Myklebust if (transport->recv.len & RPC_LAST_STREAM_FRAGMENT) 1178d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_LAST_FRAG; 1179a246b010SChuck Lever else 1180d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_LAST_FRAG; 1181d1109aa5STrond Myklebust transport->recv.len &= RPC_FRAGMENT_SIZE_MASK; 1182808012fbSChuck Lever 1183d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_FRAGHDR; 1184d1109aa5STrond Myklebust transport->recv.offset = 0; 1185808012fbSChuck Lever 1186a246b010SChuck Lever /* Sanity check of the record length */ 1187d1109aa5STrond Myklebust if (unlikely(transport->recv.len < 8)) { 11889903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 1189a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 11909903cd1cSChuck Lever return; 1191a246b010SChuck Lever } 1192a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 1193d1109aa5STrond Myklebust transport->recv.len); 1194a246b010SChuck Lever } 1195a246b010SChuck Lever 119651971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 1197a246b010SChuck Lever { 1198d1109aa5STrond Myklebust if (transport->recv.offset == transport->recv.len) { 1199d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_FRAGHDR; 1200d1109aa5STrond Myklebust transport->recv.offset = 0; 1201d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_LAST_FRAG) { 1202d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1203d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_XID; 1204d1109aa5STrond Myklebust transport->recv.copied = 0; 1205a246b010SChuck Lever } 1206a246b010SChuck Lever } 1207a246b010SChuck Lever } 1208a246b010SChuck Lever 1209dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1210a246b010SChuck Lever { 1211a246b010SChuck Lever size_t len, used; 1212a246b010SChuck Lever char *p; 1213a246b010SChuck Lever 1214d1109aa5STrond Myklebust len = sizeof(transport->recv.xid) - transport->recv.offset; 12155b5e0928SAlexey Dobriyan dprintk("RPC: reading XID (%zu bytes)\n", len); 1216d1109aa5STrond Myklebust p = ((char *) &transport->recv.xid) + transport->recv.offset; 12179d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 1218d1109aa5STrond Myklebust transport->recv.offset += used; 1219a246b010SChuck Lever if (used != len) 1220a246b010SChuck Lever return; 1221d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_XID; 1222d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_READ_CALLDIR; 1223d1109aa5STrond Myklebust transport->recv.copied = 4; 122418dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 1225d1109aa5STrond Myklebust (transport->recv.flags & TCP_RPC_REPLY) ? "reply for" 122618dca02aSRicardo Labiaga : "request with", 1227d1109aa5STrond Myklebust ntohl(transport->recv.xid)); 122851971139SChuck Lever xs_tcp_check_fraghdr(transport); 1229a246b010SChuck Lever } 1230a246b010SChuck Lever 123118dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 123218dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1233a246b010SChuck Lever { 123418dca02aSRicardo Labiaga size_t len, used; 123518dca02aSRicardo Labiaga u32 offset; 1236b76ce561STrond Myklebust char *p; 123718dca02aSRicardo Labiaga 123818dca02aSRicardo Labiaga /* 1239d1109aa5STrond Myklebust * We want transport->recv.offset to be 8 at the end of this routine 124018dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 124118dca02aSRicardo Labiaga * When this function is called for the first time, 1242d1109aa5STrond Myklebust * transport->recv.offset is 4 (after having already read the xid). 124318dca02aSRicardo Labiaga */ 1244d1109aa5STrond Myklebust offset = transport->recv.offset - sizeof(transport->recv.xid); 1245d1109aa5STrond Myklebust len = sizeof(transport->recv.calldir) - offset; 12465b5e0928SAlexey Dobriyan dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len); 1247d1109aa5STrond Myklebust p = ((char *) &transport->recv.calldir) + offset; 1248b76ce561STrond Myklebust used = xdr_skb_read_bits(desc, p, len); 1249d1109aa5STrond Myklebust transport->recv.offset += used; 125018dca02aSRicardo Labiaga if (used != len) 125118dca02aSRicardo Labiaga return; 1252d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_READ_CALLDIR; 1253f4a2e418SRicardo Labiaga /* 1254f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1255f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1256f4a2e418SRicardo Labiaga */ 1257d1109aa5STrond Myklebust switch (ntohl(transport->recv.calldir)) { 1258b76ce561STrond Myklebust case RPC_REPLY: 1259d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_CALLDIR; 1260d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_DATA; 1261d1109aa5STrond Myklebust transport->recv.flags |= TCP_RPC_REPLY; 1262b76ce561STrond Myklebust break; 1263b76ce561STrond Myklebust case RPC_CALL: 1264d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_CALLDIR; 1265d1109aa5STrond Myklebust transport->recv.flags |= TCP_RCV_COPY_DATA; 1266d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RPC_REPLY; 1267b76ce561STrond Myklebust break; 1268b76ce561STrond Myklebust default: 1269b76ce561STrond Myklebust dprintk("RPC: invalid request message type\n"); 1270a519fc7aSTrond Myklebust xs_tcp_force_close(&transport->xprt); 1271b76ce561STrond Myklebust } 127218dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 127318dca02aSRicardo Labiaga } 127418dca02aSRicardo Labiaga 127544b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 127644b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 127744b98efdSRicardo Labiaga struct rpc_rqst *req) 1278a246b010SChuck Lever { 127944b98efdSRicardo Labiaga struct sock_xprt *transport = 128044b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1281a246b010SChuck Lever struct xdr_buf *rcvbuf; 1282a246b010SChuck Lever size_t len; 1283a246b010SChuck Lever ssize_t r; 1284a246b010SChuck Lever 1285a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1286f4a2e418SRicardo Labiaga 1287d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_CALLDIR) { 1288f4a2e418SRicardo Labiaga /* 1289f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1290f4a2e418SRicardo Labiaga */ 1291d1109aa5STrond Myklebust memcpy(rcvbuf->head[0].iov_base + transport->recv.copied, 1292d1109aa5STrond Myklebust &transport->recv.calldir, 1293d1109aa5STrond Myklebust sizeof(transport->recv.calldir)); 1294d1109aa5STrond Myklebust transport->recv.copied += sizeof(transport->recv.calldir); 1295d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_CALLDIR; 1296a246b010SChuck Lever } 1297a246b010SChuck Lever 1298a246b010SChuck Lever len = desc->count; 1299d1109aa5STrond Myklebust if (len > transport->recv.len - transport->recv.offset) 1300d1109aa5STrond Myklebust desc->count = transport->recv.len - transport->recv.offset; 1301d1109aa5STrond Myklebust r = xdr_partial_copy_from_skb(rcvbuf, transport->recv.copied, 13029d292316SChuck Lever desc, xdr_skb_read_bits); 1303a246b010SChuck Lever 1304040249dfSTrond Myklebust if (desc->count) { 1305a246b010SChuck Lever /* Error when copying to the receive buffer, 1306a246b010SChuck Lever * usually because we weren't able to allocate 1307a246b010SChuck Lever * additional buffer pages. All we can do now 1308e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1309a246b010SChuck Lever * will not receive any additional updates, 1310a246b010SChuck Lever * and time out. 1311a246b010SChuck Lever * Any remaining data from this record will 1312a246b010SChuck Lever * be discarded. 1313a246b010SChuck Lever */ 1314d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1315a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 1316d1109aa5STrond Myklebust ntohl(transport->recv.xid)); 1317d1109aa5STrond Myklebust dprintk("RPC: xprt = %p, recv.copied = %lu, " 1318d1109aa5STrond Myklebust "recv.offset = %u, recv.len = %u\n", 1319d1109aa5STrond Myklebust xprt, transport->recv.copied, 1320d1109aa5STrond Myklebust transport->recv.offset, transport->recv.len); 132144b98efdSRicardo Labiaga return; 1322a246b010SChuck Lever } 1323a246b010SChuck Lever 1324d1109aa5STrond Myklebust transport->recv.copied += r; 1325d1109aa5STrond Myklebust transport->recv.offset += r; 1326040249dfSTrond Myklebust desc->count = len - r; 1327040249dfSTrond Myklebust 13285b5e0928SAlexey Dobriyan dprintk("RPC: XID %08x read %zd bytes\n", 1329d1109aa5STrond Myklebust ntohl(transport->recv.xid), r); 1330d1109aa5STrond Myklebust dprintk("RPC: xprt = %p, recv.copied = %lu, recv.offset = %u, " 1331d1109aa5STrond Myklebust "recv.len = %u\n", xprt, transport->recv.copied, 1332d1109aa5STrond Myklebust transport->recv.offset, transport->recv.len); 1333a246b010SChuck Lever 1334d1109aa5STrond Myklebust if (transport->recv.copied == req->rq_private_buf.buflen) 1335d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1336d1109aa5STrond Myklebust else if (transport->recv.offset == transport->recv.len) { 1337d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_LAST_FRAG) 1338d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 1339a246b010SChuck Lever } 134044b98efdSRicardo Labiaga } 134144b98efdSRicardo Labiaga 134244b98efdSRicardo Labiaga /* 134344b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 134444b98efdSRicardo Labiaga * tcp read code to read the data. 134544b98efdSRicardo Labiaga */ 134644b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 134744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 134844b98efdSRicardo Labiaga { 134944b98efdSRicardo Labiaga struct sock_xprt *transport = 135044b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 135144b98efdSRicardo Labiaga struct rpc_rqst *req; 135244b98efdSRicardo Labiaga 1353d1109aa5STrond Myklebust dprintk("RPC: read reply XID %08x\n", ntohl(transport->recv.xid)); 135444b98efdSRicardo Labiaga 135544b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 135675c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1357d1109aa5STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 135844b98efdSRicardo Labiaga if (!req) { 135944b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 1360d1109aa5STrond Myklebust ntohl(transport->recv.xid)); 136175c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 136244b98efdSRicardo Labiaga return -1; 136344b98efdSRicardo Labiaga } 1364729749bbSTrond Myklebust xprt_pin_rqst(req); 136575c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 136644b98efdSRicardo Labiaga 136744b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 136844b98efdSRicardo Labiaga 136975c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1370d1109aa5STrond Myklebust if (!(transport->recv.flags & TCP_RCV_COPY_DATA)) 1371d1109aa5STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 1372729749bbSTrond Myklebust xprt_unpin_rqst(req); 137375c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 137444b98efdSRicardo Labiaga return 0; 137544b98efdSRicardo Labiaga } 137644b98efdSRicardo Labiaga 13779e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 137844b98efdSRicardo Labiaga /* 137944b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 138044b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 138144b98efdSRicardo Labiaga * queue. 138244b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 138344b98efdSRicardo Labiaga * connection and return -1. 138444b98efdSRicardo Labiaga */ 13852ea24497STrond Myklebust static int xs_tcp_read_callback(struct rpc_xprt *xprt, 138644b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 138744b98efdSRicardo Labiaga { 138844b98efdSRicardo Labiaga struct sock_xprt *transport = 138944b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 139044b98efdSRicardo Labiaga struct rpc_rqst *req; 139144b98efdSRicardo Labiaga 1392c89091c8STrond Myklebust /* Look up the request corresponding to the given XID */ 1393d1109aa5STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 139444b98efdSRicardo Labiaga if (req == NULL) { 139544b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 139644b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 139744b98efdSRicardo Labiaga return -1; 139844b98efdSRicardo Labiaga } 139944b98efdSRicardo Labiaga 140044b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 140144b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 140244b98efdSRicardo Labiaga 1403d1109aa5STrond Myklebust if (!(transport->recv.flags & TCP_RCV_COPY_DATA)) 1404d1109aa5STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 140544b98efdSRicardo Labiaga 140644b98efdSRicardo Labiaga return 0; 140744b98efdSRicardo Labiaga } 140844b98efdSRicardo Labiaga 140944b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 141044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 141144b98efdSRicardo Labiaga { 141244b98efdSRicardo Labiaga struct sock_xprt *transport = 141344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 141444b98efdSRicardo Labiaga 1415d1109aa5STrond Myklebust return (transport->recv.flags & TCP_RPC_REPLY) ? 141644b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 141744b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 141844b98efdSRicardo Labiaga } 141976566773SChuck Lever 142076566773SChuck Lever static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net) 142176566773SChuck Lever { 142276566773SChuck Lever int ret; 142376566773SChuck Lever 142476566773SChuck Lever ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0, 142576566773SChuck Lever SVC_SOCK_ANONYMOUS); 142676566773SChuck Lever if (ret < 0) 142776566773SChuck Lever return ret; 142876566773SChuck Lever return 0; 142976566773SChuck Lever } 14306b26cc8cSChuck Lever 14316b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14326b26cc8cSChuck Lever { 14336b26cc8cSChuck Lever return PAGE_SIZE; 14346b26cc8cSChuck Lever } 143544b98efdSRicardo Labiaga #else 143644b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 143744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 143844b98efdSRicardo Labiaga { 143944b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 144044b98efdSRicardo Labiaga } 14419e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 144244b98efdSRicardo Labiaga 144344b98efdSRicardo Labiaga /* 144444b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 144544b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 144644b98efdSRicardo Labiaga */ 144744b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 144844b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 144944b98efdSRicardo Labiaga { 145044b98efdSRicardo Labiaga struct sock_xprt *transport = 145144b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 145244b98efdSRicardo Labiaga 145344b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 145451971139SChuck Lever xs_tcp_check_fraghdr(transport); 145544b98efdSRicardo Labiaga else { 145644b98efdSRicardo Labiaga /* 145744b98efdSRicardo Labiaga * The transport_lock protects the request handling. 1458d1109aa5STrond Myklebust * There's no need to hold it to update the recv.flags. 145944b98efdSRicardo Labiaga */ 1460d1109aa5STrond Myklebust transport->recv.flags &= ~TCP_RCV_COPY_DATA; 146144b98efdSRicardo Labiaga } 1462a246b010SChuck Lever } 1463a246b010SChuck Lever 1464dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1465a246b010SChuck Lever { 1466a246b010SChuck Lever size_t len; 1467a246b010SChuck Lever 1468d1109aa5STrond Myklebust len = transport->recv.len - transport->recv.offset; 1469a246b010SChuck Lever if (len > desc->count) 1470a246b010SChuck Lever len = desc->count; 1471a246b010SChuck Lever desc->count -= len; 1472a246b010SChuck Lever desc->offset += len; 1473d1109aa5STrond Myklebust transport->recv.offset += len; 14745b5e0928SAlexey Dobriyan dprintk("RPC: discarded %zu bytes\n", len); 147551971139SChuck Lever xs_tcp_check_fraghdr(transport); 1476a246b010SChuck Lever } 1477a246b010SChuck Lever 14789903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1479a246b010SChuck Lever { 1480a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 148151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1482dd456471SChuck Lever struct xdr_skb_reader desc = { 1483a246b010SChuck Lever .skb = skb, 1484a246b010SChuck Lever .offset = offset, 1485a246b010SChuck Lever .count = len, 1486a246b010SChuck Lever }; 14873d188805STrond Myklebust size_t ret; 1488a246b010SChuck Lever 14899903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1490a246b010SChuck Lever do { 14911a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 1492a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1493a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1494d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_FRAGHDR) { 14959903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1496a246b010SChuck Lever continue; 1497a246b010SChuck Lever } 1498a246b010SChuck Lever /* Read in the xid if necessary */ 1499d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_XID) { 150051971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1501a246b010SChuck Lever continue; 1502a246b010SChuck Lever } 150318dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1504d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_READ_CALLDIR) { 150518dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 150618dca02aSRicardo Labiaga continue; 150718dca02aSRicardo Labiaga } 1508a246b010SChuck Lever /* Read in the request data */ 1509d1109aa5STrond Myklebust if (transport->recv.flags & TCP_RCV_COPY_DATA) { 151044b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1511a246b010SChuck Lever continue; 1512a246b010SChuck Lever } 1513a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 151451971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1515a246b010SChuck Lever } while (desc.count); 15163d188805STrond Myklebust ret = len - desc.count; 15173d188805STrond Myklebust if (ret < rd_desc->count) 15183d188805STrond Myklebust rd_desc->count -= ret; 15193d188805STrond Myklebust else 15203d188805STrond Myklebust rd_desc->count = 0; 15211a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 15229903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 15233d188805STrond Myklebust return ret; 1524a246b010SChuck Lever } 1525a246b010SChuck Lever 152666d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport) 152766d7a56aSTrond Myklebust { 152866d7a56aSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 152966d7a56aSTrond Myklebust struct sock *sk; 153066d7a56aSTrond Myklebust read_descriptor_t rd_desc = { 153166d7a56aSTrond Myklebust .arg.data = xprt, 153266d7a56aSTrond Myklebust }; 153366d7a56aSTrond Myklebust unsigned long total = 0; 153466d7a56aSTrond Myklebust int read = 0; 153566d7a56aSTrond Myklebust 15360af3442aSTrond Myklebust restart: 1537edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 153866d7a56aSTrond Myklebust sk = transport->inet; 1539edc1b01cSTrond Myklebust if (sk == NULL) 1540edc1b01cSTrond Myklebust goto out; 154166d7a56aSTrond Myklebust 154266d7a56aSTrond Myklebust /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 15430af3442aSTrond Myklebust for (;;) { 15443d188805STrond Myklebust rd_desc.count = RPC_TCP_READ_CHUNK_SZ; 1545edc1b01cSTrond Myklebust lock_sock(sk); 154666d7a56aSTrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 15473d188805STrond Myklebust if (rd_desc.count != 0 || read < 0) { 154842d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 1549edc1b01cSTrond Myklebust release_sock(sk); 155066d7a56aSTrond Myklebust break; 15518d6f97d6STrond Myklebust } 155242d42a5bSTrond Myklebust release_sock(sk); 155366d7a56aSTrond Myklebust total += read; 15540af3442aSTrond Myklebust if (need_resched()) { 15550af3442aSTrond Myklebust mutex_unlock(&transport->recv_mutex); 15560af3442aSTrond Myklebust cond_resched(); 15570af3442aSTrond Myklebust goto restart; 15580af3442aSTrond Myklebust } 155966d7a56aSTrond Myklebust } 15608d6f97d6STrond Myklebust if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 15618d6f97d6STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1562edc1b01cSTrond Myklebust out: 1563edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 156466d7a56aSTrond Myklebust trace_xs_tcp_data_ready(xprt, read, total); 156566d7a56aSTrond Myklebust } 156666d7a56aSTrond Myklebust 1567edc1b01cSTrond Myklebust static void xs_tcp_data_receive_workfn(struct work_struct *work) 1568edc1b01cSTrond Myklebust { 1569edc1b01cSTrond Myklebust struct sock_xprt *transport = 1570edc1b01cSTrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1571edc1b01cSTrond Myklebust xs_tcp_data_receive(transport); 1572edc1b01cSTrond Myklebust } 1573edc1b01cSTrond Myklebust 15749903cd1cSChuck Lever /** 15759903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 15769903cd1cSChuck Lever * @sk: socket whose state has changed 15779903cd1cSChuck Lever * 15789903cd1cSChuck Lever */ 15799903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1580a246b010SChuck Lever { 1581a246b010SChuck Lever struct rpc_xprt *xprt; 15820fdea1e8STrond Myklebust struct sock_xprt *transport; 1583a246b010SChuck Lever 1584f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1585a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1586a246b010SChuck Lever goto out; 15879903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1588669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1589a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1590a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1591669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1592669502ffSAndy Chittenden sk->sk_shutdown); 1593a246b010SChuck Lever 15940fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 159540b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1596a246b010SChuck Lever switch (sk->sk_state) { 1597a246b010SChuck Lever case TCP_ESTABLISHED: 1598f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1599a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 16008b71798cSTrond Myklebust xprt->connect_cookie++; 16010fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 16020fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 160351971139SChuck Lever 16042a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1605a246b010SChuck Lever } 1606f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1607a246b010SChuck Lever break; 16083b948ae5STrond Myklebust case TCP_FIN_WAIT1: 16093b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 16107c1d71cfSTrond Myklebust xprt->connect_cookie++; 1611663b8858STrond Myklebust xprt->reestablish_timeout = 0; 16123b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 16134e857c58SPeter Zijlstra smp_mb__before_atomic(); 16143b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1615ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 16164e857c58SPeter Zijlstra smp_mb__after_atomic(); 1617a246b010SChuck Lever break; 1618632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 16193b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 16207c1d71cfSTrond Myklebust xprt->connect_cookie++; 1621d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1622a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1623e9d47639SGustavo A. R. Silva /* fall through */ 1624663b8858STrond Myklebust case TCP_CLOSING: 1625663b8858STrond Myklebust /* 1626663b8858STrond Myklebust * If the server closed down the connection, make sure that 1627663b8858STrond Myklebust * we back off before reconnecting 1628663b8858STrond Myklebust */ 1629663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1630663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 16313b948ae5STrond Myklebust break; 16323b948ae5STrond Myklebust case TCP_LAST_ACK: 1633670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 16344e857c58SPeter Zijlstra smp_mb__before_atomic(); 16353b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 16364e857c58SPeter Zijlstra smp_mb__after_atomic(); 16373b948ae5STrond Myklebust break; 16383b948ae5STrond Myklebust case TCP_CLOSE: 16390fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 16400fdea1e8STrond Myklebust &transport->sock_state)) 16410fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 16429b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 16433ffbc1d6SNeilBrown if (sk->sk_err) 16443ffbc1d6SNeilBrown xprt_wake_pending_tasks(xprt, -sk->sk_err); 16459b30889cSTrond Myklebust /* Trigger the socket release */ 16469b30889cSTrond Myklebust xs_tcp_force_close(xprt); 1647a246b010SChuck Lever } 1648a246b010SChuck Lever out: 1649f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1650a246b010SChuck Lever } 1651a246b010SChuck Lever 16521f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 16531f0fa154SIlpo Järvinen { 165413331a55STrond Myklebust struct socket_wq *wq; 16551f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 16561f0fa154SIlpo Järvinen 165713331a55STrond Myklebust if (!sk->sk_socket) 16581f0fa154SIlpo Järvinen return; 165913331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16601f0fa154SIlpo Järvinen 16611f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 16621f0fa154SIlpo Järvinen return; 166313331a55STrond Myklebust rcu_read_lock(); 166413331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 166513331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 166613331a55STrond Myklebust goto out; 16671f0fa154SIlpo Järvinen 1668*c544577dSTrond Myklebust if (xprt_write_space(xprt)) 1669*c544577dSTrond Myklebust sk->sk_write_pending--; 167013331a55STrond Myklebust out: 167113331a55STrond Myklebust rcu_read_unlock(); 16721f0fa154SIlpo Järvinen } 16731f0fa154SIlpo Järvinen 16742a9e1cfaSTrond Myklebust /** 1675c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1676c7b2cae8SChuck Lever * becomes available 16779903cd1cSChuck Lever * @sk: socket whose state has changed 16789903cd1cSChuck Lever * 1679a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1680a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1681c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1682a246b010SChuck Lever * with a bunch of small requests. 1683a246b010SChuck Lever */ 1684c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1685a246b010SChuck Lever { 1686f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1687c7b2cae8SChuck Lever 1688c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16891f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16901f0fa154SIlpo Järvinen xs_write_space(sk); 1691c7b2cae8SChuck Lever 1692f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1693c7b2cae8SChuck Lever } 1694c7b2cae8SChuck Lever 1695c7b2cae8SChuck Lever /** 1696c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1697c7b2cae8SChuck Lever * becomes available 1698c7b2cae8SChuck Lever * @sk: socket whose state has changed 1699c7b2cae8SChuck Lever * 1700c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1701c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1702c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1703c7b2cae8SChuck Lever * with a bunch of small requests. 1704c7b2cae8SChuck Lever */ 1705c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1706c7b2cae8SChuck Lever { 1707f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1708c7b2cae8SChuck Lever 1709c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 171064dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 17111f0fa154SIlpo Järvinen xs_write_space(sk); 1712c7b2cae8SChuck Lever 1713f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1714a246b010SChuck Lever } 1715a246b010SChuck Lever 1716470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1717a246b010SChuck Lever { 1718ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1719ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1720a246b010SChuck Lever 17217c6e066eSChuck Lever if (transport->rcvsize) { 1722a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 17237c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1724a246b010SChuck Lever } 17257c6e066eSChuck Lever if (transport->sndsize) { 1726a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 17277c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1728a246b010SChuck Lever sk->sk_write_space(sk); 1729a246b010SChuck Lever } 1730a246b010SChuck Lever } 1731a246b010SChuck Lever 173243118c29SChuck Lever /** 1733470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 173443118c29SChuck Lever * @xprt: generic transport 1735470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1736470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 173743118c29SChuck Lever * 1738470056c2SChuck Lever * Set socket send and receive buffer size limits. 173943118c29SChuck Lever */ 1740470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 174143118c29SChuck Lever { 17427c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 17437c6e066eSChuck Lever 17447c6e066eSChuck Lever transport->sndsize = 0; 1745470056c2SChuck Lever if (sndsize) 17467c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 17477c6e066eSChuck Lever transport->rcvsize = 0; 1748470056c2SChuck Lever if (rcvsize) 17497c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1750470056c2SChuck Lever 1751470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 175243118c29SChuck Lever } 175343118c29SChuck Lever 175446c0ee8bSChuck Lever /** 175546c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 175646c0ee8bSChuck Lever * @task: task that timed out 175746c0ee8bSChuck Lever * 175846c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 175946c0ee8bSChuck Lever */ 17606a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 176146c0ee8bSChuck Lever { 1762b977b644SChuck Lever spin_lock_bh(&xprt->transport_lock); 17636a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1764b977b644SChuck Lever spin_unlock_bh(&xprt->transport_lock); 176546c0ee8bSChuck Lever } 176646c0ee8bSChuck Lever 1767b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1768b85d8806SChuck Lever { 17695d71899aSFrank Sorenson unsigned short range = xprt_max_resvport - xprt_min_resvport + 1; 177063862b5bSAruna-Hewapathirane unsigned short rand = (unsigned short) prandom_u32() % range; 1771b85d8806SChuck Lever return rand + xprt_min_resvport; 1772b85d8806SChuck Lever } 1773b85d8806SChuck Lever 177492200412SChuck Lever /** 17754dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17764dda9c8aSTrond Myklebust * @sock: socket 17774dda9c8aSTrond Myklebust * 17784dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17794dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17804dda9c8aSTrond Myklebust */ 17814dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17824dda9c8aSTrond Myklebust { 1783402e23b4STrond Myklebust int opt = 1; 17844dda9c8aSTrond Myklebust 1785402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1786402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17874dda9c8aSTrond Myklebust } 17884dda9c8aSTrond Myklebust 17894dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17904dda9c8aSTrond Myklebust { 17914dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17924dda9c8aSTrond Myklebust unsigned short port = 0; 17934dda9c8aSTrond Myklebust 17949b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 17954dda9c8aSTrond Myklebust goto out; 17964dda9c8aSTrond Myklebust switch (buf.ss_family) { 17974dda9c8aSTrond Myklebust case AF_INET6: 17984dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17994dda9c8aSTrond Myklebust break; 18004dda9c8aSTrond Myklebust case AF_INET: 18014dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 18024dda9c8aSTrond Myklebust } 18034dda9c8aSTrond Myklebust out: 18044dda9c8aSTrond Myklebust return port; 18054dda9c8aSTrond Myklebust } 18064dda9c8aSTrond Myklebust 18074dda9c8aSTrond Myklebust /** 180892200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 180992200412SChuck Lever * @xprt: generic transport 181092200412SChuck Lever * @port: new port number 181192200412SChuck Lever * 181292200412SChuck Lever */ 181392200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 181492200412SChuck Lever { 181592200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1816c4efcb1dSChuck Lever 18179dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 18189dc3b095SChuck Lever xs_update_peer_port(xprt); 181992200412SChuck Lever } 182092200412SChuck Lever 18214dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 18224dda9c8aSTrond Myklebust { 18234dda9c8aSTrond Myklebust if (transport->srcport == 0) 18244dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 18254dda9c8aSTrond Myklebust } 18264dda9c8aSTrond Myklebust 18275d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport) 182867a391d7STrond Myklebust { 1829fbfffbd5SChuck Lever unsigned short port = transport->srcport; 183067a391d7STrond Myklebust 183167a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 183267a391d7STrond Myklebust port = xs_get_random_port(); 183367a391d7STrond Myklebust return port; 183467a391d7STrond Myklebust } 183567a391d7STrond Myklebust 1836baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 183767a391d7STrond Myklebust { 1838fbfffbd5SChuck Lever if (transport->srcport != 0) 1839fbfffbd5SChuck Lever transport->srcport = 0; 184067a391d7STrond Myklebust if (!transport->xprt.resvport) 184167a391d7STrond Myklebust return 0; 184267a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 184367a391d7STrond Myklebust return xprt_max_resvport; 184467a391d7STrond Myklebust return --port; 184567a391d7STrond Myklebust } 1846beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1847a246b010SChuck Lever { 1848beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 184967a391d7STrond Myklebust int err, nloop = 0; 18505d4ec932SPavel Emelyanov unsigned short port = xs_get_srcport(transport); 185167a391d7STrond Myklebust unsigned short last; 1852a246b010SChuck Lever 18530f7a622cSChris Perl /* 18540f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 18550f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 18560f7a622cSChris Perl * port selection happen implicitly when the socket is used 18570f7a622cSChris Perl * (for example at connect time). 18580f7a622cSChris Perl * 18590f7a622cSChris Perl * This ensures that we can continue to establish TCP 18600f7a622cSChris Perl * connections even when all local ephemeral ports are already 18610f7a622cSChris Perl * a part of some TCP connection. This makes no difference 18620f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 18630f7a622cSChris Perl * 18640f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 18650f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 18660f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 18670f7a622cSChris Perl */ 18680f7a622cSChris Perl if (port == 0) 18690f7a622cSChris Perl return 0; 18700f7a622cSChris Perl 1871beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1872a246b010SChuck Lever do { 1873beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1874e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1875beb59b68SPavel Emelyanov transport->xprt.addrlen); 1876a246b010SChuck Lever if (err == 0) { 1877fbfffbd5SChuck Lever transport->srcport = port; 1878d3bc9a1dSFrank van Maarseveen break; 1879a246b010SChuck Lever } 188067a391d7STrond Myklebust last = port; 1881baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 188267a391d7STrond Myklebust if (port > last) 188367a391d7STrond Myklebust nloop++; 188467a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1885beb59b68SPavel Emelyanov 18864232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1887beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1888beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1889beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1890beb59b68SPavel Emelyanov else 1891beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1892beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18937dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1894a246b010SChuck Lever return err; 1895a246b010SChuck Lever } 1896a246b010SChuck Lever 1897176e21eeSChuck Lever /* 1898176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1899176e21eeSChuck Lever */ 1900176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1901176e21eeSChuck Lever { 1902fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1903176e21eeSChuck Lever } 1904176e21eeSChuck Lever 1905176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1906176e21eeSChuck Lever { 1907176e21eeSChuck Lever } 1908a246b010SChuck Lever 1909ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1910ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1911ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1912ed07536eSPeter Zijlstra 1913176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1914176e21eeSChuck Lever { 1915176e21eeSChuck Lever struct sock *sk = sock->sk; 1916176e21eeSChuck Lever 1917176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1918176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1919176e21eeSChuck Lever } 1920176e21eeSChuck Lever 19218945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1922ed07536eSPeter Zijlstra { 1923ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 19248945ee5eSChuck Lever 19258945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 19268945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1927ed07536eSPeter Zijlstra } 19288945ee5eSChuck Lever 19298945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 19308945ee5eSChuck Lever { 19318945ee5eSChuck Lever struct sock *sk = sock->sk; 19328945ee5eSChuck Lever 19338945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 19348945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1935ed07536eSPeter Zijlstra } 19366bc9638aSPavel Emelyanov 19376bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19386bc9638aSPavel Emelyanov { 1939fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 19401b7a1819SWeston Andros Adamson return; 19411b7a1819SWeston Andros Adamson 19424232e863SChuck Lever switch (family) { 1943176e21eeSChuck Lever case AF_LOCAL: 1944176e21eeSChuck Lever xs_reclassify_socketu(sock); 1945176e21eeSChuck Lever break; 19464232e863SChuck Lever case AF_INET: 19476bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 19484232e863SChuck Lever break; 19494232e863SChuck Lever case AF_INET6: 19506bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 19514232e863SChuck Lever break; 19524232e863SChuck Lever } 19536bc9638aSPavel Emelyanov } 1954ed07536eSPeter Zijlstra #else 19556bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19566bc9638aSPavel Emelyanov { 19576bc9638aSPavel Emelyanov } 1958ed07536eSPeter Zijlstra #endif 1959ed07536eSPeter Zijlstra 196093dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 196193dc41bdSNeilBrown { 196293dc41bdSNeilBrown } 196393dc41bdSNeilBrown 19646bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 19654dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 19664dda9c8aSTrond Myklebust int protocol, bool reuseport) 196722f79326SPavel Emelyanov { 196822f79326SPavel Emelyanov struct socket *sock; 196922f79326SPavel Emelyanov int err; 197022f79326SPavel Emelyanov 19716bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 197222f79326SPavel Emelyanov if (err < 0) { 197322f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 197422f79326SPavel Emelyanov protocol, -err); 197522f79326SPavel Emelyanov goto out; 197622f79326SPavel Emelyanov } 19776bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 197822f79326SPavel Emelyanov 19794dda9c8aSTrond Myklebust if (reuseport) 19804dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19814dda9c8aSTrond Myklebust 19824cea288aSBen Hutchings err = xs_bind(transport, sock); 19834cea288aSBen Hutchings if (err) { 198422f79326SPavel Emelyanov sock_release(sock); 198522f79326SPavel Emelyanov goto out; 198622f79326SPavel Emelyanov } 198722f79326SPavel Emelyanov 198822f79326SPavel Emelyanov return sock; 198922f79326SPavel Emelyanov out: 199022f79326SPavel Emelyanov return ERR_PTR(err); 199122f79326SPavel Emelyanov } 199222f79326SPavel Emelyanov 1993176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1994176e21eeSChuck Lever struct socket *sock) 1995176e21eeSChuck Lever { 1996176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1997176e21eeSChuck Lever xprt); 1998176e21eeSChuck Lever 1999176e21eeSChuck Lever if (!transport->inet) { 2000176e21eeSChuck Lever struct sock *sk = sock->sk; 2001176e21eeSChuck Lever 2002176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 2003176e21eeSChuck Lever 2004176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 2005176e21eeSChuck Lever 2006176e21eeSChuck Lever sk->sk_user_data = xprt; 2007a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 2008176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 2009b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 20102118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2011c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2012176e21eeSChuck Lever 2013176e21eeSChuck Lever xprt_clear_connected(xprt); 2014176e21eeSChuck Lever 2015176e21eeSChuck Lever /* Reset to new socket */ 2016176e21eeSChuck Lever transport->sock = sock; 2017176e21eeSChuck Lever transport->inet = sk; 2018176e21eeSChuck Lever 2019176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2020176e21eeSChuck Lever } 2021176e21eeSChuck Lever 20226c7a64e5STrond Myklebust transport->xmit.offset = 0; 20236c7a64e5STrond Myklebust 2024176e21eeSChuck Lever /* Tell the socket layer to start connecting... */ 2025176e21eeSChuck Lever xprt->stat.connect_count++; 2026176e21eeSChuck Lever xprt->stat.connect_start = jiffies; 2027176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 2028176e21eeSChuck Lever } 2029176e21eeSChuck Lever 2030176e21eeSChuck Lever /** 2031176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 2032176e21eeSChuck Lever * @transport: socket transport to connect 2033176e21eeSChuck Lever */ 2034dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 2035176e21eeSChuck Lever { 2036176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2037176e21eeSChuck Lever struct socket *sock; 2038176e21eeSChuck Lever int status = -EIO; 2039176e21eeSChuck Lever 2040176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 2041176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 2042176e21eeSChuck Lever if (status < 0) { 2043176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 2044176e21eeSChuck Lever "transport socket (%d).\n", -status); 2045176e21eeSChuck Lever goto out; 2046176e21eeSChuck Lever } 2047d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 2048176e21eeSChuck Lever 2049176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 2050176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2051176e21eeSChuck Lever 2052176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 205340b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2054176e21eeSChuck Lever switch (status) { 2055176e21eeSChuck Lever case 0: 2056176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 2057176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2058176e21eeSChuck Lever xprt_set_connected(xprt); 20593601c4a9STrond Myklebust case -ENOBUFS: 2060176e21eeSChuck Lever break; 2061176e21eeSChuck Lever case -ENOENT: 2062176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2063176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2064176e21eeSChuck Lever break; 20654a20a988STrond Myklebust case -ECONNREFUSED: 20664a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20674a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20684a20a988STrond Myklebust break; 2069176e21eeSChuck Lever default: 2070176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2071176e21eeSChuck Lever __func__, -status, 2072176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2073176e21eeSChuck Lever } 2074176e21eeSChuck Lever 2075176e21eeSChuck Lever out: 2076176e21eeSChuck Lever xprt_clear_connecting(xprt); 2077176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2078dc107402SJ. Bruce Fields return status; 2079dc107402SJ. Bruce Fields } 2080dc107402SJ. Bruce Fields 2081b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2082dc107402SJ. Bruce Fields { 2083dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2084dc107402SJ. Bruce Fields int ret; 2085dc107402SJ. Bruce Fields 2086dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2087dc107402SJ. Bruce Fields /* 2088dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2089dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2090dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2091dc107402SJ. Bruce Fields * 2092dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2093dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2094dc107402SJ. Bruce Fields * connect. 2095dc107402SJ. Bruce Fields */ 2096dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2097dc107402SJ. Bruce Fields return; 2098dc107402SJ. Bruce Fields } 2099dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2100dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2101dc107402SJ. Bruce Fields msleep_interruptible(15000); 2102176e21eeSChuck Lever } 2103176e21eeSChuck Lever 21043c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2105d6e971d8SJeff Layton /* 2106d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2107d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2108d6e971d8SJeff Layton * races with xs_reset_transport. 2109d6e971d8SJeff Layton */ 2110a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2111a564b8f0SMel Gorman { 2112a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2113a564b8f0SMel Gorman xprt); 2114a564b8f0SMel Gorman 2115d6e971d8SJeff Layton /* 2116d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2117d6e971d8SJeff Layton * reconnecting process will get it for us. 2118d6e971d8SJeff Layton */ 2119d6e971d8SJeff Layton if (!transport->inet) 2120d6e971d8SJeff Layton return; 21218e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2122a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2123a564b8f0SMel Gorman } 2124a564b8f0SMel Gorman 2125a564b8f0SMel Gorman /** 2126d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2127a564b8f0SMel Gorman * @xprt: transport to tag 2128a564b8f0SMel Gorman * 21298e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 21308e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2131a564b8f0SMel Gorman */ 2132d67fa4d8SJeff Layton static int 2133d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2134a564b8f0SMel Gorman { 2135d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2136a564b8f0SMel Gorman 2137d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2138d6e971d8SJeff Layton return 0; 2139d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2140d6e971d8SJeff Layton return -ERESTARTSYS; 2141d6e971d8SJeff Layton if (xs->inet) 2142d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2143d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 21448e228133SJeff Layton return 0; 2145a564b8f0SMel Gorman } 2146a564b8f0SMel Gorman 21478e228133SJeff Layton /** 2148d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 21498e228133SJeff Layton * @xprt: transport to tag 21508e228133SJeff Layton * 21518e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 21528e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 21538e228133SJeff Layton */ 2154d67fa4d8SJeff Layton static void 2155d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21568e228133SJeff Layton { 2157d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21588e228133SJeff Layton 2159d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2160d6e971d8SJeff Layton return; 2161d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2162d6e971d8SJeff Layton return; 2163d6e971d8SJeff Layton if (xs->inet) 2164d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2165d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2166a564b8f0SMel Gorman } 2167a564b8f0SMel Gorman #else 2168a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2169a564b8f0SMel Gorman { 2170a564b8f0SMel Gorman } 2171d67fa4d8SJeff Layton 2172d67fa4d8SJeff Layton static int 2173d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2174d67fa4d8SJeff Layton { 2175d67fa4d8SJeff Layton return -EINVAL; 2176d67fa4d8SJeff Layton } 2177d67fa4d8SJeff Layton 2178d67fa4d8SJeff Layton static void 2179d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2180d67fa4d8SJeff Layton { 2181d67fa4d8SJeff Layton } 2182a564b8f0SMel Gorman #endif 2183a564b8f0SMel Gorman 218416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2185a246b010SChuck Lever { 218616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2187edb267a6SChuck Lever 2188ee0ac0c2SChuck Lever if (!transport->inet) { 2189b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2190b0d93ad5SChuck Lever 2191b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2192b0d93ad5SChuck Lever 21932a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21942a9e1cfaSTrond Myklebust 2195b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2196f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2197b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2198b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2199c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2200b0d93ad5SChuck Lever 2201b0d93ad5SChuck Lever xprt_set_connected(xprt); 2202b0d93ad5SChuck Lever 2203b0d93ad5SChuck Lever /* Reset to new socket */ 2204ee0ac0c2SChuck Lever transport->sock = sock; 2205ee0ac0c2SChuck Lever transport->inet = sk; 2206b0d93ad5SChuck Lever 2207a564b8f0SMel Gorman xs_set_memalloc(xprt); 2208a564b8f0SMel Gorman 2209b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2210b0d93ad5SChuck Lever } 2211470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 221202910177STrond Myklebust 221302910177STrond Myklebust xprt->stat.connect_start = jiffies; 221416be2d20SChuck Lever } 221516be2d20SChuck Lever 22168c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2217a246b010SChuck Lever { 2218a246b010SChuck Lever struct sock_xprt *transport = 2219a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2220a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2221d099b8afSColin Ian King struct socket *sock; 2222b65c0310SPavel Emelyanov int status = -EIO; 2223a246b010SChuck Lever 22248c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 22254dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 22264dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2227b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2228a246b010SChuck Lever goto out; 222968e220bdSChuck Lever 2230c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2231c740eff8SChuck Lever "%s (port %s)\n", xprt, 2232c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2233c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2234c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 223568e220bdSChuck Lever 223668e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 223740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2238a246b010SChuck Lever status = 0; 2239b0d93ad5SChuck Lever out: 2240718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 2241b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 22427d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2243b0d93ad5SChuck Lever } 2244b0d93ad5SChuck Lever 22454876cc77STrond Myklebust /** 22464876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 22474876cc77STrond Myklebust * @xprt: transport 22484876cc77STrond Myklebust * 22494876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 22504876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 22514876cc77STrond Myklebust */ 22524876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 22534876cc77STrond Myklebust { 22544876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22554876cc77STrond Myklebust struct socket *sock = transport->sock; 22569b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22574876cc77STrond Myklebust 22584876cc77STrond Myklebust if (sock == NULL) 22594876cc77STrond Myklebust return; 22609b30889cSTrond Myklebust switch (skst) { 22619b30889cSTrond Myklebust default: 22624876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22634876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22649b30889cSTrond Myklebust break; 22659b30889cSTrond Myklebust case TCP_CLOSE: 22669b30889cSTrond Myklebust case TCP_TIME_WAIT: 22674876cc77STrond Myklebust xs_reset_transport(transport); 22684876cc77STrond Myklebust } 22699b30889cSTrond Myklebust } 22704876cc77STrond Myklebust 22718d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22728d1b8c62STrond Myklebust struct socket *sock) 2273b0d93ad5SChuck Lever { 227416be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22757196dbb0STrond Myklebust unsigned int keepidle; 22767196dbb0STrond Myklebust unsigned int keepcnt; 22777f260e85STrond Myklebust unsigned int opt_on = 1; 2278775f06abSTrond Myklebust unsigned int timeo; 22797f260e85STrond Myklebust 22807196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22817196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22827196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22837196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22847196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22857196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22867196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22877f260e85STrond Myklebust 22887f260e85STrond Myklebust /* TCP Keepalive options */ 22897f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22907f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22917f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22927f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22937f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22947f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22957f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22967f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2297b0d93ad5SChuck Lever 22988d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 22998d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 23008d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 23018d1b8c62STrond Myklebust } 23028d1b8c62STrond Myklebust 23037196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 23047196dbb0STrond Myklebust unsigned long connect_timeout, 23057196dbb0STrond Myklebust unsigned long reconnect_timeout) 23067196dbb0STrond Myklebust { 23077196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23087196dbb0STrond Myklebust struct rpc_timeout to; 23097196dbb0STrond Myklebust unsigned long initval; 23107196dbb0STrond Myklebust 23117196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 23127196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 23137196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 23147196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 23157196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 23167196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 23177196dbb0STrond Myklebust /* Arbitrary lower limit */ 23187196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 23197196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 23207196dbb0STrond Myklebust to.to_initval = initval; 23217196dbb0STrond Myklebust to.to_maxval = initval; 23227196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 23237196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 23247196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 23257196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 23267196dbb0STrond Myklebust } 23277196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 23287196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 23297196dbb0STrond Myklebust } 23307196dbb0STrond Myklebust 23318d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 23328d1b8c62STrond Myklebust { 23338d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23348d1b8c62STrond Myklebust int ret = -ENOTCONN; 23358d1b8c62STrond Myklebust 23368d1b8c62STrond Myklebust if (!transport->inet) { 23378d1b8c62STrond Myklebust struct sock *sk = sock->sk; 23388d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 23398d1b8c62STrond Myklebust 2340d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2341d88e4d82SNeilBrown * connections such as NFS mounts. 2342d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2343d88e4d82SNeilBrown * Individual applications, which have specific 2344d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2345d88e4d82SNeilBrown * MAY override this as appropriate. 2346d88e4d82SNeilBrown */ 2347d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2348d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2349d88e4d82SNeilBrown 23508d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2351775f06abSTrond Myklebust 2352b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2353b0d93ad5SChuck Lever 23542a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23552a9e1cfaSTrond Myklebust 2356b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23575157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2358b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2359b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2360b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 23612118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2362c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 23633167e12cSChuck Lever 23643167e12cSChuck Lever /* socket options */ 23653167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 23663167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2367b0d93ad5SChuck Lever 2368b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2369b0d93ad5SChuck Lever 2370b0d93ad5SChuck Lever /* Reset to new socket */ 2371ee0ac0c2SChuck Lever transport->sock = sock; 2372ee0ac0c2SChuck Lever transport->inet = sk; 2373b0d93ad5SChuck Lever 2374b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2375b0d93ad5SChuck Lever } 2376b0d93ad5SChuck Lever 237701d37c42STrond Myklebust if (!xprt_bound(xprt)) 2378fe19a96bSTrond Myklebust goto out; 237901d37c42STrond Myklebust 2380a564b8f0SMel Gorman xs_set_memalloc(xprt); 2381a564b8f0SMel Gorman 2382e1806c7bSTrond Myklebust /* Reset TCP record info */ 2383e1806c7bSTrond Myklebust transport->recv.offset = 0; 2384e1806c7bSTrond Myklebust transport->recv.len = 0; 2385e1806c7bSTrond Myklebust transport->recv.copied = 0; 2386e1806c7bSTrond Myklebust transport->recv.flags = TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 23876c7a64e5STrond Myklebust transport->xmit.offset = 0; 2388e1806c7bSTrond Myklebust 2389b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 2390262ca07dSChuck Lever xprt->stat.connect_count++; 2391262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 23920fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2393fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2394fe19a96bSTrond Myklebust switch (ret) { 2395fe19a96bSTrond Myklebust case 0: 23964dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2397e9d47639SGustavo A. R. Silva /* fall through */ 2398fe19a96bSTrond Myklebust case -EINPROGRESS: 2399fe19a96bSTrond Myklebust /* SYN_SENT! */ 2400fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2401fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 24021f4c17a0STrond Myklebust break; 24031f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 24041f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 24051f4c17a0STrond Myklebust transport->srcport = 0; 2406fe19a96bSTrond Myklebust } 2407fe19a96bSTrond Myklebust out: 2408fe19a96bSTrond Myklebust return ret; 240916be2d20SChuck Lever } 241016be2d20SChuck Lever 241116be2d20SChuck Lever /** 2412b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 241316be2d20SChuck Lever * 241416be2d20SChuck Lever * Invoked by a work queue tasklet. 241516be2d20SChuck Lever */ 2416cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 241716be2d20SChuck Lever { 2418cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2419cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 242016be2d20SChuck Lever struct socket *sock = transport->sock; 2421a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2422b61d59ffSTrond Myklebust int status = -EIO; 242316be2d20SChuck Lever 242416be2d20SChuck Lever if (!sock) { 2425cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 24264dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 24274dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2428b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2429b61d59ffSTrond Myklebust status = PTR_ERR(sock); 243016be2d20SChuck Lever goto out; 243116be2d20SChuck Lever } 24327d1e8255STrond Myklebust } 24337d1e8255STrond Myklebust 2434c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2435c740eff8SChuck Lever "%s (port %s)\n", xprt, 2436c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2437c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2438c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 243916be2d20SChuck Lever 244016be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 244140b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2442a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 244346121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 244446121cf7SChuck Lever sock->sk->sk_state); 2445a246b010SChuck Lever switch (status) { 2446f75e6745STrond Myklebust default: 2447f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2448f75e6745STrond Myklebust __func__, status); 2449e9d47639SGustavo A. R. Silva /* fall through */ 2450f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2451f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2452f75e6745STrond Myklebust * and retry 2453f75e6745STrond Myklebust */ 2454a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 245588b5ed73STrond Myklebust break; 24562a491991STrond Myklebust case 0: 2457a246b010SChuck Lever case -EINPROGRESS: 2458a246b010SChuck Lever case -EALREADY: 2459718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 24607d1e8255STrond Myklebust return; 24619fcfe0c8STrond Myklebust case -EINVAL: 24629fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24639fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24649fcfe0c8STrond Myklebust */ 24653ed5e2a2STrond Myklebust case -ECONNREFUSED: 24663ed5e2a2STrond Myklebust case -ECONNRESET: 2467eb5b46faSTrond Myklebust case -ENETDOWN: 24683ed5e2a2STrond Myklebust case -ENETUNREACH: 24694ba161a7STrond Myklebust case -EHOSTUNREACH: 24703913c78cSTrond Myklebust case -EADDRINUSE: 24713601c4a9STrond Myklebust case -ENOBUFS: 24726ea44adcSNeilBrown /* 24736ea44adcSNeilBrown * xs_tcp_force_close() wakes tasks with -EIO. 24746ea44adcSNeilBrown * We need to wake them first to ensure the 24756ea44adcSNeilBrown * correct error code. 24766ea44adcSNeilBrown */ 24776ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24784efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 24799fcfe0c8STrond Myklebust goto out; 24808a2cec29STrond Myklebust } 24812a491991STrond Myklebust status = -EAGAIN; 2482a246b010SChuck Lever out: 2483718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 24842226feb6SChuck Lever xprt_clear_connecting(xprt); 24857d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2486a246b010SChuck Lever } 2487a246b010SChuck Lever 248802910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 248902910177STrond Myklebust { 249002910177STrond Myklebust unsigned long start, now = jiffies; 249102910177STrond Myklebust 249202910177STrond Myklebust start = xprt->stat.connect_start + xprt->reestablish_timeout; 249302910177STrond Myklebust if (time_after(start, now)) 249402910177STrond Myklebust return start - now; 249502910177STrond Myklebust return 0; 249602910177STrond Myklebust } 249702910177STrond Myklebust 24983851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt) 24993851f1cdSTrond Myklebust { 25003851f1cdSTrond Myklebust xprt->reestablish_timeout <<= 1; 25013851f1cdSTrond Myklebust if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 25023851f1cdSTrond Myklebust xprt->reestablish_timeout = xprt->max_reconnect_timeout; 25033851f1cdSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 25043851f1cdSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 25053851f1cdSTrond Myklebust } 25063851f1cdSTrond Myklebust 250768e220bdSChuck Lever /** 25089903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 25091b092092STrond Myklebust * @xprt: pointer to transport structure 25109903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 25119903cd1cSChuck Lever * 25129903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 251303bf4b70SChuck Lever * 251403bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 251503bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 251603bf4b70SChuck Lever * socket on a privileged port. 251703bf4b70SChuck Lever * 251803bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 251903bf4b70SChuck Lever * retry floods (hard mounts). 25209903cd1cSChuck Lever */ 25211b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2522a246b010SChuck Lever { 2523ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 252402910177STrond Myklebust unsigned long delay = 0; 2525a246b010SChuck Lever 2526718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2527718ba5b8STrond Myklebust 252899b1a4c3STrond Myklebust if (transport->sock != NULL) { 252946121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 253046121cf7SChuck Lever "seconds\n", 253103bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 253299b1a4c3STrond Myklebust 253399b1a4c3STrond Myklebust /* Start by resetting any existing state */ 253499b1a4c3STrond Myklebust xs_reset_transport(transport); 253599b1a4c3STrond Myklebust 253602910177STrond Myklebust delay = xs_reconnect_delay(xprt); 25373851f1cdSTrond Myklebust xs_reconnect_backoff(xprt); 253802910177STrond Myklebust 253902910177STrond Myklebust } else 25409903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 254102910177STrond Myklebust 254240a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 254302910177STrond Myklebust &transport->connect_worker, 254402910177STrond Myklebust delay); 2545a246b010SChuck Lever } 2546a246b010SChuck Lever 2547262ca07dSChuck Lever /** 2548176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2549176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2550176e21eeSChuck Lever * @seq: output file 2551176e21eeSChuck Lever * 2552176e21eeSChuck Lever */ 2553176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2554176e21eeSChuck Lever { 2555176e21eeSChuck Lever long idle_time = 0; 2556176e21eeSChuck Lever 2557176e21eeSChuck Lever if (xprt_connected(xprt)) 2558176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2559176e21eeSChuck Lever 2560176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 256115a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2562176e21eeSChuck Lever xprt->stat.bind_count, 2563176e21eeSChuck Lever xprt->stat.connect_count, 2564176e21eeSChuck Lever xprt->stat.connect_time, 2565176e21eeSChuck Lever idle_time, 2566176e21eeSChuck Lever xprt->stat.sends, 2567176e21eeSChuck Lever xprt->stat.recvs, 2568176e21eeSChuck Lever xprt->stat.bad_xids, 2569176e21eeSChuck Lever xprt->stat.req_u, 257015a45206SAndy Adamson xprt->stat.bklog_u, 257115a45206SAndy Adamson xprt->stat.max_slots, 257215a45206SAndy Adamson xprt->stat.sending_u, 257315a45206SAndy Adamson xprt->stat.pending_u); 2574176e21eeSChuck Lever } 2575176e21eeSChuck Lever 2576176e21eeSChuck Lever /** 2577262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2578262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2579262ca07dSChuck Lever * @seq: output file 2580262ca07dSChuck Lever * 2581262ca07dSChuck Lever */ 2582262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2583262ca07dSChuck Lever { 2584c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2585c8475461SChuck Lever 258615a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 258715a45206SAndy Adamson "%lu %llu %llu\n", 2588fbfffbd5SChuck Lever transport->srcport, 2589262ca07dSChuck Lever xprt->stat.bind_count, 2590262ca07dSChuck Lever xprt->stat.sends, 2591262ca07dSChuck Lever xprt->stat.recvs, 2592262ca07dSChuck Lever xprt->stat.bad_xids, 2593262ca07dSChuck Lever xprt->stat.req_u, 259415a45206SAndy Adamson xprt->stat.bklog_u, 259515a45206SAndy Adamson xprt->stat.max_slots, 259615a45206SAndy Adamson xprt->stat.sending_u, 259715a45206SAndy Adamson xprt->stat.pending_u); 2598262ca07dSChuck Lever } 2599262ca07dSChuck Lever 2600262ca07dSChuck Lever /** 2601262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2602262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2603262ca07dSChuck Lever * @seq: output file 2604262ca07dSChuck Lever * 2605262ca07dSChuck Lever */ 2606262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2607262ca07dSChuck Lever { 2608c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2609262ca07dSChuck Lever long idle_time = 0; 2610262ca07dSChuck Lever 2611262ca07dSChuck Lever if (xprt_connected(xprt)) 2612262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2613262ca07dSChuck Lever 261415a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 261515a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2616fbfffbd5SChuck Lever transport->srcport, 2617262ca07dSChuck Lever xprt->stat.bind_count, 2618262ca07dSChuck Lever xprt->stat.connect_count, 2619262ca07dSChuck Lever xprt->stat.connect_time, 2620262ca07dSChuck Lever idle_time, 2621262ca07dSChuck Lever xprt->stat.sends, 2622262ca07dSChuck Lever xprt->stat.recvs, 2623262ca07dSChuck Lever xprt->stat.bad_xids, 2624262ca07dSChuck Lever xprt->stat.req_u, 262515a45206SAndy Adamson xprt->stat.bklog_u, 262615a45206SAndy Adamson xprt->stat.max_slots, 262715a45206SAndy Adamson xprt->stat.sending_u, 262815a45206SAndy Adamson xprt->stat.pending_u); 2629262ca07dSChuck Lever } 2630262ca07dSChuck Lever 26314cfc7e60SRahul Iyer /* 26324cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 26334cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 26344cfc7e60SRahul Iyer * to use the server side send routines. 26354cfc7e60SRahul Iyer */ 26365fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 26374cfc7e60SRahul Iyer { 26385fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 26395fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 26404cfc7e60SRahul Iyer struct page *page; 26414cfc7e60SRahul Iyer struct rpc_buffer *buf; 26424cfc7e60SRahul Iyer 26435fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 26445fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 26455fe6eaa1SChuck Lever size); 26465fe6eaa1SChuck Lever return -EINVAL; 26475fe6eaa1SChuck Lever } 26484cfc7e60SRahul Iyer 2649b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 26504cfc7e60SRahul Iyer if (!page) 26515fe6eaa1SChuck Lever return -ENOMEM; 26524cfc7e60SRahul Iyer 26534cfc7e60SRahul Iyer buf = page_address(page); 26544cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 26554cfc7e60SRahul Iyer 26565fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 265718e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 26585fe6eaa1SChuck Lever return 0; 26594cfc7e60SRahul Iyer } 26604cfc7e60SRahul Iyer 26614cfc7e60SRahul Iyer /* 26624cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 26634cfc7e60SRahul Iyer */ 26643435c74aSChuck Lever static void bc_free(struct rpc_task *task) 26654cfc7e60SRahul Iyer { 26663435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 26674cfc7e60SRahul Iyer struct rpc_buffer *buf; 26684cfc7e60SRahul Iyer 26694cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 26704cfc7e60SRahul Iyer free_page((unsigned long)buf); 26714cfc7e60SRahul Iyer } 26724cfc7e60SRahul Iyer 26734cfc7e60SRahul Iyer /* 26744cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 26754cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 26764cfc7e60SRahul Iyer */ 26774cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 26784cfc7e60SRahul Iyer { 26794cfc7e60SRahul Iyer int len; 26804cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 26814cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 26824cfc7e60SRahul Iyer struct sock_xprt *transport = 26834cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 26844cfc7e60SRahul Iyer struct socket *sock = transport->sock; 26854cfc7e60SRahul Iyer unsigned long headoff; 26864cfc7e60SRahul Iyer unsigned long tailoff; 26874cfc7e60SRahul Iyer 268861677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 26894cfc7e60SRahul Iyer 26904cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26914cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 26924cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 26934cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 26944cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 26954cfc7e60SRahul Iyer 26964cfc7e60SRahul Iyer if (len != xbufp->len) { 26974cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 26984cfc7e60SRahul Iyer len = -EAGAIN; 26994cfc7e60SRahul Iyer } 27004cfc7e60SRahul Iyer 27014cfc7e60SRahul Iyer return len; 27024cfc7e60SRahul Iyer } 27034cfc7e60SRahul Iyer 27044cfc7e60SRahul Iyer /* 27054cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 27064cfc7e60SRahul Iyer */ 270750f484e2STrond Myklebust static int bc_send_request(struct rpc_rqst *req, struct rpc_task *task) 27084cfc7e60SRahul Iyer { 27094cfc7e60SRahul Iyer struct svc_xprt *xprt; 27107fc56136SAndrzej Hajda int len; 27114cfc7e60SRahul Iyer 27124cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 27134cfc7e60SRahul Iyer /* 27144cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 27154cfc7e60SRahul Iyer */ 27164cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 27174cfc7e60SRahul Iyer 27184cfc7e60SRahul Iyer /* 27194cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 27204cfc7e60SRahul Iyer * with the fore channel 27214cfc7e60SRahul Iyer */ 2722*c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 27234cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 27244cfc7e60SRahul Iyer len = -ENOTCONN; 27254cfc7e60SRahul Iyer else 27264cfc7e60SRahul Iyer len = bc_sendto(req); 27274cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 27284cfc7e60SRahul Iyer 27294cfc7e60SRahul Iyer if (len > 0) 27304cfc7e60SRahul Iyer len = 0; 27314cfc7e60SRahul Iyer 27324cfc7e60SRahul Iyer return len; 27334cfc7e60SRahul Iyer } 27344cfc7e60SRahul Iyer 27354cfc7e60SRahul Iyer /* 27364cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 27374cfc7e60SRahul Iyer */ 27384cfc7e60SRahul Iyer 27394cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 27404cfc7e60SRahul Iyer { 27414cfc7e60SRahul Iyer } 27424cfc7e60SRahul Iyer 27434cfc7e60SRahul Iyer /* 27444cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 27454cfc7e60SRahul Iyer * initiated, we do nothing 27464cfc7e60SRahul Iyer */ 27474cfc7e60SRahul Iyer 27484cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 27494cfc7e60SRahul Iyer { 275047f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 275147f72efaSKinglong Mee 275247f72efaSKinglong Mee xs_xprt_free(xprt); 275347f72efaSKinglong Mee module_put(THIS_MODULE); 27544cfc7e60SRahul Iyer } 27554cfc7e60SRahul Iyer 2756d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2757176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 27584cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2759f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2760a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2761176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2762176e21eeSChuck Lever .set_port = xs_local_set_port, 2763dc107402SJ. Bruce Fields .connect = xs_local_connect, 2764176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2765176e21eeSChuck Lever .buf_free = rpc_free, 2766176e21eeSChuck Lever .send_request = xs_local_send_request, 2767176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2768176e21eeSChuck Lever .close = xs_close, 2769a1311d87STrond Myklebust .destroy = xs_destroy, 2770176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2771d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2772d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2773176e21eeSChuck Lever }; 2774176e21eeSChuck Lever 2775d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 277643118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 277712a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 277849e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2779f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2780a9cde23aSChuck Lever .free_slot = xprt_free_slot, 278145160d62SChuck Lever .rpcbind = rpcb_getport_async, 278292200412SChuck Lever .set_port = xs_set_port, 27839903cd1cSChuck Lever .connect = xs_connect, 278402107148SChuck Lever .buf_alloc = rpc_malloc, 278502107148SChuck Lever .buf_free = rpc_free, 2786262965f5SChuck Lever .send_request = xs_udp_send_request, 2787fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 278846c0ee8bSChuck Lever .timer = xs_udp_timer, 2789a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2790262965f5SChuck Lever .close = xs_close, 2791262965f5SChuck Lever .destroy = xs_destroy, 2792262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2793d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2794d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27954a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2796262965f5SChuck Lever }; 2797262965f5SChuck Lever 2798d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 279912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 28004cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 280136bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 2802a9cde23aSChuck Lever .free_slot = xprt_free_slot, 280345160d62SChuck Lever .rpcbind = rpcb_getport_async, 280492200412SChuck Lever .set_port = xs_set_port, 28050b9e7943STrond Myklebust .connect = xs_connect, 280602107148SChuck Lever .buf_alloc = rpc_malloc, 280702107148SChuck Lever .buf_free = rpc_free, 2808262965f5SChuck Lever .send_request = xs_tcp_send_request, 2809fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2810c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 28119903cd1cSChuck Lever .destroy = xs_destroy, 28127196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2813262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2814d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2815d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28164a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 281742e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 281842e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 281976566773SChuck Lever .bc_up = xs_tcp_bc_up, 28206b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 282142e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 282242e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 282342e5c3e2SChuck Lever #endif 2824a246b010SChuck Lever }; 2825a246b010SChuck Lever 28264cfc7e60SRahul Iyer /* 28274cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 28284cfc7e60SRahul Iyer */ 28294cfc7e60SRahul Iyer 2830d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 28314cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 28324cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 283384e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2834a9cde23aSChuck Lever .free_slot = xprt_free_slot, 28354cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 28364cfc7e60SRahul Iyer .buf_free = bc_free, 28374cfc7e60SRahul Iyer .send_request = bc_send_request, 28384cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 28394cfc7e60SRahul Iyer .close = bc_close, 28404cfc7e60SRahul Iyer .destroy = bc_destroy, 28414cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2842d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2843d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28444a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 28454cfc7e60SRahul Iyer }; 28464cfc7e60SRahul Iyer 284792476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 284892476850SChuck Lever { 284992476850SChuck Lever static const struct sockaddr_in sin = { 285092476850SChuck Lever .sin_family = AF_INET, 285192476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 285292476850SChuck Lever }; 285392476850SChuck Lever static const struct sockaddr_in6 sin6 = { 285492476850SChuck Lever .sin6_family = AF_INET6, 285592476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 285692476850SChuck Lever }; 285792476850SChuck Lever 285892476850SChuck Lever switch (family) { 2859176e21eeSChuck Lever case AF_LOCAL: 2860176e21eeSChuck Lever break; 286192476850SChuck Lever case AF_INET: 286292476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 286392476850SChuck Lever break; 286492476850SChuck Lever case AF_INET6: 286592476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 286692476850SChuck Lever break; 286792476850SChuck Lever default: 286892476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 286992476850SChuck Lever return -EAFNOSUPPORT; 287092476850SChuck Lever } 287192476850SChuck Lever return 0; 287292476850SChuck Lever } 287392476850SChuck Lever 28743c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2875d9ba131dSTrond Myklebust unsigned int slot_table_size, 2876d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2877c8541ecdSChuck Lever { 2878c8541ecdSChuck Lever struct rpc_xprt *xprt; 2879ffc2e518SChuck Lever struct sock_xprt *new; 2880c8541ecdSChuck Lever 288196802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2882c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2883c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2884c8541ecdSChuck Lever } 2885c8541ecdSChuck Lever 2886d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2887d9ba131dSTrond Myklebust max_slot_table_size); 2888bd1722d4SPavel Emelyanov if (xprt == NULL) { 288946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 289046121cf7SChuck Lever "rpc_xprt\n"); 2891c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2892c8541ecdSChuck Lever } 2893c8541ecdSChuck Lever 2894bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2895edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 289696802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 289796802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2898d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2899fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 290092476850SChuck Lever else { 290192476850SChuck Lever int err; 290292476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 290392476850SChuck Lever (struct sockaddr *)&new->srcaddr); 29042aa13531SStanislav Kinsbursky if (err != 0) { 29052aa13531SStanislav Kinsbursky xprt_free(xprt); 290692476850SChuck Lever return ERR_PTR(err); 290792476850SChuck Lever } 29082aa13531SStanislav Kinsbursky } 2909c8541ecdSChuck Lever 2910c8541ecdSChuck Lever return xprt; 2911c8541ecdSChuck Lever } 2912c8541ecdSChuck Lever 2913176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2914176e21eeSChuck Lever .to_initval = 10 * HZ, 2915176e21eeSChuck Lever .to_maxval = 10 * HZ, 2916176e21eeSChuck Lever .to_retries = 2, 2917176e21eeSChuck Lever }; 2918176e21eeSChuck Lever 2919176e21eeSChuck Lever /** 2920176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2921176e21eeSChuck Lever * @args: rpc transport creation arguments 2922176e21eeSChuck Lever * 2923176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2924176e21eeSChuck Lever */ 2925176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2926176e21eeSChuck Lever { 2927176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2928176e21eeSChuck Lever struct sock_xprt *transport; 2929176e21eeSChuck Lever struct rpc_xprt *xprt; 2930176e21eeSChuck Lever struct rpc_xprt *ret; 2931176e21eeSChuck Lever 2932d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2933d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2934176e21eeSChuck Lever if (IS_ERR(xprt)) 2935176e21eeSChuck Lever return xprt; 2936176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2937176e21eeSChuck Lever 2938176e21eeSChuck Lever xprt->prot = 0; 2939176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2940176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2941176e21eeSChuck Lever 2942176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2943176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2944176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2945176e21eeSChuck Lever 2946176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2947176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2948176e21eeSChuck Lever 2949a2648094STrond Myklebust INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn); 295093dc41bdSNeilBrown INIT_DELAYED_WORK(&transport->connect_worker, 295193dc41bdSNeilBrown xs_dummy_setup_socket); 295293dc41bdSNeilBrown 2953176e21eeSChuck Lever switch (sun->sun_family) { 2954176e21eeSChuck Lever case AF_LOCAL: 2955176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2956176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2957176e21eeSChuck Lever sun->sun_path); 2958176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2959176e21eeSChuck Lever goto out_err; 2960176e21eeSChuck Lever } 2961176e21eeSChuck Lever xprt_set_bound(xprt); 2962176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 29637073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 29647073ea87SJ. Bruce Fields if (ret) 29657073ea87SJ. Bruce Fields goto out_err; 2966176e21eeSChuck Lever break; 2967176e21eeSChuck Lever default: 2968176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2969176e21eeSChuck Lever goto out_err; 2970176e21eeSChuck Lever } 2971176e21eeSChuck Lever 2972176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2973176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2974176e21eeSChuck Lever 2975176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2976176e21eeSChuck Lever return xprt; 2977176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2978176e21eeSChuck Lever out_err: 2979315f3812SKinglong Mee xs_xprt_free(xprt); 2980176e21eeSChuck Lever return ret; 2981176e21eeSChuck Lever } 2982176e21eeSChuck Lever 29832881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 29842881ae74STrond Myklebust .to_initval = 5 * HZ, 29852881ae74STrond Myklebust .to_maxval = 30 * HZ, 29862881ae74STrond Myklebust .to_increment = 5 * HZ, 29872881ae74STrond Myklebust .to_retries = 5, 29882881ae74STrond Myklebust }; 29892881ae74STrond Myklebust 29909903cd1cSChuck Lever /** 29919903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 299296802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29939903cd1cSChuck Lever * 29949903cd1cSChuck Lever */ 2995483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2996a246b010SChuck Lever { 29978f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2998c8541ecdSChuck Lever struct rpc_xprt *xprt; 2999c8475461SChuck Lever struct sock_xprt *transport; 30000a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3001a246b010SChuck Lever 3002d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 3003d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 3004c8541ecdSChuck Lever if (IS_ERR(xprt)) 3005c8541ecdSChuck Lever return xprt; 3006c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3007a246b010SChuck Lever 3008ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 3009808012fbSChuck Lever xprt->tsh_size = 0; 3010a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 3011a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3012a246b010SChuck Lever 301303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 301403bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 301503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3016a246b010SChuck Lever 3017262965f5SChuck Lever xprt->ops = &xs_udp_ops; 3018a246b010SChuck Lever 3019ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 3020a246b010SChuck Lever 3021f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 3022edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3023edc1b01cSTrond Myklebust 30248f9d5b1aSChuck Lever switch (addr->sa_family) { 30258f9d5b1aSChuck Lever case AF_INET: 30268f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30278f9d5b1aSChuck Lever xprt_set_bound(xprt); 30288f9d5b1aSChuck Lever 30299dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 30308f9d5b1aSChuck Lever break; 30318f9d5b1aSChuck Lever case AF_INET6: 30328f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30338f9d5b1aSChuck Lever xprt_set_bound(xprt); 30348f9d5b1aSChuck Lever 30359dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 30368f9d5b1aSChuck Lever break; 30378f9d5b1aSChuck Lever default: 30380a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30390a68b0beSJ. Bruce Fields goto out_err; 30408f9d5b1aSChuck Lever } 30418f9d5b1aSChuck Lever 3042c740eff8SChuck Lever if (xprt_bound(xprt)) 3043c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3044c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3045c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3046c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3047c740eff8SChuck Lever else 3048c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3049c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3050c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3051edb267a6SChuck Lever 3052bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3053c8541ecdSChuck Lever return xprt; 30540a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30550a68b0beSJ. Bruce Fields out_err: 3056315f3812SKinglong Mee xs_xprt_free(xprt); 30570a68b0beSJ. Bruce Fields return ret; 3058a246b010SChuck Lever } 3059a246b010SChuck Lever 30602881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 30612881ae74STrond Myklebust .to_initval = 60 * HZ, 30622881ae74STrond Myklebust .to_maxval = 60 * HZ, 30632881ae74STrond Myklebust .to_retries = 2, 30642881ae74STrond Myklebust }; 30652881ae74STrond Myklebust 30669903cd1cSChuck Lever /** 30679903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 306896802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30699903cd1cSChuck Lever * 30709903cd1cSChuck Lever */ 3071483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3072a246b010SChuck Lever { 30738f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3074c8541ecdSChuck Lever struct rpc_xprt *xprt; 3075c8475461SChuck Lever struct sock_xprt *transport; 30760a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3077b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3078b7993cebSTrond Myklebust 3079b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3080b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3081a246b010SChuck Lever 3082d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3083b7993cebSTrond Myklebust max_slot_table_size); 3084c8541ecdSChuck Lever if (IS_ERR(xprt)) 3085c8541ecdSChuck Lever return xprt; 3086c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3087a246b010SChuck Lever 3088ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3089808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3090808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3091a246b010SChuck Lever 309203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 309303bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 309403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3095a246b010SChuck Lever 3096262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3097ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3098a246b010SChuck Lever 30993851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 31007196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 31017196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 31023851f1cdSTrond Myklebust 3103edc1b01cSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn); 3104edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3105edc1b01cSTrond Myklebust 31068f9d5b1aSChuck Lever switch (addr->sa_family) { 31078f9d5b1aSChuck Lever case AF_INET: 31088f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 31098f9d5b1aSChuck Lever xprt_set_bound(xprt); 31108f9d5b1aSChuck Lever 31119dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 31128f9d5b1aSChuck Lever break; 31138f9d5b1aSChuck Lever case AF_INET6: 31148f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 31158f9d5b1aSChuck Lever xprt_set_bound(xprt); 31168f9d5b1aSChuck Lever 31179dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 31188f9d5b1aSChuck Lever break; 31198f9d5b1aSChuck Lever default: 31200a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31210a68b0beSJ. Bruce Fields goto out_err; 31228f9d5b1aSChuck Lever } 31238f9d5b1aSChuck Lever 3124c740eff8SChuck Lever if (xprt_bound(xprt)) 3125c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3126c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3127c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3128c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3129c740eff8SChuck Lever else 3130c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3131c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3132c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3133c740eff8SChuck Lever 3134bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3135c8541ecdSChuck Lever return xprt; 31360a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31370a68b0beSJ. Bruce Fields out_err: 3138315f3812SKinglong Mee xs_xprt_free(xprt); 31390a68b0beSJ. Bruce Fields return ret; 3140a246b010SChuck Lever } 3141282b32e1SChuck Lever 3142f300babaSAlexandros Batsakis /** 3143f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3144f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3145f300babaSAlexandros Batsakis * 3146f300babaSAlexandros Batsakis */ 3147f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3148f300babaSAlexandros Batsakis { 3149f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3150f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3151f300babaSAlexandros Batsakis struct sock_xprt *transport; 3152f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 31530a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3154f300babaSAlexandros Batsakis 3155d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3156d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3157f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3158f300babaSAlexandros Batsakis return xprt; 3159f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3160f300babaSAlexandros Batsakis 3161f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3162f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3163f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3164f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3165f300babaSAlexandros Batsakis 3166f300babaSAlexandros Batsakis /* backchannel */ 3167f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3168f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3169f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3170f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3171f300babaSAlexandros Batsakis 3172f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3173f300babaSAlexandros Batsakis 3174f300babaSAlexandros Batsakis switch (addr->sa_family) { 3175f300babaSAlexandros Batsakis case AF_INET: 3176f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3177f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3178f300babaSAlexandros Batsakis break; 3179f300babaSAlexandros Batsakis case AF_INET6: 3180f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3181f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3182f300babaSAlexandros Batsakis break; 3183f300babaSAlexandros Batsakis default: 31840a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31850a68b0beSJ. Bruce Fields goto out_err; 3186f300babaSAlexandros Batsakis } 3187f300babaSAlexandros Batsakis 3188f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3189f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3190f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3191f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3192f300babaSAlexandros Batsakis 3193f300babaSAlexandros Batsakis /* 319499de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 319528303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 319628303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 319728303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 319899de8ea9SJ. Bruce Fields */ 319999de8ea9SJ. Bruce Fields xprt_get(xprt); 320099de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 320199de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 320299de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 320399de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 320499de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 320599de8ea9SJ. Bruce Fields 320699de8ea9SJ. Bruce Fields /* 3207f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3208f300babaSAlexandros Batsakis * the xprt status to connected 3209f300babaSAlexandros Batsakis */ 3210f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3211f300babaSAlexandros Batsakis 3212f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3213f300babaSAlexandros Batsakis return xprt; 3214642aab58SKinglong Mee 3215642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 321639a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 321799de8ea9SJ. Bruce Fields xprt_put(xprt); 32180a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 32190a68b0beSJ. Bruce Fields out_err: 3220315f3812SKinglong Mee xs_xprt_free(xprt); 32210a68b0beSJ. Bruce Fields return ret; 3222f300babaSAlexandros Batsakis } 3223f300babaSAlexandros Batsakis 3224176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3225176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3226176e21eeSChuck Lever .name = "named UNIX socket", 3227176e21eeSChuck Lever .owner = THIS_MODULE, 3228176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3229176e21eeSChuck Lever .setup = xs_setup_local, 3230176e21eeSChuck Lever }; 3231176e21eeSChuck Lever 3232bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3233bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3234bc25571eS\"Talpey, Thomas\ .name = "udp", 3235bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3236f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3237bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3238bc25571eS\"Talpey, Thomas\ }; 3239bc25571eS\"Talpey, Thomas\ 3240bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3241bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3242bc25571eS\"Talpey, Thomas\ .name = "tcp", 3243bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3244f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3245bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3246bc25571eS\"Talpey, Thomas\ }; 3247bc25571eS\"Talpey, Thomas\ 3248f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3249f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3250f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3251f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3252f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3253f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3254f300babaSAlexandros Batsakis }; 3255f300babaSAlexandros Batsakis 3256282b32e1SChuck Lever /** 3257bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3258282b32e1SChuck Lever * 3259282b32e1SChuck Lever */ 3260282b32e1SChuck Lever int init_socket_xprt(void) 3261282b32e1SChuck Lever { 3262f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 32632b1bec5fSEric W. Biederman if (!sunrpc_table_header) 32640b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3265fbf76683SChuck Lever #endif 3266fbf76683SChuck Lever 3267176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3268bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3269bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3270f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3271bc25571eS\"Talpey, Thomas\ 3272282b32e1SChuck Lever return 0; 3273282b32e1SChuck Lever } 3274282b32e1SChuck Lever 3275282b32e1SChuck Lever /** 3276bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3277282b32e1SChuck Lever * 3278282b32e1SChuck Lever */ 3279282b32e1SChuck Lever void cleanup_socket_xprt(void) 3280282b32e1SChuck Lever { 3281f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3282fbf76683SChuck Lever if (sunrpc_table_header) { 3283fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3284fbf76683SChuck Lever sunrpc_table_header = NULL; 3285fbf76683SChuck Lever } 3286fbf76683SChuck Lever #endif 3287bc25571eS\"Talpey, Thomas\ 3288176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3289bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3290bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3291f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3292282b32e1SChuck Lever } 3293cbf11071STrond Myklebust 32949bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 32959bbb9e5aSRusty Russell const struct kernel_param *kp, 3296cbf11071STrond Myklebust unsigned int min, unsigned int max) 3297cbf11071STrond Myklebust { 329800cfaa94SDaniel Walter unsigned int num; 3299cbf11071STrond Myklebust int ret; 3300cbf11071STrond Myklebust 3301cbf11071STrond Myklebust if (!val) 3302cbf11071STrond Myklebust return -EINVAL; 330300cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 33049761a246SDan Carpenter if (ret) 33059761a246SDan Carpenter return ret; 33069761a246SDan Carpenter if (num < min || num > max) 3307cbf11071STrond Myklebust return -EINVAL; 3308cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3309cbf11071STrond Myklebust return 0; 3310cbf11071STrond Myklebust } 3311cbf11071STrond Myklebust 33129bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3313cbf11071STrond Myklebust { 3314ffb6ca33SFrank Sorenson if (kp->arg == &xprt_min_resvport) 3315cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3316cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3317ffb6ca33SFrank Sorenson xprt_max_resvport); 3318ffb6ca33SFrank Sorenson return param_set_uint_minmax(val, kp, 3319ffb6ca33SFrank Sorenson xprt_min_resvport, 3320cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3321cbf11071STrond Myklebust } 3322cbf11071STrond Myklebust 33239c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 33249bbb9e5aSRusty Russell .set = param_set_portnr, 33259bbb9e5aSRusty Russell .get = param_get_uint, 33269bbb9e5aSRusty Russell }; 33279bbb9e5aSRusty Russell 3328cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3329cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3330cbf11071STrond Myklebust 3331cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3332cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3333cbf11071STrond Myklebust 33349bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 33359bbb9e5aSRusty Russell const struct kernel_param *kp) 3336cbf11071STrond Myklebust { 3337cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3338cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3339cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3340cbf11071STrond Myklebust } 3341cbf11071STrond Myklebust 33429c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 33439bbb9e5aSRusty Russell .set = param_set_slot_table_size, 33449bbb9e5aSRusty Russell .get = param_get_uint, 33459bbb9e5aSRusty Russell }; 33469bbb9e5aSRusty Russell 3347cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3348cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3349cbf11071STrond Myklebust 3350d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3351d9ba131dSTrond Myklebust const struct kernel_param *kp) 3352d9ba131dSTrond Myklebust { 3353d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3354d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3355d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3356d9ba131dSTrond Myklebust } 3357d9ba131dSTrond Myklebust 33589c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3359d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3360d9ba131dSTrond Myklebust .get = param_get_uint, 3361d9ba131dSTrond Myklebust }; 3362d9ba131dSTrond Myklebust 3363d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3364d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3365d9ba131dSTrond Myklebust 3366cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3367cbf11071STrond Myklebust slot_table_size, 0644); 3368d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3369d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3370cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3371cbf11071STrond Myklebust slot_table_size, 0644); 3372