Skip to content

Commit a4e76f1

Browse files
LorenzoBianconiborkmann
authored andcommitted
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
1 parent daa5cdc commit a4e76f1

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

‎samples/bpf/xdp_redirect_cpu_user.c‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
681681
int add_cpu = -1;
682682
int opt, err;
683683
int prog_fd;
684+
int *cpu, i;
684685
__u32 qsize;
685686

686687
n_cpus = get_nprocs_conf();
@@ -716,6 +717,13 @@ int main(int argc, char **argv)
716717
}
717718
mark_cpus_unavailable();
718719

720+
cpu = malloc(n_cpus * sizeof(int));
721+
if (!cpu) {
722+
fprintf(stderr, "failed to allocate cpu array\n");
723+
return EXIT_FAIL;
724+
}
725+
memset(cpu, 0, n_cpus * sizeof(int));
726+
719727
/* Parse commands line args */
720728
while ((opt = getopt_long(argc, argv, "hSd:s:p:q:c:xzF",
721729
long_options, &longindex)) != -1) {
@@ -760,8 +768,7 @@ int main(int argc, char **argv)
760768
errno, strerror(errno));
761769
goto error;
762770
}
763-
create_cpu_entry(add_cpu, qsize, added_cpus, true);
764-
added_cpus++;
771+
cpu[added_cpus++] = add_cpu;
765772
break;
766773
case 'q':
767774
qsize = atoi(optarg);
@@ -772,6 +779,7 @@ int main(int argc, char **argv)
772779
case 'h':
773780
error:
774781
default:
782+
free(cpu);
775783
usage(argv, obj);
776784
return EXIT_FAIL_OPTION;
777785
}
@@ -784,44 +792,54 @@ int main(int argc, char **argv)
784792
if (ifindex == -1) {
785793
fprintf(stderr, "ERR: required option --dev missing\n");
786794
usage(argv, obj);
787-
return EXIT_FAIL_OPTION;
795+
err = EXIT_FAIL_OPTION;
796+
goto out;
788797
}
789798
/* Required option */
790799
if (add_cpu == -1) {
791800
fprintf(stderr, "ERR: required option --cpu missing\n");
792801
fprintf(stderr, " Specify multiple --cpu option to add more\n");
793802
usage(argv, obj);
794-
return EXIT_FAIL_OPTION;
803+
err = EXIT_FAIL_OPTION;
804+
goto out;
795805
}
796806

807+
for (i = 0; i < added_cpus; i++)
808+
create_cpu_entry(cpu[i], qsize, i, true);
809+
797810
/* Remove XDP program when program is interrupted or killed */
798811
signal(SIGINT, int_exit);
799812
signal(SIGTERM, int_exit);
800813

801814
prog = bpf_object__find_program_by_title(obj, prog_name);
802815
if (!prog) {
803816
fprintf(stderr, "bpf_object__find_program_by_title failed\n");
804-
return EXIT_FAIL;
817+
err = EXIT_FAIL;
818+
goto out;
805819
}
806820

807821
prog_fd = bpf_program__fd(prog);
808822
if (prog_fd < 0) {
809823
fprintf(stderr, "bpf_program__fd failed\n");
810-
return EXIT_FAIL;
824+
err = EXIT_FAIL;
825+
goto out;
811826
}
812827

813828
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
814829
fprintf(stderr, "link set xdp fd failed\n");
815-
return EXIT_FAIL_XDP;
830+
err = EXIT_FAIL_XDP;
831+
goto out;
816832
}
817833

818834
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
819835
if (err) {
820836
printf("can't get prog info - %s\n", strerror(errno));
821-
return err;
837+
goto out;
822838
}
823839
prog_id = info.id;
824840

825841
stats_poll(interval, use_separators, prog_name, stress_mode);
826-
return EXIT_OK;
842+
out:
843+
free(cpu);
844+
return err;
827845
}

0 commit comments

Comments
 (0)