#![cfg_attr(not(target_os = "linux"), allow(dead_code))]
use std::collections::BTreeMap;
use std::ffi::c_int;
const ETHTOOL_GDRVINFO: u32 = 0x03;
const ETHTOOL_GLINK: u32 = 0x0a;
const ETHTOOL_GCOALESCE: u32 = 0x0e;
const ETHTOOL_SCOALESCE: u32 = 0x0f;
const ETHTOOL_GRINGPARAM: u32 = 0x10;
const ETHTOOL_SRINGPARAM: u32 = 0x11;
const ETHTOOL_GPAUSEPARAM: u32 = 0x12;
const ETHTOOL_SPAUSEPARAM: u32 = 0x13;
const ETHTOOL_GSTRINGS: u32 = 0x1b;
const ETHTOOL_GSTATS: u32 = 0x1d;
const ETHTOOL_GFLAGS: u32 = 0x25;
const ETHTOOL_SFLAGS: u32 = 0x26;
const ETHTOOL_GRXCLSRLCNT: u32 = 0x2e;
const ETHTOOL_GRXCLSRULE: u32 = 0x2f;
const ETHTOOL_GRXCLSRLALL: u32 = 0x30;
const ETHTOOL_SRXCLSRLDEL: u32 = 0x31;
const ETHTOOL_SRXCLSRLINS: u32 = 0x32;
const ETHTOOL_GSSET_INFO: u32 = 0x37;
const ETHTOOL_GCHANNELS: u32 = 0x3c;
const ETHTOOL_SCHANNELS: u32 = 0x3d;
const ETHTOOL_GET_TS_INFO: u32 = 0x41;
const ETHTOOL_GEEE: u32 = 0x44;
const ETHTOOL_SEEE: u32 = 0x45;
const ETH_SS_TEST: u32 = 0;
const ETH_SS_STATS: u32 = 1;
const ETH_SS_PRIV_FLAGS: u32 = 2;
const ETH_GSTRING_LEN: usize = 32;
const ETH_FLAG_TXVLAN: u32 = 1 << 7;
const ETH_FLAG_RXVLAN: u32 = 1 << 8;
const ETH_FLAG_LRO: u32 = 1 << 15;
pub(crate) const ETH_FLAG_NTUPLE: u32 = 1 << 27;
const ETH_FLAG_RXHASH: u32 = 1 << 28;
const ETH_ALL_FLAGS: u32 =
ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH;
const RX_CLS_FLOW_DISC: u64 = u64::MAX;
const RX_CLS_FLOW_WAKE: u64 = 0xffff_ffff_ffff_fffe;
const FLOW_RSS: u32 = 0x2000_0000;
const ETHTOOL_RX_FLOW_SPEC_RING_VF: u64 = 0x0000_00FF_0000_0000;
const FLOW_SPEC_LEN: usize = 168;
const RXNFC_FS: usize = 16;
const RXNFC_RULE_CNT: usize = RXNFC_FS + FLOW_SPEC_LEN;
const RXNFC_RULE_LOCS: usize = RXNFC_RULE_CNT + 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Rings {
pub rx_max: u32,
pub rx_mini_max: u32,
pub rx_jumbo_max: u32,
pub tx_max: u32,
pub rx: u32,
pub rx_mini: u32,
pub rx_jumbo: u32,
pub tx: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Channels {
pub rx_max: u32,
pub tx_max: u32,
pub other_max: u32,
pub combined_max: u32,
pub rx: u32,
pub tx: u32,
pub other: u32,
pub combined: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Pause {
pub autoneg: bool,
pub rx: bool,
pub tx: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Eee {
pub supported: u32,
pub advertised: u32,
pub lp_advertised: u32,
pub active: bool,
pub enabled: bool,
pub tx_lpi_enabled: bool,
pub tx_lpi_timer: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Coalesce {
pub rx_usecs: u32,
pub rx_frames: u32,
pub rx_usecs_irq: u32,
pub rx_frames_irq: u32,
pub tx_usecs: u32,
pub tx_frames: u32,
pub tx_usecs_irq: u32,
pub tx_frames_irq: u32,
pub stats_block_usecs: u32,
pub adaptive_rx: bool,
pub adaptive_tx: bool,
pub pkt_rate_low: u32,
pub rx_usecs_low: u32,
pub rx_frames_low: u32,
pub tx_usecs_low: u32,
pub tx_frames_low: u32,
pub pkt_rate_high: u32,
pub rx_usecs_high: u32,
pub rx_frames_high: u32,
pub tx_usecs_high: u32,
pub tx_frames_high: u32,
pub sample_interval: u32,
}
impl Coalesce {
fn words(&self) -> [u32; 22] {
[
self.rx_usecs,
self.rx_frames,
self.rx_usecs_irq,
self.rx_frames_irq,
self.tx_usecs,
self.tx_frames,
self.tx_usecs_irq,
self.tx_frames_irq,
self.stats_block_usecs,
u32::from(self.adaptive_rx),
u32::from(self.adaptive_tx),
self.pkt_rate_low,
self.rx_usecs_low,
self.rx_frames_low,
self.tx_usecs_low,
self.tx_frames_low,
self.pkt_rate_high,
self.rx_usecs_high,
self.rx_frames_high,
self.tx_usecs_high,
self.tx_frames_high,
self.sample_interval,
]
}
fn from_words(w: [u32; 22]) -> Self {
Coalesce {
rx_usecs: w[0],
rx_frames: w[1],
rx_usecs_irq: w[2],
rx_frames_irq: w[3],
tx_usecs: w[4],
tx_frames: w[5],
tx_usecs_irq: w[6],
tx_frames_irq: w[7],
stats_block_usecs: w[8],
adaptive_rx: w[9] != 0,
adaptive_tx: w[10] != 0,
pkt_rate_low: w[11],
rx_usecs_low: w[12],
rx_frames_low: w[13],
tx_usecs_low: w[14],
tx_frames_low: w[15],
pkt_rate_high: w[16],
rx_usecs_high: w[17],
rx_frames_high: w[18],
tx_usecs_high: w[19],
tx_frames_high: w[20],
sample_interval: w[21],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct CoalesceParams(pub u32);
impl CoalesceParams {
pub const NONE: Self = Self(0);
pub const ALL: Self = Self((1 << 22) - 1);
pub const RX_USECS: Self = Self(1 << 0);
pub const RX_MAX_FRAMES: Self = Self(1 << 1);
pub const RX_USECS_IRQ: Self = Self(1 << 2);
pub const RX_MAX_FRAMES_IRQ: Self = Self(1 << 3);
pub const TX_USECS: Self = Self(1 << 4);
pub const TX_MAX_FRAMES: Self = Self(1 << 5);
pub const TX_USECS_IRQ: Self = Self(1 << 6);
pub const TX_MAX_FRAMES_IRQ: Self = Self(1 << 7);
pub const STATS_BLOCK_USECS: Self = Self(1 << 8);
pub const USE_ADAPTIVE_RX: Self = Self(1 << 9);
pub const USE_ADAPTIVE_TX: Self = Self(1 << 10);
pub const PKT_RATE_LOW: Self = Self(1 << 11);
pub const RX_USECS_LOW: Self = Self(1 << 12);
pub const RX_MAX_FRAMES_LOW: Self = Self(1 << 13);
pub const TX_USECS_LOW: Self = Self(1 << 14);
pub const TX_MAX_FRAMES_LOW: Self = Self(1 << 15);
pub const PKT_RATE_HIGH: Self = Self(1 << 16);
pub const RX_USECS_HIGH: Self = Self(1 << 17);
pub const RX_MAX_FRAMES_HIGH: Self = Self(1 << 18);
pub const TX_USECS_HIGH: Self = Self(1 << 19);
pub const TX_MAX_FRAMES_HIGH: Self = Self(1 << 20);
pub const RATE_SAMPLE_INTERVAL: Self = Self(1 << 21);
pub const USECS: Self = Self(Self::RX_USECS.0 | Self::TX_USECS.0);
pub const MAX_FRAMES: Self = Self(Self::RX_MAX_FRAMES.0 | Self::TX_MAX_FRAMES.0);
pub const USE_ADAPTIVE: Self = Self(Self::USE_ADAPTIVE_RX.0 | Self::USE_ADAPTIVE_TX.0);
}
impl std::ops::BitOr for CoalesceParams {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FlowRule {
pub location: u32,
pub flow_type: u32,
pub ring_cookie: u64,
pub spec: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct NicEthtool {
pub firmware: String,
pub expansion_rom: String,
pub register_dump_len: u32,
pub eeprom_len: u32,
pub private_flags_count: u32,
pub self_test_count: u32,
pub rings: Option<Rings>,
pub coalesce: Option<(CoalesceParams, Coalesce)>,
pub coalesce_usecs_max: u32,
pub coalesce_frames_max: u32,
pub channels: Option<Channels>,
pub pause: Option<Pause>,
pub eee: Option<Eee>,
pub features: u32,
pub hw_features: u32,
pub flow_slots: Option<u32>,
pub flow_rules: BTreeMap<u32, FlowRule>,
pub stats: Vec<(String, u64)>,
pub threaded_napi: bool,
pub etf_offload: Option<Vec<u16>>,
pub phc_index: Option<u32>,
pub ts_info: Option<(u32, u32, u32)>,
}
pub(crate) struct Ctx<'a> {
pub(crate) net_admin: bool,
pub(crate) link_up: bool,
pub(crate) hw_timestamping: bool,
pub(crate) driver: &'a str,
pub(crate) version: &'a str,
pub(crate) bus_info: &'a str,
pub(crate) queues: (&'a mut usize, &'a mut usize),
}
fn unprivileged(cmd: u32) -> bool {
matches!(
cmd,
0x01 | ETHTOOL_GDRVINFO
| 0x07 | ETHTOOL_GLINK
| ETHTOOL_GCOALESCE
| ETHTOOL_GRINGPARAM
| ETHTOOL_GPAUSEPARAM
| 0x14 | 0x16 | 0x18 | ETHTOOL_GSSET_INFO
| ETHTOOL_GSTRINGS
| ETHTOOL_GSTATS
| 0x4a | 0x1e | 0x20 | 0x21 | 0x23 | 0x2b | ETHTOOL_GFLAGS
| 0x27 | 0x29 | 0x2d | ETHTOOL_GRXCLSRLCNT
| ETHTOOL_GRXCLSRULE
| ETHTOOL_GRXCLSRLALL
| 0x38 | 0x46 | 0x3a | ETHTOOL_GCHANNELS
| ETHTOOL_GET_TS_INFO
| ETHTOOL_GEEE
| 0x48 | 0x4e | 0x4c | 0x50 )
}
unsafe fn read_words<const N: usize>(data: *const u8) -> [u32; N] {
let mut w = [0u32; N];
for (i, v) in w.iter_mut().enumerate() {
*v = unsafe { (data.add(4 * i) as *const u32).read_unaligned() };
}
w
}
unsafe fn write_words(data: *mut u8, w: &[u32]) {
for (i, v) in w.iter().enumerate() {
unsafe { (data.add(4 * i) as *mut u32).write_unaligned(*v) };
}
}
unsafe fn write_cstr(base: *mut u8, offset: usize, width: usize, s: &str) {
let bytes = s.as_bytes();
let n = bytes.len().min(width - 1);
unsafe {
std::ptr::write_bytes(base.add(offset), 0, width);
std::ptr::copy_nonoverlapping(bytes.as_ptr(), base.add(offset), n);
}
}
impl NicEthtool {
pub(crate) fn new() -> Self {
NicEthtool {
coalesce_usecs_max: u32::MAX,
coalesce_frames_max: u32::MAX,
..Default::default()
}
}
pub(crate) unsafe fn ioctl(&mut self, ctx: Ctx<'_>, data: *mut u8) -> Result<(), c_int> {
let cmd = unsafe { (data as *const u32).read_unaligned() };
if !unprivileged(cmd) && !ctx.net_admin {
return Err(libc::EPERM);
}
match cmd {
ETHTOOL_GDRVINFO => {
self.drvinfo(&ctx, data);
Ok(())
}
ETHTOOL_GET_TS_INFO => {
self.ts_info(&ctx, data);
Ok(())
}
ETHTOOL_GLINK => {
unsafe { write_words(data, &[ETHTOOL_GLINK, u32::from(ctx.link_up)]) };
Ok(())
}
ETHTOOL_GRINGPARAM => {
let r = self.rings.ok_or(libc::EOPNOTSUPP)?;
let w = [
ETHTOOL_GRINGPARAM,
r.rx_max,
r.rx_mini_max,
r.rx_jumbo_max,
r.tx_max,
r.rx,
r.rx_mini,
r.rx_jumbo,
r.tx,
];
unsafe { write_words(data, &w) };
Ok(())
}
ETHTOOL_SRINGPARAM => {
let r = self.rings.as_mut().ok_or(libc::EOPNOTSUPP)?;
let w = unsafe { read_words::<9>(data) };
if w[5] > r.rx_max
|| w[6] > r.rx_mini_max
|| w[7] > r.rx_jumbo_max
|| w[8] > r.tx_max
{
return Err(libc::EINVAL);
}
(r.rx, r.rx_mini, r.rx_jumbo, r.tx) = (w[5], w[6], w[7], w[8]);
Ok(())
}
ETHTOOL_GCOALESCE => {
let (_, c) = self.coalesce.ok_or(libc::EOPNOTSUPP)?;
let mut w = vec![ETHTOOL_GCOALESCE];
w.extend(c.words());
unsafe { write_words(data, &w) };
Ok(())
}
ETHTOOL_SCOALESCE => self.set_coalesce(data),
ETHTOOL_GPAUSEPARAM => {
let p = self.pause.ok_or(libc::EOPNOTSUPP)?;
let w = [
ETHTOOL_GPAUSEPARAM,
p.autoneg.into(),
p.rx.into(),
p.tx.into(),
];
unsafe { write_words(data, &w) };
Ok(())
}
ETHTOOL_SPAUSEPARAM => {
let p = self.pause.as_mut().ok_or(libc::EOPNOTSUPP)?;
let w = unsafe { read_words::<4>(data) };
*p = Pause {
autoneg: w[1] != 0,
rx: w[2] != 0,
tx: w[3] != 0,
};
Ok(())
}
ETHTOOL_GCHANNELS => {
let c = self.channels.ok_or(libc::EOPNOTSUPP)?;
let w = [
ETHTOOL_GCHANNELS,
c.rx_max,
c.tx_max,
c.other_max,
c.combined_max,
c.rx,
c.tx,
c.other,
c.combined,
];
unsafe { write_words(data, &w) };
Ok(())
}
ETHTOOL_SCHANNELS => self.set_channels(ctx, data),
ETHTOOL_GEEE => {
let e = self.eee.ok_or(libc::EOPNOTSUPP)?;
let active = e.enabled && ctx.link_up && e.advertised & e.lp_advertised != 0;
let w = [
ETHTOOL_GEEE,
e.supported,
e.advertised,
e.lp_advertised,
active.into(),
e.enabled.into(),
e.tx_lpi_enabled.into(),
e.tx_lpi_timer,
0,
0,
];
unsafe { write_words(data, &w) };
Ok(())
}
ETHTOOL_SEEE => {
let e = self.eee.as_mut().ok_or(libc::EOPNOTSUPP)?;
let w = unsafe { read_words::<8>(data) };
if w[2] & !e.supported != 0 {
return Err(libc::EINVAL);
}
e.advertised = w[2];
e.enabled = w[5] != 0;
e.tx_lpi_enabled = w[6] != 0;
e.tx_lpi_timer = w[7];
Ok(())
}
ETHTOOL_GFLAGS => {
unsafe { write_words(data, &[ETHTOOL_GFLAGS, self.features]) };
Ok(())
}
ETHTOOL_SFLAGS => {
let flags = unsafe { (data.add(4) as *const u32).read_unaligned() };
if flags & !ETH_ALL_FLAGS != 0 {
return Err(libc::EINVAL);
}
let changed = (flags ^ self.features) & ETH_ALL_FLAGS;
if changed & !self.hw_features != 0 {
return Err(if changed & self.hw_features != 0 {
libc::EINVAL
} else {
libc::EOPNOTSUPP
});
}
self.features = (self.features & !changed) | (flags & changed);
Ok(())
}
ETHTOOL_GRXCLSRLCNT | ETHTOOL_GRXCLSRULE | ETHTOOL_GRXCLSRLALL => unsafe {
self.get_rxnfc(cmd, data)
},
ETHTOOL_SRXCLSRLINS | ETHTOOL_SRXCLSRLDEL => unsafe { self.set_rxnfc(cmd, &ctx, data) },
ETHTOOL_GSSET_INFO => {
let mask = unsafe { (data.add(8) as *const u64).read_unaligned() };
if mask == 0 {
return Ok(());
}
let mut out_mask = 0u64;
unsafe {
std::ptr::write_bytes(data.add(4), 0, 12);
let mut offset = 16;
for (set, count) in [
(ETH_SS_TEST, self.self_test_count),
(ETH_SS_STATS, self.stats.len() as u32),
(ETH_SS_PRIV_FLAGS, self.private_flags_count),
] {
if mask & (1 << set) != 0 && count != 0 {
out_mask |= 1 << set;
(data.add(offset) as *mut u32).write_unaligned(count);
offset += 4;
}
}
(data.add(8) as *mut u64).write_unaligned(out_mask);
}
Ok(())
}
ETHTOOL_GSTRINGS => {
let set = unsafe { (data.add(4) as *const u32).read_unaligned() };
if set != ETH_SS_STATS || self.stats.is_empty() {
return Err(libc::EOPNOTSUPP);
}
unsafe {
(data.add(8) as *mut u32).write_unaligned(self.stats.len() as u32);
for (i, (name, _)) in self.stats.iter().enumerate() {
write_cstr(data, 12 + i * ETH_GSTRING_LEN, ETH_GSTRING_LEN, name);
}
}
Ok(())
}
ETHTOOL_GSTATS => {
if self.stats.is_empty() {
return Err(libc::EOPNOTSUPP);
}
unsafe {
(data.add(4) as *mut u32).write_unaligned(self.stats.len() as u32);
for (i, (_, v)) in self.stats.iter().enumerate() {
(data.add(8 + 8 * i) as *mut u64).write_unaligned(*v);
}
}
Ok(())
}
_ => Err(libc::EOPNOTSUPP),
}
}
fn drvinfo(&self, ctx: &Ctx<'_>, data: *mut u8) {
unsafe {
write_cstr(data, 4, 32, ctx.driver);
write_cstr(data, 36, 32, ctx.version);
write_cstr(data, 68, 32, &self.firmware);
write_cstr(data, 100, 32, ctx.bus_info);
write_cstr(data, 132, 32, &self.expansion_rom);
std::ptr::write_bytes(data.add(164), 0, 32);
(data.add(176) as *mut u32).write_unaligned(self.private_flags_count);
(data.add(180) as *mut u32).write_unaligned(self.stats.len() as u32);
(data.add(184) as *mut u32).write_unaligned(self.self_test_count);
(data.add(188) as *mut u32).write_unaligned(self.eeprom_len);
(data.add(192) as *mut u32).write_unaligned(self.register_dump_len);
}
}
fn ts_info(&self, ctx: &Ctx<'_>, data: *mut u8) {
let (stamping, tx_types, rx_filters) = self.timestamping(ctx.hw_timestamping);
let phc = self.phc_index.map_or(-1, |i| i as i32) as u32;
let w = [
ETHTOOL_GET_TS_INFO,
stamping,
phc,
tx_types,
0,
0,
0,
rx_filters,
0,
0,
0,
];
unsafe { write_words(data, &w) };
}
pub(crate) fn timestamping(&self, hw: bool) -> (u32, u32, u32) {
const TX_HARDWARE: u32 = 1 << 0;
const TX_SOFTWARE: u32 = 1 << 1;
const RX_HARDWARE: u32 = 1 << 2;
const RX_SOFTWARE: u32 = 1 << 3;
const SOFTWARE: u32 = 1 << 4;
const RAW_HARDWARE: u32 = 1 << 6;
match self.ts_info {
Some(caps) => caps,
None if hw => (
TX_SOFTWARE | TX_HARDWARE | RX_HARDWARE | RAW_HARDWARE | RX_SOFTWARE | SOFTWARE,
0b11,
0b11,
),
None => (RX_SOFTWARE | SOFTWARE, 0, 0),
}
}
fn set_coalesce(&mut self, data: *mut u8) -> Result<(), c_int> {
let usecs_max = self.coalesce_usecs_max;
let frames_max = self.coalesce_frames_max;
let (supported, current) = self.coalesce.as_mut().ok_or(libc::EOPNOTSUPP)?;
let w = unsafe { read_words::<23>(data) };
let mut values = [0u32; 22];
values.copy_from_slice(&w[1..]);
let nonzero = values
.iter()
.enumerate()
.filter(|&(_, &v)| v != 0)
.fold(0u32, |m, (i, _)| m | 1 << i);
if supported.0 & nonzero != nonzero {
return Err(libc::EOPNOTSUPP);
}
const USECS: [usize; 9] = [0, 2, 4, 6, 8, 12, 14, 17, 19];
const FRAMES: [usize; 8] = [1, 3, 5, 7, 13, 15, 18, 20];
if USECS.iter().any(|&i| values[i] > usecs_max)
|| FRAMES.iter().any(|&i| values[i] > frames_max)
{
return Err(libc::EINVAL);
}
*current = Coalesce::from_words(values);
Ok(())
}
fn set_channels(&mut self, ctx: Ctx<'_>, data: *mut u8) -> Result<(), c_int> {
let max_ring = self.max_rule_ring();
let c = self.channels.as_mut().ok_or(libc::EOPNOTSUPP)?;
let w = unsafe { read_words::<9>(data) };
let (rx, tx, other, combined) = (w[5], w[6], w[7], w[8]);
if (rx, tx, combined, other) == (c.rx, c.tx, c.combined, c.other) {
return Ok(());
}
if rx > c.rx_max || tx > c.tx_max || combined > c.combined_max || other > c.other_max {
return Err(libc::EINVAL);
}
if combined == 0 && (rx == 0 || tx == 0) {
return Err(libc::EINVAL);
}
if let Some(max_ring) = max_ring
&& u64::from(combined + rx) <= max_ring
{
return Err(libc::EINVAL);
}
(c.rx, c.tx, c.other, c.combined) = (rx, tx, other, combined);
*ctx.queues.0 = (combined + rx) as usize;
*ctx.queues.1 = (combined + tx) as usize;
Ok(())
}
fn max_rule_ring(&self) -> Option<u64> {
if self.flow_rules.is_empty() {
return None;
}
Some(
self.flow_rules
.values()
.filter(|r| {
r.ring_cookie != RX_CLS_FLOW_DISC
&& r.ring_cookie != RX_CLS_FLOW_WAKE
&& r.flow_type & FLOW_RSS == 0
&& r.ring_cookie & ETHTOOL_RX_FLOW_SPEC_RING_VF == 0
})
.map(|r| r.ring_cookie)
.max()
.unwrap_or(0),
)
}
unsafe fn get_rxnfc(&self, cmd: u32, data: *mut u8) -> Result<(), c_int> {
let slots = self.flow_slots.ok_or(libc::EOPNOTSUPP)?;
match cmd {
ETHTOOL_GRXCLSRLCNT => unsafe {
(data.add(8) as *mut u64).write_unaligned(u64::from(slots));
(data.add(RXNFC_RULE_CNT) as *mut u32)
.write_unaligned(self.flow_rules.len() as u32);
},
ETHTOOL_GRXCLSRULE => {
let location = unsafe { (data.add(RXNFC_FS + 160) as *const u32).read_unaligned() };
let rule = self.flow_rules.get(&location).ok_or(libc::EINVAL)?;
unsafe {
std::ptr::copy_nonoverlapping(
rule.spec.as_ptr(),
data.add(RXNFC_FS),
FLOW_SPEC_LEN,
)
};
}
_ => {
let room =
unsafe { (data.add(RXNFC_RULE_CNT) as *const u32).read_unaligned() } as usize;
if self.flow_rules.len() > room {
return Err(libc::EMSGSIZE);
}
unsafe {
(data.add(8) as *mut u64).write_unaligned(u64::from(slots));
for (i, &loc) in self.flow_rules.keys().enumerate() {
(data.add(RXNFC_RULE_LOCS + 4 * i) as *mut u32).write_unaligned(loc);
}
(data.add(RXNFC_RULE_CNT) as *mut u32)
.write_unaligned(self.flow_rules.len() as u32);
}
}
}
Ok(())
}
unsafe fn set_rxnfc(&mut self, cmd: u32, ctx: &Ctx<'_>, data: *mut u8) -> Result<(), c_int> {
let slots = self.flow_slots.ok_or(libc::EOPNOTSUPP)?;
let fs = unsafe { data.add(RXNFC_FS) };
let location = unsafe { (fs.add(160) as *const u32).read_unaligned() };
if cmd == ETHTOOL_SRXCLSRLDEL {
return self
.flow_rules
.remove(&location)
.map(drop)
.ok_or(libc::EINVAL);
}
if location >= slots {
return Err(libc::EINVAL);
}
let ring_cookie = unsafe { (fs.add(152) as *const u64).read_unaligned() };
if ring_cookie >= *ctx.queues.0 as u64 {
return Err(libc::EINVAL);
}
let flow_type = unsafe { (fs as *const u32).read_unaligned() };
let mut spec = vec![0u8; FLOW_SPEC_LEN];
unsafe { std::ptr::copy_nonoverlapping(fs, spec.as_mut_ptr(), FLOW_SPEC_LEN) };
self.flow_rules.insert(
location,
FlowRule {
location,
flow_type,
ring_cookie,
spec,
},
);
Ok(())
}
}