Packet_in method implementation using P4Runtime

I am trying to implement a packet_in method in order to receive a packet from the switch to the controller, according to p4runtime.proto file.
My code does not work and i have no idea what is going wrong.
In the beginning, i thought that i had to send a StreamMessageRequest of packet out message to get a StreamMessageResponse of packet_in message, but as i can see they work independent.
My method written in python into the switch.py file in p4runtime library looks like this:

def packet_in(self, **kwargs):
  print("Before loop")
  for response in self.stream_msg_resp:
    print("I am into it")
    if response.HasField("packet"):
        print("Packet is set")
    else:
        print(response.WhichOneof("update"))
    return(response)

When i call it from my_controller.py program, it blocks after “Before loop” print message.
I suspect that switch, when it has to send a packet to the cpu_port, it does not create a StreamMessageResponse of packet_in message in order to send it through grpc StreamChannel that is created when a connection between the stub and the server is established.

Am i doing anything or thinking it wrong? Can anybody help me?

I have a working PTF test written in Python, using the P4Runtime API, and a corresponding P4 program, such that there are times when the controller sends a PacketOut message to the switch (which it always received on a dedicated CPU port, not a regular switch port. In those tests the CPU port is numbered 510, but this is configurable on the simple_switch_grpc command line).

It also contains other test cases where the switch sends a message to the dedicated CPU port, and those become PacketIn messages to the controller.

It uses some Python functions in the directory testlib that are common to some other PTF tests I have written, which are important to read and understand, too. You can focus on only the functions in testlib that are actually called by the PTF test and ignore the rest, since not all of them are needed by this PTF test.

1 Like