瀏覽代碼

Merge pull request #103 from dave-tucker/redirect_sk

bpf: Implement redirect for skbs on sockmap/sockhash
Alessandro Decina 3 年之前
父節點
當前提交
0b3e398f11
共有 2 個文件被更改,包括 24 次插入6 次删除
  1. 12 3
      bpf/aya-bpf/src/maps/sock_hash.rs
  2. 12 3
      bpf/aya-bpf/src/maps/sock_map.rs

+ 12 - 3
bpf/aya-bpf/src/maps/sock_hash.rs

@@ -4,9 +4,9 @@ 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},
+    helpers::{bpf_msg_redirect_hash, bpf_sk_redirect_hash, bpf_sock_hash_update},
     maps::PinningType,
-    programs::SkMsgContext,
+    programs::{SkMsgContext, SkSkbContext},
     BpfContext,
 };
 
@@ -66,7 +66,7 @@ impl<K> SockHash<K> {
         }
     }
 
-    pub unsafe fn redirect(&mut self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 {
+    pub unsafe fn redirect_msg(&mut self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 {
         bpf_msg_redirect_hash(
             ctx.as_ptr() as *mut _,
             &mut self.def as *mut _ as *mut _,
@@ -74,4 +74,13 @@ impl<K> SockHash<K> {
             flags,
         )
     }
+
+    pub unsafe fn redirect_skb(&mut self, ctx: &SkSkbContext, key: &mut K, flags: u64) -> i64 {
+        bpf_sk_redirect_hash(
+            ctx.as_ptr() as *mut _,
+            &mut self.def as *mut _ as *mut _,
+            key as *mut _ as *mut _,
+            flags,
+        )
+    }
 }

+ 12 - 3
bpf/aya-bpf/src/maps/sock_map.rs

@@ -4,9 +4,9 @@ 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},
+    helpers::{bpf_msg_redirect_map, bpf_sk_redirect_map, bpf_sock_map_update},
     maps::PinningType,
-    programs::SkMsgContext,
+    programs::{SkMsgContext, SkSkbContext},
     BpfContext,
 };
 
@@ -63,7 +63,7 @@ impl SockMap {
         }
     }
 
-    pub unsafe fn redirect(&mut self, ctx: &SkMsgContext, index: u32, flags: u64) -> i64 {
+    pub unsafe fn redirect_msg(&mut self, ctx: &SkMsgContext, index: u32, flags: u64) -> i64 {
         bpf_msg_redirect_map(
             ctx.as_ptr() as *mut _,
             &mut self.def as *mut _ as *mut _,
@@ -71,4 +71,13 @@ impl SockMap {
             flags,
         )
     }
+
+    pub unsafe fn redirect_skb(&mut self, ctx: &SkSkbContext, index: u32, flags: u64) -> i64 {
+        bpf_sk_redirect_map(
+            ctx.as_ptr() as *mut _,
+            &mut self.def as *mut _ as *mut _,
+            index,
+            flags,
+        )
+    }
 }