Skip to content

dpdk: add initial unittests for DPDK codebase v15.1 - #16751

Open
lukashino wants to merge 29 commits into
OISF:mainfrom
lukashino:feat/6927-add-unit-tests-v15.1
Open

lukashino wants to merge 29 commits into
OISF:mainfrom
lukashino:feat/6927-add-unit-tests-v15.1

Conversation

@lukashino

Copy link
Copy Markdown
Contributor

Follow-up of #16748 and of the mash-up in #16649

Link to ticket: https://redmine.openinfosecfoundation.org/issues/6927

The PR is adding unit tests tailored to verify CPU threading logic and the automatic calculation of the mempool size and the mempool cache of the interface. The tests simulate the CPUs of the system, so they run on any machine.

This is the unit-test part of #16649 on its own, but with minor modifactions (one commit splitted into multiple). It also carries the DPDK changes that do not need the backlog:

User-visible changes, all in the upgrade guide: DPDK 21.11 is the minimum; mempool-size: auto reserves one RX burst per queue and a manual mempool-size is validated against it; packets of one RX burst share one timestamp; with set-cpu-affinity enabled, an invalid cpu-affinity configuration stops Suricata at startup.

The RX backlog (https://redmine.openinfosecfoundation.org/issues/9111) is not part of this PR, it would follow on top of it.

Describe changes:
v15.1:

  • address uint scope failure

v15 (changes against v14.1; these commits were already in #16649, unchanged apart from the rebase):

  • new unit tests for the mempool size calculation (DPDKRunmodeSetMempoolSize01-08, DPDKRunmodeSetMempoolCacheSize05): auto and manual size, the split among the queues, bond ports, rejected sizes, the cache size derived from the result
  • qa/live/dpdk/dpdk-testsuite.sh (+ its checklog script, the bond yaml and its CI steps) removed (Jason); the sizing is covered by the unit tests, the SV PR removes the 18 dpdk-config-* live tests that only checked it
  • use DPDK 21.11 as minimal supported version (upgrade guide)
  • update schema with DPDK capture statistics
  • timestamp per burst and not per packet (upgrade guide)
  • RX burst size added to the mempool calculation (auto size and the validation of a manual size); on a bond only the descriptors are multiplied by the member count (upgrade guide)
  • fix the cleanup after a packet processing failure
  • minor updates to the DPDK live test (qa/live/dpdk.sh and its IDS yaml)
  • fix the shutdown barrier race (a descheduled worker could spin forever)
  • update the capture counters on idle polls too
  • CPU IDs of the affinity config are checked against the CPUs of the system and of the process CPU affinity mask instead of the online CPU count, and CPU selection covers these IDs too
  • the stricter affinity validation is documented in suricata-yaml.rst and the upgrade guide, including the xdp-cpu-redirect list
  • examples with fixed CPU numbers in the shipped suricata.yaml are commented out, as the stricter validation would stop Suricata on smaller systems (review bot remark)
  • a unit test can register a cleanup function that the runner calls after the test, also after a failed one - used to reset the simulated CPUs and the affinity state (Copilot remark); it is runner-level, unlike the callback array dropped in v12.7
  • new tests ThreadingAffinityTest30-32: offline CPUs, a container CPU mask counted as configured CPUs (musl), the first CPU past the mask
  • "affinity: rename public APIs and expose test reset function" split into 4 commits (loop bound fix, bool return, rename, reset function)
  • commit message typo fixed
  • rebased

SV_BRANCH=OISF/suricata-verify#3582

Lukas Sismis added 29 commits November 26, 2026 16:30
Define the DPDK packet and error counters emitted by the capture module
so that EVE statistics validate against the schema.
The mempool sizing cases will be covered by the DPDK unit tests in
src/runmode-dpdk.c, and the cases that need a DPDK port by the
suricata-verify live tests (dpdk-config-*, dpdk-ids-*). Keeping the
script as well means every mempool sizing change has to update two
test suites.
When Suricata exits during startup it never writes a stats event, so jq
returns an empty string and the test aborted the comparison with
"unary operator expected".
Quote the value and compare it against the expected outcome, so an empty
result fails the same way a false one does.
The configured mempool of 511 mbufs is split between the worker queues,
which limits how many workers can start. As the test runs with
"threads: auto", it refuses to start on a host with more cores than the
fixed pool can serve.
Raise the minimum supported DPDK version to 21.11 and remove compatibility
paths for older releases, including legacy bond library detection and RSS
configuration. Update the documented version requirement.
Record the acquisition time immediately after each successful receive and
pass it to packet initialization. All packets in a burst retain that time
instead of including the processing delay of preceding packets.
The processing pipeline already releases the failed Packet. Avoid releasing
it twice, and free every remaining received mbuf using the array's exclusive
end index. Return the packet acquisition failure code consistently.
Share the RX burst size with the configuration code and reserve one burst
per worker in addition to the RX and TX descriptors for both automatic
sizing and manual minimum validation. Consolidate the two requirement
calculations. On a bond port only the descriptors are per member port, so
the member count multiplies them alone and the per-worker reserve is added
once. The sizing takes the member count as an argument, the bonding helper
only reports it, and a requirement that does not fit is a configuration
error like any other.
Workers synchronize at shutdown by incrementing a shared check-in
counter and spinning until it reaches the worker count. The first
worker then reset the counter after a 20 us delay. A worker that is
descheduled between its own increment and its check of the full count
never observes it after the reset, so it spins forever and Suricata
never exits.

As the counter is never reused afterwards, we can remove the reset
and mitigate the potential race.
An empty poll in polling mode skipped the rest of the loop iteration,
so an idle worker 0 never dumped its capture counters and never served a
sync requested by the stats thread. The port-level drop counters, which
only the first worker reports, stayed stale while that worker saw no
traffic.

Run the periodic counter dump and the stats sync on every iteration.
The threads field is uint16_t, so the <= 0 comparison is misleading
as the value can never be negative. Change to == 0.
UtilAffinityGetAffinedCPUNum and UtilAffinityCpusOverlap started at the
number of online CPUs, one past the highest CPU id, and so tested a CPU
that does not exist.
The function answers a yes/no question, so return a bool instead of a
uint16_t. Also remove a temporary affinity type that was initialized
but never used.
Rename affinity public API functions to use a consistent naming
convention (e.g. GetAffinityTypeForNameAndIface to
AffinityTypeGetByIfaceOrCpuset, UtilAffinityGetAffinedCPUNum to
AffinityGetAffinedCPUNum, AffinitySetupLoadFromConfig to
AffinityLoadFromConfig, etc.).
Make ResetAffinityForTest public as AffinityReset, so unit tests of
other modules can reset the affinity state.

Ticket: 6927
To better control the values within the variables and to prepare
for the follow-up unit tests, the variable was moved into global
scope and should be accessed only by functions. This allows
reinitialization of the variable value - needed for unit tests.

Ticket: 6927
Needed by unit tests to properly reset the device list.

Ticket: 6927
A failed test returns before its own cleanup code, so the global state
it changed stays behind for the following tests.

Add UtSetTestCleanup(). The test runner calls the registered function
after the test, also after a failed one, and then forgets it.

Ticket: 6927
Some unit tests depend on the CPUs of the machine they run on, e.g. an
affinity test with the CPU range "0-3" needs at least 4 of them.

Add UtilCpuSetNumProcessorsForTests() to set the configured and the
online CPU count that util-cpu reports in unit test builds.

Ticket: 6927
AffinityReset() left all CPU sets with low priority and balanced mode,
and kept the flag of the "no CPU" warning. At startup the sets have
medium priority, and the receive and worker sets are exclusive.

Reset to the startup values, so a test starts from the same state
whether it runs first, after other tests or alone.

Ticket: 6927
The worker-cpu-set example had active priority lists up to CPU 3 and
an interface-specific set up to CPU 9. With set-cpu-affinity enabled,
they refer to CPUs that smaller systems do not have.

The following commits turn such a configuration into a startup error,
so keep these lines as commented-out examples. Without the priority
lists, all worker threads get the default (medium) priority.

Ticket: 6927
The affinity unit tests used the CPUs of the machine they ran on, so
e.g. a test with the range "0-3" needed at least 4 of them. Run the
tests on 8 simulated CPUs.

Move the setup of the tests to AffinityTestInit(). The cleanup is
registered with the test runner and resets the affinity state, the
config and the simulated CPUs, so a failed test leaves nothing behind
for the next ones.

ThreadingAffinityTest01 only checked that the worker CPU set was not
empty, check the number of its CPUs.

Ticket: 6927
On an invalid threading.cpu-affinity section, AffinityLoadFromConfig()
logged the problem and went on with the next setting or CPU set, so
Suricata started with other CPU sets than the configured ones.

Return a negative errno instead: -EINVAL for a malformed value or an
unknown *-cpu-set name, -ERANGE for a CPU range that ends past the CPUs
of the system. With set-cpu-affinity enabled, Suricata now stops at
startup on these errors.

The unit tests that load a config check the returned value.

Ticket: 6927
Only the end of a CPU range was checked against the CPUs of the
system, a single number like [ 9 ] on a 4-CPU system was accepted.
Check every number and return -ERANGE when the system does not have
that CPU, so Suricata stops at startup as on the other errors of the
affinity configuration. As for the end of a range, the limit here is
still the online CPU count.

The AF_PACKET xdp-cpu-redirect list uses the same parser. A CPU the
system does not have was skipped there with an error, it now disables
the XDP CPU redirect with a warning.

Ticket: 6927
A CPU number was valid when it was below the online CPU count, and
CPU selection and counting walked the CPUs up to that count. On a
4-CPU system with CPU 1 offline the count is 3, so CPU 3 could not be
used.

Use the highest CPU ID of the system instead: of the configured CPUs
and of the CPUs of the process CPU affinity mask. The mask matters
with musl, which counts only its CPUs as configured, e.g. 4 in a
container limited to CPUs 8-11.

Ticket: 6927
Reduce affinity unit test CPU indices to a maximum of 4 cores and
lower the simulated CPU count of the tests accordingly, so that the
tests describe the smallest layout they need. Also fix assertions in
ThreadingAffinityTest12 that were incorrectly checking hiprio_cpu
instead of cpu_set.

Ticket: 6927
Add unit tests for mempool cache size auto-calculation and for
automatic thread distribution across interfaces, including
management thread overlap scenarios.

Ticket: 6927
The DPDK unit tests run in CI only in builds without ASAN. Enable
DPDK in the Fedora job that runs the unit tests under ASAN, so memory
errors in the DPDK code paths the tests cover are caught too.

Ticket: 6927
Cover the automatic and the manual mempool size of an interface: the
descriptors and the RX burst reserved for each queue, the split of a manual
size among the queues, the multiplication of the descriptors on a bond port,
the rejected sizes and the cache size derived from the result.

The tests take the place of the suricata-verify live tests dpdk-config-*
that only checked these calculations.

Ticket: 6927
@lukashino
lukashino requested a review from jasonish as a code owner November 26, 2026 23:26
Copilot AI balanced review requested due to automatic review settings November 26, 2026 23:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@suricata-review-bot suricata-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated review.

Verdict: approve

No high- or medium-severity findings.

Redmine ticket not yet In Review: #9111 (In Progress). Please set it to In Review.

Automated at commit e600cbdfbe2d using Claude Code with claude-opus-5-5/xhigh. Run: 3d2cbc9c013e790f.

@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.65743% with 49 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.94%. Comparing base (ca5de3e) to head (e600cbd).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16751      +/-   ##
==========================================
- Coverage   83.97%   83.94%   -0.03%     
==========================================
  Files        1008     1009       +1     
  Lines      286489   286754     +265     
==========================================
+ Hits       240565   240712     +147     
- Misses      45924    46042     +118     
Flag Coverage Δ
fuzzcorpus 61.58% <0.86%> (-0.05%) ⬇️
livemode 25.92% <55.44%> (-0.05%) ⬇️
netns 26.26% <3.44%> (-0.04%) ⬇️
pcap 46.15% <2.58%> (-0.01%) ⬇️
suricata-verify 69.00% <39.65%> (-0.06%) ⬇️
unittests 59.25% <87.13%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@suricata-qa

Copy link
Copy Markdown

WARNING:

field baseline test %
SURI_TLPW2_single_stats_chk
.decoder.event.erspan.unsupported_encapsulation - 0 -
SURI_TLPW2_autofp_stats_chk
.decoder.event.erspan.unsupported_encapsulation - 0 -
SURI_TLPW1_stats_chk
.decoder.event.erspan.unsupported_encapsulation - 0 -
SURI_TLPR1_stats_chk
.app_layer.flow.ftp_data 656 676 103.05%
.decoder.event.erspan.unsupported_encapsulation - 0 -
IPS_AFP_stats_chk
.decoder.event.erspan.unsupported_encapsulation - 0 -
TREX_GENERIC_stats_chk
.decoder.event.erspan.unsupported_encapsulation - 0 -

Pipeline = 36154

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants