From e22c5f9e37efe726d2a47203c61945ed2f1cb5d4 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Wed, 20 Mar 2024 17:24:02 +0900 Subject: [PATCH 1/2] Add route support to device setup functions --- cmd/apply.go | 27 +++++++++++++++++---------- config/config.go | 28 ++++++++++++++++++---------- iproute2/iproute2.go | 12 ++++++------ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/cmd/apply.go b/cmd/apply.go index 389028c..b01b0c5 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -69,20 +69,27 @@ var applyCmd = &cobra.Command{ }, } -func SetupDevice(name string, addresses []string) error { +func SetupDevice(name string, addresses []string, routes []config.Route) error { err := SetLinkUp(name) if err != nil { return err } slog.Info("add addresses", "name", name, "addresses", addresses) - for _, address := range addresses { err := ip.AddAddress(name, address) if err != nil { return err } } + + for _, route := range routes { + slog.Info("add route", "name", name, "to", route.To, "via", route.Via) + err := ip.AddRoute(route.To, route.Via, name) + if err != nil { + return err + } + } return nil } @@ -98,7 +105,7 @@ func SetupLoopback(netns string) error { }) } -func SetupEthernets(netns string, ethernets map[string]config.EthernetConfig) error { +func SetupEthernets(netns string, ethernets map[string]config.Ethernet) error { for name, values := range ethernets { err := ip.SetNetns(name, netns) if err != nil { @@ -106,13 +113,13 @@ func SetupEthernets(netns string, ethernets map[string]config.EthernetConfig) er } ip.IntoNetns(netns, func() error { - return SetupDevice(name, values.Addresses) + return SetupDevice(name, values.Addresses, values.Routes) }) } return nil } -func SetupDummyDevices(netns string, devices map[string]config.EthernetConfig) error { +func SetupDummyDevices(netns string, devices map[string]config.Ethernet) error { for name, values := range devices { ip.IntoNetns(netns, func() error { slog.Info("add dummy device", "name", name, "netns", netns) @@ -121,13 +128,13 @@ func SetupDummyDevices(netns string, devices map[string]config.EthernetConfig) e return err } - return SetupDevice(name, values.Addresses) + return SetupDevice(name, values.Addresses, values.Routes) }) } return nil } -func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig) error { +func SetupVethDevices(netns string, devices map[string]config.VethDevice) error { for name, values := range devices { peerName := values.Peer.Name peerNetns := values.Peer.Netns @@ -143,7 +150,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig) return err } ip.IntoNetns(netns, func() error { - return SetupDevice(name, values.Addresses) + return SetupDevice(name, values.Addresses, values.Routes) }) if peerNetns != "" { @@ -152,10 +159,10 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig) return err } ip.IntoNetns(netns, func() error { - return SetupDevice(peerName, values.Peer.Addresses) + return SetupDevice(peerName, values.Peer.Addresses, values.Peer.Routes) }) } else { - SetupDevice(peerName, values.Peer.Addresses) + SetupDevice(peerName, values.Peer.Addresses, values.Peer.Routes) } } return nil diff --git a/config/config.go b/config/config.go index b9b742d..6d52a21 100644 --- a/config/config.go +++ b/config/config.go @@ -28,28 +28,36 @@ import ( ) type Config struct { - Netns map[string]NetnsConfig `yaml:"netns"` + Netns map[string]Netns `yaml:"netns"` } -type NetnsConfig struct { - Ethernets map[string]EthernetConfig `yaml:"ethernets,omitempty"` - DummyDevices map[string]EthernetConfig `yaml:"dummy-devices,omitempty"` - VethDevices map[string]VethDeviceConfig `yaml:"veth-devices,omitempty"` +type Netns struct { + Ethernets map[string]Ethernet `yaml:"ethernets,omitempty"` + DummyDevices map[string]Ethernet `yaml:"dummy-devices,omitempty"` + VethDevices map[string]VethDevice `yaml:"veth-devices,omitempty"` } -type EthernetConfig struct { +type Ethernet struct { Addresses []string `yaml:"addresses"` + Routes []Route `yaml:"routes,omitempty"` } -type VethDeviceConfig struct { - Addresses []string `yaml:"addresses"` - Peer PeerConfig `yaml:"peer"` +type VethDevice struct { + Addresses []string `yaml:"addresses"` + Routes []Route `yaml:"routes,omitempty"` + Peer Peer `yaml:"peer"` } -type PeerConfig struct { +type Peer struct { Name string `yaml:"name"` Netns string `yaml:"netns,omitempty"` Addresses []string `yaml:"addresses"` + Routes []Route `yaml:"routes,omitempty"` +} + +type Route struct { + To string `yaml:"to"` + Via string `yaml:"via"` } func LoadConfig(path string) (*Config, error) { diff --git a/iproute2/iproute2.go b/iproute2/iproute2.go index 6e7aa9a..b5bfc31 100644 --- a/iproute2/iproute2.go +++ b/iproute2/iproute2.go @@ -71,8 +71,8 @@ func (i *Iproute2) AddDummyDevice(name string) error { return i.AddLink(name, "dummy") } -func (i *Iproute2) AddVethDevice(name1 string, name2 string) error { - return i.AddLink(name1, "veth", "peer", "name", name2) +func (i *Iproute2) AddVethDevice(name string, peerName string) error { + return i.AddLink(name, "veth", "peer", "name", peerName) } func (i *Iproute2) SetLinkUp(name string) error { @@ -87,12 +87,12 @@ func (i *Iproute2) DelAddress(name string, address string) error { return i.execute("address", "del", address, "dev", name) } -func (i *Iproute2) AddRoute(dst string, via string, dev string) error { - return i.execute("route", "add", dst, "via", via, "dev", dev) +func (i *Iproute2) AddRoute(name string, to string, via string) error { + return i.execute("route", "add", to, "via", via, "dev", name) } -func (i *Iproute2) DelRoute(dst string, via string, dev string) error { - return i.execute("route", "del", dst, "via", via, "dev", dev) +func (i *Iproute2) DelRoute(name string, to string, via string) error { + return i.execute("route", "del", to, "via", via, "dev", name) } func (i *Iproute2) AddNetns(name string) error { From 1cd723bdf384d314fc3e0478e22b3c91862b3c09 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Wed, 20 Mar 2024 17:27:48 +0900 Subject: [PATCH 2/2] Add logging to SetupEthernets function --- cmd/apply.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/apply.go b/cmd/apply.go index b01b0c5..0aaeae0 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -107,6 +107,7 @@ func SetupLoopback(netns string) error { func SetupEthernets(netns string, ethernets map[string]config.Ethernet) error { for name, values := range ethernets { + slog.Info("set netns", "name", name, "netns", netns) err := ip.SetNetns(name, netns) if err != nil { return err