[lxc-devel] [lxd/master] Add VLAN struct to network state

stgraber on Github lxc-bot at linuxcontainers.org
Fri Dec 11 16:39:21 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201211/b906bbb5/attachment.bin>
-------------- next part --------------
From 414b214363e759b9156d806fb3bd8646b581b016 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 11 Dec 2020 11:27:28 -0500
Subject: [PATCH 1/4] shared/api: Fix typo
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 shared/api/network.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/shared/api/network.go b/shared/api/network.go
index 12b126dc33..8647a42bf1 100644
--- a/shared/api/network.go
+++ b/shared/api/network.go
@@ -117,7 +117,7 @@ type NetworkStateBond struct {
 	LowerDevices []string `json:"lower_devices" yaml:"lower_devices"`
 }
 
-// NetworkStateBridge represents bond specific state
+// NetworkStateBridge represents bridge specific state
 // API extension: network_state_bond_bridge
 type NetworkStateBridge struct {
 	ID           string `json:"id" yaml:"id"`

From bb7e2fab1193201789bf373348607eea8c2bfe2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 11 Dec 2020 11:27:38 -0500
Subject: [PATCH 2/4] shared/api: Add NetworkStateVLAN
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 shared/api/network.go | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/shared/api/network.go b/shared/api/network.go
index 8647a42bf1..6178675e23 100644
--- a/shared/api/network.go
+++ b/shared/api/network.go
@@ -85,6 +85,9 @@ type NetworkState struct {
 	// API extension: network_state_bond_bridge
 	Bond   *NetworkStateBond   `json:"bond" yaml:"bond"`
 	Bridge *NetworkStateBridge `json:"bridge" yaml:"bridge"`
+
+	// API extension: network_state_vlan
+	VLAN *NetworkStateVLAN `json:"vlan" yaml:"vlan"`
 }
 
 // NetworkStateAddress represents a network address
@@ -129,3 +132,10 @@ type NetworkStateBridge struct {
 
 	UpperDevices []string `json:"upper_devices" yaml:"upper_devices"`
 }
+
+// NetworkStateVLAN represents VLAN specific state
+// API extension: network_state_vlan
+type NetworkStateVLAN struct {
+	LowerDevice string `json:"lower_device" yaml:"lower_device"`
+	VID         uint64 `json:"vid" yaml:"vid"`
+}

From 05ad258cd4ec72e23187f83d45b080042ec0e0cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 11 Dec 2020 11:37:39 -0500
Subject: [PATCH 3/4] lxd/resources: Add VLAN struct
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/resources/network.go | 43 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/lxd/resources/network.go b/lxd/resources/network.go
index 8edecf20b0..84597a045a 100644
--- a/lxd/resources/network.go
+++ b/lxd/resources/network.go
@@ -616,6 +616,49 @@ func GetNetworkState(name string) (*api.NetworkState, error) {
 		network.Bridge = &bridge
 	}
 
+	// Populate VLAN details.
+	type vlan struct {
+		lower string
+		vid   uint64
+	}
+	vlans := map[string]vlan{}
+
+	vlanPath := "/proc/net/vlan/config"
+	if sysfsExists(vlanPath) {
+		entries, err := ioutil.ReadFile(vlanPath)
+		if err != nil {
+			return nil, err
+		}
+
+		for _, line := range strings.Split(string(entries), "\n") {
+			fields := strings.Split(line, "|")
+			if len(fields) != 3 {
+				continue
+			}
+
+			vName := strings.TrimSpace(fields[0])
+			vVID, err := strconv.ParseUint(strings.TrimSpace(fields[1]), 10, 64)
+			if err != nil {
+				continue
+			}
+			vLower := strings.TrimSpace(fields[2])
+
+			vlans[vName] = vlan{
+				lower: vLower,
+				vid:   vVID,
+			}
+		}
+	}
+
+	// Check if the inrterface is a VLAN.
+	entry, ok := vlans[name]
+	if ok {
+		network.VLAN = &api.NetworkStateVLAN{
+			LowerDevice: entry.lower,
+			VID:         entry.vid,
+		}
+	}
+
 	// Get counters.
 	counters, err := GetNetworkCounters(name)
 	if err != nil {

From 5003b6f228b0c41a89192117cdf6cff40e2bbbc7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 11 Dec 2020 11:39:01 -0500
Subject: [PATCH 4/4] api: Add network_state_vlan
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 doc/api-extensions.md | 7 +++++++
 shared/version/api.go | 1 +
 2 files changed, 8 insertions(+)

diff --git a/doc/api-extensions.md b/doc/api-extensions.md
index bdc2cab260..e405c3045e 100644
--- a/doc/api-extensions.md
+++ b/doc/api-extensions.md
@@ -1257,3 +1257,10 @@ with `ovn.ingress_mode=routed`.
 ## projects\_limits\_instances
 Adds `limits.instances` to the available project configuration keys. If set, it
 limits the total number of instances (VMs and containers) that can be used in the project.
+
+## network\_state\_vlan
+This adds a "vlan" section to the /1.0/networks/NAME/state API.
+
+Those contain additional state information relevant to VLAN interfaces:
+ - lower\_device
+ - vid
diff --git a/shared/version/api.go b/shared/version/api.go
index e7f030047a..bfce517abc 100644
--- a/shared/version/api.go
+++ b/shared/version/api.go
@@ -243,6 +243,7 @@ var APIExtensions = []string{
 	"network_ovn_dhcp",
 	"network_physical_routes_anycast",
 	"projects_limits_instances",
+	"network_state_vlan",
 }
 
 // APIExtensionsCount returns the number of available API extensions.


More information about the lxc-devel mailing list