In this lab we will connect write our first SDN application and run it on the controller.
Open up a terminal on the controller and create the following file
$ vi ~/l2.py
It should contain the following
from ryu.base import app_manager
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
class L2Switch(app_manager.RyuApp):
def __init__(self, *args, **kwargs):
super(L2Switch, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
dp = msg.datapath
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
in_port = msg.match['in_port']
actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
out = ofp_parser.OFPPacketOut(
datapath=dp, buffer_id=msg.buffer_id, in_port=in_port,
actions=actions)
dp.send_msg(out)
With one window open on your controller, and the other window on your datapath element, start this application on the controller with the following command.
$ ryu-manager --verbose ~/l2.py
Then run the following on the datapath element
# /root/bootovs-rb532.sh
Once everything has started you can confirm that the datapath element has contacted the controller
2014-01-28T04:56:02Z|00030|rconn|INFO|br0<->tcp:10.10.0.1:6633: connected
Every time a packet arrives on either of the datapath ports the following message will be printed on the controller screen
EVENT ofp_event->L2Switch EventOFPPacketIn
EVENT ofp_event->L2Switch EventOFPPacketIn
EVENT ofp_event->L2Switch EventOFPPacketIn
EVENT ofp_event->L2Switch EventOFPPacketIn
EVENT ofp_event->L2Switch EventOFPPacketIn
EVENT ofp_event->L2Switch EventOFPPacketIn
You can confirm this by having someone plug a laptop into the rb532
git clone https://code.google.com/r/dean-nsss/
ryu-manager --verbose ./dean-nsss/simple_switch_13.py
On the datapath element run the monitor_flows command
$ ./monitor_flows.sh
This should give the output similar to the following
Tue Jan 28 05:01:57 UTC 2014
OFPST_FLOW reply (OF1.3) (xid=0x2):
cookie=0x0, duration=10.535s, table=0, n_packets=27, n_bytes=4624, priority=1,in_port=1,dl_src=3c:07:54:49:ee:97 actions=goto_table:1
cookie=0x0, duration=93.21s, table=1, n_packets=3, n_bytes=1026, priority=10,dl_dst=ff:ff:ff:ff:ff:ff actions=ALL
cookie=0x0, duration=93.21s, table=1, n_packets=0, n_bytes=0, priority=5,dl_type=0x88cc actions=drop
cookie=0x0, duration=93.21s, table=1, n_packets=0, n_bytes=0, priority=5,dl_type=0x05ff actions=drop
cookie=0x0, duration=93.21s, table=1, n_packets=15, n_bytes=3058, priority=11,dl_dst=33:33:00:00:00:00/ff:ff:00:00:00:00 actions=ALL
cookie=0x0, duration=93.21s, table=1, n_packets=0, n_bytes=0, priority=10,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=ALL
cookie=0x0, duration=93.21s, table=1, n_packets=9, n_bytes=540, priority=0 actions=FLOOD,CONTROLLER:64
--End