Skip to content

Commit 0bb52b0

Browse files
qmonnetborkmann
authored andcommitted
tools: bpftool: add "bpftool map freeze" subcommand
Add a new subcommand to freeze maps from user space. Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent c354ff2 commit 0bb52b0

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎tools/bpf/bpftool/Documentation/bpftool-map.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ MAP COMMANDS
3636
| **bpftool** **map pop** *MAP*
3737
| **bpftool** **map enqueue** *MAP* **value** *VALUE*
3838
| **bpftool** **map dequeue** *MAP*
39+
| **bpftool** **map freeze** *MAP*
3940
| **bpftool** **map help**
4041
|
4142
| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -127,6 +128,14 @@ DESCRIPTION
127128
**bpftool map dequeue** *MAP*
128129
Dequeue and print **value** from the queue.
129130

131+
**bpftool map freeze** *MAP*
132+
Freeze the map as read-only from user space. Entries from a
133+
frozen map can not longer be updated or deleted with the
134+
**bpf\ ()** system call. This operation is not reversible,
135+
and the map remains immutable from user space until its
136+
destruction. However, read and write permissions for BPF
137+
programs to the map remain unchanged.
138+
130139
**bpftool map help**
131140
Print short help message.
132141

‎tools/bpf/bpftool/bash-completion/bpftool‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ _bpftool()
449449
map)
450450
local MAP_TYPE='id pinned'
451451
case $command in
452-
show|list|dump|peek|pop|dequeue)
452+
show|list|dump|peek|pop|dequeue|freeze)
453453
case $prev in
454454
$command)
455455
COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
@@ -638,7 +638,7 @@ _bpftool()
638638
[[ $prev == $object ]] && \
639639
COMPREPLY=( $( compgen -W 'delete dump getnext help \
640640
lookup pin event_pipe show list update create \
641-
peek push enqueue pop dequeue' -- \
641+
peek push enqueue pop dequeue freeze' -- \
642642
"$cur" ) )
643643
;;
644644
esac

‎tools/bpf/bpftool/map.c‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,35 @@ static int do_pop_dequeue(int argc, char **argv)
12621262
return err;
12631263
}
12641264

1265+
static int do_freeze(int argc, char **argv)
1266+
{
1267+
int err, fd;
1268+
1269+
if (!REQ_ARGS(2))
1270+
return -1;
1271+
1272+
fd = map_parse_fd(&argc, &argv);
1273+
if (fd < 0)
1274+
return -1;
1275+
1276+
if (argc) {
1277+
close(fd);
1278+
return BAD_ARG();
1279+
}
1280+
1281+
err = bpf_map_freeze(fd);
1282+
close(fd);
1283+
if (err) {
1284+
p_err("failed to freeze map: %s", strerror(errno));
1285+
return err;
1286+
}
1287+
1288+
if (json_output)
1289+
jsonw_null(json_wtr);
1290+
1291+
return 0;
1292+
}
1293+
12651294
static int do_help(int argc, char **argv)
12661295
{
12671296
if (json_output) {
@@ -1286,6 +1315,7 @@ static int do_help(int argc, char **argv)
12861315
" %s %s pop MAP\n"
12871316
" %s %s enqueue MAP value VALUE\n"
12881317
" %s %s dequeue MAP\n"
1318+
" %s %s freeze MAP\n"
12891319
" %s %s help\n"
12901320
"\n"
12911321
" " HELP_SPEC_MAP "\n"
@@ -1304,7 +1334,8 @@ static int do_help(int argc, char **argv)
13041334
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
13051335
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
13061336
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1307-
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
1337+
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1338+
bin_name, argv[-2]);
13081339

13091340
return 0;
13101341
}
@@ -1326,6 +1357,7 @@ static const struct cmd cmds[] = {
13261357
{ "enqueue", do_update },
13271358
{ "pop", do_pop_dequeue },
13281359
{ "dequeue", do_pop_dequeue },
1360+
{ "freeze", do_freeze },
13291361
{ 0 }
13301362
};
13311363

0 commit comments

Comments
 (0)