netlink_packet_route/tc/stats/
compat.rs

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