All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC bpf-next 0/7] Add bpf_link based TC-BPF API
@ 2021-05-28 19:59 Kumar Kartikeya Dwivedi
  2021-05-28 19:59 ` [PATCH RFC bpf-next 1/7] net: sched: refactor cls_bpf creation code Kumar Kartikeya Dwivedi
                   ` (8 more replies)
  0 siblings, 9 replies; 48+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2021-05-28 19:59 UTC (permalink / raw)
  To: bpf
  Cc: Kumar Kartikeya Dwivedi, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Jamal Hadi Salim, Vlad Buslov,
	Cong Wang, Jiri Pirko, David S. Miller, Jakub Kicinski,
	Joe Stringer, Quentin Monnet, Jesper Dangaard Brouer,
	Toke Høiland-Jørgensen, netdev

This is the first RFC version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and
introduces fd based ownership for such TC filters. Netlink cannot delete or
replace such filters, but the bpf_link is severed on indirect destruction of the
filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure
that filters remain attached beyond process lifetime, the usual bpf_link fd
pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel
API and libbpf helper mirrors the semantics of the netlink based TC-BPF API
merged recently. This means that we start by always setting direct action mode,
protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more
options in the future, they can be easily exposed through the bpf_link API in
the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be
reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed
because our bpf_link object is tied to the cls_bpf_prog object. Tying it to
tcf_proto would be weird, because the update path has to replace offloaded bpf
prog, which happens using internal cls_bpf helpers, and would in general be more
code to abstract over an operation that is unlikely to be implemented for other
filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of
obtaining block reference, creating the filter object, and then calls the
bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after
setting up the internal structures. An optimization is made to not keep around
resources for extended actions, which is explained in a code comment as it wasn't
immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports
replacing the bpf_prog, we can skip tc filter's change path by reusing the
filter object but swapping its bpf_prog. This takes care of replacing the
offloaded prog as well (if that fails, update is aborted). So far however,
tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter
would never be modified concurrently. This is no longer true, and to not
penalize the classify hot path, we also cannot impose serialization around
its load. Hence the load is changed to READ_ONCE, so that the pointer value is
always consistent. Due to invocation in a RCU critical section, the lifetime of
the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning
function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions
that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog.  Doing it this way
allows us to implement update in a lightweight manner without having to recreate
a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are
numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when
cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link
specific update operation to tcf_proto ops.

The downside with tying this to the module is having to export bpf_link
management functions and introducing a tcf_proto op. Hopefully the cost of
another operation func pointer is not big enough (as there is only one ops
struct per module).

This first version is to collect feedback on the approach and get ideas if there
is a better way to do this.

Kumar Kartikeya Dwivedi (7):
  net: sched: refactor cls_bpf creation code
  bpf: export bpf_link functions for modules
  net: sched: add bpf_link API for bpf classifier
  net: sched: add lightweight update path for cls_bpf
  tools: bpf.h: sync with kernel sources
  libbpf: add bpf_link based TC-BPF management API
  libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 138 ++++++-
 net/sched/cls_bpf.c                           | 386 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   5 +
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 934 insertions(+), 44 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

-- 
2.31.1


^ permalink raw reply	[flat|nested] 48+ messages in thread
* Re: [PATCH RFC bpf-next 3/7] net: sched: add bpf_link API for bpf classifier
@ 2021-05-28 23:44 kernel test robot
  0 siblings, 0 replies; 48+ messages in thread
From: kernel test robot @ 2021-05-28 23:44 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 5220 bytes --]

CC: kbuild-all(a)lists.01.org
In-Reply-To: <20210528195946.2375109-4-memxor@gmail.com>
References: <20210528195946.2375109-4-memxor@gmail.com>
TO: Kumar Kartikeya Dwivedi <memxor@gmail.com>

Hi Kumar,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Add-bpf_link-based-TC-BPF-API/20210529-040147
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
:::::: branch date: 4 hours ago
:::::: commit date: 4 hours ago
config: x86_64-randconfig-b001-20210528 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 6505c630407c5feec818f0bb1c284f9eeebf2071)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # apt-get install iwyu # include-what-you-use
        # https://github.com/0day-ci/linux/commit/7f3d2ad7de9869028e50b1749a8144ccb75a76b8
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Add-bpf_link-based-TC-BPF-API/20210529-040147
        git checkout 7f3d2ad7de9869028e50b1749a8144ccb75a76b8
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross C=1 CHECK=iwyu ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


iwyu warnings: (new ones prefixed by >>)
   kernel/bpf/syscall.c:30:1: iwyu: warning: superfluous #include <linux/bpf_lsm.h>
   kernel/bpf/syscall.c:6:1: iwyu: warning: superfluous #include <linux/bpf_trace.h>
   kernel/bpf/syscall.c:34:1: iwyu: warning: superfluous #include <linux/memcontrol.h>
   kernel/bpf/syscall.c:29:1: iwyu: warning: superfluous #include <linux/pgtable.h>
   kernel/bpf/syscall.c:31:1: iwyu: warning: superfluous #include <linux/poll.h>
   kernel/bpf/syscall.c:12:1: iwyu: warning: superfluous #include <linux/sched/signal.h>
   kernel/bpf/syscall.c:24:1: iwyu: warning: superfluous #include <linux/timekeeping.h>
>> kernel/bpf/syscall.c:4:1: iwyu: warning: superfluous #include <net/pkt_cls.h>

vim +4 kernel/bpf/syscall.c

7f3d2ad7de9869 Kumar Kartikeya Dwivedi 2021-05-29  @4  #include <net/pkt_cls.h>
99c55f7d47c0dc Alexei Starovoitov      2014-09-26   5  #include <linux/bpf.h>
a67edbf4fb6dea Daniel Borkmann         2017-01-25   6  #include <linux/bpf_trace.h>
f4364dcfc86df7 Sean Young              2018-05-27   7  #include <linux/bpf_lirc.h>
4a1e7c0c63e02d Toke Høiland-Jørgensen  2020-09-29   8  #include <linux/bpf_verifier.h>
f56a653c1fd13a Martin KaFai Lau        2018-04-18   9  #include <linux/btf.h>
99c55f7d47c0dc Alexei Starovoitov      2014-09-26  10  #include <linux/syscalls.h>
99c55f7d47c0dc Alexei Starovoitov      2014-09-26  11  #include <linux/slab.h>
3f07c0144132e4 Ingo Molnar             2017-02-08  12  #include <linux/sched/signal.h>
d407bd25a204bd Daniel Borkmann         2017-01-18  13  #include <linux/vmalloc.h>
d407bd25a204bd Daniel Borkmann         2017-01-18  14  #include <linux/mmzone.h>
99c55f7d47c0dc Alexei Starovoitov      2014-09-26  15  #include <linux/anon_inodes.h>
41bdc4b40ed6fb Yonghong Song           2018-05-24  16  #include <linux/fdtable.h>
db20fd2b01087b Alexei Starovoitov      2014-09-26  17  #include <linux/file.h>
41bdc4b40ed6fb Yonghong Song           2018-05-24  18  #include <linux/fs.h>
09756af46893c1 Alexei Starovoitov      2014-09-26  19  #include <linux/license.h>
09756af46893c1 Alexei Starovoitov      2014-09-26  20  #include <linux/filter.h>
535e7b4b5ef220 Mickaël Salaün          2016-11-13  21  #include <linux/kernel.h>
dc4bb0e2356149 Martin KaFai Lau        2017-06-05  22  #include <linux/idr.h>
cb4d2b3f03d8ee Martin KaFai Lau        2017-09-27  23  #include <linux/cred.h>
cb4d2b3f03d8ee Martin KaFai Lau        2017-09-27  24  #include <linux/timekeeping.h>
cb4d2b3f03d8ee Martin KaFai Lau        2017-09-27  25  #include <linux/ctype.h>
9ef09e35e521bf Mark Rutland            2018-05-03  26  #include <linux/nospec.h>
bae141f54be83b Daniel Borkmann         2019-12-06  27  #include <linux/audit.h>
ccfe29eb29c2ed Alexei Starovoitov      2019-10-15  28  #include <uapi/linux/btf.h>
ca5999fde0a176 Mike Rapoport           2020-06-08  29  #include <linux/pgtable.h>
9e4e01dfd3254c KP Singh                2020-03-29  30  #include <linux/bpf_lsm.h>
457f44363a8894 Andrii Nakryiko         2020-05-29  31  #include <linux/poll.h>
a3fd7ceee05431 Jakub Sitnicki          2020-05-31  32  #include <linux/bpf-netns.h>
1e6c62a8821557 Alexei Starovoitov      2020-08-27  33  #include <linux/rcupdate_trace.h>
48edc1f78aabeb Roman Gushchin          2020-12-01  34  #include <linux/memcontrol.h>
99c55f7d47c0dc Alexei Starovoitov      2014-09-26  35  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 38270 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

