1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from ryu.base import app_manager
20 from ryu.controller import ofp_event
21 from ryu.controller import dpset
22 from ryu.controller.handler import MAIN_DISPATCHER
23 from ryu.controller.handler import set_ev_cls
24 from ryu.ofproto import ofproto_v1_2
25 from ryu.ofproto import ether
26 from ryu.ofproto import inet
27 from ryu.lib.mac import haddr_to_str
30 LOG = logging.getLogger(__name__)
33 class RunTestMininet(app_manager.RyuApp):
35 _CONTEXTS = {'dpset': dpset.DPSet}
36 OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
38 def __init__(self, *args, **kwargs):
39 super(RunTestMininet, self).__init__(*args, **kwargs)
41 def _add_flow(self, dp, match, actions):
42 inst = [dp.ofproto_parser.OFPInstructionActions(
43 dp.ofproto.OFPIT_APPLY_ACTIONS, actions)]
45 mod = dp.ofproto_parser.OFPFlowMod(
46 dp, cookie=0, cookie_mask=0, table_id=0,
47 command=dp.ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
48 priority=0xff, buffer_id=0xffffffff,
49 out_port=dp.ofproto.OFPP_ANY, out_group=dp.ofproto.OFPG_ANY,
50 flags=0, match=match, instructions=inst)
54 def _define_flow(self, dp):
58 eth_IP = ether.ETH_TYPE_IP
59 eth_VLAN = ether.ETH_TYPE_8021Q
60 ip_ICMP = inet.IPPROTO_ICMP
63 LOG.debug("--- add_flow VLAN(8) to PopVLAN")
65 match = dp.ofproto_parser.OFPMatch()
66 match.set_in_port(in_port)
67 match.set_dl_type(eth_IP)
68 match.set_vlan_vid(m_vid)
69 actions = [dp.ofproto_parser.OFPActionPopVlan(),
70 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
71 self._add_flow(dp, match, actions)
74 LOG.debug("--- add_flow ICMP to PushVLAN(9)")
76 match = dp.ofproto_parser.OFPMatch()
77 match.set_in_port(in_port)
78 match.set_dl_type(eth_IP)
79 match.set_ip_proto(ip_ICMP)
80 f = dp.ofproto_parser.OFPMatchField.make(
81 dp.ofproto.OXM_OF_VLAN_VID, s_vid)
82 actions = [dp.ofproto_parser.OFPActionPushVlan(eth_VLAN),
83 dp.ofproto_parser.OFPActionSetField(f),
84 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
85 self._add_flow(dp, match, actions)
87 # VLAN(10) -> PushVLAN(20)
88 # LOG.debug("--- add_flow VLAN(10) to PushVLAN(100)")
89 # SKIP: ovs not supported
92 match = dp.ofproto_parser.OFPMatch()
93 match.set_in_port(in_port)
94 match.set_dl_type(eth_IP)
95 match.set_vlan_vid(m_vid)
96 f = dp.ofproto_parser.OFPMatchField.make(
97 dp.ofproto.OXM_OF_VLAN_VID, s_vid)
98 actions = [dp.ofproto_parser.OFPActionPushVlan(eth_VLAN),
99 dp.ofproto_parser.OFPActionSetField(f),
100 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
101 # self._add_flow(dp, match, actions)
103 # VLAN(100):VLAN -> PopVLAN
104 LOG.debug("--- add_flow VLAN(100):VLAN to PopVLAN")
106 match = dp.ofproto_parser.OFPMatch()
107 match.set_in_port(in_port)
108 match.set_dl_type(eth_VLAN)
109 match.set_vlan_vid(m_vid)
110 actions = [dp.ofproto_parser.OFPActionPopVlan(),
111 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
112 self._add_flow(dp, match, actions)
114 @set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
115 def handler_datapath(self, ev):
117 self._define_flow(ev.dp)
119 @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
120 def packet_in_handler(self, ev):
122 dst, src, eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
123 in_port = msg.match.fields[0].value
125 LOG.info("----------------------------------------")
126 LOG.info("* PacketIn")
127 LOG.info("in_port=%d, eth_type: %s", in_port, hex(eth_type))
128 LOG.info("packet reason=%d buffer_id=%d", msg.reason, msg.buffer_id)
129 LOG.info("packet in datapath_id=%s src=%s dst=%s",
130 msg.datapath.id, haddr_to_str(src), haddr_to_str(dst))