netlink_packet_route/tc/actions/
mirror.rs1use netlink_packet_utils::{
10 DecodeError,
11 nla::{DefaultNla, Nla, NlaBuffer},
12 traits::{Emitable, Parseable},
13};
14
15use super::{TcActionGeneric, TcActionGenericBuffer};
16
17#[derive(Debug, PartialEq, Eq, Clone)]
19#[non_exhaustive]
20pub struct TcActionMirror {}
21impl TcActionMirror {
22 pub const KIND: &'static str = "mirred";
24}
25
26const TCA_MIRRED_TM: u16 = 1;
27const TCA_MIRRED_PARMS: u16 = 2;
28
29#[derive(Debug, PartialEq, Eq, Clone)]
31#[non_exhaustive]
32pub enum TcActionMirrorOption {
33 Tm(Vec<u8>),
35 Parms(TcMirror),
37 Other(DefaultNla),
39}
40
41impl Nla for TcActionMirrorOption {
42 fn value_len(&self) -> usize {
43 match self {
44 Self::Tm(bytes) => bytes.len(),
45 Self::Parms(_) => TC_MIRRED_BUF_LEN,
46 Self::Other(attr) => attr.value_len(),
47 }
48 }
49
50 fn emit_value(&self, buffer: &mut [u8]) {
51 match self {
52 Self::Tm(bytes) => buffer.copy_from_slice(bytes.as_slice()),
53 Self::Parms(p) => p.emit(buffer),
54 Self::Other(attr) => attr.emit_value(buffer),
55 }
56 }
57 fn kind(&self) -> u16 {
58 match self {
59 Self::Tm(_) => TCA_MIRRED_TM,
60 Self::Parms(_) => TCA_MIRRED_PARMS,
61 Self::Other(nla) => nla.kind(),
62 }
63 }
64}
65
66impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for TcActionMirrorOption {
67 type Error = DecodeError;
68 fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
69 let payload = buf.value();
70 Ok(match buf.kind() {
71 TCA_MIRRED_TM => Self::Tm(payload.to_vec()),
72 TCA_MIRRED_PARMS => Self::Parms(TcMirror::parse(&TcMirrorBuffer::new(payload)?)?),
73 _ => Self::Other(DefaultNla::parse(buf)?),
74 })
75 }
76}
77
78const TC_MIRRED_BUF_LEN: usize = TcActionGeneric::BUF_LEN + 8;
79
80#[derive(Debug, PartialEq, Eq, Clone, Default)]
82#[non_exhaustive]
83pub struct TcMirror {
84 pub generic: TcActionGeneric,
86 pub eaction: TcMirrorActionType,
88 pub ifindex: u32,
90}
91
92buffer!(TcMirrorBuffer(TC_MIRRED_BUF_LEN) {
94 generic: (slice, 0..20),
95 eaction: (i32, 20..24),
96 ifindex: (u32, 24..28),
97});
98
99impl Emitable for TcMirror {
100 fn buffer_len(&self) -> usize {
101 TC_MIRRED_BUF_LEN
102 }
103
104 fn emit(&self, buffer: &mut [u8]) {
105 let mut packet = TcMirrorBuffer::new_unchecked(buffer);
106 self.generic.emit(packet.generic_mut());
107 packet.set_eaction(self.eaction.into());
108 packet.set_ifindex(self.ifindex);
109 }
110}
111
112impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<TcMirrorBuffer<&'a T>> for TcMirror {
113 type Error = DecodeError;
114 fn parse(buf: &TcMirrorBuffer<&T>) -> Result<Self, DecodeError> {
115 Ok(Self {
116 generic: TcActionGeneric::parse(&TcActionGenericBuffer::new(buf.generic())?)?,
117 eaction: buf.eaction().into(),
118 ifindex: buf.ifindex(),
119 })
120 }
121}
122
123const TCA_EGRESS_REDIR: i32 = 1;
124const TCA_EGRESS_MIRROR: i32 = 2;
125const TCA_INGRESS_REDIR: i32 = 3;
126const TCA_INGRESS_MIRROR: i32 = 4;
127
128#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
130#[non_exhaustive]
131pub enum TcMirrorActionType {
132 #[default]
133 EgressRedir,
135 EgressMirror,
137 IngressRedir,
139 IngressMirror,
141 Other(i32),
143}
144
145impl From<i32> for TcMirrorActionType {
146 fn from(d: i32) -> Self {
147 match d {
148 TCA_EGRESS_REDIR => Self::EgressRedir,
149 TCA_EGRESS_MIRROR => Self::EgressMirror,
150 TCA_INGRESS_REDIR => Self::IngressRedir,
151 TCA_INGRESS_MIRROR => Self::IngressMirror,
152 _ => Self::Other(d),
153 }
154 }
155}
156
157impl From<TcMirrorActionType> for i32 {
158 fn from(v: TcMirrorActionType) -> i32 {
159 match v {
160 TcMirrorActionType::EgressRedir => TCA_EGRESS_REDIR,
161 TcMirrorActionType::EgressMirror => TCA_EGRESS_MIRROR,
162 TcMirrorActionType::IngressRedir => TCA_INGRESS_REDIR,
163 TcMirrorActionType::IngressMirror => TCA_INGRESS_MIRROR,
164 TcMirrorActionType::Other(d) => d,
165 }
166 }
167}