Skip to content

Commit 7a982f4

Browse files
authored
Merge pull request #4854 from marquiz/devel/rdt-root-clos
libcontainer/intelrdt: support explicit assignment to root CLOS
2 parents d845c4a + 7628194 commit 7a982f4

File tree

4 files changed

+171
-6
lines changed

4 files changed

+171
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package validate provides helpers for validating configuration.
2+
package validate
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package validate
2+
3+
import (
4+
"sync"
5+
6+
"github.com/opencontainers/runc/libcontainer/intelrdt"
7+
)
8+
9+
// Cache the result of intelrdt IsEnabled functions to avoid repeated sysfs
10+
// access and enable mocking for unit tests.
11+
type intelRdtStatus struct {
12+
sync.Once
13+
rdtEnabled bool
14+
catEnabled bool
15+
mbaEnabled bool
16+
}
17+
18+
var intelRdt = &intelRdtStatus{}
19+
20+
func (i *intelRdtStatus) init() {
21+
i.Do(func() {
22+
i.rdtEnabled = intelrdt.IsEnabled()
23+
i.catEnabled = intelrdt.IsCATEnabled()
24+
i.mbaEnabled = intelrdt.IsMBAEnabled()
25+
})
26+
}
27+
28+
func (i *intelRdtStatus) isEnabled() bool {
29+
i.init()
30+
return i.rdtEnabled
31+
}
32+
33+
func (i *intelRdtStatus) isCATEnabled() bool {
34+
i.init()
35+
return i.catEnabled
36+
}
37+
38+
func (i *intelRdtStatus) isMBAEnabled() bool {
39+
i.init()
40+
return i.mbaEnabled
41+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package validate
2+
3+
import (
4+
"testing"
5+
6+
"github.com/opencontainers/runc/libcontainer/configs"
7+
)
8+
9+
func TestValidateIntelRdt(t *testing.T) {
10+
// Call init to trigger the sync.Once and enable overriding the rdt status
11+
intelRdt.init()
12+
13+
testCases := []struct {
14+
name string
15+
rdtEnabled bool
16+
catEnabled bool
17+
mbaEnabled bool
18+
config *configs.IntelRdt
19+
isErr bool
20+
}{
21+
{
22+
name: "rdt not supported, no config",
23+
rdtEnabled: false,
24+
config: nil,
25+
isErr: false,
26+
},
27+
{
28+
name: "rdt not supported, with config",
29+
rdtEnabled: false,
30+
config: &configs.IntelRdt{},
31+
isErr: true,
32+
},
33+
{
34+
name: "empty config",
35+
rdtEnabled: true,
36+
config: &configs.IntelRdt{},
37+
isErr: false,
38+
},
39+
{
40+
name: "root clos",
41+
rdtEnabled: true,
42+
config: &configs.IntelRdt{
43+
ClosID: "/",
44+
},
45+
isErr: false,
46+
},
47+
{
48+
name: "invalid ClosID (.)",
49+
rdtEnabled: true,
50+
config: &configs.IntelRdt{
51+
ClosID: ".",
52+
},
53+
isErr: true,
54+
},
55+
{
56+
name: "invalid ClosID (..)",
57+
rdtEnabled: true,
58+
config: &configs.IntelRdt{
59+
ClosID: "..",
60+
},
61+
isErr: true,
62+
},
63+
{
64+
name: "invalid ClosID (contains /)",
65+
rdtEnabled: true,
66+
config: &configs.IntelRdt{
67+
ClosID: "foo/bar",
68+
},
69+
isErr: true,
70+
},
71+
{
72+
name: "cat not supported",
73+
rdtEnabled: true,
74+
catEnabled: false,
75+
config: &configs.IntelRdt{
76+
L3CacheSchema: "0=ff",
77+
},
78+
isErr: true,
79+
},
80+
{
81+
name: "mba not supported",
82+
rdtEnabled: true,
83+
mbaEnabled: false,
84+
config: &configs.IntelRdt{
85+
MemBwSchema: "0=100",
86+
},
87+
isErr: true,
88+
},
89+
{
90+
name: "valid config",
91+
rdtEnabled: true,
92+
catEnabled: true,
93+
mbaEnabled: true,
94+
config: &configs.IntelRdt{
95+
ClosID: "clos-1",
96+
L3CacheSchema: "0=ff",
97+
MemBwSchema: "0=100",
98+
},
99+
isErr: false,
100+
},
101+
}
102+
for _, tc := range testCases {
103+
t.Run(tc.name, func(t *testing.T) {
104+
intelRdt.rdtEnabled = tc.rdtEnabled
105+
intelRdt.catEnabled = tc.catEnabled
106+
intelRdt.mbaEnabled = tc.mbaEnabled
107+
108+
config := &configs.Config{
109+
Rootfs: "/var",
110+
IntelRdt: tc.config,
111+
}
112+
113+
err := Validate(config)
114+
if tc.isErr && err == nil {
115+
t.Error("expected error, got nil")
116+
}
117+
if !tc.isErr && err != nil {
118+
t.Error(err)
119+
}
120+
})
121+
}
122+
}

libcontainer/configs/validate/validator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/opencontainers/cgroups"
1212
"github.com/opencontainers/runc/libcontainer/configs"
13-
"github.com/opencontainers/runc/libcontainer/intelrdt"
1413
"github.com/opencontainers/runtime-spec/specs-go"
1514
selinux "github.com/opencontainers/selinux/go-selinux"
1615
"github.com/sirupsen/logrus"
@@ -283,18 +282,19 @@ func sysctl(config *configs.Config) error {
283282

284283
func intelrdtCheck(config *configs.Config) error {
285284
if config.IntelRdt != nil {
286-
if !intelrdt.IsEnabled() {
285+
if !intelRdt.isEnabled() {
287286
return fmt.Errorf("intelRdt is specified in config, but Intel RDT is not enabled")
288287
}
289288

290-
if config.IntelRdt.ClosID == "." || config.IntelRdt.ClosID == ".." || strings.Contains(config.IntelRdt.ClosID, "/") {
291-
return fmt.Errorf("invalid intelRdt.ClosID %q", config.IntelRdt.ClosID)
289+
switch clos := config.IntelRdt.ClosID; {
290+
case clos == ".", clos == "..", len(clos) > 1 && strings.Contains(clos, "/"):
291+
return fmt.Errorf("invalid intelRdt.ClosID %q", clos)
292292
}
293293

294-
if !intelrdt.IsCATEnabled() && config.IntelRdt.L3CacheSchema != "" {
294+
if !intelRdt.isCATEnabled() && config.IntelRdt.L3CacheSchema != "" {
295295
return errors.New("intelRdt.l3CacheSchema is specified in config, but Intel RDT/CAT is not enabled")
296296
}
297-
if !intelrdt.IsMBAEnabled() && config.IntelRdt.MemBwSchema != "" {
297+
if !intelRdt.isMBAEnabled() && config.IntelRdt.MemBwSchema != "" {
298298
return errors.New("intelRdt.memBwSchema is specified in config, but Intel RDT/MBA is not enabled")
299299
}
300300
}

0 commit comments

Comments
 (0)