Skip to content

Commit

Permalink
fixed VF selection issue from PF
Browse files Browse the repository at this point in the history
refactoring caused a new issue with selecting a free VF given a
PF name.

Change-Id: Ia7f1e62a55a67a752770332dafc9d1f632479ae4
  • Loading branch information
ahalimx86 committed Nov 29, 2018
1 parent 9c0287f commit cb8ac8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
40 changes: 18 additions & 22 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"encoding/json"
"fmt"
"os"
"strings"

sriovtypes "github.com/intel/sriov-cni/pkg/types"
Expand Down Expand Up @@ -109,37 +110,32 @@ func AssignFreeVF(conf *sriovtypes.NetConf) error {
for vf := 0; vf < vfTotal; vf++ {
infos, err = utils.GetVFLinkNames(pfName, vf)
if err != nil {
if _, ok := err.(*utils.NetDeviceNotFoundErr); ok {
if _, ok := err.(*os.PathError); ok {
continue
} else {
return fmt.Errorf("failed to read the virtfn%d dir of the device %q: %v", vf, pfName, err)
}
return fmt.Errorf("failed to read the virtfn%d dir of the device %q: %v", vf, pfName, err)
}

if (len(infos) == 0) && (vf == (vfTotal - 1)) {
return fmt.Errorf("no free Virtual function exist for PF %s, last vf is virtfn%d", pfName, vf)
}

if (len(infos) == 0) && (vf != (vfTotal - 1)) {
continue
}
} else if len(infos) > 0 {

if len(infos) == MaxSharedVf {
conf.Sharedvf = true
}
if len(infos) == MaxSharedVf {
conf.Sharedvf = true
}

if len(infos) <= MaxSharedVf {
vfIdx = vf
pciAddr, err = utils.GetPciAddress(pfName, vfIdx)
if err != nil {
return fmt.Errorf("err in getting pci address - %q", err)
if len(infos) <= MaxSharedVf {
vfIdx = vf
pciAddr, err = utils.GetPciAddress(pfName, vfIdx)
if err != nil {
return fmt.Errorf("err in getting pci address for VF %d of PF %s: %q", vf, pfName, err)
}
break
} else {
return fmt.Errorf("multiple network devices found with VF id: %d under PF %s: %+v", vf, pfName, infos)
}
break
} else {
return fmt.Errorf("multiple network devices found with VF id: %d under PF %s: %+v", vf, pfName, infos)
}
}

if len(infos) != 1 && len(infos) != MaxSharedVf {
if len(infos) == 0 {
return fmt.Errorf("no virtual network resources available for the %q", conf.Master)
}

Expand Down
21 changes: 1 addition & 20 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,38 +136,19 @@ func GetSharedPF(ifName string) (string, error) {
return pfName, fmt.Errorf("Shared PF not found")
}

// NetDeviceNotFoundErr is a custom error type
type NetDeviceNotFoundErr struct {
errMsg string
}

func (e *NetDeviceNotFoundErr) Error() string {
return e.errMsg
}

func newNetDeviceNotFoundErr(msg string) *NetDeviceNotFoundErr {
return &NetDeviceNotFoundErr{
errMsg: fmt.Sprintf("net device not found: %s", msg),
}
}

// GetVFLinkNames returns VF's network interface name given it's PF name as string and VF id as int
func GetVFLinkNames(pfName string, vfID int) ([]string, error) {
var names []string
vfDir := filepath.Join(netDirectory, pfName, "device", fmt.Sprintf("virtfn%d", vfID), "net")
if _, err := os.Lstat(vfDir); err != nil {
return nil, newNetDeviceNotFoundErr(fmt.Sprintf("failed to open the virtfn%d dir of the device %q: %v", vfID, pfName, err))
return nil, err
}

fInfos, err := ioutil.ReadDir(vfDir)
if err != nil {
return nil, fmt.Errorf("failed to read the virtfn%d dir of the device %q: %v", vfID, pfName, err)
}

if len(fInfos) < 1 {
return nil, fmt.Errorf("no netdevice found in dir: %s", vfDir)
}

names = make([]string, 0)
for _, f := range fInfos {
names = append(names, f.Name())
Expand Down

0 comments on commit cb8ac8d

Please sign in to comment.