From 38949e5a2ebff27c29187a7c096f94df68bbb7f6 Mon Sep 17 00:00:00 2001 From: Sebastian Sch Date: Thu, 28 Oct 2021 12:56:55 +0300 Subject: [PATCH] Add the tun mounting with vhost is requested This commit add also the tun device into the container. This is needed to create tap devices inside the container network namespace Vhost-net is used to accelerate the dpdk connected to kernel networking using tap devices. Signed-off-by: Sebastian Sch --- README.md | 2 +- pkg/netdevice/netInfoProviders.go | 10 +++++++++- pkg/netdevice/pciNetDevice_test.go | 2 +- pkg/netdevice/vhostNet.go | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ebde5fd3d..48c98a38b 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,7 @@ This selector is applicable when "deviceType" is "netDevice"(note: this is defau | "linkTypes" | N | The link type of the net device associated with the PCI device | `string` list Default: `null` | "linkTypes": ["ether"] | | "ddpProfiles" | N | A map of device selectors | `string` list Default: `null` | "ddpProfiles": ["GTPv1-C/U IPv4/IPv6 payload"] | | "isRdma" | N | Mount RDMA resources | `bool` values `true` or `false` Default: `false` | "isRdma": `true` | -| "needVhostNet"| N | Share /dev/vhost-net | `bool` values `true` or `false` Default: `false` | "needVhostNet": `true` | +| "needVhostNet"| N | Share /dev/vhost-net and /dev/net/tun | `bool` values `true` or `false` Default: `false` | "needVhostNet": `true` | [//]: # (The tables above generated using: https://ozh.github.io/ascii-tables/) diff --git a/pkg/netdevice/netInfoProviders.go b/pkg/netdevice/netInfoProviders.go index 72e62e41d..e3995eda3 100644 --- a/pkg/netdevice/netInfoProviders.go +++ b/pkg/netdevice/netInfoProviders.go @@ -76,7 +76,15 @@ func (rip *vhostNetInfoProvider) GetDeviceSpecs() []*pluginapi.DeviceSpec { glog.Errorf("GetDeviceSpecs(): /dev/vhost-net doesn't exist") return nil } - return GetVhostNetDeviceSpec() + deviceSpec := GetVhostNetDeviceSpec() + + if !TunDeviceExist() { + glog.Errorf("GetDeviceSpecs(): /dev/net/tun doesn't exist") + return nil + } + deviceSpec = append(deviceSpec, GetTunDeviceSpec()...) + + return deviceSpec } func (rip *vhostNetInfoProvider) GetEnvVal() string { diff --git a/pkg/netdevice/pciNetDevice_test.go b/pkg/netdevice/pciNetDevice_test.go index 6a7a4b6e5..b3e89a484 100644 --- a/pkg/netdevice/pciNetDevice_test.go +++ b/pkg/netdevice/pciNetDevice_test.go @@ -247,7 +247,7 @@ var _ = Describe("PciNetDevice", func() { Expect(dev.GetDriver()).To(Equal("vfio-pci")) Expect(dev.GetNetName()).To(Equal("")) Expect(dev.GetEnvVal()).To(Equal("0000:00:00.1")) - Expect(dev.GetDeviceSpecs()).To(HaveLen(3)) // /dev/vfio/vfio0 and default /dev/vfio/vfio + vhost-net + Expect(dev.GetDeviceSpecs()).To(HaveLen(4)) // /dev/vfio/vfio0 and default /dev/vfio/vfio + vhost-net + tun Expect(dev.GetRdmaSpec().IsRdma()).To(BeFalse()) Expect(dev.GetRdmaSpec().GetRdmaDeviceSpec()).To(HaveLen(0)) Expect(dev.GetLinkType()).To(Equal("")) diff --git a/pkg/netdevice/vhostNet.go b/pkg/netdevice/vhostNet.go index 0a0b49214..f2758ca6d 100644 --- a/pkg/netdevice/vhostNet.go +++ b/pkg/netdevice/vhostNet.go @@ -23,3 +23,21 @@ func GetVhostNetDeviceSpec() []*pluginapi.DeviceSpec { return deviceSpec } + +// TunDeviceExist returns true if /dev/net/tun exists +func TunDeviceExist() bool { + _, err := os.Stat("/dev/net/tun") + return err == nil +} + +// GetTunDeviceSpec returns an instance of DeviceSpec for Tun +func GetTunDeviceSpec() []*pluginapi.DeviceSpec { + deviceSpec := make([]*pluginapi.DeviceSpec, 0) + deviceSpec = append(deviceSpec, &pluginapi.DeviceSpec{ + HostPath: "/dev/net/tun", + ContainerPath: "/dev/net/tun", + Permissions: "mrw", + }) + + return deviceSpec +}