From 8ede7c3ab10241a3f4b23c2daafe966796aa012c Mon Sep 17 00:00:00 2001 From: mmduh-483 Date: Wed, 23 Jan 2019 16:05:35 +0200 Subject: [PATCH] Release VF when CMD ADD failed After moving VF to container check if error occurs then move the VF back to original namespace Fixes #38 --- sriov/main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sriov/main.go b/sriov/main.go index b394bee56..a427eaf95 100644 --- a/sriov/main.go +++ b/sriov/main.go @@ -11,6 +11,7 @@ import ( "github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/version" "github.com/intel/sriov-cni/pkg/config" + "github.com/vishvananda/netlink" ) func init() { @@ -56,7 +57,21 @@ func cmdAdd(args *skel.CmdArgs) error { } if n.DeviceInfo != nil && n.DeviceInfo.PCIaddr != "" && n.DeviceInfo.Vfid >= 0 && n.DeviceInfo.Pfname != "" { - if err = setupVF(n, args.IfName, args.ContainerID, netns); err != nil { + err = setupVF(n, args.IfName, args.ContainerID, netns) + defer func() { + if err != nil { + if !n.DPDKMode { + err = netns.Do(func(_ ns.NetNS) error { + _, err := netlink.LinkByName(args.IfName) + return err + }) + } + if n.DPDKMode || err == nil { + releaseVF(n, args.IfName, args.ContainerID, netns) + } + } + }() + if err != nil { return fmt.Errorf("failed to set up pod interface %q from the device %q: %v", args.IfName, n.Master, err) } } else {