1 //==========================================================================
3 // src/sys/kern/uipc_mbuf.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1988, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
55 * $FreeBSD: src/sys/kern/uipc_mbuf.c,v 1.51.2.7 2001/07/30 23:28:00 peter Exp $
58 #include <sys/param.h>
59 #include <sys/malloc.h>
61 #include <sys/domain.h>
62 #include <sys/protosw.h>
64 static void mbinit __P((void *));
65 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL)
70 u_long mbtypes[MT_NTYPES];
72 union mcluster *mclfree;
77 u_int m_mballoc_wid = 0;
78 u_int m_clalloc_wid = 0;
80 int mbuf_wait = 32; // Time in ticks to wait for mbufs to come free
82 static void m_reclaim __P((void));
85 #define NMBCLUSTERS (512 + maxusers * 16)
88 #define NMBUFS (nmbclusters * 4)
92 /* "number of clusters of pages" */
104 mmbfree = NULL; mclfree = NULL;
105 mbstat.m_msize = MSIZE;
106 mbstat.m_mclbytes = MCLBYTES;
107 mbstat.m_minclsize = MINCLSIZE;
108 mbstat.m_mlen = MLEN;
109 mbstat.m_mhlen = MHLEN;
112 if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0)
114 #if MCLBYTES <= PAGE_SIZE
115 if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
118 /* It's OK to call contigmalloc in this context. */
119 if (m_clalloc(16, M_WAIT) == 0)
129 * Allocate at least nmb mbufs and place on mbuf free list.
130 * Must be called at splimp.
141 for (i = 0; i < nmb; i++) {
142 p = (struct mbuf *)cyg_net_mbuf_alloc( );
143 if (p != (struct mbuf *)0) {
144 ((struct mbuf *)p)->m_next = mmbfree;
145 mmbfree = (struct mbuf *)p;
149 // Warn - out of mbufs?
157 * Once the mb_map has been exhausted and if the call to the allocation macros
158 * (or, in some cases, functions) is with M_WAIT, then it is necessary to rely
159 * solely on reclaimed mbufs. Here we wait for an mbuf to be freed for a
160 * designated (mbuf_wait) time.
163 m_mballoc_wait(int caller, int type)
170 if ((tsleep(&m_mballoc_wid, PVM, "mballc", mbuf_wait)) == EWOULDBLOCK)
175 * Now that we (think) that we've got something, we will redo an
176 * MGET, but avoid getting into another instance of m_mballoc_wait()
177 * XXX: We retry to fetch _even_ if the sleep timed out. This is left
178 * this way, purposely, in the [unlikely] case that an mbuf was
179 * freed but the sleep was not awakened in time.
184 MGET(p, M_DONTWAIT, type);
187 MGETHDR(p, M_DONTWAIT, type);
190 panic("m_mballoc_wait: invalid caller (%d)", caller);
194 if (p != NULL) { /* We waited and got something... */
196 /* Wake up another if we have more free. */
204 #if MCLBYTES > PAGE_SIZE
205 static int i_want_my_mcl;
213 tsleep(&i_want_my_mcl, PVM, "mclalloc", 0);
215 for (; i_want_my_mcl; i_want_my_mcl--) {
216 if (m_clalloc(1, M_WAIT) == 0)
217 printf("m_clalloc failed even in process context!\n");
222 static struct proc *mclallocproc;
223 static struct kproc_desc mclalloc_kp = {
228 SYSINIT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
233 * Allocate some number of mbuf clusters
234 * and place on cluster free list.
235 * Must be called at splimp.
246 for (i = 0; i < ncl; i++) {
247 p = (union mcluster *)cyg_net_cluster_alloc();
248 if (p != (union mcluster *)0) {
249 ((union mcluster *)p)->mcl_next = mclfree;
250 mclfree = (union mcluster *)p;
254 // Warn - no more clusters?
262 * Once the mb_map submap has been exhausted and the allocation is called with
263 * M_WAIT, we rely on the mclfree union pointers. If nothing is free, we will
264 * sleep for a designated amount of time (mbuf_wait) or until we're woken up
265 * due to sudden mcluster availability.
273 /* Sleep until something's available or until we expire. */
275 if ((tsleep(&m_clalloc_wid, PVM, "mclalc", mbuf_wait)) == EWOULDBLOCK)
279 * Now that we (think) that we've got something, we will redo and
280 * MGET, but avoid getting into another instance of m_clalloc_wait()
283 MCLALLOC(p, M_DONTWAIT);
286 if (p != NULL) { /* We waited and got something... */
288 /* Wake up another if we have more free. */
298 * When MGET fails, ask protocols to free space when short of memory,
299 * then re-attempt to allocate an mbuf.
305 register struct mbuf *m;
308 * Must only do the reclaim if not in an interrupt context.
315 * Both m_mballoc_wait and m_retry must be nulled because
316 * when the MGET macro is run from here, we deffinately do _not_
317 * want to enter an instance of m_mballoc_wait() or m_retry() (again!)
320 #undef m_mballoc_wait
321 #define m_mballoc_wait(caller,type) (struct mbuf *)0
322 #define m_retry(i, t) (struct mbuf *)0
325 #undef m_mballoc_wait
326 #define m_retry cyg_m_retry
327 #define m_retryhdr cyg_m_retryhdr
328 #define m_mballoc_wait cyg_m_mballoc_wait
339 * As above; retry an MGETHDR.
345 register struct mbuf *m;
348 * Must only do the reclaim if not in an interrupt context.
355 #undef m_mballoc_wait
356 #define m_mballoc_wait(caller,type) (struct mbuf *)0
357 #define m_retryhdr(i, t) (struct mbuf *)0
360 #undef m_mballoc_wait
361 #define m_retry cyg_m_retry
362 #define m_retryhdr cyg_m_retryhdr
363 #define m_mballoc_wait cyg_m_mballoc_wait
376 register struct domain *dp;
377 register struct protosw *pr;
380 for (dp = domains; dp; dp = dp->dom_next)
381 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
389 * Space allocation routines.
390 * These are also available as macros
391 * for critical paths.
397 register struct mbuf *m;
407 register struct mbuf *m;
409 MGETHDR(m, how, type);
417 register struct mbuf *m;
422 bzero(mtod(m, caddr_t), MLEN);
428 * m_getm(m, len, how, type)
430 * This will allocate len-worth of mbufs and/or mbuf clusters (whatever fits
431 * best) and return a pointer to the top of the allocated chain. If m is
432 * non-null, then we assume that it is a single mbuf or an mbuf chain to
433 * which we want len bytes worth of mbufs and/or clusters attached, and so
434 * if we succeed in allocating it, we will just return a pointer to m.
436 * If we happen to fail at any point during the allocation, we will free
437 * up everything we have already allocated and return NULL.
441 m_getm(struct mbuf *m, int len, int how, int type)
443 struct mbuf *top, *tail, *mp, *mtail = NULL;
448 else if (len > MINCLSIZE) {
450 if ((mp->m_flags & M_EXT) == 0) {
456 len -= M_TRAILINGSPACE(mp);
459 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
471 if (len > MINCLSIZE) {
473 if ((mp->m_flags & M_EXT) == 0)
478 len -= M_TRAILINGSPACE(mp);
494 register struct mbuf *n;
502 register struct mbuf *m;
504 register struct mbuf *n;
505 struct mbuf *orig = m;
513 diag_printf("DEBUG: Circular MBUF %p!\n", orig);
520 * Mbuffer utility routines.
524 * Lesser-used path for M_PREPEND:
525 * allocate new mbuf to prepend to chain,
529 m_prepend(m, len, how)
530 register struct mbuf *m;
535 MGET(mn, how, m->m_type);
536 if (mn == (struct mbuf *)NULL) {
538 return ((struct mbuf *)NULL);
540 if (m->m_flags & M_PKTHDR) {
541 M_COPY_PKTHDR(mn, m);
542 m->m_flags &= ~M_PKTHDR;
553 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
554 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
555 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
556 * Note that the copy is read-only, because clusters are not copied,
557 * only their reference counts are incremented.
559 #define MCFail (mbstat.m_mcfail)
562 m_copym(m, off0, len, wait)
563 register struct mbuf *m;
567 register struct mbuf *n, **np;
568 register int off = off0;
572 if (off == 0 && m->m_flags & M_PKTHDR)
586 MGET(n, wait, m->m_type);
592 if (len == M_COPYALL)
593 n->m_pkthdr.len -= off0;
595 n->m_pkthdr.len = len;
598 n->m_len = min(len, m->m_len - off);
599 if (m->m_flags & M_EXT) {
600 n->m_data = m->m_data + off;
601 if(!m->m_ext.ext_ref)
602 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
604 (*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
609 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
611 if (len != M_COPYALL)
627 * Copy an entire packet, including header (which must be present).
628 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
629 * Note that the copy is read-only, because clusters are not copied,
630 * only their reference counts are incremented.
637 struct mbuf *top, *n, *o;
639 MGET(n, how, m->m_type);
646 if (m->m_flags & M_EXT) {
647 n->m_data = m->m_data;
648 if(!m->m_ext.ext_ref)
649 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
651 (*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
656 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
661 MGET(o, how, m->m_type);
669 if (m->m_flags & M_EXT) {
670 n->m_data = m->m_data;
671 if(!m->m_ext.ext_ref)
672 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
674 (*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
679 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
692 * Copy data from an mbuf chain starting "off" bytes from the beginning,
693 * continuing for "len" bytes, into the indicated buffer.
696 m_copydata(m, off, len, cp)
697 register struct mbuf *m;
702 register unsigned count;
711 count = min(m->m_len - off, len);
712 bcopy(mtod(m, caddr_t) + off, cp, count);
721 * Copy a packet header mbuf chain into a completely new chain, including
722 * copying any mbuf clusters. Use this instead of m_copypacket() when
723 * you need a writable copy of an mbuf chain.
730 struct mbuf **p, *top = NULL;
731 int remain, moff, nsize;
737 /* While there's more data, get a new mbuf, tack it on, and fill it */
738 remain = m->m_pkthdr.len;
741 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
744 /* Get the next new mbuf */
745 MGET(n, how, m->m_type);
748 if (top == NULL) { /* first one, must be PKTHDR */
751 } else /* not the first one */
753 if (remain >= MINCLSIZE) {
755 if ((n->m_flags & M_EXT) == 0) {
763 /* Link it into the new chain */
767 /* Copy data from original mbuf(s) into new mbuf */
768 while (n->m_len < nsize && m != NULL) {
769 int chunk = min(nsize - n->m_len, m->m_len - moff);
771 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
775 if (moff == m->m_len) {
790 * Concatenate mbuf chain n to m.
791 * Both chains must be of the same type (e.g. MT_DATA).
792 * Any m_pkthdr is not updated.
796 register struct mbuf *m, *n;
801 if (m->m_flags & M_EXT ||
802 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
803 /* just join the two chains */
807 /* splat the data from one into the other */
808 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
810 m->m_len += n->m_len;
820 register int len = req_len;
821 register struct mbuf *m;
824 if ((m = mp) == NULL)
830 while (m != NULL && len > 0) {
831 if (m->m_len <= len) {
842 if (mp->m_flags & M_PKTHDR)
843 m->m_pkthdr.len -= (req_len - len);
846 * Trim from tail. Scan the mbuf chain,
847 * calculating its length and finding the last mbuf.
848 * If the adjustment only affects this mbuf, then just
849 * adjust and return. Otherwise, rescan and truncate
850 * after the remaining size.
856 if (m->m_next == (struct mbuf *)0)
860 if (m->m_len >= len) {
862 if (mp->m_flags & M_PKTHDR)
863 mp->m_pkthdr.len -= len;
870 * Correct length for chain is "count".
871 * Find the mbuf with last data, adjust its length,
872 * and toss data from remaining mbufs on chain.
875 if (m->m_flags & M_PKTHDR)
876 m->m_pkthdr.len = count;
877 for (; m; m = m->m_next) {
878 if (m->m_len >= count) {
885 (m = m->m_next) ->m_len = 0;
890 * Rearange an mbuf chain so that len bytes are contiguous
891 * and in the data area of an mbuf (so that mtod and dtom
892 * will work for a structure of size len). Returns the resulting
893 * mbuf chain on success, frees it and returns null on failure.
894 * If there is room, it will add up to max_protohdr-len extra bytes to the
895 * contiguous region in an attempt to avoid being called next time.
897 #define MPFail (mbstat.m_mpfail)
901 register struct mbuf *n;
904 register struct mbuf *m;
909 * If first mbuf has no cluster, and has room for len bytes
910 * without shifting current data, pullup into it,
911 * otherwise allocate a new mbuf to prepend to the chain.
913 if ((n->m_flags & M_EXT) == 0 &&
914 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
923 MGET(m, M_DONTWAIT, n->m_type);
927 if (n->m_flags & M_PKTHDR) {
929 n->m_flags &= ~M_PKTHDR;
932 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
934 count = min(min(max(len, max_protohdr), space), n->m_len);
935 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
945 } while (len > 0 && n);
959 * Partition an mbuf chain in two pieces, returning the tail --
960 * all but the first len0 bytes. In case of failure, it returns NULL and
961 * attempts to restore the chain to its original state.
964 m_split(m0, len0, wait)
965 register struct mbuf *m0;
968 register struct mbuf *m, *n;
969 unsigned len = len0, remain;
971 for (m = m0; m && len > m->m_len; m = m->m_next)
975 remain = m->m_len - len;
976 if (m0->m_flags & M_PKTHDR) {
977 MGETHDR(n, wait, m0->m_type);
980 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
981 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
982 m0->m_pkthdr.len = len0;
983 if (m->m_flags & M_EXT)
985 if (remain > MHLEN) {
986 /* m can't be the lead packet */
988 n->m_next = m_split(m, len, wait);
989 if (n->m_next == 0) {
996 } else if (remain == 0) {
1001 MGET(n, wait, m->m_type);
1007 if (m->m_flags & M_EXT) {
1008 n->m_flags |= M_EXT;
1009 n->m_ext = m->m_ext;
1010 if(!m->m_ext.ext_ref)
1011 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
1013 (*(m->m_ext.ext_ref))(m->m_ext.ext_buf,
1015 m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
1016 n->m_data = m->m_data + len;
1018 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1022 n->m_next = m->m_next;
1027 * Routine to copy from device local memory into mbufs.
1030 m_devget(buf, totlen, off0, ifp, copy)
1034 void (*copy) __P((char *from, caddr_t to, u_int len));
1036 register struct mbuf *m;
1037 struct mbuf *top = 0, **mp = ⊤
1038 register int off = off0, len;
1045 cp += off + 2 * sizeof(u_short);
1046 totlen -= 2 * sizeof(u_short);
1048 MGETHDR(m, M_DONTWAIT, MT_DATA);
1051 m->m_pkthdr.rcvif = ifp;
1052 m->m_pkthdr.len = totlen;
1055 while (totlen > 0) {
1057 MGET(m, M_DONTWAIT, MT_DATA);
1064 len = min(totlen, epkt - cp);
1065 if (len >= MINCLSIZE) {
1066 MCLGET(m, M_DONTWAIT);
1067 if (m->m_flags & M_EXT)
1068 m->m_len = len = min(len, MCLBYTES);
1073 * Place initial small packet/header at end of mbuf.
1075 if (len < m->m_len) {
1076 if (top == 0 && len + max_linkhdr <= m->m_len)
1077 m->m_data += max_linkhdr;
1083 copy(cp, mtod(m, caddr_t), (unsigned)len);
1085 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
1097 * Copy data from a buffer back into the indicated mbuf chain,
1098 * starting "off" bytes from the beginning, extending the mbuf
1099 * chain if necessary.
1102 m_copyback(m0, off, len, cp)
1109 register struct mbuf *m = m0, *n;
1114 while (off > (mlen = m->m_len)) {
1117 if (m->m_next == 0) {
1118 n = m_getclr(M_DONTWAIT, m->m_type);
1121 n->m_len = min(MLEN, len + off);
1127 mlen = min (m->m_len - off, len);
1128 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
1136 if (m->m_next == 0) {
1137 n = m_get(M_DONTWAIT, m->m_type);
1140 n->m_len = min(MLEN, len);
1145 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1146 m->m_pkthdr.len = totlen;
1151 m_print(const struct mbuf *m)
1154 const struct mbuf *m2;
1156 len = m->m_pkthdr.len;
1159 printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");