From 3a910ab76800b9608b05e6b188730a2290b51ae0 Mon Sep 17 00:00:00 2001 From: Filip Tehlar Date: Thu, 8 Jun 2023 17:39:39 +0200 Subject: hs-test: rename address allocator Type: test Change-Id: I1745719315d4e0785df5a03aa4312f84c40cb18f Signed-off-by: Filip Tehlar --- extras/hs-test/address_allocator.go | 42 ++++++++++++++++++++++++ extras/hs-test/hst_suite.go | 64 +++++++------------------------------ extras/hs-test/netconfig.go | 10 +++--- extras/hs-test/vppinstance.go | 2 +- 4 files changed, 59 insertions(+), 59 deletions(-) create mode 100644 extras/hs-test/address_allocator.go (limited to 'extras') diff --git a/extras/hs-test/address_allocator.go b/extras/hs-test/address_allocator.go new file mode 100644 index 00000000000..c08f88958dc --- /dev/null +++ b/extras/hs-test/address_allocator.go @@ -0,0 +1,42 @@ +package main + +import "fmt" + +type AddressCounter = int + +type Ip4AddressAllocator struct { + networks map[int]AddressCounter +} + +func (a *Ip4AddressAllocator) AddNetwork(networkNumber int) { + a.networks[networkNumber] = 1 +} + +func (a *Ip4AddressAllocator) NewIp4InterfaceAddress(inputNetworkNumber ...int) (string, error) { + var networkNumber int = 0 + if len(inputNetworkNumber) > 0 { + networkNumber = inputNetworkNumber[0] + } + + if _, ok := a.networks[networkNumber]; !ok { + a.AddNetwork(networkNumber) + } + + numberOfAddresses := a.networks[networkNumber] + + if numberOfAddresses == 254 { + return "", fmt.Errorf("no available IPv4 addresses") + } + + address := fmt.Sprintf("10.10.%v.%v/24", networkNumber, numberOfAddresses) + a.networks[networkNumber] = numberOfAddresses + 1 + + return address, nil +} + +func NewIp4AddressAllocator() *Ip4AddressAllocator { + var ip4AddrAllocator = new(Ip4AddressAllocator) + ip4AddrAllocator.networks = make(map[int]AddressCounter) + ip4AddrAllocator.AddNetwork(0) + return ip4AddrAllocator +} diff --git a/extras/hs-test/hst_suite.go b/extras/hs-test/hst_suite.go index 1fcffa42e00..d972c9d9b8e 100644 --- a/extras/hs-test/hst_suite.go +++ b/extras/hs-test/hst_suite.go @@ -2,7 +2,6 @@ package main import ( "flag" - "fmt" "io/ioutil" "os" "time" @@ -25,15 +24,15 @@ var nConfiguredCpus = flag.Int("cpus", 1, "number of CPUs assigned to vpp") type HstSuite struct { suite.Suite - containers map[string]*Container - volumes []string - netConfigs []NetConfig - netInterfaces map[string]*NetInterface - addresser *Addresser - testIds map[string]string - cpuAllocator *CpuAllocatorT - cpuContexts []*CpuContext - cpuPerVpp int + containers map[string]*Container + volumes []string + netConfigs []NetConfig + netInterfaces map[string]*NetInterface + ip4AddrAllocator *Ip4AddressAllocator + testIds map[string]string + cpuAllocator *CpuAllocatorT + cpuContexts []*CpuContext + cpuPerVpp int } func (s *HstSuite) SetupSuite() { @@ -229,7 +228,7 @@ func (s *HstSuite) loadNetworkTopology(topologyName string) { s.T().Fatalf("unmarshal error: %v", err) } - s.addresser = newAddresser(s) + s.ip4AddrAllocator = NewIp4AddressAllocator() s.netInterfaces = make(map[string]*NetInterface) for _, elem := range yamlTopo.Devices { switch elem["type"].(string) { @@ -243,7 +242,7 @@ func (s *HstSuite) loadNetworkTopology(topologyName string) { } case Veth, Tap: { - if netIf, err := newNetworkInterface(elem, s.addresser); err == nil { + if netIf, err := newNetworkInterface(elem, s.ip4AddrAllocator); err == nil { s.netConfigs = append(s.netConfigs, netIf) s.netInterfaces[netIf.Name()] = netIf } else { @@ -298,44 +297,3 @@ func (s *HstSuite) getTestId() string { return s.testIds[testName] } - -type AddressCounter = int - -type Addresser struct { - networks map[int]AddressCounter - suite *HstSuite -} - -func (a *Addresser) addNetwork(networkNumber int) { - a.networks[networkNumber] = 1 -} - -func (a *Addresser) newIp4Address(inputNetworkNumber ...int) (string, error) { - var networkNumber int = 0 - if len(inputNetworkNumber) > 0 { - networkNumber = inputNetworkNumber[0] - } - - if _, ok := a.networks[networkNumber]; !ok { - a.addNetwork(networkNumber) - } - - numberOfAddresses := a.networks[networkNumber] - - if numberOfAddresses == 254 { - return "", fmt.Errorf("no available IPv4 addresses") - } - - address := fmt.Sprintf("10.10.%v.%v/24", networkNumber, numberOfAddresses) - a.networks[networkNumber] = numberOfAddresses + 1 - - return address, nil -} - -func newAddresser(suite *HstSuite) *Addresser { - var addresser = new(Addresser) - addresser.suite = suite - addresser.networks = make(map[int]AddressCounter) - addresser.addNetwork(0) - return addresser -} diff --git a/extras/hs-test/netconfig.go b/extras/hs-test/netconfig.go index 6059b7b8567..897be6cfa65 100644 --- a/extras/hs-test/netconfig.go +++ b/extras/hs-test/netconfig.go @@ -32,8 +32,8 @@ type ( NetInterface struct { NetConfigBase - addresser *Addresser - ip4Address string // this will have form 10.10.10.1/24 + ip4AddrAllocator *Ip4AddressAllocator + ip4Address string index InterfaceIndex hwAddress MacAddress networkNamespace string @@ -72,10 +72,10 @@ var ( } ) -func newNetworkInterface(cfg NetDevConfig, a *Addresser) (*NetInterface, error) { +func newNetworkInterface(cfg NetDevConfig, a *Ip4AddressAllocator) (*NetInterface, error) { var newInterface *NetInterface = &NetInterface{} var err error - newInterface.addresser = a + newInterface.ip4AddrAllocator = a newInterface.name = cfg["name"].(string) newInterface.networkNumber = DEFAULT_NETWORK_NUM @@ -98,7 +98,7 @@ func newNetworkInterface(cfg NetDevConfig, a *Addresser) (*NetInterface, error) if n, ok := ip.(NetDevConfig)["network"]; ok { newInterface.networkNumber = n.(int) } - newInterface.ip4Address, err = newInterface.addresser.newIp4Address( + newInterface.ip4Address, err = newInterface.ip4AddrAllocator.NewIp4InterfaceAddress( newInterface.networkNumber, ) if err != nil { diff --git a/extras/hs-test/vppinstance.go b/extras/hs-test/vppinstance.go index a9b97bcaa0b..c871697cb44 100644 --- a/extras/hs-test/vppinstance.go +++ b/extras/hs-test/vppinstance.go @@ -236,7 +236,7 @@ func (vpp *VppInstance) createAfPacket( if veth.addressWithPrefix() == (AddressWithPrefix{}) { var err error var ip4Address string - if ip4Address, err = veth.addresser.newIp4Address(veth.peer.networkNumber); err == nil { + if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil { veth.ip4Address = ip4Address } else { return 0, err -- cgit 1.2.3-korg