Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 来自(示例)主机1的ryu(SDN)流量控制器_Python_Linux_Mininet_Sdn_Ryu - Fatal编程技术网

Python 来自(示例)主机1的ryu(SDN)流量控制器

Python 来自(示例)主机1的ryu(SDN)流量控制器,python,linux,mininet,sdn,ryu,Python,Linux,Mininet,Sdn,Ryu,我使用以下命令在mininet环境中创建了一个简单的网络: $ sudo mn --topo single,3 --mac --controller remote --switch ovsk 我想使用RYU控制器计算交换机特殊端口上的带宽流量。我正在考虑对传入的数据包使用ofpentpacketin事件,但我不知道如何从端口输出数据包 我的代码: from operator import attrgetter from ryu.app import simple_switch_13 from

我使用以下命令在
mininet
环境中创建了一个简单的网络:

$ sudo mn --topo single,3 --mac --controller remote --switch ovsk
我想使用
RYU控制器
计算交换机特殊端口上的带宽流量。我正在考虑对传入的数据包使用
ofpentpacketin
事件,但我不知道如何从端口输出数据包

我的代码:

from operator import attrgetter
from ryu.app import simple_switch_13
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.lib import hub

class SimpleMonitor(simple_switch_13.SimpleSwitch13):

    def __init__(self, *args, **kwargs):
        super(SimpleMonitor, self).__init__(*args, **kwargs)
        self.datapaths = {}
        self.monitor_thread = hub.spawn(self._monitor)

    @set_ev_cls(ofp_event.EventOFPStateChange,
                [MAIN_DISPATCHER, DEAD_DISPATCHER])
    def _state_change_handler1(self, ev):
        datapath = ev.datapath
        if ev.state == MAIN_DISPATCHER:
            if not datapath.id in self.datapaths:
                self.logger.debug('register datapath: %016x', datapath.id)
                self.datapaths[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]

    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)
    def _flow_stats_reply_handler(self, ev):
        body = ev.msg.body

        self.logger.info('datapath         '

                         'in-port  eth-dst           '
                         'out-port packets  bytes')
        self.logger.info('---------------- '
                         '-------- ----------------- '
                         '-------- -------- --------')

        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',
               if stat.match['in_port']==1:
                    self.logger.info('%x %x %s %x %d %d',
                             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 [1234]#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',
              # if stat.match['in_port']==1:
                  #  self.logger.info('%x %x %s %x %d %d',
                   #          ev.msg.datapath.id,
                   #          stat.match['in_port'], stat.match['eth_dst'],
                   #          stat.instructions[0].actions[0].port,
                   #          stat.packet_count, stat.byte_count)

    @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
    def _port_stats_reply_handler(self, ev):
        body = ev.msg.body

        self.logger.info('datapath         port     '
                         'rx-pkts  rx-bytes rx-error '
                         'tx-pkts  tx-bytes tx-error')
        self.logger.info('---------------- -------- '
                         '-------- -------- -------- '
                         '-------- -------- --------')
        for stat in sorted(body, key=attrgetter('port_no')):
            if stat.port_no==1:

                 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)

请帮助我编辑此代码以回答我的问题。谢谢。

您必须在控制器中添加两种方法: 1) 请求stats的方法 2) 从交换机捕获答案的方法

回答统计信息的方法如下:

def send_flow_stats_request(self, datapath):
    ofp = datapath.ofproto
    ofp_parser = datapath.ofproto_parser
cookie = cookie_mask = 0
match = ofp_parser.OFPMatch(in_port=1)
req = ofp_parser.OFPFlowStatsRequest(datapath, 0,
                                     ofp.OFPTT_ALL,
                                     ofp.OFPP_ANY, ofp.OFPG_ANY,
                                     cookie, cookie_mask,
                                     match)
datapath.send_msg(req)
获取答案的方法如下:

@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def flow_stats_reply_handler(self, ev):
    flows = []
    for stat in ev.msg.body:
        flows.append('table_id=%s '
                     'duration_sec=%d duration_nsec=%d '
                     'priority=%d '
                     'idle_timeout=%d hard_timeout=%d flags=0x%04x '
                     'cookie=%d packet_count=%d byte_count=%d '
                     'match=%s instructions=%s' %
                     (stat.table_id,
                      stat.duration_sec, stat.duration_nsec,
                      stat.priority,
                      stat.idle_timeout, stat.hard_timeout, stat.flags,
                      stat.cookie, stat.packet_count, stat.byte_count,
                      stat.match, stat.instructions))
    self.logger.debug('FlowStats: %s', flows)
捕获统计数据后,您可以计算带宽使用情况

不过,你也可以执行其他类型的请求,因此我建议你阅读rye文档


您必须在控制器中添加两种方法: 1) 请求stats的方法 2) 从交换机捕获答案的方法

回答统计信息的方法如下:

def send_flow_stats_request(self, datapath):
    ofp = datapath.ofproto
    ofp_parser = datapath.ofproto_parser
cookie = cookie_mask = 0
match = ofp_parser.OFPMatch(in_port=1)
req = ofp_parser.OFPFlowStatsRequest(datapath, 0,
                                     ofp.OFPTT_ALL,
                                     ofp.OFPP_ANY, ofp.OFPG_ANY,
                                     cookie, cookie_mask,
                                     match)
datapath.send_msg(req)
获取答案的方法如下:

@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def flow_stats_reply_handler(self, ev):
    flows = []
    for stat in ev.msg.body:
        flows.append('table_id=%s '
                     'duration_sec=%d duration_nsec=%d '
                     'priority=%d '
                     'idle_timeout=%d hard_timeout=%d flags=0x%04x '
                     'cookie=%d packet_count=%d byte_count=%d '
                     'match=%s instructions=%s' %
                     (stat.table_id,
                      stat.duration_sec, stat.duration_nsec,
                      stat.priority,
                      stat.idle_timeout, stat.hard_timeout, stat.flags,
                      stat.cookie, stat.packet_count, stat.byte_count,
                      stat.match, stat.instructions))
    self.logger.debug('FlowStats: %s', flows)
捕获统计数据后,您可以计算带宽使用情况

不过,你也可以执行其他类型的请求,因此我建议你阅读rye文档