微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 ryu 和 networkx

如何解决使用 ryu 和 networkx

我正在尝试使用 networkx 在 ryu 中执行拓扑发现。下面是添加开关和添加链接功能

'''

# Handle the event generated when a new switch enters the network
@set_ev_cls(event.EventSwitchEnter)
def handler_switch_enter(self,ev):
   
    
    datapath = ev.switch.dp
    datapath.id = ev.switch.dp.id
    self.net.add_node(datapath)
    self.logger.info("Current Switches %s",datapath)
    print("No of Switches %s",self.net.number_of_nodes())
    self.Topology['Switches'][datapath.id] = datapath
    #self.Topology = {'Hosts':{},'Switches':{switch},'Links':{}}
    print(self.Topology)

# Handle the event generated when a new link is added between two nodes
@set_ev_cls(event.EventLinkAdd)
def link_add(self,ev):
   
    
    link = ev.link
    src_port = link.src.port_no
    dst_port = link.dst.port_no
    src_id = link.src.dpid
    dst_id = link.dst.dpid
    self.net.add_edge(link.src,link.dst)
    self.logger.info("Current Links %s",link)
    print("No of Links %s",self.net.number_of_edges())
    self.Topology['Links'][(src_id,dst_id)] = (src_port,dst_port)
    self.Topology['Links'][(dst_id,src_id)] = (dst_port,src_port)
    #self.Topology = {'Hosts':{},'Switches':{},'Links':{link}}
    print(self.Topology)

'''

现在我尝试在处理程序的数据包中添加主机,如下所示:-

'''

@set_ev_cls(ofp_event.EventOFPPacketIn,MAIN_disPATCHER)
def _packet_in_handler(self,ev):

    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    # Extract the message from the event
    datapath = msg.datapath # Get the datapath at which the message arrived
    ofproto = datapath.ofproto # Get the OpenFlow protocol of the datapath
    parser = datapath.ofproto_parser # Get the parser for the OpenFlow protocol
    in_port = msg.match['in_port']  #The physical port at which the switch received this packet

    pkt = packet.Packet(msg.data)   # Extract the packet from the OpenFlow message
    eth = pkt.get_protocols(ethernet.ethernet)[0]  # Get the Ethernet header
    
    if eth.ethertype != ether_types.ETH_TYPE_ARP: # Only use ARP messages to handle forwarding. 
        return                                    # Ignore other incoming packets.

    arp_pkt = pkt.get_protocols(arp.arp)[0] # Get the ARP header
    src_ip,dst_ip = arp_pkt.src_ip,arp_pkt.dst_ip # source and destination IP of the ARP message
    
    if self.net.has_node(src_ip) != True:
        
        ipv4_addr = src_ip
        mac_addr = arp_pkt.src_mac 
        self.net.add_node(src_ip) 
        self.net.add_edge(src_ip,datapath) 
        self.Topology['Hosts'][ipv4_addr] = mac_addr
        print(self.Topology)
        self.net.nodes(data=True) 
    
    if dst_ip in self.net:
        path=nx.shortest_path(self.net,src_ip,dst_ip)
        #path=nx.shortest_path(self.net[src_ip,dst_ip])
        #next=path[path.index(dpid)+1]
        #out_port=self.net[dpid][next]['port']   
        self.install_entries_on_path(path)
        print(path)

'''

我原以为拓扑有 10 台主机(10.0.0.1 到 10.0.0.10),但我得到了关注-

    {'Hosts': {'10.0.0.1': '00:00:00:00:01:01','10.0.0.6': '00:00:00:00:06:06','10.0.0.7':           '00:00:00:00:07:07','10.0.0.2': '00:00:00:00:02:02','10.0.0.3': '00:00:00:00:03:03','10.0.0.4': '00:00:00:00:04:04','10.0.0.5': '00:00:00:00:05:05','10.0.0.8': '00:00:00:00:08:08','10.0.0.9': '00:00:00:00:09:09','10.0.0.10': '00:00:00:00:10:10','0.0.0.0': 'fa:80:c3:70:e5:f6','169.254.217.192': '86:9f:11:ba:97:4c','169.254.238.251': 'c2:66:25:34:46:48','169.254.224.57': 'c2:a0:b6:47:d1:4d','169.254.81.159': '8e:fd:4f:3c:01:4a','169.254.8.131': '1a:a0:39:02:a5:43','169.254.139.242': '62:b3:42:0d:ac:43','169.254.173.239': '4e:f7:44:68:a3:4f','169.254.55.182': 'ca:ed:0f:ea:a3:00','169.254.230.229': 'aa:fe:79:34:fa:ee','169.254.196.192': 'fa:80:c3:70:e5:f6','169.254.201.96': '46:1d:c3:ee:19:b6','169.254.199.2': 'ee:86:84:76:7f:a3','169.254.25.33': '36:8a:d3:d6:8e:63','169.254.177.37': '3e:61:01:58:73:cf','169.254.226.128': '6a:d3:9e:6d:42:76','169.254.82.48': '4e:23:eb:1d:24:78','169.254.176.112': '12:94:44:fb:e3:9b','169.254.186.215': '42:d8:fa:fb:9a:36','169.254.197.195': 'be:8b:64:36:4c:b3','169.254.220.122': 'f2:fb:70:8a:c5:dd'},'Switches': {7: <ryu.controller.controller.Datapath object at 0x7f6e5dd71ee0>,6: <ryu.controller.controller.Datapath object at 0x7f6e5dd7f3d0>,1: <ryu.controller.controller.Datapath object at 0x7f6e5dd7f550>,4: <ryu.controller.controller.Datapath object at 0x7f6e5dd7f700>,3: <ryu.controller.controller.Datapath object at 0x7f6e61da3670>,5: <ryu.controller.controller.Datapath object at 0x7f6e61ec3f10>,2: <ryu.controller.controller.Datapath object at 0x7f6e5dd131c0>},'Links': {(4,5): (3,2),(5,4): (2,3),(2,1),2): (1,(7,3): (1,(3,7): (3,1): (1,(1,3): (2,6): (2,(6,(4,2)}}

如何去除这些多余的条目?

另外,我可以在主机字典中获取主机标识符吗(现在我正在使用主机的 IP)?

非常感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。