add python test
authorHanoh Haim <[email protected]>
Tue, 13 Sep 2016 11:49:03 +0000 (14:49 +0300)
committerHanoh Haim <[email protected]>
Tue, 13 Sep 2016 16:37:29 +0000 (19:37 +0300)
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py
src/rpc-server/commands/trex_rpc_cmd_stream.cpp
src/rpc-server/commands/trex_rpc_cmds.h
src/stateless/cp/trex_stream_vm.cpp

index b6744d6..5f3a007 100755 (executable)
@@ -284,6 +284,29 @@ class CTRexVmInsFlowVar(CTRexVmInsBase):
         validate_type('step', step, int)
         assert step >= 0, 'step (%s) is negative' % step
 
+class CTRexVmInsFlowVarRandLimit(CTRexVmInsBase):
+    #TBD add more validation tests
+
+    VALID_SIZES =[1, 2, 4, 8]
+
+    def __init__(self, fv_name, size, limit, seed, min_value, max_value):
+        super(CTRexVmInsFlowVarRandLimit, self).__init__("flow_var_rand_limit")
+        self.name = fv_name;
+        validate_type('fv_name', fv_name, str)
+        self.size = size
+        self.limit=limit
+        validate_type('limit', limit, int)
+        assert limit >= 0, 'limit (%s) is negative' % limit
+        self.seed=seed
+        validate_type('seed', seed, int)
+        self.min_value=min_value
+        validate_type('min_value', min_value, int)
+        assert min_value >= 0, 'min_value (%s) is negative' % min_value
+        self.max_value=max_value
+        validate_type('max_value', max_value, int)
+        assert max_value >= 0, 'max_value (%s) is negative' % max_value
+
+
 class CTRexVmInsWrFlowVar(CTRexVmInsBase):
     def __init__(self, fv_name, pkt_offset, add_value=0, is_big_endian=True):
         super(CTRexVmInsWrFlowVar, self).__init__("write_flow_var")
@@ -569,6 +592,15 @@ def valid_fv_ops (op):
     if not (op in CTRexVmInsFlowVar.OPERATIONS):
         raise CTRexPacketBuildException(-11,("Flow var has invalid op %s ") % op  );
 
+def get_max_by_size (size):
+    d={
+    1:((1<<8) -1),
+    2:((1<<16)-1),
+    4:((1<<32)-1),
+    8:0xffffffffffffffff
+    };
+    return d[size]
+
 def convert_val (val):
     if is_integer(val):
         return val
@@ -666,10 +698,10 @@ class STLVmFlowVar(CTRexVmDescBase):
 
 class STLVmFlowVarRepetableRandom(CTRexVmDescBase):
 
-    def __init__(self, name,  size=4, limit=100, seed=None):
+    def __init__(self, name,  size=4, limit=100, seed=None, min_value=0, max_value=None):
         """
         Flow variable instruction for repeatable random with limit number of generating numbers. Allocates memory on a stream context. 
-        The size argument determines the variable size. Could be 1,2,,4 or 8
+        The size argument determines the variable size. Could be 1,2,4 or 8
 
         :parameters:
              name : string 
@@ -684,6 +716,13 @@ class STLVmFlowVarRepetableRandom(CTRexVmDescBase):
              seed   : int 
                 For deterministic result, you can set this to a uint16_t number
 
+             min_value  : int
+                Min value 
+
+             max_value  : int
+                Max value 
+
+
         .. code-block:: python
             :caption: Example1
 
@@ -691,10 +730,13 @@ class STLVmFlowVarRepetableRandom(CTRexVmDescBase):
             # input , 1 byte or random with limit of 5 
             STLVmFlowVarRepetableRandom("var1",size=1,limit=5)
 
-            # output 255,1,7,129,8,255,1,7,129,8
+            # output 255,1,7,129,8, ==> repeat 255,1,7,129,8
+
+            STLVmFlowVarRepetableRandom("var1",size=4,limit=100,min_value=0x12345678, max_value=0x32345678)
+
 
         """
-        super(STLVmFlowVar, self).__init__()
+        super(STLVmFlowVarRepetableRandom, self).__init__()
         self.name = name;
         validate_type('name', name, str)
         self.size =size
@@ -706,20 +748,18 @@ class STLVmFlowVarRepetableRandom(CTRexVmDescBase):
         else:
             self.seed = seed
 
-        # choose default value for init val
-        if init_value == None:
-            init_value = max_value if op == "dec" else min_value
-
-        self.init_value = convert_val (init_value)
         self.min_value  = convert_val (min_value);
