Skip to content

Implement PI register read / write for P4Runtime RegisterEntry - #1473

Open
Lori-Ringhio97 wants to merge 4 commits into
p4lang:mainfrom
Lori-Ringhio97:p4runtime-register-entry
Open

Lori-Ringhio97 wants to merge 4 commits into
p4lang:mainfrom
Lori-Ringhio97:p4runtime-register-entry

Conversation

@Lori-Ringhio97

@Lori-Ringhio97 Lori-Ringhio97 commented Oct 29, 2026 •

Copy link
Copy Markdown

Companion to p4lang/PI#692, which adds P4Runtime RegisterEntry support to PI (fixes p4lang/PI#376).

The PI PR adds three functions to the PI target API (PI_ABI_VERSION 4): _pi_register_read, _pi_register_write and _pi_register_write_all. This PR implements them for bmv2, so that simple_switch_grpc can serve register reads and writes, and fixes a bug in signed fields found by the new tests.

Commits

  1. Implement PI register read / write for P4Runtime RegisterEntry: PI/src/pi_register_imp.cpp, on top of register_read / register_write of the bmv2 runtime interface, the same calls the Thrift bm_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 a bm::Data, so cells wider than 64 bits work too, unlike with the Thrift API (i64). bmv2 errors are returned as PI_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.
  2. Wrap values above the maximum of signed fields: when a signed field was set to a value in (max, mask], for example 0xfffc for a 16-bit field, Field::export_bytes did not wrap the value around. The bytes were correct, but the value stayed positive (65532 instead of -4). This happens when a register<int<16>> cell holding -4 is read into an int<16> variable, since register arrays store the 2's complement representation. Arithmetic shifts, comparisons and divisions then used the wrong value. The new SignedFieldTest.SetOutOfRange checks 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.
  3. Support wildcard register writes and more register types: _pi_register_write_all uses register_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 and type registers 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 new register.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:

  • writes and reads of single cells, including a bit<160> cell, and canonical bytestrings in read responses;
  • values written over P4Runtime seen by the data plane, and values written by the data plane read over P4Runtime (with a carry over bit 64 and a wrap-around);
  • int<16>: negative values, arithmetic shift, sign-extended input, wrap-around at -32768, values out of range;
  • serializable enum (including values without a name), enum and type;
  • reads of all the cells of a register, reads with register_id 0, and wildcard writes;
  • out-of-range indices.

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_grpc from 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, commit 0aa6c691). 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:

  • the steps of Dockerfile (CMake with -DENABLE_WERROR=ON, then ctest): all 53 tests pass;
  • the simple_switch_grpc tests (ctest -C run_solo -R simple_switch_grpc): 12/12 pass, including test_register;
  • autotools with the flags of test.yml (--with-pdfixed --with-pi --with-packet-trace --with-stress-tests --enable-debugger --enable-Werror), then make check in targets/simple_switch_grpc: passes (33 gtest tests, 9 of them for registers);
  • bazel.yml (buildifier, bazel build //targets/simple_switch_grpc:..., bazel test of the simple_switch and simple_switch_grpc tests), with --override_module=com_github_p4lang_pi pointing to the PI branch, i.e. as after updating the pin: all pass (Bazel only, not compiler-specific);
  • tools/cpplint.py on the new and changed files, and reuse lint: clean.

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
Lori-Ringhio97 force-pushed the p4runtime-register-entry branch from bf213b5 to 9f1228b Compare October 30, 2026 08:29
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support to read & write Register by PI

1 participant