Skip to content

Commit

Permalink
GetVdpaDevicesByPciAddress refactoring
Browse files Browse the repository at this point in the history
GetVdpaDevicesByPciAddress has been refactored.
Instead of reading and parsing all the results from netlink
for all the vDPA devices, the function is now finding the
vDPA device name corresponding to a given PCI address
and making a more selective request down to netlink.

Signed-off-by: Leonardo Milleri <[email protected]>
  • Loading branch information
Leonardo Milleri committed Aug 23, 2023
1 parent ce80166 commit 1a135be
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 5 deletions.
25 changes: 24 additions & 1 deletion cmd/kvdpa-cli/kvdpa-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func listAction(c *cli.Context) error {
bus = nameParts[0]
name = nameParts[1]
} else {
return fmt.Errorf("Invalid management device name %s", c.String("mgmtdev"))
return fmt.Errorf("invalid management device name %s", c.String("mgmtdev"))
}
devs, err = vdpa.GetVdpaDevicesByMgmtDev(bus, name)
if err != nil {
Expand Down Expand Up @@ -72,6 +72,24 @@ func getAction(c *cli.Context) error {
return nil
}

func getFromPciAction(c *cli.Context) error {
for i := 0; i < c.Args().Len(); i++ {
pciAddress := c.Args().Get(i)
devs, err := vdpa.GetVdpaDevicesByPciAddress(pciAddress)
if err != nil {
return err
}
tmpl := template.Must(template.New("device").Parse(deviceTemplate))

for _, dev := range devs {
if err := tmpl.Execute(os.Stdout, dev); err != nil {
panic(err)
}
}
}
return nil
}

func addAction(c *cli.Context) error {
if c.Args().Len() != 2 {
err := cli.ShowAppHelp(c)
Expand Down Expand Up @@ -110,6 +128,11 @@ func main() {
},
},
},
{Name: "get-from-pci",
Usage: "Get vdpa devices from PCI address",
Action: getFromPciAction,
ArgsUsage: "[pci-address]",
},
{Name: "get",
Usage: "Get a specific vdpa device",
Action: getAction,
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.0.0-20190606203320-7fc4e5ec1444
golang.org/x/sys v0.11.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.12.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
18 changes: 17 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,24 @@ 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/sys v0.0.0-20190606203320-7fc4e5ec1444 h1:/d2cWp6PSamH4jDPFLyO150psQdqvtoNX8Zjg3AQ31g=
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.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.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.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
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
42 changes: 40 additions & 2 deletions pkg/kvdpa/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func extractBusNameAndMgmtDeviceName(fullMgmtDeviceName string) (busName string,
}

/*
GetVdpaDevicesByPciAddress returns the VdpaDevice objects for the given pciAddress
GetVdpaDevicesByPciAddress returns the VdpaDevice devices for the given pciAddress
The pciAddress must have one of the following formats:
- MgmtBusName/MgmtDevName
Expand All @@ -254,7 +254,27 @@ func GetVdpaDevicesByPciAddress(pciAddress string) ([]VdpaDevice, error) {
return nil, unix.EINVAL
}

return GetVdpaDevicesByMgmtDev(busName, mgmtDeviceName)
msgs, err := GetNetlinkOps().RunVdpaNetlinkCmd(VdpaCmdDevGet, syscall.NLM_F_DUMP, nil)
if err != nil {
return nil, err
}

vdpaNames, err := parseDevLinkGetVdpaName(busName, mgmtDeviceName, msgs)
if err != nil {
return nil, err
}

devices := make([]VdpaDevice, 0, len(vdpaNames))

for _, vdpaName := range vdpaNames {
vdpaDevice, err := GetVdpaDevice(vdpaName)
if err != nil {
return nil, err
}
devices = append(devices, vdpaDevice)
}

return devices, nil
}

/*AddVdpaDevice adds a new vdpa device to the given management device */
Expand Down Expand Up @@ -336,3 +356,21 @@ func parseDevLinkVdpaDevList(msgs [][]byte) ([]VdpaDevice, error) {
}
return devices, nil
}

func parseDevLinkGetVdpaName(mgmtBus string, mgmtDev string, msgs [][]byte) ([]string, error) {
vdpaNames := make([]string, 0, len(msgs))
for _, m := range msgs {
attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:])
if err != nil {
return nil, err
}
dev := &vdpaDev{}
if err = dev.parseAttributes(attrs); err != nil {
return nil, err
}
if dev.MgmtDev().BusName() == mgmtBus && dev.MgmtDev().DevName() == mgmtDev {
vdpaNames = append(vdpaNames, dev.Name())
}
}
return vdpaNames, nil
}
69 changes: 69 additions & 0 deletions pkg/kvdpa/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kvdpa

import (
"fmt"
"path/filepath"
"syscall"
"testing"

Expand Down Expand Up @@ -331,3 +332,71 @@ func TestVdpaDevGetByMgmt(t *testing.T) {
})
}
}

func TestVdpaDevGetByPciAddress(t *testing.T) {

listResult := []VdpaDevice{
&vdpaDev{
name: "vdpa1",
mgmtDev: &mgmtDev{
busName: "pci",
devName: "0000:01:02",
},
},
}

tests := []struct {
name string
err error
response []VdpaDevice
mgmtDevName string
mgmtBusName string
vdpaName string
}{
{
name: "PCI Address",

response: []VdpaDevice{
&vdpaDev{
name: "vdpa1",
mgmtDev: &mgmtDev{
busName: "pci",
devName: "0000:01:02",
},
},
},
mgmtDevName: "0000:01:02",
mgmtBusName: "pci",
vdpaName: "vdpa1",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s_%s", "TestDevGetByMgmt", tt.name), func(t *testing.T) {
netLinkMock := &mocks.NetlinkOps{}
SetNetlinkOps(netLinkMock)
netLinkMock.On("RunVdpaNetlinkCmd",
VdpaCmdDevGet,
mock.MatchedBy(func(flags int) bool {
return (flags|syscall.NLM_F_DUMP != 0)
}),
mock.AnythingOfType("[]*nl.RtAttr")).
Return(vdpaDevToNlMessage(t, listResult...), nil)

netLinkMock.On("NewAttribute",
VdpaAttrDevName,
tt.vdpaName,
mock.MatchedBy(func(data interface{}) bool {
_, ok := data.(string)
return ok
})).Return(&nl.RtAttr{}, nil)

devs, err := GetVdpaDevicesByPciAddress(filepath.Join(tt.mgmtBusName, tt.mgmtDevName))
if tt.err != nil {
assert.Equal(t, tt.err, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.response, devs)
}
})
}
}

0 comments on commit 1a135be

Please sign in to comment.