Skip to content

feat(userspace/libscap): Add a raw block engine - #3038

Open
geraldcombs wants to merge 11 commits into
falcosecurity:masterfrom
geraldcombs:raw-block-engine
Open

feat(userspace/libscap): Add a raw block engine#3038
geraldcombs wants to merge 11 commits into
falcosecurity:masterfrom
geraldcombs:raw-block-engine

Conversation

@geraldcombs

Copy link
Copy Markdown
Contributor

Add an engine that lets you process individual scap / pcapng blocks in memory. This lets another application that might have a scap file open make use of the analysis features in libsinsp.

Assisted-by: Claude:claude-opus-4-6 claude-opus-4-8

What type of PR is this?

Uncomment one (or more) /kind <> lines:

/kind bug

/kind cleanup

/kind design

/kind documentation

/kind failing-test

/kind test

/kind feature

/kind sync

Any specific area of the project related to this PR?

Uncomment one (or more) /area <> lines:

/area API-version

/area build

/area automation

/area drivers

/area driver-kmod

/area driver-modern-bpf

/area libscap-engine-kmod

/area libscap-engine-modern-bpf

/area libscap-engine-nodriver

/area libscap-engine-noop

/area libscap-engine-source-plugin

/area libscap-engine-savefile

/area libscap

/area libpman

/area libsinsp

/area tests

/area proposals

Does this PR require a change in the driver versions?

/version driver-API-version-major

/version driver-API-version-minor

/version driver-API-version-patch

/version driver-SCHEMA-version-major

/version driver-SCHEMA-version-minor

/version driver-SCHEMA-version-patch

What this PR does / why we need it:

This adds a "raw_block" engine, which lets you process events from scap/pcapng blocks in memory. This would let Stratoshark process scap events without having to open capture files concurrently within Stratoshark and libs: https://gitlab.com/wireshark/wireshark/-/work_items/21014.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@geraldcombs

Copy link
Copy Markdown
Contributor Author

The corresponding Stratoshark merge request can be found at https://gitlab.com/wireshark/wireshark/-/merge_requests/25550.

@github-actions

github-actions Bot commented Jun 26, 2026

Copy link
Copy Markdown

Perf diff from master - unit tests

    18.86%     +6.24%  [.] std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_add_ref_lock_nothrow()
    10.71%     -4.01%  [.] sinsp_thread_manager::create_thread_dependencies(std::shared_ptr<sinsp_threadinfo> const&)
    19.79%     -3.26%  [.] sinsp_threadinfo::get_main_thread()
    14.24%     -2.53%  [.] std::__shared_ptr<sinsp_threadinfo, (__gnu_cxx::_Lock_policy)2>::__shared_ptr(std::__weak_ptr<sinsp_threadinfo, (__gnu_cxx::_Lock_policy)2> const&, std::nothrow_t)
     4.53%     +2.44%  [.] thread_group_info::get_first_thread() const
     3.63%     +1.38%  [.] sinsp_threadinfo::get_fd_table() const
     4.58%     -1.13%  [.] std::__shared_count<(__gnu_cxx::_Lock_policy)2>::_M_get_use_count() const
     9.24%     +0.61%  [.] std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release()
     7.22%     +0.31%  [.] std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count(std::__weak_count<(__gnu_cxx::_Lock_policy)2> const&, std::nothrow_t)
     0.50%     -0.16%  [.] scap_event_encode_params_v

Heap diff from master - unit tests

peak heap memory consumption: -1.60K
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Heap diff from master - scap file

peak heap memory consumption: 0B
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Benchmarks diff from master

