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;
63 static u_long sb_max_adj =
64 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
66 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
68 static void sbdrop_internal(struct sockbuf *sb, int len);
69 static void sbflush_internal(struct sockbuf *sb);
70 static void sbrelease_internal(struct sockbuf *sb, struct socket *so);
73 * Socantsendmore indicates that no more data will be sent on the socket; it
74 * would normally be applied to a socket when the user informs the system
75 * that no more data is to be sent, by the protocol code (in case
76 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
77 * received, and will normally be applied to the socket by a protocol when it
78 * detects that the peer will send no more data. Data queued for reading in
79 * the socket may yet be read.
82 socantsendmore_locked(struct socket *so)
85 SOCKBUF_LOCK_ASSERT(&so->so_snd);
87 so->so_snd.sb_state |= SBS_CANTSENDMORE;
89 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
93 socantsendmore(struct socket *so)
96 SOCKBUF_LOCK(&so->so_snd);
97 socantsendmore_locked(so);
98 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
102 socantrcvmore_locked(struct socket *so)
105 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
107 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
108 sorwakeup_locked(so);
109 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
113 socantrcvmore(struct socket *so)
116 SOCKBUF_LOCK(&so->so_rcv);
117 socantrcvmore_locked(so);
118 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
122 * Wait for data to arrive at/drain from a socket buffer.
125 sbwait(struct sockbuf *sb)
128 SOCKBUF_LOCK_ASSERT(sb);
130 sb->sb_flags |= SB_WAIT;
131 return (msleep(&sb->sb_cc, &sb->sb_mtx,
132 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
137 sblock(struct sockbuf *sb, int flags)
140 if (flags == M_WAITOK) {
141 sx_xlock(&sb->sb_sx);
144 if (sx_try_xlock(&sb->sb_sx) == 0)
145 return (EWOULDBLOCK);
151 sbunlock(struct sockbuf *sb)
154 sx_xunlock(&sb->sb_sx);
158 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
159 * via SIGIO if the socket has the SS_ASYNC flag set.
161 * Called with the socket buffer lock held; will release the lock by the end
162 * of the function. This allows the caller to acquire the socket buffer lock
163 * while testing for the need for various sorts of wakeup and hold it through
164 * to the point where it's no longer required. We currently hold the lock
165 * through calls out to other subsystems (with the exception of kqueue), and
166 * then release it to avoid lock order issues. It's not clear that's
170 sowakeup(struct socket *so, struct sockbuf *sb)
173 SOCKBUF_LOCK_ASSERT(sb);
175 selwakeuppri(&sb->sb_sel, PSOCK);
176 sb->sb_flags &= ~SB_SEL;
177 if (sb->sb_flags & SB_WAIT) {
178 sb->sb_flags &= ~SB_WAIT;
181 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
183 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
184 pgsigio(&so->so_sigio, SIGIO, 0);
185 if (sb->sb_flags & SB_UPCALL)
186 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
187 if (sb->sb_flags & SB_AIO)
189 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
193 * Socket buffer (struct sockbuf) utility routines.
195 * Each socket contains two socket buffers: one for sending data and one for
196 * receiving data. Each buffer contains a queue of mbufs, information about
197 * the number of mbufs and amount of data in the queue, and other fields
198 * allowing select() statements and notification on data availability to be
201 * Data stored in a socket buffer is maintained as a list of records. Each
202 * record is a list of mbufs chained together with the m_next field. Records
203 * are chained together with the m_nextpkt field. The upper level routine
204 * soreceive() expects the following conventions to be observed when placing
205 * information in the receive buffer:
207 * 1. If the protocol requires each message be preceded by the sender's name,
208 * then a record containing that name must be present before any
209 * associated data (mbuf's must be of type MT_SONAME).
210 * 2. If the protocol supports the exchange of ``access rights'' (really just
211 * additional data associated with the message), and there are ``rights''
212 * to be received, then a record containing this data should be present
213 * (mbuf's must be of type MT_RIGHTS).
214 * 3. If a name or rights record exists, then it must be followed by a data
215 * record, perhaps of zero length.
217 * Before using a new socket structure it is first necessary to reserve
218 * buffer space to the socket, by calling sbreserve(). This should commit
219 * some of the available buffer space in the system buffer pool for the
220 * socket (currently, it does nothing but enforce limits). The space should
221 * be released by calling sbrelease() when the socket is destroyed.
224 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
226 struct thread *td = curthread;
228 SOCKBUF_LOCK(&so->so_snd);
229 SOCKBUF_LOCK(&so->so_rcv);
230 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
232 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
234 if (so->so_rcv.sb_lowat == 0)
235 so->so_rcv.sb_lowat = 1;
236 if (so->so_snd.sb_lowat == 0)
237 so->so_snd.sb_lowat = MCLBYTES;
238 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
239 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
240 SOCKBUF_UNLOCK(&so->so_rcv);
241 SOCKBUF_UNLOCK(&so->so_snd);
244 sbrelease_locked(&so->so_snd, so);
246 SOCKBUF_UNLOCK(&so->so_rcv);
247 SOCKBUF_UNLOCK(&so->so_snd);
252 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
255 u_long tmp_sb_max = sb_max;
257 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
258 if (error || !req->newptr)
260 if (tmp_sb_max < MSIZE + MCLBYTES)
263 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
268 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
269 * become limiting if buffering efficiency is near the normal case.
272 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
277 SOCKBUF_LOCK_ASSERT(sb);
280 * td will only be NULL when we're in an interrupt (e.g. in
283 * XXXRW: This comment needs updating, as might the code.
288 PROC_LOCK(td->td_proc);
289 sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
290 PROC_UNLOCK(td->td_proc);
292 sbsize_limit = RLIM_INFINITY;
293 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
296 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
297 if (sb->sb_lowat > sb->sb_hiwat)
298 sb->sb_lowat = sb->sb_hiwat;
303 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
309 error = sbreserve_locked(sb, cc, so, td);
315 * Free mbufs held by a socket, and reserved mbuf space.
318 sbrelease_internal(struct sockbuf *sb, struct socket *so)
321 sbflush_internal(sb);
322 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
328 sbrelease_locked(struct sockbuf *sb, struct socket *so)
331 SOCKBUF_LOCK_ASSERT(sb);
333 sbrelease_internal(sb, so);
337 sbrelease(struct sockbuf *sb, struct socket *so)
341 sbrelease_locked(sb, so);
346 sbdestroy(struct sockbuf *sb, struct socket *so)
349 sbrelease_internal(sb, so);
353 * Routines to add and remove data from an mbuf queue.
355 * The routines sbappend() or sbappendrecord() are normally called to append
356 * new mbufs to a socket buffer, after checking that adequate space is
357 * available, comparing the function sbspace() with the amount of data to be
358 * added. sbappendrecord() differs from sbappend() in that data supplied is
359 * treated as the beginning of a new record. To place a sender's address,
360 * optional access rights, and data in a socket receive buffer,
361 * sbappendaddr() should be used. To place access rights and data in a
362 * socket receive buffer, sbappendrights() should be used. In either case,
363 * the new data begins a new record. Note that unlike sbappend() and
364 * sbappendrecord(), these routines check for the caller that there will be
365 * enough space to store the data. Each fails if there is not enough space,
366 * or if it cannot find mbufs to store additional information in.
368 * Reliable protocols may use the socket send buffer to hold data awaiting
369 * acknowledgement. Data is normally copied from a socket send buffer in a
370 * protocol with m_copy for output to a peer, and then removing the data from
371 * the socket buffer with sbdrop() or sbdroprecord() when the data is
372 * acknowledged by the peer.
376 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
378 struct mbuf *m = sb->sb_mb;
380 SOCKBUF_LOCK_ASSERT(sb);
382 while (m && m->m_nextpkt)
385 if (m != sb->sb_lastrecord) {
386 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
387 __func__, sb->sb_mb, sb->sb_lastrecord, m);
388 printf("packet chain:\n");
389 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
391 panic("%s from %s:%u", __func__, file, line);
396 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
398 struct mbuf *m = sb->sb_mb;
401 SOCKBUF_LOCK_ASSERT(sb);
403 while (m && m->m_nextpkt)
406 while (m && m->m_next)
409 if (m != sb->sb_mbtail) {
410 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
411 __func__, sb->sb_mb, sb->sb_mbtail, m);
412 printf("packet tree:\n");
413 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
415 for (n = m; n != NULL; n = n->m_next)
419 panic("%s from %s:%u", __func__, file, line);
422 #endif /* SOCKBUF_DEBUG */
424 #define SBLINKRECORD(sb, m0) do { \
425 SOCKBUF_LOCK_ASSERT(sb); \
426 if ((sb)->sb_lastrecord != NULL) \
427 (sb)->sb_lastrecord->m_nextpkt = (m0); \
429 (sb)->sb_mb = (m0); \
430 (sb)->sb_lastrecord = (m0); \
431 } while (/*CONSTCOND*/0)
434 * Append mbuf chain m to the last record in the socket buffer sb. The
435 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
436 * are discarded and mbufs are compacted where possible.
439 sbappend_locked(struct sockbuf *sb, struct mbuf *m)
443 SOCKBUF_LOCK_ASSERT(sb);
454 if (n->m_flags & M_EOR) {
455 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
458 } while (n->m_next && (n = n->m_next));
461 * XXX Would like to simply use sb_mbtail here, but
462 * XXX I need to verify that I won't miss an EOR that
465 if ((n = sb->sb_lastrecord) != NULL) {
467 if (n->m_flags & M_EOR) {
468 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
471 } while (n->m_next && (n = n->m_next));
474 * If this is the first record in the socket buffer,
475 * it's also the last record.
477 sb->sb_lastrecord = m;
480 sbcompress(sb, m, n);
485 * Append mbuf chain m to the last record in the socket buffer sb. The
486 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
487 * are discarded and mbufs are compacted where possible.
490 sbappend(struct sockbuf *sb, struct mbuf *m)
494 sbappend_locked(sb, m);
499 * This version of sbappend() should only be used when the caller absolutely
500 * knows that there will never be more than one record in the socket buffer,
501 * that is, a stream protocol (such as TCP).
504 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
506 SOCKBUF_LOCK_ASSERT(sb);
508 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
509 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
513 sbcompress(sb, m, sb->sb_mbtail);
515 sb->sb_lastrecord = sb->sb_mb;
520 * This version of sbappend() should only be used when the caller absolutely
521 * knows that there will never be more than one record in the socket buffer,
522 * that is, a stream protocol (such as TCP).
525 sbappendstream(struct sockbuf *sb, struct mbuf *m)
529 sbappendstream_locked(sb, m);
535 sbcheck(struct sockbuf *sb)
539 u_long len = 0, mbcnt = 0;
541 SOCKBUF_LOCK_ASSERT(sb);
543 for (m = sb->sb_mb; m; m = n) {
545 for (; m; m = m->m_next) {
548 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
549 mbcnt += m->m_ext.ext_size;
552 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
553 printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
554 mbcnt, sb->sb_mbcnt);
561 * As above, except the mbuf chain begins a new record.
564 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
568 SOCKBUF_LOCK_ASSERT(sb);
577 * Put the first mbuf on the queue. Note this permits zero length
582 SBLINKRECORD(sb, m0);
589 if (m && (m0->m_flags & M_EOR)) {
590 m0->m_flags &= ~M_EOR;
593 sbcompress(sb, m, m0);
597 * As above, except the mbuf chain begins a new record.
600 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
604 sbappendrecord_locked(sb, m0);
609 * Append address and data, and optionally, control (ancillary) data to the
610 * receive queue of a socket. If present, m0 must include a packet header
611 * with total length. Returns 0 if no space in sockbuf or insufficient
615 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
616 struct mbuf *m0, struct mbuf *control)
618 struct mbuf *m, *n, *nlast;
619 int space = asa->sa_len;
621 SOCKBUF_LOCK_ASSERT(sb);
623 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
624 panic("sbappendaddr_locked");
626 space += m0->m_pkthdr.len;
627 space += m_length(control, &n);
629 if (space > sbspace(sb))
632 if (asa->sa_len > MLEN)
635 MGET(m, M_DONTWAIT, MT_SONAME);
638 m->m_len = asa->sa_len;
639 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
641 n->m_next = m0; /* concatenate data to control */
645 for (n = m; n->m_next != NULL; n = n->m_next)
651 sb->sb_mbtail = nlast;
659 * Append address and data, and optionally, control (ancillary) data to the
660 * receive queue of a socket. If present, m0 must include a packet header
661 * with total length. Returns 0 if no space in sockbuf or insufficient
665 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
666 struct mbuf *m0, struct mbuf *control)
671 retval = sbappendaddr_locked(sb, asa, m0, control);
677 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
678 struct mbuf *control)
680 struct mbuf *m, *n, *mlast;
683 SOCKBUF_LOCK_ASSERT(sb);
686 panic("sbappendcontrol_locked");
687 space = m_length(control, &n) + m_length(m0, NULL);
689 if (space > sbspace(sb))
691 n->m_next = m0; /* concatenate data to control */
695 for (m = control; m->m_next; m = m->m_next)
699 SBLINKRECORD(sb, control);
701 sb->sb_mbtail = mlast;
709 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
714 retval = sbappendcontrol_locked(sb, m0, control);
720 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
721 * (n). If (n) is NULL, the buffer is presumed empty.
723 * When the data is compressed, mbufs in the chain may be handled in one of
726 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
727 * record boundary, and no change in data type).
729 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
730 * an mbuf already in the socket buffer. This can occur if an
731 * appropriate mbuf exists, there is room, and no merging of data types
734 * (3) The mbuf may be appended to the end of the existing mbuf chain.
736 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
740 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
745 SOCKBUF_LOCK_ASSERT(sb);
748 eor |= m->m_flags & M_EOR;
751 (((o = m->m_next) || (o = n)) &&
752 o->m_type == m->m_type))) {
753 if (sb->sb_lastrecord == m)
754 sb->sb_lastrecord = m->m_next;
758 if (n && (n->m_flags & M_EOR) == 0 &&
760 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
761 m->m_len <= M_TRAILINGSPACE(n) &&
762 n->m_type == m->m_type) {
763 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
765 n->m_len += m->m_len;
766 sb->sb_cc += m->m_len;
767 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
768 /* XXX: Probably don't need.*/
769 sb->sb_ctl += m->m_len;
780 m->m_flags &= ~M_EOR;
785 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
792 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
795 sbflush_internal(struct sockbuf *sb)
798 while (sb->sb_mbcnt) {
800 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
801 * we would loop forever. Panic instead.
803 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
805 sbdrop_internal(sb, (int)sb->sb_cc);
807 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
808 panic("sbflush_internal: cc %u || mb %p || mbcnt %u",
809 sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
813 sbflush_locked(struct sockbuf *sb)
816 SOCKBUF_LOCK_ASSERT(sb);
817 sbflush_internal(sb);
821 sbflush(struct sockbuf *sb)
830 * Drop data from (the front of) a sockbuf.
833 sbdrop_internal(struct sockbuf *sb, int len)
838 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
847 if (m->m_len > len) {
851 if (sb->sb_sndptroff != 0)
852 sb->sb_sndptroff -= len;
853 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
861 while (m && m->m_len == 0) {
871 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
872 * sb_lastrecord is up-to-date if we dropped part of the last record.
876 sb->sb_mbtail = NULL;
877 sb->sb_lastrecord = NULL;
878 } else if (m->m_nextpkt == NULL) {
879 sb->sb_lastrecord = m;
884 * Drop data from (the front of) a sockbuf.
887 sbdrop_locked(struct sockbuf *sb, int len)
890 SOCKBUF_LOCK_ASSERT(sb);
892 sbdrop_internal(sb, len);
896 sbdrop(struct sockbuf *sb, int len)
900 sbdrop_locked(sb, len);
905 * Maintain a pointer and offset pair into the socket buffer mbuf chain to
906 * avoid traversal of the entire socket buffer for larger offsets.
909 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
911 struct mbuf *m, *ret;
913 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
914 KASSERT(off + len <= sb->sb_cc, ("%s: beyond sb", __func__));
915 KASSERT(sb->sb_sndptroff <= sb->sb_cc, ("%s: sndptroff broken", __func__));
918 * Is off below stored offset? Happens on retransmits.
919 * Just return, we can't help here.
921 if (sb->sb_sndptroff > off) {
926 /* Return closest mbuf in chain for current offset. */
927 *moff = off - sb->sb_sndptroff;
928 m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
930 /* Advance by len to be as close as possible for the next transmit. */
931 for (off = off - sb->sb_sndptroff + len - 1;
932 off > 0 && off >= m->m_len;
934 sb->sb_sndptroff += m->m_len;
943 * Drop a record off the front of a sockbuf and move the next record to the
947 sbdroprecord_locked(struct sockbuf *sb)
951 SOCKBUF_LOCK_ASSERT(sb);
955 sb->sb_mb = m->m_nextpkt;
965 * Drop a record off the front of a sockbuf and move the next record to the
969 sbdroprecord(struct sockbuf *sb)
973 sbdroprecord_locked(sb);
978 * Create a "control" mbuf containing the specified data with the specified
979 * type for presentation on a socket buffer.
982 sbcreatecontrol(caddr_t p, int size, int type, int level)
987 if (CMSG_SPACE((u_int)size) > MCLBYTES)
988 return ((struct mbuf *) NULL);
989 if (CMSG_SPACE((u_int)size) > MLEN)
990 m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
992 m = m_get(M_DONTWAIT, MT_CONTROL);
994 return ((struct mbuf *) NULL);
995 cp = mtod(m, struct cmsghdr *);
997 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
998 ("sbcreatecontrol: short mbuf"));
1000 (void)memcpy(CMSG_DATA(cp), p, size);
1001 m->m_len = CMSG_SPACE(size);
1002 cp->cmsg_len = CMSG_LEN(size);
1003 cp->cmsg_level = level;
1004 cp->cmsg_type = type;
1009 * This does the same for socket buffers that sotoxsocket does for sockets:
1010 * generate an user-format data structure describing the socket buffer. Note
1011 * that the xsockbuf structure, since it is always embedded in a socket, does
1012 * not include a self pointer nor a length. We make this entry point public
1013 * in case some other mechanism needs it.
1016 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1019 xsb->sb_cc = sb->sb_cc;
1020 xsb->sb_hiwat = sb->sb_hiwat;
1021 xsb->sb_mbcnt = sb->sb_mbcnt;
1022 xsb->sb_mbmax = sb->sb_mbmax;
1023 xsb->sb_lowat = sb->sb_lowat;
1024 xsb->sb_flags = sb->sb_flags;
1025 xsb->sb_timeo = sb->sb_timeo;
1028 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1030 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1031 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1032 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1033 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1034 &sb_efficiency, 0, "");