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/malloc.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
36 #include <asm/uaccess.h>
38 #include <linux/sunrpc/types.h>
39 #include <linux/sunrpc/xdr.h>
40 #include <linux/sunrpc/svcsock.h>
41 #include <linux/sunrpc/stats.h>
43 /* SMP locking strategy:
45 * svc_sock->sk_lock and svc_serv->sv_lock protect their
46 * respective structures.
48 * Antideadlock ordering is sk_lock --> sv_lock.
51 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
54 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
55 int *errp
, int pmap_reg
);
56 static void svc_udp_data_ready(struct sock
*, int);
57 static int svc_udp_recvfrom(struct svc_rqst
*);
58 static int svc_udp_sendto(struct svc_rqst
*);
62 * Queue up an idle server thread. Must have serv->sv_lock held.
65 svc_serv_enqueue(struct svc_serv
*serv
, struct svc_rqst
*rqstp
)
67 rpc_append_list(&serv
->sv_threads
, rqstp
);
71 * Dequeue an nfsd thread. Must have serv->sv_lock held.
74 svc_serv_dequeue(struct svc_serv
*serv
, struct svc_rqst
*rqstp
)
76 rpc_remove_list(&serv
->sv_threads
, rqstp
);
80 * Release an skbuff after use
83 svc_release_skb(struct svc_rqst
*rqstp
)
85 struct sk_buff
*skb
= rqstp
->rq_skbuff
;
89 rqstp
->rq_skbuff
= NULL
;
91 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
92 skb_free_datagram(rqstp
->rq_sock
->sk_sk
, skb
);
96 * Queue up a socket with data pending. If there are idle nfsd
97 * processes, wake 'em up.
99 * This must be called with svsk->sk_lock held.
102 svc_sock_enqueue(struct svc_sock
*svsk
)
104 struct svc_serv
*serv
= svsk
->sk_server
;
105 struct svc_rqst
*rqstp
;
107 /* NOTE: Local BH is already disabled by our caller. */
108 spin_lock(&serv
->sv_lock
);
110 if (serv
->sv_threads
&& serv
->sv_sockets
)
112 "svc_sock_enqueue: threads and sockets both waiting??\n");
115 /* Don't enqueue socket while daemon is receiving */
116 dprintk("svc: socket %p busy, not enqueued\n", svsk
->sk_sk
);
120 /* Mark socket as busy. It will remain in this state until the
121 * server has processed all pending data and put the socket back
126 if ((rqstp
= serv
->sv_threads
) != NULL
) {
127 dprintk("svc: socket %p served by daemon %p\n",
129 svc_serv_dequeue(serv
, rqstp
);
132 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
133 rqstp
, rqstp
->rq_sock
);
134 rqstp
->rq_sock
= svsk
;
136 wake_up(&rqstp
->rq_wait
);
138 dprintk("svc: socket %p put into queue\n", svsk
->sk_sk
);
139 rpc_append_list(&serv
->sv_sockets
, svsk
);
144 spin_unlock(&serv
->sv_lock
);
148 * Dequeue the first socket. Must be called with the serv->sv_lock held.
150 static inline struct svc_sock
*
151 svc_sock_dequeue(struct svc_serv
*serv
)
153 struct svc_sock
*svsk
;
155 if ((svsk
= serv
->sv_sockets
) != NULL
)
156 rpc_remove_list(&serv
->sv_sockets
, svsk
);
159 dprintk("svc: socket %p dequeued, inuse=%d\n",
160 svsk
->sk_sk
, svsk
->sk_inuse
);
168 * Having read count bytes from a socket, check whether it
169 * needs to be re-enqueued.
172 svc_sock_received(struct svc_sock
*svsk
, int count
)
174 spin_lock_bh(&svsk
->sk_lock
);
175 if ((svsk
->sk_data
-= count
) < 0) {
176 printk(KERN_NOTICE
"svc: sk_data negative!\n");
179 svsk
->sk_rqstp
= NULL
; /* XXX */
181 if (svsk
->sk_conn
|| svsk
->sk_data
|| svsk
->sk_close
) {
182 dprintk("svc: socket %p re-enqueued after receive\n",
184 svc_sock_enqueue(svsk
);
186 spin_unlock_bh(&svsk
->sk_lock
);
190 * Dequeue a new connection.
193 svc_sock_accepted(struct svc_sock
*svsk
)
195 spin_lock_bh(&svsk
->sk_lock
);
198 if (svsk
->sk_conn
|| svsk
->sk_data
|| svsk
->sk_close
) {
199 dprintk("svc: socket %p re-enqueued after accept\n",
201 svc_sock_enqueue(svsk
);
203 spin_unlock_bh(&svsk
->sk_lock
);
207 * Release a socket after use.
210 svc_sock_release(struct svc_rqst
*rqstp
)
212 struct svc_sock
*svsk
= rqstp
->rq_sock
;
216 svc_release_skb(rqstp
);
217 rqstp
->rq_sock
= NULL
;
218 if (!--(svsk
->sk_inuse
) && svsk
->sk_dead
) {
219 dprintk("svc: releasing dead socket\n");
220 sock_release(svsk
->sk_sock
);
226 * External function to wake up a server waiting for data
229 svc_wake_up(struct svc_serv
*serv
)
231 struct svc_rqst
*rqstp
;
233 spin_lock_bh(&serv
->sv_lock
);
234 if ((rqstp
= serv
->sv_threads
) != NULL
) {
235 dprintk("svc: daemon %p woken up.\n", rqstp
);
237 svc_serv_dequeue(serv, rqstp);
238 rqstp->rq_sock = NULL;
240 wake_up(&rqstp
->rq_wait
);
242 spin_unlock_bh(&serv
->sv_lock
);
246 * Generic sendto routine
249 svc_sendto(struct svc_rqst
*rqstp
, struct iovec
*iov
, int nr
)
252 struct socket
*sock
= rqstp
->rq_sock
->sk_sock
;
256 for (i
= buflen
= 0; i
< nr
; i
++)
257 buflen
+= iov
[i
].iov_len
;
259 msg
.msg_name
= &rqstp
->rq_addr
;
260 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
263 msg
.msg_control
= NULL
;
264 msg
.msg_controllen
= 0;
266 msg
.msg_flags
= MSG_DONTWAIT
;
268 oldfs
= get_fs(); set_fs(KERNEL_DS
);
269 len
= sock_sendmsg(sock
, &msg
, buflen
);
272 dprintk("svc: socket %p sendto([%p %Zu... ], %d, %d) = %d\n",
273 rqstp
->rq_sock
, iov
[0].iov_base
, iov
[0].iov_len
, nr
, buflen
, len
);
279 * Check input queue length
282 svc_recv_available(struct svc_sock
*svsk
)
285 struct socket
*sock
= svsk
->sk_sock
;
288 oldfs
= get_fs(); set_fs(KERNEL_DS
);
289 err
= sock
->ops
->ioctl(sock
, TIOCINQ
, (unsigned long) &avail
);
292 return (err
>= 0)? avail
: err
;
296 * Generic recvfrom routine.
299 svc_recvfrom(struct svc_rqst
*rqstp
, struct iovec
*iov
, int nr
, int buflen
)
306 rqstp
->rq_addrlen
= sizeof(rqstp
->rq_addr
);
307 sock
= rqstp
->rq_sock
->sk_sock
;
309 msg
.msg_name
= &rqstp
->rq_addr
;
310 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
313 msg
.msg_control
= NULL
;
314 msg
.msg_controllen
= 0;
316 msg
.msg_flags
= MSG_DONTWAIT
;
318 oldfs
= get_fs(); set_fs(KERNEL_DS
);
319 len
= sock_recvmsg(sock
, &msg
, buflen
, MSG_DONTWAIT
);
322 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
323 * possibly we should cache this in the svc_sock structure
324 * at accept time. FIXME
326 alen
= sizeof(rqstp
->rq_addr
);
327 sock
->ops
->getname(sock
, (struct sockaddr
*)&rqstp
->rq_addr
, &alen
, 1);
329 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
330 rqstp
->rq_sock
, iov
[0].iov_base
, iov
[0].iov_len
, len
);
336 * INET callback when data has been received on the socket.
339 svc_udp_data_ready(struct sock
*sk
, int count
)
341 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->user_data
);
345 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
346 svsk
, sk
, count
, svsk
->sk_busy
);
347 spin_lock_bh(&svsk
->sk_lock
);
349 svc_sock_enqueue(svsk
);
350 spin_unlock_bh(&svsk
->sk_lock
);
354 * Receive a datagram from a UDP socket.
357 svc_udp_recvfrom(struct svc_rqst
*rqstp
)
359 struct svc_sock
*svsk
= rqstp
->rq_sock
;
360 struct svc_serv
*serv
= svsk
->sk_server
;
366 while ((skb
= skb_recv_datagram(svsk
->sk_sk
, 0, 1, &err
)) == NULL
) {
367 svc_sock_received(svsk
, 0);
370 /* possibly an icmp error */
371 dprintk("svc: recvfrom returned error %d\n", -err
);
374 /* There may be more data */
377 len
= skb
->len
- sizeof(struct udphdr
);
378 data
= (u32
*) (skb
->h
.raw
+ sizeof(struct udphdr
));
380 rqstp
->rq_skbuff
= skb
;
381 rqstp
->rq_argbuf
.base
= data
;
382 rqstp
->rq_argbuf
.buf
= data
;
383 rqstp
->rq_argbuf
.len
= (len
>> 2);
384 /* rqstp->rq_resbuf = rqstp->rq_defbuf; */
385 rqstp
->rq_prot
= IPPROTO_UDP
;
387 /* Get sender address */
388 rqstp
->rq_addr
.sin_family
= AF_INET
;
389 rqstp
->rq_addr
.sin_port
= skb
->h
.uh
->source
;
390 rqstp
->rq_addr
.sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
393 serv
->sv_stats
->netudpcnt
++;
395 /* One down, maybe more to go... */
396 svsk
->sk_sk
->stamp
= skb
->stamp
;
397 svc_sock_received(svsk
, 0);
403 svc_udp_sendto(struct svc_rqst
*rqstp
)
405 struct svc_buf
*bufp
= &rqstp
->rq_resbuf
;
408 /* Set up the first element of the reply iovec.
409 * Any other iovecs that may be in use have been taken
410 * care of by the server implementation itself.
412 /* bufp->base = bufp->area; */
413 bufp
->iov
[0].iov_base
= bufp
->base
;
414 bufp
->iov
[0].iov_len
= bufp
->len
<< 2;
416 error
= svc_sendto(rqstp
, bufp
->iov
, bufp
->nriov
);
417 if (error
== -ECONNREFUSED
)
418 /* ICMP error on earlier request. */
419 error
= svc_sendto(rqstp
, bufp
->iov
, bufp
->nriov
);
420 else if (error
== -EAGAIN
)
421 /* Ignore and wait for re-xmit */
428 svc_udp_init(struct svc_sock
*svsk
)
430 svsk
->sk_sk
->data_ready
= svc_udp_data_ready
;
431 svsk
->sk_recvfrom
= svc_udp_recvfrom
;
432 svsk
->sk_sendto
= svc_udp_sendto
;
438 * A state change on a listening socket means there's a connection
442 svc_tcp_state_change1(struct sock
*sk
)
444 struct svc_sock
*svsk
;
446 dprintk("svc: socket %p TCP (listen) state change %d\n",
449 if (sk
->state
!= TCP_ESTABLISHED
) {
450 /* Aborted connection, SYN_RECV or whatever... */
453 if (!(svsk
= (struct svc_sock
*) sk
->user_data
)) {
454 printk("svc: socket %p: no user data\n", sk
);
457 spin_lock_bh(&svsk
->sk_lock
);
459 svc_sock_enqueue(svsk
);
460 spin_unlock_bh(&svsk
->sk_lock
);
464 * A state change on a connected socket means it's dying or dead.
467 svc_tcp_state_change2(struct sock
*sk
)
469 struct svc_sock
*svsk
;
471 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
472 sk
, sk
->state
, sk
->user_data
);
474 if (!(svsk
= (struct svc_sock
*) sk
->user_data
)) {
475 printk("svc: socket %p: no user data\n", sk
);
478 spin_lock_bh(&svsk
->sk_lock
);
480 svc_sock_enqueue(svsk
);
481 spin_unlock_bh(&svsk
->sk_lock
);
485 svc_tcp_data_ready(struct sock
*sk
, int count
)
487 struct svc_sock
* svsk
;
489 /* Disconnect signalled through data_ready?!? */
490 if (sk
->state
!= TCP_ESTABLISHED
) {
491 svc_tcp_state_change2(sk
);
495 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
497 if (!(svsk
= (struct svc_sock
*)(sk
->user_data
)))
499 spin_lock_bh(&svsk
->sk_lock
);
501 svc_sock_enqueue(svsk
);
502 spin_unlock_bh(&svsk
->sk_lock
);
506 * Accept a TCP connection
509 svc_tcp_accept(struct svc_sock
*svsk
)
511 struct sockaddr_in sin
;
512 struct svc_serv
*serv
= svsk
->sk_server
;
513 struct socket
*sock
= svsk
->sk_sock
;
514 struct socket
*newsock
;
515 struct proto_ops
*ops
;
516 struct svc_sock
*newsvsk
;
519 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
523 if (!(newsock
= sock_alloc())) {
524 printk(KERN_WARNING
"%s: no more sockets!\n", serv
->sv_name
);
527 dprintk("svc: tcp_accept %p allocated\n", newsock
);
529 newsock
->type
= sock
->type
;
530 newsock
->ops
= ops
= sock
->ops
;
532 if ((err
= ops
->accept(sock
, newsock
, O_NONBLOCK
)) < 0) {
534 printk(KERN_WARNING
"%s: accept failed (err %d)!\n",
535 serv
->sv_name
, -err
);
536 goto failed
; /* aborted connection or whatever */
540 err
= ops
->getname(newsock
, (struct sockaddr
*) &sin
, &slen
, 1);
543 printk(KERN_WARNING
"%s: peername failed (err %d)!\n",
544 serv
->sv_name
, -err
);
545 goto failed
; /* aborted connection or whatever */
548 /* Ideally, we would want to reject connections from unauthorized
549 * hosts here, but when we get encription, the IP of the host won't
550 * tell us anything. For now just warn about unpriv connections.
552 if (ntohs(sin
.sin_port
) >= 1024) {
555 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
557 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
560 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv
->sv_name
,
561 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
563 if (!(newsvsk
= svc_setup_socket(serv
, newsock
, &err
, 0)))
566 /* Precharge. Data may have arrived on the socket before we
567 * installed the data_ready callback.
569 spin_lock_bh(&newsvsk
->sk_lock
);
570 newsvsk
->sk_data
= 1;
571 newsvsk
->sk_temp
= 1;
572 svc_sock_enqueue(newsvsk
);
573 spin_unlock_bh(&newsvsk
->sk_lock
);
576 serv
->sv_stats
->nettcpconn
++;
581 sock_release(newsock
);
586 * Receive data from a TCP socket.
589 svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
591 struct svc_sock
*svsk
= rqstp
->rq_sock
;
592 struct svc_serv
*serv
= svsk
->sk_server
;
593 struct svc_buf
*bufp
= &rqstp
->rq_argbuf
;
594 int len
, ready
, used
;
596 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
597 svsk
, svsk
->sk_data
, svsk
->sk_conn
, svsk
->sk_close
);
599 if (svsk
->sk_close
) {
600 svc_delete_socket(svsk
);
605 svc_tcp_accept(svsk
);
606 svc_sock_accepted(svsk
);
610 ready
= svsk
->sk_data
;
612 /* Receive data. If we haven't got the record length yet, get
613 * the next four bytes. Otherwise try to gobble up as much as
614 * possible up to the complete record length.
616 if (svsk
->sk_tcplen
< 4) {
617 unsigned long want
= 4 - svsk
->sk_tcplen
;
620 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
622 if ((len
= svc_recvfrom(rqstp
, &iov
, 1, want
)) < 0)
624 svsk
->sk_tcplen
+= len
;
626 svsk
->sk_reclen
= ntohl(svsk
->sk_reclen
);
627 if (!(svsk
->sk_reclen
& 0x80000000)) {
628 /* FIXME: technically, a record can be fragmented,
629 * and non-terminal fragments will not have the top
630 * bit set in the fragment length header.
631 * But apparently no known nfs clients send fragmented
633 /* FIXME: shutdown socket */
634 printk(KERN_NOTICE
"RPC: bad TCP reclen %08lx",
635 (unsigned long) svsk
->sk_reclen
);
638 svsk
->sk_reclen
&= 0x7fffffff;
639 dprintk("svc: TCP record, %d bytes\n", svsk
->sk_reclen
);
642 /* Check whether enough data is available */
643 len
= svc_recv_available(svsk
);
647 if (len
< svsk
->sk_reclen
) {
648 /* FIXME: if sk_reclen > window-size, then we will
649 * never be able to receive the record, so should
650 * shutdown the connection
652 dprintk("svc: incomplete TCP record (%d of %d)\n",
653 len
, svsk
->sk_reclen
);
654 svc_sock_received(svsk
, ready
);
655 return -EAGAIN
; /* record not complete */
657 /* if we think there is only one more record to read, but
658 * it is bigger than we expect, then two records must have arrived
659 * together, so pretend we aren't using the record.. */
660 if (len
> svsk
->sk_reclen
&& ready
== 1)
665 bufp
->iov
[0].iov_base
+= 4;
666 bufp
->iov
[0].iov_len
-= 4;
668 /* Now receive data */
669 len
= svc_recvfrom(rqstp
, bufp
->iov
, bufp
->nriov
, svsk
->sk_reclen
);
673 dprintk("svc: TCP complete record (%d bytes)\n", len
);
675 /* Position reply write pointer immediately after
677 rqstp
->rq_resbuf
.buf
+= 1;
678 rqstp
->rq_resbuf
.len
= 1;
680 rqstp
->rq_skbuff
= 0;
681 rqstp
->rq_argbuf
.buf
+= 1;
682 rqstp
->rq_argbuf
.len
= (len
>> 2);
683 rqstp
->rq_prot
= IPPROTO_TCP
;
685 /* Reset TCP read info */
689 svc_sock_received(svsk
, used
);
691 serv
->sv_stats
->nettcpcnt
++;
696 if (len
== -EAGAIN
) {
697 dprintk("RPC: TCP recvfrom got EAGAIN\n");
698 svc_sock_received(svsk
, ready
); /* Clear data ready */
700 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
701 svsk
->sk_server
->sv_name
, -len
);
702 svc_sock_received(svsk
, 0);
709 * Send out data on TCP socket.
710 * FIXME: Make the sendto call non-blocking in order not to hang
711 * a daemon on a dead client. Requires write queue maintenance.
714 svc_tcp_sendto(struct svc_rqst
*rqstp
)
716 struct svc_buf
*bufp
= &rqstp
->rq_resbuf
;
719 /* Set up the first element of the reply iovec.
720 * Any other iovecs that may be in use have been taken
721 * care of by the server implementation itself.
723 bufp
->iov
[0].iov_base
= bufp
->base
;
724 bufp
->iov
[0].iov_len
= bufp
->len
<< 2;
725 bufp
->base
[0] = htonl(0x80000000|((bufp
->len
<< 2) - 4));
727 sent
= svc_sendto(rqstp
, bufp
->iov
, bufp
->nriov
);
728 if (sent
!= bufp
->len
<<2) {
729 printk(KERN_NOTICE
"rpc-srv/tcp: %s: sent only %d bytes of %d - should shutdown socket\n",
730 rqstp
->rq_sock
->sk_server
->sv_name
,
731 sent
, bufp
->len
<< 2);
732 /* FIXME: should shutdown the socket, or allocate more memort
733 * or wait and try again or something. Otherwise
734 * client will get confused
741 svc_tcp_init(struct svc_sock
*svsk
)
743 struct sock
*sk
= svsk
->sk_sk
;
745 svsk
->sk_recvfrom
= svc_tcp_recvfrom
;
746 svsk
->sk_sendto
= svc_tcp_sendto
;
748 if (sk
->state
== TCP_LISTEN
) {
749 dprintk("setting up TCP socket for listening\n");
750 sk
->state_change
= svc_tcp_state_change1
;
752 dprintk("setting up TCP socket for reading\n");
753 sk
->state_change
= svc_tcp_state_change2
;
754 sk
->data_ready
= svc_tcp_data_ready
;
764 * Receive the next request on any socket.
767 svc_recv(struct svc_serv
*serv
, struct svc_rqst
*rqstp
, long timeout
)
769 struct svc_sock
*svsk
;
771 DECLARE_WAITQUEUE(wait
, current
);
773 dprintk("svc: server %p waiting for data (to = %ld)\n",
778 "svc_recv: service %p, socket not NULL!\n",
780 if (waitqueue_active(&rqstp
->rq_wait
))
782 "svc_recv: service %p, wait queue active!\n",
786 /* Initialize the buffers */
787 rqstp
->rq_argbuf
= rqstp
->rq_defbuf
;
788 rqstp
->rq_resbuf
= rqstp
->rq_defbuf
;
793 spin_lock_bh(&serv
->sv_lock
);
794 if ((svsk
= svc_sock_dequeue(serv
)) != NULL
) {
795 rqstp
->rq_sock
= svsk
;
798 /* No data pending. Go to sleep */
799 svc_serv_enqueue(serv
, rqstp
);
802 * We have to be able to interrupt this wait
803 * to bring down the daemons ...
805 set_current_state(TASK_INTERRUPTIBLE
);
806 add_wait_queue(&rqstp
->rq_wait
, &wait
);
807 spin_unlock_bh(&serv
->sv_lock
);
809 schedule_timeout(timeout
);
811 spin_lock_bh(&serv
->sv_lock
);
812 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
814 if (!(svsk
= rqstp
->rq_sock
)) {
815 svc_serv_dequeue(serv
, rqstp
);
816 spin_unlock_bh(&serv
->sv_lock
);
817 dprintk("svc: server %p, no data yet\n", rqstp
);
818 return signalled()? -EINTR
: -EAGAIN
;
821 spin_unlock_bh(&serv
->sv_lock
);
823 dprintk("svc: server %p, socket %p, inuse=%d\n",
824 rqstp
, svsk
, svsk
->sk_inuse
);
825 len
= svsk
->sk_recvfrom(rqstp
);
826 dprintk("svc: got len=%d\n", len
);
828 /* No data, incomplete (TCP) read, or accept() */
829 if (len
== 0 || len
== -EAGAIN
) {
830 svc_sock_release(rqstp
);
834 rqstp
->rq_secure
= ntohs(rqstp
->rq_addr
.sin_port
) < 1024;
835 rqstp
->rq_userset
= 0;
836 rqstp
->rq_verfed
= 0;
838 svc_getlong(&rqstp
->rq_argbuf
, rqstp
->rq_xid
);
839 svc_putlong(&rqstp
->rq_resbuf
, rqstp
->rq_xid
);
841 /* Assume that the reply consists of a single buffer. */
842 rqstp
->rq_resbuf
.nriov
= 1;
845 serv
->sv_stats
->netcnt
++;
853 svc_drop(struct svc_rqst
*rqstp
)
855 dprintk("svc: socket %p dropped request\n", rqstp
->rq_sock
);
856 svc_sock_release(rqstp
);
860 * Return reply to client.
863 svc_send(struct svc_rqst
*rqstp
)
865 struct svc_sock
*svsk
;
868 if ((svsk
= rqstp
->rq_sock
) == NULL
) {
869 printk(KERN_WARNING
"NULL socket pointer in %s:%d\n",
874 /* release the receive skb before sending the reply */
875 svc_release_skb(rqstp
);
877 len
= svsk
->sk_sendto(rqstp
);
878 svc_sock_release(rqstp
);
880 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
886 * Initialize socket for RPC use and create svc_sock struct
887 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
889 static struct svc_sock
*
890 svc_setup_socket(struct svc_serv
*serv
, struct socket
*sock
,
891 int *errp
, int pmap_register
)
893 struct svc_sock
*svsk
;
896 dprintk("svc: svc_setup_socket %p\n", sock
);
897 if (!(svsk
= kmalloc(sizeof(*svsk
), GFP_KERNEL
))) {
901 memset(svsk
, 0, sizeof(*svsk
));
904 inet
->user_data
= svsk
;
905 svsk
->sk_sock
= sock
;
907 svsk
->sk_ostate
= inet
->state_change
;
908 svsk
->sk_odata
= inet
->data_ready
;
909 svsk
->sk_server
= serv
;
910 spin_lock_init(&svsk
->sk_lock
);
912 /* Initialize the socket */
913 if (sock
->type
== SOCK_DGRAM
)
914 *errp
= svc_udp_init(svsk
);
916 *errp
= svc_tcp_init(svsk
);
917 if (svsk
->sk_sk
== NULL
)
918 printk(KERN_WARNING
"svsk->sk_sk == NULL after svc_prot_init!\n");
920 /* Register socket with portmapper */
921 if (*errp
>= 0 && pmap_register
)
922 *errp
= svc_register(serv
, inet
->protocol
, ntohs(inet
->sport
));
925 inet
->user_data
= NULL
;
930 spin_lock_bh(&serv
->sv_lock
);
931 svsk
->sk_list
= serv
->sv_allsocks
;
932 serv
->sv_allsocks
= svsk
;
933 spin_unlock_bh(&serv
->sv_lock
);
935 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
941 * Create socket for RPC service.
944 svc_create_socket(struct svc_serv
*serv
, int protocol
, struct sockaddr_in
*sin
)
946 struct svc_sock
*svsk
;
951 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
952 serv
->sv_program
->pg_name
, protocol
,
953 NIPQUAD(sin
->sin_addr
.s_addr
),
954 ntohs(sin
->sin_port
));
956 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
957 printk(KERN_WARNING
"svc: only UDP and TCP "
958 "sockets supported\n");
961 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
963 if ((error
= sock_create(PF_INET
, type
, protocol
, &sock
)) < 0)
967 error
= sock
->ops
->bind(sock
, (struct sockaddr
*) sin
,
973 if (protocol
== IPPROTO_TCP
) {
974 if ((error
= sock
->ops
->listen(sock
, 5)) < 0)
978 if ((svsk
= svc_setup_socket(serv
, sock
, &error
, 1)) != NULL
)
982 dprintk("svc: svc_create_socket error = %d\n", -error
);
988 * Remove a dead socket
991 svc_delete_socket(struct svc_sock
*svsk
)
993 struct svc_sock
**rsk
;
994 struct svc_serv
*serv
;
997 dprintk("svc: svc_delete_socket(%p)\n", svsk
);
999 serv
= svsk
->sk_server
;
1002 sk
->state_change
= svsk
->sk_ostate
;
1003 sk
->data_ready
= svsk
->sk_odata
;
1005 spin_lock_bh(&serv
->sv_lock
);
1007 for (rsk
= &serv
->sv_allsocks
; *rsk
; rsk
= &(*rsk
)->sk_list
) {
1012 spin_unlock_bh(&serv
->sv_lock
);
1015 *rsk
= svsk
->sk_list
;
1017 rpc_remove_list(&serv
->sv_sockets
, svsk
);
1019 spin_unlock_bh(&serv
->sv_lock
);
1023 if (!svsk
->sk_inuse
) {
1024 sock_release(svsk
->sk_sock
);
1027 printk(KERN_NOTICE
"svc: server socket destroy delayed\n");
1028 /* svsk->sk_server = NULL; */
1033 * Make a socket for nfsd and lockd
1036 svc_makesock(struct svc_serv
*serv
, int protocol
, unsigned short port
)
1038 struct sockaddr_in sin
;
1040 dprintk("svc: creating socket proto = %d\n", protocol
);
1041 sin
.sin_family
= AF_INET
;
1042 sin
.sin_addr
.s_addr
= INADDR_ANY
;
1043 sin
.sin_port
= htons(port
);
1044 return svc_create_socket(serv
, protocol
, &sin
);