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', ethernet(dst='ff:ff:ff:ff:ff:ff',ethertype=2054,src='00:00:00:00:00:01'))
('arp', arp(dst_ip='10.0.0.2',dst_mac='00:00:00:00:00:00',hlen=6,hwtype=1,opcode=1,plen=4,proto=2048,src_ip='10.0.0.1',src_mac='00:00:00:00:00:01'))

If we add in the rest of the switch code, you can see the rest of the ARP response from h2 and the subsequent successful pings from h1 to h2:

EVENT ofp_event->SimpleSwitch13 EventOFPPacketIn
msg.data: array('B', [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 8, 6, 0, 1, 8, 0, 6, 4, 0, 2, 0, 0, 0, 0, 0, 2, 10, 0, 0, 2, 0, 0, 0, 0, 0, 1, 10, 0, 0, 1])
('ethernet', ethernet(dst='00:00:00:00:00:01',ethertype=2054,src='00:00:00:00:00:02'))
('arp', arp(dst_ip='10.0.0.1',dst_mac='00:00:00:00:00:01',hlen=6,hwtype=1,opcode=2,plen=4,proto=2048,src_ip='10.0.0.2',src_mac='00:00:00:00:00:02'))
('ethernet', ethernet(dst='00:00:00:00:00:02',ethertype=2048,src='00:00:00:00:00:01'))
('ipv4', ipv4(csum=56624,dst='10.0.0.2',flags=2,header_length=5,identification=18806,offset=0,option=None,proto=1,src='10.0.0.1',tos=0,total_length=84,ttl=64,version=4))
('icmp', icmp(code=0,csum=21806,data=echo(data=array('B', [30, 216, 230, 88, 0, 0, 0, 0, 125, 173, 9, 0, 0, 0, 0, 0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]),id=22559,seq=1),type=8))

If we were to restart Mininet (or simply delete the installed flows), we could test out an HTTP flow with a Python HTTP server from the standard library. Simply start the HTTP server on h2, and use curl to get the webpage. By default, the Python HTTP server listens on port 8000:

mininet> h2 python -m SimpleHTTPServer &
mininet> h1 curl http://10.0.0.2:8000

The observed result is as expected:

('ipv4', ipv4(csum=48411,dst='10.0.0.2',flags=2,header_length=5,identification=27038,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=60,ttl=64,version=4))
('tcp', tcp(ack=0,bits=2,csum=19098,dst_port=8000,offset=10,option=[TCPOptionMaximumSegmentSize(kind=2,length=4,max_seg_size=1460), TCPOptionSACKPermitted(kind=4,length=2), TCPOptionTimestamps(kind=8,length=10,ts_ecr=0,ts_val=8596478), TCPOptionNoOperation(kind=1,length=1), TCPOptionWindowScale(kind=3,length=3,shift_cnt=9)],seq=1316962912,src_port=39600,urgent=0,window_size=29200))

You could further decode the packet, for example, the IPv4 source and destination addresses:

@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)
if p.protocol_name == 'ipv4':
print('IP: src {} dst {}'.format(p.src, p.dst))

You will see the print-out of the HTTP curl from h1 to h2 as follows:

('ethernet', ethernet(dst='00:00:00:00:00:02',ethertype=2048,src='00:00:00:00:00:01'))('ipv4', ipv4(csum=56064,dst='10.0.0.2',flags=2,header_length=5,identification=19385,offset=0,option=None,proto=6,src='10.0.0.1',tos=0,total_length=60,ttl=64,version=4))
IP: src 10.0.0.1 dst 10.0.0.2

By looking closely at how the Ryu framework decodes/encodes OpenFlow messages and events, we are closer to building our own applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.8