1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2a246b010SChuck Lever /* 3a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 4a246b010SChuck Lever * 5a246b010SChuck Lever * Client-side transport implementation for sockets. 6a246b010SChuck Lever * 7113aa838SAlan Cox * TCP callback races fixes (C) 1998 Red Hat 8113aa838SAlan Cox * TCP send fixes (C) 1998 Red Hat 9a246b010SChuck Lever * TCP NFS related read + write fixes 10a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 11a246b010SChuck Lever * 12a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 13a246b010SChuck Lever * Fix behaviour when socket buffer is full. 14a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1555aa4f58SChuck Lever * 1655aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 178f9d5b1aSChuck Lever * 188f9d5b1aSChuck Lever * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 198f9d5b1aSChuck Lever * <gilles.quillard@bull.net> 20a246b010SChuck Lever */ 21a246b010SChuck Lever 22a246b010SChuck Lever #include <linux/types.h> 23176e21eeSChuck Lever #include <linux/string.h> 24a246b010SChuck Lever #include <linux/slab.h> 25bc25571eS\"Talpey, Thomas\ #include <linux/module.h> 26a246b010SChuck Lever #include <linux/capability.h> 27a246b010SChuck Lever #include <linux/pagemap.h> 28a246b010SChuck Lever #include <linux/errno.h> 29a246b010SChuck Lever #include <linux/socket.h> 30a246b010SChuck Lever #include <linux/in.h> 31a246b010SChuck Lever #include <linux/net.h> 32a246b010SChuck Lever #include <linux/mm.h> 33176e21eeSChuck Lever #include <linux/un.h> 34a246b010SChuck Lever #include <linux/udp.h> 35a246b010SChuck Lever #include <linux/tcp.h> 36a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 375976687aSJeff Layton #include <linux/sunrpc/addr.h> 3802107148SChuck Lever #include <linux/sunrpc/sched.h> 394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h> 4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 41a246b010SChuck Lever #include <linux/file.h> 429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 4444b98efdSRicardo Labiaga #endif 45a246b010SChuck Lever 46a246b010SChuck Lever #include <net/sock.h> 47a246b010SChuck Lever #include <net/checksum.h> 48a246b010SChuck Lever #include <net/udp.h> 49a246b010SChuck Lever #include <net/tcp.h> 50277e4ab7STrond Myklebust #include <linux/bvec.h> 516a829eb8STrond Myklebust #include <linux/highmem.h> 52277e4ab7STrond Myklebust #include <linux/uio.h> 53a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 54a246b010SChuck Lever 5540b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 5640b5ea0cSTrond Myklebust 574cfc7e60SRahul Iyer #include "sunrpc.h" 58176e21eeSChuck Lever 59176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 607196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 617196dbb0STrond Myklebust struct socket *sock); 62176e21eeSChuck Lever 639903cd1cSChuck Lever /* 64c556b754SChuck Lever * xprtsock tunables 65c556b754SChuck Lever */ 6609acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 6709acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 6809acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 69c556b754SChuck Lever 7009acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 7109acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 72c556b754SChuck Lever 737d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 7425fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 757d1e8255STrond Myklebust 76c556b754SChuck Lever /* 77fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 78fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 79fbf76683SChuck Lever * directory become the union of all files registered there. 80fbf76683SChuck Lever * 81fbf76683SChuck Lever * We simply need to make sure that we don't collide with 82fbf76683SChuck Lever * someone else's file names! 83fbf76683SChuck Lever */ 84fbf76683SChuck Lever 85fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 86fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 87d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 88fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 89fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 90fbf76683SChuck Lever 91fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 92fbf76683SChuck Lever 93fbf76683SChuck Lever /* 94fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 95fbf76683SChuck Lever * socket buffers for existing UDP transports 96fbf76683SChuck Lever */ 97fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 98fbf76683SChuck Lever { 99fbf76683SChuck Lever .procname = "udp_slot_table_entries", 100fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 101fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 102fbf76683SChuck Lever .mode = 0644, 1036d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 104fbf76683SChuck Lever .extra1 = &min_slot_table_size, 105fbf76683SChuck Lever .extra2 = &max_slot_table_size 106fbf76683SChuck Lever }, 107fbf76683SChuck Lever { 108fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 109fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 110fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 111fbf76683SChuck Lever .mode = 0644, 1126d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 113fbf76683SChuck Lever .extra1 = &min_slot_table_size, 114fbf76683SChuck Lever .extra2 = &max_slot_table_size 115fbf76683SChuck Lever }, 116fbf76683SChuck Lever { 117d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 118d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 119d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 120d9ba131dSTrond Myklebust .mode = 0644, 121d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 122d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 123d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 124d9ba131dSTrond Myklebust }, 125d9ba131dSTrond Myklebust { 126fbf76683SChuck Lever .procname = "min_resvport", 127fbf76683SChuck Lever .data = &xprt_min_resvport, 128fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 129fbf76683SChuck Lever .mode = 0644, 1306d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 131fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 132826799e6SJ. Bruce Fields .extra2 = &xprt_max_resvport_limit 133fbf76683SChuck Lever }, 134fbf76683SChuck Lever { 135fbf76683SChuck Lever .procname = "max_resvport", 136fbf76683SChuck Lever .data = &xprt_max_resvport, 137fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 138fbf76683SChuck Lever .mode = 0644, 1396d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 140826799e6SJ. Bruce Fields .extra1 = &xprt_min_resvport_limit, 141fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 142fbf76683SChuck Lever }, 143fbf76683SChuck Lever { 14425fe6142STrond Myklebust .procname = "tcp_fin_timeout", 14525fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 14625fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 14725fe6142STrond Myklebust .mode = 0644, 1486d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 14925fe6142STrond Myklebust }, 150f8572d8fSEric W. Biederman { }, 151fbf76683SChuck Lever }; 152fbf76683SChuck Lever 153fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = { 154fbf76683SChuck Lever { 155fbf76683SChuck Lever .procname = "sunrpc", 156fbf76683SChuck Lever .mode = 0555, 157fbf76683SChuck Lever .child = xs_tunables_table 158fbf76683SChuck Lever }, 159f8572d8fSEric W. Biederman { }, 160fbf76683SChuck Lever }; 161fbf76683SChuck Lever 162fbf76683SChuck Lever /* 16303bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16403bf4b70SChuck Lever */ 16503bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16603bf4b70SChuck Lever 16703bf4b70SChuck Lever /* 16803bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16903bf4b70SChuck Lever * kind of resource problem on the local host. 17003bf4b70SChuck Lever */ 17103bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17203bf4b70SChuck Lever 17303bf4b70SChuck Lever /* 17403bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17503bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17603bf4b70SChuck Lever * 17703bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17803bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17903bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18003bf4b70SChuck Lever * increase over time if the server is down or not responding. 18103bf4b70SChuck Lever */ 18203bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18303bf4b70SChuck Lever 18403bf4b70SChuck Lever /* 18503bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18603bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18703bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18803bf4b70SChuck Lever */ 18903bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19003bf4b70SChuck Lever 191f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 192a246b010SChuck Lever # undef RPC_DEBUG_DATA 1939903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 194a246b010SChuck Lever #endif 195a246b010SChuck Lever 196a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1979903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 198a246b010SChuck Lever { 199a246b010SChuck Lever u8 *buf = (u8 *) packet; 200a246b010SChuck Lever int j; 201a246b010SChuck Lever 202a246b010SChuck Lever dprintk("RPC: %s\n", msg); 203a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 204a246b010SChuck Lever if (!(j & 31)) { 205a246b010SChuck Lever if (j) 206a246b010SChuck Lever dprintk("\n"); 207a246b010SChuck Lever dprintk("0x%04x ", j); 208a246b010SChuck Lever } 209a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 210a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 211a246b010SChuck Lever } 212a246b010SChuck Lever dprintk("\n"); 213a246b010SChuck Lever } 214a246b010SChuck Lever #else 2159903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 216a246b010SChuck Lever { 217a246b010SChuck Lever /* NOP */ 218a246b010SChuck Lever } 219a246b010SChuck Lever #endif 220a246b010SChuck Lever 2212118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2222118071dSTrond Myklebust { 2232118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2242118071dSTrond Myklebust } 2252118071dSTrond Myklebust 22695392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 227edb267a6SChuck Lever { 22895392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 22995392c59SChuck Lever } 23095392c59SChuck Lever 231176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 232176e21eeSChuck Lever { 233176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 234176e21eeSChuck Lever } 235176e21eeSChuck Lever 23695392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 23795392c59SChuck Lever { 23895392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 23995392c59SChuck Lever } 24095392c59SChuck Lever 24195392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 24295392c59SChuck Lever { 24395392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 24495392c59SChuck Lever } 24595392c59SChuck Lever 246c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 247c877b849SChuck Lever { 248c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2499dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2509dc3b095SChuck Lever struct sockaddr_in *sin; 251176e21eeSChuck Lever struct sockaddr_un *sun; 252c877b849SChuck Lever char buf[128]; 253c877b849SChuck Lever 2549dc3b095SChuck Lever switch (sap->sa_family) { 255176e21eeSChuck Lever case AF_LOCAL: 256176e21eeSChuck Lever sun = xs_addr_un(xprt); 257176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 258176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 259176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 260176e21eeSChuck Lever break; 2619dc3b095SChuck Lever case AF_INET: 262176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 263176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 264176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2659dc3b095SChuck Lever sin = xs_addr_in(xprt); 266fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2679dc3b095SChuck Lever break; 2689dc3b095SChuck Lever case AF_INET6: 269176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 270176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 271176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2729dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 273fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2749dc3b095SChuck Lever break; 2759dc3b095SChuck Lever default: 2769dc3b095SChuck Lever BUG(); 2779dc3b095SChuck Lever } 278176e21eeSChuck Lever 2799dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2809dc3b095SChuck Lever } 2819dc3b095SChuck Lever 2829dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2839dc3b095SChuck Lever { 2849dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2859dc3b095SChuck Lever char buf[128]; 2869dc3b095SChuck Lever 28781160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 288c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 289c877b849SChuck Lever 29081160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 291c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 292c877b849SChuck Lever } 293c877b849SChuck Lever 2949dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 295b454ae90SChuck Lever const char *protocol, 296b454ae90SChuck Lever const char *netid) 297edb267a6SChuck Lever { 298b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 299b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 300c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3019dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 302edb267a6SChuck Lever } 303edb267a6SChuck Lever 3049dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3054b6473fbSChuck Lever { 3069dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3079dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3084b6473fbSChuck Lever 3099dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 310edb267a6SChuck Lever } 311edb267a6SChuck Lever 312edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 313edb267a6SChuck Lever { 31433e01dc7SChuck Lever unsigned int i; 31533e01dc7SChuck Lever 31633e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 31733e01dc7SChuck Lever switch (i) { 31833e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 31933e01dc7SChuck Lever case RPC_DISPLAY_NETID: 32033e01dc7SChuck Lever continue; 32133e01dc7SChuck Lever default: 32233e01dc7SChuck Lever kfree(xprt->address_strings[i]); 32333e01dc7SChuck Lever } 324edb267a6SChuck Lever } 325edb267a6SChuck Lever 326277e4ab7STrond Myklebust static size_t 327277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 328277e4ab7STrond Myklebust { 329277e4ab7STrond Myklebust size_t i,n; 330277e4ab7STrond Myklebust 33116e5e90fSTrond Myklebust if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 332277e4ab7STrond Myklebust return want; 333277e4ab7STrond Myklebust n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 334277e4ab7STrond Myklebust for (i = 0; i < n; i++) { 335277e4ab7STrond Myklebust if (buf->pages[i]) 336277e4ab7STrond Myklebust continue; 337277e4ab7STrond Myklebust buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 338277e4ab7STrond Myklebust if (!buf->pages[i]) { 33916e5e90fSTrond Myklebust i *= PAGE_SIZE; 34016e5e90fSTrond Myklebust return i > buf->page_base ? i - buf->page_base : 0; 341277e4ab7STrond Myklebust } 342277e4ab7STrond Myklebust } 343277e4ab7STrond Myklebust return want; 344277e4ab7STrond Myklebust } 345277e4ab7STrond Myklebust 346277e4ab7STrond Myklebust static ssize_t 347277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 348277e4ab7STrond Myklebust { 349277e4ab7STrond Myklebust ssize_t ret; 350277e4ab7STrond Myklebust if (seek != 0) 351277e4ab7STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 352277e4ab7STrond Myklebust ret = sock_recvmsg(sock, msg, flags); 353277e4ab7STrond Myklebust return ret > 0 ? ret + seek : ret; 354277e4ab7STrond Myklebust } 355277e4ab7STrond Myklebust 356277e4ab7STrond Myklebust static ssize_t 357277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 358277e4ab7STrond Myklebust struct kvec *kvec, size_t count, size_t seek) 359277e4ab7STrond Myklebust { 3600e9b4a82SAl Viro iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); 361277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 362277e4ab7STrond Myklebust } 363277e4ab7STrond Myklebust 364277e4ab7STrond Myklebust static ssize_t 365277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 366277e4ab7STrond Myklebust struct bio_vec *bvec, unsigned long nr, size_t count, 367277e4ab7STrond Myklebust size_t seek) 368277e4ab7STrond Myklebust { 3690e9b4a82SAl Viro iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); 370277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 371277e4ab7STrond Myklebust } 372277e4ab7STrond Myklebust 373277e4ab7STrond Myklebust static ssize_t 374277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 375277e4ab7STrond Myklebust size_t count) 376277e4ab7STrond Myklebust { 377b76a5afdSTrond Myklebust iov_iter_discard(&msg->msg_iter, READ, count); 378b76a5afdSTrond Myklebust return sock_recvmsg(sock, msg, flags); 379277e4ab7STrond Myklebust } 380277e4ab7STrond Myklebust 3816a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 3826a829eb8STrond Myklebust static void 3836a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 3846a829eb8STrond Myklebust { 3856a829eb8STrond Myklebust struct bvec_iter bi = { 3866a829eb8STrond Myklebust .bi_size = count, 3876a829eb8STrond Myklebust }; 3886a829eb8STrond Myklebust struct bio_vec bv; 3896a829eb8STrond Myklebust 3906a829eb8STrond Myklebust bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 3916a829eb8STrond Myklebust for_each_bvec(bv, bvec, bi, bi) 3926a829eb8STrond Myklebust flush_dcache_page(bv.bv_page); 3936a829eb8STrond Myklebust } 3946a829eb8STrond Myklebust #else 3956a829eb8STrond Myklebust static inline void 3966a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 3976a829eb8STrond Myklebust { 3986a829eb8STrond Myklebust } 3996a829eb8STrond Myklebust #endif 4006a829eb8STrond Myklebust 401277e4ab7STrond Myklebust static ssize_t 402277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 403277e4ab7STrond Myklebust struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 404277e4ab7STrond Myklebust { 405277e4ab7STrond Myklebust size_t want, seek_init = seek, offset = 0; 406277e4ab7STrond Myklebust ssize_t ret; 407277e4ab7STrond Myklebust 408277e4ab7STrond Myklebust want = min_t(size_t, count, buf->head[0].iov_len); 409e92053a5STrond Myklebust if (seek < want) { 410277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 411277e4ab7STrond Myklebust if (ret <= 0) 412277e4ab7STrond Myklebust goto sock_err; 413277e4ab7STrond Myklebust offset += ret; 414277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 415277e4ab7STrond Myklebust goto out; 416277e4ab7STrond Myklebust if (ret != want) 417c4433055STrond Myklebust goto out; 418277e4ab7STrond Myklebust seek = 0; 419277e4ab7STrond Myklebust } else { 420e92053a5STrond Myklebust seek -= want; 421e92053a5STrond Myklebust offset += want; 422277e4ab7STrond Myklebust } 42316e5e90fSTrond Myklebust 424277e4ab7STrond Myklebust want = xs_alloc_sparse_pages(buf, 425277e4ab7STrond Myklebust min_t(size_t, count - offset, buf->page_len), 426277e4ab7STrond Myklebust GFP_NOWAIT); 42716e5e90fSTrond Myklebust if (seek < want) { 428277e4ab7STrond Myklebust ret = xs_read_bvec(sock, msg, flags, buf->bvec, 429277e4ab7STrond Myklebust xdr_buf_pagecount(buf), 430277e4ab7STrond Myklebust want + buf->page_base, 431277e4ab7STrond Myklebust seek + buf->page_base); 432277e4ab7STrond Myklebust if (ret <= 0) 433277e4ab7STrond Myklebust goto sock_err; 4346a829eb8STrond Myklebust xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 435277e4ab7STrond Myklebust offset += ret - buf->page_base; 436277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 437277e4ab7STrond Myklebust goto out; 438277e4ab7STrond Myklebust if (ret != want) 439c4433055STrond Myklebust goto out; 440277e4ab7STrond Myklebust seek = 0; 441277e4ab7STrond Myklebust } else { 44216e5e90fSTrond Myklebust seek -= want; 44316e5e90fSTrond Myklebust offset += want; 444277e4ab7STrond Myklebust } 44516e5e90fSTrond Myklebust 446277e4ab7STrond Myklebust want = min_t(size_t, count - offset, buf->tail[0].iov_len); 447e92053a5STrond Myklebust if (seek < want) { 448277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 449277e4ab7STrond Myklebust if (ret <= 0) 450277e4ab7STrond Myklebust goto sock_err; 451277e4ab7STrond Myklebust offset += ret; 452277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 453277e4ab7STrond Myklebust goto out; 454277e4ab7STrond Myklebust if (ret != want) 455c4433055STrond Myklebust goto out; 456277e4ab7STrond Myklebust } else 457e92053a5STrond Myklebust offset = seek_init; 458277e4ab7STrond Myklebust ret = -EMSGSIZE; 459277e4ab7STrond Myklebust out: 460277e4ab7STrond Myklebust *read = offset - seek_init; 461277e4ab7STrond Myklebust return ret; 462277e4ab7STrond Myklebust sock_err: 463277e4ab7STrond Myklebust offset += seek; 464277e4ab7STrond Myklebust goto out; 465277e4ab7STrond Myklebust } 466277e4ab7STrond Myklebust 467277e4ab7STrond Myklebust static void 468277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 469277e4ab7STrond Myklebust { 470277e4ab7STrond Myklebust if (!transport->recv.copied) { 471277e4ab7STrond Myklebust if (buf->head[0].iov_len >= transport->recv.offset) 472277e4ab7STrond Myklebust memcpy(buf->head[0].iov_base, 473277e4ab7STrond Myklebust &transport->recv.xid, 474277e4ab7STrond Myklebust transport->recv.offset); 475277e4ab7STrond Myklebust transport->recv.copied = transport->recv.offset; 476277e4ab7STrond Myklebust } 477277e4ab7STrond Myklebust } 478277e4ab7STrond Myklebust 479277e4ab7STrond Myklebust static bool 480277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport) 481277e4ab7STrond Myklebust { 482277e4ab7STrond Myklebust return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 483277e4ab7STrond Myklebust } 484277e4ab7STrond Myklebust 485e92053a5STrond Myklebust static void 486e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport, 487e92053a5STrond Myklebust struct msghdr *msg) 488e92053a5STrond Myklebust { 489e92053a5STrond Myklebust if (xs_read_stream_request_done(transport)) 490e92053a5STrond Myklebust msg->msg_flags |= MSG_EOR; 491e92053a5STrond Myklebust } 492e92053a5STrond Myklebust 493277e4ab7STrond Myklebust static ssize_t 494277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 495277e4ab7STrond Myklebust int flags, struct rpc_rqst *req) 496277e4ab7STrond Myklebust { 497277e4ab7STrond Myklebust struct xdr_buf *buf = &req->rq_private_buf; 498277e4ab7STrond Myklebust size_t want, read; 499277e4ab7STrond Myklebust ssize_t ret; 500277e4ab7STrond Myklebust 501277e4ab7STrond Myklebust xs_read_header(transport, buf); 502277e4ab7STrond Myklebust 503277e4ab7STrond Myklebust want = transport->recv.len - transport->recv.offset; 504e92053a5STrond Myklebust if (want != 0) { 505277e4ab7STrond Myklebust ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 506e92053a5STrond Myklebust transport->recv.copied + want, 507e92053a5STrond Myklebust transport->recv.copied, 508277e4ab7STrond Myklebust &read); 509277e4ab7STrond Myklebust transport->recv.offset += read; 510277e4ab7STrond Myklebust transport->recv.copied += read; 511277e4ab7STrond Myklebust } 512277e4ab7STrond Myklebust 513727fcc64STrond Myklebust if (transport->recv.offset == transport->recv.len) 514727fcc64STrond Myklebust xs_read_stream_check_eor(transport, msg); 515727fcc64STrond Myklebust 516e92053a5STrond Myklebust if (want == 0) 517e92053a5STrond Myklebust return 0; 518e92053a5STrond Myklebust 519277e4ab7STrond Myklebust switch (ret) { 520c4433055STrond Myklebust default: 521c4433055STrond Myklebust break; 52226781eabSTrond Myklebust case -EFAULT: 523277e4ab7STrond Myklebust case -EMSGSIZE: 52426781eabSTrond Myklebust msg->msg_flags |= MSG_TRUNC; 525c4433055STrond Myklebust return read; 526277e4ab7STrond Myklebust case 0: 527277e4ab7STrond Myklebust return -ESHUTDOWN; 528277e4ab7STrond Myklebust } 529c4433055STrond Myklebust return ret < 0 ? ret : read; 530277e4ab7STrond Myklebust } 531277e4ab7STrond Myklebust 532277e4ab7STrond Myklebust static size_t 533277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag) 534277e4ab7STrond Myklebust { 535277e4ab7STrond Myklebust if (isfrag) 536277e4ab7STrond Myklebust return sizeof(__be32); 537277e4ab7STrond Myklebust return 3 * sizeof(__be32); 538277e4ab7STrond Myklebust } 539277e4ab7STrond Myklebust 540277e4ab7STrond Myklebust static ssize_t 541277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 542277e4ab7STrond Myklebust int flags, size_t want, size_t seek) 543277e4ab7STrond Myklebust { 544277e4ab7STrond Myklebust struct kvec kvec = { 545277e4ab7STrond Myklebust .iov_base = &transport->recv.fraghdr, 546277e4ab7STrond Myklebust .iov_len = want, 547277e4ab7STrond Myklebust }; 548277e4ab7STrond Myklebust return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 549277e4ab7STrond Myklebust } 550277e4ab7STrond Myklebust 551277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 552277e4ab7STrond Myklebust static ssize_t 553277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 554277e4ab7STrond Myklebust { 555277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 556277e4ab7STrond Myklebust struct rpc_rqst *req; 557277e4ab7STrond Myklebust ssize_t ret; 558277e4ab7STrond Myklebust 559277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 560277e4ab7STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 561277e4ab7STrond Myklebust if (!req) { 562277e4ab7STrond Myklebust printk(KERN_WARNING "Callback slot table overflowed\n"); 563277e4ab7STrond Myklebust return -ESHUTDOWN; 564277e4ab7STrond Myklebust } 565277e4ab7STrond Myklebust 566277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 567277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 568c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 569277e4ab7STrond Myklebust 570277e4ab7STrond Myklebust return ret; 571277e4ab7STrond Myklebust } 572277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 573277e4ab7STrond Myklebust static ssize_t 574277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 575277e4ab7STrond Myklebust { 576277e4ab7STrond Myklebust return -ESHUTDOWN; 577277e4ab7STrond Myklebust } 578277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 579277e4ab7STrond Myklebust 580277e4ab7STrond Myklebust static ssize_t 581277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 582277e4ab7STrond Myklebust { 583277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 584277e4ab7STrond Myklebust struct rpc_rqst *req; 585277e4ab7STrond Myklebust ssize_t ret = 0; 586277e4ab7STrond Myklebust 587277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 588277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 589277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 590277e4ab7STrond Myklebust if (!req) { 591277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 592277e4ab7STrond Myklebust goto out; 593277e4ab7STrond Myklebust } 594277e4ab7STrond Myklebust xprt_pin_rqst(req); 595277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 596277e4ab7STrond Myklebust 597277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 598277e4ab7STrond Myklebust 599277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 600277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 601c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 602277e4ab7STrond Myklebust xprt_unpin_rqst(req); 603277e4ab7STrond Myklebust out: 604277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 605277e4ab7STrond Myklebust return ret; 606277e4ab7STrond Myklebust } 607277e4ab7STrond Myklebust 608277e4ab7STrond Myklebust static ssize_t 609277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 610277e4ab7STrond Myklebust { 611277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 612277e4ab7STrond Myklebust size_t want, read = 0; 613277e4ab7STrond Myklebust ssize_t ret = 0; 614277e4ab7STrond Myklebust 615277e4ab7STrond Myklebust if (transport->recv.len == 0) { 616277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 617277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 618277e4ab7STrond Myklebust transport->recv.offset); 619277e4ab7STrond Myklebust if (ret <= 0) 620277e4ab7STrond Myklebust goto out_err; 621277e4ab7STrond Myklebust transport->recv.offset = ret; 622c4433055STrond Myklebust if (transport->recv.offset != want) 623c4433055STrond Myklebust return transport->recv.offset; 624277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 625277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 626277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 627277e4ab7STrond Myklebust read = ret; 628277e4ab7STrond Myklebust } 629277e4ab7STrond Myklebust 630277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 631c4433055STrond Myklebust default: 632c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 633c4433055STrond Myklebust break; 634277e4ab7STrond Myklebust case RPC_CALL: 635277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 636277e4ab7STrond Myklebust break; 637277e4ab7STrond Myklebust case RPC_REPLY: 638277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 639277e4ab7STrond Myklebust } 640277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 641277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 642277e4ab7STrond Myklebust transport->recv.copied = -1; 643277e4ab7STrond Myklebust } 644277e4ab7STrond Myklebust if (ret < 0) 645277e4ab7STrond Myklebust goto out_err; 646277e4ab7STrond Myklebust read += ret; 647277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 648c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 649c4433055STrond Myklebust return read; 650b76a5afdSTrond Myklebust msg.msg_flags = 0; 651277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 652277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 653277e4ab7STrond Myklebust if (ret <= 0) 654277e4ab7STrond Myklebust goto out_err; 655277e4ab7STrond Myklebust transport->recv.offset += ret; 656277e4ab7STrond Myklebust read += ret; 657277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 658c4433055STrond Myklebust return read; 659277e4ab7STrond Myklebust } 660277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 661c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 662277e4ab7STrond Myklebust transport->recv.copied = 0; 663277e4ab7STrond Myklebust } 664277e4ab7STrond Myklebust transport->recv.offset = 0; 665277e4ab7STrond Myklebust transport->recv.len = 0; 666277e4ab7STrond Myklebust return read; 667277e4ab7STrond Myklebust out_err: 66879462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 669277e4ab7STrond Myklebust } 670277e4ab7STrond Myklebust 6710ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 6720ffe86f4STrond Myklebust { 6730ffe86f4STrond Myklebust return transport->sock->ops->poll(NULL, transport->sock, NULL); 6740ffe86f4STrond Myklebust } 6750ffe86f4STrond Myklebust 6760ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 6770ffe86f4STrond Myklebust { 6780ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 6790ffe86f4STrond Myklebust 6800ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 6810ffe86f4STrond Myklebust } 6820ffe86f4STrond Myklebust 6830ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 6840ffe86f4STrond Myklebust { 6850ffe86f4STrond Myklebust 6860ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 6870ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 6880ffe86f4STrond Myklebust return; 6890ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 6900ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 6910ffe86f4STrond Myklebust } 6920ffe86f4STrond Myklebust 693c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 694c50b8ee0STrond Myklebust { 695c50b8ee0STrond Myklebust size_t read = 0; 696c50b8ee0STrond Myklebust ssize_t ret = 0; 697c50b8ee0STrond Myklebust 698c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 699c50b8ee0STrond Myklebust if (transport->sock == NULL) 700c50b8ee0STrond Myklebust goto out; 701c50b8ee0STrond Myklebust for (;;) { 702c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 703c4433055STrond Myklebust if (ret < 0) 704c50b8ee0STrond Myklebust break; 705c50b8ee0STrond Myklebust read += ret; 706c50b8ee0STrond Myklebust cond_resched(); 707c50b8ee0STrond Myklebust } 7085f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7095f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 7105f52a9d4STrond Myklebust else 7110ffe86f4STrond Myklebust xs_poll_check_readable(transport); 712c50b8ee0STrond Myklebust out: 713c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 714c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 715c50b8ee0STrond Myklebust } 716c50b8ee0STrond Myklebust 717c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 718c50b8ee0STrond Myklebust { 719c50b8ee0STrond Myklebust struct sock_xprt *transport = 720c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 721a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 722a1231fdaSTrond Myklebust 723c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 724a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 725c50b8ee0STrond Myklebust } 726c50b8ee0STrond Myklebust 727550aebfeSTrond Myklebust static void 728550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 729550aebfeSTrond Myklebust { 730550aebfeSTrond Myklebust transport->recv.offset = 0; 731550aebfeSTrond Myklebust transport->recv.len = 0; 732550aebfeSTrond Myklebust transport->recv.copied = 0; 733550aebfeSTrond Myklebust transport->xmit.offset = 0; 734ae053551STrond Myklebust } 735ae053551STrond Myklebust 736ae053551STrond Myklebust static void 737ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 738ae053551STrond Myklebust { 739550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 740550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 741550aebfeSTrond Myklebust } 742550aebfeSTrond Myklebust 743b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 744b4b5cc85SChuck Lever 745*e791f8e9STrond Myklebust static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek) 746b4b5cc85SChuck Lever { 747*e791f8e9STrond Myklebust if (seek) 748*e791f8e9STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 749*e791f8e9STrond Myklebust return sock_sendmsg(sock, msg); 750*e791f8e9STrond Myklebust } 751b4b5cc85SChuck Lever 752*e791f8e9STrond Myklebust static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek) 753*e791f8e9STrond Myklebust { 754*e791f8e9STrond Myklebust if (!vec) { 755*e791f8e9STrond Myklebust iov_iter_kvec(&msg->msg_iter, WRITE, NULL, 0, 0); 756*e791f8e9STrond Myklebust return sock_sendmsg(sock, msg); 757*e791f8e9STrond Myklebust } 758*e791f8e9STrond Myklebust iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len); 759*e791f8e9STrond Myklebust return xs_sendmsg(sock, msg, seek); 760b4b5cc85SChuck Lever } 761b4b5cc85SChuck Lever 762f279cd00SJason Baron static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p) 763b4b5cc85SChuck Lever { 764a6b31d18STrond Myklebust ssize_t (*do_sendpage)(struct socket *sock, struct page *page, 765a6b31d18STrond Myklebust int offset, size_t size, int flags); 76624c5684bSTrond Myklebust struct page **ppage; 76724c5684bSTrond Myklebust unsigned int remainder; 768f279cd00SJason Baron int err; 769b4b5cc85SChuck Lever 77024c5684bSTrond Myklebust remainder = xdr->page_len - base; 77124c5684bSTrond Myklebust base += xdr->page_base; 77224c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 77324c5684bSTrond Myklebust base &= ~PAGE_MASK; 774a6b31d18STrond Myklebust do_sendpage = sock->ops->sendpage; 775a6b31d18STrond Myklebust if (!zerocopy) 776a6b31d18STrond Myklebust do_sendpage = sock_no_sendpage; 77724c5684bSTrond Myklebust for(;;) { 77824c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 77924c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 78024c5684bSTrond Myklebust 78124c5684bSTrond Myklebust remainder -= len; 78231303d6cSTrond Myklebust if (more) 78324c5684bSTrond Myklebust flags |= MSG_MORE; 78431303d6cSTrond Myklebust if (remainder != 0) 78531303d6cSTrond Myklebust flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE; 786a6b31d18STrond Myklebust err = do_sendpage(sock, *ppage, base, len, flags); 78724c5684bSTrond Myklebust if (remainder == 0 || err != len) 78824c5684bSTrond Myklebust break; 789f279cd00SJason Baron *sent_p += err; 79024c5684bSTrond Myklebust ppage++; 79124c5684bSTrond Myklebust base = 0; 79224c5684bSTrond Myklebust } 793f279cd00SJason Baron if (err > 0) { 794f279cd00SJason Baron *sent_p += err; 795f279cd00SJason Baron err = 0; 796f279cd00SJason Baron } 79724c5684bSTrond Myklebust return err; 798b4b5cc85SChuck Lever } 799b4b5cc85SChuck Lever 8009903cd1cSChuck Lever /** 8019903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 8029903cd1cSChuck Lever * @sock: socket to send on 8039903cd1cSChuck Lever * @addr: UDP only -- address of destination 8049903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 8059903cd1cSChuck Lever * @xdr: buffer containing this request 8069903cd1cSChuck Lever * @base: starting position in the buffer 807a6b31d18STrond Myklebust * @zerocopy: true if it is safe to use sendpage() 808f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 8099903cd1cSChuck Lever * 810a246b010SChuck Lever */ 811f279cd00SJason Baron static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p) 812a246b010SChuck Lever { 813*e791f8e9STrond Myklebust struct msghdr msg = { 814*e791f8e9STrond Myklebust .msg_name = addr, 815*e791f8e9STrond Myklebust .msg_namelen = addrlen, 816*e791f8e9STrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE, 817*e791f8e9STrond Myklebust }; 81824c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 819f279cd00SJason Baron int err = 0; 820f279cd00SJason Baron int sent = 0; 821a246b010SChuck Lever 822262965f5SChuck Lever if (unlikely(!sock)) 823fba91afbSTrond Myklebust return -ENOTSOCK; 824262965f5SChuck Lever 82524c5684bSTrond Myklebust if (base != 0) { 82624c5684bSTrond Myklebust addr = NULL; 82724c5684bSTrond Myklebust addrlen = 0; 82824c5684bSTrond Myklebust } 829262965f5SChuck Lever 83024c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 83124c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 83224c5684bSTrond Myklebust remainder -= len; 833*e791f8e9STrond Myklebust if (remainder == 0) 834*e791f8e9STrond Myklebust msg.msg_flags &= ~MSG_MORE; 835*e791f8e9STrond Myklebust err = xs_send_kvec(sock, &msg, &xdr->head[0], base); 83624c5684bSTrond Myklebust if (remainder == 0 || err != len) 837a246b010SChuck Lever goto out; 838f279cd00SJason Baron *sent_p += err; 839a246b010SChuck Lever base = 0; 840a246b010SChuck Lever } else 84124c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 842a246b010SChuck Lever 84324c5684bSTrond Myklebust if (base < xdr->page_len) { 84424c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 84524c5684bSTrond Myklebust remainder -= len; 846f279cd00SJason Baron err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); 847f279cd00SJason Baron *sent_p += sent; 848f279cd00SJason Baron if (remainder == 0 || sent != len) 849a246b010SChuck Lever goto out; 850a246b010SChuck Lever base = 0; 85124c5684bSTrond Myklebust } else 85224c5684bSTrond Myklebust base -= xdr->page_len; 85324c5684bSTrond Myklebust 85424c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 855f279cd00SJason Baron return 0; 856*e791f8e9STrond Myklebust msg.msg_flags &= ~MSG_MORE; 857*e791f8e9STrond Myklebust err = xs_send_kvec(sock, &msg, &xdr->tail[0], base); 858a246b010SChuck Lever out: 859f279cd00SJason Baron if (err > 0) { 860f279cd00SJason Baron *sent_p += err; 861f279cd00SJason Baron err = 0; 862f279cd00SJason Baron } 86324c5684bSTrond Myklebust return err; 864a246b010SChuck Lever } 865a246b010SChuck Lever 8669903cd1cSChuck Lever /** 867c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 86850f484e2STrond Myklebust * @req: pointer to RPC request 8699903cd1cSChuck Lever * 870a246b010SChuck Lever */ 871c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req) 872a246b010SChuck Lever { 873262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 874ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 87506ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 87624ca9a84STrond Myklebust int ret = -EAGAIN; 877a246b010SChuck Lever 87846121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 879c544577dSTrond Myklebust req->rq_task->tk_pid, 880c544577dSTrond Myklebust req->rq_slen - transport->xmit.offset, 881262965f5SChuck Lever req->rq_slen); 882a246b010SChuck Lever 883262965f5SChuck Lever /* Protect against races with write_space */ 884262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 885a246b010SChuck Lever 886262965f5SChuck Lever /* Don't race with disconnect */ 887b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 88813331a55STrond Myklebust /* wait for more buffer space */ 88906ea0bfeSTrond Myklebust sk->sk_write_pending++; 890c544577dSTrond Myklebust xprt_wait_for_buffer_space(xprt); 89113331a55STrond Myklebust } else 8925e3771ceSTrond Myklebust ret = -ENOTCONN; 893a246b010SChuck Lever 894262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 89506ea0bfeSTrond Myklebust 89606ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 897d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 898d48f9ce7SDavid Vrabel struct socket_wq *wq; 899d48f9ce7SDavid Vrabel 900d48f9ce7SDavid Vrabel rcu_read_lock(); 901d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 902d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 903d48f9ce7SDavid Vrabel rcu_read_unlock(); 904d48f9ce7SDavid Vrabel 90506ea0bfeSTrond Myklebust sk->sk_write_space(sk); 906d48f9ce7SDavid Vrabel } 9075e3771ceSTrond Myklebust return ret; 908a246b010SChuck Lever } 909a246b010SChuck Lever 910277e4ab7STrond Myklebust static void 911277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req) 912277e4ab7STrond Myklebust { 913277e4ab7STrond Myklebust req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO); 914277e4ab7STrond Myklebust } 915277e4ab7STrond Myklebust 91661677eeeSChuck Lever /* 9174cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 9184cd34e7cSTrond Myklebust * could complete transmission. 9194cd34e7cSTrond Myklebust */ 9204cd34e7cSTrond Myklebust static bool 9214cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 9224cd34e7cSTrond Myklebust { 9234cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 9244cd34e7cSTrond Myklebust } 9254cd34e7cSTrond Myklebust 9264cd34e7cSTrond Myklebust /* 92761677eeeSChuck Lever * Construct a stream transport record marker in @buf. 92861677eeeSChuck Lever */ 92961677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 93061677eeeSChuck Lever { 93161677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 93261677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 93361677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 93461677eeeSChuck Lever } 93561677eeeSChuck Lever 9369903cd1cSChuck Lever /** 937176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 93850f484e2STrond Myklebust * @req: pointer to RPC request 939176e21eeSChuck Lever * 940176e21eeSChuck Lever * Return values: 941176e21eeSChuck Lever * 0: The request has been sent 942176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 943176e21eeSChuck Lever * complete the request 944176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 945176e21eeSChuck Lever * other: Some other error occured, the request was not sent 946176e21eeSChuck Lever */ 947adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 948176e21eeSChuck Lever { 949176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 950176e21eeSChuck Lever struct sock_xprt *transport = 951176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 952176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 953176e21eeSChuck Lever int status; 954f279cd00SJason Baron int sent = 0; 955176e21eeSChuck Lever 9564cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 9574cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 9584cd34e7cSTrond Myklebust xs_close(xprt); 9594cd34e7cSTrond Myklebust return -ENOTCONN; 9604cd34e7cSTrond Myklebust } 9614cd34e7cSTrond Myklebust 962176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 963176e21eeSChuck Lever 964176e21eeSChuck Lever xs_pktdump("packet data:", 965176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 966176e21eeSChuck Lever 96778215759SChuck Lever req->rq_xtime = ktime_get(); 9686c7a64e5STrond Myklebust status = xs_sendpages(transport->sock, NULL, 0, xdr, 9696c7a64e5STrond Myklebust transport->xmit.offset, 970f279cd00SJason Baron true, &sent); 971176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 9726c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 973743c69e7SNeilBrown 974743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 975743c69e7SNeilBrown status = -ENOBUFS; 976743c69e7SNeilBrown 977f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 9786c7a64e5STrond Myklebust transport->xmit.offset += sent; 9796c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 980176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 9816c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 9826c7a64e5STrond Myklebust transport->xmit.offset = 0; 983176e21eeSChuck Lever return 0; 984176e21eeSChuck Lever } 985176e21eeSChuck Lever status = -EAGAIN; 986176e21eeSChuck Lever } 987176e21eeSChuck Lever 988176e21eeSChuck Lever switch (status) { 9893601c4a9STrond Myklebust case -ENOBUFS: 990b5872f0cSTrond Myklebust break; 991176e21eeSChuck Lever case -EAGAIN: 992c544577dSTrond Myklebust status = xs_nospace(req); 993176e21eeSChuck Lever break; 994176e21eeSChuck Lever default: 995176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 996176e21eeSChuck Lever -status); 997e9d47639SGustavo A. R. Silva /* fall through */ 998176e21eeSChuck Lever case -EPIPE: 999176e21eeSChuck Lever xs_close(xprt); 1000176e21eeSChuck Lever status = -ENOTCONN; 1001176e21eeSChuck Lever } 1002176e21eeSChuck Lever 1003176e21eeSChuck Lever return status; 1004176e21eeSChuck Lever } 1005176e21eeSChuck Lever 1006176e21eeSChuck Lever /** 1007262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 100850f484e2STrond Myklebust * @req: pointer to RPC request 10099903cd1cSChuck Lever * 10109903cd1cSChuck Lever * Return values: 10119903cd1cSChuck Lever * 0: The request has been sent 10129903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 10139903cd1cSChuck Lever * complete the request 1014262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 101525985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 10169903cd1cSChuck Lever */ 1017adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 1018a246b010SChuck Lever { 1019a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1020ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1021262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 1022f279cd00SJason Baron int sent = 0; 1023262965f5SChuck Lever int status; 1024262965f5SChuck Lever 1025262965f5SChuck Lever xs_pktdump("packet data:", 1026262965f5SChuck Lever req->rq_svec->iov_base, 1027262965f5SChuck Lever req->rq_svec->iov_len); 1028262965f5SChuck Lever 102901d37c42STrond Myklebust if (!xprt_bound(xprt)) 103001d37c42STrond Myklebust return -ENOTCONN; 103175891f50STrond Myklebust 103275891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 103375891f50STrond Myklebust return -EBADSLT; 103475891f50STrond Myklebust 103578215759SChuck Lever req->rq_xtime = ktime_get(); 1036f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 10376c7a64e5STrond Myklebust xdr, 0, true, &sent); 1038262965f5SChuck Lever 1039262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 10406c7a64e5STrond Myklebust xdr->len, status); 1041262965f5SChuck Lever 10423dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 10433dedbb5cSJason Baron if (status == -EPERM) 10443dedbb5cSJason Baron goto process_status; 10453dedbb5cSJason Baron 1046743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 1047743c69e7SNeilBrown status = -ENOBUFS; 1048743c69e7SNeilBrown 1049f279cd00SJason Baron if (sent > 0 || status == 0) { 1050f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 1051f279cd00SJason Baron if (sent >= req->rq_slen) 1052262965f5SChuck Lever return 0; 1053262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 1054262965f5SChuck Lever status = -EAGAIN; 10552199700fSTrond Myklebust } 1056262965f5SChuck Lever 10573dedbb5cSJason Baron process_status: 1058262965f5SChuck Lever switch (status) { 1059fba91afbSTrond Myklebust case -ENOTSOCK: 1060fba91afbSTrond Myklebust status = -ENOTCONN; 1061fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1062fba91afbSTrond Myklebust break; 1063b6ddf64fSTrond Myklebust case -EAGAIN: 1064c544577dSTrond Myklebust status = xs_nospace(req); 1065b6ddf64fSTrond Myklebust break; 1066262965f5SChuck Lever case -ENETUNREACH: 10673601c4a9STrond Myklebust case -ENOBUFS: 1068262965f5SChuck Lever case -EPIPE: 1069262965f5SChuck Lever case -ECONNREFUSED: 10703dedbb5cSJason Baron case -EPERM: 1071262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 1072262965f5SChuck Lever * prompts ECONNREFUSED. */ 107313331a55STrond Myklebust break; 107413331a55STrond Myklebust default: 107513331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 107613331a55STrond Myklebust -status); 1077262965f5SChuck Lever } 10785fe46e9dSBian Naimeng 1079262965f5SChuck Lever return status; 1080262965f5SChuck Lever } 1081262965f5SChuck Lever 1082e06799f9STrond Myklebust /** 1083262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 108450f484e2STrond Myklebust * @req: pointer to RPC request 1085262965f5SChuck Lever * 1086262965f5SChuck Lever * Return values: 1087262965f5SChuck Lever * 0: The request has been sent 1088262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 1089262965f5SChuck Lever * complete the request 1090262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 109125985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 1092262965f5SChuck Lever * 1093262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 1094262965f5SChuck Lever * if sendmsg is not able to make progress? 1095262965f5SChuck Lever */ 1096adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 1097262965f5SChuck Lever { 1098262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1099ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1100262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 1101a6b31d18STrond Myklebust bool zerocopy = true; 11029ffadfbcSTrond Myklebust bool vm_wait = false; 1103b595bb15SChuck Lever int status; 1104f279cd00SJason Baron int sent; 1105a246b010SChuck Lever 11064cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 11074cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 11084cd34e7cSTrond Myklebust if (transport->sock != NULL) 11094cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 11104cd34e7cSTrond Myklebust return -ENOTCONN; 11114cd34e7cSTrond Myklebust } 11124cd34e7cSTrond Myklebust 111361677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 1114262965f5SChuck Lever 1115262965f5SChuck Lever xs_pktdump("packet data:", 1116262965f5SChuck Lever req->rq_svec->iov_base, 1117262965f5SChuck Lever req->rq_svec->iov_len); 1118a6b31d18STrond Myklebust /* Don't use zero copy if this is a resend. If the RPC call 1119a6b31d18STrond Myklebust * completes while the socket holds a reference to the pages, 1120a6b31d18STrond Myklebust * then we may end up resending corrupted data. 1121a6b31d18STrond Myklebust */ 112250f484e2STrond Myklebust if (req->rq_task->tk_flags & RPC_TASK_SENT) 1123a6b31d18STrond Myklebust zerocopy = false; 1124a246b010SChuck Lever 11257196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 11267196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 11277196dbb0STrond Myklebust 1128a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1129a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1130262965f5SChuck Lever * called sendmsg(). */ 113178215759SChuck Lever req->rq_xtime = ktime_get(); 1132a246b010SChuck Lever while (1) { 1133f279cd00SJason Baron sent = 0; 1134f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 11356c7a64e5STrond Myklebust transport->xmit.offset, 11366c7a64e5STrond Myklebust zerocopy, &sent); 1137a246b010SChuck Lever 1138262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 11396c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1140262965f5SChuck Lever 1141a246b010SChuck Lever /* If we've sent the entire packet, immediately 1142a246b010SChuck Lever * reset the count of bytes sent. */ 11436c7a64e5STrond Myklebust transport->xmit.offset += sent; 11446c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 1145262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 11466c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 11476c7a64e5STrond Myklebust transport->xmit.offset = 0; 1148a246b010SChuck Lever return 0; 1149a246b010SChuck Lever } 1150262965f5SChuck Lever 11519ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 11529ffadfbcSTrond Myklebust 11539ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 11549ffadfbcSTrond Myklebust /* 11559ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 11569ffadfbcSTrond Myklebust * socket send buffer limits. 11579ffadfbcSTrond Myklebust */ 11589ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 11599ffadfbcSTrond Myklebust break; 11609ffadfbcSTrond Myklebust /* 11619ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 11629ffadfbcSTrond Myklebust */ 11639ffadfbcSTrond Myklebust if (sent == 0) { 11649ffadfbcSTrond Myklebust status = -ENOBUFS; 11659ffadfbcSTrond Myklebust if (vm_wait) 11669ffadfbcSTrond Myklebust break; 11679ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 11689ffadfbcSTrond Myklebust * socket send buffer limit 11699ffadfbcSTrond Myklebust */ 11709ffadfbcSTrond Myklebust vm_wait = true; 11719ffadfbcSTrond Myklebust } 11729ffadfbcSTrond Myklebust continue; 11739ffadfbcSTrond Myklebust } 1174f580dd04STrond Myklebust if (status < 0) 1175f580dd04STrond Myklebust break; 11769ffadfbcSTrond Myklebust vm_wait = false; 1177a246b010SChuck Lever } 1178a246b010SChuck Lever 1179262965f5SChuck Lever switch (status) { 1180fba91afbSTrond Myklebust case -ENOTSOCK: 1181fba91afbSTrond Myklebust status = -ENOTCONN; 1182fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1183fba91afbSTrond Myklebust break; 1184262965f5SChuck Lever case -EAGAIN: 1185c544577dSTrond Myklebust status = xs_nospace(req); 1186262965f5SChuck Lever break; 1187262965f5SChuck Lever case -ECONNRESET: 1188262965f5SChuck Lever case -ECONNREFUSED: 1189262965f5SChuck Lever case -ENOTCONN: 11903913c78cSTrond Myklebust case -EADDRINUSE: 1191b5872f0cSTrond Myklebust case -ENOBUFS: 1192b9d2bb2eSTrond Myklebust case -EPIPE: 119313331a55STrond Myklebust break; 119413331a55STrond Myklebust default: 119513331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 119613331a55STrond Myklebust -status); 1197a246b010SChuck Lever } 11985fe46e9dSBian Naimeng 1199a246b010SChuck Lever return status; 1200a246b010SChuck Lever } 1201a246b010SChuck Lever 12022a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 12032a9e1cfaSTrond Myklebust { 12042a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 12052a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 12062a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 12072118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 12082a9e1cfaSTrond Myklebust } 12092a9e1cfaSTrond Myklebust 12102a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 12112a9e1cfaSTrond Myklebust { 12122a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 12132a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 12142a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 12152118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 12162118071dSTrond Myklebust } 12172118071dSTrond Myklebust 121842d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 121942d42a5bSTrond Myklebust { 122042d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 122142d42a5bSTrond Myklebust 122242d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 122342d42a5bSTrond Myklebust } 122442d42a5bSTrond Myklebust 1225b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1226b70ae915STrond Myklebust { 1227b70ae915STrond Myklebust smp_mb__before_atomic(); 1228b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1229b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 123042d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1231b70ae915STrond Myklebust smp_mb__after_atomic(); 1232b70ae915STrond Myklebust } 1233b70ae915STrond Myklebust 12342118071dSTrond Myklebust /** 12352118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 12362118071dSTrond Myklebust * @sk: socket 12372118071dSTrond Myklebust * 12382118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 12392118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 12402118071dSTrond Myklebust */ 12412118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 12422118071dSTrond Myklebust { 12432118071dSTrond Myklebust struct rpc_xprt *xprt; 12442118071dSTrond Myklebust int err; 12452118071dSTrond Myklebust 12462118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 12472118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 12482118071dSTrond Myklebust goto out; 12492118071dSTrond Myklebust 12502118071dSTrond Myklebust err = -sk->sk_err; 12512118071dSTrond Myklebust if (err == 0) 12522118071dSTrond Myklebust goto out; 12532118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 12542118071dSTrond Myklebust xprt, -err); 1255e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 12562118071dSTrond Myklebust xprt_wake_pending_tasks(xprt, err); 12572118071dSTrond Myklebust out: 12582118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 12592a9e1cfaSTrond Myklebust } 12602a9e1cfaSTrond Myklebust 1261fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1262a246b010SChuck Lever { 1263ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1264ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12656cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1266a246b010SChuck Lever 1267fe315e76SChuck Lever if (sk == NULL) 1268fe315e76SChuck Lever return; 12699903cd1cSChuck Lever 1270264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1271264d1df3SJeff Layton sk_clear_memalloc(sk); 1272264d1df3SJeff Layton 127309939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 127409939204STrond Myklebust 1275edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1276a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1277ee0ac0c2SChuck Lever transport->inet = NULL; 1278ee0ac0c2SChuck Lever transport->sock = NULL; 1279a246b010SChuck Lever 1280a246b010SChuck Lever sk->sk_user_data = NULL; 12812a9e1cfaSTrond Myklebust 12822a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12830c78789eSTrond Myklebust xprt_clear_connected(xprt); 1284a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 12856cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1286ae053551STrond Myklebust /* Reset stream record info */ 1287ae053551STrond Myklebust xs_stream_reset_connect(transport); 1288edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1289a246b010SChuck Lever 12906cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1291a246b010SChuck Lever sock_release(sock); 12920445f92cSTrond Myklebust 12930445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1294fe315e76SChuck Lever } 1295fe315e76SChuck Lever 1296fe315e76SChuck Lever /** 1297fe315e76SChuck Lever * xs_close - close a socket 1298fe315e76SChuck Lever * @xprt: transport 1299fe315e76SChuck Lever * 1300fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1301fe315e76SChuck Lever * on the server we want to save. 1302f75e6745STrond Myklebust * 1303f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1304f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1305fe315e76SChuck Lever */ 1306fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1307fe315e76SChuck Lever { 1308fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1309fe315e76SChuck Lever 1310fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1311fe315e76SChuck Lever 1312fe315e76SChuck Lever xs_reset_transport(transport); 131361d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1314a246b010SChuck Lever } 1315a246b010SChuck Lever 13164a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13174a068258SChuck Lever { 13184a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13194a068258SChuck Lever xprt); 13204a068258SChuck Lever xprt_disconnect_done(xprt); 13214a068258SChuck Lever } 13224a068258SChuck Lever 1323315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1324315f3812SKinglong Mee { 1325315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1326315f3812SKinglong Mee xprt_free(xprt); 1327315f3812SKinglong Mee } 1328315f3812SKinglong Mee 13299903cd1cSChuck Lever /** 13309903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13319903cd1cSChuck Lever * @xprt: doomed transport 13329903cd1cSChuck Lever * 13339903cd1cSChuck Lever */ 13349903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1335a246b010SChuck Lever { 133603c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 133703c78827STrond Myklebust struct sock_xprt, xprt); 13389903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13399903cd1cSChuck Lever 134003c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1341a1311d87STrond Myklebust xs_close(xprt); 1342edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1343315f3812SKinglong Mee xs_xprt_free(xprt); 1344a1311d87STrond Myklebust module_put(THIS_MODULE); 1345a246b010SChuck Lever } 1346a246b010SChuck Lever 13479903cd1cSChuck Lever /** 1348f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1349f9b2ee71STrond Myklebust * @xprt: transport 1350f9b2ee71STrond Myklebust * @sk: socket 1351f9b2ee71STrond Myklebust * @skb: skbuff 13529903cd1cSChuck Lever * 1353a246b010SChuck Lever */ 1354f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1355f9b2ee71STrond Myklebust struct sock *sk, 1356f9b2ee71STrond Myklebust struct sk_buff *skb) 1357a246b010SChuck Lever { 1358a246b010SChuck Lever struct rpc_task *task; 1359a246b010SChuck Lever struct rpc_rqst *rovr; 1360f9b2ee71STrond Myklebust int repsize, copied; 1361d8ed029dSAlexey Dobriyan u32 _xid; 1362d8ed029dSAlexey Dobriyan __be32 *xp; 1363a246b010SChuck Lever 13641da8c681SWillem de Bruijn repsize = skb->len; 1365a246b010SChuck Lever if (repsize < 4) { 13669903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1367f9b2ee71STrond Myklebust return; 1368a246b010SChuck Lever } 1369a246b010SChuck Lever 1370a246b010SChuck Lever /* Copy the XID from the skb... */ 13711da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1372a246b010SChuck Lever if (xp == NULL) 1373f9b2ee71STrond Myklebust return; 1374a246b010SChuck Lever 1375a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 137675c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1377a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1378a246b010SChuck Lever if (!rovr) 1379a246b010SChuck Lever goto out_unlock; 1380729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1381ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 138275c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1383a246b010SChuck Lever task = rovr->rq_task; 1384a246b010SChuck Lever 1385a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1386a246b010SChuck Lever copied = repsize; 1387a246b010SChuck Lever 1388a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 13891781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 139075c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13910afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1392729749bbSTrond Myklebust goto out_unpin; 13931781f7f5SHerbert Xu } 13941781f7f5SHerbert Xu 1395a246b010SChuck Lever 1396729749bbSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 13976a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1398ce7c252aSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 139975c84151STrond Myklebust spin_lock(&xprt->queue_lock); 14001570c1e4SChuck Lever xprt_complete_rqst(task, copied); 14010afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1402729749bbSTrond Myklebust out_unpin: 1403729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1404a246b010SChuck Lever out_unlock: 140575c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1406f9b2ee71STrond Myklebust } 1407f9b2ee71STrond Myklebust 1408f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1409f9b2ee71STrond Myklebust { 1410f9b2ee71STrond Myklebust struct sk_buff *skb; 1411f9b2ee71STrond Myklebust struct sock *sk; 1412f9b2ee71STrond Myklebust int err; 1413f9b2ee71STrond Myklebust 1414f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1415f9b2ee71STrond Myklebust sk = transport->inet; 1416f9b2ee71STrond Myklebust if (sk == NULL) 1417f9b2ee71STrond Myklebust goto out; 1418f9b2ee71STrond Myklebust for (;;) { 14197c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 14204f546149STrond Myklebust if (skb == NULL) 14214f546149STrond Myklebust break; 1422f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1423850cbaddSPaolo Abeni consume_skb(skb); 14240af3442aSTrond Myklebust cond_resched(); 1425f9b2ee71STrond Myklebust } 14260ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1427a246b010SChuck Lever out: 1428f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1429f9b2ee71STrond Myklebust } 1430f9b2ee71STrond Myklebust 1431f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1432f9b2ee71STrond Myklebust { 1433f9b2ee71STrond Myklebust struct sock_xprt *transport = 1434f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1435a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1436a1231fdaSTrond Myklebust 1437f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1438a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1439f9b2ee71STrond Myklebust } 1440f9b2ee71STrond Myklebust 1441f9b2ee71STrond Myklebust /** 1442f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1443f9b2ee71STrond Myklebust * @sk: socket with data to read 1444f9b2ee71STrond Myklebust * 1445f9b2ee71STrond Myklebust */ 1446f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1447f9b2ee71STrond Myklebust { 1448f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1449f9b2ee71STrond Myklebust 1450f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1451f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1452f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1453f9b2ee71STrond Myklebust if (xprt != NULL) { 1454f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1455f9b2ee71STrond Myklebust struct sock_xprt, xprt); 14565157b956STrond Myklebust transport->old_data_ready(sk); 14575157b956STrond Myklebust /* Any data means we had a useful conversation, so 14585157b956STrond Myklebust * then we don't need to delay the next reconnect 14595157b956STrond Myklebust */ 14605157b956STrond Myklebust if (xprt->reestablish_timeout) 14615157b956STrond Myklebust xprt->reestablish_timeout = 0; 146242d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 146340a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1464f9b2ee71STrond Myklebust } 1465f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1466a246b010SChuck Lever } 1467a246b010SChuck Lever 1468a519fc7aSTrond Myklebust /* 1469a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1470a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1471a519fc7aSTrond Myklebust */ 1472a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1473a519fc7aSTrond Myklebust { 1474a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1475a519fc7aSTrond Myklebust } 1476a519fc7aSTrond Myklebust 14779e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14786b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14796b26cc8cSChuck Lever { 14806b26cc8cSChuck Lever return PAGE_SIZE; 14816b26cc8cSChuck Lever } 14829e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 148344b98efdSRicardo Labiaga 14849903cd1cSChuck Lever /** 14859903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14869903cd1cSChuck Lever * @sk: socket whose state has changed 14879903cd1cSChuck Lever * 14889903cd1cSChuck Lever */ 14899903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1490a246b010SChuck Lever { 1491a246b010SChuck Lever struct rpc_xprt *xprt; 14920fdea1e8STrond Myklebust struct sock_xprt *transport; 1493a246b010SChuck Lever 1494f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1495a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1496a246b010SChuck Lever goto out; 14979903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1498669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1499a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1500a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1501669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1502669502ffSAndy Chittenden sk->sk_shutdown); 1503a246b010SChuck Lever 15040fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 150540b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1506a246b010SChuck Lever switch (sk->sk_state) { 1507a246b010SChuck Lever case TCP_ESTABLISHED: 1508f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1509a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15108b71798cSTrond Myklebust xprt->connect_cookie++; 15110fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15120fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 151351971139SChuck Lever 15143968a8a5SChuck Lever xprt->stat.connect_count++; 15153968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15163968a8a5SChuck Lever xprt->stat.connect_start; 15172a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1518a246b010SChuck Lever } 1519f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1520a246b010SChuck Lever break; 15213b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15223b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15237c1d71cfSTrond Myklebust xprt->connect_cookie++; 1524663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15253b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15264e857c58SPeter Zijlstra smp_mb__before_atomic(); 15273b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1528ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15294e857c58SPeter Zijlstra smp_mb__after_atomic(); 1530a246b010SChuck Lever break; 1531632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15323b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15337c1d71cfSTrond Myklebust xprt->connect_cookie++; 1534d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1535a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1536e9d47639SGustavo A. R. Silva /* fall through */ 1537663b8858STrond Myklebust case TCP_CLOSING: 1538663b8858STrond Myklebust /* 1539663b8858STrond Myklebust * If the server closed down the connection, make sure that 1540663b8858STrond Myklebust * we back off before reconnecting 1541663b8858STrond Myklebust */ 1542663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1543663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15443b948ae5STrond Myklebust break; 15453b948ae5STrond Myklebust case TCP_LAST_ACK: 1546670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15474e857c58SPeter Zijlstra smp_mb__before_atomic(); 15483b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15494e857c58SPeter Zijlstra smp_mb__after_atomic(); 15503b948ae5STrond Myklebust break; 15513b948ae5STrond Myklebust case TCP_CLOSE: 15520fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15530fdea1e8STrond Myklebust &transport->sock_state)) 15540fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15559b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15569b30889cSTrond Myklebust /* Trigger the socket release */ 15579b30889cSTrond Myklebust xs_tcp_force_close(xprt); 1558a246b010SChuck Lever } 1559a246b010SChuck Lever out: 1560f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1561a246b010SChuck Lever } 1562a246b010SChuck Lever 15631f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15641f0fa154SIlpo Järvinen { 156513331a55STrond Myklebust struct socket_wq *wq; 15661f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15671f0fa154SIlpo Järvinen 156813331a55STrond Myklebust if (!sk->sk_socket) 15691f0fa154SIlpo Järvinen return; 157013331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15711f0fa154SIlpo Järvinen 15721f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15731f0fa154SIlpo Järvinen return; 157413331a55STrond Myklebust rcu_read_lock(); 157513331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 157613331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 157713331a55STrond Myklebust goto out; 15781f0fa154SIlpo Järvinen 1579c544577dSTrond Myklebust if (xprt_write_space(xprt)) 1580c544577dSTrond Myklebust sk->sk_write_pending--; 158113331a55STrond Myklebust out: 158213331a55STrond Myklebust rcu_read_unlock(); 15831f0fa154SIlpo Järvinen } 15841f0fa154SIlpo Järvinen 15852a9e1cfaSTrond Myklebust /** 1586c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1587c7b2cae8SChuck Lever * becomes available 15889903cd1cSChuck Lever * @sk: socket whose state has changed 15899903cd1cSChuck Lever * 1590a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1591a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1592c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1593a246b010SChuck Lever * with a bunch of small requests. 1594a246b010SChuck Lever */ 1595c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1596a246b010SChuck Lever { 1597f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1598c7b2cae8SChuck Lever 1599c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16001f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16011f0fa154SIlpo Järvinen xs_write_space(sk); 1602c7b2cae8SChuck Lever 1603f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1604c7b2cae8SChuck Lever } 1605c7b2cae8SChuck Lever 1606c7b2cae8SChuck Lever /** 1607c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1608c7b2cae8SChuck Lever * becomes available 1609c7b2cae8SChuck Lever * @sk: socket whose state has changed 1610c7b2cae8SChuck Lever * 1611c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1612c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1613c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1614c7b2cae8SChuck Lever * with a bunch of small requests. 1615c7b2cae8SChuck Lever */ 1616c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1617c7b2cae8SChuck Lever { 1618f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1619c7b2cae8SChuck Lever 1620c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 162164dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16221f0fa154SIlpo Järvinen xs_write_space(sk); 1623c7b2cae8SChuck Lever 1624f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1625a246b010SChuck Lever } 1626a246b010SChuck Lever 1627470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1628a246b010SChuck Lever { 1629ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1630ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1631a246b010SChuck Lever 16327c6e066eSChuck Lever if (transport->rcvsize) { 1633a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16347c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1635a246b010SChuck Lever } 16367c6e066eSChuck Lever if (transport->sndsize) { 1637a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16387c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1639a246b010SChuck Lever sk->sk_write_space(sk); 1640a246b010SChuck Lever } 1641a246b010SChuck Lever } 1642a246b010SChuck Lever 164343118c29SChuck Lever /** 1644470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 164543118c29SChuck Lever * @xprt: generic transport 1646470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1647470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 164843118c29SChuck Lever * 1649470056c2SChuck Lever * Set socket send and receive buffer size limits. 165043118c29SChuck Lever */ 1651470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 165243118c29SChuck Lever { 16537c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16547c6e066eSChuck Lever 16557c6e066eSChuck Lever transport->sndsize = 0; 1656470056c2SChuck Lever if (sndsize) 16577c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16587c6e066eSChuck Lever transport->rcvsize = 0; 1659470056c2SChuck Lever if (rcvsize) 16607c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1661470056c2SChuck Lever 1662470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 166343118c29SChuck Lever } 166443118c29SChuck Lever 166546c0ee8bSChuck Lever /** 166646c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1667acf0a39fSChuck Lever * @xprt: controlling transport 166846c0ee8bSChuck Lever * @task: task that timed out 166946c0ee8bSChuck Lever * 167046c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 167146c0ee8bSChuck Lever */ 16726a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 167346c0ee8bSChuck Lever { 1674b977b644SChuck Lever spin_lock_bh(&xprt->transport_lock); 16756a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1676b977b644SChuck Lever spin_unlock_bh(&xprt->transport_lock); 167746c0ee8bSChuck Lever } 167846c0ee8bSChuck Lever 1679826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1680b85d8806SChuck Lever { 1681826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1682826799e6SJ. Bruce Fields unsigned short range; 1683826799e6SJ. Bruce Fields unsigned short rand; 1684826799e6SJ. Bruce Fields 1685826799e6SJ. Bruce Fields if (max < min) 1686826799e6SJ. Bruce Fields return -EADDRINUSE; 1687826799e6SJ. Bruce Fields range = max - min + 1; 1688826799e6SJ. Bruce Fields rand = (unsigned short) prandom_u32() % range; 1689826799e6SJ. Bruce Fields return rand + min; 1690b85d8806SChuck Lever } 1691b85d8806SChuck Lever 169292200412SChuck Lever /** 16934dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 16944dda9c8aSTrond Myklebust * @sock: socket 16954dda9c8aSTrond Myklebust * 16964dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 16974dda9c8aSTrond Myklebust * same port, and it must be called before binding. 16984dda9c8aSTrond Myklebust */ 16994dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17004dda9c8aSTrond Myklebust { 1701402e23b4STrond Myklebust int opt = 1; 17024dda9c8aSTrond Myklebust 1703402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1704402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17054dda9c8aSTrond Myklebust } 17064dda9c8aSTrond Myklebust 17074dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17084dda9c8aSTrond Myklebust { 17094dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17104dda9c8aSTrond Myklebust unsigned short port = 0; 17114dda9c8aSTrond Myklebust 17129b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 17134dda9c8aSTrond Myklebust goto out; 17144dda9c8aSTrond Myklebust switch (buf.ss_family) { 17154dda9c8aSTrond Myklebust case AF_INET6: 17164dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17174dda9c8aSTrond Myklebust break; 17184dda9c8aSTrond Myklebust case AF_INET: 17194dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17204dda9c8aSTrond Myklebust } 17214dda9c8aSTrond Myklebust out: 17224dda9c8aSTrond Myklebust return port; 17234dda9c8aSTrond Myklebust } 17244dda9c8aSTrond Myklebust 17254dda9c8aSTrond Myklebust /** 172692200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 172792200412SChuck Lever * @xprt: generic transport 172892200412SChuck Lever * @port: new port number 172992200412SChuck Lever * 173092200412SChuck Lever */ 173192200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 173292200412SChuck Lever { 173392200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1734c4efcb1dSChuck Lever 17359dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17369dc3b095SChuck Lever xs_update_peer_port(xprt); 173792200412SChuck Lever } 173892200412SChuck Lever 17394dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17404dda9c8aSTrond Myklebust { 17414dda9c8aSTrond Myklebust if (transport->srcport == 0) 17424dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17434dda9c8aSTrond Myklebust } 17444dda9c8aSTrond Myklebust 1745826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 174667a391d7STrond Myklebust { 1747826799e6SJ. Bruce Fields int port = transport->srcport; 174867a391d7STrond Myklebust 174967a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 175067a391d7STrond Myklebust port = xs_get_random_port(); 175167a391d7STrond Myklebust return port; 175267a391d7STrond Myklebust } 175367a391d7STrond Myklebust 1754baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 175567a391d7STrond Myklebust { 1756fbfffbd5SChuck Lever if (transport->srcport != 0) 1757fbfffbd5SChuck Lever transport->srcport = 0; 175867a391d7STrond Myklebust if (!transport->xprt.resvport) 175967a391d7STrond Myklebust return 0; 176067a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 176167a391d7STrond Myklebust return xprt_max_resvport; 176267a391d7STrond Myklebust return --port; 176367a391d7STrond Myklebust } 1764beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1765a246b010SChuck Lever { 1766beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 176767a391d7STrond Myklebust int err, nloop = 0; 1768826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 176967a391d7STrond Myklebust unsigned short last; 1770a246b010SChuck Lever 17710f7a622cSChris Perl /* 17720f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17730f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17740f7a622cSChris Perl * port selection happen implicitly when the socket is used 17750f7a622cSChris Perl * (for example at connect time). 17760f7a622cSChris Perl * 17770f7a622cSChris Perl * This ensures that we can continue to establish TCP 17780f7a622cSChris Perl * connections even when all local ephemeral ports are already 17790f7a622cSChris Perl * a part of some TCP connection. This makes no difference 17800f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 17810f7a622cSChris Perl * 17820f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17830f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17840f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17850f7a622cSChris Perl */ 1786826799e6SJ. Bruce Fields if (port <= 0) 1787826799e6SJ. Bruce Fields return port; 17880f7a622cSChris Perl 1789beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1790a246b010SChuck Lever do { 1791beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1792e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1793beb59b68SPavel Emelyanov transport->xprt.addrlen); 1794a246b010SChuck Lever if (err == 0) { 1795fbfffbd5SChuck Lever transport->srcport = port; 1796d3bc9a1dSFrank van Maarseveen break; 1797a246b010SChuck Lever } 179867a391d7STrond Myklebust last = port; 1799baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 180067a391d7STrond Myklebust if (port > last) 180167a391d7STrond Myklebust nloop++; 180267a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1803beb59b68SPavel Emelyanov 18044232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1805beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1806beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1807beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1808beb59b68SPavel Emelyanov else 1809beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1810beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18117dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1812a246b010SChuck Lever return err; 1813a246b010SChuck Lever } 1814a246b010SChuck Lever 1815176e21eeSChuck Lever /* 1816176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1817176e21eeSChuck Lever */ 1818176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1819176e21eeSChuck Lever { 1820fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1821176e21eeSChuck Lever } 1822176e21eeSChuck Lever 1823176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1824176e21eeSChuck Lever { 1825176e21eeSChuck Lever } 1826a246b010SChuck Lever 1827ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1828ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1829ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1830ed07536eSPeter Zijlstra 1831176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1832176e21eeSChuck Lever { 1833176e21eeSChuck Lever struct sock *sk = sock->sk; 1834176e21eeSChuck Lever 1835176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1836176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1837176e21eeSChuck Lever } 1838176e21eeSChuck Lever 18398945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1840ed07536eSPeter Zijlstra { 1841ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18428945ee5eSChuck Lever 18438945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 18448945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1845ed07536eSPeter Zijlstra } 18468945ee5eSChuck Lever 18478945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18488945ee5eSChuck Lever { 18498945ee5eSChuck Lever struct sock *sk = sock->sk; 18508945ee5eSChuck Lever 18518945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 18528945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1853ed07536eSPeter Zijlstra } 18546bc9638aSPavel Emelyanov 18556bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18566bc9638aSPavel Emelyanov { 1857fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18581b7a1819SWeston Andros Adamson return; 18591b7a1819SWeston Andros Adamson 18604232e863SChuck Lever switch (family) { 1861176e21eeSChuck Lever case AF_LOCAL: 1862176e21eeSChuck Lever xs_reclassify_socketu(sock); 1863176e21eeSChuck Lever break; 18644232e863SChuck Lever case AF_INET: 18656bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18664232e863SChuck Lever break; 18674232e863SChuck Lever case AF_INET6: 18686bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18694232e863SChuck Lever break; 18704232e863SChuck Lever } 18716bc9638aSPavel Emelyanov } 1872ed07536eSPeter Zijlstra #else 18736bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18746bc9638aSPavel Emelyanov { 18756bc9638aSPavel Emelyanov } 1876ed07536eSPeter Zijlstra #endif 1877ed07536eSPeter Zijlstra 187893dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 187993dc41bdSNeilBrown { 188093dc41bdSNeilBrown } 188193dc41bdSNeilBrown 18826bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18834dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18844dda9c8aSTrond Myklebust int protocol, bool reuseport) 188522f79326SPavel Emelyanov { 188622f79326SPavel Emelyanov struct socket *sock; 188722f79326SPavel Emelyanov int err; 188822f79326SPavel Emelyanov 18896bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 189022f79326SPavel Emelyanov if (err < 0) { 189122f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 189222f79326SPavel Emelyanov protocol, -err); 189322f79326SPavel Emelyanov goto out; 189422f79326SPavel Emelyanov } 18956bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 189622f79326SPavel Emelyanov 18974dda9c8aSTrond Myklebust if (reuseport) 18984dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 18994dda9c8aSTrond Myklebust 19004cea288aSBen Hutchings err = xs_bind(transport, sock); 19014cea288aSBen Hutchings if (err) { 190222f79326SPavel Emelyanov sock_release(sock); 190322f79326SPavel Emelyanov goto out; 190422f79326SPavel Emelyanov } 190522f79326SPavel Emelyanov 190622f79326SPavel Emelyanov return sock; 190722f79326SPavel Emelyanov out: 190822f79326SPavel Emelyanov return ERR_PTR(err); 190922f79326SPavel Emelyanov } 191022f79326SPavel Emelyanov 1911176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1912176e21eeSChuck Lever struct socket *sock) 1913176e21eeSChuck Lever { 1914176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1915176e21eeSChuck Lever xprt); 1916176e21eeSChuck Lever 1917176e21eeSChuck Lever if (!transport->inet) { 1918176e21eeSChuck Lever struct sock *sk = sock->sk; 1919176e21eeSChuck Lever 1920176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1921176e21eeSChuck Lever 1922176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1923176e21eeSChuck Lever 1924176e21eeSChuck Lever sk->sk_user_data = xprt; 1925a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1926176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1927b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 19282118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1929176e21eeSChuck Lever 1930176e21eeSChuck Lever xprt_clear_connected(xprt); 1931176e21eeSChuck Lever 1932176e21eeSChuck Lever /* Reset to new socket */ 1933176e21eeSChuck Lever transport->sock = sock; 1934176e21eeSChuck Lever transport->inet = sk; 1935176e21eeSChuck Lever 1936176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1937176e21eeSChuck Lever } 1938176e21eeSChuck Lever 1939ae053551STrond Myklebust xs_stream_start_connect(transport); 19406c7a64e5STrond Myklebust 1941176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1942176e21eeSChuck Lever } 1943176e21eeSChuck Lever 1944176e21eeSChuck Lever /** 1945176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1946176e21eeSChuck Lever * @transport: socket transport to connect 1947176e21eeSChuck Lever */ 1948dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1949176e21eeSChuck Lever { 1950176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1951176e21eeSChuck Lever struct socket *sock; 1952176e21eeSChuck Lever int status = -EIO; 1953176e21eeSChuck Lever 1954176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1955176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1956176e21eeSChuck Lever if (status < 0) { 1957176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1958176e21eeSChuck Lever "transport socket (%d).\n", -status); 1959176e21eeSChuck Lever goto out; 1960176e21eeSChuck Lever } 1961d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1962176e21eeSChuck Lever 1963176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1964176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1965176e21eeSChuck Lever 1966176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 196740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1968176e21eeSChuck Lever switch (status) { 1969176e21eeSChuck Lever case 0: 1970176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1971176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19723968a8a5SChuck Lever xprt->stat.connect_count++; 19733968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 19743968a8a5SChuck Lever xprt->stat.connect_start; 1975176e21eeSChuck Lever xprt_set_connected(xprt); 19763601c4a9STrond Myklebust case -ENOBUFS: 1977176e21eeSChuck Lever break; 1978176e21eeSChuck Lever case -ENOENT: 1979176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 1980176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1981176e21eeSChuck Lever break; 19824a20a988STrond Myklebust case -ECONNREFUSED: 19834a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 19844a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19854a20a988STrond Myklebust break; 1986176e21eeSChuck Lever default: 1987176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1988176e21eeSChuck Lever __func__, -status, 1989176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 1990176e21eeSChuck Lever } 1991176e21eeSChuck Lever 1992176e21eeSChuck Lever out: 1993176e21eeSChuck Lever xprt_clear_connecting(xprt); 1994176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 1995dc107402SJ. Bruce Fields return status; 1996dc107402SJ. Bruce Fields } 1997dc107402SJ. Bruce Fields 1998b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 1999dc107402SJ. Bruce Fields { 2000dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2001dc107402SJ. Bruce Fields int ret; 2002dc107402SJ. Bruce Fields 2003dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2004dc107402SJ. Bruce Fields /* 2005dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2006dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2007dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2008dc107402SJ. Bruce Fields * 2009dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2010dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2011dc107402SJ. Bruce Fields * connect. 2012dc107402SJ. Bruce Fields */ 2013dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2014dc107402SJ. Bruce Fields return; 2015dc107402SJ. Bruce Fields } 2016dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2017dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2018dc107402SJ. Bruce Fields msleep_interruptible(15000); 2019176e21eeSChuck Lever } 2020176e21eeSChuck Lever 20213c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2022d6e971d8SJeff Layton /* 2023d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2024d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2025d6e971d8SJeff Layton * races with xs_reset_transport. 2026d6e971d8SJeff Layton */ 2027a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2028a564b8f0SMel Gorman { 2029a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2030a564b8f0SMel Gorman xprt); 2031a564b8f0SMel Gorman 2032d6e971d8SJeff Layton /* 2033d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2034d6e971d8SJeff Layton * reconnecting process will get it for us. 2035d6e971d8SJeff Layton */ 2036d6e971d8SJeff Layton if (!transport->inet) 2037d6e971d8SJeff Layton return; 20388e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2039a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2040a564b8f0SMel Gorman } 2041a564b8f0SMel Gorman 2042a564b8f0SMel Gorman /** 2043d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2044a564b8f0SMel Gorman * @xprt: transport to tag 2045a564b8f0SMel Gorman * 20468e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20478e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2048a564b8f0SMel Gorman */ 2049d67fa4d8SJeff Layton static int 2050d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2051a564b8f0SMel Gorman { 2052d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2053a564b8f0SMel Gorman 2054d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2055d6e971d8SJeff Layton return 0; 2056d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2057d6e971d8SJeff Layton return -ERESTARTSYS; 2058d6e971d8SJeff Layton if (xs->inet) 2059d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2060d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 20618e228133SJeff Layton return 0; 2062a564b8f0SMel Gorman } 2063a564b8f0SMel Gorman 20648e228133SJeff Layton /** 2065d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20668e228133SJeff Layton * @xprt: transport to tag 20678e228133SJeff Layton * 20688e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 20698e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 20708e228133SJeff Layton */ 2071d67fa4d8SJeff Layton static void 2072d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 20738e228133SJeff Layton { 2074d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 20758e228133SJeff Layton 2076d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2077d6e971d8SJeff Layton return; 2078d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2079d6e971d8SJeff Layton return; 2080d6e971d8SJeff Layton if (xs->inet) 2081d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2082d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2083a564b8f0SMel Gorman } 2084a564b8f0SMel Gorman #else 2085a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2086a564b8f0SMel Gorman { 2087a564b8f0SMel Gorman } 2088d67fa4d8SJeff Layton 2089d67fa4d8SJeff Layton static int 2090d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2091d67fa4d8SJeff Layton { 2092d67fa4d8SJeff Layton return -EINVAL; 2093d67fa4d8SJeff Layton } 2094d67fa4d8SJeff Layton 2095d67fa4d8SJeff Layton static void 2096d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2097d67fa4d8SJeff Layton { 2098d67fa4d8SJeff Layton } 2099a564b8f0SMel Gorman #endif 2100a564b8f0SMel Gorman 210116be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2102a246b010SChuck Lever { 210316be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2104edb267a6SChuck Lever 2105ee0ac0c2SChuck Lever if (!transport->inet) { 2106b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2107b0d93ad5SChuck Lever 2108b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2109b0d93ad5SChuck Lever 21102a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21112a9e1cfaSTrond Myklebust 2112b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2113f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2114b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2115b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2116b0d93ad5SChuck Lever 2117b0d93ad5SChuck Lever xprt_set_connected(xprt); 2118b0d93ad5SChuck Lever 2119b0d93ad5SChuck Lever /* Reset to new socket */ 2120ee0ac0c2SChuck Lever transport->sock = sock; 2121ee0ac0c2SChuck Lever transport->inet = sk; 2122b0d93ad5SChuck Lever 2123a564b8f0SMel Gorman xs_set_memalloc(xprt); 2124a564b8f0SMel Gorman 2125b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2126b0d93ad5SChuck Lever } 2127470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 212802910177STrond Myklebust 212902910177STrond Myklebust xprt->stat.connect_start = jiffies; 213016be2d20SChuck Lever } 213116be2d20SChuck Lever 21328c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2133a246b010SChuck Lever { 2134a246b010SChuck Lever struct sock_xprt *transport = 2135a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2136a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2137d099b8afSColin Ian King struct socket *sock; 2138b65c0310SPavel Emelyanov int status = -EIO; 2139a246b010SChuck Lever 21408c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21414dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21424dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2143b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2144a246b010SChuck Lever goto out; 214568e220bdSChuck Lever 2146c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2147c740eff8SChuck Lever "%s (port %s)\n", xprt, 2148c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2149c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2150c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 215168e220bdSChuck Lever 215268e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 215340b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2154a246b010SChuck Lever status = 0; 2155b0d93ad5SChuck Lever out: 2156b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2157cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 21587d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2159b0d93ad5SChuck Lever } 2160b0d93ad5SChuck Lever 21614876cc77STrond Myklebust /** 21624876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21634876cc77STrond Myklebust * @xprt: transport 21644876cc77STrond Myklebust * 21654876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 21664876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 21674876cc77STrond Myklebust */ 21684876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 21694876cc77STrond Myklebust { 21704876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21714876cc77STrond Myklebust struct socket *sock = transport->sock; 21729b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 21734876cc77STrond Myklebust 21744876cc77STrond Myklebust if (sock == NULL) 21754876cc77STrond Myklebust return; 21769b30889cSTrond Myklebust switch (skst) { 21779b30889cSTrond Myklebust default: 21784876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 21794876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 21809b30889cSTrond Myklebust break; 21819b30889cSTrond Myklebust case TCP_CLOSE: 21829b30889cSTrond Myklebust case TCP_TIME_WAIT: 21834876cc77STrond Myklebust xs_reset_transport(transport); 21844876cc77STrond Myklebust } 21859b30889cSTrond Myklebust } 21864876cc77STrond Myklebust 21878d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 21888d1b8c62STrond Myklebust struct socket *sock) 2189b0d93ad5SChuck Lever { 219016be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 21917196dbb0STrond Myklebust unsigned int keepidle; 21927196dbb0STrond Myklebust unsigned int keepcnt; 21937f260e85STrond Myklebust unsigned int opt_on = 1; 2194775f06abSTrond Myklebust unsigned int timeo; 21957f260e85STrond Myklebust 21967196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 21977196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 21987196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 21997196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22007196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22017196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22027196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22037f260e85STrond Myklebust 22047f260e85STrond Myklebust /* TCP Keepalive options */ 22057f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22067f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22077f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22087f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22097f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22107f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22117f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22127f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2213b0d93ad5SChuck Lever 22148d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 22158d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 22168d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 22178d1b8c62STrond Myklebust } 22188d1b8c62STrond Myklebust 22197196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22207196dbb0STrond Myklebust unsigned long connect_timeout, 22217196dbb0STrond Myklebust unsigned long reconnect_timeout) 22227196dbb0STrond Myklebust { 22237196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22247196dbb0STrond Myklebust struct rpc_timeout to; 22257196dbb0STrond Myklebust unsigned long initval; 22267196dbb0STrond Myklebust 22277196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22287196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22297196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22307196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 22317196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22327196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 22337196dbb0STrond Myklebust /* Arbitrary lower limit */ 22347196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 22357196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 22367196dbb0STrond Myklebust to.to_initval = initval; 22377196dbb0STrond Myklebust to.to_maxval = initval; 22387196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 22397196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 22407196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 22417196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 22427196dbb0STrond Myklebust } 22437196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22447196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22457196dbb0STrond Myklebust } 22467196dbb0STrond Myklebust 22478d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 22488d1b8c62STrond Myklebust { 22498d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22508d1b8c62STrond Myklebust int ret = -ENOTCONN; 22518d1b8c62STrond Myklebust 22528d1b8c62STrond Myklebust if (!transport->inet) { 22538d1b8c62STrond Myklebust struct sock *sk = sock->sk; 22548d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 22558d1b8c62STrond Myklebust 2256d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2257d88e4d82SNeilBrown * connections such as NFS mounts. 2258d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2259d88e4d82SNeilBrown * Individual applications, which have specific 2260d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2261d88e4d82SNeilBrown * MAY override this as appropriate. 2262d88e4d82SNeilBrown */ 2263d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2264d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2265d88e4d82SNeilBrown 22668d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2267775f06abSTrond Myklebust 2268b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2269b0d93ad5SChuck Lever 22702a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 22712a9e1cfaSTrond Myklebust 2272b0d93ad5SChuck Lever sk->sk_user_data = xprt; 22735157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2274b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2275b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2276b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 22772118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 22783167e12cSChuck Lever 22793167e12cSChuck Lever /* socket options */ 22803167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 22813167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2282b0d93ad5SChuck Lever 2283b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2284b0d93ad5SChuck Lever 2285b0d93ad5SChuck Lever /* Reset to new socket */ 2286ee0ac0c2SChuck Lever transport->sock = sock; 2287ee0ac0c2SChuck Lever transport->inet = sk; 2288b0d93ad5SChuck Lever 2289b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2290b0d93ad5SChuck Lever } 2291b0d93ad5SChuck Lever 229201d37c42STrond Myklebust if (!xprt_bound(xprt)) 2293fe19a96bSTrond Myklebust goto out; 229401d37c42STrond Myklebust 2295a564b8f0SMel Gorman xs_set_memalloc(xprt); 2296a564b8f0SMel Gorman 2297ae053551STrond Myklebust xs_stream_start_connect(transport); 2298e1806c7bSTrond Myklebust 2299b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23000fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2301fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2302fe19a96bSTrond Myklebust switch (ret) { 2303fe19a96bSTrond Myklebust case 0: 23044dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2305e9d47639SGustavo A. R. Silva /* fall through */ 2306fe19a96bSTrond Myklebust case -EINPROGRESS: 2307fe19a96bSTrond Myklebust /* SYN_SENT! */ 2308fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2309fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 23101f4c17a0STrond Myklebust break; 23111f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 23121f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 23131f4c17a0STrond Myklebust transport->srcport = 0; 2314fe19a96bSTrond Myklebust } 2315fe19a96bSTrond Myklebust out: 2316fe19a96bSTrond Myklebust return ret; 231716be2d20SChuck Lever } 231816be2d20SChuck Lever 231916be2d20SChuck Lever /** 2320b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2321acf0a39fSChuck Lever * @work: queued work item 232216be2d20SChuck Lever * 232316be2d20SChuck Lever * Invoked by a work queue tasklet. 232416be2d20SChuck Lever */ 2325cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 232616be2d20SChuck Lever { 2327cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2328cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 232916be2d20SChuck Lever struct socket *sock = transport->sock; 2330a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2331b61d59ffSTrond Myklebust int status = -EIO; 233216be2d20SChuck Lever 233316be2d20SChuck Lever if (!sock) { 2334cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 23354dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 23364dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2337b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2338b61d59ffSTrond Myklebust status = PTR_ERR(sock); 233916be2d20SChuck Lever goto out; 234016be2d20SChuck Lever } 23417d1e8255STrond Myklebust } 23427d1e8255STrond Myklebust 2343c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2344c740eff8SChuck Lever "%s (port %s)\n", xprt, 2345c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2346c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2347c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 234816be2d20SChuck Lever 234916be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 235040b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2351a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 235246121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 235346121cf7SChuck Lever sock->sk->sk_state); 2354a246b010SChuck Lever switch (status) { 2355f75e6745STrond Myklebust default: 2356f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2357f75e6745STrond Myklebust __func__, status); 2358e9d47639SGustavo A. R. Silva /* fall through */ 2359f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2360f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2361f75e6745STrond Myklebust * and retry 2362f75e6745STrond Myklebust */ 2363a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 236488b5ed73STrond Myklebust break; 23652a491991STrond Myklebust case 0: 2366a246b010SChuck Lever case -EINPROGRESS: 2367a246b010SChuck Lever case -EALREADY: 2368718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 23697d1e8255STrond Myklebust return; 23709fcfe0c8STrond Myklebust case -EINVAL: 23719fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 23729fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 23739fcfe0c8STrond Myklebust */ 23743ed5e2a2STrond Myklebust case -ECONNREFUSED: 23753ed5e2a2STrond Myklebust case -ECONNRESET: 2376eb5b46faSTrond Myklebust case -ENETDOWN: 23773ed5e2a2STrond Myklebust case -ENETUNREACH: 23784ba161a7STrond Myklebust case -EHOSTUNREACH: 23793913c78cSTrond Myklebust case -EADDRINUSE: 23803601c4a9STrond Myklebust case -ENOBUFS: 23816ea44adcSNeilBrown /* 23826ea44adcSNeilBrown * xs_tcp_force_close() wakes tasks with -EIO. 23836ea44adcSNeilBrown * We need to wake them first to ensure the 23846ea44adcSNeilBrown * correct error code. 23856ea44adcSNeilBrown */ 23866ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 23874efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 23889fcfe0c8STrond Myklebust goto out; 23898a2cec29STrond Myklebust } 23902a491991STrond Myklebust status = -EAGAIN; 2391a246b010SChuck Lever out: 23922226feb6SChuck Lever xprt_clear_connecting(xprt); 2393cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 23947d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2395a246b010SChuck Lever } 2396a246b010SChuck Lever 239702910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 239802910177STrond Myklebust { 239902910177STrond Myklebust unsigned long start, now = jiffies; 240002910177STrond Myklebust 240102910177STrond Myklebust start = xprt->stat.connect_start + xprt->reestablish_timeout; 240202910177STrond Myklebust if (time_after(start, now)) 240302910177STrond Myklebust return start - now; 240402910177STrond Myklebust return 0; 240502910177STrond Myklebust } 240602910177STrond Myklebust 24073851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt) 24083851f1cdSTrond Myklebust { 24093851f1cdSTrond Myklebust xprt->reestablish_timeout <<= 1; 24103851f1cdSTrond Myklebust if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 24113851f1cdSTrond Myklebust xprt->reestablish_timeout = xprt->max_reconnect_timeout; 24123851f1cdSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 24133851f1cdSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 24143851f1cdSTrond Myklebust } 24153851f1cdSTrond Myklebust 241668e220bdSChuck Lever /** 24179903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 24181b092092STrond Myklebust * @xprt: pointer to transport structure 24199903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 24209903cd1cSChuck Lever * 24219903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 242203bf4b70SChuck Lever * 242303bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 242403bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 242503bf4b70SChuck Lever * socket on a privileged port. 242603bf4b70SChuck Lever * 242703bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 242803bf4b70SChuck Lever * retry floods (hard mounts). 24299903cd1cSChuck Lever */ 24301b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2431a246b010SChuck Lever { 2432ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 243302910177STrond Myklebust unsigned long delay = 0; 2434a246b010SChuck Lever 2435718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2436718ba5b8STrond Myklebust 243799b1a4c3STrond Myklebust if (transport->sock != NULL) { 243846121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 243946121cf7SChuck Lever "seconds\n", 244003bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 244199b1a4c3STrond Myklebust 244299b1a4c3STrond Myklebust /* Start by resetting any existing state */ 244399b1a4c3STrond Myklebust xs_reset_transport(transport); 244499b1a4c3STrond Myklebust 244502910177STrond Myklebust delay = xs_reconnect_delay(xprt); 24463851f1cdSTrond Myklebust xs_reconnect_backoff(xprt); 244702910177STrond Myklebust 244802910177STrond Myklebust } else 24499903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 245002910177STrond Myklebust 245140a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 245202910177STrond Myklebust &transport->connect_worker, 245302910177STrond Myklebust delay); 2454a246b010SChuck Lever } 2455a246b010SChuck Lever 2456262ca07dSChuck Lever /** 2457176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2458176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2459176e21eeSChuck Lever * @seq: output file 2460176e21eeSChuck Lever * 2461176e21eeSChuck Lever */ 2462176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2463176e21eeSChuck Lever { 2464176e21eeSChuck Lever long idle_time = 0; 2465176e21eeSChuck Lever 2466176e21eeSChuck Lever if (xprt_connected(xprt)) 2467176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2468176e21eeSChuck Lever 2469176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 247015a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2471176e21eeSChuck Lever xprt->stat.bind_count, 2472176e21eeSChuck Lever xprt->stat.connect_count, 24738440a886SChuck Lever xprt->stat.connect_time / HZ, 2474176e21eeSChuck Lever idle_time, 2475176e21eeSChuck Lever xprt->stat.sends, 2476176e21eeSChuck Lever xprt->stat.recvs, 2477176e21eeSChuck Lever xprt->stat.bad_xids, 2478176e21eeSChuck Lever xprt->stat.req_u, 247915a45206SAndy Adamson xprt->stat.bklog_u, 248015a45206SAndy Adamson xprt->stat.max_slots, 248115a45206SAndy Adamson xprt->stat.sending_u, 248215a45206SAndy Adamson xprt->stat.pending_u); 2483176e21eeSChuck Lever } 2484176e21eeSChuck Lever 2485176e21eeSChuck Lever /** 2486262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2487262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2488262ca07dSChuck Lever * @seq: output file 2489262ca07dSChuck Lever * 2490262ca07dSChuck Lever */ 2491262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2492262ca07dSChuck Lever { 2493c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2494c8475461SChuck Lever 249515a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 249615a45206SAndy Adamson "%lu %llu %llu\n", 2497fbfffbd5SChuck Lever transport->srcport, 2498262ca07dSChuck Lever xprt->stat.bind_count, 2499262ca07dSChuck Lever xprt->stat.sends, 2500262ca07dSChuck Lever xprt->stat.recvs, 2501262ca07dSChuck Lever xprt->stat.bad_xids, 2502262ca07dSChuck Lever xprt->stat.req_u, 250315a45206SAndy Adamson xprt->stat.bklog_u, 250415a45206SAndy Adamson xprt->stat.max_slots, 250515a45206SAndy Adamson xprt->stat.sending_u, 250615a45206SAndy Adamson xprt->stat.pending_u); 2507262ca07dSChuck Lever } 2508262ca07dSChuck Lever 2509262ca07dSChuck Lever /** 2510262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2511262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2512262ca07dSChuck Lever * @seq: output file 2513262ca07dSChuck Lever * 2514262ca07dSChuck Lever */ 2515262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2516262ca07dSChuck Lever { 2517c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2518262ca07dSChuck Lever long idle_time = 0; 2519262ca07dSChuck Lever 2520262ca07dSChuck Lever if (xprt_connected(xprt)) 2521262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2522262ca07dSChuck Lever 252315a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 252415a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2525fbfffbd5SChuck Lever transport->srcport, 2526262ca07dSChuck Lever xprt->stat.bind_count, 2527262ca07dSChuck Lever xprt->stat.connect_count, 25288440a886SChuck Lever xprt->stat.connect_time / HZ, 2529262ca07dSChuck Lever idle_time, 2530262ca07dSChuck Lever xprt->stat.sends, 2531262ca07dSChuck Lever xprt->stat.recvs, 2532262ca07dSChuck Lever xprt->stat.bad_xids, 2533262ca07dSChuck Lever xprt->stat.req_u, 253415a45206SAndy Adamson xprt->stat.bklog_u, 253515a45206SAndy Adamson xprt->stat.max_slots, 253615a45206SAndy Adamson xprt->stat.sending_u, 253715a45206SAndy Adamson xprt->stat.pending_u); 2538262ca07dSChuck Lever } 2539262ca07dSChuck Lever 25404cfc7e60SRahul Iyer /* 25414cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 25424cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 25434cfc7e60SRahul Iyer * to use the server side send routines. 25444cfc7e60SRahul Iyer */ 25455fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 25464cfc7e60SRahul Iyer { 25475fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 25485fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 25494cfc7e60SRahul Iyer struct page *page; 25504cfc7e60SRahul Iyer struct rpc_buffer *buf; 25514cfc7e60SRahul Iyer 25525fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 25535fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 25545fe6eaa1SChuck Lever size); 25555fe6eaa1SChuck Lever return -EINVAL; 25565fe6eaa1SChuck Lever } 25574cfc7e60SRahul Iyer 2558b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 25594cfc7e60SRahul Iyer if (!page) 25605fe6eaa1SChuck Lever return -ENOMEM; 25614cfc7e60SRahul Iyer 25624cfc7e60SRahul Iyer buf = page_address(page); 25634cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 25644cfc7e60SRahul Iyer 25655fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 256618e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 25675fe6eaa1SChuck Lever return 0; 25684cfc7e60SRahul Iyer } 25694cfc7e60SRahul Iyer 25704cfc7e60SRahul Iyer /* 25714cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 25724cfc7e60SRahul Iyer */ 25733435c74aSChuck Lever static void bc_free(struct rpc_task *task) 25744cfc7e60SRahul Iyer { 25753435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 25764cfc7e60SRahul Iyer struct rpc_buffer *buf; 25774cfc7e60SRahul Iyer 25784cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 25794cfc7e60SRahul Iyer free_page((unsigned long)buf); 25804cfc7e60SRahul Iyer } 25814cfc7e60SRahul Iyer 25824cfc7e60SRahul Iyer /* 25834cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 25844cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 25854cfc7e60SRahul Iyer */ 25864cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 25874cfc7e60SRahul Iyer { 25884cfc7e60SRahul Iyer int len; 25894cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 25904cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 25914cfc7e60SRahul Iyer struct sock_xprt *transport = 25924cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 25934cfc7e60SRahul Iyer struct socket *sock = transport->sock; 25944cfc7e60SRahul Iyer unsigned long headoff; 25954cfc7e60SRahul Iyer unsigned long tailoff; 25964cfc7e60SRahul Iyer 259761677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 25984cfc7e60SRahul Iyer 25994cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26004cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 26014cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 26024cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 26034cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 26044cfc7e60SRahul Iyer 26054cfc7e60SRahul Iyer if (len != xbufp->len) { 26064cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 26074cfc7e60SRahul Iyer len = -EAGAIN; 26084cfc7e60SRahul Iyer } 26094cfc7e60SRahul Iyer 26104cfc7e60SRahul Iyer return len; 26114cfc7e60SRahul Iyer } 26124cfc7e60SRahul Iyer 26134cfc7e60SRahul Iyer /* 26144cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 26154cfc7e60SRahul Iyer */ 2616adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 26174cfc7e60SRahul Iyer { 26184cfc7e60SRahul Iyer struct svc_xprt *xprt; 26197fc56136SAndrzej Hajda int len; 26204cfc7e60SRahul Iyer 26214cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 26224cfc7e60SRahul Iyer /* 26234cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 26244cfc7e60SRahul Iyer */ 26254cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 26264cfc7e60SRahul Iyer 26274cfc7e60SRahul Iyer /* 26284cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 26294cfc7e60SRahul Iyer * with the fore channel 26304cfc7e60SRahul Iyer */ 2631c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 26324cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 26334cfc7e60SRahul Iyer len = -ENOTCONN; 26344cfc7e60SRahul Iyer else 26354cfc7e60SRahul Iyer len = bc_sendto(req); 26364cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 26374cfc7e60SRahul Iyer 26384cfc7e60SRahul Iyer if (len > 0) 26394cfc7e60SRahul Iyer len = 0; 26404cfc7e60SRahul Iyer 26414cfc7e60SRahul Iyer return len; 26424cfc7e60SRahul Iyer } 26434cfc7e60SRahul Iyer 26444cfc7e60SRahul Iyer /* 26454cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 26464cfc7e60SRahul Iyer */ 26474cfc7e60SRahul Iyer 26484cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 26494cfc7e60SRahul Iyer { 26504cfc7e60SRahul Iyer } 26514cfc7e60SRahul Iyer 26524cfc7e60SRahul Iyer /* 26534cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 26544cfc7e60SRahul Iyer * initiated, we do nothing 26554cfc7e60SRahul Iyer */ 26564cfc7e60SRahul Iyer 26574cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 26584cfc7e60SRahul Iyer { 265947f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 266047f72efaSKinglong Mee 266147f72efaSKinglong Mee xs_xprt_free(xprt); 266247f72efaSKinglong Mee module_put(THIS_MODULE); 26634cfc7e60SRahul Iyer } 26644cfc7e60SRahul Iyer 2665d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2666176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 26674cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2668f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2669a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2670176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2671176e21eeSChuck Lever .set_port = xs_local_set_port, 2672dc107402SJ. Bruce Fields .connect = xs_local_connect, 2673176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2674176e21eeSChuck Lever .buf_free = rpc_free, 2675550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 2676176e21eeSChuck Lever .send_request = xs_local_send_request, 2677176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2678176e21eeSChuck Lever .close = xs_close, 2679a1311d87STrond Myklebust .destroy = xs_destroy, 2680176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2681d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2682d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2683176e21eeSChuck Lever }; 2684176e21eeSChuck Lever 2685d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 268643118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 268712a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 268849e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2689f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2690a9cde23aSChuck Lever .free_slot = xprt_free_slot, 269145160d62SChuck Lever .rpcbind = rpcb_getport_async, 269292200412SChuck Lever .set_port = xs_set_port, 26939903cd1cSChuck Lever .connect = xs_connect, 269402107148SChuck Lever .buf_alloc = rpc_malloc, 269502107148SChuck Lever .buf_free = rpc_free, 2696262965f5SChuck Lever .send_request = xs_udp_send_request, 2697fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 269846c0ee8bSChuck Lever .timer = xs_udp_timer, 2699a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2700262965f5SChuck Lever .close = xs_close, 2701262965f5SChuck Lever .destroy = xs_destroy, 2702262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2703d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2704d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27054a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2706262965f5SChuck Lever }; 2707262965f5SChuck Lever 2708d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 270912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 27104cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 271136bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 2712a9cde23aSChuck Lever .free_slot = xprt_free_slot, 271345160d62SChuck Lever .rpcbind = rpcb_getport_async, 271492200412SChuck Lever .set_port = xs_set_port, 27150b9e7943STrond Myklebust .connect = xs_connect, 271602107148SChuck Lever .buf_alloc = rpc_malloc, 271702107148SChuck Lever .buf_free = rpc_free, 2718277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 2719262965f5SChuck Lever .send_request = xs_tcp_send_request, 2720fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2721c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 27229903cd1cSChuck Lever .destroy = xs_destroy, 27237196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2724262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2725d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2726d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27274a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 272842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 272942e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 27306b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 273142e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 273242e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 273342e5c3e2SChuck Lever #endif 2734a246b010SChuck Lever }; 2735a246b010SChuck Lever 27364cfc7e60SRahul Iyer /* 27374cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 27384cfc7e60SRahul Iyer */ 27394cfc7e60SRahul Iyer 2740d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 27414cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 27424cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 274384e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 2744a9cde23aSChuck Lever .free_slot = xprt_free_slot, 27454cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 27464cfc7e60SRahul Iyer .buf_free = bc_free, 27474cfc7e60SRahul Iyer .send_request = bc_send_request, 27484cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 27494cfc7e60SRahul Iyer .close = bc_close, 27504cfc7e60SRahul Iyer .destroy = bc_destroy, 27514cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2752d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2753d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27544a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 27554cfc7e60SRahul Iyer }; 27564cfc7e60SRahul Iyer 275792476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 275892476850SChuck Lever { 275992476850SChuck Lever static const struct sockaddr_in sin = { 276092476850SChuck Lever .sin_family = AF_INET, 276192476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 276292476850SChuck Lever }; 276392476850SChuck Lever static const struct sockaddr_in6 sin6 = { 276492476850SChuck Lever .sin6_family = AF_INET6, 276592476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 276692476850SChuck Lever }; 276792476850SChuck Lever 276892476850SChuck Lever switch (family) { 2769176e21eeSChuck Lever case AF_LOCAL: 2770176e21eeSChuck Lever break; 277192476850SChuck Lever case AF_INET: 277292476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 277392476850SChuck Lever break; 277492476850SChuck Lever case AF_INET6: 277592476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 277692476850SChuck Lever break; 277792476850SChuck Lever default: 277892476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 277992476850SChuck Lever return -EAFNOSUPPORT; 278092476850SChuck Lever } 278192476850SChuck Lever return 0; 278292476850SChuck Lever } 278392476850SChuck Lever 27843c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2785d9ba131dSTrond Myklebust unsigned int slot_table_size, 2786d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2787c8541ecdSChuck Lever { 2788c8541ecdSChuck Lever struct rpc_xprt *xprt; 2789ffc2e518SChuck Lever struct sock_xprt *new; 2790c8541ecdSChuck Lever 279196802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2792c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2793c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2794c8541ecdSChuck Lever } 2795c8541ecdSChuck Lever 2796d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2797d9ba131dSTrond Myklebust max_slot_table_size); 2798bd1722d4SPavel Emelyanov if (xprt == NULL) { 279946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 280046121cf7SChuck Lever "rpc_xprt\n"); 2801c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2802c8541ecdSChuck Lever } 2803c8541ecdSChuck Lever 2804bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2805edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 280696802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 280796802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2808d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2809fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 281092476850SChuck Lever else { 281192476850SChuck Lever int err; 281292476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 281392476850SChuck Lever (struct sockaddr *)&new->srcaddr); 28142aa13531SStanislav Kinsbursky if (err != 0) { 28152aa13531SStanislav Kinsbursky xprt_free(xprt); 281692476850SChuck Lever return ERR_PTR(err); 281792476850SChuck Lever } 28182aa13531SStanislav Kinsbursky } 2819c8541ecdSChuck Lever 2820c8541ecdSChuck Lever return xprt; 2821c8541ecdSChuck Lever } 2822c8541ecdSChuck Lever 2823176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2824176e21eeSChuck Lever .to_initval = 10 * HZ, 2825176e21eeSChuck Lever .to_maxval = 10 * HZ, 2826176e21eeSChuck Lever .to_retries = 2, 2827176e21eeSChuck Lever }; 2828176e21eeSChuck Lever 2829176e21eeSChuck Lever /** 2830176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2831176e21eeSChuck Lever * @args: rpc transport creation arguments 2832176e21eeSChuck Lever * 2833176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2834176e21eeSChuck Lever */ 2835176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2836176e21eeSChuck Lever { 2837176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2838176e21eeSChuck Lever struct sock_xprt *transport; 2839176e21eeSChuck Lever struct rpc_xprt *xprt; 2840176e21eeSChuck Lever struct rpc_xprt *ret; 2841176e21eeSChuck Lever 2842d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2843d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2844176e21eeSChuck Lever if (IS_ERR(xprt)) 2845176e21eeSChuck Lever return xprt; 2846176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2847176e21eeSChuck Lever 2848176e21eeSChuck Lever xprt->prot = 0; 2849176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2850176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2851176e21eeSChuck Lever 2852176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2853176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2854176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2855176e21eeSChuck Lever 2856176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2857176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2858176e21eeSChuck Lever 2859550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2860550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 286193dc41bdSNeilBrown 2862176e21eeSChuck Lever switch (sun->sun_family) { 2863176e21eeSChuck Lever case AF_LOCAL: 2864176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2865176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2866176e21eeSChuck Lever sun->sun_path); 2867176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2868176e21eeSChuck Lever goto out_err; 2869176e21eeSChuck Lever } 2870176e21eeSChuck Lever xprt_set_bound(xprt); 2871176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 28727073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 28737073ea87SJ. Bruce Fields if (ret) 28747073ea87SJ. Bruce Fields goto out_err; 2875176e21eeSChuck Lever break; 2876176e21eeSChuck Lever default: 2877176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2878176e21eeSChuck Lever goto out_err; 2879176e21eeSChuck Lever } 2880176e21eeSChuck Lever 2881176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2882176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2883176e21eeSChuck Lever 2884176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2885176e21eeSChuck Lever return xprt; 2886176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2887176e21eeSChuck Lever out_err: 2888315f3812SKinglong Mee xs_xprt_free(xprt); 2889176e21eeSChuck Lever return ret; 2890176e21eeSChuck Lever } 2891176e21eeSChuck Lever 28922881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 28932881ae74STrond Myklebust .to_initval = 5 * HZ, 28942881ae74STrond Myklebust .to_maxval = 30 * HZ, 28952881ae74STrond Myklebust .to_increment = 5 * HZ, 28962881ae74STrond Myklebust .to_retries = 5, 28972881ae74STrond Myklebust }; 28982881ae74STrond Myklebust 28999903cd1cSChuck Lever /** 29009903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 290196802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29029903cd1cSChuck Lever * 29039903cd1cSChuck Lever */ 2904483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2905a246b010SChuck Lever { 29068f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2907c8541ecdSChuck Lever struct rpc_xprt *xprt; 2908c8475461SChuck Lever struct sock_xprt *transport; 29090a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2910a246b010SChuck Lever 2911d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2912d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2913c8541ecdSChuck Lever if (IS_ERR(xprt)) 2914c8541ecdSChuck Lever return xprt; 2915c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2916a246b010SChuck Lever 2917ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2918808012fbSChuck Lever xprt->tsh_size = 0; 2919a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2920a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2921a246b010SChuck Lever 292203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 292303bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 292403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2925a246b010SChuck Lever 2926262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2927a246b010SChuck Lever 2928ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2929a246b010SChuck Lever 2930f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 2931edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2932edc1b01cSTrond Myklebust 29338f9d5b1aSChuck Lever switch (addr->sa_family) { 29348f9d5b1aSChuck Lever case AF_INET: 29358f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 29368f9d5b1aSChuck Lever xprt_set_bound(xprt); 29378f9d5b1aSChuck Lever 29389dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 29398f9d5b1aSChuck Lever break; 29408f9d5b1aSChuck Lever case AF_INET6: 29418f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 29428f9d5b1aSChuck Lever xprt_set_bound(xprt); 29438f9d5b1aSChuck Lever 29449dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 29458f9d5b1aSChuck Lever break; 29468f9d5b1aSChuck Lever default: 29470a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 29480a68b0beSJ. Bruce Fields goto out_err; 29498f9d5b1aSChuck Lever } 29508f9d5b1aSChuck Lever 2951c740eff8SChuck Lever if (xprt_bound(xprt)) 2952c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2953c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2954c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2955c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2956c740eff8SChuck Lever else 2957c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2958c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2959c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2960edb267a6SChuck Lever 2961bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2962c8541ecdSChuck Lever return xprt; 29630a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 29640a68b0beSJ. Bruce Fields out_err: 2965315f3812SKinglong Mee xs_xprt_free(xprt); 29660a68b0beSJ. Bruce Fields return ret; 2967a246b010SChuck Lever } 2968a246b010SChuck Lever 29692881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 29702881ae74STrond Myklebust .to_initval = 60 * HZ, 29712881ae74STrond Myklebust .to_maxval = 60 * HZ, 29722881ae74STrond Myklebust .to_retries = 2, 29732881ae74STrond Myklebust }; 29742881ae74STrond Myklebust 29759903cd1cSChuck Lever /** 29769903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 297796802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29789903cd1cSChuck Lever * 29799903cd1cSChuck Lever */ 2980483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2981a246b010SChuck Lever { 29828f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2983c8541ecdSChuck Lever struct rpc_xprt *xprt; 2984c8475461SChuck Lever struct sock_xprt *transport; 29850a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2986b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2987b7993cebSTrond Myklebust 2988b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2989b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2990a246b010SChuck Lever 2991d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2992b7993cebSTrond Myklebust max_slot_table_size); 2993c8541ecdSChuck Lever if (IS_ERR(xprt)) 2994c8541ecdSChuck Lever return xprt; 2995c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2996a246b010SChuck Lever 2997ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2998808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2999808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3000a246b010SChuck Lever 300103bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 300203bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 300303bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3004a246b010SChuck Lever 3005262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3006ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3007a246b010SChuck Lever 30083851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 30097196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 30107196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 30113851f1cdSTrond Myklebust 3012c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 3013edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3014edc1b01cSTrond Myklebust 30158f9d5b1aSChuck Lever switch (addr->sa_family) { 30168f9d5b1aSChuck Lever case AF_INET: 30178f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30188f9d5b1aSChuck Lever xprt_set_bound(xprt); 30198f9d5b1aSChuck Lever 30209dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 30218f9d5b1aSChuck Lever break; 30228f9d5b1aSChuck Lever case AF_INET6: 30238f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30248f9d5b1aSChuck Lever xprt_set_bound(xprt); 30258f9d5b1aSChuck Lever 30269dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 30278f9d5b1aSChuck Lever break; 30288f9d5b1aSChuck Lever default: 30290a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30300a68b0beSJ. Bruce Fields goto out_err; 30318f9d5b1aSChuck Lever } 30328f9d5b1aSChuck Lever 3033c740eff8SChuck Lever if (xprt_bound(xprt)) 3034c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3035c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3036c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3037c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3038c740eff8SChuck Lever else 3039c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3040c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3041c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3042c740eff8SChuck Lever 3043bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3044c8541ecdSChuck Lever return xprt; 30450a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30460a68b0beSJ. Bruce Fields out_err: 3047315f3812SKinglong Mee xs_xprt_free(xprt); 30480a68b0beSJ. Bruce Fields return ret; 3049a246b010SChuck Lever } 3050282b32e1SChuck Lever 3051f300babaSAlexandros Batsakis /** 3052f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3053f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3054f300babaSAlexandros Batsakis * 3055f300babaSAlexandros Batsakis */ 3056f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3057f300babaSAlexandros Batsakis { 3058f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3059f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3060f300babaSAlexandros Batsakis struct sock_xprt *transport; 3061f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 30620a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3063f300babaSAlexandros Batsakis 3064d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3065d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3066f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3067f300babaSAlexandros Batsakis return xprt; 3068f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3069f300babaSAlexandros Batsakis 3070f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3071f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3072f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3073f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3074f300babaSAlexandros Batsakis 3075f300babaSAlexandros Batsakis /* backchannel */ 3076f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3077f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3078f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3079f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3080f300babaSAlexandros Batsakis 3081f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3082f300babaSAlexandros Batsakis 3083f300babaSAlexandros Batsakis switch (addr->sa_family) { 3084f300babaSAlexandros Batsakis case AF_INET: 3085f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3086f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3087f300babaSAlexandros Batsakis break; 3088f300babaSAlexandros Batsakis case AF_INET6: 3089f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3090f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3091f300babaSAlexandros Batsakis break; 3092f300babaSAlexandros Batsakis default: 30930a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30940a68b0beSJ. Bruce Fields goto out_err; 3095f300babaSAlexandros Batsakis } 3096f300babaSAlexandros Batsakis 3097f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3098f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3099f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3100f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3101f300babaSAlexandros Batsakis 3102f300babaSAlexandros Batsakis /* 310399de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 310428303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 310528303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 310628303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 310799de8ea9SJ. Bruce Fields */ 310899de8ea9SJ. Bruce Fields xprt_get(xprt); 310999de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 311099de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 311199de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 311299de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 311399de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 311499de8ea9SJ. Bruce Fields 311599de8ea9SJ. Bruce Fields /* 3116f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3117f300babaSAlexandros Batsakis * the xprt status to connected 3118f300babaSAlexandros Batsakis */ 3119f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3120f300babaSAlexandros Batsakis 3121f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3122f300babaSAlexandros Batsakis return xprt; 3123642aab58SKinglong Mee 3124642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 312539a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 312699de8ea9SJ. Bruce Fields xprt_put(xprt); 31270a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31280a68b0beSJ. Bruce Fields out_err: 3129315f3812SKinglong Mee xs_xprt_free(xprt); 31300a68b0beSJ. Bruce Fields return ret; 3131f300babaSAlexandros Batsakis } 3132f300babaSAlexandros Batsakis 3133176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3134176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3135176e21eeSChuck Lever .name = "named UNIX socket", 3136176e21eeSChuck Lever .owner = THIS_MODULE, 3137176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3138176e21eeSChuck Lever .setup = xs_setup_local, 3139176e21eeSChuck Lever }; 3140176e21eeSChuck Lever 3141bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3142bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3143bc25571eS\"Talpey, Thomas\ .name = "udp", 3144bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3145f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3146bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3147bc25571eS\"Talpey, Thomas\ }; 3148bc25571eS\"Talpey, Thomas\ 3149bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3150bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3151bc25571eS\"Talpey, Thomas\ .name = "tcp", 3152bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3153f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3154bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3155bc25571eS\"Talpey, Thomas\ }; 3156bc25571eS\"Talpey, Thomas\ 3157f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3158f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3159f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3160f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3161f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3162f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3163f300babaSAlexandros Batsakis }; 3164f300babaSAlexandros Batsakis 3165282b32e1SChuck Lever /** 3166bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3167282b32e1SChuck Lever * 3168282b32e1SChuck Lever */ 3169282b32e1SChuck Lever int init_socket_xprt(void) 3170282b32e1SChuck Lever { 31712b1bec5fSEric W. Biederman if (!sunrpc_table_header) 31720b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3173fbf76683SChuck Lever 3174176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3175bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3176bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3177f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3178bc25571eS\"Talpey, Thomas\ 3179282b32e1SChuck Lever return 0; 3180282b32e1SChuck Lever } 3181282b32e1SChuck Lever 3182282b32e1SChuck Lever /** 3183bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3184282b32e1SChuck Lever * 3185282b32e1SChuck Lever */ 3186282b32e1SChuck Lever void cleanup_socket_xprt(void) 3187282b32e1SChuck Lever { 3188fbf76683SChuck Lever if (sunrpc_table_header) { 3189fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3190fbf76683SChuck Lever sunrpc_table_header = NULL; 3191fbf76683SChuck Lever } 3192bc25571eS\"Talpey, Thomas\ 3193176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3194bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3195bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3196f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3197282b32e1SChuck Lever } 3198cbf11071STrond Myklebust 31999bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 32009bbb9e5aSRusty Russell const struct kernel_param *kp, 3201cbf11071STrond Myklebust unsigned int min, unsigned int max) 3202cbf11071STrond Myklebust { 320300cfaa94SDaniel Walter unsigned int num; 3204cbf11071STrond Myklebust int ret; 3205cbf11071STrond Myklebust 3206cbf11071STrond Myklebust if (!val) 3207cbf11071STrond Myklebust return -EINVAL; 320800cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 32099761a246SDan Carpenter if (ret) 32109761a246SDan Carpenter return ret; 32119761a246SDan Carpenter if (num < min || num > max) 3212cbf11071STrond Myklebust return -EINVAL; 3213cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3214cbf11071STrond Myklebust return 0; 3215cbf11071STrond Myklebust } 3216cbf11071STrond Myklebust 32179bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3218cbf11071STrond Myklebust { 3219cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3220cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3221cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3222cbf11071STrond Myklebust } 3223cbf11071STrond Myklebust 32249c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 32259bbb9e5aSRusty Russell .set = param_set_portnr, 32269bbb9e5aSRusty Russell .get = param_get_uint, 32279bbb9e5aSRusty Russell }; 32289bbb9e5aSRusty Russell 3229cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3230cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3231cbf11071STrond Myklebust 3232cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3233cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3234cbf11071STrond Myklebust 32359bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 32369bbb9e5aSRusty Russell const struct kernel_param *kp) 3237cbf11071STrond Myklebust { 3238cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3239cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3240cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3241cbf11071STrond Myklebust } 3242cbf11071STrond Myklebust 32439c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 32449bbb9e5aSRusty Russell .set = param_set_slot_table_size, 32459bbb9e5aSRusty Russell .get = param_get_uint, 32469bbb9e5aSRusty Russell }; 32479bbb9e5aSRusty Russell 3248cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3249cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3250cbf11071STrond Myklebust 3251d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3252d9ba131dSTrond Myklebust const struct kernel_param *kp) 3253d9ba131dSTrond Myklebust { 3254d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3255d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3256d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3257d9ba131dSTrond Myklebust } 3258d9ba131dSTrond Myklebust 32599c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3260d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3261d9ba131dSTrond Myklebust .get = param_get_uint, 3262d9ba131dSTrond Myklebust }; 3263d9ba131dSTrond Myklebust 3264d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3265d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3266d9ba131dSTrond Myklebust 3267cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3268cbf11071STrond Myklebust slot_table_size, 0644); 3269d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3270d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3271cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3272cbf11071STrond Myklebust slot_table_size, 0644); 3273