-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcaps.go
More file actions
204 lines (186 loc) · 6.06 KB
/
caps.go
File metadata and controls
204 lines (186 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2024 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nvconf
import (
"fmt"
"maps"
"slices"
"strings"
)
// DriverCaps is a set of NVIDIA driver capabilities as a bitmask.
type DriverCaps uint16
// Individual NVIDIA driver capabilities.
const (
// These correspond to capabilities defined by nvidia-container-toolkit.
CapCompute DriverCaps = 1 << iota
CapDisplay
CapGraphics
CapNGX
CapUtility
CapVideo
CapCompat32
// These correspond loosely to capabilities defined in
// src/nvidia/inc/kernel/os/capability.h. These are not
// covered by "all" and are considered privileged capabilities,
// requiring to be enabled explicitly.
CapFabricIMEXManagement // NV_RM_CAP_SYS_FABRIC_IMEX_MGMT
CapProfiling // GPU hardware performance counter access (Nsight Compute/Systems)
numValidCaps int = iota
)
const (
// AllCapabilitiesName is a special capability name
// that can be used to represent all capabilities.
AllCapabilitiesName = "all"
// ValidCapabilities is the set of all valid capabilities.
ValidCapabilities = DriverCaps(1<<numValidCaps - 1)
// SupportedDriverCaps is the set of driver capabilities that are supported by
// nvproxy.
SupportedDriverCaps = AllContainerDriverCaps | CapFabricIMEXManagement | CapProfiling
// AllContainerDriverCaps is the subset of SupportedDriverCaps that are
// enabled when enabling "all" capabilities is requested, which excludes
// "privileged" capabilities that are usually not intended. Similar to
// nvidia-container-toolkit/internal/config/image/capabilities.go:SupportedDriverCapabilities.
AllContainerDriverCaps = CapCompute | CapUtility | CapGraphics | CapVideo
// DefaultDriverCaps is the set of driver capabilities that are enabled by
// default in the absence of any other configuration. See
// nvidia-container-toolkit/internal/config/image/capabilities.go:DefaultDriverCapabilities.
DefaultDriverCaps = DriverCaps(CapCompute | CapUtility)
)
// individualString returns the string representation of the given capability.
// It must be one of the individual capabilities, or this will panic.
func (c DriverCaps) individualString() string {
switch c {
case CapCompute:
return "compute"
case CapDisplay:
return "display"
case CapGraphics:
return "graphics"
case CapNGX:
return "ngx"
case CapUtility:
return "utility"
case CapVideo:
return "video"
case CapCompat32:
return "compat32"
case CapFabricIMEXManagement:
return "fabric-imex-mgmt"
case CapProfiling:
return "profiling"
default:
panic(fmt.Sprintf("capability has no string mapping: %x", uint16(c)))
}
}
// individualNVIDIAFlag returns the flag that can be passed to
// nvidia-container-cli to enable the given capability.
// See nvidia-container-toolkit/blob/main/cmd/nvidia-container-runtime-hook/capabilities.go:capabilityToCLI
func (c DriverCaps) individualNVIDIAFlag() string {
switch c {
case CapCompute, CapDisplay, CapGraphics, CapNGX, CapUtility, CapVideo, CapCompat32:
return fmt.Sprintf("--%s", c.individualString())
default:
panic(fmt.Sprintf("capability has no NVIDIA flag mapping: %x", uint16(c)))
}
}
// individualCapabilityFromString returns the individual capability for the
// given string.
func individualCapabilityFromString(capName string) (DriverCaps, bool) {
for i := 0; i < numValidCaps; i++ {
cap := DriverCaps(1 << i)
if cap.String() == capName {
return cap, true
}
}
return 0, false
}
// DriverCapsFromString creates a new capability set from the given
// comma-separated list of capability names.
// The returned boolean represents whether the "all" keyword was used.
// Note that the "all" keyword is not actually expanded into the set of
// capabilities returned here; it is up to the caller to decide how to
// handle it.
func DriverCapsFromString(commaSeparatedCaps string) (DriverCaps, bool, error) {
cs := DriverCaps(0)
hasAll := false
for _, capName := range strings.Split(commaSeparatedCaps, ",") {
capName = strings.TrimSpace(capName)
if capName == "" {
continue
}
if capName == AllCapabilitiesName {
hasAll = true
continue
}
cap, ok := individualCapabilityFromString(capName)
if !ok {
return 0, false, fmt.Errorf("invalid capability: %q", capName)
}
cs |= DriverCaps(cap)
}
return cs, hasAll, nil
}
// String returns the string representation of the capability set.
func (c DriverCaps) String() string {
if c == 0 {
return ""
}
firstCap := true
var sb strings.Builder
for i := 0; i < numValidCaps; i++ {
cap := DriverCaps(1 << i)
if c&cap != 0 {
if !firstCap {
sb.WriteString(",")
}
firstCap = false
sb.WriteString(cap.individualString())
}
}
return sb.String()
}
// NVIDIAFlags returns the nvidia-container-cli flags that can be passed to
// enable the capabilities in the set.
func (c DriverCaps) NVIDIAFlags() []string {
if c == 0 {
return nil
}
caps := make([]string, 0, numValidCaps)
for i := 0; i < numValidCaps; i++ {
cap := DriverCaps(1 << i)
if c&cap != 0 {
caps = append(caps, cap.individualNVIDIAFlag())
}
}
return caps
}
// PopularCapabilitySets returns the most commonly used capability sets.
func PopularCapabilitySets() []DriverCaps {
capSets := make(map[DriverCaps]struct{})
capSets[SupportedDriverCaps] = struct{}{}
capSets[DefaultDriverCaps] = struct{}{}
// Add every individual supported capability together with CapUtility.
for i := 0; i < numValidCaps; i++ {
cap := DriverCaps(1 << i)
if cap == CapUtility {
continue
}
if cap&SupportedDriverCaps == 0 {
continue
}
capSets[cap|CapUtility] = struct{}{}
}
// Return as a sorted list.
return slices.Sorted(maps.Keys(capSets))
}