f-41: subnet: MAC de gateway propre à l'hôte, dérivée du VTEP (retour sur l'anycast) #41

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-12 01:01:05 +02:00
commit 9b2c2409eb
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
6 changed files with 60 additions and 54 deletions

View file

@ -42,11 +42,16 @@ vxlan
Le subnet vit dans le netns du VPC. La VM n'est donc **pas joignable depuis l'host** sans route Le subnet vit dans le netns du VPC. La VM n'est donc **pas joignable depuis l'host** sans route
explicite — point à connaître avant de câbler un outil externe dessus. explicite — point à connaître avant de câbler un outil externe dessus.
Le bridge du subnet, qui porte ``interface_ip``, a une **MAC de gateway anycast** calculée depuis Le bridge du subnet, qui porte ``interface_ip``, a une **MAC de gateway propre à l'hôte**, calculée
le ``vxlan_id`` : ``02:01:00`` suivi des 24 bits de la VNI (``vxlan_id`` 2811 → depuis son VTEP — l'IPv4 primaire du ``local_iface`` : ``02:03`` suivi des quatre octets de
``02:01:00:00:0a:fb``). Elle est identique sur tous les hôtes qui portent le subnet : une VM qui l'adresse (VTEP ``192.168.14.12`` → ``02:03:c0:a8:0e:0c``). Unique dans le fabric, elle est la
change d'hôte garde une entrée ARP valide pour sa gateway. Elle n'est posée qu'à la création du même pour tous les subnets d'un hôte, chacun étant un domaine L2 distinct. Elle n'est posée qu'à la
subnet — un subnet créé avant garde la MAC choisie par le noyau jusqu'à sa recréation. création du subnet — un subnet créé avant garde la MAC choisie par le noyau jusqu'à sa recréation.
Une MAC de gateway **identique sur tous les hôtes** (anycast) a été essayée puis retirée : apprise
sur le veth du subnet, elle était annoncée en EVPN par chaque hôte et détectée en double ; et une
gateway ne pouvait plus joindre en L2 une VM d'un autre hôte, la réponse revenant à la gateway
locale de la VM. Le cache ARP d'une VM migrée à chaud est un sujet de la migration, pas du subnet.
bridge bridge
------ ------

View file

@ -754,8 +754,8 @@ sortie vaut 1 si une vérification échoue **ou si aucune n'a été faite**.
- suite de ``s7``, topologie ``evpn-3hv.yml`` : migration d'une VM de hv1 vers hv2 par l'API, - suite de ``s7``, topologie ``evpn-3hv.yml`` : migration d'une VM de hv1 vers hv2 par l'API,
observée depuis hv3 qui n'a pas le subnet : migration simple (VM supprimée puis recréée, observée depuis hv3 qui n'a pas le subnet : migration simple (VM supprimée puis recréée,
``/24`` comme filet), séquence orchestrée (deux VM de même IP, deux ``/32`` en ECMP, 40 ``/24`` comme filet), séquence orchestrée (deux VM de même IP, deux ``/32`` en ECMP, 40
sources sur 40) ; l'ancien « pire cas » devient un refus (409) ; MAC de gateway anycast non sources sur 40) ; l'ancien « pire cas » devient un refus (409) ; MAC de gateway propre à
annoncée en EVPN ; watchdog : ``/32`` orphelin signalé comme trou noir, ``/32`` manquant chaque hôte, sans doublon EVPN ; watchdog : ``/32`` orphelin signalé comme trou noir, ``/32`` manquant
signalé signalé
- hv1, hv2, hv3, rr1 - hv1, hv2, hv3, rr1

View file

