fidl_fuchsia_ebpf__common/
fidl_fuchsia_ebpf__common.rs

1// WARNING: This file is machine generated by fidlgen.
2
3#![warn(clippy::all)]
4#![allow(unused_parens, unused_mut, unused_imports, nonstandard_style)]
5
6use bitflags::bitflags;
7use fidl::encoding::{MessageBufFor, ProxyChannelBox, ResourceDialect};
8use futures::future::{self, MaybeDone, TryFutureExt};
9use zx_status;
10
11/// Maximum allowed number of instructions in an eBPF program.
12pub const MAX_PROGRAM_INSTRUCTIONS: u32 = 4096;
13
14/// Maximum number of maps an eBPF program can use.
15pub const MAX_PROGRAM_MAPS: u32 = 4096;
16
17/// Type of an eBPF maps.
18#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
19#[repr(u32)]
20pub enum MapType {
21    /// An array map. Equivalent to `BPF_MAP_TYPE_ARRAY`.
22    Array = 1,
23    /// A hash map. Equivalent to `BPF_MAP_TYPE_HASH`.
24    HashMap = 2,
25    /// A hash map. Equivalent to `BPF_MAP_TYPE_RINGBUF`.
26    RingBuffer = 3,
27    /// A per-CPU array. Equivalent to `BPF_MAP_TYPE_PERCPU_ARRAY`.
28    PercpuArray = 4,
29    /// A device-map that holds references to network devices. Equivalent to
30    /// `BPF_MAP_TYPE_DEVMAP`.
31    DevmapHash = 5,
32    /// A longest prefix match trie. Equivalent to `BPF_MAP_TYPE_LPM_TRIE`.
33    LpmTrie = 6,
34}
35
36impl MapType {
37    #[inline]
38    pub fn from_primitive(prim: u32) -> Option<Self> {
39        match prim {
40            1 => Some(Self::Array),
41            2 => Some(Self::HashMap),
42            3 => Some(Self::RingBuffer),
43            4 => Some(Self::PercpuArray),
44            5 => Some(Self::DevmapHash),
45            6 => Some(Self::LpmTrie),
46            _ => None,
47        }
48    }
49
50    #[inline]
51    pub const fn into_primitive(self) -> u32 {
52        self as u32
53    }
54}
55
56/// Schema of an eBPF map.
57#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
58pub struct MapSchema {
59    /// Map type.
60    pub type_: MapType,
61    /// Key size in bytes.
62    pub key_size: u32,
63    /// Value size in bytes.
64    pub value_size: u32,
65    /// Maximum number of entries in the map.
66    pub max_entries: u32,
67}
68
69impl fidl::Persistable for MapSchema {}
70
71/// Describes location of an instruction that accesses a struct field.
72///
73/// This is used as auxiliary information passed from the verifier to the
74/// linker. The linker may update these instructions.
75#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
76pub struct StructAccess {
77    /// Index of the instruction in the program.
78    pub pc: u32,
79    /// Id of the struct being accessed.
80    pub struct_memory_id: u64,
81    /// Offset of the field being accessed.
82    pub field_offset: u32,
83    /// Indicates that the instruction loads a 32-bit pointer field. These
84    /// loads must be remapped to 64-bit fields.
85    pub is_32_bit_ptr_load: bool,
86}
87
88impl fidl::Persistable for StructAccess {}
89
90mod internal {
91    use super::*;
92    unsafe impl fidl::encoding::TypeMarker for MapType {
93        type Owned = Self;
94
95        #[inline(always)]
96        fn inline_align(_context: fidl::encoding::Context) -> usize {
97            std::mem::align_of::<u32>()
98        }
99
100        #[inline(always)]
101        fn inline_size(_context: fidl::encoding::Context) -> usize {
102            std::mem::size_of::<u32>()
103        }
104
105        #[inline(always)]
106        fn encode_is_copy() -> bool {
107            true
108        }
109
110        #[inline(always)]
111        fn decode_is_copy() -> bool {
112            false
113        }
114    }
115
116    impl fidl::encoding::ValueTypeMarker for MapType {
117        type Borrowed<'a> = Self;
118        #[inline(always)]
119        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
120            *value
121        }
122    }
123
124    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<Self, D> for MapType {
125        #[inline]
126        unsafe fn encode(
127            self,
128            encoder: &mut fidl::encoding::Encoder<'_, D>,
129            offset: usize,
130            _depth: fidl::encoding::Depth,
131        ) -> fidl::Result<()> {
132            encoder.debug_check_bounds::<Self>(offset);
133            encoder.write_num(self.into_primitive(), offset);
134            Ok(())
135        }
136    }
137
138    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for MapType {
139        #[inline(always)]
140        fn new_empty() -> Self {
141            Self::Array
142        }
143
144        #[inline]
145        unsafe fn decode(
146            &mut self,
147            decoder: &mut fidl::encoding::Decoder<'_, D>,
148            offset: usize,
149            _depth: fidl::encoding::Depth,
150        ) -> fidl::Result<()> {
151            decoder.debug_check_bounds::<Self>(offset);
152            let prim = decoder.read_num::<u32>(offset);
153
154            *self = Self::from_primitive(prim).ok_or(fidl::Error::InvalidEnumValue)?;
155            Ok(())
156        }
157    }
158
159    impl fidl::encoding::ValueTypeMarker for MapSchema {
160        type Borrowed<'a> = &'a Self;
161        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
162            value
163        }
164    }
165
166    unsafe impl fidl::encoding::TypeMarker for MapSchema {
167        type Owned = Self;
168
169        #[inline(always)]
170        fn inline_align(_context: fidl::encoding::Context) -> usize {
171            4
172        }
173
174        #[inline(always)]
175        fn inline_size(_context: fidl::encoding::Context) -> usize {
176            16
177        }
178    }
179
180    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<MapSchema, D>
181        for &MapSchema
182    {
183        #[inline]
184        unsafe fn encode(
185            self,
186            encoder: &mut fidl::encoding::Encoder<'_, D>,
187            offset: usize,
188            _depth: fidl::encoding::Depth,
189        ) -> fidl::Result<()> {
190            encoder.debug_check_bounds::<MapSchema>(offset);
191            // Delegate to tuple encoding.
192            fidl::encoding::Encode::<MapSchema, D>::encode(
193                (
194                    <MapType as fidl::encoding::ValueTypeMarker>::borrow(&self.type_),
195                    <u32 as fidl::encoding::ValueTypeMarker>::borrow(&self.key_size),
196                    <u32 as fidl::encoding::ValueTypeMarker>::borrow(&self.value_size),
197                    <u32 as fidl::encoding::ValueTypeMarker>::borrow(&self.max_entries),
198                ),
199                encoder,
200                offset,
201                _depth,
202            )
203        }
204    }
205    unsafe impl<
206            D: fidl::encoding::ResourceDialect,
207            T0: fidl::encoding::Encode<MapType, D>,
208            T1: fidl::encoding::Encode<u32, D>,
209            T2: fidl::encoding::Encode<u32, D>,
210            T3: fidl::encoding::Encode<u32, D>,
211        > fidl::encoding::Encode<MapSchema, D> for (T0, T1, T2, T3)
212    {
213        #[inline]
214        unsafe fn encode(
215            self,
216            encoder: &mut fidl::encoding::Encoder<'_, D>,
217            offset: usize,
218            depth: fidl::encoding::Depth,
219        ) -> fidl::Result<()> {
220            encoder.debug_check_bounds::<MapSchema>(offset);
221            // Zero out padding regions. There's no need to apply masks
222            // because the unmasked parts will be overwritten by fields.
223            // Write the fields.
224            self.0.encode(encoder, offset + 0, depth)?;
225            self.1.encode(encoder, offset + 4, depth)?;
226            self.2.encode(encoder, offset + 8, depth)?;
227            self.3.encode(encoder, offset + 12, depth)?;
228            Ok(())
229        }
230    }
231
232    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for MapSchema {
233        #[inline(always)]
234        fn new_empty() -> Self {
235            Self {
236                type_: fidl::new_empty!(MapType, D),
237                key_size: fidl::new_empty!(u32, D),
238                value_size: fidl::new_empty!(u32, D),
239                max_entries: fidl::new_empty!(u32, D),
240            }
241        }
242
243        #[inline]
244        unsafe fn decode(
245            &mut self,
246            decoder: &mut fidl::encoding::Decoder<'_, D>,
247            offset: usize,
248            _depth: fidl::encoding::Depth,
249        ) -> fidl::Result<()> {
250            decoder.debug_check_bounds::<Self>(offset);
251            // Verify that padding bytes are zero.
252            fidl::decode!(MapType, D, &mut self.type_, decoder, offset + 0, _depth)?;
253            fidl::decode!(u32, D, &mut self.key_size, decoder, offset + 4, _depth)?;
254            fidl::decode!(u32, D, &mut self.value_size, decoder, offset + 8, _depth)?;
255            fidl::decode!(u32, D, &mut self.max_entries, decoder, offset + 12, _depth)?;
256            Ok(())
257        }
258    }
259
260    impl fidl::encoding::ValueTypeMarker for StructAccess {
261        type Borrowed<'a> = &'a Self;
262        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
263            value
264        }
265    }
266
267    unsafe impl fidl::encoding::TypeMarker for StructAccess {
268        type Owned = Self;
269
270        #[inline(always)]
271        fn inline_align(_context: fidl::encoding::Context) -> usize {
272            8
273        }
274
275        #[inline(always)]
276        fn inline_size(_context: fidl::encoding::Context) -> usize {
277            24
278        }
279    }
280
281    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<StructAccess, D>
282        for &StructAccess
283    {
284        #[inline]
285        unsafe fn encode(
286            self,
287            encoder: &mut fidl::encoding::Encoder<'_, D>,
288            offset: usize,
289            _depth: fidl::encoding::Depth,
290        ) -> fidl::Result<()> {
291            encoder.debug_check_bounds::<StructAccess>(offset);
292            // Delegate to tuple encoding.
293            fidl::encoding::Encode::<StructAccess, D>::encode(
294                (
295                    <u32 as fidl::encoding::ValueTypeMarker>::borrow(&self.pc),
296                    <u64 as fidl::encoding::ValueTypeMarker>::borrow(&self.struct_memory_id),
297                    <u32 as fidl::encoding::ValueTypeMarker>::borrow(&self.field_offset),
298                    <bool as fidl::encoding::ValueTypeMarker>::borrow(&self.is_32_bit_ptr_load),
299                ),
300                encoder,
301                offset,
302                _depth,
303            )
304        }
305    }
306    unsafe impl<
307            D: fidl::encoding::ResourceDialect,
308            T0: fidl::encoding::Encode<u32, D>,
309            T1: fidl::encoding::Encode<u64, D>,
310            T2: fidl::encoding::Encode<u32, D>,
311            T3: fidl::encoding::Encode<bool, D>,
312        > fidl::encoding::Encode<StructAccess, D> for (T0, T1, T2, T3)
313    {
314        #[inline]
315        unsafe fn encode(
316            self,
317            encoder: &mut fidl::encoding::Encoder<'_, D>,
318            offset: usize,
319            depth: fidl::encoding::Depth,
320        ) -> fidl::Result<()> {
321            encoder.debug_check_bounds::<StructAccess>(offset);
322            // Zero out padding regions. There's no need to apply masks
323            // because the unmasked parts will be overwritten by fields.
324            unsafe {
325                let ptr = encoder.buf.as_mut_ptr().add(offset).offset(0);
326                (ptr as *mut u64).write_unaligned(0);
327            }
328            unsafe {
329                let ptr = encoder.buf.as_mut_ptr().add(offset).offset(16);
330                (ptr as *mut u64).write_unaligned(0);
331            }
332            // Write the fields.
333            self.0.encode(encoder, offset + 0, depth)?;
334            self.1.encode(encoder, offset + 8, depth)?;
335            self.2.encode(encoder, offset + 16, depth)?;
336            self.3.encode(encoder, offset + 20, depth)?;
337            Ok(())
338        }
339    }
340
341    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for StructAccess {
342        #[inline(always)]
343        fn new_empty() -> Self {
344            Self {
345                pc: fidl::new_empty!(u32, D),
346                struct_memory_id: fidl::new_empty!(u64, D),
347                field_offset: fidl::new_empty!(u32, D),
348                is_32_bit_ptr_load: fidl::new_empty!(bool, D),
349            }
350        }
351
352        #[inline]
353        unsafe fn decode(
354            &mut self,
355            decoder: &mut fidl::encoding::Decoder<'_, D>,
356            offset: usize,
357            _depth: fidl::encoding::Depth,
358        ) -> fidl::Result<()> {
359            decoder.debug_check_bounds::<Self>(offset);
360            // Verify that padding bytes are zero.
361            let ptr = unsafe { decoder.buf.as_ptr().add(offset).offset(0) };
362            let padval = unsafe { (ptr as *const u64).read_unaligned() };
363            let mask = 0xffffffff00000000u64;
364            let maskedval = padval & mask;
365            if maskedval != 0 {
366                return Err(fidl::Error::NonZeroPadding {
367                    padding_start: offset + 0 + ((mask as u64).trailing_zeros() / 8) as usize,
368                });
369            }
370            let ptr = unsafe { decoder.buf.as_ptr().add(offset).offset(16) };
371            let padval = unsafe { (ptr as *const u64).read_unaligned() };
372            let mask = 0xffffff0000000000u64;
373            let maskedval = padval & mask;
374            if maskedval != 0 {
375                return Err(fidl::Error::NonZeroPadding {
376                    padding_start: offset + 16 + ((mask as u64).trailing_zeros() / 8) as usize,
377                });
378            }
379            fidl::decode!(u32, D, &mut self.pc, decoder, offset + 0, _depth)?;
380            fidl::decode!(u64, D, &mut self.struct_memory_id, decoder, offset + 8, _depth)?;
381            fidl::decode!(u32, D, &mut self.field_offset, decoder, offset + 16, _depth)?;
382            fidl::decode!(bool, D, &mut self.is_32_bit_ptr_load, decoder, offset + 20, _depth)?;
383            Ok(())
384        }
385    }
386}