blob: 377903ff46182f849ce25544711c16b05fd61454 [file] [log] [blame]
Shad Ansari2f7f9be2017-06-07 13:34:53 -07001# Copyright (C) 2011 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.
15import logging
16
17from ryu.base import app_manager
18from time import sleep
19from ryu.controller import ofp_event
20from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
21from ryu.controller.handler import set_ev_cls
22from ryu.ofproto import ofproto_v1_3
23from ryu.lib.packet import packet
24from ryu.lib.packet import ethernet
25from ryu.lib import addrconv
26
27LOG = logging.getLogger('ryu.app.pon_topology')
28
29class PonTopology13(app_manager.RyuApp):
30 OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
31
32 def __init__(self, *args, **kwargs):
33 super(PonTopology13, self).__init__(*args, **kwargs)
34 self.mac_to_port = {}
35
36 @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
37 def switch_features_handler(self, ev):
38 datapath = ev.msg.datapath
39
40 # This does nothing if flows have not been previously configured in the OF agent
41 self.del_all_flows(datapath)
42
43 # Configure the flows (VID 100)
44 #
45 # Ports 1-128 are implicitly mapped to UNI-0 on ONU 1-12
46 # Ports 129 and above are implicitly mapped to OLT NNI-0 and above
47 self.add_vid_flow(datapath,10000, 1, 129, 100, 0)
48 self.add_vid_flow(datapath,10001, 129, 1, 100, 0)
49
50 sleep(1)
51 self.send_barrier(datapath)
52
53 sleep(5)
54
55 # Delete the downstream flow
56 self.del_flow(datapath, 10001)
57 # Then delete the upstream flow
58 self.del_flow(datapath, 10000)
59
60 @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
61 def port_status_handler(self, ev):
62 LOG.debug('port_status_handler port id: %d', ev.msg.desc.port_no)
63
64 def add_meter(self, datapath, meter_id, rate):
65 ofproto = datapath.ofproto
66 parser = datapath.ofproto_parser
67 band = parser.OFPMeterBandDrop(burst_size=0, rate=rate)
68 mod = parser.OFPMeterMod(datapath=datapath,
69 command=ofproto.OFPMC_ADD,
70 flags=ofproto.OFPMF_KBPS,
71 meter_id=meter_id,
72 bands=[band])
73 LOG.info("add_meter: meter_id=%d, rate=%d KBPS")
74 datapath.send_msg(mod)
75
76 def add_vid_flow(self, datapath, cookie, src_port, dst_port, vid, meter_id):
77 ofproto = datapath.ofproto
78 parser = datapath.ofproto_parser
79 actions = [parser.OFPActionOutput(dst_port)]
80 if meter_id != 0:
81 inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions),
82 parser.OFPInstructionMeter(meter_id)]
83 else:
84 inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)]
85
86 # NOTE: 0x1000 is OR'd into the vid to set the OFPVID_PRESENT flag in the OpenFlow FLOW_MOD (OFPFC_ADD)
87 # message match field.
88 match = parser.OFPMatch(in_port=src_port, vlan_vid=(vid | 0x1000))
89 mod = parser.OFPFlowMod(datapath=datapath,
90 cookie=cookie,
91 cookie_mask=32767,
92 priority=0,
93 match=match,
94 instructions=inst)
95 LOG.info("add_vid_flow: src_port=%d dst_port=%d vid=%d meter=%d cookie=%d",
96 src_port, dst_port, vid, meter_id, cookie)
97 datapath.send_msg(mod)
98
99 def del_flow(self, datapath, cookie):
100 LOG.info("del_flow: cookie=%d", cookie)
101 self._del_flow(datapath, cookie, 32767)
102
103 def del_all_flows(self, datapath):
104 LOG.info("del_all_flows")
105
106 # Cookie 0 (argument 2) tells the OF agent to delete all configured flows
107 self._del_flow(datapath, 0, 0)
108
109 def _del_flow(self, datapath, cookie, mask):
110 ofproto = datapath.ofproto
111 parser = datapath.ofproto_parser
112
113 mod = parser.OFPFlowMod(datapath=datapath,
114 match=None,
115 cookie=cookie,
116 cookie_mask=mask,
117 command=ofproto.OFPFC_DELETE)
118 datapath.send_msg(mod)
119
120 def send_barrier(self, datapath):
121 LOG.info("send_barrier")
122 datapath.send_barrier()
123