1. 22 Mar, 2022 5 commits
  2. 21 Mar, 2022 23 commits
  3. 19 Mar, 2022 1 commit
  4. 18 Mar, 2022 11 commits
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Factor out fd returning from bpf_btf_find_by_name_kind · edc3ec09
      Kumar Kartikeya Dwivedi authored
      In next few patches, we need a helper that searches all kernel BTFs
      (vmlinux and module BTFs), and finds the type denoted by 'name' and
      'kind'. Turns out bpf_btf_find_by_name_kind already does the same thing,
      but it instead returns a BTF ID and optionally fd (if module BTF). This
      is used for relocating ksyms in BPF loader code (bpftool gen skel -L).
      
      We extract the core code out into a new helper bpf_find_btf_id, which
      returns the BTF ID in the return value, and BTF pointer in an out
      parameter. The reference for the returned BTF pointer is always raised,
      hence user must either transfer it (e.g. to a fd), or release it after
      use.
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220317115957.3193097-2-memxor@gmail.com
      edc3ec09
    • Andrii Nakryiko's avatar
      bpftool: Add BPF_TRACE_KPROBE_MULTI to attach type names table · 08063b4b
      Andrii Nakryiko authored
      BPF_TRACE_KPROBE_MULTI is a new attach type name, add it to bpftool's
      table. This fixes a currently failing CI bpftool check.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/bpf/20220318150106.2933343-1-andrii@kernel.org
      08063b4b
    • Daniel Borkmann's avatar
      Merge branch 'bpf-fix-sock-field-tests' · 63cc8e20
      Daniel Borkmann authored
      Jakub Sitnicki says:
      
      ====================
      I think we have reached a consensus [1] on how the test for the 4-byte load from
      bpf_sock->dst_port and bpf_sk_lookup->remote_port should look, so here goes v3.
      
      I will submit a separate set of patches for bpf_sk_lookup->remote_port tests.
      
      This series has been tested on x86_64 and s390 on top of recent bpf-next -
      ad13baf4 ("selftests/bpf: Test subprog jit when toggle bpf_jit_harden
      repeatedly").
      
        [1] https://lore.kernel.org/bpf/87k0cwxkzs.fsf@cloudflare.com/
      
      v2 -> v3:
      - Split what was previously patch 2 which was doing two things
      - Use BPF_TCP_* constants (Martin)
      - Treat the result of 4-byte load from dst_port as a 16-bit value (Martin)
      - Typo fixup and some rewording in patch 4 description
      v1 -> v2:
      - Limit read_sk_dst_port only to client traffic (patch 2)
      - Make read_sk_dst_port pass on litte- and big-endian (patch 3)
      
      v1: https://lore.kernel.org/bpf/20220225184130.483208-1-jakub@cloudflare.com/
      v2: https://lore.kernel.org/bpf/20220227202757.519015-1-jakub@cloudflare.com/
      ====================
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      63cc8e20
    • Jakub Sitnicki's avatar
      selftests/bpf: Fix test for 4-byte load from dst_port on big-endian · deb59400
      Jakub Sitnicki authored
      The check for 4-byte load from dst_port offset into bpf_sock is failing on
      big-endian architecture - s390. The bpf access converter rewrites the
      4-byte load to a 2-byte load from sock_common at skc_dport offset, as shown
      below.
      
        * s390 / llvm-objdump -S --no-show-raw-insn
      
        00000000000002a0 <sk_dst_port__load_word>:
              84:       r1 = *(u32 *)(r1 + 48)
              85:       w0 = 1
              86:       if w1 == 51966 goto +1 <LBB5_2>
              87:       w0 = 0
        00000000000002c0 <LBB5_2>:
              88:       exit
      
        * s390 / bpftool prog dump xlated
      
        _Bool sk_dst_port__load_word(struct bpf_sock * sk):
          35: (69) r1 = *(u16 *)(r1 +12)
          36: (bc) w1 = w1
          37: (b4) w0 = 1
          38: (16) if w1 == 0xcafe goto pc+1
          39: (b4) w0 = 0
          40: (95) exit
      
        * x86_64 / llvm-objdump -S --no-show-raw-insn
      
        00000000000002a0 <sk_dst_port__load_word>:
              84:       r1 = *(u32 *)(r1 + 48)
              85:       w0 = 1
              86:       if w1 == 65226 goto +1 <LBB5_2>
              87:       w0 = 0
        00000000000002c0 <LBB5_2>:
              88:       exit
      
        * x86_64 / bpftool prog dump xlated
      
        _Bool sk_dst_port__load_word(struct bpf_sock * sk):
          33: (69) r1 = *(u16 *)(r1 +12)
          34: (b4) w0 = 1
          35: (16) if w1 == 0xfeca goto pc+1
          36: (b4) w0 = 0
          37: (95) exit
      
      This leads to surprises if we treat the destination register contents as a
      32-bit value, ignoring the fact that in reality it contains a 16-bit value.
      
      On little-endian the register contents reflect the bpf_sock struct
      definition, where the lower 16-bits contain the port number:
      
      	struct bpf_sock {
      		...
      		__be16 dst_port;	/* offset 48 */
      		__u16 :16;
      		...
      	};
      
      However, on big-endian the register contents suggest that field the layout
      of bpf_sock struct is as so:
      
      	struct bpf_sock {
      		...
      		__u16 :16;		/* offset 48 */
      		__be16 dst_port;
      		...
      	};
      
      Account for this quirky access conversion in the test case exercising the
      4-byte load by treating the result as 16-bit wide.
      Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Link: https://lore.kernel.org/bpf/20220317113920.1068535-5-jakub@cloudflare.com
      deb59400
    • Jakub Sitnicki's avatar
      selftests/bpf: Use constants for socket states in sock_fields test · e06b5bbc
      Jakub Sitnicki authored
      Replace magic numbers in BPF code with constants from bpf.h, so that they
      don't require an explanation in the comments.
      Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Link: https://lore.kernel.org/bpf/20220317113920.1068535-4-jakub@cloudflare.com
      e06b5bbc
    • Jakub Sitnicki's avatar
      selftests/bpf: Check dst_port only on the client socket · 2d2202ba
      Jakub Sitnicki authored
      cgroup_skb/egress programs which sock_fields test installs process packets
      flying in both directions, from the client to the server, and in reverse
      direction.
      
      Recently added dst_port check relies on the fact that destination
      port (remote peer port) of the socket which sends the packet is known ahead
      of time. This holds true only for the client socket, which connects to the
      known server port.
      
      Filter out any traffic that is not egressing from the client socket in the
      BPF program that tests reading the dst_port.
      
      Fixes: 8f50f16f ("selftests/bpf: Extend verifier and bpf_sock tests for dst_port loads")
      Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Link: https://lore.kernel.org/bpf/20220317113920.1068535-3-jakub@cloudflare.com
      2d2202ba
    • Jakub Sitnicki's avatar
      selftests/bpf: Fix error reporting from sock_fields programs · a4c9fe0e
      Jakub Sitnicki authored
      The helper macro that records an error in BPF programs that exercise sock
      fields access has been inadvertently broken by adaptation work that
      happened in commit b18c1f0a ("bpf: selftest: Adapt sock_fields test to
      use skel and global variables").
      
      BPF_NOEXIST flag cannot be used to update BPF_MAP_TYPE_ARRAY. The operation
      always fails with -EEXIST, which in turn means the error never gets
      recorded, and the checks for errors always pass.
      
      Revert the change in update flags.
      
      Fixes: b18c1f0a ("bpf: selftest: Adapt sock_fields test to use skel and global variables")
      Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Link: https://lore.kernel.org/bpf/20220317113920.1068535-2-jakub@cloudflare.com
      a4c9fe0e
    • Andrii Nakryiko's avatar
      Merge branch 'Subskeleton support for BPF librariesThread-Topic: [PATCH bpf-next v4 0/5' · 60911970
      Andrii Nakryiko authored
      Delyan Kratunov says:
      
      ====================
      
      In the quest for ever more modularity, a new need has arisen - the ability to
      access data associated with a BPF library from a corresponding userspace library.
      The catch is that we don't want the userspace library to know about the structure of the
      final BPF object that the BPF library is linked into.
      
      In pursuit of this modularity, this patch series introduces *subskeletons.*
      Subskeletons are similar in use and design to skeletons with a couple of differences:
      
      1. The generated storage types do not rely on contiguous storage for the library's
      variables because they may be interspersed randomly throughout the final BPF object's sections.
      
      2. Subskeletons do not own objects and instead require a loaded bpf_object* to
      be passed at runtime in order to be initialized. By extension, symbols are resolved at
      runtime by parsing the final object's BTF.
      
      3. Subskeletons allow access to all global variables, programs, and custom maps. They also expose
      the internal maps *of the final object*. This allows bpf_var_skeleton objects to contain a bpf_map**
      instead of a section name.
      
      Changes since v3:
       - Re-add key/value type lookup for legacy user maps (fixing btf test)
       - Minor cleanups (missed sanitize_identifier call, error messages, formatting)
      
      Changes since v2:
       - Reuse SEC_NAME strict mode flag
       - Init bpf_map->btf_value_type_id on open for internal maps *and* user BTF maps
       - Test custom section names (.data.foo) and overlapping kconfig externs between the final object and the library
       - Minor review comments in gen.c & libbpf.c
      
      Changes since v1:
       - Introduced new strict mode knob for single-routine-in-.text compatibility behavior, which
         disproportionately affects library objects. bpftool works in 1.0 mode so subskeleton generation
         doesn't have to worry about this now.
       - Made bpf_map_btf_value_type_id available earlier and used it wherever applicable.
       - Refactoring in bpftool gen.c per review comments.
       - Subskels now use typeof() for array and func proto globals to avoid the need for runtime split btf.
       - Expanded the subskeleton test to include arrays, custom maps, extern maps, weak symbols, and kconfigs.
       - selftests/bpf/Makefile now generates a subskel.h for every skel.h it would make.
      
      For reference, here is a shortened subskeleton header:
      
      #ifndef __TEST_SUBSKELETON_LIB_SUBSKEL_H__
      #define __TEST_SUBSKELETON_LIB_SUBSKEL_H__
      
      struct test_subskeleton_lib {
      	struct bpf_object *obj;
      	struct bpf_object_subskeleton *subskel;
      	struct {
      		struct bpf_map *map2;
      		struct bpf_map *map1;
      		struct bpf_map *data;
      		struct bpf_map *rodata;
      		struct bpf_map *bss;
      		struct bpf_map *kconfig;
      	} maps;
      	struct {
      		struct bpf_program *lib_perf_handler;
      	} progs;
      	struct test_subskeleton_lib__data {
      		int *var6;
      		int *var2;
      		int *var5;
      	} data;
      	struct test_subskeleton_lib__rodata {
      		int *var1;
      	} rodata;
      	struct test_subskeleton_lib__bss {
      		struct {
      			int var3_1;
      			__s64 var3_2;
      		} *var3;
      		int *libout1;
      		typeof(int[4]) *var4;
      		typeof(int (*)()) *fn_ptr;
      	} bss;
      	struct test_subskeleton_lib__kconfig {
      		_Bool *CONFIG_BPF_SYSCALL;
      	} kconfig;
      
      static inline struct test_subskeleton_lib *
      test_subskeleton_lib__open(const struct bpf_object *src)
      {
      	struct test_subskeleton_lib *obj;
      	struct bpf_object_subskeleton *s;
      	int err;
      
      	...
      	s = (struct bpf_object_subskeleton *)calloc(1, sizeof(*s));
      	...
      
      	s->var_cnt = 9;
      	...
      
      	s->vars[0].name = "var6";
      	s->vars[0].map = &obj->maps.data;
      	s->vars[0].addr = (void**) &obj->data.var6;
        ...
      
      	/* maps */
      	...
      
      	/* programs */
      	s->prog_cnt = 1;
      	...
      
      	err = bpf_object__open_subskeleton(s);
        ...
      	return obj;
      }
      #endif /* __TEST_SUBSKELETON_LIB_SUBSKEL_H__ */
      ====================
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      60911970
    • Delyan Kratunov's avatar
      selftests/bpf: Test subskeleton functionality · 3cccbaa0
      Delyan Kratunov authored
      This patch changes the selftests/bpf Makefile to also generate
      a subskel.h for every skel.h it would have normally generated.
      
      Separately, it also introduces a new subskeleton test which tests
      library objects, externs, weak symbols, kconfigs, and user maps.
      Signed-off-by: default avatarDelyan Kratunov <delyank@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1bd24956940bbbfe169bb34f7f87b11df52ef011.1647473511.git.delyank@fb.com
      3cccbaa0
    • Delyan Kratunov's avatar
      bpftool: Add support for subskeletons · 00389c58
      Delyan Kratunov authored
      Subskeletons are headers which require an already loaded program to
      operate.
      
      For example, when a BPF library is linked into a larger BPF object file,
      the library userspace needs a way to access its own global variables
      without requiring knowledge about the larger program at build time.
      
      As a result, subskeletons require a loaded bpf_object to open().
      Further, they find their own symbols in the larger program by
      walking BTF type data at run time.
      
      At this time, programs, maps, and globals are supported through
      non-owning pointers.
      Signed-off-by: default avatarDelyan Kratunov <delyank@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/ca8a48b4841c72d285ecce82371bef4a899756cb.1647473511.git.delyank@fb.com
      00389c58
    • Delyan Kratunov's avatar
      libbpf: Add subskeleton scaffolding · 430025e5
      Delyan Kratunov authored
      In symmetry with bpf_object__open_skeleton(),
      bpf_object__open_subskeleton() performs the actual walking and linking
      of maps, progs, and globals described by bpf_*_skeleton objects.
      Signed-off-by: default avatarDelyan Kratunov <delyank@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/6942a46fbe20e7ebf970affcca307ba616985b15.1647473511.git.delyank@fb.com
      430025e5