Skip to content

Commit

Permalink
No need to pass ip_stats to function.
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemann committed Feb 11, 2025
1 parent 82d3c50 commit 376b39d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
50 changes: 24 additions & 26 deletions src/xdp/rl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,37 +18,37 @@
*
* @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};
key.ip = ip;
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
{
Expand All @@ -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.
Expand All @@ -85,37 +83,37 @@ 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};
key.ip = *ip;
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
{
Expand Down
8 changes: 2 additions & 6 deletions src/xdpfw_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down

0 comments on commit 376b39d

Please sign in to comment.