@ -2,6 +2,7 @@ package subnet
import ( import (
"fmt" "fmt"
"net"
configuration "git.g3e.fr/syonad/two/internal/config/agent" configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/dhcp" "git.g3e.fr/syonad/two/internal/dhcp"
@ -73,17 +74,24 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba
return fmt.Errorf("unknown subnet mode %q", d.mode) return fmt.Errorf("unknown subnet mode %q", d.mode)
} }
var gatewayMAC net.HardwareAddr
if d.mode == ModeVxlan {
vtep, err := netif.VTEPAddress(d.localIface)
if err != nil {
return fmt.Errorf("vtep of %s: %w", d.localIface, err)
}
if gatewayMAC, err = hostGatewayMAC(vtep); err != nil {
return err
}
}
if err := netns.Call(d.vpc, func() error { if err := netns.Call(d.vpc, func() error {
if err := netif.CreateBridge(d.bridge, 1500); err != nil { if err := netif.CreateBridge(d.bridge, 1500); err != nil {
return fmt.Errorf("create bridge: %w", err) return fmt.Errorf("create bridge: %w", err)
} }
if d.mode == ModeVxlan { if gatewayMAC != nil {
mac, err := anycastGatewayMAC(d.vxlanID) if err := netif.LinkSetHardwareAddr(d.bridge, gatewayMAC); err != nil {
if err != nil { return fmt.Errorf("set gateway mac %s: %w", gatewayMAC, err)
return err
}
if err := netif.LinkSetHardwareAddr(d.bridge, mac); err != nil {
return fmt.Errorf("set anycast gateway mac %s: %w", mac, err)
} }
} }
return netif.BridgeSetMaster(vethI, d.bridge) return netif.BridgeSetMaster(vethI, d.bridge)

View file

@ -6,14 +6,14 @@ import (
) )
const ( const (
maxVNI = 1<<24 - 1 roleHostGateway = 0x03
roleAnycastGateway = 0x01
locallyAdministered = 0x02 locallyAdministered = 0x02
) )
func anycastGatewayMAC(vni int) (net.HardwareAddr, error) { func hostGatewayMAC(vtep net.IP) (net.HardwareAddr, error) {
if vni < 0 || vni > maxVNI { ip := vtep.To4()
return nil, fmt.Errorf("vxlan_id %d does not fit in 24 bits", vni) if ip == nil {
return nil, fmt.Errorf("vtep %v is not an IPv4 address", vtep)
} }
return net.HardwareAddr{locallyAdministered, roleAnycastGateway, 0x00, byte(vni >> 16), byte(vni >> 8), byte(vni)}, nil return net.HardwareAddr{locallyAdministered, roleHostGateway, ip[0], ip[1], ip[2], ip[3]}, nil
} }

View file

@ -1,31 +1,29 @@
package subnet package subnet
import "testing" import (
"net"
"testing"
)
func TestAnycastGatewayMAC(t *testing.T) { func TestHostGatewayMAC(t *testing.T) {
cases := []struct { cases := map[string]string{
vni int "192.168.14.11": "02:03:c0:a8:0e:0b",
want string "192.168.14.12": "02:03:c0:a8:0e:0c",
}{ "10.0.0.1": "02:03:0a:00:00:01",
{2811, "02:01:00:00:0a:fb"},
{0, "02:01:00:00:00:00"},
{1, "02:01:00:00:00:01"},
{0x123456, "02:01:00:12:34:56"},
{maxVNI, "02:01:00:ff:ff:ff"},
} }
for _, c := range cases { for vtep, want := range cases {
got, err := anycastGatewayMAC(c.vni) got, err := hostGatewayMAC(net.ParseIP(vtep))
if err != nil { if err != nil {
t.Fatalf("VNI %d : erreur inattendue %v", c.vni, err) t.Fatalf("VTEP %s : erreur inattendue %v", vtep, err)
} }
if got.String() != c.want { if got.String() != want {
t.Errorf("VNI %d : %s, %s attendu", c.vni, got, c.want) t.Errorf("VTEP %s : %s, %s attendu", vtep, got, want)
} }
} }
} }
func TestAnycastGatewayMACIsLocalUnicast(t *testing.T) { func TestHostGatewayMACIsLocalUnicast(t *testing.T) {
got, err := anycastGatewayMAC(2811) got, err := hostGatewayMAC(net.ParseIP("192.168.14.11"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -37,22 +35,17 @@ func TestAnycastGatewayMACIsLocalUnicast(t *testing.T) {
} }
} }
func TestAnycastGatewayMACIsIdenticalForTheSameVNIAndDistinctOtherwise(t *testing.T) { func TestHostGatewayMACDiffersFromTheRouterMACOfTheSameHost(t *testing.T) {
a, _ := anycastGatewayMAC(2811) got, _ := hostGatewayMAC(net.ParseIP("192.168.14.11"))
b, _ := anycastGatewayMAC(2811) if got.String() == "02:02:c0:a8:0e:0b" {
c, _ := anycastGatewayMAC(2812) t.Errorf("%s : même MAC que la RMAC du L3VNI de cet hôte", got)
if a.String() != b.String() {
t.Errorf("même VNI, MAC différentes : %s et %s", a, b)
}
if a.String() == c.String() {
t.Errorf("VNI différentes, même MAC : %s", a)
} }
} }
func TestAnycastGatewayMACRejectsVNIOutside24Bits(t *testing.T) { func TestHostGatewayMACRejectsANonIPv4VTEP(t *testing.T) {
for _, vni := range []int{-1, maxVNI + 1} { for _, vtep := range []net.IP{nil, net.ParseIP("fd00::1")} {
if mac, err := anycastGatewayMAC(vni); err == nil { if mac, err := hostGatewayMAC(vtep); err == nil {
t.Errorf("VNI %d accepté : %s", vni, mac) t.Errorf("VTEP %v accepté : %s", vtep, mac)
} }
} }
} }

View file

@ -52,10 +52,10 @@ check "orchestrée, 2 — VM s8-m3 10.242.1.50 sur hv1 pendant que s8-m2 tourne
check "hv1 : s8-m3 répond depuis son netns" netns_ping_wait vp-s8 10.242.1.50 check "hv1 : s8-m3 répond depuis son netns" netns_ping_wait vp-s8 10.242.1.50
NODE NODE
on rr1 <<'NODE' on rr1 <<'NODE'
check "rr1 : MAC de gateway anycast de sn-s8s (02:01:00:00:0a:fb), portée par hv1 et hv2, non annoncée en EVPN" sh -c "! vtysh -c 'show bgp l2vpn evpn route type macip' | grep -qi '02:01:00:00:0a:fb'" check "rr1 : MAC de gateway de sn-s8s propre à chaque hôte, hv1 (02:03:c0:a8:0e:0b) et hv2 (02:03:c0:a8:0e:0c)" sh -c "r=\$(vtysh -c 'show bgp l2vpn evpn route type macip'); echo \"\$r\" | grep -qi '02:03:c0:a8:0e:0b' && echo \"\$r\" | grep -qi '02:03:c0:a8:0e:0c'"
NODE NODE
on hv1 <<'NODE' on hv1 <<'NODE'
info "hv1 : MAC en double détectées sur la VNI 2811 : $(vtysh -c 'show evpn mac vni 2811 duplicate' 2>&1 | grep -ci '02:01:00' || true)" check "hv1 : aucune MAC en double sur la VNI 2811" sh -c "! vtysh -c 'show evpn mac vni 2811 duplicate' | grep -qiE '([0-9a-f]{2}:){5}[0-9a-f]{2}'"
NODE NODE
on hv3 <<'NODE' on hv3 <<'NODE'
for i in $(seq 1 30); do [ "$(bgp_paths vp-s8 10.242.1.50/32)" = 2 ] && break; sleep 2; done for i in $(seq 1 30); do [ "$(bgp_paths vp-s8 10.242.1.50/32)" = 2 ] && break; sleep 2; done