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 #if LINUX_VERSION_CODE >= 0x020100
37 #include <asm/uaccess.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>
46 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
49 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
50 int *errp
, int pmap_reg
);
51 static void svc_udp_data_ready(struct sock
*, int);
52 static int svc_udp_recvfrom(struct svc_rqst
*);
53 static int svc_udp_sendto(struct svc_rqst
*);
57 * Queue up an idle server thread.
60 svc_serv_enqueue(struct svc_serv
*serv
, struct svc_rqst
*rqstp
)
62 rpc_append_list(&serv
->sv_threads
, rqstp
);
66 * Dequeue an nfsd thread.
69 svc_serv_dequeue(struct svc_serv
*serv
, struct svc_rqst
*rqstp
)
71 rpc_remove_list(&serv
->sv_threads
, rqstp
);
75 * Release an skbuff after use
78 svc_release_skb(struct svc_rqst
*rqstp
)
80 struct sk_buff
*skb
= rqstp
->rq_skbuff
;
84 rqstp
->rq_skbuff
= NULL
;
86 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
87 skb_free_datagram(rqstp
->rq_sock
->sk_sk
, skb
);
91 * Queue up a socket with data pending. If there are idle nfsd
92 * processes, wake 'em up.
93 * When calling this function, you should make sure it can't be interrupted
94 * by the network bottom half.
97 svc_sock_enqueue(struct svc_sock
*svsk
)
99 struct svc_serv
*serv
= svsk
->sk_server
;
100 struct svc_rqst
*rqstp
;
102 if (serv
->sv_threads
&& serv
->sv_sockets
)
104 "svc_sock_enqueue: threads and sockets both waiting??\n");
107 /* Don't enqueue socket while daemon is receiving */
108 dprintk("svc: socket %p busy, not enqueued\n", svsk
->sk_sk
);
112 /* Mark socket as busy. It will remain in this state until the
113 * server has processed all pending data and put the socket back
118 if ((rqstp
= serv
->sv_threads
) != NULL
) {
119 dprintk("svc: socket %p served by daemon %p\n",
121 svc_serv_dequeue(serv
, rqstp
);
124 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
125 rqstp
, rqstp
->rq_sock
);
126 rqstp
->rq_sock
= svsk
;
128 wake_up(&rqstp
->rq_wait
);
130 dprintk("svc: socket %p put into queue\n", svsk
->sk_sk
);
131 rpc_append_list(&serv
->sv_sockets
, svsk
);
137 * Dequeue the first socket.
139 static inline struct svc_sock
*
140 svc_sock_dequeue(struct svc_serv
*serv
)
142 struct svc_sock
*svsk
;
145 if ((svsk
= serv
->sv_sockets
) != NULL
)
146 rpc_remove_list(&serv
->sv_sockets
, svsk
);
150 dprintk("svc: socket %p dequeued, inuse=%d\n",
151 svsk
->sk_sk
, svsk
->sk_inuse
);
159 * Having read count bytes from a socket, check whether it
160 * needs to be re-enqueued.
163 svc_sock_received(struct svc_sock
*svsk
, int count
)
166 if ((svsk
->sk_data
-= count
) < 0) {
167 printk(KERN_NOTICE
"svc: sk_data negative!\n");
170 svsk
->sk_rqstp
= NULL
; /* XXX */
172 if (svsk
->sk_conn
|| svsk
->sk_data
|| svsk
->sk_close
) {
173 dprintk("svc: socket %p re-enqueued after receive\n",
175 svc_sock_enqueue(svsk
);
181 * Dequeue a new connection.
184 svc_sock_accepted(struct svc_sock
*svsk
)
189 if (svsk
->sk_conn
|| svsk
->sk_data
|| svsk
->sk_close
) {
190 dprintk("svc: socket %p re-enqueued after accept\n",
192 svc_sock_enqueue(svsk
);
198 * Release a socket after use.
201 svc_sock_release(struct svc_rqst
*rqstp
)
203 struct svc_sock
*svsk
= rqstp
->rq_sock
;
207 svc_release_skb(rqstp
);
208 rqstp
->rq_sock
= NULL
;
209 if (!--(svsk
->sk_inuse
) && svsk
->sk_dead
) {
210 dprintk("svc: releasing dead socket\n");
211 sock_release(svsk
->sk_sock
);
217 * External function to wake up a server waiting for data
220 svc_wake_up(struct svc_serv
*serv
)
222 struct svc_rqst
*rqstp
;
224 if ((rqstp
= serv
->sv_threads
) != NULL
) {
225 dprintk("svc: daemon %p woken up.\n", rqstp
);
227 svc_serv_dequeue(serv, rqstp);
228 rqstp->rq_sock = NULL;
230 wake_up(&rqstp
->rq_wait
);
235 * Generic sendto routine
238 svc_sendto(struct svc_rqst
*rqstp
, struct iovec
*iov
, int nr
)
241 struct socket
*sock
= rqstp
->rq_sock
->sk_sock
;
245 for (i
= buflen
= 0; i
< nr
; i
++)
246 buflen
+= iov
[i
].iov_len
;
248 msg
.msg_name
= &rqstp
->rq_addr
;
249 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
252 msg
.msg_control
= NULL
;
253 msg
.msg_controllen
= 0;
255 #if LINUX_VERSION_CODE >= 0x020100
256 msg
.msg_flags
= MSG_DONTWAIT
;
258 oldfs
= get_fs(); set_fs(KERNEL_DS
);
259 len
= sock_sendmsg(sock
, &msg
, buflen
);
264 oldfs
= get_fs(); set_fs(KERNEL_DS
);
265 len
= sock
->ops
->sendmsg(sock
, &msg
, buflen
, 1, 0);
269 dprintk("svc: socket %p sendto([%p %lu... ], %d, %d) = %d\n",
270 rqstp
->rq_sock
, iov
[0].iov_base
,
271 (unsigned long) iov
[0].iov_len
, nr
,
278 * Check input queue length
281 svc_recv_available(struct svc_sock
*svsk
)
284 struct socket
*sock
= svsk
->sk_sock
;
287 oldfs
= get_fs(); set_fs(KERNEL_DS
);
288 err
= sock
->ops
->ioctl(sock
, TIOCINQ
, (unsigned long) &avail
);
291 return (err
>= 0)? avail
: err
;
295 * Generic recvfrom routine.
298 svc_recvfrom(struct svc_rqst
*rqstp
, struct iovec
*iov
, int nr
, int buflen
)
305 rqstp
->rq_addrlen
= sizeof(rqstp
->rq_addr
);
306 sock
= rqstp
->rq_sock
->sk_sock
;
308 msg
.msg_name
= &rqstp
->rq_addr
;
309 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
312 msg
.msg_control
= NULL
;
313 msg
.msg_controllen
= 0;
315 #if LINUX_VERSION_CODE >= 0x020100
316 msg
.msg_flags
= MSG_DONTWAIT
;
318 oldfs
= get_fs(); set_fs(KERNEL_DS
);
319 len
= sock_recvmsg(sock
, &msg
, buflen
, MSG_DONTWAIT
);
324 oldfs
= get_fs(); set_fs(KERNEL_DS
);
325 len
= sock
->ops
->recvmsg(sock
, &msg
, buflen
, 0, 1, &rqstp
->rq_addrlen
);
329 dprintk("svc: socket %p recvfrom(%p, %lu) = %d\n", rqstp
->rq_sock
,
330 iov
[0].iov_base
, (unsigned long) 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
);
348 svc_sock_enqueue(svsk
);
352 * Receive a datagram from a UDP socket.
355 svc_udp_recvfrom(struct svc_rqst
*rqstp
)
357 struct svc_sock
*svsk
= rqstp
->rq_sock
;
358 struct svc_serv
*serv
= svsk
->sk_server
;
364 while ((skb
= skb_recv_datagram(svsk
->sk_sk
, 0, 1, &err
)) == NULL
) {
365 svc_sock_received(svsk
, 0);
368 /* possibly an icmp error */
369 dprintk("svc: recvfrom returned error %d\n", -err
);
372 /* There may be more data */
375 len
= skb
->len
- sizeof(struct udphdr
);
376 data
= (u32
*) (skb
->h
.raw
+ sizeof(struct udphdr
));
378 rqstp
->rq_skbuff
= skb
;
379 rqstp
->rq_argbuf
.base
= data
;
380 rqstp
->rq_argbuf
.buf
= data
;
381 rqstp
->rq_argbuf
.len
= (len
>> 2);
382 /* rqstp->rq_resbuf = rqstp->rq_defbuf; */
383 rqstp
->rq_prot
= IPPROTO_UDP
;
385 /* Get sender address */
386 rqstp
->rq_addr
.sin_family
= AF_INET
;
387 rqstp
->rq_addr
.sin_port
= skb
->h
.uh
->source
;
388 #if LINUX_VERSION_CODE >= 0x020100
389 rqstp
->rq_addr
.sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
391 rqstp
->rq_addr
.sin_addr
.s_addr
= skb
->saddr
;
395 serv
->sv_stats
->netudpcnt
++;
397 /* One down, maybe more to go... */
398 svsk
->sk_sk
->stamp
= skb
->stamp
;
399 svc_sock_received(svsk
, 0);
405 svc_udp_sendto(struct svc_rqst
*rqstp
)
407 struct svc_buf
*bufp
= &rqstp
->rq_resbuf
;
410 /* Set up the first element of the reply iovec.
411 * Any other iovecs that may be in use have been taken
412 * care of by the server implementation itself.
414 /* bufp->base = bufp->area; */
415 bufp
->iov
[0].iov_base
= bufp
->base
;
416 bufp
->iov
[0].iov_len
= bufp
->len
<< 2;
418 error
= svc_sendto(rqstp
, bufp
->iov
, bufp
->nriov
);
419 if (error
== -ECONNREFUSED
)
420 /* ICMP error on earlier request. */
421 error
= svc_sendto(rqstp
, bufp
->iov
, bufp
->nriov
);
422 else if (error
== -EAGAIN
)
423 /* Ignore and wait for re-xmit */
430 svc_udp_init(struct svc_sock
*svsk
)
432 svsk
->sk_sk
->data_ready
= svc_udp_data_ready
;
433 svsk
->sk_recvfrom
= svc_udp_recvfrom
;
434 svsk
->sk_sendto
= svc_udp_sendto
;
440 * A state change on a listening socket means there's a connection
444 svc_tcp_state_change1(struct sock
*sk
)
446 struct svc_sock
*svsk
;
448 dprintk("svc: socket %p TCP (listen) state change %d\n",
451 if (sk
->state
!= TCP_ESTABLISHED
) {
452 /* Aborted connection, SYN_RECV or whatever... */
455 if (!(svsk
= (struct svc_sock
*) sk
->user_data
)) {
456 printk("svc: socket %p: no user data\n", sk
);
460 svc_sock_enqueue(svsk
);
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
);
479 svc_sock_enqueue(svsk
);
483 svc_tcp_data_ready(struct sock
*sk
, int count
)
485 struct svc_sock
* svsk
;
487 /* Disconnect signalled through data_ready?!? */
488 if (sk
->state
!= TCP_ESTABLISHED
) {
489 svc_tcp_state_change2(sk
);
493 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
495 if (!(svsk
= (struct svc_sock
*)(sk
->user_data
)))
498 svc_sock_enqueue(svsk
);
502 * Accept a TCP connection
505 svc_tcp_accept(struct svc_sock
*svsk
)
507 struct sockaddr_in sin
;
508 struct svc_serv
*serv
= svsk
->sk_server
;
509 struct socket
*sock
= svsk
->sk_sock
;
510 struct socket
*newsock
;
511 struct proto_ops
*ops
;
512 struct svc_sock
*newsvsk
;
515 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
519 if (!(newsock
= sock_alloc())) {
520 printk(KERN_WARNING
"%s: no more sockets!\n", serv
->sv_name
);
523 dprintk("svc: tcp_accept %p allocated\n", newsock
);
525 newsock
->type
= sock
->type
;
526 newsock
->ops
= ops
= sock
->ops
;
528 if ((err
= ops
->accept(sock
, newsock
, O_NONBLOCK
)) < 0) {
530 printk(KERN_WARNING
"%s: accept failed (err %d)!\n",
531 serv
->sv_name
, -err
);
532 goto failed
; /* aborted connection or whatever */
536 err
= ops
->getname(newsock
, (struct sockaddr
*) &sin
, &slen
, 1);
539 printk(KERN_WARNING
"%s: peername failed (err %d)!\n",
540 serv
->sv_name
, -err
);
541 goto failed
; /* aborted connection or whatever */
544 /* Ideally, we would want to reject connections from unauthorized
545 * hosts here, but we have no generic client tables. For now,
546 * we just punt connects from unprivileged ports. */
547 if (ntohs(sin
.sin_port
) >= 1024) {
550 "%s: connect from unprivileged port: %s:%d",
552 in_ntoa(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
556 dprintk("%s: connect from %s:%04x\n", serv
->sv_name
,
557 in_ntoa(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
559 if (!(newsvsk
= svc_setup_socket(serv
, newsock
, &err
, 0)))
562 /* Precharge. Data may have arrived on the socket before we
563 * installed the data_ready callback.
565 newsvsk
->sk_data
= 1;
566 newsvsk
->sk_temp
= 1;
567 svc_sock_enqueue(newsvsk
);
570 serv
->sv_stats
->nettcpconn
++;
575 sock_release(newsock
);
580 * Receive data from a TCP socket.
583 svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
585 struct svc_sock
*svsk
= rqstp
->rq_sock
;
586 struct svc_serv
*serv
= svsk
->sk_server
;
587 struct svc_buf
*bufp
= &rqstp
->rq_argbuf
;
590 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
591 svsk
, svsk
->sk_data
, svsk
->sk_conn
, svsk
->sk_close
);
593 if (svsk
->sk_close
) {
594 svc_delete_socket(svsk
);
599 svc_tcp_accept(svsk
);
600 svc_sock_accepted(svsk
);
604 ready
= svsk
->sk_data
;
606 /* Receive data. If we haven't got the record length yet, get
607 * the next four bytes. Otherwise try to gobble up as much as
608 * possible up to the complete record length.
610 if (svsk
->sk_tcplen
< 4) {
611 unsigned long want
= 4 - svsk
->sk_tcplen
;
614 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
616 if ((len
= svc_recvfrom(rqstp
, &iov
, 1, want
)) < 0)
618 svsk
->sk_tcplen
+= len
;
620 svsk
->sk_reclen
= ntohl(svsk
->sk_reclen
);
621 if (!(svsk
->sk_reclen
& 0x80000000)) {
622 /* FIXME: shutdown socket */
623 printk(KERN_NOTICE
"RPC: bad TCP reclen %08lx",
624 (unsigned long) svsk
->sk_reclen
);
627 svsk
->sk_reclen
&= 0x7fffffff;
628 dprintk("svc: TCP record, %d bytes\n", svsk
->sk_reclen
);
631 /* Check whether enough data is available */
632 len
= svc_recv_available(svsk
);
636 if (len
< svsk
->sk_reclen
) {
637 dprintk("svc: incomplete TCP record (%d of %d)\n",
638 len
, svsk
->sk_reclen
);
639 svc_sock_received(svsk
, ready
);
640 len
= -EAGAIN
; /* record not complete */
644 bufp
->iov
[0].iov_base
+= 4;
645 bufp
->iov
[0].iov_len
-= 4;
647 /* Now receive data */
648 len
= svc_recvfrom(rqstp
, bufp
->iov
, bufp
->nriov
, svsk
->sk_reclen
);
652 dprintk("svc: TCP complete record (%d bytes)\n", len
);
654 /* Position reply write pointer immediately after
656 rqstp
->rq_resbuf
.buf
+= 1;
657 rqstp
->rq_resbuf
.len
= 1;
659 rqstp
->rq_skbuff
= 0;
660 rqstp
->rq_argbuf
.buf
+= 1;
661 rqstp
->rq_argbuf
.len
= (len
>> 2);
662 rqstp
->rq_prot
= IPPROTO_TCP
;
664 /* Reset TCP read info */
668 svc_sock_received(svsk
, 1);
670 serv
->sv_stats
->nettcpcnt
++;
675 if (len
== -EAGAIN
) {
676 dprintk("RPC: TCP recvfrom got EAGAIN\n");
677 svc_sock_received(svsk
, ready
); /* Clear data ready */
679 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
680 svsk
->sk_server
->sv_name
, -len
);
681 svc_sock_received(svsk
, 0);
688 * Send out data on TCP socket.
689 * FIXME: Make the sendto call non-blocking in order not to hang
690 * a daemon on a dead client. Requires write queue maintenance.
693 svc_tcp_sendto(struct svc_rqst
*rqstp
)
695 struct svc_buf
*bufp
= &rqstp
->rq_resbuf
;
697 /* Set up the first element of the reply iovec.
698 * Any other iovecs that may be in use have been taken
699 * care of by the server implementation itself.
701 bufp
->iov
[0].iov_base
= bufp
->base
;
702 bufp
->iov
[0].iov_len
= bufp
->len
<< 2;
703 bufp
->base
[0] = htonl(0x80000000|((bufp
->len
<< 2) - 4));
705 return svc_sendto(rqstp
, bufp
->iov
, bufp
->nriov
);
709 svc_tcp_init(struct svc_sock
*svsk
)
711 struct sock
*sk
= svsk
->sk_sk
;
713 svsk
->sk_recvfrom
= svc_tcp_recvfrom
;
714 svsk
->sk_sendto
= svc_tcp_sendto
;
716 if (sk
->state
== TCP_LISTEN
) {
717 dprintk("setting up TCP socket for listening\n");
718 sk
->state_change
= svc_tcp_state_change1
;
720 dprintk("setting up TCP socket for reading\n");
721 sk
->state_change
= svc_tcp_state_change2
;
722 sk
->data_ready
= svc_tcp_data_ready
;
732 * Receive the next request on any socket.
735 svc_recv(struct svc_serv
*serv
, struct svc_rqst
*rqstp
, long timeout
)
737 struct svc_sock
*svsk
;
739 DECLARE_WAITQUEUE(wait
, current
);
741 dprintk("svc: server %p waiting for data (to = %ld)\n",
746 "svc_recv: service %p, socket not NULL!\n",
748 if (waitqueue_active(&rqstp
->rq_wait
))
750 "svc_recv: service %p, wait queue active!\n",
754 /* Initialize the buffers */
755 rqstp
->rq_argbuf
= rqstp
->rq_defbuf
;
756 rqstp
->rq_resbuf
= rqstp
->rq_defbuf
;
762 if ((svsk
= svc_sock_dequeue(serv
)) != NULL
) {
763 rqstp
->rq_sock
= svsk
;
766 /* No data pending. Go to sleep */
767 svc_serv_enqueue(serv
, rqstp
);
770 * We have to be able to interrupt this wait
771 * to bring down the daemons ...
773 current
->state
= TASK_INTERRUPTIBLE
;
774 add_wait_queue(&rqstp
->rq_wait
, &wait
);
776 schedule_timeout(timeout
);
778 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
781 if (!(svsk
= rqstp
->rq_sock
)) {
782 svc_serv_dequeue(serv
, rqstp
);
784 dprintk("svc: server %p, no data yet\n", rqstp
);
785 return signalled()? -EINTR
: -EAGAIN
;
790 dprintk("svc: server %p, socket %p, inuse=%d\n",
791 rqstp
, svsk
, svsk
->sk_inuse
);
792 len
= svsk
->sk_recvfrom(rqstp
);
793 dprintk("svc: got len=%d\n", len
);
795 /* No data, incomplete (TCP) read, or accept() */
796 if (len
== 0 || len
== -EAGAIN
) {
797 svc_sock_release(rqstp
);
801 rqstp
->rq_secure
= ntohs(rqstp
->rq_addr
.sin_port
) < 1024;
802 rqstp
->rq_userset
= 0;
803 rqstp
->rq_verfed
= 0;
805 svc_getlong(&rqstp
->rq_argbuf
, rqstp
->rq_xid
);
806 svc_putlong(&rqstp
->rq_resbuf
, rqstp
->rq_xid
);
808 /* Assume that the reply consists of a single buffer. */
809 rqstp
->rq_resbuf
.nriov
= 1;
812 serv
->sv_stats
->netcnt
++;
820 svc_drop(struct svc_rqst
*rqstp
)
822 dprintk("svc: socket %p dropped request\n", rqstp
->rq_sock
);
823 svc_sock_release(rqstp
);
827 * Return reply to client.
830 svc_send(struct svc_rqst
*rqstp
)
832 struct svc_sock
*svsk
;
835 if ((svsk
= rqstp
->rq_sock
) == NULL
) {
836 printk(KERN_WARNING
"NULL socket pointer in %s:%d\n",
841 /* release the receive skb before sending the reply */
842 svc_release_skb(rqstp
);
844 len
= svsk
->sk_sendto(rqstp
);
845 svc_sock_release(rqstp
);
847 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
853 * Initialize socket for RPC use and create svc_sock struct
854 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
856 static struct svc_sock
*
857 svc_setup_socket(struct svc_serv
*serv
, struct socket
*sock
,
858 int *errp
, int pmap_register
)
860 struct svc_sock
*svsk
;
863 dprintk("svc: svc_setup_socket %p\n", sock
);
864 if (!(svsk
= kmalloc(sizeof(*svsk
), GFP_KERNEL
))) {
868 memset(svsk
, 0, sizeof(*svsk
));
870 #if LINUX_VERSION_CODE >= 0x020100
873 inet
= (struct sock
*) sock
->data
;
875 inet
->user_data
= svsk
;
876 svsk
->sk_sock
= sock
;
878 svsk
->sk_ostate
= inet
->state_change
;
879 svsk
->sk_odata
= inet
->data_ready
;
880 svsk
->sk_server
= serv
;
882 /* Initialize the socket */
883 if (sock
->type
== SOCK_DGRAM
)
884 *errp
= svc_udp_init(svsk
);
886 *errp
= svc_tcp_init(svsk
);
887 if (svsk
->sk_sk
== NULL
)
888 printk(KERN_WARNING
"svsk->sk_sk == NULL after svc_prot_init!\n");
890 /* Register socket with portmapper */
891 if (*errp
>= 0 && pmap_register
)
892 *errp
= svc_register(serv
, inet
->protocol
, ntohs(inet
->sport
));
895 inet
->user_data
= NULL
;
900 svsk
->sk_list
= serv
->sv_allsocks
;
901 serv
->sv_allsocks
= svsk
;
903 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
909 * Create socket for RPC service.
912 svc_create_socket(struct svc_serv
*serv
, int protocol
, struct sockaddr_in
*sin
)
914 struct svc_sock
*svsk
;
919 dprintk("svc: svc_create_socket(%s, %d, %08x:%d)\n",
920 serv
->sv_program
->pg_name
, protocol
,
921 ntohl(sin
->sin_addr
.s_addr
),
922 ntohs(sin
->sin_port
));
924 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
925 printk(KERN_WARNING
"svc: only UDP and TCP "
926 "sockets supported\n");
929 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
931 if ((error
= sock_create(PF_INET
, type
, protocol
, &sock
)) < 0)
935 error
= sock
->ops
->bind(sock
, (struct sockaddr
*) sin
,
941 if (protocol
== IPPROTO_TCP
) {
942 if ((error
= sock
->ops
->listen(sock
, 5)) < 0)
944 sock
->flags
|= SO_ACCEPTCON
;
947 if ((svsk
= svc_setup_socket(serv
, sock
, &error
, 1)) != NULL
)
951 dprintk("svc: svc_create_socket error = %d\n", -error
);
957 * Remove a dead socket
960 svc_delete_socket(struct svc_sock
*svsk
)
962 struct svc_sock
**rsk
;
963 struct svc_serv
*serv
;
966 dprintk("svc: svc_delete_socket(%p)\n", svsk
);
968 serv
= svsk
->sk_server
;
971 sk
->state_change
= svsk
->sk_ostate
;
972 sk
->data_ready
= svsk
->sk_odata
;
974 for (rsk
= &serv
->sv_allsocks
; *rsk
; rsk
= &(*rsk
)->sk_list
) {
980 *rsk
= svsk
->sk_list
;
983 rpc_remove_list(&serv
->sv_sockets
, svsk
);
986 if (!svsk
->sk_inuse
) {
987 sock_release(svsk
->sk_sock
);
990 printk(KERN_NOTICE
"svc: server socket destroy delayed\n");
991 /* svsk->sk_server = NULL; */
996 * Make a socket for nfsd and lockd
999 svc_makesock(struct svc_serv
*serv
, int protocol
, unsigned short port
)
1001 struct sockaddr_in sin
;
1003 dprintk("svc: creating socket proto = %d\n", protocol
);
1004 sin
.sin_family
= AF_INET
;
1005 sin
.sin_addr
.s_addr
= INADDR_ANY
;
1006 sin
.sin_port
= htons(port
);
1007 return svc_create_socket(serv
, protocol
, &sin
);