f-41: vm: /32 de chaque interface annoncé dans la VRF #41

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-11 20:32:16 +02:00
commit 03029eabe8
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
5 changed files with 125 additions and 0 deletions

View file

@ -78,6 +78,12 @@ le nom de l'interface dans le guest. Exactement une interface doit être ``prima
la route par défaut et le serveur de metadata. Tous les subnets d'une VM doivent appartenir au la route par défaut et le serveur de metadata. Tous les subnets d'une VM doivent appartenir au
**même VPC**. **même VPC**.
**Routes.** Dans un VPC routé par ``l3vni``, chaque interface sur un subnet ``vxlan`` est annoncée
en ``/32`` dans la VRF du VPC une fois QEMU démarré, juste avant le passage en ``running``. À la
suppression, ces ``/32`` sont retirés **avant** l'arrêt de QEMU. Comme un subnet ne peut être
supprimé tant qu'une VM l'utilise, le ``/32`` d'une VM est toujours retiré avant le ``/24`` de son
subnet — l'ordre qui évite le trou noir de `#41 <https://git.g3e.fr/syonad/two/issues/41>`_.
**Stockage.** Un seul disque ``vdX`` (virtio-blk) par VM ; les disques supplémentaires passent **Stockage.** Un seul disque ``vdX`` (virtio-blk) par VM ; les disques supplémentaires passent
par le contrôleur SCSI (``sdX``). Cette contrainte vient de la carte PCI figée — voir par le contrôleur SCSI (``sdX``). Cette contrainte vient de la carte PCI figée — voir
:doc:`/architecture/contraintes`. :doc:`/architecture/contraintes`.

View file

@ -15,6 +15,7 @@ import (
"git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/internal/netns"
"git.g3e.fr/syonad/two/internal/qemu" "git.g3e.fr/syonad/two/internal/qemu"
"git.g3e.fr/syonad/two/internal/state" "git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/internal/vpc"
"github.com/dgraph-io/badger/v4" "github.com/dgraph-io/badger/v4"
) )
@ -111,6 +112,10 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
return fmt.Errorf("start qemu: %w", err) return fmt.Errorf("start qemu: %w", err)
} }
if err := applyHostRoutes(db, d, vpc.ExportRoute); err != nil {
return err
}
return state.Set(db, "vm/"+name, state.Running) return state.Set(db, "vm/"+name, state.Running)
} }

View file

@ -14,6 +14,7 @@ import (
"git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/internal/netns"
"git.g3e.fr/syonad/two/internal/qmp" "git.g3e.fr/syonad/two/internal/qmp"
"git.g3e.fr/syonad/two/internal/state" "git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/internal/vpc"
"github.com/dgraph-io/badger/v4" "github.com/dgraph-io/badger/v4"
) )
@ -38,6 +39,10 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
return err return err
} }
if err := applyHostRoutes(db, d, vpc.WithdrawRoute); err != nil {
return err
}
socketPath := filepath.Join(cfg.QEMU.QMPDir, name+".sock") socketPath := filepath.Join(cfg.QEMU.QMPDir, name+".sock")
if _, err := os.Stat(socketPath); err == nil { if _, err := os.Stat(socketPath); err == nil {

43
internal/vm/routes.go Normal file
View file

@ -0,0 +1,43 @@
package vm
import (
"fmt"
"net"
"github.com/dgraph-io/badger/v4"
)
type hostRoute struct {
vpc string
dst *net.IPNet
}
type routeFunc func(db *badger.DB, vpcName string, dst *net.IPNet) error
func hostRoutes(d vmData) ([]hostRoute, error) {
var routes []hostRoute
for _, n := range d.nics {
if n.mode != "vxlan" {
continue
}
ip := net.ParseIP(n.ip).To4()
if ip == nil {
return nil, fmt.Errorf("interface %d: %q is not an IPv4 address", n.index, n.ip)
}
routes = append(routes, hostRoute{vpc: n.vpcName, dst: &net.IPNet{IP: ip, Mask: net.CIDRMask(32, 32)}})
}
return routes, nil
}
func applyHostRoutes(db *badger.DB, d vmData, apply routeFunc) error {
routes, err := hostRoutes(d)
if err != nil {
return err
}
for _, r := range routes {
if err := apply(db, r.vpc, r.dst); err != nil {
return err
}
}
return nil
}

View file

@ -0,0 +1,66 @@
package vm
import (
"errors"
"net"
"reflect"
"strings"
"testing"
"github.com/dgraph-io/badger/v4"
)
func routedVM() vmData {
return vmData{nics: []nicData{
{index: 0, vpcName: "vp-s7", mode: "vxlan", ip: "10.241.1.10"},
{index: 1, vpcName: "vp-s7", mode: "bridge", ip: "192.168.50.10"},
{index: 2, vpcName: "vp-s7", mode: "vxlan", ip: "10.241.2.10"},
}}
}
type recorder struct {
calls []string
fail string
}
func (r *recorder) apply(_ *badger.DB, vpcName string, dst *net.IPNet) error {
r.calls = append(r.calls, vpcName+" "+dst.String())
if dst.String() == r.fail {
return errors.New("refused")
}
return nil
}
func TestApplyHostRoutesCoversEveryVxlanInterfaceAsA32(t *testing.T) {
r := &recorder{}
if err := applyHostRoutes(nil, routedVM(), r.apply); err != nil {
t.Fatal(err)
}
want := []string{"vp-s7 10.241.1.10/32", "vp-s7 10.241.2.10/32"}
if !reflect.DeepEqual(r.calls, want) {
t.Errorf("appels %q, attendu %q", r.calls, want)
}
}
func TestApplyHostRoutesStopsAtTheFirstError(t *testing.T) {
r := &recorder{fail: "10.241.1.10/32"}
if err := applyHostRoutes(nil, routedVM(), r.apply); err == nil || err.Error() != "refused" {
t.Errorf("erreur %v", err)
}
if len(r.calls) != 1 {
t.Errorf("appels après l'échec : %q", r.calls)
}
}
func TestApplyHostRoutesRefusesANonIPv4AddressBeforeAnyCall(t *testing.T) {
d := routedVM()
d.nics[2].ip = "fd00::10"
r := &recorder{}
err := applyHostRoutes(nil, d, r.apply)
if err == nil || !strings.Contains(err.Error(), `interface 2: "fd00::10" is not an IPv4 address`) {
t.Errorf("erreur %v", err)
}
if len(r.calls) != 0 {
t.Errorf("appels : %q", r.calls)
}
}