NameError: 全局名称 'get_all_switch' 未定义
我刚开始学习Python,想开发一个OpenFlow应用程序。我现在做的这个应用是一个简单的以太网交换机,代码在这里:
https://github.com/osrg/ryu/blob/master/ryu/app/simple_switch.py
我还有另一个文件:
https://github.com/osrg/ryu/blob/master/ryu/topology/api.py
这个文件看起来是用来获取拓扑中链接和交换机信息的。
但是如果我在simple_switch.py的init()函数里调用这个文件中的函数,就会出现错误。
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
s_list = get_all_switch(app_manager)
这是我遇到的错误。
loading app ryu/app/simple_switch.py
loading app ryu.controller.ofp_handler
instantiating app ryu.controller.ofp_handler of OFPHandler
instantiating app ryu/app/simple_switch.py of SimpleSwitch
Traceback (most recent call last):
File "/usr/local/bin/ryu-manager", line 9, in <module>
load_entry_point('ryu==3.8', 'console_scripts', 'ryu-manager')()
File "/usr/local/lib/python2.7/dist-packages/ryu/cmd/manager.py", line 73, in main
services.extend(app_mgr.instantiate_apps(**contexts))
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 434, in instantiate_apps
self._instantiate(app_name, cls, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 420, in _instantiate
app = cls(*args, **kwargs)
File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 35, in __init__
s_list = get_all_switch(app_manager)
NameError: global name 'get_all_switch' is not defined
我有几个问题:
1) 我能否通过在ryu/topology/api.py中定义的get_all_switch()和get_all_link()来获取我的mininet拓扑信息?
2) 如果可以,为什么上面的代码没有按预期工作?
我在这里问是因为我对Python的了解还不够深入。我想在simple_switch.py中使用topology/api.py里的函数。
在simple_switch.py中的导入部分是这样的:
import logging
import struct
from ryu.base import app_manager
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
根据答案1的更新:
我修改了我的代码,如下所示:
from ryu.base import app_manager
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
import ryu.topology.api
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
s_list = ryu.topology.api.get_all_switch(app_manager.RyuApp)
现在我遇到了一个新的错误:
loading app ryu/app/simple_switch.py
loading app ryu.topology.switches
loading app ryu.controller.ofp_handler
loading app ryu.controller.ofp_handler
instantiating app ryu.topology.switches of Switches
instantiating app ryu.controller.ofp_handler of OFPHandler
instantiating app ryu/app/simple_switch.py of SimpleSwitch
Traceback (most recent call last):
File "/usr/local/bin/ryu-manager", line 9, in <module>
load_entry_point('ryu==3.8', 'console_scripts', 'ryu-manager')()
File "/usr/local/lib/python2.7/dist-packages/ryu/cmd/manager.py", line 73, in main
services.extend(app_mgr.instantiate_apps(**contexts))
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 434, in instantiate_apps
self._instantiate(app_name, cls, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py", line 420, in _instantiate
app = cls(*args, **kwargs)
File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 36, in __init__
s_list = ryu.topology.api.get_all_switch(app_manager.RyuApp)
File "/usr/local/lib/python2.7/dist-packages/ryu/topology/api.py", line 26, in get_all_switch
return get_switch(app)
File "/usr/local/lib/python2.7/dist-packages/ryu/topology/api.py", line 21, in get_switch
rep = app.send_request(event.EventSwitchRequest(dpid))
TypeError: unbound method send_request() must be called with RyuApp instance as first argument (got EventSwitchRequest instance instead)
3 个回答
0
试着用这一行代码替代:
s_list = ryu.topology.api.get_all_switch(self)
1
根据上面的回答和一些深入研究,我的代码如下,现在运行得很好:
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.topology import event
from ryu.topology.api import get_all_switch, get_all_link
class SimpleSwitch13(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
self.mac_to_port = {}
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
self.logger.info("Received EventOFPSwitchFeatures")
msg = ev.msg
self.logger.info('OFPSwitchFeatures received: '
'\n\tdatapath_id=0x%016x n_buffers=%d '
'\n\tn_tables=%d auxiliary_id=%d '
'\n\tcapabilities=0x%08x',
msg.datapath_id, msg.n_buffers, msg.n_tables,
msg.auxiliary_id, msg.capabilities)
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
#self.logger.info("Received EventOFPPacketIn")
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("\tpacket in %s %s %s %s", dpid, src, dst, in_port)
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = in_port
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
# verify if we have a valid buffer_id, if yes avoid to send both
# flow_mod & packet_out
if msg.buffer_id != ofproto.OFP_NO_BUFFER:
self.add_flow(datapath, 1, match, actions, msg.buffer_id)
return
else:
self.add_flow(datapath, 1, match, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)
"""
The event EventSwitchEnter will trigger the activation of get_topology_data().
"""
@set_ev_cls(event.EventSwitchEnter, [MAIN_DISPATCHER,CONFIG_DISPATCHER])
def get_topology_data(self, ev):
self.logger.info("[Ehsan] Received EventSwitchEnter")
# Call get_switch() to get the list of objects Switch.
switch_list = get_all_switch(self)
# Build a list with all the switches ([switches])
topo_switches = [switch.dp.id for switch in switch_list]
# Call get_link() to get the list of objects Link.
links_list = get_all_link(self)
# Build a list with all the links [(srcNode, dstNode, port)].
topo_links = [(link.src.dpid,link.dst.dpid,{'port':link.src.port_no}) for link in links_list]
print ("\tLinks: "+str(topo_links))
print ("\tSwitches: "+str(topo_switches))
目前,我的这个功能是在连接开关时触发的,但你可以修改 get_topology_data()
这个函数,让它在任何事件发生时都能调用。比如,当接收到端口状态时,或者链接被删除、添加或修改时,你都可以调用它来更新拓扑结构。
请注意,我现在还在开发这个代码的阶段。以后我可能会把完整的代码放到我的 代码库 上。
1
- 是的。
- 很可能你应该使用一个带有前缀的名称,比如
ryu.topology.api.get_all_switch()
。要给出更准确的答案,需要更多的上下文信息(比如,展示一下你的import
语句)。