def _create_stream(self, src_ip, dst_ip): base_frame = \ Pkt.Ether() / Pkt.IP(src=src_ip, dst=dst_ip) / Pkt.UDP(dport=12, sport=1025) frame_size = self.params["traffic_profile"]["frame_size"] pad_size = max(0, frame_size - len(base_frame)) frame = base_frame / ("x" * max(0, pad_size)) frame_rate = self.params["traffic_profile"]["frame_rate"] return STLStream(packet=STLPktBuilder(pkt=frame), mode=STLTXCont(pps=frame_rate))
def generate_vm(self, packet_definition): """ generate trex vm with flows setup """ self.ether_packet = Pkt.Ether() self.ip_packet = Pkt.IP() self.ip6_packet = None self.udp_packet = Pkt.UDP() self.udp[DST_PORT] = 'UDP.dport' self.udp[SRC_PORT] = 'UDP.sport' self.qinq = False self.vm_flow_vars = [] outer_l2 = packet_definition.get('outer_l2', None) outer_l3v4 = packet_definition.get('outer_l3v4', None) outer_l3v6 = packet_definition.get('outer_l3v6', None) outer_l4 = packet_definition.get('outer_l4', None) if outer_l2: self._set_outer_l2_fields(outer_l2) if outer_l3v4: self._set_outer_l3v4_fields(outer_l3v4) if outer_l3v6: self._set_outer_l3v6_fields(outer_l3v6) if outer_l4: self._set_outer_l4_fields(outer_l4) self.trex_vm = STLScVmRaw(self.vm_flow_vars)
def _create_vm(self, packet_definition): """Create the STL Raw instructions""" self.ether_packet = Pkt.Ether() self.ip_packet = Pkt.IP() self.ip6_packet = None self.udp_packet = Pkt.UDP() self.udp[DST_PORT] = 'UDP.dport' self.udp[SRC_PORT] = 'UDP.sport' self.qinq = False self.vm_flow_vars = [] outer_l2 = packet_definition.get('outer_l2') outer_l3v4 = packet_definition.get('outer_l3v4') outer_l3v6 = packet_definition.get('outer_l3v6') outer_l4 = packet_definition.get('outer_l4') if outer_l2: self._set_outer_l2_fields(outer_l2) if outer_l3v4: self._set_outer_l3v4_fields(outer_l3v4) if outer_l3v6: self._set_outer_l3v6_fields(outer_l3v6) if outer_l4: self._set_outer_l4_fields(outer_l4) self.trex_vm = trex_stl_packet_builder_scapy.STLScVmRaw( self.vm_flow_vars)
def _build_trex_packet(self, packet_definition, adjust_size=True, required_size=64): """Builds trex packet based on provided packet definition. Args: packet_definition(tuple(dict)): Packet representation (tuple of dictionaries of dictionaries) adjust_size(bool): If set to True packet size will be increased to 60 bytes (CRC not included) required_size(int): Size (in bytes) of the result packet Returns: trex.Packet: trex.packet or list of pypacker packets (if fragsize defined) Examples:: packet_definition = ({"Ether": {"dst": "00:80:C2:00:00:00", "src": "00:00:00:00:00:02"}}, {"Dot1Q": {"vlan": 4}}, {"IP": {}}) packet=env.tg[1]._build_trex_packet(packet_definition) """ import trex_stl_lib.api as TApi def _value_repr(value): """Check if value contains layers. """ if isinstance(value, (list, tuple)): return type(value)(list(map(_value_repr, value))) elif isinstance(value, dict): return _trex_layer(value) else: return value def _trex_layer(layer_dict): """Return trex Packet object built according to definition. """ layer_name = list(layer_dict.keys())[0] sl = getattr(TApi, layer_name)() field_list = [_fl.name for _fl in sl.fields_desc] fields = layer_dict[layer_name] list([ setattr(sl, f, _value_repr(fields[f])) for f in [f for f in field_list if f in fields] ]) return sl # Converting packet_definition to trex.Packet. packet = reduce(lambda a, b: a / b, list(map(_trex_layer, packet_definition))) # Adjust packet size with padding. if adjust_size: packet_length = len(packet) if packet_length < required_size: packet.add_payload(b"\x00" * (required_size - packet_length)) elif packet_length > required_size and required_size < 60: self.class_logger.warning( "required_size is less than actual size. Packet will be cut off" ) packet_string = bytes(packet) packet_string = packet_string[0:required_size] packet = TApi.Ether(packet_string) return packet
def _set_ether_fields(self, **kwargs): """ set ethernet protocol fields """ if not self.ether_packet: self.ether_packet = Pkt.Ether() for key, value in six.iteritems(kwargs): setattr(self.ether_packet, key, value)
def run(self): sys.path.insert(0, os.path.join(self._trex_path, self.trex_stl_path)) import trex_stl_lib.api as trex_api client = trex_api.STLClient(server=self._server_hostname) client.connect() try: client.acquire(ports=self._ports, force=True) except: res_data = {} res_data["msg"] = "Failed to acquire ports" return self.set_fail(res_data) try: client.reset(ports=self._ports) except: client.release(ports=self._ports) res_data = {} res_data["msg"] = "Failed to reset ports" return self.set_fail(res_data) for i, port in enumerate(self._ports): L2 = trex_api.Ether(src=self._src_macs[i], dst=self._dst_macs[i]) L3 = trex_api.IP(src="192.168.1.%d" % (i * 2 + 1), dst="192.168.1.%d" % (i * 2 + 2)) L4 = trex_api.UDP() base_pkt = L2 / L3 / L4 pad = max(0, self._pkt_size - len(base_pkt)) * 'x' packet = base_pkt / pad trex_packet = trex_api.STLPktBuilder(pkt=packet) trex_stream = trex_api.STLStream( packet=trex_packet, mode=trex_api.STLTXCont(percentage=100)) client.add_streams(trex_stream, ports=[port]) client.set_port_attr(ports=self._ports, promiscuous=True) results = [] for i in range(self._runs): client.clear_stats(ports=self._ports) client.start(ports=self._ports) sleep(self._duration) client.stop(ports=self._ports) results.append(client.get_stats(ports=self._ports)) #wait before starting next run sleep(self._run_wait) client.release(ports=self._ports) results = self._postprocess_results(results) res_data = {"results": results} res_data["msg"] = "" return self.set_pass(res_data)