ergon-tune 0.1.45

Start-up and housekeeping checks of a host's low-latency settings (CPU isolation, NIC interrupts, transparent hugepages, the Aeron directory, NUMA, scheduling policy, SMT siblings, a CPU quota and its throttling, how CLOCK_REALTIME is kept and the kernel's clock source, background noise on the hot CPUs (unbound workqueues, compaction, other processes' tasks, kernel instrumentation, KSM, steal), a hot thread's page faults, the IRQ CPUs' idle depth and TCP autocorking, and with `nic` the NIC's moderation, deferral, VF and ring): pure functions returning findings, with no threads or statics, and one audited unsafe call (a read-only adjtimex)
Documentation
//! T24: the NIC pair the device plugin grants, reported as the two shortest
//! lines a process on a DPDK port takes. The pair is copied, never guessed,
//! and nothing here binds a NIC.

use std::net::Ipv4Addr;

use crate::finding::{Finding, Severity};

const T24_EVIDENCE: &str = "the NIC device plugin's env contract \
     (RUSTERON_DPDK_{SENDER,RECEIVER}_{PCI,IPV4_CIDR,GATEWAY}); not measured here";

/// The three sender values a granted pair always sets, in the order the
/// checks below validate them: PCI address, IPv4 cidr, gateway.
const SENDER: [&str; 3] = [
    "RUSTERON_DPDK_SENDER_PCI",
    "RUSTERON_DPDK_SENDER_IPV4_CIDR",
    "RUSTERON_DPDK_SENDER_GATEWAY",
];

/// Why a PCI value cannot be spliced into a line: not a
/// `[domain:]bus:dev.func` address and not a DPDK device name.
const PCI_BAD: &str = "is not a PCI address dddd:bb:dd.d or a DPDK device name";
/// Why a cidr value cannot be spliced into a line.
const CIDR_BAD: &str = "is not an IPv4 cidr a.b.c.d/len with a prefix of 0 to 32";
/// Why a gateway value cannot be spliced into a line.
const GATEWAY_BAD: &str = "is not an IPv4 address";

/// T24: a process on DPDK has the device plugin's NIC pair complete and
/// consistent.
///
/// `on_dpdk` is whether this process drives a DPDK port: a media driver on
/// a DPDK UDP transport, or a network I/O loop on a DPDK stack.
/// `env` looks a variable up (`|n| std::env::var(n).ok()` for this process).
/// Off the pair, or with all three sender values unset, the check is ok and
/// the message says the device plugin has not granted a pair, not a guessed
/// address. An empty or unparsable value warns with the exact key. All
/// three set and usable, the message carries the copy lines built from them.
///
/// The check reads only: it exports nothing, binds no NIC, sets no
/// `AERON_DIR`, and never returns [`Status::Unknown`](crate::Status::Unknown),
/// so it never joins
/// [`unknown_stops_require`](crate::unknown_stops_require).
#[must_use]
pub fn t24_dpdk_nic_pair(on_dpdk: bool, env: impl Fn(&str) -> Option<String>) -> Finding {
    let id = "T24";
    let sev = Severity::Warn;
    if !on_dpdk {
        return Finding::ok(
            id,
            sev,
            T24_EVIDENCE,
            "this process is not on a DPDK port, so the NIC pair does not apply here".to_owned(),
        );
    }
    let pair = match DpdkNicPair::from_env(env) {
        Ok(Some(pair)) => pair,
        Ok(None) => {
            return Finding::ok(
                id,
                sev,
                T24_EVIDENCE,
                format!(
                    "{} are unset: the device plugin has not granted a pair, so nothing here \
                     guesses an address, a gateway or a PCI device",
                    SENDER.join(", ")
                ),
            );
        }
        Err(problems) => {
            return Finding::found(
                id,
                sev,
                T24_EVIDENCE,
                format!(
                    "the NIC pair is not usable: {problems}. Effect: no copy line can be built \
                     from it. Fix: set every empty key and correct every bad one from the pair \
                     the device plugin granted; this check copies nothing, binds no NIC and \
                     sets no AERON_DIR"
                ),
            );
        }
    };
    Finding::found(
        id,
        Severity::Info,
        T24_EVIDENCE,
        format!(
            "the NIC pair is set; the two shortest lines built from it (copied, not guessed): \
             {}; {}. This check copies nothing, binds no NIC and sets no AERON_DIR",
            pair.addr_gateway_line(),
            pair.port_ip_eal_line()
        ),
    )
}

/// The NIC the ENA device plugin granted this container.
///
/// The device plugin injects its sender PCI address, IPv4 address and
/// prefix, and gateway as [`DpdkNicPair::ENV`]. T24 reports it. Copied from what the kubelet allocated, never guessed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DpdkNicPair {
    /// `RUSTERON_DPDK_SENDER_PCI`: `[domain:]bus:dev.func`, or a DPDK device
    /// name (`net_tap0`) in the plugin's test mode.
    pub pci: String,
    /// The address of `RUSTERON_DPDK_SENDER_IPV4_CIDR`.
    pub addr: Ipv4Addr,
    /// The prefix length of `RUSTERON_DPDK_SENDER_IPV4_CIDR`, 0 to 32.
    pub prefix_len: u8,
    /// `RUSTERON_DPDK_SENDER_GATEWAY`.
    pub gateway: Ipv4Addr,
}

impl DpdkNicPair {
    /// The three variables the device plugin injects for the sender NIC,
    /// in the order they are checked.
    pub const ENV: [&'static str; 3] = SENDER;

    /// Reads the pair through `env` (`|n| std::env::var(n).ok()` for this
    /// process). `Ok(None)` when all three variables are unset: no pair was
    /// granted.
    ///
    /// # Errors
    /// Every empty or unparsable variable, by name and value, joined with
    /// `; `.
    pub fn from_env(env: impl Fn(&str) -> Option<String>) -> Result<Option<Self>, String> {
        // the trimmed values, in SENDER's order; None only where the variable is unset.
        let raw: [Option<String>; 3] = SENDER.map(|name| env(name).map(|v| v.trim().to_owned()));
        if raw.iter().all(Option::is_none) {
            return Ok(None);
        }
        let mut problems: Vec<String> = Vec::new();
        for (i, name) in SENDER.iter().enumerate() {
            match raw[i].as_deref() {
                None | Some("") => problems.push(format!("{name} is still empty")),
                Some(value) => {
                    let why = match i {
                        0 => pci_why(value),
                        1 => cidr_why(value),
                        _ => address_why(value),
                    };
                    if let Some(why) = why {
                        problems.push(format!("{name}={value} {why}"));
                    }
                }
            }
        }
        if !problems.is_empty() {
            return Err(problems.join("; "));
        }
        let [Some(pci), Some(cidr), Some(gateway)] = raw else {
            return Err("the NIC pair is incomplete".to_owned());
        };
        let (addr, prefix) = cidr.split_once('/').unwrap_or((cidr.as_str(), ""));
        match (addr.parse(), prefix.parse(), gateway.parse()) {
            (Ok(addr), Ok(prefix_len), Ok(gateway)) => Ok(Some(Self {
                pci,
                addr,
                prefix_len,
                gateway,
            })),
            _ => Err(format!("{} {CIDR_BAD}", SENDER[1])),
        }
    }

    /// Whether [`DpdkNicPair::pci`] is a PCI address (a NIC bound to
    /// `vfio-pci` or `igb_uio`) rather than a DPDK device name.
    #[must_use]
    pub fn is_pci_address(&self) -> bool {
        self.pci.contains(':')
    }

    /// The EAL arguments that give DPDK this one port: `-a <pci>`, or
    /// `--vdev=<name>` for a device name.
    #[must_use]
    pub fn eal_args(&self) -> [String; 2] {
        if self.is_pci_address() {
            ["-a".to_owned(), self.pci.clone()]
        } else {
            ["--vdev".to_owned(), self.pci.clone()]
        }
    }

    /// The shortest address-and-gateway line for this NIC:
    /// `addr=<ip>,gateway=<ip>,port=<pci or device name>`, and
    /// `,prefix_len=<n>` only when the prefix is not 24.
    #[must_use]
    pub fn addr_gateway_line(&self) -> String {
        let prefix = if self.prefix_len == 24 {
            String::new()
        } else {
            format!(",prefix_len={}", self.prefix_len)
        };
        format!(
            "addr={},gateway={},port={}{prefix}",
            self.addr, self.gateway, self.pci
        )
    }

    /// The shortest port-ip-eal line for this NIC: `port`, `ip` (with its
    /// prefix length) and the `eal` arguments that give DPDK the one port.
    #[must_use]
    pub fn port_ip_eal_line(&self) -> String {
        format!(
            "port={},ip={}/{},eal={}",
            self.pci,
            self.addr,
            self.prefix_len,
            self.eal_args().join(" ")
        )
    }
}

/// `Some(reason)` when `value` is not `[domain:]bus:dev.func` and not a
/// DPDK device name (`net_tap0`, `net_af_packet0`).
fn pci_why(value: &str) -> Option<&'static str> {
    if value.split_whitespace().count() != 1 || value.contains(',') || value.contains('=') {
        return Some(PCI_BAD);
    }
    if !value.contains(':') {
        // a DPDK device name, which the test vdev mode uses in place of a PCI address.
        return None;
    }
    let fields: Vec<&str> = value.split([':', '.']).collect();
    let bdf = match fields.as_slice() {
        [domain, bus, dev, func] => {
            is_hex(domain, 1, 4) && is_hex(bus, 2, 2) && is_hex(dev, 2, 2) && is_func(func)
        }
        [bus, dev, func] => is_hex(bus, 2, 2) && is_hex(dev, 2, 2) && is_func(func),
        _ => false,
    };
    if bdf { None } else { Some(PCI_BAD) }
}

/// `Some(reason)` when `value` is not `a.b.c.d/len` with a prefix of 0 to 32.
fn cidr_why(value: &str) -> Option<&'static str> {
    let Some((addr, prefix)) = value.split_once('/') else {
        return Some(CIDR_BAD);
    };
    if addr.parse::<Ipv4Addr>().is_err() {
        return Some(CIDR_BAD);
    }
    match prefix.parse::<u8>() {
        Ok(len) if len <= 32 => None,
        _ => Some(CIDR_BAD),
    }
}

/// `Some(reason)` when `value` is not an IPv4 address.
fn address_why(value: &str) -> Option<&'static str> {
    if value.parse::<Ipv4Addr>().is_ok() {
        None
    } else {
        Some(GATEWAY_BAD)
    }
}

/// A field of a PCI address: 1 to 4 hex digits (`min` to `max`).
fn is_hex(field: &str, min: usize, max: usize) -> bool {
    field.len() >= min && field.len() <= max && field.chars().all(|c| c.is_ascii_hexdigit())
}

/// The function number of a PCI address: one hex digit, 0 to 7.
fn is_func(field: &str) -> bool {
    field.len() == 1 && field.chars().all(|c| c.is_ascii_hexdigit()) && field <= "7"
}