summaryrefslogtreecommitdiffstats
path: root/extras/hs-test/netconfig.go
diff options
context:
space:
mode:
authorMaros Ondrejicka <mondreji@cisco.com>2023-02-21 10:53:20 +0100
committerFlorin Coras <florin.coras@gmail.com>2023-02-21 18:40:49 +0000
commit300f70d3a3b09ed5d9371cb2936e72e1c01d042a (patch)
tree45a807706b009f800e4a8c4b6edd65fec8d99792 /extras/hs-test/netconfig.go
parent7c06b5790d879c216ad27f8e281789b35858db43 (diff)
hs-test: clean-up ip address generation
Type: test Signed-off-by: Maros Ondrejicka <mondreji@cisco.com> Change-Id: I74c505920d1363d0ff2b3213fd831c181b70a173
Diffstat (limited to 'extras/hs-test/netconfig.go')
-rw-r--r--extras/hs-test/netconfig.go117
1 files changed, 82 insertions, 35 deletions
diff --git a/extras/hs-test/netconfig.go b/extras/hs-test/netconfig.go
index 5b199fd12a7..db6cbe183f8 100644
--- a/extras/hs-test/netconfig.go
+++ b/extras/hs-test/netconfig.go
@@ -42,16 +42,19 @@ type (
NetInterfaceBase struct {
NetConfigBase
- addresser *Addresser
- ip4address string // this will have form 10.10.10.1/24
- index InterfaceIndex
- hwAddress MacAddress
+ addresser *Addresser
+ ip4Address string // this will have form 10.10.10.1/24
+ index InterfaceIndex
+ hwAddress MacAddress
+ networkNamespace string
+ networkNumber int
}
NetworkInterfaceVeth struct {
NetInterfaceBase
peerNetworkNamespace string
peerName string
+ peerNetworkNumber int
peerIp4Address string
}
@@ -86,7 +89,7 @@ func (b *NetConfigBase) Type() string {
}
func (b *NetInterfaceBase) SetAddress(address string) {
- b.ip4address = address
+ b.ip4Address = address
}
func (b *NetInterfaceBase) SetIndex(index InterfaceIndex) {
@@ -98,52 +101,31 @@ func (b *NetInterfaceBase) Index() InterfaceIndex {
}
func (b *NetInterfaceBase) AddressWithPrefix() AddressWithPrefix {
- address, _ := ip_types.ParseAddressWithPrefix(b.ip4address)
+ address, _ := ip_types.ParseAddressWithPrefix(b.ip4Address)
return address
}
func (b *NetInterfaceBase) IP4AddressWithPrefix() IP4AddressWithPrefix {
- IP4Prefix, _ := ip_types.ParseIP4Prefix(b.ip4address)
+ IP4Prefix, _ := ip_types.ParseIP4Prefix(b.ip4Address)
IP4AddressWithPrefix := ip_types.IP4AddressWithPrefix(IP4Prefix)
return IP4AddressWithPrefix
}
func (b *NetInterfaceBase) IP4AddressString() string {
- return strings.Split(b.ip4address, "/")[0]
+ return strings.Split(b.ip4Address, "/")[0]
}
func (b *NetInterfaceBase) HwAddress() MacAddress {
return b.hwAddress
}
-func (iface *NetworkInterfaceVeth) Configure() error {
- err := AddVethPair(iface.name, iface.peerName)
- if err != nil {
- return err
- }
-
- if iface.peerNetworkNamespace != "" {
- err := LinkSetNetns(iface.peerName, iface.peerNetworkNamespace)
- if err != nil {
- return err
- }
- }
-
- if iface.peerIp4Address != "" {
- err = AddAddress(iface.peerName, iface.peerIp4Address, iface.peerNetworkNamespace)
- if err != nil {
- return fmt.Errorf("failed to add configure address for %s: %v", iface.peerName, err)
- }
- }
- return nil
-}
-
func NewVeth(cfg NetDevConfig, a *Addresser) (NetworkInterfaceVeth, error) {
var veth NetworkInterfaceVeth
var err error
veth.addresser = a
veth.name = cfg["name"].(string)
veth.category = "veth"
+ veth.peerNetworkNumber = defaultNetworkNumber
if cfg["preset-hw-address"] != nil {
veth.hwAddress, err = ethernet_types.ParseMacAddress(cfg["preset-hw-address"].(string))
@@ -152,6 +134,20 @@ func NewVeth(cfg NetDevConfig, a *Addresser) (NetworkInterfaceVeth, error) {
}
}
+ if netns, ok := cfg["netns"]; ok {
+ veth.networkNamespace = netns.(string)
+ }
+
+ if ip, ok := cfg["ip4"]; ok {
+ if n, ok := ip.(NetDevConfig)["network"]; ok {
+ veth.networkNumber = n.(int)
+ }
+ veth.ip4Address, err = veth.addresser.NewIp4Address(veth.networkNumber)
+ if err != nil {
+ return NetworkInterfaceVeth{}, err
+ }
+ }
+
peer := cfg["peer"].(NetDevConfig)
veth.peerName = peer["name"].(string)
@@ -160,9 +156,11 @@ func NewVeth(cfg NetDevConfig, a *Addresser) (NetworkInterfaceVeth, error) {
veth.peerNetworkNamespace = peer["netns"].(string)
}
- if peer["ip4"] != nil && peer["ip4"].(bool) == true {
- veth.peerIp4Address, err = veth.addresser.
- NewIp4AddressWithNamespace(veth.peerNetworkNamespace)
+ if peerIp, ok := peer["ip4"]; ok {
+ if n, ok := peerIp.(NetDevConfig)["network"]; ok {
+ veth.peerNetworkNumber = n.(int)
+ }
+ veth.peerIp4Address, err = veth.addresser.NewIp4Address(veth.peerNetworkNumber)
if err != nil {
return NetworkInterfaceVeth{}, err
}
@@ -171,6 +169,47 @@ func NewVeth(cfg NetDevConfig, a *Addresser) (NetworkInterfaceVeth, error) {
return veth, nil
}
+func (iface *NetworkInterfaceVeth) Configure() error {
+ err := AddVethPair(iface.name, iface.peerName)
+ if err != nil {
+ return err
+ }
+
+ if iface.networkNamespace != "" {
+ err := LinkSetNetns(iface.name, iface.networkNamespace)
+ if err != nil {
+ return err
+ }
+ }
+
+ if iface.peerNetworkNamespace != "" {
+ err := LinkSetNetns(iface.peerName, iface.peerNetworkNamespace)
+ if err != nil {
+ return err
+ }
+ }
+
+ if iface.ip4Address != "" {
+ err = AddAddress(
+ iface.Name(),
+ iface.ip4Address,
+ iface.networkNamespace,
+ )
+ }
+
+ if iface.peerIp4Address != "" {
+ err = AddAddress(
+ iface.peerName,
+ iface.peerIp4Address,
+ iface.peerNetworkNamespace,
+ )
+ if err != nil {
+ return fmt.Errorf("failed to add configure address for %s: %v", iface.peerName, err)
+ }
+ }
+ return nil
+}
+
func (iface *NetworkInterfaceVeth) Unconfigure() {
DelLink(iface.name)
}
@@ -226,7 +265,11 @@ func NewBridge(cfg NetDevConfig) (NetworkBridge, error) {
for _, v := range cfg["interfaces"].([]interface{}) {
bridge.interfaces = append(bridge.interfaces, v.(string))
}
- bridge.networkNamespace = cfg["netns"].(string)
+
+ bridge.networkNamespace = ""
+ if netns, ok := cfg["netns"]; ok {
+ bridge.networkNamespace = netns.(string)
+ }
return bridge, nil
}
@@ -310,12 +353,16 @@ func AddVethPair(ifName, peerName string) error {
cmd := exec.Command("ip", "link", "add", ifName, "type", "veth", "peer", "name", peerName)
err := cmd.Run()
if err != nil {
- return fmt.Errorf("creating veth pair failed: %v", err)
+ return fmt.Errorf("creating veth pair '%v/%v' failed: %v", ifName, peerName, err)
}
err = SetDevUp(ifName, "")
if err != nil {
return fmt.Errorf("set link up failed: %v", err)
}
+ err = SetDevUp(peerName, "")
+ if err != nil {
+ return fmt.Errorf("set link up failed: %v", err)
+ }
return nil
}