1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """NAT utilities library."""
16 from resources.libraries.python.VatExecutor import VatTerminal, VatExecutor
19 class NATUtil(object):
20 """This class defines the methods to set NAT."""
26 def set_nat44_interfaces(node, int_in, int_out):
27 """Set inside and outside interfaces for NAT44.
29 :param node: DUT node.
30 :param int_in: Inside interface.
31 :param int_out: Outside interface.
35 :returns: Response of the command.
37 :raises RuntimeError: If setting of inside and outside interfaces for
42 with VatTerminal(node, json_param=False) as vat:
43 response = vat.vat_terminal_exec_cmd_from_template(
44 'nat/nat44_set_interfaces.vat',
45 int_in=int_in, int_out=int_out)
48 raise RuntimeError("Setting of inside and outside interfaces for "
52 def set_nat44_deterministic(node, ip_in, subnet_in, ip_out, subnet_out):
53 """Set deterministic behaviour of NAT44.
55 :param node: DUT node.
56 :param ip_in: Inside IP.
57 :param subnet_in: Inside IP subnet.
58 :param ip_out: Outside IP.
59 :param subnet_out: Outside IP subnet.
62 :type subnet_in: str or int
64 :type subnet_out: str or int
65 :returns: Response of the command.
67 :raises RuntimeError: If setting of deterministic behaviour of NAT44
72 with VatTerminal(node, json_param=False) as vat:
73 response = vat.vat_terminal_exec_cmd_from_template(
74 'nat/nat44_set_deterministic.vat',
75 ip_in=ip_in, subnet_in=subnet_in,
76 ip_out=ip_out, subnet_out=subnet_out)
79 raise RuntimeError("Setting of deterministic behaviour of NAT "
83 def set_nat_workers(node, lcores):
86 :param node: DUT node.
87 :param lcores: List of cores, format: range e.g. 1-5 or list of ranges
91 :returns: Response of the command.
93 :raises RuntimeError: If setting of NAT workers fails.
97 with VatTerminal(node, json_param=False) as vat:
98 response = vat.vat_terminal_exec_cmd_from_template(
99 'nat/nat_set_workers.vat', lcores=lcores)
102 raise RuntimeError("Setting of NAT workers failed!")
106 """Show the NAT settings.
108 :param node: DUT node.
110 :returns: Response of the command.
112 :raises RuntimeError: If getting of NAT settings fails.
116 with VatTerminal(node, json_param=False) as vat:
117 response = vat.vat_terminal_exec_cmd_from_template(
118 'nat/nat_show_nat.vat')
121 raise RuntimeError("Getting of NAT settings failed!")
124 def show_nat44_deterministic_forward(node, ip_addr):
125 """Show forward IP address and port(s).
127 :param node: DUT node.
128 :param ip_addr: IP address.
131 :returns: Response of the command.
133 :raises RuntimeError: If command 'exec snat deterministic forward'
138 with VatTerminal(node, json_param=False) as vat:
139 response = vat.vat_terminal_exec_cmd_from_template(
140 'nat/nat44_deterministic_forward.vat', ip=ip_addr)
143 raise RuntimeError("Command 'exec nat44 deterministic forward {ip}'"
144 " failed!".format(ip=ip_addr))
147 def show_nat44_deterministic_reverse(node, ip_addr, port):
148 """Show reverse IP address.
150 :param node: DUT node.
151 :param ip_addr: IP address.
155 :type port: str or int
156 :returns: Response of the command.
158 :raises RuntimeError: If command 'exec snat deterministic reverse'
163 with VatTerminal(node, json_param=False) as vat:
164 response = vat.vat_terminal_exec_cmd_from_template(
165 'nat/nat44_deterministic_reverse.vat',
166 ip=ip_addr, port=port)
170 "Command 'exec nat44 deterministic reverse {ip}:{port}'"
171 " failed!".format(ip=ip_addr, port=port))
174 def get_nat_static_mappings(node):
175 """Get NAT static mappings from VPP node.
177 :param node: VPP node.
179 :returns: List of static mappings.
181 :raises RuntimeError: If the output is not as expected.
185 # JSON output not supported for this command
186 vat.execute_script('nat/snat_mapping_dump.vat', node, json_out=False)
188 stdout = vat.get_script_stdout()
189 lines = stdout.split("\n")
192 # lines[0,1] are table and column headers
193 for line in lines[2::]:
194 # Ignore extra data after NAT table
195 if "snat_static_mapping_dump error: Misc" in line or "vat#" in line:
197 items = line.split(" ")
202 elif len(items) == 4:
203 # no ports were returned
205 "local_address": items[0],
206 "remote_address": items[1],
210 elif len(items) == 6:
212 "local_address": items[0],
213 "local_port": items[1],
214 "remote_address": items[2],
215 "remote_port": items[3],
220 raise RuntimeError("Unexpected output from snat_mapping_dump.")
225 def get_nat_interfaces(node):
226 """Get list of interfaces configured with NAT from VPP node.
228 :param node: VPP node.
230 :returns: List of interfaces on the node that are configured with NAT.
232 :raises RuntimeError: If the output is not as expected.
236 # JSON output not supported for this command
237 vat.execute_script('nat/snat_interface_dump.vat', node,
240 stdout = vat.get_script_stdout()
241 lines = stdout.split("\n")
245 items = line.split(" ")
246 for trash in ("", "vat#"):
247 while trash in items:
251 elif len(items) == 3:
253 # items[0] is the table header - "sw_if_index"
254 "sw_if_index": items[1],
255 "direction": items[2]
259 "Unexpected output from snat_interface_dump.")