0

I am using mininet with POX, and I am using python3 for coding. I am tring to write a code to count the number of flows avaiable on each link. my approach is to fet the flows, find the associated switches with that flow and and find the corespondig link. the problem is that I can't find a way to get the dpid of the sourse and destination switch from the flow. here is the code I'm using:

 def _handle_FlowStatsReceived(event):
  stats = flow_stats_to_list(event.stats)
  link_counters = {}  # keep track of the number of flows for each link
  for flow in stats:
    if flow is not None:
      # Check the match fields of each flow
      # to determine the source and destination switches of the flow

      src_dpid = flow['match']['in_port'].dpid
      dst_dpid = flow['actions']['port'].dpid

      # Increment the counter for the link between the source and destination switches
      link = (src_dpid, dst_dpid)
      if link in link_counters:
        link_counters[link] += 1
      else:
        link_counters[link] = 1


  print("Flow counts per link:")
  for link, count in link_counters.items():
    print("  Link {}-{}: {} flows".format(link[0], link[1], count))


def _flow_counter():
  for switch in core.openflow.connections:
    dpid = switch.dpid
    core.openflow.sendToDPID(dpid, of.ofp_stats_request(body=of.ofp_flow_stats_request()))

when I try to run this code i get the following error:

` [core] Exception while handling OpenFlowNexus!FlowStatsReceived...

Traceback (most recent call last):
  File "/home/mininet/pox/pox/lib/revent/revent.py", line 242, in raiseEventNoErrors
    return self.raiseEvent(event, *args, **kw)
  File "/home/mininet/pox/pox/lib/revent/revent.py", line 295, in raiseEvent
    rv = event._invoke(handler, *args, **kw)
  File "/home/mininet/pox/pox/lib/revent/revent.py", line 168, in _invoke
    return handler(self, *args, **kw)
  File "/home/mininet/pox/ext/l3_learning_08022023.py", line 366, in _handle_FlowStatsReceived                               
    src_dpid = flow['match']['in_port'].dpid
AttributeError: 'int' object has no attribute "dpid"

or

Traceback (most recent call last):
  File "/home/mininet/pox/pox/lib/revent/revent.py", line 242, in raiseEventNoErrors
    return self.raiseEvent(event, *args, **kw)
  File "/home/mininet/pox/pox/lib/revent/revent.py", line 295, in raiseEvent
    rv = event._invoke(handler, *args, **kw)
  File "/home/mininet/pox/pox/lib/revent/revent.py", line 168, in _invoke
    return handler(self, *args, **kw)
  File "/home/mininet/pox/ext/l3_learning_08022023.py", line 366, in _handle_FlowStatsReceived                               
    src_dpid = flow['match']['in_port'].dpid
AttributeError: 'int' object has no attribute "dpid"
KeyError: "in_port"`

based on the error I can not access the source switch dpid by the in_port object. is there any other way to find the source and destination dpid?

0 Answers0