Repository navigation
Add PCAP input format for reading packet captures - #109361
Closed
remogeissbuehler wants to merge 22 commits into
Closed
remogeissbuehler wants to merge 22 commits into
remogeissbuehler wants to merge 22 commits into
Conversation
Introduce a new `PCAP` input format that reads packet capture files (both classic `pcap` and `pcapng`) and produces one row per packet with decoded L2-L4 header fields, modelled structurally on the `DWARF` format. The format derives from `IInputFormat` (a block input format) and is registered via `registerRandomAccessInputFormat`; it supports a subset of columns. It exposes: `number`, `timestamp` (`DateTime64(9)`), `capture_length`, `original_length`, `link_type`, `protocols` (the ordered PDU chain, e.g. `['ETHERNET_II','IP','TCP']`), Ethernet fields (`eth_src`, `eth_dst`, `eth_type`, `vlan_id`), IP fields (`ip_version`, `src_addr`/`dst_addr` as IPv4-mapped `IPv6`, `ip_protocol`, `ip_ttl`), transport fields (`src_port`, `dst_port`, `tcp_flags`, `tcp_seq`, `tcp_ack`), the L4 `payload`, and the full raw frame `raw`. Parsing is done with `libtins`, which reads the capture container through `libpcap`. Both are added as contrib submodules gated behind a new `USE_PCAP` flag (`ENABLE_PCAP`, on by default with `ENABLE_LIBRARIES`): - `libpcap` (1.10.5) is built offline-only using the `null` capture backend; the BPF grammar/scanner are pre-generated (bison/flex) and checked in under `contrib/libpcap-cmake/generated` so no flex/bison is needed at build time. A hand-written `config.h` targets Linux/glibc. - `libtins` (v4.5) is built with a hand-written `config.h`; WPA2 decryption and ACK tracking are disabled to avoid OpenSSL and Boost.ICL dependencies for now. Deeper protocol dissection can be layered on later. The whole format is wrapped in `#if USE_PCAP` with empty registration stubs in the disabled branch so linking always succeeds. Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
Add `docs/en/interfaces/formats/PCAP.md` describing the format, its columns, the `capture_length` vs `original_length` distinction, and example queries, and list `PCAP` in the formats index table. Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
The read loop previously obtained packets through libtins' `FileSniffer::next_packet`, which discards the original per-packet pcap header and re-serializes libtins' parsed view. As a result `raw`, `capture_length` and `payload` came from `PDU::serialize` (which can differ from the captured bytes), and `original_length` was merely a copy of the serialized size rather than the packet's true wire length. Read packets with libpcap's `pcap_next_ex` instead, using the real `pcap_pkthdr`: - `capture_length` = `caplen`, `original_length` = `len` (the on-wire size, which exceeds `caplen` when a snapshot length was used), - `raw` = the exact captured bytes, so `length(raw) == capture_length`, - `timestamp` from the record header. The raw bytes are then dissected with `Tins::Internals::pdu_from_dlt_flag` using the sniffer's datalink type. This makes the identity `substring(raw, capture_length - payload_length + 1) == payload` hold, as documented (verified across the whole example capture). Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
Capture files are read with microsecond precision (libtins' file sniffer opens savefiles that way), so nanosecond-resolution captures are truncated to microseconds even though the `timestamp` column has a `DateTime64(9)` type. Note this in the format documentation. Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
Add a "Storing captures in a table" section to the PCAP format docs showing how to create a `MergeTree` table and `INSERT ... SELECT` from a capture file. Explain that storing both `raw` and `payload` is redundant because the payload is a suffix of the raw frame (`substring(raw, (capture_length - payload_length) + 1) = payload`), and give guidance on choosing a sorting key for flow-oriented vs time-series analysis, including the `MergeTree` restriction on nullable columns in the key. Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
Add `04498_pcap_input_format.sh` covering the `PCAP` format against four small capture fixtures (classic `pcap` and `pcapng`): an HTTP/TCP packet, a DHCP/UDP exchange, ICMP echo packets, and a TLS session. The test checks the schema, decoded L2-L4 fields, the protocol chain, subset-of-columns reads, and the `length(raw) == capture_length` / `original_length >= capture_length` / `substring(raw, capture_length - payload_length + 1) == payload` invariants. Tagged `no-fasttest` because the format requires libtins/libpcap, which are not built in fasttest (`ENABLE_LIBRARIES=0`). The fixtures are sample captures from the Wireshark test suite (https://github.com/wireshark/wireshark/tree/master/test/captures); their provenance is recorded in `data_pcap/README.md`. Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
The `PCAP` input format only reads capture files and never calls `pcap_compile`, so the BPF filter compiler (`gencode.c` and its bison/flex grammar/scanner) was dead code that existed only to satisfy the linker. Drop it from the build entirely. This also removes the checked-in bison output `grammar.c`, whose GPL-licensed header is incompatible with ClickHouse's Apache 2.0 licensing. The read path (`savefile.c`, `sf-pcap.c`, `sf-pcapng.c`, `pcap.c`, `optimize.c`) does not depend on `gencode.c`; it only needs `pcap_freecode` and the BPF interpreter in `bpf_filter.c`. A new `compile-stub.c` provides minimal `pcap_compile` / `pcap_compile_nopcap` stubs that return `PCAP_ERROR`, plus the genuine grammar-independent `pcap_freecode`, so the static archive links cleanly. Verified: the full `clickhouse` binary links, and `04498_pcap_input_format` produces output identical to its reference (classic `pcap` and `pcapng` HTTP, DHCP, ICMP, and TLS fixtures decode correctly). Co-authored-by: Claude (Anthropic claude-opus-4-8) <[email protected]>
Contributor
|
Workflow [PR], commit [52ac581] Summary: ❌
AI ReviewSummaryThis PR adds a new Missing context / blind spots
Findings❌ Blockers
|
The style check rejects `Tcp` because abbreviations such as `TCP` must be in all caps. Rename `formatTcpFlags` to `formatTCPFlags`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109361&sha=74c448c7a58747b922b788b06a155ac2e31616bc&name_0=PR&name_1=Style%20check Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
# Conflicts: # .gitmodules
# Conflicts: # docs/en/interfaces/formats.md
Member
|
🕵 Superseded by #115453 because the source fork is no longer available, so its branch cannot be updated. The replacement preserves the signed original commits, incorporates current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add PCAP input format for reading packet captures
(Human) Introduction
As someone who works with ClickHouse but also occasionally opens a pcap in Wireshark, I've always had a wish to be able to do some quick stats on a pcap using ClickHouse SQL. Especially for large files, I always thought it would also be nice to be able to persist them in a MergeTree, so they don't have to be re-scanned every time.
So this is my first draft at making this a reality: adding PCAP as an (experimental) input format which can be read by Clickhouse directly (and consequently be ingested, as well). It currently only parses very basic stuff (mostly L2-L4), but this could be extended in the future (imagine having all Wireshark dissectors 😍 )
I realize this may be considered "unfinished" (in the sense: lacking features), but I wanted to open a PR and get feedback on the idea early on, to see if it was worth pursuing.
Full transparency: this is basically 100% vibe-coded -- ideas and some design decisions are mine, but I didn't really write any code. Thanks Claude.
In that spirit, here's the Claude-generated description including some notes that may be relevant to reviewers:
AI-generated Overview
Adds a
PCAPinput format that reads packet capture files (classicpcapandpcapng) and produces one row per packet with decoded L2–L4 fields — timestamp, lengths, link type, the ordered protocol chain, Ethernet/IPv4/IPv6/TCP/UDP header fields, the L4 payload, and the raw frame. Modelled structurally on theDWARFformat.Parsing uses
libtinsfor dissection andlibpcapfor reading the capture container, added as contrib submodules behind a newUSE_PCAPflag (libpcapbuilt offline-only with pre-generated BPF grammar/scanner; WPA2/ACK-tracking disabled to avoid OpenSSL and Boost.ICL). Includes documentation and a stateless test with small Wireshark sample captures.Notes for reviewers
libtins(v4.5) andlibpcap(1.10.5), gated behindENABLE_PCAP/USE_PCAP(on by default withENABLE_LIBRARIES). They use the upstream repositories directly (not ClickHouse forks).nullcapture backend (no live capture / remote / optional backends). Its BPF grammar/scanner are pre-generated with bison/flex and checked in undercontrib/libpcap-cmake/generated/so the build needs no flex/bison. A hand-writtenconfig.htargets Linux/glibc.config.h; WPA2 decryption and ACK tracking are disabled to avoid pulling in OpenSSL and Boost.ICL for now. Deeper protocol dissection can be layered on later.#if USE_PCAPwith empty registration stubs, so linking succeeds when the feature is off (e.g. fasttest builds withENABLE_LIBRARIES=0). The stateless test is taggedno-fasttestfor the same reason.tests/queries/0_stateless/data_pcap/are sample captures from the Wireshark test suite (GPL-2.0); provenance is recorded in that directory'sREADME.md. Happy to swap them for self-authored synthetic captures if the GPL-2.0 provenance is a concern.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
New input format
PCAPfor reading network packet captures (pcapandpcapng), producing one row per packet with decoded Ethernet/IP/TCP/UDP fields, the protocol chain, payload, and raw frame.