xref: /freebsd/sys/kern/uipc_sockbuf.c (revision b2037136946df4b5c4e03d856b67c195a987bcdd)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1569a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
375b86eac4SJesper Skriver #include "opt_param.h"
38335654d7SRobert Watson 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */
41ff5c09daSGarrett Wollman #include <sys/kernel.h>
42fb919e4dSMark Murray #include <sys/lock.h>
438ec07310SGleb Smirnoff #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45960ed29cSSeigo Tanimura #include <sys/mutex.h>
46fb919e4dSMark Murray #include <sys/proc.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
482f9a2132SBrian Feldman #include <sys/resourcevar.h>
49960ed29cSSeigo Tanimura #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
527abab911SRobert Watson #include <sys/sx.h>
53ff5c09daSGarrett Wollman #include <sys/sysctl.h>
5426f9a767SRodney W. Grimes 
55f14cce87SRobert Watson /*
56f14cce87SRobert Watson  * Function pointer set by the AIO routines so that the socket buffer code
57f14cce87SRobert Watson  * can call back into the AIO module if it is loaded.
58f14cce87SRobert Watson  */
5921d56e9cSAlfred Perlstein void	(*aio_swake)(struct socket *, struct sockbuf *);
6021d56e9cSAlfred Perlstein 
61df8bae1dSRodney W. Grimes /*
62f14cce87SRobert Watson  * Primitive routines for operating on socket buffers
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
6579cb7eb4SDavid Greenman u_long	sb_max = SB_MAX;
6658d14daeSMohan Srinivasan u_long sb_max_adj =
67b233773bSBjoern A. Zeeb        (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
68df8bae1dSRodney W. Grimes 
694b29bc4fSGarrett Wollman static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
704b29bc4fSGarrett Wollman 
711d2df300SGleb Smirnoff static struct mbuf	*sbcut_internal(struct sockbuf *sb, int len);
72050ac265SRobert Watson static void	sbflush_internal(struct sockbuf *sb);
73eaa6dfbcSRobert Watson 
74df8bae1dSRodney W. Grimes /*
75829fae90SGleb Smirnoff  * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
76829fae90SGleb Smirnoff  */
77829fae90SGleb Smirnoff static void
78829fae90SGleb Smirnoff sbm_clrprotoflags(struct mbuf *m, int flags)
79829fae90SGleb Smirnoff {
80829fae90SGleb Smirnoff 	int mask;
81829fae90SGleb Smirnoff 
82829fae90SGleb Smirnoff 	mask = ~M_PROTOFLAGS;
83829fae90SGleb Smirnoff 	if (flags & PRUS_NOTREADY)
84829fae90SGleb Smirnoff 		mask |= M_NOTREADY;
85829fae90SGleb Smirnoff 	while (m) {
86829fae90SGleb Smirnoff 		m->m_flags &= mask;
87829fae90SGleb Smirnoff 		m = m->m_next;
88829fae90SGleb Smirnoff 	}
89829fae90SGleb Smirnoff }
90829fae90SGleb Smirnoff 
91829fae90SGleb Smirnoff /*
920f9d0a73SGleb Smirnoff  * Mark ready "count" mbufs starting with "m".
930f9d0a73SGleb Smirnoff  */
940f9d0a73SGleb Smirnoff int
950f9d0a73SGleb Smirnoff sbready(struct sockbuf *sb, struct mbuf *m, int count)
960f9d0a73SGleb Smirnoff {
970f9d0a73SGleb Smirnoff 	u_int blocker;
980f9d0a73SGleb Smirnoff 
990f9d0a73SGleb Smirnoff 	SOCKBUF_LOCK_ASSERT(sb);
1000f9d0a73SGleb Smirnoff 	KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
1010f9d0a73SGleb Smirnoff 
1020f9d0a73SGleb Smirnoff 	blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
1030f9d0a73SGleb Smirnoff 
1040f9d0a73SGleb Smirnoff 	for (int i = 0; i < count; i++, m = m->m_next) {
1050f9d0a73SGleb Smirnoff 		KASSERT(m->m_flags & M_NOTREADY,
1060f9d0a73SGleb Smirnoff 		    ("%s: m %p !M_NOTREADY", __func__, m));
1070f9d0a73SGleb Smirnoff 		m->m_flags &= ~(M_NOTREADY | blocker);
1080f9d0a73SGleb Smirnoff 		if (blocker)
1090f9d0a73SGleb Smirnoff 			sb->sb_acc += m->m_len;
1100f9d0a73SGleb Smirnoff 	}
1110f9d0a73SGleb Smirnoff 
1120f9d0a73SGleb Smirnoff 	if (!blocker)
1130f9d0a73SGleb Smirnoff 		return (EINPROGRESS);
1140f9d0a73SGleb Smirnoff 
1150f9d0a73SGleb Smirnoff 	/* This one was blocking all the queue. */
1160f9d0a73SGleb Smirnoff 	for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
1170f9d0a73SGleb Smirnoff 		KASSERT(m->m_flags & M_BLOCKED,
1180f9d0a73SGleb Smirnoff 		    ("%s: m %p !M_BLOCKED", __func__, m));
1190f9d0a73SGleb Smirnoff 		m->m_flags &= ~M_BLOCKED;
1200f9d0a73SGleb Smirnoff 		sb->sb_acc += m->m_len;
1210f9d0a73SGleb Smirnoff 	}
1220f9d0a73SGleb Smirnoff 
1230f9d0a73SGleb Smirnoff 	sb->sb_fnrdy = m;
1240f9d0a73SGleb Smirnoff 
1250f9d0a73SGleb Smirnoff 	return (0);
1260f9d0a73SGleb Smirnoff }
1270f9d0a73SGleb Smirnoff 
1280f9d0a73SGleb Smirnoff /*
1298967b220SGleb Smirnoff  * Adjust sockbuf state reflecting allocation of m.
1308967b220SGleb Smirnoff  */
1318967b220SGleb Smirnoff void
1328967b220SGleb Smirnoff sballoc(struct sockbuf *sb, struct mbuf *m)
1338967b220SGleb Smirnoff {
1348967b220SGleb Smirnoff 
1358967b220SGleb Smirnoff 	SOCKBUF_LOCK_ASSERT(sb);
1368967b220SGleb Smirnoff 
1370f9d0a73SGleb Smirnoff 	sb->sb_ccc += m->m_len;
1380f9d0a73SGleb Smirnoff 
1390f9d0a73SGleb Smirnoff 	if (sb->sb_fnrdy == NULL) {
1400f9d0a73SGleb Smirnoff 		if (m->m_flags & M_NOTREADY)
1410f9d0a73SGleb Smirnoff 			sb->sb_fnrdy = m;
1420f9d0a73SGleb Smirnoff 		else
1430f9d0a73SGleb Smirnoff 			sb->sb_acc += m->m_len;
1440f9d0a73SGleb Smirnoff 	} else
1450f9d0a73SGleb Smirnoff 		m->m_flags |= M_BLOCKED;
1468967b220SGleb Smirnoff 
1478967b220SGleb Smirnoff 	if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1488967b220SGleb Smirnoff 		sb->sb_ctl += m->m_len;
1498967b220SGleb Smirnoff 
1508967b220SGleb Smirnoff 	sb->sb_mbcnt += MSIZE;
1518967b220SGleb Smirnoff 	sb->sb_mcnt += 1;
1528967b220SGleb Smirnoff 
1538967b220SGleb Smirnoff 	if (m->m_flags & M_EXT) {
1548967b220SGleb Smirnoff 		sb->sb_mbcnt += m->m_ext.ext_size;
1558967b220SGleb Smirnoff 		sb->sb_ccnt += 1;
1568967b220SGleb Smirnoff 	}
1578967b220SGleb Smirnoff }
1588967b220SGleb Smirnoff 
1598967b220SGleb Smirnoff /*
1608967b220SGleb Smirnoff  * Adjust sockbuf state reflecting freeing of m.
1618967b220SGleb Smirnoff  */
1628967b220SGleb Smirnoff void
1638967b220SGleb Smirnoff sbfree(struct sockbuf *sb, struct mbuf *m)
1648967b220SGleb Smirnoff {
1658967b220SGleb Smirnoff 
1668967b220SGleb Smirnoff #if 0	/* XXX: not yet: soclose() call path comes here w/o lock. */
1678967b220SGleb Smirnoff 	SOCKBUF_LOCK_ASSERT(sb);
1688967b220SGleb Smirnoff #endif
1698967b220SGleb Smirnoff 
1700f9d0a73SGleb Smirnoff 	sb->sb_ccc -= m->m_len;
1710f9d0a73SGleb Smirnoff 
1720f9d0a73SGleb Smirnoff 	if (!(m->m_flags & M_NOTAVAIL))
1730f9d0a73SGleb Smirnoff 		sb->sb_acc -= m->m_len;
1740f9d0a73SGleb Smirnoff 
1750f9d0a73SGleb Smirnoff 	if (m == sb->sb_fnrdy) {
1760f9d0a73SGleb Smirnoff 		struct mbuf *n;
1770f9d0a73SGleb Smirnoff 
1780f9d0a73SGleb Smirnoff 		KASSERT(m->m_flags & M_NOTREADY,
1790f9d0a73SGleb Smirnoff 		    ("%s: m %p !M_NOTREADY", __func__, m));
1800f9d0a73SGleb Smirnoff 
1810f9d0a73SGleb Smirnoff 		n = m->m_next;
1820f9d0a73SGleb Smirnoff 		while (n != NULL && !(n->m_flags & M_NOTREADY)) {
1830f9d0a73SGleb Smirnoff 			n->m_flags &= ~M_BLOCKED;
1840f9d0a73SGleb Smirnoff 			sb->sb_acc += n->m_len;
1850f9d0a73SGleb Smirnoff 			n = n->m_next;
1860f9d0a73SGleb Smirnoff 		}
1870f9d0a73SGleb Smirnoff 		sb->sb_fnrdy = n;
1880f9d0a73SGleb Smirnoff 	}
1898967b220SGleb Smirnoff 
1908967b220SGleb Smirnoff 	if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1918967b220SGleb Smirnoff 		sb->sb_ctl -= m->m_len;
1928967b220SGleb Smirnoff 
1938967b220SGleb Smirnoff 	sb->sb_mbcnt -= MSIZE;
1948967b220SGleb Smirnoff 	sb->sb_mcnt -= 1;
1958967b220SGleb Smirnoff 	if (m->m_flags & M_EXT) {
1968967b220SGleb Smirnoff 		sb->sb_mbcnt -= m->m_ext.ext_size;
1978967b220SGleb Smirnoff 		sb->sb_ccnt -= 1;
1988967b220SGleb Smirnoff 	}
1998967b220SGleb Smirnoff 
2008967b220SGleb Smirnoff 	if (sb->sb_sndptr == m) {
2018967b220SGleb Smirnoff 		sb->sb_sndptr = NULL;
2028967b220SGleb Smirnoff 		sb->sb_sndptroff = 0;
2038967b220SGleb Smirnoff 	}
2048967b220SGleb Smirnoff 	if (sb->sb_sndptroff != 0)
2058967b220SGleb Smirnoff 		sb->sb_sndptroff -= m->m_len;
2068967b220SGleb Smirnoff }
2078967b220SGleb Smirnoff 
2088967b220SGleb Smirnoff /*
209050ac265SRobert Watson  * Socantsendmore indicates that no more data will be sent on the socket; it
210050ac265SRobert Watson  * would normally be applied to a socket when the user informs the system
211050ac265SRobert Watson  * that no more data is to be sent, by the protocol code (in case
212050ac265SRobert Watson  * PRU_SHUTDOWN).  Socantrcvmore indicates that no more data will be
213050ac265SRobert Watson  * received, and will normally be applied to the socket by a protocol when it
214050ac265SRobert Watson  * detects that the peer will send no more data.  Data queued for reading in
215050ac265SRobert Watson  * the socket may yet be read.
216df8bae1dSRodney W. Grimes  */
217a34b7046SRobert Watson void
218050ac265SRobert Watson socantsendmore_locked(struct socket *so)
219a34b7046SRobert Watson {
220a34b7046SRobert Watson 
221a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
222a34b7046SRobert Watson 
223a34b7046SRobert Watson 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
224a34b7046SRobert Watson 	sowwakeup_locked(so);
225a34b7046SRobert Watson 	mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
226a34b7046SRobert Watson }
227df8bae1dSRodney W. Grimes 
22826f9a767SRodney W. Grimes void
229050ac265SRobert Watson socantsendmore(struct socket *so)
230df8bae1dSRodney W. Grimes {
231df8bae1dSRodney W. Grimes 
232a34b7046SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
233a34b7046SRobert Watson 	socantsendmore_locked(so);
234a34b7046SRobert Watson 	mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
235a34b7046SRobert Watson }
236a34b7046SRobert Watson 
237a34b7046SRobert Watson void
238050ac265SRobert Watson socantrcvmore_locked(struct socket *so)
239a34b7046SRobert Watson {
240a34b7046SRobert Watson 
241a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
242a34b7046SRobert Watson 
243a34b7046SRobert Watson 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
244a34b7046SRobert Watson 	sorwakeup_locked(so);
245a34b7046SRobert Watson 	mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
246df8bae1dSRodney W. Grimes }
247df8bae1dSRodney W. Grimes 
24826f9a767SRodney W. Grimes void
249050ac265SRobert Watson socantrcvmore(struct socket *so)
250df8bae1dSRodney W. Grimes {
251df8bae1dSRodney W. Grimes 
252a34b7046SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
253a34b7046SRobert Watson 	socantrcvmore_locked(so);
254a34b7046SRobert Watson 	mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
255df8bae1dSRodney W. Grimes }
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes /*
258df8bae1dSRodney W. Grimes  * Wait for data to arrive at/drain from a socket buffer.
259df8bae1dSRodney W. Grimes  */
26026f9a767SRodney W. Grimes int
261050ac265SRobert Watson sbwait(struct sockbuf *sb)
262df8bae1dSRodney W. Grimes {
263df8bae1dSRodney W. Grimes 
26431f555a1SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
26531f555a1SRobert Watson 
266df8bae1dSRodney W. Grimes 	sb->sb_flags |= SB_WAIT;
2670f9d0a73SGleb Smirnoff 	return (msleep_sbt(&sb->sb_acc, &sb->sb_mtx,
26847daf5d5SBruce Evans 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
2697729cbf1SDavide Italiano 	    sb->sb_timeo, 0, 0));
270df8bae1dSRodney W. Grimes }
271df8bae1dSRodney W. Grimes 
27226f9a767SRodney W. Grimes int
2737abab911SRobert Watson sblock(struct sockbuf *sb, int flags)
274df8bae1dSRodney W. Grimes {
275df8bae1dSRodney W. Grimes 
276265de5bbSRobert Watson 	KASSERT((flags & SBL_VALID) == flags,
277265de5bbSRobert Watson 	    ("sblock: flags invalid (0x%x)", flags));
278265de5bbSRobert Watson 
279265de5bbSRobert Watson 	if (flags & SBL_WAIT) {
280265de5bbSRobert Watson 		if ((sb->sb_flags & SB_NOINTR) ||
281265de5bbSRobert Watson 		    (flags & SBL_NOINTR)) {
2827abab911SRobert Watson 			sx_xlock(&sb->sb_sx);
283df8bae1dSRodney W. Grimes 			return (0);
284049c3b6cSRobert Watson 		}
285049c3b6cSRobert Watson 		return (sx_xlock_sig(&sb->sb_sx));
2867abab911SRobert Watson 	} else {
2877abab911SRobert Watson 		if (sx_try_xlock(&sb->sb_sx) == 0)
2887abab911SRobert Watson 			return (EWOULDBLOCK);
2897abab911SRobert Watson 		return (0);
2907abab911SRobert Watson 	}
2917abab911SRobert Watson }
2927abab911SRobert Watson 
2937abab911SRobert Watson void
2947abab911SRobert Watson sbunlock(struct sockbuf *sb)
2957abab911SRobert Watson {
2967abab911SRobert Watson 
2977abab911SRobert Watson 	sx_xunlock(&sb->sb_sx);
298df8bae1dSRodney W. Grimes }
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes /*
301050ac265SRobert Watson  * Wakeup processes waiting on a socket buffer.  Do asynchronous notification
302050ac265SRobert Watson  * via SIGIO if the socket has the SS_ASYNC flag set.
303a34b7046SRobert Watson  *
304a34b7046SRobert Watson  * Called with the socket buffer lock held; will release the lock by the end
305a34b7046SRobert Watson  * of the function.  This allows the caller to acquire the socket buffer lock
306a34b7046SRobert Watson  * while testing for the need for various sorts of wakeup and hold it through
307a34b7046SRobert Watson  * to the point where it's no longer required.  We currently hold the lock
308a34b7046SRobert Watson  * through calls out to other subsystems (with the exception of kqueue), and
309a34b7046SRobert Watson  * then release it to avoid lock order issues.  It's not clear that's
310a34b7046SRobert Watson  * correct.
311df8bae1dSRodney W. Grimes  */
31226f9a767SRodney W. Grimes void
313050ac265SRobert Watson sowakeup(struct socket *so, struct sockbuf *sb)
314df8bae1dSRodney W. Grimes {
31574fb0ba7SJohn Baldwin 	int ret;
316d48d4b25SSeigo Tanimura 
317a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
318a34b7046SRobert Watson 
319779f106aSGleb Smirnoff 	selwakeuppri(sb->sb_sel, PSOCK);
320779f106aSGleb Smirnoff 	if (!SEL_WAITING(sb->sb_sel))
321df8bae1dSRodney W. Grimes 		sb->sb_flags &= ~SB_SEL;
322df8bae1dSRodney W. Grimes 	if (sb->sb_flags & SB_WAIT) {
323df8bae1dSRodney W. Grimes 		sb->sb_flags &= ~SB_WAIT;
3240f9d0a73SGleb Smirnoff 		wakeup(&sb->sb_acc);
325df8bae1dSRodney W. Grimes 	}
326779f106aSGleb Smirnoff 	KNOTE_LOCKED(&sb->sb_sel->si_note, 0);
32798c92369SNavdeep Parhar 	if (sb->sb_upcall != NULL) {
328eb1b1807SGleb Smirnoff 		ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
32974fb0ba7SJohn Baldwin 		if (ret == SU_ISCONNECTED) {
33074fb0ba7SJohn Baldwin 			KASSERT(sb == &so->so_rcv,
33174fb0ba7SJohn Baldwin 			    ("SO_SND upcall returned SU_ISCONNECTED"));
33274fb0ba7SJohn Baldwin 			soupcall_clear(so, SO_RCV);
33374fb0ba7SJohn Baldwin 		}
33474fb0ba7SJohn Baldwin 	} else
33574fb0ba7SJohn Baldwin 		ret = SU_OK;
3364cc20ab1SSeigo Tanimura 	if (sb->sb_flags & SB_AIO)
337f3215338SJohn Baldwin 		sowakeup_aio(so, sb);
33874fb0ba7SJohn Baldwin 	SOCKBUF_UNLOCK(sb);
339555b3e2fSGleb Smirnoff 	if (ret == SU_ISCONNECTED)
34074fb0ba7SJohn Baldwin 		soisconnected(so);
34174fb0ba7SJohn Baldwin 	if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
34274fb0ba7SJohn Baldwin 		pgsigio(&so->so_sigio, SIGIO, 0);
343a34b7046SRobert Watson 	mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
344df8bae1dSRodney W. Grimes }
345df8bae1dSRodney W. Grimes 
346df8bae1dSRodney W. Grimes /*
347df8bae1dSRodney W. Grimes  * Socket buffer (struct sockbuf) utility routines.
348df8bae1dSRodney W. Grimes  *
349050ac265SRobert Watson  * Each socket contains two socket buffers: one for sending data and one for
350050ac265SRobert Watson  * receiving data.  Each buffer contains a queue of mbufs, information about
351050ac265SRobert Watson  * the number of mbufs and amount of data in the queue, and other fields
352050ac265SRobert Watson  * allowing select() statements and notification on data availability to be
353050ac265SRobert Watson  * implemented.
354df8bae1dSRodney W. Grimes  *
355050ac265SRobert Watson  * Data stored in a socket buffer is maintained as a list of records.  Each
356050ac265SRobert Watson  * record is a list of mbufs chained together with the m_next field.  Records
357050ac265SRobert Watson  * are chained together with the m_nextpkt field. The upper level routine
358050ac265SRobert Watson  * soreceive() expects the following conventions to be observed when placing
359050ac265SRobert Watson  * information in the receive buffer:
360df8bae1dSRodney W. Grimes  *
361050ac265SRobert Watson  * 1. If the protocol requires each message be preceded by the sender's name,
362050ac265SRobert Watson  *    then a record containing that name must be present before any
363050ac265SRobert Watson  *    associated data (mbuf's must be of type MT_SONAME).
364050ac265SRobert Watson  * 2. If the protocol supports the exchange of ``access rights'' (really just
365050ac265SRobert Watson  *    additional data associated with the message), and there are ``rights''
366050ac265SRobert Watson  *    to be received, then a record containing this data should be present
367050ac265SRobert Watson  *    (mbuf's must be of type MT_RIGHTS).
368050ac265SRobert Watson  * 3. If a name or rights record exists, then it must be followed by a data
369050ac265SRobert Watson  *    record, perhaps of zero length.
370df8bae1dSRodney W. Grimes  *
371df8bae1dSRodney W. Grimes  * Before using a new socket structure it is first necessary to reserve
372df8bae1dSRodney W. Grimes  * buffer space to the socket, by calling sbreserve().  This should commit
373df8bae1dSRodney W. Grimes  * some of the available buffer space in the system buffer pool for the
374050ac265SRobert Watson  * socket (currently, it does nothing but enforce limits).  The space should
375050ac265SRobert Watson  * be released by calling sbrelease() when the socket is destroyed.
376df8bae1dSRodney W. Grimes  */
37726f9a767SRodney W. Grimes int
378050ac265SRobert Watson soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
379df8bae1dSRodney W. Grimes {
380b40ce416SJulian Elischer 	struct thread *td = curthread;
381df8bae1dSRodney W. Grimes 
3823f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3839535efc0SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3843f11a2f3SRobert Watson 	if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
3853f11a2f3SRobert Watson 		goto bad;
3863f11a2f3SRobert Watson 	if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
3873f11a2f3SRobert Watson 		goto bad2;
388df8bae1dSRodney W. Grimes 	if (so->so_rcv.sb_lowat == 0)
389df8bae1dSRodney W. Grimes 		so->so_rcv.sb_lowat = 1;
390df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat == 0)
391df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = MCLBYTES;
392df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
393df8bae1dSRodney W. Grimes 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
3943f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
3959535efc0SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
396df8bae1dSRodney W. Grimes 	return (0);
397df8bae1dSRodney W. Grimes bad2:
3983f11a2f3SRobert Watson 	sbrelease_locked(&so->so_snd, so);
399df8bae1dSRodney W. Grimes bad:
4003f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
4013f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
402df8bae1dSRodney W. Grimes 	return (ENOBUFS);
403df8bae1dSRodney W. Grimes }
404df8bae1dSRodney W. Grimes 
40579cb7eb4SDavid Greenman static int
40679cb7eb4SDavid Greenman sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
40779cb7eb4SDavid Greenman {
40879cb7eb4SDavid Greenman 	int error = 0;
40986a93d51SJohn Baldwin 	u_long tmp_sb_max = sb_max;
41079cb7eb4SDavid Greenman 
41186a93d51SJohn Baldwin 	error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
41279cb7eb4SDavid Greenman 	if (error || !req->newptr)
41379cb7eb4SDavid Greenman 		return (error);
41486a93d51SJohn Baldwin 	if (tmp_sb_max < MSIZE + MCLBYTES)
41579cb7eb4SDavid Greenman 		return (EINVAL);
41686a93d51SJohn Baldwin 	sb_max = tmp_sb_max;
41779cb7eb4SDavid Greenman 	sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
41879cb7eb4SDavid Greenman 	return (0);
41979cb7eb4SDavid Greenman }
42079cb7eb4SDavid Greenman 
421df8bae1dSRodney W. Grimes /*
422050ac265SRobert Watson  * Allot mbufs to a sockbuf.  Attempt to scale mbmax so that mbcnt doesn't
423050ac265SRobert Watson  * become limiting if buffering efficiency is near the normal case.
424df8bae1dSRodney W. Grimes  */
42526f9a767SRodney W. Grimes int
426050ac265SRobert Watson sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
427050ac265SRobert Watson     struct thread *td)
428df8bae1dSRodney W. Grimes {
42991d5354aSJohn Baldwin 	rlim_t sbsize_limit;
430ecf72308SBrian Feldman 
4313f11a2f3SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
4323f11a2f3SRobert Watson 
433ecf72308SBrian Feldman 	/*
4347978014dSRobert Watson 	 * When a thread is passed, we take into account the thread's socket
4357978014dSRobert Watson 	 * buffer size limit.  The caller will generally pass curthread, but
4367978014dSRobert Watson 	 * in the TCP input path, NULL will be passed to indicate that no
4377978014dSRobert Watson 	 * appropriate thread resource limits are available.  In that case,
4387978014dSRobert Watson 	 * we don't apply a process limit.
439ecf72308SBrian Feldman 	 */
44079cb7eb4SDavid Greenman 	if (cc > sb_max_adj)
441df8bae1dSRodney W. Grimes 		return (0);
44291d5354aSJohn Baldwin 	if (td != NULL) {
443f6f6d240SMateusz Guzik 		sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
44491d5354aSJohn Baldwin 	} else
44591d5354aSJohn Baldwin 		sbsize_limit = RLIM_INFINITY;
446f535380cSDon Lewis 	if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
44791d5354aSJohn Baldwin 	    sbsize_limit))
448ecf72308SBrian Feldman 		return (0);
4494b29bc4fSGarrett Wollman 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
450df8bae1dSRodney W. Grimes 	if (sb->sb_lowat > sb->sb_hiwat)
451df8bae1dSRodney W. Grimes 		sb->sb_lowat = sb->sb_hiwat;
452df8bae1dSRodney W. Grimes 	return (1);
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
4553f11a2f3SRobert Watson int
45664290befSGleb Smirnoff sbsetopt(struct socket *so, int cmd, u_long cc)
4573f11a2f3SRobert Watson {
45864290befSGleb Smirnoff 	struct sockbuf *sb;
45964290befSGleb Smirnoff 	short *flags;
46064290befSGleb Smirnoff 	u_int *hiwat, *lowat;
4613f11a2f3SRobert Watson 	int error;
4623f11a2f3SRobert Watson 
463*b2037136SMatt Macy 	sb = NULL;
46464290befSGleb Smirnoff 	SOCK_LOCK(so);
46564290befSGleb Smirnoff 	if (SOLISTENING(so)) {
46664290befSGleb Smirnoff 		switch (cmd) {
46764290befSGleb Smirnoff 			case SO_SNDLOWAT:
46864290befSGleb Smirnoff 			case SO_SNDBUF:
46964290befSGleb Smirnoff 				lowat = &so->sol_sbsnd_lowat;
47064290befSGleb Smirnoff 				hiwat = &so->sol_sbsnd_hiwat;
47164290befSGleb Smirnoff 				flags = &so->sol_sbsnd_flags;
47264290befSGleb Smirnoff 				break;
47364290befSGleb Smirnoff 			case SO_RCVLOWAT:
47464290befSGleb Smirnoff 			case SO_RCVBUF:
47564290befSGleb Smirnoff 				lowat = &so->sol_sbrcv_lowat;
47664290befSGleb Smirnoff 				hiwat = &so->sol_sbrcv_hiwat;
47764290befSGleb Smirnoff 				flags = &so->sol_sbrcv_flags;
47864290befSGleb Smirnoff 				break;
47964290befSGleb Smirnoff 		}
48064290befSGleb Smirnoff 	} else {
48164290befSGleb Smirnoff 		switch (cmd) {
48264290befSGleb Smirnoff 			case SO_SNDLOWAT:
48364290befSGleb Smirnoff 			case SO_SNDBUF:
48464290befSGleb Smirnoff 				sb = &so->so_snd;
48564290befSGleb Smirnoff 				break;
48664290befSGleb Smirnoff 			case SO_RCVLOWAT:
48764290befSGleb Smirnoff 			case SO_RCVBUF:
48864290befSGleb Smirnoff 				sb = &so->so_rcv;
48964290befSGleb Smirnoff 				break;
49064290befSGleb Smirnoff 		}
49164290befSGleb Smirnoff 		flags = &sb->sb_flags;
49264290befSGleb Smirnoff 		hiwat = &sb->sb_hiwat;
49364290befSGleb Smirnoff 		lowat = &sb->sb_lowat;
4943f11a2f3SRobert Watson 		SOCKBUF_LOCK(sb);
49564290befSGleb Smirnoff 	}
49664290befSGleb Smirnoff 
49764290befSGleb Smirnoff 	error = 0;
49864290befSGleb Smirnoff 	switch (cmd) {
49964290befSGleb Smirnoff 	case SO_SNDBUF:
50064290befSGleb Smirnoff 	case SO_RCVBUF:
50164290befSGleb Smirnoff 		if (SOLISTENING(so)) {
50264290befSGleb Smirnoff 			if (cc > sb_max_adj) {
50364290befSGleb Smirnoff 				error = ENOBUFS;
50464290befSGleb Smirnoff 				break;
50564290befSGleb Smirnoff 			}
50664290befSGleb Smirnoff 			*hiwat = cc;
50764290befSGleb Smirnoff 			if (*lowat > *hiwat)
50864290befSGleb Smirnoff 				*lowat = *hiwat;
50964290befSGleb Smirnoff 		} else {
51064290befSGleb Smirnoff 			if (!sbreserve_locked(sb, cc, so, curthread))
51164290befSGleb Smirnoff 				error = ENOBUFS;
51264290befSGleb Smirnoff 		}
51364290befSGleb Smirnoff 		if (error == 0)
51464290befSGleb Smirnoff 			*flags &= ~SB_AUTOSIZE;
51564290befSGleb Smirnoff 		break;
51664290befSGleb Smirnoff 	case SO_SNDLOWAT:
51764290befSGleb Smirnoff 	case SO_RCVLOWAT:
51864290befSGleb Smirnoff 		/*
51964290befSGleb Smirnoff 		 * Make sure the low-water is never greater than the
52064290befSGleb Smirnoff 		 * high-water.
52164290befSGleb Smirnoff 		 */
52264290befSGleb Smirnoff 		*lowat = (cc > *hiwat) ? *hiwat : cc;
52364290befSGleb Smirnoff 		break;
52464290befSGleb Smirnoff 	}
52564290befSGleb Smirnoff 
52664290befSGleb Smirnoff 	if (!SOLISTENING(so))
5273f11a2f3SRobert Watson 		SOCKBUF_UNLOCK(sb);
52864290befSGleb Smirnoff 	SOCK_UNLOCK(so);
5293f11a2f3SRobert Watson 	return (error);
5303f11a2f3SRobert Watson }
5313f11a2f3SRobert Watson 
532df8bae1dSRodney W. Grimes /*
533df8bae1dSRodney W. Grimes  * Free mbufs held by a socket, and reserved mbuf space.
534df8bae1dSRodney W. Grimes  */
5353f0bfcccSRobert Watson void
536050ac265SRobert Watson sbrelease_internal(struct sockbuf *sb, struct socket *so)
537eaa6dfbcSRobert Watson {
538eaa6dfbcSRobert Watson 
539eaa6dfbcSRobert Watson 	sbflush_internal(sb);
540eaa6dfbcSRobert Watson 	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
541eaa6dfbcSRobert Watson 	    RLIM_INFINITY);
542eaa6dfbcSRobert Watson 	sb->sb_mbmax = 0;
543eaa6dfbcSRobert Watson }
544eaa6dfbcSRobert Watson 
54526f9a767SRodney W. Grimes void
546050ac265SRobert Watson sbrelease_locked(struct sockbuf *sb, struct socket *so)
547df8bae1dSRodney W. Grimes {
548df8bae1dSRodney W. Grimes 
549a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
550a34b7046SRobert Watson 
551eaa6dfbcSRobert Watson 	sbrelease_internal(sb, so);
552df8bae1dSRodney W. Grimes }
553df8bae1dSRodney W. Grimes 
554a34b7046SRobert Watson void
555050ac265SRobert Watson sbrelease(struct sockbuf *sb, struct socket *so)
556a34b7046SRobert Watson {
557a34b7046SRobert Watson 
558a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
559a34b7046SRobert Watson 	sbrelease_locked(sb, so);
560a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
561a34b7046SRobert Watson }
562eaa6dfbcSRobert Watson 
563eaa6dfbcSRobert Watson void
564050ac265SRobert Watson sbdestroy(struct sockbuf *sb, struct socket *so)
565eaa6dfbcSRobert Watson {
566eaa6dfbcSRobert Watson 
567eaa6dfbcSRobert Watson 	sbrelease_internal(sb, so);
568eaa6dfbcSRobert Watson }
569eaa6dfbcSRobert Watson 
570df8bae1dSRodney W. Grimes /*
571050ac265SRobert Watson  * Routines to add and remove data from an mbuf queue.
572df8bae1dSRodney W. Grimes  *
573050ac265SRobert Watson  * The routines sbappend() or sbappendrecord() are normally called to append
574050ac265SRobert Watson  * new mbufs to a socket buffer, after checking that adequate space is
575050ac265SRobert Watson  * available, comparing the function sbspace() with the amount of data to be
576050ac265SRobert Watson  * added.  sbappendrecord() differs from sbappend() in that data supplied is
577050ac265SRobert Watson  * treated as the beginning of a new record.  To place a sender's address,
578050ac265SRobert Watson  * optional access rights, and data in a socket receive buffer,
579050ac265SRobert Watson  * sbappendaddr() should be used.  To place access rights and data in a
580050ac265SRobert Watson  * socket receive buffer, sbappendrights() should be used.  In either case,
581050ac265SRobert Watson  * the new data begins a new record.  Note that unlike sbappend() and
582050ac265SRobert Watson  * sbappendrecord(), these routines check for the caller that there will be
583050ac265SRobert Watson  * enough space to store the data.  Each fails if there is not enough space,
584050ac265SRobert Watson  * or if it cannot find mbufs to store additional information in.
585df8bae1dSRodney W. Grimes  *
586050ac265SRobert Watson  * Reliable protocols may use the socket send buffer to hold data awaiting
587050ac265SRobert Watson  * acknowledgement.  Data is normally copied from a socket send buffer in a
588050ac265SRobert Watson  * protocol with m_copy for output to a peer, and then removing the data from
589050ac265SRobert Watson  * the socket buffer with sbdrop() or sbdroprecord() when the data is
590050ac265SRobert Watson  * acknowledged by the peer.
591df8bae1dSRodney W. Grimes  */
592395bb186SSam Leffler #ifdef SOCKBUF_DEBUG
593395bb186SSam Leffler void
594395bb186SSam Leffler sblastrecordchk(struct sockbuf *sb, const char *file, int line)
595395bb186SSam Leffler {
596395bb186SSam Leffler 	struct mbuf *m = sb->sb_mb;
597395bb186SSam Leffler 
598a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
599a34b7046SRobert Watson 
600395bb186SSam Leffler 	while (m && m->m_nextpkt)
601395bb186SSam Leffler 		m = m->m_nextpkt;
602395bb186SSam Leffler 
603395bb186SSam Leffler 	if (m != sb->sb_lastrecord) {
604395bb186SSam Leffler 		printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
605395bb186SSam Leffler 			__func__, sb->sb_mb, sb->sb_lastrecord, m);
606395bb186SSam Leffler 		printf("packet chain:\n");
607395bb186SSam Leffler 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
608395bb186SSam Leffler 			printf("\t%p\n", m);
609395bb186SSam Leffler 		panic("%s from %s:%u", __func__, file, line);
610395bb186SSam Leffler 	}
611395bb186SSam Leffler }
612395bb186SSam Leffler 
613395bb186SSam Leffler void
614395bb186SSam Leffler sblastmbufchk(struct sockbuf *sb, const char *file, int line)
615395bb186SSam Leffler {
616395bb186SSam Leffler 	struct mbuf *m = sb->sb_mb;
617395bb186SSam Leffler 	struct mbuf *n;
618395bb186SSam Leffler 
619a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
620a34b7046SRobert Watson 
621395bb186SSam Leffler 	while (m && m->m_nextpkt)
622395bb186SSam Leffler 		m = m->m_nextpkt;
623395bb186SSam Leffler 
624395bb186SSam Leffler 	while (m && m->m_next)
625395bb186SSam Leffler 		m = m->m_next;
626395bb186SSam Leffler 
627395bb186SSam Leffler 	if (m != sb->sb_mbtail) {
628395bb186SSam Leffler 		printf("%s: sb_mb %p sb_mbtail %p last %p\n",
629395bb186SSam Leffler 			__func__, sb->sb_mb, sb->sb_mbtail, m);
630395bb186SSam Leffler 		printf("packet tree:\n");
631395bb186SSam Leffler 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
632395bb186SSam Leffler 			printf("\t");
633395bb186SSam Leffler 			for (n = m; n != NULL; n = n->m_next)
634395bb186SSam Leffler 				printf("%p ", n);
635395bb186SSam Leffler 			printf("\n");
636395bb186SSam Leffler 		}
637395bb186SSam Leffler 		panic("%s from %s:%u", __func__, file, line);
638395bb186SSam Leffler 	}
639395bb186SSam Leffler }
640395bb186SSam Leffler #endif /* SOCKBUF_DEBUG */
641395bb186SSam Leffler 
642395bb186SSam Leffler #define SBLINKRECORD(sb, m0) do {					\
643a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);					\
644395bb186SSam Leffler 	if ((sb)->sb_lastrecord != NULL)				\
645395bb186SSam Leffler 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
646395bb186SSam Leffler 	else								\
647395bb186SSam Leffler 		(sb)->sb_mb = (m0);					\
648395bb186SSam Leffler 	(sb)->sb_lastrecord = (m0);					\
649395bb186SSam Leffler } while (/*CONSTCOND*/0)
650395bb186SSam Leffler 
651df8bae1dSRodney W. Grimes /*
652050ac265SRobert Watson  * Append mbuf chain m to the last record in the socket buffer sb.  The
653050ac265SRobert Watson  * additional space associated the mbuf chain is recorded in sb.  Empty mbufs
654050ac265SRobert Watson  * are discarded and mbufs are compacted where possible.
655df8bae1dSRodney W. Grimes  */
65626f9a767SRodney W. Grimes void
657829fae90SGleb Smirnoff sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
658df8bae1dSRodney W. Grimes {
659050ac265SRobert Watson 	struct mbuf *n;
660df8bae1dSRodney W. Grimes 
661a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
662a34b7046SRobert Watson 
663b85f65afSPedro F. Giffuni 	if (m == NULL)
664df8bae1dSRodney W. Grimes 		return;
665829fae90SGleb Smirnoff 	sbm_clrprotoflags(m, flags);
666395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
667797f2d22SPoul-Henning Kamp 	n = sb->sb_mb;
668797f2d22SPoul-Henning Kamp 	if (n) {
669df8bae1dSRodney W. Grimes 		while (n->m_nextpkt)
670df8bae1dSRodney W. Grimes 			n = n->m_nextpkt;
671df8bae1dSRodney W. Grimes 		do {
672df8bae1dSRodney W. Grimes 			if (n->m_flags & M_EOR) {
673a34b7046SRobert Watson 				sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
674df8bae1dSRodney W. Grimes 				return;
675df8bae1dSRodney W. Grimes 			}
676df8bae1dSRodney W. Grimes 		} while (n->m_next && (n = n->m_next));
677395bb186SSam Leffler 	} else {
678395bb186SSam Leffler 		/*
679395bb186SSam Leffler 		 * XXX Would like to simply use sb_mbtail here, but
680395bb186SSam Leffler 		 * XXX I need to verify that I won't miss an EOR that
681395bb186SSam Leffler 		 * XXX way.
682395bb186SSam Leffler 		 */
683395bb186SSam Leffler 		if ((n = sb->sb_lastrecord) != NULL) {
684395bb186SSam Leffler 			do {
685395bb186SSam Leffler 				if (n->m_flags & M_EOR) {
686a34b7046SRobert Watson 					sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
687395bb186SSam Leffler 					return;
688395bb186SSam Leffler 				}
689395bb186SSam Leffler 			} while (n->m_next && (n = n->m_next));
690395bb186SSam Leffler 		} else {
691395bb186SSam Leffler 			/*
692395bb186SSam Leffler 			 * If this is the first record in the socket buffer,
693395bb186SSam Leffler 			 * it's also the last record.
694395bb186SSam Leffler 			 */
695395bb186SSam Leffler 			sb->sb_lastrecord = m;
696395bb186SSam Leffler 		}
697df8bae1dSRodney W. Grimes 	}
698df8bae1dSRodney W. Grimes 	sbcompress(sb, m, n);
699395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
700395bb186SSam Leffler }
701395bb186SSam Leffler 
702395bb186SSam Leffler /*
703050ac265SRobert Watson  * Append mbuf chain m to the last record in the socket buffer sb.  The
704050ac265SRobert Watson  * additional space associated the mbuf chain is recorded in sb.  Empty mbufs
705050ac265SRobert Watson  * are discarded and mbufs are compacted where possible.
706a34b7046SRobert Watson  */
707a34b7046SRobert Watson void
708829fae90SGleb Smirnoff sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
709a34b7046SRobert Watson {
710a34b7046SRobert Watson 
711a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
712829fae90SGleb Smirnoff 	sbappend_locked(sb, m, flags);
713a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
714a34b7046SRobert Watson }
715a34b7046SRobert Watson 
716a34b7046SRobert Watson /*
717050ac265SRobert Watson  * This version of sbappend() should only be used when the caller absolutely
718050ac265SRobert Watson  * knows that there will never be more than one record in the socket buffer,
719050ac265SRobert Watson  * that is, a stream protocol (such as TCP).
720395bb186SSam Leffler  */
721395bb186SSam Leffler void
722651e4e6aSGleb Smirnoff sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
723395bb186SSam Leffler {
724a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
725395bb186SSam Leffler 
726395bb186SSam Leffler 	KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
727395bb186SSam Leffler 	KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
728395bb186SSam Leffler 
729395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
730395bb186SSam Leffler 
731844cacd1SGleb Smirnoff 	/* Remove all packet headers and mbuf tags to get a pure data chain. */
732651e4e6aSGleb Smirnoff 	m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0);
733844cacd1SGleb Smirnoff 
734395bb186SSam Leffler 	sbcompress(sb, m, sb->sb_mbtail);
735395bb186SSam Leffler 
736395bb186SSam Leffler 	sb->sb_lastrecord = sb->sb_mb;
737395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
738df8bae1dSRodney W. Grimes }
739df8bae1dSRodney W. Grimes 
740a34b7046SRobert Watson /*
741050ac265SRobert Watson  * This version of sbappend() should only be used when the caller absolutely
742050ac265SRobert Watson  * knows that there will never be more than one record in the socket buffer,
743050ac265SRobert Watson  * that is, a stream protocol (such as TCP).
744a34b7046SRobert Watson  */
745a34b7046SRobert Watson void
746651e4e6aSGleb Smirnoff sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags)
747a34b7046SRobert Watson {
748a34b7046SRobert Watson 
749a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
750651e4e6aSGleb Smirnoff 	sbappendstream_locked(sb, m, flags);
751a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
752a34b7046SRobert Watson }
753a34b7046SRobert Watson 
754df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG
75526f9a767SRodney W. Grimes void
75657f43a45SGleb Smirnoff sbcheck(struct sockbuf *sb, const char *file, int line)
757df8bae1dSRodney W. Grimes {
7580f9d0a73SGleb Smirnoff 	struct mbuf *m, *n, *fnrdy;
7590f9d0a73SGleb Smirnoff 	u_long acc, ccc, mbcnt;
760df8bae1dSRodney W. Grimes 
761a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
762a34b7046SRobert Watson 
7630f9d0a73SGleb Smirnoff 	acc = ccc = mbcnt = 0;
7640f9d0a73SGleb Smirnoff 	fnrdy = NULL;
76557f43a45SGleb Smirnoff 
7660931333fSBill Fenner 	for (m = sb->sb_mb; m; m = n) {
7670931333fSBill Fenner 	    n = m->m_nextpkt;
7680931333fSBill Fenner 	    for (; m; m = m->m_next) {
76957f43a45SGleb Smirnoff 		if (m->m_len == 0) {
77057f43a45SGleb Smirnoff 			printf("sb %p empty mbuf %p\n", sb, m);
77157f43a45SGleb Smirnoff 			goto fail;
77257f43a45SGleb Smirnoff 		}
7730f9d0a73SGleb Smirnoff 		if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) {
7740f9d0a73SGleb Smirnoff 			if (m != sb->sb_fnrdy) {
7750f9d0a73SGleb Smirnoff 				printf("sb %p: fnrdy %p != m %p\n",
7760f9d0a73SGleb Smirnoff 				    sb, sb->sb_fnrdy, m);
7770f9d0a73SGleb Smirnoff 				goto fail;
7780f9d0a73SGleb Smirnoff 			}
7790f9d0a73SGleb Smirnoff 			fnrdy = m;
7800f9d0a73SGleb Smirnoff 		}
7810f9d0a73SGleb Smirnoff 		if (fnrdy) {
7820f9d0a73SGleb Smirnoff 			if (!(m->m_flags & M_NOTAVAIL)) {
7830f9d0a73SGleb Smirnoff 				printf("sb %p: fnrdy %p, m %p is avail\n",
7840f9d0a73SGleb Smirnoff 				    sb, sb->sb_fnrdy, m);
7850f9d0a73SGleb Smirnoff 				goto fail;
7860f9d0a73SGleb Smirnoff 			}
7870f9d0a73SGleb Smirnoff 		} else
7880f9d0a73SGleb Smirnoff 			acc += m->m_len;
7890f9d0a73SGleb Smirnoff 		ccc += m->m_len;
790df8bae1dSRodney W. Grimes 		mbcnt += MSIZE;
791313861b8SJulian Elischer 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
792df8bae1dSRodney W. Grimes 			mbcnt += m->m_ext.ext_size;
7930931333fSBill Fenner 	    }
794df8bae1dSRodney W. Grimes 	}
7950f9d0a73SGleb Smirnoff 	if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) {
7960f9d0a73SGleb Smirnoff 		printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n",
7970f9d0a73SGleb Smirnoff 		    acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt);
79857f43a45SGleb Smirnoff 		goto fail;
799df8bae1dSRodney W. Grimes 	}
80057f43a45SGleb Smirnoff 	return;
80157f43a45SGleb Smirnoff fail:
80257f43a45SGleb Smirnoff 	panic("%s from %s:%u", __func__, file, line);
803df8bae1dSRodney W. Grimes }
804df8bae1dSRodney W. Grimes #endif
805df8bae1dSRodney W. Grimes 
806df8bae1dSRodney W. Grimes /*
807050ac265SRobert Watson  * As above, except the mbuf chain begins a new record.
808df8bae1dSRodney W. Grimes  */
80926f9a767SRodney W. Grimes void
810050ac265SRobert Watson sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
811df8bae1dSRodney W. Grimes {
812050ac265SRobert Watson 	struct mbuf *m;
813df8bae1dSRodney W. Grimes 
814a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
815a34b7046SRobert Watson 
816b85f65afSPedro F. Giffuni 	if (m0 == NULL)
817df8bae1dSRodney W. Grimes 		return;
81853b680caSGleb Smirnoff 	m_clrprotoflags(m0);
819df8bae1dSRodney W. Grimes 	/*
820050ac265SRobert Watson 	 * Put the first mbuf on the queue.  Note this permits zero length
821050ac265SRobert Watson 	 * records.
822df8bae1dSRodney W. Grimes 	 */
823df8bae1dSRodney W. Grimes 	sballoc(sb, m0);
824395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
825395bb186SSam Leffler 	SBLINKRECORD(sb, m0);
826e72a94adSMaksim Yevmenkin 	sb->sb_mbtail = m0;
827df8bae1dSRodney W. Grimes 	m = m0->m_next;
828df8bae1dSRodney W. Grimes 	m0->m_next = 0;
829df8bae1dSRodney W. Grimes 	if (m && (m0->m_flags & M_EOR)) {
830df8bae1dSRodney W. Grimes 		m0->m_flags &= ~M_EOR;
831df8bae1dSRodney W. Grimes 		m->m_flags |= M_EOR;
832df8bae1dSRodney W. Grimes 	}
833e72a94adSMaksim Yevmenkin 	/* always call sbcompress() so it can do SBLASTMBUFCHK() */
834df8bae1dSRodney W. Grimes 	sbcompress(sb, m, m0);
835df8bae1dSRodney W. Grimes }
836df8bae1dSRodney W. Grimes 
837df8bae1dSRodney W. Grimes /*
838050ac265SRobert Watson  * As above, except the mbuf chain begins a new record.
839a34b7046SRobert Watson  */
840a34b7046SRobert Watson void
841050ac265SRobert Watson sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
842a34b7046SRobert Watson {
843a34b7046SRobert Watson 
844a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
845a34b7046SRobert Watson 	sbappendrecord_locked(sb, m0);
846a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
847a34b7046SRobert Watson }
848a34b7046SRobert Watson 
8498de34a88SAlan Somers /* Helper routine that appends data, control, and address to a sockbuf. */
8508de34a88SAlan Somers static int
8518de34a88SAlan Somers sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
8528de34a88SAlan Somers     struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
853df8bae1dSRodney W. Grimes {
854395bb186SSam Leffler 	struct mbuf *m, *n, *nlast;
855c43cad1aSScott Long #if MSIZE <= 256
856df8bae1dSRodney W. Grimes 	if (asa->sa_len > MLEN)
857df8bae1dSRodney W. Grimes 		return (0);
858c43cad1aSScott Long #endif
859c8b59ea7SGleb Smirnoff 	m = m_get(M_NOWAIT, MT_SONAME);
860c8b59ea7SGleb Smirnoff 	if (m == NULL)
861df8bae1dSRodney W. Grimes 		return (0);
862df8bae1dSRodney W. Grimes 	m->m_len = asa->sa_len;
86380208239SAlfred Perlstein 	bcopy(asa, mtod(m, caddr_t), asa->sa_len);
864c33a2313SAndrey V. Elsukov 	if (m0) {
86553b680caSGleb Smirnoff 		m_clrprotoflags(m0);
86657386f5dSAndrey V. Elsukov 		m_tag_delete_chain(m0, NULL);
867c33a2313SAndrey V. Elsukov 		/*
868c33a2313SAndrey V. Elsukov 		 * Clear some persistent info from pkthdr.
869c33a2313SAndrey V. Elsukov 		 * We don't use m_demote(), because some netgraph consumers
870c33a2313SAndrey V. Elsukov 		 * expect M_PKTHDR presence.
871c33a2313SAndrey V. Elsukov 		 */
872c33a2313SAndrey V. Elsukov 		m0->m_pkthdr.rcvif = NULL;
873c33a2313SAndrey V. Elsukov 		m0->m_pkthdr.flowid = 0;
874c33a2313SAndrey V. Elsukov 		m0->m_pkthdr.csum_flags = 0;
875c33a2313SAndrey V. Elsukov 		m0->m_pkthdr.fibnum = 0;
876c33a2313SAndrey V. Elsukov 		m0->m_pkthdr.rsstype = 0;
877c33a2313SAndrey V. Elsukov 	}
8788de34a88SAlan Somers 	if (ctrl_last)
8798de34a88SAlan Somers 		ctrl_last->m_next = m0;	/* concatenate data to control */
880df8bae1dSRodney W. Grimes 	else
881df8bae1dSRodney W. Grimes 		control = m0;
882df8bae1dSRodney W. Grimes 	m->m_next = control;
883395bb186SSam Leffler 	for (n = m; n->m_next != NULL; n = n->m_next)
884df8bae1dSRodney W. Grimes 		sballoc(sb, n);
885395bb186SSam Leffler 	sballoc(sb, n);
886395bb186SSam Leffler 	nlast = n;
887395bb186SSam Leffler 	SBLINKRECORD(sb, m);
888395bb186SSam Leffler 
889395bb186SSam Leffler 	sb->sb_mbtail = nlast;
890395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
891395bb186SSam Leffler 
892395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
893df8bae1dSRodney W. Grimes 	return (1);
894df8bae1dSRodney W. Grimes }
895df8bae1dSRodney W. Grimes 
896a34b7046SRobert Watson /*
897050ac265SRobert Watson  * Append address and data, and optionally, control (ancillary) data to the
898050ac265SRobert Watson  * receive queue of a socket.  If present, m0 must include a packet header
899050ac265SRobert Watson  * with total length.  Returns 0 if no space in sockbuf or insufficient
900050ac265SRobert Watson  * mbufs.
901a34b7046SRobert Watson  */
90226f9a767SRodney W. Grimes int
9038de34a88SAlan Somers sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
9048de34a88SAlan Somers     struct mbuf *m0, struct mbuf *control)
9058de34a88SAlan Somers {
9068de34a88SAlan Somers 	struct mbuf *ctrl_last;
9078de34a88SAlan Somers 	int space = asa->sa_len;
9088de34a88SAlan Somers 
9098de34a88SAlan Somers 	SOCKBUF_LOCK_ASSERT(sb);
9108de34a88SAlan Somers 
9118de34a88SAlan Somers 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
9128de34a88SAlan Somers 		panic("sbappendaddr_locked");
9138de34a88SAlan Somers 	if (m0)
9148de34a88SAlan Somers 		space += m0->m_pkthdr.len;
9158de34a88SAlan Somers 	space += m_length(control, &ctrl_last);
9168de34a88SAlan Somers 
9178de34a88SAlan Somers 	if (space > sbspace(sb))
9188de34a88SAlan Somers 		return (0);
9198de34a88SAlan Somers 	return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
9208de34a88SAlan Somers }
9218de34a88SAlan Somers 
9228de34a88SAlan Somers /*
9238de34a88SAlan Somers  * Append address and data, and optionally, control (ancillary) data to the
9248de34a88SAlan Somers  * receive queue of a socket.  If present, m0 must include a packet header
9258de34a88SAlan Somers  * with total length.  Returns 0 if insufficient mbufs.  Does not validate space
9268de34a88SAlan Somers  * on the receiving sockbuf.
9278de34a88SAlan Somers  */
9288de34a88SAlan Somers int
9298de34a88SAlan Somers sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
9308de34a88SAlan Somers     struct mbuf *m0, struct mbuf *control)
9318de34a88SAlan Somers {
9328de34a88SAlan Somers 	struct mbuf *ctrl_last;
9338de34a88SAlan Somers 
9348de34a88SAlan Somers 	SOCKBUF_LOCK_ASSERT(sb);
9358de34a88SAlan Somers 
9368de34a88SAlan Somers 	ctrl_last = (control == NULL) ? NULL : m_last(control);
9378de34a88SAlan Somers 	return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
9388de34a88SAlan Somers }
9398de34a88SAlan Somers 
9408de34a88SAlan Somers /*
9418de34a88SAlan Somers  * Append address and data, and optionally, control (ancillary) data to the
9428de34a88SAlan Somers  * receive queue of a socket.  If present, m0 must include a packet header
9438de34a88SAlan Somers  * with total length.  Returns 0 if no space in sockbuf or insufficient
9448de34a88SAlan Somers  * mbufs.
9458de34a88SAlan Somers  */
9468de34a88SAlan Somers int
947050ac265SRobert Watson sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
948050ac265SRobert Watson     struct mbuf *m0, struct mbuf *control)
949a34b7046SRobert Watson {
950a34b7046SRobert Watson 	int retval;
951a34b7046SRobert Watson 
952a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
953a34b7046SRobert Watson 	retval = sbappendaddr_locked(sb, asa, m0, control);
954a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
955a34b7046SRobert Watson 	return (retval);
956a34b7046SRobert Watson }
957a34b7046SRobert Watson 
958a34b7046SRobert Watson int
959050ac265SRobert Watson sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
960050ac265SRobert Watson     struct mbuf *control)
961df8bae1dSRodney W. Grimes {
962395bb186SSam Leffler 	struct mbuf *m, *n, *mlast;
9637ed60de8SPoul-Henning Kamp 	int space;
964df8bae1dSRodney W. Grimes 
965a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
966a34b7046SRobert Watson 
967b85f65afSPedro F. Giffuni 	if (control == NULL)
968a34b7046SRobert Watson 		panic("sbappendcontrol_locked");
9697ed60de8SPoul-Henning Kamp 	space = m_length(control, &n) + m_length(m0, NULL);
970a34b7046SRobert Watson 
971df8bae1dSRodney W. Grimes 	if (space > sbspace(sb))
972df8bae1dSRodney W. Grimes 		return (0);
97353b680caSGleb Smirnoff 	m_clrprotoflags(m0);
974df8bae1dSRodney W. Grimes 	n->m_next = m0;			/* concatenate data to control */
975395bb186SSam Leffler 
976395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
977395bb186SSam Leffler 
978395bb186SSam Leffler 	for (m = control; m->m_next; m = m->m_next)
979df8bae1dSRodney W. Grimes 		sballoc(sb, m);
980395bb186SSam Leffler 	sballoc(sb, m);
981395bb186SSam Leffler 	mlast = m;
982395bb186SSam Leffler 	SBLINKRECORD(sb, control);
983395bb186SSam Leffler 
984395bb186SSam Leffler 	sb->sb_mbtail = mlast;
985395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
986395bb186SSam Leffler 
987395bb186SSam Leffler 	SBLASTRECORDCHK(sb);
988df8bae1dSRodney W. Grimes 	return (1);
989df8bae1dSRodney W. Grimes }
990df8bae1dSRodney W. Grimes 
991a34b7046SRobert Watson int
992050ac265SRobert Watson sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
993a34b7046SRobert Watson {
994a34b7046SRobert Watson 	int retval;
995a34b7046SRobert Watson 
996a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
997a34b7046SRobert Watson 	retval = sbappendcontrol_locked(sb, m0, control);
998a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
999a34b7046SRobert Watson 	return (retval);
1000a34b7046SRobert Watson }
1001a34b7046SRobert Watson 
1002df8bae1dSRodney W. Grimes /*
10037da7362bSRobert Watson  * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
10047da7362bSRobert Watson  * (n).  If (n) is NULL, the buffer is presumed empty.
10057da7362bSRobert Watson  *
10067da7362bSRobert Watson  * When the data is compressed, mbufs in the chain may be handled in one of
10077da7362bSRobert Watson  * three ways:
10087da7362bSRobert Watson  *
10097da7362bSRobert Watson  * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
10107da7362bSRobert Watson  *     record boundary, and no change in data type).
10117da7362bSRobert Watson  *
10127da7362bSRobert Watson  * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
10137da7362bSRobert Watson  *     an mbuf already in the socket buffer.  This can occur if an
10140f9d0a73SGleb Smirnoff  *     appropriate mbuf exists, there is room, both mbufs are not marked as
10150f9d0a73SGleb Smirnoff  *     not ready, and no merging of data types will occur.
10167da7362bSRobert Watson  *
10177da7362bSRobert Watson  * (3) The mbuf may be appended to the end of the existing mbuf chain.
10187da7362bSRobert Watson  *
10197da7362bSRobert Watson  * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
10207da7362bSRobert Watson  * end-of-record.
1021df8bae1dSRodney W. Grimes  */
102226f9a767SRodney W. Grimes void
1023050ac265SRobert Watson sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1024df8bae1dSRodney W. Grimes {
1025050ac265SRobert Watson 	int eor = 0;
1026050ac265SRobert Watson 	struct mbuf *o;
1027df8bae1dSRodney W. Grimes 
1028a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
1029a34b7046SRobert Watson 
1030df8bae1dSRodney W. Grimes 	while (m) {
1031df8bae1dSRodney W. Grimes 		eor |= m->m_flags & M_EOR;
1032df8bae1dSRodney W. Grimes 		if (m->m_len == 0 &&
1033df8bae1dSRodney W. Grimes 		    (eor == 0 ||
1034df8bae1dSRodney W. Grimes 		     (((o = m->m_next) || (o = n)) &&
1035df8bae1dSRodney W. Grimes 		      o->m_type == m->m_type))) {
1036395bb186SSam Leffler 			if (sb->sb_lastrecord == m)
1037395bb186SSam Leffler 				sb->sb_lastrecord = m->m_next;
1038df8bae1dSRodney W. Grimes 			m = m_free(m);
1039df8bae1dSRodney W. Grimes 			continue;
1040df8bae1dSRodney W. Grimes 		}
104132af0d74SDavid Malone 		if (n && (n->m_flags & M_EOR) == 0 &&
104232af0d74SDavid Malone 		    M_WRITABLE(n) &&
10435e0f5cfaSKip Macy 		    ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
10440f9d0a73SGleb Smirnoff 		    !(m->m_flags & M_NOTREADY) &&
10450f9d0a73SGleb Smirnoff 		    !(n->m_flags & M_NOTREADY) &&
104632af0d74SDavid Malone 		    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
104732af0d74SDavid Malone 		    m->m_len <= M_TRAILINGSPACE(n) &&
1048df8bae1dSRodney W. Grimes 		    n->m_type == m->m_type) {
1049df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1050df8bae1dSRodney W. Grimes 			    (unsigned)m->m_len);
1051df8bae1dSRodney W. Grimes 			n->m_len += m->m_len;
10520f9d0a73SGleb Smirnoff 			sb->sb_ccc += m->m_len;
10530f9d0a73SGleb Smirnoff 			if (sb->sb_fnrdy == NULL)
10540f9d0a73SGleb Smirnoff 				sb->sb_acc += m->m_len;
105534333b16SAndre Oppermann 			if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1056b3f1af6bSTim J. Robbins 				/* XXX: Probably don't need.*/
105704ac9b97SKelly Yancey 				sb->sb_ctl += m->m_len;
1058df8bae1dSRodney W. Grimes 			m = m_free(m);
1059df8bae1dSRodney W. Grimes 			continue;
1060df8bae1dSRodney W. Grimes 		}
1061df8bae1dSRodney W. Grimes 		if (n)
1062df8bae1dSRodney W. Grimes 			n->m_next = m;
1063df8bae1dSRodney W. Grimes 		else
1064df8bae1dSRodney W. Grimes 			sb->sb_mb = m;
1065395bb186SSam Leffler 		sb->sb_mbtail = m;
1066df8bae1dSRodney W. Grimes 		sballoc(sb, m);
1067df8bae1dSRodney W. Grimes 		n = m;
1068df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_EOR;
1069df8bae1dSRodney W. Grimes 		m = m->m_next;
1070df8bae1dSRodney W. Grimes 		n->m_next = 0;
1071df8bae1dSRodney W. Grimes 	}
1072df8bae1dSRodney W. Grimes 	if (eor) {
10737da7362bSRobert Watson 		KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1074df8bae1dSRodney W. Grimes 		n->m_flags |= eor;
1075df8bae1dSRodney W. Grimes 	}
1076395bb186SSam Leffler 	SBLASTMBUFCHK(sb);
1077df8bae1dSRodney W. Grimes }
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes /*
1080050ac265SRobert Watson  * Free all mbufs in a sockbuf.  Check that all resources are reclaimed.
1081df8bae1dSRodney W. Grimes  */
1082eaa6dfbcSRobert Watson static void
1083050ac265SRobert Watson sbflush_internal(struct sockbuf *sb)
1084df8bae1dSRodney W. Grimes {
1085df8bae1dSRodney W. Grimes 
108623f84772SPierre Beyssac 	while (sb->sb_mbcnt) {
108723f84772SPierre Beyssac 		/*
1088761a9a1fSGleb Smirnoff 		 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
108923f84772SPierre Beyssac 		 * we would loop forever. Panic instead.
109023f84772SPierre Beyssac 		 */
10910f9d0a73SGleb Smirnoff 		if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
109223f84772SPierre Beyssac 			break;
10930f9d0a73SGleb Smirnoff 		m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
109423f84772SPierre Beyssac 	}
10950f9d0a73SGleb Smirnoff 	KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
10960f9d0a73SGleb Smirnoff 	    ("%s: ccc %u mb %p mbcnt %u", __func__,
10970f9d0a73SGleb Smirnoff 	    sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1098a34b7046SRobert Watson }
1099a34b7046SRobert Watson 
1100a34b7046SRobert Watson void
1101050ac265SRobert Watson sbflush_locked(struct sockbuf *sb)
1102eaa6dfbcSRobert Watson {
1103eaa6dfbcSRobert Watson 
1104eaa6dfbcSRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
1105eaa6dfbcSRobert Watson 	sbflush_internal(sb);
1106eaa6dfbcSRobert Watson }
1107eaa6dfbcSRobert Watson 
1108eaa6dfbcSRobert Watson void
1109050ac265SRobert Watson sbflush(struct sockbuf *sb)
1110a34b7046SRobert Watson {
1111a34b7046SRobert Watson 
1112a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
1113a34b7046SRobert Watson 	sbflush_locked(sb);
1114a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
1115df8bae1dSRodney W. Grimes }
1116df8bae1dSRodney W. Grimes 
1117df8bae1dSRodney W. Grimes /*
11181d2df300SGleb Smirnoff  * Cut data from (the front of) a sockbuf.
1119df8bae1dSRodney W. Grimes  */
11201d2df300SGleb Smirnoff static struct mbuf *
11211d2df300SGleb Smirnoff sbcut_internal(struct sockbuf *sb, int len)
1122df8bae1dSRodney W. Grimes {
11230f9d0a73SGleb Smirnoff 	struct mbuf *m, *next, *mfree;
1124df8bae1dSRodney W. Grimes 
1125f41b2de7SHiren Panchasara 	KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0",
1126b5b023b9SHiren Panchasara 	    __func__, len));
1127b5b023b9SHiren Panchasara 	KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u",
1128b5b023b9SHiren Panchasara 	    __func__, len, sb->sb_ccc));
1129b5b023b9SHiren Panchasara 
1130df8bae1dSRodney W. Grimes 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
11311d2df300SGleb Smirnoff 	mfree = NULL;
11321d2df300SGleb Smirnoff 
1133df8bae1dSRodney W. Grimes 	while (len > 0) {
11348146bcfeSGleb Smirnoff 		if (m == NULL) {
11358146bcfeSGleb Smirnoff 			KASSERT(next, ("%s: no next, len %d", __func__, len));
1136df8bae1dSRodney W. Grimes 			m = next;
1137df8bae1dSRodney W. Grimes 			next = m->m_nextpkt;
1138df8bae1dSRodney W. Grimes 		}
1139df8bae1dSRodney W. Grimes 		if (m->m_len > len) {
11400f9d0a73SGleb Smirnoff 			KASSERT(!(m->m_flags & M_NOTAVAIL),
11410f9d0a73SGleb Smirnoff 			    ("%s: m %p M_NOTAVAIL", __func__, m));
1142df8bae1dSRodney W. Grimes 			m->m_len -= len;
1143df8bae1dSRodney W. Grimes 			m->m_data += len;
11440f9d0a73SGleb Smirnoff 			sb->sb_ccc -= len;
11450f9d0a73SGleb Smirnoff 			sb->sb_acc -= len;
11464e023759SAndre Oppermann 			if (sb->sb_sndptroff != 0)
11474e023759SAndre Oppermann 				sb->sb_sndptroff -= len;
114834333b16SAndre Oppermann 			if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
114904ac9b97SKelly Yancey 				sb->sb_ctl -= len;
1150df8bae1dSRodney W. Grimes 			break;
1151df8bae1dSRodney W. Grimes 		}
1152df8bae1dSRodney W. Grimes 		len -= m->m_len;
1153df8bae1dSRodney W. Grimes 		sbfree(sb, m);
11540f9d0a73SGleb Smirnoff 		/*
11550f9d0a73SGleb Smirnoff 		 * Do not put M_NOTREADY buffers to the free list, they
11560f9d0a73SGleb Smirnoff 		 * are referenced from outside.
11570f9d0a73SGleb Smirnoff 		 */
11580f9d0a73SGleb Smirnoff 		if (m->m_flags & M_NOTREADY)
11590f9d0a73SGleb Smirnoff 			m = m->m_next;
11600f9d0a73SGleb Smirnoff 		else {
11610f9d0a73SGleb Smirnoff 			struct mbuf *n;
11620f9d0a73SGleb Smirnoff 
11631d2df300SGleb Smirnoff 			n = m->m_next;
11641d2df300SGleb Smirnoff 			m->m_next = mfree;
11651d2df300SGleb Smirnoff 			mfree = m;
11661d2df300SGleb Smirnoff 			m = n;
1167df8bae1dSRodney W. Grimes 		}
11680f9d0a73SGleb Smirnoff 	}
1169e834a840SGleb Smirnoff 	/*
1170e834a840SGleb Smirnoff 	 * Free any zero-length mbufs from the buffer.
1171e834a840SGleb Smirnoff 	 * For SOCK_DGRAM sockets such mbufs represent empty records.
1172e834a840SGleb Smirnoff 	 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1173e834a840SGleb Smirnoff 	 * when sosend_generic() needs to send only control data.
1174e834a840SGleb Smirnoff 	 */
1175e834a840SGleb Smirnoff 	while (m && m->m_len == 0) {
1176e834a840SGleb Smirnoff 		struct mbuf *n;
1177e834a840SGleb Smirnoff 
1178e834a840SGleb Smirnoff 		sbfree(sb, m);
1179e834a840SGleb Smirnoff 		n = m->m_next;
1180e834a840SGleb Smirnoff 		m->m_next = mfree;
1181e834a840SGleb Smirnoff 		mfree = m;
1182e834a840SGleb Smirnoff 		m = n;
1183e834a840SGleb Smirnoff 	}
1184df8bae1dSRodney W. Grimes 	if (m) {
1185df8bae1dSRodney W. Grimes 		sb->sb_mb = m;
1186df8bae1dSRodney W. Grimes 		m->m_nextpkt = next;
1187df8bae1dSRodney W. Grimes 	} else
1188df8bae1dSRodney W. Grimes 		sb->sb_mb = next;
1189395bb186SSam Leffler 	/*
1190050ac265SRobert Watson 	 * First part is an inline SB_EMPTY_FIXUP().  Second part makes sure
1191050ac265SRobert Watson 	 * sb_lastrecord is up-to-date if we dropped part of the last record.
1192395bb186SSam Leffler 	 */
1193395bb186SSam Leffler 	m = sb->sb_mb;
1194395bb186SSam Leffler 	if (m == NULL) {
1195395bb186SSam Leffler 		sb->sb_mbtail = NULL;
1196395bb186SSam Leffler 		sb->sb_lastrecord = NULL;
1197395bb186SSam Leffler 	} else if (m->m_nextpkt == NULL) {
1198395bb186SSam Leffler 		sb->sb_lastrecord = m;
1199395bb186SSam Leffler 	}
12001d2df300SGleb Smirnoff 
12011d2df300SGleb Smirnoff 	return (mfree);
1202df8bae1dSRodney W. Grimes }
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes /*
1205a34b7046SRobert Watson  * Drop data from (the front of) a sockbuf.
1206a34b7046SRobert Watson  */
1207a34b7046SRobert Watson void
1208050ac265SRobert Watson sbdrop_locked(struct sockbuf *sb, int len)
1209eaa6dfbcSRobert Watson {
1210eaa6dfbcSRobert Watson 
1211eaa6dfbcSRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
12121d2df300SGleb Smirnoff 	m_freem(sbcut_internal(sb, len));
12131d2df300SGleb Smirnoff }
1214eaa6dfbcSRobert Watson 
12151d2df300SGleb Smirnoff /*
12161d2df300SGleb Smirnoff  * Drop data from (the front of) a sockbuf,
12171d2df300SGleb Smirnoff  * and return it to caller.
12181d2df300SGleb Smirnoff  */
12191d2df300SGleb Smirnoff struct mbuf *
12201d2df300SGleb Smirnoff sbcut_locked(struct sockbuf *sb, int len)
12211d2df300SGleb Smirnoff {
12221d2df300SGleb Smirnoff 
12231d2df300SGleb Smirnoff 	SOCKBUF_LOCK_ASSERT(sb);
12241d2df300SGleb Smirnoff 	return (sbcut_internal(sb, len));
1225eaa6dfbcSRobert Watson }
1226eaa6dfbcSRobert Watson 
1227eaa6dfbcSRobert Watson void
1228050ac265SRobert Watson sbdrop(struct sockbuf *sb, int len)
1229a34b7046SRobert Watson {
12301d2df300SGleb Smirnoff 	struct mbuf *mfree;
1231a34b7046SRobert Watson 
1232a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
12331d2df300SGleb Smirnoff 	mfree = sbcut_internal(sb, len);
1234a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
12351d2df300SGleb Smirnoff 
12361d2df300SGleb Smirnoff 	m_freem(mfree);
1237a34b7046SRobert Watson }
1238a34b7046SRobert Watson 
12394e023759SAndre Oppermann /*
12404e023759SAndre Oppermann  * Maintain a pointer and offset pair into the socket buffer mbuf chain to
12414e023759SAndre Oppermann  * avoid traversal of the entire socket buffer for larger offsets.
12424e023759SAndre Oppermann  */
12434e023759SAndre Oppermann struct mbuf *
12444e023759SAndre Oppermann sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
12454e023759SAndre Oppermann {
12464e023759SAndre Oppermann 	struct mbuf *m, *ret;
12474e023759SAndre Oppermann 
12484e023759SAndre Oppermann 	KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
12490f9d0a73SGleb Smirnoff 	KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__));
12500f9d0a73SGleb Smirnoff 	KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__));
12514e023759SAndre Oppermann 
12524e023759SAndre Oppermann 	/*
12534e023759SAndre Oppermann 	 * Is off below stored offset? Happens on retransmits.
12544e023759SAndre Oppermann 	 * Just return, we can't help here.
12554e023759SAndre Oppermann 	 */
12564e023759SAndre Oppermann 	if (sb->sb_sndptroff > off) {
12574e023759SAndre Oppermann 		*moff = off;
12584e023759SAndre Oppermann 		return (sb->sb_mb);
12594e023759SAndre Oppermann 	}
12604e023759SAndre Oppermann 
12614e023759SAndre Oppermann 	/* Return closest mbuf in chain for current offset. */
12624e023759SAndre Oppermann 	*moff = off - sb->sb_sndptroff;
12634e023759SAndre Oppermann 	m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
12640963c8e4SLawrence Stewart 	if (*moff == m->m_len) {
12650963c8e4SLawrence Stewart 		*moff = 0;
12660963c8e4SLawrence Stewart 		sb->sb_sndptroff += m->m_len;
12670963c8e4SLawrence Stewart 		m = ret = m->m_next;
12680963c8e4SLawrence Stewart 		KASSERT(ret->m_len > 0,
12690963c8e4SLawrence Stewart 		    ("mbuf %p in sockbuf %p chain has no valid data", ret, sb));
12700963c8e4SLawrence Stewart 	}
12714e023759SAndre Oppermann 
12724e023759SAndre Oppermann 	/* Advance by len to be as close as possible for the next transmit. */
12734e023759SAndre Oppermann 	for (off = off - sb->sb_sndptroff + len - 1;
12746f4745d5SBjoern A. Zeeb 	     off > 0 && m != NULL && off >= m->m_len;
12754e023759SAndre Oppermann 	     m = m->m_next) {
12764e023759SAndre Oppermann 		sb->sb_sndptroff += m->m_len;
12774e023759SAndre Oppermann 		off -= m->m_len;
12784e023759SAndre Oppermann 	}
12796f4745d5SBjoern A. Zeeb 	if (off > 0 && m == NULL)
12806f4745d5SBjoern A. Zeeb 		panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret);
12814e023759SAndre Oppermann 	sb->sb_sndptr = m;
12824e023759SAndre Oppermann 
12834e023759SAndre Oppermann 	return (ret);
12844e023759SAndre Oppermann }
12854e023759SAndre Oppermann 
1286a34b7046SRobert Watson /*
12879fd573c3SHans Petter Selasky  * Return the first mbuf and the mbuf data offset for the provided
12889fd573c3SHans Petter Selasky  * send offset without changing the "sb_sndptroff" field.
12899fd573c3SHans Petter Selasky  */
12909fd573c3SHans Petter Selasky struct mbuf *
12919fd573c3SHans Petter Selasky sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
12929fd573c3SHans Petter Selasky {
12939fd573c3SHans Petter Selasky 	struct mbuf *m;
12949fd573c3SHans Petter Selasky 
12959fd573c3SHans Petter Selasky 	KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
12969fd573c3SHans Petter Selasky 
12979fd573c3SHans Petter Selasky 	/*
12989fd573c3SHans Petter Selasky 	 * If the "off" is below the stored offset, which happens on
12999fd573c3SHans Petter Selasky 	 * retransmits, just use "sb_mb":
13009fd573c3SHans Petter Selasky 	 */
13019fd573c3SHans Petter Selasky 	if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
13029fd573c3SHans Petter Selasky 		m = sb->sb_mb;
13039fd573c3SHans Petter Selasky 	} else {
13049fd573c3SHans Petter Selasky 		m = sb->sb_sndptr;
13059fd573c3SHans Petter Selasky 		off -= sb->sb_sndptroff;
13069fd573c3SHans Petter Selasky 	}
13079fd573c3SHans Petter Selasky 	while (off > 0 && m != NULL) {
13089fd573c3SHans Petter Selasky 		if (off < m->m_len)
13099fd573c3SHans Petter Selasky 			break;
13109fd573c3SHans Petter Selasky 		off -= m->m_len;
13119fd573c3SHans Petter Selasky 		m = m->m_next;
13129fd573c3SHans Petter Selasky 	}
13139fd573c3SHans Petter Selasky 	*moff = off;
13149fd573c3SHans Petter Selasky 	return (m);
13159fd573c3SHans Petter Selasky }
13169fd573c3SHans Petter Selasky 
13179fd573c3SHans Petter Selasky /*
1318050ac265SRobert Watson  * Drop a record off the front of a sockbuf and move the next record to the
1319050ac265SRobert Watson  * front.
1320df8bae1dSRodney W. Grimes  */
132126f9a767SRodney W. Grimes void
1322050ac265SRobert Watson sbdroprecord_locked(struct sockbuf *sb)
1323df8bae1dSRodney W. Grimes {
1324050ac265SRobert Watson 	struct mbuf *m;
1325df8bae1dSRodney W. Grimes 
1326a34b7046SRobert Watson 	SOCKBUF_LOCK_ASSERT(sb);
1327a34b7046SRobert Watson 
1328df8bae1dSRodney W. Grimes 	m = sb->sb_mb;
1329df8bae1dSRodney W. Grimes 	if (m) {
1330df8bae1dSRodney W. Grimes 		sb->sb_mb = m->m_nextpkt;
1331df8bae1dSRodney W. Grimes 		do {
1332df8bae1dSRodney W. Grimes 			sbfree(sb, m);
1333ecde8f7cSMatthew Dillon 			m = m_free(m);
1334797f2d22SPoul-Henning Kamp 		} while (m);
1335df8bae1dSRodney W. Grimes 	}
1336395bb186SSam Leffler 	SB_EMPTY_FIXUP(sb);
1337df8bae1dSRodney W. Grimes }
13381e4ad9ceSGarrett Wollman 
133982c23ebaSBill Fenner /*
1340050ac265SRobert Watson  * Drop a record off the front of a sockbuf and move the next record to the
1341050ac265SRobert Watson  * front.
1342a34b7046SRobert Watson  */
1343a34b7046SRobert Watson void
1344050ac265SRobert Watson sbdroprecord(struct sockbuf *sb)
1345a34b7046SRobert Watson {
1346a34b7046SRobert Watson 
1347a34b7046SRobert Watson 	SOCKBUF_LOCK(sb);
1348a34b7046SRobert Watson 	sbdroprecord_locked(sb);
1349a34b7046SRobert Watson 	SOCKBUF_UNLOCK(sb);
1350a34b7046SRobert Watson }
1351a34b7046SRobert Watson 
135220d9e5e8SRobert Watson /*
13538c799760SRobert Watson  * Create a "control" mbuf containing the specified data with the specified
13548c799760SRobert Watson  * type for presentation on a socket buffer.
135520d9e5e8SRobert Watson  */
135620d9e5e8SRobert Watson struct mbuf *
1357d19e16a7SRobert Watson sbcreatecontrol(caddr_t p, int size, int type, int level)
135820d9e5e8SRobert Watson {
1359d19e16a7SRobert Watson 	struct cmsghdr *cp;
136020d9e5e8SRobert Watson 	struct mbuf *m;
136120d9e5e8SRobert Watson 
136220d9e5e8SRobert Watson 	if (CMSG_SPACE((u_int)size) > MCLBYTES)
136320d9e5e8SRobert Watson 		return ((struct mbuf *) NULL);
136420d9e5e8SRobert Watson 	if (CMSG_SPACE((u_int)size) > MLEN)
1365eb1b1807SGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_CONTROL, 0);
136620d9e5e8SRobert Watson 	else
1367eb1b1807SGleb Smirnoff 		m = m_get(M_NOWAIT, MT_CONTROL);
136820d9e5e8SRobert Watson 	if (m == NULL)
136920d9e5e8SRobert Watson 		return ((struct mbuf *) NULL);
137020d9e5e8SRobert Watson 	cp = mtod(m, struct cmsghdr *);
137120d9e5e8SRobert Watson 	m->m_len = 0;
137220d9e5e8SRobert Watson 	KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
137320d9e5e8SRobert Watson 	    ("sbcreatecontrol: short mbuf"));
13742827952eSXin LI 	/*
13752827952eSXin LI 	 * Don't leave the padding between the msg header and the
13762827952eSXin LI 	 * cmsg data and the padding after the cmsg data un-initialized.
13772827952eSXin LI 	 */
13782827952eSXin LI 	bzero(cp, CMSG_SPACE((u_int)size));
137920d9e5e8SRobert Watson 	if (p != NULL)
138020d9e5e8SRobert Watson 		(void)memcpy(CMSG_DATA(cp), p, size);
138120d9e5e8SRobert Watson 	m->m_len = CMSG_SPACE(size);
138220d9e5e8SRobert Watson 	cp->cmsg_len = CMSG_LEN(size);
138320d9e5e8SRobert Watson 	cp->cmsg_level = level;
138420d9e5e8SRobert Watson 	cp->cmsg_type = type;
138520d9e5e8SRobert Watson 	return (m);
138620d9e5e8SRobert Watson }
138720d9e5e8SRobert Watson 
138820d9e5e8SRobert Watson /*
13898c799760SRobert Watson  * This does the same for socket buffers that sotoxsocket does for sockets:
13908c799760SRobert Watson  * generate an user-format data structure describing the socket buffer.  Note
13918c799760SRobert Watson  * that the xsockbuf structure, since it is always embedded in a socket, does
13928c799760SRobert Watson  * not include a self pointer nor a length.  We make this entry point public
13938c799760SRobert Watson  * in case some other mechanism needs it.
139420d9e5e8SRobert Watson  */
139520d9e5e8SRobert Watson void
139620d9e5e8SRobert Watson sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
139720d9e5e8SRobert Watson {
1398d19e16a7SRobert Watson 
13990f9d0a73SGleb Smirnoff 	xsb->sb_cc = sb->sb_ccc;
140020d9e5e8SRobert Watson 	xsb->sb_hiwat = sb->sb_hiwat;
140120d9e5e8SRobert Watson 	xsb->sb_mbcnt = sb->sb_mbcnt;
140249f287f8SGeorge V. Neville-Neil 	xsb->sb_mcnt = sb->sb_mcnt;
140349f287f8SGeorge V. Neville-Neil 	xsb->sb_ccnt = sb->sb_ccnt;
140420d9e5e8SRobert Watson 	xsb->sb_mbmax = sb->sb_mbmax;
140520d9e5e8SRobert Watson 	xsb->sb_lowat = sb->sb_lowat;
140620d9e5e8SRobert Watson 	xsb->sb_flags = sb->sb_flags;
140720d9e5e8SRobert Watson 	xsb->sb_timeo = sb->sb_timeo;
140820d9e5e8SRobert Watson }
140920d9e5e8SRobert Watson 
1410639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1411639acc13SGarrett Wollman static int dummy;
1412639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
14131b978d45SHartmut Brandt SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
14141b978d45SHartmut Brandt     &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
14151b978d45SHartmut Brandt SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
14163eb9ab52SEitan Adler     &sb_efficiency, 0, "Socket buffer size waste factor");
1417