summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2016-02-02 18:44:47 +0200
committerYaroslav Brustinov <ybrustin@cisco.com>2016-02-02 18:44:47 +0200
commit88bd6a73ac4d68323951fdb92ae905e35d0e52e0 (patch)
treedccb12140acea31a9b6d9a23a63a31dacbcf5e70 /scripts
parent3e0b536cf8a70ba98c0d29935be0767fe92922c8 (diff)
Fix Dan's packet builder payload_gen reference to packet, HLTAPI updated for Scapy builder, Scapy builder style corrections
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/automation/trex_control_plane/client/trex_hltapi.py205
-rwxr-xr-xscripts/automation/trex_control_plane/client_utils/packet_builder.py17
-rw-r--r--scripts/automation/trex_control_plane/client_utils/scapy_packet_builder.py213
3 files changed, 231 insertions, 204 deletions
diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py
index 59873d49..3bbf62e0 100755
--- a/scripts/automation/trex_control_plane/client/trex_hltapi.py
+++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py
@@ -64,7 +64,7 @@ traffic_stats()
import trex_root_path
-from client_utils.packet_builder import CTRexPktBuilder
+import client_utils.scapy_packet_builder as pkt_bld
from trex_stateless_client import STLClient
from common.trex_streams import *
from client_utils.general_utils import get_integer
@@ -459,19 +459,27 @@ class CTRexHltApi(object):
@staticmethod
def _generate_packet(
+ # L2
+ frame_size = 64,
l2_encap = 'ethernet_ii',
mac_src = '00:00:01:00:00:01',
mac_dst = '00:00:00:00:00:00',
+ # L3 IPv4
l3_protocol = 'ipv4',
ip_tos_field = 0,
- l3_length = 110,
+ l3_length = 50,
ip_id = 0,
ip_fragment_offset = 0,
ip_ttl = 64,
- ip_checksum = 0,
+ ip_checksum = None,
ip_src_addr = '0.0.0.0',
ip_dst_addr = '192.0.0.1',
+
+ # L3 IPv4 FE
+ ip_src_mode = 'fixed',
+ ip_src_step = 1,
+ ip_src_count = 1,
l4_protocol = 'tcp',
tcp_src_port = 1024,
@@ -486,113 +494,114 @@ class CTRexHltApi(object):
tcp_ack_flag = 0,
tcp_urg_flag = 0,
tcp_window = 4069,
- tcp_checksum = 0,
+ tcp_checksum = None,
tcp_urgent_ptr = 0,
**kwargs):
- ALLOWED_L3_PROTOCOL = {'ipv4': dpkt.ethernet.ETH_TYPE_IP,
- #'ipv6': dpkt.ethernet.ETH_TYPE_IP6,
- #'arp': dpkt.ethernet.ETH_TYPE_ARP
- }
- ALLOWED_L4_PROTOCOL = {'tcp': dpkt.ip.IP_PROTO_TCP,
- #'udp': dpkt.ip.IP_PROTO_UDP,
- #'icmp': dpkt.ip.IP_PROTO_ICMP,
- #'icmpv6': dpkt.ip.IP_PROTO_ICMP6,
- #'igmp': dpkt.ip.IP_PROTO_IGMP,
- #'rtp': dpkt.ip.IP_PROTO_IRTP,
- #'isis': dpkt.ip.IP_PROTO_ISIS,
- #'ospf': dpkt.ip.IP_PROTO_OSPF
- }
-
- pkt_bld = CTRexPktBuilder()
+
+ pkt_plus_vm = pkt_bld.CScapyTRexPktBuilder()
+
+ ### L2 ###
if l2_encap == 'ethernet_ii':
- #('dst', '6s', ''),
- #('src', '6s', ''),
- #('type', 'H', ETH_TYPE_IP)
- pkt_bld.add_pkt_layer('l2', dpkt.ethernet.Ethernet())
- # set Ethernet layer attributes
- pkt_bld.set_eth_layer_addr('l2', 'src', mac_src)
- pkt_bld.set_eth_layer_addr('l2', 'dst', mac_dst)
+ #fields_desc = [ MACField("dst","00:00:00:01:00:00"),
+ # MACField("src","00:00:00:02:00:00"),
+ # XShortEnumField("type", 0x9000, ETHER_TYPES) ]
+ pkt = pkt_bld.Ether(src = mac_src, dst = mac_dst)
else:
- raise NotImplementedError("l2_encap does not support the desired encapsulation '{0}'".format(l2_encap))
- # set l3 on l2
- if l3_protocol not in ALLOWED_L3_PROTOCOL:
- raise ValueError('l3_protocol must be one of the following: {0}'.format(ALLOWED_L3_PROTOCOL))
- pkt_bld.set_layer_attr('l2', 'type', ALLOWED_L3_PROTOCOL[l3_protocol])
+ raise NotImplementedError("l2_encap does not support the desired encapsulation '%s'" % l2_encap)
- # set l3 attributes
+ ### L3 ###
if l3_protocol == 'ipv4':
- #('v_hl', 'B', (4 << 4) | (20 >> 2)),
- #('tos', 'B', 0),
- #('len', 'H', 20),
- #('id', 'H', 0),
- #('off', 'H', 0),
- #('ttl', 'B', 64),
- #('p', 'B', 0),
- #('sum', 'H', 0),
- #('src', '4s', '\x00' * 4),
- #('dst', '4s', '\x00' * 4)
- pkt_bld.add_pkt_layer('l3', dpkt.ip.IP())
- pkt_bld.set_layer_attr('l3', 'tos', ip_tos_field)
- pkt_bld.set_layer_attr('l3', 'len', l3_length)
- pkt_bld.set_layer_attr('l3', 'id', ip_id)
- pkt_bld.set_layer_attr('l3', 'off', ip_fragment_offset)
- pkt_bld.set_layer_attr('l3', 'ttl', ip_ttl)
- pkt_bld.set_layer_attr('l3', 'sum', ip_checksum)
- pkt_bld.set_ip_layer_addr('l3', 'src', ip_src_addr)
- pkt_bld.set_ip_layer_addr('l3', 'dst', ip_dst_addr)
+ #fields_desc = [ BitField("version" , 4 , 4),
+ # BitField("ihl", None, 4),
+ # XByteField("tos", 0),
+ # ShortField("len", None),
+ # ShortField("id", 1),
+ # FlagsField("flags", 0, 3, ["MF","DF","evil"]),
+ # BitField("frag", 0, 13),
+ # ByteField("ttl", 64),
+ # ByteEnumField("proto", 0, IP_PROTOS),
+ # XShortField("chksum", None),
+ # #IPField("src", "127.0.0.1"),
+ # #Emph(SourceIPField("src","dst")),
+ # Emph(IPField("src", "16.0.0.1")),
+ # Emph(IPField("dst", "48.0.0.1")),
+ # PacketListField("options", [], IPOption, length_from=lambda p:p.ihl*4-20) ]
+ pkt /= pkt_bld.IP(tos = ip_tos_field,
+ len = l3_length,
+ id = ip_id,
+ frag = ip_fragment_offset,
+ ttl = ip_ttl,
+ chksum = ip_checksum,
+ src = ip_src_addr,
+ dst = ip_dst_addr
+ )
+ # IP FE (Field Engine)
+ print dir(pkt_bld.IP.src.fld)
+ print_r(pkt_bld.IP.src.fld)
+ if ip_src_mode == 'increment':
+ ip_src_vm = pkt_bld.CTRexScRaw([ pkt_bld.CTRexVmDescFlowVar(name='a', min_value='16.0.0.1', max_value='16.0.0.10', init_value='16.0.0.1', size=4, op='inc'),
+ pkt_bld.CTRexVmDescWrFlowVar (fv_name='a', pkt_offset = 'IP.src'),
+ pkt_bld.CTRexVmDescFixIpv4(offset = 'IP')])
+ pkt_plus_vm.add_command(ip_src_vm)
+
+ #print '>> is inc'
+ #if ip_src_step != 1:
+ # return HLT_ERR('ip_src_step has to be 1 (TRex limitation)')
+ #FE_var_ip_src_addr = pkt_bld.vm.CTRexVMFlowVariable('ip_src_addr')
+ #FE_var_ip_src_addr.set_field('size', 4)
+ #FE_var_ip_src_addr.set_field('operation', 'inc')
+ #FE_var_ip_src_addr.set_field('init_value', ip_src_addr)
+ #FE_var_ip_src_addr.set_field('init_value', 1)
+ #FE_var_ip_src_addr.set_field('max_value', ip_src_count)
+ #pkt_bld.vm.vm_var.set_field('split_by_core', False)
else:
- raise NotImplementedError("l3_protocol '{0}' is not supported by TRex yet.".format(l3_protocol))
-
- # set l4 on l3
- if l4_protocol not in ALLOWED_L4_PROTOCOL:
- raise ValueError('l4_protocol must be one of the following: {0}'.format(ALLOWED_L3_PROTOCOL))
- pkt_bld.set_layer_attr('l3', 'p', ALLOWED_L4_PROTOCOL[l4_protocol])
+ raise NotImplementedError("l3_protocol '%s' is not supported by TRex yet." % l3_protocol)
- # set l4 attributes
+ ### L4 ###
if l4_protocol == 'tcp':
- pkt_bld.add_pkt_layer('l4', dpkt.tcp.TCP())
- #('sport', 'H', 0xdead),
- #('dport', 'H', 0),
- #('seq', 'I', 0xdeadbeefL),
- #('ack', 'I', 0),
- #('off_x2', 'B', ((5 << 4) | 0)),
- #('flags', 'B', TH_SYN),
- #('win', 'H', TCP_WIN_MAX),
- #('sum', 'H', 0),
- #('urp', 'H', 0)
- pkt_bld.set_layer_attr('l4', 'sport', tcp_src_port)
- pkt_bld.set_layer_attr('l4', 'dport', tcp_dst_port)
- pkt_bld.set_layer_attr('l4', 'seq', tcp_seq_num)
- pkt_bld.set_layer_attr('l4', 'ack', tcp_ack_num)
- pkt_bld.set_layer_attr('l4', 'off_x2', tcp_data_offset)
- #TH_FIN = 0x01 # end of data
- #TH_SYN = 0x02 # synchronize sequence numbers
- #TH_RST = 0x04 # reset connection
- #TH_PUSH = 0x08 # push
- #TH_ACK = 0x10 # acknowledgment number set
- #TH_URG = 0x20 # urgent pointer set
- #TH_ECE = 0x40 # ECN echo, RFC 3168
- #TH_CWR = 0x80 # congestion window reduced
- tcp_flags = (tcp_fin_flag * dpkt.tcp.TH_FIN +
- tcp_syn_flag * dpkt.tcp.TH_SYN +
- tcp_rst_flag * dpkt.tcp.TH_RST +
- tcp_psh_flag * dpkt.tcp.TH_PUSH +
- tcp_ack_flag * dpkt.tcp.TH_ACK +
- tcp_urg_flag * dpkt.tcp.TH_URG)
- pkt_bld.set_layer_attr('l4', 'flags', tcp_flags)
- pkt_bld.set_layer_attr('l4', 'win', tcp_window)
- pkt_bld.set_layer_attr('l4', 'sum', tcp_checksum)
- pkt_bld.set_layer_attr('l4', 'urp', tcp_urgent_ptr)
+ #fields_desc = [ ShortEnumField("sport", 20, TCP_SERVICES),
+ # ShortEnumField("dport", 80, TCP_SERVICES),
+ # IntField("seq", 0),
+ # IntField("ack", 0),
+ # BitField("dataofs", None, 4),
+ # BitField("reserved", 0, 4),
+ # FlagsField("flags", 0x2, 8, "FSRPAUEC"),
+ # ShortField("window", 8192),
+ # XShortField("chksum", None),
+ # ShortField("urgptr", 0),
+ # TCPOptionsField("options", {}) ]
+ tcp_flags = ('F' if tcp_fin_flag else '' +
+ 'S' if tcp_syn_flag else '' +
+ 'R' if tcp_rst_flag else '' +
+ 'P' if tcp_psh_flag else '' +
+ 'A' if tcp_ack_flag else '' +
+ 'U' if tcp_urg_flag else '')
+
+ pkt /= pkt_bld.TCP(sport = tcp_src_port,
+ dport = tcp_dst_port,
+ seq = tcp_seq_num,
+ ack = tcp_ack_num,
+ dataofs = tcp_data_offset,
+ flags = tcp_flags,
+ window = tcp_window,
+ chksum = tcp_checksum,
+ urgptr = tcp_urgent_ptr,
+ )
else:
- raise NotImplementedError("l4_protocol '{0}' is not supported by TRex yet.".format(l3_protocol))
-
- pkt_bld.set_pkt_payload('Hello, World' + '!'*58)
-
- # debug
+ raise NotImplementedError("l4_protocol '%s' is not supported by TRex yet." % l3_protocol)
+ pkt /= 'payload'
+ pkt_plus_vm.set_packet(pkt)
+ #payload = frame_size - pkt
+ #payload = pkt_bld.payload_gen.gen_repeat_ptrn('Hello, World!')
+ #pkt_bld.set_pkt_payload(payload)
+
+ #print pkt_bld.vm
+ # debug (only base packet, without FE)
debug_filename = kwargs.get('save_to_pcap')
if type(debug_filename) is str:
- pkt_bld.dump_pkt_to_pcap(debug_filename)
- return pkt_bld
+ pkt_plus_vm.dump_pkt_to_pcap(debug_filename)
+ pkt_plus_vm.compile()
+ return pkt_plus_vm
diff --git a/scripts/automation/trex_control_plane/client_utils/packet_builder.py b/scripts/automation/trex_control_plane/client_utils/packet_builder.py
index e2a1b116..5ad6cd7a 100755
--- a/scripts/automation/trex_control_plane/client_utils/packet_builder.py
+++ b/scripts/automation/trex_control_plane/client_utils/packet_builder.py
@@ -32,7 +32,6 @@ class CTRexPktBuilder(object):
self._pkt_by_hdr = {}
self._pkt_top_layer = None
self._max_pkt_size = max_pkt_size
- self.payload_gen = CTRexPktBuilder.CTRexPayloadGen(self._packet, self._max_pkt_size)
self.vm = CTRexPktBuilder.CTRexVM()
self.metadata = ""
@@ -481,12 +480,15 @@ class CTRexPktBuilder(object):
def get_vm_data(self):
return self.vm.dump()
- def dump_pkt(self):
+ def dump_pkt(self, encode = True):
"""
Dumps the packet as a decimal array of bytes (each item x gets value between 0-255)
:parameters:
- None
+ encode : bool
+ Encode using base64. (disable for debug)
+
+ Default: **True**
:return:
+ packet representation as array of bytes
@@ -498,7 +500,10 @@ class CTRexPktBuilder(object):
if self._packet is None:
raise CTRexPktBuilder.EmptyPacketError()
- return {"binary": base64.b64encode(str(self._packet)),
+ if encode:
+ return {"binary": base64.b64encode(str(self._packet)),
+ "meta": self.metadata}
+ return {"binary": str(self._packet),
"meta": self.metadata}
@@ -629,6 +634,10 @@ class CTRexPktBuilder(object):
layer_name))
return
+ @property
+ def payload_gen(self):
+ return CTRexPktBuilder.CTRexPayloadGen(self._packet, self._max_pkt_size)
+
@staticmethod
def _decode_mac_addr(mac_addr):
"""
diff --git a/scripts/automation/trex_control_plane/client_utils/scapy_packet_builder.py b/scripts/automation/trex_control_plane/client_utils/scapy_packet_builder.py
index 5ee4a044..3385ec65 100644
--- a/scripts/automation/trex_control_plane/client_utils/scapy_packet_builder.py
+++ b/scripts/automation/trex_control_plane/client_utils/scapy_packet_builder.py
@@ -31,9 +31,9 @@ class CTRexPacketBuildException(Exception):
def ipv4_str_to_num (ipv4_buffer):
- assert(type(ipv4_buffer)==str);
- assert(len(ipv4_buffer)==4);
- res=0;
+ assert type(ipv4_buffer)==str, 'type of ipv4_buffer is not str'
+ assert len(ipv4_buffer)==4, 'size of ipv4_buffer is not 4'
+ res=0
shift=24
for i in ipv4_buffer:
res = res + (ord(i)<<shift);
@@ -70,7 +70,7 @@ class CTRexScriptsBase(object):
class CTRexScFieldRangeBase(CTRexScriptsBase):
- FILED_TYPES = ["inc","dec","rand"]
+ FILED_TYPES = ['inc', 'dec', 'rand']
def __init__(self, field_name,
field_type
@@ -79,7 +79,7 @@ class CTRexScFieldRangeBase(CTRexScriptsBase):
self.field_name =field_name
self.field_type =field_type
if not self.field_type in CTRexScFieldRangeBase.FILED_TYPES :
- raise CTRexPacketBuildException(-12,"field type should be in [inc,dec,rand] ");
+ raise CTRexPacketBuildException(-12, 'field type should be in %s' % FILED_TYPES);
class CTRexScFieldRangeValue(CTRexScFieldRangeBase):
@@ -88,30 +88,30 @@ class CTRexScFieldRangeValue(CTRexScFieldRangeBase):
"""
def __init__(self, field_name,
field_type,
- min_val,
- max_val
+ min_value,
+ max_value
):
super(CTRexScFieldRangeValue, self).__init__(field_name,field_type)
- self.min_val =min_val;
- self.max_val =max_val;
- if min_val > max_val:
- raise CTRexPacketBuildException(-12,"min is greater than max ");
- if min_val == max_val:
- raise CTRexPacketBuildException(-13,"min value is equal to max value, you can't use this type of range ");
+ self.min_value =min_value;
+ self.max_value =max_value;
+ if min_value > max_value:
+ raise CTRexPacketBuildException(-12, 'min is greater than max');
+ if min_value == max_value:
+ raise CTRexPacketBuildException(-13, 'min value is equal to max value, you can't use this type of range');
class CTRexScIpv4SimpleRange(CTRexScFieldRangeBase):
"""
range of ipv4 ip
"""
- def __init__(self, field_name,field_type,min_ip, max_ip):
+ def __init__(self, field_name, field_type, min_ip, max_ip):
super(CTRexScIpv4SimpleRange, self).__init__(field_name,field_type)
self.min_ip = min_ip
self.max_ip = max_ip
mmin=ipv4_str_to_num (is_valid_ipv4(min_ip))
mmax=ipv4_str_to_num (is_valid_ipv4(max_ip))
if mmin > mmax :
- raise CTRexPacketBuildException(-11,"CTRexScIpv4SimpleRange m_min ip is bigger than max ");
+ raise CTRexPacketBuildException(-11, 'CTRexScIpv4SimpleRange m_min ip is bigger than max');
class CTRexScIpv4TupleGen(CTRexScriptsBase):
@@ -120,14 +120,14 @@ class CTRexScIpv4TupleGen(CTRexScriptsBase):
"""
FLAGS_ULIMIT_FLOWS =1
- def __init__(self, min_ipv4, max_ipv4,num_flows=100000,min_port=1025,max_port=65535,flags=0):
+ def __init__(self, min_ipv4, max_ipv4, num_flows=100000, min_port=1025, max_port=65535, flags=0):
super(CTRexScIpv4TupleGen, self).__init__()
self.min_ip = min_ipv4
self.max_ip = max_ipv4
mmin=ipv4_str_to_num (is_valid_ipv4(min_ipv4))
mmax=ipv4_str_to_num (is_valid_ipv4(max_ipv4))
if mmin > mmax :
- raise CTRexPacketBuildException(-11,"CTRexScIpv4SimpleRange m_min ip is bigger than max ");
+ raise CTRexPacketBuildException(-11, 'CTRexScIpv4SimpleRange m_min ip is bigger than max');
self.num_flows=num_flows;
@@ -147,10 +147,10 @@ class CTRexScTrimPacketSize(CTRexScriptsBase):
self.max_pkt_size = max_pkt_size
if max_pkt_size != None and min_pkt_size !=None :
if min_pkt_size == max_pkt_size:
- raise CTRexPacketBuildException(-11,"CTRexScTrimPacketSize min_pkt_size is the same as max_pkt_size ");
+ raise CTRexPacketBuildException(-11, 'CTRexScTrimPacketSize min_pkt_size is the same as max_pkt_size ');
if min_pkt_size > max_pkt_size:
- raise CTRexPacketBuildException(-11,"CTRexScTrimPacketSize min_pkt_size is bigger than max_pkt_size ");
+ raise CTRexPacketBuildException(-11, 'CTRexScTrimPacketSize min_pkt_size is bigger than max_pkt_size ');
class CTRexScRaw(CTRexScriptsBase):
"""
@@ -176,59 +176,59 @@ class CTRexVmInsBase(object):
"""
instruction base
"""
- def __init__(self,ins_type):
+ def __init__(self, ins_type):
self.type = ins_type
- assert(type(ins_type)==str);
+ assert type(ins_type)==str, 'type of ins_type is not str'
class CTRexVmInsFixIpv4(CTRexVmInsBase):
- def __init__(self,offset):
+ def __init__(self, offset):
super(CTRexVmInsFixIpv4, self).__init__("fix_checksum_ipv4")
self.offset = offset
- assert(type(offset)==int);
+ assert type(offset)==int, 'type of offset is not int'
class CTRexVmInsFlowVar(CTRexVmInsBase):
#TBD add more validation tests
- OPERATIONS =["inc", "dec", "random"]
- VALID_SIZES =[1,2,4,8]
+ OPERATIONS =['inc', 'dec', 'random']
+ VALID_SIZES =[1, 2, 4, 8]
- def __init__(self,fv_name,size,op,init_val,min_val,max_val):
+ def __init__(self, fv_name, size, op, init_value, min_value, max_value):
super(CTRexVmInsFlowVar, self).__init__("flow_var")
- self.name =fv_name;
- assert(type(fv_name)==str);
- self.size =size
- self.op =op
- self.init_val=init_val
- assert(type(init_val)==int);
- self.min_val=min_val
- assert(type(min_val)==int);
- self.max_val=max_val
- assert(type(max_val)==int);
+ self.name = fv_name;
+ assert type(fv_name)==str, 'type of fv_name is not str'
+ self.size = size
+ self.op = op
+ self.init_value = init_value
+ assert type(init_value)==int, 'type of init_value is not int'
+ self.min_value=min_value
+ assert type(min_value)==int, 'type of min_value is not int'
+ self.max_value=max_value
+ assert type(max_value)==int, 'type of min_value is not int'
class CTRexVmInsWrFlowVar(CTRexVmInsBase):
- def __init__(self,fv_name,pkt_offset,add_val=0,is_big=True):
+ def __init__(self, fv_name, pkt_offset, add_value=0, is_big_endian=True):
super(CTRexVmInsWrFlowVar, self).__init__("write_flow_var")
- self.name =fv_name
- assert(type(fv_name)==str);
- self.pkt_offset =pkt_offset
- assert(type(pkt_offset)==int);
- self.add_val =add_val
- assert(type(add_val)==int);
- self.is_big =is_big;
- assert(type(is_big)==bool);
+ 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.add_value = add_value
+ assert type(add_value)==int, 'type of add_value 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")
self.fv_name =fv_name
- assert(type(fv_name)==str);
+ assert type(fv_name)==str, 'type of fv_name is not str'
class CTRexVmInsTupleGen(CTRexVmInsBase):
- def __init__(self,fv_name,ip_min,ip_max,port_min,port_max,limit_flows,flags=0):
+ def __init__(self, fv_name, ip_min, ip_max, port_min, port_max, limit_flows, flags=0):
super(CTRexVmInsTupleGen, self).__init__("tuple_flow_var")
self.name =fv_name
- assert(type(fv_name)==str);
+ assert type(fv_name)==str, 'type of fv_name is not str'
self.ip_min = ip_min;
self.ip_max = ip_max;
self.port_min = port_min;
@@ -281,7 +281,7 @@ class CTRexVmEngine(object):
class CTRexScapyPktUtl(object):
- def __init__(self,scapy_pkt):
+ def __init__(self, scapy_pkt):
self.pkt = scapy_pkt
def pkt_iter (self):
@@ -292,12 +292,12 @@ class CTRexScapyPktUtl(object):
if p ==None or isinstance(p,NoPayload):
break;
- def get_list_iter (self):
+ def get_list_iter(self):
l=list(self.pkt_iter())
return l
- def get_pkt_layers (self):
+ def get_pkt_layers(self):
"""
return string 'IP:UDP:TCP'
"""
@@ -305,7 +305,7 @@ class CTRexScapyPktUtl(object):
l1=map(lambda p: p.name,l );
return ":".join(l1);
- def _layer_offset(self,name,cnt=0):
+ def _layer_offset(self, name, cnt = 0):
"""
return offset of layer e.g 'IP',1 will return offfset of layer ip:1
"""
@@ -317,10 +317,10 @@ class CTRexScapyPktUtl(object):
else:
cnt=cnt -1
- raise CTRexPacketBuildException(-11,("no layer %s-%d" % (name,save_cnt)));
+ raise CTRexPacketBuildException(-11,("no layer %s-%d" % (name, save_cnt)));
- def layer_offset(self,name,cnt=0):
+ def layer_offset(self, name, cnt = 0):
"""
return offset of layer e.g 'IP',1 will return offfset of layer ip:1
"""
@@ -332,9 +332,9 @@ class CTRexScapyPktUtl(object):
else:
cnt=cnt -1
- raise CTRexPacketBuildException(-11,("no layer %s-%d" % (name,save_cnt)));
+ raise CTRexPacketBuildException(-11,("no layer %s-%d" % (name, save_cnt)));
- def get_field_offet(self,layer,layer_cnt,field_name):
+ def get_field_offet(self, layer, layer_cnt, field_name):
"""
return offset of layer e.g 'IP',1 will return offfset of layer ip:1
"""
@@ -348,9 +348,9 @@ class CTRexScapyPktUtl(object):
if f.name == field_name:
return (l_offset+f.offset,f.get_size_bytes ());
- raise CTRexPacketBuildException(-11,("no layer %s-%d." % (name,save_cnt,field_name)));
+ raise CTRexPacketBuildException(-11, "no layer %s-%d." % (name, save_cnt, field_name));
- def get_layer_offet_by_str(self,layer_des):
+ def get_layer_offet_by_str(self, layer_des):
"""
return layer offset by string
@@ -369,11 +369,11 @@ class CTRexScapyPktUtl(object):
layer=l1[0];
layer_cnt=int(l1[1]);
- return self.layer_offset(layer,layer_cnt)
+ return self.layer_offset(layer, layer_cnt)
- def get_field_offet_by_str(self,field_des):
+ def get_field_offet_by_str(self, field_des):
"""
return field_des (offset,size) layer:cnt.field
for example
@@ -388,7 +388,7 @@ class CTRexScapyPktUtl(object):
s=field_des.split(".");
if len(s)!=2:
- raise CTRexPacketBuildException(-11,("field desription should be layer:cnt.field e.g IP.src or IP:1.src "));
+ raise CTRexPacketBuildException(-11, ("field desription should be layer:cnt.field e.g IP.src or IP:1.src"));
layer_ex = s[0]
@@ -424,16 +424,16 @@ class CTRexVmDescBase(object):
def __init__(self):
pass;
- def get_obj (self):
+ def get_obj(self):
return self;
- def get_json (self):
+ def get_json(self):
return self.get_obj().__dict__
- def dump_bjson (self):
+ def dump_bjson(self):
print json.dumps(self.get_json(), sort_keys=True, indent=4)
- def dump_as_yaml (self):
+ def dump_as_yaml(self):
print yaml.dump(self.get_json(), default_flow_style=False)
@@ -469,30 +469,30 @@ def convert_val (val):
class CTRexVmDescFlowVar(CTRexVmDescBase):
- def __init__(self,name,init_val=0,min_val=0,max_val=255,size=4,op="inc"):
+ def __init__(self, name, init_value=0, min_value=0, max_value=255, size=4, op="inc"):
super(CTRexVmDescFlowVar, self).__init__()
self.name = name;
- assert(type(name)==str);
+ assert type(name)==str, 'type of name is not str'
self.size =size
valid_fv_size(size)
self.op =op
valid_fv_ops (op)
- self.init_val = convert_val (init_val)
- self.min_val = convert_val (min_val);
- self.max_val = convert_val (max_val)
+ self.init_value = convert_val (init_value)
+ self.min_value = convert_val (min_value);
+ self.max_value = convert_val (max_value)
- if self.min_val > self.max_val :
- raise CTRexPacketBuildException(-11,("max %d is lower than min %d ") % (self.max_val,self.min_val) );
+ 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_val,self.min_val,self.max_val);
+ return CTRexVmInsFlowVar(self.name,self.size,self.op,self.init_value,self.min_value,self.max_value);
def get_var_name(self):
return self.name
class CTRexVmDescFixIpv4(CTRexVmDescBase):
- def __init__(self,offset):
+ def __init__(self, offset):
super(CTRexVmDescFixIpv4, self).__init__()
self.offset = offset; # could be a name of offset
@@ -504,18 +504,18 @@ class CTRexVmDescFixIpv4(CTRexVmDescBase):
self.offset = parent._pkt_layer_offset(self.offset);
class CTRexVmDescWrFlowVar(CTRexVmDescBase):
- def __init__(self,fv_name,pkt_offset,add_val=0,is_big=True):
+ def __init__(self, fv_name, pkt_offset, add_value=0, is_big_endian=True):
super(CTRexVmDescWrFlowVar, self).__init__()
- self.name =fv_name
- assert(type(fv_name)==str);
- self.pkt_offset =pkt_offset
- self.add_val =add_val
- assert(type(add_val)==int);
- self.is_big =is_big;
- assert(type(is_big)==bool);
+ self.name = fv_name
+ assert type(fv_name)==str, 'type of fv_name is not str'
+ self.pkt_offset = pkt_offset
+ self.add_value = add_value
+ assert type(add_value)==int, 'type of add_value is not int'
+ self.is_big_endian =is_big_endian;
+ assert type(is_big_endian)==bool, 'type of is_big_endian is not bool'
- def get_obj (self):
- return CTRexVmInsWrFlowVar(self.name,self.pkt_offset,self.add_val,self.is_big)
+ def get_obj(self):
+ return CTRexVmInsWrFlowVar(self.name, self.pkt_offset, self.add_value, self.is_big_endian)
def compile(self,parent):
if type(self.pkt_offset)==str:
@@ -541,7 +541,7 @@ class CScapyTRexPktBuilder(object):
"""
super(CScapyTRexPktBuilder, self).__init__()
- self._pkt = None # scapy packet
+ self.pkt = None # scapy packet
self.vm_scripts = [] # list of high level instructions
self.vm_low_level = None
self.metadata=""
@@ -556,53 +556,62 @@ class CScapyTRexPktBuilder(object):
:return:
+ json object of instructions
+ :raises:
+ + :exc:`AssertionError`, in case VM is not compiled (is None).
"""
- return self.vm_low_level.get_json ()
+ assert self.vm_low_level is None, 'vm_low_level is None, please use compile()'
+
+ return self.vm_low_level.get_json()
- def dump_pkt(self):
+ def dump_pkt(self, encode = True):
"""
Dumps the packet as a decimal array of bytes (each item x gets value between 0-255)
:parameters:
- None
+ encode : bool
+ Encode using base64. (disable for debug)
+
+ Default: **True**
:return:
+ packet representation as array of bytes
:raises:
- + :exc:`CTRexPktBuilder.EmptyPacketError`, in case packet is empty.
+ + :exc:`AssertionError`, in case packet is empty.
"""
- assert(self.pkt);
+ assert self.pkt, 'empty packet'
- return {"binary": base64.b64encode(str(self.pkt)),
- "meta": self.metadata}
+ return {'binary': base64.b64encode(str(self.pkt)) if encode else str(self.pkt),
+ 'meta': self.metadata}
+ def dump_pkt_to_pcap(self, file_path):
+ wrpcap(file_path, self.pkt)
- def add_command (self,script):
+ def add_command (self, script):
self.vm_scripts.append(script.clone());
def dump_scripts (self):
- self.vm_low_level.dump_as_yaml ()
+ self.vm_low_level.dump_as_yaml()
- def set_packet (self,pkt):
+ def set_packet (self, pkt):
"""
Scapy packet Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/IP()/"A"*10
"""
- self.pkt =pkt;
+ self.pkt = pkt;
def compile (self):
self.vm_low_level=CTRexVmEngine()
- assert(self.pkt);
+ assert self.pkt, 'empty packet'
self.pkt.build();
for sc in self.vm_scripts:
if isinstance(sc, CTRexScRaw):
- self._compile_raw (sc)
+ self._compile_raw(sc)
#for obj in self.vm_scripts:
# # tuple gen script
@@ -622,7 +631,7 @@ class CScapyTRexPktBuilder(object):
print var_name
if var_name :
if vars.has_key(var_name):
- raise CTRexPacketBuildException(-11,("varible %s define twice ") % (var_name) );
+ raise CTRexPacketBuildException(-11, 'varible %s is defined twice' % var_name);
else:
vars[var_name]=1
desc.compile(self);
@@ -632,16 +641,16 @@ class CScapyTRexPktBuilder(object):
def _pkt_layer_offset (self,layer_name):
- assert(self.pkt != None);
+ assert self.pkt != None, 'empty packet'
p_utl=CTRexScapyPktUtl(self.pkt);
return p_utl.get_layer_offet_by_str(layer_name)
def _name_to_offset(self,field_name):
- assert(self.pkt != None);
+ assert self.pkt != None, 'empty packet'
p_utl=CTRexScapyPktUtl(self.pkt);
return p_utl.get_field_offet_by_str(field_name)
- def _add_tuple_gen(self,tuple_gen):
+ def _add_tuple_gen(self,tuple_gen):
pass;