Skip to content

Commit

Permalink
Refactoring of some public functions
Browse files Browse the repository at this point in the history
GetVdpaDevicesByPciAddress and GetVdpaDevicesByMgmtDev have been removed
ListVdpaDevices has been refactored (now taking a filter as an input).
The filter can be something like "pci/0000:01:02, letting the function
to return all the devices matching the filter.
If the filter is empty, all the available devices will be returned.

Signed-off-by: Leonardo Milleri <[email protected]>
  • Loading branch information
Leonardo Milleri committed Sep 18, 2023
1 parent ff4e4ec commit 830f29d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 197 deletions.
24 changes: 3 additions & 21 deletions cmd/kvdpa-cli/kvdpa-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"strings"
"text/template"

vdpa "github.com/k8snetworkplumbingwg/govdpa/pkg/kvdpa"
Expand All @@ -26,26 +25,9 @@ const deviceTemplate = ` - Name: {{ .Name }}
func listAction(c *cli.Context) error {
var devs []vdpa.VdpaDevice
var err error
if c.String("mgmtdev") != "" {
var bus, name string
nameParts := strings.Split(c.String("mgmtdev"), "/")
if len(nameParts) == 1 {
name = nameParts[0]
} else if len(nameParts) == 2 {
bus = nameParts[0]
name = nameParts[1]
} else {
return fmt.Errorf("Invalid management device name %s", c.String("mgmtdev"))
}
devs, err = vdpa.GetVdpaDevicesByMgmtDev(bus, name)
if err != nil {
return err
}
} else {
devs, err = vdpa.ListVdpaDevices()
if err != nil {
fmt.Println(err)
}
devs, err = vdpa.ListVdpaDevices(c.String("mgmtdev"))
if err != nil {
fmt.Println(err)
}
tmpl := template.Must(template.New("device").Parse(deviceTemplate))

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.2.0
github.com/vishvananda/netlink v1.1.0
golang.org/x/sys v0.1.0
golang.org/x/sys v0.12.0
)

require (
Expand All @@ -17,5 +17,7 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
21 changes: 19 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJ
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
63 changes: 21 additions & 42 deletions pkg/kvdpa/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,44 +185,30 @@ func GetVdpaDevice(name string) (VdpaDevice, error) {
return nil, err
}

vdpaDevs, err := parseDevLinkVdpaDevList(msgs)
// No filters, expecting to parse attributes for the device with the given name
vdpaDevs, err := parseDevLinkVdpaDevList("", "", msgs)
if err != nil {
return nil, err
}
return vdpaDevs[0], nil
}

/*
GetVdpaDevicesByMgmtDev returns the VdpaDevice objects whose MgmtDev
has the given bus and device names.
*/
func GetVdpaDevicesByMgmtDev(busName, devName string) ([]VdpaDevice, error) {
result := []VdpaDevice{}
devices, err := ListVdpaDevices()
// ListVdpaDevices returns a list of all requested vdpa devices
// fullMgmtDeviceName (if not empty) will act as a filter
// Example1: "pci/0000:01:02" will return all the devices having bus=pci and mgmtdev=0000:01:02
// Example2: "0000:01:02" will return all the devices having mgmtdev=0000:01:02 (no bus selected)
func ListVdpaDevices(fullMgmtDeviceName string) ([]VdpaDevice, error) {
msgs, err := GetNetlinkOps().RunVdpaNetlinkCmd(VdpaCmdDevGet, syscall.NLM_F_DUMP, nil)
if err != nil {
return nil, err
}
for _, device := range devices {
if device.MgmtDev() != nil &&
device.MgmtDev().BusName() == busName &&
device.MgmtDev().DevName() == devName {
result = append(result, device)
}
}
if len(result) == 0 {
return nil, syscall.ENODEV
}
return result, nil
}

/*ListVdpaDevices returns a list of all available vdpa devices */
func ListVdpaDevices() ([]VdpaDevice, error) {
msgs, err := GetNetlinkOps().RunVdpaNetlinkCmd(VdpaCmdDevGet, syscall.NLM_F_DUMP, nil)
busName, mgmtDeviceName, err := extractBusNameAndMgmtDeviceName(fullMgmtDeviceName)
if err != nil {
return nil, err
return nil, unix.EINVAL
}

vdpaDevs, err := parseDevLinkVdpaDevList(msgs)
vdpaDevs, err := parseDevLinkVdpaDevList(busName, mgmtDeviceName, msgs)
if err != nil {
return nil, err
}
Expand All @@ -241,22 +227,6 @@ func extractBusNameAndMgmtDeviceName(fullMgmtDeviceName string) (busName string,
}
}

/*
GetVdpaDevicesByPciAddress returns the VdpaDevice objects for the given pciAddress
The pciAddress must have one of the following formats:
- MgmtBusName/MgmtDevName
- MgmtDevName
*/
func GetVdpaDevicesByPciAddress(pciAddress string) ([]VdpaDevice, error) {
busName, mgmtDeviceName, err := extractBusNameAndMgmtDeviceName(pciAddress)
if err != nil {
return nil, unix.EINVAL
}

return GetVdpaDevicesByMgmtDev(busName, mgmtDeviceName)
}

/*AddVdpaDevice adds a new vdpa device to the given management device */
func AddVdpaDevice(mgmtDeviceName string, vdpaDeviceName string) error {
if mgmtDeviceName == "" || vdpaDeviceName == "" {
Expand Down Expand Up @@ -317,7 +287,7 @@ func DeleteVdpaDevice(vdpaDeviceName string) error {
return nil
}

func parseDevLinkVdpaDevList(msgs [][]byte) ([]VdpaDevice, error) {
func parseDevLinkVdpaDevList(busName string, mgmtDeviceName string, msgs [][]byte) ([]VdpaDevice, error) {
devices := make([]VdpaDevice, 0, len(msgs))

for _, m := range msgs {
Expand All @@ -329,6 +299,15 @@ func parseDevLinkVdpaDevList(msgs [][]byte) ([]VdpaDevice, error) {
if err = dev.parseAttributes(attrs); err != nil {
return nil, err
}

if busName != "" && busName != dev.mgmtDev.busName {
continue
}

if mgmtDeviceName != "" && mgmtDeviceName != dev.mgmtDev.devName {
continue
}

if err = dev.getBusInfo(); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 830f29d

Please sign in to comment.