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