github.com/cilium/cilium@v1.16.2/bpf/custom/bpf_custom.c (about)

     1  // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
     2  /* Copyright Authors of Cilium */
     3  
     4  #include <bpf/ctx/skb.h>
     5  #include <bpf/api.h>
     6  
     7  #include <node_config.h>
     8  #include "lib/common.h"
     9  
    10  #define TO_STRING(X) #X
    11  #define STRINGIFY(X) TO_STRING(X)
    12  
    13  /* Use the macros below to set the name of the program and the name of the file
    14   * containing the implementation for custom_prog(). The values for these macros
    15   * should typically be passed to the Makefile, for example:
    16   *
    17   *     BPF_CUSTOM_PROG_FILE=bytecount.h make
    18   */
    19  
    20  #ifndef BPF_CUSTOM_PROG_FILE
    21  /* Default to bytecount.h for the included file */
    22  #define BPF_CUSTOM_PROG_FILE bytecount.h
    23  #endif
    24  
    25  #ifndef BPF_CUSTOM_PROG_NAME
    26  /* Default to __section("custom") for the program */
    27  #define BPF_CUSTOM_PROG_NAME custom
    28  #endif
    29  
    30  #include STRINGIFY(BPF_CUSTOM_PROG_FILE)
    31  
    32  __section(STRINGIFY(BPF_CUSTOM_PROG_NAME))
    33  int custom_hook(const struct __ctx_buff *ctx)
    34  {
    35  	__u32 custom_meta = ctx_load_meta(ctx, CB_CUSTOM_CALLS);
    36  	__u32 identity = custom_meta & 0xffffff;
    37  	int ret = (custom_meta >> 24) & 0xff;
    38  
    39  	/* Call user-defined function from custom header file. */
    40  	custom_prog(ctx, identity);
    41  
    42  	/* Return action code selected from parent program, independently of
    43  	 * what the custom function does, to maintain datapath consistency.
    44  	 */
    45  	return ret;
    46  }