sockex1_kern.c 553 B

12345678910111213141516171819202122232425
  1. #include <uapi/linux/bpf.h>
  2. #include <uapi/linux/if_ether.h>
  3. #include <uapi/linux/ip.h>
  4. #include "bpf_helpers.h"
  5. struct bpf_map_def SEC("maps") my_map = {
  6. .type = BPF_MAP_TYPE_ARRAY,
  7. .key_size = sizeof(u32),
  8. .value_size = sizeof(long),
  9. .max_entries = 256,
  10. };
  11. SEC("socket1")
  12. int bpf_prog1(struct sk_buff *skb)
  13. {
  14. int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
  15. long *value;
  16. value = bpf_map_lookup_elem(&my_map, &index);
  17. if (value)
  18. __sync_fetch_and_add(value, 1);
  19. return 0;
  20. }
  21. char _license[] SEC("license") = "GPL";