Comparing gbench_data.json to /root/actions-runner/_work/libs/libs/build/gbench_data.json
Benchmark                                                                             Time             CPU      Time Old      Time New       CPU Old       CPU New
------------------------------------------------------------------------------------------------------------------------------------------------------------------
BM_sinsp_split_mean                                                                -0.0065         -0.0065           288           286           288           286
BM_sinsp_split_median                                                              -0.0017         -0.0017           287           287           287           286
BM_sinsp_split_stddev                                                              -0.6981         -0.6948             5             1             5             1
BM_sinsp_split_cv                                                                  -0.6962         -0.6928             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_mean                                      +0.0027         +0.0030            67            68            67            68
BM_sinsp_concatenate_paths_relative_path_median                                    +0.0026         +0.0031            67            68            67            68
BM_sinsp_concatenate_paths_relative_path_stddev                                    +0.5059         +0.4841             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_cv                                        +0.5018         +0.4797             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_mean                                         +0.2208         +0.2211            45            55            45            55
BM_sinsp_concatenate_paths_empty_path_median                                       +0.2330         +0.2330            44            55            44            55
BM_sinsp_concatenate_paths_empty_path_stddev                                       -0.9506         -0.9486             1             0             1             0
BM_sinsp_concatenate_paths_empty_path_cv                                           -0.9595         -0.9579             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_mean                                      -0.0431         -0.0431            66            63            66            63
BM_sinsp_concatenate_paths_absolute_path_median                                    -0.0446         -0.0445            66            63            66            63
BM_sinsp_concatenate_paths_absolute_path_stddev                                    +0.1256         +0.1195             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_cv                                        +0.1762         +0.1699             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_ascii_short_mean                                  +0.0032         +0.0032            15            15            15            15
BM_sinsp_utf8_sanitize_fast_path_ascii_short_median                                +0.0015         +0.0015            15            15            15            15
BM_sinsp_utf8_sanitize_fast_path_ascii_short_stddev                               +16.0119        +15.8748             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_ascii_short_cv                                   +15.9571        +15.8203             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_ascii_long_mean                                   +0.0039         +0.0037            87            87            87            87
BM_sinsp_utf8_sanitize_fast_path_ascii_long_median                                 +0.0011         +0.0009            87            87            87            87
BM_sinsp_utf8_sanitize_fast_path_ascii_long_stddev                                +11.6079        +11.6370             0             1             0             1
BM_sinsp_utf8_sanitize_fast_path_ascii_long_cv                                    +11.5583        +11.5907             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_multibyte_short_mean                              -0.0472         -0.0473            12            12            12            12
BM_sinsp_utf8_sanitize_fast_path_multibyte_short_median                            -0.0473         -0.0474            12            12            12            12
BM_sinsp_utf8_sanitize_fast_path_multibyte_short_stddev                            +1.0797         +0.7240             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_multibyte_short_cv                                +1.1828         +0.8097             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_multibyte_long_mean                               +0.0001         +0.0001          3801          3802          3800          3801
BM_sinsp_utf8_sanitize_fast_path_multibyte_long_median                             +0.0001         +0.0001          3801          3802          3800          3800
BM_sinsp_utf8_sanitize_fast_path_multibyte_long_stddev                             +0.1603         -0.0327             1             1             1             1
BM_sinsp_utf8_sanitize_fast_path_multibyte_long_cv                                 +0.1602         -0.0328             0             0             0             0
BM_sinsp_utf8_sanitize_fast_path_mixed_long_mean                                   +0.0434         +0.0434          1676          1749          1675          1748
BM_sinsp_utf8_sanitize_fast_path_mixed_long_median                                 +0.0395         +0.0394          1683          1749          1683          1749
BM_sinsp_utf8_sanitize_fast_path_mixed_long_stddev                                 +0.0013         +0.0040            25            25            25            25
BM_sinsp_utf8_sanitize_fast_path_mixed_long_cv                                     -0.0403         -0.0377             0             0             0             0
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_alloc_mean                       +0.0067         +0.0067          1115          1122          1114          1122
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_alloc_median                     +0.0076         +0.0074          1114          1122          1114          1122
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_alloc_stddev                     +1.8143         +1.8915             2             5             2             5
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_alloc_cv                         +1.7955         +1.8722             0             0             0             0
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_noalloc_mean                     +0.0077         +0.0077          1112          1120          1111          1120
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_noalloc_median                   +0.0079         +0.0080          1111          1120          1111          1120
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_noalloc_stddev                   +2.4392         +2.3604             1             3             1             3
BM_sinsp_utf8_sanitize_slow_path_c1_controls_long_noalloc_cv                       +2.4128         +2.3347             0             0             0             0
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_alloc_mean                    -0.0054         -0.0054           235           234           235           234
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_alloc_median                  -0.0044         -0.0042           234           233           234           233
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_alloc_stddev                  +0.0603         +0.0589             4             4             4             4
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_alloc_cv                      +0.0661         +0.0646             0             0             0             0
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_noalloc_mean                  +0.0018         +0.0020           143           144           143           144
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_noalloc_median                +0.0102         +0.0105           142           144           142           143
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_noalloc_stddev                -0.8722         -0.8756             2             0             2             0
BM_sinsp_utf8_sanitize_slow_path_sparse_invalid_long_noalloc_cv                    -0.8724         -0.8758             0             0             0             0
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_alloc_mean                       -0.0037         -0.0037         11817         11773         11812         11769
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_alloc_median                     -0.0011         -0.0011         11749         11736         11744         11732
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_alloc_stddev                     -0.5424         -0.5408           187            86           186            86
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_alloc_cv                         -0.5407         -0.5391             0             0             0             0
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_noalloc_mean                     +0.0349         +0.0350         11311         11706         11307         11702
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_noalloc_median                   +0.0497         +0.0497         11178         11734         11174         11729
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_noalloc_stddev                   -0.9398         -0.9398          1056            64          1056            64
BM_sinsp_utf8_sanitize_slow_path_all_invalid_long_noalloc_cv                       -0.9419         -0.9419             0             0             0             0

