port mapping - hardening
authorimarom <[email protected]>
Wed, 2 Mar 2016 14:01:08 +0000 (16:01 +0200)
committerimarom <[email protected]>
Wed, 2 Mar 2016 14:01:08 +0000 (16:01 +0200)
scripts/automation/trex_control_plane/stl/console/trex_console.py
scripts/automation/trex_control_plane/stl/examples/stl_imix.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_std.py

index ffad03f..d96a762 100755 (executable)
@@ -349,17 +349,25 @@ class TRexConsole(TRexGeneralCmd):
         with self.stateless_client.logger.supress():
             table = stl_map_ports(self.stateless_client, ports = ports)
 
-        tmp = list(ports)
+        
         print format_text('\nAcquired ports topology:\n', 'bold', 'underline')
-        while tmp:
-            a = tmp.pop(0)
-            b = table[a]
-            tmp.remove(b)
 
-            print "port {0} <--> port {1}".format(a, b)
+        # bi-dir ports
+        print format_text('Bi-directional ports:\n','underline')
+        for port_a, port_b in table['bi']:
+            print "port {0} <--> port {1}".format(port_a, port_b)
+
+        print ""
 
+        # unknown ports
+        print format_text('Mapping unknown:\n','underline')
+        for port in table['unknown']:
+            print "port {0}".format(port)
         print ""
 
+       
+      
+
     def do_history (self, line):
         '''Manage the command history\n'''
 
index e3c01ca..cc7691a 100644 (file)
@@ -30,9 +30,10 @@ def imix_test ():
         # map ports - identify the routes
         table = stl_map_ports(c)
 
-        print "Mapped ports to sides {0} <--> {1}".format(table['dir'][0], table['dir'][1])
-        dir_0 = table['dir'][0]
-        dir_1 = table['dir'][1]
+        dir_0 = [x[0] for x in table['bi']]
+        dir_1 = [x[1] for x in table['bi']]
+
+        print "Mapped ports to sides {0} <--> {1}".format(dir_0, dir_1)
 
         # load IMIX profile
         profile = STLProfile.load_py('../../../../stl/imix.py')
index 72a5ea5..e0b25b1 100644 (file)
@@ -4,7 +4,6 @@ from trex_stl_packet_builder_scapy import *
 # map ports
 # will destroy all streams/data on the ports
 def stl_map_ports (client, ports = None):
-
     # by default use all ports
     if ports == None:
         ports = client.get_all_ports()
@@ -15,12 +14,15 @@ def stl_map_ports (client, ports = None):
     # generate streams
     base_pkt = CScapyTRexPktBuilder(pkt = Ether()/IP())
     
+    tx_pkts = {}
     pkts = 1
     for port in ports:
+        tx_pkts[pkts] = port
         stream = STLStream(packet = base_pkt,
                            mode = STLTXSingleBurst(pps = 100000, total_pkts = pkts))
 
         client.add_streams(stream, [port])
+        
         pkts = pkts * 2
 
     # inject
@@ -33,35 +35,33 @@ def stl_map_ports (client, ports = None):
     # cleanup
     client.reset(ports = ports)
 
-    table = {}
-    for port in ports:
-        table[port] = None
+    table = {'map': {}, 'bi' : [], 'unknown': []}
 
+    # actual mapping
     for port in ports:
+
         ipackets = stats[port]["ipackets"]
+        table['map'][port] = None
 
-        exp = 1
-        while ipackets >= exp:
-            if ((ipackets & exp) == (exp)):
-                source = int(math.log(exp, 2))
-                table[source] = port
+        for pkts in tx_pkts.keys():
+            if ( (pkts & ipackets) == pkts ):
+                tx_port = tx_pkts[pkts]
+                table['map'][port] = tx_port
 
-            exp *= 2
 
-    if not all(x != None for x in table.values()):
-        raise STLError('unable to map ports')
+    unmapped = list(ports)
+    while len(unmapped) > 0:
+        port_a = unmapped.pop(0)
+        port_b = table['map'][port_a]
 
-    dir_a = set()
-    dir_b = set()
-    for src, dst in table.iteritems():
-        # src is not in
-        if src not in (dir_a, dir_b):
-            if dst in dir_a:
-                dir_b.add(src)
-            else:
-                dir_a.add(src)
+        # if unknown - add to the unknown list
+        if port_b == None:
+            table['unknown'].append(port_a)
 
-    table['dir'] = [list(dir_a), list(dir_b)]
+        # bi-directional ports
+        elif (table['map'][port_b] == port_a):
+            unmapped.remove(port_b)
+            table['bi'].append( (port_a, port_b) )
 
     return table