P4 interface bandwidth calculator

@taylort79 – as with most “can I do X in P4” kind of questions, it is extremely important to mention the architecture you plan to use for your program.

For example, some P4_16 architectures (specifically TNA and T2NA) provide a special extern, called Lpf() that can calculate average data rate as long as you feed it the packet length. The output can then be stored in a register or exported using some other way.

Irrespective of that, there is a separate question of obtaining the accurate packet length. Again, a lot depends on the specific architecture and the target. For example, in many cases packet length might not be available in the ingress pipeline and has to be derived (e.g. from IPv4 total_length field or a similar one). Similarly, on many platforms the packet length in the egress match-action pipeline might not fully account for the modifications that were performed there and such an adjustment has to be done manually.

Last. but not least, if you talk about the Ethernet link utilization, you need to remember that at the Layer1 all frames are 20 bytes longer than what you typically see in a P4 data plane, since there are additional 8 bytes of the preamble and 12 bytes of the inter-frame gap.

Please, see this post if you have further questions.

Happy hacking,
Vladimir