1 // SPDX-License-Identifier: GPL-2.0
3 * linux/net/sunrpc/xprtsock.c
5 * Client-side transport implementation for sockets.
7 * TCP callback races fixes (C) 1998 Red Hat
8 * TCP send fixes (C) 1998 Red Hat
9 * TCP NFS related read + write fixes
10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
12 * Rewrite of larges part of the code in order to stabilize TCP stuff.
13 * Fix behaviour when socket buffer is full.
14 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
19 * <gilles.quillard@bull.net>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/pagemap.h>
28 #include <linux/errno.h>
29 #include <linux/socket.h>
31 #include <linux/net.h>
34 #include <linux/udp.h>
35 #include <linux/tcp.h>
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/sched.h>
39 #include <linux/sunrpc/svcsock.h>
40 #include <linux/sunrpc/xprtsock.h>
41 #include <linux/file.h>
42 #ifdef CONFIG_SUNRPC_BACKCHANNEL
43 #include <linux/sunrpc/bc_xprt.h>
47 #include <net/checksum.h>
51 #include <trace/events/sunrpc.h>
55 #define RPC_TCP_READ_CHUNK_SZ (3*512*1024)
57 static void xs_close(struct rpc_xprt
*xprt
);
58 static void xs_tcp_set_socket_timeouts(struct rpc_xprt
*xprt
,
64 static unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
65 static unsigned int xprt_tcp_slot_table_entries
= RPC_MIN_SLOT_TABLE
;
66 static unsigned int xprt_max_tcp_slot_table_entries
= RPC_MAX_SLOT_TABLE
;
68 static unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
69 static unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
71 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
73 #define XS_TCP_LINGER_TO (15U * HZ)
74 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
77 * We can register our own files under /proc/sys/sunrpc by
78 * calling register_sysctl_table() again. The files in that
79 * directory become the union of all files registered there.
81 * We simply need to make sure that we don't collide with
82 * someone else's file names!
85 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
86 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
87 static unsigned int max_tcp_slot_table_limit
= RPC_MAX_SLOT_TABLE_LIMIT
;
88 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
89 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
91 static struct ctl_table_header
*sunrpc_table_header
;
94 * FIXME: changing the UDP slot table size should also resize the UDP
95 * socket buffers for existing UDP transports
97 static struct ctl_table xs_tunables_table
[] = {
99 .procname
= "udp_slot_table_entries",
100 .data
= &xprt_udp_slot_table_entries
,
101 .maxlen
= sizeof(unsigned int),
103 .proc_handler
= proc_dointvec_minmax
,
104 .extra1
= &min_slot_table_size
,
105 .extra2
= &max_slot_table_size
108 .procname
= "tcp_slot_table_entries",
109 .data
= &xprt_tcp_slot_table_entries
,
110 .maxlen
= sizeof(unsigned int),
112 .proc_handler
= proc_dointvec_minmax
,
113 .extra1
= &min_slot_table_size
,
114 .extra2
= &max_slot_table_size
117 .procname
= "tcp_max_slot_table_entries",
118 .data
= &xprt_max_tcp_slot_table_entries
,
119 .maxlen
= sizeof(unsigned int),
121 .proc_handler
= proc_dointvec_minmax
,
122 .extra1
= &min_slot_table_size
,
123 .extra2
= &max_tcp_slot_table_limit
126 .procname
= "min_resvport",
127 .data
= &xprt_min_resvport
,
128 .maxlen
= sizeof(unsigned int),
130 .proc_handler
= proc_dointvec_minmax
,
131 .extra1
= &xprt_min_resvport_limit
,
132 .extra2
= &xprt_max_resvport
135 .procname
= "max_resvport",
136 .data
= &xprt_max_resvport
,
137 .maxlen
= sizeof(unsigned int),
139 .proc_handler
= proc_dointvec_minmax
,
140 .extra1
= &xprt_min_resvport
,
141 .extra2
= &xprt_max_resvport_limit
144 .procname
= "tcp_fin_timeout",
145 .data
= &xs_tcp_fin_timeout
,
146 .maxlen
= sizeof(xs_tcp_fin_timeout
),
148 .proc_handler
= proc_dointvec_jiffies
,
153 static struct ctl_table sunrpc_table
[] = {
155 .procname
= "sunrpc",
157 .child
= xs_tunables_table
165 * Wait duration for a reply from the RPC portmapper.
167 #define XS_BIND_TO (60U * HZ)
170 * Delay if a UDP socket connect error occurs. This is most likely some
171 * kind of resource problem on the local host.
173 #define XS_UDP_REEST_TO (2U * HZ)
176 * The reestablish timeout allows clients to delay for a bit before attempting
177 * to reconnect to a server that just dropped our connection.
179 * We implement an exponential backoff when trying to reestablish a TCP
180 * transport connection with the server. Some servers like to drop a TCP
181 * connection when they are overworked, so we start with a short timeout and
182 * increase over time if the server is down or not responding.
184 #define XS_TCP_INIT_REEST_TO (3U * HZ)
187 * TCP idle timeout; client drops the transport socket if it is idle
188 * for this long. Note that we also timeout UDP sockets to prevent
189 * holding port numbers when there is no RPC traffic.
191 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
193 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
194 # undef RPC_DEBUG_DATA
195 # define RPCDBG_FACILITY RPCDBG_TRANS
198 #ifdef RPC_DEBUG_DATA
199 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
201 u8
*buf
= (u8
*) packet
;
204 dprintk("RPC: %s\n", msg
);
205 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
209 dprintk("0x%04x ", j
);
211 dprintk("%02x%02x%02x%02x ",
212 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
217 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
223 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
225 return (struct rpc_xprt
*) sk
->sk_user_data
;
228 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
230 return (struct sockaddr
*) &xprt
->addr
;
233 static inline struct sockaddr_un
*xs_addr_un(struct rpc_xprt
*xprt
)
235 return (struct sockaddr_un
*) &xprt
->addr
;
238 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
240 return (struct sockaddr_in
*) &xprt
->addr
;
243 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
245 return (struct sockaddr_in6
*) &xprt
->addr
;
248 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
250 struct sockaddr
*sap
= xs_addr(xprt
);
251 struct sockaddr_in6
*sin6
;
252 struct sockaddr_in
*sin
;
253 struct sockaddr_un
*sun
;
256 switch (sap
->sa_family
) {
258 sun
= xs_addr_un(xprt
);
259 strlcpy(buf
, sun
->sun_path
, sizeof(buf
));
260 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
261 kstrdup(buf
, GFP_KERNEL
);
264 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
265 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
266 kstrdup(buf
, GFP_KERNEL
);
267 sin
= xs_addr_in(xprt
);
268 snprintf(buf
, sizeof(buf
), "%08x", ntohl(sin
->sin_addr
.s_addr
));
271 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
272 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
273 kstrdup(buf
, GFP_KERNEL
);
274 sin6
= xs_addr_in6(xprt
);
275 snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
281 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
284 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
286 struct sockaddr
*sap
= xs_addr(xprt
);
289 snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
290 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
292 snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
293 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
296 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
297 const char *protocol
,
300 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
301 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
302 xs_format_common_peer_addresses(xprt
);
303 xs_format_common_peer_ports(xprt
);
306 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
308 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
309 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
311 xs_format_common_peer_ports(xprt
);
314 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
318 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
320 case RPC_DISPLAY_PROTO
:
321 case RPC_DISPLAY_NETID
:
324 kfree(xprt
->address_strings
[i
]);
328 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
330 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
332 struct msghdr msg
= {
334 .msg_namelen
= addrlen
,
335 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
338 .iov_base
= vec
->iov_base
+ base
,
339 .iov_len
= vec
->iov_len
- base
,
342 if (iov
.iov_len
!= 0)
343 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
344 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
347 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
, bool zerocopy
, int *sent_p
)
349 ssize_t (*do_sendpage
)(struct socket
*sock
, struct page
*page
,
350 int offset
, size_t size
, int flags
);
352 unsigned int remainder
;
355 remainder
= xdr
->page_len
- base
;
356 base
+= xdr
->page_base
;
357 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
359 do_sendpage
= sock
->ops
->sendpage
;
361 do_sendpage
= sock_no_sendpage
;
363 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
364 int flags
= XS_SENDMSG_FLAGS
;
370 flags
|= MSG_SENDPAGE_NOTLAST
| MSG_MORE
;
371 err
= do_sendpage(sock
, *ppage
, base
, len
, flags
);
372 if (remainder
== 0 || err
!= len
)
386 * xs_sendpages - write pages directly to a socket
387 * @sock: socket to send on
388 * @addr: UDP only -- address of destination
389 * @addrlen: UDP only -- length of destination address
390 * @xdr: buffer containing this request
391 * @base: starting position in the buffer
392 * @zerocopy: true if it is safe to use sendpage()
393 * @sent_p: return the total number of bytes successfully queued for sending
396 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
, bool zerocopy
, int *sent_p
)
398 unsigned int remainder
= xdr
->len
- base
;
410 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
411 unsigned int len
= xdr
->head
[0].iov_len
- base
;
413 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
414 if (remainder
== 0 || err
!= len
)
419 base
-= xdr
->head
[0].iov_len
;
421 if (base
< xdr
->page_len
) {
422 unsigned int len
= xdr
->page_len
- base
;
424 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0, zerocopy
, &sent
);
426 if (remainder
== 0 || sent
!= len
)
430 base
-= xdr
->page_len
;
432 if (base
>= xdr
->tail
[0].iov_len
)
434 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
443 static void xs_nospace_callback(struct rpc_task
*task
)
445 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
447 transport
->inet
->sk_write_pending
--;
451 * xs_nospace - place task on wait queue if transmit was incomplete
452 * @task: task to put to sleep
455 static int xs_nospace(struct rpc_task
*task
)
457 struct rpc_rqst
*req
= task
->tk_rqstp
;
458 struct rpc_xprt
*xprt
= req
->rq_xprt
;
459 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
460 struct sock
*sk
= transport
->inet
;
463 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
464 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
467 /* Protect against races with write_space */
468 spin_lock_bh(&xprt
->transport_lock
);
470 /* Don't race with disconnect */
471 if (xprt_connected(xprt
)) {
472 /* wait for more buffer space */
473 sk
->sk_write_pending
++;
474 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
478 spin_unlock_bh(&xprt
->transport_lock
);
480 /* Race breaker in case memory is freed before above code is called */
481 if (ret
== -EAGAIN
) {
482 struct socket_wq
*wq
;
485 wq
= rcu_dereference(sk
->sk_wq
);
486 set_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
);
489 sk
->sk_write_space(sk
);
495 * Construct a stream transport record marker in @buf.
497 static inline void xs_encode_stream_record_marker(struct xdr_buf
*buf
)
499 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
500 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
501 *base
= cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
| reclen
);
505 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
506 * @task: RPC task that manages the state of an RPC request
509 * 0: The request has been sent
510 * EAGAIN: The socket was blocked, please call again later to
511 * complete the request
512 * ENOTCONN: Caller needs to invoke connect logic then call again
513 * other: Some other error occured, the request was not sent
515 static int xs_local_send_request(struct rpc_task
*task
)
517 struct rpc_rqst
*req
= task
->tk_rqstp
;
518 struct rpc_xprt
*xprt
= req
->rq_xprt
;
519 struct sock_xprt
*transport
=
520 container_of(xprt
, struct sock_xprt
, xprt
);
521 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
525 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
527 xs_pktdump("packet data:",
528 req
->rq_svec
->iov_base
, req
->rq_svec
->iov_len
);
530 req
->rq_xtime
= ktime_get();
531 status
= xs_sendpages(transport
->sock
, NULL
, 0, xdr
, req
->rq_bytes_sent
,
533 dprintk("RPC: %s(%u) = %d\n",
534 __func__
, xdr
->len
- req
->rq_bytes_sent
, status
);
536 if (status
== -EAGAIN
&& sock_writeable(transport
->inet
))
539 if (likely(sent
> 0) || status
== 0) {
540 req
->rq_bytes_sent
+= sent
;
541 req
->rq_xmit_bytes_sent
+= sent
;
542 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
543 req
->rq_bytes_sent
= 0;
553 status
= xs_nospace(task
);
556 dprintk("RPC: sendmsg returned unrecognized error %d\n",
568 * xs_udp_send_request - write an RPC request to a UDP socket
569 * @task: address of RPC task that manages the state of an RPC request
572 * 0: The request has been sent
573 * EAGAIN: The socket was blocked, please call again later to
574 * complete the request
575 * ENOTCONN: Caller needs to invoke connect logic then call again
576 * other: Some other error occurred, the request was not sent
578 static int xs_udp_send_request(struct rpc_task
*task
)
580 struct rpc_rqst
*req
= task
->tk_rqstp
;
581 struct rpc_xprt
*xprt
= req
->rq_xprt
;
582 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
583 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
587 xs_pktdump("packet data:",
588 req
->rq_svec
->iov_base
,
589 req
->rq_svec
->iov_len
);
591 if (!xprt_bound(xprt
))
593 req
->rq_xtime
= ktime_get();
594 status
= xs_sendpages(transport
->sock
, xs_addr(xprt
), xprt
->addrlen
,
595 xdr
, req
->rq_bytes_sent
, true, &sent
);
597 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
598 xdr
->len
- req
->rq_bytes_sent
, status
);
600 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
601 if (status
== -EPERM
)
604 if (status
== -EAGAIN
&& sock_writeable(transport
->inet
))
607 if (sent
> 0 || status
== 0) {
608 req
->rq_xmit_bytes_sent
+= sent
;
609 if (sent
>= req
->rq_slen
)
611 /* Still some bytes left; set up for a retry later. */
619 /* Should we call xs_close() here? */
622 status
= xs_nospace(task
);
629 /* When the server has died, an ICMP port unreachable message
630 * prompts ECONNREFUSED. */
633 dprintk("RPC: sendmsg returned unrecognized error %d\n",
641 * xs_tcp_send_request - write an RPC request to a TCP socket
642 * @task: address of RPC task that manages the state of an RPC request
645 * 0: The request has been sent
646 * EAGAIN: The socket was blocked, please call again later to
647 * complete the request
648 * ENOTCONN: Caller needs to invoke connect logic then call again
649 * other: Some other error occurred, the request was not sent
651 * XXX: In the case of soft timeouts, should we eventually give up
652 * if sendmsg is not able to make progress?
654 static int xs_tcp_send_request(struct rpc_task
*task
)
656 struct rpc_rqst
*req
= task
->tk_rqstp
;
657 struct rpc_xprt
*xprt
= req
->rq_xprt
;
658 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
659 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
660 bool zerocopy
= true;
661 bool vm_wait
= false;
665 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
667 xs_pktdump("packet data:",
668 req
->rq_svec
->iov_base
,
669 req
->rq_svec
->iov_len
);
670 /* Don't use zero copy if this is a resend. If the RPC call
671 * completes while the socket holds a reference to the pages,
672 * then we may end up resending corrupted data.
674 if (task
->tk_flags
& RPC_TASK_SENT
)
677 if (test_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
))
678 xs_tcp_set_socket_timeouts(xprt
, transport
->sock
);
680 /* Continue transmitting the packet/record. We must be careful
681 * to cope with writespace callbacks arriving _after_ we have
682 * called sendmsg(). */
683 req
->rq_xtime
= ktime_get();
686 status
= xs_sendpages(transport
->sock
, NULL
, 0, xdr
,
687 req
->rq_bytes_sent
, zerocopy
, &sent
);
689 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
690 xdr
->len
- req
->rq_bytes_sent
, status
);
692 /* If we've sent the entire packet, immediately
693 * reset the count of bytes sent. */
694 req
->rq_bytes_sent
+= sent
;
695 req
->rq_xmit_bytes_sent
+= sent
;
696 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
697 req
->rq_bytes_sent
= 0;
701 WARN_ON_ONCE(sent
== 0 && status
== 0);
703 if (status
== -EAGAIN
) {
705 * Return EAGAIN if we're sure we're hitting the
706 * socket send buffer limits.
708 if (test_bit(SOCK_NOSPACE
, &transport
->sock
->flags
))
711 * Did we hit a memory allocation failure?
717 /* Retry, knowing now that we're below the
718 * socket send buffer limit
732 /* Should we call xs_close() here? */
735 status
= xs_nospace(task
);
745 dprintk("RPC: sendmsg returned unrecognized error %d\n",
753 * xs_tcp_release_xprt - clean up after a tcp transmission
757 * This cleans up if an error causes us to abort the transmission of a request.
758 * In this case, the socket may need to be reset in order to avoid confusing
761 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
763 struct rpc_rqst
*req
;
765 if (task
!= xprt
->snd_task
)
769 req
= task
->tk_rqstp
;
772 if (req
->rq_bytes_sent
== 0)
774 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
776 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
778 xprt_release_xprt(xprt
, task
);
781 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
783 transport
->old_data_ready
= sk
->sk_data_ready
;
784 transport
->old_state_change
= sk
->sk_state_change
;
785 transport
->old_write_space
= sk
->sk_write_space
;
786 transport
->old_error_report
= sk
->sk_error_report
;
789 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
791 sk
->sk_data_ready
= transport
->old_data_ready
;
792 sk
->sk_state_change
= transport
->old_state_change
;
793 sk
->sk_write_space
= transport
->old_write_space
;
794 sk
->sk_error_report
= transport
->old_error_report
;
797 static void xs_sock_reset_state_flags(struct rpc_xprt
*xprt
)
799 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
801 clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
);
804 static void xs_sock_reset_connection_flags(struct rpc_xprt
*xprt
)
806 smp_mb__before_atomic();
807 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
808 clear_bit(XPRT_CLOSING
, &xprt
->state
);
809 xs_sock_reset_state_flags(xprt
);
810 smp_mb__after_atomic();
814 * xs_error_report - callback to handle TCP socket state errors
817 * Note: we don't call sock_error() since there may be a rpc_task
818 * using the socket, and so we don't want to clear sk->sk_err.
820 static void xs_error_report(struct sock
*sk
)
822 struct rpc_xprt
*xprt
;
825 read_lock_bh(&sk
->sk_callback_lock
);
826 if (!(xprt
= xprt_from_sock(sk
)))
832 dprintk("RPC: xs_error_report client %p, error=%d...\n",
834 trace_rpc_socket_error(xprt
, sk
->sk_socket
, err
);
835 xprt_wake_pending_tasks(xprt
, err
);
837 read_unlock_bh(&sk
->sk_callback_lock
);
840 static void xs_reset_transport(struct sock_xprt
*transport
)
842 struct socket
*sock
= transport
->sock
;
843 struct sock
*sk
= transport
->inet
;
844 struct rpc_xprt
*xprt
= &transport
->xprt
;
849 if (atomic_read(&transport
->xprt
.swapper
))
850 sk_clear_memalloc(sk
);
852 kernel_sock_shutdown(sock
, SHUT_RDWR
);
854 mutex_lock(&transport
->recv_mutex
);
855 write_lock_bh(&sk
->sk_callback_lock
);
856 transport
->inet
= NULL
;
857 transport
->sock
= NULL
;
859 sk
->sk_user_data
= NULL
;
861 xs_restore_old_callbacks(transport
, sk
);
862 xprt_clear_connected(xprt
);
863 write_unlock_bh(&sk
->sk_callback_lock
);
864 xs_sock_reset_connection_flags(xprt
);
865 mutex_unlock(&transport
->recv_mutex
);
867 trace_rpc_socket_close(xprt
, sock
);
872 * xs_close - close a socket
875 * This is used when all requests are complete; ie, no DRC state remains
876 * on the server we want to save.
878 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
879 * xs_reset_transport() zeroing the socket from underneath a writer.
881 static void xs_close(struct rpc_xprt
*xprt
)
883 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
885 dprintk("RPC: xs_close xprt %p\n", xprt
);
887 xs_reset_transport(transport
);
888 xprt
->reestablish_timeout
= 0;
890 xprt_disconnect_done(xprt
);
893 static void xs_inject_disconnect(struct rpc_xprt
*xprt
)
895 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
897 xprt_disconnect_done(xprt
);
900 static void xs_xprt_free(struct rpc_xprt
*xprt
)
902 xs_free_peer_addresses(xprt
);
907 * xs_destroy - prepare to shutdown a transport
908 * @xprt: doomed transport
911 static void xs_destroy(struct rpc_xprt
*xprt
)
913 struct sock_xprt
*transport
= container_of(xprt
,
914 struct sock_xprt
, xprt
);
915 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
917 cancel_delayed_work_sync(&transport
->connect_worker
);
919 cancel_work_sync(&transport
->recv_worker
);
921 module_put(THIS_MODULE
);
924 static int xs_local_copy_to_xdr(struct xdr_buf
*xdr
, struct sk_buff
*skb
)
926 struct xdr_skb_reader desc
= {
928 .offset
= sizeof(rpc_fraghdr
),
929 .count
= skb
->len
- sizeof(rpc_fraghdr
),
932 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_bits
) < 0)
940 * xs_local_data_read_skb
945 * Currently this assumes we can read the whole reply in a single gulp.
947 static void xs_local_data_read_skb(struct rpc_xprt
*xprt
,
951 struct rpc_task
*task
;
952 struct rpc_rqst
*rovr
;
957 repsize
= skb
->len
- sizeof(rpc_fraghdr
);
959 dprintk("RPC: impossible RPC reply size %d\n", repsize
);
963 /* Copy the XID from the skb... */
964 xp
= skb_header_pointer(skb
, sizeof(rpc_fraghdr
), sizeof(_xid
), &_xid
);
968 /* Look up and lock the request corresponding to the given XID */
969 spin_lock(&xprt
->recv_lock
);
970 rovr
= xprt_lookup_rqst(xprt
, *xp
);
974 spin_unlock(&xprt
->recv_lock
);
975 task
= rovr
->rq_task
;
977 copied
= rovr
->rq_private_buf
.buflen
;
978 if (copied
> repsize
)
981 if (xs_local_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
982 dprintk("RPC: sk_buff copy failed\n");
983 spin_lock(&xprt
->recv_lock
);
987 spin_lock(&xprt
->recv_lock
);
988 xprt_complete_rqst(task
, copied
);
990 xprt_unpin_rqst(rovr
);
992 spin_unlock(&xprt
->recv_lock
);
995 static void xs_local_data_receive(struct sock_xprt
*transport
)
1002 mutex_lock(&transport
->recv_mutex
);
1003 sk
= transport
->inet
;
1007 skb
= skb_recv_datagram(sk
, 0, 1, &err
);
1009 xs_local_data_read_skb(&transport
->xprt
, sk
, skb
);
1010 skb_free_datagram(sk
, skb
);
1013 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
1015 if (need_resched()) {
1016 mutex_unlock(&transport
->recv_mutex
);
1022 mutex_unlock(&transport
->recv_mutex
);
1025 static void xs_local_data_receive_workfn(struct work_struct
*work
)
1027 struct sock_xprt
*transport
=
1028 container_of(work
, struct sock_xprt
, recv_worker
);
1029 xs_local_data_receive(transport
);
1033 * xs_udp_data_read_skb - receive callback for UDP sockets
1039 static void xs_udp_data_read_skb(struct rpc_xprt
*xprt
,
1041 struct sk_buff
*skb
)
1043 struct rpc_task
*task
;
1044 struct rpc_rqst
*rovr
;
1045 int repsize
, copied
;
1051 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
1055 /* Copy the XID from the skb... */
1056 xp
= skb_header_pointer(skb
, 0, sizeof(_xid
), &_xid
);
1060 /* Look up and lock the request corresponding to the given XID */
1061 spin_lock(&xprt
->recv_lock
);
1062 rovr
= xprt_lookup_rqst(xprt
, *xp
);
1065 xprt_pin_rqst(rovr
);
1066 xprt_update_rtt(rovr
->rq_task
);
1067 spin_unlock(&xprt
->recv_lock
);
1068 task
= rovr
->rq_task
;
1070 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
1073 /* Suck it into the iovec, verify checksum if not done by hw. */
1074 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
1075 spin_lock(&xprt
->recv_lock
);
1076 __UDPX_INC_STATS(sk
, UDP_MIB_INERRORS
);
1081 spin_lock_bh(&xprt
->transport_lock
);
1082 xprt_adjust_cwnd(xprt
, task
, copied
);
1083 spin_unlock_bh(&xprt
->transport_lock
);
1084 spin_lock(&xprt
->recv_lock
);
1085 xprt_complete_rqst(task
, copied
);
1086 __UDPX_INC_STATS(sk
, UDP_MIB_INDATAGRAMS
);
1088 xprt_unpin_rqst(rovr
);
1090 spin_unlock(&xprt
->recv_lock
);
1093 static void xs_udp_data_receive(struct sock_xprt
*transport
)
1095 struct sk_buff
*skb
;
1100 mutex_lock(&transport
->recv_mutex
);
1101 sk
= transport
->inet
;
1105 skb
= skb_recv_udp(sk
, 0, 1, &err
);
1107 xs_udp_data_read_skb(&transport
->xprt
, sk
, skb
);
1111 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
1113 if (need_resched()) {
1114 mutex_unlock(&transport
->recv_mutex
);
1120 mutex_unlock(&transport
->recv_mutex
);
1123 static void xs_udp_data_receive_workfn(struct work_struct
*work
)
1125 struct sock_xprt
*transport
=
1126 container_of(work
, struct sock_xprt
, recv_worker
);
1127 xs_udp_data_receive(transport
);
1131 * xs_data_ready - "data ready" callback for UDP sockets
1132 * @sk: socket with data to read
1135 static void xs_data_ready(struct sock
*sk
)
1137 struct rpc_xprt
*xprt
;
1139 read_lock_bh(&sk
->sk_callback_lock
);
1140 dprintk("RPC: xs_data_ready...\n");
1141 xprt
= xprt_from_sock(sk
);
1143 struct sock_xprt
*transport
= container_of(xprt
,
1144 struct sock_xprt
, xprt
);
1145 transport
->old_data_ready(sk
);
1146 /* Any data means we had a useful conversation, so
1147 * then we don't need to delay the next reconnect
1149 if (xprt
->reestablish_timeout
)
1150 xprt
->reestablish_timeout
= 0;
1151 if (!test_and_set_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
1152 queue_work(xprtiod_workqueue
, &transport
->recv_worker
);
1154 read_unlock_bh(&sk
->sk_callback_lock
);
1158 * Helper function to force a TCP close if the server is sending
1159 * junk and/or it has put us in CLOSE_WAIT
1161 static void xs_tcp_force_close(struct rpc_xprt
*xprt
)
1163 xprt_force_disconnect(xprt
);
1166 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
1168 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1172 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
1173 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
1174 used
= xdr_skb_read_bits(desc
, p
, len
);
1175 transport
->tcp_offset
+= used
;
1179 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
1180 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
1181 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
1183 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
1184 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
1186 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
1187 transport
->tcp_offset
= 0;
1189 /* Sanity check of the record length */
1190 if (unlikely(transport
->tcp_reclen
< 8)) {
1191 dprintk("RPC: invalid TCP record fragment length\n");
1192 xs_tcp_force_close(xprt
);
1195 dprintk("RPC: reading TCP record fragment of length %d\n",
1196 transport
->tcp_reclen
);
1199 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
1201 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1202 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
1203 transport
->tcp_offset
= 0;
1204 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
1205 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1206 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
1207 transport
->tcp_copied
= 0;
1212 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1217 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
1218 dprintk("RPC: reading XID (%zu bytes)\n", len
);
1219 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
1220 used
= xdr_skb_read_bits(desc
, p
, len
);
1221 transport
->tcp_offset
+= used
;
1224 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
1225 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
1226 transport
->tcp_copied
= 4;
1227 dprintk("RPC: reading %s XID %08x\n",
1228 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
1230 ntohl(transport
->tcp_xid
));
1231 xs_tcp_check_fraghdr(transport
);
1234 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
1235 struct xdr_skb_reader
*desc
)
1242 * We want transport->tcp_offset to be 8 at the end of this routine
1243 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1244 * When this function is called for the first time,
1245 * transport->tcp_offset is 4 (after having already read the xid).
1247 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
1248 len
= sizeof(transport
->tcp_calldir
) - offset
;
1249 dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len
);
1250 p
= ((char *) &transport
->tcp_calldir
) + offset
;
1251 used
= xdr_skb_read_bits(desc
, p
, len
);
1252 transport
->tcp_offset
+= used
;
1255 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
1257 * We don't yet have the XDR buffer, so we will write the calldir
1258 * out after we get the buffer from the 'struct rpc_rqst'
1260 switch (ntohl(transport
->tcp_calldir
)) {
1262 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1263 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1264 transport
->tcp_flags
|= TCP_RPC_REPLY
;
1267 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1268 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1269 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
1272 dprintk("RPC: invalid request message type\n");
1273 xs_tcp_force_close(&transport
->xprt
);
1275 xs_tcp_check_fraghdr(transport
);
1278 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
1279 struct xdr_skb_reader
*desc
,
1280 struct rpc_rqst
*req
)
1282 struct sock_xprt
*transport
=
1283 container_of(xprt
, struct sock_xprt
, xprt
);
1284 struct xdr_buf
*rcvbuf
;
1288 rcvbuf
= &req
->rq_private_buf
;
1290 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1292 * Save the RPC direction in the XDR buffer
1294 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1295 &transport
->tcp_calldir
,
1296 sizeof(transport
->tcp_calldir
));
1297 transport
->tcp_copied
+= sizeof(transport
->tcp_calldir
);
1298 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1302 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
)
1303 desc
->count
= transport
->tcp_reclen
- transport
->tcp_offset
;
1304 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1305 desc
, xdr_skb_read_bits
);
1308 /* Error when copying to the receive buffer,
1309 * usually because we weren't able to allocate
1310 * additional buffer pages. All we can do now
1311 * is turn off TCP_RCV_COPY_DATA, so the request
1312 * will not receive any additional updates,
1314 * Any remaining data from this record will
1317 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1318 dprintk("RPC: XID %08x truncated request\n",
1319 ntohl(transport
->tcp_xid
));
1320 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1321 "tcp_offset = %u, tcp_reclen = %u\n",
1322 xprt
, transport
->tcp_copied
,
1323 transport
->tcp_offset
, transport
->tcp_reclen
);
1327 transport
->tcp_copied
+= r
;
1328 transport
->tcp_offset
+= r
;
1329 desc
->count
= len
- r
;
1331 dprintk("RPC: XID %08x read %zd bytes\n",
1332 ntohl(transport
->tcp_xid
), r
);
1333 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1334 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1335 transport
->tcp_offset
, transport
->tcp_reclen
);
1337 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1338 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1339 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1340 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1341 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1346 * Finds the request corresponding to the RPC xid and invokes the common
1347 * tcp read code to read the data.
1349 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1350 struct xdr_skb_reader
*desc
)
1352 struct sock_xprt
*transport
=
1353 container_of(xprt
, struct sock_xprt
, xprt
);
1354 struct rpc_rqst
*req
;
1356 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1358 /* Find and lock the request corresponding to this xid */
1359 spin_lock(&xprt
->recv_lock
);
1360 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1362 dprintk("RPC: XID %08x request not found!\n",
1363 ntohl(transport
->tcp_xid
));
1364 spin_unlock(&xprt
->recv_lock
);
1368 spin_unlock(&xprt
->recv_lock
);
1370 xs_tcp_read_common(xprt
, desc
, req
);
1372 spin_lock(&xprt
->recv_lock
);
1373 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1374 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1375 xprt_unpin_rqst(req
);
1376 spin_unlock(&xprt
->recv_lock
);
1380 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1382 * Obtains an rpc_rqst previously allocated and invokes the common
1383 * tcp read code to read the data. The result is placed in the callback
1385 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1386 * connection and return -1.
1388 static int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1389 struct xdr_skb_reader
*desc
)
1391 struct sock_xprt
*transport
=
1392 container_of(xprt
, struct sock_xprt
, xprt
);
1393 struct rpc_rqst
*req
;
1395 /* Look up the request corresponding to the given XID */
1396 req
= xprt_lookup_bc_request(xprt
, transport
->tcp_xid
);
1398 printk(KERN_WARNING
"Callback slot table overflowed\n");
1399 xprt_force_disconnect(xprt
);
1403 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1404 xs_tcp_read_common(xprt
, desc
, req
);
1406 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1407 xprt_complete_bc_request(req
, transport
->tcp_copied
);
1412 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1413 struct xdr_skb_reader
*desc
)
1415 struct sock_xprt
*transport
=
1416 container_of(xprt
, struct sock_xprt
, xprt
);
1418 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1419 xs_tcp_read_reply(xprt
, desc
) :
1420 xs_tcp_read_callback(xprt
, desc
);
1423 static int xs_tcp_bc_up(struct svc_serv
*serv
, struct net
*net
)
1427 ret
= svc_create_xprt(serv
, "tcp-bc", net
, PF_INET
, 0,
1428 SVC_SOCK_ANONYMOUS
);
1434 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt
*xprt
)
1439 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1440 struct xdr_skb_reader
*desc
)
1442 return xs_tcp_read_reply(xprt
, desc
);
1444 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1447 * Read data off the transport. This can be either an RPC_CALL or an
1448 * RPC_REPLY. Relay the processing to helper functions.
1450 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1451 struct xdr_skb_reader
*desc
)
1453 struct sock_xprt
*transport
=
1454 container_of(xprt
, struct sock_xprt
, xprt
);
1456 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1457 xs_tcp_check_fraghdr(transport
);
1460 * The transport_lock protects the request handling.
1461 * There's no need to hold it to update the tcp_flags.
1463 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1467 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1471 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1472 if (len
> desc
->count
)
1475 desc
->offset
+= len
;
1476 transport
->tcp_offset
+= len
;
1477 dprintk("RPC: discarded %zu bytes\n", len
);
1478 xs_tcp_check_fraghdr(transport
);
1481 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1483 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1484 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1485 struct xdr_skb_reader desc
= {
1492 dprintk("RPC: xs_tcp_data_recv started\n");
1494 trace_xs_tcp_data_recv(transport
);
1495 /* Read in a new fragment marker if necessary */
1496 /* Can we ever really expect to get completely empty fragments? */
1497 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1498 xs_tcp_read_fraghdr(xprt
, &desc
);
1501 /* Read in the xid if necessary */
1502 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1503 xs_tcp_read_xid(transport
, &desc
);
1506 /* Read in the call/reply flag */
1507 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1508 xs_tcp_read_calldir(transport
, &desc
);
1511 /* Read in the request data */
1512 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1513 xs_tcp_read_data(xprt
, &desc
);
1516 /* Skip over any trailing bytes on short reads */
1517 xs_tcp_read_discard(transport
, &desc
);
1518 } while (desc
.count
);
1519 ret
= len
- desc
.count
;
1520 if (ret
< rd_desc
->count
)
1521 rd_desc
->count
-= ret
;
1524 trace_xs_tcp_data_recv(transport
);
1525 dprintk("RPC: xs_tcp_data_recv done\n");
1529 static void xs_tcp_data_receive(struct sock_xprt
*transport
)
1531 struct rpc_xprt
*xprt
= &transport
->xprt
;
1533 read_descriptor_t rd_desc
= {
1536 unsigned long total
= 0;
1540 mutex_lock(&transport
->recv_mutex
);
1541 sk
= transport
->inet
;
1545 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1547 rd_desc
.count
= RPC_TCP_READ_CHUNK_SZ
;
1549 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1550 if (rd_desc
.count
!= 0 || read
< 0) {
1551 clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
);
1557 if (need_resched()) {
1558 mutex_unlock(&transport
->recv_mutex
);
1563 if (test_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
1564 queue_work(xprtiod_workqueue
, &transport
->recv_worker
);
1566 mutex_unlock(&transport
->recv_mutex
);
1567 trace_xs_tcp_data_ready(xprt
, read
, total
);
1570 static void xs_tcp_data_receive_workfn(struct work_struct
*work
)
1572 struct sock_xprt
*transport
=
1573 container_of(work
, struct sock_xprt
, recv_worker
);
1574 xs_tcp_data_receive(transport
);
1578 * xs_tcp_state_change - callback to handle TCP socket state changes
1579 * @sk: socket whose state has changed
1582 static void xs_tcp_state_change(struct sock
*sk
)
1584 struct rpc_xprt
*xprt
;
1585 struct sock_xprt
*transport
;
1587 read_lock_bh(&sk
->sk_callback_lock
);
1588 if (!(xprt
= xprt_from_sock(sk
)))
1590 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1591 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1592 sk
->sk_state
, xprt_connected(xprt
),
1593 sock_flag(sk
, SOCK_DEAD
),
1594 sock_flag(sk
, SOCK_ZAPPED
),
1597 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1598 trace_rpc_socket_state_change(xprt
, sk
->sk_socket
);
1599 switch (sk
->sk_state
) {
1600 case TCP_ESTABLISHED
:
1601 spin_lock(&xprt
->transport_lock
);
1602 if (!xprt_test_and_set_connected(xprt
)) {
1604 /* Reset TCP record info */
1605 transport
->tcp_offset
= 0;
1606 transport
->tcp_reclen
= 0;
1607 transport
->tcp_copied
= 0;
1608 transport
->tcp_flags
=
1609 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1610 xprt
->connect_cookie
++;
1611 clear_bit(XPRT_SOCK_CONNECTING
, &transport
->sock_state
);
1612 xprt_clear_connecting(xprt
);
1614 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1616 spin_unlock(&xprt
->transport_lock
);
1619 /* The client initiated a shutdown of the socket */
1620 xprt
->connect_cookie
++;
1621 xprt
->reestablish_timeout
= 0;
1622 set_bit(XPRT_CLOSING
, &xprt
->state
);
1623 smp_mb__before_atomic();
1624 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1625 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1626 smp_mb__after_atomic();
1628 case TCP_CLOSE_WAIT
:
1629 /* The server initiated a shutdown of the socket */
1630 xprt
->connect_cookie
++;
1631 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1632 xs_tcp_force_close(xprt
);
1636 * If the server closed down the connection, make sure that
1637 * we back off before reconnecting
1639 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1640 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1643 set_bit(XPRT_CLOSING
, &xprt
->state
);
1644 smp_mb__before_atomic();
1645 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1646 smp_mb__after_atomic();
1649 if (test_and_clear_bit(XPRT_SOCK_CONNECTING
,
1650 &transport
->sock_state
))
1651 xprt_clear_connecting(xprt
);
1652 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1654 xprt_wake_pending_tasks(xprt
, -sk
->sk_err
);
1655 /* Trigger the socket release */
1656 xs_tcp_force_close(xprt
);
1659 read_unlock_bh(&sk
->sk_callback_lock
);
1662 static void xs_write_space(struct sock
*sk
)
1664 struct socket_wq
*wq
;
1665 struct rpc_xprt
*xprt
;
1669 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
1671 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1674 wq
= rcu_dereference(sk
->sk_wq
);
1675 if (!wq
|| test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
) == 0)
1678 xprt_write_space(xprt
);
1684 * xs_udp_write_space - callback invoked when socket buffer space
1686 * @sk: socket whose state has changed
1688 * Called when more output buffer space is available for this socket.
1689 * We try not to wake our writers until they can make "significant"
1690 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1691 * with a bunch of small requests.
1693 static void xs_udp_write_space(struct sock
*sk
)
1695 read_lock_bh(&sk
->sk_callback_lock
);
1697 /* from net/core/sock.c:sock_def_write_space */
1698 if (sock_writeable(sk
))
1701 read_unlock_bh(&sk
->sk_callback_lock
);
1705 * xs_tcp_write_space - callback invoked when socket buffer space
1707 * @sk: socket whose state has changed
1709 * Called when more output buffer space is available for this socket.
1710 * We try not to wake our writers until they can make "significant"
1711 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1712 * with a bunch of small requests.
1714 static void xs_tcp_write_space(struct sock
*sk
)
1716 read_lock_bh(&sk
->sk_callback_lock
);
1718 /* from net/core/stream.c:sk_stream_write_space */
1719 if (sk_stream_is_writeable(sk
))
1722 read_unlock_bh(&sk
->sk_callback_lock
);
1725 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1727 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1728 struct sock
*sk
= transport
->inet
;
1730 if (transport
->rcvsize
) {
1731 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1732 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1734 if (transport
->sndsize
) {
1735 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1736 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1737 sk
->sk_write_space(sk
);
1742 * xs_udp_set_buffer_size - set send and receive limits
1743 * @xprt: generic transport
1744 * @sndsize: requested size of send buffer, in bytes
1745 * @rcvsize: requested size of receive buffer, in bytes
1747 * Set socket send and receive buffer size limits.
1749 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1751 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1753 transport
->sndsize
= 0;
1755 transport
->sndsize
= sndsize
+ 1024;
1756 transport
->rcvsize
= 0;
1758 transport
->rcvsize
= rcvsize
+ 1024;
1760 xs_udp_do_set_buffer_size(xprt
);
1764 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1765 * @task: task that timed out
1767 * Adjust the congestion window after a retransmit timeout has occurred.
1769 static void xs_udp_timer(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1771 spin_lock_bh(&xprt
->transport_lock
);
1772 xprt_adjust_cwnd(xprt
, task
, -ETIMEDOUT
);
1773 spin_unlock_bh(&xprt
->transport_lock
);
1776 static unsigned short xs_get_random_port(void)
1778 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
+ 1;
1779 unsigned short rand
= (unsigned short) prandom_u32() % range
;
1780 return rand
+ xprt_min_resvport
;
1784 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1787 * Note that this function has to be called on all sockets that share the
1788 * same port, and it must be called before binding.
1790 static void xs_sock_set_reuseport(struct socket
*sock
)
1794 kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
,
1795 (char *)&opt
, sizeof(opt
));
1798 static unsigned short xs_sock_getport(struct socket
*sock
)
1800 struct sockaddr_storage buf
;
1801 unsigned short port
= 0;
1803 if (kernel_getsockname(sock
, (struct sockaddr
*)&buf
) < 0)
1805 switch (buf
.ss_family
) {
1807 port
= ntohs(((struct sockaddr_in6
*)&buf
)->sin6_port
);
1810 port
= ntohs(((struct sockaddr_in
*)&buf
)->sin_port
);
1817 * xs_set_port - reset the port number in the remote endpoint address
1818 * @xprt: generic transport
1819 * @port: new port number
1822 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1824 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1826 rpc_set_port(xs_addr(xprt
), port
);
1827 xs_update_peer_port(xprt
);
1830 static void xs_set_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1832 if (transport
->srcport
== 0)
1833 transport
->srcport
= xs_sock_getport(sock
);
1836 static unsigned short xs_get_srcport(struct sock_xprt
*transport
)
1838 unsigned short port
= transport
->srcport
;
1840 if (port
== 0 && transport
->xprt
.resvport
)
1841 port
= xs_get_random_port();
1845 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, unsigned short port
)
1847 if (transport
->srcport
!= 0)
1848 transport
->srcport
= 0;
1849 if (!transport
->xprt
.resvport
)
1851 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1852 return xprt_max_resvport
;
1855 static int xs_bind(struct sock_xprt
*transport
, struct socket
*sock
)
1857 struct sockaddr_storage myaddr
;
1859 unsigned short port
= xs_get_srcport(transport
);
1860 unsigned short last
;
1863 * If we are asking for any ephemeral port (i.e. port == 0 &&
1864 * transport->xprt.resvport == 0), don't bind. Let the local
1865 * port selection happen implicitly when the socket is used
1866 * (for example at connect time).
1868 * This ensures that we can continue to establish TCP
1869 * connections even when all local ephemeral ports are already
1870 * a part of some TCP connection. This makes no difference
1871 * for UDP sockets, but also doens't harm them.
1873 * If we're asking for any reserved port (i.e. port == 0 &&
1874 * transport->xprt.resvport == 1) xs_get_srcport above will
1875 * ensure that port is non-zero and we will bind as needed.
1880 memcpy(&myaddr
, &transport
->srcaddr
, transport
->xprt
.addrlen
);
1882 rpc_set_port((struct sockaddr
*)&myaddr
, port
);
1883 err
= kernel_bind(sock
, (struct sockaddr
*)&myaddr
,
1884 transport
->xprt
.addrlen
);
1886 transport
->srcport
= port
;
1890 port
= xs_next_srcport(transport
, port
);
1893 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1895 if (myaddr
.ss_family
== AF_INET
)
1896 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__
,
1897 &((struct sockaddr_in
*)&myaddr
)->sin_addr
,
1898 port
, err
? "failed" : "ok", err
);
1900 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__
,
1901 &((struct sockaddr_in6
*)&myaddr
)->sin6_addr
,
1902 port
, err
? "failed" : "ok", err
);
1907 * We don't support autobind on AF_LOCAL sockets
1909 static void xs_local_rpcbind(struct rpc_task
*task
)
1911 xprt_set_bound(task
->tk_xprt
);
1914 static void xs_local_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1918 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1919 static struct lock_class_key xs_key
[2];
1920 static struct lock_class_key xs_slock_key
[2];
1922 static inline void xs_reclassify_socketu(struct socket
*sock
)
1924 struct sock
*sk
= sock
->sk
;
1926 sock_lock_init_class_and_name(sk
, "slock-AF_LOCAL-RPC",
1927 &xs_slock_key
[1], "sk_lock-AF_LOCAL-RPC", &xs_key
[1]);
1930 static inline void xs_reclassify_socket4(struct socket
*sock
)
1932 struct sock
*sk
= sock
->sk
;
1934 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1935 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1938 static inline void xs_reclassify_socket6(struct socket
*sock
)
1940 struct sock
*sk
= sock
->sk
;
1942 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1943 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1946 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1948 if (WARN_ON_ONCE(!sock_allow_reclassification(sock
->sk
)))
1953 xs_reclassify_socketu(sock
);
1956 xs_reclassify_socket4(sock
);
1959 xs_reclassify_socket6(sock
);
1964 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1969 static void xs_dummy_setup_socket(struct work_struct
*work
)
1973 static struct socket
*xs_create_sock(struct rpc_xprt
*xprt
,
1974 struct sock_xprt
*transport
, int family
, int type
,
1975 int protocol
, bool reuseport
)
1977 struct socket
*sock
;
1980 err
= __sock_create(xprt
->xprt_net
, family
, type
, protocol
, &sock
, 1);
1982 dprintk("RPC: can't create %d transport socket (%d).\n",
1986 xs_reclassify_socket(family
, sock
);
1989 xs_sock_set_reuseport(sock
);
1991 err
= xs_bind(transport
, sock
);
1999 return ERR_PTR(err
);
2002 static int xs_local_finish_connecting(struct rpc_xprt
*xprt
,
2003 struct socket
*sock
)
2005 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
2008 if (!transport
->inet
) {
2009 struct sock
*sk
= sock
->sk
;
2011 write_lock_bh(&sk
->sk_callback_lock
);
2013 xs_save_old_callbacks(transport
, sk
);
2015 sk
->sk_user_data
= xprt
;
2016 sk
->sk_data_ready
= xs_data_ready
;
2017 sk
->sk_write_space
= xs_udp_write_space
;
2018 sock_set_flag(sk
, SOCK_FASYNC
);
2019 sk
->sk_error_report
= xs_error_report
;
2020 sk
->sk_allocation
= GFP_NOIO
;
2022 xprt_clear_connected(xprt
);
2024 /* Reset to new socket */
2025 transport
->sock
= sock
;
2026 transport
->inet
= sk
;
2028 write_unlock_bh(&sk
->sk_callback_lock
);
2031 /* Tell the socket layer to start connecting... */
2032 xprt
->stat
.connect_count
++;
2033 xprt
->stat
.connect_start
= jiffies
;
2034 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, 0);
2038 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
2039 * @transport: socket transport to connect
2041 static int xs_local_setup_socket(struct sock_xprt
*transport
)
2043 struct rpc_xprt
*xprt
= &transport
->xprt
;
2044 struct socket
*sock
;
2047 status
= __sock_create(xprt
->xprt_net
, AF_LOCAL
,
2048 SOCK_STREAM
, 0, &sock
, 1);
2050 dprintk("RPC: can't create AF_LOCAL "
2051 "transport socket (%d).\n", -status
);
2054 xs_reclassify_socket(AF_LOCAL
, sock
);
2056 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
2057 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2059 status
= xs_local_finish_connecting(xprt
, sock
);
2060 trace_rpc_socket_connect(xprt
, sock
, status
);
2063 dprintk("RPC: xprt %p connected to %s\n",
2064 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2065 xprt_set_connected(xprt
);
2069 dprintk("RPC: xprt %p: socket %s does not exist\n",
2070 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2073 dprintk("RPC: xprt %p: connection refused for %s\n",
2074 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2077 printk(KERN_ERR
"%s: unhandled error (%d) connecting to %s\n",
2079 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2083 xprt_clear_connecting(xprt
);
2084 xprt_wake_pending_tasks(xprt
, status
);
2088 static void xs_local_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2090 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2093 if (RPC_IS_ASYNC(task
)) {
2095 * We want the AF_LOCAL connect to be resolved in the
2096 * filesystem namespace of the process making the rpc
2097 * call. Thus we connect synchronously.
2099 * If we want to support asynchronous AF_LOCAL calls,
2100 * we'll need to figure out how to pass a namespace to
2103 rpc_exit(task
, -ENOTCONN
);
2106 ret
= xs_local_setup_socket(transport
);
2107 if (ret
&& !RPC_IS_SOFTCONN(task
))
2108 msleep_interruptible(15000);
2111 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2113 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2114 * know that we have exclusive access to the socket), to guard against
2115 * races with xs_reset_transport.
2117 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2119 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
2123 * If there's no sock, then we have nothing to set. The
2124 * reconnecting process will get it for us.
2126 if (!transport
->inet
)
2128 if (atomic_read(&xprt
->swapper
))
2129 sk_set_memalloc(transport
->inet
);
2133 * xs_enable_swap - Tag this transport as being used for swap.
2134 * @xprt: transport to tag
2136 * Take a reference to this transport on behalf of the rpc_clnt, and
2137 * optionally mark it for swapping if it wasn't already.
2140 xs_enable_swap(struct rpc_xprt
*xprt
)
2142 struct sock_xprt
*xs
= container_of(xprt
, struct sock_xprt
, xprt
);
2144 if (atomic_inc_return(&xprt
->swapper
) != 1)
2146 if (wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_KILLABLE
))
2147 return -ERESTARTSYS
;
2149 sk_set_memalloc(xs
->inet
);
2150 xprt_release_xprt(xprt
, NULL
);
2155 * xs_disable_swap - Untag this transport as being used for swap.
2156 * @xprt: transport to tag
2158 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2159 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2162 xs_disable_swap(struct rpc_xprt
*xprt
)
2164 struct sock_xprt
*xs
= container_of(xprt
, struct sock_xprt
, xprt
);
2166 if (!atomic_dec_and_test(&xprt
->swapper
))
2168 if (wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_KILLABLE
))
2171 sk_clear_memalloc(xs
->inet
);
2172 xprt_release_xprt(xprt
, NULL
);
2175 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2180 xs_enable_swap(struct rpc_xprt
*xprt
)
2186 xs_disable_swap(struct rpc_xprt
*xprt
)
2191 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2193 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2195 if (!transport
->inet
) {
2196 struct sock
*sk
= sock
->sk
;
2198 write_lock_bh(&sk
->sk_callback_lock
);
2200 xs_save_old_callbacks(transport
, sk
);
2202 sk
->sk_user_data
= xprt
;
2203 sk
->sk_data_ready
= xs_data_ready
;
2204 sk
->sk_write_space
= xs_udp_write_space
;
2205 sock_set_flag(sk
, SOCK_FASYNC
);
2206 sk
->sk_allocation
= GFP_NOIO
;
2208 xprt_set_connected(xprt
);
2210 /* Reset to new socket */
2211 transport
->sock
= sock
;
2212 transport
->inet
= sk
;
2214 xs_set_memalloc(xprt
);
2216 write_unlock_bh(&sk
->sk_callback_lock
);
2218 xs_udp_do_set_buffer_size(xprt
);
2220 xprt
->stat
.connect_start
= jiffies
;
2223 static void xs_udp_setup_socket(struct work_struct
*work
)
2225 struct sock_xprt
*transport
=
2226 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2227 struct rpc_xprt
*xprt
= &transport
->xprt
;
2228 struct socket
*sock
;
2231 sock
= xs_create_sock(xprt
, transport
,
2232 xs_addr(xprt
)->sa_family
, SOCK_DGRAM
,
2233 IPPROTO_UDP
, false);
2237 dprintk("RPC: worker connecting xprt %p via %s to "
2238 "%s (port %s)\n", xprt
,
2239 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2240 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2241 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2243 xs_udp_finish_connecting(xprt
, sock
);
2244 trace_rpc_socket_connect(xprt
, sock
, 0);
2247 xprt_unlock_connect(xprt
, transport
);
2248 xprt_clear_connecting(xprt
);
2249 xprt_wake_pending_tasks(xprt
, status
);
2253 * xs_tcp_shutdown - gracefully shut down a TCP socket
2256 * Initiates a graceful shutdown of the TCP socket by calling the
2257 * equivalent of shutdown(SHUT_RDWR);
2259 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
2261 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2262 struct socket
*sock
= transport
->sock
;
2263 int skst
= transport
->inet
? transport
->inet
->sk_state
: TCP_CLOSE
;
2269 kernel_sock_shutdown(sock
, SHUT_RDWR
);
2270 trace_rpc_socket_shutdown(xprt
, sock
);
2274 xs_reset_transport(transport
);
2278 static void xs_tcp_set_socket_timeouts(struct rpc_xprt
*xprt
,
2279 struct socket
*sock
)
2281 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2282 unsigned int keepidle
;
2283 unsigned int keepcnt
;
2284 unsigned int opt_on
= 1;
2287 spin_lock_bh(&xprt
->transport_lock
);
2288 keepidle
= DIV_ROUND_UP(xprt
->timeout
->to_initval
, HZ
);
2289 keepcnt
= xprt
->timeout
->to_retries
+ 1;
2290 timeo
= jiffies_to_msecs(xprt
->timeout
->to_initval
) *
2291 (xprt
->timeout
->to_retries
+ 1);
2292 clear_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
);
2293 spin_unlock_bh(&xprt
->transport_lock
);
2295 /* TCP Keepalive options */
2296 kernel_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
2297 (char *)&opt_on
, sizeof(opt_on
));
2298 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
,
2299 (char *)&keepidle
, sizeof(keepidle
));
2300 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
,
2301 (char *)&keepidle
, sizeof(keepidle
));
2302 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
,
2303 (char *)&keepcnt
, sizeof(keepcnt
));
2305 /* TCP user timeout (see RFC5482) */
2306 kernel_setsockopt(sock
, SOL_TCP
, TCP_USER_TIMEOUT
,
2307 (char *)&timeo
, sizeof(timeo
));
2310 static void xs_tcp_set_connect_timeout(struct rpc_xprt
*xprt
,
2311 unsigned long connect_timeout
,
2312 unsigned long reconnect_timeout
)
2314 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2315 struct rpc_timeout to
;
2316 unsigned long initval
;
2318 spin_lock_bh(&xprt
->transport_lock
);
2319 if (reconnect_timeout
< xprt
->max_reconnect_timeout
)
2320 xprt
->max_reconnect_timeout
= reconnect_timeout
;
2321 if (connect_timeout
< xprt
->connect_timeout
) {
2322 memcpy(&to
, xprt
->timeout
, sizeof(to
));
2323 initval
= DIV_ROUND_UP(connect_timeout
, to
.to_retries
+ 1);
2324 /* Arbitrary lower limit */
2325 if (initval
< XS_TCP_INIT_REEST_TO
<< 1)
2326 initval
= XS_TCP_INIT_REEST_TO
<< 1;
2327 to
.to_initval
= initval
;
2328 to
.to_maxval
= initval
;
2329 memcpy(&transport
->tcp_timeout
, &to
,
2330 sizeof(transport
->tcp_timeout
));
2331 xprt
->timeout
= &transport
->tcp_timeout
;
2332 xprt
->connect_timeout
= connect_timeout
;
2334 set_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
);
2335 spin_unlock_bh(&xprt
->transport_lock
);
2338 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2340 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2341 int ret
= -ENOTCONN
;
2343 if (!transport
->inet
) {
2344 struct sock
*sk
= sock
->sk
;
2345 unsigned int addr_pref
= IPV6_PREFER_SRC_PUBLIC
;
2347 /* Avoid temporary address, they are bad for long-lived
2348 * connections such as NFS mounts.
2349 * RFC4941, section 3.6 suggests that:
2350 * Individual applications, which have specific
2351 * knowledge about the normal duration of connections,
2352 * MAY override this as appropriate.
2354 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_ADDR_PREFERENCES
,
2355 (char *)&addr_pref
, sizeof(addr_pref
));
2357 xs_tcp_set_socket_timeouts(xprt
, sock
);
2359 write_lock_bh(&sk
->sk_callback_lock
);
2361 xs_save_old_callbacks(transport
, sk
);
2363 sk
->sk_user_data
= xprt
;
2364 sk
->sk_data_ready
= xs_data_ready
;
2365 sk
->sk_state_change
= xs_tcp_state_change
;
2366 sk
->sk_write_space
= xs_tcp_write_space
;
2367 sock_set_flag(sk
, SOCK_FASYNC
);
2368 sk
->sk_error_report
= xs_error_report
;
2369 sk
->sk_allocation
= GFP_NOIO
;
2371 /* socket options */
2372 sock_reset_flag(sk
, SOCK_LINGER
);
2373 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
2375 xprt_clear_connected(xprt
);
2377 /* Reset to new socket */
2378 transport
->sock
= sock
;
2379 transport
->inet
= sk
;
2381 write_unlock_bh(&sk
->sk_callback_lock
);
2384 if (!xprt_bound(xprt
))
2387 xs_set_memalloc(xprt
);
2389 /* Tell the socket layer to start connecting... */
2390 xprt
->stat
.connect_count
++;
2391 xprt
->stat
.connect_start
= jiffies
;
2392 set_bit(XPRT_SOCK_CONNECTING
, &transport
->sock_state
);
2393 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
2396 xs_set_srcport(transport
, sock
);
2400 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2401 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2403 case -EADDRNOTAVAIL
:
2404 /* Source port number is unavailable. Try a new one! */
2405 transport
->srcport
= 0;
2412 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2414 * Invoked by a work queue tasklet.
2416 static void xs_tcp_setup_socket(struct work_struct
*work
)
2418 struct sock_xprt
*transport
=
2419 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2420 struct socket
*sock
= transport
->sock
;
2421 struct rpc_xprt
*xprt
= &transport
->xprt
;
2425 sock
= xs_create_sock(xprt
, transport
,
2426 xs_addr(xprt
)->sa_family
, SOCK_STREAM
,
2429 status
= PTR_ERR(sock
);
2434 dprintk("RPC: worker connecting xprt %p via %s to "
2435 "%s (port %s)\n", xprt
,
2436 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2437 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2438 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2440 status
= xs_tcp_finish_connecting(xprt
, sock
);
2441 trace_rpc_socket_connect(xprt
, sock
, status
);
2442 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2443 xprt
, -status
, xprt_connected(xprt
),
2444 sock
->sk
->sk_state
);
2447 printk("%s: connect returned unhandled error %d\n",
2450 case -EADDRNOTAVAIL
:
2451 /* We're probably in TIME_WAIT. Get rid of existing socket,
2454 xs_tcp_force_close(xprt
);
2459 xprt_unlock_connect(xprt
, transport
);
2462 /* Happens, for instance, if the user specified a link
2463 * local IPv6 address without a scope-id.
2473 * xs_tcp_force_close() wakes tasks with -EIO.
2474 * We need to wake them first to ensure the
2475 * correct error code.
2477 xprt_wake_pending_tasks(xprt
, status
);
2478 xs_tcp_force_close(xprt
);
2483 xprt_unlock_connect(xprt
, transport
);
2484 xprt_clear_connecting(xprt
);
2485 xprt_wake_pending_tasks(xprt
, status
);
2488 static unsigned long xs_reconnect_delay(const struct rpc_xprt
*xprt
)
2490 unsigned long start
, now
= jiffies
;
2492 start
= xprt
->stat
.connect_start
+ xprt
->reestablish_timeout
;
2493 if (time_after(start
, now
))
2498 static void xs_reconnect_backoff(struct rpc_xprt
*xprt
)
2500 xprt
->reestablish_timeout
<<= 1;
2501 if (xprt
->reestablish_timeout
> xprt
->max_reconnect_timeout
)
2502 xprt
->reestablish_timeout
= xprt
->max_reconnect_timeout
;
2503 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2504 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2508 * xs_connect - connect a socket to a remote endpoint
2509 * @xprt: pointer to transport structure
2510 * @task: address of RPC task that manages state of connect request
2512 * TCP: If the remote end dropped the connection, delay reconnecting.
2514 * UDP socket connects are synchronous, but we use a work queue anyway
2515 * to guarantee that even unprivileged user processes can set up a
2516 * socket on a privileged port.
2518 * If a UDP socket connect fails, the delay behavior here prevents
2519 * retry floods (hard mounts).
2521 static void xs_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2523 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2524 unsigned long delay
= 0;
2526 WARN_ON_ONCE(!xprt_lock_connect(xprt
, task
, transport
));
2528 if (transport
->sock
!= NULL
) {
2529 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2531 xprt
, xprt
->reestablish_timeout
/ HZ
);
2533 /* Start by resetting any existing state */
2534 xs_reset_transport(transport
);
2536 delay
= xs_reconnect_delay(xprt
);
2537 xs_reconnect_backoff(xprt
);
2540 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2542 queue_delayed_work(xprtiod_workqueue
,
2543 &transport
->connect_worker
,
2548 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2549 * @xprt: rpc_xprt struct containing statistics
2553 static void xs_local_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2557 if (xprt_connected(xprt
))
2558 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2560 seq_printf(seq
, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2561 "%llu %llu %lu %llu %llu\n",
2562 xprt
->stat
.bind_count
,
2563 xprt
->stat
.connect_count
,
2564 xprt
->stat
.connect_time
,
2568 xprt
->stat
.bad_xids
,
2571 xprt
->stat
.max_slots
,
2572 xprt
->stat
.sending_u
,
2573 xprt
->stat
.pending_u
);
2577 * xs_udp_print_stats - display UDP socket-specifc stats
2578 * @xprt: rpc_xprt struct containing statistics
2582 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2584 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2586 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2589 xprt
->stat
.bind_count
,
2592 xprt
->stat
.bad_xids
,
2595 xprt
->stat
.max_slots
,
2596 xprt
->stat
.sending_u
,
2597 xprt
->stat
.pending_u
);
2601 * xs_tcp_print_stats - display TCP socket-specifc stats
2602 * @xprt: rpc_xprt struct containing statistics
2606 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2608 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2611 if (xprt_connected(xprt
))
2612 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2614 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2615 "%llu %llu %lu %llu %llu\n",
2617 xprt
->stat
.bind_count
,
2618 xprt
->stat
.connect_count
,
2619 xprt
->stat
.connect_time
,
2623 xprt
->stat
.bad_xids
,
2626 xprt
->stat
.max_slots
,
2627 xprt
->stat
.sending_u
,
2628 xprt
->stat
.pending_u
);
2632 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2633 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2634 * to use the server side send routines.
2636 static int bc_malloc(struct rpc_task
*task
)
2638 struct rpc_rqst
*rqst
= task
->tk_rqstp
;
2639 size_t size
= rqst
->rq_callsize
;
2641 struct rpc_buffer
*buf
;
2643 if (size
> PAGE_SIZE
- sizeof(struct rpc_buffer
)) {
2644 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2649 page
= alloc_page(GFP_KERNEL
);
2653 buf
= page_address(page
);
2654 buf
->len
= PAGE_SIZE
;
2656 rqst
->rq_buffer
= buf
->data
;
2657 rqst
->rq_rbuffer
= (char *)rqst
->rq_buffer
+ rqst
->rq_callsize
;
2662 * Free the space allocated in the bc_alloc routine
2664 static void bc_free(struct rpc_task
*task
)
2666 void *buffer
= task
->tk_rqstp
->rq_buffer
;
2667 struct rpc_buffer
*buf
;
2669 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2670 free_page((unsigned long)buf
);
2674 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2675 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2677 static int bc_sendto(struct rpc_rqst
*req
)
2680 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2681 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2682 struct sock_xprt
*transport
=
2683 container_of(xprt
, struct sock_xprt
, xprt
);
2684 struct socket
*sock
= transport
->sock
;
2685 unsigned long headoff
;
2686 unsigned long tailoff
;
2688 xs_encode_stream_record_marker(xbufp
);
2690 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2691 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2692 len
= svc_send_common(sock
, xbufp
,
2693 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2694 xbufp
->tail
[0].iov_base
, tailoff
);
2696 if (len
!= xbufp
->len
) {
2697 printk(KERN_NOTICE
"Error sending entire callback!\n");
2705 * The send routine. Borrows from svc_send
2707 static int bc_send_request(struct rpc_task
*task
)
2709 struct rpc_rqst
*req
= task
->tk_rqstp
;
2710 struct svc_xprt
*xprt
;
2713 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2715 * Get the server socket associated with this callback xprt
2717 xprt
= req
->rq_xprt
->bc_xprt
;
2720 * Grab the mutex to serialize data as the connection is shared
2721 * with the fore channel
2723 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2724 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2725 if (!mutex_trylock(&xprt
->xpt_mutex
))
2727 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2729 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2732 len
= bc_sendto(req
);
2733 mutex_unlock(&xprt
->xpt_mutex
);
2742 * The close routine. Since this is client initiated, we do nothing
2745 static void bc_close(struct rpc_xprt
*xprt
)
2750 * The xprt destroy routine. Again, because this connection is client
2751 * initiated, we do nothing
2754 static void bc_destroy(struct rpc_xprt
*xprt
)
2756 dprintk("RPC: bc_destroy xprt %p\n", xprt
);
2759 module_put(THIS_MODULE
);
2762 static const struct rpc_xprt_ops xs_local_ops
= {
2763 .reserve_xprt
= xprt_reserve_xprt
,
2764 .release_xprt
= xs_tcp_release_xprt
,
2765 .alloc_slot
= xprt_alloc_slot
,
2766 .rpcbind
= xs_local_rpcbind
,
2767 .set_port
= xs_local_set_port
,
2768 .connect
= xs_local_connect
,
2769 .buf_alloc
= rpc_malloc
,
2770 .buf_free
= rpc_free
,
2771 .send_request
= xs_local_send_request
,
2772 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2774 .destroy
= xs_destroy
,
2775 .print_stats
= xs_local_print_stats
,
2776 .enable_swap
= xs_enable_swap
,
2777 .disable_swap
= xs_disable_swap
,
2780 static const struct rpc_xprt_ops xs_udp_ops
= {
2781 .set_buffer_size
= xs_udp_set_buffer_size
,
2782 .reserve_xprt
= xprt_reserve_xprt_cong
,
2783 .release_xprt
= xprt_release_xprt_cong
,
2784 .alloc_slot
= xprt_alloc_slot
,
2785 .rpcbind
= rpcb_getport_async
,
2786 .set_port
= xs_set_port
,
2787 .connect
= xs_connect
,
2788 .buf_alloc
= rpc_malloc
,
2789 .buf_free
= rpc_free
,
2790 .send_request
= xs_udp_send_request
,
2791 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2792 .timer
= xs_udp_timer
,
2793 .release_request
= xprt_release_rqst_cong
,
2795 .destroy
= xs_destroy
,
2796 .print_stats
= xs_udp_print_stats
,
2797 .enable_swap
= xs_enable_swap
,
2798 .disable_swap
= xs_disable_swap
,
2799 .inject_disconnect
= xs_inject_disconnect
,
2802 static const struct rpc_xprt_ops xs_tcp_ops
= {
2803 .reserve_xprt
= xprt_reserve_xprt
,
2804 .release_xprt
= xs_tcp_release_xprt
,
2805 .alloc_slot
= xprt_lock_and_alloc_slot
,
2806 .rpcbind
= rpcb_getport_async
,
2807 .set_port
= xs_set_port
,
2808 .connect
= xs_connect
,
2809 .buf_alloc
= rpc_malloc
,
2810 .buf_free
= rpc_free
,
2811 .send_request
= xs_tcp_send_request
,
2812 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2813 .close
= xs_tcp_shutdown
,
2814 .destroy
= xs_destroy
,
2815 .set_connect_timeout
= xs_tcp_set_connect_timeout
,
2816 .print_stats
= xs_tcp_print_stats
,
2817 .enable_swap
= xs_enable_swap
,
2818 .disable_swap
= xs_disable_swap
,
2819 .inject_disconnect
= xs_inject_disconnect
,
2820 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2821 .bc_setup
= xprt_setup_bc
,
2822 .bc_up
= xs_tcp_bc_up
,
2823 .bc_maxpayload
= xs_tcp_bc_maxpayload
,
2824 .bc_free_rqst
= xprt_free_bc_rqst
,
2825 .bc_destroy
= xprt_destroy_bc
,
2830 * The rpc_xprt_ops for the server backchannel
2833 static const struct rpc_xprt_ops bc_tcp_ops
= {
2834 .reserve_xprt
= xprt_reserve_xprt
,
2835 .release_xprt
= xprt_release_xprt
,
2836 .alloc_slot
= xprt_alloc_slot
,
2837 .buf_alloc
= bc_malloc
,
2838 .buf_free
= bc_free
,
2839 .send_request
= bc_send_request
,
2840 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2842 .destroy
= bc_destroy
,
2843 .print_stats
= xs_tcp_print_stats
,
2844 .enable_swap
= xs_enable_swap
,
2845 .disable_swap
= xs_disable_swap
,
2846 .inject_disconnect
= xs_inject_disconnect
,
2849 static int xs_init_anyaddr(const int family
, struct sockaddr
*sap
)
2851 static const struct sockaddr_in sin
= {
2852 .sin_family
= AF_INET
,
2853 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2855 static const struct sockaddr_in6 sin6
= {
2856 .sin6_family
= AF_INET6
,
2857 .sin6_addr
= IN6ADDR_ANY_INIT
,
2864 memcpy(sap
, &sin
, sizeof(sin
));
2867 memcpy(sap
, &sin6
, sizeof(sin6
));
2870 dprintk("RPC: %s: Bad address family\n", __func__
);
2871 return -EAFNOSUPPORT
;
2876 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2877 unsigned int slot_table_size
,
2878 unsigned int max_slot_table_size
)
2880 struct rpc_xprt
*xprt
;
2881 struct sock_xprt
*new;
2883 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2884 dprintk("RPC: xs_setup_xprt: address too large\n");
2885 return ERR_PTR(-EBADF
);
2888 xprt
= xprt_alloc(args
->net
, sizeof(*new), slot_table_size
,
2889 max_slot_table_size
);
2891 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2893 return ERR_PTR(-ENOMEM
);
2896 new = container_of(xprt
, struct sock_xprt
, xprt
);
2897 mutex_init(&new->recv_mutex
);
2898 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2899 xprt
->addrlen
= args
->addrlen
;
2901 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2904 err
= xs_init_anyaddr(args
->dstaddr
->sa_family
,
2905 (struct sockaddr
*)&new->srcaddr
);
2908 return ERR_PTR(err
);
2915 static const struct rpc_timeout xs_local_default_timeout
= {
2916 .to_initval
= 10 * HZ
,
2917 .to_maxval
= 10 * HZ
,
2922 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2923 * @args: rpc transport creation arguments
2925 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2927 static struct rpc_xprt
*xs_setup_local(struct xprt_create
*args
)
2929 struct sockaddr_un
*sun
= (struct sockaddr_un
*)args
->dstaddr
;
2930 struct sock_xprt
*transport
;
2931 struct rpc_xprt
*xprt
;
2932 struct rpc_xprt
*ret
;
2934 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2935 xprt_max_tcp_slot_table_entries
);
2938 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2941 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2942 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2944 xprt
->bind_timeout
= XS_BIND_TO
;
2945 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2946 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2948 xprt
->ops
= &xs_local_ops
;
2949 xprt
->timeout
= &xs_local_default_timeout
;
2951 INIT_WORK(&transport
->recv_worker
, xs_local_data_receive_workfn
);
2952 INIT_DELAYED_WORK(&transport
->connect_worker
,
2953 xs_dummy_setup_socket
);
2955 switch (sun
->sun_family
) {
2957 if (sun
->sun_path
[0] != '/') {
2958 dprintk("RPC: bad AF_LOCAL address: %s\n",
2960 ret
= ERR_PTR(-EINVAL
);
2963 xprt_set_bound(xprt
);
2964 xs_format_peer_addresses(xprt
, "local", RPCBIND_NETID_LOCAL
);
2965 ret
= ERR_PTR(xs_local_setup_socket(transport
));
2970 ret
= ERR_PTR(-EAFNOSUPPORT
);
2974 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2975 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2977 if (try_module_get(THIS_MODULE
))
2979 ret
= ERR_PTR(-EINVAL
);
2985 static const struct rpc_timeout xs_udp_default_timeout
= {
2986 .to_initval
= 5 * HZ
,
2987 .to_maxval
= 30 * HZ
,
2988 .to_increment
= 5 * HZ
,
2993 * xs_setup_udp - Set up transport to use a UDP socket
2994 * @args: rpc transport creation arguments
2997 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2999 struct sockaddr
*addr
= args
->dstaddr
;
3000 struct rpc_xprt
*xprt
;
3001 struct sock_xprt
*transport
;
3002 struct rpc_xprt
*ret
;
3004 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
,
3005 xprt_udp_slot_table_entries
);
3008 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
3010 xprt
->prot
= IPPROTO_UDP
;
3012 /* XXX: header size can vary due to auth type, IPv6, etc. */
3013 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
3015 xprt
->bind_timeout
= XS_BIND_TO
;
3016 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
3017 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
3019 xprt
->ops
= &xs_udp_ops
;
3021 xprt
->timeout
= &xs_udp_default_timeout
;
3023 INIT_WORK(&transport
->recv_worker
, xs_udp_data_receive_workfn
);
3024 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_udp_setup_socket
);
3026 switch (addr
->sa_family
) {
3028 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
3029 xprt_set_bound(xprt
);
3031 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
3034 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
3035 xprt_set_bound(xprt
);
3037 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
3040 ret
= ERR_PTR(-EAFNOSUPPORT
);
3044 if (xprt_bound(xprt
))
3045 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3046 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3047 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3048 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3050 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3051 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3052 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3054 if (try_module_get(THIS_MODULE
))
3056 ret
= ERR_PTR(-EINVAL
);
3062 static const struct rpc_timeout xs_tcp_default_timeout
= {
3063 .to_initval
= 60 * HZ
,
3064 .to_maxval
= 60 * HZ
,
3069 * xs_setup_tcp - Set up transport to use a TCP socket
3070 * @args: rpc transport creation arguments
3073 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
3075 struct sockaddr
*addr
= args
->dstaddr
;
3076 struct rpc_xprt
*xprt
;
3077 struct sock_xprt
*transport
;
3078 struct rpc_xprt
*ret
;
3079 unsigned int max_slot_table_size
= xprt_max_tcp_slot_table_entries
;
3081 if (args
->flags
& XPRT_CREATE_INFINITE_SLOTS
)
3082 max_slot_table_size
= RPC_MAX_SLOT_TABLE_LIMIT
;
3084 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
3085 max_slot_table_size
);
3088 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
3090 xprt
->prot
= IPPROTO_TCP
;
3091 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
3092 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
3094 xprt
->bind_timeout
= XS_BIND_TO
;
3095 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
3096 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
3098 xprt
->ops
= &xs_tcp_ops
;
3099 xprt
->timeout
= &xs_tcp_default_timeout
;
3101 xprt
->max_reconnect_timeout
= xprt
->timeout
->to_maxval
;
3102 xprt
->connect_timeout
= xprt
->timeout
->to_initval
*
3103 (xprt
->timeout
->to_retries
+ 1);
3105 INIT_WORK(&transport
->recv_worker
, xs_tcp_data_receive_workfn
);
3106 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_setup_socket
);
3108 switch (addr
->sa_family
) {
3110 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
3111 xprt_set_bound(xprt
);
3113 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
3116 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
3117 xprt_set_bound(xprt
);
3119 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
3122 ret
= ERR_PTR(-EAFNOSUPPORT
);
3126 if (xprt_bound(xprt
))
3127 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3128 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3129 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3130 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3132 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3133 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3134 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3136 if (try_module_get(THIS_MODULE
))
3138 ret
= ERR_PTR(-EINVAL
);
3145 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3146 * @args: rpc transport creation arguments
3149 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
3151 struct sockaddr
*addr
= args
->dstaddr
;
3152 struct rpc_xprt
*xprt
;
3153 struct sock_xprt
*transport
;
3154 struct svc_sock
*bc_sock
;
3155 struct rpc_xprt
*ret
;
3157 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
3158 xprt_tcp_slot_table_entries
);
3161 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
3163 xprt
->prot
= IPPROTO_TCP
;
3164 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
3165 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
3166 xprt
->timeout
= &xs_tcp_default_timeout
;
3169 xprt_set_bound(xprt
);
3170 xprt
->bind_timeout
= 0;
3171 xprt
->reestablish_timeout
= 0;
3172 xprt
->idle_timeout
= 0;
3174 xprt
->ops
= &bc_tcp_ops
;
3176 switch (addr
->sa_family
) {
3178 xs_format_peer_addresses(xprt
, "tcp",
3182 xs_format_peer_addresses(xprt
, "tcp",
3183 RPCBIND_NETID_TCP6
);
3186 ret
= ERR_PTR(-EAFNOSUPPORT
);
3190 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3191 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3192 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3193 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3196 * Once we've associated a backchannel xprt with a connection,
3197 * we want to keep it around as long as the connection lasts,
3198 * in case we need to start using it for a backchannel again;
3199 * this reference won't be dropped until bc_xprt is destroyed.
3202 args
->bc_xprt
->xpt_bc_xprt
= xprt
;
3203 xprt
->bc_xprt
= args
->bc_xprt
;
3204 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
3205 transport
->sock
= bc_sock
->sk_sock
;
3206 transport
->inet
= bc_sock
->sk_sk
;
3209 * Since we don't want connections for the backchannel, we set
3210 * the xprt status to connected
3212 xprt_set_connected(xprt
);
3214 if (try_module_get(THIS_MODULE
))
3217 args
->bc_xprt
->xpt_bc_xprt
= NULL
;
3218 args
->bc_xprt
->xpt_bc_xps
= NULL
;
3220 ret
= ERR_PTR(-EINVAL
);
3226 static struct xprt_class xs_local_transport
= {
3227 .list
= LIST_HEAD_INIT(xs_local_transport
.list
),
3228 .name
= "named UNIX socket",
3229 .owner
= THIS_MODULE
,
3230 .ident
= XPRT_TRANSPORT_LOCAL
,
3231 .setup
= xs_setup_local
,
3234 static struct xprt_class xs_udp_transport
= {
3235 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
3237 .owner
= THIS_MODULE
,
3238 .ident
= XPRT_TRANSPORT_UDP
,
3239 .setup
= xs_setup_udp
,
3242 static struct xprt_class xs_tcp_transport
= {
3243 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
3245 .owner
= THIS_MODULE
,
3246 .ident
= XPRT_TRANSPORT_TCP
,
3247 .setup
= xs_setup_tcp
,
3250 static struct xprt_class xs_bc_tcp_transport
= {
3251 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
3252 .name
= "tcp NFSv4.1 backchannel",
3253 .owner
= THIS_MODULE
,
3254 .ident
= XPRT_TRANSPORT_BC_TCP
,
3255 .setup
= xs_setup_bc_tcp
,
3259 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3262 int init_socket_xprt(void)
3264 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3265 if (!sunrpc_table_header
)
3266 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
3269 xprt_register_transport(&xs_local_transport
);
3270 xprt_register_transport(&xs_udp_transport
);
3271 xprt_register_transport(&xs_tcp_transport
);
3272 xprt_register_transport(&xs_bc_tcp_transport
);
3278 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3281 void cleanup_socket_xprt(void)
3283 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3284 if (sunrpc_table_header
) {
3285 unregister_sysctl_table(sunrpc_table_header
);
3286 sunrpc_table_header
= NULL
;
3290 xprt_unregister_transport(&xs_local_transport
);
3291 xprt_unregister_transport(&xs_udp_transport
);
3292 xprt_unregister_transport(&xs_tcp_transport
);
3293 xprt_unregister_transport(&xs_bc_tcp_transport
);
3296 static int param_set_uint_minmax(const char *val
,
3297 const struct kernel_param
*kp
,
3298 unsigned int min
, unsigned int max
)
3305 ret
= kstrtouint(val
, 0, &num
);
3308 if (num
< min
|| num
> max
)
3310 *((unsigned int *)kp
->arg
) = num
;
3314 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
3316 if (kp
->arg
== &xprt_min_resvport
)
3317 return param_set_uint_minmax(val
, kp
,
3320 return param_set_uint_minmax(val
, kp
,
3325 static const struct kernel_param_ops param_ops_portnr
= {
3326 .set
= param_set_portnr
,
3327 .get
= param_get_uint
,
3330 #define param_check_portnr(name, p) \
3331 __param_check(name, p, unsigned int);
3333 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
3334 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
3336 static int param_set_slot_table_size(const char *val
,
3337 const struct kernel_param
*kp
)
3339 return param_set_uint_minmax(val
, kp
,
3341 RPC_MAX_SLOT_TABLE
);
3344 static const struct kernel_param_ops param_ops_slot_table_size
= {
3345 .set
= param_set_slot_table_size
,
3346 .get
= param_get_uint
,
3349 #define param_check_slot_table_size(name, p) \
3350 __param_check(name, p, unsigned int);
3352 static int param_set_max_slot_table_size(const char *val
,
3353 const struct kernel_param
*kp
)
3355 return param_set_uint_minmax(val
, kp
,
3357 RPC_MAX_SLOT_TABLE_LIMIT
);
3360 static const struct kernel_param_ops param_ops_max_slot_table_size
= {
3361 .set
= param_set_max_slot_table_size
,
3362 .get
= param_get_uint
,
3365 #define param_check_max_slot_table_size(name, p) \
3366 __param_check(name, p, unsigned int);
3368 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
3369 slot_table_size
, 0644);
3370 module_param_named(tcp_max_slot_table_entries
, xprt_max_tcp_slot_table_entries
,
3371 max_slot_table_size
, 0644);
3372 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
3373 slot_table_size
, 0644);