title: python - ryu handling packets using a switch after flow was added to switch
body: <p>I'm using a Ryu open flow controller switch written in python to monitor packets in my virtual mininet.I have 3 hosts and I'm blocking transportation from host2 to host3 and from host3 to host2. Other packets are added to the switch flow table.
  My problem is that after a flow is added, if their is a packet between 2 hosts that have a rule in the flow table of the switch, my event doesn't trigger. 
  For example, if the switch saw a packet from host1 to host2 it is legal so the flow is added to the table, but if another packet from host1 to host2 is sent it won't go through the method again. I looked in Ryu guides but didn't find anyhting regarding the case when a flow was already added to the switch flow table.
  How can I catch the packets?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>import logging
  import struct
  
  from ryu.base import app_manager
  from ryu.controller import mac_to_port
  from ryu.controller import ofp_event
  from ryu.controller.handler import MAIN_DISPATCHER
  from ryu.controller.handler import set_ev_cls
  from ryu.ofproto import ofproto_v1_0
  from ryu.lib.mac import haddr_to_str
  
  
  
  
  class SimpleSwitch(app_manager.RyuApp):
  OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
  counterTraffic=0    
  def __init__(self, *args, **kwargs):
      super(SimpleSwitch, self).__init__(*args, **kwargs)
      self.mac_to_port = {}
  
  def add_flow(self, datapath, in_port, dst, actions):
      ofproto = datapath.ofproto
  
      wildcards = ofproto_v1_0.OFPFW_ALL
      wildcards &amp;= ~ofproto_v1_0.OFPFW_IN_PORT
      wildcards &amp;= ~ofproto_v1_0.OFPFW_DL_DST
  
      match = datapath.ofproto_parser.OFPMatch(
          wildcards, in_port, 0, dst,
          0, 0, 0, 0, 0, 0, 0, 0, 0)
  
      mod = datapath.ofproto_parser.OFPFlowMod(
          datapath=datapath, match=match, cookie=0,
          command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
          priority=ofproto.OFP_DEFAULT_PRIORITY,
          flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
      datapath.send_msg(mod)
  
  @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)        
  def _packet_in_handler(self, ev):
  
  print("Im in main function")
      msg = ev.msg
      datapath = msg.datapath
      ofproto = datapath.ofproto
  
      dst, src, _eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)
  
      dpid = datapath.id
      self.mac_to_port.setdefault(dpid, {})
  
      self.logger.info("packet in %s %s %s %s",
                       dpid, haddr_to_str(src), haddr_to_str(dst),
                       msg.in_port)
  
  if (haddr_to_str(dst) == "00:00:00:00:00:01"):
      print "dst"
      self.counterTraffic +=1
  
  if not ((haddr_to_str(src) == "00:00:00:00:00:02" and  haddr_to_str(dst) =="00:00:00:00:00:03")or (haddr_to_str(src) == "00:00:00:00:00:03" and  haddr_to_str(dst) =="00:00:00:00:00:02")):
          # learn a mac address to avoid FLOOD next time.
      print("after condition")
          self.mac_to_port[dpid][src] = msg.in_port
  
          if dst in self.mac_to_port[dpid]:
              out_port = self.mac_to_port[dpid][dst]
          else:
                  out_port = ofproto.OFPP_FLOOD
  
              actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
  
           # install a flow to avoid packet_in next time
          if out_port != ofproto.OFPP_FLOOD:
                  self.add_flow(datapath, msg.in_port, dst, actions)
  
          out = datapath.ofproto_parser.OFPPacketOut(
          datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
                  actions=actions)
  
          datapath.send_msg(out)
      if (haddr_to_str(src) == "00:00:00:00:00:01"):
          print "src"
                  self.counterTraffic +=1
      print(self.counterTraffic)
  
  
  @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
  def _port_status_handler(self, ev):
      msg = ev.msg
      reason = msg.reason
      port_no = msg.desc.port_no
  
      ofproto = msg.datapath.ofproto
      if reason == ofproto.OFPPR_ADD:
          self.logger.info("port added %s", port_no)
      elif reason == ofproto.OFPPR_DELETE:
          self.logger.info("port deleted %s", port_no)
      elif reason == ofproto.OFPPR_MODIFY:
          self.logger.info("port modified %s", port_no)
      else:
          self.logger.info("Illeagal port state %s %s", port_no, reason)
  </code></pre>