|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Generates an Ethernet frame via scapy using pcap, copies pcap to DUT, replays pcap on interface, |
| 3 | +# records frame locally (or on exporter, adjust env.yaml accordingly), and compares both. |
| 4 | + |
| 5 | +import logging |
| 6 | +import os |
| 7 | +from tempfile import NamedTemporaryFile, TemporaryDirectory |
| 8 | + |
| 9 | +from labgrid import Environment |
| 10 | +from labgrid.logging import basicConfig, StepLogger |
| 11 | +from scapy.all import Ether, Raw, rdpcap, wrpcap, conf |
| 12 | + |
| 13 | +def generate_frame(): |
| 14 | + frame = [Ether(dst="11:22:33:44:55:66", src="66:55:44:33:22:11", type=0x9000)] |
| 15 | + padding = "\x00" * (conf.min_pkt_size - len(frame)) |
| 16 | + frame = frame[0] / Raw(load=padding) |
| 17 | + return frame |
| 18 | + |
| 19 | + |
| 20 | +basicConfig(level=logging.INFO) |
| 21 | +StepLogger.start() |
| 22 | +env = Environment("env.yaml") |
| 23 | +target = env.get_target() |
| 24 | + |
| 25 | +netdrv = target.get_driver("RawNetworkInterfaceDriver") |
| 26 | +ssh = target.get_driver("SSHDriver") |
| 27 | + |
| 28 | +# get DUT interface |
| 29 | +exporter_iface = netdrv.iface.ifname |
| 30 | +dut_iface = env.config.get_target_option(target.name, "local_iface_to_dut_iface")[exporter_iface] |
| 31 | + |
| 32 | +# generate test frame |
| 33 | +generated_frame = generate_frame() |
| 34 | + |
| 35 | +# write pcap, copy to DUT |
| 36 | +remote_pcap = "/tmp/pcap" |
| 37 | +with NamedTemporaryFile() as pcap: |
| 38 | + wrpcap(pcap.name, generated_frame) |
| 39 | + ssh.put(pcap.name, remote_pcap) |
| 40 | + |
| 41 | +# copy recorded pcap from DUT, compare with generated frame |
| 42 | +with TemporaryDirectory() as tempdir: |
| 43 | + # start record on exporter |
| 44 | + tempf = os.path.join(tempdir, "record.pcap") |
| 45 | + with netdrv.record(tempf, count=1) as record: |
| 46 | + # replay pcap on DUT |
| 47 | + ssh.run_check(f"ip link set {dut_iface} up") |
| 48 | + ssh.run_check(f"tcpreplay -i {dut_iface} {remote_pcap}") |
| 49 | + |
| 50 | + remote_frame = rdpcap(tempf) |
| 51 | + assert remote_frame[0] == generated_frame[0] |
| 52 | + |
| 53 | +print("statistics", netdrv.get_statistics()) |
| 54 | +print("address", netdrv.get_address()) |
0 commit comments