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> 3602107148SChuck Lever #include <linux/sunrpc/sched.h> 374cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h> 3849c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 39a246b010SChuck Lever #include <linux/file.h> 409e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL 4144b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h> 4244b98efdSRicardo Labiaga #endif 43a246b010SChuck Lever 44a246b010SChuck Lever #include <net/sock.h> 45a246b010SChuck Lever #include <net/checksum.h> 46a246b010SChuck Lever #include <net/udp.h> 47a246b010SChuck Lever #include <net/tcp.h> 48a246b010SChuck Lever 494cfc7e60SRahul Iyer #include "sunrpc.h" 50176e21eeSChuck Lever 51176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt); 52176e21eeSChuck Lever 539903cd1cSChuck Lever /* 54c556b754SChuck Lever * xprtsock tunables 55c556b754SChuck Lever */ 5609acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 5709acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 5809acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 59c556b754SChuck Lever 6009acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 6109acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 62c556b754SChuck Lever 637d1e8255STrond Myklebust #define XS_TCP_LINGER_TO (15U * HZ) 6425fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 657d1e8255STrond Myklebust 66c556b754SChuck Lever /* 67fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 68fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 69fbf76683SChuck Lever * directory become the union of all files registered there. 70fbf76683SChuck Lever * 71fbf76683SChuck Lever * We simply need to make sure that we don't collide with 72fbf76683SChuck Lever * someone else's file names! 73fbf76683SChuck Lever */ 74fbf76683SChuck Lever 75fbf76683SChuck Lever #ifdef RPC_DEBUG 76fbf76683SChuck Lever 77fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 78fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 79d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 80fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 81fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 82fbf76683SChuck Lever 83fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 84fbf76683SChuck Lever 85fbf76683SChuck Lever /* 86fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 87fbf76683SChuck Lever * socket buffers for existing UDP transports 88fbf76683SChuck Lever */ 89fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 90fbf76683SChuck Lever { 91fbf76683SChuck Lever .procname = "udp_slot_table_entries", 92fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 93fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 94fbf76683SChuck Lever .mode = 0644, 956d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 96fbf76683SChuck Lever .extra1 = &min_slot_table_size, 97fbf76683SChuck Lever .extra2 = &max_slot_table_size 98fbf76683SChuck Lever }, 99fbf76683SChuck Lever { 100fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 101fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 102fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 103fbf76683SChuck Lever .mode = 0644, 1046d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 105fbf76683SChuck Lever .extra1 = &min_slot_table_size, 106fbf76683SChuck Lever .extra2 = &max_slot_table_size 107fbf76683SChuck Lever }, 108fbf76683SChuck Lever { 109d9ba131dSTrond Myklebust .procname = "tcp_max_slot_table_entries", 110d9ba131dSTrond Myklebust .data = &xprt_max_tcp_slot_table_entries, 111d9ba131dSTrond Myklebust .maxlen = sizeof(unsigned int), 112d9ba131dSTrond Myklebust .mode = 0644, 113d9ba131dSTrond Myklebust .proc_handler = proc_dointvec_minmax, 114d9ba131dSTrond Myklebust .extra1 = &min_slot_table_size, 115d9ba131dSTrond Myklebust .extra2 = &max_tcp_slot_table_limit 116d9ba131dSTrond Myklebust }, 117d9ba131dSTrond Myklebust { 118fbf76683SChuck Lever .procname = "min_resvport", 119fbf76683SChuck Lever .data = &xprt_min_resvport, 120fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 121fbf76683SChuck Lever .mode = 0644, 1226d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 123fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 124fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 125fbf76683SChuck Lever }, 126fbf76683SChuck Lever { 127fbf76683SChuck Lever .procname = "max_resvport", 128fbf76683SChuck Lever .data = &xprt_max_resvport, 129fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 130fbf76683SChuck Lever .mode = 0644, 1316d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 132fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 133fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 134fbf76683SChuck Lever }, 135fbf76683SChuck Lever { 13625fe6142STrond Myklebust .procname = "tcp_fin_timeout", 13725fe6142STrond Myklebust .data = &xs_tcp_fin_timeout, 13825fe6142STrond Myklebust .maxlen = sizeof(xs_tcp_fin_timeout), 13925fe6142STrond Myklebust .mode = 0644, 1406d456111SEric W. Biederman .proc_handler = proc_dointvec_jiffies, 14125fe6142STrond Myklebust }, 142f8572d8fSEric W. Biederman { }, 143fbf76683SChuck Lever }; 144fbf76683SChuck Lever 145fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 146fbf76683SChuck Lever { 147fbf76683SChuck Lever .procname = "sunrpc", 148fbf76683SChuck Lever .mode = 0555, 149fbf76683SChuck Lever .child = xs_tunables_table 150fbf76683SChuck Lever }, 151f8572d8fSEric W. Biederman { }, 152fbf76683SChuck Lever }; 153fbf76683SChuck Lever 154fbf76683SChuck Lever #endif 155fbf76683SChuck Lever 156fbf76683SChuck Lever /* 15703bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 15803bf4b70SChuck Lever */ 15903bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 16003bf4b70SChuck Lever 16103bf4b70SChuck Lever /* 16203bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16303bf4b70SChuck Lever * kind of resource problem on the local host. 16403bf4b70SChuck Lever */ 16503bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 16603bf4b70SChuck Lever 16703bf4b70SChuck Lever /* 16803bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 16903bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 17003bf4b70SChuck Lever * 17103bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 17203bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17303bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17403bf4b70SChuck Lever * increase over time if the server is down or not responding. 17503bf4b70SChuck Lever */ 17603bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 17703bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 17803bf4b70SChuck Lever 17903bf4b70SChuck Lever /* 18003bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 18103bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 18203bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18303bf4b70SChuck Lever */ 18403bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18503bf4b70SChuck Lever 186a246b010SChuck Lever #ifdef RPC_DEBUG 187a246b010SChuck Lever # undef RPC_DEBUG_DATA 1889903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 189a246b010SChuck Lever #endif 190a246b010SChuck Lever 191a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1929903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 193a246b010SChuck Lever { 194a246b010SChuck Lever u8 *buf = (u8 *) packet; 195a246b010SChuck Lever int j; 196a246b010SChuck Lever 197a246b010SChuck Lever dprintk("RPC: %s\n", msg); 198a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 199a246b010SChuck Lever if (!(j & 31)) { 200a246b010SChuck Lever if (j) 201a246b010SChuck Lever dprintk("\n"); 202a246b010SChuck Lever dprintk("0x%04x ", j); 203a246b010SChuck Lever } 204a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 205a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 206a246b010SChuck Lever } 207a246b010SChuck Lever dprintk("\n"); 208a246b010SChuck Lever } 209a246b010SChuck Lever #else 2109903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 211a246b010SChuck Lever { 212a246b010SChuck Lever /* NOP */ 213a246b010SChuck Lever } 214a246b010SChuck Lever #endif 215a246b010SChuck Lever 216ffc2e518SChuck Lever struct sock_xprt { 217ffc2e518SChuck Lever struct rpc_xprt xprt; 218ee0ac0c2SChuck Lever 219ee0ac0c2SChuck Lever /* 220ee0ac0c2SChuck Lever * Network layer 221ee0ac0c2SChuck Lever */ 222ee0ac0c2SChuck Lever struct socket * sock; 223ee0ac0c2SChuck Lever struct sock * inet; 22451971139SChuck Lever 22551971139SChuck Lever /* 22651971139SChuck Lever * State of TCP reply receive 22751971139SChuck Lever */ 22851971139SChuck Lever __be32 tcp_fraghdr, 229b76ce561STrond Myklebust tcp_xid, 230b76ce561STrond Myklebust tcp_calldir; 23151971139SChuck Lever 23251971139SChuck Lever u32 tcp_offset, 23351971139SChuck Lever tcp_reclen; 23451971139SChuck Lever 23551971139SChuck Lever unsigned long tcp_copied, 23651971139SChuck Lever tcp_flags; 237c8475461SChuck Lever 238c8475461SChuck Lever /* 239c8475461SChuck Lever * Connection of transports 240c8475461SChuck Lever */ 24134161db6STrond Myklebust struct delayed_work connect_worker; 242fbfffbd5SChuck Lever struct sockaddr_storage srcaddr; 243fbfffbd5SChuck Lever unsigned short srcport; 2447c6e066eSChuck Lever 2457c6e066eSChuck Lever /* 2467c6e066eSChuck Lever * UDP socket buffer size parameters 2477c6e066eSChuck Lever */ 2487c6e066eSChuck Lever size_t rcvsize, 2497c6e066eSChuck Lever sndsize; 250314dfd79SChuck Lever 251314dfd79SChuck Lever /* 252314dfd79SChuck Lever * Saved socket callback addresses 253314dfd79SChuck Lever */ 254314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 255314dfd79SChuck Lever void (*old_state_change)(struct sock *); 256314dfd79SChuck Lever void (*old_write_space)(struct sock *); 2572a9e1cfaSTrond Myklebust void (*old_error_report)(struct sock *); 258ffc2e518SChuck Lever }; 259ffc2e518SChuck Lever 260e136d092SChuck Lever /* 261e136d092SChuck Lever * TCP receive state flags 262e136d092SChuck Lever */ 263e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 264e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 265e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 266e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 267f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR (1UL << 4) 268f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR (1UL << 5) 26918dca02aSRicardo Labiaga 27018dca02aSRicardo Labiaga /* 27118dca02aSRicardo Labiaga * TCP RPC flags 27218dca02aSRicardo Labiaga */ 273f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY (1UL << 6) 274e136d092SChuck Lever 27595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 276edb267a6SChuck Lever { 27795392c59SChuck Lever return (struct sockaddr *) &xprt->addr; 27895392c59SChuck Lever } 27995392c59SChuck Lever 280176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 281176e21eeSChuck Lever { 282176e21eeSChuck Lever return (struct sockaddr_un *) &xprt->addr; 283176e21eeSChuck Lever } 284176e21eeSChuck Lever 28595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 28695392c59SChuck Lever { 28795392c59SChuck Lever return (struct sockaddr_in *) &xprt->addr; 28895392c59SChuck Lever } 28995392c59SChuck Lever 29095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 29195392c59SChuck Lever { 29295392c59SChuck Lever return (struct sockaddr_in6 *) &xprt->addr; 29395392c59SChuck Lever } 29495392c59SChuck Lever 295c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 296c877b849SChuck Lever { 297c877b849SChuck Lever struct sockaddr *sap = xs_addr(xprt); 2989dc3b095SChuck Lever struct sockaddr_in6 *sin6; 2999dc3b095SChuck Lever struct sockaddr_in *sin; 300176e21eeSChuck Lever struct sockaddr_un *sun; 301c877b849SChuck Lever char buf[128]; 302c877b849SChuck Lever 3039dc3b095SChuck Lever switch (sap->sa_family) { 304176e21eeSChuck Lever case AF_LOCAL: 305176e21eeSChuck Lever sun = xs_addr_un(xprt); 306176e21eeSChuck Lever strlcpy(buf, sun->sun_path, sizeof(buf)); 307176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 308176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 309176e21eeSChuck Lever break; 3109dc3b095SChuck Lever case AF_INET: 311176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 312176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 313176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 3149dc3b095SChuck Lever sin = xs_addr_in(xprt); 315fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 3169dc3b095SChuck Lever break; 3179dc3b095SChuck Lever case AF_INET6: 318176e21eeSChuck Lever (void)rpc_ntop(sap, buf, sizeof(buf)); 319176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = 320176e21eeSChuck Lever kstrdup(buf, GFP_KERNEL); 3219dc3b095SChuck Lever sin6 = xs_addr_in6(xprt); 322fc0b5791SJoe Perches snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 3239dc3b095SChuck Lever break; 3249dc3b095SChuck Lever default: 3259dc3b095SChuck Lever BUG(); 3269dc3b095SChuck Lever } 327176e21eeSChuck Lever 3289dc3b095SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 3299dc3b095SChuck Lever } 3309dc3b095SChuck Lever 3319dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 3329dc3b095SChuck Lever { 3339dc3b095SChuck Lever struct sockaddr *sap = xs_addr(xprt); 3349dc3b095SChuck Lever char buf[128]; 3359dc3b095SChuck Lever 33681160e66SJoe Perches snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 337c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 338c877b849SChuck Lever 33981160e66SJoe Perches snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 340c877b849SChuck Lever xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 341c877b849SChuck Lever } 342c877b849SChuck Lever 3439dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt, 344b454ae90SChuck Lever const char *protocol, 345b454ae90SChuck Lever const char *netid) 346edb267a6SChuck Lever { 347b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 348b454ae90SChuck Lever xprt->address_strings[RPC_DISPLAY_NETID] = netid; 349c877b849SChuck Lever xs_format_common_peer_addresses(xprt); 3509dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 351edb267a6SChuck Lever } 352edb267a6SChuck Lever 3539dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt) 3544b6473fbSChuck Lever { 3559dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 3569dc3b095SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 3574b6473fbSChuck Lever 3589dc3b095SChuck Lever xs_format_common_peer_ports(xprt); 359edb267a6SChuck Lever } 360edb267a6SChuck Lever 361edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 362edb267a6SChuck Lever { 36333e01dc7SChuck Lever unsigned int i; 36433e01dc7SChuck Lever 36533e01dc7SChuck Lever for (i = 0; i < RPC_DISPLAY_MAX; i++) 36633e01dc7SChuck Lever switch (i) { 36733e01dc7SChuck Lever case RPC_DISPLAY_PROTO: 36833e01dc7SChuck Lever case RPC_DISPLAY_NETID: 36933e01dc7SChuck Lever continue; 37033e01dc7SChuck Lever default: 37133e01dc7SChuck Lever kfree(xprt->address_strings[i]); 37233e01dc7SChuck Lever } 373edb267a6SChuck Lever } 374edb267a6SChuck Lever 375b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 376b4b5cc85SChuck Lever 37724c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 378b4b5cc85SChuck Lever { 379b4b5cc85SChuck Lever struct msghdr msg = { 380b4b5cc85SChuck Lever .msg_name = addr, 381b4b5cc85SChuck Lever .msg_namelen = addrlen, 38224c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 38324c5684bSTrond Myklebust }; 38424c5684bSTrond Myklebust struct kvec iov = { 38524c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 38624c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 387b4b5cc85SChuck Lever }; 388b4b5cc85SChuck Lever 38924c5684bSTrond Myklebust if (iov.iov_len != 0) 390b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 391b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 392b4b5cc85SChuck Lever } 393b4b5cc85SChuck Lever 39424c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 395b4b5cc85SChuck Lever { 39624c5684bSTrond Myklebust struct page **ppage; 39724c5684bSTrond Myklebust unsigned int remainder; 39824c5684bSTrond Myklebust int err, sent = 0; 399b4b5cc85SChuck Lever 40024c5684bSTrond Myklebust remainder = xdr->page_len - base; 40124c5684bSTrond Myklebust base += xdr->page_base; 40224c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 40324c5684bSTrond Myklebust base &= ~PAGE_MASK; 40424c5684bSTrond Myklebust for(;;) { 40524c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 40624c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 40724c5684bSTrond Myklebust 40824c5684bSTrond Myklebust remainder -= len; 40924c5684bSTrond Myklebust if (remainder != 0 || more) 41024c5684bSTrond Myklebust flags |= MSG_MORE; 41124c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 41224c5684bSTrond Myklebust if (remainder == 0 || err != len) 41324c5684bSTrond Myklebust break; 41424c5684bSTrond Myklebust sent += err; 41524c5684bSTrond Myklebust ppage++; 41624c5684bSTrond Myklebust base = 0; 41724c5684bSTrond Myklebust } 41824c5684bSTrond Myklebust if (sent == 0) 41924c5684bSTrond Myklebust return err; 42024c5684bSTrond Myklebust if (err > 0) 42124c5684bSTrond Myklebust sent += err; 42224c5684bSTrond Myklebust return sent; 423b4b5cc85SChuck Lever } 424b4b5cc85SChuck Lever 4259903cd1cSChuck Lever /** 4269903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 4279903cd1cSChuck Lever * @sock: socket to send on 4289903cd1cSChuck Lever * @addr: UDP only -- address of destination 4299903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 4309903cd1cSChuck Lever * @xdr: buffer containing this request 4319903cd1cSChuck Lever * @base: starting position in the buffer 4329903cd1cSChuck Lever * 433a246b010SChuck Lever */ 43424c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 435a246b010SChuck Lever { 43624c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 43724c5684bSTrond Myklebust int err, sent = 0; 438a246b010SChuck Lever 439262965f5SChuck Lever if (unlikely(!sock)) 440fba91afbSTrond Myklebust return -ENOTSOCK; 441262965f5SChuck Lever 442262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 44324c5684bSTrond Myklebust if (base != 0) { 44424c5684bSTrond Myklebust addr = NULL; 44524c5684bSTrond Myklebust addrlen = 0; 44624c5684bSTrond Myklebust } 447262965f5SChuck Lever 44824c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 44924c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 45024c5684bSTrond Myklebust remainder -= len; 45124c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 45224c5684bSTrond Myklebust if (remainder == 0 || err != len) 453a246b010SChuck Lever goto out; 45424c5684bSTrond Myklebust sent += err; 455a246b010SChuck Lever base = 0; 456a246b010SChuck Lever } else 45724c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 458a246b010SChuck Lever 45924c5684bSTrond Myklebust if (base < xdr->page_len) { 46024c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 46124c5684bSTrond Myklebust remainder -= len; 46224c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 46324c5684bSTrond Myklebust if (remainder == 0 || err != len) 464a246b010SChuck Lever goto out; 46524c5684bSTrond Myklebust sent += err; 466a246b010SChuck Lever base = 0; 46724c5684bSTrond Myklebust } else 46824c5684bSTrond Myklebust base -= xdr->page_len; 46924c5684bSTrond Myklebust 47024c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 47124c5684bSTrond Myklebust return sent; 47224c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 473a246b010SChuck Lever out: 47424c5684bSTrond Myklebust if (sent == 0) 47524c5684bSTrond Myklebust return err; 47624c5684bSTrond Myklebust if (err > 0) 47724c5684bSTrond Myklebust sent += err; 47824c5684bSTrond Myklebust return sent; 479a246b010SChuck Lever } 480a246b010SChuck Lever 481b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task) 482b6ddf64fSTrond Myklebust { 483b6ddf64fSTrond Myklebust struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); 484b6ddf64fSTrond Myklebust 485b6ddf64fSTrond Myklebust transport->inet->sk_write_pending--; 486b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 487b6ddf64fSTrond Myklebust } 488b6ddf64fSTrond Myklebust 4899903cd1cSChuck Lever /** 490262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 491262965f5SChuck Lever * @task: task to put to sleep 4929903cd1cSChuck Lever * 493a246b010SChuck Lever */ 4945e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task) 495a246b010SChuck Lever { 496262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 497262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 498ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 49924ca9a84STrond Myklebust int ret = -EAGAIN; 500a246b010SChuck Lever 50146121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 502262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 503262965f5SChuck Lever req->rq_slen); 504a246b010SChuck Lever 505262965f5SChuck Lever /* Protect against races with write_space */ 506262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 507a246b010SChuck Lever 508262965f5SChuck Lever /* Don't race with disconnect */ 509b6ddf64fSTrond Myklebust if (xprt_connected(xprt)) { 510b6ddf64fSTrond Myklebust if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 511b6ddf64fSTrond Myklebust /* 512b6ddf64fSTrond Myklebust * Notify TCP that we're limited by the application 513b6ddf64fSTrond Myklebust * window size 514b6ddf64fSTrond Myklebust */ 515b6ddf64fSTrond Myklebust set_bit(SOCK_NOSPACE, &transport->sock->flags); 516b6ddf64fSTrond Myklebust transport->inet->sk_write_pending++; 517b6ddf64fSTrond Myklebust /* ...and wait for more buffer space */ 518b6ddf64fSTrond Myklebust xprt_wait_for_buffer_space(task, xs_nospace_callback); 519b6ddf64fSTrond Myklebust } 520b6ddf64fSTrond Myklebust } else { 521b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 5225e3771ceSTrond Myklebust ret = -ENOTCONN; 523b6ddf64fSTrond Myklebust } 524a246b010SChuck Lever 525262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5265e3771ceSTrond Myklebust return ret; 527a246b010SChuck Lever } 528a246b010SChuck Lever 52961677eeeSChuck Lever /* 53061677eeeSChuck Lever * Construct a stream transport record marker in @buf. 53161677eeeSChuck Lever */ 53261677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 53361677eeeSChuck Lever { 53461677eeeSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 53561677eeeSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 53661677eeeSChuck Lever *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 53761677eeeSChuck Lever } 53861677eeeSChuck Lever 5399903cd1cSChuck Lever /** 540176e21eeSChuck Lever * xs_local_send_request - write an RPC request to an AF_LOCAL socket 541176e21eeSChuck Lever * @task: RPC task that manages the state of an RPC request 542176e21eeSChuck Lever * 543176e21eeSChuck Lever * Return values: 544176e21eeSChuck Lever * 0: The request has been sent 545176e21eeSChuck Lever * EAGAIN: The socket was blocked, please call again later to 546176e21eeSChuck Lever * complete the request 547176e21eeSChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 548176e21eeSChuck Lever * other: Some other error occured, the request was not sent 549176e21eeSChuck Lever */ 550176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task) 551176e21eeSChuck Lever { 552176e21eeSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 553176e21eeSChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 554176e21eeSChuck Lever struct sock_xprt *transport = 555176e21eeSChuck Lever container_of(xprt, struct sock_xprt, xprt); 556176e21eeSChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 557176e21eeSChuck Lever int status; 558176e21eeSChuck Lever 559176e21eeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 560176e21eeSChuck Lever 561176e21eeSChuck Lever xs_pktdump("packet data:", 562176e21eeSChuck Lever req->rq_svec->iov_base, req->rq_svec->iov_len); 563176e21eeSChuck Lever 564176e21eeSChuck Lever status = xs_sendpages(transport->sock, NULL, 0, 565176e21eeSChuck Lever xdr, req->rq_bytes_sent); 566176e21eeSChuck Lever dprintk("RPC: %s(%u) = %d\n", 567176e21eeSChuck Lever __func__, xdr->len - req->rq_bytes_sent, status); 568176e21eeSChuck Lever if (likely(status >= 0)) { 569176e21eeSChuck Lever req->rq_bytes_sent += status; 570176e21eeSChuck Lever req->rq_xmit_bytes_sent += status; 571176e21eeSChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 572176e21eeSChuck Lever req->rq_bytes_sent = 0; 573176e21eeSChuck Lever return 0; 574176e21eeSChuck Lever } 575176e21eeSChuck Lever status = -EAGAIN; 576176e21eeSChuck Lever } 577176e21eeSChuck Lever 578176e21eeSChuck Lever switch (status) { 579176e21eeSChuck Lever case -EAGAIN: 580176e21eeSChuck Lever status = xs_nospace(task); 581176e21eeSChuck Lever break; 582176e21eeSChuck Lever default: 583176e21eeSChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 584176e21eeSChuck Lever -status); 585176e21eeSChuck Lever case -EPIPE: 586176e21eeSChuck Lever xs_close(xprt); 587176e21eeSChuck Lever status = -ENOTCONN; 588176e21eeSChuck Lever } 589176e21eeSChuck Lever 590176e21eeSChuck Lever return status; 591176e21eeSChuck Lever } 592176e21eeSChuck Lever 593176e21eeSChuck Lever /** 594262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 5959903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 5969903cd1cSChuck Lever * 5979903cd1cSChuck Lever * Return values: 5989903cd1cSChuck Lever * 0: The request has been sent 5999903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 6009903cd1cSChuck Lever * complete the request 601262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 60225985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 6039903cd1cSChuck Lever */ 604262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 605a246b010SChuck Lever { 606a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 607a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 608ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 609262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 610262965f5SChuck Lever int status; 611262965f5SChuck Lever 612262965f5SChuck Lever xs_pktdump("packet data:", 613262965f5SChuck Lever req->rq_svec->iov_base, 614262965f5SChuck Lever req->rq_svec->iov_len); 615262965f5SChuck Lever 61601d37c42STrond Myklebust if (!xprt_bound(xprt)) 61701d37c42STrond Myklebust return -ENOTCONN; 618ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 61995392c59SChuck Lever xs_addr(xprt), 620ee0ac0c2SChuck Lever xprt->addrlen, xdr, 621ee0ac0c2SChuck Lever req->rq_bytes_sent); 622262965f5SChuck Lever 623262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 624262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 625262965f5SChuck Lever 6262199700fSTrond Myklebust if (status >= 0) { 627d60dbb20STrond Myklebust req->rq_xmit_bytes_sent += status; 6282199700fSTrond Myklebust if (status >= req->rq_slen) 629262965f5SChuck Lever return 0; 630262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 631262965f5SChuck Lever status = -EAGAIN; 6322199700fSTrond Myklebust } 633262965f5SChuck Lever 634262965f5SChuck Lever switch (status) { 635fba91afbSTrond Myklebust case -ENOTSOCK: 636fba91afbSTrond Myklebust status = -ENOTCONN; 637fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 638fba91afbSTrond Myklebust break; 639b6ddf64fSTrond Myklebust case -EAGAIN: 6405e3771ceSTrond Myklebust status = xs_nospace(task); 641b6ddf64fSTrond Myklebust break; 642c8485e4dSTrond Myklebust default: 643c8485e4dSTrond Myklebust dprintk("RPC: sendmsg returned unrecognized error %d\n", 644c8485e4dSTrond Myklebust -status); 645262965f5SChuck Lever case -ENETUNREACH: 646262965f5SChuck Lever case -EPIPE: 647262965f5SChuck Lever case -ECONNREFUSED: 648262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 649262965f5SChuck Lever * prompts ECONNREFUSED. */ 650b6ddf64fSTrond Myklebust clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 651262965f5SChuck Lever } 6525fe46e9dSBian Naimeng 653262965f5SChuck Lever return status; 654262965f5SChuck Lever } 655262965f5SChuck Lever 656e06799f9STrond Myklebust /** 657e06799f9STrond Myklebust * xs_tcp_shutdown - gracefully shut down a TCP socket 658e06799f9STrond Myklebust * @xprt: transport 659e06799f9STrond Myklebust * 660e06799f9STrond Myklebust * Initiates a graceful shutdown of the TCP socket by calling the 661e06799f9STrond Myklebust * equivalent of shutdown(SHUT_WR); 662e06799f9STrond Myklebust */ 663e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt) 664e06799f9STrond Myklebust { 665e06799f9STrond Myklebust struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 666e06799f9STrond Myklebust struct socket *sock = transport->sock; 667e06799f9STrond Myklebust 668e06799f9STrond Myklebust if (sock != NULL) 669e06799f9STrond Myklebust kernel_sock_shutdown(sock, SHUT_WR); 670e06799f9STrond Myklebust } 671e06799f9STrond Myklebust 672262965f5SChuck Lever /** 673262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 674262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 675262965f5SChuck Lever * 676262965f5SChuck Lever * Return values: 677262965f5SChuck Lever * 0: The request has been sent 678262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 679262965f5SChuck Lever * complete the request 680262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 68125985edcSLucas De Marchi * other: Some other error occurred, the request was not sent 682262965f5SChuck Lever * 683262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 684262965f5SChuck Lever * if sendmsg is not able to make progress? 685262965f5SChuck Lever */ 686262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 687262965f5SChuck Lever { 688262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 689262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 690ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 691262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 692b595bb15SChuck Lever int status; 693a246b010SChuck Lever 69461677eeeSChuck Lever xs_encode_stream_record_marker(&req->rq_snd_buf); 695262965f5SChuck Lever 696262965f5SChuck Lever xs_pktdump("packet data:", 697262965f5SChuck Lever req->rq_svec->iov_base, 698262965f5SChuck Lever req->rq_svec->iov_len); 699a246b010SChuck Lever 700a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 701a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 702262965f5SChuck Lever * called sendmsg(). */ 703a246b010SChuck Lever while (1) { 704ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 705ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 706a246b010SChuck Lever 707262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 708262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 709262965f5SChuck Lever 710262965f5SChuck Lever if (unlikely(status < 0)) 711a246b010SChuck Lever break; 712a246b010SChuck Lever 713a246b010SChuck Lever /* If we've sent the entire packet, immediately 714a246b010SChuck Lever * reset the count of bytes sent. */ 715262965f5SChuck Lever req->rq_bytes_sent += status; 716d60dbb20STrond Myklebust req->rq_xmit_bytes_sent += status; 717262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 718a246b010SChuck Lever req->rq_bytes_sent = 0; 719a246b010SChuck Lever return 0; 720a246b010SChuck Lever } 721262965f5SChuck Lever 72206b4b681STrond Myklebust if (status != 0) 72306b4b681STrond Myklebust continue; 724a246b010SChuck Lever status = -EAGAIN; 725a246b010SChuck Lever break; 726a246b010SChuck Lever } 727a246b010SChuck Lever 728262965f5SChuck Lever switch (status) { 729fba91afbSTrond Myklebust case -ENOTSOCK: 730fba91afbSTrond Myklebust status = -ENOTCONN; 731fba91afbSTrond Myklebust /* Should we call xs_close() here? */ 732fba91afbSTrond Myklebust break; 733262965f5SChuck Lever case -EAGAIN: 7345e3771ceSTrond Myklebust status = xs_nospace(task); 735262965f5SChuck Lever break; 736262965f5SChuck Lever default: 737262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 738262965f5SChuck Lever -status); 739a246b010SChuck Lever case -ECONNRESET: 74055420c24STrond Myklebust case -EPIPE: 741e06799f9STrond Myklebust xs_tcp_shutdown(xprt); 742a246b010SChuck Lever case -ECONNREFUSED: 743a246b010SChuck Lever case -ENOTCONN: 744a246b010SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); 745a246b010SChuck Lever } 7465fe46e9dSBian Naimeng 747a246b010SChuck Lever return status; 748a246b010SChuck Lever } 749a246b010SChuck Lever 7509903cd1cSChuck Lever /** 751e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 752e0ab53deSTrond Myklebust * @xprt: transport 753e0ab53deSTrond Myklebust * @task: rpc task 754e0ab53deSTrond Myklebust * 755e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 756e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 757e0ab53deSTrond Myklebust * the server. 758e0ab53deSTrond Myklebust */ 759e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 760e0ab53deSTrond Myklebust { 761e0ab53deSTrond Myklebust struct rpc_rqst *req; 762e0ab53deSTrond Myklebust 763e0ab53deSTrond Myklebust if (task != xprt->snd_task) 764e0ab53deSTrond Myklebust return; 765e0ab53deSTrond Myklebust if (task == NULL) 766e0ab53deSTrond Myklebust goto out_release; 767e0ab53deSTrond Myklebust req = task->tk_rqstp; 76843cedbf0STrond Myklebust if (req == NULL) 76943cedbf0STrond Myklebust goto out_release; 770e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 771e0ab53deSTrond Myklebust goto out_release; 772e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 773e0ab53deSTrond Myklebust goto out_release; 774e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 775e0ab53deSTrond Myklebust out_release: 776e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 777e0ab53deSTrond Myklebust } 778e0ab53deSTrond Myklebust 7792a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7802a9e1cfaSTrond Myklebust { 7812a9e1cfaSTrond Myklebust transport->old_data_ready = sk->sk_data_ready; 7822a9e1cfaSTrond Myklebust transport->old_state_change = sk->sk_state_change; 7832a9e1cfaSTrond Myklebust transport->old_write_space = sk->sk_write_space; 7842a9e1cfaSTrond Myklebust transport->old_error_report = sk->sk_error_report; 7852a9e1cfaSTrond Myklebust } 7862a9e1cfaSTrond Myklebust 7872a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 7882a9e1cfaSTrond Myklebust { 7892a9e1cfaSTrond Myklebust sk->sk_data_ready = transport->old_data_ready; 7902a9e1cfaSTrond Myklebust sk->sk_state_change = transport->old_state_change; 7912a9e1cfaSTrond Myklebust sk->sk_write_space = transport->old_write_space; 7922a9e1cfaSTrond Myklebust sk->sk_error_report = transport->old_error_report; 7932a9e1cfaSTrond Myklebust } 7942a9e1cfaSTrond Myklebust 795fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport) 796a246b010SChuck Lever { 797ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 798ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 799a246b010SChuck Lever 800fe315e76SChuck Lever if (sk == NULL) 801fe315e76SChuck Lever return; 8029903cd1cSChuck Lever 803246408dcSTrond Myklebust transport->srcport = 0; 804246408dcSTrond Myklebust 805a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 806ee0ac0c2SChuck Lever transport->inet = NULL; 807ee0ac0c2SChuck Lever transport->sock = NULL; 808a246b010SChuck Lever 809a246b010SChuck Lever sk->sk_user_data = NULL; 8102a9e1cfaSTrond Myklebust 8112a9e1cfaSTrond Myklebust xs_restore_old_callbacks(transport, sk); 812a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 813a246b010SChuck Lever 814a246b010SChuck Lever sk->sk_no_check = 0; 815a246b010SChuck Lever 816a246b010SChuck Lever sock_release(sock); 817fe315e76SChuck Lever } 818fe315e76SChuck Lever 819fe315e76SChuck Lever /** 820fe315e76SChuck Lever * xs_close - close a socket 821fe315e76SChuck Lever * @xprt: transport 822fe315e76SChuck Lever * 823fe315e76SChuck Lever * This is used when all requests are complete; ie, no DRC state remains 824fe315e76SChuck Lever * on the server we want to save. 825f75e6745STrond Myklebust * 826f75e6745STrond Myklebust * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 827f75e6745STrond Myklebust * xs_reset_transport() zeroing the socket from underneath a writer. 828fe315e76SChuck Lever */ 829fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt) 830fe315e76SChuck Lever { 831fe315e76SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 832fe315e76SChuck Lever 833fe315e76SChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 834fe315e76SChuck Lever 835fe315e76SChuck Lever xs_reset_transport(transport); 83661d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 837fe315e76SChuck Lever 838632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 8397d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 840632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 8413b948ae5STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 842632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 84362da3b24STrond Myklebust xprt_disconnect_done(xprt); 844a246b010SChuck Lever } 845a246b010SChuck Lever 846f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt) 847f75e6745STrond Myklebust { 848f75e6745STrond Myklebust if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state)) 849f75e6745STrond Myklebust xs_close(xprt); 850f75e6745STrond Myklebust else 851f75e6745STrond Myklebust xs_tcp_shutdown(xprt); 852f75e6745STrond Myklebust } 853f75e6745STrond Myklebust 8549903cd1cSChuck Lever /** 8559903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 8569903cd1cSChuck Lever * @xprt: doomed transport 8579903cd1cSChuck Lever * 8589903cd1cSChuck Lever */ 8599903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 860a246b010SChuck Lever { 861c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 862c8475461SChuck Lever 8639903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 8649903cd1cSChuck Lever 865afe2c511STejun Heo cancel_delayed_work_sync(&transport->connect_worker); 866a246b010SChuck Lever 8679903cd1cSChuck Lever xs_close(xprt); 868edb267a6SChuck Lever xs_free_peer_addresses(xprt); 869e204e621SPavel Emelyanov xprt_free(xprt); 870bc25571eS\"Talpey, Thomas\ module_put(THIS_MODULE); 871a246b010SChuck Lever } 872a246b010SChuck Lever 8739903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 8749903cd1cSChuck Lever { 8759903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 8769903cd1cSChuck Lever } 8779903cd1cSChuck Lever 878176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) 879176e21eeSChuck Lever { 880176e21eeSChuck Lever struct xdr_skb_reader desc = { 881176e21eeSChuck Lever .skb = skb, 882176e21eeSChuck Lever .offset = sizeof(rpc_fraghdr), 883176e21eeSChuck Lever .count = skb->len - sizeof(rpc_fraghdr), 884176e21eeSChuck Lever }; 885176e21eeSChuck Lever 886176e21eeSChuck Lever if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) 887176e21eeSChuck Lever return -1; 888176e21eeSChuck Lever if (desc.count) 889176e21eeSChuck Lever return -1; 890176e21eeSChuck Lever return 0; 891176e21eeSChuck Lever } 892176e21eeSChuck Lever 893176e21eeSChuck Lever /** 894176e21eeSChuck Lever * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets 895176e21eeSChuck Lever * @sk: socket with data to read 896176e21eeSChuck Lever * @len: how much data to read 897176e21eeSChuck Lever * 898176e21eeSChuck Lever * Currently this assumes we can read the whole reply in a single gulp. 899176e21eeSChuck Lever */ 900176e21eeSChuck Lever static void xs_local_data_ready(struct sock *sk, int len) 901176e21eeSChuck Lever { 902176e21eeSChuck Lever struct rpc_task *task; 903176e21eeSChuck Lever struct rpc_xprt *xprt; 904176e21eeSChuck Lever struct rpc_rqst *rovr; 905176e21eeSChuck Lever struct sk_buff *skb; 906176e21eeSChuck Lever int err, repsize, copied; 907176e21eeSChuck Lever u32 _xid; 908176e21eeSChuck Lever __be32 *xp; 909176e21eeSChuck Lever 910176e21eeSChuck Lever read_lock_bh(&sk->sk_callback_lock); 911176e21eeSChuck Lever dprintk("RPC: %s...\n", __func__); 912176e21eeSChuck Lever xprt = xprt_from_sock(sk); 913176e21eeSChuck Lever if (xprt == NULL) 914176e21eeSChuck Lever goto out; 915176e21eeSChuck Lever 916176e21eeSChuck Lever skb = skb_recv_datagram(sk, 0, 1, &err); 917176e21eeSChuck Lever if (skb == NULL) 918176e21eeSChuck Lever goto out; 919176e21eeSChuck Lever 920176e21eeSChuck Lever if (xprt->shutdown) 921176e21eeSChuck Lever goto dropit; 922176e21eeSChuck Lever 923176e21eeSChuck Lever repsize = skb->len - sizeof(rpc_fraghdr); 924176e21eeSChuck Lever if (repsize < 4) { 925176e21eeSChuck Lever dprintk("RPC: impossible RPC reply size %d\n", repsize); 926176e21eeSChuck Lever goto dropit; 927176e21eeSChuck Lever } 928176e21eeSChuck Lever 929176e21eeSChuck Lever /* Copy the XID from the skb... */ 930176e21eeSChuck Lever xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); 931176e21eeSChuck Lever if (xp == NULL) 932176e21eeSChuck Lever goto dropit; 933176e21eeSChuck Lever 934176e21eeSChuck Lever /* Look up and lock the request corresponding to the given XID */ 935176e21eeSChuck Lever spin_lock(&xprt->transport_lock); 936176e21eeSChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 937176e21eeSChuck Lever if (!rovr) 938176e21eeSChuck Lever goto out_unlock; 939176e21eeSChuck Lever task = rovr->rq_task; 940176e21eeSChuck Lever 941176e21eeSChuck Lever copied = rovr->rq_private_buf.buflen; 942176e21eeSChuck Lever if (copied > repsize) 943176e21eeSChuck Lever copied = repsize; 944176e21eeSChuck Lever 945176e21eeSChuck Lever if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { 946176e21eeSChuck Lever dprintk("RPC: sk_buff copy failed\n"); 947176e21eeSChuck Lever goto out_unlock; 948176e21eeSChuck Lever } 949176e21eeSChuck Lever 950176e21eeSChuck Lever xprt_complete_rqst(task, copied); 951176e21eeSChuck Lever 952176e21eeSChuck Lever out_unlock: 953176e21eeSChuck Lever spin_unlock(&xprt->transport_lock); 954176e21eeSChuck Lever dropit: 955176e21eeSChuck Lever skb_free_datagram(sk, skb); 956176e21eeSChuck Lever out: 957176e21eeSChuck Lever read_unlock_bh(&sk->sk_callback_lock); 958176e21eeSChuck Lever } 959176e21eeSChuck Lever 9609903cd1cSChuck Lever /** 9619903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 9629903cd1cSChuck Lever * @sk: socket with data to read 9639903cd1cSChuck Lever * @len: how much data to read 9649903cd1cSChuck Lever * 965a246b010SChuck Lever */ 9669903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 967a246b010SChuck Lever { 968a246b010SChuck Lever struct rpc_task *task; 969a246b010SChuck Lever struct rpc_xprt *xprt; 970a246b010SChuck Lever struct rpc_rqst *rovr; 971a246b010SChuck Lever struct sk_buff *skb; 972a246b010SChuck Lever int err, repsize, copied; 973d8ed029dSAlexey Dobriyan u32 _xid; 974d8ed029dSAlexey Dobriyan __be32 *xp; 975a246b010SChuck Lever 976f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 9779903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 9789903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 979a246b010SChuck Lever goto out; 980a246b010SChuck Lever 981a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 982a246b010SChuck Lever goto out; 983a246b010SChuck Lever 984a246b010SChuck Lever if (xprt->shutdown) 985a246b010SChuck Lever goto dropit; 986a246b010SChuck Lever 987a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 988a246b010SChuck Lever if (repsize < 4) { 9899903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 990a246b010SChuck Lever goto dropit; 991a246b010SChuck Lever } 992a246b010SChuck Lever 993a246b010SChuck Lever /* Copy the XID from the skb... */ 994a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 995a246b010SChuck Lever sizeof(_xid), &_xid); 996a246b010SChuck Lever if (xp == NULL) 997a246b010SChuck Lever goto dropit; 998a246b010SChuck Lever 999a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 10004a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 1001a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 1002a246b010SChuck Lever if (!rovr) 1003a246b010SChuck Lever goto out_unlock; 1004a246b010SChuck Lever task = rovr->rq_task; 1005a246b010SChuck Lever 1006a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 1007a246b010SChuck Lever copied = repsize; 1008a246b010SChuck Lever 1009a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 10101781f7f5SHerbert Xu if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 10111781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); 1012a246b010SChuck Lever goto out_unlock; 10131781f7f5SHerbert Xu } 10141781f7f5SHerbert Xu 10151781f7f5SHerbert Xu UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); 1016a246b010SChuck Lever 10171570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 10181570c1e4SChuck Lever xprt_complete_rqst(task, copied); 1019a246b010SChuck Lever 1020a246b010SChuck Lever out_unlock: 10214a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 1022a246b010SChuck Lever dropit: 1023a246b010SChuck Lever skb_free_datagram(sk, skb); 1024a246b010SChuck Lever out: 1025f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1026a246b010SChuck Lever } 1027a246b010SChuck Lever 1028*a519fc7aSTrond Myklebust /* 1029*a519fc7aSTrond Myklebust * Helper function to force a TCP close if the server is sending 1030*a519fc7aSTrond Myklebust * junk and/or it has put us in CLOSE_WAIT 1031*a519fc7aSTrond Myklebust */ 1032*a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt) 1033*a519fc7aSTrond Myklebust { 1034*a519fc7aSTrond Myklebust set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 1035*a519fc7aSTrond Myklebust xprt_force_disconnect(xprt); 1036*a519fc7aSTrond Myklebust } 1037*a519fc7aSTrond Myklebust 1038dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 1039a246b010SChuck Lever { 104051971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1041a246b010SChuck Lever size_t len, used; 1042a246b010SChuck Lever char *p; 1043a246b010SChuck Lever 104451971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 104551971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 10469d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 104751971139SChuck Lever transport->tcp_offset += used; 1048a246b010SChuck Lever if (used != len) 1049a246b010SChuck Lever return; 1050808012fbSChuck Lever 105151971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 105251971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 1053e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 1054a246b010SChuck Lever else 1055e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 105651971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 1057808012fbSChuck Lever 1058e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 105951971139SChuck Lever transport->tcp_offset = 0; 1060808012fbSChuck Lever 1061a246b010SChuck Lever /* Sanity check of the record length */ 106218dca02aSRicardo Labiaga if (unlikely(transport->tcp_reclen < 8)) { 10639903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 1064*a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 10659903cd1cSChuck Lever return; 1066a246b010SChuck Lever } 1067a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 106851971139SChuck Lever transport->tcp_reclen); 1069a246b010SChuck Lever } 1070a246b010SChuck Lever 107151971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 1072a246b010SChuck Lever { 107351971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 1074e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 107551971139SChuck Lever transport->tcp_offset = 0; 1076e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 1077e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1078e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 107951971139SChuck Lever transport->tcp_copied = 0; 1080a246b010SChuck Lever } 1081a246b010SChuck Lever } 1082a246b010SChuck Lever } 1083a246b010SChuck Lever 1084dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1085a246b010SChuck Lever { 1086a246b010SChuck Lever size_t len, used; 1087a246b010SChuck Lever char *p; 1088a246b010SChuck Lever 108951971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 1090a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 109151971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 10929d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 109351971139SChuck Lever transport->tcp_offset += used; 1094a246b010SChuck Lever if (used != len) 1095a246b010SChuck Lever return; 1096e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 1097f4a2e418SRicardo Labiaga transport->tcp_flags |= TCP_RCV_READ_CALLDIR; 109851971139SChuck Lever transport->tcp_copied = 4; 109918dca02aSRicardo Labiaga dprintk("RPC: reading %s XID %08x\n", 110018dca02aSRicardo Labiaga (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" 110118dca02aSRicardo Labiaga : "request with", 110251971139SChuck Lever ntohl(transport->tcp_xid)); 110351971139SChuck Lever xs_tcp_check_fraghdr(transport); 1104a246b010SChuck Lever } 1105a246b010SChuck Lever 110618dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport, 110718dca02aSRicardo Labiaga struct xdr_skb_reader *desc) 1108a246b010SChuck Lever { 110918dca02aSRicardo Labiaga size_t len, used; 111018dca02aSRicardo Labiaga u32 offset; 1111b76ce561STrond Myklebust char *p; 111218dca02aSRicardo Labiaga 111318dca02aSRicardo Labiaga /* 111418dca02aSRicardo Labiaga * We want transport->tcp_offset to be 8 at the end of this routine 111518dca02aSRicardo Labiaga * (4 bytes for the xid and 4 bytes for the call/reply flag). 111618dca02aSRicardo Labiaga * When this function is called for the first time, 111718dca02aSRicardo Labiaga * transport->tcp_offset is 4 (after having already read the xid). 111818dca02aSRicardo Labiaga */ 111918dca02aSRicardo Labiaga offset = transport->tcp_offset - sizeof(transport->tcp_xid); 1120b76ce561STrond Myklebust len = sizeof(transport->tcp_calldir) - offset; 112118dca02aSRicardo Labiaga dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); 1122b76ce561STrond Myklebust p = ((char *) &transport->tcp_calldir) + offset; 1123b76ce561STrond Myklebust used = xdr_skb_read_bits(desc, p, len); 112418dca02aSRicardo Labiaga transport->tcp_offset += used; 112518dca02aSRicardo Labiaga if (used != len) 112618dca02aSRicardo Labiaga return; 1127f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; 1128f4a2e418SRicardo Labiaga /* 1129f4a2e418SRicardo Labiaga * We don't yet have the XDR buffer, so we will write the calldir 1130f4a2e418SRicardo Labiaga * out after we get the buffer from the 'struct rpc_rqst' 1131f4a2e418SRicardo Labiaga */ 1132b76ce561STrond Myklebust switch (ntohl(transport->tcp_calldir)) { 1133b76ce561STrond Myklebust case RPC_REPLY: 1134b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1135b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 113618dca02aSRicardo Labiaga transport->tcp_flags |= TCP_RPC_REPLY; 1137b76ce561STrond Myklebust break; 1138b76ce561STrond Myklebust case RPC_CALL: 1139b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; 1140b76ce561STrond Myklebust transport->tcp_flags |= TCP_RCV_COPY_DATA; 114118dca02aSRicardo Labiaga transport->tcp_flags &= ~TCP_RPC_REPLY; 1142b76ce561STrond Myklebust break; 1143b76ce561STrond Myklebust default: 1144b76ce561STrond Myklebust dprintk("RPC: invalid request message type\n"); 1145*a519fc7aSTrond Myklebust xs_tcp_force_close(&transport->xprt); 1146b76ce561STrond Myklebust } 114718dca02aSRicardo Labiaga xs_tcp_check_fraghdr(transport); 114818dca02aSRicardo Labiaga } 114918dca02aSRicardo Labiaga 115044b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt, 115144b98efdSRicardo Labiaga struct xdr_skb_reader *desc, 115244b98efdSRicardo Labiaga struct rpc_rqst *req) 1153a246b010SChuck Lever { 115444b98efdSRicardo Labiaga struct sock_xprt *transport = 115544b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 1156a246b010SChuck Lever struct xdr_buf *rcvbuf; 1157a246b010SChuck Lever size_t len; 1158a246b010SChuck Lever ssize_t r; 1159a246b010SChuck Lever 1160a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 1161f4a2e418SRicardo Labiaga 1162f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { 1163f4a2e418SRicardo Labiaga /* 1164f4a2e418SRicardo Labiaga * Save the RPC direction in the XDR buffer 1165f4a2e418SRicardo Labiaga */ 1166f4a2e418SRicardo Labiaga memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, 1167b76ce561STrond Myklebust &transport->tcp_calldir, 1168b76ce561STrond Myklebust sizeof(transport->tcp_calldir)); 1169b76ce561STrond Myklebust transport->tcp_copied += sizeof(transport->tcp_calldir); 1170f4a2e418SRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; 1171a246b010SChuck Lever } 1172a246b010SChuck Lever 1173a246b010SChuck Lever len = desc->count; 117451971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 1175dd456471SChuck Lever struct xdr_skb_reader my_desc; 1176a246b010SChuck Lever 117751971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1178a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 1179a246b010SChuck Lever my_desc.count = len; 118051971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 11819d292316SChuck Lever &my_desc, xdr_skb_read_bits); 1182a246b010SChuck Lever desc->count -= r; 1183a246b010SChuck Lever desc->offset += r; 1184a246b010SChuck Lever } else 118551971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 11869d292316SChuck Lever desc, xdr_skb_read_bits); 1187a246b010SChuck Lever 1188a246b010SChuck Lever if (r > 0) { 118951971139SChuck Lever transport->tcp_copied += r; 119051971139SChuck Lever transport->tcp_offset += r; 1191a246b010SChuck Lever } 1192a246b010SChuck Lever if (r != len) { 1193a246b010SChuck Lever /* Error when copying to the receive buffer, 1194a246b010SChuck Lever * usually because we weren't able to allocate 1195a246b010SChuck Lever * additional buffer pages. All we can do now 1196e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 1197a246b010SChuck Lever * will not receive any additional updates, 1198a246b010SChuck Lever * and time out. 1199a246b010SChuck Lever * Any remaining data from this record will 1200a246b010SChuck Lever * be discarded. 1201a246b010SChuck Lever */ 1202e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1203a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 120451971139SChuck Lever ntohl(transport->tcp_xid)); 120546121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 120646121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 120746121cf7SChuck Lever xprt, transport->tcp_copied, 120846121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 120944b98efdSRicardo Labiaga return; 1210a246b010SChuck Lever } 1211a246b010SChuck Lever 1212a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 121351971139SChuck Lever ntohl(transport->tcp_xid), r); 121446121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 121546121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 121646121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 1217a246b010SChuck Lever 121851971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 1219e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 122051971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 1221e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 1222e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 1223a246b010SChuck Lever } 122444b98efdSRicardo Labiaga } 122544b98efdSRicardo Labiaga 122644b98efdSRicardo Labiaga /* 122744b98efdSRicardo Labiaga * Finds the request corresponding to the RPC xid and invokes the common 122844b98efdSRicardo Labiaga * tcp read code to read the data. 122944b98efdSRicardo Labiaga */ 123044b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, 123144b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 123244b98efdSRicardo Labiaga { 123344b98efdSRicardo Labiaga struct sock_xprt *transport = 123444b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 123544b98efdSRicardo Labiaga struct rpc_rqst *req; 123644b98efdSRicardo Labiaga 123744b98efdSRicardo Labiaga dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); 123844b98efdSRicardo Labiaga 123944b98efdSRicardo Labiaga /* Find and lock the request corresponding to this xid */ 124044b98efdSRicardo Labiaga spin_lock(&xprt->transport_lock); 124144b98efdSRicardo Labiaga req = xprt_lookup_rqst(xprt, transport->tcp_xid); 124244b98efdSRicardo Labiaga if (!req) { 124344b98efdSRicardo Labiaga dprintk("RPC: XID %08x request not found!\n", 124444b98efdSRicardo Labiaga ntohl(transport->tcp_xid)); 124544b98efdSRicardo Labiaga spin_unlock(&xprt->transport_lock); 124644b98efdSRicardo Labiaga return -1; 124744b98efdSRicardo Labiaga } 124844b98efdSRicardo Labiaga 124944b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 125044b98efdSRicardo Labiaga 1251e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 125251971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 125344b98efdSRicardo Labiaga 12544a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 125544b98efdSRicardo Labiaga return 0; 125644b98efdSRicardo Labiaga } 125744b98efdSRicardo Labiaga 12589e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 125944b98efdSRicardo Labiaga /* 126044b98efdSRicardo Labiaga * Obtains an rpc_rqst previously allocated and invokes the common 126144b98efdSRicardo Labiaga * tcp read code to read the data. The result is placed in the callback 126244b98efdSRicardo Labiaga * queue. 126344b98efdSRicardo Labiaga * If we're unable to obtain the rpc_rqst we schedule the closing of the 126444b98efdSRicardo Labiaga * connection and return -1. 126544b98efdSRicardo Labiaga */ 126644b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt, 126744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 126844b98efdSRicardo Labiaga { 126944b98efdSRicardo Labiaga struct sock_xprt *transport = 127044b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 127144b98efdSRicardo Labiaga struct rpc_rqst *req; 127244b98efdSRicardo Labiaga 127344b98efdSRicardo Labiaga req = xprt_alloc_bc_request(xprt); 127444b98efdSRicardo Labiaga if (req == NULL) { 127544b98efdSRicardo Labiaga printk(KERN_WARNING "Callback slot table overflowed\n"); 127644b98efdSRicardo Labiaga xprt_force_disconnect(xprt); 127744b98efdSRicardo Labiaga return -1; 127844b98efdSRicardo Labiaga } 127944b98efdSRicardo Labiaga 128044b98efdSRicardo Labiaga req->rq_xid = transport->tcp_xid; 128144b98efdSRicardo Labiaga dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); 128244b98efdSRicardo Labiaga xs_tcp_read_common(xprt, desc, req); 128344b98efdSRicardo Labiaga 128444b98efdSRicardo Labiaga if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) { 128544b98efdSRicardo Labiaga struct svc_serv *bc_serv = xprt->bc_serv; 128644b98efdSRicardo Labiaga 128744b98efdSRicardo Labiaga /* 128844b98efdSRicardo Labiaga * Add callback request to callback list. The callback 128944b98efdSRicardo Labiaga * service sleeps on the sv_cb_waitq waiting for new 129044b98efdSRicardo Labiaga * requests. Wake it up after adding enqueing the 129144b98efdSRicardo Labiaga * request. 129244b98efdSRicardo Labiaga */ 129344b98efdSRicardo Labiaga dprintk("RPC: add callback request to list\n"); 129444b98efdSRicardo Labiaga spin_lock(&bc_serv->sv_cb_lock); 129544b98efdSRicardo Labiaga list_add(&req->rq_bc_list, &bc_serv->sv_cb_list); 129644b98efdSRicardo Labiaga spin_unlock(&bc_serv->sv_cb_lock); 129744b98efdSRicardo Labiaga wake_up(&bc_serv->sv_cb_waitq); 129844b98efdSRicardo Labiaga } 129944b98efdSRicardo Labiaga 130044b98efdSRicardo Labiaga req->rq_private_buf.len = transport->tcp_copied; 130144b98efdSRicardo Labiaga 130244b98efdSRicardo Labiaga return 0; 130344b98efdSRicardo Labiaga } 130444b98efdSRicardo Labiaga 130544b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 130644b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 130744b98efdSRicardo Labiaga { 130844b98efdSRicardo Labiaga struct sock_xprt *transport = 130944b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 131044b98efdSRicardo Labiaga 131144b98efdSRicardo Labiaga return (transport->tcp_flags & TCP_RPC_REPLY) ? 131244b98efdSRicardo Labiaga xs_tcp_read_reply(xprt, desc) : 131344b98efdSRicardo Labiaga xs_tcp_read_callback(xprt, desc); 131444b98efdSRicardo Labiaga } 131544b98efdSRicardo Labiaga #else 131644b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, 131744b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 131844b98efdSRicardo Labiaga { 131944b98efdSRicardo Labiaga return xs_tcp_read_reply(xprt, desc); 132044b98efdSRicardo Labiaga } 13219e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 132244b98efdSRicardo Labiaga 132344b98efdSRicardo Labiaga /* 132444b98efdSRicardo Labiaga * Read data off the transport. This can be either an RPC_CALL or an 132544b98efdSRicardo Labiaga * RPC_REPLY. Relay the processing to helper functions. 132644b98efdSRicardo Labiaga */ 132744b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt, 132844b98efdSRicardo Labiaga struct xdr_skb_reader *desc) 132944b98efdSRicardo Labiaga { 133044b98efdSRicardo Labiaga struct sock_xprt *transport = 133144b98efdSRicardo Labiaga container_of(xprt, struct sock_xprt, xprt); 133244b98efdSRicardo Labiaga 133344b98efdSRicardo Labiaga if (_xs_tcp_read_data(xprt, desc) == 0) 133451971139SChuck Lever xs_tcp_check_fraghdr(transport); 133544b98efdSRicardo Labiaga else { 133644b98efdSRicardo Labiaga /* 133744b98efdSRicardo Labiaga * The transport_lock protects the request handling. 133844b98efdSRicardo Labiaga * There's no need to hold it to update the tcp_flags. 133944b98efdSRicardo Labiaga */ 134044b98efdSRicardo Labiaga transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 134144b98efdSRicardo Labiaga } 1342a246b010SChuck Lever } 1343a246b010SChuck Lever 1344dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 1345a246b010SChuck Lever { 1346a246b010SChuck Lever size_t len; 1347a246b010SChuck Lever 134851971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 1349a246b010SChuck Lever if (len > desc->count) 1350a246b010SChuck Lever len = desc->count; 1351a246b010SChuck Lever desc->count -= len; 1352a246b010SChuck Lever desc->offset += len; 135351971139SChuck Lever transport->tcp_offset += len; 1354a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 135551971139SChuck Lever xs_tcp_check_fraghdr(transport); 1356a246b010SChuck Lever } 1357a246b010SChuck Lever 13589903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 1359a246b010SChuck Lever { 1360a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 136151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1362dd456471SChuck Lever struct xdr_skb_reader desc = { 1363a246b010SChuck Lever .skb = skb, 1364a246b010SChuck Lever .offset = offset, 1365a246b010SChuck Lever .count = len, 1366a246b010SChuck Lever }; 1367a246b010SChuck Lever 13689903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 1369a246b010SChuck Lever do { 1370a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 1371a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 1372e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 13739903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 1374a246b010SChuck Lever continue; 1375a246b010SChuck Lever } 1376a246b010SChuck Lever /* Read in the xid if necessary */ 1377e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 137851971139SChuck Lever xs_tcp_read_xid(transport, &desc); 1379a246b010SChuck Lever continue; 1380a246b010SChuck Lever } 138118dca02aSRicardo Labiaga /* Read in the call/reply flag */ 1382f4a2e418SRicardo Labiaga if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { 138318dca02aSRicardo Labiaga xs_tcp_read_calldir(transport, &desc); 138418dca02aSRicardo Labiaga continue; 138518dca02aSRicardo Labiaga } 1386a246b010SChuck Lever /* Read in the request data */ 1387e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 138844b98efdSRicardo Labiaga xs_tcp_read_data(xprt, &desc); 1389a246b010SChuck Lever continue; 1390a246b010SChuck Lever } 1391a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 139251971139SChuck Lever xs_tcp_read_discard(transport, &desc); 1393a246b010SChuck Lever } while (desc.count); 13949903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 1395a246b010SChuck Lever return len - desc.count; 1396a246b010SChuck Lever } 1397a246b010SChuck Lever 13989903cd1cSChuck Lever /** 13999903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 14009903cd1cSChuck Lever * @sk: socket with data to read 14019903cd1cSChuck Lever * @bytes: how much data to read 14029903cd1cSChuck Lever * 14039903cd1cSChuck Lever */ 14049903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 1405a246b010SChuck Lever { 1406a246b010SChuck Lever struct rpc_xprt *xprt; 1407a246b010SChuck Lever read_descriptor_t rd_desc; 1408ff2d7db8STrond Myklebust int read; 1409a246b010SChuck Lever 14109903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 141146121cf7SChuck Lever 1412f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 14139903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 1414a246b010SChuck Lever goto out; 1415a246b010SChuck Lever if (xprt->shutdown) 1416a246b010SChuck Lever goto out; 1417a246b010SChuck Lever 141861d0a8e6SNeil Brown /* Any data means we had a useful conversation, so 141961d0a8e6SNeil Brown * the we don't need to delay the next reconnect 142061d0a8e6SNeil Brown */ 142161d0a8e6SNeil Brown if (xprt->reestablish_timeout) 142261d0a8e6SNeil Brown xprt->reestablish_timeout = 0; 142361d0a8e6SNeil Brown 14249903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 1425a246b010SChuck Lever rd_desc.arg.data = xprt; 1426ff2d7db8STrond Myklebust do { 1427a246b010SChuck Lever rd_desc.count = 65536; 1428ff2d7db8STrond Myklebust read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 1429ff2d7db8STrond Myklebust } while (read > 0); 1430a246b010SChuck Lever out: 1431f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1432a246b010SChuck Lever } 1433a246b010SChuck Lever 14347d1e8255STrond Myklebust /* 14357d1e8255STrond Myklebust * Do the equivalent of linger/linger2 handling for dealing with 14367d1e8255STrond Myklebust * broken servers that don't close the socket in a timely 14377d1e8255STrond Myklebust * fashion 14387d1e8255STrond Myklebust */ 14397d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, 14407d1e8255STrond Myklebust unsigned long timeout) 14417d1e8255STrond Myklebust { 14427d1e8255STrond Myklebust struct sock_xprt *transport; 14437d1e8255STrond Myklebust 14447d1e8255STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 14457d1e8255STrond Myklebust return; 14467d1e8255STrond Myklebust set_bit(XPRT_CONNECTION_ABORT, &xprt->state); 14477d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 14487d1e8255STrond Myklebust queue_delayed_work(rpciod_workqueue, &transport->connect_worker, 14497d1e8255STrond Myklebust timeout); 14507d1e8255STrond Myklebust } 14517d1e8255STrond Myklebust 14527d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) 14537d1e8255STrond Myklebust { 14547d1e8255STrond Myklebust struct sock_xprt *transport; 14557d1e8255STrond Myklebust 14567d1e8255STrond Myklebust transport = container_of(xprt, struct sock_xprt, xprt); 14577d1e8255STrond Myklebust 14587d1e8255STrond Myklebust if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || 14597d1e8255STrond Myklebust !cancel_delayed_work(&transport->connect_worker)) 14607d1e8255STrond Myklebust return; 14617d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 14627d1e8255STrond Myklebust xprt_clear_connecting(xprt); 14637d1e8255STrond Myklebust } 14647d1e8255STrond Myklebust 14657d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt) 14667d1e8255STrond Myklebust { 14677d1e8255STrond Myklebust smp_mb__before_clear_bit(); 1468*a519fc7aSTrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 1469*a519fc7aSTrond Myklebust clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state); 14707d1e8255STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 14717d1e8255STrond Myklebust clear_bit(XPRT_CLOSING, &xprt->state); 14727d1e8255STrond Myklebust smp_mb__after_clear_bit(); 14737d1e8255STrond Myklebust /* Mark transport as closed and wake up all pending tasks */ 14747d1e8255STrond Myklebust xprt_disconnect_done(xprt); 14757d1e8255STrond Myklebust } 14767d1e8255STrond Myklebust 14779903cd1cSChuck Lever /** 14789903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 14799903cd1cSChuck Lever * @sk: socket whose state has changed 14809903cd1cSChuck Lever * 14819903cd1cSChuck Lever */ 14829903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 1483a246b010SChuck Lever { 1484a246b010SChuck Lever struct rpc_xprt *xprt; 1485a246b010SChuck Lever 1486f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1487a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 1488a246b010SChuck Lever goto out; 14899903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1490669502ffSAndy Chittenden dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1491a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 1492a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 1493669502ffSAndy Chittenden sock_flag(sk, SOCK_ZAPPED), 1494669502ffSAndy Chittenden sk->sk_shutdown); 1495a246b010SChuck Lever 1496a246b010SChuck Lever switch (sk->sk_state) { 1497a246b010SChuck Lever case TCP_ESTABLISHED: 1498f064af1eSEric Dumazet spin_lock(&xprt->transport_lock); 1499a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 150051971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 150151971139SChuck Lever struct sock_xprt, xprt); 150251971139SChuck Lever 1503a246b010SChuck Lever /* Reset TCP record info */ 150451971139SChuck Lever transport->tcp_offset = 0; 150551971139SChuck Lever transport->tcp_reclen = 0; 150651971139SChuck Lever transport->tcp_copied = 0; 1507e136d092SChuck Lever transport->tcp_flags = 1508e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 150951971139SChuck Lever 15102a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 1511a246b010SChuck Lever } 1512f064af1eSEric Dumazet spin_unlock(&xprt->transport_lock); 1513a246b010SChuck Lever break; 15143b948ae5STrond Myklebust case TCP_FIN_WAIT1: 15153b948ae5STrond Myklebust /* The client initiated a shutdown of the socket */ 15167c1d71cfSTrond Myklebust xprt->connect_cookie++; 1517663b8858STrond Myklebust xprt->reestablish_timeout = 0; 15183b948ae5STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 15193b948ae5STrond Myklebust smp_mb__before_clear_bit(); 15203b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 1521ef803670STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 15223b948ae5STrond Myklebust smp_mb__after_clear_bit(); 152325fe6142STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); 1524a246b010SChuck Lever break; 1525632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 15263b948ae5STrond Myklebust /* The server initiated a shutdown of the socket */ 15277c1d71cfSTrond Myklebust xprt->connect_cookie++; 1528*a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 1529663b8858STrond Myklebust case TCP_CLOSING: 1530663b8858STrond Myklebust /* 1531663b8858STrond Myklebust * If the server closed down the connection, make sure that 1532663b8858STrond Myklebust * we back off before reconnecting 1533663b8858STrond Myklebust */ 1534663b8858STrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1535663b8858STrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 15363b948ae5STrond Myklebust break; 15373b948ae5STrond Myklebust case TCP_LAST_ACK: 1538670f9457STrond Myklebust set_bit(XPRT_CLOSING, &xprt->state); 153925fe6142STrond Myklebust xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); 15403b948ae5STrond Myklebust smp_mb__before_clear_bit(); 15413b948ae5STrond Myklebust clear_bit(XPRT_CONNECTED, &xprt->state); 15423b948ae5STrond Myklebust smp_mb__after_clear_bit(); 15433b948ae5STrond Myklebust break; 15443b948ae5STrond Myklebust case TCP_CLOSE: 15457d1e8255STrond Myklebust xs_tcp_cancel_linger_timeout(xprt); 15467d1e8255STrond Myklebust xs_sock_mark_closed(xprt); 1547a246b010SChuck Lever } 1548a246b010SChuck Lever out: 1549f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1550a246b010SChuck Lever } 1551a246b010SChuck Lever 15529903cd1cSChuck Lever /** 1553482f32e6STrond Myklebust * xs_error_report - callback mainly for catching socket errors 15542a9e1cfaSTrond Myklebust * @sk: socket 15552a9e1cfaSTrond Myklebust */ 1556482f32e6STrond Myklebust static void xs_error_report(struct sock *sk) 15572a9e1cfaSTrond Myklebust { 15582a9e1cfaSTrond Myklebust struct rpc_xprt *xprt; 15592a9e1cfaSTrond Myklebust 1560f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 15612a9e1cfaSTrond Myklebust if (!(xprt = xprt_from_sock(sk))) 15622a9e1cfaSTrond Myklebust goto out; 15632a9e1cfaSTrond Myklebust dprintk("RPC: %s client %p...\n" 15642a9e1cfaSTrond Myklebust "RPC: error %d\n", 15652a9e1cfaSTrond Myklebust __func__, xprt, sk->sk_err); 1566482f32e6STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 15672a9e1cfaSTrond Myklebust out: 1568f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 15692a9e1cfaSTrond Myklebust } 15702a9e1cfaSTrond Myklebust 15711f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk) 15721f0fa154SIlpo Järvinen { 15731f0fa154SIlpo Järvinen struct socket *sock; 15741f0fa154SIlpo Järvinen struct rpc_xprt *xprt; 15751f0fa154SIlpo Järvinen 15761f0fa154SIlpo Järvinen if (unlikely(!(sock = sk->sk_socket))) 15771f0fa154SIlpo Järvinen return; 15781f0fa154SIlpo Järvinen clear_bit(SOCK_NOSPACE, &sock->flags); 15791f0fa154SIlpo Järvinen 15801f0fa154SIlpo Järvinen if (unlikely(!(xprt = xprt_from_sock(sk)))) 15811f0fa154SIlpo Järvinen return; 15821f0fa154SIlpo Järvinen if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) 15831f0fa154SIlpo Järvinen return; 15841f0fa154SIlpo Järvinen 15851f0fa154SIlpo Järvinen xprt_write_space(xprt); 15861f0fa154SIlpo Järvinen } 15871f0fa154SIlpo Järvinen 15882a9e1cfaSTrond Myklebust /** 1589c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1590c7b2cae8SChuck Lever * becomes available 15919903cd1cSChuck Lever * @sk: socket whose state has changed 15929903cd1cSChuck Lever * 1593a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1594a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1595c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1596a246b010SChuck Lever * with a bunch of small requests. 1597a246b010SChuck Lever */ 1598c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1599a246b010SChuck Lever { 1600f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1601c7b2cae8SChuck Lever 1602c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 16031f0fa154SIlpo Järvinen if (sock_writeable(sk)) 16041f0fa154SIlpo Järvinen xs_write_space(sk); 1605c7b2cae8SChuck Lever 1606f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1607c7b2cae8SChuck Lever } 1608c7b2cae8SChuck Lever 1609c7b2cae8SChuck Lever /** 1610c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1611c7b2cae8SChuck Lever * becomes available 1612c7b2cae8SChuck Lever * @sk: socket whose state has changed 1613c7b2cae8SChuck Lever * 1614c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1615c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1616c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1617c7b2cae8SChuck Lever * with a bunch of small requests. 1618c7b2cae8SChuck Lever */ 1619c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1620c7b2cae8SChuck Lever { 1621f064af1eSEric Dumazet read_lock_bh(&sk->sk_callback_lock); 1622c7b2cae8SChuck Lever 1623c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 16241f0fa154SIlpo Järvinen if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 16251f0fa154SIlpo Järvinen xs_write_space(sk); 1626c7b2cae8SChuck Lever 1627f064af1eSEric Dumazet read_unlock_bh(&sk->sk_callback_lock); 1628a246b010SChuck Lever } 1629a246b010SChuck Lever 1630470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1631a246b010SChuck Lever { 1632ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1633ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1634a246b010SChuck Lever 16357c6e066eSChuck Lever if (transport->rcvsize) { 1636a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 16377c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1638a246b010SChuck Lever } 16397c6e066eSChuck Lever if (transport->sndsize) { 1640a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 16417c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1642a246b010SChuck Lever sk->sk_write_space(sk); 1643a246b010SChuck Lever } 1644a246b010SChuck Lever } 1645a246b010SChuck Lever 164643118c29SChuck Lever /** 1647470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 164843118c29SChuck Lever * @xprt: generic transport 1649470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1650470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 165143118c29SChuck Lever * 1652470056c2SChuck Lever * Set socket send and receive buffer size limits. 165343118c29SChuck Lever */ 1654470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 165543118c29SChuck Lever { 16567c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 16577c6e066eSChuck Lever 16587c6e066eSChuck Lever transport->sndsize = 0; 1659470056c2SChuck Lever if (sndsize) 16607c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 16617c6e066eSChuck Lever transport->rcvsize = 0; 1662470056c2SChuck Lever if (rcvsize) 16637c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1664470056c2SChuck Lever 1665470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 166643118c29SChuck Lever } 166743118c29SChuck Lever 166846c0ee8bSChuck Lever /** 166946c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 167046c0ee8bSChuck Lever * @task: task that timed out 167146c0ee8bSChuck Lever * 167246c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 167346c0ee8bSChuck Lever */ 167446c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 167546c0ee8bSChuck Lever { 167646c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 167746c0ee8bSChuck Lever } 167846c0ee8bSChuck Lever 1679b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1680b85d8806SChuck Lever { 1681b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1682b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1683b85d8806SChuck Lever return rand + xprt_min_resvport; 1684b85d8806SChuck Lever } 1685b85d8806SChuck Lever 168692200412SChuck Lever /** 168792200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 168892200412SChuck Lever * @xprt: generic transport 168992200412SChuck Lever * @port: new port number 169092200412SChuck Lever * 169192200412SChuck Lever */ 169292200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 169392200412SChuck Lever { 169492200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1695c4efcb1dSChuck Lever 16969dc3b095SChuck Lever rpc_set_port(xs_addr(xprt), port); 16979dc3b095SChuck Lever xs_update_peer_port(xprt); 169892200412SChuck Lever } 169992200412SChuck Lever 17005d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport) 170167a391d7STrond Myklebust { 1702fbfffbd5SChuck Lever unsigned short port = transport->srcport; 170367a391d7STrond Myklebust 170467a391d7STrond Myklebust if (port == 0 && transport->xprt.resvport) 170567a391d7STrond Myklebust port = xs_get_random_port(); 170667a391d7STrond Myklebust return port; 170767a391d7STrond Myklebust } 170867a391d7STrond Myklebust 1709baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 171067a391d7STrond Myklebust { 1711fbfffbd5SChuck Lever if (transport->srcport != 0) 1712fbfffbd5SChuck Lever transport->srcport = 0; 171367a391d7STrond Myklebust if (!transport->xprt.resvport) 171467a391d7STrond Myklebust return 0; 171567a391d7STrond Myklebust if (port <= xprt_min_resvport || port > xprt_max_resvport) 171667a391d7STrond Myklebust return xprt_max_resvport; 171767a391d7STrond Myklebust return --port; 171867a391d7STrond Myklebust } 1719beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1720a246b010SChuck Lever { 1721beb59b68SPavel Emelyanov struct sockaddr_storage myaddr; 172267a391d7STrond Myklebust int err, nloop = 0; 17235d4ec932SPavel Emelyanov unsigned short port = xs_get_srcport(transport); 172467a391d7STrond Myklebust unsigned short last; 1725a246b010SChuck Lever 1726beb59b68SPavel Emelyanov memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1727a246b010SChuck Lever do { 1728beb59b68SPavel Emelyanov rpc_set_port((struct sockaddr *)&myaddr, port); 1729e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1730beb59b68SPavel Emelyanov transport->xprt.addrlen); 173167a391d7STrond Myklebust if (port == 0) 1732d3bc9a1dSFrank van Maarseveen break; 1733a246b010SChuck Lever if (err == 0) { 1734fbfffbd5SChuck Lever transport->srcport = port; 1735d3bc9a1dSFrank van Maarseveen break; 1736a246b010SChuck Lever } 173767a391d7STrond Myklebust last = port; 1738baaf4e48SPavel Emelyanov port = xs_next_srcport(transport, port); 173967a391d7STrond Myklebust if (port > last) 174067a391d7STrond Myklebust nloop++; 174167a391d7STrond Myklebust } while (err == -EADDRINUSE && nloop != 2); 1742beb59b68SPavel Emelyanov 17434232e863SChuck Lever if (myaddr.ss_family == AF_INET) 1744beb59b68SPavel Emelyanov dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1745beb59b68SPavel Emelyanov &((struct sockaddr_in *)&myaddr)->sin_addr, 1746beb59b68SPavel Emelyanov port, err ? "failed" : "ok", err); 1747beb59b68SPavel Emelyanov else 1748beb59b68SPavel Emelyanov dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1749beb59b68SPavel Emelyanov &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 17507dc753f0SChuck Lever port, err ? "failed" : "ok", err); 1751a246b010SChuck Lever return err; 1752a246b010SChuck Lever } 1753a246b010SChuck Lever 1754176e21eeSChuck Lever /* 1755176e21eeSChuck Lever * We don't support autobind on AF_LOCAL sockets 1756176e21eeSChuck Lever */ 1757176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task) 1758176e21eeSChuck Lever { 1759176e21eeSChuck Lever xprt_set_bound(task->tk_xprt); 1760176e21eeSChuck Lever } 1761176e21eeSChuck Lever 1762176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1763176e21eeSChuck Lever { 1764176e21eeSChuck Lever } 1765a246b010SChuck Lever 1766ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1767ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1768ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1769ed07536eSPeter Zijlstra 1770176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1771176e21eeSChuck Lever { 1772176e21eeSChuck Lever struct sock *sk = sock->sk; 1773176e21eeSChuck Lever 1774176e21eeSChuck Lever BUG_ON(sock_owned_by_user(sk)); 1775176e21eeSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1776176e21eeSChuck Lever &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1777176e21eeSChuck Lever } 1778176e21eeSChuck Lever 17798945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 1780ed07536eSPeter Zijlstra { 1781ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 17828945ee5eSChuck Lever 178302b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 17848945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 17858945ee5eSChuck Lever &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1786ed07536eSPeter Zijlstra } 17878945ee5eSChuck Lever 17888945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 17898945ee5eSChuck Lever { 17908945ee5eSChuck Lever struct sock *sk = sock->sk; 17918945ee5eSChuck Lever 1792f4921affSLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 17938945ee5eSChuck Lever sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 17948945ee5eSChuck Lever &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1795ed07536eSPeter Zijlstra } 17966bc9638aSPavel Emelyanov 17976bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 17986bc9638aSPavel Emelyanov { 17994232e863SChuck Lever switch (family) { 1800176e21eeSChuck Lever case AF_LOCAL: 1801176e21eeSChuck Lever xs_reclassify_socketu(sock); 1802176e21eeSChuck Lever break; 18034232e863SChuck Lever case AF_INET: 18046bc9638aSPavel Emelyanov xs_reclassify_socket4(sock); 18054232e863SChuck Lever break; 18064232e863SChuck Lever case AF_INET6: 18076bc9638aSPavel Emelyanov xs_reclassify_socket6(sock); 18084232e863SChuck Lever break; 18094232e863SChuck Lever } 18106bc9638aSPavel Emelyanov } 1811ed07536eSPeter Zijlstra #else 1812176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock) 1813176e21eeSChuck Lever { 1814176e21eeSChuck Lever } 1815176e21eeSChuck Lever 18168945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock) 18178945ee5eSChuck Lever { 18188945ee5eSChuck Lever } 18198945ee5eSChuck Lever 18208945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock) 1821ed07536eSPeter Zijlstra { 1822ed07536eSPeter Zijlstra } 18236bc9638aSPavel Emelyanov 18246bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock) 18256bc9638aSPavel Emelyanov { 18266bc9638aSPavel Emelyanov } 1827ed07536eSPeter Zijlstra #endif 1828ed07536eSPeter Zijlstra 18296bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt, 18306bc9638aSPavel Emelyanov struct sock_xprt *transport, int family, int type, int protocol) 183122f79326SPavel Emelyanov { 183222f79326SPavel Emelyanov struct socket *sock; 183322f79326SPavel Emelyanov int err; 183422f79326SPavel Emelyanov 18356bc9638aSPavel Emelyanov err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 183622f79326SPavel Emelyanov if (err < 0) { 183722f79326SPavel Emelyanov dprintk("RPC: can't create %d transport socket (%d).\n", 183822f79326SPavel Emelyanov protocol, -err); 183922f79326SPavel Emelyanov goto out; 184022f79326SPavel Emelyanov } 18416bc9638aSPavel Emelyanov xs_reclassify_socket(family, sock); 184222f79326SPavel Emelyanov 18434cea288aSBen Hutchings err = xs_bind(transport, sock); 18444cea288aSBen Hutchings if (err) { 184522f79326SPavel Emelyanov sock_release(sock); 184622f79326SPavel Emelyanov goto out; 184722f79326SPavel Emelyanov } 184822f79326SPavel Emelyanov 184922f79326SPavel Emelyanov return sock; 185022f79326SPavel Emelyanov out: 185122f79326SPavel Emelyanov return ERR_PTR(err); 185222f79326SPavel Emelyanov } 185322f79326SPavel Emelyanov 1854176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1855176e21eeSChuck Lever struct socket *sock) 1856176e21eeSChuck Lever { 1857176e21eeSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1858176e21eeSChuck Lever xprt); 1859176e21eeSChuck Lever 1860176e21eeSChuck Lever if (!transport->inet) { 1861176e21eeSChuck Lever struct sock *sk = sock->sk; 1862176e21eeSChuck Lever 1863176e21eeSChuck Lever write_lock_bh(&sk->sk_callback_lock); 1864176e21eeSChuck Lever 1865176e21eeSChuck Lever xs_save_old_callbacks(transport, sk); 1866176e21eeSChuck Lever 1867176e21eeSChuck Lever sk->sk_user_data = xprt; 1868176e21eeSChuck Lever sk->sk_data_ready = xs_local_data_ready; 1869176e21eeSChuck Lever sk->sk_write_space = xs_udp_write_space; 1870176e21eeSChuck Lever sk->sk_error_report = xs_error_report; 1871176e21eeSChuck Lever sk->sk_allocation = GFP_ATOMIC; 1872176e21eeSChuck Lever 1873176e21eeSChuck Lever xprt_clear_connected(xprt); 1874176e21eeSChuck Lever 1875176e21eeSChuck Lever /* Reset to new socket */ 1876176e21eeSChuck Lever transport->sock = sock; 1877176e21eeSChuck Lever transport->inet = sk; 1878176e21eeSChuck Lever 1879176e21eeSChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1880176e21eeSChuck Lever } 1881176e21eeSChuck Lever 1882176e21eeSChuck Lever /* Tell the socket layer to start connecting... */ 1883176e21eeSChuck Lever xprt->stat.connect_count++; 1884176e21eeSChuck Lever xprt->stat.connect_start = jiffies; 1885176e21eeSChuck Lever return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1886176e21eeSChuck Lever } 1887176e21eeSChuck Lever 1888176e21eeSChuck Lever /** 1889176e21eeSChuck Lever * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1890176e21eeSChuck Lever * @xprt: RPC transport to connect 1891176e21eeSChuck Lever * @transport: socket transport to connect 1892176e21eeSChuck Lever * @create_sock: function to create a socket of the correct type 1893176e21eeSChuck Lever * 1894176e21eeSChuck Lever * Invoked by a work queue tasklet. 1895176e21eeSChuck Lever */ 1896176e21eeSChuck Lever static void xs_local_setup_socket(struct work_struct *work) 1897176e21eeSChuck Lever { 1898176e21eeSChuck Lever struct sock_xprt *transport = 1899176e21eeSChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 1900176e21eeSChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1901176e21eeSChuck Lever struct socket *sock; 1902176e21eeSChuck Lever int status = -EIO; 1903176e21eeSChuck Lever 1904176e21eeSChuck Lever if (xprt->shutdown) 1905176e21eeSChuck Lever goto out; 1906176e21eeSChuck Lever 19075cf02d09SJeff Layton current->flags |= PF_FSTRANS; 19085cf02d09SJeff Layton 1909176e21eeSChuck Lever clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 1910176e21eeSChuck Lever status = __sock_create(xprt->xprt_net, AF_LOCAL, 1911176e21eeSChuck Lever SOCK_STREAM, 0, &sock, 1); 1912176e21eeSChuck Lever if (status < 0) { 1913176e21eeSChuck Lever dprintk("RPC: can't create AF_LOCAL " 1914176e21eeSChuck Lever "transport socket (%d).\n", -status); 1915176e21eeSChuck Lever goto out; 1916176e21eeSChuck Lever } 1917176e21eeSChuck Lever xs_reclassify_socketu(sock); 1918176e21eeSChuck Lever 1919176e21eeSChuck Lever dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1920176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1921176e21eeSChuck Lever 1922176e21eeSChuck Lever status = xs_local_finish_connecting(xprt, sock); 1923176e21eeSChuck Lever switch (status) { 1924176e21eeSChuck Lever case 0: 1925176e21eeSChuck Lever dprintk("RPC: xprt %p connected to %s\n", 1926176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1927176e21eeSChuck Lever xprt_set_connected(xprt); 1928176e21eeSChuck Lever break; 1929176e21eeSChuck Lever case -ENOENT: 1930176e21eeSChuck Lever dprintk("RPC: xprt %p: socket %s does not exist\n", 1931176e21eeSChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1932176e21eeSChuck Lever break; 1933176e21eeSChuck Lever default: 1934176e21eeSChuck Lever printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1935176e21eeSChuck Lever __func__, -status, 1936176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 1937176e21eeSChuck Lever } 1938176e21eeSChuck Lever 1939176e21eeSChuck Lever out: 1940176e21eeSChuck Lever xprt_clear_connecting(xprt); 1941176e21eeSChuck Lever xprt_wake_pending_tasks(xprt, status); 19425cf02d09SJeff Layton current->flags &= ~PF_FSTRANS; 1943176e21eeSChuck Lever } 1944176e21eeSChuck Lever 1945a564b8f0SMel Gorman #ifdef CONFIG_SUNRPC_SWAP 1946a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 1947a564b8f0SMel Gorman { 1948a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1949a564b8f0SMel Gorman xprt); 1950a564b8f0SMel Gorman 1951a564b8f0SMel Gorman if (xprt->swapper) 1952a564b8f0SMel Gorman sk_set_memalloc(transport->inet); 1953a564b8f0SMel Gorman } 1954a564b8f0SMel Gorman 1955a564b8f0SMel Gorman /** 1956a564b8f0SMel Gorman * xs_swapper - Tag this transport as being used for swap. 1957a564b8f0SMel Gorman * @xprt: transport to tag 1958a564b8f0SMel Gorman * @enable: enable/disable 1959a564b8f0SMel Gorman * 1960a564b8f0SMel Gorman */ 1961a564b8f0SMel Gorman int xs_swapper(struct rpc_xprt *xprt, int enable) 1962a564b8f0SMel Gorman { 1963a564b8f0SMel Gorman struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1964a564b8f0SMel Gorman xprt); 1965a564b8f0SMel Gorman int err = 0; 1966a564b8f0SMel Gorman 1967a564b8f0SMel Gorman if (enable) { 1968a564b8f0SMel Gorman xprt->swapper++; 1969a564b8f0SMel Gorman xs_set_memalloc(xprt); 1970a564b8f0SMel Gorman } else if (xprt->swapper) { 1971a564b8f0SMel Gorman xprt->swapper--; 1972a564b8f0SMel Gorman sk_clear_memalloc(transport->inet); 1973a564b8f0SMel Gorman } 1974a564b8f0SMel Gorman 1975a564b8f0SMel Gorman return err; 1976a564b8f0SMel Gorman } 1977a564b8f0SMel Gorman EXPORT_SYMBOL_GPL(xs_swapper); 1978a564b8f0SMel Gorman #else 1979a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt) 1980a564b8f0SMel Gorman { 1981a564b8f0SMel Gorman } 1982a564b8f0SMel Gorman #endif 1983a564b8f0SMel Gorman 198416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 1985a246b010SChuck Lever { 198616be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1987edb267a6SChuck Lever 1988ee0ac0c2SChuck Lever if (!transport->inet) { 1989b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1990b0d93ad5SChuck Lever 1991b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1992b0d93ad5SChuck Lever 19932a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 19942a9e1cfaSTrond Myklebust 1995b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1996b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1997b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1998482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 1999b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 2000b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 2001b0d93ad5SChuck Lever 2002b0d93ad5SChuck Lever xprt_set_connected(xprt); 2003b0d93ad5SChuck Lever 2004b0d93ad5SChuck Lever /* Reset to new socket */ 2005ee0ac0c2SChuck Lever transport->sock = sock; 2006ee0ac0c2SChuck Lever transport->inet = sk; 2007b0d93ad5SChuck Lever 2008a564b8f0SMel Gorman xs_set_memalloc(xprt); 2009a564b8f0SMel Gorman 2010b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2011b0d93ad5SChuck Lever } 2012470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 201316be2d20SChuck Lever } 201416be2d20SChuck Lever 20158c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work) 2016a246b010SChuck Lever { 2017a246b010SChuck Lever struct sock_xprt *transport = 2018a246b010SChuck Lever container_of(work, struct sock_xprt, connect_worker.work); 2019a246b010SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 2020a246b010SChuck Lever struct socket *sock = transport->sock; 2021b65c0310SPavel Emelyanov int status = -EIO; 2022a246b010SChuck Lever 202301d37c42STrond Myklebust if (xprt->shutdown) 20249903cd1cSChuck Lever goto out; 20259903cd1cSChuck Lever 20265cf02d09SJeff Layton current->flags |= PF_FSTRANS; 20275cf02d09SJeff Layton 2028a246b010SChuck Lever /* Start by resetting any existing state */ 2029fe315e76SChuck Lever xs_reset_transport(transport); 20308c14ff2aSPavel Emelyanov sock = xs_create_sock(xprt, transport, 20318c14ff2aSPavel Emelyanov xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP); 2032b65c0310SPavel Emelyanov if (IS_ERR(sock)) 2033a246b010SChuck Lever goto out; 203468e220bdSChuck Lever 2035c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2036c740eff8SChuck Lever "%s (port %s)\n", xprt, 2037c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2038c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2039c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 204068e220bdSChuck Lever 204168e220bdSChuck Lever xs_udp_finish_connecting(xprt, sock); 2042a246b010SChuck Lever status = 0; 2043b0d93ad5SChuck Lever out: 2044b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 20457d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 20465cf02d09SJeff Layton current->flags &= ~PF_FSTRANS; 2047b0d93ad5SChuck Lever } 2048b0d93ad5SChuck Lever 20493167e12cSChuck Lever /* 20503167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 20513167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 20523167e12cSChuck Lever */ 205358dddac9SPavel Emelyanov static void xs_abort_connection(struct sock_xprt *transport) 20543167e12cSChuck Lever { 20553167e12cSChuck Lever int result; 20563167e12cSChuck Lever struct sockaddr any; 20573167e12cSChuck Lever 205858dddac9SPavel Emelyanov dprintk("RPC: disconnecting xprt %p to reuse port\n", transport); 20593167e12cSChuck Lever 20603167e12cSChuck Lever /* 20613167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 20623167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 20633167e12cSChuck Lever */ 20643167e12cSChuck Lever memset(&any, 0, sizeof(any)); 20653167e12cSChuck Lever any.sa_family = AF_UNSPEC; 2066ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 20677d1e8255STrond Myklebust if (!result) 206858dddac9SPavel Emelyanov xs_sock_mark_closed(&transport->xprt); 20697d1e8255STrond Myklebust else 20703167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 20713167e12cSChuck Lever result); 20723167e12cSChuck Lever } 20733167e12cSChuck Lever 207458dddac9SPavel Emelyanov static void xs_tcp_reuse_connection(struct sock_xprt *transport) 207540d2549dSTrond Myklebust { 207640d2549dSTrond Myklebust unsigned int state = transport->inet->sk_state; 207740d2549dSTrond Myklebust 2078669502ffSAndy Chittenden if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) { 2079669502ffSAndy Chittenden /* we don't need to abort the connection if the socket 2080669502ffSAndy Chittenden * hasn't undergone a shutdown 2081669502ffSAndy Chittenden */ 2082669502ffSAndy Chittenden if (transport->inet->sk_shutdown == 0) 208340d2549dSTrond Myklebust return; 2084669502ffSAndy Chittenden dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n", 2085669502ffSAndy Chittenden __func__, transport->inet->sk_shutdown); 2086669502ffSAndy Chittenden } 2087669502ffSAndy Chittenden if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) { 2088669502ffSAndy Chittenden /* we don't need to abort the connection if the socket 2089669502ffSAndy Chittenden * hasn't undergone a shutdown 2090669502ffSAndy Chittenden */ 2091669502ffSAndy Chittenden if (transport->inet->sk_shutdown == 0) 209240d2549dSTrond Myklebust return; 2093669502ffSAndy Chittenden dprintk("RPC: %s: ESTABLISHED/SYN_SENT " 2094669502ffSAndy Chittenden "sk_shutdown set to %d\n", 2095669502ffSAndy Chittenden __func__, transport->inet->sk_shutdown); 2096669502ffSAndy Chittenden } 209758dddac9SPavel Emelyanov xs_abort_connection(transport); 209840d2549dSTrond Myklebust } 209940d2549dSTrond Myklebust 210016be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2101b0d93ad5SChuck Lever { 210216be2d20SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2103fe19a96bSTrond Myklebust int ret = -ENOTCONN; 2104edb267a6SChuck Lever 2105ee0ac0c2SChuck Lever if (!transport->inet) { 2106b0d93ad5SChuck Lever struct sock *sk = sock->sk; 2107b0d93ad5SChuck Lever 2108b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 2109b0d93ad5SChuck Lever 21102a9e1cfaSTrond Myklebust xs_save_old_callbacks(transport, sk); 21112a9e1cfaSTrond Myklebust 2112b0d93ad5SChuck Lever sk->sk_user_data = xprt; 2113b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 2114b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 2115b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 2116482f32e6STrond Myklebust sk->sk_error_report = xs_error_report; 2117b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 21183167e12cSChuck Lever 21193167e12cSChuck Lever /* socket options */ 21203167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 21213167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 21223167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 21233167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2124b0d93ad5SChuck Lever 2125b0d93ad5SChuck Lever xprt_clear_connected(xprt); 2126b0d93ad5SChuck Lever 2127b0d93ad5SChuck Lever /* Reset to new socket */ 2128ee0ac0c2SChuck Lever transport->sock = sock; 2129ee0ac0c2SChuck Lever transport->inet = sk; 2130b0d93ad5SChuck Lever 2131b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 2132b0d93ad5SChuck Lever } 2133b0d93ad5SChuck Lever 213401d37c42STrond Myklebust if (!xprt_bound(xprt)) 2135fe19a96bSTrond Myklebust goto out; 213601d37c42STrond Myklebust 2137a564b8f0SMel Gorman xs_set_memalloc(xprt); 2138a564b8f0SMel Gorman 2139b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 2140262ca07dSChuck Lever xprt->stat.connect_count++; 2141262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 2142fe19a96bSTrond Myklebust ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2143fe19a96bSTrond Myklebust switch (ret) { 2144fe19a96bSTrond Myklebust case 0: 2145fe19a96bSTrond Myklebust case -EINPROGRESS: 2146fe19a96bSTrond Myklebust /* SYN_SENT! */ 2147fe19a96bSTrond Myklebust xprt->connect_cookie++; 2148fe19a96bSTrond Myklebust if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2149fe19a96bSTrond Myklebust xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2150fe19a96bSTrond Myklebust } 2151fe19a96bSTrond Myklebust out: 2152fe19a96bSTrond Myklebust return ret; 215316be2d20SChuck Lever } 215416be2d20SChuck Lever 215516be2d20SChuck Lever /** 2156b61d59ffSTrond Myklebust * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2157b61d59ffSTrond Myklebust * @xprt: RPC transport to connect 2158b61d59ffSTrond Myklebust * @transport: socket transport to connect 2159b61d59ffSTrond Myklebust * @create_sock: function to create a socket of the correct type 216016be2d20SChuck Lever * 216116be2d20SChuck Lever * Invoked by a work queue tasklet. 216216be2d20SChuck Lever */ 2163cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work) 216416be2d20SChuck Lever { 2165cdd518d5SPavel Emelyanov struct sock_xprt *transport = 2166cdd518d5SPavel Emelyanov container_of(work, struct sock_xprt, connect_worker.work); 216716be2d20SChuck Lever struct socket *sock = transport->sock; 2168a9f5f0f7SPavel Emelyanov struct rpc_xprt *xprt = &transport->xprt; 2169b61d59ffSTrond Myklebust int status = -EIO; 217016be2d20SChuck Lever 217101d37c42STrond Myklebust if (xprt->shutdown) 217216be2d20SChuck Lever goto out; 217316be2d20SChuck Lever 21745cf02d09SJeff Layton current->flags |= PF_FSTRANS; 21755cf02d09SJeff Layton 217616be2d20SChuck Lever if (!sock) { 21777d1e8255STrond Myklebust clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); 2178cdd518d5SPavel Emelyanov sock = xs_create_sock(xprt, transport, 2179cdd518d5SPavel Emelyanov xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP); 2180b61d59ffSTrond Myklebust if (IS_ERR(sock)) { 2181b61d59ffSTrond Myklebust status = PTR_ERR(sock); 218216be2d20SChuck Lever goto out; 218316be2d20SChuck Lever } 21847d1e8255STrond Myklebust } else { 21857d1e8255STrond Myklebust int abort_and_exit; 21867d1e8255STrond Myklebust 21877d1e8255STrond Myklebust abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, 21887d1e8255STrond Myklebust &xprt->state); 218916be2d20SChuck Lever /* "close" the socket, preserving the local port */ 219058dddac9SPavel Emelyanov xs_tcp_reuse_connection(transport); 219116be2d20SChuck Lever 21927d1e8255STrond Myklebust if (abort_and_exit) 21937d1e8255STrond Myklebust goto out_eagain; 21947d1e8255STrond Myklebust } 21957d1e8255STrond Myklebust 2196c740eff8SChuck Lever dprintk("RPC: worker connecting xprt %p via %s to " 2197c740eff8SChuck Lever "%s (port %s)\n", xprt, 2198c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO], 2199c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2200c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT]); 220116be2d20SChuck Lever 220216be2d20SChuck Lever status = xs_tcp_finish_connecting(xprt, sock); 2203a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 220446121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 220546121cf7SChuck Lever sock->sk->sk_state); 2206a246b010SChuck Lever switch (status) { 2207f75e6745STrond Myklebust default: 2208f75e6745STrond Myklebust printk("%s: connect returned unhandled error %d\n", 2209f75e6745STrond Myklebust __func__, status); 2210f75e6745STrond Myklebust case -EADDRNOTAVAIL: 2211f75e6745STrond Myklebust /* We're probably in TIME_WAIT. Get rid of existing socket, 2212f75e6745STrond Myklebust * and retry 2213f75e6745STrond Myklebust */ 2214*a519fc7aSTrond Myklebust xs_tcp_force_close(xprt); 221588b5ed73STrond Myklebust break; 22168a2cec29STrond Myklebust case -ECONNREFUSED: 22178a2cec29STrond Myklebust case -ECONNRESET: 22188a2cec29STrond Myklebust case -ENETUNREACH: 22198a2cec29STrond Myklebust /* retry with existing socket, after a delay */ 22202a491991STrond Myklebust case 0: 2221a246b010SChuck Lever case -EINPROGRESS: 2222a246b010SChuck Lever case -EALREADY: 22237d1e8255STrond Myklebust xprt_clear_connecting(xprt); 22245cf02d09SJeff Layton current->flags &= ~PF_FSTRANS; 22257d1e8255STrond Myklebust return; 22269fcfe0c8STrond Myklebust case -EINVAL: 22279fcfe0c8STrond Myklebust /* Happens, for instance, if the user specified a link 22289fcfe0c8STrond Myklebust * local IPv6 address without a scope-id. 22299fcfe0c8STrond Myklebust */ 22309fcfe0c8STrond Myklebust goto out; 22318a2cec29STrond Myklebust } 22327d1e8255STrond Myklebust out_eagain: 22332a491991STrond Myklebust status = -EAGAIN; 2234a246b010SChuck Lever out: 22352226feb6SChuck Lever xprt_clear_connecting(xprt); 22367d1e8255STrond Myklebust xprt_wake_pending_tasks(xprt, status); 22375cf02d09SJeff Layton current->flags &= ~PF_FSTRANS; 2238a246b010SChuck Lever } 2239a246b010SChuck Lever 224068e220bdSChuck Lever /** 22419903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 22429903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 22439903cd1cSChuck Lever * 22449903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 224503bf4b70SChuck Lever * 224603bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 224703bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 224803bf4b70SChuck Lever * socket on a privileged port. 224903bf4b70SChuck Lever * 225003bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 225103bf4b70SChuck Lever * retry floods (hard mounts). 22529903cd1cSChuck Lever */ 22539903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 2254a246b010SChuck Lever { 2255a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 2256ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2257a246b010SChuck Lever 225809a21c41SChuck Lever if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) { 225946121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 226046121cf7SChuck Lever "seconds\n", 226103bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 2262c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2263c1384c9cSTrond Myklebust &transport->connect_worker, 226403bf4b70SChuck Lever xprt->reestablish_timeout); 226503bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 226661d0a8e6SNeil Brown if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 226761d0a8e6SNeil Brown xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 226803bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 226903bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 22709903cd1cSChuck Lever } else { 22719903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2272c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 2273c1384c9cSTrond Myklebust &transport->connect_worker, 0); 2274a246b010SChuck Lever } 2275a246b010SChuck Lever } 2276a246b010SChuck Lever 2277262ca07dSChuck Lever /** 2278176e21eeSChuck Lever * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2279176e21eeSChuck Lever * @xprt: rpc_xprt struct containing statistics 2280176e21eeSChuck Lever * @seq: output file 2281176e21eeSChuck Lever * 2282176e21eeSChuck Lever */ 2283176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2284176e21eeSChuck Lever { 2285176e21eeSChuck Lever long idle_time = 0; 2286176e21eeSChuck Lever 2287176e21eeSChuck Lever if (xprt_connected(xprt)) 2288176e21eeSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2289176e21eeSChuck Lever 2290176e21eeSChuck Lever seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 229115a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2292176e21eeSChuck Lever xprt->stat.bind_count, 2293176e21eeSChuck Lever xprt->stat.connect_count, 2294176e21eeSChuck Lever xprt->stat.connect_time, 2295176e21eeSChuck Lever idle_time, 2296176e21eeSChuck Lever xprt->stat.sends, 2297176e21eeSChuck Lever xprt->stat.recvs, 2298176e21eeSChuck Lever xprt->stat.bad_xids, 2299176e21eeSChuck Lever xprt->stat.req_u, 230015a45206SAndy Adamson xprt->stat.bklog_u, 230115a45206SAndy Adamson xprt->stat.max_slots, 230215a45206SAndy Adamson xprt->stat.sending_u, 230315a45206SAndy Adamson xprt->stat.pending_u); 2304176e21eeSChuck Lever } 2305176e21eeSChuck Lever 2306176e21eeSChuck Lever /** 2307262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 2308262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2309262ca07dSChuck Lever * @seq: output file 2310262ca07dSChuck Lever * 2311262ca07dSChuck Lever */ 2312262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2313262ca07dSChuck Lever { 2314c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2315c8475461SChuck Lever 231615a45206SAndy Adamson seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 231715a45206SAndy Adamson "%lu %llu %llu\n", 2318fbfffbd5SChuck Lever transport->srcport, 2319262ca07dSChuck Lever xprt->stat.bind_count, 2320262ca07dSChuck Lever xprt->stat.sends, 2321262ca07dSChuck Lever xprt->stat.recvs, 2322262ca07dSChuck Lever xprt->stat.bad_xids, 2323262ca07dSChuck Lever xprt->stat.req_u, 232415a45206SAndy Adamson xprt->stat.bklog_u, 232515a45206SAndy Adamson xprt->stat.max_slots, 232615a45206SAndy Adamson xprt->stat.sending_u, 232715a45206SAndy Adamson xprt->stat.pending_u); 2328262ca07dSChuck Lever } 2329262ca07dSChuck Lever 2330262ca07dSChuck Lever /** 2331262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 2332262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 2333262ca07dSChuck Lever * @seq: output file 2334262ca07dSChuck Lever * 2335262ca07dSChuck Lever */ 2336262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2337262ca07dSChuck Lever { 2338c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2339262ca07dSChuck Lever long idle_time = 0; 2340262ca07dSChuck Lever 2341262ca07dSChuck Lever if (xprt_connected(xprt)) 2342262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 2343262ca07dSChuck Lever 234415a45206SAndy Adamson seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 234515a45206SAndy Adamson "%llu %llu %lu %llu %llu\n", 2346fbfffbd5SChuck Lever transport->srcport, 2347262ca07dSChuck Lever xprt->stat.bind_count, 2348262ca07dSChuck Lever xprt->stat.connect_count, 2349262ca07dSChuck Lever xprt->stat.connect_time, 2350262ca07dSChuck Lever idle_time, 2351262ca07dSChuck Lever xprt->stat.sends, 2352262ca07dSChuck Lever xprt->stat.recvs, 2353262ca07dSChuck Lever xprt->stat.bad_xids, 2354262ca07dSChuck Lever xprt->stat.req_u, 235515a45206SAndy Adamson xprt->stat.bklog_u, 235615a45206SAndy Adamson xprt->stat.max_slots, 235715a45206SAndy Adamson xprt->stat.sending_u, 235815a45206SAndy Adamson xprt->stat.pending_u); 2359262ca07dSChuck Lever } 2360262ca07dSChuck Lever 23614cfc7e60SRahul Iyer /* 23624cfc7e60SRahul Iyer * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 23634cfc7e60SRahul Iyer * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 23644cfc7e60SRahul Iyer * to use the server side send routines. 23654cfc7e60SRahul Iyer */ 23665a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size) 23674cfc7e60SRahul Iyer { 23684cfc7e60SRahul Iyer struct page *page; 23694cfc7e60SRahul Iyer struct rpc_buffer *buf; 23704cfc7e60SRahul Iyer 23714cfc7e60SRahul Iyer BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer)); 23724cfc7e60SRahul Iyer page = alloc_page(GFP_KERNEL); 23734cfc7e60SRahul Iyer 23744cfc7e60SRahul Iyer if (!page) 23754cfc7e60SRahul Iyer return NULL; 23764cfc7e60SRahul Iyer 23774cfc7e60SRahul Iyer buf = page_address(page); 23784cfc7e60SRahul Iyer buf->len = PAGE_SIZE; 23794cfc7e60SRahul Iyer 23804cfc7e60SRahul Iyer return buf->data; 23814cfc7e60SRahul Iyer } 23824cfc7e60SRahul Iyer 23834cfc7e60SRahul Iyer /* 23844cfc7e60SRahul Iyer * Free the space allocated in the bc_alloc routine 23854cfc7e60SRahul Iyer */ 23865a51f13aSH Hartley Sweeten static void bc_free(void *buffer) 23874cfc7e60SRahul Iyer { 23884cfc7e60SRahul Iyer struct rpc_buffer *buf; 23894cfc7e60SRahul Iyer 23904cfc7e60SRahul Iyer if (!buffer) 23914cfc7e60SRahul Iyer return; 23924cfc7e60SRahul Iyer 23934cfc7e60SRahul Iyer buf = container_of(buffer, struct rpc_buffer, data); 23944cfc7e60SRahul Iyer free_page((unsigned long)buf); 23954cfc7e60SRahul Iyer } 23964cfc7e60SRahul Iyer 23974cfc7e60SRahul Iyer /* 23984cfc7e60SRahul Iyer * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 23994cfc7e60SRahul Iyer * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 24004cfc7e60SRahul Iyer */ 24014cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req) 24024cfc7e60SRahul Iyer { 24034cfc7e60SRahul Iyer int len; 24044cfc7e60SRahul Iyer struct xdr_buf *xbufp = &req->rq_snd_buf; 24054cfc7e60SRahul Iyer struct rpc_xprt *xprt = req->rq_xprt; 24064cfc7e60SRahul Iyer struct sock_xprt *transport = 24074cfc7e60SRahul Iyer container_of(xprt, struct sock_xprt, xprt); 24084cfc7e60SRahul Iyer struct socket *sock = transport->sock; 24094cfc7e60SRahul Iyer unsigned long headoff; 24104cfc7e60SRahul Iyer unsigned long tailoff; 24114cfc7e60SRahul Iyer 241261677eeeSChuck Lever xs_encode_stream_record_marker(xbufp); 24134cfc7e60SRahul Iyer 24144cfc7e60SRahul Iyer tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 24154cfc7e60SRahul Iyer headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 24164cfc7e60SRahul Iyer len = svc_send_common(sock, xbufp, 24174cfc7e60SRahul Iyer virt_to_page(xbufp->head[0].iov_base), headoff, 24184cfc7e60SRahul Iyer xbufp->tail[0].iov_base, tailoff); 24194cfc7e60SRahul Iyer 24204cfc7e60SRahul Iyer if (len != xbufp->len) { 24214cfc7e60SRahul Iyer printk(KERN_NOTICE "Error sending entire callback!\n"); 24224cfc7e60SRahul Iyer len = -EAGAIN; 24234cfc7e60SRahul Iyer } 24244cfc7e60SRahul Iyer 24254cfc7e60SRahul Iyer return len; 24264cfc7e60SRahul Iyer } 24274cfc7e60SRahul Iyer 24284cfc7e60SRahul Iyer /* 24294cfc7e60SRahul Iyer * The send routine. Borrows from svc_send 24304cfc7e60SRahul Iyer */ 24314cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task) 24324cfc7e60SRahul Iyer { 24334cfc7e60SRahul Iyer struct rpc_rqst *req = task->tk_rqstp; 24344cfc7e60SRahul Iyer struct svc_xprt *xprt; 24354cfc7e60SRahul Iyer struct svc_sock *svsk; 24364cfc7e60SRahul Iyer u32 len; 24374cfc7e60SRahul Iyer 24384cfc7e60SRahul Iyer dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 24394cfc7e60SRahul Iyer /* 24404cfc7e60SRahul Iyer * Get the server socket associated with this callback xprt 24414cfc7e60SRahul Iyer */ 24424cfc7e60SRahul Iyer xprt = req->rq_xprt->bc_xprt; 24434cfc7e60SRahul Iyer svsk = container_of(xprt, struct svc_sock, sk_xprt); 24444cfc7e60SRahul Iyer 24454cfc7e60SRahul Iyer /* 24464cfc7e60SRahul Iyer * Grab the mutex to serialize data as the connection is shared 24474cfc7e60SRahul Iyer * with the fore channel 24484cfc7e60SRahul Iyer */ 24494cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) { 24504cfc7e60SRahul Iyer rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); 24514cfc7e60SRahul Iyer if (!mutex_trylock(&xprt->xpt_mutex)) 24524cfc7e60SRahul Iyer return -EAGAIN; 24534cfc7e60SRahul Iyer rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); 24544cfc7e60SRahul Iyer } 24554cfc7e60SRahul Iyer if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 24564cfc7e60SRahul Iyer len = -ENOTCONN; 24574cfc7e60SRahul Iyer else 24584cfc7e60SRahul Iyer len = bc_sendto(req); 24594cfc7e60SRahul Iyer mutex_unlock(&xprt->xpt_mutex); 24604cfc7e60SRahul Iyer 24614cfc7e60SRahul Iyer if (len > 0) 24624cfc7e60SRahul Iyer len = 0; 24634cfc7e60SRahul Iyer 24644cfc7e60SRahul Iyer return len; 24654cfc7e60SRahul Iyer } 24664cfc7e60SRahul Iyer 24674cfc7e60SRahul Iyer /* 24684cfc7e60SRahul Iyer * The close routine. Since this is client initiated, we do nothing 24694cfc7e60SRahul Iyer */ 24704cfc7e60SRahul Iyer 24714cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt) 24724cfc7e60SRahul Iyer { 24734cfc7e60SRahul Iyer } 24744cfc7e60SRahul Iyer 24754cfc7e60SRahul Iyer /* 24764cfc7e60SRahul Iyer * The xprt destroy routine. Again, because this connection is client 24774cfc7e60SRahul Iyer * initiated, we do nothing 24784cfc7e60SRahul Iyer */ 24794cfc7e60SRahul Iyer 24804cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt) 24814cfc7e60SRahul Iyer { 24824cfc7e60SRahul Iyer } 24834cfc7e60SRahul Iyer 2484176e21eeSChuck Lever static struct rpc_xprt_ops xs_local_ops = { 2485176e21eeSChuck Lever .reserve_xprt = xprt_reserve_xprt, 2486176e21eeSChuck Lever .release_xprt = xs_tcp_release_xprt, 2487f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 2488176e21eeSChuck Lever .rpcbind = xs_local_rpcbind, 2489176e21eeSChuck Lever .set_port = xs_local_set_port, 2490176e21eeSChuck Lever .connect = xs_connect, 2491176e21eeSChuck Lever .buf_alloc = rpc_malloc, 2492176e21eeSChuck Lever .buf_free = rpc_free, 2493176e21eeSChuck Lever .send_request = xs_local_send_request, 2494176e21eeSChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2495176e21eeSChuck Lever .close = xs_close, 2496176e21eeSChuck Lever .destroy = xs_destroy, 2497176e21eeSChuck Lever .print_stats = xs_local_print_stats, 2498176e21eeSChuck Lever }; 2499176e21eeSChuck Lever 2500262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 250143118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 250212a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 250349e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 2504f39c1bfbSTrond Myklebust .alloc_slot = xprt_alloc_slot, 250545160d62SChuck Lever .rpcbind = rpcb_getport_async, 250692200412SChuck Lever .set_port = xs_set_port, 25079903cd1cSChuck Lever .connect = xs_connect, 250802107148SChuck Lever .buf_alloc = rpc_malloc, 250902107148SChuck Lever .buf_free = rpc_free, 2510262965f5SChuck Lever .send_request = xs_udp_send_request, 2511fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 251246c0ee8bSChuck Lever .timer = xs_udp_timer, 2513a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 2514262965f5SChuck Lever .close = xs_close, 2515262965f5SChuck Lever .destroy = xs_destroy, 2516262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 2517262965f5SChuck Lever }; 2518262965f5SChuck Lever 2519262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 252012a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 2521e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 2522f39c1bfbSTrond Myklebust .alloc_slot = xprt_lock_and_alloc_slot, 252345160d62SChuck Lever .rpcbind = rpcb_getport_async, 252492200412SChuck Lever .set_port = xs_set_port, 25250b9e7943STrond Myklebust .connect = xs_connect, 252602107148SChuck Lever .buf_alloc = rpc_malloc, 252702107148SChuck Lever .buf_free = rpc_free, 2528262965f5SChuck Lever .send_request = xs_tcp_send_request, 2529fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 2530f75e6745STrond Myklebust .close = xs_tcp_close, 25319903cd1cSChuck Lever .destroy = xs_destroy, 2532262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 2533a246b010SChuck Lever }; 2534a246b010SChuck Lever 25354cfc7e60SRahul Iyer /* 25364cfc7e60SRahul Iyer * The rpc_xprt_ops for the server backchannel 25374cfc7e60SRahul Iyer */ 25384cfc7e60SRahul Iyer 25394cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = { 25404cfc7e60SRahul Iyer .reserve_xprt = xprt_reserve_xprt, 25414cfc7e60SRahul Iyer .release_xprt = xprt_release_xprt, 254292769108SJ. Bruce Fields .rpcbind = xs_local_rpcbind, 25434cfc7e60SRahul Iyer .buf_alloc = bc_malloc, 25444cfc7e60SRahul Iyer .buf_free = bc_free, 25454cfc7e60SRahul Iyer .send_request = bc_send_request, 25464cfc7e60SRahul Iyer .set_retrans_timeout = xprt_set_retrans_timeout_def, 25474cfc7e60SRahul Iyer .close = bc_close, 25484cfc7e60SRahul Iyer .destroy = bc_destroy, 25494cfc7e60SRahul Iyer .print_stats = xs_tcp_print_stats, 25504cfc7e60SRahul Iyer }; 25514cfc7e60SRahul Iyer 255292476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap) 255392476850SChuck Lever { 255492476850SChuck Lever static const struct sockaddr_in sin = { 255592476850SChuck Lever .sin_family = AF_INET, 255692476850SChuck Lever .sin_addr.s_addr = htonl(INADDR_ANY), 255792476850SChuck Lever }; 255892476850SChuck Lever static const struct sockaddr_in6 sin6 = { 255992476850SChuck Lever .sin6_family = AF_INET6, 256092476850SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 256192476850SChuck Lever }; 256292476850SChuck Lever 256392476850SChuck Lever switch (family) { 2564176e21eeSChuck Lever case AF_LOCAL: 2565176e21eeSChuck Lever break; 256692476850SChuck Lever case AF_INET: 256792476850SChuck Lever memcpy(sap, &sin, sizeof(sin)); 256892476850SChuck Lever break; 256992476850SChuck Lever case AF_INET6: 257092476850SChuck Lever memcpy(sap, &sin6, sizeof(sin6)); 257192476850SChuck Lever break; 257292476850SChuck Lever default: 257392476850SChuck Lever dprintk("RPC: %s: Bad address family\n", __func__); 257492476850SChuck Lever return -EAFNOSUPPORT; 257592476850SChuck Lever } 257692476850SChuck Lever return 0; 257792476850SChuck Lever } 257892476850SChuck Lever 25793c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2580d9ba131dSTrond Myklebust unsigned int slot_table_size, 2581d9ba131dSTrond Myklebust unsigned int max_slot_table_size) 2582c8541ecdSChuck Lever { 2583c8541ecdSChuck Lever struct rpc_xprt *xprt; 2584ffc2e518SChuck Lever struct sock_xprt *new; 2585c8541ecdSChuck Lever 258696802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 2587c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 2588c8541ecdSChuck Lever return ERR_PTR(-EBADF); 2589c8541ecdSChuck Lever } 2590c8541ecdSChuck Lever 2591d9ba131dSTrond Myklebust xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2592d9ba131dSTrond Myklebust max_slot_table_size); 2593bd1722d4SPavel Emelyanov if (xprt == NULL) { 259446121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 259546121cf7SChuck Lever "rpc_xprt\n"); 2596c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 2597c8541ecdSChuck Lever } 2598c8541ecdSChuck Lever 2599bd1722d4SPavel Emelyanov new = container_of(xprt, struct sock_xprt, xprt); 260096802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 260196802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 2602d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 2603fbfffbd5SChuck Lever memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 260492476850SChuck Lever else { 260592476850SChuck Lever int err; 260692476850SChuck Lever err = xs_init_anyaddr(args->dstaddr->sa_family, 260792476850SChuck Lever (struct sockaddr *)&new->srcaddr); 26082aa13531SStanislav Kinsbursky if (err != 0) { 26092aa13531SStanislav Kinsbursky xprt_free(xprt); 261092476850SChuck Lever return ERR_PTR(err); 261192476850SChuck Lever } 26122aa13531SStanislav Kinsbursky } 2613c8541ecdSChuck Lever 2614c8541ecdSChuck Lever return xprt; 2615c8541ecdSChuck Lever } 2616c8541ecdSChuck Lever 2617176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = { 2618176e21eeSChuck Lever .to_initval = 10 * HZ, 2619176e21eeSChuck Lever .to_maxval = 10 * HZ, 2620176e21eeSChuck Lever .to_retries = 2, 2621176e21eeSChuck Lever }; 2622176e21eeSChuck Lever 2623176e21eeSChuck Lever /** 2624176e21eeSChuck Lever * xs_setup_local - Set up transport to use an AF_LOCAL socket 2625176e21eeSChuck Lever * @args: rpc transport creation arguments 2626176e21eeSChuck Lever * 2627176e21eeSChuck Lever * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2628176e21eeSChuck Lever */ 2629176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2630176e21eeSChuck Lever { 2631176e21eeSChuck Lever struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2632176e21eeSChuck Lever struct sock_xprt *transport; 2633176e21eeSChuck Lever struct rpc_xprt *xprt; 2634176e21eeSChuck Lever struct rpc_xprt *ret; 2635176e21eeSChuck Lever 2636d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2637d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2638176e21eeSChuck Lever if (IS_ERR(xprt)) 2639176e21eeSChuck Lever return xprt; 2640176e21eeSChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2641176e21eeSChuck Lever 2642176e21eeSChuck Lever xprt->prot = 0; 2643176e21eeSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2644176e21eeSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2645176e21eeSChuck Lever 2646176e21eeSChuck Lever xprt->bind_timeout = XS_BIND_TO; 2647176e21eeSChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2648176e21eeSChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2649176e21eeSChuck Lever 2650176e21eeSChuck Lever xprt->ops = &xs_local_ops; 2651176e21eeSChuck Lever xprt->timeout = &xs_local_default_timeout; 2652176e21eeSChuck Lever 2653176e21eeSChuck Lever switch (sun->sun_family) { 2654176e21eeSChuck Lever case AF_LOCAL: 2655176e21eeSChuck Lever if (sun->sun_path[0] != '/') { 2656176e21eeSChuck Lever dprintk("RPC: bad AF_LOCAL address: %s\n", 2657176e21eeSChuck Lever sun->sun_path); 2658176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2659176e21eeSChuck Lever goto out_err; 2660176e21eeSChuck Lever } 2661176e21eeSChuck Lever xprt_set_bound(xprt); 2662176e21eeSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 2663176e21eeSChuck Lever xs_local_setup_socket); 2664176e21eeSChuck Lever xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 2665176e21eeSChuck Lever break; 2666176e21eeSChuck Lever default: 2667176e21eeSChuck Lever ret = ERR_PTR(-EAFNOSUPPORT); 2668176e21eeSChuck Lever goto out_err; 2669176e21eeSChuck Lever } 2670176e21eeSChuck Lever 2671176e21eeSChuck Lever dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2672176e21eeSChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR]); 2673176e21eeSChuck Lever 2674176e21eeSChuck Lever if (try_module_get(THIS_MODULE)) 2675176e21eeSChuck Lever return xprt; 2676176e21eeSChuck Lever ret = ERR_PTR(-EINVAL); 2677176e21eeSChuck Lever out_err: 2678176e21eeSChuck Lever xprt_free(xprt); 2679176e21eeSChuck Lever return ret; 2680176e21eeSChuck Lever } 2681176e21eeSChuck Lever 26822881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = { 26832881ae74STrond Myklebust .to_initval = 5 * HZ, 26842881ae74STrond Myklebust .to_maxval = 30 * HZ, 26852881ae74STrond Myklebust .to_increment = 5 * HZ, 26862881ae74STrond Myklebust .to_retries = 5, 26872881ae74STrond Myklebust }; 26882881ae74STrond Myklebust 26899903cd1cSChuck Lever /** 26909903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 269196802a09SFrank van Maarseveen * @args: rpc transport creation arguments 26929903cd1cSChuck Lever * 26939903cd1cSChuck Lever */ 2694483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2695a246b010SChuck Lever { 26968f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2697c8541ecdSChuck Lever struct rpc_xprt *xprt; 2698c8475461SChuck Lever struct sock_xprt *transport; 26990a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2700a246b010SChuck Lever 2701d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2702d9ba131dSTrond Myklebust xprt_udp_slot_table_entries); 2703c8541ecdSChuck Lever if (IS_ERR(xprt)) 2704c8541ecdSChuck Lever return xprt; 2705c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2706a246b010SChuck Lever 2707ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 2708808012fbSChuck Lever xprt->tsh_size = 0; 2709a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 2710a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2711a246b010SChuck Lever 271203bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 271303bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 271403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2715a246b010SChuck Lever 2716262965f5SChuck Lever xprt->ops = &xs_udp_ops; 2717a246b010SChuck Lever 2718ba7392bbSTrond Myklebust xprt->timeout = &xs_udp_default_timeout; 2719a246b010SChuck Lever 27208f9d5b1aSChuck Lever switch (addr->sa_family) { 27218f9d5b1aSChuck Lever case AF_INET: 27228f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 27238f9d5b1aSChuck Lever xprt_set_bound(xprt); 27248f9d5b1aSChuck Lever 27258f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 27268c14ff2aSPavel Emelyanov xs_udp_setup_socket); 27279dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 27288f9d5b1aSChuck Lever break; 27298f9d5b1aSChuck Lever case AF_INET6: 27308f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 27318f9d5b1aSChuck Lever xprt_set_bound(xprt); 27328f9d5b1aSChuck Lever 27338f9d5b1aSChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 27348c14ff2aSPavel Emelyanov xs_udp_setup_socket); 27359dc3b095SChuck Lever xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 27368f9d5b1aSChuck Lever break; 27378f9d5b1aSChuck Lever default: 27380a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 27390a68b0beSJ. Bruce Fields goto out_err; 27408f9d5b1aSChuck Lever } 27418f9d5b1aSChuck Lever 2742c740eff8SChuck Lever if (xprt_bound(xprt)) 2743c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2744c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2745c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2746c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2747c740eff8SChuck Lever else 2748c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2749c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2750c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2751edb267a6SChuck Lever 2752bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2753c8541ecdSChuck Lever return xprt; 27540a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 27550a68b0beSJ. Bruce Fields out_err: 2756e204e621SPavel Emelyanov xprt_free(xprt); 27570a68b0beSJ. Bruce Fields return ret; 2758a246b010SChuck Lever } 2759a246b010SChuck Lever 27602881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = { 27612881ae74STrond Myklebust .to_initval = 60 * HZ, 27622881ae74STrond Myklebust .to_maxval = 60 * HZ, 27632881ae74STrond Myklebust .to_retries = 2, 27642881ae74STrond Myklebust }; 27652881ae74STrond Myklebust 27669903cd1cSChuck Lever /** 27679903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 276896802a09SFrank van Maarseveen * @args: rpc transport creation arguments 27699903cd1cSChuck Lever * 27709903cd1cSChuck Lever */ 2771483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2772a246b010SChuck Lever { 27738f9d5b1aSChuck Lever struct sockaddr *addr = args->dstaddr; 2774c8541ecdSChuck Lever struct rpc_xprt *xprt; 2775c8475461SChuck Lever struct sock_xprt *transport; 27760a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2777a246b010SChuck Lever 2778d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2779d9ba131dSTrond Myklebust xprt_max_tcp_slot_table_entries); 2780c8541ecdSChuck Lever if (IS_ERR(xprt)) 2781c8541ecdSChuck Lever return xprt; 2782c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 2783a246b010SChuck Lever 2784ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 2785808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2786808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2787a246b010SChuck Lever 278803bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 278903bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 279003bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 2791a246b010SChuck Lever 2792262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 2793ba7392bbSTrond Myklebust xprt->timeout = &xs_tcp_default_timeout; 2794a246b010SChuck Lever 27958f9d5b1aSChuck Lever switch (addr->sa_family) { 27968f9d5b1aSChuck Lever case AF_INET: 27978f9d5b1aSChuck Lever if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 27988f9d5b1aSChuck Lever xprt_set_bound(xprt); 27998f9d5b1aSChuck Lever 28009dc3b095SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 2801cdd518d5SPavel Emelyanov xs_tcp_setup_socket); 28029dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 28038f9d5b1aSChuck Lever break; 28048f9d5b1aSChuck Lever case AF_INET6: 28058f9d5b1aSChuck Lever if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 28068f9d5b1aSChuck Lever xprt_set_bound(xprt); 28078f9d5b1aSChuck Lever 28089dc3b095SChuck Lever INIT_DELAYED_WORK(&transport->connect_worker, 2809cdd518d5SPavel Emelyanov xs_tcp_setup_socket); 28109dc3b095SChuck Lever xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 28118f9d5b1aSChuck Lever break; 28128f9d5b1aSChuck Lever default: 28130a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 28140a68b0beSJ. Bruce Fields goto out_err; 28158f9d5b1aSChuck Lever } 28168f9d5b1aSChuck Lever 2817c740eff8SChuck Lever if (xprt_bound(xprt)) 2818c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2819c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2820c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT], 2821c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2822c740eff8SChuck Lever else 2823c740eff8SChuck Lever dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2824c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR], 2825c740eff8SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO]); 2826c740eff8SChuck Lever 2827edb267a6SChuck Lever 2828bc25571eS\"Talpey, Thomas\ if (try_module_get(THIS_MODULE)) 2829c8541ecdSChuck Lever return xprt; 28300a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 28310a68b0beSJ. Bruce Fields out_err: 2832e204e621SPavel Emelyanov xprt_free(xprt); 28330a68b0beSJ. Bruce Fields return ret; 2834a246b010SChuck Lever } 2835282b32e1SChuck Lever 2836f300babaSAlexandros Batsakis /** 2837f300babaSAlexandros Batsakis * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 2838f300babaSAlexandros Batsakis * @args: rpc transport creation arguments 2839f300babaSAlexandros Batsakis * 2840f300babaSAlexandros Batsakis */ 2841f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 2842f300babaSAlexandros Batsakis { 2843f300babaSAlexandros Batsakis struct sockaddr *addr = args->dstaddr; 2844f300babaSAlexandros Batsakis struct rpc_xprt *xprt; 2845f300babaSAlexandros Batsakis struct sock_xprt *transport; 2846f300babaSAlexandros Batsakis struct svc_sock *bc_sock; 28470a68b0beSJ. Bruce Fields struct rpc_xprt *ret; 2848f300babaSAlexandros Batsakis 2849f0418aa4SJ. Bruce Fields if (args->bc_xprt->xpt_bc_xprt) { 2850f0418aa4SJ. Bruce Fields /* 2851f0418aa4SJ. Bruce Fields * This server connection already has a backchannel 2852f0418aa4SJ. Bruce Fields * export; we can't create a new one, as we wouldn't be 2853f0418aa4SJ. Bruce Fields * able to match replies based on xid any more. So, 2854f0418aa4SJ. Bruce Fields * reuse the already-existing one: 2855f0418aa4SJ. Bruce Fields */ 2856f0418aa4SJ. Bruce Fields return args->bc_xprt->xpt_bc_xprt; 2857f0418aa4SJ. Bruce Fields } 2858d9ba131dSTrond Myklebust xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2859d9ba131dSTrond Myklebust xprt_tcp_slot_table_entries); 2860f300babaSAlexandros Batsakis if (IS_ERR(xprt)) 2861f300babaSAlexandros Batsakis return xprt; 2862f300babaSAlexandros Batsakis transport = container_of(xprt, struct sock_xprt, xprt); 2863f300babaSAlexandros Batsakis 2864f300babaSAlexandros Batsakis xprt->prot = IPPROTO_TCP; 2865f300babaSAlexandros Batsakis xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2866f300babaSAlexandros Batsakis xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2867f300babaSAlexandros Batsakis xprt->timeout = &xs_tcp_default_timeout; 2868f300babaSAlexandros Batsakis 2869f300babaSAlexandros Batsakis /* backchannel */ 2870f300babaSAlexandros Batsakis xprt_set_bound(xprt); 2871f300babaSAlexandros Batsakis xprt->bind_timeout = 0; 2872f300babaSAlexandros Batsakis xprt->reestablish_timeout = 0; 2873f300babaSAlexandros Batsakis xprt->idle_timeout = 0; 2874f300babaSAlexandros Batsakis 2875f300babaSAlexandros Batsakis xprt->ops = &bc_tcp_ops; 2876f300babaSAlexandros Batsakis 2877f300babaSAlexandros Batsakis switch (addr->sa_family) { 2878f300babaSAlexandros Batsakis case AF_INET: 2879f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 2880f300babaSAlexandros Batsakis RPCBIND_NETID_TCP); 2881f300babaSAlexandros Batsakis break; 2882f300babaSAlexandros Batsakis case AF_INET6: 2883f300babaSAlexandros Batsakis xs_format_peer_addresses(xprt, "tcp", 2884f300babaSAlexandros Batsakis RPCBIND_NETID_TCP6); 2885f300babaSAlexandros Batsakis break; 2886f300babaSAlexandros Batsakis default: 28870a68b0beSJ. Bruce Fields ret = ERR_PTR(-EAFNOSUPPORT); 28880a68b0beSJ. Bruce Fields goto out_err; 2889f300babaSAlexandros Batsakis } 2890f300babaSAlexandros Batsakis 2891f300babaSAlexandros Batsakis dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2892f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_ADDR], 2893f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PORT], 2894f300babaSAlexandros Batsakis xprt->address_strings[RPC_DISPLAY_PROTO]); 2895f300babaSAlexandros Batsakis 2896f300babaSAlexandros Batsakis /* 289799de8ea9SJ. Bruce Fields * Once we've associated a backchannel xprt with a connection, 289899de8ea9SJ. Bruce Fields * we want to keep it around as long as long as the connection 289999de8ea9SJ. Bruce Fields * lasts, in case we need to start using it for a backchannel 290099de8ea9SJ. Bruce Fields * again; this reference won't be dropped until bc_xprt is 290199de8ea9SJ. Bruce Fields * destroyed. 290299de8ea9SJ. Bruce Fields */ 290399de8ea9SJ. Bruce Fields xprt_get(xprt); 290499de8ea9SJ. Bruce Fields args->bc_xprt->xpt_bc_xprt = xprt; 290599de8ea9SJ. Bruce Fields xprt->bc_xprt = args->bc_xprt; 290699de8ea9SJ. Bruce Fields bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 290799de8ea9SJ. Bruce Fields transport->sock = bc_sock->sk_sock; 290899de8ea9SJ. Bruce Fields transport->inet = bc_sock->sk_sk; 290999de8ea9SJ. Bruce Fields 291099de8ea9SJ. Bruce Fields /* 2911f300babaSAlexandros Batsakis * Since we don't want connections for the backchannel, we set 2912f300babaSAlexandros Batsakis * the xprt status to connected 2913f300babaSAlexandros Batsakis */ 2914f300babaSAlexandros Batsakis xprt_set_connected(xprt); 2915f300babaSAlexandros Batsakis 2916f300babaSAlexandros Batsakis 2917f300babaSAlexandros Batsakis if (try_module_get(THIS_MODULE)) 2918f300babaSAlexandros Batsakis return xprt; 291999de8ea9SJ. Bruce Fields xprt_put(xprt); 29200a68b0beSJ. Bruce Fields ret = ERR_PTR(-EINVAL); 29210a68b0beSJ. Bruce Fields out_err: 2922e204e621SPavel Emelyanov xprt_free(xprt); 29230a68b0beSJ. Bruce Fields return ret; 2924f300babaSAlexandros Batsakis } 2925f300babaSAlexandros Batsakis 2926176e21eeSChuck Lever static struct xprt_class xs_local_transport = { 2927176e21eeSChuck Lever .list = LIST_HEAD_INIT(xs_local_transport.list), 2928176e21eeSChuck Lever .name = "named UNIX socket", 2929176e21eeSChuck Lever .owner = THIS_MODULE, 2930176e21eeSChuck Lever .ident = XPRT_TRANSPORT_LOCAL, 2931176e21eeSChuck Lever .setup = xs_setup_local, 2932176e21eeSChuck Lever }; 2933176e21eeSChuck Lever 2934bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_udp_transport = { 2935bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_udp_transport.list), 2936bc25571eS\"Talpey, Thomas\ .name = "udp", 2937bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 2938f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_UDP, 2939bc25571eS\"Talpey, Thomas\ .setup = xs_setup_udp, 2940bc25571eS\"Talpey, Thomas\ }; 2941bc25571eS\"Talpey, Thomas\ 2942bc25571eS\"Talpey, Thomas\ static struct xprt_class xs_tcp_transport = { 2943bc25571eS\"Talpey, Thomas\ .list = LIST_HEAD_INIT(xs_tcp_transport.list), 2944bc25571eS\"Talpey, Thomas\ .name = "tcp", 2945bc25571eS\"Talpey, Thomas\ .owner = THIS_MODULE, 2946f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_TCP, 2947bc25571eS\"Talpey, Thomas\ .setup = xs_setup_tcp, 2948bc25571eS\"Talpey, Thomas\ }; 2949bc25571eS\"Talpey, Thomas\ 2950f300babaSAlexandros Batsakis static struct xprt_class xs_bc_tcp_transport = { 2951f300babaSAlexandros Batsakis .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 2952f300babaSAlexandros Batsakis .name = "tcp NFSv4.1 backchannel", 2953f300babaSAlexandros Batsakis .owner = THIS_MODULE, 2954f300babaSAlexandros Batsakis .ident = XPRT_TRANSPORT_BC_TCP, 2955f300babaSAlexandros Batsakis .setup = xs_setup_bc_tcp, 2956f300babaSAlexandros Batsakis }; 2957f300babaSAlexandros Batsakis 2958282b32e1SChuck Lever /** 2959bc25571eS\"Talpey, Thomas\ * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 2960282b32e1SChuck Lever * 2961282b32e1SChuck Lever */ 2962282b32e1SChuck Lever int init_socket_xprt(void) 2963282b32e1SChuck Lever { 2964fbf76683SChuck Lever #ifdef RPC_DEBUG 29652b1bec5fSEric W. Biederman if (!sunrpc_table_header) 29660b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 2967fbf76683SChuck Lever #endif 2968fbf76683SChuck Lever 2969176e21eeSChuck Lever xprt_register_transport(&xs_local_transport); 2970bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_udp_transport); 2971bc25571eS\"Talpey, Thomas\ xprt_register_transport(&xs_tcp_transport); 2972f300babaSAlexandros Batsakis xprt_register_transport(&xs_bc_tcp_transport); 2973bc25571eS\"Talpey, Thomas\ 2974282b32e1SChuck Lever return 0; 2975282b32e1SChuck Lever } 2976282b32e1SChuck Lever 2977282b32e1SChuck Lever /** 2978bc25571eS\"Talpey, Thomas\ * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 2979282b32e1SChuck Lever * 2980282b32e1SChuck Lever */ 2981282b32e1SChuck Lever void cleanup_socket_xprt(void) 2982282b32e1SChuck Lever { 2983fbf76683SChuck Lever #ifdef RPC_DEBUG 2984fbf76683SChuck Lever if (sunrpc_table_header) { 2985fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 2986fbf76683SChuck Lever sunrpc_table_header = NULL; 2987fbf76683SChuck Lever } 2988fbf76683SChuck Lever #endif 2989bc25571eS\"Talpey, Thomas\ 2990176e21eeSChuck Lever xprt_unregister_transport(&xs_local_transport); 2991bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_udp_transport); 2992bc25571eS\"Talpey, Thomas\ xprt_unregister_transport(&xs_tcp_transport); 2993f300babaSAlexandros Batsakis xprt_unregister_transport(&xs_bc_tcp_transport); 2994282b32e1SChuck Lever } 2995cbf11071STrond Myklebust 29969bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val, 29979bbb9e5aSRusty Russell const struct kernel_param *kp, 2998cbf11071STrond Myklebust unsigned int min, unsigned int max) 2999cbf11071STrond Myklebust { 3000cbf11071STrond Myklebust unsigned long num; 3001cbf11071STrond Myklebust int ret; 3002cbf11071STrond Myklebust 3003cbf11071STrond Myklebust if (!val) 3004cbf11071STrond Myklebust return -EINVAL; 3005cbf11071STrond Myklebust ret = strict_strtoul(val, 0, &num); 3006cbf11071STrond Myklebust if (ret == -EINVAL || num < min || num > max) 3007cbf11071STrond Myklebust return -EINVAL; 3008cbf11071STrond Myklebust *((unsigned int *)kp->arg) = num; 3009cbf11071STrond Myklebust return 0; 3010cbf11071STrond Myklebust } 3011cbf11071STrond Myklebust 30129bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp) 3013cbf11071STrond Myklebust { 3014cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3015cbf11071STrond Myklebust RPC_MIN_RESVPORT, 3016cbf11071STrond Myklebust RPC_MAX_RESVPORT); 3017cbf11071STrond Myklebust } 3018cbf11071STrond Myklebust 30199bbb9e5aSRusty Russell static struct kernel_param_ops param_ops_portnr = { 30209bbb9e5aSRusty Russell .set = param_set_portnr, 30219bbb9e5aSRusty Russell .get = param_get_uint, 30229bbb9e5aSRusty Russell }; 30239bbb9e5aSRusty Russell 3024cbf11071STrond Myklebust #define param_check_portnr(name, p) \ 3025cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3026cbf11071STrond Myklebust 3027cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3028cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3029cbf11071STrond Myklebust 30309bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val, 30319bbb9e5aSRusty Russell const struct kernel_param *kp) 3032cbf11071STrond Myklebust { 3033cbf11071STrond Myklebust return param_set_uint_minmax(val, kp, 3034cbf11071STrond Myklebust RPC_MIN_SLOT_TABLE, 3035cbf11071STrond Myklebust RPC_MAX_SLOT_TABLE); 3036cbf11071STrond Myklebust } 3037cbf11071STrond Myklebust 30389bbb9e5aSRusty Russell static struct kernel_param_ops param_ops_slot_table_size = { 30399bbb9e5aSRusty Russell .set = param_set_slot_table_size, 30409bbb9e5aSRusty Russell .get = param_get_uint, 30419bbb9e5aSRusty Russell }; 30429bbb9e5aSRusty Russell 3043cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \ 3044cbf11071STrond Myklebust __param_check(name, p, unsigned int); 3045cbf11071STrond Myklebust 3046d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val, 3047d9ba131dSTrond Myklebust const struct kernel_param *kp) 3048d9ba131dSTrond Myklebust { 3049d9ba131dSTrond Myklebust return param_set_uint_minmax(val, kp, 3050d9ba131dSTrond Myklebust RPC_MIN_SLOT_TABLE, 3051d9ba131dSTrond Myklebust RPC_MAX_SLOT_TABLE_LIMIT); 3052d9ba131dSTrond Myklebust } 3053d9ba131dSTrond Myklebust 3054d9ba131dSTrond Myklebust static struct kernel_param_ops param_ops_max_slot_table_size = { 3055d9ba131dSTrond Myklebust .set = param_set_max_slot_table_size, 3056d9ba131dSTrond Myklebust .get = param_get_uint, 3057d9ba131dSTrond Myklebust }; 3058d9ba131dSTrond Myklebust 3059d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \ 3060d9ba131dSTrond Myklebust __param_check(name, p, unsigned int); 3061d9ba131dSTrond Myklebust 3062cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3063cbf11071STrond Myklebust slot_table_size, 0644); 3064d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3065d9ba131dSTrond Myklebust max_slot_table_size, 0644); 3066cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3067cbf11071STrond Myklebust slot_table_size, 0644); 3068cbf11071STrond Myklebust 3069