# Role of the Spanning Tree Protocol in a Layer 2 Network

Spanning tree protocol is used in layer 2 networks to prevent broadcast and multicast frames from looping repeatedly due to the presence of redundant paths.

Originally, it was used in Layer 2 Bridges to prevent frames from looping across different segments of the same LAN. Later, when L2 switches replaced L2 bridges, the spanning tree protocol continued to be used in each port of the L2 switch as well.

## The Looping problem

A typical looping of a L2 broadcast frame is shown in the diagram given below:

A L2 Broadcast frame creating a loop in a bridged LAN

When PC-A sends a L2 broadcast frame (F1) on LAN segment 1, it reaches port 1 of bridge 1 and port 1 of bridge 2. Since it is a broadcast frame, bridge 1 sends it out via. port 2, onto LAN segment 2 (F2). This frame (F2) then enters bridge 2 via. port 2 and is forwarded via. port 1 of bridge 2, onto LAN segment 1 (as F4). Similarly, on receiving Frame F1 on port 1, bridge 2 forwards it out of its port 2 (F3) onto LAN segment 2. This frame F3 then enters bridge 1 via. port 2 and is again forwarded via. port 1 of bridge 1 onto LAN segment 1 as frame F5.

Thus a single copy of frame F1 has generated multiple copies of it in both the LAN segments. This process keeps repeating and the LAN segments are flooded with duplicate copies of frame F1 and this traffic would bring down the whole L2 Network. This is the looping problem.

## The Spanning Tree Algorithm

To prevent looping of frames, the spanning tree algorithm was implemented on bridges. The spanning tree algorithm basically identifies redundant or duplicate paths in the networks and blocks them. As the name indicates, it creates a topology such that there is exactly one path (tree) between any two end stations. It also makes sure that there is connectivity between any two nodes on the network (spanning).

A sample topology for the earlier mentioned L2 network, after spanning tree algorithm is run on the two bridges is given in the diagram below:

A loop free L2 network after running the spanning tree protocol

As shown in the diagram, after running the spanning tree algorithm, port 1 of bridge 2 is maintained in a blocking state, thereby preventing the looping of L2 broadcast frames in the network.

Let us take another example to illustrate the core principles of the spanning tree algorithm

A L2 network after running spanning tree algorithm on all the five bridges

The above L2 Network has four LAN segments and five bridges connecting them. The left portion of the diagram shows the  complete topology after running the spanning tree algorithm on all the bridges and the right portion of the diagram shows the effective topology, with blocked ports removed from the diagram. The core spanning tree algorithm consists of the following steps executed through exchange of periodic special messages known as Bridge Protocol Data Units (BPDUs) between the different bridges:

a) One of the bridges is elected as the Root Bridge (RB) and all the LAN traffic has to pass the RB. In this example, bridge B3 has the lowest priority value of 1 and hence is chosen as the RB.

b) For each LAN segment, exactly one bridge is chosen as the Designated Bridge (DB). All traffic from a LAN segment is forcefully sent only via. its DB. In this example, Bridge B1 is chosen as the DB for LAN1, Bridge B3 is chosen as DB for LAN2 and LAN3 and bridge B4 is chosen as the DB for LAN4.

c) For each bridge, the port closest to the RB is chosen as the Root Port (RP). For B1 and B2, their respective port number 2 is chosen as their RP, while for  B4  and B5, their respective port 1 is chosen as their RPs. For B3, which is the RB, all of its ports are chosen as RPs.

d) For each bridge that is a DB for a LAN segment, the port connecting the bridge to the LAN segment is termed as the Designated Port (DP). This makes port 1 of B1 and port 2 of B4 as DPs.

e) After the above steps, only those ports of each bridge that are either RP or DP are maintained in active state. Rest of the ports of each bridge (that are neither RP nor DP) are put in blocked state and administratively shut down. In this example, since port 1 of B2 and port 2 of B5 are neither RPs nor DPs, they both are put in blocked state. This way, the spanning tree algorithm ensures that redundant paths in the L2 network are blocked.

f) The bridges continue to exchange messages in the form of BPDUs periodically. This enables the bridges to detect problems in the network like a Root Bridge or  one of the active ports going down and take alternative actions dynamically and adaptively. If a RB goes down, then another Bridge is dynamically elected as a RB. Similarly, if either a RP or a DP of a Bridge goes down, then this may result in one of the blocked ports of the Bridge being changed to active state, thereby maintaining seamless connectivity in the L2 network.

## Spanning Tree and L2 Switches

L2 Switches have replaced the traditional L2 Bridges in almost all the networks, with each end station having a dedicated point to point connection to one of the switch ports. In such cases, each such connection is a LAN segment by itself. Still the spanning tree algorithm is run on each port of a L2 switch, mainly to make sure that L2 broadcasts and multicasts do not flood the network due to redundant paths between end stations (either through inter-link ports between multiple L2 Switches or the same end station connected to multiple L2 Switches for load balancing and redundancy purposes).