Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rdma: add netdev field for rdma link info #822

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions nl/rdma_link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const (
)

const (
RDMA_NLDEV_CMD_GET = 1
RDMA_NLDEV_CMD_SET = 2
RDMA_NLDEV_CMD_NEWLINK = 3
RDMA_NLDEV_CMD_DELLINK = 4
RDMA_NLDEV_CMD_SYS_GET = 6
RDMA_NLDEV_CMD_SYS_SET = 7
RDMA_NLDEV_CMD_GET = 1
RDMA_NLDEV_CMD_SET = 2
RDMA_NLDEV_CMD_NEWLINK = 3
RDMA_NLDEV_CMD_DELLINK = 4
RDMA_NLDEV_CMD_PORT_GET = 5
RDMA_NLDEV_CMD_SYS_GET = 6
RDMA_NLDEV_CMD_SYS_SET = 7
)

const (
Expand Down
61 changes: 61 additions & 0 deletions rdma_link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
// LinkAttrs represents data shared by most link types
type RdmaLinkAttrs struct {
Index uint32
PortIndex uint32
Name string
FirmwareVersion string
NodeGuid string
SysImageGuid string
Netdev string
}

// Link represents a rdma device from netlink.
Expand Down Expand Up @@ -54,6 +56,11 @@ func executeOneGetRdmaLink(data []byte) (*RdmaLink, error) {
r := bytes.NewReader(value)
binary.Read(r, nl.NativeEndian(), &Index)
link.Attrs.Index = Index
case nl.RDMA_NLDEV_ATTR_PORT_INDEX:
var Index uint32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is Index with a capital I?

r := bytes.NewReader(value)
binary.Read(r, nl.NativeEndian(), &Index)
link.Attrs.PortIndex = Index
case nl.RDMA_NLDEV_ATTR_DEV_NAME:
link.Attrs.Name = string(value[0 : len-1])
case nl.RDMA_NLDEV_ATTR_FW_VERSION:
Expand Down Expand Up @@ -106,12 +113,66 @@ func (h *Handle) RdmaLinkList() ([]*RdmaLink, error) {
if err != nil {
return nil, err
}

if err := h.getNetdev(link); err != nil {
return nil, err
}

res = append(res, link)
}

return res, nil
}

func (h *Handle) getNetdev(link *RdmaLink) error {
proto := getProtoField(nl.RDMA_NL_NLDEV, nl.RDMA_NLDEV_CMD_PORT_GET)
req := h.newNetlinkRequest(proto, unix.NLM_F_ACK)

b := make([]byte, 4)
native.PutUint32(b, uint32(link.Attrs.Index))
data := nl.NewRtAttr(nl.RDMA_NLDEV_ATTR_DEV_INDEX, b)
req.AddData(data)

b = make([]byte, 4)
native.PutUint32(b, uint32(link.Attrs.PortIndex))
data = nl.NewRtAttr(nl.RDMA_NLDEV_ATTR_PORT_INDEX, b)
req.AddData(data)

msgs, err := req.Execute(unix.NETLINK_RDMA, 0)
if err != nil {
return err
}

if len(msgs) == 1 {
netdev, err := executeOneGetRdmaNetdev(msgs[0])
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this error message provide a little ore context? for example the name of the requested device?

}
link.Attrs.Netdev = netdev

return nil
}

return fmt.Errorf("link %v not found or unexpected number of msgs", link.Attrs.Name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also print the number of msgs

[...] of msgs (%d)", link.Attrs.Name, len(msgs))

}

func executeOneGetRdmaNetdev(data []byte) (string, error) {
reader := bytes.NewReader(data)
for reader.Len() >= 4 {
_, attrType, len, value := parseNfAttrTLV(reader)

switch attrType {
case nl.RDMA_NLDEV_ATTR_NDEV_NAME:
return string(value[0 : len-1]), nil
}
if (len % 4) != 0 {
// Skip pad bytes
reader.Seek(int64(4-(len%4)), seekCurrent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to gofmt your new code

}
}
return "", fmt.Errorf("Invalid rdma netdev device")
}

// RdmaLinkByName finds a link by name and returns a pointer to the object if
// found and nil error, otherwise returns error code.
func RdmaLinkByName(name string) (*RdmaLink, error) {
Expand Down