From 0b2b289427ef400d8d2f9aa6774a19b2f15217b4 Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Fri, 26 Jun 2020 22:14:36 +0000 Subject: [PATCH 1/9] mark not matching bytes red in received packet --- src/ptf/dataplane.py | 53 +++++++++++++++++++++++++++++++++++++++++++- src/ptf/mask.py | 10 +++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/ptf/dataplane.py b/src/ptf/dataplane.py index cd82d53..aa5e3c4 100644 --- a/src/ptf/dataplane.py +++ b/src/ptf/dataplane.py @@ -806,6 +806,54 @@ def format(self): in the output. If the expected packet is a scapy packet object, the output will include information about the fields in the packet. """ + # returns list of indexes of bytes not matching the expected packet + def get_indexes_not_equal(exp_pkt, pkt): + if isinstance(exp_pkt, mask.Mask): + if not exp_pkt.is_valid(): + return [] + b, indxs_n_equal = exp_pkt.pkt_match(pkt, with_indexes=True) + return indxs_n_equal + + e = bytes(exp_pkt) + p = bytes(pkt) + if len(e) < 60: + p = p[:len(e)] + + indxs_n_equal = [] + i = 0 + for b in p: + if b != e[i]: + indxs_n_equal.append(i) + i = i + 1 + + return indxs_n_equal + + # modified scapy.utils.hexdump(packet) + # https://github.com/secdev/scapy/blob/master/scapy/utils.py + def hexdump_marked(packet, indxs_n_equal): + def red(inp_str): + return '\x1b[1;31m' + inp_str + '\x1b[0m' + + s = "" + x = scapy.utils.bytes_encode(packet) + x_len = len(x) + i = 0 + while i < x_len: + s += "%04x " % i + for j in range(16): + if i + j < x_len: + if indxs_n_equal is not None and i + j in indxs_n_equal: + s += red("%02X " % scapy.utils.orb(x[i + j])) + else: + s += "%02X " % scapy.utils.orb(x[i + j]) + else: + s += " " + s += " %s\n" % scapy.utils.sane_color(x[i:i + 16]) + i += 16 + # remove trailing \n + s = s[:-1] if s.endswith("\n") else s + print(s) + try: stdout_save = sys.stdout # The scapy packet dissection methods print directly to stdout, @@ -835,7 +883,10 @@ def format(self): # the expected packet's class. scapy.packet.ls(self.expected_packet.__class__(packet)) print('--') - scapy.utils.hexdump(packet) + + indxs_n_equal = get_indexes_not_equal(self.expected_packet, packet) + hexdump_marked(packet, indxs_n_equal) + else: print("%d total packets." % self.packet_count) print("==============================") diff --git a/src/ptf/mask.py b/src/ptf/mask.py index d2d899c..91086c8 100644 --- a/src/ptf/mask.py +++ b/src/ptf/mask.py @@ -52,7 +52,7 @@ def set_ignore_extra_bytes(self): def is_valid(self): return self.valid - def pkt_match(self, pkt): + def pkt_match(self, pkt, with_indexes=False): # just to be on the safe side pkt = bytearray(bytes(pkt)) # we fail if we don't match on sizes, or if ignore_extra_bytes is set, @@ -61,9 +61,15 @@ def pkt_match(self, pkt): len(pkt) < self.size: return False exp_pkt = bytearray(bytes(self.exp_pkt)) + indxs_n_equal = [] for i in range(self.size): if (exp_pkt[i] & self.mask[i]) != (pkt[i] & self.mask[i]): - return False + if not with_indexes: + return False + else: + indxs_n_equal.append(i) + if with_indexes: + return len(indxs_n_equal) == 0, indxs_n_equal return True def __str__(self): From 8e968877aa6451937a3f5ed22399696422b7eb7d Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:12:42 +0100 Subject: [PATCH 2/9] bugfix --- src/ptf/dataplane.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ptf/dataplane.py b/src/ptf/dataplane.py index 04b5bab..f68a807 100644 --- a/src/ptf/dataplane.py +++ b/src/ptf/dataplane.py @@ -933,7 +933,7 @@ def red(inp_str): # the expected packet's class. packet.ls(self.expected_packet.__class__(recent_packet)) print("--") - indxs_n_equal = get_indexes_not_equal(self.expected_packet, packet) + indxs_n_equal = get_indexes_not_equal(self.expected_packet, recent_packet) hexdump_marked(recent_packet, indxs_n_equal) else: print("%d total packets." % self.packet_count) From a6d95871cd86014d1860adb7d64e4fd26fe5f6ce Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:30:59 +0100 Subject: [PATCH 3/9] moving scapy.utils functions to packet_scapy --- src/ptf/dataplane.py | 10 +++++----- src/ptf/packet_scapy.py | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ptf/dataplane.py b/src/ptf/dataplane.py index f68a807..d7fd12c 100644 --- a/src/ptf/dataplane.py +++ b/src/ptf/dataplane.py @@ -878,12 +878,12 @@ def get_indexes_not_equal(exp_pkt, pkt): # modified scapy.utils.hexdump(packet) # https://github.com/secdev/scapy/blob/master/scapy/utils.py - def hexdump_marked(packet, indxs_n_equal): + def hexdump_marked(in_packet, indxs_n_equal): def red(inp_str): return '\x1b[1;31m' + inp_str + '\x1b[0m' s = "" - x = scapy.utils.bytes_encode(packet) + x = packet.bytes_encode(in_packet) x_len = len(x) i = 0 while i < x_len: @@ -891,12 +891,12 @@ def red(inp_str): for j in range(16): if i + j < x_len: if indxs_n_equal is not None and i + j in indxs_n_equal: - s += red("%02X " % scapy.utils.orb(x[i + j])) + s += red("%02X " % packet.orb(x[i + j])) else: - s += "%02X " % scapy.utils.orb(x[i + j]) + s += "%02X " % packet.orb(x[i + j]) else: s += " " - s += " %s\n" % scapy.utils.sane_color(x[i:i + 16]) + s += " %s\n" % packet.sane_color(x[i:i + 16]) i += 16 # remove trailing \n s = s[:-1] if s.endswith("\n") else s diff --git a/src/ptf/packet_scapy.py b/src/ptf/packet_scapy.py index a8a43b9..a48ecf5 100644 --- a/src/ptf/packet_scapy.py +++ b/src/ptf/packet_scapy.py @@ -151,3 +151,6 @@ def mysummary(self): # Scapy has its own hexdump hexdump = scapy.utils.hexdump ls = scapy.packet.ls +bytes_encode = scapy.utils.bytes_encode +orb = scapy.utils.orb +sane_color = scapy.utils.sane_color From 7cd82face0872206be2a6ad3f63e3cf1cdf79e52 Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:52:51 +0100 Subject: [PATCH 4/9] hexdump_marked() moved to packet_scapy --- src/ptf/dataplane.py | 28 +--------------------------- src/ptf/packet_scapy.py | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/ptf/dataplane.py b/src/ptf/dataplane.py index d7fd12c..48cb0cc 100644 --- a/src/ptf/dataplane.py +++ b/src/ptf/dataplane.py @@ -876,32 +876,6 @@ def get_indexes_not_equal(exp_pkt, pkt): return indxs_n_equal - # modified scapy.utils.hexdump(packet) - # https://github.com/secdev/scapy/blob/master/scapy/utils.py - def hexdump_marked(in_packet, indxs_n_equal): - def red(inp_str): - return '\x1b[1;31m' + inp_str + '\x1b[0m' - - s = "" - x = packet.bytes_encode(in_packet) - x_len = len(x) - i = 0 - while i < x_len: - s += "%04x " % i - for j in range(16): - if i + j < x_len: - if indxs_n_equal is not None and i + j in indxs_n_equal: - s += red("%02X " % packet.orb(x[i + j])) - else: - s += "%02X " % packet.orb(x[i + j]) - else: - s += " " - s += " %s\n" % packet.sane_color(x[i:i + 16]) - i += 16 - # remove trailing \n - s = s[:-1] if s.endswith("\n") else s - print(s) - try: stdout_save = sys.stdout # The scapy packet dissection methods print directly to stdout, @@ -934,7 +908,7 @@ def red(inp_str): packet.ls(self.expected_packet.__class__(recent_packet)) print("--") indxs_n_equal = get_indexes_not_equal(self.expected_packet, recent_packet) - hexdump_marked(recent_packet, indxs_n_equal) + packet.hexdump_marked(recent_packet, indxs_n_equal) else: print("%d total packets." % self.packet_count) print("==============================") diff --git a/src/ptf/packet_scapy.py b/src/ptf/packet_scapy.py index a48ecf5..a0bcc26 100644 --- a/src/ptf/packet_scapy.py +++ b/src/ptf/packet_scapy.py @@ -148,9 +148,32 @@ def mysummary(self): pass +# modified scapy.utils.hexdump(packet) +# https://github.com/secdev/scapy/blob/master/scapy/utils.py +def hexdump_marked(in_packet, indxs_n_equal): + def red(inp_str): + return '\x1b[1;31m' + inp_str + '\x1b[0m' + + s = "" + x = scapy.utils.bytes_encode(in_packet) + x_len = len(x) + i = 0 + while i < x_len: + s += "%04x " % i + for j in range(16): + if i + j < x_len: + if indxs_n_equal is not None and i + j in indxs_n_equal: + s += red("%02X " % scapy.utils.orb(x[i + j])) + else: + s += "%02X " % scapy.utils.orb(x[i + j]) + else: + s += " " + s += " %s\n" % scapy.utils.sane_color(x[i:i + 16]) + i += 16 + # remove trailing \n + s = s[:-1] if s.endswith("\n") else s + print(s) + # Scapy has its own hexdump hexdump = scapy.utils.hexdump ls = scapy.packet.ls -bytes_encode = scapy.utils.bytes_encode -orb = scapy.utils.orb -sane_color = scapy.utils.sane_color From a9f2eb9735c83933e77043c594d2bb34809ec3fe Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:08:20 +0200 Subject: [PATCH 5/9] fix --- src/ptf/dataplane.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ptf/dataplane.py b/src/ptf/dataplane.py index 48cb0cc..a5b9683 100644 --- a/src/ptf/dataplane.py +++ b/src/ptf/dataplane.py @@ -859,8 +859,12 @@ def get_indexes_not_equal(exp_pkt, pkt): if isinstance(exp_pkt, mask.Mask): if not exp_pkt.is_valid(): return [] - b, indxs_n_equal = exp_pkt.pkt_match(pkt, with_indexes=True) - return indxs_n_equal + ret = exp_pkt.pkt_match(pkt, with_indexes=True) + if type(ret) == bool: + return ret + else: + b, indxs_n_equal = ret + return indxs_n_equal e = bytes(exp_pkt) p = bytes(pkt) From 246e0495637ccd3fc8473856381b28e88e2af699 Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:26:33 +0200 Subject: [PATCH 6/9] fix --- src/ptf/dataplane.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ptf/dataplane.py b/src/ptf/dataplane.py index a5b9683..7e307be 100644 --- a/src/ptf/dataplane.py +++ b/src/ptf/dataplane.py @@ -861,7 +861,8 @@ def get_indexes_not_equal(exp_pkt, pkt): return [] ret = exp_pkt.pkt_match(pkt, with_indexes=True) if type(ret) == bool: - return ret + # something went wrong, don't mark any bytes red + return None else: b, indxs_n_equal = ret return indxs_n_equal From 1b0e3e2946ef158954e2ddd527463ba2e47feec4 Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Mon, 9 Sep 2024 20:34:52 +0200 Subject: [PATCH 7/9] fixed deprecation warning and load_module deprecation --- ptf | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ptf b/ptf index 79becd0..71227b0 100755 --- a/ptf +++ b/ptf @@ -109,7 +109,8 @@ def import_module(root_path, module_name): Python method. Allows for dynamic imports.""" finder = importlib.machinery.PathFinder() module_specs = finder.find_spec(module_name, [root_path]) - return module_specs.loader.load_module() + # return module_specs.loader.load_module() + return module_specs.loader.exec_module() def config_setup(): @@ -819,7 +820,8 @@ platform_mod = None try: platform_mod = import_module(config["platform_dir"], platform_name) except: - logging.warn("Failed to import " + platform_name + " platform module") + logging.warning("Failed to import " + platform_name + " platform module") + # logging.warn("Failed to import " + platform_name + " platform module") raise try: From 247a3954e3e80da8c25e1fa4d72db135eecbc4ca Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Wed, 11 Sep 2024 21:16:52 +0200 Subject: [PATCH 8/9] revert to last commit --- ptf | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ptf b/ptf index 71227b0..64b8449 100755 --- a/ptf +++ b/ptf @@ -109,8 +109,7 @@ def import_module(root_path, module_name): Python method. Allows for dynamic imports.""" finder = importlib.machinery.PathFinder() module_specs = finder.find_spec(module_name, [root_path]) - # return module_specs.loader.load_module() - return module_specs.loader.exec_module() + return module_specs.loader.load_module() def config_setup(): From aac32a84b8944eaa7d46078bcbea7bc733c1e151 Mon Sep 17 00:00:00 2001 From: Fridolin Siegmund <34545570+fridolinsiegmund@users.noreply.github.com> Date: Wed, 11 Sep 2024 21:18:07 +0200 Subject: [PATCH 9/9] Update packet_scapy.py, sane() instead of sane_color() for scapy 2.5.0 --- src/ptf/packet_scapy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ptf/packet_scapy.py b/src/ptf/packet_scapy.py index a0bcc26..f6de833 100644 --- a/src/ptf/packet_scapy.py +++ b/src/ptf/packet_scapy.py @@ -168,7 +168,7 @@ def red(inp_str): s += "%02X " % scapy.utils.orb(x[i + j]) else: s += " " - s += " %s\n" % scapy.utils.sane_color(x[i:i + 16]) + s += " %s\n" % scapy.utils.sane(x[i:i + 16]) i += 16 # remove trailing \n s = s[:-1] if s.endswith("\n") else s