p4c: bmv2 "array index must be a constant" error

I am using a header stack in v1model p4-16 code. The index into the header stack array is obtained from a header in the packet and index copied to metadata. Obviously, the header is parsed in the parser. p4c-bm2-ss fails because the index is not a compile-time constant. So, how is such code supposed to work? The p4test backend does not care and compiles the code fine.

./testdata/p4_16_samples/ipv6-switch-ml-bmv2.p4(111): [--Werror=invalid] error: meta._ml_idx4: Invalid all array indexes must be constant
             hdr.vector[0].e = pool[meta.ml_idx].val;
                                    ^^^^

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 53 (53 by maintainers)

Most upvoted comments

I would imagine it would be more like:

  "value" : [ {
    "type" : "expression",
    "value" : {
      "op" : "DEREFERENCE_STACK",
      "left" : { "type" : "stack", "value" : "pool_0" },
      "right" : { "type" : "field", "value" : [ "hdr", "ml", "idx" ] } } },
    "val" ]

but I’m not familiar with the exact syntax supported by bmv2 in these json expressions.