runtime/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.

Forward port of
3e589713cf

fixes #1040

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2020-12-15 14:27:57 +11:00
parent 87c5823c4b
commit 32b40f5fe4
13 changed files with 87 additions and 68 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/containernetworking/plugins/pkg/ns" "github.com/containernetworking/plugins/pkg/ns"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// BridgedMacvlanEndpoint represents a macvlan endpoint that is bridged to the VM // BridgedMacvlanEndpoint represents a macvlan endpoint that is bridged to the VM
@ -17,7 +18,7 @@ type BridgedMacvlanEndpoint struct {
NetPair NetworkInterfacePair NetPair NetworkInterfacePair
EndpointProperties NetworkInfo EndpointProperties NetworkInfo
EndpointType EndpointType EndpointType EndpointType
PCIAddr string PCIPath vcTypes.PciPath
RxRateLimiter bool RxRateLimiter bool
TxRateLimiter bool TxRateLimiter bool
} }
@ -69,14 +70,14 @@ func (endpoint *BridgedMacvlanEndpoint) SetProperties(properties NetworkInfo) {
endpoint.EndpointProperties = properties endpoint.EndpointProperties = properties
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *BridgedMacvlanEndpoint) PciAddr() string { func (endpoint *BridgedMacvlanEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *BridgedMacvlanEndpoint) SetPciAddr(pciAddr string) { func (endpoint *BridgedMacvlanEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// Endpoint represents a physical or virtual network interface. // Endpoint represents a physical or virtual network interface.
@ -17,11 +18,11 @@ type Endpoint interface {
Name() string Name() string
HardwareAddr() string HardwareAddr() string
Type() EndpointType Type() EndpointType
PciAddr() string PciPath() vcTypes.PciPath
NetworkPair() *NetworkInterfacePair NetworkPair() *NetworkInterfacePair
SetProperties(NetworkInfo) SetProperties(NetworkInfo)
SetPciAddr(string) SetPciPath(vcTypes.PciPath)
Attach(*Sandbox) error Attach(*Sandbox) error
Detach(netNsCreated bool, netNsPath string) error Detach(netNsCreated bool, netNsPath string) error
HotAttach(h hypervisor) error HotAttach(h hypervisor) error

View File

@ -10,6 +10,7 @@ import (
"github.com/containernetworking/plugins/pkg/ns" "github.com/containernetworking/plugins/pkg/ns"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// IPVlanEndpoint represents a ipvlan endpoint that is bridged to the VM // IPVlanEndpoint represents a ipvlan endpoint that is bridged to the VM
@ -17,7 +18,7 @@ type IPVlanEndpoint struct {
NetPair NetworkInterfacePair NetPair NetworkInterfacePair
EndpointProperties NetworkInfo EndpointProperties NetworkInfo
EndpointType EndpointType EndpointType EndpointType
PCIAddr string PCIPath vcTypes.PciPath
RxRateLimiter bool RxRateLimiter bool
TxRateLimiter bool TxRateLimiter bool
} }
@ -72,14 +73,14 @@ func (endpoint *IPVlanEndpoint) SetProperties(properties NetworkInfo) {
endpoint.EndpointProperties = properties endpoint.EndpointProperties = properties
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *IPVlanEndpoint) PciAddr() string { func (endpoint *IPVlanEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *IPVlanEndpoint) SetPciAddr(pciAddr string) { func (endpoint *IPVlanEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.

View File

@ -10,6 +10,7 @@ import (
"os" "os"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// MacvtapEndpoint represents a macvtap endpoint // MacvtapEndpoint represents a macvtap endpoint
@ -18,7 +19,7 @@ type MacvtapEndpoint struct {
EndpointType EndpointType EndpointType EndpointType
VMFds []*os.File VMFds []*os.File
VhostFds []*os.File VhostFds []*os.File
PCIAddr string PCIPath vcTypes.PciPath
RxRateLimiter bool RxRateLimiter bool
TxRateLimiter bool TxRateLimiter bool
} }
@ -93,14 +94,14 @@ func (endpoint *MacvtapEndpoint) HotDetach(h hypervisor, netNsCreated bool, netN
return fmt.Errorf("MacvtapEndpoint does not support Hot detach") return fmt.Errorf("MacvtapEndpoint does not support Hot detach")
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *MacvtapEndpoint) PciAddr() string { func (endpoint *MacvtapEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *MacvtapEndpoint) SetPciAddr(pciAddr string) { func (endpoint *MacvtapEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.
@ -113,7 +114,7 @@ func (endpoint *MacvtapEndpoint) save() persistapi.NetworkEndpoint {
Type: string(endpoint.Type()), Type: string(endpoint.Type()),
Macvtap: &persistapi.MacvtapEndpoint{ Macvtap: &persistapi.MacvtapEndpoint{
PCIAddr: endpoint.PCIAddr, PCIPath: endpoint.PCIPath,
}, },
} }
} }
@ -121,7 +122,7 @@ func (endpoint *MacvtapEndpoint) load(s persistapi.NetworkEndpoint) {
endpoint.EndpointType = MacvtapEndpointType endpoint.EndpointType = MacvtapEndpointType
if s.Macvtap != nil { if s.Macvtap != nil {
endpoint.PCIAddr = s.Macvtap.PCIAddr endpoint.PCIPath = s.Macvtap.PCIPath
} }
} }

View File

@ -984,7 +984,7 @@ func generateVCNetworkStructures(networkNS NetworkNamespace) ([]*pbTypes.Interfa
Mtu: uint64(endpoint.Properties().Iface.MTU), Mtu: uint64(endpoint.Properties().Iface.MTU),
RawFlags: noarp, RawFlags: noarp,
HwAddr: endpoint.HardwareAddr(), HwAddr: endpoint.HardwareAddr(),
PciPath: endpoint.PciAddr(), PciPath: endpoint.PciPath().String(),
} }
ifaces = append(ifaces, &ifc) ifaces = append(ifaces, &ifc)

View File

@ -7,6 +7,7 @@
package persistapi package persistapi
import ( import (
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
@ -48,7 +49,7 @@ type PhysicalEndpoint struct {
type MacvtapEndpoint struct { type MacvtapEndpoint struct {
// This is for showing information. // This is for showing information.
// Remove this field won't impact anything. // Remove this field won't impact anything.
PCIAddr string PCIPath vcTypes.PciPath
} }
type TapEndpoint struct { type TapEndpoint struct {
@ -75,7 +76,7 @@ type VhostUserEndpoint struct {
// This is for showing information. // This is for showing information.
// Remove these fields won't impact anything. // Remove these fields won't impact anything.
IfaceName string IfaceName string
PCIAddr string PCIPath vcTypes.PciPath
} }
// NetworkEndpoint contains network interface information // NetworkEndpoint contains network interface information

View File

@ -17,6 +17,7 @@ import (
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cgroups" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cgroups"
"github.com/safchain/ethtool" "github.com/safchain/ethtool"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// PhysicalEndpoint gathers a physical network interface and its properties // PhysicalEndpoint gathers a physical network interface and its properties
@ -28,7 +29,7 @@ type PhysicalEndpoint struct {
BDF string BDF string
Driver string Driver string
VendorDeviceID string VendorDeviceID string
PCIAddr string PCIPath vcTypes.PciPath
} }
// Properties returns the properties of the physical interface. // Properties returns the properties of the physical interface.
@ -51,14 +52,14 @@ func (endpoint *PhysicalEndpoint) Type() EndpointType {
return endpoint.EndpointType return endpoint.EndpointType
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *PhysicalEndpoint) PciAddr() string { func (endpoint *PhysicalEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *PhysicalEndpoint) SetPciAddr(pciAddr string) { func (endpoint *PhysicalEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// SetProperties sets the properties of the physical endpoint. // SetProperties sets the properties of the physical endpoint.

View File

@ -39,6 +39,7 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/uuid"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// romFile is the file name of the ROM that can be used for virtio-pci devices. // romFile is the file name of the ROM that can be used for virtio-pci devices.
@ -1546,8 +1547,16 @@ func (q *qemu) hotplugNetDevice(endpoint Endpoint, op operation) (err error) {
} }
}() }()
pciAddr := fmt.Sprintf("%02x/%s", bridge.Addr, addr) bridgeSlot, err := vcTypes.PciSlotFromInt(bridge.Addr)
endpoint.SetPciAddr(pciAddr) 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 var machine govmmQemu.Machine
machine, err = q.getQemuMachine() machine, err = q.getQemuMachine()

View File

@ -850,7 +850,7 @@ func (s *Sandbox) AddInterface(inf *pbTypes.Interface) (*pbTypes.Interface, erro
} }
// Add network for vm // Add network for vm
inf.PciPath = endpoint.PciAddr() inf.PciPath = endpoint.PciPath().String()
return s.agent.updateInterface(inf) return s.agent.updateInterface(inf)
} }

View File

@ -13,6 +13,7 @@ import (
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/uuid"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// TapEndpoint represents just a tap endpoint // TapEndpoint represents just a tap endpoint
@ -20,7 +21,7 @@ type TapEndpoint struct {
TapInterface TapInterface TapInterface TapInterface
EndpointProperties NetworkInfo EndpointProperties NetworkInfo
EndpointType EndpointType EndpointType EndpointType
PCIAddr string PCIPath vcTypes.PciPath
RxRateLimiter bool RxRateLimiter bool
TxRateLimiter bool TxRateLimiter bool
} }
@ -45,14 +46,14 @@ func (endpoint *TapEndpoint) Type() EndpointType {
return endpoint.EndpointType return endpoint.EndpointType
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *TapEndpoint) PciAddr() string { func (endpoint *TapEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *TapEndpoint) SetPciAddr(pciAddr string) { func (endpoint *TapEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.

View File

@ -14,6 +14,7 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// TuntapEndpoint represents just a tap endpoint // TuntapEndpoint represents just a tap endpoint
@ -22,7 +23,7 @@ type TuntapEndpoint struct {
TuntapInterface TuntapInterface TuntapInterface TuntapInterface
EndpointProperties NetworkInfo EndpointProperties NetworkInfo
EndpointType EndpointType EndpointType EndpointType
PCIAddr string PCIPath vcTypes.PciPath
RxRateLimiter bool RxRateLimiter bool
TxRateLimiter bool TxRateLimiter bool
} }
@ -47,14 +48,14 @@ func (endpoint *TuntapEndpoint) Type() EndpointType {
return endpoint.EndpointType return endpoint.EndpointType
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *TuntapEndpoint) PciAddr() string { func (endpoint *TuntapEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *TuntapEndpoint) SetPciAddr(pciAddr string) { func (endpoint *TuntapEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.

View File

@ -10,6 +10,7 @@ import (
"github.com/containernetworking/plugins/pkg/ns" "github.com/containernetworking/plugins/pkg/ns"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// VethEndpoint gathers a network pair and its properties. // VethEndpoint gathers a network pair and its properties.
@ -17,7 +18,7 @@ type VethEndpoint struct {
NetPair NetworkInterfacePair NetPair NetworkInterfacePair
EndpointProperties NetworkInfo EndpointProperties NetworkInfo
EndpointType EndpointType EndpointType EndpointType
PCIAddr string PCIPath vcTypes.PciPath
RxRateLimiter bool RxRateLimiter bool
TxRateLimiter bool TxRateLimiter bool
} }
@ -67,14 +68,14 @@ func (endpoint *VethEndpoint) Type() EndpointType {
return endpoint.EndpointType return endpoint.EndpointType
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *VethEndpoint) PciAddr() string { func (endpoint *VethEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *VethEndpoint) SetPciAddr(pciAddr string) { func (endpoint *VethEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.

View File

@ -13,6 +13,7 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
) )
// Long term, this should be made more configurable. For now matching path // Long term, this should be made more configurable. For now matching path
@ -30,7 +31,7 @@ type VhostUserEndpoint struct {
IfaceName string IfaceName string
EndpointProperties NetworkInfo EndpointProperties NetworkInfo
EndpointType EndpointType EndpointType EndpointType
PCIAddr string PCIPath vcTypes.PciPath
} }
// Properties returns the properties of the interface. // Properties returns the properties of the interface.
@ -58,14 +59,14 @@ func (endpoint *VhostUserEndpoint) SetProperties(properties NetworkInfo) {
endpoint.EndpointProperties = properties endpoint.EndpointProperties = properties
} }
// PciAddr returns the PCI address of the endpoint. // PciPath returns the PCI path of the endpoint.
func (endpoint *VhostUserEndpoint) PciAddr() string { func (endpoint *VhostUserEndpoint) PciPath() vcTypes.PciPath {
return endpoint.PCIAddr return endpoint.PCIPath
} }
// SetPciAddr sets the PCI address of the endpoint. // SetPciPath sets the PCI path of the endpoint.
func (endpoint *VhostUserEndpoint) SetPciAddr(pciAddr string) { func (endpoint *VhostUserEndpoint) SetPciPath(pciPath vcTypes.PciPath) {
endpoint.PCIAddr = pciAddr endpoint.PCIPath = pciPath
} }
// NetworkPair returns the network pair of the endpoint. // NetworkPair returns the network pair of the endpoint.
@ -156,7 +157,7 @@ func (endpoint *VhostUserEndpoint) save() persistapi.NetworkEndpoint {
Type: string(endpoint.Type()), Type: string(endpoint.Type()),
VhostUser: &persistapi.VhostUserEndpoint{ VhostUser: &persistapi.VhostUserEndpoint{
IfaceName: endpoint.IfaceName, IfaceName: endpoint.IfaceName,
PCIAddr: endpoint.PCIAddr, PCIPath: endpoint.PCIPath,
}, },
} }
} }
@ -166,7 +167,7 @@ func (endpoint *VhostUserEndpoint) load(s persistapi.NetworkEndpoint) {
if s.VhostUser != nil { if s.VhostUser != nil {
endpoint.IfaceName = s.VhostUser.IfaceName endpoint.IfaceName = s.VhostUser.IfaceName
endpoint.PCIAddr = s.VhostUser.PCIAddr endpoint.PCIPath = s.VhostUser.PCIPath
} }
} }