diff --git a/nl/rdma_link_linux.go b/nl/rdma_link_linux.go index 2ad3b2e..47d736c 100644 --- a/nl/rdma_link_linux.go +++ b/nl/rdma_link_linux.go @@ -10,6 +10,7 @@ const ( const ( RDMA_NLDEV_CMD_GET = 1 + RDMA_NLDEV_CMD_SET = 2 ) const ( diff --git a/rdma_link_linux.go b/rdma_link_linux.go index 2d1685a..e7d34dd 100644 --- a/rdma_link_linux.go +++ b/rdma_link_linux.go @@ -95,6 +95,12 @@ func execRdmaGetLink(req *nl.NetlinkRequest, name string) (*RdmaLink, error) { return nil, fmt.Errorf("Rdma device %v not found", name) } +func execRdmaSetLink(req *nl.NetlinkRequest) error { + + _, err := req.Execute(unix.NETLINK_RDMA, 0) + return err +} + // 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) { @@ -110,3 +116,30 @@ func (h *Handle) RdmaLinkByName(name string) (*RdmaLink, error) { return execRdmaGetLink(req, name) } + +// RdmaLinkSetName sets the name of the rdma link device. Return nil on success +// or error otherwise. +// Equivalent to: `rdma dev set $old_devname name $name` +func RdmaLinkSetName(link *RdmaLink, name string) error { + return pkgHandle.RdmaLinkSetName(link, name) +} + +// RdmaLinkSetName sets the name of the rdma link device. Return nil on success +// or error otherwise. +// Equivalent to: `rdma dev set $old_devname name $name` +func (h *Handle) RdmaLinkSetName(link *RdmaLink, name string) error { + proto := getProtoField(nl.RDMA_NL_NLDEV, nl.RDMA_NLDEV_CMD_SET) + 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, len(name)+1) + copy(b, name) + data = nl.NewRtAttr(nl.RDMA_NLDEV_ATTR_DEV_NAME, b) + req.AddData(data) + + return execRdmaSetLink(req) +} diff --git a/rdma_link_test.go b/rdma_link_test.go index f1fa25f..daba358 100644 --- a/rdma_link_test.go +++ b/rdma_link_test.go @@ -32,3 +32,22 @@ func TestRdmaGetRdmaLink(t *testing.T) { t.Fatal(err) } } + +func TestRdmaSetRdmaLinkName(t *testing.T) { + minKernelRequired(t, 4, 19) + setupRdmaKModule(t, "ib_core") + link, err := RdmaLinkByName("foo") + if err != nil { + t.Fatal(err) + } + // Set new name + err = RdmaLinkSetName(link, "bar") + if err != nil { + t.Fatal(err) + } + // Revert back to old name + err = RdmaLinkSetName(link, "foo") + if err != nil { + t.Fatal(err) + } +}