diff --git a/api/agent.yaml b/api/agent.yaml index 6fcab75..04502fd 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -534,7 +534,7 @@ components: VMInterface: type: object - required: [subnet, ip, primary] + required: [subnet, ip, mac, primary] properties: subnet: type: string @@ -543,6 +543,15 @@ components: type: string format: ipv4 example: "10.0.0.5" + mac: + type: string + pattern: '^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$' + description: > + MAC address of the interface, chosen by the caller. It is given to + QEMU and to the DHCP reservation as is, and returned unchanged by + GET. The agent does not check its format nor its uniqueness: that + is the caller's job. Missing or empty → 400. + example: "52:54:00:00:00:05" primary: type: boolean example: true diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 197d1c3..2ec0446 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -63,6 +63,11 @@ func run(confFile string) int { return 1 } + if err := migration.MigrateVMMACs(db, log.With(slog.String("component", "migration"))); err != nil { + log.Error("vm mac migration failed", "error", err) + return 1 + } + if cfg.BGP.Enabled() { if err := applyBGPBase(cfg, frr.NewClient()); err != nil { log.Error("failed to apply the bgp base configuration", "error", err) diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go index 045e8fd..ff3e5f3 100644 --- a/internal/api/agent/models.go +++ b/internal/api/agent/models.go @@ -42,6 +42,7 @@ type Subnet struct { type VMInterface struct { Subnet string `json:"subnet"` IP string `json:"ip"` + MAC string `json:"mac"` Primary bool `json:"primary"` } diff --git a/internal/api/agent/vm.go b/internal/api/agent/vm.go index a1ab6bc..fe734ea 100644 --- a/internal/api/agent/vm.go +++ b/internal/api/agent/vm.go @@ -100,6 +100,8 @@ func interfacesFromDB(prefix string, entries map[string]string) []VMInterface { byIndex[idx].Subnet = value case "ip": byIndex[idx].IP = value + case "mac": + byIndex[idx].MAC = value case "primary": byIndex[idx].Primary = value == "true" } diff --git a/internal/api/agent/vm_test.go b/internal/api/agent/vm_test.go index 66b43d9..7412d38 100644 --- a/internal/api/agent/vm_test.go +++ b/internal/api/agent/vm_test.go @@ -104,7 +104,7 @@ func TestStartVM_MultiDisk(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-10", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.0.0.10", Primary: true}, + {Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true}, }, Storage: []VMStorage{ {Path: "/data/root.qcow2", Dev: "sda"}, @@ -139,7 +139,7 @@ func TestStartVM_StorageReturnedInResponse(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-11", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.0.0.11", Primary: true}, + {Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true}, }, Storage: []VMStorage{ {Path: "/data/root.qcow2", Dev: "sda"}, @@ -173,7 +173,7 @@ func postVM(t *testing.T, name string, meta VMMetadata) (*httptest.ResponseRecor body, _ := json.Marshal(VMCreateRequest{ Name: name, - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.20", Primary: true}}, + Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.20", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, Memory: 1024, CPUs: 2, @@ -273,6 +273,7 @@ func TestVmFromDB_MultipleInterfacesSortedByIndex(t *testing.T) { "vm/vm-multi/state": "running", "vm/vm-multi/nic/1/subnet": "sn-2", "vm/vm-multi/nic/1/ip": "10.2.0.5", + "vm/vm-multi/nic/1/mac": "52:54:00:aa:bb:cc", "vm/vm-multi/nic/0/subnet": "sn-1", "vm/vm-multi/nic/0/ip": "10.1.0.5", "vm/vm-multi/nic/0/primary": "true", @@ -290,6 +291,9 @@ func TestVmFromDB_MultipleInterfacesSortedByIndex(t *testing.T) { if vm.Interfaces[1].Subnet != "sn-2" || vm.Interfaces[1].Primary { t.Errorf("la seconde doit être l'index 1, non primaire : %+v", vm.Interfaces[1]) } + if vm.Interfaces[1].MAC != "52:54:00:aa:bb:cc" { + t.Errorf("la mac de la seconde doit être rendue : %+v", vm.Interfaces[1]) + } } func TestStartVM_StoresAllInterfaces(t *testing.T) { @@ -303,8 +307,8 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-multi", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.1.0.5", Primary: true}, - {Subnet: "sn-2", IP: "10.2.0.5"}, + {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"}, }, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) @@ -318,6 +322,9 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) { if got, _ := kv.GetFromDB(db, "vm/vm-multi/nic/1/subnet"); got != "sn-2" { t.Errorf("seconde interface non stockée : %q", got) } + if got, _ := kv.GetFromDB(db, "vm/vm-multi/nic/1/mac"); got != "52:54:00:aa:bb:cc" { + t.Errorf("mac de la seconde interface non stockée telle quelle : %q", got) + } if got, _ := kv.GetFromDB(db, "vm/vm-multi/nic/0/primary"); got != "true" { t.Errorf("primaire non marquée : %q", got) } @@ -328,10 +335,10 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) { func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) { cases := map[string][]VMInterface{ - "aucune primaire": {{Subnet: "sn-1", IP: "10.1.0.5"}}, + "aucune primaire": {{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05"}}, "deux primaires": { - {Subnet: "sn-1", IP: "10.1.0.5", Primary: true}, - {Subnet: "sn-2", IP: "10.2.0.5", Primary: true}, + {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:00:00:05", Primary: true}, }, } for label, ifaces := range cases { @@ -354,6 +361,31 @@ func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) { } } +func TestStartVM_RejectsAnInterfaceWithoutMAC(t *testing.T) { + s, db := newTestServer(t) + for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} { + kv.AddInDB(db, "subnet/"+sn+"/state", "running") + kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1") + kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr) + } + body, _ := json.Marshal(VMCreateRequest{ + Name: "vm-nomac", + Interfaces: []VMInterface{ + {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Subnet: "sn-2", IP: "10.2.0.5"}, + }, + Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, + }) + w := httptest.NewRecorder() + s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body))) + if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "interface 1: mac is required") { + t.Errorf("attendu 400 nommant l'interface 1, obtenu %d — %s", w.Code, w.Body.String()) + } + if _, err := kv.GetFromDB(db, "vm/vm-nomac/state"); err == nil { + t.Error("une VM refusée ne doit laisser aucune trace en base") + } +} + func TestStartVM_RejectsReservedOrForeignAddresses(t *testing.T) { cases := map[string]string{ "passerelle anycast": "10.0.0.254", @@ -373,7 +405,7 @@ func TestStartVM_RejectsReservedOrForeignAddresses(t *testing.T) { kv.AddInDB(db, "subnet/sn-1/gateway", "10.0.0.1") body, _ := json.Marshal(VMCreateRequest{ Name: "vm-ip", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: ip, Primary: true}}, + Interfaces: []VMInterface{{Subnet: "sn-1", IP: ip, MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) w := httptest.NewRecorder() @@ -396,7 +428,7 @@ func TestStartVM_AcceptsHostAddressBesideReservedOnes(t *testing.T) { kv.AddInDB(db, "subnet/sn-1/relay_ip", "10.0.0.253") body, _ := json.Marshal(VMCreateRequest{ Name: "vm-ip", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.252", Primary: true}}, + Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.252", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) w := httptest.NewRecorder() @@ -412,7 +444,7 @@ func TestStartVM_RejectsSubnetWithoutCIDR(t *testing.T) { kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1") body, _ := json.Marshal(VMCreateRequest{ Name: "vm-ip", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.5", Primary: true}}, + Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) w := httptest.NewRecorder() diff --git a/internal/api/agent/vms.go b/internal/api/agent/vms.go index 01d7f40..d5ca436 100644 --- a/internal/api/agent/vms.go +++ b/internal/api/agent/vms.go @@ -70,7 +70,12 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) { nics := make([]dispatcher.VMNIC, len(req.Interfaces)) primaries := 0 for i, iface := range req.Interfaces { - nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, Primary: iface.Primary} + if iface.MAC == "" { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: mac is required", i)}) + return + } + nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary} if iface.Primary { primaries++ } diff --git a/internal/dhcp/db.go b/internal/dhcp/db.go deleted file mode 100644 index c93aa1b..0000000 --- a/internal/dhcp/db.go +++ /dev/null @@ -1,19 +0,0 @@ -package dhcp - -import ( - "git.g3e.fr/syonad/two/pkg/db/kv" - "github.com/dgraph-io/badger/v4" -) - -func StoreDHCPEntries(db *badger.DB, subnetName string, entries map[string]string) error { - for ip, mac := range entries { - if err := kv.AddInDB(db, "subnet/"+subnetName+"/dhcp/"+ip, mac); err != nil { - return err - } - } - return nil -} - -func GetMACForIP(db *badger.DB, subnetName, ip string) (string, error) { - return kv.GetFromDB(db, "subnet/"+subnetName+"/dhcp/"+ip) -} diff --git a/internal/dhcp/dhcp_test.go b/internal/dhcp/dhcp_test.go index 79448b3..9ea2445 100644 --- a/internal/dhcp/dhcp_test.go +++ b/internal/dhcp/dhcp_test.go @@ -17,35 +17,6 @@ func parseNet(t *testing.T, cidr string) *net.IPNet { return network } -// --- cloneIP --- - -func TestCloneIP_IsIndependent(t *testing.T) { - ip := net.ParseIP("10.0.0.1").To4() - clone := cloneIP(ip) - clone[3] = 99 - if ip[3] == 99 { - t.Error("cloneIP devrait retourner une copie indépendante") - } -} - -// --- incrementIP --- - -func TestIncrementIP_Simple(t *testing.T) { - ip := net.ParseIP("10.0.0.1").To4() - incrementIP(ip) - if ip.String() != "10.0.0.2" { - t.Errorf("attendu 10.0.0.2, obtenu %s", ip) - } -} - -func TestIncrementIP_Carry(t *testing.T) { - ip := net.ParseIP("10.0.0.255").To4() - incrementIP(ip) - if ip.String() != "10.0.1.0" { - t.Errorf("attendu 10.0.1.0, obtenu %s", ip) - } -} - // --- GenerateConfig --- func newConf(t *testing.T, cidr string) Config { @@ -65,7 +36,7 @@ func newConf(t *testing.T, cidr string) Config { func TestGenerateConfig_CreatesFile(t *testing.T) { conf := newConf(t, "192.168.1.0/29") // 6 hôtes - path, _, err := GenerateConfig(conf) + path, err := GenerateConfig(conf) if err != nil { t.Fatalf("GenerateConfig a échoué : %v", err) } @@ -77,7 +48,7 @@ func TestGenerateConfig_CreatesFile(t *testing.T) { func TestGenerateConfig_FilenameMatchesName(t *testing.T) { conf := newConf(t, "192.168.1.0/29") - path, _, err := GenerateConfig(conf) + path, err := GenerateConfig(conf) if err != nil { t.Fatalf("GenerateConfig a échoué : %v", err) } @@ -90,7 +61,7 @@ func TestGenerateConfig_FilenameMatchesName(t *testing.T) { func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) { conf := newConf(t, "192.168.1.0/29") - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if !strings.Contains(string(content), "dhcp-option=3,192.168.1.1") { @@ -101,7 +72,7 @@ func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) { func TestGenerateConfig_NoDefaultGateway(t *testing.T) { conf := newConf(t, "192.168.1.0/29") conf.DefaultGateway = nil - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if strings.Contains(string(content), "dhcp-option=3,") { @@ -112,7 +83,7 @@ func TestGenerateConfig_NoDefaultGateway(t *testing.T) { func TestGenerateConfig_NoDefaultGatewaySuppressesRouterOption(t *testing.T) { conf := newConf(t, "192.168.1.0/29") conf.DefaultGateway = nil - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if !strings.Contains(string(content), "\ndhcp-option=3\n") { @@ -127,7 +98,7 @@ func TestGenerateConfig_NilDefaultGatewayEmitsNoRoute(t *testing.T) { conf := newConf(t, "192.168.1.0/29") conf.DefaultGateway = nil - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if !strings.Contains(string(content), "dhcp-option=121,") { @@ -163,7 +134,7 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) { Name: "vpc1", ConfDir: t.TempDir(), } - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if !strings.Contains(string(content), "dhcp-range=10.10.0.0,static,255.255.255.0,12h") { @@ -171,18 +142,6 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) { } } -func TestGenerateConfig_OneEntryPerIP(t *testing.T) { - // /29 = 8 adresses. Les entrées ne vont plus dans le fichier dnsmasq mais - // dans la map retournée, que StoreDHCPEntries écrit en base pour GetMACForIP. - _, entries, err := GenerateConfig(newConf(t, "10.0.0.0/29")) - if err != nil { - t.Fatalf("GenerateConfig : %v", err) - } - if len(entries) != 8 { - t.Errorf("attendu 8 entrées ip->mac, obtenu %d", len(entries)) - } -} - func TestGenerateConfig_NoPreGeneratedHosts(t *testing.T) { // Une entrée dhcp-host pré-générée fait rejeter celle du dhcp-hostsdir // (« duplicate dhcp-host IP address »), sans erreur : la VM reçoit alors @@ -212,18 +171,6 @@ func TestGenerateConfig_PointsToDirs(t *testing.T) { } } -func TestGenerateConfig_MACPrefix(t *testing.T) { - _, entries, err := GenerateConfig(newConf(t, "10.0.0.0/30")) - if err != nil { - t.Fatalf("GenerateConfig : %v", err) - } - for ip, mac := range entries { - if !strings.HasPrefix(mac, "00:22:33:") { - t.Errorf("mac de %s sans le préfixe 00:22:33: : %s", ip, mac) - } - } -} - func TestGenerateConfig_CreatesConfDir(t *testing.T) { dir := filepath.Join(t.TempDir(), "sous", "dossier") _, network, _ := net.ParseCIDR("10.0.0.0/30") @@ -233,7 +180,7 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) { Name: "net", ConfDir: dir, } - if _, _, err := GenerateConfig(conf); err != nil { + if _, err := GenerateConfig(conf); err != nil { t.Fatalf("GenerateConfig devrait créer les répertoires manquants : %v", err) } if _, err := os.Stat(dir); os.IsNotExist(err) { @@ -245,7 +192,7 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) { func confLines(t *testing.T, c Config) string { t.Helper() - path, _, err := GenerateConfig(c) + path, err := GenerateConfig(c) if err != nil { t.Fatalf("GenerateConfig : %v", err) } @@ -312,7 +259,7 @@ func TestGenerateConfig_MissingInterfaceIPIsAnError(t *testing.T) { conf := newConf(t, "192.168.1.0/29") conf.InterfaceIP = nil - if _, _, err := GenerateConfig(conf); err == nil { + if _, err := GenerateConfig(conf); err == nil { t.Error("sans interface_ip aucune route metadata n'est possible : il faut échouer, pas écrire une conf muette") } } @@ -321,7 +268,7 @@ func TestGenerateConfig_ListensOnTheAnycastGateway(t *testing.T) { conf := newConf(t, "192.168.1.0/29") conf.Listen = "gw-s9r1" conf.Except = "br-s9r1" - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) for _, line := range []string{"\ninterface=gw-s9r1\n", "\nexcept-interface=br-s9r1\n"} { @@ -333,7 +280,7 @@ func TestGenerateConfig_ListensOnTheAnycastGateway(t *testing.T) { func TestGenerateConfig_NoInterfaceLinesByDefault(t *testing.T) { conf := newConf(t, "192.168.1.0/29") - path, _, _ := GenerateConfig(conf) + path, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if strings.Contains(string(content), "interface=") { diff --git a/internal/dhcp/generate.go b/internal/dhcp/generate.go index f67b27f..6724b62 100644 --- a/internal/dhcp/generate.go +++ b/internal/dhcp/generate.go @@ -2,7 +2,6 @@ package dhcp import ( "fmt" - "net" "os" "path/filepath" "strings" @@ -10,9 +9,9 @@ import ( "git.g3e.fr/syonad/two/internal/metadata" ) -func GenerateConfig(c Config) (string, map[string]string, error) { +func GenerateConfig(c Config) (string, error) { if c.InterfaceIP == nil { - return "", nil, fmt.Errorf("interface ip is required: guests would have no route to the metadata server") + return "", fmt.Errorf("interface ip is required: guests would have no route to the metadata server") } mask := fmt.Sprintf("%d.%d.%d.%d", c.Network.Mask[0], c.Network.Mask[1], c.Network.Mask[2], c.Network.Mask[3]) @@ -35,26 +34,14 @@ func GenerateConfig(c Config) (string, map[string]string, error) { fmt.Fprintf(&sb, "dhcp-hostsdir=%s\n", HostsDir(c.ConfDir, c.Name)) fmt.Fprintf(&sb, "dhcp-optsdir=%s\n", OptsDir(c.ConfDir, c.Name)) - entries := Entries(c.Network) - for _, dir := range []string{c.ConfDir, HostsDir(c.ConfDir, c.Name), OptsDir(c.ConfDir, c.Name)} { if err := os.MkdirAll(dir, 0755); err != nil { - return "", nil, fmt.Errorf("create %s: %w", dir, err) + return "", fmt.Errorf("create %s: %w", dir, err) } } outPath := filepath.Join(c.ConfDir, c.Name+".conf") - return outPath, entries, os.WriteFile(outPath, []byte(sb.String()), 0644) -} - -func Entries(network *net.IPNet) map[string]string { - entries := make(map[string]string) - i := 0 - for ip := cloneIP(network.IP); network.Contains(ip); incrementIP(ip) { - entries[ip.String()] = fmt.Sprintf("00:22:33:%02X:%02X:%02X", (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF) - i++ - } - return entries + return outPath, os.WriteFile(outPath, []byte(sb.String()), 0644) } func classlessRoutes(c Config) []string { @@ -69,18 +56,3 @@ func classlessRoutes(c Config) []string { } return routes } - -func incrementIP(ip net.IP) { - for j := len(ip) - 1; j >= 0; j-- { - ip[j]++ - if ip[j] != 0 { - break - } - } -} - -func cloneIP(ip net.IP) net.IP { - clone := make(net.IP, len(ip)) - copy(clone, ip) - return clone -} diff --git a/internal/dhcpbackend/dnsmasq.go b/internal/dhcpbackend/dnsmasq.go index 9244ebf..e6ac72b 100644 --- a/internal/dhcpbackend/dnsmasq.go +++ b/internal/dhcpbackend/dnsmasq.go @@ -44,7 +44,7 @@ func (b Dnsmasq) config(s Subnet) dhcp.Config { } func (b Dnsmasq) ConfigureSubnet(s Subnet) error { - if _, _, err := dhcp.GenerateConfig(b.config(s)); err != nil { + if _, err := dhcp.GenerateConfig(b.config(s)); err != nil { return fmt.Errorf("generate dhcp config: %w", err) } diff --git a/internal/dispatcher/agent/vm_commands.go b/internal/dispatcher/agent/vm_commands.go index 9f72113..3bdd7ee 100644 --- a/internal/dispatcher/agent/vm_commands.go +++ b/internal/dispatcher/agent/vm_commands.go @@ -23,6 +23,7 @@ type VMDisk struct { type VMNIC struct { Subnet string IP string + MAC string Primary bool } @@ -60,6 +61,9 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if err := kv.AddInDB(db, prefix+"ip", n.IP); err != nil { return fmt.Errorf("store nic %d ip: %w", i, err) } + if err := kv.AddInDB(db, prefix+"mac", n.MAC); err != nil { + return fmt.Errorf("store nic %d mac: %w", i, err) + } if n.Primary { if err := kv.AddInDB(db, prefix+"primary", "true"); err != nil { return fmt.Errorf("store nic %d primary: %w", i, err) diff --git a/internal/dispatcher/agent/vm_commands_test.go b/internal/dispatcher/agent/vm_commands_test.go index 29b3f69..cb77932 100644 --- a/internal/dispatcher/agent/vm_commands_test.go +++ b/internal/dispatcher/agent/vm_commands_test.go @@ -17,7 +17,7 @@ func TestStartVMCommand_Prepare_SingleDisk(t *testing.T) { cmd := StartVMCommand{ Name: "vm-1", - NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", Primary: true}}, + NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}}, Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "sda"}}, } if err := cmd.Prepare(db, nil); err != nil { @@ -41,7 +41,7 @@ func TestStartVMCommand_Prepare_MultiDisk(t *testing.T) { cmd := StartVMCommand{ Name: "vm-2", - NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.6", Primary: true}}, + NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.6", MAC: "52:54:00:00:00:05", Primary: true}}, Disks: []VMDisk{ {Path: "/data/root.qcow2", Dev: "sda"}, {Path: "/data/data.qcow2", Dev: "sdb"}, @@ -74,7 +74,7 @@ func TestStartVMCommand_Prepare_SlotGap(t *testing.T) { // sdb absent au boot — slot réservé pour hotplug cmd := StartVMCommand{ Name: "vm-3", - NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.7", Primary: true}}, + NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.7", MAC: "52:54:00:00:00:05", Primary: true}}, Disks: []VMDisk{ {Path: "/data/root.qcow2", Dev: "sda"}, {Path: "/data/extra.qcow2", Dev: "sdc"}, @@ -103,7 +103,7 @@ func TestStartVMCommand_Prepare_NoVolumePath(t *testing.T) { cmd := StartVMCommand{ Name: "vm-4", - NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.8", Primary: true}}, + NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.8", MAC: "52:54:00:00:00:05", Primary: true}}, Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "sda"}}, } if err := cmd.Prepare(db, nil); err != nil { @@ -166,7 +166,7 @@ func TestStartVMCommand_Prepare_Duplicate(t *testing.T) { cmd := StartVMCommand{ Name: "vm-exist", - NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.9", Primary: true}}, + NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.9", MAC: "52:54:00:00:00:05", Primary: true}}, Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "sda"}}, } if err := cmd.Prepare(db, nil); err == nil { @@ -185,7 +185,7 @@ func prepareWithDocuments(t *testing.T, docs map[string]string) *badger.DB { cmd := StartVMCommand{ Name: "vm-doc", - NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", Primary: true}}, + NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}}, Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "vda"}}, Documents: docs, } diff --git a/internal/migration/mac.go b/internal/migration/mac.go new file mode 100644 index 0000000..b6c4838 --- /dev/null +++ b/internal/migration/mac.go @@ -0,0 +1,84 @@ +package migration + +import ( + "fmt" + "log/slog" + "sort" + "strings" + + "git.g3e.fr/syonad/two/internal/state" + "git.g3e.fr/syonad/two/pkg/db/kv" + + "github.com/dgraph-io/badger/v4" +) + +func MigrateVMMACs(db *badger.DB, log *slog.Logger) error { + vms, err := kv.ListByPrefix(db, "vm/") + if err != nil { + return fmt.Errorf("list vm/: %w", err) + } + + for _, prefix := range nicsWithoutMAC(vms) { + name := strings.Split(prefix, "/")[1] + if st, err := state.Get(db, "vm/"+name); err == nil && st == state.Deleted { + continue + } + + subnet, ip := vms[prefix+"subnet"], vms[prefix+"ip"] + if ip == "" { + return fmt.Errorf("%s has no ip: its mac cannot be recovered", strings.TrimSuffix(prefix, "/")) + } + mac, err := kv.GetFromDB(db, "subnet/"+subnet+"/dhcp/"+ip) + if err != nil { + return fmt.Errorf("%s: no mac for %s in subnet/%s/dhcp: %w", strings.TrimSuffix(prefix, "/"), ip, subnet, err) + } + if err := kv.AddInDB(db, prefix+"mac", mac); err != nil { + return fmt.Errorf("store %smac: %w", prefix, err) + } + log.Info("vm nic mac migrated", "resource", strings.TrimSuffix(prefix, "/"), "mac", mac, "reason", "mac now provided at creation") + } + + subnets, err := kv.ListByPrefix(db, "subnet/") + if err != nil { + return fmt.Errorf("list subnet/: %w", err) + } + for _, name := range subnetsWithMACTable(subnets) { + if err := kv.DeleteInDB(db, "subnet/"+name+"/dhcp"); err != nil { + return fmt.Errorf("delete subnet/%s/dhcp: %w", name, err) + } + log.Info("preloaded mac table removed", "resource", "subnet/"+name, "reason", "mac now provided at creation") + } + return nil +} + +func nicsWithoutMAC(entries map[string]string) []string { + var prefixes []string + for key := range entries { + parts := strings.Split(key, "/") + if len(parts) != 5 || parts[2] != "nic" || parts[4] != "subnet" { + continue + } + prefix := strings.TrimSuffix(key, "subnet") + if _, ok := entries[prefix+"mac"]; !ok { + prefixes = append(prefixes, prefix) + } + } + sort.Strings(prefixes) + return prefixes +} + +func subnetsWithMACTable(entries map[string]string) []string { + seen := make(map[string]bool) + for key := range entries { + parts := strings.SplitN(key, "/", 4) + if len(parts) == 4 && parts[2] == "dhcp" { + seen[parts[1]] = true + } + } + names := make([]string, 0, len(seen)) + for name := range seen { + names = append(names, name) + } + sort.Strings(names) + return names +} diff --git a/internal/migration/mac_test.go b/internal/migration/mac_test.go new file mode 100644 index 0000000..1cd45f3 --- /dev/null +++ b/internal/migration/mac_test.go @@ -0,0 +1,116 @@ +package migration + +import ( + "strings" + "testing" + + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" +) + +func seedPreloadedVM(t *testing.T, db *badger.DB, name, state string, nics map[int][2]string) { + t.Helper() + kv.AddInDB(db, "vm/"+name+"/state", state) + for idx, nic := range nics { + prefix := "vm/" + name + "/nic/" + string(rune('0'+idx)) + "/" + kv.AddInDB(db, prefix+"subnet", nic[0]) + kv.AddInDB(db, prefix+"ip", nic[1]) + } +} + +func seedMACTable(t *testing.T, db *badger.DB, subnet string, macs map[string]string) { + t.Helper() + kv.AddInDB(db, "subnet/"+subnet+"/state", "running") + kv.AddInDB(db, "subnet/"+subnet+"/cidr", "10.0.0.0/24") + for ip, mac := range macs { + kv.AddInDB(db, "subnet/"+subnet+"/dhcp/"+ip, mac) + } +} + +func assertKey(t *testing.T, db *badger.DB, key, want string) { + t.Helper() + got, err := kv.GetFromDB(db, key) + if err != nil || got != want { + t.Errorf("%s = %q (%v), attendu %q", key, got, err, want) + } +} + +func assertNoMACTable(t *testing.T, db *badger.DB) { + t.Helper() + entries, err := kv.ListByPrefix(db, "subnet/") + if err != nil { + t.Fatalf("ListByPrefix : %v", err) + } + for key := range entries { + if strings.Contains(key, "/dhcp/") { + t.Errorf("clé de l'ancienne table restante : %s", key) + } + } +} + +func TestMigrateVMMACs_CopiesTheMACOfEveryInterfaceThenDropsTheTable(t *testing.T) { + db := newNICDB(t) + seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A", "10.1.0.11": "00:22:33:00:00:0B"}) + seedMACTable(t, db, "sn-b", map[string]string{"10.2.0.10": "00:22:33:00:00:0A"}) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.10"}, 1: {"sn-b", "10.2.0.10"}}) + + if err := MigrateVMMACs(db, quietLog()); err != nil { + t.Fatalf("MigrateVMMACs : %v", err) + } + + assertKey(t, db, "vm/vm-1/nic/0/mac", "00:22:33:00:00:0A") + assertKey(t, db, "vm/vm-1/nic/1/mac", "00:22:33:00:00:0A") + assertNoMACTable(t, db) + assertKey(t, db, "subnet/sn-a/cidr", "10.0.0.0/24") +} + +func TestMigrateVMMACs_KeepsAnExistingMAC(t *testing.T) { + db := newNICDB(t) + seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A"}) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.10"}}) + kv.AddInDB(db, "vm/vm-1/nic/0/mac", "52:54:00:ab:cd:ef") + + if err := MigrateVMMACs(db, quietLog()); err != nil { + t.Fatalf("MigrateVMMACs : %v", err) + } + + assertKey(t, db, "vm/vm-1/nic/0/mac", "52:54:00:ab:cd:ef") + assertNoMACTable(t, db) +} + +func TestMigrateVMMACs_IsIdempotent(t *testing.T) { + db := newNICDB(t) + seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A"}) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.10"}}) + + for i := range 2 { + if err := MigrateVMMACs(db, quietLog()); err != nil { + t.Fatalf("passage %d : %v", i+1, err) + } + } + assertKey(t, db, "vm/vm-1/nic/0/mac", "00:22:33:00:00:0A") +} + +func TestMigrateVMMACs_IgnoresDeletedVMs(t *testing.T) { + db := newNICDB(t) + seedPreloadedVM(t, db, "vm-gone", "deleted", map[int][2]string{0: {"sn-gone", "10.9.0.10"}}) + + if err := MigrateVMMACs(db, quietLog()); err != nil { + t.Fatalf("une VM supprimée dont le subnet a disparu ne doit pas bloquer le démarrage : %v", err) + } + if _, err := kv.GetFromDB(db, "vm/vm-gone/nic/0/mac"); err == nil { + t.Error("aucune mac ne doit être inventée pour une VM supprimée") + } +} + +func TestMigrateVMMACs_AMissingEntryFailsAndKeepsTheTable(t *testing.T) { + db := newNICDB(t) + seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A"}) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.99"}}) + + err := MigrateVMMACs(db, quietLog()) + if err == nil || !strings.Contains(err.Error(), "vm/vm-1/nic/0") { + t.Fatalf("erreur = %v, attendu une erreur nommant vm/vm-1/nic/0", err) + } + assertKey(t, db, "subnet/sn-a/dhcp/10.1.0.10", "00:22:33:00:00:0A") +} diff --git a/internal/subnet/create.go b/internal/subnet/create.go index 8de1169..3ac6f31 100644 --- a/internal/subnet/create.go +++ b/internal/subnet/create.go @@ -5,7 +5,6 @@ import ( "net" configuration "git.g3e.fr/syonad/two/internal/config/agent" - "git.g3e.fr/syonad/two/internal/dhcp" "git.g3e.fr/syonad/two/internal/dhcpbackend" "git.g3e.fr/syonad/two/internal/ebtables" "git.g3e.fr/syonad/two/internal/l3vni" @@ -139,7 +138,7 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba } } - return startDHCP(db, subnetName, d, backend) + return startDHCP(subnetName, d, backend) } func setupVxlanHost(d subnetData, vethE string) error { @@ -165,16 +164,12 @@ func setupVxlanHost(d subnetData, vethE string) error { return nil } -func startDHCP(db *badger.DB, subnetName string, d subnetData, backend dhcpbackend.Backend) error { +func startDHCP(subnetName string, d subnetData, backend dhcpbackend.Backend) error { defaultGateway, vpcRoute, err := dhcpRouting(d, netif.GetDefaultGateway) if err != nil { return err } - if err := dhcp.StoreDHCPEntries(db, subnetName, dhcp.Entries(d.cidr)); err != nil { - return fmt.Errorf("store dhcp entries: %w", err) - } - listen := "" if d.anycast() { listen = d.gatewayLink() diff --git a/internal/subnet/delete.go b/internal/subnet/delete.go index f528eb5..7d45948 100644 --- a/internal/subnet/delete.go +++ b/internal/subnet/delete.go @@ -10,7 +10,6 @@ import ( "git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/internal/state" - "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" ) @@ -40,7 +39,7 @@ func DeleteSubnet(db *badger.DB, subnetName string, cfg *configuration.Config) e } } - if err := stopDHCP(db, subnetName, d, backend); err != nil { + if err := stopDHCP(subnetName, d, backend); err != nil { return err } @@ -60,19 +59,12 @@ func DeleteSubnet(db *badger.DB, subnetName string, cfg *configuration.Config) e return state.Set(db, "subnet/"+subnetName, state.Deleted) } -func stopDHCP(db *badger.DB, subnetName string, d subnetData, backend dhcpbackend.Backend) error { - if err := backend.TeardownSubnet(dhcpbackend.Subnet{ +func stopDHCP(subnetName string, d subnetData, backend dhcpbackend.Backend) error { + return backend.TeardownSubnet(dhcpbackend.Subnet{ Name: subnetName, VPC: d.vpc, Bridge: d.bridge, - }); err != nil { - return err - } - - if err := kv.DeleteInDB(db, "subnet/"+subnetName+"/dhcp"); err != nil { - return fmt.Errorf("delete dhcp entries: %w", err) - } - return nil + }) } func deleteSubnetVxlan(d subnetData) error { diff --git a/internal/vm/data.go b/internal/vm/data.go index 78b88a4..7b39b06 100644 --- a/internal/vm/data.go +++ b/internal/vm/data.go @@ -8,7 +8,6 @@ import ( "strconv" "strings" - "git.g3e.fr/syonad/two/internal/dhcp" "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" ) @@ -215,11 +214,10 @@ func loadNIC(db *badger.DB, name string, idx int, entries map[string]string) (ni return n, fmt.Errorf("nic %d of vm %s has no ip", idx, name) } - mac, err := dhcp.GetMACForIP(db, n.subnetName, n.ip) - if err != nil { - return n, fmt.Errorf("get mac for ip %s: %w", n.ip, err) + n.mac = entries[prefix+"mac"] + if n.mac == "" { + return n, fmt.Errorf("nic %d of vm %s has no mac", idx, name) } - n.mac = mac if tapIDStr, ok := entries[prefix+"tap_id"]; ok { tapID, err := strconv.Atoi(tapIDStr) diff --git a/internal/vm/data_test.go b/internal/vm/data_test.go index be4ffcf..d7f7a62 100644 --- a/internal/vm/data_test.go +++ b/internal/vm/data_test.go @@ -3,6 +3,7 @@ package vm import ( "fmt" "strconv" + "strings" "testing" "git.g3e.fr/syonad/two/pkg/db/kv" @@ -20,8 +21,8 @@ func newVMInDB(t *testing.T) *badger.DB { kv.AddInDB(db, "subnet/sn-000001/interface_ip", "10.1.1.1") kv.AddInDB(db, "subnet/sn-000001/mode", "vxlan") kv.AddInDB(db, "vpc/vp-admin/cidr", "192.168.0.0/16") - kv.AddInDB(db, "subnet/sn-000001/dhcp/10.1.1.2", "00:22:33:00:01:02") kv.AddInDB(db, "vm/vm-1/nic/0/ip", "10.1.1.2") + kv.AddInDB(db, "vm/vm-1/nic/0/mac", "00:22:33:00:01:02") kv.AddInDB(db, "vm/vm-1/metadata_port", "8081") kv.AddInDB(db, "vm/vm-1/disk/vda", "/data/root.qcow2") kv.AddInDB(db, "vm/vm-1/memory", "2048") @@ -101,6 +102,7 @@ func addNIC(t *testing.T, db *badger.DB, idx int, subnet, ip, mac string, primar prefix := fmt.Sprintf("vm/vm-1/nic/%d/", idx) kv.AddInDB(db, prefix+"subnet", subnet) kv.AddInDB(db, prefix+"ip", ip) + kv.AddInDB(db, prefix+"mac", mac) if primary { kv.AddInDB(db, prefix+"primary", "true") } @@ -108,7 +110,6 @@ func addNIC(t *testing.T, db *badger.DB, idx int, subnet, ip, mac string, primar kv.AddInDB(db, "subnet/"+subnet+"/interface_ip", "10.1.1.1") kv.AddInDB(db, "subnet/"+subnet+"/mode", "vxlan") kv.AddInDB(db, "vpc/vp-admin/cidr", "192.168.0.0/16") - kv.AddInDB(db, "subnet/"+subnet+"/dhcp/"+ip, mac) } func TestLoadVM_SingleNIC(t *testing.T) { @@ -143,6 +144,29 @@ func TestLoadVM_MultipleNICsSortedByIndex(t *testing.T) { } } +func TestLoadVM_MACComesFromTheInterface(t *testing.T) { + db := newVMInDB(t) + addNIC(t, db, 1, "sn-000002", "10.2.0.5", "52:54:00:ab:cd:ef", false) + + d, err := loadVM(db, "vm-1") + if err != nil { + t.Fatalf("loadVM : %v", err) + } + if d.nics[1].mac != "52:54:00:ab:cd:ef" { + t.Errorf("mac = %q, attendu celle fournie à la création, sans lien avec l'IP", d.nics[1].mac) + } +} + +func TestLoadVM_InterfaceWithoutMACIsAnError(t *testing.T) { + db := newVMInDB(t) + kv.AddInDB(db, "vm/vm-1/nic/1/subnet", "sn-000001") + kv.AddInDB(db, "vm/vm-1/nic/1/ip", "10.1.1.3") + + if _, err := loadVM(db, "vm-1"); err == nil || !strings.Contains(err.Error(), "has no mac") { + t.Errorf("loadVM = %v, attendu une erreur sur la mac absente", err) + } +} + func TestLoadVM_TapAllocatedPerNIC(t *testing.T) { db := newVMInDB(t) addNIC(t, db, 1, "sn-000002", "10.2.0.5", "00:22:33:00:02:05", false) diff --git a/internal/watchdog/check_dhcp.go b/internal/watchdog/check_dhcp.go index a3f5bf7..55d1035 100644 --- a/internal/watchdog/check_dhcp.go +++ b/internal/watchdog/check_dhcp.go @@ -8,7 +8,6 @@ import ( "strings" dhcpapi "git.g3e.fr/syonad/two/internal/api/dhcp" - "git.g3e.fr/syonad/two/internal/dhcp" "git.g3e.fr/syonad/two/internal/dhcpbackend" "git.g3e.fr/syonad/two/internal/state" "git.g3e.fr/syonad/two/internal/watchdog/notify" @@ -111,9 +110,9 @@ func expectedVMHosts(db *badger.DB, vmName, subnetName string) ([]dhcpapi.Host, if ip == "" { return nil, fmt.Errorf("nic %d of vm %s has no ip", idx, vmName) } - mac, err := dhcp.GetMACForIP(db, subnetName, ip) - if err != nil { - return nil, fmt.Errorf("get mac for ip %s: %w", ip, err) + mac := entries[nic+"mac"] + if mac == "" { + return nil, fmt.Errorf("nic %d of vm %s has no mac", idx, vmName) } hosts = append(hosts, dhcpapi.Host{ diff --git a/internal/watchdog/check_dhcp_test.go b/internal/watchdog/check_dhcp_test.go index a39e4c3..f8620af 100644 --- a/internal/watchdog/check_dhcp_test.go +++ b/internal/watchdog/check_dhcp_test.go @@ -69,13 +69,13 @@ func seedSubnetWithVM(t *testing.T, db *badger.DB, vmName, ip, mac string, prima t.Helper() seedKV(t, db, "subnet/"+testSubnetName+"/vpc", testVPC) - seedKV(t, db, "subnet/"+testSubnetName+"/dhcp/"+ip, mac) if err := state.Set(db, "vm/"+vmName, state.Running); err != nil { t.Fatalf("state.Set: %v", err) } seedKV(t, db, "vm/"+vmName+"/nic/0/subnet", testSubnetName) seedKV(t, db, "vm/"+vmName+"/nic/0/ip", ip) + seedKV(t, db, "vm/"+vmName+"/nic/0/mac", mac) seedKV(t, db, "vm/"+vmName+"/nic/0/primary", fmt.Sprintf("%t", primary)) } @@ -147,13 +147,14 @@ func TestExpectedHosts_SortsByMAC(t *testing.T) { } } -func TestExpectedHosts_ReportsAnIPWithoutAMAC(t *testing.T) { +func TestExpectedHosts_ReportsAnInterfaceWithoutAMAC(t *testing.T) { db := newTestDB(t) - seedSubnetWithVM(t, db, "vm-web", "10.0.5.10", "00:22:33:00:00:0A", true) - seedKV(t, db, "vm/vm-web/nic/0/ip", "10.0.5.99") + seedResource(t, db, "vm/", "vm-web", state.Running) + seedKV(t, db, "vm/vm-web/nic/0/subnet", testSubnetName) + seedKV(t, db, "vm/vm-web/nic/0/ip", "10.0.5.10") if _, _, err := expectedHosts(db, testSubnetName); err == nil { - t.Fatal("an ip absent from the address plan must be reported, not skipped") + t.Fatal("an interface without a mac must be reported, not skipped") } }