microsoft / ebpf-for-windows

eBPF implementation that runs on top of Windows
MIT License
2.65k stars 206 forks source link

xdp_md should be cross-platform portable #3212

Open dthaler opened 5 months ago

dthaler commented 5 months ago

Describe the feature you'd like supported

Today Windows has a different format for xdp_md than Linux does, which interferes with cross-platform portability and blocks a future feature of supporting in-market offload cards like Netronomes.

Linux:

struct xdp_md {
    __u32 data;
    __u32 data_end;
    __u32 data_meta;
    /* Below access go through struct xdp_rxq_info */
    __u32 ingress_ifindex; /* rxq->dev->ifindex */
    __u32 rx_queue_index;  /* rxq->queue_index  */
    __u32 egress_ifindex;  /* txq->dev->ifindex */
};

Windows:

typedef struct xdp_md_
{
    void* data;               ///< Pointer to start of packet data.
    void* data_end;           ///< Pointer to end of packet data.
    uint64_t data_meta;       ///< Packet metadata.
    uint32_t ingress_ifindex; ///< Ingress interface index.

    /* size: 26, cachelines: 1, members: 4 */
    /* last cacheline: 26 bytes */
} xdp_md_t;

data, data_end, and data_meta are 32-bit in Linux, but 64 bit in Windows.

In the context of BPF and XDP, data and data_end are not traditional pointers. Instead, they are offsets from the start of the packet data[2]. These offsets are relative to a base address that is not directly accessible in the BPF program[2]. This design choice is part of the security and safety features of BPF, as it prevents arbitrary memory access[2].

When the BPF program runs, it calculates the actual addresses by adding these offsets to the base address[2]. This calculation is done in 64 bits, which is why the effective pointer size in BPF is 64 bits[2].

Proposed solution

data, data_end, and data_meta should be 32-bit fields in the source, which get converted to 64-bits at time of compation to native code (bpf2c or JIT).

Additional context

No response

dthaler commented 5 months ago

Related to https://github.com/vbpf/ebpf-verifier/issues/560