ZBLOG

20版本的dpdk 接受数据包ip地址 mac地址 tcp端口 代码

以下是基于DPDK 20版本的代码示例,用于接收数据包并提取其IP地址、MAC地址和TCP端口信息:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <netinet/in.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>

#define RX_RING_SIZE 128
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define BURST_SIZE 32

static const struct rte_eth_conf port_conf_default = {
    .rxmode = {
        .max_rx_pkt_len = ETHER_MAX_LEN,
    },
};

int main(int argc, char* argv[]) {
    int ret;
    uint16_t nb_ports;
    uint16_t portid;
    struct rte_mempool* mbuf_pool;

    // Initialize the Environment Abstraction Layer (EAL).
    ret = rte_eal_init(argc, argv);
    if (ret < 0) {
        rte_exit(EXIT_FAILURE, "Error initializing EAL.\n");
    }

    // Check that there is at least one Ethernet port available.
    nb_ports = rte_eth_dev_count_avail();
    if (nb_ports == 0) {
        rte_exit(EXIT_FAILURE, "No Ethernet ports available.\n");
    }

    // Allocate and initialize a memory pool for mbufs.
    mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
    if (mbuf_pool == NULL) {
        rte_exit(EXIT_FAILURE, "Error initializing mbuf pool.\n");
    }

    // Configure each Ethernet port.
    for (portid = 0; portid < nb_ports; portid++) {
        struct rte_eth_conf port_conf = port_conf_default;
        ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
        if (ret != 0) {
            rte_exit(EXIT_FAILURE, "Error configuring Ethernet port %u.\n", portid);
        }

        // Allocate and set up a receive-only queue for the port.
        ret = rte_eth_rx_queue_setup(portid, 0, RX_RING_SIZE, rte_eth_dev_socket_id(portid), NULL, mbuf_pool);
        if (ret != 0) {
            rte_exit(EXIT_FAILURE, "Error setting up RX queue for Ethernet port %u.\n", portid);
        }

        // Start the Ethernet port.
        ret = rte_eth_dev_start(portid);
        if (ret != 0) {
            rte_exit(EXIT_FAILURE, "Error starting Ethernet port %u.\n", portid);
        }
    }

    // Receive and process packets on each Ethernet port.
    while (1) {
        for (portid = 0; portid < nb_ports; portid++) {
            struct rte_mbuf* buf[BURST_SIZE];
            int num_rx = rte_eth_rx_burst(portid, 0, buf, BURST_SIZE);
            if (num_rx > 0) {
                // Extract IP, MAC, and TCP information from each received packet.
                int i;
                for (i = 0; i < num_rx; i++) {
                    struct ether_hdr* eth_hdr = rte_pktmbuf_mtod(buf[i], struct ether_hdr*);
                    struct ipv4_hdr* ip_hdr = (struct ipv4_hdr*)((char*)eth_hdr + sizeof(struct ether_hdr));
                    struct tcp_hdr* tcp_hdr = (struct tcp_hdr*)((char*)ip_hdr + sizeof(struct ipv4_hdr));
                    uint8_t* src_mac = eth_hdr->s_addr.addr_bytes;
                    uint8_t* dst_mac = eth_hdr->d_addr.addr_bytes;
                    uint32_t src_ip = ntohl(ip_hdr->src_addr);
                    uint32_t dst_ip = ntohl(ip_hdr->dst_addr);
                    uint16_t src_port = ntohs(tcp_hdr->src_port);
                    uint16_t dst_port = ntohs(tcp_hdr->dst_port);

                    // Print the extracted information.
                    printf("Packet received on port %u:\n", portid);
                    printf("\tSource MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5]);
                    printf("\tDestination MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", dst_mac[0], dst_mac[1], dst_mac[2], dst_mac[3], dst_mac[4], dst_mac[5]);
                    printf("\tSource IP: %u.%u.%u.%u\n", (src_ip >> 24) & 0xFF, (src_ip >> 16) & 0xFF, (src_ip >> 8) & 0xFF, src_ip & 0xFF);
                    printf("\tDestination IP: %u.%u.%u.%u\n", (dst_ip >> 24) & 0xFF, (dst_ip >> 16) & 0xFF, (dst_ip >> 8) & 0xFF, dst_ip & 0xFF);
                    printf("\tSource TCP port: %u\n", src_port);
                    printf("\tDestination TCP port: %u\n", dst_port);

                    rte_pktmbuf_free(buf[i]);
                }
            }
        }
    }
}

这个示例代码中:

  • rte_eal_init 函数初始化DPDK环境抽象层(EAL)。
  • rte_eth_dev_count_avail 函数检查是否有可用的Ethernet端口。
  • rte_pktmbuf_pool_create 函数分配和初始化一个MBUF内存池。
  • rte_eth_dev_configure 函数配置每个Ethernet端口。
  • rte_eth_rx_queue_setup 函数分配和设置每个端口的接收队列。
  • rte_eth_dev_start 函数启动每个端口。
  • rte_eth_rx_burst 函数从每个端口接收数据包,并将它们放入缓冲区中。
  • 在缓冲区中,我们可以访问以太网头、IP头和TCP头,并提取源地址/目的地址、源端口/目的端口等信息。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=443

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?