Skip to content

Commit

Permalink
samples/bpf: Get rid of bpf_prog_load_xattr() use
Browse files Browse the repository at this point in the history
Remove all the remaining uses of deprecated bpf_prog_load_xattr() API.

Signed-off-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Reviewed-by: Quentin Monnet <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
anakryiko authored and borkmann committed Feb 3, 2022
1 parent e4e284a commit 1e4edb6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
16 changes: 11 additions & 5 deletions samples/bpf/xdp1_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ static void usage(const char *prog)

int main(int argc, char **argv)
{
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info);
const char *optstr = "FSN";
int prog_fd, map_fd, opt;
struct bpf_program *prog;
struct bpf_object *obj;
struct bpf_map *map;
char filename[256];
Expand Down Expand Up @@ -123,11 +121,19 @@ int main(int argc, char **argv)
}

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj))
return 1;

prog = bpf_object__next_program(obj, NULL);
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);

if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
err = bpf_object__load(obj);
if (err)
return 1;

prog_fd = bpf_program__fd(prog);

map = bpf_object__next_map(obj, NULL);
if (!map) {
printf("finding a map in obj file failed\n");
Expand Down
17 changes: 12 additions & 5 deletions samples/bpf/xdp_adjust_tail_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ static void usage(const char *cmd)

int main(int argc, char **argv)
{
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
unsigned char opt_flags[256] = {};
const char *optstr = "i:T:P:SNFh";
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info);
unsigned int kill_after_s = 0;
int i, prog_fd, map_fd, opt;
struct bpf_program *prog;
struct bpf_object *obj;
__u32 max_pckt_size = 0;
__u32 key = 0;
Expand Down Expand Up @@ -148,11 +146,20 @@ int main(int argc, char **argv)
}

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;

if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj))
return 1;

prog = bpf_object__next_program(obj, NULL);
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);

err = bpf_object__load(obj);
if (err)
return 1;

prog_fd = bpf_program__fd(prog);

/* static global var 'max_pcktsz' is accessible from .data section */
if (max_pckt_size) {
map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
Expand Down
15 changes: 9 additions & 6 deletions samples/bpf/xdp_fwd_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,11 @@ static void usage(const char *prog)

int main(int argc, char **argv)
{
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
const char *prog_name = "xdp_fwd";
struct bpf_program *prog = NULL;
struct bpf_program *pos;
const char *sec_name;
int prog_fd, map_fd = -1;
int prog_fd = -1, map_fd = -1;
char filename[PATH_MAX];
struct bpf_object *obj;
int opt, i, idx, err;
Expand Down Expand Up @@ -119,15 +116,21 @@ int main(int argc, char **argv)

if (attach) {
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;

if (access(filename, O_RDONLY) < 0) {
printf("error accessing file %s: %s\n",
filename, strerror(errno));
return 1;
}

err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj))
return 1;

prog = bpf_object__next_program(obj, NULL);
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);

err = bpf_object__load(obj);
if (err) {
printf("Does kernel support devmap lookup?\n");
/* If not, the error message will be:
Expand Down
17 changes: 10 additions & 7 deletions samples/bpf/xdp_router_ipv4_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,20 +640,17 @@ static void usage(const char *prog)

int main(int ac, char **argv)
{
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info);
const char *optstr = "SF";
struct bpf_program *prog;
struct bpf_object *obj;
char filename[256];
char **ifname_list;
int prog_fd, opt;
int err, i = 1;

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;

total_ifindex = ac - 1;
ifname_list = (argv + 1);
Expand Down Expand Up @@ -684,14 +681,20 @@ int main(int ac, char **argv)
return 1;
}

if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj))
return 1;

prog = bpf_object__next_program(obj, NULL);
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);

printf("\n******************loading bpf file*********************\n");
if (!prog_fd) {
printf("bpf_prog_load_xattr: %s\n", strerror(errno));
err = bpf_object__load(obj);
if (err) {
printf("bpf_object__load(): %s\n", strerror(errno));
return 1;
}
prog_fd = bpf_program__fd(prog);

lpm_map_fd = bpf_object__find_map_fd_by_name(obj, "lpm_map");
rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
Expand Down
16 changes: 11 additions & 5 deletions samples/bpf/xdp_rxq_info_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,12 @@ static void stats_poll(int interval, int action, __u32 cfg_opt)
int main(int argc, char **argv)
{
__u32 cfg_options= NO_TOUCH ; /* Default: Don't touch packet memory */
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info);
int prog_fd, map_fd, opt, err;
bool use_separators = true;
struct config cfg = { 0 };
struct bpf_program *prog;
struct bpf_object *obj;
struct bpf_map *map;
char filename[256];
Expand All @@ -471,11 +469,19 @@ int main(int argc, char **argv)
char *action_str = NULL;

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;

if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj))
return EXIT_FAIL;

prog = bpf_object__next_program(obj, NULL);
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);

err = bpf_object__load(obj);
if (err)
return EXIT_FAIL;
prog_fd = bpf_program__fd(prog);

map = bpf_object__find_map_by_name(obj, "config_map");
stats_global_map = bpf_object__find_map_by_name(obj, "stats_global_map");
rx_queue_index_map = bpf_object__find_map_by_name(obj, "rx_queue_index_map");
Expand Down
17 changes: 10 additions & 7 deletions samples/bpf/xdp_tx_iptunnel_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,14 @@ static int parse_ports(const char *port_str, int *min_port, int *max_port)

int main(int argc, char **argv)
{
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
int min_port = 0, max_port = 0, vip2tnl_map_fd;
const char *optstr = "i:a:p:s:d:m:T:P:FSNh";
unsigned char opt_flags[256] = {};
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info);
unsigned int kill_after_s = 0;
struct iptnl_info tnl = {};
struct bpf_program *prog;
struct bpf_object *obj;
struct vip vip = {};
char filename[256];
Expand Down Expand Up @@ -259,15 +257,20 @@ int main(int argc, char **argv)
}

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;

if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj))
return 1;

if (!prog_fd) {
printf("bpf_prog_load_xattr: %s\n", strerror(errno));
prog = bpf_object__next_program(obj, NULL);
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);

err = bpf_object__load(obj);
if (err) {
printf("bpf_object__load(): %s\n", strerror(errno));
return 1;
}
prog_fd = bpf_program__fd(prog);

rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl");
Expand Down

0 comments on commit 1e4edb6

Please sign in to comment.