History

bpf: Add fd-based tcx multi-prog infra with link support

This work refactors and adds a lightweight extension ("tcx") to the tc BPF ingress and egress data path side for allowing BPF program management based on fds via bpf() syscall through the newly added generic multi-prog API. The main goal behind this work which we also presented at LPC [0] last year and a recent update at LSF/MM/BPF this year [3] is to support long-awaited BPF link functionality for tc BPF programs, which allows for a model of safe ownership and program detachment. Given the rise in tc BPF users in cloud native environments, this becomes necessary to avoid hard to debug incidents either through stale leftover programs or 3rd party applications accidentally stepping on each others toes. As a recap, a BPF link represents the attachment of a BPF program to a BPF hook point. The BPF link holds a single reference to keep BPF program alive. Moreover, hook points do not reference a BPF link, only the application's fd or pinning does. A BPF link holds meta-data specific to attachment and implements operations for link creation, (atomic) BPF program update, detachment and introspection. The motivation for BPF links for tc BPF programs is multi-fold, for example: - From Meta: "It's especially important for applications that are deployed fleet-wide and that don't "control" hosts they are deployed to. If such application crashes and no one notices and does anything about that, BPF program will keep running draining resources or even just, say, dropping packets. We at FB had outages due to such permanent BPF attachment semantics. With fd-based BPF link we are getting a framework, which allows safe, auto-detachable behavior by default, unless application explicitly opts in by pinning the BPF link." [1] - From Cilium-side the tc BPF programs we attach to host-facing veth devices and phys devices build the core datapath for Kubernetes Pods, and they implement forwarding, load-balancing, policy, EDT-management, etc, within BPF. Currently there is no concept of 'safe' ownership, e.g. we've recently experienced hard-to-debug issues in a user's staging environment where another Kubernetes application using tc BPF attached to the same prio/handle of cls_bpf, accidentally wiping all Cilium-based BPF programs from underneath it. The goal is to establish a clear/safe ownership model via links which cannot accidentally be overridden. [0,2] BPF links for tc can co-exist with non-link attachments, and the semantics are in line also with XDP links: BPF links cannot replace other BPF links, BPF links cannot replace non-BPF links, non-BPF links cannot replace BPF links and lastly only non-BPF links can replace non-BPF links. In case of Cilium, this would solve mentioned issue of safe ownership model as 3rd party applications would not be able to accidentally wipe Cilium programs, even if they are not BPF link aware. Earlier attempts [4] have tried to integrate BPF links into core tc machinery to solve cls_bpf, which has been intrusive to the generic tc kernel API with extensions only specific to cls_bpf and suboptimal/complex since cls_bpf could be wiped from the qdisc also. Locking a tc BPF program in place this way, is getting into layering hacks given the two object models are vastly different. We instead implemented the tcx (tc 'express') layer which is an fd-based tc BPF attach API, so that the BPF link implementation blends in naturally similar to other link types which are fd-based and without the need for changing core tc internal APIs. BPF programs for tc can then be successively migrated from classic cls_bpf to the new tc BPF link without needing to change the program's source code, just the BPF loader mechanics for attaching is sufficient. For the current tc framework, there is no change in behavior with this change and neither does this change touch on tc core kernel APIs. The gist of this patch is that the ingress and egress hook have a lightweight, qdisc-less extension for BPF to attach its tc BPF programs, in other words, a minimal entry point for tc BPF. The name tcx has been suggested from discussion of earlier revisions of this work as a good fit, and to more easily differ between the classic cls_bpf attachment and the fd-based one. For the ingress and egress tcx points, the device holds a cache-friendly array with program pointers which is separated from control plane (slow-path) data. Earlier versions of this work used priority to determine ordering and expression of dependencies similar as with classic tc, but it was challenged that for something more future-proof a better user experience is required. Hence this resulted in the design and development of the generic attach/detach/query API for multi-progs. See prior patch with its discussion on the API design. tcx is the first user and later we plan to integrate also others, for example, one candidate is multi-prog support for XDP which would benefit and have the same 'look and feel' from API perspective. The goal with tcx is to have maximum compatibility to existing tc BPF programs, so they don't need to be rewritten specifically. Compatibility to call into classic tcf_classify() is also provided in order to allow successive migration or both to cleanly co-exist where needed given its all one logical tc layer and the tcx plus classic tc cls/act build one logical overall processing pipeline. tcx supports the simplified return codes TCX_NEXT which is non-terminating (go to next program) and terminating ones with TCX_PASS, TCX_DROP, TCX_REDIRECT. The fd-based API is behind a static key, so that when unused the code is also not entered. The struct tcx_entry's program array is currently static, but could be made dynamic if necessary at a point in future. The a/b pair swap design has been chosen so that for detachment there are no allocations which otherwise could fail. The work has been tested with tc-testing selftest suite which all passes, as well as the tc BPF tests from the BPF CI, and also with Cilium's L4LB. Thanks also to Nikolay Aleksandrov and Martin Lau for in-depth early reviews of this work. [0] https://lpc.events/event/16/contributions/1353/ [1] https://lore.kernel.org/bpf/CAEf4BzbokCJN33Nw_kg82sO=xppXnKWEncGTWCTB9vGCmLB6pw@mail.gmail.com [2] https://colocatedeventseu2023.sched.com/event/1Jo6O/tales-from-an-ebpf-programs-murder-mystery-hemanth-malla-guillaume-fournier-datadog [3] http://vger.kernel.org/bpfconf2023_material/tcx_meta_netdev_borkmann.pdf [4] https://lore.kernel.org/bpf/20210604063116.234316-1-memxor@gmail.com

