samples/bpf: xdp_redirect_cpu_user: Do not update bpf maps in option loop

Do not update xdp_redirect_cpu maps running while option loop but
defer it after all available options have been parsed. This is a
preliminary patch to pass the program name we want to attach to the
map entries as a user option

Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Link: https://lore.kernel.org/bpf/95dc46286fd2c609042948e04bb7ae1f5b425538.1594734381.git.lorenzo@kernel.org
This commit is contained in:
Lorenzo Bianconi 2020-07-14 15:56:36 +02:00 committed by Daniel Borkmann
parent daa5cdc3fd
commit a4e76f1bda

View File

@ -681,6 +681,7 @@ int main(int argc, char **argv)
int add_cpu = -1; int add_cpu = -1;
int opt, err; int opt, err;
int prog_fd; int prog_fd;
int *cpu, i;
__u32 qsize; __u32 qsize;
n_cpus = get_nprocs_conf(); n_cpus = get_nprocs_conf();
@ -716,6 +717,13 @@ int main(int argc, char **argv)
} }
mark_cpus_unavailable(); mark_cpus_unavailable();
cpu = malloc(n_cpus * sizeof(int));
if (!cpu) {
fprintf(stderr, "failed to allocate cpu array\n");
return EXIT_FAIL;
}
memset(cpu, 0, n_cpus * sizeof(int));
/* Parse commands line args */ /* Parse commands line args */
while ((opt = getopt_long(argc, argv, "hSd:s:p:q:c:xzF", while ((opt = getopt_long(argc, argv, "hSd:s:p:q:c:xzF",
long_options, &longindex)) != -1) { long_options, &longindex)) != -1) {
@ -760,8 +768,7 @@ int main(int argc, char **argv)
errno, strerror(errno)); errno, strerror(errno));
goto error; goto error;
} }
create_cpu_entry(add_cpu, qsize, added_cpus, true); cpu[added_cpus++] = add_cpu;
added_cpus++;
break; break;
case 'q': case 'q':
qsize = atoi(optarg); qsize = atoi(optarg);
@ -772,6 +779,7 @@ int main(int argc, char **argv)
case 'h': case 'h':
error: error:
default: default:
free(cpu);
usage(argv, obj); usage(argv, obj);
return EXIT_FAIL_OPTION; return EXIT_FAIL_OPTION;
} }
@ -784,16 +792,21 @@ int main(int argc, char **argv)
if (ifindex == -1) { if (ifindex == -1) {
fprintf(stderr, "ERR: required option --dev missing\n"); fprintf(stderr, "ERR: required option --dev missing\n");
usage(argv, obj); usage(argv, obj);
return EXIT_FAIL_OPTION; err = EXIT_FAIL_OPTION;
goto out;
} }
/* Required option */ /* Required option */
if (add_cpu == -1) { if (add_cpu == -1) {
fprintf(stderr, "ERR: required option --cpu missing\n"); fprintf(stderr, "ERR: required option --cpu missing\n");
fprintf(stderr, " Specify multiple --cpu option to add more\n"); fprintf(stderr, " Specify multiple --cpu option to add more\n");
usage(argv, obj); usage(argv, obj);
return EXIT_FAIL_OPTION; err = EXIT_FAIL_OPTION;
goto out;
} }
for (i = 0; i < added_cpus; i++)
create_cpu_entry(cpu[i], qsize, i, true);
/* Remove XDP program when program is interrupted or killed */ /* Remove XDP program when program is interrupted or killed */
signal(SIGINT, int_exit); signal(SIGINT, int_exit);
signal(SIGTERM, int_exit); signal(SIGTERM, int_exit);
@ -801,27 +814,32 @@ int main(int argc, char **argv)
prog = bpf_object__find_program_by_title(obj, prog_name); prog = bpf_object__find_program_by_title(obj, prog_name);
if (!prog) { if (!prog) {
fprintf(stderr, "bpf_object__find_program_by_title failed\n"); fprintf(stderr, "bpf_object__find_program_by_title failed\n");
return EXIT_FAIL; err = EXIT_FAIL;
goto out;
} }
prog_fd = bpf_program__fd(prog); prog_fd = bpf_program__fd(prog);
if (prog_fd < 0) { if (prog_fd < 0) {
fprintf(stderr, "bpf_program__fd failed\n"); fprintf(stderr, "bpf_program__fd failed\n");
return EXIT_FAIL; err = EXIT_FAIL;
goto out;
} }
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
fprintf(stderr, "link set xdp fd failed\n"); fprintf(stderr, "link set xdp fd failed\n");
return EXIT_FAIL_XDP; err = EXIT_FAIL_XDP;
goto out;
} }
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
if (err) { if (err) {
printf("can't get prog info - %s\n", strerror(errno)); printf("can't get prog info - %s\n", strerror(errno));
return err; goto out;
} }
prog_id = info.id; prog_id = info.id;
stats_poll(interval, use_separators, prog_name, stress_mode); stats_poll(interval, use_separators, prog_name, stress_mode);
return EXIT_OK; out:
free(cpu);
return err;
} }