Skip to content

Commit

Permalink
mgd: save generated tep to rdb
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgoodfellow committed Jan 12, 2024
1 parent 2d23195 commit 1b20200
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
26 changes: 19 additions & 7 deletions mgd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ async fn run(args: RunArgs) {
let db = rdb::Db::new(&format!("{}/rdb", args.data_dir), log.clone())
.expect("open datastore file");

// creat the randomized ULA fdxx:xxxx:xxxx:xxxx::1 as a tunnel endpoint
let mut rng = rand::thread_rng();
let mut r = [0u8; 7];
r.try_fill(&mut rng).unwrap();
let tep_ula = Ipv6Addr::from([
0xfd, r[0], r[1], r[2], r[3], r[4], r[5], r[6], 0, 0, 0, 0, 0, 0, 0, 1,
]);
let tep_ula = get_tunnel_endpoint_ula(&db);

let context = Arc::new(HandlerContext {
tep: tep_ula,
Expand Down Expand Up @@ -172,3 +166,21 @@ fn start_bgp_routers(
.unwrap_or_else(|_| panic!("add BGP neighbor {nbr:#?}"));
}
}

fn get_tunnel_endpoint_ula(db: &rdb::Db) -> Ipv6Addr {
if let Some(addr) = db.get_tep_addr().unwrap() {
return addr;
}

// creat the randomized ULA fdxx:xxxx:xxxx:xxxx::1 as a tunnel endpoint
let mut rng = rand::thread_rng();
let mut r = [0u8; 7];
r.try_fill(&mut rng).unwrap();
let tep_ula = Ipv6Addr::from([
0xfd, r[0], r[1], r[2], r[3], r[4], r[5], r[6], 0, 0, 0, 0, 0, 0, 0, 1,
]);

db.set_tep_addr(tep_ula).unwrap();

tep_ula
}
32 changes: 31 additions & 1 deletion rdb/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::types::*;
use mg_common::{lock, read_lock, write_lock};
use slog::{error, info, Logger};
use std::collections::{HashMap, HashSet};
use std::net::IpAddr;
use std::net::{IpAddr, Ipv6Addr};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex, RwLock};
Expand All @@ -31,6 +31,12 @@ const BGP_ROUTER: &str = "bgp_router";
/// information.
const BGP_NEIGHBOR: &str = "bgp_neighbor";

/// The handle used to open a persistent key-value tree for settings
/// information.
const SETTINGS: &str = "settings";
/// Key used in settings tree for tunnel endpoint setting
const TEP_KEY: &str = "tep";

/// The central routing information base. Both persistent an volatile route
/// information is managed through this structure.
#[derive(Clone)]
Expand Down Expand Up @@ -373,4 +379,28 @@ impl Db {
gen,
))
}

pub fn get_tep_addr(&self) -> Result<Option<Ipv6Addr>, Error> {
let tree = self.persistent.open_tree(SETTINGS)?;
let result = tree.get(TEP_KEY)?;
let value = match result {
Some(value) => value,
None => return Ok(None),
};
let octets: [u8; 16] = (*value).try_into().map_err(|_| {
Error::DbValue(format!(
"rdb: tep length error exepcted 16 bytes found {}",
value.len(),
))
})?;

Ok(Some(Ipv6Addr::from(octets)))
}

pub fn set_tep_addr(&self, addr: Ipv6Addr) -> Result<(), Error> {
let tree = self.persistent.open_tree(SETTINGS)?;
let key = addr.octets();
tree.insert(TEP_KEY, &key)?;
Ok(())
}
}
3 changes: 3 additions & 0 deletions rdb/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub enum Error {

#[error("db key error{0}")]
DbKey(String),

#[error("db value error{0}")]
DbValue(String),
}

0 comments on commit 1b20200

Please sign in to comment.