From 1844e85ccc6442fcf2a7c10e2f044c34b6c42e03 Mon Sep 17 00:00:00 2001
From: Dave Tucker <dave@dtucker.co.uk>
Date: Mon, 27 Sep 2021 13:46:19 +0100
Subject: [PATCH] bpf: Added pinned constructor to maps

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
---
 bpf/aya-bpf/src/maps/array.rs         | 18 ++++++-
 bpf/aya-bpf/src/maps/hash_map.rs      |  7 +--
 bpf/aya-bpf/src/maps/mod.rs           |  7 +++
 bpf/aya-bpf/src/maps/per_cpu_array.rs | 18 ++++++-
 bpf/aya-bpf/src/maps/perf_map.rs      | 18 ++++++-
 bpf/aya-bpf/src/maps/queue.rs         | 18 ++++++-
 bpf/aya-bpf/src/maps/sock_hash.rs     | 18 ++++++-
 bpf/aya-bpf/src/maps/sock_map.rs      | 17 ++++++-
 bpf/aya-bpf/src/maps/stack_trace.rs   | 67 ++++++++++++++++-----------
 9 files changed, 151 insertions(+), 37 deletions(-)

diff --git a/bpf/aya-bpf/src/maps/array.rs b/bpf/aya-bpf/src/maps/array.rs
index bbee2f75..349e7efe 100644
--- a/bpf/aya-bpf/src/maps/array.rs
+++ b/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,
         }
diff --git a/bpf/aya-bpf/src/maps/hash_map.rs b/bpf/aya-bpf/src/maps/hash_map.rs
index 3f3a803d..b2fc50ef 100644
--- a/bpf/aya-bpf/src/maps/hash_map.rs
+++ b/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,
diff --git a/bpf/aya-bpf/src/maps/mod.rs b/bpf/aya-bpf/src/maps/mod.rs
index 71db366a..b6cd3c65 100644
--- a/bpf/aya-bpf/src/maps/mod.rs
+++ b/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;
diff --git a/bpf/aya-bpf/src/maps/per_cpu_array.rs b/bpf/aya-bpf/src/maps/per_cpu_array.rs
index cfa264ad..5019e93f 100644
--- a/bpf/aya-bpf/src/maps/per_cpu_array.rs
+++ b/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,
         }
diff --git a/bpf/aya-bpf/src/maps/perf_map.rs b/bpf/aya-bpf/src/maps/perf_map.rs
index 3776f2f2..bd3d7fd4 100644
--- a/bpf/aya-bpf/src/maps/perf_map.rs
+++ b/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,
         }
diff --git a/bpf/aya-bpf/src/maps/queue.rs b/bpf/aya-bpf/src/maps/queue.rs
index b0d86a40..783c5bbd 100644
--- a/bpf/aya-bpf/src/maps/queue.rs
+++ b/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,
         }
diff --git a/bpf/aya-bpf/src/maps/sock_hash.rs b/bpf/aya-bpf/src/maps/sock_hash.rs
index 6b2c30b5..d381b3cb 100644
--- a/bpf/aya-bpf/src/maps/sock_hash.rs
+++ b/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,
         }
diff --git a/bpf/aya-bpf/src/maps/sock_map.rs b/bpf/aya-bpf/src/maps/sock_map.rs
index 2952ad37..97ddd008 100644
--- a/bpf/aya-bpf/src/maps/sock_map.rs
+++ b/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,
             },
         }
     }
diff --git a/bpf/aya-bpf/src/maps/stack_trace.rs b/bpf/aya-bpf/src/maps/stack_trace.rs
index 9557976d..d87e6667 100644
--- a/bpf/aya-bpf/src/maps/stack_trace.rs
+++ b/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)
-		}
-	}
-}
\ No newline at end of file
+    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)
+        }
+    }
+}