0001
0002
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_HASH);
0010 __uint(max_entries, 3);
0011 __type(key, __u32);
0012 __type(value, __u64);
0013 } hashmap SEC(".maps");
0014
0015 struct {
0016 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
0017 __uint(max_entries, 1);
0018 __type(key, __u32);
0019 __type(value, __u64);
0020 } percpu_map SEC(".maps");
0021
0022 struct callback_ctx {
0023 struct __sk_buff *ctx;
0024 int input;
0025 int output;
0026 };
0027
0028 static __u64
0029 check_hash_elem(struct bpf_map *map, __u32 *key, __u64 *val,
0030 struct callback_ctx *data)
0031 {
0032 struct __sk_buff *skb = data->ctx;
0033 __u32 k;
0034 __u64 v;
0035
0036 if (skb) {
0037 k = *key;
0038 v = *val;
0039 if (skb->len == 10000 && k == 10 && v == 10)
0040 data->output = 3;
0041 else
0042 data->output = 4;
0043 } else {
0044 data->output = data->input;
0045 bpf_map_delete_elem(map, key);
0046 }
0047
0048 return 0;
0049 }
0050
0051 __u32 cpu = 0;
0052 __u32 percpu_called = 0;
0053 __u32 percpu_key = 0;
0054 __u64 percpu_val = 0;
0055 int percpu_output = 0;
0056
0057 static __u64
0058 check_percpu_elem(struct bpf_map *map, __u32 *key, __u64 *val,
0059 struct callback_ctx *unused)
0060 {
0061 struct callback_ctx data;
0062
0063 percpu_called++;
0064 cpu = bpf_get_smp_processor_id();
0065 percpu_key = *key;
0066 percpu_val = *val;
0067
0068 data.ctx = 0;
0069 data.input = 100;
0070 data.output = 0;
0071 bpf_for_each_map_elem(&hashmap, check_hash_elem, &data, 0);
0072 percpu_output = data.output;
0073
0074 return 0;
0075 }
0076
0077 int hashmap_output = 0;
0078 int hashmap_elems = 0;
0079 int percpu_map_elems = 0;
0080
0081 SEC("tc")
0082 int test_pkt_access(struct __sk_buff *skb)
0083 {
0084 struct callback_ctx data;
0085
0086 data.ctx = skb;
0087 data.input = 10;
0088 data.output = 0;
0089 hashmap_elems = bpf_for_each_map_elem(&hashmap, check_hash_elem, &data, 0);
0090 hashmap_output = data.output;
0091
0092 percpu_map_elems = bpf_for_each_map_elem(&percpu_map, check_percpu_elem,
0093 (void *)0, 0);
0094 return 0;
0095 }