Register use in V1model Architecture

Dear Community,

I have used a program to write a value in a register. Here is a sample code -
// Declaring a register which is 32 bit size with 0-1023 indexed.
register<bit<32>>(1024) last_seq;
// bit<32> seq_store;
seq_store= hdr.tcp.seqNo;
// Storing the value of seq_store in the register last_seq at index 0.
last_seq.write(0, seq_store);
Presently, I used it in ingress processing stage in v1model (Bmv2) architecture. Can I use register in parser stage of v1model (Bmv2) architecture?

  1. Does p4 parser stage support register use?
  2. If not, is there any other way (like, global variable) I can store values during a packet processing and use the value later while I will handle another packet? Meta variable is not capable of doing this.

Thanks in advance.

Dear @aashiqkamal ,

v1model architecture does not support registers (or any other externs with persistent state that are modifiable by the data plane) in the parser. Neither do other architectures I am familiar with.

Why do you need that? Are you sure you cannot do the same kind of processing in the control? On most targets it is preferable to have fast, efficient parsers. Their main job is to indentify the headers that a given packet has and deal with the checksums. The rest of processing is (usually) better done in the control.

Happy hacking,
Vladimir

1 Like

Dear @vgurevich ,

Thank you so much for your answer. Presently my parser code handles/changes tcp payload and header fields as per my requirements. It made my task easier :slight_smile: As parser does not support the use of register, I will rewrite the code based on your suggestion to achieve my goal.

I am wondering is there any other way to store values during a packet parsing and use the value later while I will handle another packet at parser stage like, a global variable?

Thanks in advance.

Hello @aashiqkamal,

I am wondering is there any other way to store values during a packet parsing and use the value later while I will handle another packet at parser stage like, a global variable?

Of course. Just add a field to the metadata struct, assign it in the parser and then use it in the control.

Happy hacking,
Vladimir

Dear @vgurevich ,
Thank you for your reply. I have already tried the metadata struct. But, according to my understanding, metadata variables are initialized to zero at the beginning of each packet processing cycle. So, if I want to retain the value of metadata struct across multiple packets, I need a alternative approach.
Thanks in advance.

With Vladimir’s approach (if I understood it correctly), you would store information during parsing in a metadata field, later in the control block you would store that value in a register, and other packets can then retrieve that value after parsing in the control block.

Afaik (in the architectures that I know) it is not possible to retrieve stored state within the parser. It’s only possible after parsing.

1 Like

@aashiqkamal ,

Let us get to establish some basic facts and terminology here, otherwise it’s easy to get confused and confuse others.

First, in P4_16 both headers and metadata (including both the local variables as well as the ones instantiated by the architecture) are transient data, meaning that they are re-initialized for each packet and thus cannot be used to pass information from one packet to another. I am specifically pointing this out, since your message seems to be stating something different and that is not correct.

Second, let’s define what a global object is. I suggest that it is the one that is instantiated at the top level. In most P4 architectures, there are very few things that can be meaningfully instantiated at the top level by the programmer. However, some are instantiated implicitly, by the architecture itself. The most important are the header and the metadata structures, that are then passed into the parser, the control and the deparser as parameters. They are still transient (some people say “stateless”, although that’s not quite correct), but they allow you to pass the information from the parser to the control and to the deparser.

Last, but not least, there are 3 types of persistent objects defined in P4_16 (they are also called “stateful”), that can keep the state between packets. These are: tables, parser value sets and externs (although not all externs are requested to keep their state between packets and some do not. They are merely allowed to).

So, if you need to pass the information from the parser to the control, you need to use an architecture-instantiated metadata struct. If you need to store the data between packets you would need to use an extern (typically a register). Because P4 is a language, you should be able to combine both mechanisms easily to achieve the desired effect. Which is what @SteffenLindner correctly pointed out.

Happy hacking,
Vladimir

2 Likes

Thank you so much. I am currently following this approach to use registers. Previously, my logic was solely applied in the parser stage. That’s why I was searching any other way. :slight_smile:

Thank you so much for your explanation.