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