Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
277d80c5
Unverified
Commit
277d80c5
authored
Feb 18, 2021
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpf: add and use bpf_cgroup_devices_attach() helper
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
6f5af027
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
59 deletions
+66
-59
cgfsng.c
src/lxc/cgroups/cgfsng.c
+1
-59
cgroup2_devices.c
src/lxc/cgroups/cgroup2_devices.c
+63
-0
cgroup2_devices.h
src/lxc/cgroups/cgroup2_devices.h
+2
-0
No files found.
src/lxc/cgroups/cgfsng.c
View file @
277d80c5
...
@@ -3166,12 +3166,8 @@ __cgfsng_ops static bool cgfsng_setup_limits(struct cgroup_ops *ops,
...
@@ -3166,12 +3166,8 @@ __cgfsng_ops static bool cgfsng_setup_limits(struct cgroup_ops *ops,
__cgfsng_ops
static
bool
cgfsng_devices_activate
(
struct
cgroup_ops
*
ops
,
struct
lxc_handler
*
handler
)
__cgfsng_ops
static
bool
cgfsng_devices_activate
(
struct
cgroup_ops
*
ops
,
struct
lxc_handler
*
handler
)
{
{
__do_bpf_program_free
struct
bpf_program
*
prog
=
NULL
;
int
ret
;
struct
lxc_conf
*
conf
;
struct
lxc_conf
*
conf
;
struct
hierarchy
*
unified
;
struct
hierarchy
*
unified
;
struct
lxc_list
*
it
;
struct
bpf_program
*
prog_old
;
if
(
!
ops
)
if
(
!
ops
)
return
ret_set_errno
(
false
,
ENOENT
);
return
ret_set_errno
(
false
,
ENOENT
);
...
@@ -3191,61 +3187,7 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
...
@@ -3191,61 +3187,7 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
!
unified
->
container_full_path
||
lxc_list_empty
(
&
conf
->
devices
))
!
unified
->
container_full_path
||
lxc_list_empty
(
&
conf
->
devices
))
return
true
;
return
true
;
prog
=
bpf_program_new
(
BPF_PROG_TYPE_CGROUP_DEVICE
);
return
bpf_cgroup_devices_attach
(
ops
,
&
conf
->
devices
);
if
(
!
prog
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to create new bpf program"
);
ret
=
bpf_program_init
(
prog
);
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to initialize bpf program"
);
bpf_device_set_type
(
prog
,
&
conf
->
devices
);
TRACE
(
"Device bpf %s all devices by default"
,
bpf_device_block_all
(
prog
)
?
"blocks"
:
"allows"
);
lxc_list_for_each
(
it
,
&
conf
->
devices
)
{
struct
device_item
*
cur
=
it
->
elem
;
if
(
!
bpf_device_add
(
prog
,
cur
))
{
TRACE
(
"Skipping type %c, major %d, minor %d, access %s, allow %d"
,
cur
->
type
,
cur
->
major
,
cur
->
minor
,
cur
->
access
,
cur
->
allow
);
continue
;
}
ret
=
bpf_program_append_device
(
prog
,
cur
);
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to add new rule to bpf device program: type %c, major %d, minor %d, access %s, allow %d, global_rule %d"
,
cur
->
type
,
cur
->
major
,
cur
->
minor
,
cur
->
access
,
cur
->
allow
,
cur
->
global_rule
);
TRACE
(
"Added rule to bpf device program: type %c, major %d, minor %d, access %s, allow %d, global_rule %d"
,
cur
->
type
,
cur
->
major
,
cur
->
minor
,
cur
->
access
,
cur
->
allow
,
cur
->
global_rule
);
}
ret
=
bpf_program_finalize
(
prog
);
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to finalize bpf program"
);
ret
=
bpf_program_cgroup_attach
(
prog
,
BPF_CGROUP_DEVICE
,
unified
->
cgfd_limit
,
-
EBADF
,
BPF_F_ALLOW_MULTI
);
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to attach bpf program"
);
/* Replace old bpf program. */
prog_old
=
move_ptr
(
ops
->
cgroup2_devices
);
ops
->
cgroup2_devices
=
move_ptr
(
prog
);
prog
=
move_ptr
(
prog_old
);
return
true
;
}
}
static
bool
__cgfsng_delegate_controllers
(
struct
cgroup_ops
*
ops
,
const
char
*
cgroup
)
static
bool
__cgfsng_delegate_controllers
(
struct
cgroup_ops
*
ops
,
const
char
*
cgroup
)
...
...
src/lxc/cgroups/cgroup2_devices.c
View file @
277d80c5
...
@@ -563,3 +563,66 @@ bool bpf_devices_cgroup_supported(void)
...
@@ -563,3 +563,66 @@ bool bpf_devices_cgroup_supported(void)
return
log_trace
(
true
,
"The bpf device cgroup is supported"
);
return
log_trace
(
true
,
"The bpf device cgroup is supported"
);
}
}
static
struct
bpf_program
*
__bpf_cgroup_devices
(
struct
lxc_list
*
devices
)
{
__do_bpf_program_free
struct
bpf_program
*
prog
=
NULL
;
int
ret
;
struct
lxc_list
*
it
;
prog
=
bpf_program_new
(
BPF_PROG_TYPE_CGROUP_DEVICE
);
if
(
!
prog
)
return
syserrno
(
NULL
,
"Failed to create new bpf program"
);
ret
=
bpf_program_init
(
prog
);
if
(
ret
)
return
syserrno
(
NULL
,
"Failed to initialize bpf program"
);
bpf_device_set_type
(
prog
,
devices
);
TRACE
(
"Device bpf %s all devices by default"
,
bpf_device_block_all
(
prog
)
?
"blocks"
:
"allows"
);
lxc_list_for_each
(
it
,
devices
)
{
struct
device_item
*
cur
=
it
->
elem
;
if
(
!
bpf_device_add
(
prog
,
cur
))
{
TRACE
(
"Skipping rule: type %c, major %d, minor %d, access %s, allow %d"
,
cur
->
type
,
cur
->
major
,
cur
->
minor
,
cur
->
access
,
cur
->
allow
);
continue
;
}
ret
=
bpf_program_append_device
(
prog
,
cur
);
if
(
ret
)
return
syserrno
(
NULL
,
"Failed adding rule: type %c, major %d, minor %d, access %s, allow %d"
,
cur
->
type
,
cur
->
major
,
cur
->
minor
,
cur
->
access
,
cur
->
allow
);
TRACE
(
"Added rule to bpf device program: type %c, major %d, minor %d, access %s, allow %d"
,
cur
->
type
,
cur
->
major
,
cur
->
minor
,
cur
->
access
,
cur
->
allow
);
}
ret
=
bpf_program_finalize
(
prog
);
if
(
ret
)
return
syserrno
(
NULL
,
"Failed to finalize bpf program"
);
return
move_ptr
(
prog
);
}
bool
bpf_cgroup_devices_attach
(
struct
cgroup_ops
*
ops
,
struct
lxc_list
*
devices
)
{
__do_bpf_program_free
struct
bpf_program
*
prog
=
NULL
;
int
ret
;
prog
=
__bpf_cgroup_devices
(
devices
);
if
(
!
prog
)
return
syserrno
(
false
,
"Failed to create bpf program"
);
ret
=
bpf_program_cgroup_attach
(
prog
,
BPF_CGROUP_DEVICE
,
ops
->
unified
->
cgfd_limit
,
-
EBADF
,
BPF_F_ALLOW_MULTI
);
if
(
ret
)
return
syserrno
(
false
,
"Failed to attach bpf program"
);
/* Replace old bpf program. */
swap
(
prog
,
ops
->
cgroup2_devices
);
return
log_trace
(
true
,
"Attached bpf program"
);
}
src/lxc/cgroups/cgroup2_devices.h
View file @
277d80c5
...
@@ -97,6 +97,8 @@ __hidden extern void bpf_device_program_free(struct cgroup_ops *ops);
...
@@ -97,6 +97,8 @@ __hidden extern void bpf_device_program_free(struct cgroup_ops *ops);
__hidden
extern
bool
bpf_devices_cgroup_supported
(
void
);
__hidden
extern
bool
bpf_devices_cgroup_supported
(
void
);
__hidden
extern
int
bpf_list_add_device
(
struct
lxc_conf
*
conf
,
struct
device_item
*
device
);
__hidden
extern
int
bpf_list_add_device
(
struct
lxc_conf
*
conf
,
struct
device_item
*
device
);
__hidden
extern
bool
bpf_cgroup_devices_attach
(
struct
cgroup_ops
*
ops
,
struct
lxc_list
*
devices
);
define_cleanup_function
(
struct
bpf_program
*
,
bpf_program_free
);
define_cleanup_function
(
struct
bpf_program
*
,
bpf_program_free
);
#define __do_bpf_program_free call_cleaner(bpf_program_free)
#define __do_bpf_program_free call_cleaner(bpf_program_free)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment