minor fix hw_cs in case of random pkt size
authorHanoh Haim <[email protected]>
Thu, 29 Sep 2016 14:29:49 +0000 (17:29 +0300)
committerHanoh Haim <[email protected]>
Thu, 29 Sep 2016 14:29:49 +0000 (17:29 +0300)
scripts/stl/syn_attack_fix_cs_hw.py
src/stateless/cp/trex_stream_vm.cpp

index 4738f2b..0d279fd 100644 (file)
@@ -195,10 +195,43 @@ class STLS1(object):
                          random_seed = 0x1234,# can be remove. will give the same random value any run
                          mode = STLTXCont())
 
+    def create_stream_udp_random (self):
+        # pkt 
+        p_l2  = Ether();
+        p_l3  = IP(src="16.0.0.1",dst="48.0.0.1")
+        p_l4  = UDP(dport=12,sport=1025)
+        pyld_size = max(0, self.max_pkt_size_l3 - len(p_l3/p_l4));
+        base_pkt = p_l2/p_l3/p_l4/('\x55'*(pyld_size))
+
+        l3_len_fix =-(len(p_l2));
+        l4_len_fix =-(len(p_l2/p_l3));
+
+
+        # vm
+        vm = STLScVmRaw( [ STLVmFlowVar(name="fv_rand", min_value=64, max_value=len(base_pkt), size=2, op="random"),
+                           STLVmTrimPktSize("fv_rand"), # total packet size
+                           STLVmWrFlowVar(fv_name="fv_rand", pkt_offset= "IP.len", add_val=l3_len_fix), # fix ip len 
+                           STLVmWrFlowVar(fv_name="fv_rand", pkt_offset= "UDP.len", add_val=l4_len_fix), # fix udp len  
+
+                           STLVmFixChecksumHw(l3_offset = "IP",
+                                              l4_offset = "UDP", # not valid 
+                                              l4_type  = CTRexVmInsFixHwCs.L4_TYPE_UDP )# hint, TRex can know that 
+
+                          ]
+                       )
+
+        pkt = STLPktBuilder(pkt = base_pkt,
+                            vm = vm)
+
+        return STLStream(packet = pkt,
+                         random_seed = 0x1234,
+                         mode = STLTXCont())
+
 
     def get_streams (self, direction = 0, **kwargs):
         # create 1 stream 
         #return [ self.create_stream_ipv6_udp(),self.create_stream_tcp_syn(), self.create_stream_udp1(),self.create_stream_ipv6_tcp()]
+        #return [ self.create_stream_udp_random () ]
         return [ self.create_stream_tcp_syn()]
 
 
