Skip to content

alexbologan/Switch_implementation

Repository files navigation

Functionality for all the tasks (1-3)

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:

1. VLAN Tagging and Management

  • VLAN Parsing: The function parse_ethernet_header extracts 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_tag and remove_vlan_tag are 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.

2. Spanning Tree Protocol (STP) Support

  • Creating BPDUs: The function create_bpdu_packet constructs 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_sec function 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.

3. Handling BPDUs and Root Bridge Election

  • BPDU Handling: When a BPDU is received, the function handle_stp_frame compares 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.

4. Frame Forwarding with MAC Learning

  • Learning MAC Addresses: The handle_frame function is where frame forwarding occurs. It learns the source MAC addresses of incoming frames and records them in a MAC_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.

5. Configuration and Initialization

  • Loading Configurations: The function read_data_vlan_config reads 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 main function 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.

Running

sudo python3 checker/topo.py

This 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 2

The 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&.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors