Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid resetting the VF if the netns does not exist #313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion cmd/sriov/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ func cmdDel(args *skel.CmdArgs) error {
return nil
}

allocator := utils.NewPCIAllocator(config.DefaultCNIDir)

err = allocator.Lock(netConf.DeviceID)
if err != nil {
return fmt.Errorf("cmdDel() error obtaining lock for device [%s]: %w", netConf.DeviceID, err)
}

logging.Debug("Acquired device lock",
"func", "cmdDel",
"DeviceID", netConf.DeviceID)

defer func() {
if err == nil && cRefPath != "" {
_ = utils.CleanCachedNetConf(cRefPath)
Expand All @@ -270,6 +281,9 @@ func cmdDel(args *skel.CmdArgs) error {

sm := sriov.NewSriovManager()

logging.Debug("Reset VF configuration",
"func", "cmdDel",
"netConf.DeviceID", netConf.DeviceID)
/* ResetVFConfig resets a VF administratively. We must run ResetVFConfig
before ReleaseVF because some drivers will error out if we try to
reset netdev VF with trust off. So, reset VF MAC address via PF first.
Expand All @@ -288,13 +302,22 @@ func cmdDel(args *skel.CmdArgs) error {
// IPAM resources
_, ok := err.(ns.NSPathNotExistErr)
if ok {
logging.Debug("Exiting as the network namespace does not exists anymore",
"func", "cmdDel",
"netConf.DeviceID", netConf.DeviceID,
"args.Netns", args.Netns)
return nil
}

return fmt.Errorf("failed to open netns %s: %q", netns, err)
}
defer netns.Close()

logging.Debug("Release the VF",
"func", "cmdDel",
"netConf.DeviceID", netConf.DeviceID,
"args.Netns", args.Netns,
"args.IfName", args.IfName)
if err = sm.ReleaseVF(netConf, args.IfName, netns); err != nil {
return err
}
Expand All @@ -305,7 +328,6 @@ func cmdDel(args *skel.CmdArgs) error {
"func", "cmdDel",
"config.DefaultCNIDir", config.DefaultCNIDir,
"netConf.DeviceID", netConf.DeviceID)
allocator := utils.NewPCIAllocator(config.DefaultCNIDir)
if err = allocator.DeleteAllocatedPCI(netConf.DeviceID); err != nil {
return fmt.Errorf("error cleaning the pci allocation for vf pci address %s: %v", netConf.DeviceID, err)
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func LoadConf(bytes []byte) (*sriovtypes.NetConf, error) {
return nil, fmt.Errorf("LoadConf(): VF pci addr is required")
}

allocator := utils.NewPCIAllocator(DefaultCNIDir)
err := allocator.Lock(n.DeviceID)
if err != nil {
return nil, err
}
logging.Debug("Acquired device lock",
"func", "LoadConf",
"DeviceID", n.DeviceID)

// Check if the device is already allocated.
// This is to prevent issues where kubelet request to delete a pod and in the same time a new pod using the same
// vf is started. we can have an issue where the cmdDel of the old pod is called AFTER the cmdAdd of the new one
Expand All @@ -56,7 +65,6 @@ func LoadConf(bytes []byte) (*sriovtypes.NetConf, error) {
"func", "LoadConf",
"DefaultCNIDir", DefaultCNIDir,
"n.DeviceID", n.DeviceID)
allocator := utils.NewPCIAllocator(DefaultCNIDir)
isAllocated, err := allocator.IsAllocated(n.DeviceID)
if err != nil {
return n, err
Expand Down
38 changes: 38 additions & 0 deletions pkg/utils/pci_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/logging"
"golang.org/x/sys/unix"
)

const pciLockAcquireTimeout = 60 * time.Second

type PCIAllocation interface {
SaveAllocatedPCI(string, string) error
DeleteAllocatedPCI(string) error
Expand All @@ -25,6 +29,40 @@ func NewPCIAllocator(dataDir string) *PCIAllocator {
return &PCIAllocator{dataDir: filepath.Join(dataDir, "pci")}
}

// Lock gets an exclusive lock on the given PCI address, ensuring there is no other process configuring / or de-configuring the same device.
func (p *PCIAllocator) Lock(pciAddress string) error {
if err := os.MkdirAll(p.dataDir, 0600); err != nil {
return fmt.Errorf("failed to create the sriov data directory(%q): %v", p.dataDir, err)
}

path := filepath.Join(p.dataDir, pciAddress)

// unix.O_CREAT - Create the file if it doesn't exist
// unix.O_RDWR - Open the file for read/write
// unix.O_CLOEXEC - Automatically close the file on exit. This is useful to keep the flock until the process exits
fd, err := unix.Open(path, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0600)
if err != nil {
return fmt.Errorf("failed to open PCI file [%s] for locking: %w", path, err)
}

errCh := make(chan error)
go func() {
// unix.LOCK_EX - Exclusive lock
errCh <- unix.Flock(fd, unix.LOCK_EX)
}()

select {
case err = <-errCh:
if err != nil {
return fmt.Errorf("failed to flock PCI file [%s]: %w", path, err)
}
return nil

case <-time.After(pciLockAcquireTimeout):
return fmt.Errorf("time out while waiting to acquire exclusive lock on [%s]", path)
}
}

// SaveAllocatedPCI creates a file with the pci address as a name and the network namespace as the content
// return error if the file was not created
func (p *PCIAllocator) SaveAllocatedPCI(pciAddress, ns string) error {
Expand Down
Loading