Skip to content
Draft
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
42 changes: 20 additions & 22 deletions dpd/src/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ use crate::switch_port::FixedSideDevice;
use crate::switch_port::LedState;
use crate::transceivers::PowerState;
use crate::types::DpdError;
use crate::{Switch, arp, loopback, nat, ports, route};
use crate::{Switch, arp, loopback, ports, route};
use common::attached_subnet::AttachedSubnetEntry;
use common::nat::{Ipv4Nat, Ipv6Nat};
use common::network::{InstanceTarget, MacAddr, NatTarget};
Expand Down Expand Up @@ -1422,8 +1422,7 @@ impl DpdApi for DpdApiImpl {
WhichPage::Next(Ipv6Token { ip }) => Some(*ip),
};

let entries = nat::get_ipv6_addrs_range(
switch,
let entries = switch.nat.get_addrs_range(
last_addr,
usize::try_from(max).expect("invalid usize"),
);
Expand All @@ -1449,8 +1448,7 @@ impl DpdApi for DpdApiImpl {
WhichPage::Next(NatToken { port }) => Some(*port),
};

let entries = nat::get_ipv6_mappings_range(
switch,
let entries = switch.nat.get_mappings_range(
params.ipv6,
port,
usize::try_from(max).expect("invalid usize"),
Expand All @@ -1469,8 +1467,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseOk<NatTarget>, HttpError> {
let switch: &Switch = rqctx.context();
let params = path.into_inner();
match nat::get_ipv6_mapping(switch, params.ipv6, params.low, params.low)
{
match switch.nat.get_mapping(params.ipv6, params.low, params.low) {
Ok(tgt) => Ok(HttpResponseOk(tgt)),
Err(e) => Err(e.into()),
}
Expand All @@ -1483,7 +1480,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let switch: &Switch = rqctx.context();
let params = path.into_inner();
match nat::set_ipv6_mapping(
match switch.nat.add_mapping(
switch,
params.ipv6,
params.low,
Expand All @@ -1501,7 +1498,9 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseDeleted, HttpError> {
let switch: &Switch = rqctx.context();
let params = path.into_inner();
nat::clear_ipv6_mapping(switch, params.ipv6, params.low, params.low)
switch
.nat
.remove_mapping(switch, params.ipv6, params.low, params.low)
.map(|_| HttpResponseDeleted())
.map_err(HttpError::from)
}
Expand All @@ -1511,7 +1510,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let switch: &Switch = rqctx.context();

match nat::reset_ipv6(switch) {
match switch.nat.reset::<Ipv6Addr>(switch) {
Ok(_) => Ok(HttpResponseUpdatedNoContent()),
Err(e) => Err(e.into()),
}
Expand All @@ -1530,8 +1529,7 @@ impl DpdApi for DpdApiImpl {
WhichPage::Next(Ipv4Token { ip }) => Some(*ip),
};

let entries = nat::get_ipv4_addrs_range(
switch,
let entries = switch.nat.get_addrs_range(
last_addr,
usize::try_from(max).expect("invalid usize"),
);
Expand All @@ -1558,8 +1556,7 @@ impl DpdApi for DpdApiImpl {
WhichPage::Next(NatToken { port }) => Some(*port),
};

let entries = nat::get_ipv4_mappings_range(
switch,
let entries = switch.nat.get_mappings_range(
params.ipv4,
port,
usize::try_from(max).expect("invalid usize"),
Expand All @@ -1578,8 +1575,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseOk<NatTarget>, HttpError> {
let switch: &Switch = rqctx.context();
let params = path.into_inner();
match nat::get_ipv4_mapping(switch, params.ipv4, params.low, params.low)
{
match switch.nat.get_mapping(params.ipv4, params.low, params.low) {
Ok(tgt) => Ok(HttpResponseOk(tgt)),
Err(e) => Err(e.into()),
}
Expand All @@ -1592,7 +1588,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let switch: &Switch = rqctx.context();
let params = path.into_inner();
match nat::set_ipv4_mapping(
match switch.nat.add_mapping(
switch,
params.ipv4,
params.low,
Expand All @@ -1610,7 +1606,9 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseDeleted, HttpError> {
let switch: &Switch = rqctx.context();
let params = path.into_inner();
nat::clear_ipv4_mapping(switch, params.ipv4, params.low, params.low)
switch
.nat
.remove_mapping(switch, params.ipv4, params.low, params.low)
.map(|_| HttpResponseDeleted())
.map_err(HttpError::from)
}
Expand All @@ -1620,7 +1618,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let switch: &Switch = rqctx.context();

match nat::reset_ipv4(switch) {
match switch.nat.reset::<Ipv4Addr>(switch) {
Ok(_) => Ok(HttpResponseUpdatedNoContent()),
Err(e) => Err(e.into()),
}
Expand Down Expand Up @@ -1744,11 +1742,11 @@ impl DpdApi for DpdApiImpl {
error!(switch.log, "failed to clear all link state: {:?}", e);
err = Some(e);
}
if let Err(e) = nat::reset_ipv4(switch) {
if let Err(e) = switch.nat.reset::<Ipv4Addr>(switch) {
error!(switch.log, "failed to reset ipv4 nat table: {:?}", e);
err = Some(e);
}
if let Err(e) = nat::reset_ipv6(switch) {
if let Err(e) = switch.nat.reset::<Ipv6Addr>(switch) {
error!(switch.log, "failed to reset ipv6 nat table: {:?}", e);
err = Some(e);
}
Expand Down Expand Up @@ -1913,7 +1911,7 @@ impl DpdApi for DpdApiImpl {
) -> Result<HttpResponseOk<i64>, HttpError> {
let switch = rqctx.context();

Ok(HttpResponseOk(nat::get_nat_generation(switch)))
Ok(HttpResponseOk(switch.nat.generation()))
}

async fn nat_trigger_update(
Expand Down
6 changes: 4 additions & 2 deletions dpd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub struct Switch {
pub links: Mutex<link::LinkMap>,
pub routes: TokioMutex<route::RouteData>,
pub arp: Mutex<arp::ArpData>,
pub nat: Mutex<nat::NatData>,
pub nat: nat::Nat,
pub attached_subnet: Mutex<attached_subnet::AttachedSubnetData>,
pub loopback: Mutex<loopback::LoopbackData>,
pub identifiers: Mutex<Option<SwitchIdentifiers>>,
Expand Down Expand Up @@ -298,6 +298,8 @@ impl Switch {
let ws_log = log.new(slog::o!("unit" => "workflow_server"));
let workflow_server = rpw::WorkflowServer::new(ws_log);

let nat = nat::Nat::new(&log);

Ok(Switch {
start_time,
config: Mutex::new(config),
Expand All @@ -308,7 +310,7 @@ impl Switch {
counters,
routes: TokioMutex::new(route_data),
arp: Mutex::new(arp::init()),
nat: Mutex::new(nat::init()),
nat,
attached_subnet: Mutex::new(attached_subnet::init()),
loopback: Mutex::new(loopback::init()),
switch_ports,
Expand Down
Loading