add scapy layer for default mac
authorHanoh Haim <[email protected]>
Mon, 15 Feb 2016 16:01:09 +0000 (18:01 +0200)
committerHanoh Haim <[email protected]>
Mon, 15 Feb 2016 16:01:09 +0000 (18:01 +0200)
scripts/automation/regression/unit_tests/functional_tests/scapy_pkt_builder_test.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_streams.py
scripts/stl/udp_1pkt_simple_mac_dst.py [new file with mode: 0644]
scripts/stl/udp_1pkt_simple_mac_dst_src.py [new file with mode: 0644]
scripts/stl/udp_1pkt_simple_mac_src.py [new file with mode: 0644]
src/rpc-server/commands/trex_rpc_cmd_stream.cpp
src/stateless/cp/trex_stream.cpp
src/stateless/cp/trex_stream.h

index a9f28ba..f0dc9dc 100644 (file)
@@ -73,6 +73,32 @@ class CTRexPktBuilderSanitySCapy_Test(pkt_bld_general_test.CGeneralPktBld_Test):
         )
 
 
+    def test_simple_mac_default(self):
+
+        pkt =  Ether()/IP()/UDP()
+
+
+        pkt_builder = CScapyTRexPktBuilder(pkt = pkt);
+
+        assert_equal( pkt_builder.is_def_src_mac () ,True)
+        assert_equal( pkt_builder.is_def_dst_mac () ,True)
+
+        pkt =  Ether(src="00:00:00:00:00:01")/IP()/UDP()
+
+        pkt_builder = CScapyTRexPktBuilder(pkt = pkt);
+
+        assert_equal( pkt_builder.is_def_src_mac (), False)
+        assert_equal( pkt_builder.is_def_dst_mac (), True)
+
+        pkt =  Ether(dst="00:00:00:00:00:01")/IP()/UDP()
+
+        pkt_builder = CScapyTRexPktBuilder(pkt = pkt);
+
+        assert_equal( pkt_builder.is_def_src_mac (),True)
+        assert_equal(  pkt_builder.is_def_dst_mac (),False)
+
+
+
 
     def test_simple_teredo(self):
 
index f1462ed..1e2286c 100644 (file)
@@ -751,7 +751,21 @@ class CScapyTRexPktBuilder(CTrexPktBuilderInterface):
             else:
                 raise CTRexPacketBuildException(-14, "bad packet" )
 
-
+    def is_def_src_mac (self):
+        p = self.pkt
+        if isinstance(p, Packet):
+            if isinstance(p,Ether):
+                if 'src' in p.fields :
+                    return False
+        return True
+
+    def is_def_dst_mac (self):
+        p = self.pkt
+        if isinstance(p, Packet):
+            if isinstance(p,Ether):
+                if 'dst' in p.fields :
+                    return False
+        return True
 
     def compile (self):
         self.vm_low_level=CTRexVmEngine()
index efeb5c8..68f8f69 100644 (file)
@@ -93,6 +93,11 @@ class STLTXMultiBurst(STLTXMode):
     def __str__ (self):
         return "Multi Burst"
 
+STLStreamDstMAC_CFG_FILE=0
+STLStreamDstMAC_PKT     =1
+STLStreamDstMAC_ARP     =2
+
+
 
 class STLStream(object):
 
@@ -105,7 +110,11 @@ class STLStream(object):
                   isg = 0.0,
                   rx_stats = None,
                   next = None,
-                  stream_id = None):
+                  stream_id = None,
+                  action_count =0,
+                  mac_src_override_by_pkt=None,
+                  mac_dst_override_mode=None    #see  STLStreamDstMAC_xx
+                  ):
 
         # type checking
         if not isinstance(mode, STLTXMode):
@@ -134,8 +143,35 @@ class STLStream(object):
         self.set_id(stream_id)
         self.set_next_id(None)
 
+
         self.fields = {}
 
+        int_mac_src_override_by_pkt = 0;
+        int_mac_dst_override_mode   = 0;
+
+
+        if mac_src_override_by_pkt == None:
+            int_mac_src_override_by_pkt=0
+            if packet :
+                if packet.is_def_src_mac ()==False:
+                    int_mac_src_override_by_pkt=1
+
+        else:
+            int_mac_src_override_by_pkt = int(mac_src_override_by_pkt);
+
+        if mac_dst_override_mode == None:
+            int_mac_dst_override_mode   = 0;
+            if packet :
+                if packet.is_def_dst_mac ()==False:
+                    int_mac_dst_override_mode=STLStreamDstMAC_PKT
+        else:
+            int_mac_dst_override_mode = int(mac_dst_override_mode);
+
+
+        self.fields['flags'] = (int_mac_src_override_by_pkt&1) +  ((int_mac_dst_override_mode&3)<<1)
+
+        self.fields['action_count'] = action_count
+
         # basic fields
         self.fields['enabled'] = enabled
         self.fields['self_start'] = self_start
@@ -327,7 +363,11 @@ class YAMLLoader(object):
                            self_start = s_obj.get('self_start', defaults.fields['self_start']),
                            isg        = s_obj.get('isg', defaults.fields['isg']),
                            rx_stats   = s_obj.get('rx_stats', defaults.fields['rx_stats']),
