Skip to main content

iris_core/dpdk/
mod.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(unused)]
5// Bindgen generates functions with u128 return types
6#![allow(improper_ctypes)]
7
8include!(concat!(env!("OUT_DIR"), "/dpdk.rs"));
9
10use std::os::raw::{c_char, c_int, c_uint, c_void};
11
12#[cfg(dpdk_ge_2311)]
13impl std::fmt::Debug for rte_gtp_psc_generic_hdr {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        f.debug_struct("rte_gtp_psc_generic_hdr")
16            .field("ext_hdr_len", &self.ext_hdr_len)
17            .field("_bitfield_align_1", &self._bitfield_align_1)
18            .field("_bitfield_1", &self._bitfield_1)
19            .field("data", &self.data)
20            .finish()
21    }
22}
23
24impl rte_ipv4_hdr {
25    #[cfg(not(dpdk_ge_2311))]
26    pub fn set_version_ihl(&mut self, version_ihl: u8) {
27        self.version_ihl = version_ihl;
28    }
29
30    #[cfg(dpdk_ge_2311)]
31    pub fn set_version_ihl(&mut self, version_ihl: u8) {
32        self.__bindgen_anon_1.version_ihl = version_ihl;
33    }
34}
35
36#[cfg(not(dpdk_ge_2411))]
37impl rte_ipv6_hdr {
38    pub fn set_vtc_flow(&mut self, vtc_flow: u32) {
39        self.vtc_flow = vtc_flow;
40    }
41
42    pub fn set_src_addr(&mut self, src_addr: [u8; 16]) {
43        self.src_addr = src_addr;
44    }
45
46    pub fn set_dst_addr(&mut self, dst_addr: [u8; 16]) {
47        self.dst_addr = dst_addr;
48    }
49}
50
51#[cfg(dpdk_ge_2411)]
52impl rte_ipv6_hdr {
53    pub fn set_vtc_flow(&mut self, vtc_flow: u32) {
54        self.__bindgen_anon_1.vtc_flow = vtc_flow;
55    }
56
57    pub fn set_src_addr(&mut self, src_addr: [u8; 16]) {
58        self.src_addr = rte_ipv6_addr { a: src_addr };
59    }
60
61    pub fn set_dst_addr(&mut self, dst_addr: [u8; 16]) {
62        self.dst_addr = rte_ipv6_addr { a: dst_addr };
63    }
64}
65
66#[cfg(not(dpdk_ge_2411))]
67impl rte_mbuf {
68    pub fn get_buf_len(&self) -> usize {
69        unsafe { self.buf_len }.into()
70    }
71
72    pub fn get_data_len(&self) -> u16 {
73        unsafe { self.data_len }
74    }
75
76    pub fn get_pkt_len(&self) -> u32 {
77        unsafe { self.pkt_len }
78    }
79
80    pub fn get_data_off(&self) -> u16 {
81        unsafe { self.data_off }
82    }
83
84    pub fn get_rss_hash(&self) -> u32 {
85        unsafe { self.__bindgen_anon_2.hash.rss }
86    }
87
88    pub fn get_mark(&self) -> u32 {
89        unsafe { self.__bindgen_anon_2.hash.fdir.hi }
90    }
91
92    pub fn set_data_len(&mut self, data_len: u16) {
93        self.data_len = data_len
94    }
95
96    pub fn set_pkt_len(&mut self, pkt_len: u32) {
97        self.pkt_len = pkt_len
98    }
99
100    pub fn set_mark(&mut self, mark: u32) {
101        self.__bindgen_anon_2.hash.fdir.hi = mark
102    }
103}
104
105#[cfg(dpdk_ge_2411)]
106impl rte_mbuf {
107    pub fn get_buf_len(&self) -> usize {
108        unsafe { self.__bindgen_anon_2.__bindgen_anon_1.buf_len }.into()
109    }
110
111    pub fn get_data_len(&self) -> u16 {
112        unsafe { self.__bindgen_anon_2.__bindgen_anon_1.data_len }
113    }
114
115    pub fn get_pkt_len(&self) -> u32 {
116        unsafe { self.__bindgen_anon_2.__bindgen_anon_1.pkt_len }
117    }
118
119    pub fn get_data_off(&self) -> u16 {
120        unsafe { self.__bindgen_anon_1.__bindgen_anon_1.data_off }
121    }
122
123    pub fn get_rss_hash(&self) -> u32 {
124        unsafe {
125            self.__bindgen_anon_2
126                .__bindgen_anon_1
127                .__bindgen_anon_2
128                .hash
129                .rss
130        }
131    }
132
133    pub fn get_mark(&self) -> u32 {
134        unsafe {
135            self.__bindgen_anon_2
136                .__bindgen_anon_1
137                .__bindgen_anon_2
138                .hash
139                .fdir
140                .hi
141        }
142    }
143
144    pub fn set_data_len(&mut self, data_len: u16) {
145        self.__bindgen_anon_2.__bindgen_anon_1.data_len = data_len
146    }
147
148    pub fn set_pkt_len(&mut self, pkt_len: u32) {
149        self.__bindgen_anon_2.__bindgen_anon_1.pkt_len = pkt_len
150    }
151
152    pub fn set_mark(&mut self, mark: u32) {
153        self.__bindgen_anon_2
154            .__bindgen_anon_1
155            .__bindgen_anon_2
156            .hash
157            .fdir
158            .hi = mark
159    }
160}
161
162/*
163 * DPDK v23.11 uses RTE_BIT64(x) macro to define RSS values, so we use
164 * bindgen clang_macro_fallback to access them.
165 * There may be a bug in clang_macro_fallback impacting values >u32_max:
166 * https://github.com/rust-lang/rust-bindgen/issues/2944
167 * This is okay for the RSS values we use here, but it should be consulted
168 * before adding new RSS offload types.
169 */
170#[cfg(dpdk_ge_2311)]
171pub use rte_eth_fc_mode_RTE_ETH_FC_NONE as rte_eth_fc_mode_RTE_FC_NONE;
172#[cfg(dpdk_ge_2311)]
173pub use rte_eth_rx_mq_mode_RTE_ETH_MQ_RX_RSS as rte_eth_rx_mq_mode_ETH_MQ_RX_RSS;
174#[cfg(dpdk_ge_2311)]
175pub use RTE_ETH_RETA_GROUP_SIZE as RTE_RETA_GROUP_SIZE;
176#[cfg(dpdk_ge_2311)]
177pub use RTE_ETH_RSS_IP as ETH_RSS_IP;
178#[cfg(dpdk_ge_2311)]
179pub use RTE_ETH_RSS_TCP as ETH_RSS_TCP;
180#[cfg(dpdk_ge_2311)]
181pub use RTE_ETH_RSS_UDP as ETH_RSS_UDP;
182#[cfg(dpdk_ge_2311)]
183pub use RTE_ETH_VLAN_STRIP_OFFLOAD as DEV_RX_OFFLOAD_VLAN_STRIP;
184
185#[link(name = "inlined")]
186extern "C" {
187    fn rte_pktmbuf_free_(packet: *const rte_mbuf);
188    fn rte_pktmbuf_alloc_(mp: *mut rte_mempool) -> *mut rte_mbuf;
189    fn rte_eth_tx_burst_(
190        port_id: u16,
191        queue_id: u16,
192        tx_pkts: *mut *mut rte_mbuf,
193        nb_pkts: u16,
194    ) -> u16;
195    fn rte_eth_rx_burst_(
196        port_id: u16,
197        queue_id: u16,
198        rx_pkts: *mut *mut rte_mbuf,
199        nb_pkts: u16,
200    ) -> u16;
201    fn rte_mbuf_refcnt_read_(m: *const rte_mbuf) -> u16;
202    fn rte_mbuf_refcnt_update_(m: *mut rte_mbuf, value: i16) -> u16;
203    fn rte_mbuf_refcnt_set_(m: *mut rte_mbuf, value: i16);
204    fn rte_pktmbuf_adj_(packet: *mut rte_mbuf, len: u16) -> *mut c_char;
205    fn rte_pktmbuf_trim_(packet: *mut rte_mbuf, len: u16) -> c_int;
206    fn rte_lcore_id_() -> u16;
207    fn rte_rdtsc_() -> u64;
208    fn rte_ring_enqueue_(ring: *mut rte_ring, obj: *mut c_void) -> c_int;
209    fn rte_ring_sp_enqueue_(ring: *mut rte_ring, obj: *mut c_void) -> c_int;
210    fn rte_ring_mp_enqueue_(ring: *mut rte_ring, obj: *mut c_void) -> c_int;
211    fn rte_ring_dequeue_(ring: *mut rte_ring, obj_p: *mut *mut c_void) -> c_int;
212    fn rte_ring_sc_dequeue_(ring: *mut rte_ring, obj_p: *mut *mut c_void) -> c_int;
213    fn rte_ring_mc_dequeue_(ring: *mut rte_ring, obj_p: *mut *mut c_void) -> c_int;
214    fn rte_ring_count_(ring: *const rte_ring) -> c_uint;
215    fn rte_ring_free_count_(ring: *const rte_ring) -> c_uint;
216    fn rte_ring_full_(ring: *const rte_ring) -> c_int;
217    fn rte_ring_empty_(ring: *const rte_ring) -> c_int;
218    fn rte_ring_get_size_(ring: *const rte_ring) -> c_uint;
219    fn rte_ring_get_capacity_(ring: *const rte_ring) -> c_uint;
220}
221
222#[cfg(feature = "mlx5")]
223#[link(name = "rte_net_mlx5")]
224extern "C" {
225    fn rte_pmd_mlx5_get_dyn_flag_names();
226}
227
228#[cfg(feature = "mlx5")]
229#[inline(never)]
230pub fn load_drivers() {
231    if std::env::var("DONT_SET_THIS").is_ok() {
232        unsafe {
233            rte_pmd_mlx5_get_dyn_flag_names();
234        }
235    }
236}
237
238#[cfg(not(feature = "mlx5"))]
239#[inline(never)]
240pub fn load_drivers() {}
241
242#[inline]
243pub unsafe fn rte_pktmbuf_free(packet: *const rte_mbuf) {
244    rte_pktmbuf_free_(packet)
245}
246
247#[inline]
248pub unsafe fn rte_pktmbuf_alloc(mp: *mut rte_mempool) -> *mut rte_mbuf {
249    rte_pktmbuf_alloc_(mp)
250}
251
252#[inline]
253pub unsafe fn rte_eth_tx_burst(
254    port_id: u16,
255    queue_id: u16,
256    tx_pkts: *mut *mut rte_mbuf,
257    nb_pkts: u16,
258) -> u16 {
259    rte_eth_tx_burst_(port_id, queue_id, tx_pkts, nb_pkts)
260}
261
262#[inline]
263pub unsafe fn rte_eth_rx_burst(
264    port_id: u16,
265    queue_id: u16,
266    rx_pkts: *mut *mut rte_mbuf,
267    nb_pkts: u16,
268) -> u16 {
269    rte_eth_rx_burst_(port_id, queue_id, rx_pkts, nb_pkts)
270}
271
272#[inline]
273pub unsafe fn rte_mbuf_refcnt_read(m: *const rte_mbuf) -> u16 {
274    rte_mbuf_refcnt_read_(m)
275}
276
277#[inline]
278pub unsafe fn rte_mbuf_refcnt_update(m: *mut rte_mbuf, value: i16) -> u16 {
279    rte_mbuf_refcnt_update_(m, value)
280}
281
282#[inline]
283pub unsafe fn rte_mbuf_refcnt_set(m: *mut rte_mbuf, value: i16) {
284    rte_mbuf_refcnt_set_(m, value)
285}
286
287#[inline]
288pub unsafe fn rte_pktmbuf_adj(packet: *mut rte_mbuf, len: u16) -> *mut c_char {
289    rte_pktmbuf_adj_(packet, len)
290}
291
292#[inline]
293pub unsafe fn rte_pktmbuf_trim(packet: *mut rte_mbuf, len: u16) -> c_int {
294    rte_pktmbuf_trim_(packet, len)
295}
296/// Returns the application thread ID of the execution unit.
297///
298/// In most cases, the lcore ID returned will correspond to the processor ID of the CPU
299/// on which the thread is pinned, but this will not be the case if the user has explicitly
300/// changed the thread-to-core affinities.
301///
302/// ## Remarks
303/// This is `unsafe` because it calls the DPDK `rte_lcore_id()` function via FFI. Use at your
304/// own risk.
305#[inline]
306pub unsafe fn rte_lcore_id() -> u16 {
307    rte_lcore_id_()
308}
309
310/// Reads the timestamp counter (TSC) register.
311///
312/// This is a low-overhead way to get CPU timing information, but may not be available on all
313/// platforms and could be imprecise. It should only be used to approximate cycle counts.
314///
315/// ## Remarks
316/// This is `unsafe` because it calls the DPDK `rte_rdtsc()` function via FFI. Use at your own risk.
317#[inline]
318pub unsafe fn rte_rdtsc() -> u64 {
319    rte_rdtsc_()
320}
321
322/* RTE_RING functions */
323
324#[inline]
325pub unsafe fn rte_ring_enqueue(ring: *mut rte_ring, obj: *mut c_void) -> c_int {
326    rte_ring_enqueue_(ring, obj)
327}
328
329#[inline]
330pub unsafe fn rte_ring_sp_enqueue(ring: *mut rte_ring, obj: *mut c_void) -> c_int {
331    rte_ring_sp_enqueue_(ring, obj)
332}
333
334#[inline]
335pub unsafe fn rte_ring_mp_enqueue(ring: *mut rte_ring, obj: *mut c_void) -> c_int {
336    rte_ring_mp_enqueue_(ring, obj)
337}
338
339#[inline]
340pub unsafe fn rte_ring_dequeue(ring: *mut rte_ring, obj_p: *mut *mut c_void) -> c_int {
341    rte_ring_dequeue_(ring, obj_p)
342}
343
344#[inline]
345pub unsafe fn rte_ring_sc_dequeue(ring: *mut rte_ring, obj_p: *mut *mut c_void) -> c_int {
346    rte_ring_sc_dequeue_(ring, obj_p)
347}
348
349#[inline]
350pub unsafe fn rte_ring_mc_dequeue(ring: *mut rte_ring, obj_p: *mut *mut c_void) -> c_int {
351    rte_ring_mc_dequeue_(ring, obj_p)
352}
353
354#[inline]
355pub unsafe fn rte_ring_count(ring: *const rte_ring) -> c_uint {
356    rte_ring_count_(ring)
357}
358
359#[inline]
360pub unsafe fn rte_ring_free_count(ring: *const rte_ring) -> c_uint {
361    rte_ring_free_count_(ring)
362}
363
364#[inline]
365pub unsafe fn rte_ring_full(ring: *const rte_ring) -> c_int {
366    rte_ring_full_(ring)
367}
368
369#[inline]
370pub unsafe fn rte_ring_empty(ring: *const rte_ring) -> c_int {
371    rte_ring_empty_(ring)
372}
373
374#[inline]
375pub unsafe fn rte_ring_get_size(ring: *const rte_ring) -> c_uint {
376    rte_ring_get_size_(ring)
377}
378
379#[inline]
380pub unsafe fn rte_ring_get_capacity(ring: *const rte_ring) -> c_uint {
381    rte_ring_get_capacity_(ring)
382}