SkillPilotAI

How a switch works

A switch is the Layer 2 device at the centre of every LAN. It learns which MAC address is on which port, floods only when it must, and otherwise forwards each frame to just one port, which is why it replaced the hub.

6.1 Why is it used?

  • Four PCs on four switch ports: when PC A sends to PC B, the switch must know which port B is on.
  • At power-on the MAC table is empty; the switch fills it by watching traffic.
  • Forwarding to one port saves bandwidth and avoids the collisions of a hub.

6.2 How it works: MAC learning and flooding

  • Learning: on every incoming frame the switch reads the source MAC and records MAC -> port.
  • Forwarding: it reads the destination MAC; if it is in the table, the frame goes out of that one port only.
  • Flooding: if the destination is unknown, the frame goes out of every port except the one it came in on.
  • Non-matching PCs silently drop a flooded frame; only the owner accepts it.
First frame A -> B, then the reply
  1. 1.PC A (port 1, ..:01) sends a frame to B; table empty

  2. 2.Switch learns ..:01 = port 1 (MAC learning)

  3. 3.B unknown: flood to ports 2, 3, 4; C and D drop it, B accepts

  4. 4.B replies on port 2: switch learns ..:02 = port 2

  5. 5.A is known: reply forwarded to port 1 only; C and D never see it

After one round trip both MACs are in the table

6.3 MAC table = CAM table

TermMeaning
MAC address tableWhich MAC is on which port
CAM (Content Addressable Memory)Hardware memory that gives the port for a MAC in one step (Cisco name: CAM table)
EntryMAC + port + VLAN
Ageing timerDefault 300 seconds; no frame from that MAC -> entry deleted

6.4 When flooding happens

CaseDestinationWhy it floods
Unknown unicastA MAC not in the table yetSwitch does not know the port
Broadcastff:ff:ff:ff:ff:ff (e.g. ARP request)Always meant for everyone
MulticastGroup MAC (01:00:5e...)Flooded like broadcast unless IGMP snooping is on

6.5 Comparison: hub vs switch vs router

  • Hub: Layer 1, does not read MACs, repeats every frame out of all ports; all devices share the bandwidth.
  • Hub is half duplex: if PC C and PC D send together the signals collide, both wait a random time and retry (CSMA/CD).
  • Switch: Layer 2, forwards by MAC table, full duplex ports, no collisions.
  • Router: Layer 3, routes by IP between networks and does not forward broadcasts.
PointHubSwitchRouter
Layer123
Forwards byNothing (repeats to all)MAC tableIP routing table
DuplexHalf (collisions, CSMA/CD)Full per portFull per interface
Collision domains1 for all ports1 per port1 per interface
Broadcast domains11 (no VLANs)1 per interface (stops broadcasts)

6.6 Collision domain vs broadcast domain

  • Collision domain: area where two devices sending together can collide.
  • Broadcast domain: area one broadcast frame can reach.
  • Hub: all ports = 1 collision domain. Switch: each port its own collision domain, all ports 1 broadcast domain.
  • Router: every interface is a separate broadcast domain.
A router stops broadcasts
  1. 1.PC 1 (LAN 1) sends an ARP broadcast

  2. 2.Switch 1 floods it to PC 2 and the router

  3. 3.Router does not forward it: PC 3 and PC 4 in LAN 2 never see it

  4. 4.A unicast from PC 1 to PC 3 is routed by IP: Switch 1 -> router -> Switch 2 -> PC 3

6.7 L2 switch vs L3 switch

PointL2 switchL3 switch
Forwards byMAC onlyMAC + IP (routing between VLANs in hardware)
Where usedAccess layer (desks, floors)Office / data center core
Internet edgeNoUsually a router is still used at the edge

6.8 Practical: Linux bridge as a software switch

Docker container with a Linux bridge br0 (the switch) and hosts h1, h2, h3 on its three ports (192.168.26.11-13).

