lpm_trie.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use core::{marker::PhantomData, mem, ptr::NonNull};
  2. use aya_bpf_cty::{c_long, c_void};
  3. use crate::{
  4. bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_LPM_TRIE},
  5. helpers::{bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem},
  6. maps::PinningType,
  7. };
  8. #[repr(transparent)]
  9. pub struct LpmTrie<K, V> {
  10. def: bpf_map_def,
  11. _k: PhantomData<K>,
  12. _v: PhantomData<V>,
  13. }
  14. #[repr(packed)]
  15. pub struct Key<K> {
  16. /// Represents the number of bytes matched against.
  17. pub prefix_len: u32,
  18. /// Represents arbitrary data stored in the LpmTrie.
  19. pub data: K,
  20. }
  21. impl<K> Key<K> {
  22. pub fn new(prefix_len: u32, data: K) -> Self {
  23. Self { prefix_len, data }
  24. }
  25. }
  26. impl<K, V> LpmTrie<K, V> {
  27. pub const fn with_max_entries(max_entries: u32, flags: u32) -> LpmTrie<K, V> {
  28. LpmTrie {
  29. def: build_def::<K, V>(BPF_MAP_TYPE_LPM_TRIE, max_entries, flags, PinningType::None),
  30. _k: PhantomData,
  31. _v: PhantomData,
  32. }
  33. }
  34. pub const fn pinned(max_entries: u32, flags: u32) -> LpmTrie<K, V> {
  35. LpmTrie {
  36. def: build_def::<K, V>(
  37. BPF_MAP_TYPE_LPM_TRIE,
  38. max_entries,
  39. flags,
  40. PinningType::ByName,
  41. ),
  42. _k: PhantomData,
  43. _v: PhantomData,
  44. }
  45. }
  46. #[inline]
  47. pub fn get(&mut self, key: &Key<K>) -> Option<&V> {
  48. unsafe {
  49. let value = bpf_map_lookup_elem(
  50. &mut self.def as *mut _ as *mut _,
  51. key as *const _ as *const c_void,
  52. );
  53. // FIXME: alignment
  54. NonNull::new(value as *mut V).map(|p| p.as_ref())
  55. }
  56. }
  57. #[inline]
  58. pub fn insert(&mut self, key: &Key<K>, value: &V, flags: u64) -> Result<(), c_long> {
  59. let ret = unsafe {
  60. bpf_map_update_elem(
  61. &mut self.def as *mut _ as *mut _,
  62. key as *const _ as *const _,
  63. value as *const _ as *const _,
  64. flags,
  65. )
  66. };
  67. (ret >= 0).then(|| ()).ok_or(ret)
  68. }
  69. #[inline]
  70. pub fn remove(&mut self, key: &Key<K>) -> Result<(), c_long> {
  71. let ret = unsafe {
  72. bpf_map_delete_elem(
  73. &mut self.def as *mut _ as *mut _,
  74. key as *const _ as *const c_void,
  75. )
  76. };
  77. (ret >= 0).then(|| ()).ok_or(ret)
  78. }
  79. }
  80. const fn build_def<K, V>(ty: u32, max_entries: u32, flags: u32, pin: PinningType) -> bpf_map_def {
  81. bpf_map_def {
  82. type_: ty,
  83. key_size: mem::size_of::<Key<K>>() as u32,
  84. value_size: mem::size_of::<V>() as u32,
  85. max_entries,
  86. map_flags: flags,
  87. id: 0,
  88. pinning: pin as u32,
  89. }
  90. }