def test_ecn_marking_at_ecress(api, duthost, ecn_marking_at_ecress, start_delay, pause_line_rate, traffic_line_rate, traffic_duration, port_bandwidth, frame_size, ecn_thresholds): duthost.shell('sudo pfcwd stop') duthost.shell('sudo sudo ecnconfig -p AZURE_LOSSLESS -gmax %s' % (ecn_thresholds)) duthost.shell('sudo sudo ecnconfig -p AZURE_LOSSLESS -gmin %s' % (ecn_thresholds)) for base_config in ecn_marking_at_ecress: rx_port = base_config.ports[1] rx_port.capture = Capture(choice=[], enable=True) # create the configuration api.set_config(base_config) # start capture api.set_port_capture(PortCapture(port_names=[rx_port.name])) # start all flows api.set_flow_transmit(FlowTransmit(state='start')) exp_dur = start_delay + traffic_duration logger.info("Traffic is running for %s seconds" % (traffic_duration)) time.sleep(exp_dur) # stop all flows api.set_flow_transmit(FlowTransmit(state='stop')) pcap_bytes = api.get_capture_results( CaptureRequest(port_name=rx_port.name)) # Get statistics test_stat = api.get_flow_results(FlowRequest()) for rows in test_stat['rows']: tx_frame_index = test_stat['columns'].index('frames_tx') rx_frame_index = test_stat['columns'].index('frames_rx') caption_index = test_stat['columns'].index('name') if ((rows[caption_index] == 'Test Data') or (rows[caption_index] == 'Background Data')): tx_frames = float(rows[tx_frame_index]) rx_frames = float(rows[rx_frame_index]) if ((tx_frames != rx_frames) or (rx_frames == 0)): pytest_assert( False, "Not all %s reached Rx End" % (rows[caption_index])) # write the pcap bytes to a local file with open('%s.pcap' % rx_port.name, 'wb') as fid: fid.write(pcap_bytes) from scapy.all import PcapReader reader = PcapReader('%s.pcap' % rx_port.name) for item in reader: logger.info(tem.time) logger.info(item.show())
def __run_traffic(api, config, all_flow_names, exp_dur_sec, capture_port_name, pcap_file_name): """ Run traffic and capture packets Args: api (obj): IXIA session config (obj): experiment config all_flow_names (list): names of all the flows capture_port_name (str): name of the port to capture packets pcap_file_name (str): name of the pcap file to store captured packets Returns: N/A """ api.set_state(State(ConfigState(config=config, state='set'))) api.set_state(State(PortCaptureState(port_names=[capture_port_name], state='start'))) api.set_state(State(FlowTransmitState(state='start'))) time.sleep(exp_dur_sec) attempts = 0 max_attempts = 20 while attempts < max_attempts: rows = api.get_flow_results(FlowRequest(flow_names=all_flow_names)) """ If all the flows have stopped """ transmit_states = [row['transmit'] for row in rows] if len(rows) == len(all_flow_names) and\ list(set(transmit_states)) == ['stopped']: time.sleep(IXIA_POLL_DELAY_SEC) break else: time.sleep(1) attempts += 1 """ Dump captured packets """ pcap_bytes = api.get_capture_results(CaptureRequest(port_name=capture_port_name)) with open(pcap_file_name, 'wb') as fid: fid.write(pcap_bytes) """ Stop all the flows """ api.set_state(State(FlowTransmitState(state='stop')))
def test_capture_pcap(serializer, api, tx_port, rx_port): """Demonstrates how to start capture and get capture results """ # configure capture filter = MacAddressFilter(mac='source', filter='000000000000') rx_port.capture = Capture(choice=[BasicFilter(filter)], enable=True) # configure flow port_tx_rx = PortTxRx(tx_port_name=tx_port.name, rx_port_names=[rx_port.name]) flow = Flow(name='capture', tx_rx=TxRx(port_tx_rx), packet=[], size=Size(128), rate=Rate(unit='pps', value=100), duration=Duration(FixedPackets(packets=100))) config = Config(ports=[tx_port, rx_port], flows=[flow]) api.set_state(State(ConfigState(config=config, state='set'))) # start capture api.set_state( State(PortCaptureState(port_names=[rx_port.name], state='start'))) # start transmit api.set_state(State(FlowTransmitState(state='start'))) time.sleep(5) # stop the capture and receive the capture as a stream of bytes pcap_bytes = api.get_capture_results( CaptureRequest(port_name=rx_port.name)) # write the pcap bytes to a local file with open('%s.pcap' % rx_port.name, 'wb') as fid: fid.write(pcap_bytes) # do stuff using scapy and the pcap file from scapy.all import PcapReader reader = PcapReader('%s.pcap' % rx_port.name) for item in reader: print(item.time) item.show()