Sfoglia il codice sorgente

Merge pull request #326 from NoneTirex/fix-map-delete-return-value

Change condititon for some bpf helpers
Alessandro Decina 2 anni fa
parent
commit
150dc1b610

+ 28 - 28
bpf/aya-bpf/src/helpers.rs

@@ -48,11 +48,11 @@ pub unsafe fn bpf_probe_read<T>(src: *const T) -> Result<T, c_long> {
         mem::size_of::<T>() as u32,
         src as *const c_void,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(v.assume_init())
+    } else {
+        Err(ret)
     }
-
-    Ok(v.assume_init())
 }
 
 /// Read bytes from the pointer `src` into the provided destination buffer.
@@ -84,11 +84,11 @@ pub unsafe fn bpf_probe_read_buf(src: *const u8, dst: &mut [u8]) -> Result<(), c
         dst.len() as u32,
         src as *const c_void,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(())
+    } else {
+        Err(ret)
     }
-
-    Ok(())
 }
 
 /// Read bytes stored at the _user space_ pointer `src` and store them as a `T`.
@@ -121,11 +121,11 @@ pub unsafe fn bpf_probe_read_user<T>(src: *const T) -> Result<T, c_long> {
         mem::size_of::<T>() as u32,
         src as *const c_void,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(v.assume_init())
+    } else {
+        Err(ret)
     }
-
-    Ok(v.assume_init())
 }
 
 /// Read bytes from the _user space_ pointer `src` into the provided destination
@@ -155,11 +155,11 @@ pub unsafe fn bpf_probe_read_user_buf(src: *const u8, dst: &mut [u8]) -> Result<
         dst.len() as u32,
         src as *const c_void,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(())
+    } else {
+        Err(ret)
     }
-
-    Ok(())
 }
 
 /// Read bytes stored at the _kernel space_ pointer `src` and store them as a `T`.
@@ -192,11 +192,11 @@ pub unsafe fn bpf_probe_read_kernel<T>(src: *const T) -> Result<T, c_long> {
         mem::size_of::<T>() as u32,
         src as *const c_void,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(v.assume_init())
+    } else {
+        Err(ret)
     }
-
-    Ok(v.assume_init())
 }
 
 /// Read bytes from the _kernel space_ pointer `src` into the provided destination
@@ -226,11 +226,11 @@ pub unsafe fn bpf_probe_read_kernel_buf(src: *const u8, dst: &mut [u8]) -> Resul
         dst.len() as u32,
         src as *const c_void,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(())
+    } else {
+        Err(ret)
     }
-
-    Ok(())
 }
 
 /// Read a null-terminated string stored at `src` into `dest`.
@@ -397,11 +397,11 @@ pub unsafe fn bpf_probe_write_user<T>(dst: *mut T, src: *const T) -> Result<(),
         src as *const c_void,
         mem::size_of::<T>() as u32,
     );
-    if ret < 0 {
-        return Err(ret);
+    if ret == 0 {
+        Ok(())
+    } else {
+        Err(ret)
     }
-
-    Ok(())
 }
 
 /// Read the `comm` field associated with the current task struct

+ 2 - 2
bpf/aya-bpf/src/maps/hash_map.rs

@@ -348,11 +348,11 @@ fn insert<K, V>(def: *mut bpf_map_def, key: &K, value: &V, flags: u64) -> Result
             flags,
         )
     };
-    (ret >= 0).then(|| ()).ok_or(ret)
+    (ret == 0).then(|| ()).ok_or(ret)
 }
 
 #[inline]
 fn remove<K>(def: *mut bpf_map_def, key: &K) -> Result<(), c_long> {
     let ret = unsafe { bpf_map_delete_elem(def as *mut _, key as *const _ as *const c_void) };
-    (ret >= 0).then(|| ()).ok_or(ret)
+    (ret == 0).then(|| ()).ok_or(ret)
 }

+ 2 - 2
bpf/aya-bpf/src/maps/lpm_trie.rs

@@ -78,7 +78,7 @@ impl<K, V> LpmTrie<K, V> {
                 flags,
             )
         };
-        (ret >= 0).then(|| ()).ok_or(ret)
+        (ret == 0).then(|| ()).ok_or(ret)
     }
 
     #[inline]
@@ -86,7 +86,7 @@ impl<K, V> LpmTrie<K, V> {
         let ret = unsafe {
             bpf_map_delete_elem(self.def.get() as *mut _, key as *const _ as *const c_void)
         };
-        (ret >= 0).then(|| ()).ok_or(ret)
+        (ret == 0).then(|| ()).ok_or(ret)
     }
 }
 

+ 2 - 2
bpf/aya-bpf/src/maps/queue.rs

@@ -53,14 +53,14 @@ impl<T> Queue<T> {
                 flags,
             )
         };
-        (ret >= 0).then(|| ()).ok_or(ret)
+        (ret == 0).then(|| ()).ok_or(ret)
     }
 
     pub fn pop(&self) -> Option<T> {
         unsafe {
             let mut value = mem::MaybeUninit::uninit();
             let ret = bpf_map_pop_elem(self.def.get() as *mut _, value.as_mut_ptr() as *mut _);
-            (ret >= 0).then(|| value.assume_init())
+            (ret == 0).then(|| value.assume_init())
         }
     }
 }

+ 2 - 2
bpf/aya-bpf/src/maps/sock_hash.rs

@@ -61,7 +61,7 @@ impl<K> SockHash<K> {
                 flags,
             )
         };
-        (ret >= 0).then(|| ()).ok_or(ret)
+        (ret == 0).then(|| ()).ok_or(ret)
     }
 
     pub fn redirect_msg(&self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 {
@@ -102,7 +102,7 @@ impl<K> SockHash<K> {
             }
             let ret = bpf_sk_assign(ctx.as_ptr() as *mut _, sk, flags);
             bpf_sk_release(sk);
-            (ret >= 0).then(|| ()).ok_or(1)
+            (ret == 0).then(|| ()).ok_or(1)
         }
     }
 }

+ 4 - 4
bpf/aya-bpf/src/maps/sock_map.rs

@@ -61,10 +61,10 @@ impl SockMap {
             &mut index as *mut _ as *mut c_void,
             flags,
         );
-        if ret < 0 {
-            Err(ret)
-        } else {
+        if ret == 0 {
             Ok(())
+        } else {
+            Err(ret)
         }
     }
 
@@ -102,7 +102,7 @@ impl SockMap {
             }
             let ret = bpf_sk_assign(ctx.as_ptr() as *mut _, sk, flags);
             bpf_sk_release(sk);
-            (ret >= 0).then(|| ()).ok_or(1)
+            (ret == 0).then(|| ()).ok_or(1)
         }
     }
 }

+ 26 - 26
bpf/aya-bpf/src/programs/sk_buff.rs

@@ -57,11 +57,11 @@ impl SkBuffContext {
                 &mut data as *mut _ as *mut _,
                 mem::size_of::<T>() as u32,
             );
-            if ret < 0 {
-                return Err(ret);
+            if ret == 0 {
+                Ok(data.assume_init())
+            } else {
+                Err(ret)
             }
-
-            Ok(data.assume_init())
         }
     }
 
@@ -75,12 +75,12 @@ impl SkBuffContext {
                 mem::size_of::<T>() as u32,
                 flags,
             );
-            if ret < 0 {
-                return Err(ret);
+            if ret == 0 {
+                Ok(())
+            } else {
+                Err(ret)
             }
         }
-
-        Ok(())
     }
 
     #[inline]
@@ -93,12 +93,12 @@ impl SkBuffContext {
     ) -> Result<(), c_long> {
         unsafe {
             let ret = bpf_l3_csum_replace(self.skb as *mut _, offset as u32, from, to, size);
-            if ret < 0 {
-                return Err(ret);
+            if ret == 0 {
+                Ok(())
+            } else {
+                Err(ret)
             }
         }
-
-        Ok(())
     }
 
     #[inline]
@@ -111,41 +111,41 @@ impl SkBuffContext {
     ) -> Result<(), c_long> {
         unsafe {
             let ret = bpf_l4_csum_replace(self.skb as *mut _, offset as u32, from, to, flags);
-            if ret < 0 {
-                return Err(ret);
+            if ret == 0 {
+                Ok(())
+            } else {
+                Err(ret)
             }
         }
-
-        Ok(())
     }
 
     #[inline]
     pub fn adjust_room(&self, len_diff: i32, mode: u32, flags: u64) -> Result<(), c_long> {
         let ret = unsafe { bpf_skb_adjust_room(self.as_ptr() as *mut _, len_diff, mode, flags) };
-        if ret < 0 {
-            return Err(ret);
+        if ret == 0 {
+            Ok(())
+        } else {
+            Err(ret)
         }
-
-        Ok(())
     }
 
     #[inline]
     pub fn clone_redirect(&self, if_index: u32, flags: u64) -> Result<(), c_long> {
         let ret = unsafe { bpf_clone_redirect(self.as_ptr() as *mut _, if_index, flags) };
-        if ret < 0 {
-            Err(ret)
-        } else {
+        if ret == 0 {
             Ok(())
+        } else {
+            Err(ret)
         }
     }
 
     #[inline]
     pub fn change_type(&self, ty: u32) -> Result<(), c_long> {
         let ret = unsafe { bpf_skb_change_type(self.as_ptr() as *mut _, ty) };
-        if ret < 0 {
-            Err(ret)
-        } else {
+        if ret == 0 {
             Ok(())
+        } else {
+            Err(ret)
         }
     }
 }

+ 3 - 3
bpf/aya-bpf/src/programs/sock_ops.rs

@@ -27,10 +27,10 @@ impl SockOpsContext {
 
     pub fn set_cb_flags(&self, flags: i32) -> Result<(), i64> {
         let ret = unsafe { bpf_sock_ops_cb_flags_set(self.ops, flags) };
-        if ret < 0 {
-            Err(ret)
-        } else {
+        if ret == 0 {
             Ok(())
+        } else {
+            Err(ret)
         }
     }