fork(1) download
  1. # your code goes here
  2. ….# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://w...content-available-to-author-only...e.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. # implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16.  
  17. from ryu.base import app_manager
  18. from ryu.controller import ofp_event
  19. from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
  20. from ryu.controller.handler import set_ev_cls
  21. from ryu.ofproto import ofproto_v1_3
  22. from ryu.lib.packet import packet
  23. from ryu.lib.packet import ethernet
  24. from ryu.lib.packet import ether_types
  25.  
  26.  
  27. class SimpleSwitch13(app_manager.RyuApp):
  28. OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
  29.  
  30. def __init__(self, *args, **kwargs):
  31. super(SimpleSwitch13, self).__init__(*args, **kwargs)
  32. self.mac_to_port = {}
  33.  
  34. @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
  35. def switch_features_handler(self, ev):
  36. datapath = ev.msg.datapath #ev.msg 是用來儲存對應事件的 OpenFlow 訊息類別實體
  37. ofproto = datapath.ofproto
  38. parser = datapath.ofproto_parser
  39. #print(ofproto)
  40. #print(parser)
  41. # install table-miss flow entry
  42. #
  43. # We specify NO BUFFER to max_len of the output action due to
  44. # OVS bug. At this moment, if we specify a lesser number, e.g.,
  45. # 128, OVS will send Packet-In with invalid buffer_id and
  46. # truncated packet data. In that case, we cannot output packets
  47. # correctly. The bug has been fixed in OVS v2.1.0.
  48.  
  49.  
  50. match = parser.OFPMatch()
  51. #print(match)
  52. actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
  53. ofproto.OFPCML_NO_BUFFER)]
  54. #print(actions)
  55.  
  56.  
  57. self.add_flow(datapath, 0, match, actions)
  58.  
  59. #meter examples
  60. #add meters,m_id meter_id,unit of rate:Kbps
  61. #self.meter_table ={"00:00:00:00:00:01":1,"00:00:00:00:00:02":1}
  62. #for m_id in range(1,2):$
  63. #m_id = 1
  64. #bands = [parser.OFPMeterBandDrop(type_=ofproto.OFPMBT_DROP, len_=0,rate=50, burst_size=10)]
  65. #self.add_meter(datapath,bands,m_id)
  66.  
  67.  
  68. # add rule for metering in s1
  69. if ev.msg.datapath.id == 1:
  70. datapath = ev.msg.datapath
  71. ofproto = datapath.ofproto
  72. parser = datapath.ofproto_parser
  73.  
  74. #seed_file = open("./cntr.txt", "r")
  75. #seed_num = seed_file.read()
  76. #print("seed num:",seed_num)
  77. #seed_num.split()
  78. input_file_name = "./input_5_seed1.txt"
  79. input_file = open(input_file_name, "r")
  80. print("input file: ", input_file_name)
  81. content = input_file.readlines()
  82.  
  83. for i in content:
  84. line = i.split()
  85. flow_id_buf = int(line[0])
  86. flow_rate_buf = (int)(int(line[1]) * 1.1)
  87. print('id: ', flow_id_buf, ' d(f): ', flow_rate_buf)
  88.  
  89. bands = [parser.OFPMeterBandDrop(type_ = ofproto.OFPMBT_DROP, len_ = 0,rate= flow_rate_buf, burst_size = 5)]
  90. req = parser.OFPMeterMod(datapath = datapath, command = ofproto.OFPMC_ADD, flags = ofproto.OFPMF_KBPS, meter_id = flow_id_buf + 1 , bands = bands)
  91. #set up a controller,send for switch request
  92. print("meter id :",bands)
  93. datapath.send_msg(req)
  94.  
  95. ip_src = "10.0.0." + str((flow_id_buf + 1) % 256)
  96. ip_dst = "10.0.0." + str((flow_id_buf + 1 + 5) % 256)
  97. #print(ip_dst)
  98. match = parser.OFPMatch(in_port = flow_id_buf + 1 , eth_type = 0x0800, ipv4_src = ip_src, ipv4_dst = ip_dst)
  99. actions = [parser.OFPActionOutput(121)]
  100. inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), parser.OFPInstructionMeter(flow_id_buf + 1, ofproto.OFPIT_METER)]
  101. mod = datapath.ofproto_parser.OFPFlowMod(
  102. datapath=datapath, match=match, cookie=0,
  103. command=ofproto.OFPFC_ADD, idle_timeout=0,
  104. hard_timeout=0, priority=3, instructions=inst)
  105.  
  106. #t_start = time.time()
  107.  
  108. datapath.send_msg(mod)
  109.  
  110. #def add_meter(self,datapath,bands,meter_id):
  111. #ofproto=datapath.ofproto
  112. #parser =datapath.ofproto_parser
  113. #print(meter)
  114. #req = parser.OFPMeterMod(datapath=datapath,command=ofproto.OFPMC_ADD,flags=ofproto.OFPMF_KBPS,meter_id=meter_id,bands=bands)
  115. #datapath.send_msg(req)
  116.  
  117.  
  118. def add_flow(self, datapath, priority, match, actions, buffer_id=None):
  119. ofproto = datapath.ofproto
  120. parser = datapath.ofproto_parser
  121.  
  122. inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
  123. actions)]
  124.  
  125. if buffer_id:
  126. mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
  127. priority=priority, match=match,
  128. instructions=inst)
  129. else:
  130. mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
  131. match=match, instructions=inst)
  132.  
  133. datapath.send_msg(mod)
  134.  
  135. @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  136. def _packet_in_handler(self, ev):
  137. # If you hit this you might want to increase
  138. # the "miss_send_length" of your switch
  139. if ev.msg.msg_len < ev.msg.total_len:
  140. self.logger.debug("packet truncated: only %s of %s bytes",
  141. ev.msg.msg_len, ev.msg.total_len)
  142. msg = ev.msg
  143. datapath = msg.datapath
  144. ofproto = datapath.ofproto
  145. parser = datapath.ofproto_parser
  146. in_port = msg.match['in_port']
  147. pkt = packet.Packet(msg.data)
  148. eth = pkt.get_protocols(ethernet.ethernet)[0]
  149. #if eth.ethertype == ether_types.ETH_TYPE_LLDP:
  150. # ignore lldp packet
  151. # return
  152. dst = eth.dst
  153. src = eth.src
  154. dpid = format(datapath.id, "d").zfill(4)
  155. self.mac_to_port.setdefault(dpid, {})
  156. #print("......................................")
  157. #print("Add switch",dpid,"to mac_to_port table")
  158. #print("......................................")
  159.  
  160. self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
  161. # learn a mac address to avoid FLOOD next time
  162. print(self.mac_to_port[dpid])
  163.  
  164. self.mac_to_port[dpid][src] = in_port
  165. #dpid=switch id,src=host mac
  166.  
  167. if dst in self.mac_to_port[dpid]:
  168. out_port = self.mac_to_port[dpid][dst]
  169. ##add flow with meter id
  170. # when traffic generated frm src x.x.x.x. will be applied to a meter_id
  171. #meter_id =self.meter_table[src]
  172. else:
  173. out_port = ofproto. OFPP_FLOOD
  174.  
  175. actions = [parser.OFPActionOutput(out_port)]
  176.  
  177.  
  178. # install a flow to avoid packet_in next time
  179. if out_port != ofproto.OFPP_FLOOD:
  180. match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
  181. # verify if we have a valid buffer_id, if yes avoid to send both
  182. # flow_mod & packet_out
  183. if msg.buffer_id != ofproto.OFP_NO_BUFFER:
  184. self.add_flow(datapath, 1, match, actions,msg.buffer_id)
  185. return
  186. else:
  187. self.add_flow(datapath, 1, match, actions)
  188. data = None
  189. if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  190. data = msg.data
  191.  
  192. out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
  193. in_port=in_port, actions=actions, data=data)
  194. datapath.send_msg(out)
  195.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.7/py_compile.py", line 143, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./prog.py", line 2
    ….# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
    ^
SyntaxError: invalid character in identifier

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.7/py_compile.py", line 147, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 2
    ….# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
    ^
SyntaxError: invalid character in identifier

stdout
Standard output is empty