可以使用 DPDK 提供的 rte_eth_dev_info_get() 函数获取绑定到 DPDK 进程中网卡的相关信息,包括 IP 地址、MAC 地址等。
具体步骤如下:
在 DPDK 应用程序中调用 rte_eal_init() 函数初始化环境。
使用 rte_eth_dev_count_avail() 函数获取当前可用的网卡数量。
遍历所有网卡,使用 rte_eth_dev_info_get() 函数获取网卡信息结构体(rte_eth_dev_info),其中包含了 IP 地址、MAC 地址等相关信息。
以下是示例代码:
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#define ETH_ADDR_LEN 6
int main(int argc, char **argv)
{
int ret;
uint16_t nb_ports;
struct rte_eth_dev_info dev_info;
struct ether_addr eth_addr;
uint32_t ip_addr;
// Initialize the Environment Abstraction Layer (EAL).
ret = rte_eal_init(argc, argv);
if (ret < 0) {
rte_exit(EXIT_FAILURE, "rte_eal_init() failed\n");
}
// Get the number of available Ethernet ports.
nb_ports = rte_eth_dev_count_avail();
printf("Number of Ethernet ports: %d\n", nb_ports);
// Iterate over all Ethernet ports and print their information.
for (uint16_t port_id = 0; port_id < nb_ports; port_id++) {
printf("Ethernet Port %u:\n", port_id);
// Get the Ethernet device information structure.
rte_eth_dev_info_get(port_id, &dev_info);
// Print the MAC address.
printf(" MAC Address:");
for (int i = 0; i < ETH_ADDR_LEN; i++) {
printf(" %02X", dev_info.default_mac_addr.addr_bytes[i]);
}
printf("\n");
// Print the IP address.
if (dev_info.nb_ipv4_addrs > 0) {
ip_addr = dev_info.ipv4_addr[0].addr;
printf(" IP Address: %u.%u.%u.%u\n",
(ip_addr >> 24) & 0xFF,
(ip_addr >> 16) & 0xFF,
(ip_addr >> 8) & 0xFF,
ip_addr & 0xFF);
} else {
printf(" No IP address found.\n");
}
}
return 0;
}