Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. def __init__(self, *args, **kwargs):
  2. super(SimpleSwitch, self).__init__(*args, **kwargs)
  3. self.mac_to_port = {}
  4. s_list = get_all_switch(app_manager)
  5.  
  6. loading app ryu/app/simple_switch.py
  7. loading app ryu.controller.ofp_handler
  8. instantiating app ryu.controller.ofp_handler of OFPHandler
  9. instantiating app ryu/app/simple_switch.py of SimpleSwitch
  10. Traceback (most recent call last):
  11. File "/usr/local/bin/ryu-manager", line 9, in <module>
  12. load_entry_point('ryu==3.8', 'console_scripts', 'ryu-manager')()
  13. File "/usr/local/lib/python2.7/dist-packages/ryu/cmd/manager.py", line 73, in main
  14. services.extend(app_mgr.instantiate_apps(**contexts))
  15. File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 434, in instantiate_apps
  16. self._instantiate(app_name, cls, *args, **kwargs)
  17. File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 420, in _instantiate
  18. app = cls(*args, **kwargs)
  19. File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 35, in __init__
  20. s_list = get_all_switch(app_manager)
  21. NameError: global name 'get_all_switch' is not defined
  22.  
  23. import logging
  24. import struct
  25.  
  26. from ryu.base import app_manager
  27. from ryu.controller import mac_to_port
  28. from ryu.controller import ofp_event
  29. from ryu.controller.handler import MAIN_DISPATCHER
  30. from ryu.controller.handler import set_ev_cls
  31. from ryu.ofproto import ofproto_v1_0
  32. from ryu.lib.mac import haddr_to_bin
  33. from ryu.lib.packet import packet
  34. from ryu.lib.packet import ethernet
  35.  
  36. from ryu.base import app_manager
  37. from ryu.controller import mac_to_port
  38. from ryu.controller import ofp_event
  39. from ryu.controller.handler import MAIN_DISPATCHER
  40. from ryu.controller.handler import set_ev_cls
  41. from ryu.ofproto import ofproto_v1_0
  42. from ryu.lib.mac import haddr_to_bin
  43. from ryu.lib.packet import packet
  44. from ryu.lib.packet import ethernet
  45. import ryu.topology.api
  46.  
  47.  
  48. def __init__(self, *args, **kwargs):
  49. super(SimpleSwitch, self).__init__(*args, **kwargs)
  50. self.mac_to_port = {}
  51. s_list = ryu.topology.api.get_all_switch(app_manager.RyuApp)
  52.  
  53. loading app ryu/app/simple_switch.py
  54. loading app ryu.topology.switches
  55. loading app ryu.controller.ofp_handler
  56. loading app ryu.controller.ofp_handler
  57. instantiating app ryu.topology.switches of Switches
  58. instantiating app ryu.controller.ofp_handler of OFPHandler
  59. instantiating app ryu/app/simple_switch.py of SimpleSwitch
  60. Traceback (most recent call last):
  61. File "/usr/local/bin/ryu-manager", line 9, in <module>
  62. load_entry_point('ryu==3.8', 'console_scripts', 'ryu-manager')()
  63. File "/usr/local/lib/python2.7/dist-packages/ryu/cmd/manager.py", line 73, in main
  64. services.extend(app_mgr.instantiate_apps(**contexts))
  65. File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 434, in instantiate_apps
  66. self._instantiate(app_name, cls, *args, **kwargs)
  67. File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 420, in _instantiate
  68. app = cls(*args, **kwargs)
  69. File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 36, in __init__
  70. s_list = ryu.topology.api.get_all_switch(app_manager.RyuApp)
  71. File "/usr/local/lib/python2.7/dist-packages/ryu/topology/api.py", line 26, in get_all_switch
  72. return get_switch(app)
  73. File "/usr/local/lib/python2.7/dist-packages/ryu/topology/api.py", line 21, in get_switch
  74. rep = app.send_request(event.EventSwitchRequest(dpid))
  75. TypeError: unbound method send_request() must be called with RyuApp instance as first argument (got EventSwitchRequest instance instead)
  76.  
  77. s_list = ryu.topology.api.get_all_switch(self)
  78.  
  79. from ryu.base import app_manager
  80. from ryu.controller import ofp_event
  81. from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
  82. from ryu.controller.handler import set_ev_cls
  83. from ryu.ofproto import ofproto_v1_3
  84. from ryu.lib.packet import packet
  85. from ryu.lib.packet import ethernet
  86.  
  87. from ryu.topology import event
  88. from ryu.topology.api import get_all_switch, get_all_link
  89.  
  90.  
  91. class SimpleSwitch13(app_manager.RyuApp):
  92. OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
  93.  
  94. def __init__(self, *args, **kwargs):
  95. super(SimpleSwitch13, self).__init__(*args, **kwargs)
  96. self.mac_to_port = {}
  97.  
  98. @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
  99. def switch_features_handler(self, ev):
  100. self.logger.info("Received EventOFPSwitchFeatures")
  101. msg = ev.msg
  102. self.logger.info('OFPSwitchFeatures received: '
  103. 'ntdatapath_id=0x%016x n_buffers=%d '
  104. 'ntn_tables=%d auxiliary_id=%d '
  105. 'ntcapabilities=0x%08x',
  106. msg.datapath_id, msg.n_buffers, msg.n_tables,
  107. msg.auxiliary_id, msg.capabilities)
  108.  
  109. datapath = ev.msg.datapath
  110. ofproto = datapath.ofproto
  111. parser = datapath.ofproto_parser
  112. match = parser.OFPMatch()
  113. actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
  114. ofproto.OFPCML_NO_BUFFER)]
  115. self.add_flow(datapath, 0, match, actions)
  116.  
  117. def add_flow(self, datapath, priority, match, actions, buffer_id=None):
  118. ofproto = datapath.ofproto
  119. parser = datapath.ofproto_parser
  120.  
  121. inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
  122. actions)]
  123. if buffer_id:
  124. mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
  125. priority=priority, match=match,
  126. instructions=inst)
  127. else:
  128. mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
  129. match=match, instructions=inst)
  130. datapath.send_msg(mod)
  131.  
  132. @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
  133. def _packet_in_handler(self, ev):
  134. #self.logger.info("Received EventOFPPacketIn")
  135. # If you hit this you might want to increase
  136. # the "miss_send_length" of your switch
  137. if ev.msg.msg_len < ev.msg.total_len:
  138. self.logger.debug("packet truncated: only %s of %s bytes",
  139. ev.msg.msg_len, ev.msg.total_len)
  140. msg = ev.msg
  141. datapath = msg.datapath
  142. ofproto = datapath.ofproto
  143. parser = datapath.ofproto_parser
  144. in_port = msg.match['in_port']
  145.  
  146. pkt = packet.Packet(msg.data)
  147. eth = pkt.get_protocols(ethernet.ethernet)[0]
  148.  
  149. dst = eth.dst
  150. src = eth.src
  151.  
  152. dpid = datapath.id
  153. self.mac_to_port.setdefault(dpid, {})
  154.  
  155. self.logger.info("tpacket in %s %s %s %s", dpid, src, dst, in_port)
  156.  
  157. # learn a mac address to avoid FLOOD next time.
  158. self.mac_to_port[dpid][src] = in_port
  159.  
  160. if dst in self.mac_to_port[dpid]:
  161. out_port = self.mac_to_port[dpid][dst]
  162. else:
  163. out_port = ofproto.OFPP_FLOOD
  164.  
  165. actions = [parser.OFPActionOutput(out_port)]
  166.  
  167. # install a flow to avoid packet_in next time
  168. if out_port != ofproto.OFPP_FLOOD:
  169. match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
  170. # verify if we have a valid buffer_id, if yes avoid to send both
  171. # flow_mod & packet_out
  172. if msg.buffer_id != ofproto.OFP_NO_BUFFER:
  173. self.add_flow(datapath, 1, match, actions, msg.buffer_id)
  174. return
  175. else:
  176. self.add_flow(datapath, 1, match, actions)
  177. data = None
  178. if msg.buffer_id == ofproto.OFP_NO_BUFFER:
  179. data = msg.data
  180.  
  181. out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
  182. in_port=in_port, actions=actions, data=data)
  183. datapath.send_msg(out)
  184.  
  185. """
  186. The event EventSwitchEnter will trigger the activation of get_topology_data().
  187. """
  188. @set_ev_cls(event.EventSwitchEnter, [MAIN_DISPATCHER,CONFIG_DISPATCHER])
  189. def get_topology_data(self, ev):
  190. self.logger.info("[Ehsan] Received EventSwitchEnter")
  191. # Call get_switch() to get the list of objects Switch.
  192. switch_list = get_all_switch(self)
  193.  
  194. # Build a list with all the switches ([switches])
  195. topo_switches = [switch.dp.id for switch in switch_list]
  196.  
  197. # Call get_link() to get the list of objects Link.
  198. links_list = get_all_link(self)
  199.  
  200. # Build a list with all the links [(srcNode, dstNode, port)].
  201. topo_links = [(link.src.dpid,link.dst.dpid,{'port':link.src.port_no}) for link in links_list]
  202.  
  203. print ("tLinks: "+str(topo_links))
  204. print ("tSwitches: "+str(topo_switches))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement