freebsd-dev/sys/netinet/ipfw
Luigi Rizzo de240d1013 merge code from ipfw3-head to reduce contention on the ipfw lock
and remove all O(N) sequences from kernel critical sections in ipfw.

In detail:

 1. introduce a IPFW_UH_LOCK to arbitrate requests from
     the upper half of the kernel. Some things, such as 'ipfw show',
     can be done holding this lock in read mode, whereas insert and
     delete require IPFW_UH_WLOCK.

  2. introduce a mapping structure to keep rules together. This replaces
     the 'next' chain currently used in ipfw rules. At the moment
     the map is a simple array (sorted by rule number and then rule_id),
     so we can find a rule quickly instead of having to scan the list.
     This reduces many expensive lookups from O(N) to O(log N).

  3. when an expensive operation (such as insert or delete) is done
     by userland, we grab IPFW_UH_WLOCK, create a new copy of the map
     without blocking the bottom half of the kernel, then acquire
     IPFW_WLOCK and quickly update pointers to the map and related info.
     After dropping IPFW_LOCK we can then continue the cleanup protected
     by IPFW_UH_LOCK. So userland still costs O(N) but the kernel side
     is only blocked for O(1).

  4. do not pass pointers to rules through dummynet, netgraph, divert etc,
     but rather pass a <slot, chain_id, rulenum, rule_id> tuple.
     We validate the slot index (in the array of #2) with chain_id,
     and if successful do a O(1) dereference; otherwise, we can find
     the rule in O(log N) through <rulenum, rule_id>

All the above does not change the userland/kernel ABI, though there
are some disgusting casts between pointers and uint32_t

Operation costs now are as follows:

  Function				Old	Now	  Planned
-------------------------------------------------------------------
  + skipto X, non cached		O(N)	O(log N)
  + skipto X, cached			O(1)	O(1)
XXX dynamic rule lookup			O(1)	O(log N)  O(1)
  + skipto tablearg			O(N)	O(1)
  + reinject, non cached		O(N)	O(log N)
  + reinject, cached			O(1)	O(1)
  + kernel blocked during setsockopt()	O(N)	O(1)
-------------------------------------------------------------------

The only (very small) regression is on dynamic rule lookup and this will
be fixed in a day or two, without changing the userland/kernel ABI

Supported by: Valeria Paoli
MFC after:	1 month
2009-12-22 19:01:47 +00:00
..
ip_dummynet.c merge code from ipfw3-head to reduce contention on the ipfw lock 2009-12-22 19:01:47 +00:00
ip_fw2.c merge code from ipfw3-head to reduce contention on the ipfw lock 2009-12-22 19:01:47 +00:00
ip_fw_dynamic.c bring back a couple of #include that are supplied by nesting, 2009-12-16 13:00:37 +00:00
ip_fw_log.c merge code from ipfw3-head to reduce contention on the ipfw lock 2009-12-22 19:01:47 +00:00
ip_fw_nat.c Various cosmetic cleanup of the files: 2009-12-16 10:48:40 +00:00
ip_fw_pfil.c merge code from ipfw3-head to reduce contention on the ipfw lock 2009-12-22 19:01:47 +00:00
ip_fw_private.h merge code from ipfw3-head to reduce contention on the ipfw lock 2009-12-22 19:01:47 +00:00
ip_fw_sockopt.c merge code from ipfw3-head to reduce contention on the ipfw lock 2009-12-22 19:01:47 +00:00
ip_fw_table.c some mostly cosmetic changes in preparation for upcoming work: 2009-12-22 13:53:34 +00:00