Packet inspection
In this section, we will take a closer look at the PacketIn message and parse the packet so we can further process the event and determine the corresponding action. In chapter11_2.py, we can use the ryu.lib.packet.packet method to decode the packet:
from ryu.lib.packet import packet
import array
...
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
print("msg.data: {}".format(array.array('B', ev.msg.data)))
pkt = packet.Packet(ev.msg.data)
for p in pkt.protocols:
print(p.protocol_name, p)Note that ev.msg.data is in bytearray format, which is why use the array.array() function to print it out. We can then initiate a ping, h1 ping -c 2 h2, and observe in the output the arp from h1 to h2:
EVENT ofp_event->SimpleSwitch13 EventOFPPacketIn msg.data: array('B', [255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 1, 8, 6, 0, 1, 8, 0, 6, 4, 0, 1, 0, 0, 0, 0, 0, 1, 10, 0, 0, 1, 0, 0, 0, 0, 0, 0, 10, 0, 0, 2]) ('ethernet',...