Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/replace fasthash #58

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libpcap-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ plugin_tls_stats = ["rusticata","tls-parser"]
base16ct = { version="0.2", features=["alloc"], optional=true }
base64ct = { version="1.5", features=["alloc"], optional=true }
crossbeam-channel = "0.5"
fasthash = "0.4"
fnv = "1.0"
indexmap = { version="2.2", features=["serde"] }
lazy_static = "1.2"
Expand Down
102 changes: 89 additions & 13 deletions libpcap-analyzer/src/threaded_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use libpcap_tools::*;
use pcap_parser::data::PacketData;
use pnet_packet::ethernet::{EtherType, EtherTypes, EthernetPacket};
use std::cmp::min;
use std::hash::Hasher;
use std::panic::AssertUnwindSafe;
use std::sync::{Arc, Barrier};
use std::thread;
Expand Down Expand Up @@ -208,21 +209,91 @@ pub(crate) fn extern_dispatch_l3<'a>(
ethertype: EtherType,
) -> Result<(), Error> {
let n_workers = jobs.len();
let i = fan_out(data, ethertype, n_workers);
let i = softrss_xor_fnv(data, ethertype, n_workers);
debug_assert!(i < n_workers);
// trace!("sending job to worker {i}");
jobs[i]
.send(Job::New(packet, ctx.clone(), data, ethertype))
.or(Err(Error::Generic("Error while sending job")))
}

fn fan_out(data: &[u8], ethertype: EtherType, n_workers: usize) -> usize {
#[allow(dead_code)]
fn softrss_toeplitz(data: &[u8], ethertype: EtherType, n_workers: usize) -> usize {
use crate::toeplitz::{toeplitz_hash_aligned32_v2, AlignedU8, SYMMETRIC_KEY_U32BE};
match ethertype {
EtherTypes::Ipv4 => {
if data.len() >= 20 {
let mut aligned = AlignedU8([0; 20]);
let buf = &mut aligned.0;
let sz = 8;
let src_dst_addrs = &data[12..20];
buf[0..sz].copy_from_slice(src_dst_addrs);
// we may append source and destination ports
// XXX breaks fragmentation
// if data[9] == crate::plugin::TRANSPORT_TCP || data[9] == crate::plugin::TRANSPORT_UDP {
// if data.len() >= 24 {
// // source port, in network-order
// buf[8] = data[20];
// buf[9] = data[21];
// // destination port, in network-order
// buf[10] = data[22];
// buf[11] = data[23];
// sz = 12;
// }
// }
let hash = toeplitz_hash_aligned32_v2(SYMMETRIC_KEY_U32BE, &buf[..sz]);

// debug!("{:?} -- hash --> 0x{:x}", buf, hash);
// ((hash >> 24) ^ (hash & 0xff)) as usize % n_workers
hash as usize % n_workers
} else {
n_workers - 1
}
}
EtherTypes::Ipv6 => {
if data.len() >= 40 {
let mut aligned = AlignedU8([0; 40]);
let buf = &mut aligned.0;
let sz = 40;
let src_dst_addrs = &data[8..40];
buf[0..sz].copy_from_slice(src_dst_addrs);
// we may append source and destination ports
// XXX breaks fragmentation
// if data[6] == crate::plugin::TRANSPORT_TCP || data[6] == crate::plugin::TRANSPORT_UDP {
// if data.len() >= 44 {
// // source port, in network-order
// buf[33] = data[40];
// buf[34] = data[41];
// // destination port, in network-order
// buf[35] = data[42];
// buf[36] = data[43];
// sz += 4;
// }
// }
let hash = toeplitz_hash_aligned32_v2(SYMMETRIC_KEY_U32BE, &buf[..sz]);

// debug!("{:?} -- hash --> 0x{:x}", buf, hash);
// ((hash >> 24) ^ (hash & 0xff)) as usize % n_workers
hash as usize % n_workers
} else {
n_workers - 1
}
}
_ => 0,
}
}

// Receive-Side Scaling (RSS) based on XOR function and Fowler–Noll–Vo (FNV) hash function
// NOTE: This seems to be balanced less evenly than toeplitz hash, but still good
#[allow(dead_code)]
fn softrss_xor_fnv(data: &[u8], ethertype: EtherType, n_workers: usize) -> usize {
// This seed is just random data. It has no cryptographic value, it is just used to
// seed the hash function
const SEED: u64 = 0x3d58_216b_caff_e84b;

match ethertype {
EtherTypes::Ipv4 => {
if data.len() >= 20 {
// let src = &data[12..15];
// let dst = &data[16..19];
// let proto = data[9];
// (src[0] ^ dst[0] ^ proto) as usize % n_workers
let mut buf: [u8; 20] = [0; 20];
let sz = 4;
buf[0] = data[12] ^ data[16];
Expand All @@ -242,8 +313,11 @@ fn fan_out(data: &[u8], ethertype: EtherType, n_workers: usize) -> usize {
// sz = 12;
// }
// }
// let hash = crate::toeplitz::toeplitz_hash(crate::toeplitz::KEY, &buf[..sz]);
let hash = fasthash::metro::hash64(&buf[..sz]);

let mut hasher = fnv::FnvHasher::with_key(SEED);
hasher.write(&buf[..sz]);
let hash = hasher.finish();

// debug!("{:?} -- hash --> 0x{:x}", buf, hash);
// ((hash >> 24) ^ (hash & 0xff)) as usize % n_workers
hash as usize % n_workers
Expand Down Expand Up @@ -274,8 +348,10 @@ fn fan_out(data: &[u8], ethertype: EtherType, n_workers: usize) -> usize {
// sz += 4;
// }
// }
// let hash = crate::toeplitz::toeplitz_hash(crate::toeplitz::KEY, &buf[..sz]);
let hash = fasthash::metro::hash64(&buf[..sz]);
let mut hasher = fnv::FnvHasher::with_key(SEED);
hasher.write(&buf[..sz]);
let hash = hasher.finish();

// debug!("{:?} -- hash --> 0x{:x}", buf, hash);
// ((hash >> 24) ^ (hash & 0xff)) as usize % n_workers
hash as usize % n_workers
Expand All @@ -296,21 +372,21 @@ fn worker(mut a: Analyzer, idx: usize, r: Receiver<Job>, barrier: Arc<Barrier>)
Job::Exit => break,
Job::PrintDebug => {
{
debug!("thread {}: hash table size: {}", idx, a.flows.len());
debug!("worker {}: flow table size: {}", idx, a.flows.len());
};
}
Job::New(packet, ctx, data, ethertype) => {
pcap_index = ctx.pcap_index;
let span = span!(Level::DEBUG, "worker", pcap_index);
let _enter = span.enter();
trace!("thread {}: got a job", idx);
trace!("worker {}: got a job", idx);
let h3_res = handle_l3(&packet, &ctx, data, ethertype, &mut a);
if h3_res.is_err() {
warn!("thread {}: handle_l3 failed", idx);
}
}
Job::Wait => {
trace!("Thread {}: waiting at barrier", idx);
trace!("worker {}: waiting at barrier", idx);
barrier.wait();
}
}
Expand Down
Loading
Loading