Skip to content

Commit a1c1e4e

Browse files
committed
unify the per-family NAT mapping code
Replace the duplicated v4/v6 mapping storage and logic with a single NatEntry type and a NatMap generic over NatAddress; the public API keeps its per-family signatures as thin wrappers.
1 parent 1b86758 commit a1c1e4e

5 files changed

Lines changed: 407 additions & 524 deletions

File tree

dpd/src/api_server.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ use crate::switch_port::FixedSideDevice;
111111
use crate::switch_port::LedState;
112112
use crate::transceivers::PowerState;
113113
use crate::types::DpdError;
114-
use crate::{Switch, arp, loopback, nat, ports, route};
114+
use crate::{Switch, arp, loopback, ports, route};
115115
use common::attached_subnet::AttachedSubnetEntry;
116116
use common::nat::{Ipv4Nat, Ipv6Nat};
117117
use common::network::{InstanceTarget, MacAddr, NatTarget};
@@ -1422,8 +1422,7 @@ impl DpdApi for DpdApiImpl {
14221422
WhichPage::Next(Ipv6Token { ip }) => Some(*ip),
14231423
};
14241424

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

1452-
let entries = nat::get_ipv6_mappings_range(
1453-
switch,
1451+
let entries = switch.nat.get_mappings_range(
14541452
params.ipv6,
14551453
port,
14561454
usize::try_from(max).expect("invalid usize"),
@@ -1469,8 +1467,7 @@ impl DpdApi for DpdApiImpl {
14691467
) -> Result<HttpResponseOk<NatTarget>, HttpError> {
14701468
let switch: &Switch = rqctx.context();
14711469
let params = path.into_inner();
1472-
match nat::get_ipv6_mapping(switch, params.ipv6, params.low, params.low)
1473-
{
1470+
match switch.nat.get_mapping(params.ipv6, params.low, params.low) {
14741471
Ok(tgt) => Ok(HttpResponseOk(tgt)),
14751472
Err(e) => Err(e.into()),
14761473
}
@@ -1483,7 +1480,7 @@ impl DpdApi for DpdApiImpl {
14831480
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
14841481
let switch: &Switch = rqctx.context();
14851482
let params = path.into_inner();
1486-
match nat::set_ipv6_mapping(
1483+
match switch.nat.add_mapping(
14871484
switch,
14881485
params.ipv6,
14891486
params.low,
@@ -1501,7 +1498,9 @@ impl DpdApi for DpdApiImpl {
15011498
) -> Result<HttpResponseDeleted, HttpError> {
15021499
let switch: &Switch = rqctx.context();
15031500
let params = path.into_inner();
1504-
nat::clear_ipv6_mapping(switch, params.ipv6, params.low, params.low)
1501+
switch
1502+
.nat
1503+
.remove_mapping(switch, params.ipv6, params.low, params.low)
15051504
.map(|_| HttpResponseDeleted())
15061505
.map_err(HttpError::from)
15071506
}
@@ -1511,7 +1510,7 @@ impl DpdApi for DpdApiImpl {
15111510
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
15121511
let switch: &Switch = rqctx.context();
15131512

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

1533-
let entries = nat::get_ipv4_addrs_range(
1534-
switch,
1532+
let entries = switch.nat.get_addrs_range(
15351533
last_addr,
15361534
usize::try_from(max).expect("invalid usize"),
15371535
);
@@ -1558,8 +1556,7 @@ impl DpdApi for DpdApiImpl {
15581556
WhichPage::Next(NatToken { port }) => Some(*port),
15591557
};
15601558

1561-
let entries = nat::get_ipv4_mappings_range(
1562-
switch,
1559+
let entries = switch.nat.get_mappings_range(
15631560
params.ipv4,
15641561
port,
15651562
usize::try_from(max).expect("invalid usize"),
@@ -1578,8 +1575,7 @@ impl DpdApi for DpdApiImpl {
15781575
) -> Result<HttpResponseOk<NatTarget>, HttpError> {
15791576
let switch: &Switch = rqctx.context();
15801577
let params = path.into_inner();
1581-
match nat::get_ipv4_mapping(switch, params.ipv4, params.low, params.low)
1582-
{
1578+
match switch.nat.get_mapping(params.ipv4, params.low, params.low) {
15831579
Ok(tgt) => Ok(HttpResponseOk(tgt)),
15841580
Err(e) => Err(e.into()),
15851581
}
@@ -1592,7 +1588,7 @@ impl DpdApi for DpdApiImpl {
15921588
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
15931589
let switch: &Switch = rqctx.context();
15941590
let params = path.into_inner();
1595-
match nat::set_ipv4_mapping(
1591+
match switch.nat.add_mapping(
15961592
switch,
15971593
params.ipv4,
15981594
params.low,
@@ -1610,7 +1606,9 @@ impl DpdApi for DpdApiImpl {
16101606
) -> Result<HttpResponseDeleted, HttpError> {
16111607
let switch: &Switch = rqctx.context();
16121608
let params = path.into_inner();
1613-
nat::clear_ipv4_mapping(switch, params.ipv4, params.low, params.low)
1609+
switch
1610+
.nat
1611+
.remove_mapping(switch, params.ipv4, params.low, params.low)
16141612
.map(|_| HttpResponseDeleted())
16151613
.map_err(HttpError::from)
16161614
}
@@ -1620,7 +1618,7 @@ impl DpdApi for DpdApiImpl {
16201618
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
16211619
let switch: &Switch = rqctx.context();
16221620

1623-
match nat::reset_ipv4(switch) {
1621+
match switch.nat.reset::<Ipv4Addr>(switch) {
16241622
Ok(_) => Ok(HttpResponseUpdatedNoContent()),
16251623
Err(e) => Err(e.into()),
16261624
}
@@ -1744,11 +1742,11 @@ impl DpdApi for DpdApiImpl {
17441742
error!(switch.log, "failed to clear all link state: {:?}", e);
17451743
err = Some(e);
17461744
}
1747-
if let Err(e) = nat::reset_ipv4(switch) {
1745+
if let Err(e) = switch.nat.reset::<Ipv4Addr>(switch) {
17481746
error!(switch.log, "failed to reset ipv4 nat table: {:?}", e);
17491747
err = Some(e);
17501748
}
1751-
if let Err(e) = nat::reset_ipv6(switch) {
1749+
if let Err(e) = switch.nat.reset::<Ipv6Addr>(switch) {
17521750
error!(switch.log, "failed to reset ipv6 nat table: {:?}", e);
17531751
err = Some(e);
17541752
}
@@ -1913,7 +1911,7 @@ impl DpdApi for DpdApiImpl {
19131911
) -> Result<HttpResponseOk<i64>, HttpError> {
19141912
let switch = rqctx.context();
19151913

1916-
Ok(HttpResponseOk(nat::get_nat_generation(switch)))
1914+
Ok(HttpResponseOk(switch.nat.generation()))
19171915
}
19181916

19191917
async fn nat_trigger_update(

dpd/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub struct Switch {
193193
pub links: Mutex<link::LinkMap>,
194194
pub routes: TokioMutex<route::RouteData>,
195195
pub arp: Mutex<arp::ArpData>,
196-
pub nat: Mutex<nat::NatData>,
196+
pub nat: nat::Nat,
197197
pub attached_subnet: Mutex<attached_subnet::AttachedSubnetData>,
198198
pub loopback: Mutex<loopback::LoopbackData>,
199199
pub identifiers: Mutex<Option<SwitchIdentifiers>>,
@@ -298,6 +298,8 @@ impl Switch {
298298
let ws_log = log.new(slog::o!("unit" => "workflow_server"));
299299
let workflow_server = rpw::WorkflowServer::new(ws_log);
300300

301+
let nat = nat::Nat::new(&log);
302+
301303
Ok(Switch {
302304
start_time,
303305
config: Mutex::new(config),
@@ -308,7 +310,7 @@ impl Switch {
308310
counters,
309311
routes: TokioMutex::new(route_data),
310312
arp: Mutex::new(arp::init()),
311-
nat: Mutex::new(nat::init()),
313+
nat,
312314
attached_subnet: Mutex::new(attached_subnet::init()),
313315
loopback: Mutex::new(loopback::init()),
314316
switch_ports,

0 commit comments

Comments
 (0)