Skip to content
LinkState
Go back

Putting AI-generated tc plans behind real rollback gates

Designing a Safe Workflow for AI‑Suggested tc Changes

Introduction

Traffic Control (tc) shapes, schedules, and polices network traffic on Linux hosts. In shared laboratory environments operators frequently adjust tc qdiscs, classes, and filters to emulate WAN characteristics, test congestion‑control algorithms, or isolate workloads. Manually crafting these rules is error‑prone; a mis‑specified netem delay or an overly aggressive htb rate can impair unrelated experiments.

Large language models (LLMs) can now translate a high‑level intent—e.g., “add 50 ms jitter between containers A and B on eth0”—into a suggested set of tc commands. While this accelerates experimentation, the AI has no visibility into the current host state, lab policies, or side‑effects on other tenants. Blindly applying autogenerated changes risks cascading impairment, so a disciplined safety workflow is essential.

A safe workflow enforces five principles before any tc modification touches production‑like interfaces:

  1. Pre‑deployment validation – verify syntactic and semantic correctness without affecting live traffic.
  2. Isolated testing – execute the changes in a sandbox that mirrors the target topology but is insulated from other users.
  3. Progressive exposure – roll out the change to a small fraction of interfaces or hosts (canary) while measuring key performance indicators (KPIs).
  4. Automated rollback – revert the change instantly if any KPI crosses a predefined safety threshold.
  5. Audit and approval – retain a tamper‑evident record of the AI proposal, the dry‑run diff, canary metrics, and the final decision for compliance reviews.

The sections below show how each principle can be realized with existing Linux and Kubernetes tooling. Illustrative examples are marked as such; they are not guarantees of production‑ready functionality.


Dry‑Run Diffs for Pre‑Deployment Testing

Generating Dry‑Run Diffs

Because tc lacks a native --dry‑run flag, we emulate it by cloning the current tc state into a temporary network namespace, applying the AI suggestion there, and diffing the before/after states.

Illustrative helper script (ai_tc_dryrun.sh):

#!/usr/bin/env bash
set -euo pipefail

IFACE=$1                     # e.g., veth1
AI_TC_SCRIPT=$2              # file containing AI‑generated tc commands

# Create a temporary netns
NS="tc_dryrun_$$"
ip netns add $NS

# Provide the namespace with an interface to work on
ip link add veth0 type veth peer name veth1
ip link set veth1 netns $NS
ip link set veth0 up
ip netns exec $NS ip link set veth1 up
ip netns exec $NS ip addr add 169.254.0.1/30 dev veth1

# Capture baseline
BASE=$(mktemp)
ip netns exec $NS tc -s qdisc show dev veth1 > "$BASE"
ip netns exec $NS tc -s filter show dev veth1 >> "$BASE"

# Apply AI suggestion inside the ns
ip netns exec $NS bash -c "$(cat $AI_TC_SCRIPT)"

# Capture post‑apply state
AFTER=$(mktemp)
ip netns exec $NS tc -s qdisc show dev veth1 > "$AFTER"
ip netns exec $NS tc -s filter show dev veth1 >> "$AFTER"

# Produce diff (exit code 1 if differences exist; we still want to see them)
diff -u "$BASE" "$AFTER" || true

# Cleanup
ip netns delete $NS
rm -f "$BASE" "$AFTER"

Note: This script assumes the AI script only manipulates the specified interface and does not rely on global tc settings (e.g., root qdiscs). Adjustments may be needed for complex hierarchies.

Analyzing Dry‑Run Diffs for Potential Issues

The diff highlights three risk classes:

Diff PatternPotential IssueMitigation
New qdisc type (e.g., netem where none existed)Introduces latency/jitter that may break latency‑sensitive experiments.Verify the new qdisc matches the intent and stays within lab‑wide policy limits (e.g., max delay ≤ 200 ms).
Modified class rate (e.g., htb rate changed from 100 Mbit to 10 Mbit)Could starve existing flows sharing the same parent class.Cross‑check the rate against the aggregate bandwidth allocation recorded in the lab’s inventory system.
Added filter with mismatched protocol (e.g., u32 filter matching IPv6 on an IPv4‑only interface)May cause packets to be dropped silently, leading to false‑negative test results.Validate the filter’s protocol field against the interface’s configured address families.

If any diff line triggers a rule in the mitigation column, halt the workflow and prompt the operator for review before proceeding to namespace probes.


Namespace Probes for Isolated Testing

Creating Namespace Probes

Once the dry‑run diff passes visual or automated policy checks, we move to an isolated test that can inject real traffic and observe the behavior of the AI‑suggested tc rules. A namespace probe consists of:

Illustrative topology creation (setup_probe.sh):

#!/usr/bin/env bash
set -euo pipefail

# Create namespaces
ip netns add client-ns
ip netns add mid-ns
ip netns add server-ns

# veth pairs
ip link add veth-c-m type veth peer name veth-m-c
ip link add veth-m-s type veth peer name veth-s-m

# Attach ends
ip link set veth-c-m netns client-ns
ip link set veth-m-c netns mid-ns
ip link set veth-m-s netns mid-ns
ip link set veth-s-m netns server-ns

