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 * 3. 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>
41 #include <sys/malloc.h>
43 #include <sys/mutex.h>
45 #include <sys/protosw.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
54 * Function pointer set by the AIO routines so that the socket buffer code
55 * can call back into the AIO module if it is loaded.
57 void (*aio_swake)(struct socket *, struct sockbuf *);
60 * Primitive routines for operating on socket buffers
63 u_long sb_max = SB_MAX;
65 (quad_t)SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
67 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
69 static struct mbuf *sbcut_internal(struct sockbuf *sb, int len);
70 static void sbflush_internal(struct sockbuf *sb);
73 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
76 sbm_clrprotoflags(struct mbuf *m, int flags)
81 if (flags & PRUS_NOTREADY)
90 * Mark ready "count" mbufs starting with "m".
93 sbready(struct sockbuf *sb, struct mbuf *m, int count)
97 SOCKBUF_LOCK_ASSERT(sb);
98 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
100 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
102 for (int i = 0; i < count; i++, m = m->m_next) {
103 KASSERT(m->m_flags & M_NOTREADY,
104 ("%s: m %p !M_NOTREADY", __func__, m));
105 m->m_flags &= ~(M_NOTREADY | blocker);
107 sb->sb_acc += m->m_len;
111 return (EINPROGRESS);
113 /* This one was blocking all the queue. */
114 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
115 KASSERT(m->m_flags & M_BLOCKED,
116 ("%s: m %p !M_BLOCKED", __func__, m));
117 m->m_flags &= ~M_BLOCKED;
118 sb->sb_acc += m->m_len;
127 * Adjust sockbuf state reflecting allocation of m.
130 sballoc(struct sockbuf *sb, struct mbuf *m)
133 SOCKBUF_LOCK_ASSERT(sb);
135 sb->sb_ccc += m->m_len;
137 if (sb->sb_fnrdy == NULL) {
138 if (m->m_flags & M_NOTREADY)
141 sb->sb_acc += m->m_len;
143 m->m_flags |= M_BLOCKED;
145 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
146 sb->sb_ctl += m->m_len;
148 sb->sb_mbcnt += MSIZE;
151 if (m->m_flags & M_EXT) {
152 sb->sb_mbcnt += m->m_ext.ext_size;
158 * Adjust sockbuf state reflecting freeing of m.
161 sbfree(struct sockbuf *sb, struct mbuf *m)
164 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
165 SOCKBUF_LOCK_ASSERT(sb);
168 sb->sb_ccc -= m->m_len;
170 if (!(m->m_flags & M_NOTAVAIL))
171 sb->sb_acc -= m->m_len;
173 if (m == sb->sb_fnrdy) {
176 KASSERT(m->m_flags & M_NOTREADY,
177 ("%s: m %p !M_NOTREADY", __func__, m));
180 while (n != NULL && !(n->m_flags & M_NOTREADY)) {
181 n->m_flags &= ~M_BLOCKED;
182 sb->sb_acc += n->m_len;
188 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
189 sb->sb_ctl -= m->m_len;
191 sb->sb_mbcnt -= MSIZE;
193 if (m->m_flags & M_EXT) {
194 sb->sb_mbcnt -= m->m_ext.ext_size;
198 if (sb->sb_sndptr == m) {
199 sb->sb_sndptr = NULL;
200 sb->sb_sndptroff = 0;
202 if (sb->sb_sndptroff != 0)
203 sb->sb_sndptroff -= m->m_len;
207 * Socantsendmore indicates that no more data will be sent on the socket; it
208 * would normally be applied to a socket when the user informs the system
209 * that no more data is to be sent, by the protocol code (in case
210 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
211 * received, and will normally be applied to the socket by a protocol when it
212 * detects that the peer will send no more data. Data queued for reading in
213 * the socket may yet be read.
216 socantsendmore_locked(struct socket *so)
219 SOCKBUF_LOCK_ASSERT(&so->so_snd);
221 so->so_snd.sb_state |= SBS_CANTSENDMORE;
222 sowwakeup_locked(so);
223 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
227 socantsendmore(struct socket *so)
230 SOCKBUF_LOCK(&so->so_snd);
231 socantsendmore_locked(so);
232 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
236 socantrcvmore_locked(struct socket *so)
239 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
241 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
242 sorwakeup_locked(so);
243 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
247 socantrcvmore(struct socket *so)
250 SOCKBUF_LOCK(&so->so_rcv);
251 socantrcvmore_locked(so);
252 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
256 * Wait for data to arrive at/drain from a socket buffer.
259 sbwait(struct sockbuf *sb)
262 SOCKBUF_LOCK_ASSERT(sb);
264 sb->sb_flags |= SB_WAIT;
265 return (msleep_sbt(&sb->sb_acc, &sb->sb_mtx,
266 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
267 sb->sb_timeo, 0, 0));
271 sblock(struct sockbuf *sb, int flags)
274 KASSERT((flags & SBL_VALID) == flags,
275 ("sblock: flags invalid (0x%x)", flags));
277 if (flags & SBL_WAIT) {
278 if ((sb->sb_flags & SB_NOINTR) ||
279 (flags & SBL_NOINTR)) {
280 sx_xlock(&sb->sb_sx);
283 return (sx_xlock_sig(&sb->sb_sx));
285 if (sx_try_xlock(&sb->sb_sx) == 0)
286 return (EWOULDBLOCK);
292 sbunlock(struct sockbuf *sb)
295 sx_xunlock(&sb->sb_sx);
299 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
300 * via SIGIO if the socket has the SS_ASYNC flag set.
302 * Called with the socket buffer lock held; will release the lock by the end
303 * of the function. This allows the caller to acquire the socket buffer lock
304 * while testing for the need for various sorts of wakeup and hold it through
305 * to the point where it's no longer required. We currently hold the lock
306 * through calls out to other subsystems (with the exception of kqueue), and
307 * then release it to avoid lock order issues. It's not clear that's
311 sowakeup(struct socket *so, struct sockbuf *sb)
315 SOCKBUF_LOCK_ASSERT(sb);
317 selwakeuppri(&sb->sb_sel, PSOCK);
318 if (!SEL_WAITING(&sb->sb_sel))
319 sb->sb_flags &= ~SB_SEL;
320 if (sb->sb_flags & SB_WAIT) {
321 sb->sb_flags &= ~SB_WAIT;
324 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
325 if (sb->sb_upcall != NULL) {
326 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
327 if (ret == SU_ISCONNECTED) {
328 KASSERT(sb == &so->so_rcv,
329 ("SO_SND upcall returned SU_ISCONNECTED"));
330 soupcall_clear(so, SO_RCV);
334 if (sb->sb_flags & SB_AIO)
335 sowakeup_aio(so, sb);
337 if (ret == SU_ISCONNECTED)
339 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
340 pgsigio(&so->so_sigio, SIGIO, 0);
341 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
345 * Socket buffer (struct sockbuf) utility routines.
347 * Each socket contains two socket buffers: one for sending data and one for
348 * receiving data. Each buffer contains a queue of mbufs, information about
349 * the number of mbufs and amount of data in the queue, and other fields
350 * allowing select() statements and notification on data availability to be
353 * Data stored in a socket buffer is maintained as a list of records. Each
354 * record is a list of mbufs chained together with the m_next field. Records
355 * are chained together with the m_nextpkt field. The upper level routine
356 * soreceive() expects the following conventions to be observed when placing
357 * information in the receive buffer:
359 * 1. If the protocol requires each message be preceded by the sender's name,
360 * then a record containing that name must be present before any
361 * associated data (mbuf's must be of type MT_SONAME).
362 * 2. If the protocol supports the exchange of ``access rights'' (really just
363 * additional data associated with the message), and there are ``rights''
364 * to be received, then a record containing this data should be present
365 * (mbuf's must be of type MT_RIGHTS).
366 * 3. If a name or rights record exists, then it must be followed by a data
367 * record, perhaps of zero length.
369 * Before using a new socket structure it is first necessary to reserve
370 * buffer space to the socket, by calling sbreserve(). This should commit
371 * some of the available buffer space in the system buffer pool for the
372 * socket (currently, it does nothing but enforce limits). The space should
373 * be released by calling sbrelease() when the socket is destroyed.
376 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
378 struct thread *td = curthread;
380 SOCKBUF_LOCK(&so->so_snd);
381 SOCKBUF_LOCK(&so->so_rcv);
382 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
384 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
386 if (so->so_rcv.sb_lowat == 0)
387 so->so_rcv.sb_lowat = 1;
388 if (so->so_snd.sb_lowat == 0)
389 so->so_snd.sb_lowat = MCLBYTES;
390 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
391 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
392 SOCKBUF_UNLOCK(&so->so_rcv);
393 SOCKBUF_UNLOCK(&so->so_snd);
396 sbrelease_locked(&so->so_snd, so);
398 SOCKBUF_UNLOCK(&so->so_rcv);
399 SOCKBUF_UNLOCK(&so->so_snd);
404 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
407 u_long tmp_sb_max = sb_max;
409 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
410 if (error || !req->newptr)
412 if (tmp_sb_max < MSIZE + MCLBYTES)
415 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
420 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
421 * become limiting if buffering efficiency is near the normal case.
424 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
429 SOCKBUF_LOCK_ASSERT(sb);
432 * When a thread is passed, we take into account the thread's socket
433 * buffer size limit. The caller will generally pass curthread, but
434 * in the TCP input path, NULL will be passed to indicate that no
435 * appropriate thread resource limits are available. In that case,
436 * we don't apply a process limit.
441 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
443 sbsize_limit = RLIM_INFINITY;
444 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
447 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
448 if (sb->sb_lowat > sb->sb_hiwat)
449 sb->sb_lowat = sb->sb_hiwat;
454 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
460 error = sbreserve_locked(sb, cc, so, td);
466 * Free mbufs held by a socket, and reserved mbuf space.
469 sbrelease_internal(struct sockbuf *sb, struct socket *so)
472 sbflush_internal(sb);
473 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
479 sbrelease_locked(struct sockbuf *sb, struct socket *so)
482 SOCKBUF_LOCK_ASSERT(sb);
484 sbrelease_internal(sb, so);
488 sbrelease(struct sockbuf *sb, struct socket *so)
492 sbrelease_locked(sb, so);
497 sbdestroy(struct sockbuf *sb, struct socket *so)
500 sbrelease_internal(sb, so);
504 * Routines to add and remove data from an mbuf queue.
506 * The routines sbappend() or sbappendrecord() are normally called to append
507 * new mbufs to a socket buffer, after checking that adequate space is
508 * available, comparing the function sbspace() with the amount of data to be
509 * added. sbappendrecord() differs from sbappend() in that data supplied is
510 * treated as the beginning of a new record. To place a sender's address,
511 * optional access rights, and data in a socket receive buffer,
512 * sbappendaddr() should be used. To place access rights and data in a
513 * socket receive buffer, sbappendrights() should be used. In either case,
514 * the new data begins a new record. Note that unlike sbappend() and
515 * sbappendrecord(), these routines check for the caller that there will be
516 * enough space to store the data. Each fails if there is not enough space,
517 * or if it cannot find mbufs to store additional information in.
519 * Reliable protocols may use the socket send buffer to hold data awaiting
520 * acknowledgement. Data is normally copied from a socket send buffer in a
521 * protocol with m_copy for output to a peer, and then removing the data from
522 * the socket buffer with sbdrop() or sbdroprecord() when the data is
523 * acknowledged by the peer.
527 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
529 struct mbuf *m = sb->sb_mb;
531 SOCKBUF_LOCK_ASSERT(sb);
533 while (m && m->m_nextpkt)
536 if (m != sb->sb_lastrecord) {
537 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
538 __func__, sb->sb_mb, sb->sb_lastrecord, m);
539 printf("packet chain:\n");
540 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
542 panic("%s from %s:%u", __func__, file, line);
547 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
549 struct mbuf *m = sb->sb_mb;
552 SOCKBUF_LOCK_ASSERT(sb);
554 while (m && m->m_nextpkt)
557 while (m && m->m_next)
560 if (m != sb->sb_mbtail) {
561 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
562 __func__, sb->sb_mb, sb->sb_mbtail, m);
563 printf("packet tree:\n");
564 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
566 for (n = m; n != NULL; n = n->m_next)
570 panic("%s from %s:%u", __func__, file, line);
573 #endif /* SOCKBUF_DEBUG */
575 #define SBLINKRECORD(sb, m0) do { \
576 SOCKBUF_LOCK_ASSERT(sb); \
577 if ((sb)->sb_lastrecord != NULL) \
578 (sb)->sb_lastrecord->m_nextpkt = (m0); \
580 (sb)->sb_mb = (m0); \
581 (sb)->sb_lastrecord = (m0); \
582 } while (/*CONSTCOND*/0)
585 * Append mbuf chain m to the last record in the socket buffer sb. The
586 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
587 * are discarded and mbufs are compacted where possible.
590 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
594 SOCKBUF_LOCK_ASSERT(sb);
598 sbm_clrprotoflags(m, flags);
605 if (n->m_flags & M_EOR) {
606 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
609 } while (n->m_next && (n = n->m_next));
612 * XXX Would like to simply use sb_mbtail here, but
613 * XXX I need to verify that I won't miss an EOR that
616 if ((n = sb->sb_lastrecord) != NULL) {
618 if (n->m_flags & M_EOR) {
619 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
622 } while (n->m_next && (n = n->m_next));
625 * If this is the first record in the socket buffer,
626 * it's also the last record.
628 sb->sb_lastrecord = m;
631 sbcompress(sb, m, n);
636 * Append mbuf chain m to the last record in the socket buffer sb. The
637 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
638 * are discarded and mbufs are compacted where possible.
641 sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
645 sbappend_locked(sb, m, flags);
650 * This version of sbappend() should only be used when the caller absolutely
651 * knows that there will never be more than one record in the socket buffer,
652 * that is, a stream protocol (such as TCP).
655 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
657 SOCKBUF_LOCK_ASSERT(sb);
659 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
660 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
664 /* Remove all packet headers and mbuf tags to get a pure data chain. */
665 m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0);
667 sbcompress(sb, m, sb->sb_mbtail);
669 sb->sb_lastrecord = sb->sb_mb;
674 * This version of sbappend() should only be used when the caller absolutely
675 * knows that there will never be more than one record in the socket buffer,
676 * that is, a stream protocol (such as TCP).
679 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags)
683 sbappendstream_locked(sb, m, flags);
689 sbcheck(struct sockbuf *sb, const char *file, int line)
691 struct mbuf *m, *n, *fnrdy;
692 u_long acc, ccc, mbcnt;
694 SOCKBUF_LOCK_ASSERT(sb);
696 acc = ccc = mbcnt = 0;
699 for (m = sb->sb_mb; m; m = n) {
701 for (; m; m = m->m_next) {
703 printf("sb %p empty mbuf %p\n", sb, m);
706 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) {
707 if (m != sb->sb_fnrdy) {
708 printf("sb %p: fnrdy %p != m %p\n",
709 sb, sb->sb_fnrdy, m);
715 if (!(m->m_flags & M_NOTAVAIL)) {
716 printf("sb %p: fnrdy %p, m %p is avail\n",
717 sb, sb->sb_fnrdy, m);
724 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
725 mbcnt += m->m_ext.ext_size;
728 if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) {
729 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n",
730 acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt);
735 panic("%s from %s:%u", __func__, file, line);
740 * As above, except the mbuf chain begins a new record.
743 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
747 SOCKBUF_LOCK_ASSERT(sb);
753 * Put the first mbuf on the queue. Note this permits zero length
758 SBLINKRECORD(sb, m0);
762 if (m && (m0->m_flags & M_EOR)) {
763 m0->m_flags &= ~M_EOR;
766 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
767 sbcompress(sb, m, m0);
771 * As above, except the mbuf chain begins a new record.
774 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
778 sbappendrecord_locked(sb, m0);
782 /* Helper routine that appends data, control, and address to a sockbuf. */
784 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
785 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
787 struct mbuf *m, *n, *nlast;
789 if (asa->sa_len > MLEN)
792 m = m_get(M_NOWAIT, MT_SONAME);
795 m->m_len = asa->sa_len;
796 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
800 ctrl_last->m_next = m0; /* concatenate data to control */
804 for (n = m; n->m_next != NULL; n = n->m_next)
810 sb->sb_mbtail = nlast;
818 * Append address and data, and optionally, control (ancillary) data to the
819 * receive queue of a socket. If present, m0 must include a packet header
820 * with total length. Returns 0 if no space in sockbuf or insufficient
824 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
825 struct mbuf *m0, struct mbuf *control)
827 struct mbuf *ctrl_last;
828 int space = asa->sa_len;
830 SOCKBUF_LOCK_ASSERT(sb);
832 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
833 panic("sbappendaddr_locked");
835 space += m0->m_pkthdr.len;
836 space += m_length(control, &ctrl_last);
838 if (space > sbspace(sb))
840 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
844 * Append address and data, and optionally, control (ancillary) data to the
845 * receive queue of a socket. If present, m0 must include a packet header
846 * with total length. Returns 0 if insufficient mbufs. Does not validate space
847 * on the receiving sockbuf.
850 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
851 struct mbuf *m0, struct mbuf *control)
853 struct mbuf *ctrl_last;
855 SOCKBUF_LOCK_ASSERT(sb);
857 ctrl_last = (control == NULL) ? NULL : m_last(control);
858 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
862 * Append address and data, and optionally, control (ancillary) data to the
863 * receive queue of a socket. If present, m0 must include a packet header
864 * with total length. Returns 0 if no space in sockbuf or insufficient
868 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
869 struct mbuf *m0, struct mbuf *control)
874 retval = sbappendaddr_locked(sb, asa, m0, control);
880 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
881 struct mbuf *control)
883 struct mbuf *m, *n, *mlast;
886 SOCKBUF_LOCK_ASSERT(sb);
889 panic("sbappendcontrol_locked");
890 space = m_length(control, &n) + m_length(m0, NULL);
892 if (space > sbspace(sb))
895 n->m_next = m0; /* concatenate data to control */
899 for (m = control; m->m_next; m = m->m_next)
903 SBLINKRECORD(sb, control);
905 sb->sb_mbtail = mlast;
913 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
918 retval = sbappendcontrol_locked(sb, m0, control);
924 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
925 * (n). If (n) is NULL, the buffer is presumed empty.
927 * When the data is compressed, mbufs in the chain may be handled in one of
930 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
931 * record boundary, and no change in data type).
933 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
934 * an mbuf already in the socket buffer. This can occur if an
935 * appropriate mbuf exists, there is room, both mbufs are not marked as
936 * not ready, and no merging of data types will occur.
938 * (3) The mbuf may be appended to the end of the existing mbuf chain.
940 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
944 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
949 SOCKBUF_LOCK_ASSERT(sb);
952 eor |= m->m_flags & M_EOR;
955 (((o = m->m_next) || (o = n)) &&
956 o->m_type == m->m_type))) {
957 if (sb->sb_lastrecord == m)
958 sb->sb_lastrecord = m->m_next;
962 if (n && (n->m_flags & M_EOR) == 0 &&
964 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
965 !(m->m_flags & M_NOTREADY) &&
966 !(n->m_flags & M_NOTREADY) &&
967 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
968 m->m_len <= M_TRAILINGSPACE(n) &&
969 n->m_type == m->m_type) {
970 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
972 n->m_len += m->m_len;
973 sb->sb_ccc += m->m_len;
974 if (sb->sb_fnrdy == NULL)
975 sb->sb_acc += m->m_len;
976 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
977 /* XXX: Probably don't need.*/
978 sb->sb_ctl += m->m_len;
989 m->m_flags &= ~M_EOR;
994 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1001 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
1004 sbflush_internal(struct sockbuf *sb)
1007 while (sb->sb_mbcnt) {
1009 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
1010 * we would loop forever. Panic instead.
1012 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1014 m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
1016 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
1017 ("%s: ccc %u mb %p mbcnt %u", __func__,
1018 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1022 sbflush_locked(struct sockbuf *sb)
1025 SOCKBUF_LOCK_ASSERT(sb);
1026 sbflush_internal(sb);
1030 sbflush(struct sockbuf *sb)
1039 * Cut data from (the front of) a sockbuf.
1041 static struct mbuf *
1042 sbcut_internal(struct sockbuf *sb, int len)
1044 struct mbuf *m, *next, *mfree;
1046 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1051 KASSERT(next, ("%s: no next, len %d", __func__, len));
1053 next = m->m_nextpkt;
1055 if (m->m_len > len) {
1056 KASSERT(!(m->m_flags & M_NOTAVAIL),
1057 ("%s: m %p M_NOTAVAIL", __func__, m));
1062 if (sb->sb_sndptroff != 0)
1063 sb->sb_sndptroff -= len;
1064 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1071 * Do not put M_NOTREADY buffers to the free list, they
1072 * are referenced from outside.
1074 if (m->m_flags & M_NOTREADY)
1086 * Free any zero-length mbufs from the buffer.
1087 * For SOCK_DGRAM sockets such mbufs represent empty records.
1088 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1089 * when sosend_generic() needs to send only control data.
1091 while (m && m->m_len == 0) {
1102 m->m_nextpkt = next;
1106 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
1107 * sb_lastrecord is up-to-date if we dropped part of the last record.
1111 sb->sb_mbtail = NULL;
1112 sb->sb_lastrecord = NULL;
1113 } else if (m->m_nextpkt == NULL) {
1114 sb->sb_lastrecord = m;
1121 * Drop data from (the front of) a sockbuf.
1124 sbdrop_locked(struct sockbuf *sb, int len)
1127 SOCKBUF_LOCK_ASSERT(sb);
1128 m_freem(sbcut_internal(sb, len));
1132 * Drop data from (the front of) a sockbuf,
1133 * and return it to caller.
1136 sbcut_locked(struct sockbuf *sb, int len)
1139 SOCKBUF_LOCK_ASSERT(sb);
1140 return (sbcut_internal(sb, len));
1144 sbdrop(struct sockbuf *sb, int len)
1149 mfree = sbcut_internal(sb, len);
1156 * Maintain a pointer and offset pair into the socket buffer mbuf chain to
1157 * avoid traversal of the entire socket buffer for larger offsets.
1160 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
1162 struct mbuf *m, *ret;
1164 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1165 KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__));
1166 KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__));
1169 * Is off below stored offset? Happens on retransmits.
1170 * Just return, we can't help here.
1172 if (sb->sb_sndptroff > off) {
1177 /* Return closest mbuf in chain for current offset. */
1178 *moff = off - sb->sb_sndptroff;
1179 m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
1180 if (*moff == m->m_len) {
1182 sb->sb_sndptroff += m->m_len;
1183 m = ret = m->m_next;
1184 KASSERT(ret->m_len > 0,
1185 ("mbuf %p in sockbuf %p chain has no valid data", ret, sb));
1188 /* Advance by len to be as close as possible for the next transmit. */
1189 for (off = off - sb->sb_sndptroff + len - 1;
1190 off > 0 && m != NULL && off >= m->m_len;
1192 sb->sb_sndptroff += m->m_len;
1195 if (off > 0 && m == NULL)
1196 panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret);
1203 * Return the first mbuf and the mbuf data offset for the provided
1204 * send offset without changing the "sb_sndptroff" field.
1207 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1211 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1214 * If the "off" is below the stored offset, which happens on
1215 * retransmits, just use "sb_mb":
1217 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1221 off -= sb->sb_sndptroff;
1223 while (off > 0 && m != NULL) {
1234 * Drop a record off the front of a sockbuf and move the next record to the
1238 sbdroprecord_locked(struct sockbuf *sb)
1242 SOCKBUF_LOCK_ASSERT(sb);
1246 sb->sb_mb = m->m_nextpkt;
1256 * Drop a record off the front of a sockbuf and move the next record to the
1260 sbdroprecord(struct sockbuf *sb)
1264 sbdroprecord_locked(sb);
1269 * Create a "control" mbuf containing the specified data with the specified
1270 * type for presentation on a socket buffer.
1273 sbcreatecontrol(caddr_t p, int size, int type, int level)
1278 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1279 return ((struct mbuf *) NULL);
1280 if (CMSG_SPACE((u_int)size) > MLEN)
1281 m = m_getcl(M_NOWAIT, MT_CONTROL, 0);
1283 m = m_get(M_NOWAIT, MT_CONTROL);
1285 return ((struct mbuf *) NULL);
1286 cp = mtod(m, struct cmsghdr *);
1288 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1289 ("sbcreatecontrol: short mbuf"));
1291 * Don't leave the padding between the msg header and the
1292 * cmsg data and the padding after the cmsg data un-initialized.
1294 bzero(cp, CMSG_SPACE((u_int)size));
1296 (void)memcpy(CMSG_DATA(cp), p, size);
1297 m->m_len = CMSG_SPACE(size);
1298 cp->cmsg_len = CMSG_LEN(size);
1299 cp->cmsg_level = level;
1300 cp->cmsg_type = type;
1305 * This does the same for socket buffers that sotoxsocket does for sockets:
1306 * generate an user-format data structure describing the socket buffer. Note
1307 * that the xsockbuf structure, since it is always embedded in a socket, does
1308 * not include a self pointer nor a length. We make this entry point public
1309 * in case some other mechanism needs it.
1312 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1315 xsb->sb_cc = sb->sb_ccc;
1316 xsb->sb_hiwat = sb->sb_hiwat;
1317 xsb->sb_mbcnt = sb->sb_mbcnt;
1318 xsb->sb_mcnt = sb->sb_mcnt;
1319 xsb->sb_ccnt = sb->sb_ccnt;
1320 xsb->sb_mbmax = sb->sb_mbmax;
1321 xsb->sb_lowat = sb->sb_lowat;
1322 xsb->sb_flags = sb->sb_flags;
1323 xsb->sb_timeo = sb->sb_timeo;
1326 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1328 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1329 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1330 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1331 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1332 &sb_efficiency, 0, "Socket buffer size waste factor");