19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1990, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1369a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 355b86eac4SJesper Skriver #include "opt_param.h" 36335654d7SRobert Watson 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38960ed29cSSeigo Tanimura #include <sys/aio.h> /* for aio_swake proto */ 39ff5c09daSGarrett Wollman #include <sys/kernel.h> 40fb919e4dSMark Murray #include <sys/lock.h> 418ec07310SGleb Smirnoff #include <sys/malloc.h> 42df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 43960ed29cSSeigo Tanimura #include <sys/mutex.h> 44fb919e4dSMark Murray #include <sys/proc.h> 45df8bae1dSRodney W. Grimes #include <sys/protosw.h> 462f9a2132SBrian Feldman #include <sys/resourcevar.h> 47960ed29cSSeigo Tanimura #include <sys/signalvar.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 507abab911SRobert Watson #include <sys/sx.h> 51ff5c09daSGarrett Wollman #include <sys/sysctl.h> 5226f9a767SRodney W. Grimes 53f14cce87SRobert Watson /* 54f14cce87SRobert Watson * Function pointer set by the AIO routines so that the socket buffer code 55f14cce87SRobert Watson * can call back into the AIO module if it is loaded. 56f14cce87SRobert Watson */ 5721d56e9cSAlfred Perlstein void (*aio_swake)(struct socket *, struct sockbuf *); 5821d56e9cSAlfred Perlstein 59df8bae1dSRodney W. Grimes /* 60f14cce87SRobert Watson * Primitive routines for operating on socket buffers 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 6379cb7eb4SDavid Greenman u_long sb_max = SB_MAX; 6458d14daeSMohan Srinivasan u_long sb_max_adj = 65b233773bSBjoern A. Zeeb (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 66df8bae1dSRodney W. Grimes 674b29bc4fSGarrett Wollman static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 684b29bc4fSGarrett Wollman 691d2df300SGleb Smirnoff static struct mbuf *sbcut_internal(struct sockbuf *sb, int len); 70050ac265SRobert Watson static void sbflush_internal(struct sockbuf *sb); 71eaa6dfbcSRobert Watson 72df8bae1dSRodney W. Grimes /* 73829fae90SGleb Smirnoff * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY. 74829fae90SGleb Smirnoff */ 75829fae90SGleb Smirnoff static void 76829fae90SGleb Smirnoff sbm_clrprotoflags(struct mbuf *m, int flags) 77829fae90SGleb Smirnoff { 78829fae90SGleb Smirnoff int mask; 79829fae90SGleb Smirnoff 80829fae90SGleb Smirnoff mask = ~M_PROTOFLAGS; 81829fae90SGleb Smirnoff if (flags & PRUS_NOTREADY) 82829fae90SGleb Smirnoff mask |= M_NOTREADY; 83829fae90SGleb Smirnoff while (m) { 84829fae90SGleb Smirnoff m->m_flags &= mask; 85829fae90SGleb Smirnoff m = m->m_next; 86829fae90SGleb Smirnoff } 87829fae90SGleb Smirnoff } 88829fae90SGleb Smirnoff 89829fae90SGleb Smirnoff /* 900f9d0a73SGleb Smirnoff * Mark ready "count" mbufs starting with "m". 910f9d0a73SGleb Smirnoff */ 920f9d0a73SGleb Smirnoff int 930f9d0a73SGleb Smirnoff sbready(struct sockbuf *sb, struct mbuf *m, int count) 940f9d0a73SGleb Smirnoff { 950f9d0a73SGleb Smirnoff u_int blocker; 960f9d0a73SGleb Smirnoff 970f9d0a73SGleb Smirnoff SOCKBUF_LOCK_ASSERT(sb); 980f9d0a73SGleb Smirnoff KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb)); 990f9d0a73SGleb Smirnoff 1000f9d0a73SGleb Smirnoff blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0; 1010f9d0a73SGleb Smirnoff 1020f9d0a73SGleb Smirnoff for (int i = 0; i < count; i++, m = m->m_next) { 1030f9d0a73SGleb Smirnoff KASSERT(m->m_flags & M_NOTREADY, 1040f9d0a73SGleb Smirnoff ("%s: m %p !M_NOTREADY", __func__, m)); 1050f9d0a73SGleb Smirnoff m->m_flags &= ~(M_NOTREADY | blocker); 1060f9d0a73SGleb Smirnoff if (blocker) 1070f9d0a73SGleb Smirnoff sb->sb_acc += m->m_len; 1080f9d0a73SGleb Smirnoff } 1090f9d0a73SGleb Smirnoff 1100f9d0a73SGleb Smirnoff if (!blocker) 1110f9d0a73SGleb Smirnoff return (EINPROGRESS); 1120f9d0a73SGleb Smirnoff 1130f9d0a73SGleb Smirnoff /* This one was blocking all the queue. */ 1140f9d0a73SGleb Smirnoff for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) { 1150f9d0a73SGleb Smirnoff KASSERT(m->m_flags & M_BLOCKED, 1160f9d0a73SGleb Smirnoff ("%s: m %p !M_BLOCKED", __func__, m)); 1170f9d0a73SGleb Smirnoff m->m_flags &= ~M_BLOCKED; 1180f9d0a73SGleb Smirnoff sb->sb_acc += m->m_len; 1190f9d0a73SGleb Smirnoff } 1200f9d0a73SGleb Smirnoff 1210f9d0a73SGleb Smirnoff sb->sb_fnrdy = m; 1220f9d0a73SGleb Smirnoff 1230f9d0a73SGleb Smirnoff return (0); 1240f9d0a73SGleb Smirnoff } 1250f9d0a73SGleb Smirnoff 1260f9d0a73SGleb Smirnoff /* 1278967b220SGleb Smirnoff * Adjust sockbuf state reflecting allocation of m. 1288967b220SGleb Smirnoff */ 1298967b220SGleb Smirnoff void 1308967b220SGleb Smirnoff sballoc(struct sockbuf *sb, struct mbuf *m) 1318967b220SGleb Smirnoff { 1328967b220SGleb Smirnoff 1338967b220SGleb Smirnoff SOCKBUF_LOCK_ASSERT(sb); 1348967b220SGleb Smirnoff 1350f9d0a73SGleb Smirnoff sb->sb_ccc += m->m_len; 1360f9d0a73SGleb Smirnoff 1370f9d0a73SGleb Smirnoff if (sb->sb_fnrdy == NULL) { 1380f9d0a73SGleb Smirnoff if (m->m_flags & M_NOTREADY) 1390f9d0a73SGleb Smirnoff sb->sb_fnrdy = m; 1400f9d0a73SGleb Smirnoff else 1410f9d0a73SGleb Smirnoff sb->sb_acc += m->m_len; 1420f9d0a73SGleb Smirnoff } else 1430f9d0a73SGleb Smirnoff m->m_flags |= M_BLOCKED; 1448967b220SGleb Smirnoff 1458967b220SGleb Smirnoff if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1468967b220SGleb Smirnoff sb->sb_ctl += m->m_len; 1478967b220SGleb Smirnoff 1488967b220SGleb Smirnoff sb->sb_mbcnt += MSIZE; 1498967b220SGleb Smirnoff sb->sb_mcnt += 1; 1508967b220SGleb Smirnoff 1518967b220SGleb Smirnoff if (m->m_flags & M_EXT) { 1528967b220SGleb Smirnoff sb->sb_mbcnt += m->m_ext.ext_size; 1538967b220SGleb Smirnoff sb->sb_ccnt += 1; 1548967b220SGleb Smirnoff } 1558967b220SGleb Smirnoff } 1568967b220SGleb Smirnoff 1578967b220SGleb Smirnoff /* 1588967b220SGleb Smirnoff * Adjust sockbuf state reflecting freeing of m. 1598967b220SGleb Smirnoff */ 1608967b220SGleb Smirnoff void 1618967b220SGleb Smirnoff sbfree(struct sockbuf *sb, struct mbuf *m) 1628967b220SGleb Smirnoff { 1638967b220SGleb Smirnoff 1648967b220SGleb Smirnoff #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */ 1658967b220SGleb Smirnoff SOCKBUF_LOCK_ASSERT(sb); 1668967b220SGleb Smirnoff #endif 1678967b220SGleb Smirnoff 1680f9d0a73SGleb Smirnoff sb->sb_ccc -= m->m_len; 1690f9d0a73SGleb Smirnoff 1700f9d0a73SGleb Smirnoff if (!(m->m_flags & M_NOTAVAIL)) 1710f9d0a73SGleb Smirnoff sb->sb_acc -= m->m_len; 1720f9d0a73SGleb Smirnoff 1730f9d0a73SGleb Smirnoff if (m == sb->sb_fnrdy) { 1740f9d0a73SGleb Smirnoff struct mbuf *n; 1750f9d0a73SGleb Smirnoff 1760f9d0a73SGleb Smirnoff KASSERT(m->m_flags & M_NOTREADY, 1770f9d0a73SGleb Smirnoff ("%s: m %p !M_NOTREADY", __func__, m)); 1780f9d0a73SGleb Smirnoff 1790f9d0a73SGleb Smirnoff n = m->m_next; 1800f9d0a73SGleb Smirnoff while (n != NULL && !(n->m_flags & M_NOTREADY)) { 1810f9d0a73SGleb Smirnoff n->m_flags &= ~M_BLOCKED; 1820f9d0a73SGleb Smirnoff sb->sb_acc += n->m_len; 1830f9d0a73SGleb Smirnoff n = n->m_next; 1840f9d0a73SGleb Smirnoff } 1850f9d0a73SGleb Smirnoff sb->sb_fnrdy = n; 1860f9d0a73SGleb Smirnoff } 1878967b220SGleb Smirnoff 1888967b220SGleb Smirnoff if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1898967b220SGleb Smirnoff sb->sb_ctl -= m->m_len; 1908967b220SGleb Smirnoff 1918967b220SGleb Smirnoff sb->sb_mbcnt -= MSIZE; 1928967b220SGleb Smirnoff sb->sb_mcnt -= 1; 1938967b220SGleb Smirnoff if (m->m_flags & M_EXT) { 1948967b220SGleb Smirnoff sb->sb_mbcnt -= m->m_ext.ext_size; 1958967b220SGleb Smirnoff sb->sb_ccnt -= 1; 1968967b220SGleb Smirnoff } 1978967b220SGleb Smirnoff 1988967b220SGleb Smirnoff if (sb->sb_sndptr == m) { 1998967b220SGleb Smirnoff sb->sb_sndptr = NULL; 2008967b220SGleb Smirnoff sb->sb_sndptroff = 0; 2018967b220SGleb Smirnoff } 2028967b220SGleb Smirnoff if (sb->sb_sndptroff != 0) 2038967b220SGleb Smirnoff sb->sb_sndptroff -= m->m_len; 2048967b220SGleb Smirnoff } 2058967b220SGleb Smirnoff 2068967b220SGleb Smirnoff /* 207050ac265SRobert Watson * Socantsendmore indicates that no more data will be sent on the socket; it 208050ac265SRobert Watson * would normally be applied to a socket when the user informs the system 209050ac265SRobert Watson * that no more data is to be sent, by the protocol code (in case 210050ac265SRobert Watson * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be 211050ac265SRobert Watson * received, and will normally be applied to the socket by a protocol when it 212050ac265SRobert Watson * detects that the peer will send no more data. Data queued for reading in 213050ac265SRobert Watson * the socket may yet be read. 214df8bae1dSRodney W. Grimes */ 215a34b7046SRobert Watson void 216050ac265SRobert Watson socantsendmore_locked(struct socket *so) 217a34b7046SRobert Watson { 218a34b7046SRobert Watson 219a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 220a34b7046SRobert Watson 221a34b7046SRobert Watson so->so_snd.sb_state |= SBS_CANTSENDMORE; 222a34b7046SRobert Watson sowwakeup_locked(so); 223a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 224a34b7046SRobert Watson } 225df8bae1dSRodney W. Grimes 22626f9a767SRodney W. Grimes void 227050ac265SRobert Watson socantsendmore(struct socket *so) 228df8bae1dSRodney W. Grimes { 229df8bae1dSRodney W. Grimes 230a34b7046SRobert Watson SOCKBUF_LOCK(&so->so_snd); 231a34b7046SRobert Watson socantsendmore_locked(so); 232a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED); 233a34b7046SRobert Watson } 234a34b7046SRobert Watson 235a34b7046SRobert Watson void 236050ac265SRobert Watson socantrcvmore_locked(struct socket *so) 237a34b7046SRobert Watson { 238a34b7046SRobert Watson 239a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_rcv); 240a34b7046SRobert Watson 241a34b7046SRobert Watson so->so_rcv.sb_state |= SBS_CANTRCVMORE; 242a34b7046SRobert Watson sorwakeup_locked(so); 243a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes 24626f9a767SRodney W. Grimes void 247050ac265SRobert Watson socantrcvmore(struct socket *so) 248df8bae1dSRodney W. Grimes { 249df8bae1dSRodney W. Grimes 250a34b7046SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 251a34b7046SRobert Watson socantrcvmore_locked(so); 252a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED); 253df8bae1dSRodney W. Grimes } 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * Wait for data to arrive at/drain from a socket buffer. 257df8bae1dSRodney W. Grimes */ 25826f9a767SRodney W. Grimes int 259050ac265SRobert Watson sbwait(struct sockbuf *sb) 260df8bae1dSRodney W. Grimes { 261df8bae1dSRodney W. Grimes 26231f555a1SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 26331f555a1SRobert Watson 264df8bae1dSRodney W. Grimes sb->sb_flags |= SB_WAIT; 2650f9d0a73SGleb Smirnoff return (msleep_sbt(&sb->sb_acc, &sb->sb_mtx, 26647daf5d5SBruce Evans (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", 2677729cbf1SDavide Italiano sb->sb_timeo, 0, 0)); 268df8bae1dSRodney W. Grimes } 269df8bae1dSRodney W. Grimes 27026f9a767SRodney W. Grimes int 2717abab911SRobert Watson sblock(struct sockbuf *sb, int flags) 272df8bae1dSRodney W. Grimes { 273df8bae1dSRodney W. Grimes 274265de5bbSRobert Watson KASSERT((flags & SBL_VALID) == flags, 275265de5bbSRobert Watson ("sblock: flags invalid (0x%x)", flags)); 276265de5bbSRobert Watson 277265de5bbSRobert Watson if (flags & SBL_WAIT) { 278265de5bbSRobert Watson if ((sb->sb_flags & SB_NOINTR) || 279265de5bbSRobert Watson (flags & SBL_NOINTR)) { 2807abab911SRobert Watson sx_xlock(&sb->sb_sx); 281df8bae1dSRodney W. Grimes return (0); 282049c3b6cSRobert Watson } 283049c3b6cSRobert Watson return (sx_xlock_sig(&sb->sb_sx)); 2847abab911SRobert Watson } else { 2857abab911SRobert Watson if (sx_try_xlock(&sb->sb_sx) == 0) 2867abab911SRobert Watson return (EWOULDBLOCK); 2877abab911SRobert Watson return (0); 2887abab911SRobert Watson } 2897abab911SRobert Watson } 2907abab911SRobert Watson 2917abab911SRobert Watson void 2927abab911SRobert Watson sbunlock(struct sockbuf *sb) 2937abab911SRobert Watson { 2947abab911SRobert Watson 2957abab911SRobert Watson sx_xunlock(&sb->sb_sx); 296df8bae1dSRodney W. Grimes } 297df8bae1dSRodney W. Grimes 298df8bae1dSRodney W. Grimes /* 299050ac265SRobert Watson * Wakeup processes waiting on a socket buffer. Do asynchronous notification 300050ac265SRobert Watson * via SIGIO if the socket has the SS_ASYNC flag set. 301a34b7046SRobert Watson * 302a34b7046SRobert Watson * Called with the socket buffer lock held; will release the lock by the end 303a34b7046SRobert Watson * of the function. This allows the caller to acquire the socket buffer lock 304a34b7046SRobert Watson * while testing for the need for various sorts of wakeup and hold it through 305a34b7046SRobert Watson * to the point where it's no longer required. We currently hold the lock 306a34b7046SRobert Watson * through calls out to other subsystems (with the exception of kqueue), and 307a34b7046SRobert Watson * then release it to avoid lock order issues. It's not clear that's 308a34b7046SRobert Watson * correct. 309df8bae1dSRodney W. Grimes */ 31026f9a767SRodney W. Grimes void 311050ac265SRobert Watson sowakeup(struct socket *so, struct sockbuf *sb) 312df8bae1dSRodney W. Grimes { 31374fb0ba7SJohn Baldwin int ret; 314d48d4b25SSeigo Tanimura 315a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 316a34b7046SRobert Watson 317779f106aSGleb Smirnoff selwakeuppri(sb->sb_sel, PSOCK); 318779f106aSGleb Smirnoff if (!SEL_WAITING(sb->sb_sel)) 319df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_SEL; 320df8bae1dSRodney W. Grimes if (sb->sb_flags & SB_WAIT) { 321df8bae1dSRodney W. Grimes sb->sb_flags &= ~SB_WAIT; 3220f9d0a73SGleb Smirnoff wakeup(&sb->sb_acc); 323df8bae1dSRodney W. Grimes } 324779f106aSGleb Smirnoff KNOTE_LOCKED(&sb->sb_sel->si_note, 0); 3258d40badaSGleb Smirnoff if (sb->sb_upcall != NULL && !(so->so_state & SS_ISDISCONNECTED)) { 326eb1b1807SGleb Smirnoff ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT); 32774fb0ba7SJohn Baldwin if (ret == SU_ISCONNECTED) { 32874fb0ba7SJohn Baldwin KASSERT(sb == &so->so_rcv, 32974fb0ba7SJohn Baldwin ("SO_SND upcall returned SU_ISCONNECTED")); 33074fb0ba7SJohn Baldwin soupcall_clear(so, SO_RCV); 33174fb0ba7SJohn Baldwin } 33274fb0ba7SJohn Baldwin } else 33374fb0ba7SJohn Baldwin ret = SU_OK; 3344cc20ab1SSeigo Tanimura if (sb->sb_flags & SB_AIO) 335f3215338SJohn Baldwin sowakeup_aio(so, sb); 33674fb0ba7SJohn Baldwin SOCKBUF_UNLOCK(sb); 33774fb0ba7SJohn Baldwin if (ret == SU_ISCONNECTED) 33874fb0ba7SJohn Baldwin soisconnected(so); 33974fb0ba7SJohn Baldwin if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 34074fb0ba7SJohn Baldwin pgsigio(&so->so_sigio, SIGIO, 0); 341a34b7046SRobert Watson mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); 342df8bae1dSRodney W. Grimes } 343df8bae1dSRodney W. Grimes 344df8bae1dSRodney W. Grimes /* 345df8bae1dSRodney W. Grimes * Socket buffer (struct sockbuf) utility routines. 346df8bae1dSRodney W. Grimes * 347050ac265SRobert Watson * Each socket contains two socket buffers: one for sending data and one for 348050ac265SRobert Watson * receiving data. Each buffer contains a queue of mbufs, information about 349050ac265SRobert Watson * the number of mbufs and amount of data in the queue, and other fields 350050ac265SRobert Watson * allowing select() statements and notification on data availability to be 351050ac265SRobert Watson * implemented. 352df8bae1dSRodney W. Grimes * 353050ac265SRobert Watson * Data stored in a socket buffer is maintained as a list of records. Each 354050ac265SRobert Watson * record is a list of mbufs chained together with the m_next field. Records 355050ac265SRobert Watson * are chained together with the m_nextpkt field. The upper level routine 356050ac265SRobert Watson * soreceive() expects the following conventions to be observed when placing 357050ac265SRobert Watson * information in the receive buffer: 358df8bae1dSRodney W. Grimes * 359050ac265SRobert Watson * 1. If the protocol requires each message be preceded by the sender's name, 360050ac265SRobert Watson * then a record containing that name must be present before any 361050ac265SRobert Watson * associated data (mbuf's must be of type MT_SONAME). 362050ac265SRobert Watson * 2. If the protocol supports the exchange of ``access rights'' (really just 363050ac265SRobert Watson * additional data associated with the message), and there are ``rights'' 364050ac265SRobert Watson * to be received, then a record containing this data should be present 365050ac265SRobert Watson * (mbuf's must be of type MT_RIGHTS). 366050ac265SRobert Watson * 3. If a name or rights record exists, then it must be followed by a data 367050ac265SRobert Watson * record, perhaps of zero length. 368df8bae1dSRodney W. Grimes * 369df8bae1dSRodney W. Grimes * Before using a new socket structure it is first necessary to reserve 370df8bae1dSRodney W. Grimes * buffer space to the socket, by calling sbreserve(). This should commit 371df8bae1dSRodney W. Grimes * some of the available buffer space in the system buffer pool for the 372050ac265SRobert Watson * socket (currently, it does nothing but enforce limits). The space should 373050ac265SRobert Watson * be released by calling sbrelease() when the socket is destroyed. 374df8bae1dSRodney W. Grimes */ 37526f9a767SRodney W. Grimes int 376050ac265SRobert Watson soreserve(struct socket *so, u_long sndcc, u_long rcvcc) 377df8bae1dSRodney W. Grimes { 378b40ce416SJulian Elischer struct thread *td = curthread; 379df8bae1dSRodney W. Grimes 3803f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3819535efc0SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3823f11a2f3SRobert Watson if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) 3833f11a2f3SRobert Watson goto bad; 3843f11a2f3SRobert Watson if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) 3853f11a2f3SRobert Watson goto bad2; 386df8bae1dSRodney W. Grimes if (so->so_rcv.sb_lowat == 0) 387df8bae1dSRodney W. Grimes so->so_rcv.sb_lowat = 1; 388df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat == 0) 389df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = MCLBYTES; 390df8bae1dSRodney W. Grimes if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 391df8bae1dSRodney W. Grimes so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 3923f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3939535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 394df8bae1dSRodney W. Grimes return (0); 395df8bae1dSRodney W. Grimes bad2: 3963f11a2f3SRobert Watson sbrelease_locked(&so->so_snd, so); 397df8bae1dSRodney W. Grimes bad: 3983f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3993f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 400df8bae1dSRodney W. Grimes return (ENOBUFS); 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes 40379cb7eb4SDavid Greenman static int 40479cb7eb4SDavid Greenman sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 40579cb7eb4SDavid Greenman { 40679cb7eb4SDavid Greenman int error = 0; 40786a93d51SJohn Baldwin u_long tmp_sb_max = sb_max; 40879cb7eb4SDavid Greenman 40986a93d51SJohn Baldwin error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req); 41079cb7eb4SDavid Greenman if (error || !req->newptr) 41179cb7eb4SDavid Greenman return (error); 41286a93d51SJohn Baldwin if (tmp_sb_max < MSIZE + MCLBYTES) 41379cb7eb4SDavid Greenman return (EINVAL); 41486a93d51SJohn Baldwin sb_max = tmp_sb_max; 41579cb7eb4SDavid Greenman sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 41679cb7eb4SDavid Greenman return (0); 41779cb7eb4SDavid Greenman } 41879cb7eb4SDavid Greenman 419df8bae1dSRodney W. Grimes /* 420050ac265SRobert Watson * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't 421050ac265SRobert Watson * become limiting if buffering efficiency is near the normal case. 422df8bae1dSRodney W. Grimes */ 42326f9a767SRodney W. Grimes int 424050ac265SRobert Watson sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so, 425050ac265SRobert Watson struct thread *td) 426df8bae1dSRodney W. Grimes { 42791d5354aSJohn Baldwin rlim_t sbsize_limit; 428ecf72308SBrian Feldman 4293f11a2f3SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 4303f11a2f3SRobert Watson 431ecf72308SBrian Feldman /* 4327978014dSRobert Watson * When a thread is passed, we take into account the thread's socket 4337978014dSRobert Watson * buffer size limit. The caller will generally pass curthread, but 4347978014dSRobert Watson * in the TCP input path, NULL will be passed to indicate that no 4357978014dSRobert Watson * appropriate thread resource limits are available. In that case, 4367978014dSRobert Watson * we don't apply a process limit. 437ecf72308SBrian Feldman */ 43879cb7eb4SDavid Greenman if (cc > sb_max_adj) 439df8bae1dSRodney W. Grimes return (0); 44091d5354aSJohn Baldwin if (td != NULL) { 441f6f6d240SMateusz Guzik sbsize_limit = lim_cur(td, RLIMIT_SBSIZE); 44291d5354aSJohn Baldwin } else 44391d5354aSJohn Baldwin sbsize_limit = RLIM_INFINITY; 444f535380cSDon Lewis if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc, 44591d5354aSJohn Baldwin sbsize_limit)) 446ecf72308SBrian Feldman return (0); 4474b29bc4fSGarrett Wollman sb->sb_mbmax = min(cc * sb_efficiency, sb_max); 448df8bae1dSRodney W. Grimes if (sb->sb_lowat > sb->sb_hiwat) 449df8bae1dSRodney W. Grimes sb->sb_lowat = sb->sb_hiwat; 450df8bae1dSRodney W. Grimes return (1); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 4533f11a2f3SRobert Watson int 454*64290befSGleb Smirnoff sbsetopt(struct socket *so, int cmd, u_long cc) 4553f11a2f3SRobert Watson { 456*64290befSGleb Smirnoff struct sockbuf *sb; 457*64290befSGleb Smirnoff short *flags; 458*64290befSGleb Smirnoff u_int *hiwat, *lowat; 4593f11a2f3SRobert Watson int error; 4603f11a2f3SRobert Watson 461*64290befSGleb Smirnoff SOCK_LOCK(so); 462*64290befSGleb Smirnoff if (SOLISTENING(so)) { 463*64290befSGleb Smirnoff switch (cmd) { 464*64290befSGleb Smirnoff case SO_SNDLOWAT: 465*64290befSGleb Smirnoff case SO_SNDBUF: 466*64290befSGleb Smirnoff lowat = &so->sol_sbsnd_lowat; 467*64290befSGleb Smirnoff hiwat = &so->sol_sbsnd_hiwat; 468*64290befSGleb Smirnoff flags = &so->sol_sbsnd_flags; 469*64290befSGleb Smirnoff break; 470*64290befSGleb Smirnoff case SO_RCVLOWAT: 471*64290befSGleb Smirnoff case SO_RCVBUF: 472*64290befSGleb Smirnoff lowat = &so->sol_sbrcv_lowat; 473*64290befSGleb Smirnoff hiwat = &so->sol_sbrcv_hiwat; 474*64290befSGleb Smirnoff flags = &so->sol_sbrcv_flags; 475*64290befSGleb Smirnoff break; 476*64290befSGleb Smirnoff } 477*64290befSGleb Smirnoff } else { 478*64290befSGleb Smirnoff switch (cmd) { 479*64290befSGleb Smirnoff case SO_SNDLOWAT: 480*64290befSGleb Smirnoff case SO_SNDBUF: 481*64290befSGleb Smirnoff sb = &so->so_snd; 482*64290befSGleb Smirnoff break; 483*64290befSGleb Smirnoff case SO_RCVLOWAT: 484*64290befSGleb Smirnoff case SO_RCVBUF: 485*64290befSGleb Smirnoff sb = &so->so_rcv; 486*64290befSGleb Smirnoff break; 487*64290befSGleb Smirnoff } 488*64290befSGleb Smirnoff flags = &sb->sb_flags; 489*64290befSGleb Smirnoff hiwat = &sb->sb_hiwat; 490*64290befSGleb Smirnoff lowat = &sb->sb_lowat; 4913f11a2f3SRobert Watson SOCKBUF_LOCK(sb); 492*64290befSGleb Smirnoff } 493*64290befSGleb Smirnoff 494*64290befSGleb Smirnoff error = 0; 495*64290befSGleb Smirnoff switch (cmd) { 496*64290befSGleb Smirnoff case SO_SNDBUF: 497*64290befSGleb Smirnoff case SO_RCVBUF: 498*64290befSGleb Smirnoff if (SOLISTENING(so)) { 499*64290befSGleb Smirnoff if (cc > sb_max_adj) { 500*64290befSGleb Smirnoff error = ENOBUFS; 501*64290befSGleb Smirnoff break; 502*64290befSGleb Smirnoff } 503*64290befSGleb Smirnoff *hiwat = cc; 504*64290befSGleb Smirnoff if (*lowat > *hiwat) 505*64290befSGleb Smirnoff *lowat = *hiwat; 506*64290befSGleb Smirnoff } else { 507*64290befSGleb Smirnoff if (!sbreserve_locked(sb, cc, so, curthread)) 508*64290befSGleb Smirnoff error = ENOBUFS; 509*64290befSGleb Smirnoff } 510*64290befSGleb Smirnoff if (error == 0) 511*64290befSGleb Smirnoff *flags &= ~SB_AUTOSIZE; 512*64290befSGleb Smirnoff break; 513*64290befSGleb Smirnoff case SO_SNDLOWAT: 514*64290befSGleb Smirnoff case SO_RCVLOWAT: 515*64290befSGleb Smirnoff /* 516*64290befSGleb Smirnoff * Make sure the low-water is never greater than the 517*64290befSGleb Smirnoff * high-water. 518*64290befSGleb Smirnoff */ 519*64290befSGleb Smirnoff *lowat = (cc > *hiwat) ? *hiwat : cc; 520*64290befSGleb Smirnoff break; 521*64290befSGleb Smirnoff } 522*64290befSGleb Smirnoff 523*64290befSGleb Smirnoff if (!SOLISTENING(so)) 5243f11a2f3SRobert Watson SOCKBUF_UNLOCK(sb); 525*64290befSGleb Smirnoff SOCK_UNLOCK(so); 5263f11a2f3SRobert Watson return (error); 5273f11a2f3SRobert Watson } 5283f11a2f3SRobert Watson 529df8bae1dSRodney W. Grimes /* 530df8bae1dSRodney W. Grimes * Free mbufs held by a socket, and reserved mbuf space. 531df8bae1dSRodney W. Grimes */ 5323f0bfcccSRobert Watson void 533050ac265SRobert Watson sbrelease_internal(struct sockbuf *sb, struct socket *so) 534eaa6dfbcSRobert Watson { 535eaa6dfbcSRobert Watson 536eaa6dfbcSRobert Watson sbflush_internal(sb); 537eaa6dfbcSRobert Watson (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0, 538eaa6dfbcSRobert Watson RLIM_INFINITY); 539eaa6dfbcSRobert Watson sb->sb_mbmax = 0; 540eaa6dfbcSRobert Watson } 541eaa6dfbcSRobert Watson 54226f9a767SRodney W. Grimes void 543050ac265SRobert Watson sbrelease_locked(struct sockbuf *sb, struct socket *so) 544df8bae1dSRodney W. Grimes { 545df8bae1dSRodney W. Grimes 546a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 547a34b7046SRobert Watson 548eaa6dfbcSRobert Watson sbrelease_internal(sb, so); 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes 551a34b7046SRobert Watson void 552050ac265SRobert Watson sbrelease(struct sockbuf *sb, struct socket *so) 553a34b7046SRobert Watson { 554a34b7046SRobert Watson 555a34b7046SRobert Watson SOCKBUF_LOCK(sb); 556a34b7046SRobert Watson sbrelease_locked(sb, so); 557a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 558a34b7046SRobert Watson } 559eaa6dfbcSRobert Watson 560eaa6dfbcSRobert Watson void 561050ac265SRobert Watson sbdestroy(struct sockbuf *sb, struct socket *so) 562eaa6dfbcSRobert Watson { 563eaa6dfbcSRobert Watson 564eaa6dfbcSRobert Watson sbrelease_internal(sb, so); 565eaa6dfbcSRobert Watson } 566eaa6dfbcSRobert Watson 567df8bae1dSRodney W. Grimes /* 568050ac265SRobert Watson * Routines to add and remove data from an mbuf queue. 569df8bae1dSRodney W. Grimes * 570050ac265SRobert Watson * The routines sbappend() or sbappendrecord() are normally called to append 571050ac265SRobert Watson * new mbufs to a socket buffer, after checking that adequate space is 572050ac265SRobert Watson * available, comparing the function sbspace() with the amount of data to be 573050ac265SRobert Watson * added. sbappendrecord() differs from sbappend() in that data supplied is 574050ac265SRobert Watson * treated as the beginning of a new record. To place a sender's address, 575050ac265SRobert Watson * optional access rights, and data in a socket receive buffer, 576050ac265SRobert Watson * sbappendaddr() should be used. To place access rights and data in a 577050ac265SRobert Watson * socket receive buffer, sbappendrights() should be used. In either case, 578050ac265SRobert Watson * the new data begins a new record. Note that unlike sbappend() and 579050ac265SRobert Watson * sbappendrecord(), these routines check for the caller that there will be 580050ac265SRobert Watson * enough space to store the data. Each fails if there is not enough space, 581050ac265SRobert Watson * or if it cannot find mbufs to store additional information in. 582df8bae1dSRodney W. Grimes * 583050ac265SRobert Watson * Reliable protocols may use the socket send buffer to hold data awaiting 584050ac265SRobert Watson * acknowledgement. Data is normally copied from a socket send buffer in a 585050ac265SRobert Watson * protocol with m_copy for output to a peer, and then removing the data from 586050ac265SRobert Watson * the socket buffer with sbdrop() or sbdroprecord() when the data is 587050ac265SRobert Watson * acknowledged by the peer. 588df8bae1dSRodney W. Grimes */ 589395bb186SSam Leffler #ifdef SOCKBUF_DEBUG 590395bb186SSam Leffler void 591395bb186SSam Leffler sblastrecordchk(struct sockbuf *sb, const char *file, int line) 592395bb186SSam Leffler { 593395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 594395bb186SSam Leffler 595a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 596a34b7046SRobert Watson 597395bb186SSam Leffler while (m && m->m_nextpkt) 598395bb186SSam Leffler m = m->m_nextpkt; 599395bb186SSam Leffler 600395bb186SSam Leffler if (m != sb->sb_lastrecord) { 601395bb186SSam Leffler printf("%s: sb_mb %p sb_lastrecord %p last %p\n", 602395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_lastrecord, m); 603395bb186SSam Leffler printf("packet chain:\n"); 604395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 605395bb186SSam Leffler printf("\t%p\n", m); 606395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 607395bb186SSam Leffler } 608395bb186SSam Leffler } 609395bb186SSam Leffler 610395bb186SSam Leffler void 611395bb186SSam Leffler sblastmbufchk(struct sockbuf *sb, const char *file, int line) 612395bb186SSam Leffler { 613395bb186SSam Leffler struct mbuf *m = sb->sb_mb; 614395bb186SSam Leffler struct mbuf *n; 615395bb186SSam Leffler 616a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 617a34b7046SRobert Watson 618395bb186SSam Leffler while (m && m->m_nextpkt) 619395bb186SSam Leffler m = m->m_nextpkt; 620395bb186SSam Leffler 621395bb186SSam Leffler while (m && m->m_next) 622395bb186SSam Leffler m = m->m_next; 623395bb186SSam Leffler 624395bb186SSam Leffler if (m != sb->sb_mbtail) { 625395bb186SSam Leffler printf("%s: sb_mb %p sb_mbtail %p last %p\n", 626395bb186SSam Leffler __func__, sb->sb_mb, sb->sb_mbtail, m); 627395bb186SSam Leffler printf("packet tree:\n"); 628395bb186SSam Leffler for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 629395bb186SSam Leffler printf("\t"); 630395bb186SSam Leffler for (n = m; n != NULL; n = n->m_next) 631395bb186SSam Leffler printf("%p ", n); 632395bb186SSam Leffler printf("\n"); 633395bb186SSam Leffler } 634395bb186SSam Leffler panic("%s from %s:%u", __func__, file, line); 635395bb186SSam Leffler } 636395bb186SSam Leffler } 637395bb186SSam Leffler #endif /* SOCKBUF_DEBUG */ 638395bb186SSam Leffler 639395bb186SSam Leffler #define SBLINKRECORD(sb, m0) do { \ 640a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); \ 641395bb186SSam Leffler if ((sb)->sb_lastrecord != NULL) \ 642395bb186SSam Leffler (sb)->sb_lastrecord->m_nextpkt = (m0); \ 643395bb186SSam Leffler else \ 644395bb186SSam Leffler (sb)->sb_mb = (m0); \ 645395bb186SSam Leffler (sb)->sb_lastrecord = (m0); \ 646395bb186SSam Leffler } while (/*CONSTCOND*/0) 647395bb186SSam Leffler 648df8bae1dSRodney W. Grimes /* 649050ac265SRobert Watson * Append mbuf chain m to the last record in the socket buffer sb. The 650050ac265SRobert Watson * additional space associated the mbuf chain is recorded in sb. Empty mbufs 651050ac265SRobert Watson * are discarded and mbufs are compacted where possible. 652df8bae1dSRodney W. Grimes */ 65326f9a767SRodney W. Grimes void 654829fae90SGleb Smirnoff sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags) 655df8bae1dSRodney W. Grimes { 656050ac265SRobert Watson struct mbuf *n; 657df8bae1dSRodney W. Grimes 658a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 659a34b7046SRobert Watson 660b85f65afSPedro F. Giffuni if (m == NULL) 661df8bae1dSRodney W. Grimes return; 662829fae90SGleb Smirnoff sbm_clrprotoflags(m, flags); 663395bb186SSam Leffler SBLASTRECORDCHK(sb); 664797f2d22SPoul-Henning Kamp n = sb->sb_mb; 665797f2d22SPoul-Henning Kamp if (n) { 666df8bae1dSRodney W. Grimes while (n->m_nextpkt) 667df8bae1dSRodney W. Grimes n = n->m_nextpkt; 668df8bae1dSRodney W. Grimes do { 669df8bae1dSRodney W. Grimes if (n->m_flags & M_EOR) { 670a34b7046SRobert Watson sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 671df8bae1dSRodney W. Grimes return; 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes } while (n->m_next && (n = n->m_next)); 674395bb186SSam Leffler } else { 675395bb186SSam Leffler /* 676395bb186SSam Leffler * XXX Would like to simply use sb_mbtail here, but 677395bb186SSam Leffler * XXX I need to verify that I won't miss an EOR that 678395bb186SSam Leffler * XXX way. 679395bb186SSam Leffler */ 680395bb186SSam Leffler if ((n = sb->sb_lastrecord) != NULL) { 681395bb186SSam Leffler do { 682395bb186SSam Leffler if (n->m_flags & M_EOR) { 683a34b7046SRobert Watson sbappendrecord_locked(sb, m); /* XXXXXX!!!! */ 684395bb186SSam Leffler return; 685395bb186SSam Leffler } 686395bb186SSam Leffler } while (n->m_next && (n = n->m_next)); 687395bb186SSam Leffler } else { 688395bb186SSam Leffler /* 689395bb186SSam Leffler * If this is the first record in the socket buffer, 690395bb186SSam Leffler * it's also the last record. 691395bb186SSam Leffler */ 692395bb186SSam Leffler sb->sb_lastrecord = m; 693395bb186SSam Leffler } 694df8bae1dSRodney W. Grimes } 695df8bae1dSRodney W. Grimes sbcompress(sb, m, n); 696395bb186SSam Leffler SBLASTRECORDCHK(sb); 697395bb186SSam Leffler } 698395bb186SSam Leffler 699395bb186SSam Leffler /* 700050ac265SRobert Watson * Append mbuf chain m to the last record in the socket buffer sb. The 701050ac265SRobert Watson * additional space associated the mbuf chain is recorded in sb. Empty mbufs 702050ac265SRobert Watson * are discarded and mbufs are compacted where possible. 703a34b7046SRobert Watson */ 704a34b7046SRobert Watson void 705829fae90SGleb Smirnoff sbappend(struct sockbuf *sb, struct mbuf *m, int flags) 706a34b7046SRobert Watson { 707a34b7046SRobert Watson 708a34b7046SRobert Watson SOCKBUF_LOCK(sb); 709829fae90SGleb Smirnoff sbappend_locked(sb, m, flags); 710a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 711a34b7046SRobert Watson } 712a34b7046SRobert Watson 713a34b7046SRobert Watson /* 714050ac265SRobert Watson * This version of sbappend() should only be used when the caller absolutely 715050ac265SRobert Watson * knows that there will never be more than one record in the socket buffer, 716050ac265SRobert Watson * that is, a stream protocol (such as TCP). 717395bb186SSam Leffler */ 718395bb186SSam Leffler void 719651e4e6aSGleb Smirnoff sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags) 720395bb186SSam Leffler { 721a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 722395bb186SSam Leffler 723395bb186SSam Leffler KASSERT(m->m_nextpkt == NULL,("sbappendstream 0")); 724395bb186SSam Leffler KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1")); 725395bb186SSam Leffler 726395bb186SSam Leffler SBLASTMBUFCHK(sb); 727395bb186SSam Leffler 728844cacd1SGleb Smirnoff /* Remove all packet headers and mbuf tags to get a pure data chain. */ 729651e4e6aSGleb Smirnoff m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0); 730844cacd1SGleb Smirnoff 731395bb186SSam Leffler sbcompress(sb, m, sb->sb_mbtail); 732395bb186SSam Leffler 733395bb186SSam Leffler sb->sb_lastrecord = sb->sb_mb; 734395bb186SSam Leffler SBLASTRECORDCHK(sb); 735df8bae1dSRodney W. Grimes } 736df8bae1dSRodney W. Grimes 737a34b7046SRobert Watson /* 738050ac265SRobert Watson * This version of sbappend() should only be used when the caller absolutely 739050ac265SRobert Watson * knows that there will never be more than one record in the socket buffer, 740050ac265SRobert Watson * that is, a stream protocol (such as TCP). 741a34b7046SRobert Watson */ 742a34b7046SRobert Watson void 743651e4e6aSGleb Smirnoff sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags) 744a34b7046SRobert Watson { 745a34b7046SRobert Watson 746a34b7046SRobert Watson SOCKBUF_LOCK(sb); 747651e4e6aSGleb Smirnoff sbappendstream_locked(sb, m, flags); 748a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 749a34b7046SRobert Watson } 750a34b7046SRobert Watson 751df8bae1dSRodney W. Grimes #ifdef SOCKBUF_DEBUG 75226f9a767SRodney W. Grimes void 75357f43a45SGleb Smirnoff sbcheck(struct sockbuf *sb, const char *file, int line) 754df8bae1dSRodney W. Grimes { 7550f9d0a73SGleb Smirnoff struct mbuf *m, *n, *fnrdy; 7560f9d0a73SGleb Smirnoff u_long acc, ccc, mbcnt; 757df8bae1dSRodney W. Grimes 758a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 759a34b7046SRobert Watson 7600f9d0a73SGleb Smirnoff acc = ccc = mbcnt = 0; 7610f9d0a73SGleb Smirnoff fnrdy = NULL; 76257f43a45SGleb Smirnoff 7630931333fSBill Fenner for (m = sb->sb_mb; m; m = n) { 7640931333fSBill Fenner n = m->m_nextpkt; 7650931333fSBill Fenner for (; m; m = m->m_next) { 76657f43a45SGleb Smirnoff if (m->m_len == 0) { 76757f43a45SGleb Smirnoff printf("sb %p empty mbuf %p\n", sb, m); 76857f43a45SGleb Smirnoff goto fail; 76957f43a45SGleb Smirnoff } 7700f9d0a73SGleb Smirnoff if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) { 7710f9d0a73SGleb Smirnoff if (m != sb->sb_fnrdy) { 7720f9d0a73SGleb Smirnoff printf("sb %p: fnrdy %p != m %p\n", 7730f9d0a73SGleb Smirnoff sb, sb->sb_fnrdy, m); 7740f9d0a73SGleb Smirnoff goto fail; 7750f9d0a73SGleb Smirnoff } 7760f9d0a73SGleb Smirnoff fnrdy = m; 7770f9d0a73SGleb Smirnoff } 7780f9d0a73SGleb Smirnoff if (fnrdy) { 7790f9d0a73SGleb Smirnoff if (!(m->m_flags & M_NOTAVAIL)) { 7800f9d0a73SGleb Smirnoff printf("sb %p: fnrdy %p, m %p is avail\n", 7810f9d0a73SGleb Smirnoff sb, sb->sb_fnrdy, m); 7820f9d0a73SGleb Smirnoff goto fail; 7830f9d0a73SGleb Smirnoff } 7840f9d0a73SGleb Smirnoff } else 7850f9d0a73SGleb Smirnoff acc += m->m_len; 7860f9d0a73SGleb Smirnoff ccc += m->m_len; 787df8bae1dSRodney W. Grimes mbcnt += MSIZE; 788313861b8SJulian Elischer if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ 789df8bae1dSRodney W. Grimes mbcnt += m->m_ext.ext_size; 7900931333fSBill Fenner } 791df8bae1dSRodney W. Grimes } 7920f9d0a73SGleb Smirnoff if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) { 7930f9d0a73SGleb Smirnoff printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n", 7940f9d0a73SGleb Smirnoff acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt); 79557f43a45SGleb Smirnoff goto fail; 796df8bae1dSRodney W. Grimes } 79757f43a45SGleb Smirnoff return; 79857f43a45SGleb Smirnoff fail: 79957f43a45SGleb Smirnoff panic("%s from %s:%u", __func__, file, line); 800df8bae1dSRodney W. Grimes } 801df8bae1dSRodney W. Grimes #endif 802df8bae1dSRodney W. Grimes 803df8bae1dSRodney W. Grimes /* 804050ac265SRobert Watson * As above, except the mbuf chain begins a new record. 805df8bae1dSRodney W. Grimes */ 80626f9a767SRodney W. Grimes void 807050ac265SRobert Watson sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0) 808df8bae1dSRodney W. Grimes { 809050ac265SRobert Watson struct mbuf *m; 810df8bae1dSRodney W. Grimes 811a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 812a34b7046SRobert Watson 813b85f65afSPedro F. Giffuni if (m0 == NULL) 814df8bae1dSRodney W. Grimes return; 81553b680caSGleb Smirnoff m_clrprotoflags(m0); 816df8bae1dSRodney W. Grimes /* 817050ac265SRobert Watson * Put the first mbuf on the queue. Note this permits zero length 818050ac265SRobert Watson * records. 819df8bae1dSRodney W. Grimes */ 820df8bae1dSRodney W. Grimes sballoc(sb, m0); 821395bb186SSam Leffler SBLASTRECORDCHK(sb); 822395bb186SSam Leffler SBLINKRECORD(sb, m0); 823e72a94adSMaksim Yevmenkin sb->sb_mbtail = m0; 824df8bae1dSRodney W. Grimes m = m0->m_next; 825df8bae1dSRodney W. Grimes m0->m_next = 0; 826df8bae1dSRodney W. Grimes if (m && (m0->m_flags & M_EOR)) { 827df8bae1dSRodney W. Grimes m0->m_flags &= ~M_EOR; 828df8bae1dSRodney W. Grimes m->m_flags |= M_EOR; 829df8bae1dSRodney W. Grimes } 830e72a94adSMaksim Yevmenkin /* always call sbcompress() so it can do SBLASTMBUFCHK() */ 831df8bae1dSRodney W. Grimes sbcompress(sb, m, m0); 832df8bae1dSRodney W. Grimes } 833df8bae1dSRodney W. Grimes 834df8bae1dSRodney W. Grimes /* 835050ac265SRobert Watson * As above, except the mbuf chain begins a new record. 836a34b7046SRobert Watson */ 837a34b7046SRobert Watson void 838050ac265SRobert Watson sbappendrecord(struct sockbuf *sb, struct mbuf *m0) 839a34b7046SRobert Watson { 840a34b7046SRobert Watson 841a34b7046SRobert Watson SOCKBUF_LOCK(sb); 842a34b7046SRobert Watson sbappendrecord_locked(sb, m0); 843a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 844a34b7046SRobert Watson } 845a34b7046SRobert Watson 8468de34a88SAlan Somers /* Helper routine that appends data, control, and address to a sockbuf. */ 8478de34a88SAlan Somers static int 8488de34a88SAlan Somers sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa, 8498de34a88SAlan Somers struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last) 850df8bae1dSRodney W. Grimes { 851395bb186SSam Leffler struct mbuf *m, *n, *nlast; 852c43cad1aSScott Long #if MSIZE <= 256 853df8bae1dSRodney W. Grimes if (asa->sa_len > MLEN) 854df8bae1dSRodney W. Grimes return (0); 855c43cad1aSScott Long #endif 856c8b59ea7SGleb Smirnoff m = m_get(M_NOWAIT, MT_SONAME); 857c8b59ea7SGleb Smirnoff if (m == NULL) 858df8bae1dSRodney W. Grimes return (0); 859df8bae1dSRodney W. Grimes m->m_len = asa->sa_len; 86080208239SAlfred Perlstein bcopy(asa, mtod(m, caddr_t), asa->sa_len); 861c33a2313SAndrey V. Elsukov if (m0) { 86253b680caSGleb Smirnoff m_clrprotoflags(m0); 86357386f5dSAndrey V. Elsukov m_tag_delete_chain(m0, NULL); 864c33a2313SAndrey V. Elsukov /* 865c33a2313SAndrey V. Elsukov * Clear some persistent info from pkthdr. 866c33a2313SAndrey V. Elsukov * We don't use m_demote(), because some netgraph consumers 867c33a2313SAndrey V. Elsukov * expect M_PKTHDR presence. 868c33a2313SAndrey V. Elsukov */ 869c33a2313SAndrey V. Elsukov m0->m_pkthdr.rcvif = NULL; 870c33a2313SAndrey V. Elsukov m0->m_pkthdr.flowid = 0; 871c33a2313SAndrey V. Elsukov m0->m_pkthdr.csum_flags = 0; 872c33a2313SAndrey V. Elsukov m0->m_pkthdr.fibnum = 0; 873c33a2313SAndrey V. Elsukov m0->m_pkthdr.rsstype = 0; 874c33a2313SAndrey V. Elsukov } 8758de34a88SAlan Somers if (ctrl_last) 8768de34a88SAlan Somers ctrl_last->m_next = m0; /* concatenate data to control */ 877df8bae1dSRodney W. Grimes else 878df8bae1dSRodney W. Grimes control = m0; 879df8bae1dSRodney W. Grimes m->m_next = control; 880395bb186SSam Leffler for (n = m; n->m_next != NULL; n = n->m_next) 881df8bae1dSRodney W. Grimes sballoc(sb, n); 882395bb186SSam Leffler sballoc(sb, n); 883395bb186SSam Leffler nlast = n; 884395bb186SSam Leffler SBLINKRECORD(sb, m); 885395bb186SSam Leffler 886395bb186SSam Leffler sb->sb_mbtail = nlast; 887395bb186SSam Leffler SBLASTMBUFCHK(sb); 888395bb186SSam Leffler 889395bb186SSam Leffler SBLASTRECORDCHK(sb); 890df8bae1dSRodney W. Grimes return (1); 891df8bae1dSRodney W. Grimes } 892df8bae1dSRodney W. Grimes 893a34b7046SRobert Watson /* 894050ac265SRobert Watson * Append address and data, and optionally, control (ancillary) data to the 895050ac265SRobert Watson * receive queue of a socket. If present, m0 must include a packet header 896050ac265SRobert Watson * with total length. Returns 0 if no space in sockbuf or insufficient 897050ac265SRobert Watson * mbufs. 898a34b7046SRobert Watson */ 89926f9a767SRodney W. Grimes int 9008de34a88SAlan Somers sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa, 9018de34a88SAlan Somers struct mbuf *m0, struct mbuf *control) 9028de34a88SAlan Somers { 9038de34a88SAlan Somers struct mbuf *ctrl_last; 9048de34a88SAlan Somers int space = asa->sa_len; 9058de34a88SAlan Somers 9068de34a88SAlan Somers SOCKBUF_LOCK_ASSERT(sb); 9078de34a88SAlan Somers 9088de34a88SAlan Somers if (m0 && (m0->m_flags & M_PKTHDR) == 0) 9098de34a88SAlan Somers panic("sbappendaddr_locked"); 9108de34a88SAlan Somers if (m0) 9118de34a88SAlan Somers space += m0->m_pkthdr.len; 9128de34a88SAlan Somers space += m_length(control, &ctrl_last); 9138de34a88SAlan Somers 9148de34a88SAlan Somers if (space > sbspace(sb)) 9158de34a88SAlan Somers return (0); 9168de34a88SAlan Somers return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 9178de34a88SAlan Somers } 9188de34a88SAlan Somers 9198de34a88SAlan Somers /* 9208de34a88SAlan Somers * Append address and data, and optionally, control (ancillary) data to the 9218de34a88SAlan Somers * receive queue of a socket. If present, m0 must include a packet header 9228de34a88SAlan Somers * with total length. Returns 0 if insufficient mbufs. Does not validate space 9238de34a88SAlan Somers * on the receiving sockbuf. 9248de34a88SAlan Somers */ 9258de34a88SAlan Somers int 9268de34a88SAlan Somers sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa, 9278de34a88SAlan Somers struct mbuf *m0, struct mbuf *control) 9288de34a88SAlan Somers { 9298de34a88SAlan Somers struct mbuf *ctrl_last; 9308de34a88SAlan Somers 9318de34a88SAlan Somers SOCKBUF_LOCK_ASSERT(sb); 9328de34a88SAlan Somers 9338de34a88SAlan Somers ctrl_last = (control == NULL) ? NULL : m_last(control); 9348de34a88SAlan Somers return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last)); 9358de34a88SAlan Somers } 9368de34a88SAlan Somers 9378de34a88SAlan Somers /* 9388de34a88SAlan Somers * Append address and data, and optionally, control (ancillary) data to the 9398de34a88SAlan Somers * receive queue of a socket. If present, m0 must include a packet header 9408de34a88SAlan Somers * with total length. Returns 0 if no space in sockbuf or insufficient 9418de34a88SAlan Somers * mbufs. 9428de34a88SAlan Somers */ 9438de34a88SAlan Somers int 944050ac265SRobert Watson sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 945050ac265SRobert Watson struct mbuf *m0, struct mbuf *control) 946a34b7046SRobert Watson { 947a34b7046SRobert Watson int retval; 948a34b7046SRobert Watson 949a34b7046SRobert Watson SOCKBUF_LOCK(sb); 950a34b7046SRobert Watson retval = sbappendaddr_locked(sb, asa, m0, control); 951a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 952a34b7046SRobert Watson return (retval); 953a34b7046SRobert Watson } 954a34b7046SRobert Watson 955a34b7046SRobert Watson int 956050ac265SRobert Watson sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0, 957050ac265SRobert Watson struct mbuf *control) 958df8bae1dSRodney W. Grimes { 959395bb186SSam Leffler struct mbuf *m, *n, *mlast; 9607ed60de8SPoul-Henning Kamp int space; 961df8bae1dSRodney W. Grimes 962a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 963a34b7046SRobert Watson 964b85f65afSPedro F. Giffuni if (control == NULL) 965a34b7046SRobert Watson panic("sbappendcontrol_locked"); 9667ed60de8SPoul-Henning Kamp space = m_length(control, &n) + m_length(m0, NULL); 967a34b7046SRobert Watson 968df8bae1dSRodney W. Grimes if (space > sbspace(sb)) 969df8bae1dSRodney W. Grimes return (0); 97053b680caSGleb Smirnoff m_clrprotoflags(m0); 971df8bae1dSRodney W. Grimes n->m_next = m0; /* concatenate data to control */ 972395bb186SSam Leffler 973395bb186SSam Leffler SBLASTRECORDCHK(sb); 974395bb186SSam Leffler 975395bb186SSam Leffler for (m = control; m->m_next; m = m->m_next) 976df8bae1dSRodney W. Grimes sballoc(sb, m); 977395bb186SSam Leffler sballoc(sb, m); 978395bb186SSam Leffler mlast = m; 979395bb186SSam Leffler SBLINKRECORD(sb, control); 980395bb186SSam Leffler 981395bb186SSam Leffler sb->sb_mbtail = mlast; 982395bb186SSam Leffler SBLASTMBUFCHK(sb); 983395bb186SSam Leffler 984395bb186SSam Leffler SBLASTRECORDCHK(sb); 985df8bae1dSRodney W. Grimes return (1); 986df8bae1dSRodney W. Grimes } 987df8bae1dSRodney W. Grimes 988a34b7046SRobert Watson int 989050ac265SRobert Watson sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control) 990a34b7046SRobert Watson { 991a34b7046SRobert Watson int retval; 992a34b7046SRobert Watson 993a34b7046SRobert Watson SOCKBUF_LOCK(sb); 994a34b7046SRobert Watson retval = sbappendcontrol_locked(sb, m0, control); 995a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 996a34b7046SRobert Watson return (retval); 997a34b7046SRobert Watson } 998a34b7046SRobert Watson 999df8bae1dSRodney W. Grimes /* 10007da7362bSRobert Watson * Append the data in mbuf chain (m) into the socket buffer sb following mbuf 10017da7362bSRobert Watson * (n). If (n) is NULL, the buffer is presumed empty. 10027da7362bSRobert Watson * 10037da7362bSRobert Watson * When the data is compressed, mbufs in the chain may be handled in one of 10047da7362bSRobert Watson * three ways: 10057da7362bSRobert Watson * 10067da7362bSRobert Watson * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no 10077da7362bSRobert Watson * record boundary, and no change in data type). 10087da7362bSRobert Watson * 10097da7362bSRobert Watson * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into 10107da7362bSRobert Watson * an mbuf already in the socket buffer. This can occur if an 10110f9d0a73SGleb Smirnoff * appropriate mbuf exists, there is room, both mbufs are not marked as 10120f9d0a73SGleb Smirnoff * not ready, and no merging of data types will occur. 10137da7362bSRobert Watson * 10147da7362bSRobert Watson * (3) The mbuf may be appended to the end of the existing mbuf chain. 10157da7362bSRobert Watson * 10167da7362bSRobert Watson * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as 10177da7362bSRobert Watson * end-of-record. 1018df8bae1dSRodney W. Grimes */ 101926f9a767SRodney W. Grimes void 1020050ac265SRobert Watson sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 1021df8bae1dSRodney W. Grimes { 1022050ac265SRobert Watson int eor = 0; 1023050ac265SRobert Watson struct mbuf *o; 1024df8bae1dSRodney W. Grimes 1025a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1026a34b7046SRobert Watson 1027df8bae1dSRodney W. Grimes while (m) { 1028df8bae1dSRodney W. Grimes eor |= m->m_flags & M_EOR; 1029df8bae1dSRodney W. Grimes if (m->m_len == 0 && 1030df8bae1dSRodney W. Grimes (eor == 0 || 1031df8bae1dSRodney W. Grimes (((o = m->m_next) || (o = n)) && 1032df8bae1dSRodney W. Grimes o->m_type == m->m_type))) { 1033395bb186SSam Leffler if (sb->sb_lastrecord == m) 1034395bb186SSam Leffler sb->sb_lastrecord = m->m_next; 1035df8bae1dSRodney W. Grimes m = m_free(m); 1036df8bae1dSRodney W. Grimes continue; 1037df8bae1dSRodney W. Grimes } 103832af0d74SDavid Malone if (n && (n->m_flags & M_EOR) == 0 && 103932af0d74SDavid Malone M_WRITABLE(n) && 10405e0f5cfaSKip Macy ((sb->sb_flags & SB_NOCOALESCE) == 0) && 10410f9d0a73SGleb Smirnoff !(m->m_flags & M_NOTREADY) && 10420f9d0a73SGleb Smirnoff !(n->m_flags & M_NOTREADY) && 104332af0d74SDavid Malone m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ 104432af0d74SDavid Malone m->m_len <= M_TRAILINGSPACE(n) && 1045df8bae1dSRodney W. Grimes n->m_type == m->m_type) { 1046df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 1047df8bae1dSRodney W. Grimes (unsigned)m->m_len); 1048df8bae1dSRodney W. Grimes n->m_len += m->m_len; 10490f9d0a73SGleb Smirnoff sb->sb_ccc += m->m_len; 10500f9d0a73SGleb Smirnoff if (sb->sb_fnrdy == NULL) 10510f9d0a73SGleb Smirnoff sb->sb_acc += m->m_len; 105234333b16SAndre Oppermann if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 1053b3f1af6bSTim J. Robbins /* XXX: Probably don't need.*/ 105404ac9b97SKelly Yancey sb->sb_ctl += m->m_len; 1055df8bae1dSRodney W. Grimes m = m_free(m); 1056df8bae1dSRodney W. Grimes continue; 1057df8bae1dSRodney W. Grimes } 1058df8bae1dSRodney W. Grimes if (n) 1059df8bae1dSRodney W. Grimes n->m_next = m; 1060df8bae1dSRodney W. Grimes else 1061df8bae1dSRodney W. Grimes sb->sb_mb = m; 1062395bb186SSam Leffler sb->sb_mbtail = m; 1063df8bae1dSRodney W. Grimes sballoc(sb, m); 1064df8bae1dSRodney W. Grimes n = m; 1065df8bae1dSRodney W. Grimes m->m_flags &= ~M_EOR; 1066df8bae1dSRodney W. Grimes m = m->m_next; 1067df8bae1dSRodney W. Grimes n->m_next = 0; 1068df8bae1dSRodney W. Grimes } 1069df8bae1dSRodney W. Grimes if (eor) { 10707da7362bSRobert Watson KASSERT(n != NULL, ("sbcompress: eor && n == NULL")); 1071df8bae1dSRodney W. Grimes n->m_flags |= eor; 1072df8bae1dSRodney W. Grimes } 1073395bb186SSam Leffler SBLASTMBUFCHK(sb); 1074df8bae1dSRodney W. Grimes } 1075df8bae1dSRodney W. Grimes 1076df8bae1dSRodney W. Grimes /* 1077050ac265SRobert Watson * Free all mbufs in a sockbuf. Check that all resources are reclaimed. 1078df8bae1dSRodney W. Grimes */ 1079eaa6dfbcSRobert Watson static void 1080050ac265SRobert Watson sbflush_internal(struct sockbuf *sb) 1081df8bae1dSRodney W. Grimes { 1082df8bae1dSRodney W. Grimes 108323f84772SPierre Beyssac while (sb->sb_mbcnt) { 108423f84772SPierre Beyssac /* 1085761a9a1fSGleb Smirnoff * Don't call sbcut(sb, 0) if the leading mbuf is non-empty: 108623f84772SPierre Beyssac * we would loop forever. Panic instead. 108723f84772SPierre Beyssac */ 10880f9d0a73SGleb Smirnoff if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len)) 108923f84772SPierre Beyssac break; 10900f9d0a73SGleb Smirnoff m_freem(sbcut_internal(sb, (int)sb->sb_ccc)); 109123f84772SPierre Beyssac } 10920f9d0a73SGleb Smirnoff KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0, 10930f9d0a73SGleb Smirnoff ("%s: ccc %u mb %p mbcnt %u", __func__, 10940f9d0a73SGleb Smirnoff sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt)); 1095a34b7046SRobert Watson } 1096a34b7046SRobert Watson 1097a34b7046SRobert Watson void 1098050ac265SRobert Watson sbflush_locked(struct sockbuf *sb) 1099eaa6dfbcSRobert Watson { 1100eaa6dfbcSRobert Watson 1101eaa6dfbcSRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1102eaa6dfbcSRobert Watson sbflush_internal(sb); 1103eaa6dfbcSRobert Watson } 1104eaa6dfbcSRobert Watson 1105eaa6dfbcSRobert Watson void 1106050ac265SRobert Watson sbflush(struct sockbuf *sb) 1107a34b7046SRobert Watson { 1108a34b7046SRobert Watson 1109a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1110a34b7046SRobert Watson sbflush_locked(sb); 1111a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1112df8bae1dSRodney W. Grimes } 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes /* 11151d2df300SGleb Smirnoff * Cut data from (the front of) a sockbuf. 1116df8bae1dSRodney W. Grimes */ 11171d2df300SGleb Smirnoff static struct mbuf * 11181d2df300SGleb Smirnoff sbcut_internal(struct sockbuf *sb, int len) 1119df8bae1dSRodney W. Grimes { 11200f9d0a73SGleb Smirnoff struct mbuf *m, *next, *mfree; 1121df8bae1dSRodney W. Grimes 1122f41b2de7SHiren Panchasara KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0", 1123b5b023b9SHiren Panchasara __func__, len)); 1124b5b023b9SHiren Panchasara KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u", 1125b5b023b9SHiren Panchasara __func__, len, sb->sb_ccc)); 1126b5b023b9SHiren Panchasara 1127df8bae1dSRodney W. Grimes next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 11281d2df300SGleb Smirnoff mfree = NULL; 11291d2df300SGleb Smirnoff 1130df8bae1dSRodney W. Grimes while (len > 0) { 11318146bcfeSGleb Smirnoff if (m == NULL) { 11328146bcfeSGleb Smirnoff KASSERT(next, ("%s: no next, len %d", __func__, len)); 1133df8bae1dSRodney W. Grimes m = next; 1134df8bae1dSRodney W. Grimes next = m->m_nextpkt; 1135df8bae1dSRodney W. Grimes } 1136df8bae1dSRodney W. Grimes if (m->m_len > len) { 11370f9d0a73SGleb Smirnoff KASSERT(!(m->m_flags & M_NOTAVAIL), 11380f9d0a73SGleb Smirnoff ("%s: m %p M_NOTAVAIL", __func__, m)); 1139df8bae1dSRodney W. Grimes m->m_len -= len; 1140df8bae1dSRodney W. Grimes m->m_data += len; 11410f9d0a73SGleb Smirnoff sb->sb_ccc -= len; 11420f9d0a73SGleb Smirnoff sb->sb_acc -= len; 11434e023759SAndre Oppermann if (sb->sb_sndptroff != 0) 11444e023759SAndre Oppermann sb->sb_sndptroff -= len; 114534333b16SAndre Oppermann if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA) 114604ac9b97SKelly Yancey sb->sb_ctl -= len; 1147df8bae1dSRodney W. Grimes break; 1148df8bae1dSRodney W. Grimes } 1149df8bae1dSRodney W. Grimes len -= m->m_len; 1150df8bae1dSRodney W. Grimes sbfree(sb, m); 11510f9d0a73SGleb Smirnoff /* 11520f9d0a73SGleb Smirnoff * Do not put M_NOTREADY buffers to the free list, they 11530f9d0a73SGleb Smirnoff * are referenced from outside. 11540f9d0a73SGleb Smirnoff */ 11550f9d0a73SGleb Smirnoff if (m->m_flags & M_NOTREADY) 11560f9d0a73SGleb Smirnoff m = m->m_next; 11570f9d0a73SGleb Smirnoff else { 11580f9d0a73SGleb Smirnoff struct mbuf *n; 11590f9d0a73SGleb Smirnoff 11601d2df300SGleb Smirnoff n = m->m_next; 11611d2df300SGleb Smirnoff m->m_next = mfree; 11621d2df300SGleb Smirnoff mfree = m; 11631d2df300SGleb Smirnoff m = n; 1164df8bae1dSRodney W. Grimes } 11650f9d0a73SGleb Smirnoff } 1166e834a840SGleb Smirnoff /* 1167e834a840SGleb Smirnoff * Free any zero-length mbufs from the buffer. 1168e834a840SGleb Smirnoff * For SOCK_DGRAM sockets such mbufs represent empty records. 1169e834a840SGleb Smirnoff * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer, 1170e834a840SGleb Smirnoff * when sosend_generic() needs to send only control data. 1171e834a840SGleb Smirnoff */ 1172e834a840SGleb Smirnoff while (m && m->m_len == 0) { 1173e834a840SGleb Smirnoff struct mbuf *n; 1174e834a840SGleb Smirnoff 1175e834a840SGleb Smirnoff sbfree(sb, m); 1176e834a840SGleb Smirnoff n = m->m_next; 1177e834a840SGleb Smirnoff m->m_next = mfree; 1178e834a840SGleb Smirnoff mfree = m; 1179e834a840SGleb Smirnoff m = n; 1180e834a840SGleb Smirnoff } 1181df8bae1dSRodney W. Grimes if (m) { 1182df8bae1dSRodney W. Grimes sb->sb_mb = m; 1183df8bae1dSRodney W. Grimes m->m_nextpkt = next; 1184df8bae1dSRodney W. Grimes } else 1185df8bae1dSRodney W. Grimes sb->sb_mb = next; 1186395bb186SSam Leffler /* 1187050ac265SRobert Watson * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure 1188050ac265SRobert Watson * sb_lastrecord is up-to-date if we dropped part of the last record. 1189395bb186SSam Leffler */ 1190395bb186SSam Leffler m = sb->sb_mb; 1191395bb186SSam Leffler if (m == NULL) { 1192395bb186SSam Leffler sb->sb_mbtail = NULL; 1193395bb186SSam Leffler sb->sb_lastrecord = NULL; 1194395bb186SSam Leffler } else if (m->m_nextpkt == NULL) { 1195395bb186SSam Leffler sb->sb_lastrecord = m; 1196395bb186SSam Leffler } 11971d2df300SGleb Smirnoff 11981d2df300SGleb Smirnoff return (mfree); 1199df8bae1dSRodney W. Grimes } 1200df8bae1dSRodney W. Grimes 1201df8bae1dSRodney W. Grimes /* 1202a34b7046SRobert Watson * Drop data from (the front of) a sockbuf. 1203a34b7046SRobert Watson */ 1204a34b7046SRobert Watson void 1205050ac265SRobert Watson sbdrop_locked(struct sockbuf *sb, int len) 1206eaa6dfbcSRobert Watson { 1207eaa6dfbcSRobert Watson 1208eaa6dfbcSRobert Watson SOCKBUF_LOCK_ASSERT(sb); 12091d2df300SGleb Smirnoff m_freem(sbcut_internal(sb, len)); 12101d2df300SGleb Smirnoff } 1211eaa6dfbcSRobert Watson 12121d2df300SGleb Smirnoff /* 12131d2df300SGleb Smirnoff * Drop data from (the front of) a sockbuf, 12141d2df300SGleb Smirnoff * and return it to caller. 12151d2df300SGleb Smirnoff */ 12161d2df300SGleb Smirnoff struct mbuf * 12171d2df300SGleb Smirnoff sbcut_locked(struct sockbuf *sb, int len) 12181d2df300SGleb Smirnoff { 12191d2df300SGleb Smirnoff 12201d2df300SGleb Smirnoff SOCKBUF_LOCK_ASSERT(sb); 12211d2df300SGleb Smirnoff return (sbcut_internal(sb, len)); 1222eaa6dfbcSRobert Watson } 1223eaa6dfbcSRobert Watson 1224eaa6dfbcSRobert Watson void 1225050ac265SRobert Watson sbdrop(struct sockbuf *sb, int len) 1226a34b7046SRobert Watson { 12271d2df300SGleb Smirnoff struct mbuf *mfree; 1228a34b7046SRobert Watson 1229a34b7046SRobert Watson SOCKBUF_LOCK(sb); 12301d2df300SGleb Smirnoff mfree = sbcut_internal(sb, len); 1231a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 12321d2df300SGleb Smirnoff 12331d2df300SGleb Smirnoff m_freem(mfree); 1234a34b7046SRobert Watson } 1235a34b7046SRobert Watson 12364e023759SAndre Oppermann /* 12374e023759SAndre Oppermann * Maintain a pointer and offset pair into the socket buffer mbuf chain to 12384e023759SAndre Oppermann * avoid traversal of the entire socket buffer for larger offsets. 12394e023759SAndre Oppermann */ 12404e023759SAndre Oppermann struct mbuf * 12414e023759SAndre Oppermann sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff) 12424e023759SAndre Oppermann { 12434e023759SAndre Oppermann struct mbuf *m, *ret; 12444e023759SAndre Oppermann 12454e023759SAndre Oppermann KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 12460f9d0a73SGleb Smirnoff KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__)); 12470f9d0a73SGleb Smirnoff KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__)); 12484e023759SAndre Oppermann 12494e023759SAndre Oppermann /* 12504e023759SAndre Oppermann * Is off below stored offset? Happens on retransmits. 12514e023759SAndre Oppermann * Just return, we can't help here. 12524e023759SAndre Oppermann */ 12534e023759SAndre Oppermann if (sb->sb_sndptroff > off) { 12544e023759SAndre Oppermann *moff = off; 12554e023759SAndre Oppermann return (sb->sb_mb); 12564e023759SAndre Oppermann } 12574e023759SAndre Oppermann 12584e023759SAndre Oppermann /* Return closest mbuf in chain for current offset. */ 12594e023759SAndre Oppermann *moff = off - sb->sb_sndptroff; 12604e023759SAndre Oppermann m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb; 12610963c8e4SLawrence Stewart if (*moff == m->m_len) { 12620963c8e4SLawrence Stewart *moff = 0; 12630963c8e4SLawrence Stewart sb->sb_sndptroff += m->m_len; 12640963c8e4SLawrence Stewart m = ret = m->m_next; 12650963c8e4SLawrence Stewart KASSERT(ret->m_len > 0, 12660963c8e4SLawrence Stewart ("mbuf %p in sockbuf %p chain has no valid data", ret, sb)); 12670963c8e4SLawrence Stewart } 12684e023759SAndre Oppermann 12694e023759SAndre Oppermann /* Advance by len to be as close as possible for the next transmit. */ 12704e023759SAndre Oppermann for (off = off - sb->sb_sndptroff + len - 1; 12716f4745d5SBjoern A. Zeeb off > 0 && m != NULL && off >= m->m_len; 12724e023759SAndre Oppermann m = m->m_next) { 12734e023759SAndre Oppermann sb->sb_sndptroff += m->m_len; 12744e023759SAndre Oppermann off -= m->m_len; 12754e023759SAndre Oppermann } 12766f4745d5SBjoern A. Zeeb if (off > 0 && m == NULL) 12776f4745d5SBjoern A. Zeeb panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret); 12784e023759SAndre Oppermann sb->sb_sndptr = m; 12794e023759SAndre Oppermann 12804e023759SAndre Oppermann return (ret); 12814e023759SAndre Oppermann } 12824e023759SAndre Oppermann 1283a34b7046SRobert Watson /* 12849fd573c3SHans Petter Selasky * Return the first mbuf and the mbuf data offset for the provided 12859fd573c3SHans Petter Selasky * send offset without changing the "sb_sndptroff" field. 12869fd573c3SHans Petter Selasky */ 12879fd573c3SHans Petter Selasky struct mbuf * 12889fd573c3SHans Petter Selasky sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff) 12899fd573c3SHans Petter Selasky { 12909fd573c3SHans Petter Selasky struct mbuf *m; 12919fd573c3SHans Petter Selasky 12929fd573c3SHans Petter Selasky KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__)); 12939fd573c3SHans Petter Selasky 12949fd573c3SHans Petter Selasky /* 12959fd573c3SHans Petter Selasky * If the "off" is below the stored offset, which happens on 12969fd573c3SHans Petter Selasky * retransmits, just use "sb_mb": 12979fd573c3SHans Petter Selasky */ 12989fd573c3SHans Petter Selasky if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) { 12999fd573c3SHans Petter Selasky m = sb->sb_mb; 13009fd573c3SHans Petter Selasky } else { 13019fd573c3SHans Petter Selasky m = sb->sb_sndptr; 13029fd573c3SHans Petter Selasky off -= sb->sb_sndptroff; 13039fd573c3SHans Petter Selasky } 13049fd573c3SHans Petter Selasky while (off > 0 && m != NULL) { 13059fd573c3SHans Petter Selasky if (off < m->m_len) 13069fd573c3SHans Petter Selasky break; 13079fd573c3SHans Petter Selasky off -= m->m_len; 13089fd573c3SHans Petter Selasky m = m->m_next; 13099fd573c3SHans Petter Selasky } 13109fd573c3SHans Petter Selasky *moff = off; 13119fd573c3SHans Petter Selasky return (m); 13129fd573c3SHans Petter Selasky } 13139fd573c3SHans Petter Selasky 13149fd573c3SHans Petter Selasky /* 1315050ac265SRobert Watson * Drop a record off the front of a sockbuf and move the next record to the 1316050ac265SRobert Watson * front. 1317df8bae1dSRodney W. Grimes */ 131826f9a767SRodney W. Grimes void 1319050ac265SRobert Watson sbdroprecord_locked(struct sockbuf *sb) 1320df8bae1dSRodney W. Grimes { 1321050ac265SRobert Watson struct mbuf *m; 1322df8bae1dSRodney W. Grimes 1323a34b7046SRobert Watson SOCKBUF_LOCK_ASSERT(sb); 1324a34b7046SRobert Watson 1325df8bae1dSRodney W. Grimes m = sb->sb_mb; 1326df8bae1dSRodney W. Grimes if (m) { 1327df8bae1dSRodney W. Grimes sb->sb_mb = m->m_nextpkt; 1328df8bae1dSRodney W. Grimes do { 1329df8bae1dSRodney W. Grimes sbfree(sb, m); 1330ecde8f7cSMatthew Dillon m = m_free(m); 1331797f2d22SPoul-Henning Kamp } while (m); 1332df8bae1dSRodney W. Grimes } 1333395bb186SSam Leffler SB_EMPTY_FIXUP(sb); 1334df8bae1dSRodney W. Grimes } 13351e4ad9ceSGarrett Wollman 133682c23ebaSBill Fenner /* 1337050ac265SRobert Watson * Drop a record off the front of a sockbuf and move the next record to the 1338050ac265SRobert Watson * front. 1339a34b7046SRobert Watson */ 1340a34b7046SRobert Watson void 1341050ac265SRobert Watson sbdroprecord(struct sockbuf *sb) 1342a34b7046SRobert Watson { 1343a34b7046SRobert Watson 1344a34b7046SRobert Watson SOCKBUF_LOCK(sb); 1345a34b7046SRobert Watson sbdroprecord_locked(sb); 1346a34b7046SRobert Watson SOCKBUF_UNLOCK(sb); 1347a34b7046SRobert Watson } 1348a34b7046SRobert Watson 134920d9e5e8SRobert Watson /* 13508c799760SRobert Watson * Create a "control" mbuf containing the specified data with the specified 13518c799760SRobert Watson * type for presentation on a socket buffer. 135220d9e5e8SRobert Watson */ 135320d9e5e8SRobert Watson struct mbuf * 1354d19e16a7SRobert Watson sbcreatecontrol(caddr_t p, int size, int type, int level) 135520d9e5e8SRobert Watson { 1356d19e16a7SRobert Watson struct cmsghdr *cp; 135720d9e5e8SRobert Watson struct mbuf *m; 135820d9e5e8SRobert Watson 135920d9e5e8SRobert Watson if (CMSG_SPACE((u_int)size) > MCLBYTES) 136020d9e5e8SRobert Watson return ((struct mbuf *) NULL); 136120d9e5e8SRobert Watson if (CMSG_SPACE((u_int)size) > MLEN) 1362eb1b1807SGleb Smirnoff m = m_getcl(M_NOWAIT, MT_CONTROL, 0); 136320d9e5e8SRobert Watson else 1364eb1b1807SGleb Smirnoff m = m_get(M_NOWAIT, MT_CONTROL); 136520d9e5e8SRobert Watson if (m == NULL) 136620d9e5e8SRobert Watson return ((struct mbuf *) NULL); 136720d9e5e8SRobert Watson cp = mtod(m, struct cmsghdr *); 136820d9e5e8SRobert Watson m->m_len = 0; 136920d9e5e8SRobert Watson KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m), 137020d9e5e8SRobert Watson ("sbcreatecontrol: short mbuf")); 13712827952eSXin LI /* 13722827952eSXin LI * Don't leave the padding between the msg header and the 13732827952eSXin LI * cmsg data and the padding after the cmsg data un-initialized. 13742827952eSXin LI */ 13752827952eSXin LI bzero(cp, CMSG_SPACE((u_int)size)); 137620d9e5e8SRobert Watson if (p != NULL) 137720d9e5e8SRobert Watson (void)memcpy(CMSG_DATA(cp), p, size); 137820d9e5e8SRobert Watson m->m_len = CMSG_SPACE(size); 137920d9e5e8SRobert Watson cp->cmsg_len = CMSG_LEN(size); 138020d9e5e8SRobert Watson cp->cmsg_level = level; 138120d9e5e8SRobert Watson cp->cmsg_type = type; 138220d9e5e8SRobert Watson return (m); 138320d9e5e8SRobert Watson } 138420d9e5e8SRobert Watson 138520d9e5e8SRobert Watson /* 13868c799760SRobert Watson * This does the same for socket buffers that sotoxsocket does for sockets: 13878c799760SRobert Watson * generate an user-format data structure describing the socket buffer. Note 13888c799760SRobert Watson * that the xsockbuf structure, since it is always embedded in a socket, does 13898c799760SRobert Watson * not include a self pointer nor a length. We make this entry point public 13908c799760SRobert Watson * in case some other mechanism needs it. 139120d9e5e8SRobert Watson */ 139220d9e5e8SRobert Watson void 139320d9e5e8SRobert Watson sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 139420d9e5e8SRobert Watson { 1395d19e16a7SRobert Watson 13960f9d0a73SGleb Smirnoff xsb->sb_cc = sb->sb_ccc; 139720d9e5e8SRobert Watson xsb->sb_hiwat = sb->sb_hiwat; 139820d9e5e8SRobert Watson xsb->sb_mbcnt = sb->sb_mbcnt; 139949f287f8SGeorge V. Neville-Neil xsb->sb_mcnt = sb->sb_mcnt; 140049f287f8SGeorge V. Neville-Neil xsb->sb_ccnt = sb->sb_ccnt; 140120d9e5e8SRobert Watson xsb->sb_mbmax = sb->sb_mbmax; 140220d9e5e8SRobert Watson xsb->sb_lowat = sb->sb_lowat; 140320d9e5e8SRobert Watson xsb->sb_flags = sb->sb_flags; 140420d9e5e8SRobert Watson xsb->sb_timeo = sb->sb_timeo; 140520d9e5e8SRobert Watson } 140620d9e5e8SRobert Watson 1407639acc13SGarrett Wollman /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ 1408639acc13SGarrett Wollman static int dummy; 1409639acc13SGarrett Wollman SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 14101b978d45SHartmut Brandt SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW, 14111b978d45SHartmut Brandt &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size"); 14121b978d45SHartmut Brandt SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 14133eb9ab52SEitan Adler &sb_efficiency, 0, "Socket buffer size waste factor"); 1414