Pseudocode for an Ethernet switch
How does an Ethernet switch work? I'll explain it as far as I can understand, using the clearest way I can imagine (code). Hopefully I got it right.
Prerrequisites
The switch has a number of interfaces or ports. There are cables connected to some of them, that come from computers or other switches or network equipment.
The switch receives and sends ethernet packets (or frames) through its ports.
Each packets typically includes a couple mac addresses. A mac address is a 12 digit hexadecimal number (6 bytes) that is used to identify the sender or recipient of a packet. There several kind:
- Unicast: Every network card has a unique own address. Eg: a laptop could have 2 of them (one for LAN card, another for Wi-Fi). The 8th bit of this type is always 0.
- Non-Unicast:
- Broadcast: The mac ff:ff:ff:ff:ff:ff. Used to send packets to all the hosts in a LAN.
- Multicast: All macs that have 8th bit set to 1, but are not the broadcast mac. Used for multicast communication.
Every packet has these fields.
- Destination Mac Address: Identifies the recipient of the packet. It may be a host or a router, but it could also be multicast or broadcast.
- Source Mac Address: Identifies who sent the packet. Typically a host/router in the LAN.
- Ethernetype: Identifies the type of packet. Eg: An IP packet.
- Payload: Other headers (like TCP) and finally data.
The switch has an internal "bridge table" or "mac table". Which is used to tell on which interface is every mac address that it knows. It's fields are:
- Mac address
- Interface
- Timeout (after not seeing a packet with that mac address the entry is removed to save resources).