virtcontainers/network: Change signature of Enpoint Attach method

In order to use the device manager and receiver from the network enpoints,
the signature of the Attach method must change to revice a Sandbox instead of
a Hypervisor, this way devices can be added through the device manager API.

Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2020-06-29 10:11:38 -05:00
parent 9a9721c261
commit a134c2e083
12 changed files with 41 additions and 31 deletions

View File

@ -86,7 +86,8 @@ func (endpoint *BridgedMacvlanEndpoint) NetworkPair() *NetworkInterfacePair {
// Attach for virtual endpoint bridges the network pair and adds the // Attach for virtual endpoint bridges the network pair and adds the
// tap interface of the network pair to the hypervisor. // tap interface of the network pair to the hypervisor.
func (endpoint *BridgedMacvlanEndpoint) Attach(h hypervisor) error { func (endpoint *BridgedMacvlanEndpoint) Attach(s *Sandbox) error {
h := s.hypervisor
if err := xConnectVMNetwork(endpoint, h); err != nil { if err := xConnectVMNetwork(endpoint, h); err != nil {
networkLogger().WithError(err).Error("Error bridging virtual ep") networkLogger().WithError(err).Error("Error bridging virtual ep")
return err return err

View File

@ -22,7 +22,7 @@ type Endpoint interface {
SetProperties(NetworkInfo) SetProperties(NetworkInfo)
SetPciAddr(string) SetPciAddr(string)
Attach(hypervisor) error Attach(*Sandbox) error
Detach(netNsCreated bool, netNsPath string) error Detach(netNsCreated bool, netNsPath string) error
HotAttach(h hypervisor) error HotAttach(h hypervisor) error
HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error

View File

@ -89,7 +89,8 @@ func (endpoint *IPVlanEndpoint) NetworkPair() *NetworkInterfacePair {
// Attach for virtual endpoint bridges the network pair and adds the // Attach for virtual endpoint bridges the network pair and adds the
// tap interface of the network pair to the hypervisor. // tap interface of the network pair to the hypervisor.
func (endpoint *IPVlanEndpoint) Attach(h hypervisor) error { func (endpoint *IPVlanEndpoint) Attach(s *Sandbox) error {
h := s.hypervisor
if err := xConnectVMNetwork(endpoint, h); err != nil { if err := xConnectVMNetwork(endpoint, h); err != nil {
networkLogger().WithError(err).Error("Error bridging virtual ep") networkLogger().WithError(err).Error("Error bridging virtual ep")
return err return err

View File

@ -58,8 +58,9 @@ func (endpoint *MacvtapEndpoint) SetProperties(properties NetworkInfo) {
} }
// Attach for macvtap endpoint passes macvtap device to the hypervisor. // Attach for macvtap endpoint passes macvtap device to the hypervisor.
func (endpoint *MacvtapEndpoint) Attach(h hypervisor) error { func (endpoint *MacvtapEndpoint) Attach(s *Sandbox) error {
var err error var err error
h := s.hypervisor
endpoint.VMFds, err = createMacvtapFds(endpoint.EndpointProperties.Iface.Index, int(h.hypervisorConfig().NumVCPUs)) endpoint.VMFds, err = createMacvtapFds(endpoint.EndpointProperties.Iface.Index, int(h.hypervisorConfig().NumVCPUs))
if err != nil { if err != nil {

View File

@ -1271,7 +1271,7 @@ func (n *Network) Run(networkNSPath string, cb func() error) error {
} }
// Add adds all needed interfaces inside the network namespace. // Add adds all needed interfaces inside the network namespace.
func (n *Network) Add(ctx context.Context, config *NetworkConfig, hypervisor hypervisor, hotplug bool) ([]Endpoint, error) { func (n *Network) Add(ctx context.Context, config *NetworkConfig, s *Sandbox, hotplug bool) ([]Endpoint, error) {
span, _ := n.trace(ctx, "add") span, _ := n.trace(ctx, "add")
defer span.Finish() defer span.Finish()
@ -1284,24 +1284,24 @@ func (n *Network) Add(ctx context.Context, config *NetworkConfig, hypervisor hyp
for _, endpoint := range endpoints { for _, endpoint := range endpoints {
networkLogger().WithField("endpoint-type", endpoint.Type()).WithField("hotplug", hotplug).Info("Attaching endpoint") networkLogger().WithField("endpoint-type", endpoint.Type()).WithField("hotplug", hotplug).Info("Attaching endpoint")
if hotplug { if hotplug {
if err := endpoint.HotAttach(hypervisor); err != nil { if err := endpoint.HotAttach(s.hypervisor); err != nil {
return err return err
} }
} else { } else {
if err := endpoint.Attach(hypervisor); err != nil { if err := endpoint.Attach(s); err != nil {
return err return err
} }
} }
if !hypervisor.isRateLimiterBuiltin() { if !s.hypervisor.isRateLimiterBuiltin() {
rxRateLimiterMaxRate := hypervisor.hypervisorConfig().RxRateLimiterMaxRate rxRateLimiterMaxRate := s.hypervisor.hypervisorConfig().RxRateLimiterMaxRate
if rxRateLimiterMaxRate > 0 { if rxRateLimiterMaxRate > 0 {
networkLogger().Info("Add Rx Rate Limiter") networkLogger().Info("Add Rx Rate Limiter")
if err := addRxRateLimiter(endpoint, rxRateLimiterMaxRate); err != nil { if err := addRxRateLimiter(endpoint, rxRateLimiterMaxRate); err != nil {
return err return err
} }
} }
txRateLimiterMaxRate := hypervisor.hypervisorConfig().TxRateLimiterMaxRate txRateLimiterMaxRate := s.hypervisor.hypervisorConfig().TxRateLimiterMaxRate
if txRateLimiterMaxRate > 0 { if txRateLimiterMaxRate > 0 {
networkLogger().Info("Add Tx Rate Limiter") networkLogger().Info("Add Tx Rate Limiter")
if err := addTxRateLimiter(endpoint, txRateLimiterMaxRate); err != nil { if err := addTxRateLimiter(endpoint, txRateLimiterMaxRate); err != nil {

View File

@ -15,6 +15,7 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/drivers" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/drivers"
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/safchain/ethtool" "github.com/safchain/ethtool"
) )
@ -72,27 +73,29 @@ func (endpoint *PhysicalEndpoint) NetworkPair() *NetworkInterfacePair {
// Attach for physical endpoint binds the physical network interface to // Attach for physical endpoint binds the physical network interface to
// vfio-pci and adds device to the hypervisor with vfio-passthrough. // vfio-pci and adds device to the hypervisor with vfio-passthrough.
func (endpoint *PhysicalEndpoint) Attach(h hypervisor) error { func (endpoint *PhysicalEndpoint) Attach(s *Sandbox) error {
// Unbind physical interface from host driver and bind to vfio // Unbind physical interface from host driver and bind to vfio
// so that it can be passed to qemu. // so that it can be passed to qemu.
if err := bindNICToVFIO(endpoint); err != nil { vfioPath, err := bindNICToVFIO(endpoint)
if err != nil {
return err return err
} }
// TODO: use device manager as general device management entrance c, err := cgroups.DeviceToCgroupDevice(vfioPath)
var vendorID, deviceID string if err != nil {
if splits := strings.Split(endpoint.VendorDeviceID, " "); len(splits) == 2 { return err
vendorID = splits[0]
deviceID = splits[1]
} }
d := config.VFIODev{ d := config.DeviceInfo{
BDF: endpoint.BDF, ContainerPath: c.Path,
VendorID: vendorID, DevType: string(c.Type),
DeviceID: deviceID, Major: c.Major,
Minor: c.Minor,
ColdPlug: true,
} }
return h.addDevice(d, vfioDev) _, err = s.AddDevice(d)
return err
} }
// Detach for physical endpoint unbinds the physical network interface from vfio-pci // Detach for physical endpoint unbinds the physical network interface from vfio-pci

View File

@ -824,7 +824,7 @@ func (s *Sandbox) createNetwork() error {
// after vm is started. // after vm is started.
if s.factory == nil { if s.factory == nil {
// Add the network // Add the network
endpoints, err := s.network.Add(s.ctx, &s.config.NetworkConfig, s.hypervisor, false) endpoints, err := s.network.Add(s.ctx, &s.config.NetworkConfig, s, false)
if err != nil { if err != nil {
return err return err
} }
@ -991,7 +991,7 @@ func (s *Sandbox) startVM() (err error) {
// In case of vm factory, network interfaces are hotplugged // In case of vm factory, network interfaces are hotplugged
// after vm is started. // after vm is started.
if s.factory != nil { if s.factory != nil {
endpoints, err := s.network.Add(s.ctx, &s.config.NetworkConfig, s.hypervisor, true) endpoints, err := s.network.Add(s.ctx, &s.config.NetworkConfig, s, true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -66,7 +66,7 @@ func (endpoint *TapEndpoint) SetProperties(properties NetworkInfo) {
} }
// Attach for tap endpoint adds the tap interface to the hypervisor. // Attach for tap endpoint adds the tap interface to the hypervisor.
func (endpoint *TapEndpoint) Attach(h hypervisor) error { func (endpoint *TapEndpoint) Attach(s *Sandbox) error {
return fmt.Errorf("TapEndpoint does not support Attach, if you're using docker please use --net none") return fmt.Errorf("TapEndpoint does not support Attach, if you're using docker please use --net none")
} }

View File

@ -68,7 +68,8 @@ func (endpoint *TuntapEndpoint) SetProperties(properties NetworkInfo) {
} }
// Attach for tap endpoint adds the tap interface to the hypervisor. // Attach for tap endpoint adds the tap interface to the hypervisor.
func (endpoint *TuntapEndpoint) Attach(h hypervisor) error { func (endpoint *TuntapEndpoint) Attach(s *Sandbox) error {
h := s.hypervisor
if err := xConnectVMNetwork(endpoint, h); err != nil { if err := xConnectVMNetwork(endpoint, h); err != nil {
networkLogger().WithError(err).Error("Error bridging virtual endpoint") networkLogger().WithError(err).Error("Error bridging virtual endpoint")
return err return err

View File

@ -89,7 +89,8 @@ func (endpoint *VethEndpoint) SetProperties(properties NetworkInfo) {
// Attach for veth endpoint bridges the network pair and adds the // Attach for veth endpoint bridges the network pair and adds the
// tap interface of the network pair to the hypervisor. // tap interface of the network pair to the hypervisor.
func (endpoint *VethEndpoint) Attach(h hypervisor) error { func (endpoint *VethEndpoint) Attach(s *Sandbox) error {
h := s.hypervisor
if err := xConnectVMNetwork(endpoint, h); err != nil { if err := xConnectVMNetwork(endpoint, h); err != nil {
networkLogger().WithError(err).Error("Error bridging virtual endpoint") networkLogger().WithError(err).Error("Error bridging virtual endpoint")
return err return err

View File

@ -74,7 +74,7 @@ func (endpoint *VhostUserEndpoint) NetworkPair() *NetworkInterfacePair {
} }
// Attach for vhostuser endpoint // Attach for vhostuser endpoint
func (endpoint *VhostUserEndpoint) Attach(h hypervisor) error { func (endpoint *VhostUserEndpoint) Attach(s *Sandbox) error {
// Generate a unique ID to be used for hypervisor commandline fields // Generate a unique ID to be used for hypervisor commandline fields
randBytes, err := utils.GenerateRandomBytes(8) randBytes, err := utils.GenerateRandomBytes(8)
if err != nil { if err != nil {
@ -89,7 +89,7 @@ func (endpoint *VhostUserEndpoint) Attach(h hypervisor) error {
Type: config.VhostUserNet, Type: config.VhostUserNet,
} }
return h.addDevice(d, vhostuserDev) return s.hypervisor.addDevice(d, vhostuserDev)
} }
// Detach for vhostuser endpoint // Detach for vhostuser endpoint

View File

@ -78,9 +78,11 @@ func TestVhostUserEndpointAttach(t *testing.T) {
EndpointType: VhostUserEndpointType, EndpointType: VhostUserEndpointType,
} }
h := &mockHypervisor{} s := &Sandbox{
hypervisor: &mockHypervisor{},
}
err := v.Attach(h) err := v.Attach(s)
assert.NoError(err) assert.NoError(err)
} }