P4Net is a Python framework for BMv2 and P4Runtime experiments on Linux.
Status: 1.11.0, stable. Public APIs follow the API stability rules. See the changelog for new features, fixes and upgrade notes.
- Start BMv2 with P4Runtime in one command. P4Net compiles the P4 programs, builds the Linux topology, starts BMv2, connects the built-in P4Runtime client, and installs the pipelines.
- Reuse one topology for debugging and automated tests. Define hosts, links, and switches in Python. Put initial table entries in
setup(net)and reuse that function with the same topology in a Python test. - Debug hosts and switches in one shell. Check connectivity, run host commands, and use P4Runtime to inspect or program tables, read counters, and handle PacketIn and PacketOut. See the L3 forwarding example.
- Configure several switches concurrently. Use
AsyncP4RuntimeClientto send P4Runtime requests from one Python event loop. The async example installs forwarding entries on a three-switch mesh. - Repeat experiments under defined link conditions. Configure IPv4 and IPv6 hosts, set bandwidth, delay, jitter, and loss per direction, and capture traffic on switch ports.
- Linux kernel >= 5.4.
- Python 3.10+.
- BMv2 and p4c installed system-wide.
- Root privileges to run a network, including creating network namespaces and configuring links.
Follow the installation guide to set up p4c, BMv2 and the p4net CLI. For an editable checkout and development tools, see Contributing.
After installing the CLI, clone the repository and start the two-host example.
git clone https://github.com/zhh2001/p4net.git
cd p4net
sudo p4net examples/quick_start/quick_start.pyAt the p4net> prompt, run pingall. It reports 2/2 succeeded. Enter exit to stop the network.
The CLI loads the module-level topology and calls setup(net) if the file defines it. The example README also shows how to run the bundled example directly with Python.
Save this as demo.py in the repository root. The bundled P4 program forwards frames between switch ports 1 and 2.
from pathlib import Path
from p4net import Network
from p4net.topo import Topology
p4_src = Path(__file__).resolve().parent / "examples" / "quick_start" / "quick_start.p4"
topo = Topology()
h1 = topo.add_host("h1", ip="10.0.0.1/24", mac="00:00:00:00:00:01")
h2 = topo.add_host("h2", ip="10.0.0.2/24", mac="00:00:00:00:00:02")
s1 = topo.add_switch("s1", p4_src=p4_src)
topo.add_link(h1, s1, port_b=1)
topo.add_link(h2, s1, port_b=2)
with Network(topo) as net:
print(net.pingall())From the repository root, install p4net into a virtual environment and run the script.
python3 -m venv .venv
.venv/bin/python -m pip install p4net
sudo .venv/bin/python demo.pyThe script prints {('h1', 'h2'): True, ('h2', 'h1'): True} and stops the network.
examples/quick_start/— minimal two-host network with a hardcoded port-swap pipeline.examples/l3_forwarding/— runtime-programmed IPv4 LPM with static ARP.examples/cpu_punt/— every dataplane packet punted to the controller; demonstrates<switch> packet send/<switch> packet listen.examples/dual_stack/— two hosts carrying both IPv4 and IPv6.examples/asymmetric_link/— per-directiondelayshaping.examples/ipv6_lpm/— runtime-programmed IPv6 LPM forwarding.examples/int/— single-switch in-band network telemetry: the pipeline inserts a 14-byte INT shim into every forwarded packet; a raw-socket listener decodes it on the receiver.examples/int_multi_hop/— two switches each add a telemetry header to forwarded IPv4 packets, and the listener decodes both.examples/async_concurrent/— three-switch mesh with IPv4 forwarding entries installed concurrently usingAsyncP4RuntimeClient.
Both editions cover installation, tutorials, the CLI, and runnable examples.
The source for these pages lives under docs/.
Apache-2.0.