Skip to content

Commit

Permalink
update all the needed packages with the new global imports and interf…
Browse files Browse the repository at this point in the history
…aces

Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed Nov 27, 2023
1 parent 1fd647e commit 2b6afe5
Show file tree
Hide file tree
Showing 24 changed files with 775 additions and 663 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ tags
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
#IDE (GoLand) specific
.idea/

# test-environment files
registry-login.conf
78 changes: 78 additions & 0 deletions api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
)

Expand Down Expand Up @@ -47,6 +49,8 @@ var log = logf.Log.WithName("sriovnetwork")
// Vendor ID, Physical Function Device ID, Virtual Function Device ID
var NicIDMap = []string{}

var InitialState SriovNetworkNodeState

// NetFilterType Represents the NetFilter tags to be used
type NetFilterType int

Expand Down Expand Up @@ -211,6 +215,80 @@ func GetVfDeviceID(deviceID string) string {
return ""
}

func IsSwitchdevModeSpec(spec SriovNetworkNodeStateSpec) bool {
for _, iface := range spec.Interfaces {
if iface.EswitchMode == ESwithModeSwitchDev {
return true
}
}
return false
}

func FindInterface(interfaces Interfaces, name string) (iface Interface, err error) {
for _, i := range interfaces {
if i.Name == name {
return i, nil
}
}
return Interface{}, fmt.Errorf("unable to find interface: %v", name)
}

func NeedToUpdateSriov(iface *Interface, ifaceStatus *InterfaceExt) bool {
if iface.Mtu > 0 {
mtu := iface.Mtu
if mtu != ifaceStatus.Mtu {
log.V(2).Info("NeedUpdate(): MTU needs update", "desired", mtu, "current", ifaceStatus.Mtu)
return true
}
}

if iface.NumVfs != ifaceStatus.NumVfs {
log.V(2).Info("NeedUpdate(): NumVfs needs update", "desired", iface.NumVfs, "current", ifaceStatus.NumVfs)
return true
}
if iface.NumVfs > 0 {
for _, vf := range ifaceStatus.VFs {
ingroup := false
for _, group := range iface.VfGroups {
if IndexInRange(vf.VfID, group.VfRange) {
ingroup = true
if group.DeviceType != consts.DeviceTypeNetDevice {
if group.DeviceType != vf.Driver {
log.V(2).Info("NeedUpdate(): Driver needs update",
"desired", group.DeviceType, "current", vf.Driver)
return true
}
} else {
if StringInArray(vf.Driver, vars.DpdkDrivers) {
log.V(2).Info("NeedUpdate(): Driver needs update",
"desired", group.DeviceType, "current", vf.Driver)
return true
}
if vf.Mtu != 0 && group.Mtu != 0 && vf.Mtu != group.Mtu {
log.V(2).Info("NeedUpdate(): VF MTU needs update",
"vf", vf.VfID, "desired", group.Mtu, "current", vf.Mtu)
return true
}

// this is needed to be sure the admin mac address is configured as expected
if iface.ExternallyManaged {
log.V(2).Info("NeedUpdate(): need to update the device as it's externally manage",
"device", ifaceStatus.PciAddress)
return true
}
}
break
}
}
if !ingroup && StringInArray(vf.Driver, vars.DpdkDrivers) {
// VF which has DPDK driver loaded but not in any group, needs to be reset to default driver.
return true
}
}
}
return false
}

type ByPriority []SriovNetworkNodePolicy

func (a ByPriority) Len() int {
Expand Down
2 changes: 1 addition & 1 deletion api/v1/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts"
)

var update = flag.Bool("updategolden", false, "update .golden files")
Expand Down
92 changes: 54 additions & 38 deletions cmd/sriov-network-config-daemon/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms"
plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version"

snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
)

