summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-07-19 14:59:58 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-07-19 14:59:58 +0900
commitb5d462f6154c6027d91bce37263d153e29a0856d (patch)
treeb16a14b15a8543d6437f7d3784add2326630a595
parente3e703970d47b0b433a7b1b55146227ca2371533 (diff)
simple_switch: remove outside module dependence
simple_switch is a a learing material (who uses this in production?). It should be the simple learning switch code like one in OpenFlow tutorial. It's easier for learniners if simple_switch doesn't use the outside modules. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/app/simple_switch.py51
1 files changed, 28 insertions, 23 deletions
diff --git a/ryu/app/simple_switch.py b/ryu/app/simple_switch.py
index 7f0d6cdb..571fceb0 100644
--- a/ryu/app/simple_switch.py
+++ b/ryu/app/simple_switch.py
@@ -37,13 +37,28 @@ LOG = logging.getLogger('ryu.app.simple_switch')
class SimpleSwitch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
- _CONTEXTS = {
- 'mac2port': mac_to_port.MacToPortTable,
- }
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
- self.mac2port = kwargs['mac2port']
+ self.mac_to_port = {}
+
+ def add_flow(self, datapath, in_port, dst, actions):
+ ofproto = datapath.ofproto
+
+ wildcards = ofproto_v1_0.OFPFW_ALL
+ wildcards &= ~ofproto_v1_0.OFPFW_IN_PORT
+ wildcards &= ~ofproto_v1_0.OFPFW_DL_DST
+
+ match = datapath.ofproto_parser.OFPMatch(
+ wildcards, in_port, 0, dst,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0)
+
+ mod = datapath.ofproto_parser.OFPFlowMod(
+ datapath=datapath, match=match, cookie=0,
+ command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
+ priority=ofproto.OFP_DEFAULT_PRIORITY,
+ flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
+ datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
@@ -54,34 +69,24 @@ class SimpleSwitch(app_manager.RyuApp):
dst, src, _eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
dpid = datapath.id
- self.mac2port.dpid_add(dpid)
+ self.mac_to_port.setdefault(dpid, {})
+
LOG.info("packet in %s %s %s %s",
dpid, haddr_to_str(src), haddr_to_str(dst), msg.in_port)
- self.mac2port.port_add(dpid, msg.in_port, src)
- out_port = self.mac2port.port_get(dpid, dst)
+ # learn a mac address to avoid FLOOD next time.
+ self.mac_to_port[dpid][src] = msg.in_port
- if out_port == None:
- LOG.info("out_port not found")
+ if dst in self.mac_to_port[dpid]:
+ out_port = self.mac_to_port[dpid][dst]
+ else:
out_port = ofproto.OFPP_FLOOD
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
+ # install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
- wildcards = ofproto_v1_0.OFPFW_ALL
- wildcards &= ~ofproto_v1_0.OFPFW_IN_PORT
- wildcards &= ~ofproto_v1_0.OFPFW_DL_DST
-
- match = datapath.ofproto_parser.OFPMatch(
- wildcards, msg.in_port, 0, dst,
- 0, 0, 0, 0, 0, 0, 0, 0, 0)
-
- mod = datapath.ofproto_parser.OFPFlowMod(
- datapath=datapath, match=match, cookie=0,
- command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
- priority=ofproto.OFP_DEFAULT_PRIORITY,
- flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
- datapath.send_msg(mod)
+ self.add_flow(datapath, msg.in_port, dst, actions)
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,