~ubuntu-branches/ubuntu/wily/ryu/wily-proposed

« back to all changes in this revision

Viewing changes to ryu/tests/mininet/l2/mpls/test_mpls.py

  • Committer: Package Import Robot
  • Author(s): Dariusz Dwornikowski
  • Date: 2014-08-18 16:58:52 UTC
  • Revision ID: package-import@ubuntu.com-20140818165852-i0qck3g5mw7rtxt0
Tags: upstream-3.12
ImportĀ upstreamĀ versionĀ 3.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
 
2
#
 
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
 
6
#
 
7
#    http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
import logging
 
17
import struct
 
18
 
 
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
 
27
 
 
28
 
 
29
LOG = logging.getLogger(__name__)
 
30
 
 
31
 
 
32
class RunTestMininet(app_manager.RyuApp):
 
33
 
 
34
    _CONTEXTS = {'dpset': dpset.DPSet}
 
35
    OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
 
36
 
 
37
    def __init__(self, *args, **kwargs):
 
38
        super(RunTestMininet, self).__init__(*args, **kwargs)
 
39
 
 
40
    def _add_flow(self, dp, match, actions):
 
41
        inst = [dp.ofproto_parser.OFPInstructionActions(
 
42
            dp.ofproto.OFPIT_APPLY_ACTIONS, actions)]
 
43
 
 
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)
 
50
 
 
51
        dp.send_msg(mod)
 
52
 
 
53
    def _define_flow(self, dp):
 
54
        in_port = 1
 
55
        out_port = 2
 
56
 
 
57
        eth_IP = ether.ETH_TYPE_IP
 
58
        eth_MPLS = ether.ETH_TYPE_MPLS
 
59
 
 
60
        # MPLS(80) -> PopMPLS
 
61
        LOG.debug("--- add_flow PopMPLS")
 
62
        m_label = 80
 
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)
 
70
 
 
71
        # IP -> PushMPLS(90)
 
72
        LOG.debug("--- add_flow PushMPLS")
 
73
        s_label = 90
 
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)
 
83
 
 
84
        # MPLS(100) -> PushMPLS(200)
 
85
        LOG.debug("--- add_flow PushMPLS")
 
86
        m_label = 100
 
87
        s_label = 200
 
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)
 
98
 
 
99
        # MPLS(1000):MPLS -> PopMPLS
 
100
        # LOG.debug("--- add_flow PopMPLS")
 
101
        # SKIP: ovs not supported
 
102
        m_label = 1000
 
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)
 
110
 
 
111
    @set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
 
112
    def handler_datapath(self, ev):
 
113
        if ev.enter:
 
114
            self._define_flow(ev.dp)
 
115
 
 
116
    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
 
117
    def packet_in_handler(self, ev):
 
118
        msg = ev.msg
 
119
        dst, src, eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
 
120
        in_port = msg.match.fields[0].value
 
121
 
 
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))