Traffic Monitor源碼分析
阿新 • • 發佈:2018-08-01
chang 現在 ger pre priority quest logger 控制 sort
Traffic Monitor源碼分析
從simple_switch_13.SimpleSwitch13控制器繼承並開發
class SimpleMonitor13(simple_switch_13.SimpleSwitch13):
增添datapaths列表,存儲交換機id
def __init__(self, *args, **kwargs):
super(SimpleMonitor13, self).__init__(*args, **kwargs)
self.datapaths = {}
引入hub.spawn()函數啟動一個新線程,輸入為一個新的方法_monitor
。
創建一個EventOFPStateChange監聽事件,監聽MAIN_DISPATCHER
,DEAD_DISPATCHER
兩種情況。
@set_ev_cls(ofp_event.EventOFPStateChange,
[MAIN_DISPATCHER, DEAD_DISPATCHER])
監聽如果為MAIN_DISPATCHER,並且datapath.id不在datapath列表中,則證明是新加入的交換機
if ev.state == MAIN_DISPATCHER: if datapath.id not in self.datapaths: self.logger.debug(‘register datapath: %016x‘, datapath.id) self.datapaths[datapath.id] = datapath
如果為DEAD_DISPATCHER,並且datapath.id在datapath列表中,則證明是掉線的交換機
elif ev.state == DEAD_DISPATCHER:
if datapath.id in self.datapaths:
self.logger.debug(‘unregister datapath: %016x‘, datapath.id)
del self.datapaths[datapath.id]
_monitor方法,循環不斷向datapath列表中的交換機發送Flow狀態請求,和Port狀態請求
def _monitor(self):
while True:
for dp in self.datapaths.values():
self._request_stats(dp)
hub.sleep(10)
def _request_stats(self, datapath):
self.logger.debug(‘send stats request: %016x‘, datapath.id)
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
req = parser.OFPFlowStatsRequest(datapath)
datapath.send_msg(req)
req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY)
datapath.send_msg(req)
剛剛發送了請求,現在監聽其回復
@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
...
@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
...
打印返回的信息
for stat in sorted([flow for flow in body if flow.priority == 1],
key=lambda flow: (flow.match[‘in_port‘],
flow.match[‘eth_dst‘])):
self.logger.info(‘%016x %8x %17s %8x %8d %8d‘,
ev.msg.datapath.id,
stat.match[‘in_port‘], stat.match[‘eth_dst‘],
stat.instructions[0].actions[0].port,
stat.packet_count, stat.byte_count)
...
for stat in sorted(body, key=attrgetter(‘port_no‘)):
self.logger.info(‘%016x %8x %8d %8d %8d %8d %8d %8d‘,
ev.msg.datapath.id, stat.port_no,
stat.rx_packets, stat.rx_bytes, stat.rx_errors,
stat.tx_packets, stat.tx_bytes, stat.tx_errors)
其中sorted ... lambda語法,指元組的排列順序按照先in_port
再eth_dst
至此,整個程序解析完畢。
Traffic Monitor源碼分析