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_close - close a socket
420 * This is used when all requests are complete; ie, no DRC state remains
421 * on the server we want to save.
423 static void xs_close(struct rpc_xprt
*xprt
)
425 struct socket
*sock
= xprt
->sock
;
426 struct sock
*sk
= xprt
->inet
;
429 goto clear_close_wait
;
431 dprintk("RPC: xs_close xprt %p\n", xprt
);
433 write_lock_bh(&sk
->sk_callback_lock
);
437 sk
->sk_user_data
= NULL
;
438 sk
->sk_data_ready
= xprt
->old_data_ready
;
439 sk
->sk_state_change
= xprt
->old_state_change
;
440 sk
->sk_write_space
= xprt
->old_write_space
;
441 write_unlock_bh(&sk
->sk_callback_lock
);
447 smp_mb__before_clear_bit();
448 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
449 smp_mb__after_clear_bit();
453 * xs_destroy - prepare to shutdown a transport
454 * @xprt: doomed transport
457 static void xs_destroy(struct rpc_xprt
*xprt
)
459 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
461 cancel_delayed_work(&xprt
->connect_worker
);
462 flush_scheduled_work();
464 xprt_disconnect(xprt
);
469 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
471 return (struct rpc_xprt
*) sk
->sk_user_data
;
475 * xs_udp_data_ready - "data ready" callback for UDP sockets
476 * @sk: socket with data to read
477 * @len: how much data to read
480 static void xs_udp_data_ready(struct sock
*sk
, int len
)
482 struct rpc_task
*task
;
483 struct rpc_xprt
*xprt
;
484 struct rpc_rqst
*rovr
;
486 int err
, repsize
, copied
;
489 read_lock(&sk
->sk_callback_lock
);
490 dprintk("RPC: xs_udp_data_ready...\n");
491 if (!(xprt
= xprt_from_sock(sk
)))
494 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
500 repsize
= skb
->len
- sizeof(struct udphdr
);
502 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
506 /* Copy the XID from the skb... */
507 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
508 sizeof(_xid
), &_xid
);
512 /* Look up and lock the request corresponding to the given XID */
513 spin_lock(&xprt
->transport_lock
);
514 rovr
= xprt_lookup_rqst(xprt
, *xp
);
517 task
= rovr
->rq_task
;
519 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
522 /* Suck it into the iovec, verify checksum if not done by hw. */
523 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
))
526 /* Something worked... */
527 dst_confirm(skb
->dst
);
529 xprt_adjust_cwnd(task
, copied
);
530 xprt_update_rtt(task
);
531 xprt_complete_rqst(task
, copied
);
534 spin_unlock(&xprt
->transport_lock
);
536 skb_free_datagram(sk
, skb
);
538 read_unlock(&sk
->sk_callback_lock
);
541 static inline size_t xs_tcp_copy_data(skb_reader_t
*desc
, void *p
, size_t len
)
543 if (len
> desc
->count
)
545 if (skb_copy_bits(desc
->skb
, desc
->offset
, p
, len
)) {
546 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
552 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
557 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
562 p
= ((char *) &xprt
->tcp_recm
) + xprt
->tcp_offset
;
563 len
= sizeof(xprt
->tcp_recm
) - xprt
->tcp_offset
;
564 used
= xs_tcp_copy_data(desc
, p
, len
);
565 xprt
->tcp_offset
+= used
;
569 xprt
->tcp_reclen
= ntohl(xprt
->tcp_recm
);
570 if (xprt
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
571 xprt
->tcp_flags
|= XPRT_LAST_FRAG
;
573 xprt
->tcp_flags
&= ~XPRT_LAST_FRAG
;
574 xprt
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
576 xprt
->tcp_flags
&= ~XPRT_COPY_RECM
;
577 xprt
->tcp_offset
= 0;
579 /* Sanity check of the record length */
580 if (unlikely(xprt
->tcp_reclen
< 4)) {
581 dprintk("RPC: invalid TCP record fragment length\n");
582 xprt_disconnect(xprt
);
585 dprintk("RPC: reading TCP record fragment of length %d\n",
589 static void xs_tcp_check_recm(struct rpc_xprt
*xprt
)
591 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
592 xprt
, xprt
->tcp_copied
, xprt
->tcp_offset
, xprt
->tcp_reclen
, xprt
->tcp_flags
);
593 if (xprt
->tcp_offset
== xprt
->tcp_reclen
) {
594 xprt
->tcp_flags
|= XPRT_COPY_RECM
;
595 xprt
->tcp_offset
= 0;
596 if (xprt
->tcp_flags
& XPRT_LAST_FRAG
) {
597 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
598 xprt
->tcp_flags
|= XPRT_COPY_XID
;
599 xprt
->tcp_copied
= 0;
604 static inline void xs_tcp_read_xid(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
609 len
= sizeof(xprt
->tcp_xid
) - xprt
->tcp_offset
;
610 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
611 p
= ((char *) &xprt
->tcp_xid
) + xprt
->tcp_offset
;
612 used
= xs_tcp_copy_data(desc
, p
, len
);
613 xprt
->tcp_offset
+= used
;
616 xprt
->tcp_flags
&= ~XPRT_COPY_XID
;
617 xprt
->tcp_flags
|= XPRT_COPY_DATA
;
618 xprt
->tcp_copied
= 4;
619 dprintk("RPC: reading reply for XID %08x\n",
620 ntohl(xprt
->tcp_xid
));
621 xs_tcp_check_recm(xprt
);
624 static inline void xs_tcp_read_request(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
626 struct rpc_rqst
*req
;
627 struct xdr_buf
*rcvbuf
;
631 /* Find and lock the request corresponding to this xid */
632 spin_lock(&xprt
->transport_lock
);
633 req
= xprt_lookup_rqst(xprt
, xprt
->tcp_xid
);
635 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
636 dprintk("RPC: XID %08x request not found!\n",
637 ntohl(xprt
->tcp_xid
));
638 spin_unlock(&xprt
->transport_lock
);
642 rcvbuf
= &req
->rq_private_buf
;
644 if (len
> xprt
->tcp_reclen
- xprt
->tcp_offset
) {
645 skb_reader_t my_desc
;
647 len
= xprt
->tcp_reclen
- xprt
->tcp_offset
;
648 memcpy(&my_desc
, desc
, sizeof(my_desc
));
650 r
= xdr_partial_copy_from_skb(rcvbuf
, xprt
->tcp_copied
,
651 &my_desc
, xs_tcp_copy_data
);
655 r
= xdr_partial_copy_from_skb(rcvbuf
, xprt
->tcp_copied
,
656 desc
, xs_tcp_copy_data
);
659 xprt
->tcp_copied
+= r
;
660 xprt
->tcp_offset
+= r
;
663 /* Error when copying to the receive buffer,
664 * usually because we weren't able to allocate
665 * additional buffer pages. All we can do now
666 * is turn off XPRT_COPY_DATA, so the request
667 * will not receive any additional updates,
669 * Any remaining data from this record will
672 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
673 dprintk("RPC: XID %08x truncated request\n",
674 ntohl(xprt
->tcp_xid
));
675 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
676 xprt
, xprt
->tcp_copied
, xprt
->tcp_offset
, xprt
->tcp_reclen
);
680 dprintk("RPC: XID %08x read %Zd bytes\n",
681 ntohl(xprt
->tcp_xid
), r
);
682 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
683 xprt
, xprt
->tcp_copied
, xprt
->tcp_offset
, xprt
->tcp_reclen
);
685 if (xprt
->tcp_copied
== req
->rq_private_buf
.buflen
)
686 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
687 else if (xprt
->tcp_offset
== xprt
->tcp_reclen
) {
688 if (xprt
->tcp_flags
& XPRT_LAST_FRAG
)
689 xprt
->tcp_flags
&= ~XPRT_COPY_DATA
;
693 if (!(xprt
->tcp_flags
& XPRT_COPY_DATA
))
694 xprt_complete_rqst(req
->rq_task
, xprt
->tcp_copied
);
695 spin_unlock(&xprt
->transport_lock
);
696 xs_tcp_check_recm(xprt
);
699 static inline void xs_tcp_read_discard(struct rpc_xprt
*xprt
, skb_reader_t
*desc
)
703 len
= xprt
->tcp_reclen
- xprt
->tcp_offset
;
704 if (len
> desc
->count
)
708 xprt
->tcp_offset
+= len
;
709 dprintk("RPC: discarded %Zu bytes\n", len
);
710 xs_tcp_check_recm(xprt
);
713 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
715 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
716 skb_reader_t desc
= {
723 dprintk("RPC: xs_tcp_data_recv started\n");
725 /* Read in a new fragment marker if necessary */
726 /* Can we ever really expect to get completely empty fragments? */
727 if (xprt
->tcp_flags
& XPRT_COPY_RECM
) {
728 xs_tcp_read_fraghdr(xprt
, &desc
);
731 /* Read in the xid if necessary */
732 if (xprt
->tcp_flags
& XPRT_COPY_XID
) {
733 xs_tcp_read_xid(xprt
, &desc
);
736 /* Read in the request data */
737 if (xprt
->tcp_flags
& XPRT_COPY_DATA
) {
738 xs_tcp_read_request(xprt
, &desc
);
741 /* Skip over any trailing bytes on short reads */
742 xs_tcp_read_discard(xprt
, &desc
);
743 } while (desc
.count
);
744 dprintk("RPC: xs_tcp_data_recv done\n");
745 return len
- desc
.count
;
749 * xs_tcp_data_ready - "data ready" callback for TCP sockets
750 * @sk: socket with data to read
751 * @bytes: how much data to read
754 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
756 struct rpc_xprt
*xprt
;
757 read_descriptor_t rd_desc
;
759 read_lock(&sk
->sk_callback_lock
);
760 dprintk("RPC: xs_tcp_data_ready...\n");
761 if (!(xprt
= xprt_from_sock(sk
)))
766 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
767 rd_desc
.arg
.data
= xprt
;
768 rd_desc
.count
= 65536;
769 tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
771 read_unlock(&sk
->sk_callback_lock
);
775 * xs_tcp_state_change - callback to handle TCP socket state changes
776 * @sk: socket whose state has changed
779 static void xs_tcp_state_change(struct sock
*sk
)
781 struct rpc_xprt
*xprt
;
783 read_lock(&sk
->sk_callback_lock
);
784 if (!(xprt
= xprt_from_sock(sk
)))
786 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
787 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
788 sk
->sk_state
, xprt_connected(xprt
),
789 sock_flag(sk
, SOCK_DEAD
),
790 sock_flag(sk
, SOCK_ZAPPED
));
792 switch (sk
->sk_state
) {
793 case TCP_ESTABLISHED
:
794 spin_lock_bh(&xprt
->transport_lock
);
795 if (!xprt_test_and_set_connected(xprt
)) {
796 /* Reset TCP record info */
797 xprt
->tcp_offset
= 0;
798 xprt
->tcp_reclen
= 0;
799 xprt
->tcp_copied
= 0;
800 xprt
->tcp_flags
= XPRT_COPY_RECM
| XPRT_COPY_XID
;
801 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
802 xprt_wake_pending_tasks(xprt
, 0);
804 spin_unlock_bh(&xprt
->transport_lock
);
810 /* Try to schedule an autoclose RPC calls */
811 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
812 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
) == 0)
813 schedule_work(&xprt
->task_cleanup
);
815 xprt_disconnect(xprt
);
818 read_unlock(&sk
->sk_callback_lock
);
822 * xs_udp_write_space - callback invoked when socket buffer space
824 * @sk: socket whose state has changed
826 * Called when more output buffer space is available for this socket.
827 * We try not to wake our writers until they can make "significant"
828 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
829 * with a bunch of small requests.
831 static void xs_udp_write_space(struct sock
*sk
)
833 read_lock(&sk
->sk_callback_lock
);
835 /* from net/core/sock.c:sock_def_write_space */
836 if (sock_writeable(sk
)) {
838 struct rpc_xprt
*xprt
;
840 if (unlikely(!(sock
= sk
->sk_socket
)))
842 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
844 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE
, &sock
->flags
)))
847 xprt_write_space(xprt
);
851 read_unlock(&sk
->sk_callback_lock
);
855 * xs_tcp_write_space - callback invoked when socket buffer space
857 * @sk: socket whose state has changed
859 * Called when more output buffer space is available for this socket.
860 * We try not to wake our writers until they can make "significant"
861 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
862 * with a bunch of small requests.
864 static void xs_tcp_write_space(struct sock
*sk
)
866 read_lock(&sk
->sk_callback_lock
);
868 /* from net/core/stream.c:sk_stream_write_space */
869 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
)) {
871 struct rpc_xprt
*xprt
;
873 if (unlikely(!(sock
= sk
->sk_socket
)))
875 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
877 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE
, &sock
->flags
)))
880 xprt_write_space(xprt
);
884 read_unlock(&sk
->sk_callback_lock
);
887 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
889 struct sock
*sk
= xprt
->inet
;
892 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
893 sk
->sk_rcvbuf
= xprt
->rcvsize
* xprt
->max_reqs
* 2;
896 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
897 sk
->sk_sndbuf
= xprt
->sndsize
* xprt
->max_reqs
* 2;
898 sk
->sk_write_space(sk
);
903 * xs_udp_set_buffer_size - set send and receive limits
904 * @xprt: generic transport
905 * @sndsize: requested size of send buffer, in bytes
906 * @rcvsize: requested size of receive buffer, in bytes
908 * Set socket send and receive buffer size limits.
910 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
914 xprt
->sndsize
= sndsize
+ 1024;
917 xprt
->rcvsize
= rcvsize
+ 1024;
919 xs_udp_do_set_buffer_size(xprt
);
923 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
924 * @task: task that timed out
926 * Adjust the congestion window after a retransmit timeout has occurred.
928 static void xs_udp_timer(struct rpc_task
*task
)
930 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
934 * xs_set_port - reset the port number in the remote endpoint address
935 * @xprt: generic transport
936 * @port: new port number
939 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
941 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
942 xprt
->addr
.sin_port
= htons(port
);
945 static int xs_bindresvport(struct rpc_xprt
*xprt
, struct socket
*sock
)
947 struct sockaddr_in myaddr
= {
948 .sin_family
= AF_INET
,
951 unsigned short port
= xprt
->port
;
954 myaddr
.sin_port
= htons(port
);
955 err
= sock
->ops
->bind(sock
, (struct sockaddr
*) &myaddr
,
959 dprintk("RPC: xs_bindresvport bound to port %u\n",
963 if (port
<= xprt_min_resvport
)
964 port
= xprt_max_resvport
;
967 } while (err
== -EADDRINUSE
&& port
!= xprt
->port
);
969 dprintk("RPC: can't bind to reserved port (%d).\n", -err
);
974 * xs_udp_connect_worker - set up a UDP socket
975 * @args: RPC transport to connect
977 * Invoked by a work queue tasklet.
979 static void xs_udp_connect_worker(void *args
)
981 struct rpc_xprt
*xprt
= (struct rpc_xprt
*) args
;
982 struct socket
*sock
= xprt
->sock
;
983 int err
, status
= -EIO
;
985 if (xprt
->shutdown
|| xprt
->addr
.sin_port
== 0)
988 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt
);
990 /* Start by resetting any existing state */
993 if ((err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
)) < 0) {
994 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
998 if (xprt
->resvport
&& xs_bindresvport(xprt
, sock
) < 0) {
1004 struct sock
*sk
= sock
->sk
;
1006 write_lock_bh(&sk
->sk_callback_lock
);
1008 sk
->sk_user_data
= xprt
;
1009 xprt
->old_data_ready
= sk
->sk_data_ready
;
1010 xprt
->old_state_change
= sk
->sk_state_change
;
1011 xprt
->old_write_space
= sk
->sk_write_space
;
1012 sk
->sk_data_ready
= xs_udp_data_ready
;
1013 sk
->sk_write_space
= xs_udp_write_space
;
1014 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1015 sk
->sk_allocation
= GFP_ATOMIC
;
1017 xprt_set_connected(xprt
);
1019 /* Reset to new socket */
1023 write_unlock_bh(&sk
->sk_callback_lock
);
1025 xs_udp_do_set_buffer_size(xprt
);
1028 xprt_wake_pending_tasks(xprt
, status
);
1029 xprt_clear_connecting(xprt
);
1033 * We need to preserve the port number so the reply cache on the server can
1034 * find our cached RPC replies when we get around to reconnecting.
1036 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
)
1039 struct socket
*sock
= xprt
->sock
;
1040 struct sockaddr any
;
1042 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1045 * Disconnect the transport socket by doing a connect operation
1046 * with AF_UNSPEC. This should return immediately...
1048 memset(&any
, 0, sizeof(any
));
1049 any
.sa_family
= AF_UNSPEC
;
1050 result
= sock
->ops
->connect(sock
, &any
, sizeof(any
), 0);
1052 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1057 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1058 * @args: RPC transport to connect
1060 * Invoked by a work queue tasklet.
1062 static void xs_tcp_connect_worker(void *args
)
1064 struct rpc_xprt
*xprt
= (struct rpc_xprt
*)args
;
1065 struct socket
*sock
= xprt
->sock
;
1066 int err
, status
= -EIO
;
1068 if (xprt
->shutdown
|| xprt
->addr
.sin_port
== 0)
1071 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt
);
1074 /* start from scratch */
1075 if ((err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
)) < 0) {
1076 dprintk("RPC: can't create TCP transport socket (%d).\n", -err
);
1080 if (xprt
->resvport
&& xs_bindresvport(xprt
, sock
) < 0) {
1085 /* "close" the socket, preserving the local port */
1086 xs_tcp_reuse_connection(xprt
);
1089 struct sock
*sk
= sock
->sk
;
1091 write_lock_bh(&sk
->sk_callback_lock
);
1093 sk
->sk_user_data
= xprt
;
1094 xprt
->old_data_ready
= sk
->sk_data_ready
;
1095 xprt
->old_state_change
= sk
->sk_state_change
;
1096 xprt
->old_write_space
= sk
->sk_write_space
;
1097 sk
->sk_data_ready
= xs_tcp_data_ready
;
1098 sk
->sk_state_change
= xs_tcp_state_change
;
1099 sk
->sk_write_space
= xs_tcp_write_space
;
1100 sk
->sk_allocation
= GFP_ATOMIC
;
1102 /* socket options */
1103 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1104 sock_reset_flag(sk
, SOCK_LINGER
);
1105 tcp_sk(sk
)->linger2
= 0;
1106 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1108 xprt_clear_connected(xprt
);
1110 /* Reset to new socket */
1114 write_unlock_bh(&sk
->sk_callback_lock
);
1117 /* Tell the socket layer to start connecting... */
1118 xprt
->stat
.connect_count
++;
1119 xprt
->stat
.connect_start
= jiffies
;
1120 status
= sock
->ops
->connect(sock
, (struct sockaddr
*) &xprt
->addr
,
1121 sizeof(xprt
->addr
), O_NONBLOCK
);
1122 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1123 xprt
, -status
, xprt_connected(xprt
), sock
->sk
->sk_state
);
1131 /* retry with existing socket, after a delay */
1134 /* get rid of existing socket, and retry */
1140 xprt_wake_pending_tasks(xprt
, status
);
1142 xprt_clear_connecting(xprt
);
1146 * xs_connect - connect a socket to a remote endpoint
1147 * @task: address of RPC task that manages state of connect request
1149 * TCP: If the remote end dropped the connection, delay reconnecting.
1151 * UDP socket connects are synchronous, but we use a work queue anyway
1152 * to guarantee that even unprivileged user processes can set up a
1153 * socket on a privileged port.
1155 * If a UDP socket connect fails, the delay behavior here prevents
1156 * retry floods (hard mounts).
1158 static void xs_connect(struct rpc_task
*task
)
1160 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1162 if (xprt_test_and_set_connecting(xprt
))
1165 if (xprt
->sock
!= NULL
) {
1166 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1167 xprt
, xprt
->reestablish_timeout
/ HZ
);
1168 schedule_delayed_work(&xprt
->connect_worker
,
1169 xprt
->reestablish_timeout
);
1170 xprt
->reestablish_timeout
<<= 1;
1171 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
1172 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
1174 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
1175 schedule_work(&xprt
->connect_worker
);
1177 /* flush_scheduled_work can sleep... */
1178 if (!RPC_IS_ASYNC(task
))
1179 flush_scheduled_work();
1184 * xs_udp_print_stats - display UDP socket-specifc stats
1185 * @xprt: rpc_xprt struct containing statistics
1189 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
1191 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1193 xprt
->stat
.bind_count
,
1196 xprt
->stat
.bad_xids
,
1198 xprt
->stat
.bklog_u
);
1202 * xs_tcp_print_stats - display TCP socket-specifc stats
1203 * @xprt: rpc_xprt struct containing statistics
1207 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
1211 if (xprt_connected(xprt
))
1212 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
1214 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1216 xprt
->stat
.bind_count
,
1217 xprt
->stat
.connect_count
,
1218 xprt
->stat
.connect_time
,
1222 xprt
->stat
.bad_xids
,
1224 xprt
->stat
.bklog_u
);
1227 static struct rpc_xprt_ops xs_udp_ops
= {
1228 .set_buffer_size
= xs_udp_set_buffer_size
,
1229 .reserve_xprt
= xprt_reserve_xprt_cong
,
1230 .release_xprt
= xprt_release_xprt_cong
,
1231 .set_port
= xs_set_port
,
1232 .connect
= xs_connect
,
1233 .buf_alloc
= rpc_malloc
,
1234 .buf_free
= rpc_free
,
1235 .send_request
= xs_udp_send_request
,
1236 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
1237 .timer
= xs_udp_timer
,
1238 .release_request
= xprt_release_rqst_cong
,
1240 .destroy
= xs_destroy
,
1241 .print_stats
= xs_udp_print_stats
,
1244 static struct rpc_xprt_ops xs_tcp_ops
= {
1245 .reserve_xprt
= xprt_reserve_xprt
,
1246 .release_xprt
= xprt_release_xprt
,
1247 .set_port
= xs_set_port
,
1248 .connect
= xs_connect
,
1249 .buf_alloc
= rpc_malloc
,
1250 .buf_free
= rpc_free
,
1251 .send_request
= xs_tcp_send_request
,
1252 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
1254 .destroy
= xs_destroy
,
1255 .print_stats
= xs_tcp_print_stats
,
1259 * xs_setup_udp - Set up transport to use a UDP socket
1260 * @xprt: transport to set up
1261 * @to: timeout parameters
1264 int xs_setup_udp(struct rpc_xprt
*xprt
, struct rpc_timeout
*to
)
1266 size_t slot_table_size
;
1268 dprintk("RPC: setting up udp-ipv4 transport...\n");
1270 xprt
->max_reqs
= xprt_udp_slot_table_entries
;
1271 slot_table_size
= xprt
->max_reqs
* sizeof(xprt
->slot
[0]);
1272 xprt
->slot
= kmalloc(slot_table_size
, GFP_KERNEL
);
1273 if (xprt
->slot
== NULL
)
1275 memset(xprt
->slot
, 0, slot_table_size
);
1277 xprt
->prot
= IPPROTO_UDP
;
1278 xprt
->port
= xprt_max_resvport
;
1280 xprt
->resvport
= capable(CAP_NET_BIND_SERVICE
) ? 1 : 0;
1281 /* XXX: header size can vary due to auth type, IPv6, etc. */
1282 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
1284 INIT_WORK(&xprt
->connect_worker
, xs_udp_connect_worker
, xprt
);
1285 xprt
->bind_timeout
= XS_BIND_TO
;
1286 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
1287 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
1288 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
1290 xprt
->ops
= &xs_udp_ops
;
1293 xprt
->timeout
= *to
;
1295 xprt_set_timeout(&xprt
->timeout
, 5, 5 * HZ
);
1301 * xs_setup_tcp - Set up transport to use a TCP socket
1302 * @xprt: transport to set up
1303 * @to: timeout parameters
1306 int xs_setup_tcp(struct rpc_xprt
*xprt
, struct rpc_timeout
*to
)
1308 size_t slot_table_size
;
1310 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1312 xprt
->max_reqs
= xprt_tcp_slot_table_entries
;
1313 slot_table_size
= xprt
->max_reqs
* sizeof(xprt
->slot
[0]);
1314 xprt
->slot
= kmalloc(slot_table_size
, GFP_KERNEL
);
1315 if (xprt
->slot
== NULL
)
1317 memset(xprt
->slot
, 0, slot_table_size
);
1319 xprt
->prot
= IPPROTO_TCP
;
1320 xprt
->port
= xprt_max_resvport
;
1321 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
1322 xprt
->resvport
= capable(CAP_NET_BIND_SERVICE
) ? 1 : 0;
1323 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
1325 INIT_WORK(&xprt
->connect_worker
, xs_tcp_connect_worker
, xprt
);
1326 xprt
->bind_timeout
= XS_BIND_TO
;
1327 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
1328 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1329 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
1331 xprt
->ops
= &xs_tcp_ops
;
1334 xprt
->timeout
= *to
;
1336 xprt_set_timeout(&xprt
->timeout
, 2, 60 * HZ
);