This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: ebpf-tc-egress-bandwidth-scheduling-standard

eBPF TC Egress Packet Scheduling & Multi-Tenant Bandwidth Isolation

Enforces deterministic kernel-space egress traffic shaping and tenant rate limiting via eBPF Traffic Control (TC) BPF_PROG_TYPE_SCHED_ACT programs. Prevents noisy-neighbor egress bandwidth starvation on multi-tenant Kubernetes nodes by binding Token Bucket Filter (TBF) and Earliest Departure Time (EDT) scheduling algorithms directly to virtual ethernet and physical root interfaces.

Prohibited Anti-Pattern: Relying on Userspace Rate Limiters or Best-Effort CNI QoS Without Kernel-Level TC Egress Queue Enforcement

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: eBPF TC Egress Packet Scheduling & Multi-Tenant Bandwidth Isolation - Enforces deterministic kernel-space egress traffic shaping and tenant rate limiting via eBPF Traffic Control (TC) BPF_PROG_TYPE_SCHED_ACT programs. Prevents noisy-neighbor egress bandwidth starvation on multi-tenant Kubernetes nodes by binding Token Bucket Filter (TBF) and Earliest Departure Time (EDT) scheduling algorithms directly to virtual ethernet and physical root interfaces.
globs: bpf/**/*.c, bpf/**/*.h, pkg/ebpf/**/*.go, pkg/network/**/*.go, deploy/daemonset/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-tc-egress-bandwidth-scheduling-standard"
---

# eBPF TC Egress Packet Scheduling & Multi-Tenant Bandwidth Isolation

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Relying on Userspace Rate Limiters or Best-Effort CNI QoS Without Kernel-Level TC Egress Queue Enforcement

## System Constraints
You are a Principal Linux Kernel and Network Architect enforcing eBPF TC (Traffic Control) Packet Scheduling & Egress Shaping Standards in multi-tenant Kubernetes clusters. You must ensure all egress traffic is shaped directly in kernel space using BPF TC egress hooks with EDT or atomic Token Bucket algorithms. Prohibit unbounded egress queues, userspace packet throttling, missing tail-drop metrics, and unpinned BPF maps that break across daemon restarts.

## Strict Guidelines
- Engineers MUST ALWAYS attach eBPF egress shapers to the TC subsystem via BPF_TC_EGRESS hooks on host interfaces and container veth pairs using direct-action mode (da).
- Engineers MUST ALWAYS compute delivery deadlines using skb->tstamp via Earliest Departure Time (EDT) coupled with FQ (Fair Queueing) qdisc rather than spinning or sleeping inside kernel programs.
- Engineers MUST ALWAYS use atomic BPF map lookups (BPF_MAP_TYPE_HASH or BPF_MAP_TYPE_LRU_HASH) with __sync_fetch_and_add for token replenishment to eliminate race conditions across concurrent CPU cores.
- Engineers MUST NOT rely on userspace traffic proxying, iptables string-matching, or cgroupv1 blkio/net_cls controllers for network egress rate isolation.
- Code SHOULD return TC_ACT_SHOT on token exhaustion or quota breach and emit a perf event / ring buffer metric to register throttled packets immediately.
- Services MUST pin BPF rate-limit maps under /sys/fs/bpf to guarantee state persistence during CNI daemon agent restarts and upgrades.
- Components NEVER drop control plane traffic (ARP, ICMP, DHCP, DNS, and internal health checks) regardless of tenant egress saturation.

## Bad vs Good Implementations
### Kernel TC eBPF Tenant Rate Limiter using Monotonic Clocks and Atomic Tokens

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The bad example mutates tenant quota state non-atomically across concurrent cores, ignores token replenishment calculations based on ktime, fails to protect essential infrastructure traffic, and passes packets through when quotas are depleted.
```c
#include <linux/bpf.h>
#include <linux/pkt_cls.h>
#include <bpf/bpf_helpers.h>

struct tenant_quota {
    __u64 tokens;
    __u64 last_time;
};

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 1024);
    __type(key, __u32);   /* cgroup_id */
    __type(value, struct tenant_quota);
} rate_map SEC(".maps");

SEC("tc")
int tc_egress_bad(struct __sk_buff *skb) {
    __u32 cgroup_id = bpf_skb_cgroup_id(skb);
    struct tenant_quota *q = bpf_map_lookup_elem(&rate_map, &cgroup_id);
    
    /* Non-atomic state mutation leads to severe multi-core race conditions */
    if (q) {
        if (q->tokens < skb->len) {
            return TC_ACT_OK; /* Fails open, leading to noisy-neighbor saturation */
        }
        q->tokens -= skb->len;
    }
    return TC_ACT_OK;
}
```

