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.lib.mac import haddr_to_str
28 LOG = logging.getLogger(__name__)
31 class RunTestMininet(app_manager.RyuApp):
33 _CONTEXTS = {'dpset': dpset.DPSet}
34 OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
36 def __init__(self, *args, **kwargs):
37 super(RunTestMininet, self).__init__(*args, **kwargs)
39 def _add_flow(self, dp, match, actions):
40 inst = [dp.ofproto_parser.OFPInstructionActions(
41 dp.ofproto.OFPIT_APPLY_ACTIONS, actions)]
43 mod = dp.ofproto_parser.OFPFlowMod(
44 dp, cookie=0, cookie_mask=0, table_id=0,
45 command=dp.ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
46 priority=0xff, buffer_id=0xffffffff,
47 out_port=dp.ofproto.OFPP_ANY, out_group=dp.ofproto.OFPG_ANY,
48 flags=0, match=match, instructions=inst)
52 def _define_flow(self, dp):
57 match = dp.ofproto_parser.OFPMatch()
58 match.set_in_port(in_port)
59 actions = [dp.ofproto_parser.OFPActionOutput(out_port, 0)]
60 self._add_flow(dp, match, actions)
63 match = dp.ofproto_parser.OFPMatch()
64 match.set_in_port(out_port)
65 actions = [dp.ofproto_parser.OFPActionOutput(in_port, 0)]
66 self._add_flow(dp, match, actions)
68 @set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
69 def handler_datapath(self, ev):
71 self._define_flow(ev.dp)
73 @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
74 def packet_in_handler(self, ev):
76 dst, src, eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
77 in_port = msg.match.fields[0].value
79 LOG.info("----------------------------------------")
80 LOG.info("* PacketIn")
81 LOG.info("in_port=%d, eth_type: %s", in_port, hex(eth_type))
82 LOG.info("packet reason=%d buffer_id=%d", msg.reason, msg.buffer_id)
83 LOG.info("packet in datapath_id=%s src=%s dst=%s",
84 msg.datapath.id, haddr_to_str(src), haddr_to_str(dst))