2015-07-01 56 views
2

我目前正在使用OpenVSwitch和Ryu SDN控制器框架設置測試平臺。該OVS是在Linux上運行,並有三個端口(包括內部端口),因爲可以在下面的輸出中可以看出:如何用Ryu獲取OVS端口信息

[email protected]:~# ovs-ofctl -O OpenFlow13 show br0 
OFPT_FEATURES_REPLY (OF1.3) (xid=0x2): dpid:aaaaaaaaaaaaaa21 
n_tables:254, n_buffers:256 
capabilities: FLOW_STATS TABLE_STATS PORT_STATS QUEUE_STATS 
OFPST_PORT_DESC reply (OF1.3) (xid=0x3): 
6(eth1): addr:00:50:56:82:dc:83 
    config:  0 
    state:  0 
    current: 10GB-FD COPPER 
    advertised: COPPER 
    supported: 1GB-FD 10GB-FD COPPER 
    speed: 10000 Mbps now, 10000 Mbps max 
10(eth2): addr:00:50:56:82:29:cb 
    config:  0 
    state:  0 
    current: 10GB-FD COPPER 
    advertised: COPPER 
    supported: 1GB-FD 10GB-FD COPPER 
    speed: 10000 Mbps now, 10000 Mbps max 
LOCAL(br0): addr:00:50:56:82:29:cb 
    config:  PORT_DOWN 
    state:  LINK_DOWN 
    speed: 0 Mbps now, 0 Mbps max 
OFPT_GET_CONFIG_REPLY (OF1.3) (xid=0x5): frags=normal miss_send_len=0 

我好不容易纔得到通知時,新的交換機使用下面的代碼片段(最小連接工作示例):

class MscApp(app_manager.RyuApp): 

    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] 

    _CONTEXTS = { 
     'dpset': dpset.DPSet, 
    } 

    def __init__(self, *args, **kwargs): 
     super(MscApp, self).__init__(*args, **kwargs) 
     self.dpset = kwargs['dpset'] 

     # Initiate datapath array 
     self.datapaths = { 
      0xAAAAAAAAAAAAAA21: { 
       'name': 'Munic', 
      } 
     } 


    @set_ev_cls(ofp_event.EventOFPDescStatsReply, MAIN_DISPATCHER) 
    def desc_stats_reply_handler(self,msg): 
     ofp = msg.datapath.ofproto 
     body = ev.msg.body 

     self.logger.info('OFPDescStatsReply received: ' 
         'mfr_desc=%d hw_desc=%s sw_desc=%s ' 
         'serial_num=%s dp_desc=%s ', 
         body.mfr_desc, body.hw_desc, body.sw_desc, 
         body.serial_num, body.dp_desc) 

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) 
    def switch_features_handler(self, ev): 
     datapath = ev.msg.datapath 
     ofproto = datapath.ofproto 
     parser = datapath.ofproto_parser 
     print 'Router %s has joined!' % self.datapaths[datapath.id]['name'] 

     # Get available ports 
     req = parser.OFPPortDescStatsRequest(datapath, 0) 
     datapath.send_msg(req) 

    @set_ev_cls(event.EventLinkAdd) 
    def link_add(self, ev): 
     print ev.link.src, ev.link.dst 
     print self._get_hwaddr(ev.link.src.dpid, ev.link.src.port_no) 

當所示開關連接時,控制器正確打印Router Munic has joined!。但是,獲取有關可用端口信息的代碼片段不起作用。你知道如何獲取ryu中的可用端口嗎?代碼片段從this question。背景:OVS有兩個物理端口,一個連接到「外部」網絡,另一個連接到「內部」網絡。我不僅需要知道哪些端口可用,還需要知道哪個端口是哪個端口。任何想法如何解決這個問題?提前致謝!

回答

2

根據我的理解,您可以設置要在不同階段接收的事件。 set_ev_cls的第二個參數表示交換機的狀態。如果您想在Ryu和交換機之間的協商完成之前忽略packet_in消息,請使用MAIN_DISPATCHER。換句話說,使用MAIN_DISPATCHER表示只有在協商完成後纔會調用該函數。

有4個協商階段:

  • HANDSHAKE_DISPATCHER - >發送和等待hello消息
  • CONFIG_DISPATCHER - >版本協商和發送特徵請求消息
  • MAIN_DISPATCHER - >開關設有消息中接收併發送了set-config消息
  • DEAD_DISPATCHER - >斷開與對等體的連接。或由於某些錯誤而斷開連接。

查看Ryu API瞭解更多詳情。

所以回到你的問題,一個可能的原因是你正在使用CONFIG_DISPATCHER。將其更改爲MAIN_DISPATCHER並查看它是否有效。

此外,請確保您向交換機發送OFPPortDescStatsRequest。原因我不認爲開關生成EventOFPPortDescStatsReply除非被請求。您需要一個生成交換機請求的功能。我像這樣就會修復你的代碼。這裏是功能:(但是,我建議看看我的github中的this file)。

def send_port_desc_stats_request(self, datapath): 
    ofp_parser = datapath.ofproto_parser 

    req = ofp_parser.OFPPortDescStatsRequest(datapath, 0) 
    datapath.send_msg(req) 

活動EventOFPPortStatsReplyEventOFPPortDescStatsReply可能是有用的。我正在使用它們like this

我在我的github有一個教程,我在那裏使用了幾個事件。上面的功能在這裏被定義和解釋。