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>
47 #include <sys/mutex.h>
49 #include <sys/protosw.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
57 #include <netinet/in.h>
60 * Function pointer set by the AIO routines so that the socket buffer code
61 * can call back into the AIO module if it is loaded.
63 void (*aio_swake)(struct socket *, struct sockbuf *);
66 * Primitive routines for operating on socket buffers
69 #define BUF_MAX_ADJ(_sz) (((u_quad_t)(_sz)) * MCLBYTES / (MSIZE + MCLBYTES))
71 u_long sb_max = SB_MAX;
72 u_long sb_max_adj = BUF_MAX_ADJ(SB_MAX);
74 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
77 static void sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m,
80 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len);
81 static void sbflush_internal(struct sockbuf *sb);
84 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
87 sbm_clrprotoflags(struct mbuf *m, int flags)
92 if (flags & PRUS_NOTREADY)
101 * Compress M_NOTREADY mbufs after they have been readied by sbready().
103 * sbcompress() skips M_NOTREADY mbufs since the data is not available to
104 * be copied at the time of sbcompress(). This function combines small
105 * mbufs similar to sbcompress() once mbufs are ready. 'm0' is the first
106 * mbuf sbready() marked ready, and 'end' is the first mbuf still not
110 sbready_compress(struct sockbuf *sb, struct mbuf *m0, struct mbuf *end)
115 SOCKBUF_LOCK_ASSERT(sb);
117 if ((sb->sb_flags & SB_NOCOALESCE) != 0)
120 for (m = m0; m != end; m = m->m_next) {
121 MPASS((m->m_flags & M_NOTREADY) == 0);
123 * NB: In sbcompress(), 'n' is the last mbuf in the
124 * socket buffer and 'm' is the new mbuf being copied
125 * into the trailing space of 'n'. Here, the roles
126 * are reversed and 'n' is the next mbuf after 'm'
127 * that is being copied into the trailing space of
132 /* Try to coalesce adjacent ktls mbuf hdr/trailers. */
133 if ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 &&
134 (m->m_flags & M_EXTPG) &&
135 (n->m_flags & M_EXTPG) &&
136 !mbuf_has_tls_session(m) &&
137 !mbuf_has_tls_session(n)) {
138 int hdr_len, trail_len;
140 hdr_len = n->m_epg_hdrlen;
141 trail_len = m->m_epg_trllen;
142 if (trail_len != 0 && hdr_len != 0 &&
143 trail_len + hdr_len <= MBUF_PEXT_TRAIL_LEN) {
144 /* copy n's header to m's trailer */
145 memcpy(&m->m_epg_trail[trail_len],
146 n->m_epg_hdr, hdr_len);
147 m->m_epg_trllen += hdr_len;
155 /* Compress small unmapped mbufs into plain mbufs. */
156 if ((m->m_flags & M_EXTPG) && m->m_len <= MLEN &&
157 !mbuf_has_tls_session(m)) {
158 ext_size = m->m_ext.ext_size;
159 if (mb_unmapped_compress(m) == 0)
160 sb->sb_mbcnt -= ext_size;
163 while ((n != NULL) && (n != end) && (m->m_flags & M_EOR) == 0 &&
165 (m->m_flags & M_EXTPG) == 0 &&
166 !mbuf_has_tls_session(n) &&
167 !mbuf_has_tls_session(m) &&
168 n->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
169 n->m_len <= M_TRAILINGSPACE(m) &&
170 m->m_type == n->m_type) {
171 KASSERT(sb->sb_lastrecord != n,
172 ("%s: merging start of record (%p) into previous mbuf (%p)",
174 m_copydata(n, 0, n->m_len, mtodo(m, m->m_len));
175 m->m_len += n->m_len;
176 m->m_next = n->m_next;
177 m->m_flags |= n->m_flags & M_EOR;
178 if (sb->sb_mbtail == n)
181 sb->sb_mbcnt -= MSIZE;
182 if (n->m_flags & M_EXT)
183 sb->sb_mbcnt -= n->m_ext.ext_size;
193 * Mark ready "count" units of I/O starting with "m". Most mbufs
194 * count as a single unit of I/O except for M_EXTPG mbufs which
195 * are backed by multiple pages.
198 sbready(struct sockbuf *sb, struct mbuf *m0, int count)
203 SOCKBUF_LOCK_ASSERT(sb);
204 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
205 KASSERT(count > 0, ("%s: invalid count %d", __func__, count));
208 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
211 KASSERT(m->m_flags & M_NOTREADY,
212 ("%s: m %p !M_NOTREADY", __func__, m));
213 if ((m->m_flags & M_EXTPG) != 0 && m->m_epg_npgs != 0) {
214 if (count < m->m_epg_nrdy) {
215 m->m_epg_nrdy -= count;
219 count -= m->m_epg_nrdy;
224 m->m_flags &= ~(M_NOTREADY | blocker);
226 sb->sb_acc += m->m_len;
231 * If the first mbuf is still not fully ready because only
232 * some of its backing pages were readied, no further progress
236 MPASS(m->m_flags & M_NOTREADY);
237 return (EINPROGRESS);
241 sbready_compress(sb, m0, m);
242 return (EINPROGRESS);
245 /* This one was blocking all the queue. */
246 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
247 KASSERT(m->m_flags & M_BLOCKED,
248 ("%s: m %p !M_BLOCKED", __func__, m));
249 m->m_flags &= ~M_BLOCKED;
250 sb->sb_acc += m->m_len;
254 sbready_compress(sb, m0, m);
260 * Adjust sockbuf state reflecting allocation of m.
263 sballoc(struct sockbuf *sb, struct mbuf *m)
266 SOCKBUF_LOCK_ASSERT(sb);
268 sb->sb_ccc += m->m_len;
270 if (sb->sb_fnrdy == NULL) {
271 if (m->m_flags & M_NOTREADY)
274 sb->sb_acc += m->m_len;
276 m->m_flags |= M_BLOCKED;
278 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
279 sb->sb_ctl += m->m_len;
281 sb->sb_mbcnt += MSIZE;
283 if (m->m_flags & M_EXT)
284 sb->sb_mbcnt += m->m_ext.ext_size;
288 * Adjust sockbuf state reflecting freeing of m.
291 sbfree(struct sockbuf *sb, struct mbuf *m)
294 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
295 SOCKBUF_LOCK_ASSERT(sb);
298 sb->sb_ccc -= m->m_len;
300 if (!(m->m_flags & M_NOTAVAIL))
301 sb->sb_acc -= m->m_len;
303 if (m == sb->sb_fnrdy) {
306 KASSERT(m->m_flags & M_NOTREADY,
307 ("%s: m %p !M_NOTREADY", __func__, m));
310 while (n != NULL && !(n->m_flags & M_NOTREADY)) {
311 n->m_flags &= ~M_BLOCKED;
312 sb->sb_acc += n->m_len;
318 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
319 sb->sb_ctl -= m->m_len;
321 sb->sb_mbcnt -= MSIZE;
322 if (m->m_flags & M_EXT)
323 sb->sb_mbcnt -= m->m_ext.ext_size;
325 if (sb->sb_sndptr == m) {
326 sb->sb_sndptr = NULL;
327 sb->sb_sndptroff = 0;
329 if (sb->sb_sndptroff != 0)
330 sb->sb_sndptroff -= m->m_len;
335 * Similar to sballoc/sbfree but does not adjust state associated with
336 * the sb_mb chain such as sb_fnrdy or sb_sndptr*. Also assumes mbufs
340 sballoc_ktls_rx(struct sockbuf *sb, struct mbuf *m)
343 SOCKBUF_LOCK_ASSERT(sb);
345 sb->sb_ccc += m->m_len;
346 sb->sb_tlscc += m->m_len;
348 sb->sb_mbcnt += MSIZE;
350 if (m->m_flags & M_EXT)
351 sb->sb_mbcnt += m->m_ext.ext_size;
355 sbfree_ktls_rx(struct sockbuf *sb, struct mbuf *m)
358 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
359 SOCKBUF_LOCK_ASSERT(sb);
362 sb->sb_ccc -= m->m_len;
363 sb->sb_tlscc -= m->m_len;
365 sb->sb_mbcnt -= MSIZE;
367 if (m->m_flags & M_EXT)
368 sb->sb_mbcnt -= m->m_ext.ext_size;
373 * Socantsendmore indicates that no more data will be sent on the socket; it
374 * would normally be applied to a socket when the user informs the system
375 * that no more data is to be sent, by the protocol code (in case
376 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
377 * received, and will normally be applied to the socket by a protocol when it
378 * detects that the peer will send no more data. Data queued for reading in
379 * the socket may yet be read.
382 socantsendmore_locked(struct socket *so)
385 SOCK_SENDBUF_LOCK_ASSERT(so);
387 so->so_snd.sb_state |= SBS_CANTSENDMORE;
388 sowwakeup_locked(so);
389 SOCK_SENDBUF_UNLOCK_ASSERT(so);
393 socantsendmore(struct socket *so)
396 SOCK_SENDBUF_LOCK(so);
397 socantsendmore_locked(so);
398 SOCK_SENDBUF_UNLOCK_ASSERT(so);
402 socantrcvmore_locked(struct socket *so)
405 SOCK_RECVBUF_LOCK_ASSERT(so);
407 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
409 if (so->so_rcv.sb_flags & SB_TLS_RX)
410 ktls_check_rx(&so->so_rcv);
412 sorwakeup_locked(so);
413 SOCK_RECVBUF_UNLOCK_ASSERT(so);
417 socantrcvmore(struct socket *so)
420 SOCK_RECVBUF_LOCK(so);
421 socantrcvmore_locked(so);
422 SOCK_RECVBUF_UNLOCK_ASSERT(so);
426 soroverflow_locked(struct socket *so)
429 SOCK_RECVBUF_LOCK_ASSERT(so);
431 if (so->so_options & SO_RERROR) {
432 so->so_rerror = ENOBUFS;
433 sorwakeup_locked(so);
435 SOCK_RECVBUF_UNLOCK(so);
437 SOCK_RECVBUF_UNLOCK_ASSERT(so);
441 soroverflow(struct socket *so)
444 SOCK_RECVBUF_LOCK(so);
445 soroverflow_locked(so);
446 SOCK_RECVBUF_UNLOCK_ASSERT(so);
450 * Wait for data to arrive at/drain from a socket buffer.
453 sbwait(struct socket *so, sb_which which)
457 SOCK_BUF_LOCK_ASSERT(so, which);
459 sb = sobuf(so, which);
460 sb->sb_flags |= SB_WAIT;
461 return (msleep_sbt(&sb->sb_acc, soeventmtx(so, which),
462 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
463 sb->sb_timeo, 0, 0));
467 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
468 * via SIGIO if the socket has the SS_ASYNC flag set.
470 * Called with the socket buffer lock held; will release the lock by the end
471 * of the function. This allows the caller to acquire the socket buffer lock
472 * while testing for the need for various sorts of wakeup and hold it through
473 * to the point where it's no longer required. We currently hold the lock
474 * through calls out to other subsystems (with the exception of kqueue), and
475 * then release it to avoid lock order issues. It's not clear that's
478 static __always_inline void
479 sowakeup(struct socket *so, const sb_which which)
484 SOCK_BUF_LOCK_ASSERT(so, which);
486 sb = sobuf(so, which);
487 selwakeuppri(sb->sb_sel, PSOCK);
488 if (!SEL_WAITING(sb->sb_sel))
489 sb->sb_flags &= ~SB_SEL;
490 if (sb->sb_flags & SB_WAIT) {
491 sb->sb_flags &= ~SB_WAIT;
494 KNOTE_LOCKED(&sb->sb_sel->si_note, 0);
495 if (sb->sb_upcall != NULL) {
496 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
497 if (ret == SU_ISCONNECTED) {
498 KASSERT(sb == &so->so_rcv,
499 ("SO_SND upcall returned SU_ISCONNECTED"));
500 soupcall_clear(so, SO_RCV);
504 if (sb->sb_flags & SB_AIO)
505 sowakeup_aio(so, which);
506 SOCK_BUF_UNLOCK(so, which);
507 if (ret == SU_ISCONNECTED)
509 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
510 pgsigio(&so->so_sigio, SIGIO, 0);
511 SOCK_BUF_UNLOCK_ASSERT(so, which);
515 * Do we need to notify the other side when I/O is possible?
517 static __always_inline bool
518 sb_notify(const struct sockbuf *sb)
520 return ((sb->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC |
521 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0);
525 sorwakeup_locked(struct socket *so)
527 SOCK_RECVBUF_LOCK_ASSERT(so);
528 if (sb_notify(&so->so_rcv))
529 sowakeup(so, SO_RCV);
531 SOCK_RECVBUF_UNLOCK(so);
535 sowwakeup_locked(struct socket *so)
537 SOCK_SENDBUF_LOCK_ASSERT(so);
538 if (sb_notify(&so->so_snd))
539 sowakeup(so, SO_SND);
541 SOCK_SENDBUF_UNLOCK(so);
545 * Socket buffer (struct sockbuf) utility routines.
547 * Each socket contains two socket buffers: one for sending data and one for
548 * receiving data. Each buffer contains a queue of mbufs, information about
549 * the number of mbufs and amount of data in the queue, and other fields
550 * allowing select() statements and notification on data availability to be
553 * Data stored in a socket buffer is maintained as a list of records. Each
554 * record is a list of mbufs chained together with the m_next field. Records
555 * are chained together with the m_nextpkt field. The upper level routine
556 * soreceive() expects the following conventions to be observed when placing
557 * information in the receive buffer:
559 * 1. If the protocol requires each message be preceded by the sender's name,
560 * then a record containing that name must be present before any
561 * associated data (mbuf's must be of type MT_SONAME).
562 * 2. If the protocol supports the exchange of ``access rights'' (really just
563 * additional data associated with the message), and there are ``rights''
564 * to be received, then a record containing this data should be present
565 * (mbuf's must be of type MT_RIGHTS).
566 * 3. If a name or rights record exists, then it must be followed by a data
567 * record, perhaps of zero length.
569 * Before using a new socket structure it is first necessary to reserve
570 * buffer space to the socket, by calling sbreserve(). This should commit
571 * some of the available buffer space in the system buffer pool for the
572 * socket (currently, it does nothing but enforce limits). The space should
573 * be released by calling sbrelease() when the socket is destroyed.
576 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
578 struct thread *td = curthread;
580 SOCK_SENDBUF_LOCK(so);
581 SOCK_RECVBUF_LOCK(so);
582 if (sbreserve_locked(so, SO_SND, sndcc, td) == 0)
584 if (sbreserve_locked(so, SO_RCV, rcvcc, td) == 0)
586 if (so->so_rcv.sb_lowat == 0)
587 so->so_rcv.sb_lowat = 1;
588 if (so->so_snd.sb_lowat == 0)
589 so->so_snd.sb_lowat = MCLBYTES;
590 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
591 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
592 SOCK_RECVBUF_UNLOCK(so);
593 SOCK_SENDBUF_UNLOCK(so);
596 sbrelease_locked(so, SO_SND);
598 SOCK_RECVBUF_UNLOCK(so);
599 SOCK_SENDBUF_UNLOCK(so);
604 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
607 u_long tmp_sb_max = sb_max;
609 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
610 if (error || !req->newptr)
612 if (tmp_sb_max < MSIZE + MCLBYTES)
615 sb_max_adj = BUF_MAX_ADJ(sb_max);
620 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
621 * become limiting if buffering efficiency is near the normal case.
624 sbreserve_locked_limit(struct socket *so, sb_which which, u_long cc,
625 u_long buf_max, struct thread *td)
627 struct sockbuf *sb = sobuf(so, which);
630 SOCK_BUF_LOCK_ASSERT(so, which);
633 * When a thread is passed, we take into account the thread's socket
634 * buffer size limit. The caller will generally pass curthread, but
635 * in the TCP input path, NULL will be passed to indicate that no
636 * appropriate thread resource limits are available. In that case,
637 * we don't apply a process limit.
639 if (cc > BUF_MAX_ADJ(buf_max))
642 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
644 sbsize_limit = RLIM_INFINITY;
645 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
648 sb->sb_mbmax = min(cc * sb_efficiency, buf_max);
649 if (sb->sb_lowat > sb->sb_hiwat)
650 sb->sb_lowat = sb->sb_hiwat;
655 sbreserve_locked(struct socket *so, sb_which which, u_long cc,
658 return (sbreserve_locked_limit(so, which, cc, sb_max, td));
662 sbsetopt(struct socket *so, struct sockopt *sopt)
667 u_int cc, *hiwat, *lowat;
670 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
675 * Values < 1 make no sense for any of these options,
684 if (SOLISTENING(so)) {
685 switch (sopt->sopt_name) {
688 lowat = &so->sol_sbsnd_lowat;
689 hiwat = &so->sol_sbsnd_hiwat;
690 flags = &so->sol_sbsnd_flags;
694 lowat = &so->sol_sbrcv_lowat;
695 hiwat = &so->sol_sbrcv_hiwat;
696 flags = &so->sol_sbrcv_flags;
700 switch (sopt->sopt_name) {
712 flags = &sb->sb_flags;
713 hiwat = &sb->sb_hiwat;
714 lowat = &sb->sb_lowat;
715 SOCK_BUF_LOCK(so, wh);
719 switch (sopt->sopt_name) {
722 if (SOLISTENING(so)) {
723 if (cc > sb_max_adj) {
731 if (!sbreserve_locked(so, wh, cc, curthread))
735 *flags &= ~SB_AUTOSIZE;
740 * Make sure the low-water is never greater than the
743 *lowat = (cc > *hiwat) ? *hiwat : cc;
747 if (!SOLISTENING(so))
748 SOCK_BUF_UNLOCK(so, wh);
754 * Free mbufs held by a socket, and reserved mbuf space.
757 sbrelease_internal(struct socket *so, sb_which which)
759 struct sockbuf *sb = sobuf(so, which);
761 sbflush_internal(sb);
762 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
768 sbrelease_locked(struct socket *so, sb_which which)
771 SOCK_BUF_LOCK_ASSERT(so, which);
773 sbrelease_internal(so, which);
777 sbrelease(struct socket *so, sb_which which)
780 SOCK_BUF_LOCK(so, which);
781 sbrelease_locked(so, which);
782 SOCK_BUF_UNLOCK(so, which);
786 sbdestroy(struct socket *so, sb_which which)
789 struct sockbuf *sb = sobuf(so, which);
791 if (sb->sb_tls_info != NULL)
792 ktls_free(sb->sb_tls_info);
793 sb->sb_tls_info = NULL;
795 sbrelease_internal(so, which);
799 * Routines to add and remove data from an mbuf queue.
801 * The routines sbappend() or sbappendrecord() are normally called to append
802 * new mbufs to a socket buffer, after checking that adequate space is
803 * available, comparing the function sbspace() with the amount of data to be
804 * added. sbappendrecord() differs from sbappend() in that data supplied is
805 * treated as the beginning of a new record. To place a sender's address,
806 * optional access rights, and data in a socket receive buffer,
807 * sbappendaddr() should be used. To place access rights and data in a
808 * socket receive buffer, sbappendrights() should be used. In either case,
809 * the new data begins a new record. Note that unlike sbappend() and
810 * sbappendrecord(), these routines check for the caller that there will be
811 * enough space to store the data. Each fails if there is not enough space,
812 * or if it cannot find mbufs to store additional information in.
814 * Reliable protocols may use the socket send buffer to hold data awaiting
815 * acknowledgement. Data is normally copied from a socket send buffer in a
816 * protocol with m_copy for output to a peer, and then removing the data from
817 * the socket buffer with sbdrop() or sbdroprecord() when the data is
818 * acknowledged by the peer.
822 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
824 struct mbuf *m = sb->sb_mb;
826 SOCKBUF_LOCK_ASSERT(sb);
828 while (m && m->m_nextpkt)
831 if (m != sb->sb_lastrecord) {
832 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
833 __func__, sb->sb_mb, sb->sb_lastrecord, m);
834 printf("packet chain:\n");
835 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
837 panic("%s from %s:%u", __func__, file, line);
842 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
844 struct mbuf *m = sb->sb_mb;
847 SOCKBUF_LOCK_ASSERT(sb);
849 while (m && m->m_nextpkt)
852 while (m && m->m_next)
855 if (m != sb->sb_mbtail) {
856 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
857 __func__, sb->sb_mb, sb->sb_mbtail, m);
858 printf("packet tree:\n");
859 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
861 for (n = m; n != NULL; n = n->m_next)
865 panic("%s from %s:%u", __func__, file, line);
870 while (m && m->m_next)
873 if (m != sb->sb_mtlstail) {
874 printf("%s: sb_mtls %p sb_mtlstail %p last %p\n",
875 __func__, sb->sb_mtls, sb->sb_mtlstail, m);
876 printf("TLS packet tree:\n");
878 for (m = sb->sb_mtls; m != NULL; m = m->m_next) {
882 panic("%s from %s:%u", __func__, file, line);
886 #endif /* SOCKBUF_DEBUG */
888 #define SBLINKRECORD(sb, m0) do { \
889 SOCKBUF_LOCK_ASSERT(sb); \
890 if ((sb)->sb_lastrecord != NULL) \
891 (sb)->sb_lastrecord->m_nextpkt = (m0); \
893 (sb)->sb_mb = (m0); \
894 (sb)->sb_lastrecord = (m0); \
895 } while (/*CONSTCOND*/0)
898 * Append mbuf chain m to the last record in the socket buffer sb. The
899 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
900 * are discarded and mbufs are compacted where possible.
903 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
907 SOCKBUF_LOCK_ASSERT(sb);
911 sbm_clrprotoflags(m, flags);
918 if (n->m_flags & M_EOR) {
919 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
922 } while (n->m_next && (n = n->m_next));
925 * XXX Would like to simply use sb_mbtail here, but
926 * XXX I need to verify that I won't miss an EOR that
929 if ((n = sb->sb_lastrecord) != NULL) {
931 if (n->m_flags & M_EOR) {
932 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
935 } while (n->m_next && (n = n->m_next));
938 * If this is the first record in the socket buffer,
939 * it's also the last record.
941 sb->sb_lastrecord = m;
944 sbcompress(sb, m, n);
949 * Append mbuf chain m to the last record in the socket buffer sb. The
950 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
951 * are discarded and mbufs are compacted where possible.
954 sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
958 sbappend_locked(sb, m, flags);
964 * Append an mbuf containing encrypted TLS data. The data
965 * is marked M_NOTREADY until it has been decrypted and
966 * stored as a TLS record.
969 sbappend_ktls_rx(struct sockbuf *sb, struct mbuf *m)
980 /* Mbuf chain must start with a packet header. */
981 MPASS((m->m_flags & M_PKTHDR) != 0);
983 /* Remove all packet headers and mbuf tags to get a pure data chain. */
984 for (n = m; n != NULL; n = n->m_next) {
985 if (n->m_flags & M_PKTHDR) {
986 ifp = m->m_pkthdr.leaf_rcvif;
987 if ((n->m_pkthdr.csum_flags & CSUM_TLS_MASK) ==
988 CSUM_TLS_DECRYPTED) {
989 /* Mark all mbufs in this packet decrypted. */
990 flags = M_NOTREADY | M_DECRYPTED;
997 n->m_flags &= M_DEMOTEFLAGS;
1000 MPASS((n->m_flags & M_NOTREADY) != 0);
1003 sbcompress_ktls_rx(sb, m, sb->sb_mtlstail);
1006 /* Check for incoming packet route changes: */
1007 if (ifp != NULL && sb->sb_tls_info->rx_ifp != NULL &&
1008 sb->sb_tls_info->rx_ifp != ifp)
1009 ktls_input_ifp_mismatch(sb, ifp);
1014 * This version of sbappend() should only be used when the caller absolutely
1015 * knows that there will never be more than one record in the socket buffer,
1016 * that is, a stream protocol (such as TCP).
1019 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
1021 SOCKBUF_LOCK_ASSERT(sb);
1023 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
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);
1173 m_clrprotoflags(m0);
1175 * Put the first mbuf on the queue. Note this permits zero length
1179 SBLASTRECORDCHK(sb);
1180 SBLINKRECORD(sb, m0);
1184 if (m && (m0->m_flags & M_EOR)) {
1185 m0->m_flags &= ~M_EOR;
1186 m->m_flags |= M_EOR;
1188 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
1189 sbcompress(sb, m, m0);
1193 * As above, except the mbuf chain begins a new record.
1196 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
1200 sbappendrecord_locked(sb, m0);
1204 /* Helper routine that appends data, control, and address to a sockbuf. */
1206 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
1207 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
1209 struct mbuf *m, *n, *nlast;
1211 if (asa->sa_len > MLEN)
1214 m = m_get(M_NOWAIT, MT_SONAME);
1217 m->m_len = asa->sa_len;
1218 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
1220 M_ASSERT_NO_SND_TAG(m0);
1221 m_clrprotoflags(m0);
1222 m_tag_delete_chain(m0, NULL);
1224 * Clear some persistent info from pkthdr.
1225 * We don't use m_demote(), because some netgraph consumers
1226 * expect M_PKTHDR presence.
1228 m0->m_pkthdr.rcvif = NULL;
1229 m0->m_pkthdr.flowid = 0;
1230 m0->m_pkthdr.csum_flags = 0;
1231 m0->m_pkthdr.fibnum = 0;
1232 m0->m_pkthdr.rsstype = 0;
1235 ctrl_last->m_next = m0; /* concatenate data to control */
1238 m->m_next = control;
1239 for (n = m; n->m_next != NULL; n = n->m_next)
1243 SBLINKRECORD(sb, m);
1245 sb->sb_mbtail = nlast;
1248 SBLASTRECORDCHK(sb);
1253 * Append address and data, and optionally, control (ancillary) data to the
1254 * receive queue of a socket. If present, m0 must include a packet header
1255 * with total length. Returns 0 if no space in sockbuf or insufficient
1259 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
1260 struct mbuf *m0, struct mbuf *control)
1262 struct mbuf *ctrl_last;
1263 int space = asa->sa_len;
1265 SOCKBUF_LOCK_ASSERT(sb);
1267 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
1268 panic("sbappendaddr_locked");
1270 space += m0->m_pkthdr.len;
1271 space += m_length(control, &ctrl_last);
1273 if (space > sbspace(sb))
1275 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
1279 * Append address and data, and optionally, control (ancillary) data to the
1280 * receive queue of a socket. If present, m0 must include a packet header
1281 * with total length. Returns 0 if insufficient mbufs. Does not validate space
1282 * on the receiving sockbuf.
1285 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
1286 struct mbuf *m0, struct mbuf *control)
1288 struct mbuf *ctrl_last;
1290 SOCKBUF_LOCK_ASSERT(sb);
1292 ctrl_last = (control == NULL) ? NULL : m_last(control);
1293 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
1297 * Append address and data, and optionally, control (ancillary) data to the
1298 * receive queue of a socket. If present, m0 must include a packet header
1299 * with total length. Returns 0 if no space in sockbuf or insufficient
1303 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
1304 struct mbuf *m0, struct mbuf *control)
1309 retval = sbappendaddr_locked(sb, asa, m0, control);
1315 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
1316 struct mbuf *control, int flags)
1318 struct mbuf *m, *mlast;
1320 sbm_clrprotoflags(m0, flags);
1321 m_last(control)->m_next = m0;
1323 SBLASTRECORDCHK(sb);
1325 for (m = control; m->m_next; m = m->m_next)
1329 SBLINKRECORD(sb, control);
1331 sb->sb_mbtail = mlast;
1334 SBLASTRECORDCHK(sb);
1338 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control,
1343 sbappendcontrol_locked(sb, m0, control, flags);
1348 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
1349 * (n). If (n) is NULL, the buffer is presumed empty.
1351 * When the data is compressed, mbufs in the chain may be handled in one of
1354 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
1355 * record boundary, and no change in data type).
1357 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
1358 * an mbuf already in the socket buffer. This can occur if an
1359 * appropriate mbuf exists, there is room, both mbufs are not marked as
1360 * not ready, and no merging of data types will occur.
1362 * (3) The mbuf may be appended to the end of the existing mbuf chain.
1364 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
1368 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1373 SOCKBUF_LOCK_ASSERT(sb);
1376 eor |= m->m_flags & M_EOR;
1377 if (m->m_len == 0 &&
1379 (((o = m->m_next) || (o = n)) &&
1380 o->m_type == m->m_type))) {
1381 if (sb->sb_lastrecord == m)
1382 sb->sb_lastrecord = m->m_next;
1386 if (n && (n->m_flags & M_EOR) == 0 &&
1388 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1389 !(m->m_flags & M_NOTREADY) &&
1390 !(n->m_flags & (M_NOTREADY | M_EXTPG)) &&
1391 !mbuf_has_tls_session(m) &&
1392 !mbuf_has_tls_session(n) &&
1393 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1394 m->m_len <= M_TRAILINGSPACE(n) &&
1395 n->m_type == m->m_type) {
1396 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len));
1397 n->m_len += m->m_len;
1398 sb->sb_ccc += m->m_len;
1399 if (sb->sb_fnrdy == NULL)
1400 sb->sb_acc += m->m_len;
1401 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1402 /* XXX: Probably don't need.*/
1403 sb->sb_ctl += m->m_len;
1407 if (m->m_len <= MLEN && (m->m_flags & M_EXTPG) &&
1408 (m->m_flags & M_NOTREADY) == 0 &&
1409 !mbuf_has_tls_session(m))
1410 (void)mb_unmapped_compress(m);
1418 m->m_flags &= ~M_EOR;
1423 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1431 * A version of sbcompress() for encrypted TLS RX mbufs. These mbufs
1432 * are appended to the 'sb_mtls' chain instead of 'sb_mb' and are also
1433 * a bit simpler (no EOR markers, always MT_DATA, etc.).
1436 sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1439 SOCKBUF_LOCK_ASSERT(sb);
1442 KASSERT((m->m_flags & M_EOR) == 0,
1443 ("TLS RX mbuf %p with EOR", m));
1444 KASSERT(m->m_type == MT_DATA,
1445 ("TLS RX mbuf %p is not MT_DATA", m));
1446 KASSERT((m->m_flags & M_NOTREADY) != 0,
1447 ("TLS RX mbuf %p ready", m));
1448 KASSERT((m->m_flags & M_EXTPG) == 0,
1449 ("TLS RX mbuf %p unmapped", m));
1451 if (m->m_len == 0) {
1457 * Even though both 'n' and 'm' are NOTREADY, it's ok
1458 * to coalesce the data.
1462 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
1463 !((m->m_flags ^ n->m_flags) & M_DECRYPTED) &&
1464 !(n->m_flags & M_EXTPG) &&
1465 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1466 m->m_len <= M_TRAILINGSPACE(n)) {
1467 m_copydata(m, 0, m->m_len, mtodo(n, n->m_len));
1468 n->m_len += m->m_len;
1469 sb->sb_ccc += m->m_len;
1470 sb->sb_tlscc += m->m_len;
1478 sb->sb_mtlstail = m;
1479 sballoc_ktls_rx(sb, m);
1489 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
1492 sbflush_internal(struct sockbuf *sb)
1495 while (sb->sb_mbcnt || sb->sb_tlsdcc) {
1497 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
1498 * we would loop forever. Panic instead.
1500 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1502 m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
1504 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
1505 ("%s: ccc %u mb %p mbcnt %u", __func__,
1506 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1510 sbflush_locked(struct sockbuf *sb)
1513 SOCKBUF_LOCK_ASSERT(sb);
1514 sbflush_internal(sb);
1518 sbflush(struct sockbuf *sb)
1527 * Cut data from (the front of) a sockbuf.
1529 static struct mbuf *
1530 sbcut_internal(struct sockbuf *sb, int len)
1532 struct mbuf *m, *next, *mfree;
1535 KASSERT(len >= 0, ("%s: len is %d but it is supposed to be >= 0",
1537 KASSERT(len <= sb->sb_ccc, ("%s: len: %d is > ccc: %u",
1538 __func__, len, sb->sb_ccc));
1540 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1547 if (next == NULL && !is_tls) {
1548 if (sb->sb_tlsdcc != 0) {
1549 MPASS(len >= sb->sb_tlsdcc);
1550 len -= sb->sb_tlsdcc;
1551 sb->sb_ccc -= sb->sb_tlsdcc;
1560 KASSERT(next, ("%s: no next, len %d", __func__, len));
1562 next = m->m_nextpkt;
1564 if (m->m_len > len) {
1565 KASSERT(!(m->m_flags & M_NOTAVAIL),
1566 ("%s: m %p M_NOTAVAIL", __func__, m));
1571 if (sb->sb_sndptroff != 0)
1572 sb->sb_sndptroff -= len;
1573 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1580 sbfree_ktls_rx(sb, m);
1585 * Do not put M_NOTREADY buffers to the free list, they
1586 * are referenced from outside.
1588 if (m->m_flags & M_NOTREADY && !is_tls)
1600 * Free any zero-length mbufs from the buffer.
1601 * For SOCK_DGRAM sockets such mbufs represent empty records.
1602 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1603 * when sosend_generic() needs to send only control data.
1605 while (m && m->m_len == 0) {
1619 sb->sb_mtlstail = NULL;
1624 m->m_nextpkt = next;
1628 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
1629 * sb_lastrecord is up-to-date if we dropped part of the last record.
1633 sb->sb_mbtail = NULL;
1634 sb->sb_lastrecord = NULL;
1635 } else if (m->m_nextpkt == NULL) {
1636 sb->sb_lastrecord = m;
1643 * Drop data from (the front of) a sockbuf.
1646 sbdrop_locked(struct sockbuf *sb, int len)
1649 SOCKBUF_LOCK_ASSERT(sb);
1650 m_freem(sbcut_internal(sb, len));
1654 * Drop data from (the front of) a sockbuf,
1655 * and return it to caller.
1658 sbcut_locked(struct sockbuf *sb, int len)
1661 SOCKBUF_LOCK_ASSERT(sb);
1662 return (sbcut_internal(sb, len));
1666 sbdrop(struct sockbuf *sb, int len)
1671 mfree = sbcut_internal(sb, len);
1678 sbsndptr_noadv(struct sockbuf *sb, uint32_t off, uint32_t *moff)
1682 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1683 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1685 if (sb->sb_sndptr == NULL) {
1686 sb->sb_sndptr = sb->sb_mb;
1687 sb->sb_sndptroff = 0;
1692 off -= sb->sb_sndptroff;
1699 sbsndptr_adv(struct sockbuf *sb, struct mbuf *mb, uint32_t len)
1702 * A small copy was done, advance forward the sb_sbsndptr to cover
1707 if (mb != sb->sb_sndptr) {
1708 /* Did not copyout at the same mbuf */
1712 while (m && (len > 0)) {
1713 if (len >= m->m_len) {
1716 sb->sb_sndptroff += m->m_len;
1717 sb->sb_sndptr = m->m_next;
1727 * Return the first mbuf and the mbuf data offset for the provided
1728 * send offset without changing the "sb_sndptroff" field.
1731 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1735 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1738 * If the "off" is below the stored offset, which happens on
1739 * retransmits, just use "sb_mb":
1741 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1745 off -= sb->sb_sndptroff;
1747 while (off > 0 && m != NULL) {
1758 * Drop a record off the front of a sockbuf and move the next record to the
1762 sbdroprecord_locked(struct sockbuf *sb)
1766 SOCKBUF_LOCK_ASSERT(sb);
1770 sb->sb_mb = m->m_nextpkt;
1780 * Drop a record off the front of a sockbuf and move the next record to the
1784 sbdroprecord(struct sockbuf *sb)
1788 sbdroprecord_locked(sb);
1793 * Create a "control" mbuf containing the specified data with the specified
1794 * type for presentation on a socket buffer.
1797 sbcreatecontrol(const void *p, u_int size, int type, int level, int wait)
1802 MBUF_CHECKSLEEP(wait);
1804 if (wait == M_NOWAIT) {
1805 if (CMSG_SPACE(size) > MCLBYTES)
1808 KASSERT(CMSG_SPACE(size) <= MCLBYTES,
1809 ("%s: passed CMSG_SPACE(%u) > MCLBYTES", __func__, size));
1811 if (CMSG_SPACE(size) > MLEN)
1812 m = m_getcl(wait, MT_CONTROL, 0);
1814 m = m_get(wait, MT_CONTROL);
1818 KASSERT(CMSG_SPACE(size) <= M_TRAILINGSPACE(m),
1819 ("sbcreatecontrol: short mbuf"));
1821 * Don't leave the padding between the msg header and the
1822 * cmsg data and the padding after the cmsg data un-initialized.
1824 cp = mtod(m, struct cmsghdr *);
1825 bzero(cp, CMSG_SPACE(size));
1827 (void)memcpy(CMSG_DATA(cp), p, size);
1828 m->m_len = CMSG_SPACE(size);
1829 cp->cmsg_len = CMSG_LEN(size);
1830 cp->cmsg_level = level;
1831 cp->cmsg_type = type;
1836 * This does the same for socket buffers that sotoxsocket does for sockets:
1837 * generate an user-format data structure describing the socket buffer. Note
1838 * that the xsockbuf structure, since it is always embedded in a socket, does
1839 * not include a self pointer nor a length. We make this entry point public
1840 * in case some other mechanism needs it.
1843 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1846 xsb->sb_cc = sb->sb_ccc;
1847 xsb->sb_hiwat = sb->sb_hiwat;
1848 xsb->sb_mbcnt = sb->sb_mbcnt;
1849 xsb->sb_mbmax = sb->sb_mbmax;
1850 xsb->sb_lowat = sb->sb_lowat;
1851 xsb->sb_flags = sb->sb_flags;
1852 xsb->sb_timeo = sb->sb_timeo;
1855 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1857 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW | CTLFLAG_SKIP, &dummy, 0, "");
1858 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf,
1859 CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &sb_max, 0,
1860 sysctl_handle_sb_max, "LU",
1861 "Maximum socket buffer size");
1862 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1863 &sb_efficiency, 0, "Socket buffer size waste factor");