Could not evaluate expression at compilation time

Good Morning,

I am developing a project to implement what is defined as Ordered Proof Of Transit in draft-ietf-sfc-proof-of-transit-08, at the same time I am learning p4.

My issue is with the operations that are done with modulo prime, which are necessary to calculate a Cumulative value in each hop and to verify the transit of the packets at the egress node. So when I try to compile the p4 program, it gives me the error “could not evaluate expression at compilation time”.

From what I have been reading in the p4c github, there is not any workaround for this scenario, right?

Thanks in advance,

There is no division or modulo operator that is required to be implemented as part of the P4 language specification, except those that have a constant power of 2 as the denominator, since those are equivalent to bit-slices or right shifts, and very cheap to implement in hardware.

The fast packet processing path of the cheapest-per-Gbps switches and NICs typically have no need to implement arbitrary division and modulo operations, and thus do not. That is the rationale for why these operations are not required by the P4 language specification. That could change in the future, of course, if features like Ordered Proof Of Transit become commonplace, but I believe that no features requiring these operations are commonplace yet.

Some options are:

Look for a P4 target device that does implement these operations, by asking the vendors. In particular, P4 targets that are implemented on FPGAs often open up the possibility of adding your own custom hardware operations.

In the absence of such operations, the next best approach I can imagine is to determine whether it is possible to implement such operations using a combination of existing features that are common in P4 targets, e.g. exact match and ternary table lookups, and integer addition, subtraction, and shift operations. I would guess that with large numerator and denominator values, integer modulo could be fairly expensive to implement using only those, but I haven’t given it much thought or research before. You might want to look through the table of contents of “Hacker’s Delight” Hacker's Delight - Wikipedia to see if it has any sections on modulo operations. That book has a lot of tricks that can be useful to P4 developers.

This article might give a little bit of inspiration, but note that it contains nothing on implementing modulo operations: p4-guide/ at master · jafingerhut/p4-guide · GitHub

Here is a way using the Hash extern on the v1model architecture on BMv2 simple_switch, but note that if it works, that is no guarantee that it will work on other P4 target devices. As I mentioned in my earlier reply, high speed packet processing switches and NICs often have little use for, and thus often do not implement, a general purpose modulo operation in their ASIC gates: How do I modulo int<w> or bit<w> types? · Issue #409 · p4lang/tutorials · GitHub

Thanks a lot for all the information Andy!
I think I have managed to create a code capable of doing those operation, using the “Hacker’s Delight” book. In chapter, 10 sections 10.8,10.9 and 10.10 they define how to make this operations and how to implement them into a compiler, by using what they call “Magic Number”.
Now I have deploy a scenario and test it, to see if the code is working.