Skip to content

Giving MASK in table keys #5654

Description

@maheswari-s

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?

Activity

  1. jafingerhut commented on Jun 15, 2026

    @jafingerhut
    Contributor

    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?

  2. vgurevich commented on Jun 15, 2026

    @vgurevich

    Dear @maheswari-s ,

    To achieve what you want you need to use the ternary match 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 named type and type_mask) that you will be able to fill in as needed.

    What you have written is totally different: hdr.mac.type & 0 is 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.

  3. added
    questionThis is a topic requesting clarification.
    on Jun 17, 2026
  4. jafingerhut commented on Jun 19, 2026

    @jafingerhut
    Contributor

    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 @name annotation with the original expression.

    What is it that you are hoping would happen instead for such a P4 program?

    issue5654.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionThis is a topic requesting clarification.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions