如何确定以太网帧是否为 IPv4 数据包?

How to determine if the ethernet frame is an IPv4 packet?

我在 DPDK 应用程序中使用 pcap_pmd,它不会自动设置 packet_type 字段。从接口接收到数据包后如何判断正确的数据包类型?

编辑: 以太网头结构:

/**
 * Ethernet header: Contains the destination address, source address
 * and frame type.
 */
struct ether_hdr {
    struct ether_addr d_addr; /**< Destination address. */
    struct ether_addr s_addr; /**< Source address. */
    uint16_t ether_type;      /**< Frame type. */
} __attribute__((__packed__));

没有给出在哪里搜索 L3 协议标识符的线索。

深入研究 rte_ether.h header 给出了答案:ether_hdr::ether_type 是所需的下一个协议 ID,它可以具有以下值之一:

/* Ethernet frame types */
#define ETHER_TYPE_IPv4 0x0800 /**< IPv4 Protocol. */
#define ETHER_TYPE_IPv6 0x86DD /**< IPv6 Protocol. */
#define ETHER_TYPE_ARP  0x0806 /**< Arp Protocol. */
#define ETHER_TYPE_RARP 0x8035 /**< Reverse Arp Protocol. */
#define ETHER_TYPE_VLAN 0x8100 /**< IEEE 802.1Q VLAN tagging. */
#define ETHER_TYPE_1588 0x88F7 /**< IEEE 802.1AS 1588 Precise Time Protocol. */
#define ETHER_TYPE_SLOW 0x8809 /**< Slow protocols (LACP and Marker). */
#define ETHER_TYPE_TEB  0x6558 /**< Transparent Ethernet Bridging. */