github.com/cilium/cilium@v1.16.2/test/bpf/elf-demo.c (about)

     1  // SPDX-License-Identifier: GPL-2.0
     2  // Copyright Authors of Cilium
     3  
     4  #include <bpf/ctx/skb.h>
     5  #include <bpf/api.h>
     6  
     7  #include "lib/utils.h"
     8  #include <node_config.h>
     9  #include "lib/common.h"
    10  #include "lib/eth.h"
    11  
    12  DEFINE_U32(FOO, 0xF0F0F0F0);
    13  DEFINE_U32(BAR, 0xCECECECE);
    14  DEFINE_IPV6(GLOBAL_IPV6, 0x1, 0, 0x1, 0, 0, 0x1, 0, 0x1, 0x1, 0, 0x1, 0, 0, 0x1, 0, 0x1);
    15  DEFINE_MAC(LOCAL_MAC, 0, 0x1, 0, 0, 0, 0x1);
    16  
    17  #define CALLS_MAP_ID 0
    18  #undef CALLS_MAP
    19  #define CALLS_MAP test_cilium_calls_4278124286 // 0xFEFEFEFE
    20  
    21  // Note: This program is meant to exercise ELF substitution logic and eBPF
    22  // loader behaviour. The programs are not assigned to sections, so don't have
    23  // their progbits emitted to the ELF, and cannot be loaded into the kernel.
    24  
    25  int tail_lxc_prog(struct __sk_buff *skb) {
    26  	return TC_ACT_OK;
    27  }
    28  
    29  struct {
    30  	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
    31  	__uint(key_size, sizeof(__u32));
    32  	__uint(max_entries, 1);
    33  	__uint(pinning, LIBBPF_PIN_BY_NAME);
    34  	__array(values, int ());
    35  } CALLS_MAP __section_maps_btf = {
    36  	.values = {
    37  		[CALLS_MAP_ID] = &tail_lxc_prog,
    38  	},
    39  };
    40  
    41  int __main(struct __sk_buff *skb)
    42  {
    43  	union v6addr v6 = {};
    44  	union macaddr mac = fetch_mac(LOCAL_MAC);
    45  
    46  	skb->mark = fetch_u32(FOO);
    47  	skb->cb[0] = fetch_u32(BAR);
    48  
    49  	BPF_V6(v6, GLOBAL_IPV6);
    50  	skb->cb[1] = v6.p1;
    51  	skb->cb[2] = v6.p2;
    52  	skb->cb[3] = v6.p3;
    53  	skb->cb[4] = v6.p4;
    54  
    55  	skb->priority = mac.p1;
    56  	skb->tc_classid = mac.p2;
    57  
    58  	tail_call(skb, &CALLS_MAP, CALLS_MAP_ID);
    59  
    60  	return TC_ACT_SHOT;
    61  }
    62  
    63  char __license[] __section("license") = "";