2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 1982, 1986, 1988, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
35 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $
36 * $DragonFly: src/sys/kern/uipc_socket2.c,v 1.22 2005/07/23 07:28:34 dillon Exp $
39 #include "opt_param.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/file.h> /* for maxfiles */
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/resourcevar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/aio.h> /* for aio_swake proto */
56 #include <sys/event.h>
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
64 * Primitive routines for operating on sockets and socket buffers
67 u_long sb_max = SB_MAX;
69 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
74 * Procedures to manipulate state flags of socket
75 * and do appropriate wakeups. Normal sequence from the
76 * active (originating) side is that soisconnecting() is
77 * called during processing of connect() call,
78 * resulting in an eventual call to soisconnected() if/when the
79 * connection is established. When the connection is torn down
80 * soisdisconnecting() is called during processing of disconnect() call,
81 * and soisdisconnected() is called when the connection to the peer
82 * is totally severed. The semantics of these routines are such that
83 * connectionless protocols can call soisconnected() and soisdisconnected()
84 * only, bypassing the in-progress calls when setting up a ``connection''
87 * From the passive side, a socket is created with
88 * two queues of sockets: so_incomp for connections in progress
89 * and so_comp for connections already made and awaiting user acceptance.
90 * As a protocol is preparing incoming connections, it creates a socket
91 * structure queued on so_incomp by calling sonewconn(). When the connection
92 * is established, soisconnected() is called, and transfers the
93 * socket structure to so_comp, making it available to accept().
95 * If a socket is closed with sockets on either
96 * so_incomp or so_comp, these sockets are dropped.
98 * If higher level protocols are implemented in
99 * the kernel, the wakeups done here will sometimes
100 * cause software-interrupt process scheduling.
108 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
109 so->so_state |= SS_ISCONNECTING;
116 struct socket *head = so->so_head;
118 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
119 so->so_state |= SS_ISCONNECTED;
120 if (head && (so->so_state & SS_INCOMP)) {
121 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
122 so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
123 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
124 so->so_rcv.sb_flags |= SB_UPCALL;
125 so->so_options &= ~SO_ACCEPTFILTER;
126 so->so_upcall(so, so->so_upcallarg, 0);
129 TAILQ_REMOVE(&head->so_incomp, so, so_list);
131 so->so_state &= ~SS_INCOMP;
132 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
134 so->so_state |= SS_COMP;
136 wakeup_one(&head->so_timeo);
138 wakeup(&so->so_timeo);
145 soisdisconnecting(so)
149 so->so_state &= ~SS_ISCONNECTING;
150 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
151 wakeup((caddr_t)&so->so_timeo);
161 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
162 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
163 wakeup((caddr_t)&so->so_timeo);
164 sbdrop(&so->so_snd, so->so_snd.sb_cc);
170 * When an attempt at a new connection is noted on a socket
171 * which accepts connections, sonewconn is called. If the
172 * connection is possible (subject to space constraints, etc.)
173 * then we allocate a new structure, propoerly linked into the
174 * data structure of the original socket, and return this.
175 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
178 sonewconn(struct socket *head, int connstatus)
181 struct pru_attach_info ai;
183 if (head->so_qlen > 3 * head->so_qlimit / 2)
184 return ((struct socket *)0);
187 return ((struct socket *)0);
188 if ((head->so_options & SO_ACCEPTFILTER) != 0)
191 so->so_type = head->so_type;
192 so->so_options = head->so_options &~ SO_ACCEPTCONN;
193 so->so_linger = head->so_linger;
194 so->so_state = head->so_state | SS_NOFDREF;
195 so->so_proto = head->so_proto;
196 so->so_timeo = head->so_timeo;
197 so->so_cred = crhold(head->so_cred);
200 ai.fd_rdir = NULL; /* jail code cruft XXX JH */
201 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat, NULL) ||
202 /* Directly call function since we're already at protocol level. */
203 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, &ai)) {
205 return ((struct socket *)0);
209 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
210 so->so_state |= SS_COMP;
213 if (head->so_incqlen > head->so_qlimit) {
215 sp = TAILQ_FIRST(&head->so_incomp);
218 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
219 so->so_state |= SS_INCOMP;
224 wakeup((caddr_t)&head->so_timeo);
225 so->so_state |= connstatus;
231 * Socantsendmore indicates that no more data will be sent on the
232 * socket; it would normally be applied to a socket when the user
233 * informs the system that no more data is to be sent, by the protocol
234 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
235 * will be received, and will normally be applied to the socket by a
236 * protocol when it detects that the peer will send no more data.
237 * Data queued for reading in the socket may yet be read.
245 so->so_state |= SS_CANTSENDMORE;
254 so->so_state |= SS_CANTRCVMORE;
259 * Wait for data to arrive at/drain from a socket buffer.
266 sb->sb_flags |= SB_WAIT;
267 return (tsleep((caddr_t)&sb->sb_cc,
268 ((sb->sb_flags & SB_NOINTR) ? 0 : PCATCH),
274 * Lock a sockbuf already known to be locked;
275 * return any error returned from sleep (EINTR).
283 while (sb->sb_flags & SB_LOCK) {
284 sb->sb_flags |= SB_WANT;
285 error = tsleep((caddr_t)&sb->sb_flags,
286 ((sb->sb_flags & SB_NOINTR) ? 0 : PCATCH),
291 sb->sb_flags |= SB_LOCK;
296 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
297 * via SIGIO if the socket has the SS_ASYNC flag set.
304 struct selinfo *selinfo = &sb->sb_sel;
307 sb->sb_flags &= ~SB_SEL;
308 if (sb->sb_flags & SB_WAIT) {
309 sb->sb_flags &= ~SB_WAIT;
310 wakeup((caddr_t)&sb->sb_cc);
312 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
313 pgsigio(so->so_sigio, SIGIO, 0);
314 if (sb->sb_flags & SB_UPCALL)
315 (*so->so_upcall)(so, so->so_upcallarg, MB_DONTWAIT);
316 if (sb->sb_flags & SB_AIO)
318 KNOTE(&selinfo->si_note, 0);
319 if (sb->sb_flags & SB_MEVENT) {
320 struct netmsg_so_notify *msg, *nmsg;
322 TAILQ_FOREACH_MUTABLE(msg, &selinfo->si_mlist, nm_list, nmsg) {
323 if (msg->nm_predicate((struct netmsg *)msg)) {
324 TAILQ_REMOVE(&selinfo->si_mlist, msg, nm_list);
325 lwkt_replymsg(&msg->nm_lmsg,
326 msg->nm_lmsg.ms_error);
329 if (TAILQ_EMPTY(&sb->sb_sel.si_mlist))
330 sb->sb_flags &= ~SB_MEVENT;
335 * Socket buffer (struct sockbuf) utility routines.
337 * Each socket contains two socket buffers: one for sending data and
338 * one for receiving data. Each buffer contains a queue of mbufs,
339 * information about the number of mbufs and amount of data in the
340 * queue, and other fields allowing select() statements and notification
341 * on data availability to be implemented.
343 * Data stored in a socket buffer is maintained as a list of records.
344 * Each record is a list of mbufs chained together with the m_next
345 * field. Records are chained together with the m_nextpkt field. The upper
346 * level routine soreceive() expects the following conventions to be
347 * observed when placing information in the receive buffer:
349 * 1. If the protocol requires each message be preceded by the sender's
350 * name, then a record containing that name must be present before
351 * any associated data (mbuf's must be of type MT_SONAME).
352 * 2. If the protocol supports the exchange of ``access rights'' (really
353 * just additional data associated with the message), and there are
354 * ``rights'' to be received, then a record containing this data
355 * should be present (mbuf's must be of type MT_RIGHTS).
356 * 3. If a name or rights record exists, then it must be followed by
357 * a data record, perhaps of zero length.
359 * Before using a new socket structure it is first necessary to reserve
360 * buffer space to the socket, by calling sbreserve(). This should commit
361 * some of the available buffer space in the system buffer pool for the
362 * socket (currently, it does nothing but enforce limits). The space
363 * should be released by calling sbrelease() when the socket is destroyed.
367 soreserve(struct socket *so, u_long sndcc, u_long rcvcc, struct rlimit *rl)
369 if (sbreserve(&so->so_snd, sndcc, so, rl) == 0)
371 if (sbreserve(&so->so_rcv, rcvcc, so, rl) == 0)
373 if (so->so_rcv.sb_lowat == 0)
374 so->so_rcv.sb_lowat = 1;
375 if (so->so_snd.sb_lowat == 0)
376 so->so_snd.sb_lowat = MCLBYTES;
377 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
378 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
381 sbrelease(&so->so_snd, so);
387 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
390 u_long old_sb_max = sb_max;
392 error = SYSCTL_OUT(req, arg1, sizeof(int));
393 if (error || !req->newptr)
395 error = SYSCTL_IN(req, arg1, sizeof(int));
398 if (sb_max < MSIZE + MCLBYTES) {
402 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
407 * Allot mbufs to a sockbuf.
408 * Attempt to scale mbmax so that mbcnt doesn't become limiting
409 * if buffering efficiency is near the normal case.
412 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, struct rlimit *rl)
416 * rl will only be NULL when we're in an interrupt (eg, in tcp_input)
417 * or when called from netgraph (ie, ngd_attach)
421 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
422 rl ? rl->rlim_cur : RLIM_INFINITY)) {
425 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
426 if (sb->sb_lowat > sb->sb_hiwat)
427 sb->sb_lowat = sb->sb_hiwat;
432 * Free mbufs held by a socket, and reserved mbuf space.
441 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
447 * Routines to add and remove
448 * data from an mbuf queue.
450 * The routines sbappend() or sbappendrecord() are normally called to
451 * append new mbufs to a socket buffer, after checking that adequate
452 * space is available, comparing the function sbspace() with the amount
453 * of data to be added. sbappendrecord() differs from sbappend() in
454 * that data supplied is treated as the beginning of a new record.
455 * To place a sender's address, optional access rights, and data in a
456 * socket receive buffer, sbappendaddr() should be used. To place
457 * access rights and data in a socket receive buffer, sbappendrights()
458 * should be used. In either case, the new data begins a new record.
459 * Note that unlike sbappend() and sbappendrecord(), these routines check
460 * for the caller that there will be enough space to store the data.
461 * Each fails if there is not enough space, or if it cannot find mbufs
462 * to store additional information in.
464 * Reliable protocols may use the socket send buffer to hold data
465 * awaiting acknowledgement. Data is normally copied from a socket
466 * send buffer in a protocol with m_copy for output to a peer,
467 * and then removing the data from the socket buffer with sbdrop()
468 * or sbdroprecord() when the data is acknowledged by the peer.
472 * Append mbuf chain m to the last record in the
473 * socket buffer sb. The additional space associated
474 * the mbuf chain is recorded in sb. Empty mbufs are
475 * discarded and mbufs are compacted where possible.
478 sbappend(struct sockbuf *sb, struct mbuf *m)
488 if (n->m_flags & M_EOR) {
490 sbappendrecord(sb, m);
493 } while (n->m_next && (n = n->m_next));
495 sbcompress(sb, m, n);
500 * sbappendstream() is an optimized form of sbappend() for protocols
501 * such as TCP that only have one record in the socket buffer, are
502 * not PR_ATOMIC, nor allow MT_CONTROL data. A protocol that uses
503 * sbappendstream() must use sbappendstream() exclusively.
506 sbappendstream(struct sockbuf *sb, struct mbuf *m)
508 KKASSERT(m->m_nextpkt == NULL);
509 sbcompress(sb, m, sb->sb_lastmbuf);
515 _sbcheck(struct sockbuf *sb)
518 struct mbuf *n = NULL;
519 u_long len = 0, mbcnt = 0;
521 for (m = sb->sb_mb; m; m = n) {
523 if (n == NULL && sb->sb_lastrecord != m) {
524 printf("sockbuf %p mismatched lastrecord %p vs %p\n", sb, sb->sb_lastrecord, m);
528 for (; m; m = m->m_next) {
531 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
532 mbcnt += m->m_ext.ext_size;
533 if (n == NULL && m->m_next == NULL) {
534 if (sb->sb_lastmbuf != m) {
535 printf("sockbuf %p mismatched lastmbuf %p vs %p\n", sb, sb->sb_lastmbuf, m);
541 if (sb->sb_mb == NULL) {
542 if (sb->sb_lastrecord != NULL) {
543 printf("sockbuf %p is empty, lastrecord not NULL: %p\n",
544 sb, sb->sb_lastrecord);
547 if (sb->sb_lastmbuf != NULL) {
548 printf("sockbuf %p is empty, lastmbuf not NULL: %p\n",
549 sb, sb->sb_lastmbuf);
553 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
554 printf("sockbuf %p cc %ld != %ld || mbcnt %ld != %ld\n",
555 sb, len, sb->sb_cc, mbcnt, sb->sb_mbcnt);
563 * Same as sbappend(), except the mbuf chain begins a new record.
566 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
568 struct mbuf *firstmbuf;
569 struct mbuf *secondmbuf;
577 * Break the first mbuf off from the rest of the mbuf chain.
580 secondmbuf = m0->m_next;
584 * Insert the first mbuf of the m0 mbuf chain as the last record of
585 * the sockbuf. Note this permits zero length records! Keep the
586 * sockbuf state consistent.
588 if (sb->sb_mb == NULL)
589 sb->sb_mb = firstmbuf;
591 sb->sb_lastrecord->m_nextpkt = firstmbuf;
592 sb->sb_lastrecord = firstmbuf; /* update hint for new last record */
593 sb->sb_lastmbuf = firstmbuf; /* update hint for new last mbuf */
595 if ((firstmbuf->m_flags & M_EOR) && (secondmbuf != NULL)) {
596 /* propagate the EOR flag */
597 firstmbuf->m_flags &= ~M_EOR;
598 secondmbuf->m_flags |= M_EOR;
602 * The succeeding call to sbcompress() omits accounting for
603 * the first mbuf, so do it here.
605 sballoc(sb, firstmbuf);
607 /* Compact the rest of the mbuf chain in after the first mbuf. */
608 sbcompress(sb, secondmbuf, firstmbuf);
613 * As above except that OOB data is inserted at the beginning of the sockbuf,
614 * but after any other OOB data.
617 sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
624 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
630 continue; /* WANT next train */
635 goto again; /* inspect THIS train further */
640 * Put the first mbuf on the queue.
641 * Note this permits zero length records.
646 if (m0->m_nextpkt == NULL)
647 sb->sb_lastrecord = m0;
651 if (m && (m0->m_flags & M_EOR)) {
652 m0->m_flags &= ~M_EOR;
655 sbcompress(sb, m, m0);
660 * Append address and data, and optionally, control (ancillary) data
661 * to the receive queue of a socket. If present,
662 * m0 must include a packet header with total length.
663 * Returns 0 if no space in sockbuf or insufficient mbufs.
666 sbappendaddr(sb, asa, m0, control)
668 const struct sockaddr *asa;
669 struct mbuf *m0, *control;
672 int space = asa->sa_len;
674 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
675 panic("sbappendaddr");
679 space += m0->m_pkthdr.len;
680 for (n = control; n; n = n->m_next) {
682 if (n->m_next == 0) /* keep pointer to last control buf */
685 if (space > sbspace(sb))
687 if (asa->sa_len > MLEN)
689 MGET(m, MB_DONTWAIT, MT_SONAME);
692 KKASSERT(m->m_nextpkt == NULL);
693 m->m_len = asa->sa_len;
694 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
696 n->m_next = m0; /* concatenate data to control */
700 for (n = m; n; n = n->m_next)
703 if (sb->sb_mb == NULL)
706 sb->sb_lastrecord->m_nextpkt = m;
707 sb->sb_lastrecord = m;
716 * Append control information followed by data.
717 * control must be non-null.
720 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
723 u_int length, cmbcnt, m0mbcnt;
725 KASSERT(control != NULL, ("sbappendcontrol"));
726 KKASSERT(control->m_nextpkt == NULL);
729 length = m_countm(control, &n, &cmbcnt) + m_countm(m0, NULL, &m0mbcnt);
730 if (length > sbspace(sb))
733 n->m_next = m0; /* concatenate data to control */
735 if (sb->sb_mb == NULL)
738 sb->sb_lastrecord->m_nextpkt = control;
739 sb->sb_lastrecord = control;
740 sb->sb_lastmbuf = m0;
743 sb->sb_mbcnt += cmbcnt + m0mbcnt;
749 * Compress mbuf chain m into the socket buffer sb following mbuf tailm.
750 * If tailm is null, the buffer is presumed empty. Also, as a side-effect,
751 * increment the sockbuf counts for each mbuf in the chain.
754 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *tailm)
757 struct mbuf *free_chain = NULL;
763 eor |= m->m_flags & M_EOR;
765 * Disregard empty mbufs as long as we don't encounter
766 * an end-of-record or there is a trailing mbuf of
767 * the same type to propagate the EOR flag to.
769 * Defer the m_free() call because it can block and break
770 * the atomicy of the sockbuf.
774 (((o = m->m_next) || (o = tailm)) &&
775 o->m_type == m->m_type))) {
777 m->m_next = free_chain;
783 /* See if we can coalesce with preceding mbuf. */
784 if (tailm && !(tailm->m_flags & M_EOR) && M_WRITABLE(tailm) &&
785 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
786 m->m_len <= M_TRAILINGSPACE(tailm) &&
787 tailm->m_type == m->m_type) {
788 bcopy(mtod(m, caddr_t),
789 mtod(tailm, caddr_t) + tailm->m_len,
791 tailm->m_len += m->m_len;
792 sb->sb_cc += m->m_len; /* update sb counter */
794 m->m_next = free_chain;
800 /* Insert whole mbuf. */
802 KASSERT(sb->sb_mb == NULL,
803 ("sbcompress: sb_mb not NULL"));
804 sb->sb_mb = m; /* only mbuf in sockbuf */
805 sb->sb_lastrecord = m; /* new last record */
807 tailm->m_next = m; /* tack m on following tailm */
809 sb->sb_lastmbuf = m; /* update last mbuf hint */
811 tailm = m; /* just inserted mbuf becomes the new tail */
812 m = m->m_next; /* advance to next mbuf */
813 tailm->m_next = NULL; /* split inserted mbuf off from chain */
815 /* update sb counters for just added mbuf */
818 /* clear EOR on intermediate mbufs */
819 tailm->m_flags &= ~M_EOR;
823 * Propogate EOR to the last mbuf
827 tailm->m_flags |= eor;
829 printf("semi-panic: sbcompress");
833 * Clean up any defered frees.
836 free_chain = m_free(free_chain);
842 * Free all mbufs in a sockbuf.
843 * Check that all resources are reclaimed.
850 if (sb->sb_flags & SB_LOCK)
851 panic("sbflush: locked");
852 while (sb->sb_mbcnt) {
854 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
855 * we would loop forever. Panic instead.
857 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
859 sbdrop(sb, (int)sb->sb_cc);
861 KASSERT(!(sb->sb_cc || sb->sb_mb || sb->sb_mbcnt || sb->sb_lastmbuf),
862 ("sbflush: cc %ld || mb %p || mbcnt %ld || lastmbuf %p",
863 sb->sb_cc, sb->sb_mb, sb->sb_mbcnt, sb->sb_lastmbuf));
867 * Drop data from (the front of) a sockbuf.
875 struct mbuf *free_chain = NULL;
881 * Remove mbufs from multiple records until the count is exhausted.
884 while (m && len > 0) {
885 if (m->m_len > len) {
892 m = sbunlinkmbuf(sb, m, &free_chain);
893 if (m == NULL && len)
898 * Remove any trailing 0-length mbufs in the current record. If
899 * the last record for which data was removed is now empty, m will be
902 while (m && m->m_len == 0) {
903 m = sbunlinkmbuf(sb, m, &free_chain);
912 * Drop a record off the front of a sockbuf and move the next record
915 * Must be called while holding a critical section.
927 if ((sb->sb_mb = m->m_nextpkt) == NULL) {
928 sb->sb_lastrecord = NULL;
929 sb->sb_lastmbuf = NULL;
932 for (n = m; n; n = n->m_next)
940 * Drop the first mbuf off the sockbuf and move the next mbuf to the front.
941 * Currently only the head mbuf of the sockbuf may be dropped this way.
943 * The next mbuf in the same record as the mbuf being removed is returned
944 * or NULL if the record is exhausted. Note that other records may remain
945 * in the sockbuf when NULL is returned.
947 * Must be called while holding a critical section.
950 sbunlinkmbuf(struct sockbuf *sb, struct mbuf *m, struct mbuf **free_chain)
954 KKASSERT(sb->sb_mb == m);
959 if (sb->sb_lastrecord == m)
960 sb->sb_lastrecord = n;
961 KKASSERT(sb->sb_lastmbuf != m);
962 n->m_nextpkt = m->m_nextpkt;
964 sb->sb_mb = m->m_nextpkt;
965 if (sb->sb_lastrecord == m) {
966 KKASSERT(sb->sb_mb == NULL);
967 sb->sb_lastrecord = NULL;
969 if (sb->sb_mb == NULL)
970 sb->sb_lastmbuf = NULL;
974 m->m_next = *free_chain;
983 * Create a "control" mbuf containing the specified data
984 * with the specified type for presentation on a socket buffer.
987 sbcreatecontrol(p, size, type, level)
995 if (CMSG_SPACE((u_int)size) > MCLBYTES)
997 m = m_getl(CMSG_SPACE((u_int)size), MB_DONTWAIT, MT_CONTROL, 0, NULL);
1000 m->m_len = CMSG_SPACE(size);
1001 cp = mtod(m, struct cmsghdr *);
1003 memcpy(CMSG_DATA(cp), p, size);
1004 cp->cmsg_len = CMSG_LEN(size);
1005 cp->cmsg_level = level;
1006 cp->cmsg_type = type;
1011 * Some routines that return EOPNOTSUPP for entry points that are not
1012 * supported by a protocol. Fill in as needed.
1015 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1021 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1027 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1033 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1034 struct ifnet *ifp, struct thread *td)
1040 pru_listen_notsupp(struct socket *so, struct thread *td)
1046 pru_rcvd_notsupp(struct socket *so, int flags)
1052 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1058 * This isn't really a ``null'' operation, but it's the default one
1059 * and doesn't do anything destructive.
1062 pru_sense_null(struct socket *so, struct stat *sb)
1064 sb->st_blksize = so->so_snd.sb_hiwat;
1069 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. Callers
1070 * of this routine assume that it always succeeds, so we have to use a
1071 * blockable allocation even though we might be called from a critical thread.
1074 dup_sockaddr(const struct sockaddr *sa)
1076 struct sockaddr *sa2;
1078 sa2 = malloc(sa->sa_len, M_SONAME, M_INTWAIT);
1079 bcopy(sa, sa2, sa->sa_len);
1084 * Create an external-format (``xsocket'') structure using the information
1085 * in the kernel-format socket structure pointed to by so. This is done
1086 * to reduce the spew of irrelevant information over this interface,
1087 * to isolate user code from changes in the kernel structure, and
1088 * potentially to provide information-hiding if we decide that
1089 * some of this information should be hidden from users.
1092 sotoxsocket(struct socket *so, struct xsocket *xso)
1094 xso->xso_len = sizeof *xso;
1096 xso->so_type = so->so_type;
1097 xso->so_options = so->so_options;
1098 xso->so_linger = so->so_linger;
1099 xso->so_state = so->so_state;
1100 xso->so_pcb = so->so_pcb;
1101 xso->xso_protocol = so->so_proto->pr_protocol;
1102 xso->xso_family = so->so_proto->pr_domain->dom_family;
1103 xso->so_qlen = so->so_qlen;
1104 xso->so_incqlen = so->so_incqlen;
1105 xso->so_qlimit = so->so_qlimit;
1106 xso->so_timeo = so->so_timeo;
1107 xso->so_error = so->so_error;
1108 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
1109 xso->so_oobmark = so->so_oobmark;
1110 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1111 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1112 xso->so_uid = so->so_cred->cr_uid;
1116 * This does the same for sockbufs. Note that the xsockbuf structure,
1117 * since it is always embedded in a socket, does not include a self
1118 * pointer nor a length. We make this entry point public in case
1119 * some other mechanism needs it.
1122 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1124 xsb->sb_cc = sb->sb_cc;
1125 xsb->sb_hiwat = sb->sb_hiwat;
1126 xsb->sb_mbcnt = sb->sb_mbcnt;
1127 xsb->sb_mbmax = sb->sb_mbmax;
1128 xsb->sb_lowat = sb->sb_lowat;
1129 xsb->sb_flags = sb->sb_flags;
1130 xsb->sb_timeo = sb->sb_timeo;
1134 * Here is the definition of some of the basic objects in the kern.ipc
1135 * branch of the MIB.
1137 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1139 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1141 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1142 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
1143 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
1144 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
1145 &maxsockets, 0, "Maximum number of sockets avaliable");
1146 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1147 &sb_efficiency, 0, "");
1150 * Initialise maxsockets
1152 static void init_maxsockets(void *ignored)
1154 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1155 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1157 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);