Add devlink command to get port by index

Add a command to get information about a specific devlink port
referenced by device name and port index(bus, device, port index).

Signed-off-by: Parav Pandit <parav@mellanox.com>
This commit is contained in:
Parav Pandit 2020-06-15 12:32:49 +03:00 committed by Alessandro Boch
parent ff8f17bc19
commit e440572571
2 changed files with 54 additions and 0 deletions

View File

@ -353,3 +353,41 @@ func (h *Handle) DevLinkGetAllPortList() ([]*DevlinkPort, error) {
func DevLinkGetAllPortList() ([]*DevlinkPort, error) { func DevLinkGetAllPortList() ([]*DevlinkPort, error) {
return pkgHandle.DevLinkGetAllPortList() return pkgHandle.DevLinkGetAllPortList()
} }
func parseDevlinkPortMsg(msgs [][]byte) (*DevlinkPort, error) {
m := msgs[0]
attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:])
if err != nil {
return nil, err
}
port := &DevlinkPort{}
if err = port.parseAttributes(attrs); err != nil {
return nil, err
}
return port, nil
}
// DevLinkGetPortByIndexprovides a pointer to devlink device and nil error,
// otherwise returns an error code.
func (h *Handle) DevLinkGetPortByIndex(Bus string, Device string, PortIndex uint32) (*DevlinkPort, error) {
_, req, err := h.createCmdReq(nl.DEVLINK_CMD_PORT_GET, Bus, Device)
if err != nil {
return nil, err
}
req.AddData(nl.NewRtAttr(nl.DEVLINK_ATTR_PORT_INDEX, nl.Uint32Attr(PortIndex)))
respmsg, err := req.Execute(unix.NETLINK_GENERIC, 0)
if err != nil {
return nil, err
}
port, err := parseDevlinkPortMsg(respmsg)
return port, err
}
// DevLinkGetPortByIndex provides a pointer to devlink portand nil error,
// otherwise returns an error code.
func DevLinkGetPortByIndex(Bus string, Device string, PortIndex uint32) (*DevlinkPort, error) {
return pkgHandle.DevLinkGetPortByIndex(Bus, Device, PortIndex)
}

View File

@ -52,3 +52,19 @@ func TestDevLinkGetAllPortList(t *testing.T) {
t.Log(*port) t.Log(*port)
} }
} }
func TestDevLinkGetPortByIndex(t *testing.T) {
minKernelRequired(t, 5, 4)
ports, err := DevLinkGetAllPortList()
if err != nil {
t.Fatal(err)
}
t.Log("devlink port count = ", len(ports))
for _, port := range ports {
p, err2 := DevLinkGetPortByIndex(port.BusName, port.DeviceName, port.PortIndex)
if err2 != nil {
t.Fatal(err)
}
t.Log(*p)
}
}