Skip to content

Commit

Permalink
feat(ct-metrics): simplify preprocessor for CONNTRACK_METRICS
Browse files Browse the repository at this point in the history
  • Loading branch information
SRodi committed Dec 11, 2024
1 parent ed2706c commit 14f219c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 15 deletions.
14 changes: 0 additions & 14 deletions pkg/plugin/conntrack/_cprog/conntrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ static __always_inline bool _ct_create_new_tcp_connection(struct packet *p, stru
new_value.traffic_direction = _ct_get_traffic_direction(observation_point);

#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
new_value.conntrack_metadata.packets_forward_count = 1;
new_value.conntrack_metadata.bytes_forward_count = p->bytes;
// Update initial conntrack metadata for the connection.
__builtin_memcpy(&p->conntrack_metadata, &new_value.conntrack_metadata, sizeof(struct conntrackmetadata));
#endif
#endif // CONNTRACK_METRICS

// Update packet
Expand Down Expand Up @@ -179,12 +177,10 @@ static __always_inline bool _ct_handle_udp_connection(struct packet *p, struct c
new_value.last_report_tx_dir = now;
new_value.traffic_direction = _ct_get_traffic_direction(observation_point);
#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
new_value.conntrack_metadata.packets_forward_count = 1;
new_value.conntrack_metadata.bytes_forward_count = p->bytes;
// Update packet's conntrack metadata.
__builtin_memcpy(&p->conntrack_metadata, &new_value.conntrack_metadata, sizeof(struct conntrackmetadata));;
#endif
#endif // CONNTRACK_METRICS

// Update packet
Expand Down Expand Up @@ -230,29 +226,23 @@ static __always_inline bool _ct_handle_tcp_connection(struct packet *p, struct c
new_value.flags_seen_rx_dir = p->flags;
new_value.last_report_rx_dir = now;
#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
new_value.conntrack_metadata.bytes_reply_count = p->bytes;
new_value.conntrack_metadata.packets_reply_count = 1;
#endif
#endif // CONNTRACK_METRICS
bpf_map_update_elem(&retina_conntrack, &reverse_key, &new_value, BPF_ANY);
} else { // Otherwise, the packet is considered as a packet in the send direction.
p->is_reply = false;
new_value.flags_seen_tx_dir = p->flags;
new_value.last_report_tx_dir = now;
#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
new_value.conntrack_metadata.bytes_forward_count = p->bytes;
new_value.conntrack_metadata.packets_forward_count = 1;
#endif
#endif // CONNTRACK_METRICS
bpf_map_update_elem(&retina_conntrack, &key, &new_value, BPF_ANY);
}
#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
// Update packet's conntrack metadata.
__builtin_memcpy(&p->conntrack_metadata, &new_value.conntrack_metadata, sizeof(struct conntrackmetadata));
#endif
#endif // CONNTRACK_METRICS
return true;
}
Expand Down Expand Up @@ -373,13 +363,11 @@ static __always_inline __attribute__((unused)) bool ct_process_packet(struct pac
p->is_reply = false;
p->traffic_direction = entry->traffic_direction;
#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
// Update packet count and bytes count on conntrack entry.
WRITE_ONCE(entry->conntrack_metadata.packets_forward_count, READ_ONCE(entry->conntrack_metadata.packets_forward_count) + 1);
WRITE_ONCE(entry->conntrack_metadata.bytes_forward_count, READ_ONCE(entry->conntrack_metadata.bytes_forward_count) + p->bytes);
// Update packet's conntract metadata.
__builtin_memcpy(&p->conntrack_metadata, &entry->conntrack_metadata, sizeof(struct conntrackmetadata));
#endif
#endif // CONNTRACK_METRICS
return _ct_should_report_packet(entry, p->flags, CT_PACKET_DIR_TX, &key);
}
Expand All @@ -397,13 +385,11 @@ static __always_inline __attribute__((unused)) bool ct_process_packet(struct pac
p->is_reply = true;
p->traffic_direction = entry->traffic_direction;
#ifdef CONNTRACK_METRICS
#if CONNTRACK_METRICS == 1
// Update packet count and bytes count on conntrack entry.
WRITE_ONCE(entry->conntrack_metadata.packets_reply_count, READ_ONCE(entry->conntrack_metadata.packets_reply_count) + 1);
WRITE_ONCE(entry->conntrack_metadata.bytes_reply_count, READ_ONCE(entry->conntrack_metadata.bytes_reply_count) + p->bytes);
// Update packet's conntract metadata.
__builtin_memcpy(&p->conntrack_metadata, &entry->conntrack_metadata, sizeof(struct conntrackmetadata));
#endif
#endif // CONNTRACK_METRICS
return _ct_should_report_packet(entry, p->flags, CT_PACKET_DIR_RX, &reverse_key);
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugin/conntrack/_cprog/dynamic.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define CONNTRACK_METRICS 0
// Place holder header file that will be replaced by the actual header file during runtime
// DO NOT DELETE

0 comments on commit 14f219c

Please sign in to comment.