Skip to main content

vpp_plugin/vnet/
buffer.rs

1#![allow(missing_docs)]
2
3//! VNET buffer flags
4
5use bitflags::bitflags;
6
7use crate::{
8    bindings::{
9        VNET_BUFFER_F_AVAIL1, VNET_BUFFER_F_AVAIL2, VNET_BUFFER_F_AVAIL3, VNET_BUFFER_F_AVAIL4,
10        VNET_BUFFER_F_AVAIL5, VNET_BUFFER_F_AVAIL6, VNET_BUFFER_F_AVAIL7, VNET_BUFFER_F_AVAIL8,
11        VNET_BUFFER_F_AVAIL9, VNET_BUFFER_F_FLOW_REPORT, VNET_BUFFER_F_GSO, VNET_BUFFER_F_IS_DVR,
12        VNET_BUFFER_F_IS_IP4, VNET_BUFFER_F_IS_IP6, VNET_BUFFER_F_IS_NATED,
13        VNET_BUFFER_F_L2_HDR_OFFSET_VALID, VNET_BUFFER_F_L3_HDR_OFFSET_VALID,
14        VNET_BUFFER_F_L4_CHECKSUM_COMPUTED, VNET_BUFFER_F_L4_CHECKSUM_CORRECT,
15        VNET_BUFFER_F_L4_HDR_OFFSET_VALID, VNET_BUFFER_F_LOCALLY_ORIGINATED,
16        VNET_BUFFER_F_LOOP_COUNTER_VALID, VNET_BUFFER_F_OFFLOAD, VNET_BUFFER_F_QOS_DATA_VALID,
17        VNET_BUFFER_F_SPAN_CLONE, VNET_BUFFER_F_VLAN_1_DEEP, VNET_BUFFER_F_VLAN_2_DEEP,
18        feature_main, vlib_rx_or_tx_t_VLIB_RX, vlib_rx_or_tx_t_VLIB_TX, vnet_buffer_opaque_t,
19    },
20    vnet::types::SwIfIndex,
21};
22
23bitflags! {
24    /// VNET buffer flags
25    pub struct BufferFlags: u32 {
26        const L4_CHECKSUM_COMPUTED = VNET_BUFFER_F_L4_CHECKSUM_COMPUTED as u32;
27        const L4_CHECKSUM_CORRECT = VNET_BUFFER_F_L4_CHECKSUM_CORRECT as u32;
28        const VLAN_2_DEEP = VNET_BUFFER_F_VLAN_2_DEEP as u32;
29        const VLAN_1_DEEP = VNET_BUFFER_F_VLAN_1_DEEP as u32;
30        const SPAN_CLONE = VNET_BUFFER_F_SPAN_CLONE as u32;
31        const LOOP_COUNTER_VALID = VNET_BUFFER_F_LOOP_COUNTER_VALID as u32;
32        const LOCALLY_ORIGINATED = VNET_BUFFER_F_LOCALLY_ORIGINATED as u32;
33        const IS_IP4 = VNET_BUFFER_F_IS_IP4 as u32;
34        const IS_IP6 = VNET_BUFFER_F_IS_IP6 as u32;
35        const OFFLOAD = VNET_BUFFER_F_OFFLOAD as u32;
36        const IS_NATED = VNET_BUFFER_F_IS_NATED as u32;
37        const L2_HDR_OFFSET_VALID = VNET_BUFFER_F_L2_HDR_OFFSET_VALID as u32;
38        const L3_HDR_OFFSET_VALID = VNET_BUFFER_F_L3_HDR_OFFSET_VALID as u32;
39        const L4_HDR_OFFSET_VALID = VNET_BUFFER_F_L4_HDR_OFFSET_VALID as u32;
40        const FLOW_REPORT = VNET_BUFFER_F_FLOW_REPORT as u32;
41        const IS_DVR = VNET_BUFFER_F_IS_DVR as u32;
42        const QOS_DATA_VALID = VNET_BUFFER_F_QOS_DATA_VALID as u32;
43        const GSO = VNET_BUFFER_F_GSO as u32;
44        const AVAIL1 = VNET_BUFFER_F_AVAIL1 as u32;
45        const AVAIL2 = VNET_BUFFER_F_AVAIL2 as u32;
46        const AVAIL3 = VNET_BUFFER_F_AVAIL3 as u32;
47        const AVAIL4 = VNET_BUFFER_F_AVAIL4 as u32;
48        const AVAIL5 = VNET_BUFFER_F_AVAIL5 as u32;
49        const AVAIL6 = VNET_BUFFER_F_AVAIL6 as u32;
50        const AVAIL7 = VNET_BUFFER_F_AVAIL7 as u32;
51        const AVAIL8 = VNET_BUFFER_F_AVAIL8 as u32;
52        const AVAIL9 = VNET_BUFFER_F_AVAIL9 as u32;
53
54        // vlib flags not represented here
55        const _ = !0;
56    }
57}
58
59impl BufferFlags {
60    /// Get the VLIB buffer flags from the VNET buffer flags
61    pub fn as_vlib_flags(&self) -> crate::vlib::buffer::BufferFlags {
62        crate::vlib::buffer::BufferFlags::from_bits_retain(self.bits())
63    }
64}
65
66impl crate::vlib::buffer::BufferFlags {
67    /// Get the VNET buffer flags from the VLIB buffer flags
68    pub fn vnet_flags(&self) -> BufferFlags {
69        BufferFlags::from_bits_retain(self.bits())
70    }
71}
72
73/// Reference to a VNET buffer
74///
75/// A `&mut BufferRef` is equivalent to a `vnet_buffer_opaque_t *` in C (a `*vnet_buffer_opaque_t`
76/// in Rust).
77#[repr(transparent)]
78pub struct BufferRef(foreign_types::Opaque);
79
80impl BufferRef {
81    /// Create a `&BufferRef` from a raw pointer
82    ///
83    /// # Safety
84    ///
85    /// - The pointer must be a valid and properly initialised `vlib_buffer_t`.
86    /// - The pointer must stay valid and the contents must not be mutated for the duration of the
87    ///   lifetime of the returned object.
88    #[inline(always)]
89    pub unsafe fn from_ptr<'a>(ptr: *const vnet_buffer_opaque_t) -> &'a BufferRef {
90        // SAFETY: The safety requirements are documented in the function's safety comment.
91        unsafe { &*(ptr as *mut _) }
92    }
93
94    /// Create a `&mut BufferRef` from a raw pointer
95    ///
96    /// # Safety
97    ///
98    /// - The pointer must be a valid and properly initialised `vlib_buffer_t`.
99    /// - The pointer must stay valid and the contents must not be mutated for the duration of the
100    ///   lifetime of the returned object.
101    #[inline(always)]
102    pub unsafe fn from_ptr_mut<'a>(ptr: *mut vnet_buffer_opaque_t) -> &'a mut BufferRef {
103        // SAFETY: The safety requirements are documented in the function's safety comment.
104        unsafe { &mut *(ptr as *mut _) }
105    }
106
107    /// Returns the raw pointer to the underlying `vnet_buffer_opaque_t`
108    #[inline(always)]
109    pub fn as_ptr(&self) -> *mut vnet_buffer_opaque_t {
110        self as *const _ as *mut _
111    }
112
113    /// Returns the index of the receive software interface
114    pub fn rx_sw_if_index(&self) -> SwIfIndex {
115        // SAFETY: since the reference to self is valid, so must be the pointer
116        SwIfIndex::new(unsafe { (*self.as_ptr()).sw_if_index[vlib_rx_or_tx_t_VLIB_RX as usize] })
117    }
118
119    /// Set the index of the receive software interface
120    pub fn set_rx_sw_if_index(&mut self, sw_if_index: SwIfIndex) {
121        // SAFETY: since the reference to self is valid, so must be the pointer
122        unsafe {
123            (*self.as_ptr()).sw_if_index[vlib_rx_or_tx_t_VLIB_TX as usize] = sw_if_index.into()
124        };
125    }
126
127    /// Returns the index of the transmit software interface
128    pub fn tx_sw_if_index(&self) -> Option<SwIfIndex> {
129        // SAFETY: since the reference to self is valid, so must be the pointer
130        let sw_if_index = unsafe { (*self.as_ptr()).sw_if_index[vlib_rx_or_tx_t_VLIB_TX as usize] };
131        if sw_if_index == u32::MAX {
132            None
133        } else {
134            Some(sw_if_index.into())
135        }
136    }
137
138    /// Set the index of the transmit software interface
139    ///
140    /// If `sw_if_index` is `None` then it will be set to [`u32::MAX`], which indicates to
141    /// various nodes to use the interface from forwarding lookups.
142    pub fn set_tx_sw_if_index(&mut self, sw_if_index: Option<SwIfIndex>) {
143        let sw_if_index = sw_if_index.map(u32::from).unwrap_or(u32::MAX);
144        // SAFETY: since the reference to self is valid, so must be the pointer
145        unsafe { (*self.as_ptr()).sw_if_index[vlib_rx_or_tx_t_VLIB_TX as usize] = sw_if_index };
146    }
147}
148
149impl<FeatureData> crate::vlib::BufferRef<FeatureData> {
150    pub fn vnet_buffer(&self) -> &BufferRef {
151        let ptr = &self.as_metadata().opaque as *const _;
152        // SAFETY: ptr is valid since reference to self is valid, and the representation is a
153        // valid `vnet_buffer_opaque_t`
154        unsafe { BufferRef::from_ptr(ptr as *const vnet_buffer_opaque_t) }
155    }
156
157    pub fn vnet_buffer_mut(&mut self) -> &mut BufferRef {
158        let ptr = &mut self.as_metadata_mut().opaque as *mut _;
159        // SAFETY: ptr is valid since reference to self is valid, and the representation is a
160        // valid `vnet_buffer_opaque_t`
161        unsafe { BufferRef::from_ptr_mut(ptr as *mut vnet_buffer_opaque_t) }
162    }
163}
164
165/// Returns VNET config data for the given config index
166///
167/// In the process, the config index is advanced to the next one.
168///
169/// # Safety
170///
171/// - `config_string_heap` must be a valid pointer to a contiguous heap of `u32` values.
172/// - `*config_index` must be a valid index into the heap: `index + (size_of::<FeatureData>() /
173///   size_of::<u32>()) + 1 <= heap_length` must be satisfied, where `heap_length` is the
174///   number of `u32` elements in the heap. Additionally, the memory at
175///   `(*cm).config_string_heap.add(index)` must be properly aligned for `FeatureData` and
176///   contain valid data of that type, followed by a valid `u32` next-index value.
177/// - `FeatureData` must exactly match the type that was stored at this config index during
178///   configuration creation, including any alignment and representation requirements.
179#[inline(always)]
180unsafe fn vnet_get_config_data<FeatureData: Copy>(
181    config_string_heap: *const u32,
182    config_index: &mut u32,
183) -> (u32, FeatureData) {
184    // SAFETY: function preconditions mean that this pointer arithmetic is valid and matches what
185    // VPP expects
186    unsafe {
187        let index = *config_index;
188
189        let d = config_string_heap.add(index as usize);
190
191        let n = std::mem::size_of::<FeatureData>().div_ceil(std::mem::size_of_val(&*d));
192
193        // The last u32 is the next index
194        let next = *d.add(n);
195
196        // Advance config index to next config
197        *config_index = index + n as u32 + 1;
198
199        (next, *(d as *const FeatureData))
200    }
201}
202
203impl<FeatureData: Copy> crate::vlib::BufferRef<FeatureData> {
204    /// Get the next feature node and feature data for this buffer
205    ///
206    /// Used when continuing to the next feature node in a node invoked from a feature arc.
207    ///
208    /// # Safety
209    ///
210    /// Must only be used from nodes when invoked from a feature arc.
211    #[inline(always)]
212    pub unsafe fn vnet_feature_next(&mut self) -> (u32, FeatureData) {
213        // SAFETY: method precondition means that `current_config_index` is a valid index into
214        // `feature_main.shared_feature_config_heap`.
215        // Access to `feature_main.shared_feature_config_heap` is safe without locking because VPP only
216        // modifies this during init, before any buffers are allocated.
217        unsafe {
218            vnet_get_config_data(
219                feature_main.shared_feature_config_heap,
220                self.current_config_index_mut(),
221            )
222        }
223    }
224
225    /// The buffer's feature config index: its position in its arc's feature config string.
226    ///
227    /// Two buffers with equal indices on the same arc are at the same point of the same config
228    /// string, so [`Self::vnet_feature_next`] yields the same next node and the same advanced
229    /// index for both.
230    #[inline(always)]
231    pub fn current_config_index(&self) -> u32 {
232        // SAFETY: plain u32 read of an initialised metadata field of a valid buffer
233        unsafe { self.as_metadata().__bindgen_anon_1.current_config_index }
234    }
235
236    /// Get a mutable reference to the current config index.
237    ///
238    /// See [`Self::current_config_index`].
239    #[inline(always)]
240    pub fn current_config_index_mut(&mut self) -> &mut u32 {
241        // SAFETY: reads through the reference are sound because it's from initialised metadata
242        // field of a valid buffer. Writes through the reference are sound because both members
243        // are u32 and so no value written to the reference can be invalid for the other member.
244        unsafe { &mut self.as_metadata_mut().__bindgen_anon_1.current_config_index }
245    }
246}