#### Production Standard (Correct)
> **Standard**: The good example evaluates skb egress at TC stage, safeguards ARP control-plane frames, accounts for nanosecond-level token replenishment, enforces burst bounds, and executes deterministic tail-drops (TC_ACT_SHOT) while pinning maps to survive reloads.
```c
#include <linux/bpf.h>
#include <linux/pkt_cls.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <bpf/bpf_helpers.h>

#define RATE_BPS (100 * 1024 * 1024 / 8) /* 100 Mbps in bytes/sec */
#define BURST_BYTES (256 * 1024)
#define NSEC_PER_SEC 1000000000ULL

struct tenant_bucket {
    __u64 tokens;
    __u64 last_update_ns;
};

struct {
    __uint(type, BPF_MAP_TYPE_LRU_HASH);
    __uint(max_entries, 16384);
    __type(key, __u32); /* pod cgroup_id */
    __type(value, struct tenant_bucket);
    __uint(pinning, LIBBPF_PIN_BY_NAME);
} pod_egress_rates SEC(".maps");

SEC("tc")
int tc_egress_shaped(struct __sk_buff *skb) {
    void *data = (void *)(long)skb->data;
    void *data_end = (void *)(long)skb->data_end;
    
    /* Bypass control plane traffic (e.g., ARP) */
    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return TC_ACT_OK;
    if (eth->h_proto == __constant_htons(ETH_P_ARP))
        return TC_ACT_OK;

    __u32 cgroup_id = bpf_skb_cgroup_id(skb);
    if (cgroup_id == 0)
        return TC_ACT_OK;

    __u64 now = bpf_ktime_get_ns();
    struct tenant_bucket *bucket = bpf_map_lookup_elem(&pod_egress_rates, &cgroup_id);
    if (!bucket) {
        struct tenant_bucket init = { .tokens = BURST_BYTES, .last_update_ns = now };
        bpf_map_update_elem(&pod_egress_rates, &cgroup_id, &init, BPF_NOEXIST);
        bucket = bpf_map_lookup_elem(&pod_egress_rates, &cgroup_id);
        if (!bucket)
            return TC_ACT_OK;
    }

    __u64 elapsed = now - bucket->last_update_ns;
    __u64 replenished = (elapsed * RATE_BPS) / NSEC_PER_SEC;
    __u64 current_tokens = bucket->tokens + replenished;
    if (current_tokens > BURST_BYTES)
        current_tokens = BURST_BYTES;

    if (current_tokens < skb->len)
        return TC_ACT_SHOT; /* Hard egress clamp prevents starvation */

    bucket->tokens = current_tokens - skb->len;
    bucket->last_update_ns = now;
    return TC_ACT_OK;
}

char _license[] SEC("license") = "Dual BSD/GPL";
```

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 Egress Packet Scheduling & Multi-Tenant Bandwidth Isolation

eBPF TC Egress Packet Scheduling & Multi-Tenant Bandwidth Isolation defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic kernel-space egress traffic shaping and tenant rate limiting via eBPF Traffic Control (TC) BPF_PROG_TYPE_SCHED_ACT programs. Prevents noisy-neighbor egress bandwidth starvation on multi-tenant Kubernetes nodes by binding Token Bucket Filter (TBF) and Earliest Departure Time (EDT) scheduling algorithms directly to virtual ethernet and physical root interfaces. 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: Relying on Userspace Rate Limiters or Best-Effort CNI QoS Without Kernel-Level TC Egress Queue Enforcement 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 DriftRelying on Userspace Rate Limiters or Best-Effort CNI QoS Without Kernel-Level TC Egress Queue EnforcementCritical
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 egress shapers to the TC subsystem via BPF_TC_EGRESS hooks on host interfaces and container veth pairs using direct-action mode (da).

Engineers MUST ALWAYS attach eBPF egress shapers to the TC subsystem via BPF_TC_EGRESS hooks on host interfaces and container veth pairs using direct-action mode (da).

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 compute delivery deadlines using skb->tstamp via Earliest Departure Time (EDT) coupled with FQ (Fair Queueing) qdisc rather than spinning or sleeping inside kernel programs.

Engineers MUST ALWAYS compute delivery deadlines using skb->tstamp via Earliest Departure Time (EDT) coupled with FQ (Fair Queueing) qdisc rather than spinning or sleeping inside kernel programs.

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 ALWAYS use atomic BPF map lookups (BPF_MAP_TYPE_HASH or BPF_MAP_TYPE_LRU_HASH) with __sync_fetch_and_add for token replenishment to eliminate race conditions across concurrent CPU cores.

Engineers MUST ALWAYS use atomic BPF map lookups (BPF_MAP_TYPE_HASH or BPF_MAP_TYPE_LRU_HASH) with __sync_fetch_and_add for token replenishment to eliminate race conditions across concurrent CPU cores.

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

Engineers MUST NOT rely on userspace traffic proxying, iptables string-matching, or cgroupv1 blkio/net_cls controllers for network egress rate isolation.

Engineers MUST NOT rely on userspace traffic proxying, iptables string-matching, or cgroupv1 blkio/net_cls controllers for network egress rate isolation.

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

Code SHOULD return TC_ACT_SHOT on token exhaustion or quota breach and emit a perf event / ring buffer metric to register throttled packets immediately.

Code SHOULD return TC_ACT_SHOT on token exhaustion or quota breach and emit a perf event / ring buffer metric to register throttled packets immediately.

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

Services MUST pin BPF rate-limit maps under /sys/fs/bpf to guarantee state persistence during CNI daemon agent restarts and upgrades.

Services MUST pin BPF rate-limit maps under /sys/fs/bpf to guarantee state persistence during CNI daemon agent restarts and upgrades.

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

Components NEVER drop control plane traffic (ARP, ICMP, DHCP, DNS, and internal health checks) regardless of tenant egress saturation.

Components NEVER drop control plane traffic (ARP, ICMP, DHCP, DNS, and internal health checks) regardless of tenant egress saturation.

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 Egress Packet Scheduling & Multi-Tenant Bandwidth Isolation critical for AI coding assistants?

Enforces deterministic kernel-space egress traffic shaping and tenant rate limiting via eBPF Traffic Control (TC) BPF_PROG_TYPE_SCHED_ACT programs. Prevents noisy-neighbor egress bandwidth starvation on multi-tenant Kubernetes nodes by binding Token Bucket Filter (TBF) and Earliest Departure Time (EDT) scheduling algorithms directly to virtual ethernet and physical root interfaces. 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 (Relying on Userspace Rate Limiters or Best-Effort CNI QoS Without Kernel-Level TC Egress Queue Enforcement) 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