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 * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
75 sbm_clrprotoflags(struct mbuf *m, int flags)
80 if (flags & PRUS_NOTREADY)
89 * Mark ready "count" mbufs starting with "m".
92 sbready(struct sockbuf *sb, struct mbuf *m, int count)
96 SOCKBUF_LOCK_ASSERT(sb);
97 KASSERT(sb->sb_fnrdy != NULL, ("%s: sb %p NULL fnrdy", __func__, sb));
99 blocker = (sb->sb_fnrdy == m) ? M_BLOCKED : 0;
101 for (int i = 0; i < count; i++, m = m->m_next) {
102 KASSERT(m->m_flags & M_NOTREADY,
103 ("%s: m %p !M_NOTREADY", __func__, m));
104 m->m_flags &= ~(M_NOTREADY | blocker);
106 sb->sb_acc += m->m_len;
110 return (EINPROGRESS);
112 /* This one was blocking all the queue. */
113 for (; m && (m->m_flags & M_NOTREADY) == 0; m = m->m_next) {
114 KASSERT(m->m_flags & M_BLOCKED,
115 ("%s: m %p !M_BLOCKED", __func__, m));
116 m->m_flags &= ~M_BLOCKED;
117 sb->sb_acc += m->m_len;
126 * Adjust sockbuf state reflecting allocation of m.
129 sballoc(struct sockbuf *sb, struct mbuf *m)
132 SOCKBUF_LOCK_ASSERT(sb);
134 sb->sb_ccc += m->m_len;
136 if (sb->sb_fnrdy == NULL) {
137 if (m->m_flags & M_NOTREADY)
140 sb->sb_acc += m->m_len;
142 m->m_flags |= M_BLOCKED;
144 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
145 sb->sb_ctl += m->m_len;
147 sb->sb_mbcnt += MSIZE;
150 if (m->m_flags & M_EXT) {
151 sb->sb_mbcnt += m->m_ext.ext_size;
157 * Adjust sockbuf state reflecting freeing of m.
160 sbfree(struct sockbuf *sb, struct mbuf *m)
163 #if 0 /* XXX: not yet: soclose() call path comes here w/o lock. */
164 SOCKBUF_LOCK_ASSERT(sb);
167 sb->sb_ccc -= m->m_len;
169 if (!(m->m_flags & M_NOTAVAIL))
170 sb->sb_acc -= m->m_len;
172 if (m == sb->sb_fnrdy) {
175 KASSERT(m->m_flags & M_NOTREADY,
176 ("%s: m %p !M_NOTREADY", __func__, m));
179 while (n != NULL && !(n->m_flags & M_NOTREADY)) {
180 n->m_flags &= ~M_BLOCKED;
181 sb->sb_acc += n->m_len;
187 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
188 sb->sb_ctl -= m->m_len;
190 sb->sb_mbcnt -= MSIZE;
192 if (m->m_flags & M_EXT) {
193 sb->sb_mbcnt -= m->m_ext.ext_size;
197 if (sb->sb_sndptr == m) {
198 sb->sb_sndptr = NULL;
199 sb->sb_sndptroff = 0;
201 if (sb->sb_sndptroff != 0)
202 sb->sb_sndptroff -= m->m_len;
206 * Socantsendmore indicates that no more data will be sent on the socket; it
207 * would normally be applied to a socket when the user informs the system
208 * that no more data is to be sent, by the protocol code (in case
209 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
210 * received, and will normally be applied to the socket by a protocol when it
211 * detects that the peer will send no more data. Data queued for reading in
212 * the socket may yet be read.
215 socantsendmore_locked(struct socket *so)
218 SOCKBUF_LOCK_ASSERT(&so->so_snd);
220 so->so_snd.sb_state |= SBS_CANTSENDMORE;
221 sowwakeup_locked(so);
222 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
226 socantsendmore(struct socket *so)
229 SOCKBUF_LOCK(&so->so_snd);
230 socantsendmore_locked(so);
231 mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
235 socantrcvmore_locked(struct socket *so)
238 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
240 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
241 sorwakeup_locked(so);
242 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
246 socantrcvmore(struct socket *so)
249 SOCKBUF_LOCK(&so->so_rcv);
250 socantrcvmore_locked(so);
251 mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
255 * Wait for data to arrive at/drain from a socket buffer.
258 sbwait(struct sockbuf *sb)
261 SOCKBUF_LOCK_ASSERT(sb);
263 sb->sb_flags |= SB_WAIT;
264 return (msleep_sbt(&sb->sb_acc, &sb->sb_mtx,
265 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
266 sb->sb_timeo, 0, 0));
270 sblock(struct sockbuf *sb, int flags)
273 KASSERT((flags & SBL_VALID) == flags,
274 ("sblock: flags invalid (0x%x)", flags));
276 if (flags & SBL_WAIT) {
277 if ((sb->sb_flags & SB_NOINTR) ||
278 (flags & SBL_NOINTR)) {
279 sx_xlock(&sb->sb_sx);
282 return (sx_xlock_sig(&sb->sb_sx));
284 if (sx_try_xlock(&sb->sb_sx) == 0)
285 return (EWOULDBLOCK);
291 sbunlock(struct sockbuf *sb)
294 sx_xunlock(&sb->sb_sx);
298 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
299 * via SIGIO if the socket has the SS_ASYNC flag set.
301 * Called with the socket buffer lock held; will release the lock by the end
302 * of the function. This allows the caller to acquire the socket buffer lock
303 * while testing for the need for various sorts of wakeup and hold it through
304 * to the point where it's no longer required. We currently hold the lock
305 * through calls out to other subsystems (with the exception of kqueue), and
306 * then release it to avoid lock order issues. It's not clear that's
310 sowakeup(struct socket *so, struct sockbuf *sb)
314 SOCKBUF_LOCK_ASSERT(sb);
316 selwakeuppri(&sb->sb_sel, PSOCK);
317 if (!SEL_WAITING(&sb->sb_sel))
318 sb->sb_flags &= ~SB_SEL;
319 if (sb->sb_flags & SB_WAIT) {
320 sb->sb_flags &= ~SB_WAIT;
323 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
324 if (sb->sb_upcall != NULL) {
325 ret = sb->sb_upcall(so, sb->sb_upcallarg, M_NOWAIT);
326 if (ret == SU_ISCONNECTED) {
327 KASSERT(sb == &so->so_rcv,
328 ("SO_SND upcall returned SU_ISCONNECTED"));
329 soupcall_clear(so, SO_RCV);
333 if (sb->sb_flags & SB_AIO)
336 if (ret == SU_ISCONNECTED)
338 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
339 pgsigio(&so->so_sigio, SIGIO, 0);
340 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
344 * Socket buffer (struct sockbuf) utility routines.
346 * Each socket contains two socket buffers: one for sending data and one for
347 * receiving data. Each buffer contains a queue of mbufs, information about
348 * the number of mbufs and amount of data in the queue, and other fields
349 * allowing select() statements and notification on data availability to be
352 * Data stored in a socket buffer is maintained as a list of records. Each
353 * record is a list of mbufs chained together with the m_next field. Records
354 * are chained together with the m_nextpkt field. The upper level routine
355 * soreceive() expects the following conventions to be observed when placing
356 * information in the receive buffer:
358 * 1. If the protocol requires each message be preceded by the sender's name,
359 * then a record containing that name must be present before any
360 * associated data (mbuf's must be of type MT_SONAME).
361 * 2. If the protocol supports the exchange of ``access rights'' (really just
362 * additional data associated with the message), and there are ``rights''
363 * to be received, then a record containing this data should be present
364 * (mbuf's must be of type MT_RIGHTS).
365 * 3. If a name or rights record exists, then it must be followed by a data
366 * record, perhaps of zero length.
368 * Before using a new socket structure it is first necessary to reserve
369 * buffer space to the socket, by calling sbreserve(). This should commit
370 * some of the available buffer space in the system buffer pool for the
371 * socket (currently, it does nothing but enforce limits). The space should
372 * be released by calling sbrelease() when the socket is destroyed.
375 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
377 struct thread *td = curthread;
379 SOCKBUF_LOCK(&so->so_snd);
380 SOCKBUF_LOCK(&so->so_rcv);
381 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
383 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
385 if (so->so_rcv.sb_lowat == 0)
386 so->so_rcv.sb_lowat = 1;
387 if (so->so_snd.sb_lowat == 0)
388 so->so_snd.sb_lowat = MCLBYTES;
389 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
390 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
391 SOCKBUF_UNLOCK(&so->so_rcv);
392 SOCKBUF_UNLOCK(&so->so_snd);
395 sbrelease_locked(&so->so_snd, so);
397 SOCKBUF_UNLOCK(&so->so_rcv);
398 SOCKBUF_UNLOCK(&so->so_snd);
403 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
406 u_long tmp_sb_max = sb_max;
408 error = sysctl_handle_long(oidp, &tmp_sb_max, arg2, req);
409 if (error || !req->newptr)
411 if (tmp_sb_max < MSIZE + MCLBYTES)
414 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
419 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
420 * become limiting if buffering efficiency is near the normal case.
423 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
428 SOCKBUF_LOCK_ASSERT(sb);
431 * When a thread is passed, we take into account the thread's socket
432 * buffer size limit. The caller will generally pass curthread, but
433 * in the TCP input path, NULL will be passed to indicate that no
434 * appropriate thread resource limits are available. In that case,
435 * we don't apply a process limit.
440 sbsize_limit = lim_cur(td, RLIMIT_SBSIZE);
442 sbsize_limit = RLIM_INFINITY;
443 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
446 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
447 if (sb->sb_lowat > sb->sb_hiwat)
448 sb->sb_lowat = sb->sb_hiwat;
453 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
459 error = sbreserve_locked(sb, cc, so, td);
465 * Free mbufs held by a socket, and reserved mbuf space.
468 sbrelease_internal(struct sockbuf *sb, struct socket *so)
471 sbflush_internal(sb);
472 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
478 sbrelease_locked(struct sockbuf *sb, struct socket *so)
481 SOCKBUF_LOCK_ASSERT(sb);
483 sbrelease_internal(sb, so);
487 sbrelease(struct sockbuf *sb, struct socket *so)
491 sbrelease_locked(sb, so);
496 sbdestroy(struct sockbuf *sb, struct socket *so)
499 sbrelease_internal(sb, so);
503 * Routines to add and remove data from an mbuf queue.
505 * The routines sbappend() or sbappendrecord() are normally called to append
506 * new mbufs to a socket buffer, after checking that adequate space is
507 * available, comparing the function sbspace() with the amount of data to be
508 * added. sbappendrecord() differs from sbappend() in that data supplied is
509 * treated as the beginning of a new record. To place a sender's address,
510 * optional access rights, and data in a socket receive buffer,
511 * sbappendaddr() should be used. To place access rights and data in a
512 * socket receive buffer, sbappendrights() should be used. In either case,
513 * the new data begins a new record. Note that unlike sbappend() and
514 * sbappendrecord(), these routines check for the caller that there will be
515 * enough space to store the data. Each fails if there is not enough space,
516 * or if it cannot find mbufs to store additional information in.
518 * Reliable protocols may use the socket send buffer to hold data awaiting
519 * acknowledgement. Data is normally copied from a socket send buffer in a
520 * protocol with m_copy for output to a peer, and then removing the data from
521 * the socket buffer with sbdrop() or sbdroprecord() when the data is
522 * acknowledged by the peer.
526 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
528 struct mbuf *m = sb->sb_mb;
530 SOCKBUF_LOCK_ASSERT(sb);
532 while (m && m->m_nextpkt)
535 if (m != sb->sb_lastrecord) {
536 printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
537 __func__, sb->sb_mb, sb->sb_lastrecord, m);
538 printf("packet chain:\n");
539 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
541 panic("%s from %s:%u", __func__, file, line);
546 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
548 struct mbuf *m = sb->sb_mb;
551 SOCKBUF_LOCK_ASSERT(sb);
553 while (m && m->m_nextpkt)
556 while (m && m->m_next)
559 if (m != sb->sb_mbtail) {
560 printf("%s: sb_mb %p sb_mbtail %p last %p\n",
561 __func__, sb->sb_mb, sb->sb_mbtail, m);
562 printf("packet tree:\n");
563 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
565 for (n = m; n != NULL; n = n->m_next)
569 panic("%s from %s:%u", __func__, file, line);
572 #endif /* SOCKBUF_DEBUG */
574 #define SBLINKRECORD(sb, m0) do { \
575 SOCKBUF_LOCK_ASSERT(sb); \
576 if ((sb)->sb_lastrecord != NULL) \
577 (sb)->sb_lastrecord->m_nextpkt = (m0); \
579 (sb)->sb_mb = (m0); \
580 (sb)->sb_lastrecord = (m0); \
581 } while (/*CONSTCOND*/0)
584 * Append mbuf chain m to the last record in the socket buffer sb. The
585 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
586 * are discarded and mbufs are compacted where possible.
589 sbappend_locked(struct sockbuf *sb, struct mbuf *m, int flags)
593 SOCKBUF_LOCK_ASSERT(sb);
597 sbm_clrprotoflags(m, flags);
604 if (n->m_flags & M_EOR) {
605 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
608 } while (n->m_next && (n = n->m_next));
611 * XXX Would like to simply use sb_mbtail here, but
612 * XXX I need to verify that I won't miss an EOR that
615 if ((n = sb->sb_lastrecord) != NULL) {
617 if (n->m_flags & M_EOR) {
618 sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
621 } while (n->m_next && (n = n->m_next));
624 * If this is the first record in the socket buffer,
625 * it's also the last record.
627 sb->sb_lastrecord = m;
630 sbcompress(sb, m, n);
635 * Append mbuf chain m to the last record in the socket buffer sb. The
636 * additional space associated the mbuf chain is recorded in sb. Empty mbufs
637 * are discarded and mbufs are compacted where possible.
640 sbappend(struct sockbuf *sb, struct mbuf *m, int flags)
644 sbappend_locked(sb, m, flags);
649 * This version of sbappend() should only be used when the caller absolutely
650 * knows that there will never be more than one record in the socket buffer,
651 * that is, a stream protocol (such as TCP).
654 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m, int flags)
656 SOCKBUF_LOCK_ASSERT(sb);
658 KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
659 KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
663 /* Remove all packet headers and mbuf tags to get a pure data chain. */
664 m_demote(m, 1, flags & PRUS_NOTREADY ? M_NOTREADY : 0);
666 sbcompress(sb, m, sb->sb_mbtail);
668 sb->sb_lastrecord = sb->sb_mb;
673 * This version of sbappend() should only be used when the caller absolutely
674 * knows that there will never be more than one record in the socket buffer,
675 * that is, a stream protocol (such as TCP).
678 sbappendstream(struct sockbuf *sb, struct mbuf *m, int flags)
682 sbappendstream_locked(sb, m, flags);
688 sbcheck(struct sockbuf *sb, const char *file, int line)
690 struct mbuf *m, *n, *fnrdy;
691 u_long acc, ccc, mbcnt;
693 SOCKBUF_LOCK_ASSERT(sb);
695 acc = ccc = mbcnt = 0;
698 for (m = sb->sb_mb; m; m = n) {
700 for (; m; m = m->m_next) {
702 printf("sb %p empty mbuf %p\n", sb, m);
705 if ((m->m_flags & M_NOTREADY) && fnrdy == NULL) {
706 if (m != sb->sb_fnrdy) {
707 printf("sb %p: fnrdy %p != m %p\n",
708 sb, sb->sb_fnrdy, m);
714 if (!(m->m_flags & M_NOTAVAIL)) {
715 printf("sb %p: fnrdy %p, m %p is avail\n",
716 sb, sb->sb_fnrdy, m);
723 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
724 mbcnt += m->m_ext.ext_size;
727 if (acc != sb->sb_acc || ccc != sb->sb_ccc || mbcnt != sb->sb_mbcnt) {
728 printf("acc %ld/%u ccc %ld/%u mbcnt %ld/%u\n",
729 acc, sb->sb_acc, ccc, sb->sb_ccc, mbcnt, sb->sb_mbcnt);
734 panic("%s from %s:%u", __func__, file, line);
739 * As above, except the mbuf chain begins a new record.
742 sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0)
746 SOCKBUF_LOCK_ASSERT(sb);
752 * Put the first mbuf on the queue. Note this permits zero length
757 SBLINKRECORD(sb, m0);
761 if (m && (m0->m_flags & M_EOR)) {
762 m0->m_flags &= ~M_EOR;
765 /* always call sbcompress() so it can do SBLASTMBUFCHK() */
766 sbcompress(sb, m, m0);
770 * As above, except the mbuf chain begins a new record.
773 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
777 sbappendrecord_locked(sb, m0);
781 /* Helper routine that appends data, control, and address to a sockbuf. */
783 sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa,
784 struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last)
786 struct mbuf *m, *n, *nlast;
788 if (asa->sa_len > MLEN)
791 m = m_get(M_NOWAIT, MT_SONAME);
794 m->m_len = asa->sa_len;
795 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
799 ctrl_last->m_next = m0; /* concatenate data to control */
803 for (n = m; n->m_next != NULL; n = n->m_next)
809 sb->sb_mbtail = nlast;
817 * Append address and data, and optionally, control (ancillary) data to the
818 * receive queue of a socket. If present, m0 must include a packet header
819 * with total length. Returns 0 if no space in sockbuf or insufficient
823 sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
824 struct mbuf *m0, struct mbuf *control)
826 struct mbuf *ctrl_last;
827 int space = asa->sa_len;
829 SOCKBUF_LOCK_ASSERT(sb);
831 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
832 panic("sbappendaddr_locked");
834 space += m0->m_pkthdr.len;
835 space += m_length(control, &ctrl_last);
837 if (space > sbspace(sb))
839 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
843 * Append address and data, and optionally, control (ancillary) data to the
844 * receive queue of a socket. If present, m0 must include a packet header
845 * with total length. Returns 0 if insufficient mbufs. Does not validate space
846 * on the receiving sockbuf.
849 sbappendaddr_nospacecheck_locked(struct sockbuf *sb, const struct sockaddr *asa,
850 struct mbuf *m0, struct mbuf *control)
852 struct mbuf *ctrl_last;
854 SOCKBUF_LOCK_ASSERT(sb);
856 ctrl_last = (control == NULL) ? NULL : m_last(control);
857 return (sbappendaddr_locked_internal(sb, asa, m0, control, ctrl_last));
861 * Append address and data, and optionally, control (ancillary) data to the
862 * receive queue of a socket. If present, m0 must include a packet header
863 * with total length. Returns 0 if no space in sockbuf or insufficient
867 sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
868 struct mbuf *m0, struct mbuf *control)
873 retval = sbappendaddr_locked(sb, asa, m0, control);
879 sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
880 struct mbuf *control)
882 struct mbuf *m, *n, *mlast;
885 SOCKBUF_LOCK_ASSERT(sb);
888 panic("sbappendcontrol_locked");
889 space = m_length(control, &n) + m_length(m0, NULL);
891 if (space > sbspace(sb))
894 n->m_next = m0; /* concatenate data to control */
898 for (m = control; m->m_next; m = m->m_next)
902 SBLINKRECORD(sb, control);
904 sb->sb_mbtail = mlast;
912 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
917 retval = sbappendcontrol_locked(sb, m0, control);
923 * Append the data in mbuf chain (m) into the socket buffer sb following mbuf
924 * (n). If (n) is NULL, the buffer is presumed empty.
926 * When the data is compressed, mbufs in the chain may be handled in one of
929 * (1) The mbuf may simply be dropped, if it contributes nothing (no data, no
930 * record boundary, and no change in data type).
932 * (2) The mbuf may be coalesced -- i.e., data in the mbuf may be copied into
933 * an mbuf already in the socket buffer. This can occur if an
934 * appropriate mbuf exists, there is room, both mbufs are not marked as
935 * not ready, and no merging of data types will occur.
937 * (3) The mbuf may be appended to the end of the existing mbuf chain.
939 * If any of the new mbufs is marked as M_EOR, mark the last mbuf appended as
943 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
948 SOCKBUF_LOCK_ASSERT(sb);
951 eor |= m->m_flags & M_EOR;
954 (((o = m->m_next) || (o = n)) &&
955 o->m_type == m->m_type))) {
956 if (sb->sb_lastrecord == m)
957 sb->sb_lastrecord = m->m_next;
961 if (n && (n->m_flags & M_EOR) == 0 &&
963 ((sb->sb_flags & SB_NOCOALESCE) == 0) &&
964 !(m->m_flags & M_NOTREADY) &&
965 !(n->m_flags & M_NOTREADY) &&
966 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
967 m->m_len <= M_TRAILINGSPACE(n) &&
968 n->m_type == m->m_type) {
969 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
971 n->m_len += m->m_len;
972 sb->sb_ccc += m->m_len;
973 if (sb->sb_fnrdy == NULL)
974 sb->sb_acc += m->m_len;
975 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
976 /* XXX: Probably don't need.*/
977 sb->sb_ctl += m->m_len;
988 m->m_flags &= ~M_EOR;
993 KASSERT(n != NULL, ("sbcompress: eor && n == NULL"));
1000 * Free all mbufs in a sockbuf. Check that all resources are reclaimed.
1003 sbflush_internal(struct sockbuf *sb)
1006 while (sb->sb_mbcnt) {
1008 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
1009 * we would loop forever. Panic instead.
1011 if (sb->sb_ccc == 0 && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1013 m_freem(sbcut_internal(sb, (int)sb->sb_ccc));
1015 KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
1016 ("%s: ccc %u mb %p mbcnt %u", __func__,
1017 sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
1021 sbflush_locked(struct sockbuf *sb)
1024 SOCKBUF_LOCK_ASSERT(sb);
1025 sbflush_internal(sb);
1029 sbflush(struct sockbuf *sb)
1038 * Cut data from (the front of) a sockbuf.
1040 static struct mbuf *
1041 sbcut_internal(struct sockbuf *sb, int len)
1043 struct mbuf *m, *next, *mfree;
1045 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1050 KASSERT(next, ("%s: no next, len %d", __func__, len));
1052 next = m->m_nextpkt;
1054 if (m->m_len > len) {
1055 KASSERT(!(m->m_flags & M_NOTAVAIL),
1056 ("%s: m %p M_NOTAVAIL", __func__, m));
1061 if (sb->sb_sndptroff != 0)
1062 sb->sb_sndptroff -= len;
1063 if (m->m_type != MT_DATA && m->m_type != MT_OOBDATA)
1070 * Do not put M_NOTREADY buffers to the free list, they
1071 * are referenced from outside.
1073 if (m->m_flags & M_NOTREADY)
1085 * Free any zero-length mbufs from the buffer.
1086 * For SOCK_DGRAM sockets such mbufs represent empty records.
1087 * XXX: For SOCK_STREAM sockets such mbufs can appear in the buffer,
1088 * when sosend_generic() needs to send only control data.
1090 while (m && m->m_len == 0) {
1101 m->m_nextpkt = next;
1105 * First part is an inline SB_EMPTY_FIXUP(). Second part makes sure
1106 * sb_lastrecord is up-to-date if we dropped part of the last record.
1110 sb->sb_mbtail = NULL;
1111 sb->sb_lastrecord = NULL;
1112 } else if (m->m_nextpkt == NULL) {
1113 sb->sb_lastrecord = m;
1120 * Drop data from (the front of) a sockbuf.
1123 sbdrop_locked(struct sockbuf *sb, int len)
1126 SOCKBUF_LOCK_ASSERT(sb);
1127 m_freem(sbcut_internal(sb, len));
1131 * Drop data from (the front of) a sockbuf,
1132 * and return it to caller.
1135 sbcut_locked(struct sockbuf *sb, int len)
1138 SOCKBUF_LOCK_ASSERT(sb);
1139 return (sbcut_internal(sb, len));
1143 sbdrop(struct sockbuf *sb, int len)
1148 mfree = sbcut_internal(sb, len);
1155 * Maintain a pointer and offset pair into the socket buffer mbuf chain to
1156 * avoid traversal of the entire socket buffer for larger offsets.
1159 sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff)
1161 struct mbuf *m, *ret;
1163 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1164 KASSERT(off + len <= sb->sb_acc, ("%s: beyond sb", __func__));
1165 KASSERT(sb->sb_sndptroff <= sb->sb_acc, ("%s: sndptroff broken", __func__));
1168 * Is off below stored offset? Happens on retransmits.
1169 * Just return, we can't help here.
1171 if (sb->sb_sndptroff > off) {
1176 /* Return closest mbuf in chain for current offset. */
1177 *moff = off - sb->sb_sndptroff;
1178 m = ret = sb->sb_sndptr ? sb->sb_sndptr : sb->sb_mb;
1179 if (*moff == m->m_len) {
1181 sb->sb_sndptroff += m->m_len;
1182 m = ret = m->m_next;
1183 KASSERT(ret->m_len > 0,
1184 ("mbuf %p in sockbuf %p chain has no valid data", ret, sb));
1187 /* Advance by len to be as close as possible for the next transmit. */
1188 for (off = off - sb->sb_sndptroff + len - 1;
1189 off > 0 && m != NULL && off >= m->m_len;
1191 sb->sb_sndptroff += m->m_len;
1194 if (off > 0 && m == NULL)
1195 panic("%s: sockbuf %p and mbuf %p clashing", __func__, sb, ret);
1202 * Return the first mbuf and the mbuf data offset for the provided
1203 * send offset without changing the "sb_sndptroff" field.
1206 sbsndmbuf(struct sockbuf *sb, u_int off, u_int *moff)
1210 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb is NULL", __func__));
1213 * If the "off" is below the stored offset, which happens on
1214 * retransmits, just use "sb_mb":
1216 if (sb->sb_sndptr == NULL || sb->sb_sndptroff > off) {
1220 off -= sb->sb_sndptroff;
1222 while (off > 0 && m != NULL) {
1233 * Drop a record off the front of a sockbuf and move the next record to the
1237 sbdroprecord_locked(struct sockbuf *sb)
1241 SOCKBUF_LOCK_ASSERT(sb);
1245 sb->sb_mb = m->m_nextpkt;
1255 * Drop a record off the front of a sockbuf and move the next record to the
1259 sbdroprecord(struct sockbuf *sb)
1263 sbdroprecord_locked(sb);
1268 * Create a "control" mbuf containing the specified data with the specified
1269 * type for presentation on a socket buffer.
1272 sbcreatecontrol(caddr_t p, int size, int type, int level)
1277 if (CMSG_SPACE((u_int)size) > MCLBYTES)
1278 return ((struct mbuf *) NULL);
1279 if (CMSG_SPACE((u_int)size) > MLEN)
1280 m = m_getcl(M_NOWAIT, MT_CONTROL, 0);
1282 m = m_get(M_NOWAIT, MT_CONTROL);
1284 return ((struct mbuf *) NULL);
1285 cp = mtod(m, struct cmsghdr *);
1287 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1288 ("sbcreatecontrol: short mbuf"));
1290 * Don't leave the padding between the msg header and the
1291 * cmsg data and the padding after the cmsg data un-initialized.
1293 bzero(cp, CMSG_SPACE((u_int)size));
1295 (void)memcpy(CMSG_DATA(cp), p, size);
1296 m->m_len = CMSG_SPACE(size);
1297 cp->cmsg_len = CMSG_LEN(size);
1298 cp->cmsg_level = level;
1299 cp->cmsg_type = type;
1304 * This does the same for socket buffers that sotoxsocket does for sockets:
1305 * generate an user-format data structure describing the socket buffer. Note
1306 * that the xsockbuf structure, since it is always embedded in a socket, does
1307 * not include a self pointer nor a length. We make this entry point public
1308 * in case some other mechanism needs it.
1311 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1314 xsb->sb_cc = sb->sb_ccc;
1315 xsb->sb_hiwat = sb->sb_hiwat;
1316 xsb->sb_mbcnt = sb->sb_mbcnt;
1317 xsb->sb_mcnt = sb->sb_mcnt;
1318 xsb->sb_ccnt = sb->sb_ccnt;
1319 xsb->sb_mbmax = sb->sb_mbmax;
1320 xsb->sb_lowat = sb->sb_lowat;
1321 xsb->sb_flags = sb->sb_flags;
1322 xsb->sb_timeo = sb->sb_timeo;
1325 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1327 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1328 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1329 &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1330 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1331 &sb_efficiency, 0, "Socket buffer size waste factor");