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

In-Kernel W3C TraceContext Propagation via eBPF XDP with Zero-Copy Header Mutation

Mandates in-kernel parsing, extraction, and injection of W3C TraceContext headers directly within the eBPF XDP driver layer. Eliminates userspace context switching, socket bouncing, and TLS de-serialization latency for ingress/egress network flows. Enforces verifier-compliant boundary safety, MTU bounds enforcement, and incremental RFC-compliant L3/L4 checksum recalculation.

Prohibited Anti-Pattern: Bouncing Packets to Userspace Proxies or Mutating Headers Without In-Kernel Checksum Recalculation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: In-Kernel W3C TraceContext Propagation via eBPF XDP with Zero-Copy Header Mutation - Mandates in-kernel parsing, extraction, and injection of W3C TraceContext headers directly within the eBPF XDP driver layer. Eliminates userspace context switching, socket bouncing, and TLS de-serialization latency for ingress/egress network flows. Enforces verifier-compliant boundary safety, MTU bounds enforcement, and incremental RFC-compliant L3/L4 checksum recalculation.
globs: bpf/**/*.c, bpf/**/*.h, src/bpf/**/*.c, pkg/ebpf/**/*.go, src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-ebpf-xdp-w3c-trace-propagation-standard"
---

# In-Kernel W3C TraceContext Propagation via eBPF XDP with Zero-Copy Header Mutation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Bouncing Packets to Userspace Proxies or Mutating Headers Without In-Kernel Checksum Recalculation

## System Constraints
You are a Principal Linux Kernel and Observability Systems Architect enforcing in-kernel W3C TraceContext propagation through eBPF XDP. All network telemetry instrumentation MUST occur inside the kernel packet path without userspace serialization overhead. Enforce strict eBPF verifier safety, memory boundary guards, and zero-copy packet mutation.

## Strict Guidelines
- Engineers MUST ALWAYS validate packet boundaries against ctx->data_end prior to accessing or mutating any L2, L3, L4, or application payload offsets.
- Engineers MUST ALWAYS perform incremental L3 (IPv4) and L4 (TCP/UDP) checksum updates using bpf_csum_diff or folded 16-bit complement arithmetic when modifying header sizes or inserting W3C trace options.
- Engineers MUST NOT drop packets (XDP_DROP) upon trace buffer space exhaustion or packet parse failures; fallback MUST default to XDP_PASS to preserve line-rate availability.
- Code MUST strictly adhere to the W3C TraceContext binary wire format (version: 1 byte, trace-id: 16 bytes, parent-id: 8 bytes, trace-flags: 1 byte) inside in-kernel maps and custom IP/TCP options.
- Services MUST manage eBPF map sizing (BPF_MAP_TYPE_LRU_HASH or BPF_MAP_TYPE_PERCPU_ARRAY) explicitly to avoid unpinned dynamic allocations in the XDP fast path.
- Components NEVER bounce unencrypted raw packets to userspace sidecars merely to append or propagate OpenTelemetry trace metadata.

## Bad vs Good Implementations
### Unsafe Packet Ingress Dereference and Blind Mutation vs Verifier-Safe XDP Trace Injection

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Fails eBPF verifier checks due to lack of bounds validation against `ctx->data_end`. Mutates packet memory without adjusting buffer boundaries or recomputing IP/TCP checksums, causing corrupt frames and dropped traffic at NIC/stack level.
```c
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <bpf/bpf_helpers.h>

SEC("xdp")
int parse_trace_bad(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    // ANTI-PATTERN: No bounds verification against data_end before pointer dereference
    struct ethhdr *eth = data;
    struct iphdr *ip = data + sizeof(*eth);
    struct tcphdr *tcp = (void *)ip + (ip->ihl * 4);

    // ANTI-PATTERN: Unsafe direct write beyond payload bounds without bpf_xdp_adjust_head
    // and completely ignores IP/TCP checksum recalculation, corrupting line-rate traffic
    char *payload = (void *)tcp + (tcp->doff * 4);
    payload[0] = '0'; // Attempting to blindly prepend W3C traceparent
    payload[1] = '0';
    
    return XDP_PASS;
}
```

