Integrating ML Model with P4 switch

Suppose I want to integrate a Machine Learning Model with my P4 switch.

Can anyone provide me some suggestions on how I should proceed with the implementation?


In the scentific literature/artictles you can find some implementation, unfortunatelly I don’t know exact names but you can look for it on google.

For example:

Hey thanks for the resources well I was thinking of using the Packet In/Out function,

But I have some doubts regarding it like does it send the whole packet to the controller or just the part defined in the standard metadata.

So actually you want to perform ML in software so maybe this is a better example GitHub - p4lang/p4app-switchML: Switch ML Application .

If you keep the packets size small should be faster parse it in software, but I think that you are going to see the difference when you have to parse/manage a lot of packets.

Is there any alternative to the way I mentioned before?

If you send it to the CPU port, the whole packet is send.

Hey so when you say whole packets suppose I am trying to get all packets from my ethernet port [real intrerface] as input to my switch so if I perform packet in/out will it also send the whole packet.

Also suppose I am sending an MQTT packet on my ethernet port generally it is send on port 1883 by default so do I need to change it to CPU port ? Will the MQTT packet contents pass through all and remain unchanged.

If you send a packet to the CPU port within your P4 data plane, the whole packet is send to the controller, just as if you would send the packet to another port.

Hey so will this code
Simple-P4runtime-Controller/simple.p4 at 292d64c2da4e6c70aa9e91fbccffac5fe4007598 · demian91/Simple-P4runtime-Controller (

send the packets to the CPU Port directly like after processing or is it sending the packets to CPU port if the packet are being to send to the said before arriving at the switch

Sending the packet to the CPU port is like sending a packet to any other port. It will still traverse your P4 pipeline.