From c67d2041f9cd07bae7178ad3b8ca4c7a3d7cf1ed Mon Sep 17 00:00:00 2001 From: structix Date: Sun, 29 May 2022 17:15:04 +0000 Subject: [PATCH] Fix LPM in controller 1.3_1 --- Uebung1/controller_1_3_1.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Uebung1/controller_1_3_1.py b/Uebung1/controller_1_3_1.py index f21bb95..134f29c 100644 --- a/Uebung1/controller_1_3_1.py +++ b/Uebung1/controller_1_3_1.py @@ -10,7 +10,10 @@ from ryu.lib.packet import ipv4 from ryu.lib.packet import icmp from ryu.lib.packet.arp import arp from ryu.lib.packet.packet import Packet -#from ryu.ofproto.ofproto_v1_3 import OFPActionSetField + +# For IP address subnet matching +import ipaddress + class L3Switch(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] @@ -221,23 +224,20 @@ class L3Switch(app_manager.RyuApp): ofproto = datapath.ofproto parser = datapath.ofproto_parser - self.logger.info("Paket: ") - self.logger.info(packet) if ipv4_pkt.dst in self.ip_to_mac[dpid]: - # GEt mac and port of the given destination + # 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: - if datapath.id == 2 and ipv4_pkt.dst == "10.0.1.1": + # This flow is for the second switch if the address is in range 10.0.0.0-10.0.3.255 + # The ipaddress module assumes an IP adress as a unicode string (python 2.7 default for a string is ascii encoding) + if datapath.id == 2 and ipaddress.ip_address(unicode(ipv4_pkt.dst)) in ipaddress.ip_network(unicode("10.0.0.0/22")): actions = [parser.OFPActionDecNwTtl(), #decrease TTL count parser.OFPActionSetField(eth_src=self.MAC_ADDR[1]), # set own mac as source parser.OFPActionSetField(eth_dst=self.MAC_ADDR[0]), # set dst switch parser.OFPActionOutput(2)] - match = parser.OFPMatch(ipv4_dst=("10.0.1.0", "255.255.252.0"), eth_dst=self.MAC_ADDR[1], eth_type=0x0800) - self.logger.info("Match Switch 2: ") - self.logger.info(match) - #match = parser.OFPMatch(ipv4_src="10.0.4.1", ipv4_dst="10.0.1.1", eth_type=0x0800) + match = parser.OFPMatch(ipv4_dst=("10.0.0.0", "255.255.252.0"), eth_dst=self.MAC_ADDR[1], eth_type=0x0800) self.add_flow(datapath, 1, match, actions) data = None @@ -248,9 +248,8 @@ class L3Switch(app_manager.RyuApp): datapath.send_msg(out) return - - - if datapath.id == 1 and ipv4_pkt.dst == "10.0.4.1": + # Add a flow for the first switch if the ipadress matches network 4. + if datapath.id == 1 and ipaddress.ip_address(unicode(ipv4_pkt.dst)) in ipaddress.ip_network(unicode("10.0.4.0/30")): actions = [parser.OFPActionDecNwTtl(), #decrease TTL count parser.OFPActionSetField(eth_src=self.MAC_ADDR[0]), # set own mac as source parser.OFPActionSetField(eth_dst=self.MAC_ADDR[1]), # set dst switch @@ -259,8 +258,6 @@ class L3Switch(app_manager.RyuApp): match = parser.OFPMatch(ipv4_dst=("10.0.4.0", "255.255.255.252"), eth_dst=self.MAC_ADDR[0], eth_type=0x0800) self.add_flow(datapath, 1, match, actions) - self.logger.info("Match Switch 1: ") - self.logger.info(match) data = None if buffer_id == ofproto.OFP_NO_BUFFER: data = packet.data @@ -273,7 +270,6 @@ class L3Switch(app_manager.RyuApp): # Send an arp request and define mac and port # so we're able to apply the following actions # on the packet. - self.logger.info("ARP") out_port = ofproto.OFPP_FLOOD dstMac = "FF:FF:FF:FF:FF:FF" self.send_arp(datapath, 1, self.MAC_ADDR[datapath.id-1], ipv4_pkt.src, dstMac, ipv4_pkt.dst, out_port)