首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DPDK可以从rte_mbuff数组中删除rte_mbuff吗?

DPDK可以从rte_mbuff数组中删除rte_mbuff吗?
EN

Stack Overflow用户
提问于 2021-12-22 09:18:47
回答 1查看 83关注 0票数 0

我想移除r中的arp pakcet,并正常地发送另一个数据包。这是我的密码

代码语言:javascript
复制
for (;;) {
    /*
     * Receive packets on a port and forward them on the paired
     * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
     */
    RTE_ETH_FOREACH_DEV(port) {
        /* Get burst of RX packets, from first port of pair. */
        struct rte_mbuf *bufs[BURST_SIZE];
        uint16_t nb_rx = rte_eth_rx_burst(port, 0,
                bufs, BURST_SIZE);
        if (unlikely(nb_rx == 0))
            continue;
        /* Send burst of TX packets, to second port of pair. */
    int delete_packet_num = 0;
    for(int i=0;i<nb_rx;i++){
        struct rte_mbuf *p = bufs[i];
        unsigned char *payload =(unsigned char*)p->buf_addr;
        payload = payload+p->data_off;
        struct ethhdr* eth_hdr = (struct ethhdr *)payload;
        if(eth_hdr->h_proto == 1544)
        {
            printf("there is a arp packet!\n");
            //remove a target mbuf or not?
            rte_pktmbuf_free(bufs[i]);
            delete_packet_num++;        
        }
    
    }
    nb_rx -=delete_packet_num;
    const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,bufs, nb_rx);    
    if (unlikely(nb_tx < nb_rx)) {
        uint16_t buf;
        for (buf = nb_tx; buf < nb_rx; buf++)
            rte_pktmbuf_free(bufs[buf]);
    }
}

它可以拒绝我的vmware machine.but中的arp数据包,当代码运行大约半小时时,虚拟机变得非常慢。我猜:

代码语言:javascript
复制
rte_pktmbuf_free(bufs[i])

代码没有释放内存。有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-11 04:51:10

正如注释中提到的,在释放ARP数据包之后,内存的使用是不正确的。因此,要使用的正确代码是

代码语言:javascript
复制
for (;;) {
    /*
     * Receive packets on a port and forward them on the paired
     * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
     */
    RTE_ETH_FOREACH_DEV(port) {
        /* Get burst of RX packets, from first port of pair. */
        struct rte_mbuf *bufs[BURST_SIZE];
        uint16_t nb_rx = rte_eth_rx_burst(port, 0, bufs, BURST_SIZE);
        
        if (unlikely(nb_rx == 0))
            continue;

        /* Send burst of TX packets, to second port of pair. */
        int delete_packet_num = 0;
        int i = 0, j = 0;
        
        for(;i<nb_rx;i++){
            struct rte_mbuf *p = bufs[i];
            
            unsigned char *payload =(unsigned char*)p->buf_addr;
            payload = payload+p->data_off;
            struct ethhdr* eth_hdr = (struct ethhdr *)payload;
            
            if(eth_hdr->h_proto == 1544)
            {
                printf("there is a arp packet!\n");
                //remove a target mbuf or not?
                rte_pktmbuf_free(bufs[i]);
                delete_packet_num++;        
            }
            else
            {
                /*
                 for all valid packets update the list 
                 example:
                    - let nb_rx be 16 packets
                    - in which packet at index 5 is ARP packets
                    - with help of check for ARP we are reassigning bufs[5] = bufs[6], because j is running 1 step once it encounters ARP at index 5
                 */
                bufs[j++] = bufs[i];
            }
        }
    
    nb_rx -=delete_packet_num;
    const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,bufs, nb_rx);    
    if (unlikely(nb_tx < nb_rx)) {
        uint16_t buf;
        for (buf = nb_tx; buf < nb_rx; buf++)
            rte_pktmbuf_free(bufs[buf]);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70446736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档