Switches, learning bridges and VLANs
Connecting LAN segments at the data link layer: hubs, bridges, switches and routers compared, collision domains and broadcast domains, how a learning bridge builds its address table by flooding, forwarding and filtering, the spanning tree protocol that removes loops (root bridge, root ports, designated ports, blocked ports) and VLANs with the 802.1Q tag.
Why it exists
One shared Ethernet segment is one collision domain: every station hears every frame and they all compete for the cable. To grow a LAN, or to connect several, we need devices that separate the segments at the data link layer and forward frames only where they are needed. A learning bridge (a switch is a bridge with many ports) does it without being configured: it learns which station is on which port by watching the source addresses. But redundant links make loops, and a loop makes broadcast frames circle for ever; the spanning tree protocol switches off enough links to leave a tree. A VLAN then splits one physical switch into several logical LANs. GATE asks which device does what, how many collision and broadcast domains a drawing has, how a learning bridge acts on a sequence of frames, and what a spanning tree looks like.
Prerequisites
You need the Ethernet frame (source and destination MAC addresses, the broadcast address ff:ff:ff:ff:ff:ff) and CSMA/CD from Ethernet and the collision domain of medium access.
Precise definitions
Devices by layer.
| Device | Layer | Decides by | Collision domains | Broadcast domains |
|---|---|---|---|---|
| repeater, hub | 1 (physical) | nothing: copies bits to every port | one for all ports | one |
| bridge, switch | 2 (data link) | the destination MAC address | one per port | one (all ports) |
| router | 3 (network) | the destination IP address | one per port | one per port |
A collision domain is the set of stations whose frames can collide. A broadcast domain is the set of stations that receive a broadcast frame. A hub extends a collision domain; a switch splits collision domains (and with full-duplex links to each station, no collision occurs at all, so CSMA/CD is not even used) but passes broadcasts to every port; a router blocks broadcasts.
The learning bridge. The bridge keeps an address table (MAC address, port) and for every arriving frame does:
- Learn: record (source address, arrival port), replacing an older entry (entries expire after an ageing time).
- Decide on the destination:
- broadcast, or not in the table: flood: send out of every port except the arrival port;
- in the table on the same port the frame came in: filter: discard (sender and receiver are on the same segment);
- in the table on another port: forward to that port only.
A bridge is transparent: the stations do not know it exists. A switch may forward store-and-forward (receive the whole frame, check the CRC, then send) or cut-through (start as soon as the destination address is read).
Loops and the spanning tree. Redundant links between bridges create loops. A broadcast or a frame to an unknown destination is flooded, goes round the loop and comes back, multiplying: a broadcast storm; address tables also flap as the same source appears on two ports. The spanning tree protocol (STP, IEEE 802.1D) has the bridges exchange control messages (BPDUs) and block some ports so that the active topology is a tree. The rules (with a cost of 1 per hop):
- The bridge with the lowest bridge ID is the root bridge.
- Every other bridge picks its root port: the port with the lowest root path cost (ties: the neighbouring bridge with the lower ID, then the lower port number).
- On every LAN the bridge with the lowest root path cost (ties: the lower ID) is the designated bridge and forwards on that LAN through its designated port.
- Ports that are neither root nor designated are blocked: they receive but do not forward data frames.
The result: root ports and designated ports forward, forming a spanning tree of the bridges and LANs; if a link fails the protocol recomputes. (Real STP ports pass through the states blocking, listening, learning and forwarding, which takes tens of seconds; rapid STP is faster.)
VLANs. A virtual LAN divides the ports of one or more switches into separate logical LANs: each VLAN is its own broadcast domain (and normally its own IP subnet). Frames cannot pass between VLANs at layer 2; a router (or a layer-3 switch) connects them. An access port belongs to one VLAN. A trunk port carries the frames of several VLANs between switches, each frame carrying an IEEE 802.1Q tag of 4 bytes inserted after the source address: the tag protocol identifier 0x8100 (16 bits), then 3 bits of priority, 1 drop-eligible bit and the 12-bit VLAN ID. Identifiers 0 and 4095 are reserved, so VLANs can be used. The tag lengthens the frame: the maximum becomes 1522 bytes (from 1518) and the minimum 68 (from 64). VLANs give broadcast containment, security and the freedom to place a group of users anywhere.
How it works
A learning switch with four ports; hosts A (port 1), B (port 2), D (port 4), and C and E sharing a hub on port 3. Watch the address table fill and the three actions: flood, forward, filter (frame 4: E and C are on the same port, so the switch does not forward it).
A learning switch: four ports, hosts A, B, C, E, D (C and E share a hub on port 3): start
Computed by a script, not typed| frame | action | address table afterwards (host:port) |
|---|---|---|
| 1. A → B, arrives on port 1 | ||
| 2. B → A, arrives on port 2 | ||
| 3. C → A, arrives on port 3 | ||
| 4. E → C, arrives on port 3 | ||
| 5. A → E, arrives on port 1 | ||
| 6. D → broadcast, arrives on port 4 | ||
| 7. B → D, arrives on port 2 |
Step 0
Rules: learn the source address and its port from every frame; look the destination up; flood if unknown or broadcast; filter if it is on the arrival port; otherwise forward to that port only. Simulated by scripts/cn_t2.py (Bridge).
Counting domains. A hub, a switch, and a network with a hub, two switches and a router.
Collision domains and broadcast domains: start
Computed by a script, not typed| network | collision domains | broadcast domains |
|---|---|---|
| four hosts on one hub | ||
| four hosts on one switch | ||
| hub (2 hosts) and a host on a switch, a router, a switch with 2 hosts |
Step 0
A hub and a repeater extend a collision domain; a switch (bridge) splits collision domains but not broadcast domains; a router splits both. Counted by script from the links.
The spanning tree. Five bridges B1 to B5 and six LANs A to F; B1 has the lowest ID and is the root. Each other bridge picks its root port; on a LAN served by a nearer bridge the other bridges' ports are blocked.
Spanning tree: root, root ports and blocked ports of five bridges and six LANs: start
Computed by a script, not typed| bridge | root path cost | root port | port roles (LAN) |
|---|---|---|---|
| bridge B1 (root) | |||
| bridge B2 | |||
| bridge B3 | |||
| bridge B4 | |||
| bridge B5 |
Step 0
Designated bridge of each LAN: A: B1, B: B1, C: B2, D: B2, E: B3, F: B4. Rules: lowest bridge ID is the root; each other bridge forwards on the port with the lowest root path cost (ties: lower neighbour ID, lower port); on each LAN the bridge with the lowest root path cost (ties: lower ID) is designated; the rest block. Each hop costs 1.
The 802.1Q tag, byte by byte.
The IEEE 802.1Q VLAN tag: start
Computed by a script, not typed| tag | bytes | TPID | priority, DEI, VLAN id (bits) |
|---|---|---|---|
| priority 0, VLAN 10 | |||
| priority 3, VLAN 100 | |||
| priority 7, VLAN 4094 | |||
| priority 0, VLAN 1 | |||
| usable VLAN identifiers | |||
| frame sizes |
Step 0
Built with struct.pack and decoded again. Only the standard numbers are quoted (0x8100, 12 bits, 4 bytes); everything else is computed.
Broadcasts inside VLANs. A six-port switch with VLANs 10 (ports 1, 2), 20 (ports 3, 4), 30 (port 5) and a trunk (port 6).
VLANs: where does a broadcast go?: start
Computed by a script, not typed| event | sent out of ports | belongs to |
|---|---|---|
| a host on port 1 (VLAN 10) broadcasts | ||
| a host on port 3 (VLAN 20) broadcasts | ||
| the only host of VLAN 30 (port 5) broadcasts | ||
| a tagged frame for VLAN 20 arrives on the trunk, port 6 |
Step 0
Each VLAN is a separate broadcast domain (and has its own IP subnet); an access port belongs to one VLAN, a trunk port carries several, tagged.
Worked examples
EX 1Collision and broadcast domains
EasyFour hosts on a hub: one collision domain, one broadcast domain. Four hosts on a switch: four collision domains (one per port), one broadcast domain. Add a router between two switches: the broadcast domain splits in two, and the two links to the router are two more collision domains. In the network of the table (a hub with 2 hosts and a host on switch S1, a router, a switch S2 with 2 hosts) the collision domains are: the hub with its two hosts and the switch port they share; h3's port; the S1-router link; the router-S2 link; the port of h4; the port of h5: six; the broadcast domains are the two sides of the router: two. The rule: count a switch or router port as the edge of a domain, a hub as no edge at all, and cut broadcast domains only at routers.
EX 2A learning switch in C
MediumThe program is the algorithm: a table, learn, then flood, forward or filter. It runs the seven frames of the animation (C and E on a hub on port 3).
#include <stdio.h>
#include <string.h>
#define PORTS 4
#define MAXHOST 8
static char host[MAXHOST];
static int where[MAXHOST], nhost;
static int lookup(char h) {
for (int i = 0; i < nhost; i++)
if (host[i] == h) return where[i];
return 0;
}
static void learn(char h, int port) {
for (int i = 0; i < nhost; i++)
if (host[i] == h) { where[i] = port; return; }
host[nhost] = h;
where[nhost++] = port;
}
static void frame(char src, char dst, int in_port) {
learn(src, in_port);
int out = dst == '*' ? 0 : lookup(dst); /* '*' is the broadcast address */
printf("%c -> %c on port %d: ", src, dst, in_port);
if (out == 0) {
printf("flood to ports");
for (int p = 1; p <= PORTS; p++)
if (p != in_port) printf(" %d", p);
printf("\n");
} else if (out == in_port) {
printf("filter (same port)\n");
} else {
printf("forward to port %d\n", out);
}
}
int main(void) {
int port_of[128] = {0};
port_of['A'] = 1; port_of['B'] = 2; port_of['C'] = 3; port_of['E'] = 3; port_of['D'] = 4;
const char pairs[7][2] = {{'A', 'B'}, {'B', 'A'}, {'C', 'A'}, {'E', 'C'}, {'A', 'E'}, {'D', '*'}, {'B', 'D'}};
for (int i = 0; i < 7; i++) frame(pairs[i][0], pairs[i][1], port_of[(int)pairs[i][0]]);
printf("table:");
for (int p = 1; p <= PORTS; p++)
for (int i = 0; i < nhost; i++)
if (where[i] == p) printf(" %c:%d", host[i], p);
printf("\n");
return 0;
}A -> B on port 1: flood to ports 2 3 4
B -> A on port 2: forward to port 1
C -> A on port 3: forward to port 1
E -> C on port 3: filter (same port)
A -> E on port 1: forward to port 3
D -> * on port 4: flood to ports 1 2 3
B -> D on port 2: forward to port 4
table: A:1 B:2 C:3 E:3 D:4Reading it. Frame 1: A is not known as a destination (B has not spoken), so the switch learns A on port 1 and floods to ports 2, 3, 4. Frame 2: B answers; the switch learns B and forwards to port 1 only. Frame 4: E sends to C; both are on port 3, so after learning E the switch filters the frame: it would have been heard by C already on the shared segment. Frame 6: a broadcast is always flooded. At the end the table holds five hosts, with C and E sharing port 3: one port can have several addresses behind it.
EX 3The spanning tree of five bridges
MediumBridge B1 has the lowest ID: it is the root, and its ports on LANs A and B are designated. B2 (on A, C and D) and B3 (on B, C and E) are one hop away: root ports towards A and B. LAN C is shared by B2 and B3, both with root path cost 1: the lower ID, B2, is designated, so B3's port on C is blocked. B4 (on D, E, F) is two hops from the root through D (via B2) or through E (via B3): equal cost, so the neighbour with the lower ID (B2) decides, and the root port is the one on D; B4's port on E is blocked (B3 is designated on E at cost 1) and its port on F is designated (B4 and B5 both have cost 2, the lower ID wins). B5 (on E and F) reaches the root through E (B3, cost 1) and has cost 2; its port on F is blocked. Three ports are blocked and the forwarding ports connect all 11 nodes (5 bridges and 6 LANs) with 10 links: a tree. To answer a question about a given figure: find the root, give every bridge its cheapest path (breaking ties by lower neighbouring ID, then lower port), pick the designated bridge of each LAN, and block the rest.
EX 4An 802.1Q tag
MediumThe tag for priority 3 on VLAN 100 is 81 00 60 64: the first two bytes are the tag protocol identifier 0x8100; the next 16 bits are : priority , drop-eligible bit 0, VLAN ID . With a 12-bit ID there are 4096 values; 0 and 4095 are reserved, so 4094 VLANs can be configured. The maximum frame grows from 1518 to 1522 bytes. Frames of VLAN 10 on port 1 go out of port 2 (same VLAN) and the trunk (tagged), never to ports of VLAN 20; hosts of different VLANs reach each other only through a router.
EX 5Which statement is true?
GATE levelA switch forwards a frame by its destination IP address. False: by the destination MAC address. A switch has one collision domain per port and one broadcast domain. True (without VLANs). A hub and a switch both break a collision domain. False: only the switch does. A bridge learns the destination address from the first frame sent to it. False: it learns a station's address from the frames the station sends (source address); until it has seen one it floods frames addressed to it. A router, not a switch, connects two VLANs. True. With STP, the number of active links between bridges and the LANs forms a tree. True: links for bridges and LANs. A frame whose destination is on the same port it arrived on is forwarded to that port again. False: it is filtered. Ethernet switching makes the minimum frame size of CSMA/CD unnecessary on full duplex links. True: there are no collisions.
Cheat box
Cheat box
- Hub: layer 1, one collision domain. Switch / bridge: layer 2, one collision domain per port, one broadcast domain. Router: layer 3, splits broadcast domains.
- Learning bridge: learn the source; destination broadcast or unknown flood (all ports except the arrival port); same port filter; else forward.
- STP: lowest ID root; root port cheapest path to the root; one designated bridge per LAN (lowest cost, then ID); the rest blocked; result is a tree.
- VLAN: separate broadcast domains; access port (one VLAN) and trunk (tagged); 802.1Q tag 4 bytes: 0x8100, 3-bit priority, 1 DEI bit, 12-bit VLAN ID; 4094 usable; max frame 1522.
- Inter-VLAN traffic needs a router.
Traps and GATE patterns
Connections
Across subjects
No cross-subject links yet.
A bridge works with the MAC addresses of Ethernet and replaces the shared cable and the collisions of medium access by one collision domain per port. ARP broadcasts are flooded by the switch to its whole broadcast domain, which is what VLANs contain; a VLAN is usually one IP subnet (IPv4 addressing), and a router joins them.
PYQs
6 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 4 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider the diagram shown below where a number of LANs are connected by (transparent) bridges. In order to avoid packets looping through circuits in the graph, the bridges organize themselves in a spanning tree. First, the root bridge is identified as the bridge with the least serial number. Next, the root sends out (one or more) data units to enable the setting up of the spanning tree of shortest paths from the root bridge to each bridge. Each bridge identifies a port (the root port) through which it will forward frames to the root bridge. Port conflicts are always resolved in favour of the port with the lower index value. When there is a possibility of multiple bridges forwarding to the same LAN (but not through the root port), ties are broken as follows: bridges closest to the root get preference and between such bridges, the one with the lowest serial number is preferred. For the given connection of LANs by bridges, which one of the following choices represents the depth first traversal of the spanning tree of bridges? COMPUTER NETWORK
- (a)B1, B5, B3, B4, B2
- (b)B1, B3, B5, B2, B4
- (c)B1, B5, B2, B3, B4
- (d)B1, B3, B4, B5, B2
Consider the correct spanning tree for the previous question. Let host H1 send out a broadcast ping packet. Which of the following options represents the correct forwarding table on B3?
- (a)
- (b)
- (c)
- (d)
In a network of LANs connected by bridges, packets are sent from one LAN to another through intermediate bridges. Since more than one path may exist between two LANs, packets may have to be routed through multiple bridges. Why is the spanning tree algorithm used for bridge-routing?
- (a)For shortest path routing between LANs
- (b)For avoiding loops in the routing paths
- (c)For fault tolerance
- (d)For minimizing collisions
Node X has a TCP connection open to node Y. The packets from X to Y go through an intermediate IP router R. Ethernet switch S is the first switch on the network path between X and R. Consider a packet sent from X to Y over this connection. Which of the following statements is/are TRUE about the destination IP and MAC addresses on this packet at the time it leaves X?
- (a)The destination IP address is the IP address of Y
- (b)The destination MAC address is the MAC address of Y
- (c)The destination MAC address is the MAC address of S
- (d)The destination IP address is the IP address of R COMPUTER NETWORK
Consider that 15 machines need to be connected in a LAN using 8-port Ethernet switches. Assume that these switches do not have any separate uplink ports. The minimum number of switches needed is _______.
In the diagram shown below, L1 is an Ethernet LAN and L2 is a Token-Ring LAN. An IP packet originates from sender S and traverses to R, as shown. The links within each ISP and across the two ISPs, are all point-to-point optical links. The initial value of the TTL field is 32. The maximum possible value of the TTL field when R receives the datagram is __________.
Self-check
Question 1
How many collision domains and broadcast domains has a network of one 8-port switch with 8 hosts?
Show answerHide
8 collision domains (one per port), 1 broadcast domain.
Question 2
A bridge with an empty table receives a frame from X on port 2 for Y. What does it do, and what does it know afterwards?
Show answerHide
It learns X on port 2, then floods the frame to all ports except 2 (Y is unknown). It does not yet know where Y is.
Question 3
Bridges B7, B3, B9 are connected in a triangle by three LANs. Which is the root, and how many ports are blocked?
Show answerHide
B3 (lowest ID) is the root. The triangle has one loop, so exactly one port is blocked: on the LAN between the two non-root bridges B7 and B9, at B9 (the higher ID; B7 is the designated bridge of that LAN).
Question 4
What is the size of a tagged Ethernet frame at most, and how many VLANs can be used?
Show answerHide
1522 bytes; 4094 VLANs (IDs 1 to 4094).
Question 5
Can host A in VLAN 10 send a frame to host B in VLAN 20 through the switch alone?
Show answerHide
No: the switch never forwards between VLANs; the traffic must go through a router (or a layer-3 switch).
Revision card
One-screen revision
- Hub L1 (one collision domain); switch L2 (a collision domain per port); router L3 (splits broadcast domains).
- Learning bridge: learn source, flood unknown / broadcast, filter same port, forward otherwise.
- STP: lowest ID root, root ports, one designated bridge per LAN, others blocked; forwarding links form a tree.
- VLAN: broadcast domains; trunk with 802.1Q tag (4 bytes, 0x8100, 12-bit ID, 4094 usable, frame 1522 max); a router joins VLANs.
References
- ·Kurose, Ross, Computer Networking: A Top-Down Approach (8th edition), Chapter 6, Section 6.4 (Switched local area networks: link-layer switches, VLANs)
- ·Tanenbaum, Wetherall, Computer Networks (5th edition), Chapter 4, Section 4.8 (Data link layer switching)
- ·Forouzan, Data Communications and Networking (5th edition), Chapter 17 (Connecting LANs, backbone networks, and virtual LANs)
Verification
The learning bridge, the spanning tree protocol and the VLAN switch are simulated (scripts/cn_t2.py); scripts/enum-checks/cn_t2_check.py compares the bridge with a second implementation on 300 random runs (never out of the arrival port, filtering on the same segment), checks the spanning tree on 400 random bridge networks (root path costs equal breadth-first distances, one root port per non-root bridge, one designated bridge per LAN, and the forwarding ports form a spanning tree of the bridge-LAN graph), the collision and broadcast domain counts against a port-level model on 500 random device trees, every one of the 65536 802.1Q tags (priority, drop bit, VLAN id) and that a VLAN flood never leaves its VLAN; tests/cn-t2-traces.test.ts recomputes the tag bytes and the domain counts. The C program on the page simulates the learning switch and the check compares its output with the table. The spanning tree here uses a cost of 1 per hop and the rules lowest bridge ID, lowest root path cost, lowest sender ID, lowest port number; real bridges use bridge priority plus MAC address, port costs derived from the speed (for example 19 for 100 Mbps) and timers (listening and learning states of 15 seconds each), quoted from memory and not simulated. The page does not reproduce the bridge figure of any exam question. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (CRC remainders, binomial tail, checksums); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)