Skip to content

Commit 455b4c6

Browse files
authored
Merge pull request #907 from NetSys/nat
Mapping a logical internal/external ports to actual bess gates in nat
2 parents d08578b + e795c35 commit 455b4c6

6 files changed

Lines changed: 14 additions & 14 deletions

File tree

bessctl/conf/perftest/nat.bess

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ Source() \
4949
-> nat::NAT(ext_addrs=nat_config)
5050

5151
if bidirectional:
52-
nat -> MACSwap() -> IPSwap() -> 1:nat:1 -> Sink()
52+
nat:1 -> MACSwap() -> IPSwap() -> 1:nat:0 -> Sink()
5353
else:
54-
nat -> Sink()
54+
nat:1 -> Sink()

bessctl/conf/samples/nat.bess

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ packets = [gen_packet(scapy.UDP, '172.16.100.1', '10.0.0.1'),
5151

5252
# dynamic NAT configuration
5353
nat::NAT(ext_addrs=[{'ext_addr': '192.168.0.1'}, {'ext_addr': '192.168.0.2'}])
54-
Source() -> Rewrite(templates=packets) -> 0:nat:0 -> MACSwap() -> IPSwap() -> 1:nat:1 -> Sink()
54+
Source() -> Rewrite(templates=packets) -> 0:nat:1 -> MACSwap() -> IPSwap() -> 1:nat:0 -> Sink()
5555

5656
# static NAT configuration
5757
static_nat::StaticNAT(pairs=[
@@ -60,4 +60,4 @@ static_nat::StaticNAT(pairs=[
6060
{'int_range': {'start': '192.168.0.0', 'end': '192.168.255.255'},
6161
'ext_range': {'start': '66.77.0.0', 'end': '66.77.255.255'}},
6262
])
63-
Source() -> Rewrite(templates=packets) -> 0:static_nat:0 -> MACSwap() -> IPSwap() -> 1:static_nat:1 -> Sink()
63+
Source() -> Rewrite(templates=packets) -> 0:static_nat:1 -> MACSwap() -> IPSwap() -> 1:static_nat:0 -> Sink()

bessctl/module_tests/nat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def _swap_l4(l4):
6363
pkt_orig = eth / ip_orig / l4_orig / l7
6464

6565
pkt_outs = self.run_module(module, 0, [pkt_orig], [0, 1])
66-
self.assertEquals(len(pkt_outs[0]), 1)
67-
pkt_natted = pkt_outs[0][0]
66+
self.assertEquals(len(pkt_outs[1]), 1)
67+
pkt_natted = pkt_outs[1][0]
6868

6969
# The NAT module can choose an arbitrary source port/id.
7070
# We cannot test it, we have to read from the output.
@@ -81,9 +81,9 @@ def _swap_l4(l4):
8181
pkt_reply = eth / ip_reply / l4_reply / l7
8282

8383
pkt_outs = self.run_module(module, 1, [pkt_reply], [0, 1])
84-
self.assertEquals(len(pkt_outs[1]), 1)
84+
self.assertEquals(len(pkt_outs[0]), 1)
8585
self.assertSamePackets(eth / ip_unnatted / _swap_l4(l4_orig) / l7,
86-
pkt_outs[1][0])
86+
pkt_outs[0][0])
8787

8888
def test_nat_udp(self):
8989
nat_config = [{'ext_addr': '192.168.1.1'}]

bessctl/module_tests/placement_constraint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_nat(self):
6262
# Swap src/dst IP addresses / ports
6363
ip = IPSwap()
6464

65-
Source() -> 0:nat:0 -> mac -> ip -> 1:nat:1 -> Sink()
65+
Source() -> 0:nat:1 -> mac -> ip -> 1:nat:0 -> Sink()
6666

6767
self.assertFalse(bess.check_constraints())
6868

@@ -74,7 +74,7 @@ def test_nat_queue(self):
7474
# Swap src/dst IP addresses / ports
7575
ip = IPSwap()
7676

77-
Source() -> 0:nat:0 -> Queue() -> ip -> 1:nat:1 -> Sink()
77+
Source() -> 0:nat:1 -> Queue() -> ip -> 1:nat:0 -> Sink()
7878

7979
self.assertFalse(bess.check_constraints())
8080

@@ -85,8 +85,8 @@ def test_nat_negative(self):
8585
bess.add_worker(0, 0)
8686
bess.add_worker(1, 1)
8787
nat = NAT(ext_addrs=nat_config)
88-
src0 -> 0: nat: 0 -> Sink()
89-
src1 -> 1: nat: 1 -> Sink()
88+
src0 -> 0: nat: 1 -> Sink()
89+
src1 -> 1: nat: 0 -> Sink()
9090
src0.attach_task(wid=0)
9191
src1.attach_task(wid=1)
9292

core/modules/nat.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ inline void Stamp(Ipv4 *ip, void *l4, const Endpoint &before,
324324

325325
template <NAT::Direction dir>
326326
inline void NAT::DoProcessBatch(Context *ctx, bess::PacketBatch *batch) {
327-
gate_idx_t ogate_idx = static_cast<gate_idx_t>(dir);
327+
gate_idx_t ogate_idx = dir == kForward ? 1 : 0;
328328
int cnt = batch->cnt();
329329
uint64_t now = ctx->current_ns;
330330

core/modules/static_nat.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static inline void UpdateChecksum(bess::utils::Ipv4 *ip, uint32_t incr) {
145145

146146
template <StaticNAT::Direction dir>
147147
inline void StaticNAT::DoProcessBatch(Context *ctx, bess::PacketBatch *batch) {
148-
gate_idx_t ogate_idx = static_cast<gate_idx_t>(dir);
148+
gate_idx_t ogate_idx = dir == kForward ? 1 : 0;
149149
int cnt = batch->cnt();
150150

151151
for (int i = 0; i < cnt; i++) {

0 commit comments

Comments
 (0)