# Packet\_in method implementation using P4Runtime

**URL:** https://forum.p4.org/t/packet-in-method-implementation-using-p4runtime/216
**Category:** Getting Started with P4
**Created:** [January 4, 2022, 5:35pm UTC](https://forum.p4.org/t/packet-in-method-implementation-using-p4runtime/216 "2022-01-04T17:35:39Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ppavlidis](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@ppavlidis](https://forum.p4.org/u/ppavlidis)
#### Post date: [January 4, 2022, 5:35pm UTC](https://forum.p4.org/t/packet-in-method-implementation-using-p4runtime/216/1 "2022-01-04T17:35:39Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

### Author: ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)
#### Post date: [January 4, 2022, 5:55pm UTC](https://forum.p4.org/t/packet-in-method-implementation-using-p4runtime/216/2 "2022-01-04T17:55:01Z")

</div>

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.

> **[p4-guide/ptf-tests/packetinout at master · jafingerhut/p4-guide](https://github.com/jafingerhut/p4-guide/tree/master/ptf-tests/packetinout)**
>
> master/ptf-tests/packetinout

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.
