免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2766 | 回复: 0
打印 上一主题 下一主题

mbuf结构体 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-04 16:12 |只看该作者 |倒序浏览

                               
TCP/IP详解第二卷实现主要讲的是BSD的协议栈实现。看协议栈代码,首先看的就是经典的mbuf结构了。原来觉得mbuf结构比较复杂,硬着头皮看了几天后,感觉思路上清晰了一些。于是准备写点东西把理解到的一点点先记下来,省得以后忘记了。
书上是以4.4BSDlite源代码为基础讲的。可以说是目前各个bsd发行版的基础。在此基础上,FreeBSD、NetBSD、OpenBSD做了稍许的改动。下面是lite版的mbuf声明代码:
/*
* Mbufs are of a single size, MSIZE (machine/machparam.h), which
* includes overhead. An mbuf may add a single "mbuf cluster" of size
* MCLBYTES (also in machine/machparam.h), which has no additional overhead
* and is used instead of the internal data area; this is done when
* at least MINCLSIZE of data must be stored.
*/
#define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
#define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
/* header at beginning of each mbuf: */
struct m_hdr {
        struct mbuf *mh_next; /* next buffer in chain */
        struct mbuf *mh_nextpkt; /* next chain in queue/record */
        caddr_t mh_data; /* location of data */
        int mh_len; /* amount of data in this mbuf */
        short mh_type; /* type of data in this mbuf */
        short mh_flags; /* flags; see below */
};
/* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
struct pkthdr {
        struct ifnet *rcvif; /* rcv interface */
        int len; /* total packet length */
};
/* description of external storage mapped into mbuf, valid if M_EXT set */
struct m_ext {
        caddr_t ext_buf; /* start of buffer */
        void (*ext_free)(); /* free routine if not the usual */
        u_int ext_size; /* size of buffer, for ext_free */
};
struct mbuf {
        struct m_hdr m_hdr;
        union {
                struct {
                        struct pkthdr MH_pkthdr; /* M_PKTHDR set */
                        union {
                                struct m_ext MH_ext; /* M_EXT set */
                                char MH_databuf[MHLEN];
                        } MH_dat;
                } MH;
                char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
        } M_dat;
};
[color="#000000"]/* mbuf flags */
#define M_EXT           0x0001  /* has associated external storage */
#define M_PKTHDR        0x0002  /* start of record */
#define M_EOR           0x0004  /* end of record */

可以看到mbuf大体由两部分组成,m_hdr和M_dat。
M_dat是个联合体,使用其中哪个成员是由m_hdr.mh_flags来决定的。
  • 如果mh_flags里没有M_EXT与M_PKTHDR,则表面此mbuf完全是用来存放数据的。总共可以存放MLEN字节的数据;
  • 如果mh_flags里有M_EXT则表面此mbuf使用外部簇来存放数据;
  • 如果mh_flags里有M_PKTHDR,则表面此mbuf是一个记录的起始mbuf,由于MH_pkthdr占用了部分空间,总共只能存放MHLEN字节的数据;
  • 如果mh_flags里既有M_PKTHDR又有M_EXT则表面此mbuf是起始mbuf,但是使用外部簇来存放数据。


                   
                   
                   
                   

    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/355/showart_291392.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP