zephyr中net_buf的定义

本文介绍了Zephyr操作系统中的net_buf数据结构,包括相关数据结构的详细说明,如数组_net_buf_pool_bufs_pool和bufs_pool变量的使用。net_buf_pool_bufs_pool是一个缓冲区池,而bufs_pool描述了池的属性,如缓冲区数量、使用情况、地址、释放和销毁函数以及每个缓冲区的大小。此外,文章还提及了lifo结构在kernellifo.c中的定义和实现,以及它作为一个双向链表的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、相关数据结构说明:

数据结构如下:

struct net_buf {
    union {
        int _unused;
        struct net_buf *frags;
    };

    uint8_t ref;
    uint8_t flags;
    struct net_buf_pool *pool;
    union {
        struct {
            uint8_t *data;
            uint16_t len;
            uint16_t size;
        };

        struct net_buf_simple b;
    };
    uint8_t __buf[0] __net_buf_align;
};

struct net_buf_pool {
    struct k_lifo free;  // LIFO to place the buffer into when free 
    const uint16_t buf_count;
    uint16_t uninit_count;
    const uint16_t buf_size;
    const uint16_t user_data_size;
    void (*const destroy)(struct net_buf *buf);
    struct net_buf * const __bufs;
};
#define SYS_DLIST_STATIC_INIT(ptr_to_list) {{(ptr_to_list)}, {(ptr_to_list)}}

#define K_LIFO_INITIALIZER(obj) \
    { \
    .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
    .list = NULL, \
    _OBJECT_TRACING_INIT \
    }

#define NET_BUF_POOL_INITIALIZER(_pool, _bufs, _count, _size, _ud_size,      \
                 _destroy)                                   \
    {                                                                    \
        .free = K_LIFO_INITIALIZER(_pool.free),                      \
        .__bufs = (struct net_buf *)_bufs,                           \
        .buf_count = _count,                                         \
        .uninit_count = _count,                                      \
        .buf_size = _size,                                           \
        .user_data_size = _ud_size,                                  \
        .destroy = _destroy,                                         \
    }
#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy)        \
static struct {                                                      \
        struct net_buf buf;                                          \
        uint8_t data[_size] __net_buf_align;                         \
        uint8_t ud[ROUND_UP(_ud_size, 4)] __net_buf_align;           \
    } _net_buf_pool_##_name[_count] __noinit;                            \
    static struct net_buf_pool _name =                                   \
        NET_BUF_POOL_INITIALIZER(_name, _net_buf_pool_##_name,       \
                     _count, _size, _ud_size, _destroy)

用以上的定义,来声明几个变量,用一个宏来搞定

NET_BUF_POOL_DEFINE(bufs_pool, 22, 74, 6, buf_destroy);

这个宏展开后如下

    static struct {                                                      
        struct net_buf buf;                                          
        uint8_t data[74] __net_buf_align;                        
        uint8_t ud[ROUND_UP(6, 4)] __net_buf_align;           
    } _net_buf_pool_bufs_pool[22] __noinit;

    static struct net_buf_pool bufs_pool =                                   
    {                                                                    
        .free = { 
                .wait_q = {{(&bufs_pool.free.wait_q)}, {(&bufs_pool.free.wait_q)}},
                .list = NULL, 
                _OBJECT_TRACING_INIT //与这个结构没有关系,就先不展开这个宏定义了
                },

        .__bufs = (struct net_buf *)_net_buf_pool_bufs_pool,                           
        .buf_count = 22,                                         
        .uninit_count = 22,                                      
        .buf_size = 74,                                           
        .user_data_size = 6,                                  
        .destroy = buf_destroy,                                         
    }

相当于定义了一个数组_net_buf_pool_bufs_pool[22] 和一个变量bufs_pool.

bufs_pool用来描述一个buffer池的,即这个buffer池有多少个buffer,用了多少个,首地址在什么地方,释放和销毁的函数是什么,每个buffer的大小等信息。

net_buf_pool_bufs_pool[22] 是具体的buffer池子,bufs_pool实际上是用来描述这个池子的。

2、补充:
(lifo在\kernel\lifo.c中定义和实现相关的函数)
补充罗列不太相关的结构定义,在前面有用到了,实际上是一个双向链表:

struct _dnode {
    union {
        struct _dnode *head; /* ptr to head of list (sys_dlist_t) */
        struct _dnode *next; /* ptr to next node    (sys_dnode_t) */
    };
    union {
        struct _dnode *tail; /* ptr to tail of list (sys_dlist_t) */
        struct _dnode *prev; /* ptr to previous node (sys_dnode_t) */
    };
};
typedef struct _dnode sys_dlist_t;
typedef sys_dlist_t _wait_q_t;
struct k_lifo {
    _wait_q_t wait_q;
    void *list;

    _OBJECT_TRACING_NEXT_PTR(k_lifo);
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值