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> 506a7eccefSChuck Lever #include <net/tls_prot.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 { 36339067ddaSChuck Lever u8 content_type = tls_get_record_type(sock->sk, cmsg); 36439067ddaSChuck Lever u8 level, description; 365dea034b9SChuck Lever 366dea034b9SChuck Lever switch (content_type) { 36739067ddaSChuck Lever case 0: 36839067ddaSChuck Lever break; 369dea034b9SChuck Lever case TLS_RECORD_TYPE_DATA: 370dea034b9SChuck Lever /* TLS sets EOR at the end of each application data 371dea034b9SChuck Lever * record, even though there might be more frames 372dea034b9SChuck Lever * waiting to be decrypted. 373dea034b9SChuck Lever */ 374dea034b9SChuck Lever msg->msg_flags &= ~MSG_EOR; 375dea034b9SChuck Lever break; 376dea034b9SChuck Lever case TLS_RECORD_TYPE_ALERT: 37739067ddaSChuck Lever tls_alert_recv(sock->sk, msg, &level, &description); 37839067ddaSChuck Lever ret = (level == TLS_ALERT_LEVEL_FATAL) ? 37939067ddaSChuck Lever -EACCES : -EAGAIN; 380dea034b9SChuck Lever break; 381dea034b9SChuck Lever default: 38239067ddaSChuck Lever /* discard this record type */ 383dea034b9SChuck Lever ret = -EAGAIN; 384dea034b9SChuck Lever } 385dea034b9SChuck Lever return ret; 386dea034b9SChuck Lever } 387dea034b9SChuck Lever 388dea034b9SChuck Lever static int 389dea034b9SChuck Lever xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags) 390dea034b9SChuck Lever { 391dea034b9SChuck Lever union { 392dea034b9SChuck Lever struct cmsghdr cmsg; 393dea034b9SChuck Lever u8 buf[CMSG_SPACE(sizeof(u8))]; 394dea034b9SChuck Lever } u; 395dea034b9SChuck Lever int ret; 396dea034b9SChuck Lever 397dea034b9SChuck Lever msg->msg_control = &u; 398dea034b9SChuck Lever msg->msg_controllen = sizeof(u); 399dea034b9SChuck Lever ret = sock_recvmsg(sock, msg, flags); 400dea034b9SChuck Lever if (msg->msg_controllen != sizeof(u)) 401dea034b9SChuck Lever ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret); 402dea034b9SChuck Lever return ret; 403dea034b9SChuck Lever } 404dea034b9SChuck Lever 405277e4ab7STrond Myklebust static ssize_t 406277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 407277e4ab7STrond Myklebust { 408277e4ab7STrond Myklebust ssize_t ret; 409277e4ab7STrond Myklebust if (seek != 0) 410277e4ab7STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 411dea034b9SChuck Lever ret = xs_sock_recv_cmsg(sock, msg, flags); 412277e4ab7STrond Myklebust return ret > 0 ? ret + seek : ret; 413277e4ab7STrond Myklebust } 414277e4ab7STrond Myklebust 415277e4ab7STrond Myklebust static ssize_t 416277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 417277e4ab7STrond Myklebust struct kvec *kvec, size_t count, size_t seek) 418277e4ab7STrond Myklebust { 419de4eda9dSAl Viro iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count); 420277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 421277e4ab7STrond Myklebust } 422277e4ab7STrond Myklebust 423277e4ab7STrond Myklebust static ssize_t 424277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 425277e4ab7STrond Myklebust struct bio_vec *bvec, unsigned long nr, size_t count, 426277e4ab7STrond Myklebust size_t seek) 427277e4ab7STrond Myklebust { 428de4eda9dSAl Viro iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count); 429277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 430277e4ab7STrond Myklebust } 431277e4ab7STrond Myklebust 432277e4ab7STrond Myklebust static ssize_t 433277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 434277e4ab7STrond Myklebust size_t count) 435277e4ab7STrond Myklebust { 436de4eda9dSAl Viro iov_iter_discard(&msg->msg_iter, ITER_DEST, count); 437dea034b9SChuck Lever return xs_sock_recv_cmsg(sock, msg, flags); 438277e4ab7STrond Myklebust } 439277e4ab7STrond Myklebust 4406a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 4416a829eb8STrond Myklebust static void 4426a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 4436a829eb8STrond Myklebust { 4446a829eb8STrond Myklebust struct bvec_iter bi = { 4456a829eb8STrond Myklebust .bi_size = count, 4466a829eb8STrond Myklebust }; 4476a829eb8STrond Myklebust struct bio_vec bv; 4486a829eb8STrond Myklebust 4496a829eb8STrond Myklebust bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 4506a829eb8STrond Myklebust for_each_bvec(bv, bvec, bi, bi) 4516a829eb8STrond Myklebust flush_dcache_page(bv.bv_page); 4526a829eb8STrond Myklebust } 4536a829eb8STrond Myklebust #else 4546a829eb8STrond Myklebust static inline void 4556a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 4566a829eb8STrond Myklebust { 4576a829eb8STrond Myklebust } 4586a829eb8STrond Myklebust #endif 4596a829eb8STrond Myklebust 460277e4ab7STrond Myklebust static ssize_t 461277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 462277e4ab7STrond Myklebust struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 463277e4ab7STrond Myklebust { 464277e4ab7STrond Myklebust size_t want, seek_init = seek, offset = 0; 465277e4ab7STrond Myklebust ssize_t ret; 466277e4ab7STrond Myklebust 467277e4ab7STrond Myklebust want = min_t(size_t, count, buf->head[0].iov_len); 468e92053a5STrond Myklebust if (seek < want) { 469277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 470277e4ab7STrond Myklebust if (ret <= 0) 471277e4ab7STrond Myklebust goto sock_err; 472277e4ab7STrond Myklebust offset += ret; 473277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 474277e4ab7STrond Myklebust goto out; 475277e4ab7STrond Myklebust if (ret != want) 476c4433055STrond Myklebust goto out; 477277e4ab7STrond Myklebust seek = 0; 478277e4ab7STrond Myklebust } else { 479e92053a5STrond Myklebust seek -= want; 480e92053a5STrond Myklebust offset += want; 481277e4ab7STrond Myklebust } 48216e5e90fSTrond Myklebust 483b2648015STrond Myklebust want = xs_alloc_sparse_pages( 484b2648015STrond Myklebust buf, min_t(size_t, count - offset, buf->page_len), 485b2648015STrond Myklebust GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 48616e5e90fSTrond Myklebust if (seek < want) { 487277e4ab7STrond Myklebust ret = xs_read_bvec(sock, msg, flags, buf->bvec, 488277e4ab7STrond Myklebust xdr_buf_pagecount(buf), 489277e4ab7STrond Myklebust want + buf->page_base, 490277e4ab7STrond Myklebust seek + buf->page_base); 491277e4ab7STrond Myklebust if (ret <= 0) 492277e4ab7STrond Myklebust goto sock_err; 4936a829eb8STrond Myklebust xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 494ac9645c8SDan Aloni ret -= buf->page_base; 495ac9645c8SDan Aloni offset += ret; 496277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 497277e4ab7STrond Myklebust goto out; 498277e4ab7STrond Myklebust if (ret != want) 499c4433055STrond Myklebust goto out; 500277e4ab7STrond Myklebust seek = 0; 501277e4ab7STrond Myklebust } else { 50216e5e90fSTrond Myklebust seek -= want; 50316e5e90fSTrond Myklebust offset += want; 504277e4ab7STrond Myklebust } 50516e5e90fSTrond Myklebust 506277e4ab7STrond Myklebust want = min_t(size_t, count - offset, buf->tail[0].iov_len); 507e92053a5STrond Myklebust if (seek < want) { 508277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 509277e4ab7STrond Myklebust if (ret <= 0) 510277e4ab7STrond Myklebust goto sock_err; 511277e4ab7STrond Myklebust offset += ret; 512277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 513277e4ab7STrond Myklebust goto out; 514277e4ab7STrond Myklebust if (ret != want) 515c4433055STrond Myklebust goto out; 5169734ad57STrond Myklebust } else if (offset < seek_init) 517e92053a5STrond Myklebust offset = seek_init; 518277e4ab7STrond Myklebust ret = -EMSGSIZE; 519277e4ab7STrond Myklebust out: 520277e4ab7STrond Myklebust *read = offset - seek_init; 521277e4ab7STrond Myklebust return ret; 522277e4ab7STrond Myklebust sock_err: 523277e4ab7STrond Myklebust offset += seek; 524277e4ab7STrond Myklebust goto out; 525277e4ab7STrond Myklebust } 526277e4ab7STrond Myklebust 527277e4ab7STrond Myklebust static void 528277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 529277e4ab7STrond Myklebust { 530277e4ab7STrond Myklebust if (!transport->recv.copied) { 531277e4ab7STrond Myklebust if (buf->head[0].iov_len >= transport->recv.offset) 532277e4ab7STrond Myklebust memcpy(buf->head[0].iov_base, 533277e4ab7STrond Myklebust &transport->recv.xid, 534277e4ab7STrond Myklebust transport->recv.offset); 535277e4ab7STrond Myklebust transport->recv.copied = transport->recv.offset; 536277e4ab7STrond Myklebust } 537277e4ab7STrond Myklebust } 538277e4ab7STrond Myklebust 539277e4ab7STrond Myklebust static bool 540277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport) 541277e4ab7STrond Myklebust { 542277e4ab7STrond Myklebust return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 543277e4ab7STrond Myklebust } 544277e4ab7STrond Myklebust 545e92053a5STrond Myklebust static void 546e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport, 547e92053a5STrond Myklebust struct msghdr *msg) 548e92053a5STrond Myklebust { 549e92053a5STrond Myklebust if (xs_read_stream_request_done(transport)) 550e92053a5STrond Myklebust msg->msg_flags |= MSG_EOR; 551e92053a5STrond Myklebust } 552e92053a5STrond Myklebust 553277e4ab7STrond Myklebust static ssize_t 554277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 555277e4ab7STrond Myklebust int flags, struct rpc_rqst *req) 556277e4ab7STrond Myklebust { 557277e4ab7STrond Myklebust struct xdr_buf *buf = &req->rq_private_buf; 5583f649ab7SKees Cook size_t want, read; 5593f649ab7SKees Cook ssize_t ret; 560277e4ab7STrond Myklebust 561277e4ab7STrond Myklebust xs_read_header(transport, buf); 562277e4ab7STrond Myklebust 563277e4ab7STrond Myklebust want = transport->recv.len - transport->recv.offset; 564e92053a5STrond Myklebust if (want != 0) { 565277e4ab7STrond Myklebust ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 566e92053a5STrond Myklebust transport->recv.copied + want, 567e92053a5STrond Myklebust transport->recv.copied, 568277e4ab7STrond Myklebust &read); 569277e4ab7STrond Myklebust transport->recv.offset += read; 570277e4ab7STrond Myklebust transport->recv.copied += read; 571277e4ab7STrond Myklebust } 572277e4ab7STrond Myklebust 573727fcc64STrond Myklebust if (transport->recv.offset == transport->recv.len) 574727fcc64STrond Myklebust xs_read_stream_check_eor(transport, msg); 575727fcc64STrond Myklebust 576e92053a5STrond Myklebust if (want == 0) 577e92053a5STrond Myklebust return 0; 578e92053a5STrond Myklebust 579277e4ab7STrond Myklebust switch (ret) { 580c4433055STrond Myklebust default: 581c4433055STrond Myklebust break; 58226781eabSTrond Myklebust case -EFAULT: 583277e4ab7STrond Myklebust case -EMSGSIZE: 58426781eabSTrond Myklebust msg->msg_flags |= MSG_TRUNC; 585c4433055STrond Myklebust return read; 586277e4ab7STrond Myklebust case 0: 587277e4ab7STrond Myklebust return -ESHUTDOWN; 588277e4ab7STrond Myklebust } 589c4433055STrond Myklebust return ret < 0 ? ret : read; 590277e4ab7STrond Myklebust } 591277e4ab7STrond Myklebust 592277e4ab7STrond Myklebust static size_t 593277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag) 594277e4ab7STrond Myklebust { 595277e4ab7STrond Myklebust if (isfrag) 596277e4ab7STrond Myklebust return sizeof(__be32); 597277e4ab7STrond Myklebust return 3 * sizeof(__be32); 598277e4ab7STrond Myklebust } 599277e4ab7STrond Myklebust 600277e4ab7STrond Myklebust static ssize_t 601277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 602277e4ab7STrond Myklebust int flags, size_t want, size_t seek) 603277e4ab7STrond Myklebust { 604277e4ab7STrond Myklebust struct kvec kvec = { 605277e4ab7STrond Myklebust .iov_base = &transport->recv.fraghdr, 606277e4ab7STrond Myklebust .iov_len = want, 607277e4ab7STrond Myklebust }; 608277e4ab7STrond Myklebust return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 609277e4ab7STrond Myklebust } 610277e4ab7STrond Myklebust 611277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 612277e4ab7STrond Myklebust static ssize_t 613277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 614277e4ab7STrond Myklebust { 615277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 616277e4ab7STrond Myklebust struct rpc_rqst *req; 617277e4ab7STrond Myklebust ssize_t ret; 618277e4ab7STrond Myklebust 61998b5cee3SBenjamin Coddington /* Is this transport associated with the backchannel? */ 62098b5cee3SBenjamin Coddington if (!xprt->bc_serv) 62198b5cee3SBenjamin Coddington return -ESHUTDOWN; 62298b5cee3SBenjamin Coddington 623277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 624277e4ab7STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 625277e4ab7STrond Myklebust if (!req) { 626277e4ab7STrond Myklebust printk(KERN_WARNING "Callback slot table overflowed\n"); 627277e4ab7STrond Myklebust return -ESHUTDOWN; 628277e4ab7STrond Myklebust } 62945835a63STrond Myklebust if (transport->recv.copied && !req->rq_private_buf.len) 63045835a63STrond Myklebust return -ESHUTDOWN; 631277e4ab7STrond Myklebust 632277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 633277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 634c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 63545835a63STrond Myklebust else 63645835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 637277e4ab7STrond Myklebust 638277e4ab7STrond Myklebust return ret; 639277e4ab7STrond Myklebust } 640277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 641277e4ab7STrond Myklebust static ssize_t 642277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 643277e4ab7STrond Myklebust { 644277e4ab7STrond Myklebust return -ESHUTDOWN; 645277e4ab7STrond Myklebust } 646277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 647277e4ab7STrond Myklebust 648277e4ab7STrond Myklebust static ssize_t 649277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 650277e4ab7STrond Myklebust { 651277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 652277e4ab7STrond Myklebust struct rpc_rqst *req; 653277e4ab7STrond Myklebust ssize_t ret = 0; 654277e4ab7STrond Myklebust 655277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 656277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 657277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 65845835a63STrond Myklebust if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { 659277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 660277e4ab7STrond Myklebust goto out; 661277e4ab7STrond Myklebust } 662277e4ab7STrond Myklebust xprt_pin_rqst(req); 663277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 664277e4ab7STrond Myklebust 665277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 666277e4ab7STrond Myklebust 667277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 668277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 669c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 67045835a63STrond Myklebust else 67145835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 672277e4ab7STrond Myklebust xprt_unpin_rqst(req); 673277e4ab7STrond Myklebust out: 674277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 675277e4ab7STrond Myklebust return ret; 676277e4ab7STrond Myklebust } 677277e4ab7STrond Myklebust 678277e4ab7STrond Myklebust static ssize_t 679277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 680277e4ab7STrond Myklebust { 681277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 682277e4ab7STrond Myklebust size_t want, read = 0; 683277e4ab7STrond Myklebust ssize_t ret = 0; 684277e4ab7STrond Myklebust 685277e4ab7STrond Myklebust if (transport->recv.len == 0) { 686277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 687277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 688277e4ab7STrond Myklebust transport->recv.offset); 689277e4ab7STrond Myklebust if (ret <= 0) 690277e4ab7STrond Myklebust goto out_err; 691277e4ab7STrond Myklebust transport->recv.offset = ret; 692c4433055STrond Myklebust if (transport->recv.offset != want) 693c4433055STrond Myklebust return transport->recv.offset; 694277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 695277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 696277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 697277e4ab7STrond Myklebust read = ret; 698277e4ab7STrond Myklebust } 699277e4ab7STrond Myklebust 700277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 701c4433055STrond Myklebust default: 702c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 703c4433055STrond Myklebust break; 704277e4ab7STrond Myklebust case RPC_CALL: 705277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 706277e4ab7STrond Myklebust break; 707277e4ab7STrond Myklebust case RPC_REPLY: 708277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 709277e4ab7STrond Myklebust } 710277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 711277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 712277e4ab7STrond Myklebust transport->recv.copied = -1; 713277e4ab7STrond Myklebust } 714277e4ab7STrond Myklebust if (ret < 0) 715277e4ab7STrond Myklebust goto out_err; 716277e4ab7STrond Myklebust read += ret; 717277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 718c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 719c4433055STrond Myklebust return read; 720b76a5afdSTrond Myklebust msg.msg_flags = 0; 721277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 722277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 723277e4ab7STrond Myklebust if (ret <= 0) 724277e4ab7STrond Myklebust goto out_err; 725277e4ab7STrond Myklebust transport->recv.offset += ret; 726277e4ab7STrond Myklebust read += ret; 727277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 728c4433055STrond Myklebust return read; 729277e4ab7STrond Myklebust } 730277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 731c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 732277e4ab7STrond Myklebust transport->recv.copied = 0; 733277e4ab7STrond Myklebust } 734277e4ab7STrond Myklebust transport->recv.offset = 0; 735277e4ab7STrond Myklebust transport->recv.len = 0; 736277e4ab7STrond Myklebust return read; 737277e4ab7STrond Myklebust out_err: 73879462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 739277e4ab7STrond Myklebust } 740277e4ab7STrond Myklebust 7410ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 7420ffe86f4STrond Myklebust { 743a73881c9STrond Myklebust return transport->sock->ops->poll(transport->file, transport->sock, 744a73881c9STrond Myklebust NULL); 7450ffe86f4STrond Myklebust } 7460ffe86f4STrond Myklebust 7470ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 7480ffe86f4STrond Myklebust { 7490ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 7500ffe86f4STrond Myklebust 7510ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 7520ffe86f4STrond Myklebust } 7530ffe86f4STrond Myklebust 7540ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 7550ffe86f4STrond Myklebust { 7560ffe86f4STrond Myklebust 7570ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 7580d3ca07fSChuck Lever if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 7590d3ca07fSChuck Lever return; 7600ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 7610ffe86f4STrond Myklebust return; 7620ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 7630ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 7640ffe86f4STrond Myklebust } 7650ffe86f4STrond Myklebust 766c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 767c50b8ee0STrond Myklebust { 768c50b8ee0STrond Myklebust size_t read = 0; 769c50b8ee0STrond Myklebust ssize_t ret = 0; 770c50b8ee0STrond Myklebust 771c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 772c50b8ee0STrond Myklebust if (transport->sock == NULL) 773c50b8ee0STrond Myklebust goto out; 774c50b8ee0STrond Myklebust for (;;) { 775c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 776c4433055STrond Myklebust if (ret < 0) 777c50b8ee0STrond Myklebust break; 778c50b8ee0STrond Myklebust read += ret; 779c50b8ee0STrond Myklebust cond_resched(); 780c50b8ee0STrond Myklebust } 7815f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7825f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 78339067ddaSChuck Lever else if (ret == -EACCES) 78439067ddaSChuck Lever xprt_wake_pending_tasks(&transport->xprt, -EACCES); 7855f52a9d4STrond Myklebust else 7860ffe86f4STrond Myklebust xs_poll_check_readable(transport); 787c50b8ee0STrond Myklebust out: 788c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 789c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 790c50b8ee0STrond Myklebust } 791c50b8ee0STrond Myklebust 792c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 793c50b8ee0STrond Myklebust { 794c50b8ee0STrond Myklebust struct sock_xprt *transport = 795c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 796a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 797a1231fdaSTrond Myklebust 798c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 799a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 800c50b8ee0STrond Myklebust } 801c50b8ee0STrond Myklebust 802550aebfeSTrond Myklebust static void 803550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 804550aebfeSTrond Myklebust { 805550aebfeSTrond Myklebust transport->recv.offset = 0; 806550aebfeSTrond Myklebust transport->recv.len = 0; 807550aebfeSTrond Myklebust transport->recv.copied = 0; 808550aebfeSTrond Myklebust transport->xmit.offset = 0; 809ae053551STrond Myklebust } 810ae053551STrond Myklebust 811ae053551STrond Myklebust static void 812ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 813ae053551STrond Myklebust { 814550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 815550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 816550aebfeSTrond Myklebust } 817550aebfeSTrond Myklebust 818b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 819b4b5cc85SChuck Lever 8209903cd1cSChuck Lever /** 821c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 82250f484e2STrond Myklebust * @req: pointer to RPC request 8237496b59fSTrond Myklebust * @transport: pointer to struct sock_xprt 8249903cd1cSChuck Lever * 825a246b010SChuck Lever */ 8267496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport) 827a246b010SChuck Lever { 8287496b59fSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 82906ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 83024ca9a84STrond Myklebust int ret = -EAGAIN; 831a246b010SChuck Lever 832015747d2SChuck Lever trace_rpc_socket_nospace(req, transport); 833a246b010SChuck Lever 834262965f5SChuck Lever /* Protect against races with write_space */ 835b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 836a246b010SChuck Lever 837262965f5SChuck Lever /* Don't race with disconnect */ 838b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 8397496b59fSTrond Myklebust /* wait for more buffer space */ 8402790a624STrond Myklebust set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 8417496b59fSTrond Myklebust set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 8427496b59fSTrond Myklebust sk->sk_write_pending++; 8437496b59fSTrond Myklebust xprt_wait_for_buffer_space(xprt); 8447496b59fSTrond Myklebust } else 8457496b59fSTrond Myklebust ret = -ENOTCONN; 8467496b59fSTrond Myklebust 8477496b59fSTrond Myklebust spin_unlock(&xprt->transport_lock); 8487496b59fSTrond Myklebust return ret; 849d48f9ce7SDavid Vrabel } 8507496b59fSTrond Myklebust 8517496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req) 8527496b59fSTrond Myklebust { 8537496b59fSTrond Myklebust struct sock_xprt *transport = 8547496b59fSTrond Myklebust container_of(req->rq_xprt, struct sock_xprt, xprt); 8557496b59fSTrond Myklebust struct sock *sk = transport->inet; 8567496b59fSTrond Myklebust int ret = -EAGAIN; 8577496b59fSTrond Myklebust 8587496b59fSTrond Myklebust lock_sock(sk); 8597496b59fSTrond Myklebust if (!sock_writeable(sk)) 8607496b59fSTrond Myklebust ret = xs_nospace(req, transport); 8617496b59fSTrond Myklebust release_sock(sk); 8627496b59fSTrond Myklebust return ret; 8637496b59fSTrond Myklebust } 8647496b59fSTrond Myklebust 865d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait) 8667496b59fSTrond Myklebust { 8677496b59fSTrond Myklebust struct sock_xprt *transport = 8687496b59fSTrond Myklebust container_of(req->rq_xprt, struct sock_xprt, xprt); 8697496b59fSTrond Myklebust struct sock *sk = transport->inet; 8707496b59fSTrond Myklebust int ret = -EAGAIN; 8717496b59fSTrond Myklebust 872d0afde5fSTrond Myklebust if (vm_wait) 873d0afde5fSTrond Myklebust return -ENOBUFS; 8747496b59fSTrond Myklebust lock_sock(sk); 8757496b59fSTrond Myklebust if (!sk_stream_memory_free(sk)) 8767496b59fSTrond Myklebust ret = xs_nospace(req, transport); 8777496b59fSTrond Myklebust release_sock(sk); 8785e3771ceSTrond Myklebust return ret; 879a246b010SChuck Lever } 880a246b010SChuck Lever 88172691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf) 882277e4ab7STrond Myklebust { 88372691a26STrond Myklebust return xdr_alloc_bvec(buf, rpc_task_gfp_mask()); 884277e4ab7STrond Myklebust } 885277e4ab7STrond Myklebust 88661677eeeSChuck Lever /* 8874cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 8884cd34e7cSTrond Myklebust * could complete transmission. 8894cd34e7cSTrond Myklebust */ 8904cd34e7cSTrond Myklebust static bool 8914cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 8924cd34e7cSTrond Myklebust { 8934cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 8944cd34e7cSTrond Myklebust } 8954cd34e7cSTrond Myklebust 8964cd34e7cSTrond Myklebust /* 89706b5fc3aSTrond Myklebust * Return the stream record marker field for a record of length < 2^31-1 89861677eeeSChuck Lever */ 89906b5fc3aSTrond Myklebust static rpc_fraghdr 90006b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr) 90161677eeeSChuck Lever { 90206b5fc3aSTrond Myklebust if (!xdr->len) 90306b5fc3aSTrond Myklebust return 0; 90406b5fc3aSTrond Myklebust return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 90561677eeeSChuck Lever } 90661677eeeSChuck Lever 9079903cd1cSChuck Lever /** 908176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 90950f484e2STrond Myklebust * @req: pointer to RPC request 910176e21eeSChuck Lever * 911176e21eeSChuck Lever * Return values: 912176e21eeSChuck Lever * 0: The request has been sent 913176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 914176e21eeSChuck Lever * complete the request 915176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 91612b20ce3SBhaskar Chowdhury * other: Some other error occurred, the request was not sent 917176e21eeSChuck Lever */ 918adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 919176e21eeSChuck Lever { 920176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 921176e21eeSChuck Lever struct sock_xprt *transport = 922176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 923176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9247e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 9257e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 9269e55eef4SChuck Lever struct msghdr msg = { 9279e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9289e55eef4SChuck Lever }; 929d0afde5fSTrond Myklebust bool vm_wait; 9303f649ab7SKees Cook unsigned int sent; 931176e21eeSChuck Lever int status; 932176e21eeSChuck Lever 9334cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 9344cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 935f0043206STrond Myklebust xprt_force_disconnect(xprt); 9364cd34e7cSTrond Myklebust return -ENOTCONN; 9374cd34e7cSTrond Myklebust } 9384cd34e7cSTrond Myklebust 939176e21eeSChuck Lever xs_pktdump("packet data:", 940176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 941176e21eeSChuck Lever 942d0afde5fSTrond Myklebust vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 943d0afde5fSTrond Myklebust 94478215759SChuck Lever req->rq_xtime = ktime_get(); 9459e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 9467e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 947176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 9486c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 949743c69e7SNeilBrown 950f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 9516c7a64e5STrond Myklebust transport->xmit.offset += sent; 9526c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 9537e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 9546c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 9556c7a64e5STrond Myklebust transport->xmit.offset = 0; 956176e21eeSChuck Lever return 0; 957176e21eeSChuck Lever } 958176e21eeSChuck Lever status = -EAGAIN; 959d0afde5fSTrond Myklebust vm_wait = false; 960176e21eeSChuck Lever } 961176e21eeSChuck Lever 962176e21eeSChuck Lever switch (status) { 963176e21eeSChuck Lever case -EAGAIN: 964d0afde5fSTrond Myklebust status = xs_stream_nospace(req, vm_wait); 965176e21eeSChuck Lever break; 966176e21eeSChuck Lever default: 967176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 968176e21eeSChuck Lever -status); 969df561f66SGustavo A. R. Silva fallthrough; 970176e21eeSChuck Lever case -EPIPE: 971f0043206STrond Myklebust xprt_force_disconnect(xprt); 972176e21eeSChuck Lever status = -ENOTCONN; 973176e21eeSChuck Lever } 974176e21eeSChuck Lever 975176e21eeSChuck Lever return status; 976176e21eeSChuck Lever } 977176e21eeSChuck Lever 978176e21eeSChuck Lever /** 979262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 98050f484e2STrond Myklebust * @req: pointer to RPC request 9819903cd1cSChuck Lever * 9829903cd1cSChuck Lever * Return values: 9839903cd1cSChuck Lever * 0: The request has been sent 9849903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 9859903cd1cSChuck Lever * complete the request 986262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 98725985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 9889903cd1cSChuck Lever */ 989adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 990a246b010SChuck Lever { 991a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 992ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 993262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9949e55eef4SChuck Lever struct msghdr msg = { 9959e55eef4SChuck Lever .msg_name = xs_addr(xprt), 9969e55eef4SChuck Lever .msg_namelen = xprt->addrlen, 9979e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9989e55eef4SChuck Lever }; 9993f649ab7SKees Cook unsigned int sent; 1000262965f5SChuck Lever int status; 1001262965f5SChuck Lever 1002262965f5SChuck Lever xs_pktdump("packet data:", 1003262965f5SChuck Lever req->rq_svec->iov_base, 1004262965f5SChuck Lever req->rq_svec->iov_len); 1005262965f5SChuck Lever 100601d37c42STrond Myklebust if (!xprt_bound(xprt)) 100701d37c42STrond Myklebust return -ENOTCONN; 100875891f50STrond Myklebust 100975891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 101075891f50STrond Myklebust return -EBADSLT; 101175891f50STrond Myklebust 1012ff053dbbSTrond Myklebust status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 1013ff053dbbSTrond Myklebust if (status < 0) 1014ff053dbbSTrond Myklebust return status; 101578215759SChuck Lever req->rq_xtime = ktime_get(); 10169e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent); 1017262965f5SChuck Lever 1018262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 10196c7a64e5STrond Myklebust xdr->len, status); 1020262965f5SChuck Lever 10213dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 10223dedbb5cSJason Baron if (status == -EPERM) 10233dedbb5cSJason Baron goto process_status; 10243dedbb5cSJason Baron 1025743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 1026743c69e7SNeilBrown status = -ENOBUFS; 1027743c69e7SNeilBrown 1028f279cd00SJason Baron if (sent > 0 || status == 0) { 1029f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 1030f279cd00SJason Baron if (sent >= req->rq_slen) 1031262965f5SChuck Lever return 0; 1032262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 1033262965f5SChuck Lever status = -EAGAIN; 10342199700fSTrond Myklebust } 1035262965f5SChuck Lever 10363dedbb5cSJason Baron process_status: 1037262965f5SChuck Lever switch (status) { 1038fba91afbSTrond Myklebust case -ENOTSOCK: 1039fba91afbSTrond Myklebust status = -ENOTCONN; 1040fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1041fba91afbSTrond Myklebust break; 1042b6ddf64fSTrond Myklebust case -EAGAIN: 10437496b59fSTrond Myklebust status = xs_sock_nospace(req); 1044b6ddf64fSTrond Myklebust break; 1045262965f5SChuck Lever case -ENETUNREACH: 10463601c4a9STrond Myklebust case -ENOBUFS: 1047262965f5SChuck Lever case -EPIPE: 1048262965f5SChuck Lever case -ECONNREFUSED: 10493dedbb5cSJason Baron case -EPERM: 1050262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 1051262965f5SChuck Lever * prompts ECONNREFUSED. */ 105213331a55STrond Myklebust break; 105313331a55STrond Myklebust default: 105413331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 105513331a55STrond Myklebust -status); 1056262965f5SChuck Lever } 10575fe46e9dSBian Naimeng 1058262965f5SChuck Lever return status; 1059262965f5SChuck Lever } 1060262965f5SChuck Lever 1061e06799f9STrond Myklebust /** 1062262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 106350f484e2STrond Myklebust * @req: pointer to RPC request 1064262965f5SChuck Lever * 1065262965f5SChuck Lever * Return values: 1066262965f5SChuck Lever * 0: The request has been sent 1067262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 1068262965f5SChuck Lever * complete the request 1069262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 107025985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 1071262965f5SChuck Lever * 1072262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 1073262965f5SChuck Lever * if sendmsg is not able to make progress? 1074262965f5SChuck Lever */ 1075adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 1076262965f5SChuck Lever { 1077262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1078ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1079262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 10807e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 10817e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 10829e55eef4SChuck Lever struct msghdr msg = { 10839e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 10849e55eef4SChuck Lever }; 1085d0afde5fSTrond Myklebust bool vm_wait; 10863f649ab7SKees Cook unsigned int sent; 1087b595bb15SChuck Lever int status; 1088a246b010SChuck Lever 10894cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 10904cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 10914cd34e7cSTrond Myklebust if (transport->sock != NULL) 10924cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 10934cd34e7cSTrond Myklebust return -ENOTCONN; 10944cd34e7cSTrond Myklebust } 1095d275880aSTrond Myklebust if (!transport->inet) 1096d275880aSTrond Myklebust return -ENOTCONN; 10974cd34e7cSTrond Myklebust 1098262965f5SChuck Lever xs_pktdump("packet data:", 1099262965f5SChuck Lever req->rq_svec->iov_base, 1100262965f5SChuck Lever req->rq_svec->iov_len); 1101a246b010SChuck Lever 11027196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 11037196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 11047196dbb0STrond Myklebust 11053b21f757STrond Myklebust xs_set_srcport(transport, transport->sock); 11063b21f757STrond Myklebust 1107a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1108a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1109262965f5SChuck Lever * called sendmsg(). */ 111078215759SChuck Lever req->rq_xtime = ktime_get(); 1111d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, true); 1112d0afde5fSTrond Myklebust 1113d0afde5fSTrond Myklebust vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 1114d0afde5fSTrond Myklebust 1115d0afde5fSTrond Myklebust do { 11169e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 11177e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 1118a246b010SChuck Lever 1119262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 11206c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1121262965f5SChuck Lever 1122a246b010SChuck Lever /* If we've sent the entire packet, immediately 1123a246b010SChuck Lever * reset the count of bytes sent. */ 11246c7a64e5STrond Myklebust transport->xmit.offset += sent; 11256c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 11267e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 11276c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 11286c7a64e5STrond Myklebust transport->xmit.offset = 0; 1129d737e5d4STrond Myklebust if (atomic_long_read(&xprt->xmit_queuelen) == 1) 1130d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, false); 1131a246b010SChuck Lever return 0; 1132a246b010SChuck Lever } 1133262965f5SChuck Lever 11349ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 11359ffadfbcSTrond Myklebust 1136d0afde5fSTrond Myklebust if (sent > 0) 11379ffadfbcSTrond Myklebust vm_wait = false; 1138d0afde5fSTrond Myklebust 1139d0afde5fSTrond Myklebust } while (status == 0); 1140a246b010SChuck Lever 1141262965f5SChuck Lever switch (status) { 1142fba91afbSTrond Myklebust case -ENOTSOCK: 1143fba91afbSTrond Myklebust status = -ENOTCONN; 1144fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1145fba91afbSTrond Myklebust break; 1146262965f5SChuck Lever case -EAGAIN: 1147d0afde5fSTrond Myklebust status = xs_stream_nospace(req, vm_wait); 1148262965f5SChuck Lever break; 1149262965f5SChuck Lever case -ECONNRESET: 1150262965f5SChuck Lever case -ECONNREFUSED: 1151262965f5SChuck Lever case -ENOTCONN: 11523913c78cSTrond Myklebust case -EADDRINUSE: 1153b5872f0cSTrond Myklebust case -ENOBUFS: 1154b9d2bb2eSTrond Myklebust case -EPIPE: 115513331a55STrond Myklebust break; 115613331a55STrond Myklebust default: 115713331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 115813331a55STrond Myklebust -status); 1159a246b010SChuck Lever } 11605fe46e9dSBian Naimeng 1161a246b010SChuck Lever return status; 1162a246b010SChuck Lever } 1163a246b010SChuck Lever 11642a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11652a9e1cfaSTrond Myklebust { 11662a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 11672a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 11682a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 11692118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 11702a9e1cfaSTrond Myklebust } 11712a9e1cfaSTrond Myklebust 11722a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11732a9e1cfaSTrond Myklebust { 11742a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 11752a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 11762a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 11772118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 11782118071dSTrond Myklebust } 11792118071dSTrond Myklebust 118042d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 118142d42a5bSTrond Myklebust { 118242d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 118342d42a5bSTrond Myklebust 118442d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 11854f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 11864f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 11874f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 11882790a624STrond Myklebust clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 11893811172eSLiu Jian clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 11904f8943f8STrond Myklebust } 11914f8943f8STrond Myklebust 11924f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 11934f8943f8STrond Myklebust { 11944f8943f8STrond Myklebust set_bit(nr, &transport->sock_state); 11954f8943f8STrond Myklebust queue_work(xprtiod_workqueue, &transport->error_worker); 119642d42a5bSTrond Myklebust } 119742d42a5bSTrond Myklebust 1198b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1199b70ae915STrond Myklebust { 1200d896ba83STrond Myklebust xprt->connect_cookie++; 1201b70ae915STrond Myklebust smp_mb__before_atomic(); 1202b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1203b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 120442d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1205b70ae915STrond Myklebust smp_mb__after_atomic(); 1206b70ae915STrond Myklebust } 1207b70ae915STrond Myklebust 12082118071dSTrond Myklebust /** 12092118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 12102118071dSTrond Myklebust * @sk: socket 12112118071dSTrond Myklebust * 12122118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 12132118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 12142118071dSTrond Myklebust */ 12152118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 12162118071dSTrond Myklebust { 12174f8943f8STrond Myklebust struct sock_xprt *transport; 12182118071dSTrond Myklebust struct rpc_xprt *xprt; 12192118071dSTrond Myklebust 12202118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1221ea9afca8STrond Myklebust return; 12222118071dSTrond Myklebust 12234f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1224af84537dSBenjamin Coddington transport->xprt_err = -sk->sk_err; 1225af84537dSBenjamin Coddington if (transport->xprt_err == 0) 1226ea9afca8STrond Myklebust return; 12272118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 1228af84537dSBenjamin Coddington xprt, -transport->xprt_err); 1229af84537dSBenjamin Coddington trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); 1230af84537dSBenjamin Coddington 1231af84537dSBenjamin Coddington /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ 1232af84537dSBenjamin Coddington smp_mb__before_atomic(); 12334f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 12342a9e1cfaSTrond Myklebust } 12352a9e1cfaSTrond Myklebust 1236fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1237a246b010SChuck Lever { 1238ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1239ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12406cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1241a73881c9STrond Myklebust struct file *filp = transport->file; 1242a246b010SChuck Lever 1243fe315e76SChuck Lever if (sk == NULL) 1244fe315e76SChuck Lever return; 1245f0043206STrond Myklebust /* 1246f0043206STrond Myklebust * Make sure we're calling this in a context from which it is safe 1247f0043206STrond Myklebust * to call __fput_sync(). In practice that means rpciod and the 1248f0043206STrond Myklebust * system workqueue. 1249f0043206STrond Myklebust */ 1250f0043206STrond Myklebust if (!(current->flags & PF_WQ_WORKER)) { 1251f0043206STrond Myklebust WARN_ON_ONCE(1); 1252f0043206STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 1253f0043206STrond Myklebust return; 1254f0043206STrond Myklebust } 12559903cd1cSChuck Lever 1256264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1257264d1df3SJeff Layton sk_clear_memalloc(sk); 1258264d1df3SJeff Layton 125975eb6af7SChuck Lever tls_handshake_cancel(sk); 126075eb6af7SChuck Lever 126109939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 126209939204STrond Myklebust 1263edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1264ea9afca8STrond Myklebust lock_sock(sk); 1265ee0ac0c2SChuck Lever transport->inet = NULL; 1266ee0ac0c2SChuck Lever transport->sock = NULL; 1267a73881c9STrond Myklebust transport->file = NULL; 1268a246b010SChuck Lever 1269a246b010SChuck Lever sk->sk_user_data = NULL; 12702a9e1cfaSTrond Myklebust 12712a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12720c78789eSTrond Myklebust xprt_clear_connected(xprt); 12736cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1274ae053551STrond Myklebust /* Reset stream record info */ 1275ae053551STrond Myklebust xs_stream_reset_connect(transport); 1276ea9afca8STrond Myklebust release_sock(sk); 1277edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1278a246b010SChuck Lever 12796cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1280f0043206STrond Myklebust __fput_sync(filp); 12810445f92cSTrond Myklebust 12820445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1283fe315e76SChuck Lever } 1284fe315e76SChuck Lever 1285fe315e76SChuck Lever /** 1286fe315e76SChuck Lever * xs_close - close a socket 1287fe315e76SChuck Lever * @xprt: transport 1288fe315e76SChuck Lever * 1289fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1290fe315e76SChuck Lever * on the server we want to save. 1291f75e6745STrond Myklebust * 1292f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1293f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1294fe315e76SChuck Lever */ 1295fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1296fe315e76SChuck Lever { 1297fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1298fe315e76SChuck Lever 1299fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1300fe315e76SChuck Lever 13015dd5ad68SChuck Lever if (transport->sock) 13025dd5ad68SChuck Lever tls_handshake_close(transport->sock); 1303fe315e76SChuck Lever xs_reset_transport(transport); 130461d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1305a246b010SChuck Lever } 1306a246b010SChuck Lever 13074a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13084a068258SChuck Lever { 13094a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13104a068258SChuck Lever xprt); 13114a068258SChuck Lever xprt_disconnect_done(xprt); 13124a068258SChuck Lever } 13134a068258SChuck Lever 1314315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1315315f3812SKinglong Mee { 1316315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1317315f3812SKinglong Mee xprt_free(xprt); 1318315f3812SKinglong Mee } 1319315f3812SKinglong Mee 13209903cd1cSChuck Lever /** 13219903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13229903cd1cSChuck Lever * @xprt: doomed transport 13239903cd1cSChuck Lever * 13249903cd1cSChuck Lever */ 13259903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1326a246b010SChuck Lever { 132703c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 132803c78827STrond Myklebust struct sock_xprt, xprt); 13299903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13309903cd1cSChuck Lever 133103c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1332a1311d87STrond Myklebust xs_close(xprt); 1333edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1334b5e92419STrond Myklebust cancel_work_sync(&transport->error_worker); 1335315f3812SKinglong Mee xs_xprt_free(xprt); 1336a1311d87STrond Myklebust module_put(THIS_MODULE); 1337a246b010SChuck Lever } 1338a246b010SChuck Lever 13399903cd1cSChuck Lever /** 1340f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1341f9b2ee71STrond Myklebust * @xprt: transport 1342f9b2ee71STrond Myklebust * @sk: socket 1343f9b2ee71STrond Myklebust * @skb: skbuff 13449903cd1cSChuck Lever * 1345a246b010SChuck Lever */ 1346f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1347f9b2ee71STrond Myklebust struct sock *sk, 1348f9b2ee71STrond Myklebust struct sk_buff *skb) 1349a246b010SChuck Lever { 1350a246b010SChuck Lever struct rpc_task *task; 1351a246b010SChuck Lever struct rpc_rqst *rovr; 1352f9b2ee71STrond Myklebust int repsize, copied; 1353d8ed029dSAlexey Dobriyan u32 _xid; 1354d8ed029dSAlexey Dobriyan __be32 *xp; 1355a246b010SChuck Lever 13561da8c681SWillem de Bruijn repsize = skb->len; 1357a246b010SChuck Lever if (repsize < 4) { 13589903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1359f9b2ee71STrond Myklebust return; 1360a246b010SChuck Lever } 1361a246b010SChuck Lever 1362a246b010SChuck Lever /* Copy the XID from the skb... */ 13631da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1364a246b010SChuck Lever if (xp == NULL) 1365f9b2ee71STrond Myklebust return; 1366a246b010SChuck Lever 1367a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 136875c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1369a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1370a246b010SChuck Lever if (!rovr) 1371a246b010SChuck Lever goto out_unlock; 1372729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1373ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 137475c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1375a246b010SChuck Lever task = rovr->rq_task; 1376a246b010SChuck Lever 1377a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1378a246b010SChuck Lever copied = repsize; 1379a246b010SChuck Lever 1380a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 13811781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 138275c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13830afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1384729749bbSTrond Myklebust goto out_unpin; 13851781f7f5SHerbert Xu } 13861781f7f5SHerbert Xu 1387a246b010SChuck Lever 1388b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 13896a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1390b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 139175c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13921570c1e4SChuck Lever xprt_complete_rqst(task, copied); 13930afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1394729749bbSTrond Myklebust out_unpin: 1395729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1396a246b010SChuck Lever out_unlock: 139775c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1398f9b2ee71STrond Myklebust } 1399f9b2ee71STrond Myklebust 1400f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1401f9b2ee71STrond Myklebust { 1402f9b2ee71STrond Myklebust struct sk_buff *skb; 1403f9b2ee71STrond Myklebust struct sock *sk; 1404f9b2ee71STrond Myklebust int err; 1405f9b2ee71STrond Myklebust 1406f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1407f9b2ee71STrond Myklebust sk = transport->inet; 1408f9b2ee71STrond Myklebust if (sk == NULL) 1409f9b2ee71STrond Myklebust goto out; 1410f9b2ee71STrond Myklebust for (;;) { 1411ec095263SOliver Hartkopp skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 14124f546149STrond Myklebust if (skb == NULL) 14134f546149STrond Myklebust break; 1414f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1415850cbaddSPaolo Abeni consume_skb(skb); 14160af3442aSTrond Myklebust cond_resched(); 1417f9b2ee71STrond Myklebust } 14180ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1419a246b010SChuck Lever out: 1420f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1421f9b2ee71STrond Myklebust } 1422f9b2ee71STrond Myklebust 1423f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1424f9b2ee71STrond Myklebust { 1425f9b2ee71STrond Myklebust struct sock_xprt *transport = 1426f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1427a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1428a1231fdaSTrond Myklebust 1429f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1430a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1431f9b2ee71STrond Myklebust } 1432f9b2ee71STrond Myklebust 1433f9b2ee71STrond Myklebust /** 1434f67939e4SChuck Lever * xs_data_ready - "data ready" callback for sockets 1435f9b2ee71STrond Myklebust * @sk: socket with data to read 1436f9b2ee71STrond Myklebust * 1437f9b2ee71STrond Myklebust */ 1438f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1439f9b2ee71STrond Myklebust { 1440f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1441f9b2ee71STrond Myklebust 144240e0b090SPeilin Ye trace_sk_data_ready(sk); 144340e0b090SPeilin Ye 1444f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1445f9b2ee71STrond Myklebust if (xprt != NULL) { 1446f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1447f9b2ee71STrond Myklebust struct sock_xprt, xprt); 1448f67939e4SChuck Lever 1449f67939e4SChuck Lever trace_xs_data_ready(xprt); 1450f67939e4SChuck Lever 14515157b956STrond Myklebust transport->old_data_ready(sk); 14520d3ca07fSChuck Lever 14530d3ca07fSChuck Lever if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 14540d3ca07fSChuck Lever return; 14550d3ca07fSChuck Lever 14565157b956STrond Myklebust /* Any data means we had a useful conversation, so 14575157b956STrond Myklebust * then we don't need to delay the next reconnect 14585157b956STrond Myklebust */ 14595157b956STrond Myklebust if (xprt->reestablish_timeout) 14605157b956STrond Myklebust xprt->reestablish_timeout = 0; 146142d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 146240a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1463f9b2ee71STrond Myklebust } 1464a246b010SChuck Lever } 1465a246b010SChuck Lever 1466a519fc7aSTrond Myklebust /* 1467a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1468a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1469a519fc7aSTrond Myklebust */ 1470a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1471a519fc7aSTrond Myklebust { 1472a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1473a519fc7aSTrond Myklebust } 1474a519fc7aSTrond Myklebust 14759e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14766b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14776b26cc8cSChuck Lever { 14786b26cc8cSChuck Lever return PAGE_SIZE; 14796b26cc8cSChuck Lever } 14809e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 148144b98efdSRicardo Labiaga 14829903cd1cSChuck Lever /** 1483efce2d0bSTrond Myklebust * xs_local_state_change - callback to handle AF_LOCAL socket state changes 1484efce2d0bSTrond Myklebust * @sk: socket whose state has changed 1485efce2d0bSTrond Myklebust * 1486efce2d0bSTrond Myklebust */ 1487efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk) 1488efce2d0bSTrond Myklebust { 1489efce2d0bSTrond Myklebust struct rpc_xprt *xprt; 1490efce2d0bSTrond Myklebust struct sock_xprt *transport; 1491efce2d0bSTrond Myklebust 1492efce2d0bSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1493efce2d0bSTrond Myklebust return; 1494efce2d0bSTrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1495efce2d0bSTrond Myklebust if (sk->sk_shutdown & SHUTDOWN_MASK) { 1496efce2d0bSTrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1497efce2d0bSTrond Myklebust /* Trigger the socket release */ 1498efce2d0bSTrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1499efce2d0bSTrond Myklebust } 1500efce2d0bSTrond Myklebust } 1501efce2d0bSTrond Myklebust 1502efce2d0bSTrond Myklebust /** 15039903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 15049903cd1cSChuck Lever * @sk: socket whose state has changed 15059903cd1cSChuck Lever * 15069903cd1cSChuck Lever */ 15079903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1508a246b010SChuck Lever { 1509a246b010SChuck Lever struct rpc_xprt *xprt; 15100fdea1e8STrond Myklebust struct sock_xprt *transport; 1511a246b010SChuck Lever 1512a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1513ea9afca8STrond Myklebust return; 15149903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1515669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1516a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1517a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1518669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1519669502ffSAndy Chittenden sk->sk_shutdown); 1520a246b010SChuck Lever 15210fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 152240b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1523a246b010SChuck Lever switch (sk->sk_state) { 1524a246b010SChuck Lever case TCP_ESTABLISHED: 1525a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15268b71798cSTrond Myklebust xprt->connect_cookie++; 15270fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15280fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 152951971139SChuck Lever 15303968a8a5SChuck Lever xprt->stat.connect_count++; 15313968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15323968a8a5SChuck Lever xprt->stat.connect_start; 15334f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1534a246b010SChuck Lever } 1535a246b010SChuck Lever break; 15363b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15373b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15387c1d71cfSTrond Myklebust xprt->connect_cookie++; 1539663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15403b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15414e857c58SPeter Zijlstra smp_mb__before_atomic(); 15423b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1543ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15444e857c58SPeter Zijlstra smp_mb__after_atomic(); 1545a246b010SChuck Lever break; 1546632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15473b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15487c1d71cfSTrond Myklebust xprt->connect_cookie++; 1549d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15504f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1551df561f66SGustavo A. R. Silva fallthrough; 1552663b8858STrond Myklebust case TCP_CLOSING: 1553663b8858STrond Myklebust /* 1554663b8858STrond Myklebust * If the server closed down the connection, make sure that 1555663b8858STrond Myklebust * we back off before reconnecting 1556663b8858STrond Myklebust */ 1557663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1558663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15593b948ae5STrond Myklebust break; 15603b948ae5STrond Myklebust case TCP_LAST_ACK: 1561670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15624e857c58SPeter Zijlstra smp_mb__before_atomic(); 15633b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15644e857c58SPeter Zijlstra smp_mb__after_atomic(); 15653b948ae5STrond Myklebust break; 15663b948ae5STrond Myklebust case TCP_CLOSE: 15670fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15680fdea1e8STrond Myklebust &transport->sock_state)) 15690fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15709b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15719b30889cSTrond Myklebust /* Trigger the socket release */ 15724f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1573a246b010SChuck Lever } 1574a246b010SChuck Lever } 1575a246b010SChuck Lever 15761f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15771f0fa154SIlpo Järvinen { 15784f8943f8STrond Myklebust struct sock_xprt *transport; 15791f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15801f0fa154SIlpo Järvinen 158113331a55STrond Myklebust if (!sk->sk_socket) 15821f0fa154SIlpo Järvinen return; 158313331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15841f0fa154SIlpo Järvinen 15851f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15861f0fa154SIlpo Järvinen return; 15874f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 15882790a624STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state)) 15892790a624STrond Myklebust return; 15904f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1591c544577dSTrond Myklebust sk->sk_write_pending--; 15921f0fa154SIlpo Järvinen } 15931f0fa154SIlpo Järvinen 15942a9e1cfaSTrond Myklebust /** 1595c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1596c7b2cae8SChuck Lever * becomes available 15979903cd1cSChuck Lever * @sk: socket whose state has changed 15989903cd1cSChuck Lever * 1599a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1600a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1601c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1602a246b010SChuck Lever * with a bunch of small requests. 1603a246b010SChuck Lever */ 1604c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1605a246b010SChuck Lever { 1606c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16071f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16081f0fa154SIlpo Järvinen xs_write_space(sk); 1609c7b2cae8SChuck Lever } 1610c7b2cae8SChuck Lever 1611c7b2cae8SChuck Lever /** 1612c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1613c7b2cae8SChuck Lever * becomes available 1614c7b2cae8SChuck Lever * @sk: socket whose state has changed 1615c7b2cae8SChuck Lever * 1616c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1617c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1618c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1619c7b2cae8SChuck Lever * with a bunch of small requests. 1620c7b2cae8SChuck Lever */ 1621c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1622c7b2cae8SChuck Lever { 1623c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 162464dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16251f0fa154SIlpo Järvinen xs_write_space(sk); 1626a246b010SChuck Lever } 1627a246b010SChuck Lever 1628470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1629a246b010SChuck Lever { 1630ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1631ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1632a246b010SChuck Lever 16337c6e066eSChuck Lever if (transport->rcvsize) { 1634a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16357c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1636a246b010SChuck Lever } 16377c6e066eSChuck Lever if (transport->sndsize) { 1638a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16397c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1640a246b010SChuck Lever sk->sk_write_space(sk); 1641a246b010SChuck Lever } 1642a246b010SChuck Lever } 1643a246b010SChuck Lever 164443118c29SChuck Lever /** 1645470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 164643118c29SChuck Lever * @xprt: generic transport 1647470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1648470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 164943118c29SChuck Lever * 1650470056c2SChuck Lever * Set socket send and receive buffer size limits. 165143118c29SChuck Lever */ 1652470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 165343118c29SChuck Lever { 16547c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16557c6e066eSChuck Lever 16567c6e066eSChuck Lever transport->sndsize = 0; 1657470056c2SChuck Lever if (sndsize) 16587c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16597c6e066eSChuck Lever transport->rcvsize = 0; 1660470056c2SChuck Lever if (rcvsize) 16617c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1662470056c2SChuck Lever 1663470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 166443118c29SChuck Lever } 166543118c29SChuck Lever 166646c0ee8bSChuck Lever /** 166746c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1668acf0a39fSChuck Lever * @xprt: controlling transport 166946c0ee8bSChuck Lever * @task: task that timed out 167046c0ee8bSChuck Lever * 167146c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 167246c0ee8bSChuck Lever */ 16736a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 167446c0ee8bSChuck Lever { 1675b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 16766a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1677b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 167846c0ee8bSChuck Lever } 167946c0ee8bSChuck Lever 1680826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1681b85d8806SChuck Lever { 1682826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1683826799e6SJ. Bruce Fields unsigned short range; 1684826799e6SJ. Bruce Fields unsigned short rand; 1685826799e6SJ. Bruce Fields 1686826799e6SJ. Bruce Fields if (max < min) 1687826799e6SJ. Bruce Fields return -EADDRINUSE; 1688826799e6SJ. Bruce Fields range = max - min + 1; 16898032bf12SJason A. Donenfeld rand = get_random_u32_below(range); 1690826799e6SJ. Bruce Fields return rand + min; 1691b85d8806SChuck Lever } 1692b85d8806SChuck Lever 16934dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 16944dda9c8aSTrond Myklebust { 16954dda9c8aSTrond Myklebust struct sockaddr_storage buf; 16964dda9c8aSTrond Myklebust unsigned short port = 0; 16974dda9c8aSTrond Myklebust 16989b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 16994dda9c8aSTrond Myklebust goto out; 17004dda9c8aSTrond Myklebust switch (buf.ss_family) { 17014dda9c8aSTrond Myklebust case AF_INET6: 17024dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17034dda9c8aSTrond Myklebust break; 17044dda9c8aSTrond Myklebust case AF_INET: 17054dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17064dda9c8aSTrond Myklebust } 17074dda9c8aSTrond Myklebust out: 17084dda9c8aSTrond Myklebust return port; 17094dda9c8aSTrond Myklebust } 17104dda9c8aSTrond Myklebust 17114dda9c8aSTrond Myklebust /** 171292200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 171392200412SChuck Lever * @xprt: generic transport 171492200412SChuck Lever * @port: new port number 171592200412SChuck Lever * 171692200412SChuck Lever */ 171792200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 171892200412SChuck Lever { 171992200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1720c4efcb1dSChuck Lever 17219dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17229dc3b095SChuck Lever xs_update_peer_port(xprt); 172392200412SChuck Lever } 172492200412SChuck Lever 17254dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17264dda9c8aSTrond Myklebust { 1727e6237b6fSTrond Myklebust if (transport->srcport == 0 && transport->xprt.reuseport) 17284dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17294dda9c8aSTrond Myklebust } 17304dda9c8aSTrond Myklebust 1731826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 173267a391d7STrond Myklebust { 1733826799e6SJ. Bruce Fields int port = transport->srcport; 173467a391d7STrond Myklebust 173567a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 173667a391d7STrond Myklebust port = xs_get_random_port(); 173767a391d7STrond Myklebust return port; 173867a391d7STrond Myklebust } 173967a391d7STrond Myklebust 1740421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt) 1741a8482488SOlga Kornievskaia { 1742a8482488SOlga Kornievskaia struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1743b49ea673SNeilBrown unsigned short ret = 0; 1744b49ea673SNeilBrown mutex_lock(&sock->recv_mutex); 1745b49ea673SNeilBrown if (sock->sock) 1746b49ea673SNeilBrown ret = xs_sock_getport(sock->sock); 1747b49ea673SNeilBrown mutex_unlock(&sock->recv_mutex); 1748b49ea673SNeilBrown return ret; 1749a8482488SOlga Kornievskaia } 1750421ab1beSTrond Myklebust 1751421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen) 1752421ab1beSTrond Myklebust { 1753421ab1beSTrond Myklebust struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1754421ab1beSTrond Myklebust union { 1755421ab1beSTrond Myklebust struct sockaddr sa; 1756421ab1beSTrond Myklebust struct sockaddr_storage st; 1757421ab1beSTrond Myklebust } saddr; 1758421ab1beSTrond Myklebust int ret = -ENOTCONN; 1759421ab1beSTrond Myklebust 1760421ab1beSTrond Myklebust mutex_lock(&sock->recv_mutex); 1761421ab1beSTrond Myklebust if (sock->sock) { 1762421ab1beSTrond Myklebust ret = kernel_getsockname(sock->sock, &saddr.sa); 1763421ab1beSTrond Myklebust if (ret >= 0) 1764421ab1beSTrond Myklebust ret = snprintf(buf, buflen, "%pISc", &saddr.sa); 1765421ab1beSTrond Myklebust } 1766421ab1beSTrond Myklebust mutex_unlock(&sock->recv_mutex); 1767421ab1beSTrond Myklebust return ret; 1768421ab1beSTrond Myklebust } 1769a8482488SOlga Kornievskaia 1770baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 177167a391d7STrond Myklebust { 1772fbfffbd5SChuck Lever if (transport->srcport != 0) 1773fbfffbd5SChuck Lever transport->srcport = 0; 177467a391d7STrond Myklebust if (!transport->xprt.resvport) 177567a391d7STrond Myklebust return 0; 177667a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 177767a391d7STrond Myklebust return xprt_max_resvport; 177867a391d7STrond Myklebust return --port; 177967a391d7STrond Myklebust } 1780beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1781a246b010SChuck Lever { 1782beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 178367a391d7STrond Myklebust int err, nloop = 0; 1784826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 178567a391d7STrond Myklebust unsigned short last; 1786a246b010SChuck Lever 17870f7a622cSChris Perl /* 17880f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17890f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17900f7a622cSChris Perl * port selection happen implicitly when the socket is used 17910f7a622cSChris Perl * (for example at connect time). 17920f7a622cSChris Perl * 17930f7a622cSChris Perl * This ensures that we can continue to establish TCP 17940f7a622cSChris Perl * connections even when all local ephemeral ports are already 17950f7a622cSChris Perl * a part of some TCP connection. This makes no difference 179612b20ce3SBhaskar Chowdhury * for UDP sockets, but also doesn't harm them. 17970f7a622cSChris Perl * 17980f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17990f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 18000f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 18010f7a622cSChris Perl */ 1802826799e6SJ. Bruce Fields if (port <= 0) 1803826799e6SJ. Bruce Fields return port; 18040f7a622cSChris Perl 1805beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1806a246b010SChuck Lever do { 1807beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1808e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1809beb59b68SPavel Emelyanov transport->xprt.addrlen); 1810a246b010SChuck Lever if (err == 0) { 1811bc1c56e9SNeilBrown if (transport->xprt.reuseport) 1812fbfffbd5SChuck Lever transport->srcport = port; 1813d3bc9a1dSFrank van Maarseveen break; 1814a246b010SChuck Lever } 181567a391d7STrond Myklebust last = port; 1816baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 181767a391d7STrond Myklebust if (port > last) 181867a391d7STrond Myklebust nloop++; 181967a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1820beb59b68SPavel Emelyanov 18214232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1822beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1823beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1824beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1825beb59b68SPavel Emelyanov else 1826beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1827beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18287dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1829a246b010SChuck Lever return err; 1830a246b010SChuck Lever } 1831a246b010SChuck Lever 1832176e21eeSChuck Lever /* 1833176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1834176e21eeSChuck Lever */ 1835176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1836176e21eeSChuck Lever { 1837fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1838176e21eeSChuck Lever } 1839176e21eeSChuck Lever 1840176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1841176e21eeSChuck Lever { 1842176e21eeSChuck Lever } 1843a246b010SChuck Lever 1844ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1845064a9177SNeilBrown static struct lock_class_key xs_key[3]; 1846064a9177SNeilBrown static struct lock_class_key xs_slock_key[3]; 1847ed07536eSPeter Zijlstra 1848176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1849176e21eeSChuck Lever { 1850176e21eeSChuck Lever struct sock *sk = sock->sk; 1851176e21eeSChuck Lever 1852176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1853064a9177SNeilBrown &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]); 1854176e21eeSChuck Lever } 1855176e21eeSChuck Lever 18568945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1857ed07536eSPeter Zijlstra { 1858ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18598945ee5eSChuck Lever 18608945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 1861064a9177SNeilBrown &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]); 1862ed07536eSPeter Zijlstra } 18638945ee5eSChuck Lever 18648945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18658945ee5eSChuck Lever { 18668945ee5eSChuck Lever struct sock *sk = sock->sk; 18678945ee5eSChuck Lever 18688945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 1869064a9177SNeilBrown &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]); 1870ed07536eSPeter Zijlstra } 18716bc9638aSPavel Emelyanov 18726bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18736bc9638aSPavel Emelyanov { 1874fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18751b7a1819SWeston Andros Adamson return; 18761b7a1819SWeston Andros Adamson 18774232e863SChuck Lever switch (family) { 1878176e21eeSChuck Lever case AF_LOCAL: 1879176e21eeSChuck Lever xs_reclassify_socketu(sock); 1880176e21eeSChuck Lever break; 18814232e863SChuck Lever case AF_INET: 18826bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18834232e863SChuck Lever break; 18844232e863SChuck Lever case AF_INET6: 18856bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18864232e863SChuck Lever break; 18874232e863SChuck Lever } 18886bc9638aSPavel Emelyanov } 1889ed07536eSPeter Zijlstra #else 18906bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18916bc9638aSPavel Emelyanov { 18926bc9638aSPavel Emelyanov } 1893ed07536eSPeter Zijlstra #endif 1894ed07536eSPeter Zijlstra 189593dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 189693dc41bdSNeilBrown { 189793dc41bdSNeilBrown } 189893dc41bdSNeilBrown 18996bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 19004dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 19014dda9c8aSTrond Myklebust int protocol, bool reuseport) 190222f79326SPavel Emelyanov { 1903a73881c9STrond Myklebust struct file *filp; 190422f79326SPavel Emelyanov struct socket *sock; 190522f79326SPavel Emelyanov int err; 190622f79326SPavel Emelyanov 19076bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 190822f79326SPavel Emelyanov if (err < 0) { 190922f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 191022f79326SPavel Emelyanov protocol, -err); 191122f79326SPavel Emelyanov goto out; 191222f79326SPavel Emelyanov } 19136bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 191422f79326SPavel Emelyanov 19154dda9c8aSTrond Myklebust if (reuseport) 1916fe31a326SChristoph Hellwig sock_set_reuseport(sock->sk); 19174dda9c8aSTrond Myklebust 19184cea288aSBen Hutchings err = xs_bind(transport, sock); 19194cea288aSBen Hutchings if (err) { 192022f79326SPavel Emelyanov sock_release(sock); 192122f79326SPavel Emelyanov goto out; 192222f79326SPavel Emelyanov } 192322f79326SPavel Emelyanov 1924*0ca87e50SLiu Jian if (protocol == IPPROTO_TCP) { 1925*0ca87e50SLiu Jian __netns_tracker_free(xprt->xprt_net, &sock->sk->ns_tracker, false); 1926*0ca87e50SLiu Jian sock->sk->sk_net_refcnt = 1; 1927*0ca87e50SLiu Jian get_net_track(xprt->xprt_net, &sock->sk->ns_tracker, GFP_KERNEL); 1928*0ca87e50SLiu Jian sock_inuse_add(xprt->xprt_net, 1); 1929*0ca87e50SLiu Jian } 1930*0ca87e50SLiu Jian 1931a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1932a73881c9STrond Myklebust if (IS_ERR(filp)) 1933a73881c9STrond Myklebust return ERR_CAST(filp); 1934a73881c9STrond Myklebust transport->file = filp; 1935a73881c9STrond Myklebust 193622f79326SPavel Emelyanov return sock; 193722f79326SPavel Emelyanov out: 193822f79326SPavel Emelyanov return ERR_PTR(err); 193922f79326SPavel Emelyanov } 194022f79326SPavel Emelyanov 1941176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1942176e21eeSChuck Lever struct socket *sock) 1943176e21eeSChuck Lever { 1944176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1945176e21eeSChuck Lever xprt); 1946176e21eeSChuck Lever 1947176e21eeSChuck Lever if (!transport->inet) { 1948176e21eeSChuck Lever struct sock *sk = sock->sk; 1949176e21eeSChuck Lever 1950ea9afca8STrond Myklebust lock_sock(sk); 1951176e21eeSChuck Lever 1952176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1953176e21eeSChuck Lever 1954176e21eeSChuck Lever sk->sk_user_data = xprt; 1955a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1956176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1957efce2d0bSTrond Myklebust sk->sk_state_change = xs_local_state_change; 19582118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 195998123866SBenjamin Coddington sk->sk_use_task_frag = false; 1960176e21eeSChuck Lever 1961176e21eeSChuck Lever xprt_clear_connected(xprt); 1962176e21eeSChuck Lever 1963176e21eeSChuck Lever /* Reset to new socket */ 1964176e21eeSChuck Lever transport->sock = sock; 1965176e21eeSChuck Lever transport->inet = sk; 1966176e21eeSChuck Lever 1967ea9afca8STrond Myklebust release_sock(sk); 1968176e21eeSChuck Lever } 1969176e21eeSChuck Lever 1970ae053551STrond Myklebust xs_stream_start_connect(transport); 19716c7a64e5STrond Myklebust 1972176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1973176e21eeSChuck Lever } 1974176e21eeSChuck Lever 1975176e21eeSChuck Lever /** 1976176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1977176e21eeSChuck Lever * @transport: socket transport to connect 1978176e21eeSChuck Lever */ 1979dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1980176e21eeSChuck Lever { 1981176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1982a73881c9STrond Myklebust struct file *filp; 1983176e21eeSChuck Lever struct socket *sock; 198468e9a246SColin Ian King int status; 1985176e21eeSChuck Lever 1986176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1987176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1988176e21eeSChuck Lever if (status < 0) { 1989176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1990176e21eeSChuck Lever "transport socket (%d).\n", -status); 1991176e21eeSChuck Lever goto out; 1992176e21eeSChuck Lever } 1993d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1994176e21eeSChuck Lever 1995a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1996a73881c9STrond Myklebust if (IS_ERR(filp)) { 1997a73881c9STrond Myklebust status = PTR_ERR(filp); 1998a73881c9STrond Myklebust goto out; 1999a73881c9STrond Myklebust } 2000a73881c9STrond Myklebust transport->file = filp; 2001a73881c9STrond Myklebust 2002176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 2003176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2004176e21eeSChuck Lever 2005176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 200640b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2007176e21eeSChuck Lever switch (status) { 2008176e21eeSChuck Lever case 0: 2009176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 2010176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20113968a8a5SChuck Lever xprt->stat.connect_count++; 20123968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 20133968a8a5SChuck Lever xprt->stat.connect_start; 2014176e21eeSChuck Lever xprt_set_connected(xprt); 201593f479d3SGustavo A. R. Silva break; 20163601c4a9STrond Myklebust case -ENOBUFS: 2017176e21eeSChuck Lever break; 2018176e21eeSChuck Lever case -ENOENT: 2019176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2020176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2021176e21eeSChuck Lever break; 20224a20a988STrond Myklebust case -ECONNREFUSED: 20234a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20244a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20254a20a988STrond Myklebust break; 2026176e21eeSChuck Lever default: 2027176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2028176e21eeSChuck Lever __func__, -status, 2029176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2030176e21eeSChuck Lever } 2031176e21eeSChuck Lever 2032176e21eeSChuck Lever out: 2033176e21eeSChuck Lever xprt_clear_connecting(xprt); 2034176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2035dc107402SJ. Bruce Fields return status; 2036dc107402SJ. Bruce Fields } 2037dc107402SJ. Bruce Fields 2038b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2039dc107402SJ. Bruce Fields { 2040dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2041dc107402SJ. Bruce Fields int ret; 2042dc107402SJ. Bruce Fields 2043aad41a7dSTrond Myklebust if (transport->file) 2044aad41a7dSTrond Myklebust goto force_disconnect; 2045aad41a7dSTrond Myklebust 2046dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2047dc107402SJ. Bruce Fields /* 2048dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2049dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2050dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2051dc107402SJ. Bruce Fields * 2052dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2053dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2054dc107402SJ. Bruce Fields * connect. 2055dc107402SJ. Bruce Fields */ 205639494194STrond Myklebust rpc_task_set_rpc_status(task, -ENOTCONN); 2057aad41a7dSTrond Myklebust goto out_wake; 2058dc107402SJ. Bruce Fields } 2059dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2060dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2061dc107402SJ. Bruce Fields msleep_interruptible(15000); 2062aad41a7dSTrond Myklebust return; 2063aad41a7dSTrond Myklebust force_disconnect: 2064aad41a7dSTrond Myklebust xprt_force_disconnect(xprt); 2065aad41a7dSTrond Myklebust out_wake: 2066aad41a7dSTrond Myklebust xprt_clear_connecting(xprt); 2067aad41a7dSTrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 2068176e21eeSChuck Lever } 2069176e21eeSChuck Lever 20703c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2071d6e971d8SJeff Layton /* 2072693486d5SNeilBrown * Note that this should be called with XPRT_LOCKED held, or recv_mutex 2073693486d5SNeilBrown * held, or when we otherwise know that we have exclusive access to the 2074693486d5SNeilBrown * socket, to guard against races with xs_reset_transport. 2075d6e971d8SJeff Layton */ 2076a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2077a564b8f0SMel Gorman { 2078a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2079a564b8f0SMel Gorman xprt); 2080a564b8f0SMel Gorman 2081d6e971d8SJeff Layton /* 2082d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2083d6e971d8SJeff Layton * reconnecting process will get it for us. 2084d6e971d8SJeff Layton */ 2085d6e971d8SJeff Layton if (!transport->inet) 2086d6e971d8SJeff Layton return; 20878e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2088a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2089a564b8f0SMel Gorman } 2090a564b8f0SMel Gorman 2091a564b8f0SMel Gorman /** 2092d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2093a564b8f0SMel Gorman * @xprt: transport to tag 2094a564b8f0SMel Gorman * 20958e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20968e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2097a564b8f0SMel Gorman */ 2098d67fa4d8SJeff Layton static int 2099d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2100a564b8f0SMel Gorman { 2101d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2102a564b8f0SMel Gorman 2103693486d5SNeilBrown mutex_lock(&xs->recv_mutex); 2104693486d5SNeilBrown if (atomic_inc_return(&xprt->swapper) == 1 && 2105693486d5SNeilBrown xs->inet) 2106d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2107693486d5SNeilBrown mutex_unlock(&xs->recv_mutex); 21088e228133SJeff Layton return 0; 2109a564b8f0SMel Gorman } 2110a564b8f0SMel Gorman 21118e228133SJeff Layton /** 2112d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 21138e228133SJeff Layton * @xprt: transport to tag 21148e228133SJeff Layton * 21158e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 21168e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 21178e228133SJeff Layton */ 2118d67fa4d8SJeff Layton static void 2119d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21208e228133SJeff Layton { 2121d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21228e228133SJeff Layton 2123693486d5SNeilBrown mutex_lock(&xs->recv_mutex); 2124693486d5SNeilBrown if (atomic_dec_and_test(&xprt->swapper) && 2125693486d5SNeilBrown xs->inet) 2126d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2127693486d5SNeilBrown mutex_unlock(&xs->recv_mutex); 2128a564b8f0SMel Gorman } 2129a564b8f0SMel Gorman #else 2130a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2131a564b8f0SMel Gorman { 2132a564b8f0SMel Gorman } 2133d67fa4d8SJeff Layton 2134d67fa4d8SJeff Layton static int 2135d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2136d67fa4d8SJeff Layton { 2137d67fa4d8SJeff Layton return -EINVAL; 2138d67fa4d8SJeff Layton } 2139d67fa4d8SJeff Layton 2140d67fa4d8SJeff Layton static void 2141d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2142d67fa4d8SJeff Layton { 2143d67fa4d8SJeff Layton } 2144a564b8f0SMel Gorman #endif 2145a564b8f0SMel Gorman 214616be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2147a246b010SChuck Lever { 214816be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2149edb267a6SChuck Lever 2150ee0ac0c2SChuck Lever if (!transport->inet) { 2151b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2152b0d93ad5SChuck Lever 2153ea9afca8STrond Myklebust lock_sock(sk); 2154b0d93ad5SChuck Lever 21552a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21562a9e1cfaSTrond Myklebust 2157b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2158f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2159b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 216098123866SBenjamin Coddington sk->sk_use_task_frag = false; 2161b0d93ad5SChuck Lever 2162b0d93ad5SChuck Lever xprt_set_connected(xprt); 2163b0d93ad5SChuck Lever 2164b0d93ad5SChuck Lever /* Reset to new socket */ 2165ee0ac0c2SChuck Lever transport->sock = sock; 2166ee0ac0c2SChuck Lever transport->inet = sk; 2167b0d93ad5SChuck Lever 2168a564b8f0SMel Gorman xs_set_memalloc(xprt); 2169a564b8f0SMel Gorman 2170ea9afca8STrond Myklebust release_sock(sk); 2171b0d93ad5SChuck Lever } 2172470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 217302910177STrond Myklebust 217402910177STrond Myklebust xprt->stat.connect_start = jiffies; 217516be2d20SChuck Lever } 217616be2d20SChuck Lever 21778c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2178a246b010SChuck Lever { 2179a246b010SChuck Lever struct sock_xprt *transport = 2180a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2181a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2182d099b8afSColin Ian King struct socket *sock; 2183b65c0310SPavel Emelyanov int status = -EIO; 21848db55a03SNeilBrown unsigned int pflags = current->flags; 2185a246b010SChuck Lever 21868db55a03SNeilBrown if (atomic_read(&xprt->swapper)) 21878db55a03SNeilBrown current->flags |= PF_MEMALLOC; 21888c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21894dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21904dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2191b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2192a246b010SChuck Lever goto out; 219368e220bdSChuck Lever 2194c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2195c740eff8SChuck Lever "%s (port %s)\n", xprt, 2196c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2197c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2198c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 219968e220bdSChuck Lever 220068e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 220140b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2202a246b010SChuck Lever status = 0; 2203b0d93ad5SChuck Lever out: 2204b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2205cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 22067d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 22078db55a03SNeilBrown current_restore_flags(pflags, PF_MEMALLOC); 2208b0d93ad5SChuck Lever } 2209b0d93ad5SChuck Lever 22104876cc77STrond Myklebust /** 22114876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 22124876cc77STrond Myklebust * @xprt: transport 22134876cc77STrond Myklebust * 22144876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 22154876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 22164876cc77STrond Myklebust */ 22174876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 22184876cc77STrond Myklebust { 22194876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22204876cc77STrond Myklebust struct socket *sock = transport->sock; 22219b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22224876cc77STrond Myklebust 22234876cc77STrond Myklebust if (sock == NULL) 22244876cc77STrond Myklebust return; 22250a6ff58eSTrond Myklebust if (!xprt->reuseport) { 22260a6ff58eSTrond Myklebust xs_close(xprt); 22270a6ff58eSTrond Myklebust return; 22280a6ff58eSTrond Myklebust } 22299b30889cSTrond Myklebust switch (skst) { 22307c81e6a9STrond Myklebust case TCP_FIN_WAIT1: 22317c81e6a9STrond Myklebust case TCP_FIN_WAIT2: 2232943d045aSSiddharth Kawar case TCP_LAST_ACK: 22337c81e6a9STrond Myklebust break; 22347c81e6a9STrond Myklebust case TCP_ESTABLISHED: 22357c81e6a9STrond Myklebust case TCP_CLOSE_WAIT: 22364876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22374876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22389b30889cSTrond Myklebust break; 22397c81e6a9STrond Myklebust default: 22404876cc77STrond Myklebust xs_reset_transport(transport); 22414876cc77STrond Myklebust } 22429b30889cSTrond Myklebust } 22434876cc77STrond Myklebust 22448d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22458d1b8c62STrond Myklebust struct socket *sock) 2246b0d93ad5SChuck Lever { 224716be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22483a107f07STrond Myklebust struct net *net = sock_net(sock->sk); 22493a107f07STrond Myklebust unsigned long connect_timeout; 22503a107f07STrond Myklebust unsigned long syn_retries; 22517196dbb0STrond Myklebust unsigned int keepidle; 22527196dbb0STrond Myklebust unsigned int keepcnt; 2253775f06abSTrond Myklebust unsigned int timeo; 22543a107f07STrond Myklebust unsigned long t; 22557f260e85STrond Myklebust 2256b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22577196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22587196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22597196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22607196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22617196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2262b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22637f260e85STrond Myklebust 22647f260e85STrond Myklebust /* TCP Keepalive options */ 2265ce3d9544SChristoph Hellwig sock_set_keepalive(sock->sk); 226671c48eb8SChristoph Hellwig tcp_sock_set_keepidle(sock->sk, keepidle); 2267d41ecaacSChristoph Hellwig tcp_sock_set_keepintvl(sock->sk, keepidle); 2268480aeb96SChristoph Hellwig tcp_sock_set_keepcnt(sock->sk, keepcnt); 2269b0d93ad5SChuck Lever 22708d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 2271c488aeadSChristoph Hellwig tcp_sock_set_user_timeout(sock->sk, timeo); 22723a107f07STrond Myklebust 22733a107f07STrond Myklebust /* Connect timeout */ 22743a107f07STrond Myklebust connect_timeout = max_t(unsigned long, 22753a107f07STrond Myklebust DIV_ROUND_UP(xprt->connect_timeout, HZ), 1); 22763a107f07STrond Myklebust syn_retries = max_t(unsigned long, 22773a107f07STrond Myklebust READ_ONCE(net->ipv4.sysctl_tcp_syn_retries), 1); 22783a107f07STrond Myklebust for (t = 0; t <= syn_retries && (1UL << t) < connect_timeout; t++) 22793a107f07STrond Myklebust ; 22803a107f07STrond Myklebust if (t <= syn_retries) 22813a107f07STrond Myklebust tcp_sock_set_syncnt(sock->sk, t - 1); 22828d1b8c62STrond Myklebust } 22838d1b8c62STrond Myklebust 22843e6ff89dSTrond Myklebust static void xs_tcp_do_set_connect_timeout(struct rpc_xprt *xprt, 22853e6ff89dSTrond Myklebust unsigned long connect_timeout) 22863e6ff89dSTrond Myklebust { 22873e6ff89dSTrond Myklebust struct sock_xprt *transport = 22883e6ff89dSTrond Myklebust container_of(xprt, struct sock_xprt, xprt); 22893e6ff89dSTrond Myklebust struct rpc_timeout to; 22903e6ff89dSTrond Myklebust unsigned long initval; 22913e6ff89dSTrond Myklebust 22923e6ff89dSTrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22933e6ff89dSTrond Myklebust /* Arbitrary lower limit */ 22943e6ff89dSTrond Myklebust initval = max_t(unsigned long, connect_timeout, XS_TCP_INIT_REEST_TO); 22953e6ff89dSTrond Myklebust to.to_initval = initval; 22963e6ff89dSTrond Myklebust to.to_maxval = initval; 22973e6ff89dSTrond Myklebust to.to_retries = 0; 22983e6ff89dSTrond Myklebust memcpy(&transport->tcp_timeout, &to, sizeof(transport->tcp_timeout)); 22993e6ff89dSTrond Myklebust xprt->timeout = &transport->tcp_timeout; 23003e6ff89dSTrond Myklebust xprt->connect_timeout = connect_timeout; 23018d1b8c62STrond Myklebust } 23028d1b8c62STrond Myklebust 23037196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 23047196dbb0STrond Myklebust unsigned long connect_timeout, 23057196dbb0STrond Myklebust unsigned long reconnect_timeout) 23067196dbb0STrond Myklebust { 23077196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23087196dbb0STrond Myklebust 2309b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 23107196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 23117196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 23123e6ff89dSTrond Myklebust if (connect_timeout < xprt->connect_timeout) 23133e6ff89dSTrond Myklebust xs_tcp_do_set_connect_timeout(xprt, connect_timeout); 23147196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2315b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 23167196dbb0STrond Myklebust } 23177196dbb0STrond Myklebust 23188d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 23198d1b8c62STrond Myklebust { 23208d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23218d1b8c62STrond Myklebust 23228d1b8c62STrond Myklebust if (!transport->inet) { 23238d1b8c62STrond Myklebust struct sock *sk = sock->sk; 23248d1b8c62STrond Myklebust 2325d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2326d88e4d82SNeilBrown * connections such as NFS mounts. 2327d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2328d88e4d82SNeilBrown * Individual applications, which have specific 2329d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2330d88e4d82SNeilBrown * MAY override this as appropriate. 2331d88e4d82SNeilBrown */ 233218d5ad62SChristoph Hellwig if (xs_addr(xprt)->sa_family == PF_INET6) { 233318d5ad62SChristoph Hellwig ip6_sock_set_addr_preferences(sk, 233418d5ad62SChristoph Hellwig IPV6_PREFER_SRC_PUBLIC); 233518d5ad62SChristoph Hellwig } 2336d88e4d82SNeilBrown 23378d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2338d737e5d4STrond Myklebust tcp_sock_set_nodelay(sk); 2339775f06abSTrond Myklebust 2340ea9afca8STrond Myklebust lock_sock(sk); 2341b0d93ad5SChuck Lever 23422a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23432a9e1cfaSTrond Myklebust 2344b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23455157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2346b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2347b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 23482118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 234998123866SBenjamin Coddington sk->sk_use_task_frag = false; 23503167e12cSChuck Lever 23513167e12cSChuck Lever /* socket options */ 23523167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 2353b0d93ad5SChuck Lever 2354b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2355b0d93ad5SChuck Lever 2356b0d93ad5SChuck Lever /* Reset to new socket */ 2357ee0ac0c2SChuck Lever transport->sock = sock; 2358ee0ac0c2SChuck Lever transport->inet = sk; 2359b0d93ad5SChuck Lever 2360ea9afca8STrond Myklebust release_sock(sk); 2361b0d93ad5SChuck Lever } 2362b0d93ad5SChuck Lever 236301d37c42STrond Myklebust if (!xprt_bound(xprt)) 2364280254b6STrond Myklebust return -ENOTCONN; 236501d37c42STrond Myklebust 2366a564b8f0SMel Gorman xs_set_memalloc(xprt); 2367a564b8f0SMel Gorman 2368ae053551STrond Myklebust xs_stream_start_connect(transport); 2369e1806c7bSTrond Myklebust 2370b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23710fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2372280254b6STrond Myklebust return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 237316be2d20SChuck Lever } 237416be2d20SChuck Lever 237516be2d20SChuck Lever /** 2376b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2377acf0a39fSChuck Lever * @work: queued work item 237816be2d20SChuck Lever * 237916be2d20SChuck Lever * Invoked by a work queue tasklet. 238016be2d20SChuck Lever */ 2381cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 238216be2d20SChuck Lever { 2383cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2384cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 238516be2d20SChuck Lever struct socket *sock = transport->sock; 2386a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2387280254b6STrond Myklebust int status; 23888db55a03SNeilBrown unsigned int pflags = current->flags; 238916be2d20SChuck Lever 23908db55a03SNeilBrown if (atomic_read(&xprt->swapper)) 23918db55a03SNeilBrown current->flags |= PF_MEMALLOC; 239289f42494STrond Myklebust 239389f42494STrond Myklebust if (xprt_connected(xprt)) 239489f42494STrond Myklebust goto out; 239589f42494STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT, 239689f42494STrond Myklebust &transport->sock_state) || 239789f42494STrond Myklebust !sock) { 239889f42494STrond Myklebust xs_reset_transport(transport); 239989f42494STrond Myklebust sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family, 240089f42494STrond Myklebust SOCK_STREAM, IPPROTO_TCP, true); 2401b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2402280254b6STrond Myklebust xprt_wake_pending_tasks(xprt, PTR_ERR(sock)); 240316be2d20SChuck Lever goto out; 240416be2d20SChuck Lever } 24057d1e8255STrond Myklebust } 24067d1e8255STrond Myklebust 2407c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2408c740eff8SChuck Lever "%s (port %s)\n", xprt, 2409c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2410c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2411c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 241216be2d20SChuck Lever 241316be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 241440b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2415a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 241646121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 241746121cf7SChuck Lever sock->sk->sk_state); 2418a246b010SChuck Lever switch (status) { 24192a491991STrond Myklebust case 0: 2420a246b010SChuck Lever case -EINPROGRESS: 2421280254b6STrond Myklebust /* SYN_SENT! */ 242289f42494STrond Myklebust set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); 2423280254b6STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2424280254b6STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2425280254b6STrond Myklebust fallthrough; 2426a246b010SChuck Lever case -EALREADY: 2427280254b6STrond Myklebust goto out_unlock; 2428280254b6STrond Myklebust case -EADDRNOTAVAIL: 2429280254b6STrond Myklebust /* Source port number is unavailable. Try a new one! */ 2430280254b6STrond Myklebust transport->srcport = 0; 2431280254b6STrond Myklebust status = -EAGAIN; 2432280254b6STrond Myklebust break; 2433d6c686c0SDaniel Borkmann case -EPERM: 2434d6c686c0SDaniel Borkmann /* Happens, for instance, if a BPF program is preventing 2435d6c686c0SDaniel Borkmann * the connect. Remap the error so upper layers can better 2436d6c686c0SDaniel Borkmann * deal with it. 2437d6c686c0SDaniel Borkmann */ 2438d6c686c0SDaniel Borkmann status = -ECONNREFUSED; 2439d6c686c0SDaniel Borkmann fallthrough; 24409fcfe0c8STrond Myklebust case -EINVAL: 24419fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24429fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24439fcfe0c8STrond Myklebust */ 24443ed5e2a2STrond Myklebust case -ECONNREFUSED: 24453ed5e2a2STrond Myklebust case -ECONNRESET: 2446eb5b46faSTrond Myklebust case -ENETDOWN: 24473ed5e2a2STrond Myklebust case -ENETUNREACH: 24484ba161a7STrond Myklebust case -EHOSTUNREACH: 24493913c78cSTrond Myklebust case -EADDRINUSE: 24503601c4a9STrond Myklebust case -ENOBUFS: 2451d428de97SNeilBrown case -ENOTCONN: 2452280254b6STrond Myklebust break; 2453280254b6STrond Myklebust default: 2454280254b6STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2455280254b6STrond Myklebust __func__, status); 2456280254b6STrond Myklebust status = -EAGAIN; 2457280254b6STrond Myklebust } 2458280254b6STrond Myklebust 24598c71139dSCalum Mackay /* xs_tcp_force_close() wakes tasks with a fixed error code. 24608c71139dSCalum Mackay * We need to wake them first to ensure the correct error code. 24616ea44adcSNeilBrown */ 24626ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24634efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 2464a246b010SChuck Lever out: 24652226feb6SChuck Lever xprt_clear_connecting(xprt); 2466280254b6STrond Myklebust out_unlock: 2467cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 24688db55a03SNeilBrown current_restore_flags(pflags, PF_MEMALLOC); 2469a246b010SChuck Lever } 2470a246b010SChuck Lever 247175eb6af7SChuck Lever /* 247275eb6af7SChuck Lever * Transfer the connected socket to @upper_transport, then mark that 247375eb6af7SChuck Lever * xprt CONNECTED. 247475eb6af7SChuck Lever */ 247575eb6af7SChuck Lever static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt, 247675eb6af7SChuck Lever struct sock_xprt *upper_transport) 247775eb6af7SChuck Lever { 247875eb6af7SChuck Lever struct sock_xprt *lower_transport = 247975eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 248075eb6af7SChuck Lever struct rpc_xprt *upper_xprt = &upper_transport->xprt; 248175eb6af7SChuck Lever 248275eb6af7SChuck Lever if (!upper_transport->inet) { 248375eb6af7SChuck Lever struct socket *sock = lower_transport->sock; 248475eb6af7SChuck Lever struct sock *sk = sock->sk; 248575eb6af7SChuck Lever 248675eb6af7SChuck Lever /* Avoid temporary address, they are bad for long-lived 248775eb6af7SChuck Lever * connections such as NFS mounts. 248875eb6af7SChuck Lever * RFC4941, section 3.6 suggests that: 248975eb6af7SChuck Lever * Individual applications, which have specific 249075eb6af7SChuck Lever * knowledge about the normal duration of connections, 249175eb6af7SChuck Lever * MAY override this as appropriate. 249275eb6af7SChuck Lever */ 249375eb6af7SChuck Lever if (xs_addr(upper_xprt)->sa_family == PF_INET6) 249475eb6af7SChuck Lever ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC); 249575eb6af7SChuck Lever 249675eb6af7SChuck Lever xs_tcp_set_socket_timeouts(upper_xprt, sock); 249775eb6af7SChuck Lever tcp_sock_set_nodelay(sk); 249875eb6af7SChuck Lever 249975eb6af7SChuck Lever lock_sock(sk); 250075eb6af7SChuck Lever 250175eb6af7SChuck Lever /* @sk is already connected, so it now has the RPC callbacks. 250275eb6af7SChuck Lever * Reach into @lower_transport to save the original ones. 250375eb6af7SChuck Lever */ 250475eb6af7SChuck Lever upper_transport->old_data_ready = lower_transport->old_data_ready; 250575eb6af7SChuck Lever upper_transport->old_state_change = lower_transport->old_state_change; 250675eb6af7SChuck Lever upper_transport->old_write_space = lower_transport->old_write_space; 250775eb6af7SChuck Lever upper_transport->old_error_report = lower_transport->old_error_report; 250875eb6af7SChuck Lever sk->sk_user_data = upper_xprt; 250975eb6af7SChuck Lever 251075eb6af7SChuck Lever /* socket options */ 251175eb6af7SChuck Lever sock_reset_flag(sk, SOCK_LINGER); 251275eb6af7SChuck Lever 251375eb6af7SChuck Lever xprt_clear_connected(upper_xprt); 251475eb6af7SChuck Lever 251575eb6af7SChuck Lever upper_transport->sock = sock; 251675eb6af7SChuck Lever upper_transport->inet = sk; 251775eb6af7SChuck Lever upper_transport->file = lower_transport->file; 251875eb6af7SChuck Lever 251975eb6af7SChuck Lever release_sock(sk); 252075eb6af7SChuck Lever 252175eb6af7SChuck Lever /* Reset lower_transport before shutting down its clnt */ 252275eb6af7SChuck Lever mutex_lock(&lower_transport->recv_mutex); 252375eb6af7SChuck Lever lower_transport->inet = NULL; 252475eb6af7SChuck Lever lower_transport->sock = NULL; 252575eb6af7SChuck Lever lower_transport->file = NULL; 252675eb6af7SChuck Lever 252775eb6af7SChuck Lever xprt_clear_connected(lower_xprt); 252875eb6af7SChuck Lever xs_sock_reset_connection_flags(lower_xprt); 252975eb6af7SChuck Lever xs_stream_reset_connect(lower_transport); 253075eb6af7SChuck Lever mutex_unlock(&lower_transport->recv_mutex); 253175eb6af7SChuck Lever } 253275eb6af7SChuck Lever 253375eb6af7SChuck Lever if (!xprt_bound(upper_xprt)) 253475eb6af7SChuck Lever return -ENOTCONN; 253575eb6af7SChuck Lever 253675eb6af7SChuck Lever xs_set_memalloc(upper_xprt); 253775eb6af7SChuck Lever 253875eb6af7SChuck Lever if (!xprt_test_and_set_connected(upper_xprt)) { 253975eb6af7SChuck Lever upper_xprt->connect_cookie++; 254075eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 254175eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 254275eb6af7SChuck Lever 254375eb6af7SChuck Lever upper_xprt->stat.connect_count++; 254475eb6af7SChuck Lever upper_xprt->stat.connect_time += (long)jiffies - 254575eb6af7SChuck Lever upper_xprt->stat.connect_start; 254675eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 254775eb6af7SChuck Lever } 254875eb6af7SChuck Lever return 0; 254975eb6af7SChuck Lever } 255075eb6af7SChuck Lever 255175eb6af7SChuck Lever /** 255275eb6af7SChuck Lever * xs_tls_handshake_done - TLS handshake completion handler 255375eb6af7SChuck Lever * @data: address of xprt to wake 255475eb6af7SChuck Lever * @status: status of handshake 255575eb6af7SChuck Lever * @peerid: serial number of key containing the remote's identity 255675eb6af7SChuck Lever * 255775eb6af7SChuck Lever */ 255875eb6af7SChuck Lever static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid) 255975eb6af7SChuck Lever { 256075eb6af7SChuck Lever struct rpc_xprt *lower_xprt = data; 256175eb6af7SChuck Lever struct sock_xprt *lower_transport = 256275eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 256375eb6af7SChuck Lever 256475eb6af7SChuck Lever lower_transport->xprt_err = status ? -EACCES : 0; 256575eb6af7SChuck Lever complete(&lower_transport->handshake_done); 256675eb6af7SChuck Lever xprt_put(lower_xprt); 256775eb6af7SChuck Lever } 256875eb6af7SChuck Lever 256975eb6af7SChuck Lever static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec) 257075eb6af7SChuck Lever { 257175eb6af7SChuck Lever struct sock_xprt *lower_transport = 257275eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 257375eb6af7SChuck Lever struct tls_handshake_args args = { 257475eb6af7SChuck Lever .ta_sock = lower_transport->sock, 257575eb6af7SChuck Lever .ta_done = xs_tls_handshake_done, 257675eb6af7SChuck Lever .ta_data = xprt_get(lower_xprt), 257775eb6af7SChuck Lever .ta_peername = lower_xprt->servername, 257875eb6af7SChuck Lever }; 257975eb6af7SChuck Lever struct sock *sk = lower_transport->inet; 258075eb6af7SChuck Lever int rc; 258175eb6af7SChuck Lever 258275eb6af7SChuck Lever init_completion(&lower_transport->handshake_done); 258375eb6af7SChuck Lever set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 258475eb6af7SChuck Lever lower_transport->xprt_err = -ETIMEDOUT; 258575eb6af7SChuck Lever switch (xprtsec->policy) { 258675eb6af7SChuck Lever case RPC_XPRTSEC_TLS_ANON: 258775eb6af7SChuck Lever rc = tls_client_hello_anon(&args, GFP_KERNEL); 258875eb6af7SChuck Lever if (rc) 258975eb6af7SChuck Lever goto out_put_xprt; 259075eb6af7SChuck Lever break; 259175eb6af7SChuck Lever case RPC_XPRTSEC_TLS_X509: 259275eb6af7SChuck Lever args.ta_my_cert = xprtsec->cert_serial; 259375eb6af7SChuck Lever args.ta_my_privkey = xprtsec->privkey_serial; 259475eb6af7SChuck Lever rc = tls_client_hello_x509(&args, GFP_KERNEL); 259575eb6af7SChuck Lever if (rc) 259675eb6af7SChuck Lever goto out_put_xprt; 259775eb6af7SChuck Lever break; 259875eb6af7SChuck Lever default: 259975eb6af7SChuck Lever rc = -EACCES; 260075eb6af7SChuck Lever goto out_put_xprt; 260175eb6af7SChuck Lever } 260275eb6af7SChuck Lever 260375eb6af7SChuck Lever rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done, 260475eb6af7SChuck Lever XS_TLS_HANDSHAKE_TO); 260575eb6af7SChuck Lever if (rc <= 0) { 2606931be6b7SBenjamin Coddington tls_handshake_cancel(sk); 260775eb6af7SChuck Lever if (rc == 0) 260875eb6af7SChuck Lever rc = -ETIMEDOUT; 260975eb6af7SChuck Lever goto out_put_xprt; 261075eb6af7SChuck Lever } 261175eb6af7SChuck Lever 261275eb6af7SChuck Lever rc = lower_transport->xprt_err; 261375eb6af7SChuck Lever 261475eb6af7SChuck Lever out: 261575eb6af7SChuck Lever xs_stream_reset_connect(lower_transport); 261675eb6af7SChuck Lever clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 261775eb6af7SChuck Lever return rc; 261875eb6af7SChuck Lever 261975eb6af7SChuck Lever out_put_xprt: 262075eb6af7SChuck Lever xprt_put(lower_xprt); 262175eb6af7SChuck Lever goto out; 262275eb6af7SChuck Lever } 262375eb6af7SChuck Lever 262475eb6af7SChuck Lever /** 262575eb6af7SChuck Lever * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket 262675eb6af7SChuck Lever * @work: queued work item 262775eb6af7SChuck Lever * 262875eb6af7SChuck Lever * Invoked by a work queue tasklet. 262975eb6af7SChuck Lever * 263075eb6af7SChuck Lever * For RPC-with-TLS, there is a two-stage connection process. 263175eb6af7SChuck Lever * 263275eb6af7SChuck Lever * The "upper-layer xprt" is visible to the RPC consumer. Once it has 263375eb6af7SChuck Lever * been marked connected, the consumer knows that a TCP connection and 263475eb6af7SChuck Lever * a TLS session have been established. 263575eb6af7SChuck Lever * 263675eb6af7SChuck Lever * A "lower-layer xprt", created in this function, handles the mechanics 263775eb6af7SChuck Lever * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and 263875eb6af7SChuck Lever * then driving the TLS handshake. Once all that is complete, the upper 263975eb6af7SChuck Lever * layer xprt is marked connected. 264075eb6af7SChuck Lever */ 264175eb6af7SChuck Lever static void xs_tcp_tls_setup_socket(struct work_struct *work) 264275eb6af7SChuck Lever { 264375eb6af7SChuck Lever struct sock_xprt *upper_transport = 264475eb6af7SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 264575eb6af7SChuck Lever struct rpc_clnt *upper_clnt = upper_transport->clnt; 264675eb6af7SChuck Lever struct rpc_xprt *upper_xprt = &upper_transport->xprt; 264775eb6af7SChuck Lever struct rpc_create_args args = { 264875eb6af7SChuck Lever .net = upper_xprt->xprt_net, 264975eb6af7SChuck Lever .protocol = upper_xprt->prot, 265075eb6af7SChuck Lever .address = (struct sockaddr *)&upper_xprt->addr, 265175eb6af7SChuck Lever .addrsize = upper_xprt->addrlen, 265275eb6af7SChuck Lever .timeout = upper_clnt->cl_timeout, 265375eb6af7SChuck Lever .servername = upper_xprt->servername, 265475eb6af7SChuck Lever .program = upper_clnt->cl_program, 265575eb6af7SChuck Lever .prognumber = upper_clnt->cl_prog, 265675eb6af7SChuck Lever .version = upper_clnt->cl_vers, 265775eb6af7SChuck Lever .authflavor = RPC_AUTH_TLS, 265875eb6af7SChuck Lever .cred = upper_clnt->cl_cred, 265975eb6af7SChuck Lever .xprtsec = { 266075eb6af7SChuck Lever .policy = RPC_XPRTSEC_NONE, 266175eb6af7SChuck Lever }, 26629b332c72SOlga Kornievskaia .stats = upper_clnt->cl_stats, 266375eb6af7SChuck Lever }; 266475eb6af7SChuck Lever unsigned int pflags = current->flags; 266575eb6af7SChuck Lever struct rpc_clnt *lower_clnt; 266675eb6af7SChuck Lever struct rpc_xprt *lower_xprt; 266775eb6af7SChuck Lever int status; 266875eb6af7SChuck Lever 266975eb6af7SChuck Lever if (atomic_read(&upper_xprt->swapper)) 267075eb6af7SChuck Lever current->flags |= PF_MEMALLOC; 267175eb6af7SChuck Lever 267275eb6af7SChuck Lever xs_stream_start_connect(upper_transport); 267375eb6af7SChuck Lever 267475eb6af7SChuck Lever /* This implicitly sends an RPC_AUTH_TLS probe */ 267575eb6af7SChuck Lever lower_clnt = rpc_create(&args); 267675eb6af7SChuck Lever if (IS_ERR(lower_clnt)) { 267775eb6af7SChuck Lever trace_rpc_tls_unavailable(upper_clnt, upper_xprt); 267875eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 267975eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 268075eb6af7SChuck Lever xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt)); 268175eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 268275eb6af7SChuck Lever goto out_unlock; 268375eb6af7SChuck Lever } 268475eb6af7SChuck Lever 268575eb6af7SChuck Lever /* RPC_AUTH_TLS probe was successful. Try a TLS handshake on 268675eb6af7SChuck Lever * the lower xprt. 268775eb6af7SChuck Lever */ 268875eb6af7SChuck Lever rcu_read_lock(); 268975eb6af7SChuck Lever lower_xprt = rcu_dereference(lower_clnt->cl_xprt); 269075eb6af7SChuck Lever rcu_read_unlock(); 269126e8bfa3SAnna Schumaker 269226e8bfa3SAnna Schumaker if (wait_on_bit_lock(&lower_xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 269326e8bfa3SAnna Schumaker goto out_unlock; 269426e8bfa3SAnna Schumaker 269575eb6af7SChuck Lever status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec); 269675eb6af7SChuck Lever if (status) { 269775eb6af7SChuck Lever trace_rpc_tls_not_started(upper_clnt, upper_xprt); 269875eb6af7SChuck Lever goto out_close; 269975eb6af7SChuck Lever } 270075eb6af7SChuck Lever 270175eb6af7SChuck Lever status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport); 270275eb6af7SChuck Lever if (status) 270375eb6af7SChuck Lever goto out_close; 270426e8bfa3SAnna Schumaker xprt_release_write(lower_xprt, NULL); 270575eb6af7SChuck Lever 270675eb6af7SChuck Lever trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0); 270775eb6af7SChuck Lever if (!xprt_test_and_set_connected(upper_xprt)) { 270875eb6af7SChuck Lever upper_xprt->connect_cookie++; 270975eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 271075eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 271175eb6af7SChuck Lever 271275eb6af7SChuck Lever upper_xprt->stat.connect_count++; 271375eb6af7SChuck Lever upper_xprt->stat.connect_time += (long)jiffies - 271475eb6af7SChuck Lever upper_xprt->stat.connect_start; 271575eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 271675eb6af7SChuck Lever } 271775eb6af7SChuck Lever rpc_shutdown_client(lower_clnt); 271875eb6af7SChuck Lever 271975eb6af7SChuck Lever out_unlock: 272075eb6af7SChuck Lever current_restore_flags(pflags, PF_MEMALLOC); 272175eb6af7SChuck Lever upper_transport->clnt = NULL; 272275eb6af7SChuck Lever xprt_unlock_connect(upper_xprt, upper_transport); 272375eb6af7SChuck Lever return; 272475eb6af7SChuck Lever 272575eb6af7SChuck Lever out_close: 272626e8bfa3SAnna Schumaker xprt_release_write(lower_xprt, NULL); 272775eb6af7SChuck Lever rpc_shutdown_client(lower_clnt); 272875eb6af7SChuck Lever 272975eb6af7SChuck Lever /* xprt_force_disconnect() wakes tasks with a fixed tk_status code. 273075eb6af7SChuck Lever * Wake them first here to ensure they get our tk_status code. 273175eb6af7SChuck Lever */ 273275eb6af7SChuck Lever xprt_wake_pending_tasks(upper_xprt, status); 273375eb6af7SChuck Lever xs_tcp_force_close(upper_xprt); 273475eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 273575eb6af7SChuck Lever goto out_unlock; 273675eb6af7SChuck Lever } 273775eb6af7SChuck Lever 273868e220bdSChuck Lever /** 27399903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 27401b092092STrond Myklebust * @xprt: pointer to transport structure 27419903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 27429903cd1cSChuck Lever * 27439903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 274403bf4b70SChuck Lever * 274503bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 274603bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 274703bf4b70SChuck Lever * socket on a privileged port. 274803bf4b70SChuck Lever * 274903bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 275003bf4b70SChuck Lever * retry floods (hard mounts). 27519903cd1cSChuck Lever */ 27521b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2753a246b010SChuck Lever { 2754ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 275502910177STrond Myklebust unsigned long delay = 0; 2756a246b010SChuck Lever 2757718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2758718ba5b8STrond Myklebust 275989f42494STrond Myklebust if (transport->sock != NULL) { 276046121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 276189f42494STrond Myklebust "seconds\n", xprt, xprt->reestablish_timeout / HZ); 276299b1a4c3STrond Myklebust 2763675dd90aSChuck Lever delay = xprt_reconnect_delay(xprt); 2764675dd90aSChuck Lever xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 276502910177STrond Myklebust 276602910177STrond Myklebust } else 27679903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 276802910177STrond Myklebust 276975eb6af7SChuck Lever transport->clnt = task->tk_client; 277040a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 277102910177STrond Myklebust &transport->connect_worker, 277202910177STrond Myklebust delay); 2773a246b010SChuck Lever } 2774a246b010SChuck Lever 27754f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 27764f8943f8STrond Myklebust { 27774f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 27784f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 27794f8943f8STrond Myklebust } 27804f8943f8STrond Myklebust 27814f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 27824f8943f8STrond Myklebust { 27834f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 27844f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 27854f8943f8STrond Myklebust } 27864f8943f8STrond Myklebust 27874f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 27884f8943f8STrond Myklebust { 27894f8943f8STrond Myklebust int sockerr; 27904f8943f8STrond Myklebust 27914f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 27924f8943f8STrond Myklebust return; 27934f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 27944f8943f8STrond Myklebust if (transport->sock == NULL) 27954f8943f8STrond Myklebust goto out; 27964f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 27974f8943f8STrond Myklebust goto out; 2798af84537dSBenjamin Coddington sockerr = xchg(&transport->xprt_err, 0); 27994f8943f8STrond Myklebust if (sockerr < 0) 28004f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 28014f8943f8STrond Myklebust out: 28024f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 28034f8943f8STrond Myklebust } 28044f8943f8STrond Myklebust 28054f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 28064f8943f8STrond Myklebust { 28074f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 28084f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 28094f8943f8STrond Myklebust } 28104f8943f8STrond Myklebust 28114f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 28124f8943f8STrond Myklebust { 28134f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 28144f8943f8STrond Myklebust struct sock_xprt, error_worker); 28154f8943f8STrond Myklebust 28164f8943f8STrond Myklebust xs_wake_disconnect(transport); 28174f8943f8STrond Myklebust xs_wake_write(transport); 28184f8943f8STrond Myklebust xs_wake_error(transport); 28194f8943f8STrond Myklebust xs_wake_pending(transport); 28204f8943f8STrond Myklebust } 28214f8943f8STrond Myklebust 2822262ca07dSChuck Lever /** 282312b20ce3SBhaskar Chowdhury * xs_local_print_stats - display AF_LOCAL socket-specific stats 2824176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2825176e21eeSChuck Lever * @seq: output file 2826176e21eeSChuck Lever * 2827176e21eeSChuck Lever */ 2828176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2829176e21eeSChuck Lever { 2830176e21eeSChuck Lever long idle_time = 0; 2831176e21eeSChuck Lever 2832176e21eeSChuck Lever if (xprt_connected(xprt)) 2833176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2834176e21eeSChuck Lever 2835176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 283615a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2837176e21eeSChuck Lever xprt->stat.bind_count, 2838176e21eeSChuck Lever xprt->stat.connect_count, 28398440a886SChuck Lever xprt->stat.connect_time / HZ, 2840176e21eeSChuck Lever idle_time, 2841176e21eeSChuck Lever xprt->stat.sends, 2842176e21eeSChuck Lever xprt->stat.recvs, 2843176e21eeSChuck Lever xprt->stat.bad_xids, 2844176e21eeSChuck Lever xprt->stat.req_u, 284515a45206SAndy Adamson xprt->stat.bklog_u, 284615a45206SAndy Adamson xprt->stat.max_slots, 284715a45206SAndy Adamson xprt->stat.sending_u, 284815a45206SAndy Adamson xprt->stat.pending_u); 2849176e21eeSChuck Lever } 2850176e21eeSChuck Lever 2851176e21eeSChuck Lever /** 285212b20ce3SBhaskar Chowdhury * xs_udp_print_stats - display UDP socket-specific stats 2853262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2854262ca07dSChuck Lever * @seq: output file 2855262ca07dSChuck Lever * 2856262ca07dSChuck Lever */ 2857262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2858262ca07dSChuck Lever { 2859c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2860c8475461SChuck Lever 286115a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 286215a45206SAndy Adamson "%lu %llu %llu\n", 2863fbfffbd5SChuck Lever transport->srcport, 2864262ca07dSChuck Lever xprt->stat.bind_count, 2865262ca07dSChuck Lever xprt->stat.sends, 2866262ca07dSChuck Lever xprt->stat.recvs, 2867262ca07dSChuck Lever xprt->stat.bad_xids, 2868262ca07dSChuck Lever xprt->stat.req_u, 286915a45206SAndy Adamson xprt->stat.bklog_u, 287015a45206SAndy Adamson xprt->stat.max_slots, 287115a45206SAndy Adamson xprt->stat.sending_u, 287215a45206SAndy Adamson xprt->stat.pending_u); 2873262ca07dSChuck Lever } 2874262ca07dSChuck Lever 2875262ca07dSChuck Lever /** 287612b20ce3SBhaskar Chowdhury * xs_tcp_print_stats - display TCP socket-specific stats 2877262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2878262ca07dSChuck Lever * @seq: output file 2879262ca07dSChuck Lever * 2880262ca07dSChuck Lever */ 2881262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2882262ca07dSChuck Lever { 2883c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2884262ca07dSChuck Lever long idle_time = 0; 2885262ca07dSChuck Lever 2886262ca07dSChuck Lever if (xprt_connected(xprt)) 2887262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2888262ca07dSChuck Lever 288915a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 289015a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2891fbfffbd5SChuck Lever transport->srcport, 2892262ca07dSChuck Lever xprt->stat.bind_count, 2893262ca07dSChuck Lever xprt->stat.connect_count, 28948440a886SChuck Lever xprt->stat.connect_time / HZ, 2895262ca07dSChuck Lever idle_time, 2896262ca07dSChuck Lever xprt->stat.sends, 2897262ca07dSChuck Lever xprt->stat.recvs, 2898262ca07dSChuck Lever xprt->stat.bad_xids, 2899262ca07dSChuck Lever xprt->stat.req_u, 290015a45206SAndy Adamson xprt->stat.bklog_u, 290115a45206SAndy Adamson xprt->stat.max_slots, 290215a45206SAndy Adamson xprt->stat.sending_u, 290315a45206SAndy Adamson xprt->stat.pending_u); 2904262ca07dSChuck Lever } 2905262ca07dSChuck Lever 29064cfc7e60SRahul Iyer /* 29074cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 29084cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 29094cfc7e60SRahul Iyer * to use the server side send routines. 29104cfc7e60SRahul Iyer */ 29115fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 29124cfc7e60SRahul Iyer { 29135fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 29145fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 29154cfc7e60SRahul Iyer struct page *page; 29164cfc7e60SRahul Iyer struct rpc_buffer *buf; 29174cfc7e60SRahul Iyer 29185fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 29195fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 29205fe6eaa1SChuck Lever size); 29215fe6eaa1SChuck Lever return -EINVAL; 29225fe6eaa1SChuck Lever } 29234cfc7e60SRahul Iyer 2924b2648015STrond Myklebust page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 29254cfc7e60SRahul Iyer if (!page) 29265fe6eaa1SChuck Lever return -ENOMEM; 29274cfc7e60SRahul Iyer 29284cfc7e60SRahul Iyer buf = page_address(page); 29294cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 29304cfc7e60SRahul Iyer 29315fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 293218e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 29335fe6eaa1SChuck Lever return 0; 29344cfc7e60SRahul Iyer } 29354cfc7e60SRahul Iyer 29364cfc7e60SRahul Iyer /* 29374cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 29384cfc7e60SRahul Iyer */ 29393435c74aSChuck Lever static void bc_free(struct rpc_task *task) 29404cfc7e60SRahul Iyer { 29413435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 29424cfc7e60SRahul Iyer struct rpc_buffer *buf; 29434cfc7e60SRahul Iyer 29444cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 29454cfc7e60SRahul Iyer free_page((unsigned long)buf); 29464cfc7e60SRahul Iyer } 29474cfc7e60SRahul Iyer 29484cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 29494cfc7e60SRahul Iyer { 2950da1661b9SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 29514cfc7e60SRahul Iyer struct sock_xprt *transport = 2952067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 2953067fb11bSChuck Lever struct msghdr msg = { 2954da1661b9SChuck Lever .msg_flags = 0, 2955067fb11bSChuck Lever }; 2956067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2957da1661b9SChuck Lever (u32)xdr->len); 2958da1661b9SChuck Lever unsigned int sent = 0; 2959da1661b9SChuck Lever int err; 29604cfc7e60SRahul Iyer 29618729aabaSChuck Lever req->rq_xtime = ktime_get(); 2962ff053dbbSTrond Myklebust err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 2963ff053dbbSTrond Myklebust if (err < 0) 2964ff053dbbSTrond Myklebust return err; 2965da1661b9SChuck Lever err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); 2966da1661b9SChuck Lever xdr_free_bvec(xdr); 2967da1661b9SChuck Lever if (err < 0 || sent != (xdr->len + sizeof(marker))) 2968067fb11bSChuck Lever return -EAGAIN; 2969da1661b9SChuck Lever return sent; 29704cfc7e60SRahul Iyer } 29714cfc7e60SRahul Iyer 2972ca4faf54SChuck Lever /** 2973ca4faf54SChuck Lever * bc_send_request - Send a backchannel Call on a TCP socket 2974ca4faf54SChuck Lever * @req: rpc_rqst containing Call message to be sent 2975ca4faf54SChuck Lever * 2976ca4faf54SChuck Lever * xpt_mutex ensures @rqstp's whole message is written to the socket 2977ca4faf54SChuck Lever * without interruption. 2978ca4faf54SChuck Lever * 2979ca4faf54SChuck Lever * Return values: 2980ca4faf54SChuck Lever * %0 if the message was sent successfully 2981ca4faf54SChuck Lever * %ENOTCONN if the message was not sent 29824cfc7e60SRahul Iyer */ 2983adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 29844cfc7e60SRahul Iyer { 29854cfc7e60SRahul Iyer struct svc_xprt *xprt; 29867fc56136SAndrzej Hajda int len; 29874cfc7e60SRahul Iyer 29884cfc7e60SRahul Iyer /* 29894cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 29904cfc7e60SRahul Iyer */ 29914cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 29924cfc7e60SRahul Iyer 29934cfc7e60SRahul Iyer /* 29944cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 29954cfc7e60SRahul Iyer * with the fore channel 29964cfc7e60SRahul Iyer */ 2997c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 29984cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 29994cfc7e60SRahul Iyer len = -ENOTCONN; 30004cfc7e60SRahul Iyer else 30014cfc7e60SRahul Iyer len = bc_sendto(req); 30024cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 30034cfc7e60SRahul Iyer 30044cfc7e60SRahul Iyer if (len > 0) 30054cfc7e60SRahul Iyer len = 0; 30064cfc7e60SRahul Iyer 30074cfc7e60SRahul Iyer return len; 30084cfc7e60SRahul Iyer } 30094cfc7e60SRahul Iyer 30104cfc7e60SRahul Iyer /* 30114cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 30124cfc7e60SRahul Iyer */ 30134cfc7e60SRahul Iyer 30144cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 30154cfc7e60SRahul Iyer { 30166221f1d9SChuck Lever xprt_disconnect_done(xprt); 30174cfc7e60SRahul Iyer } 30184cfc7e60SRahul Iyer 30194cfc7e60SRahul Iyer /* 30204cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 30214cfc7e60SRahul Iyer * initiated, we do nothing 30224cfc7e60SRahul Iyer */ 30234cfc7e60SRahul Iyer 30244cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 30254cfc7e60SRahul Iyer { 302647f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 302747f72efaSKinglong Mee 302847f72efaSKinglong Mee xs_xprt_free(xprt); 302947f72efaSKinglong Mee module_put(THIS_MODULE); 30304cfc7e60SRahul Iyer } 30314cfc7e60SRahul Iyer 3032d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 3033176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 30344cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 3035f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 3036a9cde23aSChuck Lever .free_slot = xprt_free_slot, 3037176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 3038176e21eeSChuck Lever .set_port = xs_local_set_port, 3039dc107402SJ. Bruce Fields .connect = xs_local_connect, 3040176e21eeSChuck Lever .buf_alloc = rpc_malloc, 3041176e21eeSChuck Lever .buf_free = rpc_free, 3042550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 3043176e21eeSChuck Lever .send_request = xs_local_send_request, 30448ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 3045176e21eeSChuck Lever .close = xs_close, 3046a1311d87STrond Myklebust .destroy = xs_destroy, 3047176e21eeSChuck Lever .print_stats = xs_local_print_stats, 3048d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3049d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 3050176e21eeSChuck Lever }; 3051176e21eeSChuck Lever 3052d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 305343118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 305412a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 305549e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 3056f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 3057a9cde23aSChuck Lever .free_slot = xprt_free_slot, 305845160d62SChuck Lever .rpcbind = rpcb_getport_async, 305992200412SChuck Lever .set_port = xs_set_port, 30609903cd1cSChuck Lever .connect = xs_connect, 3061421ab1beSTrond Myklebust .get_srcaddr = xs_sock_srcaddr, 3062421ab1beSTrond Myklebust .get_srcport = xs_sock_srcport, 306302107148SChuck Lever .buf_alloc = rpc_malloc, 306402107148SChuck Lever .buf_free = rpc_free, 3065262965f5SChuck Lever .send_request = xs_udp_send_request, 30668ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 306746c0ee8bSChuck Lever .timer = xs_udp_timer, 3068a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 3069262965f5SChuck Lever .close = xs_close, 3070262965f5SChuck Lever .destroy = xs_destroy, 3071262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 3072d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3073d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30744a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 3075262965f5SChuck Lever }; 3076262965f5SChuck Lever 3077d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 307812a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 30794cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 308036bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 3081a9cde23aSChuck Lever .free_slot = xprt_free_slot, 308245160d62SChuck Lever .rpcbind = rpcb_getport_async, 308392200412SChuck Lever .set_port = xs_set_port, 30840b9e7943STrond Myklebust .connect = xs_connect, 3085421ab1beSTrond Myklebust .get_srcaddr = xs_sock_srcaddr, 3086421ab1beSTrond Myklebust .get_srcport = xs_sock_srcport, 308702107148SChuck Lever .buf_alloc = rpc_malloc, 308802107148SChuck Lever .buf_free = rpc_free, 3089277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 3090262965f5SChuck Lever .send_request = xs_tcp_send_request, 30918ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 3092c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 30939903cd1cSChuck Lever .destroy = xs_destroy, 30947196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 3095262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 3096d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3097d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30984a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 309942e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 310042e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 31016b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 31027402a4feSTrond Myklebust .bc_num_slots = xprt_bc_max_slots, 310342e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 310442e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 310542e5c3e2SChuck Lever #endif 3106a246b010SChuck Lever }; 3107a246b010SChuck Lever 31084cfc7e60SRahul Iyer /* 31094cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 31104cfc7e60SRahul Iyer */ 31114cfc7e60SRahul Iyer 3112d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 31134cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 31144cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 311584e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 3116a9cde23aSChuck Lever .free_slot = xprt_free_slot, 31174cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 31184cfc7e60SRahul Iyer .buf_free = bc_free, 31194cfc7e60SRahul Iyer .send_request = bc_send_request, 31208ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 31214cfc7e60SRahul Iyer .close = bc_close, 31224cfc7e60SRahul Iyer .destroy = bc_destroy, 31234cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 3124d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3125d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 31264a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 31274cfc7e60SRahul Iyer }; 31284cfc7e60SRahul Iyer 312992476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 313092476850SChuck Lever { 313192476850SChuck Lever static const struct sockaddr_in sin = { 313292476850SChuck Lever .sin_family = AF_INET, 313392476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 313492476850SChuck Lever }; 313592476850SChuck Lever static const struct sockaddr_in6 sin6 = { 313692476850SChuck Lever .sin6_family = AF_INET6, 313792476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 313892476850SChuck Lever }; 313992476850SChuck Lever 314092476850SChuck Lever switch (family) { 3141176e21eeSChuck Lever case AF_LOCAL: 3142176e21eeSChuck Lever break; 314392476850SChuck Lever case AF_INET: 314492476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 314592476850SChuck Lever break; 314692476850SChuck Lever case AF_INET6: 314792476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 314892476850SChuck Lever break; 314992476850SChuck Lever default: 315092476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 315192476850SChuck Lever return -EAFNOSUPPORT; 315292476850SChuck Lever } 315392476850SChuck Lever return 0; 315492476850SChuck Lever } 315592476850SChuck Lever 31563c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 3157d9ba131dSTrond Myklebust unsigned int slot_table_size, 3158d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 3159c8541ecdSChuck Lever { 3160c8541ecdSChuck Lever struct rpc_xprt *xprt; 3161ffc2e518SChuck Lever struct sock_xprt *new; 3162c8541ecdSChuck Lever 316396802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 3164c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 3165c8541ecdSChuck Lever return ERR_PTR(-EBADF); 3166c8541ecdSChuck Lever } 3167c8541ecdSChuck Lever 3168d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 3169d9ba131dSTrond Myklebust max_slot_table_size); 3170bd1722d4SPavel Emelyanov if (xprt == NULL) { 317146121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 317246121cf7SChuck Lever "rpc_xprt\n"); 3173c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 3174c8541ecdSChuck Lever } 3175c8541ecdSChuck Lever 3176bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 3177edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 317896802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 317996802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 3180d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 3181fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 318292476850SChuck Lever else { 318392476850SChuck Lever int err; 318492476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 318592476850SChuck Lever (struct sockaddr *)&new->srcaddr); 31862aa13531SStanislav Kinsbursky if (err != 0) { 31872aa13531SStanislav Kinsbursky xprt_free(xprt); 318892476850SChuck Lever return ERR_PTR(err); 318992476850SChuck Lever } 31902aa13531SStanislav Kinsbursky } 3191c8541ecdSChuck Lever 3192c8541ecdSChuck Lever return xprt; 3193c8541ecdSChuck Lever } 3194c8541ecdSChuck Lever 3195176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 3196176e21eeSChuck Lever .to_initval = 10 * HZ, 3197176e21eeSChuck Lever .to_maxval = 10 * HZ, 3198176e21eeSChuck Lever .to_retries = 2, 3199176e21eeSChuck Lever }; 3200176e21eeSChuck Lever 3201176e21eeSChuck Lever /** 3202176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 3203176e21eeSChuck Lever * @args: rpc transport creation arguments 3204176e21eeSChuck Lever * 3205176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 3206176e21eeSChuck Lever */ 3207176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 3208176e21eeSChuck Lever { 3209176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 3210176e21eeSChuck Lever struct sock_xprt *transport; 3211176e21eeSChuck Lever struct rpc_xprt *xprt; 3212176e21eeSChuck Lever struct rpc_xprt *ret; 3213176e21eeSChuck Lever 3214d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3215d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 3216176e21eeSChuck Lever if (IS_ERR(xprt)) 3217176e21eeSChuck Lever return xprt; 3218176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3219176e21eeSChuck Lever 3220176e21eeSChuck Lever xprt->prot = 0; 3221d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_local_transport; 3222176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3223176e21eeSChuck Lever 3224176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 3225176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 3226176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3227176e21eeSChuck Lever 3228176e21eeSChuck Lever xprt->ops = &xs_local_ops; 3229176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 3230176e21eeSChuck Lever 3231550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 32324f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3233550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 323493dc41bdSNeilBrown 3235176e21eeSChuck Lever switch (sun->sun_family) { 3236176e21eeSChuck Lever case AF_LOCAL: 32374388ce05SNeilBrown if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') { 3238176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 3239176e21eeSChuck Lever sun->sun_path); 3240176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 3241176e21eeSChuck Lever goto out_err; 3242176e21eeSChuck Lever } 3243176e21eeSChuck Lever xprt_set_bound(xprt); 3244176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 3245176e21eeSChuck Lever break; 3246176e21eeSChuck Lever default: 3247176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 3248176e21eeSChuck Lever goto out_err; 3249176e21eeSChuck Lever } 3250176e21eeSChuck Lever 3251176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 3252176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 3253176e21eeSChuck Lever 3254176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 3255176e21eeSChuck Lever return xprt; 3256176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 3257176e21eeSChuck Lever out_err: 3258315f3812SKinglong Mee xs_xprt_free(xprt); 3259176e21eeSChuck Lever return ret; 3260176e21eeSChuck Lever } 3261176e21eeSChuck Lever 32622881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 32632881ae74STrond Myklebust .to_initval = 5 * HZ, 32642881ae74STrond Myklebust .to_maxval = 30 * HZ, 32652881ae74STrond Myklebust .to_increment = 5 * HZ, 32662881ae74STrond Myklebust .to_retries = 5, 32672881ae74STrond Myklebust }; 32682881ae74STrond Myklebust 32699903cd1cSChuck Lever /** 32709903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 327196802a09SFrank van Maarseveen * @args: rpc transport creation arguments 32729903cd1cSChuck Lever * 32739903cd1cSChuck Lever */ 3274483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 3275a246b010SChuck Lever { 32768f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3277c8541ecdSChuck Lever struct rpc_xprt *xprt; 3278c8475461SChuck Lever struct sock_xprt *transport; 32790a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3280a246b010SChuck Lever 3281d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 3282d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 3283c8541ecdSChuck Lever if (IS_ERR(xprt)) 3284c8541ecdSChuck Lever return xprt; 3285c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3286a246b010SChuck Lever 3287ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 3288d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_udp_transport; 3289a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 3290a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3291a246b010SChuck Lever 329203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 329303bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 329403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3295a246b010SChuck Lever 3296262965f5SChuck Lever xprt->ops = &xs_udp_ops; 3297a246b010SChuck Lever 3298ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 3299a246b010SChuck Lever 3300f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 33014f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3302edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3303edc1b01cSTrond Myklebust 33048f9d5b1aSChuck Lever switch (addr->sa_family) { 33058f9d5b1aSChuck Lever case AF_INET: 33068f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 33078f9d5b1aSChuck Lever xprt_set_bound(xprt); 33088f9d5b1aSChuck Lever 33099dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 33108f9d5b1aSChuck Lever break; 33118f9d5b1aSChuck Lever case AF_INET6: 33128f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 33138f9d5b1aSChuck Lever xprt_set_bound(xprt); 33148f9d5b1aSChuck Lever 33159dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 33168f9d5b1aSChuck Lever break; 33178f9d5b1aSChuck Lever default: 33180a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 33190a68b0beSJ. Bruce Fields goto out_err; 33208f9d5b1aSChuck Lever } 33218f9d5b1aSChuck Lever 3322c740eff8SChuck Lever if (xprt_bound(xprt)) 3323c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3324c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3325c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3326c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3327c740eff8SChuck Lever else 3328c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3329c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3330c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3331edb267a6SChuck Lever 3332bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3333c8541ecdSChuck Lever return xprt; 33340a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 33350a68b0beSJ. Bruce Fields out_err: 3336315f3812SKinglong Mee xs_xprt_free(xprt); 33370a68b0beSJ. Bruce Fields return ret; 3338a246b010SChuck Lever } 3339a246b010SChuck Lever 33402881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 33412881ae74STrond Myklebust .to_initval = 60 * HZ, 33422881ae74STrond Myklebust .to_maxval = 60 * HZ, 33432881ae74STrond Myklebust .to_retries = 2, 33442881ae74STrond Myklebust }; 33452881ae74STrond Myklebust 33469903cd1cSChuck Lever /** 33479903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 334896802a09SFrank van Maarseveen * @args: rpc transport creation arguments 33499903cd1cSChuck Lever * 33509903cd1cSChuck Lever */ 3351483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3352a246b010SChuck Lever { 33538f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3354c8541ecdSChuck Lever struct rpc_xprt *xprt; 3355c8475461SChuck Lever struct sock_xprt *transport; 33560a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3357b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3358b7993cebSTrond Myklebust 3359b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3360b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3361a246b010SChuck Lever 3362d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3363b7993cebSTrond Myklebust max_slot_table_size); 3364c8541ecdSChuck Lever if (IS_ERR(xprt)) 3365c8541ecdSChuck Lever return xprt; 3366c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3367a246b010SChuck Lever 3368ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3369d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_tcp_transport; 3370808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3371a246b010SChuck Lever 337203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 337303bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 337403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3375a246b010SChuck Lever 3376262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3377ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3378a246b010SChuck Lever 33793851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 3380d2ee4138STrond Myklebust if (args->reconnect_timeout) 3381d2ee4138STrond Myklebust xprt->max_reconnect_timeout = args->reconnect_timeout; 3382d2ee4138STrond Myklebust 33837196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 33847196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 3385d2ee4138STrond Myklebust if (args->connect_timeout) 3386d2ee4138STrond Myklebust xs_tcp_do_set_connect_timeout(xprt, args->connect_timeout); 33873851f1cdSTrond Myklebust 3388c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 33894f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3390edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3391edc1b01cSTrond Myklebust 33928f9d5b1aSChuck Lever switch (addr->sa_family) { 33938f9d5b1aSChuck Lever case AF_INET: 33948f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 33958f9d5b1aSChuck Lever xprt_set_bound(xprt); 33968f9d5b1aSChuck Lever 33979dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 33988f9d5b1aSChuck Lever break; 33998f9d5b1aSChuck Lever case AF_INET6: 34008f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 34018f9d5b1aSChuck Lever xprt_set_bound(xprt); 34028f9d5b1aSChuck Lever 34039dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 34048f9d5b1aSChuck Lever break; 34058f9d5b1aSChuck Lever default: 34060a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 34070a68b0beSJ. Bruce Fields goto out_err; 34088f9d5b1aSChuck Lever } 34098f9d5b1aSChuck Lever 3410c740eff8SChuck Lever if (xprt_bound(xprt)) 3411c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3412c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3413c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3414c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3415c740eff8SChuck Lever else 3416c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3417c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3418c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3419c740eff8SChuck Lever 3420bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3421c8541ecdSChuck Lever return xprt; 34220a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 34230a68b0beSJ. Bruce Fields out_err: 3424315f3812SKinglong Mee xs_xprt_free(xprt); 34250a68b0beSJ. Bruce Fields return ret; 3426a246b010SChuck Lever } 3427282b32e1SChuck Lever 3428f300babaSAlexandros Batsakis /** 342975eb6af7SChuck Lever * xs_setup_tcp_tls - Set up transport to use a TCP with TLS 343075eb6af7SChuck Lever * @args: rpc transport creation arguments 343175eb6af7SChuck Lever * 343275eb6af7SChuck Lever */ 343375eb6af7SChuck Lever static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args) 343475eb6af7SChuck Lever { 343575eb6af7SChuck Lever struct sockaddr *addr = args->dstaddr; 343675eb6af7SChuck Lever struct rpc_xprt *xprt; 343775eb6af7SChuck Lever struct sock_xprt *transport; 343875eb6af7SChuck Lever struct rpc_xprt *ret; 343975eb6af7SChuck Lever unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 344075eb6af7SChuck Lever 344175eb6af7SChuck Lever if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 344275eb6af7SChuck Lever max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 344375eb6af7SChuck Lever 344475eb6af7SChuck Lever xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 344575eb6af7SChuck Lever max_slot_table_size); 344675eb6af7SChuck Lever if (IS_ERR(xprt)) 344775eb6af7SChuck Lever return xprt; 344875eb6af7SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 344975eb6af7SChuck Lever 345075eb6af7SChuck Lever xprt->prot = IPPROTO_TCP; 345175eb6af7SChuck Lever xprt->xprt_class = &xs_tcp_transport; 345275eb6af7SChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 345375eb6af7SChuck Lever 345475eb6af7SChuck Lever xprt->bind_timeout = XS_BIND_TO; 345575eb6af7SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 345675eb6af7SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 345775eb6af7SChuck Lever 345875eb6af7SChuck Lever xprt->ops = &xs_tcp_ops; 345975eb6af7SChuck Lever xprt->timeout = &xs_tcp_default_timeout; 346075eb6af7SChuck Lever 346175eb6af7SChuck Lever xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 346275eb6af7SChuck Lever xprt->connect_timeout = xprt->timeout->to_initval * 346375eb6af7SChuck Lever (xprt->timeout->to_retries + 1); 346475eb6af7SChuck Lever 346575eb6af7SChuck Lever INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 346675eb6af7SChuck Lever INIT_WORK(&transport->error_worker, xs_error_handle); 346775eb6af7SChuck Lever 346875eb6af7SChuck Lever switch (args->xprtsec.policy) { 346975eb6af7SChuck Lever case RPC_XPRTSEC_TLS_ANON: 347075eb6af7SChuck Lever case RPC_XPRTSEC_TLS_X509: 347175eb6af7SChuck Lever xprt->xprtsec = args->xprtsec; 347275eb6af7SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 347375eb6af7SChuck Lever xs_tcp_tls_setup_socket); 347475eb6af7SChuck Lever break; 347575eb6af7SChuck Lever default: 347675eb6af7SChuck Lever ret = ERR_PTR(-EACCES); 347775eb6af7SChuck Lever goto out_err; 347875eb6af7SChuck Lever } 347975eb6af7SChuck Lever 348075eb6af7SChuck Lever switch (addr->sa_family) { 348175eb6af7SChuck Lever case AF_INET: 348275eb6af7SChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 348375eb6af7SChuck Lever xprt_set_bound(xprt); 348475eb6af7SChuck Lever 348575eb6af7SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 348675eb6af7SChuck Lever break; 348775eb6af7SChuck Lever case AF_INET6: 348875eb6af7SChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 348975eb6af7SChuck Lever xprt_set_bound(xprt); 349075eb6af7SChuck Lever 349175eb6af7SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 349275eb6af7SChuck Lever break; 349375eb6af7SChuck Lever default: 349475eb6af7SChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 349575eb6af7SChuck Lever goto out_err; 349675eb6af7SChuck Lever } 349775eb6af7SChuck Lever 349875eb6af7SChuck Lever if (xprt_bound(xprt)) 349975eb6af7SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 350075eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 350175eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 350275eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 350375eb6af7SChuck Lever else 350475eb6af7SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 350575eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 350675eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 350775eb6af7SChuck Lever 350875eb6af7SChuck Lever if (try_module_get(THIS_MODULE)) 350975eb6af7SChuck Lever return xprt; 351075eb6af7SChuck Lever ret = ERR_PTR(-EINVAL); 351175eb6af7SChuck Lever out_err: 351275eb6af7SChuck Lever xs_xprt_free(xprt); 351375eb6af7SChuck Lever return ret; 351475eb6af7SChuck Lever } 351575eb6af7SChuck Lever 351675eb6af7SChuck Lever /** 3517f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3518f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3519f300babaSAlexandros Batsakis * 3520f300babaSAlexandros Batsakis */ 3521f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3522f300babaSAlexandros Batsakis { 3523f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3524f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3525f300babaSAlexandros Batsakis struct sock_xprt *transport; 3526f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 35270a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3528f300babaSAlexandros Batsakis 3529d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3530d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3531f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3532f300babaSAlexandros Batsakis return xprt; 3533f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3534f300babaSAlexandros Batsakis 3535f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3536d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_bc_tcp_transport; 3537f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3538f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3539f300babaSAlexandros Batsakis 3540f300babaSAlexandros Batsakis /* backchannel */ 3541f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3542f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3543f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3544f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3545f300babaSAlexandros Batsakis 3546f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3547f300babaSAlexandros Batsakis 3548f300babaSAlexandros Batsakis switch (addr->sa_family) { 3549f300babaSAlexandros Batsakis case AF_INET: 3550f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3551f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3552f300babaSAlexandros Batsakis break; 3553f300babaSAlexandros Batsakis case AF_INET6: 3554f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3555f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3556f300babaSAlexandros Batsakis break; 3557f300babaSAlexandros Batsakis default: 35580a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 35590a68b0beSJ. Bruce Fields goto out_err; 3560f300babaSAlexandros Batsakis } 3561f300babaSAlexandros Batsakis 3562f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3563f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3564f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3565f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3566f300babaSAlexandros Batsakis 3567f300babaSAlexandros Batsakis /* 356899de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 356928303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 357028303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 357128303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 357299de8ea9SJ. Bruce Fields */ 357399de8ea9SJ. Bruce Fields xprt_get(xprt); 357499de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 357599de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 357699de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 357799de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 357899de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 357999de8ea9SJ. Bruce Fields 358099de8ea9SJ. Bruce Fields /* 3581f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3582f300babaSAlexandros Batsakis * the xprt status to connected 3583f300babaSAlexandros Batsakis */ 3584f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3585f300babaSAlexandros Batsakis 3586f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3587f300babaSAlexandros Batsakis return xprt; 3588642aab58SKinglong Mee 3589642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 359039a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 359199de8ea9SJ. Bruce Fields xprt_put(xprt); 35920a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 35930a68b0beSJ. Bruce Fields out_err: 3594315f3812SKinglong Mee xs_xprt_free(xprt); 35950a68b0beSJ. Bruce Fields return ret; 3596f300babaSAlexandros Batsakis } 3597f300babaSAlexandros Batsakis 3598176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3599176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3600176e21eeSChuck Lever .name = "named UNIX socket", 3601176e21eeSChuck Lever .owner = THIS_MODULE, 3602176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3603176e21eeSChuck Lever .setup = xs_setup_local, 3604d5aa6b22STrond Myklebust .netid = { "" }, 3605176e21eeSChuck Lever }; 3606176e21eeSChuck Lever 3607bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3608bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3609bc25571eS\"Talpey, Thomas\ .name = "udp", 3610bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3611f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3612bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3613d5aa6b22STrond Myklebust .netid = { "udp", "udp6", "" }, 3614bc25571eS\"Talpey, Thomas\ }; 3615bc25571eS\"Talpey, Thomas\ 3616bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3617bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3618bc25571eS\"Talpey, Thomas\ .name = "tcp", 3619bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3620f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3621bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3622d5aa6b22STrond Myklebust .netid = { "tcp", "tcp6", "" }, 3623bc25571eS\"Talpey, Thomas\ }; 3624bc25571eS\"Talpey, Thomas\ 362575eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport = { 362675eb6af7SChuck Lever .list = LIST_HEAD_INIT(xs_tcp_tls_transport.list), 362775eb6af7SChuck Lever .name = "tcp-with-tls", 362875eb6af7SChuck Lever .owner = THIS_MODULE, 362975eb6af7SChuck Lever .ident = XPRT_TRANSPORT_TCP_TLS, 363075eb6af7SChuck Lever .setup = xs_setup_tcp_tls, 363175eb6af7SChuck Lever .netid = { "tcp", "tcp6", "" }, 363275eb6af7SChuck Lever }; 363375eb6af7SChuck Lever 3634f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3635f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3636f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3637f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3638f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3639f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3640d5aa6b22STrond Myklebust .netid = { "" }, 3641f300babaSAlexandros Batsakis }; 3642f300babaSAlexandros Batsakis 3643282b32e1SChuck Lever /** 3644bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3645282b32e1SChuck Lever * 3646282b32e1SChuck Lever */ 3647282b32e1SChuck Lever int init_socket_xprt(void) 3648282b32e1SChuck Lever { 36492b1bec5fSEric W. Biederman if (!sunrpc_table_header) 3650c946cb69SLuis Chamberlain sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table); 3651fbf76683SChuck Lever 3652176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3653bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3654bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 365575eb6af7SChuck Lever xprt_register_transport(&xs_tcp_tls_transport); 3656f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3657bc25571eS\"Talpey, Thomas\ 3658282b32e1SChuck Lever return 0; 3659282b32e1SChuck Lever } 3660282b32e1SChuck Lever 3661282b32e1SChuck Lever /** 3662bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3663282b32e1SChuck Lever * 3664282b32e1SChuck Lever */ 3665282b32e1SChuck Lever void cleanup_socket_xprt(void) 3666282b32e1SChuck Lever { 3667fbf76683SChuck Lever if (sunrpc_table_header) { 3668fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3669fbf76683SChuck Lever sunrpc_table_header = NULL; 3670fbf76683SChuck Lever } 3671bc25571eS\"Talpey, Thomas\ 3672176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3673bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3674bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 367575eb6af7SChuck Lever xprt_unregister_transport(&xs_tcp_tls_transport); 3676f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3677282b32e1SChuck Lever } 3678cbf11071STrond Myklebust 36799bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3680cbf11071STrond Myklebust { 3681cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3682cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3683cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3684cbf11071STrond Myklebust } 3685cbf11071STrond Myklebust 36869c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 36879bbb9e5aSRusty Russell .set = param_set_portnr, 36889bbb9e5aSRusty Russell .get = param_get_uint, 36899bbb9e5aSRusty Russell }; 36909bbb9e5aSRusty Russell 3691cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3692cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3693cbf11071STrond Myklebust 3694cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3695cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3696cbf11071STrond Myklebust 36979bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 36989bbb9e5aSRusty Russell const struct kernel_param *kp) 3699cbf11071STrond Myklebust { 3700cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3701cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3702cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3703cbf11071STrond Myklebust } 3704cbf11071STrond Myklebust 37059c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 37069bbb9e5aSRusty Russell .set = param_set_slot_table_size, 37079bbb9e5aSRusty Russell .get = param_get_uint, 37089bbb9e5aSRusty Russell }; 37099bbb9e5aSRusty Russell 3710cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3711cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3712cbf11071STrond Myklebust 3713d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3714d9ba131dSTrond Myklebust const struct kernel_param *kp) 3715d9ba131dSTrond Myklebust { 3716d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3717d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3718d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3719d9ba131dSTrond Myklebust } 3720d9ba131dSTrond Myklebust 37219c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3722d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3723d9ba131dSTrond Myklebust .get = param_get_uint, 3724d9ba131dSTrond Myklebust }; 3725d9ba131dSTrond Myklebust 3726d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3727d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3728d9ba131dSTrond Myklebust 3729cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3730cbf11071STrond Myklebust slot_table_size, 0644); 3731d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3732d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3733cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3734cbf11071STrond Myklebust slot_table_size, 0644); 3735