pythonsdnopenflowryu

Keyerror when handling the EventOFPFlowStatsReply message in the Ryu Controller


I'm trying to get the requested FlowStat information in Simple_monitor_13 (Ryu SDN Controller), but when run with a simple mininet topology and "pingall", the application keeps reporting Keyerrors coming from very basic Match Fields like Ipv4_src, eth_type. Did I misunderstand how this event works? And how can this request be fulfilled?

for stat in sorted([flow for flow in body if (flow.priority == 1)], key=lambda flow: 
            (flow.match['in_port'], flow.match['eth_dst'],flow.match['ipv4_src'],flow.match['ipv4_dst'],flow.match['ip_proto'])):
            flow_stat['table_id']= stat.table_id
            flow_stat['priority'] = stat.priority
            flow_stat['in_port'] = stat.match['in_port']
            flow_stat['ip_proto'] = stat.match['ip_proto']
            flow_stat['ipv4_src'] = stat.match['ipv4_src']
            flow_stat['ipv4_dst'] = stat.match['ipv4_dst']
     File "c:\users\acer\appdata\local\programs\python\python38-32\lib\site-packages\ryu\base\app_manager.py", line 290, in _event_loop
        handler(ev)
      File "E:\SDN_ML_Anaconda\ryu\ryu\app\simple_monitor_13_fetch.py", line 91, in _flow_stats_reply_handler
        for stat in sorted([flow for flow in body if (flow.priority == 1)], key=lambda flow:
      File "E:\SDN_ML_Anaconda\ryu\ryu\app\simple_monitor_13_fetch.py", line 92, in <lambda>
        (flow.match['in_port'], flow.match['eth_dst'],flow.match['ipv4_src'],flow.match['ipv4_dst'],flow.match['ip_proto'])):
      File "c:\users\acer\appdata\local\programs\python\python38-32\lib\site-packages\ryu\ofproto\ofproto_v1_3_parser.py", line 904, in __getitem__
        return dict(self._fields2)[key]
    KeyError: 'ipv4_src'

Solution

  • FlowStatsReply

    A FlowStatsReply contains statistics for each flow installed on the switch. Since flows do not have dedicated unique identifiers, they are instead identified by their match fields. If a flow rule does not match on a certain header field, its corresponding flow entry in the FlowStatsReply won't contain that field either.

    Matches in SimpleMonitor13

    If you look at SimpleSwitch13, which SimpleMonitor13 inherits from, you will find that the controller installs flows in its _packet_in_handler with the following match:

     match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
    

    Therefore, the FlowStatsReply in SimpleMonitor13 will only contain the fields in_port, eth_dst and eth_src, but not ipv4_src or eth_type.