• Andrii Nakryiko's avatar
    selftests/bpf: add bpf_for_each(), bpf_for(), and bpf_repeat() macros · 8c2b5e90
    Andrii Nakryiko authored
    Add bpf_for_each(), bpf_for(), and bpf_repeat() macros that make writing
    open-coded iterator-based loops much more convenient and natural. These
    macros utilize cleanup attribute to ensure proper destruction of the
    iterator and thanks to that manage to provide the ergonomics that is
    very close to C language's for() construct. Typical loop would look like:
    
      int i;
      int arr[N];
    
      bpf_for(i, 0, N) {
          /* verifier will know that i >= 0 && i < N, so could be used to
           * directly access array elements with no extra checks
           */
           arr[i] = i;
      }
    
    bpf_repeat() is very similar, but it doesn't expose iteration number and
    is meant as a simple "repeat action N times" loop:
    
      bpf_repeat(N) { /* whatever, N times */ }
    
    Note that `break` and `continue` statements inside the {} block work as
    expected.
    
    bpf_for_each() is a generalization over any kind of BPF open-coded
    iterator allowing to use for-each-like approach instead of calling
    low-level bpf_iter_<type>_{new,next,destroy}() APIs explicitly. E.g.:
    
      struct cgroup *cg;
    
      bpf_for_each(cgroup, cg, some, input, args) {
          /* do something with each cg */
      }
    
    would call (not-yet-implemented) bpf_iter_cgroup_{new,next,destroy}()
    functions to form a loop over cgroups, where `some, input, args` are
    passed verbatim into constructor as
    
      bpf_iter_cgroup_new(&it, some, input, args).
    
    As a first demonstration, add pyperf variant based on the bpf_for() loop.
    
    Also clean up a few tests that either included bpf_misc.h header
    unnecessarily from the user-space, which is unsupported, or included it
    before any common types are defined (and thus leading to unnecessary
    compilation warnings, potentially).
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20230308184121.1165081-6-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    8c2b5e90
bpf_misc.h 7.5 KB