1a246b010SChuck Lever /* 2a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 3a246b010SChuck Lever * 4a246b010SChuck Lever * Client-side transport implementation for sockets. 5a246b010SChuck Lever * 6a246b010SChuck Lever * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com> 7a246b010SChuck Lever * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com> 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> 16a246b010SChuck Lever */ 17a246b010SChuck Lever 18a246b010SChuck Lever #include <linux/types.h> 19a246b010SChuck Lever #include <linux/slab.h> 20a246b010SChuck Lever #include <linux/capability.h> 21a246b010SChuck Lever #include <linux/pagemap.h> 22a246b010SChuck Lever #include <linux/errno.h> 23a246b010SChuck Lever #include <linux/socket.h> 24a246b010SChuck Lever #include <linux/in.h> 25a246b010SChuck Lever #include <linux/net.h> 26a246b010SChuck Lever #include <linux/mm.h> 27a246b010SChuck Lever #include <linux/udp.h> 28a246b010SChuck Lever #include <linux/tcp.h> 29a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 3002107148SChuck Lever #include <linux/sunrpc/sched.h> 31a246b010SChuck Lever #include <linux/file.h> 32a246b010SChuck Lever 33a246b010SChuck Lever #include <net/sock.h> 34a246b010SChuck Lever #include <net/checksum.h> 35a246b010SChuck Lever #include <net/udp.h> 36a246b010SChuck Lever #include <net/tcp.h> 37a246b010SChuck Lever 389903cd1cSChuck Lever /* 39c556b754SChuck Lever * xprtsock tunables 40c556b754SChuck Lever */ 41c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 42c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; 43c556b754SChuck Lever 44c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 45c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 46c556b754SChuck Lever 47c556b754SChuck Lever /* 48fbf76683SChuck Lever * We can register our own files under /proc/sys/sunrpc by 49fbf76683SChuck Lever * calling register_sysctl_table() again. The files in that 50fbf76683SChuck Lever * directory become the union of all files registered there. 51fbf76683SChuck Lever * 52fbf76683SChuck Lever * We simply need to make sure that we don't collide with 53fbf76683SChuck Lever * someone else's file names! 54fbf76683SChuck Lever */ 55fbf76683SChuck Lever 56fbf76683SChuck Lever #ifdef RPC_DEBUG 57fbf76683SChuck Lever 58fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 59fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 60fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 61fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 62fbf76683SChuck Lever 63fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header; 64fbf76683SChuck Lever 65fbf76683SChuck Lever /* 66fbf76683SChuck Lever * FIXME: changing the UDP slot table size should also resize the UDP 67fbf76683SChuck Lever * socket buffers for existing UDP transports 68fbf76683SChuck Lever */ 69fbf76683SChuck Lever static ctl_table xs_tunables_table[] = { 70fbf76683SChuck Lever { 71fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_UDP, 72fbf76683SChuck Lever .procname = "udp_slot_table_entries", 73fbf76683SChuck Lever .data = &xprt_udp_slot_table_entries, 74fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 75fbf76683SChuck Lever .mode = 0644, 76fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 77fbf76683SChuck Lever .strategy = &sysctl_intvec, 78fbf76683SChuck Lever .extra1 = &min_slot_table_size, 79fbf76683SChuck Lever .extra2 = &max_slot_table_size 80fbf76683SChuck Lever }, 81fbf76683SChuck Lever { 82fbf76683SChuck Lever .ctl_name = CTL_SLOTTABLE_TCP, 83fbf76683SChuck Lever .procname = "tcp_slot_table_entries", 84fbf76683SChuck Lever .data = &xprt_tcp_slot_table_entries, 85fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 86fbf76683SChuck Lever .mode = 0644, 87fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 88fbf76683SChuck Lever .strategy = &sysctl_intvec, 89fbf76683SChuck Lever .extra1 = &min_slot_table_size, 90fbf76683SChuck Lever .extra2 = &max_slot_table_size 91fbf76683SChuck Lever }, 92fbf76683SChuck Lever { 93fbf76683SChuck Lever .ctl_name = CTL_MIN_RESVPORT, 94fbf76683SChuck Lever .procname = "min_resvport", 95fbf76683SChuck Lever .data = &xprt_min_resvport, 96fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 97fbf76683SChuck Lever .mode = 0644, 98fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 99fbf76683SChuck Lever .strategy = &sysctl_intvec, 100fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 101fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 102fbf76683SChuck Lever }, 103fbf76683SChuck Lever { 104fbf76683SChuck Lever .ctl_name = CTL_MAX_RESVPORT, 105fbf76683SChuck Lever .procname = "max_resvport", 106fbf76683SChuck Lever .data = &xprt_max_resvport, 107fbf76683SChuck Lever .maxlen = sizeof(unsigned int), 108fbf76683SChuck Lever .mode = 0644, 109fbf76683SChuck Lever .proc_handler = &proc_dointvec_minmax, 110fbf76683SChuck Lever .strategy = &sysctl_intvec, 111fbf76683SChuck Lever .extra1 = &xprt_min_resvport_limit, 112fbf76683SChuck Lever .extra2 = &xprt_max_resvport_limit 113fbf76683SChuck Lever }, 114fbf76683SChuck Lever { 115fbf76683SChuck Lever .ctl_name = 0, 116fbf76683SChuck Lever }, 117fbf76683SChuck Lever }; 118fbf76683SChuck Lever 119fbf76683SChuck Lever static ctl_table sunrpc_table[] = { 120fbf76683SChuck Lever { 121fbf76683SChuck Lever .ctl_name = CTL_SUNRPC, 122fbf76683SChuck Lever .procname = "sunrpc", 123fbf76683SChuck Lever .mode = 0555, 124fbf76683SChuck Lever .child = xs_tunables_table 125fbf76683SChuck Lever }, 126fbf76683SChuck Lever { 127fbf76683SChuck Lever .ctl_name = 0, 128fbf76683SChuck Lever }, 129fbf76683SChuck Lever }; 130fbf76683SChuck Lever 131fbf76683SChuck Lever #endif 132fbf76683SChuck Lever 133fbf76683SChuck Lever /* 134262965f5SChuck Lever * How many times to try sending a request on a socket before waiting 135262965f5SChuck Lever * for the socket buffer to clear. 136262965f5SChuck Lever */ 137262965f5SChuck Lever #define XS_SENDMSG_RETRY (10U) 138262965f5SChuck Lever 13903bf4b70SChuck Lever /* 14003bf4b70SChuck Lever * Time out for an RPC UDP socket connect. UDP socket connects are 14103bf4b70SChuck Lever * synchronous, but we set a timeout anyway in case of resource 14203bf4b70SChuck Lever * exhaustion on the local host. 14303bf4b70SChuck Lever */ 14403bf4b70SChuck Lever #define XS_UDP_CONN_TO (5U * HZ) 14503bf4b70SChuck Lever 14603bf4b70SChuck Lever /* 14703bf4b70SChuck Lever * Wait duration for an RPC TCP connection to be established. Solaris 14803bf4b70SChuck Lever * NFS over TCP uses 60 seconds, for example, which is in line with how 14903bf4b70SChuck Lever * long a server takes to reboot. 15003bf4b70SChuck Lever */ 15103bf4b70SChuck Lever #define XS_TCP_CONN_TO (60U * HZ) 15203bf4b70SChuck Lever 15303bf4b70SChuck Lever /* 15403bf4b70SChuck Lever * Wait duration for a reply from the RPC portmapper. 15503bf4b70SChuck Lever */ 15603bf4b70SChuck Lever #define XS_BIND_TO (60U * HZ) 15703bf4b70SChuck Lever 15803bf4b70SChuck Lever /* 15903bf4b70SChuck Lever * Delay if a UDP socket connect error occurs. This is most likely some 16003bf4b70SChuck Lever * kind of resource problem on the local host. 16103bf4b70SChuck Lever */ 16203bf4b70SChuck Lever #define XS_UDP_REEST_TO (2U * HZ) 16303bf4b70SChuck Lever 16403bf4b70SChuck Lever /* 16503bf4b70SChuck Lever * The reestablish timeout allows clients to delay for a bit before attempting 16603bf4b70SChuck Lever * to reconnect to a server that just dropped our connection. 16703bf4b70SChuck Lever * 16803bf4b70SChuck Lever * We implement an exponential backoff when trying to reestablish a TCP 16903bf4b70SChuck Lever * transport connection with the server. Some servers like to drop a TCP 17003bf4b70SChuck Lever * connection when they are overworked, so we start with a short timeout and 17103bf4b70SChuck Lever * increase over time if the server is down or not responding. 17203bf4b70SChuck Lever */ 17303bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO (3U * HZ) 17403bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) 17503bf4b70SChuck Lever 17603bf4b70SChuck Lever /* 17703bf4b70SChuck Lever * TCP idle timeout; client drops the transport socket if it is idle 17803bf4b70SChuck Lever * for this long. Note that we also timeout UDP sockets to prevent 17903bf4b70SChuck Lever * holding port numbers when there is no RPC traffic. 18003bf4b70SChuck Lever */ 18103bf4b70SChuck Lever #define XS_IDLE_DISC_TO (5U * 60 * HZ) 18203bf4b70SChuck Lever 183a246b010SChuck Lever #ifdef RPC_DEBUG 184a246b010SChuck Lever # undef RPC_DEBUG_DATA 1859903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 186a246b010SChuck Lever #endif 187a246b010SChuck Lever 188a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 1899903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 190a246b010SChuck Lever { 191a246b010SChuck Lever u8 *buf = (u8 *) packet; 192a246b010SChuck Lever int j; 193a246b010SChuck Lever 194a246b010SChuck Lever dprintk("RPC: %s\n", msg); 195a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 196a246b010SChuck Lever if (!(j & 31)) { 197a246b010SChuck Lever if (j) 198a246b010SChuck Lever dprintk("\n"); 199a246b010SChuck Lever dprintk("0x%04x ", j); 200a246b010SChuck Lever } 201a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 202a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 203a246b010SChuck Lever } 204a246b010SChuck Lever dprintk("\n"); 205a246b010SChuck Lever } 206a246b010SChuck Lever #else 2079903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 208a246b010SChuck Lever { 209a246b010SChuck Lever /* NOP */ 210a246b010SChuck Lever } 211a246b010SChuck Lever #endif 212a246b010SChuck Lever 213ffc2e518SChuck Lever struct sock_xprt { 214ffc2e518SChuck Lever struct rpc_xprt xprt; 215ee0ac0c2SChuck Lever 216ee0ac0c2SChuck Lever /* 217ee0ac0c2SChuck Lever * Network layer 218ee0ac0c2SChuck Lever */ 219ee0ac0c2SChuck Lever struct socket * sock; 220ee0ac0c2SChuck Lever struct sock * inet; 22151971139SChuck Lever 22251971139SChuck Lever /* 22351971139SChuck Lever * State of TCP reply receive 22451971139SChuck Lever */ 22551971139SChuck Lever __be32 tcp_fraghdr, 22651971139SChuck Lever tcp_xid; 22751971139SChuck Lever 22851971139SChuck Lever u32 tcp_offset, 22951971139SChuck Lever tcp_reclen; 23051971139SChuck Lever 23151971139SChuck Lever unsigned long tcp_copied, 23251971139SChuck Lever tcp_flags; 233c8475461SChuck Lever 234c8475461SChuck Lever /* 235c8475461SChuck Lever * Connection of transports 236c8475461SChuck Lever */ 23734161db6STrond Myklebust struct delayed_work connect_worker; 238d3bc9a1dSFrank van Maarseveen struct sockaddr_storage addr; 239c8475461SChuck Lever unsigned short port; 2407c6e066eSChuck Lever 2417c6e066eSChuck Lever /* 2427c6e066eSChuck Lever * UDP socket buffer size parameters 2437c6e066eSChuck Lever */ 2447c6e066eSChuck Lever size_t rcvsize, 2457c6e066eSChuck Lever sndsize; 246314dfd79SChuck Lever 247314dfd79SChuck Lever /* 248314dfd79SChuck Lever * Saved socket callback addresses 249314dfd79SChuck Lever */ 250314dfd79SChuck Lever void (*old_data_ready)(struct sock *, int); 251314dfd79SChuck Lever void (*old_state_change)(struct sock *); 252314dfd79SChuck Lever void (*old_write_space)(struct sock *); 253ffc2e518SChuck Lever }; 254ffc2e518SChuck Lever 255e136d092SChuck Lever /* 256e136d092SChuck Lever * TCP receive state flags 257e136d092SChuck Lever */ 258e136d092SChuck Lever #define TCP_RCV_LAST_FRAG (1UL << 0) 259e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR (1UL << 1) 260e136d092SChuck Lever #define TCP_RCV_COPY_XID (1UL << 2) 261e136d092SChuck Lever #define TCP_RCV_COPY_DATA (1UL << 3) 262e136d092SChuck Lever 263edb267a6SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt) 264edb267a6SChuck Lever { 265edb267a6SChuck Lever struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr; 266edb267a6SChuck Lever char *buf; 267edb267a6SChuck Lever 268edb267a6SChuck Lever buf = kzalloc(20, GFP_KERNEL); 269edb267a6SChuck Lever if (buf) { 270edb267a6SChuck Lever snprintf(buf, 20, "%u.%u.%u.%u", 271edb267a6SChuck Lever NIPQUAD(addr->sin_addr.s_addr)); 272edb267a6SChuck Lever } 273edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ADDR] = buf; 274edb267a6SChuck Lever 275edb267a6SChuck Lever buf = kzalloc(8, GFP_KERNEL); 276edb267a6SChuck Lever if (buf) { 277edb267a6SChuck Lever snprintf(buf, 8, "%u", 278edb267a6SChuck Lever ntohs(addr->sin_port)); 279edb267a6SChuck Lever } 280edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PORT] = buf; 281edb267a6SChuck Lever 282edb267a6SChuck Lever if (xprt->prot == IPPROTO_UDP) 283edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = "udp"; 284edb267a6SChuck Lever else 285edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp"; 286edb267a6SChuck Lever 287edb267a6SChuck Lever buf = kzalloc(48, GFP_KERNEL); 288edb267a6SChuck Lever if (buf) { 289edb267a6SChuck Lever snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s", 290edb267a6SChuck Lever NIPQUAD(addr->sin_addr.s_addr), 291edb267a6SChuck Lever ntohs(addr->sin_port), 292edb267a6SChuck Lever xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); 293edb267a6SChuck Lever } 294edb267a6SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL] = buf; 295edb267a6SChuck Lever } 296edb267a6SChuck Lever 297edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt) 298edb267a6SChuck Lever { 299edb267a6SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_ADDR]); 300edb267a6SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 301edb267a6SChuck Lever kfree(xprt->address_strings[RPC_DISPLAY_ALL]); 302edb267a6SChuck Lever } 303edb267a6SChuck Lever 304b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 305b4b5cc85SChuck Lever 30624c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 307b4b5cc85SChuck Lever { 308b4b5cc85SChuck Lever struct msghdr msg = { 309b4b5cc85SChuck Lever .msg_name = addr, 310b4b5cc85SChuck Lever .msg_namelen = addrlen, 31124c5684bSTrond Myklebust .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 31224c5684bSTrond Myklebust }; 31324c5684bSTrond Myklebust struct kvec iov = { 31424c5684bSTrond Myklebust .iov_base = vec->iov_base + base, 31524c5684bSTrond Myklebust .iov_len = vec->iov_len - base, 316b4b5cc85SChuck Lever }; 317b4b5cc85SChuck Lever 31824c5684bSTrond Myklebust if (iov.iov_len != 0) 319b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 320b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 321b4b5cc85SChuck Lever } 322b4b5cc85SChuck Lever 32324c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 324b4b5cc85SChuck Lever { 32524c5684bSTrond Myklebust struct page **ppage; 32624c5684bSTrond Myklebust unsigned int remainder; 32724c5684bSTrond Myklebust int err, sent = 0; 328b4b5cc85SChuck Lever 32924c5684bSTrond Myklebust remainder = xdr->page_len - base; 33024c5684bSTrond Myklebust base += xdr->page_base; 33124c5684bSTrond Myklebust ppage = xdr->pages + (base >> PAGE_SHIFT); 33224c5684bSTrond Myklebust base &= ~PAGE_MASK; 33324c5684bSTrond Myklebust for(;;) { 33424c5684bSTrond Myklebust unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 33524c5684bSTrond Myklebust int flags = XS_SENDMSG_FLAGS; 33624c5684bSTrond Myklebust 33724c5684bSTrond Myklebust remainder -= len; 33824c5684bSTrond Myklebust if (remainder != 0 || more) 33924c5684bSTrond Myklebust flags |= MSG_MORE; 34024c5684bSTrond Myklebust err = sock->ops->sendpage(sock, *ppage, base, len, flags); 34124c5684bSTrond Myklebust if (remainder == 0 || err != len) 34224c5684bSTrond Myklebust break; 34324c5684bSTrond Myklebust sent += err; 34424c5684bSTrond Myklebust ppage++; 34524c5684bSTrond Myklebust base = 0; 34624c5684bSTrond Myklebust } 34724c5684bSTrond Myklebust if (sent == 0) 34824c5684bSTrond Myklebust return err; 34924c5684bSTrond Myklebust if (err > 0) 35024c5684bSTrond Myklebust sent += err; 35124c5684bSTrond Myklebust return sent; 352b4b5cc85SChuck Lever } 353b4b5cc85SChuck Lever 3549903cd1cSChuck Lever /** 3559903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 3569903cd1cSChuck Lever * @sock: socket to send on 3579903cd1cSChuck Lever * @addr: UDP only -- address of destination 3589903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 3599903cd1cSChuck Lever * @xdr: buffer containing this request 3609903cd1cSChuck Lever * @base: starting position in the buffer 3619903cd1cSChuck Lever * 362a246b010SChuck Lever */ 36324c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 364a246b010SChuck Lever { 36524c5684bSTrond Myklebust unsigned int remainder = xdr->len - base; 36624c5684bSTrond Myklebust int err, sent = 0; 367a246b010SChuck Lever 368262965f5SChuck Lever if (unlikely(!sock)) 369262965f5SChuck Lever return -ENOTCONN; 370262965f5SChuck Lever 371262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 37224c5684bSTrond Myklebust if (base != 0) { 37324c5684bSTrond Myklebust addr = NULL; 37424c5684bSTrond Myklebust addrlen = 0; 37524c5684bSTrond Myklebust } 376262965f5SChuck Lever 37724c5684bSTrond Myklebust if (base < xdr->head[0].iov_len || addr != NULL) { 37824c5684bSTrond Myklebust unsigned int len = xdr->head[0].iov_len - base; 37924c5684bSTrond Myklebust remainder -= len; 38024c5684bSTrond Myklebust err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 38124c5684bSTrond Myklebust if (remainder == 0 || err != len) 382a246b010SChuck Lever goto out; 38324c5684bSTrond Myklebust sent += err; 384a246b010SChuck Lever base = 0; 385a246b010SChuck Lever } else 38624c5684bSTrond Myklebust base -= xdr->head[0].iov_len; 387a246b010SChuck Lever 38824c5684bSTrond Myklebust if (base < xdr->page_len) { 38924c5684bSTrond Myklebust unsigned int len = xdr->page_len - base; 39024c5684bSTrond Myklebust remainder -= len; 39124c5684bSTrond Myklebust err = xs_send_pagedata(sock, xdr, base, remainder != 0); 39224c5684bSTrond Myklebust if (remainder == 0 || err != len) 393a246b010SChuck Lever goto out; 39424c5684bSTrond Myklebust sent += err; 395a246b010SChuck Lever base = 0; 39624c5684bSTrond Myklebust } else 39724c5684bSTrond Myklebust base -= xdr->page_len; 39824c5684bSTrond Myklebust 39924c5684bSTrond Myklebust if (base >= xdr->tail[0].iov_len) 40024c5684bSTrond Myklebust return sent; 40124c5684bSTrond Myklebust err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 402a246b010SChuck Lever out: 40324c5684bSTrond Myklebust if (sent == 0) 40424c5684bSTrond Myklebust return err; 40524c5684bSTrond Myklebust if (err > 0) 40624c5684bSTrond Myklebust sent += err; 40724c5684bSTrond Myklebust return sent; 408a246b010SChuck Lever } 409a246b010SChuck Lever 4109903cd1cSChuck Lever /** 411262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 412262965f5SChuck Lever * @task: task to put to sleep 4139903cd1cSChuck Lever * 414a246b010SChuck Lever */ 415262965f5SChuck Lever static void xs_nospace(struct rpc_task *task) 416a246b010SChuck Lever { 417262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 418262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 419ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 420a246b010SChuck Lever 42146121cf7SChuck Lever dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 422262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 423262965f5SChuck Lever req->rq_slen); 424a246b010SChuck Lever 425ee0ac0c2SChuck Lever if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { 426262965f5SChuck Lever /* Protect against races with write_space */ 427262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 428a246b010SChuck Lever 429262965f5SChuck Lever /* Don't race with disconnect */ 430262965f5SChuck Lever if (!xprt_connected(xprt)) 431262965f5SChuck Lever task->tk_status = -ENOTCONN; 432ee0ac0c2SChuck Lever else if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 433262965f5SChuck Lever xprt_wait_for_buffer_space(task); 434a246b010SChuck Lever 435262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 436262965f5SChuck Lever } else 437262965f5SChuck Lever /* Keep holding the socket if it is blocked */ 438262965f5SChuck Lever rpc_delay(task, HZ>>4); 439a246b010SChuck Lever } 440a246b010SChuck Lever 4419903cd1cSChuck Lever /** 442262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 4439903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 4449903cd1cSChuck Lever * 4459903cd1cSChuck Lever * Return values: 4469903cd1cSChuck Lever * 0: The request has been sent 4479903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 4489903cd1cSChuck Lever * complete the request 449262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 4509903cd1cSChuck Lever * other: Some other error occured, the request was not sent 4519903cd1cSChuck Lever */ 452262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 453a246b010SChuck Lever { 454a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 455a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 456ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 457262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 458262965f5SChuck Lever int status; 459262965f5SChuck Lever 460262965f5SChuck Lever xs_pktdump("packet data:", 461262965f5SChuck Lever req->rq_svec->iov_base, 462262965f5SChuck Lever req->rq_svec->iov_len); 463262965f5SChuck Lever 464262965f5SChuck Lever req->rq_xtime = jiffies; 465ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 466ee0ac0c2SChuck Lever (struct sockaddr *) &xprt->addr, 467ee0ac0c2SChuck Lever xprt->addrlen, xdr, 468ee0ac0c2SChuck Lever req->rq_bytes_sent); 469262965f5SChuck Lever 470262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 471262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 472262965f5SChuck Lever 473262965f5SChuck Lever if (likely(status >= (int) req->rq_slen)) 474262965f5SChuck Lever return 0; 475262965f5SChuck Lever 476262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 477262965f5SChuck Lever if (status > 0) 478262965f5SChuck Lever status = -EAGAIN; 479262965f5SChuck Lever 480262965f5SChuck Lever switch (status) { 481262965f5SChuck Lever case -ENETUNREACH: 482262965f5SChuck Lever case -EPIPE: 483262965f5SChuck Lever case -ECONNREFUSED: 484262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 485262965f5SChuck Lever * prompts ECONNREFUSED. */ 486262965f5SChuck Lever break; 487262965f5SChuck Lever case -EAGAIN: 488262965f5SChuck Lever xs_nospace(task); 489262965f5SChuck Lever break; 490262965f5SChuck Lever default: 491262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 492262965f5SChuck Lever -status); 493262965f5SChuck Lever break; 494262965f5SChuck Lever } 495262965f5SChuck Lever 496262965f5SChuck Lever return status; 497262965f5SChuck Lever } 498262965f5SChuck Lever 499808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) 500808012fbSChuck Lever { 501808012fbSChuck Lever u32 reclen = buf->len - sizeof(rpc_fraghdr); 502808012fbSChuck Lever rpc_fraghdr *base = buf->head[0].iov_base; 503808012fbSChuck Lever *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); 504808012fbSChuck Lever } 505808012fbSChuck Lever 506262965f5SChuck Lever /** 507262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 508262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 509262965f5SChuck Lever * 510262965f5SChuck Lever * Return values: 511262965f5SChuck Lever * 0: The request has been sent 512262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 513262965f5SChuck Lever * complete the request 514262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 515262965f5SChuck Lever * other: Some other error occured, the request was not sent 516262965f5SChuck Lever * 517262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 518262965f5SChuck Lever * if sendmsg is not able to make progress? 519262965f5SChuck Lever */ 520262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 521262965f5SChuck Lever { 522262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 523262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 524ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 525262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 526a246b010SChuck Lever int status, retry = 0; 527a246b010SChuck Lever 528808012fbSChuck Lever xs_encode_tcp_record_marker(&req->rq_snd_buf); 529262965f5SChuck Lever 530262965f5SChuck Lever xs_pktdump("packet data:", 531262965f5SChuck Lever req->rq_svec->iov_base, 532262965f5SChuck Lever req->rq_svec->iov_len); 533a246b010SChuck Lever 534a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 535a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 536262965f5SChuck Lever * called sendmsg(). */ 537a246b010SChuck Lever while (1) { 538a246b010SChuck Lever req->rq_xtime = jiffies; 539ee0ac0c2SChuck Lever status = xs_sendpages(transport->sock, 540ee0ac0c2SChuck Lever NULL, 0, xdr, req->rq_bytes_sent); 541a246b010SChuck Lever 542262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 543262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 544262965f5SChuck Lever 545262965f5SChuck Lever if (unlikely(status < 0)) 546a246b010SChuck Lever break; 547a246b010SChuck Lever 548a246b010SChuck Lever /* If we've sent the entire packet, immediately 549a246b010SChuck Lever * reset the count of bytes sent. */ 550262965f5SChuck Lever req->rq_bytes_sent += status; 551ef759a2eSChuck Lever task->tk_bytes_sent += status; 552262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 553a246b010SChuck Lever req->rq_bytes_sent = 0; 554a246b010SChuck Lever return 0; 555a246b010SChuck Lever } 556262965f5SChuck Lever 557a246b010SChuck Lever status = -EAGAIN; 558262965f5SChuck Lever if (retry++ > XS_SENDMSG_RETRY) 559a246b010SChuck Lever break; 560a246b010SChuck Lever } 561a246b010SChuck Lever 562262965f5SChuck Lever switch (status) { 563262965f5SChuck Lever case -EAGAIN: 564262965f5SChuck Lever xs_nospace(task); 565262965f5SChuck Lever break; 566262965f5SChuck Lever case -ECONNREFUSED: 567262965f5SChuck Lever case -ECONNRESET: 568262965f5SChuck Lever case -ENOTCONN: 569262965f5SChuck Lever case -EPIPE: 570262965f5SChuck Lever status = -ENOTCONN; 571262965f5SChuck Lever break; 572262965f5SChuck Lever default: 573262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 574262965f5SChuck Lever -status); 57543118c29SChuck Lever xprt_disconnect(xprt); 576a246b010SChuck Lever break; 577a246b010SChuck Lever } 578a246b010SChuck Lever 579a246b010SChuck Lever return status; 580a246b010SChuck Lever } 581a246b010SChuck Lever 5829903cd1cSChuck Lever /** 583e0ab53deSTrond Myklebust * xs_tcp_release_xprt - clean up after a tcp transmission 584e0ab53deSTrond Myklebust * @xprt: transport 585e0ab53deSTrond Myklebust * @task: rpc task 586e0ab53deSTrond Myklebust * 587e0ab53deSTrond Myklebust * This cleans up if an error causes us to abort the transmission of a request. 588e0ab53deSTrond Myklebust * In this case, the socket may need to be reset in order to avoid confusing 589e0ab53deSTrond Myklebust * the server. 590e0ab53deSTrond Myklebust */ 591e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 592e0ab53deSTrond Myklebust { 593e0ab53deSTrond Myklebust struct rpc_rqst *req; 594e0ab53deSTrond Myklebust 595e0ab53deSTrond Myklebust if (task != xprt->snd_task) 596e0ab53deSTrond Myklebust return; 597e0ab53deSTrond Myklebust if (task == NULL) 598e0ab53deSTrond Myklebust goto out_release; 599e0ab53deSTrond Myklebust req = task->tk_rqstp; 600e0ab53deSTrond Myklebust if (req->rq_bytes_sent == 0) 601e0ab53deSTrond Myklebust goto out_release; 602e0ab53deSTrond Myklebust if (req->rq_bytes_sent == req->rq_snd_buf.len) 603e0ab53deSTrond Myklebust goto out_release; 604e0ab53deSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); 605e0ab53deSTrond Myklebust out_release: 606e0ab53deSTrond Myklebust xprt_release_xprt(xprt, task); 607e0ab53deSTrond Myklebust } 608e0ab53deSTrond Myklebust 609e0ab53deSTrond Myklebust /** 6109903cd1cSChuck Lever * xs_close - close a socket 6119903cd1cSChuck Lever * @xprt: transport 6129903cd1cSChuck Lever * 6133167e12cSChuck Lever * This is used when all requests are complete; ie, no DRC state remains 6143167e12cSChuck Lever * on the server we want to save. 615a246b010SChuck Lever */ 6169903cd1cSChuck Lever static void xs_close(struct rpc_xprt *xprt) 617a246b010SChuck Lever { 618ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 619ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 620ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 621a246b010SChuck Lever 622a246b010SChuck Lever if (!sk) 623632e3bdcSTrond Myklebust goto clear_close_wait; 624a246b010SChuck Lever 6259903cd1cSChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 6269903cd1cSChuck Lever 627a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 628ee0ac0c2SChuck Lever transport->inet = NULL; 629ee0ac0c2SChuck Lever transport->sock = NULL; 630a246b010SChuck Lever 631a246b010SChuck Lever sk->sk_user_data = NULL; 632314dfd79SChuck Lever sk->sk_data_ready = transport->old_data_ready; 633314dfd79SChuck Lever sk->sk_state_change = transport->old_state_change; 634314dfd79SChuck Lever sk->sk_write_space = transport->old_write_space; 635a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 636a246b010SChuck Lever 637a246b010SChuck Lever sk->sk_no_check = 0; 638a246b010SChuck Lever 639a246b010SChuck Lever sock_release(sock); 640632e3bdcSTrond Myklebust clear_close_wait: 641632e3bdcSTrond Myklebust smp_mb__before_clear_bit(); 642632e3bdcSTrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 643632e3bdcSTrond Myklebust smp_mb__after_clear_bit(); 644a246b010SChuck Lever } 645a246b010SChuck Lever 6469903cd1cSChuck Lever /** 6479903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 6489903cd1cSChuck Lever * @xprt: doomed transport 6499903cd1cSChuck Lever * 6509903cd1cSChuck Lever */ 6519903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 652a246b010SChuck Lever { 653c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 654c8475461SChuck Lever 6559903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 6569903cd1cSChuck Lever 657c1384c9cSTrond Myklebust cancel_rearming_delayed_work(&transport->connect_worker); 658a246b010SChuck Lever 659a246b010SChuck Lever xprt_disconnect(xprt); 6609903cd1cSChuck Lever xs_close(xprt); 661edb267a6SChuck Lever xs_free_peer_addresses(xprt); 662a246b010SChuck Lever kfree(xprt->slot); 663c8541ecdSChuck Lever kfree(xprt); 664a246b010SChuck Lever } 665a246b010SChuck Lever 6669903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 6679903cd1cSChuck Lever { 6689903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 6699903cd1cSChuck Lever } 6709903cd1cSChuck Lever 6719903cd1cSChuck Lever /** 6729903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 6739903cd1cSChuck Lever * @sk: socket with data to read 6749903cd1cSChuck Lever * @len: how much data to read 6759903cd1cSChuck Lever * 676a246b010SChuck Lever */ 6779903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 678a246b010SChuck Lever { 679a246b010SChuck Lever struct rpc_task *task; 680a246b010SChuck Lever struct rpc_xprt *xprt; 681a246b010SChuck Lever struct rpc_rqst *rovr; 682a246b010SChuck Lever struct sk_buff *skb; 683a246b010SChuck Lever int err, repsize, copied; 684d8ed029dSAlexey Dobriyan u32 _xid; 685d8ed029dSAlexey Dobriyan __be32 *xp; 686a246b010SChuck Lever 687a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 6889903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 6899903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 690a246b010SChuck Lever goto out; 691a246b010SChuck Lever 692a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 693a246b010SChuck Lever goto out; 694a246b010SChuck Lever 695a246b010SChuck Lever if (xprt->shutdown) 696a246b010SChuck Lever goto dropit; 697a246b010SChuck Lever 698a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 699a246b010SChuck Lever if (repsize < 4) { 7009903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 701a246b010SChuck Lever goto dropit; 702a246b010SChuck Lever } 703a246b010SChuck Lever 704a246b010SChuck Lever /* Copy the XID from the skb... */ 705a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 706a246b010SChuck Lever sizeof(_xid), &_xid); 707a246b010SChuck Lever if (xp == NULL) 708a246b010SChuck Lever goto dropit; 709a246b010SChuck Lever 710a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 7114a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 712a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 713a246b010SChuck Lever if (!rovr) 714a246b010SChuck Lever goto out_unlock; 715a246b010SChuck Lever task = rovr->rq_task; 716a246b010SChuck Lever 717a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 718a246b010SChuck Lever copied = repsize; 719a246b010SChuck Lever 720a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 721a246b010SChuck Lever if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) 722a246b010SChuck Lever goto out_unlock; 723a246b010SChuck Lever 724a246b010SChuck Lever /* Something worked... */ 725a246b010SChuck Lever dst_confirm(skb->dst); 726a246b010SChuck Lever 7271570c1e4SChuck Lever xprt_adjust_cwnd(task, copied); 7281570c1e4SChuck Lever xprt_update_rtt(task); 7291570c1e4SChuck Lever xprt_complete_rqst(task, copied); 730a246b010SChuck Lever 731a246b010SChuck Lever out_unlock: 7324a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 733a246b010SChuck Lever dropit: 734a246b010SChuck Lever skb_free_datagram(sk, skb); 735a246b010SChuck Lever out: 736a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 737a246b010SChuck Lever } 738a246b010SChuck Lever 739dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 740a246b010SChuck Lever { 74151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 742a246b010SChuck Lever size_t len, used; 743a246b010SChuck Lever char *p; 744a246b010SChuck Lever 74551971139SChuck Lever p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; 74651971139SChuck Lever len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; 7479d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 74851971139SChuck Lever transport->tcp_offset += used; 749a246b010SChuck Lever if (used != len) 750a246b010SChuck Lever return; 751808012fbSChuck Lever 75251971139SChuck Lever transport->tcp_reclen = ntohl(transport->tcp_fraghdr); 75351971139SChuck Lever if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) 754e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_LAST_FRAG; 755a246b010SChuck Lever else 756e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; 75751971139SChuck Lever transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; 758808012fbSChuck Lever 759e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; 76051971139SChuck Lever transport->tcp_offset = 0; 761808012fbSChuck Lever 762a246b010SChuck Lever /* Sanity check of the record length */ 76351971139SChuck Lever if (unlikely(transport->tcp_reclen < 4)) { 7649903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 765a246b010SChuck Lever xprt_disconnect(xprt); 7669903cd1cSChuck Lever return; 767a246b010SChuck Lever } 768a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 76951971139SChuck Lever transport->tcp_reclen); 770a246b010SChuck Lever } 771a246b010SChuck Lever 77251971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport) 773a246b010SChuck Lever { 77451971139SChuck Lever if (transport->tcp_offset == transport->tcp_reclen) { 775e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; 77651971139SChuck Lever transport->tcp_offset = 0; 777e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { 778e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 779e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_XID; 78051971139SChuck Lever transport->tcp_copied = 0; 781a246b010SChuck Lever } 782a246b010SChuck Lever } 783a246b010SChuck Lever } 784a246b010SChuck Lever 785dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) 786a246b010SChuck Lever { 787a246b010SChuck Lever size_t len, used; 788a246b010SChuck Lever char *p; 789a246b010SChuck Lever 79051971139SChuck Lever len = sizeof(transport->tcp_xid) - transport->tcp_offset; 791a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 79251971139SChuck Lever p = ((char *) &transport->tcp_xid) + transport->tcp_offset; 7939d292316SChuck Lever used = xdr_skb_read_bits(desc, p, len); 79451971139SChuck Lever transport->tcp_offset += used; 795a246b010SChuck Lever if (used != len) 796a246b010SChuck Lever return; 797e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_XID; 798e136d092SChuck Lever transport->tcp_flags |= TCP_RCV_COPY_DATA; 79951971139SChuck Lever transport->tcp_copied = 4; 800a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 80151971139SChuck Lever ntohl(transport->tcp_xid)); 80251971139SChuck Lever xs_tcp_check_fraghdr(transport); 803a246b010SChuck Lever } 804a246b010SChuck Lever 805dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) 806a246b010SChuck Lever { 80751971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 808a246b010SChuck Lever struct rpc_rqst *req; 809a246b010SChuck Lever struct xdr_buf *rcvbuf; 810a246b010SChuck Lever size_t len; 811a246b010SChuck Lever ssize_t r; 812a246b010SChuck Lever 813a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 8144a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 81551971139SChuck Lever req = xprt_lookup_rqst(xprt, transport->tcp_xid); 816a246b010SChuck Lever if (!req) { 817e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 818a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 81951971139SChuck Lever ntohl(transport->tcp_xid)); 8204a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 821a246b010SChuck Lever return; 822a246b010SChuck Lever } 823a246b010SChuck Lever 824a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 825a246b010SChuck Lever len = desc->count; 82651971139SChuck Lever if (len > transport->tcp_reclen - transport->tcp_offset) { 827dd456471SChuck Lever struct xdr_skb_reader my_desc; 828a246b010SChuck Lever 82951971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 830a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 831a246b010SChuck Lever my_desc.count = len; 83251971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 8339d292316SChuck Lever &my_desc, xdr_skb_read_bits); 834a246b010SChuck Lever desc->count -= r; 835a246b010SChuck Lever desc->offset += r; 836a246b010SChuck Lever } else 83751971139SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, 8389d292316SChuck Lever desc, xdr_skb_read_bits); 839a246b010SChuck Lever 840a246b010SChuck Lever if (r > 0) { 84151971139SChuck Lever transport->tcp_copied += r; 84251971139SChuck Lever transport->tcp_offset += r; 843a246b010SChuck Lever } 844a246b010SChuck Lever if (r != len) { 845a246b010SChuck Lever /* Error when copying to the receive buffer, 846a246b010SChuck Lever * usually because we weren't able to allocate 847a246b010SChuck Lever * additional buffer pages. All we can do now 848e136d092SChuck Lever * is turn off TCP_RCV_COPY_DATA, so the request 849a246b010SChuck Lever * will not receive any additional updates, 850a246b010SChuck Lever * and time out. 851a246b010SChuck Lever * Any remaining data from this record will 852a246b010SChuck Lever * be discarded. 853a246b010SChuck Lever */ 854e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 855a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 85651971139SChuck Lever ntohl(transport->tcp_xid)); 85746121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, " 85846121cf7SChuck Lever "tcp_offset = %u, tcp_reclen = %u\n", 85946121cf7SChuck Lever xprt, transport->tcp_copied, 86046121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 861a246b010SChuck Lever goto out; 862a246b010SChuck Lever } 863a246b010SChuck Lever 864a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 86551971139SChuck Lever ntohl(transport->tcp_xid), r); 86646121cf7SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " 86746121cf7SChuck Lever "tcp_reclen = %u\n", xprt, transport->tcp_copied, 86846121cf7SChuck Lever transport->tcp_offset, transport->tcp_reclen); 869a246b010SChuck Lever 87051971139SChuck Lever if (transport->tcp_copied == req->rq_private_buf.buflen) 871e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 87251971139SChuck Lever else if (transport->tcp_offset == transport->tcp_reclen) { 873e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_LAST_FRAG) 874e136d092SChuck Lever transport->tcp_flags &= ~TCP_RCV_COPY_DATA; 875a246b010SChuck Lever } 876a246b010SChuck Lever 877a246b010SChuck Lever out: 878e136d092SChuck Lever if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) 87951971139SChuck Lever xprt_complete_rqst(req->rq_task, transport->tcp_copied); 8804a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 88151971139SChuck Lever xs_tcp_check_fraghdr(transport); 882a246b010SChuck Lever } 883a246b010SChuck Lever 884dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) 885a246b010SChuck Lever { 886a246b010SChuck Lever size_t len; 887a246b010SChuck Lever 88851971139SChuck Lever len = transport->tcp_reclen - transport->tcp_offset; 889a246b010SChuck Lever if (len > desc->count) 890a246b010SChuck Lever len = desc->count; 891a246b010SChuck Lever desc->count -= len; 892a246b010SChuck Lever desc->offset += len; 89351971139SChuck Lever transport->tcp_offset += len; 894a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 89551971139SChuck Lever xs_tcp_check_fraghdr(transport); 896a246b010SChuck Lever } 897a246b010SChuck Lever 8989903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 899a246b010SChuck Lever { 900a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 90151971139SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 902dd456471SChuck Lever struct xdr_skb_reader desc = { 903a246b010SChuck Lever .skb = skb, 904a246b010SChuck Lever .offset = offset, 905a246b010SChuck Lever .count = len, 906a246b010SChuck Lever }; 907a246b010SChuck Lever 9089903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 909a246b010SChuck Lever do { 910a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 911a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 912e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { 9139903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 914a246b010SChuck Lever continue; 915a246b010SChuck Lever } 916a246b010SChuck Lever /* Read in the xid if necessary */ 917e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_XID) { 91851971139SChuck Lever xs_tcp_read_xid(transport, &desc); 919a246b010SChuck Lever continue; 920a246b010SChuck Lever } 921a246b010SChuck Lever /* Read in the request data */ 922e136d092SChuck Lever if (transport->tcp_flags & TCP_RCV_COPY_DATA) { 9239903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 924a246b010SChuck Lever continue; 925a246b010SChuck Lever } 926a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 92751971139SChuck Lever xs_tcp_read_discard(transport, &desc); 928a246b010SChuck Lever } while (desc.count); 9299903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 930a246b010SChuck Lever return len - desc.count; 931a246b010SChuck Lever } 932a246b010SChuck Lever 9339903cd1cSChuck Lever /** 9349903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 9359903cd1cSChuck Lever * @sk: socket with data to read 9369903cd1cSChuck Lever * @bytes: how much data to read 9379903cd1cSChuck Lever * 9389903cd1cSChuck Lever */ 9399903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 940a246b010SChuck Lever { 941a246b010SChuck Lever struct rpc_xprt *xprt; 942a246b010SChuck Lever read_descriptor_t rd_desc; 943a246b010SChuck Lever 9449903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 94546121cf7SChuck Lever 94646121cf7SChuck Lever read_lock(&sk->sk_callback_lock); 9479903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 948a246b010SChuck Lever goto out; 949a246b010SChuck Lever if (xprt->shutdown) 950a246b010SChuck Lever goto out; 951a246b010SChuck Lever 9529903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 953a246b010SChuck Lever rd_desc.arg.data = xprt; 954a246b010SChuck Lever rd_desc.count = 65536; 9559903cd1cSChuck Lever tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 956a246b010SChuck Lever out: 957a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 958a246b010SChuck Lever } 959a246b010SChuck Lever 9609903cd1cSChuck Lever /** 9619903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 9629903cd1cSChuck Lever * @sk: socket whose state has changed 9639903cd1cSChuck Lever * 9649903cd1cSChuck Lever */ 9659903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 966a246b010SChuck Lever { 967a246b010SChuck Lever struct rpc_xprt *xprt; 968a246b010SChuck Lever 969a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 970a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 971a246b010SChuck Lever goto out; 9729903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 973a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 974a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 975a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 976a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 977a246b010SChuck Lever 978a246b010SChuck Lever switch (sk->sk_state) { 979a246b010SChuck Lever case TCP_ESTABLISHED: 9804a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 981a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 98251971139SChuck Lever struct sock_xprt *transport = container_of(xprt, 98351971139SChuck Lever struct sock_xprt, xprt); 98451971139SChuck Lever 985a246b010SChuck Lever /* Reset TCP record info */ 98651971139SChuck Lever transport->tcp_offset = 0; 98751971139SChuck Lever transport->tcp_reclen = 0; 98851971139SChuck Lever transport->tcp_copied = 0; 989e136d092SChuck Lever transport->tcp_flags = 990e136d092SChuck Lever TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 99151971139SChuck Lever 99203bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 99344fbac22SChuck Lever xprt_wake_pending_tasks(xprt, 0); 994a246b010SChuck Lever } 9954a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 996a246b010SChuck Lever break; 997a246b010SChuck Lever case TCP_SYN_SENT: 998a246b010SChuck Lever case TCP_SYN_RECV: 999a246b010SChuck Lever break; 1000632e3bdcSTrond Myklebust case TCP_CLOSE_WAIT: 1001632e3bdcSTrond Myklebust /* Try to schedule an autoclose RPC calls */ 1002632e3bdcSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 1003632e3bdcSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 1004c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 1005a246b010SChuck Lever default: 1006a246b010SChuck Lever xprt_disconnect(xprt); 1007a246b010SChuck Lever } 1008a246b010SChuck Lever out: 1009a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1010a246b010SChuck Lever } 1011a246b010SChuck Lever 10129903cd1cSChuck Lever /** 1013c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 1014c7b2cae8SChuck Lever * becomes available 10159903cd1cSChuck Lever * @sk: socket whose state has changed 10169903cd1cSChuck Lever * 1017a246b010SChuck Lever * Called when more output buffer space is available for this socket. 1018a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 1019c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1020a246b010SChuck Lever * with a bunch of small requests. 1021a246b010SChuck Lever */ 1022c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 1023a246b010SChuck Lever { 1024a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 1025c7b2cae8SChuck Lever 1026c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 1027c7b2cae8SChuck Lever if (sock_writeable(sk)) { 1028c7b2cae8SChuck Lever struct socket *sock; 1029c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1030c7b2cae8SChuck Lever 1031c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1032a246b010SChuck Lever goto out; 1033c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1034c7b2cae8SChuck Lever goto out; 1035c7b2cae8SChuck Lever if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) 1036a246b010SChuck Lever goto out; 1037a246b010SChuck Lever 1038c7b2cae8SChuck Lever xprt_write_space(xprt); 1039a246b010SChuck Lever } 1040a246b010SChuck Lever 1041c7b2cae8SChuck Lever out: 1042c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 1043c7b2cae8SChuck Lever } 1044c7b2cae8SChuck Lever 1045c7b2cae8SChuck Lever /** 1046c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 1047c7b2cae8SChuck Lever * becomes available 1048c7b2cae8SChuck Lever * @sk: socket whose state has changed 1049c7b2cae8SChuck Lever * 1050c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 1051c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 1052c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1053c7b2cae8SChuck Lever * with a bunch of small requests. 1054c7b2cae8SChuck Lever */ 1055c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 1056c7b2cae8SChuck Lever { 1057c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 1058c7b2cae8SChuck Lever 1059c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 1060c7b2cae8SChuck Lever if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 1061c7b2cae8SChuck Lever struct socket *sock; 1062c7b2cae8SChuck Lever struct rpc_xprt *xprt; 1063c7b2cae8SChuck Lever 1064c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 1065c7b2cae8SChuck Lever goto out; 1066c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 1067c7b2cae8SChuck Lever goto out; 1068c7b2cae8SChuck Lever if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) 1069a246b010SChuck Lever goto out; 1070a246b010SChuck Lever 1071c7b2cae8SChuck Lever xprt_write_space(xprt); 1072c7b2cae8SChuck Lever } 1073c7b2cae8SChuck Lever 1074a246b010SChuck Lever out: 1075a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 1076a246b010SChuck Lever } 1077a246b010SChuck Lever 1078470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1079a246b010SChuck Lever { 1080ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1081ee0ac0c2SChuck Lever struct sock *sk = transport->inet; 1082a246b010SChuck Lever 10837c6e066eSChuck Lever if (transport->rcvsize) { 1084a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 10857c6e066eSChuck Lever sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1086a246b010SChuck Lever } 10877c6e066eSChuck Lever if (transport->sndsize) { 1088a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 10897c6e066eSChuck Lever sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1090a246b010SChuck Lever sk->sk_write_space(sk); 1091a246b010SChuck Lever } 1092a246b010SChuck Lever } 1093a246b010SChuck Lever 109443118c29SChuck Lever /** 1095470056c2SChuck Lever * xs_udp_set_buffer_size - set send and receive limits 109643118c29SChuck Lever * @xprt: generic transport 1097470056c2SChuck Lever * @sndsize: requested size of send buffer, in bytes 1098470056c2SChuck Lever * @rcvsize: requested size of receive buffer, in bytes 109943118c29SChuck Lever * 1100470056c2SChuck Lever * Set socket send and receive buffer size limits. 110143118c29SChuck Lever */ 1102470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 110343118c29SChuck Lever { 11047c6e066eSChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 11057c6e066eSChuck Lever 11067c6e066eSChuck Lever transport->sndsize = 0; 1107470056c2SChuck Lever if (sndsize) 11087c6e066eSChuck Lever transport->sndsize = sndsize + 1024; 11097c6e066eSChuck Lever transport->rcvsize = 0; 1110470056c2SChuck Lever if (rcvsize) 11117c6e066eSChuck Lever transport->rcvsize = rcvsize + 1024; 1112470056c2SChuck Lever 1113470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 111443118c29SChuck Lever } 111543118c29SChuck Lever 111646c0ee8bSChuck Lever /** 111746c0ee8bSChuck Lever * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 111846c0ee8bSChuck Lever * @task: task that timed out 111946c0ee8bSChuck Lever * 112046c0ee8bSChuck Lever * Adjust the congestion window after a retransmit timeout has occurred. 112146c0ee8bSChuck Lever */ 112246c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task) 112346c0ee8bSChuck Lever { 112446c0ee8bSChuck Lever xprt_adjust_cwnd(task, -ETIMEDOUT); 112546c0ee8bSChuck Lever } 112646c0ee8bSChuck Lever 1127b85d8806SChuck Lever static unsigned short xs_get_random_port(void) 1128b85d8806SChuck Lever { 1129b85d8806SChuck Lever unsigned short range = xprt_max_resvport - xprt_min_resvport; 1130b85d8806SChuck Lever unsigned short rand = (unsigned short) net_random() % range; 1131b85d8806SChuck Lever return rand + xprt_min_resvport; 1132b85d8806SChuck Lever } 1133b85d8806SChuck Lever 113492200412SChuck Lever /** 113592200412SChuck Lever * xs_set_port - reset the port number in the remote endpoint address 113692200412SChuck Lever * @xprt: generic transport 113792200412SChuck Lever * @port: new port number 113892200412SChuck Lever * 113992200412SChuck Lever */ 114092200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 114192200412SChuck Lever { 1142c4efcb1dSChuck Lever struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr; 1143c4efcb1dSChuck Lever 114492200412SChuck Lever dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1145c4efcb1dSChuck Lever 1146c4efcb1dSChuck Lever sap->sin_port = htons(port); 114792200412SChuck Lever } 114892200412SChuck Lever 1149d3bc9a1dSFrank van Maarseveen static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1150a246b010SChuck Lever { 1151a246b010SChuck Lever struct sockaddr_in myaddr = { 1152a246b010SChuck Lever .sin_family = AF_INET, 1153a246b010SChuck Lever }; 1154d3bc9a1dSFrank van Maarseveen struct sockaddr_in *sa; 1155529b33c6SChuck Lever int err; 1156c8475461SChuck Lever unsigned short port = transport->port; 1157a246b010SChuck Lever 1158d3bc9a1dSFrank van Maarseveen if (!transport->xprt.resvport) 1159d3bc9a1dSFrank van Maarseveen port = 0; 1160d3bc9a1dSFrank van Maarseveen sa = (struct sockaddr_in *)&transport->addr; 1161d3bc9a1dSFrank van Maarseveen myaddr.sin_addr = sa->sin_addr; 1162a246b010SChuck Lever do { 1163a246b010SChuck Lever myaddr.sin_port = htons(port); 1164e6242e92SSridhar Samudrala err = kernel_bind(sock, (struct sockaddr *) &myaddr, 1165a246b010SChuck Lever sizeof(myaddr)); 1166d3bc9a1dSFrank van Maarseveen if (!transport->xprt.resvport) 1167d3bc9a1dSFrank van Maarseveen break; 1168a246b010SChuck Lever if (err == 0) { 1169c8475461SChuck Lever transport->port = port; 1170d3bc9a1dSFrank van Maarseveen break; 1171a246b010SChuck Lever } 1172529b33c6SChuck Lever if (port <= xprt_min_resvport) 1173529b33c6SChuck Lever port = xprt_max_resvport; 1174529b33c6SChuck Lever else 1175529b33c6SChuck Lever port--; 1176c8475461SChuck Lever } while (err == -EADDRINUSE && port != transport->port); 1177d3bc9a1dSFrank van Maarseveen dprintk("RPC: xs_bind "NIPQUAD_FMT":%u: %s (%d)\n", 1178d3bc9a1dSFrank van Maarseveen NIPQUAD(myaddr.sin_addr), port, err ? "failed" : "ok", err); 1179a246b010SChuck Lever return err; 1180a246b010SChuck Lever } 1181a246b010SChuck Lever 1182ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 1183ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2]; 1184ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2]; 1185ed07536eSPeter Zijlstra 1186ed07536eSPeter Zijlstra static inline void xs_reclassify_socket(struct socket *sock) 1187ed07536eSPeter Zijlstra { 1188ed07536eSPeter Zijlstra struct sock *sk = sock->sk; 1189*02b3d346SJohn Heffner BUG_ON(sock_owned_by_user(sk)); 1190ed07536eSPeter Zijlstra switch (sk->sk_family) { 1191ed07536eSPeter Zijlstra case AF_INET: 1192ed07536eSPeter Zijlstra sock_lock_init_class_and_name(sk, "slock-AF_INET-NFS", 1193ed07536eSPeter Zijlstra &xs_slock_key[0], "sk_lock-AF_INET-NFS", &xs_key[0]); 1194ed07536eSPeter Zijlstra break; 1195ed07536eSPeter Zijlstra 1196ed07536eSPeter Zijlstra case AF_INET6: 1197ed07536eSPeter Zijlstra sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFS", 1198ed07536eSPeter Zijlstra &xs_slock_key[1], "sk_lock-AF_INET6-NFS", &xs_key[1]); 1199ed07536eSPeter Zijlstra break; 1200ed07536eSPeter Zijlstra 1201ed07536eSPeter Zijlstra default: 1202ed07536eSPeter Zijlstra BUG(); 1203ed07536eSPeter Zijlstra } 1204ed07536eSPeter Zijlstra } 1205ed07536eSPeter Zijlstra #else 1206ed07536eSPeter Zijlstra static inline void xs_reclassify_socket(struct socket *sock) 1207ed07536eSPeter Zijlstra { 1208ed07536eSPeter Zijlstra } 1209ed07536eSPeter Zijlstra #endif 1210ed07536eSPeter Zijlstra 12119903cd1cSChuck Lever /** 1212b0d93ad5SChuck Lever * xs_udp_connect_worker - set up a UDP socket 121365f27f38SDavid Howells * @work: RPC transport to connect 12149903cd1cSChuck Lever * 12159903cd1cSChuck Lever * Invoked by a work queue tasklet. 1216a246b010SChuck Lever */ 121765f27f38SDavid Howells static void xs_udp_connect_worker(struct work_struct *work) 1218a246b010SChuck Lever { 121934161db6STrond Myklebust struct sock_xprt *transport = 122034161db6STrond Myklebust container_of(work, struct sock_xprt, connect_worker.work); 1221c8475461SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1222ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1223b0d93ad5SChuck Lever int err, status = -EIO; 1224a246b010SChuck Lever 1225ec739ef0SChuck Lever if (xprt->shutdown || !xprt_bound(xprt)) 1226a246b010SChuck Lever goto out; 1227a246b010SChuck Lever 1228b0d93ad5SChuck Lever /* Start by resetting any existing state */ 12299903cd1cSChuck Lever xs_close(xprt); 1230b0d93ad5SChuck Lever 1231b0d93ad5SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { 1232b0d93ad5SChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 1233a246b010SChuck Lever goto out; 1234a246b010SChuck Lever } 1235ed07536eSPeter Zijlstra xs_reclassify_socket(sock); 1236a246b010SChuck Lever 1237d3bc9a1dSFrank van Maarseveen if (xs_bind(transport, sock)) { 1238b0d93ad5SChuck Lever sock_release(sock); 1239b0d93ad5SChuck Lever goto out; 1240b0d93ad5SChuck Lever } 1241b0d93ad5SChuck Lever 1242edb267a6SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 12437559c7a2SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1244edb267a6SChuck Lever 1245ee0ac0c2SChuck Lever if (!transport->inet) { 1246b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1247b0d93ad5SChuck Lever 1248b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1249b0d93ad5SChuck Lever 1250b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1251314dfd79SChuck Lever transport->old_data_ready = sk->sk_data_ready; 1252314dfd79SChuck Lever transport->old_state_change = sk->sk_state_change; 1253314dfd79SChuck Lever transport->old_write_space = sk->sk_write_space; 1254b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 1255b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 1256b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 1257b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 1258b0d93ad5SChuck Lever 1259b0d93ad5SChuck Lever xprt_set_connected(xprt); 1260b0d93ad5SChuck Lever 1261b0d93ad5SChuck Lever /* Reset to new socket */ 1262ee0ac0c2SChuck Lever transport->sock = sock; 1263ee0ac0c2SChuck Lever transport->inet = sk; 1264b0d93ad5SChuck Lever 1265b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1266b0d93ad5SChuck Lever } 1267470056c2SChuck Lever xs_udp_do_set_buffer_size(xprt); 1268a246b010SChuck Lever status = 0; 1269b0d93ad5SChuck Lever out: 1270b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 1271b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 1272b0d93ad5SChuck Lever } 1273b0d93ad5SChuck Lever 12743167e12cSChuck Lever /* 12753167e12cSChuck Lever * We need to preserve the port number so the reply cache on the server can 12763167e12cSChuck Lever * find our cached RPC replies when we get around to reconnecting. 12773167e12cSChuck Lever */ 12783167e12cSChuck Lever static void xs_tcp_reuse_connection(struct rpc_xprt *xprt) 12793167e12cSChuck Lever { 12803167e12cSChuck Lever int result; 1281ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 12823167e12cSChuck Lever struct sockaddr any; 12833167e12cSChuck Lever 12843167e12cSChuck Lever dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); 12853167e12cSChuck Lever 12863167e12cSChuck Lever /* 12873167e12cSChuck Lever * Disconnect the transport socket by doing a connect operation 12883167e12cSChuck Lever * with AF_UNSPEC. This should return immediately... 12893167e12cSChuck Lever */ 12903167e12cSChuck Lever memset(&any, 0, sizeof(any)); 12913167e12cSChuck Lever any.sa_family = AF_UNSPEC; 1292ee0ac0c2SChuck Lever result = kernel_connect(transport->sock, &any, sizeof(any), 0); 12933167e12cSChuck Lever if (result) 12943167e12cSChuck Lever dprintk("RPC: AF_UNSPEC connect return code %d\n", 12953167e12cSChuck Lever result); 12963167e12cSChuck Lever } 12973167e12cSChuck Lever 1298b0d93ad5SChuck Lever /** 1299b0d93ad5SChuck Lever * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint 130065f27f38SDavid Howells * @work: RPC transport to connect 1301b0d93ad5SChuck Lever * 1302b0d93ad5SChuck Lever * Invoked by a work queue tasklet. 1303b0d93ad5SChuck Lever */ 130465f27f38SDavid Howells static void xs_tcp_connect_worker(struct work_struct *work) 1305b0d93ad5SChuck Lever { 130634161db6STrond Myklebust struct sock_xprt *transport = 130734161db6STrond Myklebust container_of(work, struct sock_xprt, connect_worker.work); 1308c8475461SChuck Lever struct rpc_xprt *xprt = &transport->xprt; 1309ee0ac0c2SChuck Lever struct socket *sock = transport->sock; 1310b0d93ad5SChuck Lever int err, status = -EIO; 1311b0d93ad5SChuck Lever 1312ec739ef0SChuck Lever if (xprt->shutdown || !xprt_bound(xprt)) 1313a246b010SChuck Lever goto out; 1314a246b010SChuck Lever 1315ee0ac0c2SChuck Lever if (!sock) { 13163167e12cSChuck Lever /* start from scratch */ 1317b0d93ad5SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 131846121cf7SChuck Lever dprintk("RPC: can't create TCP transport " 131946121cf7SChuck Lever "socket (%d).\n", -err); 1320b0d93ad5SChuck Lever goto out; 1321b0d93ad5SChuck Lever } 1322ed07536eSPeter Zijlstra xs_reclassify_socket(sock); 1323b0d93ad5SChuck Lever 1324d3bc9a1dSFrank van Maarseveen if (xs_bind(transport, sock)) { 1325b0d93ad5SChuck Lever sock_release(sock); 1326b0d93ad5SChuck Lever goto out; 1327b0d93ad5SChuck Lever } 13283167e12cSChuck Lever } else 13293167e12cSChuck Lever /* "close" the socket, preserving the local port */ 13303167e12cSChuck Lever xs_tcp_reuse_connection(xprt); 1331b0d93ad5SChuck Lever 1332edb267a6SChuck Lever dprintk("RPC: worker connecting xprt %p to address: %s\n", 13337559c7a2SChuck Lever xprt, xprt->address_strings[RPC_DISPLAY_ALL]); 1334edb267a6SChuck Lever 1335ee0ac0c2SChuck Lever if (!transport->inet) { 1336b0d93ad5SChuck Lever struct sock *sk = sock->sk; 1337b0d93ad5SChuck Lever 1338b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 1339b0d93ad5SChuck Lever 1340b0d93ad5SChuck Lever sk->sk_user_data = xprt; 1341314dfd79SChuck Lever transport->old_data_ready = sk->sk_data_ready; 1342314dfd79SChuck Lever transport->old_state_change = sk->sk_state_change; 1343314dfd79SChuck Lever transport->old_write_space = sk->sk_write_space; 1344b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 1345b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 1346b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 1347b079fa7bSTrond Myklebust sk->sk_allocation = GFP_ATOMIC; 13483167e12cSChuck Lever 13493167e12cSChuck Lever /* socket options */ 13503167e12cSChuck Lever sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 13513167e12cSChuck Lever sock_reset_flag(sk, SOCK_LINGER); 13523167e12cSChuck Lever tcp_sk(sk)->linger2 = 0; 13533167e12cSChuck Lever tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 1354b0d93ad5SChuck Lever 1355b0d93ad5SChuck Lever xprt_clear_connected(xprt); 1356b0d93ad5SChuck Lever 1357b0d93ad5SChuck Lever /* Reset to new socket */ 1358ee0ac0c2SChuck Lever transport->sock = sock; 1359ee0ac0c2SChuck Lever transport->inet = sk; 1360b0d93ad5SChuck Lever 1361b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 1362b0d93ad5SChuck Lever } 1363b0d93ad5SChuck Lever 1364b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 1365262ca07dSChuck Lever xprt->stat.connect_count++; 1366262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 1367e6242e92SSridhar Samudrala status = kernel_connect(sock, (struct sockaddr *) &xprt->addr, 1368c4efcb1dSChuck Lever xprt->addrlen, O_NONBLOCK); 1369a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 137046121cf7SChuck Lever xprt, -status, xprt_connected(xprt), 137146121cf7SChuck Lever sock->sk->sk_state); 1372a246b010SChuck Lever if (status < 0) { 1373a246b010SChuck Lever switch (status) { 1374a246b010SChuck Lever case -EINPROGRESS: 1375a246b010SChuck Lever case -EALREADY: 1376a246b010SChuck Lever goto out_clear; 13773167e12cSChuck Lever case -ECONNREFUSED: 13783167e12cSChuck Lever case -ECONNRESET: 13793167e12cSChuck Lever /* retry with existing socket, after a delay */ 13803167e12cSChuck Lever break; 13813167e12cSChuck Lever default: 13823167e12cSChuck Lever /* get rid of existing socket, and retry */ 13833167e12cSChuck Lever xs_close(xprt); 13843167e12cSChuck Lever break; 1385a246b010SChuck Lever } 1386a246b010SChuck Lever } 1387a246b010SChuck Lever out: 138844fbac22SChuck Lever xprt_wake_pending_tasks(xprt, status); 1389a246b010SChuck Lever out_clear: 13902226feb6SChuck Lever xprt_clear_connecting(xprt); 1391a246b010SChuck Lever } 1392a246b010SChuck Lever 13939903cd1cSChuck Lever /** 13949903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 13959903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 13969903cd1cSChuck Lever * 13979903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 139803bf4b70SChuck Lever * 139903bf4b70SChuck Lever * UDP socket connects are synchronous, but we use a work queue anyway 140003bf4b70SChuck Lever * to guarantee that even unprivileged user processes can set up a 140103bf4b70SChuck Lever * socket on a privileged port. 140203bf4b70SChuck Lever * 140303bf4b70SChuck Lever * If a UDP socket connect fails, the delay behavior here prevents 140403bf4b70SChuck Lever * retry floods (hard mounts). 14059903cd1cSChuck Lever */ 14069903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 1407a246b010SChuck Lever { 1408a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1409ee0ac0c2SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1410a246b010SChuck Lever 1411b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1412b0d93ad5SChuck Lever return; 1413b0d93ad5SChuck Lever 1414ee0ac0c2SChuck Lever if (transport->sock != NULL) { 141546121cf7SChuck Lever dprintk("RPC: xs_connect delayed xprt %p for %lu " 141646121cf7SChuck Lever "seconds\n", 141703bf4b70SChuck Lever xprt, xprt->reestablish_timeout / HZ); 1418c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1419c1384c9cSTrond Myklebust &transport->connect_worker, 142003bf4b70SChuck Lever xprt->reestablish_timeout); 142103bf4b70SChuck Lever xprt->reestablish_timeout <<= 1; 142203bf4b70SChuck Lever if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) 142303bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; 14249903cd1cSChuck Lever } else { 14259903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 1426c1384c9cSTrond Myklebust queue_delayed_work(rpciod_workqueue, 1427c1384c9cSTrond Myklebust &transport->connect_worker, 0); 1428a246b010SChuck Lever } 1429a246b010SChuck Lever } 1430a246b010SChuck Lever 1431262ca07dSChuck Lever /** 1432262ca07dSChuck Lever * xs_udp_print_stats - display UDP socket-specifc stats 1433262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1434262ca07dSChuck Lever * @seq: output file 1435262ca07dSChuck Lever * 1436262ca07dSChuck Lever */ 1437262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1438262ca07dSChuck Lever { 1439c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1440c8475461SChuck Lever 1441262ca07dSChuck Lever seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", 1442c8475461SChuck Lever transport->port, 1443262ca07dSChuck Lever xprt->stat.bind_count, 1444262ca07dSChuck Lever xprt->stat.sends, 1445262ca07dSChuck Lever xprt->stat.recvs, 1446262ca07dSChuck Lever xprt->stat.bad_xids, 1447262ca07dSChuck Lever xprt->stat.req_u, 1448262ca07dSChuck Lever xprt->stat.bklog_u); 1449262ca07dSChuck Lever } 1450262ca07dSChuck Lever 1451262ca07dSChuck Lever /** 1452262ca07dSChuck Lever * xs_tcp_print_stats - display TCP socket-specifc stats 1453262ca07dSChuck Lever * @xprt: rpc_xprt struct containing statistics 1454262ca07dSChuck Lever * @seq: output file 1455262ca07dSChuck Lever * 1456262ca07dSChuck Lever */ 1457262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 1458262ca07dSChuck Lever { 1459c8475461SChuck Lever struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1460262ca07dSChuck Lever long idle_time = 0; 1461262ca07dSChuck Lever 1462262ca07dSChuck Lever if (xprt_connected(xprt)) 1463262ca07dSChuck Lever idle_time = (long)(jiffies - xprt->last_used) / HZ; 1464262ca07dSChuck Lever 1465262ca07dSChuck Lever seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", 1466c8475461SChuck Lever transport->port, 1467262ca07dSChuck Lever xprt->stat.bind_count, 1468262ca07dSChuck Lever xprt->stat.connect_count, 1469262ca07dSChuck Lever xprt->stat.connect_time, 1470262ca07dSChuck Lever idle_time, 1471262ca07dSChuck Lever xprt->stat.sends, 1472262ca07dSChuck Lever xprt->stat.recvs, 1473262ca07dSChuck Lever xprt->stat.bad_xids, 1474262ca07dSChuck Lever xprt->stat.req_u, 1475262ca07dSChuck Lever xprt->stat.bklog_u); 1476262ca07dSChuck Lever } 1477262ca07dSChuck Lever 1478262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 147943118c29SChuck Lever .set_buffer_size = xs_udp_set_buffer_size, 148012a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt_cong, 148149e9a890SChuck Lever .release_xprt = xprt_release_xprt_cong, 148245160d62SChuck Lever .rpcbind = rpcb_getport_async, 148392200412SChuck Lever .set_port = xs_set_port, 14849903cd1cSChuck Lever .connect = xs_connect, 148502107148SChuck Lever .buf_alloc = rpc_malloc, 148602107148SChuck Lever .buf_free = rpc_free, 1487262965f5SChuck Lever .send_request = xs_udp_send_request, 1488fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 148946c0ee8bSChuck Lever .timer = xs_udp_timer, 1490a58dd398SChuck Lever .release_request = xprt_release_rqst_cong, 1491262965f5SChuck Lever .close = xs_close, 1492262965f5SChuck Lever .destroy = xs_destroy, 1493262ca07dSChuck Lever .print_stats = xs_udp_print_stats, 1494262965f5SChuck Lever }; 1495262965f5SChuck Lever 1496262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 149712a80469SChuck Lever .reserve_xprt = xprt_reserve_xprt, 1498e0ab53deSTrond Myklebust .release_xprt = xs_tcp_release_xprt, 149945160d62SChuck Lever .rpcbind = rpcb_getport_async, 150092200412SChuck Lever .set_port = xs_set_port, 1501262965f5SChuck Lever .connect = xs_connect, 150202107148SChuck Lever .buf_alloc = rpc_malloc, 150302107148SChuck Lever .buf_free = rpc_free, 1504262965f5SChuck Lever .send_request = xs_tcp_send_request, 1505fe3aca29SChuck Lever .set_retrans_timeout = xprt_set_retrans_timeout_def, 15069903cd1cSChuck Lever .close = xs_close, 15079903cd1cSChuck Lever .destroy = xs_destroy, 1508262ca07dSChuck Lever .print_stats = xs_tcp_print_stats, 1509a246b010SChuck Lever }; 1510a246b010SChuck Lever 151196802a09SFrank van Maarseveen static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args, unsigned int slot_table_size) 1512c8541ecdSChuck Lever { 1513c8541ecdSChuck Lever struct rpc_xprt *xprt; 1514ffc2e518SChuck Lever struct sock_xprt *new; 1515c8541ecdSChuck Lever 151696802a09SFrank van Maarseveen if (args->addrlen > sizeof(xprt->addr)) { 1517c8541ecdSChuck Lever dprintk("RPC: xs_setup_xprt: address too large\n"); 1518c8541ecdSChuck Lever return ERR_PTR(-EBADF); 1519c8541ecdSChuck Lever } 1520c8541ecdSChuck Lever 1521ffc2e518SChuck Lever new = kzalloc(sizeof(*new), GFP_KERNEL); 1522ffc2e518SChuck Lever if (new == NULL) { 152346121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate " 152446121cf7SChuck Lever "rpc_xprt\n"); 1525c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1526c8541ecdSChuck Lever } 1527ffc2e518SChuck Lever xprt = &new->xprt; 1528c8541ecdSChuck Lever 1529c8541ecdSChuck Lever xprt->max_reqs = slot_table_size; 1530c8541ecdSChuck Lever xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); 1531c8541ecdSChuck Lever if (xprt->slot == NULL) { 1532c8541ecdSChuck Lever kfree(xprt); 153346121cf7SChuck Lever dprintk("RPC: xs_setup_xprt: couldn't allocate slot " 153446121cf7SChuck Lever "table\n"); 1535c8541ecdSChuck Lever return ERR_PTR(-ENOMEM); 1536c8541ecdSChuck Lever } 1537c8541ecdSChuck Lever 153896802a09SFrank van Maarseveen memcpy(&xprt->addr, args->dstaddr, args->addrlen); 153996802a09SFrank van Maarseveen xprt->addrlen = args->addrlen; 1540d3bc9a1dSFrank van Maarseveen if (args->srcaddr) 1541d3bc9a1dSFrank van Maarseveen memcpy(&new->addr, args->srcaddr, args->addrlen); 1542c8475461SChuck Lever new->port = xs_get_random_port(); 1543c8541ecdSChuck Lever 1544c8541ecdSChuck Lever return xprt; 1545c8541ecdSChuck Lever } 1546c8541ecdSChuck Lever 15479903cd1cSChuck Lever /** 15489903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 154996802a09SFrank van Maarseveen * @args: rpc transport creation arguments 15509903cd1cSChuck Lever * 15519903cd1cSChuck Lever */ 155296802a09SFrank van Maarseveen struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args) 1553a246b010SChuck Lever { 1554c8541ecdSChuck Lever struct rpc_xprt *xprt; 1555c8475461SChuck Lever struct sock_xprt *transport; 1556a246b010SChuck Lever 155796802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); 1558c8541ecdSChuck Lever if (IS_ERR(xprt)) 1559c8541ecdSChuck Lever return xprt; 1560c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 1561a246b010SChuck Lever 156296802a09SFrank van Maarseveen if (ntohs(((struct sockaddr_in *)args->dstaddr)->sin_port) != 0) 1563ec739ef0SChuck Lever xprt_set_bound(xprt); 1564ec739ef0SChuck Lever 1565ec739ef0SChuck Lever xprt->prot = IPPROTO_UDP; 1566808012fbSChuck Lever xprt->tsh_size = 0; 1567a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 1568a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 1569a246b010SChuck Lever 157034161db6STrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_connect_worker); 157103bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 157203bf4b70SChuck Lever xprt->connect_timeout = XS_UDP_CONN_TO; 157303bf4b70SChuck Lever xprt->reestablish_timeout = XS_UDP_REEST_TO; 157403bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 1575a246b010SChuck Lever 1576262965f5SChuck Lever xprt->ops = &xs_udp_ops; 1577a246b010SChuck Lever 157896802a09SFrank van Maarseveen if (args->timeout) 157996802a09SFrank van Maarseveen xprt->timeout = *args->timeout; 1580a246b010SChuck Lever else 15819903cd1cSChuck Lever xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); 1582a246b010SChuck Lever 1583edb267a6SChuck Lever xs_format_peer_addresses(xprt); 1584edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 15857559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 1586edb267a6SChuck Lever 1587c8541ecdSChuck Lever return xprt; 1588a246b010SChuck Lever } 1589a246b010SChuck Lever 15909903cd1cSChuck Lever /** 15919903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 159296802a09SFrank van Maarseveen * @args: rpc transport creation arguments 15939903cd1cSChuck Lever * 15949903cd1cSChuck Lever */ 159596802a09SFrank van Maarseveen struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args) 1596a246b010SChuck Lever { 1597c8541ecdSChuck Lever struct rpc_xprt *xprt; 1598c8475461SChuck Lever struct sock_xprt *transport; 1599a246b010SChuck Lever 160096802a09SFrank van Maarseveen xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); 1601c8541ecdSChuck Lever if (IS_ERR(xprt)) 1602c8541ecdSChuck Lever return xprt; 1603c8475461SChuck Lever transport = container_of(xprt, struct sock_xprt, xprt); 1604a246b010SChuck Lever 160596802a09SFrank van Maarseveen if (ntohs(((struct sockaddr_in *)args->dstaddr)->sin_port) != 0) 1606ec739ef0SChuck Lever xprt_set_bound(xprt); 1607ec739ef0SChuck Lever 1608ec739ef0SChuck Lever xprt->prot = IPPROTO_TCP; 1609808012fbSChuck Lever xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 1610808012fbSChuck Lever xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 1611a246b010SChuck Lever 161234161db6STrond Myklebust INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker); 161303bf4b70SChuck Lever xprt->bind_timeout = XS_BIND_TO; 161403bf4b70SChuck Lever xprt->connect_timeout = XS_TCP_CONN_TO; 161503bf4b70SChuck Lever xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 161603bf4b70SChuck Lever xprt->idle_timeout = XS_IDLE_DISC_TO; 1617a246b010SChuck Lever 1618262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 1619a246b010SChuck Lever 162096802a09SFrank van Maarseveen if (args->timeout) 162196802a09SFrank van Maarseveen xprt->timeout = *args->timeout; 1622a246b010SChuck Lever else 16239903cd1cSChuck Lever xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); 1624a246b010SChuck Lever 1625edb267a6SChuck Lever xs_format_peer_addresses(xprt); 1626edb267a6SChuck Lever dprintk("RPC: set up transport to address %s\n", 16277559c7a2SChuck Lever xprt->address_strings[RPC_DISPLAY_ALL]); 1628edb267a6SChuck Lever 1629c8541ecdSChuck Lever return xprt; 1630a246b010SChuck Lever } 1631282b32e1SChuck Lever 1632282b32e1SChuck Lever /** 1633fbf76683SChuck Lever * init_socket_xprt - set up xprtsock's sysctls 1634282b32e1SChuck Lever * 1635282b32e1SChuck Lever */ 1636282b32e1SChuck Lever int init_socket_xprt(void) 1637282b32e1SChuck Lever { 1638fbf76683SChuck Lever #ifdef RPC_DEBUG 16392b1bec5fSEric W. Biederman if (!sunrpc_table_header) 16400b4d4147SEric W. Biederman sunrpc_table_header = register_sysctl_table(sunrpc_table); 1641fbf76683SChuck Lever #endif 1642fbf76683SChuck Lever 1643282b32e1SChuck Lever return 0; 1644282b32e1SChuck Lever } 1645282b32e1SChuck Lever 1646282b32e1SChuck Lever /** 1647fbf76683SChuck Lever * cleanup_socket_xprt - remove xprtsock's sysctls 1648282b32e1SChuck Lever * 1649282b32e1SChuck Lever */ 1650282b32e1SChuck Lever void cleanup_socket_xprt(void) 1651282b32e1SChuck Lever { 1652fbf76683SChuck Lever #ifdef RPC_DEBUG 1653fbf76683SChuck Lever if (sunrpc_table_header) { 1654fbf76683SChuck Lever unregister_sysctl_table(sunrpc_table_header); 1655fbf76683SChuck Lever sunrpc_table_header = NULL; 1656fbf76683SChuck Lever } 1657fbf76683SChuck Lever #endif 1658282b32e1SChuck Lever } 1659