/* uipc_mbuf2.c - mbuf routines */ /* * Copyright (c) 2001-2009 Wind River Systems, Inc. * * The right to copy, distribute, modify or otherwise make use * of this software may be licensed only pursuant to the terms * of an applicable Wind River license agreement. */ /* $FreeBSD: src/sys/kern/uipc_mbuf2.c,v 1.2.2.2 2001/07/03 11:01:37 ume Exp $ */ /* $KAME: uipc_mbuf2.c,v 1.29 2001/02/14 13:42:10 itojun Exp $ */ /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */ /* * Copyright (C) 1999 WIDE Project. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1988, 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95 */ /* modification history -------------------- 01q,18jun08,jpb Renamed _WRS_VX_SMP to _WRS_CONFIG_SMP. Added include path for kernel configurations options set in vsb. 01p,16jan08,dlk Rename spinLockFastLib --> spinlockIsrNdLib. 01o,10dec07,dlk Use new fast spin locks in SMP case. 01n,19oct07,dlk Use bare INT_CPU_LOCK()/INT_CPU_UNLOCK() rather than spin lock in uniprocessor case. 01m,20feb07,kch Merged from base6_iter12_smp-dev - convert from intLock to spinlocks. 01l,26dec06,kch Removed references to the obsolete coreip virtual stack headers. 01k,24aug05,dlk Moved m_split() from uipc_mbuf.c to here. Fix problems in m_split(). 01j,17jun05,dlk Remove m_dup1(). Removed m_extFreeRtn check from M_SHAREDCLUSTER(). Fix problems in m_pulldown(). 01i,12jan05,vvv osdep.h cleanup 01h,25feb04,vvv updated m_aux_* routines for dtom changes (SPR #94413) 01g,20nov03,niq osdep.h cleanup 01e,06nov03,rlm Ran batch header update for header re-org. 01d,08sep03,vvv merged from ACCORDION_BASE6_MERGE_BASELINE 01c,21apr03,ijm fixed compiler warnings 01b,07jun02,pas new version of MGET/MGETHDR takes size argument, doesn't require separate call to MCLGET 01a,09aug01,deg ported to clarinet from FreeBSD 4.3 version 1.2.2.2. */ /*#define PULLDOWN_DEBUG*/ #include #include #include #include #include #define M_SHAREDCLUSTER(m) \ (((m) -> m_flags & M_EXT) != 0 && (m) -> m_extRefCnt > 1) /* * ensure that [off, off + len) is contiguous on the mbuf chain "m". * packet chain before "off" is kept untouched. * if offp == NULL, the target will start at on resulting chain. * if offp != NULL, the target will start at on resulting chain. * * on error return (NULL return value), original "m" will be freed. * * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster) */ struct mbuf * m_pulldown(m, off, len, offp) struct mbuf *m; int off, len; int *offp; { struct mbuf *n, *o; int hlen, tlen, olen; int sharedcluster; /* check invalid arguments. */ if (m == NULL) { /* panic("m == NULL in m_pulldown()"); */ return NULL; } #ifdef PULLDOWN_DEBUG { struct mbuf *t; printf("before:"); for (t = m; t; t = t->m_next) printf(" %d", t->m_len); printf("\n"); } #endif n = m; while (n != NULL && off > 0) { if (n->m_len > off) goto n_ok; /* we know n != NULL and n->m_len > 0 */ off -= n->m_len; n = n->m_next; } /* be sure to point non-empty mbuf */ while (n != NULL && n->m_len == 0) n = n->m_next; if (!n) { m_freem(m); return NULL; /* mbuf chain too short */ } n_ok: sharedcluster = M_SHAREDCLUSTER(n); /* * the target data is on . * if we got enough data on the mbuf "n", we're done. */ if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster) goto ok; /* * when len <= n->m_len - off and off != 0, it is a special case. * len bytes from sits in single mbuf, but the caller does * not like the starting position (off). * chop the current mbuf into two pieces, set off to 0. * Handle a shared cluster the same way. */ if (len <= n->m_len - off) { /* shouldn't use m_dup1() since we don't want to copy the packet header */ MGET1 (o, n->m_len - off, M_DONTWAIT, (UCHAR)MT_DATA); if (o == NULL) { m_freem(m); return NULL; } o->m_len = n->m_len - off; bcopy (n->m_data + off, o->m_data, (size_t)o->m_len); n->m_len = off; o->m_next = n->m_next; n->m_next = o; n = n->m_next; off = 0; goto ok; } /* * we need to take hlen from and tlen from m_next, 0>, * and construct contiguous mbuf with m_len == len. * note that hlen + tlen == len, and tlen > 0. */ hlen = n->m_len - off; tlen = len - hlen; /* * ensure that we have enough trailing data on mbuf chain. * if not, we can do nothing about the chain. */ olen = 0; for (o = n->m_next; o != NULL; o = o->m_next) olen += o->m_len; if (hlen + olen < len) { m_freem(m); return NULL; /* mbuf chain too short */ } /* * easy cases first. * we need to use m_copydata() to get data from m_next, 0>. */ if ((off == 0 || offp)) { if (!sharedcluster && M_TRAILINGSPACE(n) >= tlen) { m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); n->m_len += tlen; m_adj(n->m_next, tlen); goto ok; } /* * Note, since we will be writing into the cluster of n->m_next, * we must check that it (not the cluster of n) is not shared. */ if (!M_SHAREDCLUSTER(n->m_next) && M_LEADINGSPACE(n->m_next) >= hlen) { n->m_next->m_data -= hlen; n->m_next->m_len += hlen; bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), (size_t)hlen); n->m_len -= hlen; n = n->m_next; off = 0; goto ok; } } /* * now, we need to do the hard way. don't m_copy as there's no room * on both end. */ MGET1(o, len, M_DONTWAIT, (UCHAR)m->m_type); if (!o) { m_freem(m); return NULL; } o->m_len = len; /* get hlen from into */ bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), (size_t)hlen); n->m_len -= hlen; /* get tlen from m_next, 0> into */ m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + hlen); m_adj(n->m_next, tlen); o->m_next = n->m_next; n->m_next = o; n = o ; off = 0; ok: #ifdef PULLDOWN_DEBUG { struct mbuf *t; printf("after:"); for (t = m; t; t = t->m_next) printf("%c%d", t == n ? '*' : ' ', t->m_len); printf(" (off=%d)\n", off); } #endif if (offp) *offp = off; return n; } /* * pkthdr.aux chain manipulation. * we don't allow clusters at this moment. */ struct mbuf * m_aux_add2(m, af, type, p) struct mbuf *m; int af, type; void *p; { struct mbuf *n; struct mauxtag *t; if ((m->m_flags & M_PKTHDR) == 0) return NULL; n = m_aux_find(m, af, type); if (n) return n; MGET(n, M_DONTWAIT, (UCHAR)m->m_type); if (n == NULL) return NULL; t = mtod(n, struct mauxtag *); bzero ((char *) t, sizeof (*t)); t->af = af; t->type = type; t->p = p; n->m_data += sizeof(struct mauxtag); n->m_len = 0; n->m_next = m->m_pkthdr.aux; m->m_pkthdr.aux = n; return n; } struct mbuf * m_aux_find2(m, af, type, p) struct mbuf *m; int af, type; void *p; { struct mbuf *n; struct mauxtag *t; if ((m->m_flags & M_PKTHDR) == 0) return NULL; for (n = m->m_pkthdr.aux; n; n = n->m_next) { t = (struct mauxtag *)(n->m_dat); if (n->m_data != ((caddr_t)t) + sizeof(struct mauxtag)) { printf("m_aux_find: original mbuf = %p\n", (void *) m); printf("m_aux_find: invalid m_data for mbuf=%p (%p %p)\n", (void *) n, (void *) t, (void *) n->m_data); continue; } if (t->af == af && t->type == type && t->p == p) return n; } return NULL; } struct mbuf * m_aux_find(m, af, type) struct mbuf *m; int af, type; { return m_aux_find2(m, af, type, NULL); } struct mbuf * m_aux_add(m, af, type) struct mbuf *m; int af, type; { return m_aux_add2(m, af, type, NULL); } void m_aux_delete(m, victim) struct mbuf *m; struct mbuf *victim; { struct mbuf *n, *prev, *next; struct mauxtag *t; if ((m->m_flags & M_PKTHDR) == 0) return; prev = NULL; n = m->m_pkthdr.aux; while (n) { t = (struct mauxtag *)(n->m_dat); next = n->m_next; if (n->m_data != ((caddr_t)t) + sizeof(struct mauxtag)) { printf("m_aux_delete: invalid m_data for mbuf=%p (%p %p)\n", (void *) n, (void *) t, (void *) n->m_data); prev = n; n = next; continue; } if (n == victim) { if (prev) prev->m_next = n->m_next; else m->m_pkthdr.aux = n->m_next; n->m_next = NULL; m_free(n); } else prev = n; n = next; } } /* * Partition an mbuf chain in two pieces, returning the tail -- * all but the first len0 bytes. In case of failure, it returns NULL and * attempts to restore the chain to its original state. */ struct mbuf * m_split(m0, len0, wait) register struct mbuf *m0; int len0, wait; { register struct mbuf *m, *n; unsigned len = len0, remain; int key; for (m = m0; m && len > (unsigned)m -> m_len; m = m -> m_next) len -= m -> m_len; if (m == 0) return (NULL); remain = m -> m_len - len; if (remain == 0) { n = m -> m_next; m -> m_next = 0; } else { n = mBlkGet (_pNetDpool, wait, (UCHAR)m->m_type); if (n == 0) return (0); n -> m_flags |= M_EXT; n -> m_ext = m -> m_ext; NET_POOL_LOCK (&m->m_ext->pNetPool->poolLock, key); ++(m -> m_extRefCnt); NET_POOL_UNLOCK (&m->m_ext->pNetPool->poolLock, key); n -> m_data = m -> m_data + len; n -> m_len = remain; m -> m_len = len; n -> m_next = m -> m_next; m -> m_next = 0; } if (m0 -> m_flags & M_PKTHDR) { n -> m_flags |= M_PKTHDR; n -> m_pkthdr.rcvif = m0 -> m_pkthdr.rcvif; n -> m_pkthdr.len = m0 -> m_pkthdr.len - len0; m0 -> m_pkthdr.len = len0; } return (n); }