From 3e589713cf145c84b3e0b6849fc76c59b7678203 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 8 Oct 2020 13:16:27 +1100 Subject: [PATCH] network: Use PciPath type through network handling The "PCI address" returned by Endpoint::PciPath() isn't actually a PCI address (DDDD:BB:DD.F), but rather a PCI path. Rename and use the PciPath type to clean this up and the various parts of the network code connected to it. fixes #3002 Signed-off-by: David Gibson --- virtcontainers/bridgedmacvlan_endpoint.go | 15 ++++++++------- virtcontainers/endpoint.go | 5 +++-- virtcontainers/ipvlan_endpoint.go | 15 ++++++++------- virtcontainers/kata_agent.go | 8 ++++++-- virtcontainers/macvtap_endpoint.go | 19 ++++++++++--------- virtcontainers/network.go | 2 +- virtcontainers/persist/api/network.go | 5 +++-- virtcontainers/physical_endpoint.go | 15 ++++++++------- virtcontainers/pkg/types/types.go | 8 +++----- virtcontainers/qemu.go | 13 +++++++++++-- virtcontainers/sandbox.go | 2 +- virtcontainers/tap_endpoint.go | 15 ++++++++------- virtcontainers/tuntap_endpoint.go | 15 ++++++++------- virtcontainers/veth_endpoint.go | 15 ++++++++------- virtcontainers/vhostuser_endpoint.go | 19 ++++++++++--------- 15 files changed, 96 insertions(+), 75 deletions(-) diff --git a/virtcontainers/bridgedmacvlan_endpoint.go b/virtcontainers/bridgedmacvlan_endpoint.go index 3c54c006f1..2b395c2cb4 100644 --- a/virtcontainers/bridgedmacvlan_endpoint.go +++ b/virtcontainers/bridgedmacvlan_endpoint.go @@ -10,6 +10,7 @@ import ( "github.com/containernetworking/plugins/pkg/ns" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) // BridgedMacvlanEndpoint represents a macvlan endpoint that is bridged to the VM @@ -17,7 +18,7 @@ type BridgedMacvlanEndpoint struct { NetPair NetworkInterfacePair EndpointProperties NetworkInfo EndpointType EndpointType - PCIAddr string + PCIPath vcTypes.PciPath } func createBridgedMacvlanNetworkEndpoint(idx int, ifName string, interworkingModel NetInterworkingModel) (*BridgedMacvlanEndpoint, error) { @@ -67,14 +68,14 @@ func (endpoint *BridgedMacvlanEndpoint) SetProperties(properties NetworkInfo) { endpoint.EndpointProperties = properties } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *BridgedMacvlanEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *BridgedMacvlanEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *BridgedMacvlanEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *BridgedMacvlanEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. diff --git a/virtcontainers/endpoint.go b/virtcontainers/endpoint.go index 22ed0885f9..46fb81abe0 100644 --- a/virtcontainers/endpoint.go +++ b/virtcontainers/endpoint.go @@ -9,6 +9,7 @@ import ( "fmt" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) // Endpoint represents a physical or virtual network interface. @@ -17,11 +18,11 @@ type Endpoint interface { Name() string HardwareAddr() string Type() EndpointType - PciAddr() string + PciPath() vcTypes.PciPath NetworkPair() *NetworkInterfacePair SetProperties(NetworkInfo) - SetPciAddr(string) + SetPciPath(vcTypes.PciPath) Attach(*Sandbox) error Detach(netNsCreated bool, netNsPath string) error HotAttach(h hypervisor) error diff --git a/virtcontainers/ipvlan_endpoint.go b/virtcontainers/ipvlan_endpoint.go index 8b0bdd8313..25977302e4 100644 --- a/virtcontainers/ipvlan_endpoint.go +++ b/virtcontainers/ipvlan_endpoint.go @@ -10,6 +10,7 @@ import ( "github.com/containernetworking/plugins/pkg/ns" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) // IPVlanEndpoint represents a ipvlan endpoint that is bridged to the VM @@ -17,7 +18,7 @@ type IPVlanEndpoint struct { NetPair NetworkInterfacePair EndpointProperties NetworkInfo EndpointType EndpointType - PCIAddr string + PCIPath vcTypes.PciPath } func createIPVlanNetworkEndpoint(idx int, ifName string) (*IPVlanEndpoint, error) { @@ -70,14 +71,14 @@ func (endpoint *IPVlanEndpoint) SetProperties(properties NetworkInfo) { endpoint.EndpointProperties = properties } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *IPVlanEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *IPVlanEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *IPVlanEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *IPVlanEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. diff --git a/virtcontainers/kata_agent.go b/virtcontainers/kata_agent.go index b67cf688cf..00a9669d7e 100644 --- a/virtcontainers/kata_agent.go +++ b/virtcontainers/kata_agent.go @@ -2300,7 +2300,7 @@ func (k *kataAgent) convertToKataAgentInterface(iface *vcTypes.Interface) *aType Mtu: iface.Mtu, RawFlags: iface.RawFlags, HwAddr: iface.HwAddr, - PciPath: iface.PciAddr, + PciPath: iface.PciPath.String(), } } @@ -2311,13 +2311,17 @@ func (k *kataAgent) convertToInterfaces(aIfaces []*aTypes.Interface) ([]*vcTypes continue } + pcipath, err := vcTypes.PciPathFromString(aIface.PciPath) + if err != nil { + return nil, err + } iface := &vcTypes.Interface{ Device: aIface.Device, Name: aIface.Name, IPAddresses: k.convertToIPAddresses(aIface.IPAddresses), Mtu: aIface.Mtu, HwAddr: aIface.HwAddr, - PciAddr: aIface.PciPath, + PciPath: pcipath, } ifaces = append(ifaces, iface) diff --git a/virtcontainers/macvtap_endpoint.go b/virtcontainers/macvtap_endpoint.go index 2dbb3f369a..b21c81e98a 100644 --- a/virtcontainers/macvtap_endpoint.go +++ b/virtcontainers/macvtap_endpoint.go @@ -10,6 +10,7 @@ import ( "os" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) // MacvtapEndpoint represents a macvtap endpoint @@ -18,7 +19,7 @@ type MacvtapEndpoint struct { EndpointType EndpointType VMFds []*os.File VhostFds []*os.File - PCIAddr string + PCIPath vcTypes.PciPath } func createMacvtapNetworkEndpoint(netInfo NetworkInfo) (*MacvtapEndpoint, error) { @@ -91,14 +92,14 @@ func (endpoint *MacvtapEndpoint) HotDetach(h hypervisor, netNsCreated bool, netN return fmt.Errorf("MacvtapEndpoint does not support Hot detach") } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *MacvtapEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *MacvtapEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *MacvtapEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *MacvtapEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. @@ -111,7 +112,7 @@ func (endpoint *MacvtapEndpoint) save() persistapi.NetworkEndpoint { Type: string(endpoint.Type()), Macvtap: &persistapi.MacvtapEndpoint{ - PCIAddr: endpoint.PCIAddr, + PCIPath: endpoint.PCIPath, }, } } @@ -119,6 +120,6 @@ func (endpoint *MacvtapEndpoint) load(s persistapi.NetworkEndpoint) { endpoint.EndpointType = MacvtapEndpointType if s.Macvtap != nil { - endpoint.PCIAddr = s.Macvtap.PCIAddr + endpoint.PCIPath = s.Macvtap.PCIPath } } diff --git a/virtcontainers/network.go b/virtcontainers/network.go index eda0f1ea67..8fd6ac35b2 100644 --- a/virtcontainers/network.go +++ b/virtcontainers/network.go @@ -981,7 +981,7 @@ func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*vcTypes.Interfa Mtu: uint64(endpoint.Properties().Iface.MTU), RawFlags: noarp, HwAddr: endpoint.HardwareAddr(), - PciAddr: endpoint.PciAddr(), + PciPath: endpoint.PciPath(), } ifaces = append(ifaces, &ifc) diff --git a/virtcontainers/persist/api/network.go b/virtcontainers/persist/api/network.go index 69610c6706..66196174b4 100644 --- a/virtcontainers/persist/api/network.go +++ b/virtcontainers/persist/api/network.go @@ -7,6 +7,7 @@ package persistapi import ( + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/vishvananda/netlink" ) @@ -48,7 +49,7 @@ type PhysicalEndpoint struct { type MacvtapEndpoint struct { // This is for showing information. // Remove this field won't impact anything. - PCIAddr string + PCIPath vcTypes.PciPath } type TapEndpoint struct { @@ -75,7 +76,7 @@ type VhostUserEndpoint struct { // This is for showing information. // Remove these fields won't impact anything. IfaceName string - PCIAddr string + PCIPath vcTypes.PciPath } // NetworkEndpoint contains network interface information diff --git a/virtcontainers/physical_endpoint.go b/virtcontainers/physical_endpoint.go index 5d1b9df496..570d196ced 100644 --- a/virtcontainers/physical_endpoint.go +++ b/virtcontainers/physical_endpoint.go @@ -16,6 +16,7 @@ import ( "github.com/kata-containers/runtime/virtcontainers/device/drivers" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" "github.com/kata-containers/runtime/virtcontainers/pkg/cgroups" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/safchain/ethtool" ) @@ -28,7 +29,7 @@ type PhysicalEndpoint struct { BDF string Driver string VendorDeviceID string - PCIAddr string + PCIPath vcTypes.PciPath } // Properties returns the properties of the physical interface. @@ -51,14 +52,14 @@ func (endpoint *PhysicalEndpoint) Type() EndpointType { return endpoint.EndpointType } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *PhysicalEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *PhysicalEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *PhysicalEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *PhysicalEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // SetProperties sets the properties of the physical endpoint. diff --git a/virtcontainers/pkg/types/types.go b/virtcontainers/pkg/types/types.go index 5abb4922ca..c494495eae 100644 --- a/virtcontainers/pkg/types/types.go +++ b/virtcontainers/pkg/types/types.go @@ -1,4 +1,4 @@ -// Copyright 2018 Intel Corporation. +// Copyright (c) 2018 Intel Corporation. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,10 +20,8 @@ type Interface struct { Mtu uint64 RawFlags uint32 HwAddr string - // pciAddr is the PCI address in the format "bridgeAddr/deviceAddr". - // Here, bridgeAddr is the address at which the bridge is attached on the root bus, - // while deviceAddr is the address at which the network device is attached on the bridge. - PciAddr string + // PCI path for the interface + PciPath PciPath // LinkType defines the type of interface described by this structure. // The expected values are the one that are defined by the netlink // library, regarding each type of link. Here is a non exhaustive diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index c4dd91e906..86c8a0d319 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -33,6 +33,7 @@ import ( "github.com/kata-containers/runtime/virtcontainers/device/config" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/runtime/virtcontainers/types" "github.com/kata-containers/runtime/virtcontainers/utils" @@ -1420,8 +1421,16 @@ func (q *qemu) hotplugNetDevice(endpoint Endpoint, op operation) (err error) { } }() - pciAddr := fmt.Sprintf("%02x/%s", bridge.Addr, addr) - endpoint.SetPciAddr(pciAddr) + bridgeSlot, err := vcTypes.PciSlotFromInt(bridge.Addr) + if err != nil { + return err + } + devSlot, err := vcTypes.PciSlotFromString(addr) + if err != nil { + return err + } + pciPath, err := vcTypes.PciPathFromSlots(bridgeSlot, devSlot) + endpoint.SetPciPath(pciPath) var machine govmmQemu.Machine machine, err = q.getQemuMachine() diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index b44bc7c41b..302a28396b 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -988,7 +988,7 @@ func (s *Sandbox) AddInterface(inf *vcTypes.Interface) (*vcTypes.Interface, erro } // Add network for vm - inf.PciAddr = endpoint.PciAddr() + inf.PciPath = endpoint.PciPath() return s.agent.updateInterface(inf) } diff --git a/virtcontainers/tap_endpoint.go b/virtcontainers/tap_endpoint.go index 69f6d01b43..3d2d90e606 100644 --- a/virtcontainers/tap_endpoint.go +++ b/virtcontainers/tap_endpoint.go @@ -12,6 +12,7 @@ import ( "github.com/vishvananda/netlink" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/pkg/uuid" ) @@ -20,7 +21,7 @@ type TapEndpoint struct { TapInterface TapInterface EndpointProperties NetworkInfo EndpointType EndpointType - PCIAddr string + PCIPath vcTypes.PciPath } // Properties returns the properties of the tap interface. @@ -43,14 +44,14 @@ func (endpoint *TapEndpoint) Type() EndpointType { return endpoint.EndpointType } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *TapEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *TapEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *TapEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *TapEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. diff --git a/virtcontainers/tuntap_endpoint.go b/virtcontainers/tuntap_endpoint.go index c75a2cbd9d..0faea72521 100644 --- a/virtcontainers/tuntap_endpoint.go +++ b/virtcontainers/tuntap_endpoint.go @@ -14,6 +14,7 @@ import ( "github.com/vishvananda/netlink" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) // TuntapEndpoint represents just a tap endpoint @@ -22,7 +23,7 @@ type TuntapEndpoint struct { TuntapInterface TuntapInterface EndpointProperties NetworkInfo EndpointType EndpointType - PCIAddr string + PCIPath vcTypes.PciPath } // Properties returns the properties of the tap interface. @@ -45,14 +46,14 @@ func (endpoint *TuntapEndpoint) Type() EndpointType { return endpoint.EndpointType } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *TuntapEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *TuntapEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *TuntapEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *TuntapEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. diff --git a/virtcontainers/veth_endpoint.go b/virtcontainers/veth_endpoint.go index 861c860418..7d6b5de6ac 100644 --- a/virtcontainers/veth_endpoint.go +++ b/virtcontainers/veth_endpoint.go @@ -10,6 +10,7 @@ import ( "github.com/containernetworking/plugins/pkg/ns" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" ) // VethEndpoint gathers a network pair and its properties. @@ -17,7 +18,7 @@ type VethEndpoint struct { NetPair NetworkInterfacePair EndpointProperties NetworkInfo EndpointType EndpointType - PCIAddr string + PCIPath vcTypes.PciPath } func createVethNetworkEndpoint(idx int, ifName string, interworkingModel NetInterworkingModel) (*VethEndpoint, error) { @@ -65,14 +66,14 @@ func (endpoint *VethEndpoint) Type() EndpointType { return endpoint.EndpointType } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *VethEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *VethEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *VethEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *VethEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. diff --git a/virtcontainers/vhostuser_endpoint.go b/virtcontainers/vhostuser_endpoint.go index 6676d8450a..fc6259820e 100644 --- a/virtcontainers/vhostuser_endpoint.go +++ b/virtcontainers/vhostuser_endpoint.go @@ -12,6 +12,7 @@ import ( "github.com/kata-containers/runtime/virtcontainers/device/config" persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" + vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/kata-containers/runtime/virtcontainers/utils" ) @@ -30,7 +31,7 @@ type VhostUserEndpoint struct { IfaceName string EndpointProperties NetworkInfo EndpointType EndpointType - PCIAddr string + PCIPath vcTypes.PciPath } // Properties returns the properties of the interface. @@ -58,14 +59,14 @@ func (endpoint *VhostUserEndpoint) SetProperties(properties NetworkInfo) { endpoint.EndpointProperties = properties } -// PciAddr returns the PCI address of the endpoint. -func (endpoint *VhostUserEndpoint) PciAddr() string { - return endpoint.PCIAddr +// PciPath returns the PCI path of the endpoint. +func (endpoint *VhostUserEndpoint) PciPath() vcTypes.PciPath { + return endpoint.PCIPath } -// SetPciAddr sets the PCI address of the endpoint. -func (endpoint *VhostUserEndpoint) SetPciAddr(pciAddr string) { - endpoint.PCIAddr = pciAddr +// SetPciPath sets the PCI path of the endpoint. +func (endpoint *VhostUserEndpoint) SetPciPath(pciPath vcTypes.PciPath) { + endpoint.PCIPath = pciPath } // NetworkPair returns the network pair of the endpoint. @@ -156,7 +157,7 @@ func (endpoint *VhostUserEndpoint) save() persistapi.NetworkEndpoint { Type: string(endpoint.Type()), VhostUser: &persistapi.VhostUserEndpoint{ IfaceName: endpoint.IfaceName, - PCIAddr: endpoint.PCIAddr, + PCIPath: endpoint.PCIPath, }, } } @@ -166,6 +167,6 @@ func (endpoint *VhostUserEndpoint) load(s persistapi.NetworkEndpoint) { if s.VhostUser != nil { endpoint.IfaceName = s.VhostUser.IfaceName - endpoint.PCIAddr = s.VhostUser.PCIAddr + endpoint.PCIPath = s.VhostUser.PCIPath } }