Skip to content

Commit

Permalink
support RDMA devices
Browse files Browse the repository at this point in the history
Change-Id: I753e1a4ec641eccad9b2970d6318786091ad5772
  • Loading branch information
aojea committed Aug 26, 2024
1 parent cfb67f9 commit 80c45b6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/dra/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ func (np *NetworkPlugin) RunPodSandbox(_ context.Context, pod *api.PodSandbox) e
klog.Infof("RunPodSandbox error moving device %s to namespace %s: %v", result.Device, ns, err)
return err
}
rdmaDev, err := rdmamap.GetRdmaDeviceForNetdevice(result.Device)
if err != nil {
klog.Infof("RunPodSandbox error getting RDMA device %s to namespace %s: %v", result.Device, ns, err)
continue
}
// TODO signal this via DRA
if rdmaDev != "" {
err = hostdevice.MoveRDMALinkIn(rdmaDev, ns)
if err != nil {
klog.Infof("RunPodSandbox error getting RDMA device %s to namespace %s: %v", result.Device, ns, err)
continue
}
}
}
return nil
}
Expand Down Expand Up @@ -234,6 +247,18 @@ func (np *NetworkPlugin) StopPodSandbox(ctx context.Context, pod *api.PodSandbox
klog.V(2).Infof("StopPodSandbox pod %s/%s failed to deallocate interface", pod.Namespace, pod.Name)
return nil
}
rdmaDev, err := rdmamap.GetRdmaDeviceForNetdevice(result.Device)
if err != nil {
klog.Infof("RunPodSandbox error getting RDMA device %s to namespace %s: %v", result.Device, ns, err)
continue
}
if rdmaDev != "" {
err = hostdevice.MoveRDMALinkIn(rdmaDev, ns)
if err != nil {
klog.Infof("RunPodSandbox error getting RDMA device %s to namespace %s: %v", result.Device, ns, err)
continue
}
}
}
return nil
}
Expand Down
58 changes: 58 additions & 0 deletions pkg/hostdevice/rdmadevice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package hostdevice

import (
"fmt"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"
)

// Based on existing RDMA CNI plugin
// https://github.com/k8snetworkplumbingwg/rdma-cni

func MoveRDMALinkIn(hostIfName string, containerNsPAth string) error {
containerNs, err := ns.GetNS(containerNsPAth)
if err != nil {
return err
}
hostDev, err := netlink.RdmaLinkByName(hostIfName)
if err != nil {
return err
}

if err = netlink.RdmaLinkSetNsFd(hostDev, uint32(containerNs.Fd())); err != nil {
return fmt.Errorf("failed to move %q to container ns: %v", hostDev.Attrs.Name, err)
}

return nil
}

func MoveRDMALinkOut(containerNsPAth string, ifName string) error {
containerNs, err := ns.GetNS(containerNsPAth)
if err != nil {
return err
}
defaultNs, err := ns.GetCurrentNS()
if err != nil {
return err
}
defer defaultNs.Close()

err = containerNs.Do(func(_ ns.NetNS) error {
dev, err := netlink.RdmaLinkByName(ifName)
if err != nil {
return fmt.Errorf("failed to find %q: %v", ifName, err)
}

if err = netlink.RdmaLinkSetNsFd(dev, uint32(defaultNs.Fd())); err != nil {
return fmt.Errorf("failed to move %q to host netns: %v", dev.Attrs.Name, err)
}
return nil
})

if err != nil {
return err
}

return nil
}

0 comments on commit 80c45b6

Please sign in to comment.