Skip to content

Commit 5f8645a

Browse files
committed
Check SR-IOV VF before validating VF MTU, simplify unit test
Signed-off-by: Masaharu Kanda <kanlkan.naklnak@gmail.com>
1 parent b4c5e84 commit 5f8645a

3 files changed

Lines changed: 20 additions & 27 deletions

File tree

pkg/driver/dra_hooks.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,23 +278,21 @@ func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resour
278278
// For SR-IOV VFs, the requested MTU must not exceed the parent PF's MTU.
279279
// Otherwise the claim is rejected so the Pod fails fast instead of being
280280
// created with an illegal MTU configuration.
281-
if deviceCfg.NetworkInterfaceConfigInPod.Interface.MTU != nil {
281+
if deviceCfg.NetworkInterfaceConfigInPod.Interface.MTU != nil && inventory.IsSriovVf(ifName) {
282282
pfName, err := inventory.GetPFInterfaceName(ifName)
283283
if err != nil {
284-
// Not an SR-IOV VF, or the parent PF cannot be determined. This is
285-
// expected for regular interfaces, so skip the check and only log it.
286-
klog.V(4).Infof("skipping SR-IOV VF MTU check for interface %s: %v", ifName, err)
287-
} else {
288-
pfLink, err := nlHandle.LinkByName(pfName)
289-
if err != nil {
290-
errorList = append(errorList, fmt.Errorf("failed to get netlink to parent PF %s of VF %s: %v", pfName, ifName, err))
291-
continue
292-
}
293-
requestedMTU := int(*deviceCfg.NetworkInterfaceConfigInPod.Interface.MTU)
294-
if err := validateVFMTU(ifName, pfName, requestedMTU, pfLink.Attrs().MTU); err != nil {
295-
errorList = append(errorList, err)
296-
continue
297-
}
284+
errorList = append(errorList, fmt.Errorf("failed to determine parent PF for SR-IOV VF %s: %v", ifName, err))
285+
continue
286+
}
287+
pfLink, err := nlHandle.LinkByName(pfName)
288+
if err != nil {
289+
errorList = append(errorList, fmt.Errorf("failed to get netlink to parent PF %s of VF %s: %v", pfName, ifName, err))
290+
continue
291+
}
292+
requestedMTU := int(*deviceCfg.NetworkInterfaceConfigInPod.Interface.MTU)
293+
if err := validateVFMTU(ifName, pfName, requestedMTU, pfLink.Attrs().MTU); err != nil {
294+
errorList = append(errorList, err)
295+
continue
298296
}
299297
}
300298

pkg/driver/dra_hooks_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,8 @@ func TestValidateVFMTU(t *testing.T) {
307307
for _, tc := range testCases {
308308
t.Run(tc.name, func(t *testing.T) {
309309
err := validateVFMTU("eth1", "eth0", tc.requestedMTU, tc.pfMTU)
310-
if tc.wantErr {
311-
if err == nil {
312-
t.Fatalf("validateVFMTU() expected error, got nil")
313-
}
314-
want := fmt.Sprintf("requested MTU %d for SR-IOV VF eth1 exceeds parent PF eth0 MTU %d", tc.requestedMTU, tc.pfMTU)
315-
if err.Error() != want {
316-
t.Errorf("validateVFMTU() error = %q, want %q", err.Error(), want)
317-
}
318-
return
319-
}
320-
if err != nil {
321-
t.Errorf("validateVFMTU() unexpected error: %v", err)
310+
if (err != nil) != tc.wantErr {
311+
t.Errorf("validateVFMTU() error = %v, wantErr %v", err, tc.wantErr)
322312
}
323313
})
324314
}

pkg/inventory/sysfs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ func isSriovVf(name string, syspath string) bool {
112112
return info.Mode()&os.ModeSymlink != 0
113113
}
114114

115+
// IsSriovVf reports whether a network interface is a SR-IOV Virtual Function.
116+
func IsSriovVf(name string) bool {
117+
return isSriovVf(name, sysnetPath)
118+
}
119+
115120
// getPFInterfaceNameFromSysfs returns the name of the Physical Function (PF) network
116121
// interface for a given SR-IOV Virtual Function (VF) interface, using basePath as the
117122
// root of the sysfs net directory (e.g. /sys/class/net). It returns an error if the

0 commit comments

Comments
 (0)