libvirt/src/nwfilter
Laine Stump ce5aebeacd nwfilter: increase pcap buffer size to be compatible with TPACKET_V3
When an nwfilter rule sets the parameter CTRL_IP_LEARNING to "dhcp",
this turns on the "dhcpsnoop" thread, which uses libpcap to monitor
traffic on the domain's tap device and extract the IP address from the
DHCP response.

If libpcap on the host is built with HAVE_TPACKET3 defined (to enable
support for TPACKET_V3), the dhcpsnoop code's initialization of the
libpcap socket would fail with the following error:

  virNWFilterSnoopDHCPOpen:1134 : internal error: pcap_setfilter: can't remove kernel filter: Bad file descriptor

It turns out that this was because TPACKET_V3 requires a larger buffer
size than libvirt was setting (we were setting it to 128k). Changing
the buffer size to 256k eliminates the error, and the dhcpsnoop thread
once again works properly.

A fuller explanation of why TPACKET_V3 requires such a large buffer,
for future git spelunkers:

libpcap calls setsockopt(... SOL_PACKET, PACKET_RX_RING...) to setup a
ring buffer for receiving packets; two of the attributes sent to this
API are called tp_frame_size, and tp_frame_nr. If libpcap was built
with HAVE_TPACKET3 defined, tp_trame_size is set to MAXIMUM_SNAPLEN
(defined in libpcap sources as 262144) and tp_frame_nr is set to:

 [the buffer size we set, i.e. PCAP_BUFFERSIZE i.e. 262144] / tp_frame_size.

So if PCAP_BUFFERSIZE < MAXIMUM_SNAPLEN, then tp_frame_nr (the number
of frames in the ring buffer) is 0, which is nonsensical. This same
value is later used as a multiplier to determine the size for a call
to malloc() (which would also fail).

(NB: if HAVE_TPACKET3 is *not* defined, then tp_frame_size is set to
the snaplen set by the user (in our case 576) plus a small amount to
account for ethernet headers, so 256k is far more than adequate)

Since the TPACKET_V3 code in libpcap actually reads multiple packets
into each frame, it's not a problem to have only a single frame
(especially when we are monitoring such infrequent traffic), so it's
okay to set this relatively small buffer size (in comparison to the
default, which is 2MB), which is important since every guest using
dhcp snooping in a nwfilter rule will hold 2 of these buffers for the
entire life of the guest.

Thanks to Christian Ehrhardt for discovering that buffer size was the
problem (this was not at all obvious from the error that was logged!)

Resolves: https://bugzilla.redhat.com/1547237
Fixes: https://bugs.launchpad.net/libvirt/+bug/1758037

Signed-off-by: Laine Stump <laine@laine.org>
Reviewed-by: Christian Ehrhardt <christian.ehrhardt@canonical.com> (V1)
Reviewed-by: John Ferlan <jferlan@redhat.com>
Tested-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
2018-04-27 17:38:53 -04:00
..
Makefile.inc.am make: split nwfilter driver build rules into nwfilter/Makefile.inc.am 2018-03-05 17:09:08 +00:00
nwfilter_dhcpsnoop.c nwfilter: increase pcap buffer size to be compatible with TPACKET_V3 2018-04-27 17:38:53 -04:00
nwfilter_dhcpsnoop.h Remove virDomainNetType parameter from nwfilter drivers 2014-04-25 15:44:09 +01:00
nwfilter_driver.c driver: ensure NULL URI isn't passed to drivers with whitelisted URIs 2018-04-12 16:52:02 +01:00
nwfilter_driver.h maint: fix up copyright notice inconsistencies 2012-09-20 16:30:55 -06:00
nwfilter_ebiptables_driver.c nwfilter: handle missing switch enum cases 2018-02-21 16:58:25 +00:00
nwfilter_ebiptables_driver.h Convert nwfilter ebiptablesApplyNewRules to virFirewall 2014-04-25 15:44:10 +01:00
nwfilter_gentech_driver.c nwfilter: remove pointless assignment 2018-03-08 16:45:54 +01:00
nwfilter_gentech_driver.h conf: Introduce virnwfilterobj 2017-03-07 13:27:25 -05:00
nwfilter_learnipaddr.c nwfilter: handle missing switch enum cases 2018-02-21 16:58:25 +00:00
nwfilter_learnipaddr.h Force usage of virThreadCreate 2015-03-25 10:00:53 +01:00
nwfilter_tech_driver.h conf: Introduce virnwfilterobj 2017-03-07 13:27:25 -05:00