Your Cart

Guide for VLAN in SONiC

Solution Brief By

Learn how to create and manage VLANs in SONiC.

VLANs divide one switched Ethernet network into separate broadcast domains, one for each VLAN. Use separate VLANs when departments or applications need to share switches while keeping their Layer 2 traffic apart. IP traffic between them needs routing, which this lab does not configure.

In this lab, each host connects to an access port, which sends and receives untagged frames for one VLAN. The trunk between the switches carries both VLANs. On the trunk, each frame from either VLAN has an IEEE 802.1Q tag with its VLAN ID. A trunk can also carry an untagged VLAN if configured to do so, but this lab tags both.

About This Lab

This guide follows Humza Altaf’s GNS3 lab using community SONiC 202205. The original PDF is revision 1.0, dated 26 August 2023. The outputs below are from that lab, which has not been re-run for this page.

The original switch and host commands are preserved. Step 2 adds an IPv6 check, the save that the PDF recommends and an address re-check. Step 3 adds a VLAN check after creating the VLANs. Step 5 adds verification and a save on both switches. Step 6 corrects the PC1 address command and supplies the missing PC2, PC3 and PC4 commands. The connectivity section adds reverse pings and MAC-table checks, and the final section adds a reboot check and an isolation test. Summaries and excerpts are labeled where they appear.

Topology and Port Mapping

S1 and S2 connect through Ethernet8, which carries VLANs 100 and 200 tagged. PC1 and PC3 use untagged access ports in VLAN 100, while PC2 and PC4 use untagged access ports in VLAN 200.

S1S2PC1PC2PC3PC4Ethernet8Ethernet8VLAN 100 and 200 taggedEthernet0Ethernet4Ethernet0Ethernet4192.168.100.2/24VLAN 100 untagged192.168.200.2/24VLAN 200 untagged192.168.100.3/24VLAN 100 untagged192.168.200.3/24VLAN 200 untagged
Ethernet8 connects S1 to S2 and carries VLANs 100 and 200 tagged. PC1 and PC3 connect to Ethernet0 on S1 and S2 in VLAN 100. PC2 and PC4 connect to Ethernet4 on S1 and S2 in VLAN 200.
ConnectionGNS3 PortsSONiC Interfaces
Inter-switch trunkS1 e2 to S2 e2Ethernet8 to Ethernet8
PC1 access linkPC1 e0 to S1 e0S1 Ethernet0
PC2 access linkPC2 e0 to S1 e1S1 Ethernet4
PC3 access linkPC3 e0 to S2 e0S2 Ethernet0
PC4 access linkPC4 e0 to S2 e1S2 Ethernet4
GNS3 InterfaceSONiC Interface
Ethernet 0Ethernet0
Ethernet 1Ethernet4
Ethernet 2Ethernet8
Ethernet 3Ethernet12
HostAddressVLANSwitch Access Port
PC1192.168.100.2/24100S1 Ethernet0, untagged
PC2192.168.200.2/24200S1 Ethernet4, untagged
PC3192.168.100.3/24100S2 Ethernet0, untagged
PC4192.168.200.3/24200S2 Ethernet4, untagged

Use separate consoles for both switches and all four hosts. Ethernet12 is included in the mapping for reference and is not connected in this topology. The SONiC names are written as SONiC prints them, without the space in the PDF table.

Step 1. Check the Interfaces on S1

Start on S1 and confirm that Ethernet0, Ethernet4 and Ethernet8 are administratively and operationally up. These ports connect to PC1, PC2 and S2 respectively.

show interfaces status

Historical output is summarized below using selected rows and columns from the screenshot. All three connected interfaces were up.

InterfaceSpeedMTUVlanOperAdmin
Ethernet01G9100routedupup
Ethernet41G9100routedupup
Ethernet81G9100routedupup

Administrative status shows whether the port is enabled. If the operational state is down, check the cable or GNS3 connection and the interface at the other end.

In SONiC 202205, routed in the Vlan column means only that the port is not a VLAN member. The addresses are checked in Step 2.

Step 2. Remove the Routed Interface Addresses

The SONiC virtual switch first boots with the t1 sample configuration for its default Force10-S6000 hardware SKU, which gives each port a /31 address. SONiC 202205 will not add a port to a VLAN while it is still a routed interface, so remove the addresses first.

show ip interfaces

Only the three ports used here are included in this historical summary of address and state fields. The screenshot also contains Ethernet12 and Ethernet16. Its BGP neighbor names, ARISTA01T2 to ARISTA05T2, also come from the t1 preset.

InterfaceIPv4 address/maskAdmin/Oper
Ethernet010.0.0.0/31up/up
Ethernet410.0.0.2/31up/up
Ethernet810.0.0.4/31up/up

The IPv6 check below is an addition to the PDF. Look for configured IPv6 addresses on the same three ports before continuing.

