Re: adding fields to struct mbuf

From: Vadim Goncharov <vadimnuclight_at_gmail.com>
Date: Thu, 31 Jul 2025 12:55:50 UTC
On Thu, 31 Jul 2025 13:03:22 +0200
Kajetan Staszkiewicz <vegeta@tuxpowered.net> wrote:

> Hello group,
> 
> I'm researching loop prevention in pfil. There are cases where packets
> are reinjected into the network stack and would be handled by the same
> hooks again, i.e. pf + dummynet where currently pf itself handles loop
> prevention on its own. My current experiment's approach to making loop
> prevention a general, non-pf-specific thing is to create a new mtag with
> pointer to the last hook and update it in pfil.c/pfil_mbuf_common().
> That works good so far, but it means memory allocation when pfil hooks
> are involved. I'm unsure what the impact on performance would be.
> Another approach would be to extend struct mbuf, or probably rather
> struct m_pkthdr, to contain the aforementioned pointer. But is changing
> that struct something that can be easily done and approved and merged?

First, you certainly don't need it in every mbuf - just first in chain with
struct pkthdr (where mtags also start).

Second.
The "last hook ptr" does not look like general solution for all cases and
occupies 8 bytes. What about idea from network itself - TTL ? It occupies less
bytes, the main problem is to decide where to decrement (e.g. each netgraph
hook, etc.)

Third.
What about redoing mtag allocator so that it reuses m_pktdat[] when M_EXT is
set? This could optimize performance for many tags, not just yours.

> For those curious the experiment will be uploaded to Phabricator soon-ish.

-- 
WBR, @nuclight