Pārlūkot izejas kodu

bpf: Added pinned constructor to maps

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
Dave Tucker 3 gadi atpakaļ
vecāks
revīzija
69041954cb

+ 17 - 1
bpf/aya-bpf/src/maps/array.rs

@@ -5,6 +5,7 @@ use aya_bpf_cty::c_void;
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_ARRAY},
     helpers::bpf_map_lookup_elem,
+    maps::PinningType,
 };
 
 #[repr(transparent)]
@@ -23,7 +24,22 @@ impl<T> Array<T> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
+            },
+            _t: PhantomData,
+        }
+    }
+
+    pub const fn pinned(max_entries: u32, flags: u32) -> Array<T> {
+        Array {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_ARRAY,
+                key_size: mem::size_of::<u32>() as u32,
+                value_size: mem::size_of::<T>() as u32,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
             },
             _t: PhantomData,
         }

+ 4 - 3
bpf/aya-bpf/src/maps/hash_map.rs

@@ -5,6 +5,7 @@ use aya_bpf_cty::{c_long, c_void};
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_HASH},
     helpers::{bpf_map_delete_elem, bpf_map_lookup_elem, bpf_map_update_elem},
+    maps::PinningType,
 };
 
 #[repr(transparent)]
@@ -24,14 +25,14 @@ impl<K, V> HashMap<K, V> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
             },
             _k: PhantomData,
             _v: PhantomData,
         }
     }
 
-    pub const fn pinned(max_entries: u32, flags: u32, pinning: u32) -> HashMap<K, V> {
+    pub const fn pinned(max_entries: u32, flags: u32) -> HashMap<K, V> {
         HashMap {
             def: bpf_map_def {
                 type_: BPF_MAP_TYPE_HASH,
@@ -40,7 +41,7 @@ impl<K, V> HashMap<K, V> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning,
+                pinning: PinningType::ByName as u32,
             },
             _k: PhantomData,
             _v: PhantomData,

+ 7 - 0
bpf/aya-bpf/src/maps/mod.rs

@@ -1,3 +1,10 @@
+#[repr(u32)]
+#[derive(Copy, Clone, Debug, PartialEq)]
+pub(crate) enum PinningType {
+    None = 0,
+    ByName = 1,
+}
+
 pub mod array;
 pub mod hash_map;
 pub mod per_cpu_array;

+ 17 - 1
bpf/aya-bpf/src/maps/per_cpu_array.rs

@@ -5,6 +5,7 @@ use aya_bpf_cty::c_void;
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY},
     helpers::bpf_map_lookup_elem,
+    maps::PinningType,
 };
 
 #[repr(transparent)]
@@ -23,7 +24,22 @@ impl<T> PerCpuArray<T> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
+            },
+            _t: PhantomData,
+        }
+    }
+
+    pub const fn pinned(max_entries: u32, flags: u32) -> PerCpuArray<T> {
+        PerCpuArray {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_PERCPU_ARRAY,
+                key_size: mem::size_of::<u32>() as u32,
+                value_size: mem::size_of::<T>() as u32,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
             },
             _t: PhantomData,
         }

+ 17 - 1
bpf/aya-bpf/src/maps/perf_map.rs

@@ -3,6 +3,7 @@ use core::{marker::PhantomData, mem};
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_F_CURRENT_CPU},
     helpers::bpf_perf_event_output,
+    maps::PinningType,
     BpfContext,
 };
 
@@ -26,7 +27,22 @@ impl<T> PerfMap<T> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
+            },
+            _t: PhantomData,
+        }
+    }
+
+    pub const fn pinned(max_entries: u32, flags: u32) -> PerfMap<T> {
+        PerfMap {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_PERF_EVENT_ARRAY,
+                key_size: mem::size_of::<u32>() as u32,
+                value_size: mem::size_of::<u32>() as u32,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
             },
             _t: PhantomData,
         }

+ 17 - 1
bpf/aya-bpf/src/maps/queue.rs

@@ -3,6 +3,7 @@ use core::{marker::PhantomData, mem};
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_QUEUE},
     helpers::{bpf_map_pop_elem, bpf_map_push_elem},
+    maps::PinningType,
 };
 
 #[repr(transparent)]
@@ -21,7 +22,22 @@ impl<T> Queue<T> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
+            },
+            _t: PhantomData,
+        }
+    }
+
+    pub const fn pinned(max_entries: u32, flags: u32) -> Queue<T> {
+        Queue {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_QUEUE,
+                key_size: 0,
+                value_size: mem::size_of::<T>() as u32,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
             },
             _t: PhantomData,
         }

