gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / net / sunrpc / xprtsock.c
blob0bda8a73e8a86ea99ff5c04192a4624752d8a067
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
30 #include <linux/in.h>
31 #include <linux/net.h>
32 #include <linux/mm.h>
33 #include <linux/un.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>
44 #endif
46 #include <net/sock.h>
47 #include <net/checksum.h>
48 #include <net/udp.h>
49 #include <net/tcp.h>
50 #include <linux/bvec.h>
51 #include <linux/highmem.h>
52 #include <linux/uio.h>
53 #include <linux/sched/mm.h>
55 #include <trace/events/sunrpc.h>
57 #include "socklib.h"
58 #include "sunrpc.h"
60 static void xs_close(struct rpc_xprt *xprt);
61 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
62 struct socket *sock);
65 * xprtsock tunables
67 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
68 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
69 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
71 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
72 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
74 #define XS_TCP_LINGER_TO (15U * HZ)
75 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
78 * We can register our own files under /proc/sys/sunrpc by
79 * calling register_sysctl_table() again. The files in that
80 * directory become the union of all files registered there.
82 * We simply need to make sure that we don't collide with
83 * someone else's file names!
86 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
87 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
88 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
89 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
90 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
92 static struct ctl_table_header *sunrpc_table_header;
95 * FIXME: changing the UDP slot table size should also resize the UDP
96 * socket buffers for existing UDP transports
98 static struct ctl_table xs_tunables_table[] = {
100 .procname = "udp_slot_table_entries",
101 .data = &xprt_udp_slot_table_entries,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
104 .proc_handler = proc_dointvec_minmax,
105 .extra1 = &min_slot_table_size,
106 .extra2 = &max_slot_table_size
109 .procname = "tcp_slot_table_entries",
110 .data = &xprt_tcp_slot_table_entries,
111 .maxlen = sizeof(unsigned int),
112 .mode = 0644,
113 .proc_handler = proc_dointvec_minmax,
114 .extra1 = &min_slot_table_size,
115 .extra2 = &max_slot_table_size
118 .procname = "tcp_max_slot_table_entries",
119 .data = &xprt_max_tcp_slot_table_entries,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
122 .proc_handler = proc_dointvec_minmax,
123 .extra1 = &min_slot_table_size,
124 .extra2 = &max_tcp_slot_table_limit
127 .procname = "min_resvport",
128 .data = &xprt_min_resvport,
129 .maxlen = sizeof(unsigned int),
130 .mode = 0644,
131 .proc_handler = proc_dointvec_minmax,
132 .extra1 = &xprt_min_resvport_limit,
133 .extra2 = &xprt_max_resvport_limit
136 .procname = "max_resvport",
137 .data = &xprt_max_resvport,
138 .maxlen = sizeof(unsigned int),
139 .mode = 0644,
140 .proc_handler = proc_dointvec_minmax,
141 .extra1 = &xprt_min_resvport_limit,
142 .extra2 = &xprt_max_resvport_limit
145 .procname = "tcp_fin_timeout",
146 .data = &xs_tcp_fin_timeout,
147 .maxlen = sizeof(xs_tcp_fin_timeout),
148 .mode = 0644,
149 .proc_handler = proc_dointvec_jiffies,
151 { },
154 static struct ctl_table sunrpc_table[] = {
156 .procname = "sunrpc",
157 .mode = 0555,
158 .child = xs_tunables_table
160 { },
164 * Wait duration for a reply from the RPC portmapper.
166 #define XS_BIND_TO (60U * HZ)
169 * Delay if a UDP socket connect error occurs. This is most likely some
170 * kind of resource problem on the local host.
172 #define XS_UDP_REEST_TO (2U * HZ)
175 * The reestablish timeout allows clients to delay for a bit before attempting
176 * to reconnect to a server that just dropped our connection.
178 * We implement an exponential backoff when trying to reestablish a TCP
179 * transport connection with the server. Some servers like to drop a TCP
180 * connection when they are overworked, so we start with a short timeout and
181 * increase over time if the server is down or not responding.
183 #define XS_TCP_INIT_REEST_TO (3U * HZ)
186 * TCP idle timeout; client drops the transport socket if it is idle
187 * for this long. Note that we also timeout UDP sockets to prevent
188 * holding port numbers when there is no RPC traffic.
190 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
192 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
193 # undef RPC_DEBUG_DATA
194 # define RPCDBG_FACILITY RPCDBG_TRANS
195 #endif
197 #ifdef RPC_DEBUG_DATA
198 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
200 u8 *buf = (u8 *) packet;
201 int j;
203 dprintk("RPC: %s\n", msg);
204 for (j = 0; j < count && j < 128; j += 4) {
205 if (!(j & 31)) {
206 if (j)
207 dprintk("\n");
208 dprintk("0x%04x ", j);
210 dprintk("%02x%02x%02x%02x ",
211 buf[j], buf[j+1], buf[j+2], buf[j+3]);
213 dprintk("\n");
215 #else
216 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
218 /* NOP */
220 #endif
222 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
224 return (struct rpc_xprt *) sk->sk_user_data;
227 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
229 return (struct sockaddr *) &xprt->addr;
232 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
234 return (struct sockaddr_un *) &xprt->addr;
237 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
239 return (struct sockaddr_in *) &xprt->addr;
242 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
244 return (struct sockaddr_in6 *) &xprt->addr;
247 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
249 struct sockaddr *sap = xs_addr(xprt);
250 struct sockaddr_in6 *sin6;
251 struct sockaddr_in *sin;
252 struct sockaddr_un *sun;
253 char buf[128];
255 switch (sap->sa_family) {
256 case AF_LOCAL:
257 sun = xs_addr_un(xprt);
258 strlcpy(buf, sun->sun_path, sizeof(buf));
259 xprt->address_strings[RPC_DISPLAY_ADDR] =
260 kstrdup(buf, GFP_KERNEL);
261 break;
262 case AF_INET:
263 (void)rpc_ntop(sap, buf, sizeof(buf));
264 xprt->address_strings[RPC_DISPLAY_ADDR] =
265 kstrdup(buf, GFP_KERNEL);
266 sin = xs_addr_in(xprt);
267 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
268 break;
269 case AF_INET6:
270 (void)rpc_ntop(sap, buf, sizeof(buf));
271 xprt->address_strings[RPC_DISPLAY_ADDR] =
272 kstrdup(buf, GFP_KERNEL);
273 sin6 = xs_addr_in6(xprt);
274 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
275 break;
276 default:
277 BUG();
280 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
283 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
285 struct sockaddr *sap = xs_addr(xprt);
286 char buf[128];
288 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
289 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
291 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
292 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
295 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
296 const char *protocol,
297 const char *netid)
299 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
300 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
301 xs_format_common_peer_addresses(xprt);
302 xs_format_common_peer_ports(xprt);
305 static void xs_update_peer_port(struct rpc_xprt *xprt)
307 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
308 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
310 xs_format_common_peer_ports(xprt);
313 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
315 unsigned int i;
317 for (i = 0; i < RPC_DISPLAY_MAX; i++)
318 switch (i) {
319 case RPC_DISPLAY_PROTO:
320 case RPC_DISPLAY_NETID:
321 continue;
322 default:
323 kfree(xprt->address_strings[i]);
327 static size_t
328 xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
330 size_t i,n;
332 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
333 return want;
334 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
335 for (i = 0; i < n; i++) {
336 if (buf->pages[i])
337 continue;
338 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
339 if (!buf->pages[i]) {
340 i *= PAGE_SIZE;
341 return i > buf->page_base ? i - buf->page_base : 0;
344 return want;
347 static ssize_t
348 xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
350 ssize_t ret;
351 if (seek != 0)
352 iov_iter_advance(&msg->msg_iter, seek);
353 ret = sock_recvmsg(sock, msg, flags);
354 return ret > 0 ? ret + seek : ret;
357 static ssize_t
358 xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
359 struct kvec *kvec, size_t count, size_t seek)
361 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
362 return xs_sock_recvmsg(sock, msg, flags, seek);
365 static ssize_t
366 xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
367 struct bio_vec *bvec, unsigned long nr, size_t count,
368 size_t seek)
370 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
371 return xs_sock_recvmsg(sock, msg, flags, seek);
374 static ssize_t
375 xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
376 size_t count)
378 iov_iter_discard(&msg->msg_iter, READ, count);
379 return sock_recvmsg(sock, msg, flags);
382 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
383 static void
384 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
386 struct bvec_iter bi = {
387 .bi_size = count,
389 struct bio_vec bv;
391 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
392 for_each_bvec(bv, bvec, bi, bi)
393 flush_dcache_page(bv.bv_page);
395 #else
396 static inline void
397 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
400 #endif
402 static ssize_t
403 xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
404 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
406 size_t want, seek_init = seek, offset = 0;
407 ssize_t ret;
409 want = min_t(size_t, count, buf->head[0].iov_len);
410 if (seek < want) {
411 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
412 if (ret <= 0)
413 goto sock_err;
414 offset += ret;
415 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
416 goto out;
417 if (ret != want)
418 goto out;
419 seek = 0;
420 } else {
421 seek -= want;
422 offset += want;
425 want = xs_alloc_sparse_pages(buf,
426 min_t(size_t, count - offset, buf->page_len),
427 GFP_KERNEL);
428 if (seek < want) {
429 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
430 xdr_buf_pagecount(buf),
431 want + buf->page_base,
432 seek + buf->page_base);
433 if (ret <= 0)
434 goto sock_err;
435 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
436 offset += ret - buf->page_base;
437 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
438 goto out;
439 if (ret != want)
440 goto out;
441 seek = 0;
442 } else {
443 seek -= want;
444 offset += want;
447 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
448 if (seek < want) {
449 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
450 if (ret <= 0)
451 goto sock_err;
452 offset += ret;
453 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
454 goto out;
455 if (ret != want)
456 goto out;
457 } else if (offset < seek_init)
458 offset = seek_init;
459 ret = -EMSGSIZE;
460 out:
461 *read = offset - seek_init;
462 return ret;
463 sock_err:
464 offset += seek;
465 goto out;
468 static void
469 xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
471 if (!transport->recv.copied) {
472 if (buf->head[0].iov_len >= transport->recv.offset)
473 memcpy(buf->head[0].iov_base,
474 &transport->recv.xid,
475 transport->recv.offset);
476 transport->recv.copied = transport->recv.offset;
480 static bool
481 xs_read_stream_request_done(struct sock_xprt *transport)
483 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
486 static void
487 xs_read_stream_check_eor(struct sock_xprt *transport,
488 struct msghdr *msg)
490 if (xs_read_stream_request_done(transport))
491 msg->msg_flags |= MSG_EOR;
494 static ssize_t
495 xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
496 int flags, struct rpc_rqst *req)
498 struct xdr_buf *buf = &req->rq_private_buf;
499 size_t want, uninitialized_var(read);
500 ssize_t uninitialized_var(ret);
502 xs_read_header(transport, buf);
504 want = transport->recv.len - transport->recv.offset;
505 if (want != 0) {
506 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
507 transport->recv.copied + want,
508 transport->recv.copied,
509 &read);
510 transport->recv.offset += read;
511 transport->recv.copied += read;
514 if (transport->recv.offset == transport->recv.len)
515 xs_read_stream_check_eor(transport, msg);
517 if (want == 0)
518 return 0;
520 switch (ret) {
521 default:
522 break;
523 case -EFAULT:
524 case -EMSGSIZE:
525 msg->msg_flags |= MSG_TRUNC;
526 return read;
527 case 0:
528 return -ESHUTDOWN;
530 return ret < 0 ? ret : read;
533 static size_t
534 xs_read_stream_headersize(bool isfrag)
536 if (isfrag)
537 return sizeof(__be32);
538 return 3 * sizeof(__be32);
541 static ssize_t
542 xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
543 int flags, size_t want, size_t seek)
545 struct kvec kvec = {
546 .iov_base = &transport->recv.fraghdr,
547 .iov_len = want,
549 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
552 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
553 static ssize_t
554 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
556 struct rpc_xprt *xprt = &transport->xprt;
557 struct rpc_rqst *req;
558 ssize_t ret;
560 /* Look up and lock the request corresponding to the given XID */
561 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
562 if (!req) {
563 printk(KERN_WARNING "Callback slot table overflowed\n");
564 return -ESHUTDOWN;
566 if (transport->recv.copied && !req->rq_private_buf.len)
567 return -ESHUTDOWN;
569 ret = xs_read_stream_request(transport, msg, flags, req);
570 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
571 xprt_complete_bc_request(req, transport->recv.copied);
572 else
573 req->rq_private_buf.len = transport->recv.copied;
575 return ret;
577 #else /* CONFIG_SUNRPC_BACKCHANNEL */
578 static ssize_t
579 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
581 return -ESHUTDOWN;
583 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
585 static ssize_t
586 xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
588 struct rpc_xprt *xprt = &transport->xprt;
589 struct rpc_rqst *req;
590 ssize_t ret = 0;
592 /* Look up and lock the request corresponding to the given XID */
593 spin_lock(&xprt->queue_lock);
594 req = xprt_lookup_rqst(xprt, transport->recv.xid);
595 if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
596 msg->msg_flags |= MSG_TRUNC;
597 goto out;
599 xprt_pin_rqst(req);
600 spin_unlock(&xprt->queue_lock);
602 ret = xs_read_stream_request(transport, msg, flags, req);
604 spin_lock(&xprt->queue_lock);
605 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
606 xprt_complete_rqst(req->rq_task, transport->recv.copied);
607 else
608 req->rq_private_buf.len = transport->recv.copied;
609 xprt_unpin_rqst(req);
610 out:
611 spin_unlock(&xprt->queue_lock);
612 return ret;
615 static ssize_t
616 xs_read_stream(struct sock_xprt *transport, int flags)
618 struct msghdr msg = { 0 };
619 size_t want, read = 0;
620 ssize_t ret = 0;
622 if (transport->recv.len == 0) {
623 want = xs_read_stream_headersize(transport->recv.copied != 0);
624 ret = xs_read_stream_header(transport, &msg, flags, want,
625 transport->recv.offset);
626 if (ret <= 0)
627 goto out_err;
628 transport->recv.offset = ret;
629 if (transport->recv.offset != want)
630 return transport->recv.offset;
631 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
632 RPC_FRAGMENT_SIZE_MASK;
633 transport->recv.offset -= sizeof(transport->recv.fraghdr);
634 read = ret;
637 switch (be32_to_cpu(transport->recv.calldir)) {
638 default:
639 msg.msg_flags |= MSG_TRUNC;
640 break;
641 case RPC_CALL:
642 ret = xs_read_stream_call(transport, &msg, flags);
643 break;
644 case RPC_REPLY:
645 ret = xs_read_stream_reply(transport, &msg, flags);
647 if (msg.msg_flags & MSG_TRUNC) {
648 transport->recv.calldir = cpu_to_be32(-1);
649 transport->recv.copied = -1;
651 if (ret < 0)
652 goto out_err;
653 read += ret;
654 if (transport->recv.offset < transport->recv.len) {
655 if (!(msg.msg_flags & MSG_TRUNC))
656 return read;
657 msg.msg_flags = 0;
658 ret = xs_read_discard(transport->sock, &msg, flags,
659 transport->recv.len - transport->recv.offset);
660 if (ret <= 0)
661 goto out_err;
662 transport->recv.offset += ret;
663 read += ret;
664 if (transport->recv.offset != transport->recv.len)
665 return read;
667 if (xs_read_stream_request_done(transport)) {
668 trace_xs_stream_read_request(transport);
669 transport->recv.copied = 0;
671 transport->recv.offset = 0;
672 transport->recv.len = 0;
673 return read;
674 out_err:
675 return ret != 0 ? ret : -ESHUTDOWN;
678 static __poll_t xs_poll_socket(struct sock_xprt *transport)
680 return transport->sock->ops->poll(transport->file, transport->sock,
681 NULL);
684 static bool xs_poll_socket_readable(struct sock_xprt *transport)
686 __poll_t events = xs_poll_socket(transport);
688 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
691 static void xs_poll_check_readable(struct sock_xprt *transport)
694 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
695 if (!xs_poll_socket_readable(transport))
696 return;
697 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
698 queue_work(xprtiod_workqueue, &transport->recv_worker);
701 static void xs_stream_data_receive(struct sock_xprt *transport)
703 size_t read = 0;
704 ssize_t ret = 0;
706 mutex_lock(&transport->recv_mutex);
707 if (transport->sock == NULL)
708 goto out;
709 for (;;) {
710 ret = xs_read_stream(transport, MSG_DONTWAIT);
711 if (ret < 0)
712 break;
713 read += ret;
714 cond_resched();
716 if (ret == -ESHUTDOWN)
717 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
718 else
719 xs_poll_check_readable(transport);
720 out:
721 mutex_unlock(&transport->recv_mutex);
722 trace_xs_stream_read_data(&transport->xprt, ret, read);
725 static void xs_stream_data_receive_workfn(struct work_struct *work)
727 struct sock_xprt *transport =
728 container_of(work, struct sock_xprt, recv_worker);
729 unsigned int pflags = memalloc_nofs_save();
731 xs_stream_data_receive(transport);
732 memalloc_nofs_restore(pflags);
735 static void
736 xs_stream_reset_connect(struct sock_xprt *transport)
738 transport->recv.offset = 0;
739 transport->recv.len = 0;
740 transport->recv.copied = 0;
741 transport->xmit.offset = 0;
744 static void
745 xs_stream_start_connect(struct sock_xprt *transport)
747 transport->xprt.stat.connect_count++;
748 transport->xprt.stat.connect_start = jiffies;
751 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
754 * xs_nospace - handle transmit was incomplete
755 * @req: pointer to RPC request
758 static int xs_nospace(struct rpc_rqst *req)
760 struct rpc_xprt *xprt = req->rq_xprt;
761 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
762 struct sock *sk = transport->inet;
763 int ret = -EAGAIN;
765 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
766 req->rq_task->tk_pid,
767 req->rq_slen - transport->xmit.offset,
768 req->rq_slen);
770 /* Protect against races with write_space */
771 spin_lock(&xprt->transport_lock);
773 /* Don't race with disconnect */
774 if (xprt_connected(xprt)) {
775 /* wait for more buffer space */
776 sk->sk_write_pending++;
777 xprt_wait_for_buffer_space(xprt);
778 } else
779 ret = -ENOTCONN;
781 spin_unlock(&xprt->transport_lock);
783 /* Race breaker in case memory is freed before above code is called */
784 if (ret == -EAGAIN) {
785 struct socket_wq *wq;
787 rcu_read_lock();
788 wq = rcu_dereference(sk->sk_wq);
789 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
790 rcu_read_unlock();
792 sk->sk_write_space(sk);
794 return ret;
797 static void
798 xs_stream_prepare_request(struct rpc_rqst *req)
800 xdr_free_bvec(&req->rq_rcv_buf);
801 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
805 * Determine if the previous message in the stream was aborted before it
806 * could complete transmission.
808 static bool
809 xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
811 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
815 * Return the stream record marker field for a record of length < 2^31-1
817 static rpc_fraghdr
818 xs_stream_record_marker(struct xdr_buf *xdr)
820 if (!xdr->len)
821 return 0;
822 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
826 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
827 * @req: pointer to RPC request
829 * Return values:
830 * 0: The request has been sent
831 * EAGAIN: The socket was blocked, please call again later to
832 * complete the request
833 * ENOTCONN: Caller needs to invoke connect logic then call again
834 * other: Some other error occured, the request was not sent
836 static int xs_local_send_request(struct rpc_rqst *req)
838 struct rpc_xprt *xprt = req->rq_xprt;
839 struct sock_xprt *transport =
840 container_of(xprt, struct sock_xprt, xprt);
841 struct xdr_buf *xdr = &req->rq_snd_buf;
842 rpc_fraghdr rm = xs_stream_record_marker(xdr);
843 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
844 struct msghdr msg = {
845 .msg_flags = XS_SENDMSG_FLAGS,
847 unsigned int uninitialized_var(sent);
848 int status;
850 /* Close the stream if the previous transmission was incomplete */
851 if (xs_send_request_was_aborted(transport, req)) {
852 xs_close(xprt);
853 return -ENOTCONN;
856 xs_pktdump("packet data:",
857 req->rq_svec->iov_base, req->rq_svec->iov_len);
859 req->rq_xtime = ktime_get();
860 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
861 transport->xmit.offset, rm, &sent);
862 dprintk("RPC: %s(%u) = %d\n",
863 __func__, xdr->len - transport->xmit.offset, status);
865 if (status == -EAGAIN && sock_writeable(transport->inet))
866 status = -ENOBUFS;
868 if (likely(sent > 0) || status == 0) {
869 transport->xmit.offset += sent;
870 req->rq_bytes_sent = transport->xmit.offset;
871 if (likely(req->rq_bytes_sent >= msglen)) {
872 req->rq_xmit_bytes_sent += transport->xmit.offset;
873 transport->xmit.offset = 0;
874 return 0;
876 status = -EAGAIN;
879 switch (status) {
880 case -ENOBUFS:
881 break;
882 case -EAGAIN:
883 status = xs_nospace(req);
884 break;
885 default:
886 dprintk("RPC: sendmsg returned unrecognized error %d\n",
887 -status);
888 /* fall through */
889 case -EPIPE:
890 xs_close(xprt);
891 status = -ENOTCONN;
894 return status;
898 * xs_udp_send_request - write an RPC request to a UDP socket
899 * @req: pointer to RPC request
901 * Return values:
902 * 0: The request has been sent
903 * EAGAIN: The socket was blocked, please call again later to
904 * complete the request
905 * ENOTCONN: Caller needs to invoke connect logic then call again
906 * other: Some other error occurred, the request was not sent
908 static int xs_udp_send_request(struct rpc_rqst *req)
910 struct rpc_xprt *xprt = req->rq_xprt;
911 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
912 struct xdr_buf *xdr = &req->rq_snd_buf;
913 struct msghdr msg = {
914 .msg_name = xs_addr(xprt),
915 .msg_namelen = xprt->addrlen,
916 .msg_flags = XS_SENDMSG_FLAGS,
918 unsigned int uninitialized_var(sent);
919 int status;
921 xs_pktdump("packet data:",
922 req->rq_svec->iov_base,
923 req->rq_svec->iov_len);
925 if (!xprt_bound(xprt))
926 return -ENOTCONN;
928 if (!xprt_request_get_cong(xprt, req))
929 return -EBADSLT;
931 req->rq_xtime = ktime_get();
932 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
934 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
935 xdr->len, status);
937 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
938 if (status == -EPERM)
939 goto process_status;
941 if (status == -EAGAIN && sock_writeable(transport->inet))
942 status = -ENOBUFS;
944 if (sent > 0 || status == 0) {
945 req->rq_xmit_bytes_sent += sent;
946 if (sent >= req->rq_slen)
947 return 0;
948 /* Still some bytes left; set up for a retry later. */
949 status = -EAGAIN;
952 process_status:
953 switch (status) {
954 case -ENOTSOCK:
955 status = -ENOTCONN;
956 /* Should we call xs_close() here? */
957 break;
958 case -EAGAIN:
959 status = xs_nospace(req);
960 break;
961 case -ENETUNREACH:
962 case -ENOBUFS:
963 case -EPIPE:
964 case -ECONNREFUSED:
965 case -EPERM:
966 /* When the server has died, an ICMP port unreachable message
967 * prompts ECONNREFUSED. */
968 break;
969 default:
970 dprintk("RPC: sendmsg returned unrecognized error %d\n",
971 -status);
974 return status;
978 * xs_tcp_send_request - write an RPC request to a TCP socket
979 * @req: pointer to RPC request
981 * Return values:
982 * 0: The request has been sent
983 * EAGAIN: The socket was blocked, please call again later to
984 * complete the request
985 * ENOTCONN: Caller needs to invoke connect logic then call again
986 * other: Some other error occurred, the request was not sent
988 * XXX: In the case of soft timeouts, should we eventually give up
989 * if sendmsg is not able to make progress?
991 static int xs_tcp_send_request(struct rpc_rqst *req)
993 struct rpc_xprt *xprt = req->rq_xprt;
994 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
995 struct xdr_buf *xdr = &req->rq_snd_buf;
996 rpc_fraghdr rm = xs_stream_record_marker(xdr);
997 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
998 struct msghdr msg = {
999 .msg_flags = XS_SENDMSG_FLAGS,
1001 bool vm_wait = false;
1002 unsigned int uninitialized_var(sent);
1003 int status;
1005 /* Close the stream if the previous transmission was incomplete */
1006 if (xs_send_request_was_aborted(transport, req)) {
1007 if (transport->sock != NULL)
1008 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1009 return -ENOTCONN;
1012 xs_pktdump("packet data:",
1013 req->rq_svec->iov_base,
1014 req->rq_svec->iov_len);
1016 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1017 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1019 /* Continue transmitting the packet/record. We must be careful
1020 * to cope with writespace callbacks arriving _after_ we have
1021 * called sendmsg(). */
1022 req->rq_xtime = ktime_get();
1023 while (1) {
1024 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
1025 transport->xmit.offset, rm, &sent);
1027 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
1028 xdr->len - transport->xmit.offset, status);
1030 /* If we've sent the entire packet, immediately
1031 * reset the count of bytes sent. */
1032 transport->xmit.offset += sent;
1033 req->rq_bytes_sent = transport->xmit.offset;
1034 if (likely(req->rq_bytes_sent >= msglen)) {
1035 req->rq_xmit_bytes_sent += transport->xmit.offset;
1036 transport->xmit.offset = 0;
1037 return 0;
1040 WARN_ON_ONCE(sent == 0 && status == 0);
1042 if (status == -EAGAIN ) {
1044 * Return EAGAIN if we're sure we're hitting the
1045 * socket send buffer limits.
1047 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1048 break;
1050 * Did we hit a memory allocation failure?
1052 if (sent == 0) {
1053 status = -ENOBUFS;
1054 if (vm_wait)
1055 break;
1056 /* Retry, knowing now that we're below the
1057 * socket send buffer limit
1059 vm_wait = true;
1061 continue;
1063 if (status < 0)
1064 break;
1065 vm_wait = false;
1068 switch (status) {
1069 case -ENOTSOCK:
1070 status = -ENOTCONN;
1071 /* Should we call xs_close() here? */
1072 break;
1073 case -EAGAIN:
1074 status = xs_nospace(req);
1075 break;
1076 case -ECONNRESET:
1077 case -ECONNREFUSED:
1078 case -ENOTCONN:
1079 case -EADDRINUSE:
1080 case -ENOBUFS:
1081 case -EPIPE:
1082 break;
1083 default:
1084 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1085 -status);
1088 return status;
1091 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1093 transport->old_data_ready = sk->sk_data_ready;
1094 transport->old_state_change = sk->sk_state_change;
1095 transport->old_write_space = sk->sk_write_space;
1096 transport->old_error_report = sk->sk_error_report;
1099 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1101 sk->sk_data_ready = transport->old_data_ready;
1102 sk->sk_state_change = transport->old_state_change;
1103 sk->sk_write_space = transport->old_write_space;
1104 sk->sk_error_report = transport->old_error_report;
1107 static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1109 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1111 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1112 clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1113 clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1114 clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1117 static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1119 set_bit(nr, &transport->sock_state);
1120 queue_work(xprtiod_workqueue, &transport->error_worker);
1123 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1125 smp_mb__before_atomic();
1126 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1127 clear_bit(XPRT_CLOSING, &xprt->state);
1128 xs_sock_reset_state_flags(xprt);
1129 smp_mb__after_atomic();
1133 * xs_error_report - callback to handle TCP socket state errors
1134 * @sk: socket
1136 * Note: we don't call sock_error() since there may be a rpc_task
1137 * using the socket, and so we don't want to clear sk->sk_err.
1139 static void xs_error_report(struct sock *sk)
1141 struct sock_xprt *transport;
1142 struct rpc_xprt *xprt;
1144 read_lock_bh(&sk->sk_callback_lock);
1145 if (!(xprt = xprt_from_sock(sk)))
1146 goto out;
1148 transport = container_of(xprt, struct sock_xprt, xprt);
1149 transport->xprt_err = -sk->sk_err;
1150 if (transport->xprt_err == 0)
1151 goto out;
1152 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1153 xprt, -transport->xprt_err);
1154 trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1156 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1157 smp_mb__before_atomic();
1158 xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
1159 out:
1160 read_unlock_bh(&sk->sk_callback_lock);
1163 static void xs_reset_transport(struct sock_xprt *transport)
1165 struct socket *sock = transport->sock;
1166 struct sock *sk = transport->inet;
1167 struct rpc_xprt *xprt = &transport->xprt;
1168 struct file *filp = transport->file;
1170 if (sk == NULL)
1171 return;
1173 if (atomic_read(&transport->xprt.swapper))
1174 sk_clear_memalloc(sk);
1176 kernel_sock_shutdown(sock, SHUT_RDWR);
1178 mutex_lock(&transport->recv_mutex);
1179 write_lock_bh(&sk->sk_callback_lock);
1180 transport->inet = NULL;
1181 transport->sock = NULL;
1182 transport->file = NULL;
1184 sk->sk_user_data = NULL;
1186 xs_restore_old_callbacks(transport, sk);
1187 xprt_clear_connected(xprt);
1188 write_unlock_bh(&sk->sk_callback_lock);
1189 xs_sock_reset_connection_flags(xprt);
1190 /* Reset stream record info */
1191 xs_stream_reset_connect(transport);
1192 mutex_unlock(&transport->recv_mutex);
1194 trace_rpc_socket_close(xprt, sock);
1195 fput(filp);
1197 xprt_disconnect_done(xprt);
1201 * xs_close - close a socket
1202 * @xprt: transport
1204 * This is used when all requests are complete; ie, no DRC state remains
1205 * on the server we want to save.
1207 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1208 * xs_reset_transport() zeroing the socket from underneath a writer.
1210 static void xs_close(struct rpc_xprt *xprt)
1212 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1214 dprintk("RPC: xs_close xprt %p\n", xprt);
1216 xs_reset_transport(transport);
1217 xprt->reestablish_timeout = 0;
1220 static void xs_inject_disconnect(struct rpc_xprt *xprt)
1222 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1223 xprt);
1224 xprt_disconnect_done(xprt);
1227 static void xs_xprt_free(struct rpc_xprt *xprt)
1229 xs_free_peer_addresses(xprt);
1230 xprt_free(xprt);
1234 * xs_destroy - prepare to shutdown a transport
1235 * @xprt: doomed transport
1238 static void xs_destroy(struct rpc_xprt *xprt)
1240 struct sock_xprt *transport = container_of(xprt,
1241 struct sock_xprt, xprt);
1242 dprintk("RPC: xs_destroy xprt %p\n", xprt);
1244 cancel_delayed_work_sync(&transport->connect_worker);
1245 xs_close(xprt);
1246 cancel_work_sync(&transport->recv_worker);
1247 cancel_work_sync(&transport->error_worker);
1248 xs_xprt_free(xprt);
1249 module_put(THIS_MODULE);
1253 * xs_udp_data_read_skb - receive callback for UDP sockets
1254 * @xprt: transport
1255 * @sk: socket
1256 * @skb: skbuff
1259 static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1260 struct sock *sk,
1261 struct sk_buff *skb)
1263 struct rpc_task *task;
1264 struct rpc_rqst *rovr;
1265 int repsize, copied;
1266 u32 _xid;
1267 __be32 *xp;
1269 repsize = skb->len;
1270 if (repsize < 4) {
1271 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
1272 return;
1275 /* Copy the XID from the skb... */
1276 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1277 if (xp == NULL)
1278 return;
1280 /* Look up and lock the request corresponding to the given XID */
1281 spin_lock(&xprt->queue_lock);
1282 rovr = xprt_lookup_rqst(xprt, *xp);
1283 if (!rovr)
1284 goto out_unlock;
1285 xprt_pin_rqst(rovr);
1286 xprt_update_rtt(rovr->rq_task);
1287 spin_unlock(&xprt->queue_lock);
1288 task = rovr->rq_task;
1290 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1291 copied = repsize;
1293 /* Suck it into the iovec, verify checksum if not done by hw. */
1294 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1295 spin_lock(&xprt->queue_lock);
1296 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1297 goto out_unpin;
1301 spin_lock(&xprt->transport_lock);
1302 xprt_adjust_cwnd(xprt, task, copied);
1303 spin_unlock(&xprt->transport_lock);
1304 spin_lock(&xprt->queue_lock);
1305 xprt_complete_rqst(task, copied);
1306 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1307 out_unpin:
1308 xprt_unpin_rqst(rovr);
1309 out_unlock:
1310 spin_unlock(&xprt->queue_lock);
1313 static void xs_udp_data_receive(struct sock_xprt *transport)
1315 struct sk_buff *skb;
1316 struct sock *sk;
1317 int err;
1319 mutex_lock(&transport->recv_mutex);
1320 sk = transport->inet;
1321 if (sk == NULL)
1322 goto out;
1323 for (;;) {
1324 skb = skb_recv_udp(sk, 0, 1, &err);
1325 if (skb == NULL)
1326 break;
1327 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1328 consume_skb(skb);
1329 cond_resched();
1331 xs_poll_check_readable(transport);
1332 out:
1333 mutex_unlock(&transport->recv_mutex);
1336 static void xs_udp_data_receive_workfn(struct work_struct *work)
1338 struct sock_xprt *transport =
1339 container_of(work, struct sock_xprt, recv_worker);
1340 unsigned int pflags = memalloc_nofs_save();
1342 xs_udp_data_receive(transport);
1343 memalloc_nofs_restore(pflags);
1347 * xs_data_ready - "data ready" callback for UDP sockets
1348 * @sk: socket with data to read
1351 static void xs_data_ready(struct sock *sk)
1353 struct rpc_xprt *xprt;
1355 read_lock_bh(&sk->sk_callback_lock);
1356 dprintk("RPC: xs_data_ready...\n");
1357 xprt = xprt_from_sock(sk);
1358 if (xprt != NULL) {
1359 struct sock_xprt *transport = container_of(xprt,
1360 struct sock_xprt, xprt);
1361 transport->old_data_ready(sk);
1362 /* Any data means we had a useful conversation, so
1363 * then we don't need to delay the next reconnect
1365 if (xprt->reestablish_timeout)
1366 xprt->reestablish_timeout = 0;
1367 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1368 queue_work(xprtiod_workqueue, &transport->recv_worker);
1370 read_unlock_bh(&sk->sk_callback_lock);
1374 * Helper function to force a TCP close if the server is sending
1375 * junk and/or it has put us in CLOSE_WAIT
1377 static void xs_tcp_force_close(struct rpc_xprt *xprt)
1379 xprt_force_disconnect(xprt);
1382 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1383 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1385 return PAGE_SIZE;
1387 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1390 * xs_tcp_state_change - callback to handle TCP socket state changes
1391 * @sk: socket whose state has changed
1394 static void xs_tcp_state_change(struct sock *sk)
1396 struct rpc_xprt *xprt;
1397 struct sock_xprt *transport;
1399 read_lock_bh(&sk->sk_callback_lock);
1400 if (!(xprt = xprt_from_sock(sk)))
1401 goto out;
1402 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1403 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1404 sk->sk_state, xprt_connected(xprt),
1405 sock_flag(sk, SOCK_DEAD),
1406 sock_flag(sk, SOCK_ZAPPED),
1407 sk->sk_shutdown);
1409 transport = container_of(xprt, struct sock_xprt, xprt);
1410 trace_rpc_socket_state_change(xprt, sk->sk_socket);
1411 switch (sk->sk_state) {
1412 case TCP_ESTABLISHED:
1413 if (!xprt_test_and_set_connected(xprt)) {
1414 xprt->connect_cookie++;
1415 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1416 xprt_clear_connecting(xprt);
1418 xprt->stat.connect_count++;
1419 xprt->stat.connect_time += (long)jiffies -
1420 xprt->stat.connect_start;
1421 xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1423 break;
1424 case TCP_FIN_WAIT1:
1425 /* The client initiated a shutdown of the socket */
1426 xprt->connect_cookie++;
1427 xprt->reestablish_timeout = 0;
1428 set_bit(XPRT_CLOSING, &xprt->state);
1429 smp_mb__before_atomic();
1430 clear_bit(XPRT_CONNECTED, &xprt->state);
1431 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1432 smp_mb__after_atomic();
1433 break;
1434 case TCP_CLOSE_WAIT:
1435 /* The server initiated a shutdown of the socket */
1436 xprt->connect_cookie++;
1437 clear_bit(XPRT_CONNECTED, &xprt->state);
1438 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1439 /* fall through */
1440 case TCP_CLOSING:
1442 * If the server closed down the connection, make sure that
1443 * we back off before reconnecting
1445 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1446 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1447 break;
1448 case TCP_LAST_ACK:
1449 set_bit(XPRT_CLOSING, &xprt->state);
1450 smp_mb__before_atomic();
1451 clear_bit(XPRT_CONNECTED, &xprt->state);
1452 smp_mb__after_atomic();
1453 break;
1454 case TCP_CLOSE:
1455 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1456 &transport->sock_state))
1457 xprt_clear_connecting(xprt);
1458 clear_bit(XPRT_CLOSING, &xprt->state);
1459 /* Trigger the socket release */
1460 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1462 out:
1463 read_unlock_bh(&sk->sk_callback_lock);
1466 static void xs_write_space(struct sock *sk)
1468 struct socket_wq *wq;
1469 struct sock_xprt *transport;
1470 struct rpc_xprt *xprt;
1472 if (!sk->sk_socket)
1473 return;
1474 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1476 if (unlikely(!(xprt = xprt_from_sock(sk))))
1477 return;
1478 transport = container_of(xprt, struct sock_xprt, xprt);
1479 rcu_read_lock();
1480 wq = rcu_dereference(sk->sk_wq);
1481 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1482 goto out;
1484 xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1485 sk->sk_write_pending--;
1486 out:
1487 rcu_read_unlock();
1491 * xs_udp_write_space - callback invoked when socket buffer space
1492 * becomes available
1493 * @sk: socket whose state has changed
1495 * Called when more output buffer space is available for this socket.
1496 * We try not to wake our writers until they can make "significant"
1497 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1498 * with a bunch of small requests.
1500 static void xs_udp_write_space(struct sock *sk)
1502 read_lock_bh(&sk->sk_callback_lock);
1504 /* from net/core/sock.c:sock_def_write_space */
1505 if (sock_writeable(sk))
1506 xs_write_space(sk);
1508 read_unlock_bh(&sk->sk_callback_lock);
1512 * xs_tcp_write_space - callback invoked when socket buffer space
1513 * becomes available
1514 * @sk: socket whose state has changed
1516 * Called when more output buffer space is available for this socket.
1517 * We try not to wake our writers until they can make "significant"
1518 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1519 * with a bunch of small requests.
1521 static void xs_tcp_write_space(struct sock *sk)
1523 read_lock_bh(&sk->sk_callback_lock);
1525 /* from net/core/stream.c:sk_stream_write_space */
1526 if (sk_stream_is_writeable(sk))
1527 xs_write_space(sk);
1529 read_unlock_bh(&sk->sk_callback_lock);
1532 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1534 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1535 struct sock *sk = transport->inet;
1537 if (transport->rcvsize) {
1538 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1539 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1541 if (transport->sndsize) {
1542 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1543 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1544 sk->sk_write_space(sk);
1549 * xs_udp_set_buffer_size - set send and receive limits
1550 * @xprt: generic transport
1551 * @sndsize: requested size of send buffer, in bytes
1552 * @rcvsize: requested size of receive buffer, in bytes
1554 * Set socket send and receive buffer size limits.
1556 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1558 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1560 transport->sndsize = 0;
1561 if (sndsize)
1562 transport->sndsize = sndsize + 1024;
1563 transport->rcvsize = 0;
1564 if (rcvsize)
1565 transport->rcvsize = rcvsize + 1024;
1567 xs_udp_do_set_buffer_size(xprt);
1571 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1572 * @xprt: controlling transport
1573 * @task: task that timed out
1575 * Adjust the congestion window after a retransmit timeout has occurred.
1577 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1579 spin_lock(&xprt->transport_lock);
1580 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1581 spin_unlock(&xprt->transport_lock);
1584 static int xs_get_random_port(void)
1586 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1587 unsigned short range;
1588 unsigned short rand;
1590 if (max < min)
1591 return -EADDRINUSE;
1592 range = max - min + 1;
1593 rand = (unsigned short) prandom_u32() % range;
1594 return rand + min;
1598 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1599 * @sock: socket
1601 * Note that this function has to be called on all sockets that share the
1602 * same port, and it must be called before binding.
1604 static void xs_sock_set_reuseport(struct socket *sock)
1606 int opt = 1;
1608 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1609 (char *)&opt, sizeof(opt));
1612 static unsigned short xs_sock_getport(struct socket *sock)
1614 struct sockaddr_storage buf;
1615 unsigned short port = 0;
1617 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
1618 goto out;
1619 switch (buf.ss_family) {
1620 case AF_INET6:
1621 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1622 break;
1623 case AF_INET:
1624 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1626 out:
1627 return port;
1631 * xs_set_port - reset the port number in the remote endpoint address
1632 * @xprt: generic transport
1633 * @port: new port number
1636 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1638 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1640 rpc_set_port(xs_addr(xprt), port);
1641 xs_update_peer_port(xprt);
1644 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1646 if (transport->srcport == 0 && transport->xprt.reuseport)
1647 transport->srcport = xs_sock_getport(sock);
1650 static int xs_get_srcport(struct sock_xprt *transport)
1652 int port = transport->srcport;
1654 if (port == 0 && transport->xprt.resvport)
1655 port = xs_get_random_port();
1656 return port;
1659 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1661 if (transport->srcport != 0)
1662 transport->srcport = 0;
1663 if (!transport->xprt.resvport)
1664 return 0;
1665 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1666 return xprt_max_resvport;
1667 return --port;
1669 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1671 struct sockaddr_storage myaddr;
1672 int err, nloop = 0;
1673 int port = xs_get_srcport(transport);
1674 unsigned short last;
1677 * If we are asking for any ephemeral port (i.e. port == 0 &&
1678 * transport->xprt.resvport == 0), don't bind. Let the local
1679 * port selection happen implicitly when the socket is used
1680 * (for example at connect time).
1682 * This ensures that we can continue to establish TCP
1683 * connections even when all local ephemeral ports are already
1684 * a part of some TCP connection. This makes no difference
1685 * for UDP sockets, but also doens't harm them.
1687 * If we're asking for any reserved port (i.e. port == 0 &&
1688 * transport->xprt.resvport == 1) xs_get_srcport above will
1689 * ensure that port is non-zero and we will bind as needed.
1691 if (port <= 0)
1692 return port;
1694 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1695 do {
1696 rpc_set_port((struct sockaddr *)&myaddr, port);
1697 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1698 transport->xprt.addrlen);
1699 if (err == 0) {
1700 transport->srcport = port;
1701 break;
1703 last = port;
1704 port = xs_next_srcport(transport, port);
1705 if (port > last)
1706 nloop++;
1707 } while (err == -EADDRINUSE && nloop != 2);
1709 if (myaddr.ss_family == AF_INET)
1710 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1711 &((struct sockaddr_in *)&myaddr)->sin_addr,
1712 port, err ? "failed" : "ok", err);
1713 else
1714 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1715 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1716 port, err ? "failed" : "ok", err);
1717 return err;
1721 * We don't support autobind on AF_LOCAL sockets
1723 static void xs_local_rpcbind(struct rpc_task *task)
1725 xprt_set_bound(task->tk_xprt);
1728 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1732 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1733 static struct lock_class_key xs_key[2];
1734 static struct lock_class_key xs_slock_key[2];
1736 static inline void xs_reclassify_socketu(struct socket *sock)
1738 struct sock *sk = sock->sk;
1740 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1741 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1744 static inline void xs_reclassify_socket4(struct socket *sock)
1746 struct sock *sk = sock->sk;
1748 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1749 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1752 static inline void xs_reclassify_socket6(struct socket *sock)
1754 struct sock *sk = sock->sk;
1756 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1757 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1760 static inline void xs_reclassify_socket(int family, struct socket *sock)
1762 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
1763 return;
1765 switch (family) {
1766 case AF_LOCAL:
1767 xs_reclassify_socketu(sock);
1768 break;
1769 case AF_INET:
1770 xs_reclassify_socket4(sock);
1771 break;
1772 case AF_INET6:
1773 xs_reclassify_socket6(sock);
1774 break;
1777 #else
1778 static inline void xs_reclassify_socket(int family, struct socket *sock)
1781 #endif
1783 static void xs_dummy_setup_socket(struct work_struct *work)
1787 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1788 struct sock_xprt *transport, int family, int type,
1789 int protocol, bool reuseport)
1791 struct file *filp;
1792 struct socket *sock;
1793 int err;
1795 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1796 if (err < 0) {
1797 dprintk("RPC: can't create %d transport socket (%d).\n",
1798 protocol, -err);
1799 goto out;
1801 xs_reclassify_socket(family, sock);
1803 if (reuseport)
1804 xs_sock_set_reuseport(sock);
1806 err = xs_bind(transport, sock);
1807 if (err) {
1808 sock_release(sock);
1809 goto out;
1812 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1813 if (IS_ERR(filp))
1814 return ERR_CAST(filp);
1815 transport->file = filp;
1817 return sock;
1818 out:
1819 return ERR_PTR(err);
1822 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1823 struct socket *sock)
1825 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1826 xprt);
1828 if (!transport->inet) {
1829 struct sock *sk = sock->sk;
1831 write_lock_bh(&sk->sk_callback_lock);
1833 xs_save_old_callbacks(transport, sk);
1835 sk->sk_user_data = xprt;
1836 sk->sk_data_ready = xs_data_ready;
1837 sk->sk_write_space = xs_udp_write_space;
1838 sock_set_flag(sk, SOCK_FASYNC);
1839 sk->sk_error_report = xs_error_report;
1841 xprt_clear_connected(xprt);
1843 /* Reset to new socket */
1844 transport->sock = sock;
1845 transport->inet = sk;
1847 write_unlock_bh(&sk->sk_callback_lock);
1850 xs_stream_start_connect(transport);
1852 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1856 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1857 * @transport: socket transport to connect
1859 static int xs_local_setup_socket(struct sock_xprt *transport)
1861 struct rpc_xprt *xprt = &transport->xprt;
1862 struct file *filp;
1863 struct socket *sock;
1864 int status;
1866 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1867 SOCK_STREAM, 0, &sock, 1);
1868 if (status < 0) {
1869 dprintk("RPC: can't create AF_LOCAL "
1870 "transport socket (%d).\n", -status);
1871 goto out;
1873 xs_reclassify_socket(AF_LOCAL, sock);
1875 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1876 if (IS_ERR(filp)) {
1877 status = PTR_ERR(filp);
1878 goto out;
1880 transport->file = filp;
1882 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1883 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1885 status = xs_local_finish_connecting(xprt, sock);
1886 trace_rpc_socket_connect(xprt, sock, status);
1887 switch (status) {
1888 case 0:
1889 dprintk("RPC: xprt %p connected to %s\n",
1890 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1891 xprt->stat.connect_count++;
1892 xprt->stat.connect_time += (long)jiffies -
1893 xprt->stat.connect_start;
1894 xprt_set_connected(xprt);
1895 case -ENOBUFS:
1896 break;
1897 case -ENOENT:
1898 dprintk("RPC: xprt %p: socket %s does not exist\n",
1899 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1900 break;
1901 case -ECONNREFUSED:
1902 dprintk("RPC: xprt %p: connection refused for %s\n",
1903 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1904 break;
1905 default:
1906 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1907 __func__, -status,
1908 xprt->address_strings[RPC_DISPLAY_ADDR]);
1911 out:
1912 xprt_clear_connecting(xprt);
1913 xprt_wake_pending_tasks(xprt, status);
1914 return status;
1917 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1919 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1920 int ret;
1922 if (RPC_IS_ASYNC(task)) {
1924 * We want the AF_LOCAL connect to be resolved in the
1925 * filesystem namespace of the process making the rpc
1926 * call. Thus we connect synchronously.
1928 * If we want to support asynchronous AF_LOCAL calls,
1929 * we'll need to figure out how to pass a namespace to
1930 * connect.
1932 task->tk_rpc_status = -ENOTCONN;
1933 rpc_exit(task, -ENOTCONN);
1934 return;
1936 ret = xs_local_setup_socket(transport);
1937 if (ret && !RPC_IS_SOFTCONN(task))
1938 msleep_interruptible(15000);
1941 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1943 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1944 * know that we have exclusive access to the socket), to guard against
1945 * races with xs_reset_transport.
1947 static void xs_set_memalloc(struct rpc_xprt *xprt)
1949 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1950 xprt);
1953 * If there's no sock, then we have nothing to set. The
1954 * reconnecting process will get it for us.
1956 if (!transport->inet)
1957 return;
1958 if (atomic_read(&xprt->swapper))
1959 sk_set_memalloc(transport->inet);
1963 * xs_enable_swap - Tag this transport as being used for swap.
1964 * @xprt: transport to tag
1966 * Take a reference to this transport on behalf of the rpc_clnt, and
1967 * optionally mark it for swapping if it wasn't already.
1969 static int
1970 xs_enable_swap(struct rpc_xprt *xprt)
1972 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1974 if (atomic_inc_return(&xprt->swapper) != 1)
1975 return 0;
1976 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1977 return -ERESTARTSYS;
1978 if (xs->inet)
1979 sk_set_memalloc(xs->inet);
1980 xprt_release_xprt(xprt, NULL);
1981 return 0;
1985 * xs_disable_swap - Untag this transport as being used for swap.
1986 * @xprt: transport to tag
1988 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
1989 * swapper refcount goes to 0, untag the socket as a memalloc socket.
1991 static void
1992 xs_disable_swap(struct rpc_xprt *xprt)
1994 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1996 if (!atomic_dec_and_test(&xprt->swapper))
1997 return;
1998 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1999 return;
2000 if (xs->inet)
2001 sk_clear_memalloc(xs->inet);
2002 xprt_release_xprt(xprt, NULL);
2004 #else
2005 static void xs_set_memalloc(struct rpc_xprt *xprt)
2009 static int
2010 xs_enable_swap(struct rpc_xprt *xprt)
2012 return -EINVAL;
2015 static void
2016 xs_disable_swap(struct rpc_xprt *xprt)
2019 #endif
2021 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2023 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2025 if (!transport->inet) {
2026 struct sock *sk = sock->sk;
2028 write_lock_bh(&sk->sk_callback_lock);
2030 xs_save_old_callbacks(transport, sk);
2032 sk->sk_user_data = xprt;
2033 sk->sk_data_ready = xs_data_ready;
2034 sk->sk_write_space = xs_udp_write_space;
2035 sock_set_flag(sk, SOCK_FASYNC);
2037 xprt_set_connected(xprt);
2039 /* Reset to new socket */
2040 transport->sock = sock;
2041 transport->inet = sk;
2043 xs_set_memalloc(xprt);
2045 write_unlock_bh(&sk->sk_callback_lock);
2047 xs_udp_do_set_buffer_size(xprt);
2049 xprt->stat.connect_start = jiffies;
2052 static void xs_udp_setup_socket(struct work_struct *work)
2054 struct sock_xprt *transport =
2055 container_of(work, struct sock_xprt, connect_worker.work);
2056 struct rpc_xprt *xprt = &transport->xprt;
2057 struct socket *sock;
2058 int status = -EIO;
2060 sock = xs_create_sock(xprt, transport,
2061 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2062 IPPROTO_UDP, false);
2063 if (IS_ERR(sock))
2064 goto out;
2066 dprintk("RPC: worker connecting xprt %p via %s to "
2067 "%s (port %s)\n", xprt,
2068 xprt->address_strings[RPC_DISPLAY_PROTO],
2069 xprt->address_strings[RPC_DISPLAY_ADDR],
2070 xprt->address_strings[RPC_DISPLAY_PORT]);
2072 xs_udp_finish_connecting(xprt, sock);
2073 trace_rpc_socket_connect(xprt, sock, 0);
2074 status = 0;
2075 out:
2076 xprt_clear_connecting(xprt);
2077 xprt_unlock_connect(xprt, transport);
2078 xprt_wake_pending_tasks(xprt, status);
2082 * xs_tcp_shutdown - gracefully shut down a TCP socket
2083 * @xprt: transport
2085 * Initiates a graceful shutdown of the TCP socket by calling the
2086 * equivalent of shutdown(SHUT_RDWR);
2088 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2090 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2091 struct socket *sock = transport->sock;
2092 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
2094 if (sock == NULL)
2095 return;
2096 switch (skst) {
2097 default:
2098 kernel_sock_shutdown(sock, SHUT_RDWR);
2099 trace_rpc_socket_shutdown(xprt, sock);
2100 break;
2101 case TCP_CLOSE:
2102 case TCP_TIME_WAIT:
2103 xs_reset_transport(transport);
2107 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2108 struct socket *sock)
2110 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2111 unsigned int keepidle;
2112 unsigned int keepcnt;
2113 unsigned int opt_on = 1;
2114 unsigned int timeo;
2116 spin_lock(&xprt->transport_lock);
2117 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2118 keepcnt = xprt->timeout->to_retries + 1;
2119 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2120 (xprt->timeout->to_retries + 1);
2121 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2122 spin_unlock(&xprt->transport_lock);
2124 /* TCP Keepalive options */
2125 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2126 (char *)&opt_on, sizeof(opt_on));
2127 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2128 (char *)&keepidle, sizeof(keepidle));
2129 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2130 (char *)&keepidle, sizeof(keepidle));
2131 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2132 (char *)&keepcnt, sizeof(keepcnt));
2134 /* TCP user timeout (see RFC5482) */
2135 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2136 (char *)&timeo, sizeof(timeo));
2139 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2140 unsigned long connect_timeout,
2141 unsigned long reconnect_timeout)
2143 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2144 struct rpc_timeout to;
2145 unsigned long initval;
2147 spin_lock(&xprt->transport_lock);
2148 if (reconnect_timeout < xprt->max_reconnect_timeout)
2149 xprt->max_reconnect_timeout = reconnect_timeout;
2150 if (connect_timeout < xprt->connect_timeout) {
2151 memcpy(&to, xprt->timeout, sizeof(to));
2152 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2153 /* Arbitrary lower limit */
2154 if (initval < XS_TCP_INIT_REEST_TO << 1)
2155 initval = XS_TCP_INIT_REEST_TO << 1;
2156 to.to_initval = initval;
2157 to.to_maxval = initval;
2158 memcpy(&transport->tcp_timeout, &to,
2159 sizeof(transport->tcp_timeout));
2160 xprt->timeout = &transport->tcp_timeout;
2161 xprt->connect_timeout = connect_timeout;
2163 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2164 spin_unlock(&xprt->transport_lock);
2167 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2169 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2170 int ret = -ENOTCONN;
2172 if (!transport->inet) {
2173 struct sock *sk = sock->sk;
2174 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
2176 /* Avoid temporary address, they are bad for long-lived
2177 * connections such as NFS mounts.
2178 * RFC4941, section 3.6 suggests that:
2179 * Individual applications, which have specific
2180 * knowledge about the normal duration of connections,
2181 * MAY override this as appropriate.
2183 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2184 (char *)&addr_pref, sizeof(addr_pref));
2186 xs_tcp_set_socket_timeouts(xprt, sock);
2188 write_lock_bh(&sk->sk_callback_lock);
2190 xs_save_old_callbacks(transport, sk);
2192 sk->sk_user_data = xprt;
2193 sk->sk_data_ready = xs_data_ready;
2194 sk->sk_state_change = xs_tcp_state_change;
2195 sk->sk_write_space = xs_tcp_write_space;
2196 sock_set_flag(sk, SOCK_FASYNC);
2197 sk->sk_error_report = xs_error_report;
2199 /* socket options */
2200 sock_reset_flag(sk, SOCK_LINGER);
2201 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2203 xprt_clear_connected(xprt);
2205 /* Reset to new socket */
2206 transport->sock = sock;
2207 transport->inet = sk;
2209 write_unlock_bh(&sk->sk_callback_lock);
2212 if (!xprt_bound(xprt))
2213 goto out;
2215 xs_set_memalloc(xprt);
2217 xs_stream_start_connect(transport);
2219 /* Tell the socket layer to start connecting... */
2220 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2221 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2222 switch (ret) {
2223 case 0:
2224 xs_set_srcport(transport, sock);
2225 /* fall through */
2226 case -EINPROGRESS:
2227 /* SYN_SENT! */
2228 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2229 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2230 break;
2231 case -EADDRNOTAVAIL:
2232 /* Source port number is unavailable. Try a new one! */
2233 transport->srcport = 0;
2235 out:
2236 return ret;
2240 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2241 * @work: queued work item
2243 * Invoked by a work queue tasklet.
2245 static void xs_tcp_setup_socket(struct work_struct *work)
2247 struct sock_xprt *transport =
2248 container_of(work, struct sock_xprt, connect_worker.work);
2249 struct socket *sock = transport->sock;
2250 struct rpc_xprt *xprt = &transport->xprt;
2251 int status = -EIO;
2253 if (!sock) {
2254 sock = xs_create_sock(xprt, transport,
2255 xs_addr(xprt)->sa_family, SOCK_STREAM,
2256 IPPROTO_TCP, true);
2257 if (IS_ERR(sock)) {
2258 status = PTR_ERR(sock);
2259 goto out;
2263 dprintk("RPC: worker connecting xprt %p via %s to "
2264 "%s (port %s)\n", xprt,
2265 xprt->address_strings[RPC_DISPLAY_PROTO],
2266 xprt->address_strings[RPC_DISPLAY_ADDR],
2267 xprt->address_strings[RPC_DISPLAY_PORT]);
2269 status = xs_tcp_finish_connecting(xprt, sock);
2270 trace_rpc_socket_connect(xprt, sock, status);
2271 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2272 xprt, -status, xprt_connected(xprt),
2273 sock->sk->sk_state);
2274 switch (status) {
2275 default:
2276 printk("%s: connect returned unhandled error %d\n",
2277 __func__, status);
2278 /* fall through */
2279 case -EADDRNOTAVAIL:
2280 /* We're probably in TIME_WAIT. Get rid of existing socket,
2281 * and retry
2283 xs_tcp_force_close(xprt);
2284 break;
2285 case 0:
2286 case -EINPROGRESS:
2287 case -EALREADY:
2288 xprt_unlock_connect(xprt, transport);
2289 return;
2290 case -EINVAL:
2291 /* Happens, for instance, if the user specified a link
2292 * local IPv6 address without a scope-id.
2294 case -ECONNREFUSED:
2295 case -ECONNRESET:
2296 case -ENETDOWN:
2297 case -ENETUNREACH:
2298 case -EHOSTUNREACH:
2299 case -EADDRINUSE:
2300 case -ENOBUFS:
2302 * xs_tcp_force_close() wakes tasks with -EIO.
2303 * We need to wake them first to ensure the
2304 * correct error code.
2306 xprt_wake_pending_tasks(xprt, status);
2307 xs_tcp_force_close(xprt);
2308 goto out;
2310 status = -EAGAIN;
2311 out:
2312 xprt_clear_connecting(xprt);
2313 xprt_unlock_connect(xprt, transport);
2314 xprt_wake_pending_tasks(xprt, status);
2318 * xs_connect - connect a socket to a remote endpoint
2319 * @xprt: pointer to transport structure
2320 * @task: address of RPC task that manages state of connect request
2322 * TCP: If the remote end dropped the connection, delay reconnecting.
2324 * UDP socket connects are synchronous, but we use a work queue anyway
2325 * to guarantee that even unprivileged user processes can set up a
2326 * socket on a privileged port.
2328 * If a UDP socket connect fails, the delay behavior here prevents
2329 * retry floods (hard mounts).
2331 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2333 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2334 unsigned long delay = 0;
2336 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2338 if (transport->sock != NULL) {
2339 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2340 "seconds\n",
2341 xprt, xprt->reestablish_timeout / HZ);
2343 /* Start by resetting any existing state */
2344 xs_reset_transport(transport);
2346 delay = xprt_reconnect_delay(xprt);
2347 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
2349 } else
2350 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2352 queue_delayed_work(xprtiod_workqueue,
2353 &transport->connect_worker,
2354 delay);
2357 static void xs_wake_disconnect(struct sock_xprt *transport)
2359 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2360 xs_tcp_force_close(&transport->xprt);
2363 static void xs_wake_write(struct sock_xprt *transport)
2365 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2366 xprt_write_space(&transport->xprt);
2369 static void xs_wake_error(struct sock_xprt *transport)
2371 int sockerr;
2373 if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2374 return;
2375 mutex_lock(&transport->recv_mutex);
2376 if (transport->sock == NULL)
2377 goto out;
2378 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2379 goto out;
2380 sockerr = xchg(&transport->xprt_err, 0);
2381 if (sockerr < 0)
2382 xprt_wake_pending_tasks(&transport->xprt, sockerr);
2383 out:
2384 mutex_unlock(&transport->recv_mutex);
2387 static void xs_wake_pending(struct sock_xprt *transport)
2389 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2390 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2393 static void xs_error_handle(struct work_struct *work)
2395 struct sock_xprt *transport = container_of(work,
2396 struct sock_xprt, error_worker);
2398 xs_wake_disconnect(transport);
2399 xs_wake_write(transport);
2400 xs_wake_error(transport);
2401 xs_wake_pending(transport);
2405 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2406 * @xprt: rpc_xprt struct containing statistics
2407 * @seq: output file
2410 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2412 long idle_time = 0;
2414 if (xprt_connected(xprt))
2415 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2417 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2418 "%llu %llu %lu %llu %llu\n",
2419 xprt->stat.bind_count,
2420 xprt->stat.connect_count,
2421 xprt->stat.connect_time / HZ,
2422 idle_time,
2423 xprt->stat.sends,
2424 xprt->stat.recvs,
2425 xprt->stat.bad_xids,
2426 xprt->stat.req_u,
2427 xprt->stat.bklog_u,
2428 xprt->stat.max_slots,
2429 xprt->stat.sending_u,
2430 xprt->stat.pending_u);
2434 * xs_udp_print_stats - display UDP socket-specifc stats
2435 * @xprt: rpc_xprt struct containing statistics
2436 * @seq: output file
2439 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2441 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2443 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2444 "%lu %llu %llu\n",
2445 transport->srcport,
2446 xprt->stat.bind_count,
2447 xprt->stat.sends,
2448 xprt->stat.recvs,
2449 xprt->stat.bad_xids,
2450 xprt->stat.req_u,
2451 xprt->stat.bklog_u,
2452 xprt->stat.max_slots,
2453 xprt->stat.sending_u,
2454 xprt->stat.pending_u);
2458 * xs_tcp_print_stats - display TCP socket-specifc stats
2459 * @xprt: rpc_xprt struct containing statistics
2460 * @seq: output file
2463 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2465 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2466 long idle_time = 0;
2468 if (xprt_connected(xprt))
2469 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2471 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2472 "%llu %llu %lu %llu %llu\n",
2473 transport->srcport,
2474 xprt->stat.bind_count,
2475 xprt->stat.connect_count,
2476 xprt->stat.connect_time / HZ,
2477 idle_time,
2478 xprt->stat.sends,
2479 xprt->stat.recvs,
2480 xprt->stat.bad_xids,
2481 xprt->stat.req_u,
2482 xprt->stat.bklog_u,
2483 xprt->stat.max_slots,
2484 xprt->stat.sending_u,
2485 xprt->stat.pending_u);
2489 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2490 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2491 * to use the server side send routines.
2493 static int bc_malloc(struct rpc_task *task)
2495 struct rpc_rqst *rqst = task->tk_rqstp;
2496 size_t size = rqst->rq_callsize;
2497 struct page *page;
2498 struct rpc_buffer *buf;
2500 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2501 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2502 size);
2503 return -EINVAL;
2506 page = alloc_page(GFP_KERNEL);
2507 if (!page)
2508 return -ENOMEM;
2510 buf = page_address(page);
2511 buf->len = PAGE_SIZE;
2513 rqst->rq_buffer = buf->data;
2514 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
2515 return 0;
2519 * Free the space allocated in the bc_alloc routine
2521 static void bc_free(struct rpc_task *task)
2523 void *buffer = task->tk_rqstp->rq_buffer;
2524 struct rpc_buffer *buf;
2526 buf = container_of(buffer, struct rpc_buffer, data);
2527 free_page((unsigned long)buf);
2530 static int bc_sendto(struct rpc_rqst *req)
2532 struct xdr_buf *xdr = &req->rq_snd_buf;
2533 struct sock_xprt *transport =
2534 container_of(req->rq_xprt, struct sock_xprt, xprt);
2535 struct msghdr msg = {
2536 .msg_flags = 0,
2538 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2539 (u32)xdr->len);
2540 unsigned int sent = 0;
2541 int err;
2543 req->rq_xtime = ktime_get();
2544 err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2545 xdr_free_bvec(xdr);
2546 if (err < 0 || sent != (xdr->len + sizeof(marker)))
2547 return -EAGAIN;
2548 return sent;
2552 * The send routine. Borrows from svc_send
2554 static int bc_send_request(struct rpc_rqst *req)
2556 struct svc_xprt *xprt;
2557 int len;
2560 * Get the server socket associated with this callback xprt
2562 xprt = req->rq_xprt->bc_xprt;
2565 * Grab the mutex to serialize data as the connection is shared
2566 * with the fore channel
2568 mutex_lock(&xprt->xpt_mutex);
2569 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2570 len = -ENOTCONN;
2571 else
2572 len = bc_sendto(req);
2573 mutex_unlock(&xprt->xpt_mutex);
2575 if (len > 0)
2576 len = 0;
2578 return len;
2582 * The close routine. Since this is client initiated, we do nothing
2585 static void bc_close(struct rpc_xprt *xprt)
2590 * The xprt destroy routine. Again, because this connection is client
2591 * initiated, we do nothing
2594 static void bc_destroy(struct rpc_xprt *xprt)
2596 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2598 xs_xprt_free(xprt);
2599 module_put(THIS_MODULE);
2602 static const struct rpc_xprt_ops xs_local_ops = {
2603 .reserve_xprt = xprt_reserve_xprt,
2604 .release_xprt = xprt_release_xprt,
2605 .alloc_slot = xprt_alloc_slot,
2606 .free_slot = xprt_free_slot,
2607 .rpcbind = xs_local_rpcbind,
2608 .set_port = xs_local_set_port,
2609 .connect = xs_local_connect,
2610 .buf_alloc = rpc_malloc,
2611 .buf_free = rpc_free,
2612 .prepare_request = xs_stream_prepare_request,
2613 .send_request = xs_local_send_request,
2614 .wait_for_reply_request = xprt_wait_for_reply_request_def,
2615 .close = xs_close,
2616 .destroy = xs_destroy,
2617 .print_stats = xs_local_print_stats,
2618 .enable_swap = xs_enable_swap,
2619 .disable_swap = xs_disable_swap,
2622 static const struct rpc_xprt_ops xs_udp_ops = {
2623 .set_buffer_size = xs_udp_set_buffer_size,
2624 .reserve_xprt = xprt_reserve_xprt_cong,
2625 .release_xprt = xprt_release_xprt_cong,
2626 .alloc_slot = xprt_alloc_slot,
2627 .free_slot = xprt_free_slot,
2628 .rpcbind = rpcb_getport_async,
2629 .set_port = xs_set_port,
2630 .connect = xs_connect,
2631 .buf_alloc = rpc_malloc,
2632 .buf_free = rpc_free,
2633 .send_request = xs_udp_send_request,
2634 .wait_for_reply_request = xprt_wait_for_reply_request_rtt,
2635 .timer = xs_udp_timer,
2636 .release_request = xprt_release_rqst_cong,
2637 .close = xs_close,
2638 .destroy = xs_destroy,
2639 .print_stats = xs_udp_print_stats,
2640 .enable_swap = xs_enable_swap,
2641 .disable_swap = xs_disable_swap,
2642 .inject_disconnect = xs_inject_disconnect,
2645 static const struct rpc_xprt_ops xs_tcp_ops = {
2646 .reserve_xprt = xprt_reserve_xprt,
2647 .release_xprt = xprt_release_xprt,
2648 .alloc_slot = xprt_alloc_slot,
2649 .free_slot = xprt_free_slot,
2650 .rpcbind = rpcb_getport_async,
2651 .set_port = xs_set_port,
2652 .connect = xs_connect,
2653 .buf_alloc = rpc_malloc,
2654 .buf_free = rpc_free,
2655 .prepare_request = xs_stream_prepare_request,
2656 .send_request = xs_tcp_send_request,
2657 .wait_for_reply_request = xprt_wait_for_reply_request_def,
2658 .close = xs_tcp_shutdown,
2659 .destroy = xs_destroy,
2660 .set_connect_timeout = xs_tcp_set_connect_timeout,
2661 .print_stats = xs_tcp_print_stats,
2662 .enable_swap = xs_enable_swap,
2663 .disable_swap = xs_disable_swap,
2664 .inject_disconnect = xs_inject_disconnect,
2665 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2666 .bc_setup = xprt_setup_bc,
2667 .bc_maxpayload = xs_tcp_bc_maxpayload,
2668 .bc_num_slots = xprt_bc_max_slots,
2669 .bc_free_rqst = xprt_free_bc_rqst,
2670 .bc_destroy = xprt_destroy_bc,
2671 #endif
2675 * The rpc_xprt_ops for the server backchannel
2678 static const struct rpc_xprt_ops bc_tcp_ops = {
2679 .reserve_xprt = xprt_reserve_xprt,
2680 .release_xprt = xprt_release_xprt,
2681 .alloc_slot = xprt_alloc_slot,
2682 .free_slot = xprt_free_slot,
2683 .buf_alloc = bc_malloc,
2684 .buf_free = bc_free,
2685 .send_request = bc_send_request,
2686 .wait_for_reply_request = xprt_wait_for_reply_request_def,
2687 .close = bc_close,
2688 .destroy = bc_destroy,
2689 .print_stats = xs_tcp_print_stats,
2690 .enable_swap = xs_enable_swap,
2691 .disable_swap = xs_disable_swap,
2692 .inject_disconnect = xs_inject_disconnect,
2695 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2697 static const struct sockaddr_in sin = {
2698 .sin_family = AF_INET,
2699 .sin_addr.s_addr = htonl(INADDR_ANY),
2701 static const struct sockaddr_in6 sin6 = {
2702 .sin6_family = AF_INET6,
2703 .sin6_addr = IN6ADDR_ANY_INIT,
2706 switch (family) {
2707 case AF_LOCAL:
2708 break;
2709 case AF_INET:
2710 memcpy(sap, &sin, sizeof(sin));
2711 break;
2712 case AF_INET6:
2713 memcpy(sap, &sin6, sizeof(sin6));
2714 break;
2715 default:
2716 dprintk("RPC: %s: Bad address family\n", __func__);
2717 return -EAFNOSUPPORT;
2719 return 0;
2722 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2723 unsigned int slot_table_size,
2724 unsigned int max_slot_table_size)
2726 struct rpc_xprt *xprt;
2727 struct sock_xprt *new;
2729 if (args->addrlen > sizeof(xprt->addr)) {
2730 dprintk("RPC: xs_setup_xprt: address too large\n");
2731 return ERR_PTR(-EBADF);
2734 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2735 max_slot_table_size);
2736 if (xprt == NULL) {
2737 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2738 "rpc_xprt\n");
2739 return ERR_PTR(-ENOMEM);
2742 new = container_of(xprt, struct sock_xprt, xprt);
2743 mutex_init(&new->recv_mutex);
2744 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2745 xprt->addrlen = args->addrlen;
2746 if (args->srcaddr)
2747 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2748 else {
2749 int err;
2750 err = xs_init_anyaddr(args->dstaddr->sa_family,
2751 (struct sockaddr *)&new->srcaddr);
2752 if (err != 0) {
2753 xprt_free(xprt);
2754 return ERR_PTR(err);
2758 return xprt;
2761 static const struct rpc_timeout xs_local_default_timeout = {
2762 .to_initval = 10 * HZ,
2763 .to_maxval = 10 * HZ,
2764 .to_retries = 2,
2768 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2769 * @args: rpc transport creation arguments
2771 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2773 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2775 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2776 struct sock_xprt *transport;
2777 struct rpc_xprt *xprt;
2778 struct rpc_xprt *ret;
2780 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2781 xprt_max_tcp_slot_table_entries);
2782 if (IS_ERR(xprt))
2783 return xprt;
2784 transport = container_of(xprt, struct sock_xprt, xprt);
2786 xprt->prot = 0;
2787 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2789 xprt->bind_timeout = XS_BIND_TO;
2790 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2791 xprt->idle_timeout = XS_IDLE_DISC_TO;
2793 xprt->ops = &xs_local_ops;
2794 xprt->timeout = &xs_local_default_timeout;
2796 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2797 INIT_WORK(&transport->error_worker, xs_error_handle);
2798 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
2800 switch (sun->sun_family) {
2801 case AF_LOCAL:
2802 if (sun->sun_path[0] != '/') {
2803 dprintk("RPC: bad AF_LOCAL address: %s\n",
2804 sun->sun_path);
2805 ret = ERR_PTR(-EINVAL);
2806 goto out_err;
2808 xprt_set_bound(xprt);
2809 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2810 ret = ERR_PTR(xs_local_setup_socket(transport));
2811 if (ret)
2812 goto out_err;
2813 break;
2814 default:
2815 ret = ERR_PTR(-EAFNOSUPPORT);
2816 goto out_err;
2819 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2820 xprt->address_strings[RPC_DISPLAY_ADDR]);
2822 if (try_module_get(THIS_MODULE))
2823 return xprt;
2824 ret = ERR_PTR(-EINVAL);
2825 out_err:
2826 xs_xprt_free(xprt);
2827 return ret;
2830 static const struct rpc_timeout xs_udp_default_timeout = {
2831 .to_initval = 5 * HZ,
2832 .to_maxval = 30 * HZ,
2833 .to_increment = 5 * HZ,
2834 .to_retries = 5,
2838 * xs_setup_udp - Set up transport to use a UDP socket
2839 * @args: rpc transport creation arguments
2842 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2844 struct sockaddr *addr = args->dstaddr;
2845 struct rpc_xprt *xprt;
2846 struct sock_xprt *transport;
2847 struct rpc_xprt *ret;
2849 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2850 xprt_udp_slot_table_entries);
2851 if (IS_ERR(xprt))
2852 return xprt;
2853 transport = container_of(xprt, struct sock_xprt, xprt);
2855 xprt->prot = IPPROTO_UDP;
2856 /* XXX: header size can vary due to auth type, IPv6, etc. */
2857 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2859 xprt->bind_timeout = XS_BIND_TO;
2860 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2861 xprt->idle_timeout = XS_IDLE_DISC_TO;
2863 xprt->ops = &xs_udp_ops;
2865 xprt->timeout = &xs_udp_default_timeout;
2867 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
2868 INIT_WORK(&transport->error_worker, xs_error_handle);
2869 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2871 switch (addr->sa_family) {
2872 case AF_INET:
2873 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2874 xprt_set_bound(xprt);
2876 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2877 break;
2878 case AF_INET6:
2879 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2880 xprt_set_bound(xprt);
2882 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2883 break;
2884 default:
2885 ret = ERR_PTR(-EAFNOSUPPORT);
2886 goto out_err;
2889 if (xprt_bound(xprt))
2890 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2891 xprt->address_strings[RPC_DISPLAY_ADDR],
2892 xprt->address_strings[RPC_DISPLAY_PORT],
2893 xprt->address_strings[RPC_DISPLAY_PROTO]);
2894 else
2895 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2896 xprt->address_strings[RPC_DISPLAY_ADDR],
2897 xprt->address_strings[RPC_DISPLAY_PROTO]);
2899 if (try_module_get(THIS_MODULE))
2900 return xprt;
2901 ret = ERR_PTR(-EINVAL);
2902 out_err:
2903 xs_xprt_free(xprt);
2904 return ret;
2907 static const struct rpc_timeout xs_tcp_default_timeout = {
2908 .to_initval = 60 * HZ,
2909 .to_maxval = 60 * HZ,
2910 .to_retries = 2,
2914 * xs_setup_tcp - Set up transport to use a TCP socket
2915 * @args: rpc transport creation arguments
2918 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2920 struct sockaddr *addr = args->dstaddr;
2921 struct rpc_xprt *xprt;
2922 struct sock_xprt *transport;
2923 struct rpc_xprt *ret;
2924 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2926 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2927 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2929 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2930 max_slot_table_size);
2931 if (IS_ERR(xprt))
2932 return xprt;
2933 transport = container_of(xprt, struct sock_xprt, xprt);
2935 xprt->prot = IPPROTO_TCP;
2936 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2938 xprt->bind_timeout = XS_BIND_TO;
2939 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2940 xprt->idle_timeout = XS_IDLE_DISC_TO;
2942 xprt->ops = &xs_tcp_ops;
2943 xprt->timeout = &xs_tcp_default_timeout;
2945 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
2946 xprt->connect_timeout = xprt->timeout->to_initval *
2947 (xprt->timeout->to_retries + 1);
2949 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2950 INIT_WORK(&transport->error_worker, xs_error_handle);
2951 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2953 switch (addr->sa_family) {
2954 case AF_INET:
2955 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2956 xprt_set_bound(xprt);
2958 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2959 break;
2960 case AF_INET6:
2961 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2962 xprt_set_bound(xprt);
2964 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2965 break;
2966 default:
2967 ret = ERR_PTR(-EAFNOSUPPORT);
2968 goto out_err;
2971 if (xprt_bound(xprt))
2972 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2973 xprt->address_strings[RPC_DISPLAY_ADDR],
2974 xprt->address_strings[RPC_DISPLAY_PORT],
2975 xprt->address_strings[RPC_DISPLAY_PROTO]);
2976 else
2977 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2978 xprt->address_strings[RPC_DISPLAY_ADDR],
2979 xprt->address_strings[RPC_DISPLAY_PROTO]);
2981 if (try_module_get(THIS_MODULE))
2982 return xprt;
2983 ret = ERR_PTR(-EINVAL);
2984 out_err:
2985 xs_xprt_free(xprt);
2986 return ret;
2990 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2991 * @args: rpc transport creation arguments
2994 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2996 struct sockaddr *addr = args->dstaddr;
2997 struct rpc_xprt *xprt;
2998 struct sock_xprt *transport;
2999 struct svc_sock *bc_sock;
3000 struct rpc_xprt *ret;
3002 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3003 xprt_tcp_slot_table_entries);
3004 if (IS_ERR(xprt))
3005 return xprt;
3006 transport = container_of(xprt, struct sock_xprt, xprt);
3008 xprt->prot = IPPROTO_TCP;
3009 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3010 xprt->timeout = &xs_tcp_default_timeout;
3012 /* backchannel */
3013 xprt_set_bound(xprt);
3014 xprt->bind_timeout = 0;
3015 xprt->reestablish_timeout = 0;
3016 xprt->idle_timeout = 0;
3018 xprt->ops = &bc_tcp_ops;
3020 switch (addr->sa_family) {
3021 case AF_INET:
3022 xs_format_peer_addresses(xprt, "tcp",
3023 RPCBIND_NETID_TCP);
3024 break;
3025 case AF_INET6:
3026 xs_format_peer_addresses(xprt, "tcp",
3027 RPCBIND_NETID_TCP6);
3028 break;
3029 default:
3030 ret = ERR_PTR(-EAFNOSUPPORT);
3031 goto out_err;
3034 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3035 xprt->address_strings[RPC_DISPLAY_ADDR],
3036 xprt->address_strings[RPC_DISPLAY_PORT],
3037 xprt->address_strings[RPC_DISPLAY_PROTO]);
3040 * Once we've associated a backchannel xprt with a connection,
3041 * we want to keep it around as long as the connection lasts,
3042 * in case we need to start using it for a backchannel again;
3043 * this reference won't be dropped until bc_xprt is destroyed.
3045 xprt_get(xprt);
3046 args->bc_xprt->xpt_bc_xprt = xprt;
3047 xprt->bc_xprt = args->bc_xprt;
3048 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3049 transport->sock = bc_sock->sk_sock;
3050 transport->inet = bc_sock->sk_sk;
3053 * Since we don't want connections for the backchannel, we set
3054 * the xprt status to connected
3056 xprt_set_connected(xprt);
3058 if (try_module_get(THIS_MODULE))
3059 return xprt;
3061 args->bc_xprt->xpt_bc_xprt = NULL;
3062 args->bc_xprt->xpt_bc_xps = NULL;
3063 xprt_put(xprt);
3064 ret = ERR_PTR(-EINVAL);
3065 out_err:
3066 xs_xprt_free(xprt);
3067 return ret;
3070 static struct xprt_class xs_local_transport = {
3071 .list = LIST_HEAD_INIT(xs_local_transport.list),
3072 .name = "named UNIX socket",
3073 .owner = THIS_MODULE,
3074 .ident = XPRT_TRANSPORT_LOCAL,
3075 .setup = xs_setup_local,
3078 static struct xprt_class xs_udp_transport = {
3079 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3080 .name = "udp",
3081 .owner = THIS_MODULE,
3082 .ident = XPRT_TRANSPORT_UDP,
3083 .setup = xs_setup_udp,
3086 static struct xprt_class xs_tcp_transport = {
3087 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3088 .name = "tcp",
3089 .owner = THIS_MODULE,
3090 .ident = XPRT_TRANSPORT_TCP,
3091 .setup = xs_setup_tcp,
3094 static struct xprt_class xs_bc_tcp_transport = {
3095 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3096 .name = "tcp NFSv4.1 backchannel",
3097 .owner = THIS_MODULE,
3098 .ident = XPRT_TRANSPORT_BC_TCP,
3099 .setup = xs_setup_bc_tcp,
3103 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3106 int init_socket_xprt(void)
3108 if (!sunrpc_table_header)
3109 sunrpc_table_header = register_sysctl_table(sunrpc_table);
3111 xprt_register_transport(&xs_local_transport);
3112 xprt_register_transport(&xs_udp_transport);
3113 xprt_register_transport(&xs_tcp_transport);
3114 xprt_register_transport(&xs_bc_tcp_transport);
3116 return 0;
3120 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3123 void cleanup_socket_xprt(void)
3125 if (sunrpc_table_header) {
3126 unregister_sysctl_table(sunrpc_table_header);
3127 sunrpc_table_header = NULL;
3130 xprt_unregister_transport(&xs_local_transport);
3131 xprt_unregister_transport(&xs_udp_transport);
3132 xprt_unregister_transport(&xs_tcp_transport);
3133 xprt_unregister_transport(&xs_bc_tcp_transport);
3136 static int param_set_uint_minmax(const char *val,
3137 const struct kernel_param *kp,
3138 unsigned int min, unsigned int max)
3140 unsigned int num;
3141 int ret;
3143 if (!val)
3144 return -EINVAL;
3145 ret = kstrtouint(val, 0, &num);
3146 if (ret)
3147 return ret;
3148 if (num < min || num > max)
3149 return -EINVAL;
3150 *((unsigned int *)kp->arg) = num;
3151 return 0;
3154 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3156 return param_set_uint_minmax(val, kp,
3157 RPC_MIN_RESVPORT,
3158 RPC_MAX_RESVPORT);
3161 static const struct kernel_param_ops param_ops_portnr = {
3162 .set = param_set_portnr,
3163 .get = param_get_uint,
3166 #define param_check_portnr(name, p) \
3167 __param_check(name, p, unsigned int);
3169 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3170 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3172 static int param_set_slot_table_size(const char *val,
3173 const struct kernel_param *kp)
3175 return param_set_uint_minmax(val, kp,
3176 RPC_MIN_SLOT_TABLE,
3177 RPC_MAX_SLOT_TABLE);
3180 static const struct kernel_param_ops param_ops_slot_table_size = {
3181 .set = param_set_slot_table_size,
3182 .get = param_get_uint,
3185 #define param_check_slot_table_size(name, p) \
3186 __param_check(name, p, unsigned int);
3188 static int param_set_max_slot_table_size(const char *val,
3189 const struct kernel_param *kp)
3191 return param_set_uint_minmax(val, kp,
3192 RPC_MIN_SLOT_TABLE,
3193 RPC_MAX_SLOT_TABLE_LIMIT);
3196 static const struct kernel_param_ops param_ops_max_slot_table_size = {
3197 .set = param_set_max_slot_table_size,
3198 .get = param_get_uint,
3201 #define param_check_max_slot_table_size(name, p) \
3202 __param_check(name, p, unsigned int);
3204 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3205 slot_table_size, 0644);
3206 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3207 max_slot_table_size, 0644);
3208 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3209 slot_table_size, 0644);