Use these three magic words to ask your P4 questions like a pro

I think it would not be an exaggeration to state that at least once a week there is a new post on this forum with the topic

“How can I do X in P4?”

The latest examples are here, here, and there are many more.

Unfortunately, it is almost impossible to answer these questions in a specific way and the reason is: they are not being asked correctly.

In order to ask these questions correctly (and to properly understand P4_16 language in general), it is extremely important to remember that the language itself can do only the following things:

  1. It can convert an external packet representation (usually a byte stream) into a set of parsed headers and metadata using a standardized packet_in extern and its methods
  2. It can manipulate headers and metadata using assignments, standard arithmetic and logical operations as well as standard methods defined on headers, like .setValid() or .setInvalid()
  3. It can perform match-action operations using tables
  4. It can further convert some of the headers or metadata into output packets in their external representation using the standardized packet_out extern and its methods
  5. It can call methods of the architecture-specific externs (or call architecture specific extern functions)
  6. It can interact with the architecture-specific fixed-function components using intrinsic metadata, using the same methods as are available to the regular header and metadata fields (variables)

That’s all, folks! Note, that the language provides no standard way to even drop a packet or to send it to an output port, not to mention sending a packet to several ports (multicasting), creating independent copies of a packet (cloning/mirroring), counting bytes in a packet, enqueueing a packet into a specific queue, etc., etc. (In fact, the language does not have a concept of “port” to begin with). All these and a lot more truly “interesting” things are accomplished using architecture-specific facilities, be they either fixed-function devices and their intrinsic metadata or externs and extern functions.

Therefore, the right question to ask will be

“How can I do X in P4 (P4_16) using Y architecture?”

Only then a specific answer and truly helpful answer can be provided. This is especially true when we talk about the functionality that is simply not expressable in P4 (see the list above), but even for the things that are (typically some non-trivial calculations), using an architecture-specific extern will usually mean a difference between a program that can be compiled and the one that can not.

So, next time try asking your “How can I” question by adding these 3 extra words (you can even shorten it by using notation like P4_16/TNA), and you’ll be pleasantly surprised by much more specific and helpful answers.

Happy hacking,
Vladimir

2 Likes

Agreed. I suspect that some such questions may be asked by newcomers. Even if it is asked by experienced P4 developers, a reasonable way to interpret the question is: “is there any P4 implementation you know of that can do this, and if so, which ones and how?”

1 Like