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
9b7d34a2
Unverified
Commit
9b7d34a2
authored
Feb 18, 2021
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpf: enable helpers to let caller replace existing bpf programs
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
54c17d39
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
12 deletions
+31
-12
cgfsng.c
src/lxc/cgroups/cgfsng.c
+1
-1
cgroup2_devices.c
src/lxc/cgroups/cgroup2_devices.c
+27
-9
cgroup2_devices.h
src/lxc/cgroups/cgroup2_devices.h
+2
-1
commands.c
src/lxc/commands.c
+1
-1
No files found.
src/lxc/cgroups/cgfsng.c
View file @
9b7d34a2
...
@@ -3227,7 +3227,7 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
...
@@ -3227,7 +3227,7 @@ __cgfsng_ops static bool cgfsng_devices_activate(struct cgroup_ops *ops, struct
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to finalize bpf program"
);
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to finalize bpf program"
);
ret
=
bpf_program_cgroup_attach
(
prog
,
BPF_CGROUP_DEVICE
,
ret
=
bpf_program_cgroup_attach
(
prog
,
BPF_CGROUP_DEVICE
,
unified
->
cgfd_limit
,
unified
->
cgfd_limit
,
-
EBADF
,
BPF_F_ALLOW_MULTI
);
BPF_F_ALLOW_MULTI
);
if
(
ret
)
if
(
ret
)
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to attach bpf program"
);
return
log_error_errno
(
false
,
ENOMEM
,
"Failed to attach bpf program"
);
...
...
src/lxc/cgroups/cgroup2_devices.c
View file @
9b7d34a2
...
@@ -363,7 +363,7 @@ static int bpf_program_load_kernel(struct bpf_program *prog)
...
@@ -363,7 +363,7 @@ static int bpf_program_load_kernel(struct bpf_program *prog)
}
}
int
bpf_program_cgroup_attach
(
struct
bpf_program
*
prog
,
int
type
,
int
fd_cgroup
,
int
bpf_program_cgroup_attach
(
struct
bpf_program
*
prog
,
int
type
,
int
fd_cgroup
,
uint32_t
flags
)
int
replace_bpf_fd
,
uint32_t
flags
)
{
{
__do_close
int
fd_cgroup_dup
=
-
EBADF
;
__do_close
int
fd_cgroup_dup
=
-
EBADF
;
int
ret
;
int
ret
;
...
@@ -372,20 +372,34 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
...
@@ -372,20 +372,34 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
if
(
fd_cgroup
<
0
)
if
(
fd_cgroup
<
0
)
return
ret_errno
(
EBADF
);
return
ret_errno
(
EBADF
);
if
(
flags
&
~
(
BPF_F_ALLOW_OVERRIDE
|
BPF_F_ALLOW_MULTI
))
if
(
flags
&
~
(
BPF_F_ALLOW_OVERRIDE
|
BPF_F_ALLOW_MULTI
|
BPF_F_REPLACE
))
return
log_error_errno
(
-
1
,
EINVAL
,
"Invalid flags for bpf program"
);
return
syserrno_set
(
-
EINVAL
,
"Invalid flags for bpf program"
);
if
(((
flags
&
BPF_F_REPLACE
)
&&
replace_bpf_fd
<
0
)
||
(
replace_bpf_fd
>=
0
&&
!
(
flags
&
BPF_F_REPLACE
)))
return
syserrno_set
(
-
EINVAL
,
"Requested to replace bpf program with invalid parameters"
);
/*
* Don't allow the bpf program to be overwritten for now. If we ever
* allow this we need to verify that the attach_flags of the current
* bpf program and the attach_flags of the new program match.
*/
if
(
flags
&
BPF_F_ALLOW_OVERRIDE
)
INFO
(
"Allowing to override bpf program"
);
if
(
prog
->
fd_cgroup
>=
0
)
{
if
(
prog
->
fd_cgroup
>=
0
)
{
if
(
prog
->
attached_type
!=
type
)
if
(
prog
->
attached_type
!=
type
)
return
log_error_errno
(
-
1
,
EBUSY
,
"Wrong type for bpf program"
);
return
log_error_errno
(
-
1
,
EBUSY
,
"Wrong type for bpf program"
);
if
(
prog
->
attached_flags
!=
flags
)
/*
* For BPF_F_ALLOW_OVERRIDE the flags of the new and old
* program must match.
*/
if
((
flags
&
BPF_F_ALLOW_OVERRIDE
)
&&
(
prog
->
attached_flags
!=
flags
))
return
log_error_errno
(
-
1
,
EBUSY
,
"Wrong flags for bpf program"
);
return
log_error_errno
(
-
1
,
EBUSY
,
"Wrong flags for bpf program"
);
if
(
flags
!=
BPF_F_ALLOW_OVERRIDE
)
return
0
;
}
}
/* Leave the caller's fd alone. */
fd_cgroup_dup
=
dup_cloexec
(
fd_cgroup
);
fd_cgroup_dup
=
dup_cloexec
(
fd_cgroup
);
if
(
fd_cgroup_dup
<
0
)
if
(
fd_cgroup_dup
<
0
)
return
-
errno
;
return
-
errno
;
...
@@ -401,15 +415,19 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
...
@@ -401,15 +415,19 @@ int bpf_program_cgroup_attach(struct bpf_program *prog, int type, int fd_cgroup,
.
attach_flags
=
flags
,
.
attach_flags
=
flags
,
};
};
if
(
flags
&
BPF_F_REPLACE
)
attr
->
replace_bpf_fd
=
replace_bpf_fd
;
ret
=
bpf
(
BPF_PROG_ATTACH
,
attr
,
sizeof
(
*
attr
));
ret
=
bpf
(
BPF_PROG_ATTACH
,
attr
,
sizeof
(
*
attr
));
if
(
ret
<
0
)
if
(
ret
<
0
)
return
log_error_errno
(
-
1
,
errno
,
"Failed to attach bpf program"
);
return
syserrno_set
(
-
errno
,
"Failed to attach bpf program"
);
close_move_fd
(
prog
->
fd_cgroup
,
fd_cgroup_dup
);
close_move_fd
(
prog
->
fd_cgroup
,
fd_cgroup_dup
);
prog
->
attached_type
=
type
;
prog
->
attached_type
=
type
;
prog
->
attached_flags
=
flags
;
prog
->
attached_flags
=
flags
;
TRACE
(
"Loaded and attached bpf program to cgroup %d"
,
prog
->
fd_cgroup
);
TRACE
(
"Attached bpf program to cgroup %d%s"
,
prog
->
fd_cgroup
,
(
flags
&
BPF_F_REPLACE
)
?
" and replaced old bpf program"
:
""
);
return
0
;
return
0
;
}
}
...
...
src/lxc/cgroups/cgroup2_devices.h
View file @
9b7d34a2
...
@@ -61,7 +61,8 @@ __hidden extern int bpf_program_init(struct bpf_program *prog);
...
@@ -61,7 +61,8 @@ __hidden extern int bpf_program_init(struct bpf_program *prog);
__hidden
extern
int
bpf_program_append_device
(
struct
bpf_program
*
prog
,
struct
device_item
*
device
);
__hidden
extern
int
bpf_program_append_device
(
struct
bpf_program
*
prog
,
struct
device_item
*
device
);
__hidden
extern
int
bpf_program_finalize
(
struct
bpf_program
*
prog
);
__hidden
extern
int
bpf_program_finalize
(
struct
bpf_program
*
prog
);
__hidden
extern
int
bpf_program_cgroup_attach
(
struct
bpf_program
*
prog
,
int
type
,
__hidden
extern
int
bpf_program_cgroup_attach
(
struct
bpf_program
*
prog
,
int
type
,
int
fd_cgroup
,
uint32_t
flags
);
int
fd_cgroup
,
int
replace_bpf_fd
,
uint32_t
flags
);
__hidden
extern
int
bpf_program_cgroup_detach
(
struct
bpf_program
*
prog
);
__hidden
extern
int
bpf_program_cgroup_detach
(
struct
bpf_program
*
prog
);
__hidden
extern
void
bpf_program_free
(
struct
bpf_program
*
prog
);
__hidden
extern
void
bpf_program_free
(
struct
bpf_program
*
prog
);
__hidden
extern
void
bpf_device_program_free
(
struct
cgroup_ops
*
ops
);
__hidden
extern
void
bpf_device_program_free
(
struct
cgroup_ops
*
ops
);
...
...
src/lxc/commands.c
View file @
9b7d34a2
...
@@ -1250,7 +1250,7 @@ static int lxc_cmd_add_bpf_device_cgroup_callback(int fd, struct lxc_cmd_req *re
...
@@ -1250,7 +1250,7 @@ static int lxc_cmd_add_bpf_device_cgroup_callback(int fd, struct lxc_cmd_req *re
goto
respond
;
goto
respond
;
ret
=
bpf_program_cgroup_attach
(
devices
,
BPF_CGROUP_DEVICE
,
ret
=
bpf_program_cgroup_attach
(
devices
,
BPF_CGROUP_DEVICE
,
unified
->
cgfd_mon
,
BPF_F_ALLOW_MULTI
);
unified
->
cgfd_mon
,
-
EBADF
,
BPF_F_ALLOW_MULTI
);
if
(
ret
)
if
(
ret
)
goto
respond
;
goto
respond
;
...
...
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