netlink_packet_route/tc/stats/
compat.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_core::{DecodeError, Emitable, Parseable};
4
5/// Generic queue statistics
6#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
7#[non_exhaustive]
8pub struct TcStats {
9    /// Number of enqueued bytes
10    pub bytes: u64,
11    /// Number of enqueued packets
12    pub packets: u32,
13    /// Packets dropped because of lack of resources
14    pub drops: u32,
15    /// Number of throttle events when this flow goes out of allocated
16    /// bandwidth
17    pub overlimits: u32,
18    /// Current flow byte rate
19    pub bps: u32,
20    /// Current flow packet rate
21    pub pps: u32,
22    pub qlen: u32,
23    pub backlog: u32,
24}
25
26// real size is 36, but kernel is align to 64bits(8 bytes)
27const STATS_LEN: usize = 40;
28
29buffer!(TcStatsBuffer(STATS_LEN) {
30    bytes: (u64, 0..8),
31    packets: (u32, 8..12),
32    drops: (u32, 12..16),
33    overlimits: (u32, 16..20),
34    bps: (u32, 20..24),
35    pps: (u32, 24..28),
36    qlen: (u32, 28..32),
37    backlog: (u32, 32..36),
38});
39
40impl<T: AsRef<[u8]>> Parseable<TcStatsBuffer<T>> for TcStats {
41    fn parse(buf: &TcStatsBuffer<T>) -> Result<Self, DecodeError> {
42        Ok(Self {
43            bytes: buf.bytes(),
44            packets: buf.packets(),
45            drops: buf.drops(),
46            overlimits: buf.overlimits(),
47            bps: buf.bps(),
48            pps: buf.pps(),
49            qlen: buf.qlen(),
50            backlog: buf.backlog(),
51        })
52    }
53}
54
55impl Emitable for TcStats {
56    fn buffer_len(&self) -> usize {
57        STATS_LEN
58    }
59
60    fn emit(&self, buffer: &mut [u8]) {
61        let mut buffer = TcStatsBuffer::new(buffer);
62        buffer.set_bytes(self.bytes);
63        buffer.set_packets(self.packets);
64        buffer.set_drops(self.drops);
65        buffer.set_overlimits(self.overlimits);
66        buffer.set_bps(self.bps);
67        buffer.set_pps(self.pps);
68        buffer.set_qlen(self.qlen);
69        buffer.set_backlog(self.backlog);
70    }
71}