show ipv6 interfaces

Remove the three IPv4 addresses recorded in the lab. If your starting configuration differs, remove the addresses actually assigned to those interfaces.

sudo config interface ip remove Ethernet0 10.0.0.0/31
sudo config interface ip remove Ethernet4 10.0.0.2/31
sudo config interface ip remove Ethernet8 10.0.0.4/31

Remove any configured IPv6 address on these ports with the same config interface ip remove command. An automatic fe80:: link-local address that you did not configure can stay.

The PDF recommends saving after every two or three commands, so save the configuration here.

sudo config save -y

Recheck the ports with the added block below. Ethernet0, Ethernet4 and Ethernet8 should no longer appear in show ip interfaces. In show ipv6 interfaces, they should show only an fe80:: link-local address or not appear at all. The t1 preset adds no IPv6 addresses, so this check matters only if the configuration has been changed.

show ip interfaces
show ipv6 interfaces

Step 3. Create VLANs 100 and 200

Display the VLAN table before creating VLANs 100 and 200. The historical output has column headings and borders but no VLAN rows.

show vlan brief

Create the two VLANs with the original commands below. Adding a VLAN does not assign an IP address or add member ports.

sudo config vlan add 100
sudo config vlan add 200

Repeat the display command here as an added check. Both VLAN IDs should exist before assigning their members.

show vlan brief

Step 4. Assign the Access Ports and Trunk

Make Ethernet0 an untagged member of VLAN 100 and Ethernet4 an untagged member of VLAN 200. Add Ethernet8 to both as a tagged member so the same link carries traffic for each.

sudo config vlan member add -u 100 Ethernet0
sudo config vlan member add -u 200 Ethernet4
sudo config vlan member add 100 Ethernet8
sudo config vlan member add 200 Ethernet8
show vlan brief

The block above joins the member commands and the display command from two separate PDF screenshots. The historical summary below keeps the VLAN ID, IP Address, Ports and Port Tagging columns and leaves out Proxy ARP and DHCP Helper Address.

VLAN IDIP AddressPortsPort Tagging
100Ethernet0untagged
100Ethernet8tagged
200Ethernet4untagged
200Ethernet8tagged

Confirm that the access ports are untagged and Ethernet8 is tagged in both VLANs. The blank address fields match this Layer 2 lab, which does not configure VLAN gateway interfaces.

Step 5. Configure S2

Repeat Steps 1 through 4 on S2. Its Ethernet0 connects to PC3, Ethernet4 connects to PC4 and Ethernet8 connects to S1.

Check S2’s addresses before removing them. Its configuration may have changed since the virtual image first booted.

Verification on S2 is an addition at this point. Run the commands below on both switches and compare the membership with the Step 4 table.

show interfaces status
show ip interfaces
show ipv6 interfaces
show vlan brief

After Step 4, show interfaces status shows trunk for Ethernet0, Ethernet4 and Ethernet8, because SONiC 202205 prints trunk for any VLAN member. Use show vlan brief to check which ports are tagged and which are untagged.

Save the completed configuration on both switches with the command below. This save is an addition at this point.

sudo config save -y

Step 6. Configure the Hosts

Assign the four addresses in the topology table. Each communicating pair shares a subnet and needs no default gateway for this test.

PC1’s original input and reply are preserved below. The reply shows the address and mask but not the gateway, so the mistake appears only in the sh ip output after it. Use the corrected commands later in this step.

PC1> ip 192.168.100.2/24 255.255.255.0
Checking for duplicate address...
PC1 : 192.168.100.2 255.255.255.0

This historical excerpt retains only the address and gateway fields from sh ip. It shows the incorrect gateway stored after that input.

PC1> sh ip

IP/MASK     : 192.168.100.2/24
GATEWAY     : 255.255.255.0

VPCS accepts the dotted subnet mask but also stores it as the gateway in this command. Give the prefix length alone to avoid that error.

Correct PC1’s input with the first command below. The other three are additions using the addresses in the topology.

PC1> ip 192.168.100.2/24
PC2> ip 192.168.200.2/24
PC3> ip 192.168.100.3/24
PC4> ip 192.168.200.3/24

Run the original address-display command on every host. Confirm its address and prefix match the table and its gateway is 0.0.0.0.

show ip

These corrected commands clear the incorrect gateway. The PDF does not include output recorded after this correction.

Check Connectivity

Both blocks below reproduce the original ping commands and replies. All four pings ran before any correction. PC2’s failed ping shows that PC2 also had 255.255.255.0 as its gateway, although the PDF does not show how PC2 was configured.

PC1> ping 192.168.100.3

84 bytes from 192.168.100.3 icmp_seq=1 ttl=64 time=4.837 ms
84 bytes from 192.168.100.3 icmp_seq=2 ttl=64 time=5.454 ms
84 bytes from 192.168.100.3 icmp_seq=3 ttl=64 time=5.471 ms
84 bytes from 192.168.100.3 icmp_seq=4 ttl=64 time=5.299 ms
84 bytes from 192.168.100.3 icmp_seq=5 ttl=64 time=5.455 ms

PC1> ping 192.168.200.3

host (255.255.255.0) not reachable
PC2> ping 192.168.200.3

84 bytes from 192.168.200.3 icmp_seq=1 ttl=64 time=4.839 ms
84 bytes from 192.168.200.3 icmp_seq=2 ttl=64 time=5.383 ms
84 bytes from 192.168.200.3 icmp_seq=3 ttl=64 time=5.685 ms
84 bytes from 192.168.200.3 icmp_seq=4 ttl=64 time=5.044 ms
84 bytes from 192.168.200.3 icmp_seq=5 ttl=64 time=5.301 ms

PC2> ping 192.168.100.3

host (255.255.255.0) not reachable

All ten successful replies show 84 bytes and TTL 64. VPCS sends its replies with TTL 64, so no router lowered it on the way back. That fits two hosts in one VLAN, but it does not show that the VLANs or the trunk tags are correct. The reply times will vary in another run.

Both failed attempts report the same unreachable gateway, 255.255.255.0. VPCS sent only ARP requests for 255.255.255.0, and nothing answered them. With no router in the lab, these pings would fail the same way even if both VLANs were one broadcast domain, so they do not show that the VLANs are separate.

With the corrected addresses and no gateway, VPCS reports No gateway found for an off-subnet destination. That is expected behavior, and the PDF does not record the corrected result.

Test the reverse directions with the additional commands below. PC3 should reach PC1 and PC4 should reach PC2, although the PDF records neither result.

PC3> ping 192.168.100.2
PC4> ping 192.168.200.2

Run the additional MAC-table checks below on both switches soon after the pings, before the entries age out. show mac aging-time shows the timer. Each switch learns a host’s MAC address from the frames that host sends. Find each host’s MAC address on the MAC line of its show ip output. PC1’s was 00:50:79:66:68:00 in the lab.

show mac -v 100
show mac -v 200

Expected MAC locations are given below for the recorded topology. This is an added check, and the PDF contains no MAC-table output.

SwitchVLANHostExpected Port
S1100PC1Ethernet0
S1100PC3Ethernet8
S1200PC2Ethernet4
S1200PC4Ethernet8
S2100PC3Ethernet0
S2100PC1Ethernet8
S2200PC4Ethernet4
S2200PC2Ethernet8

Before Production Use

A hardware switch’s first configuration depends on its platform and image. Check show ip interfaces and show ipv6 interfaces before adding its ports to a VLAN.

SONiC 202205 has no access or trunk port mode. A port’s role comes from its VLAN memberships. -u makes the port an untagged member of that VLAN, and leaving it out makes it a tagged member. A port can be an untagged member of only one VLAN but a tagged member of several, as Ethernet8 is here.

In SONiC 202205, config vlan member add refuses a port that is still a routed interface and stops with an error such as Error: Ethernet0 is a router interface!. The port stops being a routed interface when its last IPv4 or IPv6 address is removed, which is why Step 2 checks both address families. It stays a routed interface even with no addresses if it is bound to a VRF or has IPv6 link-local-only mode enabled.

sudo config save -y writes the running configuration to /etc/sonic/config_db.json. Reboot the switch in your test environment and check the interface states, VLAN membership and host connectivity again.

Use the command reference and CLI help that match the SONiC release you run. This lab uses the 202205 command reference. Check the SONiC supported platforms and feature support for your intended hardware and image. This virtual lab does not qualify a physical switch or measure its forwarding performance.

To test isolation, temporarily give PC4 the address 192.168.100.4/24 while it stays in VLAN 200, and ping it from PC1. The ping should fail with host (192.168.100.4) not reachable, because PC1’s ARP requests stay in VLAN 100. Capture on PC4’s link in GNS3 to confirm that the requests never arrive, and capture on the S1 to S2 link to confirm that the hosts’ frames carry tag 100 or 200. These checks were not part of the original lab.

Community SONiC 202205 does not run spanning tree, so a second link between S1 and S2 in the same VLANs would create a Layer 2 loop. Use the PortChannel guide if you need several links in one logical connection between these switches. For a device connected through one PortChannel to two switches, continue with the MCLAG guide.

The many benefits of SONiC, once exclusive to hyperscalers, are now within reach for enterprise clients. Contact our team of seasoned networking experts to learn why right now is a transformative time in the industry as we move closer to wider SONiC adoption.

Humza Altaf
Hardware Nation

Quickly identify your needs and craft a plan of action with help from our seasoned experts.