📄 uipc_mbuf.c
字号:
/* $Id: uipc_mbuf.c,v 1.1.1.1 2001/10/01 18:47:34 patrik Exp $ *//* $OpenBSD: uipc_mbuf.c,v 1.17 1999/10/01 02:00:12 jason Exp $ *//* $NetBSD: uipc_mbuf.c,v 1.15.4.1 1996/06/13 17:11:44 cgd Exp $ *//* * 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.2 (Berkeley) 1/4/94 *//*%%% portions-copyright-nrl-95Portions of this software are Copyright 1995-1998 by Randall Atkinson,Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All RightsReserved. All rights under this copyright have been assigned to the USNaval Research Laboratory (NRL). The NRL Copyright Notice and LicenseAgreement Version 1.1 (January 17, 1995) applies to these portions of thesoftware.You should have received a copy of the license with this software. If youdidn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.*/#include <sys/param.h>#include <sys/systm.h>#include <sys/proc.h>#include <sys/malloc.h>#include <sys/map.h>#define MBTYPES#include <sys/mbuf.h>#include <sys/kernel.h>#include <sys/syslog.h>#include <sys/domain.h>#include <sys/protosw.h>#include <machine/cpu.h>#include <vm/vm.h>#if defined(UVM)#include <uvm/uvm_extern.h>#endifextern vm_map_t mb_map;struct mbuf *mbutl;char *mclrefcnt;int needqueuedrain;voidmbinit(){ int s; s = splimp(); if (m_clalloc(max(4096 / CLBYTES, 1), M_DONTWAIT) == 0) goto bad; splx(s); return;bad: panic("mbinit");}/* * Allocate some number of mbuf clusters * and place on cluster free list. * Must be called at splimp. *//* ARGSUSED */intm_clalloc(ncl, nowait) register int ncl; int nowait;{#ifndef PMON volatile static struct timeval lastlogged; struct timeval curtime, logdiff;#endif register caddr_t p; register int i; int npg; npg = ncl * CLSIZE;#if defined(UVM) p = (caddr_t)uvm_km_kmemalloc(mb_map, uvmexp.mb_object, ctob(npg), nowait ? 0 : UVM_KMF_NOWAIT);#else p = (caddr_t)kmem_malloc(mb_map, ctob(npg), !nowait);#endif if (p == NULL) {#ifndef PMON /* Not very useful... */ int s = splclock(); curtime = time; splx(s); timersub(&curtime, &lastlogged, &logdiff); if (logdiff.tv_sec >= 60) { lastlogged = curtime; log(LOG_ERR, "mb_map full\n"); }#endif m_reclaim(); return (mclfree != NULL); } ncl = ncl * CLBYTES / MCLBYTES; for (i = 0; i < ncl; i++) { ((union mcluster *)p)->mcl_next = mclfree; mclfree = (union mcluster *)p; p += MCLBYTES; mbstat.m_clfree++; } mbstat.m_clusters += ncl; return (1);}/* * When MGET failes, ask protocols to free space when short of memory, * then re-attempt to allocate an mbuf. */struct mbuf *m_retry(i, t) int i, t;{ register struct mbuf *m;printf("MBUFLEAK draining protocols!\n"); if (i & M_DONTWAIT) { needqueuedrain = 1; setsoftnet(); return (NULL); } m_reclaim();#define m_retry(i, t) NULL MGET(m, i, t);#undef m_retry return (m);}/* * As above; retry an MGETHDR. */struct mbuf *m_retryhdr(i, t) int i, t;{ register struct mbuf *m;printf("MBUFLEAK draining protocols!\n"); if (i & M_DONTWAIT) { needqueuedrain = 1; setsoftnet(); return (NULL); } m_reclaim();#define m_retryhdr(i, t) NULL MGETHDR(m, i, t);#undef m_retryhdr return (m);}voidm_reclaim(){ register struct domain *dp; register struct protosw *pr; int s = splimp(); needqueuedrain = 0; for (dp = domains; dp; dp = dp->dom_next) for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) if (pr->pr_drain) (*pr->pr_drain)(); splx(s); mbstat.m_drain++;}/* * Space allocation routines. * These are also available as macros * for critical paths. */struct mbuf *m_get(nowait, type) int nowait, type;{ register struct mbuf *m; MGET(m, nowait, type); return (m);}struct mbuf *m_gethdr(nowait, type) int nowait, type;{ register struct mbuf *m; MGETHDR(m, nowait, type); return (m);}struct mbuf *m_getclr(nowait, type) int nowait, type;{ register struct mbuf *m; MGET(m, nowait, type); if (m == NULL) return (NULL); bzero(mtod(m, caddr_t), MLEN); return (m);}struct mbuf *m_free(m) struct mbuf *m;{ register struct mbuf *n; MFREE(m, n); return (n);}voidm_freem(m) register struct mbuf *m;{ register struct mbuf *n; if (m == NULL) return; do { MFREE(m, n); } while ((m = n) != NULL);}/* * Mbuffer utility routines. *//* * Lesser-used path for M_PREPEND: * allocate new mbuf to prepend to chain, * copy junk along. */struct mbuf *m_prepend(m, len, how) register struct mbuf *m; int len, how;{ struct mbuf *mn; MGET(mn, how, m->m_type); if (mn == NULL) { m_freem(m); return (NULL); } if (m->m_flags & M_PKTHDR) { M_COPY_PKTHDR(mn, m); m->m_flags &= ~M_PKTHDR; } mn->m_next = m; m = mn; if (len < MHLEN) MH_ALIGN(m, len); m->m_len = len; return (m);}/* * Make a copy of an mbuf chain starting "off0" bytes from the beginning, * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. */int MCFail;struct mbuf *m_copym(m, off0, len, wait) register struct mbuf *m; int off0, wait; register int len;{ register struct mbuf *n, **np; register int off = off0; struct mbuf *top; int copyhdr = 0; if (off < 0) panic("m_copym: off %d < 0", off); if (len < 0) panic("m_copym: len %d < 0", len); if (off == 0 && m->m_flags & M_PKTHDR) copyhdr = 1; while (off > 0) { if (m == NULL) panic("m_copym: null mbuf"); if (off < m->m_len) break; off -= m->m_len; m = m->m_next; } np = ⊤ top = NULL; while (len > 0) { if (m == NULL) { if (len != M_COPYALL) panic("m_copym: %d not M_COPYALL", len); break; } MGET(n, wait, m->m_type); *np = n; if (n == NULL) goto nospace; if (copyhdr) { M_COPY_PKTHDR(n, m); if (len == M_COPYALL) n->m_pkthdr.len -= off0; else n->m_pkthdr.len = len; copyhdr = 0; } n->m_len = min(len, m->m_len - off); if (m->m_flags & M_EXT) { n->m_data = m->m_data + off; if (!m->m_ext.ext_ref) mclrefcnt[mtocl(m->m_ext.ext_buf)]++; else (*(m->m_ext.ext_ref))(m); n->m_ext = m->m_ext; n->m_flags |= M_EXT; } else bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), (unsigned)n->m_len); if (len != M_COPYALL) len -= n->m_len; off = 0; m = m->m_next; np = &n->m_next; } if (top == NULL) MCFail++; return (top);nospace: m_freem(top); MCFail++; return (NULL);}#ifndef PMON/* * m_copym2() is like m_copym(), except it COPIES cluster mbufs, instead * of merely bumping the reference count. */struct mbuf *m_copym2(m, off0, len, wait) register struct mbuf *m; int off0, wait; register int len;{ register struct mbuf *n, **np; register int off = off0; struct mbuf *top; int copyhdr = 0; if (len < 0) panic("m_copym2: len %d < 0", len); if (off < 0) panic("m_copym2: off %d < 0", off); if (off == 0 && m->m_flags & M_PKTHDR) copyhdr = 1; while (off > 0) { if (m == NULL) panic("m_copym2: null mbuf"); if (off < m->m_len) break; off -= m->m_len; m = m->m_next; } np = ⊤ top = NULL; while (len > 0) { if (m == NULL) { if (len != M_COPYALL) panic("m_copym2: %d != M_COPYALL", len); break; } MGET(n, wait, m->m_type); *np = n; if (n == NULL) goto nospace; if (copyhdr) { M_COPY_PKTHDR(n, m); if (len == M_COPYALL) n->m_pkthdr.len -= off0; else n->m_pkthdr.len = len; copyhdr = 0; } n->m_len = min(len, m->m_len - off); if ((m->m_flags & M_EXT) && (n->m_len > MHLEN)) { /* This is a cheesy hack. */ MCLGET(n, wait); if (n->m_flags & M_EXT) bcopy(mtod(m, caddr_t) + off, mtod(n, caddr_t), (unsigned)n->m_len); else goto nospace; } else bcopy(mtod(m, caddr_t) + off, mtod(n, caddr_t), (unsigned)n->m_len); if (len != M_COPYALL) len -= n->m_len; off = 0; m = m->m_next; np = &n->m_next; } if (top == NULL) MCFail++; return (top);nospace: m_freem(top); MCFail++; return (NULL);}#endif/* * Copy data from an mbuf chain starting "off" bytes from the beginning, * continuing for "len" bytes, into the indicated buffer. */voidm_copydata(m, off, len, cp) register struct mbuf *m; register int off; register int len; caddr_t cp;{ register unsigned count; if (off < 0) panic("m_copydata: off %d < 0", off); if (len < 0) panic("m_copydata: len %d < 0", len); while (off > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -