1. 07 Aug, 2018 1 commit
    • Roman Gushchin's avatar
      bpf: introduce update_effective_progs() · 85fc4b16
      Roman Gushchin authored
      __cgroup_bpf_attach() and __cgroup_bpf_detach() functions have
      a good amount of duplicated code, which is possible to eliminate
      by introducing the update_effective_progs() helper function.
      
      The update_effective_progs() calls compute_effective_progs()
      and then in case of success it calls activate_effective_progs()
      for each descendant cgroup. In case of failure (OOM), it releases
      allocated prog arrays and return the error code.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarSong Liu <songliubraving@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      85fc4b16
  2. 04 Aug, 2018 1 commit
    • Jakub Kicinski's avatar
      nfp: bpf: xdp_adjust_tail support · 0c261593
      Jakub Kicinski authored
      Add support for adjust_tail.  There are no FW changes needed but add
      a FW capability just in case there would be any issue with previously
      released FW, or we will have to change the ABI in the future.
      
      The helper is trivial and shouldn't be used too often so just inline
      the body of the function.  We add the delta to locally maintained
      packet length register and check for overflow, since add of negative
      value must overflow if result is positive.  Note that if delta of 0
      would be allowed in the kernel this trick stops working and we need
      one more instruction to compare lengths before and after the change.
      Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      0c261593
  3. 03 Aug, 2018 1 commit
  4. 02 Aug, 2018 15 commits
    • Daniel Borkmann's avatar
      Merge branch 'bpf-cgroup-local-storage' · 82c018d7
      Daniel Borkmann authored
      Roman Gushchin says:
      
      ====================
      This patchset implements cgroup local storage for bpf programs.
      The main idea is to provide a fast accessible memory for storing
      various per-cgroup data, e.g. number of transmitted packets.
      
      Cgroup local storage looks as a special type of map for userspace,
      and is accessible using generic bpf maps API for reading and
      updating of the data. The (cgroup inode id, attachment type) pair
      is used as a map key.
      
      A user can't create new entries or destroy existing entries;
      it happens automatically when a user attaches/detaches a bpf program
      to a cgroup.
      
      From a bpf program's point of view, cgroup storage is accessible
      without lookup using the special get_local_storage() helper function.
      It takes a map fd as an argument. It always returns a valid pointer
      to the corresponding memory area.
      
      To implement such a lookup-free access a pointer to the cgroup
      storage is saved for an attachment of a bpf program to a cgroup,
      if required by the program. Before running the program, it's saved
      in a special global per-cpu variable, which is accessible from the
      get_local_storage() helper.
      
      This patchset implement only cgroup local storage, however the API
      is intentionally made extensible to support other local storage types
      further: e.g. thread local storage, socket local storage, etc.
      
      v7->v6:
        - fixed a use-after-free bug, caused by not clearing
          prog->aux->cgroup_storage pointer after releasing the map
      
      v6->v5:
        - fixed an error with returning -EINVAL instead of a pointer
      
      v5->v4:
        - fixed an issue in verifier (test that flags == 0 properly)
        - added a corresponding test
        - added a note about synchronization, sync docs to tools/uapi/...
        - switched the cgroup test to use XADD
        - added a check for attr->max_entries to be 0, and atter->max_flags
          to be sane
        - use bpf_uncharge_memlock() in bpf_uncharge_memlock()
        - rebased to bpf-next
      
      v4->v3:
        - fixed a leak in cgroup attachment code (discovered by Daniel)
        - cgroup storage map will be released if the corresponding
          bpf program failed to load by any reason
        - introduced bpf_uncharge_memlock() helper
      
      v3->v2:
        - fixed more build and sparse issues
        - rebased to bpf-next
      
      v2->v1:
        - fixed build issues
        - removed explicit rlimit calls in patch 14
        - rebased to bpf-next
      ====================
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Martin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      82c018d7
    • Roman Gushchin's avatar
      samples/bpf: extend test_cgrp2_attach2 test to use cgroup storage · 28ba0687
      Roman Gushchin authored
      The test_cgrp2_attach test covers bpf cgroup attachment code well,
      so let's re-use it for testing allocation/releasing of cgroup storage.
      
      The extension is pretty straightforward: the bpf program will use
      the cgroup storage to save the number of transmitted bytes.
      
      Expected output:
        $ ./test_cgrp2_attach2
        Attached DROP prog. This ping in cgroup /foo should fail...
        ping: sendmsg: Operation not permitted
        Attached DROP prog. This ping in cgroup /foo/bar should fail...
        ping: sendmsg: Operation not permitted
        Attached PASS prog. This ping in cgroup /foo/bar should pass...
        Detached PASS from /foo/bar while DROP is attached to /foo.
        This ping in cgroup /foo/bar should fail...
        ping: sendmsg: Operation not permitted
        Attached PASS from /foo/bar and detached DROP from /foo.
        This ping in cgroup /foo/bar should pass...
        ### override:PASS
        ### multi:PASS
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      28ba0687
    • Roman Gushchin's avatar
      selftests/bpf: add a cgroup storage test · 68cfa3ac
      Roman Gushchin authored
      Implement a test to cover the cgroup storage functionality.
      The test implements a bpf program which drops every second packet
      by using the cgroup storage as a persistent storage.
      
      The test also use the userspace API to check the data
      in the cgroup storage, alter it, and check that the loaded
      and attached bpf program sees the update.
      
      Expected output:
        $ ./test_cgroup_storage
        test_cgroup_storage:PASS
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      68cfa3ac
    • Roman Gushchin's avatar
      selftests/bpf: add verifier cgroup storage tests · d4c9f573
      Roman Gushchin authored
      Add the following verifier tests to cover the cgroup storage
      functionality:
      1) valid access to the cgroup storage
      2) invalid access: use regular hashmap instead of cgroup storage map
      3) invalid access: use invalid map fd
      4) invalid access: try access memory after the cgroup storage
      5) invalid access: try access memory before the cgroup storage
      6) invalid access: call get_local_storage() with non-zero flags
      
      For tests 2)-6) check returned error strings.
      
      Expected output:
        $ ./test_verifier
        #0/u add+sub+mul OK
        #0/p add+sub+mul OK
        #1/u DIV32 by 0, zero check 1 OK
        ...
        #280/p valid cgroup storage access OK
        #281/p invalid cgroup storage access 1 OK
        #282/p invalid cgroup storage access 2 OK
        #283/p invalid per-cgroup storage access 3 OK
        #284/p invalid cgroup storage access 4 OK
        #285/p invalid cgroup storage access 5 OK
        ...
        #649/p pass modified ctx pointer to helper, 2 OK
        #650/p pass modified ctx pointer to helper, 3 OK
        Summary: 901 PASSED, 0 SKIPPED, 0 FAILED
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      d4c9f573
    • Roman Gushchin's avatar
      bpf/test_run: support cgroup local storage · f42ee093
      Roman Gushchin authored
      Allocate a temporary cgroup storage to use for bpf program test runs.
      
      Because the test program is not actually attached to a cgroup,
      the storage is allocated manually just for the execution
      of the bpf program.
      
      If the program is executed multiple times, the storage is not zeroed
      on each run, emulating multiple runs of the program, attached to
      a real cgroup.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      f42ee093
    • Roman Gushchin's avatar
      bpftool: add support for CGROUP_STORAGE maps · 34a6bbb8
      Roman Gushchin authored
      Add BPF_MAP_TYPE_CGROUP_STORAGE maps to the list
      of maps types which bpftool recognizes.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      34a6bbb8
    • Roman Gushchin's avatar
      bpf: sync bpf.h to tools/ · c419cf52
      Roman Gushchin authored
      Sync cgroup storage related changes:
      1) new BPF_MAP_TYPE_CGROUP_STORAGE map type
      2) struct bpf_cgroup_sotrage_key definition
      3) get_local_storage() helper
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      c419cf52
    • Roman Gushchin's avatar
      bpf: introduce the bpf_get_local_storage() helper function · cd339431
      Roman Gushchin authored
      The bpf_get_local_storage() helper function is used
      to get a pointer to the bpf local storage from a bpf program.
      
      It takes a pointer to a storage map and flags as arguments.
      Right now it accepts only cgroup storage maps, and flags
      argument has to be 0. Further it can be extended to support
      other types of local storage: e.g. thread local storage etc.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      cd339431
    • Roman Gushchin's avatar
      bpf: don't allow create maps of cgroup local storages · 7b5dd2bd
      Roman Gushchin authored
      As there is one-to-one relation between a bpf program
      and cgroup local storage map, there is no sense in
      creating a map of cgroup local storage maps.
      
      Forbid it explicitly to avoid possible side effects.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      7b5dd2bd
    • Roman Gushchin's avatar
      bpf/verifier: introduce BPF_PTR_TO_MAP_VALUE · 3e6a4b3e
      Roman Gushchin authored
      BPF_MAP_TYPE_CGROUP_STORAGE maps are special in a way
      that the access from the bpf program side is lookup-free.
      That means the result is guaranteed to be a valid
      pointer to the cgroup storage; no NULL-check is required.
      
      This patch introduces BPF_PTR_TO_MAP_VALUE return type,
      which is required to cause the verifier accept programs,
      which are not checking the map value pointer for being NULL.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      3e6a4b3e
    • Roman Gushchin's avatar
      bpf: extend bpf_prog_array to store pointers to the cgroup storage · 394e40a2
      Roman Gushchin authored
      This patch converts bpf_prog_array from an array of prog pointers
      to the array of struct bpf_prog_array_item elements.
      
      This allows to save a cgroup storage pointer for each bpf program
      efficiently attached to a cgroup.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      394e40a2
    • Roman Gushchin's avatar
      bpf: allocate cgroup storage entries on attaching bpf programs · d7bf2c10
      Roman Gushchin authored
      If a bpf program is using cgroup local storage, allocate
      a bpf_cgroup_storage structure automatically on attaching the program
      to a cgroup and save the pointer into the corresponding bpf_prog_list
      entry.
      Analogically, release the cgroup local storage on detaching
      of the bpf program.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      d7bf2c10
    • Roman Gushchin's avatar
      bpf: pass a pointer to a cgroup storage using pcpu variable · aa0ad5b0
      Roman Gushchin authored
      This commit introduces the bpf_cgroup_storage_set() helper,
      which will be used to pass a pointer to a cgroup storage
      to the bpf helper.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      aa0ad5b0
    • Roman Gushchin's avatar
      bpf: introduce cgroup storage maps · de9cbbaa
      Roman Gushchin authored
      This commit introduces BPF_MAP_TYPE_CGROUP_STORAGE maps:
      a special type of maps which are implementing the cgroup storage.
      
      >From the userspace point of view it's almost a generic
      hash map with the (cgroup inode id, attachment type) pair
      used as a key.
      
      The only difference is that some operations are restricted:
        1) a user can't create new entries,
        2) a user can't remove existing entries.
      
      The lookup from userspace is o(log(n)).
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      de9cbbaa
    • Roman Gushchin's avatar
      bpf: add ability to charge bpf maps memory dynamically · 0a4c58f5
      Roman Gushchin authored
      This commits extends existing bpf maps memory charging API
      to support dynamic charging/uncharging.
      
      This is required to account memory used by maps,
      if all entries are created dynamically after
      the map initialization.
      Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      0a4c58f5
  5. 31 Jul, 2018 9 commits
  6. 27 Jul, 2018 13 commits