Skip to contentSkip to conversation

Add bridge VLAN support

RFC PATCH Superseded Leigh Brown 1 participant 6 messages May 3, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Add bridge VLAN support

For many years I have been configuring VLANs on my Linux Dom0 by creating VLAN interfaces for each VLAN I wanted to connect a domain to and then a corresponding bridge. So I would tend to have things like:

enp0s0 -> br0 -> vif1, vif2
enp0s0.10 -> br0vl10 -> vif3, vif4
enp0s0.20 -> br0vl20 -> vif5
dummy0 -> br1 -> vif6

I recently discovered that iproute2 supports creating bridge VLANs that allows you to assign a VLAN to each of the interfaces associated to a bridge. This allows a greatly simplified configuration where a single bridge can support all the domains, and the iproute2 bridge command can assign each VIF to the required VLAN. This looks like this:

# bridge vlan
port              vlan-id  
enp0s0            1 PVID Egress Untagged
                  10
                  20
br0               1 PVID Egress Untagged
vif1.0            1 PVID Egress Untagged
vif2.0            10 PVID Egress Untagged
vif3.0            10 PVID Egress Untagged
vif4.0            20 PVID Egress Untagged
vif5.0            20 PVID Egress Untagged
vif6.0            30 PVID Egress Untagged

This patch set enables this capability as follows:

1. Adds `vid' as a new member of the libxl_device_nic structure;
2. Adds support to read and write vid from the xenstore;
3. Adds `vid' as a new keyword for the vif configuration option;
4. Adds support for assign the bridge VLAN in the Linux hotplug scripts.

I don't believe NetBSD or FreeBSD support this capability, but if they do please point me in the direction of some documentation and/or examples.

NB: I'm not very familiar with Xen code base so may have missed something important, although I have tested it and it is working well for me.

Cheers,

Leigh.

[email protected] (5):
tools/libs/light: Add vid field to libxl_device_nic tools/xl: add vid keyword vif option tools/hotplug/Linux: Add bridge VLAN support docs/man: document VIF vid keyword tools/examples: Examples Linux bridge VLAN config

 docs/man/xl-network-configuration.5.pod.in    |  6 +++
 tools/examples/linux-bridge-vlan/README       | 52 +++++++++++++++++++
 tools/examples/linux-bridge-vlan/br0.netdev   |  7 +++
 tools/examples/linux-bridge-vlan/br0.network  |  8 +++
 .../examples/linux-bridge-vlan/enp0s0.network | 16 ++++++
 tools/hotplug/Linux/xen-network-common.sh     |  9 ++++
 tools/libs/light/libxl_nic.c                  | 20 +++++++
 tools/libs/light/libxl_types.idl              |  1 +
 tools/xl/xl_parse.c                           |  2 +
 9 files changed, 121 insertions(+)
 create mode 100644 tools/examples/linux-bridge-vlan/README
 create mode 100644 tools/examples/linux-bridge-vlan/br0.netdev
 create mode 100644 tools/examples/linux-bridge-vlan/br0.network
 create mode 100644 tools/examples/linux-bridge-vlan/enp0s0.network
Signature · 1 line
2.39.2
#1 lore raw

RFC PATCH 1/5 tools/libs/light: Add vid field to libxl_device_nic

