[dts] [PATCH v4] framwork/packet: sniff_packet specify running target support

Phil Yang phil.yang at arm.com
Thu Apr 12 11:52:51 CEST 2018


If tester in crb file was not the machine which running dts,
the sniff_packet process will not running on tester.

Create a ssh connection to the tester and run tcpdump to make sure
sniff_packet process running on the machine we expected.

Removed load_sniff_packets function in packet module as it will be
useless.

Signed-off-by: Phil Yang <phil.yang at arm.com>
Suggested-by: Marvin Liu <yong.liu at intel.com>
---
 framework/packet.py | 70 +++++++++++++++++++++--------------------------------
 framework/tester.py | 37 ++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 42 deletions(-)

diff --git a/framework/packet.py b/framework/packet.py
index 976b82b..f99ead8 100755
--- a/framework/packet.py
+++ b/framework/packet.py
@@ -812,15 +812,30 @@ def get_filter_cmd(filters=[]):
         return ""
 
 
-def sniff_packets(intf, count=0, timeout=5, filters=[]):
+def sniff_packets(intf, count=0, timeout=5, filters=[], target=[]):
     """
     sniff all packets for certain port in certain seconds
     """
     param = ""
     direct_param = r"(\s+)\[ (\S+) in\|out\|inout \]"
-    tcpdump_help = subprocess.check_output("tcpdump -h; echo 0",
-                                           stderr=subprocess.STDOUT,
-                                           shell=True)
+
+    # target[] contain the remote machine info for ssh connection
+    # target[0]: username
+    # target[1]: ip address
+    # target[2]: pass word
+    if target:
+        tcpdump_help_pipe = subprocess.Popen(["ssh",
+                            "%s@%s" % (target[0], target[1]),
+                            "tcpdump -h"],
+                            stderr=subprocess.PIPE,
+                            stdout=subprocess.PIPE,
+                            shell=False)
+        tcpdump_help = "".join(tuple(tcpdump_help_pipe.communicate()))
+        tcpdump_help_pipe.wait()
+    else:
+        tcpdump_help = subprocess.check_output("tcpdump -h; echo 0",
+                                    stderr=subprocess.STDOUT, shell=True)
+
     for line in tcpdump_help.split('\n'):
         m = re.match(direct_param, line)
         if m:
@@ -850,9 +865,16 @@ def sniff_packets(intf, count=0, timeout=5, filters=[]):
     else:
         cmd = sniff_cmd % options
 
-    args = shlex.split(cmd)
+    if target:
+        pipe = subprocess.Popen(["ssh",
+                "%s@%s" % (target[0], target[1]),
+                cmd],
+                stdin=subprocess.PIPE,
+                shell=False)
+    else:
+        args = shlex.split(cmd)
+        pipe = subprocess.Popen(args)
 
-    pipe = subprocess.Popen(args)
     index = str(time.time())
     SNIFF_PIDS[index] = (pipe, intf, timeout)
     time.sleep(1)
@@ -886,42 +908,6 @@ def load_sniff_pcap(index=''):
     return ""
 
 
-def load_sniff_packets(index=''):
-    """
-    Stop sniffer and return packet objects
-    """
-    pkts = []
-    child_exit = False
-    if index in SNIFF_PIDS.keys():
-        pipe, intf, timeout = SNIFF_PIDS[index]
-        time_elapse = int(time.time() - float(index))
-        while time_elapse < timeout:
-            if pipe.poll() is not None:
-                child_exit = True
-                break
-
-            time.sleep(1)
-            time_elapse += 1
-
-        if not child_exit:
-            pipe.send_signal(signal.SIGINT)
-            pipe.wait()
-
-        # wait pcap file ready
-        time.sleep(1)
-        try:
-            cap_pkts = rdpcap("/tmp/sniff_%s.pcap" % intf)
-            for pkt in cap_pkts:
-                # packet gen should be scapy
-                packet = Packet(tx_port=intf)
-                packet.pktgen.assign_pkt(pkt)
-                pkts.append(packet)
-        except:
-            pass
-
-    return pkts
-
-
 def load_pcapfile(filename=""):
     pkts = []
     try:
diff --git a/framework/tester.py b/framework/tester.py
index a775f68..c787b89 100755
--- a/framework/tester.py
+++ b/framework/tester.py
@@ -35,6 +35,7 @@ Interface for bulk traffic generators.
 
 import re
 import subprocess
+import os
 from time import sleep
 from settings import NICS, load_global_setting, PERF_SETTING
 from crb import Crb
@@ -704,6 +705,42 @@ class Tester(Crb):
             self.proc.kill()
             self.proc = None
 
+    def tcpdump_sniff_packets(self, intf, count=0, timeout=5, filters=[]):
+        """
+        Wrapper for packet module sniff_packets
+        """
+        # load functions in packet module
+        module = __import__("packet")
+        sniff_f = getattr(module, "sniff_packets")
+
+        target=[]
+        target.append(self.get_username())
+        target.append(self.get_ip_address())
+        target.append(self.get_password())
+        return sniff_f(intf, count, timeout, filters, target)
+
+    def load_tcpdump_sniff_pcap(self, index=''):
+        """
+        Wrapper for packet module load_sniff_pcap
+        """
+        # load functions in packet module
+        module = __import__("packet")
+        load_pcap_f = getattr(module, "load_sniff_pcap")
+        pcap = load_pcap_f(index)
+        self.session.copy_file_from(pcap)
+
+        return pcap.split(os.sep)[-1]
+
+    def load_tcpdump_sniff_packets(self, index=''):
+        """
+        Wrapper for packet module load_sniff_packets
+        """
+        # load functions in packet module
+        packet = __import__("packet")
+        file = self.load_tcpdump_sniff_pcap(index)
+
+        return packet.load_pcapfile(file)
+
     def kill_all(self, killall=False):
         """
         Kill all scapy process or DPDK application on tester.
-- 
2.7.4



More information about the dts mailing list