sflow_parser/models/
core.rs

1//! sFlow v5 data models
2//!
3//! This module contains the data structures representing sFlow v5 datagrams
4//! as defined in <https://sflow.org/sflow_version_5.txt>
5
6use std::net::{Ipv4Addr, Ipv6Addr};
7
8/// MAC address (6 bytes)
9///
10/// Represents a 48-bit IEEE 802 MAC address.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct MacAddress(pub [u8; 6]);
13
14impl MacAddress {
15    /// Create a new MAC address from 6 bytes
16    pub const fn new(bytes: [u8; 6]) -> Self {
17        Self(bytes)
18    }
19
20    /// Get the MAC address as a byte array
21    pub const fn as_bytes(&self) -> &[u8; 6] {
22        &self.0
23    }
24
25    /// Check if this is a broadcast address (FF:FF:FF:FF:FF:FF)
26    pub fn is_broadcast(&self) -> bool {
27        self.0 == [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
28    }
29
30    /// Check if this is a multicast address (first byte has LSB set)
31    pub fn is_multicast(&self) -> bool {
32        self.0[0] & 0x01 != 0
33    }
34
35    /// Check if this is a unicast address
36    pub fn is_unicast(&self) -> bool {
37        !self.is_multicast()
38    }
39}
40
41impl std::fmt::Display for MacAddress {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(
44            f,
45            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
46            self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5]
47        )
48    }
49}
50
51impl From<[u8; 6]> for MacAddress {
52    fn from(bytes: [u8; 6]) -> Self {
53        Self(bytes)
54    }
55}
56
57impl From<MacAddress> for [u8; 6] {
58    fn from(mac: MacAddress) -> Self {
59        mac.0
60    }
61}
62
63/// sFlow datagram version
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum DatagramVersion {
66    Version5 = 5,
67}
68
69/// Address types supported by sFlow
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub enum Address {
72    Unknown,
73    IPv4(Ipv4Addr),
74    IPv6(Ipv6Addr),
75}
76
77/// Data format identifier
78/// Top 20 bits = enterprise ID, bottom 12 bits = format number
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub struct DataFormat(pub u32);
81
82impl DataFormat {
83    pub fn new(enterprise: u32, format: u32) -> Self {
84        Self((enterprise << 12) | (format & 0xFFF))
85    }
86
87    pub fn enterprise(&self) -> u32 {
88        self.0 >> 12
89    }
90
91    pub fn format(&self) -> u32 {
92        self.0 & 0xFFF
93    }
94}
95
96/// sFlow data source identifier
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub struct DataSource(pub u32);
99
100impl DataSource {
101    pub fn new(source_type: u8, index: u32) -> Self {
102        Self(((source_type as u32) << 24) | (index & 0xFFFFFF))
103    }
104
105    pub fn source_type(&self) -> u8 {
106        (self.0 >> 24) as u8
107    }
108
109    pub fn index(&self) -> u32 {
110        self.0 & 0xFFFFFF
111    }
112}
113
114/// Expanded data source (for ifIndex >= 2^24)
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub struct DataSourceExpanded {
117    pub source_id_type: u32,
118    pub source_id_index: u32,
119}
120
121/// Interface identifier
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub struct Interface(pub u32);
124
125impl Interface {
126    pub fn format(&self) -> u8 {
127        (self.0 >> 30) as u8
128    }
129
130    pub fn value(&self) -> u32 {
131        self.0 & 0x3FFFFFFF
132    }
133
134    pub fn is_single(&self) -> bool {
135        self.format() == 0
136    }
137
138    pub fn is_discarded(&self) -> bool {
139        self.format() == 1
140    }
141
142    pub fn is_multiple(&self) -> bool {
143        self.format() == 2
144    }
145}
146
147/// Expanded interface (for ifIndex >= 2^24)
148#[derive(Debug, Clone, Copy, PartialEq, Eq)]
149pub struct InterfaceExpanded {
150    pub format: u32,
151    pub value: u32,
152}
153
154/// Flow data types
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub enum FlowData {
157    /// Sampled Header - Format (0,1)
158    SampledHeader(crate::models::record_flows::SampledHeader),
159    /// Sampled Ethernet - Format (0,2)
160    SampledEthernet(crate::models::record_flows::SampledEthernet),
161    /// Sampled IPv4 - Format (0,3)
162    SampledIpv4(crate::models::record_flows::SampledIpv4),
163    /// Sampled IPv6 - Format (0,4)
164    SampledIpv6(crate::models::record_flows::SampledIpv6),
165    /// Extended Switch - Format (0,1001)
166    ExtendedSwitch(crate::models::record_flows::ExtendedSwitch),
167    /// Extended Router - Format (0,1002)
168    ExtendedRouter(crate::models::record_flows::ExtendedRouter),
169    /// Extended Gateway - Format (0,1003)
170    ExtendedGateway(crate::models::record_flows::ExtendedGateway),
171    /// Extended User - Format (0,1004)
172    ExtendedUser(crate::models::record_flows::ExtendedUser),
173    /// Extended URL - Format (0,1005)
174    /// Note: This format is deprecated but kept for backward compatibility
175    ExtendedUrl(crate::models::record_flows::ExtendedUrl),
176    /// Extended MPLS - Format (0,1006)
177    ExtendedMpls(crate::models::record_flows::ExtendedMpls),
178    /// Extended NAT - Format (0,1007)
179    ExtendedNat(crate::models::record_flows::ExtendedNat),
180    /// Extended MPLS Tunnel - Format (0,1008)
181    ExtendedMplsTunnel(crate::models::record_flows::ExtendedMplsTunnel),
182    /// Extended MPLS VC - Format (0,1009)
183    ExtendedMplsVc(crate::models::record_flows::ExtendedMplsVc),
184    /// Extended MPLS FEC - Format (0,1010)
185    ExtendedMplsFec(crate::models::record_flows::ExtendedMplsFec),
186    /// Extended MPLS LVP FEC - Format (0,1011)
187    ExtendedMplsLvpFec(crate::models::record_flows::ExtendedMplsLvpFec),
188    /// Extended VLAN Tunnel - Format (0,1012)
189    ExtendedVlanTunnel(crate::models::record_flows::ExtendedVlanTunnel),
190    /// Extended 802.11 Payload - Format (0,1013)
191    Extended80211Payload(crate::models::record_flows::Extended80211Payload),
192    /// Extended 802.11 RX - Format (0,1014)
193    Extended80211Rx(crate::models::record_flows::Extended80211Rx),
194    /// Extended 802.11 TX - Format (0,1015)
195    Extended80211Tx(crate::models::record_flows::Extended80211Tx),
196    /// Extended 802.11 Aggregation - Format (0,1016)
197    Extended80211Aggregation(crate::models::record_flows::Extended80211Aggregation),
198    /// Extended OpenFlow v1 - Format (0,1017) - DEPRECATED
199    ExtendedOpenFlowV1(crate::models::record_flows::ExtendedOpenFlowV1),
200    /// Extended Socket IPv4 - Format (0,2100)
201    ExtendedSocketIpv4(crate::models::record_flows::ExtendedSocketIpv4),
202    /// Extended Socket IPv6 - Format (0,2101)
203    ExtendedSocketIpv6(crate::models::record_flows::ExtendedSocketIpv6),
204    /// Application Operation - Format (0,2202)
205    AppOperation(crate::models::record_flows::AppOperation),
206    /// Application Parent Context - Format (0,2203)
207    AppParentContext(crate::models::record_flows::AppParentContext),
208    /// Unknown or unparsed format
209    Unknown { format: DataFormat, data: Vec<u8> },
210}
211
212/// Flow record containing flow data
213#[derive(Debug, Clone, PartialEq, Eq)]
214pub struct FlowRecord {
215    pub flow_format: DataFormat,
216    pub flow_data: FlowData,
217}
218
219/// Counter data types
220#[derive(Debug, Clone, PartialEq)]
221pub enum CounterData {
222    /// Generic Interface Counters - Format (0,1)
223    GenericInterface(crate::models::record_counters::GenericInterfaceCounters),
224    /// Ethernet Interface Counters - Format (0,2)
225    EthernetInterface(crate::models::record_counters::EthernetInterfaceCounters),
226    /// Token Ring Counters - Format (0,3)
227    TokenRing(crate::models::record_counters::TokenRingCounters),
228    /// 100BaseVG Counters - Format (0,4)
229    Vg100Interface(crate::models::record_counters::Vg100InterfaceCounters),
230    /// VLAN Counters - Format (0,5)
231    Vlan(crate::models::record_counters::VlanCounters),
232    /// IEEE 802.11 Counters - Format (0,6)
233    Ieee80211(crate::models::record_counters::Ieee80211Counters),
234    /// Processor Counters - Format (0,1001)
235    Processor(crate::models::record_counters::ProcessorCounters),
236    /// Radio Utilization - Format (0,1002)
237    RadioUtilization(crate::models::record_counters::RadioUtilization),
238    /// Host Description - Format (0,2000)
239    HostDescription(crate::models::record_counters::HostDescription),
240    /// Host Adapters - Format (0,2001)
241    HostAdapters(crate::models::record_counters::HostAdapters),
242    /// Host Parent - Format (0,2002)
243    HostParent(crate::models::record_counters::HostParent),
244    /// Host CPU - Format (0,2003)
245    HostCpu(crate::models::record_counters::HostCpu),
246    /// Host Memory - Format (0,2004)
247    HostMemory(crate::models::record_counters::HostMemory),
248    /// Host Disk I/O - Format (0,2005)
249    HostDiskIo(crate::models::record_counters::HostDiskIo),
250    /// Host Network I/O - Format (0,2006)
251    HostNetIo(crate::models::record_counters::HostNetIo),
252    /// Virtual Node - Format (0,2100)
253    VirtualNode(crate::models::record_counters::VirtualNode),
254    /// Virtual CPU - Format (0,2101)
255    VirtualCpu(crate::models::record_counters::VirtualCpu),
256    /// Virtual Memory - Format (0,2102)
257    VirtualMemory(crate::models::record_counters::VirtualMemory),
258    /// Virtual Disk I/O - Format (0,2103)
259    VirtualDiskIo(crate::models::record_counters::VirtualDiskIo),
260    /// Virtual Network I/O - Format (0,2104)
261    VirtualNetIo(crate::models::record_counters::VirtualNetIo),
262    /// OpenFlow Port - Format (0,1004)
263    OpenFlowPort(crate::models::record_counters::OpenFlowPort),
264    /// OpenFlow Port Name - Format (0,1005)
265    OpenFlowPortName(crate::models::record_counters::OpenFlowPortName),
266    /// App Operations - Format (0,2202)
267    AppOperations(crate::models::record_counters::AppOperations),
268    /// App Resources - Format (0,2203)
269    AppResources(crate::models::record_counters::AppResources),
270    /// App Workers - Format (0,2206)
271    AppWorkers(crate::models::record_counters::AppWorkers),
272    /// Unknown or unparsed format
273    Unknown { format: DataFormat, data: Vec<u8> },
274}
275
276/// Counter record containing counter data
277#[derive(Debug, Clone, PartialEq)]
278pub struct CounterRecord {
279    pub counter_format: DataFormat,
280    pub counter_data: CounterData,
281}
282
283/// Compact flow sample (enterprise=0, format=1)
284#[derive(Debug, Clone, PartialEq, Eq)]
285pub struct FlowSample {
286    pub sequence_number: u32,
287    pub source_id: DataSource,
288    pub sampling_rate: u32,
289    pub sample_pool: u32,
290    pub drops: u32,
291    pub input: Interface,
292    pub output: Interface,
293    pub flow_records: Vec<FlowRecord>,
294}
295
296/// Compact counters sample (enterprise=0, format=2)
297#[derive(Debug, Clone, PartialEq)]
298pub struct CountersSample {
299    pub sequence_number: u32,
300    pub source_id: DataSource,
301    pub counters: Vec<CounterRecord>,
302}
303
304/// Expanded flow sample (enterprise=0, format=3)
305#[derive(Debug, Clone, PartialEq, Eq)]
306pub struct FlowSampleExpanded {
307    pub sequence_number: u32,
308    pub source_id: DataSourceExpanded,
309    pub sampling_rate: u32,
310    pub sample_pool: u32,
311    pub drops: u32,
312    pub input: InterfaceExpanded,
313    pub output: InterfaceExpanded,
314    pub flow_records: Vec<FlowRecord>,
315}
316
317/// Expanded counter sample (enterprise=0, format=4)
318#[derive(Debug, Clone, PartialEq)]
319pub struct CountersSampleExpanded {
320    pub sequence_number: u32,
321    pub source_id: DataSourceExpanded,
322    pub counters: Vec<CounterRecord>,
323}
324
325/// Sample data types
326#[derive(Debug, Clone, PartialEq)]
327pub enum SampleData {
328    FlowSample(FlowSample),
329    CountersSample(CountersSample),
330    FlowSampleExpanded(FlowSampleExpanded),
331    CountersSampleExpanded(CountersSampleExpanded),
332    Unknown { format: DataFormat, data: Vec<u8> },
333}
334
335/// Sample record
336#[derive(Debug, Clone, PartialEq)]
337pub struct SampleRecord {
338    pub sample_type: DataFormat,
339    pub sample_data: SampleData,
340}
341
342/// sFlow v5 datagram
343#[derive(Debug, Clone, PartialEq)]
344pub struct SFlowDatagram {
345    pub version: DatagramVersion,
346    pub agent_address: Address,
347    pub sub_agent_id: u32,
348    pub sequence_number: u32,
349    pub uptime: u32,
350    pub samples: Vec<SampleRecord>,
351}
352
353impl SFlowDatagram {
354    /// Create a new sFlow v5 datagram
355    pub fn new(
356        agent_address: Address,
357        sub_agent_id: u32,
358        sequence_number: u32,
359        uptime: u32,
360    ) -> Self {
361        Self {
362            version: DatagramVersion::Version5,
363            agent_address,
364            sub_agent_id,
365            sequence_number,
366            uptime,
367            samples: Vec::new(),
368        }
369    }
370}