f-56: agent: MTU unique network.mtu_vm, underlay à 9000 contrôlé à la création, option DHCP 26 et host_mtu #56

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-23 12:10:01 +02:00
commit 8639c4727f
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
37 changed files with 518 additions and 24 deletions

View file

@ -19,6 +19,14 @@ type SubnetConfig struct {
InterfaceIP net.IP
VPCRoute *net.IPNet
DefaultGateway net.IP
MTU int
}
func (c SubnetConfig) InterfaceMTU() int {
if c.MTU == 0 {
return 1500
}
return c.MTU
}
type Host struct {

View file

@ -0,0 +1,68 @@
package dhcpd
import (
"os"
"testing"
"github.com/insomniacslk/dhcp/dhcpv4"
)
func replyMTU(t *testing.T, c SubnetConfig) uint16 {
t.Helper()
h := testHost(t)
reply, err := BuildReply(c, h, request(t, dhcpv4.MessageTypeRequest, h.MAC))
if err != nil {
t.Fatalf("BuildReply: %v", err)
}
m, err := dhcpv4.GetUint16(dhcpv4.OptionInterfaceMTU, reply.Options)
if err != nil {
t.Fatalf("option 26 absente : %v", err)
}
return m
}
func TestBuildReply_CarriesTheInterfaceMTU(t *testing.T) {
c := testConfig(t)
c.MTU = 1450
if got := replyMTU(t, c); got != 1450 {
t.Errorf("option 26 = %d, attendu 1450", got)
}
}
func TestBuildReply_InterfaceMTUIs1500WhenUnset(t *testing.T) {
if got := replyMTU(t, testConfig(t)); got != 1500 {
t.Errorf("option 26 = %d, attendu 1500", got)
}
}
func TestStore_MTUIsPersisted(t *testing.T) {
s, path := loadedStore(t)
c := fullConfig(t)
c.MTU = 1450
if err := s.SetSubnet(c); err != nil {
t.Fatalf("SetSubnet: %v", err)
}
reloaded := NewStore(path)
if err := reloaded.Load(); err != nil {
t.Fatalf("Load: %v", err)
}
if got, _ := reloaded.Subnet(); got.MTU != 1450 {
t.Errorf("mtu relu = %d, attendu 1450", got.MTU)
}
}
func TestStore_StateWithoutMTUIsServedAt1500(t *testing.T) {
path := statePath(t)
legacy := `{"subnet":{"network":"10.0.5.0/24","interface_ip":"10.0.5.1"},"hosts":[]}`
if err := os.WriteFile(path, []byte(legacy), 0600); err != nil {
t.Fatal(err)
}
s := NewStore(path)
if err := s.Load(); err != nil {
t.Fatalf("Load: %v", err)
}
c, _ := s.Subnet()
if got := replyMTU(t, c); got != 1500 {
t.Errorf("ancien état sans mtu : option 26 = %d, attendu 1500", got)
}
}

View file

@ -88,6 +88,7 @@ func BuildReply(c SubnetConfig, h Host, req *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, err
dhcpv4.WithOption(dhcpv4.OptServerIdentifier(c.InterfaceIP)),
dhcpv4.WithOption(dhcpv4.OptDNS(DNSServers()...)),
dhcpv4.WithOption(dhcpv4.OptClasslessStaticRoute(routes...)),
dhcpv4.WithOption(dhcpv4.OptGeneric(dhcpv4.OptionInterfaceMTU, dhcpv4.Uint16(c.InterfaceMTU()).ToBytes())),
}
if h.DefaultRoute && c.DefaultGateway != nil {
mods = append(mods, dhcpv4.WithOption(dhcpv4.OptRouter(c.DefaultGateway)))

View file

@ -20,6 +20,7 @@ type diskSubnet struct {
InterfaceIP string `json:"interface_ip"`
VPCRoute string `json:"vpc_route,omitempty"`
DefaultGateway string `json:"default_gateway,omitempty"`
MTU int `json:"mtu,omitempty"`
}
type diskHost struct {
@ -98,7 +99,7 @@ func subnetFromDisk(d diskSubnet) (SubnetConfig, error) {
return SubnetConfig{}, ErrNoInterfaceIP
}
c := SubnetConfig{Network: network, InterfaceIP: interfaceIP}
c := SubnetConfig{Network: network, InterfaceIP: interfaceIP, MTU: d.MTU}
if d.VPCRoute != "" {
if _, c.VPCRoute, err = net.ParseCIDR(d.VPCRoute); err != nil {
@ -232,6 +233,7 @@ func (s *Store) persist() error {
sub := diskSubnet{
Network: s.subnet.Network.String(),
InterfaceIP: s.subnet.InterfaceIP.String(),
MTU: s.subnet.MTU,
}
if s.subnet.VPCRoute != nil {
sub.VPCRoute = s.subnet.VPCRoute.String()