pf: fix the "keepcounters" to stop truncating to 32-bit

The machinery to support 64-bit counters even on 32-bit kernels had a
bug where it would unitentionally truncate the value back to 32-bits
when transferring to a new counter. This resulted in buggy be behavior
on 64-bit kernels as well.

Sponsored by:	Rubicon Communications, LLC ("Netgate")
This commit is contained in:
Mateusz Guzik 2024-06-20 17:48:52 +00:00
parent 3beb43dd4f
commit b6196537b0
2 changed files with 20 additions and 5 deletions

View file

@ -127,6 +127,14 @@ pf_counter_u64_critical_exit(void)
critical_exit();
}
static inline void
pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n)
{
MPASS(curthread->td_critnest > 0);
pfcu64->pfcu64_value += n;
}
static inline void
pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
{
@ -250,6 +258,13 @@ pf_counter_u64_critical_exit(void)
}
static inline void
pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n)
{
counter_u64_add(pfcu64->counter, n);
}
static inline void
pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
{

View file

@ -1408,15 +1408,15 @@ pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
continue;
}
pf_counter_u64_critical_enter();
pf_counter_u64_add_protected(&rule->evaluations,
pf_counter_u64_rollup_protected(&rule->evaluations,
pf_counter_u64_fetch(&old_rule->evaluations));
pf_counter_u64_add_protected(&rule->packets[0],
pf_counter_u64_rollup_protected(&rule->packets[0],
pf_counter_u64_fetch(&old_rule->packets[0]));
pf_counter_u64_add_protected(&rule->packets[1],
pf_counter_u64_rollup_protected(&rule->packets[1],
pf_counter_u64_fetch(&old_rule->packets[1]));
pf_counter_u64_add_protected(&rule->bytes[0],
pf_counter_u64_rollup_protected(&rule->bytes[0],
pf_counter_u64_fetch(&old_rule->bytes[0]));
pf_counter_u64_add_protected(&rule->bytes[1],
pf_counter_u64_rollup_protected(&rule->bytes[1],
pf_counter_u64_fetch(&old_rule->bytes[1]));
pf_counter_u64_critical_exit();
}