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> 50277e4ab7STrond Myklebust #include <linux/bvec.h> 516a829eb8STrond Myklebust #include <linux/highmem.h> 52277e4ab7STrond Myklebust #include <linux/uio.h> 53a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 54a246b010SChuck Lever 5540b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 5640b5ea0cSTrond Myklebust 574cfc7e60SRahul Iyer #include "sunrpc.h" 58176e21eeSChuck Lever 59176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 607196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 617196dbb0STrond Myklebust struct socket *sock); 62176e21eeSChuck Lever 639903cd1cSChuck Lever /* 64c556b754SChuck Lever * xprtsock tunables 65c556b754SChuck Lever */ 6609acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 6709acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 6809acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 69c556b754SChuck Lever 7009acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 7109acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 72c556b754SChuck Lever 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, 132826799e6SJ. Bruce Fields .extra2 = &xprt_max_resvport_limit 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, 140826799e6SJ. Bruce Fields .extra1 = &xprt_min_resvport_limit, 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 /* 16303bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16403bf4b70SChuck Lever */ 16503bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16603bf4b70SChuck Lever 16703bf4b70SChuck Lever /* 16803bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16903bf4b70SChuck Lever * kind of resource problem on the local host. 17003bf4b70SChuck Lever */ 17103bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17203bf4b70SChuck Lever 17303bf4b70SChuck Lever /* 17403bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17503bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17603bf4b70SChuck Lever * 17703bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17803bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17903bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18003bf4b70SChuck Lever * increase over time if the server is down or not responding. 18103bf4b70SChuck Lever */ 18203bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18303bf4b70SChuck Lever 18403bf4b70SChuck Lever /* 18503bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18603bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18703bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18803bf4b70SChuck Lever */ 18903bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19003bf4b70SChuck Lever 191f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 192a246b010SChuck Lever # undef RPC_DEBUG_DATA 1939903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 194a246b010SChuck Lever #endif 195a246b010SChuck Lever 196a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1979903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 198a246b010SChuck Lever { 199a246b010SChuck Lever u8 *buf = (u8 *) packet; 200a246b010SChuck Lever int j; 201a246b010SChuck Lever 202a246b010SChuck Lever dprintk("RPC: %s\n", msg); 203a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 204a246b010SChuck Lever if (!(j & 31)) { 205a246b010SChuck Lever if (j) 206a246b010SChuck Lever dprintk("\n"); 207a246b010SChuck Lever dprintk("0x%04x ", j); 208a246b010SChuck Lever } 209a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 210a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 211a246b010SChuck Lever } 212a246b010SChuck Lever dprintk("\n"); 213a246b010SChuck Lever } 214a246b010SChuck Lever #else 2159903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 216a246b010SChuck Lever { 217a246b010SChuck Lever /* NOP */ 218a246b010SChuck Lever } 219a246b010SChuck Lever #endif 220a246b010SChuck Lever 2212118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2222118071dSTrond Myklebust { 2232118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2242118071dSTrond Myklebust } 2252118071dSTrond Myklebust 22695392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 227edb267a6SChuck Lever { 22895392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 22995392c59SChuck Lever } 23095392c59SChuck Lever 231176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 232176e21eeSChuck Lever { 233176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 234176e21eeSChuck Lever } 235176e21eeSChuck Lever 23695392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 23795392c59SChuck Lever { 23895392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 23995392c59SChuck Lever } 24095392c59SChuck Lever 24195392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 24295392c59SChuck Lever { 24395392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 24495392c59SChuck Lever } 24595392c59SChuck Lever 246c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 247c877b849SChuck Lever { 248c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2499dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2509dc3b095SChuck Lever struct sockaddr_in *sin; 251176e21eeSChuck Lever struct sockaddr_un *sun; 252c877b849SChuck Lever char buf[128]; 253c877b849SChuck Lever 2549dc3b095SChuck Lever switch (sap->sa_family) { 255176e21eeSChuck Lever case AF_LOCAL: 256176e21eeSChuck Lever sun = xs_addr_un(xprt); 257176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 258176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 259176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 260176e21eeSChuck Lever break; 2619dc3b095SChuck Lever case AF_INET: 262176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 263176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 264176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2659dc3b095SChuck Lever sin = xs_addr_in(xprt); 266fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2679dc3b095SChuck Lever break; 2689dc3b095SChuck Lever case AF_INET6: 269176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 270176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 271176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2729dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 273fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2749dc3b095SChuck Lever break; 2759dc3b095SChuck Lever default: 2769dc3b095SChuck Lever BUG(); 2779dc3b095SChuck Lever } 278176e21eeSChuck Lever 2799dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2809dc3b095SChuck Lever } 2819dc3b095SChuck Lever 2829dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2839dc3b095SChuck Lever { 2849dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2859dc3b095SChuck Lever char buf[128]; 2869dc3b095SChuck Lever 28781160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 288c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 289c877b849SChuck Lever 29081160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 291c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 292c877b849SChuck Lever } 293c877b849SChuck Lever 2949dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 295b454ae90SChuck Lever const char *protocol, 296b454ae90SChuck Lever const char *netid) 297edb267a6SChuck Lever { 298b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 299b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 300c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3019dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 302edb267a6SChuck Lever } 303edb267a6SChuck Lever 3049dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3054b6473fbSChuck Lever { 3069dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3079dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3084b6473fbSChuck Lever 3099dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 310edb267a6SChuck Lever } 311edb267a6SChuck Lever 312edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 313edb267a6SChuck Lever { 31433e01dc7SChuck Lever unsigned int i; 31533e01dc7SChuck Lever 31633e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 31733e01dc7SChuck Lever switch (i) { 31833e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 31933e01dc7SChuck Lever case RPC_DISPLAY_NETID: 32033e01dc7SChuck Lever continue; 32133e01dc7SChuck Lever default: 32233e01dc7SChuck Lever kfree(xprt->address_strings[i]); 32333e01dc7SChuck Lever } 324edb267a6SChuck Lever } 325edb267a6SChuck Lever 326277e4ab7STrond Myklebust static size_t 327277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 328277e4ab7STrond Myklebust { 329277e4ab7STrond Myklebust size_t i,n; 330277e4ab7STrond Myklebust 33116e5e90fSTrond Myklebust if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 332277e4ab7STrond Myklebust return want; 333277e4ab7STrond Myklebust n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 334277e4ab7STrond Myklebust for (i = 0; i < n; i++) { 335277e4ab7STrond Myklebust if (buf->pages[i]) 336277e4ab7STrond Myklebust continue; 337277e4ab7STrond Myklebust buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 338277e4ab7STrond Myklebust if (!buf->pages[i]) { 33916e5e90fSTrond Myklebust i *= PAGE_SIZE; 34016e5e90fSTrond Myklebust return i > buf->page_base ? i - buf->page_base : 0; 341277e4ab7STrond Myklebust } 342277e4ab7STrond Myklebust } 343277e4ab7STrond Myklebust return want; 344277e4ab7STrond Myklebust } 345277e4ab7STrond Myklebust 346277e4ab7STrond Myklebust static ssize_t 347277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 348277e4ab7STrond Myklebust { 349277e4ab7STrond Myklebust ssize_t ret; 350277e4ab7STrond Myklebust if (seek != 0) 351277e4ab7STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 352277e4ab7STrond Myklebust ret = sock_recvmsg(sock, msg, flags); 353277e4ab7STrond Myklebust return ret > 0 ? ret + seek : ret; 354277e4ab7STrond Myklebust } 355277e4ab7STrond Myklebust 356277e4ab7STrond Myklebust static ssize_t 357277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 358277e4ab7STrond Myklebust struct kvec *kvec, size_t count, size_t seek) 359277e4ab7STrond Myklebust { 3600e9b4a82SAl Viro iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); 361277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 362277e4ab7STrond Myklebust } 363277e4ab7STrond Myklebust 364277e4ab7STrond Myklebust static ssize_t 365277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 366277e4ab7STrond Myklebust struct bio_vec *bvec, unsigned long nr, size_t count, 367277e4ab7STrond Myklebust size_t seek) 368277e4ab7STrond Myklebust { 3690e9b4a82SAl Viro iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); 370277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 371277e4ab7STrond Myklebust } 372277e4ab7STrond Myklebust 373277e4ab7STrond Myklebust static ssize_t 374277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 375277e4ab7STrond Myklebust size_t count) 376277e4ab7STrond Myklebust { 377b76a5afdSTrond Myklebust iov_iter_discard(&msg->msg_iter, READ, count); 378b76a5afdSTrond Myklebust return sock_recvmsg(sock, msg, flags); 379277e4ab7STrond Myklebust } 380277e4ab7STrond Myklebust 3816a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 3826a829eb8STrond Myklebust static void 3836a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 3846a829eb8STrond Myklebust { 3856a829eb8STrond Myklebust struct bvec_iter bi = { 3866a829eb8STrond Myklebust .bi_size = count, 3876a829eb8STrond Myklebust }; 3886a829eb8STrond Myklebust struct bio_vec bv; 3896a829eb8STrond Myklebust 3906a829eb8STrond Myklebust bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 3916a829eb8STrond Myklebust for_each_bvec(bv, bvec, bi, bi) 3926a829eb8STrond Myklebust flush_dcache_page(bv.bv_page); 3936a829eb8STrond Myklebust } 3946a829eb8STrond Myklebust #else 3956a829eb8STrond Myklebust static inline void 3966a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 3976a829eb8STrond Myklebust { 3986a829eb8STrond Myklebust } 3996a829eb8STrond Myklebust #endif 4006a829eb8STrond Myklebust 401277e4ab7STrond Myklebust static ssize_t 402277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 403277e4ab7STrond Myklebust struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 404277e4ab7STrond Myklebust { 405277e4ab7STrond Myklebust size_t want, seek_init = seek, offset = 0; 406277e4ab7STrond Myklebust ssize_t ret; 407277e4ab7STrond Myklebust 408277e4ab7STrond Myklebust want = min_t(size_t, count, buf->head[0].iov_len); 409e92053a5STrond Myklebust if (seek < want) { 410277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 411277e4ab7STrond Myklebust if (ret <= 0) 412277e4ab7STrond Myklebust goto sock_err; 413277e4ab7STrond Myklebust offset += ret; 414277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 415277e4ab7STrond Myklebust goto out; 416277e4ab7STrond Myklebust if (ret != want) 417c4433055STrond Myklebust goto out; 418277e4ab7STrond Myklebust seek = 0; 419277e4ab7STrond Myklebust } else { 420e92053a5STrond Myklebust seek -= want; 421e92053a5STrond Myklebust offset += want; 422277e4ab7STrond Myklebust } 42316e5e90fSTrond Myklebust 424277e4ab7STrond Myklebust want = xs_alloc_sparse_pages(buf, 425277e4ab7STrond Myklebust min_t(size_t, count - offset, buf->page_len), 42612a3ad61STrond Myklebust GFP_KERNEL); 42716e5e90fSTrond Myklebust if (seek < want) { 428277e4ab7STrond Myklebust ret = xs_read_bvec(sock, msg, flags, buf->bvec, 429277e4ab7STrond Myklebust xdr_buf_pagecount(buf), 430277e4ab7STrond Myklebust want + buf->page_base, 431277e4ab7STrond Myklebust seek + buf->page_base); 432277e4ab7STrond Myklebust if (ret <= 0) 433277e4ab7STrond Myklebust goto sock_err; 4346a829eb8STrond Myklebust xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 435277e4ab7STrond Myklebust offset += ret - buf->page_base; 436277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 437277e4ab7STrond Myklebust goto out; 438277e4ab7STrond Myklebust if (ret != want) 439c4433055STrond Myklebust goto out; 440277e4ab7STrond Myklebust seek = 0; 441277e4ab7STrond Myklebust } else { 44216e5e90fSTrond Myklebust seek -= want; 44316e5e90fSTrond Myklebust offset += want; 444277e4ab7STrond Myklebust } 44516e5e90fSTrond Myklebust 446277e4ab7STrond Myklebust want = min_t(size_t, count - offset, buf->tail[0].iov_len); 447e92053a5STrond Myklebust if (seek < want) { 448277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 449277e4ab7STrond Myklebust if (ret <= 0) 450277e4ab7STrond Myklebust goto sock_err; 451277e4ab7STrond Myklebust offset += ret; 452277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 453277e4ab7STrond Myklebust goto out; 454277e4ab7STrond Myklebust if (ret != want) 455c4433055STrond Myklebust goto out; 4569734ad57STrond Myklebust } else if (offset < seek_init) 457e92053a5STrond Myklebust offset = seek_init; 458277e4ab7STrond Myklebust ret = -EMSGSIZE; 459277e4ab7STrond Myklebust out: 460277e4ab7STrond Myklebust *read = offset - seek_init; 461277e4ab7STrond Myklebust return ret; 462277e4ab7STrond Myklebust sock_err: 463277e4ab7STrond Myklebust offset += seek; 464277e4ab7STrond Myklebust goto out; 465277e4ab7STrond Myklebust } 466277e4ab7STrond Myklebust 467277e4ab7STrond Myklebust static void 468277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 469277e4ab7STrond Myklebust { 470277e4ab7STrond Myklebust if (!transport->recv.copied) { 471277e4ab7STrond Myklebust if (buf->head[0].iov_len >= transport->recv.offset) 472277e4ab7STrond Myklebust memcpy(buf->head[0].iov_base, 473277e4ab7STrond Myklebust &transport->recv.xid, 474277e4ab7STrond Myklebust transport->recv.offset); 475277e4ab7STrond Myklebust transport->recv.copied = transport->recv.offset; 476277e4ab7STrond Myklebust } 477277e4ab7STrond Myklebust } 478277e4ab7STrond Myklebust 479277e4ab7STrond Myklebust static bool 480277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport) 481277e4ab7STrond Myklebust { 482277e4ab7STrond Myklebust return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 483277e4ab7STrond Myklebust } 484277e4ab7STrond Myklebust 485e92053a5STrond Myklebust static void 486e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport, 487e92053a5STrond Myklebust struct msghdr *msg) 488e92053a5STrond Myklebust { 489e92053a5STrond Myklebust if (xs_read_stream_request_done(transport)) 490e92053a5STrond Myklebust msg->msg_flags |= MSG_EOR; 491e92053a5STrond Myklebust } 492e92053a5STrond Myklebust 493277e4ab7STrond Myklebust static ssize_t 494277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 495277e4ab7STrond Myklebust int flags, struct rpc_rqst *req) 496277e4ab7STrond Myklebust { 497277e4ab7STrond Myklebust struct xdr_buf *buf = &req->rq_private_buf; 49801f2f5b8SAlakesh Haloi size_t want, uninitialized_var(read); 49901f2f5b8SAlakesh Haloi ssize_t uninitialized_var(ret); 500277e4ab7STrond Myklebust 501277e4ab7STrond Myklebust xs_read_header(transport, buf); 502277e4ab7STrond Myklebust 503277e4ab7STrond Myklebust want = transport->recv.len - transport->recv.offset; 504e92053a5STrond Myklebust if (want != 0) { 505277e4ab7STrond Myklebust ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 506e92053a5STrond Myklebust transport->recv.copied + want, 507e92053a5STrond Myklebust transport->recv.copied, 508277e4ab7STrond Myklebust &read); 509277e4ab7STrond Myklebust transport->recv.offset += read; 510277e4ab7STrond Myklebust transport->recv.copied += read; 511277e4ab7STrond Myklebust } 512277e4ab7STrond Myklebust 513727fcc64STrond Myklebust if (transport->recv.offset == transport->recv.len) 514727fcc64STrond Myklebust xs_read_stream_check_eor(transport, msg); 515727fcc64STrond Myklebust 516e92053a5STrond Myklebust if (want == 0) 517e92053a5STrond Myklebust return 0; 518e92053a5STrond Myklebust 519277e4ab7STrond Myklebust switch (ret) { 520c4433055STrond Myklebust default: 521c4433055STrond Myklebust break; 52226781eabSTrond Myklebust case -EFAULT: 523277e4ab7STrond Myklebust case -EMSGSIZE: 52426781eabSTrond Myklebust msg->msg_flags |= MSG_TRUNC; 525c4433055STrond Myklebust return read; 526277e4ab7STrond Myklebust case 0: 527277e4ab7STrond Myklebust return -ESHUTDOWN; 528277e4ab7STrond Myklebust } 529c4433055STrond Myklebust return ret < 0 ? ret : read; 530277e4ab7STrond Myklebust } 531277e4ab7STrond Myklebust 532277e4ab7STrond Myklebust static size_t 533277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag) 534277e4ab7STrond Myklebust { 535277e4ab7STrond Myklebust if (isfrag) 536277e4ab7STrond Myklebust return sizeof(__be32); 537277e4ab7STrond Myklebust return 3 * sizeof(__be32); 538277e4ab7STrond Myklebust } 539277e4ab7STrond Myklebust 540277e4ab7STrond Myklebust static ssize_t 541277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 542277e4ab7STrond Myklebust int flags, size_t want, size_t seek) 543277e4ab7STrond Myklebust { 544277e4ab7STrond Myklebust struct kvec kvec = { 545277e4ab7STrond Myklebust .iov_base = &transport->recv.fraghdr, 546277e4ab7STrond Myklebust .iov_len = want, 547277e4ab7STrond Myklebust }; 548277e4ab7STrond Myklebust return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 549277e4ab7STrond Myklebust } 550277e4ab7STrond Myklebust 551277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 552277e4ab7STrond Myklebust static ssize_t 553277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 554277e4ab7STrond Myklebust { 555277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 556277e4ab7STrond Myklebust struct rpc_rqst *req; 557277e4ab7STrond Myklebust ssize_t ret; 558277e4ab7STrond Myklebust 559277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 560277e4ab7STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 561277e4ab7STrond Myklebust if (!req) { 562277e4ab7STrond Myklebust printk(KERN_WARNING "Callback slot table overflowed\n"); 563277e4ab7STrond Myklebust return -ESHUTDOWN; 564277e4ab7STrond Myklebust } 565277e4ab7STrond Myklebust 566277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 567277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 568c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 569277e4ab7STrond Myklebust 570277e4ab7STrond Myklebust return ret; 571277e4ab7STrond Myklebust } 572277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 573277e4ab7STrond Myklebust static ssize_t 574277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 575277e4ab7STrond Myklebust { 576277e4ab7STrond Myklebust return -ESHUTDOWN; 577277e4ab7STrond Myklebust } 578277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 579277e4ab7STrond Myklebust 580277e4ab7STrond Myklebust static ssize_t 581277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 582277e4ab7STrond Myklebust { 583277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 584277e4ab7STrond Myklebust struct rpc_rqst *req; 585277e4ab7STrond Myklebust ssize_t ret = 0; 586277e4ab7STrond Myklebust 587277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 588277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 589277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 590277e4ab7STrond Myklebust if (!req) { 591277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 592277e4ab7STrond Myklebust goto out; 593277e4ab7STrond Myklebust } 594277e4ab7STrond Myklebust xprt_pin_rqst(req); 595277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 596277e4ab7STrond Myklebust 597277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 598277e4ab7STrond Myklebust 599277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 600277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 601c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 602277e4ab7STrond Myklebust xprt_unpin_rqst(req); 603277e4ab7STrond Myklebust out: 604277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 605277e4ab7STrond Myklebust return ret; 606277e4ab7STrond Myklebust } 607277e4ab7STrond Myklebust 608277e4ab7STrond Myklebust static ssize_t 609277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 610277e4ab7STrond Myklebust { 611277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 612277e4ab7STrond Myklebust size_t want, read = 0; 613277e4ab7STrond Myklebust ssize_t ret = 0; 614277e4ab7STrond Myklebust 615277e4ab7STrond Myklebust if (transport->recv.len == 0) { 616277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 617277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 618277e4ab7STrond Myklebust transport->recv.offset); 619277e4ab7STrond Myklebust if (ret <= 0) 620277e4ab7STrond Myklebust goto out_err; 621277e4ab7STrond Myklebust transport->recv.offset = ret; 622c4433055STrond Myklebust if (transport->recv.offset != want) 623c4433055STrond Myklebust return transport->recv.offset; 624277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 625277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 626277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 627277e4ab7STrond Myklebust read = ret; 628277e4ab7STrond Myklebust } 629277e4ab7STrond Myklebust 630277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 631c4433055STrond Myklebust default: 632c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 633c4433055STrond Myklebust break; 634277e4ab7STrond Myklebust case RPC_CALL: 635277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 636277e4ab7STrond Myklebust break; 637277e4ab7STrond Myklebust case RPC_REPLY: 638277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 639277e4ab7STrond Myklebust } 640277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 641277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 642277e4ab7STrond Myklebust transport->recv.copied = -1; 643277e4ab7STrond Myklebust } 644277e4ab7STrond Myklebust if (ret < 0) 645277e4ab7STrond Myklebust goto out_err; 646277e4ab7STrond Myklebust read += ret; 647277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 648c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 649c4433055STrond Myklebust return read; 650b76a5afdSTrond Myklebust msg.msg_flags = 0; 651277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 652277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 653277e4ab7STrond Myklebust if (ret <= 0) 654277e4ab7STrond Myklebust goto out_err; 655277e4ab7STrond Myklebust transport->recv.offset += ret; 656277e4ab7STrond Myklebust read += ret; 657277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 658c4433055STrond Myklebust return read; 659277e4ab7STrond Myklebust } 660277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 661c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 662277e4ab7STrond Myklebust transport->recv.copied = 0; 663277e4ab7STrond Myklebust } 664277e4ab7STrond Myklebust transport->recv.offset = 0; 665277e4ab7STrond Myklebust transport->recv.len = 0; 666277e4ab7STrond Myklebust return read; 667277e4ab7STrond Myklebust out_err: 66879462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 669277e4ab7STrond Myklebust } 670277e4ab7STrond Myklebust 6710ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 6720ffe86f4STrond Myklebust { 673a73881c9STrond Myklebust return transport->sock->ops->poll(transport->file, transport->sock, 674a73881c9STrond Myklebust NULL); 6750ffe86f4STrond Myklebust } 6760ffe86f4STrond Myklebust 6770ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 6780ffe86f4STrond Myklebust { 6790ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 6800ffe86f4STrond Myklebust 6810ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 6820ffe86f4STrond Myklebust } 6830ffe86f4STrond Myklebust 6840ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 6850ffe86f4STrond Myklebust { 6860ffe86f4STrond Myklebust 6870ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 6880ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 6890ffe86f4STrond Myklebust return; 6900ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 6910ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 6920ffe86f4STrond Myklebust } 6930ffe86f4STrond Myklebust 694c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 695c50b8ee0STrond Myklebust { 696c50b8ee0STrond Myklebust size_t read = 0; 697c50b8ee0STrond Myklebust ssize_t ret = 0; 698c50b8ee0STrond Myklebust 699c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 700c50b8ee0STrond Myklebust if (transport->sock == NULL) 701c50b8ee0STrond Myklebust goto out; 702c50b8ee0STrond Myklebust for (;;) { 703c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 704c4433055STrond Myklebust if (ret < 0) 705c50b8ee0STrond Myklebust break; 706c50b8ee0STrond Myklebust read += ret; 707c50b8ee0STrond Myklebust cond_resched(); 708c50b8ee0STrond Myklebust } 7095f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7105f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 7115f52a9d4STrond Myklebust else 7120ffe86f4STrond Myklebust xs_poll_check_readable(transport); 713c50b8ee0STrond Myklebust out: 714c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 715c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 716c50b8ee0STrond Myklebust } 717c50b8ee0STrond Myklebust 718c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 719c50b8ee0STrond Myklebust { 720c50b8ee0STrond Myklebust struct sock_xprt *transport = 721c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 722a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 723a1231fdaSTrond Myklebust 724c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 725a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 726c50b8ee0STrond Myklebust } 727c50b8ee0STrond Myklebust 728550aebfeSTrond Myklebust static void 729550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 730550aebfeSTrond Myklebust { 731550aebfeSTrond Myklebust transport->recv.offset = 0; 732550aebfeSTrond Myklebust transport->recv.len = 0; 733550aebfeSTrond Myklebust transport->recv.copied = 0; 734550aebfeSTrond Myklebust transport->xmit.offset = 0; 735ae053551STrond Myklebust } 736ae053551STrond Myklebust 737ae053551STrond Myklebust static void 738ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 739ae053551STrond Myklebust { 740550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 741550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 742550aebfeSTrond Myklebust } 743550aebfeSTrond Myklebust 744b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 745b4b5cc85SChuck Lever 746e791f8e9STrond Myklebust static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek) 747b4b5cc85SChuck Lever { 748e791f8e9STrond Myklebust if (seek) 749e791f8e9STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 750e791f8e9STrond Myklebust return sock_sendmsg(sock, msg); 751e791f8e9STrond Myklebust } 752b4b5cc85SChuck Lever 753e791f8e9STrond Myklebust static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek) 754e791f8e9STrond Myklebust { 755e791f8e9STrond Myklebust iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len); 756e791f8e9STrond Myklebust return xs_sendmsg(sock, msg, seek); 757b4b5cc85SChuck Lever } 758b4b5cc85SChuck Lever 7590472e476STrond Myklebust static int xs_send_pagedata(struct socket *sock, struct msghdr *msg, struct xdr_buf *xdr, size_t base) 760b4b5cc85SChuck Lever { 761f279cd00SJason Baron int err; 762b4b5cc85SChuck Lever 7630472e476STrond Myklebust err = xdr_alloc_bvec(xdr, GFP_KERNEL); 7640472e476STrond Myklebust if (err < 0) 76524c5684bSTrond Myklebust return err; 7660472e476STrond Myklebust 7670472e476STrond Myklebust iov_iter_bvec(&msg->msg_iter, WRITE, xdr->bvec, 7680472e476STrond Myklebust xdr_buf_pagecount(xdr), 7690472e476STrond Myklebust xdr->page_len + xdr->page_base); 7700472e476STrond Myklebust return xs_sendmsg(sock, msg, base + xdr->page_base); 771b4b5cc85SChuck Lever } 772b4b5cc85SChuck Lever 77306b5fc3aSTrond Myklebust #define xs_record_marker_len() sizeof(rpc_fraghdr) 77406b5fc3aSTrond Myklebust 775067fb11bSChuck Lever /* Common case: 776067fb11bSChuck Lever * - stream transport 777067fb11bSChuck Lever * - sending from byte 0 of the message 778067fb11bSChuck Lever * - the message is wholly contained in @xdr's head iovec 779067fb11bSChuck Lever */ 78006b5fc3aSTrond Myklebust static int xs_send_rm_and_kvec(struct socket *sock, struct msghdr *msg, 78106b5fc3aSTrond Myklebust rpc_fraghdr marker, struct kvec *vec, size_t base) 782067fb11bSChuck Lever { 783067fb11bSChuck Lever struct kvec iov[2] = { 78406b5fc3aSTrond Myklebust [0] = { 785067fb11bSChuck Lever .iov_base = &marker, 786067fb11bSChuck Lever .iov_len = sizeof(marker) 787067fb11bSChuck Lever }, 78806b5fc3aSTrond Myklebust [1] = *vec, 789067fb11bSChuck Lever }; 79006b5fc3aSTrond Myklebust size_t len = iov[0].iov_len + iov[1].iov_len; 791067fb11bSChuck Lever 79206b5fc3aSTrond Myklebust iov_iter_kvec(&msg->msg_iter, WRITE, iov, 2, len); 79306b5fc3aSTrond Myklebust return xs_sendmsg(sock, msg, base); 794a246b010SChuck Lever } 795a246b010SChuck Lever 7969903cd1cSChuck Lever /** 7979903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 7989903cd1cSChuck Lever * @sock: socket to send on 7999903cd1cSChuck Lever * @addr: UDP only -- address of destination 8009903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 8019903cd1cSChuck Lever * @xdr: buffer containing this request 8029903cd1cSChuck Lever * @base: starting position in the buffer 80306b5fc3aSTrond Myklebust * @rm: stream record marker field 804f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 8059903cd1cSChuck Lever * 806a246b010SChuck Lever */ 80706b5fc3aSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, rpc_fraghdr rm, int *sent_p) 808a246b010SChuck Lever { 809e791f8e9STrond Myklebust struct msghdr msg = { 810e791f8e9STrond Myklebust .msg_name = addr, 811e791f8e9STrond Myklebust .msg_namelen = addrlen, 812e791f8e9STrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE, 813e791f8e9STrond Myklebust }; 81406b5fc3aSTrond Myklebust unsigned int rmsize = rm ? sizeof(rm) : 0; 81506b5fc3aSTrond Myklebust unsigned int remainder = rmsize + xdr->len - base; 81606b5fc3aSTrond Myklebust unsigned int want; 817f279cd00SJason Baron int err = 0; 818a246b010SChuck Lever 819262965f5SChuck Lever if (unlikely(!sock)) 820fba91afbSTrond Myklebust return -ENOTSOCK; 821262965f5SChuck Lever 82206b5fc3aSTrond Myklebust want = xdr->head[0].iov_len + rmsize; 82306b5fc3aSTrond Myklebust if (base < want) { 82406b5fc3aSTrond Myklebust unsigned int len = want - base; 82524c5684bSTrond Myklebust remainder -= len; 826e791f8e9STrond Myklebust if (remainder == 0) 827e791f8e9STrond Myklebust msg.msg_flags &= ~MSG_MORE; 82806b5fc3aSTrond Myklebust if (rmsize) 82906b5fc3aSTrond Myklebust err = xs_send_rm_and_kvec(sock, &msg, rm, 83006b5fc3aSTrond Myklebust &xdr->head[0], base); 831067fb11bSChuck Lever else 832e791f8e9STrond Myklebust err = xs_send_kvec(sock, &msg, &xdr->head[0], base); 83324c5684bSTrond Myklebust if (remainder == 0 || err != len) 834a246b010SChuck Lever goto out; 835f279cd00SJason Baron *sent_p += err; 836a246b010SChuck Lever base = 0; 837a246b010SChuck Lever } else 83806b5fc3aSTrond Myklebust base -= want; 839a246b010SChuck Lever 84024c5684bSTrond Myklebust if (base < xdr->page_len) { 84124c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 84224c5684bSTrond Myklebust remainder -= len; 8430472e476STrond Myklebust if (remainder == 0) 8440472e476STrond Myklebust msg.msg_flags &= ~MSG_MORE; 8450472e476STrond Myklebust err = xs_send_pagedata(sock, &msg, xdr, base); 8460472e476STrond Myklebust if (remainder == 0 || err != len) 847a246b010SChuck Lever goto out; 8480472e476STrond Myklebust *sent_p += err; 849a246b010SChuck Lever base = 0; 85024c5684bSTrond Myklebust } else 85124c5684bSTrond Myklebust base -= xdr->page_len; 85224c5684bSTrond Myklebust 85324c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 854f279cd00SJason Baron return 0; 855e791f8e9STrond Myklebust msg.msg_flags &= ~MSG_MORE; 856e791f8e9STrond Myklebust err = xs_send_kvec(sock, &msg, &xdr->tail[0], base); 857a246b010SChuck Lever out: 858f279cd00SJason Baron if (err > 0) { 859f279cd00SJason Baron *sent_p += err; 860f279cd00SJason Baron err = 0; 861f279cd00SJason Baron } 86224c5684bSTrond Myklebust return err; 863a246b010SChuck Lever } 864a246b010SChuck Lever 8659903cd1cSChuck Lever /** 866c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 86750f484e2STrond Myklebust * @req: pointer to RPC request 8689903cd1cSChuck Lever * 869a246b010SChuck Lever */ 870c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req) 871a246b010SChuck Lever { 872262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 873ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 87406ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 87524ca9a84STrond Myklebust int ret = -EAGAIN; 876a246b010SChuck Lever 87746121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 878c544577dSTrond Myklebust req->rq_task->tk_pid, 879c544577dSTrond Myklebust req->rq_slen - transport->xmit.offset, 880262965f5SChuck Lever req->rq_slen); 881a246b010SChuck Lever 882262965f5SChuck Lever /* Protect against races with write_space */ 883262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 884a246b010SChuck Lever 885262965f5SChuck Lever /* Don't race with disconnect */ 886b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 88713331a55STrond Myklebust /* wait for more buffer space */ 88806ea0bfeSTrond Myklebust sk->sk_write_pending++; 889c544577dSTrond Myklebust xprt_wait_for_buffer_space(xprt); 89013331a55STrond Myklebust } else 8915e3771ceSTrond Myklebust ret = -ENOTCONN; 892a246b010SChuck Lever 893262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 89406ea0bfeSTrond Myklebust 89506ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 896d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 897d48f9ce7SDavid Vrabel struct socket_wq *wq; 898d48f9ce7SDavid Vrabel 899d48f9ce7SDavid Vrabel rcu_read_lock(); 900d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 901d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 902d48f9ce7SDavid Vrabel rcu_read_unlock(); 903d48f9ce7SDavid Vrabel 90406ea0bfeSTrond Myklebust sk->sk_write_space(sk); 905d48f9ce7SDavid Vrabel } 9065e3771ceSTrond Myklebust return ret; 907a246b010SChuck Lever } 908a246b010SChuck Lever 909277e4ab7STrond Myklebust static void 910277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req) 911277e4ab7STrond Myklebust { 91212a3ad61STrond Myklebust req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL); 913277e4ab7STrond Myklebust } 914277e4ab7STrond Myklebust 91561677eeeSChuck Lever /* 9164cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 9174cd34e7cSTrond Myklebust * could complete transmission. 9184cd34e7cSTrond Myklebust */ 9194cd34e7cSTrond Myklebust static bool 9204cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 9214cd34e7cSTrond Myklebust { 9224cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 9234cd34e7cSTrond Myklebust } 9244cd34e7cSTrond Myklebust 9254cd34e7cSTrond Myklebust /* 92606b5fc3aSTrond Myklebust * Return the stream record marker field for a record of length < 2^31-1 92761677eeeSChuck Lever */ 92806b5fc3aSTrond Myklebust static rpc_fraghdr 92906b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr) 93061677eeeSChuck Lever { 93106b5fc3aSTrond Myklebust if (!xdr->len) 93206b5fc3aSTrond Myklebust return 0; 93306b5fc3aSTrond Myklebust return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 93461677eeeSChuck Lever } 93561677eeeSChuck Lever 9369903cd1cSChuck Lever /** 937176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 93850f484e2STrond Myklebust * @req: pointer to RPC request 939176e21eeSChuck Lever * 940176e21eeSChuck Lever * Return values: 941176e21eeSChuck Lever * 0: The request has been sent 942176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 943176e21eeSChuck Lever * complete the request 944176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 945176e21eeSChuck Lever * other: Some other error occured, the request was not sent 946176e21eeSChuck Lever */ 947adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 948176e21eeSChuck Lever { 949176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 950176e21eeSChuck Lever struct sock_xprt *transport = 951176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 952176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9537e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 9547e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 955176e21eeSChuck Lever int status; 956f279cd00SJason Baron int sent = 0; 957176e21eeSChuck Lever 9584cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 9594cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 9604cd34e7cSTrond Myklebust xs_close(xprt); 9614cd34e7cSTrond Myklebust return -ENOTCONN; 9624cd34e7cSTrond Myklebust } 9634cd34e7cSTrond Myklebust 964176e21eeSChuck Lever xs_pktdump("packet data:", 965176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 966176e21eeSChuck Lever 96778215759SChuck Lever req->rq_xtime = ktime_get(); 9686c7a64e5STrond Myklebust status = xs_sendpages(transport->sock, NULL, 0, xdr, 9697e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 970176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 9716c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 972743c69e7SNeilBrown 973743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 974743c69e7SNeilBrown status = -ENOBUFS; 975743c69e7SNeilBrown 976f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 9776c7a64e5STrond Myklebust transport->xmit.offset += sent; 9786c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 9797e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 9806c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 9816c7a64e5STrond Myklebust transport->xmit.offset = 0; 982176e21eeSChuck Lever return 0; 983176e21eeSChuck Lever } 984176e21eeSChuck Lever status = -EAGAIN; 985176e21eeSChuck Lever } 986176e21eeSChuck Lever 987176e21eeSChuck Lever switch (status) { 9883601c4a9STrond Myklebust case -ENOBUFS: 989b5872f0cSTrond Myklebust break; 990176e21eeSChuck Lever case -EAGAIN: 991c544577dSTrond Myklebust status = xs_nospace(req); 992176e21eeSChuck Lever break; 993176e21eeSChuck Lever default: 994176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 995176e21eeSChuck Lever -status); 996e9d47639SGustavo A. R. Silva /* fall through */ 997176e21eeSChuck Lever case -EPIPE: 998176e21eeSChuck Lever xs_close(xprt); 999176e21eeSChuck Lever status = -ENOTCONN; 1000176e21eeSChuck Lever } 1001176e21eeSChuck Lever 1002176e21eeSChuck Lever return status; 1003176e21eeSChuck Lever } 1004176e21eeSChuck Lever 1005176e21eeSChuck Lever /** 1006262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 100750f484e2STrond Myklebust * @req: pointer to RPC request 10089903cd1cSChuck Lever * 10099903cd1cSChuck Lever * Return values: 10109903cd1cSChuck Lever * 0: The request has been sent 10119903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 10129903cd1cSChuck Lever * complete the request 1013262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 101425985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 10159903cd1cSChuck Lever */ 1016adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 1017a246b010SChuck Lever { 1018a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1019ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1020262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 1021f279cd00SJason Baron int sent = 0; 1022262965f5SChuck Lever int status; 1023262965f5SChuck Lever 1024262965f5SChuck Lever xs_pktdump("packet data:", 1025262965f5SChuck Lever req->rq_svec->iov_base, 1026262965f5SChuck Lever req->rq_svec->iov_len); 1027262965f5SChuck Lever 102801d37c42STrond Myklebust if (!xprt_bound(xprt)) 102901d37c42STrond Myklebust return -ENOTCONN; 103075891f50STrond Myklebust 103175891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 103275891f50STrond Myklebust return -EBADSLT; 103375891f50STrond Myklebust 103478215759SChuck Lever req->rq_xtime = ktime_get(); 1035f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 103606b5fc3aSTrond Myklebust xdr, 0, 0, &sent); 1037262965f5SChuck Lever 1038262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 10396c7a64e5STrond Myklebust xdr->len, status); 1040262965f5SChuck Lever 10413dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 10423dedbb5cSJason Baron if (status == -EPERM) 10433dedbb5cSJason Baron goto process_status; 10443dedbb5cSJason Baron 1045743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 1046743c69e7SNeilBrown status = -ENOBUFS; 1047743c69e7SNeilBrown 1048f279cd00SJason Baron if (sent > 0 || status == 0) { 1049f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 1050f279cd00SJason Baron if (sent >= req->rq_slen) 1051262965f5SChuck Lever return 0; 1052262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 1053262965f5SChuck Lever status = -EAGAIN; 10542199700fSTrond Myklebust } 1055262965f5SChuck Lever 10563dedbb5cSJason Baron process_status: 1057262965f5SChuck Lever switch (status) { 1058fba91afbSTrond Myklebust case -ENOTSOCK: 1059fba91afbSTrond Myklebust status = -ENOTCONN; 1060fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1061fba91afbSTrond Myklebust break; 1062b6ddf64fSTrond Myklebust case -EAGAIN: 1063c544577dSTrond Myklebust status = xs_nospace(req); 1064b6ddf64fSTrond Myklebust break; 1065262965f5SChuck Lever case -ENETUNREACH: 10663601c4a9STrond Myklebust case -ENOBUFS: 1067262965f5SChuck Lever case -EPIPE: 1068262965f5SChuck Lever case -ECONNREFUSED: 10693dedbb5cSJason Baron case -EPERM: 1070262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 1071262965f5SChuck Lever * prompts ECONNREFUSED. */ 107213331a55STrond Myklebust break; 107313331a55STrond Myklebust default: 107413331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 107513331a55STrond Myklebust -status); 1076262965f5SChuck Lever } 10775fe46e9dSBian Naimeng 1078262965f5SChuck Lever return status; 1079262965f5SChuck Lever } 1080262965f5SChuck Lever 1081e06799f9STrond Myklebust /** 1082262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 108350f484e2STrond Myklebust * @req: pointer to RPC request 1084262965f5SChuck Lever * 1085262965f5SChuck Lever * Return values: 1086262965f5SChuck Lever * 0: The request has been sent 1087262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 1088262965f5SChuck Lever * complete the request 1089262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 109025985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 1091262965f5SChuck Lever * 1092262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 1093262965f5SChuck Lever * if sendmsg is not able to make progress? 1094262965f5SChuck Lever */ 1095adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 1096262965f5SChuck Lever { 1097262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1098ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1099262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 11007e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 11017e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 11029ffadfbcSTrond Myklebust bool vm_wait = false; 1103b595bb15SChuck Lever int status; 1104f279cd00SJason Baron int sent; 1105a246b010SChuck Lever 11064cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 11074cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 11084cd34e7cSTrond Myklebust if (transport->sock != NULL) 11094cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 11104cd34e7cSTrond Myklebust return -ENOTCONN; 11114cd34e7cSTrond Myklebust } 11124cd34e7cSTrond Myklebust 1113262965f5SChuck Lever xs_pktdump("packet data:", 1114262965f5SChuck Lever req->rq_svec->iov_base, 1115262965f5SChuck Lever req->rq_svec->iov_len); 1116a246b010SChuck Lever 11177196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 11187196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 11197196dbb0STrond Myklebust 1120a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1121a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1122262965f5SChuck Lever * called sendmsg(). */ 112378215759SChuck Lever req->rq_xtime = ktime_get(); 1124a246b010SChuck Lever while (1) { 1125f279cd00SJason Baron sent = 0; 1126f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 11277e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 1128a246b010SChuck Lever 1129262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 11306c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1131262965f5SChuck Lever 1132a246b010SChuck Lever /* If we've sent the entire packet, immediately 1133a246b010SChuck Lever * reset the count of bytes sent. */ 11346c7a64e5STrond Myklebust transport->xmit.offset += sent; 11356c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 11367e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 11376c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 11386c7a64e5STrond Myklebust transport->xmit.offset = 0; 1139a246b010SChuck Lever return 0; 1140a246b010SChuck Lever } 1141262965f5SChuck Lever 11429ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 11439ffadfbcSTrond Myklebust 11449ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 11459ffadfbcSTrond Myklebust /* 11469ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 11479ffadfbcSTrond Myklebust * socket send buffer limits. 11489ffadfbcSTrond Myklebust */ 11499ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 11509ffadfbcSTrond Myklebust break; 11519ffadfbcSTrond Myklebust /* 11529ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 11539ffadfbcSTrond Myklebust */ 11549ffadfbcSTrond Myklebust if (sent == 0) { 11559ffadfbcSTrond Myklebust status = -ENOBUFS; 11569ffadfbcSTrond Myklebust if (vm_wait) 11579ffadfbcSTrond Myklebust break; 11589ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 11599ffadfbcSTrond Myklebust * socket send buffer limit 11609ffadfbcSTrond Myklebust */ 11619ffadfbcSTrond Myklebust vm_wait = true; 11629ffadfbcSTrond Myklebust } 11639ffadfbcSTrond Myklebust continue; 11649ffadfbcSTrond Myklebust } 1165f580dd04STrond Myklebust if (status < 0) 1166f580dd04STrond Myklebust break; 11679ffadfbcSTrond Myklebust vm_wait = false; 1168a246b010SChuck Lever } 1169a246b010SChuck Lever 1170262965f5SChuck Lever switch (status) { 1171fba91afbSTrond Myklebust case -ENOTSOCK: 1172fba91afbSTrond Myklebust status = -ENOTCONN; 1173fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1174fba91afbSTrond Myklebust break; 1175262965f5SChuck Lever case -EAGAIN: 1176c544577dSTrond Myklebust status = xs_nospace(req); 1177262965f5SChuck Lever break; 1178262965f5SChuck Lever case -ECONNRESET: 1179262965f5SChuck Lever case -ECONNREFUSED: 1180262965f5SChuck Lever case -ENOTCONN: 11813913c78cSTrond Myklebust case -EADDRINUSE: 1182b5872f0cSTrond Myklebust case -ENOBUFS: 1183b9d2bb2eSTrond Myklebust case -EPIPE: 118413331a55STrond Myklebust break; 118513331a55STrond Myklebust default: 118613331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 118713331a55STrond Myklebust -status); 1188a246b010SChuck Lever } 11895fe46e9dSBian Naimeng 1190a246b010SChuck Lever return status; 1191a246b010SChuck Lever } 1192a246b010SChuck Lever 11932a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11942a9e1cfaSTrond Myklebust { 11952a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 11962a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 11972a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 11982118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 11992a9e1cfaSTrond Myklebust } 12002a9e1cfaSTrond Myklebust 12012a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 12022a9e1cfaSTrond Myklebust { 12032a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 12042a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 12052a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 12062118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 12072118071dSTrond Myklebust } 12082118071dSTrond Myklebust 120942d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 121042d42a5bSTrond Myklebust { 121142d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 121242d42a5bSTrond Myklebust 121342d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 1214*4f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 1215*4f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 1216*4f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 1217*4f8943f8STrond Myklebust } 1218*4f8943f8STrond Myklebust 1219*4f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 1220*4f8943f8STrond Myklebust { 1221*4f8943f8STrond Myklebust set_bit(nr, &transport->sock_state); 1222*4f8943f8STrond Myklebust queue_work(xprtiod_workqueue, &transport->error_worker); 122342d42a5bSTrond Myklebust } 122442d42a5bSTrond Myklebust 1225b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1226b70ae915STrond Myklebust { 1227b70ae915STrond Myklebust smp_mb__before_atomic(); 1228b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1229b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 123042d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1231b70ae915STrond Myklebust smp_mb__after_atomic(); 1232b70ae915STrond Myklebust } 1233b70ae915STrond Myklebust 12342118071dSTrond Myklebust /** 12352118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 12362118071dSTrond Myklebust * @sk: socket 12372118071dSTrond Myklebust * 12382118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 12392118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 12402118071dSTrond Myklebust */ 12412118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 12422118071dSTrond Myklebust { 1243*4f8943f8STrond Myklebust struct sock_xprt *transport; 12442118071dSTrond Myklebust struct rpc_xprt *xprt; 12452118071dSTrond Myklebust int err; 12462118071dSTrond Myklebust 12472118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 12482118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12492118071dSTrond Myklebust goto out; 12502118071dSTrond Myklebust 1251*4f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 12522118071dSTrond Myklebust err = -sk->sk_err; 12532118071dSTrond Myklebust if (err == 0) 12542118071dSTrond Myklebust goto out; 12552118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 12562118071dSTrond Myklebust xprt, -err); 1257e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 1258*4f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 12592118071dSTrond Myklebust out: 12602118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 12612a9e1cfaSTrond Myklebust } 12622a9e1cfaSTrond Myklebust 1263fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1264a246b010SChuck Lever { 1265ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1266ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12676cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1268a73881c9STrond Myklebust struct file *filp = transport->file; 1269a246b010SChuck Lever 1270fe315e76SChuck Lever if (sk == NULL) 1271fe315e76SChuck Lever return; 12729903cd1cSChuck Lever 1273264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1274264d1df3SJeff Layton sk_clear_memalloc(sk); 1275264d1df3SJeff Layton 127609939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 127709939204STrond Myklebust 1278edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1279a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1280ee0ac0c2SChuck Lever transport->inet = NULL; 1281ee0ac0c2SChuck Lever transport->sock = NULL; 1282a73881c9STrond Myklebust transport->file = NULL; 1283a246b010SChuck Lever 1284a246b010SChuck Lever sk->sk_user_data = NULL; 12852a9e1cfaSTrond Myklebust 12862a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12870c78789eSTrond Myklebust xprt_clear_connected(xprt); 1288a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 12896cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1290ae053551STrond Myklebust /* Reset stream record info */ 1291ae053551STrond Myklebust xs_stream_reset_connect(transport); 1292edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1293a246b010SChuck Lever 12946cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1295a73881c9STrond Myklebust fput(filp); 12960445f92cSTrond Myklebust 12970445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1298fe315e76SChuck Lever } 1299fe315e76SChuck Lever 1300fe315e76SChuck Lever /** 1301fe315e76SChuck Lever * xs_close - close a socket 1302fe315e76SChuck Lever * @xprt: transport 1303fe315e76SChuck Lever * 1304fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1305fe315e76SChuck Lever * on the server we want to save. 1306f75e6745STrond Myklebust * 1307f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1308f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1309fe315e76SChuck Lever */ 1310fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1311fe315e76SChuck Lever { 1312fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1313fe315e76SChuck Lever 1314fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1315fe315e76SChuck Lever 1316fe315e76SChuck Lever xs_reset_transport(transport); 131761d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1318a246b010SChuck Lever } 1319a246b010SChuck Lever 13204a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13214a068258SChuck Lever { 13224a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13234a068258SChuck Lever xprt); 13244a068258SChuck Lever xprt_disconnect_done(xprt); 13254a068258SChuck Lever } 13264a068258SChuck Lever 1327315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1328315f3812SKinglong Mee { 1329315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1330315f3812SKinglong Mee xprt_free(xprt); 1331315f3812SKinglong Mee } 1332315f3812SKinglong Mee 13339903cd1cSChuck Lever /** 13349903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13359903cd1cSChuck Lever * @xprt: doomed transport 13369903cd1cSChuck Lever * 13379903cd1cSChuck Lever */ 13389903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1339a246b010SChuck Lever { 134003c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 134103c78827STrond Myklebust struct sock_xprt, xprt); 13429903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13439903cd1cSChuck Lever 134403c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1345a1311d87STrond Myklebust xs_close(xprt); 1346edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1347315f3812SKinglong Mee xs_xprt_free(xprt); 1348a1311d87STrond Myklebust module_put(THIS_MODULE); 1349a246b010SChuck Lever } 1350a246b010SChuck Lever 13519903cd1cSChuck Lever /** 1352f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1353f9b2ee71STrond Myklebust * @xprt: transport 1354f9b2ee71STrond Myklebust * @sk: socket 1355f9b2ee71STrond Myklebust * @skb: skbuff 13569903cd1cSChuck Lever * 1357a246b010SChuck Lever */ 1358f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1359f9b2ee71STrond Myklebust struct sock *sk, 1360f9b2ee71STrond Myklebust struct sk_buff *skb) 1361a246b010SChuck Lever { 1362a246b010SChuck Lever struct rpc_task *task; 1363a246b010SChuck Lever struct rpc_rqst *rovr; 1364f9b2ee71STrond Myklebust int repsize, copied; 1365d8ed029dSAlexey Dobriyan u32 _xid; 1366d8ed029dSAlexey Dobriyan __be32 *xp; 1367a246b010SChuck Lever 13681da8c681SWillem de Bruijn repsize = skb->len; 1369a246b010SChuck Lever if (repsize < 4) { 13709903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1371f9b2ee71STrond Myklebust return; 1372a246b010SChuck Lever } 1373a246b010SChuck Lever 1374a246b010SChuck Lever /* Copy the XID from the skb... */ 13751da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1376a246b010SChuck Lever if (xp == NULL) 1377f9b2ee71STrond Myklebust return; 1378a246b010SChuck Lever 1379a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 138075c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1381a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1382a246b010SChuck Lever if (!rovr) 1383a246b010SChuck Lever goto out_unlock; 1384729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1385ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 138675c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1387a246b010SChuck Lever task = rovr->rq_task; 1388a246b010SChuck Lever 1389a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1390a246b010SChuck Lever copied = repsize; 1391a246b010SChuck Lever 1392a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 13931781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 139475c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13950afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1396729749bbSTrond Myklebust goto out_unpin; 13971781f7f5SHerbert Xu } 13981781f7f5SHerbert Xu 1399a246b010SChuck Lever 1400729749bbSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 14016a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1402ce7c252aSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 140375c84151STrond Myklebust spin_lock(&xprt->queue_lock); 14041570c1e4SChuck Lever xprt_complete_rqst(task, copied); 14050afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1406729749bbSTrond Myklebust out_unpin: 1407729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1408a246b010SChuck Lever out_unlock: 140975c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1410f9b2ee71STrond Myklebust } 1411f9b2ee71STrond Myklebust 1412f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1413f9b2ee71STrond Myklebust { 1414f9b2ee71STrond Myklebust struct sk_buff *skb; 1415f9b2ee71STrond Myklebust struct sock *sk; 1416f9b2ee71STrond Myklebust int err; 1417f9b2ee71STrond Myklebust 1418f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1419f9b2ee71STrond Myklebust sk = transport->inet; 1420f9b2ee71STrond Myklebust if (sk == NULL) 1421f9b2ee71STrond Myklebust goto out; 1422f9b2ee71STrond Myklebust for (;;) { 14237c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 14244f546149STrond Myklebust if (skb == NULL) 14254f546149STrond Myklebust break; 1426f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1427850cbaddSPaolo Abeni consume_skb(skb); 14280af3442aSTrond Myklebust cond_resched(); 1429f9b2ee71STrond Myklebust } 14300ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1431a246b010SChuck Lever out: 1432f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1433f9b2ee71STrond Myklebust } 1434f9b2ee71STrond Myklebust 1435f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1436f9b2ee71STrond Myklebust { 1437f9b2ee71STrond Myklebust struct sock_xprt *transport = 1438f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1439a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1440a1231fdaSTrond Myklebust 1441f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1442a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1443f9b2ee71STrond Myklebust } 1444f9b2ee71STrond Myklebust 1445f9b2ee71STrond Myklebust /** 1446f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1447f9b2ee71STrond Myklebust * @sk: socket with data to read 1448f9b2ee71STrond Myklebust * 1449f9b2ee71STrond Myklebust */ 1450f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1451f9b2ee71STrond Myklebust { 1452f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1453f9b2ee71STrond Myklebust 1454f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1455f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1456f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1457f9b2ee71STrond Myklebust if (xprt != NULL) { 1458f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1459f9b2ee71STrond Myklebust struct sock_xprt, xprt); 14605157b956STrond Myklebust transport->old_data_ready(sk); 14615157b956STrond Myklebust /* Any data means we had a useful conversation, so 14625157b956STrond Myklebust * then we don't need to delay the next reconnect 14635157b956STrond Myklebust */ 14645157b956STrond Myklebust if (xprt->reestablish_timeout) 14655157b956STrond Myklebust xprt->reestablish_timeout = 0; 146642d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 146740a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1468f9b2ee71STrond Myklebust } 1469f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1470a246b010SChuck Lever } 1471a246b010SChuck Lever 1472a519fc7aSTrond Myklebust /* 1473a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1474a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1475a519fc7aSTrond Myklebust */ 1476a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1477a519fc7aSTrond Myklebust { 1478a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1479a519fc7aSTrond Myklebust } 1480a519fc7aSTrond Myklebust 14819e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14826b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14836b26cc8cSChuck Lever { 14846b26cc8cSChuck Lever return PAGE_SIZE; 14856b26cc8cSChuck Lever } 14869e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 148744b98efdSRicardo Labiaga 14889903cd1cSChuck Lever /** 14899903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14909903cd1cSChuck Lever * @sk: socket whose state has changed 14919903cd1cSChuck Lever * 14929903cd1cSChuck Lever */ 14939903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1494a246b010SChuck Lever { 1495a246b010SChuck Lever struct rpc_xprt *xprt; 14960fdea1e8STrond Myklebust struct sock_xprt *transport; 1497a246b010SChuck Lever 1498f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1499a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1500a246b010SChuck Lever goto out; 15019903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1502669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1503a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1504a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1505669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1506669502ffSAndy Chittenden sk->sk_shutdown); 1507a246b010SChuck Lever 15080fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 150940b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1510a246b010SChuck Lever switch (sk->sk_state) { 1511a246b010SChuck Lever case TCP_ESTABLISHED: 1512f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1513a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15148b71798cSTrond Myklebust xprt->connect_cookie++; 15150fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15160fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 151751971139SChuck Lever 15183968a8a5SChuck Lever xprt->stat.connect_count++; 15193968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15203968a8a5SChuck Lever xprt->stat.connect_start; 1521*4f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1522a246b010SChuck Lever } 1523f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1524a246b010SChuck Lever break; 15253b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15263b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15277c1d71cfSTrond Myklebust xprt->connect_cookie++; 1528663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15293b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15304e857c58SPeter Zijlstra smp_mb__before_atomic(); 15313b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1532ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15334e857c58SPeter Zijlstra smp_mb__after_atomic(); 1534a246b010SChuck Lever break; 1535632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15363b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15377c1d71cfSTrond Myklebust xprt->connect_cookie++; 1538d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1539*4f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1540e9d47639SGustavo A. R. Silva /* fall through */ 1541663b8858STrond Myklebust case TCP_CLOSING: 1542663b8858STrond Myklebust /* 1543663b8858STrond Myklebust * If the server closed down the connection, make sure that 1544663b8858STrond Myklebust * we back off before reconnecting 1545663b8858STrond Myklebust */ 1546663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1547663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15483b948ae5STrond Myklebust break; 15493b948ae5STrond Myklebust case TCP_LAST_ACK: 1550670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15514e857c58SPeter Zijlstra smp_mb__before_atomic(); 15523b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15534e857c58SPeter Zijlstra smp_mb__after_atomic(); 15543b948ae5STrond Myklebust break; 15553b948ae5STrond Myklebust case TCP_CLOSE: 15560fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15570fdea1e8STrond Myklebust &transport->sock_state)) 15580fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15599b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15609b30889cSTrond Myklebust /* Trigger the socket release */ 1561*4f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1562a246b010SChuck Lever } 1563a246b010SChuck Lever out: 1564f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1565a246b010SChuck Lever } 1566a246b010SChuck Lever 15671f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15681f0fa154SIlpo Järvinen { 156913331a55STrond Myklebust struct socket_wq *wq; 1570*4f8943f8STrond Myklebust struct sock_xprt *transport; 15711f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15721f0fa154SIlpo Järvinen 157313331a55STrond Myklebust if (!sk->sk_socket) 15741f0fa154SIlpo Järvinen return; 157513331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15761f0fa154SIlpo Järvinen 15771f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15781f0fa154SIlpo Järvinen return; 1579*4f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 158013331a55STrond Myklebust rcu_read_lock(); 158113331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 158213331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 158313331a55STrond Myklebust goto out; 15841f0fa154SIlpo Järvinen 1585*4f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1586c544577dSTrond Myklebust sk->sk_write_pending--; 158713331a55STrond Myklebust out: 158813331a55STrond Myklebust rcu_read_unlock(); 15891f0fa154SIlpo Järvinen } 15901f0fa154SIlpo Järvinen 15912a9e1cfaSTrond Myklebust /** 1592c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1593c7b2cae8SChuck Lever * becomes available 15949903cd1cSChuck Lever * @sk: socket whose state has changed 15959903cd1cSChuck Lever * 1596a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1597a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1598c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1599a246b010SChuck Lever * with a bunch of small requests. 1600a246b010SChuck Lever */ 1601c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1602a246b010SChuck Lever { 1603f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1604c7b2cae8SChuck Lever 1605c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16061f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16071f0fa154SIlpo Järvinen xs_write_space(sk); 1608c7b2cae8SChuck Lever 1609f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1610c7b2cae8SChuck Lever } 1611c7b2cae8SChuck Lever 1612c7b2cae8SChuck Lever /** 1613c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1614c7b2cae8SChuck Lever * becomes available 1615c7b2cae8SChuck Lever * @sk: socket whose state has changed 1616c7b2cae8SChuck Lever * 1617c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1618c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1619c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1620c7b2cae8SChuck Lever * with a bunch of small requests. 1621c7b2cae8SChuck Lever */ 1622c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1623c7b2cae8SChuck Lever { 1624f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1625c7b2cae8SChuck Lever 1626c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 162764dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16281f0fa154SIlpo Järvinen xs_write_space(sk); 1629c7b2cae8SChuck Lever 1630f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1631a246b010SChuck Lever } 1632a246b010SChuck Lever 1633470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1634a246b010SChuck Lever { 1635ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1636ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1637a246b010SChuck Lever 16387c6e066eSChuck Lever if (transport->rcvsize) { 1639a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16407c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1641a246b010SChuck Lever } 16427c6e066eSChuck Lever if (transport->sndsize) { 1643a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16447c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1645a246b010SChuck Lever sk->sk_write_space(sk); 1646a246b010SChuck Lever } 1647a246b010SChuck Lever } 1648a246b010SChuck Lever 164943118c29SChuck Lever /** 1650470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 165143118c29SChuck Lever * @xprt: generic transport 1652470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1653470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 165443118c29SChuck Lever * 1655470056c2SChuck Lever * Set socket send and receive buffer size limits. 165643118c29SChuck Lever */ 1657470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 165843118c29SChuck Lever { 16597c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16607c6e066eSChuck Lever 16617c6e066eSChuck Lever transport->sndsize = 0; 1662470056c2SChuck Lever if (sndsize) 16637c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16647c6e066eSChuck Lever transport->rcvsize = 0; 1665470056c2SChuck Lever if (rcvsize) 16667c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1667470056c2SChuck Lever 1668470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 166943118c29SChuck Lever } 167043118c29SChuck Lever 167146c0ee8bSChuck Lever /** 167246c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1673acf0a39fSChuck Lever * @xprt: controlling transport 167446c0ee8bSChuck Lever * @task: task that timed out 167546c0ee8bSChuck Lever * 167646c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 167746c0ee8bSChuck Lever */ 16786a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 167946c0ee8bSChuck Lever { 1680b977b644SChuck Lever spin_lock_bh(&xprt->transport_lock); 16816a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1682b977b644SChuck Lever spin_unlock_bh(&xprt->transport_lock); 168346c0ee8bSChuck Lever } 168446c0ee8bSChuck Lever 1685826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1686b85d8806SChuck Lever { 1687826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1688826799e6SJ. Bruce Fields unsigned short range; 1689826799e6SJ. Bruce Fields unsigned short rand; 1690826799e6SJ. Bruce Fields 1691826799e6SJ. Bruce Fields if (max < min) 1692826799e6SJ. Bruce Fields return -EADDRINUSE; 1693826799e6SJ. Bruce Fields range = max - min + 1; 1694826799e6SJ. Bruce Fields rand = (unsigned short) prandom_u32() % range; 1695826799e6SJ. Bruce Fields return rand + min; 1696b85d8806SChuck Lever } 1697b85d8806SChuck Lever 169892200412SChuck Lever /** 16994dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17004dda9c8aSTrond Myklebust * @sock: socket 17014dda9c8aSTrond Myklebust * 17024dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17034dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17044dda9c8aSTrond Myklebust */ 17054dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17064dda9c8aSTrond Myklebust { 1707402e23b4STrond Myklebust int opt = 1; 17084dda9c8aSTrond Myklebust 1709402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1710402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17114dda9c8aSTrond Myklebust } 17124dda9c8aSTrond Myklebust 17134dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17144dda9c8aSTrond Myklebust { 17154dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17164dda9c8aSTrond Myklebust unsigned short port = 0; 17174dda9c8aSTrond Myklebust 17189b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 17194dda9c8aSTrond Myklebust goto out; 17204dda9c8aSTrond Myklebust switch (buf.ss_family) { 17214dda9c8aSTrond Myklebust case AF_INET6: 17224dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17234dda9c8aSTrond Myklebust break; 17244dda9c8aSTrond Myklebust case AF_INET: 17254dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17264dda9c8aSTrond Myklebust } 17274dda9c8aSTrond Myklebust out: 17284dda9c8aSTrond Myklebust return port; 17294dda9c8aSTrond Myklebust } 17304dda9c8aSTrond Myklebust 17314dda9c8aSTrond Myklebust /** 173292200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 173392200412SChuck Lever * @xprt: generic transport 173492200412SChuck Lever * @port: new port number 173592200412SChuck Lever * 173692200412SChuck Lever */ 173792200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 173892200412SChuck Lever { 173992200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1740c4efcb1dSChuck Lever 17419dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17429dc3b095SChuck Lever xs_update_peer_port(xprt); 174392200412SChuck Lever } 174492200412SChuck Lever 17454dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17464dda9c8aSTrond Myklebust { 17474dda9c8aSTrond Myklebust if (transport->srcport == 0) 17484dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17494dda9c8aSTrond Myklebust } 17504dda9c8aSTrond Myklebust 1751826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 175267a391d7STrond Myklebust { 1753826799e6SJ. Bruce Fields int port = transport->srcport; 175467a391d7STrond Myklebust 175567a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 175667a391d7STrond Myklebust port = xs_get_random_port(); 175767a391d7STrond Myklebust return port; 175867a391d7STrond Myklebust } 175967a391d7STrond Myklebust 1760baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 176167a391d7STrond Myklebust { 1762fbfffbd5SChuck Lever if (transport->srcport != 0) 1763fbfffbd5SChuck Lever transport->srcport = 0; 176467a391d7STrond Myklebust if (!transport->xprt.resvport) 176567a391d7STrond Myklebust return 0; 176667a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 176767a391d7STrond Myklebust return xprt_max_resvport; 176867a391d7STrond Myklebust return --port; 176967a391d7STrond Myklebust } 1770beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1771a246b010SChuck Lever { 1772beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 177367a391d7STrond Myklebust int err, nloop = 0; 1774826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 177567a391d7STrond Myklebust unsigned short last; 1776a246b010SChuck Lever 17770f7a622cSChris Perl /* 17780f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17790f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17800f7a622cSChris Perl * port selection happen implicitly when the socket is used 17810f7a622cSChris Perl * (for example at connect time). 17820f7a622cSChris Perl * 17830f7a622cSChris Perl * This ensures that we can continue to establish TCP 17840f7a622cSChris Perl * connections even when all local ephemeral ports are already 17850f7a622cSChris Perl * a part of some TCP connection. This makes no difference 17860f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 17870f7a622cSChris Perl * 17880f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17890f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17900f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17910f7a622cSChris Perl */ 1792826799e6SJ. Bruce Fields if (port <= 0) 1793826799e6SJ. Bruce Fields return port; 17940f7a622cSChris Perl 1795beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1796a246b010SChuck Lever do { 1797beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1798e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1799beb59b68SPavel Emelyanov transport->xprt.addrlen); 1800a246b010SChuck Lever if (err == 0) { 1801fbfffbd5SChuck Lever transport->srcport = port; 1802d3bc9a1dSFrank van Maarseveen break; 1803a246b010SChuck Lever } 180467a391d7STrond Myklebust last = port; 1805baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 180667a391d7STrond Myklebust if (port > last) 180767a391d7STrond Myklebust nloop++; 180867a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1809beb59b68SPavel Emelyanov 18104232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1811beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1812beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1813beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1814beb59b68SPavel Emelyanov else 1815beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1816beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18177dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1818a246b010SChuck Lever return err; 1819a246b010SChuck Lever } 1820a246b010SChuck Lever 1821176e21eeSChuck Lever /* 1822176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1823176e21eeSChuck Lever */ 1824176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1825176e21eeSChuck Lever { 1826fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1827176e21eeSChuck Lever } 1828176e21eeSChuck Lever 1829176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1830176e21eeSChuck Lever { 1831176e21eeSChuck Lever } 1832a246b010SChuck Lever 1833ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1834ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1835ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1836ed07536eSPeter Zijlstra 1837176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1838176e21eeSChuck Lever { 1839176e21eeSChuck Lever struct sock *sk = sock->sk; 1840176e21eeSChuck Lever 1841176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1842176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1843176e21eeSChuck Lever } 1844176e21eeSChuck Lever 18458945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1846ed07536eSPeter Zijlstra { 1847ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18488945ee5eSChuck Lever 18498945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 18508945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1851ed07536eSPeter Zijlstra } 18528945ee5eSChuck Lever 18538945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18548945ee5eSChuck Lever { 18558945ee5eSChuck Lever struct sock *sk = sock->sk; 18568945ee5eSChuck Lever 18578945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 18588945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1859ed07536eSPeter Zijlstra } 18606bc9638aSPavel Emelyanov 18616bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18626bc9638aSPavel Emelyanov { 1863fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18641b7a1819SWeston Andros Adamson return; 18651b7a1819SWeston Andros Adamson 18664232e863SChuck Lever switch (family) { 1867176e21eeSChuck Lever case AF_LOCAL: 1868176e21eeSChuck Lever xs_reclassify_socketu(sock); 1869176e21eeSChuck Lever break; 18704232e863SChuck Lever case AF_INET: 18716bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18724232e863SChuck Lever break; 18734232e863SChuck Lever case AF_INET6: 18746bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18754232e863SChuck Lever break; 18764232e863SChuck Lever } 18776bc9638aSPavel Emelyanov } 1878ed07536eSPeter Zijlstra #else 18796bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18806bc9638aSPavel Emelyanov { 18816bc9638aSPavel Emelyanov } 1882ed07536eSPeter Zijlstra #endif 1883ed07536eSPeter Zijlstra 188493dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 188593dc41bdSNeilBrown { 188693dc41bdSNeilBrown } 188793dc41bdSNeilBrown 18886bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18894dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18904dda9c8aSTrond Myklebust int protocol, bool reuseport) 189122f79326SPavel Emelyanov { 1892a73881c9STrond Myklebust struct file *filp; 189322f79326SPavel Emelyanov struct socket *sock; 189422f79326SPavel Emelyanov int err; 189522f79326SPavel Emelyanov 18966bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 189722f79326SPavel Emelyanov if (err < 0) { 189822f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 189922f79326SPavel Emelyanov protocol, -err); 190022f79326SPavel Emelyanov goto out; 190122f79326SPavel Emelyanov } 19026bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 190322f79326SPavel Emelyanov 19044dda9c8aSTrond Myklebust if (reuseport) 19054dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19064dda9c8aSTrond Myklebust 19074cea288aSBen Hutchings err = xs_bind(transport, sock); 19084cea288aSBen Hutchings if (err) { 190922f79326SPavel Emelyanov sock_release(sock); 191022f79326SPavel Emelyanov goto out; 191122f79326SPavel Emelyanov } 191222f79326SPavel Emelyanov 1913a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1914a73881c9STrond Myklebust if (IS_ERR(filp)) 1915a73881c9STrond Myklebust return ERR_CAST(filp); 1916a73881c9STrond Myklebust transport->file = filp; 1917a73881c9STrond Myklebust 191822f79326SPavel Emelyanov return sock; 191922f79326SPavel Emelyanov out: 192022f79326SPavel Emelyanov return ERR_PTR(err); 192122f79326SPavel Emelyanov } 192222f79326SPavel Emelyanov 1923176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1924176e21eeSChuck Lever struct socket *sock) 1925176e21eeSChuck Lever { 1926176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1927176e21eeSChuck Lever xprt); 1928176e21eeSChuck Lever 1929176e21eeSChuck Lever if (!transport->inet) { 1930176e21eeSChuck Lever struct sock *sk = sock->sk; 1931176e21eeSChuck Lever 1932176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1933176e21eeSChuck Lever 1934176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1935176e21eeSChuck Lever 1936176e21eeSChuck Lever sk->sk_user_data = xprt; 1937a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1938176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1939b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 19402118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1941176e21eeSChuck Lever 1942176e21eeSChuck Lever xprt_clear_connected(xprt); 1943176e21eeSChuck Lever 1944176e21eeSChuck Lever /* Reset to new socket */ 1945176e21eeSChuck Lever transport->sock = sock; 1946176e21eeSChuck Lever transport->inet = sk; 1947176e21eeSChuck Lever 1948176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1949176e21eeSChuck Lever } 1950176e21eeSChuck Lever 1951ae053551STrond Myklebust xs_stream_start_connect(transport); 19526c7a64e5STrond Myklebust 1953176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1954176e21eeSChuck Lever } 1955176e21eeSChuck Lever 1956176e21eeSChuck Lever /** 1957176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1958176e21eeSChuck Lever * @transport: socket transport to connect 1959176e21eeSChuck Lever */ 1960dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1961176e21eeSChuck Lever { 1962176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1963a73881c9STrond Myklebust struct file *filp; 1964176e21eeSChuck Lever struct socket *sock; 1965176e21eeSChuck Lever int status = -EIO; 1966176e21eeSChuck Lever 1967176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1968176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1969176e21eeSChuck Lever if (status < 0) { 1970176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1971176e21eeSChuck Lever "transport socket (%d).\n", -status); 1972176e21eeSChuck Lever goto out; 1973176e21eeSChuck Lever } 1974d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1975176e21eeSChuck Lever 1976a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1977a73881c9STrond Myklebust if (IS_ERR(filp)) { 1978a73881c9STrond Myklebust status = PTR_ERR(filp); 1979a73881c9STrond Myklebust goto out; 1980a73881c9STrond Myklebust } 1981a73881c9STrond Myklebust transport->file = filp; 1982a73881c9STrond Myklebust 1983176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1984176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1985176e21eeSChuck Lever 1986176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 198740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1988176e21eeSChuck Lever switch (status) { 1989176e21eeSChuck Lever case 0: 1990176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1991176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19923968a8a5SChuck Lever xprt->stat.connect_count++; 19933968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 19943968a8a5SChuck Lever xprt->stat.connect_start; 1995176e21eeSChuck Lever xprt_set_connected(xprt); 19963601c4a9STrond Myklebust case -ENOBUFS: 1997176e21eeSChuck Lever break; 1998176e21eeSChuck Lever case -ENOENT: 1999176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2000176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2001176e21eeSChuck Lever break; 20024a20a988STrond Myklebust case -ECONNREFUSED: 20034a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20044a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20054a20a988STrond Myklebust break; 2006176e21eeSChuck Lever default: 2007176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2008176e21eeSChuck Lever __func__, -status, 2009176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2010176e21eeSChuck Lever } 2011176e21eeSChuck Lever 2012176e21eeSChuck Lever out: 2013176e21eeSChuck Lever xprt_clear_connecting(xprt); 2014176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2015dc107402SJ. Bruce Fields return status; 2016dc107402SJ. Bruce Fields } 2017dc107402SJ. Bruce Fields 2018b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2019dc107402SJ. Bruce Fields { 2020dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2021dc107402SJ. Bruce Fields int ret; 2022dc107402SJ. Bruce Fields 2023dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2024dc107402SJ. Bruce Fields /* 2025dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2026dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2027dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2028dc107402SJ. Bruce Fields * 2029dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2030dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2031dc107402SJ. Bruce Fields * connect. 2032dc107402SJ. Bruce Fields */ 20335ad64b36STrond Myklebust task->tk_rpc_status = -ENOTCONN; 2034dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2035dc107402SJ. Bruce Fields return; 2036dc107402SJ. Bruce Fields } 2037dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2038dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2039dc107402SJ. Bruce Fields msleep_interruptible(15000); 2040176e21eeSChuck Lever } 2041176e21eeSChuck Lever 20423c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2043d6e971d8SJeff Layton /* 2044d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2045d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2046d6e971d8SJeff Layton * races with xs_reset_transport. 2047d6e971d8SJeff Layton */ 2048a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2049a564b8f0SMel Gorman { 2050a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2051a564b8f0SMel Gorman xprt); 2052a564b8f0SMel Gorman 2053d6e971d8SJeff Layton /* 2054d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2055d6e971d8SJeff Layton * reconnecting process will get it for us. 2056d6e971d8SJeff Layton */ 2057d6e971d8SJeff Layton if (!transport->inet) 2058d6e971d8SJeff Layton return; 20598e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2060a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2061a564b8f0SMel Gorman } 2062a564b8f0SMel Gorman 2063a564b8f0SMel Gorman /** 2064d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2065a564b8f0SMel Gorman * @xprt: transport to tag 2066a564b8f0SMel Gorman * 20678e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20688e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2069a564b8f0SMel Gorman */ 2070d67fa4d8SJeff Layton static int 2071d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2072a564b8f0SMel Gorman { 2073d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2074a564b8f0SMel Gorman 2075d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2076d6e971d8SJeff Layton return 0; 2077d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2078d6e971d8SJeff Layton return -ERESTARTSYS; 2079d6e971d8SJeff Layton if (xs->inet) 2080d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2081d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 20828e228133SJeff Layton return 0; 2083a564b8f0SMel Gorman } 2084a564b8f0SMel Gorman 20858e228133SJeff Layton /** 2086d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20878e228133SJeff Layton * @xprt: transport to tag 20888e228133SJeff Layton * 20898e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 20908e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 20918e228133SJeff Layton */ 2092d67fa4d8SJeff Layton static void 2093d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 20948e228133SJeff Layton { 2095d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 20968e228133SJeff Layton 2097d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2098d6e971d8SJeff Layton return; 2099d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2100d6e971d8SJeff Layton return; 2101d6e971d8SJeff Layton if (xs->inet) 2102d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2103d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2104a564b8f0SMel Gorman } 2105a564b8f0SMel Gorman #else 2106a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2107a564b8f0SMel Gorman { 2108a564b8f0SMel Gorman } 2109d67fa4d8SJeff Layton 2110d67fa4d8SJeff Layton static int 2111d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2112d67fa4d8SJeff Layton { 2113d67fa4d8SJeff Layton return -EINVAL; 2114d67fa4d8SJeff Layton } 2115d67fa4d8SJeff Layton 2116d67fa4d8SJeff Layton static void 2117d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2118d67fa4d8SJeff Layton { 2119d67fa4d8SJeff Layton } 2120a564b8f0SMel Gorman #endif 2121a564b8f0SMel Gorman 212216be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2123a246b010SChuck Lever { 212416be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2125edb267a6SChuck Lever 2126ee0ac0c2SChuck Lever if (!transport->inet) { 2127b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2128b0d93ad5SChuck Lever 2129b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2130b0d93ad5SChuck Lever 21312a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21322a9e1cfaSTrond Myklebust 2133b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2134f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2135b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2136b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2137b0d93ad5SChuck Lever 2138b0d93ad5SChuck Lever xprt_set_connected(xprt); 2139b0d93ad5SChuck Lever 2140b0d93ad5SChuck Lever /* Reset to new socket */ 2141ee0ac0c2SChuck Lever transport->sock = sock; 2142ee0ac0c2SChuck Lever transport->inet = sk; 2143b0d93ad5SChuck Lever 2144a564b8f0SMel Gorman xs_set_memalloc(xprt); 2145a564b8f0SMel Gorman 2146b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2147b0d93ad5SChuck Lever } 2148470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 214902910177STrond Myklebust 215002910177STrond Myklebust xprt->stat.connect_start = jiffies; 215116be2d20SChuck Lever } 215216be2d20SChuck Lever 21538c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2154a246b010SChuck Lever { 2155a246b010SChuck Lever struct sock_xprt *transport = 2156a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2157a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2158d099b8afSColin Ian King struct socket *sock; 2159b65c0310SPavel Emelyanov int status = -EIO; 2160a246b010SChuck Lever 21618c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21624dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21634dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2164b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2165a246b010SChuck Lever goto out; 216668e220bdSChuck Lever 2167c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2168c740eff8SChuck Lever "%s (port %s)\n", xprt, 2169c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2170c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2171c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 217268e220bdSChuck Lever 217368e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 217440b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2175a246b010SChuck Lever status = 0; 2176b0d93ad5SChuck Lever out: 2177b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2178cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 21797d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2180b0d93ad5SChuck Lever } 2181b0d93ad5SChuck Lever 21824876cc77STrond Myklebust /** 21834876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21844876cc77STrond Myklebust * @xprt: transport 21854876cc77STrond Myklebust * 21864876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 21874876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 21884876cc77STrond Myklebust */ 21894876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 21904876cc77STrond Myklebust { 21914876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21924876cc77STrond Myklebust struct socket *sock = transport->sock; 21939b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 21944876cc77STrond Myklebust 21954876cc77STrond Myklebust if (sock == NULL) 21964876cc77STrond Myklebust return; 21979b30889cSTrond Myklebust switch (skst) { 21989b30889cSTrond Myklebust default: 21994876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22004876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22019b30889cSTrond Myklebust break; 22029b30889cSTrond Myklebust case TCP_CLOSE: 22039b30889cSTrond Myklebust case TCP_TIME_WAIT: 22044876cc77STrond Myklebust xs_reset_transport(transport); 22054876cc77STrond Myklebust } 22069b30889cSTrond Myklebust } 22074876cc77STrond Myklebust 22088d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22098d1b8c62STrond Myklebust struct socket *sock) 2210b0d93ad5SChuck Lever { 221116be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22127196dbb0STrond Myklebust unsigned int keepidle; 22137196dbb0STrond Myklebust unsigned int keepcnt; 22147f260e85STrond Myklebust unsigned int opt_on = 1; 2215775f06abSTrond Myklebust unsigned int timeo; 22167f260e85STrond Myklebust 22177196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22187196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22197196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22207196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22217196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22227196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22237196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22247f260e85STrond Myklebust 22257f260e85STrond Myklebust /* TCP Keepalive options */ 22267f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22277f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22287f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22297f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22307f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22317f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22327f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22337f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2234b0d93ad5SChuck Lever 22358d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 22368d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 22378d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 22388d1b8c62STrond Myklebust } 22398d1b8c62STrond Myklebust 22407196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22417196dbb0STrond Myklebust unsigned long connect_timeout, 22427196dbb0STrond Myklebust unsigned long reconnect_timeout) 22437196dbb0STrond Myklebust { 22447196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22457196dbb0STrond Myklebust struct rpc_timeout to; 22467196dbb0STrond Myklebust unsigned long initval; 22477196dbb0STrond Myklebust 22487196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22497196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22507196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22517196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 22527196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22537196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 22547196dbb0STrond Myklebust /* Arbitrary lower limit */ 22557196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 22567196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 22577196dbb0STrond Myklebust to.to_initval = initval; 22587196dbb0STrond Myklebust to.to_maxval = initval; 22597196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 22607196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 22617196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 22627196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 22637196dbb0STrond Myklebust } 22647196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22657196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22667196dbb0STrond Myklebust } 22677196dbb0STrond Myklebust 22688d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 22698d1b8c62STrond Myklebust { 22708d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22718d1b8c62STrond Myklebust int ret = -ENOTCONN; 22728d1b8c62STrond Myklebust 22738d1b8c62STrond Myklebust if (!transport->inet) { 22748d1b8c62STrond Myklebust struct sock *sk = sock->sk; 22758d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 22768d1b8c62STrond Myklebust 2277d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2278d88e4d82SNeilBrown * connections such as NFS mounts. 2279d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2280d88e4d82SNeilBrown * Individual applications, which have specific 2281d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2282d88e4d82SNeilBrown * MAY override this as appropriate. 2283d88e4d82SNeilBrown */ 2284d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2285d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2286d88e4d82SNeilBrown 22878d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2288775f06abSTrond Myklebust 2289b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2290b0d93ad5SChuck Lever 22912a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 22922a9e1cfaSTrond Myklebust 2293b0d93ad5SChuck Lever sk->sk_user_data = xprt; 22945157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2295b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2296b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2297b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 22982118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 22993167e12cSChuck Lever 23003167e12cSChuck Lever /* socket options */ 23013167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 23023167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2303b0d93ad5SChuck Lever 2304b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2305b0d93ad5SChuck Lever 2306b0d93ad5SChuck Lever /* Reset to new socket */ 2307ee0ac0c2SChuck Lever transport->sock = sock; 2308ee0ac0c2SChuck Lever transport->inet = sk; 2309b0d93ad5SChuck Lever 2310b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2311b0d93ad5SChuck Lever } 2312b0d93ad5SChuck Lever 231301d37c42STrond Myklebust if (!xprt_bound(xprt)) 2314fe19a96bSTrond Myklebust goto out; 231501d37c42STrond Myklebust 2316a564b8f0SMel Gorman xs_set_memalloc(xprt); 2317a564b8f0SMel Gorman 2318ae053551STrond Myklebust xs_stream_start_connect(transport); 2319e1806c7bSTrond Myklebust 2320b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23210fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2322fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2323fe19a96bSTrond Myklebust switch (ret) { 2324fe19a96bSTrond Myklebust case 0: 23254dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2326e9d47639SGustavo A. R. Silva /* fall through */ 2327fe19a96bSTrond Myklebust case -EINPROGRESS: 2328fe19a96bSTrond Myklebust /* SYN_SENT! */ 2329fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2330fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 23311f4c17a0STrond Myklebust break; 23321f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 23331f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 23341f4c17a0STrond Myklebust transport->srcport = 0; 2335fe19a96bSTrond Myklebust } 2336fe19a96bSTrond Myklebust out: 2337fe19a96bSTrond Myklebust return ret; 233816be2d20SChuck Lever } 233916be2d20SChuck Lever 234016be2d20SChuck Lever /** 2341b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2342acf0a39fSChuck Lever * @work: queued work item 234316be2d20SChuck Lever * 234416be2d20SChuck Lever * Invoked by a work queue tasklet. 234516be2d20SChuck Lever */ 2346cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 234716be2d20SChuck Lever { 2348cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2349cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 235016be2d20SChuck Lever struct socket *sock = transport->sock; 2351a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2352b61d59ffSTrond Myklebust int status = -EIO; 235316be2d20SChuck Lever 235416be2d20SChuck Lever if (!sock) { 2355cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 23564dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 23574dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2358b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2359b61d59ffSTrond Myklebust status = PTR_ERR(sock); 236016be2d20SChuck Lever goto out; 236116be2d20SChuck Lever } 23627d1e8255STrond Myklebust } 23637d1e8255STrond Myklebust 2364c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2365c740eff8SChuck Lever "%s (port %s)\n", xprt, 2366c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2367c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2368c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 236916be2d20SChuck Lever 237016be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 237140b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2372a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 237346121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 237446121cf7SChuck Lever sock->sk->sk_state); 2375a246b010SChuck Lever switch (status) { 2376f75e6745STrond Myklebust default: 2377f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2378f75e6745STrond Myklebust __func__, status); 2379e9d47639SGustavo A. R. Silva /* fall through */ 2380f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2381f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2382f75e6745STrond Myklebust * and retry 2383f75e6745STrond Myklebust */ 2384a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 238588b5ed73STrond Myklebust break; 23862a491991STrond Myklebust case 0: 2387a246b010SChuck Lever case -EINPROGRESS: 2388a246b010SChuck Lever case -EALREADY: 2389718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 23907d1e8255STrond Myklebust return; 23919fcfe0c8STrond Myklebust case -EINVAL: 23929fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 23939fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 23949fcfe0c8STrond Myklebust */ 23953ed5e2a2STrond Myklebust case -ECONNREFUSED: 23963ed5e2a2STrond Myklebust case -ECONNRESET: 2397eb5b46faSTrond Myklebust case -ENETDOWN: 23983ed5e2a2STrond Myklebust case -ENETUNREACH: 23994ba161a7STrond Myklebust case -EHOSTUNREACH: 24003913c78cSTrond Myklebust case -EADDRINUSE: 24013601c4a9STrond Myklebust case -ENOBUFS: 24026ea44adcSNeilBrown /* 24036ea44adcSNeilBrown * xs_tcp_force_close() wakes tasks with -EIO. 24046ea44adcSNeilBrown * We need to wake them first to ensure the 24056ea44adcSNeilBrown * correct error code. 24066ea44adcSNeilBrown */ 24076ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24084efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 24099fcfe0c8STrond Myklebust goto out; 24108a2cec29STrond Myklebust } 24112a491991STrond Myklebust status = -EAGAIN; 2412a246b010SChuck Lever out: 24132226feb6SChuck Lever xprt_clear_connecting(xprt); 2414cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 24157d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2416a246b010SChuck Lever } 2417a246b010SChuck Lever 241802910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 241902910177STrond Myklebust { 242002910177STrond Myklebust unsigned long start, now = jiffies; 242102910177STrond Myklebust 242202910177STrond Myklebust start = xprt->stat.connect_start + xprt->reestablish_timeout; 242302910177STrond Myklebust if (time_after(start, now)) 242402910177STrond Myklebust return start - now; 242502910177STrond Myklebust return 0; 242602910177STrond Myklebust } 242702910177STrond Myklebust 24283851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt) 24293851f1cdSTrond Myklebust { 24303851f1cdSTrond Myklebust xprt->reestablish_timeout <<= 1; 24313851f1cdSTrond Myklebust if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 24323851f1cdSTrond Myklebust xprt->reestablish_timeout = xprt->max_reconnect_timeout; 24333851f1cdSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 24343851f1cdSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 24353851f1cdSTrond Myklebust } 24363851f1cdSTrond Myklebust 243768e220bdSChuck Lever /** 24389903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 24391b092092STrond Myklebust * @xprt: pointer to transport structure 24409903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 24419903cd1cSChuck Lever * 24429903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 244303bf4b70SChuck Lever * 244403bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 244503bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 244603bf4b70SChuck Lever * socket on a privileged port. 244703bf4b70SChuck Lever * 244803bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 244903bf4b70SChuck Lever * retry floods (hard mounts). 24509903cd1cSChuck Lever */ 24511b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2452a246b010SChuck Lever { 2453ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 245402910177STrond Myklebust unsigned long delay = 0; 2455a246b010SChuck Lever 2456718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2457718ba5b8STrond Myklebust 245899b1a4c3STrond Myklebust if (transport->sock != NULL) { 245946121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 246046121cf7SChuck Lever "seconds\n", 246103bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 246299b1a4c3STrond Myklebust 246399b1a4c3STrond Myklebust /* Start by resetting any existing state */ 246499b1a4c3STrond Myklebust xs_reset_transport(transport); 246599b1a4c3STrond Myklebust 246602910177STrond Myklebust delay = xs_reconnect_delay(xprt); 24673851f1cdSTrond Myklebust xs_reconnect_backoff(xprt); 246802910177STrond Myklebust 246902910177STrond Myklebust } else 24709903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 247102910177STrond Myklebust 247240a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 247302910177STrond Myklebust &transport->connect_worker, 247402910177STrond Myklebust delay); 2475a246b010SChuck Lever } 2476a246b010SChuck Lever 2477*4f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 2478*4f8943f8STrond Myklebust { 2479*4f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 2480*4f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 2481*4f8943f8STrond Myklebust } 2482*4f8943f8STrond Myklebust 2483*4f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 2484*4f8943f8STrond Myklebust { 2485*4f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 2486*4f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 2487*4f8943f8STrond Myklebust } 2488*4f8943f8STrond Myklebust 2489*4f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 2490*4f8943f8STrond Myklebust { 2491*4f8943f8STrond Myklebust int sockerr; 2492*4f8943f8STrond Myklebust int sockerr_len = sizeof(sockerr); 2493*4f8943f8STrond Myklebust 2494*4f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 2495*4f8943f8STrond Myklebust return; 2496*4f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 2497*4f8943f8STrond Myklebust if (transport->sock == NULL) 2498*4f8943f8STrond Myklebust goto out; 2499*4f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 2500*4f8943f8STrond Myklebust goto out; 2501*4f8943f8STrond Myklebust if (kernel_getsockopt(transport->sock, SOL_SOCKET, SO_ERROR, 2502*4f8943f8STrond Myklebust (char *)&sockerr, &sockerr_len) != 0) 2503*4f8943f8STrond Myklebust goto out; 2504*4f8943f8STrond Myklebust if (sockerr < 0) 2505*4f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 2506*4f8943f8STrond Myklebust out: 2507*4f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 2508*4f8943f8STrond Myklebust } 2509*4f8943f8STrond Myklebust 2510*4f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 2511*4f8943f8STrond Myklebust { 2512*4f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 2513*4f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 2514*4f8943f8STrond Myklebust } 2515*4f8943f8STrond Myklebust 2516*4f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 2517*4f8943f8STrond Myklebust { 2518*4f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 2519*4f8943f8STrond Myklebust struct sock_xprt, error_worker); 2520*4f8943f8STrond Myklebust 2521*4f8943f8STrond Myklebust xs_wake_disconnect(transport); 2522*4f8943f8STrond Myklebust xs_wake_write(transport); 2523*4f8943f8STrond Myklebust xs_wake_error(transport); 2524*4f8943f8STrond Myklebust xs_wake_pending(transport); 2525*4f8943f8STrond Myklebust } 2526*4f8943f8STrond Myklebust 2527262ca07dSChuck Lever /** 2528176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2529176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2530176e21eeSChuck Lever * @seq: output file 2531176e21eeSChuck Lever * 2532176e21eeSChuck Lever */ 2533176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2534176e21eeSChuck Lever { 2535176e21eeSChuck Lever long idle_time = 0; 2536176e21eeSChuck Lever 2537176e21eeSChuck Lever if (xprt_connected(xprt)) 2538176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2539176e21eeSChuck Lever 2540176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 254115a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2542176e21eeSChuck Lever xprt->stat.bind_count, 2543176e21eeSChuck Lever xprt->stat.connect_count, 25448440a886SChuck Lever xprt->stat.connect_time / HZ, 2545176e21eeSChuck Lever idle_time, 2546176e21eeSChuck Lever xprt->stat.sends, 2547176e21eeSChuck Lever xprt->stat.recvs, 2548176e21eeSChuck Lever xprt->stat.bad_xids, 2549176e21eeSChuck Lever xprt->stat.req_u, 255015a45206SAndy Adamson xprt->stat.bklog_u, 255115a45206SAndy Adamson xprt->stat.max_slots, 255215a45206SAndy Adamson xprt->stat.sending_u, 255315a45206SAndy Adamson xprt->stat.pending_u); 2554176e21eeSChuck Lever } 2555176e21eeSChuck Lever 2556176e21eeSChuck Lever /** 2557262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2558262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2559262ca07dSChuck Lever * @seq: output file 2560262ca07dSChuck Lever * 2561262ca07dSChuck Lever */ 2562262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2563262ca07dSChuck Lever { 2564c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2565c8475461SChuck Lever 256615a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 256715a45206SAndy Adamson "%lu %llu %llu\n", 2568fbfffbd5SChuck Lever transport->srcport, 2569262ca07dSChuck Lever xprt->stat.bind_count, 2570262ca07dSChuck Lever xprt->stat.sends, 2571262ca07dSChuck Lever xprt->stat.recvs, 2572262ca07dSChuck Lever xprt->stat.bad_xids, 2573262ca07dSChuck Lever xprt->stat.req_u, 257415a45206SAndy Adamson xprt->stat.bklog_u, 257515a45206SAndy Adamson xprt->stat.max_slots, 257615a45206SAndy Adamson xprt->stat.sending_u, 257715a45206SAndy Adamson xprt->stat.pending_u); 2578262ca07dSChuck Lever } 2579262ca07dSChuck Lever 2580262ca07dSChuck Lever /** 2581262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2582262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2583262ca07dSChuck Lever * @seq: output file 2584262ca07dSChuck Lever * 2585262ca07dSChuck Lever */ 2586262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2587262ca07dSChuck Lever { 2588c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2589262ca07dSChuck Lever long idle_time = 0; 2590262ca07dSChuck Lever 2591262ca07dSChuck Lever if (xprt_connected(xprt)) 2592262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2593262ca07dSChuck Lever 259415a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 259515a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2596fbfffbd5SChuck Lever transport->srcport, 2597262ca07dSChuck Lever xprt->stat.bind_count, 2598262ca07dSChuck Lever xprt->stat.connect_count, 25998440a886SChuck Lever xprt->stat.connect_time / HZ, 2600262ca07dSChuck Lever idle_time, 2601262ca07dSChuck Lever xprt->stat.sends, 2602262ca07dSChuck Lever xprt->stat.recvs, 2603262ca07dSChuck Lever xprt->stat.bad_xids, 2604262ca07dSChuck Lever xprt->stat.req_u, 260515a45206SAndy Adamson xprt->stat.bklog_u, 260615a45206SAndy Adamson xprt->stat.max_slots, 260715a45206SAndy Adamson xprt->stat.sending_u, 260815a45206SAndy Adamson xprt->stat.pending_u); 2609262ca07dSChuck Lever } 2610262ca07dSChuck Lever 26114cfc7e60SRahul Iyer /* 26124cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 26134cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 26144cfc7e60SRahul Iyer * to use the server side send routines. 26154cfc7e60SRahul Iyer */ 26165fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 26174cfc7e60SRahul Iyer { 26185fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 26195fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 26204cfc7e60SRahul Iyer struct page *page; 26214cfc7e60SRahul Iyer struct rpc_buffer *buf; 26224cfc7e60SRahul Iyer 26235fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 26245fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 26255fe6eaa1SChuck Lever size); 26265fe6eaa1SChuck Lever return -EINVAL; 26275fe6eaa1SChuck Lever } 26284cfc7e60SRahul Iyer 2629b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 26304cfc7e60SRahul Iyer if (!page) 26315fe6eaa1SChuck Lever return -ENOMEM; 26324cfc7e60SRahul Iyer 26334cfc7e60SRahul Iyer buf = page_address(page); 26344cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 26354cfc7e60SRahul Iyer 26365fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 263718e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 26385fe6eaa1SChuck Lever return 0; 26394cfc7e60SRahul Iyer } 26404cfc7e60SRahul Iyer 26414cfc7e60SRahul Iyer /* 26424cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 26434cfc7e60SRahul Iyer */ 26443435c74aSChuck Lever static void bc_free(struct rpc_task *task) 26454cfc7e60SRahul Iyer { 26463435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 26474cfc7e60SRahul Iyer struct rpc_buffer *buf; 26484cfc7e60SRahul Iyer 26494cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 26504cfc7e60SRahul Iyer free_page((unsigned long)buf); 26514cfc7e60SRahul Iyer } 26524cfc7e60SRahul Iyer 26534cfc7e60SRahul Iyer /* 26544cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 26554cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 26564cfc7e60SRahul Iyer */ 26574cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 26584cfc7e60SRahul Iyer { 26594cfc7e60SRahul Iyer int len; 26604cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 26614cfc7e60SRahul Iyer struct sock_xprt *transport = 2662067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 26634cfc7e60SRahul Iyer unsigned long headoff; 26644cfc7e60SRahul Iyer unsigned long tailoff; 2665067fb11bSChuck Lever struct page *tailpage; 2666067fb11bSChuck Lever struct msghdr msg = { 2667067fb11bSChuck Lever .msg_flags = MSG_MORE 2668067fb11bSChuck Lever }; 2669067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2670067fb11bSChuck Lever (u32)xbufp->len); 2671067fb11bSChuck Lever struct kvec iov = { 2672067fb11bSChuck Lever .iov_base = &marker, 2673067fb11bSChuck Lever .iov_len = sizeof(marker), 2674067fb11bSChuck Lever }; 26754cfc7e60SRahul Iyer 2676067fb11bSChuck Lever len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len); 2677067fb11bSChuck Lever if (len != iov.iov_len) 2678067fb11bSChuck Lever return -EAGAIN; 26794cfc7e60SRahul Iyer 2680067fb11bSChuck Lever tailpage = NULL; 2681067fb11bSChuck Lever if (xbufp->tail[0].iov_len) 2682067fb11bSChuck Lever tailpage = virt_to_page(xbufp->tail[0].iov_base); 26834cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26844cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 2685067fb11bSChuck Lever len = svc_send_common(transport->sock, xbufp, 26864cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 2687067fb11bSChuck Lever tailpage, tailoff); 2688067fb11bSChuck Lever if (len != xbufp->len) 2689067fb11bSChuck Lever return -EAGAIN; 26904cfc7e60SRahul Iyer return len; 26914cfc7e60SRahul Iyer } 26924cfc7e60SRahul Iyer 26934cfc7e60SRahul Iyer /* 26944cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 26954cfc7e60SRahul Iyer */ 2696adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 26974cfc7e60SRahul Iyer { 26984cfc7e60SRahul Iyer struct svc_xprt *xprt; 26997fc56136SAndrzej Hajda int len; 27004cfc7e60SRahul Iyer 27014cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 27024cfc7e60SRahul Iyer /* 27034cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 27044cfc7e60SRahul Iyer */ 27054cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 27064cfc7e60SRahul Iyer 27074cfc7e60SRahul Iyer /* 27084cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 27094cfc7e60SRahul Iyer * with the fore channel 27104cfc7e60SRahul Iyer */ 2711c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 27124cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 27134cfc7e60SRahul Iyer len = -ENOTCONN; 27144cfc7e60SRahul Iyer else 27154cfc7e60SRahul Iyer len = bc_sendto(req); 27164cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 27174cfc7e60SRahul Iyer 27184cfc7e60SRahul Iyer if (len > 0) 27194cfc7e60SRahul Iyer len = 0; 27204cfc7e60SRahul Iyer 27214cfc7e60SRahul Iyer return len; 27224cfc7e60SRahul Iyer } 27234cfc7e60SRahul Iyer 27244cfc7e60SRahul Iyer /* 27254cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 27264cfc7e60SRahul Iyer */ 27274cfc7e60SRahul Iyer 27284cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 27294cfc7e60SRahul Iyer { 27304cfc7e60SRahul Iyer } 27314cfc7e60SRahul Iyer 27324cfc7e60SRahul Iyer /* 27334cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 27344cfc7e60SRahul Iyer * initiated, we do nothing 27354cfc7e60SRahul Iyer */ 27364cfc7e60SRahul Iyer 27374cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 27384cfc7e60SRahul Iyer { 273947f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 274047f72efaSKinglong Mee 274147f72efaSKinglong Mee xs_xprt_free(xprt); 274247f72efaSKinglong Mee module_put(THIS_MODULE); 27434cfc7e60SRahul Iyer } 27444cfc7e60SRahul Iyer 2745d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2746176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 27474cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2748f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2749a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2750176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2751176e21eeSChuck Lever .set_port = xs_local_set_port, 2752dc107402SJ. Bruce Fields .connect = xs_local_connect, 2753176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2754176e21eeSChuck Lever .buf_free = rpc_free, 2755550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 2756176e21eeSChuck Lever .send_request = xs_local_send_request, 27578ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2758176e21eeSChuck Lever .close = xs_close, 2759a1311d87STrond Myklebust .destroy = xs_destroy, 2760176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2761d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2762d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2763176e21eeSChuck Lever }; 2764176e21eeSChuck Lever 2765d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 276643118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 276712a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 276849e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2769f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2770a9cde23aSChuck Lever .free_slot = xprt_free_slot, 277145160d62SChuck Lever .rpcbind = rpcb_getport_async, 277292200412SChuck Lever .set_port = xs_set_port, 27739903cd1cSChuck Lever .connect = xs_connect, 277402107148SChuck Lever .buf_alloc = rpc_malloc, 277502107148SChuck Lever .buf_free = rpc_free, 2776262965f5SChuck Lever .send_request = xs_udp_send_request, 27778ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 277846c0ee8bSChuck Lever .timer = xs_udp_timer, 2779a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2780262965f5SChuck Lever .close = xs_close, 2781262965f5SChuck Lever .destroy = xs_destroy, 2782262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2783d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2784d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27854a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2786262965f5SChuck Lever }; 2787262965f5SChuck Lever 2788d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 278912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 27904cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 279136bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 2792a9cde23aSChuck Lever .free_slot = xprt_free_slot, 279345160d62SChuck Lever .rpcbind = rpcb_getport_async, 279492200412SChuck Lever .set_port = xs_set_port, 27950b9e7943STrond Myklebust .connect = xs_connect, 279602107148SChuck Lever .buf_alloc = rpc_malloc, 279702107148SChuck Lever .buf_free = rpc_free, 2798277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 2799262965f5SChuck Lever .send_request = xs_tcp_send_request, 28008ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2801c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 28029903cd1cSChuck Lever .destroy = xs_destroy, 28037196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2804262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2805d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2806d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28074a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 280842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 280942e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 28106b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 281142e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 281242e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 281342e5c3e2SChuck Lever #endif 2814a246b010SChuck Lever }; 2815a246b010SChuck Lever 28164cfc7e60SRahul Iyer /* 28174cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 28184cfc7e60SRahul Iyer */ 28194cfc7e60SRahul Iyer 2820d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 28214cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 28224cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 282384e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2824a9cde23aSChuck Lever .free_slot = xprt_free_slot, 28254cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 28264cfc7e60SRahul Iyer .buf_free = bc_free, 28274cfc7e60SRahul Iyer .send_request = bc_send_request, 28288ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 28294cfc7e60SRahul Iyer .close = bc_close, 28304cfc7e60SRahul Iyer .destroy = bc_destroy, 28314cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2832d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2833d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28344a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 28354cfc7e60SRahul Iyer }; 28364cfc7e60SRahul Iyer 283792476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 283892476850SChuck Lever { 283992476850SChuck Lever static const struct sockaddr_in sin = { 284092476850SChuck Lever .sin_family = AF_INET, 284192476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 284292476850SChuck Lever }; 284392476850SChuck Lever static const struct sockaddr_in6 sin6 = { 284492476850SChuck Lever .sin6_family = AF_INET6, 284592476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 284692476850SChuck Lever }; 284792476850SChuck Lever 284892476850SChuck Lever switch (family) { 2849176e21eeSChuck Lever case AF_LOCAL: 2850176e21eeSChuck Lever break; 285192476850SChuck Lever case AF_INET: 285292476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 285392476850SChuck Lever break; 285492476850SChuck Lever case AF_INET6: 285592476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 285692476850SChuck Lever break; 285792476850SChuck Lever default: 285892476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 285992476850SChuck Lever return -EAFNOSUPPORT; 286092476850SChuck Lever } 286192476850SChuck Lever return 0; 286292476850SChuck Lever } 286392476850SChuck Lever 28643c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2865d9ba131dSTrond Myklebust unsigned int slot_table_size, 2866d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2867c8541ecdSChuck Lever { 2868c8541ecdSChuck Lever struct rpc_xprt *xprt; 2869ffc2e518SChuck Lever struct sock_xprt *new; 2870c8541ecdSChuck Lever 287196802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2872c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2873c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2874c8541ecdSChuck Lever } 2875c8541ecdSChuck Lever 2876d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2877d9ba131dSTrond Myklebust max_slot_table_size); 2878bd1722d4SPavel Emelyanov if (xprt == NULL) { 287946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 288046121cf7SChuck Lever "rpc_xprt\n"); 2881c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2882c8541ecdSChuck Lever } 2883c8541ecdSChuck Lever 2884bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2885edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 288696802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 288796802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2888d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2889fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 289092476850SChuck Lever else { 289192476850SChuck Lever int err; 289292476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 289392476850SChuck Lever (struct sockaddr *)&new->srcaddr); 28942aa13531SStanislav Kinsbursky if (err != 0) { 28952aa13531SStanislav Kinsbursky xprt_free(xprt); 289692476850SChuck Lever return ERR_PTR(err); 289792476850SChuck Lever } 28982aa13531SStanislav Kinsbursky } 2899c8541ecdSChuck Lever 2900c8541ecdSChuck Lever return xprt; 2901c8541ecdSChuck Lever } 2902c8541ecdSChuck Lever 2903176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2904176e21eeSChuck Lever .to_initval = 10 * HZ, 2905176e21eeSChuck Lever .to_maxval = 10 * HZ, 2906176e21eeSChuck Lever .to_retries = 2, 2907176e21eeSChuck Lever }; 2908176e21eeSChuck Lever 2909176e21eeSChuck Lever /** 2910176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2911176e21eeSChuck Lever * @args: rpc transport creation arguments 2912176e21eeSChuck Lever * 2913176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2914176e21eeSChuck Lever */ 2915176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2916176e21eeSChuck Lever { 2917176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2918176e21eeSChuck Lever struct sock_xprt *transport; 2919176e21eeSChuck Lever struct rpc_xprt *xprt; 2920176e21eeSChuck Lever struct rpc_xprt *ret; 2921176e21eeSChuck Lever 2922d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2923d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2924176e21eeSChuck Lever if (IS_ERR(xprt)) 2925176e21eeSChuck Lever return xprt; 2926176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2927176e21eeSChuck Lever 2928176e21eeSChuck Lever xprt->prot = 0; 2929176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2930176e21eeSChuck Lever 2931176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2932176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2933176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2934176e21eeSChuck Lever 2935176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2936176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2937176e21eeSChuck Lever 2938550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2939*4f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2940550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 294193dc41bdSNeilBrown 2942176e21eeSChuck Lever switch (sun->sun_family) { 2943176e21eeSChuck Lever case AF_LOCAL: 2944176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2945176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2946176e21eeSChuck Lever sun->sun_path); 2947176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2948176e21eeSChuck Lever goto out_err; 2949176e21eeSChuck Lever } 2950176e21eeSChuck Lever xprt_set_bound(xprt); 2951176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 29527073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 29537073ea87SJ. Bruce Fields if (ret) 29547073ea87SJ. Bruce Fields goto out_err; 2955176e21eeSChuck Lever break; 2956176e21eeSChuck Lever default: 2957176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2958176e21eeSChuck Lever goto out_err; 2959176e21eeSChuck Lever } 2960176e21eeSChuck Lever 2961176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2962176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2963176e21eeSChuck Lever 2964176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2965176e21eeSChuck Lever return xprt; 2966176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2967176e21eeSChuck Lever out_err: 2968315f3812SKinglong Mee xs_xprt_free(xprt); 2969176e21eeSChuck Lever return ret; 2970176e21eeSChuck Lever } 2971176e21eeSChuck Lever 29722881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 29732881ae74STrond Myklebust .to_initval = 5 * HZ, 29742881ae74STrond Myklebust .to_maxval = 30 * HZ, 29752881ae74STrond Myklebust .to_increment = 5 * HZ, 29762881ae74STrond Myklebust .to_retries = 5, 29772881ae74STrond Myklebust }; 29782881ae74STrond Myklebust 29799903cd1cSChuck Lever /** 29809903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 298196802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29829903cd1cSChuck Lever * 29839903cd1cSChuck Lever */ 2984483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2985a246b010SChuck Lever { 29868f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2987c8541ecdSChuck Lever struct rpc_xprt *xprt; 2988c8475461SChuck Lever struct sock_xprt *transport; 29890a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2990a246b010SChuck Lever 2991d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2992d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2993c8541ecdSChuck Lever if (IS_ERR(xprt)) 2994c8541ecdSChuck Lever return xprt; 2995c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2996a246b010SChuck Lever 2997ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2998a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2999a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3000a246b010SChuck Lever 300103bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 300203bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 300303bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3004a246b010SChuck Lever 3005262965f5SChuck Lever xprt->ops = &xs_udp_ops; 3006a246b010SChuck Lever 3007ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 3008a246b010SChuck Lever 3009f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 3010*4f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3011edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3012edc1b01cSTrond Myklebust 30138f9d5b1aSChuck Lever switch (addr->sa_family) { 30148f9d5b1aSChuck Lever case AF_INET: 30158f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30168f9d5b1aSChuck Lever xprt_set_bound(xprt); 30178f9d5b1aSChuck Lever 30189dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 30198f9d5b1aSChuck Lever break; 30208f9d5b1aSChuck Lever case AF_INET6: 30218f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30228f9d5b1aSChuck Lever xprt_set_bound(xprt); 30238f9d5b1aSChuck Lever 30249dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 30258f9d5b1aSChuck Lever break; 30268f9d5b1aSChuck Lever default: 30270a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30280a68b0beSJ. Bruce Fields goto out_err; 30298f9d5b1aSChuck Lever } 30308f9d5b1aSChuck Lever 3031c740eff8SChuck Lever if (xprt_bound(xprt)) 3032c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3033c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3034c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3035c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3036c740eff8SChuck Lever else 3037c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3038c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3039c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3040edb267a6SChuck Lever 3041bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3042c8541ecdSChuck Lever return xprt; 30430a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30440a68b0beSJ. Bruce Fields out_err: 3045315f3812SKinglong Mee xs_xprt_free(xprt); 30460a68b0beSJ. Bruce Fields return ret; 3047a246b010SChuck Lever } 3048a246b010SChuck Lever 30492881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 30502881ae74STrond Myklebust .to_initval = 60 * HZ, 30512881ae74STrond Myklebust .to_maxval = 60 * HZ, 30522881ae74STrond Myklebust .to_retries = 2, 30532881ae74STrond Myklebust }; 30542881ae74STrond Myklebust 30559903cd1cSChuck Lever /** 30569903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 305796802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30589903cd1cSChuck Lever * 30599903cd1cSChuck Lever */ 3060483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3061a246b010SChuck Lever { 30628f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3063c8541ecdSChuck Lever struct rpc_xprt *xprt; 3064c8475461SChuck Lever struct sock_xprt *transport; 30650a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3066b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3067b7993cebSTrond Myklebust 3068b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3069b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3070a246b010SChuck Lever 3071d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3072b7993cebSTrond Myklebust max_slot_table_size); 3073c8541ecdSChuck Lever if (IS_ERR(xprt)) 3074c8541ecdSChuck Lever return xprt; 3075c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3076a246b010SChuck Lever 3077ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3078808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3079a246b010SChuck Lever 308003bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 308103bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 308203bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3083a246b010SChuck Lever 3084262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3085ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3086a246b010SChuck Lever 30873851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 30887196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 30897196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 30903851f1cdSTrond Myklebust 3091c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 3092*4f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3093edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3094edc1b01cSTrond Myklebust 30958f9d5b1aSChuck Lever switch (addr->sa_family) { 30968f9d5b1aSChuck Lever case AF_INET: 30978f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30988f9d5b1aSChuck Lever xprt_set_bound(xprt); 30998f9d5b1aSChuck Lever 31009dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 31018f9d5b1aSChuck Lever break; 31028f9d5b1aSChuck Lever case AF_INET6: 31038f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 31048f9d5b1aSChuck Lever xprt_set_bound(xprt); 31058f9d5b1aSChuck Lever 31069dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 31078f9d5b1aSChuck Lever break; 31088f9d5b1aSChuck Lever default: 31090a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31100a68b0beSJ. Bruce Fields goto out_err; 31118f9d5b1aSChuck Lever } 31128f9d5b1aSChuck Lever 3113c740eff8SChuck Lever if (xprt_bound(xprt)) 3114c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3115c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3116c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3117c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3118c740eff8SChuck Lever else 3119c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3120c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3121c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3122c740eff8SChuck Lever 3123bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3124c8541ecdSChuck Lever return xprt; 31250a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31260a68b0beSJ. Bruce Fields out_err: 3127315f3812SKinglong Mee xs_xprt_free(xprt); 31280a68b0beSJ. Bruce Fields return ret; 3129a246b010SChuck Lever } 3130282b32e1SChuck Lever 3131f300babaSAlexandros Batsakis /** 3132f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3133f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3134f300babaSAlexandros Batsakis * 3135f300babaSAlexandros Batsakis */ 3136f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3137f300babaSAlexandros Batsakis { 3138f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3139f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3140f300babaSAlexandros Batsakis struct sock_xprt *transport; 3141f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 31420a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3143f300babaSAlexandros Batsakis 3144d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3145d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3146f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3147f300babaSAlexandros Batsakis return xprt; 3148f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3149f300babaSAlexandros Batsakis 3150f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3151f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3152f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3153f300babaSAlexandros Batsakis 3154f300babaSAlexandros Batsakis /* backchannel */ 3155f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3156f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3157f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3158f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3159f300babaSAlexandros Batsakis 3160f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3161f300babaSAlexandros Batsakis 3162f300babaSAlexandros Batsakis switch (addr->sa_family) { 3163f300babaSAlexandros Batsakis case AF_INET: 3164f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3165f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3166f300babaSAlexandros Batsakis break; 3167f300babaSAlexandros Batsakis case AF_INET6: 3168f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3169f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3170f300babaSAlexandros Batsakis break; 3171f300babaSAlexandros Batsakis default: 31720a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31730a68b0beSJ. Bruce Fields goto out_err; 3174f300babaSAlexandros Batsakis } 3175f300babaSAlexandros Batsakis 3176f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3177f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3178f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3179f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3180f300babaSAlexandros Batsakis 3181f300babaSAlexandros Batsakis /* 318299de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 318328303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 318428303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 318528303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 318699de8ea9SJ. Bruce Fields */ 318799de8ea9SJ. Bruce Fields xprt_get(xprt); 318899de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 318999de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 319099de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 319199de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 319299de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 319399de8ea9SJ. Bruce Fields 319499de8ea9SJ. Bruce Fields /* 3195f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3196f300babaSAlexandros Batsakis * the xprt status to connected 3197f300babaSAlexandros Batsakis */ 3198f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3199f300babaSAlexandros Batsakis 3200f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3201f300babaSAlexandros Batsakis return xprt; 3202642aab58SKinglong Mee 3203642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 320439a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 320599de8ea9SJ. Bruce Fields xprt_put(xprt); 32060a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 32070a68b0beSJ. Bruce Fields out_err: 3208315f3812SKinglong Mee xs_xprt_free(xprt); 32090a68b0beSJ. Bruce Fields return ret; 3210f300babaSAlexandros Batsakis } 3211f300babaSAlexandros Batsakis 3212176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3213176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3214176e21eeSChuck Lever .name = "named UNIX socket", 3215176e21eeSChuck Lever .owner = THIS_MODULE, 3216176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3217176e21eeSChuck Lever .setup = xs_setup_local, 3218176e21eeSChuck Lever }; 3219176e21eeSChuck Lever 3220bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3221bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3222bc25571eS\"Talpey, Thomas\ .name = "udp", 3223bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3224f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3225bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3226bc25571eS\"Talpey, Thomas\ }; 3227bc25571eS\"Talpey, Thomas\ 3228bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3229bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3230bc25571eS\"Talpey, Thomas\ .name = "tcp", 3231bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3232f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3233bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3234bc25571eS\"Talpey, Thomas\ }; 3235bc25571eS\"Talpey, Thomas\ 3236f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3237f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3238f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3239f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3240f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3241f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3242f300babaSAlexandros Batsakis }; 3243f300babaSAlexandros Batsakis 3244282b32e1SChuck Lever /** 3245bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3246282b32e1SChuck Lever * 3247282b32e1SChuck Lever */ 3248282b32e1SChuck Lever int init_socket_xprt(void) 3249282b32e1SChuck Lever { 32502b1bec5fSEric W. Biederman if (!sunrpc_table_header) 32510b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3252fbf76683SChuck Lever 3253176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3254bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3255bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3256f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3257bc25571eS\"Talpey, Thomas\ 3258282b32e1SChuck Lever return 0; 3259282b32e1SChuck Lever } 3260282b32e1SChuck Lever 3261282b32e1SChuck Lever /** 3262bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3263282b32e1SChuck Lever * 3264282b32e1SChuck Lever */ 3265282b32e1SChuck Lever void cleanup_socket_xprt(void) 3266282b32e1SChuck Lever { 3267fbf76683SChuck Lever if (sunrpc_table_header) { 3268fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3269fbf76683SChuck Lever sunrpc_table_header = NULL; 3270fbf76683SChuck Lever } 3271bc25571eS\"Talpey, Thomas\ 3272176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3273bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3274bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3275f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3276282b32e1SChuck Lever } 3277cbf11071STrond Myklebust 32789bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 32799bbb9e5aSRusty Russell const struct kernel_param *kp, 3280cbf11071STrond Myklebust unsigned int min, unsigned int max) 3281cbf11071STrond Myklebust { 328200cfaa94SDaniel Walter unsigned int num; 3283cbf11071STrond Myklebust int ret; 3284cbf11071STrond Myklebust 3285cbf11071STrond Myklebust if (!val) 3286cbf11071STrond Myklebust return -EINVAL; 328700cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 32889761a246SDan Carpenter if (ret) 32899761a246SDan Carpenter return ret; 32909761a246SDan Carpenter if (num < min || num > max) 3291cbf11071STrond Myklebust return -EINVAL; 3292cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3293cbf11071STrond Myklebust return 0; 3294cbf11071STrond Myklebust } 3295cbf11071STrond Myklebust 32969bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3297cbf11071STrond Myklebust { 3298cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3299cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3300cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3301cbf11071STrond Myklebust } 3302cbf11071STrond Myklebust 33039c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 33049bbb9e5aSRusty Russell .set = param_set_portnr, 33059bbb9e5aSRusty Russell .get = param_get_uint, 33069bbb9e5aSRusty Russell }; 33079bbb9e5aSRusty Russell 3308cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3309cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3310cbf11071STrond Myklebust 3311cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3312cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3313cbf11071STrond Myklebust 33149bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 33159bbb9e5aSRusty Russell const struct kernel_param *kp) 3316cbf11071STrond Myklebust { 3317cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3318cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3319cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3320cbf11071STrond Myklebust } 3321cbf11071STrond Myklebust 33229c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 33239bbb9e5aSRusty Russell .set = param_set_slot_table_size, 33249bbb9e5aSRusty Russell .get = param_get_uint, 33259bbb9e5aSRusty Russell }; 33269bbb9e5aSRusty Russell 3327cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3328cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3329cbf11071STrond Myklebust 3330d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3331d9ba131dSTrond Myklebust const struct kernel_param *kp) 3332d9ba131dSTrond Myklebust { 3333d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3334d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3335d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3336d9ba131dSTrond Myklebust } 3337d9ba131dSTrond Myklebust 33389c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3339d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3340d9ba131dSTrond Myklebust .get = param_get_uint, 3341d9ba131dSTrond Myklebust }; 3342d9ba131dSTrond Myklebust 3343d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3344d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3345d9ba131dSTrond Myklebust 3346cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3347cbf11071STrond Myklebust slot_table_size, 0644); 3348d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3349d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3350cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3351cbf11071STrond Myklebust slot_table_size, 0644); 3352