The code implements a network switch with support for VLANs (Virtual Local Area Networks) without native vlan and the Spanning Tree Protocol (STP) to prevent loops in the network. Here’s a breakdown of its key functionalities:
-
VLAN Parsing: The function
parse_ethernet_headerextracts important information from incoming Ethernet frames. It identifies the source and destination MAC addresses, the EtherType, and checks for VLAN tags. If a VLAN tag is present, it retrieves the VLAN ID, which is crucial for managing different network segments. -
VLAN Tagging: Functions like
create_vlan_tagandremove_vlan_tagare responsible for adding and removing VLAN tags from frames. This allows frames to be properly handled as they move between switches and hosts, ensuring that devices on the same VLAN can communicate while remaining isolated from other VLANs.
-
Creating BPDUs: The function
create_bpdu_packetconstructs Bridge Protocol Data Units (BPDUs), which are essential for STP. These packets contain information about the root bridge and path costs, helping switches make decisions about the best paths through the network.(I didnt bother with the other fields, cuz i didnt use them) -
Sending BPDUs: The
send_bdpu_every_secfunction runs in a separate thread, sending out BPDUs every second if the switch is designated as the root. This periodic communication helps maintain a consistent view of the network topology.
- BPDU Handling: When a BPDU is received, the function
handle_stp_framecompares the incoming root bridge ID with the current root bridge ID. If it finds a lower ID, it updates its information, blocks other ports, and sends out updated BPDUs. This process helps elect the root bridge and manage the network topology effectively.
-
Learning MAC Addresses: The
handle_framefunction is where frame forwarding occurs. It learns the source MAC addresses of incoming frames and records them in aMAC_Table. If a destination MAC is known, the frame is sent directly to the corresponding port. If not, the frame is flooded to all ports (except the source), ensuring it reaches its destination. -
VLAN Considerations: When sending frames, the switch checks whether the source and destination devices are on the same VLAN. If the destination is a host, it removes the VLAN tag to send an untagged frame. If it’s another switch, it keeps the VLAN tag intact for proper routing.
-
Loading Configurations: The function
read_data_vlan_configreads VLAN settings from a configuration file, allowing the switch to know which ports connect to hosts and which connect to other switches. This setup is crucial for proper network segmentation. -
Main Function: The
mainfunction is the core of the program, initializing interfaces, VLANs, and setting up STP. It assumes the switch is the root bridge initially and enters a loop to receive frames, process them, and start the BPDU-sending thread.
sudo python3 checker/topo.pyThis will open 9 terminals, 6 hosts and 3 for the switches. On the switch terminal you will run
make run_switch SWITCH_ID=X # X is 0,1 or 2The hosts have the following IP addresses.
host0 192.168.1.1
host1 192.168.1.2
host2 192.168.1.3
host3 192.168.1.4
host4 192.168.1.5
host5 192.168.1.6
We will be testing using the ICMP. For example, from host0 we will run:
ping 192.168.1.2
Note: We will use wireshark for debugging. From any terminal you can run wireshark&.