Sub topic 5
Trunk ports: carrying many VLANs on one link
This chapter assumes you know how a switch learns MAC addresses and forwards, floods and filters frames. It also assumes you understand access ports, which place untagged end-device traffic into one VLAN, and basic IPv4 subnetting. You also need to be comfortable with the switch command line.
Why a trunk exists
An access port belongs to exactly one VLAN. Suppose two switches each have hosts in VLAN 10 and VLAN 20. If you connect them with access links, you need one cable and one port pair per VLAN. That does not scale. With 40 VLANs you would need 40 links.
A trunk port is a switch port that carries frames for many VLANs over one physical link. The switch needs a way to tell the receiving end which VLAN each frame belongs to. On Ethernet, the standard way is the IEEE 802.1Q tag, a 4-byte field inserted into the frame. Frames for VLANs 10 and 20 share the wire, and the tag keeps them separate.
What happens to a frame
The switch keeps the VLAN of every frame as internal metadata while it forwards the frame. The tag exists only on the wire of a trunk link. The behavior at each port type is:
- A host in VLAN 10 sends a normal untagged Ethernet frame to an access port.
- The switch assigns the frame to VLAN 10 because of the port's configuration. It looks up the destination MAC address in the VLAN 10 MAC table only. It floods unknown unicast and broadcast frames only to ports that are members of VLAN 10.
- If the egress port is an access port in VLAN 10, the switch sends the frame untagged.
- If the egress port is a trunk, the switch inserts an 802.1Q tag with VLAN ID 10 and sends the frame.
- The switch at the far end of the trunk reads the tag, removes it, and treats the frame as VLAN 10 traffic. It then repeats the same decision for its own ports.
Because the MAC table is kept per VLAN, the same MAC address can appear in two VLANs on different ports without conflict.
The tag adds 4 bytes between the source MAC address and the EtherType field. A later chapter covers the tag fields and frame size in detail. For now, remember that a trunk link carries a mix of tagged frames and, at most, one VLAN of untagged frames (the native VLAN, covered in its own chapter).
Configuring a static trunk
The following configuration turns an interface into a trunk that carries only VLANs 10 and 20, uses an unused VLAN 999 as the native VLAN, and does not send negotiation frames.
interface GigabitEthernet0/24
switchport mode trunk
switchport trunk native vlan 999
switchport trunk allowed vlan 10,20
switchport nonegotiateEach line has a specific purpose:
| Command | Effect |
|---|---|
| `switchport mode trunk` | Forces the port to operate as a trunk. It does not depend on what the neighbor sends. |
| `switchport trunk native vlan 999` | Frames in VLAN 999 are sent untagged. Untagged frames received are placed in VLAN 999. |
| `switchport trunk allowed vlan 10,20` | Only VLANs 10 and 20 are permitted on this trunk. |
| `switchport nonegotiate` | Stops the port from sending Dynamic Trunking Protocol (DTP) frames. |
On switches that support both the older Cisco ISL encapsulation and 802.1Q, you must set the encapsulation first, or switchport mode trunk is rejected:
interface GigabitEthernet0/24
switchport trunk encapsulation dot1q
switchport mode trunkMany newer switches support only 802.1Q and do not accept the encapsulation command at all. If the command is not recognized on your platform, skip it.
Configure both ends of the link. A trunk configured on one side only will not work as you expect. One end may form a trunk while the other treats the traffic as an access port, and the VLANs will not pass.
Administrative mode and operational mode
Every switch port has two states that you should keep apart:
- The administrative mode is what you configured, such as
access,trunkordynamic desirable. - The operational mode is what the port is doing now, after any negotiation.
When you force both sides with switchport mode trunk, the two are the same. When you leave a port on a dynamic mode, the operational mode depends on the neighbor. Dynamic negotiation and its risks are covered in the chapter on DTP. The practical rule for this chapter is simple: for links between switches that you manage, set the mode statically and turn negotiation off.
You can view both states with this command:
show interfaces GigabitEthernet0/24 switchportLook at the lines Administrative Mode and Operational Mode. Also check Administrative Trunking Encapsulation and Operational Trunking Encapsulation, plus Trunking VLANs Enabled.
Verifying a trunk
Two commands give most of what you need:
show interfaces trunk
show vlan briefshow interfaces trunk has several sections. Each answers a different question.
| Section | Question it answers |
|---|---|
| Port, Mode, Encapsulation, Status, Native vlan | Is the port trunking, with which encapsulation, and which native VLAN? |
| Vlans allowed on trunk | Which VLANs are permitted by configuration? |
| Vlans allowed and active in management domain | Of those, which exist in the local VLAN database? |
| Vlans in spanning tree forwarding state and not pruned | Which VLANs will actually pass traffic right now? |
A VLAN must appear in all of these places on both switches. If VLAN 20 is allowed on the trunk but was never created on one switch, it is missing from the "allowed and active" list on that switch, and its frames will not be forwarded there. Check show vlan brief for the VLAN's existence.
A trunk is not a VLAN-aware host link by default
A trunk port connected to a regular PC does not work. The PC does not understand tags, so it drops or misreads tagged frames. Trunks are for links to switches, routers, access points that support multiple SSIDs mapped to VLANs, hypervisors and other devices that are VLAN-aware.
A Linux host can be made VLAN-aware by creating a VLAN subinterface:
sudo ip link add link eth0 name eth0.10 type vlan id 10
sudo ip addr add 192.168.10.5/24 dev eth0.10
sudo ip link set eth0.10 upFrames sent out of eth0.10 leave eth0 with a VLAN 10 tag. Tagged frames for VLAN 10 arriving on eth0 are delivered to eth0.10. The switch port facing this host must be a trunk that allows VLAN 10. If it is an access port, tagged frames from the host will usually be dropped.
Router-on-a-stick uses the same principle on a router. One physical interface is split into subinterfaces, each tagged for one VLAN:
interface GigabitEthernet0/0
no shutdown
!
interface GigabitEthernet0/0.10
encapsulation dot1Q 10
ip address 192.168.10.1 255.255.255.0
!
interface GigabitEthernet0/0.20
encapsulation dot1Q 20
ip address 192.168.20.1 255.255.255.0The switch port facing this router must be a trunk that allows VLANs 10 and 20. A later chapter explains inter-VLAN routing fully.
Seeing the tag on the wire
You can inspect the tag in code. The tag starts at byte 12, right after the 6-byte destination and 6-byte source MAC addresses. The first two bytes are the Tag Protocol Identifier (TPID), which is 0x8100 for 802.1Q. The next two bytes hold the priority, drop eligible indicator and VLAN ID.
import struct
def parse_dot1q(frame: bytes):
"""Return the 802.1Q fields of an Ethernet frame, or None if it is untagged."""
tpid, tci = struct.unpack("!HH", frame[12:16])
if tpid != 0x8100:
return None
return {
"pcp": tci >> 13,
"dei": (tci >> 12) & 0x1,
"vid": tci & 0x0FFF,
}If you capture a frame on a trunk with a packet analyzer and run it through this function, VLAN frames return a dictionary and native VLAN frames return None. Capturing on a host access port shows no tags at all, because the switch removes them before sending.
To build a tagged frame for a lab, Scapy can do it:
from scapy.all import Ether, Dot1Q, IP, ICMP
frame = (
Ether(dst="ff:ff:ff:ff:ff:ff")
/ Dot1Q(vlan=10, prio=0)
/ IP(dst="192.168.10.255")
/ ICMP()
)
frame.show()Send this only on a network you own.
Common trunk faults
- One end is a trunk and the other is an access port. The access side drops tagged frames or places untagged ones in a single VLAN. Check
Operational Modeon both ends. - Mismatched native VLAN. Each end puts untagged frames into a different VLAN, so traffic leaks between VLANs. Cisco switches usually log a CDP native VLAN mismatch message.
- VLAN missing from the allowed list. The trunk is up, but one VLAN has no connectivity across it. Compare
show interfaces trunkon both switches. - VLAN not created on one switch. The allowed list is correct, but the VLAN is not active locally.
- Different encapsulation. One side expects ISL and the other 802.1Q. Use 802.1Q on both.
- Spanning tree blocking. The VLAN is allowed and active, but spanning tree puts the port in a blocking state for that VLAN. This shows in the last section of
show interfaces trunk.
Exercise
A trunk between Switch 1 and Switch 2 shows status "trunking". Hosts in VLAN 30 on each switch cannot reach each other, but VLAN 10 hosts can. List the checks you would run, in order, and what each would show.
Show answer
1. Run "show interfaces trunk" on both switches and look at "Vlans allowed on trunk". If VLAN 30 is absent, add it with "switchport trunk allowed vlan add 30". 2. Look at "Vlans allowed and active in management domain". If VLAN 30 is missing on one switch, create it with "vlan 30". 3. Look at the "forwarding state and not pruned" section to see whether spanning tree blocks VLAN 30. 4. Run "show vlan brief" to confirm the host ports are in VLAN 30. 5. Confirm the native VLAN matches on both ends in case untagged traffic is involved.
Exercise
A Linux server uses eth0.10 with VLAN 10, but it cannot reach its gateway. The switch port is configured with "switchport mode access" and "switchport access vlan 10". What is wrong and how do you fix it?
Show answer
The server sends tagged frames, but an access port expects untagged frames and drops tagged ones. Change the port to a trunk with "switchport mode trunk" and "switchport trunk allowed vlan 10", or remove the VLAN subinterface and use an untagged address on eth0 with the access port.