Skip to content

nf_tables: the batch and the programs share the elements of the sets that do not change - #827

Merged
tamnd merged 1 commit into
mainfrom
nft-46
Nov 23, 2026
Merged

tamnd merged 1 commit into
mainfrom
nft-46

Conversation

@tamnd

@tamnd tamnd commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Part of #542. This is the second step after #814: a batch and a commit no longer copy the sets that do not change.

What this does

  • nf_tables: the copies of a set share its elements (Arc).
    • NftSet::elems is an Arc<BTreeMap>. Each change of the elements goes through NftSet::elems_mut (Arc::make_mut).
    • The batch begin still clones all the tables, but a clone of a set no longer copies its elements. The first change of a set in a batch copies the elements of that set once.
    • A flush of a set gives the set new empty elements, and the committed copy keeps its own.
    • gc copies the elements only if an element has expired. merge_dynamic copies them only if dynset has changes.
  • nf_tables: the programs of the base chains share the elements of a set that did not change.
    • The data of a set in a program is in two parts. ProgElems has the keys, the ranges, the catch-all element, the stateful expressions, the times of expiry and the timeouts. ProgSet has the parts of one program: the index of each jump chain in the program, the fields, the dynset changes, the size, the count and the timeout.
    • The chain of a verdict in ProgElems is a local index into ProgElems::jumps (the chain handles). Each ProgSet maps these to the chain indexes of its program.
    • NftSet::prog (ProgCache) keeps the ProgElems of the last commit and the elements that it came from. The batch copy and the committed copy of a set share it. At a commit, prog_set uses the cached ProgElems if the elements of the set are the same Arc (Arc::ptr_eq). Else it makes them again and puts them in the cache.
    • The cache holds a reference to the elements. So a change always copies them first, and the cached ProgElems cannot become wrong. elems_mut removes the cache entry before the change, so that the cache does not keep the old elements.
    • A map of objects (NFT_SET_OBJECT) has the state of the objects in its data, so a commit makes its ProgElems again each time, as before.
    • The count of the elements that have not expired comes from a sorted list of the times of expiry, at each commit.

Tests

  • Made on main 4d97cdc, and rebased on main 11da533 with no change in net/. The tests below ran on 4d97cdc.
  • server2, under one gate ticket: cargo fmt --check passes, and cargo test -p rk-net passes (189 tests).

The work of a batch and of a commit

The counters of #814 exist only in the tests. COPIED counts the elements that a copy of a set copies. A new counter, BUILT, counts the elements that the commits put in the programs.

The new test the_programs_share_the_elements_of_a_set_that_did_not_change has:

  • a verdict map m with 2 elements (1 to chain c11, 2 to chain c12), and a set s of 10000 ports;
  • a base chain c1 with vmap @m, and a base chain c2 with lookup @s then jump c12, then vmap @m.
Work Before (main 4d97cdc, from the code) After (this PR, the test asserts it)
Elements copied by the clone of the tables at the batch begin 10002 0
Elements copied by an insert in m 0 2
Elements put in the programs of c1 and c2 by the commit 10004 3
  • Before, the clone of the tables copied each element of each set, and each program got its own copy of each set that it used: m twice and s once.
  • After, the commit makes the elements of m again (3 elements) and shares them between the two programs. The programs share s from the commit before.
  • The test also checks that a program from before the commit still has the old elements, and that each program maps the verdicts of m to its own chain indexes.

Focused tests/shell, release kernels of this branch

The list is the one of #814: the slow sets and maps tests, the tests that use sets with timeouts, rule_management/0011reset_0 and sets/reset_command_0. Each run used --timeout 1800.

Test x86_64 arm64
listing/0015dynamic_0 ok ok
maps/0004interval_map_create_once_0 ok ok
maps/0018map_leak_timeout_0 ok ok
maps/map_with_flags_0 ok ok
maps/typeof_maps_0 ok ok
maps/typeof_maps_add_delete ok ok
maps/typeof_maps_concat_update_0 ok ok
maps/typeof_maps_update_0 ok ok
maps/vmap_timeout ok hang at 1800 s (ok alone, see below)
rule_management/0011reset_0 ok ok
sets/0009comments_timeout_0 ok ok
sets/0013add_delete_many_elements_0 ok ok
sets/0028autoselect_0 ok ok
sets/0028delete_handle_0 ok ok
sets/0031set_timeout_size_0 ok ok
sets/0036add_set_element_expiration_0 ok ok
sets/0038meter_list_0 ok ok
sets/0044interval_overlap_0 ok ok
sets/0044interval_overlap_1 ok ok
sets/0045concat_ipv4_service ok ok
sets/0055tcpflags_0 ok ok
sets/0056dynamic_limit_0 ok ok
sets/0058_setupdate_timeout_0 ok ok
sets/0059set_update_multistmt_0 ok ok
sets/automerge_0 ok hang at 1800 s
sets/dynset_missing ok ok
sets/reset_command_0 fail fail
sets/typeof_sets_0 ok ok
Kernel maps/vmap_timeout on arm64, alone Load average, start and end
main 4d97cdc (k-a64-m46) ok, 1087 s 6.70 9.74 9.51 to 2.19 5.06 6.94
this branch (k-a64-n46) ok, 1607 s 3.72 3.26 3.15 to 6.81 10.01 9.59, with a kernel build on the host in the middle
this branch (k-a64-n46), again ok, 1010 s 2.12 4.81 6.81 to 2.15 3.64 4.92

The time on arm64 depends on the load of the host: in these kernels the guest clock runs slow (#813, fixed on main by #823 after these runs), so the elements expire late and the maps get large. The run at a low load is a little faster than main. The hang in the run with 2 VMs comes from the load, not from this change.

The wall time under TCG goes mostly to nft and bash in the VM, so this PR claims no large speed-up of these tests. sets/automerge_0 writes to its set in each batch, and no chain uses the set.

The full tests/shell did not run.

The full gate did not run.

@tamnd tamnd added kind/perf rukernel is slower than Linux, or a change makes it faster. area/net Networking: sockets, TCP, netlink and netfilter. labels Nov 23, 2026
@tamnd
tamnd merged commit 79dbc66 into main Nov 23, 2026
6 of 7 checks passed
@tamnd
tamnd deleted the nft-46 branch November 23, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/net Networking: sockets, TCP, netlink and netfilter. kind/perf rukernel is slower than Linux, or a change makes it faster.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant