Add comments and cleanup

This commit is contained in:
2022-05-29 18:48:24 +00:00
parent c67d2041f9
commit 92e82b93fb
4 changed files with 26 additions and 16 deletions

View File

@@ -1,3 +1,6 @@
# Abgabe von:
# Jutta Seth, Janek Schoffit, Eva Högner
# Problem 1.4 (Packet-based IP Anycast)
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
@@ -23,8 +26,10 @@ class L3Switch(app_manager.RyuApp):
self.mac_to_port = {}
self.port_to_mac = {}
self.ip_to_mac = {}
# Define the ports for anycast round robin
self.anycastPorts = [1, 3, 4]
self.anycastIndex = 0
self.anycastIndex = 0 # round robin index for the ports array
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
@@ -190,18 +195,10 @@ class L3Switch(app_manager.RyuApp):
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
# add_flow removed, otherwise the controller wouldn't receive new packets
# for the anycast switching.
# 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)
# 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)
actions = [parser.OFPActionOutput(out_port)]
data = None
if buffer_id == ofproto.OFP_NO_BUFFER:
data = packet.data
@@ -210,6 +207,7 @@ class L3Switch(app_manager.RyuApp):
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)
# Get the next port where a package should be forwarded
def anycast_next(self):
if self.anycastIndex >= 2:
self.anycastIndex = 0
@@ -221,8 +219,10 @@ class L3Switch(app_manager.RyuApp):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# Select a port
port = self.anycast_next()
# Send the package without adding a flow
packet.get_protocols(ethernet.ethernet)[0].dst = self.port_to_mac[dpid][str(port)]
self.send_packet(datapath, port, packet)
@@ -273,6 +273,7 @@ class L3Switch(app_manager.RyuApp):
icmp_pkt = pkt.get_protocol(icmp.icmp)
if icmp_pkt:
self.do_icmp(datapath, in_port, eth, ipv4_pkt, icmp_pkt)
# Do anycast if the package contains the anycast dst-IP
if ipv4_pkt.dst == "10.0.0.100":
self.do_anycast(datapath, dpid, pkt, eth, in_port, msg.buffer_id)
return