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>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/capability.h>
21 #include <linux/sched.h>
22 #include <linux/pagemap.h>
23 #include <linux/errno.h>
24 #include <linux/socket.h>
26 #include <linux/net.h>
28 #include <linux/udp.h>
29 #include <linux/tcp.h>
30 #include <linux/sunrpc/clnt.h>
31 #include <linux/sunrpc/sched.h>
32 #include <linux/file.h>
35 #include <net/checksum.h>
42 unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
43 unsigned int xprt_tcp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
45 unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
46 unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
49 * How many times to try sending a request on a socket before waiting
50 * for the socket buffer to clear.
52 #define XS_SENDMSG_RETRY (10U)
55 * Time out for an RPC UDP socket connect. UDP socket connects are
56 * synchronous, but we set a timeout anyway in case of resource
57 * exhaustion on the local host.
59 #define XS_UDP_CONN_TO (5U * HZ)
62 * Wait duration for an RPC TCP connection to be established. Solaris
63 * NFS over TCP uses 60 seconds, for example, which is in line with how
64 * long a server takes to reboot.
66 #define XS_TCP_CONN_TO (60U * HZ)
69 * Wait duration for a reply from the RPC portmapper.
71 #define XS_BIND_TO (60U * HZ)
74 * Delay if a UDP socket connect error occurs. This is most likely some
75 * kind of resource problem on the local host.
77 #define XS_UDP_REEST_TO (2U * HZ)
80 * The reestablish timeout allows clients to delay for a bit before attempting
81 * to reconnect to a server that just dropped our connection.
83 * We implement an exponential backoff when trying to reestablish a TCP
84 * transport connection with the server. Some servers like to drop a TCP
85 * connection when they are overworked, so we start with a short timeout and
86 * increase over time if the server is down or not responding.
88 #define XS_TCP_INIT_REEST_TO (3U * HZ)
89 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
92 * TCP idle timeout; client drops the transport socket if it is idle
93 * for this long. Note that we also timeout UDP sockets to prevent
94 * holding port numbers when there is no RPC traffic.
96 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
99 # undef RPC_DEBUG_DATA
100 # define RPCDBG_FACILITY RPCDBG_TRANS
103 #ifdef RPC_DEBUG_DATA
104 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
106 u8
*buf
= (u8
*) packet
;
109 dprintk("RPC: %s\n", msg
);
110 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
114 dprintk("0x%04x ", j
);
116 dprintk("%02x%02x%02x%02x ",
117 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
122 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
128 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
130 static inline int xs_send_head(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
, unsigned int len
)
133 .iov_base
= xdr
->head
[0].iov_base
+ base
,
134 .iov_len
= len
- base
,
136 struct msghdr msg
= {
138 .msg_namelen
= addrlen
,
139 .msg_flags
= XS_SENDMSG_FLAGS
,
143 msg
.msg_flags
|= MSG_MORE
;
145 if (likely(iov
.iov_len
))
146 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
147 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
150 static int xs_send_tail(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, unsigned int len
)
153 .iov_base
= xdr
->tail
[0].iov_base
+ base
,
154 .iov_len
= len
- base
,
156 struct msghdr msg
= {
157 .msg_flags
= XS_SENDMSG_FLAGS
,
160 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
164 * xs_sendpages - write pages directly to a socket
165 * @sock: socket to send on
166 * @addr: UDP only -- address of destination
167 * @addrlen: UDP only -- length of destination address
168 * @xdr: buffer containing this request
169 * @base: starting position in the buffer
172 static inline int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
174 struct page
**ppage
= xdr
->pages
;
175 unsigned int len
, pglen
= xdr
->page_len
;
177 ssize_t (*sendpage
)(struct socket
*, struct page
*, int, size_t, int);
182 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
184 len
= xdr
->head
[0].iov_len
;
185 if (base
< len
|| (addr
!= NULL
&& base
== 0)) {
186 err
= xs_send_head(sock
, addr
, addrlen
, xdr
, base
, len
);
191 if (err
!= (len
- base
))
197 if (unlikely(pglen
== 0))
199 if (unlikely(base
>= pglen
)) {
203 if (base
|| xdr
->page_base
) {
205 base
+= xdr
->page_base
;
206 ppage
+= base
>> PAGE_CACHE_SHIFT
;
207 base
&= ~PAGE_CACHE_MASK
;
210 sendpage
= sock
->ops
->sendpage
? : sock_no_sendpage
;
212 int flags
= XS_SENDMSG_FLAGS
;
214 len
= PAGE_CACHE_SIZE
;
220 if (pglen
!= len
|| xdr
->tail
[0].iov_len
!= 0)
223 /* Hmm... We might be dealing with highmem pages */
224 if (PageHighMem(*ppage
))
225 sendpage
= sock_no_sendpage
;
226 err
= sendpage(sock
, *ppage
, base
, len
, flags
);
235 } while ((pglen
-= len
) != 0);
237 len
= xdr
->tail
[0].iov_len
;
239 err
= xs_send_tail(sock
, xdr
, base
, len
);
250 * xs_nospace - place task on wait queue if transmit was incomplete
251 * @task: task to put to sleep
254 static void xs_nospace(struct rpc_task
*task
)
256 struct rpc_rqst
*req
= task
->tk_rqstp
;
257 struct rpc_xprt
*xprt
= req
->rq_xprt
;
259 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
260 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
263 if (test_bit(SOCK_ASYNC_NOSPACE
, &xprt
->sock
->flags
)) {
264 /* Protect against races with write_space */
265 spin_lock_bh(&xprt
->transport_lock
);
267 /* Don't race with disconnect */
268 if (!xprt_connected(xprt
))
269 task
->tk_status
= -ENOTCONN
;
270 else if (test_bit(SOCK_NOSPACE
, &xprt
->sock
->flags
))
271 xprt_wait_for_buffer_space(task
);
273 spin_unlock_bh(&xprt
->transport_lock
);
275 /* Keep holding the socket if it is blocked */
276 rpc_delay(task
, HZ
>>4);
280 * xs_udp_send_request - write an RPC request to a UDP socket
281 * @task: address of RPC task that manages the state of an RPC request
284 * 0: The request has been sent
285 * EAGAIN: The socket was blocked, please call again later to
286 * complete the request
287 * ENOTCONN: Caller needs to invoke connect logic then call again
288 * other: Some other error occured, the request was not sent
290 static int xs_udp_send_request(struct rpc_task
*task
)
292 struct rpc_rqst
*req
= task
->tk_rqstp
;
293 struct rpc_xprt
*xprt
= req
->rq_xprt
;
294 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
297 xs_pktdump("packet data:",
298 req
->rq_svec
->iov_base
,
299 req
->rq_svec
->iov_len
);
301 req
->rq_xtime
= jiffies
;
302 status
= xs_sendpages(xprt
->sock
, (struct sockaddr
*) &xprt
->addr
,
303 sizeof(xprt
->addr
), xdr
, req
->rq_bytes_sent
);
305 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
306 xdr
->len
- req
->rq_bytes_sent
, status
);
308 if (likely(status
>= (int) req
->rq_slen
))
311 /* Still some bytes left; set up for a retry later. */
319 /* When the server has died, an ICMP port unreachable message
320 * prompts ECONNREFUSED. */
326 dprintk("RPC: sendmsg returned unrecognized error %d\n",
334 static inline void xs_encode_tcp_record_marker(struct xdr_buf
*buf
)
336 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
337 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
338 *base
= htonl(RPC_LAST_STREAM_FRAGMENT
| reclen
);
342 * xs_tcp_send_request - write an RPC request to a TCP socket
343 * @task: address of RPC task that manages the state of an RPC request
346 * 0: The request has been sent
347 * EAGAIN: The socket was blocked, please call again later to
348 * complete the request
349 * ENOTCONN: Caller needs to invoke connect logic then call again
350 * other: Some other error occured, the request was not sent
352 * XXX: In the case of soft timeouts, should we eventually give up
353 * if sendmsg is not able to make progress?
355 static int xs_tcp_send_request(struct rpc_task
*task
)
357 struct rpc_rqst
*req
= task
->tk_rqstp
;
358 struct rpc_xprt
*xprt
= req
->rq_xprt
;
359 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
360 int status
, retry
= 0;
362 xs_encode_tcp_record_marker(&req
->rq_snd_buf
);
364 xs_pktdump("packet data:",
365 req
->rq_svec
->iov_base
,
366 req
->rq_svec
->iov_len
);
368 /* Continue transmitting the packet/record. We must be careful
369 * to cope with writespace callbacks arriving _after_ we have
370 * called sendmsg(). */
372 req
->rq_xtime
= jiffies
;
373 status
= xs_sendpages(xprt
->sock
, NULL
, 0, xdr
,
376 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
377 xdr
->len
- req
->rq_bytes_sent
, status
);
379 if (unlikely(status
< 0))
382 /* If we've sent the entire packet, immediately
383 * reset the count of bytes sent. */
384 req
->rq_bytes_sent
+= status
;
385 task
->tk_bytes_sent
+= status
;
386 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
387 req
->rq_bytes_sent
= 0;
392 if (retry
++ > XS_SENDMSG_RETRY
)
407 dprintk("RPC: sendmsg returned unrecognized error %d\n",
409 xprt_disconnect(xprt
);
417 * xs_tcp_release_xprt - clean up after a tcp transmission
421 * This cleans up if an error causes us to abort the transmission of a request.
422 * In this case, the socket may need to be reset in order to avoid confusing
425 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
427 struct rpc_rqst
*req
;
429 if (task
!= xprt
->snd_task
)
433 req
= task
->tk_rqstp
;
434 if (req
->rq_bytes_sent
== 0)
436 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
438 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
440 xprt_release_xprt(xprt
, task
);
444 * xs_close - close a socket
447 * This is used when all requests are complete; ie, no DRC state remains
448 * on the server we want to save.
450 static void xs_close(struct rpc_xprt
*xprt
)
452 struct socket
*sock
= xprt
->sock
;
453 struct sock
*sk
= xprt
->inet
;
456 goto clear_close_wait
;
458 dprintk("RPC: xs_close xprt %p\n", xprt
);
460 write_lock_bh(&sk
->sk_callback_lock
);
464 sk
->sk_user_data
= NULL
;
465 sk
->sk_data_ready
= xprt
->old_data_ready
;
466 sk
->sk_state_change
= xprt
->old_state_change
;
467 sk
->sk_write_space
= xprt
->old_write_space
;
468 write_unlock_bh(&sk
->sk_callback_lock
);
474 smp_mb__before_clear_bit();
475 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
476 smp_mb__after_clear_bit();
480 * xs_destroy - prepare to shutdown a transport
481 * @xprt: doomed transport
484 static void xs_destroy(struct rpc_xprt
*xprt
)
486 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
488 cancel_delayed_work(&xprt
->connect_worker
);
489 flush_scheduled_work();
491 xprt_disconnect(xprt
);
496 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
498 return (struct rpc_xprt
*) sk
->sk_user_data
;
502 * xs_udp_data_ready - "data ready" callback for UDP sockets
503 * @sk: socket with data to read
504 * @len: how much data to read
507 static void xs_udp_data_ready(struct sock
*sk
, int len
)
509 struct rpc_task
*task
;
510 struct rpc_xprt
*xprt
;
511 struct rpc_rqst
*rovr
;
513 int err
, repsize
, copied
;
516 read_lock(&sk
->sk_callback_lock
);
517 dprintk("RPC: xs_udp_data_ready...\n");
518 if (!(xprt
= xprt_from_sock(sk
)))
521 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
527 repsize
= skb
->len
- sizeof(struct udphdr
);
529 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
533 /* Copy the XID from the skb... */
534 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
535 sizeof(_xid
), &_xid
);
539 /* Look up and lock the request corresponding to the given XID */
540 spin_lock(&xprt
->transport_lock
);
541 rovr
= xprt_lookup_rqst(xprt
, *xp
);
544 task
= rovr
->rq_task
;
546 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
549 /* Suck it into the iovec, verify checksum if not done by hw. */
550 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
))
553 /* Something worked... */
554 dst_confirm(skb
->dst
);
556 xprt_adjust_cwnd(task
, copied
);
557 xprt_update_rtt(task
);
558 xprt_complete_rqst(task
, copied
);
561 spin_unlock(&xprt
->transport_lock
);
563 skb_free_datagram(sk
, skb
);
565 read_unlock(&sk
->sk_callback_lock
);
568 static inline size_t xs_tcp_copy_data(skb_reader_t
*desc
, void *p
, size_t len
)
570 if (len
> desc
->count
)
572 if (skb_copy_bits(desc
->skb
, desc
->offset
, p
, len
)) {
573 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
579 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
584 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
589 p
= ((char *) &xprt
->tcp_recm
) + xprt
->tcp_offset
;
590 len
= sizeof(xprt
->tcp_recm
) - xprt
->tcp_offset
;
591 used
= xs_tcp_copy_data(desc
, p
, len
);
592 xprt
->tcp_offset
+= used
;
596 xprt
->tcp_reclen
= ntohl(xprt
->tcp_recm
);
597 if (xprt
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
598 xprt
->tcp_flags
|= XPRT_LAST_FRAG
;
600 xprt
->tcp_flags
&= ~XPRT_LAST_FRAG
;
601 xprt
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
603 xprt
->tcp_flags
&= ~XPRT_COPY_RECM
;
604 xprt
->tcp_offset
= 0;
606 /* Sanity check of the record length */
607 if (unlikely(xprt
->tcp_reclen
< 4)) {
608 dprintk("RPC: invalid TCP record fragment length\n");
609 xprt_disconnect(xprt
);
612 dprintk("RPC: reading TCP record fragment of length %d\n",
616 static void xs_tcp_check_recm(struct rpc_xprt
*xprt
)
618 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
619 xprt
, xprt
->tcp_copied
, xprt
->tcp_offset
, xprt
->tcp_reclen
, xprt
->tcp_flags
);
620 if (xprt
->tcp_offset
== xprt
->tcp_reclen
) {
621 xprt
->tcp_flags
|= XPRT_COPY_RECM
;
622 xprt
->tcp_offset
= 0;
623 if (xprt
->tcp_flags
& XPRT_LAST_FRAG
) {
624 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
625 xprt
->tcp_flags
|= XPRT_COPY_XID
;
626 xprt
->tcp_copied
= 0;
631 static inline void xs_tcp_read_xid(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
636 len
= sizeof(xprt
->tcp_xid
) - xprt
->tcp_offset
;
637 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
638 p
= ((char *) &xprt
->tcp_xid
) + xprt
->tcp_offset
;
639 used
= xs_tcp_copy_data(desc
, p
, len
);
640 xprt
->tcp_offset
+= used
;
643 xprt
->tcp_flags
&= ~XPRT_COPY_XID
;
644 xprt
->tcp_flags
|= XPRT_COPY_DATA
;
645 xprt
->tcp_copied
= 4;
646 dprintk("RPC: reading reply for XID %08x\n",
647 ntohl(xprt
->tcp_xid
));
648 xs_tcp_check_recm(xprt
);
651 static inline void xs_tcp_read_request(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
653 struct rpc_rqst
*req
;
654 struct xdr_buf
*rcvbuf
;
658 /* Find and lock the request corresponding to this xid */
659 spin_lock(&xprt
->transport_lock
);
660 req
= xprt_lookup_rqst(xprt
, xprt
->tcp_xid
);
662 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
663 dprintk("RPC: XID %08x request not found!\n",
664 ntohl(xprt
->tcp_xid
));
665 spin_unlock(&xprt
->transport_lock
);
669 rcvbuf
= &req
->rq_private_buf
;
671 if (len
> xprt
->tcp_reclen
- xprt
->tcp_offset
) {
672 skb_reader_t my_desc
;
674 len
= xprt
->tcp_reclen
- xprt
->tcp_offset
;
675 memcpy(&my_desc
, desc
, sizeof(my_desc
));
677 r
= xdr_partial_copy_from_skb(rcvbuf
, xprt
->tcp_copied
,
678 &my_desc
, xs_tcp_copy_data
);
682 r
= xdr_partial_copy_from_skb(rcvbuf
, xprt
->tcp_copied
,
683 desc
, xs_tcp_copy_data
);
686 xprt
->tcp_copied
+= r
;
687 xprt
->tcp_offset
+= r
;
690 /* Error when copying to the receive buffer,
691 * usually because we weren't able to allocate
692 * additional buffer pages. All we can do now
693 * is turn off XPRT_COPY_DATA, so the request
694 * will not receive any additional updates,
696 * Any remaining data from this record will
699 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
700 dprintk("RPC: XID %08x truncated request\n",
701 ntohl(xprt
->tcp_xid
));
702 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
703 xprt
, xprt
->tcp_copied
, xprt
->tcp_offset
, xprt
->tcp_reclen
);
707 dprintk("RPC: XID %08x read %Zd bytes\n",
708 ntohl(xprt
->tcp_xid
), r
);
709 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
710 xprt
, xprt
->tcp_copied
, xprt
->tcp_offset
, xprt
->tcp_reclen
);
712 if (xprt
->tcp_copied
== req
->rq_private_buf
.buflen
)
713 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
714 else if (xprt
->tcp_offset
== xprt
->tcp_reclen
) {
715 if (xprt
->tcp_flags
& XPRT_LAST_FRAG
)
716 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
720 if (!(xprt
->tcp_flags
& XPRT_COPY_DATA
))
721 xprt_complete_rqst(req
->rq_task
, xprt
->tcp_copied
);
722 spin_unlock(&xprt
->transport_lock
);
723 xs_tcp_check_recm(xprt
);
726 static inline void xs_tcp_read_discard(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
730 len
= xprt
->tcp_reclen
- xprt
->tcp_offset
;
731 if (len
> desc
->count
)
735 xprt
->tcp_offset
+= len
;
736 dprintk("RPC: discarded %Zu bytes\n", len
);
737 xs_tcp_check_recm(xprt
);
740 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
742 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
743 skb_reader_t desc
= {
750 dprintk("RPC: xs_tcp_data_recv started\n");
752 /* Read in a new fragment marker if necessary */
753 /* Can we ever really expect to get completely empty fragments? */
754 if (xprt
->tcp_flags
& XPRT_COPY_RECM
) {
755 xs_tcp_read_fraghdr(xprt
, &desc
);
758 /* Read in the xid if necessary */
759 if (xprt
->tcp_flags
& XPRT_COPY_XID
) {
760 xs_tcp_read_xid(xprt
, &desc
);
763 /* Read in the request data */
764 if (xprt
->tcp_flags
& XPRT_COPY_DATA
) {
765 xs_tcp_read_request(xprt
, &desc
);
768 /* Skip over any trailing bytes on short reads */
769 xs_tcp_read_discard(xprt
, &desc
);
770 } while (desc
.count
);
771 dprintk("RPC: xs_tcp_data_recv done\n");
772 return len
- desc
.count
;
776 * xs_tcp_data_ready - "data ready" callback for TCP sockets
777 * @sk: socket with data to read
778 * @bytes: how much data to read
781 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
783 struct rpc_xprt
*xprt
;
784 read_descriptor_t rd_desc
;
786 read_lock(&sk
->sk_callback_lock
);
787 dprintk("RPC: xs_tcp_data_ready...\n");
788 if (!(xprt
= xprt_from_sock(sk
)))
793 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
794 rd_desc
.arg
.data
= xprt
;
795 rd_desc
.count
= 65536;
796 tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
798 read_unlock(&sk
->sk_callback_lock
);
802 * xs_tcp_state_change - callback to handle TCP socket state changes
803 * @sk: socket whose state has changed
806 static void xs_tcp_state_change(struct sock
*sk
)
808 struct rpc_xprt
*xprt
;
810 read_lock(&sk
->sk_callback_lock
);
811 if (!(xprt
= xprt_from_sock(sk
)))
813 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
814 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
815 sk
->sk_state
, xprt_connected(xprt
),
816 sock_flag(sk
, SOCK_DEAD
),
817 sock_flag(sk
, SOCK_ZAPPED
));
819 switch (sk
->sk_state
) {
820 case TCP_ESTABLISHED
:
821 spin_lock_bh(&xprt
->transport_lock
);
822 if (!xprt_test_and_set_connected(xprt
)) {
823 /* Reset TCP record info */
824 xprt
->tcp_offset
= 0;
825 xprt
->tcp_reclen
= 0;
826 xprt
->tcp_copied
= 0;
827 xprt
->tcp_flags
= XPRT_COPY_RECM
| XPRT_COPY_XID
;
828 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
829 xprt_wake_pending_tasks(xprt
, 0);
831 spin_unlock_bh(&xprt
->transport_lock
);
837 /* Try to schedule an autoclose RPC calls */
838 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
839 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
) == 0)
840 schedule_work(&xprt
->task_cleanup
);
842 xprt_disconnect(xprt
);
845 read_unlock(&sk
->sk_callback_lock
);
849 * xs_udp_write_space - callback invoked when socket buffer space
851 * @sk: socket whose state has changed
853 * Called when more output buffer space is available for this socket.
854 * We try not to wake our writers until they can make "significant"
855 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
856 * with a bunch of small requests.
858 static void xs_udp_write_space(struct sock
*sk
)
860 read_lock(&sk
->sk_callback_lock
);
862 /* from net/core/sock.c:sock_def_write_space */
863 if (sock_writeable(sk
)) {
865 struct rpc_xprt
*xprt
;
867 if (unlikely(!(sock
= sk
->sk_socket
)))
869 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
871 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE
, &sock
->flags
)))
874 xprt_write_space(xprt
);
878 read_unlock(&sk
->sk_callback_lock
);
882 * xs_tcp_write_space - callback invoked when socket buffer space
884 * @sk: socket whose state has changed
886 * Called when more output buffer space is available for this socket.
887 * We try not to wake our writers until they can make "significant"
888 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
889 * with a bunch of small requests.
891 static void xs_tcp_write_space(struct sock
*sk
)
893 read_lock(&sk
->sk_callback_lock
);
895 /* from net/core/stream.c:sk_stream_write_space */
896 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
)) {
898 struct rpc_xprt
*xprt
;
900 if (unlikely(!(sock
= sk
->sk_socket
)))
902 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
904 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE
, &sock
->flags
)))
907 xprt_write_space(xprt
);
911 read_unlock(&sk
->sk_callback_lock
);
914 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
916 struct sock
*sk
= xprt
->inet
;
919 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
920 sk
->sk_rcvbuf
= xprt
->rcvsize
* xprt
->max_reqs
* 2;
923 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
924 sk
->sk_sndbuf
= xprt
->sndsize
* xprt
->max_reqs
* 2;
925 sk
->sk_write_space(sk
);
930 * xs_udp_set_buffer_size - set send and receive limits
931 * @xprt: generic transport
932 * @sndsize: requested size of send buffer, in bytes
933 * @rcvsize: requested size of receive buffer, in bytes
935 * Set socket send and receive buffer size limits.
937 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
941 xprt
->sndsize
= sndsize
+ 1024;
944 xprt
->rcvsize
= rcvsize
+ 1024;
946 xs_udp_do_set_buffer_size(xprt
);
950 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
951 * @task: task that timed out
953 * Adjust the congestion window after a retransmit timeout has occurred.
955 static void xs_udp_timer(struct rpc_task
*task
)
957 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
960 static unsigned short xs_get_random_port(void)
962 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
963 unsigned short rand
= (unsigned short) net_random() % range
;
964 return rand
+ xprt_min_resvport
;
968 * xs_set_port - reset the port number in the remote endpoint address
969 * @xprt: generic transport
970 * @port: new port number
973 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
975 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
976 xprt
->addr
.sin_port
= htons(port
);
979 static int xs_bindresvport(struct rpc_xprt
*xprt
, struct socket
*sock
)
981 struct sockaddr_in myaddr
= {
982 .sin_family
= AF_INET
,
985 unsigned short port
= xprt
->port
;
988 myaddr
.sin_port
= htons(port
);
989 err
= sock
->ops
->bind(sock
, (struct sockaddr
*) &myaddr
,
993 dprintk("RPC: xs_bindresvport bound to port %u\n",
997 if (port
<= xprt_min_resvport
)
998 port
= xprt_max_resvport
;
1001 } while (err
== -EADDRINUSE
&& port
!= xprt
->port
);
1003 dprintk("RPC: can't bind to reserved port (%d).\n", -err
);
1008 * xs_udp_connect_worker - set up a UDP socket
1009 * @args: RPC transport to connect
1011 * Invoked by a work queue tasklet.
1013 static void xs_udp_connect_worker(void *args
)
1015 struct rpc_xprt
*xprt
= (struct rpc_xprt
*) args
;
1016 struct socket
*sock
= xprt
->sock
;
1017 int err
, status
= -EIO
;
1019 if (xprt
->shutdown
|| xprt
->addr
.sin_port
== 0)
1022 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt
);
1024 /* Start by resetting any existing state */
1027 if ((err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
)) < 0) {
1028 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1032 if (xprt
->resvport
&& xs_bindresvport(xprt
, sock
) < 0) {
1038 struct sock
*sk
= sock
->sk
;
1040 write_lock_bh(&sk
->sk_callback_lock
);
1042 sk
->sk_user_data
= xprt
;
1043 xprt
->old_data_ready
= sk
->sk_data_ready
;
1044 xprt
->old_state_change
= sk
->sk_state_change
;
1045 xprt
->old_write_space
= sk
->sk_write_space
;
1046 sk
->sk_data_ready
= xs_udp_data_ready
;
1047 sk
->sk_write_space
= xs_udp_write_space
;
1048 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1049 sk
->sk_allocation
= GFP_ATOMIC
;
1051 xprt_set_connected(xprt
);
1053 /* Reset to new socket */
1057 write_unlock_bh(&sk
->sk_callback_lock
);
1059 xs_udp_do_set_buffer_size(xprt
);
1062 xprt_wake_pending_tasks(xprt
, status
);
1063 xprt_clear_connecting(xprt
);
1067 * We need to preserve the port number so the reply cache on the server can
1068 * find our cached RPC replies when we get around to reconnecting.
1070 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
)
1073 struct socket
*sock
= xprt
->sock
;
1074 struct sockaddr any
;
1076 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1079 * Disconnect the transport socket by doing a connect operation
1080 * with AF_UNSPEC. This should return immediately...
1082 memset(&any
, 0, sizeof(any
));
1083 any
.sa_family
= AF_UNSPEC
;
1084 result
= sock
->ops
->connect(sock
, &any
, sizeof(any
), 0);
1086 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1091 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1092 * @args: RPC transport to connect
1094 * Invoked by a work queue tasklet.
1096 static void xs_tcp_connect_worker(void *args
)
1098 struct rpc_xprt
*xprt
= (struct rpc_xprt
*)args
;
1099 struct socket
*sock
= xprt
->sock
;
1100 int err
, status
= -EIO
;
1102 if (xprt
->shutdown
|| xprt
->addr
.sin_port
== 0)
1105 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt
);
1108 /* start from scratch */
1109 if ((err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
)) < 0) {
1110 dprintk("RPC: can't create TCP transport socket (%d).\n", -err
);
1114 if (xprt
->resvport
&& xs_bindresvport(xprt
, sock
) < 0) {
1119 /* "close" the socket, preserving the local port */
1120 xs_tcp_reuse_connection(xprt
);
1123 struct sock
*sk
= sock
->sk
;
1125 write_lock_bh(&sk
->sk_callback_lock
);
1127 sk
->sk_user_data
= xprt
;
1128 xprt
->old_data_ready
= sk
->sk_data_ready
;
1129 xprt
->old_state_change
= sk
->sk_state_change
;
1130 xprt
->old_write_space
= sk
->sk_write_space
;
1131 sk
->sk_data_ready
= xs_tcp_data_ready
;
1132 sk
->sk_state_change
= xs_tcp_state_change
;
1133 sk
->sk_write_space
= xs_tcp_write_space
;
1134 sk
->sk_allocation
= GFP_ATOMIC
;
1136 /* socket options */
1137 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1138 sock_reset_flag(sk
, SOCK_LINGER
);
1139 tcp_sk(sk
)->linger2
= 0;
1140 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1142 xprt_clear_connected(xprt
);
1144 /* Reset to new socket */
1148 write_unlock_bh(&sk
->sk_callback_lock
);
1151 /* Tell the socket layer to start connecting... */
1152 xprt
->stat
.connect_count
++;
1153 xprt
->stat
.connect_start
= jiffies
;
1154 status
= sock
->ops
->connect(sock
, (struct sockaddr
*) &xprt
->addr
,
1155 sizeof(xprt
->addr
), O_NONBLOCK
);
1156 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1157 xprt
, -status
, xprt_connected(xprt
), sock
->sk
->sk_state
);
1165 /* retry with existing socket, after a delay */
1168 /* get rid of existing socket, and retry */
1174 xprt_wake_pending_tasks(xprt
, status
);
1176 xprt_clear_connecting(xprt
);
1180 * xs_connect - connect a socket to a remote endpoint
1181 * @task: address of RPC task that manages state of connect request
1183 * TCP: If the remote end dropped the connection, delay reconnecting.
1185 * UDP socket connects are synchronous, but we use a work queue anyway
1186 * to guarantee that even unprivileged user processes can set up a
1187 * socket on a privileged port.
1189 * If a UDP socket connect fails, the delay behavior here prevents
1190 * retry floods (hard mounts).
1192 static void xs_connect(struct rpc_task
*task
)
1194 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1196 if (xprt_test_and_set_connecting(xprt
))
1199 if (xprt
->sock
!= NULL
) {
1200 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1201 xprt
, xprt
->reestablish_timeout
/ HZ
);
1202 schedule_delayed_work(&xprt
->connect_worker
,
1203 xprt
->reestablish_timeout
);
1204 xprt
->reestablish_timeout
<<= 1;
1205 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
1206 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
1208 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
1209 schedule_work(&xprt
->connect_worker
);
1211 /* flush_scheduled_work can sleep... */
1212 if (!RPC_IS_ASYNC(task
))
1213 flush_scheduled_work();
1218 * xs_udp_print_stats - display UDP socket-specifc stats
1219 * @xprt: rpc_xprt struct containing statistics
1223 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
1225 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1227 xprt
->stat
.bind_count
,
1230 xprt
->stat
.bad_xids
,
1232 xprt
->stat
.bklog_u
);
1236 * xs_tcp_print_stats - display TCP socket-specifc stats
1237 * @xprt: rpc_xprt struct containing statistics
1241 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
1245 if (xprt_connected(xprt
))
1246 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
1248 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1250 xprt
->stat
.bind_count
,
1251 xprt
->stat
.connect_count
,
1252 xprt
->stat
.connect_time
,
1256 xprt
->stat
.bad_xids
,
1258 xprt
->stat
.bklog_u
);
1261 static struct rpc_xprt_ops xs_udp_ops
= {
1262 .set_buffer_size
= xs_udp_set_buffer_size
,
1263 .reserve_xprt
= xprt_reserve_xprt_cong
,
1264 .release_xprt
= xprt_release_xprt_cong
,
1265 .set_port
= xs_set_port
,
1266 .connect
= xs_connect
,
1267 .buf_alloc
= rpc_malloc
,
1268 .buf_free
= rpc_free
,
1269 .send_request
= xs_udp_send_request
,
1270 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
1271 .timer
= xs_udp_timer
,
1272 .release_request
= xprt_release_rqst_cong
,
1274 .destroy
= xs_destroy
,
1275 .print_stats
= xs_udp_print_stats
,
1278 static struct rpc_xprt_ops xs_tcp_ops
= {
1279 .reserve_xprt
= xprt_reserve_xprt
,
1280 .release_xprt
= xs_tcp_release_xprt
,
1281 .set_port
= xs_set_port
,
1282 .connect
= xs_connect
,
1283 .buf_alloc
= rpc_malloc
,
1284 .buf_free
= rpc_free
,
1285 .send_request
= xs_tcp_send_request
,
1286 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
1288 .destroy
= xs_destroy
,
1289 .print_stats
= xs_tcp_print_stats
,
1293 * xs_setup_udp - Set up transport to use a UDP socket
1294 * @xprt: transport to set up
1295 * @to: timeout parameters
1298 int xs_setup_udp(struct rpc_xprt
*xprt
, struct rpc_timeout
*to
)
1300 size_t slot_table_size
;
1302 dprintk("RPC: setting up udp-ipv4 transport...\n");
1304 xprt
->max_reqs
= xprt_udp_slot_table_entries
;
1305 slot_table_size
= xprt
->max_reqs
* sizeof(xprt
->slot
[0]);
1306 xprt
->slot
= kzalloc(slot_table_size
, GFP_KERNEL
);
1307 if (xprt
->slot
== NULL
)
1310 xprt
->prot
= IPPROTO_UDP
;
1311 xprt
->port
= xs_get_random_port();
1313 xprt
->resvport
= capable(CAP_NET_BIND_SERVICE
) ? 1 : 0;
1314 /* XXX: header size can vary due to auth type, IPv6, etc. */
1315 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
1317 INIT_WORK(&xprt
->connect_worker
, xs_udp_connect_worker
, xprt
);
1318 xprt
->bind_timeout
= XS_BIND_TO
;
1319 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
1320 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
1321 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
1323 xprt
->ops
= &xs_udp_ops
;
1326 xprt
->timeout
= *to
;
1328 xprt_set_timeout(&xprt
->timeout
, 5, 5 * HZ
);
1334 * xs_setup_tcp - Set up transport to use a TCP socket
1335 * @xprt: transport to set up
1336 * @to: timeout parameters
1339 int xs_setup_tcp(struct rpc_xprt
*xprt
, struct rpc_timeout
*to
)
1341 size_t slot_table_size
;
1343 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1345 xprt
->max_reqs
= xprt_tcp_slot_table_entries
;
1346 slot_table_size
= xprt
->max_reqs
* sizeof(xprt
->slot
[0]);
1347 xprt
->slot
= kzalloc(slot_table_size
, GFP_KERNEL
);
1348 if (xprt
->slot
== NULL
)
1351 xprt
->prot
= IPPROTO_TCP
;
1352 xprt
->port
= xs_get_random_port();
1353 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
1354 xprt
->resvport
= capable(CAP_NET_BIND_SERVICE
) ? 1 : 0;
1355 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
1357 INIT_WORK(&xprt
->connect_worker
, xs_tcp_connect_worker
, xprt
);
1358 xprt
->bind_timeout
= XS_BIND_TO
;
1359 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
1360 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1361 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
1363 xprt
->ops
= &xs_tcp_ops
;
1366 xprt
->timeout
= *to
;
1368 xprt_set_timeout(&xprt
->timeout
, 2, 60 * HZ
);