Repository navigation
Implement PI register read / write for P4Runtime RegisterEntry - #1473
Open
Lori-Ringhio97 wants to merge 4 commits into
Open
Lori-Ringhio97 wants to merge 4 commits into
Lori-Ringhio97 wants to merge 4 commits into
Conversation
Implement _pi_register_read and _pi_register_write, which PI adds to its target API to support P4Runtime RegisterEntry reads and writes (p4lang/PI#376). They use register_read / register_write of the bmv2 runtime interface. PI passes a cell as a byte array in network byte order, of the width of the register, and bmv2 stores it in a bm::Data, so registers wider than 64 bits are supported. A value which does not fit in the bitwidth of the P4Info, because the P4Info and the bmv2 JSON disagree, is returned as an error. Add simple_switch_grpc tests for register reads and writes, including values written by the data plane. This requires the PI version that adds pi_register_imp.h (PI_ABI_VERSION 4). Signed-off-by: Lori-Ringhio97 <lorenzo.rinieri97@libero.it>
When a signed field was set to a value in (max, mask], e.g. 0xfffc for a 16-bit field, export_bytes did not wrap the value around: the bytes of the field were correct, but its value stayed positive (65532 instead of -4). This happens when an unsigned value is copied to a signed field without an expression, e.g. when a register<int<16>> cell holding -4 is read into an int<16> variable, since register arrays store the 2's complement representation of the value. Subsequent arithmetic shifts, comparisons or divisions then used the wrong value. The value is now wrapped around for any value outside of [min, max]. The new SetOutOfRange test checks the value and the bytes of signed fields of 2 to 16 bits for values which are 2^bitwidth above or below the range. Signed-off-by: Lori-Ringhio97 <lorenzo.rinieri97@libero.it>
Companion of the PI changes which add wildcard RegisterEntry writes and registers of type int<W>, serializable enum, enum and user-defined types: * _pi_register_write_all writes the same value to all the cells with register_write_range, which holds the lock of the register array (also taken by the actions accessing it), so the write is atomic with respect to the data plane. * The other types need no change in the target, since PI exchanges their cells as bitstrings of the bitwidth of the register array. register.p4 gains an int<16>, a serializable enum, an enum and a user-defined type register, for which the data plane writes a value computed from cell 0 to cell 1 (and cell 2 for int<16>). The new tests check that the data plane and P4Runtime agree on the values (negative values, arithmetic shift, wrap around, enum members and values without a name) and that wildcard writes set every cell. Signed-off-by: Lori-Ringhio97 <lorenzo.rinieri97@libero.it>
Lori-Ringhio97
force-pushed
the
p4runtime-register-entry
branch
from
October 30, 2026 08:29
bf213b5 to
9f1228b
Compare
PI calls the target functions for every register whose type is supported, i.e. whose bitwidth is not 0, not only for bitstring types. Signed-off-by: Lori-Ringhio97 <lorenzo.rinieri97@libero.it>
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.
Companion to p4lang/PI#692, which adds P4Runtime
RegisterEntrysupport to PI (fixes p4lang/PI#376).The PI PR adds three functions to the PI target API (
PI_ABI_VERSION4):_pi_register_read,_pi_register_writeand_pi_register_write_all. This PR implements them for bmv2, so thatsimple_switch_grpccan serve register reads and writes, and fixes a bug in signed fields found by the new tests.Commits
PI/src/pi_register_imp.cpp, on top ofregister_read/register_writeof the bmv2 runtime interface, the same calls the Thriftbm_register_*handlers use. PI passes a cell as a byte array in network byte order, of the width of the register. bmv2 stores it in abm::Data, so cells wider than 64 bits work too, unlike with the Thrift API (i64). bmv2 errors are returned asPI_STATUS_TARGET_ERROR + code, as for counters. A value that does not fit in the bitwidth of the P4Info, because the P4Info and the bmv2 JSON disagree, is returned as an error instead of overflowing the buffer.Field::export_bytesdid not wrap the value around. The bytes were correct, but the value stayed positive (65532 instead of -4). This happens when aregister<int<16>>cell holding -4 is read into anint<16>variable, since register arrays store the 2's complement representation. Arithmetic shifts, comparisons and divisions then used the wrong value. The newSignedFieldTest.SetOutOfRangechecks the value and the bytes of signed fields of 2 to 16 bits; all of its cases fail without the fix. This commit is independent of the rest and can go in a separate PR if you prefer._pi_register_write_allusesregister_write_range, which holds the lock of the register array (also taken by the actions that access it), so the write is atomic with respect to the data plane, as P4Runtime requires: an action sees either the old value or the new one in all the cells.int<W>, enum, serializable enum andtyperegisters need no change in the target, since PI exchanges their cells as bitstrings of the bitwidth of the register array.Tests
targets/simple_switch_grpc/tests/test_register.cpp, with a newregister.p4(compiled with p4c 1.2.5.18). The data plane writes a value computed from cell 0 to cell 1 of each register, so the tests check that P4Runtime and the data plane agree:bit<160>cell, and canonical bytestrings in read responses;int<16>: negative values, arithmetic shift, sign-extended input, wrap-around at -32768, values out of range;type;register_id0, and wildcard writes;Merge order
This PR needs the PI one: bmv2 CI installs PI main, so the build fails here until the PI PR is merged. The other way around, once the PI PR is merged,
simple_switch_grpcfrom bmv2 main does not link until this PR is merged, so the two should go in close together.The Bazel build pins PI in
MODULE.bazel(git_override, commit0aa6c691). I will update the pin to the PI merge commit once there is one.Testing
Run locally against the PI branch of p4lang/PI#692, with gcc and clang:
Dockerfile(CMake with-DENABLE_WERROR=ON, thenctest): all 53 tests pass;simple_switch_grpctests (ctest -C run_solo -R simple_switch_grpc): 12/12 pass, includingtest_register;test.yml(--with-pdfixed --with-pi --with-packet-trace --with-stress-tests --enable-debugger --enable-Werror), thenmake checkintargets/simple_switch_grpc: passes (33 gtest tests, 9 of them for registers);bazel.yml(buildifier,bazel build //targets/simple_switch_grpc:...,bazel testof thesimple_switchandsimple_switch_grpctests), with--override_module=com_github_p4lang_pipointing to the PI branch, i.e. as after updating the pin: all pass (Bazel only, not compiler-specific);tools/cpplint.pyon the new and changed files, andreuse lint: clean.