1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2a246b010SChuck Lever /* 3a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 4a246b010SChuck Lever * 5a246b010SChuck Lever * Client-side transport implementation for sockets. 6a246b010SChuck Lever * 7113aa838SAlan Cox * TCP callback races fixes (C) 1998 Red Hat 8113aa838SAlan Cox * TCP send fixes (C) 1998 Red Hat 9a246b010SChuck Lever * TCP NFS related read + write fixes 10a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 11a246b010SChuck Lever * 12a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 13a246b010SChuck Lever * Fix behaviour when socket buffer is full. 14a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1555aa4f58SChuck Lever * 1655aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 178f9d5b1aSChuck Lever * 188f9d5b1aSChuck Lever * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 198f9d5b1aSChuck Lever * <gilles.quillard@bull.net> 20a246b010SChuck Lever */ 21a246b010SChuck Lever 22a246b010SChuck Lever #include <linux/types.h> 23176e21eeSChuck Lever #include <linux/string.h> 24a246b010SChuck Lever #include <linux/slab.h> 25bc25571eS\"Talpey, Thomas\ #include <linux/module.h> 26a246b010SChuck Lever #include <linux/capability.h> 27a246b010SChuck Lever #include <linux/pagemap.h> 28a246b010SChuck Lever #include <linux/errno.h> 29a246b010SChuck Lever #include <linux/socket.h> 30a246b010SChuck Lever #include <linux/in.h> 31a246b010SChuck Lever #include <linux/net.h> 32a246b010SChuck Lever #include <linux/mm.h> 33176e21eeSChuck Lever #include <linux/un.h> 34a246b010SChuck Lever #include <linux/udp.h> 35a246b010SChuck Lever #include <linux/tcp.h> 36a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 375976687aSJeff Layton #include <linux/sunrpc/addr.h> 3802107148SChuck Lever #include <linux/sunrpc/sched.h> 394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h> 4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 41a246b010SChuck Lever #include <linux/file.h> 429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 4444b98efdSRicardo Labiaga #endif 45a246b010SChuck Lever 46a246b010SChuck Lever #include <net/sock.h> 47a246b010SChuck Lever #include <net/checksum.h> 48a246b010SChuck Lever #include <net/udp.h> 49a246b010SChuck Lever #include <net/tcp.h> 50dea034b9SChuck Lever #include <net/tls.h> 51*6a7eccefSChuck Lever #include <net/tls_prot.h> 5275eb6af7SChuck Lever #include <net/handshake.h> 53dea034b9SChuck Lever 54277e4ab7STrond Myklebust #include <linux/bvec.h> 556a829eb8STrond Myklebust #include <linux/highmem.h> 56277e4ab7STrond Myklebust #include <linux/uio.h> 57a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 58a246b010SChuck Lever 5940e0b090SPeilin Ye #include <trace/events/sock.h> 6040b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 6140b5ea0cSTrond Myklebust 629e55eef4SChuck Lever #include "socklib.h" 634cfc7e60SRahul Iyer #include "sunrpc.h" 64176e21eeSChuck Lever 65176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 663b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock); 677196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 687196dbb0STrond Myklebust struct socket *sock); 69176e21eeSChuck Lever 709903cd1cSChuck Lever /* 71c556b754SChuck Lever * xprtsock tunables 72c556b754SChuck Lever */ 7309acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 7409acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 7509acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 76c556b754SChuck Lever 7709acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 7809acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 79c556b754SChuck Lever 807d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 8125fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 827d1e8255STrond Myklebust 83c556b754SChuck Lever /* 84fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 85c946cb69SLuis Chamberlain * calling register_sysctl() again. The files in that 86fbf76683SChuck Lever * directory become the union of all files registered there. 87fbf76683SChuck Lever * 88fbf76683SChuck Lever * We simply need to make sure that we don't collide with 89fbf76683SChuck Lever * someone else's file names! 90fbf76683SChuck Lever */ 91fbf76683SChuck Lever 92fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 93fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 94d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 95fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 96fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 97fbf76683SChuck Lever 98fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 99fbf76683SChuck Lever 100d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport; 101d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport; 102d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport; 10375eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport; 104d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport; 105d3abc739SOlga Kornievskaia 106fbf76683SChuck Lever /* 107fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 108fbf76683SChuck Lever * socket buffers for existing UDP transports 109fbf76683SChuck Lever */ 110fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 111fbf76683SChuck Lever { 112fbf76683SChuck Lever .procname = "udp_slot_table_entries", 113fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 114fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 115fbf76683SChuck Lever .mode = 0644, 1166d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 117fbf76683SChuck Lever .extra1 = &min_slot_table_size, 118fbf76683SChuck Lever .extra2 = &max_slot_table_size 119fbf76683SChuck Lever }, 120fbf76683SChuck Lever { 121fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 122fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 123fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 124fbf76683SChuck Lever .mode = 0644, 1256d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 126fbf76683SChuck Lever .extra1 = &min_slot_table_size, 127fbf76683SChuck Lever .extra2 = &max_slot_table_size 128fbf76683SChuck Lever }, 129fbf76683SChuck Lever { 130d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 131d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 132d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 133d9ba131dSTrond Myklebust .mode = 0644, 134d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 135d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 136d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 137d9ba131dSTrond Myklebust }, 138d9ba131dSTrond Myklebust { 139fbf76683SChuck Lever .procname = "min_resvport", 140fbf76683SChuck Lever .data = &xprt_min_resvport, 141fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 142fbf76683SChuck Lever .mode = 0644, 1436d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 144fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 145826799e6SJ. Bruce Fields .extra2 = &xprt_max_resvport_limit 146fbf76683SChuck Lever }, 147fbf76683SChuck Lever { 148fbf76683SChuck Lever .procname = "max_resvport", 149fbf76683SChuck Lever .data = &xprt_max_resvport, 150fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 151fbf76683SChuck Lever .mode = 0644, 1526d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 153826799e6SJ. Bruce Fields .extra1 = &xprt_min_resvport_limit, 154fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 155fbf76683SChuck Lever }, 156fbf76683SChuck Lever { 15725fe6142STrond Myklebust .procname = "tcp_fin_timeout", 15825fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 15925fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 16025fe6142STrond Myklebust .mode = 0644, 1616d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 16225fe6142STrond Myklebust }, 163f8572d8fSEric W. Biederman { }, 164fbf76683SChuck Lever }; 165fbf76683SChuck Lever 166fbf76683SChuck Lever /* 16703bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16803bf4b70SChuck Lever */ 16903bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 17003bf4b70SChuck Lever 17103bf4b70SChuck Lever /* 17203bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 17303bf4b70SChuck Lever * kind of resource problem on the local host. 17403bf4b70SChuck Lever */ 17503bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17603bf4b70SChuck Lever 17703bf4b70SChuck Lever /* 17803bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17903bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 18003bf4b70SChuck Lever * 18103bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 18203bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 18303bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 18403bf4b70SChuck Lever * increase over time if the server is down or not responding. 18503bf4b70SChuck Lever */ 18603bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18703bf4b70SChuck Lever 18803bf4b70SChuck Lever /* 18903bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 19003bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 19103bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 19203bf4b70SChuck Lever */ 19303bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 19403bf4b70SChuck Lever 19575eb6af7SChuck Lever /* 19675eb6af7SChuck Lever * TLS handshake timeout. 19775eb6af7SChuck Lever */ 19875eb6af7SChuck Lever #define XS_TLS_HANDSHAKE_TO (10U * HZ) 19975eb6af7SChuck Lever 200f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 201a246b010SChuck Lever # undef RPC_DEBUG_DATA 2029903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 203a246b010SChuck Lever #endif 204a246b010SChuck Lever 205a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 2069903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 207a246b010SChuck Lever { 208a246b010SChuck Lever u8 *buf = (u8 *) packet; 209a246b010SChuck Lever int j; 210a246b010SChuck Lever 211a246b010SChuck Lever dprintk("RPC: %s\n", msg); 212a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 213a246b010SChuck Lever if (!(j & 31)) { 214a246b010SChuck Lever if (j) 215a246b010SChuck Lever dprintk("\n"); 216a246b010SChuck Lever dprintk("0x%04x ", j); 217a246b010SChuck Lever } 218a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 219a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 220a246b010SChuck Lever } 221a246b010SChuck Lever dprintk("\n"); 222a246b010SChuck Lever } 223a246b010SChuck Lever #else 2249903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 225a246b010SChuck Lever { 226a246b010SChuck Lever /* NOP */ 227a246b010SChuck Lever } 228a246b010SChuck Lever #endif 229a246b010SChuck Lever 2302118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2312118071dSTrond Myklebust { 2322118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2332118071dSTrond Myklebust } 2342118071dSTrond Myklebust 23595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 236edb267a6SChuck Lever { 23795392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 23895392c59SChuck Lever } 23995392c59SChuck Lever 240176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 241176e21eeSChuck Lever { 242176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 243176e21eeSChuck Lever } 244176e21eeSChuck Lever 24595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 24695392c59SChuck Lever { 24795392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 24895392c59SChuck Lever } 24995392c59SChuck Lever 25095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 25195392c59SChuck Lever { 25295392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 25395392c59SChuck Lever } 25495392c59SChuck Lever 255c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 256c877b849SChuck Lever { 257c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2589dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2599dc3b095SChuck Lever struct sockaddr_in *sin; 260176e21eeSChuck Lever struct sockaddr_un *sun; 261c877b849SChuck Lever char buf[128]; 262c877b849SChuck Lever 2639dc3b095SChuck Lever switch (sap->sa_family) { 264176e21eeSChuck Lever case AF_LOCAL: 265176e21eeSChuck Lever sun = xs_addr_un(xprt); 2664388ce05SNeilBrown if (sun->sun_path[0]) { 26715bcdc92SWolfram Sang strscpy(buf, sun->sun_path, sizeof(buf)); 2684388ce05SNeilBrown } else { 2694388ce05SNeilBrown buf[0] = '@'; 2704388ce05SNeilBrown strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1); 2714388ce05SNeilBrown } 272176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 273176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 274176e21eeSChuck Lever break; 2759dc3b095SChuck Lever case AF_INET: 276176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 277176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 278176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2799dc3b095SChuck Lever sin = xs_addr_in(xprt); 280fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2819dc3b095SChuck Lever break; 2829dc3b095SChuck Lever case AF_INET6: 283176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 284176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 285176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2869dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 287fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2889dc3b095SChuck Lever break; 2899dc3b095SChuck Lever default: 2909dc3b095SChuck Lever BUG(); 2919dc3b095SChuck Lever } 292176e21eeSChuck Lever 2939dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2949dc3b095SChuck Lever } 2959dc3b095SChuck Lever 2969dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2979dc3b095SChuck Lever { 2989dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2999dc3b095SChuck Lever char buf[128]; 3009dc3b095SChuck Lever 30181160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 302c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 303c877b849SChuck Lever 30481160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 305c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 306c877b849SChuck Lever } 307c877b849SChuck Lever 3089dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 309b454ae90SChuck Lever const char *protocol, 310b454ae90SChuck Lever const char *netid) 311edb267a6SChuck Lever { 312b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 313b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 314c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3159dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 316edb267a6SChuck Lever } 317edb267a6SChuck Lever 3189dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3194b6473fbSChuck Lever { 3209dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3219dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3224b6473fbSChuck Lever 3239dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 324edb267a6SChuck Lever } 325edb267a6SChuck Lever 326edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 327edb267a6SChuck Lever { 32833e01dc7SChuck Lever unsigned int i; 32933e01dc7SChuck Lever 33033e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 33133e01dc7SChuck Lever switch (i) { 33233e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 33333e01dc7SChuck Lever case RPC_DISPLAY_NETID: 33433e01dc7SChuck Lever continue; 33533e01dc7SChuck Lever default: 33633e01dc7SChuck Lever kfree(xprt->address_strings[i]); 33733e01dc7SChuck Lever } 338edb267a6SChuck Lever } 339edb267a6SChuck Lever 340277e4ab7STrond Myklebust static size_t 341277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 342277e4ab7STrond Myklebust { 343277e4ab7STrond Myklebust size_t i,n; 344277e4ab7STrond Myklebust 34516e5e90fSTrond Myklebust if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 346277e4ab7STrond Myklebust return want; 347277e4ab7STrond Myklebust n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 348277e4ab7STrond Myklebust for (i = 0; i < n; i++) { 349277e4ab7STrond Myklebust if (buf->pages[i]) 350277e4ab7STrond Myklebust continue; 351277e4ab7STrond Myklebust buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 352277e4ab7STrond Myklebust if (!buf->pages[i]) { 35316e5e90fSTrond Myklebust i *= PAGE_SIZE; 35416e5e90fSTrond Myklebust return i > buf->page_base ? i - buf->page_base : 0; 355277e4ab7STrond Myklebust } 356277e4ab7STrond Myklebust } 357277e4ab7STrond Myklebust return want; 358277e4ab7STrond Myklebust } 359277e4ab7STrond Myklebust 360dea034b9SChuck Lever static int 361dea034b9SChuck Lever xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg, 362dea034b9SChuck Lever struct cmsghdr *cmsg, int ret) 363dea034b9SChuck Lever { 364dea034b9SChuck Lever if (cmsg->cmsg_level == SOL_TLS && 365dea034b9SChuck Lever cmsg->cmsg_type == TLS_GET_RECORD_TYPE) { 366dea034b9SChuck Lever u8 content_type = *((u8 *)CMSG_DATA(cmsg)); 367dea034b9SChuck Lever 368dea034b9SChuck Lever switch (content_type) { 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: 377dea034b9SChuck Lever ret = -ENOTCONN; 378dea034b9SChuck Lever break; 379dea034b9SChuck Lever default: 380dea034b9SChuck Lever ret = -EAGAIN; 381dea034b9SChuck Lever } 382dea034b9SChuck Lever } 383dea034b9SChuck Lever return ret; 384dea034b9SChuck Lever } 385dea034b9SChuck Lever 386dea034b9SChuck Lever static int 387dea034b9SChuck Lever xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags) 388dea034b9SChuck Lever { 389dea034b9SChuck Lever union { 390dea034b9SChuck Lever struct cmsghdr cmsg; 391dea034b9SChuck Lever u8 buf[CMSG_SPACE(sizeof(u8))]; 392dea034b9SChuck Lever } u; 393dea034b9SChuck Lever int ret; 394dea034b9SChuck Lever 395dea034b9SChuck Lever msg->msg_control = &u; 396dea034b9SChuck Lever msg->msg_controllen = sizeof(u); 397dea034b9SChuck Lever ret = sock_recvmsg(sock, msg, flags); 398dea034b9SChuck Lever if (msg->msg_controllen != sizeof(u)) 399dea034b9SChuck Lever ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret); 400dea034b9SChuck Lever return ret; 401dea034b9SChuck Lever } 402dea034b9SChuck Lever 403277e4ab7STrond Myklebust static ssize_t 404277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 405277e4ab7STrond Myklebust { 406277e4ab7STrond Myklebust ssize_t ret; 407277e4ab7STrond Myklebust if (seek != 0) 408277e4ab7STrond Myklebust iov_iter_advance(&msg->msg_iter, seek); 409dea034b9SChuck Lever ret = xs_sock_recv_cmsg(sock, msg, flags); 410277e4ab7STrond Myklebust return ret > 0 ? ret + seek : ret; 411277e4ab7STrond Myklebust } 412277e4ab7STrond Myklebust 413277e4ab7STrond Myklebust static ssize_t 414277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 415277e4ab7STrond Myklebust struct kvec *kvec, size_t count, size_t seek) 416277e4ab7STrond Myklebust { 417de4eda9dSAl Viro iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count); 418277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 419277e4ab7STrond Myklebust } 420277e4ab7STrond Myklebust 421277e4ab7STrond Myklebust static ssize_t 422277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 423277e4ab7STrond Myklebust struct bio_vec *bvec, unsigned long nr, size_t count, 424277e4ab7STrond Myklebust size_t seek) 425277e4ab7STrond Myklebust { 426de4eda9dSAl Viro iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count); 427277e4ab7STrond Myklebust return xs_sock_recvmsg(sock, msg, flags, seek); 428277e4ab7STrond Myklebust } 429277e4ab7STrond Myklebust 430277e4ab7STrond Myklebust static ssize_t 431277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 432277e4ab7STrond Myklebust size_t count) 433277e4ab7STrond Myklebust { 434de4eda9dSAl Viro iov_iter_discard(&msg->msg_iter, ITER_DEST, count); 435dea034b9SChuck Lever return xs_sock_recv_cmsg(sock, msg, flags); 436277e4ab7STrond Myklebust } 437277e4ab7STrond Myklebust 4386a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 4396a829eb8STrond Myklebust static void 4406a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 4416a829eb8STrond Myklebust { 4426a829eb8STrond Myklebust struct bvec_iter bi = { 4436a829eb8STrond Myklebust .bi_size = count, 4446a829eb8STrond Myklebust }; 4456a829eb8STrond Myklebust struct bio_vec bv; 4466a829eb8STrond Myklebust 4476a829eb8STrond Myklebust bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 4486a829eb8STrond Myklebust for_each_bvec(bv, bvec, bi, bi) 4496a829eb8STrond Myklebust flush_dcache_page(bv.bv_page); 4506a829eb8STrond Myklebust } 4516a829eb8STrond Myklebust #else 4526a829eb8STrond Myklebust static inline void 4536a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 4546a829eb8STrond Myklebust { 4556a829eb8STrond Myklebust } 4566a829eb8STrond Myklebust #endif 4576a829eb8STrond Myklebust 458277e4ab7STrond Myklebust static ssize_t 459277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 460277e4ab7STrond Myklebust struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 461277e4ab7STrond Myklebust { 462277e4ab7STrond Myklebust size_t want, seek_init = seek, offset = 0; 463277e4ab7STrond Myklebust ssize_t ret; 464277e4ab7STrond Myklebust 465277e4ab7STrond Myklebust want = min_t(size_t, count, buf->head[0].iov_len); 466e92053a5STrond Myklebust if (seek < want) { 467277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 468277e4ab7STrond Myklebust if (ret <= 0) 469277e4ab7STrond Myklebust goto sock_err; 470277e4ab7STrond Myklebust offset += ret; 471277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 472277e4ab7STrond Myklebust goto out; 473277e4ab7STrond Myklebust if (ret != want) 474c4433055STrond Myklebust goto out; 475277e4ab7STrond Myklebust seek = 0; 476277e4ab7STrond Myklebust } else { 477e92053a5STrond Myklebust seek -= want; 478e92053a5STrond Myklebust offset += want; 479277e4ab7STrond Myklebust } 48016e5e90fSTrond Myklebust 481b2648015STrond Myklebust want = xs_alloc_sparse_pages( 482b2648015STrond Myklebust buf, min_t(size_t, count - offset, buf->page_len), 483b2648015STrond Myklebust GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 48416e5e90fSTrond Myklebust if (seek < want) { 485277e4ab7STrond Myklebust ret = xs_read_bvec(sock, msg, flags, buf->bvec, 486277e4ab7STrond Myklebust xdr_buf_pagecount(buf), 487277e4ab7STrond Myklebust want + buf->page_base, 488277e4ab7STrond Myklebust seek + buf->page_base); 489277e4ab7STrond Myklebust if (ret <= 0) 490277e4ab7STrond Myklebust goto sock_err; 4916a829eb8STrond Myklebust xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 492ac9645c8SDan Aloni ret -= buf->page_base; 493ac9645c8SDan Aloni offset += ret; 494277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 495277e4ab7STrond Myklebust goto out; 496277e4ab7STrond Myklebust if (ret != want) 497c4433055STrond Myklebust goto out; 498277e4ab7STrond Myklebust seek = 0; 499277e4ab7STrond Myklebust } else { 50016e5e90fSTrond Myklebust seek -= want; 50116e5e90fSTrond Myklebust offset += want; 502277e4ab7STrond Myklebust } 50316e5e90fSTrond Myklebust 504277e4ab7STrond Myklebust want = min_t(size_t, count - offset, buf->tail[0].iov_len); 505e92053a5STrond Myklebust if (seek < want) { 506277e4ab7STrond Myklebust ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 507277e4ab7STrond Myklebust if (ret <= 0) 508277e4ab7STrond Myklebust goto sock_err; 509277e4ab7STrond Myklebust offset += ret; 510277e4ab7STrond Myklebust if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 511277e4ab7STrond Myklebust goto out; 512277e4ab7STrond Myklebust if (ret != want) 513c4433055STrond Myklebust goto out; 5149734ad57STrond Myklebust } else if (offset < seek_init) 515e92053a5STrond Myklebust offset = seek_init; 516277e4ab7STrond Myklebust ret = -EMSGSIZE; 517277e4ab7STrond Myklebust out: 518277e4ab7STrond Myklebust *read = offset - seek_init; 519277e4ab7STrond Myklebust return ret; 520277e4ab7STrond Myklebust sock_err: 521277e4ab7STrond Myklebust offset += seek; 522277e4ab7STrond Myklebust goto out; 523277e4ab7STrond Myklebust } 524277e4ab7STrond Myklebust 525277e4ab7STrond Myklebust static void 526277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 527277e4ab7STrond Myklebust { 528277e4ab7STrond Myklebust if (!transport->recv.copied) { 529277e4ab7STrond Myklebust if (buf->head[0].iov_len >= transport->recv.offset) 530277e4ab7STrond Myklebust memcpy(buf->head[0].iov_base, 531277e4ab7STrond Myklebust &transport->recv.xid, 532277e4ab7STrond Myklebust transport->recv.offset); 533277e4ab7STrond Myklebust transport->recv.copied = transport->recv.offset; 534277e4ab7STrond Myklebust } 535277e4ab7STrond Myklebust } 536277e4ab7STrond Myklebust 537277e4ab7STrond Myklebust static bool 538277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport) 539277e4ab7STrond Myklebust { 540277e4ab7STrond Myklebust return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 541277e4ab7STrond Myklebust } 542277e4ab7STrond Myklebust 543e92053a5STrond Myklebust static void 544e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport, 545e92053a5STrond Myklebust struct msghdr *msg) 546e92053a5STrond Myklebust { 547e92053a5STrond Myklebust if (xs_read_stream_request_done(transport)) 548e92053a5STrond Myklebust msg->msg_flags |= MSG_EOR; 549e92053a5STrond Myklebust } 550e92053a5STrond Myklebust 551277e4ab7STrond Myklebust static ssize_t 552277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 553277e4ab7STrond Myklebust int flags, struct rpc_rqst *req) 554277e4ab7STrond Myklebust { 555277e4ab7STrond Myklebust struct xdr_buf *buf = &req->rq_private_buf; 5563f649ab7SKees Cook size_t want, read; 5573f649ab7SKees Cook ssize_t ret; 558277e4ab7STrond Myklebust 559277e4ab7STrond Myklebust xs_read_header(transport, buf); 560277e4ab7STrond Myklebust 561277e4ab7STrond Myklebust want = transport->recv.len - transport->recv.offset; 562e92053a5STrond Myklebust if (want != 0) { 563277e4ab7STrond Myklebust ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 564e92053a5STrond Myklebust transport->recv.copied + want, 565e92053a5STrond Myklebust transport->recv.copied, 566277e4ab7STrond Myklebust &read); 567277e4ab7STrond Myklebust transport->recv.offset += read; 568277e4ab7STrond Myklebust transport->recv.copied += read; 569277e4ab7STrond Myklebust } 570277e4ab7STrond Myklebust 571727fcc64STrond Myklebust if (transport->recv.offset == transport->recv.len) 572727fcc64STrond Myklebust xs_read_stream_check_eor(transport, msg); 573727fcc64STrond Myklebust 574e92053a5STrond Myklebust if (want == 0) 575e92053a5STrond Myklebust return 0; 576e92053a5STrond Myklebust 577277e4ab7STrond Myklebust switch (ret) { 578c4433055STrond Myklebust default: 579c4433055STrond Myklebust break; 58026781eabSTrond Myklebust case -EFAULT: 581277e4ab7STrond Myklebust case -EMSGSIZE: 58226781eabSTrond Myklebust msg->msg_flags |= MSG_TRUNC; 583c4433055STrond Myklebust return read; 584277e4ab7STrond Myklebust case 0: 585277e4ab7STrond Myklebust return -ESHUTDOWN; 586277e4ab7STrond Myklebust } 587c4433055STrond Myklebust return ret < 0 ? ret : read; 588277e4ab7STrond Myklebust } 589277e4ab7STrond Myklebust 590277e4ab7STrond Myklebust static size_t 591277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag) 592277e4ab7STrond Myklebust { 593277e4ab7STrond Myklebust if (isfrag) 594277e4ab7STrond Myklebust return sizeof(__be32); 595277e4ab7STrond Myklebust return 3 * sizeof(__be32); 596277e4ab7STrond Myklebust } 597277e4ab7STrond Myklebust 598277e4ab7STrond Myklebust static ssize_t 599277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 600277e4ab7STrond Myklebust int flags, size_t want, size_t seek) 601277e4ab7STrond Myklebust { 602277e4ab7STrond Myklebust struct kvec kvec = { 603277e4ab7STrond Myklebust .iov_base = &transport->recv.fraghdr, 604277e4ab7STrond Myklebust .iov_len = want, 605277e4ab7STrond Myklebust }; 606277e4ab7STrond Myklebust return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 607277e4ab7STrond Myklebust } 608277e4ab7STrond Myklebust 609277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 610277e4ab7STrond Myklebust static ssize_t 611277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 612277e4ab7STrond Myklebust { 613277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 614277e4ab7STrond Myklebust struct rpc_rqst *req; 615277e4ab7STrond Myklebust ssize_t ret; 616277e4ab7STrond Myklebust 61798b5cee3SBenjamin Coddington /* Is this transport associated with the backchannel? */ 61898b5cee3SBenjamin Coddington if (!xprt->bc_serv) 61998b5cee3SBenjamin Coddington return -ESHUTDOWN; 62098b5cee3SBenjamin Coddington 621277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 622277e4ab7STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->recv.xid); 623277e4ab7STrond Myklebust if (!req) { 624277e4ab7STrond Myklebust printk(KERN_WARNING "Callback slot table overflowed\n"); 625277e4ab7STrond Myklebust return -ESHUTDOWN; 626277e4ab7STrond Myklebust } 62745835a63STrond Myklebust if (transport->recv.copied && !req->rq_private_buf.len) 62845835a63STrond Myklebust return -ESHUTDOWN; 629277e4ab7STrond Myklebust 630277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 631277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 632c4433055STrond Myklebust xprt_complete_bc_request(req, transport->recv.copied); 63345835a63STrond Myklebust else 63445835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 635277e4ab7STrond Myklebust 636277e4ab7STrond Myklebust return ret; 637277e4ab7STrond Myklebust } 638277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */ 639277e4ab7STrond Myklebust static ssize_t 640277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 641277e4ab7STrond Myklebust { 642277e4ab7STrond Myklebust return -ESHUTDOWN; 643277e4ab7STrond Myklebust } 644277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 645277e4ab7STrond Myklebust 646277e4ab7STrond Myklebust static ssize_t 647277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 648277e4ab7STrond Myklebust { 649277e4ab7STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 650277e4ab7STrond Myklebust struct rpc_rqst *req; 651277e4ab7STrond Myklebust ssize_t ret = 0; 652277e4ab7STrond Myklebust 653277e4ab7STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 654277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 655277e4ab7STrond Myklebust req = xprt_lookup_rqst(xprt, transport->recv.xid); 65645835a63STrond Myklebust if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { 657277e4ab7STrond Myklebust msg->msg_flags |= MSG_TRUNC; 658277e4ab7STrond Myklebust goto out; 659277e4ab7STrond Myklebust } 660277e4ab7STrond Myklebust xprt_pin_rqst(req); 661277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 662277e4ab7STrond Myklebust 663277e4ab7STrond Myklebust ret = xs_read_stream_request(transport, msg, flags, req); 664277e4ab7STrond Myklebust 665277e4ab7STrond Myklebust spin_lock(&xprt->queue_lock); 666277e4ab7STrond Myklebust if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 667c4433055STrond Myklebust xprt_complete_rqst(req->rq_task, transport->recv.copied); 66845835a63STrond Myklebust else 66945835a63STrond Myklebust req->rq_private_buf.len = transport->recv.copied; 670277e4ab7STrond Myklebust xprt_unpin_rqst(req); 671277e4ab7STrond Myklebust out: 672277e4ab7STrond Myklebust spin_unlock(&xprt->queue_lock); 673277e4ab7STrond Myklebust return ret; 674277e4ab7STrond Myklebust } 675277e4ab7STrond Myklebust 676277e4ab7STrond Myklebust static ssize_t 677277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags) 678277e4ab7STrond Myklebust { 679277e4ab7STrond Myklebust struct msghdr msg = { 0 }; 680277e4ab7STrond Myklebust size_t want, read = 0; 681277e4ab7STrond Myklebust ssize_t ret = 0; 682277e4ab7STrond Myklebust 683277e4ab7STrond Myklebust if (transport->recv.len == 0) { 684277e4ab7STrond Myklebust want = xs_read_stream_headersize(transport->recv.copied != 0); 685277e4ab7STrond Myklebust ret = xs_read_stream_header(transport, &msg, flags, want, 686277e4ab7STrond Myklebust transport->recv.offset); 687277e4ab7STrond Myklebust if (ret <= 0) 688277e4ab7STrond Myklebust goto out_err; 689277e4ab7STrond Myklebust transport->recv.offset = ret; 690c4433055STrond Myklebust if (transport->recv.offset != want) 691c4433055STrond Myklebust return transport->recv.offset; 692277e4ab7STrond Myklebust transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 693277e4ab7STrond Myklebust RPC_FRAGMENT_SIZE_MASK; 694277e4ab7STrond Myklebust transport->recv.offset -= sizeof(transport->recv.fraghdr); 695277e4ab7STrond Myklebust read = ret; 696277e4ab7STrond Myklebust } 697277e4ab7STrond Myklebust 698277e4ab7STrond Myklebust switch (be32_to_cpu(transport->recv.calldir)) { 699c4433055STrond Myklebust default: 700c4433055STrond Myklebust msg.msg_flags |= MSG_TRUNC; 701c4433055STrond Myklebust break; 702277e4ab7STrond Myklebust case RPC_CALL: 703277e4ab7STrond Myklebust ret = xs_read_stream_call(transport, &msg, flags); 704277e4ab7STrond Myklebust break; 705277e4ab7STrond Myklebust case RPC_REPLY: 706277e4ab7STrond Myklebust ret = xs_read_stream_reply(transport, &msg, flags); 707277e4ab7STrond Myklebust } 708277e4ab7STrond Myklebust if (msg.msg_flags & MSG_TRUNC) { 709277e4ab7STrond Myklebust transport->recv.calldir = cpu_to_be32(-1); 710277e4ab7STrond Myklebust transport->recv.copied = -1; 711277e4ab7STrond Myklebust } 712277e4ab7STrond Myklebust if (ret < 0) 713277e4ab7STrond Myklebust goto out_err; 714277e4ab7STrond Myklebust read += ret; 715277e4ab7STrond Myklebust if (transport->recv.offset < transport->recv.len) { 716c4433055STrond Myklebust if (!(msg.msg_flags & MSG_TRUNC)) 717c4433055STrond Myklebust return read; 718b76a5afdSTrond Myklebust msg.msg_flags = 0; 719277e4ab7STrond Myklebust ret = xs_read_discard(transport->sock, &msg, flags, 720277e4ab7STrond Myklebust transport->recv.len - transport->recv.offset); 721277e4ab7STrond Myklebust if (ret <= 0) 722277e4ab7STrond Myklebust goto out_err; 723277e4ab7STrond Myklebust transport->recv.offset += ret; 724277e4ab7STrond Myklebust read += ret; 725277e4ab7STrond Myklebust if (transport->recv.offset != transport->recv.len) 726c4433055STrond Myklebust return read; 727277e4ab7STrond Myklebust } 728277e4ab7STrond Myklebust if (xs_read_stream_request_done(transport)) { 729c50b8ee0STrond Myklebust trace_xs_stream_read_request(transport); 730277e4ab7STrond Myklebust transport->recv.copied = 0; 731277e4ab7STrond Myklebust } 732277e4ab7STrond Myklebust transport->recv.offset = 0; 733277e4ab7STrond Myklebust transport->recv.len = 0; 734277e4ab7STrond Myklebust return read; 735277e4ab7STrond Myklebust out_err: 73679462857STrond Myklebust return ret != 0 ? ret : -ESHUTDOWN; 737277e4ab7STrond Myklebust } 738277e4ab7STrond Myklebust 7390ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport) 7400ffe86f4STrond Myklebust { 741a73881c9STrond Myklebust return transport->sock->ops->poll(transport->file, transport->sock, 742a73881c9STrond Myklebust NULL); 7430ffe86f4STrond Myklebust } 7440ffe86f4STrond Myklebust 7450ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport) 7460ffe86f4STrond Myklebust { 7470ffe86f4STrond Myklebust __poll_t events = xs_poll_socket(transport); 7480ffe86f4STrond Myklebust 7490ffe86f4STrond Myklebust return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 7500ffe86f4STrond Myklebust } 7510ffe86f4STrond Myklebust 7520ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport) 7530ffe86f4STrond Myklebust { 7540ffe86f4STrond Myklebust 7550ffe86f4STrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 7560d3ca07fSChuck Lever if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 7570d3ca07fSChuck Lever return; 7580ffe86f4STrond Myklebust if (!xs_poll_socket_readable(transport)) 7590ffe86f4STrond Myklebust return; 7600ffe86f4STrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 7610ffe86f4STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 7620ffe86f4STrond Myklebust } 7630ffe86f4STrond Myklebust 764c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport) 765c50b8ee0STrond Myklebust { 766c50b8ee0STrond Myklebust size_t read = 0; 767c50b8ee0STrond Myklebust ssize_t ret = 0; 768c50b8ee0STrond Myklebust 769c50b8ee0STrond Myklebust mutex_lock(&transport->recv_mutex); 770c50b8ee0STrond Myklebust if (transport->sock == NULL) 771c50b8ee0STrond Myklebust goto out; 772c50b8ee0STrond Myklebust for (;;) { 773c50b8ee0STrond Myklebust ret = xs_read_stream(transport, MSG_DONTWAIT); 774c4433055STrond Myklebust if (ret < 0) 775c50b8ee0STrond Myklebust break; 776c50b8ee0STrond Myklebust read += ret; 777c50b8ee0STrond Myklebust cond_resched(); 778c50b8ee0STrond Myklebust } 7795f52a9d4STrond Myklebust if (ret == -ESHUTDOWN) 7805f52a9d4STrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 7815f52a9d4STrond Myklebust else 7820ffe86f4STrond Myklebust xs_poll_check_readable(transport); 783c50b8ee0STrond Myklebust out: 784c50b8ee0STrond Myklebust mutex_unlock(&transport->recv_mutex); 785c50b8ee0STrond Myklebust trace_xs_stream_read_data(&transport->xprt, ret, read); 786c50b8ee0STrond Myklebust } 787c50b8ee0STrond Myklebust 788c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work) 789c50b8ee0STrond Myklebust { 790c50b8ee0STrond Myklebust struct sock_xprt *transport = 791c50b8ee0STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 792a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 793a1231fdaSTrond Myklebust 794c50b8ee0STrond Myklebust xs_stream_data_receive(transport); 795a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 796c50b8ee0STrond Myklebust } 797c50b8ee0STrond Myklebust 798550aebfeSTrond Myklebust static void 799550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport) 800550aebfeSTrond Myklebust { 801550aebfeSTrond Myklebust transport->recv.offset = 0; 802550aebfeSTrond Myklebust transport->recv.len = 0; 803550aebfeSTrond Myklebust transport->recv.copied = 0; 804550aebfeSTrond Myklebust transport->xmit.offset = 0; 805ae053551STrond Myklebust } 806ae053551STrond Myklebust 807ae053551STrond Myklebust static void 808ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport) 809ae053551STrond Myklebust { 810550aebfeSTrond Myklebust transport->xprt.stat.connect_count++; 811550aebfeSTrond Myklebust transport->xprt.stat.connect_start = jiffies; 812550aebfeSTrond Myklebust } 813550aebfeSTrond Myklebust 814b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 815b4b5cc85SChuck Lever 8169903cd1cSChuck Lever /** 817c544577dSTrond Myklebust * xs_nospace - handle transmit was incomplete 81850f484e2STrond Myklebust * @req: pointer to RPC request 8197496b59fSTrond Myklebust * @transport: pointer to struct sock_xprt 8209903cd1cSChuck Lever * 821a246b010SChuck Lever */ 8227496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport) 823a246b010SChuck Lever { 8247496b59fSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 82506ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 82624ca9a84STrond Myklebust int ret = -EAGAIN; 827a246b010SChuck Lever 828015747d2SChuck Lever trace_rpc_socket_nospace(req, transport); 829a246b010SChuck Lever 830262965f5SChuck Lever /* Protect against races with write_space */ 831b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 832a246b010SChuck Lever 833262965f5SChuck Lever /* Don't race with disconnect */ 834b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 8357496b59fSTrond Myklebust /* wait for more buffer space */ 8362790a624STrond Myklebust set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 8377496b59fSTrond Myklebust set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 8387496b59fSTrond Myklebust sk->sk_write_pending++; 8397496b59fSTrond Myklebust xprt_wait_for_buffer_space(xprt); 8407496b59fSTrond Myklebust } else 8417496b59fSTrond Myklebust ret = -ENOTCONN; 8427496b59fSTrond Myklebust 8437496b59fSTrond Myklebust spin_unlock(&xprt->transport_lock); 8447496b59fSTrond Myklebust return ret; 845d48f9ce7SDavid Vrabel } 8467496b59fSTrond Myklebust 8477496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req) 8487496b59fSTrond Myklebust { 8497496b59fSTrond Myklebust struct sock_xprt *transport = 8507496b59fSTrond Myklebust container_of(req->rq_xprt, struct sock_xprt, xprt); 8517496b59fSTrond Myklebust struct sock *sk = transport->inet; 8527496b59fSTrond Myklebust int ret = -EAGAIN; 8537496b59fSTrond Myklebust 8547496b59fSTrond Myklebust lock_sock(sk); 8557496b59fSTrond Myklebust if (!sock_writeable(sk)) 8567496b59fSTrond Myklebust ret = xs_nospace(req, transport); 8577496b59fSTrond Myklebust release_sock(sk); 8587496b59fSTrond Myklebust return ret; 8597496b59fSTrond Myklebust } 8607496b59fSTrond Myklebust 861d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait) 8627496b59fSTrond Myklebust { 8637496b59fSTrond Myklebust struct sock_xprt *transport = 8647496b59fSTrond Myklebust container_of(req->rq_xprt, struct sock_xprt, xprt); 8657496b59fSTrond Myklebust struct sock *sk = transport->inet; 8667496b59fSTrond Myklebust int ret = -EAGAIN; 8677496b59fSTrond Myklebust 868d0afde5fSTrond Myklebust if (vm_wait) 869d0afde5fSTrond Myklebust return -ENOBUFS; 8707496b59fSTrond Myklebust lock_sock(sk); 8717496b59fSTrond Myklebust if (!sk_stream_memory_free(sk)) 8727496b59fSTrond Myklebust ret = xs_nospace(req, transport); 8737496b59fSTrond Myklebust release_sock(sk); 8745e3771ceSTrond Myklebust return ret; 875a246b010SChuck Lever } 876a246b010SChuck Lever 87772691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf) 878277e4ab7STrond Myklebust { 87972691a26STrond Myklebust return xdr_alloc_bvec(buf, rpc_task_gfp_mask()); 880277e4ab7STrond Myklebust } 881277e4ab7STrond Myklebust 88261677eeeSChuck Lever /* 8834cd34e7cSTrond Myklebust * Determine if the previous message in the stream was aborted before it 8844cd34e7cSTrond Myklebust * could complete transmission. 8854cd34e7cSTrond Myklebust */ 8864cd34e7cSTrond Myklebust static bool 8874cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 8884cd34e7cSTrond Myklebust { 8894cd34e7cSTrond Myklebust return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 8904cd34e7cSTrond Myklebust } 8914cd34e7cSTrond Myklebust 8924cd34e7cSTrond Myklebust /* 89306b5fc3aSTrond Myklebust * Return the stream record marker field for a record of length < 2^31-1 89461677eeeSChuck Lever */ 89506b5fc3aSTrond Myklebust static rpc_fraghdr 89606b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr) 89761677eeeSChuck Lever { 89806b5fc3aSTrond Myklebust if (!xdr->len) 89906b5fc3aSTrond Myklebust return 0; 90006b5fc3aSTrond Myklebust return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 90161677eeeSChuck Lever } 90261677eeeSChuck Lever 9039903cd1cSChuck Lever /** 904176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 90550f484e2STrond Myklebust * @req: pointer to RPC request 906176e21eeSChuck Lever * 907176e21eeSChuck Lever * Return values: 908176e21eeSChuck Lever * 0: The request has been sent 909176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 910176e21eeSChuck Lever * complete the request 911176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 91212b20ce3SBhaskar Chowdhury * other: Some other error occurred, the request was not sent 913176e21eeSChuck Lever */ 914adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req) 915176e21eeSChuck Lever { 916176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 917176e21eeSChuck Lever struct sock_xprt *transport = 918176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 919176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9207e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 9217e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 9229e55eef4SChuck Lever struct msghdr msg = { 9239e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9249e55eef4SChuck Lever }; 925d0afde5fSTrond Myklebust bool vm_wait; 9263f649ab7SKees Cook unsigned int sent; 927176e21eeSChuck Lever int status; 928176e21eeSChuck Lever 9294cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 9304cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 931f0043206STrond Myklebust xprt_force_disconnect(xprt); 9324cd34e7cSTrond Myklebust return -ENOTCONN; 9334cd34e7cSTrond Myklebust } 9344cd34e7cSTrond Myklebust 935176e21eeSChuck Lever xs_pktdump("packet data:", 936176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 937176e21eeSChuck Lever 938d0afde5fSTrond Myklebust vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 939d0afde5fSTrond Myklebust 94078215759SChuck Lever req->rq_xtime = ktime_get(); 9419e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 9427e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 943176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 9446c7a64e5STrond Myklebust __func__, xdr->len - transport->xmit.offset, status); 945743c69e7SNeilBrown 946f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 9476c7a64e5STrond Myklebust transport->xmit.offset += sent; 9486c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 9497e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 9506c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 9516c7a64e5STrond Myklebust transport->xmit.offset = 0; 952176e21eeSChuck Lever return 0; 953176e21eeSChuck Lever } 954176e21eeSChuck Lever status = -EAGAIN; 955d0afde5fSTrond Myklebust vm_wait = false; 956176e21eeSChuck Lever } 957176e21eeSChuck Lever 958176e21eeSChuck Lever switch (status) { 959176e21eeSChuck Lever case -EAGAIN: 960d0afde5fSTrond Myklebust status = xs_stream_nospace(req, vm_wait); 961176e21eeSChuck Lever break; 962176e21eeSChuck Lever default: 963176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 964176e21eeSChuck Lever -status); 965df561f66SGustavo A. R. Silva fallthrough; 966176e21eeSChuck Lever case -EPIPE: 967f0043206STrond Myklebust xprt_force_disconnect(xprt); 968176e21eeSChuck Lever status = -ENOTCONN; 969176e21eeSChuck Lever } 970176e21eeSChuck Lever 971176e21eeSChuck Lever return status; 972176e21eeSChuck Lever } 973176e21eeSChuck Lever 974176e21eeSChuck Lever /** 975262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 97650f484e2STrond Myklebust * @req: pointer to RPC request 9779903cd1cSChuck Lever * 9789903cd1cSChuck Lever * Return values: 9799903cd1cSChuck Lever * 0: The request has been sent 9809903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 9819903cd1cSChuck Lever * complete the request 982262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 98325985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 9849903cd1cSChuck Lever */ 985adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req) 986a246b010SChuck Lever { 987a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 988ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 989262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 9909e55eef4SChuck Lever struct msghdr msg = { 9919e55eef4SChuck Lever .msg_name = xs_addr(xprt), 9929e55eef4SChuck Lever .msg_namelen = xprt->addrlen, 9939e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 9949e55eef4SChuck Lever }; 9953f649ab7SKees Cook unsigned int sent; 996262965f5SChuck Lever int status; 997262965f5SChuck Lever 998262965f5SChuck Lever xs_pktdump("packet data:", 999262965f5SChuck Lever req->rq_svec->iov_base, 1000262965f5SChuck Lever req->rq_svec->iov_len); 1001262965f5SChuck Lever 100201d37c42STrond Myklebust if (!xprt_bound(xprt)) 100301d37c42STrond Myklebust return -ENOTCONN; 100475891f50STrond Myklebust 100575891f50STrond Myklebust if (!xprt_request_get_cong(xprt, req)) 100675891f50STrond Myklebust return -EBADSLT; 100775891f50STrond Myklebust 1008ff053dbbSTrond Myklebust status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 1009ff053dbbSTrond Myklebust if (status < 0) 1010ff053dbbSTrond Myklebust return status; 101178215759SChuck Lever req->rq_xtime = ktime_get(); 10129e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent); 1013262965f5SChuck Lever 1014262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 10156c7a64e5STrond Myklebust xdr->len, status); 1016262965f5SChuck Lever 10173dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 10183dedbb5cSJason Baron if (status == -EPERM) 10193dedbb5cSJason Baron goto process_status; 10203dedbb5cSJason Baron 1021743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 1022743c69e7SNeilBrown status = -ENOBUFS; 1023743c69e7SNeilBrown 1024f279cd00SJason Baron if (sent > 0 || status == 0) { 1025f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 1026f279cd00SJason Baron if (sent >= req->rq_slen) 1027262965f5SChuck Lever return 0; 1028262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 1029262965f5SChuck Lever status = -EAGAIN; 10302199700fSTrond Myklebust } 1031262965f5SChuck Lever 10323dedbb5cSJason Baron process_status: 1033262965f5SChuck Lever switch (status) { 1034fba91afbSTrond Myklebust case -ENOTSOCK: 1035fba91afbSTrond Myklebust status = -ENOTCONN; 1036fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1037fba91afbSTrond Myklebust break; 1038b6ddf64fSTrond Myklebust case -EAGAIN: 10397496b59fSTrond Myklebust status = xs_sock_nospace(req); 1040b6ddf64fSTrond Myklebust break; 1041262965f5SChuck Lever case -ENETUNREACH: 10423601c4a9STrond Myklebust case -ENOBUFS: 1043262965f5SChuck Lever case -EPIPE: 1044262965f5SChuck Lever case -ECONNREFUSED: 10453dedbb5cSJason Baron case -EPERM: 1046262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 1047262965f5SChuck Lever * prompts ECONNREFUSED. */ 104813331a55STrond Myklebust break; 104913331a55STrond Myklebust default: 105013331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 105113331a55STrond Myklebust -status); 1052262965f5SChuck Lever } 10535fe46e9dSBian Naimeng 1054262965f5SChuck Lever return status; 1055262965f5SChuck Lever } 1056262965f5SChuck Lever 1057e06799f9STrond Myklebust /** 1058262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 105950f484e2STrond Myklebust * @req: pointer to RPC request 1060262965f5SChuck Lever * 1061262965f5SChuck Lever * Return values: 1062262965f5SChuck Lever * 0: The request has been sent 1063262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 1064262965f5SChuck Lever * complete the request 1065262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 106625985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 1067262965f5SChuck Lever * 1068262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 1069262965f5SChuck Lever * if sendmsg is not able to make progress? 1070262965f5SChuck Lever */ 1071adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req) 1072262965f5SChuck Lever { 1073262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 1074ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1075262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 10767e3d3620STrond Myklebust rpc_fraghdr rm = xs_stream_record_marker(xdr); 10777e3d3620STrond Myklebust unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 10789e55eef4SChuck Lever struct msghdr msg = { 10799e55eef4SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 10809e55eef4SChuck Lever }; 1081d0afde5fSTrond Myklebust bool vm_wait; 10823f649ab7SKees Cook unsigned int sent; 1083b595bb15SChuck Lever int status; 1084a246b010SChuck Lever 10854cd34e7cSTrond Myklebust /* Close the stream if the previous transmission was incomplete */ 10864cd34e7cSTrond Myklebust if (xs_send_request_was_aborted(transport, req)) { 10874cd34e7cSTrond Myklebust if (transport->sock != NULL) 10884cd34e7cSTrond Myklebust kernel_sock_shutdown(transport->sock, SHUT_RDWR); 10894cd34e7cSTrond Myklebust return -ENOTCONN; 10904cd34e7cSTrond Myklebust } 1091d275880aSTrond Myklebust if (!transport->inet) 1092d275880aSTrond Myklebust return -ENOTCONN; 10934cd34e7cSTrond Myklebust 1094262965f5SChuck Lever xs_pktdump("packet data:", 1095262965f5SChuck Lever req->rq_svec->iov_base, 1096262965f5SChuck Lever req->rq_svec->iov_len); 1097a246b010SChuck Lever 10987196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 10997196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 11007196dbb0STrond Myklebust 11013b21f757STrond Myklebust xs_set_srcport(transport, transport->sock); 11023b21f757STrond Myklebust 1103a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 1104a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 1105262965f5SChuck Lever * called sendmsg(). */ 110678215759SChuck Lever req->rq_xtime = ktime_get(); 1107d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, true); 1108d0afde5fSTrond Myklebust 1109d0afde5fSTrond Myklebust vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 1110d0afde5fSTrond Myklebust 1111d0afde5fSTrond Myklebust do { 11129e55eef4SChuck Lever status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 11137e3d3620STrond Myklebust transport->xmit.offset, rm, &sent); 1114a246b010SChuck Lever 1115262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 11166c7a64e5STrond Myklebust xdr->len - transport->xmit.offset, status); 1117262965f5SChuck Lever 1118a246b010SChuck Lever /* If we've sent the entire packet, immediately 1119a246b010SChuck Lever * reset the count of bytes sent. */ 11206c7a64e5STrond Myklebust transport->xmit.offset += sent; 11216c7a64e5STrond Myklebust req->rq_bytes_sent = transport->xmit.offset; 11227e3d3620STrond Myklebust if (likely(req->rq_bytes_sent >= msglen)) { 11236c7a64e5STrond Myklebust req->rq_xmit_bytes_sent += transport->xmit.offset; 11246c7a64e5STrond Myklebust transport->xmit.offset = 0; 1125d737e5d4STrond Myklebust if (atomic_long_read(&xprt->xmit_queuelen) == 1) 1126d737e5d4STrond Myklebust tcp_sock_set_cork(transport->inet, false); 1127a246b010SChuck Lever return 0; 1128a246b010SChuck Lever } 1129262965f5SChuck Lever 11309ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 11319ffadfbcSTrond Myklebust 1132d0afde5fSTrond Myklebust if (sent > 0) 11339ffadfbcSTrond Myklebust vm_wait = false; 1134d0afde5fSTrond Myklebust 1135d0afde5fSTrond Myklebust } while (status == 0); 1136a246b010SChuck Lever 1137262965f5SChuck Lever switch (status) { 1138fba91afbSTrond Myklebust case -ENOTSOCK: 1139fba91afbSTrond Myklebust status = -ENOTCONN; 1140fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 1141fba91afbSTrond Myklebust break; 1142262965f5SChuck Lever case -EAGAIN: 1143d0afde5fSTrond Myklebust status = xs_stream_nospace(req, vm_wait); 1144262965f5SChuck Lever break; 1145262965f5SChuck Lever case -ECONNRESET: 1146262965f5SChuck Lever case -ECONNREFUSED: 1147262965f5SChuck Lever case -ENOTCONN: 11483913c78cSTrond Myklebust case -EADDRINUSE: 1149b5872f0cSTrond Myklebust case -ENOBUFS: 1150b9d2bb2eSTrond Myklebust case -EPIPE: 115113331a55STrond Myklebust break; 115213331a55STrond Myklebust default: 115313331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 115413331a55STrond Myklebust -status); 1155a246b010SChuck Lever } 11565fe46e9dSBian Naimeng 1157a246b010SChuck Lever return status; 1158a246b010SChuck Lever } 1159a246b010SChuck Lever 11602a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11612a9e1cfaSTrond Myklebust { 11622a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 11632a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 11642a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 11652118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 11662a9e1cfaSTrond Myklebust } 11672a9e1cfaSTrond Myklebust 11682a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 11692a9e1cfaSTrond Myklebust { 11702a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 11712a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 11722a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 11732118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 11742118071dSTrond Myklebust } 11752118071dSTrond Myklebust 117642d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 117742d42a5bSTrond Myklebust { 117842d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 117942d42a5bSTrond Myklebust 118042d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 11814f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 11824f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 11834f8943f8STrond Myklebust clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 11842790a624STrond Myklebust clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 11854f8943f8STrond Myklebust } 11864f8943f8STrond Myklebust 11874f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 11884f8943f8STrond Myklebust { 11894f8943f8STrond Myklebust set_bit(nr, &transport->sock_state); 11904f8943f8STrond Myklebust queue_work(xprtiod_workqueue, &transport->error_worker); 119142d42a5bSTrond Myklebust } 119242d42a5bSTrond Myklebust 1193b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1194b70ae915STrond Myklebust { 1195d896ba83STrond Myklebust xprt->connect_cookie++; 1196b70ae915STrond Myklebust smp_mb__before_atomic(); 1197b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1198b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 119942d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 1200b70ae915STrond Myklebust smp_mb__after_atomic(); 1201b70ae915STrond Myklebust } 1202b70ae915STrond Myklebust 12032118071dSTrond Myklebust /** 12042118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 12052118071dSTrond Myklebust * @sk: socket 12062118071dSTrond Myklebust * 12072118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 12082118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 12092118071dSTrond Myklebust */ 12102118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 12112118071dSTrond Myklebust { 12124f8943f8STrond Myklebust struct sock_xprt *transport; 12132118071dSTrond Myklebust struct rpc_xprt *xprt; 12142118071dSTrond Myklebust 12152118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1216ea9afca8STrond Myklebust return; 12172118071dSTrond Myklebust 12184f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1219af84537dSBenjamin Coddington transport->xprt_err = -sk->sk_err; 1220af84537dSBenjamin Coddington if (transport->xprt_err == 0) 1221ea9afca8STrond Myklebust return; 12222118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 1223af84537dSBenjamin Coddington xprt, -transport->xprt_err); 1224af84537dSBenjamin Coddington trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); 1225af84537dSBenjamin Coddington 1226af84537dSBenjamin Coddington /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ 1227af84537dSBenjamin Coddington smp_mb__before_atomic(); 12284f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 12292a9e1cfaSTrond Myklebust } 12302a9e1cfaSTrond Myklebust 1231fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 1232a246b010SChuck Lever { 1233ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1234ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 12356cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 1236a73881c9STrond Myklebust struct file *filp = transport->file; 1237a246b010SChuck Lever 1238fe315e76SChuck Lever if (sk == NULL) 1239fe315e76SChuck Lever return; 1240f0043206STrond Myklebust /* 1241f0043206STrond Myklebust * Make sure we're calling this in a context from which it is safe 1242f0043206STrond Myklebust * to call __fput_sync(). In practice that means rpciod and the 1243f0043206STrond Myklebust * system workqueue. 1244f0043206STrond Myklebust */ 1245f0043206STrond Myklebust if (!(current->flags & PF_WQ_WORKER)) { 1246f0043206STrond Myklebust WARN_ON_ONCE(1); 1247f0043206STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 1248f0043206STrond Myklebust return; 1249f0043206STrond Myklebust } 12509903cd1cSChuck Lever 1251264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 1252264d1df3SJeff Layton sk_clear_memalloc(sk); 1253264d1df3SJeff Layton 125475eb6af7SChuck Lever tls_handshake_cancel(sk); 125575eb6af7SChuck Lever 125609939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 125709939204STrond Myklebust 1258edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 1259ea9afca8STrond Myklebust lock_sock(sk); 1260ee0ac0c2SChuck Lever transport->inet = NULL; 1261ee0ac0c2SChuck Lever transport->sock = NULL; 1262a73881c9STrond Myklebust transport->file = NULL; 1263a246b010SChuck Lever 1264a246b010SChuck Lever sk->sk_user_data = NULL; 12652a9e1cfaSTrond Myklebust 12662a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 12670c78789eSTrond Myklebust xprt_clear_connected(xprt); 12686cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 1269ae053551STrond Myklebust /* Reset stream record info */ 1270ae053551STrond Myklebust xs_stream_reset_connect(transport); 1271ea9afca8STrond Myklebust release_sock(sk); 1272edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 1273a246b010SChuck Lever 12746cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 1275f0043206STrond Myklebust __fput_sync(filp); 12760445f92cSTrond Myklebust 12770445f92cSTrond Myklebust xprt_disconnect_done(xprt); 1278fe315e76SChuck Lever } 1279fe315e76SChuck Lever 1280fe315e76SChuck Lever /** 1281fe315e76SChuck Lever * xs_close - close a socket 1282fe315e76SChuck Lever * @xprt: transport 1283fe315e76SChuck Lever * 1284fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 1285fe315e76SChuck Lever * on the server we want to save. 1286f75e6745STrond Myklebust * 1287f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1288f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 1289fe315e76SChuck Lever */ 1290fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 1291fe315e76SChuck Lever { 1292fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1293fe315e76SChuck Lever 1294fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 1295fe315e76SChuck Lever 1296fe315e76SChuck Lever xs_reset_transport(transport); 129761d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 1298a246b010SChuck Lever } 1299a246b010SChuck Lever 13004a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 13014a068258SChuck Lever { 13024a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 13034a068258SChuck Lever xprt); 13044a068258SChuck Lever xprt_disconnect_done(xprt); 13054a068258SChuck Lever } 13064a068258SChuck Lever 1307315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 1308315f3812SKinglong Mee { 1309315f3812SKinglong Mee xs_free_peer_addresses(xprt); 1310315f3812SKinglong Mee xprt_free(xprt); 1311315f3812SKinglong Mee } 1312315f3812SKinglong Mee 13139903cd1cSChuck Lever /** 13149903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 13159903cd1cSChuck Lever * @xprt: doomed transport 13169903cd1cSChuck Lever * 13179903cd1cSChuck Lever */ 13189903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 1319a246b010SChuck Lever { 132003c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 132103c78827STrond Myklebust struct sock_xprt, xprt); 13229903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 13239903cd1cSChuck Lever 132403c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 1325a1311d87STrond Myklebust xs_close(xprt); 1326edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 1327b5e92419STrond Myklebust cancel_work_sync(&transport->error_worker); 1328315f3812SKinglong Mee xs_xprt_free(xprt); 1329a1311d87STrond Myklebust module_put(THIS_MODULE); 1330a246b010SChuck Lever } 1331a246b010SChuck Lever 13329903cd1cSChuck Lever /** 1333f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1334f9b2ee71STrond Myklebust * @xprt: transport 1335f9b2ee71STrond Myklebust * @sk: socket 1336f9b2ee71STrond Myklebust * @skb: skbuff 13379903cd1cSChuck Lever * 1338a246b010SChuck Lever */ 1339f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1340f9b2ee71STrond Myklebust struct sock *sk, 1341f9b2ee71STrond Myklebust struct sk_buff *skb) 1342a246b010SChuck Lever { 1343a246b010SChuck Lever struct rpc_task *task; 1344a246b010SChuck Lever struct rpc_rqst *rovr; 1345f9b2ee71STrond Myklebust int repsize, copied; 1346d8ed029dSAlexey Dobriyan u32 _xid; 1347d8ed029dSAlexey Dobriyan __be32 *xp; 1348a246b010SChuck Lever 13491da8c681SWillem de Bruijn repsize = skb->len; 1350a246b010SChuck Lever if (repsize < 4) { 13519903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1352f9b2ee71STrond Myklebust return; 1353a246b010SChuck Lever } 1354a246b010SChuck Lever 1355a246b010SChuck Lever /* Copy the XID from the skb... */ 13561da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1357a246b010SChuck Lever if (xp == NULL) 1358f9b2ee71STrond Myklebust return; 1359a246b010SChuck Lever 1360a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 136175c84151STrond Myklebust spin_lock(&xprt->queue_lock); 1362a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1363a246b010SChuck Lever if (!rovr) 1364a246b010SChuck Lever goto out_unlock; 1365729749bbSTrond Myklebust xprt_pin_rqst(rovr); 1366ecd465eeSChuck Lever xprt_update_rtt(rovr->rq_task); 136775c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1368a246b010SChuck Lever task = rovr->rq_task; 1369a246b010SChuck Lever 1370a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1371a246b010SChuck Lever copied = repsize; 1372a246b010SChuck Lever 1373a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 13741781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 137575c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13760afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1377729749bbSTrond Myklebust goto out_unpin; 13781781f7f5SHerbert Xu } 13791781f7f5SHerbert Xu 1380a246b010SChuck Lever 1381b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 13826a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 1383b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 138475c84151STrond Myklebust spin_lock(&xprt->queue_lock); 13851570c1e4SChuck Lever xprt_complete_rqst(task, copied); 13860afa6b44STrond Myklebust __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1387729749bbSTrond Myklebust out_unpin: 1388729749bbSTrond Myklebust xprt_unpin_rqst(rovr); 1389a246b010SChuck Lever out_unlock: 139075c84151STrond Myklebust spin_unlock(&xprt->queue_lock); 1391f9b2ee71STrond Myklebust } 1392f9b2ee71STrond Myklebust 1393f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1394f9b2ee71STrond Myklebust { 1395f9b2ee71STrond Myklebust struct sk_buff *skb; 1396f9b2ee71STrond Myklebust struct sock *sk; 1397f9b2ee71STrond Myklebust int err; 1398f9b2ee71STrond Myklebust 1399f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1400f9b2ee71STrond Myklebust sk = transport->inet; 1401f9b2ee71STrond Myklebust if (sk == NULL) 1402f9b2ee71STrond Myklebust goto out; 1403f9b2ee71STrond Myklebust for (;;) { 1404ec095263SOliver Hartkopp skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 14054f546149STrond Myklebust if (skb == NULL) 14064f546149STrond Myklebust break; 1407f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1408850cbaddSPaolo Abeni consume_skb(skb); 14090af3442aSTrond Myklebust cond_resched(); 1410f9b2ee71STrond Myklebust } 14110ffe86f4STrond Myklebust xs_poll_check_readable(transport); 1412a246b010SChuck Lever out: 1413f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1414f9b2ee71STrond Myklebust } 1415f9b2ee71STrond Myklebust 1416f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1417f9b2ee71STrond Myklebust { 1418f9b2ee71STrond Myklebust struct sock_xprt *transport = 1419f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1420a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1421a1231fdaSTrond Myklebust 1422f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1423a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 1424f9b2ee71STrond Myklebust } 1425f9b2ee71STrond Myklebust 1426f9b2ee71STrond Myklebust /** 1427f67939e4SChuck Lever * xs_data_ready - "data ready" callback for sockets 1428f9b2ee71STrond Myklebust * @sk: socket with data to read 1429f9b2ee71STrond Myklebust * 1430f9b2ee71STrond Myklebust */ 1431f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1432f9b2ee71STrond Myklebust { 1433f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1434f9b2ee71STrond Myklebust 143540e0b090SPeilin Ye trace_sk_data_ready(sk); 143640e0b090SPeilin Ye 1437f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1438f9b2ee71STrond Myklebust if (xprt != NULL) { 1439f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1440f9b2ee71STrond Myklebust struct sock_xprt, xprt); 1441f67939e4SChuck Lever 1442f67939e4SChuck Lever trace_xs_data_ready(xprt); 1443f67939e4SChuck Lever 14445157b956STrond Myklebust transport->old_data_ready(sk); 14450d3ca07fSChuck Lever 14460d3ca07fSChuck Lever if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 14470d3ca07fSChuck Lever return; 14480d3ca07fSChuck Lever 14495157b956STrond Myklebust /* Any data means we had a useful conversation, so 14505157b956STrond Myklebust * then we don't need to delay the next reconnect 14515157b956STrond Myklebust */ 14525157b956STrond Myklebust if (xprt->reestablish_timeout) 14535157b956STrond Myklebust xprt->reestablish_timeout = 0; 145442d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 145540a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1456f9b2ee71STrond Myklebust } 1457a246b010SChuck Lever } 1458a246b010SChuck Lever 1459a519fc7aSTrond Myklebust /* 1460a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1461a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1462a519fc7aSTrond Myklebust */ 1463a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1464a519fc7aSTrond Myklebust { 1465a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1466a519fc7aSTrond Myklebust } 1467a519fc7aSTrond Myklebust 14689e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 14696b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14706b26cc8cSChuck Lever { 14716b26cc8cSChuck Lever return PAGE_SIZE; 14726b26cc8cSChuck Lever } 14739e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 147444b98efdSRicardo Labiaga 14759903cd1cSChuck Lever /** 1476efce2d0bSTrond Myklebust * xs_local_state_change - callback to handle AF_LOCAL socket state changes 1477efce2d0bSTrond Myklebust * @sk: socket whose state has changed 1478efce2d0bSTrond Myklebust * 1479efce2d0bSTrond Myklebust */ 1480efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk) 1481efce2d0bSTrond Myklebust { 1482efce2d0bSTrond Myklebust struct rpc_xprt *xprt; 1483efce2d0bSTrond Myklebust struct sock_xprt *transport; 1484efce2d0bSTrond Myklebust 1485efce2d0bSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 1486efce2d0bSTrond Myklebust return; 1487efce2d0bSTrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 1488efce2d0bSTrond Myklebust if (sk->sk_shutdown & SHUTDOWN_MASK) { 1489efce2d0bSTrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1490efce2d0bSTrond Myklebust /* Trigger the socket release */ 1491efce2d0bSTrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1492efce2d0bSTrond Myklebust } 1493efce2d0bSTrond Myklebust } 1494efce2d0bSTrond Myklebust 1495efce2d0bSTrond Myklebust /** 14969903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14979903cd1cSChuck Lever * @sk: socket whose state has changed 14989903cd1cSChuck Lever * 14999903cd1cSChuck Lever */ 15009903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1501a246b010SChuck Lever { 1502a246b010SChuck Lever struct rpc_xprt *xprt; 15030fdea1e8STrond Myklebust struct sock_xprt *transport; 1504a246b010SChuck Lever 1505a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1506ea9afca8STrond Myklebust return; 15079903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1508669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1509a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1510a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1511669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1512669502ffSAndy Chittenden sk->sk_shutdown); 1513a246b010SChuck Lever 15140fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 151540b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1516a246b010SChuck Lever switch (sk->sk_state) { 1517a246b010SChuck Lever case TCP_ESTABLISHED: 1518a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 15198b71798cSTrond Myklebust xprt->connect_cookie++; 15200fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15210fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 152251971139SChuck Lever 15233968a8a5SChuck Lever xprt->stat.connect_count++; 15243968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 15253968a8a5SChuck Lever xprt->stat.connect_start; 15264f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1527a246b010SChuck Lever } 1528a246b010SChuck Lever break; 15293b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15303b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15317c1d71cfSTrond Myklebust xprt->connect_cookie++; 1532663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15333b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15344e857c58SPeter Zijlstra smp_mb__before_atomic(); 15353b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1536ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15374e857c58SPeter Zijlstra smp_mb__after_atomic(); 1538a246b010SChuck Lever break; 1539632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15403b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15417c1d71cfSTrond Myklebust xprt->connect_cookie++; 1542d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15434f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1544df561f66SGustavo A. R. Silva fallthrough; 1545663b8858STrond Myklebust case TCP_CLOSING: 1546663b8858STrond Myklebust /* 1547663b8858STrond Myklebust * If the server closed down the connection, make sure that 1548663b8858STrond Myklebust * we back off before reconnecting 1549663b8858STrond Myklebust */ 1550663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1551663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15523b948ae5STrond Myklebust break; 15533b948ae5STrond Myklebust case TCP_LAST_ACK: 1554670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15554e857c58SPeter Zijlstra smp_mb__before_atomic(); 15563b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15574e857c58SPeter Zijlstra smp_mb__after_atomic(); 15583b948ae5STrond Myklebust break; 15593b948ae5STrond Myklebust case TCP_CLOSE: 15600fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 15610fdea1e8STrond Myklebust &transport->sock_state)) 15620fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 15639b30889cSTrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 15649b30889cSTrond Myklebust /* Trigger the socket release */ 15654f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1566a246b010SChuck Lever } 1567a246b010SChuck Lever } 1568a246b010SChuck Lever 15691f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15701f0fa154SIlpo Järvinen { 15714f8943f8STrond Myklebust struct sock_xprt *transport; 15721f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15731f0fa154SIlpo Järvinen 157413331a55STrond Myklebust if (!sk->sk_socket) 15751f0fa154SIlpo Järvinen return; 157613331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 15771f0fa154SIlpo Järvinen 15781f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15791f0fa154SIlpo Järvinen return; 15804f8943f8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 15812790a624STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state)) 15822790a624STrond Myklebust return; 15834f8943f8STrond Myklebust xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1584c544577dSTrond Myklebust sk->sk_write_pending--; 15851f0fa154SIlpo Järvinen } 15861f0fa154SIlpo Järvinen 15872a9e1cfaSTrond Myklebust /** 1588c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1589c7b2cae8SChuck Lever * becomes available 15909903cd1cSChuck Lever * @sk: socket whose state has changed 15919903cd1cSChuck Lever * 1592a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1593a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1594c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1595a246b010SChuck Lever * with a bunch of small requests. 1596a246b010SChuck Lever */ 1597c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1598a246b010SChuck Lever { 1599c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16001f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16011f0fa154SIlpo Järvinen xs_write_space(sk); 1602c7b2cae8SChuck Lever } 1603c7b2cae8SChuck Lever 1604c7b2cae8SChuck Lever /** 1605c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1606c7b2cae8SChuck Lever * becomes available 1607c7b2cae8SChuck Lever * @sk: socket whose state has changed 1608c7b2cae8SChuck Lever * 1609c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1610c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1611c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1612c7b2cae8SChuck Lever * with a bunch of small requests. 1613c7b2cae8SChuck Lever */ 1614c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1615c7b2cae8SChuck Lever { 1616c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 161764dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16181f0fa154SIlpo Järvinen xs_write_space(sk); 1619a246b010SChuck Lever } 1620a246b010SChuck Lever 1621470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1622a246b010SChuck Lever { 1623ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1624ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1625a246b010SChuck Lever 16267c6e066eSChuck Lever if (transport->rcvsize) { 1627a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16287c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1629a246b010SChuck Lever } 16307c6e066eSChuck Lever if (transport->sndsize) { 1631a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16327c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1633a246b010SChuck Lever sk->sk_write_space(sk); 1634a246b010SChuck Lever } 1635a246b010SChuck Lever } 1636a246b010SChuck Lever 163743118c29SChuck Lever /** 1638470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 163943118c29SChuck Lever * @xprt: generic transport 1640470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1641470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 164243118c29SChuck Lever * 1643470056c2SChuck Lever * Set socket send and receive buffer size limits. 164443118c29SChuck Lever */ 1645470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 164643118c29SChuck Lever { 16477c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16487c6e066eSChuck Lever 16497c6e066eSChuck Lever transport->sndsize = 0; 1650470056c2SChuck Lever if (sndsize) 16517c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16527c6e066eSChuck Lever transport->rcvsize = 0; 1653470056c2SChuck Lever if (rcvsize) 16547c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1655470056c2SChuck Lever 1656470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 165743118c29SChuck Lever } 165843118c29SChuck Lever 165946c0ee8bSChuck Lever /** 166046c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1661acf0a39fSChuck Lever * @xprt: controlling transport 166246c0ee8bSChuck Lever * @task: task that timed out 166346c0ee8bSChuck Lever * 166446c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 166546c0ee8bSChuck Lever */ 16666a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 166746c0ee8bSChuck Lever { 1668b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 16696a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1670b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 167146c0ee8bSChuck Lever } 167246c0ee8bSChuck Lever 1673826799e6SJ. Bruce Fields static int xs_get_random_port(void) 1674b85d8806SChuck Lever { 1675826799e6SJ. Bruce Fields unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1676826799e6SJ. Bruce Fields unsigned short range; 1677826799e6SJ. Bruce Fields unsigned short rand; 1678826799e6SJ. Bruce Fields 1679826799e6SJ. Bruce Fields if (max < min) 1680826799e6SJ. Bruce Fields return -EADDRINUSE; 1681826799e6SJ. Bruce Fields range = max - min + 1; 16828032bf12SJason A. Donenfeld rand = get_random_u32_below(range); 1683826799e6SJ. Bruce Fields return rand + min; 1684b85d8806SChuck Lever } 1685b85d8806SChuck Lever 16864dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 16874dda9c8aSTrond Myklebust { 16884dda9c8aSTrond Myklebust struct sockaddr_storage buf; 16894dda9c8aSTrond Myklebust unsigned short port = 0; 16904dda9c8aSTrond Myklebust 16919b2c45d4SDenys Vlasenko if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 16924dda9c8aSTrond Myklebust goto out; 16934dda9c8aSTrond Myklebust switch (buf.ss_family) { 16944dda9c8aSTrond Myklebust case AF_INET6: 16954dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 16964dda9c8aSTrond Myklebust break; 16974dda9c8aSTrond Myklebust case AF_INET: 16984dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 16994dda9c8aSTrond Myklebust } 17004dda9c8aSTrond Myklebust out: 17014dda9c8aSTrond Myklebust return port; 17024dda9c8aSTrond Myklebust } 17034dda9c8aSTrond Myklebust 17044dda9c8aSTrond Myklebust /** 170592200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 170692200412SChuck Lever * @xprt: generic transport 170792200412SChuck Lever * @port: new port number 170892200412SChuck Lever * 170992200412SChuck Lever */ 171092200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 171192200412SChuck Lever { 171292200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1713c4efcb1dSChuck Lever 17149dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17159dc3b095SChuck Lever xs_update_peer_port(xprt); 171692200412SChuck Lever } 171792200412SChuck Lever 17184dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 17194dda9c8aSTrond Myklebust { 1720e6237b6fSTrond Myklebust if (transport->srcport == 0 && transport->xprt.reuseport) 17214dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 17224dda9c8aSTrond Myklebust } 17234dda9c8aSTrond Myklebust 1724826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport) 172567a391d7STrond Myklebust { 1726826799e6SJ. Bruce Fields int port = transport->srcport; 172767a391d7STrond Myklebust 172867a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 172967a391d7STrond Myklebust port = xs_get_random_port(); 173067a391d7STrond Myklebust return port; 173167a391d7STrond Myklebust } 173267a391d7STrond Myklebust 1733421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt) 1734a8482488SOlga Kornievskaia { 1735a8482488SOlga Kornievskaia struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1736b49ea673SNeilBrown unsigned short ret = 0; 1737b49ea673SNeilBrown mutex_lock(&sock->recv_mutex); 1738b49ea673SNeilBrown if (sock->sock) 1739b49ea673SNeilBrown ret = xs_sock_getport(sock->sock); 1740b49ea673SNeilBrown mutex_unlock(&sock->recv_mutex); 1741b49ea673SNeilBrown return ret; 1742a8482488SOlga Kornievskaia } 1743421ab1beSTrond Myklebust 1744421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen) 1745421ab1beSTrond Myklebust { 1746421ab1beSTrond Myklebust struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1747421ab1beSTrond Myklebust union { 1748421ab1beSTrond Myklebust struct sockaddr sa; 1749421ab1beSTrond Myklebust struct sockaddr_storage st; 1750421ab1beSTrond Myklebust } saddr; 1751421ab1beSTrond Myklebust int ret = -ENOTCONN; 1752421ab1beSTrond Myklebust 1753421ab1beSTrond Myklebust mutex_lock(&sock->recv_mutex); 1754421ab1beSTrond Myklebust if (sock->sock) { 1755421ab1beSTrond Myklebust ret = kernel_getsockname(sock->sock, &saddr.sa); 1756421ab1beSTrond Myklebust if (ret >= 0) 1757421ab1beSTrond Myklebust ret = snprintf(buf, buflen, "%pISc", &saddr.sa); 1758421ab1beSTrond Myklebust } 1759421ab1beSTrond Myklebust mutex_unlock(&sock->recv_mutex); 1760421ab1beSTrond Myklebust return ret; 1761421ab1beSTrond Myklebust } 1762a8482488SOlga Kornievskaia 1763baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 176467a391d7STrond Myklebust { 1765fbfffbd5SChuck Lever if (transport->srcport != 0) 1766fbfffbd5SChuck Lever transport->srcport = 0; 176767a391d7STrond Myklebust if (!transport->xprt.resvport) 176867a391d7STrond Myklebust return 0; 176967a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 177067a391d7STrond Myklebust return xprt_max_resvport; 177167a391d7STrond Myklebust return --port; 177267a391d7STrond Myklebust } 1773beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1774a246b010SChuck Lever { 1775beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 177667a391d7STrond Myklebust int err, nloop = 0; 1777826799e6SJ. Bruce Fields int port = xs_get_srcport(transport); 177867a391d7STrond Myklebust unsigned short last; 1779a246b010SChuck Lever 17800f7a622cSChris Perl /* 17810f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 17820f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 17830f7a622cSChris Perl * port selection happen implicitly when the socket is used 17840f7a622cSChris Perl * (for example at connect time). 17850f7a622cSChris Perl * 17860f7a622cSChris Perl * This ensures that we can continue to establish TCP 17870f7a622cSChris Perl * connections even when all local ephemeral ports are already 17880f7a622cSChris Perl * a part of some TCP connection. This makes no difference 178912b20ce3SBhaskar Chowdhury * for UDP sockets, but also doesn't harm them. 17900f7a622cSChris Perl * 17910f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 17920f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 17930f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 17940f7a622cSChris Perl */ 1795826799e6SJ. Bruce Fields if (port <= 0) 1796826799e6SJ. Bruce Fields return port; 17970f7a622cSChris Perl 1798beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1799a246b010SChuck Lever do { 1800beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1801e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1802beb59b68SPavel Emelyanov transport->xprt.addrlen); 1803a246b010SChuck Lever if (err == 0) { 1804bc1c56e9SNeilBrown if (transport->xprt.reuseport) 1805fbfffbd5SChuck Lever transport->srcport = port; 1806d3bc9a1dSFrank van Maarseveen break; 1807a246b010SChuck Lever } 180867a391d7STrond Myklebust last = port; 1809baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 181067a391d7STrond Myklebust if (port > last) 181167a391d7STrond Myklebust nloop++; 181267a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1813beb59b68SPavel Emelyanov 18144232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1815beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1816beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1817beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1818beb59b68SPavel Emelyanov else 1819beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1820beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18217dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1822a246b010SChuck Lever return err; 1823a246b010SChuck Lever } 1824a246b010SChuck Lever 1825176e21eeSChuck Lever /* 1826176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1827176e21eeSChuck Lever */ 1828176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1829176e21eeSChuck Lever { 1830fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1831176e21eeSChuck Lever } 1832176e21eeSChuck Lever 1833176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1834176e21eeSChuck Lever { 1835176e21eeSChuck Lever } 1836a246b010SChuck Lever 1837ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1838064a9177SNeilBrown static struct lock_class_key xs_key[3]; 1839064a9177SNeilBrown static struct lock_class_key xs_slock_key[3]; 1840ed07536eSPeter Zijlstra 1841176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1842176e21eeSChuck Lever { 1843176e21eeSChuck Lever struct sock *sk = sock->sk; 1844176e21eeSChuck Lever 1845176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1846064a9177SNeilBrown &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]); 1847176e21eeSChuck Lever } 1848176e21eeSChuck Lever 18498945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1850ed07536eSPeter Zijlstra { 1851ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 18528945ee5eSChuck Lever 18538945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 1854064a9177SNeilBrown &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]); 1855ed07536eSPeter Zijlstra } 18568945ee5eSChuck Lever 18578945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 18588945ee5eSChuck Lever { 18598945ee5eSChuck Lever struct sock *sk = sock->sk; 18608945ee5eSChuck Lever 18618945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 1862064a9177SNeilBrown &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]); 1863ed07536eSPeter Zijlstra } 18646bc9638aSPavel Emelyanov 18656bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18666bc9638aSPavel Emelyanov { 1867fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 18681b7a1819SWeston Andros Adamson return; 18691b7a1819SWeston Andros Adamson 18704232e863SChuck Lever switch (family) { 1871176e21eeSChuck Lever case AF_LOCAL: 1872176e21eeSChuck Lever xs_reclassify_socketu(sock); 1873176e21eeSChuck Lever break; 18744232e863SChuck Lever case AF_INET: 18756bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18764232e863SChuck Lever break; 18774232e863SChuck Lever case AF_INET6: 18786bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18794232e863SChuck Lever break; 18804232e863SChuck Lever } 18816bc9638aSPavel Emelyanov } 1882ed07536eSPeter Zijlstra #else 18836bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18846bc9638aSPavel Emelyanov { 18856bc9638aSPavel Emelyanov } 1886ed07536eSPeter Zijlstra #endif 1887ed07536eSPeter Zijlstra 188893dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 188993dc41bdSNeilBrown { 189093dc41bdSNeilBrown } 189193dc41bdSNeilBrown 18926bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18934dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 18944dda9c8aSTrond Myklebust int protocol, bool reuseport) 189522f79326SPavel Emelyanov { 1896a73881c9STrond Myklebust struct file *filp; 189722f79326SPavel Emelyanov struct socket *sock; 189822f79326SPavel Emelyanov int err; 189922f79326SPavel Emelyanov 19006bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 190122f79326SPavel Emelyanov if (err < 0) { 190222f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 190322f79326SPavel Emelyanov protocol, -err); 190422f79326SPavel Emelyanov goto out; 190522f79326SPavel Emelyanov } 19066bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 190722f79326SPavel Emelyanov 19084dda9c8aSTrond Myklebust if (reuseport) 1909fe31a326SChristoph Hellwig sock_set_reuseport(sock->sk); 19104dda9c8aSTrond Myklebust 19114cea288aSBen Hutchings err = xs_bind(transport, sock); 19124cea288aSBen Hutchings if (err) { 191322f79326SPavel Emelyanov sock_release(sock); 191422f79326SPavel Emelyanov goto out; 191522f79326SPavel Emelyanov } 191622f79326SPavel Emelyanov 1917a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1918a73881c9STrond Myklebust if (IS_ERR(filp)) 1919a73881c9STrond Myklebust return ERR_CAST(filp); 1920a73881c9STrond Myklebust transport->file = filp; 1921a73881c9STrond Myklebust 192222f79326SPavel Emelyanov return sock; 192322f79326SPavel Emelyanov out: 192422f79326SPavel Emelyanov return ERR_PTR(err); 192522f79326SPavel Emelyanov } 192622f79326SPavel Emelyanov 1927176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1928176e21eeSChuck Lever struct socket *sock) 1929176e21eeSChuck Lever { 1930176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1931176e21eeSChuck Lever xprt); 1932176e21eeSChuck Lever 1933176e21eeSChuck Lever if (!transport->inet) { 1934176e21eeSChuck Lever struct sock *sk = sock->sk; 1935176e21eeSChuck Lever 1936ea9afca8STrond Myklebust lock_sock(sk); 1937176e21eeSChuck Lever 1938176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1939176e21eeSChuck Lever 1940176e21eeSChuck Lever sk->sk_user_data = xprt; 1941a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1942176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1943efce2d0bSTrond Myklebust sk->sk_state_change = xs_local_state_change; 19442118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 194598123866SBenjamin Coddington sk->sk_use_task_frag = false; 1946176e21eeSChuck Lever 1947176e21eeSChuck Lever xprt_clear_connected(xprt); 1948176e21eeSChuck Lever 1949176e21eeSChuck Lever /* Reset to new socket */ 1950176e21eeSChuck Lever transport->sock = sock; 1951176e21eeSChuck Lever transport->inet = sk; 1952176e21eeSChuck Lever 1953ea9afca8STrond Myklebust release_sock(sk); 1954176e21eeSChuck Lever } 1955176e21eeSChuck Lever 1956ae053551STrond Myklebust xs_stream_start_connect(transport); 19576c7a64e5STrond Myklebust 1958176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1959176e21eeSChuck Lever } 1960176e21eeSChuck Lever 1961176e21eeSChuck Lever /** 1962176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1963176e21eeSChuck Lever * @transport: socket transport to connect 1964176e21eeSChuck Lever */ 1965dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 1966176e21eeSChuck Lever { 1967176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1968a73881c9STrond Myklebust struct file *filp; 1969176e21eeSChuck Lever struct socket *sock; 197068e9a246SColin Ian King int status; 1971176e21eeSChuck Lever 1972176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1973176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1974176e21eeSChuck Lever if (status < 0) { 1975176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1976176e21eeSChuck Lever "transport socket (%d).\n", -status); 1977176e21eeSChuck Lever goto out; 1978176e21eeSChuck Lever } 1979d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 1980176e21eeSChuck Lever 1981a73881c9STrond Myklebust filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1982a73881c9STrond Myklebust if (IS_ERR(filp)) { 1983a73881c9STrond Myklebust status = PTR_ERR(filp); 1984a73881c9STrond Myklebust goto out; 1985a73881c9STrond Myklebust } 1986a73881c9STrond Myklebust transport->file = filp; 1987a73881c9STrond Myklebust 1988176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1989176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1990176e21eeSChuck Lever 1991176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 199240b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 1993176e21eeSChuck Lever switch (status) { 1994176e21eeSChuck Lever case 0: 1995176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1996176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 19973968a8a5SChuck Lever xprt->stat.connect_count++; 19983968a8a5SChuck Lever xprt->stat.connect_time += (long)jiffies - 19993968a8a5SChuck Lever xprt->stat.connect_start; 2000176e21eeSChuck Lever xprt_set_connected(xprt); 200193f479d3SGustavo A. R. Silva break; 20023601c4a9STrond Myklebust case -ENOBUFS: 2003176e21eeSChuck Lever break; 2004176e21eeSChuck Lever case -ENOENT: 2005176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2006176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2007176e21eeSChuck Lever break; 20084a20a988STrond Myklebust case -ECONNREFUSED: 20094a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20104a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20114a20a988STrond Myklebust break; 2012176e21eeSChuck Lever default: 2013176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2014176e21eeSChuck Lever __func__, -status, 2015176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2016176e21eeSChuck Lever } 2017176e21eeSChuck Lever 2018176e21eeSChuck Lever out: 2019176e21eeSChuck Lever xprt_clear_connecting(xprt); 2020176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2021dc107402SJ. Bruce Fields return status; 2022dc107402SJ. Bruce Fields } 2023dc107402SJ. Bruce Fields 2024b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2025dc107402SJ. Bruce Fields { 2026dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2027dc107402SJ. Bruce Fields int ret; 2028dc107402SJ. Bruce Fields 2029aad41a7dSTrond Myklebust if (transport->file) 2030aad41a7dSTrond Myklebust goto force_disconnect; 2031aad41a7dSTrond Myklebust 2032dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2033dc107402SJ. Bruce Fields /* 2034dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2035dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2036dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2037dc107402SJ. Bruce Fields * 2038dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2039dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2040dc107402SJ. Bruce Fields * connect. 2041dc107402SJ. Bruce Fields */ 204239494194STrond Myklebust rpc_task_set_rpc_status(task, -ENOTCONN); 2043aad41a7dSTrond Myklebust goto out_wake; 2044dc107402SJ. Bruce Fields } 2045dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2046dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2047dc107402SJ. Bruce Fields msleep_interruptible(15000); 2048aad41a7dSTrond Myklebust return; 2049aad41a7dSTrond Myklebust force_disconnect: 2050aad41a7dSTrond Myklebust xprt_force_disconnect(xprt); 2051aad41a7dSTrond Myklebust out_wake: 2052aad41a7dSTrond Myklebust xprt_clear_connecting(xprt); 2053aad41a7dSTrond Myklebust xprt_wake_pending_tasks(xprt, -ENOTCONN); 2054176e21eeSChuck Lever } 2055176e21eeSChuck Lever 20563c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2057d6e971d8SJeff Layton /* 2058693486d5SNeilBrown * Note that this should be called with XPRT_LOCKED held, or recv_mutex 2059693486d5SNeilBrown * held, or when we otherwise know that we have exclusive access to the 2060693486d5SNeilBrown * socket, to guard against races with xs_reset_transport. 2061d6e971d8SJeff Layton */ 2062a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2063a564b8f0SMel Gorman { 2064a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2065a564b8f0SMel Gorman xprt); 2066a564b8f0SMel Gorman 2067d6e971d8SJeff Layton /* 2068d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2069d6e971d8SJeff Layton * reconnecting process will get it for us. 2070d6e971d8SJeff Layton */ 2071d6e971d8SJeff Layton if (!transport->inet) 2072d6e971d8SJeff Layton return; 20738e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2074a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2075a564b8f0SMel Gorman } 2076a564b8f0SMel Gorman 2077a564b8f0SMel Gorman /** 2078d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2079a564b8f0SMel Gorman * @xprt: transport to tag 2080a564b8f0SMel Gorman * 20818e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 20828e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2083a564b8f0SMel Gorman */ 2084d67fa4d8SJeff Layton static int 2085d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2086a564b8f0SMel Gorman { 2087d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2088a564b8f0SMel Gorman 2089693486d5SNeilBrown mutex_lock(&xs->recv_mutex); 2090693486d5SNeilBrown if (atomic_inc_return(&xprt->swapper) == 1 && 2091693486d5SNeilBrown xs->inet) 2092d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2093693486d5SNeilBrown mutex_unlock(&xs->recv_mutex); 20948e228133SJeff Layton return 0; 2095a564b8f0SMel Gorman } 2096a564b8f0SMel Gorman 20978e228133SJeff Layton /** 2098d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 20998e228133SJeff Layton * @xprt: transport to tag 21008e228133SJeff Layton * 21018e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 21028e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 21038e228133SJeff Layton */ 2104d67fa4d8SJeff Layton static void 2105d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21068e228133SJeff Layton { 2107d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21088e228133SJeff Layton 2109693486d5SNeilBrown mutex_lock(&xs->recv_mutex); 2110693486d5SNeilBrown if (atomic_dec_and_test(&xprt->swapper) && 2111693486d5SNeilBrown xs->inet) 2112d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2113693486d5SNeilBrown mutex_unlock(&xs->recv_mutex); 2114a564b8f0SMel Gorman } 2115a564b8f0SMel Gorman #else 2116a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2117a564b8f0SMel Gorman { 2118a564b8f0SMel Gorman } 2119d67fa4d8SJeff Layton 2120d67fa4d8SJeff Layton static int 2121d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2122d67fa4d8SJeff Layton { 2123d67fa4d8SJeff Layton return -EINVAL; 2124d67fa4d8SJeff Layton } 2125d67fa4d8SJeff Layton 2126d67fa4d8SJeff Layton static void 2127d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2128d67fa4d8SJeff Layton { 2129d67fa4d8SJeff Layton } 2130a564b8f0SMel Gorman #endif 2131a564b8f0SMel Gorman 213216be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2133a246b010SChuck Lever { 213416be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2135edb267a6SChuck Lever 2136ee0ac0c2SChuck Lever if (!transport->inet) { 2137b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2138b0d93ad5SChuck Lever 2139ea9afca8STrond Myklebust lock_sock(sk); 2140b0d93ad5SChuck Lever 21412a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21422a9e1cfaSTrond Myklebust 2143b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2144f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2145b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 214698123866SBenjamin Coddington sk->sk_use_task_frag = false; 2147b0d93ad5SChuck Lever 2148b0d93ad5SChuck Lever xprt_set_connected(xprt); 2149b0d93ad5SChuck Lever 2150b0d93ad5SChuck Lever /* Reset to new socket */ 2151ee0ac0c2SChuck Lever transport->sock = sock; 2152ee0ac0c2SChuck Lever transport->inet = sk; 2153b0d93ad5SChuck Lever 2154a564b8f0SMel Gorman xs_set_memalloc(xprt); 2155a564b8f0SMel Gorman 2156ea9afca8STrond Myklebust release_sock(sk); 2157b0d93ad5SChuck Lever } 2158470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 215902910177STrond Myklebust 216002910177STrond Myklebust xprt->stat.connect_start = jiffies; 216116be2d20SChuck Lever } 216216be2d20SChuck Lever 21638c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2164a246b010SChuck Lever { 2165a246b010SChuck Lever struct sock_xprt *transport = 2166a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2167a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2168d099b8afSColin Ian King struct socket *sock; 2169b65c0310SPavel Emelyanov int status = -EIO; 21708db55a03SNeilBrown unsigned int pflags = current->flags; 2171a246b010SChuck Lever 21728db55a03SNeilBrown if (atomic_read(&xprt->swapper)) 21738db55a03SNeilBrown current->flags |= PF_MEMALLOC; 21748c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 21754dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 21764dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2177b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2178a246b010SChuck Lever goto out; 217968e220bdSChuck Lever 2180c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2181c740eff8SChuck Lever "%s (port %s)\n", xprt, 2182c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2183c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2184c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 218568e220bdSChuck Lever 218668e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 218740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2188a246b010SChuck Lever status = 0; 2189b0d93ad5SChuck Lever out: 2190b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 2191cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 21927d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 21938db55a03SNeilBrown current_restore_flags(pflags, PF_MEMALLOC); 2194b0d93ad5SChuck Lever } 2195b0d93ad5SChuck Lever 21964876cc77STrond Myklebust /** 21974876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 21984876cc77STrond Myklebust * @xprt: transport 21994876cc77STrond Myklebust * 22004876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 22014876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 22024876cc77STrond Myklebust */ 22034876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 22044876cc77STrond Myklebust { 22054876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22064876cc77STrond Myklebust struct socket *sock = transport->sock; 22079b30889cSTrond Myklebust int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 22084876cc77STrond Myklebust 22094876cc77STrond Myklebust if (sock == NULL) 22104876cc77STrond Myklebust return; 22110a6ff58eSTrond Myklebust if (!xprt->reuseport) { 22120a6ff58eSTrond Myklebust xs_close(xprt); 22130a6ff58eSTrond Myklebust return; 22140a6ff58eSTrond Myklebust } 22159b30889cSTrond Myklebust switch (skst) { 22167c81e6a9STrond Myklebust case TCP_FIN_WAIT1: 22177c81e6a9STrond Myklebust case TCP_FIN_WAIT2: 2218943d045aSSiddharth Kawar case TCP_LAST_ACK: 22197c81e6a9STrond Myklebust break; 22207c81e6a9STrond Myklebust case TCP_ESTABLISHED: 22217c81e6a9STrond Myklebust case TCP_CLOSE_WAIT: 22224876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22234876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22249b30889cSTrond Myklebust break; 22257c81e6a9STrond Myklebust default: 22264876cc77STrond Myklebust xs_reset_transport(transport); 22274876cc77STrond Myklebust } 22289b30889cSTrond Myklebust } 22294876cc77STrond Myklebust 22308d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22318d1b8c62STrond Myklebust struct socket *sock) 2232b0d93ad5SChuck Lever { 223316be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22347196dbb0STrond Myklebust unsigned int keepidle; 22357196dbb0STrond Myklebust unsigned int keepcnt; 2236775f06abSTrond Myklebust unsigned int timeo; 22377f260e85STrond Myklebust 2238b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22397196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22407196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22417196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22427196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22437196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2244b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22457f260e85STrond Myklebust 22467f260e85STrond Myklebust /* TCP Keepalive options */ 2247ce3d9544SChristoph Hellwig sock_set_keepalive(sock->sk); 224871c48eb8SChristoph Hellwig tcp_sock_set_keepidle(sock->sk, keepidle); 2249d41ecaacSChristoph Hellwig tcp_sock_set_keepintvl(sock->sk, keepidle); 2250480aeb96SChristoph Hellwig tcp_sock_set_keepcnt(sock->sk, keepcnt); 2251b0d93ad5SChuck Lever 22528d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 2253c488aeadSChristoph Hellwig tcp_sock_set_user_timeout(sock->sk, timeo); 22548d1b8c62STrond Myklebust } 22558d1b8c62STrond Myklebust 22567196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22577196dbb0STrond Myklebust unsigned long connect_timeout, 22587196dbb0STrond Myklebust unsigned long reconnect_timeout) 22597196dbb0STrond Myklebust { 22607196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22617196dbb0STrond Myklebust struct rpc_timeout to; 22627196dbb0STrond Myklebust unsigned long initval; 22637196dbb0STrond Myklebust 2264b5e92419STrond Myklebust spin_lock(&xprt->transport_lock); 22657196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22667196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22677196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 22687196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22697196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 22707196dbb0STrond Myklebust /* Arbitrary lower limit */ 22717196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 22727196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 22737196dbb0STrond Myklebust to.to_initval = initval; 22747196dbb0STrond Myklebust to.to_maxval = initval; 22757196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 22767196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 22777196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 22787196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 22797196dbb0STrond Myklebust } 22807196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2281b5e92419STrond Myklebust spin_unlock(&xprt->transport_lock); 22827196dbb0STrond Myklebust } 22837196dbb0STrond Myklebust 22848d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 22858d1b8c62STrond Myklebust { 22868d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22878d1b8c62STrond Myklebust 22888d1b8c62STrond Myklebust if (!transport->inet) { 22898d1b8c62STrond Myklebust struct sock *sk = sock->sk; 22908d1b8c62STrond Myklebust 2291d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2292d88e4d82SNeilBrown * connections such as NFS mounts. 2293d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2294d88e4d82SNeilBrown * Individual applications, which have specific 2295d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2296d88e4d82SNeilBrown * MAY override this as appropriate. 2297d88e4d82SNeilBrown */ 229818d5ad62SChristoph Hellwig if (xs_addr(xprt)->sa_family == PF_INET6) { 229918d5ad62SChristoph Hellwig ip6_sock_set_addr_preferences(sk, 230018d5ad62SChristoph Hellwig IPV6_PREFER_SRC_PUBLIC); 230118d5ad62SChristoph Hellwig } 2302d88e4d82SNeilBrown 23038d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2304d737e5d4STrond Myklebust tcp_sock_set_nodelay(sk); 2305775f06abSTrond Myklebust 2306ea9afca8STrond Myklebust lock_sock(sk); 2307b0d93ad5SChuck Lever 23082a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23092a9e1cfaSTrond Myklebust 2310b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23115157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2312b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2313b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 23142118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 231598123866SBenjamin Coddington sk->sk_use_task_frag = false; 23163167e12cSChuck Lever 23173167e12cSChuck Lever /* socket options */ 23183167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 2319b0d93ad5SChuck Lever 2320b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2321b0d93ad5SChuck Lever 2322b0d93ad5SChuck Lever /* Reset to new socket */ 2323ee0ac0c2SChuck Lever transport->sock = sock; 2324ee0ac0c2SChuck Lever transport->inet = sk; 2325b0d93ad5SChuck Lever 2326ea9afca8STrond Myklebust release_sock(sk); 2327b0d93ad5SChuck Lever } 2328b0d93ad5SChuck Lever 232901d37c42STrond Myklebust if (!xprt_bound(xprt)) 2330280254b6STrond Myklebust return -ENOTCONN; 233101d37c42STrond Myklebust 2332a564b8f0SMel Gorman xs_set_memalloc(xprt); 2333a564b8f0SMel Gorman 2334ae053551STrond Myklebust xs_stream_start_connect(transport); 2335e1806c7bSTrond Myklebust 2336b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 23370fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2338280254b6STrond Myklebust return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 233916be2d20SChuck Lever } 234016be2d20SChuck Lever 234116be2d20SChuck Lever /** 2342b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2343acf0a39fSChuck Lever * @work: queued work item 234416be2d20SChuck Lever * 234516be2d20SChuck Lever * Invoked by a work queue tasklet. 234616be2d20SChuck Lever */ 2347cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 234816be2d20SChuck Lever { 2349cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2350cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 235116be2d20SChuck Lever struct socket *sock = transport->sock; 2352a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2353280254b6STrond Myklebust int status; 23548db55a03SNeilBrown unsigned int pflags = current->flags; 235516be2d20SChuck Lever 23568db55a03SNeilBrown if (atomic_read(&xprt->swapper)) 23578db55a03SNeilBrown current->flags |= PF_MEMALLOC; 235889f42494STrond Myklebust 235989f42494STrond Myklebust if (xprt_connected(xprt)) 236089f42494STrond Myklebust goto out; 236189f42494STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT, 236289f42494STrond Myklebust &transport->sock_state) || 236389f42494STrond Myklebust !sock) { 236489f42494STrond Myklebust xs_reset_transport(transport); 236589f42494STrond Myklebust sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family, 236689f42494STrond Myklebust SOCK_STREAM, IPPROTO_TCP, true); 2367b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2368280254b6STrond Myklebust xprt_wake_pending_tasks(xprt, PTR_ERR(sock)); 236916be2d20SChuck Lever goto out; 237016be2d20SChuck Lever } 23717d1e8255STrond Myklebust } 23727d1e8255STrond Myklebust 2373c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2374c740eff8SChuck Lever "%s (port %s)\n", xprt, 2375c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2376c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2377c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 237816be2d20SChuck Lever 237916be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 238040b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2381a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 238246121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 238346121cf7SChuck Lever sock->sk->sk_state); 2384a246b010SChuck Lever switch (status) { 23852a491991STrond Myklebust case 0: 2386a246b010SChuck Lever case -EINPROGRESS: 2387280254b6STrond Myklebust /* SYN_SENT! */ 238889f42494STrond Myklebust set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); 2389280254b6STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2390280254b6STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2391280254b6STrond Myklebust fallthrough; 2392a246b010SChuck Lever case -EALREADY: 2393280254b6STrond Myklebust goto out_unlock; 2394280254b6STrond Myklebust case -EADDRNOTAVAIL: 2395280254b6STrond Myklebust /* Source port number is unavailable. Try a new one! */ 2396280254b6STrond Myklebust transport->srcport = 0; 2397280254b6STrond Myklebust status = -EAGAIN; 2398280254b6STrond Myklebust break; 23999fcfe0c8STrond Myklebust case -EINVAL: 24009fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24019fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24029fcfe0c8STrond Myklebust */ 24033ed5e2a2STrond Myklebust case -ECONNREFUSED: 24043ed5e2a2STrond Myklebust case -ECONNRESET: 2405eb5b46faSTrond Myklebust case -ENETDOWN: 24063ed5e2a2STrond Myklebust case -ENETUNREACH: 24074ba161a7STrond Myklebust case -EHOSTUNREACH: 24083913c78cSTrond Myklebust case -EADDRINUSE: 24093601c4a9STrond Myklebust case -ENOBUFS: 2410280254b6STrond Myklebust break; 2411280254b6STrond Myklebust default: 2412280254b6STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2413280254b6STrond Myklebust __func__, status); 2414280254b6STrond Myklebust status = -EAGAIN; 2415280254b6STrond Myklebust } 2416280254b6STrond Myklebust 24178c71139dSCalum Mackay /* xs_tcp_force_close() wakes tasks with a fixed error code. 24188c71139dSCalum Mackay * We need to wake them first to ensure the correct error code. 24196ea44adcSNeilBrown */ 24206ea44adcSNeilBrown xprt_wake_pending_tasks(xprt, status); 24214efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 2422a246b010SChuck Lever out: 24232226feb6SChuck Lever xprt_clear_connecting(xprt); 2424280254b6STrond Myklebust out_unlock: 2425cf76785dSTrond Myklebust xprt_unlock_connect(xprt, transport); 24268db55a03SNeilBrown current_restore_flags(pflags, PF_MEMALLOC); 2427a246b010SChuck Lever } 2428a246b010SChuck Lever 242975eb6af7SChuck Lever /* 243075eb6af7SChuck Lever * Transfer the connected socket to @upper_transport, then mark that 243175eb6af7SChuck Lever * xprt CONNECTED. 243275eb6af7SChuck Lever */ 243375eb6af7SChuck Lever static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt, 243475eb6af7SChuck Lever struct sock_xprt *upper_transport) 243575eb6af7SChuck Lever { 243675eb6af7SChuck Lever struct sock_xprt *lower_transport = 243775eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 243875eb6af7SChuck Lever struct rpc_xprt *upper_xprt = &upper_transport->xprt; 243975eb6af7SChuck Lever 244075eb6af7SChuck Lever if (!upper_transport->inet) { 244175eb6af7SChuck Lever struct socket *sock = lower_transport->sock; 244275eb6af7SChuck Lever struct sock *sk = sock->sk; 244375eb6af7SChuck Lever 244475eb6af7SChuck Lever /* Avoid temporary address, they are bad for long-lived 244575eb6af7SChuck Lever * connections such as NFS mounts. 244675eb6af7SChuck Lever * RFC4941, section 3.6 suggests that: 244775eb6af7SChuck Lever * Individual applications, which have specific 244875eb6af7SChuck Lever * knowledge about the normal duration of connections, 244975eb6af7SChuck Lever * MAY override this as appropriate. 245075eb6af7SChuck Lever */ 245175eb6af7SChuck Lever if (xs_addr(upper_xprt)->sa_family == PF_INET6) 245275eb6af7SChuck Lever ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC); 245375eb6af7SChuck Lever 245475eb6af7SChuck Lever xs_tcp_set_socket_timeouts(upper_xprt, sock); 245575eb6af7SChuck Lever tcp_sock_set_nodelay(sk); 245675eb6af7SChuck Lever 245775eb6af7SChuck Lever lock_sock(sk); 245875eb6af7SChuck Lever 245975eb6af7SChuck Lever /* @sk is already connected, so it now has the RPC callbacks. 246075eb6af7SChuck Lever * Reach into @lower_transport to save the original ones. 246175eb6af7SChuck Lever */ 246275eb6af7SChuck Lever upper_transport->old_data_ready = lower_transport->old_data_ready; 246375eb6af7SChuck Lever upper_transport->old_state_change = lower_transport->old_state_change; 246475eb6af7SChuck Lever upper_transport->old_write_space = lower_transport->old_write_space; 246575eb6af7SChuck Lever upper_transport->old_error_report = lower_transport->old_error_report; 246675eb6af7SChuck Lever sk->sk_user_data = upper_xprt; 246775eb6af7SChuck Lever 246875eb6af7SChuck Lever /* socket options */ 246975eb6af7SChuck Lever sock_reset_flag(sk, SOCK_LINGER); 247075eb6af7SChuck Lever 247175eb6af7SChuck Lever xprt_clear_connected(upper_xprt); 247275eb6af7SChuck Lever 247375eb6af7SChuck Lever upper_transport->sock = sock; 247475eb6af7SChuck Lever upper_transport->inet = sk; 247575eb6af7SChuck Lever upper_transport->file = lower_transport->file; 247675eb6af7SChuck Lever 247775eb6af7SChuck Lever release_sock(sk); 247875eb6af7SChuck Lever 247975eb6af7SChuck Lever /* Reset lower_transport before shutting down its clnt */ 248075eb6af7SChuck Lever mutex_lock(&lower_transport->recv_mutex); 248175eb6af7SChuck Lever lower_transport->inet = NULL; 248275eb6af7SChuck Lever lower_transport->sock = NULL; 248375eb6af7SChuck Lever lower_transport->file = NULL; 248475eb6af7SChuck Lever 248575eb6af7SChuck Lever xprt_clear_connected(lower_xprt); 248675eb6af7SChuck Lever xs_sock_reset_connection_flags(lower_xprt); 248775eb6af7SChuck Lever xs_stream_reset_connect(lower_transport); 248875eb6af7SChuck Lever mutex_unlock(&lower_transport->recv_mutex); 248975eb6af7SChuck Lever } 249075eb6af7SChuck Lever 249175eb6af7SChuck Lever if (!xprt_bound(upper_xprt)) 249275eb6af7SChuck Lever return -ENOTCONN; 249375eb6af7SChuck Lever 249475eb6af7SChuck Lever xs_set_memalloc(upper_xprt); 249575eb6af7SChuck Lever 249675eb6af7SChuck Lever if (!xprt_test_and_set_connected(upper_xprt)) { 249775eb6af7SChuck Lever upper_xprt->connect_cookie++; 249875eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 249975eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 250075eb6af7SChuck Lever 250175eb6af7SChuck Lever upper_xprt->stat.connect_count++; 250275eb6af7SChuck Lever upper_xprt->stat.connect_time += (long)jiffies - 250375eb6af7SChuck Lever upper_xprt->stat.connect_start; 250475eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 250575eb6af7SChuck Lever } 250675eb6af7SChuck Lever return 0; 250775eb6af7SChuck Lever } 250875eb6af7SChuck Lever 250975eb6af7SChuck Lever /** 251075eb6af7SChuck Lever * xs_tls_handshake_done - TLS handshake completion handler 251175eb6af7SChuck Lever * @data: address of xprt to wake 251275eb6af7SChuck Lever * @status: status of handshake 251375eb6af7SChuck Lever * @peerid: serial number of key containing the remote's identity 251475eb6af7SChuck Lever * 251575eb6af7SChuck Lever */ 251675eb6af7SChuck Lever static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid) 251775eb6af7SChuck Lever { 251875eb6af7SChuck Lever struct rpc_xprt *lower_xprt = data; 251975eb6af7SChuck Lever struct sock_xprt *lower_transport = 252075eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 252175eb6af7SChuck Lever 252275eb6af7SChuck Lever lower_transport->xprt_err = status ? -EACCES : 0; 252375eb6af7SChuck Lever complete(&lower_transport->handshake_done); 252475eb6af7SChuck Lever xprt_put(lower_xprt); 252575eb6af7SChuck Lever } 252675eb6af7SChuck Lever 252775eb6af7SChuck Lever static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec) 252875eb6af7SChuck Lever { 252975eb6af7SChuck Lever struct sock_xprt *lower_transport = 253075eb6af7SChuck Lever container_of(lower_xprt, struct sock_xprt, xprt); 253175eb6af7SChuck Lever struct tls_handshake_args args = { 253275eb6af7SChuck Lever .ta_sock = lower_transport->sock, 253375eb6af7SChuck Lever .ta_done = xs_tls_handshake_done, 253475eb6af7SChuck Lever .ta_data = xprt_get(lower_xprt), 253575eb6af7SChuck Lever .ta_peername = lower_xprt->servername, 253675eb6af7SChuck Lever }; 253775eb6af7SChuck Lever struct sock *sk = lower_transport->inet; 253875eb6af7SChuck Lever int rc; 253975eb6af7SChuck Lever 254075eb6af7SChuck Lever init_completion(&lower_transport->handshake_done); 254175eb6af7SChuck Lever set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 254275eb6af7SChuck Lever lower_transport->xprt_err = -ETIMEDOUT; 254375eb6af7SChuck Lever switch (xprtsec->policy) { 254475eb6af7SChuck Lever case RPC_XPRTSEC_TLS_ANON: 254575eb6af7SChuck Lever rc = tls_client_hello_anon(&args, GFP_KERNEL); 254675eb6af7SChuck Lever if (rc) 254775eb6af7SChuck Lever goto out_put_xprt; 254875eb6af7SChuck Lever break; 254975eb6af7SChuck Lever case RPC_XPRTSEC_TLS_X509: 255075eb6af7SChuck Lever args.ta_my_cert = xprtsec->cert_serial; 255175eb6af7SChuck Lever args.ta_my_privkey = xprtsec->privkey_serial; 255275eb6af7SChuck Lever rc = tls_client_hello_x509(&args, GFP_KERNEL); 255375eb6af7SChuck Lever if (rc) 255475eb6af7SChuck Lever goto out_put_xprt; 255575eb6af7SChuck Lever break; 255675eb6af7SChuck Lever default: 255775eb6af7SChuck Lever rc = -EACCES; 255875eb6af7SChuck Lever goto out_put_xprt; 255975eb6af7SChuck Lever } 256075eb6af7SChuck Lever 256175eb6af7SChuck Lever rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done, 256275eb6af7SChuck Lever XS_TLS_HANDSHAKE_TO); 256375eb6af7SChuck Lever if (rc <= 0) { 256475eb6af7SChuck Lever if (!tls_handshake_cancel(sk)) { 256575eb6af7SChuck Lever if (rc == 0) 256675eb6af7SChuck Lever rc = -ETIMEDOUT; 256775eb6af7SChuck Lever goto out_put_xprt; 256875eb6af7SChuck Lever } 256975eb6af7SChuck Lever } 257075eb6af7SChuck Lever 257175eb6af7SChuck Lever rc = lower_transport->xprt_err; 257275eb6af7SChuck Lever 257375eb6af7SChuck Lever out: 257475eb6af7SChuck Lever xs_stream_reset_connect(lower_transport); 257575eb6af7SChuck Lever clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 257675eb6af7SChuck Lever return rc; 257775eb6af7SChuck Lever 257875eb6af7SChuck Lever out_put_xprt: 257975eb6af7SChuck Lever xprt_put(lower_xprt); 258075eb6af7SChuck Lever goto out; 258175eb6af7SChuck Lever } 258275eb6af7SChuck Lever 258375eb6af7SChuck Lever /** 258475eb6af7SChuck Lever * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket 258575eb6af7SChuck Lever * @work: queued work item 258675eb6af7SChuck Lever * 258775eb6af7SChuck Lever * Invoked by a work queue tasklet. 258875eb6af7SChuck Lever * 258975eb6af7SChuck Lever * For RPC-with-TLS, there is a two-stage connection process. 259075eb6af7SChuck Lever * 259175eb6af7SChuck Lever * The "upper-layer xprt" is visible to the RPC consumer. Once it has 259275eb6af7SChuck Lever * been marked connected, the consumer knows that a TCP connection and 259375eb6af7SChuck Lever * a TLS session have been established. 259475eb6af7SChuck Lever * 259575eb6af7SChuck Lever * A "lower-layer xprt", created in this function, handles the mechanics 259675eb6af7SChuck Lever * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and 259775eb6af7SChuck Lever * then driving the TLS handshake. Once all that is complete, the upper 259875eb6af7SChuck Lever * layer xprt is marked connected. 259975eb6af7SChuck Lever */ 260075eb6af7SChuck Lever static void xs_tcp_tls_setup_socket(struct work_struct *work) 260175eb6af7SChuck Lever { 260275eb6af7SChuck Lever struct sock_xprt *upper_transport = 260375eb6af7SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 260475eb6af7SChuck Lever struct rpc_clnt *upper_clnt = upper_transport->clnt; 260575eb6af7SChuck Lever struct rpc_xprt *upper_xprt = &upper_transport->xprt; 260675eb6af7SChuck Lever struct rpc_create_args args = { 260775eb6af7SChuck Lever .net = upper_xprt->xprt_net, 260875eb6af7SChuck Lever .protocol = upper_xprt->prot, 260975eb6af7SChuck Lever .address = (struct sockaddr *)&upper_xprt->addr, 261075eb6af7SChuck Lever .addrsize = upper_xprt->addrlen, 261175eb6af7SChuck Lever .timeout = upper_clnt->cl_timeout, 261275eb6af7SChuck Lever .servername = upper_xprt->servername, 261375eb6af7SChuck Lever .program = upper_clnt->cl_program, 261475eb6af7SChuck Lever .prognumber = upper_clnt->cl_prog, 261575eb6af7SChuck Lever .version = upper_clnt->cl_vers, 261675eb6af7SChuck Lever .authflavor = RPC_AUTH_TLS, 261775eb6af7SChuck Lever .cred = upper_clnt->cl_cred, 261875eb6af7SChuck Lever .xprtsec = { 261975eb6af7SChuck Lever .policy = RPC_XPRTSEC_NONE, 262075eb6af7SChuck Lever }, 262175eb6af7SChuck Lever }; 262275eb6af7SChuck Lever unsigned int pflags = current->flags; 262375eb6af7SChuck Lever struct rpc_clnt *lower_clnt; 262475eb6af7SChuck Lever struct rpc_xprt *lower_xprt; 262575eb6af7SChuck Lever int status; 262675eb6af7SChuck Lever 262775eb6af7SChuck Lever if (atomic_read(&upper_xprt->swapper)) 262875eb6af7SChuck Lever current->flags |= PF_MEMALLOC; 262975eb6af7SChuck Lever 263075eb6af7SChuck Lever xs_stream_start_connect(upper_transport); 263175eb6af7SChuck Lever 263275eb6af7SChuck Lever /* This implicitly sends an RPC_AUTH_TLS probe */ 263375eb6af7SChuck Lever lower_clnt = rpc_create(&args); 263475eb6af7SChuck Lever if (IS_ERR(lower_clnt)) { 263575eb6af7SChuck Lever trace_rpc_tls_unavailable(upper_clnt, upper_xprt); 263675eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 263775eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 263875eb6af7SChuck Lever xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt)); 263975eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 264075eb6af7SChuck Lever goto out_unlock; 264175eb6af7SChuck Lever } 264275eb6af7SChuck Lever 264375eb6af7SChuck Lever /* RPC_AUTH_TLS probe was successful. Try a TLS handshake on 264475eb6af7SChuck Lever * the lower xprt. 264575eb6af7SChuck Lever */ 264675eb6af7SChuck Lever rcu_read_lock(); 264775eb6af7SChuck Lever lower_xprt = rcu_dereference(lower_clnt->cl_xprt); 264875eb6af7SChuck Lever rcu_read_unlock(); 264975eb6af7SChuck Lever status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec); 265075eb6af7SChuck Lever if (status) { 265175eb6af7SChuck Lever trace_rpc_tls_not_started(upper_clnt, upper_xprt); 265275eb6af7SChuck Lever goto out_close; 265375eb6af7SChuck Lever } 265475eb6af7SChuck Lever 265575eb6af7SChuck Lever status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport); 265675eb6af7SChuck Lever if (status) 265775eb6af7SChuck Lever goto out_close; 265875eb6af7SChuck Lever 265975eb6af7SChuck Lever trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0); 266075eb6af7SChuck Lever if (!xprt_test_and_set_connected(upper_xprt)) { 266175eb6af7SChuck Lever upper_xprt->connect_cookie++; 266275eb6af7SChuck Lever clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 266375eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 266475eb6af7SChuck Lever 266575eb6af7SChuck Lever upper_xprt->stat.connect_count++; 266675eb6af7SChuck Lever upper_xprt->stat.connect_time += (long)jiffies - 266775eb6af7SChuck Lever upper_xprt->stat.connect_start; 266875eb6af7SChuck Lever xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 266975eb6af7SChuck Lever } 267075eb6af7SChuck Lever rpc_shutdown_client(lower_clnt); 267175eb6af7SChuck Lever 267275eb6af7SChuck Lever out_unlock: 267375eb6af7SChuck Lever current_restore_flags(pflags, PF_MEMALLOC); 267475eb6af7SChuck Lever upper_transport->clnt = NULL; 267575eb6af7SChuck Lever xprt_unlock_connect(upper_xprt, upper_transport); 267675eb6af7SChuck Lever return; 267775eb6af7SChuck Lever 267875eb6af7SChuck Lever out_close: 267975eb6af7SChuck Lever rpc_shutdown_client(lower_clnt); 268075eb6af7SChuck Lever 268175eb6af7SChuck Lever /* xprt_force_disconnect() wakes tasks with a fixed tk_status code. 268275eb6af7SChuck Lever * Wake them first here to ensure they get our tk_status code. 268375eb6af7SChuck Lever */ 268475eb6af7SChuck Lever xprt_wake_pending_tasks(upper_xprt, status); 268575eb6af7SChuck Lever xs_tcp_force_close(upper_xprt); 268675eb6af7SChuck Lever xprt_clear_connecting(upper_xprt); 268775eb6af7SChuck Lever goto out_unlock; 268875eb6af7SChuck Lever } 268975eb6af7SChuck Lever 269068e220bdSChuck Lever /** 26919903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 26921b092092STrond Myklebust * @xprt: pointer to transport structure 26939903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 26949903cd1cSChuck Lever * 26959903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 269603bf4b70SChuck Lever * 269703bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 269803bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 269903bf4b70SChuck Lever * socket on a privileged port. 270003bf4b70SChuck Lever * 270103bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 270203bf4b70SChuck Lever * retry floods (hard mounts). 27039903cd1cSChuck Lever */ 27041b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2705a246b010SChuck Lever { 2706ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 270702910177STrond Myklebust unsigned long delay = 0; 2708a246b010SChuck Lever 2709718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2710718ba5b8STrond Myklebust 271189f42494STrond Myklebust if (transport->sock != NULL) { 271246121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 271389f42494STrond Myklebust "seconds\n", xprt, xprt->reestablish_timeout / HZ); 271499b1a4c3STrond Myklebust 2715675dd90aSChuck Lever delay = xprt_reconnect_delay(xprt); 2716675dd90aSChuck Lever xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 271702910177STrond Myklebust 271802910177STrond Myklebust } else 27199903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 272002910177STrond Myklebust 272175eb6af7SChuck Lever transport->clnt = task->tk_client; 272240a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 272302910177STrond Myklebust &transport->connect_worker, 272402910177STrond Myklebust delay); 2725a246b010SChuck Lever } 2726a246b010SChuck Lever 27274f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport) 27284f8943f8STrond Myklebust { 27294f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 27304f8943f8STrond Myklebust xs_tcp_force_close(&transport->xprt); 27314f8943f8STrond Myklebust } 27324f8943f8STrond Myklebust 27334f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport) 27344f8943f8STrond Myklebust { 27354f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 27364f8943f8STrond Myklebust xprt_write_space(&transport->xprt); 27374f8943f8STrond Myklebust } 27384f8943f8STrond Myklebust 27394f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport) 27404f8943f8STrond Myklebust { 27414f8943f8STrond Myklebust int sockerr; 27424f8943f8STrond Myklebust 27434f8943f8STrond Myklebust if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 27444f8943f8STrond Myklebust return; 27454f8943f8STrond Myklebust mutex_lock(&transport->recv_mutex); 27464f8943f8STrond Myklebust if (transport->sock == NULL) 27474f8943f8STrond Myklebust goto out; 27484f8943f8STrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 27494f8943f8STrond Myklebust goto out; 2750af84537dSBenjamin Coddington sockerr = xchg(&transport->xprt_err, 0); 27514f8943f8STrond Myklebust if (sockerr < 0) 27524f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, sockerr); 27534f8943f8STrond Myklebust out: 27544f8943f8STrond Myklebust mutex_unlock(&transport->recv_mutex); 27554f8943f8STrond Myklebust } 27564f8943f8STrond Myklebust 27574f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport) 27584f8943f8STrond Myklebust { 27594f8943f8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 27604f8943f8STrond Myklebust xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 27614f8943f8STrond Myklebust } 27624f8943f8STrond Myklebust 27634f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work) 27644f8943f8STrond Myklebust { 27654f8943f8STrond Myklebust struct sock_xprt *transport = container_of(work, 27664f8943f8STrond Myklebust struct sock_xprt, error_worker); 27674f8943f8STrond Myklebust 27684f8943f8STrond Myklebust xs_wake_disconnect(transport); 27694f8943f8STrond Myklebust xs_wake_write(transport); 27704f8943f8STrond Myklebust xs_wake_error(transport); 27714f8943f8STrond Myklebust xs_wake_pending(transport); 27724f8943f8STrond Myklebust } 27734f8943f8STrond Myklebust 2774262ca07dSChuck Lever /** 277512b20ce3SBhaskar Chowdhury * xs_local_print_stats - display AF_LOCAL socket-specific stats 2776176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2777176e21eeSChuck Lever * @seq: output file 2778176e21eeSChuck Lever * 2779176e21eeSChuck Lever */ 2780176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2781176e21eeSChuck Lever { 2782176e21eeSChuck Lever long idle_time = 0; 2783176e21eeSChuck Lever 2784176e21eeSChuck Lever if (xprt_connected(xprt)) 2785176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2786176e21eeSChuck Lever 2787176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 278815a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2789176e21eeSChuck Lever xprt->stat.bind_count, 2790176e21eeSChuck Lever xprt->stat.connect_count, 27918440a886SChuck Lever xprt->stat.connect_time / HZ, 2792176e21eeSChuck Lever idle_time, 2793176e21eeSChuck Lever xprt->stat.sends, 2794176e21eeSChuck Lever xprt->stat.recvs, 2795176e21eeSChuck Lever xprt->stat.bad_xids, 2796176e21eeSChuck Lever xprt->stat.req_u, 279715a45206SAndy Adamson xprt->stat.bklog_u, 279815a45206SAndy Adamson xprt->stat.max_slots, 279915a45206SAndy Adamson xprt->stat.sending_u, 280015a45206SAndy Adamson xprt->stat.pending_u); 2801176e21eeSChuck Lever } 2802176e21eeSChuck Lever 2803176e21eeSChuck Lever /** 280412b20ce3SBhaskar Chowdhury * xs_udp_print_stats - display UDP socket-specific stats 2805262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2806262ca07dSChuck Lever * @seq: output file 2807262ca07dSChuck Lever * 2808262ca07dSChuck Lever */ 2809262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2810262ca07dSChuck Lever { 2811c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2812c8475461SChuck Lever 281315a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 281415a45206SAndy Adamson "%lu %llu %llu\n", 2815fbfffbd5SChuck Lever transport->srcport, 2816262ca07dSChuck Lever xprt->stat.bind_count, 2817262ca07dSChuck Lever xprt->stat.sends, 2818262ca07dSChuck Lever xprt->stat.recvs, 2819262ca07dSChuck Lever xprt->stat.bad_xids, 2820262ca07dSChuck Lever xprt->stat.req_u, 282115a45206SAndy Adamson xprt->stat.bklog_u, 282215a45206SAndy Adamson xprt->stat.max_slots, 282315a45206SAndy Adamson xprt->stat.sending_u, 282415a45206SAndy Adamson xprt->stat.pending_u); 2825262ca07dSChuck Lever } 2826262ca07dSChuck Lever 2827262ca07dSChuck Lever /** 282812b20ce3SBhaskar Chowdhury * xs_tcp_print_stats - display TCP socket-specific stats 2829262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2830262ca07dSChuck Lever * @seq: output file 2831262ca07dSChuck Lever * 2832262ca07dSChuck Lever */ 2833262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2834262ca07dSChuck Lever { 2835c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2836262ca07dSChuck Lever long idle_time = 0; 2837262ca07dSChuck Lever 2838262ca07dSChuck Lever if (xprt_connected(xprt)) 2839262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2840262ca07dSChuck Lever 284115a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 284215a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2843fbfffbd5SChuck Lever transport->srcport, 2844262ca07dSChuck Lever xprt->stat.bind_count, 2845262ca07dSChuck Lever xprt->stat.connect_count, 28468440a886SChuck Lever xprt->stat.connect_time / HZ, 2847262ca07dSChuck Lever idle_time, 2848262ca07dSChuck Lever xprt->stat.sends, 2849262ca07dSChuck Lever xprt->stat.recvs, 2850262ca07dSChuck Lever xprt->stat.bad_xids, 2851262ca07dSChuck Lever xprt->stat.req_u, 285215a45206SAndy Adamson xprt->stat.bklog_u, 285315a45206SAndy Adamson xprt->stat.max_slots, 285415a45206SAndy Adamson xprt->stat.sending_u, 285515a45206SAndy Adamson xprt->stat.pending_u); 2856262ca07dSChuck Lever } 2857262ca07dSChuck Lever 28584cfc7e60SRahul Iyer /* 28594cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 28604cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 28614cfc7e60SRahul Iyer * to use the server side send routines. 28624cfc7e60SRahul Iyer */ 28635fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 28644cfc7e60SRahul Iyer { 28655fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 28665fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 28674cfc7e60SRahul Iyer struct page *page; 28684cfc7e60SRahul Iyer struct rpc_buffer *buf; 28694cfc7e60SRahul Iyer 28705fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 28715fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 28725fe6eaa1SChuck Lever size); 28735fe6eaa1SChuck Lever return -EINVAL; 28745fe6eaa1SChuck Lever } 28754cfc7e60SRahul Iyer 2876b2648015STrond Myklebust page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 28774cfc7e60SRahul Iyer if (!page) 28785fe6eaa1SChuck Lever return -ENOMEM; 28794cfc7e60SRahul Iyer 28804cfc7e60SRahul Iyer buf = page_address(page); 28814cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 28824cfc7e60SRahul Iyer 28835fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 288418e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 28855fe6eaa1SChuck Lever return 0; 28864cfc7e60SRahul Iyer } 28874cfc7e60SRahul Iyer 28884cfc7e60SRahul Iyer /* 28894cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 28904cfc7e60SRahul Iyer */ 28913435c74aSChuck Lever static void bc_free(struct rpc_task *task) 28924cfc7e60SRahul Iyer { 28933435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 28944cfc7e60SRahul Iyer struct rpc_buffer *buf; 28954cfc7e60SRahul Iyer 28964cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 28974cfc7e60SRahul Iyer free_page((unsigned long)buf); 28984cfc7e60SRahul Iyer } 28994cfc7e60SRahul Iyer 29004cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 29014cfc7e60SRahul Iyer { 2902da1661b9SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 29034cfc7e60SRahul Iyer struct sock_xprt *transport = 2904067fb11bSChuck Lever container_of(req->rq_xprt, struct sock_xprt, xprt); 2905067fb11bSChuck Lever struct msghdr msg = { 2906da1661b9SChuck Lever .msg_flags = 0, 2907067fb11bSChuck Lever }; 2908067fb11bSChuck Lever rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2909da1661b9SChuck Lever (u32)xdr->len); 2910da1661b9SChuck Lever unsigned int sent = 0; 2911da1661b9SChuck Lever int err; 29124cfc7e60SRahul Iyer 29138729aabaSChuck Lever req->rq_xtime = ktime_get(); 2914ff053dbbSTrond Myklebust err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 2915ff053dbbSTrond Myklebust if (err < 0) 2916ff053dbbSTrond Myklebust return err; 2917da1661b9SChuck Lever err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); 2918da1661b9SChuck Lever xdr_free_bvec(xdr); 2919da1661b9SChuck Lever if (err < 0 || sent != (xdr->len + sizeof(marker))) 2920067fb11bSChuck Lever return -EAGAIN; 2921da1661b9SChuck Lever return sent; 29224cfc7e60SRahul Iyer } 29234cfc7e60SRahul Iyer 2924ca4faf54SChuck Lever /** 2925ca4faf54SChuck Lever * bc_send_request - Send a backchannel Call on a TCP socket 2926ca4faf54SChuck Lever * @req: rpc_rqst containing Call message to be sent 2927ca4faf54SChuck Lever * 2928ca4faf54SChuck Lever * xpt_mutex ensures @rqstp's whole message is written to the socket 2929ca4faf54SChuck Lever * without interruption. 2930ca4faf54SChuck Lever * 2931ca4faf54SChuck Lever * Return values: 2932ca4faf54SChuck Lever * %0 if the message was sent successfully 2933ca4faf54SChuck Lever * %ENOTCONN if the message was not sent 29344cfc7e60SRahul Iyer */ 2935adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req) 29364cfc7e60SRahul Iyer { 29374cfc7e60SRahul Iyer struct svc_xprt *xprt; 29387fc56136SAndrzej Hajda int len; 29394cfc7e60SRahul Iyer 29404cfc7e60SRahul Iyer /* 29414cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 29424cfc7e60SRahul Iyer */ 29434cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 29444cfc7e60SRahul Iyer 29454cfc7e60SRahul Iyer /* 29464cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 29474cfc7e60SRahul Iyer * with the fore channel 29484cfc7e60SRahul Iyer */ 2949c544577dSTrond Myklebust mutex_lock(&xprt->xpt_mutex); 29504cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 29514cfc7e60SRahul Iyer len = -ENOTCONN; 29524cfc7e60SRahul Iyer else 29534cfc7e60SRahul Iyer len = bc_sendto(req); 29544cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 29554cfc7e60SRahul Iyer 29564cfc7e60SRahul Iyer if (len > 0) 29574cfc7e60SRahul Iyer len = 0; 29584cfc7e60SRahul Iyer 29594cfc7e60SRahul Iyer return len; 29604cfc7e60SRahul Iyer } 29614cfc7e60SRahul Iyer 29624cfc7e60SRahul Iyer /* 29634cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 29644cfc7e60SRahul Iyer */ 29654cfc7e60SRahul Iyer 29664cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 29674cfc7e60SRahul Iyer { 29686221f1d9SChuck Lever xprt_disconnect_done(xprt); 29694cfc7e60SRahul Iyer } 29704cfc7e60SRahul Iyer 29714cfc7e60SRahul Iyer /* 29724cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 29734cfc7e60SRahul Iyer * initiated, we do nothing 29744cfc7e60SRahul Iyer */ 29754cfc7e60SRahul Iyer 29764cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 29774cfc7e60SRahul Iyer { 297847f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 297947f72efaSKinglong Mee 298047f72efaSKinglong Mee xs_xprt_free(xprt); 298147f72efaSKinglong Mee module_put(THIS_MODULE); 29824cfc7e60SRahul Iyer } 29834cfc7e60SRahul Iyer 2984d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = { 2985176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 29864cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 2987f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2988a9cde23aSChuck Lever .free_slot = xprt_free_slot, 2989176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2990176e21eeSChuck Lever .set_port = xs_local_set_port, 2991dc107402SJ. Bruce Fields .connect = xs_local_connect, 2992176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2993176e21eeSChuck Lever .buf_free = rpc_free, 2994550aebfeSTrond Myklebust .prepare_request = xs_stream_prepare_request, 2995176e21eeSChuck Lever .send_request = xs_local_send_request, 29968ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 2997176e21eeSChuck Lever .close = xs_close, 2998a1311d87STrond Myklebust .destroy = xs_destroy, 2999176e21eeSChuck Lever .print_stats = xs_local_print_stats, 3000d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3001d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 3002176e21eeSChuck Lever }; 3003176e21eeSChuck Lever 3004d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = { 300543118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 300612a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 300749e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 3008f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 3009a9cde23aSChuck Lever .free_slot = xprt_free_slot, 301045160d62SChuck Lever .rpcbind = rpcb_getport_async, 301192200412SChuck Lever .set_port = xs_set_port, 30129903cd1cSChuck Lever .connect = xs_connect, 3013421ab1beSTrond Myklebust .get_srcaddr = xs_sock_srcaddr, 3014421ab1beSTrond Myklebust .get_srcport = xs_sock_srcport, 301502107148SChuck Lever .buf_alloc = rpc_malloc, 301602107148SChuck Lever .buf_free = rpc_free, 3017262965f5SChuck Lever .send_request = xs_udp_send_request, 30188ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 301946c0ee8bSChuck Lever .timer = xs_udp_timer, 3020a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 3021262965f5SChuck Lever .close = xs_close, 3022262965f5SChuck Lever .destroy = xs_destroy, 3023262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 3024d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3025d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30264a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 3027262965f5SChuck Lever }; 3028262965f5SChuck Lever 3029d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = { 303012a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 30314cd34e7cSTrond Myklebust .release_xprt = xprt_release_xprt, 303236bd7de9STrond Myklebust .alloc_slot = xprt_alloc_slot, 3033a9cde23aSChuck Lever .free_slot = xprt_free_slot, 303445160d62SChuck Lever .rpcbind = rpcb_getport_async, 303592200412SChuck Lever .set_port = xs_set_port, 30360b9e7943STrond Myklebust .connect = xs_connect, 3037421ab1beSTrond Myklebust .get_srcaddr = xs_sock_srcaddr, 3038421ab1beSTrond Myklebust .get_srcport = xs_sock_srcport, 303902107148SChuck Lever .buf_alloc = rpc_malloc, 304002107148SChuck Lever .buf_free = rpc_free, 3041277e4ab7STrond Myklebust .prepare_request = xs_stream_prepare_request, 3042262965f5SChuck Lever .send_request = xs_tcp_send_request, 30438ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 3044c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 30459903cd1cSChuck Lever .destroy = xs_destroy, 30467196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 3047262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 3048d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3049d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30504a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 305142e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 305242e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 30536b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 30547402a4feSTrond Myklebust .bc_num_slots = xprt_bc_max_slots, 305542e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 305642e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 305742e5c3e2SChuck Lever #endif 3058a246b010SChuck Lever }; 3059a246b010SChuck Lever 30604cfc7e60SRahul Iyer /* 30614cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 30624cfc7e60SRahul Iyer */ 30634cfc7e60SRahul Iyer 3064d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = { 30654cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 30664cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 306784e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 3068a9cde23aSChuck Lever .free_slot = xprt_free_slot, 30694cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 30704cfc7e60SRahul Iyer .buf_free = bc_free, 30714cfc7e60SRahul Iyer .send_request = bc_send_request, 30728ba6a92dSTrond Myklebust .wait_for_reply_request = xprt_wait_for_reply_request_def, 30734cfc7e60SRahul Iyer .close = bc_close, 30744cfc7e60SRahul Iyer .destroy = bc_destroy, 30754cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 3076d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 3077d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 30784a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 30794cfc7e60SRahul Iyer }; 30804cfc7e60SRahul Iyer 308192476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 308292476850SChuck Lever { 308392476850SChuck Lever static const struct sockaddr_in sin = { 308492476850SChuck Lever .sin_family = AF_INET, 308592476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 308692476850SChuck Lever }; 308792476850SChuck Lever static const struct sockaddr_in6 sin6 = { 308892476850SChuck Lever .sin6_family = AF_INET6, 308992476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 309092476850SChuck Lever }; 309192476850SChuck Lever 309292476850SChuck Lever switch (family) { 3093176e21eeSChuck Lever case AF_LOCAL: 3094176e21eeSChuck Lever break; 309592476850SChuck Lever case AF_INET: 309692476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 309792476850SChuck Lever break; 309892476850SChuck Lever case AF_INET6: 309992476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 310092476850SChuck Lever break; 310192476850SChuck Lever default: 310292476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 310392476850SChuck Lever return -EAFNOSUPPORT; 310492476850SChuck Lever } 310592476850SChuck Lever return 0; 310692476850SChuck Lever } 310792476850SChuck Lever 31083c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 3109d9ba131dSTrond Myklebust unsigned int slot_table_size, 3110d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 3111c8541ecdSChuck Lever { 3112c8541ecdSChuck Lever struct rpc_xprt *xprt; 3113ffc2e518SChuck Lever struct sock_xprt *new; 3114c8541ecdSChuck Lever 311596802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 3116c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 3117c8541ecdSChuck Lever return ERR_PTR(-EBADF); 3118c8541ecdSChuck Lever } 3119c8541ecdSChuck Lever 3120d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 3121d9ba131dSTrond Myklebust max_slot_table_size); 3122bd1722d4SPavel Emelyanov if (xprt == NULL) { 312346121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 312446121cf7SChuck Lever "rpc_xprt\n"); 3125c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 3126c8541ecdSChuck Lever } 3127c8541ecdSChuck Lever 3128bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 3129edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 313096802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 313196802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 3132d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 3133fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 313492476850SChuck Lever else { 313592476850SChuck Lever int err; 313692476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 313792476850SChuck Lever (struct sockaddr *)&new->srcaddr); 31382aa13531SStanislav Kinsbursky if (err != 0) { 31392aa13531SStanislav Kinsbursky xprt_free(xprt); 314092476850SChuck Lever return ERR_PTR(err); 314192476850SChuck Lever } 31422aa13531SStanislav Kinsbursky } 3143c8541ecdSChuck Lever 3144c8541ecdSChuck Lever return xprt; 3145c8541ecdSChuck Lever } 3146c8541ecdSChuck Lever 3147176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 3148176e21eeSChuck Lever .to_initval = 10 * HZ, 3149176e21eeSChuck Lever .to_maxval = 10 * HZ, 3150176e21eeSChuck Lever .to_retries = 2, 3151176e21eeSChuck Lever }; 3152176e21eeSChuck Lever 3153176e21eeSChuck Lever /** 3154176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 3155176e21eeSChuck Lever * @args: rpc transport creation arguments 3156176e21eeSChuck Lever * 3157176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 3158176e21eeSChuck Lever */ 3159176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 3160176e21eeSChuck Lever { 3161176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 3162176e21eeSChuck Lever struct sock_xprt *transport; 3163176e21eeSChuck Lever struct rpc_xprt *xprt; 3164176e21eeSChuck Lever struct rpc_xprt *ret; 3165176e21eeSChuck Lever 3166d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3167d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 3168176e21eeSChuck Lever if (IS_ERR(xprt)) 3169176e21eeSChuck Lever return xprt; 3170176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3171176e21eeSChuck Lever 3172176e21eeSChuck Lever xprt->prot = 0; 3173d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_local_transport; 3174176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3175176e21eeSChuck Lever 3176176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 3177176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 3178176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3179176e21eeSChuck Lever 3180176e21eeSChuck Lever xprt->ops = &xs_local_ops; 3181176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 3182176e21eeSChuck Lever 3183550aebfeSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 31844f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3185550aebfeSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 318693dc41bdSNeilBrown 3187176e21eeSChuck Lever switch (sun->sun_family) { 3188176e21eeSChuck Lever case AF_LOCAL: 31894388ce05SNeilBrown if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') { 3190176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 3191176e21eeSChuck Lever sun->sun_path); 3192176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 3193176e21eeSChuck Lever goto out_err; 3194176e21eeSChuck Lever } 3195176e21eeSChuck Lever xprt_set_bound(xprt); 3196176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 3197176e21eeSChuck Lever break; 3198176e21eeSChuck Lever default: 3199176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 3200176e21eeSChuck Lever goto out_err; 3201176e21eeSChuck Lever } 3202176e21eeSChuck Lever 3203176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 3204176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 3205176e21eeSChuck Lever 3206176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 3207176e21eeSChuck Lever return xprt; 3208176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 3209176e21eeSChuck Lever out_err: 3210315f3812SKinglong Mee xs_xprt_free(xprt); 3211176e21eeSChuck Lever return ret; 3212176e21eeSChuck Lever } 3213176e21eeSChuck Lever 32142881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 32152881ae74STrond Myklebust .to_initval = 5 * HZ, 32162881ae74STrond Myklebust .to_maxval = 30 * HZ, 32172881ae74STrond Myklebust .to_increment = 5 * HZ, 32182881ae74STrond Myklebust .to_retries = 5, 32192881ae74STrond Myklebust }; 32202881ae74STrond Myklebust 32219903cd1cSChuck Lever /** 32229903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 322396802a09SFrank van Maarseveen * @args: rpc transport creation arguments 32249903cd1cSChuck Lever * 32259903cd1cSChuck Lever */ 3226483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 3227a246b010SChuck Lever { 32288f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3229c8541ecdSChuck Lever struct rpc_xprt *xprt; 3230c8475461SChuck Lever struct sock_xprt *transport; 32310a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3232a246b010SChuck Lever 3233d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 3234d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 3235c8541ecdSChuck Lever if (IS_ERR(xprt)) 3236c8541ecdSChuck Lever return xprt; 3237c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3238a246b010SChuck Lever 3239ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 3240d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_udp_transport; 3241a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 3242a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3243a246b010SChuck Lever 324403bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 324503bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 324603bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3247a246b010SChuck Lever 3248262965f5SChuck Lever xprt->ops = &xs_udp_ops; 3249a246b010SChuck Lever 3250ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 3251a246b010SChuck Lever 3252f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 32534f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3254edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3255edc1b01cSTrond Myklebust 32568f9d5b1aSChuck Lever switch (addr->sa_family) { 32578f9d5b1aSChuck Lever case AF_INET: 32588f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 32598f9d5b1aSChuck Lever xprt_set_bound(xprt); 32608f9d5b1aSChuck Lever 32619dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 32628f9d5b1aSChuck Lever break; 32638f9d5b1aSChuck Lever case AF_INET6: 32648f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 32658f9d5b1aSChuck Lever xprt_set_bound(xprt); 32668f9d5b1aSChuck Lever 32679dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 32688f9d5b1aSChuck Lever break; 32698f9d5b1aSChuck Lever default: 32700a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 32710a68b0beSJ. Bruce Fields goto out_err; 32728f9d5b1aSChuck Lever } 32738f9d5b1aSChuck Lever 3274c740eff8SChuck Lever if (xprt_bound(xprt)) 3275c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3276c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3277c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3278c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3279c740eff8SChuck Lever else 3280c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3281c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3282c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3283edb267a6SChuck Lever 3284bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3285c8541ecdSChuck Lever return xprt; 32860a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 32870a68b0beSJ. Bruce Fields out_err: 3288315f3812SKinglong Mee xs_xprt_free(xprt); 32890a68b0beSJ. Bruce Fields return ret; 3290a246b010SChuck Lever } 3291a246b010SChuck Lever 32922881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 32932881ae74STrond Myklebust .to_initval = 60 * HZ, 32942881ae74STrond Myklebust .to_maxval = 60 * HZ, 32952881ae74STrond Myklebust .to_retries = 2, 32962881ae74STrond Myklebust }; 32972881ae74STrond Myklebust 32989903cd1cSChuck Lever /** 32999903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 330096802a09SFrank van Maarseveen * @args: rpc transport creation arguments 33019903cd1cSChuck Lever * 33029903cd1cSChuck Lever */ 3303483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3304a246b010SChuck Lever { 33058f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3306c8541ecdSChuck Lever struct rpc_xprt *xprt; 3307c8475461SChuck Lever struct sock_xprt *transport; 33080a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3309b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3310b7993cebSTrond Myklebust 3311b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3312b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3313a246b010SChuck Lever 3314d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3315b7993cebSTrond Myklebust max_slot_table_size); 3316c8541ecdSChuck Lever if (IS_ERR(xprt)) 3317c8541ecdSChuck Lever return xprt; 3318c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3319a246b010SChuck Lever 3320ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3321d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_tcp_transport; 3322808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3323a246b010SChuck Lever 332403bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 332503bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 332603bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3327a246b010SChuck Lever 3328262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3329ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3330a246b010SChuck Lever 33313851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 33327196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 33337196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 33343851f1cdSTrond Myklebust 3335c50b8ee0STrond Myklebust INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 33364f8943f8STrond Myklebust INIT_WORK(&transport->error_worker, xs_error_handle); 3337edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3338edc1b01cSTrond Myklebust 33398f9d5b1aSChuck Lever switch (addr->sa_family) { 33408f9d5b1aSChuck Lever case AF_INET: 33418f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 33428f9d5b1aSChuck Lever xprt_set_bound(xprt); 33438f9d5b1aSChuck Lever 33449dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 33458f9d5b1aSChuck Lever break; 33468f9d5b1aSChuck Lever case AF_INET6: 33478f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 33488f9d5b1aSChuck Lever xprt_set_bound(xprt); 33498f9d5b1aSChuck Lever 33509dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 33518f9d5b1aSChuck Lever break; 33528f9d5b1aSChuck Lever default: 33530a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 33540a68b0beSJ. Bruce Fields goto out_err; 33558f9d5b1aSChuck Lever } 33568f9d5b1aSChuck Lever 3357c740eff8SChuck Lever if (xprt_bound(xprt)) 3358c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3359c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3360c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3361c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3362c740eff8SChuck Lever else 3363c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3364c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3365c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3366c740eff8SChuck Lever 3367bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3368c8541ecdSChuck Lever return xprt; 33690a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 33700a68b0beSJ. Bruce Fields out_err: 3371315f3812SKinglong Mee xs_xprt_free(xprt); 33720a68b0beSJ. Bruce Fields return ret; 3373a246b010SChuck Lever } 3374282b32e1SChuck Lever 3375f300babaSAlexandros Batsakis /** 337675eb6af7SChuck Lever * xs_setup_tcp_tls - Set up transport to use a TCP with TLS 337775eb6af7SChuck Lever * @args: rpc transport creation arguments 337875eb6af7SChuck Lever * 337975eb6af7SChuck Lever */ 338075eb6af7SChuck Lever static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args) 338175eb6af7SChuck Lever { 338275eb6af7SChuck Lever struct sockaddr *addr = args->dstaddr; 338375eb6af7SChuck Lever struct rpc_xprt *xprt; 338475eb6af7SChuck Lever struct sock_xprt *transport; 338575eb6af7SChuck Lever struct rpc_xprt *ret; 338675eb6af7SChuck Lever unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 338775eb6af7SChuck Lever 338875eb6af7SChuck Lever if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 338975eb6af7SChuck Lever max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 339075eb6af7SChuck Lever 339175eb6af7SChuck Lever xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 339275eb6af7SChuck Lever max_slot_table_size); 339375eb6af7SChuck Lever if (IS_ERR(xprt)) 339475eb6af7SChuck Lever return xprt; 339575eb6af7SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 339675eb6af7SChuck Lever 339775eb6af7SChuck Lever xprt->prot = IPPROTO_TCP; 339875eb6af7SChuck Lever xprt->xprt_class = &xs_tcp_transport; 339975eb6af7SChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 340075eb6af7SChuck Lever 340175eb6af7SChuck Lever xprt->bind_timeout = XS_BIND_TO; 340275eb6af7SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 340375eb6af7SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 340475eb6af7SChuck Lever 340575eb6af7SChuck Lever xprt->ops = &xs_tcp_ops; 340675eb6af7SChuck Lever xprt->timeout = &xs_tcp_default_timeout; 340775eb6af7SChuck Lever 340875eb6af7SChuck Lever xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 340975eb6af7SChuck Lever xprt->connect_timeout = xprt->timeout->to_initval * 341075eb6af7SChuck Lever (xprt->timeout->to_retries + 1); 341175eb6af7SChuck Lever 341275eb6af7SChuck Lever INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 341375eb6af7SChuck Lever INIT_WORK(&transport->error_worker, xs_error_handle); 341475eb6af7SChuck Lever 341575eb6af7SChuck Lever switch (args->xprtsec.policy) { 341675eb6af7SChuck Lever case RPC_XPRTSEC_TLS_ANON: 341775eb6af7SChuck Lever case RPC_XPRTSEC_TLS_X509: 341875eb6af7SChuck Lever xprt->xprtsec = args->xprtsec; 341975eb6af7SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 342075eb6af7SChuck Lever xs_tcp_tls_setup_socket); 342175eb6af7SChuck Lever break; 342275eb6af7SChuck Lever default: 342375eb6af7SChuck Lever ret = ERR_PTR(-EACCES); 342475eb6af7SChuck Lever goto out_err; 342575eb6af7SChuck Lever } 342675eb6af7SChuck Lever 342775eb6af7SChuck Lever switch (addr->sa_family) { 342875eb6af7SChuck Lever case AF_INET: 342975eb6af7SChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 343075eb6af7SChuck Lever xprt_set_bound(xprt); 343175eb6af7SChuck Lever 343275eb6af7SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 343375eb6af7SChuck Lever break; 343475eb6af7SChuck Lever case AF_INET6: 343575eb6af7SChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 343675eb6af7SChuck Lever xprt_set_bound(xprt); 343775eb6af7SChuck Lever 343875eb6af7SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 343975eb6af7SChuck Lever break; 344075eb6af7SChuck Lever default: 344175eb6af7SChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 344275eb6af7SChuck Lever goto out_err; 344375eb6af7SChuck Lever } 344475eb6af7SChuck Lever 344575eb6af7SChuck Lever if (xprt_bound(xprt)) 344675eb6af7SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 344775eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 344875eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 344975eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 345075eb6af7SChuck Lever else 345175eb6af7SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 345275eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 345375eb6af7SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 345475eb6af7SChuck Lever 345575eb6af7SChuck Lever if (try_module_get(THIS_MODULE)) 345675eb6af7SChuck Lever return xprt; 345775eb6af7SChuck Lever ret = ERR_PTR(-EINVAL); 345875eb6af7SChuck Lever out_err: 345975eb6af7SChuck Lever xs_xprt_free(xprt); 346075eb6af7SChuck Lever return ret; 346175eb6af7SChuck Lever } 346275eb6af7SChuck Lever 346375eb6af7SChuck Lever /** 3464f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3465f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3466f300babaSAlexandros Batsakis * 3467f300babaSAlexandros Batsakis */ 3468f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3469f300babaSAlexandros Batsakis { 3470f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3471f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3472f300babaSAlexandros Batsakis struct sock_xprt *transport; 3473f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 34740a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3475f300babaSAlexandros Batsakis 3476d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3477d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3478f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3479f300babaSAlexandros Batsakis return xprt; 3480f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3481f300babaSAlexandros Batsakis 3482f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3483d3abc739SOlga Kornievskaia xprt->xprt_class = &xs_bc_tcp_transport; 3484f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3485f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3486f300babaSAlexandros Batsakis 3487f300babaSAlexandros Batsakis /* backchannel */ 3488f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3489f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3490f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3491f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3492f300babaSAlexandros Batsakis 3493f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3494f300babaSAlexandros Batsakis 3495f300babaSAlexandros Batsakis switch (addr->sa_family) { 3496f300babaSAlexandros Batsakis case AF_INET: 3497f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3498f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3499f300babaSAlexandros Batsakis break; 3500f300babaSAlexandros Batsakis case AF_INET6: 3501f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3502f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3503f300babaSAlexandros Batsakis break; 3504f300babaSAlexandros Batsakis default: 35050a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 35060a68b0beSJ. Bruce Fields goto out_err; 3507f300babaSAlexandros Batsakis } 3508f300babaSAlexandros Batsakis 3509f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3510f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3511f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3512f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3513f300babaSAlexandros Batsakis 3514f300babaSAlexandros Batsakis /* 351599de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 351628303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 351728303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 351828303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 351999de8ea9SJ. Bruce Fields */ 352099de8ea9SJ. Bruce Fields xprt_get(xprt); 352199de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 352299de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 352399de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 352499de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 352599de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 352699de8ea9SJ. Bruce Fields 352799de8ea9SJ. Bruce Fields /* 3528f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3529f300babaSAlexandros Batsakis * the xprt status to connected 3530f300babaSAlexandros Batsakis */ 3531f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3532f300babaSAlexandros Batsakis 3533f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3534f300babaSAlexandros Batsakis return xprt; 3535642aab58SKinglong Mee 3536642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 353739a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 353899de8ea9SJ. Bruce Fields xprt_put(xprt); 35390a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 35400a68b0beSJ. Bruce Fields out_err: 3541315f3812SKinglong Mee xs_xprt_free(xprt); 35420a68b0beSJ. Bruce Fields return ret; 3543f300babaSAlexandros Batsakis } 3544f300babaSAlexandros Batsakis 3545176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3546176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3547176e21eeSChuck Lever .name = "named UNIX socket", 3548176e21eeSChuck Lever .owner = THIS_MODULE, 3549176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3550176e21eeSChuck Lever .setup = xs_setup_local, 3551d5aa6b22STrond Myklebust .netid = { "" }, 3552176e21eeSChuck Lever }; 3553176e21eeSChuck Lever 3554bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3555bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3556bc25571eS\"Talpey, Thomas\ .name = "udp", 3557bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3558f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3559bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3560d5aa6b22STrond Myklebust .netid = { "udp", "udp6", "" }, 3561bc25571eS\"Talpey, Thomas\ }; 3562bc25571eS\"Talpey, Thomas\ 3563bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3564bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3565bc25571eS\"Talpey, Thomas\ .name = "tcp", 3566bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3567f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3568bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3569d5aa6b22STrond Myklebust .netid = { "tcp", "tcp6", "" }, 3570bc25571eS\"Talpey, Thomas\ }; 3571bc25571eS\"Talpey, Thomas\ 357275eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport = { 357375eb6af7SChuck Lever .list = LIST_HEAD_INIT(xs_tcp_tls_transport.list), 357475eb6af7SChuck Lever .name = "tcp-with-tls", 357575eb6af7SChuck Lever .owner = THIS_MODULE, 357675eb6af7SChuck Lever .ident = XPRT_TRANSPORT_TCP_TLS, 357775eb6af7SChuck Lever .setup = xs_setup_tcp_tls, 357875eb6af7SChuck Lever .netid = { "tcp", "tcp6", "" }, 357975eb6af7SChuck Lever }; 358075eb6af7SChuck Lever 3581f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3582f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3583f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3584f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3585f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3586f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3587d5aa6b22STrond Myklebust .netid = { "" }, 3588f300babaSAlexandros Batsakis }; 3589f300babaSAlexandros Batsakis 3590282b32e1SChuck Lever /** 3591bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3592282b32e1SChuck Lever * 3593282b32e1SChuck Lever */ 3594282b32e1SChuck Lever int init_socket_xprt(void) 3595282b32e1SChuck Lever { 35962b1bec5fSEric W. Biederman if (!sunrpc_table_header) 3597c946cb69SLuis Chamberlain sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table); 3598fbf76683SChuck Lever 3599176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3600bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3601bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 360275eb6af7SChuck Lever xprt_register_transport(&xs_tcp_tls_transport); 3603f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3604bc25571eS\"Talpey, Thomas\ 3605282b32e1SChuck Lever return 0; 3606282b32e1SChuck Lever } 3607282b32e1SChuck Lever 3608282b32e1SChuck Lever /** 3609bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3610282b32e1SChuck Lever * 3611282b32e1SChuck Lever */ 3612282b32e1SChuck Lever void cleanup_socket_xprt(void) 3613282b32e1SChuck Lever { 3614fbf76683SChuck Lever if (sunrpc_table_header) { 3615fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3616fbf76683SChuck Lever sunrpc_table_header = NULL; 3617fbf76683SChuck Lever } 3618bc25571eS\"Talpey, Thomas\ 3619176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3620bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3621bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 362275eb6af7SChuck Lever xprt_unregister_transport(&xs_tcp_tls_transport); 3623f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3624282b32e1SChuck Lever } 3625cbf11071STrond Myklebust 36269bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3627cbf11071STrond Myklebust { 3628cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3629cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3630cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3631cbf11071STrond Myklebust } 3632cbf11071STrond Myklebust 36339c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 36349bbb9e5aSRusty Russell .set = param_set_portnr, 36359bbb9e5aSRusty Russell .get = param_get_uint, 36369bbb9e5aSRusty Russell }; 36379bbb9e5aSRusty Russell 3638cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3639cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3640cbf11071STrond Myklebust 3641cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3642cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3643cbf11071STrond Myklebust 36449bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 36459bbb9e5aSRusty Russell const struct kernel_param *kp) 3646cbf11071STrond Myklebust { 3647cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3648cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3649cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3650cbf11071STrond Myklebust } 3651cbf11071STrond Myklebust 36529c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 36539bbb9e5aSRusty Russell .set = param_set_slot_table_size, 36549bbb9e5aSRusty Russell .get = param_get_uint, 36559bbb9e5aSRusty Russell }; 36569bbb9e5aSRusty Russell 3657cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3658cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3659cbf11071STrond Myklebust 3660d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3661d9ba131dSTrond Myklebust const struct kernel_param *kp) 3662d9ba131dSTrond Myklebust { 3663d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3664d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3665d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3666d9ba131dSTrond Myklebust } 3667d9ba131dSTrond Myklebust 36689c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3669d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3670d9ba131dSTrond Myklebust .get = param_get_uint, 3671d9ba131dSTrond Myklebust }; 3672d9ba131dSTrond Myklebust 3673d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3674d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3675d9ba131dSTrond Myklebust 3676cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3677cbf11071STrond Myklebust slot_table_size, 0644); 3678d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3679d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3680cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3681cbf11071STrond Myklebust slot_table_size, 0644); 3682