Netfilter hooks on SMP?
Hi all,
I'm working on a kernel module to filter packets for a specific application (to build a firewall for this specific application). I can't use iptables for performance issues (the module must filter hundred of thousands small packets per second, and rules are added/removed relatively fast to a large ruleset, which is too slow with iptables).
I'm filtering packets using the PREROUTING netfilter hooks, and I can't figure how they are executed on SMP machine.
This hook should be executed in a softirq, and so could be run by multiple CPUs at the same time.
This seems to be the case when 2 NICs are used (tested on a dual Xeon, 85%"load on each CPU).
But when there is only one NIC, only one CPU is used.
Is there a way to make packets coming from a single NIC handled by multiple CPUs?
Thanks in advance,
strawks
Quoted 8 lines · strawks
This hook should be executed in a softirq, and so could be run by multiple CPUs at the same time.
This seems to be the case when 2 NICs are used (tested on a dual Xeon, 85%"load on each CPU).
But when there is only one NIC, only one CPU is used.
Correct, each network card has an assigned interrupt, and when that fires, the CPU assigned for this interrupt will process the NIC buffer (which may have multiple packets).
Quoted 2 lines
Is there a way to make packets coming from a single NIC handled by multiple CPUs?
Possibly. I think the netif_rx() function in net/core/dev.c is the sweet spot:
queue = &__get_cpu_var(softnet_data);
include/linux/netdevice.h tells us about softnet_data:
/* * Incoming packets are placed on per-cpu queues so that * no locking is needed. */
which means if you distribute packets across CPUs (by using something else than __get_cpu_var), you need additional locking or rcu.
Quoted 17 lines · Jan Engelhardt
Quoted 1 line · strawks
Is there a way to make packets coming from a single NIC handled by
multiple
Quoted 1 line
CPUs?
Possibly. I think the
netif_rx()function innet/core/dev.cis the sweet spot:queue = &__get_cpu_var(softnet_data);
include/linux/netdevice.htells us aboutsoftnet_data:(quoted diff) +0
-0/* * Incoming packets are placed on per-cpu queues so that * no locking is needed. */
Quoted 3 lines
which means if you distribute packets across CPUs (by using something else than
__get_cpu_var), you need additional locking or rcu.
Thanks for your reply!
netif_rx is called by the NIC driver, by the IRQ handler (maybe I'm wrong since NAPI may work differently), to queue packets which will be handled by the softirq.
So when the driver exits the IRQ handler for its device, IRQ are reenabled. If a packet comes before the softirq has finished with the previous packet(s), an IRQ is raised again. It appears that this IRQ will be handled by the same CPU. But is it possible that the second IRQ should be handled by another CPU? I don't really know how IRQs are handled on SMP (nor I know how irq-balance works)
Signature · 2 lines
Regards, strawks
Quoted 7 lines · strawks
So when the driver exits the IRQ handler for its device, IRQ are reenabled. If a packet comes before the softirq has finished with the previous packet(s), an IRQ is raised again.
It appears that this IRQ will be handled by the same CPU. But is it possible that the second IRQ should be handled by another CPU?
Yes, by balancing the IRQ elsewhere, in short, setting its CPU affinity mask accordingly.
But if the processing of packets takes most of the time, then you should not worry about balancing NIC IRQs, but enqueueing the packets onto the right softirq thread.
Quoted 2 lines
I don't really know how IRQs are handled on SMP (nor I know how irq-balance works)
IRQs are enabled for one CPU only in the general case, and balancing it should be straightforward.
I have no idea what happens if the affinity mask allows an IRQ for more than one CPU, but you should not worry about that, because
(a) you probably do not want a deliver-to-a-random-CPU behavior nor
(b) deliver-to-all
(since I doubt the hardware does (c) round-robin automatically)