f-95: netif: MAC de chaque NIC figée sur son tap (static sticky) #95

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-31 21:26:08 +01:00
commit 7e0b03ae6d
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
4 changed files with 111 additions and 6 deletions

View file

@ -3,14 +3,42 @@
package netif
import (
"bytes"
"fmt"
"net"
"syscall"
"git.g3e.fr/syonad/two/internal/netns"
"github.com/vishvananda/netlink"
)
func CreateTap(tapID int, bridgeName, vpcName string, mtu int) error {
name := fmt.Sprintf("tap%d", tapID)
func tapName(tapID int) string {
return fmt.Sprintf("tap%d", tapID)
}
func tapMACEntry(tapIndex int, mac net.HardwareAddr) *netlink.Neigh {
return &netlink.Neigh{
LinkIndex: tapIndex,
Family: syscall.AF_BRIDGE,
State: netlink.NUD_NOARP,
Flags: netlink.NTF_MASTER | netlink.NTF_STICKY,
HardwareAddr: mac,
}
}
func hasTapMAC(entries []netlink.Neigh, tapIndex int, mac net.HardwareAddr) bool {
for _, e := range entries {
if e.LinkIndex == tapIndex && e.MasterIndex != 0 && e.Vlan == 0 &&
e.State&netlink.NUD_NOARP != 0 && e.Flags&netlink.NTF_STICKY != 0 &&
bytes.Equal(e.HardwareAddr, mac) {
return true
}
}
return false
}
func CreateTap(tapID int, bridgeName, vpcName string, mtu int, mac net.HardwareAddr) error {
name := tapName(tapID)
return netns.Call(vpcName, func() error {
tap := &netlink.Tuntap{
@ -23,12 +51,31 @@ func CreateTap(tapID int, bridgeName, vpcName string, mtu int) error {
if err := BridgeSetMaster(name, bridgeName); err != nil {
return err
}
link, err := netlink.LinkByName(name)
if err != nil {
return err
}
if err := netlink.NeighSet(tapMACEntry(link.Attrs().Index, mac)); err != nil {
return fmt.Errorf("pin %s on %s: %w", mac, name, err)
}
return LinkSetUp(name)
})
}
func TapHasMAC(tapID int, mac net.HardwareAddr) (bool, error) {
link, err := netlink.LinkByName(tapName(tapID))
if err != nil {
return false, err
}
entries, err := netlink.NeighList(link.Attrs().Index, syscall.AF_BRIDGE)
if err != nil {
return false, err
}
return hasTapMAC(entries, link.Attrs().Index, mac), nil
}
func DeleteTap(tapID int, vpcName string) error {
return netns.Call(vpcName, func() error {
return DeleteLink(fmt.Sprintf("tap%d", tapID))
return DeleteLink(tapName(tapID))
})
}

View file

@ -0,0 +1,47 @@
//go:build linux
package netif
import (
"net"
"syscall"
"testing"
"github.com/vishvananda/netlink"
)
func TestTapMACEntry(t *testing.T) {
mac, _ := net.ParseMAC("52:54:00:34:01:32")
e := tapMACEntry(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 != 0x40 || e.Flags != 0x04|0x40 {
t.Errorf("état %#x et drapeaux %#x, entrée static (NUD_NOARP) master sticky attendue", e.State, e.Flags)
}
}
func TestHasTapMAC(t *testing.T) {
mac, _ := net.ParseMAC("52:54:00:34:01:32")
other, _ := net.ParseMAC("52:54:00:34:01:33")
pinned := netlink.Neigh{LinkIndex: 7, MasterIndex: 3, State: netlink.NUD_NOARP, Flags: netlink.NTF_MASTER | netlink.NTF_STICKY, HardwareAddr: mac}
cases := []struct {
label string
entries []netlink.Neigh
want bool
}{
{"static sticky sur le tap", []netlink.Neigh{pinned}, true},
{"static sans sticky", []netlink.Neigh{{LinkIndex: 7, MasterIndex: 3, State: netlink.NUD_NOARP, Flags: netlink.NTF_MASTER, HardwareAddr: mac}}, false},
{"apprise", []netlink.Neigh{{LinkIndex: 7, MasterIndex: 3, State: netlink.NUD_REACHABLE, Flags: netlink.NTF_MASTER, HardwareAddr: mac}}, false},
{"seulement l'entrée du vlan 1", []netlink.Neigh{{LinkIndex: 7, MasterIndex: 3, Vlan: 1, State: netlink.NUD_NOARP, Flags: netlink.NTF_MASTER | netlink.NTF_STICKY, HardwareAddr: mac}}, false},
{"déplacée sur un autre port", []netlink.Neigh{{LinkIndex: 9, MasterIndex: 3, State: netlink.NUD_REACHABLE, Flags: netlink.NTF_MASTER, HardwareAddr: mac}}, false},
{"liste propre du tap, hors pont", []netlink.Neigh{{LinkIndex: 7, State: netlink.NUD_NOARP, Flags: netlink.NTF_SELF | netlink.NTF_STICKY, HardwareAddr: mac}}, false},
{"autre MAC", []netlink.Neigh{{LinkIndex: 7, MasterIndex: 3, State: netlink.NUD_NOARP, Flags: netlink.NTF_MASTER | netlink.NTF_STICKY, HardwareAddr: other}}, false},
{"aucune entrée", nil, false},
}
for _, c := range cases {
if got := hasTapMAC(c.entries, 7, mac); got != c.want {
t.Errorf("%s : %v, %v attendu", c.label, got, c.want)
}
}
}

View file

@ -2,12 +2,19 @@
package netif
import "errors"
import (
"errors"
"net"
)
func CreateTap(_ int, _, _ string, _ int) error {
func CreateTap(_ int, _, _ string, _ int, _ net.HardwareAddr) error {
return errors.New("netif: tap not supported on this platform")
}
func TapHasMAC(_ int, _ net.HardwareAddr) (bool, error) {
return false, errors.New("netif: tap not supported on this platform")
}
func DeleteTap(_ int, _ string) error {
return errors.New("netif: tap not supported on this platform")
}

View file

@ -53,7 +53,11 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
}
for _, n := range d.nics {
if err := netif.CreateTap(n.tapID, n.bridge, n.vpcName, cfg.Network.VM()); err != nil {
mac, err := net.ParseMAC(n.mac)
if err != nil {
return fmt.Errorf("mac of interface %d: %w", n.index, err)
}
if err := netif.CreateTap(n.tapID, n.bridge, n.vpcName, cfg.Network.VM(), mac); err != nil {
return fmt.Errorf("create tap of interface %d: %w", n.index, err)
}
}