1a246b010SChuck Lever /* 2a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 3a246b010SChuck Lever * 4a246b010SChuck Lever * Client-side transport implementation for sockets. 5a246b010SChuck Lever * 6113aa838SAlan Cox * TCP callback races fixes (C) 1998 Red Hat 7113aa838SAlan Cox * TCP send fixes (C) 1998 Red Hat 8a246b010SChuck Lever * TCP NFS related read + write fixes 9a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 10a246b010SChuck Lever * 11a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 12a246b010SChuck Lever * Fix behaviour when socket buffer is full. 13a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1455aa4f58SChuck Lever * 1555aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 168f9d5b1aSChuck Lever * 178f9d5b1aSChuck Lever * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 188f9d5b1aSChuck Lever * <gilles.quillard@bull.net> 19a246b010SChuck Lever */ 20a246b010SChuck Lever 21a246b010SChuck Lever #include <linux/types.h> 22176e21eeSChuck Lever #include <linux/string.h> 23a246b010SChuck Lever #include <linux/slab.h> 24bc25571eS\"Talpey, Thomas\ #include <linux/module.h> 25a246b010SChuck Lever #include <linux/capability.h> 26a246b010SChuck Lever #include <linux/pagemap.h> 27a246b010SChuck Lever #include <linux/errno.h> 28a246b010SChuck Lever #include <linux/socket.h> 29a246b010SChuck Lever #include <linux/in.h> 30a246b010SChuck Lever #include <linux/net.h> 31a246b010SChuck Lever #include <linux/mm.h> 32176e21eeSChuck Lever #include <linux/un.h> 33a246b010SChuck Lever #include <linux/udp.h> 34a246b010SChuck Lever #include <linux/tcp.h> 35a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 365976687aSJeff Layton #include <linux/sunrpc/addr.h> 3702107148SChuck Lever #include <linux/sunrpc/sched.h> 384cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h> 3949c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 40a246b010SChuck Lever #include <linux/file.h> 419e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 4244b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 4344b98efdSRicardo Labiaga #endif 44a246b010SChuck Lever 45a246b010SChuck Lever #include <net/sock.h> 46a246b010SChuck Lever #include <net/checksum.h> 47a246b010SChuck Lever #include <net/udp.h> 48a246b010SChuck Lever #include <net/tcp.h> 49a246b010SChuck Lever 5040b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h> 5140b5ea0cSTrond Myklebust 524cfc7e60SRahul Iyer #include "sunrpc.h" 53176e21eeSChuck Lever 54176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 557196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 567196dbb0STrond Myklebust struct socket *sock); 57176e21eeSChuck Lever 589903cd1cSChuck Lever /* 59c556b754SChuck Lever * xprtsock tunables 60c556b754SChuck Lever */ 6109acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 6209acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 6309acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 64c556b754SChuck Lever 6509acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 6609acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 67c556b754SChuck Lever 6854c09874STrond Myklebust #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 6954c09874STrond Myklebust 707d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 7125fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 727d1e8255STrond Myklebust 73c556b754SChuck Lever /* 74fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 75fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 76fbf76683SChuck Lever * directory become the union of all files registered there. 77fbf76683SChuck Lever * 78fbf76683SChuck Lever * We simply need to make sure that we don't collide with 79fbf76683SChuck Lever * someone else's file names! 80fbf76683SChuck Lever */ 81fbf76683SChuck Lever 82fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 83fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 84d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 85fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 86fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 87fbf76683SChuck Lever 88fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 89fbf76683SChuck Lever 90fbf76683SChuck Lever /* 91fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 92fbf76683SChuck Lever * socket buffers for existing UDP transports 93fbf76683SChuck Lever */ 94fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = { 95fbf76683SChuck Lever { 96fbf76683SChuck Lever .procname = "udp_slot_table_entries", 97fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 98fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 99fbf76683SChuck Lever .mode = 0644, 1006d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 101fbf76683SChuck Lever .extra1 = &min_slot_table_size, 102fbf76683SChuck Lever .extra2 = &max_slot_table_size 103fbf76683SChuck Lever }, 104fbf76683SChuck Lever { 105fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 106fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 107fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 108fbf76683SChuck Lever .mode = 0644, 1096d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 110fbf76683SChuck Lever .extra1 = &min_slot_table_size, 111fbf76683SChuck Lever .extra2 = &max_slot_table_size 112fbf76683SChuck Lever }, 113fbf76683SChuck Lever { 114d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 115d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 116d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 117d9ba131dSTrond Myklebust .mode = 0644, 118d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 119d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 120d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 121d9ba131dSTrond Myklebust }, 122d9ba131dSTrond Myklebust { 123fbf76683SChuck Lever .procname = "min_resvport", 124fbf76683SChuck Lever .data = &xprt_min_resvport, 125fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 126fbf76683SChuck Lever .mode = 0644, 1276d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 128fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 129e08ea3a9SFrank Sorenson .extra2 = &xprt_max_resvport 130fbf76683SChuck Lever }, 131fbf76683SChuck Lever { 132fbf76683SChuck Lever .procname = "max_resvport", 133fbf76683SChuck Lever .data = &xprt_max_resvport, 134fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 135fbf76683SChuck Lever .mode = 0644, 1366d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 137e08ea3a9SFrank Sorenson .extra1 = &xprt_min_resvport, 138fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 139fbf76683SChuck Lever }, 140fbf76683SChuck Lever { 14125fe6142STrond Myklebust .procname = "tcp_fin_timeout", 14225fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 14325fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 14425fe6142STrond Myklebust .mode = 0644, 1456d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 14625fe6142STrond Myklebust }, 147f8572d8fSEric W. Biederman { }, 148fbf76683SChuck Lever }; 149fbf76683SChuck Lever 150fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = { 151fbf76683SChuck Lever { 152fbf76683SChuck Lever .procname = "sunrpc", 153fbf76683SChuck Lever .mode = 0555, 154fbf76683SChuck Lever .child = xs_tunables_table 155fbf76683SChuck Lever }, 156f8572d8fSEric W. Biederman { }, 157fbf76683SChuck Lever }; 158fbf76683SChuck Lever 159fbf76683SChuck Lever #endif 160fbf76683SChuck Lever 161fbf76683SChuck Lever /* 16203bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 16303bf4b70SChuck Lever */ 16403bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16503bf4b70SChuck Lever 16603bf4b70SChuck Lever /* 16703bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16803bf4b70SChuck Lever * kind of resource problem on the local host. 16903bf4b70SChuck Lever */ 17003bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 17103bf4b70SChuck Lever 17203bf4b70SChuck Lever /* 17303bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 17403bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17503bf4b70SChuck Lever * 17603bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17703bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17803bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17903bf4b70SChuck Lever * increase over time if the server is down or not responding. 18003bf4b70SChuck Lever */ 18103bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 18203bf4b70SChuck Lever 18303bf4b70SChuck Lever /* 18403bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18503bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18603bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18703bf4b70SChuck Lever */ 18803bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18903bf4b70SChuck Lever 190f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 191a246b010SChuck Lever # undef RPC_DEBUG_DATA 1929903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 193a246b010SChuck Lever #endif 194a246b010SChuck Lever 195a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1969903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 197a246b010SChuck Lever { 198a246b010SChuck Lever u8 *buf = (u8 *) packet; 199a246b010SChuck Lever int j; 200a246b010SChuck Lever 201a246b010SChuck Lever dprintk("RPC: %s\n", msg); 202a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 203a246b010SChuck Lever if (!(j & 31)) { 204a246b010SChuck Lever if (j) 205a246b010SChuck Lever dprintk("\n"); 206a246b010SChuck Lever dprintk("0x%04x ", j); 207a246b010SChuck Lever } 208a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 209a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 210a246b010SChuck Lever } 211a246b010SChuck Lever dprintk("\n"); 212a246b010SChuck Lever } 213a246b010SChuck Lever #else 2149903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 215a246b010SChuck Lever { 216a246b010SChuck Lever /* NOP */ 217a246b010SChuck Lever } 218a246b010SChuck Lever #endif 219a246b010SChuck Lever 2202118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 2212118071dSTrond Myklebust { 2222118071dSTrond Myklebust return (struct rpc_xprt *) sk->sk_user_data; 2232118071dSTrond Myklebust } 2242118071dSTrond Myklebust 22595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 226edb267a6SChuck Lever { 22795392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 22895392c59SChuck Lever } 22995392c59SChuck Lever 230176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 231176e21eeSChuck Lever { 232176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 233176e21eeSChuck Lever } 234176e21eeSChuck Lever 23595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 23695392c59SChuck Lever { 23795392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 23895392c59SChuck Lever } 23995392c59SChuck Lever 24095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 24195392c59SChuck Lever { 24295392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 24395392c59SChuck Lever } 24495392c59SChuck Lever 245c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 246c877b849SChuck Lever { 247c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2489dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2499dc3b095SChuck Lever struct sockaddr_in *sin; 250176e21eeSChuck Lever struct sockaddr_un *sun; 251c877b849SChuck Lever char buf[128]; 252c877b849SChuck Lever 2539dc3b095SChuck Lever switch (sap->sa_family) { 254176e21eeSChuck Lever case AF_LOCAL: 255176e21eeSChuck Lever sun = xs_addr_un(xprt); 256176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 257176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 258176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 259176e21eeSChuck Lever break; 2609dc3b095SChuck Lever case AF_INET: 261176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 262176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 263176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2649dc3b095SChuck Lever sin = xs_addr_in(xprt); 265fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 2669dc3b095SChuck Lever break; 2679dc3b095SChuck Lever case AF_INET6: 268176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 269176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 270176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 2719dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 272fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 2739dc3b095SChuck Lever break; 2749dc3b095SChuck Lever default: 2759dc3b095SChuck Lever BUG(); 2769dc3b095SChuck Lever } 277176e21eeSChuck Lever 2789dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 2799dc3b095SChuck Lever } 2809dc3b095SChuck Lever 2819dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 2829dc3b095SChuck Lever { 2839dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2849dc3b095SChuck Lever char buf[128]; 2859dc3b095SChuck Lever 28681160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 287c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 288c877b849SChuck Lever 28981160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 290c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 291c877b849SChuck Lever } 292c877b849SChuck Lever 2939dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 294b454ae90SChuck Lever const char *protocol, 295b454ae90SChuck Lever const char *netid) 296edb267a6SChuck Lever { 297b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 298b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 299c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3009dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 301edb267a6SChuck Lever } 302edb267a6SChuck Lever 3039dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3044b6473fbSChuck Lever { 3059dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3069dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3074b6473fbSChuck Lever 3089dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 309edb267a6SChuck Lever } 310edb267a6SChuck Lever 311edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 312edb267a6SChuck Lever { 31333e01dc7SChuck Lever unsigned int i; 31433e01dc7SChuck Lever 31533e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 31633e01dc7SChuck Lever switch (i) { 31733e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 31833e01dc7SChuck Lever case RPC_DISPLAY_NETID: 31933e01dc7SChuck Lever continue; 32033e01dc7SChuck Lever default: 32133e01dc7SChuck Lever kfree(xprt->address_strings[i]); 32233e01dc7SChuck Lever } 323edb267a6SChuck Lever } 324edb267a6SChuck Lever 325b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 326b4b5cc85SChuck Lever 32724c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 328b4b5cc85SChuck Lever { 329b4b5cc85SChuck Lever struct msghdr msg = { 330b4b5cc85SChuck Lever .msg_name = addr, 331b4b5cc85SChuck Lever .msg_namelen = addrlen, 33224c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 33324c5684bSTrond Myklebust }; 33424c5684bSTrond Myklebust struct kvec iov = { 33524c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 33624c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 337b4b5cc85SChuck Lever }; 338b4b5cc85SChuck Lever 33924c5684bSTrond Myklebust if (iov.iov_len != 0) 340b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 341b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 342b4b5cc85SChuck Lever } 343b4b5cc85SChuck Lever 344f279cd00SJason Baron static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p) 345b4b5cc85SChuck Lever { 346a6b31d18STrond Myklebust ssize_t (*do_sendpage)(struct socket *sock, struct page *page, 347a6b31d18STrond Myklebust int offset, size_t size, int flags); 34824c5684bSTrond Myklebust struct page **ppage; 34924c5684bSTrond Myklebust unsigned int remainder; 350f279cd00SJason Baron int err; 351b4b5cc85SChuck Lever 35224c5684bSTrond Myklebust remainder = xdr->page_len - base; 35324c5684bSTrond Myklebust base += xdr->page_base; 35424c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 35524c5684bSTrond Myklebust base &= ~PAGE_MASK; 356a6b31d18STrond Myklebust do_sendpage = sock->ops->sendpage; 357a6b31d18STrond Myklebust if (!zerocopy) 358a6b31d18STrond Myklebust do_sendpage = sock_no_sendpage; 35924c5684bSTrond Myklebust for(;;) { 36024c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 36124c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 36224c5684bSTrond Myklebust 36324c5684bSTrond Myklebust remainder -= len; 36431303d6cSTrond Myklebust if (more) 36524c5684bSTrond Myklebust flags |= MSG_MORE; 36631303d6cSTrond Myklebust if (remainder != 0) 36731303d6cSTrond Myklebust flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE; 368a6b31d18STrond Myklebust err = do_sendpage(sock, *ppage, base, len, flags); 36924c5684bSTrond Myklebust if (remainder == 0 || err != len) 37024c5684bSTrond Myklebust break; 371f279cd00SJason Baron *sent_p += err; 37224c5684bSTrond Myklebust ppage++; 37324c5684bSTrond Myklebust base = 0; 37424c5684bSTrond Myklebust } 375f279cd00SJason Baron if (err > 0) { 376f279cd00SJason Baron *sent_p += err; 377f279cd00SJason Baron err = 0; 378f279cd00SJason Baron } 37924c5684bSTrond Myklebust return err; 380b4b5cc85SChuck Lever } 381b4b5cc85SChuck Lever 3829903cd1cSChuck Lever /** 3839903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 3849903cd1cSChuck Lever * @sock: socket to send on 3859903cd1cSChuck Lever * @addr: UDP only -- address of destination 3869903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 3879903cd1cSChuck Lever * @xdr: buffer containing this request 3889903cd1cSChuck Lever * @base: starting position in the buffer 389a6b31d18STrond Myklebust * @zerocopy: true if it is safe to use sendpage() 390f279cd00SJason Baron * @sent_p: return the total number of bytes successfully queued for sending 3919903cd1cSChuck Lever * 392a246b010SChuck Lever */ 393f279cd00SJason Baron static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p) 394a246b010SChuck Lever { 39524c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 396f279cd00SJason Baron int err = 0; 397f279cd00SJason Baron int sent = 0; 398a246b010SChuck Lever 399262965f5SChuck Lever if (unlikely(!sock)) 400fba91afbSTrond Myklebust return -ENOTSOCK; 401262965f5SChuck Lever 40224c5684bSTrond Myklebust if (base != 0) { 40324c5684bSTrond Myklebust addr = NULL; 40424c5684bSTrond Myklebust addrlen = 0; 40524c5684bSTrond Myklebust } 406262965f5SChuck Lever 40724c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 40824c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 40924c5684bSTrond Myklebust remainder -= len; 41024c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 41124c5684bSTrond Myklebust if (remainder == 0 || err != len) 412a246b010SChuck Lever goto out; 413f279cd00SJason Baron *sent_p += err; 414a246b010SChuck Lever base = 0; 415a246b010SChuck Lever } else 41624c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 417a246b010SChuck Lever 41824c5684bSTrond Myklebust if (base < xdr->page_len) { 41924c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 42024c5684bSTrond Myklebust remainder -= len; 421f279cd00SJason Baron err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); 422f279cd00SJason Baron *sent_p += sent; 423f279cd00SJason Baron if (remainder == 0 || sent != len) 424a246b010SChuck Lever goto out; 425a246b010SChuck Lever base = 0; 42624c5684bSTrond Myklebust } else 42724c5684bSTrond Myklebust base -= xdr->page_len; 42824c5684bSTrond Myklebust 42924c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 430f279cd00SJason Baron return 0; 43124c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 432a246b010SChuck Lever out: 433f279cd00SJason Baron if (err > 0) { 434f279cd00SJason Baron *sent_p += err; 435f279cd00SJason Baron err = 0; 436f279cd00SJason Baron } 43724c5684bSTrond Myklebust return err; 438a246b010SChuck Lever } 439a246b010SChuck Lever 440b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 441b6ddf64fSTrond Myklebust { 442b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 443b6ddf64fSTrond Myklebust 444b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 445b6ddf64fSTrond Myklebust } 446b6ddf64fSTrond Myklebust 4479903cd1cSChuck Lever /** 448262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 449262965f5SChuck Lever * @task: task to put to sleep 4509903cd1cSChuck Lever * 451a246b010SChuck Lever */ 4525e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 453a246b010SChuck Lever { 454262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 455262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 456ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 45706ea0bfeSTrond Myklebust struct sock *sk = transport->inet; 45824ca9a84STrond Myklebust int ret = -EAGAIN; 459a246b010SChuck Lever 46046121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 461262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 462262965f5SChuck Lever req->rq_slen); 463a246b010SChuck Lever 464262965f5SChuck Lever /* Protect against races with write_space */ 465262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 466a246b010SChuck Lever 467262965f5SChuck Lever /* Don't race with disconnect */ 468b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 46913331a55STrond Myklebust /* wait for more buffer space */ 47006ea0bfeSTrond Myklebust sk->sk_write_pending++; 471b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 47213331a55STrond Myklebust } else 4735e3771ceSTrond Myklebust ret = -ENOTCONN; 474a246b010SChuck Lever 475262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 47606ea0bfeSTrond Myklebust 47706ea0bfeSTrond Myklebust /* Race breaker in case memory is freed before above code is called */ 478d48f9ce7SDavid Vrabel if (ret == -EAGAIN) { 479d48f9ce7SDavid Vrabel struct socket_wq *wq; 480d48f9ce7SDavid Vrabel 481d48f9ce7SDavid Vrabel rcu_read_lock(); 482d48f9ce7SDavid Vrabel wq = rcu_dereference(sk->sk_wq); 483d48f9ce7SDavid Vrabel set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 484d48f9ce7SDavid Vrabel rcu_read_unlock(); 485d48f9ce7SDavid Vrabel 48606ea0bfeSTrond Myklebust sk->sk_write_space(sk); 487d48f9ce7SDavid Vrabel } 4885e3771ceSTrond Myklebust return ret; 489a246b010SChuck Lever } 490a246b010SChuck Lever 49161677eeeSChuck Lever /* 49261677eeeSChuck Lever * Construct a stream transport record marker in @buf. 49361677eeeSChuck Lever */ 49461677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 49561677eeeSChuck Lever { 49661677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 49761677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 49861677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 49961677eeeSChuck Lever } 50061677eeeSChuck Lever 5019903cd1cSChuck Lever /** 502176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 503176e21eeSChuck Lever * @task: RPC task that manages the state of an RPC request 504176e21eeSChuck Lever * 505176e21eeSChuck Lever * Return values: 506176e21eeSChuck Lever * 0: The request has been sent 507176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 508176e21eeSChuck Lever * complete the request 509176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 510176e21eeSChuck Lever * other: Some other error occured, the request was not sent 511176e21eeSChuck Lever */ 512176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task) 513176e21eeSChuck Lever { 514176e21eeSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 515176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 516176e21eeSChuck Lever struct sock_xprt *transport = 517176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 518176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 519176e21eeSChuck Lever int status; 520f279cd00SJason Baron int sent = 0; 521176e21eeSChuck Lever 522176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 523176e21eeSChuck Lever 524176e21eeSChuck Lever xs_pktdump("packet data:", 525176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 526176e21eeSChuck Lever 527f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent, 528f279cd00SJason Baron true, &sent); 529176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 530176e21eeSChuck Lever __func__, xdr->len - req->rq_bytes_sent, status); 531743c69e7SNeilBrown 532743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 533743c69e7SNeilBrown status = -ENOBUFS; 534743c69e7SNeilBrown 535f279cd00SJason Baron if (likely(sent > 0) || status == 0) { 536f279cd00SJason Baron req->rq_bytes_sent += sent; 537f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 538176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 539176e21eeSChuck Lever req->rq_bytes_sent = 0; 540176e21eeSChuck Lever return 0; 541176e21eeSChuck Lever } 542176e21eeSChuck Lever status = -EAGAIN; 543176e21eeSChuck Lever } 544176e21eeSChuck Lever 545176e21eeSChuck Lever switch (status) { 5463601c4a9STrond Myklebust case -ENOBUFS: 547b5872f0cSTrond Myklebust break; 548176e21eeSChuck Lever case -EAGAIN: 549176e21eeSChuck Lever status = xs_nospace(task); 550176e21eeSChuck Lever break; 551176e21eeSChuck Lever default: 552176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 553176e21eeSChuck Lever -status); 554176e21eeSChuck Lever case -EPIPE: 555176e21eeSChuck Lever xs_close(xprt); 556176e21eeSChuck Lever status = -ENOTCONN; 557176e21eeSChuck Lever } 558176e21eeSChuck Lever 559176e21eeSChuck Lever return status; 560176e21eeSChuck Lever } 561176e21eeSChuck Lever 562176e21eeSChuck Lever /** 563262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5649903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5659903cd1cSChuck Lever * 5669903cd1cSChuck Lever * Return values: 5679903cd1cSChuck Lever * 0: The request has been sent 5689903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 5699903cd1cSChuck Lever * complete the request 570262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 57125985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 5729903cd1cSChuck Lever */ 573262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 574a246b010SChuck Lever { 575a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 576a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 577ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 578262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 579f279cd00SJason Baron int sent = 0; 580262965f5SChuck Lever int status; 581262965f5SChuck Lever 582262965f5SChuck Lever xs_pktdump("packet data:", 583262965f5SChuck Lever req->rq_svec->iov_base, 584262965f5SChuck Lever req->rq_svec->iov_len); 585262965f5SChuck Lever 58601d37c42STrond Myklebust if (!xprt_bound(xprt)) 58701d37c42STrond Myklebust return -ENOTCONN; 588f279cd00SJason Baron status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 589f279cd00SJason Baron xdr, req->rq_bytes_sent, true, &sent); 590262965f5SChuck Lever 591262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 592262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 593262965f5SChuck Lever 5943dedbb5cSJason Baron /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 5953dedbb5cSJason Baron if (status == -EPERM) 5963dedbb5cSJason Baron goto process_status; 5973dedbb5cSJason Baron 598743c69e7SNeilBrown if (status == -EAGAIN && sock_writeable(transport->inet)) 599743c69e7SNeilBrown status = -ENOBUFS; 600743c69e7SNeilBrown 601f279cd00SJason Baron if (sent > 0 || status == 0) { 602f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 603f279cd00SJason Baron if (sent >= req->rq_slen) 604262965f5SChuck Lever return 0; 605262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 606262965f5SChuck Lever status = -EAGAIN; 6072199700fSTrond Myklebust } 608262965f5SChuck Lever 6093dedbb5cSJason Baron process_status: 610262965f5SChuck Lever switch (status) { 611fba91afbSTrond Myklebust case -ENOTSOCK: 612fba91afbSTrond Myklebust status = -ENOTCONN; 613fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 614fba91afbSTrond Myklebust break; 615b6ddf64fSTrond Myklebust case -EAGAIN: 6165e3771ceSTrond Myklebust status = xs_nospace(task); 617b6ddf64fSTrond Myklebust break; 618262965f5SChuck Lever case -ENETUNREACH: 6193601c4a9STrond Myklebust case -ENOBUFS: 620262965f5SChuck Lever case -EPIPE: 621262965f5SChuck Lever case -ECONNREFUSED: 6223dedbb5cSJason Baron case -EPERM: 623262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 624262965f5SChuck Lever * prompts ECONNREFUSED. */ 62513331a55STrond Myklebust break; 62613331a55STrond Myklebust default: 62713331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 62813331a55STrond Myklebust -status); 629262965f5SChuck Lever } 6305fe46e9dSBian Naimeng 631262965f5SChuck Lever return status; 632262965f5SChuck Lever } 633262965f5SChuck Lever 634e06799f9STrond Myklebust /** 635262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 636262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 637262965f5SChuck Lever * 638262965f5SChuck Lever * Return values: 639262965f5SChuck Lever * 0: The request has been sent 640262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 641262965f5SChuck Lever * complete the request 642262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 64325985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 644262965f5SChuck Lever * 645262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 646262965f5SChuck Lever * if sendmsg is not able to make progress? 647262965f5SChuck Lever */ 648262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 649262965f5SChuck Lever { 650262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 651262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 652ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 653262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 654a6b31d18STrond Myklebust bool zerocopy = true; 6559ffadfbcSTrond Myklebust bool vm_wait = false; 656b595bb15SChuck Lever int status; 657f279cd00SJason Baron int sent; 658a246b010SChuck Lever 65961677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 660262965f5SChuck Lever 661262965f5SChuck Lever xs_pktdump("packet data:", 662262965f5SChuck Lever req->rq_svec->iov_base, 663262965f5SChuck Lever req->rq_svec->iov_len); 664a6b31d18STrond Myklebust /* Don't use zero copy if this is a resend. If the RPC call 665a6b31d18STrond Myklebust * completes while the socket holds a reference to the pages, 666a6b31d18STrond Myklebust * then we may end up resending corrupted data. 667a6b31d18STrond Myklebust */ 668a6b31d18STrond Myklebust if (task->tk_flags & RPC_TASK_SENT) 669a6b31d18STrond Myklebust zerocopy = false; 670a246b010SChuck Lever 6717196dbb0STrond Myklebust if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 6727196dbb0STrond Myklebust xs_tcp_set_socket_timeouts(xprt, transport->sock); 6737196dbb0STrond Myklebust 674a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 675a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 676262965f5SChuck Lever * called sendmsg(). */ 677a246b010SChuck Lever while (1) { 678f279cd00SJason Baron sent = 0; 679f279cd00SJason Baron status = xs_sendpages(transport->sock, NULL, 0, xdr, 680f279cd00SJason Baron req->rq_bytes_sent, zerocopy, &sent); 681a246b010SChuck Lever 682262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 683262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 684262965f5SChuck Lever 685a246b010SChuck Lever /* If we've sent the entire packet, immediately 686a246b010SChuck Lever * reset the count of bytes sent. */ 687f279cd00SJason Baron req->rq_bytes_sent += sent; 688f279cd00SJason Baron req->rq_xmit_bytes_sent += sent; 689262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 690a246b010SChuck Lever req->rq_bytes_sent = 0; 691a246b010SChuck Lever return 0; 692a246b010SChuck Lever } 693262965f5SChuck Lever 6949ffadfbcSTrond Myklebust WARN_ON_ONCE(sent == 0 && status == 0); 6959ffadfbcSTrond Myklebust 6969ffadfbcSTrond Myklebust if (status == -EAGAIN ) { 6979ffadfbcSTrond Myklebust /* 6989ffadfbcSTrond Myklebust * Return EAGAIN if we're sure we're hitting the 6999ffadfbcSTrond Myklebust * socket send buffer limits. 7009ffadfbcSTrond Myklebust */ 7019ffadfbcSTrond Myklebust if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 7029ffadfbcSTrond Myklebust break; 7039ffadfbcSTrond Myklebust /* 7049ffadfbcSTrond Myklebust * Did we hit a memory allocation failure? 7059ffadfbcSTrond Myklebust */ 7069ffadfbcSTrond Myklebust if (sent == 0) { 7079ffadfbcSTrond Myklebust status = -ENOBUFS; 7089ffadfbcSTrond Myklebust if (vm_wait) 7099ffadfbcSTrond Myklebust break; 7109ffadfbcSTrond Myklebust /* Retry, knowing now that we're below the 7119ffadfbcSTrond Myklebust * socket send buffer limit 7129ffadfbcSTrond Myklebust */ 7139ffadfbcSTrond Myklebust vm_wait = true; 7149ffadfbcSTrond Myklebust } 7159ffadfbcSTrond Myklebust continue; 7169ffadfbcSTrond Myklebust } 717f580dd04STrond Myklebust if (status < 0) 718f580dd04STrond Myklebust break; 7199ffadfbcSTrond Myklebust vm_wait = false; 720a246b010SChuck Lever } 721a246b010SChuck Lever 722262965f5SChuck Lever switch (status) { 723fba91afbSTrond Myklebust case -ENOTSOCK: 724fba91afbSTrond Myklebust status = -ENOTCONN; 725fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 726fba91afbSTrond Myklebust break; 727262965f5SChuck Lever case -EAGAIN: 7285e3771ceSTrond Myklebust status = xs_nospace(task); 729262965f5SChuck Lever break; 730262965f5SChuck Lever case -ECONNRESET: 731262965f5SChuck Lever case -ECONNREFUSED: 732262965f5SChuck Lever case -ENOTCONN: 7333913c78cSTrond Myklebust case -EADDRINUSE: 734b5872f0cSTrond Myklebust case -ENOBUFS: 735b9d2bb2eSTrond Myklebust case -EPIPE: 73613331a55STrond Myklebust break; 73713331a55STrond Myklebust default: 73813331a55STrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 73913331a55STrond Myklebust -status); 740a246b010SChuck Lever } 7415fe46e9dSBian Naimeng 742a246b010SChuck Lever return status; 743a246b010SChuck Lever } 744a246b010SChuck Lever 7459903cd1cSChuck Lever /** 746e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 747e0ab53deSTrond Myklebust * @xprt: transport 748e0ab53deSTrond Myklebust * @task: rpc task 749e0ab53deSTrond Myklebust * 750e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 751e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 752e0ab53deSTrond Myklebust * the server. 753e0ab53deSTrond Myklebust */ 754e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 755e0ab53deSTrond Myklebust { 756e0ab53deSTrond Myklebust struct rpc_rqst *req; 757e0ab53deSTrond Myklebust 758e0ab53deSTrond Myklebust if (task != xprt->snd_task) 759e0ab53deSTrond Myklebust return; 760e0ab53deSTrond Myklebust if (task == NULL) 761e0ab53deSTrond Myklebust goto out_release; 762e0ab53deSTrond Myklebust req = task->tk_rqstp; 76343cedbf0STrond Myklebust if (req == NULL) 76443cedbf0STrond Myklebust goto out_release; 765e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 766e0ab53deSTrond Myklebust goto out_release; 767e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 768e0ab53deSTrond Myklebust goto out_release; 769a4f0835cSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 770e0ab53deSTrond Myklebust out_release: 771e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 772e0ab53deSTrond Myklebust } 773e0ab53deSTrond Myklebust 7742a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7752a9e1cfaSTrond Myklebust { 7762a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7772a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7782a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7792118071dSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7802a9e1cfaSTrond Myklebust } 7812a9e1cfaSTrond Myklebust 7822a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7832a9e1cfaSTrond Myklebust { 7842a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7852a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7862a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7872118071dSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7882118071dSTrond Myklebust } 7892118071dSTrond Myklebust 79042d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 79142d42a5bSTrond Myklebust { 79242d42a5bSTrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 79342d42a5bSTrond Myklebust 79442d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 79542d42a5bSTrond Myklebust } 79642d42a5bSTrond Myklebust 797b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 798b70ae915STrond Myklebust { 799b70ae915STrond Myklebust smp_mb__before_atomic(); 800b70ae915STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 801b70ae915STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 80242d42a5bSTrond Myklebust xs_sock_reset_state_flags(xprt); 803b70ae915STrond Myklebust smp_mb__after_atomic(); 804b70ae915STrond Myklebust } 805b70ae915STrond Myklebust 806b70ae915STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 807b70ae915STrond Myklebust { 808b70ae915STrond Myklebust xs_sock_reset_connection_flags(xprt); 809b70ae915STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 810b70ae915STrond Myklebust xprt_disconnect_done(xprt); 811b70ae915STrond Myklebust } 812b70ae915STrond Myklebust 8132118071dSTrond Myklebust /** 8142118071dSTrond Myklebust * xs_error_report - callback to handle TCP socket state errors 8152118071dSTrond Myklebust * @sk: socket 8162118071dSTrond Myklebust * 8172118071dSTrond Myklebust * Note: we don't call sock_error() since there may be a rpc_task 8182118071dSTrond Myklebust * using the socket, and so we don't want to clear sk->sk_err. 8192118071dSTrond Myklebust */ 8202118071dSTrond Myklebust static void xs_error_report(struct sock *sk) 8212118071dSTrond Myklebust { 8222118071dSTrond Myklebust struct rpc_xprt *xprt; 8232118071dSTrond Myklebust int err; 8242118071dSTrond Myklebust 8252118071dSTrond Myklebust read_lock_bh(&sk->sk_callback_lock); 8262118071dSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 8272118071dSTrond Myklebust goto out; 8282118071dSTrond Myklebust 8292118071dSTrond Myklebust err = -sk->sk_err; 8302118071dSTrond Myklebust if (err == 0) 8312118071dSTrond Myklebust goto out; 832b70ae915STrond Myklebust /* Is this a reset event? */ 833b70ae915STrond Myklebust if (sk->sk_state == TCP_CLOSE) 834b70ae915STrond Myklebust xs_sock_mark_closed(xprt); 8352118071dSTrond Myklebust dprintk("RPC: xs_error_report client %p, error=%d...\n", 8362118071dSTrond Myklebust xprt, -err); 837e8353c76STrond Myklebust trace_rpc_socket_error(xprt, sk->sk_socket, err); 8382118071dSTrond Myklebust xprt_wake_pending_tasks(xprt, err); 8392118071dSTrond Myklebust out: 8402118071dSTrond Myklebust read_unlock_bh(&sk->sk_callback_lock); 8412a9e1cfaSTrond Myklebust } 8422a9e1cfaSTrond Myklebust 843fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 844a246b010SChuck Lever { 845ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 846ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 8476cc7e908STrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 848a246b010SChuck Lever 849fe315e76SChuck Lever if (sk == NULL) 850fe315e76SChuck Lever return; 8519903cd1cSChuck Lever 852264d1df3SJeff Layton if (atomic_read(&transport->xprt.swapper)) 853264d1df3SJeff Layton sk_clear_memalloc(sk); 854264d1df3SJeff Layton 85509939204STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 85609939204STrond Myklebust 857edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 858a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 859ee0ac0c2SChuck Lever transport->inet = NULL; 860ee0ac0c2SChuck Lever transport->sock = NULL; 861a246b010SChuck Lever 862a246b010SChuck Lever sk->sk_user_data = NULL; 8632a9e1cfaSTrond Myklebust 8642a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 8650c78789eSTrond Myklebust xprt_clear_connected(xprt); 866a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 8676cc7e908STrond Myklebust xs_sock_reset_connection_flags(xprt); 868edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 869a246b010SChuck Lever 8706cc7e908STrond Myklebust trace_rpc_socket_close(xprt, sock); 871a246b010SChuck Lever sock_release(sock); 872fe315e76SChuck Lever } 873fe315e76SChuck Lever 874fe315e76SChuck Lever /** 875fe315e76SChuck Lever * xs_close - close a socket 876fe315e76SChuck Lever * @xprt: transport 877fe315e76SChuck Lever * 878fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 879fe315e76SChuck Lever * on the server we want to save. 880f75e6745STrond Myklebust * 881f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 882f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 883fe315e76SChuck Lever */ 884fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 885fe315e76SChuck Lever { 886fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 887fe315e76SChuck Lever 888fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 889fe315e76SChuck Lever 890fe315e76SChuck Lever xs_reset_transport(transport); 89161d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 892fe315e76SChuck Lever 89362da3b24STrond Myklebust xprt_disconnect_done(xprt); 894a246b010SChuck Lever } 895a246b010SChuck Lever 8964a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt) 8974a068258SChuck Lever { 8984a068258SChuck Lever dprintk("RPC: injecting transport disconnect on xprt=%p\n", 8994a068258SChuck Lever xprt); 9004a068258SChuck Lever xprt_disconnect_done(xprt); 9014a068258SChuck Lever } 9024a068258SChuck Lever 903315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt) 904315f3812SKinglong Mee { 905315f3812SKinglong Mee xs_free_peer_addresses(xprt); 906315f3812SKinglong Mee xprt_free(xprt); 907315f3812SKinglong Mee } 908315f3812SKinglong Mee 9099903cd1cSChuck Lever /** 9109903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 9119903cd1cSChuck Lever * @xprt: doomed transport 9129903cd1cSChuck Lever * 9139903cd1cSChuck Lever */ 9149903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 915a246b010SChuck Lever { 91603c78827STrond Myklebust struct sock_xprt *transport = container_of(xprt, 91703c78827STrond Myklebust struct sock_xprt, xprt); 9189903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 9199903cd1cSChuck Lever 92003c78827STrond Myklebust cancel_delayed_work_sync(&transport->connect_worker); 921a1311d87STrond Myklebust xs_close(xprt); 922edc1b01cSTrond Myklebust cancel_work_sync(&transport->recv_worker); 923315f3812SKinglong Mee xs_xprt_free(xprt); 924a1311d87STrond Myklebust module_put(THIS_MODULE); 925a246b010SChuck Lever } 926a246b010SChuck Lever 927176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) 928176e21eeSChuck Lever { 929176e21eeSChuck Lever struct xdr_skb_reader desc = { 930176e21eeSChuck Lever .skb = skb, 931176e21eeSChuck Lever .offset = sizeof(rpc_fraghdr), 932176e21eeSChuck Lever .count = skb->len - sizeof(rpc_fraghdr), 933176e21eeSChuck Lever }; 934176e21eeSChuck Lever 935176e21eeSChuck Lever if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) 936176e21eeSChuck Lever return -1; 937176e21eeSChuck Lever if (desc.count) 938176e21eeSChuck Lever return -1; 939176e21eeSChuck Lever return 0; 940176e21eeSChuck Lever } 941176e21eeSChuck Lever 942176e21eeSChuck Lever /** 943a2648094STrond Myklebust * xs_local_data_read_skb 944a2648094STrond Myklebust * @xprt: transport 945a2648094STrond Myklebust * @sk: socket 946a2648094STrond Myklebust * @skb: skbuff 947176e21eeSChuck Lever * 948176e21eeSChuck Lever * Currently this assumes we can read the whole reply in a single gulp. 949176e21eeSChuck Lever */ 950a2648094STrond Myklebust static void xs_local_data_read_skb(struct rpc_xprt *xprt, 951a2648094STrond Myklebust struct sock *sk, 952a2648094STrond Myklebust struct sk_buff *skb) 953176e21eeSChuck Lever { 954176e21eeSChuck Lever struct rpc_task *task; 955176e21eeSChuck Lever struct rpc_rqst *rovr; 956a2648094STrond Myklebust int repsize, copied; 957176e21eeSChuck Lever u32 _xid; 958176e21eeSChuck Lever __be32 *xp; 959176e21eeSChuck Lever 960176e21eeSChuck Lever repsize = skb->len - sizeof(rpc_fraghdr); 961176e21eeSChuck Lever if (repsize < 4) { 962176e21eeSChuck Lever dprintk("RPC: impossible RPC reply size %d\n", repsize); 963a2648094STrond Myklebust return; 964176e21eeSChuck Lever } 965176e21eeSChuck Lever 966176e21eeSChuck Lever /* Copy the XID from the skb... */ 967176e21eeSChuck Lever xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); 968176e21eeSChuck Lever if (xp == NULL) 969a2648094STrond Myklebust return; 970176e21eeSChuck Lever 971176e21eeSChuck Lever /* Look up and lock the request corresponding to the given XID */ 972a2648094STrond Myklebust spin_lock_bh(&xprt->transport_lock); 973176e21eeSChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 974176e21eeSChuck Lever if (!rovr) 975176e21eeSChuck Lever goto out_unlock; 976176e21eeSChuck Lever task = rovr->rq_task; 977176e21eeSChuck Lever 978176e21eeSChuck Lever copied = rovr->rq_private_buf.buflen; 979176e21eeSChuck Lever if (copied > repsize) 980176e21eeSChuck Lever copied = repsize; 981176e21eeSChuck Lever 982176e21eeSChuck Lever if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { 983176e21eeSChuck Lever dprintk("RPC: sk_buff copy failed\n"); 984176e21eeSChuck Lever goto out_unlock; 985176e21eeSChuck Lever } 986176e21eeSChuck Lever 987176e21eeSChuck Lever xprt_complete_rqst(task, copied); 988176e21eeSChuck Lever 989176e21eeSChuck Lever out_unlock: 990a2648094STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 991a2648094STrond Myklebust } 992a2648094STrond Myklebust 993a2648094STrond Myklebust static void xs_local_data_receive(struct sock_xprt *transport) 994a2648094STrond Myklebust { 995a2648094STrond Myklebust struct sk_buff *skb; 996a2648094STrond Myklebust struct sock *sk; 997a2648094STrond Myklebust int err; 998a2648094STrond Myklebust 999a2648094STrond Myklebust mutex_lock(&transport->recv_mutex); 1000a2648094STrond Myklebust sk = transport->inet; 1001a2648094STrond Myklebust if (sk == NULL) 1002a2648094STrond Myklebust goto out; 1003a2648094STrond Myklebust for (;;) { 1004a2648094STrond Myklebust skb = skb_recv_datagram(sk, 0, 1, &err); 100542d42a5bSTrond Myklebust if (skb != NULL) { 1006a2648094STrond Myklebust xs_local_data_read_skb(&transport->xprt, sk, skb); 1007176e21eeSChuck Lever skb_free_datagram(sk, skb); 100842d42a5bSTrond Myklebust continue; 100942d42a5bSTrond Myklebust } 101042d42a5bSTrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 101142d42a5bSTrond Myklebust break; 1012a2648094STrond Myklebust } 1013176e21eeSChuck Lever out: 1014a2648094STrond Myklebust mutex_unlock(&transport->recv_mutex); 1015a2648094STrond Myklebust } 1016a2648094STrond Myklebust 1017a2648094STrond Myklebust static void xs_local_data_receive_workfn(struct work_struct *work) 1018a2648094STrond Myklebust { 1019a2648094STrond Myklebust struct sock_xprt *transport = 1020a2648094STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1021a2648094STrond Myklebust xs_local_data_receive(transport); 1022176e21eeSChuck Lever } 1023176e21eeSChuck Lever 10249903cd1cSChuck Lever /** 1025f9b2ee71STrond Myklebust * xs_udp_data_read_skb - receive callback for UDP sockets 1026f9b2ee71STrond Myklebust * @xprt: transport 1027f9b2ee71STrond Myklebust * @sk: socket 1028f9b2ee71STrond Myklebust * @skb: skbuff 10299903cd1cSChuck Lever * 1030a246b010SChuck Lever */ 1031f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1032f9b2ee71STrond Myklebust struct sock *sk, 1033f9b2ee71STrond Myklebust struct sk_buff *skb) 1034a246b010SChuck Lever { 1035a246b010SChuck Lever struct rpc_task *task; 1036a246b010SChuck Lever struct rpc_rqst *rovr; 1037f9b2ee71STrond Myklebust int repsize, copied; 1038d8ed029dSAlexey Dobriyan u32 _xid; 1039d8ed029dSAlexey Dobriyan __be32 *xp; 1040a246b010SChuck Lever 10411da8c681SWillem de Bruijn repsize = skb->len; 1042a246b010SChuck Lever if (repsize < 4) { 10439903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1044f9b2ee71STrond Myklebust return; 1045a246b010SChuck Lever } 1046a246b010SChuck Lever 1047a246b010SChuck Lever /* Copy the XID from the skb... */ 10481da8c681SWillem de Bruijn xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1049a246b010SChuck Lever if (xp == NULL) 1050f9b2ee71STrond Myklebust return; 1051a246b010SChuck Lever 1052a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 1053f9b2ee71STrond Myklebust spin_lock_bh(&xprt->transport_lock); 1054a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1055a246b010SChuck Lever if (!rovr) 1056a246b010SChuck Lever goto out_unlock; 1057a246b010SChuck Lever task = rovr->rq_task; 1058a246b010SChuck Lever 1059a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1060a246b010SChuck Lever copied = repsize; 1061a246b010SChuck Lever 1062a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 10631781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 106402c22347SEric Dumazet __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1065a246b010SChuck Lever goto out_unlock; 10661781f7f5SHerbert Xu } 10671781f7f5SHerbert Xu 106802c22347SEric Dumazet __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1069a246b010SChuck Lever 10706a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, copied); 10711570c1e4SChuck Lever xprt_complete_rqst(task, copied); 1072a246b010SChuck Lever 1073a246b010SChuck Lever out_unlock: 1074f9b2ee71STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 1075f9b2ee71STrond Myklebust } 1076f9b2ee71STrond Myklebust 1077f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport) 1078f9b2ee71STrond Myklebust { 1079f9b2ee71STrond Myklebust struct sk_buff *skb; 1080f9b2ee71STrond Myklebust struct sock *sk; 1081f9b2ee71STrond Myklebust int err; 1082f9b2ee71STrond Myklebust 1083f9b2ee71STrond Myklebust mutex_lock(&transport->recv_mutex); 1084f9b2ee71STrond Myklebust sk = transport->inet; 1085f9b2ee71STrond Myklebust if (sk == NULL) 1086f9b2ee71STrond Myklebust goto out; 1087f9b2ee71STrond Myklebust for (;;) { 10887c13f97fSPaolo Abeni skb = skb_recv_udp(sk, 0, 1, &err); 108942d42a5bSTrond Myklebust if (skb != NULL) { 1090f9b2ee71STrond Myklebust xs_udp_data_read_skb(&transport->xprt, sk, skb); 1091850cbaddSPaolo Abeni consume_skb(skb); 109242d42a5bSTrond Myklebust continue; 109342d42a5bSTrond Myklebust } 109442d42a5bSTrond Myklebust if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 109542d42a5bSTrond Myklebust break; 1096f9b2ee71STrond Myklebust } 1097a246b010SChuck Lever out: 1098f9b2ee71STrond Myklebust mutex_unlock(&transport->recv_mutex); 1099f9b2ee71STrond Myklebust } 1100f9b2ee71STrond Myklebust 1101f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work) 1102f9b2ee71STrond Myklebust { 1103f9b2ee71STrond Myklebust struct sock_xprt *transport = 1104f9b2ee71STrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1105f9b2ee71STrond Myklebust xs_udp_data_receive(transport); 1106f9b2ee71STrond Myklebust } 1107f9b2ee71STrond Myklebust 1108f9b2ee71STrond Myklebust /** 1109f9b2ee71STrond Myklebust * xs_data_ready - "data ready" callback for UDP sockets 1110f9b2ee71STrond Myklebust * @sk: socket with data to read 1111f9b2ee71STrond Myklebust * 1112f9b2ee71STrond Myklebust */ 1113f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk) 1114f9b2ee71STrond Myklebust { 1115f9b2ee71STrond Myklebust struct rpc_xprt *xprt; 1116f9b2ee71STrond Myklebust 1117f9b2ee71STrond Myklebust read_lock_bh(&sk->sk_callback_lock); 1118f9b2ee71STrond Myklebust dprintk("RPC: xs_data_ready...\n"); 1119f9b2ee71STrond Myklebust xprt = xprt_from_sock(sk); 1120f9b2ee71STrond Myklebust if (xprt != NULL) { 1121f9b2ee71STrond Myklebust struct sock_xprt *transport = container_of(xprt, 1122f9b2ee71STrond Myklebust struct sock_xprt, xprt); 11235157b956STrond Myklebust transport->old_data_ready(sk); 11245157b956STrond Myklebust /* Any data means we had a useful conversation, so 11255157b956STrond Myklebust * then we don't need to delay the next reconnect 11265157b956STrond Myklebust */ 11275157b956STrond Myklebust if (xprt->reestablish_timeout) 11285157b956STrond Myklebust xprt->reestablish_timeout = 0; 112942d42a5bSTrond Myklebust if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 113040a5f1b1STrond Myklebust queue_work(xprtiod_workqueue, &transport->recv_worker); 1131f9b2ee71STrond Myklebust } 1132f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1133a246b010SChuck Lever } 1134a246b010SChuck Lever 1135a519fc7aSTrond Myklebust /* 1136a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1137a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1138a519fc7aSTrond Myklebust */ 1139a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1140a519fc7aSTrond Myklebust { 1141a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1142a519fc7aSTrond Myklebust } 1143a519fc7aSTrond Myklebust 1144dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1145a246b010SChuck Lever { 114651971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1147a246b010SChuck Lever size_t len, used; 1148a246b010SChuck Lever char *p; 1149a246b010SChuck Lever 115051971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 115151971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 11529d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 115351971139SChuck Lever transport->tcp_offset += used; 1154a246b010SChuck Lever if (used != len) 1155a246b010SChuck Lever return; 1156808012fbSChuck Lever 115751971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 115851971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 1159e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 1160a246b010SChuck Lever else 1161e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 116251971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 1163808012fbSChuck Lever 1164e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 116551971139SChuck Lever transport->tcp_offset = 0; 1166808012fbSChuck Lever 1167a246b010SChuck Lever /* Sanity check of the record length */ 116818dca02aSRicardo Labiaga if (unlikely(transport->tcp_reclen < 8)) { 11699903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 1170a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 11719903cd1cSChuck Lever return; 1172a246b010SChuck Lever } 1173a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 117451971139SChuck Lever transport->tcp_reclen); 1175a246b010SChuck Lever } 1176a246b010SChuck Lever 117751971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 1178a246b010SChuck Lever { 117951971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 1180e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 118151971139SChuck Lever transport->tcp_offset = 0; 1182e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 1183e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1184e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 118551971139SChuck Lever transport->tcp_copied = 0; 1186a246b010SChuck Lever } 1187a246b010SChuck Lever } 1188a246b010SChuck Lever } 1189a246b010SChuck Lever 1190dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1191a246b010SChuck Lever { 1192a246b010SChuck Lever size_t len, used; 1193a246b010SChuck Lever char *p; 1194a246b010SChuck Lever 119551971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 1196a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 119751971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 11989d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 119951971139SChuck Lever transport->tcp_offset += used; 1200a246b010SChuck Lever if (used != len) 1201a246b010SChuck Lever return; 1202e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 1203f4a2e418SRicardo Labiaga transport->tcp_flags |= TCP_RCV_READ_CALLDIR; 120451971139SChuck Lever transport->tcp_copied = 4; 120518dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 120618dca02aSRicardo Labiaga (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" 120718dca02aSRicardo Labiaga : "request with", 120851971139SChuck Lever ntohl(transport->tcp_xid)); 120951971139SChuck Lever xs_tcp_check_fraghdr(transport); 1210a246b010SChuck Lever } 1211a246b010SChuck Lever 121218dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 121318dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1214a246b010SChuck Lever { 121518dca02aSRicardo Labiaga size_t len, used; 121618dca02aSRicardo Labiaga u32 offset; 1217b76ce561STrond Myklebust char *p; 121818dca02aSRicardo Labiaga 121918dca02aSRicardo Labiaga /* 122018dca02aSRicardo Labiaga * We want transport->tcp_offset to be 8 at the end of this routine 122118dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 122218dca02aSRicardo Labiaga * When this function is called for the first time, 122318dca02aSRicardo Labiaga * transport->tcp_offset is 4 (after having already read the xid). 122418dca02aSRicardo Labiaga */ 122518dca02aSRicardo Labiaga offset = transport->tcp_offset - sizeof(transport->tcp_xid); 1226b76ce561STrond Myklebust len = sizeof(transport->tcp_calldir) - offset; 122718dca02aSRicardo Labiaga dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); 1228b76ce561STrond Myklebust p = ((char *) &transport->tcp_calldir) + offset; 1229b76ce561STrond Myklebust used = xdr_skb_read_bits(desc, p, len); 123018dca02aSRicardo Labiaga transport->tcp_offset += used; 123118dca02aSRicardo Labiaga if (used != len) 123218dca02aSRicardo Labiaga return; 1233f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; 1234f4a2e418SRicardo Labiaga /* 1235f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1236f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1237f4a2e418SRicardo Labiaga */ 1238b76ce561STrond Myklebust switch (ntohl(transport->tcp_calldir)) { 1239b76ce561STrond Myklebust case RPC_REPLY: 1240b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1241b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 124218dca02aSRicardo Labiaga transport->tcp_flags |= TCP_RPC_REPLY; 1243b76ce561STrond Myklebust break; 1244b76ce561STrond Myklebust case RPC_CALL: 1245b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1246b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 124718dca02aSRicardo Labiaga transport->tcp_flags &= ~TCP_RPC_REPLY; 1248b76ce561STrond Myklebust break; 1249b76ce561STrond Myklebust default: 1250b76ce561STrond Myklebust dprintk("RPC: invalid request message type\n"); 1251a519fc7aSTrond Myklebust xs_tcp_force_close(&transport->xprt); 1252b76ce561STrond Myklebust } 125318dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 125418dca02aSRicardo Labiaga } 125518dca02aSRicardo Labiaga 125644b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 125744b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 125844b98efdSRicardo Labiaga struct rpc_rqst *req) 1259a246b010SChuck Lever { 126044b98efdSRicardo Labiaga struct sock_xprt *transport = 126144b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1262a246b010SChuck Lever struct xdr_buf *rcvbuf; 1263a246b010SChuck Lever size_t len; 1264a246b010SChuck Lever ssize_t r; 1265a246b010SChuck Lever 1266a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1267f4a2e418SRicardo Labiaga 1268f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { 1269f4a2e418SRicardo Labiaga /* 1270f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1271f4a2e418SRicardo Labiaga */ 1272f4a2e418SRicardo Labiaga memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, 1273b76ce561STrond Myklebust &transport->tcp_calldir, 1274b76ce561STrond Myklebust sizeof(transport->tcp_calldir)); 1275b76ce561STrond Myklebust transport->tcp_copied += sizeof(transport->tcp_calldir); 1276f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; 1277a246b010SChuck Lever } 1278a246b010SChuck Lever 1279a246b010SChuck Lever len = desc->count; 128051971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1281dd456471SChuck Lever struct xdr_skb_reader my_desc; 1282a246b010SChuck Lever 128351971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1284a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1285a246b010SChuck Lever my_desc.count = len; 128651971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 12879d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1288a246b010SChuck Lever desc->count -= r; 1289a246b010SChuck Lever desc->offset += r; 1290a246b010SChuck Lever } else 129151971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 12929d292316SChuck Lever desc, xdr_skb_read_bits); 1293a246b010SChuck Lever 1294a246b010SChuck Lever if (r > 0) { 129551971139SChuck Lever transport->tcp_copied += r; 129651971139SChuck Lever transport->tcp_offset += r; 1297a246b010SChuck Lever } 1298a246b010SChuck Lever if (r != len) { 1299a246b010SChuck Lever /* Error when copying to the receive buffer, 1300a246b010SChuck Lever * usually because we weren't able to allocate 1301a246b010SChuck Lever * additional buffer pages. All we can do now 1302e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1303a246b010SChuck Lever * will not receive any additional updates, 1304a246b010SChuck Lever * and time out. 1305a246b010SChuck Lever * Any remaining data from this record will 1306a246b010SChuck Lever * be discarded. 1307a246b010SChuck Lever */ 1308e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1309a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 131051971139SChuck Lever ntohl(transport->tcp_xid)); 131146121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 131246121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 131346121cf7SChuck Lever xprt, transport->tcp_copied, 131446121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 131544b98efdSRicardo Labiaga return; 1316a246b010SChuck Lever } 1317a246b010SChuck Lever 1318a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 131951971139SChuck Lever ntohl(transport->tcp_xid), r); 132046121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 132146121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 132246121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1323a246b010SChuck Lever 132451971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1325e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 132651971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1327e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1328e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1329a246b010SChuck Lever } 133044b98efdSRicardo Labiaga } 133144b98efdSRicardo Labiaga 133244b98efdSRicardo Labiaga /* 133344b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 133444b98efdSRicardo Labiaga * tcp read code to read the data. 133544b98efdSRicardo Labiaga */ 133644b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 133744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 133844b98efdSRicardo Labiaga { 133944b98efdSRicardo Labiaga struct sock_xprt *transport = 134044b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 134144b98efdSRicardo Labiaga struct rpc_rqst *req; 134244b98efdSRicardo Labiaga 134344b98efdSRicardo Labiaga dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); 134444b98efdSRicardo Labiaga 134544b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 1346edc1b01cSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 134744b98efdSRicardo Labiaga req = xprt_lookup_rqst(xprt, transport->tcp_xid); 134844b98efdSRicardo Labiaga if (!req) { 134944b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 135044b98efdSRicardo Labiaga ntohl(transport->tcp_xid)); 1351edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 135244b98efdSRicardo Labiaga return -1; 135344b98efdSRicardo Labiaga } 135444b98efdSRicardo Labiaga 135544b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 135644b98efdSRicardo Labiaga 1357e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 135851971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 135944b98efdSRicardo Labiaga 1360edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 136144b98efdSRicardo Labiaga return 0; 136244b98efdSRicardo Labiaga } 136344b98efdSRicardo Labiaga 13649e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 136544b98efdSRicardo Labiaga /* 136644b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 136744b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 136844b98efdSRicardo Labiaga * queue. 136944b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 137044b98efdSRicardo Labiaga * connection and return -1. 137144b98efdSRicardo Labiaga */ 13722ea24497STrond Myklebust static int xs_tcp_read_callback(struct rpc_xprt *xprt, 137344b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 137444b98efdSRicardo Labiaga { 137544b98efdSRicardo Labiaga struct sock_xprt *transport = 137644b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 137744b98efdSRicardo Labiaga struct rpc_rqst *req; 137844b98efdSRicardo Labiaga 13792ea24497STrond Myklebust /* Look up and lock the request corresponding to the given XID */ 1380edc1b01cSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 13812ea24497STrond Myklebust req = xprt_lookup_bc_request(xprt, transport->tcp_xid); 138244b98efdSRicardo Labiaga if (req == NULL) { 1383edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 138444b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 138544b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 138644b98efdSRicardo Labiaga return -1; 138744b98efdSRicardo Labiaga } 138844b98efdSRicardo Labiaga 138944b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 139044b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 139144b98efdSRicardo Labiaga 13922ea24497STrond Myklebust if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 13932ea24497STrond Myklebust xprt_complete_bc_request(req, transport->tcp_copied); 1394edc1b01cSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 139544b98efdSRicardo Labiaga 139644b98efdSRicardo Labiaga return 0; 139744b98efdSRicardo Labiaga } 139844b98efdSRicardo Labiaga 139944b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 140044b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 140144b98efdSRicardo Labiaga { 140244b98efdSRicardo Labiaga struct sock_xprt *transport = 140344b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 140444b98efdSRicardo Labiaga 140544b98efdSRicardo Labiaga return (transport->tcp_flags & TCP_RPC_REPLY) ? 140644b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 140744b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 140844b98efdSRicardo Labiaga } 140976566773SChuck Lever 141076566773SChuck Lever static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net) 141176566773SChuck Lever { 141276566773SChuck Lever int ret; 141376566773SChuck Lever 141476566773SChuck Lever ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0, 141576566773SChuck Lever SVC_SOCK_ANONYMOUS); 141676566773SChuck Lever if (ret < 0) 141776566773SChuck Lever return ret; 141876566773SChuck Lever return 0; 141976566773SChuck Lever } 14206b26cc8cSChuck Lever 14216b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 14226b26cc8cSChuck Lever { 14236b26cc8cSChuck Lever return PAGE_SIZE; 14246b26cc8cSChuck Lever } 142544b98efdSRicardo Labiaga #else 142644b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 142744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 142844b98efdSRicardo Labiaga { 142944b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 143044b98efdSRicardo Labiaga } 14319e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 143244b98efdSRicardo Labiaga 143344b98efdSRicardo Labiaga /* 143444b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 143544b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 143644b98efdSRicardo Labiaga */ 143744b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 143844b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 143944b98efdSRicardo Labiaga { 144044b98efdSRicardo Labiaga struct sock_xprt *transport = 144144b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 144244b98efdSRicardo Labiaga 144344b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 144451971139SChuck Lever xs_tcp_check_fraghdr(transport); 144544b98efdSRicardo Labiaga else { 144644b98efdSRicardo Labiaga /* 144744b98efdSRicardo Labiaga * The transport_lock protects the request handling. 144844b98efdSRicardo Labiaga * There's no need to hold it to update the tcp_flags. 144944b98efdSRicardo Labiaga */ 145044b98efdSRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 145144b98efdSRicardo Labiaga } 1452a246b010SChuck Lever } 1453a246b010SChuck Lever 1454dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1455a246b010SChuck Lever { 1456a246b010SChuck Lever size_t len; 1457a246b010SChuck Lever 145851971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1459a246b010SChuck Lever if (len > desc->count) 1460a246b010SChuck Lever len = desc->count; 1461a246b010SChuck Lever desc->count -= len; 1462a246b010SChuck Lever desc->offset += len; 146351971139SChuck Lever transport->tcp_offset += len; 1464a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 146551971139SChuck Lever xs_tcp_check_fraghdr(transport); 1466a246b010SChuck Lever } 1467a246b010SChuck Lever 14689903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1469a246b010SChuck Lever { 1470a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 147151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1472dd456471SChuck Lever struct xdr_skb_reader desc = { 1473a246b010SChuck Lever .skb = skb, 1474a246b010SChuck Lever .offset = offset, 1475a246b010SChuck Lever .count = len, 1476a246b010SChuck Lever }; 1477a246b010SChuck Lever 14789903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1479a246b010SChuck Lever do { 14801a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 1481a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1482a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1483e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 14849903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1485a246b010SChuck Lever continue; 1486a246b010SChuck Lever } 1487a246b010SChuck Lever /* Read in the xid if necessary */ 1488e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 148951971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1490a246b010SChuck Lever continue; 1491a246b010SChuck Lever } 149218dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1493f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { 149418dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 149518dca02aSRicardo Labiaga continue; 149618dca02aSRicardo Labiaga } 1497a246b010SChuck Lever /* Read in the request data */ 1498e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 149944b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1500a246b010SChuck Lever continue; 1501a246b010SChuck Lever } 1502a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 150351971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1504a246b010SChuck Lever } while (desc.count); 15051a867a08SJeff Layton trace_xs_tcp_data_recv(transport); 15069903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1507a246b010SChuck Lever return len - desc.count; 1508a246b010SChuck Lever } 1509a246b010SChuck Lever 151066d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport) 151166d7a56aSTrond Myklebust { 151266d7a56aSTrond Myklebust struct rpc_xprt *xprt = &transport->xprt; 151366d7a56aSTrond Myklebust struct sock *sk; 151466d7a56aSTrond Myklebust read_descriptor_t rd_desc = { 151566d7a56aSTrond Myklebust .count = 2*1024*1024, 151666d7a56aSTrond Myklebust .arg.data = xprt, 151766d7a56aSTrond Myklebust }; 151866d7a56aSTrond Myklebust unsigned long total = 0; 151966d7a56aSTrond Myklebust int read = 0; 152066d7a56aSTrond Myklebust 1521edc1b01cSTrond Myklebust mutex_lock(&transport->recv_mutex); 152266d7a56aSTrond Myklebust sk = transport->inet; 1523edc1b01cSTrond Myklebust if (sk == NULL) 1524edc1b01cSTrond Myklebust goto out; 152566d7a56aSTrond Myklebust 152666d7a56aSTrond Myklebust /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 152766d7a56aSTrond Myklebust for (;;) { 1528edc1b01cSTrond Myklebust lock_sock(sk); 152966d7a56aSTrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 153042d42a5bSTrond Myklebust if (read <= 0) { 153142d42a5bSTrond Myklebust clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 1532edc1b01cSTrond Myklebust release_sock(sk); 153342d42a5bSTrond Myklebust if (!test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 153466d7a56aSTrond Myklebust break; 153542d42a5bSTrond Myklebust } else { 153642d42a5bSTrond Myklebust release_sock(sk); 153766d7a56aSTrond Myklebust total += read; 153842d42a5bSTrond Myklebust } 153966d7a56aSTrond Myklebust rd_desc.count = 65536; 154066d7a56aSTrond Myklebust } 1541edc1b01cSTrond Myklebust out: 1542edc1b01cSTrond Myklebust mutex_unlock(&transport->recv_mutex); 154366d7a56aSTrond Myklebust trace_xs_tcp_data_ready(xprt, read, total); 154466d7a56aSTrond Myklebust } 154566d7a56aSTrond Myklebust 1546edc1b01cSTrond Myklebust static void xs_tcp_data_receive_workfn(struct work_struct *work) 1547edc1b01cSTrond Myklebust { 1548edc1b01cSTrond Myklebust struct sock_xprt *transport = 1549edc1b01cSTrond Myklebust container_of(work, struct sock_xprt, recv_worker); 1550edc1b01cSTrond Myklebust xs_tcp_data_receive(transport); 1551edc1b01cSTrond Myklebust } 1552edc1b01cSTrond Myklebust 15539903cd1cSChuck Lever /** 15549903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 15559903cd1cSChuck Lever * @sk: socket whose state has changed 15569903cd1cSChuck Lever * 15579903cd1cSChuck Lever */ 15589903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1559a246b010SChuck Lever { 1560a246b010SChuck Lever struct rpc_xprt *xprt; 15610fdea1e8STrond Myklebust struct sock_xprt *transport; 1562a246b010SChuck Lever 1563f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1564a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1565a246b010SChuck Lever goto out; 15669903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1567669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1568a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1569a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1570669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1571669502ffSAndy Chittenden sk->sk_shutdown); 1572a246b010SChuck Lever 15730fdea1e8STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 157440b5ea0cSTrond Myklebust trace_rpc_socket_state_change(xprt, sk->sk_socket); 1575a246b010SChuck Lever switch (sk->sk_state) { 1576a246b010SChuck Lever case TCP_ESTABLISHED: 1577f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1578a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 157951971139SChuck Lever 1580a246b010SChuck Lever /* Reset TCP record info */ 158151971139SChuck Lever transport->tcp_offset = 0; 158251971139SChuck Lever transport->tcp_reclen = 0; 158351971139SChuck Lever transport->tcp_copied = 0; 1584e136d092SChuck Lever transport->tcp_flags = 1585e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 15868b71798cSTrond Myklebust xprt->connect_cookie++; 15870fdea1e8STrond Myklebust clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 15880fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 158951971139SChuck Lever 15902a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1591a246b010SChuck Lever } 1592f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1593a246b010SChuck Lever break; 15943b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15953b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15967c1d71cfSTrond Myklebust xprt->connect_cookie++; 1597663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15983b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15994e857c58SPeter Zijlstra smp_mb__before_atomic(); 16003b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1601ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 16024e857c58SPeter Zijlstra smp_mb__after_atomic(); 1603a246b010SChuck Lever break; 1604632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 16053b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 16067c1d71cfSTrond Myklebust xprt->connect_cookie++; 1607d0bea455STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1608a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1609663b8858STrond Myklebust case TCP_CLOSING: 1610663b8858STrond Myklebust /* 1611663b8858STrond Myklebust * If the server closed down the connection, make sure that 1612663b8858STrond Myklebust * we back off before reconnecting 1613663b8858STrond Myklebust */ 1614663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1615663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 16163b948ae5STrond Myklebust break; 16173b948ae5STrond Myklebust case TCP_LAST_ACK: 1618670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 16194e857c58SPeter Zijlstra smp_mb__before_atomic(); 16203b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 16214e857c58SPeter Zijlstra smp_mb__after_atomic(); 16223b948ae5STrond Myklebust break; 16233b948ae5STrond Myklebust case TCP_CLOSE: 16240fdea1e8STrond Myklebust if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 16250fdea1e8STrond Myklebust &transport->sock_state)) 16260fdea1e8STrond Myklebust xprt_clear_connecting(xprt); 16277d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1628a246b010SChuck Lever } 1629a246b010SChuck Lever out: 1630f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1631a246b010SChuck Lever } 1632a246b010SChuck Lever 16331f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 16341f0fa154SIlpo Järvinen { 163513331a55STrond Myklebust struct socket_wq *wq; 16361f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 16371f0fa154SIlpo Järvinen 163813331a55STrond Myklebust if (!sk->sk_socket) 16391f0fa154SIlpo Järvinen return; 164013331a55STrond Myklebust clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 16411f0fa154SIlpo Järvinen 16421f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 16431f0fa154SIlpo Järvinen return; 164413331a55STrond Myklebust rcu_read_lock(); 164513331a55STrond Myklebust wq = rcu_dereference(sk->sk_wq); 164613331a55STrond Myklebust if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 164713331a55STrond Myklebust goto out; 16481f0fa154SIlpo Järvinen 16491f0fa154SIlpo Järvinen xprt_write_space(xprt); 165013331a55STrond Myklebust out: 165113331a55STrond Myklebust rcu_read_unlock(); 16521f0fa154SIlpo Järvinen } 16531f0fa154SIlpo Järvinen 16542a9e1cfaSTrond Myklebust /** 1655c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1656c7b2cae8SChuck Lever * becomes available 16579903cd1cSChuck Lever * @sk: socket whose state has changed 16589903cd1cSChuck Lever * 1659a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1660a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1661c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1662a246b010SChuck Lever * with a bunch of small requests. 1663a246b010SChuck Lever */ 1664c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1665a246b010SChuck Lever { 1666f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1667c7b2cae8SChuck Lever 1668c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16691f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16701f0fa154SIlpo Järvinen xs_write_space(sk); 1671c7b2cae8SChuck Lever 1672f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1673c7b2cae8SChuck Lever } 1674c7b2cae8SChuck Lever 1675c7b2cae8SChuck Lever /** 1676c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1677c7b2cae8SChuck Lever * becomes available 1678c7b2cae8SChuck Lever * @sk: socket whose state has changed 1679c7b2cae8SChuck Lever * 1680c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1681c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1682c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1683c7b2cae8SChuck Lever * with a bunch of small requests. 1684c7b2cae8SChuck Lever */ 1685c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1686c7b2cae8SChuck Lever { 1687f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1688c7b2cae8SChuck Lever 1689c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 169064dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 16911f0fa154SIlpo Järvinen xs_write_space(sk); 1692c7b2cae8SChuck Lever 1693f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1694a246b010SChuck Lever } 1695a246b010SChuck Lever 1696470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1697a246b010SChuck Lever { 1698ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1699ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1700a246b010SChuck Lever 17017c6e066eSChuck Lever if (transport->rcvsize) { 1702a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 17037c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1704a246b010SChuck Lever } 17057c6e066eSChuck Lever if (transport->sndsize) { 1706a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 17077c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1708a246b010SChuck Lever sk->sk_write_space(sk); 1709a246b010SChuck Lever } 1710a246b010SChuck Lever } 1711a246b010SChuck Lever 171243118c29SChuck Lever /** 1713470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 171443118c29SChuck Lever * @xprt: generic transport 1715470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1716470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 171743118c29SChuck Lever * 1718470056c2SChuck Lever * Set socket send and receive buffer size limits. 171943118c29SChuck Lever */ 1720470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 172143118c29SChuck Lever { 17227c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 17237c6e066eSChuck Lever 17247c6e066eSChuck Lever transport->sndsize = 0; 1725470056c2SChuck Lever if (sndsize) 17267c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 17277c6e066eSChuck Lever transport->rcvsize = 0; 1728470056c2SChuck Lever if (rcvsize) 17297c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1730470056c2SChuck Lever 1731470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 173243118c29SChuck Lever } 173343118c29SChuck Lever 173446c0ee8bSChuck Lever /** 173546c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 173646c0ee8bSChuck Lever * @task: task that timed out 173746c0ee8bSChuck Lever * 173846c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 173946c0ee8bSChuck Lever */ 17406a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 174146c0ee8bSChuck Lever { 1742b977b644SChuck Lever spin_lock_bh(&xprt->transport_lock); 17436a24dfb6STrond Myklebust xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1744b977b644SChuck Lever spin_unlock_bh(&xprt->transport_lock); 174546c0ee8bSChuck Lever } 174646c0ee8bSChuck Lever 1747b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1748b85d8806SChuck Lever { 17495d71899aSFrank Sorenson unsigned short range = xprt_max_resvport - xprt_min_resvport + 1; 175063862b5bSAruna-Hewapathirane unsigned short rand = (unsigned short) prandom_u32() % range; 1751b85d8806SChuck Lever return rand + xprt_min_resvport; 1752b85d8806SChuck Lever } 1753b85d8806SChuck Lever 175492200412SChuck Lever /** 17554dda9c8aSTrond Myklebust * xs_set_reuseaddr_port - set the socket's port and address reuse options 17564dda9c8aSTrond Myklebust * @sock: socket 17574dda9c8aSTrond Myklebust * 17584dda9c8aSTrond Myklebust * Note that this function has to be called on all sockets that share the 17594dda9c8aSTrond Myklebust * same port, and it must be called before binding. 17604dda9c8aSTrond Myklebust */ 17614dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock) 17624dda9c8aSTrond Myklebust { 1763402e23b4STrond Myklebust int opt = 1; 17644dda9c8aSTrond Myklebust 1765402e23b4STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1766402e23b4STrond Myklebust (char *)&opt, sizeof(opt)); 17674dda9c8aSTrond Myklebust } 17684dda9c8aSTrond Myklebust 17694dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock) 17704dda9c8aSTrond Myklebust { 17714dda9c8aSTrond Myklebust struct sockaddr_storage buf; 17724dda9c8aSTrond Myklebust int buflen; 17734dda9c8aSTrond Myklebust unsigned short port = 0; 17744dda9c8aSTrond Myklebust 17754dda9c8aSTrond Myklebust if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0) 17764dda9c8aSTrond Myklebust goto out; 17774dda9c8aSTrond Myklebust switch (buf.ss_family) { 17784dda9c8aSTrond Myklebust case AF_INET6: 17794dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 17804dda9c8aSTrond Myklebust break; 17814dda9c8aSTrond Myklebust case AF_INET: 17824dda9c8aSTrond Myklebust port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 17834dda9c8aSTrond Myklebust } 17844dda9c8aSTrond Myklebust out: 17854dda9c8aSTrond Myklebust return port; 17864dda9c8aSTrond Myklebust } 17874dda9c8aSTrond Myklebust 17884dda9c8aSTrond Myklebust /** 178992200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 179092200412SChuck Lever * @xprt: generic transport 179192200412SChuck Lever * @port: new port number 179292200412SChuck Lever * 179392200412SChuck Lever */ 179492200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 179592200412SChuck Lever { 179692200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1797c4efcb1dSChuck Lever 17989dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 17999dc3b095SChuck Lever xs_update_peer_port(xprt); 180092200412SChuck Lever } 180192200412SChuck Lever 18024dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 18034dda9c8aSTrond Myklebust { 18044dda9c8aSTrond Myklebust if (transport->srcport == 0) 18054dda9c8aSTrond Myklebust transport->srcport = xs_sock_getport(sock); 18064dda9c8aSTrond Myklebust } 18074dda9c8aSTrond Myklebust 18085d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport) 180967a391d7STrond Myklebust { 1810fbfffbd5SChuck Lever unsigned short port = transport->srcport; 181167a391d7STrond Myklebust 181267a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 181367a391d7STrond Myklebust port = xs_get_random_port(); 181467a391d7STrond Myklebust return port; 181567a391d7STrond Myklebust } 181667a391d7STrond Myklebust 1817baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 181867a391d7STrond Myklebust { 1819fbfffbd5SChuck Lever if (transport->srcport != 0) 1820fbfffbd5SChuck Lever transport->srcport = 0; 182167a391d7STrond Myklebust if (!transport->xprt.resvport) 182267a391d7STrond Myklebust return 0; 182367a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 182467a391d7STrond Myklebust return xprt_max_resvport; 182567a391d7STrond Myklebust return --port; 182667a391d7STrond Myklebust } 1827beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1828a246b010SChuck Lever { 1829beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 183067a391d7STrond Myklebust int err, nloop = 0; 18315d4ec932SPavel Emelyanov unsigned short port = xs_get_srcport(transport); 183267a391d7STrond Myklebust unsigned short last; 1833a246b010SChuck Lever 18340f7a622cSChris Perl /* 18350f7a622cSChris Perl * If we are asking for any ephemeral port (i.e. port == 0 && 18360f7a622cSChris Perl * transport->xprt.resvport == 0), don't bind. Let the local 18370f7a622cSChris Perl * port selection happen implicitly when the socket is used 18380f7a622cSChris Perl * (for example at connect time). 18390f7a622cSChris Perl * 18400f7a622cSChris Perl * This ensures that we can continue to establish TCP 18410f7a622cSChris Perl * connections even when all local ephemeral ports are already 18420f7a622cSChris Perl * a part of some TCP connection. This makes no difference 18430f7a622cSChris Perl * for UDP sockets, but also doens't harm them. 18440f7a622cSChris Perl * 18450f7a622cSChris Perl * If we're asking for any reserved port (i.e. port == 0 && 18460f7a622cSChris Perl * transport->xprt.resvport == 1) xs_get_srcport above will 18470f7a622cSChris Perl * ensure that port is non-zero and we will bind as needed. 18480f7a622cSChris Perl */ 18490f7a622cSChris Perl if (port == 0) 18500f7a622cSChris Perl return 0; 18510f7a622cSChris Perl 1852beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1853a246b010SChuck Lever do { 1854beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1855e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1856beb59b68SPavel Emelyanov transport->xprt.addrlen); 1857a246b010SChuck Lever if (err == 0) { 1858fbfffbd5SChuck Lever transport->srcport = port; 1859d3bc9a1dSFrank van Maarseveen break; 1860a246b010SChuck Lever } 186167a391d7STrond Myklebust last = port; 1862baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 186367a391d7STrond Myklebust if (port > last) 186467a391d7STrond Myklebust nloop++; 186567a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1866beb59b68SPavel Emelyanov 18674232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1868beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1869beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1870beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1871beb59b68SPavel Emelyanov else 1872beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1873beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 18747dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1875a246b010SChuck Lever return err; 1876a246b010SChuck Lever } 1877a246b010SChuck Lever 1878176e21eeSChuck Lever /* 1879176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1880176e21eeSChuck Lever */ 1881176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1882176e21eeSChuck Lever { 1883fb43d172STrond Myklebust xprt_set_bound(task->tk_xprt); 1884176e21eeSChuck Lever } 1885176e21eeSChuck Lever 1886176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1887176e21eeSChuck Lever { 1888176e21eeSChuck Lever } 1889a246b010SChuck Lever 1890ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1891ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1892ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1893ed07536eSPeter Zijlstra 1894176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1895176e21eeSChuck Lever { 1896176e21eeSChuck Lever struct sock *sk = sock->sk; 1897176e21eeSChuck Lever 1898176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1899176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1900176e21eeSChuck Lever } 1901176e21eeSChuck Lever 19028945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1903ed07536eSPeter Zijlstra { 1904ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 19058945ee5eSChuck Lever 19068945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 19078945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1908ed07536eSPeter Zijlstra } 19098945ee5eSChuck Lever 19108945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 19118945ee5eSChuck Lever { 19128945ee5eSChuck Lever struct sock *sk = sock->sk; 19138945ee5eSChuck Lever 19148945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 19158945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1916ed07536eSPeter Zijlstra } 19176bc9638aSPavel Emelyanov 19186bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19196bc9638aSPavel Emelyanov { 1920fafc4e1eSHannes Frederic Sowa if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 19211b7a1819SWeston Andros Adamson return; 19221b7a1819SWeston Andros Adamson 19234232e863SChuck Lever switch (family) { 1924176e21eeSChuck Lever case AF_LOCAL: 1925176e21eeSChuck Lever xs_reclassify_socketu(sock); 1926176e21eeSChuck Lever break; 19274232e863SChuck Lever case AF_INET: 19286bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 19294232e863SChuck Lever break; 19304232e863SChuck Lever case AF_INET6: 19316bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 19324232e863SChuck Lever break; 19334232e863SChuck Lever } 19346bc9638aSPavel Emelyanov } 1935ed07536eSPeter Zijlstra #else 19366bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 19376bc9638aSPavel Emelyanov { 19386bc9638aSPavel Emelyanov } 1939ed07536eSPeter Zijlstra #endif 1940ed07536eSPeter Zijlstra 194193dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work) 194293dc41bdSNeilBrown { 194393dc41bdSNeilBrown } 194493dc41bdSNeilBrown 19456bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 19464dda9c8aSTrond Myklebust struct sock_xprt *transport, int family, int type, 19474dda9c8aSTrond Myklebust int protocol, bool reuseport) 194822f79326SPavel Emelyanov { 194922f79326SPavel Emelyanov struct socket *sock; 195022f79326SPavel Emelyanov int err; 195122f79326SPavel Emelyanov 19526bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 195322f79326SPavel Emelyanov if (err < 0) { 195422f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 195522f79326SPavel Emelyanov protocol, -err); 195622f79326SPavel Emelyanov goto out; 195722f79326SPavel Emelyanov } 19586bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 195922f79326SPavel Emelyanov 19604dda9c8aSTrond Myklebust if (reuseport) 19614dda9c8aSTrond Myklebust xs_sock_set_reuseport(sock); 19624dda9c8aSTrond Myklebust 19634cea288aSBen Hutchings err = xs_bind(transport, sock); 19644cea288aSBen Hutchings if (err) { 196522f79326SPavel Emelyanov sock_release(sock); 196622f79326SPavel Emelyanov goto out; 196722f79326SPavel Emelyanov } 196822f79326SPavel Emelyanov 196922f79326SPavel Emelyanov return sock; 197022f79326SPavel Emelyanov out: 197122f79326SPavel Emelyanov return ERR_PTR(err); 197222f79326SPavel Emelyanov } 197322f79326SPavel Emelyanov 1974176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1975176e21eeSChuck Lever struct socket *sock) 1976176e21eeSChuck Lever { 1977176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1978176e21eeSChuck Lever xprt); 1979176e21eeSChuck Lever 1980176e21eeSChuck Lever if (!transport->inet) { 1981176e21eeSChuck Lever struct sock *sk = sock->sk; 1982176e21eeSChuck Lever 1983176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1984176e21eeSChuck Lever 1985176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1986176e21eeSChuck Lever 1987176e21eeSChuck Lever sk->sk_user_data = xprt; 1988a2648094STrond Myklebust sk->sk_data_ready = xs_data_ready; 1989176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1990b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 19912118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 1992c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 1993176e21eeSChuck Lever 1994176e21eeSChuck Lever xprt_clear_connected(xprt); 1995176e21eeSChuck Lever 1996176e21eeSChuck Lever /* Reset to new socket */ 1997176e21eeSChuck Lever transport->sock = sock; 1998176e21eeSChuck Lever transport->inet = sk; 1999176e21eeSChuck Lever 2000176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2001176e21eeSChuck Lever } 2002176e21eeSChuck Lever 2003176e21eeSChuck Lever /* Tell the socket layer to start connecting... */ 2004176e21eeSChuck Lever xprt->stat.connect_count++; 2005176e21eeSChuck Lever xprt->stat.connect_start = jiffies; 2006176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 2007176e21eeSChuck Lever } 2008176e21eeSChuck Lever 2009176e21eeSChuck Lever /** 2010176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 2011176e21eeSChuck Lever * @transport: socket transport to connect 2012176e21eeSChuck Lever */ 2013dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport) 2014176e21eeSChuck Lever { 2015176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2016176e21eeSChuck Lever struct socket *sock; 2017176e21eeSChuck Lever int status = -EIO; 2018176e21eeSChuck Lever 2019176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 2020176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 2021176e21eeSChuck Lever if (status < 0) { 2022176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 2023176e21eeSChuck Lever "transport socket (%d).\n", -status); 2024176e21eeSChuck Lever goto out; 2025176e21eeSChuck Lever } 2026d1358917SStefan Hajnoczi xs_reclassify_socket(AF_LOCAL, sock); 2027176e21eeSChuck Lever 2028176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 2029176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2030176e21eeSChuck Lever 2031176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 203240b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2033176e21eeSChuck Lever switch (status) { 2034176e21eeSChuck Lever case 0: 2035176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 2036176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2037176e21eeSChuck Lever xprt_set_connected(xprt); 20383601c4a9STrond Myklebust case -ENOBUFS: 2039176e21eeSChuck Lever break; 2040176e21eeSChuck Lever case -ENOENT: 2041176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 2042176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2043176e21eeSChuck Lever break; 20444a20a988STrond Myklebust case -ECONNREFUSED: 20454a20a988STrond Myklebust dprintk("RPC: xprt %p: connection refused for %s\n", 20464a20a988STrond Myklebust xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 20474a20a988STrond Myklebust break; 2048176e21eeSChuck Lever default: 2049176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2050176e21eeSChuck Lever __func__, -status, 2051176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2052176e21eeSChuck Lever } 2053176e21eeSChuck Lever 2054176e21eeSChuck Lever out: 2055176e21eeSChuck Lever xprt_clear_connecting(xprt); 2056176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 2057dc107402SJ. Bruce Fields return status; 2058dc107402SJ. Bruce Fields } 2059dc107402SJ. Bruce Fields 2060b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2061dc107402SJ. Bruce Fields { 2062dc107402SJ. Bruce Fields struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2063dc107402SJ. Bruce Fields int ret; 2064dc107402SJ. Bruce Fields 2065dc107402SJ. Bruce Fields if (RPC_IS_ASYNC(task)) { 2066dc107402SJ. Bruce Fields /* 2067dc107402SJ. Bruce Fields * We want the AF_LOCAL connect to be resolved in the 2068dc107402SJ. Bruce Fields * filesystem namespace of the process making the rpc 2069dc107402SJ. Bruce Fields * call. Thus we connect synchronously. 2070dc107402SJ. Bruce Fields * 2071dc107402SJ. Bruce Fields * If we want to support asynchronous AF_LOCAL calls, 2072dc107402SJ. Bruce Fields * we'll need to figure out how to pass a namespace to 2073dc107402SJ. Bruce Fields * connect. 2074dc107402SJ. Bruce Fields */ 2075dc107402SJ. Bruce Fields rpc_exit(task, -ENOTCONN); 2076dc107402SJ. Bruce Fields return; 2077dc107402SJ. Bruce Fields } 2078dc107402SJ. Bruce Fields ret = xs_local_setup_socket(transport); 2079dc107402SJ. Bruce Fields if (ret && !RPC_IS_SOFTCONN(task)) 2080dc107402SJ. Bruce Fields msleep_interruptible(15000); 2081176e21eeSChuck Lever } 2082176e21eeSChuck Lever 20833c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2084d6e971d8SJeff Layton /* 2085d6e971d8SJeff Layton * Note that this should be called with XPRT_LOCKED held (or when we otherwise 2086d6e971d8SJeff Layton * know that we have exclusive access to the socket), to guard against 2087d6e971d8SJeff Layton * races with xs_reset_transport. 2088d6e971d8SJeff Layton */ 2089a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2090a564b8f0SMel Gorman { 2091a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2092a564b8f0SMel Gorman xprt); 2093a564b8f0SMel Gorman 2094d6e971d8SJeff Layton /* 2095d6e971d8SJeff Layton * If there's no sock, then we have nothing to set. The 2096d6e971d8SJeff Layton * reconnecting process will get it for us. 2097d6e971d8SJeff Layton */ 2098d6e971d8SJeff Layton if (!transport->inet) 2099d6e971d8SJeff Layton return; 21008e228133SJeff Layton if (atomic_read(&xprt->swapper)) 2101a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 2102a564b8f0SMel Gorman } 2103a564b8f0SMel Gorman 2104a564b8f0SMel Gorman /** 2105d67fa4d8SJeff Layton * xs_enable_swap - Tag this transport as being used for swap. 2106a564b8f0SMel Gorman * @xprt: transport to tag 2107a564b8f0SMel Gorman * 21088e228133SJeff Layton * Take a reference to this transport on behalf of the rpc_clnt, and 21098e228133SJeff Layton * optionally mark it for swapping if it wasn't already. 2110a564b8f0SMel Gorman */ 2111d67fa4d8SJeff Layton static int 2112d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2113a564b8f0SMel Gorman { 2114d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2115a564b8f0SMel Gorman 2116d6e971d8SJeff Layton if (atomic_inc_return(&xprt->swapper) != 1) 2117d6e971d8SJeff Layton return 0; 2118d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2119d6e971d8SJeff Layton return -ERESTARTSYS; 2120d6e971d8SJeff Layton if (xs->inet) 2121d6e971d8SJeff Layton sk_set_memalloc(xs->inet); 2122d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 21238e228133SJeff Layton return 0; 2124a564b8f0SMel Gorman } 2125a564b8f0SMel Gorman 21268e228133SJeff Layton /** 2127d67fa4d8SJeff Layton * xs_disable_swap - Untag this transport as being used for swap. 21288e228133SJeff Layton * @xprt: transport to tag 21298e228133SJeff Layton * 21308e228133SJeff Layton * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 21318e228133SJeff Layton * swapper refcount goes to 0, untag the socket as a memalloc socket. 21328e228133SJeff Layton */ 2133d67fa4d8SJeff Layton static void 2134d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 21358e228133SJeff Layton { 2136d6e971d8SJeff Layton struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 21378e228133SJeff Layton 2138d6e971d8SJeff Layton if (!atomic_dec_and_test(&xprt->swapper)) 2139d6e971d8SJeff Layton return; 2140d6e971d8SJeff Layton if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2141d6e971d8SJeff Layton return; 2142d6e971d8SJeff Layton if (xs->inet) 2143d6e971d8SJeff Layton sk_clear_memalloc(xs->inet); 2144d6e971d8SJeff Layton xprt_release_xprt(xprt, NULL); 2145a564b8f0SMel Gorman } 2146a564b8f0SMel Gorman #else 2147a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 2148a564b8f0SMel Gorman { 2149a564b8f0SMel Gorman } 2150d67fa4d8SJeff Layton 2151d67fa4d8SJeff Layton static int 2152d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt) 2153d67fa4d8SJeff Layton { 2154d67fa4d8SJeff Layton return -EINVAL; 2155d67fa4d8SJeff Layton } 2156d67fa4d8SJeff Layton 2157d67fa4d8SJeff Layton static void 2158d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt) 2159d67fa4d8SJeff Layton { 2160d67fa4d8SJeff Layton } 2161a564b8f0SMel Gorman #endif 2162a564b8f0SMel Gorman 216316be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2164a246b010SChuck Lever { 216516be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2166edb267a6SChuck Lever 2167ee0ac0c2SChuck Lever if (!transport->inet) { 2168b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2169b0d93ad5SChuck Lever 2170b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2171b0d93ad5SChuck Lever 21722a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21732a9e1cfaSTrond Myklebust 2174b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2175f9b2ee71STrond Myklebust sk->sk_data_ready = xs_data_ready; 2176b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 2177b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 2178c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 2179b0d93ad5SChuck Lever 2180b0d93ad5SChuck Lever xprt_set_connected(xprt); 2181b0d93ad5SChuck Lever 2182b0d93ad5SChuck Lever /* Reset to new socket */ 2183ee0ac0c2SChuck Lever transport->sock = sock; 2184ee0ac0c2SChuck Lever transport->inet = sk; 2185b0d93ad5SChuck Lever 2186a564b8f0SMel Gorman xs_set_memalloc(xprt); 2187a564b8f0SMel Gorman 2188b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2189b0d93ad5SChuck Lever } 2190470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 219102910177STrond Myklebust 219202910177STrond Myklebust xprt->stat.connect_start = jiffies; 219316be2d20SChuck Lever } 219416be2d20SChuck Lever 21958c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2196a246b010SChuck Lever { 2197a246b010SChuck Lever struct sock_xprt *transport = 2198a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2199a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2200a246b010SChuck Lever struct socket *sock = transport->sock; 2201b65c0310SPavel Emelyanov int status = -EIO; 2202a246b010SChuck Lever 22038c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 22044dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_DGRAM, 22054dda9c8aSTrond Myklebust IPPROTO_UDP, false); 2206b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2207a246b010SChuck Lever goto out; 220868e220bdSChuck Lever 2209c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2210c740eff8SChuck Lever "%s (port %s)\n", xprt, 2211c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2212c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2213c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 221468e220bdSChuck Lever 221568e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 221640b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, 0); 2217a246b010SChuck Lever status = 0; 2218b0d93ad5SChuck Lever out: 2219718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 2220b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 22217d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2222b0d93ad5SChuck Lever } 2223b0d93ad5SChuck Lever 22244876cc77STrond Myklebust /** 22254876cc77STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 22264876cc77STrond Myklebust * @xprt: transport 22274876cc77STrond Myklebust * 22284876cc77STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 22294876cc77STrond Myklebust * equivalent of shutdown(SHUT_RDWR); 22304876cc77STrond Myklebust */ 22314876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 22324876cc77STrond Myklebust { 22334876cc77STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22344876cc77STrond Myklebust struct socket *sock = transport->sock; 22354876cc77STrond Myklebust 22364876cc77STrond Myklebust if (sock == NULL) 22374876cc77STrond Myklebust return; 22384876cc77STrond Myklebust if (xprt_connected(xprt)) { 22394876cc77STrond Myklebust kernel_sock_shutdown(sock, SHUT_RDWR); 22404876cc77STrond Myklebust trace_rpc_socket_shutdown(xprt, sock); 22414876cc77STrond Myklebust } else 22424876cc77STrond Myklebust xs_reset_transport(transport); 22434876cc77STrond Myklebust } 22444876cc77STrond Myklebust 22458d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 22468d1b8c62STrond Myklebust struct socket *sock) 2247b0d93ad5SChuck Lever { 22487196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22497196dbb0STrond Myklebust unsigned int keepidle; 22507196dbb0STrond Myklebust unsigned int keepcnt; 22517f260e85STrond Myklebust unsigned int opt_on = 1; 2252775f06abSTrond Myklebust unsigned int timeo; 22537f260e85STrond Myklebust 22547196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22557196dbb0STrond Myklebust keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 22567196dbb0STrond Myklebust keepcnt = xprt->timeout->to_retries + 1; 22577196dbb0STrond Myklebust timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 22587196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 22597196dbb0STrond Myklebust clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 22607196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 22617196dbb0STrond Myklebust 22627f260e85STrond Myklebust /* TCP Keepalive options */ 22637f260e85STrond Myklebust kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 22647f260e85STrond Myklebust (char *)&opt_on, sizeof(opt_on)); 22657f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 22667f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22677f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 22687f260e85STrond Myklebust (char *)&keepidle, sizeof(keepidle)); 22697f260e85STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 22707f260e85STrond Myklebust (char *)&keepcnt, sizeof(keepcnt)); 2271b0d93ad5SChuck Lever 22728d1b8c62STrond Myklebust /* TCP user timeout (see RFC5482) */ 22738d1b8c62STrond Myklebust kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 22748d1b8c62STrond Myklebust (char *)&timeo, sizeof(timeo)); 22758d1b8c62STrond Myklebust } 22768d1b8c62STrond Myklebust 22777196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 22787196dbb0STrond Myklebust unsigned long connect_timeout, 22797196dbb0STrond Myklebust unsigned long reconnect_timeout) 22807196dbb0STrond Myklebust { 22817196dbb0STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 22827196dbb0STrond Myklebust struct rpc_timeout to; 22837196dbb0STrond Myklebust unsigned long initval; 22847196dbb0STrond Myklebust 22857196dbb0STrond Myklebust spin_lock_bh(&xprt->transport_lock); 22867196dbb0STrond Myklebust if (reconnect_timeout < xprt->max_reconnect_timeout) 22877196dbb0STrond Myklebust xprt->max_reconnect_timeout = reconnect_timeout; 22887196dbb0STrond Myklebust if (connect_timeout < xprt->connect_timeout) { 22897196dbb0STrond Myklebust memcpy(&to, xprt->timeout, sizeof(to)); 22907196dbb0STrond Myklebust initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 22917196dbb0STrond Myklebust /* Arbitrary lower limit */ 22927196dbb0STrond Myklebust if (initval < XS_TCP_INIT_REEST_TO << 1) 22937196dbb0STrond Myklebust initval = XS_TCP_INIT_REEST_TO << 1; 22947196dbb0STrond Myklebust to.to_initval = initval; 22957196dbb0STrond Myklebust to.to_maxval = initval; 22967196dbb0STrond Myklebust memcpy(&transport->tcp_timeout, &to, 22977196dbb0STrond Myklebust sizeof(transport->tcp_timeout)); 22987196dbb0STrond Myklebust xprt->timeout = &transport->tcp_timeout; 22997196dbb0STrond Myklebust xprt->connect_timeout = connect_timeout; 23007196dbb0STrond Myklebust } 23017196dbb0STrond Myklebust set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 23027196dbb0STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 23037196dbb0STrond Myklebust } 23047196dbb0STrond Myklebust 23058d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 23068d1b8c62STrond Myklebust { 23078d1b8c62STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 23088d1b8c62STrond Myklebust int ret = -ENOTCONN; 23098d1b8c62STrond Myklebust 23108d1b8c62STrond Myklebust if (!transport->inet) { 23118d1b8c62STrond Myklebust struct sock *sk = sock->sk; 23128d1b8c62STrond Myklebust unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 23138d1b8c62STrond Myklebust 2314d88e4d82SNeilBrown /* Avoid temporary address, they are bad for long-lived 2315d88e4d82SNeilBrown * connections such as NFS mounts. 2316d88e4d82SNeilBrown * RFC4941, section 3.6 suggests that: 2317d88e4d82SNeilBrown * Individual applications, which have specific 2318d88e4d82SNeilBrown * knowledge about the normal duration of connections, 2319d88e4d82SNeilBrown * MAY override this as appropriate. 2320d88e4d82SNeilBrown */ 2321d88e4d82SNeilBrown kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2322d88e4d82SNeilBrown (char *)&addr_pref, sizeof(addr_pref)); 2323d88e4d82SNeilBrown 23248d1b8c62STrond Myklebust xs_tcp_set_socket_timeouts(xprt, sock); 2325775f06abSTrond Myklebust 2326b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2327b0d93ad5SChuck Lever 23282a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 23292a9e1cfaSTrond Myklebust 2330b0d93ad5SChuck Lever sk->sk_user_data = xprt; 23315157b956STrond Myklebust sk->sk_data_ready = xs_data_ready; 2332b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2333b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2334b4411457SEric Dumazet sock_set_flag(sk, SOCK_FASYNC); 23352118071dSTrond Myklebust sk->sk_error_report = xs_error_report; 2336c2126157STrond Myklebust sk->sk_allocation = GFP_NOIO; 23373167e12cSChuck Lever 23383167e12cSChuck Lever /* socket options */ 23393167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 23403167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2341b0d93ad5SChuck Lever 2342b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2343b0d93ad5SChuck Lever 2344b0d93ad5SChuck Lever /* Reset to new socket */ 2345ee0ac0c2SChuck Lever transport->sock = sock; 2346ee0ac0c2SChuck Lever transport->inet = sk; 2347b0d93ad5SChuck Lever 2348b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2349b0d93ad5SChuck Lever } 2350b0d93ad5SChuck Lever 235101d37c42STrond Myklebust if (!xprt_bound(xprt)) 2352fe19a96bSTrond Myklebust goto out; 235301d37c42STrond Myklebust 2354a564b8f0SMel Gorman xs_set_memalloc(xprt); 2355a564b8f0SMel Gorman 2356b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 2357262ca07dSChuck Lever xprt->stat.connect_count++; 2358262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 23590fdea1e8STrond Myklebust set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2360fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2361fe19a96bSTrond Myklebust switch (ret) { 2362fe19a96bSTrond Myklebust case 0: 23634dda9c8aSTrond Myklebust xs_set_srcport(transport, sock); 2364fe19a96bSTrond Myklebust case -EINPROGRESS: 2365fe19a96bSTrond Myklebust /* SYN_SENT! */ 2366fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2367fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 23681f4c17a0STrond Myklebust break; 23691f4c17a0STrond Myklebust case -EADDRNOTAVAIL: 23701f4c17a0STrond Myklebust /* Source port number is unavailable. Try a new one! */ 23711f4c17a0STrond Myklebust transport->srcport = 0; 2372fe19a96bSTrond Myklebust } 2373fe19a96bSTrond Myklebust out: 2374fe19a96bSTrond Myklebust return ret; 237516be2d20SChuck Lever } 237616be2d20SChuck Lever 237716be2d20SChuck Lever /** 2378b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 237916be2d20SChuck Lever * 238016be2d20SChuck Lever * Invoked by a work queue tasklet. 238116be2d20SChuck Lever */ 2382cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 238316be2d20SChuck Lever { 2384cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2385cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 238616be2d20SChuck Lever struct socket *sock = transport->sock; 2387a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2388b61d59ffSTrond Myklebust int status = -EIO; 238916be2d20SChuck Lever 239016be2d20SChuck Lever if (!sock) { 2391cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 23924dda9c8aSTrond Myklebust xs_addr(xprt)->sa_family, SOCK_STREAM, 23934dda9c8aSTrond Myklebust IPPROTO_TCP, true); 2394b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2395b61d59ffSTrond Myklebust status = PTR_ERR(sock); 239616be2d20SChuck Lever goto out; 239716be2d20SChuck Lever } 23987d1e8255STrond Myklebust } 23997d1e8255STrond Myklebust 2400c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2401c740eff8SChuck Lever "%s (port %s)\n", xprt, 2402c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2403c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2404c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 240516be2d20SChuck Lever 240616be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 240740b5ea0cSTrond Myklebust trace_rpc_socket_connect(xprt, sock, status); 2408a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 240946121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 241046121cf7SChuck Lever sock->sk->sk_state); 2411a246b010SChuck Lever switch (status) { 2412f75e6745STrond Myklebust default: 2413f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2414f75e6745STrond Myklebust __func__, status); 2415f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2416f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2417f75e6745STrond Myklebust * and retry 2418f75e6745STrond Myklebust */ 2419a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 242088b5ed73STrond Myklebust break; 24212a491991STrond Myklebust case 0: 2422a246b010SChuck Lever case -EINPROGRESS: 2423a246b010SChuck Lever case -EALREADY: 2424718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 24257d1e8255STrond Myklebust return; 24269fcfe0c8STrond Myklebust case -EINVAL: 24279fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 24289fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 24299fcfe0c8STrond Myklebust */ 24303ed5e2a2STrond Myklebust case -ECONNREFUSED: 24313ed5e2a2STrond Myklebust case -ECONNRESET: 24323ed5e2a2STrond Myklebust case -ENETUNREACH: 24333913c78cSTrond Myklebust case -EADDRINUSE: 24343601c4a9STrond Myklebust case -ENOBUFS: 24353ed5e2a2STrond Myklebust /* retry with existing socket, after a delay */ 24364efdd92cSTrond Myklebust xs_tcp_force_close(xprt); 24379fcfe0c8STrond Myklebust goto out; 24388a2cec29STrond Myklebust } 24392a491991STrond Myklebust status = -EAGAIN; 2440a246b010SChuck Lever out: 2441718ba5b8STrond Myklebust xprt_unlock_connect(xprt, transport); 24422226feb6SChuck Lever xprt_clear_connecting(xprt); 24437d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 2444a246b010SChuck Lever } 2445a246b010SChuck Lever 244602910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 244702910177STrond Myklebust { 244802910177STrond Myklebust unsigned long start, now = jiffies; 244902910177STrond Myklebust 245002910177STrond Myklebust start = xprt->stat.connect_start + xprt->reestablish_timeout; 245102910177STrond Myklebust if (time_after(start, now)) 245202910177STrond Myklebust return start - now; 245302910177STrond Myklebust return 0; 245402910177STrond Myklebust } 245502910177STrond Myklebust 24563851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt) 24573851f1cdSTrond Myklebust { 24583851f1cdSTrond Myklebust xprt->reestablish_timeout <<= 1; 24593851f1cdSTrond Myklebust if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 24603851f1cdSTrond Myklebust xprt->reestablish_timeout = xprt->max_reconnect_timeout; 24613851f1cdSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 24623851f1cdSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 24633851f1cdSTrond Myklebust } 24643851f1cdSTrond Myklebust 246568e220bdSChuck Lever /** 24669903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 24671b092092STrond Myklebust * @xprt: pointer to transport structure 24689903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 24699903cd1cSChuck Lever * 24709903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 247103bf4b70SChuck Lever * 247203bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 247303bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 247403bf4b70SChuck Lever * socket on a privileged port. 247503bf4b70SChuck Lever * 247603bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 247703bf4b70SChuck Lever * retry floods (hard mounts). 24789903cd1cSChuck Lever */ 24791b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2480a246b010SChuck Lever { 2481ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 248202910177STrond Myklebust unsigned long delay = 0; 2483a246b010SChuck Lever 2484718ba5b8STrond Myklebust WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2485718ba5b8STrond Myklebust 248699b1a4c3STrond Myklebust if (transport->sock != NULL) { 248746121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 248846121cf7SChuck Lever "seconds\n", 248903bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 249099b1a4c3STrond Myklebust 249199b1a4c3STrond Myklebust /* Start by resetting any existing state */ 249299b1a4c3STrond Myklebust xs_reset_transport(transport); 249399b1a4c3STrond Myklebust 249402910177STrond Myklebust delay = xs_reconnect_delay(xprt); 24953851f1cdSTrond Myklebust xs_reconnect_backoff(xprt); 249602910177STrond Myklebust 249702910177STrond Myklebust } else 24989903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 249902910177STrond Myklebust 250040a5f1b1STrond Myklebust queue_delayed_work(xprtiod_workqueue, 250102910177STrond Myklebust &transport->connect_worker, 250202910177STrond Myklebust delay); 2503a246b010SChuck Lever } 2504a246b010SChuck Lever 2505262ca07dSChuck Lever /** 2506176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2507176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2508176e21eeSChuck Lever * @seq: output file 2509176e21eeSChuck Lever * 2510176e21eeSChuck Lever */ 2511176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2512176e21eeSChuck Lever { 2513176e21eeSChuck Lever long idle_time = 0; 2514176e21eeSChuck Lever 2515176e21eeSChuck Lever if (xprt_connected(xprt)) 2516176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2517176e21eeSChuck Lever 2518176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 251915a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2520176e21eeSChuck Lever xprt->stat.bind_count, 2521176e21eeSChuck Lever xprt->stat.connect_count, 2522176e21eeSChuck Lever xprt->stat.connect_time, 2523176e21eeSChuck Lever idle_time, 2524176e21eeSChuck Lever xprt->stat.sends, 2525176e21eeSChuck Lever xprt->stat.recvs, 2526176e21eeSChuck Lever xprt->stat.bad_xids, 2527176e21eeSChuck Lever xprt->stat.req_u, 252815a45206SAndy Adamson xprt->stat.bklog_u, 252915a45206SAndy Adamson xprt->stat.max_slots, 253015a45206SAndy Adamson xprt->stat.sending_u, 253115a45206SAndy Adamson xprt->stat.pending_u); 2532176e21eeSChuck Lever } 2533176e21eeSChuck Lever 2534176e21eeSChuck Lever /** 2535262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2536262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2537262ca07dSChuck Lever * @seq: output file 2538262ca07dSChuck Lever * 2539262ca07dSChuck Lever */ 2540262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2541262ca07dSChuck Lever { 2542c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2543c8475461SChuck Lever 254415a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 254515a45206SAndy Adamson "%lu %llu %llu\n", 2546fbfffbd5SChuck Lever transport->srcport, 2547262ca07dSChuck Lever xprt->stat.bind_count, 2548262ca07dSChuck Lever xprt->stat.sends, 2549262ca07dSChuck Lever xprt->stat.recvs, 2550262ca07dSChuck Lever xprt->stat.bad_xids, 2551262ca07dSChuck Lever xprt->stat.req_u, 255215a45206SAndy Adamson xprt->stat.bklog_u, 255315a45206SAndy Adamson xprt->stat.max_slots, 255415a45206SAndy Adamson xprt->stat.sending_u, 255515a45206SAndy Adamson xprt->stat.pending_u); 2556262ca07dSChuck Lever } 2557262ca07dSChuck Lever 2558262ca07dSChuck Lever /** 2559262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2560262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2561262ca07dSChuck Lever * @seq: output file 2562262ca07dSChuck Lever * 2563262ca07dSChuck Lever */ 2564262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2565262ca07dSChuck Lever { 2566c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2567262ca07dSChuck Lever long idle_time = 0; 2568262ca07dSChuck Lever 2569262ca07dSChuck Lever if (xprt_connected(xprt)) 2570262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2571262ca07dSChuck Lever 257215a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 257315a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2574fbfffbd5SChuck Lever transport->srcport, 2575262ca07dSChuck Lever xprt->stat.bind_count, 2576262ca07dSChuck Lever xprt->stat.connect_count, 2577262ca07dSChuck Lever xprt->stat.connect_time, 2578262ca07dSChuck Lever idle_time, 2579262ca07dSChuck Lever xprt->stat.sends, 2580262ca07dSChuck Lever xprt->stat.recvs, 2581262ca07dSChuck Lever xprt->stat.bad_xids, 2582262ca07dSChuck Lever xprt->stat.req_u, 258315a45206SAndy Adamson xprt->stat.bklog_u, 258415a45206SAndy Adamson xprt->stat.max_slots, 258515a45206SAndy Adamson xprt->stat.sending_u, 258615a45206SAndy Adamson xprt->stat.pending_u); 2587262ca07dSChuck Lever } 2588262ca07dSChuck Lever 25894cfc7e60SRahul Iyer /* 25904cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 25914cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 25924cfc7e60SRahul Iyer * to use the server side send routines. 25934cfc7e60SRahul Iyer */ 25945fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task) 25954cfc7e60SRahul Iyer { 25965fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 25975fe6eaa1SChuck Lever size_t size = rqst->rq_callsize; 25984cfc7e60SRahul Iyer struct page *page; 25994cfc7e60SRahul Iyer struct rpc_buffer *buf; 26004cfc7e60SRahul Iyer 26015fe6eaa1SChuck Lever if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 26025fe6eaa1SChuck Lever WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 26035fe6eaa1SChuck Lever size); 26045fe6eaa1SChuck Lever return -EINVAL; 26055fe6eaa1SChuck Lever } 26064cfc7e60SRahul Iyer 2607b8a13d03SWeston Andros Adamson page = alloc_page(GFP_KERNEL); 26084cfc7e60SRahul Iyer if (!page) 26095fe6eaa1SChuck Lever return -ENOMEM; 26104cfc7e60SRahul Iyer 26114cfc7e60SRahul Iyer buf = page_address(page); 26124cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 26134cfc7e60SRahul Iyer 26145fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 261518e601d6SJeff Layton rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 26165fe6eaa1SChuck Lever return 0; 26174cfc7e60SRahul Iyer } 26184cfc7e60SRahul Iyer 26194cfc7e60SRahul Iyer /* 26204cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 26214cfc7e60SRahul Iyer */ 26223435c74aSChuck Lever static void bc_free(struct rpc_task *task) 26234cfc7e60SRahul Iyer { 26243435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 26254cfc7e60SRahul Iyer struct rpc_buffer *buf; 26264cfc7e60SRahul Iyer 26274cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 26284cfc7e60SRahul Iyer free_page((unsigned long)buf); 26294cfc7e60SRahul Iyer } 26304cfc7e60SRahul Iyer 26314cfc7e60SRahul Iyer /* 26324cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 26334cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 26344cfc7e60SRahul Iyer */ 26354cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 26364cfc7e60SRahul Iyer { 26374cfc7e60SRahul Iyer int len; 26384cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 26394cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 26404cfc7e60SRahul Iyer struct sock_xprt *transport = 26414cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 26424cfc7e60SRahul Iyer struct socket *sock = transport->sock; 26434cfc7e60SRahul Iyer unsigned long headoff; 26444cfc7e60SRahul Iyer unsigned long tailoff; 26454cfc7e60SRahul Iyer 264661677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 26474cfc7e60SRahul Iyer 26484cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 26494cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 26504cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 26514cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 26524cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 26534cfc7e60SRahul Iyer 26544cfc7e60SRahul Iyer if (len != xbufp->len) { 26554cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 26564cfc7e60SRahul Iyer len = -EAGAIN; 26574cfc7e60SRahul Iyer } 26584cfc7e60SRahul Iyer 26594cfc7e60SRahul Iyer return len; 26604cfc7e60SRahul Iyer } 26614cfc7e60SRahul Iyer 26624cfc7e60SRahul Iyer /* 26634cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 26644cfc7e60SRahul Iyer */ 26654cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task) 26664cfc7e60SRahul Iyer { 26674cfc7e60SRahul Iyer struct rpc_rqst *req = task->tk_rqstp; 26684cfc7e60SRahul Iyer struct svc_xprt *xprt; 26697fc56136SAndrzej Hajda int len; 26704cfc7e60SRahul Iyer 26714cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 26724cfc7e60SRahul Iyer /* 26734cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 26744cfc7e60SRahul Iyer */ 26754cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 26764cfc7e60SRahul Iyer 26774cfc7e60SRahul Iyer /* 26784cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 26794cfc7e60SRahul Iyer * with the fore channel 26804cfc7e60SRahul Iyer */ 26814cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) { 26824cfc7e60SRahul Iyer rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); 26834cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) 26844cfc7e60SRahul Iyer return -EAGAIN; 26854cfc7e60SRahul Iyer rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); 26864cfc7e60SRahul Iyer } 26874cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 26884cfc7e60SRahul Iyer len = -ENOTCONN; 26894cfc7e60SRahul Iyer else 26904cfc7e60SRahul Iyer len = bc_sendto(req); 26914cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 26924cfc7e60SRahul Iyer 26934cfc7e60SRahul Iyer if (len > 0) 26944cfc7e60SRahul Iyer len = 0; 26954cfc7e60SRahul Iyer 26964cfc7e60SRahul Iyer return len; 26974cfc7e60SRahul Iyer } 26984cfc7e60SRahul Iyer 26994cfc7e60SRahul Iyer /* 27004cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 27014cfc7e60SRahul Iyer */ 27024cfc7e60SRahul Iyer 27034cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 27044cfc7e60SRahul Iyer { 27054cfc7e60SRahul Iyer } 27064cfc7e60SRahul Iyer 27074cfc7e60SRahul Iyer /* 27084cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 27094cfc7e60SRahul Iyer * initiated, we do nothing 27104cfc7e60SRahul Iyer */ 27114cfc7e60SRahul Iyer 27124cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 27134cfc7e60SRahul Iyer { 271447f72efaSKinglong Mee dprintk("RPC: bc_destroy xprt %p\n", xprt); 271547f72efaSKinglong Mee 271647f72efaSKinglong Mee xs_xprt_free(xprt); 271747f72efaSKinglong Mee module_put(THIS_MODULE); 27184cfc7e60SRahul Iyer } 27194cfc7e60SRahul Iyer 2720176e21eeSChuck Lever static struct rpc_xprt_ops xs_local_ops = { 2721176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 2722176e21eeSChuck Lever .release_xprt = xs_tcp_release_xprt, 2723f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2724176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2725176e21eeSChuck Lever .set_port = xs_local_set_port, 2726dc107402SJ. Bruce Fields .connect = xs_local_connect, 2727176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2728176e21eeSChuck Lever .buf_free = rpc_free, 2729176e21eeSChuck Lever .send_request = xs_local_send_request, 2730176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2731176e21eeSChuck Lever .close = xs_close, 2732a1311d87STrond Myklebust .destroy = xs_destroy, 2733176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2734d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2735d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 2736176e21eeSChuck Lever }; 2737176e21eeSChuck Lever 2738262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 273943118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 274012a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 274149e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2742f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 274345160d62SChuck Lever .rpcbind = rpcb_getport_async, 274492200412SChuck Lever .set_port = xs_set_port, 27459903cd1cSChuck Lever .connect = xs_connect, 274602107148SChuck Lever .buf_alloc = rpc_malloc, 274702107148SChuck Lever .buf_free = rpc_free, 2748262965f5SChuck Lever .send_request = xs_udp_send_request, 2749fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 275046c0ee8bSChuck Lever .timer = xs_udp_timer, 2751a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2752262965f5SChuck Lever .close = xs_close, 2753262965f5SChuck Lever .destroy = xs_destroy, 2754262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2755d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2756d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27574a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 2758262965f5SChuck Lever }; 2759262965f5SChuck Lever 2760262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 276112a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 2762e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 2763f39c1bfbSTrond Myklebust .alloc_slot = xprt_lock_and_alloc_slot, 276445160d62SChuck Lever .rpcbind = rpcb_getport_async, 276592200412SChuck Lever .set_port = xs_set_port, 27660b9e7943STrond Myklebust .connect = xs_connect, 276702107148SChuck Lever .buf_alloc = rpc_malloc, 276802107148SChuck Lever .buf_free = rpc_free, 2769262965f5SChuck Lever .send_request = xs_tcp_send_request, 2770fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2771c627d31bSTrond Myklebust .close = xs_tcp_shutdown, 27729903cd1cSChuck Lever .destroy = xs_destroy, 27737196dbb0STrond Myklebust .set_connect_timeout = xs_tcp_set_connect_timeout, 2774262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2775d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2776d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 27774a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 277842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL 277942e5c3e2SChuck Lever .bc_setup = xprt_setup_bc, 278076566773SChuck Lever .bc_up = xs_tcp_bc_up, 27816b26cc8cSChuck Lever .bc_maxpayload = xs_tcp_bc_maxpayload, 278242e5c3e2SChuck Lever .bc_free_rqst = xprt_free_bc_rqst, 278342e5c3e2SChuck Lever .bc_destroy = xprt_destroy_bc, 278442e5c3e2SChuck Lever #endif 2785a246b010SChuck Lever }; 2786a246b010SChuck Lever 27874cfc7e60SRahul Iyer /* 27884cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 27894cfc7e60SRahul Iyer */ 27904cfc7e60SRahul Iyer 27914cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = { 27924cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 27934cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 279484e28a30SBryan Schumaker .alloc_slot = xprt_alloc_slot, 27954cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 27964cfc7e60SRahul Iyer .buf_free = bc_free, 27974cfc7e60SRahul Iyer .send_request = bc_send_request, 27984cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 27994cfc7e60SRahul Iyer .close = bc_close, 28004cfc7e60SRahul Iyer .destroy = bc_destroy, 28014cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 2802d67fa4d8SJeff Layton .enable_swap = xs_enable_swap, 2803d67fa4d8SJeff Layton .disable_swap = xs_disable_swap, 28044a068258SChuck Lever .inject_disconnect = xs_inject_disconnect, 28054cfc7e60SRahul Iyer }; 28064cfc7e60SRahul Iyer 280792476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 280892476850SChuck Lever { 280992476850SChuck Lever static const struct sockaddr_in sin = { 281092476850SChuck Lever .sin_family = AF_INET, 281192476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 281292476850SChuck Lever }; 281392476850SChuck Lever static const struct sockaddr_in6 sin6 = { 281492476850SChuck Lever .sin6_family = AF_INET6, 281592476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 281692476850SChuck Lever }; 281792476850SChuck Lever 281892476850SChuck Lever switch (family) { 2819176e21eeSChuck Lever case AF_LOCAL: 2820176e21eeSChuck Lever break; 282192476850SChuck Lever case AF_INET: 282292476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 282392476850SChuck Lever break; 282492476850SChuck Lever case AF_INET6: 282592476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 282692476850SChuck Lever break; 282792476850SChuck Lever default: 282892476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 282992476850SChuck Lever return -EAFNOSUPPORT; 283092476850SChuck Lever } 283192476850SChuck Lever return 0; 283292476850SChuck Lever } 283392476850SChuck Lever 28343c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2835d9ba131dSTrond Myklebust unsigned int slot_table_size, 2836d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2837c8541ecdSChuck Lever { 2838c8541ecdSChuck Lever struct rpc_xprt *xprt; 2839ffc2e518SChuck Lever struct sock_xprt *new; 2840c8541ecdSChuck Lever 284196802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2842c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2843c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2844c8541ecdSChuck Lever } 2845c8541ecdSChuck Lever 2846d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2847d9ba131dSTrond Myklebust max_slot_table_size); 2848bd1722d4SPavel Emelyanov if (xprt == NULL) { 284946121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 285046121cf7SChuck Lever "rpc_xprt\n"); 2851c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2852c8541ecdSChuck Lever } 2853c8541ecdSChuck Lever 2854bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 2855edc1b01cSTrond Myklebust mutex_init(&new->recv_mutex); 285696802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 285796802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2858d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2859fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 286092476850SChuck Lever else { 286192476850SChuck Lever int err; 286292476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 286392476850SChuck Lever (struct sockaddr *)&new->srcaddr); 28642aa13531SStanislav Kinsbursky if (err != 0) { 28652aa13531SStanislav Kinsbursky xprt_free(xprt); 286692476850SChuck Lever return ERR_PTR(err); 286792476850SChuck Lever } 28682aa13531SStanislav Kinsbursky } 2869c8541ecdSChuck Lever 2870c8541ecdSChuck Lever return xprt; 2871c8541ecdSChuck Lever } 2872c8541ecdSChuck Lever 2873176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2874176e21eeSChuck Lever .to_initval = 10 * HZ, 2875176e21eeSChuck Lever .to_maxval = 10 * HZ, 2876176e21eeSChuck Lever .to_retries = 2, 2877176e21eeSChuck Lever }; 2878176e21eeSChuck Lever 2879176e21eeSChuck Lever /** 2880176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2881176e21eeSChuck Lever * @args: rpc transport creation arguments 2882176e21eeSChuck Lever * 2883176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2884176e21eeSChuck Lever */ 2885176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2886176e21eeSChuck Lever { 2887176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2888176e21eeSChuck Lever struct sock_xprt *transport; 2889176e21eeSChuck Lever struct rpc_xprt *xprt; 2890176e21eeSChuck Lever struct rpc_xprt *ret; 2891176e21eeSChuck Lever 2892d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2893d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2894176e21eeSChuck Lever if (IS_ERR(xprt)) 2895176e21eeSChuck Lever return xprt; 2896176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2897176e21eeSChuck Lever 2898176e21eeSChuck Lever xprt->prot = 0; 2899176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2900176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2901176e21eeSChuck Lever 2902176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2903176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2904176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2905176e21eeSChuck Lever 2906176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2907176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2908176e21eeSChuck Lever 2909a2648094STrond Myklebust INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn); 291093dc41bdSNeilBrown INIT_DELAYED_WORK(&transport->connect_worker, 291193dc41bdSNeilBrown xs_dummy_setup_socket); 291293dc41bdSNeilBrown 2913176e21eeSChuck Lever switch (sun->sun_family) { 2914176e21eeSChuck Lever case AF_LOCAL: 2915176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2916176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2917176e21eeSChuck Lever sun->sun_path); 2918176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2919176e21eeSChuck Lever goto out_err; 2920176e21eeSChuck Lever } 2921176e21eeSChuck Lever xprt_set_bound(xprt); 2922176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 29237073ea87SJ. Bruce Fields ret = ERR_PTR(xs_local_setup_socket(transport)); 29247073ea87SJ. Bruce Fields if (ret) 29257073ea87SJ. Bruce Fields goto out_err; 2926176e21eeSChuck Lever break; 2927176e21eeSChuck Lever default: 2928176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2929176e21eeSChuck Lever goto out_err; 2930176e21eeSChuck Lever } 2931176e21eeSChuck Lever 2932176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2933176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2934176e21eeSChuck Lever 2935176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2936176e21eeSChuck Lever return xprt; 2937176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2938176e21eeSChuck Lever out_err: 2939315f3812SKinglong Mee xs_xprt_free(xprt); 2940176e21eeSChuck Lever return ret; 2941176e21eeSChuck Lever } 2942176e21eeSChuck Lever 29432881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 29442881ae74STrond Myklebust .to_initval = 5 * HZ, 29452881ae74STrond Myklebust .to_maxval = 30 * HZ, 29462881ae74STrond Myklebust .to_increment = 5 * HZ, 29472881ae74STrond Myklebust .to_retries = 5, 29482881ae74STrond Myklebust }; 29492881ae74STrond Myklebust 29509903cd1cSChuck Lever /** 29519903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 295296802a09SFrank van Maarseveen * @args: rpc transport creation arguments 29539903cd1cSChuck Lever * 29549903cd1cSChuck Lever */ 2955483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2956a246b010SChuck Lever { 29578f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2958c8541ecdSChuck Lever struct rpc_xprt *xprt; 2959c8475461SChuck Lever struct sock_xprt *transport; 29600a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2961a246b010SChuck Lever 2962d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2963d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2964c8541ecdSChuck Lever if (IS_ERR(xprt)) 2965c8541ecdSChuck Lever return xprt; 2966c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2967a246b010SChuck Lever 2968ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2969808012fbSChuck Lever xprt->tsh_size = 0; 2970a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2971a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2972a246b010SChuck Lever 297303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 297403bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 297503bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2976a246b010SChuck Lever 2977262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2978a246b010SChuck Lever 2979ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2980a246b010SChuck Lever 2981f9b2ee71STrond Myklebust INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 2982edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2983edc1b01cSTrond Myklebust 29848f9d5b1aSChuck Lever switch (addr->sa_family) { 29858f9d5b1aSChuck Lever case AF_INET: 29868f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 29878f9d5b1aSChuck Lever xprt_set_bound(xprt); 29888f9d5b1aSChuck Lever 29899dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 29908f9d5b1aSChuck Lever break; 29918f9d5b1aSChuck Lever case AF_INET6: 29928f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 29938f9d5b1aSChuck Lever xprt_set_bound(xprt); 29948f9d5b1aSChuck Lever 29959dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 29968f9d5b1aSChuck Lever break; 29978f9d5b1aSChuck Lever default: 29980a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 29990a68b0beSJ. Bruce Fields goto out_err; 30008f9d5b1aSChuck Lever } 30018f9d5b1aSChuck Lever 3002c740eff8SChuck Lever if (xprt_bound(xprt)) 3003c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3004c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3005c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3006c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3007c740eff8SChuck Lever else 3008c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3009c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3010c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3011edb267a6SChuck Lever 3012bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3013c8541ecdSChuck Lever return xprt; 30140a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30150a68b0beSJ. Bruce Fields out_err: 3016315f3812SKinglong Mee xs_xprt_free(xprt); 30170a68b0beSJ. Bruce Fields return ret; 3018a246b010SChuck Lever } 3019a246b010SChuck Lever 30202881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 30212881ae74STrond Myklebust .to_initval = 60 * HZ, 30222881ae74STrond Myklebust .to_maxval = 60 * HZ, 30232881ae74STrond Myklebust .to_retries = 2, 30242881ae74STrond Myklebust }; 30252881ae74STrond Myklebust 30269903cd1cSChuck Lever /** 30279903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 302896802a09SFrank van Maarseveen * @args: rpc transport creation arguments 30299903cd1cSChuck Lever * 30309903cd1cSChuck Lever */ 3031483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3032a246b010SChuck Lever { 30338f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 3034c8541ecdSChuck Lever struct rpc_xprt *xprt; 3035c8475461SChuck Lever struct sock_xprt *transport; 30360a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3037b7993cebSTrond Myklebust unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3038b7993cebSTrond Myklebust 3039b7993cebSTrond Myklebust if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3040b7993cebSTrond Myklebust max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3041a246b010SChuck Lever 3042d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3043b7993cebSTrond Myklebust max_slot_table_size); 3044c8541ecdSChuck Lever if (IS_ERR(xprt)) 3045c8541ecdSChuck Lever return xprt; 3046c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 3047a246b010SChuck Lever 3048ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 3049808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3050808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3051a246b010SChuck Lever 305203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 305303bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 305403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 3055a246b010SChuck Lever 3056262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 3057ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 3058a246b010SChuck Lever 30593851f1cdSTrond Myklebust xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 30607196dbb0STrond Myklebust xprt->connect_timeout = xprt->timeout->to_initval * 30617196dbb0STrond Myklebust (xprt->timeout->to_retries + 1); 30623851f1cdSTrond Myklebust 3063edc1b01cSTrond Myklebust INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn); 3064edc1b01cSTrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3065edc1b01cSTrond Myklebust 30668f9d5b1aSChuck Lever switch (addr->sa_family) { 30678f9d5b1aSChuck Lever case AF_INET: 30688f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 30698f9d5b1aSChuck Lever xprt_set_bound(xprt); 30708f9d5b1aSChuck Lever 30719dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 30728f9d5b1aSChuck Lever break; 30738f9d5b1aSChuck Lever case AF_INET6: 30748f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 30758f9d5b1aSChuck Lever xprt_set_bound(xprt); 30768f9d5b1aSChuck Lever 30779dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 30788f9d5b1aSChuck Lever break; 30798f9d5b1aSChuck Lever default: 30800a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 30810a68b0beSJ. Bruce Fields goto out_err; 30828f9d5b1aSChuck Lever } 30838f9d5b1aSChuck Lever 3084c740eff8SChuck Lever if (xprt_bound(xprt)) 3085c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3086c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3087c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 3088c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3089c740eff8SChuck Lever else 3090c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3091c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 3092c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 3093c740eff8SChuck Lever 3094bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 3095c8541ecdSChuck Lever return xprt; 30960a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 30970a68b0beSJ. Bruce Fields out_err: 3098315f3812SKinglong Mee xs_xprt_free(xprt); 30990a68b0beSJ. Bruce Fields return ret; 3100a246b010SChuck Lever } 3101282b32e1SChuck Lever 3102f300babaSAlexandros Batsakis /** 3103f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3104f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 3105f300babaSAlexandros Batsakis * 3106f300babaSAlexandros Batsakis */ 3107f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3108f300babaSAlexandros Batsakis { 3109f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 3110f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 3111f300babaSAlexandros Batsakis struct sock_xprt *transport; 3112f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 31130a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 3114f300babaSAlexandros Batsakis 3115d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3116d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 3117f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 3118f300babaSAlexandros Batsakis return xprt; 3119f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 3120f300babaSAlexandros Batsakis 3121f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 3122f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3123f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3124f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 3125f300babaSAlexandros Batsakis 3126f300babaSAlexandros Batsakis /* backchannel */ 3127f300babaSAlexandros Batsakis xprt_set_bound(xprt); 3128f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 3129f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 3130f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 3131f300babaSAlexandros Batsakis 3132f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 3133f300babaSAlexandros Batsakis 3134f300babaSAlexandros Batsakis switch (addr->sa_family) { 3135f300babaSAlexandros Batsakis case AF_INET: 3136f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3137f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 3138f300babaSAlexandros Batsakis break; 3139f300babaSAlexandros Batsakis case AF_INET6: 3140f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 3141f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 3142f300babaSAlexandros Batsakis break; 3143f300babaSAlexandros Batsakis default: 31440a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 31450a68b0beSJ. Bruce Fields goto out_err; 3146f300babaSAlexandros Batsakis } 3147f300babaSAlexandros Batsakis 3148f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3149f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 3150f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 3151f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 3152f300babaSAlexandros Batsakis 3153f300babaSAlexandros Batsakis /* 315499de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 315528303ca3SWeng Meiling * we want to keep it around as long as the connection lasts, 315628303ca3SWeng Meiling * in case we need to start using it for a backchannel again; 315728303ca3SWeng Meiling * this reference won't be dropped until bc_xprt is destroyed. 315899de8ea9SJ. Bruce Fields */ 315999de8ea9SJ. Bruce Fields xprt_get(xprt); 316099de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 316199de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 316299de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 316399de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 316499de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 316599de8ea9SJ. Bruce Fields 316699de8ea9SJ. Bruce Fields /* 3167f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 3168f300babaSAlexandros Batsakis * the xprt status to connected 3169f300babaSAlexandros Batsakis */ 3170f300babaSAlexandros Batsakis xprt_set_connected(xprt); 3171f300babaSAlexandros Batsakis 3172f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 3173f300babaSAlexandros Batsakis return xprt; 3174642aab58SKinglong Mee 3175642aab58SKinglong Mee args->bc_xprt->xpt_bc_xprt = NULL; 317639a9beabSJ. Bruce Fields args->bc_xprt->xpt_bc_xps = NULL; 317799de8ea9SJ. Bruce Fields xprt_put(xprt); 31780a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 31790a68b0beSJ. Bruce Fields out_err: 3180315f3812SKinglong Mee xs_xprt_free(xprt); 31810a68b0beSJ. Bruce Fields return ret; 3182f300babaSAlexandros Batsakis } 3183f300babaSAlexandros Batsakis 3184176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 3185176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 3186176e21eeSChuck Lever .name = "named UNIX socket", 3187176e21eeSChuck Lever .owner = THIS_MODULE, 3188176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 3189176e21eeSChuck Lever .setup = xs_setup_local, 3190176e21eeSChuck Lever }; 3191176e21eeSChuck Lever 3192bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 3193bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 3194bc25571eS\"Talpey, Thomas\ .name = "udp", 3195bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3196f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 3197bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 3198bc25571eS\"Talpey, Thomas\ }; 3199bc25571eS\"Talpey, Thomas\ 3200bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 3201bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3202bc25571eS\"Talpey, Thomas\ .name = "tcp", 3203bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 3204f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 3205bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 3206bc25571eS\"Talpey, Thomas\ }; 3207bc25571eS\"Talpey, Thomas\ 3208f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 3209f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3210f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 3211f300babaSAlexandros Batsakis .owner = THIS_MODULE, 3212f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 3213f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 3214f300babaSAlexandros Batsakis }; 3215f300babaSAlexandros Batsakis 3216282b32e1SChuck Lever /** 3217bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3218282b32e1SChuck Lever * 3219282b32e1SChuck Lever */ 3220282b32e1SChuck Lever int init_socket_xprt(void) 3221282b32e1SChuck Lever { 3222f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 32232b1bec5fSEric W. Biederman if (!sunrpc_table_header) 32240b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 3225fbf76683SChuck Lever #endif 3226fbf76683SChuck Lever 3227176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 3228bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 3229bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 3230f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 3231bc25571eS\"Talpey, Thomas\ 3232282b32e1SChuck Lever return 0; 3233282b32e1SChuck Lever } 3234282b32e1SChuck Lever 3235282b32e1SChuck Lever /** 3236bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3237282b32e1SChuck Lever * 3238282b32e1SChuck Lever */ 3239282b32e1SChuck Lever void cleanup_socket_xprt(void) 3240282b32e1SChuck Lever { 3241f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3242fbf76683SChuck Lever if (sunrpc_table_header) { 3243fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 3244fbf76683SChuck Lever sunrpc_table_header = NULL; 3245fbf76683SChuck Lever } 3246fbf76683SChuck Lever #endif 3247bc25571eS\"Talpey, Thomas\ 3248176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 3249bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 3250bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 3251f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 3252282b32e1SChuck Lever } 3253cbf11071STrond Myklebust 32549bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 32559bbb9e5aSRusty Russell const struct kernel_param *kp, 3256cbf11071STrond Myklebust unsigned int min, unsigned int max) 3257cbf11071STrond Myklebust { 325800cfaa94SDaniel Walter unsigned int num; 3259cbf11071STrond Myklebust int ret; 3260cbf11071STrond Myklebust 3261cbf11071STrond Myklebust if (!val) 3262cbf11071STrond Myklebust return -EINVAL; 326300cfaa94SDaniel Walter ret = kstrtouint(val, 0, &num); 3264*9761a246SDan Carpenter if (ret) 3265*9761a246SDan Carpenter return ret; 3266*9761a246SDan Carpenter if (num < min || num > max) 3267cbf11071STrond Myklebust return -EINVAL; 3268cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3269cbf11071STrond Myklebust return 0; 3270cbf11071STrond Myklebust } 3271cbf11071STrond Myklebust 32729bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3273cbf11071STrond Myklebust { 3274ffb6ca33SFrank Sorenson if (kp->arg == &xprt_min_resvport) 3275cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3276cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3277ffb6ca33SFrank Sorenson xprt_max_resvport); 3278ffb6ca33SFrank Sorenson return param_set_uint_minmax(val, kp, 3279ffb6ca33SFrank Sorenson xprt_min_resvport, 3280cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3281cbf11071STrond Myklebust } 3282cbf11071STrond Myklebust 32839c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = { 32849bbb9e5aSRusty Russell .set = param_set_portnr, 32859bbb9e5aSRusty Russell .get = param_get_uint, 32869bbb9e5aSRusty Russell }; 32879bbb9e5aSRusty Russell 3288cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3289cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3290cbf11071STrond Myklebust 3291cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3292cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3293cbf11071STrond Myklebust 32949bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 32959bbb9e5aSRusty Russell const struct kernel_param *kp) 3296cbf11071STrond Myklebust { 3297cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3298cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3299cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3300cbf11071STrond Myklebust } 3301cbf11071STrond Myklebust 33029c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = { 33039bbb9e5aSRusty Russell .set = param_set_slot_table_size, 33049bbb9e5aSRusty Russell .get = param_get_uint, 33059bbb9e5aSRusty Russell }; 33069bbb9e5aSRusty Russell 3307cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3308cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3309cbf11071STrond Myklebust 3310d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3311d9ba131dSTrond Myklebust const struct kernel_param *kp) 3312d9ba131dSTrond Myklebust { 3313d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3314d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3315d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3316d9ba131dSTrond Myklebust } 3317d9ba131dSTrond Myklebust 33189c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = { 3319d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3320d9ba131dSTrond Myklebust .get = param_get_uint, 3321d9ba131dSTrond Myklebust }; 3322d9ba131dSTrond Myklebust 3323d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3324d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3325d9ba131dSTrond Myklebust 3326cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3327cbf11071STrond Myklebust slot_table_size, 0644); 3328d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3329d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3330cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3331cbf11071STrond Myklebust slot_table_size, 0644); 3332cbf11071STrond Myklebust 3333