Skip to content

SCX eBPF macro __contains

v6.12

The __contains macro is used during the definition of graph data structures such as linked lists or red-black trees to inform the verifier of the types that will make up the graph.

Definition

#define __contains(name, node) __attribute__((btf_decl_tag("contains:" #name ":" #node)))

Usage

This macro is used when declaring the root/head of a graph. These graphs, in eBPF must always be made up of of nodes that embed known types that actually implement the graph. Followed by the data the user wishes to store in the graph node.

For example:

struct my_linked_list_node {
    struct bpf_list_node list_node;
    __u64 data;
}

or

struct my_rb_tree_node {
    struct bpf_rb_node rb_node;
    __u64 data;
}

Each known type has a corresponding root / head type which is what would be part of a map value or global variable. It is on this root type that the __contains macro is used.

The name parameter is the name of the node type. The node parameter of the macro informs the kernel of the field name of the embedded known type.

Example

/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2022 Tejun Heo <tj@kernel.org> */

struct bpf_rb_root {
    __u64 __opaque[2];
} __attribute__((aligned(8)));

struct bpf_rb_node {
    __u64 __opaque[4];
} __attribute__((aligned(8)));

struct cgv_node {
    struct bpf_rb_node  rb_node;
    __u64               cvtime;
    __u64               cgid;
};

private(CGV_TREE) struct bpf_rb_root cgv_tree __contains(cgv_node, rb_node);