Skip to content
Open
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
59 changes: 18 additions & 41 deletions dpd/src/nat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ops::Bound;

use crate::Switch;
use crate::table::nat;
use crate::table;
use crate::table::nat::{add_entry, delete_entry};
use crate::types::{DpdError, DpdResult};
use common::nat::{Ipv4Nat, Ipv6Nat};
use common::network::NatTarget;
Expand Down Expand Up @@ -45,6 +46,14 @@ impl PortRange {
fn overlaps(self, other: PortRange) -> bool {
self.low <= other.high && self.high >= other.low
}

pub(crate) fn low(self) -> u16 {
self.low
}

pub(crate) fn high(self) -> u16 {
self.high
}
}

impl fmt::Display for PortRange {
Expand Down Expand Up @@ -277,7 +286,7 @@ pub fn set_ipv6_mapping(
}
};

match nat::add_ipv6_entry(switch, nat_ip, low, high, tgt) {
match add_entry(switch, nat_ip, l4_ports, tgt) {
Err(e) => {
error!(switch.log, "failed to add {}: {:?}", full, e);
Err(e)
Expand Down Expand Up @@ -311,12 +320,7 @@ pub fn clear_ipv6_mapping(
nat.ipv6_mappings.remove(&nat_ip);
}
let full = ipv6_entry(nat_ip, &ent);
return match nat::delete_ipv6_entry(
switch,
nat_ip,
ent.l4_ports.low,
ent.l4_ports.high,
) {
return match delete_entry(switch, nat_ip, ent.l4_ports) {
Err(e) => {
error!(switch.log, "failed to clear {}: {:?}", full, e);
Err(e)
Expand Down Expand Up @@ -452,7 +456,7 @@ pub fn set_ipv4_mapping(
}
};

match nat::add_ipv4_entry(switch, nat_ip, low, high, tgt) {
match add_entry(switch, nat_ip, l4_ports, tgt) {
Err(e) => {
error!(switch.log, "failed to add nat entry {}: {:?}", full, e);
Err(e)
Expand Down Expand Up @@ -501,12 +505,7 @@ pub fn clear_ipv4_mapping(
nat.ipv4_mappings.remove(&nat_ip);
}
let full = ipv4_entry(nat_ip, &ent);
return match nat::delete_ipv4_entry(
switch,
nat_ip,
ent.l4_ports.low,
ent.l4_ports.high,
) {
return match delete_entry(switch, nat_ip, ent.l4_ports) {
Err(e) => {
error!(switch.log, "failed to clear {}: {:?}", full, e);
Err(e)
Expand Down Expand Up @@ -561,12 +560,7 @@ pub fn clear_overlapping_mappings_v4(
for idx in mappings_to_delete {
let ent = mappings.remove(idx);
let full = ipv4_entry(nat_ip, &ent);
match nat::delete_ipv4_entry(
switch,
nat_ip,
ent.l4_ports.low,
ent.l4_ports.high,
) {
match delete_entry(switch, nat_ip, ent.l4_ports) {
Err(e) => {
error!(switch.log, "failed to clear {}: {:?}", full, e);
return Err(e);
Expand Down Expand Up @@ -606,12 +600,7 @@ pub fn clear_overlapping_mappings_v6(
for idx in mappings_to_delete {
let ent = mappings.remove(idx);
let full = ipv6_entry(nat_ip, &ent);
match nat::delete_ipv6_entry(
switch,
nat_ip,
ent.l4_ports.low,
ent.l4_ports.high,
) {
match delete_entry(switch, nat_ip, ent.l4_ports) {
Err(e) => {
error!(switch.log, "failed to clear {}: {:?}", full, e);
return Err(e);
Expand All @@ -632,27 +621,15 @@ pub fn clear_overlapping_mappings_v6(
pub fn reset_ipv6(switch: &Switch) -> DpdResult<()> {
let mut nat = switch.nat.lock().unwrap();

debug!(switch.log, "resetting ipv6 nat tables");
nat.ipv6_mappings.clear();
if let Err(e) = nat::reset_ipv6(switch) {
error!(switch.log, "failed to reset ipv6 nat table: {:?}", e);
Err(e)
} else {
Ok(())
}
table::nat::reset::<Ipv6Addr>(switch)
}

pub fn reset_ipv4(switch: &Switch) -> DpdResult<()> {
let mut nat = switch.nat.lock().unwrap();

debug!(switch.log, "resetting ipv4 nat tables");
nat.ipv4_mappings.clear();
if let Err(e) = nat::reset_ipv4(switch) {
error!(switch.log, "failed to reset ipv4 nat table: {:?}", e);
Err(e)
} else {
Ok(())
}
table::nat::reset::<Ipv4Addr>(switch)
}

pub fn set_nat_generation(switch: &Switch, generation: i64) {
Expand Down
9 changes: 5 additions & 4 deletions dpd/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::convert::TryFrom;
use std::hash::Hash;
use std::net::{Ipv4Addr, Ipv6Addr};

use common::table;
use slog::debug;
Expand Down Expand Up @@ -233,10 +234,10 @@ pub fn get_entries(
}
TableType::ArpIpv4 => arp_ipv4::table_dump(switch, from_hardware),
TableType::NatIngressIpv4 => {
nat::ipv4_table_dump(switch, from_hardware)
nat::table_dump::<Ipv4Addr>(switch, from_hardware)
}
TableType::NatIngressIpv6 => {
nat::ipv6_table_dump(switch, from_hardware)
nat::table_dump::<Ipv6Addr>(switch, from_hardware)
}
TableType::AttachedSubnetIpv4 => {
attached_subnet_v4::table_dump(switch, from_hardware)
Expand Down Expand Up @@ -329,10 +330,10 @@ pub fn get_counters(
TableType::ArpIpv4 => arp_ipv4::counter_fetch(switch, force_sync),
TableType::PortMacAddress => mac::counter_fetch(switch, force_sync),
TableType::NatIngressIpv4 => {
nat::ipv4_counter_fetch(switch, force_sync)
nat::counter_fetch::<Ipv4Addr>(switch, force_sync)
}
TableType::NatIngressIpv6 => {
nat::ipv6_counter_fetch(switch, force_sync)
nat::counter_fetch::<Ipv6Addr>(switch, force_sync)
}
TableType::PortAddrIpv4 => {
port_ip::ipv4_counter_fetch(switch, force_sync)
Expand Down
Loading