1. 01 2月, 2017 3 次提交
    • J
      tools lib bpf: Add bpf_object__pin() · d5148d85
      Joe Stringer 提交于
      Add a new API to pin a BPF object to the filesystem. The user can
      specify the path within a BPF filesystem to pin the object.
      Programs will be pinned under a subdirectory named the same as the
      program, with each instance appearing as a numbered file under that
      directory, and maps will be pinned under the path using the name of
      the map as the file basename.
      
      For example, with the directory '/sys/fs/bpf/foo' and a BPF object which
      contains two instances of a program named 'bar', and a map named 'baz':
      
      /sys/fs/bpf/foo/bar/0
      /sys/fs/bpf/foo/bar/1
      /sys/fs/bpf/foo/baz
      Signed-off-by: NJoe Stringer <joe@ovn.org>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Wang Nan <wangnan0@huawei.com>
      Cc: netdev@vger.kernel.org
      Link: http://lkml.kernel.org/r/20170126212001.14103-4-joe@ovn.org
      [ Check snprintf >= for truncation, as snprintf(bf, size, ...) == size also means truncation ]
      Signed-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      d5148d85
    • J
      tools lib bpf: Add bpf_map__pin() · b6989f35
      Joe Stringer 提交于
      Add a new API to pin a BPF map to the filesystem. The user can specify
      the path full path within a BPF filesystem to pin the map.
      Signed-off-by: NJoe Stringer <joe@ovn.org>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Wang Nan <wangnan0@huawei.com>
      Cc: netdev@vger.kernel.org
      Link: http://lkml.kernel.org/r/20170126212001.14103-3-joe@ovn.orgSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      b6989f35
    • J
      tools lib bpf: Add BPF program pinning APIs · f367540c
      Joe Stringer 提交于
      Add new APIs to pin a BPF program (or specific instances) to the
      filesystem.  The user can specify the path full path within a BPF
      filesystem to pin the program.
      
      bpf_program__pin_instance(prog, path, n) will pin the nth instance of
      'prog' to the specified path.
      
      bpf_program__pin(prog, path) will create the directory 'path' (if it
      does not exist) and pin each instance within that directory. For
      instance, path/0, path/1, path/2.
      
      Committer notes:
      
      - Add missing headers for mkdir()
      
      - Check strdup() for failure
      
      - Check snprintf >= size, not >, as == also means truncated, see 'man
        snprintf', return value.
      
      - Conditionally define BPF_FS_MAGIC, as it isn't in magic.h in older
        systems and we're not yet having a tools/include/uapi/linux/magic.h
        copy.
      
      - Do not include linux/magic.h, not present in older distros.
      Signed-off-by: NJoe Stringer <joe@ovn.org>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Wang Nan <wangnan0@huawei.com>
      Cc: netdev@vger.kernel.org
      Link: http://lkml.kernel.org/r/20170126212001.14103-2-joe@ovn.orgSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      f367540c
  2. 26 1月, 2017 4 次提交
  3. 16 12月, 2016 1 次提交
  4. 29 11月, 2016 2 次提交
  5. 25 11月, 2016 1 次提交
    • E
      tools lib bpf: Fix maps resolution · 4708bbda
      Eric Leblond 提交于
      It is not correct to assimilate the elf data of the maps section to an
      array of map definition. In fact the sizes differ. The offset provided
      in the symbol section has to be used instead.
      
      This patch fixes a bug causing a elf with two maps not to load
      correctly.
      
      Wang Nan added:
      
      This patch requires a name for each BPF map, so array of BPF maps is not
      allowed. This restriction is reasonable, because kernel verifier forbid
      indexing BPF map from such array unless the index is a fixed value, but
      if the index is fixed why not merging it into name?
      
      For example:
      
      Program like this:
        ...
        unsigned long cpu = get_smp_processor_id();
        int *pval = map_lookup_elem(&map_array[cpu], &key);
        ...
      
      Generates bytecode like this:
      
      0: (b7) r1 = 0
      1: (63) *(u32 *)(r10 -4) = r1
      2: (b7) r1 = 680997
      3: (63) *(u32 *)(r10 -8) = r1
      4: (85) call 8
      5: (67) r0 <<= 4
      6: (18) r1 = 0x112dd000
      8: (0f) r0 += r1
      9: (bf) r2 = r10
      10: (07) r2 += -4
      11: (bf) r1 = r0
      12: (85) call 1
      
      Where instruction 8 is the computation, 8 and 11 render r1 to an invalid
      value for function map_lookup_elem, causes verifier report error.
      Signed-off-by: NEric Leblond <eric@regit.org>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Wang Nan <wangnan0@huawei.com>
      [ Merge bpf_object__init_maps_name into bpf_object__init_maps.
        Fix segfault for buggy BPF script Validate obj->maps ]
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/20161115040617.69788-5-wangnan0@huawei.comSigned-off-by: NWang Nan <wangnan0@huawei.com>
      Signed-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      4708bbda
  6. 26 7月, 2016 1 次提交
  7. 14 7月, 2016 2 次提交
  8. 05 7月, 2016 1 次提交
  9. 29 6月, 2016 1 次提交
  10. 07 6月, 2016 7 次提交
  11. 26 1月, 2016 1 次提交
    • W
      perf bpf: Check relocation target section · 666810e8
      Wang Nan 提交于
      Libbpf should check the target section before doing relocation to ensure
      the relocation is correct. If not, a bug in LLVM causes an error. See
      [1].  Also, if an incorrect BPF script uses both global variable and
      map, global variable whould be treated as map and be relocated without
      error.
      
      This patch saves the id of the map section into obj->efile and compare
      target section of a relocation symbol against it during relocation.
      
      Previous patch introduces a test case about this problem.  After this
      patch:
      
        # ~/perf test BPF
        37: Test BPF filter                                          :
        37.1: Test basic BPF filtering                               : Ok
        37.2: Test BPF prologue generation                           : Ok
        37.3: Test BPF relocation checker                            : Ok
      
        # perf test -v BPF
        ...
        37.3: Test BPF relocation checker                            :
        ...
        libbpf: loading object '[bpf_relocation_test]' from buffer
        libbpf: section .strtab, size 126, link 0, flags 0, type=3
        libbpf: section .text, size 0, link 0, flags 6, type=1
        libbpf: section .data, size 0, link 0, flags 3, type=1
        libbpf: section .bss, size 0, link 0, flags 3, type=8
        libbpf: section func=sys_write, size 104, link 0, flags 6, type=1
        libbpf: found program func=sys_write
        libbpf: section .relfunc=sys_write, size 16, link 10, flags 0, type=9
        libbpf: section maps, size 16, link 0, flags 3, type=1
        libbpf: maps in [bpf_relocation_test]: 16 bytes
        libbpf: section license, size 4, link 0, flags 3, type=1
        libbpf: license of [bpf_relocation_test] is GPL
        libbpf: section version, size 4, link 0, flags 3, type=1
        libbpf: kernel version of [bpf_relocation_test] is 40400
        libbpf: section .symtab, size 144, link 1, flags 0, type=2
        libbpf: map 0 is "my_table"
        libbpf: collecting relocating info for: 'func=sys_write'
        libbpf: Program 'func=sys_write' contains non-map related relo data pointing to section 65522
        bpf: failed to load buffer
        Compile BPF program failed.
        test child finished with 0
        ---- end ----
        Test BPF filter subtest 2: Ok
      
      [1] https://llvm.org/bugs/show_bug.cgi?id=26243Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@kernel.org>
      Tested-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Li Zefan <lizefan@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Will Deacon <will.deacon@arm.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1453715801-7732-3-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      666810e8
  12. 11 12月, 2015 2 次提交
    • W
      tools lib bpf: Fetch map names from correct strtab · 77ba9a5b
      Wang Nan 提交于
      Namhyung Kim pointed out a potential problem in original code that it
      fetches names of maps from section header string table, which is used
      to store section names.
      
      Original code doesn't cause error because of a LLVM behavior that, it
      combines shstrtab into strtab. For example:
      
       $ echo 'int func() {return 0;}' | x86_64-oe-linux-clang -x c -o temp.o -c -
       $ readelf -h ./temp.o
       ELF Header:
         Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00
         ...
         Section header string table index: 1
       $ readelf -S ./temp.o
       There are 10 section headers, starting at offset 0x288:
      
       Section Headers:
         [Nr] Name              Type             Address           Offset
              Size              EntSize          Flags  Link  Info  Align
         [ 0]                   NULL             0000000000000000  00000000
              0000000000000000  0000000000000000           0     0     0
         [ 1] .strtab           STRTAB           0000000000000000  00000230
              0000000000000051  0000000000000000           0     0     1
              ...
       $ readelf -p .strtab ./temp.o
      
       String dump of section '.strtab':
         [     1]  .text
         [     7]  .comment
         [    10]  .bss
         [    15]  .note.GNU-stack
         [    25]  .rela.eh_frame
         [    34]  func
         [    39]  .strtab
         [    41]  .symtab
         [    49]  .data
         [    4f]  -
      
       $ readelf -p .shstrtab ./temp.o
       readelf: Warning: Section '.shstrtab' was not dumped because it does not exist!
      
      Where, 'section header string table index' points to '.strtab', and
      symbol names are also stored there.
      
      However, in case of gcc:
      
       $ echo 'int func() {return 0;}' | gcc -x c -o temp.o -c -
       $ readelf -p .shstrtab ./temp.o
      
       String dump of section '.shstrtab':
         [     1]  .symtab
         [     9]  .strtab
         [    11]  .shstrtab
         [    1b]  .text
         [    21]  .data
         [    27]  .bss
         [    2c]  .comment
         [    35]  .note.GNU-stack
         [    45]  .rela.eh_frame
       $ readelf -p .strtab ./temp.o
      
       String dump of section '.strtab':
         [     1]  func
      
      They are separated sections.
      
      Although original code doesn't cause error, we'd better use canonical
      method for fetching symbol names to avoid potential behavior changing.
      This patch learns from readelf's code, fetches string from sh_link
      of .symbol section.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Reported-and-Acked-by: NNamhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1449541544-67621-3-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      77ba9a5b
    • W
      tools lib bpf: Check return value of strdup when reading map names · 973170e6
      Wang Nan 提交于
      Commit 561bbcca ("tools lib bpf:
      Extract and collect map names from BPF object file") forgets checking
      return value of strdup(). This patch fixes it. It also checks names
      pointer before strcmp() for safety.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NNamhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Fixes: 561bbcca ("tools lib bpf: Extract and collect map names from BPF object file")
      Link: http://lkml.kernel.org/r/1449541544-67621-2-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      973170e6
  13. 28 11月, 2015 2 次提交
  14. 19 11月, 2015 1 次提交
    • W
      bpf tools: Load a program with different instances using preprocessor · b580563e
      Wang Nan 提交于
      This patch is a preparation for BPF prologue support which allows
      generating a series of BPF bytecode for fetching kernel data before
      calling program code. With the newly introduced multiple instances
      support, perf is able to create different prologues for different kprobe
      points.
      
      Before this patch, a bpf_program can be loaded into kernel only once,
      and get the only resulting fd. What this patch does is to allow creating
      and loading different variants of one bpf_program, then fetching their
      fds.
      
      Here we describe the basic idea in this patch. The detailed description
      of the newly introduced APIs can be found in comments in the patch body.
      
      The key of this patch is the new mechanism in bpf_program__load().
      Instead of loading BPF program into kernel directly, it calls a
      'pre-processor' to generate program instances which would be finally
      loaded into the kernel based on the original code. To enable the
      generation of multiple instances, libbpf passes an index to the
      pre-processor so it know which instance is being loaded.
      
      Pre-processor should be called from libbpf's user (perf) using
      bpf_program__set_prep(). The number of instances and the relationship
      between indices and the target instance should be clear when calling
      bpf_program__set_prep().
      
      To retrieve a fd for a specific instance of a program,
      bpf_program__nth_fd() is introduced. It returns the resulting fd
      according to index.
      Signed-off-by: NHe Kuang <hekuang@huawei.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1447675815-166222-8-git-send-email-wangnan0@huawei.comSigned-off-by: NWang Nan <wangnan0@huawei.com>
      [ Enclosed multi-line if/else blocks with {}, (*func_ptr)() -> func_ptr() ]
      Signed-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      b580563e
  15. 07 11月, 2015 2 次提交
    • W
      bpf tools: Add new API bpf_object__get_kversion() · 45825d8a
      Wang Nan 提交于
      bpf_object__get_kversion() can be used to fetch value of object's
      'version' section. Following patch will use it for error reporting.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1446817783-86722-3-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      45825d8a
    • W
      bpf tools: Improve libbpf error reporting · 6371ca3b
      Wang Nan 提交于
      In this patch, a series of libbpf specific error numbers and
      libbpf_strerror() are introduced to help reporting errors.
      
      Functions are updated to pass correct the error number through the
      CHECK_ERR() macro.
      
      All users of bpf_object__open{_buffer}() and bpf_program__title() in
      perf are modified accordingly. In addition, due to the error codes
      changing, bpf__strerror_load() is also modified to use them.
      
      bpf__strerror_head() is also changed accordingly so it can parse libbpf
      errors. bpf_loader_strerror() is introduced for that purpose, and will
      be improved by the following patch.
      
      load_program() is improved not to dump log buffer if it is empty. log
      buffer is also used to deduce whether the error was caused by an invalid
      program or other problem.
      
      v1 -> v2:
      
       - Using macro for error code.
      
       - Fetch error message based on array index, eliminate for-loop.
      
       - Use log buffer to detect the reason of failure. 3 new error code
         are introduced to replace LIBBPF_ERRNO__LOAD.
      
      In v1:
      
        # perf record -e ./test_ill_program.o ls
        event syntax error: './test_ill_program.o'
                             \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
        SKIP
      
        # perf record -e ./test_kversion_nomatch_program.o ls
        event syntax error: './test_kversion_nomatch_program.o'
                             \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
        SKIP
      
        # perf record -e ./test_big_program.o ls
        event syntax error: './test_big_program.o'
                             \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
        SKIP
      
        In v2:
      
        # perf record -e ./test_ill_program.o ls
        event syntax error: './test_ill_program.o'
                             \___ Kernel verifier blocks program loading
        SKIP
      
        # perf record -e ./test_kversion_nomatch_program.o
        event syntax error: './test_kversion_nomatch_program.o'
                             \___ Incorrect kernel version
        SKIP
        (Will be further improved by following patches)
      
        # perf record -e ./test_big_program.o
        event syntax error: './test_big_program.o'
                             \___ Program too big
        SKIP
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Tested-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1446817783-86722-2-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      6371ca3b
  16. 03 11月, 2015 1 次提交
  17. 01 9月, 2015 1 次提交
    • W
      bpf tools: New API to get name from a BPF object · acf860ae
      Wang Nan 提交于
      Before this patch there's no way to connect a loaded bpf object
      to its source file. However, during applying perf's '--filter' to BPF
      object, without this connection makes things harder, because perf loads
      all programs together, but '--filter' setting is for each object.
      
      The API of bpf_object__open_buffer() is changed to allow passing a name.
      Fortunately, at this time there's only one user of it (perf test LLVM),
      so we change it together.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Cc: Alexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1440742821-44548-2-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      acf860ae
  18. 07 8月, 2015 7 次提交
    • W
      bpf tools: Link all bpf objects onto a list · 9a208eff
      Wang Nan 提交于
      To allow enumeration of all bpf_objects, keep them in a list (hidden to
      caller). bpf_object__for_each_safe() is introduced to do this iteration.
      It is safe even user close the object during iteration.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-23-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      9a208eff
    • W
      bpf tools: Introduce accessors for struct bpf_program · aa9b1ac3
      Wang Nan 提交于
      This patch introduces accessors for user of libbpf to retrieve section
      name and fd of a opened/loaded eBPF program. 'struct bpf_prog_handler'
      is used for that purpose. Accessors of programs section name and file
      descriptor are provided. Set/get private data are also impelmented.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Link: http://lkml.kernel.org/r/1435716878-189507-21-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      aa9b1ac3
    • W
      bpf tools: Load eBPF programs in object files into kernel · 55cffde2
      Wang Nan 提交于
      This patch utilizes previous introduced bpf_load_program to load
      programs in the ELF file into kernel. Result is stored in 'fd' field in
      'struct bpf_program'.
      
      During loading, it allocs a log buffer and free it before return.  Note
      that that buffer is not passed to bpf_load_program() if the first
      loading try is successful. Doesn't use a statically allocated log buffer
      to avoid potention multi-thread problem.
      
      Instructions collected during opening is cleared after loading.
      
      load_program() is created for loading a 'struct bpf_insn' array into
      kernel, bpf_program__load() calls it. By this design we have a function
      loads instructions into kernel. It will be used by further patches,
      which creates different instances from a program and load them into
      kernel.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-20-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      55cffde2
    • W
      bpf tools: Relocate eBPF programs · 8a47a6c5
      Wang Nan 提交于
      If an eBPF program accesses a map, LLVM generates a load instruction
      which loads an absolute address into a register, like this:
      
        ld_64   r1, <MCOperand Expr:(mymap)>
        ...
        call    2
      
      That ld_64 instruction will be recorded in relocation section.
      To enable the usage of that map, relocation must be done by replacing
      the immediate value by real map file descriptor so it can be found by
      eBPF map functions.
      
      This patch to the relocation work based on information collected by
      patches:
      
      'bpf tools: Collect symbol table from SHT_SYMTAB section',
      'bpf tools: Collect relocation sections from SHT_REL sections'
      and
      'bpf tools: Record map accessing instructions for each program'.
      
      For each instruction which needs relocation, it inject corresponding
      file descriptor to imm field. As a part of protocol, src_reg is set to
      BPF_PSEUDO_MAP_FD to notify kernel this is a map loading instruction.
      
      This is the final part of map relocation patch. The principle of map
      relocation is described in commit message of 'bpf tools: Collect symbol
      table from SHT_SYMTAB section'.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-18-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      8a47a6c5
    • W
      bpf tools: Create eBPF maps defined in an object file · 52d3352e
      Wang Nan 提交于
      This patch creates maps based on 'map' section in object file using
      bpf_create_map(), and stores the fds into an array in 'struct
      bpf_object'.
      
      Previous patches parse ELF object file and collects required data, but
      doesn't play with the kernel. They belong to the 'opening' phase. This
      patch is the first patch in 'loading' phase. The 'loaded' field is
      introduced in 'struct bpf_object' to avoid loading an object twice,
      because the loading phase clears resources collected during the opening
      which becomes useless after loading. In this patch, maps_buf is cleared.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-17-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      52d3352e
    • W
      bpf tools: Record map accessing instructions for each program · 34090915
      Wang Nan 提交于
      This patch records the indices of instructions which are needed to be
      relocated. That information is saved in the 'reloc_desc' field in
      'struct bpf_program'. In the loading phase (this patch takes effect in
      the opening phase), the collected instructions will be replaced by map
      loading instructions.
      
      Since we are going to close the ELF file and clear all data at the end
      of the 'opening' phase, the ELF information will no longer be valid in
      the 'loading' phase. We have to locate the instructions before maps are
      loaded, instead of directly modifying the instruction.
      
      'struct bpf_map_def' is introduced in this patch to let us know how many
      maps are defined in the object.
      
      This is the third part of map relocation. The principle of map relocation
      is described in commit message of 'bpf tools: Collect symbol table from
      SHT_SYMTAB section'.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-15-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      34090915
    • W
      bpf tools: Collect relocation sections from SHT_REL sections · b62f06e8
      Wang Nan 提交于
      This patch collects relocation sections into 'struct object'.  Such
      sections are used for connecting maps to bpf programs. 'reloc' field in
      'struct bpf_object' is introduced for storing such information.
      
      This patch simply store the data into 'reloc' field. Following patch
      will parse them to know the exact instructions which are needed to be
      relocated.
      
      Note that the collected data will be invalid after ELF object file is
      closed.
      
      This is the second patch related to map relocation. The first one is
      'bpf tools: Collect symbol table from SHT_SYMTAB section'. The
      principle of map relocation is described in its commit message.
      Signed-off-by: NWang Nan <wangnan0@huawei.com>
      Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
      Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: David Ahern <dsahern@gmail.com>
      Cc: He Kuang <hekuang@huawei.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Kaixu Xia <xiakaixu@huawei.com>
      Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Zefan Li <lizefan@huawei.com>
      Cc: pi3orama@163.com
      Link: http://lkml.kernel.org/r/1435716878-189507-14-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      b62f06e8