]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
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
57 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/domain.h>
63 #include <sys/kernel.h>
65 #include <sys/malloc.h>
67 #include <sys/protosw.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/signalvar.h>
72 #include <sys/sysctl.h>
75 #include <sys/kdebug.h>
77 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
78 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
82 * Primitive routines for operating on sockets and socket buffers
85 u_long sb_max
= SB_MAX
; /* XXX should be static */
87 static u_long sb_efficiency
= 8; /* parameter for sbreserve() */
90 * Procedures to manipulate state flags of socket
91 * and do appropriate wakeups. Normal sequence from the
92 * active (originating) side is that soisconnecting() is
93 * called during processing of connect() call,
94 * resulting in an eventual call to soisconnected() if/when the
95 * connection is established. When the connection is torn down
96 * soisdisconnecting() is called during processing of disconnect() call,
97 * and soisdisconnected() is called when the connection to the peer
98 * is totally severed. The semantics of these routines are such that
99 * connectionless protocols can call soisconnected() and soisdisconnected()
100 * only, bypassing the in-progress calls when setting up a ``connection''
103 * From the passive side, a socket is created with
104 * two queues of sockets: so_incomp for connections in progress
105 * and so_comp for connections already made and awaiting user acceptance.
106 * As a protocol is preparing incoming connections, it creates a socket
107 * structure queued on so_incomp by calling sonewconn(). When the connection
108 * is established, soisconnected() is called, and transfers the
109 * socket structure to so_comp, making it available to accept().
111 * If a socket is closed with sockets on either
112 * so_incomp or so_comp, these sockets are dropped.
114 * If higher level protocols are implemented in
115 * the kernel, the wakeups done here will sometimes
116 * cause software-interrupt process scheduling.
121 register struct socket
*so
;
124 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
125 so
->so_state
|= SS_ISCONNECTING
;
132 struct socket
*head
= so
->so_head
;
137 if (kp
->e_soif
&& kp
->e_soif
->sf_soisconnected
) {
138 if ((*kp
->e_soif
->sf_soisconnected
)(so
, kp
))
144 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
145 so
->so_state
|= SS_ISCONNECTED
;
146 if (head
&& (so
->so_state
& SS_INCOMP
)) {
147 postevent(head
,0,EV_RCONN
);
148 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
150 so
->so_state
&= ~SS_INCOMP
;
151 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
152 so
->so_state
|= SS_COMP
;
154 wakeup_one(&head
->so_timeo
);
156 postevent(so
,0,EV_WCONN
);
157 wakeup((caddr_t
)&so
->so_timeo
);
164 soisdisconnecting(so
)
165 register struct socket
*so
;
167 register struct kextcb
*kp
;
171 if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnecting
) {
172 if ((*kp
->e_soif
->sf_soisdisconnecting
)(so
, kp
))
178 so
->so_state
&= ~SS_ISCONNECTING
;
179 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
180 wakeup((caddr_t
)&so
->so_timeo
);
187 register struct socket
*so
;
189 register struct kextcb
*kp
;
193 if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnected
) {
194 if ((*kp
->e_soif
->sf_soisdisconnected
)(so
, kp
))
200 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
201 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
|SS_ISDISCONNECTED
);
202 wakeup((caddr_t
)&so
->so_timeo
);
208 * Return a random connection that hasn't been serviced yet and
209 * is eligible for discard. There is a one in qlen chance that
210 * we will return a null, saying that there are no dropable
211 * requests. In this case, the protocol specific code should drop
212 * the new request. This insures fairness.
214 * This may be used in conjunction with protocol specific queue
215 * congestion routines.
219 register struct socket
*head
;
221 register struct socket
*so
;
222 unsigned int i
, j
, qlen
;
224 static struct timeval old_runtime
;
225 static unsigned int cur_cnt
, old_cnt
;
229 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
231 old_cnt
= cur_cnt
/ i
;
235 so
= TAILQ_FIRST(&head
->so_incomp
);
239 qlen
= head
->so_incqlen
;
240 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
241 rnd
= (314159 * rnd
+ 66329) & 0xffff;
242 j
= ((qlen
+ 1) * rnd
) >> 16;
245 so
= TAILQ_NEXT(so
, so_list
);
252 * When an attempt at a new connection is noted on a socket
253 * which accepts connections, sonewconn is called. If the
254 * connection is possible (subject to space constraints, etc.)
255 * then we allocate a new structure, propoerly linked into the
256 * data structure of the original socket, and return this.
257 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
260 sonewconn(head
, connstatus
)
261 register struct socket
*head
;
265 register struct socket
*so
;
266 register struct kextcb
*kp
;
268 if (head
->so_qlen
> 3 * head
->so_qlimit
/ 2)
269 return ((struct socket
*)0);
270 so
= soalloc(1, head
->so_proto
->pr_domain
->dom_family
, head
->so_type
);
272 return ((struct socket
*)0);
273 /* check if head was closed during the soalloc */
274 if (head
->so_proto
== NULL
) {
276 return ((struct socket
*)0);
280 so
->so_type
= head
->so_type
;
281 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
282 so
->so_linger
= head
->so_linger
;
283 so
->so_state
= head
->so_state
| SS_NOFDREF
;
284 so
->so_proto
= head
->so_proto
;
285 so
->so_timeo
= head
->so_timeo
;
286 so
->so_pgid
= head
->so_pgid
;
287 so
->so_uid
= head
->so_uid
;
289 /* Attach socket filters for this protocol */
290 if (so
->so_proto
->pr_sfilter
.tqh_first
)
291 error
= sfilter_init(so
);
294 return ((struct socket
*)0);
297 /* Call socket filters' sonewconn1 function if set */
300 if (kp
->e_soif
&& kp
->e_soif
->sf_sonewconn
) {
301 error
= (int)(*kp
->e_soif
->sf_sonewconn
)(so
, connstatus
, kp
);
302 if (error
== EJUSTRETURN
) {
304 } else if (error
!= 0) {
312 if (soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
) ||
313 (*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
)) {
316 return ((struct socket
*)0);
319 so
->so_proto
->pr_domain
->dom_refs
++;
323 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
324 so
->so_state
|= SS_COMP
;
326 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
327 so
->so_state
|= SS_INCOMP
;
333 wakeup((caddr_t
)&head
->so_timeo
);
334 so
->so_state
|= connstatus
;
337 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
338 TAILQ_INIT(&so
->so_evlist
);
344 * Socantsendmore indicates that no more data will be sent on the
345 * socket; it would normally be applied to a socket when the user
346 * informs the system that no more data is to be sent, by the protocol
347 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
348 * will be received, and will normally be applied to the socket by a
349 * protocol when it detects that the peer will send no more data.
350 * Data queued for reading in the socket may yet be read.
357 register struct kextcb
*kp
;
361 if (kp
->e_soif
&& kp
->e_soif
->sf_socantsendmore
) {
362 if ((*kp
->e_soif
->sf_socantsendmore
)(so
, kp
))
369 so
->so_state
|= SS_CANTSENDMORE
;
377 register struct kextcb
*kp
;
381 if (kp
->e_soif
&& kp
->e_soif
->sf_socantrcvmore
) {
382 if ((*kp
->e_soif
->sf_socantrcvmore
)(so
, kp
))
389 so
->so_state
|= SS_CANTRCVMORE
;
394 * Wait for data to arrive at/drain from a socket buffer.
401 sb
->sb_flags
|= SB_WAIT
;
402 return (tsleep((caddr_t
)&sb
->sb_cc
,
403 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait",
408 * Lock a sockbuf already known to be locked;
409 * return any error returned from sleep (EINTR).
413 register struct sockbuf
*sb
;
417 while (sb
->sb_flags
& SB_LOCK
) {
418 sb
->sb_flags
|= SB_WANT
;
419 error
= tsleep((caddr_t
)&sb
->sb_flags
,
420 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
|PCATCH
,
425 sb
->sb_flags
|= SB_LOCK
;
430 * Wakeup processes waiting on a socket buffer.
431 * Do asynchronous notification via SIGIO
432 * if the socket has the SS_ASYNC flag set.
436 register struct socket
*so
;
437 register struct sockbuf
*sb
;
439 struct proc
*p
= current_proc();
440 /* We clear the flag before calling selwakeup. */
441 /* BSD calls selwakeup then sets the flag */
442 sb
->sb_flags
&= ~SB_SEL
;
443 selwakeup(&sb
->sb_sel
);
444 if (sb
->sb_flags
& SB_WAIT
) {
445 sb
->sb_flags
&= ~SB_WAIT
;
446 wakeup((caddr_t
)&sb
->sb_cc
);
448 if (so
->so_state
& SS_ASYNC
) {
450 gsignal(-so
->so_pgid
, SIGIO
);
451 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
454 if (sb
->sb_flags
& SB_UPCALL
)
455 (*so
->so_upcall
)(so
, so
->so_upcallarg
, M_DONTWAIT
);
459 * Socket buffer (struct sockbuf) utility routines.
461 * Each socket contains two socket buffers: one for sending data and
462 * one for receiving data. Each buffer contains a queue of mbufs,
463 * information about the number of mbufs and amount of data in the
464 * queue, and other fields allowing select() statements and notification
465 * on data availability to be implemented.
467 * Data stored in a socket buffer is maintained as a list of records.
468 * Each record is a list of mbufs chained together with the m_next
469 * field. Records are chained together with the m_nextpkt field. The upper
470 * level routine soreceive() expects the following conventions to be
471 * observed when placing information in the receive buffer:
473 * 1. If the protocol requires each message be preceded by the sender's
474 * name, then a record containing that name must be present before
475 * any associated data (mbuf's must be of type MT_SONAME).
476 * 2. If the protocol supports the exchange of ``access rights'' (really
477 * just additional data associated with the message), and there are
478 * ``rights'' to be received, then a record containing this data
479 * should be present (mbuf's must be of type MT_RIGHTS).
480 * 3. If a name or rights record exists, then it must be followed by
481 * a data record, perhaps of zero length.
483 * Before using a new socket structure it is first necessary to reserve
484 * buffer space to the socket, by calling sbreserve(). This should commit
485 * some of the available buffer space in the system buffer pool for the
486 * socket (currently, it does nothing but enforce limits). The space
487 * should be released by calling sbrelease() when the socket is destroyed.
491 soreserve(so
, sndcc
, rcvcc
)
492 register struct socket
*so
;
495 register struct kextcb
*kp
;
499 if (kp
->e_soif
&& kp
->e_soif
->sf_soreserve
) {
500 if ((*kp
->e_soif
->sf_soreserve
)(so
, sndcc
, rcvcc
, kp
))
506 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
508 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
510 if (so
->so_rcv
.sb_lowat
== 0)
511 so
->so_rcv
.sb_lowat
= 1;
512 if (so
->so_snd
.sb_lowat
== 0)
513 so
->so_snd
.sb_lowat
= MCLBYTES
;
514 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
515 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
519 selthreadclear(&so
->so_snd
.sb_sel
);
521 sbrelease(&so
->so_snd
);
527 * Allot mbufs to a sockbuf.
528 * Attempt to scale mbmax so that mbcnt doesn't become limiting
529 * if buffering efficiency is near the normal case.
536 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
539 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
540 if (sb
->sb_lowat
> sb
->sb_hiwat
)
541 sb
->sb_lowat
= sb
->sb_hiwat
;
546 * Free mbufs held by a socket, and reserved mbuf space.
548 /* WARNING needs to do selthreadclear() before calling this */
561 * Routines to add and remove
562 * data from an mbuf queue.
564 * The routines sbappend() or sbappendrecord() are normally called to
565 * append new mbufs to a socket buffer, after checking that adequate
566 * space is available, comparing the function sbspace() with the amount
567 * of data to be added. sbappendrecord() differs from sbappend() in
568 * that data supplied is treated as the beginning of a new record.
569 * To place a sender's address, optional access rights, and data in a
570 * socket receive buffer, sbappendaddr() should be used. To place
571 * access rights and data in a socket receive buffer, sbappendrights()
572 * should be used. In either case, the new data begins a new record.
573 * Note that unlike sbappend() and sbappendrecord(), these routines check
574 * for the caller that there will be enough space to store the data.
575 * Each fails if there is not enough space, or if it cannot find mbufs
576 * to store additional information in.
578 * Reliable protocols may use the socket send buffer to hold data
579 * awaiting acknowledgement. Data is normally copied from a socket
580 * send buffer in a protocol with m_copy for output to a peer,
581 * and then removing the data from the socket buffer with sbdrop()
582 * or sbdroprecord() when the data is acknowledged by the peer.
586 * Append mbuf chain m to the last record in the
587 * socket buffer sb. The additional space associated
588 * the mbuf chain is recorded in sb. Empty mbufs are
589 * discarded and mbufs are compacted where possible.
597 register struct mbuf
*n
;
600 KERNEL_DEBUG((DBG_FNC_SBAPPEND
| DBG_FUNC_START
), sb
, m
->m_len
, 0, 0, 0);
604 kp
= sotokextcb(sbtoso(sb
));
606 if (kp
->e_sout
&& kp
->e_sout
->su_sbappend
) {
607 if ((*kp
->e_sout
->su_sbappend
)(sb
, m
, kp
))
617 if (n
->m_flags
& M_EOR
) {
618 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
621 } while (n
->m_next
&& (n
= n
->m_next
));
623 sbcompress(sb
, m
, n
);
625 KERNEL_DEBUG((DBG_FNC_SBAPPEND
| DBG_FUNC_END
), sb
, sb
->sb_cc
, 0, 0, 0);
631 register struct sockbuf
*sb
;
633 register struct mbuf
*m
;
634 register struct mbuf
*n
= 0;
635 register u_long len
= 0, mbcnt
= 0;
637 for (m
= sb
->sb_mb
; m
; m
= n
) {
639 for (; m
; m
= m
->m_next
) {
642 if (m
->m_flags
& M_EXT
) /*XXX*/ /* pretty sure this is bogus */
643 mbcnt
+= m
->m_ext
.ext_size
;
647 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
648 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
649 mbcnt
, sb
->sb_mbcnt
);
653 if (len
!= sb
->sb_cc
)
654 printf("sbcheck len %ld != sb_cc %ld\n", len
, sb
->sb_cc
);
655 if (mbcnt
!= sb
->sb_mbcnt
)
656 printf("sbcheck mbcnt %ld != sb_mbcnt %ld\n", mbcnt
, sb
->sb_mbcnt
);
662 * As above, except the mbuf chain
663 * begins a new record.
666 sbappendrecord(sb
, m0
)
667 register struct sockbuf
*sb
;
668 register struct mbuf
*m0
;
670 register struct mbuf
*m
;
671 register struct kextcb
*kp
;
676 kp
= sotokextcb(sbtoso(sb
));
678 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendrecord
)
679 { if ((*kp
->e_sout
->su_sbappendrecord
)(sb
, m0
, kp
))
690 * Put the first mbuf on the queue.
691 * Note this permits zero length records.
700 if (m
&& (m0
->m_flags
& M_EOR
)) {
701 m0
->m_flags
&= ~M_EOR
;
704 sbcompress(sb
, m
, m0
);
708 * As above except that OOB data
709 * is inserted at the beginning of the sockbuf,
710 * but after any other OOB data.
714 register struct sockbuf
*sb
;
715 register struct mbuf
*m0
;
717 register struct mbuf
*m
;
718 register struct mbuf
**mp
;
719 register struct kextcb
*kp
;
724 kp
= sotokextcb(sbtoso(sb
));
726 { if (kp
->e_sout
&& kp
->e_sout
->su_sbinsertoob
)
727 { if ((*kp
->e_sout
->su_sbinsertoob
)(sb
, m0
, kp
))
733 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
739 continue; /* WANT next train */
744 goto again
; /* inspect THIS train further */
749 * Put the first mbuf on the queue.
750 * Note this permits zero length records.
757 if (m
&& (m0
->m_flags
& M_EOR
)) {
758 m0
->m_flags
&= ~M_EOR
;
761 sbcompress(sb
, m
, m0
);
765 * Append address and data, and optionally, control (ancillary) data
766 * to the receive queue of a socket. If present,
767 * m0 must include a packet header with total length.
768 * Returns 0 if no space in sockbuf or insufficient mbufs.
771 sbappendaddr(sb
, asa
, m0
, control
)
772 register struct sockbuf
*sb
;
773 struct sockaddr
*asa
;
774 struct mbuf
*m0
, *control
;
776 register struct mbuf
*m
, *n
;
777 int space
= asa
->sa_len
;
778 register struct kextcb
*kp
;
780 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
781 panic("sbappendaddr");
783 kp
= sotokextcb(sbtoso(sb
));
785 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendaddr
)
786 { if ((*kp
->e_sout
->su_sbappendaddr
)(sb
, asa
, m0
, control
, kp
))
793 space
+= m0
->m_pkthdr
.len
;
794 for (n
= control
; n
; n
= n
->m_next
) {
796 if (n
->m_next
== 0) /* keep pointer to last control buf */
799 if (space
> sbspace(sb
))
801 if (asa
->sa_len
> MLEN
)
803 MGET(m
, M_DONTWAIT
, MT_SONAME
);
806 m
->m_len
= asa
->sa_len
;
807 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
809 n
->m_next
= m0
; /* concatenate data to control */
813 for (n
= m
; n
; n
= n
->m_next
)
822 postevent(0,sb
,EV_RWBYTES
);
827 sbappendcontrol(sb
, m0
, control
)
829 struct mbuf
*control
, *m0
;
831 register struct mbuf
*m
, *n
;
833 register struct kextcb
*kp
;
836 panic("sbappendcontrol");
838 kp
= sotokextcb(sbtoso(sb
));
840 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendcontrol
)
841 { if ((*kp
->e_sout
->su_sbappendcontrol
)(sb
, m0
, control
, kp
))
847 for (m
= control
; ; m
= m
->m_next
) {
852 n
= m
; /* save pointer to last control buffer */
853 for (m
= m0
; m
; m
= m
->m_next
)
855 if (space
> sbspace(sb
))
857 n
->m_next
= m0
; /* concatenate data to control */
858 for (m
= control
; m
; m
= m
->m_next
)
864 n
->m_nextpkt
= control
;
867 postevent(0,sb
,EV_RWBYTES
);
872 * Compress mbuf chain m into the socket
873 * buffer sb following mbuf n. If n
874 * is null, the buffer is presumed empty.
878 register struct sockbuf
*sb
;
879 register struct mbuf
*m
, *n
;
881 register int eor
= 0;
882 register struct mbuf
*o
;
885 eor
|= m
->m_flags
& M_EOR
;
888 (((o
= m
->m_next
) || (o
= n
)) &&
889 o
->m_type
== m
->m_type
))) {
893 if (n
&& (n
->m_flags
& M_EOR
) == 0 &&
897 m
->m_len
<= MCLBYTES
/ 4 && /* XXX: Don't copy too much */
898 m
->m_len
<= M_TRAILINGSPACE(n
) &&
899 n
->m_type
== m
->m_type
) {
900 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
902 n
->m_len
+= m
->m_len
;
903 sb
->sb_cc
+= m
->m_len
;
913 m
->m_flags
&= ~M_EOR
;
921 printf("semi-panic: sbcompress\n");
923 postevent(0,sb
, EV_RWBYTES
);
927 * Free all mbufs in a sockbuf.
928 * Check that all resources are reclaimed.
932 register struct sockbuf
*sb
;
934 register struct kextcb
*kp
;
936 kp
= sotokextcb(sbtoso(sb
));
938 if (kp
->e_sout
&& kp
->e_sout
->su_sbflush
) {
939 if ((*kp
->e_sout
->su_sbflush
)(sb
, kp
))
945 if (sb
->sb_flags
& SB_LOCK
)
947 while (sb
->sb_mbcnt
) {
949 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
950 * we would loop forever. Panic instead.
952 if (!sb
->sb_cc
&& (sb
->sb_mb
== NULL
|| sb
->sb_mb
->m_len
))
954 sbdrop(sb
, (int)sb
->sb_cc
);
956 if (sb
->sb_cc
|| sb
->sb_mb
|| sb
->sb_mbcnt
)
957 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb
->sb_cc
, (void *)sb
->sb_mb
, sb
->sb_mbcnt
);
958 postevent(0, sb
, EV_RWBYTES
);
962 * Drop data from (the front of) a sockbuf.
963 * use m_freem_list to free the mbuf structures
964 * under a single lock... this is done by pruning
965 * the top of the tree from the body by keeping track
966 * of where we get to in the tree and then zeroing the
967 * two pertinent pointers m_nextpkt and m_next
968 * the socket buffer is then updated to point at the new
969 * top of the tree and the pruned area is released via
974 register struct sockbuf
*sb
;
977 register struct mbuf
*m
, *free_list
, *ml
;
978 struct mbuf
*next
, *last
;
979 register struct kextcb
*kp
;
981 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_START
), sb
, len
, 0, 0, 0);
983 kp
= sotokextcb(sbtoso(sb
));
985 if (kp
->e_sout
&& kp
->e_sout
->su_sbdrop
) {
986 if ((*kp
->e_sout
->su_sbdrop
)(sb
, len
, kp
))
991 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
992 free_list
= last
= m
;
993 ml
= (struct mbuf
*)0;
998 /* temporarily replacing this panic with printf because
999 * it occurs occasionally when closing a socket when there
1000 * is no harm in ignoring it. This problem will be investigated
1003 /* panic("sbdrop"); */
1004 printf("sbdrop - count not zero\n");
1006 /* zero the counts. if we have no mbufs, we have no data (PR-2986815) */
1012 next
= m
->m_nextpkt
;
1015 if (m
->m_len
> len
) {
1027 while (m
&& m
->m_len
== 0) {
1034 ml
->m_next
= (struct mbuf
*)0;
1035 last
->m_nextpkt
= (struct mbuf
*)0;
1036 m_freem_list(free_list
);
1040 m
->m_nextpkt
= next
;
1044 postevent(0, sb
, EV_RWBYTES
);
1046 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_END
), sb
, 0, 0, 0, 0);
1050 * Drop a record off the front of a sockbuf
1051 * and move the next record to the front.
1055 register struct sockbuf
*sb
;
1057 register struct mbuf
*m
, *mn
;
1058 register struct kextcb
*kp
;
1060 kp
= sotokextcb(sbtoso(sb
));
1062 if (kp
->e_sout
&& kp
->e_sout
->su_sbdroprecord
) {
1063 if ((*kp
->e_sout
->su_sbdroprecord
)(sb
, kp
))
1071 sb
->sb_mb
= m
->m_nextpkt
;
1078 postevent(0, sb
, EV_RWBYTES
);
1082 * Create a "control" mbuf containing the specified data
1083 * with the specified type for presentation on a socket buffer.
1086 sbcreatecontrol(p
, size
, type
, level
)
1091 register struct cmsghdr
*cp
;
1094 if (CMSG_SPACE((u_int
)size
) > MLEN
)
1095 return ((struct mbuf
*) NULL
);
1096 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1097 return ((struct mbuf
*) NULL
);
1098 cp
= mtod(m
, struct cmsghdr
*);
1099 /* XXX check size? */
1100 (void)memcpy(CMSG_DATA(cp
), p
, size
);
1101 m
->m_len
= CMSG_SPACE(size
);
1102 cp
->cmsg_len
= CMSG_LEN(size
);
1103 cp
->cmsg_level
= level
;
1104 cp
->cmsg_type
= type
;
1109 * Some routines that return EOPNOTSUPP for entry points that are not
1110 * supported by a protocol. Fill in as needed.
1113 pru_abort_notsupp(struct socket
*so
)
1120 pru_accept_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1126 pru_attach_notsupp(struct socket
*so
, int proto
, struct proc
*p
)
1132 pru_bind_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1138 pru_connect_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1144 pru_connect2_notsupp(struct socket
*so1
, struct socket
*so2
)
1150 pru_control_notsupp(struct socket
*so
, u_long cmd
, caddr_t data
,
1151 struct ifnet
*ifp
, struct proc
*p
)
1157 pru_detach_notsupp(struct socket
*so
)
1163 pru_disconnect_notsupp(struct socket
*so
)
1169 pru_listen_notsupp(struct socket
*so
, struct proc
*p
)
1175 pru_peeraddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1181 pru_rcvd_notsupp(struct socket
*so
, int flags
)
1187 pru_rcvoob_notsupp(struct socket
*so
, struct mbuf
*m
, int flags
)
1193 pru_send_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
1194 struct sockaddr
*addr
, struct mbuf
*control
,
1203 * This isn't really a ``null'' operation, but it's the default one
1204 * and doesn't do anything destructive.
1207 pru_sense_null(struct socket
*so
, struct stat
*sb
)
1209 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1214 int pru_sosend_notsupp(struct socket
*so
, struct sockaddr
*addr
,
1215 struct uio
*uio
, struct mbuf
*top
,
1216 struct mbuf
*control
, int flags
)
1222 int pru_soreceive_notsupp(struct socket
*so
,
1223 struct sockaddr
**paddr
,
1224 struct uio
*uio
, struct mbuf
**mp0
,
1225 struct mbuf
**controlp
, int *flagsp
)
1232 pru_shutdown_notsupp(struct socket
*so
)
1238 pru_sockaddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1243 int pru_sosend(struct socket
*so
, struct sockaddr
*addr
,
1244 struct uio
*uio
, struct mbuf
*top
,
1245 struct mbuf
*control
, int flags
)
1250 int pru_soreceive(struct socket
*so
,
1251 struct sockaddr
**paddr
,
1252 struct uio
*uio
, struct mbuf
**mp0
,
1253 struct mbuf
**controlp
, int *flagsp
)
1259 int pru_sopoll_notsupp(struct socket
*so
, int events
,
1268 * The following are macros on BSD and functions on Darwin
1272 * Do we need to notify the other side when I/O is possible?
1276 sb_notify(struct sockbuf
*sb
)
1278 return ((sb
->sb_flags
& (SB_WAIT
|SB_SEL
|SB_ASYNC
|SB_UPCALL
)) != 0);
1282 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1283 * This is problematical if the fields are unsigned, as the space might
1284 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1285 * overflow and return 0. Should use "lmin" but it doesn't exist now.
1288 sbspace(struct sockbuf
*sb
)
1290 return ((long) imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
1291 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
)));
1294 /* do we have to send all at once on a socket? */
1296 sosendallatonce(struct socket
*so
)
1298 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1301 /* can we read something from so? */
1303 soreadable(struct socket
*so
)
1305 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1306 (so
->so_state
& SS_CANTRCVMORE
) ||
1307 so
->so_comp
.tqh_first
|| so
->so_error
);
1310 /* can we write something to so? */
1313 sowriteable(struct socket
*so
)
1315 return ((sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
&&
1316 ((so
->so_state
&SS_ISCONNECTED
) ||
1317 (so
->so_proto
->pr_flags
&PR_CONNREQUIRED
)==0)) ||
1318 (so
->so_state
& SS_CANTSENDMORE
) ||
1322 /* adjust counters in sb reflecting allocation of m */
1325 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
1327 sb
->sb_cc
+= m
->m_len
;
1328 sb
->sb_mbcnt
+= MSIZE
;
1329 if (m
->m_flags
& M_EXT
)
1330 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
1333 /* adjust counters in sb reflecting freeing of m */
1335 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
1337 sb
->sb_cc
-= m
->m_len
;
1338 sb
->sb_mbcnt
-= MSIZE
;
1339 if (m
->m_flags
& M_EXT
)
1340 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
1344 * Set lock on sockbuf sb; sleep if lock is already held.
1345 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1346 * Returns error without lock if sleep is interrupted.
1349 sblock(struct sockbuf
*sb
, int wf
)
1351 return(sb
->sb_flags
& SB_LOCK
?
1352 ((wf
== M_WAIT
) ? sb_lock(sb
) : EWOULDBLOCK
) :
1353 (sb
->sb_flags
|= SB_LOCK
), 0);
1356 /* release lock on sockbuf sb */
1358 sbunlock(struct sockbuf
*sb
)
1360 sb
->sb_flags
&= ~SB_LOCK
;
1361 if (sb
->sb_flags
& SB_WANT
) {
1362 sb
->sb_flags
&= ~SB_WANT
;
1363 wakeup((caddr_t
)&(sb
)->sb_flags
);
1368 sorwakeup(struct socket
* so
)
1370 if (sb_notify(&so
->so_rcv
))
1371 sowakeup(so
, &so
->so_rcv
);
1375 sowwakeup(struct socket
* so
)
1377 if (sb_notify(&so
->so_snd
))
1378 sowakeup(so
, &so
->so_snd
);
1383 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1386 dup_sockaddr(sa
, canwait
)
1387 struct sockaddr
*sa
;
1390 struct sockaddr
*sa2
;
1392 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1393 canwait
? M_WAITOK
: M_NOWAIT
);
1395 bcopy(sa
, sa2
, sa
->sa_len
);
1400 * Create an external-format (``xsocket'') structure using the information
1401 * in the kernel-format socket structure pointed to by so. This is done
1402 * to reduce the spew of irrelevant information over this interface,
1403 * to isolate user code from changes in the kernel structure, and
1404 * potentially to provide information-hiding if we decide that
1405 * some of this information should be hidden from users.
1408 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1410 xso
->xso_len
= sizeof *xso
;
1412 xso
->so_type
= so
->so_type
;
1413 xso
->so_options
= so
->so_options
;
1414 xso
->so_linger
= so
->so_linger
;
1415 xso
->so_state
= so
->so_state
;
1416 xso
->so_pcb
= so
->so_pcb
;
1417 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1418 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1419 xso
->so_qlen
= so
->so_qlen
;
1420 xso
->so_incqlen
= so
->so_incqlen
;
1421 xso
->so_qlimit
= so
->so_qlimit
;
1422 xso
->so_timeo
= so
->so_timeo
;
1423 xso
->so_error
= so
->so_error
;
1424 xso
->so_pgid
= so
->so_pgid
;
1425 xso
->so_oobmark
= so
->so_oobmark
;
1426 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1427 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1428 xso
->so_uid
= so
->so_uid
;
1432 * This does the same for sockbufs. Note that the xsockbuf structure,
1433 * since it is always embedded in a socket, does not include a self
1434 * pointer nor a length. We make this entry point public in case
1435 * some other mechanism needs it.
1438 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1440 xsb
->sb_cc
= sb
->sb_cc
;
1441 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1442 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1443 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1444 xsb
->sb_lowat
= sb
->sb_lowat
;
1445 xsb
->sb_flags
= sb
->sb_flags
;
1446 xsb
->sb_timeo
= sb
->sb_timeo
;
1450 * Here is the definition of some of the basic objects in the kern.ipc
1451 * branch of the MIB.
1453 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
, 0, "IPC");
1455 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1457 SYSCTL_INT(_kern
, KERN_DUMMY
, dummy
, CTLFLAG_RW
, &dummy
, 0, "");
1459 SYSCTL_INT(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLFLAG_RW
,
1460 &sb_max
, 0, "Maximum socket buffer size");
1461 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
,
1462 &maxsockets
, 0, "Maximum number of sockets avaliable");
1463 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
,
1464 &sb_efficiency
, 0, "");
1465 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
, &nmbclusters
, 0, "");