]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
4026fe6773f4c75e2a16b27bb7d7127c25bb1bb2
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/domain.h>
62 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/signalvar.h>
71 #include <sys/sysctl.h>
75 * Primitive routines for operating on sockets and socket buffers
78 u_long sb_max
= SB_MAX
; /* XXX should be static */
80 static u_long sb_efficiency
= 8; /* parameter for sbreserve() */
82 char netcon
[] = "netcon";
85 * Procedures to manipulate state flags of socket
86 * and do appropriate wakeups. Normal sequence from the
87 * active (originating) side is that soisconnecting() is
88 * called during processing of connect() call,
89 * resulting in an eventual call to soisconnected() if/when the
90 * connection is established. When the connection is torn down
91 * soisdisconnecting() is called during processing of disconnect() call,
92 * and soisdisconnected() is called when the connection to the peer
93 * is totally severed. The semantics of these routines are such that
94 * connectionless protocols can call soisconnected() and soisdisconnected()
95 * only, bypassing the in-progress calls when setting up a ``connection''
98 * From the passive side, a socket is created with
99 * two queues of sockets: so_incomp for connections in progress
100 * and so_comp for connections already made and awaiting user acceptance.
101 * As a protocol is preparing incoming connections, it creates a socket
102 * structure queued on so_incomp by calling sonewconn(). When the connection
103 * is established, soisconnected() is called, and transfers the
104 * socket structure to so_comp, making it available to accept().
106 * If a socket is closed with sockets on either
107 * so_incomp or so_comp, these sockets are dropped.
109 * If higher level protocols are implemented in
110 * the kernel, the wakeups done here will sometimes
111 * cause software-interrupt process scheduling.
116 register struct socket
*so
;
119 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
120 so
->so_state
|= SS_ISCONNECTING
;
125 register struct socket
*so
;
126 { register struct kextcb
*kp
;
127 register struct socket
*head
= so
->so_head
;
131 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisconnected
)
132 { if ((*kp
->e_soif
->sf_soisconnected
)(so
, kp
))
138 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
139 so
->so_state
|= SS_ISCONNECTED
;
140 if (head
&& (so
->so_state
& SS_INCOMP
)) {
141 postevent(head
,0,EV_RCONN
);
142 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
144 so
->so_state
&= ~SS_INCOMP
;
145 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
146 so
->so_state
|= SS_COMP
;
148 wakeup((caddr_t
)&head
->so_timeo
);
150 postevent(so
,0,EV_WCONN
);
151 wakeup((caddr_t
)&so
->so_timeo
);
158 soisdisconnecting(so
)
159 register struct socket
*so
;
160 { register struct kextcb
*kp
;
164 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnecting
)
165 { if ((*kp
->e_soif
->sf_soisdisconnecting
)(so
, kp
))
171 so
->so_state
&= ~SS_ISCONNECTING
;
172 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
173 wakeup((caddr_t
)&so
->so_timeo
);
180 register struct socket
*so
;
181 { register struct kextcb
*kp
;
185 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnected
)
186 { if ((*kp
->e_soif
->sf_soisdisconnected
)(so
, kp
))
192 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
193 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
);
194 wakeup((caddr_t
)&so
->so_timeo
);
200 * Return a random connection that hasn't been serviced yet and
201 * is eligible for discard. There is a one in qlen chance that
202 * we will return a null, saying that there are no dropable
203 * requests. In this case, the protocol specific code should drop
204 * the new request. This insures fairness.
206 * This may be used in conjunction with protocol specific queue
207 * congestion routines.
211 register struct socket
*head
;
213 register struct socket
*so
;
214 unsigned int i
, j
, qlen
;
216 static struct timeval old_runtime
;
217 static unsigned int cur_cnt
, old_cnt
;
221 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
223 old_cnt
= cur_cnt
/ i
;
227 so
= TAILQ_FIRST(&head
->so_incomp
);
231 qlen
= head
->so_incqlen
;
232 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
233 rnd
= (314159 * rnd
+ 66329) & 0xffff;
234 j
= ((qlen
+ 1) * rnd
) >> 16;
237 so
= TAILQ_NEXT(so
, so_list
);
244 * When an attempt at a new connection is noted on a socket
245 * which accepts connections, sonewconn is called. If the
246 * connection is possible (subject to space constraints, etc.)
247 * then we allocate a new structure, propoerly linked into the
248 * data structure of the original socket, and return this.
249 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
252 sonewconn(head
, connstatus
)
253 register struct socket
*head
;
256 register struct socket
*so
;
257 register struct kextcb
*kp
;
259 if (head
->so_qlen
> 3 * head
->so_qlimit
/ 2)
260 return ((struct socket
*)0);
261 so
= soalloc(1, head
->so_proto
->pr_domain
->dom_family
, head
->so_type
);
263 return ((struct socket
*)0);
267 { if (kp
->e_soif
&& kp
->e_soif
->sf_sonewconn1
)
268 { if ((*kp
->e_soif
->sf_sonewconn1
)(so
, connstatus
, kp
))
275 so
->so_type
= head
->so_type
;
276 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
277 so
->so_linger
= head
->so_linger
;
278 so
->so_state
= head
->so_state
| SS_NOFDREF
;
279 so
->so_proto
= head
->so_proto
;
280 so
->so_timeo
= head
->so_timeo
;
281 so
->so_pgid
= head
->so_pgid
;
282 so
->so_uid
= head
->so_uid
;
283 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
284 (void) soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
);
286 if (so
->so_proto
->pr_sfilter
.tqh_first
)
287 error
= sfilter_init(so
);
288 if (error
== 0 && (*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
)) {
291 return ((struct socket
*)0);
293 so
->so_proto
->pr_domain
->dom_refs
++;
296 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
297 so
->so_state
|= SS_COMP
;
299 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
300 so
->so_state
|= SS_INCOMP
;
306 wakeup((caddr_t
)&head
->so_timeo
);
307 so
->so_state
|= connstatus
;
309 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
310 TAILQ_INIT(&so
->so_evlist
);
315 * Socantsendmore indicates that no more data will be sent on the
316 * socket; it would normally be applied to a socket when the user
317 * informs the system that no more data is to be sent, by the protocol
318 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
319 * will be received, and will normally be applied to the socket by a
320 * protocol when it detects that the peer will send no more data.
321 * Data queued for reading in the socket may yet be read.
327 { register struct kextcb
*kp
;
331 { if (kp
->e_soif
&& kp
->e_soif
->sf_socantsendmore
)
332 { if ((*kp
->e_soif
->sf_socantsendmore
)(so
, kp
))
339 so
->so_state
|= SS_CANTSENDMORE
;
346 { register struct kextcb
*kp
;
350 { if (kp
->e_soif
&& kp
->e_soif
->sf_socantrcvmore
)
351 { if ((*kp
->e_soif
->sf_socantrcvmore
)(so
, kp
))
358 so
->so_state
|= SS_CANTRCVMORE
;
363 * Wait for data to arrive at/drain from a socket buffer.
370 sb
->sb_flags
|= SB_WAIT
;
371 return (tsleep((caddr_t
)&sb
->sb_cc
,
372 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait",
377 * Lock a sockbuf already known to be locked;
378 * return any error returned from sleep (EINTR).
382 register struct sockbuf
*sb
;
386 while (sb
->sb_flags
& SB_LOCK
) {
387 sb
->sb_flags
|= SB_WANT
;
388 error
= tsleep((caddr_t
)&sb
->sb_flags
,
389 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
|PCATCH
,
394 sb
->sb_flags
|= SB_LOCK
;
399 * Wakeup processes waiting on a socket buffer.
400 * Do asynchronous notification via SIGIO
401 * if the socket has the SS_ASYNC flag set.
405 register struct socket
*so
;
406 register struct sockbuf
*sb
;
408 struct proc
*p
= current_proc();
413 sb
->sb_flags
&= ~SB_SEL
;
414 selwakeup(&sb
->sb_sel
);
416 if (sb
->sb_flags
& SB_WAIT
) {
417 sb
->sb_flags
&= ~SB_WAIT
;
418 wakeup((caddr_t
)&sb
->sb_cc
);
420 if (so
->so_state
& SS_ASYNC
) {
422 gsignal(-so
->so_pgid
, SIGIO
);
423 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
427 if (sb
->sb_flags
& SB_UPCALL
)
428 (*so
->so_upcall
)(so
, so
->so_upcallarg
, M_DONTWAIT
);
432 * Socket buffer (struct sockbuf) utility routines.
434 * Each socket contains two socket buffers: one for sending data and
435 * one for receiving data. Each buffer contains a queue of mbufs,
436 * information about the number of mbufs and amount of data in the
437 * queue, and other fields allowing select() statements and notification
438 * on data availability to be implemented.
440 * Data stored in a socket buffer is maintained as a list of records.
441 * Each record is a list of mbufs chained together with the m_next
442 * field. Records are chained together with the m_nextpkt field. The upper
443 * level routine soreceive() expects the following conventions to be
444 * observed when placing information in the receive buffer:
446 * 1. If the protocol requires each message be preceded by the sender's
447 * name, then a record containing that name must be present before
448 * any associated data (mbuf's must be of type MT_SONAME).
449 * 2. If the protocol supports the exchange of ``access rights'' (really
450 * just additional data associated with the message), and there are
451 * ``rights'' to be received, then a record containing this data
452 * should be present (mbuf's must be of type MT_RIGHTS).
453 * 3. If a name or rights record exists, then it must be followed by
454 * a data record, perhaps of zero length.
456 * Before using a new socket structure it is first necessary to reserve
457 * buffer space to the socket, by calling sbreserve(). This should commit
458 * some of the available buffer space in the system buffer pool for the
459 * socket (currently, it does nothing but enforce limits). The space
460 * should be released by calling sbrelease() when the socket is destroyed.
464 soreserve(so
, sndcc
, rcvcc
)
465 register struct socket
*so
;
468 register struct kextcb
*kp
;
472 { if (kp
->e_soif
&& kp
->e_soif
->sf_soreserve
)
473 { if ((*kp
->e_soif
->sf_soreserve
)(so
, sndcc
, rcvcc
, kp
))
479 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
481 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
483 if (so
->so_rcv
.sb_lowat
== 0)
484 so
->so_rcv
.sb_lowat
= 1;
485 if (so
->so_snd
.sb_lowat
== 0)
486 so
->so_snd
.sb_lowat
= MCLBYTES
;
487 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
488 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
491 selthreadclear(&so
->so_snd
.sb_sel
);
492 sbrelease(&so
->so_snd
);
498 * Allot mbufs to a sockbuf.
499 * Attempt to scale mbmax so that mbcnt doesn't become limiting
500 * if buffering efficiency is near the normal case.
507 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
510 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
511 if (sb
->sb_lowat
> sb
->sb_hiwat
)
512 sb
->sb_lowat
= sb
->sb_hiwat
;
517 * Free mbufs held by a socket, and reserved mbuf space.
519 /* WARNING needs to do selthreadclear() before calling this */
526 sb
->sb_hiwat
= sb
->sb_mbmax
= 0;
528 /* this is getting called with bzeroed sb in sorflush */
530 int oldpri
= splimp();
531 selthreadclear(&sb
->sb_sel
);
538 * Routines to add and remove
539 * data from an mbuf queue.
541 * The routines sbappend() or sbappendrecord() are normally called to
542 * append new mbufs to a socket buffer, after checking that adequate
543 * space is available, comparing the function sbspace() with the amount
544 * of data to be added. sbappendrecord() differs from sbappend() in
545 * that data supplied is treated as the beginning of a new record.
546 * To place a sender's address, optional access rights, and data in a
547 * socket receive buffer, sbappendaddr() should be used. To place
548 * access rights and data in a socket receive buffer, sbappendrights()
549 * should be used. In either case, the new data begins a new record.
550 * Note that unlike sbappend() and sbappendrecord(), these routines check
551 * for the caller that there will be enough space to store the data.
552 * Each fails if there is not enough space, or if it cannot find mbufs
553 * to store additional information in.
555 * Reliable protocols may use the socket send buffer to hold data
556 * awaiting acknowledgement. Data is normally copied from a socket
557 * send buffer in a protocol with m_copy for output to a peer,
558 * and then removing the data from the socket buffer with sbdrop()
559 * or sbdroprecord() when the data is acknowledged by the peer.
563 * Append mbuf chain m to the last record in the
564 * socket buffer sb. The additional space associated
565 * the mbuf chain is recorded in sb. Empty mbufs are
566 * discarded and mbufs are compacted where possible.
572 { register struct kextcb
*kp
;
573 register struct mbuf
*n
;
577 kp
= sotokextcb(sbtoso(sb
));
579 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappend
)
580 { if ((*kp
->e_sout
->su_sbappend
)(sb
, m
, kp
))
590 if (n
->m_flags
& M_EOR
) {
591 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
594 } while (n
->m_next
&& (n
= n
->m_next
));
596 sbcompress(sb
, m
, n
);
602 register struct sockbuf
*sb
;
604 register struct mbuf
*m
;
605 register struct mbuf
*n
= 0;
606 register u_long len
= 0, mbcnt
= 0;
608 for (m
= sb
->sb_mb
; m
; m
= n
) {
610 for (; m
; m
= m
->m_next
) {
613 if (m
->m_flags
& M_EXT
) /*XXX*/ /* pretty sure this is bogus */
614 mbcnt
+= m
->m_ext
.ext_size
;
616 panic("sbcheck nextpkt");
618 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
619 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
620 mbcnt
, sb
->sb_mbcnt
);
627 * As above, except the mbuf chain
628 * begins a new record.
631 sbappendrecord(sb
, m0
)
632 register struct sockbuf
*sb
;
633 register struct mbuf
*m0
;
635 register struct mbuf
*m
;
636 register struct kextcb
*kp
;
641 kp
= sotokextcb(sbtoso(sb
));
643 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendrecord
)
644 { if ((*kp
->e_sout
->su_sbappendrecord
)(sb
, m0
, kp
))
655 * Put the first mbuf on the queue.
656 * Note this permits zero length records.
665 if (m
&& (m0
->m_flags
& M_EOR
)) {
666 m0
->m_flags
&= ~M_EOR
;
669 sbcompress(sb
, m
, m0
);
673 * As above except that OOB data
674 * is inserted at the beginning of the sockbuf,
675 * but after any other OOB data.
679 register struct sockbuf
*sb
;
680 register struct mbuf
*m0
;
682 register struct mbuf
*m
;
683 register struct mbuf
**mp
;
684 register struct kextcb
*kp
;
689 kp
= sotokextcb(sbtoso(sb
));
691 { if (kp
->e_sout
&& kp
->e_sout
->su_sbinsertoob
)
692 { if ((*kp
->e_sout
->su_sbinsertoob
)(sb
, m0
, kp
))
698 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
704 continue; /* WANT next train */
709 goto again
; /* inspect THIS train further */
714 * Put the first mbuf on the queue.
715 * Note this permits zero length records.
722 if (m
&& (m0
->m_flags
& M_EOR
)) {
723 m0
->m_flags
&= ~M_EOR
;
726 sbcompress(sb
, m
, m0
);
730 * Append address and data, and optionally, control (ancillary) data
731 * to the receive queue of a socket. If present,
732 * m0 must include a packet header with total length.
733 * Returns 0 if no space in sockbuf or insufficient mbufs.
736 sbappendaddr(sb
, asa
, m0
, control
)
737 register struct sockbuf
*sb
;
738 struct sockaddr
*asa
;
739 struct mbuf
*m0
, *control
;
741 register struct mbuf
*m
, *n
;
742 int space
= asa
->sa_len
;
743 register struct kextcb
*kp
;
745 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
746 panic("sbappendaddr");
748 kp
= sotokextcb(sbtoso(sb
));
750 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendaddr
)
751 { if ((*kp
->e_sout
->su_sbappendaddr
)(sb
, asa
, m0
, control
, kp
))
758 space
+= m0
->m_pkthdr
.len
;
759 for (n
= control
; n
; n
= n
->m_next
) {
761 if (n
->m_next
== 0) /* keep pointer to last control buf */
764 if (space
> sbspace(sb
))
766 if (asa
->sa_len
> MLEN
)
768 MGET(m
, M_DONTWAIT
, MT_SONAME
);
771 m
->m_len
= asa
->sa_len
;
772 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
774 n
->m_next
= m0
; /* concatenate data to control */
778 for (n
= m
; n
; n
= n
->m_next
)
787 postevent(0,sb
,EV_RWBYTES
);
792 sbappendcontrol(sb
, m0
, control
)
794 struct mbuf
*control
, *m0
;
796 register struct mbuf
*m
, *n
;
798 register struct kextcb
*kp
;
801 panic("sbappendcontrol");
803 kp
= sotokextcb(sbtoso(sb
));
805 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendcontrol
)
806 { if ((*kp
->e_sout
->su_sbappendcontrol
)(sb
, m0
, control
, kp
))
812 for (m
= control
; ; m
= m
->m_next
) {
817 n
= m
; /* save pointer to last control buffer */
818 for (m
= m0
; m
; m
= m
->m_next
)
820 if (space
> sbspace(sb
))
822 n
->m_next
= m0
; /* concatenate data to control */
823 for (m
= control
; m
; m
= m
->m_next
)
829 n
->m_nextpkt
= control
;
832 postevent(0,sb
,EV_RWBYTES
);
837 * Compress mbuf chain m into the socket
838 * buffer sb following mbuf n. If n
839 * is null, the buffer is presumed empty.
843 register struct sockbuf
*sb
;
844 register struct mbuf
*m
, *n
;
846 register int eor
= 0;
847 register struct mbuf
*o
;
850 eor
|= m
->m_flags
& M_EOR
;
853 (((o
= m
->m_next
) || (o
= n
)) &&
854 o
->m_type
== m
->m_type
))) {
858 if (n
&& (n
->m_flags
& (M_EXT
| M_EOR
)) == 0 &&
859 (n
->m_data
+ n
->m_len
+ m
->m_len
) < &n
->m_dat
[MLEN
] &&
860 n
->m_type
== m
->m_type
) {
861 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
863 n
->m_len
+= m
->m_len
;
864 sb
->sb_cc
+= m
->m_len
;
874 m
->m_flags
&= ~M_EOR
;
882 printf("semi-panic: sbcompress\n");
884 postevent(0,sb
, EV_RWBYTES
);
888 * Free all mbufs in a sockbuf.
889 * Check that all resources are reclaimed.
893 register struct sockbuf
*sb
;
895 register struct kextcb
*kp
;
897 kp
= sotokextcb(sbtoso(sb
));
899 { if (kp
->e_sout
&& kp
->e_sout
->su_sbflush
)
900 { if ((*kp
->e_sout
->su_sbflush
)(sb
, kp
))
906 if (sb
->sb_flags
& SB_LOCK
)
907 panic("sbflush: locked");
908 while (sb
->sb_mbcnt
&& sb
->sb_cc
)
909 sbdrop(sb
, (int)sb
->sb_cc
);
910 if (sb
->sb_cc
|| sb
->sb_mb
|| sb
->sb_mbcnt
)
911 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb
->sb_cc
, (void *)sb
->sb_mb
, sb
->sb_mbcnt
);
912 postevent(0, sb
, EV_RWBYTES
);
916 * Drop data from (the front of) a sockbuf.
920 register struct sockbuf
*sb
;
923 register struct mbuf
*m
, *mn
;
925 register struct kextcb
*kp
;
927 kp
= sotokextcb(sbtoso(sb
));
929 { if (kp
->e_sout
&& kp
->e_sout
->su_sbdrop
)
930 { if ((*kp
->e_sout
->su_sbdrop
)(sb
, len
, kp
))
936 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
945 if (m
->m_len
> len
) {
956 while (m
&& m
->m_len
== 0) {
966 postevent(0, sb
, EV_RWBYTES
);
970 * Drop a record off the front of a sockbuf
971 * and move the next record to the front.
975 register struct sockbuf
*sb
;
977 register struct mbuf
*m
, *mn
;
978 register struct kextcb
*kp
;
980 kp
= sotokextcb(sbtoso(sb
));
982 { if (kp
->e_sout
&& kp
->e_sout
->su_sbdroprecord
)
983 { if ((*kp
->e_sout
->su_sbdroprecord
)(sb
, kp
))
991 sb
->sb_mb
= m
->m_nextpkt
;
997 postevent(0, sb
, EV_RWBYTES
);
1001 * Create a "control" mbuf containing the specified data
1002 * with the specified type for presentation on a socket buffer.
1005 sbcreatecontrol(p
, size
, type
, level
)
1010 register struct cmsghdr
*cp
;
1013 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1014 return ((struct mbuf
*) NULL
);
1015 cp
= mtod(m
, struct cmsghdr
*);
1016 /* XXX check size? */
1017 (void)memcpy(CMSG_DATA(cp
), p
, size
);
1018 size
+= sizeof(*cp
);
1020 cp
->cmsg_len
= size
;
1021 cp
->cmsg_level
= level
;
1022 cp
->cmsg_type
= type
;
1027 * Some routines that return EOPNOTSUPP for entry points that are not
1028 * supported by a protocol. Fill in as needed.
1031 pru_abort_notsupp(struct socket
*so
)
1038 pru_accept_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1044 pru_attach_notsupp(struct socket
*so
, int proto
, struct proc
*p
)
1050 pru_bind_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1056 pru_connect_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1062 pru_connect2_notsupp(struct socket
*so1
, struct socket
*so2
)
1068 pru_control_notsupp(struct socket
*so
, u_long cmd
, caddr_t data
,
1069 struct ifnet
*ifp
, struct proc
*p
)
1075 pru_detach_notsupp(struct socket
*so
)
1081 pru_disconnect_notsupp(struct socket
*so
)
1087 pru_listen_notsupp(struct socket
*so
, struct proc
*p
)
1093 pru_peeraddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1099 pru_rcvd_notsupp(struct socket
*so
, int flags
)
1105 pru_rcvoob_notsupp(struct socket
*so
, struct mbuf
*m
, int flags
)
1111 pru_send_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
1112 struct sockaddr
*addr
, struct mbuf
*control
,
1121 * This isn't really a ``null'' operation, but it's the default one
1122 * and doesn't do anything destructive.
1125 pru_sense_null(struct socket
*so
, struct stat
*sb
)
1127 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1132 int pru_sosend_notsupp(struct socket
*so
, struct sockaddr
*addr
,
1133 struct uio
*uio
, struct mbuf
*top
,
1134 struct mbuf
*control
, int flags
)
1140 int pru_soreceive_notsupp(struct socket
*so
,
1141 struct sockaddr
**paddr
,
1142 struct uio
*uio
, struct mbuf
**mp0
,
1143 struct mbuf
**controlp
, int *flagsp
)
1150 pru_shutdown_notsupp(struct socket
*so
)
1156 pru_sockaddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1161 int pru_sosend(struct socket
*so
, struct sockaddr
*addr
,
1162 struct uio
*uio
, struct mbuf
*top
,
1163 struct mbuf
*control
, int flags
)
1168 int pru_soreceive(struct socket
*so
,
1169 struct sockaddr
**paddr
,
1170 struct uio
*uio
, struct mbuf
**mp0
,
1171 struct mbuf
**controlp
, int *flagsp
)
1177 int pru_sopoll_notsupp(struct socket
*so
, int events
,
1186 * Do we need to notify the other side when I/O is possible?
1190 sb_notify(struct sockbuf
*sb
)
1192 return ((sb
->sb_flags
& (SB_WAIT
|SB_SEL
|SB_ASYNC
|SB_UPCALL
)) != 0);
1196 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1197 * This is problematical if the fields are unsigned, as the space might
1198 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1199 * overflow and return 0. Should use "lmin" but it doesn't exist now.
1202 sbspace(struct sockbuf
*sb
)
1204 return ((long) imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
1205 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
)));
1208 /* do we have to send all at once on a socket? */
1210 sosendallatonce(struct socket
*so
)
1212 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1215 /* can we read something from so? */
1217 soreadable(struct socket
*so
)
1219 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1220 (so
->so_state
& SS_CANTRCVMORE
) ||
1221 so
->so_comp
.tqh_first
|| so
->so_error
);
1224 /* can we write something to so? */
1227 sowriteable(struct socket
*so
)
1229 return ((sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
&&
1230 ((so
->so_state
&SS_ISCONNECTED
) ||
1231 (so
->so_proto
->pr_flags
&PR_CONNREQUIRED
)==0)) ||
1232 (so
->so_state
& SS_CANTSENDMORE
) ||
1236 /* adjust counters in sb reflecting allocation of m */
1239 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
1241 sb
->sb_cc
+= m
->m_len
;
1242 sb
->sb_mbcnt
+= MSIZE
;
1243 if (m
->m_flags
& M_EXT
)
1244 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
1247 /* adjust counters in sb reflecting freeing of m */
1249 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
1251 sb
->sb_cc
-= m
->m_len
;
1252 sb
->sb_mbcnt
-= MSIZE
;
1253 if (m
->m_flags
& M_EXT
)
1254 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
1258 * Set lock on sockbuf sb; sleep if lock is already held.
1259 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1260 * Returns error without lock if sleep is interrupted.
1263 sblock(struct sockbuf
*sb
, int wf
)
1265 return(sb
->sb_flags
& SB_LOCK
?
1266 ((wf
== M_WAIT
) ? sb_lock(sb
) : EWOULDBLOCK
) :
1267 (sb
->sb_flags
|= SB_LOCK
), 0);
1270 /* release lock on sockbuf sb */
1272 sbunlock(struct sockbuf
*sb
)
1274 sb
->sb_flags
&= ~SB_LOCK
;
1275 if (sb
->sb_flags
& SB_WANT
) {
1276 sb
->sb_flags
&= ~SB_WANT
;
1277 wakeup((caddr_t
)&(sb
)->sb_flags
);
1282 sorwakeup(struct socket
* so
)
1284 if (sb_notify(&so
->so_rcv
))
1285 sowakeup(so
, &so
->so_rcv
);
1289 sowwakeup(struct socket
* so
)
1291 if (sb_notify(&so
->so_snd
))
1292 sowakeup(so
, &so
->so_snd
);
1296 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1299 dup_sockaddr(sa
, canwait
)
1300 struct sockaddr
*sa
;
1303 struct sockaddr
*sa2
;
1305 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1306 canwait
? M_WAITOK
: M_NOWAIT
);
1308 bcopy(sa
, sa2
, sa
->sa_len
);
1313 * Create an external-format (``xsocket'') structure using the information
1314 * in the kernel-format socket structure pointed to by so. This is done
1315 * to reduce the spew of irrelevant information over this interface,
1316 * to isolate user code from changes in the kernel structure, and
1317 * potentially to provide information-hiding if we decide that
1318 * some of this information should be hidden from users.
1321 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1323 xso
->xso_len
= sizeof *xso
;
1325 xso
->so_type
= so
->so_type
;
1326 xso
->so_options
= so
->so_options
;
1327 xso
->so_linger
= so
->so_linger
;
1328 xso
->so_state
= so
->so_state
;
1329 xso
->so_pcb
= so
->so_pcb
;
1330 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1331 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1332 xso
->so_qlen
= so
->so_qlen
;
1333 xso
->so_incqlen
= so
->so_incqlen
;
1334 xso
->so_qlimit
= so
->so_qlimit
;
1335 xso
->so_timeo
= so
->so_timeo
;
1336 xso
->so_error
= so
->so_error
;
1337 xso
->so_pgid
= so
->so_pgid
;
1338 xso
->so_oobmark
= so
->so_oobmark
;
1339 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1340 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1341 xso
->so_uid
= so
->so_uid
;
1345 * This does the same for sockbufs. Note that the xsockbuf structure,
1346 * since it is always embedded in a socket, does not include a self
1347 * pointer nor a length. We make this entry point public in case
1348 * some other mechanism needs it.
1351 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1353 xsb
->sb_cc
= sb
->sb_cc
;
1354 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1355 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1356 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1357 xsb
->sb_lowat
= sb
->sb_lowat
;
1358 xsb
->sb_flags
= sb
->sb_flags
;
1359 xsb
->sb_timeo
= sb
->sb_timeo
;
1363 * Here is the definition of some of the basic objects in the kern.ipc
1364 * branch of the MIB.
1368 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
, 0, "IPC");
1370 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1372 SYSCTL_INT(_kern
, KERN_DUMMY
, dummy
, CTLFLAG_RW
, &dummy
, 0, "");
1374 SYSCTL_INT(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLFLAG_RW
, &sb_max
, 0, "");
1375 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
, &maxsockets
, 0, "");
1376 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
,
1377 &sb_efficiency
, 0, "");
1378 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
, &nmbclusters
, 0, "");