> For the complete documentation index, see [llms.txt](https://jncia-workbook.gitbook.io/workbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jncia-workbook.gitbook.io/workbook/junos-switching/2-spanning-tree.md).

# 2) Spanning-Tree

### Pre-Work

Load the config called **lab2.cfg** using the **load\_config\_on\_nodes.py** script.

```
python3 load_config_on_nodes.py --lab_dir switched-lan --config_filename lab2.cfg
```

### Lab

<figure><img src="/files/S04zEMEcISAMR7Nah70H" alt=""><figcaption></figcaption></figure>

The basic L2 switching that you completed in the previous lab is already pre-configured.

Our goal is to safely add trunking to SW1. Because this will form a physical loop, enable RSTP on all three switches as follows:

* SW1 should be the root bridge with priority 4096
* All ports on SW1 should automatically participate in RSTP, even if more trunk ports are added in the future
* SW2 and SW3 `ge-0/0/2` and `ge-0/0/3` should be edge ports
* There should be one RSTP topology no matter how many VLANs there are

Once RSTP is configured, configure the SW1-SW2 and SW1-SW3 links as trunks. Ensure you define the VLANs on SW1 as well. (This is already preconfigured on SW2 and SW3).

### Answer

<details>

<summary>Expand to reveal</summary>

Configure RSTP:

```
#SW1
set protocols rstp interface all
set protocols rstp bridge-priority 4096

#SW2 and SW3
set protocol rstp interface ge-0/0/0
set protocol rstp interface ge-0/0/1
set protocol rstp interface ge-0/0/2 edge
set protocol rstp interface ge-0/0/3 edge
```

\
Configure trunk ports:

```
#SW1
set vlans PROD vlan-id 100
set vlans DEV vlan-id 200
set interfaces ge-0/0/2.0 family ethernet-switching interface-mode trunk 
set interfaces ge-0/0/2.0 family ethernet-switching vlan members [ PROD DEV ]  
set interfaces ge-0/0/3.0 family ethernet-switching interface-mode trunk 
set interfaces ge-0/0/3.0 family ethernet-switching vlan members [ PROD DEV ] 

#SW2 and SW3
set interfaces ge-0/0/0.0 family ethernet-switching interface-mode trunk 
set interfaces ge-0/0/0.0 family ethernet-switching vlan members [ PROD DEV ]
```

</details>

### Explanation

<details>

<summary>Expand to reveal</summary>

It seems that by default, spanning-tree is not enabled on Junos switches. It's possible that this depends on the platform you are using, I am not certain. However, I did verify that without RSTP explicitly enabled, I was able to form a data plane loop in my lab. This does seem strange to me, as typically STP is enabled by default on switches to protect against accidental loops.

\
The "default" mode for spanning-tree on Junos would be RSTP. If you want to use STP you must use the `force-version` command under RSTP. This forces RSTP verison 0 which is compatible with 802.1D (classic STP). In RSTP "version 0," the switch sends 802.1D BPDUs instead of 802.1w BPDUs. Version 0 normally happens automatically when the switch receives an 802.1D BPDU from a connected switch.

```
set protocols rstp force-version stp
```

\
We enable RSTP on a port-by-port basis. We can either use `interface all`, or specify individual interfaces. (`Interface all` is done on SW1 in this lab to fulfill the requirement that all interfaces automatically have spanning-tree enabled). To enable "portfast" functionality (in Cisco terminology), we use RSTP edge ports.

\
Bridge priority is configured with the `bridge-priority` command. Note that we can also use a shorthand such as `4k`, `8k`, etc. instead of remembering the exact power of 2.

```
set protocols rstp bridge-priority 4k
```

* The switch will really use 4096 when we configure `4k`.

\
SW2 and SW3 should see that SW1 is the root bridge:

```
admin@SW2> show spanning-tree bridge 
STP bridge parameters 
Routing instance name               : GLOBAL
Context ID                          : 0
Enabled protocol                    : RSTP
  Root ID                           : 4096.2c:6b:f5:78:ad:d0
  Root cost                         : 20000
  Root port                         : ge-0/0/0
  Hello time                        : 2 seconds
  Maximum age                       : 20 seconds
  Forward delay                     : 15 seconds
  Message age                       : 1 
  Number of topology changes        : 3
  Time since last topology change   : 7 seconds
  Local parameters 
    Bridge ID                       : 32768.2c:6b:f5:cf:e5:d0
    Extended system ID              : 0
```

\
One port on either SW2 or SW3 should be blocking:

```
admin@SW2> show spanning-tree interface    

Spanning tree interface parameters for instance 0

Interface                  Port ID    Designated         Designated         Port    State  Role
                                       port ID           bridge ID          Cost
ge-0/0/1                   128:490      128:490  32768.2c6bf5162bd0        20000    BLK    ALT  
ge-0/0/2                   128:491      128:491  32768.2c6bf5cfe5d0        20000    FWD    DESG 
ge-0/0/3                   128:492      128:492  32768.2c6bf5cfe5d0        20000    FWD    DESG 
ge-0/0/0                   128:493      128:491   4096.2c6bf578add0        20000    FWD    ROOT 

```

On SW2 and SW3, `ge-0/0/2` and `ge-0/0/3` should be edge ports:

```
admin@SW2> show spanning-tree interface ge-0/0/2 detail    

Spanning tree interface parameters for instance 0

Interface name                 : ge-0/0/2
Port identifier                : 128.491
Designated port ID             : 128.491
Port cost                      : 20000
Port state                     : Forwarding
Designated bridge ID           : 32768.2c:6b:f5:cf:e5:d0
Port role                      : Designated
Link type                      : Pt-Pt/EDGE         <-------------
Boundary port                  : NA 
```

\
Note that this is not "per-vlan" spanning-tree. There is only one single instance. To do "per-vlan" spanning-tree you would use `set protocols vstp` instead of `set protocols rstp`. Typically this is only done when you have to interoperate with Cisco equipment that is running per-vlan spanning-tree.

</details>

### Further Reading

<https://www.juniper.net/documentation/us/en/software/junos/stp-l2/topics/topic-map/spanning-tree-configuring-rstp.html>

<https://www.youtube.com/watch?v=3vDYqQbR77M>

<https://www.juniper.net/documentation/us/en/software/junos/stp-l2/stp-l2.pdf>

<https://www.reddit.com/r/networking/comments/rwp6jh/juniper_stp_logic/>
