No need to pass ip_stats to function.

This commit is contained in:
Christian Deacon
2025-02-11 08:17:20 -05:00
parent 82d3c50bf3
commit 376b39d307
2 changed files with 26 additions and 32 deletions

View File

@@ -10,7 +10,6 @@
*
* @param pps A pointer to the PPS integer.
* @param bps A pointer to the BPS integer.
* @param ip_stats A pointer to pointer to the IP stats structure value.
* @param ip The client's source IP.
* @param port The client's source port.
* @param protocol The client's protocol.
@@ -19,7 +18,7 @@
*
* @return void
*/
static __always_inline void UpdateIpStats(__u64 *pps, __u64 *bps, struct ip_stats **ip_stats, __u32 ip, __u16 port, __u8 protocol, __u16 pkt_len, __u64 now)
static __always_inline void UpdateIpStats(__u64 *pps, __u64 *bps, __u32 ip, __u16 port, __u8 protocol, __u16 pkt_len, __u64 now)
{
#ifdef USE_FLOW_RL
struct flow key = {0};
@@ -27,29 +26,29 @@ static __always_inline void UpdateIpStats(__u64 *pps, __u64 *bps, struct ip_stat
key.port = port;
key.protocol = protocol;
*ip_stats = bpf_map_lookup_elem(&ip_stats_map, &key);
struct ip_stats *ip_stats = bpf_map_lookup_elem(&ip_stats_map, &key);
#else
*ip_stats = bpf_map_lookup_elem(&ip_stats_map, &ip);
struct ip_stats *ip_stats* = bpf_map_lookup_elem(&ip_stats_map, &ip);
#endif
if (*ip_stats)
if (ip_stats)
{
// Check for next update.
if (now > (*ip_stats)->next_update)
if (now > ip_stats->next_update)
{
(*ip_stats)->pps = 1;
(*ip_stats)->bps = pkt_len;
(*ip_stats)->next_update = now + NANO_TO_SEC;
ip_stats->pps = 1;
ip_stats->bps = pkt_len;
ip_stats->next_update = now + NANO_TO_SEC;
}
else
{
// Increment PPS and BPS using built-in functions.
__sync_fetch_and_add(&(*ip_stats)->pps, 1);
__sync_fetch_and_add(&(*ip_stats)->bps, pkt_len);
__sync_fetch_and_add(&ip_stats->pps, 1);
__sync_fetch_and_add(&ip_stats->bps, pkt_len);
}
*pps = (*ip_stats)->pps;
*bps = (*ip_stats)->bps;
*pps = ip_stats->pps;
*bps = ip_stats->bps;
}
else
{
@@ -76,7 +75,6 @@ static __always_inline void UpdateIpStats(__u64 *pps, __u64 *bps, struct ip_stat
*
* @param pps A pointer to the PPS integer.
* @param bps A pointer to the BPS integer.
* @param ip_stats A pointer to pointer to the IP stats structure value.
* @param ip The client's source IP.
* @param port The client's source port.
* @param protocol The client's protocol.
@@ -85,7 +83,7 @@ static __always_inline void UpdateIpStats(__u64 *pps, __u64 *bps, struct ip_stat
*
* @return void
*/
static __always_inline void UpdateIp6Stats(__u64 *pps, __u64 *bps, struct ip_stats **ip_stats, __u128 *ip, __u16 port, __u8 protocol, __u16 pkt_len, __u64 now)
static __always_inline void UpdateIp6Stats(__u64 *pps, __u64 *bps, __u128 *ip, __u16 port, __u8 protocol, __u16 pkt_len, __u64 now)
{
#ifdef USE_FLOW_RL
struct flow6 key = {0};
@@ -93,29 +91,29 @@ static __always_inline void UpdateIp6Stats(__u64 *pps, __u64 *bps, struct ip_sta
key.port = port;
key.protocol = protocol;
*ip_stats = bpf_map_lookup_elem(&ip_stats_map, &key);
struct ip_stats *ip_stats = bpf_map_lookup_elem(&ip_stats_map, &key);
#else
*ip_stats = bpf_map_lookup_elem(&ip_stats_map, ip);
struct ip_stats *ip_stats = bpf_map_lookup_elem(&ip_stats_map, ip);
#endif
if (*ip_stats)
if (ip_stats)
{
// Check for next update.
if (now > (*ip_stats)->next_update)
if (now > ip_stats->next_update)
{
(*ip_stats)->pps = 1;
(*ip_stats)->bps = pkt_len;
(*ip_stats)->next_update = now + NANO_TO_SEC;
ip_stats->pps = 1;
ip_stats->bps = pkt_len;
ip_stats->next_update = now + NANO_TO_SEC;
}
else
{
// Increment PPS and BPS using built-in functions.
__sync_fetch_and_add(&(*ip_stats)->pps, 1);
__sync_fetch_and_add(&(*ip_stats)->bps, pkt_len);
__sync_fetch_and_add(&ip_stats->pps, 1);
__sync_fetch_and_add(&ip_stats->bps, pkt_len);
}
*pps = (*ip_stats)->pps;
*bps = (*ip_stats)->bps;
*pps = ip_stats->pps;
*bps = ip_stats->bps;
}
else
{

View File

@@ -231,19 +231,15 @@ int xdp_prog_main(struct xdp_md *ctx)
// Update client stats (PPS/BPS).
__u64 pps = 0;
__u64 bps = 0;
struct ip_stats *ip_stats = NULL;
if (iph6)
{
UpdateIp6Stats(&pps, &bps, &ip_stats, &src_ip6, src_port, protocol, pkt_len, now);
UpdateIp6Stats(&pps, &bps, &src_ip6, src_port, protocol, pkt_len, now);
}
else if (iph)
{
UpdateIpStats(&pps, &bps, &ip_stats, iph->saddr, src_port, protocol, pkt_len, now);
UpdateIpStats(&pps, &bps, iph->saddr, src_port, protocol, pkt_len, now);
}
bpf_printk("PPS => %llu. BPS => %llu.\n", pps, bps);
for (__u8 i = 0; i < MAX_FILTERS; i++)
{