Add integer member `vid' to libxl_device_nic, to represent the VLAN ID to assign to the VIF when adding it to the bridge device.

Update libxl_nic.c to read and write the vid field from the xenstore.

This provides the capability for supported operating systems (e.g. Linux) to perform VLAN filtering on bridge ports. The Xen hotplug scripts need to be updated to read this information from then xenstore and perform the required configuration.

---
 tools/libs/light/libxl_nic.c     | 20 ++++++++++++++++++++
 tools/libs/light/libxl_types.idl |  1 +
 2 files changed, 21 insertions(+)
tools/libs/light/libxl_nic.c +20 -0
index d6bf06fc34..e28b9101ee 100644
@@ -233,6 +233,11 @@ static int libxl__set_xenstore_nic(libxl__gc *gc, uint32_t domid,
         flexarray_append(back, GCSPRINTF("%u", nic->mtu));
     }
     
+    if (nic->vid) {
+        flexarray_append(back, "vid");
+        flexarray_append(back, GCSPRINTF("%u", nic->vid));
+    }
+
     flexarray_append(back, "bridge");
     flexarray_append(back, libxl__strdup(gc, nic->bridge));
     flexarray_append(back, "handle");
@@ -313,6 +318,21 @@ static int libxl__nic_from_xenstore(libxl__gc *gc, const char *libxl_path,
         nic->mtu = LIBXL_DEVICE_NIC_MTU_DEFAULT;
     }
 
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/vid", libxl_path), &tmp);
+    if (rc) goto out;
+    if (tmp) {
+        char *endptr;
+
+        nic->vid = strtol(tmp, &endptr, 10);
+        if (*endptr != '\0') {
+            rc = ERROR_INVAL;
+            goto out;
+        }
+    } else {
+        nic->vid = 0;
+    }
+
     rc = libxl__xs_read_checked(gc, XBT_NULL,
                                 GCSPRINTF("%s/mac", libxl_path), &tmp);
     if (rc) goto out;
tools/libs/light/libxl_types.idl +1 -0
index 7d8bd5d216..df5185128c 100644
@@ -809,6 +809,7 @@ libxl_device_nic = Struct("device_nic", [
     ("backend_domname", string),
     ("devid", libxl_devid),
     ("mtu", integer),
+    ("vid", integer),
     ("model", string),
     ("mac", libxl_mac),
     ("ip", string),
Signature · 1 line
2.39.2
#2 lore raw

RFC PATCH 2/5 tools/xl: add vid keyword vif option

In reply to Leigh Brown

Update parse_nic_config() to support a new `vid' keyword. This keyword specifies the numeric VLAN ID to assign to the VIF when attaching it to the bridge port, on operating systems that support the capability (e.g. Linux).

---
 tools/xl/xl_parse.c | 2 ++
 1 file changed, 2 insertions(+)
