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 L2 Tunnel Egress - Format (0,1021)
201    ExtendedL2TunnelEgress(crate::models::record_flows::ExtendedL2TunnelEgress),
202    /// Extended L2 Tunnel Ingress - Format (0,1022)
203    ExtendedL2TunnelIngress(crate::models::record_flows::ExtendedL2TunnelIngress),
204    /// Extended IPv4 Tunnel Egress - Format (0,1023)
205    ExtendedIpv4TunnelEgress(crate::models::record_flows::ExtendedIpv4TunnelEgress),
206    /// Extended IPv4 Tunnel Ingress - Format (0,1024)
207    ExtendedIpv4TunnelIngress(crate::models::record_flows::ExtendedIpv4TunnelIngress),
208    /// Extended IPv6 Tunnel Egress - Format (0,1025)
209    ExtendedIpv6TunnelEgress(crate::models::record_flows::ExtendedIpv6TunnelEgress),
210    /// Extended IPv6 Tunnel Ingress - Format (0,1026)
211    ExtendedIpv6TunnelIngress(crate::models::record_flows::ExtendedIpv6TunnelIngress),
212    /// Extended Decapsulate Egress - Format (0,1027)
213    ExtendedDecapsulateEgress(crate::models::record_flows::ExtendedDecapsulateEgress),
214    /// Extended Decapsulate Ingress - Format (0,1028)
215    ExtendedDecapsulateIngress(crate::models::record_flows::ExtendedDecapsulateIngress),
216    /// Extended VNI Egress - Format (0,1029)
217    ExtendedVniEgress(crate::models::record_flows::ExtendedVniEgress),
218    /// Extended VNI Ingress - Format (0,1030)
219    ExtendedVniIngress(crate::models::record_flows::ExtendedVniIngress),
220    /// Extended Socket IPv4 - Format (0,2100)
221    ExtendedSocketIpv4(crate::models::record_flows::ExtendedSocketIpv4),
222    /// Extended Socket IPv6 - Format (0,2101)
223    ExtendedSocketIpv6(crate::models::record_flows::ExtendedSocketIpv6),
224    /// Application Operation - Format (0,2202)
225    AppOperation(crate::models::record_flows::AppOperation),
226    /// Application Parent Context - Format (0,2203)
227    AppParentContext(crate::models::record_flows::AppParentContext),
228    /// Application Initiator - Format (0,2204)
229    AppInitiator(crate::models::record_flows::AppInitiator),
230    /// Application Target - Format (0,2205)
231    AppTarget(crate::models::record_flows::AppTarget),
232    /// Unknown or unparsed format
233    Unknown { format: DataFormat, data: Vec<u8> },
234}
235
236/// Flow record containing flow data
237#[derive(Debug, Clone, PartialEq, Eq)]
238pub struct FlowRecord {
239    pub flow_format: DataFormat,
240    pub flow_data: FlowData,
241}
242
243/// Counter data types
244#[derive(Debug, Clone, PartialEq)]
245pub enum CounterData {
246    /// Generic Interface Counters - Format (0,1)
247    GenericInterface(crate::models::record_counters::GenericInterfaceCounters),
248    /// Ethernet Interface Counters - Format (0,2)
249    EthernetInterface(crate::models::record_counters::EthernetInterfaceCounters),
250    /// Token Ring Counters - Format (0,3)
251    TokenRing(crate::models::record_counters::TokenRingCounters),
252    /// 100BaseVG Counters - Format (0,4)
253    Vg100Interface(crate::models::record_counters::Vg100InterfaceCounters),
254    /// VLAN Counters - Format (0,5)
255    Vlan(crate::models::record_counters::VlanCounters),
256    /// IEEE 802.11 Counters - Format (0,6)
257    Ieee80211(crate::models::record_counters::Ieee80211Counters),
258    /// Processor Counters - Format (0,1001)
259    Processor(crate::models::record_counters::ProcessorCounters),
260    /// Radio Utilization - Format (0,1002)
261    RadioUtilization(crate::models::record_counters::RadioUtilization),
262    /// Host Description - Format (0,2000)
263    HostDescription(crate::models::record_counters::HostDescription),
264    /// Host Adapters - Format (0,2001)
265    HostAdapters(crate::models::record_counters::HostAdapters),
266    /// Host Parent - Format (0,2002)
267    HostParent(crate::models::record_counters::HostParent),
268    /// Host CPU - Format (0,2003)
269    HostCpu(crate::models::record_counters::HostCpu),
270    /// Host Memory - Format (0,2004)
271    HostMemory(crate::models::record_counters::HostMemory),
272    /// Host Disk I/O - Format (0,2005)
273    HostDiskIo(crate::models::record_counters::HostDiskIo),
274    /// Host Network I/O - Format (0,2006)
275    HostNetIo(crate::models::record_counters::HostNetIo),
276    /// Virtual Node - Format (0,2100)
277    VirtualNode(crate::models::record_counters::VirtualNode),
278    /// Virtual CPU - Format (0,2101)
279    VirtualCpu(crate::models::record_counters::VirtualCpu),
280    /// Virtual Memory - Format (0,2102)
281    VirtualMemory(crate::models::record_counters::VirtualMemory),
282    /// Virtual Disk I/O - Format (0,2103)
283    VirtualDiskIo(crate::models::record_counters::VirtualDiskIo),
284    /// Virtual Network I/O - Format (0,2104)
285    VirtualNetIo(crate::models::record_counters::VirtualNetIo),
286    /// OpenFlow Port - Format (0,1004)
287    OpenFlowPort(crate::models::record_counters::OpenFlowPort),
288    /// OpenFlow Port Name - Format (0,1005)
289    OpenFlowPortName(crate::models::record_counters::OpenFlowPortName),
290    /// App Operations - Format (0,2202)
291    AppOperations(crate::models::record_counters::AppOperations),
292    /// App Resources - Format (0,2203)
293    AppResources(crate::models::record_counters::AppResources),
294    /// App Workers - Format (0,2206)
295    AppWorkers(crate::models::record_counters::AppWorkers),
296    /// Unknown or unparsed format
297    Unknown { format: DataFormat, data: Vec<u8> },
298}
299
300/// Counter record containing counter data
301#[derive(Debug, Clone, PartialEq)]
302pub struct CounterRecord {
303    pub counter_format: DataFormat,
304    pub counter_data: CounterData,
305}
306
307/// Compact flow sample (enterprise=0, format=1)
308#[derive(Debug, Clone, PartialEq, Eq)]
309pub struct FlowSample {
310    pub sequence_number: u32,
311    pub source_id: DataSource,
312    pub sampling_rate: u32,
313    pub sample_pool: u32,
314    pub drops: u32,
315    pub input: Interface,
316    pub output: Interface,
317    pub flow_records: Vec<FlowRecord>,
318}
319
320/// Compact counters sample (enterprise=0, format=2)
321#[derive(Debug, Clone, PartialEq)]
322pub struct CountersSample {
323    pub sequence_number: u32,
324    pub source_id: DataSource,
325    pub counters: Vec<CounterRecord>,
326}
327
328/// Expanded flow sample (enterprise=0, format=3)
329#[derive(Debug, Clone, PartialEq, Eq)]
330pub struct FlowSampleExpanded {
331    pub sequence_number: u32,
332    pub source_id: DataSourceExpanded,
333    pub sampling_rate: u32,
334    pub sample_pool: u32,
335    pub drops: u32,
336    pub input: InterfaceExpanded,
337    pub output: InterfaceExpanded,
338    pub flow_records: Vec<FlowRecord>,
339}
340
341/// Expanded counter sample (enterprise=0, format=4)
342#[derive(Debug, Clone, PartialEq)]
343pub struct CountersSampleExpanded {
344    pub sequence_number: u32,
345    pub source_id: DataSourceExpanded,
346    pub counters: Vec<CounterRecord>,
347}
348
349/// Sample data types
350#[derive(Debug, Clone, PartialEq)]
351pub enum SampleData {
352    FlowSample(FlowSample),
353    CountersSample(CountersSample),
354    FlowSampleExpanded(FlowSampleExpanded),
355    CountersSampleExpanded(CountersSampleExpanded),
356    Unknown { format: DataFormat, data: Vec<u8> },
357}
358
359/// Sample record
360#[derive(Debug, Clone, PartialEq)]
361pub struct SampleRecord {
362    pub sample_type: DataFormat,
363    pub sample_data: SampleData,
364}
365
366/// sFlow v5 datagram
367#[derive(Debug, Clone, PartialEq)]
368pub struct SFlowDatagram {
369    pub version: DatagramVersion,
370    pub agent_address: Address,
371    pub sub_agent_id: u32,
372    pub sequence_number: u32,
373    pub uptime: u32,
374    pub samples: Vec<SampleRecord>,
375}
376
377impl SFlowDatagram {
378    /// Create a new sFlow v5 datagram
379    pub fn new(
380        agent_address: Address,
381        sub_agent_id: u32,
382        sequence_number: u32,
383        uptime: u32,
384    ) -> Self {
385        Self {
386            version: DatagramVersion::Version5,
387            agent_address,
388            sub_agent_id,
389            sequence_number,
390            uptime,
391            samples: Vec::new(),
392        }
393    }
394}