DPDK项目中数组&rte_ring&mempool结合使用

文章讨论了数组、rte_ring和mempool在元素管理中的结合使用。通过rte_ring保存数组下标,实现O(1)的查找空闲位置,mempool用于动态分配元素实体。当元素增加或删除时,利用rte_ring高效地跟踪数组状态,确保资源有效利用。

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

数组 && rte_ring && mempool 结合

数组的优缺点

  • 优点:
    知道数组,以及数组的下标,可以O(1)查找到元素。
  • 缺点:
    数组中的元素增删,会导致数组有空闲,且不知道哪个位置空闲。

rte_ring

可以利用 rte_ring 来保存数组的所有下标。

从rte_ring中取一个元素,作为数组的下标,添加一个item到数组中。
从数组中删除item,则只需要将这item的下标再释放到 rte_ring中,同时数组的对应位置设置为NULL;
这样,数组的元素增加,从rte_ring中取下标,可以做到O(1) 找到空闲位置, 填充元素。

注:数组的大小==rte_ring的大小+1。rte_ring 起始可以从 1号开始填充,1-N(忽略0)

mempool

数组中的所有的元素实体都是从 mempool中申请而来。

结合

一般而言,lpm 下一跳 以及 acl 的 action可以保存一个 idx,即元素在数组中的下标。
基于数组,然后可以O(1)来查找到元素。

范例

参考

/** * * Retrieve a burst of input packets from a receive queue of an Ethernet * device. The retrieved packets are stored in *rte_mbuf* structures whose * pointers are supplied in the *rx_pkts* array. * * The rte_eth_rx_burst() function loops, parsing the RX ring of the * receive queue, up to *nb_pkts* packets, and for each completed RX * descriptor in the ring, it performs the following operations: * * - Initialize the *rte_mbuf* data structure associated with the * RX descriptor according to the information provided by the NIC into * that RX descriptor. * * - Store the *rte_mbuf* data structure into the next entry of the * *rx_pkts* array. * * - Replenish the RX descriptor with a new *rte_mbuf* buffer * allocated from the memory pool associated with the receive queue at * initialization time. * * When retrieving an input packet that was scattered by the controller * into multiple receive descriptors, the rte_eth_rx_burst() function * appends the associated *rte_mbuf* buffers to the first buffer of the * packet. * * The rte_eth_rx_burst() function returns the number of packets * actually retrieved, which is the number of *rte_mbuf* data structures * effectively supplied into the *rx_pkts* array. * A return value equal to *nb_pkts* indicates that the RX queue contained * at least *rx_pkts* packets, and this is likely to signify that other * received packets remain in the input queue. Applications implementing * a "retrieve as much received packets as possible" policy can check this * specific case and keep invoking the rte_eth_rx_burst() function until * a value less than *nb_pkts* is returned. * * This receive method has the following advantages: * * - It allows a run-to-completion network stack engine to retrieve and * to immediately process received packets in a fast burst-oriented * approach, avoiding the overhead of unnecessary intermediate packet * queue/dequeue operations. * * - 此api作用
最新发布
03-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值