19454b2d8SWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro 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 46364290befSGleb Smirnoff SOCK_LOCK(so); 46464290befSGleb Smirnoff if (SOLISTENING(so)) { 46564290befSGleb Smirnoff switch (cmd) { 46664290befSGleb Smirnoff case SO_SNDLOWAT: 46764290befSGleb Smirnoff case SO_SNDBUF: 46864290befSGleb Smirnoff lowat = &so->sol_sbsnd_lowat; 46964290befSGleb Smirnoff hiwat = &so->sol_sbsnd_hiwat; 47064290befSGleb Smirnoff flags = &so->sol_sbsnd_flags; 47164290befSGleb Smirnoff break; 47264290befSGleb Smirnoff case SO_RCVLOWAT: 47364290befSGleb Smirnoff case SO_RCVBUF: 47464290befSGleb Smirnoff lowat = &so->sol_sbrcv_lowat; 47564290befSGleb Smirnoff hiwat = &so->sol_sbrcv_hiwat; 47664290befSGleb Smirnoff flags = &so->sol_sbrcv_flags; 47764290befSGleb Smirnoff break; 47864290befSGleb Smirnoff } 47964290befSGleb Smirnoff } else { 48064290befSGleb Smirnoff switch (cmd) { 48164290befSGleb Smirnoff case SO_SNDLOWAT: 48264290befSGleb Smirnoff case SO_SNDBUF: 48364290befSGleb Smirnoff sb = &so->so_snd; 48464290befSGleb Smirnoff break; 48564290befSGleb Smirnoff case SO_RCVLOWAT: 48664290befSGleb Smirnoff case SO_RCVBUF: 48764290befSGleb Smirnoff sb = &so->so_rcv; 48864290befSGleb Smirnoff break; 48964290befSGleb Smirnoff } 49064290befSGleb Smirnoff flags = &sb->sb_flags; 49164290befSGleb Smirnoff hiwat = &sb->sb_hiwat; 49264290befSGleb Smirnoff lowat = &sb->sb_lowat; 4933f11a2f3SRobert Watson SOCKBUF_LOCK(sb); 49464290befSGleb Smirnoff } 49564290befSGleb Smirnoff 49664290befSGleb Smirnoff error = 0; 49764290befSGleb Smirnoff switch (cmd) { 49864290befSGleb Smirnoff case SO_SNDBUF: 49964290befSGleb Smirnoff case SO_RCVBUF: 50064290befSGleb Smirnoff if (SOLISTENING(so)) { 50164290befSGleb Smirnoff if (cc > sb_max_adj) { 50264290befSGleb Smirnoff error = ENOBUFS; 50364290befSGleb Smirnoff break; 50464290befSGleb Smirnoff } 50564290befSGleb Smirnoff *hiwat = cc; 50664290befSGleb Smirnoff if (*lowat > *hiwat) 50764290befSGleb Smirnoff *lowat = *hiwat; 50864290befSGleb Smirnoff } else { 50964290befSGleb Smirnoff if (!sbreserve_locked(sb, cc, so, curthread)) 51064290befSGleb Smirnoff error = ENOBUFS; 51164290befSGleb Smirnoff } 51264290befSGleb Smirnoff if (error == 0) 51364290befSGleb Smirnoff *flags &= ~SB_AUTOSIZE; 51464290befSGleb Smirnoff break; 51564290befSGleb Smirnoff case SO_SNDLOWAT: 51664290befSGleb Smirnoff case SO_RCVLOWAT: 51764290befSGleb Smirnoff /* 51864290befSGleb Smirnoff * Make sure the low-water is never greater than the 51964290befSGleb Smirnoff * high-water. 52064290befSGleb Smirnoff */ 52164290befSGleb Smirnoff *lowat = (cc > *hiwat) ? *hiwat : cc; 52264290befSGleb Smirnoff break; 52364290befSGleb Smirnoff } 52464290befSGleb Smirnoff 52564290befSGleb Smirnoff if (!SOLISTENING(so)) 5263f11a2f3SRobert Watson SOCKBUF_UNLOCK(sb); 52764290befSGleb Smirnoff SOCK_UNLOCK(so); 5283f11a2f3SRobert Watson return (error); 5293f11a2f3SRobert Watson } 5303f11a2f3SRobert Watson 531df8bae1dSRodney W. Grimes /* 532df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 533df8bae1dSRodney W. Grimes */ 5343f0bfcccSRobert Watson void 535050ac265SRobert Watson sbrelease_internal(struct sockbuf *sb, struct socket *so) 536eaa6dfbcSRobert Watson { 537eaa6dfbcSRobert Watson 538eaa6dfbcSRobert Watson sbflush_internal(sb); 539eaa6dfbcSRobert Watson (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 540eaa6dfbcSRobert Watson RLIM_INFINITY); 541eaa6dfbcSRobert Watson sb->sb_mbmax = 0; 542eaa6dfbcSRobert Watson } 543eaa6dfbcSRobert Watson 54426f9a767SRodney W. Grimes void 545050ac265SRobert Watson sbrelease_locked(struct sockbuf *sb, struct socket *so) 546df8bae1dSRodney W. Grimes { 547df8bae1dSRodney W. Grimes 548a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 549a34b7046SRobert Watson 550eaa6dfbcSRobert Watson sbrelease_internal(sb, so); 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes 553a34b7046SRobert Watson void 554050ac265SRobert Watson sbrelease(struct sockbuf *sb, struct socket *so) 555a34b7046SRobert Watson { 556a34b7046SRobert Watson 557a34b7046SRobert Watson SOCKBUF_LOCK(sb); 558a34b7046SRobert Watson sbrelease_locked(sb, so); 559a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 560a34b7046SRobert Watson } 561eaa6dfbcSRobert Watson 562eaa6dfbcSRobert Watson void 563050ac265SRobert Watson sbdestroy(struct sockbuf *sb, struct socket *so) 564eaa6dfbcSRobert Watson { 565eaa6dfbcSRobert Watson 566eaa6dfbcSRobert Watson sbrelease_internal(sb, so); 567eaa6dfbcSRobert Watson } 568eaa6dfbcSRobert Watson 569df8bae1dSRodney W. Grimes /* 570050ac265SRobert Watson * Routines to add and remove data from an mbuf queue. 571df8bae1dSRodney W. Grimes * 572050ac265SRobert Watson * The routines sbappend() or sbappendrecord() are normally called to append 573050ac265SRobert Watson * new mbufs to a socket buffer, after checking that adequate space is 574050ac265SRobert Watson * available, comparing the function sbspace() with the amount of data to be 575050ac265SRobert Watson * added. sbappendrecord() differs from sbappend() in that data supplied is 576050ac265SRobert Watson * treated as the beginning of a new record. To place a sender's address, 577050ac265SRobert Watson * optional access rights, and data in a socket receive buffer, 578050ac265SRobert Watson * sbappendaddr() should be used. To place access rights and data in a 579050ac265SRobert Watson * socket receive buffer, sbappendrights() should be used. In either case, 580050ac265SRobert Watson * the new data begins a new record. Note that unlike sbappend() and 581050ac265SRobert Watson * sbappendrecord(), these routines check for the caller that there will be 582050ac265SRobert Watson * enough space to store the data. Each fails if there is not enough space, 583050ac265SRobert Watson * or if it cannot find mbufs to store additional information in. 584df8bae1dSRodney W. Grimes * 585050ac265SRobert Watson * Reliable protocols may use the socket send buffer to hold data awaiting 586050ac265SRobert Watson * acknowledgement. Data is normally copied from a socket send buffer in a 587050ac265SRobert Watson * protocol with m_copy for output to a peer, and then removing the data from 588050ac265SRobert Watson * the socket buffer with sbdrop() or sbdroprecord() when the data is 589050ac265SRobert Watson * acknowledged by the peer. 590df8bae1dSRodney W. Grimes */ 591395bb186SSam Leffler #ifdef SOCKBUF_DEBUG 592395bb186SSam Leffler void 593395bb186SSam Leffler sblastrecordchk(struct sockbuf *sb, const char *file, int line) 594395bb186SSam Leffler { 595395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 596395bb186SSam Leffler 597a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 598a34b7046SRobert Watson 599395bb186SSam Leffler while (m && m->m_nextpkt) 600395bb186SSam Leffler m = m->m_nextpkt; 601395bb186SSam Leffler 602395bb186SSam Leffler if (m != sb->sb_lastrecord) { 603395bb186SSam Leffler printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 604395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_lastrecord, m); 605395bb186SSam Leffler printf("packet chain:\n"); 606395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 607395bb186SSam Leffler printf("\t%p\n", m); 608395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 609395bb186SSam Leffler } 610395bb186SSam Leffler } 611395bb186SSam Leffler 612395bb186SSam Leffler void 613395bb186SSam Leffler sblastmbufchk(struct sockbuf *sb, const char *file, int line) 614395bb186SSam Leffler { 615395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 616395bb186SSam Leffler struct mbuf *n; 617395bb186SSam Leffler 618a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 619a34b7046SRobert Watson 620395bb186SSam Leffler while (m && m->m_nextpkt) 621395bb186SSam Leffler m = m->m_nextpkt; 622395bb186SSam Leffler 623395bb186SSam Leffler while (m && m->m_next) 624395bb186SSam Leffler m = m->m_next; 625395bb186SSam Leffler 626395bb186SSam Leffler if (m != sb->sb_mbtail) { 627395bb186SSam Leffler printf("%s: sb_mb %p sb_mbtail %p last %p\n", 628395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_mbtail, m); 629395bb186SSam Leffler printf("packet tree:\n"); 630395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 631395bb186SSam Leffler printf("\t"); 632395bb186SSam Leffler for (n = m; n != NULL; n = n->m_next) 633395bb186SSam Leffler printf("%p ", n); 634395bb186SSam Leffler printf("\n"); 635395bb186SSam Leffler } 636395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 637395bb186SSam Leffler } 638395bb186SSam Leffler } 639395bb186SSam Leffler #endif /* SOCKBUF_DEBUG */ 640395bb186SSam Leffler 641395bb186SSam Leffler #define SBLINKRECORD(sb, m0) do { \ 642a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); \ 643395bb186SSam Leffler if ((sb)->sb_lastrecord != NULL) \ 644395bb186SSam Leffler (sb)->sb_lastrecord->m_nextpkt = (m0); \ 645395bb186SSam Leffler else \ 646395bb186SSam Leffler (sb)->sb_mb = (m0); \ 647395bb186SSam Leffler (sb)->sb_lastrecord = (m0); \ 648395bb186SSam Leffler } while (/*CONSTCOND*/0) 649395bb186SSam Leffler 650df8bae1dSRodney W. Grimes /* 651050ac265SRobert Watson * Append mbuf chain m to the last record in the socket buffer sb. The 652050ac265SRobert Watson * additional space associated the mbuf chain is recorded in sb. Empty mbufs 653050ac265SRobert Watson * are discarded and mbufs are compacted where possible. 654df8bae1dSRodney W. Grimes */ 65526f9a767SRodney W. Grimes void 656829fae90SGleb Smirnoff sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags) 657df8bae1dSRodney W. Grimes { 658050ac265SRobert Watson struct mbuf *n; 659df8bae1dSRodney W. Grimes 660a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 661a34b7046SRobert Watson 662b85f65afSPedro F. Giffuni if (m == NULL) 663df8bae1dSRodney W. Grimes return; 664829fae90SGleb Smirnoff sbm_clrprotoflags(m, flags); 665395bb186SSam Leffler SBLASTRECORDCHK(sb); 666797f2d22SPoul-Henning Kamp n = sb->sb_mb; 667797f2d22SPoul-Henning Kamp if (n) { 668df8bae1dSRodney W. Grimes while (n->m_nextpkt) 669df8bae1dSRodney W. Grimes n = n->m_nextpkt; 670df8bae1dSRodney W. Grimes do { 671df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 672a34b7046SRobert Watson sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 673df8bae1dSRodney W. Grimes return; 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 676395bb186SSam Leffler } else { 677395bb186SSam Leffler /* 678395bb186SSam Leffler * XXX Would like to simply use sb_mbtail here, but 679395bb186SSam Leffler * XXX I need to verify that I won't miss an EOR that 680395bb186SSam Leffler * XXX way. 681395bb186SSam Leffler */ 682395bb186SSam Leffler if ((n = sb->sb_lastrecord) != NULL) { 683395bb186SSam Leffler do { 684395bb186SSam Leffler if (n->m_flags & M_EOR) { 685a34b7046SRobert Watson sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 686395bb186SSam Leffler return; 687395bb186SSam Leffler } 688395bb186SSam Leffler } while (n->m_next && (n = n->m_next)); 689395bb186SSam Leffler } else { 690395bb186SSam Leffler /* 691395bb186SSam Leffler * If this is the first record in the socket buffer, 692395bb186SSam Leffler * it's also the last record. 693395bb186SSam Leffler */ 694395bb186SSam Leffler sb->sb_lastrecord = m; 695395bb186SSam Leffler } 696df8bae1dSRodney W. Grimes } 697df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 698395bb186SSam Leffler SBLASTRECORDCHK(sb); 699395bb186SSam Leffler } 700395bb186SSam Leffler 701395bb186SSam Leffler /* 702050ac265SRobert Watson * Append mbuf chain m to the last record in the socket buffer sb. The 703050ac265SRobert Watson * additional space associated the mbuf chain is recorded in sb. Empty mbufs 704050ac265SRobert Watson * are discarded and mbufs are compacted where possible. 705a34b7046SRobert Watson */ 706a34b7046SRobert Watson void 707829fae90SGleb Smirnoff sbappend(struct sockbuf *sb, struct mbuf *m, int flags) 708a34b7046SRobert Watson { 709a34b7046SRobert Watson 710a34b7046SRobert Watson SOCKBUF_LOCK(sb); 711829fae90SGleb Smirnoff sbappend_locked(sb, m, flags); 712a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 713a34b7046SRobert Watson } 714a34b7046SRobert Watson 715a34b7046SRobert Watson /* 716050ac265SRobert Watson * This version of sbappend() should only be used when the caller absolutely 717050ac265SRobert Watson * knows that there will never be more than one record in the socket buffer, 718050ac265SRobert Watson * that is, a stream protocol (such as TCP). 719395bb186SSam Leffler */ 720395bb186SSam Leffler void 721651e4e6aSGleb Smirnoff sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags) 722395bb186SSam Leffler { 723a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 724395bb186SSam Leffler 725395bb186SSam Leffler KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 726395bb186SSam Leffler KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 727395bb186SSam Leffler 728395bb186SSam Leffler SBLASTMBUFCHK(sb); 729395bb186SSam Leffler 730844cacd1SGleb Smirnoff /* Remove all packet headers and mbuf tags to get a pure data chain. */ 731651e4e6aSGleb Smirnoff m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0); 732844cacd1SGleb Smirnoff 733395bb186SSam Leffler sbcompress(sb, m, sb->sb_mbtail); 734395bb186SSam Leffler 735395bb186SSam Leffler sb->sb_lastrecord = sb->sb_mb; 736395bb186SSam Leffler SBLASTRECORDCHK(sb); 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes 739a34b7046SRobert Watson /* 740050ac265SRobert Watson * This version of sbappend() should only be used when the caller absolutely 741050ac265SRobert Watson * knows that there will never be more than one record in the socket buffer, 742050ac265SRobert Watson * that is, a stream protocol (such as TCP). 743a34b7046SRobert Watson */ 744a34b7046SRobert Watson void 745651e4e6aSGleb Smirnoff sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags) 746a34b7046SRobert Watson { 747a34b7046SRobert Watson 748a34b7046SRobert Watson SOCKBUF_LOCK(sb); 749651e4e6aSGleb Smirnoff sbappendstream_locked(sb, m, flags); 750a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 751a34b7046SRobert Watson } 752a34b7046SRobert Watson 753df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 75426f9a767SRodney W. Grimes void 75557f43a45SGleb Smirnoff sbcheck(struct sockbuf *sb, const char *file, int line) 756df8bae1dSRodney W. Grimes { 7570f9d0a73SGleb Smirnoff struct mbuf *m, *n, *fnrdy; 7580f9d0a73SGleb Smirnoff u_long acc, ccc, mbcnt; 759df8bae1dSRodney W. Grimes 760a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 761a34b7046SRobert Watson 7620f9d0a73SGleb Smirnoff acc = ccc = mbcnt = 0; 7630f9d0a73SGleb Smirnoff fnrdy = NULL; 76457f43a45SGleb Smirnoff 7650931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 7660931333fSBill Fenner n = m->m_nextpkt; 7670931333fSBill Fenner for (; m; m = m->m_next) { 76857f43a45SGleb Smirnoff if (m->m_len == 0) { 76957f43a45SGleb Smirnoff printf("sb %p empty mbuf %p\n", sb, m); 77057f43a45SGleb Smirnoff goto fail; 77157f43a45SGleb Smirnoff } 7720f9d0a73SGleb Smirnoff if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) { 7730f9d0a73SGleb Smirnoff if (m != sb->sb_fnrdy) { 7740f9d0a73SGleb Smirnoff printf("sb %p: fnrdy %p != m %p\n", 7750f9d0a73SGleb Smirnoff sb, sb->sb_fnrdy, m); 7760f9d0a73SGleb Smirnoff goto fail; 7770f9d0a73SGleb Smirnoff } 7780f9d0a73SGleb Smirnoff fnrdy = m; 7790f9d0a73SGleb Smirnoff } 7800f9d0a73SGleb Smirnoff if (fnrdy) { 7810f9d0a73SGleb Smirnoff if (!(m->m_flags & M_NOTAVAIL)) { 7820f9d0a73SGleb Smirnoff printf("sb %p: fnrdy %p, m %p is avail\n", 7830f9d0a73SGleb Smirnoff sb, sb->sb_fnrdy, m); 7840f9d0a73SGleb Smirnoff goto fail; 7850f9d0a73SGleb Smirnoff } 7860f9d0a73SGleb Smirnoff } else 7870f9d0a73SGleb Smirnoff acc += m->m_len; 7880f9d0a73SGleb Smirnoff ccc += m->m_len; 789df8bae1dSRodney W. Grimes mbcnt += MSIZE; 790313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 791df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 7920931333fSBill Fenner } 793df8bae1dSRodney W. Grimes } 7940f9d0a73SGleb Smirnoff if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) { 7950f9d0a73SGleb Smirnoff printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n", 7960f9d0a73SGleb Smirnoff acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt); 79757f43a45SGleb Smirnoff goto fail; 798df8bae1dSRodney W. Grimes } 79957f43a45SGleb Smirnoff return; 80057f43a45SGleb Smirnoff fail: 80157f43a45SGleb Smirnoff panic("%s from %s:%u", __func__, file, line); 802df8bae1dSRodney W. Grimes } 803df8bae1dSRodney W. Grimes #endif 804df8bae1dSRodney W. Grimes 805df8bae1dSRodney W. Grimes /* 806050ac265SRobert Watson * As above, except the mbuf chain begins a new record. 807df8bae1dSRodney W. Grimes */ 80826f9a767SRodney W. Grimes void 809050ac265SRobert Watson sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0) 810df8bae1dSRodney W. Grimes { 811050ac265SRobert Watson struct mbuf *m; 812df8bae1dSRodney W. Grimes 813a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 814a34b7046SRobert Watson 815b85f65afSPedro F. Giffuni if (m0 == NULL) 816df8bae1dSRodney W. Grimes return; 81753b680caSGleb Smirnoff m_clrprotoflags(m0); 818df8bae1dSRodney W. Grimes /* 819050ac265SRobert Watson * Put the first mbuf on the queue. Note this permits zero length 820050ac265SRobert Watson * records. 821df8bae1dSRodney W. Grimes */ 822df8bae1dSRodney W. Grimes sballoc(sb, m0); 823395bb186SSam Leffler SBLASTRECORDCHK(sb); 824395bb186SSam Leffler SBLINKRECORD(sb, m0); 825e72a94adSMaksim Yevmenkin sb->sb_mbtail = m0; 826df8bae1dSRodney W. Grimes m = m0->m_next; 827df8bae1dSRodney W. Grimes m0->m_next = 0; 828df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 829df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 830df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 831df8bae1dSRodney W. Grimes } 832e72a94adSMaksim Yevmenkin /* always call sbcompress() so it can do SBLASTMBUFCHK() */ 833df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 834df8bae1dSRodney W. Grimes } 835df8bae1dSRodney W. Grimes 836df8bae1dSRodney W. Grimes /* 837050ac265SRobert Watson * As above, except the mbuf chain begins a new record. 838a34b7046SRobert Watson */ 839a34b7046SRobert Watson void 840050ac265SRobert Watson sbappendrecord(struct sockbuf *sb, struct mbuf *m0) 841a34b7046SRobert Watson { 842a34b7046SRobert Watson 843a34b7046SRobert Watson SOCKBUF_LOCK(sb); 844a34b7046SRobert Watson sbappendrecord_locked(sb, m0); 845a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 846a34b7046SRobert Watson } 847a34b7046SRobert Watson 8488de34a88SAlan Somers /* Helper routine that appends data, control, and address to a sockbuf. */ 8498de34a88SAlan Somers static int 8508de34a88SAlan Somers sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa, 8518de34a88SAlan Somers struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last) 852df8bae1dSRodney W. Grimes { 853395bb186SSam Leffler struct mbuf *m, *n, *nlast; 854c43cad1aSScott Long #if MSIZE <= 256 855df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 856df8bae1dSRodney W. Grimes return (0); 857c43cad1aSScott Long #endif 858c8b59ea7SGleb Smirnoff m = m_get(M_NOWAIT, MT_SONAME); 859c8b59ea7SGleb Smirnoff if (m == NULL) 860df8bae1dSRodney W. Grimes return (0); 861df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 86280208239SAlfred Perlstein bcopy(asa, mtod(m, caddr_t), asa->sa_len); 863c33a2313SAndrey V. Elsukov if (m0) { 86453b680caSGleb Smirnoff m_clrprotoflags(m0); 86557386f5dSAndrey V. Elsukov m_tag_delete_chain(m0, NULL); 866c33a2313SAndrey V. Elsukov /* 867c33a2313SAndrey V. Elsukov * Clear some persistent info from pkthdr. 868c33a2313SAndrey V. Elsukov * We don't use m_demote(), because some netgraph consumers 869c33a2313SAndrey V. Elsukov * expect M_PKTHDR presence. 870c33a2313SAndrey V. Elsukov */ 871c33a2313SAndrey V. Elsukov m0->m_pkthdr.rcvif = NULL; 872c33a2313SAndrey V. Elsukov m0->m_pkthdr.flowid = 0; 873c33a2313SAndrey V. Elsukov m0->m_pkthdr.csum_flags = 0; 874c33a2313SAndrey V. Elsukov m0->m_pkthdr.fibnum = 0; 875c33a2313SAndrey V. Elsukov m0->m_pkthdr.rsstype = 0; 876c33a2313SAndrey V. Elsukov } 8778de34a88SAlan Somers if (ctrl_last) 8788de34a88SAlan Somers ctrl_last->m_next = m0; /* concatenate data to control */ 879df8bae1dSRodney W. Grimes else 880df8bae1dSRodney W. Grimes control = m0; 881df8bae1dSRodney W. Grimes m->m_next = control; 882395bb186SSam Leffler for (n = m; n->m_next != NULL; n = n->m_next) 883df8bae1dSRodney W. Grimes sballoc(sb, n); 884395bb186SSam Leffler sballoc(sb, n); 885395bb186SSam Leffler nlast = n; 886395bb186SSam Leffler SBLINKRECORD(sb, m); 887395bb186SSam Leffler 888395bb186SSam Leffler sb->sb_mbtail = nlast; 889395bb186SSam Leffler SBLASTMBUFCHK(sb); 890395bb186SSam Leffler 891395bb186SSam Leffler SBLASTRECORDCHK(sb); 892df8bae1dSRodney W. Grimes return (1); 893df8bae1dSRodney W. Grimes } 894df8bae1dSRodney W. Grimes 895a34b7046SRobert Watson /* 896050ac265SRobert Watson * Append address and data, and optionally, control (ancillary) data to the 897050ac265SRobert Watson * receive queue of a socket. If present, m0 must include a packet header 898050ac265SRobert Watson * with total length. Returns 0 if no space in sockbuf or insufficient 899050ac265SRobert Watson * mbufs. 900a34b7046SRobert Watson */ 90126f9a767SRodney W. Grimes int 9028de34a88SAlan Somers sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 9038de34a88SAlan Somers struct mbuf *m0, struct mbuf *control) 9048de34a88SAlan Somers { 9058de34a88SAlan Somers struct mbuf *ctrl_last; 9068de34a88SAlan Somers int space = asa->sa_len; 9078de34a88SAlan Somers 9088de34a88SAlan Somers SOCKBUF_LOCK_ASSERT(sb); 9098de34a88SAlan Somers 9108de34a88SAlan Somers if (m0 && (m0->m_flags & M_PKTHDR) == 0) 9118de34a88SAlan Somers panic("sbappendaddr_locked"); 9128de34a88SAlan Somers if (m0) 9138de34a88SAlan Somers space += m0->m_pkthdr.len; 9148de34a88SAlan Somers space += m_length(control, &ctrl_last); 9158de34a88SAlan Somers 9168de34a88SAlan Somers if (space > sbspace(sb)) 9178de34a88SAlan Somers return (0); 9188de34a88SAlan Somers return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 9198de34a88SAlan Somers } 9208de34a88SAlan Somers 9218de34a88SAlan Somers /* 9228de34a88SAlan Somers * Append address and data, and optionally, control (ancillary) data to the 9238de34a88SAlan Somers * receive queue of a socket. If present, m0 must include a packet header 9248de34a88SAlan Somers * with total length. Returns 0 if insufficient mbufs. Does not validate space 9258de34a88SAlan Somers * on the receiving sockbuf. 9268de34a88SAlan Somers */ 9278de34a88SAlan Somers int 9288de34a88SAlan Somers sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa, 9298de34a88SAlan Somers struct mbuf *m0, struct mbuf *control) 9308de34a88SAlan Somers { 9318de34a88SAlan Somers struct mbuf *ctrl_last; 9328de34a88SAlan Somers 9338de34a88SAlan Somers SOCKBUF_LOCK_ASSERT(sb); 9348de34a88SAlan Somers 9358de34a88SAlan Somers ctrl_last = (control == NULL) ? NULL : m_last(control); 9368de34a88SAlan Somers return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 9378de34a88SAlan Somers } 9388de34a88SAlan Somers 9398de34a88SAlan Somers /* 9408de34a88SAlan Somers * Append address and data, and optionally, control (ancillary) data to the 9418de34a88SAlan Somers * receive queue of a socket. If present, m0 must include a packet header 9428de34a88SAlan Somers * with total length. Returns 0 if no space in sockbuf or insufficient 9438de34a88SAlan Somers * mbufs. 9448de34a88SAlan Somers */ 9458de34a88SAlan Somers int 946050ac265SRobert Watson sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 947050ac265SRobert Watson struct mbuf *m0, struct mbuf *control) 948a34b7046SRobert Watson { 949a34b7046SRobert Watson int retval; 950a34b7046SRobert Watson 951a34b7046SRobert Watson SOCKBUF_LOCK(sb); 952a34b7046SRobert Watson retval = sbappendaddr_locked(sb, asa, m0, control); 953a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 954a34b7046SRobert Watson return (retval); 955a34b7046SRobert Watson } 956a34b7046SRobert Watson 957a34b7046SRobert Watson int 958050ac265SRobert Watson sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 959050ac265SRobert Watson struct mbuf *control) 960df8bae1dSRodney W. Grimes { 961395bb186SSam Leffler struct mbuf *m, *n, *mlast; 9627ed60de8SPoul-Henning Kamp int space; 963df8bae1dSRodney W. Grimes 964a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 965a34b7046SRobert Watson 966b85f65afSPedro F. Giffuni if (control == NULL) 967a34b7046SRobert Watson panic("sbappendcontrol_locked"); 9687ed60de8SPoul-Henning Kamp space = m_length(control, &n) + m_length(m0, NULL); 969a34b7046SRobert Watson 970df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 971df8bae1dSRodney W. Grimes return (0); 97253b680caSGleb Smirnoff m_clrprotoflags(m0); 973df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 974395bb186SSam Leffler 975395bb186SSam Leffler SBLASTRECORDCHK(sb); 976395bb186SSam Leffler 977395bb186SSam Leffler for (m = control; m->m_next; m = m->m_next) 978df8bae1dSRodney W. Grimes sballoc(sb, m); 979395bb186SSam Leffler sballoc(sb, m); 980395bb186SSam Leffler mlast = m; 981395bb186SSam Leffler SBLINKRECORD(sb, control); 982395bb186SSam Leffler 983395bb186SSam Leffler sb->sb_mbtail = mlast; 984395bb186SSam Leffler SBLASTMBUFCHK(sb); 985395bb186SSam Leffler 986395bb186SSam Leffler SBLASTRECORDCHK(sb); 987df8bae1dSRodney W. Grimes return (1); 988df8bae1dSRodney W. Grimes } 989df8bae1dSRodney W. Grimes 990a34b7046SRobert Watson int 991050ac265SRobert Watson sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control) 992a34b7046SRobert Watson { 993a34b7046SRobert Watson int retval; 994a34b7046SRobert Watson 995a34b7046SRobert Watson SOCKBUF_LOCK(sb); 996a34b7046SRobert Watson retval = sbappendcontrol_locked(sb, m0, control); 997a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 998a34b7046SRobert Watson return (retval); 999a34b7046SRobert Watson } 1000a34b7046SRobert Watson 1001df8bae1dSRodney W. Grimes /* 10027da7362bSRobert Watson * Append the data in mbuf chain (m) into the socket buffer sb following mbuf 10037da7362bSRobert Watson * (n). If (n) is NULL, the buffer is presumed empty. 10047da7362bSRobert Watson * 10057da7362bSRobert Watson * When the data is compressed, mbufs in the chain may be handled in one of 10067da7362bSRobert Watson * three ways: 10077da7362bSRobert Watson * 10087da7362bSRobert Watson * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no 10097da7362bSRobert Watson * record boundary, and no change in data type). 10107da7362bSRobert Watson * 10117da7362bSRobert Watson * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into 10127da7362bSRobert Watson * an mbuf already in the socket buffer. This can occur if an 10130f9d0a73SGleb Smirnoff * appropriate mbuf exists, there is room, both mbufs are not marked as 10140f9d0a73SGleb Smirnoff * not ready, and no merging of data types will occur. 10157da7362bSRobert Watson * 10167da7362bSRobert Watson * (3) The mbuf may be appended to the end of the existing mbuf chain. 10177da7362bSRobert Watson * 10187da7362bSRobert Watson * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as 10197da7362bSRobert Watson * end-of-record. 1020df8bae1dSRodney W. Grimes */ 102126f9a767SRodney W. Grimes void 1022050ac265SRobert Watson sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 1023df8bae1dSRodney W. Grimes { 1024050ac265SRobert Watson int eor = 0; 1025050ac265SRobert Watson struct mbuf *o; 1026df8bae1dSRodney W. Grimes 1027a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1028a34b7046SRobert Watson 1029df8bae1dSRodney W. Grimes while (m) { 1030df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 1031df8bae1dSRodney W. Grimes if (m->m_len == 0 && 1032df8bae1dSRodney W. Grimes (eor == 0 || 1033df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 1034df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 1035395bb186SSam Leffler if (sb->sb_lastrecord == m) 1036395bb186SSam Leffler sb->sb_lastrecord = m->m_next; 1037df8bae1dSRodney W. Grimes m = m_free(m); 1038df8bae1dSRodney W. Grimes continue; 1039df8bae1dSRodney W. Grimes } 104032af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 104132af0d74SDavid Malone M_WRITABLE(n) && 10425e0f5cfaSKip Macy ((sb->sb_flags & SB_NOCOALESCE) == 0) && 10430f9d0a73SGleb Smirnoff !(m->m_flags & M_NOTREADY) && 10440f9d0a73SGleb Smirnoff !(n->m_flags & M_NOTREADY) && 104532af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 104632af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 1047df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 1048df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 1049df8bae1dSRodney W. Grimes (unsigned)m->m_len); 1050df8bae1dSRodney W. Grimes n->m_len += m->m_len; 10510f9d0a73SGleb Smirnoff sb->sb_ccc += m->m_len; 10520f9d0a73SGleb Smirnoff if (sb->sb_fnrdy == NULL) 10530f9d0a73SGleb Smirnoff sb->sb_acc += m->m_len; 105434333b16SAndre Oppermann if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1055b3f1af6bSTim J. Robbins /* XXX: Probably don't need.*/ 105604ac9b97SKelly Yancey sb->sb_ctl += m->m_len; 1057df8bae1dSRodney W. Grimes m = m_free(m); 1058df8bae1dSRodney W. Grimes continue; 1059df8bae1dSRodney W. Grimes } 1060df8bae1dSRodney W. Grimes if (n) 1061df8bae1dSRodney W. Grimes n->m_next = m; 1062df8bae1dSRodney W. Grimes else 1063df8bae1dSRodney W. Grimes sb->sb_mb = m; 1064395bb186SSam Leffler sb->sb_mbtail = m; 1065df8bae1dSRodney W. Grimes sballoc(sb, m); 1066df8bae1dSRodney W. Grimes n = m; 1067df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 1068df8bae1dSRodney W. Grimes m = m->m_next; 1069df8bae1dSRodney W. Grimes n->m_next = 0; 1070df8bae1dSRodney W. Grimes } 1071df8bae1dSRodney W. Grimes if (eor) { 10727da7362bSRobert Watson KASSERT(n != NULL, ("sbcompress: eor && n == NULL")); 1073df8bae1dSRodney W. Grimes n->m_flags |= eor; 1074df8bae1dSRodney W. Grimes } 1075395bb186SSam Leffler SBLASTMBUFCHK(sb); 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes 1078df8bae1dSRodney W. Grimes /* 1079050ac265SRobert Watson * Free all mbufs in a sockbuf. Check that all resources are reclaimed. 1080df8bae1dSRodney W. Grimes */ 1081eaa6dfbcSRobert Watson static void 1082050ac265SRobert Watson sbflush_internal(struct sockbuf *sb) 1083df8bae1dSRodney W. Grimes { 1084df8bae1dSRodney W. Grimes 108523f84772SPierre Beyssac while (sb->sb_mbcnt) { 108623f84772SPierre Beyssac /* 1087761a9a1fSGleb Smirnoff * Don't call sbcut(sb, 0) if the leading mbuf is non-empty: 108823f84772SPierre Beyssac * we would loop forever. Panic instead. 108923f84772SPierre Beyssac */ 10900f9d0a73SGleb Smirnoff if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 109123f84772SPierre Beyssac break; 10920f9d0a73SGleb Smirnoff m_freem(sbcut_internal(sb, (int)sb->sb_ccc)); 109323f84772SPierre Beyssac } 10940f9d0a73SGleb Smirnoff KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0, 10950f9d0a73SGleb Smirnoff ("%s: ccc %u mb %p mbcnt %u", __func__, 10960f9d0a73SGleb Smirnoff sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt)); 1097a34b7046SRobert Watson } 1098a34b7046SRobert Watson 1099a34b7046SRobert Watson void 1100050ac265SRobert Watson sbflush_locked(struct sockbuf *sb) 1101eaa6dfbcSRobert Watson { 1102eaa6dfbcSRobert Watson 1103eaa6dfbcSRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1104eaa6dfbcSRobert Watson sbflush_internal(sb); 1105eaa6dfbcSRobert Watson } 1106eaa6dfbcSRobert Watson 1107eaa6dfbcSRobert Watson void 1108050ac265SRobert Watson sbflush(struct sockbuf *sb) 1109a34b7046SRobert Watson { 1110a34b7046SRobert Watson 1111a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1112a34b7046SRobert Watson sbflush_locked(sb); 1113a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes 1116df8bae1dSRodney W. Grimes /* 11171d2df300SGleb Smirnoff * Cut data from (the front of) a sockbuf. 1118df8bae1dSRodney W. Grimes */ 11191d2df300SGleb Smirnoff static struct mbuf * 11201d2df300SGleb Smirnoff sbcut_internal(struct sockbuf *sb, int len) 1121df8bae1dSRodney W. Grimes { 11220f9d0a73SGleb Smirnoff struct mbuf *m, *next, *mfree; 1123df8bae1dSRodney W. Grimes 1124f41b2de7SHiren Panchasara KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0", 1125b5b023b9SHiren Panchasara __func__, len)); 1126b5b023b9SHiren Panchasara KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u", 1127b5b023b9SHiren Panchasara __func__, len, sb->sb_ccc)); 1128b5b023b9SHiren Panchasara 1129df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 11301d2df300SGleb Smirnoff mfree = NULL; 11311d2df300SGleb Smirnoff 1132df8bae1dSRodney W. Grimes while (len > 0) { 11338146bcfeSGleb Smirnoff if (m == NULL) { 11348146bcfeSGleb Smirnoff KASSERT(next, ("%s: no next, len %d", __func__, len)); 1135df8bae1dSRodney W. Grimes m = next; 1136df8bae1dSRodney W. Grimes next = m->m_nextpkt; 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes if (m->m_len > len) { 11390f9d0a73SGleb Smirnoff KASSERT(!(m->m_flags & M_NOTAVAIL), 11400f9d0a73SGleb Smirnoff ("%s: m %p M_NOTAVAIL", __func__, m)); 1141df8bae1dSRodney W. Grimes m->m_len -= len; 1142df8bae1dSRodney W. Grimes m->m_data += len; 11430f9d0a73SGleb Smirnoff sb->sb_ccc -= len; 11440f9d0a73SGleb Smirnoff sb->sb_acc -= len; 11454e023759SAndre Oppermann if (sb->sb_sndptroff != 0) 11464e023759SAndre Oppermann sb->sb_sndptroff -= len; 114734333b16SAndre Oppermann if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 114804ac9b97SKelly Yancey sb->sb_ctl -= len; 1149df8bae1dSRodney W. Grimes break; 1150df8bae1dSRodney W. Grimes } 1151df8bae1dSRodney W. Grimes len -= m->m_len; 1152df8bae1dSRodney W. Grimes sbfree(sb, m); 11530f9d0a73SGleb Smirnoff /* 11540f9d0a73SGleb Smirnoff * Do not put M_NOTREADY buffers to the free list, they 11550f9d0a73SGleb Smirnoff * are referenced from outside. 11560f9d0a73SGleb Smirnoff */ 11570f9d0a73SGleb Smirnoff if (m->m_flags & M_NOTREADY) 11580f9d0a73SGleb Smirnoff m = m->m_next; 11590f9d0a73SGleb Smirnoff else { 11600f9d0a73SGleb Smirnoff struct mbuf *n; 11610f9d0a73SGleb Smirnoff 11621d2df300SGleb Smirnoff n = m->m_next; 11631d2df300SGleb Smirnoff m->m_next = mfree; 11641d2df300SGleb Smirnoff mfree = m; 11651d2df300SGleb Smirnoff m = n; 1166df8bae1dSRodney W. Grimes } 11670f9d0a73SGleb Smirnoff } 1168e834a840SGleb Smirnoff /* 1169e834a840SGleb Smirnoff * Free any zero-length mbufs from the buffer. 1170e834a840SGleb Smirnoff * For SOCK_DGRAM sockets such mbufs represent empty records. 1171e834a840SGleb Smirnoff * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer, 1172e834a840SGleb Smirnoff * when sosend_generic() needs to send only control data. 1173e834a840SGleb Smirnoff */ 1174e834a840SGleb Smirnoff while (m && m->m_len == 0) { 1175e834a840SGleb Smirnoff struct mbuf *n; 1176e834a840SGleb Smirnoff 1177e834a840SGleb Smirnoff sbfree(sb, m); 1178e834a840SGleb Smirnoff n = m->m_next; 1179e834a840SGleb Smirnoff m->m_next = mfree; 1180e834a840SGleb Smirnoff mfree = m; 1181e834a840SGleb Smirnoff m = n; 1182e834a840SGleb Smirnoff } 1183df8bae1dSRodney W. Grimes if (m) { 1184df8bae1dSRodney W. Grimes sb->sb_mb = m; 1185df8bae1dSRodney W. Grimes m->m_nextpkt = next; 1186df8bae1dSRodney W. Grimes } else 1187df8bae1dSRodney W. Grimes sb->sb_mb = next; 1188395bb186SSam Leffler /* 1189050ac265SRobert Watson * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure 1190050ac265SRobert Watson * sb_lastrecord is up-to-date if we dropped part of the last record. 1191395bb186SSam Leffler */ 1192395bb186SSam Leffler m = sb->sb_mb; 1193395bb186SSam Leffler if (m == NULL) { 1194395bb186SSam Leffler sb->sb_mbtail = NULL; 1195395bb186SSam Leffler sb->sb_lastrecord = NULL; 1196395bb186SSam Leffler } else if (m->m_nextpkt == NULL) { 1197395bb186SSam Leffler sb->sb_lastrecord = m; 1198395bb186SSam Leffler } 11991d2df300SGleb Smirnoff 12001d2df300SGleb Smirnoff return (mfree); 1201df8bae1dSRodney W. Grimes } 1202df8bae1dSRodney W. Grimes 1203df8bae1dSRodney W. Grimes /* 1204a34b7046SRobert Watson * Drop data from (the front of) a sockbuf. 1205a34b7046SRobert Watson */ 1206a34b7046SRobert Watson void 1207050ac265SRobert Watson sbdrop_locked(struct sockbuf *sb, int len) 1208eaa6dfbcSRobert Watson { 1209eaa6dfbcSRobert Watson 1210eaa6dfbcSRobert Watson SOCKBUF_LOCK_ASSERT(sb); 12111d2df300SGleb Smirnoff m_freem(sbcut_internal(sb, len)); 12121d2df300SGleb Smirnoff } 1213eaa6dfbcSRobert Watson 12141d2df300SGleb Smirnoff /* 12151d2df300SGleb Smirnoff * Drop data from (the front of) a sockbuf, 12161d2df300SGleb Smirnoff * and return it to caller. 12171d2df300SGleb Smirnoff */ 12181d2df300SGleb Smirnoff struct mbuf * 12191d2df300SGleb Smirnoff sbcut_locked(struct sockbuf *sb, int len) 12201d2df300SGleb Smirnoff { 12211d2df300SGleb Smirnoff 12221d2df300SGleb Smirnoff SOCKBUF_LOCK_ASSERT(sb); 12231d2df300SGleb Smirnoff return (sbcut_internal(sb, len)); 1224eaa6dfbcSRobert Watson } 1225eaa6dfbcSRobert Watson 1226eaa6dfbcSRobert Watson void 1227050ac265SRobert Watson sbdrop(struct sockbuf *sb, int len) 1228a34b7046SRobert Watson { 12291d2df300SGleb Smirnoff struct mbuf *mfree; 1230a34b7046SRobert Watson 1231a34b7046SRobert Watson SOCKBUF_LOCK(sb); 12321d2df300SGleb Smirnoff mfree = sbcut_internal(sb, len); 1233a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 12341d2df300SGleb Smirnoff 12351d2df300SGleb Smirnoff m_freem(mfree); 1236a34b7046SRobert Watson } 1237a34b7046SRobert Watson 12384e023759SAndre Oppermann /* 12394e023759SAndre Oppermann * Maintain a pointer and offset pair into the socket buffer mbuf chain to 12404e023759SAndre Oppermann * avoid traversal of the entire socket buffer for larger offsets. 12414e023759SAndre Oppermann */ 12424e023759SAndre Oppermann struct mbuf * 12434e023759SAndre Oppermann sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff) 12444e023759SAndre Oppermann { 12454e023759SAndre Oppermann struct mbuf *m, *ret; 12464e023759SAndre Oppermann 12474e023759SAndre Oppermann KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 12480f9d0a73SGleb Smirnoff KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__)); 12490f9d0a73SGleb Smirnoff KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__)); 12504e023759SAndre Oppermann 12514e023759SAndre Oppermann /* 12524e023759SAndre Oppermann * Is off below stored offset? Happens on retransmits. 12534e023759SAndre Oppermann * Just return, we can't help here. 12544e023759SAndre Oppermann */ 12554e023759SAndre Oppermann if (sb->sb_sndptroff > off) { 12564e023759SAndre Oppermann *moff = off; 12574e023759SAndre Oppermann return (sb->sb_mb); 12584e023759SAndre Oppermann } 12594e023759SAndre Oppermann 12604e023759SAndre Oppermann /* Return closest mbuf in chain for current offset. */ 12614e023759SAndre Oppermann *moff = off - sb->sb_sndptroff; 12624e023759SAndre Oppermann m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb; 12630963c8e4SLawrence Stewart if (*moff == m->m_len) { 12640963c8e4SLawrence Stewart *moff = 0; 12650963c8e4SLawrence Stewart sb->sb_sndptroff += m->m_len; 12660963c8e4SLawrence Stewart m = ret = m->m_next; 12670963c8e4SLawrence Stewart KASSERT(ret->m_len > 0, 12680963c8e4SLawrence Stewart ("mbuf %p in sockbuf %p chain has no valid data", ret, sb)); 12690963c8e4SLawrence Stewart } 12704e023759SAndre Oppermann 12714e023759SAndre Oppermann /* Advance by len to be as close as possible for the next transmit. */ 12724e023759SAndre Oppermann for (off = off - sb->sb_sndptroff + len - 1; 12736f4745d5SBjoern A. Zeeb off > 0 && m != NULL && off >= m->m_len; 12744e023759SAndre Oppermann m = m->m_next) { 12754e023759SAndre Oppermann sb->sb_sndptroff += m->m_len; 12764e023759SAndre Oppermann off -= m->m_len; 12774e023759SAndre Oppermann } 12786f4745d5SBjoern A. Zeeb if (off > 0 && m == NULL) 12796f4745d5SBjoern A. Zeeb panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret); 12804e023759SAndre Oppermann sb->sb_sndptr = m; 12814e023759SAndre Oppermann 12824e023759SAndre Oppermann return (ret); 12834e023759SAndre Oppermann } 12844e023759SAndre Oppermann 1285a34b7046SRobert Watson /* 12869fd573c3SHans Petter Selasky * Return the first mbuf and the mbuf data offset for the provided 12879fd573c3SHans Petter Selasky * send offset without changing the "sb_sndptroff" field. 12889fd573c3SHans Petter Selasky */ 12899fd573c3SHans Petter Selasky struct mbuf * 12909fd573c3SHans Petter Selasky sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff) 12919fd573c3SHans Petter Selasky { 12929fd573c3SHans Petter Selasky struct mbuf *m; 12939fd573c3SHans Petter Selasky 12949fd573c3SHans Petter Selasky KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 12959fd573c3SHans Petter Selasky 12969fd573c3SHans Petter Selasky /* 12979fd573c3SHans Petter Selasky * If the "off" is below the stored offset, which happens on 12989fd573c3SHans Petter Selasky * retransmits, just use "sb_mb": 12999fd573c3SHans Petter Selasky */ 13009fd573c3SHans Petter Selasky if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) { 13019fd573c3SHans Petter Selasky m = sb->sb_mb; 13029fd573c3SHans Petter Selasky } else { 13039fd573c3SHans Petter Selasky m = sb->sb_sndptr; 13049fd573c3SHans Petter Selasky off -= sb->sb_sndptroff; 13059fd573c3SHans Petter Selasky } 13069fd573c3SHans Petter Selasky while (off > 0 && m != NULL) { 13079fd573c3SHans Petter Selasky if (off < m->m_len) 13089fd573c3SHans Petter Selasky break; 13099fd573c3SHans Petter Selasky off -= m->m_len; 13109fd573c3SHans Petter Selasky m = m->m_next; 13119fd573c3SHans Petter Selasky } 13129fd573c3SHans Petter Selasky *moff = off; 13139fd573c3SHans Petter Selasky return (m); 13149fd573c3SHans Petter Selasky } 13159fd573c3SHans Petter Selasky 13169fd573c3SHans Petter Selasky /* 1317050ac265SRobert Watson * Drop a record off the front of a sockbuf and move the next record to the 1318050ac265SRobert Watson * front. 1319df8bae1dSRodney W. Grimes */ 132026f9a767SRodney W. Grimes void 1321050ac265SRobert Watson sbdroprecord_locked(struct sockbuf *sb) 1322df8bae1dSRodney W. Grimes { 1323050ac265SRobert Watson struct mbuf *m; 1324df8bae1dSRodney W. Grimes 1325a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1326a34b7046SRobert Watson 1327df8bae1dSRodney W. Grimes m = sb->sb_mb; 1328df8bae1dSRodney W. Grimes if (m) { 1329df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 1330df8bae1dSRodney W. Grimes do { 1331df8bae1dSRodney W. Grimes sbfree(sb, m); 1332ecde8f7cSMatthew Dillon m = m_free(m); 1333797f2d22SPoul-Henning Kamp } while (m); 1334df8bae1dSRodney W. Grimes } 1335395bb186SSam Leffler SB_EMPTY_FIXUP(sb); 1336df8bae1dSRodney W. Grimes } 13371e4ad9ceSGarrett Wollman 133882c23ebaSBill Fenner /* 1339050ac265SRobert Watson * Drop a record off the front of a sockbuf and move the next record to the 1340050ac265SRobert Watson * front. 1341a34b7046SRobert Watson */ 1342a34b7046SRobert Watson void 1343050ac265SRobert Watson sbdroprecord(struct sockbuf *sb) 1344a34b7046SRobert Watson { 1345a34b7046SRobert Watson 1346a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1347a34b7046SRobert Watson sbdroprecord_locked(sb); 1348a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1349a34b7046SRobert Watson } 1350a34b7046SRobert Watson 135120d9e5e8SRobert Watson /* 13528c799760SRobert Watson * Create a "control" mbuf containing the specified data with the specified 13538c799760SRobert Watson * type for presentation on a socket buffer. 135420d9e5e8SRobert Watson */ 135520d9e5e8SRobert Watson struct mbuf * 1356d19e16a7SRobert Watson sbcreatecontrol(caddr_t p, int size, int type, int level) 135720d9e5e8SRobert Watson { 1358d19e16a7SRobert Watson struct cmsghdr *cp; 135920d9e5e8SRobert Watson struct mbuf *m; 136020d9e5e8SRobert Watson 136120d9e5e8SRobert Watson if (CMSG_SPACE((u_int)size) > MCLBYTES) 136220d9e5e8SRobert Watson return ((struct mbuf *) NULL); 136320d9e5e8SRobert Watson if (CMSG_SPACE((u_int)size) > MLEN) 1364eb1b1807SGleb Smirnoff m = m_getcl(M_NOWAIT, MT_CONTROL, 0); 136520d9e5e8SRobert Watson else 1366eb1b1807SGleb Smirnoff m = m_get(M_NOWAIT, MT_CONTROL); 136720d9e5e8SRobert Watson if (m == NULL) 136820d9e5e8SRobert Watson return ((struct mbuf *) NULL); 136920d9e5e8SRobert Watson cp = mtod(m, struct cmsghdr *); 137020d9e5e8SRobert Watson m->m_len = 0; 137120d9e5e8SRobert Watson KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 137220d9e5e8SRobert Watson ("sbcreatecontrol: short mbuf")); 13732827952eSXin LI /* 13742827952eSXin LI * Don't leave the padding between the msg header and the 13752827952eSXin LI * cmsg data and the padding after the cmsg data un-initialized. 13762827952eSXin LI */ 13772827952eSXin LI bzero(cp, CMSG_SPACE((u_int)size)); 137820d9e5e8SRobert Watson if (p != NULL) 137920d9e5e8SRobert Watson (void)memcpy(CMSG_DATA(cp), p, size); 138020d9e5e8SRobert Watson m->m_len = CMSG_SPACE(size); 138120d9e5e8SRobert Watson cp->cmsg_len = CMSG_LEN(size); 138220d9e5e8SRobert Watson cp->cmsg_level = level; 138320d9e5e8SRobert Watson cp->cmsg_type = type; 138420d9e5e8SRobert Watson return (m); 138520d9e5e8SRobert Watson } 138620d9e5e8SRobert Watson 138720d9e5e8SRobert Watson /* 13888c799760SRobert Watson * This does the same for socket buffers that sotoxsocket does for sockets: 13898c799760SRobert Watson * generate an user-format data structure describing the socket buffer. Note 13908c799760SRobert Watson * that the xsockbuf structure, since it is always embedded in a socket, does 13918c799760SRobert Watson * not include a self pointer nor a length. We make this entry point public 13928c799760SRobert Watson * in case some other mechanism needs it. 139320d9e5e8SRobert Watson */ 139420d9e5e8SRobert Watson void 139520d9e5e8SRobert Watson sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 139620d9e5e8SRobert Watson { 1397d19e16a7SRobert Watson 13980f9d0a73SGleb Smirnoff xsb->sb_cc = sb->sb_ccc; 139920d9e5e8SRobert Watson xsb->sb_hiwat = sb->sb_hiwat; 140020d9e5e8SRobert Watson xsb->sb_mbcnt = sb->sb_mbcnt; 140149f287f8SGeorge V. Neville-Neil xsb->sb_mcnt = sb->sb_mcnt; 140249f287f8SGeorge V. Neville-Neil xsb->sb_ccnt = sb->sb_ccnt; 140320d9e5e8SRobert Watson xsb->sb_mbmax = sb->sb_mbmax; 140420d9e5e8SRobert Watson xsb->sb_lowat = sb->sb_lowat; 140520d9e5e8SRobert Watson xsb->sb_flags = sb->sb_flags; 140620d9e5e8SRobert Watson xsb->sb_timeo = sb->sb_timeo; 140720d9e5e8SRobert Watson } 140820d9e5e8SRobert Watson 1409639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1410639acc13SGarrett Wollman static int dummy; 1411639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 14121b978d45SHartmut Brandt SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 14131b978d45SHartmut Brandt &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 14141b978d45SHartmut Brandt SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 14153eb9ab52SEitan Adler &sb_efficiency, 0, "Socket buffer size waste factor"); 1416