sockex1_kern.c 648 B

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