1
0
Fork 0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-05-27 21:26:18 +02:00

Reworked increment counters functions to use references

This commit is contained in:
Pavel Odintsov 2023-02-28 20:45:57 +00:00
parent 59af2ed719
commit eb982f4820

View File

@ -7,76 +7,76 @@ extern time_t current_inaccurate_time;
#ifdef USE_NEW_ATOMIC_BUILTINS
// Increment fields using data from specified packet
void increment_outgoing_counters(subnet_counter_t* current_element,
simple_packet_t& current_packet,
void increment_outgoing_counters(subnet_counter_t& current_element,
const simple_packet_t& current_packet,
uint64_t sampled_number_of_packets,
uint64_t sampled_number_of_bytes) {
// Update last update time
current_element->last_update_time = current_inaccurate_time;
current_element.last_update_time = current_inaccurate_time;
// Main packet/bytes counter
__atomic_add_fetch(&current_element->total.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->total.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.total.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.total.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
// Fragmented IP packets
if (current_packet.ip_fragmented) {
__atomic_add_fetch(&current_element->fragmented.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->fragmented.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.fragmented.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.fragmented.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
}
if (current_packet.protocol == IPPROTO_TCP) {
__atomic_add_fetch(&current_element->tcp.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->tcp.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
if (extract_bit_value(current_packet.flags, TCP_SYN_FLAG_SHIFT)) {
__atomic_add_fetch(&current_element->tcp_syn.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->tcp_syn.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp_syn.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp_syn.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
}
} else if (current_packet.protocol == IPPROTO_UDP) {
__atomic_add_fetch(&current_element->udp.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->udp.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.udp.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.udp.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
} else if (current_packet.protocol == IPPROTO_ICMP) {
__atomic_add_fetch(&current_element->icmp.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->icmp.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.icmp.out_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.icmp.out_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
// no flow tracking for icmp
} else {
}
}
#else
// Increment fields using data from specified packet
void increment_outgoing_counters(subnet_counter_t* current_element,
simple_packet_t& current_packet,
void increment_outgoing_counters(subnet_counter_t& current_element,
const simple_packet_t& current_packet,
uint64_t sampled_number_of_packets,
uint64_t sampled_number_of_bytes) {
// Update last update time
current_element->last_update_time = current_inaccurate_time;
current_element.last_update_time = current_inaccurate_time;
// Main packet/bytes counter
__sync_fetch_and_add(&current_element->total.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->total.out_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.total.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.total.out_bytes, sampled_number_of_bytes);
// Fragmented IP packets
if (current_packet.ip_fragmented) {
__sync_fetch_and_add(&current_element->fragmented.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->fragmented.out_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.fragmented.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.fragmented.out_bytes, sampled_number_of_bytes);
}
if (current_packet.protocol == IPPROTO_TCP) {
__sync_fetch_and_add(&current_element->tcp.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->tcp.out_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.tcp.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.tcp.out_bytes, sampled_number_of_bytes);
if (extract_bit_value(current_packet.flags, TCP_SYN_FLAG_SHIFT)) {
__sync_fetch_and_add(&current_element->tcp_syn.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->tcp_syn.out_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.tcp_syn.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.tcp_syn.out_bytes, sampled_number_of_bytes);
}
} else if (current_packet.protocol == IPPROTO_UDP) {
__sync_fetch_and_add(&current_element->udp.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->udp.out_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.udp.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.udp.out_bytes, sampled_number_of_bytes);
} else if (current_packet.protocol == IPPROTO_ICMP) {
__sync_fetch_and_add(&current_element->icmp.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->icmp.out_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.icmp.out_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.icmp.out_bytes, sampled_number_of_bytes);
// no flow tracking for icmp
} else {
}
@ -86,39 +86,39 @@ void increment_outgoing_counters(subnet_counter_t* current_element,
#ifdef USE_NEW_ATOMIC_BUILTINS
// This function increments all our accumulators according to data from packet
void increment_incoming_counters(subnet_counter_t* current_element,
simple_packet_t& current_packet,
void increment_incoming_counters(subnet_counter_t& current_element,
const simple_packet_t& current_packet,
uint64_t sampled_number_of_packets,
uint64_t sampled_number_of_bytes) {
// Uodate last update time
current_element->last_update_time = current_inaccurate_time;
current_element.last_update_time = current_inaccurate_time;
// Main packet/bytes counter
__atomic_add_fetch(&current_element->total.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->total.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.total.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.total.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
// Count fragmented IP packets
if (current_packet.ip_fragmented) {
__atomic_add_fetch(&current_element->fragmented.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->fragmented.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.fragmented.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.fragmented.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
}
// Count per protocol packets
if (current_packet.protocol == IPPROTO_TCP) {
__atomic_add_fetch(&current_element->tcp.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->tcp.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
if (extract_bit_value(current_packet.flags, TCP_SYN_FLAG_SHIFT)) {
__atomic_add_fetch(&current_element->tcp_syn.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->tcp_syn.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp_syn.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.tcp_syn.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
}
} else if (current_packet.protocol == IPPROTO_UDP) {
__atomic_add_fetch(&current_element->udp.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->udp.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.udp.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.udp.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
} else if (current_packet.protocol == IPPROTO_ICMP) {
__atomic_add_fetch(&current_element->icmp.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element->icmp.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.icmp.in_packets, sampled_number_of_packets, __ATOMIC_RELAXED);
__atomic_add_fetch(&current_element.icmp.in_bytes, sampled_number_of_bytes, __ATOMIC_RELAXED);
} else {
// TBD
}
@ -127,39 +127,39 @@ void increment_incoming_counters(subnet_counter_t* current_element,
#else
// This function increments all our accumulators according to data from packet
void increment_incoming_counters(subnet_counter_t* current_element,
simple_packet_t& current_packet,
void increment_incoming_counters(subnet_counter_t& current_element,
const simple_packet_t& current_packet,
uint64_t sampled_number_of_packets,
uint64_t sampled_number_of_bytes) {
// Uodate last update time
current_element->last_update_time = current_inaccurate_time;
current_element.last_update_time = current_inaccurate_time;
// Main packet/bytes counter
__sync_fetch_and_add(&current_element->total.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->total.in_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.total.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.total.in_bytes, sampled_number_of_bytes);
// Count fragmented IP packets
if (current_packet.ip_fragmented) {
__sync_fetch_and_add(&current_element->fragmented.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->fragmented.in_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.fragmented.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.fragmented.in_bytes, sampled_number_of_bytes);
}
// Count per protocol packets
if (current_packet.protocol == IPPROTO_TCP) {
__sync_fetch_and_add(&current_element->tcp.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->tcp.in_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.tcp.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.tcp.in_bytes, sampled_number_of_bytes);
if (extract_bit_value(current_packet.flags, TCP_SYN_FLAG_SHIFT)) {
__sync_fetch_and_add(&current_element->tcp_syn.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->tcp_syn.in_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.tcp_syn.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.tcp_syn.in_bytes, sampled_number_of_bytes);
}
} else if (current_packet.protocol == IPPROTO_UDP) {
__sync_fetch_and_add(&current_element->udp.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->udp.in_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.udp.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.udp.in_bytes, sampled_number_of_bytes);
} else if (current_packet.protocol == IPPROTO_ICMP) {
__sync_fetch_and_add(&current_element->icmp.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element->icmp.in_bytes, sampled_number_of_bytes);
__sync_fetch_and_add(&current_element.icmp.in_packets, sampled_number_of_packets);
__sync_fetch_and_add(&current_element.icmp.in_bytes, sampled_number_of_bytes);
} else {
// TBD
}