Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 #include "vmlinux.h"
0004 #include <bpf/bpf_helpers.h>
0005 
0006 char _license[] SEC("license") = "GPL";
0007 
0008 struct {
0009     __uint(type, BPF_MAP_TYPE_ARRAY);
0010     __uint(max_entries, 3);
0011     __type(key, __u32);
0012     __type(value, __u64);
0013 } arraymap SEC(".maps");
0014 
0015 struct {
0016     __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
0017     __uint(max_entries, 1);
0018     __type(key, __u32);
0019     __type(value, __u64);
0020 } percpu_map SEC(".maps");
0021 
0022 struct callback_ctx {
0023     int output;
0024 };
0025 
0026 const volatile int bypass_unused = 1;
0027 
0028 static __u64
0029 unused_subprog(struct bpf_map *map, __u32 *key, __u64 *val,
0030            struct callback_ctx *data)
0031 {
0032     data->output = 0;
0033     return 1;
0034 }
0035 
0036 static __u64
0037 check_array_elem(struct bpf_map *map, __u32 *key, __u64 *val,
0038          struct callback_ctx *data)
0039 {
0040     data->output += *val;
0041     if (*key == 1)
0042         return 1; /* stop the iteration */
0043     return 0;
0044 }
0045 
0046 __u32 cpu = 0;
0047 __u64 percpu_val = 0;
0048 
0049 static __u64
0050 check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
0051           struct callback_ctx *data)
0052 {
0053     cpu = bpf_get_smp_processor_id();
0054     percpu_val = *val;
0055     return 0;
0056 }
0057 
0058 u32 arraymap_output = 0;
0059 
0060 SEC("tc")
0061 int test_pkt_access(struct __sk_buff *skb)
0062 {
0063     struct callback_ctx data;
0064 
0065     data.output = 0;
0066     bpf_for_each_map_elem(&arraymap, check_array_elem, &data, 0);
0067     if (!bypass_unused)
0068         bpf_for_each_map_elem(&arraymap, unused_subprog, &data, 0);
0069     arraymap_output = data.output;
0070 
0071     bpf_for_each_map_elem(&percpu_map, check_percpu_elem, (void *)0, 0);
0072     return 0;
0073 }