Skip to main content

P4Net

CI PyPI Python Versions License Documentation Status

P4Net is a Python framework for BMv2 and P4Runtime experiments on Linux.

Status: 1.12.0, stable. Public APIs follow the API stability rules. See the changelog for new features, fixes and upgrade notes.

Why use P4Net

Run a P4 experiment with one command. Starting with Mininet alone, you need a switch adapter to map Linux interfaces to BMv2 ports and launch the switches. You also need control-plane code to connect to each switch and install its pipeline.

In P4Net, define the topology in Python and point each switch at its P4 source. Once p4c and BMv2 are installed, sudo p4net topology.py compiles the programs, creates the links, starts the switches, and uses the built-in P4Runtime client to install each pipeline before opening the shell.

  • Use your own P4Runtime controller. Start BMv2 with --external-controller, then install pipelines and program the switches from your controller. See Using an external controller.
  • 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 AsyncP4RuntimeClient to 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.

Requirements

  • Linux kernel >= 5.4.
  • Python 3.10+.
  • BMv2's simple_switch_grpc and the Linux network tools on PATH. Install p4c to compile P4 programs locally.
  • Root privileges to run a network, including creating network namespaces and configuring links.

Installation

Follow the installation guide to set up p4c, BMv2 and the p4net CLI. For an editable checkout and development tools, see Contributing.

Quick Start

Run the CLI example

After installing the CLI globally with pipx, 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.py

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

Use the Python API

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

The script prints {('h1', 'h2'): True, ('h2', 'h1'): True} and stops the network.

Examples

Documentation

English documentation | 中文文档

Both editions cover installation, tutorials, the CLI, and runnable examples.

The source for these pages lives under docs/.

License

Apache-2.0.

Metadata

Release files for p4net 1.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for p4net 1.12.0
File Size Uploaded Provenance Actions
p4net-1.12.0.tar.gz 595.9 kB With provenance Details for p4net-1.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for p4net 1.12.0
File Interpreter ABI Platform Actions
p4net-1.12.0-py3-none-any.whl Python 3 none any Details for p4net-1.12.0-py3-none-any.whl

Total release size: 719.8 kB

Release files / p4net-1.12.0.tar.gz

Download URL p4net-1.12.0.tar.gz
Size 595.9 kB
Tags Source
SHA-256 checksum
How to use checksums
817393ef912b29ec19c3ccd23251be64230067ac8b3f7b6fda7b35fd16f60690
BLAKE2b-256 checksum
How to use checksums
f2aeefbf2674d15eef8d9aab61d1102319b38f60d8d7ce300fedd90e5d70a2e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Release files / p4net-1.12.0-py3-none-any.whl

Download URL p4net-1.12.0-py3-none-any.whl
Size 123.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
928f9c6173656f1104d933cc295c3b878064ffe7387827a8a8f5f60044c8cb5b
BLAKE2b-256 checksum
How to use checksums
65a8fb42fff78c6cad2546dfd144a3cadc0db8e63e60c73d9358e00ee13fc3aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Security for p4net 1.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for p4net-1.12.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page