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
10 changes: 6 additions & 4 deletions doc/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ the run time in the default build.
`counter_reset`, `mc_node_update`, the PRE commands of PNA, ...) still starts
the Python CLI for the whole file, at about 85 ms per switch. Add them to the
loader, with cases in `p4-flow-table-loader`, when a flow table needs them.
- **Fields and objects looked up by name per packet.** The v1model core calls
`phv->get_field("standard_metadata...")` about ten times per packet, and
`get_pipeline("ingress")` and the like: 3 to 4 %, and cache misses in large
networks. Proposal: resolve them once per program (and again after a swap).
- **Fields and objects looked up by name per packet.** Done for the v1model
core: it finds its PHV fields, parser, pipelines and deparser once per
program (and again after a swap), which made one switch 6.6 % and fat trees
12 to 14 % faster ([performance.md](performance.md)). The PSA, PNA and
pipeline cores still look them up by name on every packet; the same change
would suit them.
- **The time per hop grows with the network**: the instructions per hop stay the
same, but from k=4 to k=8 the cycles per hop double (77,000 to 151,000), as the
state of 80 BMv2 switches no longer fits in the caches. Fewer allocations and
Expand Down
15 changes: 11 additions & 4 deletions doc/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,18 @@ the C and C++ libraries (`malloc`, `memcpy`, `std::string`, ...) and in ns-3's
for them: 9 to 11 % of the time, and 28 % (one switch) to 43 % (fat tree) of the
instructions of the optimized build (next section). P4sim's log patch for BMv2
makes the macros check the level first ([Build options](#build-options)).
- **Fields by name.** P4sim's v1model core reads and writes about ten
- **Fields by name.** P4sim's v1model core read and wrote about ten
`standard_metadata` and intrinsic fields per packet with
`phv->get_field("standard_metadata.egress_spec")` and finds its parser,
`phv->get_field("standard_metadata.egress_spec")` and found its parser,
pipelines and deparser by name (`get_pipeline("ingress")`): each a string hash
and a map lookup, and in a large network a cache miss.
and a map lookup, and in a large network a cache miss. It now finds them once
per program (December 2026), and keeps for each field its header and offset in
the PHV. CPU time of the traffic, the median of three to five runs taking
turns with the version before, the same traffic: one switch (200,000 packets)
4.10 s → 3.85 s (6.6 %), a k=4 fat tree 2.75 s → 2.45 s (12 %), k=8
2.79 s → 2.44 s (14 %): more than the 3 to 4 % of the profile, as the large
networks also lose cache misses. The PSA, PNA and pipeline cores still look
them up by name.
- **P4sim's glue** has no single hot spot: the largest items are the
switched-Ethernet channel (4 %), the egress dequeue (1 %), `Receive()` (1 %)
and `ConvertToBmPacket()` (1 to 2 %), which allocates and copies each frame
Expand Down Expand Up @@ -340,7 +347,7 @@ Ranked by gain for the cost.
| 2 | Make BMv2's log macros check the level before they build their message. Done where `install.sh` and the `Dockerfile` build BMv2 (`patches/bmv2-log-level-check.patch`); upstream proposal pending | the effect of `--disable-logging-macros` without losing `BmLogLevel`: in the default build 23 to 40 % of the instructions, 1.4 to 1.7 times the speed | a BMv2 rebuild for a BMv2 installed otherwise |
| 3 | Done: flow tables load in the process (`FlowTableLoader` `Auto`, the default) instead of one Python CLI per switch | init of k=16 from 228 s to 3.9 s, k=8 from 12 s to 0.4 s; 3 file descriptors per switch instead of 8 | – |
| 4 | Raise the soft limit of open files to the hard limit when switches start | more than about 330 switches (120 with the runtime CLI) without `ulimit -n` | a few lines |
| 5 | Resolve the PHV fields and P4 objects that the v1model core uses once per program | 3 to 4 %, more in large networks | small |
| 5 | Done for v1model: the core finds the PHV fields and P4 objects it uses once per program | one switch 6.6 %, fat trees 12 % (k=4) and 14 % (k=8) | – |
| 6 | Use switched-Ethernet ports instead of CSMA bridge ports | 25 to 30 % per packet; no CSMA losses at line rate | a choice in the user's script |
| – | BMv2 `-O3`, `--disable-elogger` | none | – |

Expand Down
259 changes: 176 additions & 83 deletions model/p4-core-v1model.cc

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions model/p4-core-v1model.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "ns3/switched-ethernet-channel.h"

#include <bm/bm_sim/counters.h>
#include <bm/bm_sim/phv.h>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -204,6 +206,88 @@ class P4CoreV1model : public P4SwitchCore
/// end the simulation if one is missing
void ResolveSchedulerFields();

/// A field of the PHV, found by its name once per program: the index of its
/// header in the PHV and its offset in the header. Every PHV of a program
/// has the same headers, so these find the field in any of them without
/// the string hash and map lookup of PHV::get_field(name).
struct PhvField
{
int header{-1}; ///< the header's index in the PHV; -1: the program has no such field
int offset{0}; ///< the field's offset in its header

/// @return the program has the field
bool Exists() const
{
return header >= 0;
}

/// @param phv a PHV of the program
/// @return the field in \p phv
bm::Field& In(bm::PHV* phv) const
{
return phv->get_field(static_cast<bm::header_id_t>(header), offset);
}
};

/**
* @brief What the core uses of the loaded program on every packet, found by
* name once per program (Program()): its parser, pipelines and deparser, and
* the standard, intrinsic and queueing metadata fields and the scheduler's
* fields. A program swap makes the core find them again.
*/
struct ProgramObjects
{
bool found{false}; ///< found for the loaded program
size_t headers{0}; ///< the headers of the program's PHV (a check)
bm::Parser* parser{nullptr}; ///< "parser"
bm::Pipeline* ingress{nullptr}; ///< "ingress"
bm::Pipeline* egress{nullptr}; ///< "egress"
bm::Deparser* deparser{nullptr}; ///< "deparser"
PhvField ingressPort; ///< standard_metadata.ingress_port
PhvField packetLength; ///< standard_metadata.packet_length
PhvField instanceType; ///< standard_metadata.instance_type
PhvField egressSpec; ///< standard_metadata.egress_spec
PhvField egressPort; ///< standard_metadata.egress_port
PhvField parserError; ///< standard_metadata.parser_error
PhvField checksumError; ///< standard_metadata.checksum_error
PhvField ingressTimestamp; ///< intrinsic_metadata.ingress_global_timestamp
PhvField egressTimestamp; ///< intrinsic_metadata.egress_global_timestamp
PhvField mcastGrp; ///< intrinsic_metadata.mcast_grp
PhvField priority; ///< intrinsic_metadata.priority
PhvField egressRid; ///< intrinsic_metadata.egress_rid
PhvField lagHash; ///< intrinsic_metadata.lag_hash
PhvField enqTimestamp; ///< queueing_metadata.enq_timestamp
PhvField enqQdepth; ///< queueing_metadata.enq_qdepth
PhvField deqTimedelta; ///< queueing_metadata.deq_timedelta
PhvField deqQdepth; ///< queueing_metadata.deq_qdepth
PhvField qid; ///< queueing_metadata.qid
std::vector<PhvField> inputFields; ///< the scheduler's input fields (m_inputFields)
std::vector<PhvField> outputFields; ///< the scheduler's output fields (m_outputFields)
};

ProgramObjects m_program; ///< found for the loaded program, once a packet came

/**
* @param phv a PHV of the loaded program
* @return the program's objects, found with \p phv if they are not yet
*/
const ProgramObjects& Program(bm::PHV* phv);

/**
* @brief Find a field by name in a PHV
* @param phv a PHV of the program
* @param name the field's name, also an alias (intrinsic_metadata.*); empty: none
* @return its header and offset; header -1 if the program has no such field
*/
static PhvField FindField(bm::PHV* phv, const std::string& name);

/**
* @param field a field of the program
* @param name its name, for the message
* @return \p field; ends the simulation if the program has no such field
*/
PhvField Required(PhvField field, std::string_view name) const;

/**
* @brief Put \p packet into the input buffer and let the ingress pipeline
* take it as SwitchRate allows (see P4SwitchCore::StartIngress()).
Expand Down
4 changes: 2 additions & 2 deletions test/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ No environment variable is needed, and suites can run in parallel.

## Suites

24 suites, 264 test cases. The first seven do not need BMv2.
24 suites, 265 test cases. The first seven do not need BMv2.

| Suite | Checks |
|---|---|
Expand All @@ -27,7 +27,7 @@ No environment variable is needed, and suites can run in parallel.
| `p4-queue` | egress queue: capacity, rate, FIFO, per-port rate, priorities |
| `p4-traffic-manager` | the VOQ + fabric traffic manager |
| `p4sim` | v1model, PSA and PNA load their program; Thrift ports; `BmLogLevel` and its default, `off`; errors in a flow table are printed with file and line; a switch runs commands again during the simulation; the deprecated `ChannelType` and `EnableTracing` are still accepted |
| `p4-packet` | frames sent into a switch and the frames that leave it, on v1model, PNA, PSA and the pipeline core; a frame from a host on a switched-Ethernet port enters on that port; the PSA and PNA externs `Counter`, `Meter`, `Hash` and `InternetChecksum`, and `Random`, which p4c compiles into v1model's `random()`; `random()` follows ns-3's seed and run, with an engine per switch; the pipeline core warns about the multicast, clones, resubmit and recirculation that it ignores |
| `p4-packet` | frames sent into a switch and the frames that leave it, on v1model, PNA, PSA and the pipeline core; a frame from a host on a switched-Ethernet port enters on that port; the PSA and PNA externs `Counter`, `Meter`, `Hash` and `InternetChecksum`, and `Random`, which p4c compiles into v1model's `random()`; `random()` follows ns-3's seed and run, with an engine per switch; v1model gives the program the ingress and egress timestamps and the parser error (`PacketTooShort`, `NoError`); the pipeline core warns about the multicast, clones, resubmit and recirculation that it ignores |
| `p4-externs` | a meter extern of a PSA or PNA program counts simulation time (rates set by `psa_switch_CLI` or `P4RuntimeApi`); `P4RuntimeApi` reads, writes and resets a PSA counter extern and sets, reads and unsets meter rates, by index and for the whole array |
| `p4-controller` | `P4Controller`, read back from the switch: flow entries on v1model, PSA, PNA and the pipeline core, action profiles and a meter array on v1model (the rates read through `P4Controller` too), mirroring sessions on v1model and PSA; `P4RuntimeApi` with a null output argument fails; the mirroring, queue and time commands of `simple_switch_CLI` and `psa_switch_CLI`; `StartThriftServer` with a flow table loaded in the process; the Thrift port of a destroyed switch answers with an error |
| `p4-flow-table-loader` | the in-process flow-table loader against the runtime CLI: a flow table goes into two switches, one each way, and everything its commands can change (entries with key, priority, timeout, action and data, direct counters and meters, default entries, multicast groups, nodes and LAGs, mirroring sessions, queue depths and rates, registers, counters, meters) and the errors printed must be the same. [`p4src/flow_table/flowtable_v1model.txt`](p4src/flow_table/flowtable_v1model.txt) holds every command, match kind (exact, lpm, ternary, optional, range) and value format, and the CLI's errors; also a `valid` key (in a program the test generates), PSA, its externs, PNA and the pipeline core; a file with a command that only the CLI runs, or a value that ends the CLI, goes to the CLI as a whole |
Expand Down
3 changes: 3 additions & 0 deletions test/p4-packet-test-suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,9 @@ static class P4PacketTestSuite : public TestSuite
P4SIM_TEST_QUICK);
AddTestCase(new StfTestCase("bounds", "v1model", P4SWITCH_ARCH_V1MODEL, "truncate.stf"),
P4SIM_TEST_QUICK);
// The timestamps and the parser error that the switch writes for the program
AddTestCase(new StfTestCase("metadata", "v1model", P4SWITCH_ARCH_V1MODEL, "timestamps.stf"),
P4SIM_TEST_QUICK);
// The pipeline core warns about the requests it ignores (EtherTypes as in
// the .stf files of these programs)
const std::vector<std::tuple<std::string, uint16_t, std::string, uint32_t>> warnings = {
Expand Down
Loading