summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/client_utils/packet_builder.py
diff options
context:
space:
mode:
authorDan Klein <danklei@cisco.com>2015-08-27 19:18:16 +0300
committerDan Klein <danklei@cisco.com>2015-08-27 19:18:16 +0300
commit68df86e2005dc4693b1270a3e663e2450f81fa93 (patch)
tree018d35202b5860905c878131c03bc2a0adba762b /scripts/automation/trex_control_plane/client_utils/packet_builder.py
parentc2154c0d362ced8f8b5181799c369e1497c958e1 (diff)
progress in packet builder module
Diffstat (limited to 'scripts/automation/trex_control_plane/client_utils/packet_builder.py')
-rw-r--r--scripts/automation/trex_control_plane/client_utils/packet_builder.py150
1 files changed, 148 insertions, 2 deletions
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 c33444a7..172dcda2 100644
--- a/scripts/automation/trex_control_plane/client_utils/packet_builder.py
+++ b/scripts/automation/trex_control_plane/client_utils/packet_builder.py
@@ -3,9 +3,155 @@
import outer_packages
import dpkt
+import socket
+import binascii
+
class CTRexPktBuilder(object):
"""docstring for CTRexPktBuilder"""
- def __init__(self, arg):
+ def __init__(self):
super(CTRexPktBuilder, self).__init__()
- self.arg = arg \ No newline at end of file
+ self.packet = None
+ self.vm = CTRexPktBuilder.CTRexVM(self.packet)
+
+ def add_l2_header(self):
+ pass
+
+ def add_l3_header(self):
+ pass
+
+ def add_pkt_payload(self):
+ pass
+
+ # VM access methods
+ def set_vm_ip_range(self, ip_start, ip_end, ip_type="ipv4"):
+ pass
+
+ def set_vm_range_type(self, ip_type):
+ pass
+
+ def set_vm_core_mask(self, ip_type):
+ pass
+
+ def get_vm_data(self):
+ pass
+
+ def dump_pkt(self):
+ pkt_in_hex = binascii.hexlify(str(self.packet))
+ return [pkt_in_hex[i:i+2] for i in range(0, len(pkt_in_hex), 2)]
+
+ # ----- useful shortcut methods ----- #
+ def gen_dns_packet(self):
+ pass
+
+ # ----- internal methods ----- #
+ @staticmethod
+ def _is_valid_ip_addr(ip_addr, ip_type):
+ if ip_type == "ipv4":
+ try:
+ socket.inet_pton(socket.AF_INET, ip_addr)
+ except AttributeError: # no inet_pton here, sorry
+ try:
+ socket.inet_aton(ip_addr)
+ except socket.error:
+ return False
+ return ip_addr.count('.') == 3
+ except socket.error: # not a valid address
+ return False
+ return True
+ elif ip_type == "ipv6":
+ try:
+ socket.inet_pton(socket.AF_INET6, ip_addr)
+ except socket.error: # not a valid address
+ return False
+ return True
+ else:
+ raise CTRexPktBuilder.IPAddressError()
+
+ # ------ private classes ------
+ class CTRexVM(object):
+ """docstring for CTRexVM"""
+ def __init__(self, packet):
+ super(CTRexPktBuilder.CTRexVM, self).__init__()
+ self.packet = packet
+ self.vm_variables = {}
+
+ def add_vm_variable(self, name):
+ if name not in self.vm_variables.keys():
+ self.vm_variables[name] = self.CTRexVMVariable(name)
+
+ def fix_checksum_ipv4(self):
+ pass
+
+ def flow_man_simple(self):
+ pass
+
+ def write_to_pkt(self):
+ pass
+
+ def dump(self):
+ return [var.dump()
+ for var in self.vm_variables
+ if var.is_validty()]
+
+ class CTRexVMVariable(object):
+ VALID_SIZE = [1, 2, 4, 8]
+ VALID_TYPE = ["inc", "dec", "random"]
+ VALID_CORE_MASK = ["split", "none"]
+
+ def __init__(self, name):
+ super(CTRexPktBuilder.CTRexVM.CTRexVMVariable, self).__init__()
+ self.name = name
+ self.size = 4
+ self.big_endian = True
+ self.type = "inc"
+ self.core_mask = "none"
+ self.init_addr = "10.0.0.1"
+ self.min_addr = str(self.init_addr)
+ self.max_addr = str(self.init_addr)
+
+ def is_valid(self):
+ if self.size not in self.VALID_SIZE:
+ return False
+ if self.type not in self.VALID_TYPE:
+ return False
+ if self.core_mask not in self.VALID_CORE_MASK:
+ return False
+ return True
+
+ def dump(self):
+ return {"name" : self.name,
+ "Size" : self.size,
+ "big_endian" : self.big_endian,
+ "type" : self.type,
+ "core_mask" : self.core_mask,
+ "init_addr" : self.init_addr,
+ "min_addr" : self.min_addr,
+ "max_addr" : self.max_addr}
+
+ class CPacketBuildException(Exception):
+ """
+ This is the general Packet error exception class.
+ """
+ def __init__(self, code, message):
+ self.code = code
+ self.message = message
+
+ def __str__(self):
+ return self.__repr__()
+
+ def __repr__(self):
+ return u"[errcode:%r] %r" % (self.code, self.message)
+
+ class IPAddressError(CPacketBuildException):
+ """
+ This exception is used to indicate an error on the IP addressing part of the packet.
+ """
+ def __init__(self, message=''):
+ self._default_message = 'Illegal type of IP addressing has been provided.'
+ self.message = message or self._default_message
+ super(CTRexPktBuilder.IPAddressError, self).__init__(-11, self.message)
+
+
+if __name__ == "__main__":
+ pass