Skip to content

LPM Match Fields with Bitwidth Not Divisible by 8. #146

@dorakerman

Description

@dorakerman

Hi, seen this issue with match fields with bitwidth % 8 !=0.

When setting match value using runtime shell, it seems like runtime shell expects values to be calculated aligned to full bytes, for example, for a field with bitwidth = 2 match on "0x1/2" need to be translated to "0x80/2". If not, value is seen as 0, causing 2 entries with different match keys to seem like they have the same key:

Keys 1 and 3 regarded both as zeros:

P4Runtime sh >>> te.match["MY_FIELD"] = "0x1/2"
LPM value was transformed to conform to the P4Runtime spec (trailing bits must be unset)
field_id: 1
lpm {
  value: "\000"  # Zeros
  prefix_len: 2
}


P4Runtime sh >>> te.match["MY_FIELD"] = "0x3/2"
LPM value was transformed to conform to the P4Runtime spec (trailing bits must be unset)
field_id: 1
lpm {
  value: "\000"  # Zeros
  prefix_len: 2
}

One can also avoid it by using a different prefix length, let's say instead of `"0x1/2", use "0x1/8" to be byte aligned.
In both cases, we get an error that size of value or prefix length does not fit the field info.

Looking into code, it seems that byte_mask = 0xff & ((0xff << (8 - r))) does create the mask from start of byte and without regarding the bitwidth, meaning for a field with bitwidth = 2 and prefix_len = 2 the mask will be 0b11000000.

It is debatable whether it makes more sense to have the prefix length as the real length and not completed to a byte.
So question is - is this expected? Should I try and implment a fix?

Some logs:


> /usr/local/lib/python3.12/dist-packages/p4runtime_sh/shell.py(428)_sanitize_and_convert_mf_lpm()
-> if length == 0:
(Pdb) prefix
b'\x01'
(Pdb) length
2
(Pdb) field_info
id: 1
name: "MY_FIELD"
bitwidth: 2
match_type: LPM


(Pdb) n
> /usr/local/lib/python3.12/dist-packages/p4runtime_sh/shell.py(445)_sanitize_and_convert_mf_lpm()
-> if barray[first_byte_masked] & byte_mask != barray[first_byte_masked]:
(Pdb) byte_mask
192
(Pdb) bin(192)
'0b11000000'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions