From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id BF792A0032; Fri, 29 Oct 2021 10:57:19 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id AD064410E0; Fri, 29 Oct 2021 10:57:19 +0200 (CEST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mails.dpdk.org (Postfix) with ESMTP id 1313740688 for ; Fri, 29 Oct 2021 10:57:17 +0200 (CEST) X-IronPort-AV: E=McAfee;i="6200,9189,10151"; a="291460164" X-IronPort-AV: E=Sophos;i="5.87,192,1631602800"; d="scan'208";a="291460164" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2021 01:57:17 -0700 X-IronPort-AV: E=Sophos;i="5.87,192,1631602800"; d="scan'208";a="726110136" Received: from shwdenpg197.ccr.corp.intel.com ([10.253.106.73]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2021 01:57:12 -0700 From: DongJunX To: dts@dpdk.org Cc: "Dong,JunX" , Dong@dpdk.org Date: Fri, 29 Oct 2021 16:57:06 +0800 Message-Id: <20211029085706.1393-1-junx.dong@intel.com> X-Mailer: git-send-email 2.33.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dts] [PATCH V1] framework/*: add scapy modules to packet module X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dts-bounces@dpdk.org Sender: "dts" From: "Dong,JunX" scapy required modules contain itself contained modules and dts self contained module, dts self contained module that be in dep dir must copy from platform to tester, so can delay import the modules after tester copied it Signed-off-by: Dong,JunX --- framework/packet.py | 32 +++++++++++++++----------------- framework/tester.py | 5 +++++ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/framework/packet.py b/framework/packet.py index e6fd74a0..eb9d21df 100644 --- a/framework/packet.py +++ b/framework/packet.py @@ -35,9 +35,7 @@ Base on scapy(python program for packet manipulation) from importlib import import_module from socket import AF_INET6 - from scapy.all import * - from .utils import convert_int2ip, convert_ip2int # load extension layers @@ -50,20 +48,24 @@ if not os.path.exists(TMP_PATH): scapy_modules_required = {'scapy.contrib.gtp': ['GTP_U_Header', 'GTPPDUSessionContainer'], 'scapy.contrib.lldp': ['LLDPDU', 'LLDPDUManagementAddress'], - 'dep.scapy_modules.Dot1BR': ['Dot1BR'], + 'Dot1BR': ['Dot1BR'], 'scapy.contrib.pfcp': ['PFCP'], 'scapy.contrib.nsh': ['NSH'], 'scapy.contrib.igmp': ['IGMP'], 'scapy.contrib.mpls': ['MPLS'], } -for m in scapy_modules_required: - try: - module = import_module(m) - for clazz in scapy_modules_required[m]: - locals().update({clazz: getattr(module, clazz)}) - except Exception as e: - print(e) + +def bind_scapy_modules(_module_path): + sys.path.append(_module_path) + for _module in scapy_modules_required: + try: + module = import_module(_module) + for clazz in scapy_modules_required[_module]: + setattr(sys.modules[__name__], clazz, getattr(module, clazz)) + except Exception as e: + print(e) + def get_scapy_module_impcmd(): cmd_li = list() @@ -71,11 +73,10 @@ def get_scapy_module_impcmd(): cmd_li.append(f'from {m} import {",".join(scapy_modules_required[m])}') return ';'.join(cmd_li) -SCAPY_IMP_CMD = get_scapy_module_impcmd() +SCAPY_IMP_CMD = get_scapy_module_impcmd() # packet generator type should be configured later PACKETGEN = "scapy" - LayersTypes = { "L2": ['ether', 'vlan', 'etag', '1588', 'arp', 'lldp', 'mpls', 'nsh'], # ipv4_ext_unknown, ipv6_ext_unknown @@ -96,17 +97,15 @@ LayersTypes = { "INNER L4": ['inner_tcp', 'inner_udp', 'inner_frag', 'inner_sctp', 'inner_icmp', 'inner_nofrag'], "PAYLOAD": ['raw'] } - # Saved background sniff process id SNIFF_PIDS = {} - # Saved packet generator process id # used in pktgen or tgen PKTGEN_PIDS = {} - # default filter for LLDP packet LLDP_FILTER = {'layer': 'ether', 'config': {'type': 'not lldp'}} + def write_raw_pkt(pkt_str, file_name): tmp = eval(pkt_str) tmp = bytearray(bytes(tmp)) @@ -114,6 +113,7 @@ def write_raw_pkt(pkt_str, file_name): w.write(tmp) w.close() + class scapy(object): SCAPY_LAYERS = { 'ether': Ether(dst="ff:ff:ff:ff:ff:ff"), @@ -1117,8 +1117,6 @@ def strip_pktload(pkt=None, layer="L2", p_index=0): return load -############################################################################### -############################################################################### if __name__ == "__main__": pkt = Packet('Ether(type=0x894f)/NSH(Len=0x6,NextProto=0x0,NSP=0x000002,NSI=0xff)') sendp(pkt, iface='lo') diff --git a/framework/tester.py b/framework/tester.py index 9dba4e4d..acb7d82a 100644 --- a/framework/tester.py +++ b/framework/tester.py @@ -52,6 +52,7 @@ from .packet import ( start_tcpdump, stop_and_load_tcpdump_packets, strip_pktload, + bind_scapy_modules ) from .pktgen import getPacketGenerator from .settings import ( @@ -120,6 +121,10 @@ class Tester(Crb): out = session.session.send_expect(SCAPY_IMP_CMD, '>>> ') if 'ImportError' in out: session.logger.warning(f'entering import error: {out}') + + # bind scapy modules to packet module + bind_scapy_modules(self.tmp_scapy_module_dir) + return session def check_scapy_version(self): -- 2.24.1.windows.2