Skip to content

Commit

Permalink
netResourcePool: add vdpa to Device Info file
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
  • Loading branch information
amorenoz committed Dec 10, 2020
1 parent d8466dd commit 911330a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
31 changes: 25 additions & 6 deletions pkg/netdevice/netResourcePool.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,36 @@ func (rp *netResourcePool) GetDeviceSpecs(deviceIDs []string) []*pluginapi.Devic
// StoreDeviceInfoFile stores the Device Info files according to the
// k8snetworkplumbingwg/device-info-spec
func (rp *netResourcePool) StoreDeviceInfoFile(resourceNamePrefix string) error {
var devInfo nettypes.DeviceInfo
for id, dev := range rp.GetDevicePool() {
netDev, ok := dev.(types.PciNetDevice)
if !ok {
return fmt.Errorf("StoreDeviceInfoFile: Only pciNetDevices are supported")
}
devInfo := nettypes.DeviceInfo{
Type: nettypes.DeviceInfoTypePCI,
Version: nettypes.DeviceInfoVersion,
Pci: &nettypes.PciDevice{
PciAddress: netDev.GetPciAddr(),
},

vdpaDev := netDev.GetVdpaDevice()
fmt.Printf("VDPA DEVICE %+v \n", vdpaDev)
fmt.Printf("NETPCI DEVICE %+v \n", netDev)
if vdpaDev != nil {
devInfo = nettypes.DeviceInfo{
Type: nettypes.DeviceInfoTypeVDPA,
Version: nettypes.DeviceInfoVersion,
Vdpa: &nettypes.VdpaDevice{
ParentDevice: vdpaDev.GetParent(),
Driver: vdpaDev.GetDriver(),
Path: vdpaDev.GetPath(),
PciAddress: netDev.GetPciAddr(),
},
}

} else {
devInfo = nettypes.DeviceInfo{
Type: nettypes.DeviceInfoTypePCI,
Version: nettypes.DeviceInfoVersion,
Pci: &nettypes.PciDevice{
PciAddress: netDev.GetPciAddr(),
},
}
}
resource := fmt.Sprintf("%s/%s", resourceNamePrefix, rp.GetConfig().ResourceName)
if err := rp.nadutils.SaveDeviceInfoFile(resource, id, &devInfo); err != nil {
Expand Down
74 changes: 72 additions & 2 deletions pkg/netdevice/netResourcePool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ var _ = Describe("NetResourcePool", func() {

devs := map[string]*v1beta1.Device{}
fake1 := &mocks.PciNetDevice{}
fake1.On("GetPciAddr").Return("0000:01:00.1")
fake1.On("GetPciAddr").Return("0000:01:00.1").
On("GetVdpaDevice").Return(nil)
fake2 := &mocks.PciNetDevice{}
fake2.On("GetPciAddr").Return("0000:01:00.2")
fake2.On("GetPciAddr").Return("0000:01:00.2").
On("GetVdpaDevice").Return(nil)
pcis := map[string]types.PciDevice{"fake1": fake1, "fake2": fake2}

It("should call nadutils to create a well formatted DeviceInfo object", func() {
Expand Down Expand Up @@ -146,5 +148,73 @@ var _ = Describe("NetResourcePool", func() {
Expect(err).ToNot(HaveOccurred())
})
})
Context("for vdpa devices devices", func() {
rc := &types.ResourceConfig{
ResourceName: "fakeResource",
ResourcePrefix: "fakeOrg.io",
SelectorObj: &types.NetDeviceSelectors{
VdpaType: "vhost",
},
}

devs := map[string]*v1beta1.Device{}
fakeVdpa1 := &mocks.VdpaDevice{}
fakeVdpa1.On("GetDriver").Return("vhost_vdpa").
On("GetParent").Return("vdpa1").
On("GetPath").Return("/dev/vhost-vdpa5")

fakeVdpa2 := &mocks.VdpaDevice{}
fakeVdpa2.On("GetDriver").Return("vhost_vdpa").
On("GetParent").Return("vdpa2").
On("GetPath").Return("/dev/vhost-vdpa6")

fake1 := &mocks.PciNetDevice{}
fake2 := &mocks.PciNetDevice{}
fake1.On("GetPciAddr").Return("0000:01:00.1").
On("GetVdpaDevice").Return(fakeVdpa1)
fake2.On("GetPciAddr").Return("0000:01:00.2").
On("GetVdpaDevice").Return(fakeVdpa2)

pcis := map[string]types.PciDevice{"fake1": fake1, "fake2": fake2}

It("should call nadutils to create a well formatted DeviceInfo object", func() {
nadutils := &mocks.NadUtils{}
nadutils.On("SaveDeviceInfoFile", "fakeOrg.io/fakeResource", "fake1", Anything).
Return(func(rName, id string, devInfo *nettypes.DeviceInfo) error {
if devInfo.Type != nettypes.DeviceInfoTypeVDPA ||
devInfo.Vdpa == nil ||
devInfo.Vdpa.ParentDevice != "vdpa1" ||
devInfo.Vdpa.Driver != "vhost_vdpa" ||
devInfo.Vdpa.Path != "/dev/vhost-vdpa5" {
return fmt.Errorf("wrong device info %+v", devInfo)
}
return nil
})
nadutils.On("SaveDeviceInfoFile", "fakeOrg.io/fakeResource", "fake2", Anything).
Return(func(rName, id string, devInfo *nettypes.DeviceInfo) error {
if devInfo.Type != nettypes.DeviceInfoTypeVDPA ||
devInfo.Vdpa == nil ||
devInfo.Vdpa.ParentDevice != "vdpa2" ||
devInfo.Vdpa.Driver != "vhost_vdpa" ||
devInfo.Vdpa.Path != "/dev/vhost-vdpa6" {
return fmt.Errorf("wrong device info %+v", devInfo)
}
return nil
})
rp := netdevice.NewNetResourcePool(nadutils, rc, devs, pcis)
err := rp.StoreDeviceInfoFile("fakeOrg.io")
Expect(err).ToNot(HaveOccurred())
nadutils.AssertExpectations(t)
})
It("should call nadutils to clean the DeviceInfo objects", func() {
nadutils := &mocks.NadUtils{}
nadutils.On("CleanDeviceInfoFile", "fakeOrg.io/fakeResource", "fake1").Return(nil)
nadutils.On("CleanDeviceInfoFile", "fakeOrg.io/fakeResource", "fake2").Return(nil)
rp := netdevice.NewNetResourcePool(nadutils, rc, devs, pcis)
err := rp.CleanDeviceInfoFile("fakeOrg.io")
Expect(err).ToNot(HaveOccurred())
nadutils.AssertExpectations(t)
})
})
})
})

0 comments on commit 911330a

Please sign in to comment.