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_sock_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/sched.h>
23 #include <linux/errno.h>
24 #include <linux/fcntl.h>
25 #include <linux/net.h>
27 #include <linux/inet.h>
28 #include <linux/udp.h>
29 #include <linux/tcp.h>
30 #include <linux/unistd.h>
31 #include <linux/slab.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/file.h>
35 #include <linux/freezer.h>
37 #include <net/checksum.h>
40 #include <net/tcp_states.h>
41 #include <asm/uaccess.h>
42 #include <asm/ioctls.h>
44 #include <linux/sunrpc/types.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/xdr.h>
47 #include <linux/sunrpc/svcsock.h>
48 #include <linux/sunrpc/stats.h>
50 /* SMP locking strategy:
52 * svc_pool->sp_lock protects most of the fields of that pool.
53 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
54 * when both need to be taken (rare), svc_serv->sv_lock is first.
55 * BKL protects svc_serv->sv_nrthread.
56 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
57 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
59 * Some flags can be set to certain values at any time
60 * providing that certain rules are followed:
62 * SK_CONN, SK_DATA, can be set or cleared at any time.
63 * after a set, svc_sock_enqueue must be called.
64 * after a clear, the socket must be read/accepted
65 * if this succeeds, it must be set again.
66 * SK_CLOSE can set at any time. It is never cleared.
67 * sk_inuse contains a bias of '1' until SK_DEAD is set.
68 * so when sk_inuse hits zero, we know the socket is dead
69 * and no-one is using it.
70 * SK_DEAD can only be set while SK_BUSY is held which ensures
71 * no other thread will be using the socket or will try to
76 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
79 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
80 int *errp
, int flags
);
81 static void svc_delete_socket(struct svc_sock
*svsk
);
82 static void svc_udp_data_ready(struct sock
*, int);
83 static int svc_udp_recvfrom(struct svc_rqst
*);
84 static int svc_udp_sendto(struct svc_rqst
*);
86 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_sock
*svsk
);
87 static int svc_deferred_recv(struct svc_rqst
*rqstp
);
88 static struct cache_deferred_req
*svc_defer(struct cache_req
*req
);
90 /* apparently the "standard" is that clients close
91 * idle connections after 5 minutes, servers after
93 * http://www.connectathon.org/talks96/nfstcp.pdf
95 static int svc_conn_age_period
= 6*60;
97 #ifdef CONFIG_DEBUG_LOCK_ALLOC
98 static struct lock_class_key svc_key
[2];
99 static struct lock_class_key svc_slock_key
[2];
101 static inline void svc_reclassify_socket(struct socket
*sock
)
103 struct sock
*sk
= sock
->sk
;
104 BUG_ON(sk
->sk_lock
.owner
!= NULL
);
105 switch (sk
->sk_family
) {
107 sock_lock_init_class_and_name(sk
, "slock-AF_INET-NFSD",
108 &svc_slock_key
[0], "sk_lock-AF_INET-NFSD", &svc_key
[0]);
112 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-NFSD",
113 &svc_slock_key
[1], "sk_lock-AF_INET6-NFSD", &svc_key
[1]);
121 static inline void svc_reclassify_socket(struct socket
*sock
)
126 static char *__svc_print_addr(struct sockaddr
*addr
, char *buf
, size_t len
)
128 switch (addr
->sa_family
) {
130 snprintf(buf
, len
, "%u.%u.%u.%u, port=%u",
131 NIPQUAD(((struct sockaddr_in
*) addr
)->sin_addr
),
132 htons(((struct sockaddr_in
*) addr
)->sin_port
));
134 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
136 snprintf(buf
, len
, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
137 NIP6(((struct sockaddr_in6
*) addr
)->sin6_addr
),
138 htons(((struct sockaddr_in6
*) addr
)->sin6_port
));
142 snprintf(buf
, len
, "unknown address type: %d", addr
->sa_family
);
149 * svc_print_addr - Format rq_addr field for printing
150 * @rqstp: svc_rqst struct containing address to print
151 * @buf: target buffer for formatted address
152 * @len: length of target buffer
155 char *svc_print_addr(struct svc_rqst
*rqstp
, char *buf
, size_t len
)
157 return __svc_print_addr(svc_addr(rqstp
), buf
, len
);
159 EXPORT_SYMBOL_GPL(svc_print_addr
);
162 * Queue up an idle server thread. Must have pool->sp_lock held.
163 * Note: this is really a stack rather than a queue, so that we only
164 * use as many different threads as we need, and the rest don't pollute
168 svc_thread_enqueue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
170 list_add(&rqstp
->rq_list
, &pool
->sp_threads
);
174 * Dequeue an nfsd thread. Must have pool->sp_lock held.
177 svc_thread_dequeue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
179 list_del(&rqstp
->rq_list
);
183 * Release an skbuff after use
186 svc_release_skb(struct svc_rqst
*rqstp
)
188 struct sk_buff
*skb
= rqstp
->rq_skbuff
;
189 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
192 rqstp
->rq_skbuff
= NULL
;
194 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
195 skb_free_datagram(rqstp
->rq_sock
->sk_sk
, skb
);
198 rqstp
->rq_deferred
= NULL
;
204 * Any space to write?
206 static inline unsigned long
207 svc_sock_wspace(struct svc_sock
*svsk
)
211 if (svsk
->sk_sock
->type
== SOCK_STREAM
)
212 wspace
= sk_stream_wspace(svsk
->sk_sk
);
214 wspace
= sock_wspace(svsk
->sk_sk
);
220 * Queue up a socket with data pending. If there are idle nfsd
221 * processes, wake 'em up.
225 svc_sock_enqueue(struct svc_sock
*svsk
)
227 struct svc_serv
*serv
= svsk
->sk_server
;
228 struct svc_pool
*pool
;
229 struct svc_rqst
*rqstp
;
232 if (!(svsk
->sk_flags
&
233 ( (1<<SK_CONN
)|(1<<SK_DATA
)|(1<<SK_CLOSE
)|(1<<SK_DEFERRED
)) ))
235 if (test_bit(SK_DEAD
, &svsk
->sk_flags
))
239 pool
= svc_pool_for_cpu(svsk
->sk_server
, cpu
);
242 spin_lock_bh(&pool
->sp_lock
);
244 if (!list_empty(&pool
->sp_threads
) &&
245 !list_empty(&pool
->sp_sockets
))
247 "svc_sock_enqueue: threads and sockets both waiting??\n");
249 if (test_bit(SK_DEAD
, &svsk
->sk_flags
)) {
250 /* Don't enqueue dead sockets */
251 dprintk("svc: socket %p is dead, not enqueued\n", svsk
->sk_sk
);
255 /* Mark socket as busy. It will remain in this state until the
256 * server has processed all pending data and put the socket back
257 * on the idle list. We update SK_BUSY atomically because
258 * it also guards against trying to enqueue the svc_sock twice.
260 if (test_and_set_bit(SK_BUSY
, &svsk
->sk_flags
)) {
261 /* Don't enqueue socket while already enqueued */
262 dprintk("svc: socket %p busy, not enqueued\n", svsk
->sk_sk
);
265 BUG_ON(svsk
->sk_pool
!= NULL
);
266 svsk
->sk_pool
= pool
;
268 set_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
269 if (((atomic_read(&svsk
->sk_reserved
) + serv
->sv_max_mesg
)*2
270 > svc_sock_wspace(svsk
))
271 && !test_bit(SK_CLOSE
, &svsk
->sk_flags
)
272 && !test_bit(SK_CONN
, &svsk
->sk_flags
)) {
273 /* Don't enqueue while not enough space for reply */
274 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
275 svsk
->sk_sk
, atomic_read(&svsk
->sk_reserved
)+serv
->sv_max_mesg
,
276 svc_sock_wspace(svsk
));
277 svsk
->sk_pool
= NULL
;
278 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
281 clear_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
284 if (!list_empty(&pool
->sp_threads
)) {
285 rqstp
= list_entry(pool
->sp_threads
.next
,
288 dprintk("svc: socket %p served by daemon %p\n",
290 svc_thread_dequeue(pool
, rqstp
);
293 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
294 rqstp
, rqstp
->rq_sock
);
295 rqstp
->rq_sock
= svsk
;
296 atomic_inc(&svsk
->sk_inuse
);
297 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
298 atomic_add(rqstp
->rq_reserved
, &svsk
->sk_reserved
);
299 BUG_ON(svsk
->sk_pool
!= pool
);
300 wake_up(&rqstp
->rq_wait
);
302 dprintk("svc: socket %p put into queue\n", svsk
->sk_sk
);
303 list_add_tail(&svsk
->sk_ready
, &pool
->sp_sockets
);
304 BUG_ON(svsk
->sk_pool
!= pool
);
308 spin_unlock_bh(&pool
->sp_lock
);
312 * Dequeue the first socket. Must be called with the pool->sp_lock held.
314 static inline struct svc_sock
*
315 svc_sock_dequeue(struct svc_pool
*pool
)
317 struct svc_sock
*svsk
;
319 if (list_empty(&pool
->sp_sockets
))
322 svsk
= list_entry(pool
->sp_sockets
.next
,
323 struct svc_sock
, sk_ready
);
324 list_del_init(&svsk
->sk_ready
);
326 dprintk("svc: socket %p dequeued, inuse=%d\n",
327 svsk
->sk_sk
, atomic_read(&svsk
->sk_inuse
));
333 * Having read something from a socket, check whether it
334 * needs to be re-enqueued.
335 * Note: SK_DATA only gets cleared when a read-attempt finds
336 * no (or insufficient) data.
339 svc_sock_received(struct svc_sock
*svsk
)
341 svsk
->sk_pool
= NULL
;
342 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
343 svc_sock_enqueue(svsk
);
348 * svc_reserve - change the space reserved for the reply to a request.
349 * @rqstp: The request in question
350 * @space: new max space to reserve
352 * Each request reserves some space on the output queue of the socket
353 * to make sure the reply fits. This function reduces that reserved
354 * space to be the amount of space used already, plus @space.
357 void svc_reserve(struct svc_rqst
*rqstp
, int space
)
359 space
+= rqstp
->rq_res
.head
[0].iov_len
;
361 if (space
< rqstp
->rq_reserved
) {
362 struct svc_sock
*svsk
= rqstp
->rq_sock
;
363 atomic_sub((rqstp
->rq_reserved
- space
), &svsk
->sk_reserved
);
364 rqstp
->rq_reserved
= space
;
366 svc_sock_enqueue(svsk
);
371 * Release a socket after use.
374 svc_sock_put(struct svc_sock
*svsk
)
376 if (atomic_dec_and_test(&svsk
->sk_inuse
)) {
377 BUG_ON(! test_bit(SK_DEAD
, &svsk
->sk_flags
));
379 dprintk("svc: releasing dead socket\n");
380 if (svsk
->sk_sock
->file
)
381 sockfd_put(svsk
->sk_sock
);
383 sock_release(svsk
->sk_sock
);
384 if (svsk
->sk_info_authunix
!= NULL
)
385 svcauth_unix_info_release(svsk
->sk_info_authunix
);
391 svc_sock_release(struct svc_rqst
*rqstp
)
393 struct svc_sock
*svsk
= rqstp
->rq_sock
;
395 svc_release_skb(rqstp
);
397 svc_free_res_pages(rqstp
);
398 rqstp
->rq_res
.page_len
= 0;
399 rqstp
->rq_res
.page_base
= 0;
402 /* Reset response buffer and release
404 * But first, check that enough space was reserved
405 * for the reply, otherwise we have a bug!
407 if ((rqstp
->rq_res
.len
) > rqstp
->rq_reserved
)
408 printk(KERN_ERR
"RPC request reserved %d but used %d\n",
412 rqstp
->rq_res
.head
[0].iov_len
= 0;
413 svc_reserve(rqstp
, 0);
414 rqstp
->rq_sock
= NULL
;
420 * External function to wake up a server waiting for data
421 * This really only makes sense for services like lockd
422 * which have exactly one thread anyway.
425 svc_wake_up(struct svc_serv
*serv
)
427 struct svc_rqst
*rqstp
;
429 struct svc_pool
*pool
;
431 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
432 pool
= &serv
->sv_pools
[i
];
434 spin_lock_bh(&pool
->sp_lock
);
435 if (!list_empty(&pool
->sp_threads
)) {
436 rqstp
= list_entry(pool
->sp_threads
.next
,
439 dprintk("svc: daemon %p woken up.\n", rqstp
);
441 svc_thread_dequeue(pool, rqstp);
442 rqstp->rq_sock = NULL;
444 wake_up(&rqstp
->rq_wait
);
446 spin_unlock_bh(&pool
->sp_lock
);
450 union svc_pktinfo_u
{
451 struct in_pktinfo pkti
;
452 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
453 struct in6_pktinfo pkti6
;
457 static void svc_set_cmsg_data(struct svc_rqst
*rqstp
, struct cmsghdr
*cmh
)
459 switch (rqstp
->rq_sock
->sk_sk
->sk_family
) {
461 struct in_pktinfo
*pki
= CMSG_DATA(cmh
);
463 cmh
->cmsg_level
= SOL_IP
;
464 cmh
->cmsg_type
= IP_PKTINFO
;
465 pki
->ipi_ifindex
= 0;
466 pki
->ipi_spec_dst
.s_addr
= rqstp
->rq_daddr
.addr
.s_addr
;
467 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
470 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
472 struct in6_pktinfo
*pki
= CMSG_DATA(cmh
);
474 cmh
->cmsg_level
= SOL_IPV6
;
475 cmh
->cmsg_type
= IPV6_PKTINFO
;
476 pki
->ipi6_ifindex
= 0;
477 ipv6_addr_copy(&pki
->ipi6_addr
,
478 &rqstp
->rq_daddr
.addr6
);
479 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
488 * Generic sendto routine
491 svc_sendto(struct svc_rqst
*rqstp
, struct xdr_buf
*xdr
)
493 struct svc_sock
*svsk
= rqstp
->rq_sock
;
494 struct socket
*sock
= svsk
->sk_sock
;
496 char buffer
[CMSG_SPACE(sizeof(union svc_pktinfo_u
))];
497 struct cmsghdr
*cmh
= (struct cmsghdr
*)buffer
;
501 struct page
**ppage
= xdr
->pages
;
502 size_t base
= xdr
->page_base
;
503 unsigned int pglen
= xdr
->page_len
;
504 unsigned int flags
= MSG_MORE
;
505 char buf
[RPC_MAX_ADDRBUFLEN
];
509 if (rqstp
->rq_prot
== IPPROTO_UDP
) {
510 struct msghdr msg
= {
511 .msg_name
= &rqstp
->rq_addr
,
512 .msg_namelen
= rqstp
->rq_addrlen
,
514 .msg_controllen
= sizeof(buffer
),
515 .msg_flags
= MSG_MORE
,
518 svc_set_cmsg_data(rqstp
, cmh
);
520 if (sock_sendmsg(sock
, &msg
, 0) < 0)
525 if (slen
== xdr
->head
[0].iov_len
)
527 len
= kernel_sendpage(sock
, rqstp
->rq_respages
[0], 0,
528 xdr
->head
[0].iov_len
, flags
);
529 if (len
!= xdr
->head
[0].iov_len
)
531 slen
-= xdr
->head
[0].iov_len
;
536 size
= PAGE_SIZE
- base
< pglen
? PAGE_SIZE
- base
: pglen
;
540 result
= kernel_sendpage(sock
, *ppage
, base
, size
, flags
);
547 size
= PAGE_SIZE
< pglen
? PAGE_SIZE
: pglen
;
552 if (xdr
->tail
[0].iov_len
) {
553 result
= kernel_sendpage(sock
, rqstp
->rq_respages
[0],
554 ((unsigned long)xdr
->tail
[0].iov_base
)
556 xdr
->tail
[0].iov_len
, 0);
562 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
563 rqstp
->rq_sock
, xdr
->head
[0].iov_base
, xdr
->head
[0].iov_len
,
564 xdr
->len
, len
, svc_print_addr(rqstp
, buf
, sizeof(buf
)));
570 * Report socket names for nfsdfs
572 static int one_sock_name(char *buf
, struct svc_sock
*svsk
)
576 switch(svsk
->sk_sk
->sk_family
) {
578 len
= sprintf(buf
, "ipv4 %s %u.%u.%u.%u %d\n",
579 svsk
->sk_sk
->sk_protocol
==IPPROTO_UDP
?
581 NIPQUAD(inet_sk(svsk
->sk_sk
)->rcv_saddr
),
582 inet_sk(svsk
->sk_sk
)->num
);
585 len
= sprintf(buf
, "*unknown-%d*\n",
586 svsk
->sk_sk
->sk_family
);
592 svc_sock_names(char *buf
, struct svc_serv
*serv
, char *toclose
)
594 struct svc_sock
*svsk
, *closesk
= NULL
;
599 spin_lock_bh(&serv
->sv_lock
);
600 list_for_each_entry(svsk
, &serv
->sv_permsocks
, sk_list
) {
601 int onelen
= one_sock_name(buf
+len
, svsk
);
602 if (toclose
&& strcmp(toclose
, buf
+len
) == 0)
607 spin_unlock_bh(&serv
->sv_lock
);
609 /* Should unregister with portmap, but you cannot
610 * unregister just one protocol...
612 svc_close_socket(closesk
);
617 EXPORT_SYMBOL(svc_sock_names
);
620 * Check input queue length
623 svc_recv_available(struct svc_sock
*svsk
)
625 struct socket
*sock
= svsk
->sk_sock
;
628 err
= kernel_sock_ioctl(sock
, TIOCINQ
, (unsigned long) &avail
);
630 return (err
>= 0)? avail
: err
;
634 * Generic recvfrom routine.
637 svc_recvfrom(struct svc_rqst
*rqstp
, struct kvec
*iov
, int nr
, int buflen
)
639 struct svc_sock
*svsk
= rqstp
->rq_sock
;
640 struct msghdr msg
= {
641 .msg_flags
= MSG_DONTWAIT
,
645 len
= kernel_recvmsg(svsk
->sk_sock
, &msg
, iov
, nr
, buflen
,
648 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
650 memcpy(&rqstp
->rq_addr
, &svsk
->sk_remote
, svsk
->sk_remotelen
);
651 rqstp
->rq_addrlen
= svsk
->sk_remotelen
;
653 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
654 svsk
, iov
[0].iov_base
, iov
[0].iov_len
, len
);
660 * Set socket snd and rcv buffer lengths
663 svc_sock_setbufsize(struct socket
*sock
, unsigned int snd
, unsigned int rcv
)
667 oldfs
= get_fs(); set_fs(KERNEL_DS
);
668 sock_setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
,
669 (char*)&snd
, sizeof(snd
));
670 sock_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
671 (char*)&rcv
, sizeof(rcv
));
673 /* sock_setsockopt limits use to sysctl_?mem_max,
674 * which isn't acceptable. Until that is made conditional
675 * on not having CAP_SYS_RESOURCE or similar, we go direct...
676 * DaveM said I could!
679 sock
->sk
->sk_sndbuf
= snd
* 2;
680 sock
->sk
->sk_rcvbuf
= rcv
* 2;
681 sock
->sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
|SOCK_RCVBUF_LOCK
;
682 release_sock(sock
->sk
);
686 * INET callback when data has been received on the socket.
689 svc_udp_data_ready(struct sock
*sk
, int count
)
691 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
694 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
695 svsk
, sk
, count
, test_bit(SK_BUSY
, &svsk
->sk_flags
));
696 set_bit(SK_DATA
, &svsk
->sk_flags
);
697 svc_sock_enqueue(svsk
);
699 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
700 wake_up_interruptible(sk
->sk_sleep
);
704 * INET callback when space is newly available on the socket.
707 svc_write_space(struct sock
*sk
)
709 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->sk_user_data
);
712 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
713 svsk
, sk
, test_bit(SK_BUSY
, &svsk
->sk_flags
));
714 svc_sock_enqueue(svsk
);
717 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
)) {
718 dprintk("RPC svc_write_space: someone sleeping on %p\n",
720 wake_up_interruptible(sk
->sk_sleep
);
725 * Receive a datagram from a UDP socket.
728 svc_udp_recvfrom(struct svc_rqst
*rqstp
)
730 struct sockaddr_in
*sin
= svc_addr_in(rqstp
);
731 struct svc_sock
*svsk
= rqstp
->rq_sock
;
732 struct svc_serv
*serv
= svsk
->sk_server
;
736 if (test_and_clear_bit(SK_CHNGBUF
, &svsk
->sk_flags
))
737 /* udp sockets need large rcvbuf as all pending
738 * requests are still in that buffer. sndbuf must
739 * also be large enough that there is enough space
740 * for one reply per thread. We count all threads
741 * rather than threads in a particular pool, which
742 * provides an upper bound on the number of threads
743 * which will access the socket.
745 svc_sock_setbufsize(svsk
->sk_sock
,
746 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
,
747 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
);
749 if ((rqstp
->rq_deferred
= svc_deferred_dequeue(svsk
))) {
750 svc_sock_received(svsk
);
751 return svc_deferred_recv(rqstp
);
754 if (test_bit(SK_CLOSE
, &svsk
->sk_flags
)) {
755 svc_delete_socket(svsk
);
759 clear_bit(SK_DATA
, &svsk
->sk_flags
);
760 while ((skb
= skb_recv_datagram(svsk
->sk_sk
, 0, 1, &err
)) == NULL
) {
761 if (err
== -EAGAIN
) {
762 svc_sock_received(svsk
);
765 /* possibly an icmp error */
766 dprintk("svc: recvfrom returned error %d\n", -err
);
768 if (skb
->tstamp
.off_sec
== 0) {
771 tv
.tv_sec
= xtime
.tv_sec
;
772 tv
.tv_usec
= xtime
.tv_nsec
/ NSEC_PER_USEC
;
773 skb_set_timestamp(skb
, &tv
);
774 /* Don't enable netstamp, sunrpc doesn't
775 need that much accuracy */
777 skb_get_timestamp(skb
, &svsk
->sk_sk
->sk_stamp
);
778 set_bit(SK_DATA
, &svsk
->sk_flags
); /* there may be more data... */
781 * Maybe more packets - kick another thread ASAP.
783 svc_sock_received(svsk
);
785 len
= skb
->len
- sizeof(struct udphdr
);
786 rqstp
->rq_arg
.len
= len
;
788 rqstp
->rq_prot
= IPPROTO_UDP
;
790 /* Get sender address */
791 sin
->sin_family
= AF_INET
;
792 sin
->sin_port
= skb
->h
.uh
->source
;
793 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
794 rqstp
->rq_addrlen
= sizeof(struct sockaddr_in
);
796 /* Remember which interface received this request */
797 rqstp
->rq_daddr
.addr
.s_addr
= skb
->nh
.iph
->daddr
;
799 if (skb_is_nonlinear(skb
)) {
800 /* we have to copy */
802 if (csum_partial_copy_to_xdr(&rqstp
->rq_arg
, skb
)) {
805 skb_free_datagram(svsk
->sk_sk
, skb
);
809 skb_free_datagram(svsk
->sk_sk
, skb
);
811 /* we can use it in-place */
812 rqstp
->rq_arg
.head
[0].iov_base
= skb
->data
+ sizeof(struct udphdr
);
813 rqstp
->rq_arg
.head
[0].iov_len
= len
;
814 if (skb_checksum_complete(skb
)) {
815 skb_free_datagram(svsk
->sk_sk
, skb
);
818 rqstp
->rq_skbuff
= skb
;
821 rqstp
->rq_arg
.page_base
= 0;
822 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
823 rqstp
->rq_arg
.head
[0].iov_len
= len
;
824 rqstp
->rq_arg
.page_len
= 0;
825 rqstp
->rq_respages
= rqstp
->rq_pages
+1;
827 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
828 rqstp
->rq_respages
= rqstp
->rq_pages
+ 1 +
829 (rqstp
->rq_arg
.page_len
+ PAGE_SIZE
- 1)/ PAGE_SIZE
;
833 serv
->sv_stats
->netudpcnt
++;
839 svc_udp_sendto(struct svc_rqst
*rqstp
)
843 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
844 if (error
== -ECONNREFUSED
)
845 /* ICMP error on earlier request. */
846 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
852 svc_udp_init(struct svc_sock
*svsk
)
854 svsk
->sk_sk
->sk_data_ready
= svc_udp_data_ready
;
855 svsk
->sk_sk
->sk_write_space
= svc_write_space
;
856 svsk
->sk_recvfrom
= svc_udp_recvfrom
;
857 svsk
->sk_sendto
= svc_udp_sendto
;
859 /* initialise setting must have enough space to
860 * receive and respond to one request.
861 * svc_udp_recvfrom will re-adjust if necessary
863 svc_sock_setbufsize(svsk
->sk_sock
,
864 3 * svsk
->sk_server
->sv_max_mesg
,
865 3 * svsk
->sk_server
->sv_max_mesg
);
867 set_bit(SK_DATA
, &svsk
->sk_flags
); /* might have come in before data_ready set up */
868 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
872 * A data_ready event on a listening socket means there's a connection
873 * pending. Do not use state_change as a substitute for it.
876 svc_tcp_listen_data_ready(struct sock
*sk
, int count_unused
)
878 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
880 dprintk("svc: socket %p TCP (listen) state change %d\n",
884 * This callback may called twice when a new connection
885 * is established as a child socket inherits everything
886 * from a parent LISTEN socket.
887 * 1) data_ready method of the parent socket will be called
888 * when one of child sockets become ESTABLISHED.
889 * 2) data_ready method of the child socket may be called
890 * when it receives data before the socket is accepted.
891 * In case of 2, we should ignore it silently.
893 if (sk
->sk_state
== TCP_LISTEN
) {
895 set_bit(SK_CONN
, &svsk
->sk_flags
);
896 svc_sock_enqueue(svsk
);
898 printk("svc: socket %p: no user data\n", sk
);
901 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
902 wake_up_interruptible_all(sk
->sk_sleep
);
906 * A state change on a connected socket means it's dying or dead.
909 svc_tcp_state_change(struct sock
*sk
)
911 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
913 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
914 sk
, sk
->sk_state
, sk
->sk_user_data
);
917 printk("svc: socket %p: no user data\n", sk
);
919 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
920 svc_sock_enqueue(svsk
);
922 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
923 wake_up_interruptible_all(sk
->sk_sleep
);
927 svc_tcp_data_ready(struct sock
*sk
, int count
)
929 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
931 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
932 sk
, sk
->sk_user_data
);
934 set_bit(SK_DATA
, &svsk
->sk_flags
);
935 svc_sock_enqueue(svsk
);
937 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
938 wake_up_interruptible(sk
->sk_sleep
);
941 static inline int svc_port_is_privileged(struct sockaddr
*sin
)
943 switch (sin
->sa_family
) {
945 return ntohs(((struct sockaddr_in
*)sin
)->sin_port
)
947 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
949 return ntohs(((struct sockaddr_in6
*)sin
)->sin6_port
)
958 * Accept a TCP connection
961 svc_tcp_accept(struct svc_sock
*svsk
)
963 struct sockaddr_storage addr
;
964 struct sockaddr
*sin
= (struct sockaddr
*) &addr
;
965 struct svc_serv
*serv
= svsk
->sk_server
;
966 struct socket
*sock
= svsk
->sk_sock
;
967 struct socket
*newsock
;
968 struct svc_sock
*newsvsk
;
970 char buf
[RPC_MAX_ADDRBUFLEN
];
972 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
976 clear_bit(SK_CONN
, &svsk
->sk_flags
);
977 err
= kernel_accept(sock
, &newsock
, O_NONBLOCK
);
980 printk(KERN_WARNING
"%s: no more sockets!\n",
982 else if (err
!= -EAGAIN
&& net_ratelimit())
983 printk(KERN_WARNING
"%s: accept failed (err %d)!\n",
984 serv
->sv_name
, -err
);
988 set_bit(SK_CONN
, &svsk
->sk_flags
);
989 svc_sock_enqueue(svsk
);
991 err
= kernel_getpeername(newsock
, sin
, &slen
);
994 printk(KERN_WARNING
"%s: peername failed (err %d)!\n",
995 serv
->sv_name
, -err
);
996 goto failed
; /* aborted connection or whatever */
999 /* Ideally, we would want to reject connections from unauthorized
1000 * hosts here, but when we get encryption, the IP of the host won't
1001 * tell us anything. For now just warn about unpriv connections.
1003 if (!svc_port_is_privileged(sin
)) {
1004 dprintk(KERN_WARNING
1005 "%s: connect from unprivileged port: %s\n",
1007 __svc_print_addr(sin
, buf
, sizeof(buf
)));
1009 dprintk("%s: connect from %s\n", serv
->sv_name
,
1010 __svc_print_addr(sin
, buf
, sizeof(buf
)));
1012 /* make sure that a write doesn't block forever when
1015 newsock
->sk
->sk_sndtimeo
= HZ
*30;
1017 if (!(newsvsk
= svc_setup_socket(serv
, newsock
, &err
,
1018 (SVC_SOCK_ANONYMOUS
| SVC_SOCK_TEMPORARY
))))
1020 memcpy(&newsvsk
->sk_remote
, sin
, slen
);
1021 newsvsk
->sk_remotelen
= slen
;
1023 svc_sock_received(newsvsk
);
1025 /* make sure that we don't have too many active connections.
1026 * If we have, something must be dropped.
1028 * There's no point in trying to do random drop here for
1029 * DoS prevention. The NFS clients does 1 reconnect in 15
1030 * seconds. An attacker can easily beat that.
1032 * The only somewhat efficient mechanism would be if drop
1033 * old connections from the same IP first. But right now
1034 * we don't even record the client IP in svc_sock.
1036 if (serv
->sv_tmpcnt
> (serv
->sv_nrthreads
+3)*20) {
1037 struct svc_sock
*svsk
= NULL
;
1038 spin_lock_bh(&serv
->sv_lock
);
1039 if (!list_empty(&serv
->sv_tempsocks
)) {
1040 if (net_ratelimit()) {
1041 /* Try to help the admin */
1042 printk(KERN_NOTICE
"%s: too many open TCP "
1043 "sockets, consider increasing the "
1044 "number of nfsd threads\n",
1047 "%s: last TCP connect from %s\n",
1048 serv
->sv_name
, buf
);
1051 * Always select the oldest socket. It's not fair,
1054 svsk
= list_entry(serv
->sv_tempsocks
.prev
,
1057 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1058 atomic_inc(&svsk
->sk_inuse
);
1060 spin_unlock_bh(&serv
->sv_lock
);
1063 svc_sock_enqueue(svsk
);
1070 serv
->sv_stats
->nettcpconn
++;
1075 sock_release(newsock
);
1080 * Receive data from a TCP socket.
1083 svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
1085 struct svc_sock
*svsk
= rqstp
->rq_sock
;
1086 struct svc_serv
*serv
= svsk
->sk_server
;
1091 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1092 svsk
, test_bit(SK_DATA
, &svsk
->sk_flags
),
1093 test_bit(SK_CONN
, &svsk
->sk_flags
),
1094 test_bit(SK_CLOSE
, &svsk
->sk_flags
));
1096 if ((rqstp
->rq_deferred
= svc_deferred_dequeue(svsk
))) {
1097 svc_sock_received(svsk
);
1098 return svc_deferred_recv(rqstp
);
1101 if (test_bit(SK_CLOSE
, &svsk
->sk_flags
)) {
1102 svc_delete_socket(svsk
);
1106 if (svsk
->sk_sk
->sk_state
== TCP_LISTEN
) {
1107 svc_tcp_accept(svsk
);
1108 svc_sock_received(svsk
);
1112 if (test_and_clear_bit(SK_CHNGBUF
, &svsk
->sk_flags
))
1113 /* sndbuf needs to have room for one request
1114 * per thread, otherwise we can stall even when the
1115 * network isn't a bottleneck.
1117 * We count all threads rather than threads in a
1118 * particular pool, which provides an upper bound
1119 * on the number of threads which will access the socket.
1121 * rcvbuf just needs to be able to hold a few requests.
1122 * Normally they will be removed from the queue
1123 * as soon a a complete request arrives.
1125 svc_sock_setbufsize(svsk
->sk_sock
,
1126 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
,
1127 3 * serv
->sv_max_mesg
);
1129 clear_bit(SK_DATA
, &svsk
->sk_flags
);
1131 /* Receive data. If we haven't got the record length yet, get
1132 * the next four bytes. Otherwise try to gobble up as much as
1133 * possible up to the complete record length.
1135 if (svsk
->sk_tcplen
< 4) {
1136 unsigned long want
= 4 - svsk
->sk_tcplen
;
1139 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
1141 if ((len
= svc_recvfrom(rqstp
, &iov
, 1, want
)) < 0)
1143 svsk
->sk_tcplen
+= len
;
1146 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
1148 svc_sock_received(svsk
);
1149 return -EAGAIN
; /* record header not complete */
1152 svsk
->sk_reclen
= ntohl(svsk
->sk_reclen
);
1153 if (!(svsk
->sk_reclen
& 0x80000000)) {
1154 /* FIXME: technically, a record can be fragmented,
1155 * and non-terminal fragments will not have the top
1156 * bit set in the fragment length header.
1157 * But apparently no known nfs clients send fragmented
1159 if (net_ratelimit())
1160 printk(KERN_NOTICE
"RPC: bad TCP reclen 0x%08lx"
1161 " (non-terminal)\n",
1162 (unsigned long) svsk
->sk_reclen
);
1165 svsk
->sk_reclen
&= 0x7fffffff;
1166 dprintk("svc: TCP record, %d bytes\n", svsk
->sk_reclen
);
1167 if (svsk
->sk_reclen
> serv
->sv_max_mesg
) {
1168 if (net_ratelimit())
1169 printk(KERN_NOTICE
"RPC: bad TCP reclen 0x%08lx"
1171 (unsigned long) svsk
->sk_reclen
);
1176 /* Check whether enough data is available */
1177 len
= svc_recv_available(svsk
);
1181 if (len
< svsk
->sk_reclen
) {
1182 dprintk("svc: incomplete TCP record (%d of %d)\n",
1183 len
, svsk
->sk_reclen
);
1184 svc_sock_received(svsk
);
1185 return -EAGAIN
; /* record not complete */
1187 len
= svsk
->sk_reclen
;
1188 set_bit(SK_DATA
, &svsk
->sk_flags
);
1190 vec
= rqstp
->rq_vec
;
1191 vec
[0] = rqstp
->rq_arg
.head
[0];
1194 while (vlen
< len
) {
1195 vec
[pnum
].iov_base
= page_address(rqstp
->rq_pages
[pnum
]);
1196 vec
[pnum
].iov_len
= PAGE_SIZE
;
1200 rqstp
->rq_respages
= &rqstp
->rq_pages
[pnum
];
1202 /* Now receive data */
1203 len
= svc_recvfrom(rqstp
, vec
, pnum
, len
);
1207 dprintk("svc: TCP complete record (%d bytes)\n", len
);
1208 rqstp
->rq_arg
.len
= len
;
1209 rqstp
->rq_arg
.page_base
= 0;
1210 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
1211 rqstp
->rq_arg
.head
[0].iov_len
= len
;
1212 rqstp
->rq_arg
.page_len
= 0;
1214 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
1217 rqstp
->rq_skbuff
= NULL
;
1218 rqstp
->rq_prot
= IPPROTO_TCP
;
1220 /* Reset TCP read info */
1221 svsk
->sk_reclen
= 0;
1222 svsk
->sk_tcplen
= 0;
1224 svc_sock_received(svsk
);
1226 serv
->sv_stats
->nettcpcnt
++;
1231 svc_delete_socket(svsk
);
1235 if (len
== -EAGAIN
) {
1236 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1237 svc_sock_received(svsk
);
1239 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
1240 svsk
->sk_server
->sv_name
, -len
);
1248 * Send out data on TCP socket.
1251 svc_tcp_sendto(struct svc_rqst
*rqstp
)
1253 struct xdr_buf
*xbufp
= &rqstp
->rq_res
;
1257 /* Set up the first element of the reply kvec.
1258 * Any other kvecs that may be in use have been taken
1259 * care of by the server implementation itself.
1261 reclen
= htonl(0x80000000|((xbufp
->len
) - 4));
1262 memcpy(xbufp
->head
[0].iov_base
, &reclen
, 4);
1264 if (test_bit(SK_DEAD
, &rqstp
->rq_sock
->sk_flags
))
1267 sent
= svc_sendto(rqstp
, &rqstp
->rq_res
);
1268 if (sent
!= xbufp
->len
) {
1269 printk(KERN_NOTICE
"rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1270 rqstp
->rq_sock
->sk_server
->sv_name
,
1271 (sent
<0)?"got error":"sent only",
1273 set_bit(SK_CLOSE
, &rqstp
->rq_sock
->sk_flags
);
1274 svc_sock_enqueue(rqstp
->rq_sock
);
1281 svc_tcp_init(struct svc_sock
*svsk
)
1283 struct sock
*sk
= svsk
->sk_sk
;
1284 struct tcp_sock
*tp
= tcp_sk(sk
);
1286 svsk
->sk_recvfrom
= svc_tcp_recvfrom
;
1287 svsk
->sk_sendto
= svc_tcp_sendto
;
1289 if (sk
->sk_state
== TCP_LISTEN
) {
1290 dprintk("setting up TCP socket for listening\n");
1291 sk
->sk_data_ready
= svc_tcp_listen_data_ready
;
1292 set_bit(SK_CONN
, &svsk
->sk_flags
);
1294 dprintk("setting up TCP socket for reading\n");
1295 sk
->sk_state_change
= svc_tcp_state_change
;
1296 sk
->sk_data_ready
= svc_tcp_data_ready
;
1297 sk
->sk_write_space
= svc_write_space
;
1299 svsk
->sk_reclen
= 0;
1300 svsk
->sk_tcplen
= 0;
1302 tp
->nonagle
= 1; /* disable Nagle's algorithm */
1304 /* initialise setting must have enough space to
1305 * receive and respond to one request.
1306 * svc_tcp_recvfrom will re-adjust if necessary
1308 svc_sock_setbufsize(svsk
->sk_sock
,
1309 3 * svsk
->sk_server
->sv_max_mesg
,
1310 3 * svsk
->sk_server
->sv_max_mesg
);
1312 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1313 set_bit(SK_DATA
, &svsk
->sk_flags
);
1314 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1315 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1320 svc_sock_update_bufs(struct svc_serv
*serv
)
1323 * The number of server threads has changed. Update
1324 * rcvbuf and sndbuf accordingly on all sockets
1326 struct list_head
*le
;
1328 spin_lock_bh(&serv
->sv_lock
);
1329 list_for_each(le
, &serv
->sv_permsocks
) {
1330 struct svc_sock
*svsk
=
1331 list_entry(le
, struct svc_sock
, sk_list
);
1332 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1334 list_for_each(le
, &serv
->sv_tempsocks
) {
1335 struct svc_sock
*svsk
=
1336 list_entry(le
, struct svc_sock
, sk_list
);
1337 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1339 spin_unlock_bh(&serv
->sv_lock
);
1343 * Receive the next request on any socket. This code is carefully
1344 * organised not to touch any cachelines in the shared svc_serv
1345 * structure, only cachelines in the local svc_pool.
1348 svc_recv(struct svc_rqst
*rqstp
, long timeout
)
1350 struct svc_sock
*svsk
= NULL
;
1351 struct svc_serv
*serv
= rqstp
->rq_server
;
1352 struct svc_pool
*pool
= rqstp
->rq_pool
;
1355 struct xdr_buf
*arg
;
1356 DECLARE_WAITQUEUE(wait
, current
);
1358 dprintk("svc: server %p waiting for data (to = %ld)\n",
1363 "svc_recv: service %p, socket not NULL!\n",
1365 if (waitqueue_active(&rqstp
->rq_wait
))
1367 "svc_recv: service %p, wait queue active!\n",
1371 /* now allocate needed pages. If we get a failure, sleep briefly */
1372 pages
= (serv
->sv_max_mesg
+ PAGE_SIZE
) / PAGE_SIZE
;
1373 for (i
=0; i
< pages
; i
++)
1374 while (rqstp
->rq_pages
[i
] == NULL
) {
1375 struct page
*p
= alloc_page(GFP_KERNEL
);
1377 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1378 rqstp
->rq_pages
[i
] = p
;
1380 rqstp
->rq_pages
[i
++] = NULL
; /* this might be seen in nfs_read_actor */
1381 BUG_ON(pages
>= RPCSVC_MAXPAGES
);
1383 /* Make arg->head point to first page and arg->pages point to rest */
1384 arg
= &rqstp
->rq_arg
;
1385 arg
->head
[0].iov_base
= page_address(rqstp
->rq_pages
[0]);
1386 arg
->head
[0].iov_len
= PAGE_SIZE
;
1387 arg
->pages
= rqstp
->rq_pages
+ 1;
1389 /* save at least one page for response */
1390 arg
->page_len
= (pages
-2)*PAGE_SIZE
;
1391 arg
->len
= (pages
-1)*PAGE_SIZE
;
1392 arg
->tail
[0].iov_len
= 0;
1399 spin_lock_bh(&pool
->sp_lock
);
1400 if ((svsk
= svc_sock_dequeue(pool
)) != NULL
) {
1401 rqstp
->rq_sock
= svsk
;
1402 atomic_inc(&svsk
->sk_inuse
);
1403 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
1404 atomic_add(rqstp
->rq_reserved
, &svsk
->sk_reserved
);
1406 /* No data pending. Go to sleep */
1407 svc_thread_enqueue(pool
, rqstp
);
1410 * We have to be able to interrupt this wait
1411 * to bring down the daemons ...
1413 set_current_state(TASK_INTERRUPTIBLE
);
1414 add_wait_queue(&rqstp
->rq_wait
, &wait
);
1415 spin_unlock_bh(&pool
->sp_lock
);
1417 schedule_timeout(timeout
);
1421 spin_lock_bh(&pool
->sp_lock
);
1422 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
1424 if (!(svsk
= rqstp
->rq_sock
)) {
1425 svc_thread_dequeue(pool
, rqstp
);
1426 spin_unlock_bh(&pool
->sp_lock
);
1427 dprintk("svc: server %p, no data yet\n", rqstp
);
1428 return signalled()? -EINTR
: -EAGAIN
;
1431 spin_unlock_bh(&pool
->sp_lock
);
1433 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1434 rqstp
, pool
->sp_id
, svsk
, atomic_read(&svsk
->sk_inuse
));
1435 len
= svsk
->sk_recvfrom(rqstp
);
1436 dprintk("svc: got len=%d\n", len
);
1438 /* No data, incomplete (TCP) read, or accept() */
1439 if (len
== 0 || len
== -EAGAIN
) {
1440 rqstp
->rq_res
.len
= 0;
1441 svc_sock_release(rqstp
);
1444 svsk
->sk_lastrecv
= get_seconds();
1445 clear_bit(SK_OLD
, &svsk
->sk_flags
);
1447 rqstp
->rq_secure
= svc_port_is_privileged(svc_addr(rqstp
));
1448 rqstp
->rq_chandle
.defer
= svc_defer
;
1451 serv
->sv_stats
->netcnt
++;
1459 svc_drop(struct svc_rqst
*rqstp
)
1461 dprintk("svc: socket %p dropped request\n", rqstp
->rq_sock
);
1462 svc_sock_release(rqstp
);
1466 * Return reply to client.
1469 svc_send(struct svc_rqst
*rqstp
)
1471 struct svc_sock
*svsk
;
1475 if ((svsk
= rqstp
->rq_sock
) == NULL
) {
1476 printk(KERN_WARNING
"NULL socket pointer in %s:%d\n",
1477 __FILE__
, __LINE__
);
1481 /* release the receive skb before sending the reply */
1482 svc_release_skb(rqstp
);
1484 /* calculate over-all length */
1485 xb
= & rqstp
->rq_res
;
1486 xb
->len
= xb
->head
[0].iov_len
+
1488 xb
->tail
[0].iov_len
;
1490 /* Grab svsk->sk_mutex to serialize outgoing data. */
1491 mutex_lock(&svsk
->sk_mutex
);
1492 if (test_bit(SK_DEAD
, &svsk
->sk_flags
))
1495 len
= svsk
->sk_sendto(rqstp
);
1496 mutex_unlock(&svsk
->sk_mutex
);
1497 svc_sock_release(rqstp
);
1499 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
1505 * Timer function to close old temporary sockets, using
1506 * a mark-and-sweep algorithm.
1509 svc_age_temp_sockets(unsigned long closure
)
1511 struct svc_serv
*serv
= (struct svc_serv
*)closure
;
1512 struct svc_sock
*svsk
;
1513 struct list_head
*le
, *next
;
1514 LIST_HEAD(to_be_aged
);
1516 dprintk("svc_age_temp_sockets\n");
1518 if (!spin_trylock_bh(&serv
->sv_lock
)) {
1519 /* busy, try again 1 sec later */
1520 dprintk("svc_age_temp_sockets: busy\n");
1521 mod_timer(&serv
->sv_temptimer
, jiffies
+ HZ
);
1525 list_for_each_safe(le
, next
, &serv
->sv_tempsocks
) {
1526 svsk
= list_entry(le
, struct svc_sock
, sk_list
);
1528 if (!test_and_set_bit(SK_OLD
, &svsk
->sk_flags
))
1530 if (atomic_read(&svsk
->sk_inuse
) || test_bit(SK_BUSY
, &svsk
->sk_flags
))
1532 atomic_inc(&svsk
->sk_inuse
);
1533 list_move(le
, &to_be_aged
);
1534 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1535 set_bit(SK_DETACHED
, &svsk
->sk_flags
);
1537 spin_unlock_bh(&serv
->sv_lock
);
1539 while (!list_empty(&to_be_aged
)) {
1540 le
= to_be_aged
.next
;
1541 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1543 svsk
= list_entry(le
, struct svc_sock
, sk_list
);
1545 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1546 svsk
, get_seconds() - svsk
->sk_lastrecv
);
1548 /* a thread will dequeue and close it soon */
1549 svc_sock_enqueue(svsk
);
1553 mod_timer(&serv
->sv_temptimer
, jiffies
+ svc_conn_age_period
* HZ
);
1557 * Initialize socket for RPC use and create svc_sock struct
1558 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1560 static struct svc_sock
*svc_setup_socket(struct svc_serv
*serv
,
1561 struct socket
*sock
,
1562 int *errp
, int flags
)
1564 struct svc_sock
*svsk
;
1566 int pmap_register
= !(flags
& SVC_SOCK_ANONYMOUS
);
1567 int is_temporary
= flags
& SVC_SOCK_TEMPORARY
;
1569 dprintk("svc: svc_setup_socket %p\n", sock
);
1570 if (!(svsk
= kzalloc(sizeof(*svsk
), GFP_KERNEL
))) {
1577 /* Register socket with portmapper */
1578 if (*errp
>= 0 && pmap_register
)
1579 *errp
= svc_register(serv
, inet
->sk_protocol
,
1580 ntohs(inet_sk(inet
)->sport
));
1587 set_bit(SK_BUSY
, &svsk
->sk_flags
);
1588 inet
->sk_user_data
= svsk
;
1589 svsk
->sk_sock
= sock
;
1591 svsk
->sk_ostate
= inet
->sk_state_change
;
1592 svsk
->sk_odata
= inet
->sk_data_ready
;
1593 svsk
->sk_owspace
= inet
->sk_write_space
;
1594 svsk
->sk_server
= serv
;
1595 atomic_set(&svsk
->sk_inuse
, 1);
1596 svsk
->sk_lastrecv
= get_seconds();
1597 spin_lock_init(&svsk
->sk_defer_lock
);
1598 INIT_LIST_HEAD(&svsk
->sk_deferred
);
1599 INIT_LIST_HEAD(&svsk
->sk_ready
);
1600 mutex_init(&svsk
->sk_mutex
);
1602 /* Initialize the socket */
1603 if (sock
->type
== SOCK_DGRAM
)
1608 spin_lock_bh(&serv
->sv_lock
);
1610 set_bit(SK_TEMP
, &svsk
->sk_flags
);
1611 list_add(&svsk
->sk_list
, &serv
->sv_tempsocks
);
1613 if (serv
->sv_temptimer
.function
== NULL
) {
1614 /* setup timer to age temp sockets */
1615 setup_timer(&serv
->sv_temptimer
, svc_age_temp_sockets
,
1616 (unsigned long)serv
);
1617 mod_timer(&serv
->sv_temptimer
,
1618 jiffies
+ svc_conn_age_period
* HZ
);
1621 clear_bit(SK_TEMP
, &svsk
->sk_flags
);
1622 list_add(&svsk
->sk_list
, &serv
->sv_permsocks
);
1624 spin_unlock_bh(&serv
->sv_lock
);
1626 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1632 int svc_addsock(struct svc_serv
*serv
,
1638 struct socket
*so
= sockfd_lookup(fd
, &err
);
1639 struct svc_sock
*svsk
= NULL
;
1643 if (so
->sk
->sk_family
!= AF_INET
)
1644 err
= -EAFNOSUPPORT
;
1645 else if (so
->sk
->sk_protocol
!= IPPROTO_TCP
&&
1646 so
->sk
->sk_protocol
!= IPPROTO_UDP
)
1647 err
= -EPROTONOSUPPORT
;
1648 else if (so
->state
> SS_UNCONNECTED
)
1651 svsk
= svc_setup_socket(serv
, so
, &err
, SVC_SOCK_DEFAULTS
);
1653 svc_sock_received(svsk
);
1661 if (proto
) *proto
= so
->sk
->sk_protocol
;
1662 return one_sock_name(name_return
, svsk
);
1664 EXPORT_SYMBOL_GPL(svc_addsock
);
1667 * Create socket for RPC service.
1669 static int svc_create_socket(struct svc_serv
*serv
, int protocol
,
1670 struct sockaddr_in
*sin
, int flags
)
1672 struct svc_sock
*svsk
;
1673 struct socket
*sock
;
1676 char buf
[RPC_MAX_ADDRBUFLEN
];
1678 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1679 serv
->sv_program
->pg_name
, protocol
,
1680 __svc_print_addr((struct sockaddr
*) sin
, buf
,
1683 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
1684 printk(KERN_WARNING
"svc: only UDP and TCP "
1685 "sockets supported\n");
1688 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
1690 if ((error
= sock_create_kern(PF_INET
, type
, protocol
, &sock
)) < 0)
1693 svc_reclassify_socket(sock
);
1695 if (type
== SOCK_STREAM
)
1696 sock
->sk
->sk_reuse
= 1; /* allow address reuse */
1697 error
= kernel_bind(sock
, (struct sockaddr
*) sin
,
1702 if (protocol
== IPPROTO_TCP
) {
1703 if ((error
= kernel_listen(sock
, 64)) < 0)
1707 if ((svsk
= svc_setup_socket(serv
, sock
, &error
, flags
)) != NULL
) {
1708 svc_sock_received(svsk
);
1709 return ntohs(inet_sk(svsk
->sk_sk
)->sport
);
1713 dprintk("svc: svc_create_socket error = %d\n", -error
);
1719 * Remove a dead socket
1722 svc_delete_socket(struct svc_sock
*svsk
)
1724 struct svc_serv
*serv
;
1727 dprintk("svc: svc_delete_socket(%p)\n", svsk
);
1729 serv
= svsk
->sk_server
;
1732 sk
->sk_state_change
= svsk
->sk_ostate
;
1733 sk
->sk_data_ready
= svsk
->sk_odata
;
1734 sk
->sk_write_space
= svsk
->sk_owspace
;
1736 spin_lock_bh(&serv
->sv_lock
);
1738 if (!test_and_set_bit(SK_DETACHED
, &svsk
->sk_flags
))
1739 list_del_init(&svsk
->sk_list
);
1741 * We used to delete the svc_sock from whichever list
1742 * it's sk_ready node was on, but we don't actually
1743 * need to. This is because the only time we're called
1744 * while still attached to a queue, the queue itself
1745 * is about to be destroyed (in svc_destroy).
1747 if (!test_and_set_bit(SK_DEAD
, &svsk
->sk_flags
)) {
1748 BUG_ON(atomic_read(&svsk
->sk_inuse
)<2);
1749 atomic_dec(&svsk
->sk_inuse
);
1750 if (test_bit(SK_TEMP
, &svsk
->sk_flags
))
1754 spin_unlock_bh(&serv
->sv_lock
);
1757 void svc_close_socket(struct svc_sock
*svsk
)
1759 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1760 if (test_and_set_bit(SK_BUSY
, &svsk
->sk_flags
))
1761 /* someone else will have to effect the close */
1764 atomic_inc(&svsk
->sk_inuse
);
1765 svc_delete_socket(svsk
);
1766 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
1771 * svc_makesock - Make a socket for nfsd and lockd
1772 * @serv: RPC server structure
1773 * @protocol: transport protocol to use
1774 * @port: port to use
1775 * @flags: requested socket characteristics
1778 int svc_makesock(struct svc_serv
*serv
, int protocol
, unsigned short port
,
1781 struct sockaddr_in sin
= {
1782 .sin_family
= AF_INET
,
1783 .sin_addr
.s_addr
= INADDR_ANY
,
1784 .sin_port
= htons(port
),
1787 dprintk("svc: creating socket proto = %d\n", protocol
);
1788 return svc_create_socket(serv
, protocol
, &sin
, flags
);
1792 * Handle defer and revisit of requests
1795 static void svc_revisit(struct cache_deferred_req
*dreq
, int too_many
)
1797 struct svc_deferred_req
*dr
= container_of(dreq
, struct svc_deferred_req
, handle
);
1798 struct svc_sock
*svsk
;
1801 svc_sock_put(dr
->svsk
);
1805 dprintk("revisit queued\n");
1808 spin_lock_bh(&svsk
->sk_defer_lock
);
1809 list_add(&dr
->handle
.recent
, &svsk
->sk_deferred
);
1810 spin_unlock_bh(&svsk
->sk_defer_lock
);
1811 set_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1812 svc_sock_enqueue(svsk
);
1816 static struct cache_deferred_req
*
1817 svc_defer(struct cache_req
*req
)
1819 struct svc_rqst
*rqstp
= container_of(req
, struct svc_rqst
, rq_chandle
);
1820 int size
= sizeof(struct svc_deferred_req
) + (rqstp
->rq_arg
.len
);
1821 struct svc_deferred_req
*dr
;
1823 if (rqstp
->rq_arg
.page_len
)
1824 return NULL
; /* if more than a page, give up FIXME */
1825 if (rqstp
->rq_deferred
) {
1826 dr
= rqstp
->rq_deferred
;
1827 rqstp
->rq_deferred
= NULL
;
1829 int skip
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
1830 /* FIXME maybe discard if size too large */
1831 dr
= kmalloc(size
, GFP_KERNEL
);
1835 dr
->handle
.owner
= rqstp
->rq_server
;
1836 dr
->prot
= rqstp
->rq_prot
;
1837 memcpy(&dr
->addr
, &rqstp
->rq_addr
, rqstp
->rq_addrlen
);
1838 dr
->addrlen
= rqstp
->rq_addrlen
;
1839 dr
->daddr
= rqstp
->rq_daddr
;
1840 dr
->argslen
= rqstp
->rq_arg
.len
>> 2;
1841 memcpy(dr
->args
, rqstp
->rq_arg
.head
[0].iov_base
-skip
, dr
->argslen
<<2);
1843 atomic_inc(&rqstp
->rq_sock
->sk_inuse
);
1844 dr
->svsk
= rqstp
->rq_sock
;
1846 dr
->handle
.revisit
= svc_revisit
;
1851 * recv data from a deferred request into an active one
1853 static int svc_deferred_recv(struct svc_rqst
*rqstp
)
1855 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
1857 rqstp
->rq_arg
.head
[0].iov_base
= dr
->args
;
1858 rqstp
->rq_arg
.head
[0].iov_len
= dr
->argslen
<<2;
1859 rqstp
->rq_arg
.page_len
= 0;
1860 rqstp
->rq_arg
.len
= dr
->argslen
<<2;
1861 rqstp
->rq_prot
= dr
->prot
;
1862 memcpy(&rqstp
->rq_addr
, &dr
->addr
, dr
->addrlen
);
1863 rqstp
->rq_addrlen
= dr
->addrlen
;
1864 rqstp
->rq_daddr
= dr
->daddr
;
1865 rqstp
->rq_respages
= rqstp
->rq_pages
;
1866 return dr
->argslen
<<2;
1870 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_sock
*svsk
)
1872 struct svc_deferred_req
*dr
= NULL
;
1874 if (!test_bit(SK_DEFERRED
, &svsk
->sk_flags
))
1876 spin_lock_bh(&svsk
->sk_defer_lock
);
1877 clear_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1878 if (!list_empty(&svsk
->sk_deferred
)) {
1879 dr
= list_entry(svsk
->sk_deferred
.next
,
1880 struct svc_deferred_req
,
1882 list_del_init(&dr
->handle
.recent
);
1883 set_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1885 spin_unlock_bh(&svsk
->sk_defer_lock
);