tools/xl/xl_parse.c +2 -0
index ed983200c3..5d5dd4ec04 100644
@@ -565,6 +565,8 @@ int parse_nic_config(libxl_device_nic *nic, XLU_Config **config, char *token)
         nic->devid = parse_ulong(oparg);
     } else if (MATCH_OPTION("mtu", token, oparg)) {
         nic->mtu = parse_ulong(oparg);
+    } else if (MATCH_OPTION("vid", token, oparg)) {
+        nic->vid = parse_ulong(oparg);
     } else if (!strcmp("trusted", token)) {
         libxl_defbool_set(&nic->trusted, true);
     } else if (!strcmp("untrusted", token)) {
Signature · 1 line
2.39.2
#3 lore raw

RFC PATCH 3/5 tools/hotplug/Linux: Add bridge VLAN support

In reply to Leigh Brown

Update add_to_bridge shell function to read the vid parameter from xenstore and set the bridge LAN for the VID to the given value. This only works when using the iproute2 bridge command, so a warning is issued if using the legacy brctl command and a vid is set.

---
 tools/hotplug/Linux/xen-network-common.sh | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/hotplug/Linux/xen-network-common.sh +9 -0
index 42fa704e8d..19a8b3c7e5 100644
@@ -125,14 +125,23 @@ create_bridge () {
 add_to_bridge () {
     local bridge=$1
     local dev=$2
+    local vid=$(xenstore_read_default "$XENBUS_PATH/vid" "")
 
     # Don't add $dev to $bridge if it's already on the bridge.
     if [ ! -e "/sys/class/net/${bridge}/brif/${dev}" ]; then
         log debug "adding $dev to bridge $bridge"
         if which brctl >&/dev/null; then
             brctl addif ${bridge} ${dev}
+            if [ -n "${vid}" ] ;then
+                log warning "bridge command not available, ignoring vid"
+            fi
         else
             ip link set ${dev} master ${bridge}
+            if [ -n "${vid}" ] ;then
+                log debug "Assigning $dev to vid $vid"
+                bridge vlan del dev ${dev} vid 1
+                bridge vlan add dev ${dev} vid ${vid} pvid untagged
+            fi
         fi
     else
         log debug "$dev already on bridge $bridge"
Signature · 1 line
2.39.2
#4 lore raw

RFC PATCH 4/5 docs/man: document VIF vid keyword

In reply to Leigh Brown

Document the new `vid' keyword in xl-network-configuration(5).

---
 docs/man/xl-network-configuration.5.pod.in | 6 ++++++
 1 file changed, 6 insertions(+)
docs/man/xl-network-configuration.5.pod.in +6 -0
index f3e379bcf8..fe2615ae30 100644
@@ -259,6 +259,12 @@ Specifies the MTU (i.e. the maximum size of an IP payload, exclusing headers). T
 default value is 1500 but, if the VIF is attached to a bridge, it will be set to match
 unless overridden by this parameter.
 
+=head2 vid
+
+Specifies the VLAN ID. If this is set to a non-zero value, it will be specified
+when attaching the VIF to a bridge.  This can be used on operating systems that
+support bridge VLANs (e.g. Linux using iproute2).
+
 =head2 trusted / untrusted
 
 An advisory setting for the frontend driver on whether the backend should be
Signature · 1 line
2.39.2
#5 lore raw

RFC PATCH 5/5 tools/examples: Examples Linux bridge VLAN config

In reply to Leigh Brown

Add a new directory linux-bridge-vlan showing how to configure systemd-networkd to support a bridge VLAN configuration.

---
 tools/examples/linux-bridge-vlan/README       | 52 +++++++++++++++++++
 tools/examples/linux-bridge-vlan/br0.netdev   |  7 +++
 tools/examples/linux-bridge-vlan/br0.network  |  8 +++
 .../examples/linux-bridge-vlan/enp0s0.network | 16 ++++++
 4 files changed, 83 insertions(+)
 create mode 100644 tools/examples/linux-bridge-vlan/README
 create mode 100644 tools/examples/linux-bridge-vlan/br0.netdev
 create mode 100644 tools/examples/linux-bridge-vlan/br0.network
 create mode 100644 tools/examples/linux-bridge-vlan/enp0s0.network
tools/examples/linux-bridge-vlan/README new file +52 -0
new file mode 100644
index 0000000000..b287710e0f
@@ -0,0 +1,52 @@
+Linux Xen Dom0 single bridge multiple VLAN configuration with systemd
+=====================================================================
+
+Introduction
+------------
+
+This directory contains example files to be placed in /etc/systemd/network
+to enable a single bridge with multiple VLAN support.
+
+The example is to support the scenario where the Xen host network interface
+is connected to an Ethernet switch configured as a trunk port. Each domain
+VIF can then be configured with the VLAN id (vid) of the required VLAN.
+
+The example files create a bridge device called br0, with a physical interface 
+called enp0s0. You will need to update this with your system's device name.
+
+Key points of the configuration are:
+
+1. In br0.netdev, VLANFiltering=on is set. This is required to ensure the
+   VLAN tags are handled correctly.  If it is not set then the packets
+   from the vif interfaces will not have the correct VLAN tags set.  I
+   observed them with the pvid in the switch MAC address table.
+
+2. In br0.network, a system IPv4 address is configured that can be updated
+   according to your local network settings.
+
+3. In enp0s0.network, Bridge=br0 sets the bridge device to connect to and
+   there is a [BridgeVLAN] section for each VLAN you want to give access
+   to the switch. Note, if you want to create an internal VLAN private to
+   the host, do not include that VLAN id in this file.
+
+
+Domain configuration
+--------------------
+
+Add the vid= keyword to the vif definition in the domain. For example:
+
+vif = [ 'mac=xx:xx:xx:xx:xx:xx, bridge=br0, vid=10' ]
+
+
+Hints and tips
+--------------
+
+1. To check if vlan_filtering is enabled, run:
+   # cat /sys/devices/virtual/net/<name>/bridge/vlan_filtering
+
+2. To check the bridge port VLAN assignments, run:
+   # bridge vlan
+
+3. To check the vid setting in the xenstore, run:
+   # xenstore-ls -f | grep 'vid ='
+
tools/examples/linux-bridge-vlan/br0.netdev new file +7 -0
new file mode 100644
index 0000000000..ae1fe487c3
@@ -0,0 +1,7 @@
+[NetDev]
+Name=br0
+Kind=bridge
+MACAddress=xx:xx:xx:xx:xx:xx
+
+[Bridge]
+VLANFiltering=on
tools/examples/linux-bridge-vlan/br0.network new file +8 -0
new file mode 100644
index 0000000000..b56203b66a
@@ -0,0 +1,8 @@
+[Match]
+Name=br0
+
+[Network]
+DNS=8.8.8.8
+#Domains=example.com
+Address=10.1.1.10/24
+Gateway=10.1.1.1
tools/examples/linux-bridge-vlan/enp0s0.network new file +16 -0
new file mode 100644
index 0000000000..6ee3154dfc
@@ -0,0 +1,16 @@
+[Match]
+Name=enp0s0
+
+[Network]
+Bridge=br0
+
+# If Jumbo frames are required
+#[Link]
+#MTUBytes=9000
+
+[BridgeVLAN]
+VLAN=10
+
+[BridgeVLAN]
+VLAN=20
+
Signature · 1 line
2.39.2