index 96ba893..716ae5d 100644 (file)
@@ -654,81 +654,81 @@ void StreamVm::build_program(){
             uint16_t l4_header_size =0;
 
 
-            assert( m_pkt );
-
-            bool packet_is_ipv4=true;
-            IPHeader * ipv4= (IPHeader *)(m_pkt+lpFix->m_l2_len);
-            if (ipv4->getVersion() ==4 ) {
-                packet_is_ipv4=true;
-                if (ipv4->getSize() != lpFix->m_l3_len ) {
-                    std::stringstream ss;
-                    ss << "instruction id '" << ins_id << "' fix hw command IPv4 header size is not valid  " << ipv4->getSize() ;
-                    err(ss.str());
-                }
-                if ( !((ipv4->getNextProtocol() == IPHeader::Protocol::TCP) || 
-                      (ipv4->getNextProtocol() == IPHeader::Protocol::UDP) ) ) {
-                    std::stringstream ss;
-                    ss << "instruction id '" << ins_id << "' fix hw command L4 should be TCP or UDP  " << ipv4->getSize() ;
-                    err(ss.str());
-                }
-            }else{
-                if (ipv4->getVersion() ==6) {
-                    packet_is_ipv4=false;
+            if ( m_pkt ) {
+                /* not pre compile for estimation of average packet size */
+                bool packet_is_ipv4=true;
+                IPHeader * ipv4= (IPHeader *)(m_pkt+lpFix->m_l2_len);
+                if (ipv4->getVersion() ==4 ) {
+                    packet_is_ipv4=true;
+                    if (ipv4->getSize() != lpFix->m_l3_len ) {
+                        std::stringstream ss;
+                        ss << "instruction id '" << ins_id << "' fix hw command IPv4 header size is not valid  " << ipv4->getSize() ;
+                        err(ss.str());
+                    }
+                    if ( !((ipv4->getNextProtocol() == IPHeader::Protocol::TCP) || 
+                          (ipv4->getNextProtocol() == IPHeader::Protocol::UDP) ) ) {
+                        std::stringstream ss;
+                        ss << "instruction id '" << ins_id << "' fix hw command L4 should be TCP or UDP  " << ipv4->getSize() ;
+                        err(ss.str());
+                    }
                 }else{
-                    std::stringstream ss;
-                    ss << "instruction id '" << ins_id << "' fix hw command should work on IPv4 or IPv6  "  ;
-                    err(ss.str());
+                    if (ipv4->getVersion() ==6) {
+                        packet_is_ipv4=false;
+                    }else{
+                        std::stringstream ss;
+                        ss << "instruction id '" << ins_id << "' fix hw command should work on IPv4 or IPv6  "  ;
+                        err(ss.str());
+                    }
                 }
-            }
 
-            StreamDPOpHwCsFix ipv_fix;
-            ipv_fix.m_l2_len = lpFix->m_l2_len;
-            ipv_fix.m_l3_len = lpFix->m_l3_len;
-            ipv_fix.m_op = StreamDPVmInstructions::ditFIX_HW_CS;
+                StreamDPOpHwCsFix ipv_fix;
+                ipv_fix.m_l2_len = lpFix->m_l2_len;
+                ipv_fix.m_l3_len = lpFix->m_l3_len;
+                ipv_fix.m_op = StreamDPVmInstructions::ditFIX_HW_CS;
 
-            if (packet_is_ipv4) {
-                if ( ipv4->getNextProtocol() == IPHeader::Protocol::TCP ){
-                    /* Ipv4 TCP */
-                    ipv_fix.m_ol_flags = (PKT_TX_IPV4 | PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM);
-                    l4_header_size = TCP_HEADER_LEN;
-                }else{
-                    assert( ipv4->getNextProtocol() == IPHeader::Protocol::UDP );
-                    /* Ipv4 UDP */
-                    ipv_fix.m_ol_flags = (PKT_TX_IPV4 | PKT_TX_IP_CKSUM | PKT_TX_UDP_CKSUM);
-                    l4_header_size = UDP_HEADER_LEN;
-                }
-            }else{
-                /* Ipv6*/
-                /* in this case we need to scan the Ipv6 headers */
-                /* TBD replace with parser of IPv6 function */
-                if ( lpFix->m_l4_type==StreamVmInstructionFixHwChecksum::L4_TYPE_TCP ){
-                    ipv_fix.m_ol_flags = (PKT_TX_IPV6 | PKT_TX_TCP_CKSUM);
-                    l4_header_size = TCP_HEADER_LEN;
-                }else{
-                    if ( lpFix->m_l4_type==StreamVmInstructionFixHwChecksum::L4_TYPE_UDP ){
-                        ipv_fix.m_ol_flags = (PKT_TX_IPV6 | PKT_TX_UDP_CKSUM);
+                if (packet_is_ipv4) {
+                    if ( ipv4->getNextProtocol() == IPHeader::Protocol::TCP ){
+                        /* Ipv4 TCP */
+                        ipv_fix.m_ol_flags = (PKT_TX_IPV4 | PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM);
+                        l4_header_size = TCP_HEADER_LEN;
+                    }else{
+                        assert( ipv4->getNextProtocol() == IPHeader::Protocol::UDP );
+                        /* Ipv4 UDP */
+                        ipv_fix.m_ol_flags = (PKT_TX_IPV4 | PKT_TX_IP_CKSUM | PKT_TX_UDP_CKSUM);
                         l4_header_size = UDP_HEADER_LEN;
+                    }
+                }else{
+                    /* Ipv6*/
+                    /* in this case we need to scan the Ipv6 headers */
+                    /* TBD replace with parser of IPv6 function */
+                    if ( lpFix->m_l4_type==StreamVmInstructionFixHwChecksum::L4_TYPE_TCP ){
+                        ipv_fix.m_ol_flags = (PKT_TX_IPV6 | PKT_TX_TCP_CKSUM);
+                        l4_header_size = TCP_HEADER_LEN;
                     }else{
-                        std::stringstream ss;
-                        ss << "instruction id '" << ins_id << "' fix hw command offsets should be TCP or UDP ";
-                        err(ss.str());
+                        if ( lpFix->m_l4_type==StreamVmInstructionFixHwChecksum::L4_TYPE_UDP ){
+                            ipv_fix.m_ol_flags = (PKT_TX_IPV6 | PKT_TX_UDP_CKSUM);
+                            l4_header_size = UDP_HEADER_LEN;
+                        }else{
+                            std::stringstream ss;
+                            ss << "instruction id '" << ins_id << "' fix hw command offsets should be TCP or UDP ";
+                            err(ss.str());
+                        }
                     }
                 }
-            }
-
-            if ( (total_l4_offset + l4_header_size) > m_pkt_size  ) {
 
-                std::stringstream ss;
-                ss << "instruction id '" << ins_id << "' fix hw command offsets  " << (total_l4_offset + l4_header_size) << "  is too high relative to packet size  "<< m_pkt_size;
-                err(ss.str());
-            }
+                if ( (total_l4_offset + l4_header_size) > m_pkt_size  ) {
 
-            /* add the instruction*/
-            m_instructions.add_command(&ipv_fix,sizeof(ipv_fix));
+                    std::stringstream ss;
+                    ss << "instruction id '" << ins_id << "' fix hw command offsets  " << (total_l4_offset + l4_header_size) << "  is too high relative to packet size  "<< m_pkt_size;
+                    err(ss.str());
+                }
 
-            /* mark R/W of the packet */
-            add_field_cnt(total_l4_offset + l4_header_size);
+                /* add the instruction*/
+                m_instructions.add_command(&ipv_fix,sizeof(ipv_fix));
 
+                /* mark R/W of the packet */
+                add_field_cnt(total_l4_offset + l4_header_size);
+            }
         }
 
         /* itFIX_IPV4_CS */