def do_network_off(console: boot_cheribsd.CheriBSDInstance, args):
    ifc = get_network_iface(args)
    console.run('/sbin/ifconfig {} down'.format(ifc))
    #    console.run('killall dhclient')
    # Note: If we devctl disable le0, we can't enable it anymore
    if not args.use_qemu_instead_of_fpga:
        console.sendline('/usr/sbin/devctl disable {}'.format(ifc))
        console.expect(['{}: detached'.format(ifc),
                        "Failed to disable {}: Device not configured".format(ifc)
                        ])
def get_board_ip_address(console: boot_cheribsd.CheriBSDInstance, args):
    assert not args.use_qemu_instead_of_fpga
    ifc = get_network_iface(args)
    console.sendline('ifconfig {}'.format(ifc))
    idx = console.expect([
        re.compile("inet (.+) netmask "),
        # error cases:
        pexpect.TIMEOUT, "interface " + ifc + " does not exist"], timeout=10)
    if idx == 0:
        print(console.match)
        return console.match.group(1)
    console.expect_prompt()
def do_network_on(console: boot_cheribsd.CheriBSDInstance, args, timeout=300):
    ifc = get_network_iface(args)
    # Note: If we devctl disable le0, we can't enable it anymore
    if not args.use_qemu_instead_of_fpga:
        console.run('/usr/sbin/devctl enable {}'.format(ifc),
                    expected_output='{}: bpf attached'.format(ifc))
    console.run('/sbin/ifconfig {} up'.format(ifc))
    if ifc != "le0":
        # apparently the le0 driver doesn't print this message
        console.expect_exact(['{}: link state changed to UP'.format(ifc)])
    # Send a newline to ensure a prompt:
    console.sendline()
    console.expect_prompt()
    # No longer needed? console.run('/sbin/ifconfig {} polling'.format(ifc))
    console.sendline('/sbin/dhclient {}'.format(ifc))
    console.expect(["bound to .* -- renewal in .*\\."], timeout=timeout)
    console.expect_prompt()
def do_runbench(console: boot_cheribsd.CheriBSDInstance, tgtdir, script, scriptargs,
                failstr="FAILED RUNNING BENCHMARKS", timeout=None, pre_cmd=None, *, args):
    if timeout is None:
        timeout = args.timeout

    badcmd = "/this/command/does/not/exist"
    runbenchcmd = './{} {} || {}'.format(script, scriptargs, badcmd)
    console.sendline()
    console.expect_prompt()
    console.run('cd {} && ls -la'.format(tgtdir))
    if args.lazy_binding:
        console.run('unset LD_CHERI_BIND_NOW')
        console.run('unset LD_BIND_NOW')
    else:
        # Ensure that we don't use lazy binding for CheriABI since MIPS doesn't support it
        # This can skew the results since we have faster startup on CHERI but slower runtime
        # due to trampolines
        console.run('export LD_CHERI_BIND_NOW=1')
        console.run('export LD_BIND_NOW=1')

    if pre_cmd:
        console.run(pre_cmd)
    # Log the current environment:
    console.run("env")
    console.sendline(runbenchcmd)
    panicstr = "KDB: enter: "

    expects = ["DONE RUNNING BENCHMARKS", ": Command not found.", badcmd + ": not found", failstr, panicstr]
    idx = console.expect(expects, timeout=timeout)
    if idx != 0:
        print("Failed to run benchmark")
    if expects[idx] == panicstr:
        print("Panic!  Extracting backtrace...")
        console.sendline("bt")