Fix LPM in controller 1.3_1

This commit is contained in:
2022-05-29 17:15:04 +00:00
parent 7909737713
commit c67d2041f9

View File

@@ -10,7 +10,10 @@ from ryu.lib.packet import ipv4
from ryu.lib.packet import icmp from ryu.lib.packet import icmp
from ryu.lib.packet.arp import arp from ryu.lib.packet.arp import arp
from ryu.lib.packet.packet import Packet 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): class L3Switch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
@@ -221,23 +224,20 @@ class L3Switch(app_manager.RyuApp):
ofproto = datapath.ofproto ofproto = datapath.ofproto
parser = datapath.ofproto_parser parser = datapath.ofproto_parser
self.logger.info("Paket: ")
self.logger.info(packet)
if ipv4_pkt.dst in self.ip_to_mac[dpid]: 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] dstMac = self.ip_to_mac[dpid][ipv4_pkt.dst]
out_port = self.mac_to_port[dpid][dstMac] out_port = self.mac_to_port[dpid][dstMac]
else: 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 actions = [parser.OFPActionDecNwTtl(), #decrease TTL count
parser.OFPActionSetField(eth_src=self.MAC_ADDR[1]), # set own mac as source 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.OFPActionSetField(eth_dst=self.MAC_ADDR[0]), # set dst switch
parser.OFPActionOutput(2)] 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) match = parser.OFPMatch(ipv4_dst=("10.0.0.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)
self.add_flow(datapath, 1, match, actions) self.add_flow(datapath, 1, match, actions)
data = None data = None
@@ -248,9 +248,8 @@ class L3Switch(app_manager.RyuApp):
datapath.send_msg(out) datapath.send_msg(out)
return return
# 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")):
if datapath.id == 1 and ipv4_pkt.dst == "10.0.4.1":
actions = [parser.OFPActionDecNwTtl(), #decrease TTL count actions = [parser.OFPActionDecNwTtl(), #decrease TTL count
parser.OFPActionSetField(eth_src=self.MAC_ADDR[0]), # set own mac as source parser.OFPActionSetField(eth_src=self.MAC_ADDR[0]), # set own mac as source
parser.OFPActionSetField(eth_dst=self.MAC_ADDR[1]), # set dst switch 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) 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.add_flow(datapath, 1, match, actions)
self.logger.info("Match Switch 1: ")
self.logger.info(match)
data = None data = None
if buffer_id == ofproto.OFP_NO_BUFFER: if buffer_id == ofproto.OFP_NO_BUFFER:
data = packet.data data = packet.data
@@ -273,7 +270,6 @@ class L3Switch(app_manager.RyuApp):
# Send an arp request and define mac and port # Send an arp request and define mac and port
# so we're able to apply the following actions # so we're able to apply the following actions
# on the packet. # on the packet.
self.logger.info("ARP")
out_port = ofproto.OFPP_FLOOD out_port = ofproto.OFPP_FLOOD
dstMac = "FF:FF:FF:FF:FF:FF" 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) self.send_arp(datapath, 1, self.MAC_ADDR[datapath.id-1], ipv4_pkt.src, dstMac, ipv4_pkt.dst, out_port)