Register manipulation

Hello,

I’ve some questions regarding registers in P4 that I’d appreciate if someone could clarify:

  • When I use read in a register am I taking out the stored value or does the value remain stored?
  • If a value is stored within a register position, does writing in that position replace the previous content?
  • Is there any way to fully erase/reset the content stored within a register?

Thanks

A P4 register is in some ways better named an “array”, if that makes it more familiar to you. It is an array of values, all with the same type.

A read operation retrieves the value stored in the array at one index, without changing any values stored in the array.

A write operation modifies the value stored in the array at one index, without changing any values stored at other indices.

There is no single operation that changes all array elements.

2 Likes

Understood, thanks for the help.

I guess for completeness I should say “there is no single operation that can be invoked from the data plane, i.e. from inside of your P4 program, that changes all array elements”.

Some P4 implementations provide a control plane API operation that allows the control plane to assign the same value to all array elements.

Whether they can do this while making that change appear to happen “between two consecutive packets processed”, or whether that change might appear to data packet flowing through as one at a time spread out over time, is implementation-specific.

1 Like

I’ll be looking into that, thanks once again.

@andyfingerhut thanks for making this clear. I have a follow-up question on how to read and write to registers. I’m working on a Project for my academics on a load balancer in P4 and I want to maintain some values like flags, counters, number of available servers, etc. I want to store values, read from them, and write to them in my ingress, and egress blocks. Can you provide a “signature” or “syntax” on how I can read/ write to registers?

PS: I’m new to P4 and couldn’t exactly find answers on the P4 specification or maybe I didn’t look closely.

The syntax for register read/write is somewhat different depending upon what target device you are writing a P4 program for, or more precisely, depending upon what P4 architecture that target implements.

Here is an example P4 program that reads/writes registers, and runs on the Bmv2 simple_switch software switch, written using the v1model architecture it implements: p4-guide/ptf-tests/registeraccess/registeraccess.p4 at master · jafingerhut/p4-guide · GitHub

In that file, this is the line that defines the instance of a register:

    register<SeqNum_t>(NUM_SEQ_NUMS) seq_num_reg;

This is a line of code that reads one element from the register:

seq_num_reg.read(read_data, (bit<32>) idx);

and this is a line of code in that program that writes one element to the register:

seq_num_reg.write((bit<32>) idx, write_data);

Clearly the program is doing lots of stuff besides merely reading/writing that register.

If you are looking to program a different target device, then please clarify which device(s) you are targeting.

Thank you so much.

Currently, I’m writing the program on a Bmv2 switch and the syntax you provided helps. But if I want to use a hardware switch, for ex-Tofino, is there a syntax I can follow?

I can point you at some public example programs, but there are restrictions on how to do it that might not be clear from the examples. Right now probably the quickest way to learn those restrictions is a training course from https://www.p4ica.com/ that covers that topic (I am not sure exactly which one, but you can contact them if you are interested).

Example 1: GitHub - p4lang/p4app-switchML: Switch ML Application

Example 2: open-p4studio/pkgsrc/p4-examples/p4_16_programs/tna_register at main · p4lang/open-p4studio · GitHub

Thank you! I will check this out.

@andyfingerhut I want to initialize my register to one of the servers or an ID assigned to the server as the value in the register. In my case, I want to read and write values to it based on some conditions later on. I’m confused about whether the initialization can happen through the data plane or the control plane. After going through some open issues online, and referring to your p4-guide/demo6 at master · jafingerhut/p4-guide · GitHub READMe.md files, I’m not sure if the switch CLI will work in my case.

Could you help me with that?

For more context- I’m building a load balancer with map reduce model using P4. I want to have Weighted Round Robin logic for multiple mappers and want to store the weights, mapper_id, in registers. I want to initialize the first mapper and also read their weights and manipulate the weights based on the selected mapper.

Thanks in advance!

If you are writing a controller program that uses the P4Runtime API to control table entries in your P4 program, then unfortunately the open source P4Runtime server in BMv2 does not support reading and writing of register values via the P4Runtime API: Implement support for P4Runtime API register read/write for BMv2 · Issue #11 · p4lang/project-ideas · GitHub

If you are writing a controller that uses BMv2’s custom Thrift API, which is what the simple_switch_CLI command line utility uses to communicate with BMv2, then it is possible to read and write register contents from your controller. However, I have no example controller programs to point you to that do this, and given the workaround below, I would not recommend you try to take this approach.

Given the P4Runtime API’s lack of working register access mentioned above, there is a workaround I have used that is effective: using the PacketIn/PacketOut capability from the controller to create and send packets to the switch, and modify your P4 program to recognize and process those packets in a way that does nothing except read or write one register entry, and for a read, puts the read result into a packet header field and sends the packet back to the controller.

An example program that uses this last approach can be found here, using the P4Runtime API from the controller (written in Python) to use PacketIn/PacketOut messages: p4-guide/ptf-tests/registeraccess at master · jafingerhut/p4-guide · GitHub

1 Like