Repository navigation
Giving MASK in table keys #5654
Description
Activity
So from the P4 code snippet you gave above, you are hoping that the P4Info file, and/or other control plane output file defining the API for the table, still includes the field "hdrs.mac.type" in it?
And that the current p4c control plane output files do not have "hdrs.mac.type" in it, but either no field at all, or some kind of "always 0" field, for that table?
Dear @maheswari-s ,
To achieve what you want you need to use the
ternarymatch kind:keys = { hdrs.mac.type : ternary; }That will allocate the correct resources (e.g. TCAM on the hardware) and instruct the API generator to create two table fields for
hdrs.mac.type(typically namedtypeandtype_mask) that you will be able to fill in as needed.What you have written is totally different:
hdr.mac.type & 0is always equal to 0, because&is a bitwise AND operator. Thus, no wonder the compiler optimized that out.As for the
&&&it is used to specify specific key values, whereas you needed to define your table -- this is a totally different thing.- addedquestionThis is a topic requesting clarification.This is a topic requesting clarification.
on Jun 17, 2026 So from the P4 code snippet you gave above, you are hoping that the P4Info file, and/or other control plane output file defining the API for the table, still includes the field "hdrs.mac.type" in it?
And that the current p4c control plane output files do not have "hdrs.mac.type" in it, but either no field at all, or some kind of "always 0" field, for that table?
I did a quick experiment with the attached test P4 file, and it shows that the P4Info file created by p4c does contain a separate key field for each key, even for the one that is bit-wise ANDed with 0. The compiler also auto-creates a
@nameannotation with the original expression.What is it that you are hoping would happen instead for such a P4 program?
There is requirement to take the key and mask it to given value and then go for rule matching. And the mask can be 0 also.
Example:
keys = {
hdrs.mac.type & 0x0 : exact;
// other keys
}
But, the above mask expression is optimized by frontend and 0 is present in the place of (hdrs.mac.type & 0x0) at the end of frontend passes.
I checked if Mask operator (&&&) would help. But this operation is allowed only in constant entries of a table as per P4 language spec.
Is there any other cleaner way available? Or, should we stop frontend to optimize the & with 0 in table key expression?