-        self.max_value  = convert_val (max_value)
-        self.step       = convert_val (step)
+
+        if max_value == None :
+            max_value = get_max_by_size()
+
+        self.max_value  = get_max_by_size (self.size)
 
         if self.min_value > self.max_value :
             raise CTRexPacketBuildException(-11,("max %d is lower than min %d ") % (self.max_value,self.min_value)  );
 
     def get_obj (self):
-        return CTRexVmInsFlowVar(self.name,self.size,self.op,self.init_value,self.min_value,self.max_value,self.step);
+        return  CTRexVmInsFlowVarRandLimit(self.name, self.size, self.limit, self.seed, self.min_value, self.max_value);
 
     def get_var_name(self):
         return [self.name]
index f057749..f3addfd 100644 (file)
@@ -251,6 +251,46 @@ TrexRpcCmdAddStream::parse_vm_instr_tuple_flow_var(const Json::Value &inst, std:
                                                                 ));
 }
 
+
+void 
+TrexRpcCmdAddStream::check_min_max(uint8_t flow_var_size, 
+                                   uint64_t init_value,
+                                   uint64_t step,
+                                   uint64_t min_value,
+                                   uint64_t max_value, 
+                                   Json::Value &result){
+
+    if (max_value < min_value ) {
+        std::stringstream ss;
+        ss << "VM: request flow var variable '" << max_value << "' is smaller than " << min_value;
+        generate_parse_err(result, ss.str());
+    }
+
+    if (flow_var_size == 1 ) {
+        if ( (init_value > UINT8_MAX) || (min_value > UINT8_MAX) || (max_value > UINT8_MAX) || (step >UINT8_MAX) )  {
+            std::stringstream ss;
+            ss << "VM: request val is bigger than " << UINT8_MAX;
+            generate_parse_err(result, ss.str());
+        }
+    }
+
+    if (flow_var_size == 2 ) {
+        if ( (init_value > UINT16_MAX) || (min_value > UINT16_MAX) || (max_value > UINT16_MAX) || (step > UINT16_MAX) )  {
+            std::stringstream ss;
+            ss << "VM: request val is bigger than " << UINT16_MAX;
+            generate_parse_err(result, ss.str());
+        }
+    }
+
+    if (flow_var_size == 4 ) {
+        if ( (init_value > UINT32_MAX) || (min_value > UINT32_MAX) || (max_value > UINT32_MAX) || (step > UINT32_MAX) )  {
+            std::stringstream ss;
+            ss << "VM: request val is bigger than " << UINT32_MAX;
+            generate_parse_err(result, ss.str());
+        }
+    }
+}
+
 void 
 TrexRpcCmdAddStream::parse_vm_instr_flow_var_rand_limit(const Json::Value &inst, std::unique_ptr<TrexStream> &stream, Json::Value &result) {
     std::string  flow_var_name = parse_string(inst, "name", result);
@@ -259,6 +299,8 @@ TrexRpcCmdAddStream::parse_vm_instr_flow_var_rand_limit(const Json::Value &inst,
     uint8_t      flow_var_size = parse_choice(inst, "size", sizes, result);
     uint64_t seed    = parse_uint64(inst, "seed", result);
     uint64_t limit   = parse_uint64(inst, "limit", result);
+    uint64_t min_value   = parse_uint64(inst, "min_value", result);
+    uint64_t max_value   = parse_uint64(inst, "max_value", result);
 
     if (limit < 1 ) {
         std::stringstream ss;
@@ -266,10 +308,13 @@ TrexRpcCmdAddStream::parse_vm_instr_flow_var_rand_limit(const Json::Value &inst,
         generate_parse_err(result, ss.str());
     }
 
+    check_min_max(flow_var_size, 0, 0, min_value, max_value, result);
+
     stream->m_vm.add_instruction(new StreamVmInstructionFlowRandLimit(flow_var_name,
                                                                       flow_var_size,
                                                                       (int)limit,
-                                                                      0,0,
+                                                                      min_value,
+                                                                      max_value,
                                                                       seed)
                                  );
 }
@@ -301,36 +346,7 @@ TrexRpcCmdAddStream::parse_vm_instr_flow_var(const Json::Value &inst, std::uniqu
     uint64_t max_value   = parse_uint64(inst, "max_value", result);
     uint64_t step        = parse_uint64(inst, "step", result);
 
-    if (max_value < min_value ) {
-        std::stringstream ss;
-        ss << "VM: request flow var variable '" << max_value << "' is smaller than " << min_value;
-        generate_parse_err(result, ss.str());
-    }
-
-    if (flow_var_size == 1 ) {
-        if ( (init_value > UINT8_MAX) || (min_value > UINT8_MAX) || (max_value > UINT8_MAX) || (step >UINT8_MAX) )  {
-            std::stringstream ss;
-            ss << "VM: request val is bigger than " << UINT8_MAX;
-            generate_parse_err(result, ss.str());
-        }
-    }
-
-    if (flow_var_size == 2 ) {
-        if ( (init_value > UINT16_MAX) || (min_value > UINT16_MAX) || (max_value > UINT16_MAX) || (step > UINT16_MAX) )  {
-            std::stringstream ss;
-            ss << "VM: request val is bigger than " << UINT16_MAX;
-            generate_parse_err(result, ss.str());
-        }
-    }
-
-    if (flow_var_size == 4 ) {
-        if ( (init_value > UINT32_MAX) || (min_value > UINT32_MAX) || (max_value > UINT32_MAX) || (step > UINT32_MAX) )  {
-            std::stringstream ss;
-            ss << "VM: request val is bigger than " << UINT32_MAX;
-            generate_parse_err(result, ss.str());
-        }
-    }
-
+    check_min_max(flow_var_size, init_value, step, min_value, max_value, result);
 
     stream->m_vm.add_instruction(new StreamVmInstructionFlowMan(flow_var_name,
                                                                 flow_var_size,
@@ -385,7 +401,7 @@ TrexRpcCmdAddStream::parse_vm(const Json::Value &vm, std::unique_ptr<TrexStream>
     for (int i = 0; i < instructions.size(); i++) {
         const Json::Value & inst = parse_object(instructions, i, result);
 
-        auto vm_types = {"fix_checksum_ipv4", "flow_var", "write_flow_var","tuple_flow_var","trim_pkt_size","write_mask_flow_var"};
+        auto vm_types = {"fix_checksum_ipv4", "flow_var", "write_flow_var","tuple_flow_var","trim_pkt_size","write_mask_flow_var","flow_var_rand_limit"};
         std::string vm_type = parse_choice(inst, "type", vm_types, result);
 
         // checksum instruction
@@ -396,7 +412,7 @@ TrexRpcCmdAddStream::parse_vm(const Json::Value &vm, std::unique_ptr<TrexStream>
             parse_vm_instr_flow_var(inst, stream, result);
 
         } else if (vm_type == "flow_var_rand_limit") {
-            parse_vm_instr_flow_var(inst, stream, result);
+            parse_vm_instr_flow_var_rand_limit(inst, stream, result);
 
         } else if (vm_type == "write_flow_var") {
             parse_vm_instr_write_flow_var(inst, stream, result);
index 94aa2a5..c987f32 100644 (file)
@@ -106,6 +106,8 @@ void parse_vm(const Json::Value &vm, std::unique_ptr<TrexStream> &stream, Json::
 void parse_vm_instr_checksum(const Json::Value &inst, std::unique_ptr<TrexStream> &stream, Json::Value &result);
 void parse_vm_instr_flow_var(const Json::Value &inst, std::unique_ptr<TrexStream> &stream, Json::Value &result);
 void parse_vm_instr_flow_var_rand_limit(const Json::Value &inst, std::unique_ptr<TrexStream> &stream, Json::Value &result);
+void check_min_max(uint8_t flow_var_size, uint64_t init_value,uint64_t step,uint64_t min_value,uint64_t max_value,Json::Value &result);
+
 
 void parse_vm_instr_tuple_flow_var(const Json::Value &inst, std::unique_ptr<TrexStream> &stream, Json::Value &result);
 void parse_vm_instr_trim_pkt_size(const Json::Value &inst, std::unique_ptr<TrexStream> &stream, Json::Value &result);
index bdfca36..4bfd7d0 100644 (file)
@@ -1609,7 +1609,7 @@ void StreamDPOpFlowRandLimit32::dump(FILE *fd,std::string opt){
 }
 
 void StreamDPOpFlowRandLimit64::dump(FILE *fd,std::string opt){
-    fprintf(fd," %10s, flow_offset: %lu  limit :%lu seed:%x  \n",  opt.c_str(),(ulong)m_flow_offset,(ulong)m_limit,m_seed);
+    fprintf(fd," %10s, flow_offset: %lu  limit :%lu seed:%x (%lu-%lu) \n",  opt.c_str(),(ulong)m_flow_offset,(ulong)m_limit,m_seed,m_min_val,m_max_val);
 }