Metadata-Version: 2.5
Name: p4net
Version: 1.12.0
Summary: A P4Runtime-native SDN simulation framework for BMv2.
Project-URL: Homepage, https://github.com/zhh2001/p4net
Project-URL: Repository, https://github.com/zhh2001/p4net
Project-URL: Issues, https://github.com/zhh2001/p4net/issues
Project-URL: Changelog, https://github.com/zhh2001/p4net/blob/main/CHANGELOG.md
Author: The p4net Authors
Maintainer-email: Henghua Zhang <1652709417@qq.com>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: System :: Networking
Requires-Python: >=3.10
Requires-Dist: grpcio>=1.60
Requires-Dist: p4runtime>=1.5
Requires-Dist: prompt-toolkit>=3.0
Requires-Dist: protobuf>=5.0
Requires-Dist: pyroute2>=0.7.12
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pre-commit>=3; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest-mock; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: docs
Requires-Dist: cairosvg>=2.7; extra == 'docs'
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
Requires-Dist: mkdocs-static-i18n>=1.2; extra == 'docs'
Requires-Dist: mkdocs>=1.6; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.26; (python_version < '3.11') and extra == 'docs'
Requires-Dist: mkdocstrings[python]>=1.0.7; (python_version >= '3.11') and extra == 'docs'
Requires-Dist: pillow>=10.0; extra == 'docs'
Requires-Dist: pymdown-extensions<12.2,>=10.7; (python_version < '3.11') and extra == 'docs'
Requires-Dist: pymdown-extensions>=10.7; (python_version >= '3.11') and extra == 'docs'
Description-Content-Type: text/markdown

# P4Net

[![CI](https://github.com/zhh2001/p4net/actions/workflows/ci.yml/badge.svg)](https://github.com/zhh2001/p4net/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/p4net.svg)](https://pypi.org/project/p4net/)
[![Python Versions](https://img.shields.io/pypi/pyversions/p4net.svg)](https://pypi.org/project/p4net/)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/zhh2001/p4net/blob/main/LICENSE)
[![Documentation](https://img.shields.io/badge/docs-zhh2001.github.io%2Fp4net-blue)](https://zhh2001.github.io/p4net/)
[![Status](https://img.shields.io/badge/status-stable-green.svg)](https://zhh2001.github.io/p4net/api-stability/)

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

Status: 1.12.0, stable. Public APIs follow the [API stability rules](https://zhh2001.github.io/p4net/api-stability/). See the [changelog](https://zhh2001.github.io/p4net/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](https://zhh2001.github.io/p4net/cli/#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](https://zhh2001.github.io/p4net/examples/l3-forwarding/).
- **Configure several switches concurrently.** Use `AsyncP4RuntimeClient` to send P4Runtime requests from one Python event loop. The [async example](https://zhh2001.github.io/p4net/examples/async-concurrent/) 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](https://zhh2001.github.io/p4net/getting-started/installation/#required-external-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](https://zhh2001.github.io/p4net/getting-started/installation/) to set up p4c, BMv2 and the p4net CLI. For an editable checkout and development tools, see [Contributing](https://zhh2001.github.io/p4net/dev/contributing/).

## Quick Start

### Run the CLI example

After installing the CLI globally with pipx, clone the repository and start the two-host example.

```sh
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](https://github.com/zhh2001/p4net/blob/main/examples/quick_start/README.md#as-a-self-contained-script) 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.

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

```sh
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

- [`examples/quick_start/`](https://github.com/zhh2001/p4net/tree/main/examples/quick_start) — minimal two-host network with a hardcoded port-swap pipeline.
- [`examples/l3_forwarding/`](https://github.com/zhh2001/p4net/tree/main/examples/l3_forwarding) — runtime-programmed IPv4 LPM with static ARP.
- [`examples/cpu_punt/`](https://github.com/zhh2001/p4net/tree/main/examples/cpu_punt) sends host packets to the controller and shows how to listen for PacketIn and send PacketOut from the CLI.
- [`examples/dual_stack/`](https://github.com/zhh2001/p4net/tree/main/examples/dual_stack) — two hosts carrying both IPv4 and IPv6.
- [`examples/asymmetric_link/`](https://github.com/zhh2001/p4net/tree/main/examples/asymmetric_link) — per-direction `delay` shaping.
- [`examples/ipv6_lpm/`](https://github.com/zhh2001/p4net/tree/main/examples/ipv6_lpm) — runtime-programmed IPv6 LPM forwarding.
- [`examples/int/`](https://github.com/zhh2001/p4net/tree/main/examples/int) adds a 14-byte in-band network telemetry (INT) header to forwarded IPv4 packets and decodes it at the receiving host.
- [`examples/int_multi_hop/`](https://github.com/zhh2001/p4net/tree/main/examples/int_multi_hop) — two switches each add a telemetry header to forwarded IPv4 packets, and the listener decodes both.
- [`examples/async_concurrent/`](https://github.com/zhh2001/p4net/tree/main/examples/async_concurrent) — three-switch mesh with IPv4 forwarding entries installed concurrently using `AsyncP4RuntimeClient`.

## Documentation

[English documentation](https://zhh2001.github.io/p4net/) | [中文文档](https://zhh2001.github.io/p4net/zh/)

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

The source for these pages lives under [`docs/`](https://github.com/zhh2001/p4net/tree/main/docs).

## License

Apache-2.0.