var (
Expand All @@ -57,11 +57,16 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
setupLog.V(2).Info("sriov-config-service", "version", version.Version)

setupLog.V(0).Info("Starting sriov-config-service")

// Mark that we are running on host
vars.UsingSystemdMode = true
vars.InChroot = true

supportedNicIds, err := systemd.ReadSriovSupportedNics()
if err != nil {
setupLog.Error(err, "failed to read list of supported nic ids")
sriovResult := &systemd.SriovResult{
SyncStatus: "Failed",
SyncStatus: consts.SyncStatusFailed,
LastSyncError: fmt.Sprintf("failed to read list of supported nic ids: %v", err),
}
err = systemd.WriteSriovResult(sriovResult)
Expand All @@ -78,7 +83,7 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
if _, err := os.Stat(systemd.SriovSystemdConfigPath); !errors.Is(err, os.ErrNotExist) {
setupLog.Error(err, "failed to read the sriov configuration file", "path", systemd.SriovSystemdConfigPath)
sriovResult := &systemd.SriovResult{
SyncStatus: "Failed",
SyncStatus: consts.SyncStatusFailed,
LastSyncError: fmt.Sprintf("failed to read the sriov configuration file in path %s: %v", systemd.SriovSystemdConfigPath, err),
}
err = systemd.WriteSriovResult(sriovResult)
Expand All @@ -91,67 +96,64 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
nodeStateSpec = &systemd.SriovConfig{
Spec: sriovv1.SriovNetworkNodeStateSpec{},
UnsupportedNics: false,
PlatformType: utils.Baremetal,
PlatformType: consts.Baremetal,
}
}

setupLog.V(2).Info("sriov-config-service", "config", nodeStateSpec)

storeManager, err := utils.NewStoreManager(true)
vendorHelpers, err := plugin.NewDefaultVendorPluginHelpers()
if err != nil {
setupLog.Error(err, "failed to create store manager")
return err
setupLog.Error(err, "failed to create vendorHelpers")
return updateSriovResultErr("failed to create vendorHelpers")
}
// Load kernel modules
hostManager := host.NewHostManager(true)
_, err = hostManager.TryEnableRdma()
platformHelper, err := platforms.NewDefaultPlatformHelper()
if err != nil {
setupLog.Error(err, "failed to create platformHelpers")
return updateSriovResultErr("failed to create platformHelpers")
}

_, err = vendorHelpers.TryEnableRdma()
if err != nil {
setupLog.Error(err, "warning, failed to enable RDMA")
}
hostManager.TryEnableTun()
hostManager.TryEnableVhostNet()
vendorHelpers.TryEnableTun()
vendorHelpers.TryEnableVhostNet()

var configPlugin plugin.VendorPlugin
var ifaceStatuses []sriovv1.InterfaceExt
if nodeStateSpec.PlatformType == utils.Baremetal {
if nodeStateSpec.PlatformType == consts.Baremetal {
// Bare metal support
ifaceStatuses, err = utils.DiscoverSriovDevices(nodeStateSpec.UnsupportedNics, storeManager)
vars.DevMode = nodeStateSpec.UnsupportedNics
ifaceStatuses, err = vendorHelpers.DiscoverSriovDevices(vendorHelpers.StoreManagerInterface)
if err != nil {
setupLog.Error(err, "failed to discover sriov devices on the host")
return fmt.Errorf("sriov-config-service: failed to discover sriov devices on the host: %v", err)
return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to discover sriov devices on the host: %v", err))
}

// Create the generic plugin
configPlugin, err = generic.NewGenericPlugin(true, hostManager, storeManager)
configPlugin, err = generic.NewGenericPlugin(vendorHelpers)
if err != nil {
setupLog.Error(err, "failed to create generic plugin")
return fmt.Errorf("sriov-config-service failed to create generic plugin %v", err)
}
} else if nodeStateSpec.PlatformType == utils.VirtualOpenStack {
// Openstack support
metaData, networkData, err := utils.GetOpenstackData(false)
if err != nil {
setupLog.Error(err, "failed to read OpenStack data")
return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err)
return updateSriovResultErr(fmt.Sprintf("sriov-config-service failed to create generic plugin %v", err))
}

openStackDevicesInfo, err := utils.CreateOpenstackDevicesInfo(metaData, networkData)
} else if nodeStateSpec.PlatformType == consts.VirtualOpenStack {
err = platformHelper.CreateOpenstackDevicesInfo()
if err != nil {
setupLog.Error(err, "failed to read OpenStack data")
return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err)
return updateSriovResultErr(fmt.Sprintf("sriov-config-service failed to read OpenStack data: %v", err))
}

ifaceStatuses, err = utils.DiscoverSriovDevicesVirtual(openStackDevicesInfo)
ifaceStatuses, err = platformHelper.DiscoverSriovDevicesVirtual()
if err != nil {
setupLog.Error(err, "failed to read OpenStack data")
return fmt.Errorf("sriov-config-service: failed to read OpenStack data: %v", err)
return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to read OpenStack data: %v", err))
}

// Create the virtual plugin
configPlugin, err = virtual.NewVirtualPlugin(true)
configPlugin, err = virtual.NewVirtualPlugin(vendorHelpers)
if err != nil {
setupLog.Error(err, "failed to create virtual plugin")
return fmt.Errorf("sriov-config-service: failed to create virtual plugin %v", err)
return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to create virtual plugin %v", err))
}
}

Expand All @@ -163,18 +165,18 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
_, _, err = configPlugin.OnNodeStateChange(nodeState)
if err != nil {
setupLog.Error(err, "failed to run OnNodeStateChange to update the generic plugin status")
return fmt.Errorf("sriov-config-service: failed to run OnNodeStateChange to update the generic plugin status %v", err)
return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to run OnNodeStateChange to update the generic plugin status %v", err))
}

sriovResult := &systemd.SriovResult{
SyncStatus: "Succeeded",
SyncStatus: consts.SyncStatusSucceeded,
LastSyncError: "",
}

err = configPlugin.Apply()
if err != nil {
setupLog.Error(err, "failed to run apply node configuration")
sriovResult.SyncStatus = "Failed"
sriovResult.SyncStatus = consts.SyncStatusFailed
sriovResult.LastSyncError = err.Error()
}

Expand All @@ -187,3 +189,17 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
setupLog.V(0).Info("shutting down sriov-config-service")
return nil
}

func updateSriovResultErr(errMsg string) error {
sriovResult := &systemd.SriovResult{
SyncStatus: consts.SyncStatusFailed,
LastSyncError: errMsg,
}

err := systemd.WriteSriovResult(sriovResult)
if err != nil {
log.Log.Error(err, "failed to write sriov result file", "content", *sriovResult)
return fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err)
}
return nil
}
Loading

0 comments on commit 2b6afe5

Please sign in to comment.