Terminal
# br0 = switch, hosts h1 h2 h3bridge link showbridge fdb show br br0 dynamic
Output
$ bridge link show3: veth1@br0: master br0 state forwarding4: veth2@br0: master br0 state forwarding5: veth3@br0: master br0 state forwarding$ bridge fdb show br br0 dynamic(empty)
  • All three ports are in the forwarding state.
  • The forwarding database (MAC table) has no dynamic entries yet.

6.9 Practical: unknown unicast flooding

Terminal
ip netns exec h3 tcpdump -eni eth0 icmpip netns exec h1 ping -c 3 192.168.26.12bridge fdb show br br0 dynamic
Output
$ ip netns exec h1 ping -c 3 192.168.26.123 packets transmitted, 3 received, 0% loss# h3 tcpdump:02:00:00:00:00:01 > 02:00:00:00:00:02  ICMP echo request, id 51, seq 11 packet captured$ bridge fdb show br br0 dynamic02:00:00:00:00:01 dev veth1 master br002:00:00:00:00:02 dev veth2 master br0
  • h1 already knows h2's MAC, the switch does not.
  • h3 captures only the first echo request: unknown unicast, flooded.
  • After the reply the switch knows both h1 and h2; later pings go only to h2's port.

6.10 Practical: broadcast to every port

Terminal
ip netns exec h2 tcpdump -eni eth0 arpip netns exec h1 ping -c 1 192.168.26.13bridge fdb show br br0 dynamic
Output
$ ip netns exec h1 ping -c 1 192.168.26.131 packets transmitted, 1 received, 0% loss# h2 tcpdump:02:00:00:00:00:01 > ff:ff:ff:ff:ff:ff, ARP  Request who-has 192.168.26.13  tell 192.168.26.11$ bridge fdb show br br0 dynamic02:00:00:00:00:01 dev veth1 master br002:00:00:00:00:02 dev veth2 master br002:00:00:00:00:03 dev veth3 master br0
  • h1 does not know h3's MAC, so an ARP request goes to ff:ff:ff:ff:ff:ff.
  • The broadcast is flooded, so h2 sees it too.
  • The table now has a third entry for h3 (veth3).

6.11 Practical: ageing time 0 = hub

Terminal
ip link set br0 type bridge ageing_time 0ip netns exec h3 tcpdump -eni eth0 icmpip netns exec h1 ping -c 3 192.168.26.12
Output
$ ip link set br0 type bridge ageing_time 0$ bridge fdb show br br0 dynamic(empty)# h3 tcpdump (hub mode):..:01 > ..:02 ICMP echo request, seq 1..:02 > ..:01 ICMP echo reply, seq 1..:01 > ..:02 ICMP echo request, seq 2..:02 > ..:01 ICMP echo reply, seq 2..:01 > ..:02 ICMP echo request, seq 3..:02 > ..:01 ICMP echo reply, seq 36 packets captured
  • ageing_time 0: the switch forgets every MAC immediately; the table stays empty.
  • Now h3 captures all 6 packets (3 requests + 3 replies): the bridge behaves exactly like a hub.

6.12 Common mistakes

  • Myth: a switch keeps traffic private. Broadcast and unknown unicast are flooded to every port anyway.
  • MAC flooding attack: an attacker sends thousands of fake MACs, fills the CAM table, and the switch floods new frames like a hub, so the attacker sees everything.
  • Defence: port security (limit MACs per port).
  • Saying a switch splits broadcast domains: without VLANs a switch is one broadcast domain; a router (or VLANs) splits it.

6.13 Interview tips

  • Hub = Layer 1, repeats to all ports, 1 collision domain. Switch = Layer 2, MAC table, each port its own collision domain. Router = Layer 3, routes by IP, separates broadcast domains.
  • 24-port switch, no VLAN: 24 collision domains, 1 broadcast domain.
  • Switch learns from the source MAC, forwards by the destination MAC.
  • Three flood cases: unknown unicast, broadcast, multicast (without IGMP snooping).
  • CAM ageing default is 300 seconds.