P4sim

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.

A P4 switch in ns-3 A P4SwitchNetDevice with four ports. The P4 controller sends a rule through the control-plane interface into the match-action table. Host 1 sends a packet into port 0; the parser extracts its headers, the table matches the destination and sends it out of port 3 to Host 2. P4 controller flow tables · API P4SwitchNetDevice an ns-3 NetDevice CONTROL PLANE Control-plane interface Thrift · runtime CLI · P4Controller table_add ipv4_nhop 10.1.1.2 → port 3 PROGRAMMABLE DATA PLANE P4SwitchCore BMv2 · V1model / PSA / PNA Parser bytes → fields table ipv4_nhop · match ipv4.dst 10.1.1.1 → ipv4_forward(port 0) 10.1.1.2 → ipv4_forward(port 3) ✓ hit default → drop Port 0 Port 1 Port 2 Port 3 Host 1 sender · 10.1.1.1 Host 2 receiver · 10.1.1.2
  1. P4SwitchNetDevice is 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.

  2. Rules arrive through the control-plane interface: runtime CLI commands over Thrift at start-up, or P4Controller calls during the run. Here a rule sends 10.1.1.2 to port 3.

  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.

  4. The table ipv4_nhop looks up ipv4.dst. The rule installed a moment ago matches, and its action sets the egress port to 3.

  5. 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 V1model pipeline in P4sim Port 0, input buffer, parser, ingress match-action stages, traffic manager with eight priority queues for port 3, egress stages, deparser and port 3. Configuration comes from ns-3 attributes and from the P4 program. Loops for resubmit, recirculate and egress-to-egress clones run below. FROM ns-3 ATTRIBUTES buffers SwitchRate ports FROM THE P4 PROGRAM (COMPILED JSON) parser graph control flow actions tables PHV · header & metadata fields ipv4.dst = 10.1.1.2 egress_spec = – priority = – Port 0 Input buffer internal external Parser parse graph Ingress 1 2 N match-action stages Traffic manager port 3 · 8 priority queues 7 6 5 4 3 2 1 0 1 packet every 1 / SwitchRate Egress 1 … N match-action Deparser PHV → bytes Port 3 resubmit recirculate clone E2E clone I2E multicast copies
  1. 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.

  2. An arriving packet joins the input buffer. Packets from outside wait in one queue; resubmitted and recirculated packets have their own, higher-priority queue.

  3. The parser follows the parse graph of the P4 program and extracts the headers into the packet header vector (PHV).

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

  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.

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

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

Priority queues and the scheduler of a P4sim V1model switch Host 0 sends three UDP flows. The ingress table udp_priority gives them priority 1, 2 and 3. Port 1 has eight queues in the traffic manager; on every tick of its clock the scheduler takes one packet from the highest queue whose head is ready. Packets then pass egress, the port's ns-3 TX queue and the link to Host 1. A panel shows the queueing metadata the switch writes. P4-QUEUE-TEST 1 UDP :2000 → priority 1 2 UDP :3000 → priority 2 3 UDP :4000 → priority 3 SwitchRate 1000 pps → 1 tick = 1 ms link · DataRate Host 0 3 UDP flows 1 2 3 Ingress table udp_priority dst 2000 → priority 1 dst 3000 → priority 2 dst 4000 → priority 3 key: udp.dstPort action set_priority(p) Traffic manager · port 1 priority p → queue p · head on the right q7 q6 q5 q4 q3 q2 q1 q0 ≤ 500 pps ready waiting waiting capacity 6 dropped Scheduler one for the switch tick = 1 / SwitchRate 1 packet per tick, highest ready queue t = 0 ms Egress + deparser reads deq_* Port 1 TX queue DropTail ns-3 device queueing_metadata (times in ns) written by the queues, read in egress enq_timestamp = – · enq_qdepth = – deq_timedelta = – · deq_qdepth = – · qid = – one scheduler serves every port, so they share SwitchRate Host 1 · received 1 0 2 0 3 0
  1. The p4-queue-test example sends three UDP flows from Host 0 to Host 1. Its P4 program, qos.p4, matches the UDP destination port in the table udp_priority and sets standard_metadata.priority: port 2000 gets priority 1, port 3000 priority 2, port 4000 priority 3.

  2. Every egress port has eight queues, and priority p goes to queue p. On the way in, the switch writes enq_timestamp and enq_qdepth into the packet's metadata. Packets arrive faster than they leave, so the queues fill.

  3. The scheduler runs on an ns-3 timer. Every 1 / SwitchRate it 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.

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

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

  6. Each queue holds QueueBufferSize packets, 128 by default (6 in this picture). A packet that finds its queue full is dropped at once.

  7. At dequeue the switch writes deq_timedelta (the time spent queued, in nanoseconds in P4sim), deq_qdepth and qid, 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 its DataRate. 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.

Scenario

Offered load
Switch
Link to Host 1
Port 1 queuest = 0.0 s
Delivered to Host 1 · packets per secondlast 10 s
    Per flow · rates and queue delay over the last second, drops since the start
    FlowOfferedDeliveredDropped: switchDropped: TXQueue 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.

    Custom header position
    A custom header on its way through the network A packet with Ethernet, IPv4, UDP and payload. The sending CustomP2PNetDevice inserts a custom header and changes the type field that points to it. P4 switches parse the custom header. The receiving device removes it and restores the original packet. Host 1 stack CustomP2PNetDevice P4 switch CustomP2PNetDevice Host 2 stack sender inserts parses · matches removes receiver Ethernet etherType 0x0800 LAYER 2 IPv4 protocol 0x11 (UDP) LAYER 3 UDP ports LAYER 4 Payload application data CustomHeader proto_id · dst_id next: 0x0800 (IPv4) CUSTOM etherType 0x0800 tells the receiver that IPv4 comes next P4 PARSER (basic_tunnel) ethernet custom ipv4 0x00DC 0x0800 → match dst_id
    1. 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.

    2. A CustomHeader is a list of named fields with bit widths, plus a layer and an operator that say where it goes. From the p4-basic-tunnel example:

      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);
    3. CustomP2PNetDevice inserts 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.

    4. 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_tunnel forwards on dst_id.

    5. The receiving CustomP2PNetDevice walks 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.

    Try P4sim

    Install it with ns-3.39 and run the examples.