In the first task, the switch was built to handle MAC address learning, enabling efficient packet forwarding.
-
MAC Table Initialization
- A dictionary
mac_tablestores mappings between MAC addresses and their corresponding interfaces.
- A dictionary
-
Frame Reception
- Upon receiving a frame on an interface, the switch extracts the source and destination MAC addresses from the Ethernet header.
-
MAC Learning
- The switch learns the source MAC address by adding it to
mac_tablewith the interface it arrived on. This learning process allows the switch to determine where to send frames for known MAC addresses.
- The switch learns the source MAC address by adding it to
-
Forwarding Logic
- Unicast Frames
- If the destination MAC address is in
mac_table, the frame is forwarded to the associated interface. - If the destination MAC address is not in
mac_table, the frame is broadcasted to all other interfaces except the incoming one.
- If the destination MAC address is in
- Broadcast/Multicast Frames
- The frame is forwarded to all interfaces except the one it arrived on.
- Unicast Check
- A helper function,
is_unicast, checks if the destination MAC address is a unicast address by verifying it is not the broadcast address (ff:ff:ff:ff:ff:ff).
- A helper function,
- Unicast Frames
In the second task, VLAN support was added to enable network segmentation at the data link layer.
-
VLAN Table Initialization
- A
vlan_tabledictionary stores VLAN configurations for each interface. - The
parse_configfunction reads VLAN configurations from files located in theconfigsdirectory. These files specify VLAN IDs for each interface or mark them as trunk ports (which carry traffic for multiple VLANs).
- A
-
Reading Configuration Files
- The switch reads its configuration file (e.g.,
switch0.cfg) based on its ID. - Each line after the first (priority line, unused in this task) contains the interface name and its VLAN ID or
Tto indicate a trunk port. - VLAN IDs are stored in
vlan_table, with trunk ports assigned a VLAN ID of-1.
- The switch reads its configuration file (e.g.,
-
Frame Processing with VLANs
- VLAN ID Determination
- Trunk Ports: Extract the VLAN ID from the VLAN tag in the frame header, then remove the tag.
- Access Ports: Obtain the VLAN ID from
vlan_tableusing the interface.
- VLAN ID Determination
-
Forwarding with VLAN Consideration
- The switch updates
mac_tableto associate MAC addresses with both the interface and VLAN ID. - Frames are forwarded only to interfaces that belong to the same VLAN or are trunk ports.
- VLAN checks are applied before forwarding to ensure that frames are restricted to the correct VLAN.
- The switch updates
-
VLAN Tagging and Untagging
- Adding VLAN Tags: VLAN tags are added to frames forwarded to trunk ports.
- Removing VLAN Tags: VLAN tags are removed when frames are forwarded to access ports.
-
Updating Forwarding Logic
- The forwarding function was enhanced to consider VLAN IDs in addition to MAC addresses.
- The switch checks both the MAC address and VLAN ID during forwarding.
-
Handling Interface Types
- Trunk Ports: Use the VLAN ID from the frame’s VLAN tag.
- Access Ports: Use the VLAN ID assigned to the interface in
vlan_table.