- Packet-based filtering - Filters packets based on their content (headers)
- Metadata-based filtering - Filters packets based on their associated kernel metadata
Filtering also works with packet tracking. Even if a packet doesn’t match the filter at a particular probe point, it will still be reported if it was previously tracked.
Packet Filtering
Packet filtering uses the familiarpcap-filter syntax. The filter expression is compiled to cBPF, then translated to eBPF for efficient in-kernel execution.
Basic Syntax
Seeman pcap-filter for complete syntax details. Here are common examples:
- L2 and L3 Filters
- L2-Only Filters
Retis automatically detects and generates both L2 and L3 filters based on your expression. This allows matching:Internally, Retis generates two filters:
- Fully formed packets with valid L2 headers (Layer 2)
- Packets without L2 headers but with valid network headers (Layer 3)
- For probes where only
network_headeris valid: matches TCP port 443 - For probes with valid
mac_header: matches both ARP and TCP port 443
Metadata Filtering
Metadata filtering allows you to write filters that match packets based on their kernel metadata - any field in thesk_buff structure or nested data structures it references.
Syntax Overview
Metadata filters automatically follow struct pointers, allowing indirect access to structures referenced bysk_buff fields:
The
sk_buff keyword must always be present and must always appear first in each expression.Relational Operators
Metadata filters support standard comparison operators:| Operator | Meaning | Example |
|---|---|---|
== | Equal to | sk_buff.mark == 0x100 |
!= | Not equal to | sk_buff.mark != 0 |
< | Less than | sk_buff.len < 1500 |
<= | Less than or equal to | sk_buff.len <= 1500 |
> | Greater than | sk_buff.len > 64 |
>= | Greater than or equal to | sk_buff.len >= 64 |
| (omit) | Not equal to zero (implicit) | sk_buff.cloned |
Boolean Operators
Combine conditions with logical operators:| Operator | Alternate | Meaning | Example | ||||
|---|---|---|---|---|---|---|---|
&& | and | Conjunction | sk_buff.mark == 0x100 && sk_buff.cloned | ||||
| ` | ` | or | Disjunction | `sk_buff.pkt_type == 0x0 | sk_buff.mark == 0x100` |
Operator Precedence and Parentheses
Operator Precedence and Parentheses
Boolean operators have the same precedence and are left-associative by default. Use parentheses to change associativity:This is equivalent to:To change the evaluation order:There’s no limit to parentheses nesting. Use them to avoid ambiguity and optimize short-circuit evaluation.
Numeric Values
Numeric values can be expressed in multiple bases:- Bitwise Masks
- Bitwise NOT
Apply bitwise AND operations before comparison using masks:This is equivalent to the C expression:Masks can be specified in any base (hexadecimal, binary, or decimal) and must be positive. While masks up to
u64::MAX are allowed, ensuring consistency is your responsibility.String Comparisons
String values must be enclosed in quotes and only support equality operators:Type Casting
Follow pointers embedded in members with different defined types:mark field of the nf_conn structure pointed to by _nfct after masking.
Signed vs. Unsigned
Retis supports both signed and unsigned comparisons:- Unsigned numbers can use any base (decimal, hex, binary)
- Signed numbers (negative values) can only use base 10
- Negative numbers are only allowed against signed struct members
- Bitfields are supported and treated as regular numbers
Combining Filter Types
You can use packet and metadata filtering together. Both filters must match for an event to be generated:Advanced Examples
Next Steps
Tracking
Learn how filtered packets are tracked across the stack
Collectors
Understand what data collectors can provide for filtering
