Course contentsShow
← Linux
Lesson 1188 of 1,81910. Network Administration and Kernel StacksFree lesson

XDP Redirects and Virtual Networking

Move packets directly between interfaces with near-zero overhead.

In this lesson, you will learn how to bypass the standard Linux networking stack to move packets directly between virtual and physical interfaces using XDP redirects.

The Problem with the Standard Stack

Normally, when a packet arrives at an interface, the kernel performs a series of expensive operations: it allocates an sk_buff (socket buffer) structure, traverses the IP stack, consults the routing table, and finally prepares the packet for transmission out of another interface. For high-speed virtual networking—such as moving traffic between a physical NIC and a virtual container interface (veth)—this path introduces significant latency and CPU overhead.

The XDP Redirect Approach

XDP (eXpress Data Path) allows us to intercept packets at the very first moment the driver receives them. Instead of letting the kernel process the packet, we can use the bpf_redirect_map helper function to steer the packet directly into the transmit queue of a target interface.

When you use XDP redirects with virtual networking, the packet essentially "teleports" from one interface buffer to another without being converted into a formal kernel networking structure. This results in "near-zero overhead" because you are essentially performing a direct memory copy from the source RX ring to the destination TX ring.

Implementation Snippet

In your BPF program, you define a DEVMAP that maps an index to a specific target network device. When a packet arrives, your logic decides the destination and calls the redirect helper:

// Define the destination map
struct {
    __uint(type, BPF_MAP_TYPE_DEVMAP);
    __uint(max_entries, 16);
    __type(key, int);
    __type(value, int);
} tx_port_map SEC(".maps");

// In the XDP hook:
int xdp_prog(struct xdp_md *ctx) {
    // Logic to select interface index 1
    return bpf_redirect_map(&tx_port_map, 1, 0);
}

By bypassing the skb allocation and the entire IP stack traversal, you achieve line-rate performance for virtual switching, which is critical in modern containerized and NFV (Network Functions Virtualization) environments. You are moving raw frame data directly where it needs to go, long before the kernel even realizes a packet has arrived.

Key Takeaway: XDP redirects allow you to bypass the kernel stack and copy packets directly between network interfaces, achieving near-zero latency for virtual switching tasks.