From 68df86e2005dc4693b1270a3e663e2450f81fa93 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 27 Aug 2015 19:18:16 +0300 Subject: progress in packet builder module --- .../client/trex_stateless_client.py | 7 - .../client_utils/outer_packages.py | 2 +- .../client_utils/packet_builder.py | 150 ++++++++++++++++++++- .../trex_control_plane/common/trex_exceptions.py | 14 +- .../examples/interactive_stateless.py | 6 +- .../trex_control_plane/server/trex_server.py | 2 +- 6 files changed, 160 insertions(+), 21 deletions(-) (limited to 'scripts/automation') diff --git a/scripts/automation/trex_control_plane/client/trex_stateless_client.py b/scripts/automation/trex_control_plane/client/trex_stateless_client.py index 670eda1d..5513f420 100644 --- a/scripts/automation/trex_control_plane/client/trex_stateless_client.py +++ b/scripts/automation/trex_control_plane/client/trex_stateless_client.py @@ -47,12 +47,5 @@ class CTRexStatelessClient(object): - -class CTRexVM(object): - """docstring for CTRexVM""" - def __init__(self, arg): - super(CTRexVM, self).__init__() - self.arg = arg - if __name__ == "__main__": pass diff --git a/scripts/automation/trex_control_plane/client_utils/outer_packages.py b/scripts/automation/trex_control_plane/client_utils/outer_packages.py index 3ba73ec1..a6c9a2eb 100644 --- a/scripts/automation/trex_control_plane/client_utils/outer_packages.py +++ b/scripts/automation/trex_control_plane/client_utils/outer_packages.py @@ -9,7 +9,7 @@ ROOT_PATH = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir)) PATH_TO_PYTHON_LIB = os.path.abspath(os.path.join(ROOT_PATH, os.pardir, os.pardir, 'external_libs')) CLIENT_UTILS_MODULES = ['zmq', - 'dpkt-1.8.6.2' + 'dpkt-1.8.6' ] 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 diff --git a/scripts/automation/trex_control_plane/common/trex_exceptions.py b/scripts/automation/trex_control_plane/common/trex_exceptions.py index 1353fd00..a2a64e19 100755 --- a/scripts/automation/trex_control_plane/common/trex_exceptions.py +++ b/scripts/automation/trex_control_plane/common/trex_exceptions.py @@ -17,13 +17,13 @@ class RPCError(Exception): self.data = remote_data self.args = (code, self.msg, remote_data) - def __str__(self): - return self.__repr__() - def __repr__(self): - if self.args[2] is not None: - return u"[errcode:%r] %r. Extended data: %r" % (self.args[0], self.args[1], self.args[2]) - else: - return u"[errcode:%r] %r" % (self.args[0], self.args[1]) + def __str__(self): + return self.__repr__() + def __repr__(self): + if self.args[2] is not None: + return u"[errcode:%r] %r. Extended data: %r" % (self.args[0], self.args[1], self.args[2]) + else: + return u"[errcode:%r] %r" % (self.args[0], self.args[1]) class TRexException(RPCError): """ diff --git a/scripts/automation/trex_control_plane/examples/interactive_stateless.py b/scripts/automation/trex_control_plane/examples/interactive_stateless.py index 016888d2..7c25b4ef 100644 --- a/scripts/automation/trex_control_plane/examples/interactive_stateless.py +++ b/scripts/automation/trex_control_plane/examples/interactive_stateless.py @@ -93,7 +93,7 @@ class InteractiveStatelessTRex(cmd.Cmd): print termstyle.magenta(inst) if __name__ == "__main__": - parser = ArgumentParser(description=termstyle.cyan('Run T-Rex client API demos and scenarios.'), + parser = ArgumentParser(description=termstyle.cyan('Run TRex client stateless API demos and scenarios.'), usage="client_interactive_example [options]") parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0 \t (C) Cisco Systems Inc.\n') @@ -101,8 +101,8 @@ if __name__ == "__main__": parser.add_argument("-t", "--trex-host", required = True, dest="trex_host", action="store", help="Specify the hostname or ip to connect with T-Rex server.", metavar="HOST" ) - parser.add_argument("-p", "--trex-port", type=int, default = 8090, metavar="PORT", dest="trex_port", - help="Select port on which the T-Rex server listens. Default port is 8090.", action="store") + parser.add_argument("-p", "--trex-port", type=int, default = 5050, metavar="PORT", dest="trex_port", + help="Select port on which the T-Rex server listens. Default port is 5050.", action="store") # parser.add_argument("-m", "--maxhist", type=int, default = 100, metavar="SIZE", dest="hist_size", # help="Specify maximum history size saved at client side. Default size is 100.", action="store") parser.add_argument("--virtual", dest="virtual", diff --git a/scripts/automation/trex_control_plane/server/trex_server.py b/scripts/automation/trex_control_plane/server/trex_server.py index 992a1d5f..35b2669a 100755 --- a/scripts/automation/trex_control_plane/server/trex_server.py +++ b/scripts/automation/trex_control_plane/server/trex_server.py @@ -53,7 +53,7 @@ class CTRexServer(object): the port number on which trex's zmq module will interact with daemon server default value: 4500 - Instatiate a T-Rex client object, and connecting it to listening daemon-server + Instantiate a T-Rex client object, and connecting it to listening daemon-server """ self.TREX_PATH = os.path.abspath(os.path.dirname(trex_path+'/')) self.trex_files_path = os.path.abspath(os.path.dirname(trex_files_path+'/')) -- cgit 1.2.3-korg