Control plane PacketIn and PacketOut

Hello guys,

I am new in P4 and i am a bit confused about the data plane and control plane communication using P4Runtime. I think that i understand how to import rules to the target using P4Runtime API but i can’t understand how can i establish a packetIn and packetOut to and from controller like in Openflow.

Can someone help me with this?

Thanks a lot!

PacketIn and PacketOut messages are exchanged using the bi-directional stream between client and server: p4runtime/p4runtime.proto at 627f28726bcdf06dcedd2df71da0ab42e2e589a2 · p4lang/p4runtime · GitHub

If you are not familiar with gRPC bi-directional stream channels, you may want to read up on them first. How to open the the stream from the client and how to write to the stream will depend on the programming language of choice for your client.

For information about the stream messages and the expected sequence of messages (the client needs to establish itself as leader before being able to do packet-IO), I recommend reading section 16 of the P4Runtime spec: P4Runtime Specification

Finally there are a few examples out there on how to write a P4Runtime client:


I would only like to add that I remember learning the Packet In/Out procedure and the corresponding Python-based code from the P4-researching repository. I think you have plenty of sources to learn the same and they probably have the same type of code, I just try to link the one I found.

If you need a grab-and-go solution, I think the has packet in/out examples going on but you also need to consider the helper libraries as those implement the functions (here) and so on. What I did back in time is to modify the Antonin’s linked P4 tutorial Python code and incorporate the most significant changes from Kevin Bird and other author’s P4-researching repository (mostly helper and controller files I think).

And, by the way, if you need an example with ONOS instead of the Python-based controller, you can find an excellent tutorial from the ONF. I think the tutorial is pretty well guided and the Packet in/out functions used with OpenFlow switches are pretty similar (if not the same) to the ones used with P4 switches. The only difference is that you need to incorporate the pipeconf (among other things), which is explained in the tutorial.

Hope it helps.


What i can’t figure out is the following,in order to implement a controller using P4Runtime API, should i use the scapy library for creating and handling PacketIn and PacketOut messages?


The scapy library can be interesting to parse packets or use some interesting functions. For instance, in ONOS you first get an Ethernet-type of object on a packet in event, and then you cast the payload to other types of header like IPv4/IPv6 first (to name an example) and then TCP or UDP and so on. But everything is pretty well integrated in the framework.

In the Python example (for a controller that is not ONOS, of course), I guess they use Scapy because it is a “fast way” to achieve the same type of thing. You can create an “Ethernet packet” variable and then extract the information you need from it.

You can also generate a new packet too and then send it as a packet out to the switch.

Please ask any doubts you have,

Cheers and Happy New Year

1 Like

Thank you all for your answers!

I wish a Happy New Year!!

Hello guys and Happy New Year,

To clarify this, in p4runtime.proto file packet I/O is manipulated through streamChannel.
For implementing packet in operation to the controller,which is a stream response message,should i send a stream request first with packet out message? And if so, what will be the data of a packet out message when i just want to receive a packet in response?

PacketOut and PacketIn messages are pretty much independent of each other. For example, a P4 program could be written to receive and process PacketOut messages from a controller, e.g. by making a few changes to some header fields, then sending that packet out of a non-CPU port determined by some metadata field in the controller header, or by some P4 table lookup. In such a program, there might be thousands of PacketOut messages from controller to switch with no PacketIn messages from switch to controller.

Similarly there could be times with thousands of PacketIn messages, with no PacketOut messages.

You could write a P4 program where it responds to every PacketOut packet by sending one PacketIn message back to the controller, or 10 PacketIn messages in response to every PacketOut message. Or a variable number from 0 up to 8 that cannot be easily determined by the controller in advance.