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 } 56545835a63STrond Myklebust if (transport->recv.copied && !req->rq_private_buf.len) 56645835a63STrond 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); 57145835a63STrond Myklebust else 57245835a63STrond 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); 59445835a63STrond 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); 60645835a63STrond Myklebust else 60745835a63STrond 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 12532118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 12542118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12552118071dSTrond Myklebust goto out; 12562118071dSTrond Myklebust 12574f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1258af84537dSBenjamin Coddington transport->xprt_err = -sk->sk_err; 1259af84537dSBenjamin Coddington if (transport->xprt_err == 0) 12602118071dSTrond Myklebust goto out; 12612118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 1262af84537dSBenjamin Coddington xprt, -transport->xprt_err); 1263af84537dSBenjamin Coddington trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); 1264af84537dSBenjamin Coddington 1265af84537dSBenjamin Coddington /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ 1266af84537dSBenjamin Coddington smp_mb__before_atomic(); 12674f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 12682118071dSTrond Myklebust out: 12692118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 12702a9e1cfaSTrond Myklebust } 12712a9e1cfaSTrond Myklebust 1272fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1273a246b010SChuck Lever { 1274ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1275ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12766cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1277a73881c9STrond Myklebust struct file *filp = transport->file; 1278a246b010SChuck Lever 1279fe315e76SChuck Lever if (sk == NULL) 1280fe315e76SChuck Lever return; 12819903cd1cSChuck Lever 1282264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1283264d1df3SJeff Layton sk_clear_memalloc(sk); 1284264d1df3SJeff Layton 128509939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 128609939204STrond Myklebust 1287edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1288a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1289ee0ac0c2SChuck Lever transport->inet = NULL; 1290ee0ac0c2SChuck Lever transport->sock = NULL; 1291a73881c9STrond Myklebust transport->file = NULL; 1292a246b010SChuck Lever 1293a246b010SChuck Lever sk->sk_user_data = NULL; 12942a9e1cfaSTrond Myklebust 12952a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12960c78789eSTrond Myklebust xprt_clear_connected(xprt); 1297a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 12986cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1299ae053551STrond Myklebust /* Reset stream record info */ 1300ae053551STrond Myklebust xs_stream_reset_connect(transport); 1301edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1302a246b010SChuck Lever 13036cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1304a73881c9STrond Myklebust fput(filp); 13050445f92cSTrond Myklebust 13060445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1307fe315e76SChuck Lever } 1308fe315e76SChuck Lever 1309fe315e76SChuck Lever /** 1310fe315e76SChuck Lever * xs_close - close a socket 1311fe315e76SChuck Lever * @xprt: transport 1312fe315e76SChuck Lever * 1313fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1314fe315e76SChuck Lever * on the server we want to save. 1315f75e6745STrond Myklebust * 1316f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1317f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1318fe315e76SChuck Lever */ 1319fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1320fe315e76SChuck Lever { 1321fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1322fe315e76SChuck Lever 1323fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1324fe315e76SChuck Lever 1325fe315e76SChuck Lever xs_reset_transport(transport); 132661d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1327a246b010SChuck Lever } 1328a246b010SChuck Lever 13294a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13304a068258SChuck Lever { 13314a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13324a068258SChuck Lever xprt); 13334a068258SChuck Lever xprt_disconnect_done(xprt); 13344a068258SChuck Lever } 13354a068258SChuck Lever 1336315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1337315f3812SKinglong Mee { 1338315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1339315f3812SKinglong Mee xprt_free(xprt); 1340315f3812SKinglong Mee } 1341315f3812SKinglong Mee 13429903cd1cSChuck Lever /** 13439903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13449903cd1cSChuck Lever * @xprt: doomed transport 13459903cd1cSChuck Lever * 13469903cd1cSChuck Lever */ 13479903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1348a246b010SChuck Lever { 134903c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 135003c78827STrond Myklebust struct sock_xprt, xprt); 13519903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13529903cd1cSChuck Lever 135303c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1354a1311d87STrond Myklebust xs_close(xprt); 1355edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1356b5e92419STrond Myklebust cancel_work_sync(&transport->error_worker); 1357315f3812SKinglong Mee xs_xprt_free(xprt); 1358a1311d87STrond Myklebust module_put(THIS_MODULE); 1359a246b010SChuck Lever } 1360a246b010SChuck Lever 13619903cd1cSChuck Lever /** 1362f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1363f9b2ee71STrond Myklebust * @xprt: transport 1364f9b2ee71STrond Myklebust * @sk: socket 1365f9b2ee71STrond Myklebust * @skb: skbuff 13669903cd1cSChuck Lever * 1367a246b010SChuck Lever */ 1368f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1369f9b2ee71STrond Myklebust struct sock *sk, 1370f9b2ee71STrond Myklebust struct sk_buff *skb) 1371a246b010SChuck Lever { 1372a246b010SChuck Lever struct rpc_task *task; 1373a246b010SChuck Lever struct rpc_rqst *rovr; 1374f9b2ee71STrond Myklebust int repsize, copied; 1375d8ed029dSAlexey Dobriyan u32 _xid; 1376d8ed029dSAlexey Dobriyan __be32 *xp; 1377a246b010SChuck Lever 13781da8c681SWillem de Bruijn repsize = skb->len; 1379a246b010SChuck Lever if (repsize < 4) { 13809903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1381f9b2ee71STrond Myklebust return; 1382a246b010SChuck Lever } 1383a246b010SChuck Lever 1384a246b010SChuck Lever /* Copy the XID from the skb... */ 13851da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1386a246b010SChuck Lever if (xp == NULL) 1387f9b2ee71STrond Myklebust return; 1388a246b010SChuck Lever 1389a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 139075c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1391a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1392a246b010SChuck Lever if (!rovr) 1393a246b010SChuck Lever goto out_unlock; 1394729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1395ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 139675c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1397a246b010SChuck Lever task = rovr->rq_task; 1398a246b010SChuck Lever 1399a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1400a246b010SChuck Lever copied = repsize; 1401a246b010SChuck Lever 1402a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 14031781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 140475c84151STrond Myklebust spin_lock(&xprt->queue_lock); 14050afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1406729749bbSTrond Myklebust goto out_unpin; 14071781f7f5SHerbert Xu } 14081781f7f5SHerbert Xu 1409a246b010SChuck Lever 1410b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 14116a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1412b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 141375c84151STrond Myklebust spin_lock(&xprt->queue_lock); 14141570c1e4SChuck Lever xprt_complete_rqst(task, copied); 14150afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1416729749bbSTrond Myklebust out_unpin: 1417729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1418a246b010SChuck Lever out_unlock: 141975c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1420f9b2ee71STrond Myklebust } 1421f9b2ee71STrond Myklebust 1422f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1423f9b2ee71STrond Myklebust { 1424f9b2ee71STrond Myklebust struct sk_buff *skb; 1425f9b2ee71STrond Myklebust struct sock *sk; 1426f9b2ee71STrond Myklebust int err; 1427f9b2ee71STrond Myklebust 1428f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1429f9b2ee71STrond Myklebust sk = transport->inet; 1430f9b2ee71STrond Myklebust if (sk == NULL) 1431f9b2ee71STrond Myklebust goto out; 1432f9b2ee71STrond Myklebust for (;;) { 14337c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 14344f546149STrond Myklebust if (skb == NULL) 14354f546149STrond Myklebust break; 1436f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1437850cbaddSPaolo Abeni consume_skb(skb); 14380af3442aSTrond Myklebust cond_resched(); 1439f9b2ee71STrond Myklebust } 14400ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1441a246b010SChuck Lever out: 1442f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1443f9b2ee71STrond Myklebust } 1444f9b2ee71STrond Myklebust 1445f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1446f9b2ee71STrond Myklebust { 1447f9b2ee71STrond Myklebust struct sock_xprt *transport = 1448f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1449a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1450a1231fdaSTrond Myklebust 1451f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1452a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1453f9b2ee71STrond Myklebust } 1454f9b2ee71STrond Myklebust 1455f9b2ee71STrond Myklebust /** 1456f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1457f9b2ee71STrond Myklebust * @sk: socket with data to read 1458f9b2ee71STrond Myklebust * 1459f9b2ee71STrond Myklebust */ 1460f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1461f9b2ee71STrond Myklebust { 1462f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1463f9b2ee71STrond Myklebust 1464f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1465f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1466f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1467f9b2ee71STrond Myklebust if (xprt != NULL) { 1468f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1469f9b2ee71STrond Myklebust struct sock_xprt, xprt); 14705157b956STrond Myklebust transport->old_data_ready(sk); 14715157b956STrond Myklebust /* Any data means we had a useful conversation, so 14725157b956STrond Myklebust * then we don't need to delay the next reconnect 14735157b956STrond Myklebust */ 14745157b956STrond Myklebust if (xprt->reestablish_timeout) 14755157b956STrond Myklebust xprt->reestablish_timeout = 0; 147642d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 147740a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1478f9b2ee71STrond Myklebust } 1479f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1480a246b010SChuck Lever } 1481a246b010SChuck Lever 1482a519fc7aSTrond Myklebust /* 1483a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1484a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1485a519fc7aSTrond Myklebust */ 1486a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1487a519fc7aSTrond Myklebust { 1488a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1489a519fc7aSTrond Myklebust } 1490a519fc7aSTrond Myklebust 14919e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14926b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14936b26cc8cSChuck Lever { 14946b26cc8cSChuck Lever return PAGE_SIZE; 14956b26cc8cSChuck Lever } 14969e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 149744b98efdSRicardo Labiaga 14989903cd1cSChuck Lever /** 14999903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 15009903cd1cSChuck Lever * @sk: socket whose state has changed 15019903cd1cSChuck Lever * 15029903cd1cSChuck Lever */ 15039903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1504a246b010SChuck Lever { 1505a246b010SChuck Lever struct rpc_xprt *xprt; 15060fdea1e8STrond Myklebust struct sock_xprt *transport; 1507a246b010SChuck Lever 1508f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1509a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1510a246b010SChuck Lever goto out; 15119903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1512669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1513a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1514a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1515669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1516669502ffSAndy Chittenden sk->sk_shutdown); 1517a246b010SChuck Lever 15180fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 151940b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1520a246b010SChuck Lever switch (sk->sk_state) { 1521a246b010SChuck Lever case TCP_ESTABLISHED: 1522a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15238b71798cSTrond Myklebust xprt->connect_cookie++; 15240fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15250fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 152651971139SChuck Lever 15273968a8a5SChuck Lever xprt->stat.connect_count++; 15283968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15293968a8a5SChuck Lever xprt->stat.connect_start; 15304f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1531a246b010SChuck Lever } 1532a246b010SChuck Lever break; 15333b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15343b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15357c1d71cfSTrond Myklebust xprt->connect_cookie++; 1536663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15373b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15384e857c58SPeter Zijlstra smp_mb__before_atomic(); 15393b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1540ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15414e857c58SPeter Zijlstra smp_mb__after_atomic(); 1542a246b010SChuck Lever break; 1543632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15443b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15457c1d71cfSTrond Myklebust xprt->connect_cookie++; 1546d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15474f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1548e9d47639SGustavo A. R. Silva /* fall through */ 1549663b8858STrond Myklebust case TCP_CLOSING: 1550663b8858STrond Myklebust /* 1551663b8858STrond Myklebust * If the server closed down the connection, make sure that 1552663b8858STrond Myklebust * we back off before reconnecting 1553663b8858STrond Myklebust */ 1554663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1555663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15563b948ae5STrond Myklebust break; 15573b948ae5STrond Myklebust case TCP_LAST_ACK: 1558670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15594e857c58SPeter Zijlstra smp_mb__before_atomic(); 15603b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15614e857c58SPeter Zijlstra smp_mb__after_atomic(); 15623b948ae5STrond Myklebust break; 15633b948ae5STrond Myklebust case TCP_CLOSE: 15640fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15650fdea1e8STrond Myklebust &transport->sock_state)) 15660fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15679b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15689b30889cSTrond Myklebust /* Trigger the socket release */ 15694f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1570a246b010SChuck Lever } 1571a246b010SChuck Lever out: 1572f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1573a246b010SChuck Lever } 1574a246b010SChuck Lever 15751f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15761f0fa154SIlpo Järvinen { 157713331a55STrond Myklebust struct socket_wq *wq; 15784f8943f8STrond Myklebust struct sock_xprt *transport; 15791f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15801f0fa154SIlpo Järvinen 158113331a55STrond Myklebust if (!sk->sk_socket) 15821f0fa154SIlpo Järvinen return; 158313331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15841f0fa154SIlpo Järvinen 15851f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15861f0fa154SIlpo Järvinen return; 15874f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 158813331a55STrond Myklebust rcu_read_lock(); 158913331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 159013331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 159113331a55STrond Myklebust goto out; 15921f0fa154SIlpo Järvinen 15934f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1594c544577dSTrond Myklebust sk->sk_write_pending--; 159513331a55STrond Myklebust out: 159613331a55STrond Myklebust rcu_read_unlock(); 15971f0fa154SIlpo Järvinen } 15981f0fa154SIlpo Järvinen 15992a9e1cfaSTrond Myklebust /** 1600c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1601c7b2cae8SChuck Lever * becomes available 16029903cd1cSChuck Lever * @sk: socket whose state has changed 16039903cd1cSChuck Lever * 1604a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1605a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1606c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1607a246b010SChuck Lever * with a bunch of small requests. 1608a246b010SChuck Lever */ 1609c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1610a246b010SChuck Lever { 1611f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1612c7b2cae8SChuck Lever 1613c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16141f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16151f0fa154SIlpo Järvinen xs_write_space(sk); 1616c7b2cae8SChuck Lever 1617f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1618c7b2cae8SChuck Lever } 1619c7b2cae8SChuck Lever 1620c7b2cae8SChuck Lever /** 1621c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1622c7b2cae8SChuck Lever * becomes available 1623c7b2cae8SChuck Lever * @sk: socket whose state has changed 1624c7b2cae8SChuck Lever * 1625c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1626c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1627c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1628c7b2cae8SChuck Lever * with a bunch of small requests. 1629c7b2cae8SChuck Lever */ 1630c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1631c7b2cae8SChuck Lever { 1632f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1633c7b2cae8SChuck Lever 1634c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 163564dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16361f0fa154SIlpo Järvinen xs_write_space(sk); 1637c7b2cae8SChuck Lever 1638f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1639a246b010SChuck Lever } 1640a246b010SChuck Lever 1641470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1642a246b010SChuck Lever { 1643ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1644ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1645a246b010SChuck Lever 16467c6e066eSChuck Lever if (transport->rcvsize) { 1647a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16487c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1649a246b010SChuck Lever } 16507c6e066eSChuck Lever if (transport->sndsize) { 1651a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16527c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1653a246b010SChuck Lever sk->sk_write_space(sk); 1654a246b010SChuck Lever } 1655a246b010SChuck Lever } 1656a246b010SChuck Lever 165743118c29SChuck Lever /** 1658470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 165943118c29SChuck Lever * @xprt: generic transport 1660470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1661470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 166243118c29SChuck Lever * 1663470056c2SChuck Lever * Set socket send and receive buffer size limits. 166443118c29SChuck Lever */ 1665470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 166643118c29SChuck Lever { 16677c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16687c6e066eSChuck Lever 16697c6e066eSChuck Lever transport->sndsize = 0; 1670470056c2SChuck Lever if (sndsize) 16717c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16727c6e066eSChuck Lever transport->rcvsize = 0; 1673470056c2SChuck Lever if (rcvsize) 16747c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1675470056c2SChuck Lever 1676470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 167743118c29SChuck Lever } 167843118c29SChuck Lever 167946c0ee8bSChuck Lever /** 168046c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1681acf0a39fSChuck Lever * @xprt: controlling transport 168246c0ee8bSChuck Lever * @task: task that timed out 168346c0ee8bSChuck Lever * 168446c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 168546c0ee8bSChuck Lever */ 16866a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 168746c0ee8bSChuck Lever { 1688b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 16896a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1690b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 169146c0ee8bSChuck Lever } 169246c0ee8bSChuck Lever 1693826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1694b85d8806SChuck Lever { 1695826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1696826799e6SJ. Bruce Fields unsigned short range; 1697826799e6SJ. Bruce Fields unsigned short rand; 1698826799e6SJ. Bruce Fields 1699826799e6SJ. Bruce Fields if (max < min) 1700826799e6SJ. Bruce Fields return -EADDRINUSE; 1701826799e6SJ. Bruce Fields range = max - min + 1; 1702826799e6SJ. Bruce Fields rand = (unsigned short) prandom_u32() % range; 1703826799e6SJ. Bruce Fields return rand + min; 1704b85d8806SChuck Lever } 1705b85d8806SChuck Lever 170692200412SChuck Lever /** 17074dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17084dda9c8aSTrond Myklebust * @sock: socket 17094dda9c8aSTrond Myklebust * 17104dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17114dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17124dda9c8aSTrond Myklebust */ 17134dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17144dda9c8aSTrond Myklebust { 1715402e23b4STrond Myklebust int opt = 1; 17164dda9c8aSTrond Myklebust 1717402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1718402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17194dda9c8aSTrond Myklebust } 17204dda9c8aSTrond Myklebust 17214dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17224dda9c8aSTrond Myklebust { 17234dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17244dda9c8aSTrond Myklebust unsigned short port = 0; 17254dda9c8aSTrond Myklebust 17269b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 17274dda9c8aSTrond Myklebust goto out; 17284dda9c8aSTrond Myklebust switch (buf.ss_family) { 17294dda9c8aSTrond Myklebust case AF_INET6: 17304dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17314dda9c8aSTrond Myklebust break; 17324dda9c8aSTrond Myklebust case AF_INET: 17334dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17344dda9c8aSTrond Myklebust } 17354dda9c8aSTrond Myklebust out: 17364dda9c8aSTrond Myklebust return port; 17374dda9c8aSTrond Myklebust } 17384dda9c8aSTrond Myklebust 17394dda9c8aSTrond Myklebust /** 174092200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 174192200412SChuck Lever * @xprt: generic transport 174292200412SChuck Lever * @port: new port number 174392200412SChuck Lever * 174492200412SChuck Lever */ 174592200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 174692200412SChuck Lever { 174792200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1748c4efcb1dSChuck Lever 17499dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17509dc3b095SChuck Lever xs_update_peer_port(xprt); 175192200412SChuck Lever } 175292200412SChuck Lever 17534dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17544dda9c8aSTrond Myklebust { 1755*e6237b6fSTrond Myklebust if (transport->srcport == 0 && transport->xprt.reuseport) 17564dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17574dda9c8aSTrond Myklebust } 17584dda9c8aSTrond Myklebust 1759826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 176067a391d7STrond Myklebust { 1761826799e6SJ. Bruce Fields int port = transport->srcport; 176267a391d7STrond Myklebust 176367a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 176467a391d7STrond Myklebust port = xs_get_random_port(); 176567a391d7STrond Myklebust return port; 176667a391d7STrond Myklebust } 176767a391d7STrond Myklebust 1768baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 176967a391d7STrond Myklebust { 1770fbfffbd5SChuck Lever if (transport->srcport != 0) 1771fbfffbd5SChuck Lever transport->srcport = 0; 177267a391d7STrond Myklebust if (!transport->xprt.resvport) 177367a391d7STrond Myklebust return 0; 177467a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 177567a391d7STrond Myklebust return xprt_max_resvport; 177667a391d7STrond Myklebust return --port; 177767a391d7STrond Myklebust } 1778beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1779a246b010SChuck Lever { 1780beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 178167a391d7STrond Myklebust int err, nloop = 0; 1782826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 178367a391d7STrond Myklebust unsigned short last; 1784a246b010SChuck Lever 17850f7a622cSChris Perl /* 17860f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17870f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17880f7a622cSChris Perl * port selection happen implicitly when the socket is used 17890f7a622cSChris Perl * (for example at connect time). 17900f7a622cSChris Perl * 17910f7a622cSChris Perl * This ensures that we can continue to establish TCP 17920f7a622cSChris Perl * connections even when all local ephemeral ports are already 17930f7a622cSChris Perl * a part of some TCP connection. This makes no difference 17940f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 17950f7a622cSChris Perl * 17960f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17970f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17980f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17990f7a622cSChris Perl */ 1800826799e6SJ. Bruce Fields if (port <= 0) 1801826799e6SJ. Bruce Fields return port; 18020f7a622cSChris Perl 1803beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1804a246b010SChuck Lever do { 1805beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1806e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1807beb59b68SPavel Emelyanov transport->xprt.addrlen); 1808a246b010SChuck Lever if (err == 0) { 1809fbfffbd5SChuck Lever transport->srcport = port; 1810d3bc9a1dSFrank van Maarseveen break; 1811a246b010SChuck Lever } 181267a391d7STrond Myklebust last = port; 1813baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 181467a391d7STrond Myklebust if (port > last) 181567a391d7STrond Myklebust nloop++; 181667a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1817beb59b68SPavel Emelyanov 18184232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1819beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1820beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1821beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1822beb59b68SPavel Emelyanov else 1823beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1824beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18257dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1826a246b010SChuck Lever return err; 1827a246b010SChuck Lever } 1828a246b010SChuck Lever 1829176e21eeSChuck Lever /* 1830176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1831176e21eeSChuck Lever */ 1832176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1833176e21eeSChuck Lever { 1834fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1835176e21eeSChuck Lever } 1836176e21eeSChuck Lever 1837176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1838176e21eeSChuck Lever { 1839176e21eeSChuck Lever } 1840a246b010SChuck Lever 1841ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1842ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1843ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1844ed07536eSPeter Zijlstra 1845176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1846176e21eeSChuck Lever { 1847176e21eeSChuck Lever struct sock *sk = sock->sk; 1848176e21eeSChuck Lever 1849176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1850176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1851176e21eeSChuck Lever } 1852176e21eeSChuck Lever 18538945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1854ed07536eSPeter Zijlstra { 1855ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18568945ee5eSChuck Lever 18578945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 18588945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1859ed07536eSPeter Zijlstra } 18608945ee5eSChuck Lever 18618945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18628945ee5eSChuck Lever { 18638945ee5eSChuck Lever struct sock *sk = sock->sk; 18648945ee5eSChuck Lever 18658945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 18668945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1867ed07536eSPeter Zijlstra } 18686bc9638aSPavel Emelyanov 18696bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18706bc9638aSPavel Emelyanov { 1871fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18721b7a1819SWeston Andros Adamson return; 18731b7a1819SWeston Andros Adamson 18744232e863SChuck Lever switch (family) { 1875176e21eeSChuck Lever case AF_LOCAL: 1876176e21eeSChuck Lever xs_reclassify_socketu(sock); 1877176e21eeSChuck Lever break; 18784232e863SChuck Lever case AF_INET: 18796bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18804232e863SChuck Lever break; 18814232e863SChuck Lever case AF_INET6: 18826bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18834232e863SChuck Lever break; 18844232e863SChuck Lever } 18856bc9638aSPavel Emelyanov } 1886ed07536eSPeter Zijlstra #else 18876bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18886bc9638aSPavel Emelyanov { 18896bc9638aSPavel Emelyanov } 1890ed07536eSPeter Zijlstra #endif 1891ed07536eSPeter Zijlstra 189293dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 189393dc41bdSNeilBrown { 189493dc41bdSNeilBrown } 189593dc41bdSNeilBrown 18966bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18974dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18984dda9c8aSTrond Myklebust int protocol, bool reuseport) 189922f79326SPavel Emelyanov { 1900a73881c9STrond Myklebust struct file *filp; 190122f79326SPavel Emelyanov struct socket *sock; 190222f79326SPavel Emelyanov int err; 190322f79326SPavel Emelyanov 19046bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 190522f79326SPavel Emelyanov if (err < 0) { 190622f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 190722f79326SPavel Emelyanov protocol, -err); 190822f79326SPavel Emelyanov goto out; 190922f79326SPavel Emelyanov } 19106bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 191122f79326SPavel Emelyanov 19124dda9c8aSTrond Myklebust if (reuseport) 19134dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19144dda9c8aSTrond Myklebust 19154cea288aSBen Hutchings err = xs_bind(transport, sock); 19164cea288aSBen Hutchings if (err) { 191722f79326SPavel Emelyanov sock_release(sock); 191822f79326SPavel Emelyanov goto out; 191922f79326SPavel Emelyanov } 192022f79326SPavel Emelyanov 1921a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1922a73881c9STrond Myklebust if (IS_ERR(filp)) 1923a73881c9STrond Myklebust return ERR_CAST(filp); 1924a73881c9STrond Myklebust transport->file = filp; 1925a73881c9STrond Myklebust 192622f79326SPavel Emelyanov return sock; 192722f79326SPavel Emelyanov out: 192822f79326SPavel Emelyanov return ERR_PTR(err); 192922f79326SPavel Emelyanov } 193022f79326SPavel Emelyanov 1931176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1932176e21eeSChuck Lever struct socket *sock) 1933176e21eeSChuck Lever { 1934176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1935176e21eeSChuck Lever xprt); 1936176e21eeSChuck Lever 1937176e21eeSChuck Lever if (!transport->inet) { 1938176e21eeSChuck Lever struct sock *sk = sock->sk; 1939176e21eeSChuck Lever 1940176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1941176e21eeSChuck Lever 1942176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1943176e21eeSChuck Lever 1944176e21eeSChuck Lever sk->sk_user_data = xprt; 1945a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1946176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1947b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 19482118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1949176e21eeSChuck Lever 1950176e21eeSChuck Lever xprt_clear_connected(xprt); 1951176e21eeSChuck Lever 1952176e21eeSChuck Lever /* Reset to new socket */ 1953176e21eeSChuck Lever transport->sock = sock; 1954176e21eeSChuck Lever transport->inet = sk; 1955176e21eeSChuck Lever 1956176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1957176e21eeSChuck Lever } 1958176e21eeSChuck Lever 1959ae053551STrond Myklebust xs_stream_start_connect(transport); 19606c7a64e5STrond Myklebust 1961176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1962176e21eeSChuck Lever } 1963176e21eeSChuck Lever 1964176e21eeSChuck Lever /** 1965176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1966176e21eeSChuck Lever * @transport: socket transport to connect 1967176e21eeSChuck Lever */ 1968dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1969176e21eeSChuck Lever { 1970176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1971a73881c9STrond Myklebust struct file *filp; 1972176e21eeSChuck Lever struct socket *sock; 1973176e21eeSChuck Lever int status = -EIO; 1974176e21eeSChuck Lever 1975176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1976176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1977176e21eeSChuck Lever if (status < 0) { 1978176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1979176e21eeSChuck Lever "transport socket (%d).\n", -status); 1980176e21eeSChuck Lever goto out; 1981176e21eeSChuck Lever } 1982d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1983176e21eeSChuck Lever 1984a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1985a73881c9STrond Myklebust if (IS_ERR(filp)) { 1986a73881c9STrond Myklebust status = PTR_ERR(filp); 1987a73881c9STrond Myklebust goto out; 1988a73881c9STrond Myklebust } 1989a73881c9STrond Myklebust transport->file = filp; 1990a73881c9STrond Myklebust 1991176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1992176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1993176e21eeSChuck Lever 1994176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 199540b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1996176e21eeSChuck Lever switch (status) { 1997176e21eeSChuck Lever case 0: 1998176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1999176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20003968a8a5SChuck Lever xprt->stat.connect_count++; 20013968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 20023968a8a5SChuck Lever xprt->stat.connect_start; 2003176e21eeSChuck Lever xprt_set_connected(xprt); 20043601c4a9STrond Myklebust case -ENOBUFS: 2005176e21eeSChuck Lever break; 2006176e21eeSChuck Lever case -ENOENT: 2007176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2008176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2009176e21eeSChuck Lever break; 20104a20a988STrond Myklebust case -ECONNREFUSED: 20114a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20124a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20134a20a988STrond Myklebust break; 2014176e21eeSChuck Lever default: 2015176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2016176e21eeSChuck Lever __func__, -status, 2017176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2018176e21eeSChuck Lever } 2019176e21eeSChuck Lever 2020176e21eeSChuck Lever out: 2021176e21eeSChuck Lever xprt_clear_connecting(xprt); 2022176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2023dc107402SJ. Bruce Fields return status; 2024dc107402SJ. Bruce Fields } 2025dc107402SJ. Bruce Fields 2026b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2027dc107402SJ. Bruce Fields { 2028dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2029dc107402SJ. Bruce Fields int ret; 2030dc107402SJ. Bruce Fields 2031dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2032dc107402SJ. Bruce Fields /* 2033dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2034dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2035dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2036dc107402SJ. Bruce Fields * 2037dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2038dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2039dc107402SJ. Bruce Fields * connect. 2040dc107402SJ. Bruce Fields */ 20415ad64b36STrond Myklebust task->tk_rpc_status = -ENOTCONN; 2042dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2043dc107402SJ. Bruce Fields return; 2044dc107402SJ. Bruce Fields } 2045dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2046dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2047dc107402SJ. Bruce Fields msleep_interruptible(15000); 2048176e21eeSChuck Lever } 2049176e21eeSChuck Lever 20503c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2051d6e971d8SJeff Layton /* 2052d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2053d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2054d6e971d8SJeff Layton * races with xs_reset_transport. 2055d6e971d8SJeff Layton */ 2056a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2057a564b8f0SMel Gorman { 2058a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2059a564b8f0SMel Gorman xprt); 2060a564b8f0SMel Gorman 2061d6e971d8SJeff Layton /* 2062d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2063d6e971d8SJeff Layton * reconnecting process will get it for us. 2064d6e971d8SJeff Layton */ 2065d6e971d8SJeff Layton if (!transport->inet) 2066d6e971d8SJeff Layton return; 20678e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2068a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2069a564b8f0SMel Gorman } 2070a564b8f0SMel Gorman 2071a564b8f0SMel Gorman /** 2072d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2073a564b8f0SMel Gorman * @xprt: transport to tag 2074a564b8f0SMel Gorman * 20758e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20768e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2077a564b8f0SMel Gorman */ 2078d67fa4d8SJeff Layton static int 2079d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2080a564b8f0SMel Gorman { 2081d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2082a564b8f0SMel Gorman 2083d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2084d6e971d8SJeff Layton return 0; 2085d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2086d6e971d8SJeff Layton return -ERESTARTSYS; 2087d6e971d8SJeff Layton if (xs->inet) 2088d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2089d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 20908e228133SJeff Layton return 0; 2091a564b8f0SMel Gorman } 2092a564b8f0SMel Gorman 20938e228133SJeff Layton /** 2094d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20958e228133SJeff Layton * @xprt: transport to tag 20968e228133SJeff Layton * 20978e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 20988e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 20998e228133SJeff Layton */ 2100d67fa4d8SJeff Layton static void 2101d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21028e228133SJeff Layton { 2103d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21048e228133SJeff Layton 2105d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2106d6e971d8SJeff Layton return; 2107d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2108d6e971d8SJeff Layton return; 2109d6e971d8SJeff Layton if (xs->inet) 2110d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2111d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2112a564b8f0SMel Gorman } 2113a564b8f0SMel Gorman #else 2114a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2115a564b8f0SMel Gorman { 2116a564b8f0SMel Gorman } 2117d67fa4d8SJeff Layton 2118d67fa4d8SJeff Layton static int 2119d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2120d67fa4d8SJeff Layton { 2121d67fa4d8SJeff Layton return -EINVAL; 2122d67fa4d8SJeff Layton } 2123d67fa4d8SJeff Layton 2124d67fa4d8SJeff Layton static void 2125d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2126d67fa4d8SJeff Layton { 2127d67fa4d8SJeff Layton } 2128a564b8f0SMel Gorman #endif 2129a564b8f0SMel Gorman 213016be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2131a246b010SChuck Lever { 213216be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2133edb267a6SChuck Lever 2134ee0ac0c2SChuck Lever if (!transport->inet) { 2135b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2136b0d93ad5SChuck Lever 2137b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2138b0d93ad5SChuck Lever 21392a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21402a9e1cfaSTrond Myklebust 2141b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2142f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2143b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2144b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2145b0d93ad5SChuck Lever 2146b0d93ad5SChuck Lever xprt_set_connected(xprt); 2147b0d93ad5SChuck Lever 2148b0d93ad5SChuck Lever /* Reset to new socket */ 2149ee0ac0c2SChuck Lever transport->sock = sock; 2150ee0ac0c2SChuck Lever transport->inet = sk; 2151b0d93ad5SChuck Lever 2152a564b8f0SMel Gorman xs_set_memalloc(xprt); 2153a564b8f0SMel Gorman 2154b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2155b0d93ad5SChuck Lever } 2156470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 215702910177STrond Myklebust 215802910177STrond Myklebust xprt->stat.connect_start = jiffies; 215916be2d20SChuck Lever } 216016be2d20SChuck Lever 21618c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2162a246b010SChuck Lever { 2163a246b010SChuck Lever struct sock_xprt *transport = 2164a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2165a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2166d099b8afSColin Ian King struct socket *sock; 2167b65c0310SPavel Emelyanov int status = -EIO; 2168a246b010SChuck Lever 21698c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21704dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21714dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2172b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2173a246b010SChuck Lever goto out; 217468e220bdSChuck Lever 2175c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2176c740eff8SChuck Lever "%s (port %s)\n", xprt, 2177c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2178c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2179c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 218068e220bdSChuck Lever 218168e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 218240b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2183a246b010SChuck Lever status = 0; 2184b0d93ad5SChuck Lever out: 2185b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2186cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 21877d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2188b0d93ad5SChuck Lever } 2189b0d93ad5SChuck Lever 21904876cc77STrond Myklebust /** 21914876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21924876cc77STrond Myklebust * @xprt: transport 21934876cc77STrond Myklebust * 21944876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 21954876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 21964876cc77STrond Myklebust */ 21974876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 21984876cc77STrond Myklebust { 21994876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22004876cc77STrond Myklebust struct socket *sock = transport->sock; 22019b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22024876cc77STrond Myklebust 22034876cc77STrond Myklebust if (sock == NULL) 22044876cc77STrond Myklebust return; 22059b30889cSTrond Myklebust switch (skst) { 22069b30889cSTrond Myklebust default: 22074876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22084876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22099b30889cSTrond Myklebust break; 22109b30889cSTrond Myklebust case TCP_CLOSE: 22119b30889cSTrond Myklebust case TCP_TIME_WAIT: 22124876cc77STrond Myklebust xs_reset_transport(transport); 22134876cc77STrond Myklebust } 22149b30889cSTrond Myklebust } 22154876cc77STrond Myklebust 22168d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22178d1b8c62STrond Myklebust struct socket *sock) 2218b0d93ad5SChuck Lever { 221916be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22207196dbb0STrond Myklebust unsigned int keepidle; 22217196dbb0STrond Myklebust unsigned int keepcnt; 22227f260e85STrond Myklebust unsigned int opt_on = 1; 2223775f06abSTrond Myklebust unsigned int timeo; 22247f260e85STrond Myklebust 2225b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22267196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22277196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22287196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22297196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22307196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2231b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22327f260e85STrond Myklebust 22337f260e85STrond Myklebust /* TCP Keepalive options */ 22347f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22357f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22367f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22377f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22387f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22397f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22407f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22417f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2242b0d93ad5SChuck Lever 22438d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 22448d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 22458d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 22468d1b8c62STrond Myklebust } 22478d1b8c62STrond Myklebust 22487196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22497196dbb0STrond Myklebust unsigned long connect_timeout, 22507196dbb0STrond Myklebust unsigned long reconnect_timeout) 22517196dbb0STrond Myklebust { 22527196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22537196dbb0STrond Myklebust struct rpc_timeout to; 22547196dbb0STrond Myklebust unsigned long initval; 22557196dbb0STrond Myklebust 2256b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22577196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22587196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22597196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 22607196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22617196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 22627196dbb0STrond Myklebust /* Arbitrary lower limit */ 22637196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 22647196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 22657196dbb0STrond Myklebust to.to_initval = initval; 22667196dbb0STrond Myklebust to.to_maxval = initval; 22677196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 22687196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 22697196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 22707196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 22717196dbb0STrond Myklebust } 22727196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2273b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22747196dbb0STrond Myklebust } 22757196dbb0STrond Myklebust 22768d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 22778d1b8c62STrond Myklebust { 22788d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22798d1b8c62STrond Myklebust int ret = -ENOTCONN; 22808d1b8c62STrond Myklebust 22818d1b8c62STrond Myklebust if (!transport->inet) { 22828d1b8c62STrond Myklebust struct sock *sk = sock->sk; 22838d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 22848d1b8c62STrond Myklebust 2285d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2286d88e4d82SNeilBrown * connections such as NFS mounts. 2287d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2288d88e4d82SNeilBrown * Individual applications, which have specific 2289d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2290d88e4d82SNeilBrown * MAY override this as appropriate. 2291d88e4d82SNeilBrown */ 2292d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2293d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2294d88e4d82SNeilBrown 22958d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2296775f06abSTrond Myklebust 2297b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2298b0d93ad5SChuck Lever 22992a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23002a9e1cfaSTrond Myklebust 2301b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23025157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2303b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2304b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2305b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 23062118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 23073167e12cSChuck Lever 23083167e12cSChuck Lever /* socket options */ 23093167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 23103167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2311b0d93ad5SChuck Lever 2312b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2313b0d93ad5SChuck Lever 2314b0d93ad5SChuck Lever /* Reset to new socket */ 2315ee0ac0c2SChuck Lever transport->sock = sock; 2316ee0ac0c2SChuck Lever transport->inet = sk; 2317b0d93ad5SChuck Lever 2318b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2319b0d93ad5SChuck Lever } 2320b0d93ad5SChuck Lever 232101d37c42STrond Myklebust if (!xprt_bound(xprt)) 2322fe19a96bSTrond Myklebust goto out; 232301d37c42STrond Myklebust 2324a564b8f0SMel Gorman xs_set_memalloc(xprt); 2325a564b8f0SMel Gorman 2326ae053551STrond Myklebust xs_stream_start_connect(transport); 2327e1806c7bSTrond Myklebust 2328b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23290fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2330fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2331fe19a96bSTrond Myklebust switch (ret) { 2332fe19a96bSTrond Myklebust case 0: 23334dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2334e9d47639SGustavo A. R. Silva /* fall through */ 2335fe19a96bSTrond Myklebust case -EINPROGRESS: 2336fe19a96bSTrond Myklebust /* SYN_SENT! */ 2337fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2338fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 23391f4c17a0STrond Myklebust break; 23401f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 23411f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 23421f4c17a0STrond Myklebust transport->srcport = 0; 2343fe19a96bSTrond Myklebust } 2344fe19a96bSTrond Myklebust out: 2345fe19a96bSTrond Myklebust return ret; 234616be2d20SChuck Lever } 234716be2d20SChuck Lever 234816be2d20SChuck Lever /** 2349b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2350acf0a39fSChuck Lever * @work: queued work item 235116be2d20SChuck Lever * 235216be2d20SChuck Lever * Invoked by a work queue tasklet. 235316be2d20SChuck Lever */ 2354cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 235516be2d20SChuck Lever { 2356cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2357cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 235816be2d20SChuck Lever struct socket *sock = transport->sock; 2359a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2360b61d59ffSTrond Myklebust int status = -EIO; 236116be2d20SChuck Lever 236216be2d20SChuck Lever if (!sock) { 2363cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 23644dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 23654dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2366b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2367b61d59ffSTrond Myklebust status = PTR_ERR(sock); 236816be2d20SChuck Lever goto out; 236916be2d20SChuck Lever } 23707d1e8255STrond Myklebust } 23717d1e8255STrond Myklebust 2372c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2373c740eff8SChuck Lever "%s (port %s)\n", xprt, 2374c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2375c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2376c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 237716be2d20SChuck Lever 237816be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 237940b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2380a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 238146121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 238246121cf7SChuck Lever sock->sk->sk_state); 2383a246b010SChuck Lever switch (status) { 2384f75e6745STrond Myklebust default: 2385f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2386f75e6745STrond Myklebust __func__, status); 2387e9d47639SGustavo A. R. Silva /* fall through */ 2388f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2389f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2390f75e6745STrond Myklebust * and retry 2391f75e6745STrond Myklebust */ 2392a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 239388b5ed73STrond Myklebust break; 23942a491991STrond Myklebust case 0: 2395a246b010SChuck Lever case -EINPROGRESS: 2396a246b010SChuck Lever case -EALREADY: 2397718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 23987d1e8255STrond Myklebust return; 23999fcfe0c8STrond Myklebust case -EINVAL: 24009fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24019fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24029fcfe0c8STrond Myklebust */ 24033ed5e2a2STrond Myklebust case -ECONNREFUSED: 24043ed5e2a2STrond Myklebust case -ECONNRESET: 2405eb5b46faSTrond Myklebust case -ENETDOWN: 24063ed5e2a2STrond Myklebust case -ENETUNREACH: 24074ba161a7STrond Myklebust case -EHOSTUNREACH: 24083913c78cSTrond Myklebust case -EADDRINUSE: 24093601c4a9STrond Myklebust case -ENOBUFS: 24106ea44adcSNeilBrown /* 24116ea44adcSNeilBrown * xs_tcp_force_close() wakes tasks with -EIO. 24126ea44adcSNeilBrown * We need to wake them first to ensure the 24136ea44adcSNeilBrown * correct error code. 24146ea44adcSNeilBrown */ 24156ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24164efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 24179fcfe0c8STrond Myklebust goto out; 24188a2cec29STrond Myklebust } 24192a491991STrond Myklebust status = -EAGAIN; 2420a246b010SChuck Lever out: 24212226feb6SChuck Lever xprt_clear_connecting(xprt); 2422cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 24237d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2424a246b010SChuck Lever } 2425a246b010SChuck Lever 242668e220bdSChuck Lever /** 24279903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 24281b092092STrond Myklebust * @xprt: pointer to transport structure 24299903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 24309903cd1cSChuck Lever * 24319903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 243203bf4b70SChuck Lever * 243303bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 243403bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 243503bf4b70SChuck Lever * socket on a privileged port. 243603bf4b70SChuck Lever * 243703bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 243803bf4b70SChuck Lever * retry floods (hard mounts). 24399903cd1cSChuck Lever */ 24401b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2441a246b010SChuck Lever { 2442ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 244302910177STrond Myklebust unsigned long delay = 0; 2444a246b010SChuck Lever 2445718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2446718ba5b8STrond Myklebust 244799b1a4c3STrond Myklebust if (transport->sock != NULL) { 244846121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 244946121cf7SChuck Lever "seconds\n", 245003bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 245199b1a4c3STrond Myklebust 245299b1a4c3STrond Myklebust /* Start by resetting any existing state */ 245399b1a4c3STrond Myklebust xs_reset_transport(transport); 245499b1a4c3STrond Myklebust 2455675dd90aSChuck Lever delay = xprt_reconnect_delay(xprt); 2456675dd90aSChuck Lever xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 245702910177STrond Myklebust 245802910177STrond Myklebust } else 24599903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 246002910177STrond Myklebust 246140a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 246202910177STrond Myklebust &transport->connect_worker, 246302910177STrond Myklebust delay); 2464a246b010SChuck Lever } 2465a246b010SChuck Lever 24664f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 24674f8943f8STrond Myklebust { 24684f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 24694f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 24704f8943f8STrond Myklebust } 24714f8943f8STrond Myklebust 24724f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 24734f8943f8STrond Myklebust { 24744f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 24754f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 24764f8943f8STrond Myklebust } 24774f8943f8STrond Myklebust 24784f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 24794f8943f8STrond Myklebust { 24804f8943f8STrond Myklebust int sockerr; 24814f8943f8STrond Myklebust 24824f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 24834f8943f8STrond Myklebust return; 24844f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 24854f8943f8STrond Myklebust if (transport->sock == NULL) 24864f8943f8STrond Myklebust goto out; 24874f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 24884f8943f8STrond Myklebust goto out; 2489af84537dSBenjamin Coddington sockerr = xchg(&transport->xprt_err, 0); 24904f8943f8STrond Myklebust if (sockerr < 0) 24914f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 24924f8943f8STrond Myklebust out: 24934f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 24944f8943f8STrond Myklebust } 24954f8943f8STrond Myklebust 24964f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 24974f8943f8STrond Myklebust { 24984f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 24994f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 25004f8943f8STrond Myklebust } 25014f8943f8STrond Myklebust 25024f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 25034f8943f8STrond Myklebust { 25044f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 25054f8943f8STrond Myklebust struct sock_xprt, error_worker); 25064f8943f8STrond Myklebust 25074f8943f8STrond Myklebust xs_wake_disconnect(transport); 25084f8943f8STrond Myklebust xs_wake_write(transport); 25094f8943f8STrond Myklebust xs_wake_error(transport); 25104f8943f8STrond Myklebust xs_wake_pending(transport); 25114f8943f8STrond Myklebust } 25124f8943f8STrond Myklebust 2513262ca07dSChuck Lever /** 2514176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2515176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2516176e21eeSChuck Lever * @seq: output file 2517176e21eeSChuck Lever * 2518176e21eeSChuck Lever */ 2519176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2520176e21eeSChuck Lever { 2521176e21eeSChuck Lever long idle_time = 0; 2522176e21eeSChuck Lever 2523176e21eeSChuck Lever if (xprt_connected(xprt)) 2524176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2525176e21eeSChuck Lever 2526176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 252715a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2528176e21eeSChuck Lever xprt->stat.bind_count, 2529176e21eeSChuck Lever xprt->stat.connect_count, 25308440a886SChuck Lever xprt->stat.connect_time / HZ, 2531176e21eeSChuck Lever idle_time, 2532176e21eeSChuck Lever xprt->stat.sends, 2533176e21eeSChuck Lever xprt->stat.recvs, 2534176e21eeSChuck Lever xprt->stat.bad_xids, 2535176e21eeSChuck Lever xprt->stat.req_u, 253615a45206SAndy Adamson xprt->stat.bklog_u, 253715a45206SAndy Adamson xprt->stat.max_slots, 253815a45206SAndy Adamson xprt->stat.sending_u, 253915a45206SAndy Adamson xprt->stat.pending_u); 2540176e21eeSChuck Lever } 2541176e21eeSChuck Lever 2542176e21eeSChuck Lever /** 2543262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2544262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2545262ca07dSChuck Lever * @seq: output file 2546262ca07dSChuck Lever * 2547262ca07dSChuck Lever */ 2548262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2549262ca07dSChuck Lever { 2550c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2551c8475461SChuck Lever 255215a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 255315a45206SAndy Adamson "%lu %llu %llu\n", 2554fbfffbd5SChuck Lever transport->srcport, 2555262ca07dSChuck Lever xprt->stat.bind_count, 2556262ca07dSChuck Lever xprt->stat.sends, 2557262ca07dSChuck Lever xprt->stat.recvs, 2558262ca07dSChuck Lever xprt->stat.bad_xids, 2559262ca07dSChuck Lever xprt->stat.req_u, 256015a45206SAndy Adamson xprt->stat.bklog_u, 256115a45206SAndy Adamson xprt->stat.max_slots, 256215a45206SAndy Adamson xprt->stat.sending_u, 256315a45206SAndy Adamson xprt->stat.pending_u); 2564262ca07dSChuck Lever } 2565262ca07dSChuck Lever 2566262ca07dSChuck Lever /** 2567262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2568262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2569262ca07dSChuck Lever * @seq: output file 2570262ca07dSChuck Lever * 2571262ca07dSChuck Lever */ 2572262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2573262ca07dSChuck Lever { 2574c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2575262ca07dSChuck Lever long idle_time = 0; 2576262ca07dSChuck Lever 2577262ca07dSChuck Lever if (xprt_connected(xprt)) 2578262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2579262ca07dSChuck Lever 258015a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 258115a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2582fbfffbd5SChuck Lever transport->srcport, 2583262ca07dSChuck Lever xprt->stat.bind_count, 2584262ca07dSChuck Lever xprt->stat.connect_count, 25858440a886SChuck Lever xprt->stat.connect_time / HZ, 2586262ca07dSChuck Lever idle_time, 2587262ca07dSChuck Lever xprt->stat.sends, 2588262ca07dSChuck Lever xprt->stat.recvs, 2589262ca07dSChuck Lever xprt->stat.bad_xids, 2590262ca07dSChuck Lever xprt->stat.req_u, 259115a45206SAndy Adamson xprt->stat.bklog_u, 259215a45206SAndy Adamson xprt->stat.max_slots, 259315a45206SAndy Adamson xprt->stat.sending_u, 259415a45206SAndy Adamson xprt->stat.pending_u); 2595262ca07dSChuck Lever } 2596262ca07dSChuck Lever 25974cfc7e60SRahul Iyer /* 25984cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 25994cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 26004cfc7e60SRahul Iyer * to use the server side send routines. 26014cfc7e60SRahul Iyer */ 26025fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 26034cfc7e60SRahul Iyer { 26045fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 26055fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 26064cfc7e60SRahul Iyer struct page *page; 26074cfc7e60SRahul Iyer struct rpc_buffer *buf; 26084cfc7e60SRahul Iyer 26095fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 26105fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 26115fe6eaa1SChuck Lever size); 26125fe6eaa1SChuck Lever return -EINVAL; 26135fe6eaa1SChuck Lever } 26144cfc7e60SRahul Iyer 2615b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 26164cfc7e60SRahul Iyer if (!page) 26175fe6eaa1SChuck Lever return -ENOMEM; 26184cfc7e60SRahul Iyer 26194cfc7e60SRahul Iyer buf = page_address(page); 26204cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 26214cfc7e60SRahul Iyer 26225fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 262318e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 26245fe6eaa1SChuck Lever return 0; 26254cfc7e60SRahul Iyer } 26264cfc7e60SRahul Iyer 26274cfc7e60SRahul Iyer /* 26284cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 26294cfc7e60SRahul Iyer */ 26303435c74aSChuck Lever static void bc_free(struct rpc_task *task) 26314cfc7e60SRahul Iyer { 26323435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 26334cfc7e60SRahul Iyer struct rpc_buffer *buf; 26344cfc7e60SRahul Iyer 26354cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 26364cfc7e60SRahul Iyer free_page((unsigned long)buf); 26374cfc7e60SRahul Iyer } 26384cfc7e60SRahul Iyer 26394cfc7e60SRahul Iyer /* 26404cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 26414cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 26424cfc7e60SRahul Iyer */ 26434cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 26444cfc7e60SRahul Iyer { 26454cfc7e60SRahul Iyer int len; 26464cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 26474cfc7e60SRahul Iyer struct sock_xprt *transport = 2648067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 26494cfc7e60SRahul Iyer unsigned long headoff; 26504cfc7e60SRahul Iyer unsigned long tailoff; 2651067fb11bSChuck Lever struct page *tailpage; 2652067fb11bSChuck Lever struct msghdr msg = { 2653067fb11bSChuck Lever .msg_flags = MSG_MORE 2654067fb11bSChuck Lever }; 2655067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2656067fb11bSChuck Lever (u32)xbufp->len); 2657067fb11bSChuck Lever struct kvec iov = { 2658067fb11bSChuck Lever .iov_base = &marker, 2659067fb11bSChuck Lever .iov_len = sizeof(marker), 2660067fb11bSChuck Lever }; 26614cfc7e60SRahul Iyer 2662067fb11bSChuck Lever len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len); 2663067fb11bSChuck Lever if (len != iov.iov_len) 2664067fb11bSChuck Lever return -EAGAIN; 26654cfc7e60SRahul Iyer 2666067fb11bSChuck Lever tailpage = NULL; 2667067fb11bSChuck Lever if (xbufp->tail[0].iov_len) 2668067fb11bSChuck Lever tailpage = virt_to_page(xbufp->tail[0].iov_base); 26694cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26704cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 2671067fb11bSChuck Lever len = svc_send_common(transport->sock, xbufp, 26724cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 2673067fb11bSChuck Lever tailpage, tailoff); 2674067fb11bSChuck Lever if (len != xbufp->len) 2675067fb11bSChuck Lever return -EAGAIN; 26764cfc7e60SRahul Iyer return len; 26774cfc7e60SRahul Iyer } 26784cfc7e60SRahul Iyer 26794cfc7e60SRahul Iyer /* 26804cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 26814cfc7e60SRahul Iyer */ 2682adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 26834cfc7e60SRahul Iyer { 26844cfc7e60SRahul Iyer struct svc_xprt *xprt; 26857fc56136SAndrzej Hajda int len; 26864cfc7e60SRahul Iyer 26874cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 26884cfc7e60SRahul Iyer /* 26894cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 26904cfc7e60SRahul Iyer */ 26914cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 26924cfc7e60SRahul Iyer 26934cfc7e60SRahul Iyer /* 26944cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 26954cfc7e60SRahul Iyer * with the fore channel 26964cfc7e60SRahul Iyer */ 2697c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 26984cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 26994cfc7e60SRahul Iyer len = -ENOTCONN; 27004cfc7e60SRahul Iyer else 27014cfc7e60SRahul Iyer len = bc_sendto(req); 27024cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 27034cfc7e60SRahul Iyer 27044cfc7e60SRahul Iyer if (len > 0) 27054cfc7e60SRahul Iyer len = 0; 27064cfc7e60SRahul Iyer 27074cfc7e60SRahul Iyer return len; 27084cfc7e60SRahul Iyer } 27094cfc7e60SRahul Iyer 27104cfc7e60SRahul Iyer /* 27114cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 27124cfc7e60SRahul Iyer */ 27134cfc7e60SRahul Iyer 27144cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 27154cfc7e60SRahul Iyer { 27164cfc7e60SRahul Iyer } 27174cfc7e60SRahul Iyer 27184cfc7e60SRahul Iyer /* 27194cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 27204cfc7e60SRahul Iyer * initiated, we do nothing 27214cfc7e60SRahul Iyer */ 27224cfc7e60SRahul Iyer 27234cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 27244cfc7e60SRahul Iyer { 272547f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 272647f72efaSKinglong Mee 272747f72efaSKinglong Mee xs_xprt_free(xprt); 272847f72efaSKinglong Mee module_put(THIS_MODULE); 27294cfc7e60SRahul Iyer } 27304cfc7e60SRahul Iyer 2731d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2732176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 27334cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2734f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2735a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2736176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2737176e21eeSChuck Lever .set_port = xs_local_set_port, 2738dc107402SJ. Bruce Fields .connect = xs_local_connect, 2739176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2740176e21eeSChuck Lever .buf_free = rpc_free, 2741550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 2742176e21eeSChuck Lever .send_request = xs_local_send_request, 27438ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2744176e21eeSChuck Lever .close = xs_close, 2745a1311d87STrond Myklebust .destroy = xs_destroy, 2746176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2747d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2748d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2749176e21eeSChuck Lever }; 2750176e21eeSChuck Lever 2751d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 275243118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 275312a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 275449e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2755f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2756a9cde23aSChuck Lever .free_slot = xprt_free_slot, 275745160d62SChuck Lever .rpcbind = rpcb_getport_async, 275892200412SChuck Lever .set_port = xs_set_port, 27599903cd1cSChuck Lever .connect = xs_connect, 276002107148SChuck Lever .buf_alloc = rpc_malloc, 276102107148SChuck Lever .buf_free = rpc_free, 2762262965f5SChuck Lever .send_request = xs_udp_send_request, 27638ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 276446c0ee8bSChuck Lever .timer = xs_udp_timer, 2765a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2766262965f5SChuck Lever .close = xs_close, 2767262965f5SChuck Lever .destroy = xs_destroy, 2768262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2769d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2770d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27714a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2772262965f5SChuck Lever }; 2773262965f5SChuck Lever 2774d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 277512a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 27764cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 277736bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 2778a9cde23aSChuck Lever .free_slot = xprt_free_slot, 277945160d62SChuck Lever .rpcbind = rpcb_getport_async, 278092200412SChuck Lever .set_port = xs_set_port, 27810b9e7943STrond Myklebust .connect = xs_connect, 278202107148SChuck Lever .buf_alloc = rpc_malloc, 278302107148SChuck Lever .buf_free = rpc_free, 2784277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 2785262965f5SChuck Lever .send_request = xs_tcp_send_request, 27868ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2787c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 27889903cd1cSChuck Lever .destroy = xs_destroy, 27897196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2790262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2791d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2792d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27934a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 279442e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 279542e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 27966b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 27977402a4feSTrond Myklebust .bc_num_slots = xprt_bc_max_slots, 279842e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 279942e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 280042e5c3e2SChuck Lever #endif 2801a246b010SChuck Lever }; 2802a246b010SChuck Lever 28034cfc7e60SRahul Iyer /* 28044cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 28054cfc7e60SRahul Iyer */ 28064cfc7e60SRahul Iyer 2807d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 28084cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 28094cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 281084e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2811a9cde23aSChuck Lever .free_slot = xprt_free_slot, 28124cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 28134cfc7e60SRahul Iyer .buf_free = bc_free, 28144cfc7e60SRahul Iyer .send_request = bc_send_request, 28158ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 28164cfc7e60SRahul Iyer .close = bc_close, 28174cfc7e60SRahul Iyer .destroy = bc_destroy, 28184cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2819d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2820d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28214a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 28224cfc7e60SRahul Iyer }; 28234cfc7e60SRahul Iyer 282492476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 282592476850SChuck Lever { 282692476850SChuck Lever static const struct sockaddr_in sin = { 282792476850SChuck Lever .sin_family = AF_INET, 282892476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 282992476850SChuck Lever }; 283092476850SChuck Lever static const struct sockaddr_in6 sin6 = { 283192476850SChuck Lever .sin6_family = AF_INET6, 283292476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 283392476850SChuck Lever }; 283492476850SChuck Lever 283592476850SChuck Lever switch (family) { 2836176e21eeSChuck Lever case AF_LOCAL: 2837176e21eeSChuck Lever break; 283892476850SChuck Lever case AF_INET: 283992476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 284092476850SChuck Lever break; 284192476850SChuck Lever case AF_INET6: 284292476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 284392476850SChuck Lever break; 284492476850SChuck Lever default: 284592476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 284692476850SChuck Lever return -EAFNOSUPPORT; 284792476850SChuck Lever } 284892476850SChuck Lever return 0; 284992476850SChuck Lever } 285092476850SChuck Lever 28513c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2852d9ba131dSTrond Myklebust unsigned int slot_table_size, 2853d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2854c8541ecdSChuck Lever { 2855c8541ecdSChuck Lever struct rpc_xprt *xprt; 2856ffc2e518SChuck Lever struct sock_xprt *new; 2857c8541ecdSChuck Lever 285896802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2859c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2860c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2861c8541ecdSChuck Lever } 2862c8541ecdSChuck Lever 2863d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2864d9ba131dSTrond Myklebust max_slot_table_size); 2865bd1722d4SPavel Emelyanov if (xprt == NULL) { 286646121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 286746121cf7SChuck Lever "rpc_xprt\n"); 2868c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2869c8541ecdSChuck Lever } 2870c8541ecdSChuck Lever 2871bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2872edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 287396802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 287496802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2875d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2876fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 287792476850SChuck Lever else { 287892476850SChuck Lever int err; 287992476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 288092476850SChuck Lever (struct sockaddr *)&new->srcaddr); 28812aa13531SStanislav Kinsbursky if (err != 0) { 28822aa13531SStanislav Kinsbursky xprt_free(xprt); 288392476850SChuck Lever return ERR_PTR(err); 288492476850SChuck Lever } 28852aa13531SStanislav Kinsbursky } 2886c8541ecdSChuck Lever 2887c8541ecdSChuck Lever return xprt; 2888c8541ecdSChuck Lever } 2889c8541ecdSChuck Lever 2890176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2891176e21eeSChuck Lever .to_initval = 10 * HZ, 2892176e21eeSChuck Lever .to_maxval = 10 * HZ, 2893176e21eeSChuck Lever .to_retries = 2, 2894176e21eeSChuck Lever }; 2895176e21eeSChuck Lever 2896176e21eeSChuck Lever /** 2897176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2898176e21eeSChuck Lever * @args: rpc transport creation arguments 2899176e21eeSChuck Lever * 2900176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2901176e21eeSChuck Lever */ 2902176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2903176e21eeSChuck Lever { 2904176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2905176e21eeSChuck Lever struct sock_xprt *transport; 2906176e21eeSChuck Lever struct rpc_xprt *xprt; 2907176e21eeSChuck Lever struct rpc_xprt *ret; 2908176e21eeSChuck Lever 2909d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2910d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2911176e21eeSChuck Lever if (IS_ERR(xprt)) 2912176e21eeSChuck Lever return xprt; 2913176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2914176e21eeSChuck Lever 2915176e21eeSChuck Lever xprt->prot = 0; 2916176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2917176e21eeSChuck Lever 2918176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2919176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2920176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2921176e21eeSChuck Lever 2922176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2923176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2924176e21eeSChuck Lever 2925550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 29264f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2927550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 292893dc41bdSNeilBrown 2929176e21eeSChuck Lever switch (sun->sun_family) { 2930176e21eeSChuck Lever case AF_LOCAL: 2931176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2932176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2933176e21eeSChuck Lever sun->sun_path); 2934176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2935176e21eeSChuck Lever goto out_err; 2936176e21eeSChuck Lever } 2937176e21eeSChuck Lever xprt_set_bound(xprt); 2938176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 29397073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 29407073ea87SJ. Bruce Fields if (ret) 29417073ea87SJ. Bruce Fields goto out_err; 2942176e21eeSChuck Lever break; 2943176e21eeSChuck Lever default: 2944176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2945176e21eeSChuck Lever goto out_err; 2946176e21eeSChuck Lever } 2947176e21eeSChuck Lever 2948176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2949176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2950176e21eeSChuck Lever 2951176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2952176e21eeSChuck Lever return xprt; 2953176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2954176e21eeSChuck Lever out_err: 2955315f3812SKinglong Mee xs_xprt_free(xprt); 2956176e21eeSChuck Lever return ret; 2957176e21eeSChuck Lever } 2958176e21eeSChuck Lever 29592881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 29602881ae74STrond Myklebust .to_initval = 5 * HZ, 29612881ae74STrond Myklebust .to_maxval = 30 * HZ, 29622881ae74STrond Myklebust .to_increment = 5 * HZ, 29632881ae74STrond Myklebust .to_retries = 5, 29642881ae74STrond Myklebust }; 29652881ae74STrond Myklebust 29669903cd1cSChuck Lever /** 29679903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 296896802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29699903cd1cSChuck Lever * 29709903cd1cSChuck Lever */ 2971483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2972a246b010SChuck Lever { 29738f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2974c8541ecdSChuck Lever struct rpc_xprt *xprt; 2975c8475461SChuck Lever struct sock_xprt *transport; 29760a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2977a246b010SChuck Lever 2978d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2979d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2980c8541ecdSChuck Lever if (IS_ERR(xprt)) 2981c8541ecdSChuck Lever return xprt; 2982c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2983a246b010SChuck Lever 2984ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2985a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2986a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2987a246b010SChuck Lever 298803bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 298903bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 299003bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2991a246b010SChuck Lever 2992262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2993a246b010SChuck Lever 2994ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2995a246b010SChuck Lever 2996f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 29974f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2998edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2999edc1b01cSTrond Myklebust 30008f9d5b1aSChuck Lever switch (addr->sa_family) { 30018f9d5b1aSChuck Lever case AF_INET: 30028f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30038f9d5b1aSChuck Lever xprt_set_bound(xprt); 30048f9d5b1aSChuck Lever 30059dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 30068f9d5b1aSChuck Lever break; 30078f9d5b1aSChuck Lever case AF_INET6: 30088f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30098f9d5b1aSChuck Lever xprt_set_bound(xprt); 30108f9d5b1aSChuck Lever 30119dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 30128f9d5b1aSChuck Lever break; 30138f9d5b1aSChuck Lever default: 30140a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30150a68b0beSJ. Bruce Fields goto out_err; 30168f9d5b1aSChuck Lever } 30178f9d5b1aSChuck Lever 3018c740eff8SChuck Lever if (xprt_bound(xprt)) 3019c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3020c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3021c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3022c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3023c740eff8SChuck Lever else 3024c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3025c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3026c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3027edb267a6SChuck Lever 3028bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3029c8541ecdSChuck Lever return xprt; 30300a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30310a68b0beSJ. Bruce Fields out_err: 3032315f3812SKinglong Mee xs_xprt_free(xprt); 30330a68b0beSJ. Bruce Fields return ret; 3034a246b010SChuck Lever } 3035a246b010SChuck Lever 30362881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 30372881ae74STrond Myklebust .to_initval = 60 * HZ, 30382881ae74STrond Myklebust .to_maxval = 60 * HZ, 30392881ae74STrond Myklebust .to_retries = 2, 30402881ae74STrond Myklebust }; 30412881ae74STrond Myklebust 30429903cd1cSChuck Lever /** 30439903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 304496802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30459903cd1cSChuck Lever * 30469903cd1cSChuck Lever */ 3047483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3048a246b010SChuck Lever { 30498f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3050c8541ecdSChuck Lever struct rpc_xprt *xprt; 3051c8475461SChuck Lever struct sock_xprt *transport; 30520a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3053b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3054b7993cebSTrond Myklebust 3055b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3056b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3057a246b010SChuck Lever 3058d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3059b7993cebSTrond Myklebust max_slot_table_size); 3060c8541ecdSChuck Lever if (IS_ERR(xprt)) 3061c8541ecdSChuck Lever return xprt; 3062c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3063a246b010SChuck Lever 3064ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3065808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3066a246b010SChuck Lever 306703bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 306803bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 306903bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3070a246b010SChuck Lever 3071262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3072ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3073a246b010SChuck Lever 30743851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 30757196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 30767196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 30773851f1cdSTrond Myklebust 3078c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 30794f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3080edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3081edc1b01cSTrond Myklebust 30828f9d5b1aSChuck Lever switch (addr->sa_family) { 30838f9d5b1aSChuck Lever case AF_INET: 30848f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30858f9d5b1aSChuck Lever xprt_set_bound(xprt); 30868f9d5b1aSChuck Lever 30879dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 30888f9d5b1aSChuck Lever break; 30898f9d5b1aSChuck Lever case AF_INET6: 30908f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30918f9d5b1aSChuck Lever xprt_set_bound(xprt); 30928f9d5b1aSChuck Lever 30939dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 30948f9d5b1aSChuck Lever break; 30958f9d5b1aSChuck Lever default: 30960a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30970a68b0beSJ. Bruce Fields goto out_err; 30988f9d5b1aSChuck Lever } 30998f9d5b1aSChuck Lever 3100c740eff8SChuck Lever if (xprt_bound(xprt)) 3101c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3102c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3103c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3104c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3105c740eff8SChuck Lever else 3106c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3107c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3108c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3109c740eff8SChuck Lever 3110bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3111c8541ecdSChuck Lever return xprt; 31120a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31130a68b0beSJ. Bruce Fields out_err: 3114315f3812SKinglong Mee xs_xprt_free(xprt); 31150a68b0beSJ. Bruce Fields return ret; 3116a246b010SChuck Lever } 3117282b32e1SChuck Lever 3118f300babaSAlexandros Batsakis /** 3119f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3120f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3121f300babaSAlexandros Batsakis * 3122f300babaSAlexandros Batsakis */ 3123f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3124f300babaSAlexandros Batsakis { 3125f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3126f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3127f300babaSAlexandros Batsakis struct sock_xprt *transport; 3128f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 31290a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3130f300babaSAlexandros Batsakis 3131d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3132d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3133f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3134f300babaSAlexandros Batsakis return xprt; 3135f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3136f300babaSAlexandros Batsakis 3137f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3138f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3139f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3140f300babaSAlexandros Batsakis 3141f300babaSAlexandros Batsakis /* backchannel */ 3142f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3143f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3144f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3145f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3146f300babaSAlexandros Batsakis 3147f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3148f300babaSAlexandros Batsakis 3149f300babaSAlexandros Batsakis switch (addr->sa_family) { 3150f300babaSAlexandros Batsakis case AF_INET: 3151f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3152f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3153f300babaSAlexandros Batsakis break; 3154f300babaSAlexandros Batsakis case AF_INET6: 3155f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3156f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3157f300babaSAlexandros Batsakis break; 3158f300babaSAlexandros Batsakis default: 31590a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31600a68b0beSJ. Bruce Fields goto out_err; 3161f300babaSAlexandros Batsakis } 3162f300babaSAlexandros Batsakis 3163f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3164f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3165f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3166f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3167f300babaSAlexandros Batsakis 3168f300babaSAlexandros Batsakis /* 316999de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 317028303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 317128303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 317228303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 317399de8ea9SJ. Bruce Fields */ 317499de8ea9SJ. Bruce Fields xprt_get(xprt); 317599de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 317699de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 317799de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 317899de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 317999de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 318099de8ea9SJ. Bruce Fields 318199de8ea9SJ. Bruce Fields /* 3182f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3183f300babaSAlexandros Batsakis * the xprt status to connected 3184f300babaSAlexandros Batsakis */ 3185f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3186f300babaSAlexandros Batsakis 3187f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3188f300babaSAlexandros Batsakis return xprt; 3189642aab58SKinglong Mee 3190642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 319139a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 319299de8ea9SJ. Bruce Fields xprt_put(xprt); 31930a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31940a68b0beSJ. Bruce Fields out_err: 3195315f3812SKinglong Mee xs_xprt_free(xprt); 31960a68b0beSJ. Bruce Fields return ret; 3197f300babaSAlexandros Batsakis } 3198f300babaSAlexandros Batsakis 3199176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3200176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3201176e21eeSChuck Lever .name = "named UNIX socket", 3202176e21eeSChuck Lever .owner = THIS_MODULE, 3203176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3204176e21eeSChuck Lever .setup = xs_setup_local, 3205176e21eeSChuck Lever }; 3206176e21eeSChuck Lever 3207bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3208bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3209bc25571eS\"Talpey, Thomas\ .name = "udp", 3210bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3211f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3212bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3213bc25571eS\"Talpey, Thomas\ }; 3214bc25571eS\"Talpey, Thomas\ 3215bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3216bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3217bc25571eS\"Talpey, Thomas\ .name = "tcp", 3218bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3219f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3220bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3221bc25571eS\"Talpey, Thomas\ }; 3222bc25571eS\"Talpey, Thomas\ 3223f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3224f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3225f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3226f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3227f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3228f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3229f300babaSAlexandros Batsakis }; 3230f300babaSAlexandros Batsakis 3231282b32e1SChuck Lever /** 3232bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3233282b32e1SChuck Lever * 3234282b32e1SChuck Lever */ 3235282b32e1SChuck Lever int init_socket_xprt(void) 3236282b32e1SChuck Lever { 32372b1bec5fSEric W. Biederman if (!sunrpc_table_header) 32380b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3239fbf76683SChuck Lever 3240176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3241bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3242bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3243f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3244bc25571eS\"Talpey, Thomas\ 3245282b32e1SChuck Lever return 0; 3246282b32e1SChuck Lever } 3247282b32e1SChuck Lever 3248282b32e1SChuck Lever /** 3249bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3250282b32e1SChuck Lever * 3251282b32e1SChuck Lever */ 3252282b32e1SChuck Lever void cleanup_socket_xprt(void) 3253282b32e1SChuck Lever { 3254fbf76683SChuck Lever if (sunrpc_table_header) { 3255fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3256fbf76683SChuck Lever sunrpc_table_header = NULL; 3257fbf76683SChuck Lever } 3258bc25571eS\"Talpey, Thomas\ 3259176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3260bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3261bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3262f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3263282b32e1SChuck Lever } 3264cbf11071STrond Myklebust 32659bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 32669bbb9e5aSRusty Russell const struct kernel_param *kp, 3267cbf11071STrond Myklebust unsigned int min, unsigned int max) 3268cbf11071STrond Myklebust { 326900cfaa94SDaniel Walter unsigned int num; 3270cbf11071STrond Myklebust int ret; 3271cbf11071STrond Myklebust 3272cbf11071STrond Myklebust if (!val) 3273cbf11071STrond Myklebust return -EINVAL; 327400cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 32759761a246SDan Carpenter if (ret) 32769761a246SDan Carpenter return ret; 32779761a246SDan Carpenter if (num < min || num > max) 3278cbf11071STrond Myklebust return -EINVAL; 3279cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3280cbf11071STrond Myklebust return 0; 3281cbf11071STrond Myklebust } 3282cbf11071STrond Myklebust 32839bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3284cbf11071STrond Myklebust { 3285cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3286cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3287cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3288cbf11071STrond Myklebust } 3289cbf11071STrond Myklebust 32909c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 32919bbb9e5aSRusty Russell .set = param_set_portnr, 32929bbb9e5aSRusty Russell .get = param_get_uint, 32939bbb9e5aSRusty Russell }; 32949bbb9e5aSRusty Russell 3295cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3296cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3297cbf11071STrond Myklebust 3298cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3299cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3300cbf11071STrond Myklebust 33019bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 33029bbb9e5aSRusty Russell const struct kernel_param *kp) 3303cbf11071STrond Myklebust { 3304cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3305cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3306cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3307cbf11071STrond Myklebust } 3308cbf11071STrond Myklebust 33099c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 33109bbb9e5aSRusty Russell .set = param_set_slot_table_size, 33119bbb9e5aSRusty Russell .get = param_get_uint, 33129bbb9e5aSRusty Russell }; 33139bbb9e5aSRusty Russell 3314cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3315cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3316cbf11071STrond Myklebust 3317d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3318d9ba131dSTrond Myklebust const struct kernel_param *kp) 3319d9ba131dSTrond Myklebust { 3320d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3321d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3322d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3323d9ba131dSTrond Myklebust } 3324d9ba131dSTrond Myklebust 33259c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3326d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3327d9ba131dSTrond Myklebust .get = param_get_uint, 3328d9ba131dSTrond Myklebust }; 3329d9ba131dSTrond Myklebust 3330d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3331d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3332d9ba131dSTrond Myklebust 3333cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3334cbf11071STrond Myklebust slot_table_size, 0644); 3335d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3336d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3337cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3338cbf11071STrond Myklebust slot_table_size, 0644); 3339