-# Copyright (c) 2020 Cisco and/or its affiliates.
+# Copyright (c) 2021 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
flag=u"NAT_IS_NONE"):
"""Set NAT44 address range.
+ The return value is a callable (zero argument Python function)
+ which can be used to reset NAT state, so repeated trial measurements
+ hit the same slow path.
+
:param node: DUT node.
:param start_ip: IP range start.
:param end_ip: IP range end.
:type end_ip: str
:type vrf_id: int
:type flag: str
+ :returns: Resetter of the NAT state.
+ :rtype: Callable[[], None]
"""
cmd = u"nat44_add_del_address_range"
err_msg = f"Failed to set NAT44 address range on host {node[u'host']}"
with PapiSocketExecutor(node) as papi_exec:
papi_exec.add(cmd, **args_in).get_reply(err_msg)
+ # A closure, accessing the variables above.
+ def resetter():
+ """Delete and re-add the NAT range setting."""
+ with PapiSocketExecutor(node) as papi_exec:
+ args_in[u"is_add"] = False
+ papi_exec.add(cmd, **args_in)
+ args_in[u"is_add"] = True
+ papi_exec.add(cmd, **args_in)
+ papi_exec.get_replies(err_msg)
+
+ return resetter
+
@staticmethod
- def show_nat_config(node):
- """Show the NAT configuration.
+ def show_nat44_config(node):
+ """Show the NAT44 plugin running configuration.
:param node: DUT node.
:type node: dict
"""
- cmd = u"nat_show_config"
- err_msg = f"Failed to get NAT configuration on host {node[u'host']}"
+ cmd = u"nat44_show_running_config"
+ err_msg = f"Failed to get NAT44 configuration on host {node[u'host']}"
- with PapiSocketExecutor(node) as papi_exec:
- reply = papi_exec.add(cmd).get_reply(err_msg)
+ try:
+ with PapiSocketExecutor(node) as papi_exec:
+ reply = papi_exec.add(cmd).get_reply(err_msg)
+ except AssertionError:
+ # Perhaps VPP is an older version
+ old_cmd = u"nat_show_config"
+ with PapiSocketExecutor(node) as papi_exec:
+ reply = papi_exec.add(old_cmd).get_reply(err_msg)
- logger.debug(f"NAT Configuration:\n{pformat(reply)}")
+ logger.debug(f"NAT44 Configuration:\n{pformat(reply)}")
@staticmethod
def show_nat44_summary(node):
:rtype: int
"""
# vpp-device tests have not dedicated physical core so
- # ${thr_count_int} == 0 but we need to use one thread
+ # ${dp_count_int} == 0 but we need to use one thread
threads = 1 if not int(threads) else int(threads)
rest, mult = modf(log2(sessions/(10*threads)))
return 2 ** (int(mult) + (1 if rest else 0)) * 10
def set_det44_mapping(node, ip_in, subnet_in, ip_out, subnet_out):
"""Set DET44 mapping.
+ The return value is a callable (zero argument Python function)
+ which can be used to reset NAT state, so repeated trial measurements
+ hit the same slow path.
+
:param node: DUT node.
:param ip_in: Inside IP.
:param subnet_in: Inside IP subnet.
with PapiSocketExecutor(node) as papi_exec:
papi_exec.add(cmd, **args_in).get_reply(err_msg)
+ # A closure, accessing the variables above.
+ def resetter():
+ """Delete and re-add the deterministic NAT mapping."""
+ with PapiSocketExecutor(node) as papi_exec:
+ args_in[u"is_add"] = False
+ papi_exec.add(cmd, **args_in)
+ args_in[u"is_add"] = True
+ papi_exec.add(cmd, **args_in)
+ papi_exec.get_replies(err_msg)
+
+ return resetter
+
@staticmethod
def get_det44_mapping(node):
"""Get DET44 mapping data.
def get_det44_sessions_number(node):
"""Get number of established DET44 sessions from actual DET44 mapping
data.
-
:param node: DUT node.
:type node: dict
:returns: Number of established DET44 sessions.
:rtype: int
"""
det44_data = NATUtil.get_det44_mapping(node)
-
return det44_data.get(u"ses_num", 0)
@staticmethod