Skip to content

SCX eBPF macro BPF_STRUCT_OPS

v6.12

The BPF_STRUCT_OPS macro makes it easier to define struct ops programs correctly.

Definition

#define BPF_STRUCT_OPS(name, args...)   \
    SEC("struct_ops/"#name)             \
    BPF_PROG(name, ##args)

Usage

This macro can be used to shorted the definition of struct ops programs. It places the program in an ELF section starting with struct_ops/ and names the program with the given name, this signals the loader of the program type. It also unpacks the program context into the arguments specified via args. See BPF_PROG for details.

Example

/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
 * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
 * Copyright (c) 2022 David Vernet <dvernet@meta.com>
 */

void BPF_STRUCT_OPS(qmap_dump, struct scx_dump_ctx *dctx)
{
    s32 i, pid;

    if (suppress_dump)
        return;

    bpf_for(i, 0, 5) {
        void *fifo;

        if (!(fifo = bpf_map_lookup_elem(&queue_arr, &i)))
            return;

        scx_bpf_dump("QMAP FIFO[%d]:", i);
        bpf_repeat(4096) {
            if (bpf_map_pop_elem(fifo, &pid))
                break;
            scx_bpf_dump(" %d", pid);
        }
        scx_bpf_dump("\n");
    }
}