| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
P4sim is a high-performance simulation framework that brings P4-programmable data plane processing into the ns-3 network simulator. It enables researchers and developers to model, execute, and evaluate P4 programs within realistic end-to-end network simulations, tightly coupling a P4-driven packet processing engine with ns-3's flexible network modeling for fine-grained analysis of programmable networks at scale.
P4sim is open-source software licensed under the Apache License 2.0.
This repository provides two P4 switch architectures in ns-3. Arch 1 (main branch) follows a bridge-based design, where each port corresponds to an external NetDevice (e.g., CSMA or P2P), offering flexibility but limited control over port-level behavior. Arch 2 (switch_channel branch) introduces a switch-centric design, in which ports are internal components of the P4SwitchNetDevice and interconnected via a switched Ethernet channel, enabling fine-grained control, improved modularity, and closer alignment with real-world programmable switch architectures.
The branch switch_channel now is ongoing development and may contain breaking changes. The main branch is stable and recommended for users who want to get started with P4sim immediately.
Our implementation builds upon the P4-driven Network Simulator Module described in:
The following steps set up a local environment to run P4sim with ns-3.39 on Ubuntu 24.04 LTS.
Note: The BMv2 and P4 software installation will take 1–2 hours and consume up to 15 GB of disk space.
Why ns-3.39 or earlier? Starting from ns-3.40, ns-3 requires C++20. However, BMv2 is currently built with C++17. P4sim therefore supports ns-3.39 and earlier versions. We plan to upgrade once a C++20-compatible BMv2 build becomes available.
sudo apt update
sudo apt install git vim cmake
mkdir ~/workdir && cd ~/workdirInstall all required libraries and tools via the official p4lang/tutorials repository:
cd ~
git clone https://github.com/p4lang/tutorials
mkdir ~/src && cd ~/src
../tutorials/vm-ubuntu-24.04/install.sh |& tee log.txtVerify the installation:
simple_switch --versioncd ~/workdir
git clone https://github.com/nsnam/ns-3-dev-git.git ns3.39
cd ns3.39 && git checkout ns-3.39Add the P4sim module:
cd contrib
git clone https://github.com/HapCommSys/p4sim.git
cd p4sim && sudo ./set_pkg_config_env.shConfigure and build:
cd ../..
./ns3 configure --enable-tests --enable-examples
./ns3 buildP4sim resolves P4 artifact paths (JSON pipelines, flow tables, topology files) via the P4SIM_DIR environment variable. Add it to your shell profile:
echo 'export P4SIM_DIR="$HOME/workdir/ns3.39/contrib/p4sim"' >> ~/.bashrc
source ~/.bashrcTip: If P4SIM_DIR is not set, P4sim falls back to a path derived from the executable location, but setting it explicitly is recommended for reliability.
./ns3 run p4-v1model-ipv4-forwardingNo manual path editing is required — all examples use portable path helpers. A full list of available example names can be found in examples/CMakeLists.txt.
See the full step-by-step guide (including VM setup) in doc/vm-env.md.
| Value | Architecture | BMv2 Target |
|---|---|---|
| 0 | V1model | simple_switch |
| 1 | PSA | psa_switch |
| 2 | PNA | pna_nic |
The forwarding behaviour is defined by the P4 program and its flow-table configuration. The following ns-3 attributes on ns3::P4SwitchNetDevice control simulation-level settings:
| Attribute | Description |
|---|---|
| JsonPath | Path to the compiled P4 JSON file |
| FlowTablePath | Path to the flow-table configuration file |
| P4SwitchArch | Architecture selector (0 = V1model, 1 = PSA, 2 = PNA) |
| ChannelType | Channel type (0 = CSMA, 1 = point-to-point) |
| SwitchRate | Processing rate in packets per second |
| QueueBufferSize | Total queue buffer size (packets) |
| InputBufferSizeLow | Input buffer size for low-priority (external) packets |
| InputBufferSizeHigh | Input buffer size for high-priority (internal) packets |
| EnableTracing | Enable basic throughput tracing |
| EnableSwap | Enable runtime swapping of the P4 configuration |
Notes:
- When using a CSMA channel, the P4 program must handle ARP explicitly.
- Buffer attributes only take effect if the selected architecture models that buffer.
- EnableTracing currently supports basic throughput measurement only.
Using P4sim typically involves the following steps:
See the full list and descriptions in doc/examples.md.
Selected examples:
| Script | Description |
|---|---|
| p4-v1model-ipv4-forwarding | 2-host, 1-switch IPv4 forwarding (V1model) |
| p4-psa-ipv4-forwarding | Same topology, PSA architecture |
| p4-pna-ipv4-forwarding | Same topology, PNA architecture |
| p4-basic-example | 4-host, 4-switch mesh (V1model) |
| p4-basic-tunnel | 3-host tunnel with custom header |
| p4-firewall | Stateful firewall |
| p4-l3-router | 3-router line topology, L3 forwarding |
| p4-link-monitoring | In-band link utilisation probes |
| p4-spine-leaf-topo | Spine-leaf with ECMP load balancing |
| p4-topo-fattree | Auto-generated fat-tree topology |
| p4-queue-test | QoS-aware queuing |
| p4-source-routing | Source routing with custom headers |
| p4-basic-controller | Runtime controller flow-table updates |
In the paper, P4sim is evaluated using representative networking scenarios demonstrating its capability to model basic tunneling (custom header encapsulation/decapsulation) and load balancing (distributing traffic across multiple network paths using P4 pipelines).
The packet processing rate SwitchRate (packets per second) must currently be configured manually for each switch. An inappropriate value can cause the switch to enter an idle polling loop, leading to wasted CPU cycles. Automatic rate tuning is planned for a future release.
sudo apt install doxygen graphviz dia
./ns3 configure --enable-tests --enable-examples
./ns3 build
./ns3 docs doxygen
xdg-open build/doxygen/html/index.html| Back | FazBrowse Home | New Git URL |