Sub topic 1
What a switch reads from an Ethernet frame
Prerequisites
This chapter assumes you know three things:
- A MAC address is a 48-bit hardware address, written as six hex octets such as
00:11:22:33:44:55. It differs from an IP address, which is assigned by the network layer. - An Ethernet frame carries a destination MAC, a source MAC, and an EtherType.
- A hub repeats signals, a switch forwards frames, and a router connects networks.
The switch works at layer 2
A switch makes its forwarding decision from the Ethernet header alone. It does not look at IP addresses, TCP ports, or the payload. This is why a switch needs no IP configuration to forward traffic.
An Ethernet II frame has these fields:
| Field | Size | Purpose |
|---|---|---|
| Preamble and start delimiter | 8 bytes | Lets the receiver synchronize. The hardware strips it. |
| Destination MAC | 6 bytes | Who the frame is for. |
| Source MAC | 6 bytes | Who sent the frame. |
| EtherType | 2 bytes | Which protocol is in the payload, for example `0x0800` for IPv4. |
| Payload | 46 to 1500 bytes | The data being carried. |
| Frame check sequence (FCS) | 4 bytes | A CRC used to detect corruption. |
The two fields that matter
A basic switch uses two fields to build and use its table:
- The source MAC tells the switch which address is reachable through the port the frame arrived on. The switch uses it for learning.
- The destination MAC tells the switch where the frame should go. The switch uses it for the lookup.
The same frame is therefore used twice. The source address updates what the switch knows. The destination address is checked against what the switch already knows.
Fields a switch may also read
Two other fields matter in some setups:
- The VLAN tag. If the frame has an 802.1Q tag, the switch reads the VLAN ID. It uses the ID to pick which VLAN's table to consult. A later chapter covers this.
- The EtherType. A plain switch ignores it. If the value is
0x8100, a VLAN tag follows. Some switches also use it for filtering or QoS rules.
Following one frame
Suppose host A sends a frame to host B through a switch.
The switch performs these steps in order:
- It receives the frame on port 1 and verifies the FCS.
- It reads the source MAC,
00:00:00:00:00:0a, and associates it with port 1. - It reads the destination MAC,
00:00:00:00:00:0b, and looks for it in the table. - It forwards, floods, or filters the frame, depending on the result.
The next chapters cover each outcome.
Reading the fields in code
The following Python script extracts the two addresses from raw frame bytes.
def parse_header(frame: bytes):
"""Return (destination, source, ethertype) from an Ethernet II frame."""
if len(frame) < 14:
raise ValueError("frame shorter than an Ethernet header")
def fmt(b):
return ":".join(f"{x:02x}" for x in b)
dst = fmt(frame[0:6])
src = fmt(frame[6:12])
ethertype = int.from_bytes(frame[12:14], "big")
return dst, src, ethertype
raw = bytes.fromhex("00000000000b" "00000000000a" "0800") + b"payload"
dst, src, etype = parse_header(raw)
print(dst, src, hex(etype))The output is 00:00:00:00:00:0b 00:00:00:00:00:0a 0x800.
Common mistakes
- Mixing up source and destination. Learning uses the source. Lookup uses the destination.
- Expecting a switch to read IP addresses. A layer 2 switch forwards on MAC addresses only.
- Thinking the table holds a hostname or IP address. It holds MAC addresses, which are tied to ports.
Exercise
A frame arrives on port 3 with source MAC 00:00:00:00:00:0c and destination MAC 00:00:00:00:00:0d. Which field does the switch use to record that ...0c is reachable through port 3, and which field does it use to decide where to send the frame?
Show answer
The source MAC is used to learn that 00:00:00:00:00:0c is reachable through port 3. The destination MAC, 00:00:00:00:00:0d, is used for the table lookup that decides where to send the frame.
Exercise
Why can a switch forward frames without having an IP address, and which header fields does a basic switch rely on?
Show answer
A basic switch forwards at layer 2 using only the Ethernet header. It relies on the source MAC for learning and the destination MAC for lookup. It also checks the FCS, and it reads the 802.1Q tag when one is present. It never reads IP addresses, so it needs no IP configuration to forward traffic.