Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IncompleteNetworkInterface: allow user-specified explicit slot #5065

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions nexus/db-model/src/network_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ use nexus_types::identity::Resource;
use omicron_common::api::external;
use uuid::Uuid;

/// The max number of interfaces that may be associated with a resource,
/// e.g., instance or service.
///
/// RFD 135 caps instances at 8 interfaces and we use the same limit for
/// all types of interfaces for simplicity.
pub const MAX_NICS_PER_INSTANCE: usize = 8;

impl_enum_type! {
#[derive(SqlType, QueryId, Debug, Clone, Copy)]
#[diesel(postgres_type(name = "network_interface_kind", schema = "public"))]
Expand Down Expand Up @@ -210,9 +217,11 @@ pub struct IncompleteNetworkInterface {
pub subnet: VpcSubnet,
pub ip: Option<std::net::IpAddr>,
pub mac: Option<external::MacAddr>,
pub slot: Option<u8>,
}

impl IncompleteNetworkInterface {
#[allow(clippy::too_many_arguments)]
fn new(
interface_id: Uuid,
kind: NetworkInterfaceKind,
Expand All @@ -221,24 +230,36 @@ impl IncompleteNetworkInterface {
identity: external::IdentityMetadataCreateParams,
ip: Option<std::net::IpAddr>,
mac: Option<external::MacAddr>,
slot: Option<u8>,
) -> Result<Self, external::Error> {
if let Some(ip) = ip {
subnet.check_requestable_addr(ip)?;
};
match (mac, kind) {
(Some(mac), NetworkInterfaceKind::Instance) if !mac.is_guest() => {
return Err(external::Error::invalid_request(&format!(
"invalid MAC address {} for guest NIC",
mac
)));
if let Some(mac) = mac {
match kind {
NetworkInterfaceKind::Instance => {
if !mac.is_guest() {
return Err(external::Error::invalid_request(format!(
"invalid MAC address {mac} for guest NIC",
)));
}
}
NetworkInterfaceKind::Service => {
if !mac.is_system() {
return Err(external::Error::invalid_request(format!(
"invalid MAC address {mac} for service NIC",
)));
}
}
}
(Some(mac), NetworkInterfaceKind::Service) if !mac.is_system() => {
return Err(external::Error::invalid_request(&format!(
"invalid MAC address {} for service NIC",
mac
}
if let Some(slot) = slot {
if usize::from(slot) >= MAX_NICS_PER_INSTANCE {
return Err(external::Error::invalid_request(format!(
"invalid slot {slot} for NIC (max slot = {})",
MAX_NICS_PER_INSTANCE - 1,
)));
}
_ => {}
}
let identity = NetworkInterfaceIdentity::new(interface_id, identity);
Ok(IncompleteNetworkInterface {
Expand All @@ -248,6 +269,7 @@ impl IncompleteNetworkInterface {
subnet,
ip,
mac,
slot,
})
}

Expand All @@ -266,6 +288,7 @@ impl IncompleteNetworkInterface {
identity,
ip,
None,
None,
)
}

Expand All @@ -274,17 +297,19 @@ impl IncompleteNetworkInterface {
service_id: Uuid,
subnet: VpcSubnet,
identity: external::IdentityMetadataCreateParams,
ip: Option<std::net::IpAddr>,
mac: Option<external::MacAddr>,
ip: std::net::IpAddr,
mac: external::MacAddr,
slot: u8,
) -> Result<Self, external::Error> {
Self::new(
interface_id,
NetworkInterfaceKind::Service,
service_id,
subnet,
identity,
ip,
mac,
Some(ip),
Some(mac),
Some(slot),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use omicron_common::api::external::SemverVersion;
///
/// This should be updated whenever the schema is changed. For more details,
/// refer to: schema/crdb/README.adoc
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(33, 0, 1);
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(34, 0, 0);

table! {
disk (id) {
Expand Down
19 changes: 15 additions & 4 deletions nexus/db-queries/src/db/datastore/rack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ impl DataStore {
name: nic.name.clone(),
description: format!("{} service vNIC", service.kind),
},
Some(nic.ip),
Some(nic.mac),
nic.ip,
nic.mac,
nic.slot,
)
.map_err(|e| RackInitError::AddingNic(e))?;
Some((db_ip, db_nic))
Expand All @@ -504,8 +505,9 @@ impl DataStore {
name: nic.name.clone(),
description: format!("{} service vNIC", service.kind),
},
Some(nic.ip),
Some(nic.mac),
nic.ip,
nic.mac,
nic.slot,
)
.map_err(|e| RackInitError::AddingNic(e))?;
Some((db_ip, db_nic))
Expand Down Expand Up @@ -1159,6 +1161,7 @@ mod test {
name: "external-dns".parse().unwrap(),
ip: external_dns_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand All @@ -1178,6 +1181,7 @@ mod test {
name: "ntp1".parse().unwrap(),
ip: ntp1_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand All @@ -1193,6 +1197,7 @@ mod test {
name: "nexus".parse().unwrap(),
ip: nexus_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand All @@ -1212,6 +1217,7 @@ mod test {
name: "ntp2".parse().unwrap(),
ip: ntp2_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand Down Expand Up @@ -1406,6 +1412,7 @@ mod test {
name: "nexus1".parse().unwrap(),
ip: nexus_pip1.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand All @@ -1421,6 +1428,7 @@ mod test {
name: "nexus2".parse().unwrap(),
ip: nexus_pip2.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand Down Expand Up @@ -1603,6 +1611,7 @@ mod test {
name: "nexus".parse().unwrap(),
ip: nexus_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
}];
Expand Down Expand Up @@ -1662,6 +1671,7 @@ mod test {
name: "external-dns".parse().unwrap(),
ip: external_dns_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand All @@ -1677,6 +1687,7 @@ mod test {
name: "nexus".parse().unwrap(),
ip: nexus_pip.into(),
mac: macs.next().unwrap(),
slot: 0,
},
},
},
Expand Down
Loading
Loading