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?”
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:
- It can convert an external packet representation (usually a byte stream) into a set of parsed headers and metadata using a standardized
packet_inextern and its methods
- It can manipulate headers and metadata using assignments, standard arithmetic and logical operations as well as standard methods defined on headers, like
- It can perform match-action operations using tables
- It can further convert some of the headers or metadata into output packets in their external representation using the standardized
packet_outextern and its methods
- It can call methods of the architecture-specific externs (or call architecture specific extern functions)
- 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.