2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com>
7 * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com>
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
21 #include <linux/types.h>
22 #include <linux/slab.h>
23 #include <linux/capability.h>
24 #include <linux/pagemap.h>
25 #include <linux/errno.h>
26 #include <linux/socket.h>
28 #include <linux/net.h>
30 #include <linux/udp.h>
31 #include <linux/tcp.h>
32 #include <linux/sunrpc/clnt.h>
33 #include <linux/sunrpc/sched.h>
34 #include <linux/file.h>
37 #include <net/checksum.h>
44 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
45 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
48 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
51 * We can register our own files under /proc/sys/sunrpc by
52 * calling register_sysctl_table() again. The files in that
53 * directory become the union of all files registered there.
55 * We simply need to make sure that we don't collide with
56 * someone else's file names!
61 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
62 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
63 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
64 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
66 static struct ctl_table_header *sunrpc_table_header;
69 * FIXME: changing the UDP slot table size should also resize the UDP
70 * socket buffers for existing UDP transports
72 static ctl_table xs_tunables_table[] = {
74 .ctl_name = CTL_SLOTTABLE_UDP,
75 .procname = "udp_slot_table_entries",
76 .data = &xprt_udp_slot_table_entries,
77 .maxlen = sizeof(unsigned int),
79 .proc_handler = &proc_dointvec_minmax,
80 .strategy = &sysctl_intvec,
81 .extra1 = &min_slot_table_size,
82 .extra2 = &max_slot_table_size
85 .ctl_name = CTL_SLOTTABLE_TCP,
86 .procname = "tcp_slot_table_entries",
87 .data = &xprt_tcp_slot_table_entries,
88 .maxlen = sizeof(unsigned int),
90 .proc_handler = &proc_dointvec_minmax,
91 .strategy = &sysctl_intvec,
92 .extra1 = &min_slot_table_size,
93 .extra2 = &max_slot_table_size
96 .ctl_name = CTL_MIN_RESVPORT,
97 .procname = "min_resvport",
98 .data = &xprt_min_resvport,
99 .maxlen = sizeof(unsigned int),
101 .proc_handler = &proc_dointvec_minmax,
102 .strategy = &sysctl_intvec,
103 .extra1 = &xprt_min_resvport_limit,
104 .extra2 = &xprt_max_resvport_limit
107 .ctl_name = CTL_MAX_RESVPORT,
108 .procname = "max_resvport",
109 .data = &xprt_max_resvport,
110 .maxlen = sizeof(unsigned int),
112 .proc_handler = &proc_dointvec_minmax,
113 .strategy = &sysctl_intvec,
114 .extra1 = &xprt_min_resvport_limit,
115 .extra2 = &xprt_max_resvport_limit
122 static ctl_table sunrpc_table[] = {
124 .ctl_name = CTL_SUNRPC,
125 .procname = "sunrpc",
127 .child = xs_tunables_table
137 * How many times to try sending a request on a socket before waiting
138 * for the socket buffer to clear.
140 #define XS_SENDMSG_RETRY (10U)
143 * Time out for an RPC UDP socket connect. UDP socket connects are
144 * synchronous, but we set a timeout anyway in case of resource
145 * exhaustion on the local host.
147 #define XS_UDP_CONN_TO (5U * HZ)
150 * Wait duration for an RPC TCP connection to be established. Solaris
151 * NFS over TCP uses 60 seconds, for example, which is in line with how
152 * long a server takes to reboot.
154 #define XS_TCP_CONN_TO (60U * HZ)
157 * Wait duration for a reply from the RPC portmapper.
159 #define XS_BIND_TO (60U * HZ)
162 * Delay if a UDP socket connect error occurs. This is most likely some
163 * kind of resource problem on the local host.
165 #define XS_UDP_REEST_TO (2U * HZ)
168 * The reestablish timeout allows clients to delay for a bit before attempting
169 * to reconnect to a server that just dropped our connection.
171 * We implement an exponential backoff when trying to reestablish a TCP
172 * transport connection with the server. Some servers like to drop a TCP
173 * connection when they are overworked, so we start with a short timeout and
174 * increase over time if the server is down or not responding.
176 #define XS_TCP_INIT_REEST_TO (3U * HZ)
177 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
180 * TCP idle timeout; client drops the transport socket if it is idle
181 * for this long. Note that we also timeout UDP sockets to prevent
182 * holding port numbers when there is no RPC traffic.
184 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
187 # undef RPC_DEBUG_DATA
188 # define RPCDBG_FACILITY RPCDBG_TRANS
191 #ifdef RPC_DEBUG_DATA
192 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
194 u8 *buf = (u8 *) packet;
197 dprintk("RPC: %s\n", msg);
198 for (j = 0; j < count && j < 128; j += 4) {
202 dprintk("0x%04x ", j);
204 dprintk("%02x%02x%02x%02x ",
205 buf[j], buf[j+1], buf[j+2], buf[j+3]);
210 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
217 struct rpc_xprt xprt;
222 struct socket * sock;
226 * State of TCP reply receive
234 unsigned long tcp_copied,
238 * Connection of transports
240 struct delayed_work connect_worker;
241 struct sockaddr_storage addr;
245 * UDP socket buffer size parameters
251 * Saved socket callback addresses
253 void (*old_data_ready)(struct sock *, int);
254 void (*old_state_change)(struct sock *);
255 void (*old_write_space)(struct sock *);
259 * TCP receive state flags
261 #define TCP_RCV_LAST_FRAG (1UL << 0)
262 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
263 #define TCP_RCV_COPY_XID (1UL << 2)
264 #define TCP_RCV_COPY_DATA (1UL << 3)
266 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
268 return (struct sockaddr *) &xprt->addr;
271 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
273 return (struct sockaddr_in *) &xprt->addr;
276 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
278 return (struct sockaddr_in6 *) &xprt->addr;
281 static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt)
283 struct sockaddr_in *addr = xs_addr_in(xprt);
286 buf = kzalloc(20, GFP_KERNEL);
288 snprintf(buf, 20, NIPQUAD_FMT,
289 NIPQUAD(addr->sin_addr.s_addr));
291 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
293 buf = kzalloc(8, GFP_KERNEL);
295 snprintf(buf, 8, "%u",
296 ntohs(addr->sin_port));
298 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
300 buf = kzalloc(8, GFP_KERNEL);
302 if (xprt->prot == IPPROTO_UDP)
303 snprintf(buf, 8, "udp");
305 snprintf(buf, 8, "tcp");
307 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
309 buf = kzalloc(48, GFP_KERNEL);
311 snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s",
312 NIPQUAD(addr->sin_addr.s_addr),
313 ntohs(addr->sin_port),
314 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
316 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
318 buf = kzalloc(10, GFP_KERNEL);
320 snprintf(buf, 10, "%02x%02x%02x%02x",
321 NIPQUAD(addr->sin_addr.s_addr));
323 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
325 buf = kzalloc(8, GFP_KERNEL);
327 snprintf(buf, 8, "%4hx",
328 ntohs(addr->sin_port));
330 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
333 static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt)
335 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
338 buf = kzalloc(40, GFP_KERNEL);
340 snprintf(buf, 40, NIP6_FMT,
341 NIP6(addr->sin6_addr));
343 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
345 buf = kzalloc(8, GFP_KERNEL);
347 snprintf(buf, 8, "%u",
348 ntohs(addr->sin6_port));
350 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
352 buf = kzalloc(8, GFP_KERNEL);
354 if (xprt->prot == IPPROTO_UDP)
355 snprintf(buf, 8, "udp");
357 snprintf(buf, 8, "tcp");
359 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
361 buf = kzalloc(64, GFP_KERNEL);
363 snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s",
364 NIP6(addr->sin6_addr),
365 ntohs(addr->sin6_port),
366 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
368 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
370 buf = kzalloc(36, GFP_KERNEL);
372 snprintf(buf, 36, NIP6_SEQFMT,
373 NIP6(addr->sin6_addr));
375 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
377 buf = kzalloc(8, GFP_KERNEL);
379 snprintf(buf, 8, "%4hx",
380 ntohs(addr->sin6_port));
382 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
385 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
389 for (i = 0; i < RPC_DISPLAY_MAX; i++)
390 kfree(xprt->address_strings[i]);
393 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
395 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
397 struct msghdr msg = {
399 .msg_namelen = addrlen,
400 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
403 .iov_base = vec->iov_base + base,
404 .iov_len = vec->iov_len - base,
407 if (iov.iov_len != 0)
408 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
409 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
412 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
415 unsigned int remainder;
418 remainder = xdr->page_len - base;
419 base += xdr->page_base;
420 ppage = xdr->pages + (base >> PAGE_SHIFT);
423 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
424 int flags = XS_SENDMSG_FLAGS;
427 if (remainder != 0 || more)
429 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
430 if (remainder == 0 || err != len)
444 * xs_sendpages - write pages directly to a socket
445 * @sock: socket to send on
446 * @addr: UDP only -- address of destination
447 * @addrlen: UDP only -- length of destination address
448 * @xdr: buffer containing this request
449 * @base: starting position in the buffer
452 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
454 unsigned int remainder = xdr->len - base;
460 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
466 if (base < xdr->head[0].iov_len || addr != NULL) {
467 unsigned int len = xdr->head[0].iov_len - base;
469 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
470 if (remainder == 0 || err != len)
475 base -= xdr->head[0].iov_len;
477 if (base < xdr->page_len) {
478 unsigned int len = xdr->page_len - base;
480 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
481 if (remainder == 0 || err != len)
486 base -= xdr->page_len;
488 if (base >= xdr->tail[0].iov_len)
490 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
500 * xs_nospace - place task on wait queue if transmit was incomplete
501 * @task: task to put to sleep
504 static void xs_nospace(struct rpc_task *task)
506 struct rpc_rqst *req = task->tk_rqstp;
507 struct rpc_xprt *xprt = req->rq_xprt;
508 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
510 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
511 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
514 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
515 /* Protect against races with write_space */
516 spin_lock_bh(&xprt->transport_lock);
518 /* Don't race with disconnect */
519 if (!xprt_connected(xprt))
520 task->tk_status = -ENOTCONN;
521 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
522 xprt_wait_for_buffer_space(task);
524 spin_unlock_bh(&xprt->transport_lock);
526 /* Keep holding the socket if it is blocked */
527 rpc_delay(task, HZ>>4);
531 * xs_udp_send_request - write an RPC request to a UDP socket
532 * @task: address of RPC task that manages the state of an RPC request
535 * 0: The request has been sent
536 * EAGAIN: The socket was blocked, please call again later to
537 * complete the request
538 * ENOTCONN: Caller needs to invoke connect logic then call again
539 * other: Some other error occured, the request was not sent
541 static int xs_udp_send_request(struct rpc_task *task)
543 struct rpc_rqst *req = task->tk_rqstp;
544 struct rpc_xprt *xprt = req->rq_xprt;
545 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
546 struct xdr_buf *xdr = &req->rq_snd_buf;
549 xs_pktdump("packet data:",
550 req->rq_svec->iov_base,
551 req->rq_svec->iov_len);
553 req->rq_xtime = jiffies;
554 status = xs_sendpages(transport->sock,
559 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
560 xdr->len - req->rq_bytes_sent, status);
562 if (likely(status >= (int) req->rq_slen))
565 /* Still some bytes left; set up for a retry later. */
573 /* When the server has died, an ICMP port unreachable message
574 * prompts ECONNREFUSED. */
580 dprintk("RPC: sendmsg returned unrecognized error %d\n",
588 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
590 u32 reclen = buf->len - sizeof(rpc_fraghdr);
591 rpc_fraghdr *base = buf->head[0].iov_base;
592 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
596 * xs_tcp_send_request - write an RPC request to a TCP socket
597 * @task: address of RPC task that manages the state of an RPC request
600 * 0: The request has been sent
601 * EAGAIN: The socket was blocked, please call again later to
602 * complete the request
603 * ENOTCONN: Caller needs to invoke connect logic then call again
604 * other: Some other error occured, the request was not sent
606 * XXX: In the case of soft timeouts, should we eventually give up
607 * if sendmsg is not able to make progress?
609 static int xs_tcp_send_request(struct rpc_task *task)
611 struct rpc_rqst *req = task->tk_rqstp;
612 struct rpc_xprt *xprt = req->rq_xprt;
613 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
614 struct xdr_buf *xdr = &req->rq_snd_buf;
616 unsigned int retry = 0;
618 xs_encode_tcp_record_marker(&req->rq_snd_buf);
620 xs_pktdump("packet data:",
621 req->rq_svec->iov_base,
622 req->rq_svec->iov_len);
624 /* Continue transmitting the packet/record. We must be careful
625 * to cope with writespace callbacks arriving _after_ we have
626 * called sendmsg(). */
628 req->rq_xtime = jiffies;
629 status = xs_sendpages(transport->sock,
630 NULL, 0, xdr, req->rq_bytes_sent);
632 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
633 xdr->len - req->rq_bytes_sent, status);
635 if (unlikely(status < 0))
638 /* If we've sent the entire packet, immediately
639 * reset the count of bytes sent. */
640 req->rq_bytes_sent += status;
641 task->tk_bytes_sent += status;
642 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
643 req->rq_bytes_sent = 0;
648 if (retry++ > XS_SENDMSG_RETRY)
663 dprintk("RPC: sendmsg returned unrecognized error %d\n",
665 xprt_disconnect(xprt);
673 * xs_tcp_release_xprt - clean up after a tcp transmission
677 * This cleans up if an error causes us to abort the transmission of a request.
678 * In this case, the socket may need to be reset in order to avoid confusing
681 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
683 struct rpc_rqst *req;
685 if (task != xprt->snd_task)
689 req = task->tk_rqstp;
690 if (req->rq_bytes_sent == 0)
692 if (req->rq_bytes_sent == req->rq_snd_buf.len)
694 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
696 xprt_release_xprt(xprt, task);
700 * xs_close - close a socket
703 * This is used when all requests are complete; ie, no DRC state remains
704 * on the server we want to save.
706 static void xs_close(struct rpc_xprt *xprt)
708 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
709 struct socket *sock = transport->sock;
710 struct sock *sk = transport->inet;
713 goto clear_close_wait;
715 dprintk("RPC: xs_close xprt %p\n", xprt);
717 write_lock_bh(&sk->sk_callback_lock);
718 transport->inet = NULL;
719 transport->sock = NULL;
721 sk->sk_user_data = NULL;
722 sk->sk_data_ready = transport->old_data_ready;
723 sk->sk_state_change = transport->old_state_change;
724 sk->sk_write_space = transport->old_write_space;
725 write_unlock_bh(&sk->sk_callback_lock);
731 smp_mb__before_clear_bit();
732 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
733 smp_mb__after_clear_bit();
737 * xs_destroy - prepare to shutdown a transport
738 * @xprt: doomed transport
741 static void xs_destroy(struct rpc_xprt *xprt)
743 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
745 dprintk("RPC: xs_destroy xprt %p\n", xprt);
747 cancel_rearming_delayed_work(&transport->connect_worker);
749 xprt_disconnect(xprt);
751 xs_free_peer_addresses(xprt);
756 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
758 return (struct rpc_xprt *) sk->sk_user_data;
762 * xs_udp_data_ready - "data ready" callback for UDP sockets
763 * @sk: socket with data to read
764 * @len: how much data to read
767 static void xs_udp_data_ready(struct sock *sk, int len)
769 struct rpc_task *task;
770 struct rpc_xprt *xprt;
771 struct rpc_rqst *rovr;
773 int err, repsize, copied;
777 read_lock(&sk->sk_callback_lock);
778 dprintk("RPC: xs_udp_data_ready...\n");
779 if (!(xprt = xprt_from_sock(sk)))
782 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
788 repsize = skb->len - sizeof(struct udphdr);
790 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
794 /* Copy the XID from the skb... */
795 xp = skb_header_pointer(skb, sizeof(struct udphdr),
796 sizeof(_xid), &_xid);
800 /* Look up and lock the request corresponding to the given XID */
801 spin_lock(&xprt->transport_lock);
802 rovr = xprt_lookup_rqst(xprt, *xp);
805 task = rovr->rq_task;
807 if ((copied = rovr->rq_private_buf.buflen) > repsize)
810 /* Suck it into the iovec, verify checksum if not done by hw. */
811 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
814 /* Something worked... */
815 dst_confirm(skb->dst);
817 xprt_adjust_cwnd(task, copied);
818 xprt_update_rtt(task);
819 xprt_complete_rqst(task, copied);
822 spin_unlock(&xprt->transport_lock);
824 skb_free_datagram(sk, skb);
826 read_unlock(&sk->sk_callback_lock);
829 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
831 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
835 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
836 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
837 used = xdr_skb_read_bits(desc, p, len);
838 transport->tcp_offset += used;
842 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
843 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
844 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
846 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
847 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
849 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
850 transport->tcp_offset = 0;
852 /* Sanity check of the record length */
853 if (unlikely(transport->tcp_reclen < 4)) {
854 dprintk("RPC: invalid TCP record fragment length\n");
855 xprt_disconnect(xprt);
858 dprintk("RPC: reading TCP record fragment of length %d\n",
859 transport->tcp_reclen);
862 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
864 if (transport->tcp_offset == transport->tcp_reclen) {
865 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
866 transport->tcp_offset = 0;
867 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
868 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
869 transport->tcp_flags |= TCP_RCV_COPY_XID;
870 transport->tcp_copied = 0;
875 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
880 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
881 dprintk("RPC: reading XID (%Zu bytes)\n", len);
882 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
883 used = xdr_skb_read_bits(desc, p, len);
884 transport->tcp_offset += used;
887 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
888 transport->tcp_flags |= TCP_RCV_COPY_DATA;
889 transport->tcp_copied = 4;
890 dprintk("RPC: reading reply for XID %08x\n",
891 ntohl(transport->tcp_xid));
892 xs_tcp_check_fraghdr(transport);
895 static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
897 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
898 struct rpc_rqst *req;
899 struct xdr_buf *rcvbuf;
903 /* Find and lock the request corresponding to this xid */
904 spin_lock(&xprt->transport_lock);
905 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
907 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
908 dprintk("RPC: XID %08x request not found!\n",
909 ntohl(transport->tcp_xid));
910 spin_unlock(&xprt->transport_lock);
914 rcvbuf = &req->rq_private_buf;
916 if (len > transport->tcp_reclen - transport->tcp_offset) {
917 struct xdr_skb_reader my_desc;
919 len = transport->tcp_reclen - transport->tcp_offset;
920 memcpy(&my_desc, desc, sizeof(my_desc));
922 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
923 &my_desc, xdr_skb_read_bits);
927 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
928 desc, xdr_skb_read_bits);
931 transport->tcp_copied += r;
932 transport->tcp_offset += r;
935 /* Error when copying to the receive buffer,
936 * usually because we weren't able to allocate
937 * additional buffer pages. All we can do now
938 * is turn off TCP_RCV_COPY_DATA, so the request
939 * will not receive any additional updates,
941 * Any remaining data from this record will
944 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
945 dprintk("RPC: XID %08x truncated request\n",
946 ntohl(transport->tcp_xid));
947 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
948 "tcp_offset = %u, tcp_reclen = %u\n",
949 xprt, transport->tcp_copied,
950 transport->tcp_offset, transport->tcp_reclen);
954 dprintk("RPC: XID %08x read %Zd bytes\n",
955 ntohl(transport->tcp_xid), r);
956 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
957 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
958 transport->tcp_offset, transport->tcp_reclen);
960 if (transport->tcp_copied == req->rq_private_buf.buflen)
961 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
962 else if (transport->tcp_offset == transport->tcp_reclen) {
963 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
964 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
968 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
969 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
970 spin_unlock(&xprt->transport_lock);
971 xs_tcp_check_fraghdr(transport);
974 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
978 len = transport->tcp_reclen - transport->tcp_offset;
979 if (len > desc->count)
983 transport->tcp_offset += len;
984 dprintk("RPC: discarded %Zu bytes\n", len);
985 xs_tcp_check_fraghdr(transport);
988 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
990 struct rpc_xprt *xprt = rd_desc->arg.data;
991 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
992 struct xdr_skb_reader desc = {
998 dprintk("RPC: xs_tcp_data_recv started\n");
1000 /* Read in a new fragment marker if necessary */
1001 /* Can we ever really expect to get completely empty fragments? */
1002 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1003 xs_tcp_read_fraghdr(xprt, &desc);
1006 /* Read in the xid if necessary */
1007 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1008 xs_tcp_read_xid(transport, &desc);
1011 /* Read in the request data */
1012 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1013 xs_tcp_read_request(xprt, &desc);
1016 /* Skip over any trailing bytes on short reads */
1017 xs_tcp_read_discard(transport, &desc);
1018 } while (desc.count);
1019 dprintk("RPC: xs_tcp_data_recv done\n");
1020 return len - desc.count;
1024 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1025 * @sk: socket with data to read
1026 * @bytes: how much data to read
1029 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1031 struct rpc_xprt *xprt;
1032 read_descriptor_t rd_desc;
1034 dprintk("RPC: xs_tcp_data_ready...\n");
1036 read_lock(&sk->sk_callback_lock);
1037 if (!(xprt = xprt_from_sock(sk)))
1042 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1043 rd_desc.arg.data = xprt;
1044 rd_desc.count = 65536;
1045 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1047 read_unlock(&sk->sk_callback_lock);
1051 * xs_tcp_state_change - callback to handle TCP socket state changes
1052 * @sk: socket whose state has changed
1055 static void xs_tcp_state_change(struct sock *sk)
1057 struct rpc_xprt *xprt;
1059 read_lock(&sk->sk_callback_lock);
1060 if (!(xprt = xprt_from_sock(sk)))
1062 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1063 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1064 sk->sk_state, xprt_connected(xprt),
1065 sock_flag(sk, SOCK_DEAD),
1066 sock_flag(sk, SOCK_ZAPPED));
1068 switch (sk->sk_state) {
1069 case TCP_ESTABLISHED:
1070 spin_lock_bh(&xprt->transport_lock);
1071 if (!xprt_test_and_set_connected(xprt)) {
1072 struct sock_xprt *transport = container_of(xprt,
1073 struct sock_xprt, xprt);
1075 /* Reset TCP record info */
1076 transport->tcp_offset = 0;
1077 transport->tcp_reclen = 0;
1078 transport->tcp_copied = 0;
1079 transport->tcp_flags =
1080 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1082 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1083 xprt_wake_pending_tasks(xprt, 0);
1085 spin_unlock_bh(&xprt->transport_lock);
1090 case TCP_CLOSE_WAIT:
1091 /* Try to schedule an autoclose RPC calls */
1092 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1093 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
1094 queue_work(rpciod_workqueue, &xprt->task_cleanup);
1096 xprt_disconnect(xprt);
1099 read_unlock(&sk->sk_callback_lock);
1103 * xs_udp_write_space - callback invoked when socket buffer space
1105 * @sk: socket whose state has changed
1107 * Called when more output buffer space is available for this socket.
1108 * We try not to wake our writers until they can make "significant"
1109 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1110 * with a bunch of small requests.
1112 static void xs_udp_write_space(struct sock *sk)
1114 read_lock(&sk->sk_callback_lock);
1116 /* from net/core/sock.c:sock_def_write_space */
1117 if (sock_writeable(sk)) {
1118 struct socket *sock;
1119 struct rpc_xprt *xprt;
1121 if (unlikely(!(sock = sk->sk_socket)))
1123 if (unlikely(!(xprt = xprt_from_sock(sk))))
1125 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1128 xprt_write_space(xprt);
1132 read_unlock(&sk->sk_callback_lock);
1136 * xs_tcp_write_space - callback invoked when socket buffer space
1138 * @sk: socket whose state has changed
1140 * Called when more output buffer space is available for this socket.
1141 * We try not to wake our writers until they can make "significant"
1142 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1143 * with a bunch of small requests.
1145 static void xs_tcp_write_space(struct sock *sk)
1147 read_lock(&sk->sk_callback_lock);
1149 /* from net/core/stream.c:sk_stream_write_space */
1150 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1151 struct socket *sock;
1152 struct rpc_xprt *xprt;
1154 if (unlikely(!(sock = sk->sk_socket)))
1156 if (unlikely(!(xprt = xprt_from_sock(sk))))
1158 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1161 xprt_write_space(xprt);
1165 read_unlock(&sk->sk_callback_lock);
1168 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1170 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1171 struct sock *sk = transport->inet;
1173 if (transport->rcvsize) {
1174 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1175 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1177 if (transport->sndsize) {
1178 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1179 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1180 sk->sk_write_space(sk);
1185 * xs_udp_set_buffer_size - set send and receive limits
1186 * @xprt: generic transport
1187 * @sndsize: requested size of send buffer, in bytes
1188 * @rcvsize: requested size of receive buffer, in bytes
1190 * Set socket send and receive buffer size limits.
1192 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1194 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1196 transport->sndsize = 0;
1198 transport->sndsize = sndsize + 1024;
1199 transport->rcvsize = 0;
1201 transport->rcvsize = rcvsize + 1024;
1203 xs_udp_do_set_buffer_size(xprt);
1207 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1208 * @task: task that timed out
1210 * Adjust the congestion window after a retransmit timeout has occurred.
1212 static void xs_udp_timer(struct rpc_task *task)
1214 xprt_adjust_cwnd(task, -ETIMEDOUT);
1217 static unsigned short xs_get_random_port(void)
1219 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1220 unsigned short rand = (unsigned short) net_random() % range;
1221 return rand + xprt_min_resvport;
1225 * xs_set_port - reset the port number in the remote endpoint address
1226 * @xprt: generic transport
1227 * @port: new port number
1230 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1232 struct sockaddr *addr = xs_addr(xprt);
1234 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1236 switch (addr->sa_family) {
1238 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1241 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1248 static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1250 struct sockaddr_in myaddr = {
1251 .sin_family = AF_INET,
1253 struct sockaddr_in *sa;
1255 unsigned short port = transport->port;
1257 if (!transport->xprt.resvport)
1259 sa = (struct sockaddr_in *)&transport->addr;
1260 myaddr.sin_addr = sa->sin_addr;
1262 myaddr.sin_port = htons(port);
1263 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1265 if (!transport->xprt.resvport)
1268 transport->port = port;
1271 if (port <= xprt_min_resvport)
1272 port = xprt_max_resvport;
1275 } while (err == -EADDRINUSE && port != transport->port);
1276 dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n",
1277 __FUNCTION__, NIPQUAD(myaddr.sin_addr),
1278 port, err ? "failed" : "ok", err);
1282 static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1284 struct sockaddr_in6 myaddr = {
1285 .sin6_family = AF_INET6,
1287 struct sockaddr_in6 *sa;
1289 unsigned short port = transport->port;
1291 if (!transport->xprt.resvport)
1293 sa = (struct sockaddr_in6 *)&transport->addr;
1294 myaddr.sin6_addr = sa->sin6_addr;
1296 myaddr.sin6_port = htons(port);
1297 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1299 if (!transport->xprt.resvport)
1302 transport->port = port;
1305 if (port <= xprt_min_resvport)
1306 port = xprt_max_resvport;
1309 } while (err == -EADDRINUSE && port != transport->port);
1310 dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n",
1311 NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err);
1315 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1316 static struct lock_class_key xs_key[2];
1317 static struct lock_class_key xs_slock_key[2];
1319 static inline void xs_reclassify_socket(struct socket *sock)
1321 struct sock *sk = sock->sk;
1322 BUG_ON(sk->sk_lock.owner != NULL);
1323 switch (sk->sk_family) {
1325 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFS",
1326 &xs_slock_key[0], "sk_lock-AF_INET-NFS", &xs_key[0]);
1330 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFS",
1331 &xs_slock_key[1], "sk_lock-AF_INET6-NFS", &xs_key[1]);
1339 static inline void xs_reclassify_socket(struct socket *sock)
1344 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1346 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1348 if (!transport->inet) {
1349 struct sock *sk = sock->sk;
1351 write_lock_bh(&sk->sk_callback_lock);
1353 sk->sk_user_data = xprt;
1354 transport->old_data_ready = sk->sk_data_ready;
1355 transport->old_state_change = sk->sk_state_change;
1356 transport->old_write_space = sk->sk_write_space;
1357 sk->sk_data_ready = xs_udp_data_ready;
1358 sk->sk_write_space = xs_udp_write_space;
1359 sk->sk_no_check = UDP_CSUM_NORCV;
1360 sk->sk_allocation = GFP_ATOMIC;
1362 xprt_set_connected(xprt);
1364 /* Reset to new socket */
1365 transport->sock = sock;
1366 transport->inet = sk;
1368 write_unlock_bh(&sk->sk_callback_lock);
1370 xs_udp_do_set_buffer_size(xprt);
1374 * xs_udp_connect_worker4 - set up a UDP socket
1375 * @work: RPC transport to connect
1377 * Invoked by a work queue tasklet.
1379 static void xs_udp_connect_worker4(struct work_struct *work)
1381 struct sock_xprt *transport =
1382 container_of(work, struct sock_xprt, connect_worker.work);
1383 struct rpc_xprt *xprt = &transport->xprt;
1384 struct socket *sock = transport->sock;
1385 int err, status = -EIO;
1387 if (xprt->shutdown || !xprt_bound(xprt))
1390 /* Start by resetting any existing state */
1393 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1394 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1397 xs_reclassify_socket(sock);
1399 if (xs_bind4(transport, sock)) {
1404 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1405 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1407 xs_udp_finish_connecting(xprt, sock);
1410 xprt_wake_pending_tasks(xprt, status);
1411 xprt_clear_connecting(xprt);
1415 * xs_udp_connect_worker6 - set up a UDP socket
1416 * @work: RPC transport to connect
1418 * Invoked by a work queue tasklet.
1420 static void xs_udp_connect_worker6(struct work_struct *work)
1422 struct sock_xprt *transport =
1423 container_of(work, struct sock_xprt, connect_worker.work);
1424 struct rpc_xprt *xprt = &transport->xprt;
1425 struct socket *sock = transport->sock;
1426 int err, status = -EIO;
1428 if (xprt->shutdown || !xprt_bound(xprt))
1431 /* Start by resetting any existing state */
1434 if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1435 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1438 xs_reclassify_socket(sock);
1440 if (xs_bind6(transport, sock) < 0) {
1445 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1446 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1448 xs_udp_finish_connecting(xprt, sock);
1451 xprt_wake_pending_tasks(xprt, status);
1452 xprt_clear_connecting(xprt);
1456 * We need to preserve the port number so the reply cache on the server can
1457 * find our cached RPC replies when we get around to reconnecting.
1459 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1462 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1463 struct sockaddr any;
1465 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1468 * Disconnect the transport socket by doing a connect operation
1469 * with AF_UNSPEC. This should return immediately...
1471 memset(&any, 0, sizeof(any));
1472 any.sa_family = AF_UNSPEC;
1473 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
1475 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1479 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1481 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1483 if (!transport->inet) {
1484 struct sock *sk = sock->sk;
1486 write_lock_bh(&sk->sk_callback_lock);
1488 sk->sk_user_data = xprt;
1489 transport->old_data_ready = sk->sk_data_ready;
1490 transport->old_state_change = sk->sk_state_change;
1491 transport->old_write_space = sk->sk_write_space;
1492 sk->sk_data_ready = xs_tcp_data_ready;
1493 sk->sk_state_change = xs_tcp_state_change;
1494 sk->sk_write_space = xs_tcp_write_space;
1495 sk->sk_allocation = GFP_ATOMIC;
1497 /* socket options */
1498 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1499 sock_reset_flag(sk, SOCK_LINGER);
1500 tcp_sk(sk)->linger2 = 0;
1501 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1503 xprt_clear_connected(xprt);
1505 /* Reset to new socket */
1506 transport->sock = sock;
1507 transport->inet = sk;
1509 write_unlock_bh(&sk->sk_callback_lock);
1512 /* Tell the socket layer to start connecting... */
1513 xprt->stat.connect_count++;
1514 xprt->stat.connect_start = jiffies;
1515 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
1519 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1520 * @work: RPC transport to connect
1522 * Invoked by a work queue tasklet.
1524 static void xs_tcp_connect_worker4(struct work_struct *work)
1526 struct sock_xprt *transport =
1527 container_of(work, struct sock_xprt, connect_worker.work);
1528 struct rpc_xprt *xprt = &transport->xprt;
1529 struct socket *sock = transport->sock;
1530 int err, status = -EIO;
1532 if (xprt->shutdown || !xprt_bound(xprt))
1536 /* start from scratch */
1537 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1538 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1541 xs_reclassify_socket(sock);
1543 if (xs_bind4(transport, sock) < 0) {
1548 /* "close" the socket, preserving the local port */
1549 xs_tcp_reuse_connection(xprt);
1551 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1552 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1554 status = xs_tcp_finish_connecting(xprt, sock);
1555 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1556 xprt, -status, xprt_connected(xprt),
1557 sock->sk->sk_state);
1565 /* retry with existing socket, after a delay */
1568 /* get rid of existing socket, and retry */
1574 xprt_wake_pending_tasks(xprt, status);
1576 xprt_clear_connecting(xprt);
1580 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1581 * @work: RPC transport to connect
1583 * Invoked by a work queue tasklet.
1585 static void xs_tcp_connect_worker6(struct work_struct *work)
1587 struct sock_xprt *transport =
1588 container_of(work, struct sock_xprt, connect_worker.work);
1589 struct rpc_xprt *xprt = &transport->xprt;
1590 struct socket *sock = transport->sock;
1591 int err, status = -EIO;
1593 if (xprt->shutdown || !xprt_bound(xprt))
1597 /* start from scratch */
1598 if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1599 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1602 xs_reclassify_socket(sock);
1604 if (xs_bind6(transport, sock) < 0) {
1609 /* "close" the socket, preserving the local port */
1610 xs_tcp_reuse_connection(xprt);
1612 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1613 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1615 status = xs_tcp_finish_connecting(xprt, sock);
1616 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1617 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1625 /* retry with existing socket, after a delay */
1628 /* get rid of existing socket, and retry */
1634 xprt_wake_pending_tasks(xprt, status);
1636 xprt_clear_connecting(xprt);
1640 * xs_connect - connect a socket to a remote endpoint
1641 * @task: address of RPC task that manages state of connect request
1643 * TCP: If the remote end dropped the connection, delay reconnecting.
1645 * UDP socket connects are synchronous, but we use a work queue anyway
1646 * to guarantee that even unprivileged user processes can set up a
1647 * socket on a privileged port.
1649 * If a UDP socket connect fails, the delay behavior here prevents
1650 * retry floods (hard mounts).
1652 static void xs_connect(struct rpc_task *task)
1654 struct rpc_xprt *xprt = task->tk_xprt;
1655 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1657 if (xprt_test_and_set_connecting(xprt))
1660 if (transport->sock != NULL) {
1661 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1663 xprt, xprt->reestablish_timeout / HZ);
1664 queue_delayed_work(rpciod_workqueue,
1665 &transport->connect_worker,
1666 xprt->reestablish_timeout);
1667 xprt->reestablish_timeout <<= 1;
1668 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1669 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
1671 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1672 queue_delayed_work(rpciod_workqueue,
1673 &transport->connect_worker, 0);
1678 * xs_udp_print_stats - display UDP socket-specifc stats
1679 * @xprt: rpc_xprt struct containing statistics
1683 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1685 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1687 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1689 xprt->stat.bind_count,
1692 xprt->stat.bad_xids,
1694 xprt->stat.bklog_u);
1698 * xs_tcp_print_stats - display TCP socket-specifc stats
1699 * @xprt: rpc_xprt struct containing statistics
1703 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1705 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1708 if (xprt_connected(xprt))
1709 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1711 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1713 xprt->stat.bind_count,
1714 xprt->stat.connect_count,
1715 xprt->stat.connect_time,
1719 xprt->stat.bad_xids,
1721 xprt->stat.bklog_u);
1724 static struct rpc_xprt_ops xs_udp_ops = {
1725 .set_buffer_size = xs_udp_set_buffer_size,
1726 .reserve_xprt = xprt_reserve_xprt_cong,
1727 .release_xprt = xprt_release_xprt_cong,
1728 .rpcbind = rpcb_getport_async,
1729 .set_port = xs_set_port,
1730 .connect = xs_connect,
1731 .buf_alloc = rpc_malloc,
1732 .buf_free = rpc_free,
1733 .send_request = xs_udp_send_request,
1734 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
1735 .timer = xs_udp_timer,
1736 .release_request = xprt_release_rqst_cong,
1738 .destroy = xs_destroy,
1739 .print_stats = xs_udp_print_stats,
1742 static struct rpc_xprt_ops xs_tcp_ops = {
1743 .reserve_xprt = xprt_reserve_xprt,
1744 .release_xprt = xs_tcp_release_xprt,
1745 .rpcbind = rpcb_getport_async,
1746 .set_port = xs_set_port,
1747 .connect = xs_connect,
1748 .buf_alloc = rpc_malloc,
1749 .buf_free = rpc_free,
1750 .send_request = xs_tcp_send_request,
1751 .set_retrans_timeout = xprt_set_retrans_timeout_def,
1753 .destroy = xs_destroy,
1754 .print_stats = xs_tcp_print_stats,
1757 static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args, unsigned int slot_table_size)
1759 struct rpc_xprt *xprt;
1760 struct sock_xprt *new;
1762 if (args->addrlen > sizeof(xprt->addr)) {
1763 dprintk("RPC: xs_setup_xprt: address too large\n");
1764 return ERR_PTR(-EBADF);
1767 new = kzalloc(sizeof(*new), GFP_KERNEL);
1769 dprintk("RPC: xs_setup_xprt: couldn't allocate "
1771 return ERR_PTR(-ENOMEM);
1775 xprt->max_reqs = slot_table_size;
1776 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1777 if (xprt->slot == NULL) {
1779 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
1781 return ERR_PTR(-ENOMEM);
1784 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
1785 xprt->addrlen = args->addrlen;
1787 memcpy(&new->addr, args->srcaddr, args->addrlen);
1788 new->port = xs_get_random_port();
1794 * xs_setup_udp - Set up transport to use a UDP socket
1795 * @args: rpc transport creation arguments
1798 struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args)
1800 struct sockaddr *addr = args->dstaddr;
1801 struct rpc_xprt *xprt;
1802 struct sock_xprt *transport;
1804 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
1807 transport = container_of(xprt, struct sock_xprt, xprt);
1809 xprt->prot = IPPROTO_UDP;
1811 /* XXX: header size can vary due to auth type, IPv6, etc. */
1812 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1814 xprt->bind_timeout = XS_BIND_TO;
1815 xprt->connect_timeout = XS_UDP_CONN_TO;
1816 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1817 xprt->idle_timeout = XS_IDLE_DISC_TO;
1819 xprt->ops = &xs_udp_ops;
1822 xprt->timeout = *args->timeout;
1824 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
1826 switch (addr->sa_family) {
1828 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1829 xprt_set_bound(xprt);
1831 INIT_DELAYED_WORK(&transport->connect_worker,
1832 xs_udp_connect_worker4);
1833 xs_format_ipv4_peer_addresses(xprt);
1836 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1837 xprt_set_bound(xprt);
1839 INIT_DELAYED_WORK(&transport->connect_worker,
1840 xs_udp_connect_worker6);
1841 xs_format_ipv6_peer_addresses(xprt);
1845 return ERR_PTR(-EAFNOSUPPORT);
1848 dprintk("RPC: set up transport to address %s\n",
1849 xprt->address_strings[RPC_DISPLAY_ALL]);
1855 * xs_setup_tcp - Set up transport to use a TCP socket
1856 * @args: rpc transport creation arguments
1859 struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args)
1861 struct sockaddr *addr = args->dstaddr;
1862 struct rpc_xprt *xprt;
1863 struct sock_xprt *transport;
1865 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
1868 transport = container_of(xprt, struct sock_xprt, xprt);
1870 xprt->prot = IPPROTO_TCP;
1871 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
1872 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
1874 xprt->bind_timeout = XS_BIND_TO;
1875 xprt->connect_timeout = XS_TCP_CONN_TO;
1876 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1877 xprt->idle_timeout = XS_IDLE_DISC_TO;
1879 xprt->ops = &xs_tcp_ops;
1882 xprt->timeout = *args->timeout;
1884 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
1886 switch (addr->sa_family) {
1888 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1889 xprt_set_bound(xprt);
1891 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
1892 xs_format_ipv4_peer_addresses(xprt);
1895 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1896 xprt_set_bound(xprt);
1898 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
1899 xs_format_ipv6_peer_addresses(xprt);
1903 return ERR_PTR(-EAFNOSUPPORT);
1906 dprintk("RPC: set up transport to address %s\n",
1907 xprt->address_strings[RPC_DISPLAY_ALL]);
1913 * init_socket_xprt - set up xprtsock's sysctls
1916 int init_socket_xprt(void)
1919 if (!sunrpc_table_header)
1920 sunrpc_table_header = register_sysctl_table(sunrpc_table);
1927 * cleanup_socket_xprt - remove xprtsock's sysctls
1930 void cleanup_socket_xprt(void)
1933 if (sunrpc_table_header) {
1934 unregister_sysctl_table(sunrpc_table_header);
1935 sunrpc_table_header = NULL;