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/version.h>
30 #include <linux/unistd.h>
31 #include <linux/slab.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
35 #include <net/checksum.h>
37 #include <asm/uaccess.h>
38 #include <asm/ioctls.h>
40 #include <linux/sunrpc/types.h>
41 #include <linux/sunrpc/xdr.h>
42 #include <linux/sunrpc/svcsock.h>
43 #include <linux/sunrpc/stats.h>
45 /* SMP locking strategy:
47 * svc_serv->sv_lock protects most stuff for that service.
49 * Some flags can be set to certain values at any time
50 * providing that certain rules are followed:
52 * SK_BUSY can be set to 0 at any time.
53 * svc_sock_enqueue must be called afterwards
54 * SK_CONN, SK_DATA, can be set or cleared at any time.
55 * after a set, svc_sock_enqueue must be called.
56 * after a clear, the socket must be read/accepted
57 * if this succeeds, it must be set again.
58 * SK_CLOSE can set at any time. It is never cleared.
62 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
65 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
66 int *errp, int pmap_reg);
67 static void svc_udp_data_ready(struct sock *, int);
68 static int svc_udp_recvfrom(struct svc_rqst *);
69 static int svc_udp_sendto(struct svc_rqst *);
73 * Queue up an idle server thread. Must have serv->sv_lock held.
74 * Note: this is really a stack rather than a queue, so that we only
75 * use as many different threads as we need, and the rest don't polute
79 svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
81 list_add(&rqstp->rq_list, &serv->sv_threads);
85 * Dequeue an nfsd thread. Must have serv->sv_lock held.
88 svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
90 list_del(&rqstp->rq_list);
94 * Release an skbuff after use
97 svc_release_skb(struct svc_rqst *rqstp)
99 struct sk_buff *skb = rqstp->rq_skbuff;
103 rqstp->rq_skbuff = NULL;
105 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
106 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
110 * Queue up a socket with data pending. If there are idle nfsd
111 * processes, wake 'em up.
115 svc_sock_enqueue(struct svc_sock *svsk)
117 struct svc_serv *serv = svsk->sk_server;
118 struct svc_rqst *rqstp;
120 if (!(svsk->sk_flags &
121 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)) ))
124 spin_lock_bh(&serv->sv_lock);
126 if (!list_empty(&serv->sv_threads) &&
127 !list_empty(&serv->sv_sockets))
129 "svc_sock_enqueue: threads and sockets both waiting??\n");
131 if (test_bit(SK_BUSY, &svsk->sk_flags)) {
132 /* Don't enqueue socket while daemon is receiving */
133 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
137 if (((svsk->sk_reserved + serv->sv_bufsz)*2
138 > sock_wspace(svsk->sk_sk))
139 && !test_bit(SK_CLOSE, &svsk->sk_flags)
140 && !test_bit(SK_CONN, &svsk->sk_flags)) {
141 /* Don't enqueue while not enough space for reply */
142 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
143 svsk->sk_sk, svsk->sk_reserved+serv->sv_bufsz,
144 sock_wspace(svsk->sk_sk));
148 /* Mark socket as busy. It will remain in this state until the
149 * server has processed all pending data and put the socket back
152 set_bit(SK_BUSY, &svsk->sk_flags);
154 if (!list_empty(&serv->sv_threads)) {
155 rqstp = list_entry(serv->sv_threads.next,
158 dprintk("svc: socket %p served by daemon %p\n",
160 svc_serv_dequeue(serv, rqstp);
163 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
164 rqstp, rqstp->rq_sock);
165 rqstp->rq_sock = svsk;
167 rqstp->rq_reserved = serv->sv_bufsz;
168 svsk->sk_reserved += rqstp->rq_reserved;
169 wake_up(&rqstp->rq_wait);
171 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
172 list_add_tail(&svsk->sk_ready, &serv->sv_sockets);
173 set_bit(SK_QUED, &svsk->sk_flags);
177 spin_unlock_bh(&serv->sv_lock);
181 * Dequeue the first socket. Must be called with the serv->sv_lock held.
183 static inline struct svc_sock *
184 svc_sock_dequeue(struct svc_serv *serv)
186 struct svc_sock *svsk;
188 if (list_empty(&serv->sv_sockets))
191 svsk = list_entry(serv->sv_sockets.next,
192 struct svc_sock, sk_ready);
193 list_del(&svsk->sk_ready);
195 dprintk("svc: socket %p dequeued, inuse=%d\n",
196 svsk->sk_sk, svsk->sk_inuse);
197 clear_bit(SK_QUED, &svsk->sk_flags);
203 * Having read something from a socket, check whether it
204 * needs to be re-enqueued.
205 * Note: SK_DATA only gets cleared when a read-attempt finds
206 * no (or insufficient) data.
209 svc_sock_received(struct svc_sock *svsk)
211 clear_bit(SK_BUSY, &svsk->sk_flags);
212 svc_sock_enqueue(svsk);
217 * svc_reserve - change the space reserved for the reply to a request.
218 * @rqstp: The request in question
219 * @space: new max space to reserve
221 * Each request reserves some space on the output queue of the socket
222 * to make sure the reply fits. This function reduces that reserved
223 * space to be the amount of space used already, plus @space.
226 void svc_reserve(struct svc_rqst *rqstp, int space)
228 space += rqstp->rq_resbuf.len<<2;
230 if (space < rqstp->rq_reserved) {
231 struct svc_sock *svsk = rqstp->rq_sock;
232 spin_lock_bh(&svsk->sk_server->sv_lock);
233 svsk->sk_reserved -= (rqstp->rq_reserved - space);
234 rqstp->rq_reserved = space;
235 spin_unlock_bh(&svsk->sk_server->sv_lock);
237 svc_sock_enqueue(svsk);
242 * Release a socket after use.
245 svc_sock_put(struct svc_sock *svsk)
247 struct svc_serv *serv = svsk->sk_server;
249 spin_lock_bh(&serv->sv_lock);
250 if (!--(svsk->sk_inuse) && test_bit(SK_DEAD, &svsk->sk_flags)) {
251 spin_unlock_bh(&serv->sv_lock);
252 dprintk("svc: releasing dead socket\n");
253 sock_release(svsk->sk_sock);
257 spin_unlock_bh(&serv->sv_lock);
261 svc_sock_release(struct svc_rqst *rqstp)
263 struct svc_sock *svsk = rqstp->rq_sock;
265 svc_release_skb(rqstp);
267 /* Reset response buffer and release
269 * But first, check that enough space was reserved
270 * for the reply, otherwise we have a bug!
272 if ((rqstp->rq_resbuf.len<<2) > rqstp->rq_reserved)
273 printk(KERN_ERR "RPC request reserved %d but used %d\n",
275 rqstp->rq_resbuf.len<<2);
277 rqstp->rq_resbuf.buf = rqstp->rq_resbuf.base;
278 rqstp->rq_resbuf.len = 0;
279 svc_reserve(rqstp, 0);
280 rqstp->rq_sock = NULL;
286 * External function to wake up a server waiting for data
289 svc_wake_up(struct svc_serv *serv)
291 struct svc_rqst *rqstp;
293 spin_lock_bh(&serv->sv_lock);
294 if (!list_empty(&serv->sv_threads)) {
295 rqstp = list_entry(serv->sv_threads.next,
298 dprintk("svc: daemon %p woken up.\n", rqstp);
300 svc_serv_dequeue(serv, rqstp);
301 rqstp->rq_sock = NULL;
303 wake_up(&rqstp->rq_wait);
305 spin_unlock_bh(&serv->sv_lock);
309 * Generic sendto routine
312 svc_sendto(struct svc_rqst *rqstp, struct iovec *iov, int nr)
315 struct svc_sock *svsk = rqstp->rq_sock;
316 struct socket *sock = svsk->sk_sock;
320 for (i = buflen = 0; i < nr; i++)
321 buflen += iov[i].iov_len;
323 msg.msg_name = &rqstp->rq_addr;
324 msg.msg_namelen = sizeof(rqstp->rq_addr);
327 msg.msg_control = NULL;
328 msg.msg_controllen = 0;
330 /* This was MSG_DONTWAIT, but I now want it to wait.
331 * The only thing that it would wait for is memory and
332 * if we are fairly low on memory, then we aren't likely
333 * to make much progress anyway.
334 * sk->sndtimeo is set to 30seconds just in case.
338 oldfs = get_fs(); set_fs(KERNEL_DS);
339 len = sock_sendmsg(sock, &msg, buflen);
342 dprintk("svc: socket %p sendto([%p %Zu... ], %d, %d) = %d\n",
343 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, nr, buflen, len);
349 * Check input queue length
352 svc_recv_available(struct svc_sock *svsk)
355 struct socket *sock = svsk->sk_sock;
358 oldfs = get_fs(); set_fs(KERNEL_DS);
359 err = sock->ops->ioctl(sock, TIOCINQ, (unsigned long) &avail);
362 return (err >= 0)? avail : err;
366 * Generic recvfrom routine.
369 svc_recvfrom(struct svc_rqst *rqstp, struct iovec *iov, int nr, int buflen)
376 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
377 sock = rqstp->rq_sock->sk_sock;
379 msg.msg_name = &rqstp->rq_addr;
380 msg.msg_namelen = sizeof(rqstp->rq_addr);
383 msg.msg_control = NULL;
384 msg.msg_controllen = 0;
386 msg.msg_flags = MSG_DONTWAIT;
388 oldfs = get_fs(); set_fs(KERNEL_DS);
389 len = sock_recvmsg(sock, &msg, buflen, MSG_DONTWAIT);
392 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
393 * possibly we should cache this in the svc_sock structure
394 * at accept time. FIXME
396 alen = sizeof(rqstp->rq_addr);
397 sock->ops->getname(sock, (struct sockaddr *)&rqstp->rq_addr, &alen, 1);
399 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
400 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
406 * Set socket snd and rcv buffer lengths
409 svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
413 oldfs = get_fs(); set_fs(KERNEL_DS);
414 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
415 (char*)&snd, sizeof(snd));
416 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
417 (char*)&rcv, sizeof(rcv));
419 /* sock_setsockopt limits use to sysctl_?mem_max,
420 * which isn't acceptable. Until that is made conditional
421 * on not having CAP_SYS_RESOURCE or similar, we go direct...
422 * DaveM said I could!
425 sock->sk->sndbuf = snd * 2;
426 sock->sk->rcvbuf = rcv * 2;
427 sock->sk->userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
428 release_sock(sock->sk);
432 * INET callback when data has been received on the socket.
435 svc_udp_data_ready(struct sock *sk, int count)
437 struct svc_sock *svsk = (struct svc_sock *)(sk->user_data);
441 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
442 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
443 set_bit(SK_DATA, &svsk->sk_flags);
444 svc_sock_enqueue(svsk);
446 if (sk->sleep && waitqueue_active(sk->sleep))
447 wake_up_interruptible(sk->sleep);
451 * INET callback when space is newly available on the socket.
454 svc_write_space(struct sock *sk)
456 struct svc_sock *svsk = (struct svc_sock *)(sk->user_data);
459 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
460 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
461 svc_sock_enqueue(svsk);
464 if (sk->sleep && waitqueue_active(sk->sleep))
465 wake_up_interruptible(sk->sleep);
469 * Receive a datagram from a UDP socket.
472 svc_udp_recvfrom(struct svc_rqst *rqstp)
474 struct svc_sock *svsk = rqstp->rq_sock;
475 struct svc_serv *serv = svsk->sk_server;
480 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
481 /* udp sockets need large rcvbuf as all pending
482 * requests are still in that buffer. sndbuf must
483 * also be large enough that there is enough space
484 * for one reply per thread.
486 svc_sock_setbufsize(svsk->sk_sock,
487 (serv->sv_nrthreads+3)* serv->sv_bufsz,
488 (serv->sv_nrthreads+3)* serv->sv_bufsz);
490 clear_bit(SK_DATA, &svsk->sk_flags);
491 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
492 svc_sock_received(svsk);
495 /* possibly an icmp error */
496 dprintk("svc: recvfrom returned error %d\n", -err);
498 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
501 if (skb_is_nonlinear(skb)) {
502 if (skb_linearize(skb, GFP_KERNEL) != 0) {
504 svc_sock_received(svsk);
509 if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
510 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
511 skb_free_datagram(svsk->sk_sk, skb);
512 svc_sock_received(svsk);
518 len = skb->len - sizeof(struct udphdr);
519 data = (u32 *) (skb->data + sizeof(struct udphdr));
521 rqstp->rq_skbuff = skb;
522 rqstp->rq_argbuf.base = data;
523 rqstp->rq_argbuf.buf = data;
524 rqstp->rq_argbuf.len = (len >> 2);
525 /* rqstp->rq_resbuf = rqstp->rq_defbuf; */
526 rqstp->rq_prot = IPPROTO_UDP;
528 /* Get sender address */
529 rqstp->rq_addr.sin_family = AF_INET;
530 rqstp->rq_addr.sin_port = skb->h.uh->source;
531 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
534 serv->sv_stats->netudpcnt++;
536 /* One down, maybe more to go... */
537 svsk->sk_sk->stamp = skb->stamp;
538 svc_sock_received(svsk);
544 svc_udp_sendto(struct svc_rqst *rqstp)
546 struct svc_buf *bufp = &rqstp->rq_resbuf;
549 /* Set up the first element of the reply iovec.
550 * Any other iovecs that may be in use have been taken
551 * care of by the server implementation itself.
553 /* bufp->base = bufp->area; */
554 bufp->iov[0].iov_base = bufp->base;
555 bufp->iov[0].iov_len = bufp->len << 2;
557 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
558 if (error == -ECONNREFUSED)
559 /* ICMP error on earlier request. */
560 error = svc_sendto(rqstp, bufp->iov, bufp->nriov);
566 svc_udp_init(struct svc_sock *svsk)
568 svsk->sk_sk->data_ready = svc_udp_data_ready;
569 svsk->sk_sk->write_space = svc_write_space;
570 svsk->sk_recvfrom = svc_udp_recvfrom;
571 svsk->sk_sendto = svc_udp_sendto;
573 /* initialise setting must have enough space to
574 * receive and respond to one request.
575 * svc_udp_recvfrom will re-adjust if necessary
577 svc_sock_setbufsize(svsk->sk_sock,
578 3 * svsk->sk_server->sv_bufsz,
579 3 * svsk->sk_server->sv_bufsz);
581 set_bit(SK_CHNGBUF, &svsk->sk_flags);
587 * A data_ready event on a listening socket means there's a connection
588 * pending. Do not use state_change as a substitute for it.
591 svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
593 struct svc_sock *svsk;
595 dprintk("svc: socket %p TCP (listen) state change %d\n",
598 if (sk->state != TCP_ESTABLISHED) {
599 /* Aborted connection, SYN_RECV or whatever... */
602 if (!(svsk = (struct svc_sock *) sk->user_data)) {
603 printk("svc: socket %p: no user data\n", sk);
606 set_bit(SK_CONN, &svsk->sk_flags);
607 svc_sock_enqueue(svsk);
609 if (sk->sleep && waitqueue_active(sk->sleep))
610 wake_up_interruptible_all(sk->sleep);
614 * A state change on a connected socket means it's dying or dead.
617 svc_tcp_state_change(struct sock *sk)
619 struct svc_sock *svsk;
621 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
622 sk, sk->state, sk->user_data);
624 if (!(svsk = (struct svc_sock *) sk->user_data)) {
625 printk("svc: socket %p: no user data\n", sk);
628 set_bit(SK_CLOSE, &svsk->sk_flags);
629 svc_sock_enqueue(svsk);
631 if (sk->sleep && waitqueue_active(sk->sleep))
632 wake_up_interruptible_all(sk->sleep);
636 svc_tcp_data_ready(struct sock *sk, int count)
638 struct svc_sock * svsk;
640 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
642 if (!(svsk = (struct svc_sock *)(sk->user_data)))
644 set_bit(SK_DATA, &svsk->sk_flags);
645 svc_sock_enqueue(svsk);
647 if (sk->sleep && waitqueue_active(sk->sleep))
648 wake_up_interruptible(sk->sleep);
652 * Accept a TCP connection
655 svc_tcp_accept(struct svc_sock *svsk)
657 struct sockaddr_in sin;
658 struct svc_serv *serv = svsk->sk_server;
659 struct socket *sock = svsk->sk_sock;
660 struct socket *newsock;
661 struct proto_ops *ops;
662 struct svc_sock *newsvsk;
665 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
669 if (!(newsock = sock_alloc())) {
670 printk(KERN_WARNING "%s: no more sockets!\n", serv->sv_name);
673 dprintk("svc: tcp_accept %p allocated\n", newsock);
675 newsock->type = sock->type;
676 newsock->ops = ops = sock->ops;
678 clear_bit(SK_CONN, &svsk->sk_flags);
679 if ((err = ops->accept(sock, newsock, O_NONBLOCK)) < 0) {
680 if (err != -EAGAIN && net_ratelimit())
681 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
682 serv->sv_name, -err);
683 goto failed; /* aborted connection or whatever */
685 set_bit(SK_CONN, &svsk->sk_flags);
686 svc_sock_enqueue(svsk);
689 err = ops->getname(newsock, (struct sockaddr *) &sin, &slen, 1);
692 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
693 serv->sv_name, -err);
694 goto failed; /* aborted connection or whatever */
697 /* Ideally, we would want to reject connections from unauthorized
698 * hosts here, but when we get encription, the IP of the host won't
699 * tell us anything. For now just warn about unpriv connections.
701 if (ntohs(sin.sin_port) >= 1024) {
703 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
705 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
708 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
709 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
711 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
714 /* make sure that a write doesn't block forever when
717 newsock->sk->sndtimeo = HZ*30;
719 /* Precharge. Data may have arrived on the socket before we
720 * installed the data_ready callback.
722 set_bit(SK_DATA, &newsvsk->sk_flags);
723 svc_sock_enqueue(newsvsk);
725 /* make sure that we don't have too many active connections.
726 * If we have, something must be dropped.
727 * We randomly choose between newest and oldest (in terms
728 * of recent activity) and drop it.
730 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*10) {
731 struct svc_sock *svsk = NULL;
732 spin_lock_bh(&serv->sv_lock);
733 if (!list_empty(&serv->sv_tempsocks)) {
735 svsk = list_entry(serv->sv_tempsocks.prev,
739 svsk = list_entry(serv->sv_tempsocks.next,
742 set_bit(SK_CLOSE, &svsk->sk_flags);
745 spin_unlock_bh(&serv->sv_lock);
748 svc_sock_enqueue(svsk);
755 serv->sv_stats->nettcpconn++;
760 sock_release(newsock);
765 * Receive data from a TCP socket.
768 svc_tcp_recvfrom(struct svc_rqst *rqstp)
770 struct svc_sock *svsk = rqstp->rq_sock;
771 struct svc_serv *serv = svsk->sk_server;
772 struct svc_buf *bufp = &rqstp->rq_argbuf;
775 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
776 svsk, test_bit(SK_DATA, &svsk->sk_flags),
777 test_bit(SK_CONN, &svsk->sk_flags),
778 test_bit(SK_CLOSE, &svsk->sk_flags));
780 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
781 svc_delete_socket(svsk);
785 if (test_bit(SK_CONN, &svsk->sk_flags)) {
786 svc_tcp_accept(svsk);
787 svc_sock_received(svsk);
791 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
792 /* sndbuf needs to have room for one request
793 * per thread, otherwise we can stall even when the
794 * network isn't a bottleneck.
795 * rcvbuf just needs to be able to hold a few requests.
796 * Normally they will be removed from the queue
797 * as soon as a complete request arrives.
799 svc_sock_setbufsize(svsk->sk_sock,
800 (serv->sv_nrthreads+3) *
804 clear_bit(SK_DATA, &svsk->sk_flags);
806 /* Receive data. If we haven't got the record length yet, get
807 * the next four bytes. Otherwise try to gobble up as much as
808 * possible up to the complete record length.
810 if (svsk->sk_tcplen < 4) {
811 unsigned long want = 4 - svsk->sk_tcplen;
814 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
816 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
818 svsk->sk_tcplen += len;
822 svsk->sk_reclen = ntohl(svsk->sk_reclen);
823 if (!(svsk->sk_reclen & 0x80000000)) {
824 /* FIXME: technically, a record can be fragmented,
825 * and non-terminal fragments will not have the top
826 * bit set in the fragment length header.
827 * But apparently no known nfs clients send fragmented
829 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
830 (unsigned long) svsk->sk_reclen);
833 svsk->sk_reclen &= 0x7fffffff;
834 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
835 if (svsk->sk_reclen > (bufp->buflen<<2)) {
836 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (large)\n",
837 (unsigned long) svsk->sk_reclen);
842 /* Check whether enough data is available */
843 len = svc_recv_available(svsk);
847 if (len < svsk->sk_reclen) {
848 dprintk("svc: incomplete TCP record (%d of %d)\n",
849 len, svsk->sk_reclen);
850 svc_sock_received(svsk);
851 return -EAGAIN; /* record not complete */
853 set_bit(SK_DATA, &svsk->sk_flags);
856 bufp->iov[0].iov_base += 4;
857 bufp->iov[0].iov_len -= 4;
859 /* Now receive data */
860 len = svc_recvfrom(rqstp, bufp->iov, bufp->nriov, svsk->sk_reclen);
864 dprintk("svc: TCP complete record (%d bytes)\n", len);
866 /* Position reply write pointer immediately after
868 rqstp->rq_resbuf.buf += 1;
869 rqstp->rq_resbuf.len = 1;
871 rqstp->rq_skbuff = 0;
872 rqstp->rq_argbuf.buf += 1;
873 rqstp->rq_argbuf.len = (len >> 2);
874 rqstp->rq_prot = IPPROTO_TCP;
876 /* Reset TCP read info */
880 svc_sock_received(svsk);
882 serv->sv_stats->nettcpcnt++;
887 svc_delete_socket(svsk);
891 if (len == -EAGAIN) {
892 dprintk("RPC: TCP recvfrom got EAGAIN\n");
893 svc_sock_received(svsk);
895 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
896 svsk->sk_server->sv_name, -len);
897 svc_sock_received(svsk);
904 * Send out data on TCP socket.
907 svc_tcp_sendto(struct svc_rqst *rqstp)
909 struct svc_buf *bufp = &rqstp->rq_resbuf;
912 /* Set up the first element of the reply iovec.
913 * Any other iovecs that may be in use have been taken
914 * care of by the server implementation itself.
916 bufp->iov[0].iov_base = bufp->base;
917 bufp->iov[0].iov_len = bufp->len << 2;
918 bufp->base[0] = htonl(0x80000000|((bufp->len << 2) - 4));
920 sent = svc_sendto(rqstp, bufp->iov, bufp->nriov);
921 if (sent != bufp->len<<2) {
922 printk(KERN_NOTICE "rpc-srv/tcp: %s: sent only %d bytes of %d - shutting down socket\n",
923 rqstp->rq_sock->sk_server->sv_name,
924 sent, bufp->len << 2);
925 svc_delete_socket(rqstp->rq_sock);
932 svc_tcp_init(struct svc_sock *svsk)
934 struct sock *sk = svsk->sk_sk;
936 svsk->sk_recvfrom = svc_tcp_recvfrom;
937 svsk->sk_sendto = svc_tcp_sendto;
939 if (sk->state == TCP_LISTEN) {
940 dprintk("setting up TCP socket for listening\n");
941 sk->data_ready = svc_tcp_listen_data_ready;
943 dprintk("setting up TCP socket for reading\n");
944 sk->state_change = svc_tcp_state_change;
945 sk->data_ready = svc_tcp_data_ready;
946 sk->write_space = svc_write_space;
951 /* initialise setting must have enough space to
952 * receive and respond to one request.
953 * svc_tcp_recvfrom will re-adjust if necessary
955 svc_sock_setbufsize(svsk->sk_sock,
956 3 * svsk->sk_server->sv_bufsz,
957 3 * svsk->sk_server->sv_bufsz);
959 set_bit(SK_CHNGBUF, &svsk->sk_flags);
966 svc_sock_update_bufs(struct svc_serv *serv)
969 * The number of server threads has changed.
970 * flag all socket to the snd/rcv buffer sizes
972 * We don't just do it, as the locking is rather
973 * awkward at this point
975 struct list_head *le;
977 spin_lock_bh(&serv->sv_lock);
978 list_for_each(le, &serv->sv_permsocks) {
979 struct svc_sock *svsk =
980 list_entry(le, struct svc_sock, sk_list);
981 set_bit(SK_CHNGBUF, &svsk->sk_flags);
983 list_for_each(le, &serv->sv_tempsocks) {
984 struct svc_sock *svsk =
985 list_entry(le, struct svc_sock, sk_list);
986 set_bit(SK_CHNGBUF, &svsk->sk_flags);
988 spin_unlock_bh(&serv->sv_lock);
992 * Receive the next request on any socket.
995 svc_recv(struct svc_serv *serv, struct svc_rqst *rqstp, long timeout)
997 struct svc_sock *svsk =NULL;
999 DECLARE_WAITQUEUE(wait, current);
1001 dprintk("svc: server %p waiting for data (to = %ld)\n",
1006 "svc_recv: service %p, socket not NULL!\n",
1008 if (waitqueue_active(&rqstp->rq_wait))
1010 "svc_recv: service %p, wait queue active!\n",
1013 /* Initialize the buffers */
1014 rqstp->rq_argbuf = rqstp->rq_defbuf;
1015 rqstp->rq_resbuf = rqstp->rq_defbuf;
1020 spin_lock_bh(&serv->sv_lock);
1021 if (!list_empty(&serv->sv_tempsocks)) {
1022 svsk = list_entry(serv->sv_tempsocks.next,
1023 struct svc_sock, sk_list);
1024 /* apparently the "standard" is that clients close
1025 * idle connections after 5 minutes, servers after
1027 * http://www.connectathon.org/talks96/nfstcp.pdf
1029 if (CURRENT_TIME - svsk->sk_lastrecv < 6*60
1030 || test_bit(SK_BUSY, &svsk->sk_flags))
1034 set_bit(SK_BUSY, &svsk->sk_flags);
1035 set_bit(SK_CLOSE, &svsk->sk_flags);
1036 rqstp->rq_sock = svsk;
1038 } else if ((svsk = svc_sock_dequeue(serv)) != NULL) {
1039 rqstp->rq_sock = svsk;
1041 rqstp->rq_reserved = serv->sv_bufsz;
1042 svsk->sk_reserved += rqstp->rq_reserved;
1044 /* No data pending. Go to sleep */
1045 svc_serv_enqueue(serv, rqstp);
1048 * We have to be able to interrupt this wait
1049 * to bring down the daemons ...
1051 set_current_state(TASK_INTERRUPTIBLE);
1052 add_wait_queue(&rqstp->rq_wait, &wait);
1053 spin_unlock_bh(&serv->sv_lock);
1055 schedule_timeout(timeout);
1057 spin_lock_bh(&serv->sv_lock);
1058 remove_wait_queue(&rqstp->rq_wait, &wait);
1060 if (!(svsk = rqstp->rq_sock)) {
1061 svc_serv_dequeue(serv, rqstp);
1062 spin_unlock_bh(&serv->sv_lock);
1063 dprintk("svc: server %p, no data yet\n", rqstp);
1064 return signalled()? -EINTR : -EAGAIN;
1067 spin_unlock_bh(&serv->sv_lock);
1069 dprintk("svc: server %p, socket %p, inuse=%d\n",
1070 rqstp, svsk, svsk->sk_inuse);
1071 len = svsk->sk_recvfrom(rqstp);
1072 dprintk("svc: got len=%d\n", len);
1074 /* No data, incomplete (TCP) read, or accept() */
1075 if (len == 0 || len == -EAGAIN) {
1076 svc_sock_release(rqstp);
1079 svsk->sk_lastrecv = CURRENT_TIME;
1080 if (test_bit(SK_TEMP, &svsk->sk_flags)) {
1081 /* push active sockets to end of list */
1082 spin_lock_bh(&serv->sv_lock);
1083 list_del(&svsk->sk_list);
1084 list_add_tail(&svsk->sk_list, &serv->sv_tempsocks);
1085 spin_unlock_bh(&serv->sv_lock);
1088 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
1089 rqstp->rq_userset = 0;
1090 rqstp->rq_verfed = 0;
1092 svc_getlong(&rqstp->rq_argbuf, rqstp->rq_xid);
1093 svc_putlong(&rqstp->rq_resbuf, rqstp->rq_xid);
1095 /* Assume that the reply consists of a single buffer. */
1096 rqstp->rq_resbuf.nriov = 1;
1099 serv->sv_stats->netcnt++;
1107 svc_drop(struct svc_rqst *rqstp)
1109 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1110 svc_sock_release(rqstp);
1114 * Return reply to client.
1117 svc_send(struct svc_rqst *rqstp)
1119 struct svc_sock *svsk;
1122 if ((svsk = rqstp->rq_sock) == NULL) {
1123 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1124 __FILE__, __LINE__);
1128 /* release the receive skb before sending the reply */
1129 svc_release_skb(rqstp);
1131 len = svsk->sk_sendto(rqstp);
1132 svc_sock_release(rqstp);
1134 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1140 * Initialize socket for RPC use and create svc_sock struct
1141 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1143 static struct svc_sock *
1144 svc_setup_socket(struct svc_serv *serv, struct socket *sock,
1145 int *errp, int pmap_register)
1147 struct svc_sock *svsk;
1150 dprintk("svc: svc_setup_socket %p\n", sock);
1151 if (!(svsk = kmalloc(sizeof(*svsk), GFP_KERNEL))) {
1155 memset(svsk, 0, sizeof(*svsk));
1158 inet->user_data = svsk;
1159 svsk->sk_sock = sock;
1161 svsk->sk_ostate = inet->state_change;
1162 svsk->sk_odata = inet->data_ready;
1163 svsk->sk_owspace = inet->write_space;
1164 svsk->sk_server = serv;
1165 svsk->sk_lastrecv = CURRENT_TIME;
1167 /* Initialize the socket */
1168 if (sock->type == SOCK_DGRAM)
1169 *errp = svc_udp_init(svsk);
1171 *errp = svc_tcp_init(svsk);
1172 if (svsk->sk_sk == NULL)
1173 printk(KERN_WARNING "svsk->sk_sk == NULL after svc_prot_init!\n");
1175 /* Register socket with portmapper */
1176 if (*errp >= 0 && pmap_register)
1177 *errp = svc_register(serv, inet->protocol, ntohs(inet->sport));
1180 inet->user_data = NULL;
1186 spin_lock_bh(&serv->sv_lock);
1187 if (!pmap_register) {
1188 set_bit(SK_TEMP, &svsk->sk_flags);
1189 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1192 clear_bit(SK_TEMP, &svsk->sk_flags);
1193 list_add(&svsk->sk_list, &serv->sv_permsocks);
1195 spin_unlock_bh(&serv->sv_lock);
1197 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1203 * Create socket for RPC service.
1206 svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
1208 struct svc_sock *svsk;
1209 struct socket *sock;
1213 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1214 serv->sv_program->pg_name, protocol,
1215 NIPQUAD(sin->sin_addr.s_addr),
1216 ntohs(sin->sin_port));
1218 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1219 printk(KERN_WARNING "svc: only UDP and TCP "
1220 "sockets supported\n");
1223 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1225 if ((error = sock_create(PF_INET, type, protocol, &sock)) < 0)
1229 sock->sk->reuse = 1; /* allow address reuse */
1230 error = sock->ops->bind(sock, (struct sockaddr *) sin,
1236 if (protocol == IPPROTO_TCP) {
1237 if ((error = sock->ops->listen(sock, 64)) < 0)
1241 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
1245 dprintk("svc: svc_create_socket error = %d\n", -error);
1251 * Remove a dead socket
1254 svc_delete_socket(struct svc_sock *svsk)
1256 struct svc_serv *serv;
1259 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1261 serv = svsk->sk_server;
1264 sk->state_change = svsk->sk_ostate;
1265 sk->data_ready = svsk->sk_odata;
1266 sk->write_space = svsk->sk_owspace;
1268 spin_lock_bh(&serv->sv_lock);
1270 list_del(&svsk->sk_list);
1271 if (test_bit(SK_TEMP, &svsk->sk_flags))
1273 if (test_bit(SK_QUED, &svsk->sk_flags))
1274 list_del(&svsk->sk_ready);
1277 set_bit(SK_DEAD, &svsk->sk_flags);
1279 if (!svsk->sk_inuse) {
1280 spin_unlock_bh(&serv->sv_lock);
1281 sock_release(svsk->sk_sock);
1284 spin_unlock_bh(&serv->sv_lock);
1285 dprintk(KERN_NOTICE "svc: server socket destroy delayed\n");
1286 /* svsk->sk_server = NULL; */
1291 * Make a socket for nfsd and lockd
1294 svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1296 struct sockaddr_in sin;
1298 dprintk("svc: creating socket proto = %d\n", protocol);
1299 sin.sin_family = AF_INET;
1300 sin.sin_addr.s_addr = INADDR_ANY;
1301 sin.sin_port = htons(port);
1302 return svc_create_socket(serv, protocol, &sin);