netlink_packet_route/link/
stats.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_core::{DecodeError, Emitable, Parseable};
4
5pub(crate) const LINK_STATS_LEN: usize = 96;
6
7#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
8#[non_exhaustive]
9pub struct Stats {
10    /// total packets received
11    pub rx_packets: u32,
12    /// total packets transmitted
13    pub tx_packets: u32,
14    /// total bytes received
15    pub rx_bytes: u32,
16    /// total bytes transmitted
17    pub tx_bytes: u32,
18    /// bad packets received
19    pub rx_errors: u32,
20    /// packet transmit problems
21    pub tx_errors: u32,
22    /// no space in linux buffers
23    pub rx_dropped: u32,
24    /// no space available in linux
25    pub tx_dropped: u32,
26    /// multicast packets received
27    pub multicast: u32,
28    pub collisions: u32,
29
30    // detailed rx_errors
31    pub rx_length_errors: u32,
32    /// receiver ring buff overflow
33    pub rx_over_errors: u32,
34    /// received packets with crc error
35    pub rx_crc_errors: u32,
36    /// received frame alignment errors
37    pub rx_frame_errors: u32,
38    /// recv'r fifo overrun
39    pub rx_fifo_errors: u32,
40    /// receiver missed packet
41    pub rx_missed_errors: u32,
42
43    // detailed tx_errors
44    pub tx_aborted_errors: u32,
45    pub tx_carrier_errors: u32,
46    pub tx_fifo_errors: u32,
47    pub tx_heartbeat_errors: u32,
48    pub tx_window_errors: u32,
49
50    // for cslip etc
51    pub rx_compressed: u32,
52    pub tx_compressed: u32,
53
54    /// dropped, no handler found
55    pub rx_nohandler: u32,
56}
57
58buffer!(StatsBuffer(LINK_STATS_LEN) {
59    rx_packets: (u32, 0..4),
60    tx_packets: (u32, 4..8),
61    rx_bytes: (u32, 8..12),
62    tx_bytes: (u32, 12..16),
63    rx_errors: (u32, 16..20),
64    tx_errors: (u32, 20..24),
65    rx_dropped: (u32, 24..28),
66    tx_dropped: (u32, 28..32),
67    multicast: (u32, 32..36),
68    collisions: (u32, 36..40),
69    rx_length_errors: (u32, 40..44),
70    rx_over_errors: (u32, 44..48),
71    rx_crc_errors: (u32, 48..52),
72    rx_frame_errors: (u32, 52..56),
73    rx_fifo_errors: (u32, 56..60),
74    rx_missed_errors: (u32, 60..64),
75    tx_aborted_errors: (u32, 64..68),
76    tx_carrier_errors: (u32, 68..72),
77    tx_fifo_errors: (u32, 72..76),
78    tx_heartbeat_errors: (u32, 76..80),
79    tx_window_errors: (u32, 80..84),
80    rx_compressed: (u32, 84..88),
81    tx_compressed: (u32, 88..92),
82    rx_nohandler: (u32, 92..96),
83});
84
85impl<T: AsRef<[u8]>> Parseable<StatsBuffer<T>> for Stats {
86    fn parse(buf: &StatsBuffer<T>) -> Result<Self, DecodeError> {
87        Ok(Self {
88            rx_packets: buf.rx_packets(),
89            tx_packets: buf.tx_packets(),
90            rx_bytes: buf.rx_bytes(),
91            tx_bytes: buf.tx_bytes(),
92            rx_errors: buf.rx_errors(),
93            tx_errors: buf.tx_errors(),
94            rx_dropped: buf.rx_dropped(),
95            tx_dropped: buf.tx_dropped(),
96            multicast: buf.multicast(),
97            collisions: buf.collisions(),
98            rx_length_errors: buf.rx_length_errors(),
99            rx_over_errors: buf.rx_over_errors(),
100            rx_crc_errors: buf.rx_crc_errors(),
101            rx_frame_errors: buf.rx_frame_errors(),
102            rx_fifo_errors: buf.rx_fifo_errors(),
103            rx_missed_errors: buf.rx_missed_errors(),
104            tx_aborted_errors: buf.tx_aborted_errors(),
105            tx_carrier_errors: buf.tx_carrier_errors(),
106            tx_fifo_errors: buf.tx_fifo_errors(),
107            tx_heartbeat_errors: buf.tx_heartbeat_errors(),
108            tx_window_errors: buf.tx_window_errors(),
109            rx_compressed: buf.rx_compressed(),
110            tx_compressed: buf.tx_compressed(),
111            rx_nohandler: buf.rx_nohandler(),
112        })
113    }
114}
115
116impl Emitable for Stats {
117    fn buffer_len(&self) -> usize {
118        LINK_STATS_LEN
119    }
120
121    fn emit(&self, buffer: &mut [u8]) {
122        let mut buffer = StatsBuffer::new(buffer);
123        buffer.set_rx_packets(self.rx_packets);
124        buffer.set_tx_packets(self.tx_packets);
125        buffer.set_rx_bytes(self.rx_bytes);
126        buffer.set_tx_bytes(self.tx_bytes);
127        buffer.set_rx_errors(self.rx_errors);
128        buffer.set_tx_errors(self.tx_errors);
129        buffer.set_rx_dropped(self.rx_dropped);
130        buffer.set_tx_dropped(self.tx_dropped);
131        buffer.set_multicast(self.multicast);
132        buffer.set_collisions(self.collisions);
133        buffer.set_rx_length_errors(self.rx_length_errors);
134        buffer.set_rx_over_errors(self.rx_over_errors);
135        buffer.set_rx_crc_errors(self.rx_crc_errors);
136        buffer.set_rx_frame_errors(self.rx_frame_errors);
137        buffer.set_rx_fifo_errors(self.rx_fifo_errors);
138        buffer.set_rx_missed_errors(self.rx_missed_errors);
139        buffer.set_tx_aborted_errors(self.tx_aborted_errors);
140        buffer.set_tx_carrier_errors(self.tx_carrier_errors);
141        buffer.set_tx_fifo_errors(self.tx_fifo_errors);
142        buffer.set_tx_heartbeat_errors(self.tx_heartbeat_errors);
143        buffer.set_tx_window_errors(self.tx_window_errors);
144        buffer.set_rx_compressed(self.rx_compressed);
145        buffer.set_tx_compressed(self.tx_compressed);
146        buffer.set_rx_nohandler(self.rx_nohandler);
147    }
148}