# Assign IPs
ip netns exec client-ns ip addr add 10.0.0.1/24 dev veth-c-m
ip netns exec mid-ns ip addr add 10.0.0.2/24 dev veth-m-c
ip netns exec mid-ns ip addr add 10.0.1.1/24 dev veth-m-s
ip netns exec server-ns ip addr add 10.0.1.2/24 dev veth-s-m

# Bring up interfaces
for ns in client-ns mid-ns server-ns; do
  ip netns exec $ns ip link set lo up
  ip netns exec $ns ip link set veth-*-* up
done

Run setup_probe.sh once; it leaves the three namespaces and veth links ready for testing.

Executing Namespace Probes for Validation

Validation proceeds in three phases: baseline measurement, apply AI tc, post‑apply measurement.

Illustrative validation loop (run_probe.sh):

#!/usr/bin/env bash
set -euo pipefail

# 1. Baseline measurement (TCP iperf3)
ip netns exec server-ns iperf3 -s -1 &
SERVER_PID=$!
sleep 1   # give server time to start
ip netns exec client-ns iperf3 -c 10.0.1.2 -t 10 -J > baseline_iperf.json
kill $SERVER_PID

# 2. Apply AI tc script inside the intermediate namespace
ip netns exec mid-ns bash -c "$(cat ai_tc_script.sh)"

# 3. Post‑apply measurement
ip netns exec server-ns iperf3 -s -1 &
SERVER_PID=$!
sleep 1
ip netns exec client-ns iperf3 -c 10.0.1.2 -t 10 -J > after_iperf.json
kill $SERVER_PID

# Optional: latency/jitter via ping
ip netns exec server-ns ping -i 0.2 10.0.1.1 -c 100 > /dev/null &
PING_PID=$!
sleep 2
ip netns exec client-ns ping -c 100 10.0.1.2 > ping_raw.txt
kill $PING_PID

# Process JSON with jq (example)
# throughput_delta=$(jq '.end.sum_received.bits_per_second' after_iperf.json)
# baseline_throughput=$(jq '.end.sum_received.bits_per_second' baseline_iperf.json)
# echo "Throughput delta: $((throughput_delta - baseline_throughput)) bps"

The JSON outputs are processed (e.g., with jq) to compute:

If any metric exceeds a pre‑defined safety threshold, the workflow aborts and prompts the operator for review before proceeding to a canary rollout.


Canary Deployment and Automated Rollback

After successful namespace probing, the change is rolled out to a small fraction of production interfaces (the canary).

  1. Select canary targets – e.g., a subset of hosts or a single interface per host, identified via labels or an inventory database.
  2. Apply the AI‑generated tc commands to those targets.
  3. Monitor KPIs (latency, jitter, packet loss, throughput) in real time using a monitoring stack (Prometheus + Grafana, or a simple sidecar script).
  4. Define rollback thresholds – e.g., latency increase > 20 % or throughput drop > 15 % relative to baseline.
  5. Automated rollback – if any threshold is breached, an orchestrator (a Kubernetes Operator, a cron‑based watchdog, or a custom script) immediately reapplies the previous tc state (saved during the dry‑run) and alerts the operator.

Illustrative canary controller snippet (canary_watchdog.sh):

#!/usr/bin/env bash
set -euo pipefail

CANARY_IFACE=$1          # interface to monitor
BASELINE_FILE=$2         # output of `tc -s qdisc show dev $CANARY_IFACE` before change
THRESHOLD_LATENCY_MS=20  # example: 20 ms increase triggers rollback

# Apply AI tc (assumed already done elsewhere)
# ...

# Monitoring loop
while true; do
  CURRENT_LATENCY=$(tc -s qdisc show dev $CANARY_IFACE | awk '/delay/ {print $2}' | tr -d 'ms')
  BASELINE_LATENCY=$(awk '/delay/ {print $2}' $BASELINE_FILE | tr -d 'ms')
  DELTA=$((CURRENT_LATENCY - BASELINE_LATENCY))

  if (( DELTA > THRESHOLD_LATENCY_MS )); then
    echo "Latency spike detected (+${DELTA}ms). Rolling back..."
    # Re‑apply baseline tc state
    tc qdisc replace dev $CANARY_IFACE root handle 1: prio
    # (re‑apply filters/classes from baseline as needed)
    break
  fi
  sleep 5
done

The watchdog runs as a sidecar process or a Kubernetes Job, ensuring rapid reaction without manual intervention.


Audit and Approval

For compliance and reproducibility, retain a tamper‑evident record of every step:

Immutable storage (e.g., WORM‑enabled S3 buckets, append‑only logs, or a blockchain‑based ledger) guarantees that the record cannot be altered after the fact, facilitating audits and post‑mortem analysis.


By combining dry‑run diffs, isolated namespace probes, canary exposure with automated rollback, and immutable auditing, laboratory operators can safely harness AI‑suggested tc changes without risking unintended impairment of shared resources.


Share this post on:

Previous Post
Why Identical Topologies Fail Only in CI
Next Post
The First Ping Fails Then Everything Looks Fine