Skip to content

Commit 5535248

Browse files
committed
Merge branch 'no-existing-cgroupv2-filters' into 'master'
Support writing entire device filter eBPF program (if none present) See merge request nvidia/container-toolkit/libnvidia-container!128
2 parents 5cfafd5 + 1a6762b commit 5535248

File tree

1 file changed

+35
-17
lines changed
  • src/nvcgo/internal/cgroup

1 file changed

+35
-17
lines changed

src/nvcgo/internal/cgroup/v2.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/cilium/ebpf"
27+
"github.com/cilium/ebpf/asm"
2728
"golang.org/x/sys/unix"
2829
)
2930

@@ -111,38 +112,34 @@ func (c *cgroupv2) AddDeviceRules(cgroupPath string, rules []DeviceRule) error {
111112

112113
// Generate a new set of eBPF programs by prepending instructions for the
113114
// new devices to the instructions of each existing program.
115+
// If no existing programs found, create a new program with just our device filter.
114116
var newProgs []*ebpf.Program
117+
if len(oldProgs) == 0 {
118+
oldInsts := asm.Instructions{asm.Return()}
119+
120+
newProg, err := generateNewProgram(rules, oldInsts)
121+
if err != nil {
122+
return fmt.Errorf("unable to generate new device filter program with no existing programs: %v", err)
123+
}
124+
125+
newProgs = append(newProgs, newProg)
126+
}
115127
for _, oldProg := range oldProgs {
116-
// Retreive Info() from the original program.
117128
oldInfo, err := oldProg.Info()
118129
if err != nil {
119130
return fmt.Errorf("unable to get Info() of the original device filters program: %v", err)
120131
}
121132

122-
// Retreive the instructions from the original program.
123133
oldInsts, err := oldInfo.Instructions()
124134
if err != nil {
125135
return fmt.Errorf("unable to get the instructions of the original device filters program: %v", err)
126136
}
127137

128-
// Prepend instructions for the new devices to the original set of instructions.
129-
newInsts, err := PrependDeviceFilter(rules, oldInsts)
130-
if err != nil {
131-
return fmt.Errorf("unable to prepend new device filters to the original device filters program: %v", err)
132-
}
133-
134-
// Generate new eBPF program for the merged device filter instructions.
135-
spec := &ebpf.ProgramSpec{
136-
Type: oldProg.Type(),
137-
Instructions: newInsts,
138-
License: BpfProgramLicense,
139-
}
140-
newProg, err := ebpf.NewProgram(spec)
138+
newProg, err := generateNewProgram(rules, oldInsts)
141139
if err != nil {
142-
return fmt.Errorf("unable to create new device filters program: %v", err)
140+
return fmt.Errorf("unable to generate new device filter program from existing programs: %v", err)
143141
}
144142

145-
// Append to the list of new programs.
146143
newProgs = append(newProgs, newProg)
147144
}
148145

@@ -173,3 +170,24 @@ func (c *cgroupv2) AddDeviceRules(cgroupPath string, rules []DeviceRule) error {
173170

174171
return nil
175172
}
173+
174+
func generateNewProgram(rules []DeviceRule, oldInsts asm.Instructions) (*ebpf.Program, error) {
175+
// Prepend instructions for the new devices to the original set of instructions.
176+
newInsts, err := PrependDeviceFilter(rules, oldInsts)
177+
if err != nil {
178+
return nil, fmt.Errorf("unable to prepend new device filters to the original device filters program: %v", err)
179+
}
180+
181+
// Generate new eBPF program for the merged device filter instructions.
182+
spec := &ebpf.ProgramSpec{
183+
Type: ebpf.CGroupDevice,
184+
Instructions: newInsts,
185+
License: BpfProgramLicense,
186+
}
187+
newProg, err := ebpf.NewProgram(spec)
188+
if err != nil {
189+
return nil, fmt.Errorf("unable to create new device filters program: %v", err)
190+
}
191+
192+
return newProg, nil
193+
}

0 commit comments

Comments
 (0)