f-57: netif: MAC anycast de la gateway et création d'un macvlan #57

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-13 14:27:34 +02:00
commit fe8c83b1d6
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
4 changed files with 103 additions and 2 deletions

View file

@ -8,8 +8,11 @@ import (
const (
locallyAdministered byte = 0x02
RoleRouter byte = 0x02
RoleHostGateway byte = 0x03
RoleAnycastGateway byte = 0x01
RoleRouter byte = 0x02
RoleHostGateway byte = 0x03
MaxVNI = 1<<24 - 1
)
func HostMAC(role byte, vtep net.IP) (net.HardwareAddr, error) {
@ -19,3 +22,10 @@ func HostMAC(role byte, vtep net.IP) (net.HardwareAddr, error) {
}
return net.HardwareAddr{locallyAdministered, role, ip[0], ip[1], ip[2], ip[3]}, nil
}
func AnycastMAC(vni int) (net.HardwareAddr, error) {
if vni < 1 || vni > MaxVNI {
return nil, fmt.Errorf("vni %d out of range 1-%d", vni, MaxVNI)
}
return net.HardwareAddr{locallyAdministered, RoleAnycastGateway, 0x00, byte(vni >> 16), byte(vni >> 8), byte(vni)}, nil
}

View file

@ -58,3 +58,43 @@ func TestHostMACRejectsANonIPv4VTEP(t *testing.T) {
}
}
}
func TestAnycastMAC(t *testing.T) {
cases := map[int]string{
1: "02:01:00:00:00:01",
2911: "02:01:00:00:0b:5f",
70000: "02:01:00:01:11:70",
MaxVNI: "02:01:00:ff:ff:ff",
0x123456: "02:01:00:12:34:56",
}
for vni, want := range cases {
got, err := AnycastMAC(vni)
if err != nil {
t.Fatalf("VNI %d : erreur inattendue %v", vni, err)
}
if got.String() != want {
t.Errorf("VNI %d : %s, %s attendu", vni, got, want)
}
if got[0]&0x02 == 0 || got[0]&0x01 != 0 {
t.Errorf("VNI %d : %s n'est pas une MAC unicast administrée localement", vni, got)
}
}
}
func TestAnycastMACRefusesOutOfRangeVNI(t *testing.T) {
for _, vni := range []int{0, -1, MaxVNI + 1} {
if _, err := AnycastMAC(vni); err == nil {
t.Errorf("VNI %d : une erreur était attendue", vni)
}
}
}
func TestAnycastMACDiffersFromHostMACs(t *testing.T) {
anycast, _ := AnycastMAC(2911)
for _, role := range []byte{RoleRouter, RoleHostGateway} {
host, _ := HostMAC(role, net.ParseIP("192.168.14.11"))
if host[1] == anycast[1] {
t.Errorf("rôle %#x : même octet de rôle que la MAC anycast (%s, %s)", role, host, anycast)
}
}
}

26
internal/netif/macvlan.go Normal file
View file

@ -0,0 +1,26 @@
package netif
import (
"net"
"github.com/vishvananda/netlink"
)
func macvlanLink(name string, parentIndex int, mac net.HardwareAddr) *netlink.Macvlan {
return &netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
Name: name,
ParentIndex: parentIndex,
HardwareAddr: mac,
},
Mode: netlink.MACVLAN_MODE_BRIDGE,
}
}
func CreateMacvlan(name, parent string, mac net.HardwareAddr) error {
link, err := netlink.LinkByName(parent)
if err != nil {
return err
}
return netlink.LinkAdd(macvlanLink(name, link.Attrs().Index, mac))
}

View file

@ -0,0 +1,25 @@
package netif
import (
"net"
"testing"
"github.com/vishvananda/netlink"
)
func TestMacvlanLink(t *testing.T) {
mac, _ := AnycastMAC(2911)
link := macvlanLink("gw-sn1", 7, mac)
if link.Name != "gw-sn1" || link.ParentIndex != 7 {
t.Errorf("nom ou parent inattendu : %s, %d", link.Name, link.ParentIndex)
}
if link.HardwareAddr.String() != "02:01:00:00:0b:5f" {
t.Errorf("MAC %s, 02:01:00:00:0b:5f attendue", link.HardwareAddr)
}
if link.Mode != netlink.MACVLAN_MODE_BRIDGE {
t.Errorf("mode %v, bridge attendu", link.Mode)
}
if link.Flags&net.FlagUp != 0 {
t.Error("le macvlan doit être créé éteint")
}
}