2 * Copyright (c) 1982, 1986, 1988, 1991, 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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
38 #include "opt_param.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
57 * sysctl(8) exported objects
59 SYSCTL_DECL(_kern_ipc);
60 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
62 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
63 &max_protohdr, 0, "");
64 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
65 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
69 * Copy mbuf pkthdr from "from" to "to".
70 * "from" must have M_PKTHDR set, and "to" must be empty.
71 * aux pointer will be moved to "to".
74 m_copy_pkthdr(struct mbuf *to, struct mbuf *from)
78 KASSERT(to->m_flags & M_PKTHDR,
79 ("m_copy_pkthdr() called on non-header"));
82 if (to->m_flags & M_PKTHDR)
85 to->m_data = to->m_pktdat;
86 to->m_flags = from->m_flags & M_COPYFLAGS;
87 to->m_pkthdr = from->m_pkthdr;
89 mac_init_mbuf(to, 1); /* XXXMAC no way to fail */
90 mac_create_mbuf_from_mbuf(from, to);
92 SLIST_INIT(&from->m_pkthdr.tags);
96 * Lesser-used path for M_PREPEND:
97 * allocate new mbuf to prepend to chain,
101 m_prepend(struct mbuf *m, int len, int how)
105 MGET(mn, how, m->m_type);
110 if (m->m_flags & M_PKTHDR) {
111 M_COPY_PKTHDR(mn, m);
115 m->m_flags &= ~M_PKTHDR;
126 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
127 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
128 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
129 * Note that the copy is read-only, because clusters are not copied,
130 * only their reference counts are incremented.
133 m_copym(struct mbuf *m, int off0, int len, int wait)
135 struct mbuf *n, **np;
140 KASSERT(off >= 0, ("m_copym, negative off %d", off));
141 KASSERT(len >= 0, ("m_copym, negative len %d", len));
142 if (off == 0 && m->m_flags & M_PKTHDR)
145 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
155 KASSERT(len == M_COPYALL,
156 ("m_copym, length > size of mbuf chain"));
159 MGET(n, wait, m->m_type);
165 if (len == M_COPYALL)
166 n->m_pkthdr.len -= off0;
168 n->m_pkthdr.len = len;
171 n->m_len = min(len, m->m_len - off);
172 if (m->m_flags & M_EXT) {
173 n->m_data = m->m_data + off;
178 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
180 if (len != M_COPYALL)
187 mbstat.m_mcfail++; /* XXX: No consistency. */
192 mbstat.m_mcfail++; /* XXX: No consistency. */
197 * Copy an entire packet, including header (which must be present).
198 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
199 * Note that the copy is read-only, because clusters are not copied,
200 * only their reference counts are incremented.
201 * Preserve alignment of the first mbuf so if the creator has left
202 * some room at the beginning (e.g. for inserting protocol headers)
203 * the copies still have the room available.
206 m_copypacket(struct mbuf *m, int how)
208 struct mbuf *top, *n, *o;
210 MGET(n, how, m->m_type);
217 if (m->m_flags & M_EXT) {
218 n->m_data = m->m_data;
223 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
224 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
229 MGET(o, how, m->m_type);
237 if (m->m_flags & M_EXT) {
238 n->m_data = m->m_data;
243 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
251 mbstat.m_mcfail++; /* XXX: No consistency. */
256 * Copy data from an mbuf chain starting "off" bytes from the beginning,
257 * continuing for "len" bytes, into the indicated buffer.
260 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
264 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
265 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
267 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
274 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
275 count = min(m->m_len - off, len);
276 bcopy(mtod(m, caddr_t) + off, cp, count);
285 * Copy a packet header mbuf chain into a completely new chain, including
286 * copying any mbuf clusters. Use this instead of m_copypacket() when
287 * you need a writable copy of an mbuf chain.
290 m_dup(struct mbuf *m, int how)
292 struct mbuf **p, *top = NULL;
293 int remain, moff, nsize;
298 KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
300 /* While there's more data, get a new mbuf, tack it on, and fill it */
301 remain = m->m_pkthdr.len;
304 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
307 /* Get the next new mbuf */
308 MGET(n, how, m->m_type);
311 if (top == NULL) { /* first one, must be PKTHDR */
314 } else /* not the first one */
316 if (remain >= MINCLSIZE) {
318 if ((n->m_flags & M_EXT) == 0) {
326 /* Link it into the new chain */
330 /* Copy data from original mbuf(s) into new mbuf */
331 while (n->m_len < nsize && m != NULL) {
332 int chunk = min(nsize - n->m_len, m->m_len - moff);
334 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
338 if (moff == m->m_len) {
344 /* Check correct total mbuf length */
345 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
346 ("%s: bogus m_pkthdr.len", __func__));
352 mbstat.m_mcfail++; /* XXX: No consistency. */
357 * Concatenate mbuf chain n to m.
358 * Both chains must be of the same type (e.g. MT_DATA).
359 * Any m_pkthdr is not updated.
362 m_cat(struct mbuf *m, struct mbuf *n)
367 if (m->m_flags & M_EXT ||
368 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
369 /* just join the two chains */
373 /* splat the data from one into the other */
374 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
376 m->m_len += n->m_len;
382 m_adj(struct mbuf *mp, int req_len)
388 if ((m = mp) == NULL)
394 while (m != NULL && len > 0) {
395 if (m->m_len <= len) {
406 if (mp->m_flags & M_PKTHDR)
407 m->m_pkthdr.len -= (req_len - len);
410 * Trim from tail. Scan the mbuf chain,
411 * calculating its length and finding the last mbuf.
412 * If the adjustment only affects this mbuf, then just
413 * adjust and return. Otherwise, rescan and truncate
414 * after the remaining size.
420 if (m->m_next == (struct mbuf *)0)
424 if (m->m_len >= len) {
426 if (mp->m_flags & M_PKTHDR)
427 mp->m_pkthdr.len -= len;
434 * Correct length for chain is "count".
435 * Find the mbuf with last data, adjust its length,
436 * and toss data from remaining mbufs on chain.
439 if (m->m_flags & M_PKTHDR)
440 m->m_pkthdr.len = count;
441 for (; m; m = m->m_next) {
442 if (m->m_len >= count) {
449 (m = m->m_next) ->m_len = 0;
454 * Rearange an mbuf chain so that len bytes are contiguous
455 * and in the data area of an mbuf (so that mtod and dtom
456 * will work for a structure of size len). Returns the resulting
457 * mbuf chain on success, frees it and returns null on failure.
458 * If there is room, it will add up to max_protohdr-len extra bytes to the
459 * contiguous region in an attempt to avoid being called next time.
462 m_pullup(struct mbuf *n, int len)
469 * If first mbuf has no cluster, and has room for len bytes
470 * without shifting current data, pullup into it,
471 * otherwise allocate a new mbuf to prepend to the chain.
473 if ((n->m_flags & M_EXT) == 0 &&
474 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
483 MGET(m, M_DONTWAIT, n->m_type);
487 if (n->m_flags & M_PKTHDR) {
489 n->m_flags &= ~M_PKTHDR;
492 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
494 count = min(min(max(len, max_protohdr), space), n->m_len);
495 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
505 } while (len > 0 && n);
514 mbstat.m_mpfail++; /* XXX: No consistency. */
519 * Partition an mbuf chain in two pieces, returning the tail --
520 * all but the first len0 bytes. In case of failure, it returns NULL and
521 * attempts to restore the chain to its original state.
523 * Note that the resulting mbufs might be read-only, because the new
524 * mbuf can end up sharing an mbuf cluster with the original mbuf if
525 * the "breaking point" happens to lie within a cluster mbuf. Use the
526 * M_WRITABLE() macro to check for this case.
529 m_split(struct mbuf *m0, int len0, int wait)
532 u_int len = len0, remain;
534 for (m = m0; m && len > m->m_len; m = m->m_next)
538 remain = m->m_len - len;
539 if (m0->m_flags & M_PKTHDR) {
540 MGETHDR(n, wait, m0->m_type);
543 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
544 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
545 m0->m_pkthdr.len = len0;
546 if (m->m_flags & M_EXT)
548 if (remain > MHLEN) {
549 /* m can't be the lead packet */
551 n->m_next = m_split(m, len, wait);
552 if (n->m_next == NULL) {
561 } else if (remain == 0) {
566 MGET(n, wait, m->m_type);
572 if (m->m_flags & M_EXT) {
576 n->m_data = m->m_data + len;
578 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
582 n->m_next = m->m_next;
587 * Routine to copy from device local memory into mbufs.
588 * Note that `off' argument is offset into first mbuf of target chain from
589 * which to begin copying the data to.
592 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
593 void (*copy)(char *from, caddr_t to, u_int len))
596 struct mbuf *top = 0, **mp = ⊤
599 if (off < 0 || off > MHLEN)
602 MGETHDR(m, M_DONTWAIT, MT_DATA);
605 m->m_pkthdr.rcvif = ifp;
606 m->m_pkthdr.len = totlen;
611 MGET(m, M_DONTWAIT, MT_DATA);
618 if (totlen + off >= MINCLSIZE) {
619 MCLGET(m, M_DONTWAIT);
620 if (m->m_flags & M_EXT)
624 * Place initial small packet/header at end of mbuf.
626 if (top == NULL && totlen + off + max_linkhdr <= len) {
627 m->m_data += max_linkhdr;
636 m->m_len = len = min(totlen, len);
638 copy(buf, mtod(m, caddr_t), (u_int)len);
640 bcopy(buf, mtod(m, caddr_t), (u_int)len);
650 * Copy data from a buffer back into the indicated mbuf chain,
651 * starting "off" bytes from the beginning, extending the mbuf
652 * chain if necessary.
655 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
658 struct mbuf *m = m0, *n;
663 while (off > (mlen = m->m_len)) {
666 if (m->m_next == NULL) {
667 n = m_get_clrd(M_DONTWAIT, m->m_type);
670 n->m_len = min(MLEN, len + off);
676 mlen = min (m->m_len - off, len);
677 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
685 if (m->m_next == NULL) {
686 n = m_get(M_DONTWAIT, m->m_type);
689 n->m_len = min(MLEN, len);
694 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
695 m->m_pkthdr.len = totlen;
699 m_print(const struct mbuf *m)
702 const struct mbuf *m2;
704 len = m->m_pkthdr.len;
707 printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
715 m_fixhdr(struct mbuf *m0)
719 len = m_length(m0, NULL);
720 m0->m_pkthdr.len = len;
725 m_length(struct mbuf *m0, struct mbuf **last)
731 for (m = m0; m != NULL; m = m->m_next) {
733 if (m->m_next == NULL)