Esempio n. 1
0
def tun_tap_example_test(dut):
    """Example of a test using TUN/TAP.

    Creates an interface (192.168.255.1) and any packets received are sent
    into the DUT.  The response output by the DUT is then sent back out on
    this interface.

    Note to create the TUN interface this test must be run as root or the user
    must have CAP_NET_ADMIN capability.
    """

    cocotb.fork(Clock(dut.clk, 5000).start())

    stream_in = AvalonSTDriver(dut, "stream_in", dut.clk)
    stream_out = AvalonSTMonitor(dut, "stream_out", dut.clk)

    # Enable verbose logging so we can see what's going on
    stream_in.log.setLevel(logging.DEBUG)
    stream_out.log.setLevel(logging.DEBUG)

    # Reset the DUT
    dut._log.debug("Resetting DUT")
    dut.reset_n <= 0
    stream_in.bus.valid <= 0
    yield Timer(10000)
    yield RisingEdge(dut.clk)
    dut.reset_n <= 1
    dut.stream_out_ready <= 1
    dut._log.debug("Out of reset")

    # Create our interface (destroyed at the end of the test)
    tun = create_tun()
    fd = tun.fileno()

    # Kick off a ping...
    subprocess.check_call('ping -c 5 192.168.255.2 &', shell=True)

    # Respond to 5 pings, then quit
    pingcounter = 0
    while True:
        cocotb.log.info("Waiting for packets on tun interface")
        packet = os.read(fd, 2048)
        cocotb.log.info("Received a packet!")

        if packet[9] == '\x01' and packet[20] == '\x08':
            cocotb.log.debug("Packet is an ICMP echo request")
            pingcounter += 1
        else:
            cocotb.log.info(
                "Packet is no ICMP echo request, throwing away packet")
            continue

        stream_in.append(packet)
        result = yield stream_out.wait_for_recv()

        cocotb.log.info("Rtl replied!")
        os.write(fd, str(result))

        if pingcounter == 5:
            break
Esempio n. 2
0
def tun_tap_example_test(dut):
    """Example of a test using TUN/TAP.

    Creates an interface (192.168.255.1) and any packets received are sent
    into the DUT.  The response output by the DUT is then sent back out on
    this interface.

    Note to create the TUN interface this test must be run as root or the user
    must have CAP_NET_ADMIN capability.
    """

    cocotb.fork(Clock(dut.clk, 5000).start())

    stream_in = AvalonSTDriver(dut, "stream_in", dut.clk)
    stream_out = AvalonSTMonitor(dut, "stream_out", dut.clk)

    # Enable verbose logging so we can see what's going on
    stream_in.log.setLevel(logging.DEBUG)
    stream_out.log.setLevel(logging.DEBUG)

    # Reset the DUT
    dut._log.debug("Resetting DUT")
    dut.reset_n <= 0
    stream_in.bus.valid <= 0
    yield Timer(10000)
    yield RisingEdge(dut.clk)
    dut.reset_n <= 1
    dut.stream_out_ready <= 1
    dut._log.debug("Out of reset")

    # Create our interface (destroyed at the end of the test)
    tun = create_tun()
    fd = tun.fileno()

    # Kick off a ping...
    subprocess.check_call('ping -c 5 192.168.255.2 &', shell=True)

    # Respond to 5 pings, then quit
    pingcounter = 0
    while True:
        cocotb.log.info("Waiting for packets on tun interface")
        packet = os.read(fd, 2048)
        cocotb.log.info("Received a packet!")

        if packet[9] == '\x01' and packet[20] == '\x08':
            cocotb.log.debug("Packet is an ICMP echo request")
            pingcounter += 1
        else:
            cocotb.log.info("Packet is no ICMP echo request, throwing away packet")
            continue

        stream_in.append(packet)
        result = yield stream_out.wait_for_recv()

        cocotb.log.info("Rtl replied!")
        os.write(fd, str(result))

        if pingcounter == 5:
            break