|
| 1 | +// Copyright 2025 Intel Corporation. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path" |
| 22 | + "regexp" |
| 23 | + "slices" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/pkg/errors" |
| 28 | + |
| 29 | + "k8s.io/klog/v2" |
| 30 | + pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1" |
| 31 | + |
| 32 | + dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + sysfAccelDirectory = "/sys/class/accel" |
| 37 | + devfsAccelDirectory = "/dev/accel" |
| 38 | + npuDeviceRE = `^accel[0-9]+$` |
| 39 | + vendorString = "0x8086" |
| 40 | + |
| 41 | + // Device plugin settings. |
| 42 | + namespace = "npu.intel.com" |
| 43 | + deviceTypeNpu = "npu" |
| 44 | + |
| 45 | + // Period of device scans. |
| 46 | + scanPeriod = 5 * time.Second |
| 47 | +) |
| 48 | + |
| 49 | +var npuIDs = []string{ |
| 50 | + "0x7e4c", // Core Ultra Series 1 |
| 51 | + "0x643e", // Core Ultra 200V Series |
| 52 | + "0xad1d", // Core Ultra Series 2 |
| 53 | + "0x7d1d", // Core Ultra Series 2 (H) |
| 54 | +} |
| 55 | + |
| 56 | +type cliOptions struct { |
| 57 | + sharedDevNum int |
| 58 | +} |
| 59 | + |
| 60 | +type devicePlugin struct { |
| 61 | + npuDeviceReg *regexp.Regexp |
| 62 | + |
| 63 | + scanTicker *time.Ticker |
| 64 | + scanDone chan bool |
| 65 | + |
| 66 | + sysfsDir string |
| 67 | + devfsDir string |
| 68 | + |
| 69 | + options cliOptions |
| 70 | +} |
| 71 | + |
| 72 | +func newDevicePlugin(sysfsDir, devfsDir string, options cliOptions) *devicePlugin { |
| 73 | + dp := &devicePlugin{ |
| 74 | + sysfsDir: sysfsDir, |
| 75 | + devfsDir: devfsDir, |
| 76 | + options: options, |
| 77 | + npuDeviceReg: regexp.MustCompile(npuDeviceRE), |
| 78 | + scanTicker: time.NewTicker(scanPeriod), |
| 79 | + scanDone: make(chan bool, 1), // buffered as we may send to it before Scan starts receiving from it |
| 80 | + } |
| 81 | + |
| 82 | + return dp |
| 83 | +} |
| 84 | + |
| 85 | +func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error { |
| 86 | + defer dp.scanTicker.Stop() |
| 87 | + |
| 88 | + klog.V(1).Infof("NPU (%s) resource share count = %d", deviceTypeNpu, dp.options.sharedDevNum) |
| 89 | + |
| 90 | + previousCount := 0 |
| 91 | + devType := fmt.Sprintf("%s/%s", namespace, deviceTypeNpu) |
| 92 | + |
| 93 | + for { |
| 94 | + devTree, err := dp.scan() |
| 95 | + if err != nil { |
| 96 | + klog.Errorf("NPU scan failed: %v", err) |
| 97 | + return errors.Wrap(err, "NPU scan failed") |
| 98 | + } |
| 99 | + |
| 100 | + count := devTree.DeviceTypeCount(devType) |
| 101 | + if count != previousCount { |
| 102 | + klog.V(1).Infof("NPU scan update: %d->%d '%s' resources found", previousCount, count, devType) |
| 103 | + |
| 104 | + previousCount = count |
| 105 | + } |
| 106 | + |
| 107 | + notifier.Notify(devTree) |
| 108 | + |
| 109 | + select { |
| 110 | + case <-dp.scanDone: |
| 111 | + return nil |
| 112 | + case <-dp.scanTicker.C: |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (dp *devicePlugin) isCompatibleDevice(name string) bool { |
| 118 | + if !dp.npuDeviceReg.MatchString(name) { |
| 119 | + klog.V(4).Info("Not compatible device: ", name) |
| 120 | + return false |
| 121 | + } |
| 122 | + |
| 123 | + dat, err := os.ReadFile(path.Join(dp.sysfsDir, name, "device/vendor")) |
| 124 | + if err != nil { |
| 125 | + klog.Warning("Skipping. Can't read vendor file: ", err) |
| 126 | + return false |
| 127 | + } |
| 128 | + |
| 129 | + if strings.TrimSpace(string(dat)) != vendorString { |
| 130 | + klog.V(4).Info("Non-Intel NPU: ", name) |
| 131 | + return false |
| 132 | + } |
| 133 | + |
| 134 | + dat, err = os.ReadFile(path.Join(dp.sysfsDir, name, "device/device")) |
| 135 | + if err != nil { |
| 136 | + klog.Warning("Skipping. Can't read device file: ", err) |
| 137 | + return false |
| 138 | + } |
| 139 | + |
| 140 | + datStr := strings.Split(string(dat), "\n")[0] |
| 141 | + if !slices.Contains(npuIDs, datStr) { |
| 142 | + klog.Warning("Unknown device id: ", datStr) |
| 143 | + return false |
| 144 | + } |
| 145 | + |
| 146 | + return true |
| 147 | +} |
| 148 | + |
| 149 | +func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) { |
| 150 | + files, err := os.ReadDir(dp.sysfsDir) |
| 151 | + if err != nil { |
| 152 | + return nil, errors.Wrap(err, "Can't read sysfs folder") |
| 153 | + } |
| 154 | + |
| 155 | + devTree := dpapi.NewDeviceTree() |
| 156 | + |
| 157 | + for _, f := range files { |
| 158 | + name := f.Name() |
| 159 | + |
| 160 | + if !dp.isCompatibleDevice(name) { |
| 161 | + continue |
| 162 | + } |
| 163 | + |
| 164 | + devPath := path.Join(dp.devfsDir, name) |
| 165 | + if _, err = os.Stat(devPath); err != nil { |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + // even querying metrics requires device to be writable |
| 170 | + devSpec := pluginapi.DeviceSpec{ |
| 171 | + HostPath: devPath, |
| 172 | + ContainerPath: devPath, |
| 173 | + Permissions: "rw", |
| 174 | + } |
| 175 | + |
| 176 | + deviceInfo := dpapi.NewDeviceInfo(pluginapi.Healthy, []pluginapi.DeviceSpec{devSpec}, nil, nil, nil, nil) |
| 177 | + |
| 178 | + for i := 0; i < dp.options.sharedDevNum; i++ { |
| 179 | + devID := fmt.Sprintf("%s-%d", name, i) |
| 180 | + devTree.AddDevice("npu", devID, deviceInfo) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return devTree, nil |
| 185 | +} |
| 186 | + |
| 187 | +func (dp *devicePlugin) Allocate(request *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) { |
| 188 | + return nil, &dpapi.UseDefaultMethodError{} |
| 189 | +} |
| 190 | + |
| 191 | +func main() { |
| 192 | + var ( |
| 193 | + prefix string |
| 194 | + opts cliOptions |
| 195 | + ) |
| 196 | + |
| 197 | + flag.StringVar(&prefix, "prefix", "", "Prefix for devfs & sysfs paths") |
| 198 | + flag.IntVar(&opts.sharedDevNum, "shared-dev-num", 1, "number of containers sharing the same NPU device") |
| 199 | + flag.Parse() |
| 200 | + |
| 201 | + if opts.sharedDevNum < 1 { |
| 202 | + klog.Error("The number of containers sharing the same NPU must greater than zero") |
| 203 | + os.Exit(1) |
| 204 | + } |
| 205 | + |
| 206 | + klog.V(1).Infof("NPU device plugin started") |
| 207 | + |
| 208 | + plugin := newDevicePlugin(prefix+sysfAccelDirectory, prefix+devfsAccelDirectory, opts) |
| 209 | + |
| 210 | + manager := dpapi.NewManager(namespace, plugin) |
| 211 | + manager.Run() |
| 212 | +} |
0 commit comments