+ 17 - 1
bpf/aya-bpf/src/maps/sock_hash.rs

@@ -5,6 +5,7 @@ use aya_bpf_cty::c_void;
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_SOCKHASH, bpf_sock_ops},
     helpers::{bpf_msg_redirect_hash, bpf_sock_hash_update},
+    maps::PinningType,
     programs::SkMsgContext,
     BpfContext,
 };
@@ -25,7 +26,22 @@ impl<K> SockHash<K> {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
+            },
+            _k: PhantomData,
+        }
+    }
+
+    pub const fn pinned(max_entries: u32, flags: u32) -> SockHash<K> {
+        SockHash {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_SOCKHASH,
+                key_size: mem::size_of::<K>() as u32,
+                value_size: mem::size_of::<u32>() as u32,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
             },
             _k: PhantomData,
         }

+ 16 - 1
bpf/aya-bpf/src/maps/sock_map.rs

@@ -5,6 +5,7 @@ use aya_bpf_cty::c_void;
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_SOCKMAP, bpf_sock_ops},
     helpers::{bpf_msg_redirect_map, bpf_sock_map_update},
+    maps::PinningType,
     programs::SkMsgContext,
     BpfContext,
 };
@@ -24,7 +25,21 @@ impl SockMap {
                 max_entries,
                 map_flags: flags,
                 id: 0,
-                pinning: 0,
+                pinning: PinningType::None as u32,
+            },
+        }
+    }
+
+    pub const fn pinned(max_entries: u32, flags: u32) -> SockMap {
+        SockMap {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_SOCKMAP,
+                key_size: mem::size_of::<u32>() as u32,
+                value_size: mem::size_of::<u32>() as u32,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
             },
         }
     }

+ 39 - 28
bpf/aya-bpf/src/maps/stack_trace.rs

@@ -2,42 +2,53 @@ use core::mem;
 
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_STACK_TRACE},
-    helpers::{bpf_get_stackid},
+    helpers::bpf_get_stackid,
+    maps::PinningType,
     BpfContext,
 };
 
 #[repr(transparent)]
 pub struct StackTrace {
-	def: bpf_map_def,
+    def: bpf_map_def,
 }
 
 const PERF_MAX_STACK_DEPTH: u32 = 127;
 
 impl StackTrace {
-	pub const fn with_max_entries(max_entries: u32, flags: u32) -> StackTrace {
-		StackTrace {
-			def: bpf_map_def {
-				type_: BPF_MAP_TYPE_STACK_TRACE,
-				key_size: mem::size_of::<u32>() as u32,
-				value_size: mem::size_of::<u64>() as u32 * PERF_MAX_STACK_DEPTH,
-				max_entries,
-				map_flags: flags,
-				id: 0,
-				pinning: 0,
-			},
-		}
-	}
+    pub const fn with_max_entries(max_entries: u32, flags: u32) -> StackTrace {
+        StackTrace {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_STACK_TRACE,
+                key_size: mem::size_of::<u32>() as u32,
+                value_size: mem::size_of::<u64>() as u32 * PERF_MAX_STACK_DEPTH,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::None as u32,
+            },
+        }
+    }
 
-	pub unsafe fn get_stackid<C: BpfContext>(&mut self, ctx: &C, flags: u64) -> Result<i64, i64> {
-		let ret = bpf_get_stackid(
-			ctx.as_ptr(),
-			&mut self.def as *mut _ as *mut _,
-			flags,
-		);
-		if ret < 0 {
-			Err(ret)
-		} else {
-			Ok(ret)
-		}
-	}
-}
+    pub const fn pinned(max_entries: u32, flags: u32) -> StackTrace {
+        StackTrace {
+            def: bpf_map_def {
+                type_: BPF_MAP_TYPE_STACK_TRACE,
+                key_size: mem::size_of::<u32>() as u32,
+                value_size: mem::size_of::<u64>() as u32 * PERF_MAX_STACK_DEPTH,
+                max_entries,
+                map_flags: flags,
+                id: 0,
+                pinning: PinningType::ByName as u32,
+            },
+        }
+    }
+
+    pub unsafe fn get_stackid<C: BpfContext>(&mut self, ctx: &C, flags: u64) -> Result<i64, i64> {
+        let ret = bpf_get_stackid(ctx.as_ptr(), &mut self.def as *mut _ as *mut _, flags);
+        if ret < 0 {
+            Err(ret)
+        } else {
+            Ok(ret)
+        }
+    }
+}