From 78bcd5a1585525c6c256109e44797c305676e8ca Mon Sep 17 00:00:00 2001 From: Milad Ebrahimi Date: Wed, 9 Sep 2026 15:33:18 +0200 Subject: [PATCH 1/2] feat: add rdma enabled for netword devices for matched devices --- README.md | 7 ++ cmd/udev-manager/config_test.go | 38 +++++++ cmd/udev-manager/main.go | 24 +++- internal/plugin/net_rdma.go | 117 +++++++++++++++++++- internal/plugin/net_rdma_test.go | 183 +++++++++++++++++++++++++++++++ internal/plugin/scatter.go | 8 ++ internal/plugin/scatter_test.go | 23 ++++ internal/udev/udev.go | 3 + 8 files changed, 394 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f0d6bf9..04804d2 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,15 @@ Exposes RDMA character devices for a network interface. networkRdma: - matcher: '(^ib0$)' resourceCount: 4 + deviceType: vf # optional: vf or pf ``` +Only interfaces with an RDMA-device mapping are exposed. `deviceType: vf` +requires the interface's PCI function to have a `physfn` attribute; +`deviceType: pf` requires a positive `sriov_totalvfs` value. Omitting +`deviceType` disables PF/VF filtering. The `pf` and `vf` values are +case-insensitive. + ### Numa affinity Exposes statically-configured resources that exist purely to fake NUMA affinity. Unlike the other types these resources are **not** backed by udev: the devices are created directly from configuration and are always healthy. Each device advertises a NUMA topology hint for the configured node, so the kubelet topology manager co-locates a requesting pod with that node. Allocating one mounts nothing and sets no env vars. diff --git a/cmd/udev-manager/config_test.go b/cmd/udev-manager/config_test.go index 0a24a41..a0248b4 100644 --- a/cmd/udev-manager/config_test.go +++ b/cmd/udev-manager/config_test.go @@ -5,6 +5,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + + "github.com/ydb-platform/udev-manager/internal/plugin" ) // parseYAML is a test helper that parses a YAML string into a appConfig. @@ -217,6 +219,30 @@ var _ = Describe("netRdmaConfig.validate", func() { nc := &netRdmaConfig{Matcher: `[`} Expect(nc.validate()).To(MatchError(ContainSubstring(".matcher"))) }) + + DescribeTable("accepts supported device types", + func(deviceType, expected plugin.NetRdmaDeviceType) { + nc := &netRdmaConfig{Matcher: `ib.*`, DeviceType: deviceType} + Expect(nc.validate()).NotTo(HaveOccurred()) + Expect(nc.DeviceType).To(Equal(expected)) + }, + Entry("omitted", plugin.NetRdmaDeviceTypeAny, plugin.NetRdmaDeviceTypeAny), + Entry("lowercase PF", plugin.NetRdmaDeviceTypePF, plugin.NetRdmaDeviceTypePF), + Entry("uppercase PF", plugin.NetRdmaDeviceType("PF"), plugin.NetRdmaDeviceTypePF), + Entry("mixed-case PF", plugin.NetRdmaDeviceType("pF"), plugin.NetRdmaDeviceTypePF), + Entry("lowercase VF", plugin.NetRdmaDeviceTypeVF, plugin.NetRdmaDeviceTypeVF), + Entry("uppercase VF", plugin.NetRdmaDeviceType("VF"), plugin.NetRdmaDeviceTypeVF), + Entry("mixed-case VF", plugin.NetRdmaDeviceType("vF"), plugin.NetRdmaDeviceTypeVF), + ) + + DescribeTable("rejects unsupported device types", + func(deviceType plugin.NetRdmaDeviceType) { + nc := &netRdmaConfig{Matcher: `ib.*`, DeviceType: deviceType} + Expect(nc.validate()).To(MatchError(ContainSubstring(".deviceType"))) + }, + Entry("explicit any", plugin.NetRdmaDeviceType("any")), + Entry("unknown value", plugin.NetRdmaDeviceType("nic")), + ) }) var _ = Describe("numaAffinityConfig.validate", func() { @@ -332,6 +358,18 @@ networkRdma: Expect(cfg.NetworkRdma[0].matcher).NotTo(BeNil()) }) + It("parses networkRdma deviceType", func() { + cfg := mustParseYAML(` +domain: ydb.tech +networkRdma: + - matcher: "eth(.*)" + resourceCount: 2 + deviceType: vf +`) + Expect(cfg.NetworkRdma).To(HaveLen(1)) + Expect(string(cfg.NetworkRdma[0].DeviceType)).To(Equal("vf")) + }) + It("parses numaAffinity section", func() { cfg := mustParseYAML(` domain: ydb.tech diff --git a/cmd/udev-manager/main.go b/cmd/udev-manager/main.go index 4adea22..1f492d9 100644 --- a/cmd/udev-manager/main.go +++ b/cmd/udev-manager/main.go @@ -146,7 +146,12 @@ func startApp( discovery, registry, plugin.NetRdmaMatcherTemplater(domain, netRdmaConfig.matcher), - plugin.NetRdmaMatcherInstances(domain, netRdmaConfig.matcher, int(netRdmaConfig.ResourceCount)), + plugin.NetRdmaMatcherInstances( + domain, + netRdmaConfig.matcher, + int(netRdmaConfig.ResourceCount), + netRdmaConfig.DeviceType, + ), ), cancel, ) @@ -380,8 +385,9 @@ func (nbc *netBWConfig) validate() error { } type netRdmaConfig struct { - Matcher string `yaml:"matcher"` // matcher should be a valid regular expression - ResourceCount uint `yaml:"resourceCount"` + Matcher string `yaml:"matcher"` // matcher should be a valid regular expression + ResourceCount uint `yaml:"resourceCount"` + DeviceType plugin.NetRdmaDeviceType `yaml:"deviceType,omitempty"` matcher *regexp.Regexp // compiled matcher if the config is valid } @@ -393,6 +399,18 @@ func (nrc *netRdmaConfig) validate() error { return fmt.Errorf(".matcher: %q must be a valid regexp: %w", nrc.Matcher, err) } nrc.matcher = matcher + nrc.DeviceType = plugin.NetRdmaDeviceType(strings.ToLower(string(nrc.DeviceType))) + + switch nrc.DeviceType { + case plugin.NetRdmaDeviceTypeAny, plugin.NetRdmaDeviceTypePF, plugin.NetRdmaDeviceTypeVF: + default: + return fmt.Errorf( + ".deviceType: %q must be either %q, %q, or omitted", + nrc.DeviceType, + plugin.NetRdmaDeviceTypePF, + plugin.NetRdmaDeviceTypeVF, + ) + } return nil } diff --git a/internal/plugin/net_rdma.go b/internal/plugin/net_rdma.go index acd887c..fc57cbc 100644 --- a/internal/plugin/net_rdma.go +++ b/internal/plugin/net_rdma.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "regexp" + "strconv" "strings" "github.com/Mellanox/rdmamap" @@ -13,6 +14,21 @@ import ( pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1" ) +// NetRdmaDeviceType restricts networkRdma resources to a particular SR-IOV +// function type. The zero value disables function-type filtering. +type NetRdmaDeviceType string + +const ( + // NetRdmaDeviceTypeAny disables PF/VF filtering. + NetRdmaDeviceTypeAny NetRdmaDeviceType = "" + // NetRdmaDeviceTypePF selects SR-IOV physical functions. + NetRdmaDeviceTypePF NetRdmaDeviceType = "pf" + // NetRdmaDeviceTypeVF selects SR-IOV virtual functions. + NetRdmaDeviceTypeVF NetRdmaDeviceType = "vf" + + netRdmaDeviceTypeUnknown NetRdmaDeviceType = "unknown" +) + type netRdma struct { domain string ifname string @@ -81,8 +97,34 @@ func NetRdmaMatcherTemplater(domain string, matcher *regexp.Regexp) FromDevice[* } // NetRdmaMatcherInstances returns a FromDevice function that produces -// resourcesCount netRdma instances for each matching RDMA-capable net device. -func NetRdmaMatcherInstances(domain string, matcher *regexp.Regexp, resourcesCount int) FromDevice[[]*netRdma] { +// resourcesCount netRdma instances for each matching RDMA-capable net device +// of the requested device type. +func NetRdmaMatcherInstances( + domain string, + matcher *regexp.Regexp, + resourcesCount int, + deviceType NetRdmaDeviceType, +) FromDevice[[]*netRdma] { + return netRdmaMatcherInstances( + domain, + matcher, + resourcesCount, + deviceType, + rdmamap.GetRdmaDeviceForNetdevice, + rdmamap.GetRdmaCharDevices, + classifyNetRdmaDevice, + ) +} + +func netRdmaMatcherInstances( + domain string, + matcher *regexp.Regexp, + resourcesCount int, + deviceType NetRdmaDeviceType, + lookupDevice func(string) (string, error), + lookupCharDevices func(string) []string, + classifyDevice func(udev.Device) (NetRdmaDeviceType, error), +) FromDevice[[]*netRdma] { return func(dev udev.Device) ([]*netRdma, error) { if dev.Subsystem() != udev.NetSubsystem { return nil, nil @@ -97,12 +139,24 @@ func NetRdmaMatcherInstances(domain string, matcher *regexp.Regexp, resourcesCou return nil, nil } - rdmaDevice, err := rdmamap.GetRdmaDeviceForNetdevice(ifname) - if err != nil { - klog.Errorf("fail to get rdma devices for network device: %s %v", ifname, err) + if deviceType != NetRdmaDeviceTypeAny { + actualDeviceType, err := classifyDevice(dev) + if err != nil { + return nil, fmt.Errorf("classify network interface %q: %w", ifname, err) + } + if actualDeviceType != deviceType { + return nil, nil + } + } + + rdmaDevice, err := lookupDevice(ifname) + if isRdmaNonMatch(ifname, rdmaDevice, err) { return nil, nil } - rdmaCharDevices := rdmamap.GetRdmaCharDevices(rdmaDevice) + if err != nil { + return nil, fmt.Errorf("get RDMA device for network interface %q: %w", ifname, err) + } + rdmaCharDevices := lookupCharDevices(rdmaDevice) klog.Infof("found rdma character devices for ifname: %s devices: %v", ifname, rdmaCharDevices) instances := make([]*netRdma, 0, resourcesCount) @@ -119,3 +173,54 @@ func NetRdmaMatcherInstances(domain string, matcher *regexp.Regexp, resourcesCou return instances, nil } } + +func classifyNetRdmaDevice(dev udev.Device) (NetRdmaDeviceType, error) { + for parent := dev.Parent(); parent != nil; parent = parent.Parent() { + if parent.Subsystem() != udev.PciSubsystem { + continue + } + + attributeKeys := parent.SystemAttributeKeys() + attributes := make(map[string]struct{}, len(attributeKeys)) + for _, attribute := range attributeKeys { + attributes[attribute] = struct{}{} + } + + if _, ok := attributes[udev.SysAttrPhysfn]; ok { + return NetRdmaDeviceTypeVF, nil + } + + if _, ok := attributes[udev.SysAttrTotalVFs]; !ok { + return netRdmaDeviceTypeUnknown, nil + } + + totalVFsString := parent.SystemAttribute(udev.SysAttrTotalVFs) + totalVFs, err := strconv.ParseUint(totalVFsString, 10, 32) + if err != nil { + return netRdmaDeviceTypeUnknown, fmt.Errorf( + "parse %s value %q: %w", + udev.SysAttrTotalVFs, + totalVFsString, + err, + ) + } + if totalVFs > 0 { + return NetRdmaDeviceTypePF, nil + } + return netRdmaDeviceTypeUnknown, nil + } + + return netRdmaDeviceTypeUnknown, nil +} + +func isRdmaNonMatch(ifname, rdmaDevice string, err error) bool { + if err == nil { + // rdmamap returns an empty device without an error when an IPoIB + // interface has no matching RDMA device. + return rdmaDevice == "" + } + + // rdmamap does not expose sentinel errors for these definitive non-matches. + return err.Error() == fmt.Sprintf("rdma device not found for netdev %s", ifname) || + err.Error() == "unknown device type" +} diff --git a/internal/plugin/net_rdma_test.go b/internal/plugin/net_rdma_test.go index 52d0680..087da0d 100644 --- a/internal/plugin/net_rdma_test.go +++ b/internal/plugin/net_rdma_test.go @@ -2,6 +2,7 @@ package plugin import ( "context" + "errors" "regexp" . "github.com/onsi/ginkgo/v2" @@ -10,6 +11,16 @@ import ( "github.com/ydb-platform/udev-manager/internal/udev" ) +func netDeviceWithPCIParent(ifname string, sysattrs map[string]string) *mockDevice { + dev := netDevice(ifname, "100000", "up") + dev.parent = &mockDevice{ + id: udev.Id("/sys/devices/pci0000:00/0000:01:00.0"), + subsystem: "pci", + sysattrs: sysattrs, + } + return dev +} + var _ = Describe("NetRdmaMatcherTemplater", func() { var matcher *regexp.Regexp @@ -48,6 +59,178 @@ var _ = Describe("NetRdmaMatcherTemplater", func() { }) }) +var _ = Describe("classifyNetRdmaDevice", func() { + DescribeTable("classifies the closest PCI function", + func(sysattrs map[string]string, expected NetRdmaDeviceType) { + deviceType, err := classifyNetRdmaDevice(netDeviceWithPCIParent("eth0", sysattrs)) + Expect(err).NotTo(HaveOccurred()) + Expect(deviceType).To(Equal(expected)) + }, + Entry("VF by physfn", map[string]string{"physfn": "../0000:01:00.0"}, NetRdmaDeviceTypeVF), + Entry("PF by positive sriov_totalvfs", map[string]string{"sriov_totalvfs": "8"}, NetRdmaDeviceTypePF), + Entry("VF takes precedence", map[string]string{"physfn": "../0000:01:00.0", "sriov_totalvfs": "8"}, NetRdmaDeviceTypeVF), + Entry("zero total VFs is neither", map[string]string{"sriov_totalvfs": "0"}, netRdmaDeviceTypeUnknown), + Entry("missing SR-IOV attributes is neither", map[string]string{}, netRdmaDeviceTypeUnknown), + ) + + It("walks through an intermediate parent to the closest PCI function", func() { + dev := netDevice("ib0", "100000", "up") + dev.parent = &mockDevice{ + subsystem: "infiniband", + parent: &mockDevice{ + subsystem: "pci", + sysattrs: map[string]string{"sriov_totalvfs": "4"}, + }, + } + + deviceType, err := classifyNetRdmaDevice(dev) + Expect(err).NotTo(HaveOccurred()) + Expect(deviceType).To(Equal(NetRdmaDeviceTypePF)) + }) + + It("returns neither when there is no PCI ancestor", func() { + dev := netDevice("vlan0", "100000", "up") + dev.parent = &mockDevice{subsystem: "virtual", sysattrs: map[string]string{}} + + deviceType, err := classifyNetRdmaDevice(dev) + Expect(err).NotTo(HaveOccurred()) + Expect(deviceType).To(Equal(netRdmaDeviceTypeUnknown)) + }) + + It("rejects a malformed sriov_totalvfs value", func() { + _, err := classifyNetRdmaDevice(netDeviceWithPCIParent("eth0", map[string]string{ + "sriov_totalvfs": "invalid", + })) + Expect(err).To(MatchError(ContainSubstring("sriov_totalvfs"))) + }) +}) + +var _ = Describe("NetRdmaMatcherInstances", func() { + newMapper := func( + deviceType NetRdmaDeviceType, + lookupDevice func(string) (string, error), + lookupCharDevices func(string) []string, + classifyDevice func(udev.Device) (NetRdmaDeviceType, error), + ) FromDevice[[]*netRdma] { + return netRdmaMatcherInstances( + "ydb.tech", + regexp.MustCompile(`.*`), + 2, + deviceType, + lookupDevice, + lookupCharDevices, + classifyDevice, + ) + } + + DescribeTable("treats definitive non-RDMA lookup results as a non-match", + func(rdmaDevice string, lookupErr error) { + charDevicesCalled := false + mapper := newMapper( + NetRdmaDeviceTypeAny, + func(string) (string, error) { return rdmaDevice, lookupErr }, + func(string) []string { + charDevicesCalled = true + return nil + }, + func(udev.Device) (NetRdmaDeviceType, error) { + Fail("device classification must be skipped when deviceType is omitted") + return netRdmaDeviceTypeUnknown, nil + }, + ) + + instances, err := mapper(netDevice("eth0", "1000", "up")) + + Expect(err).NotTo(HaveOccurred()) + Expect(instances).To(BeNil()) + Expect(charDevicesCalled).To(BeFalse()) + }, + Entry("Ethernet has no RDMA mapping", "", errors.New("rdma device not found for netdev eth0")), + Entry("IPoIB lookup returns an empty mapping", "", nil), + Entry("link type cannot support RDMA", "", errors.New("unknown device type")), + ) + + It("returns unexpected RDMA lookup failures", func() { + mapper := newMapper( + NetRdmaDeviceTypeAny, + func(string) (string, error) { return "", errors.New("temporary sysfs failure") }, + func(string) []string { return nil }, + func(udev.Device) (NetRdmaDeviceType, error) { return netRdmaDeviceTypeUnknown, nil }, + ) + + instances, err := mapper(netDevice("eth0", "1000", "up")) + + Expect(err).To(MatchError(ContainSubstring("temporary sysfs failure"))) + Expect(instances).To(BeNil()) + }) + + It("uses a non-empty RDMA mapping even when no character devices are found", func() { + mapper := newMapper( + NetRdmaDeviceTypeAny, + func(string) (string, error) { return "mlx5_0", nil }, + func(string) []string { return nil }, + func(udev.Device) (NetRdmaDeviceType, error) { + Fail("device classification must be skipped when deviceType is omitted") + return netRdmaDeviceTypeUnknown, nil + }, + ) + + instances, err := mapper(netDevice("eth0", "100000", "up")) + + Expect(err).NotTo(HaveOccurred()) + Expect(instances).To(HaveLen(2)) + Expect(instances[0].associatedDevices).To(BeEmpty()) + }) + + DescribeTable("filters interfaces by the requested device type", + func(requested, detected NetRdmaDeviceType, expectedInstances int) { + lookupCalled := false + mapper := newMapper( + requested, + func(string) (string, error) { + lookupCalled = true + return "mlx5_0", nil + }, + func(string) []string { return []string{"/dev/infiniband/uverbs0"} }, + func(udev.Device) (NetRdmaDeviceType, error) { return detected, nil }, + ) + + instances, err := mapper(netDevice("eth0", "100000", "up")) + + Expect(err).NotTo(HaveOccurred()) + Expect(instances).To(HaveLen(expectedInstances)) + Expect(lookupCalled).To(Equal(expectedInstances > 0)) + }, + Entry("PF accepts PF", NetRdmaDeviceTypePF, NetRdmaDeviceTypePF, 2), + Entry("PF rejects VF", NetRdmaDeviceTypePF, NetRdmaDeviceTypeVF, 0), + Entry("PF rejects unknown", NetRdmaDeviceTypePF, netRdmaDeviceTypeUnknown, 0), + Entry("VF accepts VF", NetRdmaDeviceTypeVF, NetRdmaDeviceTypeVF, 2), + Entry("VF rejects PF", NetRdmaDeviceTypeVF, NetRdmaDeviceTypePF, 0), + Entry("VF rejects unknown", NetRdmaDeviceTypeVF, netRdmaDeviceTypeUnknown, 0), + ) + + It("returns device classification failures", func() { + lookupCalled := false + mapper := newMapper( + NetRdmaDeviceTypePF, + func(string) (string, error) { + lookupCalled = true + return "mlx5_0", nil + }, + func(string) []string { return nil }, + func(udev.Device) (NetRdmaDeviceType, error) { + return netRdmaDeviceTypeUnknown, errors.New("classification failed") + }, + ) + + instances, err := mapper(netDevice("eth0", "100000", "up")) + + Expect(err).To(MatchError(ContainSubstring("classification failed"))) + Expect(instances).To(BeNil()) + Expect(lookupCalled).To(BeFalse()) + }) +}) + var _ = Describe("netRdma", func() { Describe("Id", func() { It("returns ifname_idx format", func() { diff --git a/internal/plugin/scatter.go b/internal/plugin/scatter.go index c47023c..ee1b002 100644 --- a/internal/plugin/scatter.go +++ b/internal/plugin/scatter.go @@ -60,6 +60,10 @@ func (s *Scatter[T]) added(dev udev.Device) { klog.Errorf("failed to map device %q to instances, caused by %q", dev.Debug(), err.Error()) return } + if len(instances) == 0 { + klog.V(5).Infof("unmatched device: %q, mapper returned no instances", dev.Debug()) + return + } klog.V(5).Infof("Init: Matched device: %q", dev.Debug()) @@ -110,6 +114,10 @@ func (s *Scatter[T]) removed(dev udev.Device) { klog.Errorf("failed to map device %q to instances for 'Removed', caused by %q", dev.Debug(), err.Error()) return } + if len(instances) == 0 { + klog.V(5).Infof("unmatched device: %q, mapper returned no instances for 'Removed'", dev.Debug()) + return + } klog.V(5).Infof("Removed: Matched device: %q", dev.Debug()) diff --git a/internal/plugin/scatter_test.go b/internal/plugin/scatter_test.go index 0642194..22a6c7e 100644 --- a/internal/plugin/scatter_test.go +++ b/internal/plugin/scatter_test.go @@ -7,6 +7,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + + "github.com/ydb-platform/udev-manager/internal/udev" ) var _ = Describe("Scatter", func() { @@ -57,6 +59,27 @@ var _ = Describe("Scatter", func() { }) }) + Describe("added without instances", func() { + It("does not register a resource for an empty mapper result", func() { + emptyTemplate := ResourceTemplate{Domain: "ydb.tech", Prefix: "netrdma-0"} + emptyScatter := &Scatter[*partition]{ + templater: func(udev.Device) (*ResourceTemplate, error) { + return &emptyTemplate, nil + }, + mapper: func(udev.Device) ([]*partition, error) { + return nil, nil + }, + registry: nil, + routes: make(map[ResourceTemplate]Resource), + } + + Expect(func() { + emptyScatter.added(partitionDevice("nvme0n1p1", "nvme_disk01")) + }).NotTo(Panic()) + Expect(emptyScatter.routes).To(BeEmpty()) + }) + }) + Describe("removed with an existing route", func() { It("submits a HealthEvent when a matching device is removed", func() { dev := partitionDevice("nvme0n1p1", "nvme_disk01") diff --git a/internal/udev/udev.go b/internal/udev/udev.go index 99ad806..008a8ca 100644 --- a/internal/udev/udev.go +++ b/internal/udev/udev.go @@ -20,6 +20,7 @@ import ( const ( BlockSubsystem = "block" NetSubsystem = "net" + PciSubsystem = "pci" DeviceTypeKey = "DEVTYPE" DeviceTypePart = "partition" @@ -36,6 +37,8 @@ const ( SysAttrSpeed = "speed" SysAttrOperstate = "operstate" + SysAttrPhysfn = "physfn" + SysAttrTotalVFs = "sriov_totalvfs" ActionAdd = "add" ActionRemove = "remove" From aca9fccd0531712e2ff171da7fef3532350f8f69 Mon Sep 17 00:00:00 2001 From: Milad Ebrahimi Date: Thu, 10 Sep 2026 19:37:58 +0200 Subject: [PATCH 2/2] chore: add changie file --- .changes/unreleased/Fixed-20260910-115122.yaml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changes/unreleased/Fixed-20260910-115122.yaml diff --git a/.changes/unreleased/Fixed-20260910-115122.yaml b/.changes/unreleased/Fixed-20260910-115122.yaml new file mode 100644 index 0000000..e5c41b9 --- /dev/null +++ b/.changes/unreleased/Fixed-20260910-115122.yaml @@ -0,0 +1,3 @@ +kind: Fixed +body: Check if a matched network interface is RDMA enabled before registering it to kubernetes +time: 2026-09-10T11:51:22.339221+02:00