2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include "opt_kern_tls.h"
34 #include "opt_param.h"
36 #include <sys/param.h>
37 #include <sys/aio.h> /* for aio_swake proto */
38 #include <sys/kernel.h>
41 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/protosw.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
54 #include <netinet/in.h>
57 * Function pointer set by the AIO routines so that the socket buffer code
58 * can call back into the AIO module if it is loaded.
60 void (*aio_swake)(struct socket *, struct sockbuf *);
63 * Primitive routines for operating on socket buffers
66 #define BUF_MAX_ADJ(_sz) (((u_quad_t)(_sz)) * MCLBYTES / (MSIZE + MCLBYTES))
68 u_long sb_max = SB_MAX;
69 u_long sb_max_adj = BUF_MAX_ADJ(SB_MAX);
71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
74 static void sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m,
77 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len);
78 static void sbflush_internal(struct sockbuf *sb);
81 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
84 sbm_clrprotoflags(struct mbuf *m, int flags)
89 if (flags & PRUS_NOTREADY)
98 * Compress M_NOTREADY mbufs after they have been readied by sbready().
100 * sbcompress() skips M_NOTREADY mbufs since the data is not available to
101 * be copied at the time of sbcompress(). This function combines small
102 * mbufs similar to sbcompress() once mbufs are ready. 'm0' is the first
103 * mbuf sbready() marked ready, and 'end' is the first mbuf still not
107 sbready_compress(struct sockbuf *sb, struct mbuf *m0, struct mbuf *end)
112 SOCKBUF_LOCK_ASSERT(sb);
114 if ((sb->sb_flags & SB_NOCOALESCE) != 0)
117 for (m = m0; m != end; m = m->m_next) {
118 MPASS((m->m_flags & M_NOTREADY) == 0);
120 * NB: In sbcompress(), 'n' is the last mbuf in the
121 * socket buffer and 'm' is the new mbuf being copied
122 * into the trailing space of 'n'. Here, the roles
123 * are reversed and 'n' is the next mbuf after 'm'
124 * that is being copied into the trailing space of
129 /* Try to coalesce adjacent ktls mbuf hdr/trailers. */
130 if ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 &&
131 (m->m_flags & M_EXTPG) &&
132 (n->m_flags & M_EXTPG) &&
133 !mbuf_has_tls_session(m) &&
134 !mbuf_has_tls_session(n)) {
135 int hdr_len, trail_len;
137 hdr_len = n->m_epg_hdrlen;
138 trail_len = m->m_epg_trllen;
139 if (trail_len != 0 && hdr_len != 0 &&
140 trail_len + hdr_len <= MBUF_PEXT_TRAIL_LEN) {
141 /* copy n's header to m's trailer */
142 memcpy(&m->m_epg_trail[trail_len],
143 n->m_epg_hdr, hdr_len);
144 m->m_epg_trllen += hdr_len;
152 /* Compress small unmapped mbufs into plain mbufs. */
153 if ((m->m_flags & M_EXTPG) && m->m_len <= MLEN &&
154 !mbuf_has_tls_session(m)) {
155 ext_size = m->m_ext.ext_size;
156 if (mb_unmapped_compress(m) == 0)
157 sb->sb_mbcnt -= ext_size;
160 while ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 &&
162 (m->m_flags & M_EXTPG) == 0 &&
163 !mbuf_has_tls_session(n) &&
164 !mbuf_has_tls_session(m) &&
165 n->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
166 n->m_len <= M_TRAILINGSPACE(m) &&
167 m->m_type == n->m_type) {
168 KASSERT(sb->sb_lastrecord != n,
169 ("%s: merging start of record (%p) into previous mbuf (%p)",
171 m_copydata(n, 0, n->m_len, mtodo(m, m->m_len));
172 m->m_len += n->m_len;
173 m->m_next = n->m_next;
174 m->m_flags |= n->m_flags & M_EOR;
175 if (sb->sb_mbtail == n)
178 sb->sb_mbcnt -= MSIZE;
179 if (n->m_flags & M_EXT)
180 sb->sb_mbcnt -= n->m_ext.ext_size;
190 * Mark ready "count" units of I/O starting with "m". Most mbufs
191 * count as a single unit of I/O except for M_EXTPG mbufs which
192 * are backed by multiple pages.
195 sbready(struct sockbuf *sb, struct mbuf *m0, int count)
200 SOCKBUF_LOCK_ASSERT(sb);
201 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
202 KASSERT(count > 0, ("%s: invalid count %d", __func__, count));
205 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
208 KASSERT(m->m_flags & M_NOTREADY,
209 ("%s: m %p !M_NOTREADY", __func__, m));
210 if ((m->m_flags & M_EXTPG) != 0 && m->m_epg_npgs != 0) {
211 if (count < m->m_epg_nrdy) {
212 m->m_epg_nrdy -= count;
216 count -= m->m_epg_nrdy;
221 m->m_flags &= ~(M_NOTREADY | blocker);
223 sb->sb_acc += m->m_len;
228 * If the first mbuf is still not fully ready because only
229 * some of its backing pages were readied, no further progress
233 MPASS(m->m_flags & M_NOTREADY);
234 return (EINPROGRESS);
238 sbready_compress(sb, m0, m);
239 return (EINPROGRESS);
242 /* This one was blocking all the queue. */
243 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
244 KASSERT(m->m_flags & M_BLOCKED,
245 ("%s: m %p !M_BLOCKED", __func__, m));
246 m->m_flags &= ~M_BLOCKED;
247 sb->sb_acc += m->m_len;
251 sbready_compress(sb, m0, m);
257 * Adjust sockbuf state reflecting allocation of m.
260 sballoc(struct sockbuf *sb, struct mbuf *m)
263 SOCKBUF_LOCK_ASSERT(sb);
265 sb->sb_ccc += m->m_len;
267 if (sb->sb_fnrdy == NULL) {
268 if (m->m_flags & M_NOTREADY)
271 sb->sb_acc += m->m_len;
273 m->m_flags |= M_BLOCKED;
275 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
276 sb->sb_ctl += m->m_len;
278 sb->sb_mbcnt += MSIZE;
280 if (m->m_flags & M_EXT)
281 sb->sb_mbcnt += m->m_ext.ext_size;
285 * Adjust sockbuf state reflecting freeing of m.
288 sbfree(struct sockbuf *sb, struct mbuf *m)
291 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
292 SOCKBUF_LOCK_ASSERT(sb);
295 sb->sb_ccc -= m->m_len;
297 if (!(m->m_flags & M_NOTAVAIL))
298 sb->sb_acc -= m->m_len;
300 if (m == sb->sb_fnrdy) {
303 KASSERT(m->m_flags & M_NOTREADY,
304 ("%s: m %p !M_NOTREADY", __func__, m));
307 while (n != NULL && !(n->m_flags & M_NOTREADY)) {
308 n->m_flags &= ~M_BLOCKED;
309 sb->sb_acc += n->m_len;
315 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
316 sb->sb_ctl -= m->m_len;
318 sb->sb_mbcnt -= MSIZE;
319 if (m->m_flags & M_EXT)
320 sb->sb_mbcnt -= m->m_ext.ext_size;
322 if (sb->sb_sndptr == m) {
323 sb->sb_sndptr = NULL;
324 sb->sb_sndptroff = 0;
326 if (sb->sb_sndptroff != 0)
327 sb->sb_sndptroff -= m->m_len;
332 * Similar to sballoc/sbfree but does not adjust state associated with
333 * the sb_mb chain such as sb_fnrdy or sb_sndptr*. Also assumes mbufs
337 sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m)
340 SOCKBUF_LOCK_ASSERT(sb);
342 sb->sb_ccc += m->m_len;
343 sb->sb_tlscc += m->m_len;
345 sb->sb_mbcnt += MSIZE;
347 if (m->m_flags & M_EXT)
348 sb->sb_mbcnt += m->m_ext.ext_size;
352 sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m)
355 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
356 SOCKBUF_LOCK_ASSERT(sb);
359 sb->sb_ccc -= m->m_len;
360 sb->sb_tlscc -= m->m_len;
362 sb->sb_mbcnt -= MSIZE;
364 if (m->m_flags & M_EXT)
365 sb->sb_mbcnt -= m->m_ext.ext_size;
370 * Socantsendmore indicates that no more data will be sent on the socket; it
371 * would normally be applied to a socket when the user informs the system
372 * that no more data is to be sent, by the protocol code (in case
373 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
374 * received, and will normally be applied to the socket by a protocol when it
375 * detects that the peer will send no more data. Data queued for reading in
376 * the socket may yet be read.
379 socantsendmore_locked(struct socket *so)
382 SOCK_SENDBUF_LOCK_ASSERT(so);
384 so->so_snd.sb_state |= SBS_CANTSENDMORE;
385 sowwakeup_locked(so);
386 SOCK_SENDBUF_UNLOCK_ASSERT(so);
390 socantsendmore(struct socket *so)
393 SOCK_SENDBUF_LOCK(so);
394 socantsendmore_locked(so);
395 SOCK_SENDBUF_UNLOCK_ASSERT(so);
399 socantrcvmore_locked(struct socket *so)
402 SOCK_RECVBUF_LOCK_ASSERT(so);
404 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
406 if (so->so_rcv.sb_flags & SB_TLS_RX)
407 ktls_check_rx(&so->so_rcv);
409 sorwakeup_locked(so);
410 SOCK_RECVBUF_UNLOCK_ASSERT(so);
414 socantrcvmore(struct socket *so)
417 SOCK_RECVBUF_LOCK(so);
418 socantrcvmore_locked(so);
419 SOCK_RECVBUF_UNLOCK_ASSERT(so);
423 soroverflow_locked(struct socket *so)
426 SOCK_RECVBUF_LOCK_ASSERT(so);
428 if (so->so_options & SO_RERROR) {
429 so->so_rerror = ENOBUFS;
430 sorwakeup_locked(so);
432 SOCK_RECVBUF_UNLOCK(so);
434 SOCK_RECVBUF_UNLOCK_ASSERT(so);
438 soroverflow(struct socket *so)
441 SOCK_RECVBUF_LOCK(so);
442 soroverflow_locked(so);
443 SOCK_RECVBUF_UNLOCK_ASSERT(so);
447 * Wait for data to arrive at/drain from a socket buffer.
450 sbwait(struct socket *so, sb_which which)
454 SOCK_BUF_LOCK_ASSERT(so, which);
456 sb = sobuf(so, which);
457 sb->sb_flags |= SB_WAIT;
458 return (msleep_sbt(&sb->sb_acc, soeventmtx(so, which),
459 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
460 sb->sb_timeo, 0, 0));
464 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
465 * via SIGIO if the socket has the SS_ASYNC flag set.
467 * Called with the socket buffer lock held; will release the lock by the end
468 * of the function. This allows the caller to acquire the socket buffer lock
469 * while testing for the need for various sorts of wakeup and hold it through
470 * to the point where it's no longer required. We currently hold the lock
471 * through calls out to other subsystems (with the exception of kqueue), and
472 * then release it to avoid lock order issues. It's not clear that's
475 static __always_inline void
476 sowakeup(struct socket *so, const sb_which which)
481 SOCK_BUF_LOCK_ASSERT(so, which);
483 sb = sobuf(so, which);
484 selwakeuppri(sb->sb_sel, PSOCK);
485 if (!SEL_WAITING(sb->sb_sel))
486 sb->sb_flags &= ~SB_SEL;
487 if (sb->sb_flags & SB_WAIT) {
488 sb->sb_flags &= ~SB_WAIT;
491 KNOTE_LOCKED(&sb->sb_sel->si_note, 0);
492 if (sb->sb_upcall != NULL) {
493 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
494 if (ret == SU_ISCONNECTED) {
495 KASSERT(sb == &so->so_rcv,
496 ("SO_SND upcall returned SU_ISCONNECTED"));
497 soupcall_clear(so, SO_RCV);
501 if (sb->sb_flags & SB_AIO)
502 sowakeup_aio(so, which);
503 SOCK_BUF_UNLOCK(so, which);
504 if (ret == SU_ISCONNECTED)
506 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
507 pgsigio(&so->so_sigio, SIGIO, 0);
508 SOCK_BUF_UNLOCK_ASSERT(so, which);
512 * Do we need to notify the other side when I/O is possible?
514 static __always_inline bool
515 sb_notify(const struct sockbuf *sb)
517 return ((sb->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC |
518 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0);
522 sorwakeup_locked(struct socket *so)
524 SOCK_RECVBUF_LOCK_ASSERT(so);
525 if (sb_notify(&so->so_rcv))
526 sowakeup(so, SO_RCV);
528 SOCK_RECVBUF_UNLOCK(so);
532 sowwakeup_locked(struct socket *so)
534 SOCK_SENDBUF_LOCK_ASSERT(so);
535 if (sb_notify(&so->so_snd))
536 sowakeup(so, SO_SND);
538 SOCK_SENDBUF_UNLOCK(so);
542 * Socket buffer (struct sockbuf) utility routines.
544 * Each socket contains two socket buffers: one for sending data and one for
545 * receiving data. Each buffer contains a queue of mbufs, information about
546 * the number of mbufs and amount of data in the queue, and other fields
547 * allowing select() statements and notification on data availability to be
550 * Data stored in a socket buffer is maintained as a list of records. Each
551 * record is a list of mbufs chained together with the m_next field. Records
552 * are chained together with the m_nextpkt field. The upper level routine
553 * soreceive() expects the following conventions to be observed when placing
554 * information in the receive buffer:
556 * 1. If the protocol requires each message be preceded by the sender's name,
557 * then a record containing that name must be present before any
558 * associated data (mbuf's must be of type MT_SONAME).
559 * 2. If the protocol supports the exchange of ``access rights'' (really just
560 * additional data associated with the message), and there are ``rights''
561 * to be received, then a record containing this data should be present
562 * (mbuf's must be of type MT_RIGHTS).
563 * 3. If a name or rights record exists, then it must be followed by a data
564 * record, perhaps of zero length.
566 * Before using a new socket structure it is first necessary to reserve
567 * buffer space to the socket, by calling sbreserve(). This should commit
568 * some of the available buffer space in the system buffer pool for the
569 * socket (currently, it does nothing but enforce limits). The space should
570 * be released by calling sbrelease() when the socket is destroyed.
573 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
575 struct thread *td = curthread;
577 SOCK_SENDBUF_LOCK(so);
578 SOCK_RECVBUF_LOCK(so);
579 if (sbreserve_locked(so, SO_SND, sndcc, td) == 0)
581 if (sbreserve_locked(so, SO_RCV, rcvcc, td) == 0)
583 if (so->so_rcv.sb_lowat == 0)
584 so->so_rcv.sb_lowat = 1;
585 if (so->so_snd.sb_lowat == 0)
586 so->so_snd.sb_lowat = MCLBYTES;
587 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
588 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
589 SOCK_RECVBUF_UNLOCK(so);
590 SOCK_SENDBUF_UNLOCK(so);
593 sbrelease_locked(so, SO_SND);
595 SOCK_RECVBUF_UNLOCK(so);
596 SOCK_SENDBUF_UNLOCK(so);
601 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
604 u_long tmp_sb_max = sb_max;
606 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
607 if (error || !req->newptr)
609 if (tmp_sb_max < MSIZE + MCLBYTES)
612 sb_max_adj = BUF_MAX_ADJ(sb_max);
617 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
618 * become limiting if buffering efficiency is near the normal case.
621 sbreserve_locked_limit(struct socket *so, sb_which which, u_long cc,
622 u_long buf_max, struct thread *td)
624 struct sockbuf *sb = sobuf(so, which);
627 SOCK_BUF_LOCK_ASSERT(so, which);
630 * When a thread is passed, we take into account the thread's socket
631 * buffer size limit. The caller will generally pass curthread, but
632 * in the TCP input path, NULL will be passed to indicate that no
633 * appropriate thread resource limits are available. In that case,
634 * we don't apply a process limit.
636 if (cc > BUF_MAX_ADJ(buf_max))
639 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
641 sbsize_limit = RLIM_INFINITY;
642 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
645 sb->sb_mbmax = min(cc * sb_efficiency, buf_max);
646 if (sb->sb_lowat > sb->sb_hiwat)
647 sb->sb_lowat = sb->sb_hiwat;
652 sbreserve_locked(struct socket *so, sb_which which, u_long cc,
655 return (sbreserve_locked_limit(so, which, cc, sb_max, td));
659 sbsetopt(struct socket *so, struct sockopt *sopt)
664 u_int cc, *hiwat, *lowat;
667 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
672 * Values < 1 make no sense for any of these options,
681 if (SOLISTENING(so)) {
682 switch (sopt->sopt_name) {
685 lowat = &so->sol_sbsnd_lowat;
686 hiwat = &so->sol_sbsnd_hiwat;
687 flags = &so->sol_sbsnd_flags;
691 lowat = &so->sol_sbrcv_lowat;
692 hiwat = &so->sol_sbrcv_hiwat;
693 flags = &so->sol_sbrcv_flags;
697 switch (sopt->sopt_name) {
709 flags = &sb->sb_flags;
710 hiwat = &sb->sb_hiwat;
711 lowat = &sb->sb_lowat;
712 SOCK_BUF_LOCK(so, wh);
716 switch (sopt->sopt_name) {
719 if (SOLISTENING(so)) {
720 if (cc > sb_max_adj) {
728 if (!sbreserve_locked(so, wh, cc, curthread))
732 *flags &= ~SB_AUTOSIZE;
737 * Make sure the low-water is never greater than the
740 *lowat = (cc > *hiwat) ? *hiwat : cc;
744 if (!SOLISTENING(so))
745 SOCK_BUF_UNLOCK(so, wh);
751 * Free mbufs held by a socket, and reserved mbuf space.
754 sbrelease_internal(struct socket *so, sb_which which)
756 struct sockbuf *sb = sobuf(so, which);
758 sbflush_internal(sb);
759 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
765 sbrelease_locked(struct socket *so, sb_which which)
768 SOCK_BUF_LOCK_ASSERT(so, which);
770 sbrelease_internal(so, which);
774 sbrelease(struct socket *so, sb_which which)
777 SOCK_BUF_LOCK(so, which);
778 sbrelease_locked(so, which);
779 SOCK_BUF_UNLOCK(so, which);
783 sbdestroy(struct socket *so, sb_which which)
786 struct sockbuf *sb = sobuf(so, which);
788 if (sb->sb_tls_info != NULL)
789 ktls_free(sb->sb_tls_info);
790 sb->sb_tls_info = NULL;
792 sbrelease_internal(so, which);
796 * Routines to add and remove data from an mbuf queue.
798 * The routines sbappend() or sbappendrecord() are normally called to append
799 * new mbufs to a socket buffer, after checking that adequate space is
800 * available, comparing the function sbspace() with the amount of data to be
801 * added. sbappendrecord() differs from sbappend() in that data supplied is
802 * treated as the beginning of a new record. To place a sender's address,
803 * optional access rights, and data in a socket receive buffer,
804 * sbappendaddr() should be used. To place access rights and data in a
805 * socket receive buffer, sbappendrights() should be used. In either case,
806 * the new data begins a new record. Note that unlike sbappend() and
807 * sbappendrecord(), these routines check for the caller that there will be
808 * enough space to store the data. Each fails if there is not enough space,
809 * or if it cannot find mbufs to store additional information in.
811 * Reliable protocols may use the socket send buffer to hold data awaiting
812 * acknowledgement. Data is normally copied from a socket send buffer in a
813 * protocol with m_copy for output to a peer, and then removing the data from
814 * the socket buffer with sbdrop() or sbdroprecord() when the data is
815 * acknowledged by the peer.
819 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
821 struct mbuf *m = sb->sb_mb;
823 SOCKBUF_LOCK_ASSERT(sb);
825 while (m && m->m_nextpkt)
828 if (m != sb->sb_lastrecord) {
829 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
830 __func__, sb->sb_mb, sb->sb_lastrecord, m);
831 printf("packet chain:\n");
832 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
834 panic("%s from %s:%u", __func__, file, line);
839 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
841 struct mbuf *m = sb->sb_mb;
844 SOCKBUF_LOCK_ASSERT(sb);
846 while (m && m->m_nextpkt)
849 while (m && m->m_next)
852 if (m != sb->sb_mbtail) {
853 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
854 __func__, sb->sb_mb, sb->sb_mbtail, m);
855 printf("packet tree:\n");
856 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
858 for (n = m; n != NULL; n = n->m_next)
862 panic("%s from %s:%u", __func__, file, line);
867 while (m && m->m_next)
870 if (m != sb->sb_mtlstail) {
871 printf("%s: sb_mtls %p sb_mtlstail %p last %p\n",
872 __func__, sb->sb_mtls, sb->sb_mtlstail, m);
873 printf("TLS packet tree:\n");
875 for (m = sb->sb_mtls; m != NULL; m = m->m_next) {
879 panic("%s from %s:%u", __func__, file, line);
883 #endif /* SOCKBUF_DEBUG */
885 #define SBLINKRECORD(sb, m0) do { \
886 SOCKBUF_LOCK_ASSERT(sb); \
887 if ((sb)->sb_lastrecord != NULL) \
888 (sb)->sb_lastrecord->m_nextpkt = (m0); \
890 (sb)->sb_mb = (m0); \
891 (sb)->sb_lastrecord = (m0); \
892 } while (/*CONSTCOND*/0)
895 * Append mbuf chain m to the last record in the socket buffer sb. The
896 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
897 * are discarded and mbufs are compacted where possible.
900 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
904 SOCKBUF_LOCK_ASSERT(sb);
908 kmsan_check_mbuf(m, "sbappend");
909 sbm_clrprotoflags(m, flags);
916 if (n->m_flags & M_EOR) {
917 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
920 } while (n->m_next && (n = n->m_next));
923 * XXX Would like to simply use sb_mbtail here, but
924 * XXX I need to verify that I won't miss an EOR that
927 if ((n = sb->sb_lastrecord) != NULL) {
929 if (n->m_flags & M_EOR) {
930 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
933 } while (n->m_next && (n = n->m_next));
936 * If this is the first record in the socket buffer,
937 * it's also the last record.
939 sb->sb_lastrecord = m;
942 sbcompress(sb, m, n);
947 * Append mbuf chain m to the last record in the socket buffer sb. The
948 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
949 * are discarded and mbufs are compacted where possible.
952 sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
956 sbappend_locked(sb, m, flags);
962 * Append an mbuf containing encrypted TLS data. The data
963 * is marked M_NOTREADY until it has been decrypted and
964 * stored as a TLS record.
967 sbappend_ktls_rx(struct sockbuf *sb, struct mbuf *m)
978 /* Mbuf chain must start with a packet header. */
979 MPASS((m->m_flags & M_PKTHDR) != 0);
981 /* Remove all packet headers and mbuf tags to get a pure data chain. */
982 for (n = m; n != NULL; n = n->m_next) {
983 if (n->m_flags & M_PKTHDR) {
984 ifp = m->m_pkthdr.leaf_rcvif;
985 if ((n->m_pkthdr.csum_flags & CSUM_TLS_MASK) ==
986 CSUM_TLS_DECRYPTED) {
987 /* Mark all mbufs in this packet decrypted. */
988 flags = M_NOTREADY | M_DECRYPTED;
995 n->m_flags &= M_DEMOTEFLAGS;
998 MPASS((n->m_flags & M_NOTREADY) != 0);
1001 sbcompress_ktls_rx(sb, m, sb->sb_mtlstail);
1004 /* Check for incoming packet route changes: */
1005 if (ifp != NULL && sb->sb_tls_info->rx_ifp != NULL &&
1006 sb->sb_tls_info->rx_ifp != ifp)
1007 ktls_input_ifp_mismatch(sb, ifp);
1012 * This version of sbappend() should only be used when the caller absolutely
1013 * knows that there will never be more than one record in the socket buffer,
1014 * that is, a stream protocol (such as TCP).
1017 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
1019 SOCKBUF_LOCK_ASSERT(sb);
1021 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
1023 kmsan_check_mbuf(m, "sbappend");
1027 * Decrypted TLS records are appended as records via
1028 * sbappendrecord(). TCP passes encrypted TLS records to this
1029 * function which must be scheduled for decryption.
1031 if (sb->sb_flags & SB_TLS_RX) {
1032 sbappend_ktls_rx(sb, m);
1037 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
1042 if (sb->sb_tls_info != NULL)
1046 /* Remove all packet headers and mbuf tags to get a pure data chain. */
1047 m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0);
1049 sbcompress(sb, m, sb->sb_mbtail);
1051 sb->sb_lastrecord = sb->sb_mb;
1052 SBLASTRECORDCHK(sb);
1056 * This version of sbappend() should only be used when the caller absolutely
1057 * knows that there will never be more than one record in the socket buffer,
1058 * that is, a stream protocol (such as TCP).
1061 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags)
1065 sbappendstream_locked(sb, m, flags);
1069 #ifdef SOCKBUF_DEBUG
1071 sbcheck(struct sockbuf *sb, const char *file, int line)
1073 struct mbuf *m, *n, *fnrdy;
1074 u_long acc, ccc, mbcnt;
1079 SOCKBUF_LOCK_ASSERT(sb);
1081 acc = ccc = mbcnt = 0;
1084 for (m = sb->sb_mb; m; m = n) {
1086 for (; m; m = m->m_next) {
1087 if (m->m_len == 0) {
1088 printf("sb %p empty mbuf %p\n", sb, m);
1091 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) {
1092 if (m != sb->sb_fnrdy) {
1093 printf("sb %p: fnrdy %p != m %p\n",
1094 sb, sb->sb_fnrdy, m);
1100 if (!(m->m_flags & M_NOTAVAIL)) {
1101 printf("sb %p: fnrdy %p, m %p is avail\n",
1102 sb, sb->sb_fnrdy, m);
1109 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
1110 mbcnt += m->m_ext.ext_size;
1115 * Account for mbufs "detached" by ktls_detach_record() while
1116 * they are decrypted by ktls_decrypt(). tlsdcc gives a count
1117 * of the detached bytes that are included in ccc. The mbufs
1118 * and clusters are not included in the socket buffer
1121 ccc += sb->sb_tlsdcc;
1124 for (m = sb->sb_mtls; m; m = m->m_next) {
1125 if (m->m_nextpkt != NULL) {
1126 printf("sb %p TLS mbuf %p with nextpkt\n", sb, m);
1129 if ((m->m_flags & M_NOTREADY) == 0) {
1130 printf("sb %p TLS mbuf %p ready\n", sb, m);
1136 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
1137 mbcnt += m->m_ext.ext_size;
1140 if (sb->sb_tlscc != tlscc) {
1141 printf("tlscc %ld/%u dcc %u\n", tlscc, sb->sb_tlscc,
1146 if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) {
1147 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n",
1148 acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt);
1150 printf("tlscc %ld/%u dcc %u\n", tlscc, sb->sb_tlscc,
1157 panic("%s from %s:%u", __func__, file, line);
1162 * As above, except the mbuf chain begins a new record.
1165 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
1169 SOCKBUF_LOCK_ASSERT(sb);
1174 kmsan_check_mbuf(m0, "sbappend");
1175 m_clrprotoflags(m0);
1178 * Put the first mbuf on the queue. Note this permits zero length
1182 SBLASTRECORDCHK(sb);
1183 SBLINKRECORD(sb, m0);
1187 if (m && (m0->m_flags & M_EOR)) {
1188 m0->m_flags &= ~M_EOR;
1189 m->m_flags |= M_EOR;
1191 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
1192 sbcompress(sb, m, m0);
1196 * As above, except the mbuf chain begins a new record.
1199 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
1203 sbappendrecord_locked(sb, m0);
1207 /* Helper routine that appends data, control, and address to a sockbuf. */
1209 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
1210 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
1212 struct mbuf *m, *n, *nlast;
1215 kmsan_check_mbuf(m0, "sbappend");
1216 if (control != NULL)
1217 kmsan_check_mbuf(control, "sbappend");
1220 if (asa->sa_len > MLEN)
1223 m = m_get(M_NOWAIT, MT_SONAME);
1226 m->m_len = asa->sa_len;
1227 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
1229 M_ASSERT_NO_SND_TAG(m0);
1230 m_clrprotoflags(m0);
1231 m_tag_delete_chain(m0, NULL);
1233 * Clear some persistent info from pkthdr.
1234 * We don't use m_demote(), because some netgraph consumers
1235 * expect M_PKTHDR presence.
1237 m0->m_pkthdr.rcvif = NULL;
1238 m0->m_pkthdr.flowid = 0;
1239 m0->m_pkthdr.csum_flags = 0;
1240 m0->m_pkthdr.fibnum = 0;
1241 m0->m_pkthdr.rsstype = 0;
1244 ctrl_last->m_next = m0; /* concatenate data to control */
1247 m->m_next = control;
1248 for (n = m; n->m_next != NULL; n = n->m_next)
1252 SBLINKRECORD(sb, m);
1254 sb->sb_mbtail = nlast;
1257 SBLASTRECORDCHK(sb);
1262 * Append address and data, and optionally, control (ancillary) data to the
1263 * receive queue of a socket. If present, m0 must include a packet header
1264 * with total length. Returns 0 if no space in sockbuf or insufficient
1268 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
1269 struct mbuf *m0, struct mbuf *control)
1271 struct mbuf *ctrl_last;
1272 int space = asa->sa_len;
1274 SOCKBUF_LOCK_ASSERT(sb);
1276 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
1277 panic("sbappendaddr_locked");
1279 space += m0->m_pkthdr.len;
1280 space += m_length(control, &ctrl_last);
1282 if (space > sbspace(sb))
1284 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
1288 * Append address and data, and optionally, control (ancillary) data to the
1289 * receive queue of a socket. If present, m0 must include a packet header
1290 * with total length. Returns 0 if insufficient mbufs. Does not validate space
1291 * on the receiving sockbuf.
1294 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
1295 struct mbuf *m0, struct mbuf *control)
1297 struct mbuf *ctrl_last;
1299 SOCKBUF_LOCK_ASSERT(sb);
1301 ctrl_last = (control == NULL) ? NULL : m_last(control);
1302 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
1306 * Append address and data, and optionally, control (ancillary) data to the
1307 * receive queue of a socket. If present, m0 must include a packet header
1308 * with total length. Returns 0 if no space in sockbuf or insufficient
1312 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
1313 struct mbuf *m0, struct mbuf *control)
1318 retval = sbappendaddr_locked(sb, asa, m0, control);
1324 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
1325 struct mbuf *control, int flags)
1327 struct mbuf *m, *mlast;
1329 kmsan_check_mbuf(m0, "sbappend");
1330 kmsan_check_mbuf(control, "sbappend");
1332 sbm_clrprotoflags(m0, flags);
1333 m_last(control)->m_next = m0;
1335 SBLASTRECORDCHK(sb);
1337 for (m = control; m->m_next; m = m->m_next)
1341 SBLINKRECORD(sb, control);
1343 sb->sb_mbtail = mlast;
1346 SBLASTRECORDCHK(sb);
1350 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control,
1355 sbappendcontrol_locked(sb, m0, control, flags);
1360 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
1361 * (n). If (n) is NULL, the buffer is presumed empty.
1363 * When the data is compressed, mbufs in the chain may be handled in one of
1366 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
1367 * record boundary, and no change in data type).
1369 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
1370 * an mbuf already in the socket buffer. This can occur if an
1371 * appropriate mbuf exists, there is room, both mbufs are not marked as
1372 * not ready, and no merging of data types will occur.
1374 * (3) The mbuf may be appended to the end of the existing mbuf chain.
1376 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
1380 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1385 SOCKBUF_LOCK_ASSERT(sb);
1388 eor |= m->m_flags & M_EOR;
1389 if (m->m_len == 0 &&
1391 (((o = m->m_next) || (o = n)) &&
1392 o->m_type == m->m_type))) {
1393 if (sb->sb_lastrecord == m)
1394 sb->sb_lastrecord = m->m_next;
1398 if (n && (n->m_flags & M_EOR) == 0 &&
1400 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1401 !(m->m_flags & M_NOTREADY) &&
1402 !(n->m_flags & (M_NOTREADY | M_EXTPG)) &&
1403 !mbuf_has_tls_session(m) &&
1404 !mbuf_has_tls_session(n) &&
1405 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1406 m->m_len <= M_TRAILINGSPACE(n) &&
1407 n->m_type == m->m_type) {
1408 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len));
1409 n->m_len += m->m_len;
1410 sb->sb_ccc += m->m_len;
1411 if (sb->sb_fnrdy == NULL)
1412 sb->sb_acc += m->m_len;
1413 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1414 /* XXX: Probably don't need.*/
1415 sb->sb_ctl += m->m_len;
1419 if (m->m_len <= MLEN && (m->m_flags & M_EXTPG) &&
1420 (m->m_flags & M_NOTREADY) == 0 &&
1421 !mbuf_has_tls_session(m))
1422 (void)mb_unmapped_compress(m);
1430 m->m_flags &= ~M_EOR;
1435 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1443 * A version of sbcompress() for encrypted TLS RX mbufs. These mbufs
1444 * are appended to the 'sb_mtls' chain instead of 'sb_mb' and are also
1445 * a bit simpler (no EOR markers, always MT_DATA, etc.).
1448 sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1451 SOCKBUF_LOCK_ASSERT(sb);
1454 KASSERT((m->m_flags & M_EOR) == 0,
1455 ("TLS RX mbuf %p with EOR", m));
1456 KASSERT(m->m_type == MT_DATA,
1457 ("TLS RX mbuf %p is not MT_DATA", m));
1458 KASSERT((m->m_flags & M_NOTREADY) != 0,
1459 ("TLS RX mbuf %p ready", m));
1460 KASSERT((m->m_flags & M_EXTPG) == 0,
1461 ("TLS RX mbuf %p unmapped", m));
1463 if (m->m_len == 0) {
1469 * Even though both 'n' and 'm' are NOTREADY, it's ok
1470 * to coalesce the data.
1474 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1475 !((m->m_flags ^ n->m_flags) & M_DECRYPTED) &&
1476 !(n->m_flags & M_EXTPG) &&
1477 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1478 m->m_len <= M_TRAILINGSPACE(n)) {
1479 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len));
1480 n->m_len += m->m_len;
1481 sb->sb_ccc += m->m_len;
1482 sb->sb_tlscc += m->m_len;
1490 sb->sb_mtlstail = m;
1491 sballoc_ktls_rx(sb, m);
1501 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
1504 sbflush_internal(struct sockbuf *sb)
1507 while (sb->sb_mbcnt || sb->sb_tlsdcc) {
1509 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
1510 * we would loop forever. Panic instead.
1512 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1514 m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
1516 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
1517 ("%s: ccc %u mb %p mbcnt %u", __func__,
1518 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1522 sbflush_locked(struct sockbuf *sb)
1525 SOCKBUF_LOCK_ASSERT(sb);
1526 sbflush_internal(sb);
1530 sbflush(struct sockbuf *sb)
1539 * Cut data from (the front of) a sockbuf.
1541 static struct mbuf *
1542 sbcut_internal(struct sockbuf *sb, int len)
1544 struct mbuf *m, *next, *mfree;
1547 KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0",
1549 KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u",
1550 __func__, len, sb->sb_ccc));
1552 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1559 if (next == NULL && !is_tls) {
1560 if (sb->sb_tlsdcc != 0) {
1561 MPASS(len >= sb->sb_tlsdcc);
1562 len -= sb->sb_tlsdcc;
1563 sb->sb_ccc -= sb->sb_tlsdcc;
1572 KASSERT(next, ("%s: no next, len %d", __func__, len));
1574 next = m->m_nextpkt;
1576 if (m->m_len > len) {
1577 KASSERT(!(m->m_flags & M_NOTAVAIL),
1578 ("%s: m %p M_NOTAVAIL", __func__, m));
1583 if (sb->sb_sndptroff != 0)
1584 sb->sb_sndptroff -= len;
1585 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1592 sbfree_ktls_rx(sb, m);
1597 * Do not put M_NOTREADY buffers to the free list, they
1598 * are referenced from outside.
1600 if (m->m_flags & M_NOTREADY && !is_tls)
1612 * Free any zero-length mbufs from the buffer.
1613 * For SOCK_DGRAM sockets such mbufs represent empty records.
1614 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1615 * when sosend_generic() needs to send only control data.
1617 while (m && m->m_len == 0) {
1631 sb->sb_mtlstail = NULL;
1636 m->m_nextpkt = next;
1640 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
1641 * sb_lastrecord is up-to-date if we dropped part of the last record.
1645 sb->sb_mbtail = NULL;
1646 sb->sb_lastrecord = NULL;
1647 } else if (m->m_nextpkt == NULL) {
1648 sb->sb_lastrecord = m;
1655 * Drop data from (the front of) a sockbuf.
1658 sbdrop_locked(struct sockbuf *sb, int len)
1661 SOCKBUF_LOCK_ASSERT(sb);
1662 m_freem(sbcut_internal(sb, len));
1666 * Drop data from (the front of) a sockbuf,
1667 * and return it to caller.
1670 sbcut_locked(struct sockbuf *sb, int len)
1673 SOCKBUF_LOCK_ASSERT(sb);
1674 return (sbcut_internal(sb, len));
1678 sbdrop(struct sockbuf *sb, int len)
1683 mfree = sbcut_internal(sb, len);
1690 sbsndptr_noadv(struct sockbuf *sb, uint32_t off, uint32_t *moff)
1694 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1695 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1697 if (sb->sb_sndptr == NULL) {
1698 sb->sb_sndptr = sb->sb_mb;
1699 sb->sb_sndptroff = 0;
1704 off -= sb->sb_sndptroff;
1711 sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, uint32_t len)
1714 * A small copy was done, advance forward the sb_sbsndptr to cover
1719 if (mb != sb->sb_sndptr) {
1720 /* Did not copyout at the same mbuf */
1724 while (m && (len > 0)) {
1725 if (len >= m->m_len) {
1728 sb->sb_sndptroff += m->m_len;
1729 sb->sb_sndptr = m->m_next;
1739 * Return the first mbuf and the mbuf data offset for the provided
1740 * send offset without changing the "sb_sndptroff" field.
1743 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1747 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1750 * If the "off" is below the stored offset, which happens on
1751 * retransmits, just use "sb_mb":
1753 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1757 off -= sb->sb_sndptroff;
1759 while (off > 0 && m != NULL) {
1770 * Drop a record off the front of a sockbuf and move the next record to the
1774 sbdroprecord_locked(struct sockbuf *sb)
1778 SOCKBUF_LOCK_ASSERT(sb);
1782 sb->sb_mb = m->m_nextpkt;
1792 * Drop a record off the front of a sockbuf and move the next record to the
1796 sbdroprecord(struct sockbuf *sb)
1800 sbdroprecord_locked(sb);
1805 * Create a "control" mbuf containing the specified data with the specified
1806 * type for presentation on a socket buffer.
1809 sbcreatecontrol(const void *p, u_int size, int type, int level, int wait)
1814 MBUF_CHECKSLEEP(wait);
1816 if (wait == M_NOWAIT) {
1817 if (CMSG_SPACE(size) > MCLBYTES)
1820 KASSERT(CMSG_SPACE(size) <= MCLBYTES,
1821 ("%s: passed CMSG_SPACE(%u) > MCLBYTES", __func__, size));
1823 if (CMSG_SPACE(size) > MLEN)
1824 m = m_getcl(wait, MT_CONTROL, 0);
1826 m = m_get(wait, MT_CONTROL);
1830 KASSERT(CMSG_SPACE(size) <= M_TRAILINGSPACE(m),
1831 ("sbcreatecontrol: short mbuf"));
1833 * Don't leave the padding between the msg header and the
1834 * cmsg data and the padding after the cmsg data un-initialized.
1836 cp = mtod(m, struct cmsghdr *);
1837 bzero(cp, CMSG_SPACE(size));
1839 (void)memcpy(CMSG_DATA(cp), p, size);
1840 m->m_len = CMSG_SPACE(size);
1841 cp->cmsg_len = CMSG_LEN(size);
1842 cp->cmsg_level = level;
1843 cp->cmsg_type = type;
1848 * This does the same for socket buffers that sotoxsocket does for sockets:
1849 * generate an user-format data structure describing the socket buffer. Note
1850 * that the xsockbuf structure, since it is always embedded in a socket, does
1851 * not include a self pointer nor a length. We make this entry point public
1852 * in case some other mechanism needs it.
1855 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1858 xsb->sb_cc = sb->sb_ccc;
1859 xsb->sb_hiwat = sb->sb_hiwat;
1860 xsb->sb_mbcnt = sb->sb_mbcnt;
1861 xsb->sb_mbmax = sb->sb_mbmax;
1862 xsb->sb_lowat = sb->sb_lowat;
1863 xsb->sb_flags = sb->sb_flags;
1864 xsb->sb_timeo = sb->sb_timeo;
1867 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1869 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW | CTLFLAG_SKIP, &dummy, 0, "");
1870 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf,
1871 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &sb_max, 0,
1872 sysctl_handle_sb_max, "LU",
1873 "Maximum socket buffer size");
1874 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1875 &sb_efficiency, 0, "Socket buffer size waste factor");