Skip to content

Commit

Permalink
WIP: Initial DB Model for NAT RPW
Browse files Browse the repository at this point in the history
  • Loading branch information
internet-diglett committed Jul 31, 2023
1 parent c79d1bf commit 8e1ca66
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/src/api/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ pub enum ResourceType {
UpdateableComponent,
UserBuiltin,
Zpool,
Ipv4NatEntry,
}

// IDENTITY METADATA
Expand Down
29 changes: 29 additions & 0 deletions common/src/sql/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,35 @@ CREATE UNIQUE INDEX ON omicron.public.switch_port_settings_group (
) WHERE
time_deleted IS NULL;

CREATE SEQUENCE nat_gen START 1 INCREMENT 1;

CREATE TABLE omicron.public.ipv4_nat_entry (
id UUID PRIMARY KEY,
external_address INET NOT NULL,
first_port INT4 NOT NULL,
last_port INT4 NOT NULL,
sled_address INET NOT NULL,
vni INT4 NOT NULL,
mac INT8 NOT NULL,
gen INT8 NOT NULL DEFAULT nextval('nat_gen') ON UPDATE nextval('nat_gen'),
time_created TIMESTAMPTZ NOT NULL DEFAULT now(),
time_deleted TIMESTAMPTZ
);

CREATE UNIQUE INDEX ON omicron.public.ipv4_nat_entry (
gen
)
STORING (
external_address,
first_port,
last_port,
sled_address,
vni,
mac,
time_created,
time_deleted
);

CREATE TABLE omicron.public.switch_port_settings (
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
Expand Down
46 changes: 46 additions & 0 deletions nexus/db-model/src/ipv4_nat_entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use super::MacAddr;
use crate::{schema::ipv4_nat_entry, SqlU16, SqlU32, Vni};
use chrono::{DateTime, Utc};
use uuid::Uuid;

/// Database representation of an Ipv4 NAT Entry.
#[derive(Insertable, Debug, Clone)]
#[diesel(table_name = ipv4_nat_entry)]
pub struct NewIpv4NatEntry {
pub id: Uuid,
pub external_address: ipnetwork::IpNetwork,
pub first_port: SqlU16,
pub last_port: SqlU16,
pub sled_address: ipnetwork::IpNetwork,
pub vni: Vni,
pub mac: MacAddr,
}

#[derive(Queryable, Debug, Clone, Selectable)]
#[diesel(table_name = ipv4_nat_entry)]
pub struct Ipv4NatEntry {
pub id: Uuid,
pub external_address: ipnetwork::IpNetwork,
pub first_port: SqlU16,
pub last_port: SqlU16,
pub sled_address: ipnetwork::IpNetwork,
pub vni: Vni,
pub mac: MacAddr,
pub gen: SqlU32,
pub time_created: DateTime<Utc>,
pub time_deleted: Option<DateTime<Utc>>,
}

impl Ipv4NatEntry {
pub fn first_port(&self) -> u16 {
self.first_port.into()
}

pub fn last_port(&self) -> u16 {
self.first_port.into()
}

pub fn gen(&self) -> u32 {
self.gen.into()
}
}
2 changes: 2 additions & 0 deletions nexus/db-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod system_update;
// These actually represent subqueries, not real table.
// However, they must be defined in the same crate as our tables
// for join-based marker trait generation.
mod ipv4_nat_entry;
pub mod queries;
mod rack;
mod region;
Expand Down Expand Up @@ -119,6 +120,7 @@ pub use instance::*;
pub use instance_cpu_count::*;
pub use instance_state::*;
pub use ip_pool::*;
pub use ipv4_nat_entry::*;
pub use ipv4net::*;
pub use ipv6::*;
pub use ipv6net::*;
Expand Down
15 changes: 15 additions & 0 deletions nexus/db-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@ table! {
}
}

table! {
ipv4_nat_entry (id) {
id -> Uuid,
external_address -> Inet,
first_port -> Int4,
last_port -> Int4,
sled_address -> Inet,
vni -> Int4,
mac -> Int8,
gen -> Int8,
time_created -> Timestamptz,
time_deleted -> Nullable<Timestamptz>,
}
}

table! {
external_ip (id) {
id -> Uuid,
Expand Down
Loading

0 comments on commit 8e1ca66

Please sign in to comment.