Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Refactor cdq into pkg (#76)
Browse files Browse the repository at this point in the history
* Refactor SubFunction code into public package for usability outside this project

* Inlcude wrapper comments for subfunctions API calls
  • Loading branch information
davecremins authored and garyloug committed Jan 12, 2023
1 parent c30d397 commit a78309c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
8 changes: 4 additions & 4 deletions internal/deviceplugin/poolManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func (pm *PoolManager) Init(config PoolConfig) error {
pm.BpfHandler = bpf.NewHandler()
pm.NetHandler = networking.NewHandler()

if err := pm.startGRPC(); err != nil {
return err
}
logging.Infof("Pool "+pm.DevicePrefix+"/%s started serving", pm.Name)
if err := pm.startGRPC(); err != nil {
return err
}
logging.Infof("Pool "+pm.DevicePrefix+"/%s started serving", pm.Name)

if err := pm.registerWithKubelet(); err != nil {
return err
Expand Down
60 changes: 51 additions & 9 deletions internal/networking/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/intel/afxdp-plugins-for-kubernetes/constants"
"github.com/intel/afxdp-plugins-for-kubernetes/internal/tools"
logging "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"io/ioutil"
"net"
"os"
"path/filepath"

"github.com/intel/afxdp-plugins-for-kubernetes/constants"
"github.com/intel/afxdp-plugins-for-kubernetes/internal/tools"
"github.com/intel/afxdp-plugins-for-kubernetes/pkg/subfunctions"
logging "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)

var (
Expand All @@ -53,11 +55,11 @@ type Handler interface {
NetDevExists(device string) (bool, error)
GetDeviceFromFile(deviceName string, filepath string) (*Device, error)
WriteDeviceFile(device *Device, filepath string) error
CreateCdqSubfunction(parentPci string, sfnum string) error // see cdq.go
DeleteCdqSubfunction(portIndex string) error // see cdq.go
IsCdqSubfunction(name string) (bool, error) // see cdq.go
NumAvailableCdqSubfunctions(interfaceName string) (int, error) // see cdq.go
GetCdqPortIndex(netdev string) (string, error) // see cdq.go
CreateCdqSubfunction(parentPci string, sfnum string) error // see subfunction package
DeleteCdqSubfunction(portIndex string) error // see subfunction package
IsCdqSubfunction(name string) (bool, error) // see subfunction package
NumAvailableCdqSubfunctions(interfaceName string) (int, error) // see subfunction package
GetCdqPortIndex(netdev string) (string, error) // see subfucntions package
SetEthtool(ethtoolCmd []string, interfaceName string, ipResult string) error // see ethtool.go
DeleteEthtool(interfaceName string) error // see ethtool.go
IsPhysicalPort(name string) (bool, error)
Expand Down Expand Up @@ -347,6 +349,46 @@ func (r *handler) IsPhysicalPort(name string) (bool, error) {
}
}

/*
Wrapper for Subfunctions API calls
*/
func (r *handler) CreateCdqSubfunction(parentPci string, sfnum string) error {
err := subfunctions.CreateCdqSubfunction(parentPci, sfnum)
return err
}

/*
Wrapper for Subfunctions API calls
*/
func (r *handler) DeleteCdqSubfunction(portIndex string) error {
err := subfunctions.DeleteCdqSubfunction(portIndex)
return err
}

/*
Wrapper for Subfunctions API calls
*/
func (r *handler) IsCdqSubfunction(name string) (bool, error) {
result, err := subfunctions.IsCdqSubfunction(name)
return result, err
}

/*
Wrapper for Subfunctions API calls
*/
func (r *handler) NumAvailableCdqSubfunctions(interfaceName string) (int, error) {
result, err := subfunctions.NumAvailableCdqSubfunctions(interfaceName)
return result, err
}

/*
Wrapper for Subfunctions API calls
*/
func (r *handler) GetCdqPortIndex(netdev string) (string, error) {
result, err := subfunctions.GetCdqPortIndex(netdev)
return result, err
}

/*
readDevice reads the device file unmarshalls device information to
a device map object
Expand Down
17 changes: 9 additions & 8 deletions internal/networking/cdq.go → pkg/subfunctions/cdq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
* limitations under the License.
*/

package networking
package subfunctions

import (
"fmt"
logging "github.com/sirupsen/logrus"
"os/exec"
"strconv"
"strings"

logging "github.com/sirupsen/logrus"
)

/*
CreateCdqSubfunction takes the PCI address of a port and a subfunction number
It creates that subfunction on top of that port and activates it
*/
func (r *handler) CreateCdqSubfunction(parentPci string, sfnum string) error {
func CreateCdqSubfunction(parentPci string, sfnum string) error {
app := "devlink"
args := []string{"port", "add", "pci/" + parentPci, "flavour", "pcisf", "pfnum", "0", "sfnum", sfnum}

Expand All @@ -52,7 +53,7 @@ func (r *handler) CreateCdqSubfunction(parentPci string, sfnum string) error {
/*
DeleteCdqSubfunction takes the port index of a subfunction, deactivates and deletes it
*/
func (r *handler) DeleteCdqSubfunction(portIndex string) error {
func DeleteCdqSubfunction(portIndex string) error {
app := "devlink"
args := []string{"port", "function", "set", "pci/" + portIndex, "state", "inactive"}

Expand All @@ -76,8 +77,8 @@ func (r *handler) DeleteCdqSubfunction(portIndex string) error {
/*
IsCdqSubfunction takes a netdev name and returns true if is a CDQ subfunction.
*/
func (r *handler) IsCdqSubfunction(name string) (bool, error) {
portIndex, err := r.GetCdqPortIndex(name)
func IsCdqSubfunction(name string) (bool, error) {
portIndex, err := GetCdqPortIndex(name)
if err != nil {
return false, err
}
Expand All @@ -96,7 +97,7 @@ GetCdqPortIndex takes a netdev name and returns the port index (pci/sfnum)
Note this function only works on physical devices and CDQ subfunctions
Other netdevs will return a "device not found by devlink" error
*/
func (r *handler) GetCdqPortIndex(netdev string) (string, error) {
func GetCdqPortIndex(netdev string) (string, error) {
devlinkList := "devlink port list | grep " + `"\b` + netdev + `\b"`

devList, err := exec.Command("sh", "-c", devlinkList).CombinedOutput()
Expand Down Expand Up @@ -125,7 +126,7 @@ func (r *handler) GetCdqPortIndex(netdev string) (string, error) {
NumAvailableCdqSubfunctions takes the PCI of a physical port and returns how
many unused CDQ subfunctions are available
*/
func (r *handler) NumAvailableCdqSubfunctions(pci string) (int, error) {
func NumAvailableCdqSubfunctions(pci string) (int, error) {
app := "devlink"
args := []string{"resource", "show", "pci/" + pci}

Expand Down

0 comments on commit a78309c

Please sign in to comment.