Repository navigation
v1model core: find PHV fields and P4 objects once per program - #129
Merged
Merged
Conversation
The v1model core looked up about thirty PHV fields and four P4 objects by
name on every packet (phv->get_field("standard_metadata.egress_spec"),
get_pipeline("ingress"), ...), each a string hash and a map lookup. It now
finds them once per program, from the first packet's PHV: the parser,
pipelines and deparser, and for each field the index of its header in the
PHV and its offset there (every PHV of a program has the same headers), read
with PHV::get_field(header, offset). A program swap, which happens only when
the switch holds no packet, makes it find them again.
- ProgramObjects, PhvField, Program(), FindField() (also for aliases such as
intrinsic_metadata.*), Required() in P4CoreV1model; the standard fields
that simple_switch requires stop the simulation with a message if a
program lacks them (BMv2 threw std::out_of_range before).
- All per-packet paths: ReceivePacket, the ingress, EnqueueNow, the egress,
clones, recirculation, resubmission, multicast, the VOQ path, and the
queue scheduler's input and output fields.
- Same traffic, CPU time of the traffic (medians, taking turns with main):
one switch 6.6 % faster, fat trees k=4 12 % and k=8 14 %.
- Test (suite p4-packet, p4src/metadata/timestamps.stf): the ingress and
egress timestamps and the parser error reach the program. No test read
them before, so mutants that dropped these writes passed.
- Docs: performance.md, TODO.md, test/Readme.md (261 cases).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mingyumaz
added a commit
that referenced
this pull request
Dec 2, 2026
Conflicts: - model/p4-core-v1model.cc, EnqueueNow(): keeps #129's fields resolved once per program and adds this branch's meta.bytes. - test/Readme.md: 268 cases (265 on main + 3 of this branch). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
doc/performance.mdlists this as recommendation 5: the v1model core looks up the PHV fields and P4 objects it needs by name on every packet. A packet that crosses a switch costs about thirty calls such asphv->get_field("standard_metadata.egress_spec")orphv->has_field("intrinsic_metadata.mcast_grp"), plusget_parser("parser"),get_pipeline("ingress"),get_pipeline("egress")andget_deparser("deparser"). Each is a string hash and anunordered_maplookup, and in a large network the maps of many switches no longer fit in the caches. The profile put this at 3 to 4 % of the run time; measured after the change, the gain is 6.6 % for one switch and 12 to 14 % in fat trees, with the same traffic.How it works
Once per program, not per packet. All PHVs of a program come from the same
PHVFactory, so they have the same headers in the same order and every field sits at the same place. The core therefore finds each field once and keeps where it is: the index of its header in the PHV and the field's offset in that header (PhvField). On every packet it reads the field withPHV::get_field(header, offset), a vector index instead of a string hash. The parser, the two pipelines and the deparser are kept as pointers in the same struct (ProgramObjects).When it finds them.
Program(phv)fills the struct from the first packet's PHV and then returns it as long as the program stays. A program swap clears it inProgramSwapped(). P4sim swaps only when the switch holds no packet (TrySwap()), so every PHV after the swap is the new program's, and the next packet fills the struct again. The scheduler's input and output fields (ResolveSchedulerFields(), which runs at the start and after a swap, beforeProgram()) are found the same way.Aliases. Some names are aliases:
intrinsic_metadata.ingress_global_timestampandqueueing_metadata.enq_timestampare fields ofstandard_metadataunder another name.FindField()therefore gets the field by its name once and then looks for the header that holds it, instead of splitting the name.Missing fields. Fields that the old code checked with
has_field()stay optional (PhvField::Exists()). The standard fields that simple_switch requires of every v1model program (ingress_port,packet_length,instance_type,egress_spec,egress_port) are checked when the struct is filled: a program without one stops the simulation with a message, where BMv2 threwstd::out_of_rangeon the first packet before.egress_ridis checked where multicast uses it, the queueing fields when the program has queueing metadata.Review guide
1.
model/p4-core-v1model.hPhvFieldExists(),In(phv)ProgramObjectsfoundm_program,Program(phv),FindField(phv, name),Required(field, name)2.
model/p4-core-v1model.ccProgram(),FindField(),Required()(new, afterFindMetadataField()).ProgramSwapped()clears the cache.Program(phv):ReceivePacket,HandleIngressPipeline(also the ingress clone and resubmission),EnqueueNow,EventDrivenEgressDequeue(also egress clones and recirculation),MulticastPacket,CopyFieldList, and the VOQ path (EnqueueToTrafficManager,TmTransmit). The code keeps its order and its branches; only the lookups change. No lookup by name is left in the file.3. Test (
test/p4src/metadata/, suitep4-packet): a v1model program copiesingress_global_timestamp,egress_global_timestampand whetherparser_errorisPacketTooShortorNoErrorinto the frame;timestamps.stfsends a frame that is too short for the program's last header and one that is not. No test read these fields before: the mutation run found that dropping their writes passed every test.4. Docs:
performance.md(the measurement, recommendation 5 done),TODO.md(done for v1model; the PSA, PNA and pipeline cores still look up by name),test/Readme.md(261 cases).Measured
CPU time of the traffic phase of
p4-benchmark(ns-3.39, default build, Docker, nothing else running), main and this branch taking turns, median of 5 (one switch) or 3 runs. The traffic is the same: the same packets sent, received and forwarded, the same events and simulated time.--load=0.1 --packets=5000--load=0.1 --packets=500Scope
TODO.md).run-tests.shpasses 24 suites with 261 test cases and 47 example runs on both versions. Every example prints the same as on main except its wall-clock times, with the same exit code and output files. Mutation testing: 12 mutants of the new code (a wrong header index or offset, a cache kept after a swap, the priority, the multicast group, the clone type, a scheduler field at the wrong index,enq_qdepth, and the timestamps and the parser error left unwritten); all are caught now, three of them only by the newtimestamps.stfcase. An independent review of the diff found no bugs.perfin the image), so the gain is in CPU time only.Notes for reviewers
Program()(what is required, what is optional) and that the cache is cleared on every program change (ProgramSwapped()).Two reviews are needed before merging.
🤖 Generated with Claude Code