Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ A switch has two kinds of port, which can be mixed. The port number is the order

## Examples

[doc/examples.md](doc/examples.md) describes all 36 examples, and [doc/tutorials.md](doc/tutorials.md) maps the exercises of [p4lang/tutorials](https://github.com/p4lang/tutorials) to them. A selection:
[doc/examples.md](doc/examples.md) describes all 38 examples, and [doc/tutorials.md](doc/tutorials.md) maps the exercises of [p4lang/tutorials](https://github.com/p4lang/tutorials) to them. A selection:

| Example | Description |
|---|---|
Expand All @@ -181,6 +181,7 @@ A switch has two kinds of port, which can be mixed. The port number is the order
| `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](https://dl.acm.org/doi/10.1145/3747204.3747210) evaluates P4sim with tunnelling (a custom header) and load balancing over several paths.
Expand Down
10 changes: 7 additions & 3 deletions doc/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ line (bridge ports only, the old timing model) and gets no more changes.
v1model only. PSA keeps its own copy of the strict-priority queues
(`P4CorePsa::egress_buffer`); proposal: give it a `P4QueueScheduler` too. The
VOQ + fabric traffic manager is a path of its own and stays apart.
- Next for PIFOs: more rank examples (LAS, EDF, and the approximations SP-PIFO
and AIFO on priority queues), then PIFO trees for hierarchical scheduling,
which need a rank per level (`P4QueueMetadata` holds one).
- Next for PIFOs: PIFO trees for hierarchical scheduling, which need a rank per
level (`P4QueueMetadata` holds one). The rank examples (STFQ, LAS, EDF) and
the approximations SP-PIFO and AIFO exist (`p4-pifo-*`).
- AIFO (`examples/p4src/pifo_approx/aifo.p4`) learns the queue length from the
egress, after a packet left; the ingress of real hardware may see it sooner.
With LAS, whose ranks only grow, it drops a long flow's retransmissions too
(`p4-pifo-las`); a sampled or per-flow window is not tried.
- A `P4QueueDisc` (P4 in an ns-3 queue disc, for NICs and hosts) waits for a
test with a P4 SmartNIC.
- **The pipeline core** (`P4SwitchArch` 3) ignores `mcast_grp`, clones,
Expand Down
43 changes: 43 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ port, set up by `SwitchedEthernetHelper`; and **bridge** ports, ordinary ns-3 de
| `p4-codel` | CoDel in the egress, as a programmable switch runs it [12]: two TCP flows through a 100 Mbps port with a queue of 1000 packets; CoDel keeps the time in the queue below its 5 ms target instead of about 99 ms, at the same goodput. `--aqm=none` turns it off; `--model=ns3` runs an ns-3 router with `CoDelQueueDisc` instead, as a baseline | v1model | switched Ethernet | `codel` |
| `p4-red` | RED with ECN in the egress: a moving average of the queue depth decides which packets to mark (ECN-capable TCP) or, with `--ecn=0`, to drop; the queue stays short and the link busy | v1model | switched Ethernet | `red` |
| `p4-pifo-stfq` | Programmable scheduling (`QueueScheduler` `ns3::P4PifoScheduler`) [13]: the ingress computes the ranks of start-time fair queueing [14], weights 1:1:2 from the flow table, and three UDP flows of 60 Mbps share a 100 Mbps port 25:25:50 %. `--scheduler=strict` serves one FIFO instead: about a third each | v1model | switched Ethernet | `stfq` |
| `p4-pifo-las` | Least attained service [15]: the rank of a packet is the bytes its TCP flow sent before it, so short flows pass two long ones without the switch knowing flow sizes. Four switches side by side with the same flows: FIFO, SP-PIFO [16] and AIFO [17] (the same ranks on a switch without PIFOs) and the PIFO; it prints the flow completion times of the short flows | v1model | switched Ethernet | `las`, `pifo_approx` |
| `p4-pifo-edf` | Earliest deadline first: the rank is the arrival time plus the deadline of the packet's class (1, 2 and 4 ms, from the flow table); three UDP flows keep a 100 Mbps port 94 % busy. FIFO, SP-PIFO, AIFO and PIFO side by side; the egress counts the packets that waited longer than their deadline | v1model | switched Ethernet | `edf`, `pifo_approx` |
| `p4-spine-leaf-topo` | A spine-leaf network with ECMP load balancing (tutorial `load_balance` [7]) | v1model | CSMA and P4 point-to-point | `load_balance` |
| `p4-topo-fattree` | A k-ary fat tree with generated flow tables (`FattreeTopoHelper`, `BuildFlowtableHelper`); `--model=1` runs ns-3 bridges instead | v1model | CSMA and P4 point-to-point | `fat-tree` |
| `topo-fattree` | The same fat tree from plain ns-3 nodes, without P4, as a baseline | – | CSMA | – |
Expand Down Expand Up @@ -94,6 +96,42 @@ goodput of `p4-red` depends on its random marks (`--RngRun`): its thresholds lie
the bandwidth-delay product of the flows, so the link idles when both slow down at
once: 74 to 96 Mbps over eight seeds with ns-3.39.

`p4-pifo-las` and `p4-pifo-edf` build their network four times in one simulation and
give each the same traffic (`examples/pifo-example.h`); only the way the switch serves
the ranks differs. SP-PIFO and AIFO are P4 controls in `examples/p4src/pifo_approx/`
for a switch with the usual eight priority queues; the register `approximation` of the
program switches them on. With ns-3.39 and the default arguments:

| `p4-pifo-las` | Short flows: mean FCT | median | max | Long flows: goodput |
|---|--:|--:|--:|--:|
| FIFO | 52.5 ms | 50.5 ms | 87 ms | 85.1 Mbps |
| SP-PIFO | 5.7 ms | 6.2 ms | 11 ms | 85.1 Mbps |
| AIFO | 21.9 ms | 6.9 ms | 214 ms | 51.9 Mbps |
| PIFO | 5.7 ms | 6.2 ms | 11 ms | 85.2 Mbps |

SP-PIFO comes close to the PIFO. AIFO keeps the queue short but only drops: LAS ranks
grow over the life of a flow, so a long flow's own earlier packets have the smaller
ranks, and AIFO drops it, its retransmissions too, whenever the queue holds more than
an eighth of its capacity. Each lost retransmission costs TCP a timeout of 200 ms,
which shows in the long flows' goodput and in the slowest short flows. Over eight
seeds (`--seconds=1`), the PIFO's mean FCT is 7 to 17 times below the FIFO's,
SP-PIFO's at most 1.2 times the PIFO's, and AIFO's long flows get 19 to 74 Mbps.
ns-3.49's TCP loses more behind the FIFO (mean FCT 53 to 138 ms, against 46 to 84 ms)
and less with AIFO (long flows 52 to 86 Mbps); the PIFO and SP-PIFO give nearly the
same times as with ns-3.39.

| `p4-pifo-edf`: missed deadlines | class 0 (1 ms) | class 1 (2 ms) | class 2 (4 ms) |
|---|--:|--:|--:|
| FIFO | 23.3 % | 7.7 % | 0 % |
| SP-PIFO | 0 % | 0 % | 7.5 % |
| AIFO | 12.6 % | 0 % | 1.2 % (dropped) |
| PIFO | 0.6 % | 1.0 % | 0.7 % |

SP-PIFO's bounds settle into one class per priority, which is strict priority: the
class with the loosest deadline pays for it. EDF spreads its few misses over the
classes. Over twelve seeds, the class that missed most missed for 0 to 7.9 % of its
packets with the PIFO and 14 to 26 % with the FIFO; ns-3.49 gives the same numbers.

Results of the examples in the paper are in
[p4sim-artifact-icns3](https://github.com/HapCommSys/p4sim-artifact-icns3).

Expand Down Expand Up @@ -136,3 +174,8 @@ in `examples/CMakeLists.txt`, and a line in `test/examples-to-run.py`.
14. P. Goyal, H. M. Vin, H. Cheng: Start-Time Fair Queueing: A Scheduling Algorithm for
Integrated Services Packet Switching Networks. IEEE/ACM Transactions on Networking,
1997
15. I. A. Rai, G. Urvoy-Keller, E. W. Biersack: Analysis of LAS Scheduling for Job Size
Distributions with High Variance. ACM SIGMETRICS 2003
16. R. Alcoz, A. Dietmüller, L. Vanbever: SP-PIFO: Approximating Push-In First-Out
Behaviors using Strict-Priority Queues. USENIX NSDI 2020
17. Z. Yu et al.: Programmable Packet Scheduling with a Single Queue. ACM SIGCOMM 2021
19 changes: 18 additions & 1 deletion doc/switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,27 @@ p4.SetDeviceAttribute("QueueScheduler",
- `TransferBufferSize` works as with the priority queues.

[`p4-pifo-stfq`](examples.md) computes the ranks of start-time fair queueing in
the ingress and shares a port by weights.
the ingress and shares a port by weights. [`p4-pifo-las`](examples.md) (least
attained service: short TCP flows pass long ones) and
[`p4-pifo-edf`](examples.md) (earliest deadline first) compare the PIFO with two
ways to serve the same ranks on a switch without PIFOs, side by side:

- SP-PIFO [2] (`examples/p4src/pifo_approx/sp_pifo.p4`) maps the ranks to the
eight priority queues of `ns3::P4StrictPriorityScheduler`, with a bound per
queue that follows the ranks.
- AIFO [3] (`examples/p4src/pifo_approx/aifo.p4`) keeps one FIFO and drops, in the
ingress, the packets whose rank is large among the last 16 while the queue fills;
the egress tells the ingress the queue's length.

Both are P4 controls that a program applies after it has computed the rank and
the egress port; `las.p4` and `edf.p4` include them.

[1] A. Sivaraman et al., "Programmable Packet Scheduling at Line Rate", ACM
SIGCOMM 2016.
[2] R. Alcoz, A. Dietmüller, L. Vanbever, "SP-PIFO: Approximating Push-In
First-Out Behaviors using Strict-Priority Queues", USENIX NSDI 2020.
[3] Z. Yu et al., "Programmable Packet Scheduling with a Single Queue", ACM
SIGCOMM 2021.

## Time and random numbers in the P4 program

Expand Down
16 changes: 16 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ build_lib_example(
LIBRARIES_TO_LINK ${P4SIM_BASE_LIBS}
)

# Least attained service: short TCP flows pass long ones; FIFO, SP-PIFO, AIFO and
# PIFO side by side
build_lib_example(
NAME p4-pifo-las
SOURCE_FILES p4-pifo-las.cc
LIBRARIES_TO_LINK ${P4SIM_BASE_LIBS}
)

# Earliest deadline first: three traffic classes with deadlines; FIFO, SP-PIFO,
# AIFO and PIFO side by side
build_lib_example(
NAME p4-pifo-edf
SOURCE_FILES p4-pifo-edf.cc
LIBRARIES_TO_LINK ${P4SIM_BASE_LIBS}
)

# What makes up the one-way delay through a P4 switch
build_lib_example(
NAME p4-latency
Expand Down
151 changes: 151 additions & 0 deletions examples/p4-pifo-edf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2026 TU Dresden
*
* SPDX-License-Identifier: Apache-2.0
*
* Authors: Mingyu Ma <mingyu.ma@tu-dresden.de>
*/

/**
* Earliest deadline first (EDF) with a PIFO, and on switches without PIFOs:
* SP-PIFO and AIFO.
*
* host0 (class 0, deadline 1 ms) ──1 Gbps──┐
* host1 (class 1, deadline 2 ms) ──1 Gbps──┼── P4 switch ──100 Mbps──> host3
* host2 (class 2, deadline 4 ms) ──1 Gbps──┘
*
* Each of hosts 0 to 2 sends a UDP flow to host3 with random times between the
* packets (Poisson arrivals), 15, 30 and 45 Mbps: the port to host3 is busy
* about 94 % of the time, and its queue grows and shrinks at random. The P4
* program (edf.p4) gives each packet the rank EDF asks for: its arrival time
* plus the deadline of its class, the time by which it must leave the queue.
* The egress counts per class the packets that waited longer (late).
*
* The example builds this network four times, side by side in one simulation,
* with the same packets, and serves the ranks in four ways:
* FIFO the priority queues, all packets in queue 0: the ranks do nothing
* SP-PIFO the ranks go to the 8 priority queues by SP-PIFO (sp_pifo.p4)
* AIFO one FIFO; the ranks decide which packets enter it (aifo.p4)
* PIFO ns3::P4PifoScheduler: the smallest rank leaves first
* A packet misses its deadline if it is late or lost; it prints, per class, the
* share of the packets that missed it, and their time in the queue.
*
* What to expect: the FIFO makes every class wait alike, so class 0 misses
* most. EDF meets every deadline that can be met at all; when a burst makes
* that impossible, it spreads the misses over the classes. SP-PIFO's bounds
* settle into one class per priority, which is strict priority: classes 0 and
* 1 never miss, class 2 misses more than with EDF. AIFO reorders nothing: it
* drops class 2 when the queue grows, which shortens the wait of all classes a
* little.
*/

#include "pifo-example.h"

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <vector>

using namespace ns3;
using namespace p4sim_example;

NS_LOG_COMPONENT_DEFINE("P4PifoEdf");

namespace
{

const uint32_t BOTTLENECK = 3; ///< the port of the switch to host3
const uint32_t CLASSES = 3;

/// A class: its deadline is in the flow table (flowtable_0.txt)
struct TrafficClass
{
double deadlineMs; ///< as in the flow table
const char* rate; ///< of its flow
};

const TrafficClass TRAFFIC_CLASSES[CLASSES] = {{1, "15Mbps"}, {2, "30Mbps"}, {4, "45Mbps"}};

} // namespace

int
main(int argc, char* argv[])
{
double seconds = 2.0;
CommandLine cmd;
cmd.AddValue("seconds", "How long the flows send", seconds);
cmd.Parse(argc, argv);
NS_ABORT_MSG_IF(seconds <= 0, "--seconds must be above 0");

const Time start = Seconds(0.1);
std::vector<PifoNetwork> nets;
std::vector<std::vector<uint64_t>> sent(std::size(RANK_SCHEDULERS),
std::vector<uint64_t>(CLASSES));
for (size_t n = 0; n < std::size(RANK_SCHEDULERS); n++)
{
nets.push_back(BuildPifoNetwork("edf", 4, RANK_SCHEDULERS[n], MicroSeconds(10)));
PifoNetwork& net = nets.back();
for (uint32_t c = 0; c < CLASSES; c++)
{
uint16_t port = 5000 + c; // the class in the flow table
PacketSinkHelper sink("ns3::UdpSocketFactory",
InetSocketAddress(Ipv4Address::GetAny(), port));
sink.Install(net.hosts.Get(3));
// The same stream in every network: the same packets at the same times
Ptr<PoissonSource> source =
CreateObject<PoissonSource>(InetSocketAddress(net.interfaces.GetAddress(3), port),
DataRate(TRAFFIC_CLASSES[c].rate),
c,
&sent[n][c]);
net.hosts.Get(c)->AddApplication(source);
source->SetStartTime(start);
source->SetStopTime(start + Seconds(seconds));
}
}
Simulator::Stop(start + Seconds(seconds + 0.5));
Simulator::Run();

std::cout << "EDF: three UDP flows of 15, 30 and 45 Mbps into 100 Mbps, four switches "
"side by side\n\n"
<< "scheduler class deadline sent missed: late lost share"
<< " time in queue (ms): mean max\n";
// missed[n][c]: the share of the packets of class c that missed the deadline
std::vector<std::vector<double>> missed(nets.size(), std::vector<double>(CLASSES));
for (size_t n = 0; n < nets.size(); n++)
{
for (uint32_t c = 0; c < CLASSES; c++)
{
Ptr<P4SwitchNetDevice> sw = nets[n].sw;
uint64_t left = ReadP4Register(sw, "packets", c);
uint64_t late = ReadP4Register(sw, "late", c);
uint64_t lost = sent[n][c] - left;
missed[n][c] = sent[n][c] ? double(late + lost) / sent[n][c] : 0;
double mean = left ? ReadP4Register(sw, "queue_time", c) / 1e6 / left : 0;
std::cout << std::left << std::setw(11) << (c == 0 ? nets[n].scheduler.name : "")
<< std::right << std::setw(5) << c << std::fixed << std::setprecision(0)
<< std::setw(7) << TRAFFIC_CLASSES[c].deadlineMs << " ms" << std::setw(10)
<< sent[n][c] << std::setw(15) << late << std::setw(7) << lost
<< std::setprecision(1) << std::setw(7) << missed[n][c] * 100 << " %"
<< std::setprecision(2) << std::setw(27) << mean << std::setw(8)
<< ReadP4Register(sw, "max_queue_time", c) / 1e6 << "\n";
}
}
Simulator::Destroy();

// The largest share of missed deadlines in a class
std::vector<double> worst(nets.size());
std::cout << "\nThe class that missed most:";
for (size_t n = 0; n < nets.size(); n++)
{
worst[n] = *std::max_element(missed[n].begin(), missed[n].end());
std::cout << (n ? ", " : " ") << nets[n].scheduler.name << " " << std::setprecision(1)
<< worst[n] * 100 << " %";
}
std::cout << "\n";

// FIFO 0, SP-PIFO 1, AIFO 2, PIFO 3
return Check(worst[3] * 2 < worst[0] && worst[1] < worst[0] && worst[2] < worst[0],
"the class that missed most missed less than half as often with the PIFO as "
"with the FIFO, and less often with SP-PIFO and with AIFO");
}
Loading