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
247 unsigned long tcp_copied
,
251 * Connection of transports
253 struct delayed_work connect_worker
;
254 struct sockaddr_storage srcaddr
;
255 unsigned short srcport
;
258 * UDP socket buffer size parameters
264 * Saved socket callback addresses
266 void (*old_data_ready
)(struct sock
*, int);
267 void (*old_state_change
)(struct sock
*);
268 void (*old_write_space
)(struct sock
*);
269 void (*old_error_report
)(struct sock
*);
273 * TCP receive state flags
275 #define TCP_RCV_LAST_FRAG (1UL << 0)
276 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
277 #define TCP_RCV_COPY_XID (1UL << 2)
278 #define TCP_RCV_COPY_DATA (1UL << 3)
279 #define TCP_RCV_READ_CALLDIR (1UL << 4)
280 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
285 #define TCP_RPC_REPLY (1UL << 6)
287 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
289 return (struct sockaddr
*) &xprt
->addr
;
292 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
294 return (struct sockaddr_in
*) &xprt
->addr
;
297 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
299 return (struct sockaddr_in6
*) &xprt
->addr
;
302 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
304 struct sockaddr
*sap
= xs_addr(xprt
);
305 struct sockaddr_in6
*sin6
;
306 struct sockaddr_in
*sin
;
309 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
310 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
312 switch (sap
->sa_family
) {
314 sin
= xs_addr_in(xprt
);
315 (void)snprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x",
316 NIPQUAD(sin
->sin_addr
.s_addr
));
319 sin6
= xs_addr_in6(xprt
);
320 (void)snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
325 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
328 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
330 struct sockaddr
*sap
= xs_addr(xprt
);
333 (void)snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
334 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
336 (void)snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
337 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
340 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
341 const char *protocol
,
344 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
345 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
346 xs_format_common_peer_addresses(xprt
);
347 xs_format_common_peer_ports(xprt
);
350 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
352 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
353 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
355 xs_format_common_peer_ports(xprt
);
358 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
362 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
364 case RPC_DISPLAY_PROTO
:
365 case RPC_DISPLAY_NETID
:
368 kfree(xprt
->address_strings
[i
]);
372 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
374 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
376 struct msghdr msg
= {
378 .msg_namelen
= addrlen
,
379 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
382 .iov_base
= vec
->iov_base
+ base
,
383 .iov_len
= vec
->iov_len
- base
,
386 if (iov
.iov_len
!= 0)
387 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
388 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
391 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
)
394 unsigned int remainder
;
397 remainder
= xdr
->page_len
- base
;
398 base
+= xdr
->page_base
;
399 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
402 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
403 int flags
= XS_SENDMSG_FLAGS
;
406 if (remainder
!= 0 || more
)
408 err
= sock
->ops
->sendpage(sock
, *ppage
, base
, len
, flags
);
409 if (remainder
== 0 || err
!= len
)
423 * xs_sendpages - write pages directly to a socket
424 * @sock: socket to send on
425 * @addr: UDP only -- address of destination
426 * @addrlen: UDP only -- length of destination address
427 * @xdr: buffer containing this request
428 * @base: starting position in the buffer
431 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
433 unsigned int remainder
= xdr
->len
- base
;
439 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
445 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
446 unsigned int len
= xdr
->head
[0].iov_len
- base
;
448 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
449 if (remainder
== 0 || err
!= len
)
454 base
-= xdr
->head
[0].iov_len
;
456 if (base
< xdr
->page_len
) {
457 unsigned int len
= xdr
->page_len
- base
;
459 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0);
460 if (remainder
== 0 || err
!= len
)
465 base
-= xdr
->page_len
;
467 if (base
>= xdr
->tail
[0].iov_len
)
469 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
478 static void xs_nospace_callback(struct rpc_task
*task
)
480 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
482 transport
->inet
->sk_write_pending
--;
483 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
487 * xs_nospace - place task on wait queue if transmit was incomplete
488 * @task: task to put to sleep
491 static int xs_nospace(struct rpc_task
*task
)
493 struct rpc_rqst
*req
= task
->tk_rqstp
;
494 struct rpc_xprt
*xprt
= req
->rq_xprt
;
495 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
498 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
499 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
502 /* Protect against races with write_space */
503 spin_lock_bh(&xprt
->transport_lock
);
505 /* Don't race with disconnect */
506 if (xprt_connected(xprt
)) {
507 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. */
570 /* Should we call xs_close() here? */
573 status
= xs_nospace(task
);
576 dprintk("RPC: sendmsg returned unrecognized error %d\n",
581 /* When the server has died, an ICMP port unreachable message
582 * prompts ECONNREFUSED. */
583 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
590 * xs_tcp_shutdown - gracefully shut down a TCP socket
593 * Initiates a graceful shutdown of the TCP socket by calling the
594 * equivalent of shutdown(SHUT_WR);
596 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
598 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
599 struct socket
*sock
= transport
->sock
;
602 kernel_sock_shutdown(sock
, SHUT_WR
);
605 static inline void xs_encode_tcp_record_marker(struct xdr_buf
*buf
)
607 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
608 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
609 *base
= htonl(RPC_LAST_STREAM_FRAGMENT
| reclen
);
613 * xs_tcp_send_request - write an RPC request to a TCP socket
614 * @task: address of RPC task that manages the state of an RPC request
617 * 0: The request has been sent
618 * EAGAIN: The socket was blocked, please call again later to
619 * complete the request
620 * ENOTCONN: Caller needs to invoke connect logic then call again
621 * other: Some other error occured, the request was not sent
623 * XXX: In the case of soft timeouts, should we eventually give up
624 * if sendmsg is not able to make progress?
626 static int xs_tcp_send_request(struct rpc_task
*task
)
628 struct rpc_rqst
*req
= task
->tk_rqstp
;
629 struct rpc_xprt
*xprt
= req
->rq_xprt
;
630 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
631 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
634 xs_encode_tcp_record_marker(&req
->rq_snd_buf
);
636 xs_pktdump("packet data:",
637 req
->rq_svec
->iov_base
,
638 req
->rq_svec
->iov_len
);
640 /* Continue transmitting the packet/record. We must be careful
641 * to cope with writespace callbacks arriving _after_ we have
642 * called sendmsg(). */
644 status
= xs_sendpages(transport
->sock
,
645 NULL
, 0, xdr
, req
->rq_bytes_sent
);
647 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
648 xdr
->len
- req
->rq_bytes_sent
, status
);
650 if (unlikely(status
< 0))
653 /* If we've sent the entire packet, immediately
654 * reset the count of bytes sent. */
655 req
->rq_bytes_sent
+= status
;
656 task
->tk_bytes_sent
+= status
;
657 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
658 req
->rq_bytes_sent
= 0;
671 /* Should we call xs_close() here? */
674 status
= xs_nospace(task
);
677 dprintk("RPC: sendmsg returned unrecognized error %d\n",
681 xs_tcp_shutdown(xprt
);
684 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
691 * xs_tcp_release_xprt - clean up after a tcp transmission
695 * This cleans up if an error causes us to abort the transmission of a request.
696 * In this case, the socket may need to be reset in order to avoid confusing
699 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
701 struct rpc_rqst
*req
;
703 if (task
!= xprt
->snd_task
)
707 req
= task
->tk_rqstp
;
708 if (req
->rq_bytes_sent
== 0)
710 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
712 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
714 xprt_release_xprt(xprt
, task
);
717 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
719 transport
->old_data_ready
= sk
->sk_data_ready
;
720 transport
->old_state_change
= sk
->sk_state_change
;
721 transport
->old_write_space
= sk
->sk_write_space
;
722 transport
->old_error_report
= sk
->sk_error_report
;
725 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
727 sk
->sk_data_ready
= transport
->old_data_ready
;
728 sk
->sk_state_change
= transport
->old_state_change
;
729 sk
->sk_write_space
= transport
->old_write_space
;
730 sk
->sk_error_report
= transport
->old_error_report
;
733 static void xs_reset_transport(struct sock_xprt
*transport
)
735 struct socket
*sock
= transport
->sock
;
736 struct sock
*sk
= transport
->inet
;
741 transport
->srcport
= 0;
743 write_lock_bh(&sk
->sk_callback_lock
);
744 transport
->inet
= NULL
;
745 transport
->sock
= NULL
;
747 sk
->sk_user_data
= NULL
;
749 xs_restore_old_callbacks(transport
, sk
);
750 write_unlock_bh(&sk
->sk_callback_lock
);
758 * xs_close - close a socket
761 * This is used when all requests are complete; ie, no DRC state remains
762 * on the server we want to save.
764 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
765 * xs_reset_transport() zeroing the socket from underneath a writer.
767 static void xs_close(struct rpc_xprt
*xprt
)
769 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
771 dprintk("RPC: xs_close xprt %p\n", xprt
);
773 xs_reset_transport(transport
);
774 xprt
->reestablish_timeout
= 0;
776 smp_mb__before_clear_bit();
777 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
778 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
779 clear_bit(XPRT_CLOSING
, &xprt
->state
);
780 smp_mb__after_clear_bit();
781 xprt_disconnect_done(xprt
);
784 static void xs_tcp_close(struct rpc_xprt
*xprt
)
786 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
789 xs_tcp_shutdown(xprt
);
793 * xs_destroy - prepare to shutdown a transport
794 * @xprt: doomed transport
797 static void xs_destroy(struct rpc_xprt
*xprt
)
799 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
801 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
803 cancel_rearming_delayed_work(&transport
->connect_worker
);
806 xs_free_peer_addresses(xprt
);
809 module_put(THIS_MODULE
);
812 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
814 return (struct rpc_xprt
*) sk
->sk_user_data
;
818 * xs_udp_data_ready - "data ready" callback for UDP sockets
819 * @sk: socket with data to read
820 * @len: how much data to read
823 static void xs_udp_data_ready(struct sock
*sk
, int len
)
825 struct rpc_task
*task
;
826 struct rpc_xprt
*xprt
;
827 struct rpc_rqst
*rovr
;
829 int err
, repsize
, copied
;
833 read_lock(&sk
->sk_callback_lock
);
834 dprintk("RPC: xs_udp_data_ready...\n");
835 if (!(xprt
= xprt_from_sock(sk
)))
838 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
844 repsize
= skb
->len
- sizeof(struct udphdr
);
846 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
850 /* Copy the XID from the skb... */
851 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
852 sizeof(_xid
), &_xid
);
856 /* Look up and lock the request corresponding to the given XID */
857 spin_lock(&xprt
->transport_lock
);
858 rovr
= xprt_lookup_rqst(xprt
, *xp
);
861 task
= rovr
->rq_task
;
863 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
866 /* Suck it into the iovec, verify checksum if not done by hw. */
867 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
868 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
872 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
874 /* Something worked... */
875 dst_confirm(skb_dst(skb
));
877 xprt_adjust_cwnd(task
, copied
);
878 xprt_update_rtt(task
);
879 xprt_complete_rqst(task
, copied
);
882 spin_unlock(&xprt
->transport_lock
);
884 skb_free_datagram(sk
, skb
);
886 read_unlock(&sk
->sk_callback_lock
);
889 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
891 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
895 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
896 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
897 used
= xdr_skb_read_bits(desc
, p
, len
);
898 transport
->tcp_offset
+= used
;
902 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
903 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
904 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
906 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
907 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
909 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
910 transport
->tcp_offset
= 0;
912 /* Sanity check of the record length */
913 if (unlikely(transport
->tcp_reclen
< 8)) {
914 dprintk("RPC: invalid TCP record fragment length\n");
915 xprt_force_disconnect(xprt
);
918 dprintk("RPC: reading TCP record fragment of length %d\n",
919 transport
->tcp_reclen
);
922 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
924 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
925 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
926 transport
->tcp_offset
= 0;
927 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
928 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
929 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
930 transport
->tcp_copied
= 0;
935 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
940 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
941 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
942 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
943 used
= xdr_skb_read_bits(desc
, p
, len
);
944 transport
->tcp_offset
+= used
;
947 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
948 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
949 transport
->tcp_copied
= 4;
950 dprintk("RPC: reading %s XID %08x\n",
951 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
953 ntohl(transport
->tcp_xid
));
954 xs_tcp_check_fraghdr(transport
);
957 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
958 struct xdr_skb_reader
*desc
)
965 * We want transport->tcp_offset to be 8 at the end of this routine
966 * (4 bytes for the xid and 4 bytes for the call/reply flag).
967 * When this function is called for the first time,
968 * transport->tcp_offset is 4 (after having already read the xid).
970 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
971 len
= sizeof(transport
->tcp_calldir
) - offset
;
972 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
973 p
= ((char *) &transport
->tcp_calldir
) + offset
;
974 used
= xdr_skb_read_bits(desc
, p
, len
);
975 transport
->tcp_offset
+= used
;
978 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
980 * We don't yet have the XDR buffer, so we will write the calldir
981 * out after we get the buffer from the 'struct rpc_rqst'
983 switch (ntohl(transport
->tcp_calldir
)) {
985 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
986 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
987 transport
->tcp_flags
|= TCP_RPC_REPLY
;
990 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
991 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
992 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
995 dprintk("RPC: invalid request message type\n");
996 xprt_force_disconnect(&transport
->xprt
);
998 xs_tcp_check_fraghdr(transport
);
1001 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
1002 struct xdr_skb_reader
*desc
,
1003 struct rpc_rqst
*req
)
1005 struct sock_xprt
*transport
=
1006 container_of(xprt
, struct sock_xprt
, xprt
);
1007 struct xdr_buf
*rcvbuf
;
1011 rcvbuf
= &req
->rq_private_buf
;
1013 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1015 * Save the RPC direction in the XDR buffer
1017 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1018 &transport
->tcp_calldir
,
1019 sizeof(transport
->tcp_calldir
));
1020 transport
->tcp_copied
+= sizeof(transport
->tcp_calldir
);
1021 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1025 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1026 struct xdr_skb_reader my_desc
;
1028 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1029 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1030 my_desc
.count
= len
;
1031 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1032 &my_desc
, xdr_skb_read_bits
);
1036 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1037 desc
, xdr_skb_read_bits
);
1040 transport
->tcp_copied
+= r
;
1041 transport
->tcp_offset
+= r
;
1044 /* Error when copying to the receive buffer,
1045 * usually because we weren't able to allocate
1046 * additional buffer pages. All we can do now
1047 * is turn off TCP_RCV_COPY_DATA, so the request
1048 * will not receive any additional updates,
1050 * Any remaining data from this record will
1053 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1054 dprintk("RPC: XID %08x truncated request\n",
1055 ntohl(transport
->tcp_xid
));
1056 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1057 "tcp_offset = %u, tcp_reclen = %u\n",
1058 xprt
, transport
->tcp_copied
,
1059 transport
->tcp_offset
, transport
->tcp_reclen
);
1063 dprintk("RPC: XID %08x read %Zd bytes\n",
1064 ntohl(transport
->tcp_xid
), r
);
1065 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1066 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1067 transport
->tcp_offset
, transport
->tcp_reclen
);
1069 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1070 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1071 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1072 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1073 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1080 * Finds the request corresponding to the RPC xid and invokes the common
1081 * tcp read code to read the data.
1083 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1084 struct xdr_skb_reader
*desc
)
1086 struct sock_xprt
*transport
=
1087 container_of(xprt
, struct sock_xprt
, xprt
);
1088 struct rpc_rqst
*req
;
1090 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1092 /* Find and lock the request corresponding to this xid */
1093 spin_lock(&xprt
->transport_lock
);
1094 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1096 dprintk("RPC: XID %08x request not found!\n",
1097 ntohl(transport
->tcp_xid
));
1098 spin_unlock(&xprt
->transport_lock
);
1102 xs_tcp_read_common(xprt
, desc
, req
);
1104 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1105 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1107 spin_unlock(&xprt
->transport_lock
);
1111 #if defined(CONFIG_NFS_V4_1)
1113 * Obtains an rpc_rqst previously allocated and invokes the common
1114 * tcp read code to read the data. The result is placed in the callback
1116 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1117 * connection and return -1.
1119 static inline int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1120 struct xdr_skb_reader
*desc
)
1122 struct sock_xprt
*transport
=
1123 container_of(xprt
, struct sock_xprt
, xprt
);
1124 struct rpc_rqst
*req
;
1126 req
= xprt_alloc_bc_request(xprt
);
1128 printk(KERN_WARNING
"Callback slot table overflowed\n");
1129 xprt_force_disconnect(xprt
);
1133 req
->rq_xid
= transport
->tcp_xid
;
1134 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1135 xs_tcp_read_common(xprt
, desc
, req
);
1137 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
)) {
1138 struct svc_serv
*bc_serv
= xprt
->bc_serv
;
1141 * Add callback request to callback list. The callback
1142 * service sleeps on the sv_cb_waitq waiting for new
1143 * requests. Wake it up after adding enqueing the
1146 dprintk("RPC: add callback request to list\n");
1147 spin_lock(&bc_serv
->sv_cb_lock
);
1148 list_add(&req
->rq_bc_list
, &bc_serv
->sv_cb_list
);
1149 spin_unlock(&bc_serv
->sv_cb_lock
);
1150 wake_up(&bc_serv
->sv_cb_waitq
);
1153 req
->rq_private_buf
.len
= transport
->tcp_copied
;
1158 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1159 struct xdr_skb_reader
*desc
)
1161 struct sock_xprt
*transport
=
1162 container_of(xprt
, struct sock_xprt
, xprt
);
1164 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1165 xs_tcp_read_reply(xprt
, desc
) :
1166 xs_tcp_read_callback(xprt
, desc
);
1169 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1170 struct xdr_skb_reader
*desc
)
1172 return xs_tcp_read_reply(xprt
, desc
);
1174 #endif /* CONFIG_NFS_V4_1 */
1177 * Read data off the transport. This can be either an RPC_CALL or an
1178 * RPC_REPLY. Relay the processing to helper functions.
1180 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1181 struct xdr_skb_reader
*desc
)
1183 struct sock_xprt
*transport
=
1184 container_of(xprt
, struct sock_xprt
, xprt
);
1186 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1187 xs_tcp_check_fraghdr(transport
);
1190 * The transport_lock protects the request handling.
1191 * There's no need to hold it to update the tcp_flags.
1193 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1197 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1201 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1202 if (len
> desc
->count
)
1205 desc
->offset
+= len
;
1206 transport
->tcp_offset
+= len
;
1207 dprintk("RPC: discarded %Zu bytes\n", len
);
1208 xs_tcp_check_fraghdr(transport
);
1211 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1213 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1214 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1215 struct xdr_skb_reader desc
= {
1221 dprintk("RPC: xs_tcp_data_recv started\n");
1223 /* Read in a new fragment marker if necessary */
1224 /* Can we ever really expect to get completely empty fragments? */
1225 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1226 xs_tcp_read_fraghdr(xprt
, &desc
);
1229 /* Read in the xid if necessary */
1230 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1231 xs_tcp_read_xid(transport
, &desc
);
1234 /* Read in the call/reply flag */
1235 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1236 xs_tcp_read_calldir(transport
, &desc
);
1239 /* Read in the request data */
1240 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1241 xs_tcp_read_data(xprt
, &desc
);
1244 /* Skip over any trailing bytes on short reads */
1245 xs_tcp_read_discard(transport
, &desc
);
1246 } while (desc
.count
);
1247 dprintk("RPC: xs_tcp_data_recv done\n");
1248 return len
- desc
.count
;
1252 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1253 * @sk: socket with data to read
1254 * @bytes: how much data to read
1257 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1259 struct rpc_xprt
*xprt
;
1260 read_descriptor_t rd_desc
;
1263 dprintk("RPC: xs_tcp_data_ready...\n");
1265 read_lock(&sk
->sk_callback_lock
);
1266 if (!(xprt
= xprt_from_sock(sk
)))
1271 /* Any data means we had a useful conversation, so
1272 * the we don't need to delay the next reconnect
1274 if (xprt
->reestablish_timeout
)
1275 xprt
->reestablish_timeout
= 0;
1277 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1278 rd_desc
.arg
.data
= xprt
;
1280 rd_desc
.count
= 65536;
1281 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1284 read_unlock(&sk
->sk_callback_lock
);
1288 * Do the equivalent of linger/linger2 handling for dealing with
1289 * broken servers that don't close the socket in a timely
1292 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1293 unsigned long timeout
)
1295 struct sock_xprt
*transport
;
1297 if (xprt_test_and_set_connecting(xprt
))
1299 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1300 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1301 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1305 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1307 struct sock_xprt
*transport
;
1309 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1311 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1312 !cancel_delayed_work(&transport
->connect_worker
))
1314 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1315 xprt_clear_connecting(xprt
);
1318 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1320 smp_mb__before_clear_bit();
1321 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1322 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1323 smp_mb__after_clear_bit();
1324 /* Mark transport as closed and wake up all pending tasks */
1325 xprt_disconnect_done(xprt
);
1329 * xs_tcp_state_change - callback to handle TCP socket state changes
1330 * @sk: socket whose state has changed
1333 static void xs_tcp_state_change(struct sock
*sk
)
1335 struct rpc_xprt
*xprt
;
1337 read_lock(&sk
->sk_callback_lock
);
1338 if (!(xprt
= xprt_from_sock(sk
)))
1340 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1341 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1342 sk
->sk_state
, xprt_connected(xprt
),
1343 sock_flag(sk
, SOCK_DEAD
),
1344 sock_flag(sk
, SOCK_ZAPPED
),
1347 switch (sk
->sk_state
) {
1348 case TCP_ESTABLISHED
:
1349 spin_lock_bh(&xprt
->transport_lock
);
1350 if (!xprt_test_and_set_connected(xprt
)) {
1351 struct sock_xprt
*transport
= container_of(xprt
,
1352 struct sock_xprt
, xprt
);
1354 /* Reset TCP record info */
1355 transport
->tcp_offset
= 0;
1356 transport
->tcp_reclen
= 0;
1357 transport
->tcp_copied
= 0;
1358 transport
->tcp_flags
=
1359 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1361 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1363 spin_unlock_bh(&xprt
->transport_lock
);
1366 /* The client initiated a shutdown of the socket */
1367 xprt
->connect_cookie
++;
1368 xprt
->reestablish_timeout
= 0;
1369 set_bit(XPRT_CLOSING
, &xprt
->state
);
1370 smp_mb__before_clear_bit();
1371 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1372 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1373 smp_mb__after_clear_bit();
1374 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1376 case TCP_CLOSE_WAIT
:
1377 /* The server initiated a shutdown of the socket */
1378 xprt_force_disconnect(xprt
);
1379 xprt
->connect_cookie
++;
1382 * If the server closed down the connection, make sure that
1383 * we back off before reconnecting
1385 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1386 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1389 set_bit(XPRT_CLOSING
, &xprt
->state
);
1390 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1391 smp_mb__before_clear_bit();
1392 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1393 smp_mb__after_clear_bit();
1396 xs_tcp_cancel_linger_timeout(xprt
);
1397 xs_sock_mark_closed(xprt
);
1400 read_unlock(&sk
->sk_callback_lock
);
1404 * xs_error_report - callback mainly for catching socket errors
1407 static void xs_error_report(struct sock
*sk
)
1409 struct rpc_xprt
*xprt
;
1411 read_lock(&sk
->sk_callback_lock
);
1412 if (!(xprt
= xprt_from_sock(sk
)))
1414 dprintk("RPC: %s client %p...\n"
1416 __func__
, xprt
, sk
->sk_err
);
1417 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1419 read_unlock(&sk
->sk_callback_lock
);
1422 static void xs_write_space(struct sock
*sk
)
1424 struct socket
*sock
;
1425 struct rpc_xprt
*xprt
;
1427 if (unlikely(!(sock
= sk
->sk_socket
)))
1429 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1431 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1433 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1436 xprt_write_space(xprt
);
1440 * xs_udp_write_space - callback invoked when socket buffer space
1442 * @sk: socket whose state has changed
1444 * Called when more output buffer space is available for this socket.
1445 * We try not to wake our writers until they can make "significant"
1446 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1447 * with a bunch of small requests.
1449 static void xs_udp_write_space(struct sock
*sk
)
1451 read_lock(&sk
->sk_callback_lock
);
1453 /* from net/core/sock.c:sock_def_write_space */
1454 if (sock_writeable(sk
))
1457 read_unlock(&sk
->sk_callback_lock
);
1461 * xs_tcp_write_space - callback invoked when socket buffer space
1463 * @sk: socket whose state has changed
1465 * Called when more output buffer space is available for this socket.
1466 * We try not to wake our writers until they can make "significant"
1467 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1468 * with a bunch of small requests.
1470 static void xs_tcp_write_space(struct sock
*sk
)
1472 read_lock(&sk
->sk_callback_lock
);
1474 /* from net/core/stream.c:sk_stream_write_space */
1475 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
))
1478 read_unlock(&sk
->sk_callback_lock
);
1481 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1483 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1484 struct sock
*sk
= transport
->inet
;
1486 if (transport
->rcvsize
) {
1487 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1488 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1490 if (transport
->sndsize
) {
1491 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1492 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1493 sk
->sk_write_space(sk
);
1498 * xs_udp_set_buffer_size - set send and receive limits
1499 * @xprt: generic transport
1500 * @sndsize: requested size of send buffer, in bytes
1501 * @rcvsize: requested size of receive buffer, in bytes
1503 * Set socket send and receive buffer size limits.
1505 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1507 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1509 transport
->sndsize
= 0;
1511 transport
->sndsize
= sndsize
+ 1024;
1512 transport
->rcvsize
= 0;
1514 transport
->rcvsize
= rcvsize
+ 1024;
1516 xs_udp_do_set_buffer_size(xprt
);
1520 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1521 * @task: task that timed out
1523 * Adjust the congestion window after a retransmit timeout has occurred.
1525 static void xs_udp_timer(struct rpc_task
*task
)
1527 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
1530 static unsigned short xs_get_random_port(void)
1532 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1533 unsigned short rand
= (unsigned short) net_random() % range
;
1534 return rand
+ xprt_min_resvport
;
1538 * xs_set_port - reset the port number in the remote endpoint address
1539 * @xprt: generic transport
1540 * @port: new port number
1543 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1545 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1547 rpc_set_port(xs_addr(xprt
), port
);
1548 xs_update_peer_port(xprt
);
1551 static unsigned short xs_get_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1553 unsigned short port
= transport
->srcport
;
1555 if (port
== 0 && transport
->xprt
.resvport
)
1556 port
= xs_get_random_port();
1560 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, struct socket
*sock
, unsigned short port
)
1562 if (transport
->srcport
!= 0)
1563 transport
->srcport
= 0;
1564 if (!transport
->xprt
.resvport
)
1566 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1567 return xprt_max_resvport
;
1571 static int xs_bind4(struct sock_xprt
*transport
, struct socket
*sock
)
1573 struct sockaddr_in myaddr
= {
1574 .sin_family
= AF_INET
,
1576 struct sockaddr_in
*sa
;
1578 unsigned short port
= xs_get_srcport(transport
, sock
);
1579 unsigned short last
;
1581 sa
= (struct sockaddr_in
*)&transport
->srcaddr
;
1582 myaddr
.sin_addr
= sa
->sin_addr
;
1584 myaddr
.sin_port
= htons(port
);
1585 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1590 transport
->srcport
= port
;
1594 port
= xs_next_srcport(transport
, sock
, port
);
1597 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1598 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1599 __func__
, &myaddr
.sin_addr
,
1600 port
, err
? "failed" : "ok", err
);
1604 static int xs_bind6(struct sock_xprt
*transport
, struct socket
*sock
)
1606 struct sockaddr_in6 myaddr
= {
1607 .sin6_family
= AF_INET6
,
1609 struct sockaddr_in6
*sa
;
1611 unsigned short port
= xs_get_srcport(transport
, sock
);
1612 unsigned short last
;
1614 sa
= (struct sockaddr_in6
*)&transport
->srcaddr
;
1615 myaddr
.sin6_addr
= sa
->sin6_addr
;
1617 myaddr
.sin6_port
= htons(port
);
1618 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1623 transport
->srcport
= port
;
1627 port
= xs_next_srcport(transport
, sock
, port
);
1630 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1631 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1632 &myaddr
.sin6_addr
, port
, err
? "failed" : "ok", err
);
1636 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1637 static struct lock_class_key xs_key
[2];
1638 static struct lock_class_key xs_slock_key
[2];
1640 static inline void xs_reclassify_socket4(struct socket
*sock
)
1642 struct sock
*sk
= sock
->sk
;
1644 BUG_ON(sock_owned_by_user(sk
));
1645 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1646 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1649 static inline void xs_reclassify_socket6(struct socket
*sock
)
1651 struct sock
*sk
= sock
->sk
;
1653 BUG_ON(sock_owned_by_user(sk
));
1654 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1655 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1658 static inline void xs_reclassify_socket4(struct socket
*sock
)
1662 static inline void xs_reclassify_socket6(struct socket
*sock
)
1667 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1669 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1671 if (!transport
->inet
) {
1672 struct sock
*sk
= sock
->sk
;
1674 write_lock_bh(&sk
->sk_callback_lock
);
1676 xs_save_old_callbacks(transport
, sk
);
1678 sk
->sk_user_data
= xprt
;
1679 sk
->sk_data_ready
= xs_udp_data_ready
;
1680 sk
->sk_write_space
= xs_udp_write_space
;
1681 sk
->sk_error_report
= xs_error_report
;
1682 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1683 sk
->sk_allocation
= GFP_ATOMIC
;
1685 xprt_set_connected(xprt
);
1687 /* Reset to new socket */
1688 transport
->sock
= sock
;
1689 transport
->inet
= sk
;
1691 write_unlock_bh(&sk
->sk_callback_lock
);
1693 xs_udp_do_set_buffer_size(xprt
);
1697 * xs_udp_connect_worker4 - set up a UDP socket
1698 * @work: RPC transport to connect
1700 * Invoked by a work queue tasklet.
1702 static void xs_udp_connect_worker4(struct work_struct
*work
)
1704 struct sock_xprt
*transport
=
1705 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1706 struct rpc_xprt
*xprt
= &transport
->xprt
;
1707 struct socket
*sock
= transport
->sock
;
1708 int err
, status
= -EIO
;
1713 /* Start by resetting any existing state */
1714 xs_reset_transport(transport
);
1716 err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1718 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1721 xs_reclassify_socket4(sock
);
1723 if (xs_bind4(transport
, sock
)) {
1728 dprintk("RPC: worker connecting xprt %p via %s to "
1729 "%s (port %s)\n", xprt
,
1730 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1731 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1732 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1734 xs_udp_finish_connecting(xprt
, sock
);
1737 xprt_clear_connecting(xprt
);
1738 xprt_wake_pending_tasks(xprt
, status
);
1742 * xs_udp_connect_worker6 - set up a UDP socket
1743 * @work: RPC transport to connect
1745 * Invoked by a work queue tasklet.
1747 static void xs_udp_connect_worker6(struct work_struct
*work
)
1749 struct sock_xprt
*transport
=
1750 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1751 struct rpc_xprt
*xprt
= &transport
->xprt
;
1752 struct socket
*sock
= transport
->sock
;
1753 int err
, status
= -EIO
;
1758 /* Start by resetting any existing state */
1759 xs_reset_transport(transport
);
1761 err
= sock_create_kern(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1763 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1766 xs_reclassify_socket6(sock
);
1768 if (xs_bind6(transport
, sock
) < 0) {
1773 dprintk("RPC: worker connecting xprt %p via %s to "
1774 "%s (port %s)\n", xprt
,
1775 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1776 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1777 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1779 xs_udp_finish_connecting(xprt
, sock
);
1782 xprt_clear_connecting(xprt
);
1783 xprt_wake_pending_tasks(xprt
, status
);
1787 * We need to preserve the port number so the reply cache on the server can
1788 * find our cached RPC replies when we get around to reconnecting.
1790 static void xs_abort_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1793 struct sockaddr any
;
1795 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1798 * Disconnect the transport socket by doing a connect operation
1799 * with AF_UNSPEC. This should return immediately...
1801 memset(&any
, 0, sizeof(any
));
1802 any
.sa_family
= AF_UNSPEC
;
1803 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
1805 xs_sock_mark_closed(xprt
);
1807 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1811 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1813 unsigned int state
= transport
->inet
->sk_state
;
1815 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
) {
1816 /* we don't need to abort the connection if the socket
1817 * hasn't undergone a shutdown
1819 if (transport
->inet
->sk_shutdown
== 0)
1821 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
1822 __func__
, transport
->inet
->sk_shutdown
);
1824 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
)) {
1825 /* we don't need to abort the connection if the socket
1826 * hasn't undergone a shutdown
1828 if (transport
->inet
->sk_shutdown
== 0)
1830 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
1831 "sk_shutdown set to %d\n",
1832 __func__
, transport
->inet
->sk_shutdown
);
1834 xs_abort_connection(xprt
, transport
);
1837 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1839 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1840 int ret
= -ENOTCONN
;
1842 if (!transport
->inet
) {
1843 struct sock
*sk
= sock
->sk
;
1845 write_lock_bh(&sk
->sk_callback_lock
);
1847 xs_save_old_callbacks(transport
, sk
);
1849 sk
->sk_user_data
= xprt
;
1850 sk
->sk_data_ready
= xs_tcp_data_ready
;
1851 sk
->sk_state_change
= xs_tcp_state_change
;
1852 sk
->sk_write_space
= xs_tcp_write_space
;
1853 sk
->sk_error_report
= xs_error_report
;
1854 sk
->sk_allocation
= GFP_ATOMIC
;
1856 /* socket options */
1857 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1858 sock_reset_flag(sk
, SOCK_LINGER
);
1859 tcp_sk(sk
)->linger2
= 0;
1860 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1862 xprt_clear_connected(xprt
);
1864 /* Reset to new socket */
1865 transport
->sock
= sock
;
1866 transport
->inet
= sk
;
1868 write_unlock_bh(&sk
->sk_callback_lock
);
1871 if (!xprt_bound(xprt
))
1874 /* Tell the socket layer to start connecting... */
1875 xprt
->stat
.connect_count
++;
1876 xprt
->stat
.connect_start
= jiffies
;
1877 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
1882 xprt
->connect_cookie
++;
1883 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1884 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1891 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1892 * @xprt: RPC transport to connect
1893 * @transport: socket transport to connect
1894 * @create_sock: function to create a socket of the correct type
1896 * Invoked by a work queue tasklet.
1898 static void xs_tcp_setup_socket(struct rpc_xprt
*xprt
,
1899 struct sock_xprt
*transport
,
1900 struct socket
*(*create_sock
)(struct rpc_xprt
*,
1901 struct sock_xprt
*))
1903 struct socket
*sock
= transport
->sock
;
1910 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1911 sock
= create_sock(xprt
, transport
);
1913 status
= PTR_ERR(sock
);
1919 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
1921 /* "close" the socket, preserving the local port */
1922 xs_tcp_reuse_connection(xprt
, transport
);
1928 dprintk("RPC: worker connecting xprt %p via %s to "
1929 "%s (port %s)\n", xprt
,
1930 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1931 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1932 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1934 status
= xs_tcp_finish_connecting(xprt
, sock
);
1935 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1936 xprt
, -status
, xprt_connected(xprt
),
1937 sock
->sk
->sk_state
);
1940 printk("%s: connect returned unhandled error %d\n",
1942 case -EADDRNOTAVAIL
:
1943 /* We're probably in TIME_WAIT. Get rid of existing socket,
1946 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1947 xprt_force_disconnect(xprt
);
1952 /* retry with existing socket, after a delay */
1956 xprt_clear_connecting(xprt
);
1959 /* Happens, for instance, if the user specified a link
1960 * local IPv6 address without a scope-id.
1967 xprt_clear_connecting(xprt
);
1968 xprt_wake_pending_tasks(xprt
, status
);
1971 static struct socket
*xs_create_tcp_sock4(struct rpc_xprt
*xprt
,
1972 struct sock_xprt
*transport
)
1974 struct socket
*sock
;
1977 /* start from scratch */
1978 err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1980 dprintk("RPC: can't create TCP transport socket (%d).\n",
1984 xs_reclassify_socket4(sock
);
1986 if (xs_bind4(transport
, sock
) < 0) {
1992 return ERR_PTR(-EIO
);
1996 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1997 * @work: RPC transport to connect
1999 * Invoked by a work queue tasklet.
2001 static void xs_tcp_connect_worker4(struct work_struct
*work
)
2003 struct sock_xprt
*transport
=
2004 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2005 struct rpc_xprt
*xprt
= &transport
->xprt
;
2007 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock4
);
2010 static struct socket
*xs_create_tcp_sock6(struct rpc_xprt
*xprt
,
2011 struct sock_xprt
*transport
)
2013 struct socket
*sock
;
2016 /* start from scratch */
2017 err
= sock_create_kern(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
2019 dprintk("RPC: can't create TCP transport socket (%d).\n",
2023 xs_reclassify_socket6(sock
);
2025 if (xs_bind6(transport
, sock
) < 0) {
2031 return ERR_PTR(-EIO
);
2035 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
2036 * @work: RPC transport to connect
2038 * Invoked by a work queue tasklet.
2040 static void xs_tcp_connect_worker6(struct work_struct
*work
)
2042 struct sock_xprt
*transport
=
2043 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2044 struct rpc_xprt
*xprt
= &transport
->xprt
;
2046 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock6
);
2050 * xs_connect - connect a socket to a remote endpoint
2051 * @task: address of RPC task that manages state of connect request
2053 * TCP: If the remote end dropped the connection, delay reconnecting.
2055 * UDP socket connects are synchronous, but we use a work queue anyway
2056 * to guarantee that even unprivileged user processes can set up a
2057 * socket on a privileged port.
2059 * If a UDP socket connect fails, the delay behavior here prevents
2060 * retry floods (hard mounts).
2062 static void xs_connect(struct rpc_task
*task
)
2064 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2065 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2067 if (xprt_test_and_set_connecting(xprt
))
2070 if (transport
->sock
!= NULL
) {
2071 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2073 xprt
, xprt
->reestablish_timeout
/ HZ
);
2074 queue_delayed_work(rpciod_workqueue
,
2075 &transport
->connect_worker
,
2076 xprt
->reestablish_timeout
);
2077 xprt
->reestablish_timeout
<<= 1;
2078 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2079 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2080 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2081 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2083 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2084 queue_delayed_work(rpciod_workqueue
,
2085 &transport
->connect_worker
, 0);
2089 static void xs_tcp_connect(struct rpc_task
*task
)
2091 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2093 /* Exit if we need to wait for socket shutdown to complete */
2094 if (test_bit(XPRT_CLOSING
, &xprt
->state
))
2100 * xs_udp_print_stats - display UDP socket-specifc stats
2101 * @xprt: rpc_xprt struct containing statistics
2105 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2107 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2109 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2111 xprt
->stat
.bind_count
,
2114 xprt
->stat
.bad_xids
,
2116 xprt
->stat
.bklog_u
);
2120 * xs_tcp_print_stats - display TCP socket-specifc stats
2121 * @xprt: rpc_xprt struct containing statistics
2125 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2127 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2130 if (xprt_connected(xprt
))
2131 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2133 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2135 xprt
->stat
.bind_count
,
2136 xprt
->stat
.connect_count
,
2137 xprt
->stat
.connect_time
,
2141 xprt
->stat
.bad_xids
,
2143 xprt
->stat
.bklog_u
);
2147 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2148 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2149 * to use the server side send routines.
2151 void *bc_malloc(struct rpc_task
*task
, size_t size
)
2154 struct rpc_buffer
*buf
;
2156 BUG_ON(size
> PAGE_SIZE
- sizeof(struct rpc_buffer
));
2157 page
= alloc_page(GFP_KERNEL
);
2162 buf
= page_address(page
);
2163 buf
->len
= PAGE_SIZE
;
2169 * Free the space allocated in the bc_alloc routine
2171 void bc_free(void *buffer
)
2173 struct rpc_buffer
*buf
;
2178 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2179 free_page((unsigned long)buf
);
2183 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2184 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2186 static int bc_sendto(struct rpc_rqst
*req
)
2189 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2190 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2191 struct sock_xprt
*transport
=
2192 container_of(xprt
, struct sock_xprt
, xprt
);
2193 struct socket
*sock
= transport
->sock
;
2194 unsigned long headoff
;
2195 unsigned long tailoff
;
2198 * Set up the rpc header and record marker stuff
2200 xs_encode_tcp_record_marker(xbufp
);
2202 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2203 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2204 len
= svc_send_common(sock
, xbufp
,
2205 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2206 xbufp
->tail
[0].iov_base
, tailoff
);
2208 if (len
!= xbufp
->len
) {
2209 printk(KERN_NOTICE
"Error sending entire callback!\n");
2217 * The send routine. Borrows from svc_send
2219 static int bc_send_request(struct rpc_task
*task
)
2221 struct rpc_rqst
*req
= task
->tk_rqstp
;
2222 struct svc_xprt
*xprt
;
2223 struct svc_sock
*svsk
;
2226 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2228 * Get the server socket associated with this callback xprt
2230 xprt
= req
->rq_xprt
->bc_xprt
;
2231 svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
2234 * Grab the mutex to serialize data as the connection is shared
2235 * with the fore channel
2237 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2238 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2239 if (!mutex_trylock(&xprt
->xpt_mutex
))
2241 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2243 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2246 len
= bc_sendto(req
);
2247 mutex_unlock(&xprt
->xpt_mutex
);
2256 * The close routine. Since this is client initiated, we do nothing
2259 static void bc_close(struct rpc_xprt
*xprt
)
2265 * The xprt destroy routine. Again, because this connection is client
2266 * initiated, we do nothing
2269 static void bc_destroy(struct rpc_xprt
*xprt
)
2274 static struct rpc_xprt_ops xs_udp_ops
= {
2275 .set_buffer_size
= xs_udp_set_buffer_size
,
2276 .reserve_xprt
= xprt_reserve_xprt_cong
,
2277 .release_xprt
= xprt_release_xprt_cong
,
2278 .rpcbind
= rpcb_getport_async
,
2279 .set_port
= xs_set_port
,
2280 .connect
= xs_connect
,
2281 .buf_alloc
= rpc_malloc
,
2282 .buf_free
= rpc_free
,
2283 .send_request
= xs_udp_send_request
,
2284 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2285 .timer
= xs_udp_timer
,
2286 .release_request
= xprt_release_rqst_cong
,
2288 .destroy
= xs_destroy
,
2289 .print_stats
= xs_udp_print_stats
,
2292 static struct rpc_xprt_ops xs_tcp_ops
= {
2293 .reserve_xprt
= xprt_reserve_xprt
,
2294 .release_xprt
= xs_tcp_release_xprt
,
2295 .rpcbind
= rpcb_getport_async
,
2296 .set_port
= xs_set_port
,
2297 .connect
= xs_tcp_connect
,
2298 .buf_alloc
= rpc_malloc
,
2299 .buf_free
= rpc_free
,
2300 .send_request
= xs_tcp_send_request
,
2301 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2302 #if defined(CONFIG_NFS_V4_1)
2303 .release_request
= bc_release_request
,
2304 #endif /* CONFIG_NFS_V4_1 */
2305 .close
= xs_tcp_close
,
2306 .destroy
= xs_destroy
,
2307 .print_stats
= xs_tcp_print_stats
,
2311 * The rpc_xprt_ops for the server backchannel
2314 static struct rpc_xprt_ops bc_tcp_ops
= {
2315 .reserve_xprt
= xprt_reserve_xprt
,
2316 .release_xprt
= xprt_release_xprt
,
2317 .buf_alloc
= bc_malloc
,
2318 .buf_free
= bc_free
,
2319 .send_request
= bc_send_request
,
2320 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2322 .destroy
= bc_destroy
,
2323 .print_stats
= xs_tcp_print_stats
,
2326 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2327 unsigned int slot_table_size
)
2329 struct rpc_xprt
*xprt
;
2330 struct sock_xprt
*new;
2332 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2333 dprintk("RPC: xs_setup_xprt: address too large\n");
2334 return ERR_PTR(-EBADF
);
2337 new = kzalloc(sizeof(*new), GFP_KERNEL
);
2339 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2341 return ERR_PTR(-ENOMEM
);
2345 xprt
->max_reqs
= slot_table_size
;
2346 xprt
->slot
= kcalloc(xprt
->max_reqs
, sizeof(struct rpc_rqst
), GFP_KERNEL
);
2347 if (xprt
->slot
== NULL
) {
2349 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2351 return ERR_PTR(-ENOMEM
);
2354 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2355 xprt
->addrlen
= args
->addrlen
;
2357 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2362 static const struct rpc_timeout xs_udp_default_timeout
= {
2363 .to_initval
= 5 * HZ
,
2364 .to_maxval
= 30 * HZ
,
2365 .to_increment
= 5 * HZ
,
2370 * xs_setup_udp - Set up transport to use a UDP socket
2371 * @args: rpc transport creation arguments
2374 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2376 struct sockaddr
*addr
= args
->dstaddr
;
2377 struct rpc_xprt
*xprt
;
2378 struct sock_xprt
*transport
;
2380 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
);
2383 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2385 xprt
->prot
= IPPROTO_UDP
;
2387 /* XXX: header size can vary due to auth type, IPv6, etc. */
2388 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2390 xprt
->bind_timeout
= XS_BIND_TO
;
2391 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
2392 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2393 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2395 xprt
->ops
= &xs_udp_ops
;
2397 xprt
->timeout
= &xs_udp_default_timeout
;
2399 switch (addr
->sa_family
) {
2401 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2402 xprt_set_bound(xprt
);
2404 INIT_DELAYED_WORK(&transport
->connect_worker
,
2405 xs_udp_connect_worker4
);
2406 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2409 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2410 xprt_set_bound(xprt
);
2412 INIT_DELAYED_WORK(&transport
->connect_worker
,
2413 xs_udp_connect_worker6
);
2414 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2418 return ERR_PTR(-EAFNOSUPPORT
);
2421 if (xprt_bound(xprt
))
2422 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2423 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2424 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2425 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2427 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2428 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2429 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2431 if (try_module_get(THIS_MODULE
))
2436 return ERR_PTR(-EINVAL
);
2439 static const struct rpc_timeout xs_tcp_default_timeout
= {
2440 .to_initval
= 60 * HZ
,
2441 .to_maxval
= 60 * HZ
,
2446 * xs_setup_tcp - Set up transport to use a TCP socket
2447 * @args: rpc transport creation arguments
2450 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2452 struct sockaddr
*addr
= args
->dstaddr
;
2453 struct rpc_xprt
*xprt
;
2454 struct sock_xprt
*transport
;
2456 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2459 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2461 xprt
->prot
= IPPROTO_TCP
;
2462 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2463 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2465 xprt
->bind_timeout
= XS_BIND_TO
;
2466 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
2467 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2468 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2470 xprt
->ops
= &xs_tcp_ops
;
2471 xprt
->timeout
= &xs_tcp_default_timeout
;
2473 switch (addr
->sa_family
) {
2475 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2476 xprt_set_bound(xprt
);
2478 INIT_DELAYED_WORK(&transport
->connect_worker
,
2479 xs_tcp_connect_worker4
);
2480 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2483 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2484 xprt_set_bound(xprt
);
2486 INIT_DELAYED_WORK(&transport
->connect_worker
,
2487 xs_tcp_connect_worker6
);
2488 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2492 return ERR_PTR(-EAFNOSUPPORT
);
2495 if (xprt_bound(xprt
))
2496 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2497 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2498 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2499 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2501 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2502 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2503 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2506 if (try_module_get(THIS_MODULE
))
2511 return ERR_PTR(-EINVAL
);
2515 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2516 * @args: rpc transport creation arguments
2519 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2521 struct sockaddr
*addr
= args
->dstaddr
;
2522 struct rpc_xprt
*xprt
;
2523 struct sock_xprt
*transport
;
2524 struct svc_sock
*bc_sock
;
2529 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2532 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2534 xprt
->prot
= IPPROTO_TCP
;
2535 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2536 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2537 xprt
->timeout
= &xs_tcp_default_timeout
;
2540 xprt_set_bound(xprt
);
2541 xprt
->bind_timeout
= 0;
2542 xprt
->connect_timeout
= 0;
2543 xprt
->reestablish_timeout
= 0;
2544 xprt
->idle_timeout
= 0;
2547 * The backchannel uses the same socket connection as the
2550 xprt
->bc_xprt
= args
->bc_xprt
;
2551 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
2552 bc_sock
->sk_bc_xprt
= xprt
;
2553 transport
->sock
= bc_sock
->sk_sock
;
2554 transport
->inet
= bc_sock
->sk_sk
;
2556 xprt
->ops
= &bc_tcp_ops
;
2558 switch (addr
->sa_family
) {
2560 xs_format_peer_addresses(xprt
, "tcp",
2564 xs_format_peer_addresses(xprt
, "tcp",
2565 RPCBIND_NETID_TCP6
);
2569 return ERR_PTR(-EAFNOSUPPORT
);
2572 if (xprt_bound(xprt
))
2573 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2574 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2575 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2576 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2578 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2579 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2580 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2583 * Since we don't want connections for the backchannel, we set
2584 * the xprt status to connected
2586 xprt_set_connected(xprt
);
2589 if (try_module_get(THIS_MODULE
))
2593 return ERR_PTR(-EINVAL
);
2596 static struct xprt_class xs_udp_transport
= {
2597 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2599 .owner
= THIS_MODULE
,
2600 .ident
= XPRT_TRANSPORT_UDP
,
2601 .setup
= xs_setup_udp
,
2604 static struct xprt_class xs_tcp_transport
= {
2605 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
2607 .owner
= THIS_MODULE
,
2608 .ident
= XPRT_TRANSPORT_TCP
,
2609 .setup
= xs_setup_tcp
,
2612 static struct xprt_class xs_bc_tcp_transport
= {
2613 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
2614 .name
= "tcp NFSv4.1 backchannel",
2615 .owner
= THIS_MODULE
,
2616 .ident
= XPRT_TRANSPORT_BC_TCP
,
2617 .setup
= xs_setup_bc_tcp
,
2621 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2624 int init_socket_xprt(void)
2627 if (!sunrpc_table_header
)
2628 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
2631 xprt_register_transport(&xs_udp_transport
);
2632 xprt_register_transport(&xs_tcp_transport
);
2633 xprt_register_transport(&xs_bc_tcp_transport
);
2639 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2642 void cleanup_socket_xprt(void)
2645 if (sunrpc_table_header
) {
2646 unregister_sysctl_table(sunrpc_table_header
);
2647 sunrpc_table_header
= NULL
;
2651 xprt_unregister_transport(&xs_udp_transport
);
2652 xprt_unregister_transport(&xs_tcp_transport
);
2653 xprt_unregister_transport(&xs_bc_tcp_transport
);
2656 static int param_set_uint_minmax(const char *val
, struct kernel_param
*kp
,
2657 unsigned int min
, unsigned int max
)
2664 ret
= strict_strtoul(val
, 0, &num
);
2665 if (ret
== -EINVAL
|| num
< min
|| num
> max
)
2667 *((unsigned int *)kp
->arg
) = num
;
2671 static int param_set_portnr(const char *val
, struct kernel_param
*kp
)
2673 return param_set_uint_minmax(val
, kp
,
2678 static int param_get_portnr(char *buffer
, struct kernel_param
*kp
)
2680 return param_get_uint(buffer
, kp
);
2682 #define param_check_portnr(name, p) \
2683 __param_check(name, p, unsigned int);
2685 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
2686 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
2688 static int param_set_slot_table_size(const char *val
, struct kernel_param
*kp
)
2690 return param_set_uint_minmax(val
, kp
,
2692 RPC_MAX_SLOT_TABLE
);
2695 static int param_get_slot_table_size(char *buffer
, struct kernel_param
*kp
)
2697 return param_get_uint(buffer
, kp
);
2699 #define param_check_slot_table_size(name, p) \
2700 __param_check(name, p, unsigned int);
2702 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
2703 slot_table_size
, 0644);
2704 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
2705 slot_table_size
, 0644);