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> 50dea034b9SChuck Lever #include <net/tls.h> 5175eb6af7SChuck Lever #include <net/handshake.h> 52dea034b9SChuck Lever 53277e4ab7STrond Myklebust #include <linux/bvec.h> 546a829eb8STrond Myklebust #include <linux/highmem.h> 55277e4ab7STrond Myklebust #include <linux/uio.h> 56a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 57a246b010SChuck Lever 5840e0b090SPeilin Ye #include <trace/events/sock.h> 5940b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 6040b5ea0cSTrond Myklebust 619e55eef4SChuck Lever #include "socklib.h" 624cfc7e60SRahul Iyer #include "sunrpc.h" 63176e21eeSChuck Lever 64176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 653b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock); 667196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 677196dbb0STrond Myklebust struct socket *sock); 68176e21eeSChuck Lever 699903cd1cSChuck Lever /* 70c556b754SChuck Lever * xprtsock tunables 71c556b754SChuck Lever */ 7209acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 7309acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 7409acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 75c556b754SChuck Lever 7609acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 7709acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 78c556b754SChuck Lever 797d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 8025fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 817d1e8255STrond Myklebust 82c556b754SChuck Lever /* 83fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 84c946cb69SLuis Chamberlain * calling register_sysctl() again. The files in that 85fbf76683SChuck Lever * directory become the union of all files registered there. 86fbf76683SChuck Lever * 87fbf76683SChuck Lever * We simply need to make sure that we don't collide with 88fbf76683SChuck Lever * someone else's file names! 89fbf76683SChuck Lever */ 90fbf76683SChuck Lever 91fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 92fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 93d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 94fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 95fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 96fbf76683SChuck Lever 97fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 98fbf76683SChuck Lever 99d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport; 100d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport; 101d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport; 10275eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport; 103d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport; 104d3abc739SOlga Kornievskaia 105fbf76683SChuck Lever /* 106fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 107fbf76683SChuck Lever * socket buffers for existing UDP transports 108fbf76683SChuck Lever */ 109fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 110fbf76683SChuck Lever { 111fbf76683SChuck Lever .procname = "udp_slot_table_entries", 112fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 113fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 114fbf76683SChuck Lever .mode = 0644, 1156d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 116fbf76683SChuck Lever .extra1 = &min_slot_table_size, 117fbf76683SChuck Lever .extra2 = &max_slot_table_size 118fbf76683SChuck Lever }, 119fbf76683SChuck Lever { 120fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 121fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 122fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 123fbf76683SChuck Lever .mode = 0644, 1246d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 125fbf76683SChuck Lever .extra1 = &min_slot_table_size, 126fbf76683SChuck Lever .extra2 = &max_slot_table_size 127fbf76683SChuck Lever }, 128fbf76683SChuck Lever { 129d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 130d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 131d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 132d9ba131dSTrond Myklebust .mode = 0644, 133d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 134d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 135d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 136d9ba131dSTrond Myklebust }, 137d9ba131dSTrond Myklebust { 138fbf76683SChuck Lever .procname = "min_resvport", 139fbf76683SChuck Lever .data = &xprt_min_resvport, 140fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 141fbf76683SChuck Lever .mode = 0644, 1426d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 143fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 144826799e6SJ. Bruce Fields .extra2 = &xprt_max_resvport_limit 145fbf76683SChuck Lever }, 146fbf76683SChuck Lever { 147fbf76683SChuck Lever .procname = "max_resvport", 148fbf76683SChuck Lever .data = &xprt_max_resvport, 149fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 150fbf76683SChuck Lever .mode = 0644, 1516d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 152826799e6SJ. Bruce Fields .extra1 = &xprt_min_resvport_limit, 153fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 154fbf76683SChuck Lever }, 155fbf76683SChuck Lever { 15625fe6142STrond Myklebust .procname = "tcp_fin_timeout", 15725fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 15825fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 15925fe6142STrond Myklebust .mode = 0644, 1606d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 16125fe6142STrond Myklebust }, 162f8572d8fSEric W. Biederman { }, 163fbf76683SChuck Lever }; 164fbf76683SChuck Lever 165fbf76683SChuck Lever /* 16603bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16703bf4b70SChuck Lever */ 16803bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16903bf4b70SChuck Lever 17003bf4b70SChuck Lever /* 17103bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 17203bf4b70SChuck Lever * kind of resource problem on the local host. 17303bf4b70SChuck Lever */ 17403bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17503bf4b70SChuck Lever 17603bf4b70SChuck Lever /* 17703bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17803bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17903bf4b70SChuck Lever * 18003bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 18103bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 18203bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18303bf4b70SChuck Lever * increase over time if the server is down or not responding. 18403bf4b70SChuck Lever */ 18503bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18603bf4b70SChuck Lever 18703bf4b70SChuck Lever /* 18803bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18903bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 19003bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 19103bf4b70SChuck Lever */ 19203bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19303bf4b70SChuck Lever 19475eb6af7SChuck Lever /* 19575eb6af7SChuck Lever * TLS handshake timeout. 19675eb6af7SChuck Lever */ 19775eb6af7SChuck Lever #define XS_TLS_HANDSHAKE_TO (10U * HZ) 19875eb6af7SChuck Lever 199f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 200a246b010SChuck Lever # undef RPC_DEBUG_DATA 2019903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 202a246b010SChuck Lever #endif 203a246b010SChuck Lever 204a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 2059903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 206a246b010SChuck Lever { 207a246b010SChuck Lever u8 *buf = (u8 *) packet; 208a246b010SChuck Lever int j; 209a246b010SChuck Lever 210a246b010SChuck Lever dprintk("RPC: %s\n", msg); 211a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 212a246b010SChuck Lever if (!(j & 31)) { 213a246b010SChuck Lever if (j) 214a246b010SChuck Lever dprintk("\n"); 215a246b010SChuck Lever dprintk("0x%04x ", j); 216a246b010SChuck Lever } 217a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 218a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 219a246b010SChuck Lever } 220a246b010SChuck Lever dprintk("\n"); 221a246b010SChuck Lever } 222a246b010SChuck Lever #else 2239903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 224a246b010SChuck Lever { 225a246b010SChuck Lever /* NOP */ 226a246b010SChuck Lever } 227a246b010SChuck Lever #endif 228a246b010SChuck Lever 2292118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2302118071dSTrond Myklebust { 2312118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2322118071dSTrond Myklebust } 2332118071dSTrond Myklebust 23495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 235edb267a6SChuck Lever { 23695392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 23795392c59SChuck Lever } 23895392c59SChuck Lever 239176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 240176e21eeSChuck Lever { 241176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 242176e21eeSChuck Lever } 243176e21eeSChuck Lever 24495392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 24595392c59SChuck Lever { 24695392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 24795392c59SChuck Lever } 24895392c59SChuck Lever 24995392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 25095392c59SChuck Lever { 25195392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 25295392c59SChuck Lever } 25395392c59SChuck Lever 254c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 255c877b849SChuck Lever { 256c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2579dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2589dc3b095SChuck Lever struct sockaddr_in *sin; 259176e21eeSChuck Lever struct sockaddr_un *sun; 260c877b849SChuck Lever char buf[128]; 261c877b849SChuck Lever 2629dc3b095SChuck Lever switch (sap->sa_family) { 263176e21eeSChuck Lever case AF_LOCAL: 264176e21eeSChuck Lever sun = xs_addr_un(xprt); 2654388ce05SNeilBrown if (sun->sun_path[0]) { 26615bcdc92SWolfram Sang strscpy(buf, sun->sun_path, sizeof(buf)); 2674388ce05SNeilBrown } else { 2684388ce05SNeilBrown buf[0] = '@'; 2694388ce05SNeilBrown strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1); 2704388ce05SNeilBrown } 271176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 272176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 273176e21eeSChuck Lever break; 2749dc3b095SChuck Lever case AF_INET: 275176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 276176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 277176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2789dc3b095SChuck Lever sin = xs_addr_in(xprt); 279fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2809dc3b095SChuck Lever break; 2819dc3b095SChuck Lever case AF_INET6: 282176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 283176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 284176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2859dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 286fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2879dc3b095SChuck Lever break; 2889dc3b095SChuck Lever default: 2899dc3b095SChuck Lever BUG(); 2909dc3b095SChuck Lever } 291176e21eeSChuck Lever 2929dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2939dc3b095SChuck Lever } 2949dc3b095SChuck Lever 2959dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2969dc3b095SChuck Lever { 2979dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2989dc3b095SChuck Lever char buf[128]; 2999dc3b095SChuck Lever 30081160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 301c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 302c877b849SChuck Lever 30381160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 304c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 305c877b849SChuck Lever } 306c877b849SChuck Lever 3079dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 308b454ae90SChuck Lever const char *protocol, 309b454ae90SChuck Lever const char *netid) 310edb267a6SChuck Lever { 311b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 312b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 313c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3149dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 315edb267a6SChuck Lever } 316edb267a6SChuck Lever 3179dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3184b6473fbSChuck Lever { 3199dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3209dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3214b6473fbSChuck Lever 3229dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 323edb267a6SChuck Lever } 324edb267a6SChuck Lever 325edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 326edb267a6SChuck Lever { 32733e01dc7SChuck Lever unsigned int i; 32833e01dc7SChuck Lever 32933e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 33033e01dc7SChuck Lever switch (i) { 33133e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 33233e01dc7SChuck Lever case RPC_DISPLAY_NETID: 33333e01dc7SChuck Lever continue; 33433e01dc7SChuck Lever default: 33533e01dc7SChuck Lever kfree(xprt->address_strings[i]); 33633e01dc7SChuck Lever } 337edb267a6SChuck Lever } 338edb267a6SChuck Lever 339277e4ab7STrond Myklebust static size_t 340277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 341277e4ab7STrond Myklebust { 342277e4ab7STrond Myklebust size_t i,n; 343277e4ab7STrond Myklebust 34416e5e90fSTrond Myklebust if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 345277e4ab7STrond Myklebust return want; 346277e4ab7STrond Myklebust n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 347277e4ab7STrond Myklebust for (i = 0; i < n; i++) { 348277e4ab7STrond Myklebust if (buf->pages[i]) 349277e4ab7STrond Myklebust continue; 350277e4ab7STrond Myklebust buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 351277e4ab7STrond Myklebust if (!buf->pages[i]) { 35216e5e90fSTrond Myklebust i *= PAGE_SIZE; 35316e5e90fSTrond Myklebust return i > buf->page_base ? i - buf->page_base : 0; 354277e4ab7STrond Myklebust } 355277e4ab7STrond Myklebust } 356277e4ab7STrond Myklebust return want; 357277e4ab7STrond Myklebust } 358277e4ab7STrond Myklebust 359dea034b9SChuck Lever static int 360dea034b9SChuck Lever xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg, 361dea034b9SChuck Lever struct cmsghdr *cmsg, int ret) 362dea034b9SChuck Lever { 363dea034b9SChuck Lever if (cmsg->cmsg_level == SOL_TLS && 364dea034b9SChuck Lever cmsg->cmsg_type == TLS_GET_RECORD_TYPE) { 365dea034b9SChuck Lever u8 content_type = *((u8 *)CMSG_DATA(cmsg)); 366dea034b9SChuck Lever 367dea034b9SChuck Lever switch (content_type) { 368dea034b9SChuck Lever case TLS_RECORD_TYPE_DATA: 369dea034b9SChuck Lever /* TLS sets EOR at the end of each application data 370dea034b9SChuck Lever * record, even though there might be more frames 371dea034b9SChuck Lever * waiting to be decrypted. 372dea034b9SChuck Lever */ 373dea034b9SChuck Lever msg->msg_flags &= ~MSG_EOR; 374dea034b9SChuck Lever break; 375dea034b9SChuck Lever case TLS_RECORD_TYPE_ALERT: 376dea034b9SChuck Lever ret = -ENOTCONN; 377dea034b9SChuck Lever break; 378dea034b9SChuck Lever default: 379dea034b9SChuck Lever ret = -EAGAIN; 380dea034b9SChuck Lever } 381dea034b9SChuck Lever } 382dea034b9SChuck Lever return ret; 383dea034b9SChuck Lever } 384dea034b9SChuck Lever 385dea034b9SChuck Lever static int 386dea034b9SChuck Lever xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags) 387dea034b9SChuck Lever { 388dea034b9SChuck Lever union { 389dea034b9SChuck Lever struct cmsghdr cmsg; 390dea034b9SChuck Lever u8 buf[CMSG_SPACE(sizeof(u8))]; 391dea034b9SChuck Lever } u; 392dea034b9SChuck Lever int ret; 393dea034b9SChuck Lever 394dea034b9SChuck Lever msg->msg_control = &u; 395dea034b9SChuck Lever msg->msg_controllen = sizeof(u); 396dea034b9SChuck Lever ret = sock_recvmsg(sock, msg, flags); 397dea034b9SChuck Lever if (msg->msg_controllen != sizeof(u)) 398dea034b9SChuck Lever ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret); 399dea034b9SChuck Lever return ret; 400dea034b9SChuck Lever } 401dea034b9SChuck Lever 402277e4ab7STrond Myklebust static ssize_t 403277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 404277e4ab7STrond Myklebust { 405277e4ab7STrond Myklebust ssize_t ret; 406277e4ab7STrond Myklebust if (seek != 0) 407277e4ab7STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 408dea034b9SChuck Lever ret = xs_sock_recv_cmsg(sock, msg, flags); 409277e4ab7STrond Myklebust return ret > 0 ? ret + seek : ret; 410277e4ab7STrond Myklebust } 411277e4ab7STrond Myklebust 412277e4ab7STrond Myklebust static ssize_t 413277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 414277e4ab7STrond Myklebust struct kvec *kvec, size_t count, size_t seek) 415277e4ab7STrond Myklebust { 416de4eda9dSAl Viro iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count); 417277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 418277e4ab7STrond Myklebust } 419277e4ab7STrond Myklebust 420277e4ab7STrond Myklebust static ssize_t 421277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 422277e4ab7STrond Myklebust struct bio_vec *bvec, unsigned long nr, size_t count, 423277e4ab7STrond Myklebust size_t seek) 424277e4ab7STrond Myklebust { 425de4eda9dSAl Viro iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count); 426277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 427277e4ab7STrond Myklebust } 428277e4ab7STrond Myklebust 429277e4ab7STrond Myklebust static ssize_t 430277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 431277e4ab7STrond Myklebust size_t count) 432277e4ab7STrond Myklebust { 433de4eda9dSAl Viro iov_iter_discard(&msg->msg_iter, ITER_DEST, count); 434dea034b9SChuck Lever return xs_sock_recv_cmsg(sock, msg, flags); 435277e4ab7STrond Myklebust } 436277e4ab7STrond Myklebust 4376a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 4386a829eb8STrond Myklebust static void 4396a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 4406a829eb8STrond Myklebust { 4416a829eb8STrond Myklebust struct bvec_iter bi = { 4426a829eb8STrond Myklebust .bi_size = count, 4436a829eb8STrond Myklebust }; 4446a829eb8STrond Myklebust struct bio_vec bv; 4456a829eb8STrond Myklebust 4466a829eb8STrond Myklebust bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 4476a829eb8STrond Myklebust for_each_bvec(bv, bvec, bi, bi) 4486a829eb8STrond Myklebust flush_dcache_page(bv.bv_page); 4496a829eb8STrond Myklebust } 4506a829eb8STrond Myklebust #else 4516a829eb8STrond Myklebust static inline void 4526a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 4536a829eb8STrond Myklebust { 4546a829eb8STrond Myklebust } 4556a829eb8STrond Myklebust #endif 4566a829eb8STrond Myklebust 457277e4ab7STrond Myklebust static ssize_t 458277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 459277e4ab7STrond Myklebust struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 460277e4ab7STrond Myklebust { 461277e4ab7STrond Myklebust size_t want, seek_init = seek, offset = 0; 462277e4ab7STrond Myklebust ssize_t ret; 463277e4ab7STrond Myklebust 464277e4ab7STrond Myklebust want = min_t(size_t, count, buf->head[0].iov_len); 465e92053a5STrond Myklebust if (seek < want) { 466277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 467277e4ab7STrond Myklebust if (ret <= 0) 468277e4ab7STrond Myklebust goto sock_err; 469277e4ab7STrond Myklebust offset += ret; 470277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 471277e4ab7STrond Myklebust goto out; 472277e4ab7STrond Myklebust if (ret != want) 473c4433055STrond Myklebust goto out; 474277e4ab7STrond Myklebust seek = 0; 475277e4ab7STrond Myklebust } else { 476e92053a5STrond Myklebust seek -= want; 477e92053a5STrond Myklebust offset += want; 478277e4ab7STrond Myklebust } 47916e5e90fSTrond Myklebust 480b2648015STrond Myklebust want = xs_alloc_sparse_pages( 481b2648015STrond Myklebust buf, min_t(size_t, count - offset, buf->page_len), 482b2648015STrond Myklebust GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 48316e5e90fSTrond Myklebust if (seek < want) { 484277e4ab7STrond Myklebust ret = xs_read_bvec(sock, msg, flags, buf->bvec, 485277e4ab7STrond Myklebust xdr_buf_pagecount(buf), 486277e4ab7STrond Myklebust want + buf->page_base, 487277e4ab7STrond Myklebust seek + buf->page_base); 488277e4ab7STrond Myklebust if (ret <= 0) 489277e4ab7STrond Myklebust goto sock_err; 4906a829eb8STrond Myklebust xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 491ac9645c8SDan Aloni ret -= buf->page_base; 492ac9645c8SDan Aloni offset += ret; 493277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 494277e4ab7STrond Myklebust goto out; 495277e4ab7STrond Myklebust if (ret != want) 496c4433055STrond Myklebust goto out; 497277e4ab7STrond Myklebust seek = 0; 498277e4ab7STrond Myklebust } else { 49916e5e90fSTrond Myklebust seek -= want; 50016e5e90fSTrond Myklebust offset += want; 501277e4ab7STrond Myklebust } 50216e5e90fSTrond Myklebust 503277e4ab7STrond Myklebust want = min_t(size_t, count - offset, buf->tail[0].iov_len); 504e92053a5STrond Myklebust if (seek < want) { 505277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 506277e4ab7STrond Myklebust if (ret <= 0) 507277e4ab7STrond Myklebust goto sock_err; 508277e4ab7STrond Myklebust offset += ret; 509277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 510277e4ab7STrond Myklebust goto out; 511277e4ab7STrond Myklebust if (ret != want) 512c4433055STrond Myklebust goto out; 5139734ad57STrond Myklebust } else if (offset < seek_init) 514e92053a5STrond Myklebust offset = seek_init; 515277e4ab7STrond Myklebust ret = -EMSGSIZE; 516277e4ab7STrond Myklebust out: 517277e4ab7STrond Myklebust *read = offset - seek_init; 518277e4ab7STrond Myklebust return ret; 519277e4ab7STrond Myklebust sock_err: 520277e4ab7STrond Myklebust offset += seek; 521277e4ab7STrond Myklebust goto out; 522277e4ab7STrond Myklebust } 523277e4ab7STrond Myklebust 524277e4ab7STrond Myklebust static void 525277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 526277e4ab7STrond Myklebust { 527277e4ab7STrond Myklebust if (!transport->recv.copied) { 528277e4ab7STrond Myklebust if (buf->head[0].iov_len >= transport->recv.offset) 529277e4ab7STrond Myklebust memcpy(buf->head[0].iov_base, 530277e4ab7STrond Myklebust &transport->recv.xid, 531277e4ab7STrond Myklebust transport->recv.offset); 532277e4ab7STrond Myklebust transport->recv.copied = transport->recv.offset; 533277e4ab7STrond Myklebust } 534277e4ab7STrond Myklebust } 535277e4ab7STrond Myklebust 536277e4ab7STrond Myklebust static bool 537277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport) 538277e4ab7STrond Myklebust { 539277e4ab7STrond Myklebust return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 540277e4ab7STrond Myklebust } 541277e4ab7STrond Myklebust 542e92053a5STrond Myklebust static void 543e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport, 544e92053a5STrond Myklebust struct msghdr *msg) 545e92053a5STrond Myklebust { 546e92053a5STrond Myklebust if (xs_read_stream_request_done(transport)) 547e92053a5STrond Myklebust msg->msg_flags |= MSG_EOR; 548e92053a5STrond Myklebust } 549e92053a5STrond Myklebust 550277e4ab7STrond Myklebust static ssize_t 551277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 552277e4ab7STrond Myklebust int flags, struct rpc_rqst *req) 553277e4ab7STrond Myklebust { 554277e4ab7STrond Myklebust struct xdr_buf *buf = &req->rq_private_buf; 5553f649ab7SKees Cook size_t want, read; 5563f649ab7SKees Cook ssize_t ret; 557277e4ab7STrond Myklebust 558277e4ab7STrond Myklebust xs_read_header(transport, buf); 559277e4ab7STrond Myklebust 560277e4ab7STrond Myklebust want = transport->recv.len - transport->recv.offset; 561e92053a5STrond Myklebust if (want != 0) { 562277e4ab7STrond Myklebust ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 563e92053a5STrond Myklebust transport->recv.copied + want, 564e92053a5STrond Myklebust transport->recv.copied, 565277e4ab7STrond Myklebust &read); 566277e4ab7STrond Myklebust transport->recv.offset += read; 567277e4ab7STrond Myklebust transport->recv.copied += read; 568277e4ab7STrond Myklebust } 569277e4ab7STrond Myklebust 570727fcc64STrond Myklebust if (transport->recv.offset == transport->recv.len) 571727fcc64STrond Myklebust xs_read_stream_check_eor(transport, msg); 572727fcc64STrond Myklebust 573e92053a5STrond Myklebust if (want == 0) 574e92053a5STrond Myklebust return 0; 575e92053a5STrond Myklebust 576277e4ab7STrond Myklebust switch (ret) { 577c4433055STrond Myklebust default: 578c4433055STrond Myklebust break; 57926781eabSTrond Myklebust case -EFAULT: 580277e4ab7STrond Myklebust case -EMSGSIZE: 58126781eabSTrond Myklebust msg->msg_flags |= MSG_TRUNC; 582c4433055STrond Myklebust return read; 583277e4ab7STrond Myklebust case 0: 584277e4ab7STrond Myklebust return -ESHUTDOWN; 585277e4ab7STrond Myklebust } 586c4433055STrond Myklebust return ret < 0 ? ret : read; 587277e4ab7STrond Myklebust } 588277e4ab7STrond Myklebust 589277e4ab7STrond Myklebust static size_t 590277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag) 591277e4ab7STrond Myklebust { 592277e4ab7STrond Myklebust if (isfrag) 593277e4ab7STrond Myklebust return sizeof(__be32); 594277e4ab7STrond Myklebust return 3 * sizeof(__be32); 595277e4ab7STrond Myklebust } 596277e4ab7STrond Myklebust 597277e4ab7STrond Myklebust static ssize_t 598277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 599277e4ab7STrond Myklebust int flags, size_t want, size_t seek) 600277e4ab7STrond Myklebust { 601277e4ab7STrond Myklebust struct kvec kvec = { 602277e4ab7STrond Myklebust .iov_base = &transport->recv.fraghdr, 603277e4ab7STrond Myklebust .iov_len = want, 604277e4ab7STrond Myklebust }; 605277e4ab7STrond Myklebust return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 606277e4ab7STrond Myklebust } 607277e4ab7STrond Myklebust 608277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 609277e4ab7STrond Myklebust static ssize_t 610277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 611277e4ab7STrond Myklebust { 612277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 613277e4ab7STrond Myklebust struct rpc_rqst *req; 614277e4ab7STrond Myklebust ssize_t ret; 615277e4ab7STrond Myklebust 61698b5cee3SBenjamin Coddington /* Is this transport associated with the backchannel? */ 61798b5cee3SBenjamin Coddington if (!xprt->bc_serv) 61898b5cee3SBenjamin Coddington return -ESHUTDOWN; 61998b5cee3SBenjamin Coddington 620277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 621277e4ab7STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 622277e4ab7STrond Myklebust if (!req) { 623277e4ab7STrond Myklebust printk(KERN_WARNING "Callback slot table overflowed\n"); 624277e4ab7STrond Myklebust return -ESHUTDOWN; 625277e4ab7STrond Myklebust } 62645835a63STrond Myklebust if (transport->recv.copied && !req->rq_private_buf.len) 62745835a63STrond Myklebust return -ESHUTDOWN; 628277e4ab7STrond Myklebust 629277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 630277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 631c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 63245835a63STrond Myklebust else 63345835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 634277e4ab7STrond Myklebust 635277e4ab7STrond Myklebust return ret; 636277e4ab7STrond Myklebust } 637277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 638277e4ab7STrond Myklebust static ssize_t 639277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 640277e4ab7STrond Myklebust { 641277e4ab7STrond Myklebust return -ESHUTDOWN; 642277e4ab7STrond Myklebust } 643277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 644277e4ab7STrond Myklebust 645277e4ab7STrond Myklebust static ssize_t 646277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 647277e4ab7STrond Myklebust { 648277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 649277e4ab7STrond Myklebust struct rpc_rqst *req; 650277e4ab7STrond Myklebust ssize_t ret = 0; 651277e4ab7STrond Myklebust 652277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 653277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 654277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 65545835a63STrond Myklebust if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { 656277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 657277e4ab7STrond Myklebust goto out; 658277e4ab7STrond Myklebust } 659277e4ab7STrond Myklebust xprt_pin_rqst(req); 660277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 661277e4ab7STrond Myklebust 662277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 663277e4ab7STrond Myklebust 664277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 665277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 666c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 66745835a63STrond Myklebust else 66845835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 669277e4ab7STrond Myklebust xprt_unpin_rqst(req); 670277e4ab7STrond Myklebust out: 671277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 672277e4ab7STrond Myklebust return ret; 673277e4ab7STrond Myklebust } 674277e4ab7STrond Myklebust 675277e4ab7STrond Myklebust static ssize_t 676277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 677277e4ab7STrond Myklebust { 678277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 679277e4ab7STrond Myklebust size_t want, read = 0; 680277e4ab7STrond Myklebust ssize_t ret = 0; 681277e4ab7STrond Myklebust 682277e4ab7STrond Myklebust if (transport->recv.len == 0) { 683277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 684277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 685277e4ab7STrond Myklebust transport->recv.offset); 686277e4ab7STrond Myklebust if (ret <= 0) 687277e4ab7STrond Myklebust goto out_err; 688277e4ab7STrond Myklebust transport->recv.offset = ret; 689c4433055STrond Myklebust if (transport->recv.offset != want) 690c4433055STrond Myklebust return transport->recv.offset; 691277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 692277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 693277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 694277e4ab7STrond Myklebust read = ret; 695277e4ab7STrond Myklebust } 696277e4ab7STrond Myklebust 697277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 698c4433055STrond Myklebust default: 699c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 700c4433055STrond Myklebust break; 701277e4ab7STrond Myklebust case RPC_CALL: 702277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 703277e4ab7STrond Myklebust break; 704277e4ab7STrond Myklebust case RPC_REPLY: 705277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 706277e4ab7STrond Myklebust } 707277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 708277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 709277e4ab7STrond Myklebust transport->recv.copied = -1; 710277e4ab7STrond Myklebust } 711277e4ab7STrond Myklebust if (ret < 0) 712277e4ab7STrond Myklebust goto out_err; 713277e4ab7STrond Myklebust read += ret; 714277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 715c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 716c4433055STrond Myklebust return read; 717b76a5afdSTrond Myklebust msg.msg_flags = 0; 718277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 719277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 720277e4ab7STrond Myklebust if (ret <= 0) 721277e4ab7STrond Myklebust goto out_err; 722277e4ab7STrond Myklebust transport->recv.offset += ret; 723277e4ab7STrond Myklebust read += ret; 724277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 725c4433055STrond Myklebust return read; 726277e4ab7STrond Myklebust } 727277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 728c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 729277e4ab7STrond Myklebust transport->recv.copied = 0; 730277e4ab7STrond Myklebust } 731277e4ab7STrond Myklebust transport->recv.offset = 0; 732277e4ab7STrond Myklebust transport->recv.len = 0; 733277e4ab7STrond Myklebust return read; 734277e4ab7STrond Myklebust out_err: 73579462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 736277e4ab7STrond Myklebust } 737277e4ab7STrond Myklebust 7380ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 7390ffe86f4STrond Myklebust { 740a73881c9STrond Myklebust return transport->sock->ops->poll(transport->file, transport->sock, 741a73881c9STrond Myklebust NULL); 7420ffe86f4STrond Myklebust } 7430ffe86f4STrond Myklebust 7440ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 7450ffe86f4STrond Myklebust { 7460ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 7470ffe86f4STrond Myklebust 7480ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 7490ffe86f4STrond Myklebust } 7500ffe86f4STrond Myklebust 7510ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 7520ffe86f4STrond Myklebust { 7530ffe86f4STrond Myklebust 7540ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 7550d3ca07fSChuck Lever if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 7560d3ca07fSChuck Lever return; 7570ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 7580ffe86f4STrond Myklebust return; 7590ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 7600ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 7610ffe86f4STrond Myklebust } 7620ffe86f4STrond Myklebust 763c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 764c50b8ee0STrond Myklebust { 765c50b8ee0STrond Myklebust size_t read = 0; 766c50b8ee0STrond Myklebust ssize_t ret = 0; 767c50b8ee0STrond Myklebust 768c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 769c50b8ee0STrond Myklebust if (transport->sock == NULL) 770c50b8ee0STrond Myklebust goto out; 771c50b8ee0STrond Myklebust for (;;) { 772c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 773c4433055STrond Myklebust if (ret < 0) 774c50b8ee0STrond Myklebust break; 775c50b8ee0STrond Myklebust read += ret; 776c50b8ee0STrond Myklebust cond_resched(); 777c50b8ee0STrond Myklebust } 7785f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7795f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 7805f52a9d4STrond Myklebust else 7810ffe86f4STrond Myklebust xs_poll_check_readable(transport); 782c50b8ee0STrond Myklebust out: 783c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 784c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 785c50b8ee0STrond Myklebust } 786c50b8ee0STrond Myklebust 787c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 788c50b8ee0STrond Myklebust { 789c50b8ee0STrond Myklebust struct sock_xprt *transport = 790c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 791a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 792a1231fdaSTrond Myklebust 793c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 794a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 795c50b8ee0STrond Myklebust } 796c50b8ee0STrond Myklebust 797550aebfeSTrond Myklebust static void 798550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 799550aebfeSTrond Myklebust { 800550aebfeSTrond Myklebust transport->recv.offset = 0; 801550aebfeSTrond Myklebust transport->recv.len = 0; 802550aebfeSTrond Myklebust transport->recv.copied = 0; 803550aebfeSTrond Myklebust transport->xmit.offset = 0; 804ae053551STrond Myklebust } 805ae053551STrond Myklebust 806ae053551STrond Myklebust static void 807ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 808ae053551STrond Myklebust { 809550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 810550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 811550aebfeSTrond Myklebust } 812550aebfeSTrond Myklebust 813b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 814b4b5cc85SChuck Lever 8159903cd1cSChuck Lever /** 816c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 81750f484e2STrond Myklebust * @req: pointer to RPC request 8187496b59fSTrond Myklebust * @transport: pointer to struct sock_xprt 8199903cd1cSChuck Lever * 820a246b010SChuck Lever */ 8217496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport) 822a246b010SChuck Lever { 8237496b59fSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 82406ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 82524ca9a84STrond Myklebust int ret = -EAGAIN; 826a246b010SChuck Lever 827015747d2SChuck Lever trace_rpc_socket_nospace(req, transport); 828a246b010SChuck Lever 829262965f5SChuck Lever /* Protect against races with write_space */ 830b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 831a246b010SChuck Lever 832262965f5SChuck Lever /* Don't race with disconnect */ 833b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 8347496b59fSTrond Myklebust /* wait for more buffer space */ 8352790a624STrond Myklebust set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 8367496b59fSTrond Myklebust set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 8377496b59fSTrond Myklebust sk->sk_write_pending++; 8387496b59fSTrond Myklebust xprt_wait_for_buffer_space(xprt); 8397496b59fSTrond Myklebust } else 8407496b59fSTrond Myklebust ret = -ENOTCONN; 8417496b59fSTrond Myklebust 8427496b59fSTrond Myklebust spin_unlock(&xprt->transport_lock); 8437496b59fSTrond Myklebust return ret; 844d48f9ce7SDavid Vrabel } 8457496b59fSTrond Myklebust 8467496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req) 8477496b59fSTrond Myklebust { 8487496b59fSTrond Myklebust struct sock_xprt *transport = 8497496b59fSTrond Myklebust container_of(req->rq_xprt, struct sock_xprt, xprt); 8507496b59fSTrond Myklebust struct sock *sk = transport->inet; 8517496b59fSTrond Myklebust int ret = -EAGAIN; 8527496b59fSTrond Myklebust 8537496b59fSTrond Myklebust lock_sock(sk); 8547496b59fSTrond Myklebust if (!sock_writeable(sk)) 8557496b59fSTrond Myklebust ret = xs_nospace(req, transport); 8567496b59fSTrond Myklebust release_sock(sk); 8577496b59fSTrond Myklebust return ret; 8587496b59fSTrond Myklebust } 8597496b59fSTrond Myklebust 860d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait) 8617496b59fSTrond Myklebust { 8627496b59fSTrond Myklebust struct sock_xprt *transport = 8637496b59fSTrond Myklebust container_of(req->rq_xprt, struct sock_xprt, xprt); 8647496b59fSTrond Myklebust struct sock *sk = transport->inet; 8657496b59fSTrond Myklebust int ret = -EAGAIN; 8667496b59fSTrond Myklebust 867d0afde5fSTrond Myklebust if (vm_wait) 868d0afde5fSTrond Myklebust return -ENOBUFS; 8697496b59fSTrond Myklebust lock_sock(sk); 8707496b59fSTrond Myklebust if (!sk_stream_memory_free(sk)) 8717496b59fSTrond Myklebust ret = xs_nospace(req, transport); 8727496b59fSTrond Myklebust release_sock(sk); 8735e3771ceSTrond Myklebust return ret; 874a246b010SChuck Lever } 875a246b010SChuck Lever 87672691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf) 877277e4ab7STrond Myklebust { 87872691a26STrond Myklebust return xdr_alloc_bvec(buf, rpc_task_gfp_mask()); 879277e4ab7STrond Myklebust } 880277e4ab7STrond Myklebust 88161677eeeSChuck Lever /* 8824cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 8834cd34e7cSTrond Myklebust * could complete transmission. 8844cd34e7cSTrond Myklebust */ 8854cd34e7cSTrond Myklebust static bool 8864cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 8874cd34e7cSTrond Myklebust { 8884cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 8894cd34e7cSTrond Myklebust } 8904cd34e7cSTrond Myklebust 8914cd34e7cSTrond Myklebust /* 89206b5fc3aSTrond Myklebust * Return the stream record marker field for a record of length < 2^31-1 89361677eeeSChuck Lever */ 89406b5fc3aSTrond Myklebust static rpc_fraghdr 89506b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr) 89661677eeeSChuck Lever { 89706b5fc3aSTrond Myklebust if (!xdr->len) 89806b5fc3aSTrond Myklebust return 0; 89906b5fc3aSTrond Myklebust return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 90061677eeeSChuck Lever } 90161677eeeSChuck Lever 9029903cd1cSChuck Lever /** 903176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 90450f484e2STrond Myklebust * @req: pointer to RPC request 905176e21eeSChuck Lever * 906176e21eeSChuck Lever * Return values: 907176e21eeSChuck Lever * 0: The request has been sent 908176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 909176e21eeSChuck Lever * complete the request 910176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 91112b20ce3SBhaskar Chowdhury * other: Some other error occurred, the request was not sent 912176e21eeSChuck Lever */ 913adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 914176e21eeSChuck Lever { 915176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 916176e21eeSChuck Lever struct sock_xprt *transport = 917176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 918176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9197e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 9207e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 9219e55eef4SChuck Lever struct msghdr msg = { 9229e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9239e55eef4SChuck Lever }; 924d0afde5fSTrond Myklebust bool vm_wait; 9253f649ab7SKees Cook unsigned int sent; 926176e21eeSChuck Lever int status; 927176e21eeSChuck Lever 9284cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 9294cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 930f0043206STrond Myklebust xprt_force_disconnect(xprt); 9314cd34e7cSTrond Myklebust return -ENOTCONN; 9324cd34e7cSTrond Myklebust } 9334cd34e7cSTrond Myklebust 934176e21eeSChuck Lever xs_pktdump("packet data:", 935176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 936176e21eeSChuck Lever 937d0afde5fSTrond Myklebust vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 938d0afde5fSTrond Myklebust 93978215759SChuck Lever req->rq_xtime = ktime_get(); 9409e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 9417e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 942176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 9436c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 944743c69e7SNeilBrown 945f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 9466c7a64e5STrond Myklebust transport->xmit.offset += sent; 9476c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 9487e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 9496c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 9506c7a64e5STrond Myklebust transport->xmit.offset = 0; 951176e21eeSChuck Lever return 0; 952176e21eeSChuck Lever } 953176e21eeSChuck Lever status = -EAGAIN; 954d0afde5fSTrond Myklebust vm_wait = false; 955176e21eeSChuck Lever } 956176e21eeSChuck Lever 957176e21eeSChuck Lever switch (status) { 958176e21eeSChuck Lever case -EAGAIN: 959d0afde5fSTrond Myklebust status = xs_stream_nospace(req, vm_wait); 960176e21eeSChuck Lever break; 961176e21eeSChuck Lever default: 962176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 963176e21eeSChuck Lever -status); 964df561f66SGustavo A. R. Silva fallthrough; 965176e21eeSChuck Lever case -EPIPE: 966f0043206STrond Myklebust xprt_force_disconnect(xprt); 967176e21eeSChuck Lever status = -ENOTCONN; 968176e21eeSChuck Lever } 969176e21eeSChuck Lever 970176e21eeSChuck Lever return status; 971176e21eeSChuck Lever } 972176e21eeSChuck Lever 973176e21eeSChuck Lever /** 974262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 97550f484e2STrond Myklebust * @req: pointer to RPC request 9769903cd1cSChuck Lever * 9779903cd1cSChuck Lever * Return values: 9789903cd1cSChuck Lever * 0: The request has been sent 9799903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 9809903cd1cSChuck Lever * complete the request 981262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 98225985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 9839903cd1cSChuck Lever */ 984adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 985a246b010SChuck Lever { 986a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 987ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 988262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9899e55eef4SChuck Lever struct msghdr msg = { 9909e55eef4SChuck Lever .msg_name = xs_addr(xprt), 9919e55eef4SChuck Lever .msg_namelen = xprt->addrlen, 9929e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9939e55eef4SChuck Lever }; 9943f649ab7SKees Cook unsigned int sent; 995262965f5SChuck Lever int status; 996262965f5SChuck Lever 997262965f5SChuck Lever xs_pktdump("packet data:", 998262965f5SChuck Lever req->rq_svec->iov_base, 999262965f5SChuck Lever req->rq_svec->iov_len); 1000262965f5SChuck Lever 100101d37c42STrond Myklebust if (!xprt_bound(xprt)) 100201d37c42STrond Myklebust return -ENOTCONN; 100375891f50STrond Myklebust 100475891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 100575891f50STrond Myklebust return -EBADSLT; 100675891f50STrond Myklebust 1007ff053dbbSTrond Myklebust status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 1008ff053dbbSTrond Myklebust if (status < 0) 1009ff053dbbSTrond Myklebust return status; 101078215759SChuck Lever req->rq_xtime = ktime_get(); 10119e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent); 1012262965f5SChuck Lever 1013262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 10146c7a64e5STrond Myklebust xdr->len, status); 1015262965f5SChuck Lever 10163dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 10173dedbb5cSJason Baron if (status == -EPERM) 10183dedbb5cSJason Baron goto process_status; 10193dedbb5cSJason Baron 1020743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 1021743c69e7SNeilBrown status = -ENOBUFS; 1022743c69e7SNeilBrown 1023f279cd00SJason Baron if (sent > 0 || status == 0) { 1024f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 1025f279cd00SJason Baron if (sent >= req->rq_slen) 1026262965f5SChuck Lever return 0; 1027262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 1028262965f5SChuck Lever status = -EAGAIN; 10292199700fSTrond Myklebust } 1030262965f5SChuck Lever 10313dedbb5cSJason Baron process_status: 1032262965f5SChuck Lever switch (status) { 1033fba91afbSTrond Myklebust case -ENOTSOCK: 1034fba91afbSTrond Myklebust status = -ENOTCONN; 1035fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1036fba91afbSTrond Myklebust break; 1037b6ddf64fSTrond Myklebust case -EAGAIN: 10387496b59fSTrond Myklebust status = xs_sock_nospace(req); 1039b6ddf64fSTrond Myklebust break; 1040262965f5SChuck Lever case -ENETUNREACH: 10413601c4a9STrond Myklebust case -ENOBUFS: 1042262965f5SChuck Lever case -EPIPE: 1043262965f5SChuck Lever case -ECONNREFUSED: 10443dedbb5cSJason Baron case -EPERM: 1045262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 1046262965f5SChuck Lever * prompts ECONNREFUSED. */ 104713331a55STrond Myklebust break; 104813331a55STrond Myklebust default: 104913331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 105013331a55STrond Myklebust -status); 1051262965f5SChuck Lever } 10525fe46e9dSBian Naimeng 1053262965f5SChuck Lever return status; 1054262965f5SChuck Lever } 1055262965f5SChuck Lever 1056e06799f9STrond Myklebust /** 1057262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 105850f484e2STrond Myklebust * @req: pointer to RPC request 1059262965f5SChuck Lever * 1060262965f5SChuck Lever * Return values: 1061262965f5SChuck Lever * 0: The request has been sent 1062262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 1063262965f5SChuck Lever * complete the request 1064262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 106525985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 1066262965f5SChuck Lever * 1067262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 1068262965f5SChuck Lever * if sendmsg is not able to make progress? 1069262965f5SChuck Lever */ 1070adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 1071262965f5SChuck Lever { 1072262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1073ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1074262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 10757e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 10767e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 10779e55eef4SChuck Lever struct msghdr msg = { 10789e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 10799e55eef4SChuck Lever }; 1080d0afde5fSTrond Myklebust bool vm_wait; 10813f649ab7SKees Cook unsigned int sent; 1082b595bb15SChuck Lever int status; 1083a246b010SChuck Lever 10844cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 10854cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 10864cd34e7cSTrond Myklebust if (transport->sock != NULL) 10874cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 10884cd34e7cSTrond Myklebust return -ENOTCONN; 10894cd34e7cSTrond Myklebust } 1090d275880aSTrond Myklebust if (!transport->inet) 1091d275880aSTrond Myklebust return -ENOTCONN; 10924cd34e7cSTrond Myklebust 1093262965f5SChuck Lever xs_pktdump("packet data:", 1094262965f5SChuck Lever req->rq_svec->iov_base, 1095262965f5SChuck Lever req->rq_svec->iov_len); 1096a246b010SChuck Lever 10977196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 10987196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 10997196dbb0STrond Myklebust 11003b21f757STrond Myklebust xs_set_srcport(transport, transport->sock); 11013b21f757STrond Myklebust 1102a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1103a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1104262965f5SChuck Lever * called sendmsg(). */ 110578215759SChuck Lever req->rq_xtime = ktime_get(); 1106d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, true); 1107d0afde5fSTrond Myklebust 1108d0afde5fSTrond Myklebust vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 1109d0afde5fSTrond Myklebust 1110d0afde5fSTrond Myklebust do { 11119e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 11127e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 1113a246b010SChuck Lever 1114262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 11156c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1116262965f5SChuck Lever 1117a246b010SChuck Lever /* If we've sent the entire packet, immediately 1118a246b010SChuck Lever * reset the count of bytes sent. */ 11196c7a64e5STrond Myklebust transport->xmit.offset += sent; 11206c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 11217e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 11226c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 11236c7a64e5STrond Myklebust transport->xmit.offset = 0; 1124d737e5d4STrond Myklebust if (atomic_long_read(&xprt->xmit_queuelen) == 1) 1125d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, false); 1126a246b010SChuck Lever return 0; 1127a246b010SChuck Lever } 1128262965f5SChuck Lever 11299ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 11309ffadfbcSTrond Myklebust 1131d0afde5fSTrond Myklebust if (sent > 0) 11329ffadfbcSTrond Myklebust vm_wait = false; 1133d0afde5fSTrond Myklebust 1134d0afde5fSTrond Myklebust } while (status == 0); 1135a246b010SChuck Lever 1136262965f5SChuck Lever switch (status) { 1137fba91afbSTrond Myklebust case -ENOTSOCK: 1138fba91afbSTrond Myklebust status = -ENOTCONN; 1139fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1140fba91afbSTrond Myklebust break; 1141262965f5SChuck Lever case -EAGAIN: 1142d0afde5fSTrond Myklebust status = xs_stream_nospace(req, vm_wait); 1143262965f5SChuck Lever break; 1144262965f5SChuck Lever case -ECONNRESET: 1145262965f5SChuck Lever case -ECONNREFUSED: 1146262965f5SChuck Lever case -ENOTCONN: 11473913c78cSTrond Myklebust case -EADDRINUSE: 1148b5872f0cSTrond Myklebust case -ENOBUFS: 1149b9d2bb2eSTrond Myklebust case -EPIPE: 115013331a55STrond Myklebust break; 115113331a55STrond Myklebust default: 115213331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 115313331a55STrond Myklebust -status); 1154a246b010SChuck Lever } 11555fe46e9dSBian Naimeng 1156a246b010SChuck Lever return status; 1157a246b010SChuck Lever } 1158a246b010SChuck Lever 11592a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11602a9e1cfaSTrond Myklebust { 11612a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 11622a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 11632a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 11642118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 11652a9e1cfaSTrond Myklebust } 11662a9e1cfaSTrond Myklebust 11672a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11682a9e1cfaSTrond Myklebust { 11692a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 11702a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 11712a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 11722118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 11732118071dSTrond Myklebust } 11742118071dSTrond Myklebust 117542d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 117642d42a5bSTrond Myklebust { 117742d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 117842d42a5bSTrond Myklebust 117942d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 11804f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 11814f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 11824f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 11832790a624STrond Myklebust clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 11844f8943f8STrond Myklebust } 11854f8943f8STrond Myklebust 11864f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 11874f8943f8STrond Myklebust { 11884f8943f8STrond Myklebust set_bit(nr, &transport->sock_state); 11894f8943f8STrond Myklebust queue_work(xprtiod_workqueue, &transport->error_worker); 119042d42a5bSTrond Myklebust } 119142d42a5bSTrond Myklebust 1192b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1193b70ae915STrond Myklebust { 1194d896ba83STrond Myklebust xprt->connect_cookie++; 1195b70ae915STrond Myklebust smp_mb__before_atomic(); 1196b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1197b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 119842d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1199b70ae915STrond Myklebust smp_mb__after_atomic(); 1200b70ae915STrond Myklebust } 1201b70ae915STrond Myklebust 12022118071dSTrond Myklebust /** 12032118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 12042118071dSTrond Myklebust * @sk: socket 12052118071dSTrond Myklebust * 12062118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 12072118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 12082118071dSTrond Myklebust */ 12092118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 12102118071dSTrond Myklebust { 12114f8943f8STrond Myklebust struct sock_xprt *transport; 12122118071dSTrond Myklebust struct rpc_xprt *xprt; 12132118071dSTrond Myklebust 12142118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1215ea9afca8STrond Myklebust return; 12162118071dSTrond Myklebust 12174f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1218af84537dSBenjamin Coddington transport->xprt_err = -sk->sk_err; 1219af84537dSBenjamin Coddington if (transport->xprt_err == 0) 1220ea9afca8STrond Myklebust return; 12212118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 1222af84537dSBenjamin Coddington xprt, -transport->xprt_err); 1223af84537dSBenjamin Coddington trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); 1224af84537dSBenjamin Coddington 1225af84537dSBenjamin Coddington /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ 1226af84537dSBenjamin Coddington smp_mb__before_atomic(); 12274f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 12282a9e1cfaSTrond Myklebust } 12292a9e1cfaSTrond Myklebust 1230fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1231a246b010SChuck Lever { 1232ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1233ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12346cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1235a73881c9STrond Myklebust struct file *filp = transport->file; 1236a246b010SChuck Lever 1237fe315e76SChuck Lever if (sk == NULL) 1238fe315e76SChuck Lever return; 1239f0043206STrond Myklebust /* 1240f0043206STrond Myklebust * Make sure we're calling this in a context from which it is safe 1241f0043206STrond Myklebust * to call __fput_sync(). In practice that means rpciod and the 1242f0043206STrond Myklebust * system workqueue. 1243f0043206STrond Myklebust */ 1244f0043206STrond Myklebust if (!(current->flags & PF_WQ_WORKER)) { 1245f0043206STrond Myklebust WARN_ON_ONCE(1); 1246f0043206STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 1247f0043206STrond Myklebust return; 1248f0043206STrond Myklebust } 12499903cd1cSChuck Lever 1250264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1251264d1df3SJeff Layton sk_clear_memalloc(sk); 1252264d1df3SJeff Layton 125375eb6af7SChuck Lever tls_handshake_cancel(sk); 125475eb6af7SChuck Lever 125509939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 125609939204STrond Myklebust 1257edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1258ea9afca8STrond Myklebust lock_sock(sk); 1259ee0ac0c2SChuck Lever transport->inet = NULL; 1260ee0ac0c2SChuck Lever transport->sock = NULL; 1261a73881c9STrond Myklebust transport->file = NULL; 1262a246b010SChuck Lever 1263a246b010SChuck Lever sk->sk_user_data = NULL; 12642a9e1cfaSTrond Myklebust 12652a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12660c78789eSTrond Myklebust xprt_clear_connected(xprt); 12676cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1268ae053551STrond Myklebust /* Reset stream record info */ 1269ae053551STrond Myklebust xs_stream_reset_connect(transport); 1270ea9afca8STrond Myklebust release_sock(sk); 1271edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1272a246b010SChuck Lever 12736cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1274f0043206STrond Myklebust __fput_sync(filp); 12750445f92cSTrond Myklebust 12760445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1277fe315e76SChuck Lever } 1278fe315e76SChuck Lever 1279fe315e76SChuck Lever /** 1280fe315e76SChuck Lever * xs_close - close a socket 1281fe315e76SChuck Lever * @xprt: transport 1282fe315e76SChuck Lever * 1283fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1284fe315e76SChuck Lever * on the server we want to save. 1285f75e6745STrond Myklebust * 1286f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1287f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1288fe315e76SChuck Lever */ 1289fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1290fe315e76SChuck Lever { 1291fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1292fe315e76SChuck Lever 1293fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1294fe315e76SChuck Lever 1295fe315e76SChuck Lever xs_reset_transport(transport); 129661d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1297a246b010SChuck Lever } 1298a246b010SChuck Lever 12994a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13004a068258SChuck Lever { 13014a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13024a068258SChuck Lever xprt); 13034a068258SChuck Lever xprt_disconnect_done(xprt); 13044a068258SChuck Lever } 13054a068258SChuck Lever 1306315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1307315f3812SKinglong Mee { 1308315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1309315f3812SKinglong Mee xprt_free(xprt); 1310315f3812SKinglong Mee } 1311315f3812SKinglong Mee 13129903cd1cSChuck Lever /** 13139903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13149903cd1cSChuck Lever * @xprt: doomed transport 13159903cd1cSChuck Lever * 13169903cd1cSChuck Lever */ 13179903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1318a246b010SChuck Lever { 131903c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 132003c78827STrond Myklebust struct sock_xprt, xprt); 13219903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13229903cd1cSChuck Lever 132303c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1324a1311d87STrond Myklebust xs_close(xprt); 1325edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1326b5e92419STrond Myklebust cancel_work_sync(&transport->error_worker); 1327315f3812SKinglong Mee xs_xprt_free(xprt); 1328a1311d87STrond Myklebust module_put(THIS_MODULE); 1329a246b010SChuck Lever } 1330a246b010SChuck Lever 13319903cd1cSChuck Lever /** 1332f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1333f9b2ee71STrond Myklebust * @xprt: transport 1334f9b2ee71STrond Myklebust * @sk: socket 1335f9b2ee71STrond Myklebust * @skb: skbuff 13369903cd1cSChuck Lever * 1337a246b010SChuck Lever */ 1338f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1339f9b2ee71STrond Myklebust struct sock *sk, 1340f9b2ee71STrond Myklebust struct sk_buff *skb) 1341a246b010SChuck Lever { 1342a246b010SChuck Lever struct rpc_task *task; 1343a246b010SChuck Lever struct rpc_rqst *rovr; 1344f9b2ee71STrond Myklebust int repsize, copied; 1345d8ed029dSAlexey Dobriyan u32 _xid; 1346d8ed029dSAlexey Dobriyan __be32 *xp; 1347a246b010SChuck Lever 13481da8c681SWillem de Bruijn repsize = skb->len; 1349a246b010SChuck Lever if (repsize < 4) { 13509903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1351f9b2ee71STrond Myklebust return; 1352a246b010SChuck Lever } 1353a246b010SChuck Lever 1354a246b010SChuck Lever /* Copy the XID from the skb... */ 13551da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1356a246b010SChuck Lever if (xp == NULL) 1357f9b2ee71STrond Myklebust return; 1358a246b010SChuck Lever 1359a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 136075c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1361a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1362a246b010SChuck Lever if (!rovr) 1363a246b010SChuck Lever goto out_unlock; 1364729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1365ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 136675c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1367a246b010SChuck Lever task = rovr->rq_task; 1368a246b010SChuck Lever 1369a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1370a246b010SChuck Lever copied = repsize; 1371a246b010SChuck Lever 1372a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 13731781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 137475c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13750afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1376729749bbSTrond Myklebust goto out_unpin; 13771781f7f5SHerbert Xu } 13781781f7f5SHerbert Xu 1379a246b010SChuck Lever 1380b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 13816a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1382b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 138375c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13841570c1e4SChuck Lever xprt_complete_rqst(task, copied); 13850afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1386729749bbSTrond Myklebust out_unpin: 1387729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1388a246b010SChuck Lever out_unlock: 138975c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1390f9b2ee71STrond Myklebust } 1391f9b2ee71STrond Myklebust 1392f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1393f9b2ee71STrond Myklebust { 1394f9b2ee71STrond Myklebust struct sk_buff *skb; 1395f9b2ee71STrond Myklebust struct sock *sk; 1396f9b2ee71STrond Myklebust int err; 1397f9b2ee71STrond Myklebust 1398f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1399f9b2ee71STrond Myklebust sk = transport->inet; 1400f9b2ee71STrond Myklebust if (sk == NULL) 1401f9b2ee71STrond Myklebust goto out; 1402f9b2ee71STrond Myklebust for (;;) { 1403ec095263SOliver Hartkopp skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 14044f546149STrond Myklebust if (skb == NULL) 14054f546149STrond Myklebust break; 1406f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1407850cbaddSPaolo Abeni consume_skb(skb); 14080af3442aSTrond Myklebust cond_resched(); 1409f9b2ee71STrond Myklebust } 14100ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1411a246b010SChuck Lever out: 1412f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1413f9b2ee71STrond Myklebust } 1414f9b2ee71STrond Myklebust 1415f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1416f9b2ee71STrond Myklebust { 1417f9b2ee71STrond Myklebust struct sock_xprt *transport = 1418f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1419a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1420a1231fdaSTrond Myklebust 1421f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1422a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1423f9b2ee71STrond Myklebust } 1424f9b2ee71STrond Myklebust 1425f9b2ee71STrond Myklebust /** 1426f67939e4SChuck Lever * xs_data_ready - "data ready" callback for sockets 1427f9b2ee71STrond Myklebust * @sk: socket with data to read 1428f9b2ee71STrond Myklebust * 1429f9b2ee71STrond Myklebust */ 1430f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1431f9b2ee71STrond Myklebust { 1432f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1433f9b2ee71STrond Myklebust 143440e0b090SPeilin Ye trace_sk_data_ready(sk); 143540e0b090SPeilin Ye 1436f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1437f9b2ee71STrond Myklebust if (xprt != NULL) { 1438f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1439f9b2ee71STrond Myklebust struct sock_xprt, xprt); 1440f67939e4SChuck Lever 1441f67939e4SChuck Lever trace_xs_data_ready(xprt); 1442f67939e4SChuck Lever 14435157b956STrond Myklebust transport->old_data_ready(sk); 14440d3ca07fSChuck Lever 14450d3ca07fSChuck Lever if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 14460d3ca07fSChuck Lever return; 14470d3ca07fSChuck Lever 14485157b956STrond Myklebust /* Any data means we had a useful conversation, so 14495157b956STrond Myklebust * then we don't need to delay the next reconnect 14505157b956STrond Myklebust */ 14515157b956STrond Myklebust if (xprt->reestablish_timeout) 14525157b956STrond Myklebust xprt->reestablish_timeout = 0; 145342d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 145440a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1455f9b2ee71STrond Myklebust } 1456a246b010SChuck Lever } 1457a246b010SChuck Lever 1458a519fc7aSTrond Myklebust /* 1459a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1460a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1461a519fc7aSTrond Myklebust */ 1462a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1463a519fc7aSTrond Myklebust { 1464a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1465a519fc7aSTrond Myklebust } 1466a519fc7aSTrond Myklebust 14679e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14686b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14696b26cc8cSChuck Lever { 14706b26cc8cSChuck Lever return PAGE_SIZE; 14716b26cc8cSChuck Lever } 14729e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 147344b98efdSRicardo Labiaga 14749903cd1cSChuck Lever /** 1475efce2d0bSTrond Myklebust * xs_local_state_change - callback to handle AF_LOCAL socket state changes 1476efce2d0bSTrond Myklebust * @sk: socket whose state has changed 1477efce2d0bSTrond Myklebust * 1478efce2d0bSTrond Myklebust */ 1479efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk) 1480efce2d0bSTrond Myklebust { 1481efce2d0bSTrond Myklebust struct rpc_xprt *xprt; 1482efce2d0bSTrond Myklebust struct sock_xprt *transport; 1483efce2d0bSTrond Myklebust 1484efce2d0bSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1485efce2d0bSTrond Myklebust return; 1486efce2d0bSTrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1487efce2d0bSTrond Myklebust if (sk->sk_shutdown & SHUTDOWN_MASK) { 1488efce2d0bSTrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1489efce2d0bSTrond Myklebust /* Trigger the socket release */ 1490efce2d0bSTrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1491efce2d0bSTrond Myklebust } 1492efce2d0bSTrond Myklebust } 1493efce2d0bSTrond Myklebust 1494efce2d0bSTrond Myklebust /** 14959903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14969903cd1cSChuck Lever * @sk: socket whose state has changed 14979903cd1cSChuck Lever * 14989903cd1cSChuck Lever */ 14999903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1500a246b010SChuck Lever { 1501a246b010SChuck Lever struct rpc_xprt *xprt; 15020fdea1e8STrond Myklebust struct sock_xprt *transport; 1503a246b010SChuck Lever 1504a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1505ea9afca8STrond Myklebust return; 15069903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1507669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1508a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1509a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1510669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1511669502ffSAndy Chittenden sk->sk_shutdown); 1512a246b010SChuck Lever 15130fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 151440b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1515a246b010SChuck Lever switch (sk->sk_state) { 1516a246b010SChuck Lever case TCP_ESTABLISHED: 1517a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15188b71798cSTrond Myklebust xprt->connect_cookie++; 15190fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15200fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 152151971139SChuck Lever 15223968a8a5SChuck Lever xprt->stat.connect_count++; 15233968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15243968a8a5SChuck Lever xprt->stat.connect_start; 15254f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1526a246b010SChuck Lever } 1527a246b010SChuck Lever break; 15283b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15293b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15307c1d71cfSTrond Myklebust xprt->connect_cookie++; 1531663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15323b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15334e857c58SPeter Zijlstra smp_mb__before_atomic(); 15343b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1535ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15364e857c58SPeter Zijlstra smp_mb__after_atomic(); 1537a246b010SChuck Lever break; 1538632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15393b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15407c1d71cfSTrond Myklebust xprt->connect_cookie++; 1541d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15424f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1543df561f66SGustavo A. R. Silva fallthrough; 1544663b8858STrond Myklebust case TCP_CLOSING: 1545663b8858STrond Myklebust /* 1546663b8858STrond Myklebust * If the server closed down the connection, make sure that 1547663b8858STrond Myklebust * we back off before reconnecting 1548663b8858STrond Myklebust */ 1549663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1550663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15513b948ae5STrond Myklebust break; 15523b948ae5STrond Myklebust case TCP_LAST_ACK: 1553670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15544e857c58SPeter Zijlstra smp_mb__before_atomic(); 15553b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15564e857c58SPeter Zijlstra smp_mb__after_atomic(); 15573b948ae5STrond Myklebust break; 15583b948ae5STrond Myklebust case TCP_CLOSE: 15590fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15600fdea1e8STrond Myklebust &transport->sock_state)) 15610fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15629b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15639b30889cSTrond Myklebust /* Trigger the socket release */ 15644f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1565a246b010SChuck Lever } 1566a246b010SChuck Lever } 1567a246b010SChuck Lever 15681f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15691f0fa154SIlpo Järvinen { 15704f8943f8STrond Myklebust struct sock_xprt *transport; 15711f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15721f0fa154SIlpo Järvinen 157313331a55STrond Myklebust if (!sk->sk_socket) 15741f0fa154SIlpo Järvinen return; 157513331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15761f0fa154SIlpo Järvinen 15771f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15781f0fa154SIlpo Järvinen return; 15794f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 15802790a624STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state)) 15812790a624STrond Myklebust return; 15824f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1583c544577dSTrond Myklebust sk->sk_write_pending--; 15841f0fa154SIlpo Järvinen } 15851f0fa154SIlpo Järvinen 15862a9e1cfaSTrond Myklebust /** 1587c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1588c7b2cae8SChuck Lever * becomes available 15899903cd1cSChuck Lever * @sk: socket whose state has changed 15909903cd1cSChuck Lever * 1591a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1592a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1593c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1594a246b010SChuck Lever * with a bunch of small requests. 1595a246b010SChuck Lever */ 1596c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1597a246b010SChuck Lever { 1598c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 15991f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16001f0fa154SIlpo Järvinen xs_write_space(sk); 1601c7b2cae8SChuck Lever } 1602c7b2cae8SChuck Lever 1603c7b2cae8SChuck Lever /** 1604c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1605c7b2cae8SChuck Lever * becomes available 1606c7b2cae8SChuck Lever * @sk: socket whose state has changed 1607c7b2cae8SChuck Lever * 1608c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1609c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1610c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1611c7b2cae8SChuck Lever * with a bunch of small requests. 1612c7b2cae8SChuck Lever */ 1613c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1614c7b2cae8SChuck Lever { 1615c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 161664dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16171f0fa154SIlpo Järvinen xs_write_space(sk); 1618a246b010SChuck Lever } 1619a246b010SChuck Lever 1620470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1621a246b010SChuck Lever { 1622ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1623ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1624a246b010SChuck Lever 16257c6e066eSChuck Lever if (transport->rcvsize) { 1626a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16277c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1628a246b010SChuck Lever } 16297c6e066eSChuck Lever if (transport->sndsize) { 1630a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16317c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1632a246b010SChuck Lever sk->sk_write_space(sk); 1633a246b010SChuck Lever } 1634a246b010SChuck Lever } 1635a246b010SChuck Lever 163643118c29SChuck Lever /** 1637470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 163843118c29SChuck Lever * @xprt: generic transport 1639470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1640470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 164143118c29SChuck Lever * 1642470056c2SChuck Lever * Set socket send and receive buffer size limits. 164343118c29SChuck Lever */ 1644470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 164543118c29SChuck Lever { 16467c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16477c6e066eSChuck Lever 16487c6e066eSChuck Lever transport->sndsize = 0; 1649470056c2SChuck Lever if (sndsize) 16507c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16517c6e066eSChuck Lever transport->rcvsize = 0; 1652470056c2SChuck Lever if (rcvsize) 16537c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1654470056c2SChuck Lever 1655470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 165643118c29SChuck Lever } 165743118c29SChuck Lever 165846c0ee8bSChuck Lever /** 165946c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1660acf0a39fSChuck Lever * @xprt: controlling transport 166146c0ee8bSChuck Lever * @task: task that timed out 166246c0ee8bSChuck Lever * 166346c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 166446c0ee8bSChuck Lever */ 16656a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 166646c0ee8bSChuck Lever { 1667b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 16686a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1669b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 167046c0ee8bSChuck Lever } 167146c0ee8bSChuck Lever 1672826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1673b85d8806SChuck Lever { 1674826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1675826799e6SJ. Bruce Fields unsigned short range; 1676826799e6SJ. Bruce Fields unsigned short rand; 1677826799e6SJ. Bruce Fields 1678826799e6SJ. Bruce Fields if (max < min) 1679826799e6SJ. Bruce Fields return -EADDRINUSE; 1680826799e6SJ. Bruce Fields range = max - min + 1; 16818032bf12SJason A. Donenfeld rand = get_random_u32_below(range); 1682826799e6SJ. Bruce Fields return rand + min; 1683b85d8806SChuck Lever } 1684b85d8806SChuck Lever 16854dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 16864dda9c8aSTrond Myklebust { 16874dda9c8aSTrond Myklebust struct sockaddr_storage buf; 16884dda9c8aSTrond Myklebust unsigned short port = 0; 16894dda9c8aSTrond Myklebust 16909b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 16914dda9c8aSTrond Myklebust goto out; 16924dda9c8aSTrond Myklebust switch (buf.ss_family) { 16934dda9c8aSTrond Myklebust case AF_INET6: 16944dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 16954dda9c8aSTrond Myklebust break; 16964dda9c8aSTrond Myklebust case AF_INET: 16974dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 16984dda9c8aSTrond Myklebust } 16994dda9c8aSTrond Myklebust out: 17004dda9c8aSTrond Myklebust return port; 17014dda9c8aSTrond Myklebust } 17024dda9c8aSTrond Myklebust 17034dda9c8aSTrond Myklebust /** 170492200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 170592200412SChuck Lever * @xprt: generic transport 170692200412SChuck Lever * @port: new port number 170792200412SChuck Lever * 170892200412SChuck Lever */ 170992200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 171092200412SChuck Lever { 171192200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1712c4efcb1dSChuck Lever 17139dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17149dc3b095SChuck Lever xs_update_peer_port(xprt); 171592200412SChuck Lever } 171692200412SChuck Lever 17174dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17184dda9c8aSTrond Myklebust { 1719e6237b6fSTrond Myklebust if (transport->srcport == 0 && transport->xprt.reuseport) 17204dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17214dda9c8aSTrond Myklebust } 17224dda9c8aSTrond Myklebust 1723826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 172467a391d7STrond Myklebust { 1725826799e6SJ. Bruce Fields int port = transport->srcport; 172667a391d7STrond Myklebust 172767a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 172867a391d7STrond Myklebust port = xs_get_random_port(); 172967a391d7STrond Myklebust return port; 173067a391d7STrond Myklebust } 173167a391d7STrond Myklebust 1732421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt) 1733a8482488SOlga Kornievskaia { 1734a8482488SOlga Kornievskaia struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1735b49ea673SNeilBrown unsigned short ret = 0; 1736b49ea673SNeilBrown mutex_lock(&sock->recv_mutex); 1737b49ea673SNeilBrown if (sock->sock) 1738b49ea673SNeilBrown ret = xs_sock_getport(sock->sock); 1739b49ea673SNeilBrown mutex_unlock(&sock->recv_mutex); 1740b49ea673SNeilBrown return ret; 1741a8482488SOlga Kornievskaia } 1742421ab1beSTrond Myklebust 1743421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen) 1744421ab1beSTrond Myklebust { 1745421ab1beSTrond Myklebust struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1746421ab1beSTrond Myklebust union { 1747421ab1beSTrond Myklebust struct sockaddr sa; 1748421ab1beSTrond Myklebust struct sockaddr_storage st; 1749421ab1beSTrond Myklebust } saddr; 1750421ab1beSTrond Myklebust int ret = -ENOTCONN; 1751421ab1beSTrond Myklebust 1752421ab1beSTrond Myklebust mutex_lock(&sock->recv_mutex); 1753421ab1beSTrond Myklebust if (sock->sock) { 1754421ab1beSTrond Myklebust ret = kernel_getsockname(sock->sock, &saddr.sa); 1755421ab1beSTrond Myklebust if (ret >= 0) 1756421ab1beSTrond Myklebust ret = snprintf(buf, buflen, "%pISc", &saddr.sa); 1757421ab1beSTrond Myklebust } 1758421ab1beSTrond Myklebust mutex_unlock(&sock->recv_mutex); 1759421ab1beSTrond Myklebust return ret; 1760421ab1beSTrond Myklebust } 1761a8482488SOlga Kornievskaia 1762baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 176367a391d7STrond Myklebust { 1764fbfffbd5SChuck Lever if (transport->srcport != 0) 1765fbfffbd5SChuck Lever transport->srcport = 0; 176667a391d7STrond Myklebust if (!transport->xprt.resvport) 176767a391d7STrond Myklebust return 0; 176867a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 176967a391d7STrond Myklebust return xprt_max_resvport; 177067a391d7STrond Myklebust return --port; 177167a391d7STrond Myklebust } 1772beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1773a246b010SChuck Lever { 1774beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 177567a391d7STrond Myklebust int err, nloop = 0; 1776826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 177767a391d7STrond Myklebust unsigned short last; 1778a246b010SChuck Lever 17790f7a622cSChris Perl /* 17800f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17810f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17820f7a622cSChris Perl * port selection happen implicitly when the socket is used 17830f7a622cSChris Perl * (for example at connect time). 17840f7a622cSChris Perl * 17850f7a622cSChris Perl * This ensures that we can continue to establish TCP 17860f7a622cSChris Perl * connections even when all local ephemeral ports are already 17870f7a622cSChris Perl * a part of some TCP connection. This makes no difference 178812b20ce3SBhaskar Chowdhury * for UDP sockets, but also doesn't harm them. 17890f7a622cSChris Perl * 17900f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17910f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17920f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17930f7a622cSChris Perl */ 1794826799e6SJ. Bruce Fields if (port <= 0) 1795826799e6SJ. Bruce Fields return port; 17960f7a622cSChris Perl 1797beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1798a246b010SChuck Lever do { 1799beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1800e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1801beb59b68SPavel Emelyanov transport->xprt.addrlen); 1802a246b010SChuck Lever if (err == 0) { 1803bc1c56e9SNeilBrown if (transport->xprt.reuseport) 1804fbfffbd5SChuck Lever transport->srcport = port; 1805d3bc9a1dSFrank van Maarseveen break; 1806a246b010SChuck Lever } 180767a391d7STrond Myklebust last = port; 1808baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 180967a391d7STrond Myklebust if (port > last) 181067a391d7STrond Myklebust nloop++; 181167a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1812beb59b68SPavel Emelyanov 18134232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1814beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1815beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1816beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1817beb59b68SPavel Emelyanov else 1818beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1819beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18207dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1821a246b010SChuck Lever return err; 1822a246b010SChuck Lever } 1823a246b010SChuck Lever 1824176e21eeSChuck Lever /* 1825176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1826176e21eeSChuck Lever */ 1827176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1828176e21eeSChuck Lever { 1829fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1830176e21eeSChuck Lever } 1831176e21eeSChuck Lever 1832176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1833176e21eeSChuck Lever { 1834176e21eeSChuck Lever } 1835a246b010SChuck Lever 1836ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1837064a9177SNeilBrown static struct lock_class_key xs_key[3]; 1838064a9177SNeilBrown static struct lock_class_key xs_slock_key[3]; 1839ed07536eSPeter Zijlstra 1840176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1841176e21eeSChuck Lever { 1842176e21eeSChuck Lever struct sock *sk = sock->sk; 1843176e21eeSChuck Lever 1844176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1845064a9177SNeilBrown &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]); 1846176e21eeSChuck Lever } 1847176e21eeSChuck Lever 18488945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1849ed07536eSPeter Zijlstra { 1850ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18518945ee5eSChuck Lever 18528945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 1853064a9177SNeilBrown &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]); 1854ed07536eSPeter Zijlstra } 18558945ee5eSChuck Lever 18568945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18578945ee5eSChuck Lever { 18588945ee5eSChuck Lever struct sock *sk = sock->sk; 18598945ee5eSChuck Lever 18608945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 1861064a9177SNeilBrown &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]); 1862ed07536eSPeter Zijlstra } 18636bc9638aSPavel Emelyanov 18646bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18656bc9638aSPavel Emelyanov { 1866fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18671b7a1819SWeston Andros Adamson return; 18681b7a1819SWeston Andros Adamson 18694232e863SChuck Lever switch (family) { 1870176e21eeSChuck Lever case AF_LOCAL: 1871176e21eeSChuck Lever xs_reclassify_socketu(sock); 1872176e21eeSChuck Lever break; 18734232e863SChuck Lever case AF_INET: 18746bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18754232e863SChuck Lever break; 18764232e863SChuck Lever case AF_INET6: 18776bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18784232e863SChuck Lever break; 18794232e863SChuck Lever } 18806bc9638aSPavel Emelyanov } 1881ed07536eSPeter Zijlstra #else 18826bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18836bc9638aSPavel Emelyanov { 18846bc9638aSPavel Emelyanov } 1885ed07536eSPeter Zijlstra #endif 1886ed07536eSPeter Zijlstra 188793dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 188893dc41bdSNeilBrown { 188993dc41bdSNeilBrown } 189093dc41bdSNeilBrown 18916bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18924dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18934dda9c8aSTrond Myklebust int protocol, bool reuseport) 189422f79326SPavel Emelyanov { 1895a73881c9STrond Myklebust struct file *filp; 189622f79326SPavel Emelyanov struct socket *sock; 189722f79326SPavel Emelyanov int err; 189822f79326SPavel Emelyanov 18996bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 190022f79326SPavel Emelyanov if (err < 0) { 190122f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 190222f79326SPavel Emelyanov protocol, -err); 190322f79326SPavel Emelyanov goto out; 190422f79326SPavel Emelyanov } 19056bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 190622f79326SPavel Emelyanov 19074dda9c8aSTrond Myklebust if (reuseport) 1908fe31a326SChristoph Hellwig sock_set_reuseport(sock->sk); 19094dda9c8aSTrond Myklebust 19104cea288aSBen Hutchings err = xs_bind(transport, sock); 19114cea288aSBen Hutchings if (err) { 191222f79326SPavel Emelyanov sock_release(sock); 191322f79326SPavel Emelyanov goto out; 191422f79326SPavel Emelyanov } 191522f79326SPavel Emelyanov 1916a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1917a73881c9STrond Myklebust if (IS_ERR(filp)) 1918a73881c9STrond Myklebust return ERR_CAST(filp); 1919a73881c9STrond Myklebust transport->file = filp; 1920a73881c9STrond Myklebust 192122f79326SPavel Emelyanov return sock; 192222f79326SPavel Emelyanov out: 192322f79326SPavel Emelyanov return ERR_PTR(err); 192422f79326SPavel Emelyanov } 192522f79326SPavel Emelyanov 1926176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1927176e21eeSChuck Lever struct socket *sock) 1928176e21eeSChuck Lever { 1929176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1930176e21eeSChuck Lever xprt); 1931176e21eeSChuck Lever 1932176e21eeSChuck Lever if (!transport->inet) { 1933176e21eeSChuck Lever struct sock *sk = sock->sk; 1934176e21eeSChuck Lever 1935ea9afca8STrond Myklebust lock_sock(sk); 1936176e21eeSChuck Lever 1937176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1938176e21eeSChuck Lever 1939176e21eeSChuck Lever sk->sk_user_data = xprt; 1940a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1941176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1942efce2d0bSTrond Myklebust sk->sk_state_change = xs_local_state_change; 19432118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 194498123866SBenjamin Coddington sk->sk_use_task_frag = false; 1945176e21eeSChuck Lever 1946176e21eeSChuck Lever xprt_clear_connected(xprt); 1947176e21eeSChuck Lever 1948176e21eeSChuck Lever /* Reset to new socket */ 1949176e21eeSChuck Lever transport->sock = sock; 1950176e21eeSChuck Lever transport->inet = sk; 1951176e21eeSChuck Lever 1952ea9afca8STrond Myklebust release_sock(sk); 1953176e21eeSChuck Lever } 1954176e21eeSChuck Lever 1955ae053551STrond Myklebust xs_stream_start_connect(transport); 19566c7a64e5STrond Myklebust 1957176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1958176e21eeSChuck Lever } 1959176e21eeSChuck Lever 1960176e21eeSChuck Lever /** 1961176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1962176e21eeSChuck Lever * @transport: socket transport to connect 1963176e21eeSChuck Lever */ 1964dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1965176e21eeSChuck Lever { 1966176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1967a73881c9STrond Myklebust struct file *filp; 1968176e21eeSChuck Lever struct socket *sock; 196968e9a246SColin Ian King int status; 1970176e21eeSChuck Lever 1971176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1972176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1973176e21eeSChuck Lever if (status < 0) { 1974176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1975176e21eeSChuck Lever "transport socket (%d).\n", -status); 1976176e21eeSChuck Lever goto out; 1977176e21eeSChuck Lever } 1978d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1979176e21eeSChuck Lever 1980a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1981a73881c9STrond Myklebust if (IS_ERR(filp)) { 1982a73881c9STrond Myklebust status = PTR_ERR(filp); 1983a73881c9STrond Myklebust goto out; 1984a73881c9STrond Myklebust } 1985a73881c9STrond Myklebust transport->file = filp; 1986a73881c9STrond Myklebust 1987176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1988176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1989176e21eeSChuck Lever 1990176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 199140b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1992176e21eeSChuck Lever switch (status) { 1993176e21eeSChuck Lever case 0: 1994176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1995176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19963968a8a5SChuck Lever xprt->stat.connect_count++; 19973968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 19983968a8a5SChuck Lever xprt->stat.connect_start; 1999176e21eeSChuck Lever xprt_set_connected(xprt); 200093f479d3SGustavo A. R. Silva break; 20013601c4a9STrond Myklebust case -ENOBUFS: 2002176e21eeSChuck Lever break; 2003176e21eeSChuck Lever case -ENOENT: 2004176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2005176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2006176e21eeSChuck Lever break; 20074a20a988STrond Myklebust case -ECONNREFUSED: 20084a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20094a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20104a20a988STrond Myklebust break; 2011176e21eeSChuck Lever default: 2012176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2013176e21eeSChuck Lever __func__, -status, 2014176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2015176e21eeSChuck Lever } 2016176e21eeSChuck Lever 2017176e21eeSChuck Lever out: 2018176e21eeSChuck Lever xprt_clear_connecting(xprt); 2019176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2020dc107402SJ. Bruce Fields return status; 2021dc107402SJ. Bruce Fields } 2022dc107402SJ. Bruce Fields 2023b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2024dc107402SJ. Bruce Fields { 2025dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2026dc107402SJ. Bruce Fields int ret; 2027dc107402SJ. Bruce Fields 2028aad41a7dSTrond Myklebust if (transport->file) 2029aad41a7dSTrond Myklebust goto force_disconnect; 2030aad41a7dSTrond Myklebust 2031dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2032dc107402SJ. Bruce Fields /* 2033dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2034dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2035dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2036dc107402SJ. Bruce Fields * 2037dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2038dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2039dc107402SJ. Bruce Fields * connect. 2040dc107402SJ. Bruce Fields */ 204139494194STrond Myklebust rpc_task_set_rpc_status(task, -ENOTCONN); 2042aad41a7dSTrond Myklebust goto out_wake; 2043dc107402SJ. Bruce Fields } 2044dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2045dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2046dc107402SJ. Bruce Fields msleep_interruptible(15000); 2047aad41a7dSTrond Myklebust return; 2048aad41a7dSTrond Myklebust force_disconnect: 2049aad41a7dSTrond Myklebust xprt_force_disconnect(xprt); 2050aad41a7dSTrond Myklebust out_wake: 2051aad41a7dSTrond Myklebust xprt_clear_connecting(xprt); 2052aad41a7dSTrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 2053176e21eeSChuck Lever } 2054176e21eeSChuck Lever 20553c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2056d6e971d8SJeff Layton /* 2057693486d5SNeilBrown * Note that this should be called with XPRT_LOCKED held, or recv_mutex 2058693486d5SNeilBrown * held, or when we otherwise know that we have exclusive access to the 2059693486d5SNeilBrown * socket, to guard against races with xs_reset_transport. 2060d6e971d8SJeff Layton */ 2061a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2062a564b8f0SMel Gorman { 2063a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2064a564b8f0SMel Gorman xprt); 2065a564b8f0SMel Gorman 2066d6e971d8SJeff Layton /* 2067d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2068d6e971d8SJeff Layton * reconnecting process will get it for us. 2069d6e971d8SJeff Layton */ 2070d6e971d8SJeff Layton if (!transport->inet) 2071d6e971d8SJeff Layton return; 20728e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2073a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2074a564b8f0SMel Gorman } 2075a564b8f0SMel Gorman 2076a564b8f0SMel Gorman /** 2077d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2078a564b8f0SMel Gorman * @xprt: transport to tag 2079a564b8f0SMel Gorman * 20808e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20818e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2082a564b8f0SMel Gorman */ 2083d67fa4d8SJeff Layton static int 2084d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2085a564b8f0SMel Gorman { 2086d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2087a564b8f0SMel Gorman 2088693486d5SNeilBrown mutex_lock(&xs->recv_mutex); 2089693486d5SNeilBrown if (atomic_inc_return(&xprt->swapper) == 1 && 2090693486d5SNeilBrown xs->inet) 2091d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2092693486d5SNeilBrown mutex_unlock(&xs->recv_mutex); 20938e228133SJeff Layton return 0; 2094a564b8f0SMel Gorman } 2095a564b8f0SMel Gorman 20968e228133SJeff Layton /** 2097d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20988e228133SJeff Layton * @xprt: transport to tag 20998e228133SJeff Layton * 21008e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 21018e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 21028e228133SJeff Layton */ 2103d67fa4d8SJeff Layton static void 2104d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21058e228133SJeff Layton { 2106d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21078e228133SJeff Layton 2108693486d5SNeilBrown mutex_lock(&xs->recv_mutex); 2109693486d5SNeilBrown if (atomic_dec_and_test(&xprt->swapper) && 2110693486d5SNeilBrown xs->inet) 2111d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2112693486d5SNeilBrown mutex_unlock(&xs->recv_mutex); 2113a564b8f0SMel Gorman } 2114a564b8f0SMel Gorman #else 2115a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2116a564b8f0SMel Gorman { 2117a564b8f0SMel Gorman } 2118d67fa4d8SJeff Layton 2119d67fa4d8SJeff Layton static int 2120d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2121d67fa4d8SJeff Layton { 2122d67fa4d8SJeff Layton return -EINVAL; 2123d67fa4d8SJeff Layton } 2124d67fa4d8SJeff Layton 2125d67fa4d8SJeff Layton static void 2126d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2127d67fa4d8SJeff Layton { 2128d67fa4d8SJeff Layton } 2129a564b8f0SMel Gorman #endif 2130a564b8f0SMel Gorman 213116be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2132a246b010SChuck Lever { 213316be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2134edb267a6SChuck Lever 2135ee0ac0c2SChuck Lever if (!transport->inet) { 2136b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2137b0d93ad5SChuck Lever 2138ea9afca8STrond Myklebust lock_sock(sk); 2139b0d93ad5SChuck Lever 21402a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21412a9e1cfaSTrond Myklebust 2142b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2143f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2144b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 214598123866SBenjamin Coddington sk->sk_use_task_frag = false; 2146b0d93ad5SChuck Lever 2147b0d93ad5SChuck Lever xprt_set_connected(xprt); 2148b0d93ad5SChuck Lever 2149b0d93ad5SChuck Lever /* Reset to new socket */ 2150ee0ac0c2SChuck Lever transport->sock = sock; 2151ee0ac0c2SChuck Lever transport->inet = sk; 2152b0d93ad5SChuck Lever 2153a564b8f0SMel Gorman xs_set_memalloc(xprt); 2154a564b8f0SMel Gorman 2155ea9afca8STrond Myklebust release_sock(sk); 2156b0d93ad5SChuck Lever } 2157470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 215802910177STrond Myklebust 215902910177STrond Myklebust xprt->stat.connect_start = jiffies; 216016be2d20SChuck Lever } 216116be2d20SChuck Lever 21628c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2163a246b010SChuck Lever { 2164a246b010SChuck Lever struct sock_xprt *transport = 2165a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2166a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2167d099b8afSColin Ian King struct socket *sock; 2168b65c0310SPavel Emelyanov int status = -EIO; 21698db55a03SNeilBrown unsigned int pflags = current->flags; 2170a246b010SChuck Lever 21718db55a03SNeilBrown if (atomic_read(&xprt->swapper)) 21728db55a03SNeilBrown current->flags |= PF_MEMALLOC; 21738c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21744dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21754dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2176b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2177a246b010SChuck Lever goto out; 217868e220bdSChuck Lever 2179c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2180c740eff8SChuck Lever "%s (port %s)\n", xprt, 2181c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2182c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2183c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 218468e220bdSChuck Lever 218568e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 218640b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2187a246b010SChuck Lever status = 0; 2188b0d93ad5SChuck Lever out: 2189b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2190cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 21917d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 21928db55a03SNeilBrown current_restore_flags(pflags, PF_MEMALLOC); 2193b0d93ad5SChuck Lever } 2194b0d93ad5SChuck Lever 21954876cc77STrond Myklebust /** 21964876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21974876cc77STrond Myklebust * @xprt: transport 21984876cc77STrond Myklebust * 21994876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 22004876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 22014876cc77STrond Myklebust */ 22024876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 22034876cc77STrond Myklebust { 22044876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22054876cc77STrond Myklebust struct socket *sock = transport->sock; 22069b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22074876cc77STrond Myklebust 22084876cc77STrond Myklebust if (sock == NULL) 22094876cc77STrond Myklebust return; 22100a6ff58eSTrond Myklebust if (!xprt->reuseport) { 22110a6ff58eSTrond Myklebust xs_close(xprt); 22120a6ff58eSTrond Myklebust return; 22130a6ff58eSTrond Myklebust } 22149b30889cSTrond Myklebust switch (skst) { 22157c81e6a9STrond Myklebust case TCP_FIN_WAIT1: 22167c81e6a9STrond Myklebust case TCP_FIN_WAIT2: 2217943d045aSSiddharth Kawar case TCP_LAST_ACK: 22187c81e6a9STrond Myklebust break; 22197c81e6a9STrond Myklebust case TCP_ESTABLISHED: 22207c81e6a9STrond Myklebust case TCP_CLOSE_WAIT: 22214876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22224876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22239b30889cSTrond Myklebust break; 22247c81e6a9STrond Myklebust default: 22254876cc77STrond Myklebust xs_reset_transport(transport); 22264876cc77STrond Myklebust } 22279b30889cSTrond Myklebust } 22284876cc77STrond Myklebust 22298d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22308d1b8c62STrond Myklebust struct socket *sock) 2231b0d93ad5SChuck Lever { 223216be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22333a107f07STrond Myklebust struct net *net = sock_net(sock->sk); 22343a107f07STrond Myklebust unsigned long connect_timeout; 22353a107f07STrond Myklebust unsigned long syn_retries; 22367196dbb0STrond Myklebust unsigned int keepidle; 22377196dbb0STrond Myklebust unsigned int keepcnt; 2238775f06abSTrond Myklebust unsigned int timeo; 22393a107f07STrond Myklebust unsigned long t; 22407f260e85STrond Myklebust 2241b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22427196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22437196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22447196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22457196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22467196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2247b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22487f260e85STrond Myklebust 22497f260e85STrond Myklebust /* TCP Keepalive options */ 2250ce3d9544SChristoph Hellwig sock_set_keepalive(sock->sk); 225171c48eb8SChristoph Hellwig tcp_sock_set_keepidle(sock->sk, keepidle); 2252d41ecaacSChristoph Hellwig tcp_sock_set_keepintvl(sock->sk, keepidle); 2253480aeb96SChristoph Hellwig tcp_sock_set_keepcnt(sock->sk, keepcnt); 2254b0d93ad5SChuck Lever 22558d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 2256c488aeadSChristoph Hellwig tcp_sock_set_user_timeout(sock->sk, timeo); 22573a107f07STrond Myklebust 22583a107f07STrond Myklebust /* Connect timeout */ 22593a107f07STrond Myklebust connect_timeout = max_t(unsigned long, 22603a107f07STrond Myklebust DIV_ROUND_UP(xprt->connect_timeout, HZ), 1); 22613a107f07STrond Myklebust syn_retries = max_t(unsigned long, 22623a107f07STrond Myklebust READ_ONCE(net->ipv4.sysctl_tcp_syn_retries), 1); 22633a107f07STrond Myklebust for (t = 0; t <= syn_retries && (1UL << t) < connect_timeout; t++) 22643a107f07STrond Myklebust ; 22653a107f07STrond Myklebust if (t <= syn_retries) 22663a107f07STrond Myklebust tcp_sock_set_syncnt(sock->sk, t - 1); 22678d1b8c62STrond Myklebust } 22688d1b8c62STrond Myklebust 22693e6ff89dSTrond Myklebust static void xs_tcp_do_set_connect_timeout(struct rpc_xprt *xprt, 22703e6ff89dSTrond Myklebust unsigned long connect_timeout) 22713e6ff89dSTrond Myklebust { 22723e6ff89dSTrond Myklebust struct sock_xprt *transport = 22733e6ff89dSTrond Myklebust container_of(xprt, struct sock_xprt, xprt); 22743e6ff89dSTrond Myklebust struct rpc_timeout to; 22753e6ff89dSTrond Myklebust unsigned long initval; 22763e6ff89dSTrond Myklebust 22773e6ff89dSTrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22783e6ff89dSTrond Myklebust /* Arbitrary lower limit */ 22793e6ff89dSTrond Myklebust initval = max_t(unsigned long, connect_timeout, XS_TCP_INIT_REEST_TO); 22803e6ff89dSTrond Myklebust to.to_initval = initval; 22813e6ff89dSTrond Myklebust to.to_maxval = initval; 22823e6ff89dSTrond Myklebust to.to_retries = 0; 22833e6ff89dSTrond Myklebust memcpy(&transport->tcp_timeout, &to, sizeof(transport->tcp_timeout)); 22843e6ff89dSTrond Myklebust xprt->timeout = &transport->tcp_timeout; 22853e6ff89dSTrond Myklebust xprt->connect_timeout = connect_timeout; 22863e6ff89dSTrond Myklebust } 22873e6ff89dSTrond Myklebust 22887196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22897196dbb0STrond Myklebust unsigned long connect_timeout, 22907196dbb0STrond Myklebust unsigned long reconnect_timeout) 22917196dbb0STrond Myklebust { 22927196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22937196dbb0STrond Myklebust 2294b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22957196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22967196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22973e6ff89dSTrond Myklebust if (connect_timeout < xprt->connect_timeout) 22983e6ff89dSTrond Myklebust xs_tcp_do_set_connect_timeout(xprt, connect_timeout); 22997196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2300b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 23017196dbb0STrond Myklebust } 23027196dbb0STrond Myklebust 23038d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 23048d1b8c62STrond Myklebust { 23058d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23068d1b8c62STrond Myklebust 23078d1b8c62STrond Myklebust if (!transport->inet) { 23088d1b8c62STrond Myklebust struct sock *sk = sock->sk; 23098d1b8c62STrond Myklebust 2310d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2311d88e4d82SNeilBrown * connections such as NFS mounts. 2312d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2313d88e4d82SNeilBrown * Individual applications, which have specific 2314d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2315d88e4d82SNeilBrown * MAY override this as appropriate. 2316d88e4d82SNeilBrown */ 231718d5ad62SChristoph Hellwig if (xs_addr(xprt)->sa_family == PF_INET6) { 231818d5ad62SChristoph Hellwig ip6_sock_set_addr_preferences(sk, 231918d5ad62SChristoph Hellwig IPV6_PREFER_SRC_PUBLIC); 232018d5ad62SChristoph Hellwig } 2321d88e4d82SNeilBrown 23228d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2323d737e5d4STrond Myklebust tcp_sock_set_nodelay(sk); 2324775f06abSTrond Myklebust 2325ea9afca8STrond Myklebust lock_sock(sk); 2326b0d93ad5SChuck Lever 23272a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23282a9e1cfaSTrond Myklebust 2329b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23305157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2331b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2332b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 23332118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 233498123866SBenjamin Coddington sk->sk_use_task_frag = false; 23353167e12cSChuck Lever 23363167e12cSChuck Lever /* socket options */ 23373167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 2338b0d93ad5SChuck Lever 2339b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2340b0d93ad5SChuck Lever 2341b0d93ad5SChuck Lever /* Reset to new socket */ 2342ee0ac0c2SChuck Lever transport->sock = sock; 2343ee0ac0c2SChuck Lever transport->inet = sk; 2344b0d93ad5SChuck Lever 2345ea9afca8STrond Myklebust release_sock(sk); 2346b0d93ad5SChuck Lever } 2347b0d93ad5SChuck Lever 234801d37c42STrond Myklebust if (!xprt_bound(xprt)) 2349280254b6STrond Myklebust return -ENOTCONN; 235001d37c42STrond Myklebust 2351a564b8f0SMel Gorman xs_set_memalloc(xprt); 2352a564b8f0SMel Gorman 2353ae053551STrond Myklebust xs_stream_start_connect(transport); 2354e1806c7bSTrond Myklebust 2355b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23560fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2357280254b6STrond Myklebust return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 235816be2d20SChuck Lever } 235916be2d20SChuck Lever 236016be2d20SChuck Lever /** 2361b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2362acf0a39fSChuck Lever * @work: queued work item 236316be2d20SChuck Lever * 236416be2d20SChuck Lever * Invoked by a work queue tasklet. 236516be2d20SChuck Lever */ 2366cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 236716be2d20SChuck Lever { 2368cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2369cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 237016be2d20SChuck Lever struct socket *sock = transport->sock; 2371a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2372280254b6STrond Myklebust int status; 23738db55a03SNeilBrown unsigned int pflags = current->flags; 237416be2d20SChuck Lever 23758db55a03SNeilBrown if (atomic_read(&xprt->swapper)) 23768db55a03SNeilBrown current->flags |= PF_MEMALLOC; 237789f42494STrond Myklebust 237889f42494STrond Myklebust if (xprt_connected(xprt)) 237989f42494STrond Myklebust goto out; 238089f42494STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT, 238189f42494STrond Myklebust &transport->sock_state) || 238289f42494STrond Myklebust !sock) { 238389f42494STrond Myklebust xs_reset_transport(transport); 238489f42494STrond Myklebust sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family, 238589f42494STrond Myklebust SOCK_STREAM, IPPROTO_TCP, true); 2386b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2387280254b6STrond Myklebust xprt_wake_pending_tasks(xprt, PTR_ERR(sock)); 238816be2d20SChuck Lever goto out; 238916be2d20SChuck Lever } 23907d1e8255STrond Myklebust } 23917d1e8255STrond Myklebust 2392c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2393c740eff8SChuck Lever "%s (port %s)\n", xprt, 2394c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2395c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2396c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 239716be2d20SChuck Lever 239816be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 239940b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2400a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 240146121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 240246121cf7SChuck Lever sock->sk->sk_state); 2403a246b010SChuck Lever switch (status) { 24042a491991STrond Myklebust case 0: 2405a246b010SChuck Lever case -EINPROGRESS: 2406280254b6STrond Myklebust /* SYN_SENT! */ 240789f42494STrond Myklebust set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); 2408280254b6STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2409280254b6STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2410280254b6STrond Myklebust fallthrough; 2411a246b010SChuck Lever case -EALREADY: 2412280254b6STrond Myklebust goto out_unlock; 2413280254b6STrond Myklebust case -EADDRNOTAVAIL: 2414280254b6STrond Myklebust /* Source port number is unavailable. Try a new one! */ 2415280254b6STrond Myklebust transport->srcport = 0; 2416280254b6STrond Myklebust status = -EAGAIN; 2417280254b6STrond Myklebust break; 24189fcfe0c8STrond Myklebust case -EINVAL: 24199fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24209fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24219fcfe0c8STrond Myklebust */ 24223ed5e2a2STrond Myklebust case -ECONNREFUSED: 24233ed5e2a2STrond Myklebust case -ECONNRESET: 2424eb5b46faSTrond Myklebust case -ENETDOWN: 24253ed5e2a2STrond Myklebust case -ENETUNREACH: 24264ba161a7STrond Myklebust case -EHOSTUNREACH: 24273913c78cSTrond Myklebust case -EADDRINUSE: 24283601c4a9STrond Myklebust case -ENOBUFS: 2429280254b6STrond Myklebust break; 2430280254b6STrond Myklebust default: 2431280254b6STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2432280254b6STrond Myklebust __func__, status); 2433280254b6STrond Myklebust status = -EAGAIN; 2434280254b6STrond Myklebust } 2435280254b6STrond Myklebust 24368c71139dSCalum Mackay /* xs_tcp_force_close() wakes tasks with a fixed error code. 24378c71139dSCalum Mackay * We need to wake them first to ensure the correct error code. 24386ea44adcSNeilBrown */ 24396ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24404efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 2441a246b010SChuck Lever out: 24422226feb6SChuck Lever xprt_clear_connecting(xprt); 2443280254b6STrond Myklebust out_unlock: 2444cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 24458db55a03SNeilBrown current_restore_flags(pflags, PF_MEMALLOC); 2446a246b010SChuck Lever } 2447a246b010SChuck Lever 244875eb6af7SChuck Lever /* 244975eb6af7SChuck Lever * Transfer the connected socket to @upper_transport, then mark that 245075eb6af7SChuck Lever * xprt CONNECTED. 245175eb6af7SChuck Lever */ 245275eb6af7SChuck Lever static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt, 245375eb6af7SChuck Lever struct sock_xprt *upper_transport) 245475eb6af7SChuck Lever { 245575eb6af7SChuck Lever struct sock_xprt *lower_transport = 245675eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 245775eb6af7SChuck Lever struct rpc_xprt *upper_xprt = &upper_transport->xprt; 245875eb6af7SChuck Lever 245975eb6af7SChuck Lever if (!upper_transport->inet) { 246075eb6af7SChuck Lever struct socket *sock = lower_transport->sock; 246175eb6af7SChuck Lever struct sock *sk = sock->sk; 246275eb6af7SChuck Lever 246375eb6af7SChuck Lever /* Avoid temporary address, they are bad for long-lived 246475eb6af7SChuck Lever * connections such as NFS mounts. 246575eb6af7SChuck Lever * RFC4941, section 3.6 suggests that: 246675eb6af7SChuck Lever * Individual applications, which have specific 246775eb6af7SChuck Lever * knowledge about the normal duration of connections, 246875eb6af7SChuck Lever * MAY override this as appropriate. 246975eb6af7SChuck Lever */ 247075eb6af7SChuck Lever if (xs_addr(upper_xprt)->sa_family == PF_INET6) 247175eb6af7SChuck Lever ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC); 247275eb6af7SChuck Lever 247375eb6af7SChuck Lever xs_tcp_set_socket_timeouts(upper_xprt, sock); 247475eb6af7SChuck Lever tcp_sock_set_nodelay(sk); 247575eb6af7SChuck Lever 247675eb6af7SChuck Lever lock_sock(sk); 247775eb6af7SChuck Lever 247875eb6af7SChuck Lever /* @sk is already connected, so it now has the RPC callbacks. 247975eb6af7SChuck Lever * Reach into @lower_transport to save the original ones. 248075eb6af7SChuck Lever */ 248175eb6af7SChuck Lever upper_transport->old_data_ready = lower_transport->old_data_ready; 248275eb6af7SChuck Lever upper_transport->old_state_change = lower_transport->old_state_change; 248375eb6af7SChuck Lever upper_transport->old_write_space = lower_transport->old_write_space; 248475eb6af7SChuck Lever upper_transport->old_error_report = lower_transport->old_error_report; 248575eb6af7SChuck Lever sk->sk_user_data = upper_xprt; 248675eb6af7SChuck Lever 248775eb6af7SChuck Lever /* socket options */ 248875eb6af7SChuck Lever sock_reset_flag(sk, SOCK_LINGER); 248975eb6af7SChuck Lever 249075eb6af7SChuck Lever xprt_clear_connected(upper_xprt); 249175eb6af7SChuck Lever 249275eb6af7SChuck Lever upper_transport->sock = sock; 249375eb6af7SChuck Lever upper_transport->inet = sk; 249475eb6af7SChuck Lever upper_transport->file = lower_transport->file; 249575eb6af7SChuck Lever 249675eb6af7SChuck Lever release_sock(sk); 249775eb6af7SChuck Lever 249875eb6af7SChuck Lever /* Reset lower_transport before shutting down its clnt */ 249975eb6af7SChuck Lever mutex_lock(&lower_transport->recv_mutex); 250075eb6af7SChuck Lever lower_transport->inet = NULL; 250175eb6af7SChuck Lever lower_transport->sock = NULL; 250275eb6af7SChuck Lever lower_transport->file = NULL; 250375eb6af7SChuck Lever 250475eb6af7SChuck Lever xprt_clear_connected(lower_xprt); 250575eb6af7SChuck Lever xs_sock_reset_connection_flags(lower_xprt); 250675eb6af7SChuck Lever xs_stream_reset_connect(lower_transport); 250775eb6af7SChuck Lever mutex_unlock(&lower_transport->recv_mutex); 250875eb6af7SChuck Lever } 250975eb6af7SChuck Lever 251075eb6af7SChuck Lever if (!xprt_bound(upper_xprt)) 251175eb6af7SChuck Lever return -ENOTCONN; 251275eb6af7SChuck Lever 251375eb6af7SChuck Lever xs_set_memalloc(upper_xprt); 251475eb6af7SChuck Lever 251575eb6af7SChuck Lever if (!xprt_test_and_set_connected(upper_xprt)) { 251675eb6af7SChuck Lever upper_xprt->connect_cookie++; 251775eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 251875eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 251975eb6af7SChuck Lever 252075eb6af7SChuck Lever upper_xprt->stat.connect_count++; 252175eb6af7SChuck Lever upper_xprt->stat.connect_time += (long)jiffies - 252275eb6af7SChuck Lever upper_xprt->stat.connect_start; 252375eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 252475eb6af7SChuck Lever } 252575eb6af7SChuck Lever return 0; 252675eb6af7SChuck Lever } 252775eb6af7SChuck Lever 252875eb6af7SChuck Lever /** 252975eb6af7SChuck Lever * xs_tls_handshake_done - TLS handshake completion handler 253075eb6af7SChuck Lever * @data: address of xprt to wake 253175eb6af7SChuck Lever * @status: status of handshake 253275eb6af7SChuck Lever * @peerid: serial number of key containing the remote's identity 253375eb6af7SChuck Lever * 253475eb6af7SChuck Lever */ 253575eb6af7SChuck Lever static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid) 253675eb6af7SChuck Lever { 253775eb6af7SChuck Lever struct rpc_xprt *lower_xprt = data; 253875eb6af7SChuck Lever struct sock_xprt *lower_transport = 253975eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 254075eb6af7SChuck Lever 254175eb6af7SChuck Lever lower_transport->xprt_err = status ? -EACCES : 0; 254275eb6af7SChuck Lever complete(&lower_transport->handshake_done); 254375eb6af7SChuck Lever xprt_put(lower_xprt); 254475eb6af7SChuck Lever } 254575eb6af7SChuck Lever 254675eb6af7SChuck Lever static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec) 254775eb6af7SChuck Lever { 254875eb6af7SChuck Lever struct sock_xprt *lower_transport = 254975eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 255075eb6af7SChuck Lever struct tls_handshake_args args = { 255175eb6af7SChuck Lever .ta_sock = lower_transport->sock, 255275eb6af7SChuck Lever .ta_done = xs_tls_handshake_done, 255375eb6af7SChuck Lever .ta_data = xprt_get(lower_xprt), 255475eb6af7SChuck Lever .ta_peername = lower_xprt->servername, 255575eb6af7SChuck Lever }; 255675eb6af7SChuck Lever struct sock *sk = lower_transport->inet; 255775eb6af7SChuck Lever int rc; 255875eb6af7SChuck Lever 255975eb6af7SChuck Lever init_completion(&lower_transport->handshake_done); 256075eb6af7SChuck Lever set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 256175eb6af7SChuck Lever lower_transport->xprt_err = -ETIMEDOUT; 256275eb6af7SChuck Lever switch (xprtsec->policy) { 256375eb6af7SChuck Lever case RPC_XPRTSEC_TLS_ANON: 256475eb6af7SChuck Lever rc = tls_client_hello_anon(&args, GFP_KERNEL); 256575eb6af7SChuck Lever if (rc) 256675eb6af7SChuck Lever goto out_put_xprt; 256775eb6af7SChuck Lever break; 256875eb6af7SChuck Lever case RPC_XPRTSEC_TLS_X509: 256975eb6af7SChuck Lever args.ta_my_cert = xprtsec->cert_serial; 257075eb6af7SChuck Lever args.ta_my_privkey = xprtsec->privkey_serial; 257175eb6af7SChuck Lever rc = tls_client_hello_x509(&args, GFP_KERNEL); 257275eb6af7SChuck Lever if (rc) 257375eb6af7SChuck Lever goto out_put_xprt; 257475eb6af7SChuck Lever break; 257575eb6af7SChuck Lever default: 257675eb6af7SChuck Lever rc = -EACCES; 257775eb6af7SChuck Lever goto out_put_xprt; 257875eb6af7SChuck Lever } 257975eb6af7SChuck Lever 258075eb6af7SChuck Lever rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done, 258175eb6af7SChuck Lever XS_TLS_HANDSHAKE_TO); 258275eb6af7SChuck Lever if (rc <= 0) { 258375eb6af7SChuck Lever if (!tls_handshake_cancel(sk)) { 258475eb6af7SChuck Lever if (rc == 0) 258575eb6af7SChuck Lever rc = -ETIMEDOUT; 258675eb6af7SChuck Lever goto out_put_xprt; 258775eb6af7SChuck Lever } 258875eb6af7SChuck Lever } 258975eb6af7SChuck Lever 259075eb6af7SChuck Lever rc = lower_transport->xprt_err; 259175eb6af7SChuck Lever 259275eb6af7SChuck Lever out: 259375eb6af7SChuck Lever xs_stream_reset_connect(lower_transport); 259475eb6af7SChuck Lever clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 259575eb6af7SChuck Lever return rc; 259675eb6af7SChuck Lever 259775eb6af7SChuck Lever out_put_xprt: 259875eb6af7SChuck Lever xprt_put(lower_xprt); 259975eb6af7SChuck Lever goto out; 260075eb6af7SChuck Lever } 260175eb6af7SChuck Lever 260275eb6af7SChuck Lever /** 260375eb6af7SChuck Lever * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket 260475eb6af7SChuck Lever * @work: queued work item 260575eb6af7SChuck Lever * 260675eb6af7SChuck Lever * Invoked by a work queue tasklet. 260775eb6af7SChuck Lever * 260875eb6af7SChuck Lever * For RPC-with-TLS, there is a two-stage connection process. 260975eb6af7SChuck Lever * 261075eb6af7SChuck Lever * The "upper-layer xprt" is visible to the RPC consumer. Once it has 261175eb6af7SChuck Lever * been marked connected, the consumer knows that a TCP connection and 261275eb6af7SChuck Lever * a TLS session have been established. 261375eb6af7SChuck Lever * 261475eb6af7SChuck Lever * A "lower-layer xprt", created in this function, handles the mechanics 261575eb6af7SChuck Lever * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and 261675eb6af7SChuck Lever * then driving the TLS handshake. Once all that is complete, the upper 261775eb6af7SChuck Lever * layer xprt is marked connected. 261875eb6af7SChuck Lever */ 261975eb6af7SChuck Lever static void xs_tcp_tls_setup_socket(struct work_struct *work) 262075eb6af7SChuck Lever { 262175eb6af7SChuck Lever struct sock_xprt *upper_transport = 262275eb6af7SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 262375eb6af7SChuck Lever struct rpc_clnt *upper_clnt = upper_transport->clnt; 262475eb6af7SChuck Lever struct rpc_xprt *upper_xprt = &upper_transport->xprt; 262575eb6af7SChuck Lever struct rpc_create_args args = { 262675eb6af7SChuck Lever .net = upper_xprt->xprt_net, 262775eb6af7SChuck Lever .protocol = upper_xprt->prot, 262875eb6af7SChuck Lever .address = (struct sockaddr *)&upper_xprt->addr, 262975eb6af7SChuck Lever .addrsize = upper_xprt->addrlen, 263075eb6af7SChuck Lever .timeout = upper_clnt->cl_timeout, 263175eb6af7SChuck Lever .servername = upper_xprt->servername, 263275eb6af7SChuck Lever .program = upper_clnt->cl_program, 263375eb6af7SChuck Lever .prognumber = upper_clnt->cl_prog, 263475eb6af7SChuck Lever .version = upper_clnt->cl_vers, 263575eb6af7SChuck Lever .authflavor = RPC_AUTH_TLS, 263675eb6af7SChuck Lever .cred = upper_clnt->cl_cred, 263775eb6af7SChuck Lever .xprtsec = { 263875eb6af7SChuck Lever .policy = RPC_XPRTSEC_NONE, 263975eb6af7SChuck Lever }, 264075eb6af7SChuck Lever }; 264175eb6af7SChuck Lever unsigned int pflags = current->flags; 264275eb6af7SChuck Lever struct rpc_clnt *lower_clnt; 264375eb6af7SChuck Lever struct rpc_xprt *lower_xprt; 264475eb6af7SChuck Lever int status; 264575eb6af7SChuck Lever 264675eb6af7SChuck Lever if (atomic_read(&upper_xprt->swapper)) 264775eb6af7SChuck Lever current->flags |= PF_MEMALLOC; 264875eb6af7SChuck Lever 264975eb6af7SChuck Lever xs_stream_start_connect(upper_transport); 265075eb6af7SChuck Lever 265175eb6af7SChuck Lever /* This implicitly sends an RPC_AUTH_TLS probe */ 265275eb6af7SChuck Lever lower_clnt = rpc_create(&args); 265375eb6af7SChuck Lever if (IS_ERR(lower_clnt)) { 265475eb6af7SChuck Lever trace_rpc_tls_unavailable(upper_clnt, upper_xprt); 265575eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 265675eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 265775eb6af7SChuck Lever xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt)); 265875eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 265975eb6af7SChuck Lever goto out_unlock; 266075eb6af7SChuck Lever } 266175eb6af7SChuck Lever 266275eb6af7SChuck Lever /* RPC_AUTH_TLS probe was successful. Try a TLS handshake on 266375eb6af7SChuck Lever * the lower xprt. 266475eb6af7SChuck Lever */ 266575eb6af7SChuck Lever rcu_read_lock(); 266675eb6af7SChuck Lever lower_xprt = rcu_dereference(lower_clnt->cl_xprt); 266775eb6af7SChuck Lever rcu_read_unlock(); 266875eb6af7SChuck Lever status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec); 266975eb6af7SChuck Lever if (status) { 267075eb6af7SChuck Lever trace_rpc_tls_not_started(upper_clnt, upper_xprt); 267175eb6af7SChuck Lever goto out_close; 267275eb6af7SChuck Lever } 267375eb6af7SChuck Lever 267475eb6af7SChuck Lever status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport); 267575eb6af7SChuck Lever if (status) 267675eb6af7SChuck Lever goto out_close; 267775eb6af7SChuck Lever 267875eb6af7SChuck Lever trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0); 267975eb6af7SChuck Lever if (!xprt_test_and_set_connected(upper_xprt)) { 268075eb6af7SChuck Lever upper_xprt->connect_cookie++; 268175eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 268275eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 268375eb6af7SChuck Lever 268475eb6af7SChuck Lever upper_xprt->stat.connect_count++; 268575eb6af7SChuck Lever upper_xprt->stat.connect_time += (long)jiffies - 268675eb6af7SChuck Lever upper_xprt->stat.connect_start; 268775eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 268875eb6af7SChuck Lever } 268975eb6af7SChuck Lever rpc_shutdown_client(lower_clnt); 269075eb6af7SChuck Lever 269175eb6af7SChuck Lever out_unlock: 269275eb6af7SChuck Lever current_restore_flags(pflags, PF_MEMALLOC); 269375eb6af7SChuck Lever upper_transport->clnt = NULL; 269475eb6af7SChuck Lever xprt_unlock_connect(upper_xprt, upper_transport); 269575eb6af7SChuck Lever return; 269675eb6af7SChuck Lever 269775eb6af7SChuck Lever out_close: 269875eb6af7SChuck Lever rpc_shutdown_client(lower_clnt); 269975eb6af7SChuck Lever 270075eb6af7SChuck Lever /* xprt_force_disconnect() wakes tasks with a fixed tk_status code. 270175eb6af7SChuck Lever * Wake them first here to ensure they get our tk_status code. 270275eb6af7SChuck Lever */ 270375eb6af7SChuck Lever xprt_wake_pending_tasks(upper_xprt, status); 270475eb6af7SChuck Lever xs_tcp_force_close(upper_xprt); 270575eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 270675eb6af7SChuck Lever goto out_unlock; 270775eb6af7SChuck Lever } 270875eb6af7SChuck Lever 270968e220bdSChuck Lever /** 27109903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 27111b092092STrond Myklebust * @xprt: pointer to transport structure 27129903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 27139903cd1cSChuck Lever * 27149903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 271503bf4b70SChuck Lever * 271603bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 271703bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 271803bf4b70SChuck Lever * socket on a privileged port. 271903bf4b70SChuck Lever * 272003bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 272103bf4b70SChuck Lever * retry floods (hard mounts). 27229903cd1cSChuck Lever */ 27231b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2724a246b010SChuck Lever { 2725ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 272602910177STrond Myklebust unsigned long delay = 0; 2727a246b010SChuck Lever 2728718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2729718ba5b8STrond Myklebust 273089f42494STrond Myklebust if (transport->sock != NULL) { 273146121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 273289f42494STrond Myklebust "seconds\n", xprt, xprt->reestablish_timeout / HZ); 273399b1a4c3STrond Myklebust 2734675dd90aSChuck Lever delay = xprt_reconnect_delay(xprt); 2735675dd90aSChuck Lever xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 273602910177STrond Myklebust 273702910177STrond Myklebust } else 27389903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 273902910177STrond Myklebust 274075eb6af7SChuck Lever transport->clnt = task->tk_client; 274140a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 274202910177STrond Myklebust &transport->connect_worker, 274302910177STrond Myklebust delay); 2744a246b010SChuck Lever } 2745a246b010SChuck Lever 27464f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 27474f8943f8STrond Myklebust { 27484f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 27494f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 27504f8943f8STrond Myklebust } 27514f8943f8STrond Myklebust 27524f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 27534f8943f8STrond Myklebust { 27544f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 27554f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 27564f8943f8STrond Myklebust } 27574f8943f8STrond Myklebust 27584f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 27594f8943f8STrond Myklebust { 27604f8943f8STrond Myklebust int sockerr; 27614f8943f8STrond Myklebust 27624f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 27634f8943f8STrond Myklebust return; 27644f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 27654f8943f8STrond Myklebust if (transport->sock == NULL) 27664f8943f8STrond Myklebust goto out; 27674f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 27684f8943f8STrond Myklebust goto out; 2769af84537dSBenjamin Coddington sockerr = xchg(&transport->xprt_err, 0); 27704f8943f8STrond Myklebust if (sockerr < 0) 27714f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 27724f8943f8STrond Myklebust out: 27734f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 27744f8943f8STrond Myklebust } 27754f8943f8STrond Myklebust 27764f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 27774f8943f8STrond Myklebust { 27784f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 27794f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 27804f8943f8STrond Myklebust } 27814f8943f8STrond Myklebust 27824f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 27834f8943f8STrond Myklebust { 27844f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 27854f8943f8STrond Myklebust struct sock_xprt, error_worker); 27864f8943f8STrond Myklebust 27874f8943f8STrond Myklebust xs_wake_disconnect(transport); 27884f8943f8STrond Myklebust xs_wake_write(transport); 27894f8943f8STrond Myklebust xs_wake_error(transport); 27904f8943f8STrond Myklebust xs_wake_pending(transport); 27914f8943f8STrond Myklebust } 27924f8943f8STrond Myklebust 2793262ca07dSChuck Lever /** 279412b20ce3SBhaskar Chowdhury * xs_local_print_stats - display AF_LOCAL socket-specific stats 2795176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2796176e21eeSChuck Lever * @seq: output file 2797176e21eeSChuck Lever * 2798176e21eeSChuck Lever */ 2799176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2800176e21eeSChuck Lever { 2801176e21eeSChuck Lever long idle_time = 0; 2802176e21eeSChuck Lever 2803176e21eeSChuck Lever if (xprt_connected(xprt)) 2804176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2805176e21eeSChuck Lever 2806176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 280715a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2808176e21eeSChuck Lever xprt->stat.bind_count, 2809176e21eeSChuck Lever xprt->stat.connect_count, 28108440a886SChuck Lever xprt->stat.connect_time / HZ, 2811176e21eeSChuck Lever idle_time, 2812176e21eeSChuck Lever xprt->stat.sends, 2813176e21eeSChuck Lever xprt->stat.recvs, 2814176e21eeSChuck Lever xprt->stat.bad_xids, 2815176e21eeSChuck Lever xprt->stat.req_u, 281615a45206SAndy Adamson xprt->stat.bklog_u, 281715a45206SAndy Adamson xprt->stat.max_slots, 281815a45206SAndy Adamson xprt->stat.sending_u, 281915a45206SAndy Adamson xprt->stat.pending_u); 2820176e21eeSChuck Lever } 2821176e21eeSChuck Lever 2822176e21eeSChuck Lever /** 282312b20ce3SBhaskar Chowdhury * xs_udp_print_stats - display UDP socket-specific stats 2824262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2825262ca07dSChuck Lever * @seq: output file 2826262ca07dSChuck Lever * 2827262ca07dSChuck Lever */ 2828262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2829262ca07dSChuck Lever { 2830c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2831c8475461SChuck Lever 283215a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 283315a45206SAndy Adamson "%lu %llu %llu\n", 2834fbfffbd5SChuck Lever transport->srcport, 2835262ca07dSChuck Lever xprt->stat.bind_count, 2836262ca07dSChuck Lever xprt->stat.sends, 2837262ca07dSChuck Lever xprt->stat.recvs, 2838262ca07dSChuck Lever xprt->stat.bad_xids, 2839262ca07dSChuck Lever xprt->stat.req_u, 284015a45206SAndy Adamson xprt->stat.bklog_u, 284115a45206SAndy Adamson xprt->stat.max_slots, 284215a45206SAndy Adamson xprt->stat.sending_u, 284315a45206SAndy Adamson xprt->stat.pending_u); 2844262ca07dSChuck Lever } 2845262ca07dSChuck Lever 2846262ca07dSChuck Lever /** 284712b20ce3SBhaskar Chowdhury * xs_tcp_print_stats - display TCP socket-specific stats 2848262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2849262ca07dSChuck Lever * @seq: output file 2850262ca07dSChuck Lever * 2851262ca07dSChuck Lever */ 2852262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2853262ca07dSChuck Lever { 2854c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2855262ca07dSChuck Lever long idle_time = 0; 2856262ca07dSChuck Lever 2857262ca07dSChuck Lever if (xprt_connected(xprt)) 2858262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2859262ca07dSChuck Lever 286015a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 286115a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2862fbfffbd5SChuck Lever transport->srcport, 2863262ca07dSChuck Lever xprt->stat.bind_count, 2864262ca07dSChuck Lever xprt->stat.connect_count, 28658440a886SChuck Lever xprt->stat.connect_time / HZ, 2866262ca07dSChuck Lever idle_time, 2867262ca07dSChuck Lever xprt->stat.sends, 2868262ca07dSChuck Lever xprt->stat.recvs, 2869262ca07dSChuck Lever xprt->stat.bad_xids, 2870262ca07dSChuck Lever xprt->stat.req_u, 287115a45206SAndy Adamson xprt->stat.bklog_u, 287215a45206SAndy Adamson xprt->stat.max_slots, 287315a45206SAndy Adamson xprt->stat.sending_u, 287415a45206SAndy Adamson xprt->stat.pending_u); 2875262ca07dSChuck Lever } 2876262ca07dSChuck Lever 28774cfc7e60SRahul Iyer /* 28784cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 28794cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 28804cfc7e60SRahul Iyer * to use the server side send routines. 28814cfc7e60SRahul Iyer */ 28825fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 28834cfc7e60SRahul Iyer { 28845fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 28855fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 28864cfc7e60SRahul Iyer struct page *page; 28874cfc7e60SRahul Iyer struct rpc_buffer *buf; 28884cfc7e60SRahul Iyer 28895fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 28905fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 28915fe6eaa1SChuck Lever size); 28925fe6eaa1SChuck Lever return -EINVAL; 28935fe6eaa1SChuck Lever } 28944cfc7e60SRahul Iyer 2895b2648015STrond Myklebust page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 28964cfc7e60SRahul Iyer if (!page) 28975fe6eaa1SChuck Lever return -ENOMEM; 28984cfc7e60SRahul Iyer 28994cfc7e60SRahul Iyer buf = page_address(page); 29004cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 29014cfc7e60SRahul Iyer 29025fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 290318e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 29045fe6eaa1SChuck Lever return 0; 29054cfc7e60SRahul Iyer } 29064cfc7e60SRahul Iyer 29074cfc7e60SRahul Iyer /* 29084cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 29094cfc7e60SRahul Iyer */ 29103435c74aSChuck Lever static void bc_free(struct rpc_task *task) 29114cfc7e60SRahul Iyer { 29123435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 29134cfc7e60SRahul Iyer struct rpc_buffer *buf; 29144cfc7e60SRahul Iyer 29154cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 29164cfc7e60SRahul Iyer free_page((unsigned long)buf); 29174cfc7e60SRahul Iyer } 29184cfc7e60SRahul Iyer 29194cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 29204cfc7e60SRahul Iyer { 2921da1661b9SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 29224cfc7e60SRahul Iyer struct sock_xprt *transport = 2923067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 2924067fb11bSChuck Lever struct msghdr msg = { 2925da1661b9SChuck Lever .msg_flags = 0, 2926067fb11bSChuck Lever }; 2927067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2928da1661b9SChuck Lever (u32)xdr->len); 2929da1661b9SChuck Lever unsigned int sent = 0; 2930da1661b9SChuck Lever int err; 29314cfc7e60SRahul Iyer 29328729aabaSChuck Lever req->rq_xtime = ktime_get(); 2933ff053dbbSTrond Myklebust err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 2934ff053dbbSTrond Myklebust if (err < 0) 2935ff053dbbSTrond Myklebust return err; 2936da1661b9SChuck Lever err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); 2937da1661b9SChuck Lever xdr_free_bvec(xdr); 2938da1661b9SChuck Lever if (err < 0 || sent != (xdr->len + sizeof(marker))) 2939067fb11bSChuck Lever return -EAGAIN; 2940da1661b9SChuck Lever return sent; 29414cfc7e60SRahul Iyer } 29424cfc7e60SRahul Iyer 2943ca4faf54SChuck Lever /** 2944ca4faf54SChuck Lever * bc_send_request - Send a backchannel Call on a TCP socket 2945ca4faf54SChuck Lever * @req: rpc_rqst containing Call message to be sent 2946ca4faf54SChuck Lever * 2947ca4faf54SChuck Lever * xpt_mutex ensures @rqstp's whole message is written to the socket 2948ca4faf54SChuck Lever * without interruption. 2949ca4faf54SChuck Lever * 2950ca4faf54SChuck Lever * Return values: 2951ca4faf54SChuck Lever * %0 if the message was sent successfully 2952ca4faf54SChuck Lever * %ENOTCONN if the message was not sent 29534cfc7e60SRahul Iyer */ 2954adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 29554cfc7e60SRahul Iyer { 29564cfc7e60SRahul Iyer struct svc_xprt *xprt; 29577fc56136SAndrzej Hajda int len; 29584cfc7e60SRahul Iyer 29594cfc7e60SRahul Iyer /* 29604cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 29614cfc7e60SRahul Iyer */ 29624cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 29634cfc7e60SRahul Iyer 29644cfc7e60SRahul Iyer /* 29654cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 29664cfc7e60SRahul Iyer * with the fore channel 29674cfc7e60SRahul Iyer */ 2968c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 29694cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 29704cfc7e60SRahul Iyer len = -ENOTCONN; 29714cfc7e60SRahul Iyer else 29724cfc7e60SRahul Iyer len = bc_sendto(req); 29734cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 29744cfc7e60SRahul Iyer 29754cfc7e60SRahul Iyer if (len > 0) 29764cfc7e60SRahul Iyer len = 0; 29774cfc7e60SRahul Iyer 29784cfc7e60SRahul Iyer return len; 29794cfc7e60SRahul Iyer } 29804cfc7e60SRahul Iyer 29814cfc7e60SRahul Iyer /* 29824cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 29834cfc7e60SRahul Iyer */ 29844cfc7e60SRahul Iyer 29854cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 29864cfc7e60SRahul Iyer { 29876221f1d9SChuck Lever xprt_disconnect_done(xprt); 29884cfc7e60SRahul Iyer } 29894cfc7e60SRahul Iyer 29904cfc7e60SRahul Iyer /* 29914cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 29924cfc7e60SRahul Iyer * initiated, we do nothing 29934cfc7e60SRahul Iyer */ 29944cfc7e60SRahul Iyer 29954cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 29964cfc7e60SRahul Iyer { 299747f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 299847f72efaSKinglong Mee 299947f72efaSKinglong Mee xs_xprt_free(xprt); 300047f72efaSKinglong Mee module_put(THIS_MODULE); 30014cfc7e60SRahul Iyer } 30024cfc7e60SRahul Iyer 3003d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 3004176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 30054cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 3006f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 3007a9cde23aSChuck Lever .free_slot = xprt_free_slot, 3008176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 3009176e21eeSChuck Lever .set_port = xs_local_set_port, 3010dc107402SJ. Bruce Fields .connect = xs_local_connect, 3011176e21eeSChuck Lever .buf_alloc = rpc_malloc, 3012176e21eeSChuck Lever .buf_free = rpc_free, 3013550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 3014176e21eeSChuck Lever .send_request = xs_local_send_request, 30158ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 3016176e21eeSChuck Lever .close = xs_close, 3017a1311d87STrond Myklebust .destroy = xs_destroy, 3018176e21eeSChuck Lever .print_stats = xs_local_print_stats, 3019d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3020d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 3021176e21eeSChuck Lever }; 3022176e21eeSChuck Lever 3023d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 302443118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 302512a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 302649e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 3027f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 3028a9cde23aSChuck Lever .free_slot = xprt_free_slot, 302945160d62SChuck Lever .rpcbind = rpcb_getport_async, 303092200412SChuck Lever .set_port = xs_set_port, 30319903cd1cSChuck Lever .connect = xs_connect, 3032421ab1beSTrond Myklebust .get_srcaddr = xs_sock_srcaddr, 3033421ab1beSTrond Myklebust .get_srcport = xs_sock_srcport, 303402107148SChuck Lever .buf_alloc = rpc_malloc, 303502107148SChuck Lever .buf_free = rpc_free, 3036262965f5SChuck Lever .send_request = xs_udp_send_request, 30378ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 303846c0ee8bSChuck Lever .timer = xs_udp_timer, 3039a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 3040262965f5SChuck Lever .close = xs_close, 3041262965f5SChuck Lever .destroy = xs_destroy, 3042262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 3043d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3044d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30454a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 3046262965f5SChuck Lever }; 3047262965f5SChuck Lever 3048d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 304912a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 30504cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 305136bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 3052a9cde23aSChuck Lever .free_slot = xprt_free_slot, 305345160d62SChuck Lever .rpcbind = rpcb_getport_async, 305492200412SChuck Lever .set_port = xs_set_port, 30550b9e7943STrond Myklebust .connect = xs_connect, 3056421ab1beSTrond Myklebust .get_srcaddr = xs_sock_srcaddr, 3057421ab1beSTrond Myklebust .get_srcport = xs_sock_srcport, 305802107148SChuck Lever .buf_alloc = rpc_malloc, 305902107148SChuck Lever .buf_free = rpc_free, 3060277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 3061262965f5SChuck Lever .send_request = xs_tcp_send_request, 30628ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 3063c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 30649903cd1cSChuck Lever .destroy = xs_destroy, 30657196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 3066262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 3067d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3068d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30694a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 307042e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 307142e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 30726b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 30737402a4feSTrond Myklebust .bc_num_slots = xprt_bc_max_slots, 307442e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 307542e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 307642e5c3e2SChuck Lever #endif 3077a246b010SChuck Lever }; 3078a246b010SChuck Lever 30794cfc7e60SRahul Iyer /* 30804cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 30814cfc7e60SRahul Iyer */ 30824cfc7e60SRahul Iyer 3083d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 30844cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 30854cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 308684e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 3087a9cde23aSChuck Lever .free_slot = xprt_free_slot, 30884cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 30894cfc7e60SRahul Iyer .buf_free = bc_free, 30904cfc7e60SRahul Iyer .send_request = bc_send_request, 30918ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 30924cfc7e60SRahul Iyer .close = bc_close, 30934cfc7e60SRahul Iyer .destroy = bc_destroy, 30944cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 3095d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3096d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30974a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 30984cfc7e60SRahul Iyer }; 30994cfc7e60SRahul Iyer 310092476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 310192476850SChuck Lever { 310292476850SChuck Lever static const struct sockaddr_in sin = { 310392476850SChuck Lever .sin_family = AF_INET, 310492476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 310592476850SChuck Lever }; 310692476850SChuck Lever static const struct sockaddr_in6 sin6 = { 310792476850SChuck Lever .sin6_family = AF_INET6, 310892476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 310992476850SChuck Lever }; 311092476850SChuck Lever 311192476850SChuck Lever switch (family) { 3112176e21eeSChuck Lever case AF_LOCAL: 3113176e21eeSChuck Lever break; 311492476850SChuck Lever case AF_INET: 311592476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 311692476850SChuck Lever break; 311792476850SChuck Lever case AF_INET6: 311892476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 311992476850SChuck Lever break; 312092476850SChuck Lever default: 312192476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 312292476850SChuck Lever return -EAFNOSUPPORT; 312392476850SChuck Lever } 312492476850SChuck Lever return 0; 312592476850SChuck Lever } 312692476850SChuck Lever 31273c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 3128d9ba131dSTrond Myklebust unsigned int slot_table_size, 3129d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 3130c8541ecdSChuck Lever { 3131c8541ecdSChuck Lever struct rpc_xprt *xprt; 3132ffc2e518SChuck Lever struct sock_xprt *new; 3133c8541ecdSChuck Lever 313496802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 3135c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 3136c8541ecdSChuck Lever return ERR_PTR(-EBADF); 3137c8541ecdSChuck Lever } 3138c8541ecdSChuck Lever 3139d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 3140d9ba131dSTrond Myklebust max_slot_table_size); 3141bd1722d4SPavel Emelyanov if (xprt == NULL) { 314246121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 314346121cf7SChuck Lever "rpc_xprt\n"); 3144c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 3145c8541ecdSChuck Lever } 3146c8541ecdSChuck Lever 3147bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 3148edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 314996802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 315096802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 3151d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 3152fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 315392476850SChuck Lever else { 315492476850SChuck Lever int err; 315592476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 315692476850SChuck Lever (struct sockaddr *)&new->srcaddr); 31572aa13531SStanislav Kinsbursky if (err != 0) { 31582aa13531SStanislav Kinsbursky xprt_free(xprt); 315992476850SChuck Lever return ERR_PTR(err); 316092476850SChuck Lever } 31612aa13531SStanislav Kinsbursky } 3162c8541ecdSChuck Lever 3163c8541ecdSChuck Lever return xprt; 3164c8541ecdSChuck Lever } 3165c8541ecdSChuck Lever 3166176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 3167176e21eeSChuck Lever .to_initval = 10 * HZ, 3168176e21eeSChuck Lever .to_maxval = 10 * HZ, 3169176e21eeSChuck Lever .to_retries = 2, 3170176e21eeSChuck Lever }; 3171176e21eeSChuck Lever 3172176e21eeSChuck Lever /** 3173176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 3174176e21eeSChuck Lever * @args: rpc transport creation arguments 3175176e21eeSChuck Lever * 3176176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 3177176e21eeSChuck Lever */ 3178176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 3179176e21eeSChuck Lever { 3180176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 3181176e21eeSChuck Lever struct sock_xprt *transport; 3182176e21eeSChuck Lever struct rpc_xprt *xprt; 3183176e21eeSChuck Lever struct rpc_xprt *ret; 3184176e21eeSChuck Lever 3185d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3186d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 3187176e21eeSChuck Lever if (IS_ERR(xprt)) 3188176e21eeSChuck Lever return xprt; 3189176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3190176e21eeSChuck Lever 3191176e21eeSChuck Lever xprt->prot = 0; 3192d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_local_transport; 3193176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3194176e21eeSChuck Lever 3195176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 3196176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 3197176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3198176e21eeSChuck Lever 3199176e21eeSChuck Lever xprt->ops = &xs_local_ops; 3200176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 3201176e21eeSChuck Lever 3202550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 32034f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3204550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 320593dc41bdSNeilBrown 3206176e21eeSChuck Lever switch (sun->sun_family) { 3207176e21eeSChuck Lever case AF_LOCAL: 32084388ce05SNeilBrown if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') { 3209176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 3210176e21eeSChuck Lever sun->sun_path); 3211176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 3212176e21eeSChuck Lever goto out_err; 3213176e21eeSChuck Lever } 3214176e21eeSChuck Lever xprt_set_bound(xprt); 3215176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 3216176e21eeSChuck Lever break; 3217176e21eeSChuck Lever default: 3218176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 3219176e21eeSChuck Lever goto out_err; 3220176e21eeSChuck Lever } 3221176e21eeSChuck Lever 3222176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 3223176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 3224176e21eeSChuck Lever 3225176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 3226176e21eeSChuck Lever return xprt; 3227176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 3228176e21eeSChuck Lever out_err: 3229315f3812SKinglong Mee xs_xprt_free(xprt); 3230176e21eeSChuck Lever return ret; 3231176e21eeSChuck Lever } 3232176e21eeSChuck Lever 32332881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 32342881ae74STrond Myklebust .to_initval = 5 * HZ, 32352881ae74STrond Myklebust .to_maxval = 30 * HZ, 32362881ae74STrond Myklebust .to_increment = 5 * HZ, 32372881ae74STrond Myklebust .to_retries = 5, 32382881ae74STrond Myklebust }; 32392881ae74STrond Myklebust 32409903cd1cSChuck Lever /** 32419903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 324296802a09SFrank van Maarseveen * @args: rpc transport creation arguments 32439903cd1cSChuck Lever * 32449903cd1cSChuck Lever */ 3245483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 3246a246b010SChuck Lever { 32478f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3248c8541ecdSChuck Lever struct rpc_xprt *xprt; 3249c8475461SChuck Lever struct sock_xprt *transport; 32500a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3251a246b010SChuck Lever 3252d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 3253d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 3254c8541ecdSChuck Lever if (IS_ERR(xprt)) 3255c8541ecdSChuck Lever return xprt; 3256c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3257a246b010SChuck Lever 3258ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 3259d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_udp_transport; 3260a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 3261a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3262a246b010SChuck Lever 326303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 326403bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 326503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3266a246b010SChuck Lever 3267262965f5SChuck Lever xprt->ops = &xs_udp_ops; 3268a246b010SChuck Lever 3269ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 3270a246b010SChuck Lever 3271f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 32724f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3273edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3274edc1b01cSTrond Myklebust 32758f9d5b1aSChuck Lever switch (addr->sa_family) { 32768f9d5b1aSChuck Lever case AF_INET: 32778f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 32788f9d5b1aSChuck Lever xprt_set_bound(xprt); 32798f9d5b1aSChuck Lever 32809dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 32818f9d5b1aSChuck Lever break; 32828f9d5b1aSChuck Lever case AF_INET6: 32838f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 32848f9d5b1aSChuck Lever xprt_set_bound(xprt); 32858f9d5b1aSChuck Lever 32869dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 32878f9d5b1aSChuck Lever break; 32888f9d5b1aSChuck Lever default: 32890a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 32900a68b0beSJ. Bruce Fields goto out_err; 32918f9d5b1aSChuck Lever } 32928f9d5b1aSChuck Lever 3293c740eff8SChuck Lever if (xprt_bound(xprt)) 3294c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3295c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3296c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3297c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3298c740eff8SChuck Lever else 3299c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3300c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3301c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3302edb267a6SChuck Lever 3303bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3304c8541ecdSChuck Lever return xprt; 33050a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 33060a68b0beSJ. Bruce Fields out_err: 3307315f3812SKinglong Mee xs_xprt_free(xprt); 33080a68b0beSJ. Bruce Fields return ret; 3309a246b010SChuck Lever } 3310a246b010SChuck Lever 33112881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 33122881ae74STrond Myklebust .to_initval = 60 * HZ, 33132881ae74STrond Myklebust .to_maxval = 60 * HZ, 33142881ae74STrond Myklebust .to_retries = 2, 33152881ae74STrond Myklebust }; 33162881ae74STrond Myklebust 33179903cd1cSChuck Lever /** 33189903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 331996802a09SFrank van Maarseveen * @args: rpc transport creation arguments 33209903cd1cSChuck Lever * 33219903cd1cSChuck Lever */ 3322483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3323a246b010SChuck Lever { 33248f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3325c8541ecdSChuck Lever struct rpc_xprt *xprt; 3326c8475461SChuck Lever struct sock_xprt *transport; 33270a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3328b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3329b7993cebSTrond Myklebust 3330b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3331b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3332a246b010SChuck Lever 3333d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3334b7993cebSTrond Myklebust max_slot_table_size); 3335c8541ecdSChuck Lever if (IS_ERR(xprt)) 3336c8541ecdSChuck Lever return xprt; 3337c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3338a246b010SChuck Lever 3339ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3340d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_tcp_transport; 3341808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3342a246b010SChuck Lever 334303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 334403bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 334503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3346a246b010SChuck Lever 3347262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3348ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3349a246b010SChuck Lever 33503851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 3351*d2ee4138STrond Myklebust if (args->reconnect_timeout) 3352*d2ee4138STrond Myklebust xprt->max_reconnect_timeout = args->reconnect_timeout; 3353*d2ee4138STrond Myklebust 33547196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 33557196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 3356*d2ee4138STrond Myklebust if (args->connect_timeout) 3357*d2ee4138STrond Myklebust xs_tcp_do_set_connect_timeout(xprt, args->connect_timeout); 33583851f1cdSTrond Myklebust 3359c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 33604f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3361edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3362edc1b01cSTrond Myklebust 33638f9d5b1aSChuck Lever switch (addr->sa_family) { 33648f9d5b1aSChuck Lever case AF_INET: 33658f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 33668f9d5b1aSChuck Lever xprt_set_bound(xprt); 33678f9d5b1aSChuck Lever 33689dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 33698f9d5b1aSChuck Lever break; 33708f9d5b1aSChuck Lever case AF_INET6: 33718f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 33728f9d5b1aSChuck Lever xprt_set_bound(xprt); 33738f9d5b1aSChuck Lever 33749dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 33758f9d5b1aSChuck Lever break; 33768f9d5b1aSChuck Lever default: 33770a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 33780a68b0beSJ. Bruce Fields goto out_err; 33798f9d5b1aSChuck Lever } 33808f9d5b1aSChuck Lever 3381c740eff8SChuck Lever if (xprt_bound(xprt)) 3382c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3383c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3384c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3385c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3386c740eff8SChuck Lever else 3387c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3388c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3389c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3390c740eff8SChuck Lever 3391bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3392c8541ecdSChuck Lever return xprt; 33930a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 33940a68b0beSJ. Bruce Fields out_err: 3395315f3812SKinglong Mee xs_xprt_free(xprt); 33960a68b0beSJ. Bruce Fields return ret; 3397a246b010SChuck Lever } 3398282b32e1SChuck Lever 3399f300babaSAlexandros Batsakis /** 340075eb6af7SChuck Lever * xs_setup_tcp_tls - Set up transport to use a TCP with TLS 340175eb6af7SChuck Lever * @args: rpc transport creation arguments 340275eb6af7SChuck Lever * 340375eb6af7SChuck Lever */ 340475eb6af7SChuck Lever static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args) 340575eb6af7SChuck Lever { 340675eb6af7SChuck Lever struct sockaddr *addr = args->dstaddr; 340775eb6af7SChuck Lever struct rpc_xprt *xprt; 340875eb6af7SChuck Lever struct sock_xprt *transport; 340975eb6af7SChuck Lever struct rpc_xprt *ret; 341075eb6af7SChuck Lever unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 341175eb6af7SChuck Lever 341275eb6af7SChuck Lever if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 341375eb6af7SChuck Lever max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 341475eb6af7SChuck Lever 341575eb6af7SChuck Lever xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 341675eb6af7SChuck Lever max_slot_table_size); 341775eb6af7SChuck Lever if (IS_ERR(xprt)) 341875eb6af7SChuck Lever return xprt; 341975eb6af7SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 342075eb6af7SChuck Lever 342175eb6af7SChuck Lever xprt->prot = IPPROTO_TCP; 342275eb6af7SChuck Lever xprt->xprt_class = &xs_tcp_transport; 342375eb6af7SChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 342475eb6af7SChuck Lever 342575eb6af7SChuck Lever xprt->bind_timeout = XS_BIND_TO; 342675eb6af7SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 342775eb6af7SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 342875eb6af7SChuck Lever 342975eb6af7SChuck Lever xprt->ops = &xs_tcp_ops; 343075eb6af7SChuck Lever xprt->timeout = &xs_tcp_default_timeout; 343175eb6af7SChuck Lever 343275eb6af7SChuck Lever xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 343375eb6af7SChuck Lever xprt->connect_timeout = xprt->timeout->to_initval * 343475eb6af7SChuck Lever (xprt->timeout->to_retries + 1); 343575eb6af7SChuck Lever 343675eb6af7SChuck Lever INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 343775eb6af7SChuck Lever INIT_WORK(&transport->error_worker, xs_error_handle); 343875eb6af7SChuck Lever 343975eb6af7SChuck Lever switch (args->xprtsec.policy) { 344075eb6af7SChuck Lever case RPC_XPRTSEC_TLS_ANON: 344175eb6af7SChuck Lever case RPC_XPRTSEC_TLS_X509: 344275eb6af7SChuck Lever xprt->xprtsec = args->xprtsec; 344375eb6af7SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 344475eb6af7SChuck Lever xs_tcp_tls_setup_socket); 344575eb6af7SChuck Lever break; 344675eb6af7SChuck Lever default: 344775eb6af7SChuck Lever ret = ERR_PTR(-EACCES); 344875eb6af7SChuck Lever goto out_err; 344975eb6af7SChuck Lever } 345075eb6af7SChuck Lever 345175eb6af7SChuck Lever switch (addr->sa_family) { 345275eb6af7SChuck Lever case AF_INET: 345375eb6af7SChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 345475eb6af7SChuck Lever xprt_set_bound(xprt); 345575eb6af7SChuck Lever 345675eb6af7SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 345775eb6af7SChuck Lever break; 345875eb6af7SChuck Lever case AF_INET6: 345975eb6af7SChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 346075eb6af7SChuck Lever xprt_set_bound(xprt); 346175eb6af7SChuck Lever 346275eb6af7SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 346375eb6af7SChuck Lever break; 346475eb6af7SChuck Lever default: 346575eb6af7SChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 346675eb6af7SChuck Lever goto out_err; 346775eb6af7SChuck Lever } 346875eb6af7SChuck Lever 346975eb6af7SChuck Lever if (xprt_bound(xprt)) 347075eb6af7SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 347175eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 347275eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 347375eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 347475eb6af7SChuck Lever else 347575eb6af7SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 347675eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 347775eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 347875eb6af7SChuck Lever 347975eb6af7SChuck Lever if (try_module_get(THIS_MODULE)) 348075eb6af7SChuck Lever return xprt; 348175eb6af7SChuck Lever ret = ERR_PTR(-EINVAL); 348275eb6af7SChuck Lever out_err: 348375eb6af7SChuck Lever xs_xprt_free(xprt); 348475eb6af7SChuck Lever return ret; 348575eb6af7SChuck Lever } 348675eb6af7SChuck Lever 348775eb6af7SChuck Lever /** 3488f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3489f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3490f300babaSAlexandros Batsakis * 3491f300babaSAlexandros Batsakis */ 3492f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3493f300babaSAlexandros Batsakis { 3494f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3495f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3496f300babaSAlexandros Batsakis struct sock_xprt *transport; 3497f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 34980a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3499f300babaSAlexandros Batsakis 3500d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3501d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3502f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3503f300babaSAlexandros Batsakis return xprt; 3504f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3505f300babaSAlexandros Batsakis 3506f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3507d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_bc_tcp_transport; 3508f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3509f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3510f300babaSAlexandros Batsakis 3511f300babaSAlexandros Batsakis /* backchannel */ 3512f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3513f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3514f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3515f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3516f300babaSAlexandros Batsakis 3517f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3518f300babaSAlexandros Batsakis 3519f300babaSAlexandros Batsakis switch (addr->sa_family) { 3520f300babaSAlexandros Batsakis case AF_INET: 3521f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3522f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3523f300babaSAlexandros Batsakis break; 3524f300babaSAlexandros Batsakis case AF_INET6: 3525f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3526f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3527f300babaSAlexandros Batsakis break; 3528f300babaSAlexandros Batsakis default: 35290a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 35300a68b0beSJ. Bruce Fields goto out_err; 3531f300babaSAlexandros Batsakis } 3532f300babaSAlexandros Batsakis 3533f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3534f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3535f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3536f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3537f300babaSAlexandros Batsakis 3538f300babaSAlexandros Batsakis /* 353999de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 354028303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 354128303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 354228303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 354399de8ea9SJ. Bruce Fields */ 354499de8ea9SJ. Bruce Fields xprt_get(xprt); 354599de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 354699de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 354799de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 354899de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 354999de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 355099de8ea9SJ. Bruce Fields 355199de8ea9SJ. Bruce Fields /* 3552f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3553f300babaSAlexandros Batsakis * the xprt status to connected 3554f300babaSAlexandros Batsakis */ 3555f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3556f300babaSAlexandros Batsakis 3557f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3558f300babaSAlexandros Batsakis return xprt; 3559642aab58SKinglong Mee 3560642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 356139a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 356299de8ea9SJ. Bruce Fields xprt_put(xprt); 35630a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 35640a68b0beSJ. Bruce Fields out_err: 3565315f3812SKinglong Mee xs_xprt_free(xprt); 35660a68b0beSJ. Bruce Fields return ret; 3567f300babaSAlexandros Batsakis } 3568f300babaSAlexandros Batsakis 3569176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3570176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3571176e21eeSChuck Lever .name = "named UNIX socket", 3572176e21eeSChuck Lever .owner = THIS_MODULE, 3573176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3574176e21eeSChuck Lever .setup = xs_setup_local, 3575d5aa6b22STrond Myklebust .netid = { "" }, 3576176e21eeSChuck Lever }; 3577176e21eeSChuck Lever 3578bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3579bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3580bc25571eS\"Talpey, Thomas\ .name = "udp", 3581bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3582f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3583bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3584d5aa6b22STrond Myklebust .netid = { "udp", "udp6", "" }, 3585bc25571eS\"Talpey, Thomas\ }; 3586bc25571eS\"Talpey, Thomas\ 3587bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3588bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3589bc25571eS\"Talpey, Thomas\ .name = "tcp", 3590bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3591f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3592bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3593d5aa6b22STrond Myklebust .netid = { "tcp", "tcp6", "" }, 3594bc25571eS\"Talpey, Thomas\ }; 3595bc25571eS\"Talpey, Thomas\ 359675eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport = { 359775eb6af7SChuck Lever .list = LIST_HEAD_INIT(xs_tcp_tls_transport.list), 359875eb6af7SChuck Lever .name = "tcp-with-tls", 359975eb6af7SChuck Lever .owner = THIS_MODULE, 360075eb6af7SChuck Lever .ident = XPRT_TRANSPORT_TCP_TLS, 360175eb6af7SChuck Lever .setup = xs_setup_tcp_tls, 360275eb6af7SChuck Lever .netid = { "tcp", "tcp6", "" }, 360375eb6af7SChuck Lever }; 360475eb6af7SChuck Lever 3605f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3606f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3607f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3608f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3609f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3610f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3611d5aa6b22STrond Myklebust .netid = { "" }, 3612f300babaSAlexandros Batsakis }; 3613f300babaSAlexandros Batsakis 3614282b32e1SChuck Lever /** 3615bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3616282b32e1SChuck Lever * 3617282b32e1SChuck Lever */ 3618282b32e1SChuck Lever int init_socket_xprt(void) 3619282b32e1SChuck Lever { 36202b1bec5fSEric W. Biederman if (!sunrpc_table_header) 3621c946cb69SLuis Chamberlain sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table); 3622fbf76683SChuck Lever 3623176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3624bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3625bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 362675eb6af7SChuck Lever xprt_register_transport(&xs_tcp_tls_transport); 3627f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3628bc25571eS\"Talpey, Thomas\ 3629282b32e1SChuck Lever return 0; 3630282b32e1SChuck Lever } 3631282b32e1SChuck Lever 3632282b32e1SChuck Lever /** 3633bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3634282b32e1SChuck Lever * 3635282b32e1SChuck Lever */ 3636282b32e1SChuck Lever void cleanup_socket_xprt(void) 3637282b32e1SChuck Lever { 3638fbf76683SChuck Lever if (sunrpc_table_header) { 3639fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3640fbf76683SChuck Lever sunrpc_table_header = NULL; 3641fbf76683SChuck Lever } 3642bc25571eS\"Talpey, Thomas\ 3643176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3644bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3645bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 364675eb6af7SChuck Lever xprt_unregister_transport(&xs_tcp_tls_transport); 3647f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3648282b32e1SChuck Lever } 3649cbf11071STrond Myklebust 36509bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3651cbf11071STrond Myklebust { 3652cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3653cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3654cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3655cbf11071STrond Myklebust } 3656cbf11071STrond Myklebust 36579c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 36589bbb9e5aSRusty Russell .set = param_set_portnr, 36599bbb9e5aSRusty Russell .get = param_get_uint, 36609bbb9e5aSRusty Russell }; 36619bbb9e5aSRusty Russell 3662cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3663cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3664cbf11071STrond Myklebust 3665cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3666cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3667cbf11071STrond Myklebust 36689bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 36699bbb9e5aSRusty Russell const struct kernel_param *kp) 3670cbf11071STrond Myklebust { 3671cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3672cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3673cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3674cbf11071STrond Myklebust } 3675cbf11071STrond Myklebust 36769c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 36779bbb9e5aSRusty Russell .set = param_set_slot_table_size, 36789bbb9e5aSRusty Russell .get = param_get_uint, 36799bbb9e5aSRusty Russell }; 36809bbb9e5aSRusty Russell 3681cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3682cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3683cbf11071STrond Myklebust 3684d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3685d9ba131dSTrond Myklebust const struct kernel_param *kp) 3686d9ba131dSTrond Myklebust { 3687d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3688d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3689d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3690d9ba131dSTrond Myklebust } 3691d9ba131dSTrond Myklebust 36929c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3693d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3694d9ba131dSTrond Myklebust .get = param_get_uint, 3695d9ba131dSTrond Myklebust }; 3696d9ba131dSTrond Myklebust 3697d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3698d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3699d9ba131dSTrond Myklebust 3700cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3701cbf11071STrond Myklebust slot_table_size, 0644); 3702d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3703d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3704cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3705cbf11071STrond Myklebust slot_table_size, 0644); 3706