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.lib.mac import haddr_to_str
29 LOG = logging.getLogger(__name__)
32 class RunTestMininet(app_manager.RyuApp):
34 _CONTEXTS = {'dpset': dpset.DPSet}
35 OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
37 def __init__(self, *args, **kwargs):
38 super(RunTestMininet, self).__init__(*args, **kwargs)
40 def _add_flow(self, dp, match, actions):
41 inst = [dp.ofproto_parser.OFPInstructionActions(
42 dp.ofproto.OFPIT_APPLY_ACTIONS, actions)]
44 mod = dp.ofproto_parser.OFPFlowMod(
45 dp, cookie=0, cookie_mask=0, table_id=0,
46 command=dp.ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
47 priority=0xff, buffer_id=0xffffffff,
48 out_port=dp.ofproto.OFPP_ANY, out_group=dp.ofproto.OFPG_ANY,
49 flags=0, match=match, instructions=inst)
53 def _define_flow(self, dp):
57 eth_IP = ether.ETH_TYPE_IP
58 eth_MPLS = ether.ETH_TYPE_MPLS
61 LOG.debug("--- add_flow PopMPLS")
63 match = dp.ofproto_parser.OFPMatch()
64 match.set_in_port(in_port)
65 match.set_dl_type(eth_MPLS)
66 match.set_mpls_label(m_label)
67 actions = [dp.ofproto_parser.OFPActionPopMpls(eth_IP),
68 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
69 self._add_flow(dp, match, actions)
72 LOG.debug("--- add_flow PushMPLS")
74 match = dp.ofproto_parser.OFPMatch()
75 match.set_in_port(in_port)
76 match.set_dl_type(eth_IP)
77 f = dp.ofproto_parser.OFPMatchField.make(
78 dp.ofproto.OXM_OF_MPLS_LABEL, s_label)
79 actions = [dp.ofproto_parser.OFPActionPushMpls(eth_MPLS),
80 dp.ofproto_parser.OFPActionSetField(f),
81 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
82 self._add_flow(dp, match, actions)
84 # MPLS(100) -> PushMPLS(200)
85 LOG.debug("--- add_flow PushMPLS")
88 match = dp.ofproto_parser.OFPMatch()
89 match.set_in_port(in_port)
90 match.set_dl_type(eth_MPLS)
91 match.set_mpls_label(m_label)
92 f = dp.ofproto_parser.OFPMatchField.make(
93 dp.ofproto.OXM_OF_MPLS_LABEL, s_label)
94 actions = [dp.ofproto_parser.OFPActionPushMpls(eth_MPLS),
95 dp.ofproto_parser.OFPActionSetField(f),
96 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
97 self._add_flow(dp, match, actions)
99 # MPLS(1000):MPLS -> PopMPLS
100 # LOG.debug("--- add_flow PopMPLS")
101 # SKIP: ovs not supported
103 match = dp.ofproto_parser.OFPMatch()
104 match.set_in_port(in_port)
105 match.set_dl_type(eth_MPLS)
106 match.set_mpls_label(m_label)
107 actions = [dp.ofproto_parser.OFPActionPopMpls(eth_MPLS),
108 dp.ofproto_parser.OFPActionOutput(out_port, 0)]
109 # self._add_flow(dp, match, actions)
111 @set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
112 def handler_datapath(self, ev):
114 self._define_flow(ev.dp)
116 @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
117 def packet_in_handler(self, ev):
119 dst, src, eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
120 in_port = msg.match.fields[0].value
122 LOG.info("----------------------------------------")
123 LOG.info("* PacketIn")
124 LOG.info("in_port=%d, eth_type: %s", in_port, hex(eth_type))
125 LOG.info("packet reason=%d buffer_id=%d", msg.reason, msg.buffer_id)
126 LOG.info("packet in datapath_id=%s src=%s dst=%s",
127 msg.datapath.id, haddr_to_str(src), haddr_to_str(dst))