2 * linux/net/sunrpc/svcsock.c
4 * These are the RPC server socket internals.
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
8 * svc_xprt_enqueue procedure...
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/fcntl.h>
27 #include <linux/net.h>
29 #include <linux/inet.h>
30 #include <linux/udp.h>
31 #include <linux/tcp.h>
32 #include <linux/unistd.h>
33 #include <linux/slab.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/file.h>
37 #include <linux/freezer.h>
39 #include <net/checksum.h>
43 #include <net/tcp_states.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
46 #include <trace/events/skb.h>
48 #include <linux/sunrpc/types.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/xdr.h>
51 #include <linux/sunrpc/msg_prot.h>
52 #include <linux/sunrpc/svcsock.h>
53 #include <linux/sunrpc/stats.h>
54 #include <linux/sunrpc/xprt.h>
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
61 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
63 static void svc_udp_data_ready(struct sock
*);
64 static int svc_udp_recvfrom(struct svc_rqst
*);
65 static int svc_udp_sendto(struct svc_rqst
*);
66 static void svc_sock_detach(struct svc_xprt
*);
67 static void svc_tcp_sock_detach(struct svc_xprt
*);
68 static void svc_sock_free(struct svc_xprt
*);
70 static struct svc_xprt
*svc_create_socket(struct svc_serv
*, int,
71 struct net
*, struct sockaddr
*,
73 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
74 static struct svc_xprt
*svc_bc_create_socket(struct svc_serv
*, int,
75 struct net
*, struct sockaddr
*,
77 static void svc_bc_sock_free(struct svc_xprt
*xprt
);
78 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
80 #ifdef CONFIG_DEBUG_LOCK_ALLOC
81 static struct lock_class_key svc_key
[2];
82 static struct lock_class_key svc_slock_key
[2];
84 static void svc_reclassify_socket(struct socket
*sock
)
86 struct sock
*sk
= sock
->sk
;
88 if (WARN_ON_ONCE(!sock_allow_reclassification(sk
)))
91 switch (sk
->sk_family
) {
93 sock_lock_init_class_and_name(sk
, "slock-AF_INET-NFSD",
95 "sk_xprt.xpt_lock-AF_INET-NFSD",
100 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-NFSD",
102 "sk_xprt.xpt_lock-AF_INET6-NFSD",
111 static void svc_reclassify_socket(struct socket
*sock
)
117 * Release an skbuff after use
119 static void svc_release_skb(struct svc_rqst
*rqstp
)
121 struct sk_buff
*skb
= rqstp
->rq_xprt_ctxt
;
124 struct svc_sock
*svsk
=
125 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
126 rqstp
->rq_xprt_ctxt
= NULL
;
128 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
129 skb_free_datagram_locked(svsk
->sk_sk
, skb
);
133 union svc_pktinfo_u
{
134 struct in_pktinfo pkti
;
135 struct in6_pktinfo pkti6
;
137 #define SVC_PKTINFO_SPACE \
138 CMSG_SPACE(sizeof(union svc_pktinfo_u))
140 static void svc_set_cmsg_data(struct svc_rqst
*rqstp
, struct cmsghdr
*cmh
)
142 struct svc_sock
*svsk
=
143 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
144 switch (svsk
->sk_sk
->sk_family
) {
146 struct in_pktinfo
*pki
= CMSG_DATA(cmh
);
148 cmh
->cmsg_level
= SOL_IP
;
149 cmh
->cmsg_type
= IP_PKTINFO
;
150 pki
->ipi_ifindex
= 0;
151 pki
->ipi_spec_dst
.s_addr
=
152 svc_daddr_in(rqstp
)->sin_addr
.s_addr
;
153 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
158 struct in6_pktinfo
*pki
= CMSG_DATA(cmh
);
159 struct sockaddr_in6
*daddr
= svc_daddr_in6(rqstp
);
161 cmh
->cmsg_level
= SOL_IPV6
;
162 cmh
->cmsg_type
= IPV6_PKTINFO
;
163 pki
->ipi6_ifindex
= daddr
->sin6_scope_id
;
164 pki
->ipi6_addr
= daddr
->sin6_addr
;
165 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
172 * send routine intended to be shared by the fore- and back-channel
174 int svc_send_common(struct socket
*sock
, struct xdr_buf
*xdr
,
175 struct page
*headpage
, unsigned long headoffset
,
176 struct page
*tailpage
, unsigned long tailoffset
)
180 struct page
**ppage
= xdr
->pages
;
181 size_t base
= xdr
->page_base
;
182 unsigned int pglen
= xdr
->page_len
;
183 unsigned int flags
= MSG_MORE
| MSG_SENDPAGE_NOTLAST
;
190 if (slen
== xdr
->head
[0].iov_len
)
192 len
= kernel_sendpage(sock
, headpage
, headoffset
,
193 xdr
->head
[0].iov_len
, flags
);
194 if (len
!= xdr
->head
[0].iov_len
)
196 slen
-= xdr
->head
[0].iov_len
;
201 size
= PAGE_SIZE
- base
< pglen
? PAGE_SIZE
- base
: pglen
;
205 result
= kernel_sendpage(sock
, *ppage
, base
, size
, flags
);
212 size
= PAGE_SIZE
< pglen
? PAGE_SIZE
: pglen
;
218 if (xdr
->tail
[0].iov_len
) {
219 result
= kernel_sendpage(sock
, tailpage
, tailoffset
,
220 xdr
->tail
[0].iov_len
, 0);
231 * Generic sendto routine
233 static int svc_sendto(struct svc_rqst
*rqstp
, struct xdr_buf
*xdr
)
235 struct svc_sock
*svsk
=
236 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
237 struct socket
*sock
= svsk
->sk_sock
;
240 long all
[SVC_PKTINFO_SPACE
/ sizeof(long)];
242 struct cmsghdr
*cmh
= &buffer
.hdr
;
244 unsigned long tailoff
;
245 unsigned long headoff
;
246 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
248 if (rqstp
->rq_prot
== IPPROTO_UDP
) {
249 struct msghdr msg
= {
250 .msg_name
= &rqstp
->rq_addr
,
251 .msg_namelen
= rqstp
->rq_addrlen
,
253 .msg_controllen
= sizeof(buffer
),
254 .msg_flags
= MSG_MORE
,
257 svc_set_cmsg_data(rqstp
, cmh
);
259 if (sock_sendmsg(sock
, &msg
) < 0)
263 tailoff
= ((unsigned long)xdr
->tail
[0].iov_base
) & (PAGE_SIZE
-1);
265 len
= svc_send_common(sock
, xdr
, rqstp
->rq_respages
[0], headoff
,
266 rqstp
->rq_respages
[0], tailoff
);
269 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
270 svsk
, xdr
->head
[0].iov_base
, xdr
->head
[0].iov_len
,
271 xdr
->len
, len
, svc_print_addr(rqstp
, buf
, sizeof(buf
)));
277 * Report socket names for nfsdfs
279 static int svc_one_sock_name(struct svc_sock
*svsk
, char *buf
, int remaining
)
281 const struct sock
*sk
= svsk
->sk_sk
;
282 const char *proto_name
= sk
->sk_protocol
== IPPROTO_UDP
?
286 switch (sk
->sk_family
) {
288 len
= snprintf(buf
, remaining
, "ipv4 %s %pI4 %d\n",
290 &inet_sk(sk
)->inet_rcv_saddr
,
291 inet_sk(sk
)->inet_num
);
293 #if IS_ENABLED(CONFIG_IPV6)
295 len
= snprintf(buf
, remaining
, "ipv6 %s %pI6 %d\n",
297 &sk
->sk_v6_rcv_saddr
,
298 inet_sk(sk
)->inet_num
);
302 len
= snprintf(buf
, remaining
, "*unknown-%d*\n",
306 if (len
>= remaining
) {
308 return -ENAMETOOLONG
;
314 * Generic recvfrom routine.
316 static int svc_recvfrom(struct svc_rqst
*rqstp
, struct kvec
*iov
, int nr
,
319 struct svc_sock
*svsk
=
320 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
321 struct msghdr msg
= {
322 .msg_flags
= MSG_DONTWAIT
,
326 rqstp
->rq_xprt_hlen
= 0;
328 clear_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
329 len
= kernel_recvmsg(svsk
->sk_sock
, &msg
, iov
, nr
, buflen
,
331 /* If we read a full record, then assume there may be more
332 * data to read (stream based sockets only!)
335 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
337 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
338 svsk
, iov
[0].iov_base
, iov
[0].iov_len
, len
);
342 static int svc_partial_recvfrom(struct svc_rqst
*rqstp
,
343 struct kvec
*iov
, int nr
,
344 int buflen
, unsigned int base
)
352 return svc_recvfrom(rqstp
, iov
, nr
, buflen
);
354 for (i
= 0; i
< nr
; i
++) {
355 if (iov
[i
].iov_len
> base
)
357 base
-= iov
[i
].iov_len
;
359 save_iovlen
= iov
[i
].iov_len
;
360 save_iovbase
= iov
[i
].iov_base
;
361 iov
[i
].iov_len
-= base
;
362 iov
[i
].iov_base
+= base
;
363 ret
= svc_recvfrom(rqstp
, &iov
[i
], nr
- i
, buflen
);
364 iov
[i
].iov_len
= save_iovlen
;
365 iov
[i
].iov_base
= save_iovbase
;
370 * Set socket snd and rcv buffer lengths
372 static void svc_sock_setbufsize(struct socket
*sock
, unsigned int snd
,
377 oldfs
= get_fs(); set_fs(KERNEL_DS
);
378 sock_setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
,
379 (char*)&snd
, sizeof(snd
));
380 sock_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
381 (char*)&rcv
, sizeof(rcv
));
383 /* sock_setsockopt limits use to sysctl_?mem_max,
384 * which isn't acceptable. Until that is made conditional
385 * on not having CAP_SYS_RESOURCE or similar, we go direct...
386 * DaveM said I could!
389 sock
->sk
->sk_sndbuf
= snd
* 2;
390 sock
->sk
->sk_rcvbuf
= rcv
* 2;
391 sock
->sk
->sk_write_space(sock
->sk
);
392 release_sock(sock
->sk
);
396 static int svc_sock_secure_port(struct svc_rqst
*rqstp
)
398 return svc_port_is_privileged(svc_addr(rqstp
));
401 static bool sunrpc_waitqueue_active(wait_queue_head_t
*wq
)
406 * There should normally be a memory * barrier here--see
409 * It appears that isn't currently necessary, though, basically
410 * because callers all appear to have sufficient memory barriers
411 * between the time the relevant change is made and the
412 * time they call these callbacks.
414 * The nfsd code itself doesn't actually explicitly wait on
415 * these waitqueues, but it may wait on them for example in
416 * sendpage() or sendmsg() calls. (And those may be the only
417 * places, since it it uses nonblocking reads.)
419 * Maybe we should add the memory barriers anyway, but these are
420 * hot paths so we'd need to be convinced there's no sigificant
423 return waitqueue_active(wq
);
427 * INET callback when data has been received on the socket.
429 static void svc_udp_data_ready(struct sock
*sk
)
431 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
432 wait_queue_head_t
*wq
= sk_sleep(sk
);
435 dprintk("svc: socket %p(inet %p), busy=%d\n",
437 test_bit(XPT_BUSY
, &svsk
->sk_xprt
.xpt_flags
));
438 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
439 svc_xprt_enqueue(&svsk
->sk_xprt
);
441 if (sunrpc_waitqueue_active(wq
))
442 wake_up_interruptible(wq
);
446 * INET callback when space is newly available on the socket.
448 static void svc_write_space(struct sock
*sk
)
450 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->sk_user_data
);
451 wait_queue_head_t
*wq
= sk_sleep(sk
);
454 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
455 svsk
, sk
, test_bit(XPT_BUSY
, &svsk
->sk_xprt
.xpt_flags
));
456 svc_xprt_enqueue(&svsk
->sk_xprt
);
459 if (sunrpc_waitqueue_active(wq
)) {
460 dprintk("RPC svc_write_space: someone sleeping on %p\n",
462 wake_up_interruptible(wq
);
466 static int svc_tcp_has_wspace(struct svc_xprt
*xprt
)
468 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
469 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
472 if (test_bit(XPT_LISTENER
, &xprt
->xpt_flags
))
474 required
= atomic_read(&xprt
->xpt_reserved
) + serv
->sv_max_mesg
;
475 if (sk_stream_wspace(svsk
->sk_sk
) >= required
||
476 (sk_stream_min_wspace(svsk
->sk_sk
) == 0 &&
477 atomic_read(&xprt
->xpt_reserved
) == 0))
479 set_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
483 static void svc_tcp_write_space(struct sock
*sk
)
485 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->sk_user_data
);
486 struct socket
*sock
= sk
->sk_socket
;
488 if (!sk_stream_is_writeable(sk
) || !sock
)
490 if (!svsk
|| svc_tcp_has_wspace(&svsk
->sk_xprt
))
491 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
495 static void svc_tcp_adjust_wspace(struct svc_xprt
*xprt
)
497 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
499 if (svc_tcp_has_wspace(xprt
))
500 clear_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
504 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
506 static int svc_udp_get_dest_address4(struct svc_rqst
*rqstp
,
509 struct in_pktinfo
*pki
= CMSG_DATA(cmh
);
510 struct sockaddr_in
*daddr
= svc_daddr_in(rqstp
);
512 if (cmh
->cmsg_type
!= IP_PKTINFO
)
515 daddr
->sin_family
= AF_INET
;
516 daddr
->sin_addr
.s_addr
= pki
->ipi_spec_dst
.s_addr
;
521 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
523 static int svc_udp_get_dest_address6(struct svc_rqst
*rqstp
,
526 struct in6_pktinfo
*pki
= CMSG_DATA(cmh
);
527 struct sockaddr_in6
*daddr
= svc_daddr_in6(rqstp
);
529 if (cmh
->cmsg_type
!= IPV6_PKTINFO
)
532 daddr
->sin6_family
= AF_INET6
;
533 daddr
->sin6_addr
= pki
->ipi6_addr
;
534 daddr
->sin6_scope_id
= pki
->ipi6_ifindex
;
539 * Copy the UDP datagram's destination address to the rqstp structure.
540 * The 'destination' address in this case is the address to which the
541 * peer sent the datagram, i.e. our local address. For multihomed
542 * hosts, this can change from msg to msg. Note that only the IP
543 * address changes, the port number should remain the same.
545 static int svc_udp_get_dest_address(struct svc_rqst
*rqstp
,
548 switch (cmh
->cmsg_level
) {
550 return svc_udp_get_dest_address4(rqstp
, cmh
);
552 return svc_udp_get_dest_address6(rqstp
, cmh
);
559 * Receive a datagram from a UDP socket.
561 static int svc_udp_recvfrom(struct svc_rqst
*rqstp
)
563 struct svc_sock
*svsk
=
564 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
565 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
569 long all
[SVC_PKTINFO_SPACE
/ sizeof(long)];
571 struct cmsghdr
*cmh
= &buffer
.hdr
;
572 struct msghdr msg
= {
573 .msg_name
= svc_addr(rqstp
),
575 .msg_controllen
= sizeof(buffer
),
576 .msg_flags
= MSG_DONTWAIT
,
581 if (test_and_clear_bit(XPT_CHNGBUF
, &svsk
->sk_xprt
.xpt_flags
))
582 /* udp sockets need large rcvbuf as all pending
583 * requests are still in that buffer. sndbuf must
584 * also be large enough that there is enough space
585 * for one reply per thread. We count all threads
586 * rather than threads in a particular pool, which
587 * provides an upper bound on the number of threads
588 * which will access the socket.
590 svc_sock_setbufsize(svsk
->sk_sock
,
591 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
,
592 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
);
594 clear_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
596 err
= kernel_recvmsg(svsk
->sk_sock
, &msg
, NULL
,
597 0, 0, MSG_PEEK
| MSG_DONTWAIT
);
599 skb
= skb_recv_datagram(svsk
->sk_sk
, 0, 1, &err
);
602 if (err
!= -EAGAIN
) {
603 /* possibly an icmp error */
604 dprintk("svc: recvfrom returned error %d\n", -err
);
605 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
609 len
= svc_addr_len(svc_addr(rqstp
));
610 rqstp
->rq_addrlen
= len
;
611 if (skb
->tstamp
.tv64
== 0) {
612 skb
->tstamp
= ktime_get_real();
613 /* Don't enable netstamp, sunrpc doesn't
614 need that much accuracy */
616 svsk
->sk_sk
->sk_stamp
= skb
->tstamp
;
617 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
); /* there may be more data... */
620 rqstp
->rq_arg
.len
= len
;
622 rqstp
->rq_prot
= IPPROTO_UDP
;
624 if (!svc_udp_get_dest_address(rqstp
, cmh
)) {
625 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
626 cmh
->cmsg_level
, cmh
->cmsg_type
);
629 rqstp
->rq_daddrlen
= svc_addr_len(svc_daddr(rqstp
));
631 if (skb_is_nonlinear(skb
)) {
632 /* we have to copy */
634 if (csum_partial_copy_to_xdr(&rqstp
->rq_arg
, skb
)) {
640 skb_free_datagram_locked(svsk
->sk_sk
, skb
);
642 /* we can use it in-place */
643 rqstp
->rq_arg
.head
[0].iov_base
= skb
->data
;
644 rqstp
->rq_arg
.head
[0].iov_len
= len
;
645 if (skb_checksum_complete(skb
))
647 rqstp
->rq_xprt_ctxt
= skb
;
650 rqstp
->rq_arg
.page_base
= 0;
651 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
652 rqstp
->rq_arg
.head
[0].iov_len
= len
;
653 rqstp
->rq_arg
.page_len
= 0;
654 rqstp
->rq_respages
= rqstp
->rq_pages
+1;
656 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
657 rqstp
->rq_respages
= rqstp
->rq_pages
+ 1 +
658 DIV_ROUND_UP(rqstp
->rq_arg
.page_len
, PAGE_SIZE
);
660 rqstp
->rq_next_page
= rqstp
->rq_respages
+1;
663 serv
->sv_stats
->netudpcnt
++;
667 trace_kfree_skb(skb
, svc_udp_recvfrom
);
668 skb_free_datagram_locked(svsk
->sk_sk
, skb
);
673 svc_udp_sendto(struct svc_rqst
*rqstp
)
677 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
678 if (error
== -ECONNREFUSED
)
679 /* ICMP error on earlier request. */
680 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
685 static void svc_udp_prep_reply_hdr(struct svc_rqst
*rqstp
)
689 static int svc_udp_has_wspace(struct svc_xprt
*xprt
)
691 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
692 struct svc_serv
*serv
= xprt
->xpt_server
;
693 unsigned long required
;
696 * Set the SOCK_NOSPACE flag before checking the available
699 set_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
700 required
= atomic_read(&svsk
->sk_xprt
.xpt_reserved
) + serv
->sv_max_mesg
;
701 if (required
*2 > sock_wspace(svsk
->sk_sk
))
703 clear_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
707 static struct svc_xprt
*svc_udp_accept(struct svc_xprt
*xprt
)
713 static struct svc_xprt
*svc_udp_create(struct svc_serv
*serv
,
715 struct sockaddr
*sa
, int salen
,
718 return svc_create_socket(serv
, IPPROTO_UDP
, net
, sa
, salen
, flags
);
721 static struct svc_xprt_ops svc_udp_ops
= {
722 .xpo_create
= svc_udp_create
,
723 .xpo_recvfrom
= svc_udp_recvfrom
,
724 .xpo_sendto
= svc_udp_sendto
,
725 .xpo_release_rqst
= svc_release_skb
,
726 .xpo_detach
= svc_sock_detach
,
727 .xpo_free
= svc_sock_free
,
728 .xpo_prep_reply_hdr
= svc_udp_prep_reply_hdr
,
729 .xpo_has_wspace
= svc_udp_has_wspace
,
730 .xpo_accept
= svc_udp_accept
,
731 .xpo_secure_port
= svc_sock_secure_port
,
734 static struct svc_xprt_class svc_udp_class
= {
736 .xcl_owner
= THIS_MODULE
,
737 .xcl_ops
= &svc_udp_ops
,
738 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_UDP
,
739 .xcl_ident
= XPRT_TRANSPORT_UDP
,
742 static void svc_udp_init(struct svc_sock
*svsk
, struct svc_serv
*serv
)
744 int err
, level
, optname
, one
= 1;
746 svc_xprt_init(sock_net(svsk
->sk_sock
->sk
), &svc_udp_class
,
747 &svsk
->sk_xprt
, serv
);
748 clear_bit(XPT_CACHE_AUTH
, &svsk
->sk_xprt
.xpt_flags
);
749 svsk
->sk_sk
->sk_data_ready
= svc_udp_data_ready
;
750 svsk
->sk_sk
->sk_write_space
= svc_write_space
;
752 /* initialise setting must have enough space to
753 * receive and respond to one request.
754 * svc_udp_recvfrom will re-adjust if necessary
756 svc_sock_setbufsize(svsk
->sk_sock
,
757 3 * svsk
->sk_xprt
.xpt_server
->sv_max_mesg
,
758 3 * svsk
->sk_xprt
.xpt_server
->sv_max_mesg
);
760 /* data might have come in before data_ready set up */
761 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
762 set_bit(XPT_CHNGBUF
, &svsk
->sk_xprt
.xpt_flags
);
764 /* make sure we get destination address info */
765 switch (svsk
->sk_sk
->sk_family
) {
768 optname
= IP_PKTINFO
;
772 optname
= IPV6_RECVPKTINFO
;
777 err
= kernel_setsockopt(svsk
->sk_sock
, level
, optname
,
778 (char *)&one
, sizeof(one
));
779 dprintk("svc: kernel_setsockopt returned %d\n", err
);
783 * A data_ready event on a listening socket means there's a connection
784 * pending. Do not use state_change as a substitute for it.
786 static void svc_tcp_listen_data_ready(struct sock
*sk
)
788 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
789 wait_queue_head_t
*wq
;
791 dprintk("svc: socket %p TCP (listen) state change %d\n",
795 * This callback may called twice when a new connection
796 * is established as a child socket inherits everything
797 * from a parent LISTEN socket.
798 * 1) data_ready method of the parent socket will be called
799 * when one of child sockets become ESTABLISHED.
800 * 2) data_ready method of the child socket may be called
801 * when it receives data before the socket is accepted.
802 * In case of 2, we should ignore it silently.
804 if (sk
->sk_state
== TCP_LISTEN
) {
806 set_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
807 svc_xprt_enqueue(&svsk
->sk_xprt
);
809 printk("svc: socket %p: no user data\n", sk
);
813 if (sunrpc_waitqueue_active(wq
))
814 wake_up_interruptible_all(wq
);
818 * A state change on a connected socket means it's dying or dead.
820 static void svc_tcp_state_change(struct sock
*sk
)
822 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
823 wait_queue_head_t
*wq
= sk_sleep(sk
);
825 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
826 sk
, sk
->sk_state
, sk
->sk_user_data
);
829 printk("svc: socket %p: no user data\n", sk
);
831 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
832 svc_xprt_enqueue(&svsk
->sk_xprt
);
834 if (sunrpc_waitqueue_active(wq
))
835 wake_up_interruptible_all(wq
);
838 static void svc_tcp_data_ready(struct sock
*sk
)
840 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
841 wait_queue_head_t
*wq
= sk_sleep(sk
);
843 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
844 sk
, sk
->sk_user_data
);
846 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
847 svc_xprt_enqueue(&svsk
->sk_xprt
);
849 if (sunrpc_waitqueue_active(wq
))
850 wake_up_interruptible(wq
);
854 * Accept a TCP connection
856 static struct svc_xprt
*svc_tcp_accept(struct svc_xprt
*xprt
)
858 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
859 struct sockaddr_storage addr
;
860 struct sockaddr
*sin
= (struct sockaddr
*) &addr
;
861 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
862 struct socket
*sock
= svsk
->sk_sock
;
863 struct socket
*newsock
;
864 struct svc_sock
*newsvsk
;
866 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
868 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
872 clear_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
873 err
= kernel_accept(sock
, &newsock
, O_NONBLOCK
);
876 printk(KERN_WARNING
"%s: no more sockets!\n",
878 else if (err
!= -EAGAIN
)
879 net_warn_ratelimited("%s: accept failed (err %d)!\n",
880 serv
->sv_name
, -err
);
883 set_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
885 err
= kernel_getpeername(newsock
, sin
, &slen
);
887 net_warn_ratelimited("%s: peername failed (err %d)!\n",
888 serv
->sv_name
, -err
);
889 goto failed
; /* aborted connection or whatever */
892 /* Ideally, we would want to reject connections from unauthorized
893 * hosts here, but when we get encryption, the IP of the host won't
894 * tell us anything. For now just warn about unpriv connections.
896 if (!svc_port_is_privileged(sin
)) {
897 dprintk("%s: connect from unprivileged port: %s\n",
899 __svc_print_addr(sin
, buf
, sizeof(buf
)));
901 dprintk("%s: connect from %s\n", serv
->sv_name
,
902 __svc_print_addr(sin
, buf
, sizeof(buf
)));
904 /* make sure that a write doesn't block forever when
907 newsock
->sk
->sk_sndtimeo
= HZ
*30;
909 newsvsk
= svc_setup_socket(serv
, newsock
,
910 (SVC_SOCK_ANONYMOUS
| SVC_SOCK_TEMPORARY
));
913 svc_xprt_set_remote(&newsvsk
->sk_xprt
, sin
, slen
);
914 err
= kernel_getsockname(newsock
, sin
, &slen
);
915 if (unlikely(err
< 0)) {
916 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err
);
917 slen
= offsetof(struct sockaddr
, sa_data
);
919 svc_xprt_set_local(&newsvsk
->sk_xprt
, sin
, slen
);
921 if (sock_is_loopback(newsock
->sk
))
922 set_bit(XPT_LOCAL
, &newsvsk
->sk_xprt
.xpt_flags
);
924 clear_bit(XPT_LOCAL
, &newsvsk
->sk_xprt
.xpt_flags
);
926 serv
->sv_stats
->nettcpconn
++;
928 return &newsvsk
->sk_xprt
;
931 sock_release(newsock
);
935 static unsigned int svc_tcp_restore_pages(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
937 unsigned int i
, len
, npages
;
939 if (svsk
->sk_datalen
== 0)
941 len
= svsk
->sk_datalen
;
942 npages
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
943 for (i
= 0; i
< npages
; i
++) {
944 if (rqstp
->rq_pages
[i
] != NULL
)
945 put_page(rqstp
->rq_pages
[i
]);
946 BUG_ON(svsk
->sk_pages
[i
] == NULL
);
947 rqstp
->rq_pages
[i
] = svsk
->sk_pages
[i
];
948 svsk
->sk_pages
[i
] = NULL
;
950 rqstp
->rq_arg
.head
[0].iov_base
= page_address(rqstp
->rq_pages
[0]);
954 static void svc_tcp_save_pages(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
956 unsigned int i
, len
, npages
;
958 if (svsk
->sk_datalen
== 0)
960 len
= svsk
->sk_datalen
;
961 npages
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
962 for (i
= 0; i
< npages
; i
++) {
963 svsk
->sk_pages
[i
] = rqstp
->rq_pages
[i
];
964 rqstp
->rq_pages
[i
] = NULL
;
968 static void svc_tcp_clear_pages(struct svc_sock
*svsk
)
970 unsigned int i
, len
, npages
;
972 if (svsk
->sk_datalen
== 0)
974 len
= svsk
->sk_datalen
;
975 npages
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
976 for (i
= 0; i
< npages
; i
++) {
977 if (svsk
->sk_pages
[i
] == NULL
) {
981 put_page(svsk
->sk_pages
[i
]);
982 svsk
->sk_pages
[i
] = NULL
;
986 svsk
->sk_datalen
= 0;
990 * Receive fragment record header.
991 * If we haven't gotten the record length yet, get the next four bytes.
993 static int svc_tcp_recv_record(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
995 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
999 if (svsk
->sk_tcplen
< sizeof(rpc_fraghdr
)) {
1002 want
= sizeof(rpc_fraghdr
) - svsk
->sk_tcplen
;
1003 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
1005 if ((len
= svc_recvfrom(rqstp
, &iov
, 1, want
)) < 0)
1007 svsk
->sk_tcplen
+= len
;
1010 dprintk("svc: short recvfrom while reading record "
1011 "length (%d of %d)\n", len
, want
);
1015 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk
));
1016 if (svc_sock_reclen(svsk
) + svsk
->sk_datalen
>
1017 serv
->sv_max_mesg
) {
1018 net_notice_ratelimited("RPC: fragment too large: %d\n",
1019 svc_sock_reclen(svsk
));
1024 return svc_sock_reclen(svsk
);
1026 dprintk("RPC: TCP recv_record got %d\n", len
);
1029 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
1033 static int receive_cb_reply(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
1035 struct rpc_xprt
*bc_xprt
= svsk
->sk_xprt
.xpt_bc_xprt
;
1036 struct rpc_rqst
*req
= NULL
;
1037 struct kvec
*src
, *dst
;
1038 __be32
*p
= (__be32
*)rqstp
->rq_arg
.head
[0].iov_base
;
1047 spin_lock_bh(&bc_xprt
->transport_lock
);
1048 req
= xprt_lookup_rqst(bc_xprt
, xid
);
1050 goto unlock_notfound
;
1052 memcpy(&req
->rq_private_buf
, &req
->rq_rcv_buf
, sizeof(struct xdr_buf
));
1054 * XXX!: cheating for now! Only copying HEAD.
1055 * But we know this is good enough for now (in fact, for any
1056 * callback reply in the forseeable future).
1058 dst
= &req
->rq_private_buf
.head
[0];
1059 src
= &rqstp
->rq_arg
.head
[0];
1060 if (dst
->iov_len
< src
->iov_len
)
1061 goto unlock_eagain
; /* whatever; just giving up. */
1062 memcpy(dst
->iov_base
, src
->iov_base
, src
->iov_len
);
1063 xprt_complete_rqst(req
->rq_task
, rqstp
->rq_arg
.len
);
1064 rqstp
->rq_arg
.len
= 0;
1065 spin_unlock_bh(&bc_xprt
->transport_lock
);
1069 "%s: Got unrecognized reply: "
1070 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1071 __func__
, ntohl(calldir
),
1072 bc_xprt
, ntohl(xid
));
1074 spin_unlock_bh(&bc_xprt
->transport_lock
);
1078 static int copy_pages_to_kvecs(struct kvec
*vec
, struct page
**pages
, int len
)
1084 vec
[i
].iov_base
= page_address(pages
[i
]);
1085 vec
[i
].iov_len
= PAGE_SIZE
;
1092 static void svc_tcp_fragment_received(struct svc_sock
*svsk
)
1094 /* If we have more data, signal svc_xprt_enqueue() to try again */
1095 dprintk("svc: TCP %s record (%d bytes)\n",
1096 svc_sock_final_rec(svsk
) ? "final" : "nonfinal",
1097 svc_sock_reclen(svsk
));
1098 svsk
->sk_tcplen
= 0;
1099 svsk
->sk_reclen
= 0;
1103 * Receive data from a TCP socket.
1105 static int svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
1107 struct svc_sock
*svsk
=
1108 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
1109 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
1112 unsigned int want
, base
;
1117 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1118 svsk
, test_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
),
1119 test_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
),
1120 test_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
));
1122 len
= svc_tcp_recv_record(svsk
, rqstp
);
1126 base
= svc_tcp_restore_pages(svsk
, rqstp
);
1127 want
= svc_sock_reclen(svsk
) - (svsk
->sk_tcplen
- sizeof(rpc_fraghdr
));
1129 vec
= rqstp
->rq_vec
;
1131 pnum
= copy_pages_to_kvecs(&vec
[0], &rqstp
->rq_pages
[0],
1132 svsk
->sk_datalen
+ want
);
1134 rqstp
->rq_respages
= &rqstp
->rq_pages
[pnum
];
1135 rqstp
->rq_next_page
= rqstp
->rq_respages
+ 1;
1137 /* Now receive data */
1138 len
= svc_partial_recvfrom(rqstp
, vec
, pnum
, want
, base
);
1140 svsk
->sk_tcplen
+= len
;
1141 svsk
->sk_datalen
+= len
;
1143 if (len
!= want
|| !svc_sock_final_rec(svsk
)) {
1144 svc_tcp_save_pages(svsk
, rqstp
);
1145 if (len
< 0 && len
!= -EAGAIN
)
1148 svc_tcp_fragment_received(svsk
);
1150 dprintk("svc: incomplete TCP record (%d of %d)\n",
1151 (int)(svsk
->sk_tcplen
- sizeof(rpc_fraghdr
)),
1152 svc_sock_reclen(svsk
));
1156 if (svsk
->sk_datalen
< 8) {
1157 svsk
->sk_datalen
= 0;
1158 goto err_delete
; /* client is nuts. */
1161 rqstp
->rq_arg
.len
= svsk
->sk_datalen
;
1162 rqstp
->rq_arg
.page_base
= 0;
1163 if (rqstp
->rq_arg
.len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
1164 rqstp
->rq_arg
.head
[0].iov_len
= rqstp
->rq_arg
.len
;
1165 rqstp
->rq_arg
.page_len
= 0;
1167 rqstp
->rq_arg
.page_len
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
1169 rqstp
->rq_xprt_ctxt
= NULL
;
1170 rqstp
->rq_prot
= IPPROTO_TCP
;
1171 if (test_bit(XPT_LOCAL
, &svsk
->sk_xprt
.xpt_flags
))
1172 set_bit(RQ_LOCAL
, &rqstp
->rq_flags
);
1174 clear_bit(RQ_LOCAL
, &rqstp
->rq_flags
);
1176 p
= (__be32
*)rqstp
->rq_arg
.head
[0].iov_base
;
1179 len
= receive_cb_reply(svsk
, rqstp
);
1181 /* Reset TCP read info */
1182 svsk
->sk_datalen
= 0;
1183 svc_tcp_fragment_received(svsk
);
1188 svc_xprt_copy_addrs(rqstp
, &svsk
->sk_xprt
);
1190 serv
->sv_stats
->nettcpcnt
++;
1192 return rqstp
->rq_arg
.len
;
1197 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1200 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
1201 svsk
->sk_xprt
.xpt_server
->sv_name
, -len
);
1202 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
1204 return 0; /* record not complete */
1208 * Send out data on TCP socket.
1210 static int svc_tcp_sendto(struct svc_rqst
*rqstp
)
1212 struct xdr_buf
*xbufp
= &rqstp
->rq_res
;
1216 /* Set up the first element of the reply kvec.
1217 * Any other kvecs that may be in use have been taken
1218 * care of by the server implementation itself.
1220 reclen
= htonl(0x80000000|((xbufp
->len
) - 4));
1221 memcpy(xbufp
->head
[0].iov_base
, &reclen
, 4);
1223 sent
= svc_sendto(rqstp
, &rqstp
->rq_res
);
1224 if (sent
!= xbufp
->len
) {
1226 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1227 "- shutting down socket\n",
1228 rqstp
->rq_xprt
->xpt_server
->sv_name
,
1229 (sent
<0)?"got error":"sent only",
1231 set_bit(XPT_CLOSE
, &rqstp
->rq_xprt
->xpt_flags
);
1232 svc_xprt_enqueue(rqstp
->rq_xprt
);
1239 * Setup response header. TCP has a 4B record length field.
1241 static void svc_tcp_prep_reply_hdr(struct svc_rqst
*rqstp
)
1243 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
1245 /* tcp needs a space for the record length... */
1249 static struct svc_xprt
*svc_tcp_create(struct svc_serv
*serv
,
1251 struct sockaddr
*sa
, int salen
,
1254 return svc_create_socket(serv
, IPPROTO_TCP
, net
, sa
, salen
, flags
);
1257 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1258 static struct svc_xprt
*svc_bc_create_socket(struct svc_serv
*, int,
1259 struct net
*, struct sockaddr
*,
1261 static void svc_bc_sock_free(struct svc_xprt
*xprt
);
1263 static struct svc_xprt
*svc_bc_tcp_create(struct svc_serv
*serv
,
1265 struct sockaddr
*sa
, int salen
,
1268 return svc_bc_create_socket(serv
, IPPROTO_TCP
, net
, sa
, salen
, flags
);
1271 static void svc_bc_tcp_sock_detach(struct svc_xprt
*xprt
)
1275 static struct svc_xprt_ops svc_tcp_bc_ops
= {
1276 .xpo_create
= svc_bc_tcp_create
,
1277 .xpo_detach
= svc_bc_tcp_sock_detach
,
1278 .xpo_free
= svc_bc_sock_free
,
1279 .xpo_prep_reply_hdr
= svc_tcp_prep_reply_hdr
,
1280 .xpo_secure_port
= svc_sock_secure_port
,
1283 static struct svc_xprt_class svc_tcp_bc_class
= {
1284 .xcl_name
= "tcp-bc",
1285 .xcl_owner
= THIS_MODULE
,
1286 .xcl_ops
= &svc_tcp_bc_ops
,
1287 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
1290 static void svc_init_bc_xprt_sock(void)
1292 svc_reg_xprt_class(&svc_tcp_bc_class
);
1295 static void svc_cleanup_bc_xprt_sock(void)
1297 svc_unreg_xprt_class(&svc_tcp_bc_class
);
1299 #else /* CONFIG_SUNRPC_BACKCHANNEL */
1300 static void svc_init_bc_xprt_sock(void)
1304 static void svc_cleanup_bc_xprt_sock(void)
1307 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1309 static struct svc_xprt_ops svc_tcp_ops
= {
1310 .xpo_create
= svc_tcp_create
,
1311 .xpo_recvfrom
= svc_tcp_recvfrom
,
1312 .xpo_sendto
= svc_tcp_sendto
,
1313 .xpo_release_rqst
= svc_release_skb
,
1314 .xpo_detach
= svc_tcp_sock_detach
,
1315 .xpo_free
= svc_sock_free
,
1316 .xpo_prep_reply_hdr
= svc_tcp_prep_reply_hdr
,
1317 .xpo_has_wspace
= svc_tcp_has_wspace
,
1318 .xpo_accept
= svc_tcp_accept
,
1319 .xpo_secure_port
= svc_sock_secure_port
,
1320 .xpo_adjust_wspace
= svc_tcp_adjust_wspace
,
1323 static struct svc_xprt_class svc_tcp_class
= {
1325 .xcl_owner
= THIS_MODULE
,
1326 .xcl_ops
= &svc_tcp_ops
,
1327 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
1328 .xcl_ident
= XPRT_TRANSPORT_TCP
,
1331 void svc_init_xprt_sock(void)
1333 svc_reg_xprt_class(&svc_tcp_class
);
1334 svc_reg_xprt_class(&svc_udp_class
);
1335 svc_init_bc_xprt_sock();
1338 void svc_cleanup_xprt_sock(void)
1340 svc_unreg_xprt_class(&svc_tcp_class
);
1341 svc_unreg_xprt_class(&svc_udp_class
);
1342 svc_cleanup_bc_xprt_sock();
1345 static void svc_tcp_init(struct svc_sock
*svsk
, struct svc_serv
*serv
)
1347 struct sock
*sk
= svsk
->sk_sk
;
1349 svc_xprt_init(sock_net(svsk
->sk_sock
->sk
), &svc_tcp_class
,
1350 &svsk
->sk_xprt
, serv
);
1351 set_bit(XPT_CACHE_AUTH
, &svsk
->sk_xprt
.xpt_flags
);
1352 if (sk
->sk_state
== TCP_LISTEN
) {
1353 dprintk("setting up TCP socket for listening\n");
1354 set_bit(XPT_LISTENER
, &svsk
->sk_xprt
.xpt_flags
);
1355 sk
->sk_data_ready
= svc_tcp_listen_data_ready
;
1356 set_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
1358 dprintk("setting up TCP socket for reading\n");
1359 sk
->sk_state_change
= svc_tcp_state_change
;
1360 sk
->sk_data_ready
= svc_tcp_data_ready
;
1361 sk
->sk_write_space
= svc_tcp_write_space
;
1363 svsk
->sk_reclen
= 0;
1364 svsk
->sk_tcplen
= 0;
1365 svsk
->sk_datalen
= 0;
1366 memset(&svsk
->sk_pages
[0], 0, sizeof(svsk
->sk_pages
));
1368 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1370 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
1371 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1372 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
1376 void svc_sock_update_bufs(struct svc_serv
*serv
)
1379 * The number of server threads has changed. Update
1380 * rcvbuf and sndbuf accordingly on all sockets
1382 struct svc_sock
*svsk
;
1384 spin_lock_bh(&serv
->sv_lock
);
1385 list_for_each_entry(svsk
, &serv
->sv_permsocks
, sk_xprt
.xpt_list
)
1386 set_bit(XPT_CHNGBUF
, &svsk
->sk_xprt
.xpt_flags
);
1387 spin_unlock_bh(&serv
->sv_lock
);
1389 EXPORT_SYMBOL_GPL(svc_sock_update_bufs
);
1392 * Initialize socket for RPC use and create svc_sock struct
1394 static struct svc_sock
*svc_setup_socket(struct svc_serv
*serv
,
1395 struct socket
*sock
,
1398 struct svc_sock
*svsk
;
1400 int pmap_register
= !(flags
& SVC_SOCK_ANONYMOUS
);
1403 dprintk("svc: svc_setup_socket %p\n", sock
);
1404 svsk
= kzalloc(sizeof(*svsk
), GFP_KERNEL
);
1406 return ERR_PTR(-ENOMEM
);
1410 /* Register socket with portmapper */
1412 err
= svc_register(serv
, sock_net(sock
->sk
), inet
->sk_family
,
1414 ntohs(inet_sk(inet
)->inet_sport
));
1418 return ERR_PTR(err
);
1421 inet
->sk_user_data
= svsk
;
1422 svsk
->sk_sock
= sock
;
1424 svsk
->sk_ostate
= inet
->sk_state_change
;
1425 svsk
->sk_odata
= inet
->sk_data_ready
;
1426 svsk
->sk_owspace
= inet
->sk_write_space
;
1428 /* Initialize the socket */
1429 if (sock
->type
== SOCK_DGRAM
)
1430 svc_udp_init(svsk
, serv
);
1432 /* initialise setting must have enough space to
1433 * receive and respond to one request.
1435 svc_sock_setbufsize(svsk
->sk_sock
, 4 * serv
->sv_max_mesg
,
1436 4 * serv
->sv_max_mesg
);
1437 svc_tcp_init(svsk
, serv
);
1440 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1446 bool svc_alien_sock(struct net
*net
, int fd
)
1449 struct socket
*sock
= sockfd_lookup(fd
, &err
);
1454 if (sock_net(sock
->sk
) != net
)
1460 EXPORT_SYMBOL_GPL(svc_alien_sock
);
1463 * svc_addsock - add a listener socket to an RPC service
1464 * @serv: pointer to RPC service to which to add a new listener
1465 * @fd: file descriptor of the new listener
1466 * @name_return: pointer to buffer to fill in with name of listener
1467 * @len: size of the buffer
1469 * Fills in socket name and returns positive length of name if successful.
1470 * Name is terminated with '\n'. On error, returns a negative errno
1473 int svc_addsock(struct svc_serv
*serv
, const int fd
, char *name_return
,
1477 struct socket
*so
= sockfd_lookup(fd
, &err
);
1478 struct svc_sock
*svsk
= NULL
;
1479 struct sockaddr_storage addr
;
1480 struct sockaddr
*sin
= (struct sockaddr
*)&addr
;
1485 err
= -EAFNOSUPPORT
;
1486 if ((so
->sk
->sk_family
!= PF_INET
) && (so
->sk
->sk_family
!= PF_INET6
))
1488 err
= -EPROTONOSUPPORT
;
1489 if (so
->sk
->sk_protocol
!= IPPROTO_TCP
&&
1490 so
->sk
->sk_protocol
!= IPPROTO_UDP
)
1493 if (so
->state
> SS_UNCONNECTED
)
1496 if (!try_module_get(THIS_MODULE
))
1498 svsk
= svc_setup_socket(serv
, so
, SVC_SOCK_DEFAULTS
);
1500 module_put(THIS_MODULE
);
1501 err
= PTR_ERR(svsk
);
1504 if (kernel_getsockname(svsk
->sk_sock
, sin
, &salen
) == 0)
1505 svc_xprt_set_local(&svsk
->sk_xprt
, sin
, salen
);
1506 svc_add_new_perm_xprt(serv
, &svsk
->sk_xprt
);
1507 return svc_one_sock_name(svsk
, name_return
, len
);
1512 EXPORT_SYMBOL_GPL(svc_addsock
);
1515 * Create socket for RPC service.
1517 static struct svc_xprt
*svc_create_socket(struct svc_serv
*serv
,
1520 struct sockaddr
*sin
, int len
,
1523 struct svc_sock
*svsk
;
1524 struct socket
*sock
;
1527 struct sockaddr_storage addr
;
1528 struct sockaddr
*newsin
= (struct sockaddr
*)&addr
;
1532 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
1534 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1535 serv
->sv_program
->pg_name
, protocol
,
1536 __svc_print_addr(sin
, buf
, sizeof(buf
)));
1538 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
1539 printk(KERN_WARNING
"svc: only UDP and TCP "
1540 "sockets supported\n");
1541 return ERR_PTR(-EINVAL
);
1544 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
1545 switch (sin
->sa_family
) {
1553 return ERR_PTR(-EINVAL
);
1556 error
= __sock_create(net
, family
, type
, protocol
, &sock
, 1);
1558 return ERR_PTR(error
);
1560 svc_reclassify_socket(sock
);
1563 * If this is an PF_INET6 listener, we want to avoid
1564 * getting requests from IPv4 remotes. Those should
1565 * be shunted to a PF_INET listener via rpcbind.
1568 if (family
== PF_INET6
)
1569 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_V6ONLY
,
1570 (char *)&val
, sizeof(val
));
1572 if (type
== SOCK_STREAM
)
1573 sock
->sk
->sk_reuse
= SK_CAN_REUSE
; /* allow address reuse */
1574 error
= kernel_bind(sock
, sin
, len
);
1579 error
= kernel_getsockname(sock
, newsin
, &newlen
);
1583 if (protocol
== IPPROTO_TCP
) {
1584 if ((error
= kernel_listen(sock
, 64)) < 0)
1588 svsk
= svc_setup_socket(serv
, sock
, flags
);
1590 error
= PTR_ERR(svsk
);
1593 svc_xprt_set_local(&svsk
->sk_xprt
, newsin
, newlen
);
1594 return (struct svc_xprt
*)svsk
;
1596 dprintk("svc: svc_create_socket error = %d\n", -error
);
1598 return ERR_PTR(error
);
1602 * Detach the svc_sock from the socket so that no
1603 * more callbacks occur.
1605 static void svc_sock_detach(struct svc_xprt
*xprt
)
1607 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
1608 struct sock
*sk
= svsk
->sk_sk
;
1609 wait_queue_head_t
*wq
;
1611 dprintk("svc: svc_sock_detach(%p)\n", svsk
);
1613 /* put back the old socket callbacks */
1614 sk
->sk_state_change
= svsk
->sk_ostate
;
1615 sk
->sk_data_ready
= svsk
->sk_odata
;
1616 sk
->sk_write_space
= svsk
->sk_owspace
;
1619 if (sunrpc_waitqueue_active(wq
))
1620 wake_up_interruptible(wq
);
1624 * Disconnect the socket, and reset the callbacks
1626 static void svc_tcp_sock_detach(struct svc_xprt
*xprt
)
1628 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
1630 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk
);
1632 svc_sock_detach(xprt
);
1634 if (!test_bit(XPT_LISTENER
, &xprt
->xpt_flags
)) {
1635 svc_tcp_clear_pages(svsk
);
1636 kernel_sock_shutdown(svsk
->sk_sock
, SHUT_RDWR
);
1641 * Free the svc_sock's socket resources and the svc_sock itself.
1643 static void svc_sock_free(struct svc_xprt
*xprt
)
1645 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
1646 dprintk("svc: svc_sock_free(%p)\n", svsk
);
1648 if (svsk
->sk_sock
->file
)
1649 sockfd_put(svsk
->sk_sock
);
1651 sock_release(svsk
->sk_sock
);
1655 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1657 * Create a back channel svc_xprt which shares the fore channel socket.
1659 static struct svc_xprt
*svc_bc_create_socket(struct svc_serv
*serv
,
1662 struct sockaddr
*sin
, int len
,
1665 struct svc_sock
*svsk
;
1666 struct svc_xprt
*xprt
;
1668 if (protocol
!= IPPROTO_TCP
) {
1669 printk(KERN_WARNING
"svc: only TCP sockets"
1670 " supported on shared back channel\n");
1671 return ERR_PTR(-EINVAL
);
1674 svsk
= kzalloc(sizeof(*svsk
), GFP_KERNEL
);
1676 return ERR_PTR(-ENOMEM
);
1678 xprt
= &svsk
->sk_xprt
;
1679 svc_xprt_init(net
, &svc_tcp_bc_class
, xprt
, serv
);
1681 serv
->sv_bc_xprt
= xprt
;
1687 * Free a back channel svc_sock.
1689 static void svc_bc_sock_free(struct svc_xprt
*xprt
)
1692 kfree(container_of(xprt
, struct svc_sock
, sk_xprt
));
1694 #endif /* CONFIG_SUNRPC_BACKCHANNEL */