diff options
author | Hanoh Haim <hhaim@cisco.com> | 2016-02-18 16:51:54 +0200 |
---|---|---|
committer | Hanoh Haim <hhaim@cisco.com> | 2016-02-18 16:51:54 +0200 |
commit | 77531f879979de5c261802c88b41a361a18095ab (patch) | |
tree | 511845d62f2e6c9839dcfa98e2db95f1bab3b279 | |
parent | 8b12867a012f56b92bd3a3797aa2e554c0b71bef (diff) | |
parent | 38defe118db360190121c44a9b65c1477f54afa3 (diff) |
Merge new mask instruction
26 files changed, 877 insertions, 9 deletions
@@ -62,6 +62,13 @@ scripts/exp/stl_single_stream_mac0-0.erf scripts/exp/stl_single_stream_mac01-0.erf scripts/exp/stl_single_stream_mac10-0.erf scripts/exp/stl_single_stream_mac11-0.erf +scripts/exp/udp_64B_vm_mask1.pcap +scripts/exp/udp_64B_vm_mask2.pcap +scripts/exp/udp_64B_vm_mask3.pcap +scripts/exp/udp_64B_vm_mask4.pcap +scripts/exp/udp_64B_vm_mask5.pcap +scripts/exp/udp_64B_vm_mask6.pcap + #files generated by global diff --git a/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py b/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py index 03dbcf82..e6a82cf1 100644 --- a/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py +++ b/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py @@ -177,12 +177,17 @@ class CStlBasic_Test(functional_general_test.CGeneralFunctional_Test): ["udp_1pkt_simple_mac_dst.py","-m 1 -l 1 ",True], ["udp_1pkt_simple_mac_src.py","-m 1 -l 1 ",True], ["udp_1pkt_simple_mac_dst_src.py","-m 1 -l 1 ",True], - ["burst_3st_loop_x_times.py","-m 1 -l 20 ",True] + ["burst_3st_loop_x_times.py","-m 1 -l 20 ",True], + ["udp_1pkt_mac_step.py","-m 1 -l 20 ",True], + ["udp_1pkt_mac_mask1.py","-m 1 -l 20 ",True] , + ["udp_1pkt_mac_mask2.py","-m 1 -l 20 ",True], + ["udp_1pkt_mac_mask3.py","-m 1 -l 20 ",True] + ]; - p1 = [ ["udp_1pkt_mac_step.py","-m 1 -l 20 ",True] ] + p1 = [ ["udp_1pkt_mpls_vm.py","-m 1 ",True] ] for obj in p: diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py index 9ce9f7f1..d19dcaf0 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py @@ -16,6 +16,7 @@ STLPktBuilder = CScapyTRexPktBuilder # VM STLVmFlowVar = CTRexVmDescFlowVar STLVmWrFlowVar = CTRexVmDescWrFlowVar +STLVmWrMaskFlowVar = CTRexVmDescWrMaskFlowVar STLVmFixIpv4 = CTRexVmDescFixIpv4 STLVmTrimPktSize = CTRexVmDescTrimPktSize STLVmTupleGen = CTRexVmDescTupleGen diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py index e028d6d5..24a7301b 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py @@ -234,6 +234,22 @@ class CTRexVmInsWrFlowVar(CTRexVmInsBase): self.is_big_endian = is_big_endian assert type(is_big_endian)==bool, 'type of is_big_endian is not bool' +class CTRexVmInsWrMaskFlowVar(CTRexVmInsBase): + def __init__(self, fv_name, pkt_offset,pkt_cast_size,mask,shift, is_big_endian=True): + super(CTRexVmInsWrMaskFlowVar, self).__init__("write_mask_flow_var") + self.name = fv_name + assert type(fv_name)==str, 'type of fv_name is not str' + self.pkt_offset = pkt_offset + assert type(pkt_offset)==int, 'type of pkt_offset is not int' + self.pkt_cast_size = pkt_cast_size + assert type(pkt_cast_size)==int, 'type of pkt_cast_size is not int' + self.mask = mask + assert type(mask)==int, 'type of mask is not int' + self.shift = shift + assert type(shift)==int, 'type of shift is not int' + self.is_big_endian = is_big_endian + assert type(is_big_endian)==bool, 'type of is_big_endian is not bool' + class CTRexVmInsTrimPktSize(CTRexVmInsBase): def __init__(self,fv_name): super(CTRexVmInsTrimPktSize, self).__init__("trim_pkt_size") @@ -562,6 +578,37 @@ class CTRexVmDescWrFlowVar(CTRexVmDescBase): t=parent._name_to_offset(self.pkt_offset) self.pkt_offset = t[0] +class CTRexVmDescWrMaskFlowVar(CTRexVmDescBase): + def __init__(self, fv_name, pkt_offset, pkt_cast_size=1, mask=0xff, shift=0, offset_fixup=0, is_big=True): + super(CTRexVmDescWrMaskFlowVar, self).__init__() + self.name =fv_name + assert type(fv_name)==str, 'type of fv_name is not str' + self.offset_fixup =offset_fixup + assert type(offset_fixup)==int, 'type of offset_fixup is not int' + self.pkt_offset =pkt_offset + self.pkt_cast_size =pkt_cast_size + assert type(pkt_cast_size)==int,'type of pkt_cast_size is not int' + if not (pkt_cast_size in [1,2,4]): + raise CTRexPacketBuildException(-10,"not valid cast size"); + + self.mask = mask + assert type(mask)==int,'type of mask is not int' + self.shift = shift + assert type(shift)==int,'type of shift is not int' + self.is_big =is_big; + assert type(is_big)==bool,'type of is_big_endian is not bool' + + def get_var_ref (self): + return self.name + + def get_obj (self): + return CTRexVmInsWrMaskFlowVar(self.name,self.pkt_offset+self.offset_fixup,self.pkt_cast_size,self.mask,self.shift,self.is_big) + + def compile(self,parent): + if type(self.pkt_offset)==str: + t=parent._name_to_offset(self.pkt_offset) + self.pkt_offset = t[0] + class CTRexVmDescTrimPktSize(CTRexVmDescBase): def __init__(self,fv_name): diff --git a/scripts/exp/udp_1pkt_mac_mask1.pcap b/scripts/exp/udp_1pkt_mac_mask1.pcap Binary files differnew file mode 100644 index 00000000..65adb5eb --- /dev/null +++ b/scripts/exp/udp_1pkt_mac_mask1.pcap diff --git a/scripts/exp/udp_1pkt_mac_mask2.pcap b/scripts/exp/udp_1pkt_mac_mask2.pcap Binary files differnew file mode 100644 index 00000000..07b00113 --- /dev/null +++ b/scripts/exp/udp_1pkt_mac_mask2.pcap diff --git a/scripts/exp/udp_1pkt_mac_mask3.pcap b/scripts/exp/udp_1pkt_mac_mask3.pcap Binary files differnew file mode 100644 index 00000000..d168dee0 --- /dev/null +++ b/scripts/exp/udp_1pkt_mac_mask3.pcap diff --git a/scripts/exp/udp_1pkt_mpls_vm.pcap b/scripts/exp/udp_1pkt_mpls_vm.pcap Binary files differindex f205b334..23fdc7fe 100644 --- a/scripts/exp/udp_1pkt_mpls_vm.pcap +++ b/scripts/exp/udp_1pkt_mpls_vm.pcap diff --git a/scripts/exp/udp_64B_mask1-ex.pcap b/scripts/exp/udp_64B_mask1-ex.pcap Binary files differnew file mode 100644 index 00000000..41005eeb --- /dev/null +++ b/scripts/exp/udp_64B_mask1-ex.pcap diff --git a/scripts/exp/udp_64B_vm_mask1-ex.pcap b/scripts/exp/udp_64B_vm_mask1-ex.pcap Binary files differnew file mode 100644 index 00000000..80d4e370 --- /dev/null +++ b/scripts/exp/udp_64B_vm_mask1-ex.pcap diff --git a/scripts/exp/udp_64B_vm_mask2-ex.pcap b/scripts/exp/udp_64B_vm_mask2-ex.pcap Binary files differnew file mode 100644 index 00000000..89e43ebe --- /dev/null +++ b/scripts/exp/udp_64B_vm_mask2-ex.pcap diff --git a/scripts/exp/udp_64B_vm_mask3-ex.pcap b/scripts/exp/udp_64B_vm_mask3-ex.pcap Binary files differnew file mode 100644 index 00000000..e8f7afee --- /dev/null +++ b/scripts/exp/udp_64B_vm_mask3-ex.pcap diff --git a/scripts/exp/udp_64B_vm_mask4-ex.pcap b/scripts/exp/udp_64B_vm_mask4-ex.pcap Binary files differnew file mode 100644 index 00000000..4275b196 --- /dev/null +++ b/scripts/exp/udp_64B_vm_mask4-ex.pcap diff --git a/scripts/exp/udp_64B_vm_mask5-ex.pcap b/scripts/exp/udp_64B_vm_mask5-ex.pcap Binary files differnew file mode 100644 index 00000000..a738c780 --- /dev/null +++ b/scripts/exp/udp_64B_vm_mask5-ex.pcap diff --git a/scripts/exp/udp_64B_vm_mask6-ex.pcap b/scripts/exp/udp_64B_vm_mask6-ex.pcap Binary files differnew file mode 100644 index 00000000..d791abc5 --- /dev/null +++ b/scripts/exp/udp_64B_vm_mask6-ex.pcap diff --git a/scripts/stl/udp_1pkt_1mac_step.py b/scripts/stl/udp_1pkt_1mac_step.py new file mode 100644 index 00000000..bab46fb7 --- /dev/null +++ b/scripts/stl/udp_1pkt_1mac_step.py @@ -0,0 +1,35 @@ +from trex_stl_lib.api import * + + +# step is not 1. +class STLS1(object): + + def __init__ (self): + self.fsize =64; # the size of the packet + + def create_stream (self): + + # create a base packet and pad it to size + size = self.fsize - 4; # no FCS + base_pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025) + pad = max(0, size - len(base_pkt)) * 'x' + + vm = CTRexScRaw( [ STLVmFlowVar(name="mac_src", min_value=1, max_value=30, size=1, op="dec",step=7), + STLVmWrFlowVar(fv_name="mac_src", pkt_offset= 11) # write it to LSB of SRC offset it 11 + ] + ) + + return STLStream(packet = STLPktBuilder(pkt = base_pkt/pad,vm = vm), + mode = STLTXCont( pps=10 )) + + 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_mac_mask1.py b/scripts/stl/udp_1pkt_mac_mask1.py new file mode 100644 index 00000000..37f5079d --- /dev/null +++ b/scripts/stl/udp_1pkt_mac_mask1.py @@ -0,0 +1,35 @@ +from trex_stl_lib.api import * + + +# step is not 1. +class STLS1(object): + + def __init__ (self): + self.fsize =64; # the size of the packet + + def create_stream (self): + + # create a base packet and pad it to size + size = self.fsize - 4; # no FCS + base_pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025) + pad = max(0, size - len(base_pkt)) * 'x' + + vm = CTRexScRaw( [ STLVmFlowVar(name="mac_src", min_value=1, max_value=30, size=2, op="dec",step=1), + STLVmWrMaskFlowVar(fv_name="mac_src", pkt_offset= 11,pkt_cast_size=1, mask=0xff) # mask command ->write it as one byte + ] + ) + + return STLStream(packet = STLPktBuilder(pkt = base_pkt/pad,vm = vm), + mode = STLTXCont( pps=10 )) + + 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_mac_mask2.py b/scripts/stl/udp_1pkt_mac_mask2.py new file mode 100644 index 00000000..b72f0c3b --- /dev/null +++ b/scripts/stl/udp_1pkt_mac_mask2.py @@ -0,0 +1,35 @@ +from trex_stl_lib.api import * + + +# step is not 1. +class STLS1(object): + + def __init__ (self): + self.fsize =64; # the size of the packet + + def create_stream (self): + + # create a base packet and pad it to size + size = self.fsize - 4; # no FCS + base_pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025) + pad = max(0, size - len(base_pkt)) * 'x' + + vm = CTRexScRaw( [ STLVmFlowVar(name="mac_src", min_value=1, max_value=30, size=2, op="dec",step=1), + STLVmWrMaskFlowVar(fv_name="mac_src", pkt_offset= 10,pkt_cast_size=2, mask=0xff00,shift=8) # take the var shift it 8 (x256) write only to LSB + ] + ) + + return STLStream(packet = STLPktBuilder(pkt = base_pkt/pad,vm = vm), + mode = STLTXCont( pps=10 )) + + 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_mac_mask3.py b/scripts/stl/udp_1pkt_mac_mask3.py new file mode 100644 index 00000000..ef5bc4f0 --- /dev/null +++ b/scripts/stl/udp_1pkt_mac_mask3.py @@ -0,0 +1,35 @@ +from trex_stl_lib.api import * + + +# step is not 1. +class STLS1(object): + + def __init__ (self): + self.fsize =64; # the size of the packet + + def create_stream (self): + + # create a base packet and pad it to size + size = self.fsize - 4; # no FCS + base_pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025) + pad = max(0, size - len(base_pkt)) * 'x' + + vm = CTRexScRaw( [ STLVmFlowVar(name="mac_src", min_value=1, max_value=30, size=2, op="dec",step=1), + STLVmWrMaskFlowVar(fv_name="mac_src", pkt_offset= 10,pkt_cast_size=1, mask=0x1,shift=-1) # take var mac_src>>1 and write the LSB every two packet there should be a change + ] + ) + + return STLStream(packet = STLPktBuilder(pkt = base_pkt/pad,vm = vm), + mode = STLTXCont( pps=10 )) + + 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_mpls_vm.py b/scripts/stl/udp_1pkt_mpls_vm.py index 262a662f..57cfb7ee 100644 --- a/scripts/stl/udp_1pkt_mpls_vm.py +++ b/scripts/stl/udp_1pkt_mpls_vm.py @@ -12,7 +12,7 @@ class STLS1(object): pkt = Ether()/MPLS(label=17,cos=1,s=0,ttl=255)/MPLS(label=0,cos=1,s=1,ttl=12)/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/('x'*20) vm = CTRexScRaw( [ STLVmFlowVar(name="mlabel", min_value=1, max_value=2000, size=2, op="inc"), # 2 bytes var - STLVmWrFlowVar(fv_name="mlabel", pkt_offset= "MPLS:1.label") # LABEL is 20 bits expected is val*8 as 3 LSB are off, 16,32,64 .. using new instruction it will be possible to write to any bits + STLVmWrMaskFlowVar(fv_name="mlabel", pkt_offset= "MPLS:1.label",pkt_cast_size=4, mask=0xFFFFF000,shift=12) # write to 20bit MSB ] ) diff --git a/scripts/stl/yaml/imix_1pkt_vm_minus.yaml b/scripts/stl/yaml/imix_1pkt_vm_minus.yaml new file mode 100644 index 00000000..bf67c83c --- /dev/null +++ b/scripts/stl/yaml/imix_1pkt_vm_minus.yaml @@ -0,0 +1,33 @@ +### Single stream UDP packet, 64B ### +##################################### +- name: udp_64B + stream: + self_start: True + packet: + pcap: udp_64B_no_crc.pcap # pcap should not include CRC + mode: + type: continuous + pps: 100 + rx_stats: [] + + vm: + instructions: [ + { + "init_value" : 500, + "max_value" : 75000, + "min_value" : 1000, + "name" : "l3_src", + "op" : "inc", + "size" : 2, + "type" : "flow_var" + }, + { + "add_value" : 1, + "is_big_endian" : false, + "name" : "l3_src", + "pkt_offset" : 34, + "type" : "write_flow_var" + } + ] + split_by_var: "l3_src" + diff --git a/src/gtest/trex_stateless_gtest.cpp b/src/gtest/trex_stateless_gtest.cpp index 68f9a4b7..3faaedeb 100644 --- a/src/gtest/trex_stateless_gtest.cpp +++ b/src/gtest/trex_stateless_gtest.cpp @@ -724,6 +724,351 @@ TEST_F(basic_vm, vm7) { EXPECT_EQ(1, res1?1:0); } + +//////////////////////////////////////////////////////// + +TEST_F(basic_vm, vm_mask_err) { + + bool fail=false; + /* should fail */ + + try { + StreamVm vm; + vm.add_instruction( new StreamVmInstructionFixChecksumIpv4(14) ); + vm.compile(128); + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + printf(" program_size : %lu \n",(ulong)program_size); + } catch (const TrexException &ex) { + fail=true; + } + + EXPECT_EQ(true, fail); +} + + +TEST_F(basic_vm, vm_mask1) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,0x10000007,0x10000007,0x100000fe) ); + + + vm.add_instruction( new StreamVmInstructionWriteMaskToPkt("var1", 36,2,0x00ff,0,1) ); + + vm.compile(128); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm_mask1.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + uint32_t random_per_thread=0; + + int i; + for (i=0; i<20; i++) { + runner.run(&random_per_thread, + program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm_mask1.pcap","exp/udp_64B_vm_mask1-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + + +TEST_F(basic_vm, vm_mask2) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,0x10000007,0x10000007,0x100000fe) ); + + + vm.add_instruction( new StreamVmInstructionWriteMaskToPkt("var1", 36,2,0xff00,8,1) ); + + vm.compile(128); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm_mask2.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + uint32_t random_per_thread=0; + + int i; + for (i=0; i<20; i++) { + runner.run(&random_per_thread, + program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm_mask2.pcap","exp/udp_64B_vm_mask2-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +TEST_F(basic_vm, vm_mask3) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,0x10000007,0x10000007,0x100000fe) ); + + + vm.add_instruction( new StreamVmInstructionWriteMaskToPkt("var1", 36,1,0x2,1,1) ); + + vm.compile(128); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm_mask3.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + uint32_t random_per_thread=0; + + int i; + for (i=0; i<20; i++) { + runner.run(&random_per_thread, + program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm_mask3.pcap","exp/udp_64B_vm_mask3-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +TEST_F(basic_vm, vm_mask4) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",1 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,1,1,10) ); + + + vm.add_instruction( new StreamVmInstructionWriteMaskToPkt("var1", 36,2,0xFF00,8,1) ); + + vm.compile(128); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm_mask4.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + uint32_t random_per_thread=0; + + int i; + for (i=0; i<20; i++) { + runner.run(&random_per_thread, + program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm_mask4.pcap","exp/udp_64B_vm_mask4-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +TEST_F(basic_vm, vm_mask5) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",1 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,1,1,10) ); + + + vm.add_instruction( new StreamVmInstructionWriteMaskToPkt("var1", 36,4,0x00FF0000,16,1) ); + + vm.compile(128); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm_mask5.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + uint32_t random_per_thread=0; + + int i; + for (i=0; i<20; i++) { + runner.run(&random_per_thread, + program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm_mask5.pcap","exp/udp_64B_vm_mask5-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + + +TEST_F(basic_vm, vm_mask6) { + + + + StreamVm vm; + + vm.add_instruction( new StreamVmInstructionFlowMan( "var1",4 /* size */, + StreamVmInstructionFlowMan::FLOW_VAR_OP_INC,1,1,20) ); + + + vm.add_instruction( new StreamVmInstructionWriteMaskToPkt("var1", 36,2,0x00FF,-1,1) ); + + vm.compile(128); + + + uint32_t program_size=vm.get_dp_instruction_buffer()->get_program_size(); + + printf (" program size : %lu \n",(ulong)program_size); + + + vm.Dump(stdout); + + CPcapLoader pcap; + pcap.load_pcap_file("cap2/udp_64B.pcap",0); + + + CFileWriterBase * lpWriter=CCapWriterFactory::CreateWriter(LIBPCAP,(char *)"exp/udp_64B_vm_mask6.pcap"); + assert(lpWriter); + + + StreamDPVmInstructionsRunner runner; + + uint32_t random_per_thread=0; + + int i; + for (i=0; i<20; i++) { + runner.run(&random_per_thread, + program_size, + vm.get_dp_instruction_buffer()->get_program(), + vm.get_bss_ptr(), + (uint8_t*)pcap.m_raw.raw); + + assert(lpWriter->write_packet(&pcap.m_raw)); + } + + delete lpWriter; + + CErfCmp cmp; + + bool res1=cmp.compare("exp/udp_64B_vm_mask6.pcap","exp/udp_64B_vm_mask6-ex.pcap"); + EXPECT_EQ(1, res1?1:0); +} + +//////////////////////////////////////////////////////// + + TEST_F(basic_vm, vm8) { diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index 508967b9..920991e2 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -292,6 +292,25 @@ TrexRpcCmdAddStream::parse_vm_instr_flow_var(const Json::Value &inst, TrexStream ); } + +void +TrexRpcCmdAddStream::parse_vm_instr_write_mask_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result) { + std::string flow_var_name = parse_string(inst, "name", result); + uint16_t pkt_offset = parse_uint16(inst, "pkt_offset", result); + uint16_t pkt_cast_size = parse_uint16(inst, "pkt_cast_size", result); + uint32_t mask = parse_uint32(inst, "mask", result); + int shift = parse_int(inst, "shift", result); + bool is_big_endian = parse_bool(inst, "is_big_endian", result); + + stream->m_vm.add_instruction(new StreamVmInstructionWriteMaskToPkt(flow_var_name, + pkt_offset, + (uint8_t)pkt_cast_size, + mask, + shift, + is_big_endian)); +} + + void TrexRpcCmdAddStream::parse_vm_instr_write_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result) { std::string flow_var_name = parse_string(inst, "name", result); @@ -314,7 +333,7 @@ TrexRpcCmdAddStream::parse_vm(const Json::Value &vm, TrexStream *stream, Json::V 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"}; + auto vm_types = {"fix_checksum_ipv4", "flow_var", "write_flow_var","tuple_flow_var","trim_pkt_size","write_mask_flow_var"}; std::string vm_type = parse_choice(inst, "type", vm_types, result); // checksum instruction @@ -332,6 +351,8 @@ TrexRpcCmdAddStream::parse_vm(const Json::Value &vm, TrexStream *stream, Json::V } else if (vm_type == "trim_pkt_size") { parse_vm_instr_trim_pkt_size(inst, stream, result); + }else if (vm_type == "write_mask_flow_var") { + parse_vm_instr_write_mask_flow_var(inst, stream, result); } else { /* internal error */ throw TrexRpcException("internal error"); diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index 48a38780..3dc2ce0a 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -100,6 +100,8 @@ void parse_vm_instr_tuple_flow_var(const Json::Value &inst, TrexStream *stream, void parse_vm_instr_trim_pkt_size(const Json::Value &inst, TrexStream *stream, Json::Value &result); void parse_vm_instr_write_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result); +void parse_vm_instr_write_mask_flow_var(const Json::Value &inst, TrexStream *stream, Json::Value &result); + ); diff --git a/src/stateless/cp/trex_stream_vm.cpp b/src/stateless/cp/trex_stream_vm.cpp index f99c3c9c..f83025dd 100644 --- a/src/stateless/cp/trex_stream_vm.cpp +++ b/src/stateless/cp/trex_stream_vm.cpp @@ -83,6 +83,11 @@ void StreamVmInstructionFlowMan::sanity_check(uint32_t ins_id,StreamVm *lp){ } +void StreamVmInstructionWriteMaskToPkt::Dump(FILE *fd){ + fprintf(fd," flow_var:%s, offset:%lu, cast_size:%lu, mask:0x%lx, shift:%ld, is_big:%lu \n",m_flow_var_name.c_str(),(ulong)m_pkt_offset,(ulong)m_pkt_cast_size,(ulong)m_mask,(long)m_shift,(ulong)(m_is_big_endian?1:0)); +} + + void StreamVmInstructionFlowMan::Dump(FILE *fd){ fprintf(fd," flow_var , %s ,%lu, ",m_var_name.c_str(),(ulong)m_size_bytes); @@ -380,6 +385,7 @@ void StreamVm::build_program(){ /* build the commands into a buffer */ m_instructions.clear(); + int var_cnt=0; clean_max_field_cnt(); uint32_t ins_id=0; @@ -410,6 +416,7 @@ void StreamVm::build_program(){ if (ins_type == StreamVmInstruction::itFLOW_MAN) { StreamVmInstructionFlowMan *lpMan =(StreamVmInstructionFlowMan *)inst; + var_cnt++; if (lpMan->m_size_bytes == 1 ){ if ( (lpMan->m_step == 1) || (lpMan->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_RANDOM ) ){ @@ -663,8 +670,64 @@ void StreamVm::build_program(){ } + if (ins_type == StreamVmInstruction::itPKT_WR_MASK) { + StreamVmInstructionWriteMaskToPkt *lpPkt =(StreamVmInstructionWriteMaskToPkt *)inst; + + VmFlowVarRec var; + + uint8_t cast_size = lpPkt->m_pkt_cast_size; + if (!((cast_size==4)||(cast_size==2)||(cast_size==1))){ + std::stringstream ss; + ss << "instruction id '" << ins_id << " cast size should be 1,2,4 it is "<<lpPkt->m_pkt_cast_size; + err(ss.str()); + } + + if ( var_lookup(lpPkt->m_flow_var_name ,var) == false){ + + std::stringstream ss; + ss << "instruction id '" << ins_id << "' packet write with no valid flow varible name '" << lpPkt->m_flow_var_name << "'" ; + err(ss.str()); + } + + if (lpPkt->m_pkt_offset + lpPkt->m_pkt_cast_size > m_pkt_size ) { + std::stringstream ss; + ss << "instruction id '" << ins_id << "' packet write with packet_offset " << (lpPkt->m_pkt_offset + lpPkt->m_pkt_cast_size) << " bigger than packet size "<< m_pkt_size; + err(ss.str()); + } + + + add_field_cnt(lpPkt->m_pkt_offset + lpPkt->m_pkt_cast_size); + + + uint8_t op_size = var.m_size_bytes; + bool is_big = lpPkt->m_is_big_endian; + uint8_t flags = (is_big?StreamDPOpPktWrMask::MASK_PKT_WR_IS_BIG:0); + uint8_t flow_offset = get_var_offset(lpPkt->m_flow_var_name); + + /* read LSB in case of 64bit varible */ + if (op_size == 8) { + op_size = 4; + if ( is_big ) { + flow_offset +=4; + } + } + + StreamDPOpPktWrMask pmask; + pmask.m_op = StreamDPVmInstructions::itPKT_WR_MASK; + pmask.m_flags = flags; + pmask.m_var_offset = flow_offset; + pmask.m_shift = lpPkt->m_shift; + pmask.m_pkt_cast_size = cast_size; + pmask.m_flowv_cast_size = op_size; + pmask.m_pkt_offset = lpPkt->m_pkt_offset; + pmask.m_mask = lpPkt->m_mask; + + m_instructions.add_command(&pmask,sizeof(pmask)); + } + if (ins_type == StreamVmInstruction::itFLOW_CLIENT) { + var_cnt++; StreamVmInstructionFlowClient *lpMan =(StreamVmInstructionFlowClient *)inst; if ( lpMan->is_unlimited_flows() ){ @@ -722,6 +785,13 @@ void StreamVm::build_program(){ ins_id++; } + + + if ( var_cnt ==0 ){ + std::stringstream ss; + ss << "It is not valid to have a VM program without a variable or tuple generator "; + err(ss.str()); + } } @@ -985,6 +1055,7 @@ void StreamDPVmInstructions::Dump(FILE *fd){ StreamDPOpPktWr16 *lpw16; StreamDPOpPktWr32 *lpw32; StreamDPOpPktWr64 *lpw64; + StreamDPOpPktWrMask *lpwrmask; StreamDPOpClientsLimit *lp_client; StreamDPOpClientsUnLimit *lp_client_unlimited; StreamDPOpPktSizeChange *lp_pkt_size_change; @@ -1148,6 +1219,12 @@ void StreamDPVmInstructions::Dump(FILE *fd){ p+=sizeof(StreamDPOpFlowVar64Step); break; + case itPKT_WR_MASK : + lpwrmask =(StreamDPOpPktWrMask *)p; + lpwrmask->dump(fd,"WR_MASK"); + p+=sizeof(StreamDPOpPktWrMask); + break; + default: assert(0); } @@ -1204,6 +1281,10 @@ void StreamDPOpPktWr64::dump(FILE *fd,std::string opt){ fprintf(fd," %10s op:%lu, flags:%lu, pkt_of:%lu , f_of:%lu \n", opt.c_str(),(ulong)m_op,(ulong)m_flags,(ulong)m_pkt_offset,(ulong)m_offset); } +void StreamDPOpPktWrMask::dump(FILE *fd,std::string opt){ + fprintf(fd," %10s op:%lu, flags:%lu, var_of:%lu , (%ld-%lu-%lu-%lu-%lu) \n", opt.c_str(),(ulong)m_op,(ulong)m_flags,(ulong)m_var_offset,(long)m_shift,(ulong)m_pkt_offset,(ulong)m_mask,(ulong)m_pkt_cast_size,(ulong)m_flowv_cast_size); +} + void StreamDPOpIpv4Fix::dump(FILE *fd,std::string opt){ fprintf(fd," %10s op:%lu, offset: %lu \n", opt.c_str(),(ulong)m_op,(ulong)m_offset); @@ -1224,6 +1305,86 @@ void StreamDPOpPktSizeChange::dump(FILE *fd,std::string opt){ + +void StreamDPOpPktWrMask::wr(uint8_t * flow_var_base, + uint8_t * pkt_base) { + uint32_t val=0; + uint8_t * pv=(flow_var_base+m_var_offset); + /* read flow var with the right size */ + switch (m_flowv_cast_size) { + case 1: + val= (uint32_t)(*((uint8_t*)pv)); + break; + case 2: + val=(uint32_t)(*((uint16_t*)pv)); + break; + case 4: + val=(*((uint32_t*)pv)); + break; + default: + assert(0); + } + + /* shift the flow var val */ + if (m_shift>0) { + val=val<<m_shift; + }else{ + if (m_shift<0) { + val=val>>(-m_shift); + } + } + + uint8_t * p_pkt = pkt_base+m_pkt_offset; + uint32_t pkt_val=0; + + /* RMW */ + if ( likely( is_big() ) ) { + + switch (m_pkt_cast_size) { + case 1: + pkt_val= (uint32_t)(*((uint8_t*)p_pkt)); + pkt_val = ((pkt_val & ~m_mask) | (val & m_mask)) & 0xff; + *p_pkt=pkt_val; + break; + case 2: + pkt_val= (uint32_t)PKT_NTOHS((*((uint16_t*)p_pkt))); + pkt_val = ((pkt_val & ~m_mask) | (val & m_mask)) & 0xffff; + *((uint16_t*)p_pkt)=PKT_NTOHS(pkt_val); + break; + case 4: + pkt_val= (uint32_t)PKT_NTOHL((*((uint32_t*)p_pkt))); + pkt_val = ((pkt_val & ~m_mask) | (val & m_mask)) ; + *((uint32_t*)p_pkt)=PKT_NTOHL(pkt_val); + break; + default: + assert(0); + } + }else{ + switch (m_flowv_cast_size) { + case 1: + pkt_val= (uint32_t)(*((uint8_t*)p_pkt)); + pkt_val = ((pkt_val & ~m_mask) | (val & m_mask)) & 0xff; + *p_pkt=pkt_val; + break; + case 2: + pkt_val= (uint32_t)(*((uint16_t*)p_pkt)); + pkt_val = ((pkt_val & ~m_mask) | (val & m_mask)) & 0xffff; + *((uint16_t*)p_pkt)=pkt_val; + break; + case 4: + pkt_val= (uint32_t)(*((uint32_t*)p_pkt)); + pkt_val = ((pkt_val & ~m_mask) | (val & m_mask)) ; + *((uint32_t*)p_pkt)=pkt_val; + break; + default: + assert(0); + } + } +} + + + + void StreamDPVmInstructionsRunner::slow_commands(uint8_t op_code, uint8_t * flow_var, /* flow var */ uint8_t * pkt, @@ -1274,8 +1435,16 @@ void StreamDPVmInstructionsRunner::slow_commands(uint8_t op_code, ua.lpv64s->run_dec(flow_var); p+=sizeof(StreamDPOpFlowVar64Step); break; + case StreamDPVmInstructions::itPKT_WR_MASK: + ua.lpwr_mask =(StreamDPOpPktWrMask *)p; + ua.lpwr_mask->wr(flow_var,pkt); + p+=sizeof(StreamDPOpPktWrMask); + break; + default: assert(0); } } + + diff --git a/src/stateless/cp/trex_stream_vm.h b/src/stateless/cp/trex_stream_vm.h index fd685b24..13504703 100644 --- a/src/stateless/cp/trex_stream_vm.h +++ b/src/stateless/cp/trex_stream_vm.h @@ -429,6 +429,37 @@ public: } __attribute__((packed)); + +struct StreamDPOpPktWrMask { + + enum { + MASK_PKT_WR_IS_BIG = 1 + }; /* for flags */ + + uint8_t m_op; + uint8_t m_flags; + uint8_t m_var_offset; + int8_t m_shift; + uint8_t m_pkt_cast_size; + uint8_t m_flowv_cast_size; /* 1,2,4 */ + uint16_t m_pkt_offset; + uint32_t m_mask; + bool is_big(){ + return ( (m_flags &StreamDPOpPktWrMask::MASK_PKT_WR_IS_BIG) == StreamDPOpPktWrMask::MASK_PKT_WR_IS_BIG ?true:false); + } + + +public: + void dump(FILE *fd,std::string opt); + + void wr(uint8_t * flow_var_base, + uint8_t * pkt_base) ; + +} __attribute__((packed)); + + + + struct StreamDPOpIpv4Fix { uint8_t m_op; uint16_t m_offset; @@ -572,6 +603,7 @@ public: ditDEC16_STEP , ditDEC32_STEP , ditDEC64_STEP , + itPKT_WR_MASK }; @@ -637,6 +669,8 @@ typedef union ua_ { StreamDPOpFlowVar16Step *lpv16s; StreamDPOpFlowVar32Step *lpv32s; StreamDPOpFlowVar64Step *lpv64s; + StreamDPOpPktWrMask *lpwr_mask; + } ua_t ; @@ -792,7 +826,8 @@ public: itFLOW_MAN = 5, itPKT_WR = 6, itFLOW_CLIENT = 7 , - itPKT_SIZE_CHANGE = 8 + itPKT_SIZE_CHANGE = 8, + itPKT_WR_MASK = 9 }; @@ -980,6 +1015,69 @@ public: /** + * write flow-write-mask to packet, hhaim + * + * uint32_t var_tmp=(uint32_t )(flow_var_t size )flow_var; + * if (shift){ + * var_tmp=var_tmp<<shift + * }else{ + * var_tmp=var_tmp>>shift + * } + * + * pkt_data=read_pkt_size() + * pkt_data = (pkt_data & ~mask) |(var_tmp & mask) + * write_pkt(pkt_data) + * + */ +class StreamVmInstructionWriteMaskToPkt : public StreamVmInstruction { +public: + + StreamVmInstructionWriteMaskToPkt(const std::string &flow_var_name, + uint16_t pkt_offset, + uint8_t pkt_cast_size, /* valid 1,2,4 */ + uint32_t mask, + int shift, /* positive is shift left, negetive shift right */ + bool is_big_endian = true) : + m_flow_var_name(flow_var_name), + m_pkt_offset(pkt_offset), + m_pkt_cast_size(pkt_cast_size), + m_mask(mask), + m_shift(shift), + m_is_big_endian(is_big_endian) {} + + virtual instruction_type_t get_instruction_type() const { + return ( StreamVmInstruction::itPKT_WR_MASK); + } + + virtual void Dump(FILE *fd); + + virtual StreamVmInstruction * clone() { + return new StreamVmInstructionWriteMaskToPkt(m_flow_var_name, + m_pkt_offset, + m_pkt_cast_size, + m_mask, + m_shift, + m_is_big_endian); + } + +public: + + /* flow var name to write */ + std::string m_flow_var_name; + + /* where to write */ + uint16_t m_pkt_offset; + uint8_t m_pkt_cast_size; /* valid 1,2,4 */ + + uint32_t m_mask; + int m_shift; + bool m_is_big_endian; +}; + + + + +/** * flow client instruction - save state for client range and port range * * @author hhaim @@ -1081,10 +1179,10 @@ public: class StreamVmInstructionWriteToPkt : public StreamVmInstruction { public: - StreamVmInstructionWriteToPkt(const std::string &flow_var_name, - uint16_t pkt_offset, - int32_t add_value = 0, - bool is_big_endian = true) : + StreamVmInstructionWriteToPkt(const std::string &flow_var_name, + uint16_t pkt_offset, + int32_t add_value = 0, + bool is_big_endian = true) : m_flow_var_name(flow_var_name), m_pkt_offset(pkt_offset), |