1. 01 May, 2020 5 commits
    • Stanislav Fomichev's avatar
      bpf: Bpf_{g,s}etsockopt for struct bpf_sock_addr · beecf11b
      Stanislav Fomichev authored
      Currently, bpf_getsockopt and bpf_setsockopt helpers operate on the
      'struct bpf_sock_ops' context in BPF_PROG_TYPE_SOCK_OPS program.
      Let's generalize them and make them available for 'struct bpf_sock_addr'.
      That way, in the future, we can allow those helpers in more places.
      
      As an example, let's expose those 'struct bpf_sock_addr' based helpers to
      BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the
      connection is made.
      
      v3:
      * Expose custom helpers for bpf_sock_addr context instead of doing
        generic bpf_sock argument (as suggested by Daniel). Even with
        try_socket_lock that doesn't sleep we have a problem where context sk
        is already locked and socket lock is non-nestable.
      
      v2:
      * s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Link: https://lore.kernel.org/bpf/20200430233152.199403-1-sdf@google.com
      beecf11b
    • Alexei Starovoitov's avatar
      Merge branch 'bpf_enable_stats' · 3dbb5b50
      Alexei Starovoitov authored
      Song Liu says:
      
      ====================
      run_time_ns is a useful stats for BPF programs. However, it is gated by
      sysctl kernel.bpf_stats_enabled. When multiple user space tools are
      toggling kernl.bpf_stats_enabled at the same time, they may confuse each
      other.
      
      Solve this problem with a new BPF command BPF_ENABLE_STATS.
      
      Changes v8 => v9:
        1. Clean up in selftest (Andrii).
        2. Not using static variable in test program (Andrii).
      
      Changes v7 => v8:
        1. Change name BPF_STATS_RUNTIME_CNT => BPF_STATS_RUN_TIME (Alexei).
        2. Add CHECK_ATTR to bpf_enable_stats() (Alexei).
        3. Rebase (Andrii).
        4. Simplfy the selftest (Alexei).
      
      Changes v6 => v7:
        1. Add test to verify run_cnt matches count measured by the program.
      
      Changes v5 => v6:
        1. Simplify test program (Yonghong).
        2. Rebase (with some conflicts).
      
      Changes v4 => v5:
        1. Use memset to zero bpf_attr in bpf_enable_stats() (Andrii).
      
      Changes v3 => v4:
        1. Add libbpf support and selftest;
        2. Avoid cleaning trailing space.
      
      Changes v2 => v3:
        1. Rename the command to BPF_ENABLE_STATS, and make it extendible.
        2. fix commit log;
        3. remove unnecessary headers.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      3dbb5b50
    • Song Liu's avatar
      bpf: Add selftest for BPF_ENABLE_STATS · 31a9f7fe
      Song Liu authored
      Add test for BPF_ENABLE_STATS, which should enable run_time_ns stats.
      
      ~/selftests/bpf# ./test_progs -t enable_stats  -v
      test_enable_stats:PASS:skel_open_and_load 0 nsec
      test_enable_stats:PASS:get_stats_fd 0 nsec
      test_enable_stats:PASS:attach_raw_tp 0 nsec
      test_enable_stats:PASS:get_prog_info 0 nsec
      test_enable_stats:PASS:check_stats_enabled 0 nsec
      test_enable_stats:PASS:check_run_cnt_valid 0 nsec
      Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
      Signed-off-by: default avatarSong Liu <songliubraving@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Link: https://lore.kernel.org/bpf/20200430071506.1408910-4-songliubraving@fb.com
      31a9f7fe
    • Song Liu's avatar
      libbpf: Add support for command BPF_ENABLE_STATS · 0bee1067
      Song Liu authored
      bpf_enable_stats() is added to enable given stats.
      Signed-off-by: default avatarSong Liu <songliubraving@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20200430071506.1408910-3-songliubraving@fb.com
      0bee1067
    • Song Liu's avatar
      bpf: Sharing bpf runtime stats with BPF_ENABLE_STATS · d46edd67
      Song Liu authored
      Currently, sysctl kernel.bpf_stats_enabled controls BPF runtime stats.
      Typical userspace tools use kernel.bpf_stats_enabled as follows:
      
        1. Enable kernel.bpf_stats_enabled;
        2. Check program run_time_ns;
        3. Sleep for the monitoring period;
        4. Check program run_time_ns again, calculate the difference;
        5. Disable kernel.bpf_stats_enabled.
      
      The problem with this approach is that only one userspace tool can toggle
      this sysctl. If multiple tools toggle the sysctl at the same time, the
      measurement may be inaccurate.
      
      To fix this problem while keep backward compatibility, introduce a new
      bpf command BPF_ENABLE_STATS. On success, this command enables stats and
      returns a valid fd. BPF_ENABLE_STATS takes argument "type". Currently,
      only one type, BPF_STATS_RUN_TIME, is supported. We can extend the
      command to support other types of stats in the future.
      
      With BPF_ENABLE_STATS, user space tool would have the following flow:
      
        1. Get a fd with BPF_ENABLE_STATS, and make sure it is valid;
        2. Check program run_time_ns;
        3. Sleep for the monitoring period;
        4. Check program run_time_ns again, calculate the difference;
        5. Close the fd.
      Signed-off-by: default avatarSong Liu <songliubraving@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20200430071506.1408910-2-songliubraving@fb.com
      d46edd67
  2. 30 Apr, 2020 3 commits
  3. 29 Apr, 2020 32 commits