[dts] [patch v2] framework/pktgen.py: remove “ __name__ == “__main__” part modify create_vm() as a private method modify some variables as private variables modify _retrieve_port_statistic() returning to a dict object 

wang fei feix.y.wang at intel.com
Thu Sep 21 13:33:20 CEST 2017


Signed-off-by: wang fei <feix.y.wang at intel.com>
---
 framework/pktgen.py | 105 ++++++++++++++++++----------------------------------
 1 file changed, 36 insertions(+), 69 deletions(-)

diff --git a/framework/pktgen.py b/framework/pktgen.py
index 03550bd..4ba6b25 100644
--- a/framework/pktgen.py
+++ b/framework/pktgen.py
@@ -94,7 +94,7 @@ class PacketGenerator(object):
         pass
 
     @abstractmethod
-    def _start_transmission(self, stream_ids, delay=50):
+    def _start_transmission(self, stream_ids):
         pass
 
     @abstractmethod
@@ -136,10 +136,10 @@ class PacketGenerator(object):
 
         time.sleep(delay)
         for stream_id in stream_ids:
-            rxbps_rates, rxpps_rates = self._retrieve_port_statistic(stream_id)
+            rx_statistics = self._retrieve_port_statistic(stream_id)
 
-            bps_rx.append(rxbps_rates)
-            pps_rx.append(rxpps_rates)
+            bps_rx.append(rx_statistics["rate_rx_bits"])
+            pps_rx.append(rx_statistics["rate_rx_pkts"])
             self._stop_transmission(stream_id)
             bps_rx_total = self._summary_statistic(bps_rx)
             pps_rx_total = self._summary_statistic(pps_rx)
@@ -148,8 +148,6 @@ class PacketGenerator(object):
 
         return bps_rx_total, pps_rx_total
 
