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 } 565*45835a63STrond Myklebust if (transport->recv.copied && !req->rq_private_buf.len) 566*45835a63STrond Myklebust return -ESHUTDOWN; 567277e4ab7STrond Myklebust 568277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 569277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 570c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 571*45835a63STrond Myklebust else 572*45835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 573277e4ab7STrond Myklebust 574277e4ab7STrond Myklebust return ret; 575277e4ab7STrond Myklebust } 576277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 577277e4ab7STrond Myklebust static ssize_t 578277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 579277e4ab7STrond Myklebust { 580277e4ab7STrond Myklebust return -ESHUTDOWN; 581277e4ab7STrond Myklebust } 582277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 583277e4ab7STrond Myklebust 584277e4ab7STrond Myklebust static ssize_t 585277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 586277e4ab7STrond Myklebust { 587277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 588277e4ab7STrond Myklebust struct rpc_rqst *req; 589277e4ab7STrond Myklebust ssize_t ret = 0; 590277e4ab7STrond Myklebust 591277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 592277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 593277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 594*45835a63STrond Myklebust if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { 595277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 596277e4ab7STrond Myklebust goto out; 597277e4ab7STrond Myklebust } 598277e4ab7STrond Myklebust xprt_pin_rqst(req); 599277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 600277e4ab7STrond Myklebust 601277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 602277e4ab7STrond Myklebust 603277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 604277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 605c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 606*45835a63STrond Myklebust else 607*45835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 608277e4ab7STrond Myklebust xprt_unpin_rqst(req); 609277e4ab7STrond Myklebust out: 610277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 611277e4ab7STrond Myklebust return ret; 612277e4ab7STrond Myklebust } 613277e4ab7STrond Myklebust 614277e4ab7STrond Myklebust static ssize_t 615277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 616277e4ab7STrond Myklebust { 617277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 618277e4ab7STrond Myklebust size_t want, read = 0; 619277e4ab7STrond Myklebust ssize_t ret = 0; 620277e4ab7STrond Myklebust 621277e4ab7STrond Myklebust if (transport->recv.len == 0) { 622277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 623277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 624277e4ab7STrond Myklebust transport->recv.offset); 625277e4ab7STrond Myklebust if (ret <= 0) 626277e4ab7STrond Myklebust goto out_err; 627277e4ab7STrond Myklebust transport->recv.offset = ret; 628c4433055STrond Myklebust if (transport->recv.offset != want) 629c4433055STrond Myklebust return transport->recv.offset; 630277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 631277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 632277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 633277e4ab7STrond Myklebust read = ret; 634277e4ab7STrond Myklebust } 635277e4ab7STrond Myklebust 636277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 637c4433055STrond Myklebust default: 638c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 639c4433055STrond Myklebust break; 640277e4ab7STrond Myklebust case RPC_CALL: 641277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 642277e4ab7STrond Myklebust break; 643277e4ab7STrond Myklebust case RPC_REPLY: 644277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 645277e4ab7STrond Myklebust } 646277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 647277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 648277e4ab7STrond Myklebust transport->recv.copied = -1; 649277e4ab7STrond Myklebust } 650277e4ab7STrond Myklebust if (ret < 0) 651277e4ab7STrond Myklebust goto out_err; 652277e4ab7STrond Myklebust read += ret; 653277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 654c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 655c4433055STrond Myklebust return read; 656b76a5afdSTrond Myklebust msg.msg_flags = 0; 657277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 658277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 659277e4ab7STrond Myklebust if (ret <= 0) 660277e4ab7STrond Myklebust goto out_err; 661277e4ab7STrond Myklebust transport->recv.offset += ret; 662277e4ab7STrond Myklebust read += ret; 663277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 664c4433055STrond Myklebust return read; 665277e4ab7STrond Myklebust } 666277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 667c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 668277e4ab7STrond Myklebust transport->recv.copied = 0; 669277e4ab7STrond Myklebust } 670277e4ab7STrond Myklebust transport->recv.offset = 0; 671277e4ab7STrond Myklebust transport->recv.len = 0; 672277e4ab7STrond Myklebust return read; 673277e4ab7STrond Myklebust out_err: 67479462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 675277e4ab7STrond Myklebust } 676277e4ab7STrond Myklebust 6770ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 6780ffe86f4STrond Myklebust { 679a73881c9STrond Myklebust return transport->sock->ops->poll(transport->file, transport->sock, 680a73881c9STrond Myklebust NULL); 6810ffe86f4STrond Myklebust } 6820ffe86f4STrond Myklebust 6830ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 6840ffe86f4STrond Myklebust { 6850ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 6860ffe86f4STrond Myklebust 6870ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 6880ffe86f4STrond Myklebust } 6890ffe86f4STrond Myklebust 6900ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 6910ffe86f4STrond Myklebust { 6920ffe86f4STrond Myklebust 6930ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 6940ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 6950ffe86f4STrond Myklebust return; 6960ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 6970ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 6980ffe86f4STrond Myklebust } 6990ffe86f4STrond Myklebust 700c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 701c50b8ee0STrond Myklebust { 702c50b8ee0STrond Myklebust size_t read = 0; 703c50b8ee0STrond Myklebust ssize_t ret = 0; 704c50b8ee0STrond Myklebust 705c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 706c50b8ee0STrond Myklebust if (transport->sock == NULL) 707c50b8ee0STrond Myklebust goto out; 708c50b8ee0STrond Myklebust for (;;) { 709c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 710c4433055STrond Myklebust if (ret < 0) 711c50b8ee0STrond Myklebust break; 712c50b8ee0STrond Myklebust read += ret; 713c50b8ee0STrond Myklebust cond_resched(); 714c50b8ee0STrond Myklebust } 7155f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7165f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 7175f52a9d4STrond Myklebust else 7180ffe86f4STrond Myklebust xs_poll_check_readable(transport); 719c50b8ee0STrond Myklebust out: 720c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 721c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 722c50b8ee0STrond Myklebust } 723c50b8ee0STrond Myklebust 724c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 725c50b8ee0STrond Myklebust { 726c50b8ee0STrond Myklebust struct sock_xprt *transport = 727c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 728a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 729a1231fdaSTrond Myklebust 730c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 731a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 732c50b8ee0STrond Myklebust } 733c50b8ee0STrond Myklebust 734550aebfeSTrond Myklebust static void 735550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 736550aebfeSTrond Myklebust { 737550aebfeSTrond Myklebust transport->recv.offset = 0; 738550aebfeSTrond Myklebust transport->recv.len = 0; 739550aebfeSTrond Myklebust transport->recv.copied = 0; 740550aebfeSTrond Myklebust transport->xmit.offset = 0; 741ae053551STrond Myklebust } 742ae053551STrond Myklebust 743ae053551STrond Myklebust static void 744ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 745ae053551STrond Myklebust { 746550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 747550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 748550aebfeSTrond Myklebust } 749550aebfeSTrond Myklebust 750b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 751b4b5cc85SChuck Lever 752e791f8e9STrond Myklebust static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek) 753b4b5cc85SChuck Lever { 754e791f8e9STrond Myklebust if (seek) 755e791f8e9STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 756e791f8e9STrond Myklebust return sock_sendmsg(sock, msg); 757e791f8e9STrond Myklebust } 758b4b5cc85SChuck Lever 759e791f8e9STrond Myklebust static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek) 760e791f8e9STrond Myklebust { 761e791f8e9STrond Myklebust iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len); 762e791f8e9STrond Myklebust return xs_sendmsg(sock, msg, seek); 763b4b5cc85SChuck Lever } 764b4b5cc85SChuck Lever 7650472e476STrond Myklebust static int xs_send_pagedata(struct socket *sock, struct msghdr *msg, struct xdr_buf *xdr, size_t base) 766b4b5cc85SChuck Lever { 767f279cd00SJason Baron int err; 768b4b5cc85SChuck Lever 7690472e476STrond Myklebust err = xdr_alloc_bvec(xdr, GFP_KERNEL); 7700472e476STrond Myklebust if (err < 0) 77124c5684bSTrond Myklebust return err; 7720472e476STrond Myklebust 7730472e476STrond Myklebust iov_iter_bvec(&msg->msg_iter, WRITE, xdr->bvec, 7740472e476STrond Myklebust xdr_buf_pagecount(xdr), 7750472e476STrond Myklebust xdr->page_len + xdr->page_base); 7760472e476STrond Myklebust return xs_sendmsg(sock, msg, base + xdr->page_base); 777b4b5cc85SChuck Lever } 778b4b5cc85SChuck Lever 77906b5fc3aSTrond Myklebust #define xs_record_marker_len() sizeof(rpc_fraghdr) 78006b5fc3aSTrond Myklebust 781067fb11bSChuck Lever /* Common case: 782067fb11bSChuck Lever * - stream transport 783067fb11bSChuck Lever * - sending from byte 0 of the message 784067fb11bSChuck Lever * - the message is wholly contained in @xdr's head iovec 785067fb11bSChuck Lever */ 78606b5fc3aSTrond Myklebust static int xs_send_rm_and_kvec(struct socket *sock, struct msghdr *msg, 78706b5fc3aSTrond Myklebust rpc_fraghdr marker, struct kvec *vec, size_t base) 788067fb11bSChuck Lever { 789067fb11bSChuck Lever struct kvec iov[2] = { 79006b5fc3aSTrond Myklebust [0] = { 791067fb11bSChuck Lever .iov_base = &marker, 792067fb11bSChuck Lever .iov_len = sizeof(marker) 793067fb11bSChuck Lever }, 79406b5fc3aSTrond Myklebust [1] = *vec, 795067fb11bSChuck Lever }; 79606b5fc3aSTrond Myklebust size_t len = iov[0].iov_len + iov[1].iov_len; 797067fb11bSChuck Lever 79806b5fc3aSTrond Myklebust iov_iter_kvec(&msg->msg_iter, WRITE, iov, 2, len); 79906b5fc3aSTrond Myklebust return xs_sendmsg(sock, msg, base); 800a246b010SChuck Lever } 801a246b010SChuck Lever 8029903cd1cSChuck Lever /** 8039903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 8049903cd1cSChuck Lever * @sock: socket to send on 8059903cd1cSChuck Lever * @addr: UDP only -- address of destination 8069903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 8079903cd1cSChuck Lever * @xdr: buffer containing this request 8089903cd1cSChuck Lever * @base: starting position in the buffer 80906b5fc3aSTrond Myklebust * @rm: stream record marker field 810f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 8119903cd1cSChuck Lever * 812a246b010SChuck Lever */ 81306b5fc3aSTrond 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) 814a246b010SChuck Lever { 815e791f8e9STrond Myklebust struct msghdr msg = { 816e791f8e9STrond Myklebust .msg_name = addr, 817e791f8e9STrond Myklebust .msg_namelen = addrlen, 818e791f8e9STrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE, 819e791f8e9STrond Myklebust }; 82006b5fc3aSTrond Myklebust unsigned int rmsize = rm ? sizeof(rm) : 0; 82106b5fc3aSTrond Myklebust unsigned int remainder = rmsize + xdr->len - base; 82206b5fc3aSTrond Myklebust unsigned int want; 823f279cd00SJason Baron int err = 0; 824a246b010SChuck Lever 825262965f5SChuck Lever if (unlikely(!sock)) 826fba91afbSTrond Myklebust return -ENOTSOCK; 827262965f5SChuck Lever 82806b5fc3aSTrond Myklebust want = xdr->head[0].iov_len + rmsize; 82906b5fc3aSTrond Myklebust if (base < want) { 83006b5fc3aSTrond Myklebust unsigned int len = want - base; 83124c5684bSTrond Myklebust remainder -= len; 832e791f8e9STrond Myklebust if (remainder == 0) 833e791f8e9STrond Myklebust msg.msg_flags &= ~MSG_MORE; 83406b5fc3aSTrond Myklebust if (rmsize) 83506b5fc3aSTrond Myklebust err = xs_send_rm_and_kvec(sock, &msg, rm, 83606b5fc3aSTrond Myklebust &xdr->head[0], base); 837067fb11bSChuck Lever else 838e791f8e9STrond Myklebust err = xs_send_kvec(sock, &msg, &xdr->head[0], base); 83924c5684bSTrond Myklebust if (remainder == 0 || err != len) 840a246b010SChuck Lever goto out; 841f279cd00SJason Baron *sent_p += err; 842a246b010SChuck Lever base = 0; 843a246b010SChuck Lever } else 84406b5fc3aSTrond Myklebust base -= want; 845a246b010SChuck Lever 84624c5684bSTrond Myklebust if (base < xdr->page_len) { 84724c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 84824c5684bSTrond Myklebust remainder -= len; 8490472e476STrond Myklebust if (remainder == 0) 8500472e476STrond Myklebust msg.msg_flags &= ~MSG_MORE; 8510472e476STrond Myklebust err = xs_send_pagedata(sock, &msg, xdr, base); 8520472e476STrond Myklebust if (remainder == 0 || err != len) 853a246b010SChuck Lever goto out; 8540472e476STrond Myklebust *sent_p += err; 855a246b010SChuck Lever base = 0; 85624c5684bSTrond Myklebust } else 85724c5684bSTrond Myklebust base -= xdr->page_len; 85824c5684bSTrond Myklebust 85924c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 860f279cd00SJason Baron return 0; 861e791f8e9STrond Myklebust msg.msg_flags &= ~MSG_MORE; 862e791f8e9STrond Myklebust err = xs_send_kvec(sock, &msg, &xdr->tail[0], base); 863a246b010SChuck Lever out: 864f279cd00SJason Baron if (err > 0) { 865f279cd00SJason Baron *sent_p += err; 866f279cd00SJason Baron err = 0; 867f279cd00SJason Baron } 86824c5684bSTrond Myklebust return err; 869a246b010SChuck Lever } 870a246b010SChuck Lever 8719903cd1cSChuck Lever /** 872c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 87350f484e2STrond Myklebust * @req: pointer to RPC request 8749903cd1cSChuck Lever * 875a246b010SChuck Lever */ 876c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req) 877a246b010SChuck Lever { 878262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 879ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 88006ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 88124ca9a84STrond Myklebust int ret = -EAGAIN; 882a246b010SChuck Lever 88346121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 884c544577dSTrond Myklebust req->rq_task->tk_pid, 885c544577dSTrond Myklebust req->rq_slen - transport->xmit.offset, 886262965f5SChuck Lever req->rq_slen); 887a246b010SChuck Lever 888262965f5SChuck Lever /* Protect against races with write_space */ 889b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 890a246b010SChuck Lever 891262965f5SChuck Lever /* Don't race with disconnect */ 892b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 89313331a55STrond Myklebust /* wait for more buffer space */ 89406ea0bfeSTrond Myklebust sk->sk_write_pending++; 895c544577dSTrond Myklebust xprt_wait_for_buffer_space(xprt); 89613331a55STrond Myklebust } else 8975e3771ceSTrond Myklebust ret = -ENOTCONN; 898a246b010SChuck Lever 899b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 90006ea0bfeSTrond Myklebust 90106ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 902d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 903d48f9ce7SDavid Vrabel struct socket_wq *wq; 904d48f9ce7SDavid Vrabel 905d48f9ce7SDavid Vrabel rcu_read_lock(); 906d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 907d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 908d48f9ce7SDavid Vrabel rcu_read_unlock(); 909d48f9ce7SDavid Vrabel 91006ea0bfeSTrond Myklebust sk->sk_write_space(sk); 911d48f9ce7SDavid Vrabel } 9125e3771ceSTrond Myklebust return ret; 913a246b010SChuck Lever } 914a246b010SChuck Lever 915277e4ab7STrond Myklebust static void 916277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req) 917277e4ab7STrond Myklebust { 91875369089STrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 91912a3ad61STrond Myklebust req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL); 920277e4ab7STrond Myklebust } 921277e4ab7STrond Myklebust 92261677eeeSChuck Lever /* 9234cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 9244cd34e7cSTrond Myklebust * could complete transmission. 9254cd34e7cSTrond Myklebust */ 9264cd34e7cSTrond Myklebust static bool 9274cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 9284cd34e7cSTrond Myklebust { 9294cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 9304cd34e7cSTrond Myklebust } 9314cd34e7cSTrond Myklebust 9324cd34e7cSTrond Myklebust /* 93306b5fc3aSTrond Myklebust * Return the stream record marker field for a record of length < 2^31-1 93461677eeeSChuck Lever */ 93506b5fc3aSTrond Myklebust static rpc_fraghdr 93606b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr) 93761677eeeSChuck Lever { 93806b5fc3aSTrond Myklebust if (!xdr->len) 93906b5fc3aSTrond Myklebust return 0; 94006b5fc3aSTrond Myklebust return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 94161677eeeSChuck Lever } 94261677eeeSChuck Lever 9439903cd1cSChuck Lever /** 944176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 94550f484e2STrond Myklebust * @req: pointer to RPC request 946176e21eeSChuck Lever * 947176e21eeSChuck Lever * Return values: 948176e21eeSChuck Lever * 0: The request has been sent 949176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 950176e21eeSChuck Lever * complete the request 951176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 952176e21eeSChuck Lever * other: Some other error occured, the request was not sent 953176e21eeSChuck Lever */ 954adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 955176e21eeSChuck Lever { 956176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 957176e21eeSChuck Lever struct sock_xprt *transport = 958176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 959176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9607e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 9617e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 962176e21eeSChuck Lever int status; 963f279cd00SJason Baron int sent = 0; 964176e21eeSChuck Lever 9654cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 9664cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 9674cd34e7cSTrond Myklebust xs_close(xprt); 9684cd34e7cSTrond Myklebust return -ENOTCONN; 9694cd34e7cSTrond Myklebust } 9704cd34e7cSTrond Myklebust 971176e21eeSChuck Lever xs_pktdump("packet data:", 972176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 973176e21eeSChuck Lever 97478215759SChuck Lever req->rq_xtime = ktime_get(); 9756c7a64e5STrond Myklebust status = xs_sendpages(transport->sock, NULL, 0, xdr, 9767e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 977176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 9786c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 979743c69e7SNeilBrown 980743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 981743c69e7SNeilBrown status = -ENOBUFS; 982743c69e7SNeilBrown 983f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 9846c7a64e5STrond Myklebust transport->xmit.offset += sent; 9856c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 9867e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 9876c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 9886c7a64e5STrond Myklebust transport->xmit.offset = 0; 989176e21eeSChuck Lever return 0; 990176e21eeSChuck Lever } 991176e21eeSChuck Lever status = -EAGAIN; 992176e21eeSChuck Lever } 993176e21eeSChuck Lever 994176e21eeSChuck Lever switch (status) { 9953601c4a9STrond Myklebust case -ENOBUFS: 996b5872f0cSTrond Myklebust break; 997176e21eeSChuck Lever case -EAGAIN: 998c544577dSTrond Myklebust status = xs_nospace(req); 999176e21eeSChuck Lever break; 1000176e21eeSChuck Lever default: 1001176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 1002176e21eeSChuck Lever -status); 1003e9d47639SGustavo A. R. Silva /* fall through */ 1004176e21eeSChuck Lever case -EPIPE: 1005176e21eeSChuck Lever xs_close(xprt); 1006176e21eeSChuck Lever status = -ENOTCONN; 1007176e21eeSChuck Lever } 1008176e21eeSChuck Lever 1009176e21eeSChuck Lever return status; 1010176e21eeSChuck Lever } 1011176e21eeSChuck Lever 1012176e21eeSChuck Lever /** 1013262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 101450f484e2STrond Myklebust * @req: pointer to RPC request 10159903cd1cSChuck Lever * 10169903cd1cSChuck Lever * Return values: 10179903cd1cSChuck Lever * 0: The request has been sent 10189903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 10199903cd1cSChuck Lever * complete the request 1020262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 102125985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 10229903cd1cSChuck Lever */ 1023adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 1024a246b010SChuck Lever { 1025a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1026ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1027262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 1028f279cd00SJason Baron int sent = 0; 1029262965f5SChuck Lever int status; 1030262965f5SChuck Lever 1031262965f5SChuck Lever xs_pktdump("packet data:", 1032262965f5SChuck Lever req->rq_svec->iov_base, 1033262965f5SChuck Lever req->rq_svec->iov_len); 1034262965f5SChuck Lever 103501d37c42STrond Myklebust if (!xprt_bound(xprt)) 103601d37c42STrond Myklebust return -ENOTCONN; 103775891f50STrond Myklebust 103875891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 103975891f50STrond Myklebust return -EBADSLT; 104075891f50STrond Myklebust 104178215759SChuck Lever req->rq_xtime = ktime_get(); 1042f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 104306b5fc3aSTrond Myklebust xdr, 0, 0, &sent); 1044262965f5SChuck Lever 1045262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 10466c7a64e5STrond Myklebust xdr->len, status); 1047262965f5SChuck Lever 10483dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 10493dedbb5cSJason Baron if (status == -EPERM) 10503dedbb5cSJason Baron goto process_status; 10513dedbb5cSJason Baron 1052743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 1053743c69e7SNeilBrown status = -ENOBUFS; 1054743c69e7SNeilBrown 1055f279cd00SJason Baron if (sent > 0 || status == 0) { 1056f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 1057f279cd00SJason Baron if (sent >= req->rq_slen) 1058262965f5SChuck Lever return 0; 1059262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 1060262965f5SChuck Lever status = -EAGAIN; 10612199700fSTrond Myklebust } 1062262965f5SChuck Lever 10633dedbb5cSJason Baron process_status: 1064262965f5SChuck Lever switch (status) { 1065fba91afbSTrond Myklebust case -ENOTSOCK: 1066fba91afbSTrond Myklebust status = -ENOTCONN; 1067fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1068fba91afbSTrond Myklebust break; 1069b6ddf64fSTrond Myklebust case -EAGAIN: 1070c544577dSTrond Myklebust status = xs_nospace(req); 1071b6ddf64fSTrond Myklebust break; 1072262965f5SChuck Lever case -ENETUNREACH: 10733601c4a9STrond Myklebust case -ENOBUFS: 1074262965f5SChuck Lever case -EPIPE: 1075262965f5SChuck Lever case -ECONNREFUSED: 10763dedbb5cSJason Baron case -EPERM: 1077262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 1078262965f5SChuck Lever * prompts ECONNREFUSED. */ 107913331a55STrond Myklebust break; 108013331a55STrond Myklebust default: 108113331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 108213331a55STrond Myklebust -status); 1083262965f5SChuck Lever } 10845fe46e9dSBian Naimeng 1085262965f5SChuck Lever return status; 1086262965f5SChuck Lever } 1087262965f5SChuck Lever 1088e06799f9STrond Myklebust /** 1089262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 109050f484e2STrond Myklebust * @req: pointer to RPC request 1091262965f5SChuck Lever * 1092262965f5SChuck Lever * Return values: 1093262965f5SChuck Lever * 0: The request has been sent 1094262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 1095262965f5SChuck Lever * complete the request 1096262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 109725985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 1098262965f5SChuck Lever * 1099262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 1100262965f5SChuck Lever * if sendmsg is not able to make progress? 1101262965f5SChuck Lever */ 1102adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 1103262965f5SChuck Lever { 1104262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1105ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1106262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 11077e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 11087e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 11099ffadfbcSTrond Myklebust bool vm_wait = false; 1110b595bb15SChuck Lever int status; 1111f279cd00SJason Baron int sent; 1112a246b010SChuck Lever 11134cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 11144cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 11154cd34e7cSTrond Myklebust if (transport->sock != NULL) 11164cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 11174cd34e7cSTrond Myklebust return -ENOTCONN; 11184cd34e7cSTrond Myklebust } 11194cd34e7cSTrond Myklebust 1120262965f5SChuck Lever xs_pktdump("packet data:", 1121262965f5SChuck Lever req->rq_svec->iov_base, 1122262965f5SChuck Lever req->rq_svec->iov_len); 1123a246b010SChuck Lever 11247196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 11257196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 11267196dbb0STrond Myklebust 1127a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1128a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1129262965f5SChuck Lever * called sendmsg(). */ 113078215759SChuck Lever req->rq_xtime = ktime_get(); 1131a246b010SChuck Lever while (1) { 1132f279cd00SJason Baron sent = 0; 1133f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 11347e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 1135a246b010SChuck Lever 1136262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 11376c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1138262965f5SChuck Lever 1139a246b010SChuck Lever /* If we've sent the entire packet, immediately 1140a246b010SChuck Lever * reset the count of bytes sent. */ 11416c7a64e5STrond Myklebust transport->xmit.offset += sent; 11426c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 11437e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 11446c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 11456c7a64e5STrond Myklebust transport->xmit.offset = 0; 1146a246b010SChuck Lever return 0; 1147a246b010SChuck Lever } 1148262965f5SChuck Lever 11499ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 11509ffadfbcSTrond Myklebust 11519ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 11529ffadfbcSTrond Myklebust /* 11539ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 11549ffadfbcSTrond Myklebust * socket send buffer limits. 11559ffadfbcSTrond Myklebust */ 11569ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 11579ffadfbcSTrond Myklebust break; 11589ffadfbcSTrond Myklebust /* 11599ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 11609ffadfbcSTrond Myklebust */ 11619ffadfbcSTrond Myklebust if (sent == 0) { 11629ffadfbcSTrond Myklebust status = -ENOBUFS; 11639ffadfbcSTrond Myklebust if (vm_wait) 11649ffadfbcSTrond Myklebust break; 11659ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 11669ffadfbcSTrond Myklebust * socket send buffer limit 11679ffadfbcSTrond Myklebust */ 11689ffadfbcSTrond Myklebust vm_wait = true; 11699ffadfbcSTrond Myklebust } 11709ffadfbcSTrond Myklebust continue; 11719ffadfbcSTrond Myklebust } 1172f580dd04STrond Myklebust if (status < 0) 1173f580dd04STrond Myklebust break; 11749ffadfbcSTrond Myklebust vm_wait = false; 1175a246b010SChuck Lever } 1176a246b010SChuck Lever 1177262965f5SChuck Lever switch (status) { 1178fba91afbSTrond Myklebust case -ENOTSOCK: 1179fba91afbSTrond Myklebust status = -ENOTCONN; 1180fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1181fba91afbSTrond Myklebust break; 1182262965f5SChuck Lever case -EAGAIN: 1183c544577dSTrond Myklebust status = xs_nospace(req); 1184262965f5SChuck Lever break; 1185262965f5SChuck Lever case -ECONNRESET: 1186262965f5SChuck Lever case -ECONNREFUSED: 1187262965f5SChuck Lever case -ENOTCONN: 11883913c78cSTrond Myklebust case -EADDRINUSE: 1189b5872f0cSTrond Myklebust case -ENOBUFS: 1190b9d2bb2eSTrond Myklebust case -EPIPE: 119113331a55STrond Myklebust break; 119213331a55STrond Myklebust default: 119313331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 119413331a55STrond Myklebust -status); 1195a246b010SChuck Lever } 11965fe46e9dSBian Naimeng 1197a246b010SChuck Lever return status; 1198a246b010SChuck Lever } 1199a246b010SChuck Lever 12002a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 12012a9e1cfaSTrond Myklebust { 12022a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 12032a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 12042a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 12052118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 12062a9e1cfaSTrond Myklebust } 12072a9e1cfaSTrond Myklebust 12082a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 12092a9e1cfaSTrond Myklebust { 12102a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 12112a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 12122a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 12132118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 12142118071dSTrond Myklebust } 12152118071dSTrond Myklebust 121642d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 121742d42a5bSTrond Myklebust { 121842d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 121942d42a5bSTrond Myklebust 122042d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 12214f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 12224f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 12234f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 12244f8943f8STrond Myklebust } 12254f8943f8STrond Myklebust 12264f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 12274f8943f8STrond Myklebust { 12284f8943f8STrond Myklebust set_bit(nr, &transport->sock_state); 12294f8943f8STrond Myklebust queue_work(xprtiod_workqueue, &transport->error_worker); 123042d42a5bSTrond Myklebust } 123142d42a5bSTrond Myklebust 1232b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1233b70ae915STrond Myklebust { 1234b70ae915STrond Myklebust smp_mb__before_atomic(); 1235b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1236b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 123742d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1238b70ae915STrond Myklebust smp_mb__after_atomic(); 1239b70ae915STrond Myklebust } 1240b70ae915STrond Myklebust 12412118071dSTrond Myklebust /** 12422118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 12432118071dSTrond Myklebust * @sk: socket 12442118071dSTrond Myklebust * 12452118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 12462118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 12472118071dSTrond Myklebust */ 12482118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 12492118071dSTrond Myklebust { 12504f8943f8STrond Myklebust struct sock_xprt *transport; 12512118071dSTrond Myklebust struct rpc_xprt *xprt; 12522118071dSTrond Myklebust int err; 12532118071dSTrond Myklebust 12542118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 12552118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12562118071dSTrond Myklebust goto out; 12572118071dSTrond Myklebust 12584f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 12592118071dSTrond Myklebust err = -sk->sk_err; 12602118071dSTrond Myklebust if (err == 0) 12612118071dSTrond Myklebust goto out; 12622118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 12632118071dSTrond Myklebust xprt, -err); 1264e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 12654f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 12662118071dSTrond Myklebust out: 12672118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 12682a9e1cfaSTrond Myklebust } 12692a9e1cfaSTrond Myklebust 1270fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1271a246b010SChuck Lever { 1272ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1273ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12746cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1275a73881c9STrond Myklebust struct file *filp = transport->file; 1276a246b010SChuck Lever 1277fe315e76SChuck Lever if (sk == NULL) 1278fe315e76SChuck Lever return; 12799903cd1cSChuck Lever 1280264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1281264d1df3SJeff Layton sk_clear_memalloc(sk); 1282264d1df3SJeff Layton 128309939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 128409939204STrond Myklebust 1285edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1286a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1287ee0ac0c2SChuck Lever transport->inet = NULL; 1288ee0ac0c2SChuck Lever transport->sock = NULL; 1289a73881c9STrond Myklebust transport->file = NULL; 1290a246b010SChuck Lever 1291a246b010SChuck Lever sk->sk_user_data = NULL; 12922a9e1cfaSTrond Myklebust 12932a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12940c78789eSTrond Myklebust xprt_clear_connected(xprt); 1295a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 12966cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1297ae053551STrond Myklebust /* Reset stream record info */ 1298ae053551STrond Myklebust xs_stream_reset_connect(transport); 1299edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1300a246b010SChuck Lever 13016cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1302a73881c9STrond Myklebust fput(filp); 13030445f92cSTrond Myklebust 13040445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1305fe315e76SChuck Lever } 1306fe315e76SChuck Lever 1307fe315e76SChuck Lever /** 1308fe315e76SChuck Lever * xs_close - close a socket 1309fe315e76SChuck Lever * @xprt: transport 1310fe315e76SChuck Lever * 1311fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1312fe315e76SChuck Lever * on the server we want to save. 1313f75e6745STrond Myklebust * 1314f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1315f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1316fe315e76SChuck Lever */ 1317fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1318fe315e76SChuck Lever { 1319fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1320fe315e76SChuck Lever 1321fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1322fe315e76SChuck Lever 1323fe315e76SChuck Lever xs_reset_transport(transport); 132461d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1325a246b010SChuck Lever } 1326a246b010SChuck Lever 13274a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13284a068258SChuck Lever { 13294a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13304a068258SChuck Lever xprt); 13314a068258SChuck Lever xprt_disconnect_done(xprt); 13324a068258SChuck Lever } 13334a068258SChuck Lever 1334315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1335315f3812SKinglong Mee { 1336315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1337315f3812SKinglong Mee xprt_free(xprt); 1338315f3812SKinglong Mee } 1339315f3812SKinglong Mee 13409903cd1cSChuck Lever /** 13419903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13429903cd1cSChuck Lever * @xprt: doomed transport 13439903cd1cSChuck Lever * 13449903cd1cSChuck Lever */ 13459903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1346a246b010SChuck Lever { 134703c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 134803c78827STrond Myklebust struct sock_xprt, xprt); 13499903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13509903cd1cSChuck Lever 135103c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1352a1311d87STrond Myklebust xs_close(xprt); 1353edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1354b5e92419STrond Myklebust cancel_work_sync(&transport->error_worker); 1355315f3812SKinglong Mee xs_xprt_free(xprt); 1356a1311d87STrond Myklebust module_put(THIS_MODULE); 1357a246b010SChuck Lever } 1358a246b010SChuck Lever 13599903cd1cSChuck Lever /** 1360f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1361f9b2ee71STrond Myklebust * @xprt: transport 1362f9b2ee71STrond Myklebust * @sk: socket 1363f9b2ee71STrond Myklebust * @skb: skbuff 13649903cd1cSChuck Lever * 1365a246b010SChuck Lever */ 1366f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1367f9b2ee71STrond Myklebust struct sock *sk, 1368f9b2ee71STrond Myklebust struct sk_buff *skb) 1369a246b010SChuck Lever { 1370a246b010SChuck Lever struct rpc_task *task; 1371a246b010SChuck Lever struct rpc_rqst *rovr; 1372f9b2ee71STrond Myklebust int repsize, copied; 1373d8ed029dSAlexey Dobriyan u32 _xid; 1374d8ed029dSAlexey Dobriyan __be32 *xp; 1375a246b010SChuck Lever 13761da8c681SWillem de Bruijn repsize = skb->len; 1377a246b010SChuck Lever if (repsize < 4) { 13789903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1379f9b2ee71STrond Myklebust return; 1380a246b010SChuck Lever } 1381a246b010SChuck Lever 1382a246b010SChuck Lever /* Copy the XID from the skb... */ 13831da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1384a246b010SChuck Lever if (xp == NULL) 1385f9b2ee71STrond Myklebust return; 1386a246b010SChuck Lever 1387a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 138875c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1389a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1390a246b010SChuck Lever if (!rovr) 1391a246b010SChuck Lever goto out_unlock; 1392729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1393ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 139475c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1395a246b010SChuck Lever task = rovr->rq_task; 1396a246b010SChuck Lever 1397a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1398a246b010SChuck Lever copied = repsize; 1399a246b010SChuck Lever 1400a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 14011781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 140275c84151STrond Myklebust spin_lock(&xprt->queue_lock); 14030afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1404729749bbSTrond Myklebust goto out_unpin; 14051781f7f5SHerbert Xu } 14061781f7f5SHerbert Xu 1407a246b010SChuck Lever 1408b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 14096a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1410b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 141175c84151STrond Myklebust spin_lock(&xprt->queue_lock); 14121570c1e4SChuck Lever xprt_complete_rqst(task, copied); 14130afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1414729749bbSTrond Myklebust out_unpin: 1415729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1416a246b010SChuck Lever out_unlock: 141775c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1418f9b2ee71STrond Myklebust } 1419f9b2ee71STrond Myklebust 1420f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1421f9b2ee71STrond Myklebust { 1422f9b2ee71STrond Myklebust struct sk_buff *skb; 1423f9b2ee71STrond Myklebust struct sock *sk; 1424f9b2ee71STrond Myklebust int err; 1425f9b2ee71STrond Myklebust 1426f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1427f9b2ee71STrond Myklebust sk = transport->inet; 1428f9b2ee71STrond Myklebust if (sk == NULL) 1429f9b2ee71STrond Myklebust goto out; 1430f9b2ee71STrond Myklebust for (;;) { 14317c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 14324f546149STrond Myklebust if (skb == NULL) 14334f546149STrond Myklebust break; 1434f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1435850cbaddSPaolo Abeni consume_skb(skb); 14360af3442aSTrond Myklebust cond_resched(); 1437f9b2ee71STrond Myklebust } 14380ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1439a246b010SChuck Lever out: 1440f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1441f9b2ee71STrond Myklebust } 1442f9b2ee71STrond Myklebust 1443f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1444f9b2ee71STrond Myklebust { 1445f9b2ee71STrond Myklebust struct sock_xprt *transport = 1446f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1447a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1448a1231fdaSTrond Myklebust 1449f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1450a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1451f9b2ee71STrond Myklebust } 1452f9b2ee71STrond Myklebust 1453f9b2ee71STrond Myklebust /** 1454f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1455f9b2ee71STrond Myklebust * @sk: socket with data to read 1456f9b2ee71STrond Myklebust * 1457f9b2ee71STrond Myklebust */ 1458f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1459f9b2ee71STrond Myklebust { 1460f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1461f9b2ee71STrond Myklebust 1462f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1463f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1464f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1465f9b2ee71STrond Myklebust if (xprt != NULL) { 1466f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1467f9b2ee71STrond Myklebust struct sock_xprt, xprt); 14685157b956STrond Myklebust transport->old_data_ready(sk); 14695157b956STrond Myklebust /* Any data means we had a useful conversation, so 14705157b956STrond Myklebust * then we don't need to delay the next reconnect 14715157b956STrond Myklebust */ 14725157b956STrond Myklebust if (xprt->reestablish_timeout) 14735157b956STrond Myklebust xprt->reestablish_timeout = 0; 147442d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 147540a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1476f9b2ee71STrond Myklebust } 1477f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1478a246b010SChuck Lever } 1479a246b010SChuck Lever 1480a519fc7aSTrond Myklebust /* 1481a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1482a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1483a519fc7aSTrond Myklebust */ 1484a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1485a519fc7aSTrond Myklebust { 1486a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1487a519fc7aSTrond Myklebust } 1488a519fc7aSTrond Myklebust 14899e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14906b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14916b26cc8cSChuck Lever { 14926b26cc8cSChuck Lever return PAGE_SIZE; 14936b26cc8cSChuck Lever } 14949e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 149544b98efdSRicardo Labiaga 14969903cd1cSChuck Lever /** 14979903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14989903cd1cSChuck Lever * @sk: socket whose state has changed 14999903cd1cSChuck Lever * 15009903cd1cSChuck Lever */ 15019903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1502a246b010SChuck Lever { 1503a246b010SChuck Lever struct rpc_xprt *xprt; 15040fdea1e8STrond Myklebust struct sock_xprt *transport; 1505a246b010SChuck Lever 1506f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1507a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1508a246b010SChuck Lever goto out; 15099903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1510669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1511a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1512a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1513669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1514669502ffSAndy Chittenden sk->sk_shutdown); 1515a246b010SChuck Lever 15160fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 151740b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1518a246b010SChuck Lever switch (sk->sk_state) { 1519a246b010SChuck Lever case TCP_ESTABLISHED: 1520a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15218b71798cSTrond Myklebust xprt->connect_cookie++; 15220fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15230fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 152451971139SChuck Lever 15253968a8a5SChuck Lever xprt->stat.connect_count++; 15263968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15273968a8a5SChuck Lever xprt->stat.connect_start; 15284f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1529a246b010SChuck Lever } 1530a246b010SChuck Lever break; 15313b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15323b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15337c1d71cfSTrond Myklebust xprt->connect_cookie++; 1534663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15353b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15364e857c58SPeter Zijlstra smp_mb__before_atomic(); 15373b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1538ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15394e857c58SPeter Zijlstra smp_mb__after_atomic(); 1540a246b010SChuck Lever break; 1541632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15423b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15437c1d71cfSTrond Myklebust xprt->connect_cookie++; 1544d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15454f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1546e9d47639SGustavo A. R. Silva /* fall through */ 1547663b8858STrond Myklebust case TCP_CLOSING: 1548663b8858STrond Myklebust /* 1549663b8858STrond Myklebust * If the server closed down the connection, make sure that 1550663b8858STrond Myklebust * we back off before reconnecting 1551663b8858STrond Myklebust */ 1552663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1553663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15543b948ae5STrond Myklebust break; 15553b948ae5STrond Myklebust case TCP_LAST_ACK: 1556670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15574e857c58SPeter Zijlstra smp_mb__before_atomic(); 15583b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15594e857c58SPeter Zijlstra smp_mb__after_atomic(); 15603b948ae5STrond Myklebust break; 15613b948ae5STrond Myklebust case TCP_CLOSE: 15620fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15630fdea1e8STrond Myklebust &transport->sock_state)) 15640fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15659b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15669b30889cSTrond Myklebust /* Trigger the socket release */ 15674f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1568a246b010SChuck Lever } 1569a246b010SChuck Lever out: 1570f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1571a246b010SChuck Lever } 1572a246b010SChuck Lever 15731f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15741f0fa154SIlpo Järvinen { 157513331a55STrond Myklebust struct socket_wq *wq; 15764f8943f8STrond Myklebust struct sock_xprt *transport; 15771f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15781f0fa154SIlpo Järvinen 157913331a55STrond Myklebust if (!sk->sk_socket) 15801f0fa154SIlpo Järvinen return; 158113331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15821f0fa154SIlpo Järvinen 15831f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15841f0fa154SIlpo Järvinen return; 15854f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 158613331a55STrond Myklebust rcu_read_lock(); 158713331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 158813331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 158913331a55STrond Myklebust goto out; 15901f0fa154SIlpo Järvinen 15914f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1592c544577dSTrond Myklebust sk->sk_write_pending--; 159313331a55STrond Myklebust out: 159413331a55STrond Myklebust rcu_read_unlock(); 15951f0fa154SIlpo Järvinen } 15961f0fa154SIlpo Järvinen 15972a9e1cfaSTrond Myklebust /** 1598c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1599c7b2cae8SChuck Lever * becomes available 16009903cd1cSChuck Lever * @sk: socket whose state has changed 16019903cd1cSChuck Lever * 1602a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1603a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1604c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1605a246b010SChuck Lever * with a bunch of small requests. 1606a246b010SChuck Lever */ 1607c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1608a246b010SChuck Lever { 1609f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1610c7b2cae8SChuck Lever 1611c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16121f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16131f0fa154SIlpo Järvinen xs_write_space(sk); 1614c7b2cae8SChuck Lever 1615f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1616c7b2cae8SChuck Lever } 1617c7b2cae8SChuck Lever 1618c7b2cae8SChuck Lever /** 1619c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1620c7b2cae8SChuck Lever * becomes available 1621c7b2cae8SChuck Lever * @sk: socket whose state has changed 1622c7b2cae8SChuck Lever * 1623c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1624c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1625c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1626c7b2cae8SChuck Lever * with a bunch of small requests. 1627c7b2cae8SChuck Lever */ 1628c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1629c7b2cae8SChuck Lever { 1630f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1631c7b2cae8SChuck Lever 1632c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 163364dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16341f0fa154SIlpo Järvinen xs_write_space(sk); 1635c7b2cae8SChuck Lever 1636f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1637a246b010SChuck Lever } 1638a246b010SChuck Lever 1639470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1640a246b010SChuck Lever { 1641ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1642ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1643a246b010SChuck Lever 16447c6e066eSChuck Lever if (transport->rcvsize) { 1645a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16467c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1647a246b010SChuck Lever } 16487c6e066eSChuck Lever if (transport->sndsize) { 1649a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16507c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1651a246b010SChuck Lever sk->sk_write_space(sk); 1652a246b010SChuck Lever } 1653a246b010SChuck Lever } 1654a246b010SChuck Lever 165543118c29SChuck Lever /** 1656470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 165743118c29SChuck Lever * @xprt: generic transport 1658470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1659470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 166043118c29SChuck Lever * 1661470056c2SChuck Lever * Set socket send and receive buffer size limits. 166243118c29SChuck Lever */ 1663470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 166443118c29SChuck Lever { 16657c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16667c6e066eSChuck Lever 16677c6e066eSChuck Lever transport->sndsize = 0; 1668470056c2SChuck Lever if (sndsize) 16697c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16707c6e066eSChuck Lever transport->rcvsize = 0; 1671470056c2SChuck Lever if (rcvsize) 16727c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1673470056c2SChuck Lever 1674470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 167543118c29SChuck Lever } 167643118c29SChuck Lever 167746c0ee8bSChuck Lever /** 167846c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1679acf0a39fSChuck Lever * @xprt: controlling transport 168046c0ee8bSChuck Lever * @task: task that timed out 168146c0ee8bSChuck Lever * 168246c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 168346c0ee8bSChuck Lever */ 16846a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 168546c0ee8bSChuck Lever { 1686b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 16876a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1688b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 168946c0ee8bSChuck Lever } 169046c0ee8bSChuck Lever 1691826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1692b85d8806SChuck Lever { 1693826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1694826799e6SJ. Bruce Fields unsigned short range; 1695826799e6SJ. Bruce Fields unsigned short rand; 1696826799e6SJ. Bruce Fields 1697826799e6SJ. Bruce Fields if (max < min) 1698826799e6SJ. Bruce Fields return -EADDRINUSE; 1699826799e6SJ. Bruce Fields range = max - min + 1; 1700826799e6SJ. Bruce Fields rand = (unsigned short) prandom_u32() % range; 1701826799e6SJ. Bruce Fields return rand + min; 1702b85d8806SChuck Lever } 1703b85d8806SChuck Lever 170492200412SChuck Lever /** 17054dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17064dda9c8aSTrond Myklebust * @sock: socket 17074dda9c8aSTrond Myklebust * 17084dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17094dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17104dda9c8aSTrond Myklebust */ 17114dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17124dda9c8aSTrond Myklebust { 1713402e23b4STrond Myklebust int opt = 1; 17144dda9c8aSTrond Myklebust 1715402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1716402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17174dda9c8aSTrond Myklebust } 17184dda9c8aSTrond Myklebust 17194dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17204dda9c8aSTrond Myklebust { 17214dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17224dda9c8aSTrond Myklebust unsigned short port = 0; 17234dda9c8aSTrond Myklebust 17249b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 17254dda9c8aSTrond Myklebust goto out; 17264dda9c8aSTrond Myklebust switch (buf.ss_family) { 17274dda9c8aSTrond Myklebust case AF_INET6: 17284dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17294dda9c8aSTrond Myklebust break; 17304dda9c8aSTrond Myklebust case AF_INET: 17314dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17324dda9c8aSTrond Myklebust } 17334dda9c8aSTrond Myklebust out: 17344dda9c8aSTrond Myklebust return port; 17354dda9c8aSTrond Myklebust } 17364dda9c8aSTrond Myklebust 17374dda9c8aSTrond Myklebust /** 173892200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 173992200412SChuck Lever * @xprt: generic transport 174092200412SChuck Lever * @port: new port number 174192200412SChuck Lever * 174292200412SChuck Lever */ 174392200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 174492200412SChuck Lever { 174592200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1746c4efcb1dSChuck Lever 17479dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17489dc3b095SChuck Lever xs_update_peer_port(xprt); 174992200412SChuck Lever } 175092200412SChuck Lever 17514dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17524dda9c8aSTrond Myklebust { 17534dda9c8aSTrond Myklebust if (transport->srcport == 0) 17544dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17554dda9c8aSTrond Myklebust } 17564dda9c8aSTrond Myklebust 1757826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 175867a391d7STrond Myklebust { 1759826799e6SJ. Bruce Fields int port = transport->srcport; 176067a391d7STrond Myklebust 176167a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 176267a391d7STrond Myklebust port = xs_get_random_port(); 176367a391d7STrond Myklebust return port; 176467a391d7STrond Myklebust } 176567a391d7STrond Myklebust 1766baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 176767a391d7STrond Myklebust { 1768fbfffbd5SChuck Lever if (transport->srcport != 0) 1769fbfffbd5SChuck Lever transport->srcport = 0; 177067a391d7STrond Myklebust if (!transport->xprt.resvport) 177167a391d7STrond Myklebust return 0; 177267a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 177367a391d7STrond Myklebust return xprt_max_resvport; 177467a391d7STrond Myklebust return --port; 177567a391d7STrond Myklebust } 1776beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1777a246b010SChuck Lever { 1778beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 177967a391d7STrond Myklebust int err, nloop = 0; 1780826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 178167a391d7STrond Myklebust unsigned short last; 1782a246b010SChuck Lever 17830f7a622cSChris Perl /* 17840f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17850f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17860f7a622cSChris Perl * port selection happen implicitly when the socket is used 17870f7a622cSChris Perl * (for example at connect time). 17880f7a622cSChris Perl * 17890f7a622cSChris Perl * This ensures that we can continue to establish TCP 17900f7a622cSChris Perl * connections even when all local ephemeral ports are already 17910f7a622cSChris Perl * a part of some TCP connection. This makes no difference 17920f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 17930f7a622cSChris Perl * 17940f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17950f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17960f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17970f7a622cSChris Perl */ 1798826799e6SJ. Bruce Fields if (port <= 0) 1799826799e6SJ. Bruce Fields return port; 18000f7a622cSChris Perl 1801beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1802a246b010SChuck Lever do { 1803beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1804e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1805beb59b68SPavel Emelyanov transport->xprt.addrlen); 1806a246b010SChuck Lever if (err == 0) { 1807fbfffbd5SChuck Lever transport->srcport = port; 1808d3bc9a1dSFrank van Maarseveen break; 1809a246b010SChuck Lever } 181067a391d7STrond Myklebust last = port; 1811baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 181267a391d7STrond Myklebust if (port > last) 181367a391d7STrond Myklebust nloop++; 181467a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1815beb59b68SPavel Emelyanov 18164232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1817beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1818beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1819beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1820beb59b68SPavel Emelyanov else 1821beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1822beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18237dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1824a246b010SChuck Lever return err; 1825a246b010SChuck Lever } 1826a246b010SChuck Lever 1827176e21eeSChuck Lever /* 1828176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1829176e21eeSChuck Lever */ 1830176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1831176e21eeSChuck Lever { 1832fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1833176e21eeSChuck Lever } 1834176e21eeSChuck Lever 1835176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1836176e21eeSChuck Lever { 1837176e21eeSChuck Lever } 1838a246b010SChuck Lever 1839ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1840ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1841ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1842ed07536eSPeter Zijlstra 1843176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1844176e21eeSChuck Lever { 1845176e21eeSChuck Lever struct sock *sk = sock->sk; 1846176e21eeSChuck Lever 1847176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1848176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1849176e21eeSChuck Lever } 1850176e21eeSChuck Lever 18518945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1852ed07536eSPeter Zijlstra { 1853ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18548945ee5eSChuck Lever 18558945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 18568945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1857ed07536eSPeter Zijlstra } 18588945ee5eSChuck Lever 18598945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18608945ee5eSChuck Lever { 18618945ee5eSChuck Lever struct sock *sk = sock->sk; 18628945ee5eSChuck Lever 18638945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 18648945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1865ed07536eSPeter Zijlstra } 18666bc9638aSPavel Emelyanov 18676bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18686bc9638aSPavel Emelyanov { 1869fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18701b7a1819SWeston Andros Adamson return; 18711b7a1819SWeston Andros Adamson 18724232e863SChuck Lever switch (family) { 1873176e21eeSChuck Lever case AF_LOCAL: 1874176e21eeSChuck Lever xs_reclassify_socketu(sock); 1875176e21eeSChuck Lever break; 18764232e863SChuck Lever case AF_INET: 18776bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18784232e863SChuck Lever break; 18794232e863SChuck Lever case AF_INET6: 18806bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18814232e863SChuck Lever break; 18824232e863SChuck Lever } 18836bc9638aSPavel Emelyanov } 1884ed07536eSPeter Zijlstra #else 18856bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18866bc9638aSPavel Emelyanov { 18876bc9638aSPavel Emelyanov } 1888ed07536eSPeter Zijlstra #endif 1889ed07536eSPeter Zijlstra 189093dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 189193dc41bdSNeilBrown { 189293dc41bdSNeilBrown } 189393dc41bdSNeilBrown 18946bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18954dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18964dda9c8aSTrond Myklebust int protocol, bool reuseport) 189722f79326SPavel Emelyanov { 1898a73881c9STrond Myklebust struct file *filp; 189922f79326SPavel Emelyanov struct socket *sock; 190022f79326SPavel Emelyanov int err; 190122f79326SPavel Emelyanov 19026bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 190322f79326SPavel Emelyanov if (err < 0) { 190422f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 190522f79326SPavel Emelyanov protocol, -err); 190622f79326SPavel Emelyanov goto out; 190722f79326SPavel Emelyanov } 19086bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 190922f79326SPavel Emelyanov 19104dda9c8aSTrond Myklebust if (reuseport) 19114dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19124dda9c8aSTrond Myklebust 19134cea288aSBen Hutchings err = xs_bind(transport, sock); 19144cea288aSBen Hutchings if (err) { 191522f79326SPavel Emelyanov sock_release(sock); 191622f79326SPavel Emelyanov goto out; 191722f79326SPavel Emelyanov } 191822f79326SPavel Emelyanov 1919a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1920a73881c9STrond Myklebust if (IS_ERR(filp)) 1921a73881c9STrond Myklebust return ERR_CAST(filp); 1922a73881c9STrond Myklebust transport->file = filp; 1923a73881c9STrond Myklebust 192422f79326SPavel Emelyanov return sock; 192522f79326SPavel Emelyanov out: 192622f79326SPavel Emelyanov return ERR_PTR(err); 192722f79326SPavel Emelyanov } 192822f79326SPavel Emelyanov 1929176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1930176e21eeSChuck Lever struct socket *sock) 1931176e21eeSChuck Lever { 1932176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1933176e21eeSChuck Lever xprt); 1934176e21eeSChuck Lever 1935176e21eeSChuck Lever if (!transport->inet) { 1936176e21eeSChuck Lever struct sock *sk = sock->sk; 1937176e21eeSChuck Lever 1938176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1939176e21eeSChuck Lever 1940176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1941176e21eeSChuck Lever 1942176e21eeSChuck Lever sk->sk_user_data = xprt; 1943a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1944176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1945b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 19462118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1947176e21eeSChuck Lever 1948176e21eeSChuck Lever xprt_clear_connected(xprt); 1949176e21eeSChuck Lever 1950176e21eeSChuck Lever /* Reset to new socket */ 1951176e21eeSChuck Lever transport->sock = sock; 1952176e21eeSChuck Lever transport->inet = sk; 1953176e21eeSChuck Lever 1954176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1955176e21eeSChuck Lever } 1956176e21eeSChuck Lever 1957ae053551STrond Myklebust xs_stream_start_connect(transport); 19586c7a64e5STrond Myklebust 1959176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1960176e21eeSChuck Lever } 1961176e21eeSChuck Lever 1962176e21eeSChuck Lever /** 1963176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1964176e21eeSChuck Lever * @transport: socket transport to connect 1965176e21eeSChuck Lever */ 1966dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1967176e21eeSChuck Lever { 1968176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1969a73881c9STrond Myklebust struct file *filp; 1970176e21eeSChuck Lever struct socket *sock; 1971176e21eeSChuck Lever int status = -EIO; 1972176e21eeSChuck Lever 1973176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1974176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1975176e21eeSChuck Lever if (status < 0) { 1976176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1977176e21eeSChuck Lever "transport socket (%d).\n", -status); 1978176e21eeSChuck Lever goto out; 1979176e21eeSChuck Lever } 1980d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1981176e21eeSChuck Lever 1982a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1983a73881c9STrond Myklebust if (IS_ERR(filp)) { 1984a73881c9STrond Myklebust status = PTR_ERR(filp); 1985a73881c9STrond Myklebust goto out; 1986a73881c9STrond Myklebust } 1987a73881c9STrond Myklebust transport->file = filp; 1988a73881c9STrond Myklebust 1989176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1990176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1991176e21eeSChuck Lever 1992176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 199340b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1994176e21eeSChuck Lever switch (status) { 1995176e21eeSChuck Lever case 0: 1996176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1997176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19983968a8a5SChuck Lever xprt->stat.connect_count++; 19993968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 20003968a8a5SChuck Lever xprt->stat.connect_start; 2001176e21eeSChuck Lever xprt_set_connected(xprt); 20023601c4a9STrond Myklebust case -ENOBUFS: 2003176e21eeSChuck Lever break; 2004176e21eeSChuck Lever case -ENOENT: 2005176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2006176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2007176e21eeSChuck Lever break; 20084a20a988STrond Myklebust case -ECONNREFUSED: 20094a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20104a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20114a20a988STrond Myklebust break; 2012176e21eeSChuck Lever default: 2013176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2014176e21eeSChuck Lever __func__, -status, 2015176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2016176e21eeSChuck Lever } 2017176e21eeSChuck Lever 2018176e21eeSChuck Lever out: 2019176e21eeSChuck Lever xprt_clear_connecting(xprt); 2020176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2021dc107402SJ. Bruce Fields return status; 2022dc107402SJ. Bruce Fields } 2023dc107402SJ. Bruce Fields 2024b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2025dc107402SJ. Bruce Fields { 2026dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2027dc107402SJ. Bruce Fields int ret; 2028dc107402SJ. Bruce Fields 2029dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2030dc107402SJ. Bruce Fields /* 2031dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2032dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2033dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2034dc107402SJ. Bruce Fields * 2035dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2036dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2037dc107402SJ. Bruce Fields * connect. 2038dc107402SJ. Bruce Fields */ 20395ad64b36STrond Myklebust task->tk_rpc_status = -ENOTCONN; 2040dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2041dc107402SJ. Bruce Fields return; 2042dc107402SJ. Bruce Fields } 2043dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2044dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2045dc107402SJ. Bruce Fields msleep_interruptible(15000); 2046176e21eeSChuck Lever } 2047176e21eeSChuck Lever 20483c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2049d6e971d8SJeff Layton /* 2050d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2051d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2052d6e971d8SJeff Layton * races with xs_reset_transport. 2053d6e971d8SJeff Layton */ 2054a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2055a564b8f0SMel Gorman { 2056a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2057a564b8f0SMel Gorman xprt); 2058a564b8f0SMel Gorman 2059d6e971d8SJeff Layton /* 2060d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2061d6e971d8SJeff Layton * reconnecting process will get it for us. 2062d6e971d8SJeff Layton */ 2063d6e971d8SJeff Layton if (!transport->inet) 2064d6e971d8SJeff Layton return; 20658e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2066a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2067a564b8f0SMel Gorman } 2068a564b8f0SMel Gorman 2069a564b8f0SMel Gorman /** 2070d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2071a564b8f0SMel Gorman * @xprt: transport to tag 2072a564b8f0SMel Gorman * 20738e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20748e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2075a564b8f0SMel Gorman */ 2076d67fa4d8SJeff Layton static int 2077d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2078a564b8f0SMel Gorman { 2079d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2080a564b8f0SMel Gorman 2081d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2082d6e971d8SJeff Layton return 0; 2083d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2084d6e971d8SJeff Layton return -ERESTARTSYS; 2085d6e971d8SJeff Layton if (xs->inet) 2086d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2087d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 20888e228133SJeff Layton return 0; 2089a564b8f0SMel Gorman } 2090a564b8f0SMel Gorman 20918e228133SJeff Layton /** 2092d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20938e228133SJeff Layton * @xprt: transport to tag 20948e228133SJeff Layton * 20958e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 20968e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 20978e228133SJeff Layton */ 2098d67fa4d8SJeff Layton static void 2099d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21008e228133SJeff Layton { 2101d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21028e228133SJeff Layton 2103d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2104d6e971d8SJeff Layton return; 2105d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2106d6e971d8SJeff Layton return; 2107d6e971d8SJeff Layton if (xs->inet) 2108d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2109d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2110a564b8f0SMel Gorman } 2111a564b8f0SMel Gorman #else 2112a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2113a564b8f0SMel Gorman { 2114a564b8f0SMel Gorman } 2115d67fa4d8SJeff Layton 2116d67fa4d8SJeff Layton static int 2117d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2118d67fa4d8SJeff Layton { 2119d67fa4d8SJeff Layton return -EINVAL; 2120d67fa4d8SJeff Layton } 2121d67fa4d8SJeff Layton 2122d67fa4d8SJeff Layton static void 2123d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2124d67fa4d8SJeff Layton { 2125d67fa4d8SJeff Layton } 2126a564b8f0SMel Gorman #endif 2127a564b8f0SMel Gorman 212816be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2129a246b010SChuck Lever { 213016be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2131edb267a6SChuck Lever 2132ee0ac0c2SChuck Lever if (!transport->inet) { 2133b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2134b0d93ad5SChuck Lever 2135b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2136b0d93ad5SChuck Lever 21372a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21382a9e1cfaSTrond Myklebust 2139b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2140f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2141b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2142b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2143b0d93ad5SChuck Lever 2144b0d93ad5SChuck Lever xprt_set_connected(xprt); 2145b0d93ad5SChuck Lever 2146b0d93ad5SChuck Lever /* Reset to new socket */ 2147ee0ac0c2SChuck Lever transport->sock = sock; 2148ee0ac0c2SChuck Lever transport->inet = sk; 2149b0d93ad5SChuck Lever 2150a564b8f0SMel Gorman xs_set_memalloc(xprt); 2151a564b8f0SMel Gorman 2152b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2153b0d93ad5SChuck Lever } 2154470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 215502910177STrond Myklebust 215602910177STrond Myklebust xprt->stat.connect_start = jiffies; 215716be2d20SChuck Lever } 215816be2d20SChuck Lever 21598c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2160a246b010SChuck Lever { 2161a246b010SChuck Lever struct sock_xprt *transport = 2162a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2163a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2164d099b8afSColin Ian King struct socket *sock; 2165b65c0310SPavel Emelyanov int status = -EIO; 2166a246b010SChuck Lever 21678c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21684dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21694dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2170b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2171a246b010SChuck Lever goto out; 217268e220bdSChuck Lever 2173c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2174c740eff8SChuck Lever "%s (port %s)\n", xprt, 2175c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2176c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2177c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 217868e220bdSChuck Lever 217968e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 218040b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2181a246b010SChuck Lever status = 0; 2182b0d93ad5SChuck Lever out: 2183b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2184cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 21857d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2186b0d93ad5SChuck Lever } 2187b0d93ad5SChuck Lever 21884876cc77STrond Myklebust /** 21894876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21904876cc77STrond Myklebust * @xprt: transport 21914876cc77STrond Myklebust * 21924876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 21934876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 21944876cc77STrond Myklebust */ 21954876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 21964876cc77STrond Myklebust { 21974876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21984876cc77STrond Myklebust struct socket *sock = transport->sock; 21999b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22004876cc77STrond Myklebust 22014876cc77STrond Myklebust if (sock == NULL) 22024876cc77STrond Myklebust return; 22039b30889cSTrond Myklebust switch (skst) { 22049b30889cSTrond Myklebust default: 22054876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22064876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22079b30889cSTrond Myklebust break; 22089b30889cSTrond Myklebust case TCP_CLOSE: 22099b30889cSTrond Myklebust case TCP_TIME_WAIT: 22104876cc77STrond Myklebust xs_reset_transport(transport); 22114876cc77STrond Myklebust } 22129b30889cSTrond Myklebust } 22134876cc77STrond Myklebust 22148d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22158d1b8c62STrond Myklebust struct socket *sock) 2216b0d93ad5SChuck Lever { 221716be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22187196dbb0STrond Myklebust unsigned int keepidle; 22197196dbb0STrond Myklebust unsigned int keepcnt; 22207f260e85STrond Myklebust unsigned int opt_on = 1; 2221775f06abSTrond Myklebust unsigned int timeo; 22227f260e85STrond Myklebust 2223b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22247196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22257196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22267196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22277196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22287196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2229b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22307f260e85STrond Myklebust 22317f260e85STrond Myklebust /* TCP Keepalive options */ 22327f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22337f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22347f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22357f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22367f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22377f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22387f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22397f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2240b0d93ad5SChuck Lever 22418d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 22428d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 22438d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 22448d1b8c62STrond Myklebust } 22458d1b8c62STrond Myklebust 22467196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22477196dbb0STrond Myklebust unsigned long connect_timeout, 22487196dbb0STrond Myklebust unsigned long reconnect_timeout) 22497196dbb0STrond Myklebust { 22507196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22517196dbb0STrond Myklebust struct rpc_timeout to; 22527196dbb0STrond Myklebust unsigned long initval; 22537196dbb0STrond Myklebust 2254b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22557196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22567196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22577196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 22587196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22597196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 22607196dbb0STrond Myklebust /* Arbitrary lower limit */ 22617196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 22627196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 22637196dbb0STrond Myklebust to.to_initval = initval; 22647196dbb0STrond Myklebust to.to_maxval = initval; 22657196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 22667196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 22677196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 22687196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 22697196dbb0STrond Myklebust } 22707196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2271b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22727196dbb0STrond Myklebust } 22737196dbb0STrond Myklebust 22748d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 22758d1b8c62STrond Myklebust { 22768d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22778d1b8c62STrond Myklebust int ret = -ENOTCONN; 22788d1b8c62STrond Myklebust 22798d1b8c62STrond Myklebust if (!transport->inet) { 22808d1b8c62STrond Myklebust struct sock *sk = sock->sk; 22818d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 22828d1b8c62STrond Myklebust 2283d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2284d88e4d82SNeilBrown * connections such as NFS mounts. 2285d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2286d88e4d82SNeilBrown * Individual applications, which have specific 2287d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2288d88e4d82SNeilBrown * MAY override this as appropriate. 2289d88e4d82SNeilBrown */ 2290d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2291d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2292d88e4d82SNeilBrown 22938d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2294775f06abSTrond Myklebust 2295b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2296b0d93ad5SChuck Lever 22972a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 22982a9e1cfaSTrond Myklebust 2299b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23005157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2301b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2302b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2303b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 23042118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 23053167e12cSChuck Lever 23063167e12cSChuck Lever /* socket options */ 23073167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 23083167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2309b0d93ad5SChuck Lever 2310b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2311b0d93ad5SChuck Lever 2312b0d93ad5SChuck Lever /* Reset to new socket */ 2313ee0ac0c2SChuck Lever transport->sock = sock; 2314ee0ac0c2SChuck Lever transport->inet = sk; 2315b0d93ad5SChuck Lever 2316b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2317b0d93ad5SChuck Lever } 2318b0d93ad5SChuck Lever 231901d37c42STrond Myklebust if (!xprt_bound(xprt)) 2320fe19a96bSTrond Myklebust goto out; 232101d37c42STrond Myklebust 2322a564b8f0SMel Gorman xs_set_memalloc(xprt); 2323a564b8f0SMel Gorman 2324ae053551STrond Myklebust xs_stream_start_connect(transport); 2325e1806c7bSTrond Myklebust 2326b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23270fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2328fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2329fe19a96bSTrond Myklebust switch (ret) { 2330fe19a96bSTrond Myklebust case 0: 23314dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2332e9d47639SGustavo A. R. Silva /* fall through */ 2333fe19a96bSTrond Myklebust case -EINPROGRESS: 2334fe19a96bSTrond Myklebust /* SYN_SENT! */ 2335fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2336fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 23371f4c17a0STrond Myklebust break; 23381f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 23391f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 23401f4c17a0STrond Myklebust transport->srcport = 0; 2341fe19a96bSTrond Myklebust } 2342fe19a96bSTrond Myklebust out: 2343fe19a96bSTrond Myklebust return ret; 234416be2d20SChuck Lever } 234516be2d20SChuck Lever 234616be2d20SChuck Lever /** 2347b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2348acf0a39fSChuck Lever * @work: queued work item 234916be2d20SChuck Lever * 235016be2d20SChuck Lever * Invoked by a work queue tasklet. 235116be2d20SChuck Lever */ 2352cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 235316be2d20SChuck Lever { 2354cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2355cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 235616be2d20SChuck Lever struct socket *sock = transport->sock; 2357a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2358b61d59ffSTrond Myklebust int status = -EIO; 235916be2d20SChuck Lever 236016be2d20SChuck Lever if (!sock) { 2361cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 23624dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 23634dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2364b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2365b61d59ffSTrond Myklebust status = PTR_ERR(sock); 236616be2d20SChuck Lever goto out; 236716be2d20SChuck Lever } 23687d1e8255STrond Myklebust } 23697d1e8255STrond Myklebust 2370c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2371c740eff8SChuck Lever "%s (port %s)\n", xprt, 2372c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2373c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2374c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 237516be2d20SChuck Lever 237616be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 237740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2378a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 237946121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 238046121cf7SChuck Lever sock->sk->sk_state); 2381a246b010SChuck Lever switch (status) { 2382f75e6745STrond Myklebust default: 2383f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2384f75e6745STrond Myklebust __func__, status); 2385e9d47639SGustavo A. R. Silva /* fall through */ 2386f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2387f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2388f75e6745STrond Myklebust * and retry 2389f75e6745STrond Myklebust */ 2390a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 239188b5ed73STrond Myklebust break; 23922a491991STrond Myklebust case 0: 2393a246b010SChuck Lever case -EINPROGRESS: 2394a246b010SChuck Lever case -EALREADY: 2395718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 23967d1e8255STrond Myklebust return; 23979fcfe0c8STrond Myklebust case -EINVAL: 23989fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 23999fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24009fcfe0c8STrond Myklebust */ 24013ed5e2a2STrond Myklebust case -ECONNREFUSED: 24023ed5e2a2STrond Myklebust case -ECONNRESET: 2403eb5b46faSTrond Myklebust case -ENETDOWN: 24043ed5e2a2STrond Myklebust case -ENETUNREACH: 24054ba161a7STrond Myklebust case -EHOSTUNREACH: 24063913c78cSTrond Myklebust case -EADDRINUSE: 24073601c4a9STrond Myklebust case -ENOBUFS: 24086ea44adcSNeilBrown /* 24096ea44adcSNeilBrown * xs_tcp_force_close() wakes tasks with -EIO. 24106ea44adcSNeilBrown * We need to wake them first to ensure the 24116ea44adcSNeilBrown * correct error code. 24126ea44adcSNeilBrown */ 24136ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24144efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 24159fcfe0c8STrond Myklebust goto out; 24168a2cec29STrond Myklebust } 24172a491991STrond Myklebust status = -EAGAIN; 2418a246b010SChuck Lever out: 24192226feb6SChuck Lever xprt_clear_connecting(xprt); 2420cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 24217d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2422a246b010SChuck Lever } 2423a246b010SChuck Lever 242468e220bdSChuck Lever /** 24259903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 24261b092092STrond Myklebust * @xprt: pointer to transport structure 24279903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 24289903cd1cSChuck Lever * 24299903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 243003bf4b70SChuck Lever * 243103bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 243203bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 243303bf4b70SChuck Lever * socket on a privileged port. 243403bf4b70SChuck Lever * 243503bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 243603bf4b70SChuck Lever * retry floods (hard mounts). 24379903cd1cSChuck Lever */ 24381b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2439a246b010SChuck Lever { 2440ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 244102910177STrond Myklebust unsigned long delay = 0; 2442a246b010SChuck Lever 2443718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2444718ba5b8STrond Myklebust 244599b1a4c3STrond Myklebust if (transport->sock != NULL) { 244646121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 244746121cf7SChuck Lever "seconds\n", 244803bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 244999b1a4c3STrond Myklebust 245099b1a4c3STrond Myklebust /* Start by resetting any existing state */ 245199b1a4c3STrond Myklebust xs_reset_transport(transport); 245299b1a4c3STrond Myklebust 2453675dd90aSChuck Lever delay = xprt_reconnect_delay(xprt); 2454675dd90aSChuck Lever xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 245502910177STrond Myklebust 245602910177STrond Myklebust } else 24579903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 245802910177STrond Myklebust 245940a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 246002910177STrond Myklebust &transport->connect_worker, 246102910177STrond Myklebust delay); 2462a246b010SChuck Lever } 2463a246b010SChuck Lever 24644f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 24654f8943f8STrond Myklebust { 24664f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 24674f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 24684f8943f8STrond Myklebust } 24694f8943f8STrond Myklebust 24704f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 24714f8943f8STrond Myklebust { 24724f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 24734f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 24744f8943f8STrond Myklebust } 24754f8943f8STrond Myklebust 24764f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 24774f8943f8STrond Myklebust { 24784f8943f8STrond Myklebust int sockerr; 24794f8943f8STrond Myklebust int sockerr_len = sizeof(sockerr); 24804f8943f8STrond Myklebust 24814f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 24824f8943f8STrond Myklebust return; 24834f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 24844f8943f8STrond Myklebust if (transport->sock == NULL) 24854f8943f8STrond Myklebust goto out; 24864f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 24874f8943f8STrond Myklebust goto out; 24884f8943f8STrond Myklebust if (kernel_getsockopt(transport->sock, SOL_SOCKET, SO_ERROR, 24894f8943f8STrond Myklebust (char *)&sockerr, &sockerr_len) != 0) 24904f8943f8STrond Myklebust goto out; 24914f8943f8STrond Myklebust if (sockerr < 0) 24924f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 24934f8943f8STrond Myklebust out: 24944f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 24954f8943f8STrond Myklebust } 24964f8943f8STrond Myklebust 24974f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 24984f8943f8STrond Myklebust { 24994f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 25004f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 25014f8943f8STrond Myklebust } 25024f8943f8STrond Myklebust 25034f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 25044f8943f8STrond Myklebust { 25054f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 25064f8943f8STrond Myklebust struct sock_xprt, error_worker); 25074f8943f8STrond Myklebust 25084f8943f8STrond Myklebust xs_wake_disconnect(transport); 25094f8943f8STrond Myklebust xs_wake_write(transport); 25104f8943f8STrond Myklebust xs_wake_error(transport); 25114f8943f8STrond Myklebust xs_wake_pending(transport); 25124f8943f8STrond Myklebust } 25134f8943f8STrond Myklebust 2514262ca07dSChuck Lever /** 2515176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2516176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2517176e21eeSChuck Lever * @seq: output file 2518176e21eeSChuck Lever * 2519176e21eeSChuck Lever */ 2520176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2521176e21eeSChuck Lever { 2522176e21eeSChuck Lever long idle_time = 0; 2523176e21eeSChuck Lever 2524176e21eeSChuck Lever if (xprt_connected(xprt)) 2525176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2526176e21eeSChuck Lever 2527176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 252815a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2529176e21eeSChuck Lever xprt->stat.bind_count, 2530176e21eeSChuck Lever xprt->stat.connect_count, 25318440a886SChuck Lever xprt->stat.connect_time / HZ, 2532176e21eeSChuck Lever idle_time, 2533176e21eeSChuck Lever xprt->stat.sends, 2534176e21eeSChuck Lever xprt->stat.recvs, 2535176e21eeSChuck Lever xprt->stat.bad_xids, 2536176e21eeSChuck Lever xprt->stat.req_u, 253715a45206SAndy Adamson xprt->stat.bklog_u, 253815a45206SAndy Adamson xprt->stat.max_slots, 253915a45206SAndy Adamson xprt->stat.sending_u, 254015a45206SAndy Adamson xprt->stat.pending_u); 2541176e21eeSChuck Lever } 2542176e21eeSChuck Lever 2543176e21eeSChuck Lever /** 2544262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2545262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2546262ca07dSChuck Lever * @seq: output file 2547262ca07dSChuck Lever * 2548262ca07dSChuck Lever */ 2549262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2550262ca07dSChuck Lever { 2551c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2552c8475461SChuck Lever 255315a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 255415a45206SAndy Adamson "%lu %llu %llu\n", 2555fbfffbd5SChuck Lever transport->srcport, 2556262ca07dSChuck Lever xprt->stat.bind_count, 2557262ca07dSChuck Lever xprt->stat.sends, 2558262ca07dSChuck Lever xprt->stat.recvs, 2559262ca07dSChuck Lever xprt->stat.bad_xids, 2560262ca07dSChuck Lever xprt->stat.req_u, 256115a45206SAndy Adamson xprt->stat.bklog_u, 256215a45206SAndy Adamson xprt->stat.max_slots, 256315a45206SAndy Adamson xprt->stat.sending_u, 256415a45206SAndy Adamson xprt->stat.pending_u); 2565262ca07dSChuck Lever } 2566262ca07dSChuck Lever 2567262ca07dSChuck Lever /** 2568262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2569262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2570262ca07dSChuck Lever * @seq: output file 2571262ca07dSChuck Lever * 2572262ca07dSChuck Lever */ 2573262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2574262ca07dSChuck Lever { 2575c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2576262ca07dSChuck Lever long idle_time = 0; 2577262ca07dSChuck Lever 2578262ca07dSChuck Lever if (xprt_connected(xprt)) 2579262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2580262ca07dSChuck Lever 258115a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 258215a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2583fbfffbd5SChuck Lever transport->srcport, 2584262ca07dSChuck Lever xprt->stat.bind_count, 2585262ca07dSChuck Lever xprt->stat.connect_count, 25868440a886SChuck Lever xprt->stat.connect_time / HZ, 2587262ca07dSChuck Lever idle_time, 2588262ca07dSChuck Lever xprt->stat.sends, 2589262ca07dSChuck Lever xprt->stat.recvs, 2590262ca07dSChuck Lever xprt->stat.bad_xids, 2591262ca07dSChuck Lever xprt->stat.req_u, 259215a45206SAndy Adamson xprt->stat.bklog_u, 259315a45206SAndy Adamson xprt->stat.max_slots, 259415a45206SAndy Adamson xprt->stat.sending_u, 259515a45206SAndy Adamson xprt->stat.pending_u); 2596262ca07dSChuck Lever } 2597262ca07dSChuck Lever 25984cfc7e60SRahul Iyer /* 25994cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 26004cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 26014cfc7e60SRahul Iyer * to use the server side send routines. 26024cfc7e60SRahul Iyer */ 26035fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 26044cfc7e60SRahul Iyer { 26055fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 26065fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 26074cfc7e60SRahul Iyer struct page *page; 26084cfc7e60SRahul Iyer struct rpc_buffer *buf; 26094cfc7e60SRahul Iyer 26105fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 26115fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 26125fe6eaa1SChuck Lever size); 26135fe6eaa1SChuck Lever return -EINVAL; 26145fe6eaa1SChuck Lever } 26154cfc7e60SRahul Iyer 2616b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 26174cfc7e60SRahul Iyer if (!page) 26185fe6eaa1SChuck Lever return -ENOMEM; 26194cfc7e60SRahul Iyer 26204cfc7e60SRahul Iyer buf = page_address(page); 26214cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 26224cfc7e60SRahul Iyer 26235fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 262418e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 26255fe6eaa1SChuck Lever return 0; 26264cfc7e60SRahul Iyer } 26274cfc7e60SRahul Iyer 26284cfc7e60SRahul Iyer /* 26294cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 26304cfc7e60SRahul Iyer */ 26313435c74aSChuck Lever static void bc_free(struct rpc_task *task) 26324cfc7e60SRahul Iyer { 26333435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 26344cfc7e60SRahul Iyer struct rpc_buffer *buf; 26354cfc7e60SRahul Iyer 26364cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 26374cfc7e60SRahul Iyer free_page((unsigned long)buf); 26384cfc7e60SRahul Iyer } 26394cfc7e60SRahul Iyer 26404cfc7e60SRahul Iyer /* 26414cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 26424cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 26434cfc7e60SRahul Iyer */ 26444cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 26454cfc7e60SRahul Iyer { 26464cfc7e60SRahul Iyer int len; 26474cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 26484cfc7e60SRahul Iyer struct sock_xprt *transport = 2649067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 26504cfc7e60SRahul Iyer unsigned long headoff; 26514cfc7e60SRahul Iyer unsigned long tailoff; 2652067fb11bSChuck Lever struct page *tailpage; 2653067fb11bSChuck Lever struct msghdr msg = { 2654067fb11bSChuck Lever .msg_flags = MSG_MORE 2655067fb11bSChuck Lever }; 2656067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2657067fb11bSChuck Lever (u32)xbufp->len); 2658067fb11bSChuck Lever struct kvec iov = { 2659067fb11bSChuck Lever .iov_base = &marker, 2660067fb11bSChuck Lever .iov_len = sizeof(marker), 2661067fb11bSChuck Lever }; 26624cfc7e60SRahul Iyer 2663067fb11bSChuck Lever len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len); 2664067fb11bSChuck Lever if (len != iov.iov_len) 2665067fb11bSChuck Lever return -EAGAIN; 26664cfc7e60SRahul Iyer 2667067fb11bSChuck Lever tailpage = NULL; 2668067fb11bSChuck Lever if (xbufp->tail[0].iov_len) 2669067fb11bSChuck Lever tailpage = virt_to_page(xbufp->tail[0].iov_base); 26704cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26714cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 2672067fb11bSChuck Lever len = svc_send_common(transport->sock, xbufp, 26734cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 2674067fb11bSChuck Lever tailpage, tailoff); 2675067fb11bSChuck Lever if (len != xbufp->len) 2676067fb11bSChuck Lever return -EAGAIN; 26774cfc7e60SRahul Iyer return len; 26784cfc7e60SRahul Iyer } 26794cfc7e60SRahul Iyer 26804cfc7e60SRahul Iyer /* 26814cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 26824cfc7e60SRahul Iyer */ 2683adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 26844cfc7e60SRahul Iyer { 26854cfc7e60SRahul Iyer struct svc_xprt *xprt; 26867fc56136SAndrzej Hajda int len; 26874cfc7e60SRahul Iyer 26884cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 26894cfc7e60SRahul Iyer /* 26904cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 26914cfc7e60SRahul Iyer */ 26924cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 26934cfc7e60SRahul Iyer 26944cfc7e60SRahul Iyer /* 26954cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 26964cfc7e60SRahul Iyer * with the fore channel 26974cfc7e60SRahul Iyer */ 2698c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 26994cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 27004cfc7e60SRahul Iyer len = -ENOTCONN; 27014cfc7e60SRahul Iyer else 27024cfc7e60SRahul Iyer len = bc_sendto(req); 27034cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 27044cfc7e60SRahul Iyer 27054cfc7e60SRahul Iyer if (len > 0) 27064cfc7e60SRahul Iyer len = 0; 27074cfc7e60SRahul Iyer 27084cfc7e60SRahul Iyer return len; 27094cfc7e60SRahul Iyer } 27104cfc7e60SRahul Iyer 27114cfc7e60SRahul Iyer /* 27124cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 27134cfc7e60SRahul Iyer */ 27144cfc7e60SRahul Iyer 27154cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 27164cfc7e60SRahul Iyer { 27174cfc7e60SRahul Iyer } 27184cfc7e60SRahul Iyer 27194cfc7e60SRahul Iyer /* 27204cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 27214cfc7e60SRahul Iyer * initiated, we do nothing 27224cfc7e60SRahul Iyer */ 27234cfc7e60SRahul Iyer 27244cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 27254cfc7e60SRahul Iyer { 272647f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 272747f72efaSKinglong Mee 272847f72efaSKinglong Mee xs_xprt_free(xprt); 272947f72efaSKinglong Mee module_put(THIS_MODULE); 27304cfc7e60SRahul Iyer } 27314cfc7e60SRahul Iyer 2732d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2733176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 27344cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2735f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2736a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2737176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2738176e21eeSChuck Lever .set_port = xs_local_set_port, 2739dc107402SJ. Bruce Fields .connect = xs_local_connect, 2740176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2741176e21eeSChuck Lever .buf_free = rpc_free, 2742550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 2743176e21eeSChuck Lever .send_request = xs_local_send_request, 27448ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2745176e21eeSChuck Lever .close = xs_close, 2746a1311d87STrond Myklebust .destroy = xs_destroy, 2747176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2748d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2749d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2750176e21eeSChuck Lever }; 2751176e21eeSChuck Lever 2752d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 275343118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 275412a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 275549e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2756f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2757a9cde23aSChuck Lever .free_slot = xprt_free_slot, 275845160d62SChuck Lever .rpcbind = rpcb_getport_async, 275992200412SChuck Lever .set_port = xs_set_port, 27609903cd1cSChuck Lever .connect = xs_connect, 276102107148SChuck Lever .buf_alloc = rpc_malloc, 276202107148SChuck Lever .buf_free = rpc_free, 2763262965f5SChuck Lever .send_request = xs_udp_send_request, 27648ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 276546c0ee8bSChuck Lever .timer = xs_udp_timer, 2766a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2767262965f5SChuck Lever .close = xs_close, 2768262965f5SChuck Lever .destroy = xs_destroy, 2769262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2770d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2771d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27724a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2773262965f5SChuck Lever }; 2774262965f5SChuck Lever 2775d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 277612a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 27774cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 277836bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 2779a9cde23aSChuck Lever .free_slot = xprt_free_slot, 278045160d62SChuck Lever .rpcbind = rpcb_getport_async, 278192200412SChuck Lever .set_port = xs_set_port, 27820b9e7943STrond Myklebust .connect = xs_connect, 278302107148SChuck Lever .buf_alloc = rpc_malloc, 278402107148SChuck Lever .buf_free = rpc_free, 2785277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 2786262965f5SChuck Lever .send_request = xs_tcp_send_request, 27878ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2788c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 27899903cd1cSChuck Lever .destroy = xs_destroy, 27907196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2791262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2792d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2793d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27944a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 279542e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 279642e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 27976b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 27987402a4feSTrond Myklebust .bc_num_slots = xprt_bc_max_slots, 279942e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 280042e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 280142e5c3e2SChuck Lever #endif 2802a246b010SChuck Lever }; 2803a246b010SChuck Lever 28044cfc7e60SRahul Iyer /* 28054cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 28064cfc7e60SRahul Iyer */ 28074cfc7e60SRahul Iyer 2808d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 28094cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 28104cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 281184e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2812a9cde23aSChuck Lever .free_slot = xprt_free_slot, 28134cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 28144cfc7e60SRahul Iyer .buf_free = bc_free, 28154cfc7e60SRahul Iyer .send_request = bc_send_request, 28168ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 28174cfc7e60SRahul Iyer .close = bc_close, 28184cfc7e60SRahul Iyer .destroy = bc_destroy, 28194cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2820d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2821d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28224a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 28234cfc7e60SRahul Iyer }; 28244cfc7e60SRahul Iyer 282592476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 282692476850SChuck Lever { 282792476850SChuck Lever static const struct sockaddr_in sin = { 282892476850SChuck Lever .sin_family = AF_INET, 282992476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 283092476850SChuck Lever }; 283192476850SChuck Lever static const struct sockaddr_in6 sin6 = { 283292476850SChuck Lever .sin6_family = AF_INET6, 283392476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 283492476850SChuck Lever }; 283592476850SChuck Lever 283692476850SChuck Lever switch (family) { 2837176e21eeSChuck Lever case AF_LOCAL: 2838176e21eeSChuck Lever break; 283992476850SChuck Lever case AF_INET: 284092476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 284192476850SChuck Lever break; 284292476850SChuck Lever case AF_INET6: 284392476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 284492476850SChuck Lever break; 284592476850SChuck Lever default: 284692476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 284792476850SChuck Lever return -EAFNOSUPPORT; 284892476850SChuck Lever } 284992476850SChuck Lever return 0; 285092476850SChuck Lever } 285192476850SChuck Lever 28523c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2853d9ba131dSTrond Myklebust unsigned int slot_table_size, 2854d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2855c8541ecdSChuck Lever { 2856c8541ecdSChuck Lever struct rpc_xprt *xprt; 2857ffc2e518SChuck Lever struct sock_xprt *new; 2858c8541ecdSChuck Lever 285996802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2860c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2861c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2862c8541ecdSChuck Lever } 2863c8541ecdSChuck Lever 2864d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2865d9ba131dSTrond Myklebust max_slot_table_size); 2866bd1722d4SPavel Emelyanov if (xprt == NULL) { 286746121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 286846121cf7SChuck Lever "rpc_xprt\n"); 2869c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2870c8541ecdSChuck Lever } 2871c8541ecdSChuck Lever 2872bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2873edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 287496802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 287596802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2876d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2877fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 287892476850SChuck Lever else { 287992476850SChuck Lever int err; 288092476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 288192476850SChuck Lever (struct sockaddr *)&new->srcaddr); 28822aa13531SStanislav Kinsbursky if (err != 0) { 28832aa13531SStanislav Kinsbursky xprt_free(xprt); 288492476850SChuck Lever return ERR_PTR(err); 288592476850SChuck Lever } 28862aa13531SStanislav Kinsbursky } 2887c8541ecdSChuck Lever 2888c8541ecdSChuck Lever return xprt; 2889c8541ecdSChuck Lever } 2890c8541ecdSChuck Lever 2891176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2892176e21eeSChuck Lever .to_initval = 10 * HZ, 2893176e21eeSChuck Lever .to_maxval = 10 * HZ, 2894176e21eeSChuck Lever .to_retries = 2, 2895176e21eeSChuck Lever }; 2896176e21eeSChuck Lever 2897176e21eeSChuck Lever /** 2898176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2899176e21eeSChuck Lever * @args: rpc transport creation arguments 2900176e21eeSChuck Lever * 2901176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2902176e21eeSChuck Lever */ 2903176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2904176e21eeSChuck Lever { 2905176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2906176e21eeSChuck Lever struct sock_xprt *transport; 2907176e21eeSChuck Lever struct rpc_xprt *xprt; 2908176e21eeSChuck Lever struct rpc_xprt *ret; 2909176e21eeSChuck Lever 2910d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2911d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2912176e21eeSChuck Lever if (IS_ERR(xprt)) 2913176e21eeSChuck Lever return xprt; 2914176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2915176e21eeSChuck Lever 2916176e21eeSChuck Lever xprt->prot = 0; 2917176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2918176e21eeSChuck Lever 2919176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2920176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2921176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2922176e21eeSChuck Lever 2923176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2924176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2925176e21eeSChuck Lever 2926550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 29274f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2928550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 292993dc41bdSNeilBrown 2930176e21eeSChuck Lever switch (sun->sun_family) { 2931176e21eeSChuck Lever case AF_LOCAL: 2932176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2933176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2934176e21eeSChuck Lever sun->sun_path); 2935176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2936176e21eeSChuck Lever goto out_err; 2937176e21eeSChuck Lever } 2938176e21eeSChuck Lever xprt_set_bound(xprt); 2939176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 29407073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 29417073ea87SJ. Bruce Fields if (ret) 29427073ea87SJ. Bruce Fields goto out_err; 2943176e21eeSChuck Lever break; 2944176e21eeSChuck Lever default: 2945176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2946176e21eeSChuck Lever goto out_err; 2947176e21eeSChuck Lever } 2948176e21eeSChuck Lever 2949176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2950176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2951176e21eeSChuck Lever 2952176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2953176e21eeSChuck Lever return xprt; 2954176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2955176e21eeSChuck Lever out_err: 2956315f3812SKinglong Mee xs_xprt_free(xprt); 2957176e21eeSChuck Lever return ret; 2958176e21eeSChuck Lever } 2959176e21eeSChuck Lever 29602881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 29612881ae74STrond Myklebust .to_initval = 5 * HZ, 29622881ae74STrond Myklebust .to_maxval = 30 * HZ, 29632881ae74STrond Myklebust .to_increment = 5 * HZ, 29642881ae74STrond Myklebust .to_retries = 5, 29652881ae74STrond Myklebust }; 29662881ae74STrond Myklebust 29679903cd1cSChuck Lever /** 29689903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 296996802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29709903cd1cSChuck Lever * 29719903cd1cSChuck Lever */ 2972483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2973a246b010SChuck Lever { 29748f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2975c8541ecdSChuck Lever struct rpc_xprt *xprt; 2976c8475461SChuck Lever struct sock_xprt *transport; 29770a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2978a246b010SChuck Lever 2979d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2980d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2981c8541ecdSChuck Lever if (IS_ERR(xprt)) 2982c8541ecdSChuck Lever return xprt; 2983c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2984a246b010SChuck Lever 2985ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2986a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2987a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2988a246b010SChuck Lever 298903bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 299003bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 299103bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2992a246b010SChuck Lever 2993262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2994a246b010SChuck Lever 2995ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2996a246b010SChuck Lever 2997f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 29984f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2999edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3000edc1b01cSTrond Myklebust 30018f9d5b1aSChuck Lever switch (addr->sa_family) { 30028f9d5b1aSChuck Lever case AF_INET: 30038f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30048f9d5b1aSChuck Lever xprt_set_bound(xprt); 30058f9d5b1aSChuck Lever 30069dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 30078f9d5b1aSChuck Lever break; 30088f9d5b1aSChuck Lever case AF_INET6: 30098f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30108f9d5b1aSChuck Lever xprt_set_bound(xprt); 30118f9d5b1aSChuck Lever 30129dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 30138f9d5b1aSChuck Lever break; 30148f9d5b1aSChuck Lever default: 30150a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30160a68b0beSJ. Bruce Fields goto out_err; 30178f9d5b1aSChuck Lever } 30188f9d5b1aSChuck Lever 3019c740eff8SChuck Lever if (xprt_bound(xprt)) 3020c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3021c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3022c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3023c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3024c740eff8SChuck Lever else 3025c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3026c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3027c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3028edb267a6SChuck Lever 3029bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3030c8541ecdSChuck Lever return xprt; 30310a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30320a68b0beSJ. Bruce Fields out_err: 3033315f3812SKinglong Mee xs_xprt_free(xprt); 30340a68b0beSJ. Bruce Fields return ret; 3035a246b010SChuck Lever } 3036a246b010SChuck Lever 30372881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 30382881ae74STrond Myklebust .to_initval = 60 * HZ, 30392881ae74STrond Myklebust .to_maxval = 60 * HZ, 30402881ae74STrond Myklebust .to_retries = 2, 30412881ae74STrond Myklebust }; 30422881ae74STrond Myklebust 30439903cd1cSChuck Lever /** 30449903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 304596802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30469903cd1cSChuck Lever * 30479903cd1cSChuck Lever */ 3048483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3049a246b010SChuck Lever { 30508f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3051c8541ecdSChuck Lever struct rpc_xprt *xprt; 3052c8475461SChuck Lever struct sock_xprt *transport; 30530a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3054b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3055b7993cebSTrond Myklebust 3056b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3057b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3058a246b010SChuck Lever 3059d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3060b7993cebSTrond Myklebust max_slot_table_size); 3061c8541ecdSChuck Lever if (IS_ERR(xprt)) 3062c8541ecdSChuck Lever return xprt; 3063c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3064a246b010SChuck Lever 3065ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3066808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3067a246b010SChuck Lever 306803bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 306903bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 307003bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3071a246b010SChuck Lever 3072262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3073ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3074a246b010SChuck Lever 30753851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 30767196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 30777196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 30783851f1cdSTrond Myklebust 3079c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 30804f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3081edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3082edc1b01cSTrond Myklebust 30838f9d5b1aSChuck Lever switch (addr->sa_family) { 30848f9d5b1aSChuck Lever case AF_INET: 30858f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30868f9d5b1aSChuck Lever xprt_set_bound(xprt); 30878f9d5b1aSChuck Lever 30889dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 30898f9d5b1aSChuck Lever break; 30908f9d5b1aSChuck Lever case AF_INET6: 30918f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30928f9d5b1aSChuck Lever xprt_set_bound(xprt); 30938f9d5b1aSChuck Lever 30949dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 30958f9d5b1aSChuck Lever break; 30968f9d5b1aSChuck Lever default: 30970a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30980a68b0beSJ. Bruce Fields goto out_err; 30998f9d5b1aSChuck Lever } 31008f9d5b1aSChuck Lever 3101c740eff8SChuck Lever if (xprt_bound(xprt)) 3102c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3103c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3104c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3105c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3106c740eff8SChuck Lever else 3107c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3108c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3109c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3110c740eff8SChuck Lever 3111bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3112c8541ecdSChuck Lever return xprt; 31130a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31140a68b0beSJ. Bruce Fields out_err: 3115315f3812SKinglong Mee xs_xprt_free(xprt); 31160a68b0beSJ. Bruce Fields return ret; 3117a246b010SChuck Lever } 3118282b32e1SChuck Lever 3119f300babaSAlexandros Batsakis /** 3120f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3121f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3122f300babaSAlexandros Batsakis * 3123f300babaSAlexandros Batsakis */ 3124f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3125f300babaSAlexandros Batsakis { 3126f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3127f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3128f300babaSAlexandros Batsakis struct sock_xprt *transport; 3129f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 31300a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3131f300babaSAlexandros Batsakis 3132d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3133d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3134f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3135f300babaSAlexandros Batsakis return xprt; 3136f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3137f300babaSAlexandros Batsakis 3138f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3139f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3140f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3141f300babaSAlexandros Batsakis 3142f300babaSAlexandros Batsakis /* backchannel */ 3143f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3144f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3145f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3146f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3147f300babaSAlexandros Batsakis 3148f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3149f300babaSAlexandros Batsakis 3150f300babaSAlexandros Batsakis switch (addr->sa_family) { 3151f300babaSAlexandros Batsakis case AF_INET: 3152f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3153f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3154f300babaSAlexandros Batsakis break; 3155f300babaSAlexandros Batsakis case AF_INET6: 3156f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3157f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3158f300babaSAlexandros Batsakis break; 3159f300babaSAlexandros Batsakis default: 31600a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31610a68b0beSJ. Bruce Fields goto out_err; 3162f300babaSAlexandros Batsakis } 3163f300babaSAlexandros Batsakis 3164f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3165f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3166f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3167f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3168f300babaSAlexandros Batsakis 3169f300babaSAlexandros Batsakis /* 317099de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 317128303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 317228303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 317328303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 317499de8ea9SJ. Bruce Fields */ 317599de8ea9SJ. Bruce Fields xprt_get(xprt); 317699de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 317799de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 317899de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 317999de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 318099de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 318199de8ea9SJ. Bruce Fields 318299de8ea9SJ. Bruce Fields /* 3183f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3184f300babaSAlexandros Batsakis * the xprt status to connected 3185f300babaSAlexandros Batsakis */ 3186f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3187f300babaSAlexandros Batsakis 3188f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3189f300babaSAlexandros Batsakis return xprt; 3190642aab58SKinglong Mee 3191642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 319239a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 319399de8ea9SJ. Bruce Fields xprt_put(xprt); 31940a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31950a68b0beSJ. Bruce Fields out_err: 3196315f3812SKinglong Mee xs_xprt_free(xprt); 31970a68b0beSJ. Bruce Fields return ret; 3198f300babaSAlexandros Batsakis } 3199f300babaSAlexandros Batsakis 3200176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3201176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3202176e21eeSChuck Lever .name = "named UNIX socket", 3203176e21eeSChuck Lever .owner = THIS_MODULE, 3204176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3205176e21eeSChuck Lever .setup = xs_setup_local, 3206176e21eeSChuck Lever }; 3207176e21eeSChuck Lever 3208bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3209bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3210bc25571eS\"Talpey, Thomas\ .name = "udp", 3211bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3212f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3213bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3214bc25571eS\"Talpey, Thomas\ }; 3215bc25571eS\"Talpey, Thomas\ 3216bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3217bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3218bc25571eS\"Talpey, Thomas\ .name = "tcp", 3219bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3220f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3221bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3222bc25571eS\"Talpey, Thomas\ }; 3223bc25571eS\"Talpey, Thomas\ 3224f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3225f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3226f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3227f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3228f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3229f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3230f300babaSAlexandros Batsakis }; 3231f300babaSAlexandros Batsakis 3232282b32e1SChuck Lever /** 3233bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3234282b32e1SChuck Lever * 3235282b32e1SChuck Lever */ 3236282b32e1SChuck Lever int init_socket_xprt(void) 3237282b32e1SChuck Lever { 32382b1bec5fSEric W. Biederman if (!sunrpc_table_header) 32390b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3240fbf76683SChuck Lever 3241176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3242bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3243bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3244f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3245bc25571eS\"Talpey, Thomas\ 3246282b32e1SChuck Lever return 0; 3247282b32e1SChuck Lever } 3248282b32e1SChuck Lever 3249282b32e1SChuck Lever /** 3250bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3251282b32e1SChuck Lever * 3252282b32e1SChuck Lever */ 3253282b32e1SChuck Lever void cleanup_socket_xprt(void) 3254282b32e1SChuck Lever { 3255fbf76683SChuck Lever if (sunrpc_table_header) { 3256fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3257fbf76683SChuck Lever sunrpc_table_header = NULL; 3258fbf76683SChuck Lever } 3259bc25571eS\"Talpey, Thomas\ 3260176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3261bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3262bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3263f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3264282b32e1SChuck Lever } 3265cbf11071STrond Myklebust 32669bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 32679bbb9e5aSRusty Russell const struct kernel_param *kp, 3268cbf11071STrond Myklebust unsigned int min, unsigned int max) 3269cbf11071STrond Myklebust { 327000cfaa94SDaniel Walter unsigned int num; 3271cbf11071STrond Myklebust int ret; 3272cbf11071STrond Myklebust 3273cbf11071STrond Myklebust if (!val) 3274cbf11071STrond Myklebust return -EINVAL; 327500cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 32769761a246SDan Carpenter if (ret) 32779761a246SDan Carpenter return ret; 32789761a246SDan Carpenter if (num < min || num > max) 3279cbf11071STrond Myklebust return -EINVAL; 3280cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3281cbf11071STrond Myklebust return 0; 3282cbf11071STrond Myklebust } 3283cbf11071STrond Myklebust 32849bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3285cbf11071STrond Myklebust { 3286cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3287cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3288cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3289cbf11071STrond Myklebust } 3290cbf11071STrond Myklebust 32919c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 32929bbb9e5aSRusty Russell .set = param_set_portnr, 32939bbb9e5aSRusty Russell .get = param_get_uint, 32949bbb9e5aSRusty Russell }; 32959bbb9e5aSRusty Russell 3296cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3297cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3298cbf11071STrond Myklebust 3299cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3300cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3301cbf11071STrond Myklebust 33029bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 33039bbb9e5aSRusty Russell const struct kernel_param *kp) 3304cbf11071STrond Myklebust { 3305cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3306cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3307cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3308cbf11071STrond Myklebust } 3309cbf11071STrond Myklebust 33109c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 33119bbb9e5aSRusty Russell .set = param_set_slot_table_size, 33129bbb9e5aSRusty Russell .get = param_get_uint, 33139bbb9e5aSRusty Russell }; 33149bbb9e5aSRusty Russell 3315cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3316cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3317cbf11071STrond Myklebust 3318d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3319d9ba131dSTrond Myklebust const struct kernel_param *kp) 3320d9ba131dSTrond Myklebust { 3321d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3322d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3323d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3324d9ba131dSTrond Myklebust } 3325d9ba131dSTrond Myklebust 33269c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3327d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3328d9ba131dSTrond Myklebust .get = param_get_uint, 3329d9ba131dSTrond Myklebust }; 3330d9ba131dSTrond Myklebust 3331d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3332d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3333d9ba131dSTrond Myklebust 3334cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3335cbf11071STrond Myklebust slot_table_size, 0644); 3336d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3337d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3338cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3339cbf11071STrond Myklebust slot_table_size, 0644); 3340