Add cache invalidation to the hot cache. Whenever an IP mapping

changes, the cache is invalidated - meaning it will re-cache
the correct values.
This commit is contained in:
Herbert Wolverson 2024-05-17 14:30:30 -05:00
parent 6dc811968b
commit d9eedcd804

View File

@ -36,6 +36,7 @@ pub fn add_ip_to_tc(
let mut value =
IpHashData { cpu: ip_to_add.cpu, tc_handle: ip_to_add.handle() };
bpf_map.insert_or_update(&mut key, &mut value)?;
clear_hot_cache()?;
Ok(())
}
@ -56,6 +57,7 @@ pub fn del_ip_from_tc(address: &str, upload: bool) -> Result<()> {
let ip = XdpIpAddress::from_ip(ip);
let mut key = IpHashKey { prefixlen: ip_to_add.prefix, address: ip.0 };
bpf_map.delete(&mut key)?;
clear_hot_cache()?;
Ok(())
}
@ -71,6 +73,8 @@ pub fn clear_ips_from_tc() -> Result<()> {
)?;
bpf_map.clear()?;
clear_hot_cache()?;
Ok(())
}
@ -89,3 +93,12 @@ pub fn list_mapped_ips() -> Result<Vec<(IpHashKey, IpHashData)>> {
Ok(raw)
}
/// Clears the "hot cache", which should be done whenever you change the IP
/// mappings - because otherwise cached data will keep going to the previous
/// destinations.
fn clear_hot_cache() -> Result<()> {
let mut bpf_map = BpfMap::<XdpIpAddress, IpHashData>::from_path("/sys/fs/bpf/ip_to_cpu_and_tc_hotcache")?;
bpf_map.clear()?;
Ok(())
}