backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / mininet / l3 / ip_ttl / test_ip_ttl.py
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
59         # ICMP -> DecNwTtl
60         LOG.debug("--- add_flow DecNwTtl")
61         match = dp.ofproto_parser.OFPMatch()
62         match.set_in_port(in_port)
63         match.set_dl_type(eth_IP)
64         actions = [dp.ofproto_parser.OFPActionDecNwTtl(),
65                    dp.ofproto_parser.OFPActionOutput(out_port, 0)]
66         self._add_flow(dp, match, actions)
67
68     @set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
69     def handler_datapath(self, ev):
70         if ev.enter:
71             self._define_flow(ev.dp)
72
73     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
74     def packet_in_handler(self, ev):
75         msg = ev.msg
76         dst, src, eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
77         in_port = msg.match.fields[0].value
78
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))