This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: ebpf-xdp-dsr-flow-invariant-standard

eBPF XDP Direct Server Return (DSR) Flow Invariant & Conntrack Bypass

Enforces strict bounds verification, stateless encapsulation, and non-linear packet header adjustments within eBPF XDP programs executing Layer-4 Direct Server Return (DSR). Prevents Linux kernel networking stack traversal, sk_buff allocation, and nf_conntrack table exhaustion by guaranteeing hardware/driver-level redirection via XDP_TX or devmap lookups with strict MTU safety.

Prohibited Anti-Pattern: Falling back to Linux netfilter conntrack or mutating L3/L4 payloads without incremental checksum recalculation and verifier-safe head-adjustment.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: eBPF XDP Direct Server Return (DSR) Flow Invariant & Conntrack Bypass - Enforces strict bounds verification, stateless encapsulation, and non-linear packet header adjustments within eBPF XDP programs executing Layer-4 Direct Server Return (DSR). Prevents Linux kernel networking stack traversal, sk_buff allocation, and nf_conntrack table exhaustion by guaranteeing hardware/driver-level redirection via XDP_TX or devmap lookups with strict MTU safety.
globs: **/*.bpf.c, **/*.bpf.h, src/bpf/**/*.c, kernel/**/*.c, pkg/loadbalancer/**/*.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-xdp-dsr-flow-invariant-standard"
---

# eBPF XDP Direct Server Return (DSR) Flow Invariant & Conntrack Bypass

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Falling back to Linux netfilter conntrack or mutating L3/L4 payloads without incremental checksum recalculation and verifier-safe head-adjustment.

## System Constraints
You are a Principal Systems & Linux Kernel Engineer enforcing eBPF XDP Direct Server Return (DSR) invariants. Mandate driver-level wire-speed packet processing, zero sk_buff allocations, strict bounds checking for the in-kernel BPF verifier, and full avoidance of nf_conntrack table pollution. Ensure encapsulation preserves the original client 5-tuple for asymmetric return paths.

## Strict Guidelines
- Engineers MUST ALWAYS verify packet boundaries (`data + sizeof(hdr) > data_end`) after every pointer arithmetic operation and call to `bpf_xdp_adjust_head` to satisfy BPF verifier memory safety guarantees.
- Engineers MUST NOT emit `XDP_PASS` for serviceable load-balanced ingress traffic, as passing ingress packets to the host stack triggers expensive `sk_buff` allocation and `nf_conntrack` entry creation, risking hash table exhaustion under line-rate volumetric floods.
- Engineers MUST implement DSR using IP-in-IP (IPIP) or Generic UDP Encapsulation (GUE) rather than Source NAT (SNAT), maintaining the original client IP and port in the inner header so backend service instances reply directly to the client.
- Services MUST compute incremental L3/L4 checksums using `bpf_csum_diff` or RFC 1624 one's complement arithmetic rather than recalculating the entire packet payload checksum inside the XDP hook.
- Components NEVER encapsulate packets exceeding the egress interface MTU without validating the Don't Fragment (DF) flag and generating an ICMP Type 3 Code 4 (Fragmentation Needed) packet back to the client via `XDP_TX`.

## Bad vs Good Implementations
### XDP DSR Packet Encapsulation and Driver-Level Transmit

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The BPF verifier will reject unverified pointer access beyond `ctx->data_end`. Passing the packet to `XDP_PASS` defeats the purpose of eBPF XDP line-rate load balancing, subjecting the kernel to connection tracking (`nf_conntrack_alloc`) lock contention and table exhaustion. Mutating destination IP without encapsulation also prevents asymmetric direct return paths.
```c
// BAD: Mutates packet in-place without head room check, uses full stack PASS,
// and causes nf_conntrack table exhaustion under heavy L4 load.
SEC("xdp")
int xdp_dsr_lb(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;
    struct ethhdr *eth = data;
    struct iphdr *iph = (void *)(eth + 1);

    // Missing bounds checks; fails BPF verifier on strict kernels
    if (iph->protocol == IPPROTO_TCP) {
        // Overwriting destination IP directly loses original client identity
        // and triggers conntrack state creation on return path
        iph->daddr = __constant_htonl(0x0A000002); // 10.0.0.2
        
        // Passing to OS stack: allocates sk_buff, engages conntrack
        return XDP_PASS;
    }
    return XDP_PASS;
}
```

#### Production Standard (Correct)
> **Standard**: Re-reads `ctx->data` and validates boundaries against `ctx->data_end` immediately after `bpf_xdp_adjust_head` to guarantee verifier approval. Prepends an outer IPv4 header for IP-in-IP Direct Server Return, preserving the inner original client 5-tuple, and emits `XDP_TX` to hairpin egress out the same NIC driver without touching kernel conntrack.
```c
// GOOD: Bounds-verified IPIP encapsulation at driver XDP layer with incremental csum update
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

#define BACKEND_IP 0x0A000002 // 10.0.0.2 in host byte order

SEC("xdp")
int xdp_dsr_lb(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;
    struct ethhdr *eth = data;

    if ((void *)(eth + 1) > data_end)
        return XDP_DROP;

    if (eth->h_proto != bpf_htons(ETH_P_IP))
        return XDP_PASS;

    struct iphdr *iph = (void *)(eth + 1);
    if ((void *)(iph + 1) > data_end)
        return XDP_DROP;

    // Prepend outer IPv4 header for DSR encapsulation
    int encap_size = sizeof(struct iphdr);
    if (bpf_xdp_adjust_head(ctx, 0 - encap_size))
        return XDP_DROP;

    // Pointers invalidated after adjust_head; must re-fetch and re-verify
    data = (void *)(long)ctx->data;
    data_end = (void *)(long)ctx->data_end;
    eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_DROP;

    struct iphdr *outer_iph = (void *)(eth + 1);
    if ((void *)(outer_iph + 1) > data_end)
        return XDP_DROP;

    struct iphdr *inner_iph = (void *)(outer_iph + 1);
    if ((void *)(inner_iph + 1) > data_end)
        return XDP_DROP;

    // Populate outer IP header
    outer_iph->version = 4;
    outer_iph->ihl = 5;
    outer_iph->tos = inner_iph->tos;
    outer_iph->tot_len = bpf_htons(bpf_ntohs(inner_iph->tot_len) + encap_size);
    outer_iph->id = 0;
    outer_iph->frag_off = bpf_htons(IP_DF);
    outer_iph->ttl = 64;
    outer_iph->protocol = IPPROTO_IPIP;
    outer_iph->check = 0;
    outer_iph->saddr = inner_iph->daddr;
    outer_iph->daddr = bpf_htonl(BACKEND_IP);

    // Calculate IPv4 checksum for outer header
    __u32 csum = 0;
    __u16 *next_iph_u16 = (__u16 *)outer_iph;
    #pragma unroll
    for (int i = 0; i < (sizeof(struct iphdr) >> 1); i++) {
        csum += *next_iph_u16++;
    }
    outer_iph->check = ~((csum & 0xffff) + (csum >> 16));

    // Direct wire-speed re-transmission bypasses host network stack & conntrack
    return XDP_TX;
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: eBPF XDP Direct Server Return (DSR) Flow Invariant & Conntrack Bypass

eBPF XDP Direct Server Return (DSR) Flow Invariant & Conntrack Bypass defines an industrial-grade behavioral standard for devops engineering. Enforces strict bounds verification, stateless encapsulation, and non-linear packet header adjustments within eBPF XDP programs executing Layer-4 Direct Server Return (DSR). Prevents Linux kernel networking stack traversal, sk_buff allocation, and nf_conntrack table exhaustion by guaranteeing hardware/driver-level redirection via XDP_TX or devmap lookups with strict MTU safety. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Falling back to Linux netfilter conntrack or mutating L3/L4 payloads without incremental checksum recalculation and verifier-safe head-adjustment. Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftFalling back to Linux netfilter conntrack or mutating L3/L4 payloads without incremental checksum recalculation and verifier-safe head-adjustment.Critical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS verify packet boundaries (`data + sizeof(hdr) > data_end`) after every pointer arithmetic operation and call to `bpf_xdp_adjust_head` to satisfy BPF verifier memory safety guarantees.

Engineers MUST ALWAYS verify packet boundaries (`data + sizeof(hdr) > data_end`) after every pointer arithmetic operation and call to `bpf_xdp_adjust_head` to satisfy BPF verifier memory safety guarantees.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT emit `XDP_PASS` for serviceable load-balanced ingress traffic, as passing ingress packets to the host stack triggers expensive `sk_buff` allocation and `nf_conntrack` entry creation, risking hash table exhaustion under line-rate volumetric floods.

Engineers MUST NOT emit `XDP_PASS` for serviceable load-balanced ingress traffic, as passing ingress packets to the host stack triggers expensive `sk_buff` allocation and `nf_conntrack` entry creation, risking hash table exhaustion under line-rate volumetric floods.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST implement DSR using IP-in-IP (IPIP) or Generic UDP Encapsulation (GUE) rather than Source NAT (SNAT), maintaining the original client IP and port in the inner header so backend service instances reply directly to the client.

Engineers MUST implement DSR using IP-in-IP (IPIP) or Generic UDP Encapsulation (GUE) rather than Source NAT (SNAT), maintaining the original client IP and port in the inner header so backend service instances reply directly to the client.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST compute incremental L3/L4 checksums using `bpf_csum_diff` or RFC 1624 one's complement arithmetic rather than recalculating the entire packet payload checksum inside the XDP hook.

Services MUST compute incremental L3/L4 checksums using `bpf_csum_diff` or RFC 1624 one's complement arithmetic rather than recalculating the entire packet payload checksum inside the XDP hook.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER encapsulate packets exceeding the egress interface MTU without validating the Don't Fragment (DF) flag and generating an ICMP Type 3 Code 4 (Fragmentation Needed) packet back to the client via `XDP_TX`.

Components NEVER encapsulate packets exceeding the egress interface MTU without validating the Don't Fragment (DF) flag and generating an ICMP Type 3 Code 4 (Fragmentation Needed) packet back to the client via `XDP_TX`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is eBPF XDP Direct Server Return (DSR) Flow Invariant & Conntrack Bypass critical for AI coding assistants?

Enforces strict bounds verification, stateless encapsulation, and non-linear packet header adjustments within eBPF XDP programs executing Layer-4 Direct Server Return (DSR). Prevents Linux kernel networking stack traversal, sk_buff allocation, and nf_conntrack table exhaustion by guaranteeing hardware/driver-level redirection via XDP_TX or devmap lookups with strict MTU safety. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Falling back to Linux netfilter conntrack or mutating L3/L4 payloads without incremental checksum recalculation and verifier-safe head-adjustment.) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

eBPF