diff --git a/src/xdpfw_kern.c b/src/xdpfw_kern.c index 8747ad4..800cbda 100644 --- a/src/xdpfw_kern.c +++ b/src/xdpfw_kern.c @@ -59,7 +59,7 @@ struct bpf_map_def SEC("maps") filters_map = struct bpf_map_def SEC("maps") stats_map = { - .type = BPF_MAP_TYPE_ARRAY, + .type = BPF_MAP_TYPE_PERCPU_ARRAY, .key_size = sizeof(uint32_t), .value_size = sizeof(struct xdpfw_stats), .max_entries = 1 @@ -200,7 +200,7 @@ int xdp_prog_main(struct xdp_md *ctx) // Increase blocked stats entry. if (stats) { - __sync_fetch_and_add(&stats->blocked, 1); + stats->blocked++; } #endif @@ -634,7 +634,7 @@ int xdp_prog_main(struct xdp_md *ctx) if (stats) { - __sync_fetch_and_add(&stats->blocked, 1); + stats->blocked++; } return XDP_DROP; @@ -643,7 +643,7 @@ int xdp_prog_main(struct xdp_md *ctx) { if (stats) { - __sync_fetch_and_add(&stats->allowed, 1); + stats->allowed++; } } diff --git a/src/xdpfw_loader.c b/src/xdpfw_loader.c index 9c53c50..db9e177 100644 --- a/src/xdpfw_loader.c +++ b/src/xdpfw_loader.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -446,6 +447,9 @@ int main(int argc, char *argv[]) // Signal. signal(SIGINT, signalHndl); + // Receive CPU count for stats map parsing. + int cpus = get_nprocs_conf(); + while (cont) { // Get current time. @@ -468,12 +472,21 @@ int main(int argc, char *argv[]) if ((curTime - statsLastUpdated) > 2 && !conf->nostats) { uint32_t key = 0; - struct xdpfw_stats stats; + struct xdpfw_stats stats[cpus]; + + uint64_t allowed = 0; + uint64_t dropped = 0; bpf_map_lookup_elem(stats_map_fd, &key, &stats); + for (int i = 0; i < cpus; i++) + { + allowed += stats[i].allowed; + dropped += stats[i].blocked; + } + fflush(stdout); - fprintf(stdout, "\rPackets Allowed: %" PRIu64 " | Packets Blocked: %" PRIu64, stats.allowed, stats.blocked); + fprintf(stdout, "\rPackets Allowed: %" PRIu64 " | Packets Blocked: %" PRIu64, allowed, dropped); statsLastUpdated = time(NULL); }