Skip to content

Commit

Permalink
Add helper funcs to retrieve eswitch mode from interface spec and status
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Kulazhenkov <ykulazhenkov@nvidia.com>
  • Loading branch information
ykulazhenkov committed Feb 23, 2024
1 parent eb2cc96 commit 35997a4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ func FindInterface(interfaces Interfaces, name string) (iface Interface, err err
return Interface{}, fmt.Errorf("unable to find interface: %v", name)
}

// GetEswitchModeFromSpec returns ESwitchMode from the interface spec, returns legacy if not set
func GetEswitchModeFromSpec(ifaceSpec *Interface) string {
if ifaceSpec.EswitchMode == "" {
return ESwithModeLegacy
}
return ifaceSpec.EswitchMode
}

// GetEswitchModeFromStatus returns ESwitchMode from the interface status, returns legacy if not set
func GetEswitchModeFromStatus(ifaceStatus *InterfaceExt) string {
if ifaceStatus.EswitchMode == "" {
return ESwithModeLegacy
}
return ifaceStatus.EswitchMode
}

func NeedToUpdateSriov(ifaceSpec *Interface, ifaceStatus *InterfaceExt) bool {
if ifaceSpec.Mtu > 0 {
mtu := ifaceSpec.Mtu
Expand Down
64 changes: 64 additions & 0 deletions api/v1/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,67 @@ func TestVhostVdpaNodePolicyApply(t *testing.T) {
})
}
}

func TestGetEswitchModeFromSpec(t *testing.T) {
testtable := []struct {
tname string
spec *v1.Interface
expectedResult string
}{
{
tname: "set to legacy",
spec: &v1.Interface{EswitchMode: v1.ESwithModeLegacy},
expectedResult: v1.ESwithModeLegacy,
},
{
tname: "set to switchdev",
spec: &v1.Interface{EswitchMode: v1.ESwithModeSwitchDev},
expectedResult: v1.ESwithModeSwitchDev,
},
{
tname: "not set",
spec: &v1.Interface{},
expectedResult: v1.ESwithModeLegacy,
},
}
for _, tc := range testtable {
t.Run(tc.tname, func(t *testing.T) {
result := v1.GetEswitchModeFromSpec(tc.spec)
if diff := cmp.Diff(tc.expectedResult, result); diff != "" {
t.Errorf("unexpected result (-want +got):\n%s", diff)
}
})
}
}

func TestGetEswitchModeFromStatus(t *testing.T) {
testtable := []struct {
tname string
spec *v1.InterfaceExt
expectedResult string
}{
{
tname: "set to legacy",
spec: &v1.InterfaceExt{EswitchMode: v1.ESwithModeLegacy},
expectedResult: v1.ESwithModeLegacy,
},
{
tname: "set to switchdev",
spec: &v1.InterfaceExt{EswitchMode: v1.ESwithModeSwitchDev},
expectedResult: v1.ESwithModeSwitchDev,
},
{
tname: "not set",
spec: &v1.InterfaceExt{},
expectedResult: v1.ESwithModeLegacy,
},
}
for _, tc := range testtable {
t.Run(tc.tname, func(t *testing.T) {
result := v1.GetEswitchModeFromStatus(tc.spec)
if diff := cmp.Diff(tc.expectedResult, result); diff != "" {
t.Errorf("unexpected result (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 35997a4

Please sign in to comment.