Storing payload byte in a temporary variable

Hi,
I am developing P4 code to do some operation on packet payload. For example, If I have a table like below, I am trying to find a way to store the packet byte in a temporary variable and do some operation on it. Is there a possibility to achieve such kind of operation on P4 targets.

table example {
key = {
hdr.app.byte: exact; // Is there a way to store this byte in any other variable?
}
actions = {
function;
}
}

Any help is appreciated. Thanks in Advance !

The answer depends upon when you want to be able to read the value of that packet byte back and use it later.

If you want to be able to use it later while processing the same packet, then you can simply declare a local variable with the same type as hdr.app.byte, and assign its value to that local variable, e.g. something like this, assuming that the type of hdr.app.byte is bit<8>:

bit<8> saved_byte;
action save_byte() {
    saved_byte = hdr.app.byte;
}
table example {
    key = { hdr.app.byte: exact; }
    actions = { save_byte; /* any other desired actions for the table go here */ }
}

If you want to be able to use the value of the packet byte while processing a different packet that arrives later, then storing in a local variable will not achieve that, because the value of all variables declared inside the control is not saved when the processing of the current packet is complete. You can use an extern like a P4 register array to store a copy of the header byte, and while processing a later packet, read the value of that byte from the P4 register array and then use it.

1 Like