2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
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/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
26 #include <linux/pagemap.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
30 #include <linux/net.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/sched.h>
37 #include <linux/sunrpc/svcsock.h>
38 #include <linux/sunrpc/xprtsock.h>
39 #include <linux/file.h>
40 #ifdef CONFIG_SUNRPC_BACKCHANNEL
41 #include <linux/sunrpc/bc_xprt.h>
45 #include <net/checksum.h>
51 static void xs_close(struct rpc_xprt
*xprt
);
56 unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
57 unsigned int xprt_tcp_slot_table_entries
= RPC_MIN_SLOT_TABLE
;
58 unsigned int xprt_max_tcp_slot_table_entries
= RPC_MAX_SLOT_TABLE
;
60 unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
61 unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
63 #define XS_TCP_LINGER_TO (15U * HZ)
64 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
67 * We can register our own files under /proc/sys/sunrpc by
68 * calling register_sysctl_table() again. The files in that
69 * directory become the union of all files registered there.
71 * We simply need to make sure that we don't collide with
72 * someone else's file names!
77 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
78 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
79 static unsigned int max_tcp_slot_table_limit
= RPC_MAX_SLOT_TABLE_LIMIT
;
80 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
81 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
83 static struct ctl_table_header
*sunrpc_table_header
;
86 * FIXME: changing the UDP slot table size should also resize the UDP
87 * socket buffers for existing UDP transports
89 static ctl_table xs_tunables_table
[] = {
91 .procname
= "udp_slot_table_entries",
92 .data
= &xprt_udp_slot_table_entries
,
93 .maxlen
= sizeof(unsigned int),
95 .proc_handler
= proc_dointvec_minmax
,
96 .extra1
= &min_slot_table_size
,
97 .extra2
= &max_slot_table_size
100 .procname
= "tcp_slot_table_entries",
101 .data
= &xprt_tcp_slot_table_entries
,
102 .maxlen
= sizeof(unsigned int),
104 .proc_handler
= proc_dointvec_minmax
,
105 .extra1
= &min_slot_table_size
,
106 .extra2
= &max_slot_table_size
109 .procname
= "tcp_max_slot_table_entries",
110 .data
= &xprt_max_tcp_slot_table_entries
,
111 .maxlen
= sizeof(unsigned int),
113 .proc_handler
= proc_dointvec_minmax
,
114 .extra1
= &min_slot_table_size
,
115 .extra2
= &max_tcp_slot_table_limit
118 .procname
= "min_resvport",
119 .data
= &xprt_min_resvport
,
120 .maxlen
= sizeof(unsigned int),
122 .proc_handler
= proc_dointvec_minmax
,
123 .extra1
= &xprt_min_resvport_limit
,
124 .extra2
= &xprt_max_resvport_limit
127 .procname
= "max_resvport",
128 .data
= &xprt_max_resvport
,
129 .maxlen
= sizeof(unsigned int),
131 .proc_handler
= proc_dointvec_minmax
,
132 .extra1
= &xprt_min_resvport_limit
,
133 .extra2
= &xprt_max_resvport_limit
136 .procname
= "tcp_fin_timeout",
137 .data
= &xs_tcp_fin_timeout
,
138 .maxlen
= sizeof(xs_tcp_fin_timeout
),
140 .proc_handler
= proc_dointvec_jiffies
,
145 static ctl_table sunrpc_table
[] = {
147 .procname
= "sunrpc",
149 .child
= xs_tunables_table
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
235 unsigned long tcp_copied
,
239 * Connection of transports
241 struct delayed_work connect_worker
;
242 struct sockaddr_storage srcaddr
;
243 unsigned short srcport
;
246 * UDP socket buffer size parameters
252 * Saved socket callback addresses
254 void (*old_data_ready
)(struct sock
*, int);
255 void (*old_state_change
)(struct sock
*);
256 void (*old_write_space
)(struct sock
*);
257 void (*old_error_report
)(struct sock
*);
261 * TCP receive state flags
263 #define TCP_RCV_LAST_FRAG (1UL << 0)
264 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
265 #define TCP_RCV_COPY_XID (1UL << 2)
266 #define TCP_RCV_COPY_DATA (1UL << 3)
267 #define TCP_RCV_READ_CALLDIR (1UL << 4)
268 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
273 #define TCP_RPC_REPLY (1UL << 6)
275 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
277 return (struct sockaddr
*) &xprt
->addr
;
280 static inline struct sockaddr_un
*xs_addr_un(struct rpc_xprt
*xprt
)
282 return (struct sockaddr_un
*) &xprt
->addr
;
285 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
287 return (struct sockaddr_in
*) &xprt
->addr
;
290 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
292 return (struct sockaddr_in6
*) &xprt
->addr
;
295 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
297 struct sockaddr
*sap
= xs_addr(xprt
);
298 struct sockaddr_in6
*sin6
;
299 struct sockaddr_in
*sin
;
300 struct sockaddr_un
*sun
;
303 switch (sap
->sa_family
) {
305 sun
= xs_addr_un(xprt
);
306 strlcpy(buf
, sun
->sun_path
, sizeof(buf
));
307 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
308 kstrdup(buf
, GFP_KERNEL
);
311 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
312 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
313 kstrdup(buf
, GFP_KERNEL
);
314 sin
= xs_addr_in(xprt
);
315 snprintf(buf
, sizeof(buf
), "%08x", ntohl(sin
->sin_addr
.s_addr
));
318 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
319 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
320 kstrdup(buf
, GFP_KERNEL
);
321 sin6
= xs_addr_in6(xprt
);
322 snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
328 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
331 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
333 struct sockaddr
*sap
= xs_addr(xprt
);
336 snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
337 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
339 snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
340 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
343 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
344 const char *protocol
,
347 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
348 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
349 xs_format_common_peer_addresses(xprt
);
350 xs_format_common_peer_ports(xprt
);
353 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
355 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
356 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
358 xs_format_common_peer_ports(xprt
);
361 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
365 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
367 case RPC_DISPLAY_PROTO
:
368 case RPC_DISPLAY_NETID
:
371 kfree(xprt
->address_strings
[i
]);
375 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
377 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
379 struct msghdr msg
= {
381 .msg_namelen
= addrlen
,
382 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
385 .iov_base
= vec
->iov_base
+ base
,
386 .iov_len
= vec
->iov_len
- base
,
389 if (iov
.iov_len
!= 0)
390 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
391 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
394 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
)
397 unsigned int remainder
;
400 remainder
= xdr
->page_len
- base
;
401 base
+= xdr
->page_base
;
402 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
405 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
406 int flags
= XS_SENDMSG_FLAGS
;
409 if (remainder
!= 0 || more
)
411 err
= sock
->ops
->sendpage(sock
, *ppage
, base
, len
, flags
);
412 if (remainder
== 0 || err
!= len
)
426 * xs_sendpages - write pages directly to a socket
427 * @sock: socket to send on
428 * @addr: UDP only -- address of destination
429 * @addrlen: UDP only -- length of destination address
430 * @xdr: buffer containing this request
431 * @base: starting position in the buffer
434 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
436 unsigned int remainder
= xdr
->len
- base
;
442 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
448 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
449 unsigned int len
= xdr
->head
[0].iov_len
- base
;
451 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
452 if (remainder
== 0 || err
!= len
)
457 base
-= xdr
->head
[0].iov_len
;
459 if (base
< xdr
->page_len
) {
460 unsigned int len
= xdr
->page_len
- base
;
462 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0);
463 if (remainder
== 0 || err
!= len
)
468 base
-= xdr
->page_len
;
470 if (base
>= xdr
->tail
[0].iov_len
)
472 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
481 static void xs_nospace_callback(struct rpc_task
*task
)
483 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
485 transport
->inet
->sk_write_pending
--;
486 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
490 * xs_nospace - place task on wait queue if transmit was incomplete
491 * @task: task to put to sleep
494 static int xs_nospace(struct rpc_task
*task
)
496 struct rpc_rqst
*req
= task
->tk_rqstp
;
497 struct rpc_xprt
*xprt
= req
->rq_xprt
;
498 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
501 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
502 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
505 /* Protect against races with write_space */
506 spin_lock_bh(&xprt
->transport_lock
);
508 /* Don't race with disconnect */
509 if (xprt_connected(xprt
)) {
510 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
513 * Notify TCP that we're limited by the application
516 set_bit(SOCK_NOSPACE
, &transport
->sock
->flags
);
517 transport
->inet
->sk_write_pending
++;
518 /* ...and wait for more buffer space */
519 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
522 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
526 spin_unlock_bh(&xprt
->transport_lock
);
531 * Construct a stream transport record marker in @buf.
533 static inline void xs_encode_stream_record_marker(struct xdr_buf
*buf
)
535 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
536 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
537 *base
= cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
| reclen
);
541 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
542 * @task: RPC task that manages the state of an RPC request
545 * 0: The request has been sent
546 * EAGAIN: The socket was blocked, please call again later to
547 * complete the request
548 * ENOTCONN: Caller needs to invoke connect logic then call again
549 * other: Some other error occured, the request was not sent
551 static int xs_local_send_request(struct rpc_task
*task
)
553 struct rpc_rqst
*req
= task
->tk_rqstp
;
554 struct rpc_xprt
*xprt
= req
->rq_xprt
;
555 struct sock_xprt
*transport
=
556 container_of(xprt
, struct sock_xprt
, xprt
);
557 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
560 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
562 xs_pktdump("packet data:",
563 req
->rq_svec
->iov_base
, req
->rq_svec
->iov_len
);
565 status
= xs_sendpages(transport
->sock
, NULL
, 0,
566 xdr
, req
->rq_bytes_sent
);
567 dprintk("RPC: %s(%u) = %d\n",
568 __func__
, xdr
->len
- req
->rq_bytes_sent
, status
);
569 if (likely(status
>= 0)) {
570 req
->rq_bytes_sent
+= status
;
571 req
->rq_xmit_bytes_sent
+= status
;
572 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
573 req
->rq_bytes_sent
= 0;
581 status
= xs_nospace(task
);
584 dprintk("RPC: sendmsg returned unrecognized error %d\n",
595 * xs_udp_send_request - write an RPC request to a UDP socket
596 * @task: address of RPC task that manages the state of an RPC request
599 * 0: The request has been sent
600 * EAGAIN: The socket was blocked, please call again later to
601 * complete the request
602 * ENOTCONN: Caller needs to invoke connect logic then call again
603 * other: Some other error occurred, the request was not sent
605 static int xs_udp_send_request(struct rpc_task
*task
)
607 struct rpc_rqst
*req
= task
->tk_rqstp
;
608 struct rpc_xprt
*xprt
= req
->rq_xprt
;
609 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
610 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
613 xs_pktdump("packet data:",
614 req
->rq_svec
->iov_base
,
615 req
->rq_svec
->iov_len
);
617 if (!xprt_bound(xprt
))
619 status
= xs_sendpages(transport
->sock
,
624 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
625 xdr
->len
- req
->rq_bytes_sent
, status
);
628 req
->rq_xmit_bytes_sent
+= status
;
629 if (status
>= req
->rq_slen
)
631 /* Still some bytes left; set up for a retry later. */
638 /* Should we call xs_close() here? */
641 status
= xs_nospace(task
);
644 dprintk("RPC: sendmsg returned unrecognized error %d\n",
649 /* When the server has died, an ICMP port unreachable message
650 * prompts ECONNREFUSED. */
651 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
658 * xs_tcp_shutdown - gracefully shut down a TCP socket
661 * Initiates a graceful shutdown of the TCP socket by calling the
662 * equivalent of shutdown(SHUT_WR);
664 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
666 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
667 struct socket
*sock
= transport
->sock
;
670 kernel_sock_shutdown(sock
, SHUT_WR
);
674 * xs_tcp_send_request - write an RPC request to a TCP socket
675 * @task: address of RPC task that manages the state of an RPC request
678 * 0: The request has been sent
679 * EAGAIN: The socket was blocked, please call again later to
680 * complete the request
681 * ENOTCONN: Caller needs to invoke connect logic then call again
682 * other: Some other error occurred, the request was not sent
684 * XXX: In the case of soft timeouts, should we eventually give up
685 * if sendmsg is not able to make progress?
687 static int xs_tcp_send_request(struct rpc_task
*task
)
689 struct rpc_rqst
*req
= task
->tk_rqstp
;
690 struct rpc_xprt
*xprt
= req
->rq_xprt
;
691 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
692 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
695 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
697 xs_pktdump("packet data:",
698 req
->rq_svec
->iov_base
,
699 req
->rq_svec
->iov_len
);
701 /* Continue transmitting the packet/record. We must be careful
702 * to cope with writespace callbacks arriving _after_ we have
703 * called sendmsg(). */
705 status
= xs_sendpages(transport
->sock
,
706 NULL
, 0, xdr
, req
->rq_bytes_sent
);
708 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
709 xdr
->len
- req
->rq_bytes_sent
, status
);
711 if (unlikely(status
< 0))
714 /* If we've sent the entire packet, immediately
715 * reset the count of bytes sent. */
716 req
->rq_bytes_sent
+= status
;
717 req
->rq_xmit_bytes_sent
+= status
;
718 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
719 req
->rq_bytes_sent
= 0;
732 /* Should we call xs_close() here? */
735 status
= xs_nospace(task
);
738 dprintk("RPC: sendmsg returned unrecognized error %d\n",
742 xs_tcp_shutdown(xprt
);
745 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
752 * xs_tcp_release_xprt - clean up after a tcp transmission
756 * This cleans up if an error causes us to abort the transmission of a request.
757 * In this case, the socket may need to be reset in order to avoid confusing
760 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
762 struct rpc_rqst
*req
;
764 if (task
!= xprt
->snd_task
)
768 req
= task
->tk_rqstp
;
771 if (req
->rq_bytes_sent
== 0)
773 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
775 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
777 xprt_release_xprt(xprt
, task
);
780 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
782 transport
->old_data_ready
= sk
->sk_data_ready
;
783 transport
->old_state_change
= sk
->sk_state_change
;
784 transport
->old_write_space
= sk
->sk_write_space
;
785 transport
->old_error_report
= sk
->sk_error_report
;
788 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
790 sk
->sk_data_ready
= transport
->old_data_ready
;
791 sk
->sk_state_change
= transport
->old_state_change
;
792 sk
->sk_write_space
= transport
->old_write_space
;
793 sk
->sk_error_report
= transport
->old_error_report
;
796 static void xs_reset_transport(struct sock_xprt
*transport
)
798 struct socket
*sock
= transport
->sock
;
799 struct sock
*sk
= transport
->inet
;
804 transport
->srcport
= 0;
806 write_lock_bh(&sk
->sk_callback_lock
);
807 transport
->inet
= NULL
;
808 transport
->sock
= NULL
;
810 sk
->sk_user_data
= NULL
;
812 xs_restore_old_callbacks(transport
, sk
);
813 write_unlock_bh(&sk
->sk_callback_lock
);
821 * xs_close - close a socket
824 * This is used when all requests are complete; ie, no DRC state remains
825 * on the server we want to save.
827 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
828 * xs_reset_transport() zeroing the socket from underneath a writer.
830 static void xs_close(struct rpc_xprt
*xprt
)
832 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
834 dprintk("RPC: xs_close xprt %p\n", xprt
);
836 xs_reset_transport(transport
);
837 xprt
->reestablish_timeout
= 0;
839 smp_mb__before_clear_bit();
840 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
841 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
842 clear_bit(XPRT_CLOSING
, &xprt
->state
);
843 smp_mb__after_clear_bit();
844 xprt_disconnect_done(xprt
);
847 static void xs_tcp_close(struct rpc_xprt
*xprt
)
849 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
852 xs_tcp_shutdown(xprt
);
856 * xs_destroy - prepare to shutdown a transport
857 * @xprt: doomed transport
860 static void xs_destroy(struct rpc_xprt
*xprt
)
862 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
864 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
866 cancel_delayed_work_sync(&transport
->connect_worker
);
869 xs_free_peer_addresses(xprt
);
871 module_put(THIS_MODULE
);
874 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
876 return (struct rpc_xprt
*) sk
->sk_user_data
;
879 static int xs_local_copy_to_xdr(struct xdr_buf
*xdr
, struct sk_buff
*skb
)
881 struct xdr_skb_reader desc
= {
883 .offset
= sizeof(rpc_fraghdr
),
884 .count
= skb
->len
- sizeof(rpc_fraghdr
),
887 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_bits
) < 0)
895 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
896 * @sk: socket with data to read
897 * @len: how much data to read
899 * Currently this assumes we can read the whole reply in a single gulp.
901 static void xs_local_data_ready(struct sock
*sk
, int len
)
903 struct rpc_task
*task
;
904 struct rpc_xprt
*xprt
;
905 struct rpc_rqst
*rovr
;
907 int err
, repsize
, copied
;
911 read_lock_bh(&sk
->sk_callback_lock
);
912 dprintk("RPC: %s...\n", __func__
);
913 xprt
= xprt_from_sock(sk
);
917 skb
= skb_recv_datagram(sk
, 0, 1, &err
);
924 repsize
= skb
->len
- sizeof(rpc_fraghdr
);
926 dprintk("RPC: impossible RPC reply size %d\n", repsize
);
930 /* Copy the XID from the skb... */
931 xp
= skb_header_pointer(skb
, sizeof(rpc_fraghdr
), sizeof(_xid
), &_xid
);
935 /* Look up and lock the request corresponding to the given XID */
936 spin_lock(&xprt
->transport_lock
);
937 rovr
= xprt_lookup_rqst(xprt
, *xp
);
940 task
= rovr
->rq_task
;
942 copied
= rovr
->rq_private_buf
.buflen
;
943 if (copied
> repsize
)
946 if (xs_local_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
947 dprintk("RPC: sk_buff copy failed\n");
951 xprt_complete_rqst(task
, copied
);
954 spin_unlock(&xprt
->transport_lock
);
956 skb_free_datagram(sk
, skb
);
958 read_unlock_bh(&sk
->sk_callback_lock
);
962 * xs_udp_data_ready - "data ready" callback for UDP sockets
963 * @sk: socket with data to read
964 * @len: how much data to read
967 static void xs_udp_data_ready(struct sock
*sk
, int len
)
969 struct rpc_task
*task
;
970 struct rpc_xprt
*xprt
;
971 struct rpc_rqst
*rovr
;
973 int err
, repsize
, copied
;
977 read_lock_bh(&sk
->sk_callback_lock
);
978 dprintk("RPC: xs_udp_data_ready...\n");
979 if (!(xprt
= xprt_from_sock(sk
)))
982 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
988 repsize
= skb
->len
- sizeof(struct udphdr
);
990 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
994 /* Copy the XID from the skb... */
995 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
996 sizeof(_xid
), &_xid
);
1000 /* Look up and lock the request corresponding to the given XID */
1001 spin_lock(&xprt
->transport_lock
);
1002 rovr
= xprt_lookup_rqst(xprt
, *xp
);
1005 task
= rovr
->rq_task
;
1007 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
1010 /* Suck it into the iovec, verify checksum if not done by hw. */
1011 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
1012 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
1016 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
1018 /* Something worked... */
1019 dst_confirm(skb_dst(skb
));
1021 xprt_adjust_cwnd(task
, copied
);
1022 xprt_complete_rqst(task
, copied
);
1025 spin_unlock(&xprt
->transport_lock
);
1027 skb_free_datagram(sk
, skb
);
1029 read_unlock_bh(&sk
->sk_callback_lock
);
1032 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
1034 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1038 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
1039 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
1040 used
= xdr_skb_read_bits(desc
, p
, len
);
1041 transport
->tcp_offset
+= used
;
1045 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
1046 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
1047 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
1049 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
1050 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
1052 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
1053 transport
->tcp_offset
= 0;
1055 /* Sanity check of the record length */
1056 if (unlikely(transport
->tcp_reclen
< 8)) {
1057 dprintk("RPC: invalid TCP record fragment length\n");
1058 xprt_force_disconnect(xprt
);
1061 dprintk("RPC: reading TCP record fragment of length %d\n",
1062 transport
->tcp_reclen
);
1065 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
1067 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1068 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
1069 transport
->tcp_offset
= 0;
1070 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
1071 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1072 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
1073 transport
->tcp_copied
= 0;
1078 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1083 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
1084 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
1085 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
1086 used
= xdr_skb_read_bits(desc
, p
, len
);
1087 transport
->tcp_offset
+= used
;
1090 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
1091 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
1092 transport
->tcp_copied
= 4;
1093 dprintk("RPC: reading %s XID %08x\n",
1094 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
1096 ntohl(transport
->tcp_xid
));
1097 xs_tcp_check_fraghdr(transport
);
1100 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
1101 struct xdr_skb_reader
*desc
)
1108 * We want transport->tcp_offset to be 8 at the end of this routine
1109 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1110 * When this function is called for the first time,
1111 * transport->tcp_offset is 4 (after having already read the xid).
1113 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
1114 len
= sizeof(transport
->tcp_calldir
) - offset
;
1115 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
1116 p
= ((char *) &transport
->tcp_calldir
) + offset
;
1117 used
= xdr_skb_read_bits(desc
, p
, len
);
1118 transport
->tcp_offset
+= used
;
1121 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
1123 * We don't yet have the XDR buffer, so we will write the calldir
1124 * out after we get the buffer from the 'struct rpc_rqst'
1126 switch (ntohl(transport
->tcp_calldir
)) {
1128 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1129 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1130 transport
->tcp_flags
|= TCP_RPC_REPLY
;
1133 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1134 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1135 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
1138 dprintk("RPC: invalid request message type\n");
1139 xprt_force_disconnect(&transport
->xprt
);
1141 xs_tcp_check_fraghdr(transport
);
1144 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
1145 struct xdr_skb_reader
*desc
,
1146 struct rpc_rqst
*req
)
1148 struct sock_xprt
*transport
=
1149 container_of(xprt
, struct sock_xprt
, xprt
);
1150 struct xdr_buf
*rcvbuf
;
1154 rcvbuf
= &req
->rq_private_buf
;
1156 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1158 * Save the RPC direction in the XDR buffer
1160 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1161 &transport
->tcp_calldir
,
1162 sizeof(transport
->tcp_calldir
));
1163 transport
->tcp_copied
+= sizeof(transport
->tcp_calldir
);
1164 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1168 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1169 struct xdr_skb_reader my_desc
;
1171 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1172 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1173 my_desc
.count
= len
;
1174 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1175 &my_desc
, xdr_skb_read_bits
);
1179 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1180 desc
, xdr_skb_read_bits
);
1183 transport
->tcp_copied
+= r
;
1184 transport
->tcp_offset
+= r
;
1187 /* Error when copying to the receive buffer,
1188 * usually because we weren't able to allocate
1189 * additional buffer pages. All we can do now
1190 * is turn off TCP_RCV_COPY_DATA, so the request
1191 * will not receive any additional updates,
1193 * Any remaining data from this record will
1196 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1197 dprintk("RPC: XID %08x truncated request\n",
1198 ntohl(transport
->tcp_xid
));
1199 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1200 "tcp_offset = %u, tcp_reclen = %u\n",
1201 xprt
, transport
->tcp_copied
,
1202 transport
->tcp_offset
, transport
->tcp_reclen
);
1206 dprintk("RPC: XID %08x read %Zd bytes\n",
1207 ntohl(transport
->tcp_xid
), r
);
1208 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1209 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1210 transport
->tcp_offset
, transport
->tcp_reclen
);
1212 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1213 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1214 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1215 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1216 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1221 * Finds the request corresponding to the RPC xid and invokes the common
1222 * tcp read code to read the data.
1224 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1225 struct xdr_skb_reader
*desc
)
1227 struct sock_xprt
*transport
=
1228 container_of(xprt
, struct sock_xprt
, xprt
);
1229 struct rpc_rqst
*req
;
1231 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1233 /* Find and lock the request corresponding to this xid */
1234 spin_lock(&xprt
->transport_lock
);
1235 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1237 dprintk("RPC: XID %08x request not found!\n",
1238 ntohl(transport
->tcp_xid
));
1239 spin_unlock(&xprt
->transport_lock
);
1243 xs_tcp_read_common(xprt
, desc
, req
);
1245 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1246 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1248 spin_unlock(&xprt
->transport_lock
);
1252 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1254 * Obtains an rpc_rqst previously allocated and invokes the common
1255 * tcp read code to read the data. The result is placed in the callback
1257 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1258 * connection and return -1.
1260 static inline int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1261 struct xdr_skb_reader
*desc
)
1263 struct sock_xprt
*transport
=
1264 container_of(xprt
, struct sock_xprt
, xprt
);
1265 struct rpc_rqst
*req
;
1267 req
= xprt_alloc_bc_request(xprt
);
1269 printk(KERN_WARNING
"Callback slot table overflowed\n");
1270 xprt_force_disconnect(xprt
);
1274 req
->rq_xid
= transport
->tcp_xid
;
1275 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1276 xs_tcp_read_common(xprt
, desc
, req
);
1278 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
)) {
1279 struct svc_serv
*bc_serv
= xprt
->bc_serv
;
1282 * Add callback request to callback list. The callback
1283 * service sleeps on the sv_cb_waitq waiting for new
1284 * requests. Wake it up after adding enqueing the
1287 dprintk("RPC: add callback request to list\n");
1288 spin_lock(&bc_serv
->sv_cb_lock
);
1289 list_add(&req
->rq_bc_list
, &bc_serv
->sv_cb_list
);
1290 spin_unlock(&bc_serv
->sv_cb_lock
);
1291 wake_up(&bc_serv
->sv_cb_waitq
);
1294 req
->rq_private_buf
.len
= transport
->tcp_copied
;
1299 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1300 struct xdr_skb_reader
*desc
)
1302 struct sock_xprt
*transport
=
1303 container_of(xprt
, struct sock_xprt
, xprt
);
1305 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1306 xs_tcp_read_reply(xprt
, desc
) :
1307 xs_tcp_read_callback(xprt
, desc
);
1310 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1311 struct xdr_skb_reader
*desc
)
1313 return xs_tcp_read_reply(xprt
, desc
);
1315 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1318 * Read data off the transport. This can be either an RPC_CALL or an
1319 * RPC_REPLY. Relay the processing to helper functions.
1321 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1322 struct xdr_skb_reader
*desc
)
1324 struct sock_xprt
*transport
=
1325 container_of(xprt
, struct sock_xprt
, xprt
);
1327 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1328 xs_tcp_check_fraghdr(transport
);
1331 * The transport_lock protects the request handling.
1332 * There's no need to hold it to update the tcp_flags.
1334 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1338 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1342 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1343 if (len
> desc
->count
)
1346 desc
->offset
+= len
;
1347 transport
->tcp_offset
+= len
;
1348 dprintk("RPC: discarded %Zu bytes\n", len
);
1349 xs_tcp_check_fraghdr(transport
);
1352 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1354 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1355 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1356 struct xdr_skb_reader desc
= {
1362 dprintk("RPC: xs_tcp_data_recv started\n");
1364 /* Read in a new fragment marker if necessary */
1365 /* Can we ever really expect to get completely empty fragments? */
1366 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1367 xs_tcp_read_fraghdr(xprt
, &desc
);
1370 /* Read in the xid if necessary */
1371 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1372 xs_tcp_read_xid(transport
, &desc
);
1375 /* Read in the call/reply flag */
1376 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1377 xs_tcp_read_calldir(transport
, &desc
);
1380 /* Read in the request data */
1381 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1382 xs_tcp_read_data(xprt
, &desc
);
1385 /* Skip over any trailing bytes on short reads */
1386 xs_tcp_read_discard(transport
, &desc
);
1387 } while (desc
.count
);
1388 dprintk("RPC: xs_tcp_data_recv done\n");
1389 return len
- desc
.count
;
1393 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1394 * @sk: socket with data to read
1395 * @bytes: how much data to read
1398 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1400 struct rpc_xprt
*xprt
;
1401 read_descriptor_t rd_desc
;
1404 dprintk("RPC: xs_tcp_data_ready...\n");
1406 read_lock_bh(&sk
->sk_callback_lock
);
1407 if (!(xprt
= xprt_from_sock(sk
)))
1412 /* Any data means we had a useful conversation, so
1413 * the we don't need to delay the next reconnect
1415 if (xprt
->reestablish_timeout
)
1416 xprt
->reestablish_timeout
= 0;
1418 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1419 rd_desc
.arg
.data
= xprt
;
1421 rd_desc
.count
= 65536;
1422 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1425 read_unlock_bh(&sk
->sk_callback_lock
);
1429 * Do the equivalent of linger/linger2 handling for dealing with
1430 * broken servers that don't close the socket in a timely
1433 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1434 unsigned long timeout
)
1436 struct sock_xprt
*transport
;
1438 if (xprt_test_and_set_connecting(xprt
))
1440 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1441 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1442 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1446 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1448 struct sock_xprt
*transport
;
1450 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1452 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1453 !cancel_delayed_work(&transport
->connect_worker
))
1455 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1456 xprt_clear_connecting(xprt
);
1459 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1461 smp_mb__before_clear_bit();
1462 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1463 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1464 smp_mb__after_clear_bit();
1465 /* Mark transport as closed and wake up all pending tasks */
1466 xprt_disconnect_done(xprt
);
1470 * xs_tcp_state_change - callback to handle TCP socket state changes
1471 * @sk: socket whose state has changed
1474 static void xs_tcp_state_change(struct sock
*sk
)
1476 struct rpc_xprt
*xprt
;
1478 read_lock_bh(&sk
->sk_callback_lock
);
1479 if (!(xprt
= xprt_from_sock(sk
)))
1481 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1482 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1483 sk
->sk_state
, xprt_connected(xprt
),
1484 sock_flag(sk
, SOCK_DEAD
),
1485 sock_flag(sk
, SOCK_ZAPPED
),
1488 switch (sk
->sk_state
) {
1489 case TCP_ESTABLISHED
:
1490 spin_lock(&xprt
->transport_lock
);
1491 if (!xprt_test_and_set_connected(xprt
)) {
1492 struct sock_xprt
*transport
= container_of(xprt
,
1493 struct sock_xprt
, xprt
);
1495 /* Reset TCP record info */
1496 transport
->tcp_offset
= 0;
1497 transport
->tcp_reclen
= 0;
1498 transport
->tcp_copied
= 0;
1499 transport
->tcp_flags
=
1500 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1502 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1504 spin_unlock(&xprt
->transport_lock
);
1507 /* The client initiated a shutdown of the socket */
1508 xprt
->connect_cookie
++;
1509 xprt
->reestablish_timeout
= 0;
1510 set_bit(XPRT_CLOSING
, &xprt
->state
);
1511 smp_mb__before_clear_bit();
1512 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1513 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1514 smp_mb__after_clear_bit();
1515 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1517 case TCP_CLOSE_WAIT
:
1518 /* The server initiated a shutdown of the socket */
1519 xprt_force_disconnect(xprt
);
1520 xprt
->connect_cookie
++;
1523 * If the server closed down the connection, make sure that
1524 * we back off before reconnecting
1526 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1527 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1530 set_bit(XPRT_CLOSING
, &xprt
->state
);
1531 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1532 smp_mb__before_clear_bit();
1533 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1534 smp_mb__after_clear_bit();
1537 xs_tcp_cancel_linger_timeout(xprt
);
1538 xs_sock_mark_closed(xprt
);
1541 read_unlock_bh(&sk
->sk_callback_lock
);
1545 * xs_error_report - callback mainly for catching socket errors
1548 static void xs_error_report(struct sock
*sk
)
1550 struct rpc_xprt
*xprt
;
1552 read_lock_bh(&sk
->sk_callback_lock
);
1553 if (!(xprt
= xprt_from_sock(sk
)))
1555 dprintk("RPC: %s client %p...\n"
1557 __func__
, xprt
, sk
->sk_err
);
1558 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1560 read_unlock_bh(&sk
->sk_callback_lock
);
1563 static void xs_write_space(struct sock
*sk
)
1565 struct socket
*sock
;
1566 struct rpc_xprt
*xprt
;
1568 if (unlikely(!(sock
= sk
->sk_socket
)))
1570 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1572 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1574 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1577 xprt_write_space(xprt
);
1581 * xs_udp_write_space - callback invoked when socket buffer space
1583 * @sk: socket whose state has changed
1585 * Called when more output buffer space is available for this socket.
1586 * We try not to wake our writers until they can make "significant"
1587 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1588 * with a bunch of small requests.
1590 static void xs_udp_write_space(struct sock
*sk
)
1592 read_lock_bh(&sk
->sk_callback_lock
);
1594 /* from net/core/sock.c:sock_def_write_space */
1595 if (sock_writeable(sk
))
1598 read_unlock_bh(&sk
->sk_callback_lock
);
1602 * xs_tcp_write_space - callback invoked when socket buffer space
1604 * @sk: socket whose state has changed
1606 * Called when more output buffer space is available for this socket.
1607 * We try not to wake our writers until they can make "significant"
1608 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1609 * with a bunch of small requests.
1611 static void xs_tcp_write_space(struct sock
*sk
)
1613 read_lock_bh(&sk
->sk_callback_lock
);
1615 /* from net/core/stream.c:sk_stream_write_space */
1616 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
))
1619 read_unlock_bh(&sk
->sk_callback_lock
);
1622 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1624 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1625 struct sock
*sk
= transport
->inet
;
1627 if (transport
->rcvsize
) {
1628 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1629 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1631 if (transport
->sndsize
) {
1632 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1633 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1634 sk
->sk_write_space(sk
);
1639 * xs_udp_set_buffer_size - set send and receive limits
1640 * @xprt: generic transport
1641 * @sndsize: requested size of send buffer, in bytes
1642 * @rcvsize: requested size of receive buffer, in bytes
1644 * Set socket send and receive buffer size limits.
1646 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1648 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1650 transport
->sndsize
= 0;
1652 transport
->sndsize
= sndsize
+ 1024;
1653 transport
->rcvsize
= 0;
1655 transport
->rcvsize
= rcvsize
+ 1024;
1657 xs_udp_do_set_buffer_size(xprt
);
1661 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1662 * @task: task that timed out
1664 * Adjust the congestion window after a retransmit timeout has occurred.
1666 static void xs_udp_timer(struct rpc_task
*task
)
1668 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
1671 static unsigned short xs_get_random_port(void)
1673 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1674 unsigned short rand
= (unsigned short) net_random() % range
;
1675 return rand
+ xprt_min_resvport
;
1679 * xs_set_port - reset the port number in the remote endpoint address
1680 * @xprt: generic transport
1681 * @port: new port number
1684 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1686 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1688 rpc_set_port(xs_addr(xprt
), port
);
1689 xs_update_peer_port(xprt
);
1692 static unsigned short xs_get_srcport(struct sock_xprt
*transport
)
1694 unsigned short port
= transport
->srcport
;
1696 if (port
== 0 && transport
->xprt
.resvport
)
1697 port
= xs_get_random_port();
1701 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, unsigned short port
)
1703 if (transport
->srcport
!= 0)
1704 transport
->srcport
= 0;
1705 if (!transport
->xprt
.resvport
)
1707 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1708 return xprt_max_resvport
;
1711 static int xs_bind(struct sock_xprt
*transport
, struct socket
*sock
)
1713 struct sockaddr_storage myaddr
;
1715 unsigned short port
= xs_get_srcport(transport
);
1716 unsigned short last
;
1718 memcpy(&myaddr
, &transport
->srcaddr
, transport
->xprt
.addrlen
);
1720 rpc_set_port((struct sockaddr
*)&myaddr
, port
);
1721 err
= kernel_bind(sock
, (struct sockaddr
*)&myaddr
,
1722 transport
->xprt
.addrlen
);
1726 transport
->srcport
= port
;
1730 port
= xs_next_srcport(transport
, port
);
1733 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1735 if (myaddr
.ss_family
== AF_INET
)
1736 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__
,
1737 &((struct sockaddr_in
*)&myaddr
)->sin_addr
,
1738 port
, err
? "failed" : "ok", err
);
1740 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__
,
1741 &((struct sockaddr_in6
*)&myaddr
)->sin6_addr
,
1742 port
, err
? "failed" : "ok", err
);
1747 * We don't support autobind on AF_LOCAL sockets
1749 static void xs_local_rpcbind(struct rpc_task
*task
)
1751 xprt_set_bound(task
->tk_xprt
);
1754 static void xs_local_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1758 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1759 static struct lock_class_key xs_key
[2];
1760 static struct lock_class_key xs_slock_key
[2];
1762 static inline void xs_reclassify_socketu(struct socket
*sock
)
1764 struct sock
*sk
= sock
->sk
;
1766 BUG_ON(sock_owned_by_user(sk
));
1767 sock_lock_init_class_and_name(sk
, "slock-AF_LOCAL-RPC",
1768 &xs_slock_key
[1], "sk_lock-AF_LOCAL-RPC", &xs_key
[1]);
1771 static inline void xs_reclassify_socket4(struct socket
*sock
)
1773 struct sock
*sk
= sock
->sk
;
1775 BUG_ON(sock_owned_by_user(sk
));
1776 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1777 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1780 static inline void xs_reclassify_socket6(struct socket
*sock
)
1782 struct sock
*sk
= sock
->sk
;
1784 BUG_ON(sock_owned_by_user(sk
));
1785 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1786 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1789 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1793 xs_reclassify_socketu(sock
);
1796 xs_reclassify_socket4(sock
);
1799 xs_reclassify_socket6(sock
);
1804 static inline void xs_reclassify_socketu(struct socket
*sock
)
1808 static inline void xs_reclassify_socket4(struct socket
*sock
)
1812 static inline void xs_reclassify_socket6(struct socket
*sock
)
1816 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1821 static struct socket
*xs_create_sock(struct rpc_xprt
*xprt
,
1822 struct sock_xprt
*transport
, int family
, int type
, int protocol
)
1824 struct socket
*sock
;
1827 err
= __sock_create(xprt
->xprt_net
, family
, type
, protocol
, &sock
, 1);
1829 dprintk("RPC: can't create %d transport socket (%d).\n",
1833 xs_reclassify_socket(family
, sock
);
1835 err
= xs_bind(transport
, sock
);
1843 return ERR_PTR(err
);
1846 static int xs_local_finish_connecting(struct rpc_xprt
*xprt
,
1847 struct socket
*sock
)
1849 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1852 if (!transport
->inet
) {
1853 struct sock
*sk
= sock
->sk
;
1855 write_lock_bh(&sk
->sk_callback_lock
);
1857 xs_save_old_callbacks(transport
, sk
);
1859 sk
->sk_user_data
= xprt
;
1860 sk
->sk_data_ready
= xs_local_data_ready
;
1861 sk
->sk_write_space
= xs_udp_write_space
;
1862 sk
->sk_error_report
= xs_error_report
;
1863 sk
->sk_allocation
= GFP_ATOMIC
;
1865 xprt_clear_connected(xprt
);
1867 /* Reset to new socket */
1868 transport
->sock
= sock
;
1869 transport
->inet
= sk
;
1871 write_unlock_bh(&sk
->sk_callback_lock
);
1874 /* Tell the socket layer to start connecting... */
1875 xprt
->stat
.connect_count
++;
1876 xprt
->stat
.connect_start
= jiffies
;
1877 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, 0);
1881 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1882 * @xprt: RPC transport to connect
1883 * @transport: socket transport to connect
1884 * @create_sock: function to create a socket of the correct type
1886 * Invoked by a work queue tasklet.
1888 static void xs_local_setup_socket(struct work_struct
*work
)
1890 struct sock_xprt
*transport
=
1891 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1892 struct rpc_xprt
*xprt
= &transport
->xprt
;
1893 struct socket
*sock
;
1899 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1900 status
= __sock_create(xprt
->xprt_net
, AF_LOCAL
,
1901 SOCK_STREAM
, 0, &sock
, 1);
1903 dprintk("RPC: can't create AF_LOCAL "
1904 "transport socket (%d).\n", -status
);
1907 xs_reclassify_socketu(sock
);
1909 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1910 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1912 status
= xs_local_finish_connecting(xprt
, sock
);
1915 dprintk("RPC: xprt %p connected to %s\n",
1916 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1917 xprt_set_connected(xprt
);
1920 dprintk("RPC: xprt %p: socket %s does not exist\n",
1921 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1924 printk(KERN_ERR
"%s: unhandled error (%d) connecting to %s\n",
1926 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1930 xprt_clear_connecting(xprt
);
1931 xprt_wake_pending_tasks(xprt
, status
);
1934 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1936 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1938 if (!transport
->inet
) {
1939 struct sock
*sk
= sock
->sk
;
1941 write_lock_bh(&sk
->sk_callback_lock
);
1943 xs_save_old_callbacks(transport
, sk
);
1945 sk
->sk_user_data
= xprt
;
1946 sk
->sk_data_ready
= xs_udp_data_ready
;
1947 sk
->sk_write_space
= xs_udp_write_space
;
1948 sk
->sk_error_report
= xs_error_report
;
1949 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1950 sk
->sk_allocation
= GFP_ATOMIC
;
1952 xprt_set_connected(xprt
);
1954 /* Reset to new socket */
1955 transport
->sock
= sock
;
1956 transport
->inet
= sk
;
1958 write_unlock_bh(&sk
->sk_callback_lock
);
1960 xs_udp_do_set_buffer_size(xprt
);
1963 static void xs_udp_setup_socket(struct work_struct
*work
)
1965 struct sock_xprt
*transport
=
1966 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1967 struct rpc_xprt
*xprt
= &transport
->xprt
;
1968 struct socket
*sock
= transport
->sock
;
1974 /* Start by resetting any existing state */
1975 xs_reset_transport(transport
);
1976 sock
= xs_create_sock(xprt
, transport
,
1977 xs_addr(xprt
)->sa_family
, SOCK_DGRAM
, IPPROTO_UDP
);
1981 dprintk("RPC: worker connecting xprt %p via %s to "
1982 "%s (port %s)\n", xprt
,
1983 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1984 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1985 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1987 xs_udp_finish_connecting(xprt
, sock
);
1990 xprt_clear_connecting(xprt
);
1991 xprt_wake_pending_tasks(xprt
, status
);
1995 * We need to preserve the port number so the reply cache on the server can
1996 * find our cached RPC replies when we get around to reconnecting.
1998 static void xs_abort_connection(struct sock_xprt
*transport
)
2001 struct sockaddr any
;
2003 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport
);
2006 * Disconnect the transport socket by doing a connect operation
2007 * with AF_UNSPEC. This should return immediately...
2009 memset(&any
, 0, sizeof(any
));
2010 any
.sa_family
= AF_UNSPEC
;
2011 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
2013 xs_sock_mark_closed(&transport
->xprt
);
2015 dprintk("RPC: AF_UNSPEC connect return code %d\n",
2019 static void xs_tcp_reuse_connection(struct sock_xprt
*transport
)
2021 unsigned int state
= transport
->inet
->sk_state
;
2023 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
) {
2024 /* we don't need to abort the connection if the socket
2025 * hasn't undergone a shutdown
2027 if (transport
->inet
->sk_shutdown
== 0)
2029 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2030 __func__
, transport
->inet
->sk_shutdown
);
2032 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
)) {
2033 /* we don't need to abort the connection if the socket
2034 * hasn't undergone a shutdown
2036 if (transport
->inet
->sk_shutdown
== 0)
2038 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2039 "sk_shutdown set to %d\n",
2040 __func__
, transport
->inet
->sk_shutdown
);
2042 xs_abort_connection(transport
);
2045 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2047 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2048 int ret
= -ENOTCONN
;
2050 if (!transport
->inet
) {
2051 struct sock
*sk
= sock
->sk
;
2053 write_lock_bh(&sk
->sk_callback_lock
);
2055 xs_save_old_callbacks(transport
, sk
);
2057 sk
->sk_user_data
= xprt
;
2058 sk
->sk_data_ready
= xs_tcp_data_ready
;
2059 sk
->sk_state_change
= xs_tcp_state_change
;
2060 sk
->sk_write_space
= xs_tcp_write_space
;
2061 sk
->sk_error_report
= xs_error_report
;
2062 sk
->sk_allocation
= GFP_ATOMIC
;
2064 /* socket options */
2065 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
2066 sock_reset_flag(sk
, SOCK_LINGER
);
2067 tcp_sk(sk
)->linger2
= 0;
2068 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
2070 xprt_clear_connected(xprt
);
2072 /* Reset to new socket */
2073 transport
->sock
= sock
;
2074 transport
->inet
= sk
;
2076 write_unlock_bh(&sk
->sk_callback_lock
);
2079 if (!xprt_bound(xprt
))
2082 /* Tell the socket layer to start connecting... */
2083 xprt
->stat
.connect_count
++;
2084 xprt
->stat
.connect_start
= jiffies
;
2085 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
2090 xprt
->connect_cookie
++;
2091 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2092 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2099 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2100 * @xprt: RPC transport to connect
2101 * @transport: socket transport to connect
2102 * @create_sock: function to create a socket of the correct type
2104 * Invoked by a work queue tasklet.
2106 static void xs_tcp_setup_socket(struct work_struct
*work
)
2108 struct sock_xprt
*transport
=
2109 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2110 struct socket
*sock
= transport
->sock
;
2111 struct rpc_xprt
*xprt
= &transport
->xprt
;
2118 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
2119 sock
= xs_create_sock(xprt
, transport
,
2120 xs_addr(xprt
)->sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
2122 status
= PTR_ERR(sock
);
2128 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
2130 /* "close" the socket, preserving the local port */
2131 xs_tcp_reuse_connection(transport
);
2137 dprintk("RPC: worker connecting xprt %p via %s to "
2138 "%s (port %s)\n", xprt
,
2139 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2140 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2141 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2143 status
= xs_tcp_finish_connecting(xprt
, sock
);
2144 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2145 xprt
, -status
, xprt_connected(xprt
),
2146 sock
->sk
->sk_state
);
2149 printk("%s: connect returned unhandled error %d\n",
2151 case -EADDRNOTAVAIL
:
2152 /* We're probably in TIME_WAIT. Get rid of existing socket,
2155 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
2156 xprt_force_disconnect(xprt
);
2161 /* retry with existing socket, after a delay */
2165 xprt_clear_connecting(xprt
);
2168 /* Happens, for instance, if the user specified a link
2169 * local IPv6 address without a scope-id.
2176 xprt_clear_connecting(xprt
);
2177 xprt_wake_pending_tasks(xprt
, status
);
2181 * xs_connect - connect a socket to a remote endpoint
2182 * @task: address of RPC task that manages state of connect request
2184 * TCP: If the remote end dropped the connection, delay reconnecting.
2186 * UDP socket connects are synchronous, but we use a work queue anyway
2187 * to guarantee that even unprivileged user processes can set up a
2188 * socket on a privileged port.
2190 * If a UDP socket connect fails, the delay behavior here prevents
2191 * retry floods (hard mounts).
2193 static void xs_connect(struct rpc_task
*task
)
2195 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2196 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2198 if (transport
->sock
!= NULL
&& !RPC_IS_SOFTCONN(task
)) {
2199 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2201 xprt
, xprt
->reestablish_timeout
/ HZ
);
2202 queue_delayed_work(rpciod_workqueue
,
2203 &transport
->connect_worker
,
2204 xprt
->reestablish_timeout
);
2205 xprt
->reestablish_timeout
<<= 1;
2206 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2207 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2208 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2209 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2211 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2212 queue_delayed_work(rpciod_workqueue
,
2213 &transport
->connect_worker
, 0);
2218 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2219 * @xprt: rpc_xprt struct containing statistics
2223 static void xs_local_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2227 if (xprt_connected(xprt
))
2228 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2230 seq_printf(seq
, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2232 xprt
->stat
.bind_count
,
2233 xprt
->stat
.connect_count
,
2234 xprt
->stat
.connect_time
,
2238 xprt
->stat
.bad_xids
,
2240 xprt
->stat
.bklog_u
);
2244 * xs_udp_print_stats - display UDP socket-specifc stats
2245 * @xprt: rpc_xprt struct containing statistics
2249 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2251 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2253 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2255 xprt
->stat
.bind_count
,
2258 xprt
->stat
.bad_xids
,
2260 xprt
->stat
.bklog_u
);
2264 * xs_tcp_print_stats - display TCP socket-specifc stats
2265 * @xprt: rpc_xprt struct containing statistics
2269 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2271 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2274 if (xprt_connected(xprt
))
2275 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2277 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2279 xprt
->stat
.bind_count
,
2280 xprt
->stat
.connect_count
,
2281 xprt
->stat
.connect_time
,
2285 xprt
->stat
.bad_xids
,
2287 xprt
->stat
.bklog_u
);
2291 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2292 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2293 * to use the server side send routines.
2295 static void *bc_malloc(struct rpc_task
*task
, size_t size
)
2298 struct rpc_buffer
*buf
;
2300 BUG_ON(size
> PAGE_SIZE
- sizeof(struct rpc_buffer
));
2301 page
= alloc_page(GFP_KERNEL
);
2306 buf
= page_address(page
);
2307 buf
->len
= PAGE_SIZE
;
2313 * Free the space allocated in the bc_alloc routine
2315 static void bc_free(void *buffer
)
2317 struct rpc_buffer
*buf
;
2322 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2323 free_page((unsigned long)buf
);
2327 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2328 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2330 static int bc_sendto(struct rpc_rqst
*req
)
2333 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2334 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2335 struct sock_xprt
*transport
=
2336 container_of(xprt
, struct sock_xprt
, xprt
);
2337 struct socket
*sock
= transport
->sock
;
2338 unsigned long headoff
;
2339 unsigned long tailoff
;
2341 xs_encode_stream_record_marker(xbufp
);
2343 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2344 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2345 len
= svc_send_common(sock
, xbufp
,
2346 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2347 xbufp
->tail
[0].iov_base
, tailoff
);
2349 if (len
!= xbufp
->len
) {
2350 printk(KERN_NOTICE
"Error sending entire callback!\n");
2358 * The send routine. Borrows from svc_send
2360 static int bc_send_request(struct rpc_task
*task
)
2362 struct rpc_rqst
*req
= task
->tk_rqstp
;
2363 struct svc_xprt
*xprt
;
2364 struct svc_sock
*svsk
;
2367 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2369 * Get the server socket associated with this callback xprt
2371 xprt
= req
->rq_xprt
->bc_xprt
;
2372 svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
2375 * Grab the mutex to serialize data as the connection is shared
2376 * with the fore channel
2378 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2379 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2380 if (!mutex_trylock(&xprt
->xpt_mutex
))
2382 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2384 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2387 len
= bc_sendto(req
);
2388 mutex_unlock(&xprt
->xpt_mutex
);
2397 * The close routine. Since this is client initiated, we do nothing
2400 static void bc_close(struct rpc_xprt
*xprt
)
2405 * The xprt destroy routine. Again, because this connection is client
2406 * initiated, we do nothing
2409 static void bc_destroy(struct rpc_xprt
*xprt
)
2413 static struct rpc_xprt_ops xs_local_ops
= {
2414 .reserve_xprt
= xprt_reserve_xprt
,
2415 .release_xprt
= xs_tcp_release_xprt
,
2416 .rpcbind
= xs_local_rpcbind
,
2417 .set_port
= xs_local_set_port
,
2418 .connect
= xs_connect
,
2419 .buf_alloc
= rpc_malloc
,
2420 .buf_free
= rpc_free
,
2421 .send_request
= xs_local_send_request
,
2422 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2424 .destroy
= xs_destroy
,
2425 .print_stats
= xs_local_print_stats
,
2428 static struct rpc_xprt_ops xs_udp_ops
= {
2429 .set_buffer_size
= xs_udp_set_buffer_size
,
2430 .reserve_xprt
= xprt_reserve_xprt_cong
,
2431 .release_xprt
= xprt_release_xprt_cong
,
2432 .rpcbind
= rpcb_getport_async
,
2433 .set_port
= xs_set_port
,
2434 .connect
= xs_connect
,
2435 .buf_alloc
= rpc_malloc
,
2436 .buf_free
= rpc_free
,
2437 .send_request
= xs_udp_send_request
,
2438 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2439 .timer
= xs_udp_timer
,
2440 .release_request
= xprt_release_rqst_cong
,
2442 .destroy
= xs_destroy
,
2443 .print_stats
= xs_udp_print_stats
,
2446 static struct rpc_xprt_ops xs_tcp_ops
= {
2447 .reserve_xprt
= xprt_reserve_xprt
,
2448 .release_xprt
= xs_tcp_release_xprt
,
2449 .rpcbind
= rpcb_getport_async
,
2450 .set_port
= xs_set_port
,
2451 .connect
= xs_connect
,
2452 .buf_alloc
= rpc_malloc
,
2453 .buf_free
= rpc_free
,
2454 .send_request
= xs_tcp_send_request
,
2455 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2456 .close
= xs_tcp_close
,
2457 .destroy
= xs_destroy
,
2458 .print_stats
= xs_tcp_print_stats
,
2462 * The rpc_xprt_ops for the server backchannel
2465 static struct rpc_xprt_ops bc_tcp_ops
= {
2466 .reserve_xprt
= xprt_reserve_xprt
,
2467 .release_xprt
= xprt_release_xprt
,
2468 .buf_alloc
= bc_malloc
,
2469 .buf_free
= bc_free
,
2470 .send_request
= bc_send_request
,
2471 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2473 .destroy
= bc_destroy
,
2474 .print_stats
= xs_tcp_print_stats
,
2477 static int xs_init_anyaddr(const int family
, struct sockaddr
*sap
)
2479 static const struct sockaddr_in sin
= {
2480 .sin_family
= AF_INET
,
2481 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2483 static const struct sockaddr_in6 sin6
= {
2484 .sin6_family
= AF_INET6
,
2485 .sin6_addr
= IN6ADDR_ANY_INIT
,
2492 memcpy(sap
, &sin
, sizeof(sin
));
2495 memcpy(sap
, &sin6
, sizeof(sin6
));
2498 dprintk("RPC: %s: Bad address family\n", __func__
);
2499 return -EAFNOSUPPORT
;
2504 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2505 unsigned int slot_table_size
,
2506 unsigned int max_slot_table_size
)
2508 struct rpc_xprt
*xprt
;
2509 struct sock_xprt
*new;
2511 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2512 dprintk("RPC: xs_setup_xprt: address too large\n");
2513 return ERR_PTR(-EBADF
);
2516 xprt
= xprt_alloc(args
->net
, sizeof(*new), slot_table_size
,
2517 max_slot_table_size
);
2519 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2521 return ERR_PTR(-ENOMEM
);
2524 new = container_of(xprt
, struct sock_xprt
, xprt
);
2525 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2526 xprt
->addrlen
= args
->addrlen
;
2528 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2531 err
= xs_init_anyaddr(args
->dstaddr
->sa_family
,
2532 (struct sockaddr
*)&new->srcaddr
);
2534 return ERR_PTR(err
);
2540 static const struct rpc_timeout xs_local_default_timeout
= {
2541 .to_initval
= 10 * HZ
,
2542 .to_maxval
= 10 * HZ
,
2547 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2548 * @args: rpc transport creation arguments
2550 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2552 static struct rpc_xprt
*xs_setup_local(struct xprt_create
*args
)
2554 struct sockaddr_un
*sun
= (struct sockaddr_un
*)args
->dstaddr
;
2555 struct sock_xprt
*transport
;
2556 struct rpc_xprt
*xprt
;
2557 struct rpc_xprt
*ret
;
2559 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2560 xprt_max_tcp_slot_table_entries
);
2563 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2566 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2567 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2569 xprt
->bind_timeout
= XS_BIND_TO
;
2570 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2571 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2573 xprt
->ops
= &xs_local_ops
;
2574 xprt
->timeout
= &xs_local_default_timeout
;
2576 switch (sun
->sun_family
) {
2578 if (sun
->sun_path
[0] != '/') {
2579 dprintk("RPC: bad AF_LOCAL address: %s\n",
2581 ret
= ERR_PTR(-EINVAL
);
2584 xprt_set_bound(xprt
);
2585 INIT_DELAYED_WORK(&transport
->connect_worker
,
2586 xs_local_setup_socket
);
2587 xs_format_peer_addresses(xprt
, "local", RPCBIND_NETID_LOCAL
);
2590 ret
= ERR_PTR(-EAFNOSUPPORT
);
2594 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2595 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2597 if (try_module_get(THIS_MODULE
))
2599 ret
= ERR_PTR(-EINVAL
);
2605 static const struct rpc_timeout xs_udp_default_timeout
= {
2606 .to_initval
= 5 * HZ
,
2607 .to_maxval
= 30 * HZ
,
2608 .to_increment
= 5 * HZ
,
2613 * xs_setup_udp - Set up transport to use a UDP socket
2614 * @args: rpc transport creation arguments
2617 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2619 struct sockaddr
*addr
= args
->dstaddr
;
2620 struct rpc_xprt
*xprt
;
2621 struct sock_xprt
*transport
;
2622 struct rpc_xprt
*ret
;
2624 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
,
2625 xprt_udp_slot_table_entries
);
2628 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2630 xprt
->prot
= IPPROTO_UDP
;
2632 /* XXX: header size can vary due to auth type, IPv6, etc. */
2633 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2635 xprt
->bind_timeout
= XS_BIND_TO
;
2636 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2637 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2639 xprt
->ops
= &xs_udp_ops
;
2641 xprt
->timeout
= &xs_udp_default_timeout
;
2643 switch (addr
->sa_family
) {
2645 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2646 xprt_set_bound(xprt
);
2648 INIT_DELAYED_WORK(&transport
->connect_worker
,
2649 xs_udp_setup_socket
);
2650 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2653 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2654 xprt_set_bound(xprt
);
2656 INIT_DELAYED_WORK(&transport
->connect_worker
,
2657 xs_udp_setup_socket
);
2658 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2661 ret
= ERR_PTR(-EAFNOSUPPORT
);
2665 if (xprt_bound(xprt
))
2666 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2667 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2668 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2669 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2671 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2672 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2673 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2675 if (try_module_get(THIS_MODULE
))
2677 ret
= ERR_PTR(-EINVAL
);
2683 static const struct rpc_timeout xs_tcp_default_timeout
= {
2684 .to_initval
= 60 * HZ
,
2685 .to_maxval
= 60 * HZ
,
2690 * xs_setup_tcp - Set up transport to use a TCP socket
2691 * @args: rpc transport creation arguments
2694 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2696 struct sockaddr
*addr
= args
->dstaddr
;
2697 struct rpc_xprt
*xprt
;
2698 struct sock_xprt
*transport
;
2699 struct rpc_xprt
*ret
;
2701 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2702 xprt_max_tcp_slot_table_entries
);
2705 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2707 xprt
->prot
= IPPROTO_TCP
;
2708 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2709 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2711 xprt
->bind_timeout
= XS_BIND_TO
;
2712 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2713 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2715 xprt
->ops
= &xs_tcp_ops
;
2716 xprt
->timeout
= &xs_tcp_default_timeout
;
2718 switch (addr
->sa_family
) {
2720 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2721 xprt_set_bound(xprt
);
2723 INIT_DELAYED_WORK(&transport
->connect_worker
,
2724 xs_tcp_setup_socket
);
2725 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2728 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2729 xprt_set_bound(xprt
);
2731 INIT_DELAYED_WORK(&transport
->connect_worker
,
2732 xs_tcp_setup_socket
);
2733 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2736 ret
= ERR_PTR(-EAFNOSUPPORT
);
2740 if (xprt_bound(xprt
))
2741 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2742 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2743 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2744 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2746 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2747 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2748 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2751 if (try_module_get(THIS_MODULE
))
2753 ret
= ERR_PTR(-EINVAL
);
2760 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2761 * @args: rpc transport creation arguments
2764 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2766 struct sockaddr
*addr
= args
->dstaddr
;
2767 struct rpc_xprt
*xprt
;
2768 struct sock_xprt
*transport
;
2769 struct svc_sock
*bc_sock
;
2770 struct rpc_xprt
*ret
;
2772 if (args
->bc_xprt
->xpt_bc_xprt
) {
2774 * This server connection already has a backchannel
2775 * export; we can't create a new one, as we wouldn't be
2776 * able to match replies based on xid any more. So,
2777 * reuse the already-existing one:
2779 return args
->bc_xprt
->xpt_bc_xprt
;
2781 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2782 xprt_tcp_slot_table_entries
);
2785 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2787 xprt
->prot
= IPPROTO_TCP
;
2788 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2789 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2790 xprt
->timeout
= &xs_tcp_default_timeout
;
2793 xprt_set_bound(xprt
);
2794 xprt
->bind_timeout
= 0;
2795 xprt
->reestablish_timeout
= 0;
2796 xprt
->idle_timeout
= 0;
2798 xprt
->ops
= &bc_tcp_ops
;
2800 switch (addr
->sa_family
) {
2802 xs_format_peer_addresses(xprt
, "tcp",
2806 xs_format_peer_addresses(xprt
, "tcp",
2807 RPCBIND_NETID_TCP6
);
2810 ret
= ERR_PTR(-EAFNOSUPPORT
);
2814 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2815 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2816 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2817 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2820 * Once we've associated a backchannel xprt with a connection,
2821 * we want to keep it around as long as long as the connection
2822 * lasts, in case we need to start using it for a backchannel
2823 * again; this reference won't be dropped until bc_xprt is
2827 args
->bc_xprt
->xpt_bc_xprt
= xprt
;
2828 xprt
->bc_xprt
= args
->bc_xprt
;
2829 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
2830 transport
->sock
= bc_sock
->sk_sock
;
2831 transport
->inet
= bc_sock
->sk_sk
;
2834 * Since we don't want connections for the backchannel, we set
2835 * the xprt status to connected
2837 xprt_set_connected(xprt
);
2840 if (try_module_get(THIS_MODULE
))
2843 ret
= ERR_PTR(-EINVAL
);
2849 static struct xprt_class xs_local_transport
= {
2850 .list
= LIST_HEAD_INIT(xs_local_transport
.list
),
2851 .name
= "named UNIX socket",
2852 .owner
= THIS_MODULE
,
2853 .ident
= XPRT_TRANSPORT_LOCAL
,
2854 .setup
= xs_setup_local
,
2857 static struct xprt_class xs_udp_transport
= {
2858 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2860 .owner
= THIS_MODULE
,
2861 .ident
= XPRT_TRANSPORT_UDP
,
2862 .setup
= xs_setup_udp
,
2865 static struct xprt_class xs_tcp_transport
= {
2866 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
2868 .owner
= THIS_MODULE
,
2869 .ident
= XPRT_TRANSPORT_TCP
,
2870 .setup
= xs_setup_tcp
,
2873 static struct xprt_class xs_bc_tcp_transport
= {
2874 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
2875 .name
= "tcp NFSv4.1 backchannel",
2876 .owner
= THIS_MODULE
,
2877 .ident
= XPRT_TRANSPORT_BC_TCP
,
2878 .setup
= xs_setup_bc_tcp
,
2882 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2885 int init_socket_xprt(void)
2888 if (!sunrpc_table_header
)
2889 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
2892 xprt_register_transport(&xs_local_transport
);
2893 xprt_register_transport(&xs_udp_transport
);
2894 xprt_register_transport(&xs_tcp_transport
);
2895 xprt_register_transport(&xs_bc_tcp_transport
);
2901 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2904 void cleanup_socket_xprt(void)
2907 if (sunrpc_table_header
) {
2908 unregister_sysctl_table(sunrpc_table_header
);
2909 sunrpc_table_header
= NULL
;
2913 xprt_unregister_transport(&xs_local_transport
);
2914 xprt_unregister_transport(&xs_udp_transport
);
2915 xprt_unregister_transport(&xs_tcp_transport
);
2916 xprt_unregister_transport(&xs_bc_tcp_transport
);
2919 static int param_set_uint_minmax(const char *val
,
2920 const struct kernel_param
*kp
,
2921 unsigned int min
, unsigned int max
)
2928 ret
= strict_strtoul(val
, 0, &num
);
2929 if (ret
== -EINVAL
|| num
< min
|| num
> max
)
2931 *((unsigned int *)kp
->arg
) = num
;
2935 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
2937 return param_set_uint_minmax(val
, kp
,
2942 static struct kernel_param_ops param_ops_portnr
= {
2943 .set
= param_set_portnr
,
2944 .get
= param_get_uint
,
2947 #define param_check_portnr(name, p) \
2948 __param_check(name, p, unsigned int);
2950 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
2951 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
2953 static int param_set_slot_table_size(const char *val
,
2954 const struct kernel_param
*kp
)
2956 return param_set_uint_minmax(val
, kp
,
2958 RPC_MAX_SLOT_TABLE
);
2961 static struct kernel_param_ops param_ops_slot_table_size
= {
2962 .set
= param_set_slot_table_size
,
2963 .get
= param_get_uint
,
2966 #define param_check_slot_table_size(name, p) \
2967 __param_check(name, p, unsigned int);
2969 static int param_set_max_slot_table_size(const char *val
,
2970 const struct kernel_param
*kp
)
2972 return param_set_uint_minmax(val
, kp
,
2974 RPC_MAX_SLOT_TABLE_LIMIT
);
2977 static struct kernel_param_ops param_ops_max_slot_table_size
= {
2978 .set
= param_set_max_slot_table_size
,
2979 .get
= param_get_uint
,
2982 #define param_check_max_slot_table_size(name, p) \
2983 __param_check(name, p, unsigned int);
2985 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
2986 slot_table_size
, 0644);
2987 module_param_named(tcp_max_slot_table_entries
, xprt_max_tcp_slot_table_entries
,
2988 max_slot_table_size
, 0644);
2989 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
2990 slot_table_size
, 0644);