end of thread, other threads:[~2021-06-21 13:55 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-28 19:59 [PATCH RFC bpf-next 0/7] Add bpf_link based TC-BPF API Kumar Kartikeya Dwivedi
2021-05-28 19:59 ` [PATCH RFC bpf-next 1/7] net: sched: refactor cls_bpf creation code Kumar Kartikeya Dwivedi
2021-05-28 19:59 ` [PATCH RFC bpf-next 2/7] bpf: export bpf_link functions for modules Kumar Kartikeya Dwivedi
2021-05-28 19:59 ` [PATCH RFC bpf-next 3/7] net: sched: add bpf_link API for bpf classifier Kumar Kartikeya Dwivedi
2021-05-28 22:37   ` kernel test robot
2021-05-28 23:18   ` kernel test robot
2021-06-02 20:56   ` Andrii Nakryiko
2021-05-28 19:59 ` [PATCH RFC bpf-next 4/7] net: sched: add lightweight update path for cls_bpf Kumar Kartikeya Dwivedi
2021-05-28 19:59 ` [PATCH RFC bpf-next 5/7] tools: bpf.h: sync with kernel sources Kumar Kartikeya Dwivedi
2021-05-28 19:59 ` [PATCH RFC bpf-next 6/7] libbpf: add bpf_link based TC-BPF management API Kumar Kartikeya Dwivedi
2021-06-02 21:03   ` Andrii Nakryiko
2021-05-28 19:59 ` [PATCH RFC bpf-next 7/7] libbpf: add selftest for " Kumar Kartikeya Dwivedi
2021-06-02 21:09 ` [PATCH RFC bpf-next 0/7] Add bpf_link based TC-BPF API Andrii Nakryiko
2021-06-02 21:45   ` Kumar Kartikeya Dwivedi
2021-06-02 23:50     ` Alexei Starovoitov
2021-06-04  6:43       ` Kumar Kartikeya Dwivedi
2021-06-06 23:37 ` Cong Wang
2021-06-07  3:37   ` Kumar Kartikeya Dwivedi
2021-06-07  5:18     ` Cong Wang
2021-06-07  6:07       ` Kumar Kartikeya Dwivedi
2021-06-08  2:00         ` Cong Wang
2021-06-08  7:19           ` Kumar Kartikeya Dwivedi
2021-06-08 15:39             ` Alexei Starovoitov
2021-06-11  2:10               ` Cong Wang
2021-06-11  2:00             ` Cong Wang
2021-06-13  2:53               ` Kumar Kartikeya Dwivedi
2021-06-13 20:27                 ` Jamal Hadi Salim
2021-06-13 20:34                   ` Kumar Kartikeya Dwivedi
2021-06-13 21:10                     ` Jamal Hadi Salim
2021-06-14 13:03                       ` Marcelo Ricardo Leitner
2021-06-15 23:07                       ` Daniel Borkmann
2021-06-16 14:40                         ` Jamal Hadi Salim
2021-06-16 15:32                           ` Kumar Kartikeya Dwivedi
2021-06-16 16:00                             ` Daniel Borkmann
2021-06-18 11:40                               ` Jamal Hadi Salim
2021-06-18 14:38                                 ` Alexei Starovoitov
2021-06-18 14:50                                   ` Jamal Hadi Salim
2021-06-18 16:23                                     ` Alexei Starovoitov
2021-06-18 16:41                                       ` Jamal Hadi Salim
2021-06-18 22:42                                 ` Daniel Borkmann
2021-06-21 13:55                                   ` Jamal Hadi Salim
2021-06-15  4:33                 ` Cong Wang
2021-06-15 11:54                   ` Toke Høiland-Jørgensen
2021-06-15 23:44                     ` Daniel Borkmann
2021-06-16 12:03                       ` Toke Høiland-Jørgensen
2021-06-16 15:33                       ` Jamal Hadi Salim
2021-06-13  3:08               ` Kumar Kartikeya Dwivedi
2021-05-28 23:44 [PATCH RFC bpf-next 3/7] net: sched: add bpf_link API for bpf classifier kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.