2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_param.h"
37 #include <sys/param.h>
38 #include <sys/aio.h> /* for aio_swake proto */
39 #include <sys/kernel.h>
42 #include <sys/mutex.h>
44 #include <sys/protosw.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
53 * Function pointer set by the AIO routines so that the socket buffer code
54 * can call back into the AIO module if it is loaded.
56 void (*aio_swake)(struct socket *, struct sockbuf *);
59 * Primitive routines for operating on socket buffers
62 u_long sb_max = SB_MAX;
64 (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
66 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
68 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len);
69 static void sbflush_internal(struct sockbuf *sb);
72 * Socantsendmore indicates that no more data will be sent on the socket; it
73 * would normally be applied to a socket when the user informs the system
74 * that no more data is to be sent, by the protocol code (in case
75 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
76 * received, and will normally be applied to the socket by a protocol when it
77 * detects that the peer will send no more data. Data queued for reading in
78 * the socket may yet be read.
81 socantsendmore_locked(struct socket *so)
84 SOCKBUF_LOCK_ASSERT(&so->so_snd);
86 so->so_snd.sb_state |= SBS_CANTSENDMORE;
88 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
92 socantsendmore(struct socket *so)
95 SOCKBUF_LOCK(&so->so_snd);
96 socantsendmore_locked(so);
97 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
101 socantrcvmore_locked(struct socket *so)
104 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
106 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
107 sorwakeup_locked(so);
108 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
112 socantrcvmore(struct socket *so)
115 SOCKBUF_LOCK(&so->so_rcv);
116 socantrcvmore_locked(so);
117 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
121 * Wait for data to arrive at/drain from a socket buffer.
124 sbwait(struct sockbuf *sb)
127 SOCKBUF_LOCK_ASSERT(sb);
129 sb->sb_flags |= SB_WAIT;
130 return (msleep_sbt(&sb->sb_cc, &sb->sb_mtx,
131 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
132 sb->sb_timeo, 0, 0));
136 sblock(struct sockbuf *sb, int flags)
139 KASSERT((flags & SBL_VALID) == flags,
140 ("sblock: flags invalid (0x%x)", flags));
142 if (flags & SBL_WAIT) {
143 if ((sb->sb_flags & SB_NOINTR) ||
144 (flags & SBL_NOINTR)) {
145 sx_xlock(&sb->sb_sx);
148 return (sx_xlock_sig(&sb->sb_sx));
150 if (sx_try_xlock(&sb->sb_sx) == 0)
151 return (EWOULDBLOCK);
157 sbunlock(struct sockbuf *sb)
160 sx_xunlock(&sb->sb_sx);
164 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
165 * via SIGIO if the socket has the SS_ASYNC flag set.
167 * Called with the socket buffer lock held; will release the lock by the end
168 * of the function. This allows the caller to acquire the socket buffer lock
169 * while testing for the need for various sorts of wakeup and hold it through
170 * to the point where it's no longer required. We currently hold the lock
171 * through calls out to other subsystems (with the exception of kqueue), and
172 * then release it to avoid lock order issues. It's not clear that's
176 sowakeup(struct socket *so, struct sockbuf *sb)
180 SOCKBUF_LOCK_ASSERT(sb);
182 selwakeuppri(&sb->sb_sel, PSOCK);
183 if (!SEL_WAITING(&sb->sb_sel))
184 sb->sb_flags &= ~SB_SEL;
185 if (sb->sb_flags & SB_WAIT) {
186 sb->sb_flags &= ~SB_WAIT;
189 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
190 if (sb->sb_upcall != NULL) {
191 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
192 if (ret == SU_ISCONNECTED) {
193 KASSERT(sb == &so->so_rcv,
194 ("SO_SND upcall returned SU_ISCONNECTED"));
195 soupcall_clear(so, SO_RCV);
199 if (sb->sb_flags & SB_AIO)
202 if (ret == SU_ISCONNECTED)
204 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
205 pgsigio(&so->so_sigio, SIGIO, 0);
206 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
210 * Socket buffer (struct sockbuf) utility routines.
212 * Each socket contains two socket buffers: one for sending data and one for
213 * receiving data. Each buffer contains a queue of mbufs, information about
214 * the number of mbufs and amount of data in the queue, and other fields
215 * allowing select() statements and notification on data availability to be
218 * Data stored in a socket buffer is maintained as a list of records. Each
219 * record is a list of mbufs chained together with the m_next field. Records
220 * are chained together with the m_nextpkt field. The upper level routine
221 * soreceive() expects the following conventions to be observed when placing
222 * information in the receive buffer:
224 * 1. If the protocol requires each message be preceded by the sender's name,
225 * then a record containing that name must be present before any
226 * associated data (mbuf's must be of type MT_SONAME).
227 * 2. If the protocol supports the exchange of ``access rights'' (really just
228 * additional data associated with the message), and there are ``rights''
229 * to be received, then a record containing this data should be present
230 * (mbuf's must be of type MT_RIGHTS).
231 * 3. If a name or rights record exists, then it must be followed by a data
232 * record, perhaps of zero length.
234 * Before using a new socket structure it is first necessary to reserve
235 * buffer space to the socket, by calling sbreserve(). This should commit
236 * some of the available buffer space in the system buffer pool for the
237 * socket (currently, it does nothing but enforce limits). The space should
238 * be released by calling sbrelease() when the socket is destroyed.
241 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
243 struct thread *td = curthread;
245 SOCKBUF_LOCK(&so->so_snd);
246 SOCKBUF_LOCK(&so->so_rcv);
247 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
249 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
251 if (so->so_rcv.sb_lowat == 0)
252 so->so_rcv.sb_lowat = 1;
253 if (so->so_snd.sb_lowat == 0)
254 so->so_snd.sb_lowat = MCLBYTES;
255 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
256 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
257 SOCKBUF_UNLOCK(&so->so_rcv);
258 SOCKBUF_UNLOCK(&so->so_snd);
261 sbrelease_locked(&so->so_snd, so);
263 SOCKBUF_UNLOCK(&so->so_rcv);
264 SOCKBUF_UNLOCK(&so->so_snd);
269 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
272 u_long tmp_sb_max = sb_max;
274 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
275 if (error || !req->newptr)
277 if (tmp_sb_max < MSIZE + MCLBYTES)
280 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
285 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
286 * become limiting if buffering efficiency is near the normal case.
289 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
294 SOCKBUF_LOCK_ASSERT(sb);
297 * When a thread is passed, we take into account the thread's socket
298 * buffer size limit. The caller will generally pass curthread, but
299 * in the TCP input path, NULL will be passed to indicate that no
300 * appropriate thread resource limits are available. In that case,
301 * we don't apply a process limit.
306 PROC_LOCK(td->td_proc);
307 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
308 PROC_UNLOCK(td->td_proc);
310 sbsize_limit = RLIM_INFINITY;
311 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
314 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
315 if (sb->sb_lowat > sb->sb_hiwat)
316 sb->sb_lowat = sb->sb_hiwat;
321 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
327 error = sbreserve_locked(sb, cc, so, td);
333 * Free mbufs held by a socket, and reserved mbuf space.
336 sbrelease_internal(struct sockbuf *sb, struct socket *so)
339 sbflush_internal(sb);
340 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
346 sbrelease_locked(struct sockbuf *sb, struct socket *so)
349 SOCKBUF_LOCK_ASSERT(sb);
351 sbrelease_internal(sb, so);
355 sbrelease(struct sockbuf *sb, struct socket *so)
359 sbrelease_locked(sb, so);
364 sbdestroy(struct sockbuf *sb, struct socket *so)
367 sbrelease_internal(sb, so);
371 * Routines to add and remove data from an mbuf queue.
373 * The routines sbappend() or sbappendrecord() are normally called to append
374 * new mbufs to a socket buffer, after checking that adequate space is
375 * available, comparing the function sbspace() with the amount of data to be
376 * added. sbappendrecord() differs from sbappend() in that data supplied is
377 * treated as the beginning of a new record. To place a sender's address,
378 * optional access rights, and data in a socket receive buffer,
379 * sbappendaddr() should be used. To place access rights and data in a
380 * socket receive buffer, sbappendrights() should be used. In either case,
381 * the new data begins a new record. Note that unlike sbappend() and
382 * sbappendrecord(), these routines check for the caller that there will be
383 * enough space to store the data. Each fails if there is not enough space,
384 * or if it cannot find mbufs to store additional information in.
386 * Reliable protocols may use the socket send buffer to hold data awaiting
387 * acknowledgement. Data is normally copied from a socket send buffer in a
388 * protocol with m_copy for output to a peer, and then removing the data from
389 * the socket buffer with sbdrop() or sbdroprecord() when the data is
390 * acknowledged by the peer.
394 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
396 struct mbuf *m = sb->sb_mb;
398 SOCKBUF_LOCK_ASSERT(sb);
400 while (m && m->m_nextpkt)
403 if (m != sb->sb_lastrecord) {
404 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
405 __func__, sb->sb_mb, sb->sb_lastrecord, m);
406 printf("packet chain:\n");
407 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
409 panic("%s from %s:%u", __func__, file, line);
414 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
416 struct mbuf *m = sb->sb_mb;
419 SOCKBUF_LOCK_ASSERT(sb);
421 while (m && m->m_nextpkt)
424 while (m && m->m_next)
427 if (m != sb->sb_mbtail) {
428 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
429 __func__, sb->sb_mb, sb->sb_mbtail, m);
430 printf("packet tree:\n");
431 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
433 for (n = m; n != NULL; n = n->m_next)
437 panic("%s from %s:%u", __func__, file, line);
440 #endif /* SOCKBUF_DEBUG */
442 #define SBLINKRECORD(sb, m0) do { \
443 SOCKBUF_LOCK_ASSERT(sb); \
444 if ((sb)->sb_lastrecord != NULL) \
445 (sb)->sb_lastrecord->m_nextpkt = (m0); \
447 (sb)->sb_mb = (m0); \
448 (sb)->sb_lastrecord = (m0); \
449 } while (/*CONSTCOND*/0)
452 * Append mbuf chain m to the last record in the socket buffer sb. The
453 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
454 * are discarded and mbufs are compacted where possible.
457 sbappend_locked(struct sockbuf *sb, struct mbuf *m)
461 SOCKBUF_LOCK_ASSERT(sb);
472 if (n->m_flags & M_EOR) {
473 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
476 } while (n->m_next && (n = n->m_next));
479 * XXX Would like to simply use sb_mbtail here, but
480 * XXX I need to verify that I won't miss an EOR that
483 if ((n = sb->sb_lastrecord) != NULL) {
485 if (n->m_flags & M_EOR) {
486 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
489 } while (n->m_next && (n = n->m_next));
492 * If this is the first record in the socket buffer,
493 * it's also the last record.
495 sb->sb_lastrecord = m;
498 sbcompress(sb, m, n);
503 * Append mbuf chain m to the last record in the socket buffer sb. The
504 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
505 * are discarded and mbufs are compacted where possible.
508 sbappend(struct sockbuf *sb, struct mbuf *m)
512 sbappend_locked(sb, m);
517 * This version of sbappend() should only be used when the caller absolutely
518 * knows that there will never be more than one record in the socket buffer,
519 * that is, a stream protocol (such as TCP).
522 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
524 SOCKBUF_LOCK_ASSERT(sb);
526 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
527 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
531 /* Remove all packet headers and mbuf tags to get a pure data chain. */
534 sbcompress(sb, m, sb->sb_mbtail);
536 sb->sb_lastrecord = sb->sb_mb;
541 * This version of sbappend() should only be used when the caller absolutely
542 * knows that there will never be more than one record in the socket buffer,
543 * that is, a stream protocol (such as TCP).
546 sbappendstream(struct sockbuf *sb, struct mbuf *m)
550 sbappendstream_locked(sb, m);
556 sbcheck(struct sockbuf *sb)
560 u_long len = 0, mbcnt = 0;
562 SOCKBUF_LOCK_ASSERT(sb);
564 for (m = sb->sb_mb; m; m = n) {
566 for (; m; m = m->m_next) {
569 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
570 mbcnt += m->m_ext.ext_size;
573 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
574 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
575 mbcnt, sb->sb_mbcnt);
582 * As above, except the mbuf chain begins a new record.
585 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
589 SOCKBUF_LOCK_ASSERT(sb);
594 * Put the first mbuf on the queue. Note this permits zero length
599 SBLINKRECORD(sb, m0);
603 if (m && (m0->m_flags & M_EOR)) {
604 m0->m_flags &= ~M_EOR;
607 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
608 sbcompress(sb, m, m0);
612 * As above, except the mbuf chain begins a new record.
615 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
619 sbappendrecord_locked(sb, m0);
623 /* Helper routine that appends data, control, and address to a sockbuf. */
625 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
626 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
628 struct mbuf *m, *n, *nlast;
630 if (asa->sa_len > MLEN)
633 m = m_get(M_NOWAIT, MT_SONAME);
636 m->m_len = asa->sa_len;
637 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
639 ctrl_last->m_next = m0; /* concatenate data to control */
643 for (n = m; n->m_next != NULL; n = n->m_next)
649 sb->sb_mbtail = nlast;
657 * Append address and data, and optionally, control (ancillary) data to the
658 * receive queue of a socket. If present, m0 must include a packet header
659 * with total length. Returns 0 if no space in sockbuf or insufficient
663 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
664 struct mbuf *m0, struct mbuf *control)
666 struct mbuf *ctrl_last;
667 int space = asa->sa_len;
669 SOCKBUF_LOCK_ASSERT(sb);
671 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
672 panic("sbappendaddr_locked");
674 space += m0->m_pkthdr.len;
675 space += m_length(control, &ctrl_last);
677 if (space > sbspace(sb))
679 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
683 * Append address and data, and optionally, control (ancillary) data to the
684 * receive queue of a socket. If present, m0 must include a packet header
685 * with total length. Returns 0 if insufficient mbufs. Does not validate space
686 * on the receiving sockbuf.
689 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
690 struct mbuf *m0, struct mbuf *control)
692 struct mbuf *ctrl_last;
694 SOCKBUF_LOCK_ASSERT(sb);
696 ctrl_last = (control == NULL) ? NULL : m_last(control);
697 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
701 * Append address and data, and optionally, control (ancillary) data to the
702 * receive queue of a socket. If present, m0 must include a packet header
703 * with total length. Returns 0 if no space in sockbuf or insufficient
707 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
708 struct mbuf *m0, struct mbuf *control)
713 retval = sbappendaddr_locked(sb, asa, m0, control);
719 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
720 struct mbuf *control)
722 struct mbuf *m, *n, *mlast;
725 SOCKBUF_LOCK_ASSERT(sb);
728 panic("sbappendcontrol_locked");
729 space = m_length(control, &n) + m_length(m0, NULL);
731 if (space > sbspace(sb))
733 n->m_next = m0; /* concatenate data to control */
737 for (m = control; m->m_next; m = m->m_next)
741 SBLINKRECORD(sb, control);
743 sb->sb_mbtail = mlast;
751 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
756 retval = sbappendcontrol_locked(sb, m0, control);
762 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
763 * (n). If (n) is NULL, the buffer is presumed empty.
765 * When the data is compressed, mbufs in the chain may be handled in one of
768 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
769 * record boundary, and no change in data type).
771 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
772 * an mbuf already in the socket buffer. This can occur if an
773 * appropriate mbuf exists, there is room, and no merging of data types
776 * (3) The mbuf may be appended to the end of the existing mbuf chain.
778 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
782 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
787 SOCKBUF_LOCK_ASSERT(sb);
790 eor |= m->m_flags & M_EOR;
793 (((o = m->m_next) || (o = n)) &&
794 o->m_type == m->m_type))) {
795 if (sb->sb_lastrecord == m)
796 sb->sb_lastrecord = m->m_next;
800 if (n && (n->m_flags & M_EOR) == 0 &&
802 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
803 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
804 m->m_len <= M_TRAILINGSPACE(n) &&
805 n->m_type == m->m_type) {
806 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
808 n->m_len += m->m_len;
809 sb->sb_cc += m->m_len;
810 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
811 /* XXX: Probably don't need.*/
812 sb->sb_ctl += m->m_len;
823 m->m_flags &= ~M_EOR;
828 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
835 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
838 sbflush_internal(struct sockbuf *sb)
841 while (sb->sb_mbcnt) {
843 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
844 * we would loop forever. Panic instead.
846 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
848 m_freem(sbcut_internal(sb, (int)sb->sb_cc));
850 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
851 panic("sbflush_internal: cc %u || mb %p || mbcnt %u",
852 sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
856 sbflush_locked(struct sockbuf *sb)
859 SOCKBUF_LOCK_ASSERT(sb);
860 sbflush_internal(sb);
864 sbflush(struct sockbuf *sb)
873 * Cut data from (the front of) a sockbuf.
876 sbcut_internal(struct sockbuf *sb, int len)
878 struct mbuf *m, *n, *next, *mfree;
880 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
891 if (m->m_len > len) {
895 if (sb->sb_sndptroff != 0)
896 sb->sb_sndptroff -= len;
897 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
908 while (m && m->m_len == 0) {
921 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
922 * sb_lastrecord is up-to-date if we dropped part of the last record.
926 sb->sb_mbtail = NULL;
927 sb->sb_lastrecord = NULL;
928 } else if (m->m_nextpkt == NULL) {
929 sb->sb_lastrecord = m;
936 * Drop data from (the front of) a sockbuf.
939 sbdrop_locked(struct sockbuf *sb, int len)
942 SOCKBUF_LOCK_ASSERT(sb);
943 m_freem(sbcut_internal(sb, len));
947 * Drop data from (the front of) a sockbuf,
948 * and return it to caller.
951 sbcut_locked(struct sockbuf *sb, int len)
954 SOCKBUF_LOCK_ASSERT(sb);
955 return (sbcut_internal(sb, len));
959 sbdrop(struct sockbuf *sb, int len)
964 mfree = sbcut_internal(sb, len);
971 * Maintain a pointer and offset pair into the socket buffer mbuf chain to
972 * avoid traversal of the entire socket buffer for larger offsets.
975 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
977 struct mbuf *m, *ret;
979 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
980 KASSERT(off + len <= sb->sb_cc, ("%s: beyond sb", __func__));
981 KASSERT(sb->sb_sndptroff <= sb->sb_cc, ("%s: sndptroff broken", __func__));
984 * Is off below stored offset? Happens on retransmits.
985 * Just return, we can't help here.
987 if (sb->sb_sndptroff > off) {
992 /* Return closest mbuf in chain for current offset. */
993 *moff = off - sb->sb_sndptroff;
994 m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
995 if (*moff == m->m_len) {
997 sb->sb_sndptroff += m->m_len;
999 KASSERT(ret->m_len > 0,
1000 ("mbuf %p in sockbuf %p chain has no valid data", ret, sb));
1003 /* Advance by len to be as close as possible for the next transmit. */
1004 for (off = off - sb->sb_sndptroff + len - 1;
1005 off > 0 && m != NULL && off >= m->m_len;
1007 sb->sb_sndptroff += m->m_len;
1010 if (off > 0 && m == NULL)
1011 panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret);
1018 * Return the first mbuf and the mbuf data offset for the provided
1019 * send offset without changing the "sb_sndptroff" field.
1022 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1026 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1029 * If the "off" is below the stored offset, which happens on
1030 * retransmits, just use "sb_mb":
1032 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1036 off -= sb->sb_sndptroff;
1038 while (off > 0 && m != NULL) {
1049 * Drop a record off the front of a sockbuf and move the next record to the
1053 sbdroprecord_locked(struct sockbuf *sb)
1057 SOCKBUF_LOCK_ASSERT(sb);
1061 sb->sb_mb = m->m_nextpkt;
1071 * Drop a record off the front of a sockbuf and move the next record to the
1075 sbdroprecord(struct sockbuf *sb)
1079 sbdroprecord_locked(sb);
1084 * Create a "control" mbuf containing the specified data with the specified
1085 * type for presentation on a socket buffer.
1088 sbcreatecontrol(caddr_t p, int size, int type, int level)
1093 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1094 return ((struct mbuf *) NULL);
1095 if (CMSG_SPACE((u_int)size) > MLEN)
1096 m = m_getcl(M_NOWAIT, MT_CONTROL, 0);
1098 m = m_get(M_NOWAIT, MT_CONTROL);
1100 return ((struct mbuf *) NULL);
1101 cp = mtod(m, struct cmsghdr *);
1103 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1104 ("sbcreatecontrol: short mbuf"));
1106 * Don't leave the padding between the msg header and the
1107 * cmsg data and the padding after the cmsg data un-initialized.
1109 bzero(cp, CMSG_SPACE((u_int)size));
1111 (void)memcpy(CMSG_DATA(cp), p, size);
1112 m->m_len = CMSG_SPACE(size);
1113 cp->cmsg_len = CMSG_LEN(size);
1114 cp->cmsg_level = level;
1115 cp->cmsg_type = type;
1120 * This does the same for socket buffers that sotoxsocket does for sockets:
1121 * generate an user-format data structure describing the socket buffer. Note
1122 * that the xsockbuf structure, since it is always embedded in a socket, does
1123 * not include a self pointer nor a length. We make this entry point public
1124 * in case some other mechanism needs it.
1127 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1130 xsb->sb_cc = sb->sb_cc;
1131 xsb->sb_hiwat = sb->sb_hiwat;
1132 xsb->sb_mbcnt = sb->sb_mbcnt;
1133 xsb->sb_mcnt = sb->sb_mcnt;
1134 xsb->sb_ccnt = sb->sb_ccnt;
1135 xsb->sb_mbmax = sb->sb_mbmax;
1136 xsb->sb_lowat = sb->sb_lowat;
1137 xsb->sb_flags = sb->sb_flags;
1138 xsb->sb_timeo = sb->sb_timeo;
1141 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1143 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1144 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1145 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1146 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1147 &sb_efficiency, 0, "Socket buffer size waste factor");