-
-
     def _summary_statistic(self, array=[]):
         """
         Summary all values in statistic array
@@ -158,7 +156,6 @@ class PacketGenerator(object):
         for value in array:
             summary += value
 
-
         return summary
     
     def _get_stream(self, stream_id):
@@ -182,17 +179,17 @@ class TrexPacketGenerator(PacketGenerator):
         self._ports = []
         self._traffic_ports = []
         self._transmit_streams = {}
-        self.trex_app = "scripts/t-rex-64"
+        self._trex_app = "scripts/t-rex-64"
     
-        self.conf_inst = self._get_generator_conf_instance("trex")
-        self.conf = self.conf_inst.load_pktgen_config()
-        self.options_keys = [ 'rate', 'ip', 'vlan']
-        self.ip_keys = ['start', 'end','action', 'mask', 'step']
-        self.vlan_keys = ['start', 'end', 'action', 'step', 'count']
+        self._conf_inst = self._get_generator_conf_instance("trex")
+        self._conf = self._conf_inst.load_pktgen_config()
+        self._options_keys = [ 'rate', 'ip', 'vlan']
+        self._ip_keys = ['start', 'end','action', 'mask', 'step']
+        self._vlan_keys = ['start', 'end', 'action', 'step', 'count']
         super(TrexPacketGenerator, self).__init__(tester)
         
     def connect(self):
-        self._conn = STLClient(server=self.conf["server"])
+        self._conn = STLClient(server=self._conf["server"])
         time.sleep(30)
         self._conn.connect()
         for p in self._conn.get_all_ports():
@@ -205,11 +202,11 @@ class TrexPacketGenerator(PacketGenerator):
 
     def _check_options(self, opts={}):
         for key in opts:
-            if key in self.options_keys:
+            if key in self._options_keys:
                 if key == 'ip':
                     ip = opts['ip']
                     for ip_key in ip:
-                        if not ip_key in self.ip_keys:
+                        if not ip_key in self._ip_keys:
                             print " %s is invalid ip option" % ip_key
                             return False
                         if key == 'action':
@@ -219,7 +216,7 @@ class TrexPacketGenerator(PacketGenerator):
                 elif key == 'vlan':
                     vlan = opts['vlan']
                     for vlan_key in vlan:
-                        if not vlan_key in self.vlan_keys:
+                        if not vlan_key in self._vlan_keys:
                             print " %s is invalid vlan option" % vlan_key
                             return False
                         if key == 'action':
@@ -231,7 +228,7 @@ class TrexPacketGenerator(PacketGenerator):
                 return False
         return True
 
-    def create_vm (self, ip_src_range, ip_dst_range, action='inc', step=1):
+    def _create_vm (self, ip_src_range, ip_dst_range, action='inc', step=1):
         if not ip_src_range and not ip_dst_range:
             return None
 
@@ -255,14 +252,14 @@ class TrexPacketGenerator(PacketGenerator):
     def prepare_generator(self):
         app_param_temp = "-i"
 
-        for key in self.conf:
+        for key in self._conf:
             #key, value = pktgen_conf
             if key == 'config_file':
-                app_param_temp = app_param_temp + " --cfg " + self.conf[key]
+                app_param_temp = app_param_temp + " --cfg " + self._conf[key]
             elif key == 'core_num':
-                app_param_temp = app_param_temp + " -c " + self.conf[key]
+                app_param_temp = app_param_temp + " -c " + self._conf[key]
 
-        app = self.conf['trex_root_path'] + os.sep + self.trex_app
+        app = self._conf['trex_root_path'] + os.sep + self._trex_app
 
         cmd = app + " " + app_param_temp
 
@@ -298,15 +295,15 @@ class TrexPacketGenerator(PacketGenerator):
             step_temp = ip['step'].split('.')
 
             #get the subnet range of src and dst ip
-            if self.conf.has_key("ip_src"):
-                ip_src = self.conf['ip_src']
+            if self._conf.has_key("ip_src"):
+                ip_src = self._conf['ip_src']
                 ip_src_range_string = IPy.IP(IPy.IP(ip_src).make_net(mask).strNormal()).strNormal(3)
                 ip_src_range_temp = ip_src_range_string.split('-')
                 ip_src_range['start'] = ip_src_range_temp[0]
                 ip_src_range['end'] = ip_src_range_temp[1]
             
-            if self.conf.has_key("ip_dst"):
-                ip_dst = self.conf['ip_dst']
+            if self._conf.has_key("ip_dst"):
+                ip_dst = self._conf['ip_dst']
                 ip_dst_range_string = IPy.IP(IPy.IP(ip_dst).make_net(mask).strNormal()).strNormal(3)
                 ip_dst_range_temp = ip_dst_range_string.split('-')
                 ip_dst_range['start'] = ip_dst_range_temp[0]
@@ -314,22 +311,22 @@ class TrexPacketGenerator(PacketGenerator):
 
             pcap_file = stream['pcap_file']
 
-            vm = self.create_vm(ip_src_range, ip_dst_range, action=ip['action'], step=step_temp[3])
+            vm = self._create_vm(ip_src_range, ip_dst_range, action=ip['action'], step=step_temp[3])
     
             stl_stream = STLStream(packet = STLPktBuilder(pkt = pcap_file, vm=vm), mode = STLTXCont(percentage=100)) 
 
             self._transmit_streams[stream_id] = stl_stream
 
-    def _start_transmission(self, stream_ids, delay=50):
+    def _start_transmission(self, stream_ids):
         self._conn.reset(ports=self._ports)
         self._conn.clear_stats()
         self._conn.set_port_attr(self._ports, promiscuous=True)
-        duration_int = int(self.conf["duration"])
+        duration_int = int(self._conf["duration"])
         rate = "100%"
         warmup = 15
         
-        if self.conf.has_key("warmup"):
-            warmup = int(self.conf["warmup"])
+        if self._conf.has_key("warmup"):
+            warmup = int(self._conf["warmup"])
 
         for p in self._ports:
             for stream_id in stream_ids:
@@ -339,8 +336,8 @@ class TrexPacketGenerator(PacketGenerator):
                     rate = stream["options"]["rate"]
                     self._traffic_ports.append(p)
 
-        if self.conf.has_key("core_mask"):
-            self._conn.start(ports=self._traffic_ports, mult=rate, duration=warmup, core_mask=self.conf["core_mask"])
+        if self._conf.has_key("core_mask"):
+            self._conn.start(ports=self._traffic_ports, mult=rate, duration=warmup, core_mask=self._conf["core_mask"])
             self._conn.wait_on_traffic(ports=self._traffic_ports, timeout=warmup+30)
         else:
             self._conn.start(ports=self._traffic_ports, mult=rate, duration=warmup)
@@ -348,8 +345,8 @@ class TrexPacketGenerator(PacketGenerator):
             
         self._conn.clear_stats()
 
-        if self.conf.has_key("core_mask"):
-            self._conn.start(ports=self._traffic_ports, mult=rate, duration=duration_int, core_mask=self.conf["core_mask"])
+        if self._conf.has_key("core_mask"):
+            self._conn.start(ports=self._traffic_ports, mult=rate, duration=duration_int, core_mask=self._conf["core_mask"])
         else:
             self._conn.start(ports=self._traffic_ports, mult=rate, duration=duration_int)
 
@@ -361,6 +358,7 @@ class TrexPacketGenerator(PacketGenerator):
         self._conn.stop(ports=self._traffic_ports, rx_delay_ms=5000)
 
     def _retrieve_port_statistic(self, stream_id):
+        rx_statistics = {}
         stats = self._conn.get_stats()
         stream = self._get_stream(stream_id)
         port_id = stream["rx_port"]
@@ -369,7 +367,9 @@ class TrexPacketGenerator(PacketGenerator):
         rate_rx_pkts = port_stats["rx_pps"]
         rate_rx_bits = port_stats["rx_bps_L1"]
         print "rx_port: %d,  rate_rx_pkts: %f, rate_rx_bits:%f " % (port_id,rate_rx_pkts,rate_rx_bits)
-        return rate_rx_bits, rate_rx_pkts
+        rx_statistics["rate_rx_bits"] = rate_rx_bits
+        rx_statistics["rate_rx_pkts"] = rate_rx_pkts
+        return rx_statistics
 
     def quit_generator(self):
         self.disconnect()
@@ -385,36 +385,3 @@ def getPacketGenerator(tester, pktgen_type="trex"):
         return IxiaPacketGenerator(tester)
     elif pktgen_type == "trex":
         return TrexPacketGenerator(tester)
-
-
-if __name__ == "__main__":
-    # init pktgen stream options
-    options = {
-    'rate' : '100%',
-    'ip': {'action': 'inc', 'mask' : '255.255.255.0', 'step':'0.0.0.1'}
-    }
-    crbsconf = CrbsConf()
-    crb = (crbsconf.load_crbs_config())[0]
-    tester = Tester(crb, None)
-    # framework initial
-    trex = getPacketGenerator(tester, pktgen_type="trex")
-    
-    conf_inst = trex._get_generator_conf_instance("trex")
-    conf = conf_inst.load_pktgen_config()
-    # prepare running environment
-    trex.prepare_generator()
-
-    #config stream and convert options into pktgen commands
-    stream_id1 = trex.add_stream(0, 1, conf['pcap_file'])
-    trex.config_stream(stream_id=stream_id1, opts=options)
-    stream_id2 = trex.add_stream(1, 0, conf['pcap_file'])
-    trex.config_stream(stream_id=stream_id2, opts=options)
-    stream_id3 = trex.add_stream(0, 1, conf['pcap_file'])
-    trex.config_stream(stream_id=stream_id3, opts=options)
-    stream_id4 = trex.add_stream(1, 0, conf['pcap_file'])
-    trex.config_stream(stream_id=stream_id4, opts=options)
-    #pktgen.prepare_transmission(stream_ids=[stream_id])
-    trex.measure_throughput(stream_ids=[stream_id1,stream_id2,stream_id3,stream_id4], delay=5)
-    #trex.measure_throughput(stream_ids=[stream_id1,stream_id2], delay=5)
-    # comeback to framework
-    trex.quit_generator()
-- 
2.7.4



More information about the dts mailing list