]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
735acaf4c075dc2232bd1043607c972ca049fa9b
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
26 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
28 * Copyright (c) 1982, 1986, 1988, 1990, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
60 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/domain.h>
66 #include <sys/kernel.h>
68 #include <sys/malloc.h>
70 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/signalvar.h>
75 #include <sys/sysctl.h>
78 #include <sys/kdebug.h>
80 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
81 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
85 * Primitive routines for operating on sockets and socket buffers
88 u_long sb_max
= SB_MAX
; /* XXX should be static */
90 static u_long sb_efficiency
= 8; /* parameter for sbreserve() */
93 * Procedures to manipulate state flags of socket
94 * and do appropriate wakeups. Normal sequence from the
95 * active (originating) side is that soisconnecting() is
96 * called during processing of connect() call,
97 * resulting in an eventual call to soisconnected() if/when the
98 * connection is established. When the connection is torn down
99 * soisdisconnecting() is called during processing of disconnect() call,
100 * and soisdisconnected() is called when the connection to the peer
101 * is totally severed. The semantics of these routines are such that
102 * connectionless protocols can call soisconnected() and soisdisconnected()
103 * only, bypassing the in-progress calls when setting up a ``connection''
106 * From the passive side, a socket is created with
107 * two queues of sockets: so_incomp for connections in progress
108 * and so_comp for connections already made and awaiting user acceptance.
109 * As a protocol is preparing incoming connections, it creates a socket
110 * structure queued on so_incomp by calling sonewconn(). When the connection
111 * is established, soisconnected() is called, and transfers the
112 * socket structure to so_comp, making it available to accept().
114 * If a socket is closed with sockets on either
115 * so_incomp or so_comp, these sockets are dropped.
117 * If higher level protocols are implemented in
118 * the kernel, the wakeups done here will sometimes
119 * cause software-interrupt process scheduling.
124 register struct socket
*so
;
127 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
128 so
->so_state
|= SS_ISCONNECTING
;
135 struct socket
*head
= so
->so_head
;
140 if (kp
->e_soif
&& kp
->e_soif
->sf_soisconnected
) {
141 if ((*kp
->e_soif
->sf_soisconnected
)(so
, kp
))
147 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
148 so
->so_state
|= SS_ISCONNECTED
;
149 if (head
&& (so
->so_state
& SS_INCOMP
)) {
150 postevent(head
,0,EV_RCONN
);
151 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
153 so
->so_state
&= ~SS_INCOMP
;
154 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
155 so
->so_state
|= SS_COMP
;
157 wakeup_one(&head
->so_timeo
);
159 postevent(so
,0,EV_WCONN
);
160 wakeup((caddr_t
)&so
->so_timeo
);
167 soisdisconnecting(so
)
168 register struct socket
*so
;
170 register struct kextcb
*kp
;
174 if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnecting
) {
175 if ((*kp
->e_soif
->sf_soisdisconnecting
)(so
, kp
))
181 so
->so_state
&= ~SS_ISCONNECTING
;
182 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
183 wakeup((caddr_t
)&so
->so_timeo
);
190 register struct socket
*so
;
192 register struct kextcb
*kp
;
196 if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnected
) {
197 if ((*kp
->e_soif
->sf_soisdisconnected
)(so
, kp
))
203 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
204 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
205 wakeup((caddr_t
)&so
->so_timeo
);
211 * Return a random connection that hasn't been serviced yet and
212 * is eligible for discard. There is a one in qlen chance that
213 * we will return a null, saying that there are no dropable
214 * requests. In this case, the protocol specific code should drop
215 * the new request. This insures fairness.
217 * This may be used in conjunction with protocol specific queue
218 * congestion routines.
222 register struct socket
*head
;
224 register struct socket
*so
;
225 unsigned int i
, j
, qlen
;
227 static struct timeval old_runtime
;
228 static unsigned int cur_cnt
, old_cnt
;
232 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
234 old_cnt
= cur_cnt
/ i
;
238 so
= TAILQ_FIRST(&head
->so_incomp
);
242 qlen
= head
->so_incqlen
;
243 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
244 rnd
= (314159 * rnd
+ 66329) & 0xffff;
245 j
= ((qlen
+ 1) * rnd
) >> 16;
248 so
= TAILQ_NEXT(so
, so_list
);
255 * When an attempt at a new connection is noted on a socket
256 * which accepts connections, sonewconn is called. If the
257 * connection is possible (subject to space constraints, etc.)
258 * then we allocate a new structure, propoerly linked into the
259 * data structure of the original socket, and return this.
260 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
263 sonewconn(head
, connstatus
)
264 register struct socket
*head
;
268 register struct socket
*so
;
269 register struct kextcb
*kp
;
271 if (head
->so_qlen
> 3 * head
->so_qlimit
/ 2)
272 return ((struct socket
*)0);
273 so
= soalloc(1, head
->so_proto
->pr_domain
->dom_family
, head
->so_type
);
275 return ((struct socket
*)0);
276 /* check if head was closed during the soalloc */
277 if (head
->so_proto
== NULL
) {
279 return ((struct socket
*)0);
283 so
->so_type
= head
->so_type
;
284 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
285 so
->so_linger
= head
->so_linger
;
286 so
->so_state
= head
->so_state
| SS_NOFDREF
;
287 so
->so_proto
= head
->so_proto
;
288 so
->so_timeo
= head
->so_timeo
;
289 so
->so_pgid
= head
->so_pgid
;
290 so
->so_uid
= head
->so_uid
;
292 /* Attach socket filters for this protocol */
293 if (so
->so_proto
->pr_sfilter
.tqh_first
)
294 error
= sfilter_init(so
);
297 return ((struct socket
*)0);
300 /* Call socket filters' sonewconn1 function if set */
303 if (kp
->e_soif
&& kp
->e_soif
->sf_sonewconn
) {
304 error
= (int)(*kp
->e_soif
->sf_sonewconn
)(so
, connstatus
, kp
);
305 if (error
== EJUSTRETURN
) {
307 } else if (error
!= 0) {
315 if (soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
) ||
316 (*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
)) {
319 return ((struct socket
*)0);
322 so
->so_proto
->pr_domain
->dom_refs
++;
326 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
327 so
->so_state
|= SS_COMP
;
329 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
330 so
->so_state
|= SS_INCOMP
;
336 wakeup((caddr_t
)&head
->so_timeo
);
337 so
->so_state
|= connstatus
;
340 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
341 TAILQ_INIT(&so
->so_evlist
);
347 * Socantsendmore indicates that no more data will be sent on the
348 * socket; it would normally be applied to a socket when the user
349 * informs the system that no more data is to be sent, by the protocol
350 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
351 * will be received, and will normally be applied to the socket by a
352 * protocol when it detects that the peer will send no more data.
353 * Data queued for reading in the socket may yet be read.
360 register struct kextcb
*kp
;
364 if (kp
->e_soif
&& kp
->e_soif
->sf_socantsendmore
) {
365 if ((*kp
->e_soif
->sf_socantsendmore
)(so
, kp
))
372 so
->so_state
|= SS_CANTSENDMORE
;
380 register struct kextcb
*kp
;
384 if (kp
->e_soif
&& kp
->e_soif
->sf_socantrcvmore
) {
385 if ((*kp
->e_soif
->sf_socantrcvmore
)(so
, kp
))
392 so
->so_state
|= SS_CANTRCVMORE
;
397 * Wait for data to arrive at/drain from a socket buffer.
404 sb
->sb_flags
|= SB_WAIT
;
405 return (tsleep((caddr_t
)&sb
->sb_cc
,
406 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait",
411 * Lock a sockbuf already known to be locked;
412 * return any error returned from sleep (EINTR).
416 register struct sockbuf
*sb
;
420 while (sb
->sb_flags
& SB_LOCK
) {
421 sb
->sb_flags
|= SB_WANT
;
422 error
= tsleep((caddr_t
)&sb
->sb_flags
,
423 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
|PCATCH
,
428 sb
->sb_flags
|= SB_LOCK
;
433 * Wakeup processes waiting on a socket buffer.
434 * Do asynchronous notification via SIGIO
435 * if the socket has the SS_ASYNC flag set.
439 register struct socket
*so
;
440 register struct sockbuf
*sb
;
442 struct proc
*p
= current_proc();
443 /* We clear the flag before calling selwakeup. */
444 /* BSD calls selwakeup then sets the flag */
445 sb
->sb_flags
&= ~SB_SEL
;
446 selwakeup(&sb
->sb_sel
);
447 if (sb
->sb_flags
& SB_WAIT
) {
448 sb
->sb_flags
&= ~SB_WAIT
;
449 wakeup((caddr_t
)&sb
->sb_cc
);
451 if (so
->so_state
& SS_ASYNC
) {
453 gsignal(-so
->so_pgid
, SIGIO
);
454 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
457 if (sb
->sb_flags
& SB_UPCALL
)
458 (*so
->so_upcall
)(so
, so
->so_upcallarg
, M_DONTWAIT
);
462 * Socket buffer (struct sockbuf) utility routines.
464 * Each socket contains two socket buffers: one for sending data and
465 * one for receiving data. Each buffer contains a queue of mbufs,
466 * information about the number of mbufs and amount of data in the
467 * queue, and other fields allowing select() statements and notification
468 * on data availability to be implemented.
470 * Data stored in a socket buffer is maintained as a list of records.
471 * Each record is a list of mbufs chained together with the m_next
472 * field. Records are chained together with the m_nextpkt field. The upper
473 * level routine soreceive() expects the following conventions to be
474 * observed when placing information in the receive buffer:
476 * 1. If the protocol requires each message be preceded by the sender's
477 * name, then a record containing that name must be present before
478 * any associated data (mbuf's must be of type MT_SONAME).
479 * 2. If the protocol supports the exchange of ``access rights'' (really
480 * just additional data associated with the message), and there are
481 * ``rights'' to be received, then a record containing this data
482 * should be present (mbuf's must be of type MT_RIGHTS).
483 * 3. If a name or rights record exists, then it must be followed by
484 * a data record, perhaps of zero length.
486 * Before using a new socket structure it is first necessary to reserve
487 * buffer space to the socket, by calling sbreserve(). This should commit
488 * some of the available buffer space in the system buffer pool for the
489 * socket (currently, it does nothing but enforce limits). The space
490 * should be released by calling sbrelease() when the socket is destroyed.
494 soreserve(so
, sndcc
, rcvcc
)
495 register struct socket
*so
;
498 register struct kextcb
*kp
;
502 if (kp
->e_soif
&& kp
->e_soif
->sf_soreserve
) {
503 if ((*kp
->e_soif
->sf_soreserve
)(so
, sndcc
, rcvcc
, kp
))
509 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
511 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
513 if (so
->so_rcv
.sb_lowat
== 0)
514 so
->so_rcv
.sb_lowat
= 1;
515 if (so
->so_snd
.sb_lowat
== 0)
516 so
->so_snd
.sb_lowat
= MCLBYTES
;
517 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
518 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
522 selthreadclear(&so
->so_snd
.sb_sel
);
524 sbrelease(&so
->so_snd
);
530 * Allot mbufs to a sockbuf.
531 * Attempt to scale mbmax so that mbcnt doesn't become limiting
532 * if buffering efficiency is near the normal case.
539 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
542 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
543 if (sb
->sb_lowat
> sb
->sb_hiwat
)
544 sb
->sb_lowat
= sb
->sb_hiwat
;
549 * Free mbufs held by a socket, and reserved mbuf space.
551 /* WARNING needs to do selthreadclear() before calling this */
564 * Routines to add and remove
565 * data from an mbuf queue.
567 * The routines sbappend() or sbappendrecord() are normally called to
568 * append new mbufs to a socket buffer, after checking that adequate
569 * space is available, comparing the function sbspace() with the amount
570 * of data to be added. sbappendrecord() differs from sbappend() in
571 * that data supplied is treated as the beginning of a new record.
572 * To place a sender's address, optional access rights, and data in a
573 * socket receive buffer, sbappendaddr() should be used. To place
574 * access rights and data in a socket receive buffer, sbappendrights()
575 * should be used. In either case, the new data begins a new record.
576 * Note that unlike sbappend() and sbappendrecord(), these routines check
577 * for the caller that there will be enough space to store the data.
578 * Each fails if there is not enough space, or if it cannot find mbufs
579 * to store additional information in.
581 * Reliable protocols may use the socket send buffer to hold data
582 * awaiting acknowledgement. Data is normally copied from a socket
583 * send buffer in a protocol with m_copy for output to a peer,
584 * and then removing the data from the socket buffer with sbdrop()
585 * or sbdroprecord() when the data is acknowledged by the peer.
589 * Append mbuf chain m to the last record in the
590 * socket buffer sb. The additional space associated
591 * the mbuf chain is recorded in sb. Empty mbufs are
592 * discarded and mbufs are compacted where possible.
600 register struct mbuf
*n
;
603 KERNEL_DEBUG((DBG_FNC_SBAPPEND
| DBG_FUNC_START
), sb
, m
->m_len
, 0, 0, 0);
607 kp
= sotokextcb(sbtoso(sb
));
609 if (kp
->e_sout
&& kp
->e_sout
->su_sbappend
) {
610 if ((*kp
->e_sout
->su_sbappend
)(sb
, m
, kp
))
620 if (n
->m_flags
& M_EOR
) {
621 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
624 } while (n
->m_next
&& (n
= n
->m_next
));
626 sbcompress(sb
, m
, n
);
628 KERNEL_DEBUG((DBG_FNC_SBAPPEND
| DBG_FUNC_END
), sb
, sb
->sb_cc
, 0, 0, 0);
634 register struct sockbuf
*sb
;
636 register struct mbuf
*m
;
637 register struct mbuf
*n
= 0;
638 register u_long len
= 0, mbcnt
= 0;
640 for (m
= sb
->sb_mb
; m
; m
= n
) {
642 for (; m
; m
= m
->m_next
) {
645 if (m
->m_flags
& M_EXT
) /*XXX*/ /* pretty sure this is bogus */
646 mbcnt
+= m
->m_ext
.ext_size
;
650 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
651 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
652 mbcnt
, sb
->sb_mbcnt
);
656 if (len
!= sb
->sb_cc
)
657 printf("sbcheck len %ld != sb_cc %ld\n", len
, sb
->sb_cc
);
658 if (mbcnt
!= sb
->sb_mbcnt
)
659 printf("sbcheck mbcnt %ld != sb_mbcnt %ld\n", mbcnt
, sb
->sb_mbcnt
);
665 * As above, except the mbuf chain
666 * begins a new record.
669 sbappendrecord(sb
, m0
)
670 register struct sockbuf
*sb
;
671 register struct mbuf
*m0
;
673 register struct mbuf
*m
;
674 register struct kextcb
*kp
;
679 kp
= sotokextcb(sbtoso(sb
));
681 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendrecord
)
682 { if ((*kp
->e_sout
->su_sbappendrecord
)(sb
, m0
, kp
))
693 * Put the first mbuf on the queue.
694 * Note this permits zero length records.
703 if (m
&& (m0
->m_flags
& M_EOR
)) {
704 m0
->m_flags
&= ~M_EOR
;
707 sbcompress(sb
, m
, m0
);
711 * As above except that OOB data
712 * is inserted at the beginning of the sockbuf,
713 * but after any other OOB data.
717 register struct sockbuf
*sb
;
718 register struct mbuf
*m0
;
720 register struct mbuf
*m
;
721 register struct mbuf
**mp
;
722 register struct kextcb
*kp
;
727 kp
= sotokextcb(sbtoso(sb
));
729 { if (kp
->e_sout
&& kp
->e_sout
->su_sbinsertoob
)
730 { if ((*kp
->e_sout
->su_sbinsertoob
)(sb
, m0
, kp
))
736 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
742 continue; /* WANT next train */
747 goto again
; /* inspect THIS train further */
752 * Put the first mbuf on the queue.
753 * Note this permits zero length records.
760 if (m
&& (m0
->m_flags
& M_EOR
)) {
761 m0
->m_flags
&= ~M_EOR
;
764 sbcompress(sb
, m
, m0
);
768 * Append address and data, and optionally, control (ancillary) data
769 * to the receive queue of a socket. If present,
770 * m0 must include a packet header with total length.
771 * Returns 0 if no space in sockbuf or insufficient mbufs.
774 sbappendaddr(sb
, asa
, m0
, control
)
775 register struct sockbuf
*sb
;
776 struct sockaddr
*asa
;
777 struct mbuf
*m0
, *control
;
779 register struct mbuf
*m
, *n
;
780 int space
= asa
->sa_len
;
781 register struct kextcb
*kp
;
783 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
784 panic("sbappendaddr");
786 kp
= sotokextcb(sbtoso(sb
));
788 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendaddr
)
789 { if ((*kp
->e_sout
->su_sbappendaddr
)(sb
, asa
, m0
, control
, kp
))
796 space
+= m0
->m_pkthdr
.len
;
797 for (n
= control
; n
; n
= n
->m_next
) {
799 if (n
->m_next
== 0) /* keep pointer to last control buf */
802 if (space
> sbspace(sb
))
804 if (asa
->sa_len
> MLEN
)
806 MGET(m
, M_DONTWAIT
, MT_SONAME
);
809 m
->m_len
= asa
->sa_len
;
810 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
812 n
->m_next
= m0
; /* concatenate data to control */
816 for (n
= m
; n
; n
= n
->m_next
)
825 postevent(0,sb
,EV_RWBYTES
);
830 sbappendcontrol(sb
, m0
, control
)
832 struct mbuf
*control
, *m0
;
834 register struct mbuf
*m
, *n
;
836 register struct kextcb
*kp
;
839 panic("sbappendcontrol");
841 kp
= sotokextcb(sbtoso(sb
));
843 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendcontrol
)
844 { if ((*kp
->e_sout
->su_sbappendcontrol
)(sb
, m0
, control
, kp
))
850 for (m
= control
; ; m
= m
->m_next
) {
855 n
= m
; /* save pointer to last control buffer */
856 for (m
= m0
; m
; m
= m
->m_next
)
858 if (space
> sbspace(sb
))
860 n
->m_next
= m0
; /* concatenate data to control */
861 for (m
= control
; m
; m
= m
->m_next
)
867 n
->m_nextpkt
= control
;
870 postevent(0,sb
,EV_RWBYTES
);
875 * Compress mbuf chain m into the socket
876 * buffer sb following mbuf n. If n
877 * is null, the buffer is presumed empty.
881 register struct sockbuf
*sb
;
882 register struct mbuf
*m
, *n
;
884 register int eor
= 0;
885 register struct mbuf
*o
;
888 eor
|= m
->m_flags
& M_EOR
;
891 (((o
= m
->m_next
) || (o
= n
)) &&
892 o
->m_type
== m
->m_type
))) {
896 if (n
&& (n
->m_flags
& M_EOR
) == 0 &&
900 m
->m_len
<= MCLBYTES
/ 4 && /* XXX: Don't copy too much */
901 m
->m_len
<= M_TRAILINGSPACE(n
) &&
902 n
->m_type
== m
->m_type
) {
903 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
905 n
->m_len
+= m
->m_len
;
906 sb
->sb_cc
+= m
->m_len
;
916 m
->m_flags
&= ~M_EOR
;
924 printf("semi-panic: sbcompress\n");
926 postevent(0,sb
, EV_RWBYTES
);
930 * Free all mbufs in a sockbuf.
931 * Check that all resources are reclaimed.
935 register struct sockbuf
*sb
;
937 register struct kextcb
*kp
;
939 kp
= sotokextcb(sbtoso(sb
));
941 if (kp
->e_sout
&& kp
->e_sout
->su_sbflush
) {
942 if ((*kp
->e_sout
->su_sbflush
)(sb
, kp
))
948 if (sb
->sb_flags
& SB_LOCK
)
950 while (sb
->sb_mbcnt
) {
952 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
953 * we would loop forever. Panic instead.
955 if (!sb
->sb_cc
&& (sb
->sb_mb
== NULL
|| sb
->sb_mb
->m_len
))
957 sbdrop(sb
, (int)sb
->sb_cc
);
959 if (sb
->sb_cc
|| sb
->sb_mb
|| sb
->sb_mbcnt
)
960 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb
->sb_cc
, (void *)sb
->sb_mb
, sb
->sb_mbcnt
);
961 postevent(0, sb
, EV_RWBYTES
);
965 * Drop data from (the front of) a sockbuf.
966 * use m_freem_list to free the mbuf structures
967 * under a single lock... this is done by pruning
968 * the top of the tree from the body by keeping track
969 * of where we get to in the tree and then zeroing the
970 * two pertinent pointers m_nextpkt and m_next
971 * the socket buffer is then updated to point at the new
972 * top of the tree and the pruned area is released via
977 register struct sockbuf
*sb
;
980 register struct mbuf
*m
, *free_list
, *ml
;
981 struct mbuf
*next
, *last
;
982 register struct kextcb
*kp
;
984 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_START
), sb
, len
, 0, 0, 0);
986 kp
= sotokextcb(sbtoso(sb
));
988 if (kp
->e_sout
&& kp
->e_sout
->su_sbdrop
) {
989 if ((*kp
->e_sout
->su_sbdrop
)(sb
, len
, kp
))
994 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
995 free_list
= last
= m
;
996 ml
= (struct mbuf
*)0;
1001 /* temporarily replacing this panic with printf because
1002 * it occurs occasionally when closing a socket when there
1003 * is no harm in ignoring it. This problem will be investigated
1006 /* panic("sbdrop"); */
1007 printf("sbdrop - count not zero\n");
1009 /* zero the counts. if we have no mbufs, we have no data (PR-2986815) */
1015 next
= m
->m_nextpkt
;
1018 if (m
->m_len
> len
) {
1030 while (m
&& m
->m_len
== 0) {
1037 ml
->m_next
= (struct mbuf
*)0;
1038 last
->m_nextpkt
= (struct mbuf
*)0;
1039 m_freem_list(free_list
);
1043 m
->m_nextpkt
= next
;
1047 postevent(0, sb
, EV_RWBYTES
);
1049 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_END
), sb
, 0, 0, 0, 0);
1053 * Drop a record off the front of a sockbuf
1054 * and move the next record to the front.
1058 register struct sockbuf
*sb
;
1060 register struct mbuf
*m
, *mn
;
1061 register struct kextcb
*kp
;
1063 kp
= sotokextcb(sbtoso(sb
));
1065 if (kp
->e_sout
&& kp
->e_sout
->su_sbdroprecord
) {
1066 if ((*kp
->e_sout
->su_sbdroprecord
)(sb
, kp
))
1074 sb
->sb_mb
= m
->m_nextpkt
;
1081 postevent(0, sb
, EV_RWBYTES
);
1085 * Create a "control" mbuf containing the specified data
1086 * with the specified type for presentation on a socket buffer.
1089 sbcreatecontrol(p
, size
, type
, level
)
1094 register struct cmsghdr
*cp
;
1097 if (CMSG_SPACE((u_int
)size
) > MLEN
)
1098 return ((struct mbuf
*) NULL
);
1099 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1100 return ((struct mbuf
*) NULL
);
1101 cp
= mtod(m
, struct cmsghdr
*);
1102 /* XXX check size? */
1103 (void)memcpy(CMSG_DATA(cp
), p
, size
);
1104 m
->m_len
= CMSG_SPACE(size
);
1105 cp
->cmsg_len
= CMSG_LEN(size
);
1106 cp
->cmsg_level
= level
;
1107 cp
->cmsg_type
= type
;
1112 * Some routines that return EOPNOTSUPP for entry points that are not
1113 * supported by a protocol. Fill in as needed.
1116 pru_abort_notsupp(struct socket
*so
)
1123 pru_accept_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1129 pru_attach_notsupp(struct socket
*so
, int proto
, struct proc
*p
)
1135 pru_bind_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1141 pru_connect_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1147 pru_connect2_notsupp(struct socket
*so1
, struct socket
*so2
)
1153 pru_control_notsupp(struct socket
*so
, u_long cmd
, caddr_t data
,
1154 struct ifnet
*ifp
, struct proc
*p
)
1160 pru_detach_notsupp(struct socket
*so
)
1166 pru_disconnect_notsupp(struct socket
*so
)
1172 pru_listen_notsupp(struct socket
*so
, struct proc
*p
)
1178 pru_peeraddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1184 pru_rcvd_notsupp(struct socket
*so
, int flags
)
1190 pru_rcvoob_notsupp(struct socket
*so
, struct mbuf
*m
, int flags
)
1196 pru_send_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
1197 struct sockaddr
*addr
, struct mbuf
*control
,
1206 * This isn't really a ``null'' operation, but it's the default one
1207 * and doesn't do anything destructive.
1210 pru_sense_null(struct socket
*so
, struct stat
*sb
)
1212 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1217 int pru_sosend_notsupp(struct socket
*so
, struct sockaddr
*addr
,
1218 struct uio
*uio
, struct mbuf
*top
,
1219 struct mbuf
*control
, int flags
)
1225 int pru_soreceive_notsupp(struct socket
*so
,
1226 struct sockaddr
**paddr
,
1227 struct uio
*uio
, struct mbuf
**mp0
,
1228 struct mbuf
**controlp
, int *flagsp
)
1235 pru_shutdown_notsupp(struct socket
*so
)
1241 pru_sockaddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1246 int pru_sosend(struct socket
*so
, struct sockaddr
*addr
,
1247 struct uio
*uio
, struct mbuf
*top
,
1248 struct mbuf
*control
, int flags
)
1253 int pru_soreceive(struct socket
*so
,
1254 struct sockaddr
**paddr
,
1255 struct uio
*uio
, struct mbuf
**mp0
,
1256 struct mbuf
**controlp
, int *flagsp
)
1262 int pru_sopoll_notsupp(struct socket
*so
, int events
,
1271 * The following are macros on BSD and functions on Darwin
1275 * Do we need to notify the other side when I/O is possible?
1279 sb_notify(struct sockbuf
*sb
)
1281 return ((sb
->sb_flags
& (SB_WAIT
|SB_SEL
|SB_ASYNC
|SB_UPCALL
)) != 0);
1285 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1286 * This is problematical if the fields are unsigned, as the space might
1287 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1288 * overflow and return 0. Should use "lmin" but it doesn't exist now.
1291 sbspace(struct sockbuf
*sb
)
1293 return ((long) imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
1294 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
)));
1297 /* do we have to send all at once on a socket? */
1299 sosendallatonce(struct socket
*so
)
1301 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1304 /* can we read something from so? */
1306 soreadable(struct socket
*so
)
1308 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1309 (so
->so_state
& SS_CANTRCVMORE
) ||
1310 so
->so_comp
.tqh_first
|| so
->so_error
);
1313 /* can we write something to so? */
1316 sowriteable(struct socket
*so
)
1318 return ((sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
&&
1319 ((so
->so_state
&SS_ISCONNECTED
) ||
1320 (so
->so_proto
->pr_flags
&PR_CONNREQUIRED
)==0)) ||
1321 (so
->so_state
& SS_CANTSENDMORE
) ||
1325 /* adjust counters in sb reflecting allocation of m */
1328 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
1330 sb
->sb_cc
+= m
->m_len
;
1331 sb
->sb_mbcnt
+= MSIZE
;
1332 if (m
->m_flags
& M_EXT
)
1333 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
1336 /* adjust counters in sb reflecting freeing of m */
1338 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
1340 sb
->sb_cc
-= m
->m_len
;
1341 sb
->sb_mbcnt
-= MSIZE
;
1342 if (m
->m_flags
& M_EXT
)
1343 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
1347 * Set lock on sockbuf sb; sleep if lock is already held.
1348 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1349 * Returns error without lock if sleep is interrupted.
1352 sblock(struct sockbuf
*sb
, int wf
)
1354 return(sb
->sb_flags
& SB_LOCK
?
1355 ((wf
== M_WAIT
) ? sb_lock(sb
) : EWOULDBLOCK
) :
1356 (sb
->sb_flags
|= SB_LOCK
), 0);
1359 /* release lock on sockbuf sb */
1361 sbunlock(struct sockbuf
*sb
)
1363 sb
->sb_flags
&= ~SB_LOCK
;
1364 if (sb
->sb_flags
& SB_WANT
) {
1365 sb
->sb_flags
&= ~SB_WANT
;
1366 wakeup((caddr_t
)&(sb
)->sb_flags
);
1371 sorwakeup(struct socket
* so
)
1373 if (sb_notify(&so
->so_rcv
))
1374 sowakeup(so
, &so
->so_rcv
);
1378 sowwakeup(struct socket
* so
)
1380 if (sb_notify(&so
->so_snd
))
1381 sowakeup(so
, &so
->so_snd
);
1386 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1389 dup_sockaddr(sa
, canwait
)
1390 struct sockaddr
*sa
;
1393 struct sockaddr
*sa2
;
1395 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1396 canwait
? M_WAITOK
: M_NOWAIT
);
1398 bcopy(sa
, sa2
, sa
->sa_len
);
1403 * Create an external-format (``xsocket'') structure using the information
1404 * in the kernel-format socket structure pointed to by so. This is done
1405 * to reduce the spew of irrelevant information over this interface,
1406 * to isolate user code from changes in the kernel structure, and
1407 * potentially to provide information-hiding if we decide that
1408 * some of this information should be hidden from users.
1411 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1413 xso
->xso_len
= sizeof *xso
;
1415 xso
->so_type
= so
->so_type
;
1416 xso
->so_options
= so
->so_options
;
1417 xso
->so_linger
= so
->so_linger
;
1418 xso
->so_state
= so
->so_state
;
1419 xso
->so_pcb
= so
->so_pcb
;
1420 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1421 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1422 xso
->so_qlen
= so
->so_qlen
;
1423 xso
->so_incqlen
= so
->so_incqlen
;
1424 xso
->so_qlimit
= so
->so_qlimit
;
1425 xso
->so_timeo
= so
->so_timeo
;
1426 xso
->so_error
= so
->so_error
;
1427 xso
->so_pgid
= so
->so_pgid
;
1428 xso
->so_oobmark
= so
->so_oobmark
;
1429 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1430 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1431 xso
->so_uid
= so
->so_uid
;
1435 * This does the same for sockbufs. Note that the xsockbuf structure,
1436 * since it is always embedded in a socket, does not include a self
1437 * pointer nor a length. We make this entry point public in case
1438 * some other mechanism needs it.
1441 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1443 xsb
->sb_cc
= sb
->sb_cc
;
1444 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1445 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1446 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1447 xsb
->sb_lowat
= sb
->sb_lowat
;
1448 xsb
->sb_flags
= sb
->sb_flags
;
1449 xsb
->sb_timeo
= sb
->sb_timeo
;
1453 * Here is the definition of some of the basic objects in the kern.ipc
1454 * branch of the MIB.
1456 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
, 0, "IPC");
1458 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1460 SYSCTL_INT(_kern
, KERN_DUMMY
, dummy
, CTLFLAG_RW
, &dummy
, 0, "");
1462 SYSCTL_INT(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLFLAG_RW
,
1463 &sb_max
, 0, "Maximum socket buffer size");
1464 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
,
1465 &maxsockets
, 0, "Maximum number of sockets avaliable");
1466 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
,
1467 &sb_efficiency
, 0, "");
1468 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
, &nmbclusters
, 0, "");