Skip to content

Commit

Permalink
migrate net/prefix types to oxnet (#266)
Browse files Browse the repository at this point in the history
In addition, this cleans up some of the features to ensure that illumos-only dependencies aren't pulled in when they aren't needed.
  • Loading branch information
ahl authored Jun 25, 2024
1 parent f22b10f commit 77749e3
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 530 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mg-common = { path = "mg-common" }
chrono = { version = "0.4.38", features = ["serde"] }
oximeter = { git = "https://github.com/oxidecomputer/omicron", branch = "main"}
oximeter-producer = { git = "https://github.com/oxidecomputer/omicron", branch = "main"}
oxnet = { git = "https://github.com/oxidecomputer/oxnet" }
omicron-common = { git = "https://github.com/oxidecomputer/omicron", branch = "main"}
internal-dns = { git = "https://github.com/oxidecomputer/omicron", branch = "main"}
uuid = { version = "1.8", features = ["serde", "v4"] }
Expand Down
7 changes: 4 additions & 3 deletions ddm-admin-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ version = "0.1.0"
edition = "2021"

[dependencies]
oxnet.workspace = true
percent-encoding.workspace = true
progenitor.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
slog.workspace = true
percent-encoding.workspace = true
reqwest.workspace = true
progenitor.workspace = true
uuid.workspace = true
3 changes: 0 additions & 3 deletions ddm-admin-client/build.rs

This file was deleted.

63 changes: 3 additions & 60 deletions ddm-admin-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,14 @@ progenitor::generate_api!(
"body" => ?&request.body(),
);
}),
crates = {
"oxnet" = "0.1.0",
},
post_hook = (|log: &slog::Logger, result: &Result<_, _>| {
slog::trace!(log, "client response"; "result" => ?result);
})
);

impl Copy for types::Ipv4Prefix {}
impl Copy for types::Ipv6Prefix {}
impl Copy for types::IpPrefix {}

impl std::cmp::PartialEq for types::Ipv4Prefix {
fn eq(&self, other: &Self) -> bool {
self.addr.eq(&other.addr) && self.len.eq(&other.len)
}
}

impl std::cmp::Eq for types::Ipv4Prefix {}

impl std::hash::Hash for types::Ipv4Prefix {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.addr.hash(state);
self.len.hash(state);
}
}

impl std::cmp::PartialEq for types::Ipv6Prefix {
fn eq(&self, other: &Self) -> bool {
self.addr.eq(&other.addr) && self.len.eq(&other.len)
}
}

impl std::cmp::Eq for types::Ipv6Prefix {}

impl std::hash::Hash for types::Ipv6Prefix {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.addr.hash(state);
self.len.hash(state);
}
}

impl std::cmp::PartialEq for types::IpPrefix {
fn eq(&self, other: &Self) -> bool {
match self {
types::IpPrefix::V4(x) => match other {
types::IpPrefix::V4(y) => x.eq(y),
_ => false,
},
types::IpPrefix::V6(x) => match other {
types::IpPrefix::V6(y) => x.eq(y),
_ => false,
},
}
}
}

impl std::hash::Hash for types::IpPrefix {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
types::IpPrefix::V4(x) => x.hash(state),
types::IpPrefix::V6(x) => x.hash(state),
}
}
}

impl std::cmp::Eq for types::IpPrefix {}

