#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(clippy::missing_safety_doc)]
#[cfg(not(netmap_disabled))]
mod bindings {
include!(concat!(env!("OUT_DIR"), "/binding.rs"));
}
#[cfg(not(netmap_disabled))]
pub use bindings::*;
#[cfg(not(netmap_disabled))]
pub mod exports {
pub use super::bindings::NR_REG_ALL_NIC;
pub use super::bindings::NR_REG_NIC_SW;
pub use super::bindings::NR_REG_ONE_NIC;
pub use super::bindings::NR_REG_PIPE_MASTER;
pub use super::bindings::NR_REG_PIPE_SLAVE;
pub use super::bindings::NR_REG_SW;
}
#[cfg(not(netmap_disabled))]
#[repr(C)]
#[derive(Debug)]
pub struct nm_desc {
pub self_: *mut nm_desc,
pub fd: i32,
pub mem: *mut core::ffi::c_void,
pub memsize: usize,
pub done_mmap: i32,
pub nifp: *const netmap_if,
pub first_tx_ring: u16,
pub last_tx_ring: u16,
pub cur_tx_ring: u16,
pub first_rx_ring: u16,
pub last_rx_ring: u16,
pub cur_rx_ring: u16,
pub req: nmreq,
pub hdr: nm_pkthdr,
pub some_ring: *const netmap_ring,
pub buf_start: *const core::ffi::c_void,
pub buf_end: *const core::ffi::c_void,
pub snaplen: i32,
pub promisc: i32,
pub to_ms: i32,
pub errbuf: *mut core::ffi::c_char,
pub if_flags: u32,
pub if_reqcap: u32,
pub if_curcap: u32,
pub st: nm_stat,
pub msg: [core::ffi::c_char; NM_ERRBUF_SIZE as usize],
}
#[cfg(not(netmap_disabled))]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct nm_pkthdr {
pub ts: timeval,
pub caplen: core::ffi::c_uint,
pub len: core::ffi::c_uint,
pub flags: u64,
pub d: *mut nm_desc,
pub slot: *mut netmap_slot,
pub buf: *mut core::ffi::c_uchar,
}
#[cfg(not(netmap_disabled))]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct nm_stat {
pub ps_recv: core::ffi::c_uint,
pub ps_drop: core::ffi::c_uint,
pub ps_ifdrop: core::ffi::c_uint,
}
#[cfg(not(netmap_disabled))]
extern "C" {
#[link_name = "ffi_nm_open"]
pub fn nm_open(
ifname: *const core::ffi::c_char,
req: *const nmreq,
flags: u64,
arg: *const nm_desc,
) -> *mut nm_desc;
#[link_name = "ffi_nm_close"]
pub fn nm_close(d: *mut nm_desc) -> core::ffi::c_int;
#[link_name = "ffi_nm_mmap"]
pub fn nm_mmap(d: *mut nm_desc, parent: *const nm_desc) -> core::ffi::c_int;
#[link_name = "ffi_nm_inject"]
pub fn nm_inject(
d: *mut nm_desc,
buf: *const core::ffi::c_void,
size: usize,
) -> core::ffi::c_int;
}
pub const NIOCTXSYNC: std::os::raw::c_ulong = 0x6994;
pub const NIOCRXSYNC: std::os::raw::c_ulong = 0x6995;
pub const NIOCREGIF: std::os::raw::c_ulong = 0xc03c_6992;
pub const NIOCCTRL: std::os::raw::c_ulong = 0xc058_6997;
#[cfg(not(netmap_disabled))]
pub const NR_REG_SW_ONLY: u32 = NR_REG_SW as u32;
#[cfg(not(netmap_disabled))]
pub const NR_REG_NIC_ONLY: u32 = NR_REG_ALL_NIC as u32;
#[cfg(not(netmap_disabled))]
pub const NR_REG_NIC_AND_SW: u32 = NR_REG_NIC_SW as u32;
#[cfg(not(netmap_disabled))]
#[inline]
pub unsafe fn NETMAP_TXRING(nifp: *const netmap_if, index: u32) -> *mut netmap_ring {
let offset = (*nifp).ring_ofs.as_ptr().add(index as usize).read();
(nifp as *const u8).add(offset as usize) as *mut netmap_ring
}
#[cfg(not(netmap_disabled))]
#[inline]
pub unsafe fn NETMAP_RXRING(nifp: *const netmap_if, index: u32) -> *mut netmap_ring {
let base = (*nifp).ni_tx_rings + (*nifp).ni_host_tx_rings;
let offset = (*nifp)
.ring_ofs
.as_ptr()
.add((base + index) as usize)
.read();
(nifp as *const u8).add(offset as usize) as *mut netmap_ring
}
#[cfg(not(netmap_disabled))]
#[inline]
pub unsafe fn NETMAP_IF(base: *const core::ffi::c_void, ofs: isize) -> *mut netmap_if {
(base as *const u8).offset(ofs) as *mut netmap_if
}
#[cfg(not(netmap_disabled))]
#[inline]
pub unsafe fn NETMAP_BUF(ring: *const netmap_ring, index: u32) -> *mut core::ffi::c_void {
(ring as *const u8)
.offset((*ring).buf_ofs as isize)
.add(index as usize * (*ring).nr_buf_size as usize) as *mut core::ffi::c_void
}
#[cfg(netmap_disabled)]
mod disabled {
pub const NETMAP_DISABLED: bool = true;
}
#[cfg(all(test, not(netmap_disabled)))]
mod tests {
use super::*;
use core::mem;
#[test]
fn struct_sizes() {
assert_eq!(mem::size_of::<netmap_slot>(), 16);
assert_eq!(mem::align_of::<netmap_slot>(), 8);
assert_eq!(mem::size_of::<netmap_ring>(), 256);
assert_eq!(mem::size_of::<nmreq>(), 60);
assert_eq!(mem::size_of::<netmap_if>(), 56);
assert_eq!(mem::size_of::<nm_desc>(), 760);
assert_eq!(mem::size_of::<nm_pkthdr>(), 56);
assert_eq!(mem::size_of::<nm_stat>(), 12);
}
#[test]
fn ioctl_constants() {
assert_eq!(NIOCTXSYNC, 0x6994);
assert_eq!(NIOCRXSYNC, 0x6995);
assert_eq!(NETMAP_API, 14);
assert_eq!(NR_REG_ALL_NIC as u32, 1);
assert_eq!(NR_REG_SW as u32, 2);
}
#[test]
fn reg_mode_constants_make_sense() {
let all_nic = NR_REG_ALL_NIC as u32;
let sw = NR_REG_SW as u32;
let nic_sw = NR_REG_NIC_SW as u32;
assert!(all_nic < sw);
assert!(sw < nic_sw);
assert_eq!(NR_REG_SW_ONLY, NR_REG_SW as u32);
assert_eq!(NR_REG_NIC_ONLY, NR_REG_ALL_NIC as u32);
}
#[test]
fn ring_macro_shims_are_unsafe_fn() {
fn _assert_fn_ptr(_f: unsafe fn(*const netmap_if, u32) -> *mut netmap_ring) {}
_assert_fn_ptr(NETMAP_TXRING);
_assert_fn_ptr(NETMAP_RXRING);
}
}