This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: ebpf-tc-dsr-conntrack-bypass-guardrail

eBPF TC Socket-Layer DSR Packet Scheduling & Conntrack Bypass Invariant

Mandates kernel-space eBPF TC and socket-layer hooks to implement Direct Server Return (DSR), completely bypassing Linux Netfilter connection tracking (nf_conntrack). Prevents conntrack table exhaustion, SNAT port starvation, and packet drops under multi-gigabit ingress loads by delegating reverse packet egress directly to backend nodes.

Prohibited Anti-Pattern: Routing Multi-Gigabit Ingress Traffic Through Netfilter Conntrack and Reverse-Proxy SNAT

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: eBPF TC Socket-Layer DSR Packet Scheduling & Conntrack Bypass Invariant - Mandates kernel-space eBPF TC and socket-layer hooks to implement Direct Server Return (DSR), completely bypassing Linux Netfilter connection tracking (nf_conntrack). Prevents conntrack table exhaustion, SNAT port starvation, and packet drops under multi-gigabit ingress loads by delegating reverse packet egress directly to backend nodes.
globs: bpf/**/*.c, bpf/**/*.h, pkg/datapath/**/*.go, deploy/k8s/**/*.yaml, src/ebpf/**/*.c
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-tc-dsr-conntrack-bypass-guardrail"
---

# eBPF TC Socket-Layer DSR Packet Scheduling & Conntrack Bypass Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Routing Multi-Gigabit Ingress Traffic Through Netfilter Conntrack and Reverse-Proxy SNAT

## System Constraints
You are a Principal Systems and DevOps Architect enforcing kernel-space eBPF TC (Traffic Control) Socket-Layer Direct Server Return (DSR) invariants. Mandate zero Netfilter conntrack allocation for ingress L4 service loads, verify MTU encapsulation room, enforce socket-level bypass via sock_ops/bpf_sk_lookup, and prevent return traffic routing bottlenecks.

## Strict Guidelines
- Engineers MUST ALWAYS attach eBPF TC ingress hooks (`cls_bpf`) with `BPF_F_INGRESS` to schedule incoming VIP packets directly via DSR encapsulation before Netfilter `PREROUTING` connection tracking runs.
- Engineers MUST NOT allow ingress service packets to trigger `nf_conntrack` entry allocation or rely on IPVS/iptables node-level NAT tables that bottleneck at `net.netfilter.nf_conntrack_max`.
- Code SHOULD validate packet length and enforce MTU headroom dynamically using `bpf_skb_adjust_room()` with `BPF_ADJ_ROOM_NET` before applying Geneve, GUE, or IP-in-IP DSR headers to avoid ICMP fragmentation storms.
- Services MUST configure backend socket-layer programs (`BPF_PROG_TYPE_SOCK_OPS` / `BPF_PROG_TYPE_SK_LOOKUP`) to intercept VIP socket binds and write the original VIP into the IP source header on egress, ensuring direct return to the client without load balancer re-entry.
- Components NEVER drop or ignore return flow checksum recalculations; all L3/L4 offsets must be recomputed via `bpf_l3_csum_replace()` and `bpf_l4_csum_replace()` upon DSR header injection or translation.

## Bad vs Good Implementations
### Kernel eBPF TC Hook: Netfilter Conntrack Fallback vs. Zero-Conntrack DSR Encapsulation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Returning TC_ACT_OK without encapsulating or redirecting hands the packet over to Linux kernel IP/Netfilter stacks, triggering `nf_conntrack` table state allocation per connection tuple and leading to multi-gigabit ingress drops when tables fill.
```c
// ANTI-PATTERN: Passing VIP packet down standard Linux IP stack for IPVS/Netfilter NAT.
// Allocates an nf_conntrack entry for every new tuple, exhausting conntrack tables.
#include <linux/bpf.h>
#include <linux/pkt_cls.h>
#include <bpf/bpf_helpers.h>

SEC("classifier/ingress")
int handle_vip_ingress_bad(struct __sk_buff *skb) {
    // Naive packet forwarding without DSR encapsulation:
    // Lets packet traverse PREROUTING -> conntrack allocation -> IPVS DNAT.
    // Under 10Mpps SYN/UDP load, nf_conntrack tables exhaust instantly.
    return TC_ACT_OK; // Passes up to Netfilter nf_conntrack stack
}
```

