This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #852 from amshinde/ipvlan
Add support for ipvlan network driver
- Loading branch information
Showing
5 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/containernetworking/plugins/pkg/ns" | ||
) | ||
|
||
// IPVlanEndpoint represents a ipvlan endpoint that is bridged to the VM | ||
type IPVlanEndpoint struct { | ||
NetPair NetworkInterfacePair | ||
EndpointProperties NetworkInfo | ||
EndpointType EndpointType | ||
PCIAddr string | ||
} | ||
|
||
func createIPVlanNetworkEndpoint(idx int, ifName string) (*IPVlanEndpoint, error) { | ||
if idx < 0 { | ||
return &IPVlanEndpoint{}, fmt.Errorf("invalid network endpoint index: %d", idx) | ||
} | ||
|
||
// Use tc filtering for ipvlan, since the other inter networking models will | ||
// not work for ipvlan. | ||
interworkingModel := NetXConnectTCFilterModel | ||
netPair, err := createNetworkInterfacePair(idx, ifName, interworkingModel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
endpoint := &IPVlanEndpoint{ | ||
NetPair: netPair, | ||
EndpointType: IPVlanEndpointType, | ||
} | ||
if ifName != "" { | ||
endpoint.NetPair.VirtIface.Name = ifName | ||
} | ||
|
||
return endpoint, nil | ||
} | ||
|
||
// Properties returns properties of the interface. | ||
func (endpoint *IPVlanEndpoint) Properties() NetworkInfo { | ||
return endpoint.EndpointProperties | ||
} | ||
|
||
// Name returns name of the veth interface in the network pair. | ||
func (endpoint *IPVlanEndpoint) Name() string { | ||
return endpoint.NetPair.VirtIface.Name | ||
} | ||
|
||
// HardwareAddr returns the mac address that is assigned to the tap interface | ||
// in th network pair. | ||
func (endpoint *IPVlanEndpoint) HardwareAddr() string { | ||
return endpoint.NetPair.TAPIface.HardAddr | ||
} | ||
|
||
// Type identifies the endpoint as a virtual endpoint. | ||
func (endpoint *IPVlanEndpoint) Type() EndpointType { | ||
return endpoint.EndpointType | ||
} | ||
|
||
// SetProperties sets the properties for the endpoint. | ||
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 | ||
} | ||
|
||
// SetPciAddr sets the PCI address of the endpoint. | ||
func (endpoint *IPVlanEndpoint) SetPciAddr(pciAddr string) { | ||
endpoint.PCIAddr = pciAddr | ||
} | ||
|
||
// NetworkPair returns the network pair of the endpoint. | ||
func (endpoint *IPVlanEndpoint) NetworkPair() *NetworkInterfacePair { | ||
return &endpoint.NetPair | ||
} | ||
|
||
// 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 { | ||
networkLogger().WithError(err).Error("Error bridging virtual ep") | ||
return err | ||
} | ||
|
||
return h.addDevice(endpoint, netDev) | ||
} | ||
|
||
// Detach for the virtual endpoint tears down the tap and bridge | ||
// created for the veth interface. | ||
func (endpoint *IPVlanEndpoint) Detach(netNsCreated bool, netNsPath string) error { | ||
// The network namespace would have been deleted at this point | ||
// if it has not been created by virtcontainers. | ||
if !netNsCreated { | ||
return nil | ||
} | ||
|
||
return doNetNS(netNsPath, func(_ ns.NetNS) error { | ||
return xconnectVMNetwork(endpoint, false, 0, false) | ||
}) | ||
} | ||
|
||
// HotAttach for physical endpoint not supported yet | ||
func (endpoint *IPVlanEndpoint) HotAttach(h hypervisor) error { | ||
return fmt.Errorf("IPVlanEndpoint does not support Hot attach") | ||
} | ||
|
||
// HotDetach for physical endpoint not supported yet | ||
func (endpoint *IPVlanEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error { | ||
return fmt.Errorf("IPVlanEndpoint does not support Hot detach") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCreateIPVlanEndpoint(t *testing.T) { | ||
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04} | ||
|
||
expected := &IPVlanEndpoint{ | ||
NetPair: NetworkInterfacePair{ | ||
TapInterface: TapInterface{ | ||
ID: "uniqueTestID-5", | ||
Name: "br5_kata", | ||
TAPIface: NetworkInterface{ | ||
Name: "tap5_kata", | ||
}, | ||
}, | ||
VirtIface: NetworkInterface{ | ||
Name: "eth5", | ||
HardAddr: macAddr.String(), | ||
}, | ||
|
||
NetInterworkingModel: NetXConnectTCFilterModel, | ||
}, | ||
EndpointType: IPVlanEndpointType, | ||
} | ||
|
||
result, err := createIPVlanNetworkEndpoint(5, "") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// the resulting ID will be random - so let's overwrite to test the rest of the flow | ||
result.NetPair.ID = "uniqueTestID-5" | ||
|
||
// the resulting mac address will be random - so lets overwrite it | ||
result.NetPair.VirtIface.HardAddr = macAddr.String() | ||
|
||
if reflect.DeepEqual(result, expected) == false { | ||
t.Fatalf("\nGot: %+v, \n\nExpected: %+v", result, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters