flowparser-sflow
An sFlow v5 parser library written in Rust. Parses sFlow v5 datagrams (sFlow v5 specification) including flow samples, counter samples, and all standard record types.
Features
- sFlow v5 datagram parsing with IPv4 and IPv6 agent addresses
- All four sample types: Flow Sample, Counter Sample, Expanded Flow Sample, Expanded Counter Sample
- Flow record types: Raw Packet Header, Sampled Ethernet, Sampled IPv4/IPv6, Extended Switch, Extended Router, Extended Gateway, Extended User, Extended URL
- Counter record types: Generic Interface, Ethernet Interface, Token Ring, VLAN, Processor
- Unknown record handling: Unrecognized records captured as raw bytes for forward compatibility
- Serde support: All types serialize/deserialize to JSON and other formats
- Builder pattern: Optional configuration (e.g., max samples limit for DoS protection)
Usage
Add to your Cargo.toml:
[]
= "0.1.0"
Basic Parsing
use ;
let parser = default;
// Parse sFlow datagram bytes (e.g., received from UDP socket)
let result = parser.parse_bytes;
for datagram in &result.datagrams
if let Some = &result.error
JSON Serialization
use SflowParser;
let parser = default;
let result = parser.parse_bytes;
// Serialize to JSON
let json = to_string_pretty.unwrap;
println!;
Builder Configuration
use SflowParser;
// Limit max samples per datagram (DoS protection)
let parser = builder
.with_max_samples
.build;
UDP Listener Example
use SflowParser;
use UdpSocket;
let socket = bind.unwrap;
let parser = default;
let mut buf = ;
loop
sFlow v5 Protocol Structure
Datagram
├── Header (version, agent address, sub-agent ID, sequence, uptime)
└── Samples[]
├── Flow Sample (enterprise=0, format=1)
│ └── Flow Records[]
│ ├── Raw Packet Header (0:1)
│ ├── Sampled Ethernet (0:2)
│ ├── Sampled IPv4 (0:3)
│ ├── Sampled IPv6 (0:4)
│ ├── Extended Switch (0:1001)
│ ├── Extended Router (0:1002)
│ ├── Extended Gateway (0:1003)
│ ├── Extended User (0:1004)
│ └── Extended URL (0:1005)
├── Counter Sample (enterprise=0, format=2)
│ └── Counter Records[]
│ ├── Generic Interface (0:1)
│ ├── Ethernet Interface (0:2)
│ ├── Token Ring (0:3)
│ ├── VLAN (0:5)
│ └── Processor (0:1001)
├── Expanded Flow Sample (enterprise=0, format=3)
└── Expanded Counter Sample (enterprise=0, format=4)
Types
| Type | Description |
|---|---|
SflowParser |
Main parser with optional configuration |
SflowDatagram |
Parsed datagram with header fields and samples |
SflowSample |
Enum: Flow, Counter, ExpandedFlow, ExpandedCounter, Unknown |
FlowRecord |
Enum of all flow record types |
CounterRecord |
Enum of all counter record types |
AddressType |
IPv4 or IPv6 agent address |
ParseResult |
Contains parsed datagrams and optional error |
SflowError |
Error variants: Incomplete, UnsupportedVersion, ParseError, TooManySamples |
Examples
Run the included examples:
# Single-threaded UDP listener
# Multi-threaded UDP listener
# Async (tokio) UDP listener
# Parse from pcap file
Benchmarks
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.