-                           next       = yaml_object.get('next'))
+                           next       = yaml_object.get('next'),
+                           action_count = s_obj.get('action_count', defaults.fields['action_count']),
+                           mac_src_override_by_pkt = s_obj.get('mac_src_override_by_pkt', 0),
+                           mac_dst_override_mode = s_obj.get('mac_src_override_by_pkt', 0) 
+                           )
 
         # hack the VM fields for now
         if 'vm' in s_obj:
diff --git a/scripts/stl/udp_1pkt_simple_mac_dst.py b/scripts/stl/udp_1pkt_simple_mac_dst.py
new file mode 100644 (file)
index 0000000..6b72888
--- /dev/null
@@ -0,0 +1,22 @@
+from trex_stl_lib.api import *
+
+# stream will be sent with src MAC addrees dst="60:60:60:60:60:60" and not from default of trex_cfg.yaml port src mac  
+class STLS1(object):
+
+    def create_stream (self):
+        return STLStream( packet = STLPktBuilder(pkt = Ether(dst="60:60:60:60:60:60")/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/(10*'x')),
+                          mode = STLTXCont(),
+                          #mac_dst_override_mode=STLStreamDstMAC_PKT # another way to explictly take it
+                          )
+
+    def get_streams (self, direction = 0):
+        # create 1 stream 
+        return [ self.create_stream() ]
+
+
+# dynamic load - used for trex console or simulator
+def register():
+    return STLS1()
+
+
+
diff --git a/scripts/stl/udp_1pkt_simple_mac_dst_src.py b/scripts/stl/udp_1pkt_simple_mac_dst_src.py
new file mode 100644 (file)
index 0000000..e3df0d0
--- /dev/null
@@ -0,0 +1,22 @@
+from trex_stl_lib.api import *
+
+# stream will be sent with src MAC addrees dst="60:60:60:60:60:60" and not from default of trex_cfg.yaml port src mac  
+class STLS1(object):
+
+    def create_stream (self):
+        return STLStream( packet = STLPktBuilder(pkt = Ether(src="61:61:61:61:61:61",dst="60:60:60:60:60:60")/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/(10*'x')),
+                          mode = STLTXCont(),
+                          #mac_dst_override_mode=STLStreamDstMAC_PKT # another way to explictly take it
+                          )
+
+    def get_streams (self, direction = 0):
+        # create 1 stream 
+        return [ self.create_stream() ]
+
+
+# dynamic load - used for trex console or simulator
+def register():
+    return STLS1()
+
+
+
diff --git a/scripts/stl/udp_1pkt_simple_mac_src.py b/scripts/stl/udp_1pkt_simple_mac_src.py
new file mode 100644 (file)
index 0000000..fe5a2a8
--- /dev/null
@@ -0,0 +1,22 @@
+from trex_stl_lib.api import *
+
+# stream will be sent with src MAC addrees src="60:60:60:60:60:60" and not from default of trex_cfg.yaml port src mac  
+class STLS1(object):
+
+    def create_stream (self):
+        return STLStream( packet = STLPktBuilder(pkt = Ether(src="60:60:60:60:60:60")/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/(10*'x')),
+                          mode = STLTXCont(),
+                          #mac_src_override_by_pkt=True # another way to explictly take it
+                          )
+
+    def get_streams (self, direction = 0):
+        # create 1 stream 
+        return [ self.create_stream() ]
+
+
+# dynamic load - used for trex console or simulator
+def register():
+    return STLS1()
+
+
+
index 650720b..87e205b 100644 (file)
@@ -56,6 +56,8 @@ TrexRpcCmdAddStream::_run(const Json::Value &params, Json::Value &result) {
     /* some fields */
     stream->m_enabled         = parse_bool(section, "enabled", result);
     stream->m_self_start      = parse_bool(section, "self_start", result);
+    stream->m_flags           = parse_int(section, "flags", result);
+    stream->m_action_count    = (uint16_t)parse_int(section, "action_count", result);
 
     /* inter stream gap */
     stream->m_isg_usec  = parse_double(section, "isg", result);
index af35dbe..fb0b35d 100644 (file)
@@ -131,7 +131,7 @@ TrexStream::TrexStream(uint8_t type,
     m_ibg_usec=0.0;  
     m_vm_dp = NULL;
     m_flags=0;
-    m_stream_count=0;
+    m_action_count=0;
 }
 
 TrexStream::~TrexStream() {
index 0f2a16c..2ab90c9 100644 (file)
@@ -228,7 +228,7 @@ public:
         dp->m_num_bursts            =   m_num_bursts;
         dp->m_ibg_usec              =   m_ibg_usec;
         dp->m_flags                 =   m_flags;
-        dp->m_stream_count          =   m_stream_count;
+        dp->m_action_count          =   m_action_count;
 
         return(dp);
     }
@@ -290,7 +290,7 @@ public:
     uint8_t       m_port_id;
     uint16_t      m_flags;
     uint32_t      m_stream_id;              /* id from RPC can be anything */
-    uint16_t      m_stream_count;       
+    uint16_t      m_action_count;       
     
 
     /* config fields */