dpdk - DPDK 函数 rte_pktmbuf_alloc 和 rte_pktmbuf_free 线程安全吗?
问题描述
我们有一个用例,它有一个 RX 线程和两个 TX 线程在同一个端口上运行。RX 线程在处理收到的数据包后使用 rte_pktmbuf_free 释放 mbuf。TX 线程使用 rte_pktmbuf_alloc 调用分配 mbuf 并在端口上传输。
当为 RX 描述符 init 和 TX 线程中的 mbuf 分配使用相同的池时,我们看到有时会出现意外的 mbuf 泄漏和分配失败。如果我们为 RX 和每个 TX 线程使用单独的池,那么我们看不到这些问题
我们没有在 mempool_create 调用中使用任何标志。DPDK 文档似乎表明内存池操作是线程安全的?
我们正在使用 DPDK 版本:19.11
我们是否应该为每个 RX 和两个 TX 线程创建唯一的内存池?
EDIT1:按 Vipin 要求添加的代码片段
DPDK 初级流程
mbuf_pool = rte_pktmbuf_pool_create("fast_pkt", 8192,
256, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
/* Configure the Ethernet device. */
retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
/* Allocate and set up 1 RX queue per Ethernet port. */
retval = rte_eth_rx_queue_setup(port, q, 128,
rte_eth_dev_socket_id(port), NULL, mbuf_pool);
/* Allocate and set up 2 TX queue */
for (q = 0; q < tx_rings; q++) {
retval = rte_eth_tx_queue_setup(port, q, 512,
rte_eth_dev_socket_id(port), NULL);
}
/* Start the Ethernet port. */
retval = rte_eth_dev_start(port);
二次加工
主线程
mbuf_pool = rte_mempool_lookup("fast_pkt");
/* Create rx thread attach it to vCPU #4 */
rte_eal_remote_launch( main_rx, NULL, 4 );
/* create two tx threads and assign affinity to different vCPUs*/
pthread_create(&thread_id, NULL, tx_1, NULL );
CPU_ZERO(&cpuset);
CPU_SET(5, &cpuset);
pthread_setaffinity_np(thread_id, sizeof(cpuset), &cpuset);
pthread_create(&thread_id, NULL, tx_2, NULL );
CPU_ZERO(&cpuset);
CPU_SET(6, &cpuset);
pthread_setaffinity_np(thread_id, sizeof(cpuset), &cpuset);
接收线程 (main_rx)
for (;; )
{
nb_rx = rte_eth_rx_burst(port , 0, pkts_burst, 32);
for ( i = 0; i < nb_rx ; i++ )
{
process_packet( pkts_burst[ i] );
rte_pktmbuf_free(pkts_burst[ i]);
}
}
传输线程(tx_1,tx_2)
tx_q = <thread_sepcific queue ID>
for ( ;; )
{
/* Receive Internal message for TX*/
get_packet ( work_buffer [Burst]);
for ( iterate through each index in work_buffer )
{
tx_mbuf[i] = rte_pktmbuf_alloc(mbuf_pool);
/* combination of following calls to construct packet */
rte_pktmbuf_mtod( .. );
/* follow above call with copy from work buffer */
rte_pktmbuf_prepend( .. );
rte_pktmbuf_append( .. );
}
rte_eth_tx_burst(start_port_id, tx_q, tx_mbuf, num_burst)
/* loop through packets not processed by rte_eth_tx_burst */
for(.. )
{
rte_pktmbuf_free (... );
}
}
解决方案
[下面共享的 EDIT-1 答案是共享的融合问题并提供了澄清]
- 问题 1
is mempool operations are thread-safe?
-(答案)是的,它与 Lcore DPDK 线程有关。 - 问题 2
Are we supposed to create unique mempool for each of the RX and the two TX threads?
-(答案)不,可以安全地跨不同端口和端口队列对使用相同的内存池。 - 问题 3
there is mbuf leak and depletion of mbuf_pool (sample code flow shared in question)
- (答案)更正代码后,它已经运行了 12 多个小时而没有泄漏。 - 问题 4
we wanted to understand if the rte_pktmbuf_alloc and rte_pktmbuf_free are intended to be thread-safe or not
- (答案)是的 - 问题 5
Our RX thread uses "rte_eal_remote_launch" whereas our TX threads are created directly using pthread_create from the application. Do you think that makes a difference to thread safety? Should our TX threads also be launched using "rte_eal_remote_launch"?
- (答案)是的 - 问题6
we should not have multiple threads (I.e., pthreads) allocated to same lcore and accessing same memory pool.
- (答案)是的,这是正确的理解
原因:DPDK lcore 线程在内部完成的事情比thread create and set affinity
. 请参阅DPDK EAL 文档以了解清楚。基于选择的选项rte_eal_init
- coremask(十六进制)是 DPDK lcore 线程的 1:1 CPU 逻辑内核。
- lcores(十进制)是 DPDK lcore 线程的 1:1 CPU 逻辑核心。
- lcoresmask(十进制)为 1:N,其中 1 个 CPU 逻辑核心可以拆分为
N
DPDK lcore 线程。 - 每个 DPDK lcore 线程在内部存储线程局部变量
index
,即rte_lcore_id
- 因此,当使用
rte_pktmbuf_alloc
它时,它会使用id
来获取缓存内存池对象。在多个线程上使用相同的 id 违反了无锁模型,如果多个请求同时出现,可能会导致危险。
skeleton
因此,使用正确的 API 调用在主从模式下使用 DPDK 示例重写代码片段不会产生内存泄漏。显示下面共享的运行的统计信息
######################## NIC statistics for port 0 ########################
RX-packets: 15048531 RX-errors: 0 RX-bytes: 1123238216
RX-nombuf: 0
TX-packets: 15048504 TX-errors: 0 TX-bytes: 902910240
############################################################################
######################## NIC statistics for port 0 ########################
RX-packets: 23424430063 RX-errors: 0 RX-bytes: 1405686130136
RX-nombuf: 0
TX-packets: 23424430036 TX-errors: 0 TX-bytes: 1405465802032
############################################################################
RX-nombuf
轻松显示 PMD 是否由于缺少 mbuf 而无法获取数据包
内存池信息
========== show - MEMPOOL ==========
mempool <MBUF_POOL>@0x11ff9fad00
flags=10
socket_id=1
pool=0x11ff9eab00
iova=0xeff9fad00
nb_mem_chunks=1
size=8191
populated_size=8191
header_size=64
elt_size=2304
trailer_size=0
total_obj_size=2368
private_data_size=64
ops_index=7
ops_name: <ring_mp_mc>
avg bytes/object=2368.289098
internal cache infos:
cache_size=250
cache_count[32]=171
cache_count[34]=90
total_cache_count=261
common_pool_count=2874
注意:以上统计数据是通过 dpdk-proc-info 实用程序获取的。
推荐阅读
- pointers - 在函数中使用多个指针
- python - 将压缩后的 Stata 文件从 URL 读取到 pandas
- android - 在 React Native 项目中重新生成 Android 和 IOS 文件夹
- ethereum - 有没有办法将函数返回为 false 并且仍然执行代码直到它恢复,eq: function owner() { owner = 0x0; 恢复(); }
- r - 将用 R 制作的闪亮/传单地图发布到网站
- java - 错误:无法找到或加载主类 Maven Spring Boot 应用程序 - 可执行 JAR
- c# - C 结构并在 C# 中使用 C dll
- xna - XNA Mediaplayer.Play() 开始在不同时间跨计算机播放?
- c++ - 关于winrt Demo对象的大小
- linux - 未找到 EMCC - 仅在 emsdk 中有效,在终端中不全局