Skip to content

Commit

Permalink
fix: save all the interface names
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitrios Markou <[email protected]>
  • Loading branch information
mardim91 committed May 3, 2024
1 parent bff971a commit 5c47616
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func deleteResources(args *skel.CmdArgs, netConf *evpngwtypes.NetConf) error {
}()

// Release VF from Pods namespace and rename it to the original name
err = sm.ReleaseVF(netConf, netns, args.Netns)
err = sm.ReleaseVF(netConf, netns)
if err != nil {
return fmt.Errorf("deleteResources() error releasing VF: %q", err)
}
Expand Down
49 changes: 34 additions & 15 deletions pkg/sriov/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

evpngwtypes "github.com/opiproject/opi-gateway-evpn-cni/pkg/types"
"github.com/opiproject/opi-gateway-evpn-cni/pkg/utils"
"github.com/vishvananda/netlink"
)

type pciUtils interface {
Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *pciUtilsImpl) EnableArpAndNdiscNotify(ifName string) error {
// Manager provides interface invoke sriov nic related operations
type Manager interface {
SetupVF(conf *evpngwtypes.NetConf, podifName string, netns ns.NetNS) (string, error)
ReleaseVF(conf *evpngwtypes.NetConf, netns ns.NetNS, netNSPath string) error
ReleaseVF(conf *evpngwtypes.NetConf, netns ns.NetNS) error
ResetVFConfig(conf *evpngwtypes.NetConf) error
ResetVF(conf *evpngwtypes.NetConf) error
ApplyVFConfig(conf *evpngwtypes.NetConf) error
Expand All @@ -73,6 +74,7 @@ func (s *sriovManager) SetupVF(conf *evpngwtypes.NetConf, podifName string, netn
ctx := context.Background()

linkName := conf.OrigVfState.HostIFName
conf.TotalIntfNames = append(conf.TotalIntfNames, linkName)

linkObj, err := s.nLink.LinkByName(ctx, linkName)
if err != nil {
Expand All @@ -92,6 +94,8 @@ func (s *sriovManager) SetupVF(conf *evpngwtypes.NetConf, podifName string, netn
return "", fmt.Errorf("error setting temp IF name %s for %s", tempName, linkName)
}

conf.TotalIntfNames = append(conf.TotalIntfNames, tempName)

macAddress := linkObj.Attrs().HardwareAddr.String()
if conf.MAC != "" {
fmt.Printf("SetupVF(): MAC address configuration functionality is not supported currently")
Expand Down Expand Up @@ -139,6 +143,8 @@ func (s *sriovManager) SetupVF(conf *evpngwtypes.NetConf, podifName string, netn
return fmt.Errorf("error setting container interface name %s for %s", linkName, tempName)
}

conf.TotalIntfNames = append(conf.TotalIntfNames, podifName)

// 6. Enable IPv4 ARP notify and IPv6 Network Discovery notify
// Error is ignored here because enabling this feature is only a performance enhancement.
_ = s.utils.EnableArpAndNdiscNotify(podifName)
Expand All @@ -157,32 +163,39 @@ func (s *sriovManager) SetupVF(conf *evpngwtypes.NetConf, podifName string, netn
}

// ReleaseVF reset a VF from Pod netns and return it to init netns
func (s *sriovManager) ReleaseVF(conf *evpngwtypes.NetConf, netns ns.NetNS, netNSPath string) error {
func (s *sriovManager) ReleaseVF(conf *evpngwtypes.NetConf, netns ns.NetNS) error {
var linkObj netlink.Link
var podifName string
ctx := context.Background()

initns, err := ns.GetCurrentNS()
if err != nil {
return fmt.Errorf("ReleaseVF(): failed to get init netns: %v", err)
}

// get VF netdevice from PCI that is attached to container. This is executed on the host namespace accessing
// the containers filesystem through the /proc/<PID> path on the host.
vfNetdevices, err := utils.GetContainerNetDevFromPci(netNSPath, conf.DeviceID)
if err != nil {
return fmt.Errorf("ReleaseVF(): failed to get VF netdevice from PCI %s : %v", conf.DeviceID, err)
}

if len(vfNetdevices) == 0 {
// The VF has not been found in the Container namespace so no point to continue
if len(conf.TotalIntfNames) == 0 {
// No SetupVF action has been executed as the slice is empty.
// There is no point to continue further
return nil
}

podifName := vfNetdevices[0]
return netns.Do(func(_ ns.NetNS) error {
// get VF device
linkObj, err := s.nLink.LinkByName(ctx, podifName)
if err != nil {
return fmt.Errorf("ReleaseVF(): failed to get netlink device with name %s: %q", podifName, err)
for _, ifName := range conf.TotalIntfNames {
tempLinkObj, err := s.nLink.LinkByName(ctx, ifName)
if err != nil {
continue
}
linkObj = tempLinkObj
podifName = ifName
break
}

if linkObj == nil {
// The VF has not been found in the container's namespace so no point to continue.
// This is according to the idempotent logic where if something is not found
// then is considered that is not an error.
return nil
}

// shutdown VF device
Expand Down Expand Up @@ -448,6 +461,12 @@ func (s *sriovManager) ResetVF(conf *evpngwtypes.NetConf) error {
}

// shutdown VF device
// Although this call is executed successfully
// sometimes the VF is not going down. That makes
// the system to report a "resource is busy" error when
// we try to rename the interface further below.
// This error is not fatal and the interface gets renamed correctly
// so we will not do anything to address the issue currently.
if err = s.nLink.LinkSetDown(ctx, linkObj); err != nil {
return fmt.Errorf("ResetVF(): failed to set link %s down: %q", curNetVFName, err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type NetConf struct {
LogicalBridge string `json:"logical_bridge,omitempty"`
LogicalBridges []string `json:"logical_bridges,omitempty"`
PortType string
BridgePortName string // Stores the "name" of the created Bridge Port
DeviceID string `json:"deviceID"` // PCI address of a VF in valid sysfs format
BridgePortName string // Stores the "name" of the created Bridge Port
TotalIntfNames []string // Stores all the possible "names" of the interface
DeviceID string `json:"deviceID"` // PCI address of a VF in valid sysfs format
VFID int
MinTxRate *int `json:"min_tx_rate"` // Mbps, 0 = disable rate limiting (XPU Not supported)
MaxTxRate *int `json:"max_tx_rate"` // Mbps, 0 = disable rate limiting (XPU Not supported)
Expand Down

0 comments on commit 5c47616

Please sign in to comment.