Examples¶
p4net ships eight runnable example topologies under examples/ in the
repository. Each one isolates a single capability so the source is
easy to read and copy. Run any of them with:
(or sudo python examples/<name>/topology.py if you don't have the
console script on PATH).
What's bundled¶
- Quick start (port swap) — two hosts on a single switch with a hardcoded port-swap pipeline. The smallest working topology; no runtime table programming needed.
- L3 forwarding — same two hosts, but the
pipeline routes via an
ipv4_lpmtable that's programmed from Python at startup. ARP is pre-seeded. - CPU punt — a one-host topology where every
dataplane packet is punted to the controller via the CPU port.
Demonstrates
<switch> packet sendand<switch> packet listen. - Dual stack — two hosts each with both IPv4 and
IPv6 addresses on the same
/24and/64. Shows the per-interface IPv6 sysctl gating. - Asymmetric link — two hosts with
per-direction link delay. Demonstrates
delay_a_to_b/delay_b_to_aand the resulting one-way RTT asymmetry. - IPv6 LPM — two IPv6-only hosts with a 128-bit
LPM table programmed at runtime. Shows how
<switch> table dumprenders IPv6 entries in human form (fd00::1/128) instead of raw bytes. - INT (in-band telemetry) — single-switch INT: the pipeline inserts a 14-byte shim header carrying switch identity, ingress timestamp, egress port, queue depth, and the original etherType. A raw-socket listener decodes the shim on the receiver.
- Multi-hop INT — two switches in series each insert their own shim; the listener decodes the full hop-by-hop stack. Production-style telemetry topology; same P4 binary on both switches, identity supplied by the v1.2 register API.
How they fit together¶
| Example | Hosts | Switches | IPv4 | IPv6 | Asymmetric | CPU punt | Runtime tables | Telemetry |
|---|---|---|---|---|---|---|---|---|
| quick_start | 2 | 1 | ✓ | |||||
| l3_forwarding | 2 | 1 | ✓ | ✓ | ||||
| cpu_punt | 1 | 1 | ✓ | ✓ | ||||
| dual_stack | 2 | 1 | ✓ | ✓ | ||||
| asymmetric_link | 2 | 1 | ✓ | ✓ | ||||
| ipv6_lpm | 2 | 1 | ✓ | ✓ | ||||
| int | 2 | 1 | ✓ | ✓ | ✓ | |||
| int_multi_hop | 2 | 2 | ✓ | ✓ | ✓ (×2) |
The Tutorial combines every feature in one 4-host, 2-switch program — useful as a kitchen-sink example, but each individual feature is easier to understand in isolation here.