1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-License-Identifier: MIT

use netlink_packet_utils::{
    nla::{NlaBuffer, NlasIterator},
    traits::{Emitable, Parseable},
    DecodeError,
};

use crate::AddressFamily;

const TC_HEADER_LEN: usize = 20;

buffer!(TcMessageBuffer(TC_HEADER_LEN) {
    family: (u8, 0),
    pad1: (u8, 1),
    pad2: (u16, 2..4),
    index: (i32, 4..8),
    handle: (u32, 8..12),
    parent: (u32, 12..16),
    info: (u32, 16..TC_HEADER_LEN),
    payload: (slice, TC_HEADER_LEN..),
});

impl<'a, T: AsRef<[u8]> + ?Sized> TcMessageBuffer<&'a T> {
    pub fn attributes(
        &self,
    ) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, DecodeError>> {
        NlasIterator::new(self.payload())
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct TcHeader {
    pub family: AddressFamily,
    // Interface index
    pub index: i32,
    // Qdisc handle
    pub handle: TcHandle,
    // Parent Qdisc
    pub parent: TcHandle,
    pub info: u32,
}

impl TcHeader {
    pub const TCM_IFINDEX_MAGIC_BLOCK: u32 = 0xFFFFFFFF;
}

impl Emitable for TcHeader {
    fn buffer_len(&self) -> usize {
        TC_HEADER_LEN
    }

    fn emit(&self, buffer: &mut [u8]) {
        let mut packet = TcMessageBuffer::new(buffer);
        packet.set_family(self.family.into());
        packet.set_index(self.index);
        packet.set_handle(self.handle.into());
        packet.set_parent(self.parent.into());
        packet.set_info(self.info);
    }
}

impl<T: AsRef<[u8]>> Parseable<TcMessageBuffer<T>> for TcHeader {
    fn parse(buf: &TcMessageBuffer<T>) -> Result<Self, DecodeError> {
        Ok(Self {
            family: buf.family().into(),
            index: buf.index(),
            handle: buf.handle().into(),
            parent: buf.parent().into(),
            info: buf.info(),
        })
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct TcHandle {
    pub major: u16,
    pub minor: u16,
}

impl TcHandle {
    pub const UNSPEC: Self = Self { major: 0, minor: 0 };
    pub const ROOT: Self = Self {
        major: u16::MAX,
        minor: u16::MAX,
    };
    pub const INGRESS: Self = Self {
        major: u16::MAX,
        minor: 0xfff1,
    };

    pub const CLSACT: Self = Self::INGRESS;

    pub const MIN_PRIORITY: u16 = 0xFFE0;
    pub const MIN_INGRESS: u16 = 0xFFF2;
    pub const MIN_EGRESS: u16 = 0xFFF3;
}

impl From<u32> for TcHandle {
    fn from(d: u32) -> Self {
        let bytes = d.to_be_bytes();
        Self {
            major: u16::from_be_bytes([bytes[0], bytes[1]]),
            minor: u16::from_be_bytes([bytes[2], bytes[3]]),
        }
    }
}

impl From<TcHandle> for u32 {
    fn from(v: TcHandle) -> u32 {
        let major_bytes = v.major.to_be_bytes();
        let minor_bytes = v.minor.to_be_bytes();
        u32::from_be_bytes([
            major_bytes[0],
            major_bytes[1],
            minor_bytes[0],
            minor_bytes[1],
        ])
    }
}

impl std::fmt::Display for TcHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.major, self.minor)
    }
}