上一篇我们分析了hmap,hamp可以说是Open vSwitch中基石结构,很多Open vSwitch中数据结构都依赖hmap。本篇我们来分析一下ofpbuf,这个结构,我们从名字上就可得知,此数据结构用于存储数据的,比如收发OpenFlow报文。 我们首先来看一下,它数据结构定义。(有些内容我是直接写在代码注释中的)
/* Buffer for holding arbitrary data. An ofpbuf is automatically reallocated
* as necessary if it grows too large for the available memory.
*
* 'frame' and offset conventions:
*
* Network frames (aka "packets"): 'frame' MUST be set to the start of the
* packet, layer offsets MAY be set as appropriate for the packet.
* Additionally, we assume in many places that the 'frame' and 'data' are
* the same for packets.
*
* OpenFlow messages: 'frame' points to the start of the OpenFlow
* header, while 'l3_ofs' is the length of the OpenFlow header.
* When parsing, the 'data' will move past these, as data is being
* pulled from the OpenFlow message.
*
* Actions: When encoding OVS action lists, the 'frame' is used
* as a pointer to the beginning of the current action (see ofpact_put()).
*
* rconn: Reuses 'frame' as a private pointer while queuing.
*/
struct ofpbuf {//这个有一个预编译,为了简单起见,我们认为DPDK_NETDEV宏无效(关于DPDK网上有很多资料)。
#ifdef DPDK_NETDEV
struct rte_mbuf mbuf; /* DPDK mbuf */
#else
void *base_; /* First byte of allocated space. 指向内存申请的起始位置。释放内存时候此变量传给free */
void *data_; /* First byte actually in use. 指向当前可用内存起始位置。最开始base_和data_ 是一样的 */
uint32_t size_; /* Number of bytes in use. 表示内存已经使用的字节数 当size_ = allocated时候表示内存用完。 */
#endif
uint32_t allocated; /* Number of bytes allocated. 表示从系统中申请的内存块大小*/
void *frame; /* Packet frame start, or NULL. 这个字段可参考上面注释*/
uint16_t l2_5_ofs; /* MPLS label stack offset from 'frame', or
* UINT16_MAX 2.5层 偏移量 */
uint16_t l3_ofs; /* Network-level header offset from 'frame',
or UINT16_MAX. 3层网络层 偏移量*/
uint16_t l4_ofs; /* Transport-level header offset from 'frame',
or UINT16_MAX. 4层传输层 偏移量*/
enum ofpbuf_source source; /* Source of memory allocated as 'base'. 表示该内存来自堆、栈,主要用于内存释放。取值为ofpbuf_source枚举*/
struct list list_node; /* Private list element for use by owner. 链表节点。 用于将多个ofpbuf关联在一起 */
};
//枚举类型
enum OVS_PACKED_ENUM ofpbuf_source {
OFPBUF_MALLOC, /* Obtained via malloc(). */