From 3ce390cb40e20d18b4eef8ac66b0e56020b536fd Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 13 Oct 2026 17:13:32 +0200 Subject: [PATCH] =?UTF-8?q?f-57:=20subnet:=20entr=C3=A9e=20FDB=20locale=20?= =?UTF-8?q?pour=20la=20MAC=20anycast,=20route=20du=20subnet=20=C3=A9mise?= =?UTF-8?q?=20depuis=20la=20gateway,=20contr=C3=B4le=20watchdog=20#57?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- internal/netif/fdb_linux.go | 51 +++++++++++++++++++++++++ internal/netif/fdb_linux_test.go | 43 +++++++++++++++++++++ internal/netif/fdb_other.go | 16 ++++++++ internal/netif/route_linux.go | 5 +++ internal/netif/route_other.go | 4 ++ internal/subnet/anycast.go | 3 +- internal/watchdog/check_anycast.go | 11 +++++- internal/watchdog/check_anycast_test.go | 3 ++ 8 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 internal/netif/fdb_linux.go create mode 100644 internal/netif/fdb_linux_test.go create mode 100644 internal/netif/fdb_other.go diff --git a/internal/netif/fdb_linux.go b/internal/netif/fdb_linux.go new file mode 100644 index 0000000..462cab2 --- /dev/null +++ b/internal/netif/fdb_linux.go @@ -0,0 +1,51 @@ +//go:build linux + +package netif + +import ( + "bytes" + "net" + "syscall" + + "github.com/vishvananda/netlink" +) + +func bridgeLocalEntry(bridgeIndex int, mac net.HardwareAddr) *netlink.Neigh { + return &netlink.Neigh{ + LinkIndex: bridgeIndex, + Family: syscall.AF_BRIDGE, + State: netlink.NUD_PERMANENT, + Flags: netlink.NTF_SELF, + HardwareAddr: mac, + } +} + +func hasBridgeLocalMAC(entries []netlink.Neigh, bridgeIndex int, mac net.HardwareAddr) bool { + for _, e := range entries { + if e.LinkIndex == bridgeIndex && e.MasterIndex == bridgeIndex && + e.State&netlink.NUD_PERMANENT != 0 && bytes.Equal(e.HardwareAddr, mac) { + return true + } + } + return false +} + +func BridgeAddLocalMAC(bridge string, mac net.HardwareAddr) error { + link, err := netlink.LinkByName(bridge) + if err != nil { + return err + } + return netlink.NeighAdd(bridgeLocalEntry(link.Attrs().Index, mac)) +} + +func BridgeHasLocalMAC(bridge string, mac net.HardwareAddr) (bool, error) { + link, err := netlink.LinkByName(bridge) + if err != nil { + return false, err + } + entries, err := netlink.NeighList(link.Attrs().Index, syscall.AF_BRIDGE) + if err != nil { + return false, err + } + return hasBridgeLocalMAC(entries, link.Attrs().Index, mac), nil +} diff --git a/internal/netif/fdb_linux_test.go b/internal/netif/fdb_linux_test.go new file mode 100644 index 0000000..b518bcc --- /dev/null +++ b/internal/netif/fdb_linux_test.go @@ -0,0 +1,43 @@ +//go:build linux + +package netif + +import ( + "syscall" + "testing" + + "github.com/vishvananda/netlink" +) + +func TestBridgeLocalEntry(t *testing.T) { + mac, _ := AnycastMAC(2911) + e := bridgeLocalEntry(7, mac) + if e.LinkIndex != 7 || e.Family != syscall.AF_BRIDGE { + t.Errorf("index ou famille inattendus : %d, %d", e.LinkIndex, e.Family) + } + if e.State != netlink.NUD_PERMANENT || e.Flags != netlink.NTF_SELF { + t.Errorf("état %#x et drapeaux %#x, entrée permanente du bridge attendue", e.State, e.Flags) + } +} + +func TestHasBridgeLocalMAC(t *testing.T) { + mac, _ := AnycastMAC(2911) + other, _ := AnycastMAC(2912) + local := netlink.Neigh{LinkIndex: 7, MasterIndex: 7, State: netlink.NUD_PERMANENT, HardwareAddr: mac} + cases := []struct { + label string + entries []netlink.Neigh + want bool + }{ + {"entrée locale du bridge", []netlink.Neigh{local}, true}, + {"seulement la liste unicast du bridge (macvlan)", []netlink.Neigh{{LinkIndex: 7, State: netlink.NUD_PERMANENT, Flags: netlink.NTF_SELF, HardwareAddr: mac}}, false}, + {"apprise sur un port", []netlink.Neigh{{LinkIndex: 9, MasterIndex: 7, State: netlink.NUD_REACHABLE, HardwareAddr: mac}}, false}, + {"autre MAC", []netlink.Neigh{{LinkIndex: 7, MasterIndex: 7, State: netlink.NUD_PERMANENT, HardwareAddr: other}}, false}, + {"aucune entrée", nil, false}, + } + for _, c := range cases { + if got := hasBridgeLocalMAC(c.entries, 7, mac); got != c.want { + t.Errorf("%s : %v, %v attendu", c.label, got, c.want) + } + } +} diff --git a/internal/netif/fdb_other.go b/internal/netif/fdb_other.go new file mode 100644 index 0000000..8c861ee --- /dev/null +++ b/internal/netif/fdb_other.go @@ -0,0 +1,16 @@ +//go:build !linux + +package netif + +import ( + "errors" + "net" +) + +func BridgeAddLocalMAC(_ string, _ net.HardwareAddr) error { + return errors.New("not supported on this platform") +} + +func BridgeHasLocalMAC(_ string, _ net.HardwareAddr) (bool, error) { + return false, errors.New("not supported on this platform") +} diff --git a/internal/netif/route_linux.go b/internal/netif/route_linux.go index fc444d1..a7be050 100644 --- a/internal/netif/route_linux.go +++ b/internal/netif/route_linux.go @@ -9,6 +9,10 @@ import ( ) func RouteAdd(iface string, subnet *net.IPNet) error { + return RouteAddFrom(iface, subnet, nil) +} + +func RouteAddFrom(iface string, subnet *net.IPNet, src net.IP) error { link, err := netlink.LinkByName(iface) if err != nil { return err @@ -16,6 +20,7 @@ func RouteAdd(iface string, subnet *net.IPNet) error { return netlink.RouteAdd(&netlink.Route{ LinkIndex: link.Attrs().Index, Dst: subnet, + Src: src, Scope: netlink.SCOPE_LINK, }) } diff --git a/internal/netif/route_other.go b/internal/netif/route_other.go index dcfd025..d87bf82 100644 --- a/internal/netif/route_other.go +++ b/internal/netif/route_other.go @@ -11,6 +11,10 @@ func RouteAdd(_ string, _ *net.IPNet) error { return errors.New("not supported on this platform") } +func RouteAddFrom(_ string, _ *net.IPNet, _ net.IP) error { + return errors.New("not supported on this platform") +} + func TableRoutesVia(_ int, _ string) ([]*net.IPNet, error) { return nil, errors.New("route listing is only implemented on linux") } diff --git a/internal/subnet/anycast.go b/internal/subnet/anycast.go index cabe22a..cf62889 100644 --- a/internal/subnet/anycast.go +++ b/internal/subnet/anycast.go @@ -35,10 +35,11 @@ func setupAnycastGateway(d subnetData, vethI string) error { {"confine " + mac.String(), func() error { return ebtables.ConfineMAC(vethI, mac.String()) }}, {"arp sysctls on " + d.bridge, func() error { return setSysctls(arpSysctls(d.bridge)) }}, {"relay address on " + d.bridge, func() error { return netif.AddrAdd(d.bridge, d.relayIP) }}, - {"route " + d.cidr.String(), func() error { return netif.RouteAdd(d.bridge, d.cidr) }}, {"create " + gw, func() error { return netif.CreateMacvlan(gw, d.bridge, mac) }}, + {"local fdb entry " + mac.String(), func() error { return netif.BridgeAddLocalMAC(d.bridge, mac) }}, {"arp sysctls on " + gw, func() error { return setSysctls(arpSysctls(gw)) }}, {"gateway address on " + gw, func() error { return netif.AddrAdd(gw, d.interfaceIP) }}, + {"route " + d.cidr.String(), func() error { return netif.RouteAddFrom(d.bridge, d.cidr, d.interfaceIP) }}, {"set up " + gw, func() error { return netif.LinkSetUp(gw) }}, {"drop arp to gateway", func() error { return ebtables.DropARPToGateway(vethI, d.interfaceIP.String()) }}, {"drop arp to relay", func() error { return ebtables.DropARPToGateway(vethI, d.relayIP.String()) }}, diff --git a/internal/watchdog/check_anycast.go b/internal/watchdog/check_anycast.go index 2aa7515..1129d52 100644 --- a/internal/watchdog/check_anycast.go +++ b/internal/watchdog/check_anycast.go @@ -16,6 +16,7 @@ import ( type gatewayState struct { MAC string + LocalFDB bool GatewayAddrs []string BridgeAddrs []string } @@ -34,6 +35,9 @@ func gatewayProblems(gw, bridge string, got gatewayState, wantMAC, gatewayIP, re if got.MAC != wantMAC { problems = append(problems, fmt.Sprintf("interface %s has mac %q, expected anycast %s", gw, got.MAC, wantMAC)) } + if !got.LocalFDB { + problems = append(problems, fmt.Sprintf("bridge %s has no local fdb entry for %s, frames to the gateway are flooded", bridge, wantMAC)) + } if !hasAddr(got.GatewayAddrs, gatewayIP) { problems = append(problems, fmt.Sprintf("interface %s lacks the gateway address %s", gw, gatewayIP)) } @@ -46,13 +50,16 @@ func gatewayProblems(gw, bridge string, got gatewayState, wantMAC, gatewayIP, re return problems } -func readGatewayState(gw, bridge string) (gatewayState, error) { +func readGatewayState(gw, bridge string, anycast net.HardwareAddr) (gatewayState, error) { var st gatewayState mac, err := netif.LinkMAC(gw) if err != nil { return st, err } st.MAC = mac.String() + if st.LocalFDB, err = netif.BridgeHasLocalMAC(bridge, anycast); err != nil { + return st, err + } for _, side := range []struct { iface string dst *[]string @@ -106,7 +113,7 @@ func checkAnycastGateway(db *badger.DB, name, vpc, bridge string, n notify.Notif n.Notify(kindSubnet, name, p+" (in netns "+vpc+")") return nil } - st, err := readGatewayState(gw, bridge) + st, err := readGatewayState(gw, bridge, wantMAC) if err != nil { n.Notify(kindSubnet, name, fmt.Sprintf("anycast gateway unreadable in netns %s: %v", vpc, err)) return nil diff --git a/internal/watchdog/check_anycast_test.go b/internal/watchdog/check_anycast_test.go index e813b74..3cc367e 100644 --- a/internal/watchdog/check_anycast_test.go +++ b/internal/watchdog/check_anycast_test.go @@ -8,6 +8,7 @@ import ( func TestGatewayProblems_Healthy(t *testing.T) { st := gatewayState{ MAC: "02:01:00:00:0b:5f", + LocalFDB: true, GatewayAddrs: []string{"10.0.0.254"}, BridgeAddrs: []string{"10.0.0.253"}, } @@ -20,6 +21,7 @@ func TestGatewayProblems_EachDefectIsReported(t *testing.T) { healthy := func() gatewayState { return gatewayState{ MAC: "02:01:00:00:0b:5f", + LocalFDB: true, GatewayAddrs: []string{"10.0.0.254"}, BridgeAddrs: []string{"10.0.0.253"}, } @@ -30,6 +32,7 @@ func TestGatewayProblems_EachDefectIsReported(t *testing.T) { want string }{ {"MAC du macvlan changée", func(s *gatewayState) { s.MAC = "02:03:c0:a8:0e:0b" }, "expected anycast 02:01:00:00:0b:5f"}, + {"entrée FDB locale absente du bridge", func(s *gatewayState) { s.LocalFDB = false }, "no local fdb entry for 02:01:00:00:0b:5f"}, {"gateway absente du macvlan", func(s *gatewayState) { s.GatewayAddrs = nil }, "lacks the gateway address 10.0.0.254"}, {"adresse d'émission absente du bridge", func(s *gatewayState) { s.BridgeAddrs = nil }, "lacks the relay address 10.0.0.253"}, {"gateway posée sur le bridge", func(s *gatewayState) { s.BridgeAddrs = append(s.BridgeAddrs, "10.0.0.254") }, "bypass the anycast mac"},