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>
50 #include <net/tls_prot.h>
51 #include <net/handshake.h>
53 #include <linux/bvec.h>
54 #include <linux/highmem.h>
55 #include <linux/uio.h>
56 #include <linux/sched/mm.h>
58 #include <trace/events/sock.h>
59 #include <trace/events/sunrpc.h>
64 static void xs_close(struct rpc_xprt *xprt);
65 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
66 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
72 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
73 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
74 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
76 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
77 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
79 #define XS_TCP_LINGER_TO (15U * HZ)
80 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
83 * We can register our own files under /proc/sys/sunrpc by
84 * calling register_sysctl() again. The files in that
85 * directory become the union of all files registered there.
87 * We simply need to make sure that we don't collide with
88 * someone else's file names!
91 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
92 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
93 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
94 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
95 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
97 static struct ctl_table_header *sunrpc_table_header;
99 static struct xprt_class xs_local_transport;
100 static struct xprt_class xs_udp_transport;
101 static struct xprt_class xs_tcp_transport;
102 static struct xprt_class xs_tcp_tls_transport;
103 static struct xprt_class xs_bc_tcp_transport;
106 * FIXME: changing the UDP slot table size should also resize the UDP
107 * socket buffers for existing UDP transports
109 static struct ctl_table xs_tunables_table[] = {
111 .procname = "udp_slot_table_entries",
112 .data = &xprt_udp_slot_table_entries,
113 .maxlen = sizeof(unsigned int),
115 .proc_handler = proc_dointvec_minmax,
116 .extra1 = &min_slot_table_size,
117 .extra2 = &max_slot_table_size
120 .procname = "tcp_slot_table_entries",
121 .data = &xprt_tcp_slot_table_entries,
122 .maxlen = sizeof(unsigned int),
124 .proc_handler = proc_dointvec_minmax,
125 .extra1 = &min_slot_table_size,
126 .extra2 = &max_slot_table_size
129 .procname = "tcp_max_slot_table_entries",
130 .data = &xprt_max_tcp_slot_table_entries,
131 .maxlen = sizeof(unsigned int),
133 .proc_handler = proc_dointvec_minmax,
134 .extra1 = &min_slot_table_size,
135 .extra2 = &max_tcp_slot_table_limit
138 .procname = "min_resvport",
139 .data = &xprt_min_resvport,
140 .maxlen = sizeof(unsigned int),
142 .proc_handler = proc_dointvec_minmax,
143 .extra1 = &xprt_min_resvport_limit,
144 .extra2 = &xprt_max_resvport_limit
147 .procname = "max_resvport",
148 .data = &xprt_max_resvport,
149 .maxlen = sizeof(unsigned int),
151 .proc_handler = proc_dointvec_minmax,
152 .extra1 = &xprt_min_resvport_limit,
153 .extra2 = &xprt_max_resvport_limit
156 .procname = "tcp_fin_timeout",
157 .data = &xs_tcp_fin_timeout,
158 .maxlen = sizeof(xs_tcp_fin_timeout),
160 .proc_handler = proc_dointvec_jiffies,
166 * Wait duration for a reply from the RPC portmapper.
168 #define XS_BIND_TO (60U * HZ)
171 * Delay if a UDP socket connect error occurs. This is most likely some
172 * kind of resource problem on the local host.
174 #define XS_UDP_REEST_TO (2U * HZ)
177 * The reestablish timeout allows clients to delay for a bit before attempting
178 * to reconnect to a server that just dropped our connection.
180 * We implement an exponential backoff when trying to reestablish a TCP
181 * transport connection with the server. Some servers like to drop a TCP
182 * connection when they are overworked, so we start with a short timeout and
183 * increase over time if the server is down or not responding.
185 #define XS_TCP_INIT_REEST_TO (3U * HZ)
188 * TCP idle timeout; client drops the transport socket if it is idle
189 * for this long. Note that we also timeout UDP sockets to prevent
190 * holding port numbers when there is no RPC traffic.
192 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
195 * TLS handshake timeout.
197 #define XS_TLS_HANDSHAKE_TO (10U * HZ)
199 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
200 # undef RPC_DEBUG_DATA
201 # define RPCDBG_FACILITY RPCDBG_TRANS
204 #ifdef RPC_DEBUG_DATA
205 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
207 u8 *buf = (u8 *) packet;
210 dprintk("RPC: %s\n", msg);
211 for (j = 0; j < count && j < 128; j += 4) {
215 dprintk("0x%04x ", j);
217 dprintk("%02x%02x%02x%02x ",
218 buf[j], buf[j+1], buf[j+2], buf[j+3]);
223 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
229 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
231 return (struct rpc_xprt *) sk->sk_user_data;
234 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
236 return (struct sockaddr *) &xprt->addr;
239 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
241 return (struct sockaddr_un *) &xprt->addr;
244 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
246 return (struct sockaddr_in *) &xprt->addr;
249 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
251 return (struct sockaddr_in6 *) &xprt->addr;
254 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
256 struct sockaddr *sap = xs_addr(xprt);
257 struct sockaddr_in6 *sin6;
258 struct sockaddr_in *sin;
259 struct sockaddr_un *sun;
262 switch (sap->sa_family) {
264 sun = xs_addr_un(xprt);
265 if (sun->sun_path[0]) {
266 strscpy(buf, sun->sun_path, sizeof(buf));
269 strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1);
271 xprt->address_strings[RPC_DISPLAY_ADDR] =
272 kstrdup(buf, GFP_KERNEL);
275 (void)rpc_ntop(sap, buf, sizeof(buf));
276 xprt->address_strings[RPC_DISPLAY_ADDR] =
277 kstrdup(buf, GFP_KERNEL);
278 sin = xs_addr_in(xprt);
279 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
282 (void)rpc_ntop(sap, buf, sizeof(buf));
283 xprt->address_strings[RPC_DISPLAY_ADDR] =
284 kstrdup(buf, GFP_KERNEL);
285 sin6 = xs_addr_in6(xprt);
286 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
292 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
295 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
297 struct sockaddr *sap = xs_addr(xprt);
300 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
301 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
303 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
304 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
307 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
308 const char *protocol,
311 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
312 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
313 xs_format_common_peer_addresses(xprt);
314 xs_format_common_peer_ports(xprt);
317 static void xs_update_peer_port(struct rpc_xprt *xprt)
319 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
320 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
322 xs_format_common_peer_ports(xprt);
325 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
329 for (i = 0; i < RPC_DISPLAY_MAX; i++)
331 case RPC_DISPLAY_PROTO:
332 case RPC_DISPLAY_NETID:
335 kfree(xprt->address_strings[i]);
340 xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
344 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
346 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
347 for (i = 0; i < n; i++) {
350 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
351 if (!buf->pages[i]) {
353 return i > buf->page_base ? i - buf->page_base : 0;
360 xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
361 struct cmsghdr *cmsg, int ret)
363 u8 content_type = tls_get_record_type(sock->sk, cmsg);
364 u8 level, description;
366 switch (content_type) {
369 case TLS_RECORD_TYPE_DATA:
370 /* TLS sets EOR at the end of each application data
371 * record, even though there might be more frames
372 * waiting to be decrypted.
374 msg->msg_flags &= ~MSG_EOR;
376 case TLS_RECORD_TYPE_ALERT:
377 tls_alert_recv(sock->sk, msg, &level, &description);
378 ret = (level == TLS_ALERT_LEVEL_FATAL) ?
382 /* discard this record type */
389 xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags)
393 u8 buf[CMSG_SPACE(sizeof(u8))];
397 msg->msg_control = &u;
398 msg->msg_controllen = sizeof(u);
399 ret = sock_recvmsg(sock, msg, flags);
400 if (msg->msg_controllen != sizeof(u))
401 ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret);
406 xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
410 iov_iter_advance(&msg->msg_iter, seek);
411 ret = xs_sock_recv_cmsg(sock, msg, flags);
412 return ret > 0 ? ret + seek : ret;
416 xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
417 struct kvec *kvec, size_t count, size_t seek)
419 iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
420 return xs_sock_recvmsg(sock, msg, flags, seek);
424 xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
425 struct bio_vec *bvec, unsigned long nr, size_t count,
428 iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
429 return xs_sock_recvmsg(sock, msg, flags, seek);
433 xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
436 iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
437 return xs_sock_recv_cmsg(sock, msg, flags);
440 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
442 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
444 struct bvec_iter bi = {
449 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
450 for_each_bvec(bv, bvec, bi, bi)
451 flush_dcache_page(bv.bv_page);
455 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
461 xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
462 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
464 size_t want, seek_init = seek, offset = 0;
467 want = min_t(size_t, count, buf->head[0].iov_len);
469 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
473 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
483 want = xs_alloc_sparse_pages(
484 buf, min_t(size_t, count - offset, buf->page_len),
485 GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
487 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
488 xdr_buf_pagecount(buf),
489 want + buf->page_base,
490 seek + buf->page_base);
493 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
494 ret -= buf->page_base;
496 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
506 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
508 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
512 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
516 } else if (offset < seek_init)
520 *read = offset - seek_init;
528 xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
530 if (!transport->recv.copied) {
531 if (buf->head[0].iov_len >= transport->recv.offset)
532 memcpy(buf->head[0].iov_base,
533 &transport->recv.xid,
534 transport->recv.offset);
535 transport->recv.copied = transport->recv.offset;
540 xs_read_stream_request_done(struct sock_xprt *transport)
542 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
546 xs_read_stream_check_eor(struct sock_xprt *transport,
549 if (xs_read_stream_request_done(transport))
550 msg->msg_flags |= MSG_EOR;
554 xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
555 int flags, struct rpc_rqst *req)
557 struct xdr_buf *buf = &req->rq_private_buf;
561 xs_read_header(transport, buf);
563 want = transport->recv.len - transport->recv.offset;
565 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
566 transport->recv.copied + want,
567 transport->recv.copied,
569 transport->recv.offset += read;
570 transport->recv.copied += read;
573 if (transport->recv.offset == transport->recv.len)
574 xs_read_stream_check_eor(transport, msg);
584 msg->msg_flags |= MSG_TRUNC;
589 return ret < 0 ? ret : read;
593 xs_read_stream_headersize(bool isfrag)
596 return sizeof(__be32);
597 return 3 * sizeof(__be32);
601 xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
602 int flags, size_t want, size_t seek)
605 .iov_base = &transport->recv.fraghdr,
608 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
611 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
613 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
615 struct rpc_xprt *xprt = &transport->xprt;
616 struct rpc_rqst *req;
619 /* Is this transport associated with the backchannel? */
623 /* Look up and lock the request corresponding to the given XID */
624 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
626 printk(KERN_WARNING "Callback slot table overflowed\n");
629 if (transport->recv.copied && !req->rq_private_buf.len)
632 ret = xs_read_stream_request(transport, msg, flags, req);
633 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
634 xprt_complete_bc_request(req, transport->recv.copied);
636 req->rq_private_buf.len = transport->recv.copied;
640 #else /* CONFIG_SUNRPC_BACKCHANNEL */
642 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
646 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
649 xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
651 struct rpc_xprt *xprt = &transport->xprt;
652 struct rpc_rqst *req;
655 /* Look up and lock the request corresponding to the given XID */
656 spin_lock(&xprt->queue_lock);
657 req = xprt_lookup_rqst(xprt, transport->recv.xid);
658 if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
659 msg->msg_flags |= MSG_TRUNC;
663 spin_unlock(&xprt->queue_lock);
665 ret = xs_read_stream_request(transport, msg, flags, req);
667 spin_lock(&xprt->queue_lock);
668 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
669 xprt_complete_rqst(req->rq_task, transport->recv.copied);
671 req->rq_private_buf.len = transport->recv.copied;
672 xprt_unpin_rqst(req);
674 spin_unlock(&xprt->queue_lock);
679 xs_read_stream(struct sock_xprt *transport, int flags)
681 struct msghdr msg = { 0 };
682 size_t want, read = 0;
685 if (transport->recv.len == 0) {
686 want = xs_read_stream_headersize(transport->recv.copied != 0);
687 ret = xs_read_stream_header(transport, &msg, flags, want,
688 transport->recv.offset);
691 transport->recv.offset = ret;
692 if (transport->recv.offset != want)
693 return transport->recv.offset;
694 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
695 RPC_FRAGMENT_SIZE_MASK;
696 transport->recv.offset -= sizeof(transport->recv.fraghdr);
700 switch (be32_to_cpu(transport->recv.calldir)) {
702 msg.msg_flags |= MSG_TRUNC;
705 ret = xs_read_stream_call(transport, &msg, flags);
708 ret = xs_read_stream_reply(transport, &msg, flags);
710 if (msg.msg_flags & MSG_TRUNC) {
711 transport->recv.calldir = cpu_to_be32(-1);
712 transport->recv.copied = -1;
717 if (transport->recv.offset < transport->recv.len) {
718 if (!(msg.msg_flags & MSG_TRUNC))
721 ret = xs_read_discard(transport->sock, &msg, flags,
722 transport->recv.len - transport->recv.offset);
725 transport->recv.offset += ret;
727 if (transport->recv.offset != transport->recv.len)
730 if (xs_read_stream_request_done(transport)) {
731 trace_xs_stream_read_request(transport);
732 transport->recv.copied = 0;
734 transport->recv.offset = 0;
735 transport->recv.len = 0;
738 return ret != 0 ? ret : -ESHUTDOWN;
741 static __poll_t xs_poll_socket(struct sock_xprt *transport)
743 return transport->sock->ops->poll(transport->file, transport->sock,
747 static bool xs_poll_socket_readable(struct sock_xprt *transport)
749 __poll_t events = xs_poll_socket(transport);
751 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
754 static void xs_poll_check_readable(struct sock_xprt *transport)
757 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
758 if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
760 if (!xs_poll_socket_readable(transport))
762 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
763 queue_work(xprtiod_workqueue, &transport->recv_worker);
766 static void xs_stream_data_receive(struct sock_xprt *transport)
771 mutex_lock(&transport->recv_mutex);
772 if (transport->sock == NULL)
775 ret = xs_read_stream(transport, MSG_DONTWAIT);
781 if (ret == -ESHUTDOWN)
782 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
783 else if (ret == -EACCES)
784 xprt_wake_pending_tasks(&transport->xprt, -EACCES);
786 xs_poll_check_readable(transport);
788 mutex_unlock(&transport->recv_mutex);
789 trace_xs_stream_read_data(&transport->xprt, ret, read);
792 static void xs_stream_data_receive_workfn(struct work_struct *work)
794 struct sock_xprt *transport =
795 container_of(work, struct sock_xprt, recv_worker);
796 unsigned int pflags = memalloc_nofs_save();
798 xs_stream_data_receive(transport);
799 memalloc_nofs_restore(pflags);
803 xs_stream_reset_connect(struct sock_xprt *transport)
805 transport->recv.offset = 0;
806 transport->recv.len = 0;
807 transport->recv.copied = 0;
808 transport->xmit.offset = 0;
812 xs_stream_start_connect(struct sock_xprt *transport)
814 transport->xprt.stat.connect_count++;
815 transport->xprt.stat.connect_start = jiffies;
818 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
821 * xs_nospace - handle transmit was incomplete
822 * @req: pointer to RPC request
823 * @transport: pointer to struct sock_xprt
826 static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
828 struct rpc_xprt *xprt = &transport->xprt;
829 struct sock *sk = transport->inet;
832 trace_rpc_socket_nospace(req, transport);
834 /* Protect against races with write_space */
835 spin_lock(&xprt->transport_lock);
837 /* Don't race with disconnect */
838 if (xprt_connected(xprt)) {
839 /* wait for more buffer space */
840 set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
841 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
842 sk->sk_write_pending++;
843 xprt_wait_for_buffer_space(xprt);
847 spin_unlock(&xprt->transport_lock);
851 static int xs_sock_nospace(struct rpc_rqst *req)
853 struct sock_xprt *transport =
854 container_of(req->rq_xprt, struct sock_xprt, xprt);
855 struct sock *sk = transport->inet;
859 if (!sock_writeable(sk))
860 ret = xs_nospace(req, transport);
865 static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
867 struct sock_xprt *transport =
868 container_of(req->rq_xprt, struct sock_xprt, xprt);
869 struct sock *sk = transport->inet;
875 if (!sk_stream_memory_free(sk))
876 ret = xs_nospace(req, transport);
881 static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
883 return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
887 * Determine if the previous message in the stream was aborted before it
888 * could complete transmission.
891 xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
893 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
897 * Return the stream record marker field for a record of length < 2^31-1
900 xs_stream_record_marker(struct xdr_buf *xdr)
904 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
908 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
909 * @req: pointer to RPC request
912 * 0: The request has been sent
913 * EAGAIN: The socket was blocked, please call again later to
914 * complete the request
915 * ENOTCONN: Caller needs to invoke connect logic then call again
916 * other: Some other error occurred, the request was not sent
918 static int xs_local_send_request(struct rpc_rqst *req)
920 struct rpc_xprt *xprt = req->rq_xprt;
921 struct sock_xprt *transport =
922 container_of(xprt, struct sock_xprt, xprt);
923 struct xdr_buf *xdr = &req->rq_snd_buf;
924 rpc_fraghdr rm = xs_stream_record_marker(xdr);
925 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
926 struct msghdr msg = {
927 .msg_flags = XS_SENDMSG_FLAGS,
933 /* Close the stream if the previous transmission was incomplete */
934 if (xs_send_request_was_aborted(transport, req)) {
935 xprt_force_disconnect(xprt);
939 xs_pktdump("packet data:",
940 req->rq_svec->iov_base, req->rq_svec->iov_len);
942 vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
944 req->rq_xtime = ktime_get();
945 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
946 transport->xmit.offset, rm, &sent);
947 dprintk("RPC: %s(%u) = %d\n",
948 __func__, xdr->len - transport->xmit.offset, status);
950 if (likely(sent > 0) || status == 0) {
951 transport->xmit.offset += sent;
952 req->rq_bytes_sent = transport->xmit.offset;
953 if (likely(req->rq_bytes_sent >= msglen)) {
954 req->rq_xmit_bytes_sent += transport->xmit.offset;
955 transport->xmit.offset = 0;
964 status = xs_stream_nospace(req, vm_wait);
967 dprintk("RPC: sendmsg returned unrecognized error %d\n",
971 xprt_force_disconnect(xprt);
979 * xs_udp_send_request - write an RPC request to a UDP socket
980 * @req: pointer to RPC request
983 * 0: The request has been sent
984 * EAGAIN: The socket was blocked, please call again later to
985 * complete the request
986 * ENOTCONN: Caller needs to invoke connect logic then call again
987 * other: Some other error occurred, the request was not sent
989 static int xs_udp_send_request(struct rpc_rqst *req)
991 struct rpc_xprt *xprt = req->rq_xprt;
992 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
993 struct xdr_buf *xdr = &req->rq_snd_buf;
994 struct msghdr msg = {
995 .msg_name = xs_addr(xprt),
996 .msg_namelen = xprt->addrlen,
997 .msg_flags = XS_SENDMSG_FLAGS,
1002 xs_pktdump("packet data:",
1003 req->rq_svec->iov_base,
1004 req->rq_svec->iov_len);
1006 if (!xprt_bound(xprt))
1009 if (!xprt_request_get_cong(xprt, req))
1012 status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
1015 req->rq_xtime = ktime_get();
1016 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
1018 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
1021 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1022 if (status == -EPERM)
1023 goto process_status;
1025 if (status == -EAGAIN && sock_writeable(transport->inet))
1028 if (sent > 0 || status == 0) {
1029 req->rq_xmit_bytes_sent += sent;
1030 if (sent >= req->rq_slen)
1032 /* Still some bytes left; set up for a retry later. */
1040 /* Should we call xs_close() here? */
1043 status = xs_sock_nospace(req);
1050 /* When the server has died, an ICMP port unreachable message
1051 * prompts ECONNREFUSED. */
1054 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1062 * xs_tcp_send_request - write an RPC request to a TCP socket
1063 * @req: pointer to RPC request
1066 * 0: The request has been sent
1067 * EAGAIN: The socket was blocked, please call again later to
1068 * complete the request
1069 * ENOTCONN: Caller needs to invoke connect logic then call again
1070 * other: Some other error occurred, the request was not sent
1072 * XXX: In the case of soft timeouts, should we eventually give up
1073 * if sendmsg is not able to make progress?
1075 static int xs_tcp_send_request(struct rpc_rqst *req)
1077 struct rpc_xprt *xprt = req->rq_xprt;
1078 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1079 struct xdr_buf *xdr = &req->rq_snd_buf;
1080 rpc_fraghdr rm = xs_stream_record_marker(xdr);
1081 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
1082 struct msghdr msg = {
1083 .msg_flags = XS_SENDMSG_FLAGS,
1089 /* Close the stream if the previous transmission was incomplete */
1090 if (xs_send_request_was_aborted(transport, req)) {
1091 if (transport->sock != NULL)
1092 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1095 if (!transport->inet)
1098 xs_pktdump("packet data:",
1099 req->rq_svec->iov_base,
1100 req->rq_svec->iov_len);
1102 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1103 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1105 xs_set_srcport(transport, transport->sock);
1107 /* Continue transmitting the packet/record. We must be careful
1108 * to cope with writespace callbacks arriving _after_ we have
1109 * called sendmsg(). */
1110 req->rq_xtime = ktime_get();
1111 tcp_sock_set_cork(transport->inet, true);
1113 vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1116 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
1117 transport->xmit.offset, rm, &sent);
1119 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
1120 xdr->len - transport->xmit.offset, status);
1122 /* If we've sent the entire packet, immediately
1123 * reset the count of bytes sent. */
1124 transport->xmit.offset += sent;
1125 req->rq_bytes_sent = transport->xmit.offset;
1126 if (likely(req->rq_bytes_sent >= msglen)) {
1127 req->rq_xmit_bytes_sent += transport->xmit.offset;
1128 transport->xmit.offset = 0;
1129 if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1130 tcp_sock_set_cork(transport->inet, false);
1134 WARN_ON_ONCE(sent == 0 && status == 0);
1139 } while (status == 0);
1144 /* Should we call xs_close() here? */
1147 status = xs_stream_nospace(req, vm_wait);
1157 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1164 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1166 transport->old_data_ready = sk->sk_data_ready;
1167 transport->old_state_change = sk->sk_state_change;
1168 transport->old_write_space = sk->sk_write_space;
1169 transport->old_error_report = sk->sk_error_report;
1172 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1174 sk->sk_data_ready = transport->old_data_ready;
1175 sk->sk_state_change = transport->old_state_change;
1176 sk->sk_write_space = transport->old_write_space;
1177 sk->sk_error_report = transport->old_error_report;
1180 static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1182 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1184 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1185 clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1186 clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1187 clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1188 clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
1191 static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1193 set_bit(nr, &transport->sock_state);
1194 queue_work(xprtiod_workqueue, &transport->error_worker);
1197 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1199 xprt->connect_cookie++;
1200 smp_mb__before_atomic();
1201 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1202 clear_bit(XPRT_CLOSING, &xprt->state);
1203 xs_sock_reset_state_flags(xprt);
1204 smp_mb__after_atomic();
1208 * xs_error_report - callback to handle TCP socket state errors
1211 * Note: we don't call sock_error() since there may be a rpc_task
1212 * using the socket, and so we don't want to clear sk->sk_err.
1214 static void xs_error_report(struct sock *sk)
1216 struct sock_xprt *transport;
1217 struct rpc_xprt *xprt;
1219 if (!(xprt = xprt_from_sock(sk)))
1222 transport = container_of(xprt, struct sock_xprt, xprt);
1223 transport->xprt_err = -sk->sk_err;
1224 if (transport->xprt_err == 0)
1226 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1227 xprt, -transport->xprt_err);
1228 trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1230 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1231 smp_mb__before_atomic();
1232 xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
1235 static void xs_reset_transport(struct sock_xprt *transport)
1237 struct socket *sock = transport->sock;
1238 struct sock *sk = transport->inet;
1239 struct rpc_xprt *xprt = &transport->xprt;
1240 struct file *filp = transport->file;
1245 * Make sure we're calling this in a context from which it is safe
1246 * to call __fput_sync(). In practice that means rpciod and the
1249 if (!(current->flags & PF_WQ_WORKER)) {
1251 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1255 if (atomic_read(&transport->xprt.swapper))
1256 sk_clear_memalloc(sk);
1258 tls_handshake_cancel(sk);
1260 kernel_sock_shutdown(sock, SHUT_RDWR);
1262 mutex_lock(&transport->recv_mutex);
1264 transport->inet = NULL;
1265 transport->sock = NULL;
1266 transport->file = NULL;
1268 sk->sk_user_data = NULL;
1270 xs_restore_old_callbacks(transport, sk);
1271 xprt_clear_connected(xprt);
1272 xs_sock_reset_connection_flags(xprt);
1273 /* Reset stream record info */
1274 xs_stream_reset_connect(transport);
1276 mutex_unlock(&transport->recv_mutex);
1278 trace_rpc_socket_close(xprt, sock);
1281 xprt_disconnect_done(xprt);
1285 * xs_close - close a socket
1288 * This is used when all requests are complete; ie, no DRC state remains
1289 * on the server we want to save.
1291 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1292 * xs_reset_transport() zeroing the socket from underneath a writer.
1294 static void xs_close(struct rpc_xprt *xprt)
1296 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1298 dprintk("RPC: xs_close xprt %p\n", xprt);
1300 if (transport->sock)
1301 tls_handshake_close(transport->sock);
1302 xs_reset_transport(transport);
1303 xprt->reestablish_timeout = 0;
1306 static void xs_inject_disconnect(struct rpc_xprt *xprt)
1308 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1310 xprt_disconnect_done(xprt);
1313 static void xs_xprt_free(struct rpc_xprt *xprt)
1315 xs_free_peer_addresses(xprt);
1320 * xs_destroy - prepare to shutdown a transport
1321 * @xprt: doomed transport
1324 static void xs_destroy(struct rpc_xprt *xprt)
1326 struct sock_xprt *transport = container_of(xprt,
1327 struct sock_xprt, xprt);
1328 dprintk("RPC: xs_destroy xprt %p\n", xprt);
1330 cancel_delayed_work_sync(&transport->connect_worker);
1332 cancel_work_sync(&transport->recv_worker);
1333 cancel_work_sync(&transport->error_worker);
1335 module_put(THIS_MODULE);
1339 * xs_udp_data_read_skb - receive callback for UDP sockets
1345 static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1347 struct sk_buff *skb)
1349 struct rpc_task *task;
1350 struct rpc_rqst *rovr;
1351 int repsize, copied;
1357 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
1361 /* Copy the XID from the skb... */
1362 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1366 /* Look up and lock the request corresponding to the given XID */
1367 spin_lock(&xprt->queue_lock);
1368 rovr = xprt_lookup_rqst(xprt, *xp);
1371 xprt_pin_rqst(rovr);
1372 xprt_update_rtt(rovr->rq_task);
1373 spin_unlock(&xprt->queue_lock);
1374 task = rovr->rq_task;
1376 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1379 /* Suck it into the iovec, verify checksum if not done by hw. */
1380 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1381 spin_lock(&xprt->queue_lock);
1382 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1387 spin_lock(&xprt->transport_lock);
1388 xprt_adjust_cwnd(xprt, task, copied);
1389 spin_unlock(&xprt->transport_lock);
1390 spin_lock(&xprt->queue_lock);
1391 xprt_complete_rqst(task, copied);
1392 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1394 xprt_unpin_rqst(rovr);
1396 spin_unlock(&xprt->queue_lock);
1399 static void xs_udp_data_receive(struct sock_xprt *transport)
1401 struct sk_buff *skb;
1405 mutex_lock(&transport->recv_mutex);
1406 sk = transport->inet;
1410 skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
1413 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1417 xs_poll_check_readable(transport);
1419 mutex_unlock(&transport->recv_mutex);
1422 static void xs_udp_data_receive_workfn(struct work_struct *work)
1424 struct sock_xprt *transport =
1425 container_of(work, struct sock_xprt, recv_worker);
1426 unsigned int pflags = memalloc_nofs_save();
1428 xs_udp_data_receive(transport);
1429 memalloc_nofs_restore(pflags);
1433 * xs_data_ready - "data ready" callback for sockets
1434 * @sk: socket with data to read
1437 static void xs_data_ready(struct sock *sk)
1439 struct rpc_xprt *xprt;
1441 trace_sk_data_ready(sk);
1443 xprt = xprt_from_sock(sk);
1445 struct sock_xprt *transport = container_of(xprt,
1446 struct sock_xprt, xprt);
1448 trace_xs_data_ready(xprt);
1450 transport->old_data_ready(sk);
1452 if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
1455 /* Any data means we had a useful conversation, so
1456 * then we don't need to delay the next reconnect
1458 if (xprt->reestablish_timeout)
1459 xprt->reestablish_timeout = 0;
1460 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1461 queue_work(xprtiod_workqueue, &transport->recv_worker);
1466 * Helper function to force a TCP close if the server is sending
1467 * junk and/or it has put us in CLOSE_WAIT
1469 static void xs_tcp_force_close(struct rpc_xprt *xprt)
1471 xprt_force_disconnect(xprt);
1474 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1475 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1479 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1482 * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1483 * @sk: socket whose state has changed
1486 static void xs_local_state_change(struct sock *sk)
1488 struct rpc_xprt *xprt;
1489 struct sock_xprt *transport;
1491 if (!(xprt = xprt_from_sock(sk)))
1493 transport = container_of(xprt, struct sock_xprt, xprt);
1494 if (sk->sk_shutdown & SHUTDOWN_MASK) {
1495 clear_bit(XPRT_CONNECTED, &xprt->state);
1496 /* Trigger the socket release */
1497 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1502 * xs_tcp_state_change - callback to handle TCP socket state changes
1503 * @sk: socket whose state has changed
1506 static void xs_tcp_state_change(struct sock *sk)
1508 struct rpc_xprt *xprt;
1509 struct sock_xprt *transport;
1511 if (!(xprt = xprt_from_sock(sk)))
1513 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1514 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1515 sk->sk_state, xprt_connected(xprt),
1516 sock_flag(sk, SOCK_DEAD),
1517 sock_flag(sk, SOCK_ZAPPED),
1520 transport = container_of(xprt, struct sock_xprt, xprt);
1521 trace_rpc_socket_state_change(xprt, sk->sk_socket);
1522 switch (sk->sk_state) {
1523 case TCP_ESTABLISHED:
1524 if (!xprt_test_and_set_connected(xprt)) {
1525 xprt->connect_cookie++;
1526 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1527 xprt_clear_connecting(xprt);
1529 xprt->stat.connect_count++;
1530 xprt->stat.connect_time += (long)jiffies -
1531 xprt->stat.connect_start;
1532 xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1536 /* The client initiated a shutdown of the socket */
1537 xprt->connect_cookie++;
1538 xprt->reestablish_timeout = 0;
1539 set_bit(XPRT_CLOSING, &xprt->state);
1540 smp_mb__before_atomic();
1541 clear_bit(XPRT_CONNECTED, &xprt->state);
1542 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1543 smp_mb__after_atomic();
1545 case TCP_CLOSE_WAIT:
1546 /* The server initiated a shutdown of the socket */
1547 xprt->connect_cookie++;
1548 clear_bit(XPRT_CONNECTED, &xprt->state);
1549 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1553 * If the server closed down the connection, make sure that
1554 * we back off before reconnecting
1556 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1557 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1560 set_bit(XPRT_CLOSING, &xprt->state);
1561 smp_mb__before_atomic();
1562 clear_bit(XPRT_CONNECTED, &xprt->state);
1563 smp_mb__after_atomic();
1566 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1567 &transport->sock_state))
1568 xprt_clear_connecting(xprt);
1569 clear_bit(XPRT_CLOSING, &xprt->state);
1570 /* Trigger the socket release */
1571 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1575 static void xs_write_space(struct sock *sk)
1577 struct sock_xprt *transport;
1578 struct rpc_xprt *xprt;
1582 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1584 if (unlikely(!(xprt = xprt_from_sock(sk))))
1586 transport = container_of(xprt, struct sock_xprt, xprt);
1587 if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
1589 xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1590 sk->sk_write_pending--;
1594 * xs_udp_write_space - callback invoked when socket buffer space
1596 * @sk: socket whose state has changed
1598 * Called when more output buffer space is available for this socket.
1599 * We try not to wake our writers until they can make "significant"
1600 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1601 * with a bunch of small requests.
1603 static void xs_udp_write_space(struct sock *sk)
1605 /* from net/core/sock.c:sock_def_write_space */
1606 if (sock_writeable(sk))
1611 * xs_tcp_write_space - callback invoked when socket buffer space
1613 * @sk: socket whose state has changed
1615 * Called when more output buffer space is available for this socket.
1616 * We try not to wake our writers until they can make "significant"
1617 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1618 * with a bunch of small requests.
1620 static void xs_tcp_write_space(struct sock *sk)
1622 /* from net/core/stream.c:sk_stream_write_space */
1623 if (sk_stream_is_writeable(sk))
1627 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1629 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1630 struct sock *sk = transport->inet;
1632 if (transport->rcvsize) {
1633 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1634 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1636 if (transport->sndsize) {
1637 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1638 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1639 sk->sk_write_space(sk);
1644 * xs_udp_set_buffer_size - set send and receive limits
1645 * @xprt: generic transport
1646 * @sndsize: requested size of send buffer, in bytes
1647 * @rcvsize: requested size of receive buffer, in bytes
1649 * Set socket send and receive buffer size limits.
1651 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1653 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1655 transport->sndsize = 0;
1657 transport->sndsize = sndsize + 1024;
1658 transport->rcvsize = 0;
1660 transport->rcvsize = rcvsize + 1024;
1662 xs_udp_do_set_buffer_size(xprt);
1666 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1667 * @xprt: controlling transport
1668 * @task: task that timed out
1670 * Adjust the congestion window after a retransmit timeout has occurred.
1672 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1674 spin_lock(&xprt->transport_lock);
1675 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1676 spin_unlock(&xprt->transport_lock);
1679 static int xs_get_random_port(void)
1681 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1682 unsigned short range;
1683 unsigned short rand;
1687 range = max - min + 1;
1688 rand = get_random_u32_below(range);
1692 static unsigned short xs_sock_getport(struct socket *sock)
1694 struct sockaddr_storage buf;
1695 unsigned short port = 0;
1697 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
1699 switch (buf.ss_family) {
1701 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1704 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1711 * xs_set_port - reset the port number in the remote endpoint address
1712 * @xprt: generic transport
1713 * @port: new port number
1716 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1718 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1720 rpc_set_port(xs_addr(xprt), port);
1721 xs_update_peer_port(xprt);
1724 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1726 if (transport->srcport == 0 && transport->xprt.reuseport)
1727 transport->srcport = xs_sock_getport(sock);
1730 static int xs_get_srcport(struct sock_xprt *transport)
1732 int port = transport->srcport;
1734 if (port == 0 && transport->xprt.resvport)
1735 port = xs_get_random_port();
1739 static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1741 struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1742 unsigned short ret = 0;
1743 mutex_lock(&sock->recv_mutex);
1745 ret = xs_sock_getport(sock->sock);
1746 mutex_unlock(&sock->recv_mutex);
1750 static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1752 struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1755 struct sockaddr_storage st;
1757 int ret = -ENOTCONN;
1759 mutex_lock(&sock->recv_mutex);
1761 ret = kernel_getsockname(sock->sock, &saddr.sa);
1763 ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1765 mutex_unlock(&sock->recv_mutex);
1769 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1771 if (transport->srcport != 0)
1772 transport->srcport = 0;
1773 if (!transport->xprt.resvport)
1775 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1776 return xprt_max_resvport;
1779 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1781 struct sockaddr_storage myaddr;
1783 int port = xs_get_srcport(transport);
1784 unsigned short last;
1787 * If we are asking for any ephemeral port (i.e. port == 0 &&
1788 * transport->xprt.resvport == 0), don't bind. Let the local
1789 * port selection happen implicitly when the socket is used
1790 * (for example at connect time).
1792 * This ensures that we can continue to establish TCP
1793 * connections even when all local ephemeral ports are already
1794 * a part of some TCP connection. This makes no difference
1795 * for UDP sockets, but also doesn't harm them.
1797 * If we're asking for any reserved port (i.e. port == 0 &&
1798 * transport->xprt.resvport == 1) xs_get_srcport above will
1799 * ensure that port is non-zero and we will bind as needed.
1804 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1806 rpc_set_port((struct sockaddr *)&myaddr, port);
1807 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1808 transport->xprt.addrlen);
1810 if (transport->xprt.reuseport)
1811 transport->srcport = port;
1815 port = xs_next_srcport(transport, port);
1818 } while (err == -EADDRINUSE && nloop != 2);
1820 if (myaddr.ss_family == AF_INET)
1821 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1822 &((struct sockaddr_in *)&myaddr)->sin_addr,
1823 port, err ? "failed" : "ok", err);
1825 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1826 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1827 port, err ? "failed" : "ok", err);
1832 * We don't support autobind on AF_LOCAL sockets
1834 static void xs_local_rpcbind(struct rpc_task *task)
1836 xprt_set_bound(task->tk_xprt);
1839 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1843 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1844 static struct lock_class_key xs_key[3];
1845 static struct lock_class_key xs_slock_key[3];
1847 static inline void xs_reclassify_socketu(struct socket *sock)
1849 struct sock *sk = sock->sk;
1851 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1852 &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1855 static inline void xs_reclassify_socket4(struct socket *sock)
1857 struct sock *sk = sock->sk;
1859 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1860 &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1863 static inline void xs_reclassify_socket6(struct socket *sock)
1865 struct sock *sk = sock->sk;
1867 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1868 &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1871 static inline void xs_reclassify_socket(int family, struct socket *sock)
1873 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
1878 xs_reclassify_socketu(sock);
1881 xs_reclassify_socket4(sock);
1884 xs_reclassify_socket6(sock);
1889 static inline void xs_reclassify_socket(int family, struct socket *sock)
1894 static void xs_dummy_setup_socket(struct work_struct *work)
1898 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1899 struct sock_xprt *transport, int family, int type,
1900 int protocol, bool reuseport)
1903 struct socket *sock;
1906 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1908 dprintk("RPC: can't create %d transport socket (%d).\n",
1912 xs_reclassify_socket(family, sock);
1915 sock_set_reuseport(sock->sk);
1917 err = xs_bind(transport, sock);
1923 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1925 return ERR_CAST(filp);
1926 transport->file = filp;
1930 return ERR_PTR(err);
1933 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1934 struct socket *sock)
1936 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1939 if (!transport->inet) {
1940 struct sock *sk = sock->sk;
1944 xs_save_old_callbacks(transport, sk);
1946 sk->sk_user_data = xprt;
1947 sk->sk_data_ready = xs_data_ready;
1948 sk->sk_write_space = xs_udp_write_space;
1949 sk->sk_state_change = xs_local_state_change;
1950 sk->sk_error_report = xs_error_report;
1951 sk->sk_use_task_frag = false;
1953 xprt_clear_connected(xprt);
1955 /* Reset to new socket */
1956 transport->sock = sock;
1957 transport->inet = sk;
1962 xs_stream_start_connect(transport);
1964 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1968 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1969 * @transport: socket transport to connect
1971 static int xs_local_setup_socket(struct sock_xprt *transport)
1973 struct rpc_xprt *xprt = &transport->xprt;
1975 struct socket *sock;
1978 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1979 SOCK_STREAM, 0, &sock, 1);
1981 dprintk("RPC: can't create AF_LOCAL "
1982 "transport socket (%d).\n", -status);
1985 xs_reclassify_socket(AF_LOCAL, sock);
1987 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1989 status = PTR_ERR(filp);
1992 transport->file = filp;
1994 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1995 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1997 status = xs_local_finish_connecting(xprt, sock);
1998 trace_rpc_socket_connect(xprt, sock, status);
2001 dprintk("RPC: xprt %p connected to %s\n",
2002 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2003 xprt->stat.connect_count++;
2004 xprt->stat.connect_time += (long)jiffies -
2005 xprt->stat.connect_start;
2006 xprt_set_connected(xprt);
2011 dprintk("RPC: xprt %p: socket %s does not exist\n",
2012 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2015 dprintk("RPC: xprt %p: connection refused for %s\n",
2016 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2019 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2021 xprt->address_strings[RPC_DISPLAY_ADDR]);
2025 xprt_clear_connecting(xprt);
2026 xprt_wake_pending_tasks(xprt, status);
2030 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2032 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2035 if (transport->file)
2036 goto force_disconnect;
2038 if (RPC_IS_ASYNC(task)) {
2040 * We want the AF_LOCAL connect to be resolved in the
2041 * filesystem namespace of the process making the rpc
2042 * call. Thus we connect synchronously.
2044 * If we want to support asynchronous AF_LOCAL calls,
2045 * we'll need to figure out how to pass a namespace to
2048 rpc_task_set_rpc_status(task, -ENOTCONN);
2051 ret = xs_local_setup_socket(transport);
2052 if (ret && !RPC_IS_SOFTCONN(task))
2053 msleep_interruptible(15000);
2056 xprt_force_disconnect(xprt);
2058 xprt_clear_connecting(xprt);
2059 xprt_wake_pending_tasks(xprt, -ENOTCONN);
2062 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2064 * Note that this should be called with XPRT_LOCKED held, or recv_mutex
2065 * held, or when we otherwise know that we have exclusive access to the
2066 * socket, to guard against races with xs_reset_transport.
2068 static void xs_set_memalloc(struct rpc_xprt *xprt)
2070 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2074 * If there's no sock, then we have nothing to set. The
2075 * reconnecting process will get it for us.
2077 if (!transport->inet)
2079 if (atomic_read(&xprt->swapper))
2080 sk_set_memalloc(transport->inet);
2084 * xs_enable_swap - Tag this transport as being used for swap.
2085 * @xprt: transport to tag
2087 * Take a reference to this transport on behalf of the rpc_clnt, and
2088 * optionally mark it for swapping if it wasn't already.
2091 xs_enable_swap(struct rpc_xprt *xprt)
2093 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2095 mutex_lock(&xs->recv_mutex);
2096 if (atomic_inc_return(&xprt->swapper) == 1 &&
2098 sk_set_memalloc(xs->inet);
2099 mutex_unlock(&xs->recv_mutex);
2104 * xs_disable_swap - Untag this transport as being used for swap.
2105 * @xprt: transport to tag
2107 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2108 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2111 xs_disable_swap(struct rpc_xprt *xprt)
2113 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2115 mutex_lock(&xs->recv_mutex);
2116 if (atomic_dec_and_test(&xprt->swapper) &&
2118 sk_clear_memalloc(xs->inet);
2119 mutex_unlock(&xs->recv_mutex);
2122 static void xs_set_memalloc(struct rpc_xprt *xprt)
2127 xs_enable_swap(struct rpc_xprt *xprt)
2133 xs_disable_swap(struct rpc_xprt *xprt)
2138 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2140 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2142 if (!transport->inet) {
2143 struct sock *sk = sock->sk;
2147 xs_save_old_callbacks(transport, sk);
2149 sk->sk_user_data = xprt;
2150 sk->sk_data_ready = xs_data_ready;
2151 sk->sk_write_space = xs_udp_write_space;
2152 sk->sk_use_task_frag = false;
2154 xprt_set_connected(xprt);
2156 /* Reset to new socket */
2157 transport->sock = sock;
2158 transport->inet = sk;
2160 xs_set_memalloc(xprt);
2164 xs_udp_do_set_buffer_size(xprt);
2166 xprt->stat.connect_start = jiffies;
2169 static void xs_udp_setup_socket(struct work_struct *work)
2171 struct sock_xprt *transport =
2172 container_of(work, struct sock_xprt, connect_worker.work);
2173 struct rpc_xprt *xprt = &transport->xprt;
2174 struct socket *sock;
2176 unsigned int pflags = current->flags;
2178 if (atomic_read(&xprt->swapper))
2179 current->flags |= PF_MEMALLOC;
2180 sock = xs_create_sock(xprt, transport,
2181 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2182 IPPROTO_UDP, false);
2186 dprintk("RPC: worker connecting xprt %p via %s to "
2187 "%s (port %s)\n", xprt,
2188 xprt->address_strings[RPC_DISPLAY_PROTO],
2189 xprt->address_strings[RPC_DISPLAY_ADDR],
2190 xprt->address_strings[RPC_DISPLAY_PORT]);
2192 xs_udp_finish_connecting(xprt, sock);
2193 trace_rpc_socket_connect(xprt, sock, 0);
2196 xprt_clear_connecting(xprt);
2197 xprt_unlock_connect(xprt, transport);
2198 xprt_wake_pending_tasks(xprt, status);
2199 current_restore_flags(pflags, PF_MEMALLOC);
2203 * xs_tcp_shutdown - gracefully shut down a TCP socket
2206 * Initiates a graceful shutdown of the TCP socket by calling the
2207 * equivalent of shutdown(SHUT_RDWR);
2209 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2211 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2212 struct socket *sock = transport->sock;
2213 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
2217 if (!xprt->reuseport) {
2226 case TCP_ESTABLISHED:
2227 case TCP_CLOSE_WAIT:
2228 kernel_sock_shutdown(sock, SHUT_RDWR);
2229 trace_rpc_socket_shutdown(xprt, sock);
2232 xs_reset_transport(transport);
2236 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2237 struct socket *sock)
2239 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2240 unsigned int keepidle;
2241 unsigned int keepcnt;
2244 spin_lock(&xprt->transport_lock);
2245 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2246 keepcnt = xprt->timeout->to_retries + 1;
2247 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2248 (xprt->timeout->to_retries + 1);
2249 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2250 spin_unlock(&xprt->transport_lock);
2252 /* TCP Keepalive options */
2253 sock_set_keepalive(sock->sk);
2254 tcp_sock_set_keepidle(sock->sk, keepidle);
2255 tcp_sock_set_keepintvl(sock->sk, keepidle);
2256 tcp_sock_set_keepcnt(sock->sk, keepcnt);
2258 /* TCP user timeout (see RFC5482) */
2259 tcp_sock_set_user_timeout(sock->sk, timeo);
2262 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2263 unsigned long connect_timeout,
2264 unsigned long reconnect_timeout)
2266 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2267 struct rpc_timeout to;
2268 unsigned long initval;
2270 spin_lock(&xprt->transport_lock);
2271 if (reconnect_timeout < xprt->max_reconnect_timeout)
2272 xprt->max_reconnect_timeout = reconnect_timeout;
2273 if (connect_timeout < xprt->connect_timeout) {
2274 memcpy(&to, xprt->timeout, sizeof(to));
2275 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2276 /* Arbitrary lower limit */
2277 if (initval < XS_TCP_INIT_REEST_TO << 1)
2278 initval = XS_TCP_INIT_REEST_TO << 1;
2279 to.to_initval = initval;
2280 to.to_maxval = initval;
2281 memcpy(&transport->tcp_timeout, &to,
2282 sizeof(transport->tcp_timeout));
2283 xprt->timeout = &transport->tcp_timeout;
2284 xprt->connect_timeout = connect_timeout;
2286 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2287 spin_unlock(&xprt->transport_lock);
2290 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2292 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2294 if (!transport->inet) {
2295 struct sock *sk = sock->sk;
2297 /* Avoid temporary address, they are bad for long-lived
2298 * connections such as NFS mounts.
2299 * RFC4941, section 3.6 suggests that:
2300 * Individual applications, which have specific
2301 * knowledge about the normal duration of connections,
2302 * MAY override this as appropriate.
2304 if (xs_addr(xprt)->sa_family == PF_INET6) {
2305 ip6_sock_set_addr_preferences(sk,
2306 IPV6_PREFER_SRC_PUBLIC);
2309 xs_tcp_set_socket_timeouts(xprt, sock);
2310 tcp_sock_set_nodelay(sk);
2314 xs_save_old_callbacks(transport, sk);
2316 sk->sk_user_data = xprt;
2317 sk->sk_data_ready = xs_data_ready;
2318 sk->sk_state_change = xs_tcp_state_change;
2319 sk->sk_write_space = xs_tcp_write_space;
2320 sk->sk_error_report = xs_error_report;
2321 sk->sk_use_task_frag = false;
2323 /* socket options */
2324 sock_reset_flag(sk, SOCK_LINGER);
2326 xprt_clear_connected(xprt);
2328 /* Reset to new socket */
2329 transport->sock = sock;
2330 transport->inet = sk;
2335 if (!xprt_bound(xprt))
2338 xs_set_memalloc(xprt);
2340 xs_stream_start_connect(transport);
2342 /* Tell the socket layer to start connecting... */
2343 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2344 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2348 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2349 * @work: queued work item
2351 * Invoked by a work queue tasklet.
2353 static void xs_tcp_setup_socket(struct work_struct *work)
2355 struct sock_xprt *transport =
2356 container_of(work, struct sock_xprt, connect_worker.work);
2357 struct socket *sock = transport->sock;
2358 struct rpc_xprt *xprt = &transport->xprt;
2360 unsigned int pflags = current->flags;
2362 if (atomic_read(&xprt->swapper))
2363 current->flags |= PF_MEMALLOC;
2365 if (xprt_connected(xprt))
2367 if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
2368 &transport->sock_state) ||
2370 xs_reset_transport(transport);
2371 sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
2372 SOCK_STREAM, IPPROTO_TCP, true);
2374 xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
2379 dprintk("RPC: worker connecting xprt %p via %s to "
2380 "%s (port %s)\n", xprt,
2381 xprt->address_strings[RPC_DISPLAY_PROTO],
2382 xprt->address_strings[RPC_DISPLAY_ADDR],
2383 xprt->address_strings[RPC_DISPLAY_PORT]);
2385 status = xs_tcp_finish_connecting(xprt, sock);
2386 trace_rpc_socket_connect(xprt, sock, status);
2387 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2388 xprt, -status, xprt_connected(xprt),
2389 sock->sk->sk_state);
2394 set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2395 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2396 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2400 case -EADDRNOTAVAIL:
2401 /* Source port number is unavailable. Try a new one! */
2402 transport->srcport = 0;
2406 /* Happens, for instance, if the user specified a link
2407 * local IPv6 address without a scope-id.
2418 printk("%s: connect returned unhandled error %d\n",
2423 /* xs_tcp_force_close() wakes tasks with a fixed error code.
2424 * We need to wake them first to ensure the correct error code.
2426 xprt_wake_pending_tasks(xprt, status);
2427 xs_tcp_force_close(xprt);
2429 xprt_clear_connecting(xprt);
2431 xprt_unlock_connect(xprt, transport);
2432 current_restore_flags(pflags, PF_MEMALLOC);
2436 * Transfer the connected socket to @upper_transport, then mark that
2439 static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt,
2440 struct sock_xprt *upper_transport)
2442 struct sock_xprt *lower_transport =
2443 container_of(lower_xprt, struct sock_xprt, xprt);
2444 struct rpc_xprt *upper_xprt = &upper_transport->xprt;
2446 if (!upper_transport->inet) {
2447 struct socket *sock = lower_transport->sock;
2448 struct sock *sk = sock->sk;
2450 /* Avoid temporary address, they are bad for long-lived
2451 * connections such as NFS mounts.
2452 * RFC4941, section 3.6 suggests that:
2453 * Individual applications, which have specific
2454 * knowledge about the normal duration of connections,
2455 * MAY override this as appropriate.
2457 if (xs_addr(upper_xprt)->sa_family == PF_INET6)
2458 ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC);
2460 xs_tcp_set_socket_timeouts(upper_xprt, sock);
2461 tcp_sock_set_nodelay(sk);
2465 /* @sk is already connected, so it now has the RPC callbacks.
2466 * Reach into @lower_transport to save the original ones.
2468 upper_transport->old_data_ready = lower_transport->old_data_ready;
2469 upper_transport->old_state_change = lower_transport->old_state_change;
2470 upper_transport->old_write_space = lower_transport->old_write_space;
2471 upper_transport->old_error_report = lower_transport->old_error_report;
2472 sk->sk_user_data = upper_xprt;
2474 /* socket options */
2475 sock_reset_flag(sk, SOCK_LINGER);
2477 xprt_clear_connected(upper_xprt);
2479 upper_transport->sock = sock;
2480 upper_transport->inet = sk;
2481 upper_transport->file = lower_transport->file;
2485 /* Reset lower_transport before shutting down its clnt */
2486 mutex_lock(&lower_transport->recv_mutex);
2487 lower_transport->inet = NULL;
2488 lower_transport->sock = NULL;
2489 lower_transport->file = NULL;
2491 xprt_clear_connected(lower_xprt);
2492 xs_sock_reset_connection_flags(lower_xprt);
2493 xs_stream_reset_connect(lower_transport);
2494 mutex_unlock(&lower_transport->recv_mutex);
2497 if (!xprt_bound(upper_xprt))
2500 xs_set_memalloc(upper_xprt);
2502 if (!xprt_test_and_set_connected(upper_xprt)) {
2503 upper_xprt->connect_cookie++;
2504 clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
2505 xprt_clear_connecting(upper_xprt);
2507 upper_xprt->stat.connect_count++;
2508 upper_xprt->stat.connect_time += (long)jiffies -
2509 upper_xprt->stat.connect_start;
2510 xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
2516 * xs_tls_handshake_done - TLS handshake completion handler
2517 * @data: address of xprt to wake
2518 * @status: status of handshake
2519 * @peerid: serial number of key containing the remote's identity
2522 static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid)
2524 struct rpc_xprt *lower_xprt = data;
2525 struct sock_xprt *lower_transport =
2526 container_of(lower_xprt, struct sock_xprt, xprt);
2528 lower_transport->xprt_err = status ? -EACCES : 0;
2529 complete(&lower_transport->handshake_done);
2530 xprt_put(lower_xprt);
2533 static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec)
2535 struct sock_xprt *lower_transport =
2536 container_of(lower_xprt, struct sock_xprt, xprt);
2537 struct tls_handshake_args args = {
2538 .ta_sock = lower_transport->sock,
2539 .ta_done = xs_tls_handshake_done,
2540 .ta_data = xprt_get(lower_xprt),
2541 .ta_peername = lower_xprt->servername,
2543 struct sock *sk = lower_transport->inet;
2546 init_completion(&lower_transport->handshake_done);
2547 set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state);
2548 lower_transport->xprt_err = -ETIMEDOUT;
2549 switch (xprtsec->policy) {
2550 case RPC_XPRTSEC_TLS_ANON:
2551 rc = tls_client_hello_anon(&args, GFP_KERNEL);
2555 case RPC_XPRTSEC_TLS_X509:
2556 args.ta_my_cert = xprtsec->cert_serial;
2557 args.ta_my_privkey = xprtsec->privkey_serial;
2558 rc = tls_client_hello_x509(&args, GFP_KERNEL);
2567 rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done,
2568 XS_TLS_HANDSHAKE_TO);
2570 if (!tls_handshake_cancel(sk)) {
2577 rc = lower_transport->xprt_err;
2580 xs_stream_reset_connect(lower_transport);
2581 clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state);
2585 xprt_put(lower_xprt);
2590 * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket
2591 * @work: queued work item
2593 * Invoked by a work queue tasklet.
2595 * For RPC-with-TLS, there is a two-stage connection process.
2597 * The "upper-layer xprt" is visible to the RPC consumer. Once it has
2598 * been marked connected, the consumer knows that a TCP connection and
2599 * a TLS session have been established.
2601 * A "lower-layer xprt", created in this function, handles the mechanics
2602 * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and
2603 * then driving the TLS handshake. Once all that is complete, the upper
2604 * layer xprt is marked connected.
2606 static void xs_tcp_tls_setup_socket(struct work_struct *work)
2608 struct sock_xprt *upper_transport =
2609 container_of(work, struct sock_xprt, connect_worker.work);
2610 struct rpc_clnt *upper_clnt = upper_transport->clnt;
2611 struct rpc_xprt *upper_xprt = &upper_transport->xprt;
2612 struct rpc_create_args args = {
2613 .net = upper_xprt->xprt_net,
2614 .protocol = upper_xprt->prot,
2615 .address = (struct sockaddr *)&upper_xprt->addr,
2616 .addrsize = upper_xprt->addrlen,
2617 .timeout = upper_clnt->cl_timeout,
2618 .servername = upper_xprt->servername,
2619 .program = upper_clnt->cl_program,
2620 .prognumber = upper_clnt->cl_prog,
2621 .version = upper_clnt->cl_vers,
2622 .authflavor = RPC_AUTH_TLS,
2623 .cred = upper_clnt->cl_cred,
2625 .policy = RPC_XPRTSEC_NONE,
2628 unsigned int pflags = current->flags;
2629 struct rpc_clnt *lower_clnt;
2630 struct rpc_xprt *lower_xprt;
2633 if (atomic_read(&upper_xprt->swapper))
2634 current->flags |= PF_MEMALLOC;
2636 xs_stream_start_connect(upper_transport);
2638 /* This implicitly sends an RPC_AUTH_TLS probe */
2639 lower_clnt = rpc_create(&args);
2640 if (IS_ERR(lower_clnt)) {
2641 trace_rpc_tls_unavailable(upper_clnt, upper_xprt);
2642 clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
2643 xprt_clear_connecting(upper_xprt);
2644 xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt));
2645 xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
2649 /* RPC_AUTH_TLS probe was successful. Try a TLS handshake on
2653 lower_xprt = rcu_dereference(lower_clnt->cl_xprt);
2655 status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec);
2657 trace_rpc_tls_not_started(upper_clnt, upper_xprt);
2661 status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport);
2665 trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0);
2666 if (!xprt_test_and_set_connected(upper_xprt)) {
2667 upper_xprt->connect_cookie++;
2668 clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
2669 xprt_clear_connecting(upper_xprt);
2671 upper_xprt->stat.connect_count++;
2672 upper_xprt->stat.connect_time += (long)jiffies -
2673 upper_xprt->stat.connect_start;
2674 xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
2676 rpc_shutdown_client(lower_clnt);
2679 current_restore_flags(pflags, PF_MEMALLOC);
2680 upper_transport->clnt = NULL;
2681 xprt_unlock_connect(upper_xprt, upper_transport);
2685 rpc_shutdown_client(lower_clnt);
2687 /* xprt_force_disconnect() wakes tasks with a fixed tk_status code.
2688 * Wake them first here to ensure they get our tk_status code.
2690 xprt_wake_pending_tasks(upper_xprt, status);
2691 xs_tcp_force_close(upper_xprt);
2692 xprt_clear_connecting(upper_xprt);
2697 * xs_connect - connect a socket to a remote endpoint
2698 * @xprt: pointer to transport structure
2699 * @task: address of RPC task that manages state of connect request
2701 * TCP: If the remote end dropped the connection, delay reconnecting.
2703 * UDP socket connects are synchronous, but we use a work queue anyway
2704 * to guarantee that even unprivileged user processes can set up a
2705 * socket on a privileged port.
2707 * If a UDP socket connect fails, the delay behavior here prevents
2708 * retry floods (hard mounts).
2710 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2712 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2713 unsigned long delay = 0;
2715 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2717 if (transport->sock != NULL) {
2718 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2719 "seconds\n", xprt, xprt->reestablish_timeout / HZ);
2721 delay = xprt_reconnect_delay(xprt);
2722 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
2725 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2727 transport->clnt = task->tk_client;
2728 queue_delayed_work(xprtiod_workqueue,
2729 &transport->connect_worker,
2733 static void xs_wake_disconnect(struct sock_xprt *transport)
2735 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2736 xs_tcp_force_close(&transport->xprt);
2739 static void xs_wake_write(struct sock_xprt *transport)
2741 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2742 xprt_write_space(&transport->xprt);
2745 static void xs_wake_error(struct sock_xprt *transport)
2749 if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2751 mutex_lock(&transport->recv_mutex);
2752 if (transport->sock == NULL)
2754 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2756 sockerr = xchg(&transport->xprt_err, 0);
2758 xprt_wake_pending_tasks(&transport->xprt, sockerr);
2760 mutex_unlock(&transport->recv_mutex);
2763 static void xs_wake_pending(struct sock_xprt *transport)
2765 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2766 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2769 static void xs_error_handle(struct work_struct *work)
2771 struct sock_xprt *transport = container_of(work,
2772 struct sock_xprt, error_worker);
2774 xs_wake_disconnect(transport);
2775 xs_wake_write(transport);
2776 xs_wake_error(transport);
2777 xs_wake_pending(transport);
2781 * xs_local_print_stats - display AF_LOCAL socket-specific stats
2782 * @xprt: rpc_xprt struct containing statistics
2786 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2790 if (xprt_connected(xprt))
2791 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2793 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2794 "%llu %llu %lu %llu %llu\n",
2795 xprt->stat.bind_count,
2796 xprt->stat.connect_count,
2797 xprt->stat.connect_time / HZ,
2801 xprt->stat.bad_xids,
2804 xprt->stat.max_slots,
2805 xprt->stat.sending_u,
2806 xprt->stat.pending_u);
2810 * xs_udp_print_stats - display UDP socket-specific stats
2811 * @xprt: rpc_xprt struct containing statistics
2815 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2817 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2819 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2822 xprt->stat.bind_count,
2825 xprt->stat.bad_xids,
2828 xprt->stat.max_slots,
2829 xprt->stat.sending_u,
2830 xprt->stat.pending_u);
2834 * xs_tcp_print_stats - display TCP socket-specific stats
2835 * @xprt: rpc_xprt struct containing statistics
2839 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2841 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2844 if (xprt_connected(xprt))
2845 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2847 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2848 "%llu %llu %lu %llu %llu\n",
2850 xprt->stat.bind_count,
2851 xprt->stat.connect_count,
2852 xprt->stat.connect_time / HZ,
2856 xprt->stat.bad_xids,
2859 xprt->stat.max_slots,
2860 xprt->stat.sending_u,
2861 xprt->stat.pending_u);
2865 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2866 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2867 * to use the server side send routines.
2869 static int bc_malloc(struct rpc_task *task)
2871 struct rpc_rqst *rqst = task->tk_rqstp;
2872 size_t size = rqst->rq_callsize;
2874 struct rpc_buffer *buf;
2876 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2877 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2882 page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
2886 buf = page_address(page);
2887 buf->len = PAGE_SIZE;
2889 rqst->rq_buffer = buf->data;
2890 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
2895 * Free the space allocated in the bc_alloc routine
2897 static void bc_free(struct rpc_task *task)
2899 void *buffer = task->tk_rqstp->rq_buffer;
2900 struct rpc_buffer *buf;
2902 buf = container_of(buffer, struct rpc_buffer, data);
2903 free_page((unsigned long)buf);
2906 static int bc_sendto(struct rpc_rqst *req)
2908 struct xdr_buf *xdr = &req->rq_snd_buf;
2909 struct sock_xprt *transport =
2910 container_of(req->rq_xprt, struct sock_xprt, xprt);
2911 struct msghdr msg = {
2914 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2916 unsigned int sent = 0;
2919 req->rq_xtime = ktime_get();
2920 err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2923 err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2925 if (err < 0 || sent != (xdr->len + sizeof(marker)))
2931 * bc_send_request - Send a backchannel Call on a TCP socket
2932 * @req: rpc_rqst containing Call message to be sent
2934 * xpt_mutex ensures @rqstp's whole message is written to the socket
2935 * without interruption.
2938 * %0 if the message was sent successfully
2939 * %ENOTCONN if the message was not sent
2941 static int bc_send_request(struct rpc_rqst *req)
2943 struct svc_xprt *xprt;
2947 * Get the server socket associated with this callback xprt
2949 xprt = req->rq_xprt->bc_xprt;
2952 * Grab the mutex to serialize data as the connection is shared
2953 * with the fore channel
2955 mutex_lock(&xprt->xpt_mutex);
2956 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2959 len = bc_sendto(req);
2960 mutex_unlock(&xprt->xpt_mutex);
2969 * The close routine. Since this is client initiated, we do nothing
2972 static void bc_close(struct rpc_xprt *xprt)
2974 xprt_disconnect_done(xprt);
2978 * The xprt destroy routine. Again, because this connection is client
2979 * initiated, we do nothing
2982 static void bc_destroy(struct rpc_xprt *xprt)
2984 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2987 module_put(THIS_MODULE);
2990 static const struct rpc_xprt_ops xs_local_ops = {
2991 .reserve_xprt = xprt_reserve_xprt,
2992 .release_xprt = xprt_release_xprt,
2993 .alloc_slot = xprt_alloc_slot,
2994 .free_slot = xprt_free_slot,
2995 .rpcbind = xs_local_rpcbind,
2996 .set_port = xs_local_set_port,
2997 .connect = xs_local_connect,
2998 .buf_alloc = rpc_malloc,
2999 .buf_free = rpc_free,
3000 .prepare_request = xs_stream_prepare_request,
3001 .send_request = xs_local_send_request,
3002 .wait_for_reply_request = xprt_wait_for_reply_request_def,
3004 .destroy = xs_destroy,
3005 .print_stats = xs_local_print_stats,
3006 .enable_swap = xs_enable_swap,
3007 .disable_swap = xs_disable_swap,
3010 static const struct rpc_xprt_ops xs_udp_ops = {
3011 .set_buffer_size = xs_udp_set_buffer_size,
3012 .reserve_xprt = xprt_reserve_xprt_cong,
3013 .release_xprt = xprt_release_xprt_cong,
3014 .alloc_slot = xprt_alloc_slot,
3015 .free_slot = xprt_free_slot,
3016 .rpcbind = rpcb_getport_async,
3017 .set_port = xs_set_port,
3018 .connect = xs_connect,
3019 .get_srcaddr = xs_sock_srcaddr,
3020 .get_srcport = xs_sock_srcport,
3021 .buf_alloc = rpc_malloc,
3022 .buf_free = rpc_free,
3023 .send_request = xs_udp_send_request,
3024 .wait_for_reply_request = xprt_wait_for_reply_request_rtt,
3025 .timer = xs_udp_timer,
3026 .release_request = xprt_release_rqst_cong,
3028 .destroy = xs_destroy,
3029 .print_stats = xs_udp_print_stats,
3030 .enable_swap = xs_enable_swap,
3031 .disable_swap = xs_disable_swap,
3032 .inject_disconnect = xs_inject_disconnect,
3035 static const struct rpc_xprt_ops xs_tcp_ops = {
3036 .reserve_xprt = xprt_reserve_xprt,
3037 .release_xprt = xprt_release_xprt,
3038 .alloc_slot = xprt_alloc_slot,
3039 .free_slot = xprt_free_slot,
3040 .rpcbind = rpcb_getport_async,
3041 .set_port = xs_set_port,
3042 .connect = xs_connect,
3043 .get_srcaddr = xs_sock_srcaddr,
3044 .get_srcport = xs_sock_srcport,
3045 .buf_alloc = rpc_malloc,
3046 .buf_free = rpc_free,
3047 .prepare_request = xs_stream_prepare_request,
3048 .send_request = xs_tcp_send_request,
3049 .wait_for_reply_request = xprt_wait_for_reply_request_def,
3050 .close = xs_tcp_shutdown,
3051 .destroy = xs_destroy,
3052 .set_connect_timeout = xs_tcp_set_connect_timeout,
3053 .print_stats = xs_tcp_print_stats,
3054 .enable_swap = xs_enable_swap,
3055 .disable_swap = xs_disable_swap,
3056 .inject_disconnect = xs_inject_disconnect,
3057 #ifdef CONFIG_SUNRPC_BACKCHANNEL
3058 .bc_setup = xprt_setup_bc,
3059 .bc_maxpayload = xs_tcp_bc_maxpayload,
3060 .bc_num_slots = xprt_bc_max_slots,
3061 .bc_free_rqst = xprt_free_bc_rqst,
3062 .bc_destroy = xprt_destroy_bc,
3067 * The rpc_xprt_ops for the server backchannel
3070 static const struct rpc_xprt_ops bc_tcp_ops = {
3071 .reserve_xprt = xprt_reserve_xprt,
3072 .release_xprt = xprt_release_xprt,
3073 .alloc_slot = xprt_alloc_slot,
3074 .free_slot = xprt_free_slot,
3075 .buf_alloc = bc_malloc,
3076 .buf_free = bc_free,
3077 .send_request = bc_send_request,
3078 .wait_for_reply_request = xprt_wait_for_reply_request_def,
3080 .destroy = bc_destroy,
3081 .print_stats = xs_tcp_print_stats,
3082 .enable_swap = xs_enable_swap,
3083 .disable_swap = xs_disable_swap,
3084 .inject_disconnect = xs_inject_disconnect,
3087 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
3089 static const struct sockaddr_in sin = {
3090 .sin_family = AF_INET,
3091 .sin_addr.s_addr = htonl(INADDR_ANY),
3093 static const struct sockaddr_in6 sin6 = {
3094 .sin6_family = AF_INET6,
3095 .sin6_addr = IN6ADDR_ANY_INIT,
3102 memcpy(sap, &sin, sizeof(sin));
3105 memcpy(sap, &sin6, sizeof(sin6));
3108 dprintk("RPC: %s: Bad address family\n", __func__);
3109 return -EAFNOSUPPORT;
3114 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
3115 unsigned int slot_table_size,
3116 unsigned int max_slot_table_size)
3118 struct rpc_xprt *xprt;
3119 struct sock_xprt *new;
3121 if (args->addrlen > sizeof(xprt->addr)) {
3122 dprintk("RPC: xs_setup_xprt: address too large\n");
3123 return ERR_PTR(-EBADF);
3126 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
3127 max_slot_table_size);
3129 dprintk("RPC: xs_setup_xprt: couldn't allocate "
3131 return ERR_PTR(-ENOMEM);
3134 new = container_of(xprt, struct sock_xprt, xprt);
3135 mutex_init(&new->recv_mutex);
3136 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
3137 xprt->addrlen = args->addrlen;
3139 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
3142 err = xs_init_anyaddr(args->dstaddr->sa_family,
3143 (struct sockaddr *)&new->srcaddr);
3146 return ERR_PTR(err);
3153 static const struct rpc_timeout xs_local_default_timeout = {
3154 .to_initval = 10 * HZ,
3155 .to_maxval = 10 * HZ,
3160 * xs_setup_local - Set up transport to use an AF_LOCAL socket
3161 * @args: rpc transport creation arguments
3163 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
3165 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
3167 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
3168 struct sock_xprt *transport;
3169 struct rpc_xprt *xprt;
3170 struct rpc_xprt *ret;
3172 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3173 xprt_max_tcp_slot_table_entries);
3176 transport = container_of(xprt, struct sock_xprt, xprt);
3179 xprt->xprt_class = &xs_local_transport;
3180 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3182 xprt->bind_timeout = XS_BIND_TO;
3183 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3184 xprt->idle_timeout = XS_IDLE_DISC_TO;
3186 xprt->ops = &xs_local_ops;
3187 xprt->timeout = &xs_local_default_timeout;
3189 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
3190 INIT_WORK(&transport->error_worker, xs_error_handle);
3191 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
3193 switch (sun->sun_family) {
3195 if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') {
3196 dprintk("RPC: bad AF_LOCAL address: %s\n",
3198 ret = ERR_PTR(-EINVAL);
3201 xprt_set_bound(xprt);
3202 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
3205 ret = ERR_PTR(-EAFNOSUPPORT);
3209 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
3210 xprt->address_strings[RPC_DISPLAY_ADDR]);
3212 if (try_module_get(THIS_MODULE))
3214 ret = ERR_PTR(-EINVAL);
3220 static const struct rpc_timeout xs_udp_default_timeout = {
3221 .to_initval = 5 * HZ,
3222 .to_maxval = 30 * HZ,
3223 .to_increment = 5 * HZ,
3228 * xs_setup_udp - Set up transport to use a UDP socket
3229 * @args: rpc transport creation arguments
3232 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
3234 struct sockaddr *addr = args->dstaddr;
3235 struct rpc_xprt *xprt;
3236 struct sock_xprt *transport;
3237 struct rpc_xprt *ret;
3239 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
3240 xprt_udp_slot_table_entries);
3243 transport = container_of(xprt, struct sock_xprt, xprt);
3245 xprt->prot = IPPROTO_UDP;
3246 xprt->xprt_class = &xs_udp_transport;
3247 /* XXX: header size can vary due to auth type, IPv6, etc. */
3248 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
3250 xprt->bind_timeout = XS_BIND_TO;
3251 xprt->reestablish_timeout = XS_UDP_REEST_TO;
3252 xprt->idle_timeout = XS_IDLE_DISC_TO;
3254 xprt->ops = &xs_udp_ops;
3256 xprt->timeout = &xs_udp_default_timeout;
3258 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
3259 INIT_WORK(&transport->error_worker, xs_error_handle);
3260 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
3262 switch (addr->sa_family) {
3264 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3265 xprt_set_bound(xprt);
3267 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
3270 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3271 xprt_set_bound(xprt);
3273 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
3276 ret = ERR_PTR(-EAFNOSUPPORT);
3280 if (xprt_bound(xprt))
3281 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3282 xprt->address_strings[RPC_DISPLAY_ADDR],
3283 xprt->address_strings[RPC_DISPLAY_PORT],
3284 xprt->address_strings[RPC_DISPLAY_PROTO]);
3286 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3287 xprt->address_strings[RPC_DISPLAY_ADDR],
3288 xprt->address_strings[RPC_DISPLAY_PROTO]);
3290 if (try_module_get(THIS_MODULE))
3292 ret = ERR_PTR(-EINVAL);
3298 static const struct rpc_timeout xs_tcp_default_timeout = {
3299 .to_initval = 60 * HZ,
3300 .to_maxval = 60 * HZ,
3305 * xs_setup_tcp - Set up transport to use a TCP socket
3306 * @args: rpc transport creation arguments
3309 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3311 struct sockaddr *addr = args->dstaddr;
3312 struct rpc_xprt *xprt;
3313 struct sock_xprt *transport;
3314 struct rpc_xprt *ret;
3315 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3317 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3318 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3320 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3321 max_slot_table_size);
3324 transport = container_of(xprt, struct sock_xprt, xprt);
3326 xprt->prot = IPPROTO_TCP;
3327 xprt->xprt_class = &xs_tcp_transport;
3328 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3330 xprt->bind_timeout = XS_BIND_TO;
3331 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3332 xprt->idle_timeout = XS_IDLE_DISC_TO;
3334 xprt->ops = &xs_tcp_ops;
3335 xprt->timeout = &xs_tcp_default_timeout;
3337 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
3338 xprt->connect_timeout = xprt->timeout->to_initval *
3339 (xprt->timeout->to_retries + 1);
3341 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
3342 INIT_WORK(&transport->error_worker, xs_error_handle);
3343 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3345 switch (addr->sa_family) {
3347 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3348 xprt_set_bound(xprt);
3350 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
3353 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3354 xprt_set_bound(xprt);
3356 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
3359 ret = ERR_PTR(-EAFNOSUPPORT);
3363 if (xprt_bound(xprt))
3364 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3365 xprt->address_strings[RPC_DISPLAY_ADDR],
3366 xprt->address_strings[RPC_DISPLAY_PORT],
3367 xprt->address_strings[RPC_DISPLAY_PROTO]);
3369 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3370 xprt->address_strings[RPC_DISPLAY_ADDR],
3371 xprt->address_strings[RPC_DISPLAY_PROTO]);
3373 if (try_module_get(THIS_MODULE))
3375 ret = ERR_PTR(-EINVAL);
3382 * xs_setup_tcp_tls - Set up transport to use a TCP with TLS
3383 * @args: rpc transport creation arguments
3386 static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args)
3388 struct sockaddr *addr = args->dstaddr;
3389 struct rpc_xprt *xprt;
3390 struct sock_xprt *transport;
3391 struct rpc_xprt *ret;
3392 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3394 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3395 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3397 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3398 max_slot_table_size);
3401 transport = container_of(xprt, struct sock_xprt, xprt);
3403 xprt->prot = IPPROTO_TCP;
3404 xprt->xprt_class = &xs_tcp_transport;
3405 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3407 xprt->bind_timeout = XS_BIND_TO;
3408 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3409 xprt->idle_timeout = XS_IDLE_DISC_TO;
3411 xprt->ops = &xs_tcp_ops;
3412 xprt->timeout = &xs_tcp_default_timeout;
3414 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
3415 xprt->connect_timeout = xprt->timeout->to_initval *
3416 (xprt->timeout->to_retries + 1);
3418 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
3419 INIT_WORK(&transport->error_worker, xs_error_handle);
3421 switch (args->xprtsec.policy) {
3422 case RPC_XPRTSEC_TLS_ANON:
3423 case RPC_XPRTSEC_TLS_X509:
3424 xprt->xprtsec = args->xprtsec;
3425 INIT_DELAYED_WORK(&transport->connect_worker,
3426 xs_tcp_tls_setup_socket);
3429 ret = ERR_PTR(-EACCES);
3433 switch (addr->sa_family) {
3435 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3436 xprt_set_bound(xprt);
3438 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
3441 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3442 xprt_set_bound(xprt);
3444 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
3447 ret = ERR_PTR(-EAFNOSUPPORT);
3451 if (xprt_bound(xprt))
3452 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3453 xprt->address_strings[RPC_DISPLAY_ADDR],
3454 xprt->address_strings[RPC_DISPLAY_PORT],
3455 xprt->address_strings[RPC_DISPLAY_PROTO]);
3457 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3458 xprt->address_strings[RPC_DISPLAY_ADDR],
3459 xprt->address_strings[RPC_DISPLAY_PROTO]);
3461 if (try_module_get(THIS_MODULE))
3463 ret = ERR_PTR(-EINVAL);
3470 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3471 * @args: rpc transport creation arguments
3474 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3476 struct sockaddr *addr = args->dstaddr;
3477 struct rpc_xprt *xprt;
3478 struct sock_xprt *transport;
3479 struct svc_sock *bc_sock;
3480 struct rpc_xprt *ret;
3482 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3483 xprt_tcp_slot_table_entries);
3486 transport = container_of(xprt, struct sock_xprt, xprt);
3488 xprt->prot = IPPROTO_TCP;
3489 xprt->xprt_class = &xs_bc_tcp_transport;
3490 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3491 xprt->timeout = &xs_tcp_default_timeout;
3494 xprt_set_bound(xprt);
3495 xprt->bind_timeout = 0;
3496 xprt->reestablish_timeout = 0;
3497 xprt->idle_timeout = 0;
3499 xprt->ops = &bc_tcp_ops;
3501 switch (addr->sa_family) {
3503 xs_format_peer_addresses(xprt, "tcp",
3507 xs_format_peer_addresses(xprt, "tcp",
3508 RPCBIND_NETID_TCP6);
3511 ret = ERR_PTR(-EAFNOSUPPORT);
3515 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3516 xprt->address_strings[RPC_DISPLAY_ADDR],
3517 xprt->address_strings[RPC_DISPLAY_PORT],
3518 xprt->address_strings[RPC_DISPLAY_PROTO]);
3521 * Once we've associated a backchannel xprt with a connection,
3522 * we want to keep it around as long as the connection lasts,
3523 * in case we need to start using it for a backchannel again;
3524 * this reference won't be dropped until bc_xprt is destroyed.
3527 args->bc_xprt->xpt_bc_xprt = xprt;
3528 xprt->bc_xprt = args->bc_xprt;
3529 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3530 transport->sock = bc_sock->sk_sock;
3531 transport->inet = bc_sock->sk_sk;
3534 * Since we don't want connections for the backchannel, we set
3535 * the xprt status to connected
3537 xprt_set_connected(xprt);
3539 if (try_module_get(THIS_MODULE))
3542 args->bc_xprt->xpt_bc_xprt = NULL;
3543 args->bc_xprt->xpt_bc_xps = NULL;
3545 ret = ERR_PTR(-EINVAL);
3551 static struct xprt_class xs_local_transport = {
3552 .list = LIST_HEAD_INIT(xs_local_transport.list),
3553 .name = "named UNIX socket",
3554 .owner = THIS_MODULE,
3555 .ident = XPRT_TRANSPORT_LOCAL,
3556 .setup = xs_setup_local,
3560 static struct xprt_class xs_udp_transport = {
3561 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3563 .owner = THIS_MODULE,
3564 .ident = XPRT_TRANSPORT_UDP,
3565 .setup = xs_setup_udp,
3566 .netid = { "udp", "udp6", "" },
3569 static struct xprt_class xs_tcp_transport = {
3570 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3572 .owner = THIS_MODULE,
3573 .ident = XPRT_TRANSPORT_TCP,
3574 .setup = xs_setup_tcp,
3575 .netid = { "tcp", "tcp6", "" },
3578 static struct xprt_class xs_tcp_tls_transport = {
3579 .list = LIST_HEAD_INIT(xs_tcp_tls_transport.list),
3580 .name = "tcp-with-tls",
3581 .owner = THIS_MODULE,
3582 .ident = XPRT_TRANSPORT_TCP_TLS,
3583 .setup = xs_setup_tcp_tls,
3584 .netid = { "tcp", "tcp6", "" },
3587 static struct xprt_class xs_bc_tcp_transport = {
3588 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3589 .name = "tcp NFSv4.1 backchannel",
3590 .owner = THIS_MODULE,
3591 .ident = XPRT_TRANSPORT_BC_TCP,
3592 .setup = xs_setup_bc_tcp,
3597 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3600 int init_socket_xprt(void)
3602 if (!sunrpc_table_header)
3603 sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table);
3605 xprt_register_transport(&xs_local_transport);
3606 xprt_register_transport(&xs_udp_transport);
3607 xprt_register_transport(&xs_tcp_transport);
3608 xprt_register_transport(&xs_tcp_tls_transport);
3609 xprt_register_transport(&xs_bc_tcp_transport);
3615 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3618 void cleanup_socket_xprt(void)
3620 if (sunrpc_table_header) {
3621 unregister_sysctl_table(sunrpc_table_header);
3622 sunrpc_table_header = NULL;
3625 xprt_unregister_transport(&xs_local_transport);
3626 xprt_unregister_transport(&xs_udp_transport);
3627 xprt_unregister_transport(&xs_tcp_transport);
3628 xprt_unregister_transport(&xs_tcp_tls_transport);
3629 xprt_unregister_transport(&xs_bc_tcp_transport);
3632 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3634 return param_set_uint_minmax(val, kp,
3639 static const struct kernel_param_ops param_ops_portnr = {
3640 .set = param_set_portnr,
3641 .get = param_get_uint,
3644 #define param_check_portnr(name, p) \
3645 __param_check(name, p, unsigned int);
3647 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3648 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3650 static int param_set_slot_table_size(const char *val,
3651 const struct kernel_param *kp)
3653 return param_set_uint_minmax(val, kp,
3655 RPC_MAX_SLOT_TABLE);
3658 static const struct kernel_param_ops param_ops_slot_table_size = {
3659 .set = param_set_slot_table_size,
3660 .get = param_get_uint,
3663 #define param_check_slot_table_size(name, p) \
3664 __param_check(name, p, unsigned int);
3666 static int param_set_max_slot_table_size(const char *val,
3667 const struct kernel_param *kp)
3669 return param_set_uint_minmax(val, kp,
3671 RPC_MAX_SLOT_TABLE_LIMIT);
3674 static const struct kernel_param_ops param_ops_max_slot_table_size = {
3675 .set = param_set_max_slot_table_size,
3676 .get = param_get_uint,
3679 #define param_check_max_slot_table_size(name, p) \
3680 __param_check(name, p, unsigned int);
3682 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3683 slot_table_size, 0644);
3684 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3685 max_slot_table_size, 0644);
3686 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3687 slot_table_size, 0644);