Skip to content

Commit

Permalink
pf: atomically increment state ids
Browse files Browse the repository at this point in the history
Rather than using a per-cpu state counter, and adding in the CPU id we
can atomically increment the number.
This has the advantage of removing the assumption that the CPU ID fits
in 8 bits.

Event:		Aberdeen Hackathon 2022
Reviewed by:	mjg
Differential Revision:	https://reviews.freebsd.org/D36915
  • Loading branch information
kprovost committed Oct 8, 2022
1 parent 5b966d7 commit 133935d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sys/net/pfvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ VNET_DECLARE(void *, pf_swi_cookie);
VNET_DECLARE(struct intr_event *, pf_swi_ie);
#define V_pf_swi_ie VNET(pf_swi_ie)

VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
VNET_DECLARE(struct unrhdr64, pf_stateid);
#define V_pf_stateid VNET(pf_stateid)

TAILQ_HEAD(pf_altqqueue, pf_altq);
Expand Down
12 changes: 2 additions & 10 deletions sys/netpfil/pf/pf.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,7 @@ uma_zone_t pf_mtag_z;
VNET_DEFINE(uma_zone_t, pf_state_z);
VNET_DEFINE(uma_zone_t, pf_state_key_z);

VNET_DEFINE(uint64_t, pf_stateid[MAXCPU]);
#define PFID_CPUBITS 8
#define PFID_CPUSHIFT (sizeof(uint64_t) * NBBY - PFID_CPUBITS)
#define PFID_CPUMASK ((uint64_t)((1 << PFID_CPUBITS) - 1) << PFID_CPUSHIFT)
#define PFID_MAXID (~PFID_CPUMASK)
CTASSERT((1 << PFID_CPUBITS) >= MAXCPU);
VNET_DEFINE(struct unrhdr64, pf_stateid);

static void pf_src_tree_remove_state(struct pf_kstate *);
static void pf_init_threshold(struct pf_threshold *, u_int32_t,
Expand Down Expand Up @@ -1416,10 +1411,7 @@ pf_state_insert(struct pfi_kkif *kif, struct pfi_kkif *orig_kif,
s->orig_kif = orig_kif;

if (s->id == 0 && s->creatorid == 0) {
/* XXX: should be atomic, but probability of collision low */
if ((s->id = V_pf_stateid[curcpu]++) == PFID_MAXID)
V_pf_stateid[curcpu] = 1;
s->id |= (uint64_t )curcpu << PFID_CPUSHIFT;
s->id = alloc_unr64(&V_pf_stateid);
s->id = htobe64(s->id);
s->creatorid = V_pf_status.hostid;
}
Expand Down
6 changes: 1 addition & 5 deletions sys/netpfil/pf/pf_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2595,16 +2595,12 @@ pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td
if (V_pf_status.running)
error = EEXIST;
else {
int cpu;

hook_pf();
if (! TAILQ_EMPTY(V_pf_keth->active.rules))
hook_pf_eth();
V_pf_status.running = 1;
V_pf_status.since = time_second;

CPU_FOREACH(cpu)
V_pf_stateid[cpu] = time_second;
new_unrhdr64(&V_pf_stateid, time_second);

DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
}
Expand Down

0 comments on commit 133935d

Please sign in to comment.