X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FNATUtil.py;h=36f9da3994441749e5ff515ff86766cfe8d55779;hb=de5e719fbd965e2f8e26b7f3a9cd597b572d7ab3;hp=aabcd36cdae92457bf2f5d52096c0b25a0dad426;hpb=2c0affb70d06918e6ba0c9409d4cb7afc39b603a;p=csit.git diff --git a/resources/libraries/python/NATUtil.py b/resources/libraries/python/NATUtil.py index aabcd36cda..36f9da3994 100644 --- a/resources/libraries/python/NATUtil.py +++ b/resources/libraries/python/NATUtil.py @@ -13,6 +13,7 @@ """NAT utilities library.""" +from math import log2, modf from pprint import pformat from enum import IntEnum @@ -95,6 +96,10 @@ class NATUtil: 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. @@ -105,6 +110,8 @@ class NATUtil: :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']}" @@ -119,6 +126,18 @@ class NATUtil: 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. @@ -140,8 +159,10 @@ class NATUtil: :param node: Topology node. :type node: dict + :returns: NAT44 summary data. + :rtype: str """ - PapiSocketExecutor.run_cli_cmd(node, u"show nat44 summary") + return PapiSocketExecutor.run_cli_cmd(node, u"show nat44 summary") @staticmethod def show_nat_base_data(node): @@ -197,10 +218,33 @@ class NATUtil: :returns: Value of max_translations_per_thread NAT44 parameter. :rtype: int """ - from math import log2, modf rest, mult = modf(log2(sessions/(10*threads))) return 2 ** (int(mult) + (1 if rest else 0)) * 10 + @staticmethod + def get_nat44_sessions_number(node, proto): + """Get number of established NAT44 sessions from actual NAT44 mapping + data. + + :param node: DUT node. + :param proto: Required protocol - TCP/UDP/ICMP. + :type node: dict + :type proto: str + :returns: Number of established NAT44 sessions. + :rtype: int + :raises ValueError: If not supported protocol. + """ + nat44_data = dict() + if proto in [u"UDP", u"TCP", u"ICMP"]: + for line in NATUtil.show_nat44_summary(node).splitlines(): + sum_k, sum_v = line.split(u":") if u":" in line \ + else (line, None) + nat44_data[sum_k] = sum_v.strip() if isinstance(sum_v, str) \ + else sum_v + else: + raise ValueError(f"Unsupported protocol: {proto}!") + return nat44_data.get(f"total {proto.lower()} sessions", 0) + # DET44 PAPI calls # DET44 means deterministic mode of NAT44 @staticmethod @@ -253,6 +297,10 @@ class NATUtil: 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. @@ -277,6 +325,18 @@ class NATUtil: 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. @@ -299,14 +359,12 @@ class NATUtil: 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