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 579e55eef4SChuck Lever #include "socklib.h" 584cfc7e60SRahul Iyer #include "sunrpc.h" 59176e21eeSChuck Lever 60176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 617196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 627196dbb0STrond Myklebust struct socket *sock); 63176e21eeSChuck Lever 649903cd1cSChuck Lever /* 65c556b754SChuck Lever * xprtsock tunables 66c556b754SChuck Lever */ 6709acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 6809acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 6909acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 70c556b754SChuck Lever 7109acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 7209acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 73c556b754SChuck Lever 747d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 7525fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 767d1e8255STrond Myklebust 77c556b754SChuck Lever /* 78fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 79fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 80fbf76683SChuck Lever * directory become the union of all files registered there. 81fbf76683SChuck Lever * 82fbf76683SChuck Lever * We simply need to make sure that we don't collide with 83fbf76683SChuck Lever * someone else's file names! 84fbf76683SChuck Lever */ 85fbf76683SChuck Lever 86fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 87fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 88d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 89fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 90fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 91fbf76683SChuck Lever 92fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 93fbf76683SChuck Lever 94fbf76683SChuck Lever /* 95fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 96fbf76683SChuck Lever * socket buffers for existing UDP transports 97fbf76683SChuck Lever */ 98fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 99fbf76683SChuck Lever { 100fbf76683SChuck Lever .procname = "udp_slot_table_entries", 101fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 102fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 103fbf76683SChuck Lever .mode = 0644, 1046d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 105fbf76683SChuck Lever .extra1 = &min_slot_table_size, 106fbf76683SChuck Lever .extra2 = &max_slot_table_size 107fbf76683SChuck Lever }, 108fbf76683SChuck Lever { 109fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 110fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 111fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 112fbf76683SChuck Lever .mode = 0644, 1136d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 114fbf76683SChuck Lever .extra1 = &min_slot_table_size, 115fbf76683SChuck Lever .extra2 = &max_slot_table_size 116fbf76683SChuck Lever }, 117fbf76683SChuck Lever { 118d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 119d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 120d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 121d9ba131dSTrond Myklebust .mode = 0644, 122d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 123d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 124d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 125d9ba131dSTrond Myklebust }, 126d9ba131dSTrond Myklebust { 127fbf76683SChuck Lever .procname = "min_resvport", 128fbf76683SChuck Lever .data = &xprt_min_resvport, 129fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 130fbf76683SChuck Lever .mode = 0644, 1316d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 132fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 133826799e6SJ. Bruce Fields .extra2 = &xprt_max_resvport_limit 134fbf76683SChuck Lever }, 135fbf76683SChuck Lever { 136fbf76683SChuck Lever .procname = "max_resvport", 137fbf76683SChuck Lever .data = &xprt_max_resvport, 138fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 139fbf76683SChuck Lever .mode = 0644, 1406d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 141826799e6SJ. Bruce Fields .extra1 = &xprt_min_resvport_limit, 142fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 143fbf76683SChuck Lever }, 144fbf76683SChuck Lever { 14525fe6142STrond Myklebust .procname = "tcp_fin_timeout", 14625fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 14725fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 14825fe6142STrond Myklebust .mode = 0644, 1496d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 15025fe6142STrond Myklebust }, 151f8572d8fSEric W. Biederman { }, 152fbf76683SChuck Lever }; 153fbf76683SChuck Lever 154fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = { 155fbf76683SChuck Lever { 156fbf76683SChuck Lever .procname = "sunrpc", 157fbf76683SChuck Lever .mode = 0555, 158fbf76683SChuck Lever .child = xs_tunables_table 159fbf76683SChuck Lever }, 160f8572d8fSEric W. Biederman { }, 161fbf76683SChuck Lever }; 162fbf76683SChuck Lever 163fbf76683SChuck Lever /* 16403bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16503bf4b70SChuck Lever */ 16603bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16703bf4b70SChuck Lever 16803bf4b70SChuck Lever /* 16903bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 17003bf4b70SChuck Lever * kind of resource problem on the local host. 17103bf4b70SChuck Lever */ 17203bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17303bf4b70SChuck Lever 17403bf4b70SChuck Lever /* 17503bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17603bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17703bf4b70SChuck Lever * 17803bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17903bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 18003bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18103bf4b70SChuck Lever * increase over time if the server is down or not responding. 18203bf4b70SChuck Lever */ 18303bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18403bf4b70SChuck Lever 18503bf4b70SChuck Lever /* 18603bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18703bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18803bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18903bf4b70SChuck Lever */ 19003bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19103bf4b70SChuck Lever 192f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 193a246b010SChuck Lever # undef RPC_DEBUG_DATA 1949903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 195a246b010SChuck Lever #endif 196a246b010SChuck Lever 197a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1989903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 199a246b010SChuck Lever { 200a246b010SChuck Lever u8 *buf = (u8 *) packet; 201a246b010SChuck Lever int j; 202a246b010SChuck Lever 203a246b010SChuck Lever dprintk("RPC: %s\n", msg); 204a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 205a246b010SChuck Lever if (!(j & 31)) { 206a246b010SChuck Lever if (j) 207a246b010SChuck Lever dprintk("\n"); 208a246b010SChuck Lever dprintk("0x%04x ", j); 209a246b010SChuck Lever } 210a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 211a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 212a246b010SChuck Lever } 213a246b010SChuck Lever dprintk("\n"); 214a246b010SChuck Lever } 215a246b010SChuck Lever #else 2169903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 217a246b010SChuck Lever { 218a246b010SChuck Lever /* NOP */ 219a246b010SChuck Lever } 220a246b010SChuck Lever #endif 221a246b010SChuck Lever 2222118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2232118071dSTrond Myklebust { 2242118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2252118071dSTrond Myklebust } 2262118071dSTrond Myklebust 22795392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 228edb267a6SChuck Lever { 22995392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 23095392c59SChuck Lever } 23195392c59SChuck Lever 232176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 233176e21eeSChuck Lever { 234176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 235176e21eeSChuck Lever } 236176e21eeSChuck Lever 23795392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 23895392c59SChuck Lever { 23995392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 24095392c59SChuck Lever } 24195392c59SChuck Lever 24295392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 24395392c59SChuck Lever { 24495392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 24595392c59SChuck Lever } 24695392c59SChuck Lever 247c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 248c877b849SChuck Lever { 249c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2509dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2519dc3b095SChuck Lever struct sockaddr_in *sin; 252176e21eeSChuck Lever struct sockaddr_un *sun; 253c877b849SChuck Lever char buf[128]; 254c877b849SChuck Lever 2559dc3b095SChuck Lever switch (sap->sa_family) { 256176e21eeSChuck Lever case AF_LOCAL: 257176e21eeSChuck Lever sun = xs_addr_un(xprt); 258176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 259176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 260176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 261176e21eeSChuck Lever break; 2629dc3b095SChuck Lever case AF_INET: 263176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 264176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 265176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2669dc3b095SChuck Lever sin = xs_addr_in(xprt); 267fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2689dc3b095SChuck Lever break; 2699dc3b095SChuck Lever case AF_INET6: 270176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 271176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 272176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2739dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 274fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2759dc3b095SChuck Lever break; 2769dc3b095SChuck Lever default: 2779dc3b095SChuck Lever BUG(); 2789dc3b095SChuck Lever } 279176e21eeSChuck Lever 2809dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2819dc3b095SChuck Lever } 2829dc3b095SChuck Lever 2839dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2849dc3b095SChuck Lever { 2859dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2869dc3b095SChuck Lever char buf[128]; 2879dc3b095SChuck Lever 28881160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 289c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 290c877b849SChuck Lever 29181160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 292c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 293c877b849SChuck Lever } 294c877b849SChuck Lever 2959dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 296b454ae90SChuck Lever const char *protocol, 297b454ae90SChuck Lever const char *netid) 298edb267a6SChuck Lever { 299b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 300b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 301c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3029dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 303edb267a6SChuck Lever } 304edb267a6SChuck Lever 3059dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3064b6473fbSChuck Lever { 3079dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3089dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3094b6473fbSChuck Lever 3109dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 311edb267a6SChuck Lever } 312edb267a6SChuck Lever 313edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 314edb267a6SChuck Lever { 31533e01dc7SChuck Lever unsigned int i; 31633e01dc7SChuck Lever 31733e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 31833e01dc7SChuck Lever switch (i) { 31933e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 32033e01dc7SChuck Lever case RPC_DISPLAY_NETID: 32133e01dc7SChuck Lever continue; 32233e01dc7SChuck Lever default: 32333e01dc7SChuck Lever kfree(xprt->address_strings[i]); 32433e01dc7SChuck Lever } 325edb267a6SChuck Lever } 326edb267a6SChuck Lever 327277e4ab7STrond Myklebust static size_t 328277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 329277e4ab7STrond Myklebust { 330277e4ab7STrond Myklebust size_t i,n; 331277e4ab7STrond Myklebust 33216e5e90fSTrond Myklebust if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 333277e4ab7STrond Myklebust return want; 334277e4ab7STrond Myklebust n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 335277e4ab7STrond Myklebust for (i = 0; i < n; i++) { 336277e4ab7STrond Myklebust if (buf->pages[i]) 337277e4ab7STrond Myklebust continue; 338277e4ab7STrond Myklebust buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 339277e4ab7STrond Myklebust if (!buf->pages[i]) { 34016e5e90fSTrond Myklebust i *= PAGE_SIZE; 34116e5e90fSTrond Myklebust return i > buf->page_base ? i - buf->page_base : 0; 342277e4ab7STrond Myklebust } 343277e4ab7STrond Myklebust } 344277e4ab7STrond Myklebust return want; 345277e4ab7STrond Myklebust } 346277e4ab7STrond Myklebust 347277e4ab7STrond Myklebust static ssize_t 348277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 349277e4ab7STrond Myklebust { 350277e4ab7STrond Myklebust ssize_t ret; 351277e4ab7STrond Myklebust if (seek != 0) 352277e4ab7STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 353277e4ab7STrond Myklebust ret = sock_recvmsg(sock, msg, flags); 354277e4ab7STrond Myklebust return ret > 0 ? ret + seek : ret; 355277e4ab7STrond Myklebust } 356277e4ab7STrond Myklebust 357277e4ab7STrond Myklebust static ssize_t 358277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 359277e4ab7STrond Myklebust struct kvec *kvec, size_t count, size_t seek) 360277e4ab7STrond Myklebust { 3610e9b4a82SAl Viro iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); 362277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 363277e4ab7STrond Myklebust } 364277e4ab7STrond Myklebust 365277e4ab7STrond Myklebust static ssize_t 366277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 367277e4ab7STrond Myklebust struct bio_vec *bvec, unsigned long nr, size_t count, 368277e4ab7STrond Myklebust size_t seek) 369277e4ab7STrond Myklebust { 3700e9b4a82SAl Viro iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); 371277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 372277e4ab7STrond Myklebust } 373277e4ab7STrond Myklebust 374277e4ab7STrond Myklebust static ssize_t 375277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 376277e4ab7STrond Myklebust size_t count) 377277e4ab7STrond Myklebust { 378b76a5afdSTrond Myklebust iov_iter_discard(&msg->msg_iter, READ, count); 379b76a5afdSTrond Myklebust return sock_recvmsg(sock, msg, flags); 380277e4ab7STrond Myklebust } 381277e4ab7STrond Myklebust 3826a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 3836a829eb8STrond Myklebust static void 3846a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 3856a829eb8STrond Myklebust { 3866a829eb8STrond Myklebust struct bvec_iter bi = { 3876a829eb8STrond Myklebust .bi_size = count, 3886a829eb8STrond Myklebust }; 3896a829eb8STrond Myklebust struct bio_vec bv; 3906a829eb8STrond Myklebust 3916a829eb8STrond Myklebust bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 3926a829eb8STrond Myklebust for_each_bvec(bv, bvec, bi, bi) 3936a829eb8STrond Myklebust flush_dcache_page(bv.bv_page); 3946a829eb8STrond Myklebust } 3956a829eb8STrond Myklebust #else 3966a829eb8STrond Myklebust static inline void 3976a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 3986a829eb8STrond Myklebust { 3996a829eb8STrond Myklebust } 4006a829eb8STrond Myklebust #endif 4016a829eb8STrond Myklebust 402277e4ab7STrond Myklebust static ssize_t 403277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 404277e4ab7STrond Myklebust struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 405277e4ab7STrond Myklebust { 406277e4ab7STrond Myklebust size_t want, seek_init = seek, offset = 0; 407277e4ab7STrond Myklebust ssize_t ret; 408277e4ab7STrond Myklebust 409277e4ab7STrond Myklebust want = min_t(size_t, count, buf->head[0].iov_len); 410e92053a5STrond Myklebust if (seek < want) { 411277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 412277e4ab7STrond Myklebust if (ret <= 0) 413277e4ab7STrond Myklebust goto sock_err; 414277e4ab7STrond Myklebust offset += ret; 415277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 416277e4ab7STrond Myklebust goto out; 417277e4ab7STrond Myklebust if (ret != want) 418c4433055STrond Myklebust goto out; 419277e4ab7STrond Myklebust seek = 0; 420277e4ab7STrond Myklebust } else { 421e92053a5STrond Myklebust seek -= want; 422e92053a5STrond Myklebust offset += want; 423277e4ab7STrond Myklebust } 42416e5e90fSTrond Myklebust 425277e4ab7STrond Myklebust want = xs_alloc_sparse_pages(buf, 426277e4ab7STrond Myklebust min_t(size_t, count - offset, buf->page_len), 42712a3ad61STrond Myklebust GFP_KERNEL); 42816e5e90fSTrond Myklebust if (seek < want) { 429277e4ab7STrond Myklebust ret = xs_read_bvec(sock, msg, flags, buf->bvec, 430277e4ab7STrond Myklebust xdr_buf_pagecount(buf), 431277e4ab7STrond Myklebust want + buf->page_base, 432277e4ab7STrond Myklebust seek + buf->page_base); 433277e4ab7STrond Myklebust if (ret <= 0) 434277e4ab7STrond Myklebust goto sock_err; 4356a829eb8STrond Myklebust xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 436ac9645c8SDan Aloni ret -= buf->page_base; 437ac9645c8SDan Aloni offset += ret; 438277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 439277e4ab7STrond Myklebust goto out; 440277e4ab7STrond Myklebust if (ret != want) 441c4433055STrond Myklebust goto out; 442277e4ab7STrond Myklebust seek = 0; 443277e4ab7STrond Myklebust } else { 44416e5e90fSTrond Myklebust seek -= want; 44516e5e90fSTrond Myklebust offset += want; 446277e4ab7STrond Myklebust } 44716e5e90fSTrond Myklebust 448277e4ab7STrond Myklebust want = min_t(size_t, count - offset, buf->tail[0].iov_len); 449e92053a5STrond Myklebust if (seek < want) { 450277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 451277e4ab7STrond Myklebust if (ret <= 0) 452277e4ab7STrond Myklebust goto sock_err; 453277e4ab7STrond Myklebust offset += ret; 454277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 455277e4ab7STrond Myklebust goto out; 456277e4ab7STrond Myklebust if (ret != want) 457c4433055STrond Myklebust goto out; 4589734ad57STrond Myklebust } else if (offset < seek_init) 459e92053a5STrond Myklebust offset = seek_init; 460277e4ab7STrond Myklebust ret = -EMSGSIZE; 461277e4ab7STrond Myklebust out: 462277e4ab7STrond Myklebust *read = offset - seek_init; 463277e4ab7STrond Myklebust return ret; 464277e4ab7STrond Myklebust sock_err: 465277e4ab7STrond Myklebust offset += seek; 466277e4ab7STrond Myklebust goto out; 467277e4ab7STrond Myklebust } 468277e4ab7STrond Myklebust 469277e4ab7STrond Myklebust static void 470277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 471277e4ab7STrond Myklebust { 472277e4ab7STrond Myklebust if (!transport->recv.copied) { 473277e4ab7STrond Myklebust if (buf->head[0].iov_len >= transport->recv.offset) 474277e4ab7STrond Myklebust memcpy(buf->head[0].iov_base, 475277e4ab7STrond Myklebust &transport->recv.xid, 476277e4ab7STrond Myklebust transport->recv.offset); 477277e4ab7STrond Myklebust transport->recv.copied = transport->recv.offset; 478277e4ab7STrond Myklebust } 479277e4ab7STrond Myklebust } 480277e4ab7STrond Myklebust 481277e4ab7STrond Myklebust static bool 482277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport) 483277e4ab7STrond Myklebust { 484277e4ab7STrond Myklebust return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 485277e4ab7STrond Myklebust } 486277e4ab7STrond Myklebust 487e92053a5STrond Myklebust static void 488e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport, 489e92053a5STrond Myklebust struct msghdr *msg) 490e92053a5STrond Myklebust { 491e92053a5STrond Myklebust if (xs_read_stream_request_done(transport)) 492e92053a5STrond Myklebust msg->msg_flags |= MSG_EOR; 493e92053a5STrond Myklebust } 494e92053a5STrond Myklebust 495277e4ab7STrond Myklebust static ssize_t 496277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 497277e4ab7STrond Myklebust int flags, struct rpc_rqst *req) 498277e4ab7STrond Myklebust { 499277e4ab7STrond Myklebust struct xdr_buf *buf = &req->rq_private_buf; 5003f649ab7SKees Cook size_t want, read; 5013f649ab7SKees Cook ssize_t ret; 502277e4ab7STrond Myklebust 503277e4ab7STrond Myklebust xs_read_header(transport, buf); 504277e4ab7STrond Myklebust 505277e4ab7STrond Myklebust want = transport->recv.len - transport->recv.offset; 506e92053a5STrond Myklebust if (want != 0) { 507277e4ab7STrond Myklebust ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 508e92053a5STrond Myklebust transport->recv.copied + want, 509e92053a5STrond Myklebust transport->recv.copied, 510277e4ab7STrond Myklebust &read); 511277e4ab7STrond Myklebust transport->recv.offset += read; 512277e4ab7STrond Myklebust transport->recv.copied += read; 513277e4ab7STrond Myklebust } 514277e4ab7STrond Myklebust 515727fcc64STrond Myklebust if (transport->recv.offset == transport->recv.len) 516727fcc64STrond Myklebust xs_read_stream_check_eor(transport, msg); 517727fcc64STrond Myklebust 518e92053a5STrond Myklebust if (want == 0) 519e92053a5STrond Myklebust return 0; 520e92053a5STrond Myklebust 521277e4ab7STrond Myklebust switch (ret) { 522c4433055STrond Myklebust default: 523c4433055STrond Myklebust break; 52426781eabSTrond Myklebust case -EFAULT: 525277e4ab7STrond Myklebust case -EMSGSIZE: 52626781eabSTrond Myklebust msg->msg_flags |= MSG_TRUNC; 527c4433055STrond Myklebust return read; 528277e4ab7STrond Myklebust case 0: 529277e4ab7STrond Myklebust return -ESHUTDOWN; 530277e4ab7STrond Myklebust } 531c4433055STrond Myklebust return ret < 0 ? ret : read; 532277e4ab7STrond Myklebust } 533277e4ab7STrond Myklebust 534277e4ab7STrond Myklebust static size_t 535277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag) 536277e4ab7STrond Myklebust { 537277e4ab7STrond Myklebust if (isfrag) 538277e4ab7STrond Myklebust return sizeof(__be32); 539277e4ab7STrond Myklebust return 3 * sizeof(__be32); 540277e4ab7STrond Myklebust } 541277e4ab7STrond Myklebust 542277e4ab7STrond Myklebust static ssize_t 543277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 544277e4ab7STrond Myklebust int flags, size_t want, size_t seek) 545277e4ab7STrond Myklebust { 546277e4ab7STrond Myklebust struct kvec kvec = { 547277e4ab7STrond Myklebust .iov_base = &transport->recv.fraghdr, 548277e4ab7STrond Myklebust .iov_len = want, 549277e4ab7STrond Myklebust }; 550277e4ab7STrond Myklebust return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 551277e4ab7STrond Myklebust } 552277e4ab7STrond Myklebust 553277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 554277e4ab7STrond Myklebust static ssize_t 555277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 556277e4ab7STrond Myklebust { 557277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 558277e4ab7STrond Myklebust struct rpc_rqst *req; 559277e4ab7STrond Myklebust ssize_t ret; 560277e4ab7STrond Myklebust 56198b5cee3SBenjamin Coddington /* Is this transport associated with the backchannel? */ 56298b5cee3SBenjamin Coddington if (!xprt->bc_serv) 56398b5cee3SBenjamin Coddington return -ESHUTDOWN; 56498b5cee3SBenjamin Coddington 565277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 566277e4ab7STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 567277e4ab7STrond Myklebust if (!req) { 568277e4ab7STrond Myklebust printk(KERN_WARNING "Callback slot table overflowed\n"); 569277e4ab7STrond Myklebust return -ESHUTDOWN; 570277e4ab7STrond Myklebust } 57145835a63STrond Myklebust if (transport->recv.copied && !req->rq_private_buf.len) 57245835a63STrond Myklebust return -ESHUTDOWN; 573277e4ab7STrond Myklebust 574277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 575277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 576c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 57745835a63STrond Myklebust else 57845835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 579277e4ab7STrond Myklebust 580277e4ab7STrond Myklebust return ret; 581277e4ab7STrond Myklebust } 582277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 583277e4ab7STrond Myklebust static ssize_t 584277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 585277e4ab7STrond Myklebust { 586277e4ab7STrond Myklebust return -ESHUTDOWN; 587277e4ab7STrond Myklebust } 588277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 589277e4ab7STrond Myklebust 590277e4ab7STrond Myklebust static ssize_t 591277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 592277e4ab7STrond Myklebust { 593277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 594277e4ab7STrond Myklebust struct rpc_rqst *req; 595277e4ab7STrond Myklebust ssize_t ret = 0; 596277e4ab7STrond Myklebust 597277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 598277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 599277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 60045835a63STrond Myklebust if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { 601277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 602277e4ab7STrond Myklebust goto out; 603277e4ab7STrond Myklebust } 604277e4ab7STrond Myklebust xprt_pin_rqst(req); 605277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 606277e4ab7STrond Myklebust 607277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 608277e4ab7STrond Myklebust 609277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 610277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 611c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 61245835a63STrond Myklebust else 61345835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 614277e4ab7STrond Myklebust xprt_unpin_rqst(req); 615277e4ab7STrond Myklebust out: 616277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 617277e4ab7STrond Myklebust return ret; 618277e4ab7STrond Myklebust } 619277e4ab7STrond Myklebust 620277e4ab7STrond Myklebust static ssize_t 621277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 622277e4ab7STrond Myklebust { 623277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 624277e4ab7STrond Myklebust size_t want, read = 0; 625277e4ab7STrond Myklebust ssize_t ret = 0; 626277e4ab7STrond Myklebust 627277e4ab7STrond Myklebust if (transport->recv.len == 0) { 628277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 629277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 630277e4ab7STrond Myklebust transport->recv.offset); 631277e4ab7STrond Myklebust if (ret <= 0) 632277e4ab7STrond Myklebust goto out_err; 633277e4ab7STrond Myklebust transport->recv.offset = ret; 634c4433055STrond Myklebust if (transport->recv.offset != want) 635c4433055STrond Myklebust return transport->recv.offset; 636277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 637277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 638277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 639277e4ab7STrond Myklebust read = ret; 640277e4ab7STrond Myklebust } 641277e4ab7STrond Myklebust 642277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 643c4433055STrond Myklebust default: 644c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 645c4433055STrond Myklebust break; 646277e4ab7STrond Myklebust case RPC_CALL: 647277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 648277e4ab7STrond Myklebust break; 649277e4ab7STrond Myklebust case RPC_REPLY: 650277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 651277e4ab7STrond Myklebust } 652277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 653277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 654277e4ab7STrond Myklebust transport->recv.copied = -1; 655277e4ab7STrond Myklebust } 656277e4ab7STrond Myklebust if (ret < 0) 657277e4ab7STrond Myklebust goto out_err; 658277e4ab7STrond Myklebust read += ret; 659277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 660c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 661c4433055STrond Myklebust return read; 662b76a5afdSTrond Myklebust msg.msg_flags = 0; 663277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 664277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 665277e4ab7STrond Myklebust if (ret <= 0) 666277e4ab7STrond Myklebust goto out_err; 667277e4ab7STrond Myklebust transport->recv.offset += ret; 668277e4ab7STrond Myklebust read += ret; 669277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 670c4433055STrond Myklebust return read; 671277e4ab7STrond Myklebust } 672277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 673c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 674277e4ab7STrond Myklebust transport->recv.copied = 0; 675277e4ab7STrond Myklebust } 676277e4ab7STrond Myklebust transport->recv.offset = 0; 677277e4ab7STrond Myklebust transport->recv.len = 0; 678277e4ab7STrond Myklebust return read; 679277e4ab7STrond Myklebust out_err: 68079462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 681277e4ab7STrond Myklebust } 682277e4ab7STrond Myklebust 6830ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 6840ffe86f4STrond Myklebust { 685a73881c9STrond Myklebust return transport->sock->ops->poll(transport->file, transport->sock, 686a73881c9STrond Myklebust NULL); 6870ffe86f4STrond Myklebust } 6880ffe86f4STrond Myklebust 6890ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 6900ffe86f4STrond Myklebust { 6910ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 6920ffe86f4STrond Myklebust 6930ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 6940ffe86f4STrond Myklebust } 6950ffe86f4STrond Myklebust 6960ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 6970ffe86f4STrond Myklebust { 6980ffe86f4STrond Myklebust 6990ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 7000ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 7010ffe86f4STrond Myklebust return; 7020ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 7030ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 7040ffe86f4STrond Myklebust } 7050ffe86f4STrond Myklebust 706c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 707c50b8ee0STrond Myklebust { 708c50b8ee0STrond Myklebust size_t read = 0; 709c50b8ee0STrond Myklebust ssize_t ret = 0; 710c50b8ee0STrond Myklebust 711c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 712c50b8ee0STrond Myklebust if (transport->sock == NULL) 713c50b8ee0STrond Myklebust goto out; 714c50b8ee0STrond Myklebust for (;;) { 715c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 716c4433055STrond Myklebust if (ret < 0) 717c50b8ee0STrond Myklebust break; 718c50b8ee0STrond Myklebust read += ret; 719c50b8ee0STrond Myklebust cond_resched(); 720c50b8ee0STrond Myklebust } 7215f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7225f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 7235f52a9d4STrond Myklebust else 7240ffe86f4STrond Myklebust xs_poll_check_readable(transport); 725c50b8ee0STrond Myklebust out: 726c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 727c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 728c50b8ee0STrond Myklebust } 729c50b8ee0STrond Myklebust 730c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 731c50b8ee0STrond Myklebust { 732c50b8ee0STrond Myklebust struct sock_xprt *transport = 733c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 734a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 735a1231fdaSTrond Myklebust 736c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 737a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 738c50b8ee0STrond Myklebust } 739c50b8ee0STrond Myklebust 740550aebfeSTrond Myklebust static void 741550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 742550aebfeSTrond Myklebust { 743550aebfeSTrond Myklebust transport->recv.offset = 0; 744550aebfeSTrond Myklebust transport->recv.len = 0; 745550aebfeSTrond Myklebust transport->recv.copied = 0; 746550aebfeSTrond Myklebust transport->xmit.offset = 0; 747ae053551STrond Myklebust } 748ae053551STrond Myklebust 749ae053551STrond Myklebust static void 750ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 751ae053551STrond Myklebust { 752550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 753550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 754550aebfeSTrond Myklebust } 755550aebfeSTrond Myklebust 756b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 757b4b5cc85SChuck Lever 7589903cd1cSChuck Lever /** 759c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 76050f484e2STrond Myklebust * @req: pointer to RPC request 7619903cd1cSChuck Lever * 762a246b010SChuck Lever */ 763c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req) 764a246b010SChuck Lever { 765262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 766ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 76706ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 76824ca9a84STrond Myklebust int ret = -EAGAIN; 769a246b010SChuck Lever 770015747d2SChuck Lever trace_rpc_socket_nospace(req, transport); 771a246b010SChuck Lever 772262965f5SChuck Lever /* Protect against races with write_space */ 773b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 774a246b010SChuck Lever 775262965f5SChuck Lever /* Don't race with disconnect */ 776b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 77713331a55STrond Myklebust /* wait for more buffer space */ 77806ea0bfeSTrond Myklebust sk->sk_write_pending++; 779c544577dSTrond Myklebust xprt_wait_for_buffer_space(xprt); 78013331a55STrond Myklebust } else 7815e3771ceSTrond Myklebust ret = -ENOTCONN; 782a246b010SChuck Lever 783b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 78406ea0bfeSTrond Myklebust 78506ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 786d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 787d48f9ce7SDavid Vrabel struct socket_wq *wq; 788d48f9ce7SDavid Vrabel 789d48f9ce7SDavid Vrabel rcu_read_lock(); 790d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 791d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 792d48f9ce7SDavid Vrabel rcu_read_unlock(); 793d48f9ce7SDavid Vrabel 79406ea0bfeSTrond Myklebust sk->sk_write_space(sk); 795d48f9ce7SDavid Vrabel } 7965e3771ceSTrond Myklebust return ret; 797a246b010SChuck Lever } 798a246b010SChuck Lever 799277e4ab7STrond Myklebust static void 800277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req) 801277e4ab7STrond Myklebust { 80275369089STrond Myklebust xdr_free_bvec(&req->rq_rcv_buf); 80312a3ad61STrond Myklebust req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL); 804277e4ab7STrond Myklebust } 805277e4ab7STrond Myklebust 80661677eeeSChuck Lever /* 8074cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 8084cd34e7cSTrond Myklebust * could complete transmission. 8094cd34e7cSTrond Myklebust */ 8104cd34e7cSTrond Myklebust static bool 8114cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 8124cd34e7cSTrond Myklebust { 8134cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 8144cd34e7cSTrond Myklebust } 8154cd34e7cSTrond Myklebust 8164cd34e7cSTrond Myklebust /* 81706b5fc3aSTrond Myklebust * Return the stream record marker field for a record of length < 2^31-1 81861677eeeSChuck Lever */ 81906b5fc3aSTrond Myklebust static rpc_fraghdr 82006b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr) 82161677eeeSChuck Lever { 82206b5fc3aSTrond Myklebust if (!xdr->len) 82306b5fc3aSTrond Myklebust return 0; 82406b5fc3aSTrond Myklebust return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 82561677eeeSChuck Lever } 82661677eeeSChuck Lever 8279903cd1cSChuck Lever /** 828176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 82950f484e2STrond Myklebust * @req: pointer to RPC request 830176e21eeSChuck Lever * 831176e21eeSChuck Lever * Return values: 832176e21eeSChuck Lever * 0: The request has been sent 833176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 834176e21eeSChuck Lever * complete the request 835176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 83612b20ce3SBhaskar Chowdhury * other: Some other error occurred, the request was not sent 837176e21eeSChuck Lever */ 838adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 839176e21eeSChuck Lever { 840176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 841176e21eeSChuck Lever struct sock_xprt *transport = 842176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 843176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 8447e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 8457e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 8469e55eef4SChuck Lever struct msghdr msg = { 8479e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 8489e55eef4SChuck Lever }; 8493f649ab7SKees Cook unsigned int sent; 850176e21eeSChuck Lever int status; 851176e21eeSChuck Lever 8524cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 8534cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 8544cd34e7cSTrond Myklebust xs_close(xprt); 8554cd34e7cSTrond Myklebust return -ENOTCONN; 8564cd34e7cSTrond Myklebust } 8574cd34e7cSTrond Myklebust 858176e21eeSChuck Lever xs_pktdump("packet data:", 859176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 860176e21eeSChuck Lever 86178215759SChuck Lever req->rq_xtime = ktime_get(); 8629e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 8637e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 864176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 8656c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 866743c69e7SNeilBrown 867743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 868743c69e7SNeilBrown status = -ENOBUFS; 869743c69e7SNeilBrown 870f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 8716c7a64e5STrond Myklebust transport->xmit.offset += sent; 8726c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 8737e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 8746c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 8756c7a64e5STrond Myklebust transport->xmit.offset = 0; 876176e21eeSChuck Lever return 0; 877176e21eeSChuck Lever } 878176e21eeSChuck Lever status = -EAGAIN; 879176e21eeSChuck Lever } 880176e21eeSChuck Lever 881176e21eeSChuck Lever switch (status) { 8823601c4a9STrond Myklebust case -ENOBUFS: 883b5872f0cSTrond Myklebust break; 884176e21eeSChuck Lever case -EAGAIN: 885c544577dSTrond Myklebust status = xs_nospace(req); 886176e21eeSChuck Lever break; 887176e21eeSChuck Lever default: 888176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 889176e21eeSChuck Lever -status); 890df561f66SGustavo A. R. Silva fallthrough; 891176e21eeSChuck Lever case -EPIPE: 892176e21eeSChuck Lever xs_close(xprt); 893176e21eeSChuck Lever status = -ENOTCONN; 894176e21eeSChuck Lever } 895176e21eeSChuck Lever 896176e21eeSChuck Lever return status; 897176e21eeSChuck Lever } 898176e21eeSChuck Lever 899176e21eeSChuck Lever /** 900262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 90150f484e2STrond Myklebust * @req: pointer to RPC request 9029903cd1cSChuck Lever * 9039903cd1cSChuck Lever * Return values: 9049903cd1cSChuck Lever * 0: The request has been sent 9059903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 9069903cd1cSChuck Lever * complete the request 907262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 90825985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 9099903cd1cSChuck Lever */ 910adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 911a246b010SChuck Lever { 912a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 913ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 914262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9159e55eef4SChuck Lever struct msghdr msg = { 9169e55eef4SChuck Lever .msg_name = xs_addr(xprt), 9179e55eef4SChuck Lever .msg_namelen = xprt->addrlen, 9189e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9199e55eef4SChuck Lever }; 9203f649ab7SKees Cook unsigned int sent; 921262965f5SChuck Lever int status; 922262965f5SChuck Lever 923262965f5SChuck Lever xs_pktdump("packet data:", 924262965f5SChuck Lever req->rq_svec->iov_base, 925262965f5SChuck Lever req->rq_svec->iov_len); 926262965f5SChuck Lever 92701d37c42STrond Myklebust if (!xprt_bound(xprt)) 92801d37c42STrond Myklebust return -ENOTCONN; 92975891f50STrond Myklebust 93075891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 93175891f50STrond Myklebust return -EBADSLT; 93275891f50STrond Myklebust 93378215759SChuck Lever req->rq_xtime = ktime_get(); 9349e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent); 935262965f5SChuck Lever 936262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 9376c7a64e5STrond Myklebust xdr->len, status); 938262965f5SChuck Lever 9393dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 9403dedbb5cSJason Baron if (status == -EPERM) 9413dedbb5cSJason Baron goto process_status; 9423dedbb5cSJason Baron 943743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 944743c69e7SNeilBrown status = -ENOBUFS; 945743c69e7SNeilBrown 946f279cd00SJason Baron if (sent > 0 || status == 0) { 947f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 948f279cd00SJason Baron if (sent >= req->rq_slen) 949262965f5SChuck Lever return 0; 950262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 951262965f5SChuck Lever status = -EAGAIN; 9522199700fSTrond Myklebust } 953262965f5SChuck Lever 9543dedbb5cSJason Baron process_status: 955262965f5SChuck Lever switch (status) { 956fba91afbSTrond Myklebust case -ENOTSOCK: 957fba91afbSTrond Myklebust status = -ENOTCONN; 958fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 959fba91afbSTrond Myklebust break; 960b6ddf64fSTrond Myklebust case -EAGAIN: 961c544577dSTrond Myklebust status = xs_nospace(req); 962b6ddf64fSTrond Myklebust break; 963262965f5SChuck Lever case -ENETUNREACH: 9643601c4a9STrond Myklebust case -ENOBUFS: 965262965f5SChuck Lever case -EPIPE: 966262965f5SChuck Lever case -ECONNREFUSED: 9673dedbb5cSJason Baron case -EPERM: 968262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 969262965f5SChuck Lever * prompts ECONNREFUSED. */ 97013331a55STrond Myklebust break; 97113331a55STrond Myklebust default: 97213331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 97313331a55STrond Myklebust -status); 974262965f5SChuck Lever } 9755fe46e9dSBian Naimeng 976262965f5SChuck Lever return status; 977262965f5SChuck Lever } 978262965f5SChuck Lever 979e06799f9STrond Myklebust /** 980262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 98150f484e2STrond Myklebust * @req: pointer to RPC request 982262965f5SChuck Lever * 983262965f5SChuck Lever * Return values: 984262965f5SChuck Lever * 0: The request has been sent 985262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 986262965f5SChuck Lever * complete the request 987262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 98825985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 989262965f5SChuck Lever * 990262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 991262965f5SChuck Lever * if sendmsg is not able to make progress? 992262965f5SChuck Lever */ 993adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 994262965f5SChuck Lever { 995262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 996ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 997262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9987e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 9997e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 10009e55eef4SChuck Lever struct msghdr msg = { 10019e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 10029e55eef4SChuck Lever }; 10039ffadfbcSTrond Myklebust bool vm_wait = false; 10043f649ab7SKees Cook unsigned int sent; 1005b595bb15SChuck Lever int status; 1006a246b010SChuck Lever 10074cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 10084cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 10094cd34e7cSTrond Myklebust if (transport->sock != NULL) 10104cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 10114cd34e7cSTrond Myklebust return -ENOTCONN; 10124cd34e7cSTrond Myklebust } 1013*d275880aSTrond Myklebust if (!transport->inet) 1014*d275880aSTrond Myklebust return -ENOTCONN; 10154cd34e7cSTrond Myklebust 1016262965f5SChuck Lever xs_pktdump("packet data:", 1017262965f5SChuck Lever req->rq_svec->iov_base, 1018262965f5SChuck Lever req->rq_svec->iov_len); 1019a246b010SChuck Lever 10207196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 10217196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 10227196dbb0STrond Myklebust 1023a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1024a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1025262965f5SChuck Lever * called sendmsg(). */ 102678215759SChuck Lever req->rq_xtime = ktime_get(); 1027d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, true); 1028a246b010SChuck Lever while (1) { 10299e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 10307e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 1031a246b010SChuck Lever 1032262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 10336c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1034262965f5SChuck Lever 1035a246b010SChuck Lever /* If we've sent the entire packet, immediately 1036a246b010SChuck Lever * reset the count of bytes sent. */ 10376c7a64e5STrond Myklebust transport->xmit.offset += sent; 10386c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 10397e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 10406c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 10416c7a64e5STrond Myklebust transport->xmit.offset = 0; 1042d737e5d4STrond Myklebust if (atomic_long_read(&xprt->xmit_queuelen) == 1) 1043d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, false); 1044a246b010SChuck Lever return 0; 1045a246b010SChuck Lever } 1046262965f5SChuck Lever 10479ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 10489ffadfbcSTrond Myklebust 10499ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 10509ffadfbcSTrond Myklebust /* 10519ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 10529ffadfbcSTrond Myklebust * socket send buffer limits. 10539ffadfbcSTrond Myklebust */ 10549ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 10559ffadfbcSTrond Myklebust break; 10569ffadfbcSTrond Myklebust /* 10579ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 10589ffadfbcSTrond Myklebust */ 10599ffadfbcSTrond Myklebust if (sent == 0) { 10609ffadfbcSTrond Myklebust status = -ENOBUFS; 10619ffadfbcSTrond Myklebust if (vm_wait) 10629ffadfbcSTrond Myklebust break; 10639ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 10649ffadfbcSTrond Myklebust * socket send buffer limit 10659ffadfbcSTrond Myklebust */ 10669ffadfbcSTrond Myklebust vm_wait = true; 10679ffadfbcSTrond Myklebust } 10689ffadfbcSTrond Myklebust continue; 10699ffadfbcSTrond Myklebust } 1070f580dd04STrond Myklebust if (status < 0) 1071f580dd04STrond Myklebust break; 10729ffadfbcSTrond Myklebust vm_wait = false; 1073a246b010SChuck Lever } 1074a246b010SChuck Lever 1075262965f5SChuck Lever switch (status) { 1076fba91afbSTrond Myklebust case -ENOTSOCK: 1077fba91afbSTrond Myklebust status = -ENOTCONN; 1078fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1079fba91afbSTrond Myklebust break; 1080262965f5SChuck Lever case -EAGAIN: 1081c544577dSTrond Myklebust status = xs_nospace(req); 1082262965f5SChuck Lever break; 1083262965f5SChuck Lever case -ECONNRESET: 1084262965f5SChuck Lever case -ECONNREFUSED: 1085262965f5SChuck Lever case -ENOTCONN: 10863913c78cSTrond Myklebust case -EADDRINUSE: 1087b5872f0cSTrond Myklebust case -ENOBUFS: 1088b9d2bb2eSTrond Myklebust case -EPIPE: 108913331a55STrond Myklebust break; 109013331a55STrond Myklebust default: 109113331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 109213331a55STrond Myklebust -status); 1093a246b010SChuck Lever } 10945fe46e9dSBian Naimeng 1095a246b010SChuck Lever return status; 1096a246b010SChuck Lever } 1097a246b010SChuck Lever 10982a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 10992a9e1cfaSTrond Myklebust { 11002a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 11012a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 11022a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 11032118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 11042a9e1cfaSTrond Myklebust } 11052a9e1cfaSTrond Myklebust 11062a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11072a9e1cfaSTrond Myklebust { 11082a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 11092a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 11102a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 11112118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 11122118071dSTrond Myklebust } 11132118071dSTrond Myklebust 111442d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 111542d42a5bSTrond Myklebust { 111642d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 111742d42a5bSTrond Myklebust 111842d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 11194f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 11204f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 11214f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 11224f8943f8STrond Myklebust } 11234f8943f8STrond Myklebust 11244f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 11254f8943f8STrond Myklebust { 11264f8943f8STrond Myklebust set_bit(nr, &transport->sock_state); 11274f8943f8STrond Myklebust queue_work(xprtiod_workqueue, &transport->error_worker); 112842d42a5bSTrond Myklebust } 112942d42a5bSTrond Myklebust 1130b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1131b70ae915STrond Myklebust { 1132b70ae915STrond Myklebust smp_mb__before_atomic(); 1133b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1134b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 113542d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1136b70ae915STrond Myklebust smp_mb__after_atomic(); 1137b70ae915STrond Myklebust } 1138b70ae915STrond Myklebust 11392118071dSTrond Myklebust /** 11402118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 11412118071dSTrond Myklebust * @sk: socket 11422118071dSTrond Myklebust * 11432118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 11442118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 11452118071dSTrond Myklebust */ 11462118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 11472118071dSTrond Myklebust { 11484f8943f8STrond Myklebust struct sock_xprt *transport; 11492118071dSTrond Myklebust struct rpc_xprt *xprt; 11502118071dSTrond Myklebust 11512118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 11522118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 11532118071dSTrond Myklebust goto out; 11542118071dSTrond Myklebust 11554f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1156af84537dSBenjamin Coddington transport->xprt_err = -sk->sk_err; 1157af84537dSBenjamin Coddington if (transport->xprt_err == 0) 11582118071dSTrond Myklebust goto out; 11592118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 1160af84537dSBenjamin Coddington xprt, -transport->xprt_err); 1161af84537dSBenjamin Coddington trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); 1162af84537dSBenjamin Coddington 1163af84537dSBenjamin Coddington /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ 1164af84537dSBenjamin Coddington smp_mb__before_atomic(); 11654f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 11662118071dSTrond Myklebust out: 11672118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 11682a9e1cfaSTrond Myklebust } 11692a9e1cfaSTrond Myklebust 1170fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1171a246b010SChuck Lever { 1172ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1173ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 11746cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1175a73881c9STrond Myklebust struct file *filp = transport->file; 1176a246b010SChuck Lever 1177fe315e76SChuck Lever if (sk == NULL) 1178fe315e76SChuck Lever return; 11799903cd1cSChuck Lever 1180264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1181264d1df3SJeff Layton sk_clear_memalloc(sk); 1182264d1df3SJeff Layton 118309939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 118409939204STrond Myklebust 1185edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1186a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1187ee0ac0c2SChuck Lever transport->inet = NULL; 1188ee0ac0c2SChuck Lever transport->sock = NULL; 1189a73881c9STrond Myklebust transport->file = NULL; 1190a246b010SChuck Lever 1191a246b010SChuck Lever sk->sk_user_data = NULL; 11922a9e1cfaSTrond Myklebust 11932a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 11940c78789eSTrond Myklebust xprt_clear_connected(xprt); 1195a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 11966cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1197ae053551STrond Myklebust /* Reset stream record info */ 1198ae053551STrond Myklebust xs_stream_reset_connect(transport); 1199edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1200a246b010SChuck Lever 12016cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1202a73881c9STrond Myklebust fput(filp); 12030445f92cSTrond Myklebust 12040445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1205fe315e76SChuck Lever } 1206fe315e76SChuck Lever 1207fe315e76SChuck Lever /** 1208fe315e76SChuck Lever * xs_close - close a socket 1209fe315e76SChuck Lever * @xprt: transport 1210fe315e76SChuck Lever * 1211fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1212fe315e76SChuck Lever * on the server we want to save. 1213f75e6745STrond Myklebust * 1214f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1215f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1216fe315e76SChuck Lever */ 1217fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1218fe315e76SChuck Lever { 1219fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1220fe315e76SChuck Lever 1221fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1222fe315e76SChuck Lever 1223fe315e76SChuck Lever xs_reset_transport(transport); 122461d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1225a246b010SChuck Lever } 1226a246b010SChuck Lever 12274a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 12284a068258SChuck Lever { 12294a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 12304a068258SChuck Lever xprt); 12314a068258SChuck Lever xprt_disconnect_done(xprt); 12324a068258SChuck Lever } 12334a068258SChuck Lever 1234315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1235315f3812SKinglong Mee { 1236315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1237315f3812SKinglong Mee xprt_free(xprt); 1238315f3812SKinglong Mee } 1239315f3812SKinglong Mee 12409903cd1cSChuck Lever /** 12419903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 12429903cd1cSChuck Lever * @xprt: doomed transport 12439903cd1cSChuck Lever * 12449903cd1cSChuck Lever */ 12459903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1246a246b010SChuck Lever { 124703c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 124803c78827STrond Myklebust struct sock_xprt, xprt); 12499903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 12509903cd1cSChuck Lever 125103c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1252a1311d87STrond Myklebust xs_close(xprt); 1253edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1254b5e92419STrond Myklebust cancel_work_sync(&transport->error_worker); 1255315f3812SKinglong Mee xs_xprt_free(xprt); 1256a1311d87STrond Myklebust module_put(THIS_MODULE); 1257a246b010SChuck Lever } 1258a246b010SChuck Lever 12599903cd1cSChuck Lever /** 1260f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1261f9b2ee71STrond Myklebust * @xprt: transport 1262f9b2ee71STrond Myklebust * @sk: socket 1263f9b2ee71STrond Myklebust * @skb: skbuff 12649903cd1cSChuck Lever * 1265a246b010SChuck Lever */ 1266f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1267f9b2ee71STrond Myklebust struct sock *sk, 1268f9b2ee71STrond Myklebust struct sk_buff *skb) 1269a246b010SChuck Lever { 1270a246b010SChuck Lever struct rpc_task *task; 1271a246b010SChuck Lever struct rpc_rqst *rovr; 1272f9b2ee71STrond Myklebust int repsize, copied; 1273d8ed029dSAlexey Dobriyan u32 _xid; 1274d8ed029dSAlexey Dobriyan __be32 *xp; 1275a246b010SChuck Lever 12761da8c681SWillem de Bruijn repsize = skb->len; 1277a246b010SChuck Lever if (repsize < 4) { 12789903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1279f9b2ee71STrond Myklebust return; 1280a246b010SChuck Lever } 1281a246b010SChuck Lever 1282a246b010SChuck Lever /* Copy the XID from the skb... */ 12831da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1284a246b010SChuck Lever if (xp == NULL) 1285f9b2ee71STrond Myklebust return; 1286a246b010SChuck Lever 1287a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 128875c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1289a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1290a246b010SChuck Lever if (!rovr) 1291a246b010SChuck Lever goto out_unlock; 1292729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1293ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 129475c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1295a246b010SChuck Lever task = rovr->rq_task; 1296a246b010SChuck Lever 1297a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1298a246b010SChuck Lever copied = repsize; 1299a246b010SChuck Lever 1300a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 13011781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 130275c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13030afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1304729749bbSTrond Myklebust goto out_unpin; 13051781f7f5SHerbert Xu } 13061781f7f5SHerbert Xu 1307a246b010SChuck Lever 1308b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 13096a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1310b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 131175c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13121570c1e4SChuck Lever xprt_complete_rqst(task, copied); 13130afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1314729749bbSTrond Myklebust out_unpin: 1315729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1316a246b010SChuck Lever out_unlock: 131775c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1318f9b2ee71STrond Myklebust } 1319f9b2ee71STrond Myklebust 1320f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1321f9b2ee71STrond Myklebust { 1322f9b2ee71STrond Myklebust struct sk_buff *skb; 1323f9b2ee71STrond Myklebust struct sock *sk; 1324f9b2ee71STrond Myklebust int err; 1325f9b2ee71STrond Myklebust 1326f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1327f9b2ee71STrond Myklebust sk = transport->inet; 1328f9b2ee71STrond Myklebust if (sk == NULL) 1329f9b2ee71STrond Myklebust goto out; 1330f9b2ee71STrond Myklebust for (;;) { 13317c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 13324f546149STrond Myklebust if (skb == NULL) 13334f546149STrond Myklebust break; 1334f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1335850cbaddSPaolo Abeni consume_skb(skb); 13360af3442aSTrond Myklebust cond_resched(); 1337f9b2ee71STrond Myklebust } 13380ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1339a246b010SChuck Lever out: 1340f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1341f9b2ee71STrond Myklebust } 1342f9b2ee71STrond Myklebust 1343f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1344f9b2ee71STrond Myklebust { 1345f9b2ee71STrond Myklebust struct sock_xprt *transport = 1346f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1347a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1348a1231fdaSTrond Myklebust 1349f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1350a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1351f9b2ee71STrond Myklebust } 1352f9b2ee71STrond Myklebust 1353f9b2ee71STrond Myklebust /** 1354f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1355f9b2ee71STrond Myklebust * @sk: socket with data to read 1356f9b2ee71STrond Myklebust * 1357f9b2ee71STrond Myklebust */ 1358f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1359f9b2ee71STrond Myklebust { 1360f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1361f9b2ee71STrond Myklebust 1362f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1363f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1364f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1365f9b2ee71STrond Myklebust if (xprt != NULL) { 1366f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1367f9b2ee71STrond Myklebust struct sock_xprt, xprt); 13685157b956STrond Myklebust transport->old_data_ready(sk); 13695157b956STrond Myklebust /* Any data means we had a useful conversation, so 13705157b956STrond Myklebust * then we don't need to delay the next reconnect 13715157b956STrond Myklebust */ 13725157b956STrond Myklebust if (xprt->reestablish_timeout) 13735157b956STrond Myklebust xprt->reestablish_timeout = 0; 137442d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 137540a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1376f9b2ee71STrond Myklebust } 1377f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1378a246b010SChuck Lever } 1379a246b010SChuck Lever 1380a519fc7aSTrond Myklebust /* 1381a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1382a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1383a519fc7aSTrond Myklebust */ 1384a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1385a519fc7aSTrond Myklebust { 1386a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1387a519fc7aSTrond Myklebust } 1388a519fc7aSTrond Myklebust 13899e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 13906b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 13916b26cc8cSChuck Lever { 13926b26cc8cSChuck Lever return PAGE_SIZE; 13936b26cc8cSChuck Lever } 13949e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 139544b98efdSRicardo Labiaga 13969903cd1cSChuck Lever /** 13979903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 13989903cd1cSChuck Lever * @sk: socket whose state has changed 13999903cd1cSChuck Lever * 14009903cd1cSChuck Lever */ 14019903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1402a246b010SChuck Lever { 1403a246b010SChuck Lever struct rpc_xprt *xprt; 14040fdea1e8STrond Myklebust struct sock_xprt *transport; 1405a246b010SChuck Lever 1406f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1407a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1408a246b010SChuck Lever goto out; 14099903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1410669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1411a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1412a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1413669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1414669502ffSAndy Chittenden sk->sk_shutdown); 1415a246b010SChuck Lever 14160fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 141740b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1418a246b010SChuck Lever switch (sk->sk_state) { 1419a246b010SChuck Lever case TCP_ESTABLISHED: 1420a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 14218b71798cSTrond Myklebust xprt->connect_cookie++; 14220fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 14230fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 142451971139SChuck Lever 14253968a8a5SChuck Lever xprt->stat.connect_count++; 14263968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 14273968a8a5SChuck Lever xprt->stat.connect_start; 14284f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1429a246b010SChuck Lever } 1430a246b010SChuck Lever break; 14313b948ae5STrond Myklebust case TCP_FIN_WAIT1: 14323b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 14337c1d71cfSTrond Myklebust xprt->connect_cookie++; 1434663b8858STrond Myklebust xprt->reestablish_timeout = 0; 14353b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 14364e857c58SPeter Zijlstra smp_mb__before_atomic(); 14373b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1438ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 14394e857c58SPeter Zijlstra smp_mb__after_atomic(); 1440a246b010SChuck Lever break; 1441632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 14423b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 14437c1d71cfSTrond Myklebust xprt->connect_cookie++; 1444d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 14454f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1446df561f66SGustavo A. R. Silva fallthrough; 1447663b8858STrond Myklebust case TCP_CLOSING: 1448663b8858STrond Myklebust /* 1449663b8858STrond Myklebust * If the server closed down the connection, make sure that 1450663b8858STrond Myklebust * we back off before reconnecting 1451663b8858STrond Myklebust */ 1452663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1453663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 14543b948ae5STrond Myklebust break; 14553b948ae5STrond Myklebust case TCP_LAST_ACK: 1456670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 14574e857c58SPeter Zijlstra smp_mb__before_atomic(); 14583b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 14594e857c58SPeter Zijlstra smp_mb__after_atomic(); 14603b948ae5STrond Myklebust break; 14613b948ae5STrond Myklebust case TCP_CLOSE: 14620fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 14630fdea1e8STrond Myklebust &transport->sock_state)) 14640fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 14659b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 14669b30889cSTrond Myklebust /* Trigger the socket release */ 14674f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1468a246b010SChuck Lever } 1469a246b010SChuck Lever out: 1470f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1471a246b010SChuck Lever } 1472a246b010SChuck Lever 14731f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 14741f0fa154SIlpo Järvinen { 147513331a55STrond Myklebust struct socket_wq *wq; 14764f8943f8STrond Myklebust struct sock_xprt *transport; 14771f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 14781f0fa154SIlpo Järvinen 147913331a55STrond Myklebust if (!sk->sk_socket) 14801f0fa154SIlpo Järvinen return; 148113331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 14821f0fa154SIlpo Järvinen 14831f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 14841f0fa154SIlpo Järvinen return; 14854f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 148613331a55STrond Myklebust rcu_read_lock(); 148713331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 148813331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 148913331a55STrond Myklebust goto out; 14901f0fa154SIlpo Järvinen 14914f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1492c544577dSTrond Myklebust sk->sk_write_pending--; 149313331a55STrond Myklebust out: 149413331a55STrond Myklebust rcu_read_unlock(); 14951f0fa154SIlpo Järvinen } 14961f0fa154SIlpo Järvinen 14972a9e1cfaSTrond Myklebust /** 1498c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1499c7b2cae8SChuck Lever * becomes available 15009903cd1cSChuck Lever * @sk: socket whose state has changed 15019903cd1cSChuck Lever * 1502a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1503a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1504c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1505a246b010SChuck Lever * with a bunch of small requests. 1506a246b010SChuck Lever */ 1507c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1508a246b010SChuck Lever { 1509f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1510c7b2cae8SChuck Lever 1511c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 15121f0fa154SIlpo Järvinen if (sock_writeable(sk)) 15131f0fa154SIlpo Järvinen xs_write_space(sk); 1514c7b2cae8SChuck Lever 1515f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1516c7b2cae8SChuck Lever } 1517c7b2cae8SChuck Lever 1518c7b2cae8SChuck Lever /** 1519c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1520c7b2cae8SChuck Lever * becomes available 1521c7b2cae8SChuck Lever * @sk: socket whose state has changed 1522c7b2cae8SChuck Lever * 1523c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1524c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1525c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1526c7b2cae8SChuck Lever * with a bunch of small requests. 1527c7b2cae8SChuck Lever */ 1528c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1529c7b2cae8SChuck Lever { 1530f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1531c7b2cae8SChuck Lever 1532c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 153364dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 15341f0fa154SIlpo Järvinen xs_write_space(sk); 1535c7b2cae8SChuck Lever 1536f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1537a246b010SChuck Lever } 1538a246b010SChuck Lever 1539470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1540a246b010SChuck Lever { 1541ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1542ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1543a246b010SChuck Lever 15447c6e066eSChuck Lever if (transport->rcvsize) { 1545a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 15467c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1547a246b010SChuck Lever } 15487c6e066eSChuck Lever if (transport->sndsize) { 1549a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 15507c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1551a246b010SChuck Lever sk->sk_write_space(sk); 1552a246b010SChuck Lever } 1553a246b010SChuck Lever } 1554a246b010SChuck Lever 155543118c29SChuck Lever /** 1556470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 155743118c29SChuck Lever * @xprt: generic transport 1558470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1559470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 156043118c29SChuck Lever * 1561470056c2SChuck Lever * Set socket send and receive buffer size limits. 156243118c29SChuck Lever */ 1563470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 156443118c29SChuck Lever { 15657c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 15667c6e066eSChuck Lever 15677c6e066eSChuck Lever transport->sndsize = 0; 1568470056c2SChuck Lever if (sndsize) 15697c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 15707c6e066eSChuck Lever transport->rcvsize = 0; 1571470056c2SChuck Lever if (rcvsize) 15727c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1573470056c2SChuck Lever 1574470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 157543118c29SChuck Lever } 157643118c29SChuck Lever 157746c0ee8bSChuck Lever /** 157846c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1579acf0a39fSChuck Lever * @xprt: controlling transport 158046c0ee8bSChuck Lever * @task: task that timed out 158146c0ee8bSChuck Lever * 158246c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 158346c0ee8bSChuck Lever */ 15846a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 158546c0ee8bSChuck Lever { 1586b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 15876a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1588b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 158946c0ee8bSChuck Lever } 159046c0ee8bSChuck Lever 1591826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1592b85d8806SChuck Lever { 1593826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1594826799e6SJ. Bruce Fields unsigned short range; 1595826799e6SJ. Bruce Fields unsigned short rand; 1596826799e6SJ. Bruce Fields 1597826799e6SJ. Bruce Fields if (max < min) 1598826799e6SJ. Bruce Fields return -EADDRINUSE; 1599826799e6SJ. Bruce Fields range = max - min + 1; 1600826799e6SJ. Bruce Fields rand = (unsigned short) prandom_u32() % range; 1601826799e6SJ. Bruce Fields return rand + min; 1602b85d8806SChuck Lever } 1603b85d8806SChuck Lever 16044dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 16054dda9c8aSTrond Myklebust { 16064dda9c8aSTrond Myklebust struct sockaddr_storage buf; 16074dda9c8aSTrond Myklebust unsigned short port = 0; 16084dda9c8aSTrond Myklebust 16099b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 16104dda9c8aSTrond Myklebust goto out; 16114dda9c8aSTrond Myklebust switch (buf.ss_family) { 16124dda9c8aSTrond Myklebust case AF_INET6: 16134dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 16144dda9c8aSTrond Myklebust break; 16154dda9c8aSTrond Myklebust case AF_INET: 16164dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 16174dda9c8aSTrond Myklebust } 16184dda9c8aSTrond Myklebust out: 16194dda9c8aSTrond Myklebust return port; 16204dda9c8aSTrond Myklebust } 16214dda9c8aSTrond Myklebust 16224dda9c8aSTrond Myklebust /** 162392200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 162492200412SChuck Lever * @xprt: generic transport 162592200412SChuck Lever * @port: new port number 162692200412SChuck Lever * 162792200412SChuck Lever */ 162892200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 162992200412SChuck Lever { 163092200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1631c4efcb1dSChuck Lever 16329dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 16339dc3b095SChuck Lever xs_update_peer_port(xprt); 163492200412SChuck Lever } 163592200412SChuck Lever 16364dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 16374dda9c8aSTrond Myklebust { 1638e6237b6fSTrond Myklebust if (transport->srcport == 0 && transport->xprt.reuseport) 16394dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 16404dda9c8aSTrond Myklebust } 16414dda9c8aSTrond Myklebust 1642826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 164367a391d7STrond Myklebust { 1644826799e6SJ. Bruce Fields int port = transport->srcport; 164567a391d7STrond Myklebust 164667a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 164767a391d7STrond Myklebust port = xs_get_random_port(); 164867a391d7STrond Myklebust return port; 164967a391d7STrond Myklebust } 165067a391d7STrond Myklebust 1651baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 165267a391d7STrond Myklebust { 1653fbfffbd5SChuck Lever if (transport->srcport != 0) 1654fbfffbd5SChuck Lever transport->srcport = 0; 165567a391d7STrond Myklebust if (!transport->xprt.resvport) 165667a391d7STrond Myklebust return 0; 165767a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 165867a391d7STrond Myklebust return xprt_max_resvport; 165967a391d7STrond Myklebust return --port; 166067a391d7STrond Myklebust } 1661beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1662a246b010SChuck Lever { 1663beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 166467a391d7STrond Myklebust int err, nloop = 0; 1665826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 166667a391d7STrond Myklebust unsigned short last; 1667a246b010SChuck Lever 16680f7a622cSChris Perl /* 16690f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 16700f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 16710f7a622cSChris Perl * port selection happen implicitly when the socket is used 16720f7a622cSChris Perl * (for example at connect time). 16730f7a622cSChris Perl * 16740f7a622cSChris Perl * This ensures that we can continue to establish TCP 16750f7a622cSChris Perl * connections even when all local ephemeral ports are already 16760f7a622cSChris Perl * a part of some TCP connection. This makes no difference 167712b20ce3SBhaskar Chowdhury * for UDP sockets, but also doesn't harm them. 16780f7a622cSChris Perl * 16790f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 16800f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 16810f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 16820f7a622cSChris Perl */ 1683826799e6SJ. Bruce Fields if (port <= 0) 1684826799e6SJ. Bruce Fields return port; 16850f7a622cSChris Perl 1686beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1687a246b010SChuck Lever do { 1688beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1689e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1690beb59b68SPavel Emelyanov transport->xprt.addrlen); 1691a246b010SChuck Lever if (err == 0) { 1692fbfffbd5SChuck Lever transport->srcport = port; 1693d3bc9a1dSFrank van Maarseveen break; 1694a246b010SChuck Lever } 169567a391d7STrond Myklebust last = port; 1696baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 169767a391d7STrond Myklebust if (port > last) 169867a391d7STrond Myklebust nloop++; 169967a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1700beb59b68SPavel Emelyanov 17014232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1702beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1703beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1704beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1705beb59b68SPavel Emelyanov else 1706beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1707beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 17087dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1709a246b010SChuck Lever return err; 1710a246b010SChuck Lever } 1711a246b010SChuck Lever 1712176e21eeSChuck Lever /* 1713176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1714176e21eeSChuck Lever */ 1715176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1716176e21eeSChuck Lever { 1717fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1718176e21eeSChuck Lever } 1719176e21eeSChuck Lever 1720176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1721176e21eeSChuck Lever { 1722176e21eeSChuck Lever } 1723a246b010SChuck Lever 1724ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1725ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1726ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1727ed07536eSPeter Zijlstra 1728176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1729176e21eeSChuck Lever { 1730176e21eeSChuck Lever struct sock *sk = sock->sk; 1731176e21eeSChuck Lever 1732176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1733176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1734176e21eeSChuck Lever } 1735176e21eeSChuck Lever 17368945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1737ed07536eSPeter Zijlstra { 1738ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 17398945ee5eSChuck Lever 17408945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 17418945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1742ed07536eSPeter Zijlstra } 17438945ee5eSChuck Lever 17448945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 17458945ee5eSChuck Lever { 17468945ee5eSChuck Lever struct sock *sk = sock->sk; 17478945ee5eSChuck Lever 17488945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 17498945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1750ed07536eSPeter Zijlstra } 17516bc9638aSPavel Emelyanov 17526bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 17536bc9638aSPavel Emelyanov { 1754fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 17551b7a1819SWeston Andros Adamson return; 17561b7a1819SWeston Andros Adamson 17574232e863SChuck Lever switch (family) { 1758176e21eeSChuck Lever case AF_LOCAL: 1759176e21eeSChuck Lever xs_reclassify_socketu(sock); 1760176e21eeSChuck Lever break; 17614232e863SChuck Lever case AF_INET: 17626bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 17634232e863SChuck Lever break; 17644232e863SChuck Lever case AF_INET6: 17656bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 17664232e863SChuck Lever break; 17674232e863SChuck Lever } 17686bc9638aSPavel Emelyanov } 1769ed07536eSPeter Zijlstra #else 17706bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 17716bc9638aSPavel Emelyanov { 17726bc9638aSPavel Emelyanov } 1773ed07536eSPeter Zijlstra #endif 1774ed07536eSPeter Zijlstra 177593dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 177693dc41bdSNeilBrown { 177793dc41bdSNeilBrown } 177893dc41bdSNeilBrown 17796bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 17804dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 17814dda9c8aSTrond Myklebust int protocol, bool reuseport) 178222f79326SPavel Emelyanov { 1783a73881c9STrond Myklebust struct file *filp; 178422f79326SPavel Emelyanov struct socket *sock; 178522f79326SPavel Emelyanov int err; 178622f79326SPavel Emelyanov 17876bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 178822f79326SPavel Emelyanov if (err < 0) { 178922f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 179022f79326SPavel Emelyanov protocol, -err); 179122f79326SPavel Emelyanov goto out; 179222f79326SPavel Emelyanov } 17936bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 179422f79326SPavel Emelyanov 17954dda9c8aSTrond Myklebust if (reuseport) 1796fe31a326SChristoph Hellwig sock_set_reuseport(sock->sk); 17974dda9c8aSTrond Myklebust 17984cea288aSBen Hutchings err = xs_bind(transport, sock); 17994cea288aSBen Hutchings if (err) { 180022f79326SPavel Emelyanov sock_release(sock); 180122f79326SPavel Emelyanov goto out; 180222f79326SPavel Emelyanov } 180322f79326SPavel Emelyanov 1804a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1805a73881c9STrond Myklebust if (IS_ERR(filp)) 1806a73881c9STrond Myklebust return ERR_CAST(filp); 1807a73881c9STrond Myklebust transport->file = filp; 1808a73881c9STrond Myklebust 180922f79326SPavel Emelyanov return sock; 181022f79326SPavel Emelyanov out: 181122f79326SPavel Emelyanov return ERR_PTR(err); 181222f79326SPavel Emelyanov } 181322f79326SPavel Emelyanov 1814176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1815176e21eeSChuck Lever struct socket *sock) 1816176e21eeSChuck Lever { 1817176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1818176e21eeSChuck Lever xprt); 1819176e21eeSChuck Lever 1820176e21eeSChuck Lever if (!transport->inet) { 1821176e21eeSChuck Lever struct sock *sk = sock->sk; 1822176e21eeSChuck Lever 1823176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1824176e21eeSChuck Lever 1825176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1826176e21eeSChuck Lever 1827176e21eeSChuck Lever sk->sk_user_data = xprt; 1828a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1829176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1830b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 18312118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1832176e21eeSChuck Lever 1833176e21eeSChuck Lever xprt_clear_connected(xprt); 1834176e21eeSChuck Lever 1835176e21eeSChuck Lever /* Reset to new socket */ 1836176e21eeSChuck Lever transport->sock = sock; 1837176e21eeSChuck Lever transport->inet = sk; 1838176e21eeSChuck Lever 1839176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1840176e21eeSChuck Lever } 1841176e21eeSChuck Lever 1842ae053551STrond Myklebust xs_stream_start_connect(transport); 18436c7a64e5STrond Myklebust 1844176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1845176e21eeSChuck Lever } 1846176e21eeSChuck Lever 1847176e21eeSChuck Lever /** 1848176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1849176e21eeSChuck Lever * @transport: socket transport to connect 1850176e21eeSChuck Lever */ 1851dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1852176e21eeSChuck Lever { 1853176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1854a73881c9STrond Myklebust struct file *filp; 1855176e21eeSChuck Lever struct socket *sock; 185668e9a246SColin Ian King int status; 1857176e21eeSChuck Lever 1858176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1859176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1860176e21eeSChuck Lever if (status < 0) { 1861176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1862176e21eeSChuck Lever "transport socket (%d).\n", -status); 1863176e21eeSChuck Lever goto out; 1864176e21eeSChuck Lever } 1865d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1866176e21eeSChuck Lever 1867a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1868a73881c9STrond Myklebust if (IS_ERR(filp)) { 1869a73881c9STrond Myklebust status = PTR_ERR(filp); 1870a73881c9STrond Myklebust goto out; 1871a73881c9STrond Myklebust } 1872a73881c9STrond Myklebust transport->file = filp; 1873a73881c9STrond Myklebust 1874176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1875176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1876176e21eeSChuck Lever 1877176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 187840b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1879176e21eeSChuck Lever switch (status) { 1880176e21eeSChuck Lever case 0: 1881176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1882176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 18833968a8a5SChuck Lever xprt->stat.connect_count++; 18843968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 18853968a8a5SChuck Lever xprt->stat.connect_start; 1886176e21eeSChuck Lever xprt_set_connected(xprt); 188793f479d3SGustavo A. R. Silva break; 18883601c4a9STrond Myklebust case -ENOBUFS: 1889176e21eeSChuck Lever break; 1890176e21eeSChuck Lever case -ENOENT: 1891176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 1892176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1893176e21eeSChuck Lever break; 18944a20a988STrond Myklebust case -ECONNREFUSED: 18954a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 18964a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 18974a20a988STrond Myklebust break; 1898176e21eeSChuck Lever default: 1899176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1900176e21eeSChuck Lever __func__, -status, 1901176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 1902176e21eeSChuck Lever } 1903176e21eeSChuck Lever 1904176e21eeSChuck Lever out: 1905176e21eeSChuck Lever xprt_clear_connecting(xprt); 1906176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 1907dc107402SJ. Bruce Fields return status; 1908dc107402SJ. Bruce Fields } 1909dc107402SJ. Bruce Fields 1910b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 1911dc107402SJ. Bruce Fields { 1912dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1913dc107402SJ. Bruce Fields int ret; 1914dc107402SJ. Bruce Fields 1915dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 1916dc107402SJ. Bruce Fields /* 1917dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 1918dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 1919dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 1920dc107402SJ. Bruce Fields * 1921dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 1922dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 1923dc107402SJ. Bruce Fields * connect. 1924dc107402SJ. Bruce Fields */ 19255ad64b36STrond Myklebust task->tk_rpc_status = -ENOTCONN; 1926dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 1927dc107402SJ. Bruce Fields return; 1928dc107402SJ. Bruce Fields } 1929dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 1930dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 1931dc107402SJ. Bruce Fields msleep_interruptible(15000); 1932176e21eeSChuck Lever } 1933176e21eeSChuck Lever 19343c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 1935d6e971d8SJeff Layton /* 1936d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 1937d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 1938d6e971d8SJeff Layton * races with xs_reset_transport. 1939d6e971d8SJeff Layton */ 1940a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 1941a564b8f0SMel Gorman { 1942a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1943a564b8f0SMel Gorman xprt); 1944a564b8f0SMel Gorman 1945d6e971d8SJeff Layton /* 1946d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 1947d6e971d8SJeff Layton * reconnecting process will get it for us. 1948d6e971d8SJeff Layton */ 1949d6e971d8SJeff Layton if (!transport->inet) 1950d6e971d8SJeff Layton return; 19518e228133SJeff Layton if (atomic_read(&xprt->swapper)) 1952a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 1953a564b8f0SMel Gorman } 1954a564b8f0SMel Gorman 1955a564b8f0SMel Gorman /** 1956d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 1957a564b8f0SMel Gorman * @xprt: transport to tag 1958a564b8f0SMel Gorman * 19598e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 19608e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 1961a564b8f0SMel Gorman */ 1962d67fa4d8SJeff Layton static int 1963d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 1964a564b8f0SMel Gorman { 1965d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 1966a564b8f0SMel Gorman 1967d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 1968d6e971d8SJeff Layton return 0; 1969d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 1970d6e971d8SJeff Layton return -ERESTARTSYS; 1971d6e971d8SJeff Layton if (xs->inet) 1972d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 1973d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 19748e228133SJeff Layton return 0; 1975a564b8f0SMel Gorman } 1976a564b8f0SMel Gorman 19778e228133SJeff Layton /** 1978d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 19798e228133SJeff Layton * @xprt: transport to tag 19808e228133SJeff Layton * 19818e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 19828e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 19838e228133SJeff Layton */ 1984d67fa4d8SJeff Layton static void 1985d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 19868e228133SJeff Layton { 1987d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 19888e228133SJeff Layton 1989d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 1990d6e971d8SJeff Layton return; 1991d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 1992d6e971d8SJeff Layton return; 1993d6e971d8SJeff Layton if (xs->inet) 1994d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 1995d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 1996a564b8f0SMel Gorman } 1997a564b8f0SMel Gorman #else 1998a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 1999a564b8f0SMel Gorman { 2000a564b8f0SMel Gorman } 2001d67fa4d8SJeff Layton 2002d67fa4d8SJeff Layton static int 2003d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2004d67fa4d8SJeff Layton { 2005d67fa4d8SJeff Layton return -EINVAL; 2006d67fa4d8SJeff Layton } 2007d67fa4d8SJeff Layton 2008d67fa4d8SJeff Layton static void 2009d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2010d67fa4d8SJeff Layton { 2011d67fa4d8SJeff Layton } 2012a564b8f0SMel Gorman #endif 2013a564b8f0SMel Gorman 201416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2015a246b010SChuck Lever { 201616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2017edb267a6SChuck Lever 2018ee0ac0c2SChuck Lever if (!transport->inet) { 2019b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2020b0d93ad5SChuck Lever 2021b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2022b0d93ad5SChuck Lever 20232a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 20242a9e1cfaSTrond Myklebust 2025b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2026f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2027b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2028b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2029b0d93ad5SChuck Lever 2030b0d93ad5SChuck Lever xprt_set_connected(xprt); 2031b0d93ad5SChuck Lever 2032b0d93ad5SChuck Lever /* Reset to new socket */ 2033ee0ac0c2SChuck Lever transport->sock = sock; 2034ee0ac0c2SChuck Lever transport->inet = sk; 2035b0d93ad5SChuck Lever 2036a564b8f0SMel Gorman xs_set_memalloc(xprt); 2037a564b8f0SMel Gorman 2038b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2039b0d93ad5SChuck Lever } 2040470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 204102910177STrond Myklebust 204202910177STrond Myklebust xprt->stat.connect_start = jiffies; 204316be2d20SChuck Lever } 204416be2d20SChuck Lever 20458c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2046a246b010SChuck Lever { 2047a246b010SChuck Lever struct sock_xprt *transport = 2048a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2049a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2050d099b8afSColin Ian King struct socket *sock; 2051b65c0310SPavel Emelyanov int status = -EIO; 2052a246b010SChuck Lever 20538c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 20544dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 20554dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2056b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2057a246b010SChuck Lever goto out; 205868e220bdSChuck Lever 2059c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2060c740eff8SChuck Lever "%s (port %s)\n", xprt, 2061c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2062c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2063c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 206468e220bdSChuck Lever 206568e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 206640b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2067a246b010SChuck Lever status = 0; 2068b0d93ad5SChuck Lever out: 2069b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2070cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 20717d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2072b0d93ad5SChuck Lever } 2073b0d93ad5SChuck Lever 20744876cc77STrond Myklebust /** 20754876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 20764876cc77STrond Myklebust * @xprt: transport 20774876cc77STrond Myklebust * 20784876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 20794876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 20804876cc77STrond Myklebust */ 20814876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 20824876cc77STrond Myklebust { 20834876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 20844876cc77STrond Myklebust struct socket *sock = transport->sock; 20859b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 20864876cc77STrond Myklebust 20874876cc77STrond Myklebust if (sock == NULL) 20884876cc77STrond Myklebust return; 20899b30889cSTrond Myklebust switch (skst) { 20909b30889cSTrond Myklebust default: 20914876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 20924876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 20939b30889cSTrond Myklebust break; 20949b30889cSTrond Myklebust case TCP_CLOSE: 20959b30889cSTrond Myklebust case TCP_TIME_WAIT: 20964876cc77STrond Myklebust xs_reset_transport(transport); 20974876cc77STrond Myklebust } 20989b30889cSTrond Myklebust } 20994876cc77STrond Myklebust 21008d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 21018d1b8c62STrond Myklebust struct socket *sock) 2102b0d93ad5SChuck Lever { 210316be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21047196dbb0STrond Myklebust unsigned int keepidle; 21057196dbb0STrond Myklebust unsigned int keepcnt; 2106775f06abSTrond Myklebust unsigned int timeo; 21077f260e85STrond Myklebust 2108b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 21097196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 21107196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 21117196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 21127196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 21137196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2114b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 21157f260e85STrond Myklebust 21167f260e85STrond Myklebust /* TCP Keepalive options */ 2117ce3d9544SChristoph Hellwig sock_set_keepalive(sock->sk); 211871c48eb8SChristoph Hellwig tcp_sock_set_keepidle(sock->sk, keepidle); 2119d41ecaacSChristoph Hellwig tcp_sock_set_keepintvl(sock->sk, keepidle); 2120480aeb96SChristoph Hellwig tcp_sock_set_keepcnt(sock->sk, keepcnt); 2121b0d93ad5SChuck Lever 21228d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 2123c488aeadSChristoph Hellwig tcp_sock_set_user_timeout(sock->sk, timeo); 21248d1b8c62STrond Myklebust } 21258d1b8c62STrond Myklebust 21267196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 21277196dbb0STrond Myklebust unsigned long connect_timeout, 21287196dbb0STrond Myklebust unsigned long reconnect_timeout) 21297196dbb0STrond Myklebust { 21307196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21317196dbb0STrond Myklebust struct rpc_timeout to; 21327196dbb0STrond Myklebust unsigned long initval; 21337196dbb0STrond Myklebust 2134b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 21357196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 21367196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 21377196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 21387196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 21397196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 21407196dbb0STrond Myklebust /* Arbitrary lower limit */ 21417196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 21427196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 21437196dbb0STrond Myklebust to.to_initval = initval; 21447196dbb0STrond Myklebust to.to_maxval = initval; 21457196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 21467196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 21477196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 21487196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 21497196dbb0STrond Myklebust } 21507196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2151b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 21527196dbb0STrond Myklebust } 21537196dbb0STrond Myklebust 21548d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 21558d1b8c62STrond Myklebust { 21568d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21578d1b8c62STrond Myklebust int ret = -ENOTCONN; 21588d1b8c62STrond Myklebust 21598d1b8c62STrond Myklebust if (!transport->inet) { 21608d1b8c62STrond Myklebust struct sock *sk = sock->sk; 21618d1b8c62STrond Myklebust 2162d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2163d88e4d82SNeilBrown * connections such as NFS mounts. 2164d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2165d88e4d82SNeilBrown * Individual applications, which have specific 2166d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2167d88e4d82SNeilBrown * MAY override this as appropriate. 2168d88e4d82SNeilBrown */ 216918d5ad62SChristoph Hellwig if (xs_addr(xprt)->sa_family == PF_INET6) { 217018d5ad62SChristoph Hellwig ip6_sock_set_addr_preferences(sk, 217118d5ad62SChristoph Hellwig IPV6_PREFER_SRC_PUBLIC); 217218d5ad62SChristoph Hellwig } 2173d88e4d82SNeilBrown 21748d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2175d737e5d4STrond Myklebust tcp_sock_set_nodelay(sk); 2176775f06abSTrond Myklebust 2177b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2178b0d93ad5SChuck Lever 21792a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21802a9e1cfaSTrond Myklebust 2181b0d93ad5SChuck Lever sk->sk_user_data = xprt; 21825157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2183b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2184b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2185b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 21862118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 21873167e12cSChuck Lever 21883167e12cSChuck Lever /* socket options */ 21893167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 2190b0d93ad5SChuck Lever 2191b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2192b0d93ad5SChuck Lever 2193b0d93ad5SChuck Lever /* Reset to new socket */ 2194ee0ac0c2SChuck Lever transport->sock = sock; 2195ee0ac0c2SChuck Lever transport->inet = sk; 2196b0d93ad5SChuck Lever 2197b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2198b0d93ad5SChuck Lever } 2199b0d93ad5SChuck Lever 220001d37c42STrond Myklebust if (!xprt_bound(xprt)) 2201fe19a96bSTrond Myklebust goto out; 220201d37c42STrond Myklebust 2203a564b8f0SMel Gorman xs_set_memalloc(xprt); 2204a564b8f0SMel Gorman 2205ae053551STrond Myklebust xs_stream_start_connect(transport); 2206e1806c7bSTrond Myklebust 2207b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 22080fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2209fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2210fe19a96bSTrond Myklebust switch (ret) { 2211fe19a96bSTrond Myklebust case 0: 22124dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2213df561f66SGustavo A. R. Silva fallthrough; 2214fe19a96bSTrond Myklebust case -EINPROGRESS: 2215fe19a96bSTrond Myklebust /* SYN_SENT! */ 2216fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2217fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 22181f4c17a0STrond Myklebust break; 22191f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 22201f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 22211f4c17a0STrond Myklebust transport->srcport = 0; 2222fe19a96bSTrond Myklebust } 2223fe19a96bSTrond Myklebust out: 2224fe19a96bSTrond Myklebust return ret; 222516be2d20SChuck Lever } 222616be2d20SChuck Lever 222716be2d20SChuck Lever /** 2228b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2229acf0a39fSChuck Lever * @work: queued work item 223016be2d20SChuck Lever * 223116be2d20SChuck Lever * Invoked by a work queue tasklet. 223216be2d20SChuck Lever */ 2233cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 223416be2d20SChuck Lever { 2235cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2236cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 223716be2d20SChuck Lever struct socket *sock = transport->sock; 2238a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2239b61d59ffSTrond Myklebust int status = -EIO; 224016be2d20SChuck Lever 224116be2d20SChuck Lever if (!sock) { 2242cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 22434dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 22444dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2245b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2246b61d59ffSTrond Myklebust status = PTR_ERR(sock); 224716be2d20SChuck Lever goto out; 224816be2d20SChuck Lever } 22497d1e8255STrond Myklebust } 22507d1e8255STrond Myklebust 2251c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2252c740eff8SChuck Lever "%s (port %s)\n", xprt, 2253c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2254c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2255c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 225616be2d20SChuck Lever 225716be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 225840b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2259a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 226046121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 226146121cf7SChuck Lever sock->sk->sk_state); 2262a246b010SChuck Lever switch (status) { 2263f75e6745STrond Myklebust default: 2264f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2265f75e6745STrond Myklebust __func__, status); 2266df561f66SGustavo A. R. Silva fallthrough; 2267f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2268f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2269f75e6745STrond Myklebust * and retry 2270f75e6745STrond Myklebust */ 2271a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 227288b5ed73STrond Myklebust break; 22732a491991STrond Myklebust case 0: 2274a246b010SChuck Lever case -EINPROGRESS: 2275a246b010SChuck Lever case -EALREADY: 2276718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 22777d1e8255STrond Myklebust return; 22789fcfe0c8STrond Myklebust case -EINVAL: 22799fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 22809fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 22819fcfe0c8STrond Myklebust */ 22823ed5e2a2STrond Myklebust case -ECONNREFUSED: 22833ed5e2a2STrond Myklebust case -ECONNRESET: 2284eb5b46faSTrond Myklebust case -ENETDOWN: 22853ed5e2a2STrond Myklebust case -ENETUNREACH: 22864ba161a7STrond Myklebust case -EHOSTUNREACH: 22873913c78cSTrond Myklebust case -EADDRINUSE: 22883601c4a9STrond Myklebust case -ENOBUFS: 22898c71139dSCalum Mackay /* xs_tcp_force_close() wakes tasks with a fixed error code. 22908c71139dSCalum Mackay * We need to wake them first to ensure the correct error code. 22916ea44adcSNeilBrown */ 22926ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 22934efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 22949fcfe0c8STrond Myklebust goto out; 22958a2cec29STrond Myklebust } 22962a491991STrond Myklebust status = -EAGAIN; 2297a246b010SChuck Lever out: 22982226feb6SChuck Lever xprt_clear_connecting(xprt); 2299cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 23007d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2301a246b010SChuck Lever } 2302a246b010SChuck Lever 230368e220bdSChuck Lever /** 23049903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 23051b092092STrond Myklebust * @xprt: pointer to transport structure 23069903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 23079903cd1cSChuck Lever * 23089903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 230903bf4b70SChuck Lever * 231003bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 231103bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 231203bf4b70SChuck Lever * socket on a privileged port. 231303bf4b70SChuck Lever * 231403bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 231503bf4b70SChuck Lever * retry floods (hard mounts). 23169903cd1cSChuck Lever */ 23171b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2318a246b010SChuck Lever { 2319ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 232002910177STrond Myklebust unsigned long delay = 0; 2321a246b010SChuck Lever 2322718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2323718ba5b8STrond Myklebust 232499b1a4c3STrond Myklebust if (transport->sock != NULL) { 232546121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 232646121cf7SChuck Lever "seconds\n", 232703bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 232899b1a4c3STrond Myklebust 232999b1a4c3STrond Myklebust /* Start by resetting any existing state */ 233099b1a4c3STrond Myklebust xs_reset_transport(transport); 233199b1a4c3STrond Myklebust 2332675dd90aSChuck Lever delay = xprt_reconnect_delay(xprt); 2333675dd90aSChuck Lever xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 233402910177STrond Myklebust 233502910177STrond Myklebust } else 23369903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 233702910177STrond Myklebust 233840a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 233902910177STrond Myklebust &transport->connect_worker, 234002910177STrond Myklebust delay); 2341a246b010SChuck Lever } 2342a246b010SChuck Lever 23434f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 23444f8943f8STrond Myklebust { 23454f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 23464f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 23474f8943f8STrond Myklebust } 23484f8943f8STrond Myklebust 23494f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 23504f8943f8STrond Myklebust { 23514f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 23524f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 23534f8943f8STrond Myklebust } 23544f8943f8STrond Myklebust 23554f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 23564f8943f8STrond Myklebust { 23574f8943f8STrond Myklebust int sockerr; 23584f8943f8STrond Myklebust 23594f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 23604f8943f8STrond Myklebust return; 23614f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 23624f8943f8STrond Myklebust if (transport->sock == NULL) 23634f8943f8STrond Myklebust goto out; 23644f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 23654f8943f8STrond Myklebust goto out; 2366af84537dSBenjamin Coddington sockerr = xchg(&transport->xprt_err, 0); 23674f8943f8STrond Myklebust if (sockerr < 0) 23684f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 23694f8943f8STrond Myklebust out: 23704f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 23714f8943f8STrond Myklebust } 23724f8943f8STrond Myklebust 23734f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 23744f8943f8STrond Myklebust { 23754f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 23764f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 23774f8943f8STrond Myklebust } 23784f8943f8STrond Myklebust 23794f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 23804f8943f8STrond Myklebust { 23814f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 23824f8943f8STrond Myklebust struct sock_xprt, error_worker); 23834f8943f8STrond Myklebust 23844f8943f8STrond Myklebust xs_wake_disconnect(transport); 23854f8943f8STrond Myklebust xs_wake_write(transport); 23864f8943f8STrond Myklebust xs_wake_error(transport); 23874f8943f8STrond Myklebust xs_wake_pending(transport); 23884f8943f8STrond Myklebust } 23894f8943f8STrond Myklebust 2390262ca07dSChuck Lever /** 239112b20ce3SBhaskar Chowdhury * xs_local_print_stats - display AF_LOCAL socket-specific stats 2392176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2393176e21eeSChuck Lever * @seq: output file 2394176e21eeSChuck Lever * 2395176e21eeSChuck Lever */ 2396176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2397176e21eeSChuck Lever { 2398176e21eeSChuck Lever long idle_time = 0; 2399176e21eeSChuck Lever 2400176e21eeSChuck Lever if (xprt_connected(xprt)) 2401176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2402176e21eeSChuck Lever 2403176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 240415a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2405176e21eeSChuck Lever xprt->stat.bind_count, 2406176e21eeSChuck Lever xprt->stat.connect_count, 24078440a886SChuck Lever xprt->stat.connect_time / HZ, 2408176e21eeSChuck Lever idle_time, 2409176e21eeSChuck Lever xprt->stat.sends, 2410176e21eeSChuck Lever xprt->stat.recvs, 2411176e21eeSChuck Lever xprt->stat.bad_xids, 2412176e21eeSChuck Lever xprt->stat.req_u, 241315a45206SAndy Adamson xprt->stat.bklog_u, 241415a45206SAndy Adamson xprt->stat.max_slots, 241515a45206SAndy Adamson xprt->stat.sending_u, 241615a45206SAndy Adamson xprt->stat.pending_u); 2417176e21eeSChuck Lever } 2418176e21eeSChuck Lever 2419176e21eeSChuck Lever /** 242012b20ce3SBhaskar Chowdhury * xs_udp_print_stats - display UDP socket-specific stats 2421262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2422262ca07dSChuck Lever * @seq: output file 2423262ca07dSChuck Lever * 2424262ca07dSChuck Lever */ 2425262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2426262ca07dSChuck Lever { 2427c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2428c8475461SChuck Lever 242915a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 243015a45206SAndy Adamson "%lu %llu %llu\n", 2431fbfffbd5SChuck Lever transport->srcport, 2432262ca07dSChuck Lever xprt->stat.bind_count, 2433262ca07dSChuck Lever xprt->stat.sends, 2434262ca07dSChuck Lever xprt->stat.recvs, 2435262ca07dSChuck Lever xprt->stat.bad_xids, 2436262ca07dSChuck Lever xprt->stat.req_u, 243715a45206SAndy Adamson xprt->stat.bklog_u, 243815a45206SAndy Adamson xprt->stat.max_slots, 243915a45206SAndy Adamson xprt->stat.sending_u, 244015a45206SAndy Adamson xprt->stat.pending_u); 2441262ca07dSChuck Lever } 2442262ca07dSChuck Lever 2443262ca07dSChuck Lever /** 244412b20ce3SBhaskar Chowdhury * xs_tcp_print_stats - display TCP socket-specific stats 2445262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2446262ca07dSChuck Lever * @seq: output file 2447262ca07dSChuck Lever * 2448262ca07dSChuck Lever */ 2449262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2450262ca07dSChuck Lever { 2451c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2452262ca07dSChuck Lever long idle_time = 0; 2453262ca07dSChuck Lever 2454262ca07dSChuck Lever if (xprt_connected(xprt)) 2455262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2456262ca07dSChuck Lever 245715a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 245815a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2459fbfffbd5SChuck Lever transport->srcport, 2460262ca07dSChuck Lever xprt->stat.bind_count, 2461262ca07dSChuck Lever xprt->stat.connect_count, 24628440a886SChuck Lever xprt->stat.connect_time / HZ, 2463262ca07dSChuck Lever idle_time, 2464262ca07dSChuck Lever xprt->stat.sends, 2465262ca07dSChuck Lever xprt->stat.recvs, 2466262ca07dSChuck Lever xprt->stat.bad_xids, 2467262ca07dSChuck Lever xprt->stat.req_u, 246815a45206SAndy Adamson xprt->stat.bklog_u, 246915a45206SAndy Adamson xprt->stat.max_slots, 247015a45206SAndy Adamson xprt->stat.sending_u, 247115a45206SAndy Adamson xprt->stat.pending_u); 2472262ca07dSChuck Lever } 2473262ca07dSChuck Lever 24744cfc7e60SRahul Iyer /* 24754cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 24764cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 24774cfc7e60SRahul Iyer * to use the server side send routines. 24784cfc7e60SRahul Iyer */ 24795fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 24804cfc7e60SRahul Iyer { 24815fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 24825fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 24834cfc7e60SRahul Iyer struct page *page; 24844cfc7e60SRahul Iyer struct rpc_buffer *buf; 24854cfc7e60SRahul Iyer 24865fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 24875fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 24885fe6eaa1SChuck Lever size); 24895fe6eaa1SChuck Lever return -EINVAL; 24905fe6eaa1SChuck Lever } 24914cfc7e60SRahul Iyer 2492b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 24934cfc7e60SRahul Iyer if (!page) 24945fe6eaa1SChuck Lever return -ENOMEM; 24954cfc7e60SRahul Iyer 24964cfc7e60SRahul Iyer buf = page_address(page); 24974cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 24984cfc7e60SRahul Iyer 24995fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 250018e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 25015fe6eaa1SChuck Lever return 0; 25024cfc7e60SRahul Iyer } 25034cfc7e60SRahul Iyer 25044cfc7e60SRahul Iyer /* 25054cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 25064cfc7e60SRahul Iyer */ 25073435c74aSChuck Lever static void bc_free(struct rpc_task *task) 25084cfc7e60SRahul Iyer { 25093435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 25104cfc7e60SRahul Iyer struct rpc_buffer *buf; 25114cfc7e60SRahul Iyer 25124cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 25134cfc7e60SRahul Iyer free_page((unsigned long)buf); 25144cfc7e60SRahul Iyer } 25154cfc7e60SRahul Iyer 25164cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 25174cfc7e60SRahul Iyer { 2518da1661b9SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 25194cfc7e60SRahul Iyer struct sock_xprt *transport = 2520067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 2521067fb11bSChuck Lever struct msghdr msg = { 2522da1661b9SChuck Lever .msg_flags = 0, 2523067fb11bSChuck Lever }; 2524067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2525da1661b9SChuck Lever (u32)xdr->len); 2526da1661b9SChuck Lever unsigned int sent = 0; 2527da1661b9SChuck Lever int err; 25284cfc7e60SRahul Iyer 25298729aabaSChuck Lever req->rq_xtime = ktime_get(); 2530da1661b9SChuck Lever err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); 2531da1661b9SChuck Lever xdr_free_bvec(xdr); 2532da1661b9SChuck Lever if (err < 0 || sent != (xdr->len + sizeof(marker))) 2533067fb11bSChuck Lever return -EAGAIN; 2534da1661b9SChuck Lever return sent; 25354cfc7e60SRahul Iyer } 25364cfc7e60SRahul Iyer 2537ca4faf54SChuck Lever /** 2538ca4faf54SChuck Lever * bc_send_request - Send a backchannel Call on a TCP socket 2539ca4faf54SChuck Lever * @req: rpc_rqst containing Call message to be sent 2540ca4faf54SChuck Lever * 2541ca4faf54SChuck Lever * xpt_mutex ensures @rqstp's whole message is written to the socket 2542ca4faf54SChuck Lever * without interruption. 2543ca4faf54SChuck Lever * 2544ca4faf54SChuck Lever * Return values: 2545ca4faf54SChuck Lever * %0 if the message was sent successfully 2546ca4faf54SChuck Lever * %ENOTCONN if the message was not sent 25474cfc7e60SRahul Iyer */ 2548adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 25494cfc7e60SRahul Iyer { 25504cfc7e60SRahul Iyer struct svc_xprt *xprt; 25517fc56136SAndrzej Hajda int len; 25524cfc7e60SRahul Iyer 25534cfc7e60SRahul Iyer /* 25544cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 25554cfc7e60SRahul Iyer */ 25564cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 25574cfc7e60SRahul Iyer 25584cfc7e60SRahul Iyer /* 25594cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 25604cfc7e60SRahul Iyer * with the fore channel 25614cfc7e60SRahul Iyer */ 2562c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 25634cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 25644cfc7e60SRahul Iyer len = -ENOTCONN; 25654cfc7e60SRahul Iyer else 25664cfc7e60SRahul Iyer len = bc_sendto(req); 25674cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 25684cfc7e60SRahul Iyer 25694cfc7e60SRahul Iyer if (len > 0) 25704cfc7e60SRahul Iyer len = 0; 25714cfc7e60SRahul Iyer 25724cfc7e60SRahul Iyer return len; 25734cfc7e60SRahul Iyer } 25744cfc7e60SRahul Iyer 25754cfc7e60SRahul Iyer /* 25764cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 25774cfc7e60SRahul Iyer */ 25784cfc7e60SRahul Iyer 25794cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 25804cfc7e60SRahul Iyer { 25816221f1d9SChuck Lever xprt_disconnect_done(xprt); 25824cfc7e60SRahul Iyer } 25834cfc7e60SRahul Iyer 25844cfc7e60SRahul Iyer /* 25854cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 25864cfc7e60SRahul Iyer * initiated, we do nothing 25874cfc7e60SRahul Iyer */ 25884cfc7e60SRahul Iyer 25894cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 25904cfc7e60SRahul Iyer { 259147f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 259247f72efaSKinglong Mee 259347f72efaSKinglong Mee xs_xprt_free(xprt); 259447f72efaSKinglong Mee module_put(THIS_MODULE); 25954cfc7e60SRahul Iyer } 25964cfc7e60SRahul Iyer 2597d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2598176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 25994cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2600f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2601a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2602176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2603176e21eeSChuck Lever .set_port = xs_local_set_port, 2604dc107402SJ. Bruce Fields .connect = xs_local_connect, 2605176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2606176e21eeSChuck Lever .buf_free = rpc_free, 2607550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 2608176e21eeSChuck Lever .send_request = xs_local_send_request, 26098ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2610176e21eeSChuck Lever .close = xs_close, 2611a1311d87STrond Myklebust .destroy = xs_destroy, 2612176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2613d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2614d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2615176e21eeSChuck Lever }; 2616176e21eeSChuck Lever 2617d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 261843118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 261912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 262049e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2621f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2622a9cde23aSChuck Lever .free_slot = xprt_free_slot, 262345160d62SChuck Lever .rpcbind = rpcb_getport_async, 262492200412SChuck Lever .set_port = xs_set_port, 26259903cd1cSChuck Lever .connect = xs_connect, 262602107148SChuck Lever .buf_alloc = rpc_malloc, 262702107148SChuck Lever .buf_free = rpc_free, 2628262965f5SChuck Lever .send_request = xs_udp_send_request, 26298ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 263046c0ee8bSChuck Lever .timer = xs_udp_timer, 2631a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2632262965f5SChuck Lever .close = xs_close, 2633262965f5SChuck Lever .destroy = xs_destroy, 2634262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2635d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2636d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26374a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2638262965f5SChuck Lever }; 2639262965f5SChuck Lever 2640d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 264112a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 26424cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 264336bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 2644a9cde23aSChuck Lever .free_slot = xprt_free_slot, 264545160d62SChuck Lever .rpcbind = rpcb_getport_async, 264692200412SChuck Lever .set_port = xs_set_port, 26470b9e7943STrond Myklebust .connect = xs_connect, 264802107148SChuck Lever .buf_alloc = rpc_malloc, 264902107148SChuck Lever .buf_free = rpc_free, 2650277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 2651262965f5SChuck Lever .send_request = xs_tcp_send_request, 26528ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2653c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 26549903cd1cSChuck Lever .destroy = xs_destroy, 26557196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2656262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2657d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2658d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26594a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 266042e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 266142e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 26626b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 26637402a4feSTrond Myklebust .bc_num_slots = xprt_bc_max_slots, 266442e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 266542e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 266642e5c3e2SChuck Lever #endif 2667a246b010SChuck Lever }; 2668a246b010SChuck Lever 26694cfc7e60SRahul Iyer /* 26704cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 26714cfc7e60SRahul Iyer */ 26724cfc7e60SRahul Iyer 2673d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 26744cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 26754cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 267684e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2677a9cde23aSChuck Lever .free_slot = xprt_free_slot, 26784cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 26794cfc7e60SRahul Iyer .buf_free = bc_free, 26804cfc7e60SRahul Iyer .send_request = bc_send_request, 26818ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 26824cfc7e60SRahul Iyer .close = bc_close, 26834cfc7e60SRahul Iyer .destroy = bc_destroy, 26844cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2685d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2686d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 26874a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 26884cfc7e60SRahul Iyer }; 26894cfc7e60SRahul Iyer 269092476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 269192476850SChuck Lever { 269292476850SChuck Lever static const struct sockaddr_in sin = { 269392476850SChuck Lever .sin_family = AF_INET, 269492476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 269592476850SChuck Lever }; 269692476850SChuck Lever static const struct sockaddr_in6 sin6 = { 269792476850SChuck Lever .sin6_family = AF_INET6, 269892476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 269992476850SChuck Lever }; 270092476850SChuck Lever 270192476850SChuck Lever switch (family) { 2702176e21eeSChuck Lever case AF_LOCAL: 2703176e21eeSChuck Lever break; 270492476850SChuck Lever case AF_INET: 270592476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 270692476850SChuck Lever break; 270792476850SChuck Lever case AF_INET6: 270892476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 270992476850SChuck Lever break; 271092476850SChuck Lever default: 271192476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 271292476850SChuck Lever return -EAFNOSUPPORT; 271392476850SChuck Lever } 271492476850SChuck Lever return 0; 271592476850SChuck Lever } 271692476850SChuck Lever 27173c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2718d9ba131dSTrond Myklebust unsigned int slot_table_size, 2719d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2720c8541ecdSChuck Lever { 2721c8541ecdSChuck Lever struct rpc_xprt *xprt; 2722ffc2e518SChuck Lever struct sock_xprt *new; 2723c8541ecdSChuck Lever 272496802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2725c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2726c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2727c8541ecdSChuck Lever } 2728c8541ecdSChuck Lever 2729d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2730d9ba131dSTrond Myklebust max_slot_table_size); 2731bd1722d4SPavel Emelyanov if (xprt == NULL) { 273246121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 273346121cf7SChuck Lever "rpc_xprt\n"); 2734c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2735c8541ecdSChuck Lever } 2736c8541ecdSChuck Lever 2737bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2738edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 273996802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 274096802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2741d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2742fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 274392476850SChuck Lever else { 274492476850SChuck Lever int err; 274592476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 274692476850SChuck Lever (struct sockaddr *)&new->srcaddr); 27472aa13531SStanislav Kinsbursky if (err != 0) { 27482aa13531SStanislav Kinsbursky xprt_free(xprt); 274992476850SChuck Lever return ERR_PTR(err); 275092476850SChuck Lever } 27512aa13531SStanislav Kinsbursky } 2752c8541ecdSChuck Lever 2753c8541ecdSChuck Lever return xprt; 2754c8541ecdSChuck Lever } 2755c8541ecdSChuck Lever 2756176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2757176e21eeSChuck Lever .to_initval = 10 * HZ, 2758176e21eeSChuck Lever .to_maxval = 10 * HZ, 2759176e21eeSChuck Lever .to_retries = 2, 2760176e21eeSChuck Lever }; 2761176e21eeSChuck Lever 2762176e21eeSChuck Lever /** 2763176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2764176e21eeSChuck Lever * @args: rpc transport creation arguments 2765176e21eeSChuck Lever * 2766176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2767176e21eeSChuck Lever */ 2768176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2769176e21eeSChuck Lever { 2770176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2771176e21eeSChuck Lever struct sock_xprt *transport; 2772176e21eeSChuck Lever struct rpc_xprt *xprt; 2773176e21eeSChuck Lever struct rpc_xprt *ret; 2774176e21eeSChuck Lever 2775d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2776d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2777176e21eeSChuck Lever if (IS_ERR(xprt)) 2778176e21eeSChuck Lever return xprt; 2779176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2780176e21eeSChuck Lever 2781176e21eeSChuck Lever xprt->prot = 0; 2782176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2783176e21eeSChuck Lever 2784176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2785176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2786176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2787176e21eeSChuck Lever 2788176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2789176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2790176e21eeSChuck Lever 2791550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 27924f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2793550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 279493dc41bdSNeilBrown 2795176e21eeSChuck Lever switch (sun->sun_family) { 2796176e21eeSChuck Lever case AF_LOCAL: 2797176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2798176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2799176e21eeSChuck Lever sun->sun_path); 2800176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2801176e21eeSChuck Lever goto out_err; 2802176e21eeSChuck Lever } 2803176e21eeSChuck Lever xprt_set_bound(xprt); 2804176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 28057073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 28067073ea87SJ. Bruce Fields if (ret) 28077073ea87SJ. Bruce Fields goto out_err; 2808176e21eeSChuck Lever break; 2809176e21eeSChuck Lever default: 2810176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2811176e21eeSChuck Lever goto out_err; 2812176e21eeSChuck Lever } 2813176e21eeSChuck Lever 2814176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2815176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2816176e21eeSChuck Lever 2817176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2818176e21eeSChuck Lever return xprt; 2819176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2820176e21eeSChuck Lever out_err: 2821315f3812SKinglong Mee xs_xprt_free(xprt); 2822176e21eeSChuck Lever return ret; 2823176e21eeSChuck Lever } 2824176e21eeSChuck Lever 28252881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 28262881ae74STrond Myklebust .to_initval = 5 * HZ, 28272881ae74STrond Myklebust .to_maxval = 30 * HZ, 28282881ae74STrond Myklebust .to_increment = 5 * HZ, 28292881ae74STrond Myklebust .to_retries = 5, 28302881ae74STrond Myklebust }; 28312881ae74STrond Myklebust 28329903cd1cSChuck Lever /** 28339903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 283496802a09SFrank van Maarseveen * @args: rpc transport creation arguments 28359903cd1cSChuck Lever * 28369903cd1cSChuck Lever */ 2837483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2838a246b010SChuck Lever { 28398f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2840c8541ecdSChuck Lever struct rpc_xprt *xprt; 2841c8475461SChuck Lever struct sock_xprt *transport; 28420a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2843a246b010SChuck Lever 2844d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2845d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2846c8541ecdSChuck Lever if (IS_ERR(xprt)) 2847c8541ecdSChuck Lever return xprt; 2848c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2849a246b010SChuck Lever 2850ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2851a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2852a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2853a246b010SChuck Lever 285403bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 285503bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 285603bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2857a246b010SChuck Lever 2858262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2859a246b010SChuck Lever 2860ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2861a246b010SChuck Lever 2862f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 28634f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2864edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2865edc1b01cSTrond Myklebust 28668f9d5b1aSChuck Lever switch (addr->sa_family) { 28678f9d5b1aSChuck Lever case AF_INET: 28688f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 28698f9d5b1aSChuck Lever xprt_set_bound(xprt); 28708f9d5b1aSChuck Lever 28719dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 28728f9d5b1aSChuck Lever break; 28738f9d5b1aSChuck Lever case AF_INET6: 28748f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 28758f9d5b1aSChuck Lever xprt_set_bound(xprt); 28768f9d5b1aSChuck Lever 28779dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 28788f9d5b1aSChuck Lever break; 28798f9d5b1aSChuck Lever default: 28800a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 28810a68b0beSJ. Bruce Fields goto out_err; 28828f9d5b1aSChuck Lever } 28838f9d5b1aSChuck Lever 2884c740eff8SChuck Lever if (xprt_bound(xprt)) 2885c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2886c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2887c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2888c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2889c740eff8SChuck Lever else 2890c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2891c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2892c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2893edb267a6SChuck Lever 2894bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2895c8541ecdSChuck Lever return xprt; 28960a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 28970a68b0beSJ. Bruce Fields out_err: 2898315f3812SKinglong Mee xs_xprt_free(xprt); 28990a68b0beSJ. Bruce Fields return ret; 2900a246b010SChuck Lever } 2901a246b010SChuck Lever 29022881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 29032881ae74STrond Myklebust .to_initval = 60 * HZ, 29042881ae74STrond Myklebust .to_maxval = 60 * HZ, 29052881ae74STrond Myklebust .to_retries = 2, 29062881ae74STrond Myklebust }; 29072881ae74STrond Myklebust 29089903cd1cSChuck Lever /** 29099903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 291096802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29119903cd1cSChuck Lever * 29129903cd1cSChuck Lever */ 2913483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2914a246b010SChuck Lever { 29158f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2916c8541ecdSChuck Lever struct rpc_xprt *xprt; 2917c8475461SChuck Lever struct sock_xprt *transport; 29180a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2919b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2920b7993cebSTrond Myklebust 2921b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2922b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2923a246b010SChuck Lever 2924d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2925b7993cebSTrond Myklebust max_slot_table_size); 2926c8541ecdSChuck Lever if (IS_ERR(xprt)) 2927c8541ecdSChuck Lever return xprt; 2928c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2929a246b010SChuck Lever 2930ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2931808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2932a246b010SChuck Lever 293303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 293403bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 293503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2936a246b010SChuck Lever 2937262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2938ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2939a246b010SChuck Lever 29403851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 29417196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 29427196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 29433851f1cdSTrond Myklebust 2944c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 29454f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 2946edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 2947edc1b01cSTrond Myklebust 29488f9d5b1aSChuck Lever switch (addr->sa_family) { 29498f9d5b1aSChuck Lever case AF_INET: 29508f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 29518f9d5b1aSChuck Lever xprt_set_bound(xprt); 29528f9d5b1aSChuck Lever 29539dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 29548f9d5b1aSChuck Lever break; 29558f9d5b1aSChuck Lever case AF_INET6: 29568f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 29578f9d5b1aSChuck Lever xprt_set_bound(xprt); 29588f9d5b1aSChuck Lever 29599dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 29608f9d5b1aSChuck Lever break; 29618f9d5b1aSChuck Lever default: 29620a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 29630a68b0beSJ. Bruce Fields goto out_err; 29648f9d5b1aSChuck Lever } 29658f9d5b1aSChuck Lever 2966c740eff8SChuck Lever if (xprt_bound(xprt)) 2967c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2968c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2969c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2970c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2971c740eff8SChuck Lever else 2972c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2973c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2974c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2975c740eff8SChuck Lever 2976bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2977c8541ecdSChuck Lever return xprt; 29780a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 29790a68b0beSJ. Bruce Fields out_err: 2980315f3812SKinglong Mee xs_xprt_free(xprt); 29810a68b0beSJ. Bruce Fields return ret; 2982a246b010SChuck Lever } 2983282b32e1SChuck Lever 2984f300babaSAlexandros Batsakis /** 2985f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 2986f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 2987f300babaSAlexandros Batsakis * 2988f300babaSAlexandros Batsakis */ 2989f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 2990f300babaSAlexandros Batsakis { 2991f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 2992f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 2993f300babaSAlexandros Batsakis struct sock_xprt *transport; 2994f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 29950a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2996f300babaSAlexandros Batsakis 2997d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2998d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 2999f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3000f300babaSAlexandros Batsakis return xprt; 3001f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3002f300babaSAlexandros Batsakis 3003f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3004f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3005f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3006f300babaSAlexandros Batsakis 3007f300babaSAlexandros Batsakis /* backchannel */ 3008f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3009f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3010f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3011f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3012f300babaSAlexandros Batsakis 3013f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3014f300babaSAlexandros Batsakis 3015f300babaSAlexandros Batsakis switch (addr->sa_family) { 3016f300babaSAlexandros Batsakis case AF_INET: 3017f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3018f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3019f300babaSAlexandros Batsakis break; 3020f300babaSAlexandros Batsakis case AF_INET6: 3021f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3022f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3023f300babaSAlexandros Batsakis break; 3024f300babaSAlexandros Batsakis default: 30250a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30260a68b0beSJ. Bruce Fields goto out_err; 3027f300babaSAlexandros Batsakis } 3028f300babaSAlexandros Batsakis 3029f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3030f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3031f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3032f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3033f300babaSAlexandros Batsakis 3034f300babaSAlexandros Batsakis /* 303599de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 303628303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 303728303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 303828303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 303999de8ea9SJ. Bruce Fields */ 304099de8ea9SJ. Bruce Fields xprt_get(xprt); 304199de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 304299de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 304399de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 304499de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 304599de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 304699de8ea9SJ. Bruce Fields 304799de8ea9SJ. Bruce Fields /* 3048f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3049f300babaSAlexandros Batsakis * the xprt status to connected 3050f300babaSAlexandros Batsakis */ 3051f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3052f300babaSAlexandros Batsakis 3053f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3054f300babaSAlexandros Batsakis return xprt; 3055642aab58SKinglong Mee 3056642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 305739a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 305899de8ea9SJ. Bruce Fields xprt_put(xprt); 30590a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30600a68b0beSJ. Bruce Fields out_err: 3061315f3812SKinglong Mee xs_xprt_free(xprt); 30620a68b0beSJ. Bruce Fields return ret; 3063f300babaSAlexandros Batsakis } 3064f300babaSAlexandros Batsakis 3065176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3066176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3067176e21eeSChuck Lever .name = "named UNIX socket", 3068176e21eeSChuck Lever .owner = THIS_MODULE, 3069176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3070176e21eeSChuck Lever .setup = xs_setup_local, 3071d5aa6b22STrond Myklebust .netid = { "" }, 3072176e21eeSChuck Lever }; 3073176e21eeSChuck Lever 3074bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3075bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3076bc25571eS\"Talpey, Thomas\ .name = "udp", 3077bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3078f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3079bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3080d5aa6b22STrond Myklebust .netid = { "udp", "udp6", "" }, 3081bc25571eS\"Talpey, Thomas\ }; 3082bc25571eS\"Talpey, Thomas\ 3083bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3084bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3085bc25571eS\"Talpey, Thomas\ .name = "tcp", 3086bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3087f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3088bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3089d5aa6b22STrond Myklebust .netid = { "tcp", "tcp6", "" }, 3090bc25571eS\"Talpey, Thomas\ }; 3091bc25571eS\"Talpey, Thomas\ 3092f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3093f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3094f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3095f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3096f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3097f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3098d5aa6b22STrond Myklebust .netid = { "" }, 3099f300babaSAlexandros Batsakis }; 3100f300babaSAlexandros Batsakis 3101282b32e1SChuck Lever /** 3102bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3103282b32e1SChuck Lever * 3104282b32e1SChuck Lever */ 3105282b32e1SChuck Lever int init_socket_xprt(void) 3106282b32e1SChuck Lever { 31072b1bec5fSEric W. Biederman if (!sunrpc_table_header) 31080b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3109fbf76683SChuck Lever 3110176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3111bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3112bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3113f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3114bc25571eS\"Talpey, Thomas\ 3115282b32e1SChuck Lever return 0; 3116282b32e1SChuck Lever } 3117282b32e1SChuck Lever 3118282b32e1SChuck Lever /** 3119bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3120282b32e1SChuck Lever * 3121282b32e1SChuck Lever */ 3122282b32e1SChuck Lever void cleanup_socket_xprt(void) 3123282b32e1SChuck Lever { 3124fbf76683SChuck Lever if (sunrpc_table_header) { 3125fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3126fbf76683SChuck Lever sunrpc_table_header = NULL; 3127fbf76683SChuck Lever } 3128bc25571eS\"Talpey, Thomas\ 3129176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3130bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3131bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3132f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3133282b32e1SChuck Lever } 3134cbf11071STrond Myklebust 31359bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 31369bbb9e5aSRusty Russell const struct kernel_param *kp, 3137cbf11071STrond Myklebust unsigned int min, unsigned int max) 3138cbf11071STrond Myklebust { 313900cfaa94SDaniel Walter unsigned int num; 3140cbf11071STrond Myklebust int ret; 3141cbf11071STrond Myklebust 3142cbf11071STrond Myklebust if (!val) 3143cbf11071STrond Myklebust return -EINVAL; 314400cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 31459761a246SDan Carpenter if (ret) 31469761a246SDan Carpenter return ret; 31479761a246SDan Carpenter if (num < min || num > max) 3148cbf11071STrond Myklebust return -EINVAL; 3149cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3150cbf11071STrond Myklebust return 0; 3151cbf11071STrond Myklebust } 3152cbf11071STrond Myklebust 31539bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3154cbf11071STrond Myklebust { 3155cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3156cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3157cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3158cbf11071STrond Myklebust } 3159cbf11071STrond Myklebust 31609c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 31619bbb9e5aSRusty Russell .set = param_set_portnr, 31629bbb9e5aSRusty Russell .get = param_get_uint, 31639bbb9e5aSRusty Russell }; 31649bbb9e5aSRusty Russell 3165cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3166cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3167cbf11071STrond Myklebust 3168cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3169cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3170cbf11071STrond Myklebust 31719bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 31729bbb9e5aSRusty Russell const struct kernel_param *kp) 3173cbf11071STrond Myklebust { 3174cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3175cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3176cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3177cbf11071STrond Myklebust } 3178cbf11071STrond Myklebust 31799c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 31809bbb9e5aSRusty Russell .set = param_set_slot_table_size, 31819bbb9e5aSRusty Russell .get = param_get_uint, 31829bbb9e5aSRusty Russell }; 31839bbb9e5aSRusty Russell 3184cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3185cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3186cbf11071STrond Myklebust 3187d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3188d9ba131dSTrond Myklebust const struct kernel_param *kp) 3189d9ba131dSTrond Myklebust { 3190d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3191d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3192d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3193d9ba131dSTrond Myklebust } 3194d9ba131dSTrond Myklebust 31959c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3196d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3197d9ba131dSTrond Myklebust .get = param_get_uint, 3198d9ba131dSTrond Myklebust }; 3199d9ba131dSTrond Myklebust 3200d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3201d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3202d9ba131dSTrond Myklebust 3203cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3204cbf11071STrond Myklebust slot_table_size, 0644); 3205d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3206d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3207cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3208cbf11071STrond Myklebust slot_table_size, 0644); 3209