P4sim runs P4 programs in the ns-3 network simulator: switches execute their P4 program on the BMv2 behavioral model, while ns-3 provides the topology, the traffic and the time. This lets you evaluate programmable networks in end-to-end simulations.
Website: mingyumaz.github.io/p4sim gives an overview of P4sim, the roadmap and an animated look inside the switch.
P4sim is open-source software licensed under the Apache License 2.0; some files are under GPL-2.0-only (see License).
- BMv2 behaviour: the pipeline is BMv2, the reference model of the P4 community. P4 programs written for BMv2 run unchanged, for v1model, PSA and PNA, and so do their flow-table files (on PNA without the time commands of
pna_nic_CLI). - ns-3 integration: topology, traffic and timing come from ns-3 (ns-3.39 to ns-3.49), so P4sim combines with the other ns-3 modules.
- Timing: links serialise every frame at their data rate, a congested port sends back to back at its line rate, and the pipeline can have a packet rate (
SwitchRate) and a latency (PipelineDelay). Optional: a port queue after the egress (TransferBufferSize), and a traffic manager with virtual output queues that models the fabric of a switch. - Control plane: flow-table files at start-up; during the simulation,
P4Controllerchanges tables, counters, meters, registers and mirroring sessions, learns fromdigest()records and swaps the P4 program. - Scale: built for large networks and high traffic rates.
A P4SwitchNetDevice runs a P4 program on BMv2 inside the simulation. The figure follows one frame (green) from Host 1 to Host 2:
- Two kinds of port, which can be mixed in one switch (see Switch ports):
- Switched-Ethernet ports (right, recommended) are part of the switch: each port is a full-duplex
SwitchedEthernetChannel. They model a real switch port more closely: the switch puts each frame on the wire itself, so it knows exactly when the frame has left, and the backlog stays in its own queues. The channel can add the Ethernet overhead on the wire (Framing). - Bridge ports (left) are ordinary ns-3 devices of the switch's node, such as a CSMA or a P4 point-to-point device. They connect the switch most easily to other ns-3 modules and devices, and the topology reader builds its networks with them. P4sim 1.x had only these.
- Switched-Ethernet ports (right, recommended) are part of the switch: each port is a full-duplex
- The pipeline.
P4SwitchCoreruns the program on BMv2 (v1model, PSA, PNA or the pipeline core): input buffer, parser and ingress, traffic manager, egress and deparser, and an optional transfer buffer. Each stage has its attributes (see Switch Parameters); not every architecture has every stage. - Event-driven egress. A port takes its next frame from the traffic manager when it has sent the last one, so a congested port sends back to back at its line rate.
- The control plane. At start-up, the switch runs the flow-table file (
FlowTablePath), commands of the runtime CLI of its architecture, in the process, as the CLI would (FlowTableLoader). During the simulation,P4Controllerchanges tables, counters, meters, registers and mirroring sessions throughP4RuntimeApi, learns fromdigest()records and swaps the P4 program.
The figure is an editable draw.io SVG: open doc/figures/p4sim-architecture.drawio.svg in draw.io to change it.
Branches: main holds P4sim 2.0, which added the switched-Ethernet ports, the event-driven egress (v1model, PSA and PNA), the traffic manager and PipelineDelay. main-deprecated keeps the 1.x line (v1.0.0 and its later fixes, bridge ports only); it gets no more changes.
- Mingyu Ma, Giang T. Nguyen. "P4sim: Programming Protocol-independent Packet Processors in ns-3." 2025. [ACM DL] [arXiv]
- Reproducibility artifact: p4sim-artifact-icns3 — accepted at the 2025 International Conference on ns-3 (ICNS3).
Our implementation builds upon the P4-driven Network Simulator Module described in:
- Bai, Jiasong, et al. "NS4: Enabling programmable data plane simulation." Proc. of the Symposium on SDN Research, pp. 1–7, 2018. [ACM DL]
- Fan, Chengze, et al. "NS4: A P4-driven network simulator." Proc. of the SIGCOMM Posters and Demos, pp. 105–107, 2017. [ACM DL]
P4sim is an ns-3 module: it lives in contrib/p4sim of an ns-3 tree and needs BMv2 and the P4 compiler p4c. It builds with ns-3.39 to ns-3.49 and is tested with ns-3.39 and ns-3.49 and BMv2 1.15.8. There are four ways to set it up:
| Way | Time | Where |
|---|---|---|
Quick installation with install.sh |
about 6 minutes on 16 cores | Ubuntu 24.04 or 26.04, also in WSL2 |
| Docker | 6 to 30 minutes on 32 to 8 cores | any x86-64 host with Docker; nothing is installed on the host |
| Step by step, with the P4 tools of p4lang/tutorials | 1 to 2 hours, 15 GB | Ubuntu 24.04 |
| Virtual machine | 1 to 2 hours | Vagrant and VirtualBox |
On Ubuntu 24.04 or 26.04 (also in WSL2):
sudo apt update && sudo apt install -y git
mkdir -p ~/workdir && cd ~/workdir
git clone --depth 1 -b ns-3.39 https://github.com/nsnam/ns-3-dev-git.git ns3.39
git clone https://github.com/HapCommSys/p4sim.git ns3.39/contrib/p4sim
ns3.39/contrib/p4sim/install.shinstall.sh asks for your sudo password, installs the Ubuntu packages and p4c (prebuilt, from the p4lang repository), builds BMv2 1.15.8 into /usr/local, and builds and tests ns-3 with P4sim. It ends with the number of passed test suites and examples. For ns-3.49, replace ns-3.39 and ns3.39 by ns-3.49 and ns3.49. doc/installation.md explains what it changes, its settings and how to rerun single steps.
git clone https://github.com/HapCommSys/p4sim.git && cd p4sim
docker build -t p4sim .
docker run --rm p4sim ./ns3 run p4-v1model-ipv4-forwardingdoc/docker.md shows how to run your own simulations and compile P4 programs in the image.
cd ~/workdir/ns3.39
./ns3 run p4-hello
./test.py -s p4-packetdoc/getting-started.md continues from here: change the flow table and the P4 program of p4-hello, then write your own. doc/examples.md lists the examples and test/Readme.md the test suites. P4sim reads the P4 files (programs, flow tables, topologies) from the p4sim folder of the build, so no path needs to be set, and several ns-3 trees can live side by side. Earlier versions asked you to set P4SIM_DIR in ~/.bashrc; it is no longer needed.
P4SwitchArch |
Architecture | BMv2 target | Runtime CLI of the flow table | Notes |
|---|---|---|---|---|
| 0 | V1model | simple_switch |
simple_switch_CLI |
output queues per port and priority, or the VOQ traffic manager; digests |
| 1 | PSA | psa_switch |
psa_switch_CLI |
output queues per port and priority |
| 2 | PNA | pna_nic |
bm_CLI |
one output queue per port; no time commands in the CLI |
| 3 | Pipeline (P4CorePipeline) |
– | simple_switch_CLI |
no output queue, no packet replication; digests |
The P4 program and its flow tables define the forwarding; the P4 program sees every frame, ARP included, and must forward it. These attributes of ns3::P4SwitchNetDevice set up the switch in the simulation (doc/p4sim.rst has their defaults, doc/switch.md the details):
| Attribute | Description |
|---|---|
JsonPath |
Path to the compiled P4 program (JSON) |
FlowTablePath |
Path to the flow-table file, commands of the runtime CLI of the architecture, run at start-up |
FlowTableLoader |
Auto (default): run the flow table in the process, as the runtime CLI would; a file with a command that only the CLI runs goes to the CLI, with a warning (once per command). Cli: always the CLI, a Python program per switch, much slower (doc/switch.md) |
StartThriftServer |
Start the Thrift server of the switch at start-up, for a runtime CLI of your own (ThriftPort); default false. The CLI that runs a flow table starts it anyway |
P4SwitchArch |
Architecture: 0 V1model, 1 PSA, 2 PNA, 3 Pipeline |
SwitchRate |
v1model and PSA: packet rate of the whole P4 pipeline, in packets per second; 0 (default) = no limit |
PipelineDelay |
Latency of every packet in the P4 pipeline; 0 (default) = none |
InputBufferSizeLow |
v1model and PSA: input buffer for the packets received on the ports, in packets (PNA: 1024) |
InputBufferSizeHigh |
v1model: input buffer for resubmitted and recirculated packets, served first |
QueueBufferSize |
Each output queue, in packets: per port and priority (v1model, PSA), or per port (PNA) |
TransferBufferSize |
Frames a port holds after the egress pipeline, besides the one it sends (the hardware port queue); 0 (default) = none. v1model and PSA, not with EnableVoqFabric |
EnableVoqFabric |
v1model: use the VOQ + fabric traffic manager instead of the output queues (doc/traffic-manager.md) |
QueueScheduler |
v1model: the output queues and the scheduler that picks the next packet of a port, by type and attributes; default ns3::P4StrictPriorityScheduler, eight priority queues per port as in BMv2; ns3::P4PifoScheduler, one PIFO per port that sends the smallest rank first, a rank that the P4 program computes (doc/switch.md) |
EnableSwap |
Allow swapping the P4 program at run time (load_new_config and swap_configs of the runtime CLI, or P4Controller). The swap waits until the switch holds no packet (PNA: except those in its output queues) |
BmLogLevel |
Level of the BMv2 pipeline log in /tmp/bmv2-<thrift port>-pipeline.log.txt, trace to off. off (default) writes none; trace logs every step of every packet, which helps to debug a P4 program but slows the simulation down |
ThriftPort |
Thrift port of the runtime CLI; its server starts with StartThriftServer or when a runtime CLI first runs on the switch. 0 (default) picks the first free port from $P4SIM_THRIFT_PORT_BASE (9090), so simulations can run in parallel |
TargetProfile |
A target profile (doc/latency-model.md): tofino-like (per-packet latency by Models A and B, store-and-forward), cut-through-reference (a zero-buffer cut-through switch), or a JSON file. Replaces PipelineDelay for the packets that pass the ingress; empty (default) = none |
LearningTable, LearningAction, ControlPlaneDelay |
A table that the switch fills from the program's digests, with the profile's capacity, eviction, aging and learning path |
ChannelType, EnableTracing |
Deprecated, no effect; removed in the next major version. Trace packets with the trace sources of the devices (MacTx, MacRx, ...), PacketSink or FlowMonitor |
A switch has two kinds of port, which can be mixed. The port number is the order in which the ports are added.
- Switched-Ethernet ports:
P4SwitchNetDevice::Attach()with aSwitchedEthernetChannel, usually throughSwitchedEthernetHelper::Install(), which also creates the hosts' devices (see doc/switch.md). - Bridge ports: an ordinary ns-3 device of the switch's node, such as a CSMA or a P4 point-to-point device, added with
P4SwitchNetDevice::AddBridgePort()orP4Helper::Install(node, devices). Switches are connected to each other this way, and the topology reader (p4-net-builder.h) builds its networks with them.
- Write the P4 program (headers, parser, tables, controls).
- Compile it with
p4cinto a JSON file for BMv2. - Write an ns-3 script (in
scratch/orexamples/) that installs P4 switches on nodes. - Fill the tables: with a flow-table file at start-up, or with
P4Controllerduring the simulation. - Run it and measure throughput, latency, or packet traces.
doc/examples.md describes all 38 examples, and doc/tutorials.md maps the exercises of p4lang/tutorials to them. A selection:
| Example | Description |
|---|---|
p4-hello |
The first example, and a test bed for own P4 programs without C++ |
p4-v1model-ipv4-forwarding |
Two hosts and a switch with IPv4 forwarding (v1model) |
p4-v1model-ipv4-forwarding-bridge |
The same over point-to-point or CSMA bridge ports |
p4-psa-ipv4-forwarding, p4-pna-ipv4-forwarding |
The same with PSA and PNA |
p4-psa-meter, p4-psa-ecmp |
PSA externs: a policer with Meter and Counter, ECMP with Hash |
p4-basic-example |
Four hosts and four switches (p4lang tutorial basic) |
p4-basic-controller |
Flow entries changed by P4Controller during the simulation |
p4-l2-learning |
L2 learning: P4Controller fills the tables from the digests of the switch |
p4-basic-tunnel |
A tunnel with a custom header |
p4-firewall |
A stateful TCP firewall |
p4-ecn, p4-mri, p4-multicast |
The p4lang tutorials ecn, mri and multicast, with their solutions unchanged; they also check your own solution |
p4-spine-leaf-topo |
A spine-leaf network with ECMP load balancing |
p4-topo-fattree |
A generated k-ary fat tree with its flow tables |
p4-priority-queues |
Strict-priority queues, with the classification in the P4 program |
p4-latency |
What makes up the one-way delay through a P4 switch |
p4-codel, p4-red |
Active queue management in the egress, as programmable switches do it: CoDel, and RED with ECN |
p4-pifo-stfq |
Programmable scheduling: the P4 program computes the ranks of weighted fair queueing (STFQ), and a PIFO per port serves them |
p4-pifo-las, p4-pifo-edf |
Ranks for short flows first (LAS) and for deadlines (EDF): a PIFO side by side with SP-PIFO and AIFO, which serve the same ranks on a switch without PIFOs |
p4-voq-fabric-throughput |
Throughput of the VOQ + fabric traffic manager |
The paper evaluates P4sim with tunnelling (a custom header) and load balancing over several paths.
For a first look, the P4sim website explains the switch with figures and animations. The files below are the reference.
| File | Contents |
|---|---|
| doc/installation.md | Installation: quick, step by step, or in a virtual machine; checks |
| doc/getting-started.md | The first steps, the learning path, and common errors |
| doc/docker.md | Building and running P4sim in Docker |
| doc/examples.md | All examples and the format of their P4 files |
| doc/tutorials.md | The exercises of p4lang/tutorials, their examples, and the way from Mininet to ns-3 |
| doc/switch.md | Pipeline and queues of the switch, switched-Ethernet ports and host devices, timing on the wire |
| doc/latency-model.md | Target profiles: per-packet latency of a Tofino-like switch, a zero-buffer cut-through switch, learning tables |
| doc/traffic-manager.md | The VOQ + fabric traffic manager |
| doc/event-driven-tm-scheduler.md | How the egress sends the next frame when a port is free |
| doc/performance.md | How long a simulation takes and how to estimate it on your machine; packets per second of a switch, fat trees, where the time goes, build options |
| doc/p4sim.rst | The model in the format of the ns-3 documentation |
| test/Readme.md | The test suites and how to write a test |
| doc/TODO.md | Open design questions, feature gaps and known bugs |
SwitchRatemodels only the throughput of the P4 pipeline, not its per-packet latency: a packet that finds the pipeline idle passes it without delay. UsePipelineDelayor aTargetProfilefor the latency.- Every device stores a frame completely before it forwards it, except a switch with the
cut-through-referenceprofile (v1model, switched-Ethernet ports, no buffer). P4sim does not model the stages of a hardware pipeline; thetofino-likeprofile gives the latency of Tofino's ingress pipeline by a published linear model (doc/latency-model.md). - ns-3.39 to ns-3.49; tested with ns-3.39 and ns-3.49.
- PNA has no Thrift service of its own: its
FlowTablePathholds commands of BMv2's genericbm_CLI, which has the table commands but not the time commands ofpna_nic_CLI. - PSA and PNA programs give no digest records, as BMv2's
psa_switchandpna_nicgive none. - PSA and PNA programs can use the externs
Counter,Meter,Hash,InternetChecksumandRandom(BMv2's, in utils/psa_externs). p4c does not compileDirectCounterandDirectMeter, or aCounterin a PNA program, for BMv2 yet. Their meters measure simulation time, asmeter.execute_meter()of v1model does. - PNA has no
recirculate()and nomirror_packet(), as BMv2'spna_nichas neither: a PNA program that calls one is rejected when the switch loads it. - v1model direct meters (
direct_meter) count wall-clock time, as BMv2 runs them;meter.execute_meter()counts simulation time (doc/switch.md). - The pipeline core (
P4SwitchArch3) has no output queue: a congested port can lose frames. It has no packet replication engine either: multicast (mcast_grp), clones, resubmit and recirculation have no effect, and the frame goes toegress_speconly. The switch warns on stderr, once for each of them, when the P4 program asks for one. - Each switch keeps 3 file descriptors open, 8 once its Thrift server runs: with the usual limit of 1024, about 330 switches fit in one simulation (120 with the server); raise it with
ulimit -nfor more. doc/performance.md gives the speed and memory per switch. - The Thrift server of a switch runs in its own thread: use the runtime CLIs before and after the simulation, and
P4Controllerduring it. It starts withStartThriftServeror with the first runtime CLI that runs on the switch (FlowTableLoaderCli, a flow table that needs the CLI,P4SwitchCore::ExecuteCliCommands()). BMv2 cannot stop the server, so the Thrift port of a destroyed switch stays open; it answers every call with an error.
doc/TODO.md has the details and the other open items.
In the ns-3 tree, with all of ns-3 configured:
sudo apt install doxygen graphviz dia
./ns3 configure --enable-tests --enable-examples --enable-modules='' --filter-module-examples-and-tests=''
./ns3 build
./ns3 docs doxygen
xdg-open doc/html/index.htmlP4sim is licensed under the Apache License 2.0. The header of each file names its license; a file without a license header is under Apache 2.0. Exceptions:
- GPL-2.0-only (license text):
- code based on ns-3 and on Jeff Young's switched Ethernet channel:
custom-p2p-net-device,p4-p2p-channel,p4-p2p-helper,p4-topology-reader,p4-topology-reader-helper,switched-ethernet-channelandtest/p4-p2p-channel-test-suite.cc; p4-core-pipeline;- files with code by Vineet Goel:
eth-net-device,p4-switch-net-device,p4-traffic-manager; the testschannel-state-test-suite.cc,p4-controller-test-suite.ccandp4-traffic-manager-test-suite.cc; the examplesp4-basic-controller,p4-controller-action-profile,p4-source-routingandp4-voq-fabric-*.
- code based on ns-3 and on Jeff Young's switched Ethernet channel:
- Apache 2.0 with the notices of BMv2's authors: the parts taken from BMv2 (
p4-core-v1model,p4-core-psa,p4-nic-pna,p4-queue,primitives-v1model.h,primitives-pna.h,register-access-v1model.h,utils/psa_externs,patches/config.h,patches/bmv2-log-level-check.patch). - Apache-2.0, from BMv2:
utils/bm-event-observer.h, a copy of BMv2'sevent_observer.h(Copyright 2026 Yuao Ma), which BMv2 1.15.8 does not install. - MIT:
patches/bmv2-spdlog-cxx20.patch, a change to spdlog (Copyright (c) 2014 Gabi Melman) as bundled with BMv2. - Code taken from P4Simulator (Apache 2.0) says so in its header.
P4sim builds on ns-3, which is licensed under GPL-2.0-only, and on BMv2 (Apache 2.0).
- Maintainers: Mingyu Ma
- Contributors: Thanks to GSoC 2025 & 2026 with Davide support and contributor Vineet.