Skip to content

Commit c7039a4

Browse files
mattedalloaboch
authored andcommitted
bridge: add per-device BridgeVlanTunnelShowDev
Add BridgeVlanTunnelShowDev(link) to retrieve VLAN-to-tunnel-ID mappings for a specific device, equivalent to `bridge vlan tunnelshow dev DEV`. The existing BridgeVlanTunnelShow() returns a flat []TunnelInfo for all bridge ports. Since TunnelInfo only contains {TunId, Vid} without an ifindex, callers cannot determine which device each mapping belongs to. This forces users to shell out to `bridge -j vlan tunnelshow dev <name>` and parse JSON instead. The internal bridgeVlanTunnelShowBy(ifindex) helper performs a full dump and filters client-side by ifindex (matching how iproute2 implements `bridge vlan tunnelshow dev DEV`). When ifindex is 0, all devices are returned. BridgeVlanTunnelShow() is refactored to use the same helper with no behavior change (ifindex=0 dumps all devices). Signed-off-by: Matteo Dallaglio <mdallagl@redhat.com>
1 parent ab2b190 commit c7039a4

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

bridge_linux.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ func BridgeVlanTunnelShow() ([]nl.TunnelInfo, error) {
1919
}
2020

2121
func (h *Handle) BridgeVlanTunnelShow() ([]nl.TunnelInfo, error) {
22+
// ifindex 0 means no filtering (return all devices)
23+
return h.bridgeVlanTunnelShowBy(0)
24+
}
25+
26+
// BridgeVlanTunnelShowDev gets vlanid-tunnelid mapping for a specific device.
27+
// Equivalent to: `bridge vlan tunnelshow dev DEV`
28+
//
29+
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
30+
// or incomplete.
31+
func BridgeVlanTunnelShowDev(link Link) ([]nl.TunnelInfo, error) {
32+
return pkgHandle.BridgeVlanTunnelShowDev(link)
33+
}
34+
35+
func (h *Handle) BridgeVlanTunnelShowDev(link Link) ([]nl.TunnelInfo, error) {
36+
base := link.Attrs()
37+
h.ensureIndex(base)
38+
return h.bridgeVlanTunnelShowBy(int32(base.Index))
39+
}
40+
41+
// bridgeVlanTunnelShowBy performs a bridge VLAN tunnel dump and optionally
42+
// filters by device ifindex.
43+
// When ifindex is 0, all devices are returned.
44+
func (h *Handle) bridgeVlanTunnelShowBy(ifindex int32) ([]nl.TunnelInfo, error) {
2245
req := h.newNetlinkRequest(unix.RTM_GETLINK, unix.NLM_F_DUMP)
2346
msg := nl.NewIfInfomsg(unix.AF_BRIDGE)
2447
req.AddData(msg)
@@ -32,6 +55,10 @@ func (h *Handle) BridgeVlanTunnelShow() ([]nl.TunnelInfo, error) {
3255
for _, m := range msgs {
3356
msg := nl.DeserializeIfInfomsg(m)
3457

58+
if ifindex != 0 && msg.Index != ifindex {
59+
continue
60+
}
61+
3562
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
3663
if err != nil {
3764
return nil, err
@@ -54,6 +81,11 @@ func (h *Handle) BridgeVlanTunnelShow() ([]nl.TunnelInfo, error) {
5481
}
5582
}
5683
}
84+
// Each device has exactly one message in the dump;
85+
// return early once we've processed the target device.
86+
if ifindex != 0 {
87+
return ret, executeErr
88+
}
5789
}
5890
return ret, executeErr
5991
}

bridge_linux_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"io/ioutil"
66
"testing"
7+
8+
"github.com/vishvananda/netlink/nl"
79
)
810

911
func TestBridgeVlan(t *testing.T) {
@@ -295,6 +297,133 @@ func TestBridgeVni(t *testing.T) {
295297
}
296298
}
297299

300+
func setupBridgeWithTwoVxlans(t *testing.T) (*Bridge, *Vxlan, *Vxlan) {
301+
t.Helper()
302+
303+
// ip link add br0 type bridge
304+
bridge := &Bridge{LinkAttrs: LinkAttrs{Name: "br0"}}
305+
if err := LinkAdd(bridge); err != nil {
306+
t.Fatal(err)
307+
}
308+
309+
// ip link add vxlan0 type vxlan dstport 4789 nolearning external local 10.0.1.1
310+
vxlan0 := &Vxlan{
311+
LinkAttrs: LinkAttrs{Name: "vxlan0"},
312+
SrcAddr: []byte("10.0.1.1"),
313+
Learning: false,
314+
FlowBased: true,
315+
Port: 4789,
316+
}
317+
if err := LinkAdd(vxlan0); err != nil {
318+
t.Fatal(err)
319+
}
320+
321+
// ip link add vxlan1 type vxlan dstport 4790 nolearning external local 10.0.1.1
322+
vxlan1 := &Vxlan{
323+
LinkAttrs: LinkAttrs{Name: "vxlan1"},
324+
SrcAddr: []byte("10.0.1.1"),
325+
Learning: false,
326+
FlowBased: true,
327+
Port: 4790,
328+
}
329+
if err := LinkAdd(vxlan1); err != nil {
330+
t.Fatal(err)
331+
}
332+
333+
// ip link set dev vxlan0 master br0
334+
// ip link set dev vxlan1 master br0
335+
for _, vxlan := range []*Vxlan{vxlan0, vxlan1} {
336+
if err := LinkSetMaster(vxlan, bridge); err != nil {
337+
t.Fatal(err)
338+
}
339+
}
340+
341+
// ip link set br0 type bridge vlan_filtering 1
342+
if err := BridgeSetVlanFiltering(bridge, true); err != nil {
343+
t.Fatal(err)
344+
}
345+
346+
// bridge link set dev vxlan0 vlan_tunnel on
347+
// bridge link set dev vxlan1 vlan_tunnel on
348+
for _, vxlan := range []*Vxlan{vxlan0, vxlan1} {
349+
if err := LinkSetVlanTunnel(vxlan, true); err != nil {
350+
t.Fatal(err)
351+
}
352+
}
353+
354+
return bridge, vxlan0, vxlan1
355+
}
356+
357+
func TestBridgeVlanTunnelShowDev(t *testing.T) {
358+
minKernelRequired(t, 4, 11)
359+
t.Cleanup(setUpNetlinkTest(t))
360+
361+
if err := remountSysfs(); err != nil {
362+
t.Fatal(err)
363+
}
364+
365+
bridge, vxlan0, vxlan1 := setupBridgeWithTwoVxlans(t)
366+
367+
// bridge vlan add vid 10 dev vxlan0
368+
if err := BridgeVlanAdd(vxlan0, 10, false, false, false, false); err != nil {
369+
t.Fatal(err)
370+
}
371+
// bridge vlan add dev vxlan0 vid 10 tunnel_info id 100
372+
if err := BridgeVlanAddTunnelInfo(vxlan0, 10, 100, false, false); err != nil {
373+
t.Fatal(err)
374+
}
375+
// bridge vlan add vid 11 dev vxlan0
376+
if err := BridgeVlanAdd(vxlan0, 11, false, false, false, false); err != nil {
377+
t.Fatal(err)
378+
}
379+
// bridge vlan add dev vxlan0 vid 11 tunnel_info id 101
380+
if err := BridgeVlanAddTunnelInfo(vxlan0, 11, 101, false, false); err != nil {
381+
t.Fatal(err)
382+
}
383+
384+
// bridge vlan add vid 20 dev vxlan1
385+
if err := BridgeVlanAdd(vxlan1, 20, false, false, false, false); err != nil {
386+
t.Fatal(err)
387+
}
388+
// bridge vlan add dev vxlan1 vid 20 tunnel_info id 200
389+
if err := BridgeVlanAddTunnelInfo(vxlan1, 20, 200, false, false); err != nil {
390+
t.Fatal(err)
391+
}
392+
393+
// Verify vxlan0 returns only its tunnel infos
394+
tis, err := BridgeVlanTunnelShowDev(vxlan0)
395+
if err != nil {
396+
t.Fatal(err)
397+
}
398+
if len(tis) != 2 {
399+
t.Fatalf("vxlan0: expected 2 tunnel infos, got %d: %v", len(tis), tis)
400+
}
401+
if tis[0] != (nl.TunnelInfo{Vid: 10, TunId: 100}) || tis[1] != (nl.TunnelInfo{Vid: 11, TunId: 101}) {
402+
t.Fatalf("vxlan0: unexpected tunnel infos: %+v", tis)
403+
}
404+
405+
// Verify vxlan1 returns only its tunnel infos
406+
tis, err = BridgeVlanTunnelShowDev(vxlan1)
407+
if err != nil {
408+
t.Fatal(err)
409+
}
410+
if len(tis) != 1 {
411+
t.Fatalf("vxlan1: expected 1 tunnel info, got %d: %v", len(tis), tis)
412+
}
413+
if tis[0] != (nl.TunnelInfo{Vid: 20, TunId: 200}) {
414+
t.Fatalf("vxlan1: unexpected tunnel info: %+v", tis)
415+
}
416+
417+
// Verify bridge itself returns no tunnel infos
418+
tis, err = BridgeVlanTunnelShowDev(bridge)
419+
if err != nil {
420+
t.Fatal(err)
421+
}
422+
if len(tis) != 0 {
423+
t.Fatalf("bridge: expected 0 tunnel infos, got %d: %v", len(tis), tis)
424+
}
425+
}
426+
298427
func TestBridgeGroupFwdMask(t *testing.T) {
299428
minKernelRequired(t, 4, 15) //minimal release for per-port group_fwd_mask
300429
t.Cleanup(setUpNetlinkTest(t))

0 commit comments

Comments
 (0)