mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-01 09:42:45 +00:00
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 <sebastien.boeuf@intel.com>
This commit is contained in:
parent
bcf995bfe1
commit
2cb4bb9db7
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user