From 5dac2f97025b61af3bfa33f3c167851d4969171b Mon Sep 17 00:00:00 2001 From: structix Date: Thu, 19 May 2022 13:16:49 +0000 Subject: [PATCH] Fix first ping package packet loss --- example.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/example.py b/example.py index 50a7479..ba86ebb 100644 --- a/example.py +++ b/example.py @@ -200,8 +200,6 @@ class L3Switch(app_manager.RyuApp): # install a flow to avoid packet_in next time if out_port != ofproto.OFPP_FLOOD: match = parser.OFPMatch(in_port=in_port, eth_dst=frame.dst) - self.logger.info(actions) - self.logger.info(match) # verify if we have a valid buffer_id, if yes avoid to send both # flow_mod & packet_out if buffer_id != ofproto.OFP_NO_BUFFER: @@ -222,27 +220,37 @@ class L3Switch(app_manager.RyuApp): parser = datapath.ofproto_parser if ipv4_pkt.dst in self.ip_to_mac[dpid]: + # GEt mac and port of the given destination dstMac = self.ip_to_mac[dpid][ipv4_pkt.dst] + out_port = self.mac_to_port[dpid][dstMac] else: - self.send_arp(datapath, 1, self.MAC_ADDR, ipv4_pkt.src, "FF:FF:FF:FF:FF:FF", ipv4_pkt.dst, ofproto.OFPP_FLOOD) - return + # Send an arp request and define mac and port + # so we're able to apply the following actions + # on the packet. + out_port = ofproto.OFPP_FLOOD + dstMac = "FF:FF:FF:FF:FF:FF" + self.send_arp(datapath, 1, self.MAC_ADDR, ipv4_pkt.src, dstMac, ipv4_pkt.dst, out_port) - out_port = self.mac_to_port[dpid][dstMac] + # Actions are applied in the same order as defined in the list. + # OFPActionOutput terminates the chain. So this call should be the last action. actions = [parser.OFPActionDecNwTtl(), #decrease TTL count parser.OFPActionSetField(eth_src=self.MAC_ADDR), # set own mac as source parser.OFPActionSetField(eth_dst=dstMac), # set dst from arp request parser.OFPActionOutput(out_port)] # forward packet through out_port - # install a flow to avoid packet_in next time - match = parser.OFPMatch(in_port=in_port, ipv4_dst=ipv4_pkt.dst, eth_type=0x0800) #onlz match ipv4 packets + # Only install a flow if we#re in the first case (specific mac and out_port) + if ipv4_pkt.dst in self.ip_to_mac[dpid]: + # install a flow to avoid packet_in next time + match = parser.OFPMatch(in_port=in_port, ipv4_dst=ipv4_pkt.dst, eth_type=0x0800) #onlz match ipv4 packets + + # verify if we have a valid buffer_id, if yes avoid to send both + # flow_mod & packet_out + if buffer_id != ofproto.OFP_NO_BUFFER: + self.add_flow(datapath, 1, match, actions, buffer_id) + return + else: + self.add_flow(datapath, 1, match, actions) - # verify if we have a valid buffer_id, if yes avoid to send both - # flow_mod & packet_out - if buffer_id != ofproto.OFP_NO_BUFFER: - self.add_flow(datapath, 1, match, actions, buffer_id) - return - else: - self.add_flow(datapath, 1, match, actions) data = None if buffer_id == ofproto.OFP_NO_BUFFER: data = packet.data @@ -301,6 +309,7 @@ class L3Switch(app_manager.RyuApp): if icmp_pkt: self.do_icmp(datapath, in_port, eth, ipv4_pkt, icmp_pkt) else: + # forward the package into a different subnet self.do_l3_switch(datapath, dpid, pkt, eth, ipv4_pkt, in_port, msg.buffer_id)