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/slab.h>
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/pagemap.h>
26 #include <linux/errno.h>
27 #include <linux/socket.h>
29 #include <linux/net.h>
31 #include <linux/udp.h>
32 #include <linux/tcp.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/sched.h>
35 #include <linux/sunrpc/svcsock.h>
36 #include <linux/sunrpc/xprtsock.h>
37 #include <linux/file.h>
38 #ifdef CONFIG_NFS_V4_1
39 #include <linux/sunrpc/bc_xprt.h>
43 #include <net/checksum.h>
51 unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
52 unsigned int xprt_tcp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
54 unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
55 unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
57 #define XS_TCP_LINGER_TO (15U * HZ)
58 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
61 * We can register our own files under /proc/sys/sunrpc by
62 * calling register_sysctl_table() again. The files in that
63 * directory become the union of all files registered there.
65 * We simply need to make sure that we don't collide with
66 * someone else's file names!
71 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
72 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
73 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
74 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
76 static struct ctl_table_header
*sunrpc_table_header
;
79 * FIXME: changing the UDP slot table size should also resize the UDP
80 * socket buffers for existing UDP transports
82 static ctl_table xs_tunables_table
[] = {
84 .ctl_name
= CTL_SLOTTABLE_UDP
,
85 .procname
= "udp_slot_table_entries",
86 .data
= &xprt_udp_slot_table_entries
,
87 .maxlen
= sizeof(unsigned int),
89 .proc_handler
= &proc_dointvec_minmax
,
90 .strategy
= &sysctl_intvec
,
91 .extra1
= &min_slot_table_size
,
92 .extra2
= &max_slot_table_size
95 .ctl_name
= CTL_SLOTTABLE_TCP
,
96 .procname
= "tcp_slot_table_entries",
97 .data
= &xprt_tcp_slot_table_entries
,
98 .maxlen
= sizeof(unsigned int),
100 .proc_handler
= &proc_dointvec_minmax
,
101 .strategy
= &sysctl_intvec
,
102 .extra1
= &min_slot_table_size
,
103 .extra2
= &max_slot_table_size
106 .ctl_name
= CTL_MIN_RESVPORT
,
107 .procname
= "min_resvport",
108 .data
= &xprt_min_resvport
,
109 .maxlen
= sizeof(unsigned int),
111 .proc_handler
= &proc_dointvec_minmax
,
112 .strategy
= &sysctl_intvec
,
113 .extra1
= &xprt_min_resvport_limit
,
114 .extra2
= &xprt_max_resvport_limit
117 .ctl_name
= CTL_MAX_RESVPORT
,
118 .procname
= "max_resvport",
119 .data
= &xprt_max_resvport
,
120 .maxlen
= sizeof(unsigned int),
122 .proc_handler
= &proc_dointvec_minmax
,
123 .strategy
= &sysctl_intvec
,
124 .extra1
= &xprt_min_resvport_limit
,
125 .extra2
= &xprt_max_resvport_limit
128 .procname
= "tcp_fin_timeout",
129 .data
= &xs_tcp_fin_timeout
,
130 .maxlen
= sizeof(xs_tcp_fin_timeout
),
132 .proc_handler
= &proc_dointvec_jiffies
,
133 .strategy
= sysctl_jiffies
140 static ctl_table sunrpc_table
[] = {
142 .ctl_name
= CTL_SUNRPC
,
143 .procname
= "sunrpc",
145 .child
= xs_tunables_table
155 * Time out for an RPC UDP socket connect. UDP socket connects are
156 * synchronous, but we set a timeout anyway in case of resource
157 * exhaustion on the local host.
159 #define XS_UDP_CONN_TO (5U * HZ)
162 * Wait duration for an RPC TCP connection to be established. Solaris
163 * NFS over TCP uses 60 seconds, for example, which is in line with how
164 * long a server takes to reboot.
166 #define XS_TCP_CONN_TO (60U * HZ)
169 * Wait duration for a reply from the RPC portmapper.
171 #define XS_BIND_TO (60U * HZ)
174 * Delay if a UDP socket connect error occurs. This is most likely some
175 * kind of resource problem on the local host.
177 #define XS_UDP_REEST_TO (2U * HZ)
180 * The reestablish timeout allows clients to delay for a bit before attempting
181 * to reconnect to a server that just dropped our connection.
183 * We implement an exponential backoff when trying to reestablish a TCP
184 * transport connection with the server. Some servers like to drop a TCP
185 * connection when they are overworked, so we start with a short timeout and
186 * increase over time if the server is down or not responding.
188 #define XS_TCP_INIT_REEST_TO (3U * HZ)
189 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
192 * TCP idle timeout; client drops the transport socket if it is idle
193 * for this long. Note that we also timeout UDP sockets to prevent
194 * holding port numbers when there is no RPC traffic.
196 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
199 # undef RPC_DEBUG_DATA
200 # define RPCDBG_FACILITY RPCDBG_TRANS
203 #ifdef RPC_DEBUG_DATA
204 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
206 u8
*buf
= (u8
*) packet
;
209 dprintk("RPC: %s\n", msg
);
210 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
214 dprintk("0x%04x ", j
);
216 dprintk("%02x%02x%02x%02x ",
217 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
222 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
229 struct rpc_xprt xprt
;
234 struct socket
* sock
;
238 * State of TCP reply receive
246 unsigned long tcp_copied
,
250 * Connection of transports
252 struct delayed_work connect_worker
;
253 struct sockaddr_storage srcaddr
;
254 unsigned short srcport
;
257 * UDP socket buffer size parameters
263 * Saved socket callback addresses
265 void (*old_data_ready
)(struct sock
*, int);
266 void (*old_state_change
)(struct sock
*);
267 void (*old_write_space
)(struct sock
*);
268 void (*old_error_report
)(struct sock
*);
272 * TCP receive state flags
274 #define TCP_RCV_LAST_FRAG (1UL << 0)
275 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
276 #define TCP_RCV_COPY_XID (1UL << 2)
277 #define TCP_RCV_COPY_DATA (1UL << 3)
278 #define TCP_RCV_READ_CALLDIR (1UL << 4)
279 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
284 #define TCP_RPC_REPLY (1UL << 6)
286 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
288 return (struct sockaddr
*) &xprt
->addr
;
291 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
293 return (struct sockaddr_in
*) &xprt
->addr
;
296 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
298 return (struct sockaddr_in6
*) &xprt
->addr
;
301 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
303 struct sockaddr
*sap
= xs_addr(xprt
);
304 struct sockaddr_in6
*sin6
;
305 struct sockaddr_in
*sin
;
308 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
309 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
311 switch (sap
->sa_family
) {
313 sin
= xs_addr_in(xprt
);
314 (void)snprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x",
315 NIPQUAD(sin
->sin_addr
.s_addr
));
318 sin6
= xs_addr_in6(xprt
);
319 (void)snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
324 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
327 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
329 struct sockaddr
*sap
= xs_addr(xprt
);
332 (void)snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
333 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
335 (void)snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
336 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
339 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
340 const char *protocol
,
343 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
344 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
345 xs_format_common_peer_addresses(xprt
);
346 xs_format_common_peer_ports(xprt
);
349 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
351 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
352 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
354 xs_format_common_peer_ports(xprt
);
357 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
361 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
363 case RPC_DISPLAY_PROTO
:
364 case RPC_DISPLAY_NETID
:
367 kfree(xprt
->address_strings
[i
]);
371 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
373 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
375 struct msghdr msg
= {
377 .msg_namelen
= addrlen
,
378 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
381 .iov_base
= vec
->iov_base
+ base
,
382 .iov_len
= vec
->iov_len
- base
,
385 if (iov
.iov_len
!= 0)
386 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
387 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
390 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
)
393 unsigned int remainder
;
396 remainder
= xdr
->page_len
- base
;
397 base
+= xdr
->page_base
;
398 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
401 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
402 int flags
= XS_SENDMSG_FLAGS
;
405 if (remainder
!= 0 || more
)
407 err
= sock
->ops
->sendpage(sock
, *ppage
, base
, len
, flags
);
408 if (remainder
== 0 || err
!= len
)
422 * xs_sendpages - write pages directly to a socket
423 * @sock: socket to send on
424 * @addr: UDP only -- address of destination
425 * @addrlen: UDP only -- length of destination address
426 * @xdr: buffer containing this request
427 * @base: starting position in the buffer
430 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
432 unsigned int remainder
= xdr
->len
- base
;
438 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
444 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
445 unsigned int len
= xdr
->head
[0].iov_len
- base
;
447 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
448 if (remainder
== 0 || err
!= len
)
453 base
-= xdr
->head
[0].iov_len
;
455 if (base
< xdr
->page_len
) {
456 unsigned int len
= xdr
->page_len
- base
;
458 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0);
459 if (remainder
== 0 || err
!= len
)
464 base
-= xdr
->page_len
;
466 if (base
>= xdr
->tail
[0].iov_len
)
468 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
477 static void xs_nospace_callback(struct rpc_task
*task
)
479 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
481 transport
->inet
->sk_write_pending
--;
482 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
486 * xs_nospace - place task on wait queue if transmit was incomplete
487 * @task: task to put to sleep
490 static int xs_nospace(struct rpc_task
*task
)
492 struct rpc_rqst
*req
= task
->tk_rqstp
;
493 struct rpc_xprt
*xprt
= req
->rq_xprt
;
494 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
497 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
498 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
501 /* Protect against races with write_space */
502 spin_lock_bh(&xprt
->transport_lock
);
504 /* Don't race with disconnect */
505 if (xprt_connected(xprt
)) {
506 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
509 * Notify TCP that we're limited by the application
512 set_bit(SOCK_NOSPACE
, &transport
->sock
->flags
);
513 transport
->inet
->sk_write_pending
++;
514 /* ...and wait for more buffer space */
515 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
518 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
522 spin_unlock_bh(&xprt
->transport_lock
);
527 * xs_udp_send_request - write an RPC request to a UDP socket
528 * @task: address of RPC task that manages the state of an RPC request
531 * 0: The request has been sent
532 * EAGAIN: The socket was blocked, please call again later to
533 * complete the request
534 * ENOTCONN: Caller needs to invoke connect logic then call again
535 * other: Some other error occured, the request was not sent
537 static int xs_udp_send_request(struct rpc_task
*task
)
539 struct rpc_rqst
*req
= task
->tk_rqstp
;
540 struct rpc_xprt
*xprt
= req
->rq_xprt
;
541 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
542 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
545 xs_pktdump("packet data:",
546 req
->rq_svec
->iov_base
,
547 req
->rq_svec
->iov_len
);
549 if (!xprt_bound(xprt
))
551 status
= xs_sendpages(transport
->sock
,
556 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
557 xdr
->len
- req
->rq_bytes_sent
, status
);
560 task
->tk_bytes_sent
+= status
;
561 if (status
>= req
->rq_slen
)
563 /* Still some bytes left; set up for a retry later. */
566 if (!transport
->sock
)
572 /* Should we call xs_close() here? */
575 status
= xs_nospace(task
);
578 dprintk("RPC: sendmsg returned unrecognized error %d\n",
583 /* When the server has died, an ICMP port unreachable message
584 * prompts ECONNREFUSED. */
585 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
592 * xs_tcp_shutdown - gracefully shut down a TCP socket
595 * Initiates a graceful shutdown of the TCP socket by calling the
596 * equivalent of shutdown(SHUT_WR);
598 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
600 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
601 struct socket
*sock
= transport
->sock
;
604 kernel_sock_shutdown(sock
, SHUT_WR
);
607 static inline void xs_encode_tcp_record_marker(struct xdr_buf
*buf
)
609 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
610 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
611 *base
= htonl(RPC_LAST_STREAM_FRAGMENT
| reclen
);
615 * xs_tcp_send_request - write an RPC request to a TCP socket
616 * @task: address of RPC task that manages the state of an RPC request
619 * 0: The request has been sent
620 * EAGAIN: The socket was blocked, please call again later to
621 * complete the request
622 * ENOTCONN: Caller needs to invoke connect logic then call again
623 * other: Some other error occured, the request was not sent
625 * XXX: In the case of soft timeouts, should we eventually give up
626 * if sendmsg is not able to make progress?
628 static int xs_tcp_send_request(struct rpc_task
*task
)
630 struct rpc_rqst
*req
= task
->tk_rqstp
;
631 struct rpc_xprt
*xprt
= req
->rq_xprt
;
632 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
633 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
636 xs_encode_tcp_record_marker(&req
->rq_snd_buf
);
638 xs_pktdump("packet data:",
639 req
->rq_svec
->iov_base
,
640 req
->rq_svec
->iov_len
);
642 /* Continue transmitting the packet/record. We must be careful
643 * to cope with writespace callbacks arriving _after_ we have
644 * called sendmsg(). */
646 status
= xs_sendpages(transport
->sock
,
647 NULL
, 0, xdr
, req
->rq_bytes_sent
);
649 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
650 xdr
->len
- req
->rq_bytes_sent
, status
);
652 if (unlikely(status
< 0))
655 /* If we've sent the entire packet, immediately
656 * reset the count of bytes sent. */
657 req
->rq_bytes_sent
+= status
;
658 task
->tk_bytes_sent
+= status
;
659 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
660 req
->rq_bytes_sent
= 0;
669 if (!transport
->sock
)
675 /* Should we call xs_close() here? */
678 status
= xs_nospace(task
);
681 dprintk("RPC: sendmsg returned unrecognized error %d\n",
685 xs_tcp_shutdown(xprt
);
688 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
695 * xs_tcp_release_xprt - clean up after a tcp transmission
699 * This cleans up if an error causes us to abort the transmission of a request.
700 * In this case, the socket may need to be reset in order to avoid confusing
703 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
705 struct rpc_rqst
*req
;
707 if (task
!= xprt
->snd_task
)
711 req
= task
->tk_rqstp
;
712 if (req
->rq_bytes_sent
== 0)
714 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
716 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
718 xprt_release_xprt(xprt
, task
);
721 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
723 transport
->old_data_ready
= sk
->sk_data_ready
;
724 transport
->old_state_change
= sk
->sk_state_change
;
725 transport
->old_write_space
= sk
->sk_write_space
;
726 transport
->old_error_report
= sk
->sk_error_report
;
729 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
731 sk
->sk_data_ready
= transport
->old_data_ready
;
732 sk
->sk_state_change
= transport
->old_state_change
;
733 sk
->sk_write_space
= transport
->old_write_space
;
734 sk
->sk_error_report
= transport
->old_error_report
;
737 static void xs_reset_transport(struct sock_xprt
*transport
)
739 struct socket
*sock
= transport
->sock
;
740 struct sock
*sk
= transport
->inet
;
745 write_lock_bh(&sk
->sk_callback_lock
);
746 transport
->inet
= NULL
;
747 transport
->sock
= NULL
;
749 sk
->sk_user_data
= NULL
;
751 xs_restore_old_callbacks(transport
, sk
);
752 write_unlock_bh(&sk
->sk_callback_lock
);
760 * xs_close - close a socket
763 * This is used when all requests are complete; ie, no DRC state remains
764 * on the server we want to save.
766 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
767 * xs_reset_transport() zeroing the socket from underneath a writer.
769 static void xs_close(struct rpc_xprt
*xprt
)
771 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
773 dprintk("RPC: xs_close xprt %p\n", xprt
);
775 xs_reset_transport(transport
);
777 smp_mb__before_clear_bit();
778 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
779 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
780 clear_bit(XPRT_CLOSING
, &xprt
->state
);
781 smp_mb__after_clear_bit();
782 xprt_disconnect_done(xprt
);
785 static void xs_tcp_close(struct rpc_xprt
*xprt
)
787 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
790 xs_tcp_shutdown(xprt
);
794 * xs_destroy - prepare to shutdown a transport
795 * @xprt: doomed transport
798 static void xs_destroy(struct rpc_xprt
*xprt
)
800 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
802 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
804 cancel_rearming_delayed_work(&transport
->connect_worker
);
807 xs_free_peer_addresses(xprt
);
810 module_put(THIS_MODULE
);
813 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
815 return (struct rpc_xprt
*) sk
->sk_user_data
;
819 * xs_udp_data_ready - "data ready" callback for UDP sockets
820 * @sk: socket with data to read
821 * @len: how much data to read
824 static void xs_udp_data_ready(struct sock
*sk
, int len
)
826 struct rpc_task
*task
;
827 struct rpc_xprt
*xprt
;
828 struct rpc_rqst
*rovr
;
830 int err
, repsize
, copied
;
834 read_lock(&sk
->sk_callback_lock
);
835 dprintk("RPC: xs_udp_data_ready...\n");
836 if (!(xprt
= xprt_from_sock(sk
)))
839 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
845 repsize
= skb
->len
- sizeof(struct udphdr
);
847 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
851 /* Copy the XID from the skb... */
852 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
853 sizeof(_xid
), &_xid
);
857 /* Look up and lock the request corresponding to the given XID */
858 spin_lock(&xprt
->transport_lock
);
859 rovr
= xprt_lookup_rqst(xprt
, *xp
);
862 task
= rovr
->rq_task
;
864 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
867 /* Suck it into the iovec, verify checksum if not done by hw. */
868 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
869 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
873 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
875 /* Something worked... */
876 dst_confirm(skb_dst(skb
));
878 xprt_adjust_cwnd(task
, copied
);
879 xprt_update_rtt(task
);
880 xprt_complete_rqst(task
, copied
);
883 spin_unlock(&xprt
->transport_lock
);
885 skb_free_datagram(sk
, skb
);
887 read_unlock(&sk
->sk_callback_lock
);
890 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
892 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
896 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
897 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
898 used
= xdr_skb_read_bits(desc
, p
, len
);
899 transport
->tcp_offset
+= used
;
903 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
904 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
905 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
907 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
908 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
910 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
911 transport
->tcp_offset
= 0;
913 /* Sanity check of the record length */
914 if (unlikely(transport
->tcp_reclen
< 8)) {
915 dprintk("RPC: invalid TCP record fragment length\n");
916 xprt_force_disconnect(xprt
);
919 dprintk("RPC: reading TCP record fragment of length %d\n",
920 transport
->tcp_reclen
);
923 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
925 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
926 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
927 transport
->tcp_offset
= 0;
928 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
929 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
930 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
931 transport
->tcp_copied
= 0;
936 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
941 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
942 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
943 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
944 used
= xdr_skb_read_bits(desc
, p
, len
);
945 transport
->tcp_offset
+= used
;
948 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
949 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
950 transport
->tcp_copied
= 4;
951 dprintk("RPC: reading %s XID %08x\n",
952 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
954 ntohl(transport
->tcp_xid
));
955 xs_tcp_check_fraghdr(transport
);
958 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
959 struct xdr_skb_reader
*desc
)
966 * We want transport->tcp_offset to be 8 at the end of this routine
967 * (4 bytes for the xid and 4 bytes for the call/reply flag).
968 * When this function is called for the first time,
969 * transport->tcp_offset is 4 (after having already read the xid).
971 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
972 len
= sizeof(calldir
) - offset
;
973 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
974 used
= xdr_skb_read_bits(desc
, &calldir
, len
);
975 transport
->tcp_offset
+= used
;
978 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
979 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
980 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
982 * We don't yet have the XDR buffer, so we will write the calldir
983 * out after we get the buffer from the 'struct rpc_rqst'
985 if (ntohl(calldir
) == RPC_REPLY
)
986 transport
->tcp_flags
|= TCP_RPC_REPLY
;
988 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
989 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
990 (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
991 "reply for" : "request with", calldir
);
992 xs_tcp_check_fraghdr(transport
);
995 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
996 struct xdr_skb_reader
*desc
,
997 struct rpc_rqst
*req
)
999 struct sock_xprt
*transport
=
1000 container_of(xprt
, struct sock_xprt
, xprt
);
1001 struct xdr_buf
*rcvbuf
;
1005 rcvbuf
= &req
->rq_private_buf
;
1007 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1009 * Save the RPC direction in the XDR buffer
1011 __be32 calldir
= transport
->tcp_flags
& TCP_RPC_REPLY
?
1012 htonl(RPC_REPLY
) : 0;
1014 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1015 &calldir
, sizeof(calldir
));
1016 transport
->tcp_copied
+= sizeof(calldir
);
1017 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1021 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1022 struct xdr_skb_reader my_desc
;
1024 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1025 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1026 my_desc
.count
= len
;
1027 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1028 &my_desc
, xdr_skb_read_bits
);
1032 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1033 desc
, xdr_skb_read_bits
);
1036 transport
->tcp_copied
+= r
;
1037 transport
->tcp_offset
+= r
;
1040 /* Error when copying to the receive buffer,
1041 * usually because we weren't able to allocate
1042 * additional buffer pages. All we can do now
1043 * is turn off TCP_RCV_COPY_DATA, so the request
1044 * will not receive any additional updates,
1046 * Any remaining data from this record will
1049 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1050 dprintk("RPC: XID %08x truncated request\n",
1051 ntohl(transport
->tcp_xid
));
1052 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1053 "tcp_offset = %u, tcp_reclen = %u\n",
1054 xprt
, transport
->tcp_copied
,
1055 transport
->tcp_offset
, transport
->tcp_reclen
);
1059 dprintk("RPC: XID %08x read %Zd bytes\n",
1060 ntohl(transport
->tcp_xid
), r
);
1061 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1062 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1063 transport
->tcp_offset
, transport
->tcp_reclen
);
1065 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1066 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1067 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1068 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1069 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1076 * Finds the request corresponding to the RPC xid and invokes the common
1077 * tcp read code to read the data.
1079 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1080 struct xdr_skb_reader
*desc
)
1082 struct sock_xprt
*transport
=
1083 container_of(xprt
, struct sock_xprt
, xprt
);
1084 struct rpc_rqst
*req
;
1086 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1088 /* Find and lock the request corresponding to this xid */
1089 spin_lock(&xprt
->transport_lock
);
1090 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1092 dprintk("RPC: XID %08x request not found!\n",
1093 ntohl(transport
->tcp_xid
));
1094 spin_unlock(&xprt
->transport_lock
);
1098 xs_tcp_read_common(xprt
, desc
, req
);
1100 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1101 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1103 spin_unlock(&xprt
->transport_lock
);
1107 #if defined(CONFIG_NFS_V4_1)
1109 * Obtains an rpc_rqst previously allocated and invokes the common
1110 * tcp read code to read the data. The result is placed in the callback
1112 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1113 * connection and return -1.
1115 static inline int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1116 struct xdr_skb_reader
*desc
)
1118 struct sock_xprt
*transport
=
1119 container_of(xprt
, struct sock_xprt
, xprt
);
1120 struct rpc_rqst
*req
;
1122 req
= xprt_alloc_bc_request(xprt
);
1124 printk(KERN_WARNING
"Callback slot table overflowed\n");
1125 xprt_force_disconnect(xprt
);
1129 req
->rq_xid
= transport
->tcp_xid
;
1130 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1131 xs_tcp_read_common(xprt
, desc
, req
);
1133 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
)) {
1134 struct svc_serv
*bc_serv
= xprt
->bc_serv
;
1137 * Add callback request to callback list. The callback
1138 * service sleeps on the sv_cb_waitq waiting for new
1139 * requests. Wake it up after adding enqueing the
1142 dprintk("RPC: add callback request to list\n");
1143 spin_lock(&bc_serv
->sv_cb_lock
);
1144 list_add(&req
->rq_bc_list
, &bc_serv
->sv_cb_list
);
1145 spin_unlock(&bc_serv
->sv_cb_lock
);
1146 wake_up(&bc_serv
->sv_cb_waitq
);
1149 req
->rq_private_buf
.len
= transport
->tcp_copied
;
1154 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1155 struct xdr_skb_reader
*desc
)
1157 struct sock_xprt
*transport
=
1158 container_of(xprt
, struct sock_xprt
, xprt
);
1160 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1161 xs_tcp_read_reply(xprt
, desc
) :
1162 xs_tcp_read_callback(xprt
, desc
);
1165 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1166 struct xdr_skb_reader
*desc
)
1168 return xs_tcp_read_reply(xprt
, desc
);
1170 #endif /* CONFIG_NFS_V4_1 */
1173 * Read data off the transport. This can be either an RPC_CALL or an
1174 * RPC_REPLY. Relay the processing to helper functions.
1176 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1177 struct xdr_skb_reader
*desc
)
1179 struct sock_xprt
*transport
=
1180 container_of(xprt
, struct sock_xprt
, xprt
);
1182 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1183 xs_tcp_check_fraghdr(transport
);
1186 * The transport_lock protects the request handling.
1187 * There's no need to hold it to update the tcp_flags.
1189 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1193 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1197 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1198 if (len
> desc
->count
)
1201 desc
->offset
+= len
;
1202 transport
->tcp_offset
+= len
;
1203 dprintk("RPC: discarded %Zu bytes\n", len
);
1204 xs_tcp_check_fraghdr(transport
);
1207 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1209 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1210 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1211 struct xdr_skb_reader desc
= {
1217 dprintk("RPC: xs_tcp_data_recv started\n");
1219 /* Read in a new fragment marker if necessary */
1220 /* Can we ever really expect to get completely empty fragments? */
1221 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1222 xs_tcp_read_fraghdr(xprt
, &desc
);
1225 /* Read in the xid if necessary */
1226 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1227 xs_tcp_read_xid(transport
, &desc
);
1230 /* Read in the call/reply flag */
1231 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1232 xs_tcp_read_calldir(transport
, &desc
);
1235 /* Read in the request data */
1236 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1237 xs_tcp_read_data(xprt
, &desc
);
1240 /* Skip over any trailing bytes on short reads */
1241 xs_tcp_read_discard(transport
, &desc
);
1242 } while (desc
.count
);
1243 dprintk("RPC: xs_tcp_data_recv done\n");
1244 return len
- desc
.count
;
1248 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1249 * @sk: socket with data to read
1250 * @bytes: how much data to read
1253 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1255 struct rpc_xprt
*xprt
;
1256 read_descriptor_t rd_desc
;
1259 dprintk("RPC: xs_tcp_data_ready...\n");
1261 read_lock(&sk
->sk_callback_lock
);
1262 if (!(xprt
= xprt_from_sock(sk
)))
1267 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1268 rd_desc
.arg
.data
= xprt
;
1270 rd_desc
.count
= 65536;
1271 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1274 read_unlock(&sk
->sk_callback_lock
);
1278 * Do the equivalent of linger/linger2 handling for dealing with
1279 * broken servers that don't close the socket in a timely
1282 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1283 unsigned long timeout
)
1285 struct sock_xprt
*transport
;
1287 if (xprt_test_and_set_connecting(xprt
))
1289 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1290 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1291 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1295 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1297 struct sock_xprt
*transport
;
1299 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1301 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1302 !cancel_delayed_work(&transport
->connect_worker
))
1304 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1305 xprt_clear_connecting(xprt
);
1308 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1310 smp_mb__before_clear_bit();
1311 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1312 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1313 smp_mb__after_clear_bit();
1314 /* Mark transport as closed and wake up all pending tasks */
1315 xprt_disconnect_done(xprt
);
1319 * xs_tcp_state_change - callback to handle TCP socket state changes
1320 * @sk: socket whose state has changed
1323 static void xs_tcp_state_change(struct sock
*sk
)
1325 struct rpc_xprt
*xprt
;
1327 read_lock(&sk
->sk_callback_lock
);
1328 if (!(xprt
= xprt_from_sock(sk
)))
1330 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1331 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1332 sk
->sk_state
, xprt_connected(xprt
),
1333 sock_flag(sk
, SOCK_DEAD
),
1334 sock_flag(sk
, SOCK_ZAPPED
));
1336 switch (sk
->sk_state
) {
1337 case TCP_ESTABLISHED
:
1338 spin_lock_bh(&xprt
->transport_lock
);
1339 if (!xprt_test_and_set_connected(xprt
)) {
1340 struct sock_xprt
*transport
= container_of(xprt
,
1341 struct sock_xprt
, xprt
);
1343 /* Reset TCP record info */
1344 transport
->tcp_offset
= 0;
1345 transport
->tcp_reclen
= 0;
1346 transport
->tcp_copied
= 0;
1347 transport
->tcp_flags
=
1348 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1350 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1352 spin_unlock_bh(&xprt
->transport_lock
);
1355 /* The client initiated a shutdown of the socket */
1356 xprt
->connect_cookie
++;
1357 xprt
->reestablish_timeout
= 0;
1358 set_bit(XPRT_CLOSING
, &xprt
->state
);
1359 smp_mb__before_clear_bit();
1360 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1361 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1362 smp_mb__after_clear_bit();
1363 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1365 case TCP_CLOSE_WAIT
:
1366 /* The server initiated a shutdown of the socket */
1367 xprt_force_disconnect(xprt
);
1369 xprt
->connect_cookie
++;
1372 * If the server closed down the connection, make sure that
1373 * we back off before reconnecting
1375 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1376 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1379 set_bit(XPRT_CLOSING
, &xprt
->state
);
1380 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1381 smp_mb__before_clear_bit();
1382 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1383 smp_mb__after_clear_bit();
1386 xs_tcp_cancel_linger_timeout(xprt
);
1387 xs_sock_mark_closed(xprt
);
1390 read_unlock(&sk
->sk_callback_lock
);
1394 * xs_error_report - callback mainly for catching socket errors
1397 static void xs_error_report(struct sock
*sk
)
1399 struct rpc_xprt
*xprt
;
1401 read_lock(&sk
->sk_callback_lock
);
1402 if (!(xprt
= xprt_from_sock(sk
)))
1404 dprintk("RPC: %s client %p...\n"
1406 __func__
, xprt
, sk
->sk_err
);
1407 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1409 read_unlock(&sk
->sk_callback_lock
);
1412 static void xs_write_space(struct sock
*sk
)
1414 struct socket
*sock
;
1415 struct rpc_xprt
*xprt
;
1417 if (unlikely(!(sock
= sk
->sk_socket
)))
1419 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1421 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1423 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1426 xprt_write_space(xprt
);
1430 * xs_udp_write_space - callback invoked when socket buffer space
1432 * @sk: socket whose state has changed
1434 * Called when more output buffer space is available for this socket.
1435 * We try not to wake our writers until they can make "significant"
1436 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1437 * with a bunch of small requests.
1439 static void xs_udp_write_space(struct sock
*sk
)
1441 read_lock(&sk
->sk_callback_lock
);
1443 /* from net/core/sock.c:sock_def_write_space */
1444 if (sock_writeable(sk
))
1447 read_unlock(&sk
->sk_callback_lock
);
1451 * xs_tcp_write_space - callback invoked when socket buffer space
1453 * @sk: socket whose state has changed
1455 * Called when more output buffer space is available for this socket.
1456 * We try not to wake our writers until they can make "significant"
1457 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1458 * with a bunch of small requests.
1460 static void xs_tcp_write_space(struct sock
*sk
)
1462 read_lock(&sk
->sk_callback_lock
);
1464 /* from net/core/stream.c:sk_stream_write_space */
1465 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
))
1468 read_unlock(&sk
->sk_callback_lock
);
1471 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1473 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1474 struct sock
*sk
= transport
->inet
;
1476 if (transport
->rcvsize
) {
1477 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1478 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1480 if (transport
->sndsize
) {
1481 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1482 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1483 sk
->sk_write_space(sk
);
1488 * xs_udp_set_buffer_size - set send and receive limits
1489 * @xprt: generic transport
1490 * @sndsize: requested size of send buffer, in bytes
1491 * @rcvsize: requested size of receive buffer, in bytes
1493 * Set socket send and receive buffer size limits.
1495 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1497 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1499 transport
->sndsize
= 0;
1501 transport
->sndsize
= sndsize
+ 1024;
1502 transport
->rcvsize
= 0;
1504 transport
->rcvsize
= rcvsize
+ 1024;
1506 xs_udp_do_set_buffer_size(xprt
);
1510 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1511 * @task: task that timed out
1513 * Adjust the congestion window after a retransmit timeout has occurred.
1515 static void xs_udp_timer(struct rpc_task
*task
)
1517 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
1520 static unsigned short xs_get_random_port(void)
1522 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1523 unsigned short rand
= (unsigned short) net_random() % range
;
1524 return rand
+ xprt_min_resvport
;
1528 * xs_set_port - reset the port number in the remote endpoint address
1529 * @xprt: generic transport
1530 * @port: new port number
1533 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1535 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1537 rpc_set_port(xs_addr(xprt
), port
);
1538 xs_update_peer_port(xprt
);
1541 static unsigned short xs_get_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1543 unsigned short port
= transport
->srcport
;
1545 if (port
== 0 && transport
->xprt
.resvport
)
1546 port
= xs_get_random_port();
1550 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, struct socket
*sock
, unsigned short port
)
1552 if (transport
->srcport
!= 0)
1553 transport
->srcport
= 0;
1554 if (!transport
->xprt
.resvport
)
1556 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1557 return xprt_max_resvport
;
1561 static int xs_bind4(struct sock_xprt
*transport
, struct socket
*sock
)
1563 struct sockaddr_in myaddr
= {
1564 .sin_family
= AF_INET
,
1566 struct sockaddr_in
*sa
;
1568 unsigned short port
= xs_get_srcport(transport
, sock
);
1569 unsigned short last
;
1571 sa
= (struct sockaddr_in
*)&transport
->srcaddr
;
1572 myaddr
.sin_addr
= sa
->sin_addr
;
1574 myaddr
.sin_port
= htons(port
);
1575 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1580 transport
->srcport
= port
;
1584 port
= xs_next_srcport(transport
, sock
, port
);
1587 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1588 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1589 __func__
, &myaddr
.sin_addr
,
1590 port
, err
? "failed" : "ok", err
);
1594 static int xs_bind6(struct sock_xprt
*transport
, struct socket
*sock
)
1596 struct sockaddr_in6 myaddr
= {
1597 .sin6_family
= AF_INET6
,
1599 struct sockaddr_in6
*sa
;
1601 unsigned short port
= xs_get_srcport(transport
, sock
);
1602 unsigned short last
;
1604 sa
= (struct sockaddr_in6
*)&transport
->srcaddr
;
1605 myaddr
.sin6_addr
= sa
->sin6_addr
;
1607 myaddr
.sin6_port
= htons(port
);
1608 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1613 transport
->srcport
= port
;
1617 port
= xs_next_srcport(transport
, sock
, port
);
1620 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1621 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1622 &myaddr
.sin6_addr
, port
, err
? "failed" : "ok", err
);
1626 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1627 static struct lock_class_key xs_key
[2];
1628 static struct lock_class_key xs_slock_key
[2];
1630 static inline void xs_reclassify_socket4(struct socket
*sock
)
1632 struct sock
*sk
= sock
->sk
;
1634 BUG_ON(sock_owned_by_user(sk
));
1635 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1636 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1639 static inline void xs_reclassify_socket6(struct socket
*sock
)
1641 struct sock
*sk
= sock
->sk
;
1643 BUG_ON(sock_owned_by_user(sk
));
1644 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1645 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1648 static inline void xs_reclassify_socket4(struct socket
*sock
)
1652 static inline void xs_reclassify_socket6(struct socket
*sock
)
1657 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1659 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1661 if (!transport
->inet
) {
1662 struct sock
*sk
= sock
->sk
;
1664 write_lock_bh(&sk
->sk_callback_lock
);
1666 xs_save_old_callbacks(transport
, sk
);
1668 sk
->sk_user_data
= xprt
;
1669 sk
->sk_data_ready
= xs_udp_data_ready
;
1670 sk
->sk_write_space
= xs_udp_write_space
;
1671 sk
->sk_error_report
= xs_error_report
;
1672 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1673 sk
->sk_allocation
= GFP_ATOMIC
;
1675 xprt_set_connected(xprt
);
1677 /* Reset to new socket */
1678 transport
->sock
= sock
;
1679 transport
->inet
= sk
;
1681 write_unlock_bh(&sk
->sk_callback_lock
);
1683 xs_udp_do_set_buffer_size(xprt
);
1687 * xs_udp_connect_worker4 - set up a UDP socket
1688 * @work: RPC transport to connect
1690 * Invoked by a work queue tasklet.
1692 static void xs_udp_connect_worker4(struct work_struct
*work
)
1694 struct sock_xprt
*transport
=
1695 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1696 struct rpc_xprt
*xprt
= &transport
->xprt
;
1697 struct socket
*sock
= transport
->sock
;
1698 int err
, status
= -EIO
;
1703 /* Start by resetting any existing state */
1704 xs_reset_transport(transport
);
1706 err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1708 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1711 xs_reclassify_socket4(sock
);
1713 if (xs_bind4(transport
, sock
)) {
1718 dprintk("RPC: worker connecting xprt %p via %s to "
1719 "%s (port %s)\n", xprt
,
1720 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1721 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1722 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1724 xs_udp_finish_connecting(xprt
, sock
);
1727 xprt_clear_connecting(xprt
);
1728 xprt_wake_pending_tasks(xprt
, status
);
1732 * xs_udp_connect_worker6 - set up a UDP socket
1733 * @work: RPC transport to connect
1735 * Invoked by a work queue tasklet.
1737 static void xs_udp_connect_worker6(struct work_struct
*work
)
1739 struct sock_xprt
*transport
=
1740 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1741 struct rpc_xprt
*xprt
= &transport
->xprt
;
1742 struct socket
*sock
= transport
->sock
;
1743 int err
, status
= -EIO
;
1748 /* Start by resetting any existing state */
1749 xs_reset_transport(transport
);
1751 err
= sock_create_kern(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1753 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1756 xs_reclassify_socket6(sock
);
1758 if (xs_bind6(transport
, sock
) < 0) {
1763 dprintk("RPC: worker connecting xprt %p via %s to "
1764 "%s (port %s)\n", xprt
,
1765 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1766 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1767 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1769 xs_udp_finish_connecting(xprt
, sock
);
1772 xprt_clear_connecting(xprt
);
1773 xprt_wake_pending_tasks(xprt
, status
);
1777 * We need to preserve the port number so the reply cache on the server can
1778 * find our cached RPC replies when we get around to reconnecting.
1780 static void xs_abort_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1783 struct sockaddr any
;
1785 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1788 * Disconnect the transport socket by doing a connect operation
1789 * with AF_UNSPEC. This should return immediately...
1791 memset(&any
, 0, sizeof(any
));
1792 any
.sa_family
= AF_UNSPEC
;
1793 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
1795 xs_sock_mark_closed(xprt
);
1797 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1801 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1803 unsigned int state
= transport
->inet
->sk_state
;
1805 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
)
1807 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
))
1809 xs_abort_connection(xprt
, transport
);
1812 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1814 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1816 if (!transport
->inet
) {
1817 struct sock
*sk
= sock
->sk
;
1819 write_lock_bh(&sk
->sk_callback_lock
);
1821 xs_save_old_callbacks(transport
, sk
);
1823 sk
->sk_user_data
= xprt
;
1824 sk
->sk_data_ready
= xs_tcp_data_ready
;
1825 sk
->sk_state_change
= xs_tcp_state_change
;
1826 sk
->sk_write_space
= xs_tcp_write_space
;
1827 sk
->sk_error_report
= xs_error_report
;
1828 sk
->sk_allocation
= GFP_ATOMIC
;
1830 /* socket options */
1831 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1832 sock_reset_flag(sk
, SOCK_LINGER
);
1833 tcp_sk(sk
)->linger2
= 0;
1834 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1836 xprt_clear_connected(xprt
);
1838 /* Reset to new socket */
1839 transport
->sock
= sock
;
1840 transport
->inet
= sk
;
1842 write_unlock_bh(&sk
->sk_callback_lock
);
1845 if (!xprt_bound(xprt
))
1848 /* Tell the socket layer to start connecting... */
1849 xprt
->stat
.connect_count
++;
1850 xprt
->stat
.connect_start
= jiffies
;
1851 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
1855 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1856 * @xprt: RPC transport to connect
1857 * @transport: socket transport to connect
1858 * @create_sock: function to create a socket of the correct type
1860 * Invoked by a work queue tasklet.
1862 static void xs_tcp_setup_socket(struct rpc_xprt
*xprt
,
1863 struct sock_xprt
*transport
,
1864 struct socket
*(*create_sock
)(struct rpc_xprt
*,
1865 struct sock_xprt
*))
1867 struct socket
*sock
= transport
->sock
;
1874 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1875 sock
= create_sock(xprt
, transport
);
1877 status
= PTR_ERR(sock
);
1883 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
1885 /* "close" the socket, preserving the local port */
1886 xs_tcp_reuse_connection(xprt
, transport
);
1892 dprintk("RPC: worker connecting xprt %p via %s to "
1893 "%s (port %s)\n", xprt
,
1894 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1895 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1896 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1898 status
= xs_tcp_finish_connecting(xprt
, sock
);
1899 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1900 xprt
, -status
, xprt_connected(xprt
),
1901 sock
->sk
->sk_state
);
1904 printk("%s: connect returned unhandled error %d\n",
1906 case -EADDRNOTAVAIL
:
1907 /* We're probably in TIME_WAIT. Get rid of existing socket,
1910 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1911 xprt_force_disconnect(xprt
);
1916 /* retry with existing socket, after a delay */
1920 xprt_clear_connecting(xprt
);
1926 xprt_clear_connecting(xprt
);
1927 xprt_wake_pending_tasks(xprt
, status
);
1930 static struct socket
*xs_create_tcp_sock4(struct rpc_xprt
*xprt
,
1931 struct sock_xprt
*transport
)
1933 struct socket
*sock
;
1936 /* start from scratch */
1937 err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1939 dprintk("RPC: can't create TCP transport socket (%d).\n",
1943 xs_reclassify_socket4(sock
);
1945 if (xs_bind4(transport
, sock
) < 0) {
1951 return ERR_PTR(-EIO
);
1955 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1956 * @work: RPC transport to connect
1958 * Invoked by a work queue tasklet.
1960 static void xs_tcp_connect_worker4(struct work_struct
*work
)
1962 struct sock_xprt
*transport
=
1963 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1964 struct rpc_xprt
*xprt
= &transport
->xprt
;
1966 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock4
);
1969 static struct socket
*xs_create_tcp_sock6(struct rpc_xprt
*xprt
,
1970 struct sock_xprt
*transport
)
1972 struct socket
*sock
;
1975 /* start from scratch */
1976 err
= sock_create_kern(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1978 dprintk("RPC: can't create TCP transport socket (%d).\n",
1982 xs_reclassify_socket6(sock
);
1984 if (xs_bind6(transport
, sock
) < 0) {
1990 return ERR_PTR(-EIO
);
1994 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1995 * @work: RPC transport to connect
1997 * Invoked by a work queue tasklet.
1999 static void xs_tcp_connect_worker6(struct work_struct
*work
)
2001 struct sock_xprt
*transport
=
2002 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2003 struct rpc_xprt
*xprt
= &transport
->xprt
;
2005 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock6
);
2009 * xs_connect - connect a socket to a remote endpoint
2010 * @task: address of RPC task that manages state of connect request
2012 * TCP: If the remote end dropped the connection, delay reconnecting.
2014 * UDP socket connects are synchronous, but we use a work queue anyway
2015 * to guarantee that even unprivileged user processes can set up a
2016 * socket on a privileged port.
2018 * If a UDP socket connect fails, the delay behavior here prevents
2019 * retry floods (hard mounts).
2021 static void xs_connect(struct rpc_task
*task
)
2023 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2024 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2026 if (xprt_test_and_set_connecting(xprt
))
2029 if (transport
->sock
!= NULL
) {
2030 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2032 xprt
, xprt
->reestablish_timeout
/ HZ
);
2033 queue_delayed_work(rpciod_workqueue
,
2034 &transport
->connect_worker
,
2035 xprt
->reestablish_timeout
);
2036 xprt
->reestablish_timeout
<<= 1;
2037 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2038 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2040 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2041 queue_delayed_work(rpciod_workqueue
,
2042 &transport
->connect_worker
, 0);
2046 static void xs_tcp_connect(struct rpc_task
*task
)
2048 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2050 /* Exit if we need to wait for socket shutdown to complete */
2051 if (test_bit(XPRT_CLOSING
, &xprt
->state
))
2057 * xs_udp_print_stats - display UDP socket-specifc stats
2058 * @xprt: rpc_xprt struct containing statistics
2062 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2064 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2066 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2068 xprt
->stat
.bind_count
,
2071 xprt
->stat
.bad_xids
,
2073 xprt
->stat
.bklog_u
);
2077 * xs_tcp_print_stats - display TCP socket-specifc stats
2078 * @xprt: rpc_xprt struct containing statistics
2082 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2084 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2087 if (xprt_connected(xprt
))
2088 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2090 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2092 xprt
->stat
.bind_count
,
2093 xprt
->stat
.connect_count
,
2094 xprt
->stat
.connect_time
,
2098 xprt
->stat
.bad_xids
,
2100 xprt
->stat
.bklog_u
);
2104 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2105 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2106 * to use the server side send routines.
2108 void *bc_malloc(struct rpc_task
*task
, size_t size
)
2111 struct rpc_buffer
*buf
;
2113 BUG_ON(size
> PAGE_SIZE
- sizeof(struct rpc_buffer
));
2114 page
= alloc_page(GFP_KERNEL
);
2119 buf
= page_address(page
);
2120 buf
->len
= PAGE_SIZE
;
2126 * Free the space allocated in the bc_alloc routine
2128 void bc_free(void *buffer
)
2130 struct rpc_buffer
*buf
;
2135 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2136 free_page((unsigned long)buf
);
2140 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2141 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2143 static int bc_sendto(struct rpc_rqst
*req
)
2146 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2147 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2148 struct sock_xprt
*transport
=
2149 container_of(xprt
, struct sock_xprt
, xprt
);
2150 struct socket
*sock
= transport
->sock
;
2151 unsigned long headoff
;
2152 unsigned long tailoff
;
2155 * Set up the rpc header and record marker stuff
2157 xs_encode_tcp_record_marker(xbufp
);
2159 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2160 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2161 len
= svc_send_common(sock
, xbufp
,
2162 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2163 xbufp
->tail
[0].iov_base
, tailoff
);
2165 if (len
!= xbufp
->len
) {
2166 printk(KERN_NOTICE
"Error sending entire callback!\n");
2174 * The send routine. Borrows from svc_send
2176 static int bc_send_request(struct rpc_task
*task
)
2178 struct rpc_rqst
*req
= task
->tk_rqstp
;
2179 struct svc_xprt
*xprt
;
2180 struct svc_sock
*svsk
;
2183 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2185 * Get the server socket associated with this callback xprt
2187 xprt
= req
->rq_xprt
->bc_xprt
;
2188 svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
2191 * Grab the mutex to serialize data as the connection is shared
2192 * with the fore channel
2194 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2195 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2196 if (!mutex_trylock(&xprt
->xpt_mutex
))
2198 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2200 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2203 len
= bc_sendto(req
);
2204 mutex_unlock(&xprt
->xpt_mutex
);
2213 * The close routine. Since this is client initiated, we do nothing
2216 static void bc_close(struct rpc_xprt
*xprt
)
2222 * The xprt destroy routine. Again, because this connection is client
2223 * initiated, we do nothing
2226 static void bc_destroy(struct rpc_xprt
*xprt
)
2231 static struct rpc_xprt_ops xs_udp_ops
= {
2232 .set_buffer_size
= xs_udp_set_buffer_size
,
2233 .reserve_xprt
= xprt_reserve_xprt_cong
,
2234 .release_xprt
= xprt_release_xprt_cong
,
2235 .rpcbind
= rpcb_getport_async
,
2236 .set_port
= xs_set_port
,
2237 .connect
= xs_connect
,
2238 .buf_alloc
= rpc_malloc
,
2239 .buf_free
= rpc_free
,
2240 .send_request
= xs_udp_send_request
,
2241 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2242 .timer
= xs_udp_timer
,
2243 .release_request
= xprt_release_rqst_cong
,
2245 .destroy
= xs_destroy
,
2246 .print_stats
= xs_udp_print_stats
,
2249 static struct rpc_xprt_ops xs_tcp_ops
= {
2250 .reserve_xprt
= xprt_reserve_xprt
,
2251 .release_xprt
= xs_tcp_release_xprt
,
2252 .rpcbind
= rpcb_getport_async
,
2253 .set_port
= xs_set_port
,
2254 .connect
= xs_tcp_connect
,
2255 .buf_alloc
= rpc_malloc
,
2256 .buf_free
= rpc_free
,
2257 .send_request
= xs_tcp_send_request
,
2258 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2259 #if defined(CONFIG_NFS_V4_1)
2260 .release_request
= bc_release_request
,
2261 #endif /* CONFIG_NFS_V4_1 */
2262 .close
= xs_tcp_close
,
2263 .destroy
= xs_destroy
,
2264 .print_stats
= xs_tcp_print_stats
,
2268 * The rpc_xprt_ops for the server backchannel
2271 static struct rpc_xprt_ops bc_tcp_ops
= {
2272 .reserve_xprt
= xprt_reserve_xprt
,
2273 .release_xprt
= xprt_release_xprt
,
2274 .buf_alloc
= bc_malloc
,
2275 .buf_free
= bc_free
,
2276 .send_request
= bc_send_request
,
2277 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2279 .destroy
= bc_destroy
,
2280 .print_stats
= xs_tcp_print_stats
,
2283 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2284 unsigned int slot_table_size
)
2286 struct rpc_xprt
*xprt
;
2287 struct sock_xprt
*new;
2289 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2290 dprintk("RPC: xs_setup_xprt: address too large\n");
2291 return ERR_PTR(-EBADF
);
2294 new = kzalloc(sizeof(*new), GFP_KERNEL
);
2296 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2298 return ERR_PTR(-ENOMEM
);
2302 xprt
->max_reqs
= slot_table_size
;
2303 xprt
->slot
= kcalloc(xprt
->max_reqs
, sizeof(struct rpc_rqst
), GFP_KERNEL
);
2304 if (xprt
->slot
== NULL
) {
2306 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2308 return ERR_PTR(-ENOMEM
);
2311 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2312 xprt
->addrlen
= args
->addrlen
;
2314 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2319 static const struct rpc_timeout xs_udp_default_timeout
= {
2320 .to_initval
= 5 * HZ
,
2321 .to_maxval
= 30 * HZ
,
2322 .to_increment
= 5 * HZ
,
2327 * xs_setup_udp - Set up transport to use a UDP socket
2328 * @args: rpc transport creation arguments
2331 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2333 struct sockaddr
*addr
= args
->dstaddr
;
2334 struct rpc_xprt
*xprt
;
2335 struct sock_xprt
*transport
;
2337 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
);
2340 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2342 xprt
->prot
= IPPROTO_UDP
;
2344 /* XXX: header size can vary due to auth type, IPv6, etc. */
2345 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2347 xprt
->bind_timeout
= XS_BIND_TO
;
2348 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
2349 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2350 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2352 xprt
->ops
= &xs_udp_ops
;
2354 xprt
->timeout
= &xs_udp_default_timeout
;
2356 switch (addr
->sa_family
) {
2358 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2359 xprt_set_bound(xprt
);
2361 INIT_DELAYED_WORK(&transport
->connect_worker
,
2362 xs_udp_connect_worker4
);
2363 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2366 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2367 xprt_set_bound(xprt
);
2369 INIT_DELAYED_WORK(&transport
->connect_worker
,
2370 xs_udp_connect_worker6
);
2371 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2375 return ERR_PTR(-EAFNOSUPPORT
);
2378 if (xprt_bound(xprt
))
2379 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2380 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2381 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2382 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2384 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2385 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2386 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2388 if (try_module_get(THIS_MODULE
))
2393 return ERR_PTR(-EINVAL
);
2396 static const struct rpc_timeout xs_tcp_default_timeout
= {
2397 .to_initval
= 60 * HZ
,
2398 .to_maxval
= 60 * HZ
,
2403 * xs_setup_tcp - Set up transport to use a TCP socket
2404 * @args: rpc transport creation arguments
2407 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2409 struct sockaddr
*addr
= args
->dstaddr
;
2410 struct rpc_xprt
*xprt
;
2411 struct sock_xprt
*transport
;
2413 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2416 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2418 xprt
->prot
= IPPROTO_TCP
;
2419 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2420 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2422 xprt
->bind_timeout
= XS_BIND_TO
;
2423 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
2424 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2425 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2427 xprt
->ops
= &xs_tcp_ops
;
2428 xprt
->timeout
= &xs_tcp_default_timeout
;
2430 switch (addr
->sa_family
) {
2432 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2433 xprt_set_bound(xprt
);
2435 INIT_DELAYED_WORK(&transport
->connect_worker
,
2436 xs_tcp_connect_worker4
);
2437 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2440 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2441 xprt_set_bound(xprt
);
2443 INIT_DELAYED_WORK(&transport
->connect_worker
,
2444 xs_tcp_connect_worker6
);
2445 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2449 return ERR_PTR(-EAFNOSUPPORT
);
2452 if (xprt_bound(xprt
))
2453 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2454 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2455 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2456 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2458 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2459 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2460 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2463 if (try_module_get(THIS_MODULE
))
2468 return ERR_PTR(-EINVAL
);
2472 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2473 * @args: rpc transport creation arguments
2476 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2478 struct sockaddr
*addr
= args
->dstaddr
;
2479 struct rpc_xprt
*xprt
;
2480 struct sock_xprt
*transport
;
2481 struct svc_sock
*bc_sock
;
2486 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2489 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2491 xprt
->prot
= IPPROTO_TCP
;
2492 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2493 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2494 xprt
->timeout
= &xs_tcp_default_timeout
;
2497 xprt_set_bound(xprt
);
2498 xprt
->bind_timeout
= 0;
2499 xprt
->connect_timeout
= 0;
2500 xprt
->reestablish_timeout
= 0;
2501 xprt
->idle_timeout
= 0;
2504 * The backchannel uses the same socket connection as the
2507 xprt
->bc_xprt
= args
->bc_xprt
;
2508 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
2509 bc_sock
->sk_bc_xprt
= xprt
;
2510 transport
->sock
= bc_sock
->sk_sock
;
2511 transport
->inet
= bc_sock
->sk_sk
;
2513 xprt
->ops
= &bc_tcp_ops
;
2515 switch (addr
->sa_family
) {
2517 xs_format_peer_addresses(xprt
, "tcp",
2521 xs_format_peer_addresses(xprt
, "tcp",
2522 RPCBIND_NETID_TCP6
);
2526 return ERR_PTR(-EAFNOSUPPORT
);
2529 if (xprt_bound(xprt
))
2530 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2531 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2532 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2533 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2535 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2536 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2537 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2540 * Since we don't want connections for the backchannel, we set
2541 * the xprt status to connected
2543 xprt_set_connected(xprt
);
2546 if (try_module_get(THIS_MODULE
))
2550 return ERR_PTR(-EINVAL
);
2553 static struct xprt_class xs_udp_transport
= {
2554 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2556 .owner
= THIS_MODULE
,
2557 .ident
= XPRT_TRANSPORT_UDP
,
2558 .setup
= xs_setup_udp
,
2561 static struct xprt_class xs_tcp_transport
= {
2562 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
2564 .owner
= THIS_MODULE
,
2565 .ident
= XPRT_TRANSPORT_TCP
,
2566 .setup
= xs_setup_tcp
,
2569 static struct xprt_class xs_bc_tcp_transport
= {
2570 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
2571 .name
= "tcp NFSv4.1 backchannel",
2572 .owner
= THIS_MODULE
,
2573 .ident
= XPRT_TRANSPORT_BC_TCP
,
2574 .setup
= xs_setup_bc_tcp
,
2578 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2581 int init_socket_xprt(void)
2584 if (!sunrpc_table_header
)
2585 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
2588 xprt_register_transport(&xs_udp_transport
);
2589 xprt_register_transport(&xs_tcp_transport
);
2590 xprt_register_transport(&xs_bc_tcp_transport
);
2596 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2599 void cleanup_socket_xprt(void)
2602 if (sunrpc_table_header
) {
2603 unregister_sysctl_table(sunrpc_table_header
);
2604 sunrpc_table_header
= NULL
;
2608 xprt_unregister_transport(&xs_udp_transport
);
2609 xprt_unregister_transport(&xs_tcp_transport
);
2610 xprt_unregister_transport(&xs_bc_tcp_transport
);
2613 static int param_set_uint_minmax(const char *val
, struct kernel_param
*kp
,
2614 unsigned int min
, unsigned int max
)
2621 ret
= strict_strtoul(val
, 0, &num
);
2622 if (ret
== -EINVAL
|| num
< min
|| num
> max
)
2624 *((unsigned int *)kp
->arg
) = num
;
2628 static int param_set_portnr(const char *val
, struct kernel_param
*kp
)
2630 return param_set_uint_minmax(val
, kp
,
2635 static int param_get_portnr(char *buffer
, struct kernel_param
*kp
)
2637 return param_get_uint(buffer
, kp
);
2639 #define param_check_portnr(name, p) \
2640 __param_check(name, p, unsigned int);
2642 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
2643 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
2645 static int param_set_slot_table_size(const char *val
, struct kernel_param
*kp
)
2647 return param_set_uint_minmax(val
, kp
,
2649 RPC_MAX_SLOT_TABLE
);
2652 static int param_get_slot_table_size(char *buffer
, struct kernel_param
*kp
)
2654 return param_get_uint(buffer
, kp
);
2656 #define param_check_slot_table_size(name, p) \
2657 __param_check(name, p, unsigned int);
2659 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
2660 slot_table_size
, 0644);
2661 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
2662 slot_table_size
, 0644);