Inside the switch
How a P4sim switch works
Four animated walkthroughs, drawn from the P4sim design figures and checked against its code: where the switch sits in ns-3, how a packet travels through the pipeline, how the queues and the scheduler decide which packet leaves next, and how custom headers cross the network. Each one plays by itself; click a step to jump to it.
Chapter 1 · Switch model
A P4 switch inside ns-3
A control plane programs the tables; the data plane runs every packet through the P4 program.
-
P4SwitchNetDeviceis installed on an ns-3 node like any other device. Its ports are the node's CSMA or point-to-point devices, so hosts and other switches attach over ordinary ns-3 links. -
Rules arrive through the control-plane interface: runtime CLI commands over Thrift at start-up, or
P4Controllercalls during the run. Here a rule sends 10.1.1.2 to port 3. -
Host 1 sends a packet to 10.1.1.2. The switch hands it to its BMv2 core, whose parser splits the bytes into header fields.
-
The table
ipv4_nhoplooks upipv4.dst. The rule installed a moment ago matches, and its action sets the egress port to 3. -
The deparser turns the fields back into bytes, and the packet leaves port 3 on an ns-3 link to Host 2. Every step ran the real P4 program.
Chapter 2 · Pipeline and queues
Through the pipeline
From the input buffer to the egress port: parsing, match-action stages, the traffic manager's priority queues, and the loops a P4 program can take.
-
The compiled P4 program defines the parser graph, the control flow, the actions and the tables. ns-3 attributes set the buffer sizes, the processing rate (
SwitchRate) and the ports. -
An arriving packet joins the input buffer. Packets from outside wait in one queue; resubmitted and recirculated packets have their own, higher-priority queue.
-
The parser follows the parse graph of the P4 program and extracts the headers into the packet header vector (PHV).
-
Stage by stage, ingress tables read the PHV and run actions, with tables, registers and counters as state. Here they set the egress port to 3 and the priority to 5.
-
The packet joins queue 5 of port 3. An ns-3 timer takes one packet every 1 /
SwitchRate, highest queue first: the packet in queue 7 leaves before ours. Chapter 3 looks at the queues closely. -
Egress tables can still rewrite the packet; the deparser turns the PHV back into bytes, and the packet leaves on port 3. V1model parses once and deparses at the end; PSA adds a deparser after ingress and a parser before egress.
-
A program can also resubmit a packet to ingress, recirculate it after egress, clone it (ingress-to-egress or egress-to-egress), or send multicast copies to several ports.
Chapter 3 · Queues and scheduling
Which packet leaves next?
A closer look at the traffic manager: eight priority queues per port, a scheduler driven by a clock, per-queue rate limits and full queues. Below the walkthrough, a live model lets you try it.
-
The
p4-queue-testexample sends three UDP flows from Host 0 to Host 1. Its P4 program,qos.p4, matches the UDP destination port in the tableudp_priorityand setsstandard_metadata.priority: port 2000 gets priority 1, port 3000 priority 2, port 4000 priority 3. -
Every egress port has eight queues, and priority p goes to queue p. On the way in, the switch writes
enq_timestampandenq_qdepthinto the packet's metadata. Packets arrive faster than they leave, so the queues fill. -
The scheduler runs on an ns-3 timer. Every 1 /
SwitchRateit takes one packet, from the highest queue whose head is ready: one packet per millisecond with the default 1000 packets per second. There is one scheduler for the whole switch, so all ports share this rate. -
By default each queue may send as fast as
SwitchRate, so the scheduler is plain strict priority. While queue 3 always has a packet ready, it wins every tick, and the packets in queues 2 and 1 keep waiting. -
Each queue can have its own rate. Here queue 3 is limited to 500 packets per second, one every 2 ms. When its next packet is not due yet, the scheduler serves the next queue down, so flows 2 and 1 get through. The V1model core sets these rates with
SetEgressPriorityQueueRate. -
Each queue holds
QueueBufferSizepackets, 128 by default (6 in this picture). A packet that finds its queue full is dropped at once. -
At dequeue the switch writes
deq_timedelta(the time spent queued, in nanoseconds in P4sim),deq_qdepthandqid, which egress code can read, for example to mark congestion. The packet then joins the port's ns-3 TX queue, and the link sends it at itsDataRate. If the link is slower than the switch, packets wait there instead, in arrival order.
Try it
Play with the queues
The same rules, simulated in your browser: three constant-rate UDP flows with 1000-byte payloads as in p4-queue-test, port 1's eight queues and scheduler, and its ns-3 TX queue of 100 packets. A simplified model, not ns-3 output.
| Flow | Offered | Delivered | Dropped: switch | Dropped: TX | Queue delay |
|---|
Chapter 4 · Custom headers
Custom headers across the network
Hosts in ns-3 can carry a header of your own design, so P4 programs such as tunnels have something to parse and match.
-
Host 1's ns-3 stack builds Ethernet, IPv4 and UDP headers. Each header's type field names the next one: etherType 0x0800 means IPv4.
-
A
CustomHeaderis a list of named fields with bit widths, plus a layer and an operator that say where it goes. From thep4-basic-tunnelexample:CustomHeader h; h.SetLayer(HeaderLayer::LAYER_3); h.SetOperator(ADD_BEFORE); // before IPv4 h.AddField("proto_id", 16); h.AddField("dst_id", 16); device->SetCustomHeader(h); -
CustomP2PNetDeviceinserts the header and rewires the chain: the type field in front of it becomes 0xDC, P4sim's custom protocol number, and the custom header records which protocol followed, so the original packet can be restored. -
On the way, a P4 switch sees the 0xDC type and its parser extracts the custom header. Tables can then match on its fields:
basic_tunnelforwards ondst_id. -
The receiving
CustomP2PNetDevicewalks the headers, drops the custom one, restores the original type field and passes an ordinary packet to its IPv4 stack.
Next
Run it yourself
Every walkthrough above maps to code and examples in the repository: p4-switch-net-device.cc, p4-core-v1model.cc, p4-queue.h, custom-p2p-net-device.cc, and the examples p4-v1model-ipv4-forwarding, p4-queue-test and p4-basic-tunnel.