impl std::cmp::PartialEq for types::TunnelOrigin {
fn eq(&self, other: &Self) -> bool {
self.overlay_prefix.eq(&other.overlay_prefix)
Expand Down
1 change: 1 addition & 0 deletions ddm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ chrono.workspace = true
omicron-common.workspace = true
oximeter.workspace = true
oximeter-producer.workspace = true
oxnet.workspace = true
uuid.workspace = true
9 changes: 5 additions & 4 deletions ddm/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use dropshot::HttpServerStarter;
use dropshot::Path;
use dropshot::RequestContext;
use dropshot::TypedBody;
use mg_common::net::{Ipv6Prefix, TunnelOrigin};
use mg_common::net::TunnelOrigin;
use oxnet::Ipv6Net;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use slog::{error, info, warn, Logger};
Expand Down Expand Up @@ -125,7 +126,7 @@ type PrefixMap = BTreeMap<Ipv6Addr, HashSet<PathVector>>;
#[endpoint { method = GET, path = "/originated" }]
async fn get_originated(
ctx: RequestContext<Arc<Mutex<HandlerContext>>>,
) -> Result<HttpResponseOk<HashSet<Ipv6Prefix>>, HttpError> {
) -> Result<HttpResponseOk<HashSet<Ipv6Net>>, HttpError> {
let ctx = ctx.context().lock().unwrap();
let originated = ctx
.db
Expand Down Expand Up @@ -186,7 +187,7 @@ async fn get_tunnel_endpoints(
#[endpoint { method = PUT, path = "/prefix" }]
async fn advertise_prefixes(
ctx: RequestContext<Arc<Mutex<HandlerContext>>>,
request: TypedBody<HashSet<Ipv6Prefix>>,
request: TypedBody<HashSet<Ipv6Net>>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let ctx = ctx.context().lock().unwrap();
let prefixes = request.into_inner();
Expand Down Expand Up @@ -258,7 +259,7 @@ async fn advertise_tunnel_endpoints(
#[endpoint { method = DELETE, path = "/prefix" }]
async fn withdraw_prefixes(
ctx: RequestContext<Arc<Mutex<HandlerContext>>>,
request: TypedBody<HashSet<Ipv6Prefix>>,
request: TypedBody<HashSet<Ipv6Net>>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let ctx = ctx.context().lock().unwrap();
let prefixes = request.into_inner();
Expand Down
66 changes: 23 additions & 43 deletions ddm/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use mg_common::net::{IpPrefix, Ipv6Prefix, TunnelOrigin};
use mg_common::net::TunnelOrigin;
use oxnet::{IpNet, Ipv6Net};
use schemars::{JsonSchema, JsonSchema_repr};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
Expand Down Expand Up @@ -105,10 +106,7 @@ impl Db {
}
}

pub fn originate(
&self,
prefixes: &HashSet<Ipv6Prefix>,
) -> Result<(), Error> {
pub fn originate(&self, prefixes: &HashSet<Ipv6Net>) -> Result<(), Error> {
let tree = self.persistent_data.open_tree(ORIGINATE)?;
for p in prefixes {
tree.insert(p.db_key(), "")?;
Expand All @@ -130,7 +128,7 @@ impl Db {
Ok(())
}

pub fn originated(&self) -> Result<HashSet<Ipv6Prefix>, Error> {
pub fn originated(&self) -> Result<HashSet<Ipv6Net>, Error> {
let tree = self.persistent_data.open_tree(ORIGINATE)?;
let result = tree
.scan_prefix(vec![])
Expand All @@ -145,7 +143,7 @@ impl Db {
return None;
}
};
Some(match Ipv6Prefix::from_db_key(&key) {
Some(match Ipv6Net::from_db_key(&key) {
Ok(item) => item,
Err(e) => {
error!(
Expand Down Expand Up @@ -200,10 +198,7 @@ impl Db {
Ok(self.originated_tunnel()?.len())
}

pub fn withdraw(
&self,
prefixes: &HashSet<Ipv6Prefix>,
) -> Result<(), Error> {
pub fn withdraw(&self, prefixes: &HashSet<Ipv6Net>) -> Result<(), Error> {
let tree = self.persistent_data.open_tree(ORIGINATE)?;
for p in prefixes {
tree.remove(p.db_key())?;
Expand Down Expand Up @@ -269,7 +264,7 @@ impl Db {

pub fn routes_by_vector(
&self,
dst: Ipv6Prefix,
dst: Ipv6Net,
nexthop: Ipv6Addr,
) -> Vec<Route> {
let data = self.data.lock().unwrap();
Expand Down Expand Up @@ -356,7 +351,7 @@ pub struct TunnelRoute {
Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema,
)]
pub struct Route {
pub destination: Ipv6Prefix,
pub destination: Ipv6Net,
pub nexthop: Ipv6Addr,
pub ifname: String,
pub path: Vec<String>,
Expand Down Expand Up @@ -390,7 +385,7 @@ impl EffectiveTunnelRouteSet {
pub fn effective_route_set(
full: &HashSet<TunnelRoute>,
) -> HashSet<TunnelRoute> {
let mut sets = HashMap::<IpPrefix, EffectiveTunnelRouteSet>::new();
let mut sets = HashMap::<IpNet, EffectiveTunnelRouteSet>::new();
for x in full.iter() {
match sets.get_mut(&x.origin.overlay_prefix) {
Some(set) => {
Expand Down Expand Up @@ -458,24 +453,25 @@ trait DbKey: Sized {
fn from_db_key(v: &[u8]) -> Result<Self, Error>;
}

impl DbKey for Ipv6Prefix {
impl DbKey for Ipv6Net {
fn db_key(&self) -> Vec<u8> {
let mut buf: Vec<u8> = self.addr.octets().into();
buf.push(self.len);
let mut buf: Vec<u8> = self.addr().octets().into();
buf.push(self.width());
buf
}

fn from_db_key(v: &[u8]) -> Result<Self, Error> {
if v.len() < 17 {
Err(Error::DbKey(format!(
"buffer to short for prefix 6 key {} < 17",
"buffer too short for prefix 6 key {} < 17",
v.len()
)))
} else {
Ok(Self {
addr: Ipv6Addr::from(<[u8; 16]>::try_from(&v[..16]).unwrap()),
len: v[16],
})
Self::new(
Ipv6Addr::from(<[u8; 16]>::try_from(&v[..16]).unwrap()),
v[16],
)
.map_err(|e| Error::DbKey(e.to_string()))
}
}
}
Expand All @@ -494,7 +490,6 @@ impl From<crate::db::TunnelRoute> for TunnelOrigin {
#[cfg(test)]
mod test {
use super::*;
use mg_common::net::{IpPrefix, Ipv4Prefix};
use pretty_assertions::assert_eq;
use std::collections::HashSet;

Expand All @@ -503,10 +498,7 @@ mod test {
let mut before = HashSet::<TunnelRoute>::new();
before.insert(TunnelRoute {
origin: TunnelOrigin {
overlay_prefix: IpPrefix::V4(Ipv4Prefix {
addr: "0.0.0.0".parse().unwrap(),
len: 0,
}),
overlay_prefix: "0.0.0.0/0".parse().unwrap(),
boundary_addr: "fd00:a::1".parse().unwrap(),
vni: 99,
metric: 0,
Expand All @@ -515,10 +507,7 @@ mod test {
});
before.insert(TunnelRoute {
origin: TunnelOrigin {
overlay_prefix: IpPrefix::V4(Ipv4Prefix {
addr: "0.0.0.0".parse().unwrap(),
len: 0,
}),
overlay_prefix: "0.0.0.0/0".parse().unwrap(),
boundary_addr: "fd00:b::1".parse().unwrap(),
vni: 99,
metric: 0,
Expand All @@ -530,10 +519,7 @@ mod test {
let mut after = HashSet::<TunnelRoute>::new();
after.insert(TunnelRoute {
origin: TunnelOrigin {
overlay_prefix: IpPrefix::V4(Ipv4Prefix {
addr: "0.0.0.0".parse().unwrap(),
len: 0,
}),
overlay_prefix: "0.0.0.0/0".parse().unwrap(),
boundary_addr: "fd00:a::1".parse().unwrap(),
vni: 99,
metric: 0,
Expand All @@ -542,10 +528,7 @@ mod test {
});
after.insert(TunnelRoute {
origin: TunnelOrigin {
overlay_prefix: IpPrefix::V4(Ipv4Prefix {
addr: "0.0.0.0".parse().unwrap(),
len: 0,
}),
overlay_prefix: "0.0.0.0/0".parse().unwrap(),
boundary_addr: "fd00:b::1".parse().unwrap(),
vni: 99,
metric: 100,
Expand All @@ -570,10 +553,7 @@ mod test {
let mut expected_del = HashSet::<TunnelRoute>::new();
expected_del.insert(TunnelRoute {
origin: TunnelOrigin {
overlay_prefix: IpPrefix::V4(Ipv4Prefix {
addr: "0.0.0.0".parse().unwrap(),
len: 0,
}),
overlay_prefix: "0.0.0.0/0".parse().unwrap(),
boundary_addr: "fd00:a::1".parse().unwrap(),
vni: 99,
metric: 0,
Expand Down
Loading

0 comments on commit 77749e3

Please sign in to comment.