brintos

brintos / linux-shallow public Read only

0
0
Text · 919 B · ff0bed7 Raw
50 lines · c
1// SPDX-License-Identifier: GPL-2.02#include "vmlinux.h"3#include <bpf/bpf_helpers.h>4 5char _license[] SEC("license") = "GPL";6 7struct {8	__uint(type, BPF_MAP_TYPE_ARRAY);9	__uint(max_entries, 3);10	__type(key, __u32);11	__type(value, __u64);12} arraymap SEC(".maps");13 14struct {15	__uint(type, BPF_MAP_TYPE_HASH);16	__uint(max_entries, 5);17	__type(key, __u32);18	__type(value, __u64);19} hashmap SEC(".maps");20 21struct callback_ctx {22	int output;23};24 25u32 data_output = 0;26int use_array = 0;27 28static __u6429check_map_elem(struct bpf_map *map, __u32 *key, __u64 *val,30	       struct callback_ctx *data)31{32	data->output += *val;33	return 0;34}35 36SEC("tc")37int test_pkt_access(struct __sk_buff *skb)38{39	struct callback_ctx data;40 41	data.output = 0;42	if (use_array)43		bpf_for_each_map_elem(&arraymap, check_map_elem, &data, 0);44	else45		bpf_for_each_map_elem(&hashmap, check_map_elem, &data, 0);46	data_output = data.output;47 48	return 0;49}50