test_queue_stack_map.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. // Copyright (c) 2018 Politecnico di Torino
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <linux/bpf.h>
  6. #include <linux/if_ether.h>
  7. #include <linux/ip.h>
  8. #include <linux/pkt_cls.h>
  9. #include "bpf_helpers.h"
  10. int _version SEC("version") = 1;
  11. struct bpf_map_def __attribute__ ((section("maps"), used)) map_in = {
  12. .type = MAP_TYPE,
  13. .key_size = 0,
  14. .value_size = sizeof(__u32),
  15. .max_entries = 32,
  16. .map_flags = 0,
  17. };
  18. struct bpf_map_def __attribute__ ((section("maps"), used)) map_out = {
  19. .type = MAP_TYPE,
  20. .key_size = 0,
  21. .value_size = sizeof(__u32),
  22. .max_entries = 32,
  23. .map_flags = 0,
  24. };
  25. SEC("test")
  26. int _test(struct __sk_buff *skb)
  27. {
  28. void *data_end = (void *)(long)skb->data_end;
  29. void *data = (void *)(long)skb->data;
  30. struct ethhdr *eth = (struct ethhdr *)(data);
  31. __u32 value;
  32. int err;
  33. if (eth + 1 > data_end)
  34. return TC_ACT_SHOT;
  35. struct iphdr *iph = (struct iphdr *)(eth + 1);
  36. if (iph + 1 > data_end)
  37. return TC_ACT_SHOT;
  38. err = bpf_map_pop_elem(&map_in, &value);
  39. if (err)
  40. return TC_ACT_SHOT;
  41. iph->daddr = value;
  42. err = bpf_map_push_elem(&map_out, &iph->saddr, 0);
  43. if (err)
  44. return TC_ACT_SHOT;
  45. return TC_ACT_OK;
  46. }
  47. char _license[] SEC("license") = "GPL";