netfilter: nfnetlink_queue: optimize verdict lookup with hash table
Subsystems (3)
PATCH 1/1 netfilter: nfnetlink_queue: optimize verdict lookup with hash table
The current implementation uses a linear list to find queued packets by ID when processing verdicts from userspace. With large queue depths and out-of-order verdicting, this O(n) lookup becomes a significant bottleneck, causing userspace verdict processing to dominate CPU time.
Replace the linear search with a hash table for O(1) average-case packet lookup by ID. The hash table size is configurable via the new NFQA_CFG_HASH_SIZE netlink attribute (default 1024 buckets, matching NFQNL_QMAX_DEFAULT; max 131072). The size is normalized to a power of two to enable efficient bitwise masking instead of modulo operations. Unpatched kernels silently ignore the new attribute, maintaining backward compatibility.
The existing list data structure is retained for operations requiring linear iteration (e.g. flush, device down events). Hot fields (queue_hash_mask, queue_hash pointer) are placed in the same cache line as the spinlock and packet counters for optimal memory access patterns.
--- include/net/netfilter/nf_queue.h | 1 + .../uapi/linux/netfilter/nfnetlink_queue.h | 1 + net/netfilter/nfnetlink_queue.c | 137 +++++++++++++++++- 3 files changed, 131 insertions(+), 8 deletions(-)
include/net/netfilter/nf_queue.h +1 -0
@@ -11,6 +11,7 @@/* Each queued (to userspace) skbuff has one of these. */ struct nf_queue_entry {struct list_head list; +struct hlist_node hash_node;struct sk_buff *skb;unsigned int id;unsigned int hook_index;/* index in hook_entries->hook[] */
include/uapi/linux/netfilter/nfnetlink_queue.h +1 -0
@@ -107,6 +107,7 @@ enum nfqnl_attr_config {NFQA_CFG_QUEUE_MAXLEN ,/* __u32 */ NFQA_CFG_MASK ,/* identify which flags to change */ NFQA_CFG_FLAGS ,/* value of these flags (__u32) */ +NFQA_CFG_HASH_SIZE ,/* __u32 hash table size (rounded to power of 2) */ __NFQA_CFG_MAX };#define NFQA_CFG_MAX (__NFQA_CFG_MAX-1)
net/netfilter/nfnetlink_queue.c +129 -8
255 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Florian Westphal
maintainerQuoted 10 lines · Scott Mitchell
static inline
u_int8_tinstance_hashfn(u_int16_t queue_num){net/netfilter/nfnetlink_queue.c +3
-0return ((queue_num >>8 ) ^ queue_num) %INSTANCE_BUCKETS ;@@ -114,13 +153,63 @@ instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)return NULL ; } +static int +nfqnl_hash_resize (struct nfqnl_instance *inst,u32 hash_size) +{
net/netfilter/nfnetlink_queue.c instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) · line 168
Quoted 10 lines
net/netfilter/nfnetlink_queue.c +10
-0+struct hlist_head *new_hash, *old_hash; +struct nf_queue_entry *entry; +unsigned int h, hash_mask; + +/* lock scope includes kcalloc/kfree to bound memory if concurrent resizes. +* lock scope could be reduced to exclude the kcalloc/kfree at the cost +* of increased code complexity (re-check of hash_size) and relaxed memory +* bounds (concurrent resize may each do allocations). since resize is +* expected to be rare, the broader lock scope is simpler and preferred. +*/
I'm all for simplicity. but I don't see how concurrent resizes are possible. NFQNL_MSG_CONFIG runs under nfnetlink subsystem mutex.
Or did I miss something?
net/netfilter/nfnetlink_queue.c instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) · line 175
Quoted 1 line
net/netfilter/nfnetlink_queue.c +1
-0+ new_hash =kcalloc (hash_size,sizeof (*new_hash),GFP_ATOMIC );
Since the hash table could be large I would prefer if this could be GFP_KERNEL_ACCOUNT + kvcalloc to permit vmalloc fallback.
net/netfilter/nfnetlink_queue.c static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, · line 1615
Quoted 3 lines
net/netfilter/nfnetlink_queue.c +3
-0+if (nfqa[NFQA_CFG_HASH_SIZE ]) { + hash_size =ntohl (nla_get_be32 (nfqa[NFQA_CFG_HASH_SIZE ])); + }
Nit, no { } here.
Quoted 26 lines · Florian Westphal
Quoted 10 lines · Scott Mitchell
static inline
u_int8_tinstance_hashfn(u_int16_t queue_num){ return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;net/netfilter/nfnetlink_queue.c +3
-0@@ -114,13 +153,63 @@ instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)return NULL ; } +static int +nfqnl_hash_resize (struct nfqnl_instance *inst,u32 hash_size) +{net/netfilter/nfnetlink_queue.c instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) · line 168
Quoted 10 lines
net/netfilter/nfnetlink_queue.c +10
-0+struct hlist_head *new_hash, *old_hash; +struct nf_queue_entry *entry; +unsigned int h, hash_mask; + +/* lock scope includes kcalloc/kfree to bound memory if concurrent resizes. +* lock scope could be reduced to exclude the kcalloc/kfree at the cost +* of increased code complexity (re-check of hash_size) and relaxed memory +* bounds (concurrent resize may each do allocations). since resize is +* expected to be rare, the broader lock scope is simpler and preferred. +*/ I'm all for simplicity. but I don't see how concurrent resizes are possible.
NFQNL_MSG_CONFIGruns under nfnetlink subsystem mutex.
Quoted 1 line
Or did I miss something?
This makes sense, and I will fix this.
Quoted 5 lines
net/netfilter/nfnetlink_queue.c instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) · line 175
Quoted 2 lines
net/netfilter/nfnetlink_queue.c +1
-0+ new_hash =kcalloc (hash_size,sizeof (*new_hash),GFP_ATOMIC );Since the hash table could be large I would prefer if this could be
GFP_KERNEL_ACCOUNT+ kvcalloc to permit vmalloc fallback.
Good feedback, done.
Quoted 6 lines
net/netfilter/nfnetlink_queue.c static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, · line 1615
Quoted 4 lines
net/netfilter/nfnetlink_queue.c +3
-0+if (nfqa[NFQA_CFG_HASH_SIZE ]) { + hash_size =ntohl (nla_get_be32 (nfqa[NFQA_CFG_HASH_SIZE ])); + }Nit, no { } here.
Fixed.
Thanks for the review! I'll send v2 shortly.