Skip to content

KFunc bpf_rdonly_cast

v6.2

Definition

This kfunc tries to cast the object to a specified type.

The function returns the same obj but with PTR_TO_BTF_ID with btf_id. The verifier will ensure btf_id being a struct type.

Since the supported type cast may not reflect what the 'obj' represents, the returned btf_id is marked as PTR_UNTRUSTED, so the return value and subsequent pointer chasing cannot be used as helper/kfunc arguments.

void *bpf_rdonly_cast(const void *obj__ign, u32 btf_id__k)

signature changed

The signature of this kfunc has changed in v6.9. The previous signature was void *bpf_rdonly_cast(void *obj__ign, u32 btf_id__k) weak ELF symbols can be used to support both versions.

Usage

This tries to support use case like below:

#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))

where skb_end_pointer(SKB) is a unsigned char * and needs to be casted to struct skb_shared_info *.

Program types

The following program types can make use of this kfunc:

Example

This example shows a use case of bpf_rdonly_cast, which in the example is called by the bpf_core_cast macro.

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2018 Facebook

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_tracing_net.h"
#define NUM_CGROUP_LEVELS   4

__u64 cgroup_ids[NUM_CGROUP_LEVELS];
__u16 dport;

static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level)
{
    /* [1] &level passed to external function that may change it, it's
     *     incompatible with loop unroll.
     */
    cgroup_ids[level] = bpf_skb_ancestor_cgroup_id(skb, level);
}

SEC("tc")
int log_cgroup_id(struct __sk_buff *skb)
{
    struct sock *sk = (void *)skb->sk;

    if (!sk)
        return TC_ACT_OK;

    sk = bpf_core_cast(sk, struct sock);
    if (sk->sk_protocol == IPPROTO_UDP && sk->sk_dport == dport) {
        log_nth_level(skb, 0);
        log_nth_level(skb, 1);
        log_nth_level(skb, 2);
        log_nth_level(skb, 3);
    }

    return TC_ACT_OK;
}

char _license[] SEC("license") = "GPL";