blob: 16e3b1f4de1d890165575ab839b8c90c02e36835 [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->128
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 self.del_all_flows(datapath)
56
57 @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
58 def port_status_handler(self, ev):
59 LOG.debug('port_status_handler port id: %d', ev.msg.desc.port_no)
60
61 def add_meter(self, datapath, meter_id, rate):
62 ofproto = datapath.ofproto
63 parser = datapath.ofproto_parser
64 band = parser.OFPMeterBandDrop(burst_size=0, rate=rate)
65 mod = parser.OFPMeterMod(datapath=datapath,command=ofproto.OFPMC_ADD, flags=ofproto.OFPMF_KBPS, meter_id=meter_id, bands=[band])
66 LOG.info("add_meter: meter_id=%d, rate=%d KBPS")
67 datapath.send_msg(mod)
68
69 def add_vid_flow(self, datapath, cookie, src_port, dst_port, vid, meter_id):
70 ofproto = datapath.ofproto
71 parser = datapath.ofproto_parser
72 actions = [parser.OFPActionOutput(dst_port)]
73 if meter_id != 0:
74 inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions), parser.OFPInstructionMeter(meter_id)]
75 else:
76 inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,actions)]
77
78 # NOTE: 0x1000 is OR'd into the vid to set the OFPVID_PRESENT flag in the OpenFlow FLOW_MOD (OFPFC_ADD)
79 # message match field.
80 match = parser.OFPMatch(in_port=src_port, vlan_vid=(vid | 0x1000))
81 mod = parser.OFPFlowMod(datapath=datapath,
82 cookie=cookie,
83 cookie_mask=32767,
84 priority=0,
85 match=match,
86 instructions=inst)
87 LOG.info("add_vid_flow: src_port=%d dst_port=%d vid=%d meter=%d cookie=%d",
88 src_port, dst_port, vid, meter_id, cookie)
89 datapath.send_msg(mod)
90
91 def del_flow(self, datapath, cookie):
92 LOG.info("del_flow: cookie=%d", cookie)
93 self._del_flow(datapath, cookie, 32767)
94
95 def del_all_flows(self, datapath):
96 LOG.info("del_all_flows")
97 # Cookie 0 (argument 2) tells the OF agent to delete all configured flows
98 self._del_flow(datapath, 0, 0)
99
100 def _del_flow(self, datapath, cookie, mask):
101 ofproto = datapath.ofproto
102 parser = datapath.ofproto_parser
103
104 mod = parser.OFPFlowMod(
105 datapath=datapath, match=None, cookie=cookie, cookie_mask=mask,
106 command=ofproto.OFPFC_DELETE)
107 datapath.send_msg(mod)
108
109 def send_barrier(self, datapath):
110 LOG.info("send_barrier")
111 datapath.send_barrier()
112