forked from k8snetworkplumbingwg/sriov-cni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch includes vlan trunking for Intel and Mellanox:
(1) k8snetworkplumbingwg#149, plus (2) extension for Mellanox Signed-off-by: Jing Zhang <[email protected]>
- Loading branch information
1 parent
fca6591
commit c465ccb
Showing
16 changed files
with
865 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package factory | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/providers" | ||
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/types" | ||
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils" | ||
) | ||
|
||
const ( | ||
//IntelProviderID Intel vendor ID | ||
IntelProviderID = "0x8086" | ||
//MellanoxProviderID Mellanox vendor ID | ||
MellanoxProviderID = "0x15b3" | ||
) | ||
|
||
// GetProviderConfig get Config for specific NIC | ||
func GetProviderConfig(deviceID string) (types.VlanTrunkProviderConfig, error) { | ||
vendor, err := utils.GetVendorID(deviceID) | ||
if err != nil { | ||
return nil, fmt.Errorf("GetVendorID Error: %q", err) | ||
} | ||
|
||
switch vendor { | ||
case IntelProviderID: | ||
return providers.NewIntelTrunkProviderConfig(), nil | ||
case MellanoxProviderID: | ||
return providers.NewMellanoxTrunkProviderConfig(), nil | ||
default: | ||
return nil, fmt.Errorf("Not supported vendor: %q", vendor) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package factory | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func check(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} | ||
func TestFactory(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Factory Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
// create test sys tree | ||
err := utils.CreateTmpSysFs() | ||
check(err) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
var err error | ||
err = utils.RemoveTmpSysFs() | ||
check(err) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package factory | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Factory", func() { | ||
|
||
Context("Checking GetProviderConfig function", func() { | ||
It("Assuming existing vf", func() { | ||
_, err := GetProviderConfig("0000:af:06.1") | ||
// Expect(result).To(Equal(&providers.IntelTrunkProviderConfig{ProviderName: "Intel"})) | ||
Expect(err).NotTo(HaveOccurred(), "Existing vf should not return an error") | ||
}) | ||
It("Assuming existing vf", func() { | ||
_, err := GetProviderConfig("0000:cf:06.0") | ||
// Expect(result).To(Equal(&providers.MellanoxTrunkProviderConfig{ProviderName: "Mellanox"})) | ||
Expect(err).NotTo(HaveOccurred(), "Existing vf should not return an error, unless the vendor is not supported") | ||
}) | ||
It("Assuming not existing vf", func() { | ||
_, err := GetProviderConfig("0000:af:07.0") | ||
Expect(err).To(HaveOccurred(), "Not existing vf should return an error") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package providers | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/Masterminds/semver" | ||
|
||
sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types" | ||
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils" | ||
) | ||
|
||
var execCommand = exec.Command | ||
|
||
//IntelTrunkProviderConfig stores name of the provider | ||
type IntelTrunkProviderConfig struct { | ||
ProviderName string | ||
VlanData string | ||
} | ||
|
||
//NewIntelTrunkProviderConfig creates new Intel provider configuraton | ||
func NewIntelTrunkProviderConfig() sriovtypes.VlanTrunkProviderConfig { | ||
return &IntelTrunkProviderConfig{ | ||
ProviderName: "Intel", | ||
} | ||
} | ||
|
||
//InitConfig initializes provider configuration for given trunking ranges | ||
func (p *IntelTrunkProviderConfig) InitConfig(vlanRanges *sriovtypes.VlanTrunkRangeData) { | ||
p.GetVlanData(vlanRanges) | ||
return | ||
} | ||
|
||
//ApplyConfig applies provider configuration | ||
func (p *IntelTrunkProviderConfig) ApplyConfig(conf *sriovtypes.NetConf) error { | ||
if trunkingSupported := CheckTrunkSupport(); trunkingSupported == false { | ||
return fmt.Errorf("Vlan trunking supported only by i40e version 2.7.11 and higher, please upgrade your driver") | ||
} | ||
|
||
if err := AddVlanFiltering(p.VlanData, conf.Master, conf.VFID); err != nil { | ||
Check failure on line 44 in pkg/providers/intel.go GitHub Actions / Lint
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//RemoveConfig removes configuration | ||
func (p *IntelTrunkProviderConfig) RemoveConfig(conf *sriovtypes.NetConf) error { | ||
if err := RemoveVlanFiltering(p.VlanData, conf.Master, conf.VFID); err != nil { | ||
Check failure on line 53 in pkg/providers/intel.go GitHub Actions / Lint
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//GetVlanData converts vlanRanges.VlanTrunkRanges into string | ||
func (p *IntelTrunkProviderConfig) GetVlanData(vlanRanges *sriovtypes.VlanTrunkRangeData) { | ||
vlanData := "" | ||
var start, end string | ||
|
||
for i, vlanRange := range vlanRanges.VlanTrunkRanges { | ||
|
||
start = strconv.Itoa(int(vlanRange.Start)) | ||
end = strconv.Itoa(int(vlanRange.End)) | ||
vlanData = vlanData + start | ||
|
||
if start != end { | ||
vlanData = vlanData + "-" + end | ||
} | ||
if i < len(vlanRanges.VlanTrunkRanges)-1 { | ||
vlanData = vlanData + "," | ||
} | ||
|
||
} | ||
p.VlanData = vlanData | ||
return | ||
} | ||
|
||
//AddVlanFiltering writes "add [trunking ranges]" to trunk file | ||
func AddVlanFiltering(vlanData, pfName string, vfid int) error { | ||
addTrunk := "add " + vlanData | ||
trunkFile := fmt.Sprintf(utils.TrunkFileDirectory, pfName, vfid) | ||
|
||
errwrite := ioutil.WriteFile(trunkFile, []byte(addTrunk), 0644) | ||
Check failure on line 88 in pkg/providers/intel.go GitHub Actions / Lint
|
||
if errwrite != nil { | ||
return fmt.Errorf("f.Write: %q", errwrite) | ||
} | ||
|
||
infraInterfaces, infraVlans, err := utils.GetInfraVlanData() | ||
if err == nil { | ||
for _, pf := range infraInterfaces { | ||
if pf == pfName { | ||
vlanData := "" | ||
for _, vlan := range infraVlans { | ||
if vlanData == "" { | ||
vlanData = fmt.Sprintf("%d", vlan) | ||
} else { | ||
vlanData = fmt.Sprintf("%s,%d", vlanData, vlan) | ||
} | ||
} | ||
if vlanData != "" { | ||
removeTrunk := "rem " + vlanData | ||
errwrite := ioutil.WriteFile(trunkFile, []byte(removeTrunk), 0644) | ||
Check failure on line 107 in pkg/providers/intel.go GitHub Actions / Lint
|
||
if errwrite != nil { | ||
return fmt.Errorf("f.Write: %q", errwrite) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//RemoveVlanFiltering writes "rem [trunking ranges]" to trunk file | ||
func RemoveVlanFiltering(vlanData, pfName string, vfid int) error { | ||
removeTrunk := "rem " + vlanData | ||
trunkFile := fmt.Sprintf(utils.TrunkFileDirectory, pfName, vfid) | ||
|
||
errwrite := ioutil.WriteFile(trunkFile, []byte(removeTrunk), 0644) | ||
Check failure on line 124 in pkg/providers/intel.go GitHub Actions / Lint
|
||
if errwrite != nil { | ||
return fmt.Errorf("f.Write: %q", errwrite) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CheckTrunkSupport checks installed driver version; trunking is supported for version 2.7.11 and higher | ||
func CheckTrunkSupport() bool { | ||
var stdout bytes.Buffer | ||
modinfoCmd := "modinfo -F version i40e" | ||
cmd := execCommand("sh", "-c", modinfoCmd) | ||
cmd.Stdout = &stdout | ||
|
||
if err := cmd.Run(); err != nil { | ||
fmt.Printf("modinfo returned error: %v %s", err, stdout.String()) | ||
return false | ||
} | ||
|
||
stdoutSplit := strings.Split(stdout.String(), "\n") | ||
if len(stdoutSplit) == 0 { | ||
fmt.Printf("unexpected output after parsing driver version: %s", stdout.String()) | ||
return false | ||
} | ||
driverVersion := stdoutSplit[0] | ||
numDots := strings.Count(driverVersion, ".") | ||
if numDots < 2 { | ||
fmt.Printf("unexpected driver version: %s", driverVersion) | ||
return false | ||
} | ||
//truncate driver version to only major.minor.patch version format length to ensure semver compatibility | ||
if numDots > 2 { | ||
truncVersion := strings.Split(driverVersion, ".")[:3] | ||
driverVersion = strings.Join(truncVersion, ".") | ||
} | ||
|
||
v1, _ := semver.NewVersion("2.7.11") | ||
v2, err := semver.NewVersion(driverVersion) | ||
if err != nil { | ||
fmt.Printf("invalid version error: %v %s", err, driverVersion) | ||
return false | ||
} | ||
|
||
if v2.Compare(v1) >= 0 { | ||
return true | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.