1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/svcsock.c
5 * These are the RPC server socket internals.
7 * The server scheduling algorithm does not always distribute the load
8 * evenly when servicing a single client. May need to modify the
9 * svc_xprt_enqueue procedure...
11 * TCP support is largely untested and may be a little slow. The problem
12 * is that we currently do two separate recvfrom's, one for the 4-byte
13 * record length, and the second for the actual record. This could possibly
14 * be improved by always reading a minimum size of around 100 bytes and
15 * tucking any superfluous bytes away in a temporary store. Still, that
16 * leaves write requests out in the rain. An alternative may be to peek at
17 * the first skb in the queue, and if it matches the next TCP sequence
18 * number, to extract the record marker. Yuck.
20 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/fcntl.h>
28 #include <linux/net.h>
30 #include <linux/inet.h>
31 #include <linux/udp.h>
32 #include <linux/tcp.h>
33 #include <linux/unistd.h>
34 #include <linux/slab.h>
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/file.h>
38 #include <linux/freezer.h>
40 #include <net/checksum.h>
45 #include <net/tcp_states.h>
46 #include <linux/uaccess.h>
47 #include <asm/ioctls.h>
49 #include <linux/sunrpc/types.h>
50 #include <linux/sunrpc/clnt.h>
51 #include <linux/sunrpc/xdr.h>
52 #include <linux/sunrpc/msg_prot.h>
53 #include <linux/sunrpc/svcsock.h>
54 #include <linux/sunrpc/stats.h>
55 #include <linux/sunrpc/xprt.h>
57 #include <trace/events/sunrpc.h>
62 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
65 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
67 static int svc_udp_recvfrom(struct svc_rqst *);
68 static int svc_udp_sendto(struct svc_rqst *);
69 static void svc_sock_detach(struct svc_xprt *);
70 static void svc_tcp_sock_detach(struct svc_xprt *);
71 static void svc_sock_free(struct svc_xprt *);
73 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
74 struct net *, struct sockaddr *,
76 #ifdef CONFIG_DEBUG_LOCK_ALLOC
77 static struct lock_class_key svc_key[2];
78 static struct lock_class_key svc_slock_key[2];
80 static void svc_reclassify_socket(struct socket *sock)
82 struct sock *sk = sock->sk;
84 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
87 switch (sk->sk_family) {
89 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
91 "sk_xprt.xpt_lock-AF_INET-NFSD",
96 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
98 "sk_xprt.xpt_lock-AF_INET6-NFSD",
107 static void svc_reclassify_socket(struct socket *sock)
113 * svc_tcp_release_rqst - Release transport-related resources
114 * @rqstp: request structure with resources to be released
117 static void svc_tcp_release_rqst(struct svc_rqst *rqstp)
119 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
122 struct svc_sock *svsk =
123 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
125 rqstp->rq_xprt_ctxt = NULL;
126 skb_free_datagram_locked(svsk->sk_sk, skb);
131 * svc_udp_release_rqst - Release transport-related resources
132 * @rqstp: request structure with resources to be released
135 static void svc_udp_release_rqst(struct svc_rqst *rqstp)
137 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
140 rqstp->rq_xprt_ctxt = NULL;
145 union svc_pktinfo_u {
146 struct in_pktinfo pkti;
147 struct in6_pktinfo pkti6;
149 #define SVC_PKTINFO_SPACE \
150 CMSG_SPACE(sizeof(union svc_pktinfo_u))
152 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
154 struct svc_sock *svsk =
155 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
156 switch (svsk->sk_sk->sk_family) {
158 struct in_pktinfo *pki = CMSG_DATA(cmh);
160 cmh->cmsg_level = SOL_IP;
161 cmh->cmsg_type = IP_PKTINFO;
162 pki->ipi_ifindex = 0;
163 pki->ipi_spec_dst.s_addr =
164 svc_daddr_in(rqstp)->sin_addr.s_addr;
165 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
170 struct in6_pktinfo *pki = CMSG_DATA(cmh);
171 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
173 cmh->cmsg_level = SOL_IPV6;
174 cmh->cmsg_type = IPV6_PKTINFO;
175 pki->ipi6_ifindex = daddr->sin6_scope_id;
176 pki->ipi6_addr = daddr->sin6_addr;
177 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
183 static int svc_sock_read_payload(struct svc_rqst *rqstp, unsigned int offset,
190 * Report socket names for nfsdfs
192 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
194 const struct sock *sk = svsk->sk_sk;
195 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
199 switch (sk->sk_family) {
201 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
203 &inet_sk(sk)->inet_rcv_saddr,
204 inet_sk(sk)->inet_num);
206 #if IS_ENABLED(CONFIG_IPV6)
208 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
210 &sk->sk_v6_rcv_saddr,
211 inet_sk(sk)->inet_num);
215 len = snprintf(buf, remaining, "*unknown-%d*\n",
219 if (len >= remaining) {
221 return -ENAMETOOLONG;
226 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
227 static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
229 struct bvec_iter bi = {
234 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
235 for_each_bvec(bv, bvec, bi, bi)
236 flush_dcache_page(bv.bv_page);
239 static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
246 * Read from @rqstp's transport socket. The incoming message fills whole
247 * pages in @rqstp's rq_pages array until the last page of the message
248 * has been received into a partial page.
250 static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
253 struct svc_sock *svsk =
254 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
255 struct bio_vec *bvec = rqstp->rq_bvec;
256 struct msghdr msg = { NULL };
261 rqstp->rq_xprt_hlen = 0;
263 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
265 for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE) {
266 bvec[i].bv_page = rqstp->rq_pages[i];
267 bvec[i].bv_len = PAGE_SIZE;
268 bvec[i].bv_offset = 0;
270 rqstp->rq_respages = &rqstp->rq_pages[i];
271 rqstp->rq_next_page = rqstp->rq_respages + 1;
273 iov_iter_bvec(&msg.msg_iter, READ, bvec, i, buflen);
275 iov_iter_advance(&msg.msg_iter, seek);
278 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
280 svc_flush_bvec(bvec, len, seek);
282 /* If we read a full record, then assume there may be more
283 * data to read (stream based sockets only!)
286 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
292 * Set socket snd and rcv buffer lengths
294 static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
296 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
297 struct socket *sock = svsk->sk_sock;
299 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
302 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
303 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
304 sock->sk->sk_write_space(sock->sk);
305 release_sock(sock->sk);
308 static void svc_sock_secure_port(struct svc_rqst *rqstp)
310 if (svc_port_is_privileged(svc_addr(rqstp)))
311 set_bit(RQ_SECURE, &rqstp->rq_flags);
313 clear_bit(RQ_SECURE, &rqstp->rq_flags);
317 * INET callback when data has been received on the socket.
319 static void svc_data_ready(struct sock *sk)
321 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
324 /* Refer to svc_setup_socket() for details. */
327 trace_svcsock_data_ready(&svsk->sk_xprt, 0);
328 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
329 svc_xprt_enqueue(&svsk->sk_xprt);
334 * INET callback when space is newly available on the socket.
336 static void svc_write_space(struct sock *sk)
338 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
341 /* Refer to svc_setup_socket() for details. */
343 trace_svcsock_write_space(&svsk->sk_xprt, 0);
344 svsk->sk_owspace(sk);
345 svc_xprt_enqueue(&svsk->sk_xprt);
349 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
351 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
353 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
355 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
358 static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
360 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
362 sock_no_linger(svsk->sk_sock->sk);
366 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
368 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
371 struct in_pktinfo *pki = CMSG_DATA(cmh);
372 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
374 if (cmh->cmsg_type != IP_PKTINFO)
377 daddr->sin_family = AF_INET;
378 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
383 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
385 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
388 struct in6_pktinfo *pki = CMSG_DATA(cmh);
389 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
391 if (cmh->cmsg_type != IPV6_PKTINFO)
394 daddr->sin6_family = AF_INET6;
395 daddr->sin6_addr = pki->ipi6_addr;
396 daddr->sin6_scope_id = pki->ipi6_ifindex;
401 * Copy the UDP datagram's destination address to the rqstp structure.
402 * The 'destination' address in this case is the address to which the
403 * peer sent the datagram, i.e. our local address. For multihomed
404 * hosts, this can change from msg to msg. Note that only the IP
405 * address changes, the port number should remain the same.
407 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
410 switch (cmh->cmsg_level) {
412 return svc_udp_get_dest_address4(rqstp, cmh);
414 return svc_udp_get_dest_address6(rqstp, cmh);
421 * svc_udp_recvfrom - Receive a datagram from a UDP socket.
422 * @rqstp: request structure into which to receive an RPC Call
424 * Called in a loop when XPT_DATA has been set.
427 * On success, the number of bytes in a received RPC Call, or
428 * %0 if a complete RPC Call message was not ready to return
430 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
432 struct svc_sock *svsk =
433 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
434 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
438 long all[SVC_PKTINFO_SPACE / sizeof(long)];
440 struct cmsghdr *cmh = &buffer.hdr;
441 struct msghdr msg = {
442 .msg_name = svc_addr(rqstp),
444 .msg_controllen = sizeof(buffer),
445 .msg_flags = MSG_DONTWAIT,
450 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
451 /* udp sockets need large rcvbuf as all pending
452 * requests are still in that buffer. sndbuf must
453 * also be large enough that there is enough space
454 * for one reply per thread. We count all threads
455 * rather than threads in a particular pool, which
456 * provides an upper bound on the number of threads
457 * which will access the socket.
459 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
461 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
462 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
463 0, 0, MSG_PEEK | MSG_DONTWAIT);
466 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err);
470 len = svc_addr_len(svc_addr(rqstp));
471 rqstp->rq_addrlen = len;
472 if (skb->tstamp == 0) {
473 skb->tstamp = ktime_get_real();
474 /* Don't enable netstamp, sunrpc doesn't
475 need that much accuracy */
477 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
478 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
481 rqstp->rq_arg.len = len;
482 trace_svcsock_udp_recv(&svsk->sk_xprt, len);
484 rqstp->rq_prot = IPPROTO_UDP;
486 if (!svc_udp_get_dest_address(rqstp, cmh))
488 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
490 if (skb_is_nonlinear(skb)) {
491 /* we have to copy */
493 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
498 /* we can use it in-place */
499 rqstp->rq_arg.head[0].iov_base = skb->data;
500 rqstp->rq_arg.head[0].iov_len = len;
501 if (skb_checksum_complete(skb))
503 rqstp->rq_xprt_ctxt = skb;
506 rqstp->rq_arg.page_base = 0;
507 if (len <= rqstp->rq_arg.head[0].iov_len) {
508 rqstp->rq_arg.head[0].iov_len = len;
509 rqstp->rq_arg.page_len = 0;
510 rqstp->rq_respages = rqstp->rq_pages+1;
512 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
513 rqstp->rq_respages = rqstp->rq_pages + 1 +
514 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
516 rqstp->rq_next_page = rqstp->rq_respages+1;
519 serv->sv_stats->netudpcnt++;
524 if (err != -EAGAIN) {
525 /* possibly an icmp error */
526 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
528 trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
531 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
532 cmh->cmsg_level, cmh->cmsg_type);
542 * svc_udp_sendto - Send out a reply on a UDP socket
543 * @rqstp: completed svc_rqst
545 * xpt_mutex ensures @rqstp's whole message is written to the socket
546 * without interruption.
548 * Returns the number of bytes sent, or a negative errno.
550 static int svc_udp_sendto(struct svc_rqst *rqstp)
552 struct svc_xprt *xprt = rqstp->rq_xprt;
553 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
554 struct xdr_buf *xdr = &rqstp->rq_res;
557 long all[SVC_PKTINFO_SPACE / sizeof(long)];
559 struct cmsghdr *cmh = &buffer.hdr;
560 struct msghdr msg = {
561 .msg_name = &rqstp->rq_addr,
562 .msg_namelen = rqstp->rq_addrlen,
564 .msg_controllen = sizeof(buffer),
566 unsigned int uninitialized_var(sent);
569 svc_udp_release_rqst(rqstp);
571 svc_set_cmsg_data(rqstp, cmh);
573 mutex_lock(&xprt->xpt_mutex);
575 if (svc_xprt_is_dead(xprt))
578 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
580 if (err == -ECONNREFUSED) {
581 /* ICMP error on earlier request. */
582 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
585 trace_svcsock_udp_send(xprt, err);
587 mutex_unlock(&xprt->xpt_mutex);
593 mutex_unlock(&xprt->xpt_mutex);
597 static int svc_udp_has_wspace(struct svc_xprt *xprt)
599 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
600 struct svc_serv *serv = xprt->xpt_server;
601 unsigned long required;
604 * Set the SOCK_NOSPACE flag before checking the available
607 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
608 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
609 if (required*2 > sock_wspace(svsk->sk_sk))
611 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
615 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
621 static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
625 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
627 struct sockaddr *sa, int salen,
630 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
633 static const struct svc_xprt_ops svc_udp_ops = {
634 .xpo_create = svc_udp_create,
635 .xpo_recvfrom = svc_udp_recvfrom,
636 .xpo_sendto = svc_udp_sendto,
637 .xpo_read_payload = svc_sock_read_payload,
638 .xpo_release_rqst = svc_udp_release_rqst,
639 .xpo_detach = svc_sock_detach,
640 .xpo_free = svc_sock_free,
641 .xpo_has_wspace = svc_udp_has_wspace,
642 .xpo_accept = svc_udp_accept,
643 .xpo_secure_port = svc_sock_secure_port,
644 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
647 static struct svc_xprt_class svc_udp_class = {
649 .xcl_owner = THIS_MODULE,
650 .xcl_ops = &svc_udp_ops,
651 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
652 .xcl_ident = XPRT_TRANSPORT_UDP,
655 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
657 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
658 &svsk->sk_xprt, serv);
659 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
660 svsk->sk_sk->sk_data_ready = svc_data_ready;
661 svsk->sk_sk->sk_write_space = svc_write_space;
663 /* initialise setting must have enough space to
664 * receive and respond to one request.
665 * svc_udp_recvfrom will re-adjust if necessary
667 svc_sock_setbufsize(svsk, 3);
669 /* data might have come in before data_ready set up */
670 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
671 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
673 /* make sure we get destination address info */
674 switch (svsk->sk_sk->sk_family) {
676 ip_sock_set_pktinfo(svsk->sk_sock->sk);
679 ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
687 * A data_ready event on a listening socket means there's a connection
688 * pending. Do not use state_change as a substitute for it.
690 static void svc_tcp_listen_data_ready(struct sock *sk)
692 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
695 /* Refer to svc_setup_socket() for details. */
701 * This callback may called twice when a new connection
702 * is established as a child socket inherits everything
703 * from a parent LISTEN socket.
704 * 1) data_ready method of the parent socket will be called
705 * when one of child sockets become ESTABLISHED.
706 * 2) data_ready method of the child socket may be called
707 * when it receives data before the socket is accepted.
708 * In case of 2, we should ignore it silently.
710 if (sk->sk_state == TCP_LISTEN) {
712 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
713 svc_xprt_enqueue(&svsk->sk_xprt);
719 * A state change on a connected socket means it's dying or dead.
721 static void svc_tcp_state_change(struct sock *sk)
723 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
726 /* Refer to svc_setup_socket() for details. */
729 trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
730 if (sk->sk_state != TCP_ESTABLISHED) {
731 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
732 svc_xprt_enqueue(&svsk->sk_xprt);
738 * Accept a TCP connection
740 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
742 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
743 struct sockaddr_storage addr;
744 struct sockaddr *sin = (struct sockaddr *) &addr;
745 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
746 struct socket *sock = svsk->sk_sock;
747 struct socket *newsock;
748 struct svc_sock *newsvsk;
754 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
755 err = kernel_accept(sock, &newsock, O_NONBLOCK);
758 printk(KERN_WARNING "%s: no more sockets!\n",
760 else if (err != -EAGAIN)
761 net_warn_ratelimited("%s: accept failed (err %d)!\n",
762 serv->sv_name, -err);
763 trace_svcsock_accept_err(xprt, serv->sv_name, err);
766 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
768 err = kernel_getpeername(newsock, sin);
770 trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
771 goto failed; /* aborted connection or whatever */
775 /* Reset the inherited callbacks before calling svc_setup_socket */
776 newsock->sk->sk_state_change = svsk->sk_ostate;
777 newsock->sk->sk_data_ready = svsk->sk_odata;
778 newsock->sk->sk_write_space = svsk->sk_owspace;
780 /* make sure that a write doesn't block forever when
783 newsock->sk->sk_sndtimeo = HZ*30;
785 newsvsk = svc_setup_socket(serv, newsock,
786 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
789 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
790 err = kernel_getsockname(newsock, sin);
792 if (unlikely(err < 0))
793 slen = offsetof(struct sockaddr, sa_data);
794 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
796 if (sock_is_loopback(newsock->sk))
797 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
799 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
801 serv->sv_stats->nettcpconn++;
803 return &newsvsk->sk_xprt;
806 sock_release(newsock);
810 static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
811 struct svc_rqst *rqstp)
813 size_t len = svsk->sk_datalen;
814 unsigned int i, npages;
818 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
819 for (i = 0; i < npages; i++) {
820 if (rqstp->rq_pages[i] != NULL)
821 put_page(rqstp->rq_pages[i]);
822 BUG_ON(svsk->sk_pages[i] == NULL);
823 rqstp->rq_pages[i] = svsk->sk_pages[i];
824 svsk->sk_pages[i] = NULL;
826 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
830 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
832 unsigned int i, len, npages;
834 if (svsk->sk_datalen == 0)
836 len = svsk->sk_datalen;
837 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
838 for (i = 0; i < npages; i++) {
839 svsk->sk_pages[i] = rqstp->rq_pages[i];
840 rqstp->rq_pages[i] = NULL;
844 static void svc_tcp_clear_pages(struct svc_sock *svsk)
846 unsigned int i, len, npages;
848 if (svsk->sk_datalen == 0)
850 len = svsk->sk_datalen;
851 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
852 for (i = 0; i < npages; i++) {
853 if (svsk->sk_pages[i] == NULL) {
857 put_page(svsk->sk_pages[i]);
858 svsk->sk_pages[i] = NULL;
862 svsk->sk_datalen = 0;
866 * Receive fragment record header into sk_marker.
868 static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
869 struct svc_rqst *rqstp)
873 /* If we haven't gotten the record length yet,
874 * get the next four bytes.
876 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
877 struct msghdr msg = { NULL };
880 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
881 iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
883 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, want);
884 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
887 svsk->sk_tcplen += len;
889 /* call again to read the remaining bytes */
892 trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
893 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
894 svsk->sk_xprt.xpt_server->sv_max_mesg)
897 return svc_sock_reclen(svsk);
900 net_notice_ratelimited("svc: %s %s RPC fragment too large: %d\n",
901 __func__, svsk->sk_xprt.xpt_server->sv_name,
902 svc_sock_reclen(svsk));
903 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
908 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
910 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
911 struct rpc_rqst *req = NULL;
912 struct kvec *src, *dst;
913 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
922 spin_lock(&bc_xprt->queue_lock);
923 req = xprt_lookup_rqst(bc_xprt, xid);
925 goto unlock_notfound;
927 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
929 * XXX!: cheating for now! Only copying HEAD.
930 * But we know this is good enough for now (in fact, for any
931 * callback reply in the forseeable future).
933 dst = &req->rq_private_buf.head[0];
934 src = &rqstp->rq_arg.head[0];
935 if (dst->iov_len < src->iov_len)
936 goto unlock_eagain; /* whatever; just giving up. */
937 memcpy(dst->iov_base, src->iov_base, src->iov_len);
938 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
939 rqstp->rq_arg.len = 0;
940 spin_unlock(&bc_xprt->queue_lock);
944 "%s: Got unrecognized reply: "
945 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
946 __func__, ntohl(calldir),
947 bc_xprt, ntohl(xid));
949 spin_unlock(&bc_xprt->queue_lock);
953 static void svc_tcp_fragment_received(struct svc_sock *svsk)
955 /* If we have more data, signal svc_xprt_enqueue() to try again */
957 svsk->sk_marker = xdr_zero;
961 * svc_tcp_recvfrom - Receive data from a TCP socket
962 * @rqstp: request structure into which to receive an RPC Call
964 * Called in a loop when XPT_DATA has been set.
966 * Read the 4-byte stream record marker, then use the record length
967 * in that marker to set up exactly the resources needed to receive
968 * the next RPC message into @rqstp.
971 * On success, the number of bytes in a received RPC Call, or
972 * %0 if a complete RPC Call message was not ready to return
974 * The zero return case handles partial receives and callback Replies.
975 * The state of a partial receive is preserved in the svc_sock for
976 * the next call to svc_tcp_recvfrom.
978 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
980 struct svc_sock *svsk =
981 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
982 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
988 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
989 len = svc_tcp_read_marker(svsk, rqstp);
993 base = svc_tcp_restore_pages(svsk, rqstp);
994 want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
995 len = svc_tcp_read_msg(rqstp, base + want, base);
997 trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
998 svsk->sk_tcplen += len;
999 svsk->sk_datalen += len;
1001 if (len != want || !svc_sock_final_rec(svsk))
1002 goto err_incomplete;
1003 if (svsk->sk_datalen < 8)
1006 rqstp->rq_arg.len = svsk->sk_datalen;
1007 rqstp->rq_arg.page_base = 0;
1008 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1009 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1010 rqstp->rq_arg.page_len = 0;
1012 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1014 rqstp->rq_xprt_ctxt = NULL;
1015 rqstp->rq_prot = IPPROTO_TCP;
1016 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1017 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1019 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1021 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1024 len = receive_cb_reply(svsk, rqstp);
1026 /* Reset TCP read info */
1027 svsk->sk_datalen = 0;
1028 svc_tcp_fragment_received(svsk);
1033 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1035 serv->sv_stats->nettcpcnt++;
1037 return rqstp->rq_arg.len;
1040 svc_tcp_save_pages(svsk, rqstp);
1041 if (len < 0 && len != -EAGAIN)
1044 svc_tcp_fragment_received(svsk);
1046 trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
1047 svc_sock_reclen(svsk),
1048 svsk->sk_tcplen - sizeof(rpc_fraghdr));
1053 trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
1056 svsk->sk_datalen = 0;
1058 trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
1059 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1061 return 0; /* record not complete */
1065 * svc_tcp_sendto - Send out a reply on a TCP socket
1066 * @rqstp: completed svc_rqst
1068 * xpt_mutex ensures @rqstp's whole message is written to the socket
1069 * without interruption.
1071 * Returns the number of bytes sent, or a negative errno.
1073 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1075 struct svc_xprt *xprt = rqstp->rq_xprt;
1076 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1077 struct xdr_buf *xdr = &rqstp->rq_res;
1078 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1080 struct msghdr msg = {
1083 unsigned int uninitialized_var(sent);
1086 svc_tcp_release_rqst(rqstp);
1088 mutex_lock(&xprt->xpt_mutex);
1089 if (svc_xprt_is_dead(xprt))
1091 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, marker, &sent);
1093 trace_svcsock_tcp_send(xprt, err < 0 ? err : sent);
1094 if (err < 0 || sent != (xdr->len + sizeof(marker)))
1096 mutex_unlock(&xprt->xpt_mutex);
1100 mutex_unlock(&xprt->xpt_mutex);
1103 pr_notice("rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1104 xprt->xpt_server->sv_name,
1105 (err < 0) ? "got error" : "sent",
1106 (err < 0) ? err : sent, xdr->len);
1107 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1108 svc_xprt_enqueue(xprt);
1109 mutex_unlock(&xprt->xpt_mutex);
1113 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1115 struct sockaddr *sa, int salen,
1118 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1121 static const struct svc_xprt_ops svc_tcp_ops = {
1122 .xpo_create = svc_tcp_create,
1123 .xpo_recvfrom = svc_tcp_recvfrom,
1124 .xpo_sendto = svc_tcp_sendto,
1125 .xpo_read_payload = svc_sock_read_payload,
1126 .xpo_release_rqst = svc_tcp_release_rqst,
1127 .xpo_detach = svc_tcp_sock_detach,
1128 .xpo_free = svc_sock_free,
1129 .xpo_has_wspace = svc_tcp_has_wspace,
1130 .xpo_accept = svc_tcp_accept,
1131 .xpo_secure_port = svc_sock_secure_port,
1132 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1135 static struct svc_xprt_class svc_tcp_class = {
1137 .xcl_owner = THIS_MODULE,
1138 .xcl_ops = &svc_tcp_ops,
1139 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1140 .xcl_ident = XPRT_TRANSPORT_TCP,
1143 void svc_init_xprt_sock(void)
1145 svc_reg_xprt_class(&svc_tcp_class);
1146 svc_reg_xprt_class(&svc_udp_class);
1149 void svc_cleanup_xprt_sock(void)
1151 svc_unreg_xprt_class(&svc_tcp_class);
1152 svc_unreg_xprt_class(&svc_udp_class);
1155 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1157 struct sock *sk = svsk->sk_sk;
1159 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1160 &svsk->sk_xprt, serv);
1161 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1162 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
1163 if (sk->sk_state == TCP_LISTEN) {
1164 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
1165 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1166 sk->sk_data_ready = svc_tcp_listen_data_ready;
1167 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1169 sk->sk_state_change = svc_tcp_state_change;
1170 sk->sk_data_ready = svc_data_ready;
1171 sk->sk_write_space = svc_write_space;
1173 svsk->sk_marker = xdr_zero;
1174 svsk->sk_tcplen = 0;
1175 svsk->sk_datalen = 0;
1176 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1178 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1180 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1181 switch (sk->sk_state) {
1183 case TCP_ESTABLISHED:
1186 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1191 void svc_sock_update_bufs(struct svc_serv *serv)
1194 * The number of server threads has changed. Update
1195 * rcvbuf and sndbuf accordingly on all sockets
1197 struct svc_sock *svsk;
1199 spin_lock_bh(&serv->sv_lock);
1200 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1201 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1202 spin_unlock_bh(&serv->sv_lock);
1204 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1207 * Initialize socket for RPC use and create svc_sock struct
1209 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1210 struct socket *sock,
1213 struct svc_sock *svsk;
1215 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1218 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1220 return ERR_PTR(-ENOMEM);
1224 /* Register socket with portmapper */
1226 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1228 ntohs(inet_sk(inet)->inet_sport));
1232 return ERR_PTR(err);
1235 svsk->sk_sock = sock;
1237 svsk->sk_ostate = inet->sk_state_change;
1238 svsk->sk_odata = inet->sk_data_ready;
1239 svsk->sk_owspace = inet->sk_write_space;
1241 * This barrier is necessary in order to prevent race condition
1242 * with svc_data_ready(), svc_listen_data_ready() and others
1243 * when calling callbacks above.
1246 inet->sk_user_data = svsk;
1248 /* Initialize the socket */
1249 if (sock->type == SOCK_DGRAM)
1250 svc_udp_init(svsk, serv);
1252 svc_tcp_init(svsk, serv);
1254 trace_svcsock_new_socket(sock);
1258 bool svc_alien_sock(struct net *net, int fd)
1261 struct socket *sock = sockfd_lookup(fd, &err);
1266 if (sock_net(sock->sk) != net)
1272 EXPORT_SYMBOL_GPL(svc_alien_sock);
1275 * svc_addsock - add a listener socket to an RPC service
1276 * @serv: pointer to RPC service to which to add a new listener
1277 * @fd: file descriptor of the new listener
1278 * @name_return: pointer to buffer to fill in with name of listener
1279 * @len: size of the buffer
1282 * Fills in socket name and returns positive length of name if successful.
1283 * Name is terminated with '\n'. On error, returns a negative errno
1286 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1287 const size_t len, const struct cred *cred)
1290 struct socket *so = sockfd_lookup(fd, &err);
1291 struct svc_sock *svsk = NULL;
1292 struct sockaddr_storage addr;
1293 struct sockaddr *sin = (struct sockaddr *)&addr;
1298 err = -EAFNOSUPPORT;
1299 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1301 err = -EPROTONOSUPPORT;
1302 if (so->sk->sk_protocol != IPPROTO_TCP &&
1303 so->sk->sk_protocol != IPPROTO_UDP)
1306 if (so->state > SS_UNCONNECTED)
1309 if (!try_module_get(THIS_MODULE))
1311 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1313 module_put(THIS_MODULE);
1314 err = PTR_ERR(svsk);
1317 salen = kernel_getsockname(svsk->sk_sock, sin);
1319 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1320 svsk->sk_xprt.xpt_cred = get_cred(cred);
1321 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1322 return svc_one_sock_name(svsk, name_return, len);
1327 EXPORT_SYMBOL_GPL(svc_addsock);
1330 * Create socket for RPC service.
1332 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1335 struct sockaddr *sin, int len,
1338 struct svc_sock *svsk;
1339 struct socket *sock;
1342 struct sockaddr_storage addr;
1343 struct sockaddr *newsin = (struct sockaddr *)&addr;
1347 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1348 printk(KERN_WARNING "svc: only UDP and TCP "
1349 "sockets supported\n");
1350 return ERR_PTR(-EINVAL);
1353 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1354 switch (sin->sa_family) {
1362 return ERR_PTR(-EINVAL);
1365 error = __sock_create(net, family, type, protocol, &sock, 1);
1367 return ERR_PTR(error);
1369 svc_reclassify_socket(sock);
1372 * If this is an PF_INET6 listener, we want to avoid
1373 * getting requests from IPv4 remotes. Those should
1374 * be shunted to a PF_INET listener via rpcbind.
1376 if (family == PF_INET6)
1377 ip6_sock_set_v6only(sock->sk);
1378 if (type == SOCK_STREAM)
1379 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1380 error = kernel_bind(sock, sin, len);
1384 error = kernel_getsockname(sock, newsin);
1389 if (protocol == IPPROTO_TCP) {
1390 if ((error = kernel_listen(sock, 64)) < 0)
1394 svsk = svc_setup_socket(serv, sock, flags);
1396 error = PTR_ERR(svsk);
1399 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1400 return (struct svc_xprt *)svsk;
1403 return ERR_PTR(error);
1407 * Detach the svc_sock from the socket so that no
1408 * more callbacks occur.
1410 static void svc_sock_detach(struct svc_xprt *xprt)
1412 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1413 struct sock *sk = svsk->sk_sk;
1415 /* put back the old socket callbacks */
1417 sk->sk_state_change = svsk->sk_ostate;
1418 sk->sk_data_ready = svsk->sk_odata;
1419 sk->sk_write_space = svsk->sk_owspace;
1420 sk->sk_user_data = NULL;
1425 * Disconnect the socket, and reset the callbacks
1427 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1429 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1431 svc_sock_detach(xprt);
1433 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1434 svc_tcp_clear_pages(svsk);
1435 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1440 * Free the svc_sock's socket resources and the svc_sock itself.
1442 static void svc_sock_free(struct svc_xprt *xprt)
1444 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1446 if (svsk->sk_sock->file)
1447 sockfd_put(svsk->sk_sock);
1449 sock_release(svsk->sk_sock);