03 mayo, 2023

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.


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).


# Start

p, i := Wait_for_packet()
; p is the packet
; i is the ingress interface

; get source and destination macs
d := parse_destination_mac(p)
s := parse_source_mac(p)

; Handle lookup table for source mac
ci := get_interface_from_lookup_table(s)
if ci is null
  add_to_lookup_table(s, i)
  ; adds the entry and sets up the timer
elif ci == i
  refresh_lookup_table(s, ci)
  ; shouldn't happen unless a host changed interface
  delete_lookup_entry(s, ci)
  add_to_lookup_table(s, i)

; Analyze recipient and finally send packet
if mac_type(d) == unicast
   co := get_interface_from_lookup_table(d)
   if co is null
      ; when destination is not cached we flood
      send_packet_to_all_interfaces_except(p, i)
      send_packet_to_interface(p, i)
   send_packet_to_all_interfaces_except(p, i)

goto start

Note: This example is suposed to model a basic switch and does not cover advanced functionality like: VLAN, multicast groups, spanning tree.