简体   繁体   English

DPDK:修改捕获的数据包头的最有效方法

[英]DPDK: Most efficient way to modify captured packet's headers

i'm trying to modify headers of some GTP packets using the mbuf and mempool libraries, specifically i want to cut out all of ETH, IP, UDP ,GTP layers and obtain a (deep) copy of the packet's payload . 我正在尝试使用mbuf和mempool库修改某些GTP数据包的标头,特别是我想剪切掉所有ETH,IP,UDP,GTP层,并获得数据包有效负载的(深层)副本

Here's the piece of code that should do the work: 这是应该完成工作的代码:

void (const unsigned char* packet, size_t size)
{
    auto outer_header_len = sizeof(ether_header) + sizeof(ip) + sizeof(udphdr) + sizeof(gtp); //length to cut
    uint8_t byte_size = static_cast<uint8_t>(size);
    struct rte_mempool* mbuf_pool;

    struct rte_mbuf *mbuf_pkt = rte_pktmbuf_alloc(mbuf_pool);
    mbuf_pkt->data_len = byte_size;
    mbuf_pkt->pkt_len = byte_size;

    rte_pktmbuf_append(mbuf_pkt, packet[byte_size]);    
    auto payload = rte_pktmbuf_adj(mbuf_pkt, outer_header_len);
}

This function is called from a loop which parses the stream of packets and passes packet and size at each iteration. 从循环中调用此函数,该循环解析数据包流,并在每次迭代时传递数据包大小 Since there will be lots of calls, how can i make my code memory efficient and better? 由于会有很多调用,我该如何提高代码存储的效率和改善性能? Any tips? 有小费吗?

There are few issues with the code. 代码很少有问题。

byte_size 字节大小

uint8_t byte_size = static_cast<uint8_t>(size); I am not sure that is correct, since it basically limiting all packets to 256 bytes, which might not be always the case. 我不确定这是正确的,因为它基本上将所有数据包限制为256个字节,但情况并非总是如此。 So I would just deleted this line, since the size itself is a perfectly fine. 所以我只是删除了这一行,因为size本身就很好。

rte_pktmbuf_append() rte_pktmbuf_append()

rte_pktmbuf_append(mbuf_pkt, packet[byte_size]); this function increases the length of mbuf_pkt , but do not change the mbuf bytes themselves. 此函数会增加mbuf_pkt的长度,但不要更改mbuf字节本身。 So we have to use the pointer it returns to actually do the copy, ie 所以我们必须使用它返回的指针来实际执行复制,即

ptr = rte_pktmbuf_append(mbuf_pkt, size);
if (ptr != NULL)
    rte_memcpy(ptr, packet + outer_header_len, size -outer_header_len);

Here are also the links to the rte_pktmbuf_append() and rte_memcpy() documentation. 这也是指向rte_pktmbuf_append()rte_memcpy()文档的链接。

Other Mbuf Length Manipulations 其他Mbuf长度操作

Since the rte_pktmbuf_append() increases the length of the packet, the following lines are redundant: 由于rte_pktmbuf_append()增加了数据包的长度,因此以下几行是多余的:

mbuf_pkt->data_len = byte_size;
mbuf_pkt->pkt_len = byte_size;
auto payload = rte_pktmbuf_adj(mbuf_pkt, outer_header_len);

Performance 性能

We can copy data without the outer header by taking into account outer_header_len during the data copy as in the example before: 我们可以通过在复制数据时考虑到outer_header_len来复制没有外部标头的数据,如上例所示:

ptr = rte_pktmbuf_append(mbuf_pkt, size);
if (ptr != NULL)
    rte_memcpy(ptr, packet + outer_header_len, size -outer_header_len);

A check if the outer_header_len is less than the packet length might be also required, unless there is such a check made earlier in the code. 可能还需要检查outer_header_len是否小于数据包长度,除非在代码的前面进行了此类检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM