From b1a6c7d5c40e9c9967f8d85b9b4c9ff43615f89f Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Fri, 27 Nov 2026 22:08:13 -0800 Subject: [PATCH] nvx: limit address and lowering helpers to the network module The address and lowering modules declared their production items pub(crate), yet only host::network uses them. Declare them pub(super), as their siblings guest_subnet and parse_rules already are. Closes #2313 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- nvx/src/host/network/address.rs | 28 +++++++++++------------ nvx/src/host/network/lowering.rs | 38 ++++++++++++++++---------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/nvx/src/host/network/address.rs b/nvx/src/host/network/address.rs index 9e4ab0b2d..f9cb8c78e 100644 --- a/nvx/src/host/network/address.rs +++ b/nvx/src/host/network/address.rs @@ -15,7 +15,7 @@ use std::net::{Ipv4Addr, Ipv6Addr}; /// /// IPv4 orders before IPv6, as Python's IP versions 4 and 6 do. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub(crate) enum Family { +pub(super) enum Family { /// IPv4: 32-bit addresses. V4, /// IPv6: 128-bit addresses. @@ -24,7 +24,7 @@ pub(crate) enum Family { impl Family { /// Both families, in the order in which the compiler lowers them. - pub(crate) const ALL: [Self; 2] = [Self::V4, Self::V6]; + pub(super) const ALL: [Self; 2] = [Self::V4, Self::V6]; /// Returns the number of bits of an address of this family. pub(super) const fn bits(self) -> u32 { @@ -43,20 +43,20 @@ impl Family { } /// Returns every address of this family as one interval. - pub(crate) const fn everything(self) -> Interval { + pub(super) const fn everything(self) -> Interval { (0, self.last_address()) } } /// An inclusive interval of addresses of one family: its first and its last address. -pub(crate) type Interval = (u128, u128); +pub(super) type Interval = (u128, u128); /// A network of one family, without host bits. /// /// Networks order as the compiler sorts its output: by family, then by address, then by prefix /// length. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub(crate) struct Network { +pub(super) struct Network { /// Address family. family: Family, /// First address of the network, with every host bit clear. @@ -81,12 +81,12 @@ impl Network { } /// Returns the address family of the network. - pub(crate) fn family(self) -> Family { + pub(super) fn family(self) -> Family { self.family } /// Returns the interval of the network's addresses: its network and broadcast addresses. - pub(crate) fn interval(self) -> Interval { + pub(super) fn interval(self) -> Interval { (self.address, self.address | self.host_mask()) } @@ -101,7 +101,7 @@ impl Network { } /// Returns whether `other` lies within this network, as Python's `subnet_of` decides. - pub(crate) fn contains(self, other: Self) -> bool { + pub(super) fn contains(self, other: Self) -> bool { let (first, last) = self.interval(); let (other_first, other_last) = other.interval(); self.family == other.family && first <= other_first && last >= other_last @@ -165,7 +165,7 @@ fn write_ipv6(formatter: &mut fmt::Formatter<'_>, address: u128) -> fmt::Result /// Why [`parse_interface`] refused a string. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum InterfaceError { +pub(super) enum InterfaceError { /// The string is neither an IPv4 nor an IPv6 interface. Invalid, /// The string is an IPv6 interface that names a scope, such as `fe80::1%eth0/64`. @@ -174,7 +174,7 @@ pub(crate) enum InterfaceError { /// Parses `text` as Python's `ipaddress.ip_interface` does and returns its network, whose host /// bits are clear. -pub(crate) fn parse_interface(text: &str) -> Result { +pub(super) fn parse_interface(text: &str) -> Result { let text = text.as_bytes(); // `ip_interface` tries IPv4 first, and no string is both. if let Some(network) = parse_ipv4_interface(text) { @@ -405,7 +405,7 @@ fn parse_ipv6(text: &[u8]) -> Option { /// The networks that Python's `ipaddress.summarize_address_range` yields for an interval: the /// fewest networks that cover it exactly, from its first address. -pub(crate) struct Summary { +pub(super) struct Summary { /// Address family of the interval. family: Family, /// First address that the networks yielded so far do not cover, or `None` once they cover the @@ -417,7 +417,7 @@ pub(crate) struct Summary { /// Returns the networks that cover the addresses of `family` from `first` through `last`, as /// `ipaddress.summarize_address_range` yields them. -pub(crate) fn summarize(family: Family, (first, last): Interval) -> Summary { +pub(super) fn summarize(family: Family, (first, last): Interval) -> Summary { #[cfg(test)] tests::probe::SUMMARIES.with(|count| count.set(count.get() + 1)); Summary { @@ -455,7 +455,7 @@ impl Iterator for Summary { /// Returns the union of `intervals`, sorted, with overlapping and adjacent intervals merged, as /// `_merge_intervals` does. -pub(crate) fn merge(intervals: impl IntoIterator) -> Vec { +pub(super) fn merge(intervals: impl IntoIterator) -> Vec { let mut sorted: Vec = intervals.into_iter().collect(); #[cfg(test)] tests::probe::MERGED_INTERVALS.with(|count| count.set(count.get() + sorted.len())); @@ -474,7 +474,7 @@ pub(crate) fn merge(intervals: impl IntoIterator) -> Vec Vec { +pub(super) fn subtract(sources: &[Interval], exclusions: &[Interval]) -> Vec { let mut remaining = Vec::new(); let mut first_exclusion = 0; for &(source_start, source_end) in sources { diff --git a/nvx/src/host/network/lowering.rs b/nvx/src/host/network/lowering.rs index 1ad6375f2..115b0419f 100644 --- a/nvx/src/host/network/lowering.rs +++ b/nvx/src/host/network/lowering.rs @@ -30,7 +30,7 @@ use crate::error::{Error, Result}; /// /// Protocols order by name, as the compiler sorts them. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub(crate) enum NativeProtocol { +pub(super) enum NativeProtocol { /// ICMP, or ICMPv6 for IPv6 networks. Icmp, /// TCP. @@ -41,7 +41,7 @@ pub(crate) enum NativeProtocol { impl NativeProtocol { /// Returns OpenVMM's name of the protocol. - pub(crate) const fn as_str(self) -> &'static str { + pub(super) const fn as_str(self) -> &'static str { match self { Self::Icmp => "icmp", Self::Tcp => "tcp", @@ -52,7 +52,7 @@ impl NativeProtocol { /// The protocol of a rule as written: a native protocol, or `any`. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum RuleProtocol { +pub(super) enum RuleProtocol { /// TCP. Tcp, /// UDP. @@ -65,7 +65,7 @@ pub(crate) enum RuleProtocol { impl RuleProtocol { /// Returns the native protocol, or `None` for `any`. - pub(crate) fn native(self) -> Option { + pub(super) fn native(self) -> Option { match self { Self::Tcp => Some(NativeProtocol::Tcp), Self::Udp => Some(NativeProtocol::Udp), @@ -87,33 +87,33 @@ impl RuleProtocol { } /// An inclusive range of ports. -pub(crate) type PortRange = (u16, u16); +pub(super) type PortRange = (u16, u16); /// Traffic that a rule selects at its destinations: a protocol, or every protocol, and a range of /// its ports, or every port. -pub(crate) type Selector = (Option, Option); +pub(super) type Selector = (Option, Option); /// A value that should be a port number. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum PortValue { +pub(super) enum PortValue { /// An integer, saturated to the range of `i128`. Integer(i128), } /// A rule of one action, as the compiler lowers it. #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Rule { +pub(super) struct Rule { /// Address family of the rule's addresses. - pub(crate) family: Family, + pub(super) family: Family, /// Addresses that the rule selects: sorted, disjoint intervals of `family`, possibly none. /// /// The rules of one rule's selectors share them, as Python's share one tuple, so that many /// destinations times many selectors cost no more memory than they take to write. - pub(crate) addresses: Rc<[Interval]>, + pub(super) addresses: Rc<[Interval]>, /// Protocol that the rule selects, or `None` for every protocol. - pub(crate) protocol: Option, + pub(super) protocol: Option, /// Ports that the rule selects, or `None` for every port. Only TCP and UDP rules have ports. - pub(crate) ports: Option, + pub(super) ports: Option, } /// Returns an [`ErrorKind::Unsupported`](crate::ErrorKind::Unsupported) error with `message`. @@ -123,7 +123,7 @@ fn invalid(message: impl Into) -> Error { /// Parses the CIDR `value` of the field `description`, as `_network` does: the network of an /// IPv4 or IPv6 interface without an IPv6 scope. -pub(crate) fn network(value: &str, description: &str) -> Result { +pub(super) fn network(value: &str, description: &str) -> Result { address::parse_interface(value).map_err(|error| { invalid(match error { InterfaceError::Invalid => { @@ -139,7 +139,7 @@ pub(crate) fn network(value: &str, description: &str) -> Result { /// Returns the family of the destination `description`, whose network is `parent`, and the /// addresses that it holds outside `exceptions`, as `_subtract_exclusions` does. Each exception, /// which is parsed when it is read, must lie within `parent`. -pub(crate) fn destination( +pub(super) fn destination( parent: Network, exceptions: impl IntoIterator>, description: &str, @@ -162,7 +162,7 @@ pub(crate) fn destination( /// Returns the addresses of each family that `destinations`, each a family and its addresses, /// select, for the families that have destinations, as an MXC rule's `to` selects them. -pub(crate) fn group_destinations( +pub(super) fn group_destinations( destinations: &[(Family, Vec)], ) -> Vec<(Family, Vec)> { Family::ALL @@ -209,7 +209,7 @@ fn port_range(start: PortValue, end: Option, description: &str) -> Re /// Returns the selectors of the port fields, `port` and the optional `endPort`, of /// `description`, an MXC port selector or a legacy flat rule whose protocol is valid, as the rest /// of `_parse_mxc_port` does. ICMP takes no port. -pub(crate) fn port_selectors( +pub(super) fn port_selectors( protocol: RuleProtocol, port: Option, end_port: Option, @@ -238,7 +238,7 @@ pub(crate) fn port_selectors( /// `destinations`, which the rules share, as `_parse_mxc_rule` and `_parse_legacy_rule` build /// them. Without destinations, the rules select every address of both families, and without /// selectors every protocol and port. -pub(crate) fn combine( +pub(super) fn combine( destinations: Option)>>, selectors: Option>, ) -> Vec { @@ -278,14 +278,14 @@ fn too_many_rules(category: &str) -> Error { /// Lowers the rules of the action `category` into native rules, in the order in which the /// compiler sorts them, as `_compile_category` does once it has parsed them. -pub(crate) fn lower(rules: &[Rule], category: &str) -> Result> { +pub(super) fn lower(rules: &[Rule], category: &str) -> Result> { lower_within(rules, category, MAX_RULES) } /// Lowers the rules of the action `category` as [`lower`] does, but fails once they need more /// than `limit` native rules rather than [`MAX_RULES`]: tests lift the limit to learn how many /// native rules an action needs. -pub(crate) fn lower_within(rules: &[Rule], category: &str, limit: usize) -> Result> { +pub(super) fn lower_within(rules: &[Rule], category: &str, limit: usize) -> Result> { // A native rule matches only its own family, so each family is lowered on its own, within // the action's shared limit. let mut lowered: Vec = Vec::new();