#### Production Standard (Correct)
> **Standard**: Validates all protocol layers against `data_end` before dereferencing, satisfying the BPF verifier. Extracts W3C TraceContext metadata in-kernel without unaligned access, memory corruption, or userspace context switches.
```c
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/in.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

struct w3c_trace_hdr {
    __u8  version;
    __u8  trace_id[16];
    __u8  parent_id[8];
    __u8  flags;
} __attribute__((packed));

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

    // 1. Boundary check L2 Ethernet
    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_PASS;

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

    // 2. Boundary check L3 IPv4
    struct iphdr *ip = (void *)(eth + 1);
    if ((void *)(ip + 1) > data_end)
        return XDP_PASS;

    if (ip->protocol != IPPROTO_TCP)
        return XDP_PASS;

    // 3. Boundary check L4 TCP
    __u32 ip_hdr_len = ip->ihl * 4;
    if (ip_hdr_len < sizeof(struct iphdr))
        return XDP_PASS;

    struct tcphdr *tcp = (void *)ip + ip_hdr_len;
    if ((void *)(tcp + 1) > data_end)
        return XDP_PASS;

    // 4. Verify payload offset holds W3C header safely without overflow
    __u32 tcp_hdr_len = tcp->doff * 4;
    if (tcp_hdr_len < sizeof(struct tcphdr))
        return XDP_PASS;

    void *payload = (void *)tcp + tcp_hdr_len;
    if (payload + sizeof(struct w3c_trace_hdr) > data_end)
        return XDP_PASS;

    struct w3c_trace_hdr *trace = (struct w3c_trace_hdr *)payload;
    if (trace->version == 0x00) {
        // In-kernel processing verified: emit perf event or update per-CPU telemetry map
        bpf_tail_call(ctx, &trace_dispatch_map, 0);
    }

    return XDP_PASS;
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: In-Kernel W3C TraceContext Propagation via eBPF XDP with Zero-Copy Header Mutation

In-Kernel W3C TraceContext Propagation via eBPF XDP with Zero-Copy Header Mutation defines an industrial-grade behavioral standard for devops engineering. Mandates in-kernel parsing, extraction, and injection of W3C TraceContext headers directly within the eBPF XDP driver layer. Eliminates userspace context switching, socket bouncing, and TLS de-serialization latency for ingress/egress network flows. Enforces verifier-compliant boundary safety, MTU bounds enforcement, and incremental RFC-compliant L3/L4 checksum recalculation. 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: Bouncing Packets to Userspace Proxies or Mutating Headers Without In-Kernel Checksum Recalculation 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 DriftBouncing Packets to Userspace Proxies or Mutating Headers Without In-Kernel Checksum RecalculationCritical
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 validate packet boundaries against ctx->data_end prior to accessing or mutating any L2, L3, L4, or application payload offsets.

Engineers MUST ALWAYS validate packet boundaries against ctx->data_end prior to accessing or mutating any L2, L3, L4, or application payload offsets.

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 ALWAYS perform incremental L3 (IPv4) and L4 (TCP/UDP) checksum updates using bpf_csum_diff or folded 16-bit complement arithmetic when modifying header sizes or inserting W3C trace options.

Engineers MUST ALWAYS perform incremental L3 (IPv4) and L4 (TCP/UDP) checksum updates using bpf_csum_diff or folded 16-bit complement arithmetic when modifying header sizes or inserting W3C trace options.

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 NOT drop packets (XDP_DROP) upon trace buffer space exhaustion or packet parse failures

Engineers MUST NOT drop packets (XDP_DROP) upon trace buffer space exhaustion or packet parse failures; fallback MUST default to XDP_PASS to preserve line-rate availability.

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

Code MUST strictly adhere to the W3C TraceContext binary wire format (version

Code MUST strictly adhere to the W3C TraceContext binary wire format (version: 1 byte, trace-id: 16 bytes, parent-id: 8 bytes, trace-flags: 1 byte) inside in-kernel maps and custom IP/TCP options.

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

Services MUST manage eBPF map sizing (BPF_MAP_TYPE_LRU_HASH or BPF_MAP_TYPE_PERCPU_ARRAY) explicitly to avoid unpinned dynamic allocations in the XDP fast path.

Services MUST manage eBPF map sizing (BPF_MAP_TYPE_LRU_HASH or BPF_MAP_TYPE_PERCPU_ARRAY) explicitly to avoid unpinned dynamic allocations in the XDP fast path.

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.
6

Components NEVER bounce unencrypted raw packets to userspace sidecars merely to append or propagate OpenTelemetry trace metadata.

Components NEVER bounce unencrypted raw packets to userspace sidecars merely to append or propagate OpenTelemetry trace metadata.

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 In-Kernel W3C TraceContext Propagation via eBPF XDP with Zero-Copy Header Mutation critical for AI coding assistants?

Mandates in-kernel parsing, extraction, and injection of W3C TraceContext headers directly within the eBPF XDP driver layer. Eliminates userspace context switching, socket bouncing, and TLS de-serialization latency for ingress/egress network flows. Enforces verifier-compliant boundary safety, MTU bounds enforcement, and incremental RFC-compliant L3/L4 checksum recalculation. 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 (Bouncing Packets to Userspace Proxies or Mutating Headers Without In-Kernel Checksum Recalculation) 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

In-Kernel