Skip to content

Commit 4d32bcd

Browse files
T7315: Change CLI fot VPP NAT (#25)
New CLI ``` set vpp nat44 static rule 10 outside-interface 'eth0' set vpp nat44 static rule 10 inside-interface 'eth1' set vpp nat44 static rule 10 external address 192.168.122.10 set vpp nat44 static rule 10 external port 6545 # optional set vpp nat44 static rule 10 protocol tcp|udp|icmp|all # optional, defaults to "all" set vpp nat44 static rule 10 local address 100.64.0.10 set vpp nat44 static rule 10 local port 64010 # optional ```
1 parent 07a3b0f commit 4d32bcd

File tree

5 files changed

+61
-96
lines changed

5 files changed

+61
-96
lines changed

Diff for: interface-definitions/vpp.xml.in

+12-13
Original file line numberDiff line numberDiff line change
@@ -853,18 +853,18 @@
853853
<priority>320</priority>
854854
</properties>
855855
<children>
856-
<leafNode name="inbound-interface">
856+
<leafNode name="inside-interface">
857857
<properties>
858-
<help>Inbound interface of NAT traffic</help>
858+
<help>NAT inside interface</help>
859859
<completionHelp>
860860
<list>any</list>
861861
<script>${vyos_completion_dir}/list_interfaces</script>
862862
</completionHelp>
863863
</properties>
864864
</leafNode>
865-
<leafNode name="outbound-interface">
865+
<leafNode name="outside-interface">
866866
<properties>
867-
<help>Outbound interface of NAT traffic</help>
867+
<help>NAT outside interface</help>
868868
<completionHelp>
869869
<list>any</list>
870870
<script>${vyos_completion_dir}/list_interfaces</script>
@@ -920,7 +920,7 @@
920920
</valueHelp>
921921
</properties>
922922
<children>
923-
<node name="destination">
923+
<node name="external">
924924
<properties>
925925
<help>NAT external parameters</help>
926926
</properties>
@@ -968,27 +968,25 @@
968968
</properties>
969969
<defaultValue>all</defaultValue>
970970
</leafNode>
971-
<leafNode name="outbound-interface">
971+
<leafNode name="outside-interface">
972972
<properties>
973-
<help>Outbound interface of NAT traffic</help>
973+
<help>NAT outside interface</help>
974974
<completionHelp>
975-
<list>any</list>
976975
<script>${vyos_completion_dir}/list_interfaces</script>
977976
</completionHelp>
978977
</properties>
979978
</leafNode>
980-
<leafNode name="inbound-interface">
979+
<leafNode name="inside-interface">
981980
<properties>
982-
<help>Inbound interface of NAT traffic</help>
981+
<help>NAT inside interface</help>
983982
<completionHelp>
984-
<list>any</list>
985983
<script>${vyos_completion_dir}/list_interfaces</script>
986984
</completionHelp>
987985
</properties>
988986
</leafNode>
989-
<node name="translation">
987+
<node name="local">
990988
<properties>
991-
<help>NAT internal parameters</help>
989+
<help>NAT local parameters</help>
992990
</properties>
993991
<children>
994992
<leafNode name="address">
@@ -1006,6 +1004,7 @@
10061004
#include <include/port-number.xml.i>
10071005
</children>
10081006
</node>
1007+
#include <include/generic-description.xml.i>
10091008
</children>
10101009
</tagNode>
10111010
</children>

Diff for: python/vyos/vpp/nat/nat44.py

+6-26
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,24 @@ class Nat44Static(Nat44):
151151
def __init__(self):
152152
self.vpp = VPPControl()
153153

154-
def add_inbound_interface(self, interface_in):
154+
def add_inside_interface(self, interface_in):
155155
self.interface_in = interface_in
156156
self.add_nat44_interface_inside()
157157

158-
def delete_inbound_interface(self, interface_in):
158+
def delete_inside_interface(self, interface_in):
159159
self.interface_in = interface_in
160160
self.delete_nat44_interface_inside()
161161

162-
def add_outbound_interface(self, interface_out):
162+
def add_outside_interface(self, interface_out):
163163
self.interface_out = interface_out
164164
self.add_nat44_interface_outside()
165165

166-
def delete_outbound_interface(self, interface_out):
166+
def delete_outside_interface(self, interface_out):
167167
self.interface_out = interface_out
168168
self.delete_nat44_interface_outside()
169169

170170
def add_nat44_static_mapping(
171-
self,
172-
iface_out,
173-
local_ip,
174-
external_ip,
175-
local_port,
176-
external_port,
177-
protocol,
178-
use_iface,
171+
self, local_ip, external_ip, local_port, external_port, protocol
179172
):
180173
"""Add NAT44 static mapping"""
181174
self.vpp.api.nat44_add_del_static_mapping_v2(
@@ -185,21 +178,11 @@ def add_nat44_static_mapping(
185178
local_port=local_port,
186179
external_port=external_port,
187180
flags=0x08 if not (protocol or local_port) else 0x00,
188-
external_sw_if_index=(
189-
self.vpp.get_sw_if_index(iface_out) if use_iface else 0xFFFFFFFF
190-
),
191181
is_add=True,
192182
)
193183

194184
def delete_nat44_static_mapping(
195-
self,
196-
iface_out,
197-
local_ip,
198-
external_ip,
199-
local_port,
200-
external_port,
201-
protocol,
202-
use_iface,
185+
self, local_ip, external_ip, local_port, external_port, protocol
203186
):
204187
"""Delete NAT44 static mapping"""
205188
self.vpp.api.nat44_add_del_static_mapping_v2(
@@ -209,8 +192,5 @@ def delete_nat44_static_mapping(
209192
local_port=local_port,
210193
external_port=external_port,
211194
flags=0x08 if not (protocol or local_port) else 0x00,
212-
external_sw_if_index=(
213-
self.vpp.get_sw_if_index(iface_out) if use_iface else 0xFFFFFFFF
214-
),
215195
is_add=False,
216196
)

Diff for: src/conf_mode/vpp_nat_source.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def verify(config):
6161
if 'remove' in config:
6262
return None
6363

64-
required_keys = {'inbound_interface', 'outbound_interface'}
64+
required_keys = {'inside_interface', 'outside_interface'}
6565
if not all(key in config for key in required_keys):
6666
missing_keys = required_keys - set(config.keys())
6767
raise ConfigError(
@@ -83,8 +83,8 @@ def apply(config):
8383
# Delete NAT source
8484
if 'effective' in config:
8585
remove_config = config.get('effective')
86-
interface_in = remove_config.get('inbound_interface')
87-
interface_out = remove_config.get('outbound_interface')
86+
interface_in = remove_config.get('inside_interface')
87+
interface_out = remove_config.get('outside_interface')
8888
translation_address = remove_config.get('translation', {}).get('address')
8989

9090
n = Nat44(interface_in, interface_out, translation_address)
@@ -96,8 +96,8 @@ def apply(config):
9696
return None
9797

9898
# Add NAT44
99-
interface_in = config.get('inbound_interface')
100-
interface_out = config.get('outbound_interface')
99+
interface_in = config.get('inside_interface')
100+
interface_out = config.get('outside_interface')
101101
translation_address = config.get('translation', {}).get('address')
102102

103103
n = Nat44(interface_in, interface_out, translation_address)

Diff for: src/conf_mode/vpp_nat_static.py

+27-36
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ def get_config(config=None) -> dict:
8686
expand_nodes=Diff.DELETE | Diff.ADD,
8787
)
8888

89-
if 'inbound_interface' in tmp:
90-
new, old = diff.get_value_diff(base_rule + ['inbound-interface'])
89+
if 'inside_interface' in tmp:
90+
new, old = diff.get_value_diff(base_rule + ['inside-interface'])
9191
in_iface_add.append(new) if new else None
9292
in_iface_del.append(old) if old else None
93-
if 'outbound_interface' in tmp:
94-
new, old = diff.get_value_diff(base_rule + ['outbound-interface'])
93+
if 'outside_interface' in tmp:
94+
new, old = diff.get_value_diff(base_rule + ['outside-interface'])
9595
out_iface_add.append(new) if new else None
9696
out_iface_del.append(old) if old else None
9797

@@ -120,21 +120,24 @@ def verify(config):
120120
if 'remove' in config:
121121
return None
122122

123-
required_keys = {'inbound_interface', 'outbound_interface'}
123+
required_keys = {'inside_interface', 'outside_interface'}
124124
for rule, rule_config in config['rule'].items():
125125
missing_keys = required_keys - rule_config.keys()
126126
if missing_keys:
127127
raise ConfigError(
128128
f"Required options are missing: {', '.join(missing_keys).replace('_', '-')} in rule {rule}"
129129
)
130130

131-
if not rule_config.get('translation', {}).get('address'):
132-
raise ConfigError(f'Translation requires address in rule {rule}')
131+
if not rule_config.get('local', {}).get('address'):
132+
raise ConfigError(f'Local settings require address in rule {rule}')
133133

134-
has_dest_port = 'port' in rule_config.get('destination', {})
135-
has_trans_port = 'port' in rule_config.get('translation', {})
134+
if not rule_config.get('external', {}).get('address'):
135+
raise ConfigError(f'External settings require address in rule {rule}')
136136

137-
if not has_trans_port == has_dest_port:
137+
has_local_port = 'port' in rule_config.get('local', {})
138+
has_external_port = 'port' in rule_config.get('external', {})
139+
140+
if not has_external_port == has_local_port:
138141
raise ConfigError(
139142
'Source and destination ports must either both be specified, or neither must be specified'
140143
)
@@ -147,28 +150,22 @@ def generate(config):
147150
def apply(config):
148151
n = Nat44Static()
149152

150-
# Delete inbound interfaces
153+
# Delete inside interfaces
151154
for interface in config['in_iface_del']:
152-
n.delete_inbound_interface(interface)
153-
# Delete outbound interfaces
155+
n.delete_inside_interface(interface)
156+
# Delete outside interfaces
154157
for interface in config['out_iface_del']:
155-
n.delete_outbound_interface(interface)
158+
n.delete_outside_interface(interface)
156159
# Delete NAT static mapping rules
157160
for rule in config['changed_rules']:
158161
if rule in config.get('effective', {}).get('rule', {}):
159162
rule_config = config['effective']['rule'][rule]
160163
n.delete_nat44_static_mapping(
161-
iface_out=rule_config.get('outbound_interface'),
162-
local_ip=rule_config.get('translation').get('address'),
163-
external_ip=rule_config.get('destination', {}).get('address', ''),
164-
local_port=int(rule_config.get('translation', {}).get('port', 0)),
165-
external_port=int(rule_config.get('destination', {}).get('port', 0)),
164+
local_ip=rule_config.get('local').get('address'),
165+
external_ip=rule_config.get('external', {}).get('address', ''),
166+
local_port=int(rule_config.get('local', {}).get('port', 0)),
167+
external_port=int(rule_config.get('external', {}).get('port', 0)),
166168
protocol=protocol_map[rule_config.get('protocol', 'all')],
167-
use_iface=(
168-
True
169-
if not rule_config.get('destination', {}).get('address')
170-
else False
171-
),
172169
)
173170

174171
if 'remove' in config:
@@ -177,24 +174,18 @@ def apply(config):
177174
# Add NAT44 static mapping rules
178175
n.enable_nat44_ed()
179176
for interface in config['in_iface_add']:
180-
n.add_inbound_interface(interface)
177+
n.add_inside_interface(interface)
181178
for interface in config['out_iface_add']:
182-
n.add_outbound_interface(interface)
179+
n.add_outside_interface(interface)
183180
for rule in config['changed_rules']:
184181
if rule in config.get('rule', {}):
185182
rule_config = config['rule'][rule]
186183
n.add_nat44_static_mapping(
187-
iface_out=rule_config.get('outbound_interface'),
188-
local_ip=rule_config.get('translation').get('address'),
189-
external_ip=rule_config.get('destination', {}).get('address', ''),
190-
local_port=int(rule_config.get('translation', {}).get('port', 0)),
191-
external_port=int(rule_config.get('destination', {}).get('port', 0)),
184+
local_ip=rule_config.get('local').get('address'),
185+
external_ip=rule_config.get('external', {}).get('address', ''),
186+
local_port=int(rule_config.get('local', {}).get('port', 0)),
187+
external_port=int(rule_config.get('external', {}).get('port', 0)),
192188
protocol=protocol_map[rule_config.get('protocol', 'all')],
193-
use_iface=(
194-
True
195-
if not rule_config.get('destination', {}).get('address')
196-
else False
197-
),
198189
)
199190

200191

Diff for: src/op_mode/show_vpp_nat44.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,22 @@ def _get_raw_output_static_rules(vpp_api):
116116
return rules_list
117117

118118

119-
def _get_formatted_output_rules(vpp, rules_list):
119+
def _get_formatted_output_rules(rules_list):
120120
data_entries = []
121121
for rule in rules_list:
122-
dest_address = rule.get('external_ip_address')
123-
dest_port = rule.get('external_port') or ''
124-
trans_address = rule.get('local_ip_address')
125-
trans_port = rule.get('local_port') or ''
122+
external_address = rule.get('external_ip_address')
123+
external_port = rule.get('external_port') or ''
124+
local_address = rule.get('local_ip_address')
125+
local_port = rule.get('local_port') or ''
126126
protocol = protocol_map[rule.get('protocol', 0)]
127-
dest_sh_if_index = rule.get('external_sw_if_index')
128127

129-
vpp_if_name = vpp.get_interface_name(dest_sh_if_index)
130-
if vpp_if_name:
131-
dest_address = vpp_if_name
132-
133-
values = [dest_address, dest_port, trans_address, trans_port, protocol]
128+
values = [external_address, external_port, local_address, local_port, protocol]
134129
data_entries.append(values)
135130
headers = [
136-
'Des_address/interface',
137-
'Dest_port',
138-
'Trans_address',
139-
'Trans_port',
131+
'External address',
132+
'External port',
133+
'Local address',
134+
'Local port',
140135
'Protocol',
141136
]
142137
out = sorted(data_entries, key=lambda x: x[2])
@@ -170,7 +165,7 @@ def show_static(raw: bool):
170165
return rules_list
171166

172167
else:
173-
return _get_formatted_output_rules(vpp, rules_list)
168+
return _get_formatted_output_rules(rules_list)
174169

175170

176171
if __name__ == '__main__':

0 commit comments

Comments
 (0)