1 //==========================================================================
3 // sys/kern/uipc_mbuf.c
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
33 /* $OpenBSD: uipc_mbuf.c,v 1.18 1999/12/05 07:30:31 angelos Exp $ */
34 /* $NetBSD: uipc_mbuf.c,v 1.15.4.1 1996/06/13 17:11:44 cgd Exp $ */
37 * Copyright (c) 1982, 1986, 1988, 1991, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
72 %%% portions-copyright-nrl-95
73 Portions of this software are Copyright 1995-1998 by Randall Atkinson,
74 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
75 Reserved. All rights under this copyright have been assigned to the US
76 Naval Research Laboratory (NRL). The NRL Copyright Notice and License
77 Agreement Version 1.1 (January 17, 1995) applies to these portions of the
79 You should have received a copy of the license with this software. If you
80 didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.
83 #include <sys/param.h>
85 #include <sys/systm.h>
88 #include <sys/malloc.h>
94 #include <sys/kernel.h>
96 #include <sys/syslog.h>
98 #include <sys/domain.h>
99 #include <sys/protosw.h>
101 #include <machine/cpu.h>
108 #include <uvm/uvm_extern.h>
112 extern vm_map_t mb_map;
119 extern void setsoftnet(void);
120 extern void *cyg_net_cluster_alloc(void);
123 /* Declarations of variables move from mbuf.h to keep g++ happy */
125 struct mbstat mbstat;
126 union mcluster *mclfree;
127 int max_linkhdr; /* largest link-level header */
128 int max_protohdr; /* largest protocol header */
129 int max_hdr; /* largest link+protocol header */
130 int max_datalen; /* MHLEN - max_hdr */
132 int mbtypes[] = { /* XXX */
133 M_FREE, /* MT_FREE 0 should be on free list */
134 M_MBUF, /* MT_DATA 1 dynamic (data) allocation */
135 M_MBUF, /* MT_HEADER 2 packet header */
136 M_SOCKET, /* MT_SOCKET 3 socket structure */
137 M_PCB, /* MT_PCB 4 protocol control block */
138 M_RTABLE, /* MT_RTABLE 5 routing tables */
139 M_HTABLE, /* MT_HTABLE 6 IMP host tables */
140 0, /* MT_ATABLE 7 address resolution tables */
141 M_MBUF, /* MT_SONAME 8 socket name */
143 M_SOOPTS, /* MT_SOOPTS 10 socket options */
144 M_FTABLE, /* MT_FTABLE 11 fragment reassembly header */
145 M_MBUF, /* MT_RIGHTS 12 access rights */
146 M_IFADDR, /* MT_IFADDR 13 interface address */
147 M_MBUF, /* MT_CONTROL 14 extra-data protocol message */
148 M_MBUF, /* MT_OOBDATA 15 expedited data */
150 25, 26, 27, 28, 29, 30, 31, 32 /* datakit ugliness */
163 if (m_clalloc(1, M_DONTWAIT) == 0)
165 if (m_clalloc(max(4096 / CLBYTES, 1), M_DONTWAIT) == 0)
175 * Allocate some number of mbuf clusters
176 * and place on cluster free list.
177 * Must be called at splimp.
181 m_clalloc(ncl, nowait)
190 panic("Allocate multiple clusters!");
192 p = (caddr_t)cyg_net_cluster_alloc();
195 return (mclfree != NULL);
197 for (i = 0; i < ncl; i++) {
198 ((union mcluster *)p)->mcl_next = mclfree;
199 mclfree = (union mcluster *)p;
203 mbstat.m_clusters += ncl;
206 volatile static struct timeval lastlogged;
207 struct timeval curtime, logdiff;
214 p = (caddr_t)uvm_km_kmemalloc(mb_map, uvmexp.mb_object, ctob(npg),
215 nowait ? 0 : UVM_KMF_NOWAIT);
217 p = (caddr_t)kmem_malloc(mb_map, ctob(npg), !nowait);
223 timersub(&curtime, &lastlogged, &logdiff);
224 if (logdiff.tv_sec >= 60) {
225 lastlogged = curtime;
226 log(LOG_ERR, "mb_map full\n");
229 return (mclfree != NULL);
231 ncl = ncl * CLBYTES / MCLBYTES;
232 for (i = 0; i < ncl; i++) {
233 ((union mcluster *)p)->mcl_next = mclfree;
234 mclfree = (union mcluster *)p;
238 mbstat.m_clusters += ncl;
244 * When MGET failes, ask protocols to free space when short of memory,
245 * then re-attempt to allocate an mbuf.
251 register struct mbuf *m;
253 if (i & M_DONTWAIT) {
259 #define m_retry(i, t) NULL
266 * As above; retry an MGETHDR.
272 register struct mbuf *m;
274 if (i & M_DONTWAIT) {
280 #define m_retryhdr(i, t) NULL
289 register struct domain *dp;
290 register struct protosw *pr;
294 for (dp = domains; dp; dp = dp->dom_next)
295 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
303 * Space allocation routines.
304 * These are also available as macros
305 * for critical paths.
311 register struct mbuf *m;
313 MGET(m, nowait, type);
318 m_gethdr(nowait, type)
321 register struct mbuf *m;
323 MGETHDR(m, nowait, type);
328 m_getclr(nowait, type)
331 register struct mbuf *m;
333 MGET(m, nowait, type);
336 bzero(mtod(m, caddr_t), MLEN);
344 register struct mbuf *n;
352 register struct mbuf *m;
354 register struct mbuf *n;
360 } while ((m = n) != NULL);
364 * Mbuffer utility routines.
368 * Lesser-used path for M_PREPEND:
369 * allocate new mbuf to prepend to chain,
373 m_prepend(m, len, how)
374 register struct mbuf *m;
379 MGET(mn, how, m->m_type);
384 if (m->m_flags & M_PKTHDR) {
385 M_COPY_PKTHDR(mn, m);
386 m->m_flags &= ~M_PKTHDR;
397 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
398 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
399 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
404 m_copym(m, off0, len, wait)
405 register struct mbuf *m;
409 register struct mbuf *n, **np;
410 register int off = off0;
415 panic("m_copym: off %d < 0", off);
417 panic("m_copym: len %d < 0", len);
418 if (off == 0 && m->m_flags & M_PKTHDR)
422 panic("m_copym: null mbuf");
432 if (len != M_COPYALL)
433 panic("m_copym: %d not M_COPYALL", len);
436 MGET(n, wait, m->m_type);
442 if (len == M_COPYALL)
443 n->m_pkthdr.len -= off0;
445 n->m_pkthdr.len = len;
448 n->m_len = min(len, m->m_len - off);
449 if (m->m_flags & M_EXT) {
450 n->m_data = m->m_data + off;
451 if (!m->m_ext.ext_ref)
452 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
454 (*(m->m_ext.ext_ref))(m);
458 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
460 if (len != M_COPYALL)
476 * m_copym2() is like m_copym(), except it COPIES cluster mbufs, instead
477 * of merely bumping the reference count.
480 m_copym2(m, off0, len, wait)
481 register struct mbuf *m;
485 register struct mbuf *n, **np;
486 register int off = off0;
491 panic("m_copym2: len %d < 0", len);
493 panic("m_copym2: off %d < 0", off);
494 if (off == 0 && m->m_flags & M_PKTHDR)
498 panic("m_copym2: null mbuf");
508 if (len != M_COPYALL)
509 panic("m_copym2: %d != M_COPYALL", len);
512 MGET(n, wait, m->m_type);
518 if (len == M_COPYALL)
519 n->m_pkthdr.len -= off0;
521 n->m_pkthdr.len = len;
524 n->m_len = min(len, m->m_len - off);
525 if ((m->m_flags & M_EXT) && (n->m_len > MHLEN)) {
526 /* This is a cheesy hack. */
528 if (n->m_flags & M_EXT)
529 bcopy(mtod(m, caddr_t) + off, mtod(n, caddr_t),
534 bcopy(mtod(m, caddr_t) + off, mtod(n, caddr_t),
536 if (len != M_COPYALL)
552 * Copy data from an mbuf chain starting "off" bytes from the beginning,
553 * continuing for "len" bytes, into the indicated buffer.
556 m_copydata(m, off, len, cp)
557 register struct mbuf *m;
562 register unsigned count;
565 panic("m_copydata: off %d < 0", off);
567 panic("m_copydata: len %d < 0", len);
570 panic("m_copydata: null mbuf in skip");
578 panic("m_copydata: null mbuf");
579 count = min(m->m_len - off, len);
580 bcopy(mtod(m, caddr_t) + off, cp, count);
589 * Concatenate mbuf chain n to m.
590 * Both chains must be of the same type (e.g. MT_DATA).
591 * Any m_pkthdr is not updated.
595 register struct mbuf *m, *n;
600 if (m->m_flags & M_EXT ||
601 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
602 /* just join the two chains */
606 /* splat the data from one into the other */
607 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
609 m->m_len += n->m_len;
619 register int len = req_len;
620 register struct mbuf *m;
623 if ((m = mp) == NULL)
629 while (m != NULL && len > 0) {
630 if (m->m_len <= len) {
641 if (mp->m_flags & M_PKTHDR)
642 m->m_pkthdr.len -= (req_len - len);
645 * Trim from tail. Scan the mbuf chain,
646 * calculating its length and finding the last mbuf.
647 * If the adjustment only affects this mbuf, then just
648 * adjust and return. Otherwise, rescan and truncate
649 * after the remaining size.
655 if (m->m_next == NULL)
659 if (m->m_len >= len) {
661 if (mp->m_flags & M_PKTHDR)
662 mp->m_pkthdr.len -= len;
669 * Correct length for chain is "count".
670 * Find the mbuf with last data, adjust its length,
671 * and toss data from remaining mbufs on chain.
674 if (m->m_flags & M_PKTHDR)
675 m->m_pkthdr.len = count;
676 for (; m; m = m->m_next) {
677 if (m->m_len >= count) {
683 while ((m = m->m_next) != NULL)
689 * Rearange an mbuf chain so that len bytes are contiguous
690 * and in the data area of an mbuf (so that mtod and dtom
691 * will work for a structure of size len). Returns the resulting
692 * mbuf chain on success, frees it and returns null on failure.
693 * If there is room, it will add up to max_protohdr-len extra bytes to the
694 * contiguous region in an attempt to avoid being called next time.
700 register struct mbuf *n;
703 register struct mbuf *m;
708 * If first mbuf has no cluster, and has room for len bytes
709 * without shifting current data, pullup into it,
710 * otherwise allocate a new mbuf to prepend to the chain.
712 if ((n->m_flags & M_EXT) == 0 &&
713 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
722 MGET(m, M_DONTWAIT, n->m_type);
726 if (n->m_flags & M_PKTHDR) {
728 n->m_flags &= ~M_PKTHDR;
731 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
733 count = min(min(max(len, max_protohdr), space), n->m_len);
734 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
744 } while (len > 0 && n);
758 * m_pullup2() works like m_pullup, save that len can be <= MCLBYTES.
759 * m_pullup2() only works on values of len such that MHLEN < len <= MCLBYTES,
760 * it calls m_pullup() for values <= MHLEN. It also only coagulates the
761 * reqested number of bytes. (For those of us who expect unwieldly option
764 * KEBE SAYS: Remember that dtom() calls with data in clusters does not work!
768 register struct mbuf *n;
771 register struct mbuf *m;
775 return m_pullup(n, len);
777 if ((n->m_flags & M_EXT) != 0 &&
778 n->m_data + len < &n->m_data[MCLBYTES] && n->m_next) {
787 MGET(m, M_DONTWAIT, n->m_type);
790 MCLGET(m, M_DONTWAIT);
791 if ((m->m_flags & M_EXT) == 0)
794 if (n->m_flags & M_PKTHDR) {
795 /* M_COPY_PKTHDR(m, n);*//* Too many adverse side effects. */
796 m->m_pkthdr = n->m_pkthdr;
797 m->m_flags = (n->m_flags & M_COPYFLAGS) | M_EXT;
798 n->m_flags &= ~M_PKTHDR;
799 /* n->m_data is cool. */
804 count = min(len, n->m_len);
805 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
815 } while (len > 0 && n);
830 * Inject a new mbuf chain of length siz in mbuf chain m0 at
831 * position len0. Returns a pointer to the first injected mbuf, or
832 * NULL on failure (m0 is left undisturbed). Note that if there is
833 * enough space for an object of size siz in the appropriate position,
834 * no memory will be allocated. Also, there will be no data movement in
835 * the first len0 bytes (pointers to that will remain valid).
837 * XXX It is assumed that siz is less than the size of an mbuf at the moment.
840 m_inject(m0, len0, siz, wait)
841 register struct mbuf *m0;
844 register struct mbuf *m, *n, *n2 = NULL, *n3;
845 unsigned len = len0, remain;
847 if ((siz >= MHLEN) || (len0 <= 0))
849 for (m = m0; m && len > m->m_len; m = m->m_next)
853 remain = m->m_len - len;
855 if ((m->m_next) && (M_LEADINGSPACE(m->m_next) >= siz)) {
856 m->m_next->m_len += siz;
857 m0->m_pkthdr.len += siz;
858 m->m_next->m_data -= siz;
862 n2 = m_copym2(m, len, remain, wait);
867 MGET(n, wait, MT_DATA);
875 m0->m_pkthdr.len += siz;
876 m->m_len -= remain; /* Trim */
878 for (n3 = n; n3->m_next != NULL; n3 = n3->m_next)
883 for (; n3->m_next != NULL; n3 = n3->m_next)
885 n3->m_next = m->m_next;
891 * Partition an mbuf chain in two pieces, returning the tail --
892 * all but the first len0 bytes. In case of failure, it returns NULL and
893 * attempts to restore the chain to its original state.
896 m_split(m0, len0, wait)
897 register struct mbuf *m0;
900 register struct mbuf *m, *n;
901 unsigned len = len0, remain, olen;
903 for (m = m0; m && len > m->m_len; m = m->m_next)
907 remain = m->m_len - len;
908 if (m0->m_flags & M_PKTHDR) {
909 MGETHDR(n, wait, m0->m_type);
912 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
913 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
914 olen = m0->m_pkthdr.len;
915 m0->m_pkthdr.len = len0;
916 if (m->m_flags & M_EXT)
918 if (remain > MHLEN) {
919 /* m can't be the lead packet */
921 n->m_next = m_split(m, len, wait);
922 if (n->m_next == NULL) {
924 m0->m_pkthdr.len = olen;
930 } else if (remain == 0) {
935 MGET(n, wait, m->m_type);
941 if (m->m_flags & M_EXT) {
944 if(!m->m_ext.ext_ref)
945 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
947 (*(m->m_ext.ext_ref))(m);
948 m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
949 n->m_data = m->m_data + len;
951 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
955 n->m_next = m->m_next;
960 * Routine to copy from device local memory into mbufs.
963 m_devget(buf, totlen, off0, ifp, copy)
967 void (*copy) __P((const void *, void *, size_t));
969 register struct mbuf *m;
970 struct mbuf *top = NULL, **mp = ⊤
971 register int off = off0, len;
979 * If 'off' is non-zero, packet is trailer-encapsulated,
980 * so we have to skip the type and length fields.
982 cp += off + 2 * sizeof(u_int16_t);
983 totlen -= 2 * sizeof(u_int16_t);
985 MGETHDR(m, M_DONTWAIT, MT_DATA);
988 m->m_pkthdr.rcvif = ifp;
989 m->m_pkthdr.len = totlen;
994 MGET(m, M_DONTWAIT, MT_DATA);
1001 len = min(totlen, epkt - cp);
1002 if (len >= MINCLSIZE) {
1003 MCLGET(m, M_DONTWAIT);
1004 if (m->m_flags & M_EXT)
1005 m->m_len = len = min(len, MCLBYTES);
1010 * Place initial small packet/header at end of mbuf.
1012 if (len < m->m_len) {
1014 len + max_linkhdr <= m->m_len)
1015 m->m_data += max_linkhdr;
1021 copy(cp, mtod(m, caddr_t), (size_t)len);
1023 bcopy(cp, mtod(m, caddr_t), (size_t)len);
1039 if (m->m_flags & M_PKTHDR)
1040 bzero((unsigned char *)m + sizeof(struct m_hdr) +
1041 sizeof(struct pkthdr), MHLEN);
1043 bzero((unsigned char *)m + sizeof(struct m_hdr), MLEN);
1044 if ((m->m_flags & M_EXT) &&
1045 (m->m_ext.ext_free == NULL) &&
1046 !mclrefcnt[mtocl((m)->m_ext.ext_buf)])
1047 bzero(m->m_ext.ext_buf, m->m_ext.ext_size);
1053 * Apply function f to the data in an mbuf chain starting "off" bytes from the
1054 * beginning, continuing for "len" bytes.
1057 m_apply(m, off, len, f, fstate)
1061 /* fstate, data, len */
1062 int (*f)(caddr_t, caddr_t, unsigned int);
1069 panic("m_apply: len %d < 0", len);
1071 panic("m_apply: off %d < 0", off);
1074 panic("m_apply: null mbuf in skip");
1082 panic("m_apply: null mbuf");
1083 count = min(m->m_len - off, len);
1085 rval = f(fstate, mtod(m, caddr_t) + off, count);