Daniel Borkmann committed 3 years ago

Commit e420bed1 parent 053c8e117 files · +935 −144Browse files

Showing the changes to include/uapi (1 of 17 files). Show all

Unified · Split

include/uapi/linux/bpf.h+30−4
@@ -1036,6 +1036,8 @@ enum bpf_attach_type { · 1035 unmodified lines
1036 BPF_LSM_CGROUP,1036 BPF_LSM_CGROUP,
1037 BPF_STRUCT_OPS,1037 BPF_STRUCT_OPS,
1038 BPF_NETFILTER,1038 BPF_NETFILTER,
1039 BPF_TCX_INGRESS,
1040 BPF_TCX_EGRESS,
1039 __MAX_BPF_ATTACH_TYPE1041 __MAX_BPF_ATTACH_TYPE
1040};1042};
1041 1043
@@ -1053,7 +1055,7 @@ enum bpf_link_type { · 11 unmodified lines
1053 BPF_LINK_TYPE_KPROBE_MULTI = 8,1055 BPF_LINK_TYPE_KPROBE_MULTI = 8,
1054 BPF_LINK_TYPE_STRUCT_OPS = 9,1056 BPF_LINK_TYPE_STRUCT_OPS = 9,
1055 BPF_LINK_TYPE_NETFILTER = 10,1057 BPF_LINK_TYPE_NETFILTER = 10,
1056 1058 BPF_LINK_TYPE_TCX = 11,
1057 MAX_BPF_LINK_TYPE,1059 MAX_BPF_LINK_TYPE,
1058};1060};
1059 1061
@@ -1569,13 +1571,13 @@ union bpf_attr { · 509 unmodified lines
1569 __u32 map_fd; /* struct_ops to attach */1571 __u32 map_fd; /* struct_ops to attach */
1570 };1572 };
1571 union {1573 union {
1572 __u32 target_fd; /* object to attach to */1574 __u32 target_fd; /* target object to attach to or ... */
1573 __u32 target_ifindex; /* target ifindex */1575 __u32 target_ifindex; /* target ifindex */
1574 };1576 };
1575 __u32 attach_type; /* attach type */1577 __u32 attach_type; /* attach type */
1576 __u32 flags; /* extra flags */1578 __u32 flags; /* extra flags */
1577 union {1579 union {
1578 __u32 target_btf_id; /* btf_id of target to attach to */1580 __u32 target_btf_id; /* btf_id of target to attach to */
1579 struct {1581 struct {
1580 __aligned_u64 iter_info; /* extra bpf_iter_link_info */1582 __aligned_u64 iter_info; /* extra bpf_iter_link_info */
1581 __u32 iter_info_len; /* iter_info length */1583 __u32 iter_info_len; /* iter_info length */
@@ -1609,6 +1611,13 @@ union bpf_attr { · 27 unmodified lines
1609 __s32 priority;1611 __s32 priority;
1610 __u32 flags;1612 __u32 flags;
1611 } netfilter;1613 } netfilter;
1614 struct {
1615 union {
1616 __u32 relative_fd;
1617 __u32 relative_id;
1618 };
1619 __u64 expected_revision;
1620 } tcx;
1612 };1621 };
1613 } link_create;1622 } link_create;
1614 1623
@@ -6217,6 +6226,19 @@ struct bpf_sock_tuple { · 4602 unmodified lines
6217 };6226 };
6218};6227};
6219 6228
6229/* (Simplified) user return codes for tcx prog type.
6230 * A valid tcx program must return one of these defined values. All other
6231 * return codes are reserved for future use. Must remain compatible with
6232 * their TC_ACT_* counter-parts. For compatibility in behavior, unknown
6233 * return codes are mapped to TCX_NEXT.
6234 */
6235enum tcx_action_base {
6236 TCX_NEXT = -1,
6237 TCX_PASS = 0,
6238 TCX_DROP = 2,
6239 TCX_REDIRECT = 7,
6240};
6241
6220struct bpf_xdp_sock {6242struct bpf_xdp_sock {
6221 __u32 queue_id;6243 __u32 queue_id;
6222};6244};
@@ -6499,6 +6521,10 @@ struct bpf_link_info { · 276 unmodified lines
6499 } event; /* BPF_PERF_EVENT_EVENT */6521 } event; /* BPF_PERF_EVENT_EVENT */
6500 };6522 };
6501 } perf_event;6523 } perf_event;
6524 struct {
6525 __u32 ifindex;
6526 __u32 attach_type;
6527 } tcx;
6502 };6528 };
6503} __attribute__((aligned(8)));6529} __attribute__((aligned(8)));
6504 6530

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t