Merge pull request #547 from LibreQoE/issue_518_reloading_2

Issue 518 reloading 2
This commit is contained in:
Herbert "TheBracket" Wolverson 2024-08-22 13:07:06 -05:00 committed by GitHub
commit 001694c77f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 215 additions and 197 deletions

View File

@ -1,15 +1,15 @@
#![allow(dead_code)] #![allow(dead_code)]
use anyhow::{Error, Result};
use libbpf_sys::{
bpf_map_delete_elem, bpf_map_get_next_key, bpf_map_lookup_elem,
bpf_map_update_elem, bpf_obj_get, BPF_NOEXIST,
};
use std::{ use std::{
ffi::{c_void, CString}, ffi::{c_void, CString},
marker::PhantomData, marker::PhantomData,
ptr::null_mut, ptr::null_mut,
}; };
use anyhow::{Error, Result};
use libbpf_sys::{bpf_map_delete_elem, bpf_map_get_next_key, bpf_map_lookup_elem, bpf_map_update_elem, BPF_NOEXIST, bpf_obj_get};
use crate::lqos_kernel::bpf::bpf_map_delete_batch;
/// Represents an underlying BPF map, accessed via the filesystem. /// Represents an underlying BPF map, accessed via the filesystem.
/// `BpfMap` *only* talks to shared (not PER-CPU) variants of maps. /// `BpfMap` *only* talks to shared (not PER-CPU) variants of maps.
/// ///
@ -39,7 +39,7 @@ where
} }
} }
/// Iterates the undlering BPF map, and adds the results /// Iterates the underlying BPF map, and adds the results
/// to a vector. Each entry contains a `key, value` tuple. /// to a vector. Each entry contains a `key, value` tuple.
/// ///
/// This has performance issues due to excessive cloning /// This has performance issues due to excessive cloning
@ -220,6 +220,24 @@ where
} }
Ok(()) Ok(())
} }
/// Clears an eBPF map using `bpf_map_delete_batch`, which
/// has better locking semantics than per-row.
pub fn clear_bulk(&mut self) -> Result<()> {
let mut keys: Vec<K> = self.dump_vec().iter().map(|(k, _)| {
k.clone()
}).collect();
let mut count = keys.len() as u32;
loop {
let ret = unsafe {
bpf_map_delete_batch(self.fd, keys.as_mut_ptr() as *mut c_void, &mut count, null_mut())
};
if ret != 0 || count == 0 {
break;
}
}
Ok(())
}
} }
impl<K, V> Drop for BpfMap<K, V> { impl<K, V> Drop for BpfMap<K, V> {

View File

@ -100,6 +100,6 @@ pub fn list_mapped_ips() -> Result<Vec<(IpHashKey, IpHashData)>> {
/// destinations. /// destinations.
pub fn clear_hot_cache() -> Result<()> { pub fn clear_hot_cache() -> Result<()> {
let mut bpf_map = BpfMap::<XdpIpAddress, IpHashData>::from_path("/sys/fs/bpf/ip_to_cpu_and_tc_hotcache")?; let mut bpf_map = BpfMap::<XdpIpAddress, IpHashData>::from_path("/sys/fs/bpf/ip_to_cpu_and_tc_hotcache")?;
bpf_map.clear()?; bpf_map.clear_bulk()?;
Ok(()) Ok(())
} }