From 2cb4bb9db709c8e622ca0ede73089a458516cf3c Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 14 Dec 2018 14:50:11 -0800 Subject: [PATCH] virtcontainers: network: Reorganize endpoints interconnection In order to prevent from future duplication of calls into the hypervisor interface, the hypervisor is directly passed as part of the xConnectVMNetwork() function. Because this does not apply the disconnection case, this commit splits the former function into two separate ones. Signed-off-by: Sebastien Boeuf --- virtcontainers/bridgedmacvlan_endpoint.go | 4 +-- virtcontainers/ipvlan_endpoint.go | 4 +-- virtcontainers/network.go | 41 ++++++++++++++++------- virtcontainers/veth_endpoint.go | 8 ++--- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/virtcontainers/bridgedmacvlan_endpoint.go b/virtcontainers/bridgedmacvlan_endpoint.go index 9ea0a3066..d2efd56f5 100644 --- a/virtcontainers/bridgedmacvlan_endpoint.go +++ b/virtcontainers/bridgedmacvlan_endpoint.go @@ -84,7 +84,7 @@ func (endpoint *BridgedMacvlanEndpoint) NetworkPair() *NetworkInterfacePair { // Attach for virtual endpoint bridges the network pair and adds the // tap interface of the network pair to the hypervisor. func (endpoint *BridgedMacvlanEndpoint) Attach(h hypervisor) error { - if err := xconnectVMNetwork(endpoint, true, h.hypervisorConfig().NumVCPUs, h.hypervisorConfig().DisableVhostNet); err != nil { + if err := xConnectVMNetwork(endpoint, h); err != nil { networkLogger().WithError(err).Error("Error bridging virtual ep") return err } @@ -102,7 +102,7 @@ func (endpoint *BridgedMacvlanEndpoint) Detach(netNsCreated bool, netNsPath stri } return doNetNS(netNsPath, func(_ ns.NetNS) error { - return xconnectVMNetwork(endpoint, false, 0, false) + return xDisconnectVMNetwork(endpoint) }) } diff --git a/virtcontainers/ipvlan_endpoint.go b/virtcontainers/ipvlan_endpoint.go index beab38d86..b71bbd0fb 100644 --- a/virtcontainers/ipvlan_endpoint.go +++ b/virtcontainers/ipvlan_endpoint.go @@ -87,7 +87,7 @@ func (endpoint *IPVlanEndpoint) NetworkPair() *NetworkInterfacePair { // Attach for virtual endpoint bridges the network pair and adds the // tap interface of the network pair to the hypervisor. func (endpoint *IPVlanEndpoint) Attach(h hypervisor) error { - if err := xconnectVMNetwork(endpoint, true, h.hypervisorConfig().NumVCPUs, h.hypervisorConfig().DisableVhostNet); err != nil { + if err := xConnectVMNetwork(endpoint, h); err != nil { networkLogger().WithError(err).Error("Error bridging virtual ep") return err } @@ -105,7 +105,7 @@ func (endpoint *IPVlanEndpoint) Detach(netNsCreated bool, netNsPath string) erro } return doNetNS(netNsPath, func(_ ns.NetNS) error { - return xconnectVMNetwork(endpoint, false, 0, false) + return xDisconnectVMNetwork(endpoint) }) } diff --git a/virtcontainers/network.go b/virtcontainers/network.go index a0846e638..613caa68f 100644 --- a/virtcontainers/network.go +++ b/virtcontainers/network.go @@ -501,30 +501,45 @@ func getLinkByName(netHandle *netlink.Handle, name string, expectedLink netlink. return nil, fmt.Errorf("Incorrect link type %s, expecting %s", link.Type(), expectedLink.Type()) } -// The endpoint type should dictate how the connection needs to be made -func xconnectVMNetwork(endpoint Endpoint, connect bool, numCPUs uint32, disableVhostNet bool) error { +// The endpoint type should dictate how the connection needs to happen. +func xConnectVMNetwork(endpoint Endpoint, h hypervisor) error { + netPair := endpoint.NetworkPair() + + numCPUs := h.hypervisorConfig().NumVCPUs + disableVhostNet := h.hypervisorConfig().DisableVhostNet + + if netPair.NetInterworkingModel == NetXConnectDefaultModel { + netPair.NetInterworkingModel = DefaultNetInterworkingModel + } + + switch netPair.NetInterworkingModel { + case NetXConnectBridgedModel: + return bridgeNetworkPair(endpoint, numCPUs, disableVhostNet) + case NetXConnectMacVtapModel: + return tapNetworkPair(endpoint, numCPUs, disableVhostNet) + case NetXConnectTCFilterModel: + return setupTCFiltering(endpoint, numCPUs, disableVhostNet) + case NetXConnectEnlightenedModel: + return fmt.Errorf("Unsupported networking model") + default: + return fmt.Errorf("Invalid internetworking model") + } +} + +// The endpoint type should dictate how the disconnection needs to happen. +func xDisconnectVMNetwork(endpoint Endpoint) error { netPair := endpoint.NetworkPair() if netPair.NetInterworkingModel == NetXConnectDefaultModel { netPair.NetInterworkingModel = DefaultNetInterworkingModel } + switch netPair.NetInterworkingModel { case NetXConnectBridgedModel: - netPair.NetInterworkingModel = NetXConnectBridgedModel - if connect { - return bridgeNetworkPair(endpoint, numCPUs, disableVhostNet) - } return unBridgeNetworkPair(endpoint) case NetXConnectMacVtapModel: - netPair.NetInterworkingModel = NetXConnectMacVtapModel - if connect { - return tapNetworkPair(endpoint, numCPUs, disableVhostNet) - } return untapNetworkPair(endpoint) case NetXConnectTCFilterModel: - if connect { - return setupTCFiltering(endpoint, numCPUs, disableVhostNet) - } return removeTCFiltering(endpoint) case NetXConnectEnlightenedModel: return fmt.Errorf("Unsupported networking model") diff --git a/virtcontainers/veth_endpoint.go b/virtcontainers/veth_endpoint.go index 324615154..66994a149 100644 --- a/virtcontainers/veth_endpoint.go +++ b/virtcontainers/veth_endpoint.go @@ -88,7 +88,7 @@ func (endpoint *VethEndpoint) SetProperties(properties NetworkInfo) { // Attach for veth endpoint bridges the network pair and adds the // tap interface of the network pair to the hypervisor. func (endpoint *VethEndpoint) Attach(h hypervisor) error { - if err := xconnectVMNetwork(endpoint, true, h.hypervisorConfig().NumVCPUs, h.hypervisorConfig().DisableVhostNet); err != nil { + if err := xConnectVMNetwork(endpoint, h); err != nil { networkLogger().WithError(err).Error("Error bridging virtual endpoint") return err } @@ -106,13 +106,13 @@ func (endpoint *VethEndpoint) Detach(netNsCreated bool, netNsPath string) error } return doNetNS(netNsPath, func(_ ns.NetNS) error { - return xconnectVMNetwork(endpoint, false, 0, false) + return xDisconnectVMNetwork(endpoint) }) } // HotAttach for the veth endpoint uses hot plug device func (endpoint *VethEndpoint) HotAttach(h hypervisor) error { - if err := xconnectVMNetwork(endpoint, true, h.hypervisorConfig().NumVCPUs, h.hypervisorConfig().DisableVhostNet); err != nil { + if err := xConnectVMNetwork(endpoint, h); err != nil { networkLogger().WithError(err).Error("Error bridging virtual ep") return err } @@ -131,7 +131,7 @@ func (endpoint *VethEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPa } if err := doNetNS(netNsPath, func(_ ns.NetNS) error { - return xconnectVMNetwork(endpoint, false, 0, h.hypervisorConfig().DisableVhostNet) + return xDisconnectVMNetwork(endpoint) }); err != nil { networkLogger().WithError(err).Warn("Error un-bridging virtual ep") }