4
4
#ifndef _GNU_SOURCE
5
5
#define _GNU_SOURCE
6
6
#endif
7
+ #include <assert.h>
7
8
#include <ctype.h>
8
9
#include <errno.h>
9
10
#include <fcntl.h>
@@ -193,7 +194,8 @@ int mount_tracefs(const char *target)
193
194
return err ;
194
195
}
195
196
196
- int open_obj_pinned (const char * path , bool quiet )
197
+ int open_obj_pinned (const char * path , bool quiet ,
198
+ const struct bpf_obj_get_opts * opts )
197
199
{
198
200
char * pname ;
199
201
int fd = -1 ;
@@ -205,7 +207,7 @@ int open_obj_pinned(const char *path, bool quiet)
205
207
goto out_ret ;
206
208
}
207
209
208
- fd = bpf_obj_get (pname );
210
+ fd = bpf_obj_get_opts (pname , opts );
209
211
if (fd < 0 ) {
210
212
if (!quiet )
211
213
p_err ("bpf obj get (%s): %s" , pname ,
@@ -221,12 +223,13 @@ int open_obj_pinned(const char *path, bool quiet)
221
223
return fd ;
222
224
}
223
225
224
- int open_obj_pinned_any (const char * path , enum bpf_obj_type exp_type )
226
+ int open_obj_pinned_any (const char * path , enum bpf_obj_type exp_type ,
227
+ const struct bpf_obj_get_opts * opts )
225
228
{
226
229
enum bpf_obj_type type ;
227
230
int fd ;
228
231
229
- fd = open_obj_pinned (path , false);
232
+ fd = open_obj_pinned (path , false, opts );
230
233
if (fd < 0 )
231
234
return -1 ;
232
235
@@ -555,7 +558,7 @@ static int do_build_table_cb(const char *fpath, const struct stat *sb,
555
558
if (typeflag != FTW_F )
556
559
goto out_ret ;
557
560
558
- fd = open_obj_pinned (fpath , true);
561
+ fd = open_obj_pinned (fpath , true, NULL );
559
562
if (fd < 0 )
560
563
goto out_ret ;
561
564
@@ -928,7 +931,7 @@ int prog_parse_fds(int *argc, char ***argv, int **fds)
928
931
path = * * argv ;
929
932
NEXT_ARGP ();
930
933
931
- (* fds )[0 ] = open_obj_pinned_any (path , BPF_OBJ_PROG );
934
+ (* fds )[0 ] = open_obj_pinned_any (path , BPF_OBJ_PROG , NULL );
932
935
if ((* fds )[0 ] < 0 )
933
936
return -1 ;
934
937
return 1 ;
@@ -965,14 +968,16 @@ int prog_parse_fd(int *argc, char ***argv)
965
968
return fd ;
966
969
}
967
970
968
- static int map_fd_by_name (char * name , int * * fds )
971
+ static int map_fd_by_name (char * name , int * * fds ,
972
+ const struct bpf_get_fd_by_id_opts * opts )
969
973
{
970
974
unsigned int id = 0 ;
971
975
int fd , nb_fds = 0 ;
972
976
void * tmp ;
973
977
int err ;
974
978
975
979
while (true) {
980
+ LIBBPF_OPTS (bpf_get_fd_by_id_opts , opts_ro );
976
981
struct bpf_map_info info = {};
977
982
__u32 len = sizeof (info );
978
983
@@ -985,7 +990,9 @@ static int map_fd_by_name(char *name, int **fds)
985
990
return nb_fds ;
986
991
}
987
992
988
- fd = bpf_map_get_fd_by_id (id );
993
+ /* Request a read-only fd to query the map info */
994
+ opts_ro .open_flags = BPF_F_RDONLY ;
995
+ fd = bpf_map_get_fd_by_id_opts (id , & opts_ro );
989
996
if (fd < 0 ) {
990
997
p_err ("can't get map by id (%u): %s" ,
991
998
id , strerror (errno ));
@@ -1004,6 +1011,19 @@ static int map_fd_by_name(char *name, int **fds)
1004
1011
continue ;
1005
1012
}
1006
1013
1014
+ /* Get an fd with the requested options, if they differ
1015
+ * from the read-only options used to get the fd above.
1016
+ */
1017
+ if (memcmp (opts , & opts_ro , sizeof (opts_ro ))) {
1018
+ close (fd );
1019
+ fd = bpf_map_get_fd_by_id_opts (id , opts );
1020
+ if (fd < 0 ) {
1021
+ p_err ("can't get map by id (%u): %s" , id ,
1022
+ strerror (errno ));
1023
+ goto err_close_fds ;
1024
+ }
1025
+ }
1026
+
1007
1027
if (nb_fds > 0 ) {
1008
1028
tmp = realloc (* fds , (nb_fds + 1 ) * sizeof (int ));
1009
1029
if (!tmp ) {
@@ -1023,8 +1043,13 @@ static int map_fd_by_name(char *name, int **fds)
1023
1043
return -1 ;
1024
1044
}
1025
1045
1026
- int map_parse_fds (int * argc , char * * * argv , int * * fds )
1046
+ int map_parse_fds (int * argc , char * * * argv , int * * fds , __u32 open_flags )
1027
1047
{
1048
+ LIBBPF_OPTS (bpf_get_fd_by_id_opts , opts );
1049
+
1050
+ assert ((open_flags & ~BPF_F_RDONLY ) == 0 );
1051
+ opts .open_flags = open_flags ;
1052
+
1028
1053
if (is_prefix (* * argv , "id" )) {
1029
1054
unsigned int id ;
1030
1055
char * endptr ;
@@ -1038,7 +1063,7 @@ int map_parse_fds(int *argc, char ***argv, int **fds)
1038
1063
}
1039
1064
NEXT_ARGP ();
1040
1065
1041
- (* fds )[0 ] = bpf_map_get_fd_by_id (id );
1066
+ (* fds )[0 ] = bpf_map_get_fd_by_id_opts (id , & opts );
1042
1067
if ((* fds )[0 ] < 0 ) {
1043
1068
p_err ("get map by id (%u): %s" , id , strerror (errno ));
1044
1069
return -1 ;
@@ -1056,16 +1081,18 @@ int map_parse_fds(int *argc, char ***argv, int **fds)
1056
1081
}
1057
1082
NEXT_ARGP ();
1058
1083
1059
- return map_fd_by_name (name , fds );
1084
+ return map_fd_by_name (name , fds , & opts );
1060
1085
} else if (is_prefix (* * argv , "pinned" )) {
1061
1086
char * path ;
1087
+ LIBBPF_OPTS (bpf_obj_get_opts , get_opts );
1088
+ get_opts .file_flags = open_flags ;
1062
1089
1063
1090
NEXT_ARGP ();
1064
1091
1065
1092
path = * * argv ;
1066
1093
NEXT_ARGP ();
1067
1094
1068
- (* fds )[0 ] = open_obj_pinned_any (path , BPF_OBJ_MAP );
1095
+ (* fds )[0 ] = open_obj_pinned_any (path , BPF_OBJ_MAP , & get_opts );
1069
1096
if ((* fds )[0 ] < 0 )
1070
1097
return -1 ;
1071
1098
return 1 ;
@@ -1075,7 +1102,7 @@ int map_parse_fds(int *argc, char ***argv, int **fds)
1075
1102
return -1 ;
1076
1103
}
1077
1104
1078
- int map_parse_fd (int * argc , char * * * argv )
1105
+ int map_parse_fd (int * argc , char * * * argv , __u32 open_flags )
1079
1106
{
1080
1107
int * fds = NULL ;
1081
1108
int nb_fds , fd ;
@@ -1085,7 +1112,7 @@ int map_parse_fd(int *argc, char ***argv)
1085
1112
p_err ("mem alloc failed" );
1086
1113
return -1 ;
1087
1114
}
1088
- nb_fds = map_parse_fds (argc , argv , & fds );
1115
+ nb_fds = map_parse_fds (argc , argv , & fds , open_flags );
1089
1116
if (nb_fds != 1 ) {
1090
1117
if (nb_fds > 1 ) {
1091
1118
p_err ("several maps match this handle" );
@@ -1103,12 +1130,12 @@ int map_parse_fd(int *argc, char ***argv)
1103
1130
}
1104
1131
1105
1132
int map_parse_fd_and_info (int * argc , char * * * argv , struct bpf_map_info * info ,
1106
- __u32 * info_len )
1133
+ __u32 * info_len , __u32 open_flags )
1107
1134
{
1108
1135
int err ;
1109
1136
int fd ;
1110
1137
1111
- fd = map_parse_fd (argc , argv );
1138
+ fd = map_parse_fd (argc , argv , open_flags );
1112
1139
if (fd < 0 )
1113
1140
return -1 ;
1114
1141
0 commit comments