#### Production Standard (Correct)
> **Standard**: Performs IP-in-IP DSR encapsulation at the eBPF TC layer, updating SKB headroom and redirecting directly to the backend interface via `bpf_redirect()`. The packet bypasses local Netfilter conntrack completely, eliminating conntrack state exhaustion.
```c
// PRODUCTION STANDARD: Direct Server Return (DSR) eBPF TC classifier with zero conntrack allocation.
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/pkt_cls.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

#define DSR_ENCAP_LEN sizeof(struct iphdr)

SEC("tc/ingress")
int handle_vip_ingress_good(struct __sk_buff *skb) {
    void *data_end = (void *)(long)skb->data_end;
    void *data = (void *)(long)skb->data;
    struct ethhdr *eth = data;

    if ((void *)(eth + 1) > data_end)
        return TC_ACT_SHOT;
    if (eth->h_proto != bpf_htons(ETH_P_IP))
        return TC_ACT_OK;

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

    // Verify VIP destination (e.g., 10.244.0.100)
    if (iph->daddr == bpf_htonl(0x0AF40064)) {
        // Expand headroom for outer IP-in-IP DSR header before conntrack hook runs
        if (bpf_skb_adjust_room(skb, DSR_ENCAP_LEN, BPF_ADJ_ROOM_NET, 0) < 0)
            return TC_ACT_SHOT;

        // Re-read data pointers after adjustment
        data = (void *)(long)skb->data;
        data_end = (void *)(long)skb->data_end;
        eth = data;
        if ((void *)(eth + 1) > data_end)
            return TC_ACT_SHOT;

        struct iphdr *outer_ip = (void *)(eth + 1);
        if ((void *)(outer_ip + 1) > data_end)
            return TC_ACT_SHOT;

        // Populate outer IP header for backend node routing
        outer_ip->version = 4;
        outer_ip->ihl = 5;
        outer_ip->tos = 0;
        outer_ip->tot_len = bpf_htons(bpf_ntohs(iph->tot_len) + DSR_ENCAP_LEN);
        outer_ip->ttl = 64;
        outer_ip->protocol = IPPROTO_IPIP;
        outer_ip->saddr = iph->daddr;
        outer_ip->daddr = bpf_htonl(0x0A000114); // Target backend node IP: 10.0.1.20
        outer_ip->check = 0;

        // Bypass local conntrack entirely and redirect to egress interface
        return bpf_redirect(skb->ifindex, 0);
    }

    return TC_ACT_OK;
}
```

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 TC Socket-Layer DSR Packet Scheduling & Conntrack Bypass Invariant

eBPF TC Socket-Layer DSR Packet Scheduling & Conntrack Bypass Invariant defines an industrial-grade behavioral standard for devops engineering. Mandates kernel-space eBPF TC and socket-layer hooks to implement Direct Server Return (DSR), completely bypassing Linux Netfilter connection tracking (nf_conntrack). Prevents conntrack table exhaustion, SNAT port starvation, and packet drops under multi-gigabit ingress loads by delegating reverse packet egress directly to backend nodes. 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: Routing Multi-Gigabit Ingress Traffic Through Netfilter Conntrack and Reverse-Proxy SNAT 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 DriftRouting Multi-Gigabit Ingress Traffic Through Netfilter Conntrack and Reverse-Proxy SNATCritical
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 attach eBPF TC ingress hooks (`cls_bpf`) with `BPF_F_INGRESS` to schedule incoming VIP packets directly via DSR encapsulation before Netfilter `PREROUTING` connection tracking runs.

Engineers MUST ALWAYS attach eBPF TC ingress hooks (`cls_bpf`) with `BPF_F_INGRESS` to schedule incoming VIP packets directly via DSR encapsulation before Netfilter `PREROUTING` connection tracking runs.

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 allow ingress service packets to trigger `nf_conntrack` entry allocation or rely on IPVS/iptables node-level NAT tables that bottleneck at `net.netfilter.nf_conntrack_max`.

Engineers MUST NOT allow ingress service packets to trigger `nf_conntrack` entry allocation or rely on IPVS/iptables node-level NAT tables that bottleneck at `net.netfilter.nf_conntrack_max`.

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

Code SHOULD validate packet length and enforce MTU headroom dynamically using `bpf_skb_adjust_room()` with `BPF_ADJ_ROOM_NET` before applying Geneve, GUE, or IP-in-IP DSR headers to avoid ICMP fragmentation storms.

Code SHOULD validate packet length and enforce MTU headroom dynamically using `bpf_skb_adjust_room()` with `BPF_ADJ_ROOM_NET` before applying Geneve, GUE, or IP-in-IP DSR headers to avoid ICMP fragmentation storms.

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 configure backend socket-layer programs (`BPF_PROG_TYPE_SOCK_OPS` / `BPF_PROG_TYPE_SK_LOOKUP`) to intercept VIP socket binds and write the original VIP into the IP source header on egress, ensuring direct return to the client without load balancer re-entry.

Services MUST configure backend socket-layer programs (`BPF_PROG_TYPE_SOCK_OPS` / `BPF_PROG_TYPE_SK_LOOKUP`) to intercept VIP socket binds and write the original VIP into the IP source header on egress, ensuring direct return to the client without load balancer re-entry.

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 drop or ignore return flow checksum recalculations

Components NEVER drop or ignore return flow checksum recalculations; all L3/L4 offsets must be recomputed via `bpf_l3_csum_replace()` and `bpf_l4_csum_replace()` upon DSR header injection or translation.

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 TC Socket-Layer DSR Packet Scheduling & Conntrack Bypass Invariant critical for AI coding assistants?

Mandates kernel-space eBPF TC and socket-layer hooks to implement Direct Server Return (DSR), completely bypassing Linux Netfilter connection tracking (nf_conntrack). Prevents conntrack table exhaustion, SNAT port starvation, and packet drops under multi-gigabit ingress loads by delegating reverse packet egress directly to backend nodes. 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 (Routing Multi-Gigabit Ingress Traffic Through Netfilter Conntrack and Reverse-Proxy SNAT) 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