map_test.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Socket Filter program for testing with an arbitrary program with maps.
  2. // This is mainly used in tests with consideration for old kernels.
  3. #![no_std]
  4. #![no_main]
  5. use aya_ebpf::{
  6. macros::{map, socket_filter},
  7. maps::{Array, HashMap},
  8. programs::SkBuffContext,
  9. };
  10. #[cfg(not(test))]
  11. extern crate ebpf_panic;
  12. // Introduced in kernel v3.19.
  13. #[map]
  14. static FOO: Array<u32> = Array::<u32>::with_max_entries(10, 0);
  15. // Introduced in kernel v3.19.
  16. #[map(name = "BAR")]
  17. static BAZ: HashMap<u32, u8> = HashMap::<u32, u8>::with_max_entries(8, 0);
  18. // The limit of map names is 16 (including a NUL byte). Ensure that we are
  19. // able to create maps with names exceeding that limit by truncating them.
  20. #[map(name = "MAP_WITH_LOOOONG_NAAAAAAAAME")]
  21. static MAP_WITH_LOOOONG_NAAAAAAAAME: HashMap<u32, u8> = HashMap::<u32, u8>::with_max_entries(8, 0);
  22. // Introduced in kernel v3.19.
  23. #[socket_filter]
  24. pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
  25. // So that these maps show up under the `map_ids` field.
  26. FOO.get(0);
  27. // If we use the literal value `0` instead of the local variable `i`, then an additional
  28. // `.rodata` map will be associated with the program.
  29. let i = 0;
  30. BAZ.get_ptr(&i);
  31. 0
  32. }