⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uipc_mbuf.c

📁 完整的TCP/IP源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* uipc_mbuf.c - mbuf routines *//* Copyright 1984-1996 Wind River Systems, Inc. */#include "copyright_wrs.h"/* * Copyright (c) 1982, 1986, 1988, 1991, 1993, 1995 *	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--------------------01n,25aug98,n_s  made _bufCollect only call pr_drain 1 time. spr #2210401m,11dec97,vin  made m_free and m_freem as macros calling netMblkClFree		 and netMblkClChainFree functions.01l,08dec97,vin  removed m_copym merged with netMblkChainDup SPR 9966.		 moved old m_copym code to #if 0 section.01k,01dec97,vin  added system pool configuration logic.		 Ifdefed m_copyback and m_copydata in uipc_mbuf.c these                 need some rework so that they can be tied up efficiently                 with the new buffering scheme.01j,08oct97,vin  fixed clBlkFree.01i,05oct97,vin  bug fix in m_copym01h,19sep97,vin  added clBlk specific code, changed pointers to refcounts		 of clusters to refcounts since refcount has been moved to                 clBlk01g,11aug97,vin  interfaced with netBufLib.c, moved all windRiver specific		 buffering code to netBufLib.c01f,02jul97,vin  SPR 8878.01e,15may97,vin  reworked m_devget(), added support for width of copying.01d,31jan97,vin  removed local variable clLog2Def, cleanup01c,03dec96,vin  changed interface to m_getclr() added additional parameters.01b,13nov96,vin  removed BSD4.4 mbufs, modified m_devget, deleted m_get,		 m_gethdr, m_split, wrote new cluster and mBlk functions.		 moved m_copyback from rtsock.c.01a,03mar96,vin  created from BSD4.4 stuff,integrated with 02v of uipc_mbuf.c*/#include "vxWorks.h"#include "intLib.h"#include "net/mbuf.h"#include "net/systm.h"#include "net/domain.h"#include "net/protosw.h"/* externs */IMPORT struct domain *domains;IMPORT VOIDFUNCPTR _pNetBufCollect;IMPORT VOIDFUNCPTR _pWakeupRtn;/* globals *//* locals */LOCAL int 	MPFail;	 	/* XXX temp variable pullup fails */LOCAL NET_POOL	_netDpool; 	/* system network data pool */LOCAL NET_POOL	_netSysPool;	/* system network structure pool */NET_POOL_ID	_pNetDpool   = &_netDpool; NET_POOL_ID	_pNetSysPool = &_netSysPool; /* forward declarations */LOCAL void	_bufCollect (FAST SEM_ID pSem, VOID ** pPtrHead,                             FAST int *	pNumTasks, M_STAT * pPoolStat);LOCAL STATUS 	_netStackPoolInit (NET_POOL_ID pNetPool,                                   M_CL_CONFIG * pMclConfig,                                   CL_DESC * pClDescTbl,                                   int clTblNumEnt);/********************************************************************************* _netStackPoolInit - initialize the net pool** This routines initializes the netpool.** NOMANUAL** RETURNS: OK/ERROR*/LOCAL STATUS _netStackPoolInit    (    NET_POOL_ID		pNetPool,	/* pointer to a net pool */    M_CL_CONFIG * 	pMclConfig,	/* ptr to mblk/clBlk config tbl */    CL_DESC * 		pClDescTbl,	/* ptr to first entry in clDesc tbl */    int 		clTblNumEnt	/* number of entries */    )    {    int 		ix;		/* index variable */    CL_DESC * 		pClDesc;	/* pointer to the cluster descriptor */    if (pMclConfig->memArea == NULL)	/* do a default allocation */        {        /* memory size adjusted to hold the netPool pointer at the head */        pMclConfig->memSize = (pMclConfig->mBlkNum *                                (MSIZE + sizeof (long))) +            		       (pMclConfig->clBlkNum * CL_BLK_SZ);         if ((pMclConfig->memArea = (char *) malloc (pMclConfig->memSize))            == NULL)            {            panic ("no memory: mBlks/clBlks");            return (ERROR);             }        }    for (pClDesc = pClDescTbl, ix = 0; ix < clTblNumEnt; ix++, pClDesc++)        {        /* memory size adjusted for refcount, pool ptr and 4 byte alignmnt */                if (pClDesc->memArea == NULL) /* do a default allocation */            {            pClDesc->memSize = (pClDesc->clNum *                                (pClDesc->clSize + sizeof (long)));            if ((pClDesc->memArea = (char *)malloc (pClDesc->memSize)) == NULL)                {                panic ("no memory: clusters");                return (ERROR);                }            }        }    return (netPoolInit (pNetPool, pMclConfig, pClDescTbl, clTblNumEnt, NULL));    }/********************************************************************************* mbinit - initialize mbuf package** Initialize a number of mbufs and clusters to start the ball rolling.* This is accomplished by calling routines _mBlkAlloc() and _clAlloc()** NOMANUAL*/void mbinit (void)    {    netBufLibInit ();			/* initialize the buffering library */    _pNetBufCollect = _bufCollect; 	/* intialize buffer collect routine */    _pWakeupRtn	    = wakeup;		/* defined in unixLib.c */    if (_netStackPoolInit (_pNetDpool, &mClBlkConfig, &clDescTbl [0],                           clDescTblNumEnt) != OK)        goto mbInitError;    if (_netStackPoolInit (_pNetSysPool, &sysMclBlkConfig, &sysClDescTbl [0],                           sysClDescTblNumEnt) != OK)        goto mbInitError;    return;        mbInitError:    	{        panic ("mbinit");        return;        }    }/********************************************************************************* _bufCollect - drain protocols and collect required buffers** Must be called at splimp.* * NOMANUAL**/LOCAL void _bufCollect    (    FAST SEM_ID	pSem,			/* pointer to semaphore to block on */    VOID **	pPtrHead,		/* pointer to the head of free list */    FAST int *	pNumTasks,		/* pointer to the no. task blocked */    M_STAT *	pPoolStat		/* pointer to the pools stats struct */    )    {    FAST struct domain	*dp;    FAST struct protosw	*pr;    int	 		level; 		/* level of interrupt */    /* ask protocols to free space */    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)();    level = intLock ();     pPoolStat->mDrain++;		/* no. of times protocols drained */    intUnlock (level);     }/********************************************************************************* mBufClGet - get a new mBlk with a cluster ** This routine allocates an mBlk and points it to a cluster allocated* Different clusters are allocated depending upon the cluster size requested.* * INTERNAL* * NOMANUAL* * RETURNS: NULL/mBlk*/struct mbuf * mBufClGet    (    int			canWait,	/* M_DONTWAIT/M_WAIT */    UCHAR		type,		/* type of data */    int			bufSize,	/* size of the buffer to get */    BOOL		bestFit		/* TRUE/FALSE */    )    {    FAST struct mbuf *	pMblk; 		/* pointer to mbuf */    pMblk = mBlkGet (_pNetDpool, canWait, type); 	/* get an mBlk */    /* allocate a cluster and point the mbuf to it */    if (pMblk && (mClGet (_pNetDpool, pMblk, bufSize, canWait, bestFit) != OK))	{	(void)m_free (pMblk); 	pMblk = NULL; 	}    return (pMblk);    }/********************************************************************************* mHdrClGet - get a new mBlkHdr with a cluster ** This routine allocates an mBlkHdr and points it to a cluster allocated* Different clusters are allocated depending upon the cluster size requested.** INTERNAL*  * NOMANUAL* * RETURNS: NULL/mBlkHdr*/struct mbuf * mHdrClGet    (    int			canWait,		/* M_DONTWAIT/M_WAIT */    UCHAR		type,			/* type of data */    int			bufSize,		/* size of the buffer to get */    BOOL		bestFit			/* TRUE/FALSE */    )    {    FAST struct mbuf *	pMblk = NULL; 			/* pointer to mbuf */    /* get an mBlk/cluster pair */        pMblk = mBlkGet (_pNetDpool, canWait, type); 	/* get an mBlk */    if (pMblk == NULL)        return (NULL);        pMblk->m_flags |= M_PKTHDR;     /* allocate a cluster and point the mbuf to it */    if (mClGet (_pNetDpool, pMblk, bufSize, canWait, bestFit) != OK)	{	(void)m_free (pMblk); 	pMblk = NULL; 	}    return (pMblk);    }/********************************************************************************* m_getclr - get a clean mBlk, cluster pair** This routine allocates an mBlk, points it to a cluster,* and zero's the cluster allocated.** INTERNAL*  * NOMANUAL* * RETURNS: NULL/mBlk*/struct mbuf * m_getclr    (    int 		canwait,		/* M_WAIT/M_DONTWAIT */    UCHAR		type,			/* type of mbuf to allocate */    int			bufSize,		/* size of the buffer to get */    BOOL		bestFit			/* TRUE/FALSE */    )    {    FAST struct mbuf *	pMblk = NULL; 		/* pointer to mbuf */    /* allocate mBlk/cluster pair */        if ((pMblk = mBufClGet (canwait, type, bufSize, bestFit)) != NULL)	bzero(mtod(pMblk, caddr_t), pMblk->m_extSize);     return (pMblk);    }#if 0 /* XXX */#endif /* XXX *//* * Mbuffer utility routines. *//********************************************************************************* m_prepend - prepend an mbuf to a chain.** 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;    int how;    {    struct mbuf *mn;    if ((mn = mBufClGet (how, m->m_type, len, TRUE)) == NULL)	{	m_freem(m);	return ((struct mbuf *)NULL);	}    if (m->m_flags & M_PKTHDR)	{	mn->m_pkthdr = m->m_pkthdr;	mn->m_flags = m->m_flags;	m->m_flags &= ~M_PKTHDR;	}    mn->m_next = m;    m = mn;    if (len < m->m_extSize)	MH_ALIGN(m, len);    m->m_len = len;    return (m);    }/********************************************************************************* m_cat - concatenate mbuf chain n to m.** Concatenate mbuf chain n to m.* Both chains must be of the same type (e.g. MT_DATA).* Any m_pkthdr is not updated.*/void m_cat(m, n)    register struct mbuf * m;    register struct mbuf * n;    {    while (m->m_next)	m = m->m_next;    if (n != NULL)		/* clusters are used by default */	m->m_next = n;     }/********************************************************************************* m_adj - adjust the data in the mbuf**/void m_adj(mp, req_len)    struct mbuf * mp;    int req_len;    {    register int len = req_len;    register struct mbuf *m;    register count;    if ((m = mp) == NULL)	return;    if (len >= 0)	{	/*	 * Trim from head.	 */	while (m != NULL && len > 0)	    {	    if (m->m_len <= len)		{		len -= m->m_len;		m->m_len = 0;		m = m->m_next;		}	    else		{		m->m_len -= len;		m->m_data += len;		len = 0;		}	    }	m = mp;	if (mp->m_flags & M_PKTHDR)	    m->m_pkthdr.len -= (req_len - len);	}     else	{	/*	 * Trim from tail.  Scan the mbuf chain,	 * calculating its length and finding the last mbuf.	 * If the adjustment only affects this mbuf, then just	 * adjust and return.  Otherwise, rescan and truncate	 * after the remaining size.	 */	len = -len;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -