Sub topic 5
Path cost and root path cost
This chapter assumes you know how a switch learns MAC addresses, what a BPDU carries, and how the bridge ID decides the root bridge election. It covers the number that decides which links the tree uses. That number is path cost.
Port cost versus root path cost
Spanning tree uses two related numbers. They are easy to confuse.
- Port cost is a value assigned to a single port. By default it depends on the link speed. Faster links get lower costs.
- Root path cost is the total cost from a switch to the root bridge. It is the sum of the port costs along the best path.
The root bridge advertises a root path cost of 0. Each switch learns its own root path cost from the BPDUs it receives. A switch always prefers the path with the lowest root path cost.
Where the cost is added
A switch adds cost on the port where a BPDU arrives, not on the port where it leaves. The rule has three steps:
- The root sends a BPDU with root path cost 0.
- A neighbor receives it on a port. It adds the cost of that receiving port and gets its own root path cost.
- That switch sends BPDUs out of its designated ports with the new total. Its neighbors add their own receiving-port costs in turn.
The cost of an outgoing port never enters the calculation for that BPDU. It matters only when the neighbor receives something on that link in the other direction. Because of this, you change a switch's choice of path by changing the cost on the port facing the root, not the port on the far side.
Default costs: short and long methods
The original IEEE 802.1D-1998 standard used a short scale of 16-bit values. Links faster than 10 Gb/s did not fit, so IEEE 802.1t and later 802.1D-2004 defined a long scale. The long scale is 20 Tb/s divided by the link speed.
| Link speed | Short cost | Long cost |
|---|---|---|
| 10 Mb/s | 100 | 2,000,000 |
| 100 Mb/s | 19 | 200,000 |
| 1 Gb/s | 4 | 20,000 |
| 10 Gb/s | 2 | 2,000 |
| 100 Gb/s | 1 | 200 |
The short scale cannot tell 10 Gb/s from 100 Gb/s links, because it has no value below 1. Cisco IOS also assigns 2 to 10 Gb/s, so on a short scale every link above 10 Gb/s ties with it.
Cisco IOS defaults to the short method on PVST+ and Rapid PVST+. You can switch to the long method with a global command.
A worked example
Switch R is the root. Switch A connects to R over a 1 Gb/s link. Switch B connects to R over a 100 Mb/s link. Switch C connects to both A and B over 1 Gb/s links. All costs below use the short scale.
Switch A receives R's BPDU on a 1 Gb/s port and gets root path cost 4. Switch B receives it on a 100 Mb/s port and gets 19. Switch C then hears two advertisements:
- From A: 4 advertised, plus 4 for C's receiving port, gives 8.
- From B: 19 advertised, plus 4 for C's receiving port, gives 23.
C picks the port facing A as its root port, with a root path cost of 8. The port facing B is not chosen. Whether it ends up blocking depends on the designated port election on the B-C segment, which is covered in the chapter on port roles and tie-breakers.
Seeing costs in the output
In Cisco IOS output, the Cost line under Root ID is the root path cost. The Cost column in the interface table is each port's own cost. They are different numbers. A typical excerpt from switch C in the example looks like this:
C# show spanning-tree vlan 10
VLAN0010
Spanning tree enabled protocol rstp
Root ID Priority 4106
Address 0011.2233.4401
Cost 8
Port 1 (GigabitEthernet0/1)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32778 (priority 32768 sys-id-ext 10)
Address 0011.2233.4403
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- ----
Gi0/1 Root FWD 4 128.1 P2p
Gi0/2 Altn BLK 4 128.2 P2pThe root path cost is 8 (4 from A's link plus A's own 4). The root port Gi0/1 has a port cost of 4. The root path cost is the number to compare when you suspect a switch chose an unexpected path.
Changing costs
You can override the defaults on a per-port basis. Do this to steer traffic onto a preferred uplink, or to make a lower-speed link the preferred one for a particular VLAN.
! Cisco IOS: use the 802.1t long scale on every switch in the domain
configure terminal
spanning-tree pathcost method long
end
! Override the cost for all VLANs on this port
configure terminal
interface GigabitEthernet0/1
spanning-tree cost 10
end
! Override the cost for one VLAN only
configure terminal
interface GigabitEthernet0/1
spanning-tree vlan 10 cost 10
endOn a Linux bridge, you set the port cost with the bridge tool:
sudo bridge link set dev eth0 cost 100
bridge -d link show dev eth0Setting a lower cost on one uplink makes the switch prefer it. A per-VLAN cost lets you split VLANs across two uplinks, so each link carries traffic for some VLANs and blocks others. This is a common load-balancing technique with per-VLAN spanning tree.
Edge cases and common errors
- Mixed methods. A switch on the short scale and a switch on the long scale produce root path costs that cannot be compared sensibly. A 1 Gb/s link costs 4 on one side and 20,000 on the other. Set the same method on every switch in the domain.
- Sums that exceed the scale. On the short scale, root path cost is limited to 16 bits. A long chain of slow links can overflow it. This is a main reason the long scale exists.
- Port channels. A bundle is one logical port with its own cost, derived from the bundle's bandwidth. On Cisco switches the cost of the bundle may not be recalculated when a member link fails, so a degraded bundle can still look cheap.
- Cost on the wrong side. Raising the cost on the root's own port has no effect on the root's choices, because the root never uses a root port. To influence the path of a downstream switch, change the cost on that switch's receiving port.
- Ties. If two paths have the same root path cost, the switch compares the sender's bridge ID, then the sender's port ID, then its own port ID. Those tie-breakers are covered in the next chapters.
Exercise
Switch R is the root. Switch X connects to R over a 100 Mb/s link. Switch Y connects to X over a 1 Gb/s link, and also connects directly to R over a 10 Gb/s link. Using the short scale (100 Mb/s = 19, 1 Gb/s = 4, 10 Gb/s = 2), what is Y's root path cost, and which port becomes its root port?
Show answer
Through X, Y receives X's advertised cost of 19 and adds 4 for its 1 Gb/s receiving port, giving 23. Directly from R, Y receives cost 0 and adds 2 for its 10 Gb/s receiving port, giving 2. Y's root path cost is 2, and the port on the 10 Gb/s link to R is the root port.
Exercise
A network has some switches on the short path cost method and others on the long method. Why can this cause unexpected paths, and what is the fix?
Show answer
Root path costs from the two scales are added together and compared as if they were the same unit, but a 1 Gb/s link costs 4 on one scale and 20,000 on the other. The fix is to set the same method, for example spanning-tree pathcost method long, on every switch.