Skip to main content

vpp_plugin/vnet/
error.rs

1//! VPP networking errors
2
3use std::{
4    ffi::{CString, c_int},
5    fmt::Display,
6};
7
8use crate::bindings::{vnet_error, vnet_error_t};
9
10/// VPP networking error
11#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
12pub struct VnetError(vnet_error_t);
13
14impl VnetError {
15    /// Creates an error stack from the VPP networking error and adds context
16    ///
17    /// # Panics
18    ///
19    /// Panics if the context message contains nul characters.
20    pub fn context<M: Display>(self, message: M) -> crate::vppinfra::error::ErrorStack {
21        // SAFETY: pointers passed into vnet_error are valid and are valid nul-terminated C
22        // strings. vnet_error aborts on memory allocation error so cannot fail and therefore the
23        // returned pointer is always valid.
24        unsafe {
25            let message_c = CString::new(message.to_string())
26                .expect("message should not contain nul terminator");
27            let ptr = vnet_error(self.0, c"%s".as_ptr().cast_mut().cast(), message_c.as_ptr());
28            crate::vppinfra::error::ErrorStack::from_raw(ptr)
29        }
30    }
31}
32
33impl From<c_int> for VnetError {
34    fn from(value: c_int) -> Self {
35        Self(value)
36    }
37}
38
39impl From<VnetError> for i32 {
40    fn from(value: VnetError) -> Self {
41        value.0
42    }
43}
44
45/// Invalid Argument
46pub const VNET_ERR_INVALID_ARGUMENT: VnetError = VnetError(-73);