]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
33d2ede3b7c4ff949b1b14a646d72b0ace16a56f
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>
74 #include <sys/kdebug.h>
76 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
77 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
81 * Primitive routines for operating on sockets and socket buffers
84 u_long sb_max
= SB_MAX
; /* XXX should be static */
86 static u_long sb_efficiency
= 8; /* parameter for sbreserve() */
88 char netcon
[] = "netcon";
91 * Procedures to manipulate state flags of socket
92 * and do appropriate wakeups. Normal sequence from the
93 * active (originating) side is that soisconnecting() is
94 * called during processing of connect() call,
95 * resulting in an eventual call to soisconnected() if/when the
96 * connection is established. When the connection is torn down
97 * soisdisconnecting() is called during processing of disconnect() call,
98 * and soisdisconnected() is called when the connection to the peer
99 * is totally severed. The semantics of these routines are such that
100 * connectionless protocols can call soisconnected() and soisdisconnected()
101 * only, bypassing the in-progress calls when setting up a ``connection''
104 * From the passive side, a socket is created with
105 * two queues of sockets: so_incomp for connections in progress
106 * and so_comp for connections already made and awaiting user acceptance.
107 * As a protocol is preparing incoming connections, it creates a socket
108 * structure queued on so_incomp by calling sonewconn(). When the connection
109 * is established, soisconnected() is called, and transfers the
110 * socket structure to so_comp, making it available to accept().
112 * If a socket is closed with sockets on either
113 * so_incomp or so_comp, these sockets are dropped.
115 * If higher level protocols are implemented in
116 * the kernel, the wakeups done here will sometimes
117 * cause software-interrupt process scheduling.
122 register struct socket
*so
;
125 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
126 so
->so_state
|= SS_ISCONNECTING
;
131 register struct socket
*so
;
132 { register struct kextcb
*kp
;
133 register 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((caddr_t
)&head
->so_timeo
);
156 postevent(so
,0,EV_WCONN
);
157 wakeup((caddr_t
)&so
->so_timeo
);
164 soisdisconnecting(so
)
165 register struct socket
*so
;
166 { register struct kextcb
*kp
;
170 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnecting
)
171 { if ((*kp
->e_soif
->sf_soisdisconnecting
)(so
, kp
))
177 so
->so_state
&= ~SS_ISCONNECTING
;
178 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
179 wakeup((caddr_t
)&so
->so_timeo
);
186 register struct socket
*so
;
187 { register struct kextcb
*kp
;
191 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnected
)
192 { if ((*kp
->e_soif
->sf_soisdisconnected
)(so
, kp
))
198 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
199 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
);
200 wakeup((caddr_t
)&so
->so_timeo
);
206 * Return a random connection that hasn't been serviced yet and
207 * is eligible for discard. There is a one in qlen chance that
208 * we will return a null, saying that there are no dropable
209 * requests. In this case, the protocol specific code should drop
210 * the new request. This insures fairness.
212 * This may be used in conjunction with protocol specific queue
213 * congestion routines.
217 register struct socket
*head
;
219 register struct socket
*so
;
220 unsigned int i
, j
, qlen
;
222 static struct timeval old_runtime
;
223 static unsigned int cur_cnt
, old_cnt
;
227 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
229 old_cnt
= cur_cnt
/ i
;
233 so
= TAILQ_FIRST(&head
->so_incomp
);
237 qlen
= head
->so_incqlen
;
238 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
239 rnd
= (314159 * rnd
+ 66329) & 0xffff;
240 j
= ((qlen
+ 1) * rnd
) >> 16;
243 so
= TAILQ_NEXT(so
, so_list
);
250 * When an attempt at a new connection is noted on a socket
251 * which accepts connections, sonewconn is called. If the
252 * connection is possible (subject to space constraints, etc.)
253 * then we allocate a new structure, propoerly linked into the
254 * data structure of the original socket, and return this.
255 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
258 sonewconn(head
, connstatus
)
259 register struct socket
*head
;
262 register struct socket
*so
;
263 register struct kextcb
*kp
;
265 if (head
->so_qlen
> 3 * head
->so_qlimit
/ 2)
266 return ((struct socket
*)0);
267 so
= soalloc(1, head
->so_proto
->pr_domain
->dom_family
, head
->so_type
);
269 return ((struct socket
*)0);
273 { if (kp
->e_soif
&& kp
->e_soif
->sf_sonewconn1
)
274 { if ((*kp
->e_soif
->sf_sonewconn1
)(so
, connstatus
, kp
))
281 so
->so_type
= head
->so_type
;
282 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
283 so
->so_linger
= head
->so_linger
;
284 so
->so_state
= head
->so_state
| SS_NOFDREF
;
285 so
->so_proto
= head
->so_proto
;
286 so
->so_timeo
= head
->so_timeo
;
287 so
->so_pgid
= head
->so_pgid
;
288 so
->so_uid
= head
->so_uid
;
289 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
291 (void) soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
);
293 if (so
->so_proto
->pr_sfilter
.tqh_first
)
294 error
= sfilter_init(so
);
295 if (error
== 0 && (*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
)) {
298 return ((struct socket
*)0);
300 so
->so_proto
->pr_domain
->dom_refs
++;
303 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
304 so
->so_state
|= SS_COMP
;
306 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
307 so
->so_state
|= SS_INCOMP
;
313 wakeup((caddr_t
)&head
->so_timeo
);
314 so
->so_state
|= connstatus
;
316 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
317 TAILQ_INIT(&so
->so_evlist
);
322 * Socantsendmore indicates that no more data will be sent on the
323 * socket; it would normally be applied to a socket when the user
324 * informs the system that no more data is to be sent, by the protocol
325 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
326 * will be received, and will normally be applied to the socket by a
327 * protocol when it detects that the peer will send no more data.
328 * Data queued for reading in the socket may yet be read.
334 { register struct kextcb
*kp
;
338 { if (kp
->e_soif
&& kp
->e_soif
->sf_socantsendmore
)
339 { if ((*kp
->e_soif
->sf_socantsendmore
)(so
, kp
))
346 so
->so_state
|= SS_CANTSENDMORE
;
353 { register struct kextcb
*kp
;
357 { if (kp
->e_soif
&& kp
->e_soif
->sf_socantrcvmore
)
358 { if ((*kp
->e_soif
->sf_socantrcvmore
)(so
, kp
))
365 so
->so_state
|= SS_CANTRCVMORE
;
370 * Wait for data to arrive at/drain from a socket buffer.
377 sb
->sb_flags
|= SB_WAIT
;
378 return (tsleep((caddr_t
)&sb
->sb_cc
,
379 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait",
384 * Lock a sockbuf already known to be locked;
385 * return any error returned from sleep (EINTR).
389 register struct sockbuf
*sb
;
393 while (sb
->sb_flags
& SB_LOCK
) {
394 sb
->sb_flags
|= SB_WANT
;
395 error
= tsleep((caddr_t
)&sb
->sb_flags
,
396 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
|PCATCH
,
401 sb
->sb_flags
|= SB_LOCK
;
406 * Wakeup processes waiting on a socket buffer.
407 * Do asynchronous notification via SIGIO
408 * if the socket has the SS_ASYNC flag set.
412 register struct socket
*so
;
413 register struct sockbuf
*sb
;
415 struct proc
*p
= current_proc();
420 sb
->sb_flags
&= ~SB_SEL
;
421 selwakeup(&sb
->sb_sel
);
423 if (sb
->sb_flags
& SB_WAIT
) {
424 sb
->sb_flags
&= ~SB_WAIT
;
425 wakeup((caddr_t
)&sb
->sb_cc
);
427 if (so
->so_state
& SS_ASYNC
) {
429 gsignal(-so
->so_pgid
, SIGIO
);
430 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
434 if (sb
->sb_flags
& SB_UPCALL
)
435 (*so
->so_upcall
)(so
, so
->so_upcallarg
, M_DONTWAIT
);
439 * Socket buffer (struct sockbuf) utility routines.
441 * Each socket contains two socket buffers: one for sending data and
442 * one for receiving data. Each buffer contains a queue of mbufs,
443 * information about the number of mbufs and amount of data in the
444 * queue, and other fields allowing select() statements and notification
445 * on data availability to be implemented.
447 * Data stored in a socket buffer is maintained as a list of records.
448 * Each record is a list of mbufs chained together with the m_next
449 * field. Records are chained together with the m_nextpkt field. The upper
450 * level routine soreceive() expects the following conventions to be
451 * observed when placing information in the receive buffer:
453 * 1. If the protocol requires each message be preceded by the sender's
454 * name, then a record containing that name must be present before
455 * any associated data (mbuf's must be of type MT_SONAME).
456 * 2. If the protocol supports the exchange of ``access rights'' (really
457 * just additional data associated with the message), and there are
458 * ``rights'' to be received, then a record containing this data
459 * should be present (mbuf's must be of type MT_RIGHTS).
460 * 3. If a name or rights record exists, then it must be followed by
461 * a data record, perhaps of zero length.
463 * Before using a new socket structure it is first necessary to reserve
464 * buffer space to the socket, by calling sbreserve(). This should commit
465 * some of the available buffer space in the system buffer pool for the
466 * socket (currently, it does nothing but enforce limits). The space
467 * should be released by calling sbrelease() when the socket is destroyed.
471 soreserve(so
, sndcc
, rcvcc
)
472 register struct socket
*so
;
475 register struct kextcb
*kp
;
479 { if (kp
->e_soif
&& kp
->e_soif
->sf_soreserve
)
480 { if ((*kp
->e_soif
->sf_soreserve
)(so
, sndcc
, rcvcc
, kp
))
486 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
488 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
490 if (so
->so_rcv
.sb_lowat
== 0)
491 so
->so_rcv
.sb_lowat
= 1;
492 if (so
->so_snd
.sb_lowat
== 0)
493 so
->so_snd
.sb_lowat
= MCLBYTES
;
494 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
495 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
498 selthreadclear(&so
->so_snd
.sb_sel
);
499 sbrelease(&so
->so_snd
);
505 * Allot mbufs to a sockbuf.
506 * Attempt to scale mbmax so that mbcnt doesn't become limiting
507 * if buffering efficiency is near the normal case.
514 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
517 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
518 if (sb
->sb_lowat
> sb
->sb_hiwat
)
519 sb
->sb_lowat
= sb
->sb_hiwat
;
524 * Free mbufs held by a socket, and reserved mbuf space.
526 /* WARNING needs to do selthreadclear() before calling this */
533 sb
->sb_hiwat
= sb
->sb_mbmax
= 0;
535 /* this is getting called with bzeroed sb in sorflush */
537 int oldpri
= splimp();
538 selthreadclear(&sb
->sb_sel
);
545 * Routines to add and remove
546 * data from an mbuf queue.
548 * The routines sbappend() or sbappendrecord() are normally called to
549 * append new mbufs to a socket buffer, after checking that adequate
550 * space is available, comparing the function sbspace() with the amount
551 * of data to be added. sbappendrecord() differs from sbappend() in
552 * that data supplied is treated as the beginning of a new record.
553 * To place a sender's address, optional access rights, and data in a
554 * socket receive buffer, sbappendaddr() should be used. To place
555 * access rights and data in a socket receive buffer, sbappendrights()
556 * should be used. In either case, the new data begins a new record.
557 * Note that unlike sbappend() and sbappendrecord(), these routines check
558 * for the caller that there will be enough space to store the data.
559 * Each fails if there is not enough space, or if it cannot find mbufs
560 * to store additional information in.
562 * Reliable protocols may use the socket send buffer to hold data
563 * awaiting acknowledgement. Data is normally copied from a socket
564 * send buffer in a protocol with m_copy for output to a peer,
565 * and then removing the data from the socket buffer with sbdrop()
566 * or sbdroprecord() when the data is acknowledged by the peer.
570 * Append mbuf chain m to the last record in the
571 * socket buffer sb. The additional space associated
572 * the mbuf chain is recorded in sb. Empty mbufs are
573 * discarded and mbufs are compacted where possible.
579 { register struct kextcb
*kp
;
580 register struct mbuf
*n
;
583 KERNEL_DEBUG((DBG_FNC_SBAPPEND
| DBG_FUNC_START
), sb
, m
->m_len
, 0, 0, 0);
587 kp
= sotokextcb(sbtoso(sb
));
589 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappend
)
590 { if ((*kp
->e_sout
->su_sbappend
)(sb
, m
, kp
))
600 if (n
->m_flags
& M_EOR
) {
601 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
604 } while (n
->m_next
&& (n
= n
->m_next
));
606 sbcompress(sb
, m
, n
);
608 KERNEL_DEBUG((DBG_FNC_SBAPPEND
| DBG_FUNC_END
), sb
, sb
->sb_cc
, 0, 0, 0);
614 register struct sockbuf
*sb
;
616 register struct mbuf
*m
;
617 register struct mbuf
*n
= 0;
618 register u_long len
= 0, mbcnt
= 0;
620 for (m
= sb
->sb_mb
; m
; m
= n
) {
622 for (; m
; m
= m
->m_next
) {
625 if (m
->m_flags
& M_EXT
) /*XXX*/ /* pretty sure this is bogus */
626 mbcnt
+= m
->m_ext
.ext_size
;
628 panic("sbcheck nextpkt");
630 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
631 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
632 mbcnt
, sb
->sb_mbcnt
);
639 * As above, except the mbuf chain
640 * begins a new record.
643 sbappendrecord(sb
, m0
)
644 register struct sockbuf
*sb
;
645 register struct mbuf
*m0
;
647 register struct mbuf
*m
;
648 register struct kextcb
*kp
;
653 kp
= sotokextcb(sbtoso(sb
));
655 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendrecord
)
656 { if ((*kp
->e_sout
->su_sbappendrecord
)(sb
, m0
, kp
))
667 * Put the first mbuf on the queue.
668 * Note this permits zero length records.
677 if (m
&& (m0
->m_flags
& M_EOR
)) {
678 m0
->m_flags
&= ~M_EOR
;
681 sbcompress(sb
, m
, m0
);
685 * As above except that OOB data
686 * is inserted at the beginning of the sockbuf,
687 * but after any other OOB data.
691 register struct sockbuf
*sb
;
692 register struct mbuf
*m0
;
694 register struct mbuf
*m
;
695 register struct mbuf
**mp
;
696 register struct kextcb
*kp
;
701 kp
= sotokextcb(sbtoso(sb
));
703 { if (kp
->e_sout
&& kp
->e_sout
->su_sbinsertoob
)
704 { if ((*kp
->e_sout
->su_sbinsertoob
)(sb
, m0
, kp
))
710 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
716 continue; /* WANT next train */
721 goto again
; /* inspect THIS train further */
726 * Put the first mbuf on the queue.
727 * Note this permits zero length records.
734 if (m
&& (m0
->m_flags
& M_EOR
)) {
735 m0
->m_flags
&= ~M_EOR
;
738 sbcompress(sb
, m
, m0
);
742 * Append address and data, and optionally, control (ancillary) data
743 * to the receive queue of a socket. If present,
744 * m0 must include a packet header with total length.
745 * Returns 0 if no space in sockbuf or insufficient mbufs.
748 sbappendaddr(sb
, asa
, m0
, control
)
749 register struct sockbuf
*sb
;
750 struct sockaddr
*asa
;
751 struct mbuf
*m0
, *control
;
753 register struct mbuf
*m
, *n
;
754 int space
= asa
->sa_len
;
755 register struct kextcb
*kp
;
757 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
758 panic("sbappendaddr");
760 kp
= sotokextcb(sbtoso(sb
));
762 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendaddr
)
763 { if ((*kp
->e_sout
->su_sbappendaddr
)(sb
, asa
, m0
, control
, kp
))
770 space
+= m0
->m_pkthdr
.len
;
771 for (n
= control
; n
; n
= n
->m_next
) {
773 if (n
->m_next
== 0) /* keep pointer to last control buf */
776 if (space
> sbspace(sb
))
778 if (asa
->sa_len
> MLEN
)
780 MGET(m
, M_DONTWAIT
, MT_SONAME
);
783 m
->m_len
= asa
->sa_len
;
784 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
786 n
->m_next
= m0
; /* concatenate data to control */
790 for (n
= m
; n
; n
= n
->m_next
)
799 postevent(0,sb
,EV_RWBYTES
);
804 sbappendcontrol(sb
, m0
, control
)
806 struct mbuf
*control
, *m0
;
808 register struct mbuf
*m
, *n
;
810 register struct kextcb
*kp
;
813 panic("sbappendcontrol");
815 kp
= sotokextcb(sbtoso(sb
));
817 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendcontrol
)
818 { if ((*kp
->e_sout
->su_sbappendcontrol
)(sb
, m0
, control
, kp
))
824 for (m
= control
; ; m
= m
->m_next
) {
829 n
= m
; /* save pointer to last control buffer */
830 for (m
= m0
; m
; m
= m
->m_next
)
832 if (space
> sbspace(sb
))
834 n
->m_next
= m0
; /* concatenate data to control */
835 for (m
= control
; m
; m
= m
->m_next
)
841 n
->m_nextpkt
= control
;
844 postevent(0,sb
,EV_RWBYTES
);
849 * Compress mbuf chain m into the socket
850 * buffer sb following mbuf n. If n
851 * is null, the buffer is presumed empty.
855 register struct sockbuf
*sb
;
856 register struct mbuf
*m
, *n
;
858 register int eor
= 0;
859 register struct mbuf
*o
;
862 eor
|= m
->m_flags
& M_EOR
;
865 (((o
= m
->m_next
) || (o
= n
)) &&
866 o
->m_type
== m
->m_type
))) {
870 if (n
&& (n
->m_flags
& (M_EXT
| M_EOR
)) == 0 &&
871 (n
->m_data
+ n
->m_len
+ m
->m_len
) < &n
->m_dat
[MLEN
] &&
872 n
->m_type
== m
->m_type
) {
873 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
875 n
->m_len
+= m
->m_len
;
876 sb
->sb_cc
+= m
->m_len
;
886 m
->m_flags
&= ~M_EOR
;
894 printf("semi-panic: sbcompress\n");
896 postevent(0,sb
, EV_RWBYTES
);
900 * Free all mbufs in a sockbuf.
901 * Check that all resources are reclaimed.
905 register struct sockbuf
*sb
;
907 register struct kextcb
*kp
;
909 kp
= sotokextcb(sbtoso(sb
));
911 { if (kp
->e_sout
&& kp
->e_sout
->su_sbflush
)
912 { if ((*kp
->e_sout
->su_sbflush
)(sb
, kp
))
918 if (sb
->sb_flags
& SB_LOCK
)
919 panic("sbflush: locked");
920 while (sb
->sb_mbcnt
&& sb
->sb_cc
)
921 sbdrop(sb
, (int)sb
->sb_cc
);
922 if (sb
->sb_cc
|| sb
->sb_mb
|| sb
->sb_mbcnt
)
923 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb
->sb_cc
, (void *)sb
->sb_mb
, sb
->sb_mbcnt
);
924 postevent(0, sb
, EV_RWBYTES
);
928 * Drop data from (the front of) a sockbuf.
932 register struct sockbuf
*sb
;
935 register struct mbuf
*m
, *free_list
, *ml
;
936 struct mbuf
*next
, *last
;
937 register struct kextcb
*kp
;
939 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_START
), sb
, len
, 0, 0, 0);
941 kp
= sotokextcb(sbtoso(sb
));
943 { if (kp
->e_sout
&& kp
->e_sout
->su_sbdrop
)
944 { if ((*kp
->e_sout
->su_sbdrop
)(sb
, len
, kp
))
949 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
950 free_list
= last
= m
;
951 ml
= (struct mbuf
*)0;
961 if (m
->m_len
> len
) {
973 while (m
&& m
->m_len
== 0) {
980 ml
->m_next
= (struct mbuf
*)0;
981 last
->m_nextpkt
= (struct mbuf
*)0;
982 m_freem_list(free_list
);
990 postevent(0, sb
, EV_RWBYTES
);
992 KERNEL_DEBUG((DBG_FNC_SBDROP
| DBG_FUNC_END
), sb
, 0, 0, 0, 0);
996 * Drop a record off the front of a sockbuf
997 * and move the next record to the front.
1001 register struct sockbuf
*sb
;
1003 register struct mbuf
*m
, *mn
;
1004 register struct kextcb
*kp
;
1006 kp
= sotokextcb(sbtoso(sb
));
1008 { if (kp
->e_sout
&& kp
->e_sout
->su_sbdroprecord
)
1009 { if ((*kp
->e_sout
->su_sbdroprecord
)(sb
, kp
))
1017 sb
->sb_mb
= m
->m_nextpkt
;
1023 postevent(0, sb
, EV_RWBYTES
);
1027 * Create a "control" mbuf containing the specified data
1028 * with the specified type for presentation on a socket buffer.
1031 sbcreatecontrol(p
, size
, type
, level
)
1036 register struct cmsghdr
*cp
;
1039 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1040 return ((struct mbuf
*) NULL
);
1041 cp
= mtod(m
, struct cmsghdr
*);
1042 /* XXX check size? */
1043 (void)memcpy(CMSG_DATA(cp
), p
, size
);
1044 size
+= sizeof(*cp
);
1046 cp
->cmsg_len
= size
;
1047 cp
->cmsg_level
= level
;
1048 cp
->cmsg_type
= type
;
1053 * Some routines that return EOPNOTSUPP for entry points that are not
1054 * supported by a protocol. Fill in as needed.
1057 pru_abort_notsupp(struct socket
*so
)
1064 pru_accept_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1070 pru_attach_notsupp(struct socket
*so
, int proto
, struct proc
*p
)
1076 pru_bind_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1082 pru_connect_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1088 pru_connect2_notsupp(struct socket
*so1
, struct socket
*so2
)
1094 pru_control_notsupp(struct socket
*so
, u_long cmd
, caddr_t data
,
1095 struct ifnet
*ifp
, struct proc
*p
)
1101 pru_detach_notsupp(struct socket
*so
)
1107 pru_disconnect_notsupp(struct socket
*so
)
1113 pru_listen_notsupp(struct socket
*so
, struct proc
*p
)
1119 pru_peeraddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1125 pru_rcvd_notsupp(struct socket
*so
, int flags
)
1131 pru_rcvoob_notsupp(struct socket
*so
, struct mbuf
*m
, int flags
)
1137 pru_send_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
1138 struct sockaddr
*addr
, struct mbuf
*control
,
1147 * This isn't really a ``null'' operation, but it's the default one
1148 * and doesn't do anything destructive.
1151 pru_sense_null(struct socket
*so
, struct stat
*sb
)
1153 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1158 int pru_sosend_notsupp(struct socket
*so
, struct sockaddr
*addr
,
1159 struct uio
*uio
, struct mbuf
*top
,
1160 struct mbuf
*control
, int flags
)
1166 int pru_soreceive_notsupp(struct socket
*so
,
1167 struct sockaddr
**paddr
,
1168 struct uio
*uio
, struct mbuf
**mp0
,
1169 struct mbuf
**controlp
, int *flagsp
)
1176 pru_shutdown_notsupp(struct socket
*so
)
1182 pru_sockaddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1187 int pru_sosend(struct socket
*so
, struct sockaddr
*addr
,
1188 struct uio
*uio
, struct mbuf
*top
,
1189 struct mbuf
*control
, int flags
)
1194 int pru_soreceive(struct socket
*so
,
1195 struct sockaddr
**paddr
,
1196 struct uio
*uio
, struct mbuf
**mp0
,
1197 struct mbuf
**controlp
, int *flagsp
)
1203 int pru_sopoll_notsupp(struct socket
*so
, int events
,
1212 * Do we need to notify the other side when I/O is possible?
1216 sb_notify(struct sockbuf
*sb
)
1218 return ((sb
->sb_flags
& (SB_WAIT
|SB_SEL
|SB_ASYNC
|SB_UPCALL
)) != 0);
1222 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1223 * This is problematical if the fields are unsigned, as the space might
1224 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1225 * overflow and return 0. Should use "lmin" but it doesn't exist now.
1228 sbspace(struct sockbuf
*sb
)
1230 return ((long) imin((int)(sb
->sb_hiwat
- sb
->sb_cc
),
1231 (int)(sb
->sb_mbmax
- sb
->sb_mbcnt
)));
1234 /* do we have to send all at once on a socket? */
1236 sosendallatonce(struct socket
*so
)
1238 return (so
->so_proto
->pr_flags
& PR_ATOMIC
);
1241 /* can we read something from so? */
1243 soreadable(struct socket
*so
)
1245 return (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1246 (so
->so_state
& SS_CANTRCVMORE
) ||
1247 so
->so_comp
.tqh_first
|| so
->so_error
);
1250 /* can we write something to so? */
1253 sowriteable(struct socket
*so
)
1255 return ((sbspace(&(so
)->so_snd
) >= (so
)->so_snd
.sb_lowat
&&
1256 ((so
->so_state
&SS_ISCONNECTED
) ||
1257 (so
->so_proto
->pr_flags
&PR_CONNREQUIRED
)==0)) ||
1258 (so
->so_state
& SS_CANTSENDMORE
) ||
1262 /* adjust counters in sb reflecting allocation of m */
1265 sballoc(struct sockbuf
*sb
, struct mbuf
*m
)
1267 sb
->sb_cc
+= m
->m_len
;
1268 sb
->sb_mbcnt
+= MSIZE
;
1269 if (m
->m_flags
& M_EXT
)
1270 sb
->sb_mbcnt
+= m
->m_ext
.ext_size
;
1273 /* adjust counters in sb reflecting freeing of m */
1275 sbfree(struct sockbuf
*sb
, struct mbuf
*m
)
1277 sb
->sb_cc
-= m
->m_len
;
1278 sb
->sb_mbcnt
-= MSIZE
;
1279 if (m
->m_flags
& M_EXT
)
1280 sb
->sb_mbcnt
-= m
->m_ext
.ext_size
;
1284 * Set lock on sockbuf sb; sleep if lock is already held.
1285 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1286 * Returns error without lock if sleep is interrupted.
1289 sblock(struct sockbuf
*sb
, int wf
)
1291 return(sb
->sb_flags
& SB_LOCK
?
1292 ((wf
== M_WAIT
) ? sb_lock(sb
) : EWOULDBLOCK
) :
1293 (sb
->sb_flags
|= SB_LOCK
), 0);
1296 /* release lock on sockbuf sb */
1298 sbunlock(struct sockbuf
*sb
)
1300 sb
->sb_flags
&= ~SB_LOCK
;
1301 if (sb
->sb_flags
& SB_WANT
) {
1302 sb
->sb_flags
&= ~SB_WANT
;
1303 wakeup((caddr_t
)&(sb
)->sb_flags
);
1308 sorwakeup(struct socket
* so
)
1310 if (sb_notify(&so
->so_rcv
))
1311 sowakeup(so
, &so
->so_rcv
);
1315 sowwakeup(struct socket
* so
)
1317 if (sb_notify(&so
->so_snd
))
1318 sowakeup(so
, &so
->so_snd
);
1322 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1325 dup_sockaddr(sa
, canwait
)
1326 struct sockaddr
*sa
;
1329 struct sockaddr
*sa2
;
1331 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1332 canwait
? M_WAITOK
: M_NOWAIT
);
1334 bcopy(sa
, sa2
, sa
->sa_len
);
1339 * Create an external-format (``xsocket'') structure using the information
1340 * in the kernel-format socket structure pointed to by so. This is done
1341 * to reduce the spew of irrelevant information over this interface,
1342 * to isolate user code from changes in the kernel structure, and
1343 * potentially to provide information-hiding if we decide that
1344 * some of this information should be hidden from users.
1347 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1349 xso
->xso_len
= sizeof *xso
;
1351 xso
->so_type
= so
->so_type
;
1352 xso
->so_options
= so
->so_options
;
1353 xso
->so_linger
= so
->so_linger
;
1354 xso
->so_state
= so
->so_state
;
1355 xso
->so_pcb
= so
->so_pcb
;
1356 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1357 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1358 xso
->so_qlen
= so
->so_qlen
;
1359 xso
->so_incqlen
= so
->so_incqlen
;
1360 xso
->so_qlimit
= so
->so_qlimit
;
1361 xso
->so_timeo
= so
->so_timeo
;
1362 xso
->so_error
= so
->so_error
;
1363 xso
->so_pgid
= so
->so_pgid
;
1364 xso
->so_oobmark
= so
->so_oobmark
;
1365 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1366 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1367 xso
->so_uid
= so
->so_uid
;
1371 * This does the same for sockbufs. Note that the xsockbuf structure,
1372 * since it is always embedded in a socket, does not include a self
1373 * pointer nor a length. We make this entry point public in case
1374 * some other mechanism needs it.
1377 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1379 xsb
->sb_cc
= sb
->sb_cc
;
1380 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1381 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1382 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1383 xsb
->sb_lowat
= sb
->sb_lowat
;
1384 xsb
->sb_flags
= sb
->sb_flags
;
1385 xsb
->sb_timeo
= sb
->sb_timeo
;
1389 * Here is the definition of some of the basic objects in the kern.ipc
1390 * branch of the MIB.
1394 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
, 0, "IPC");
1396 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1398 SYSCTL_INT(_kern
, KERN_DUMMY
, dummy
, CTLFLAG_RW
, &dummy
, 0, "");
1400 SYSCTL_INT(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLFLAG_RW
, &sb_max
, 0, "");
1401 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
, &maxsockets
, 0, "");
1402 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
,
1403 &sb_efficiency
, 0, "");
1404 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
, &nmbclusters
, 0, "");