Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * bpf-script-example.c
0004  * Test basic LLVM building
0005  */
0006 #ifndef LINUX_VERSION_CODE
0007 # error Need LINUX_VERSION_CODE
0008 # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
0009 #endif
0010 #define BPF_ANY 0
0011 #define BPF_MAP_TYPE_ARRAY 2
0012 #define BPF_FUNC_map_lookup_elem 1
0013 #define BPF_FUNC_map_update_elem 2
0014 
0015 static void *(*bpf_map_lookup_elem)(void *map, void *key) =
0016     (void *) BPF_FUNC_map_lookup_elem;
0017 static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
0018     (void *) BPF_FUNC_map_update_elem;
0019 
0020 /*
0021  * Following macros are taken from tools/lib/bpf/bpf_helpers.h,
0022  * and are used to create BTF defined maps. It is easier to take
0023  * 2 simple macros, than being able to include above header in
0024  * runtime.
0025  *
0026  * __uint - defines integer attribute of BTF map definition,
0027  * Such attributes are represented using a pointer to an array,
0028  * in which dimensionality of array encodes specified integer
0029  * value.
0030  *
0031  * __type - defines pointer variable with typeof(val) type for
0032  * attributes like key or value, which will be defined by the
0033  * size of the type.
0034  */
0035 #define __uint(name, val) int (*name)[val]
0036 #define __type(name, val) typeof(val) *name
0037 
0038 #define SEC(NAME) __attribute__((section(NAME), used))
0039 struct {
0040     __uint(type, BPF_MAP_TYPE_ARRAY);
0041     __uint(max_entries, 1);
0042     __type(key, int);
0043     __type(value, int);
0044 } flip_table SEC(".maps");
0045 
0046 SEC("func=do_epoll_wait")
0047 int bpf_func__SyS_epoll_pwait(void *ctx)
0048 {
0049     int ind =0;
0050     int *flag = bpf_map_lookup_elem(&flip_table, &ind);
0051     int new_flag;
0052     if (!flag)
0053         return 0;
0054     /* flip flag and store back */
0055     new_flag = !*flag;
0056     bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
0057     return new_flag;
0058 }
0059 char _license[] SEC("license") = "GPL";
0060 int _version SEC("version") = LINUX_VERSION_CODE;