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
31 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_kern_tls.h"
38 #include "opt_param.h"
40 #include <sys/param.h>
41 #include <sys/aio.h> /* for aio_swake proto */
42 #include <sys/kernel.h>
45 #include <sys/malloc.h>
48 #include <sys/mutex.h>
50 #include <sys/protosw.h>
51 #include <sys/resourcevar.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
58 #include <netinet/in.h>
61 * Function pointer set by the AIO routines so that the socket buffer code
62 * can call back into the AIO module if it is loaded.
64 void (*aio_swake)(struct socket *, struct sockbuf *);
67 * Primitive routines for operating on socket buffers
70 #define BUF_MAX_ADJ(_sz) (((u_quad_t)(_sz)) * MCLBYTES / (MSIZE + MCLBYTES))
72 u_long sb_max = SB_MAX;
73 u_long sb_max_adj = BUF_MAX_ADJ(SB_MAX);
75 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
78 static void sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m,
81 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len);
82 static void sbflush_internal(struct sockbuf *sb);
85 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
88 sbm_clrprotoflags(struct mbuf *m, int flags)
93 if (flags & PRUS_NOTREADY)
102 * Compress M_NOTREADY mbufs after they have been readied by sbready().
104 * sbcompress() skips M_NOTREADY mbufs since the data is not available to
105 * be copied at the time of sbcompress(). This function combines small
106 * mbufs similar to sbcompress() once mbufs are ready. 'm0' is the first
107 * mbuf sbready() marked ready, and 'end' is the first mbuf still not
111 sbready_compress(struct sockbuf *sb, struct mbuf *m0, struct mbuf *end)
116 SOCKBUF_LOCK_ASSERT(sb);
118 if ((sb->sb_flags & SB_NOCOALESCE) != 0)
121 for (m = m0; m != end; m = m->m_next) {
122 MPASS((m->m_flags & M_NOTREADY) == 0);
124 * NB: In sbcompress(), 'n' is the last mbuf in the
125 * socket buffer and 'm' is the new mbuf being copied
126 * into the trailing space of 'n'. Here, the roles
127 * are reversed and 'n' is the next mbuf after 'm'
128 * that is being copied into the trailing space of
133 /* Try to coalesce adjacent ktls mbuf hdr/trailers. */
134 if ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 &&
135 (m->m_flags & M_EXTPG) &&
136 (n->m_flags & M_EXTPG) &&
137 !mbuf_has_tls_session(m) &&
138 !mbuf_has_tls_session(n)) {
139 int hdr_len, trail_len;
141 hdr_len = n->m_epg_hdrlen;
142 trail_len = m->m_epg_trllen;
143 if (trail_len != 0 && hdr_len != 0 &&
144 trail_len + hdr_len <= MBUF_PEXT_TRAIL_LEN) {
145 /* copy n's header to m's trailer */
146 memcpy(&m->m_epg_trail[trail_len],
147 n->m_epg_hdr, hdr_len);
148 m->m_epg_trllen += hdr_len;
156 /* Compress small unmapped mbufs into plain mbufs. */
157 if ((m->m_flags & M_EXTPG) && m->m_len <= MLEN &&
158 !mbuf_has_tls_session(m)) {
159 ext_size = m->m_ext.ext_size;
160 if (mb_unmapped_compress(m) == 0)
161 sb->sb_mbcnt -= ext_size;
164 while ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 &&
166 (m->m_flags & M_EXTPG) == 0 &&
167 !mbuf_has_tls_session(n) &&
168 !mbuf_has_tls_session(m) &&
169 n->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
170 n->m_len <= M_TRAILINGSPACE(m) &&
171 m->m_type == n->m_type) {
172 KASSERT(sb->sb_lastrecord != n,
173 ("%s: merging start of record (%p) into previous mbuf (%p)",
175 m_copydata(n, 0, n->m_len, mtodo(m, m->m_len));
176 m->m_len += n->m_len;
177 m->m_next = n->m_next;
178 m->m_flags |= n->m_flags & M_EOR;
179 if (sb->sb_mbtail == n)
182 sb->sb_mbcnt -= MSIZE;
183 if (n->m_flags & M_EXT)
184 sb->sb_mbcnt -= n->m_ext.ext_size;
194 * Mark ready "count" units of I/O starting with "m". Most mbufs
195 * count as a single unit of I/O except for M_EXTPG mbufs which
196 * are backed by multiple pages.
199 sbready(struct sockbuf *sb, struct mbuf *m0, int count)
204 SOCKBUF_LOCK_ASSERT(sb);
205 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
206 KASSERT(count > 0, ("%s: invalid count %d", __func__, count));
209 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
212 KASSERT(m->m_flags & M_NOTREADY,
213 ("%s: m %p !M_NOTREADY", __func__, m));
214 if ((m->m_flags & M_EXTPG) != 0 && m->m_epg_npgs != 0) {
215 if (count < m->m_epg_nrdy) {
216 m->m_epg_nrdy -= count;
220 count -= m->m_epg_nrdy;
225 m->m_flags &= ~(M_NOTREADY | blocker);
227 sb->sb_acc += m->m_len;
232 * If the first mbuf is still not fully ready because only
233 * some of its backing pages were readied, no further progress
237 MPASS(m->m_flags & M_NOTREADY);
238 return (EINPROGRESS);
242 sbready_compress(sb, m0, m);
243 return (EINPROGRESS);
246 /* This one was blocking all the queue. */
247 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
248 KASSERT(m->m_flags & M_BLOCKED,
249 ("%s: m %p !M_BLOCKED", __func__, m));
250 m->m_flags &= ~M_BLOCKED;
251 sb->sb_acc += m->m_len;
255 sbready_compress(sb, m0, m);
261 * Adjust sockbuf state reflecting allocation of m.
264 sballoc(struct sockbuf *sb, struct mbuf *m)
267 SOCKBUF_LOCK_ASSERT(sb);
269 sb->sb_ccc += m->m_len;
271 if (sb->sb_fnrdy == NULL) {
272 if (m->m_flags & M_NOTREADY)
275 sb->sb_acc += m->m_len;
277 m->m_flags |= M_BLOCKED;
279 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
280 sb->sb_ctl += m->m_len;
282 sb->sb_mbcnt += MSIZE;
284 if (m->m_flags & M_EXT)
285 sb->sb_mbcnt += m->m_ext.ext_size;
289 * Adjust sockbuf state reflecting freeing of m.
292 sbfree(struct sockbuf *sb, struct mbuf *m)
295 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
296 SOCKBUF_LOCK_ASSERT(sb);
299 sb->sb_ccc -= m->m_len;
301 if (!(m->m_flags & M_NOTAVAIL))
302 sb->sb_acc -= m->m_len;
304 if (m == sb->sb_fnrdy) {
307 KASSERT(m->m_flags & M_NOTREADY,
308 ("%s: m %p !M_NOTREADY", __func__, m));
311 while (n != NULL && !(n->m_flags & M_NOTREADY)) {
312 n->m_flags &= ~M_BLOCKED;
313 sb->sb_acc += n->m_len;
319 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
320 sb->sb_ctl -= m->m_len;
322 sb->sb_mbcnt -= MSIZE;
323 if (m->m_flags & M_EXT)
324 sb->sb_mbcnt -= m->m_ext.ext_size;
326 if (sb->sb_sndptr == m) {
327 sb->sb_sndptr = NULL;
328 sb->sb_sndptroff = 0;
330 if (sb->sb_sndptroff != 0)
331 sb->sb_sndptroff -= m->m_len;
336 * Similar to sballoc/sbfree but does not adjust state associated with
337 * the sb_mb chain such as sb_fnrdy or sb_sndptr*. Also assumes mbufs
341 sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m)
344 SOCKBUF_LOCK_ASSERT(sb);
346 sb->sb_ccc += m->m_len;
347 sb->sb_tlscc += m->m_len;
349 sb->sb_mbcnt += MSIZE;
351 if (m->m_flags & M_EXT)
352 sb->sb_mbcnt += m->m_ext.ext_size;
356 sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m)
359 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
360 SOCKBUF_LOCK_ASSERT(sb);
363 sb->sb_ccc -= m->m_len;
364 sb->sb_tlscc -= m->m_len;
366 sb->sb_mbcnt -= MSIZE;
368 if (m->m_flags & M_EXT)
369 sb->sb_mbcnt -= m->m_ext.ext_size;
374 * Socantsendmore indicates that no more data will be sent on the socket; it
375 * would normally be applied to a socket when the user informs the system
376 * that no more data is to be sent, by the protocol code (in case
377 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
378 * received, and will normally be applied to the socket by a protocol when it
379 * detects that the peer will send no more data. Data queued for reading in
380 * the socket may yet be read.
383 socantsendmore_locked(struct socket *so)
386 SOCK_SENDBUF_LOCK_ASSERT(so);
388 so->so_snd.sb_state |= SBS_CANTSENDMORE;
389 sowwakeup_locked(so);
390 SOCK_SENDBUF_UNLOCK_ASSERT(so);
394 socantsendmore(struct socket *so)
397 SOCK_SENDBUF_LOCK(so);
398 socantsendmore_locked(so);
399 SOCK_SENDBUF_UNLOCK_ASSERT(so);
403 socantrcvmore_locked(struct socket *so)
406 SOCK_RECVBUF_LOCK_ASSERT(so);
408 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
410 if (so->so_rcv.sb_flags & SB_TLS_RX)
411 ktls_check_rx(&so->so_rcv);
413 sorwakeup_locked(so);
414 SOCK_RECVBUF_UNLOCK_ASSERT(so);
418 socantrcvmore(struct socket *so)
421 SOCK_RECVBUF_LOCK(so);
422 socantrcvmore_locked(so);
423 SOCK_RECVBUF_UNLOCK_ASSERT(so);
427 soroverflow_locked(struct socket *so)
430 SOCK_RECVBUF_LOCK_ASSERT(so);
432 if (so->so_options & SO_RERROR) {
433 so->so_rerror = ENOBUFS;
434 sorwakeup_locked(so);
436 SOCK_RECVBUF_UNLOCK(so);
438 SOCK_RECVBUF_UNLOCK_ASSERT(so);
442 soroverflow(struct socket *so)
445 SOCK_RECVBUF_LOCK(so);
446 soroverflow_locked(so);
447 SOCK_RECVBUF_UNLOCK_ASSERT(so);
451 * Wait for data to arrive at/drain from a socket buffer.
454 sbwait(struct socket *so, sb_which which)
458 SOCK_BUF_LOCK_ASSERT(so, which);
460 sb = sobuf(so, which);
461 sb->sb_flags |= SB_WAIT;
462 return (msleep_sbt(&sb->sb_acc, soeventmtx(so, which),
463 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
464 sb->sb_timeo, 0, 0));
468 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
469 * via SIGIO if the socket has the SS_ASYNC flag set.
471 * Called with the socket buffer lock held; will release the lock by the end
472 * of the function. This allows the caller to acquire the socket buffer lock
473 * while testing for the need for various sorts of wakeup and hold it through
474 * to the point where it's no longer required. We currently hold the lock
475 * through calls out to other subsystems (with the exception of kqueue), and
476 * then release it to avoid lock order issues. It's not clear that's
479 static __always_inline void
480 sowakeup(struct socket *so, const sb_which which)
485 SOCK_BUF_LOCK_ASSERT(so, which);
487 sb = sobuf(so, which);
488 selwakeuppri(sb->sb_sel, PSOCK);
489 if (!SEL_WAITING(sb->sb_sel))
490 sb->sb_flags &= ~SB_SEL;
491 if (sb->sb_flags & SB_WAIT) {
492 sb->sb_flags &= ~SB_WAIT;
495 KNOTE_LOCKED(&sb->sb_sel->si_note, 0);
496 if (sb->sb_upcall != NULL) {
497 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
498 if (ret == SU_ISCONNECTED) {
499 KASSERT(sb == &so->so_rcv,
500 ("SO_SND upcall returned SU_ISCONNECTED"));
501 soupcall_clear(so, SO_RCV);
505 if (sb->sb_flags & SB_AIO)
506 sowakeup_aio(so, which);
507 SOCK_BUF_UNLOCK(so, which);
508 if (ret == SU_ISCONNECTED)
510 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
511 pgsigio(&so->so_sigio, SIGIO, 0);
512 SOCK_BUF_UNLOCK_ASSERT(so, which);
516 * Do we need to notify the other side when I/O is possible?
518 static __always_inline bool
519 sb_notify(const struct sockbuf *sb)
521 return ((sb->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC |
522 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0);
526 sorwakeup_locked(struct socket *so)
528 SOCK_RECVBUF_LOCK_ASSERT(so);
529 if (sb_notify(&so->so_rcv))
530 sowakeup(so, SO_RCV);
532 SOCK_RECVBUF_UNLOCK(so);
536 sowwakeup_locked(struct socket *so)
538 SOCK_SENDBUF_LOCK_ASSERT(so);
539 if (sb_notify(&so->so_snd))
540 sowakeup(so, SO_SND);
542 SOCK_SENDBUF_UNLOCK(so);
546 * Socket buffer (struct sockbuf) utility routines.
548 * Each socket contains two socket buffers: one for sending data and one for
549 * receiving data. Each buffer contains a queue of mbufs, information about
550 * the number of mbufs and amount of data in the queue, and other fields
551 * allowing select() statements and notification on data availability to be
554 * Data stored in a socket buffer is maintained as a list of records. Each
555 * record is a list of mbufs chained together with the m_next field. Records
556 * are chained together with the m_nextpkt field. The upper level routine
557 * soreceive() expects the following conventions to be observed when placing
558 * information in the receive buffer:
560 * 1. If the protocol requires each message be preceded by the sender's name,
561 * then a record containing that name must be present before any
562 * associated data (mbuf's must be of type MT_SONAME).
563 * 2. If the protocol supports the exchange of ``access rights'' (really just
564 * additional data associated with the message), and there are ``rights''
565 * to be received, then a record containing this data should be present
566 * (mbuf's must be of type MT_RIGHTS).
567 * 3. If a name or rights record exists, then it must be followed by a data
568 * record, perhaps of zero length.
570 * Before using a new socket structure it is first necessary to reserve
571 * buffer space to the socket, by calling sbreserve(). This should commit
572 * some of the available buffer space in the system buffer pool for the
573 * socket (currently, it does nothing but enforce limits). The space should
574 * be released by calling sbrelease() when the socket is destroyed.
577 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
579 struct thread *td = curthread;
581 SOCK_SENDBUF_LOCK(so);
582 SOCK_RECVBUF_LOCK(so);
583 if (sbreserve_locked(so, SO_SND, sndcc, td) == 0)
585 if (sbreserve_locked(so, SO_RCV, rcvcc, td) == 0)
587 if (so->so_rcv.sb_lowat == 0)
588 so->so_rcv.sb_lowat = 1;
589 if (so->so_snd.sb_lowat == 0)
590 so->so_snd.sb_lowat = MCLBYTES;
591 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
592 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
593 SOCK_RECVBUF_UNLOCK(so);
594 SOCK_SENDBUF_UNLOCK(so);
597 sbrelease_locked(so, SO_SND);
599 SOCK_RECVBUF_UNLOCK(so);
600 SOCK_SENDBUF_UNLOCK(so);
605 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
608 u_long tmp_sb_max = sb_max;
610 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
611 if (error || !req->newptr)
613 if (tmp_sb_max < MSIZE + MCLBYTES)
616 sb_max_adj = BUF_MAX_ADJ(sb_max);
621 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
622 * become limiting if buffering efficiency is near the normal case.
625 sbreserve_locked_limit(struct socket *so, sb_which which, u_long cc,
626 u_long buf_max, struct thread *td)
628 struct sockbuf *sb = sobuf(so, which);
631 SOCK_BUF_LOCK_ASSERT(so, which);
634 * When a thread is passed, we take into account the thread's socket
635 * buffer size limit. The caller will generally pass curthread, but
636 * in the TCP input path, NULL will be passed to indicate that no
637 * appropriate thread resource limits are available. In that case,
638 * we don't apply a process limit.
640 if (cc > BUF_MAX_ADJ(buf_max))
643 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
645 sbsize_limit = RLIM_INFINITY;
646 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
649 sb->sb_mbmax = min(cc * sb_efficiency, buf_max);
650 if (sb->sb_lowat > sb->sb_hiwat)
651 sb->sb_lowat = sb->sb_hiwat;
656 sbreserve_locked(struct socket *so, sb_which which, u_long cc,
659 return (sbreserve_locked_limit(so, which, cc, sb_max, td));
663 sbsetopt(struct socket *so, struct sockopt *sopt)
668 u_int cc, *hiwat, *lowat;
671 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
676 * Values < 1 make no sense for any of these options,
685 if (SOLISTENING(so)) {
686 switch (sopt->sopt_name) {
689 lowat = &so->sol_sbsnd_lowat;
690 hiwat = &so->sol_sbsnd_hiwat;
691 flags = &so->sol_sbsnd_flags;
695 lowat = &so->sol_sbrcv_lowat;
696 hiwat = &so->sol_sbrcv_hiwat;
697 flags = &so->sol_sbrcv_flags;
701 switch (sopt->sopt_name) {
713 flags = &sb->sb_flags;
714 hiwat = &sb->sb_hiwat;
715 lowat = &sb->sb_lowat;
716 SOCK_BUF_LOCK(so, wh);
720 switch (sopt->sopt_name) {
723 if (SOLISTENING(so)) {
724 if (cc > sb_max_adj) {
732 if (!sbreserve_locked(so, wh, cc, curthread))
736 *flags &= ~SB_AUTOSIZE;
741 * Make sure the low-water is never greater than the
744 *lowat = (cc > *hiwat) ? *hiwat : cc;
748 if (!SOLISTENING(so))
749 SOCK_BUF_UNLOCK(so, wh);
755 * Free mbufs held by a socket, and reserved mbuf space.
758 sbrelease_internal(struct socket *so, sb_which which)
760 struct sockbuf *sb = sobuf(so, which);
762 sbflush_internal(sb);
763 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
769 sbrelease_locked(struct socket *so, sb_which which)
772 SOCK_BUF_LOCK_ASSERT(so, which);
774 sbrelease_internal(so, which);
778 sbrelease(struct socket *so, sb_which which)
781 SOCK_BUF_LOCK(so, which);
782 sbrelease_locked(so, which);
783 SOCK_BUF_UNLOCK(so, which);
787 sbdestroy(struct socket *so, sb_which which)
790 struct sockbuf *sb = sobuf(so, which);
792 if (sb->sb_tls_info != NULL)
793 ktls_free(sb->sb_tls_info);
794 sb->sb_tls_info = NULL;
796 sbrelease_internal(so, which);
800 * Routines to add and remove data from an mbuf queue.
802 * The routines sbappend() or sbappendrecord() are normally called to append
803 * new mbufs to a socket buffer, after checking that adequate space is
804 * available, comparing the function sbspace() with the amount of data to be
805 * added. sbappendrecord() differs from sbappend() in that data supplied is
806 * treated as the beginning of a new record. To place a sender's address,
807 * optional access rights, and data in a socket receive buffer,
808 * sbappendaddr() should be used. To place access rights and data in a
809 * socket receive buffer, sbappendrights() should be used. In either case,
810 * the new data begins a new record. Note that unlike sbappend() and
811 * sbappendrecord(), these routines check for the caller that there will be
812 * enough space to store the data. Each fails if there is not enough space,
813 * or if it cannot find mbufs to store additional information in.
815 * Reliable protocols may use the socket send buffer to hold data awaiting
816 * acknowledgement. Data is normally copied from a socket send buffer in a
817 * protocol with m_copy for output to a peer, and then removing the data from
818 * the socket buffer with sbdrop() or sbdroprecord() when the data is
819 * acknowledged by the peer.
823 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
825 struct mbuf *m = sb->sb_mb;
827 SOCKBUF_LOCK_ASSERT(sb);
829 while (m && m->m_nextpkt)
832 if (m != sb->sb_lastrecord) {
833 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
834 __func__, sb->sb_mb, sb->sb_lastrecord, m);
835 printf("packet chain:\n");
836 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
838 panic("%s from %s:%u", __func__, file, line);
843 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
845 struct mbuf *m = sb->sb_mb;
848 SOCKBUF_LOCK_ASSERT(sb);
850 while (m && m->m_nextpkt)
853 while (m && m->m_next)
856 if (m != sb->sb_mbtail) {
857 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
858 __func__, sb->sb_mb, sb->sb_mbtail, m);
859 printf("packet tree:\n");
860 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
862 for (n = m; n != NULL; n = n->m_next)
866 panic("%s from %s:%u", __func__, file, line);
871 while (m && m->m_next)
874 if (m != sb->sb_mtlstail) {
875 printf("%s: sb_mtls %p sb_mtlstail %p last %p\n",
876 __func__, sb->sb_mtls, sb->sb_mtlstail, m);
877 printf("TLS packet tree:\n");
879 for (m = sb->sb_mtls; m != NULL; m = m->m_next) {
883 panic("%s from %s:%u", __func__, file, line);
887 #endif /* SOCKBUF_DEBUG */
889 #define SBLINKRECORD(sb, m0) do { \
890 SOCKBUF_LOCK_ASSERT(sb); \
891 if ((sb)->sb_lastrecord != NULL) \
892 (sb)->sb_lastrecord->m_nextpkt = (m0); \
894 (sb)->sb_mb = (m0); \
895 (sb)->sb_lastrecord = (m0); \
896 } while (/*CONSTCOND*/0)
899 * Append mbuf chain m to the last record in the socket buffer sb. The
900 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
901 * are discarded and mbufs are compacted where possible.
904 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
908 SOCKBUF_LOCK_ASSERT(sb);
912 kmsan_check_mbuf(m, "sbappend");
913 sbm_clrprotoflags(m, flags);
920 if (n->m_flags & M_EOR) {
921 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
924 } while (n->m_next && (n = n->m_next));
927 * XXX Would like to simply use sb_mbtail here, but
928 * XXX I need to verify that I won't miss an EOR that
931 if ((n = sb->sb_lastrecord) != NULL) {
933 if (n->m_flags & M_EOR) {
934 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
937 } while (n->m_next && (n = n->m_next));
940 * If this is the first record in the socket buffer,
941 * it's also the last record.
943 sb->sb_lastrecord = m;
946 sbcompress(sb, m, n);
951 * Append mbuf chain m to the last record in the socket buffer sb. The
952 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
953 * are discarded and mbufs are compacted where possible.
956 sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
960 sbappend_locked(sb, m, flags);
966 * Append an mbuf containing encrypted TLS data. The data
967 * is marked M_NOTREADY until it has been decrypted and
968 * stored as a TLS record.
971 sbappend_ktls_rx(struct sockbuf *sb, struct mbuf *m)
982 /* Mbuf chain must start with a packet header. */
983 MPASS((m->m_flags & M_PKTHDR) != 0);
985 /* Remove all packet headers and mbuf tags to get a pure data chain. */
986 for (n = m; n != NULL; n = n->m_next) {
987 if (n->m_flags & M_PKTHDR) {
988 ifp = m->m_pkthdr.leaf_rcvif;
989 if ((n->m_pkthdr.csum_flags & CSUM_TLS_MASK) ==
990 CSUM_TLS_DECRYPTED) {
991 /* Mark all mbufs in this packet decrypted. */
992 flags = M_NOTREADY | M_DECRYPTED;
999 n->m_flags &= M_DEMOTEFLAGS;
1000 n->m_flags |= flags;
1002 MPASS((n->m_flags & M_NOTREADY) != 0);
1005 sbcompress_ktls_rx(sb, m, sb->sb_mtlstail);
1008 /* Check for incoming packet route changes: */
1009 if (ifp != NULL && sb->sb_tls_info->rx_ifp != NULL &&
1010 sb->sb_tls_info->rx_ifp != ifp)
1011 ktls_input_ifp_mismatch(sb, ifp);
1016 * This version of sbappend() should only be used when the caller absolutely
1017 * knows that there will never be more than one record in the socket buffer,
1018 * that is, a stream protocol (such as TCP).
1021 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
1023 SOCKBUF_LOCK_ASSERT(sb);
1025 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
1027 kmsan_check_mbuf(m, "sbappend");
1031 * Decrypted TLS records are appended as records via
1032 * sbappendrecord(). TCP passes encrypted TLS records to this
1033 * function which must be scheduled for decryption.
1035 if (sb->sb_flags & SB_TLS_RX) {
1036 sbappend_ktls_rx(sb, m);
1041 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
1046 if (sb->sb_tls_info != NULL)
1050 /* Remove all packet headers and mbuf tags to get a pure data chain. */
1051 m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0);
1053 sbcompress(sb, m, sb->sb_mbtail);
1055 sb->sb_lastrecord = sb->sb_mb;
1056 SBLASTRECORDCHK(sb);
1060 * This version of sbappend() should only be used when the caller absolutely
1061 * knows that there will never be more than one record in the socket buffer,
1062 * that is, a stream protocol (such as TCP).
1065 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags)
1069 sbappendstream_locked(sb, m, flags);
1073 #ifdef SOCKBUF_DEBUG
1075 sbcheck(struct sockbuf *sb, const char *file, int line)
1077 struct mbuf *m, *n, *fnrdy;
1078 u_long acc, ccc, mbcnt;
1083 SOCKBUF_LOCK_ASSERT(sb);
1085 acc = ccc = mbcnt = 0;
1088 for (m = sb->sb_mb; m; m = n) {
1090 for (; m; m = m->m_next) {
1091 if (m->m_len == 0) {
1092 printf("sb %p empty mbuf %p\n", sb, m);
1095 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) {
1096 if (m != sb->sb_fnrdy) {
1097 printf("sb %p: fnrdy %p != m %p\n",
1098 sb, sb->sb_fnrdy, m);
1104 if (!(m->m_flags & M_NOTAVAIL)) {
1105 printf("sb %p: fnrdy %p, m %p is avail\n",
1106 sb, sb->sb_fnrdy, m);
1113 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
1114 mbcnt += m->m_ext.ext_size;
1119 * Account for mbufs "detached" by ktls_detach_record() while
1120 * they are decrypted by ktls_decrypt(). tlsdcc gives a count
1121 * of the detached bytes that are included in ccc. The mbufs
1122 * and clusters are not included in the socket buffer
1125 ccc += sb->sb_tlsdcc;
1128 for (m = sb->sb_mtls; m; m = m->m_next) {
1129 if (m->m_nextpkt != NULL) {
1130 printf("sb %p TLS mbuf %p with nextpkt\n", sb, m);
1133 if ((m->m_flags & M_NOTREADY) == 0) {
1134 printf("sb %p TLS mbuf %p ready\n", sb, m);
1140 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
1141 mbcnt += m->m_ext.ext_size;
1144 if (sb->sb_tlscc != tlscc) {
1145 printf("tlscc %ld/%u dcc %u\n", tlscc, sb->sb_tlscc,
1150 if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) {
1151 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n",
1152 acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt);
1154 printf("tlscc %ld/%u dcc %u\n", tlscc, sb->sb_tlscc,
1161 panic("%s from %s:%u", __func__, file, line);
1166 * As above, except the mbuf chain begins a new record.
1169 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
1173 SOCKBUF_LOCK_ASSERT(sb);
1178 kmsan_check_mbuf(m0, "sbappend");
1179 m_clrprotoflags(m0);
1182 * Put the first mbuf on the queue. Note this permits zero length
1186 SBLASTRECORDCHK(sb);
1187 SBLINKRECORD(sb, m0);
1191 if (m && (m0->m_flags & M_EOR)) {
1192 m0->m_flags &= ~M_EOR;
1193 m->m_flags |= M_EOR;
1195 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
1196 sbcompress(sb, m, m0);
1200 * As above, except the mbuf chain begins a new record.
1203 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
1207 sbappendrecord_locked(sb, m0);
1211 /* Helper routine that appends data, control, and address to a sockbuf. */
1213 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
1214 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
1216 struct mbuf *m, *n, *nlast;
1219 kmsan_check_mbuf(m0, "sbappend");
1220 if (control != NULL)
1221 kmsan_check_mbuf(control, "sbappend");
1224 if (asa->sa_len > MLEN)
1227 m = m_get(M_NOWAIT, MT_SONAME);
1230 m->m_len = asa->sa_len;
1231 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
1233 M_ASSERT_NO_SND_TAG(m0);
1234 m_clrprotoflags(m0);
1235 m_tag_delete_chain(m0, NULL);
1237 * Clear some persistent info from pkthdr.
1238 * We don't use m_demote(), because some netgraph consumers
1239 * expect M_PKTHDR presence.
1241 m0->m_pkthdr.rcvif = NULL;
1242 m0->m_pkthdr.flowid = 0;
1243 m0->m_pkthdr.csum_flags = 0;
1244 m0->m_pkthdr.fibnum = 0;
1245 m0->m_pkthdr.rsstype = 0;
1248 ctrl_last->m_next = m0; /* concatenate data to control */
1251 m->m_next = control;
1252 for (n = m; n->m_next != NULL; n = n->m_next)
1256 SBLINKRECORD(sb, m);
1258 sb->sb_mbtail = nlast;
1261 SBLASTRECORDCHK(sb);
1266 * Append address and data, and optionally, control (ancillary) data to the
1267 * receive queue of a socket. If present, m0 must include a packet header
1268 * with total length. Returns 0 if no space in sockbuf or insufficient
1272 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
1273 struct mbuf *m0, struct mbuf *control)
1275 struct mbuf *ctrl_last;
1276 int space = asa->sa_len;
1278 SOCKBUF_LOCK_ASSERT(sb);
1280 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
1281 panic("sbappendaddr_locked");
1283 space += m0->m_pkthdr.len;
1284 space += m_length(control, &ctrl_last);
1286 if (space > sbspace(sb))
1288 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
1292 * Append address and data, and optionally, control (ancillary) data to the
1293 * receive queue of a socket. If present, m0 must include a packet header
1294 * with total length. Returns 0 if insufficient mbufs. Does not validate space
1295 * on the receiving sockbuf.
1298 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
1299 struct mbuf *m0, struct mbuf *control)
1301 struct mbuf *ctrl_last;
1303 SOCKBUF_LOCK_ASSERT(sb);
1305 ctrl_last = (control == NULL) ? NULL : m_last(control);
1306 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
1310 * Append address and data, and optionally, control (ancillary) data to the
1311 * receive queue of a socket. If present, m0 must include a packet header
1312 * with total length. Returns 0 if no space in sockbuf or insufficient
1316 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
1317 struct mbuf *m0, struct mbuf *control)
1322 retval = sbappendaddr_locked(sb, asa, m0, control);
1328 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
1329 struct mbuf *control, int flags)
1331 struct mbuf *m, *mlast;
1333 kmsan_check_mbuf(m0, "sbappend");
1334 kmsan_check_mbuf(control, "sbappend");
1336 sbm_clrprotoflags(m0, flags);
1337 m_last(control)->m_next = m0;
1339 SBLASTRECORDCHK(sb);
1341 for (m = control; m->m_next; m = m->m_next)
1345 SBLINKRECORD(sb, control);
1347 sb->sb_mbtail = mlast;
1350 SBLASTRECORDCHK(sb);
1354 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control,
1359 sbappendcontrol_locked(sb, m0, control, flags);
1364 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
1365 * (n). If (n) is NULL, the buffer is presumed empty.
1367 * When the data is compressed, mbufs in the chain may be handled in one of
1370 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
1371 * record boundary, and no change in data type).
1373 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
1374 * an mbuf already in the socket buffer. This can occur if an
1375 * appropriate mbuf exists, there is room, both mbufs are not marked as
1376 * not ready, and no merging of data types will occur.
1378 * (3) The mbuf may be appended to the end of the existing mbuf chain.
1380 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
1384 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1389 SOCKBUF_LOCK_ASSERT(sb);
1392 eor |= m->m_flags & M_EOR;
1393 if (m->m_len == 0 &&
1395 (((o = m->m_next) || (o = n)) &&
1396 o->m_type == m->m_type))) {
1397 if (sb->sb_lastrecord == m)
1398 sb->sb_lastrecord = m->m_next;
1402 if (n && (n->m_flags & M_EOR) == 0 &&
1404 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1405 !(m->m_flags & M_NOTREADY) &&
1406 !(n->m_flags & (M_NOTREADY | M_EXTPG)) &&
1407 !mbuf_has_tls_session(m) &&
1408 !mbuf_has_tls_session(n) &&
1409 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1410 m->m_len <= M_TRAILINGSPACE(n) &&
1411 n->m_type == m->m_type) {
1412 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len));
1413 n->m_len += m->m_len;
1414 sb->sb_ccc += m->m_len;
1415 if (sb->sb_fnrdy == NULL)
1416 sb->sb_acc += m->m_len;
1417 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1418 /* XXX: Probably don't need.*/
1419 sb->sb_ctl += m->m_len;
1423 if (m->m_len <= MLEN && (m->m_flags & M_EXTPG) &&
1424 (m->m_flags & M_NOTREADY) == 0 &&
1425 !mbuf_has_tls_session(m))
1426 (void)mb_unmapped_compress(m);
1434 m->m_flags &= ~M_EOR;
1439 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1447 * A version of sbcompress() for encrypted TLS RX mbufs. These mbufs
1448 * are appended to the 'sb_mtls' chain instead of 'sb_mb' and are also
1449 * a bit simpler (no EOR markers, always MT_DATA, etc.).
1452 sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1455 SOCKBUF_LOCK_ASSERT(sb);
1458 KASSERT((m->m_flags & M_EOR) == 0,
1459 ("TLS RX mbuf %p with EOR", m));
1460 KASSERT(m->m_type == MT_DATA,
1461 ("TLS RX mbuf %p is not MT_DATA", m));
1462 KASSERT((m->m_flags & M_NOTREADY) != 0,
1463 ("TLS RX mbuf %p ready", m));
1464 KASSERT((m->m_flags & M_EXTPG) == 0,
1465 ("TLS RX mbuf %p unmapped", m));
1467 if (m->m_len == 0) {
1473 * Even though both 'n' and 'm' are NOTREADY, it's ok
1474 * to coalesce the data.
1478 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1479 !((m->m_flags ^ n->m_flags) & M_DECRYPTED) &&
1480 !(n->m_flags & M_EXTPG) &&
1481 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1482 m->m_len <= M_TRAILINGSPACE(n)) {
1483 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len));
1484 n->m_len += m->m_len;
1485 sb->sb_ccc += m->m_len;
1486 sb->sb_tlscc += m->m_len;
1494 sb->sb_mtlstail = m;
1495 sballoc_ktls_rx(sb, m);
1505 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
1508 sbflush_internal(struct sockbuf *sb)
1511 while (sb->sb_mbcnt || sb->sb_tlsdcc) {
1513 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
1514 * we would loop forever. Panic instead.
1516 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1518 m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
1520 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
1521 ("%s: ccc %u mb %p mbcnt %u", __func__,
1522 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1526 sbflush_locked(struct sockbuf *sb)
1529 SOCKBUF_LOCK_ASSERT(sb);
1530 sbflush_internal(sb);
1534 sbflush(struct sockbuf *sb)
1543 * Cut data from (the front of) a sockbuf.
1545 static struct mbuf *
1546 sbcut_internal(struct sockbuf *sb, int len)
1548 struct mbuf *m, *next, *mfree;
1551 KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0",
1553 KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u",
1554 __func__, len, sb->sb_ccc));
1556 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1563 if (next == NULL && !is_tls) {
1564 if (sb->sb_tlsdcc != 0) {
1565 MPASS(len >= sb->sb_tlsdcc);
1566 len -= sb->sb_tlsdcc;
1567 sb->sb_ccc -= sb->sb_tlsdcc;
1576 KASSERT(next, ("%s: no next, len %d", __func__, len));
1578 next = m->m_nextpkt;
1580 if (m->m_len > len) {
1581 KASSERT(!(m->m_flags & M_NOTAVAIL),
1582 ("%s: m %p M_NOTAVAIL", __func__, m));
1587 if (sb->sb_sndptroff != 0)
1588 sb->sb_sndptroff -= len;
1589 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1596 sbfree_ktls_rx(sb, m);
1601 * Do not put M_NOTREADY buffers to the free list, they
1602 * are referenced from outside.
1604 if (m->m_flags & M_NOTREADY && !is_tls)
1616 * Free any zero-length mbufs from the buffer.
1617 * For SOCK_DGRAM sockets such mbufs represent empty records.
1618 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1619 * when sosend_generic() needs to send only control data.
1621 while (m && m->m_len == 0) {
1635 sb->sb_mtlstail = NULL;
1640 m->m_nextpkt = next;
1644 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
1645 * sb_lastrecord is up-to-date if we dropped part of the last record.
1649 sb->sb_mbtail = NULL;
1650 sb->sb_lastrecord = NULL;
1651 } else if (m->m_nextpkt == NULL) {
1652 sb->sb_lastrecord = m;
1659 * Drop data from (the front of) a sockbuf.
1662 sbdrop_locked(struct sockbuf *sb, int len)
1665 SOCKBUF_LOCK_ASSERT(sb);
1666 m_freem(sbcut_internal(sb, len));
1670 * Drop data from (the front of) a sockbuf,
1671 * and return it to caller.
1674 sbcut_locked(struct sockbuf *sb, int len)
1677 SOCKBUF_LOCK_ASSERT(sb);
1678 return (sbcut_internal(sb, len));
1682 sbdrop(struct sockbuf *sb, int len)
1687 mfree = sbcut_internal(sb, len);
1694 sbsndptr_noadv(struct sockbuf *sb, uint32_t off, uint32_t *moff)
1698 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1699 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1701 if (sb->sb_sndptr == NULL) {
1702 sb->sb_sndptr = sb->sb_mb;
1703 sb->sb_sndptroff = 0;
1708 off -= sb->sb_sndptroff;
1715 sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, uint32_t len)
1718 * A small copy was done, advance forward the sb_sbsndptr to cover
1723 if (mb != sb->sb_sndptr) {
1724 /* Did not copyout at the same mbuf */
1728 while (m && (len > 0)) {
1729 if (len >= m->m_len) {
1732 sb->sb_sndptroff += m->m_len;
1733 sb->sb_sndptr = m->m_next;
1743 * Return the first mbuf and the mbuf data offset for the provided
1744 * send offset without changing the "sb_sndptroff" field.
1747 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1751 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1754 * If the "off" is below the stored offset, which happens on
1755 * retransmits, just use "sb_mb":
1757 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1761 off -= sb->sb_sndptroff;
1763 while (off > 0 && m != NULL) {
1774 * Drop a record off the front of a sockbuf and move the next record to the
1778 sbdroprecord_locked(struct sockbuf *sb)
1782 SOCKBUF_LOCK_ASSERT(sb);
1786 sb->sb_mb = m->m_nextpkt;
1796 * Drop a record off the front of a sockbuf and move the next record to the
1800 sbdroprecord(struct sockbuf *sb)
1804 sbdroprecord_locked(sb);
1809 * Create a "control" mbuf containing the specified data with the specified
1810 * type for presentation on a socket buffer.
1813 sbcreatecontrol(const void *p, u_int size, int type, int level, int wait)
1818 MBUF_CHECKSLEEP(wait);
1820 if (wait == M_NOWAIT) {
1821 if (CMSG_SPACE(size) > MCLBYTES)
1824 KASSERT(CMSG_SPACE(size) <= MCLBYTES,
1825 ("%s: passed CMSG_SPACE(%u) > MCLBYTES", __func__, size));
1827 if (CMSG_SPACE(size) > MLEN)
1828 m = m_getcl(wait, MT_CONTROL, 0);
1830 m = m_get(wait, MT_CONTROL);
1834 KASSERT(CMSG_SPACE(size) <= M_TRAILINGSPACE(m),
1835 ("sbcreatecontrol: short mbuf"));
1837 * Don't leave the padding between the msg header and the
1838 * cmsg data and the padding after the cmsg data un-initialized.
1840 cp = mtod(m, struct cmsghdr *);
1841 bzero(cp, CMSG_SPACE(size));
1843 (void)memcpy(CMSG_DATA(cp), p, size);
1844 m->m_len = CMSG_SPACE(size);
1845 cp->cmsg_len = CMSG_LEN(size);
1846 cp->cmsg_level = level;
1847 cp->cmsg_type = type;
1852 * This does the same for socket buffers that sotoxsocket does for sockets:
1853 * generate an user-format data structure describing the socket buffer. Note
1854 * that the xsockbuf structure, since it is always embedded in a socket, does
1855 * not include a self pointer nor a length. We make this entry point public
1856 * in case some other mechanism needs it.
1859 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1862 xsb->sb_cc = sb->sb_ccc;
1863 xsb->sb_hiwat = sb->sb_hiwat;
1864 xsb->sb_mbcnt = sb->sb_mbcnt;
1865 xsb->sb_mbmax = sb->sb_mbmax;
1866 xsb->sb_lowat = sb->sb_lowat;
1867 xsb->sb_flags = sb->sb_flags;
1868 xsb->sb_timeo = sb->sb_timeo;
1871 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1873 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW | CTLFLAG_SKIP, &dummy, 0, "");
1874 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf,
1875 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &sb_max, 0,
1876 sysctl_handle_sb_max, "LU",
1877 "Maximum socket buffer size");
1878 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1879 &sb_efficiency, 0, "Socket buffer size waste factor");