@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 52.98805% with 118 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.39%. Comparing base (1a1d1e7) to head (9ab1e9e).

Files with missing lines Patch % Lines
userspace/libsinsp/examples/test.cpp 0.00% 112 Missing ⚠️
userspace/libsinsp/test/raw_block.ut.cpp 95.34% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3038      +/-   ##
==========================================
- Coverage   76.56%   76.39%   -0.18%     
==========================================
  Files         301      302       +1     
  Lines       33693    33944     +251     
  Branches     5038     5117      +79     
==========================================
+ Hits        25797    25931     +134     
- Misses       7896     8013     +117     
Flag Coverage Δ
libsinsp 76.39% <52.98%> (-0.18%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@geraldcombs
geraldcombs force-pushed the raw-block-engine branch 2 times, most recently from 0466770 to 8144283 Compare June 30, 2026 18:14
@terror96
terror96 requested a review from Copilot July 2, 2026 09:57

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.

Pull request overview

Adds a new libscap “raw_block” engine and corresponding libsinsp API to process scap/pcapng blocks from an in-memory buffer, enabling external applications to reuse libsinsp analysis without concurrently opening capture files.

Changes:

  • Introduces the raw_block libscap engine (plus a membuf reader) and wires it into engine selection/build config.
  • Exposes sinsp::open_raw_block() and updates the sinsp example + CI to exercise the new engine.
  • Refactors parts of the savefile engine to share block parsing/event reading code with the new engine.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
userspace/libsinsp/sinsp.h Adds open_raw_block() API and documentation.
userspace/libsinsp/sinsp.cpp Implements sinsp::open_raw_block() using the new engine/platform.
userspace/libsinsp/examples/test.cpp Adds --raw_block option and in-memory (optionally gzip-inflated) replay logic.
userspace/libsinsp/examples/CMakeLists.txt Links the example against zlib for gzip inflation support.
userspace/libscap/scap.h Includes the raw_block engine public header.
userspace/libscap/scap_engines.h Declares scap_raw_block_engine under HAS_ENGINE_RAW_BLOCK.
userspace/libscap/scap_config.h.in Adds HAS_ENGINE_RAW_BLOCK configure define.
userspace/libscap/engine/savefile/scap_savefile.c Exposes savefile init/next helpers for reuse by raw_block.
userspace/libscap/engine/savefile/savefile.h Declares the newly exposed savefile helper APIs.
userspace/libscap/engine/raw_block/scap_reader_membuf.c New reader implementation over a memory buffer.
userspace/libscap/engine/raw_block/scap_raw_block.c New engine implementation reusing savefile parsing + converter.
userspace/libscap/engine/raw_block/raw_block.h Defines raw_block engine handle type (alias of savefile_engine).
userspace/libscap/engine/raw_block/raw_block_public.h Public params/docs for configuring the raw_block engine.
userspace/libscap/engine/raw_block/raw_block_platform.h Platform struct definition for raw_block.
userspace/libscap/engine/raw_block/CMakeLists.txt Adds the raw_block engine static library target and deps.
userspace/libscap/CMakeLists.txt Builds/links raw_block engine when enabled.
cmake/modules/engine_config.cmake Enables HAS_ENGINE_RAW_BLOCK by default.
.github/workflows/ci.yml Runs sinsp-example with --raw_block and diffs output vs savefile engine.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread userspace/libsinsp/sinsp.cpp
Comment thread userspace/libscap/engine/raw_block/scap_raw_block.c
Comment thread userspace/libscap/engine/raw_block/scap_reader_membuf.c
Comment thread userspace/libscap/engine/raw_block/scap_raw_block.c Outdated
Comment thread userspace/libscap/engine/raw_block/raw_block_public.h Outdated
Comment thread userspace/libsinsp/sinsp.h
Comment thread userspace/libsinsp/examples/test.cpp
Comment thread userspace/libsinsp/examples/test.cpp
Comment thread userspace/libsinsp/sinsp.cpp
ekoops
ekoops previously approved these changes Jul 16, 2026

@ekoops ekoops left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/approve

@poiana

poiana commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LGTM label has been added.

DetailsGit tree hash: 571c909d00135556b3a7d4768f217ef7babe924f

@ekoops ekoops added this to the 0.26.0 milestone Jul 16, 2026

@leogr leogr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi,

This is a really nice work 👍

I took a look by myself and then with the help of https://github.com/leogr/falco-expert

It SGTM already, I just have one doubt before the final approve, plus a few minor things (that you're free to ignore 😅 )

See inline comments 👇

Comment thread userspace/libsinsp/sinsp.cpp Outdated
Comment thread userspace/libsinsp/sinsp.cpp
Comment thread userspace/libsinsp/sinsp.cpp
Comment thread userspace/libscap/engine/raw_block/scap_raw_block.c Outdated
Comment thread userspace/libsinsp/examples/test.cpp Outdated
Comment thread userspace/libscap/engine/raw_block/raw_block_public.h Outdated
Comment thread userspace/libscap/engine/raw_block/raw_block_public.h
Add an engine that lets you process individual scap / pcapng blocks in
memory. This lets another application that might have a scap file open
make use of the analysis features in libsinsp.

Assisted-by: Claude:claude-opus-4-6 claude-opus-4-8

Signed-off-by: Gerald Combs <gerald@wireshark.org>
Fix a few issues found by Copilot.

Assisted-by: GitHub Copilot

Signed-off-by: Gerald Combs <gerald@wireshark.org>
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
Use the savefile engine's next() routine in the raw_block engine.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
…tests

Update test.cpp to test blocks incrementally. Add a separate "whole
file" option and add it to our CI tests.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
Note that only host-endian data is currently supported. Fix a typo.

Signed-off-by: Gerald Combs <gerald@wireshark.org>
Comment thread userspace/libscap/engine/raw_block/scap_raw_block.c Outdated
Comment thread userspace/libsinsp/examples/test.cpp Outdated
@leogr

leogr commented Aug 12, 2026

Copy link
Copy Markdown
Member

The failing test-e2e (asan) is not on you. The runner image bumped Podman from 4.9.3 to 5.8.4, and that breaks the EXE_LOWER_LAYER / FD_*_LAYER assertions hardcoded in test/e2e/. The very same 16 failures show up on unrelated PRs (see #3076), and that job is not required anyway. We should track it separately.

gnosek
gnosek previously approved these changes Aug 12, 2026
@poiana

poiana commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

LGTM label has been added.

DetailsGit tree hash: 8b2c240825fd3f85157b09f956c9faba507cc72f

Add support for concatenated files / mid-stream metadata blocks to the
raw block engine.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
@poiana

poiana commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

New changes are detected. LGTM label has been removed.

@poiana

poiana commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ekoops, geraldcombs

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

1 similar comment
@poiana

poiana commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ekoops, geraldcombs

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Add a test for the incremental append mode, and change the raw block
mode flag to support "whole_file", "incremental_replace", and
"incremental_append" modes.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
This makes it easier to test sinsp::open_raw_block and should improve
our code coverage.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Gerald Combs <gerald@wireshark.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

6 participants