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>
44 #include <net/tcp_states.h>
45 #include <linux/uaccess.h>
46 #include <asm/ioctls.h>
47 #include <trace/events/skb.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>
59 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
62 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
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 #ifdef CONFIG_DEBUG_LOCK_ALLOC
74 static struct lock_class_key svc_key
[2];
75 static struct lock_class_key svc_slock_key
[2];
77 static void svc_reclassify_socket(struct socket
*sock
)
79 struct sock
*sk
= sock
->sk
;
81 if (WARN_ON_ONCE(!sock_allow_reclassification(sk
)))
84 switch (sk
->sk_family
) {
86 sock_lock_init_class_and_name(sk
, "slock-AF_INET-NFSD",
88 "sk_xprt.xpt_lock-AF_INET-NFSD",
93 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-NFSD",
95 "sk_xprt.xpt_lock-AF_INET6-NFSD",
104 static void svc_reclassify_socket(struct socket
*sock
)
110 * Release an skbuff after use
112 static void svc_release_skb(struct svc_rqst
*rqstp
)
114 struct sk_buff
*skb
= rqstp
->rq_xprt_ctxt
;
117 struct svc_sock
*svsk
=
118 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
119 rqstp
->rq_xprt_ctxt
= NULL
;
121 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
122 skb_free_datagram_locked(svsk
->sk_sk
, skb
);
126 static void svc_release_udp_skb(struct svc_rqst
*rqstp
)
128 struct sk_buff
*skb
= rqstp
->rq_xprt_ctxt
;
131 rqstp
->rq_xprt_ctxt
= NULL
;
133 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
138 union svc_pktinfo_u
{
139 struct in_pktinfo pkti
;
140 struct in6_pktinfo pkti6
;
142 #define SVC_PKTINFO_SPACE \
143 CMSG_SPACE(sizeof(union svc_pktinfo_u))
145 static void svc_set_cmsg_data(struct svc_rqst
*rqstp
, struct cmsghdr
*cmh
)
147 struct svc_sock
*svsk
=
148 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
149 switch (svsk
->sk_sk
->sk_family
) {
151 struct in_pktinfo
*pki
= CMSG_DATA(cmh
);
153 cmh
->cmsg_level
= SOL_IP
;
154 cmh
->cmsg_type
= IP_PKTINFO
;
155 pki
->ipi_ifindex
= 0;
156 pki
->ipi_spec_dst
.s_addr
=
157 svc_daddr_in(rqstp
)->sin_addr
.s_addr
;
158 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
163 struct in6_pktinfo
*pki
= CMSG_DATA(cmh
);
164 struct sockaddr_in6
*daddr
= svc_daddr_in6(rqstp
);
166 cmh
->cmsg_level
= SOL_IPV6
;
167 cmh
->cmsg_type
= IPV6_PKTINFO
;
168 pki
->ipi6_ifindex
= daddr
->sin6_scope_id
;
169 pki
->ipi6_addr
= daddr
->sin6_addr
;
170 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
177 * send routine intended to be shared by the fore- and back-channel
179 int svc_send_common(struct socket
*sock
, struct xdr_buf
*xdr
,
180 struct page
*headpage
, unsigned long headoffset
,
181 struct page
*tailpage
, unsigned long tailoffset
)
185 struct page
**ppage
= xdr
->pages
;
186 size_t base
= xdr
->page_base
;
187 unsigned int pglen
= xdr
->page_len
;
188 unsigned int flags
= MSG_MORE
| MSG_SENDPAGE_NOTLAST
;
195 if (slen
== xdr
->head
[0].iov_len
)
197 len
= kernel_sendpage(sock
, headpage
, headoffset
,
198 xdr
->head
[0].iov_len
, flags
);
199 if (len
!= xdr
->head
[0].iov_len
)
201 slen
-= xdr
->head
[0].iov_len
;
206 size
= PAGE_SIZE
- base
< pglen
? PAGE_SIZE
- base
: pglen
;
210 result
= kernel_sendpage(sock
, *ppage
, base
, size
, flags
);
217 size
= PAGE_SIZE
< pglen
? PAGE_SIZE
: pglen
;
223 if (xdr
->tail
[0].iov_len
) {
224 result
= kernel_sendpage(sock
, tailpage
, tailoffset
,
225 xdr
->tail
[0].iov_len
, 0);
236 * Generic sendto routine
238 static int svc_sendto(struct svc_rqst
*rqstp
, struct xdr_buf
*xdr
)
240 struct svc_sock
*svsk
=
241 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
242 struct socket
*sock
= svsk
->sk_sock
;
245 long all
[SVC_PKTINFO_SPACE
/ sizeof(long)];
247 struct cmsghdr
*cmh
= &buffer
.hdr
;
249 unsigned long tailoff
;
250 unsigned long headoff
;
251 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
253 if (rqstp
->rq_prot
== IPPROTO_UDP
) {
254 struct msghdr msg
= {
255 .msg_name
= &rqstp
->rq_addr
,
256 .msg_namelen
= rqstp
->rq_addrlen
,
258 .msg_controllen
= sizeof(buffer
),
259 .msg_flags
= MSG_MORE
,
262 svc_set_cmsg_data(rqstp
, cmh
);
264 if (sock_sendmsg(sock
, &msg
) < 0)
268 tailoff
= ((unsigned long)xdr
->tail
[0].iov_base
) & (PAGE_SIZE
-1);
270 len
= svc_send_common(sock
, xdr
, rqstp
->rq_respages
[0], headoff
,
271 rqstp
->rq_respages
[0], tailoff
);
274 dprintk("svc: socket %p sendto([%p %zu... ], %d) = %d (addr %s)\n",
275 svsk
, xdr
->head
[0].iov_base
, xdr
->head
[0].iov_len
,
276 xdr
->len
, len
, svc_print_addr(rqstp
, buf
, sizeof(buf
)));
282 * Report socket names for nfsdfs
284 static int svc_one_sock_name(struct svc_sock
*svsk
, char *buf
, int remaining
)
286 const struct sock
*sk
= svsk
->sk_sk
;
287 const char *proto_name
= sk
->sk_protocol
== IPPROTO_UDP
?
291 switch (sk
->sk_family
) {
293 len
= snprintf(buf
, remaining
, "ipv4 %s %pI4 %d\n",
295 &inet_sk(sk
)->inet_rcv_saddr
,
296 inet_sk(sk
)->inet_num
);
298 #if IS_ENABLED(CONFIG_IPV6)
300 len
= snprintf(buf
, remaining
, "ipv6 %s %pI6 %d\n",
302 &sk
->sk_v6_rcv_saddr
,
303 inet_sk(sk
)->inet_num
);
307 len
= snprintf(buf
, remaining
, "*unknown-%d*\n",
311 if (len
>= remaining
) {
313 return -ENAMETOOLONG
;
319 * Generic recvfrom routine.
321 static ssize_t
svc_recvfrom(struct svc_rqst
*rqstp
, struct kvec
*iov
,
322 unsigned int nr
, size_t buflen
, unsigned int base
)
324 struct svc_sock
*svsk
=
325 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
326 struct msghdr msg
= { NULL
};
329 rqstp
->rq_xprt_hlen
= 0;
331 clear_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
332 iov_iter_kvec(&msg
.msg_iter
, READ
, iov
, nr
, buflen
);
334 iov_iter_advance(&msg
.msg_iter
, base
);
337 len
= sock_recvmsg(svsk
->sk_sock
, &msg
, MSG_DONTWAIT
);
338 /* If we read a full record, then assume there may be more
339 * data to read (stream based sockets only!)
342 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
344 dprintk("svc: socket %p recvfrom(%p, %zu) = %zd\n",
345 svsk
, iov
[0].iov_base
, iov
[0].iov_len
, len
);
350 * Set socket snd and rcv buffer lengths
352 static void svc_sock_setbufsize(struct svc_sock
*svsk
, unsigned int nreqs
)
354 unsigned int max_mesg
= svsk
->sk_xprt
.xpt_server
->sv_max_mesg
;
355 struct socket
*sock
= svsk
->sk_sock
;
357 nreqs
= min(nreqs
, INT_MAX
/ 2 / max_mesg
);
360 sock
->sk
->sk_sndbuf
= nreqs
* max_mesg
* 2;
361 sock
->sk
->sk_rcvbuf
= nreqs
* max_mesg
* 2;
362 sock
->sk
->sk_write_space(sock
->sk
);
363 release_sock(sock
->sk
);
366 static void svc_sock_secure_port(struct svc_rqst
*rqstp
)
368 if (svc_port_is_privileged(svc_addr(rqstp
)))
369 set_bit(RQ_SECURE
, &rqstp
->rq_flags
);
371 clear_bit(RQ_SECURE
, &rqstp
->rq_flags
);
375 * INET callback when data has been received on the socket.
377 static void svc_data_ready(struct sock
*sk
)
379 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
382 dprintk("svc: socket %p(inet %p), busy=%d\n",
384 test_bit(XPT_BUSY
, &svsk
->sk_xprt
.xpt_flags
));
386 /* Refer to svc_setup_socket() for details. */
389 if (!test_and_set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
))
390 svc_xprt_enqueue(&svsk
->sk_xprt
);
395 * INET callback when space is newly available on the socket.
397 static void svc_write_space(struct sock
*sk
)
399 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->sk_user_data
);
402 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
403 svsk
, sk
, test_bit(XPT_BUSY
, &svsk
->sk_xprt
.xpt_flags
));
405 /* Refer to svc_setup_socket() for details. */
407 svsk
->sk_owspace(sk
);
408 svc_xprt_enqueue(&svsk
->sk_xprt
);
412 static int svc_tcp_has_wspace(struct svc_xprt
*xprt
)
414 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
416 if (test_bit(XPT_LISTENER
, &xprt
->xpt_flags
))
418 return !test_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
421 static void svc_tcp_kill_temp_xprt(struct svc_xprt
*xprt
)
423 struct svc_sock
*svsk
;
425 struct linger no_linger
= {
430 svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
431 sock
= svsk
->sk_sock
;
432 kernel_setsockopt(sock
, SOL_SOCKET
, SO_LINGER
,
433 (char *)&no_linger
, sizeof(no_linger
));
437 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
439 static int svc_udp_get_dest_address4(struct svc_rqst
*rqstp
,
442 struct in_pktinfo
*pki
= CMSG_DATA(cmh
);
443 struct sockaddr_in
*daddr
= svc_daddr_in(rqstp
);
445 if (cmh
->cmsg_type
!= IP_PKTINFO
)
448 daddr
->sin_family
= AF_INET
;
449 daddr
->sin_addr
.s_addr
= pki
->ipi_spec_dst
.s_addr
;
454 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
456 static int svc_udp_get_dest_address6(struct svc_rqst
*rqstp
,
459 struct in6_pktinfo
*pki
= CMSG_DATA(cmh
);
460 struct sockaddr_in6
*daddr
= svc_daddr_in6(rqstp
);
462 if (cmh
->cmsg_type
!= IPV6_PKTINFO
)
465 daddr
->sin6_family
= AF_INET6
;
466 daddr
->sin6_addr
= pki
->ipi6_addr
;
467 daddr
->sin6_scope_id
= pki
->ipi6_ifindex
;
472 * Copy the UDP datagram's destination address to the rqstp structure.
473 * The 'destination' address in this case is the address to which the
474 * peer sent the datagram, i.e. our local address. For multihomed
475 * hosts, this can change from msg to msg. Note that only the IP
476 * address changes, the port number should remain the same.
478 static int svc_udp_get_dest_address(struct svc_rqst
*rqstp
,
481 switch (cmh
->cmsg_level
) {
483 return svc_udp_get_dest_address4(rqstp
, cmh
);
485 return svc_udp_get_dest_address6(rqstp
, cmh
);
492 * Receive a datagram from a UDP socket.
494 static int svc_udp_recvfrom(struct svc_rqst
*rqstp
)
496 struct svc_sock
*svsk
=
497 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
498 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
502 long all
[SVC_PKTINFO_SPACE
/ sizeof(long)];
504 struct cmsghdr
*cmh
= &buffer
.hdr
;
505 struct msghdr msg
= {
506 .msg_name
= svc_addr(rqstp
),
508 .msg_controllen
= sizeof(buffer
),
509 .msg_flags
= MSG_DONTWAIT
,
514 if (test_and_clear_bit(XPT_CHNGBUF
, &svsk
->sk_xprt
.xpt_flags
))
515 /* udp sockets need large rcvbuf as all pending
516 * requests are still in that buffer. sndbuf must
517 * also be large enough that there is enough space
518 * for one reply per thread. We count all threads
519 * rather than threads in a particular pool, which
520 * provides an upper bound on the number of threads
521 * which will access the socket.
523 svc_sock_setbufsize(svsk
, serv
->sv_nrthreads
+ 3);
525 clear_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
527 err
= kernel_recvmsg(svsk
->sk_sock
, &msg
, NULL
,
528 0, 0, MSG_PEEK
| MSG_DONTWAIT
);
530 skb
= skb_recv_udp(svsk
->sk_sk
, 0, 1, &err
);
533 if (err
!= -EAGAIN
) {
534 /* possibly an icmp error */
535 dprintk("svc: recvfrom returned error %d\n", -err
);
536 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
540 len
= svc_addr_len(svc_addr(rqstp
));
541 rqstp
->rq_addrlen
= len
;
542 if (skb
->tstamp
== 0) {
543 skb
->tstamp
= ktime_get_real();
544 /* Don't enable netstamp, sunrpc doesn't
545 need that much accuracy */
547 sock_write_timestamp(svsk
->sk_sk
, skb
->tstamp
);
548 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
); /* there may be more data... */
551 rqstp
->rq_arg
.len
= len
;
553 rqstp
->rq_prot
= IPPROTO_UDP
;
555 if (!svc_udp_get_dest_address(rqstp
, cmh
)) {
556 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
557 cmh
->cmsg_level
, cmh
->cmsg_type
);
560 rqstp
->rq_daddrlen
= svc_addr_len(svc_daddr(rqstp
));
562 if (skb_is_nonlinear(skb
)) {
563 /* we have to copy */
565 if (csum_partial_copy_to_xdr(&rqstp
->rq_arg
, skb
)) {
573 /* we can use it in-place */
574 rqstp
->rq_arg
.head
[0].iov_base
= skb
->data
;
575 rqstp
->rq_arg
.head
[0].iov_len
= len
;
576 if (skb_checksum_complete(skb
))
578 rqstp
->rq_xprt_ctxt
= skb
;
581 rqstp
->rq_arg
.page_base
= 0;
582 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
583 rqstp
->rq_arg
.head
[0].iov_len
= len
;
584 rqstp
->rq_arg
.page_len
= 0;
585 rqstp
->rq_respages
= rqstp
->rq_pages
+1;
587 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
588 rqstp
->rq_respages
= rqstp
->rq_pages
+ 1 +
589 DIV_ROUND_UP(rqstp
->rq_arg
.page_len
, PAGE_SIZE
);
591 rqstp
->rq_next_page
= rqstp
->rq_respages
+1;
594 serv
->sv_stats
->netudpcnt
++;
603 svc_udp_sendto(struct svc_rqst
*rqstp
)
607 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
608 if (error
== -ECONNREFUSED
)
609 /* ICMP error on earlier request. */
610 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
615 static int svc_udp_has_wspace(struct svc_xprt
*xprt
)
617 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
618 struct svc_serv
*serv
= xprt
->xpt_server
;
619 unsigned long required
;
622 * Set the SOCK_NOSPACE flag before checking the available
625 set_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
626 required
= atomic_read(&svsk
->sk_xprt
.xpt_reserved
) + serv
->sv_max_mesg
;
627 if (required
*2 > sock_wspace(svsk
->sk_sk
))
629 clear_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
633 static struct svc_xprt
*svc_udp_accept(struct svc_xprt
*xprt
)
639 static void svc_udp_kill_temp_xprt(struct svc_xprt
*xprt
)
643 static struct svc_xprt
*svc_udp_create(struct svc_serv
*serv
,
645 struct sockaddr
*sa
, int salen
,
648 return svc_create_socket(serv
, IPPROTO_UDP
, net
, sa
, salen
, flags
);
651 static const struct svc_xprt_ops svc_udp_ops
= {
652 .xpo_create
= svc_udp_create
,
653 .xpo_recvfrom
= svc_udp_recvfrom
,
654 .xpo_sendto
= svc_udp_sendto
,
655 .xpo_release_rqst
= svc_release_udp_skb
,
656 .xpo_detach
= svc_sock_detach
,
657 .xpo_free
= svc_sock_free
,
658 .xpo_has_wspace
= svc_udp_has_wspace
,
659 .xpo_accept
= svc_udp_accept
,
660 .xpo_secure_port
= svc_sock_secure_port
,
661 .xpo_kill_temp_xprt
= svc_udp_kill_temp_xprt
,
664 static struct svc_xprt_class svc_udp_class
= {
666 .xcl_owner
= THIS_MODULE
,
667 .xcl_ops
= &svc_udp_ops
,
668 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_UDP
,
669 .xcl_ident
= XPRT_TRANSPORT_UDP
,
672 static void svc_udp_init(struct svc_sock
*svsk
, struct svc_serv
*serv
)
674 int err
, level
, optname
, one
= 1;
676 svc_xprt_init(sock_net(svsk
->sk_sock
->sk
), &svc_udp_class
,
677 &svsk
->sk_xprt
, serv
);
678 clear_bit(XPT_CACHE_AUTH
, &svsk
->sk_xprt
.xpt_flags
);
679 svsk
->sk_sk
->sk_data_ready
= svc_data_ready
;
680 svsk
->sk_sk
->sk_write_space
= svc_write_space
;
682 /* initialise setting must have enough space to
683 * receive and respond to one request.
684 * svc_udp_recvfrom will re-adjust if necessary
686 svc_sock_setbufsize(svsk
, 3);
688 /* data might have come in before data_ready set up */
689 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
690 set_bit(XPT_CHNGBUF
, &svsk
->sk_xprt
.xpt_flags
);
692 /* make sure we get destination address info */
693 switch (svsk
->sk_sk
->sk_family
) {
696 optname
= IP_PKTINFO
;
700 optname
= IPV6_RECVPKTINFO
;
705 err
= kernel_setsockopt(svsk
->sk_sock
, level
, optname
,
706 (char *)&one
, sizeof(one
));
707 dprintk("svc: kernel_setsockopt returned %d\n", err
);
711 * A data_ready event on a listening socket means there's a connection
712 * pending. Do not use state_change as a substitute for it.
714 static void svc_tcp_listen_data_ready(struct sock
*sk
)
716 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
718 dprintk("svc: socket %p TCP (listen) state change %d\n",
722 /* Refer to svc_setup_socket() for details. */
728 * This callback may called twice when a new connection
729 * is established as a child socket inherits everything
730 * from a parent LISTEN socket.
731 * 1) data_ready method of the parent socket will be called
732 * when one of child sockets become ESTABLISHED.
733 * 2) data_ready method of the child socket may be called
734 * when it receives data before the socket is accepted.
735 * In case of 2, we should ignore it silently.
737 if (sk
->sk_state
== TCP_LISTEN
) {
739 set_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
740 svc_xprt_enqueue(&svsk
->sk_xprt
);
742 printk("svc: socket %p: no user data\n", sk
);
747 * A state change on a connected socket means it's dying or dead.
749 static void svc_tcp_state_change(struct sock
*sk
)
751 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
753 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
754 sk
, sk
->sk_state
, sk
->sk_user_data
);
757 printk("svc: socket %p: no user data\n", sk
);
759 /* Refer to svc_setup_socket() for details. */
762 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
763 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
764 svc_xprt_enqueue(&svsk
->sk_xprt
);
770 * Accept a TCP connection
772 static struct svc_xprt
*svc_tcp_accept(struct svc_xprt
*xprt
)
774 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
775 struct sockaddr_storage addr
;
776 struct sockaddr
*sin
= (struct sockaddr
*) &addr
;
777 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
778 struct socket
*sock
= svsk
->sk_sock
;
779 struct socket
*newsock
;
780 struct svc_sock
*newsvsk
;
782 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
784 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
788 clear_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
789 err
= kernel_accept(sock
, &newsock
, O_NONBLOCK
);
792 printk(KERN_WARNING
"%s: no more sockets!\n",
794 else if (err
!= -EAGAIN
)
795 net_warn_ratelimited("%s: accept failed (err %d)!\n",
796 serv
->sv_name
, -err
);
799 set_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
801 err
= kernel_getpeername(newsock
, sin
);
803 net_warn_ratelimited("%s: peername failed (err %d)!\n",
804 serv
->sv_name
, -err
);
805 goto failed
; /* aborted connection or whatever */
809 /* Ideally, we would want to reject connections from unauthorized
810 * hosts here, but when we get encryption, the IP of the host won't
811 * tell us anything. For now just warn about unpriv connections.
813 if (!svc_port_is_privileged(sin
)) {
814 dprintk("%s: connect from unprivileged port: %s\n",
816 __svc_print_addr(sin
, buf
, sizeof(buf
)));
818 dprintk("%s: connect from %s\n", serv
->sv_name
,
819 __svc_print_addr(sin
, buf
, sizeof(buf
)));
821 /* Reset the inherited callbacks before calling svc_setup_socket */
822 newsock
->sk
->sk_state_change
= svsk
->sk_ostate
;
823 newsock
->sk
->sk_data_ready
= svsk
->sk_odata
;
824 newsock
->sk
->sk_write_space
= svsk
->sk_owspace
;
826 /* make sure that a write doesn't block forever when
829 newsock
->sk
->sk_sndtimeo
= HZ
*30;
831 newsvsk
= svc_setup_socket(serv
, newsock
,
832 (SVC_SOCK_ANONYMOUS
| SVC_SOCK_TEMPORARY
));
835 svc_xprt_set_remote(&newsvsk
->sk_xprt
, sin
, slen
);
836 err
= kernel_getsockname(newsock
, sin
);
838 if (unlikely(err
< 0)) {
839 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err
);
840 slen
= offsetof(struct sockaddr
, sa_data
);
842 svc_xprt_set_local(&newsvsk
->sk_xprt
, sin
, slen
);
844 if (sock_is_loopback(newsock
->sk
))
845 set_bit(XPT_LOCAL
, &newsvsk
->sk_xprt
.xpt_flags
);
847 clear_bit(XPT_LOCAL
, &newsvsk
->sk_xprt
.xpt_flags
);
849 serv
->sv_stats
->nettcpconn
++;
851 return &newsvsk
->sk_xprt
;
854 sock_release(newsock
);
858 static unsigned int svc_tcp_restore_pages(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
860 unsigned int i
, len
, npages
;
862 if (svsk
->sk_datalen
== 0)
864 len
= svsk
->sk_datalen
;
865 npages
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
866 for (i
= 0; i
< npages
; i
++) {
867 if (rqstp
->rq_pages
[i
] != NULL
)
868 put_page(rqstp
->rq_pages
[i
]);
869 BUG_ON(svsk
->sk_pages
[i
] == NULL
);
870 rqstp
->rq_pages
[i
] = svsk
->sk_pages
[i
];
871 svsk
->sk_pages
[i
] = NULL
;
873 rqstp
->rq_arg
.head
[0].iov_base
= page_address(rqstp
->rq_pages
[0]);
877 static void svc_tcp_save_pages(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
879 unsigned int i
, len
, npages
;
881 if (svsk
->sk_datalen
== 0)
883 len
= svsk
->sk_datalen
;
884 npages
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
885 for (i
= 0; i
< npages
; i
++) {
886 svsk
->sk_pages
[i
] = rqstp
->rq_pages
[i
];
887 rqstp
->rq_pages
[i
] = NULL
;
891 static void svc_tcp_clear_pages(struct svc_sock
*svsk
)
893 unsigned int i
, len
, npages
;
895 if (svsk
->sk_datalen
== 0)
897 len
= svsk
->sk_datalen
;
898 npages
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
899 for (i
= 0; i
< npages
; i
++) {
900 if (svsk
->sk_pages
[i
] == NULL
) {
904 put_page(svsk
->sk_pages
[i
]);
905 svsk
->sk_pages
[i
] = NULL
;
909 svsk
->sk_datalen
= 0;
913 * Receive fragment record header.
914 * If we haven't gotten the record length yet, get the next four bytes.
916 static int svc_tcp_recv_record(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
918 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
922 if (svsk
->sk_tcplen
< sizeof(rpc_fraghdr
)) {
925 want
= sizeof(rpc_fraghdr
) - svsk
->sk_tcplen
;
926 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
928 len
= svc_recvfrom(rqstp
, &iov
, 1, want
, 0);
931 svsk
->sk_tcplen
+= len
;
934 dprintk("svc: short recvfrom while reading record "
935 "length (%d of %d)\n", len
, want
);
939 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk
));
940 if (svc_sock_reclen(svsk
) + svsk
->sk_datalen
>
942 net_notice_ratelimited("RPC: fragment too large: %d\n",
943 svc_sock_reclen(svsk
));
948 return svc_sock_reclen(svsk
);
950 dprintk("RPC: TCP recv_record got %d\n", len
);
953 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
957 static int receive_cb_reply(struct svc_sock
*svsk
, struct svc_rqst
*rqstp
)
959 struct rpc_xprt
*bc_xprt
= svsk
->sk_xprt
.xpt_bc_xprt
;
960 struct rpc_rqst
*req
= NULL
;
961 struct kvec
*src
, *dst
;
962 __be32
*p
= (__be32
*)rqstp
->rq_arg
.head
[0].iov_base
;
971 spin_lock(&bc_xprt
->queue_lock
);
972 req
= xprt_lookup_rqst(bc_xprt
, xid
);
974 goto unlock_notfound
;
976 memcpy(&req
->rq_private_buf
, &req
->rq_rcv_buf
, sizeof(struct xdr_buf
));
978 * XXX!: cheating for now! Only copying HEAD.
979 * But we know this is good enough for now (in fact, for any
980 * callback reply in the forseeable future).
982 dst
= &req
->rq_private_buf
.head
[0];
983 src
= &rqstp
->rq_arg
.head
[0];
984 if (dst
->iov_len
< src
->iov_len
)
985 goto unlock_eagain
; /* whatever; just giving up. */
986 memcpy(dst
->iov_base
, src
->iov_base
, src
->iov_len
);
987 xprt_complete_rqst(req
->rq_task
, rqstp
->rq_arg
.len
);
988 rqstp
->rq_arg
.len
= 0;
989 spin_unlock(&bc_xprt
->queue_lock
);
993 "%s: Got unrecognized reply: "
994 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
995 __func__
, ntohl(calldir
),
996 bc_xprt
, ntohl(xid
));
998 spin_unlock(&bc_xprt
->queue_lock
);
1002 static int copy_pages_to_kvecs(struct kvec
*vec
, struct page
**pages
, int len
)
1008 vec
[i
].iov_base
= page_address(pages
[i
]);
1009 vec
[i
].iov_len
= PAGE_SIZE
;
1016 static void svc_tcp_fragment_received(struct svc_sock
*svsk
)
1018 /* If we have more data, signal svc_xprt_enqueue() to try again */
1019 dprintk("svc: TCP %s record (%d bytes)\n",
1020 svc_sock_final_rec(svsk
) ? "final" : "nonfinal",
1021 svc_sock_reclen(svsk
));
1022 svsk
->sk_tcplen
= 0;
1023 svsk
->sk_reclen
= 0;
1027 * Receive data from a TCP socket.
1029 static int svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
1031 struct svc_sock
*svsk
=
1032 container_of(rqstp
->rq_xprt
, struct svc_sock
, sk_xprt
);
1033 struct svc_serv
*serv
= svsk
->sk_xprt
.xpt_server
;
1036 unsigned int want
, base
;
1041 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1042 svsk
, test_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
),
1043 test_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
),
1044 test_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
));
1046 len
= svc_tcp_recv_record(svsk
, rqstp
);
1050 base
= svc_tcp_restore_pages(svsk
, rqstp
);
1051 want
= svc_sock_reclen(svsk
) - (svsk
->sk_tcplen
- sizeof(rpc_fraghdr
));
1053 vec
= rqstp
->rq_vec
;
1055 pnum
= copy_pages_to_kvecs(&vec
[0], &rqstp
->rq_pages
[0], base
+ want
);
1057 rqstp
->rq_respages
= &rqstp
->rq_pages
[pnum
];
1058 rqstp
->rq_next_page
= rqstp
->rq_respages
+ 1;
1060 /* Now receive data */
1061 len
= svc_recvfrom(rqstp
, vec
, pnum
, base
+ want
, base
);
1063 svsk
->sk_tcplen
+= len
;
1064 svsk
->sk_datalen
+= len
;
1066 if (len
!= want
|| !svc_sock_final_rec(svsk
)) {
1067 svc_tcp_save_pages(svsk
, rqstp
);
1068 if (len
< 0 && len
!= -EAGAIN
)
1071 svc_tcp_fragment_received(svsk
);
1073 dprintk("svc: incomplete TCP record (%d of %d)\n",
1074 (int)(svsk
->sk_tcplen
- sizeof(rpc_fraghdr
)),
1075 svc_sock_reclen(svsk
));
1079 if (svsk
->sk_datalen
< 8) {
1080 svsk
->sk_datalen
= 0;
1081 goto err_delete
; /* client is nuts. */
1084 rqstp
->rq_arg
.len
= svsk
->sk_datalen
;
1085 rqstp
->rq_arg
.page_base
= 0;
1086 if (rqstp
->rq_arg
.len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
1087 rqstp
->rq_arg
.head
[0].iov_len
= rqstp
->rq_arg
.len
;
1088 rqstp
->rq_arg
.page_len
= 0;
1090 rqstp
->rq_arg
.page_len
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
1092 rqstp
->rq_xprt_ctxt
= NULL
;
1093 rqstp
->rq_prot
= IPPROTO_TCP
;
1094 if (test_bit(XPT_LOCAL
, &svsk
->sk_xprt
.xpt_flags
))
1095 set_bit(RQ_LOCAL
, &rqstp
->rq_flags
);
1097 clear_bit(RQ_LOCAL
, &rqstp
->rq_flags
);
1099 p
= (__be32
*)rqstp
->rq_arg
.head
[0].iov_base
;
1102 len
= receive_cb_reply(svsk
, rqstp
);
1104 /* Reset TCP read info */
1105 svsk
->sk_datalen
= 0;
1106 svc_tcp_fragment_received(svsk
);
1111 svc_xprt_copy_addrs(rqstp
, &svsk
->sk_xprt
);
1113 serv
->sv_stats
->nettcpcnt
++;
1115 return rqstp
->rq_arg
.len
;
1120 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1123 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
1124 svsk
->sk_xprt
.xpt_server
->sv_name
, -len
);
1125 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
1127 return 0; /* record not complete */
1131 * Send out data on TCP socket.
1133 static int svc_tcp_sendto(struct svc_rqst
*rqstp
)
1135 struct xdr_buf
*xbufp
= &rqstp
->rq_res
;
1139 /* Set up the first element of the reply kvec.
1140 * Any other kvecs that may be in use have been taken
1141 * care of by the server implementation itself.
1143 reclen
= htonl(0x80000000|((xbufp
->len
) - 4));
1144 memcpy(xbufp
->head
[0].iov_base
, &reclen
, 4);
1146 sent
= svc_sendto(rqstp
, &rqstp
->rq_res
);
1147 if (sent
!= xbufp
->len
) {
1149 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1150 "- shutting down socket\n",
1151 rqstp
->rq_xprt
->xpt_server
->sv_name
,
1152 (sent
<0)?"got error":"sent only",
1154 set_bit(XPT_CLOSE
, &rqstp
->rq_xprt
->xpt_flags
);
1155 svc_xprt_enqueue(rqstp
->rq_xprt
);
1161 static struct svc_xprt
*svc_tcp_create(struct svc_serv
*serv
,
1163 struct sockaddr
*sa
, int salen
,
1166 return svc_create_socket(serv
, IPPROTO_TCP
, net
, sa
, salen
, flags
);
1169 static const struct svc_xprt_ops svc_tcp_ops
= {
1170 .xpo_create
= svc_tcp_create
,
1171 .xpo_recvfrom
= svc_tcp_recvfrom
,
1172 .xpo_sendto
= svc_tcp_sendto
,
1173 .xpo_release_rqst
= svc_release_skb
,
1174 .xpo_detach
= svc_tcp_sock_detach
,
1175 .xpo_free
= svc_sock_free
,
1176 .xpo_has_wspace
= svc_tcp_has_wspace
,
1177 .xpo_accept
= svc_tcp_accept
,
1178 .xpo_secure_port
= svc_sock_secure_port
,
1179 .xpo_kill_temp_xprt
= svc_tcp_kill_temp_xprt
,
1182 static struct svc_xprt_class svc_tcp_class
= {
1184 .xcl_owner
= THIS_MODULE
,
1185 .xcl_ops
= &svc_tcp_ops
,
1186 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
1187 .xcl_ident
= XPRT_TRANSPORT_TCP
,
1190 void svc_init_xprt_sock(void)
1192 svc_reg_xprt_class(&svc_tcp_class
);
1193 svc_reg_xprt_class(&svc_udp_class
);
1196 void svc_cleanup_xprt_sock(void)
1198 svc_unreg_xprt_class(&svc_tcp_class
);
1199 svc_unreg_xprt_class(&svc_udp_class
);
1202 static void svc_tcp_init(struct svc_sock
*svsk
, struct svc_serv
*serv
)
1204 struct sock
*sk
= svsk
->sk_sk
;
1206 svc_xprt_init(sock_net(svsk
->sk_sock
->sk
), &svc_tcp_class
,
1207 &svsk
->sk_xprt
, serv
);
1208 set_bit(XPT_CACHE_AUTH
, &svsk
->sk_xprt
.xpt_flags
);
1209 set_bit(XPT_CONG_CTRL
, &svsk
->sk_xprt
.xpt_flags
);
1210 if (sk
->sk_state
== TCP_LISTEN
) {
1211 dprintk("setting up TCP socket for listening\n");
1212 strcpy(svsk
->sk_xprt
.xpt_remotebuf
, "listener");
1213 set_bit(XPT_LISTENER
, &svsk
->sk_xprt
.xpt_flags
);
1214 sk
->sk_data_ready
= svc_tcp_listen_data_ready
;
1215 set_bit(XPT_CONN
, &svsk
->sk_xprt
.xpt_flags
);
1217 dprintk("setting up TCP socket for reading\n");
1218 sk
->sk_state_change
= svc_tcp_state_change
;
1219 sk
->sk_data_ready
= svc_data_ready
;
1220 sk
->sk_write_space
= svc_write_space
;
1222 svsk
->sk_reclen
= 0;
1223 svsk
->sk_tcplen
= 0;
1224 svsk
->sk_datalen
= 0;
1225 memset(&svsk
->sk_pages
[0], 0, sizeof(svsk
->sk_pages
));
1227 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1229 set_bit(XPT_DATA
, &svsk
->sk_xprt
.xpt_flags
);
1230 switch (sk
->sk_state
) {
1232 case TCP_ESTABLISHED
:
1235 set_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
);
1240 void svc_sock_update_bufs(struct svc_serv
*serv
)
1243 * The number of server threads has changed. Update
1244 * rcvbuf and sndbuf accordingly on all sockets
1246 struct svc_sock
*svsk
;
1248 spin_lock_bh(&serv
->sv_lock
);
1249 list_for_each_entry(svsk
, &serv
->sv_permsocks
, sk_xprt
.xpt_list
)
1250 set_bit(XPT_CHNGBUF
, &svsk
->sk_xprt
.xpt_flags
);
1251 spin_unlock_bh(&serv
->sv_lock
);
1253 EXPORT_SYMBOL_GPL(svc_sock_update_bufs
);
1256 * Initialize socket for RPC use and create svc_sock struct
1258 static struct svc_sock
*svc_setup_socket(struct svc_serv
*serv
,
1259 struct socket
*sock
,
1262 struct svc_sock
*svsk
;
1264 int pmap_register
= !(flags
& SVC_SOCK_ANONYMOUS
);
1267 dprintk("svc: svc_setup_socket %p\n", sock
);
1268 svsk
= kzalloc(sizeof(*svsk
), GFP_KERNEL
);
1270 return ERR_PTR(-ENOMEM
);
1274 /* Register socket with portmapper */
1276 err
= svc_register(serv
, sock_net(sock
->sk
), inet
->sk_family
,
1278 ntohs(inet_sk(inet
)->inet_sport
));
1282 return ERR_PTR(err
);
1285 svsk
->sk_sock
= sock
;
1287 svsk
->sk_ostate
= inet
->sk_state_change
;
1288 svsk
->sk_odata
= inet
->sk_data_ready
;
1289 svsk
->sk_owspace
= inet
->sk_write_space
;
1291 * This barrier is necessary in order to prevent race condition
1292 * with svc_data_ready(), svc_listen_data_ready() and others
1293 * when calling callbacks above.
1296 inet
->sk_user_data
= svsk
;
1298 /* Initialize the socket */
1299 if (sock
->type
== SOCK_DGRAM
)
1300 svc_udp_init(svsk
, serv
);
1302 svc_tcp_init(svsk
, serv
);
1304 dprintk("svc: svc_setup_socket created %p (inet %p), "
1305 "listen %d close %d\n",
1307 test_bit(XPT_LISTENER
, &svsk
->sk_xprt
.xpt_flags
),
1308 test_bit(XPT_CLOSE
, &svsk
->sk_xprt
.xpt_flags
));
1313 bool svc_alien_sock(struct net
*net
, int fd
)
1316 struct socket
*sock
= sockfd_lookup(fd
, &err
);
1321 if (sock_net(sock
->sk
) != net
)
1327 EXPORT_SYMBOL_GPL(svc_alien_sock
);
1330 * svc_addsock - add a listener socket to an RPC service
1331 * @serv: pointer to RPC service to which to add a new listener
1332 * @fd: file descriptor of the new listener
1333 * @name_return: pointer to buffer to fill in with name of listener
1334 * @len: size of the buffer
1337 * Fills in socket name and returns positive length of name if successful.
1338 * Name is terminated with '\n'. On error, returns a negative errno
1341 int svc_addsock(struct svc_serv
*serv
, const int fd
, char *name_return
,
1342 const size_t len
, const struct cred
*cred
)
1345 struct socket
*so
= sockfd_lookup(fd
, &err
);
1346 struct svc_sock
*svsk
= NULL
;
1347 struct sockaddr_storage addr
;
1348 struct sockaddr
*sin
= (struct sockaddr
*)&addr
;
1353 err
= -EAFNOSUPPORT
;
1354 if ((so
->sk
->sk_family
!= PF_INET
) && (so
->sk
->sk_family
!= PF_INET6
))
1356 err
= -EPROTONOSUPPORT
;
1357 if (so
->sk
->sk_protocol
!= IPPROTO_TCP
&&
1358 so
->sk
->sk_protocol
!= IPPROTO_UDP
)
1361 if (so
->state
> SS_UNCONNECTED
)
1364 if (!try_module_get(THIS_MODULE
))
1366 svsk
= svc_setup_socket(serv
, so
, SVC_SOCK_DEFAULTS
);
1368 module_put(THIS_MODULE
);
1369 err
= PTR_ERR(svsk
);
1372 salen
= kernel_getsockname(svsk
->sk_sock
, sin
);
1374 svc_xprt_set_local(&svsk
->sk_xprt
, sin
, salen
);
1375 svsk
->sk_xprt
.xpt_cred
= get_cred(cred
);
1376 svc_add_new_perm_xprt(serv
, &svsk
->sk_xprt
);
1377 return svc_one_sock_name(svsk
, name_return
, len
);
1382 EXPORT_SYMBOL_GPL(svc_addsock
);
1385 * Create socket for RPC service.
1387 static struct svc_xprt
*svc_create_socket(struct svc_serv
*serv
,
1390 struct sockaddr
*sin
, int len
,
1393 struct svc_sock
*svsk
;
1394 struct socket
*sock
;
1397 struct sockaddr_storage addr
;
1398 struct sockaddr
*newsin
= (struct sockaddr
*)&addr
;
1402 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
1404 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1405 serv
->sv_program
->pg_name
, protocol
,
1406 __svc_print_addr(sin
, buf
, sizeof(buf
)));
1408 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
1409 printk(KERN_WARNING
"svc: only UDP and TCP "
1410 "sockets supported\n");
1411 return ERR_PTR(-EINVAL
);
1414 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
1415 switch (sin
->sa_family
) {
1423 return ERR_PTR(-EINVAL
);
1426 error
= __sock_create(net
, family
, type
, protocol
, &sock
, 1);
1428 return ERR_PTR(error
);
1430 svc_reclassify_socket(sock
);
1433 * If this is an PF_INET6 listener, we want to avoid
1434 * getting requests from IPv4 remotes. Those should
1435 * be shunted to a PF_INET listener via rpcbind.
1438 if (family
== PF_INET6
)
1439 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_V6ONLY
,
1440 (char *)&val
, sizeof(val
));
1442 if (type
== SOCK_STREAM
)
1443 sock
->sk
->sk_reuse
= SK_CAN_REUSE
; /* allow address reuse */
1444 error
= kernel_bind(sock
, sin
, len
);
1448 error
= kernel_getsockname(sock
, newsin
);
1453 if (protocol
== IPPROTO_TCP
) {
1454 if ((error
= kernel_listen(sock
, 64)) < 0)
1458 svsk
= svc_setup_socket(serv
, sock
, flags
);
1460 error
= PTR_ERR(svsk
);
1463 svc_xprt_set_local(&svsk
->sk_xprt
, newsin
, newlen
);
1464 return (struct svc_xprt
*)svsk
;
1466 dprintk("svc: svc_create_socket error = %d\n", -error
);
1468 return ERR_PTR(error
);
1472 * Detach the svc_sock from the socket so that no
1473 * more callbacks occur.
1475 static void svc_sock_detach(struct svc_xprt
*xprt
)
1477 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
1478 struct sock
*sk
= svsk
->sk_sk
;
1480 dprintk("svc: svc_sock_detach(%p)\n", svsk
);
1482 /* put back the old socket callbacks */
1484 sk
->sk_state_change
= svsk
->sk_ostate
;
1485 sk
->sk_data_ready
= svsk
->sk_odata
;
1486 sk
->sk_write_space
= svsk
->sk_owspace
;
1487 sk
->sk_user_data
= NULL
;
1492 * Disconnect the socket, and reset the callbacks
1494 static void svc_tcp_sock_detach(struct svc_xprt
*xprt
)
1496 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
1498 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk
);
1500 svc_sock_detach(xprt
);
1502 if (!test_bit(XPT_LISTENER
, &xprt
->xpt_flags
)) {
1503 svc_tcp_clear_pages(svsk
);
1504 kernel_sock_shutdown(svsk
->sk_sock
, SHUT_RDWR
);
1509 * Free the svc_sock's socket resources and the svc_sock itself.
1511 static void svc_sock_free(struct svc_xprt
*xprt
)
1513 struct svc_sock
*svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
1514 dprintk("svc: svc_sock_free(%p)\n", svsk
);
1516 if (svsk
->sk_sock
->file
)
1517 sockfd_put(svsk
->sk_sock
);
1519 sock_release(svsk
->sk_sock
);