<commit_before>#!/bin/bash

SKIPPED_CODE=43

if [ ! -f ~/bench_conf.sh ]; then
    cat <<EOF
~/bench_conf.sh does not exist, skipping test.

For this test to run you need to configure the following variables:
SNABB_LOG0=<path_to_logfile>
NFV_PCI0=<pciaddr>
NFV_SOCKET0=vhost_A.sock
NFV_SOCKET1=vhost_B.sock
GUEST_IP0=fe80::5054:ff:fe00:0
GUEST_MAC0=52:54:00:00:00:00
IMAGE0=<ubuntu-image-a>
GUEST_IP1=fe80::5054:ff:fe00:1
GUEST_MAC1=52:54:00:00:00:01
IMAGE1=<ubuntu-image-b>
BOOTARGS0="earlyprintk root=/dev/vda rw console=ttyS0 ip=$GUEST_IP0"
BOOTARGS1="earlyprintk root=/dev/vda rw console=ttyS0 ip=$GUEST_IP1"
EOF
    exit $SKIPPED_CODE
fi

. /etc/bench_conf.sh
. ~/bench_conf.sh

export BENCH_ENV_PID

TESTCONFPATH="/tmp/snabb_nfv_selftest_ports.$$"
FUZZCONFPATH="/tmp/snabb_nfv_selftest_fuzz$$.ports"

# Usage: run_telnet <port> <command> [<sleep>]
# Runs <command> on VM listening on telnet <port>. Waits <sleep> seconds
# for before closing connection. The default of <sleep> is 2.
function run_telnet {
    (echo "$2"; sleep ${3:-2}) \
        | telnet localhost $1 2>&1
}

# Usage: load_config <path>
# Copies <path> to TESTCONFPATH and sleeps for a bit.
function load_config {
    echo "USING $1"
    cp "$1" "$TESTCONFPATH"
    sleep 2
}

function start_bench_env {
    scripts/bench_env/host-nic-snabbnfv-guests.sh $TESTCONFPATH &
    BENCH_ENV_PID=$!

    # Give bench_env time to print its stuff.
    sleep 0.25

    # Wait until VMs are ready.
    wait_vm_up $TELNET_PORT0
    wait_vm_up $TELNET_PORT1

    # Manually set ip addresses.
    run_telnet $TELNET_PORT0 "ifconfig eth0 up" >/dev/null
    run_telnet $TELNET_PORT1 "ifconfig eth0 up" >/dev/null
    run_telnet $TELNET_PORT0 "ip -6 addr add $GUEST_IP0 dev eth0" >/dev/null
    run_telnet $TELNET_PORT1 "ip -6 addr add $GUEST_IP1 dev eth0" >/dev/null

}

function stop_bench_env {
    kill $BENCH_ENV_PID

    # Give VMs and snabbnfv-traffic time to shut down.
    sleep 5

    # Clean up temporary config location.
    rm -f "$TESTCONFPATH"

    exit
}

# Set up graceful `exit'.
trap stop_bench_env EXIT HUP INT QUIT TERM

# Usage: wait_vm_up <port>
# Blocks until ping to 0::0 suceeds.
function wait_vm_up {
    local timeout_counter=0
    local timeout_max=50
    echo -n "Waiting for VM listening on telnet port $1 to get ready..."
    while ( ! (run_telnet $1 "ping6 -c 1 0::0" | grep "1 received" \
        >/dev/null) ); do
        # Time out eventually.
        if [ $timeout_counter -gt $timeout_max ]; then
            echo " [TIMEOUT]"
            exit 1
        fi
        timeout_counter=$(expr $timeout_counter + 1)
        sleep 2
    done
    echo " [OK]"
}

function assert {
    if [ $2 == "0" ]; then echo "$1 succeded."
    else echo "$1 failed."
         exit 1
    fi
}

# Usage: debug_tcpdump <telnet_port> <n>
# Capture <n> packets on eth0 for VM listening in <telnet_port> to
# /eth0.cap.
function debug_tcpdump {<commit_msg>[snabbnfv selftest] Include qemu and snabbnfv logs in output in case of failure.<commit_after>#!/bin/bash

SKIPPED_CODE=43

if [ ! -f ~/bench_conf.sh ]; then
    cat <<EOF
~/bench_conf.sh does not exist, skipping test.

For this test to run you need to configure the following variables:
SNABB_LOG0=<path_to_logfile>
NFV_PCI0=<pciaddr>
NFV_SOCKET0=vhost_A.sock
NFV_SOCKET1=vhost_B.sock
GUEST_IP0=fe80::5054:ff:fe00:0
GUEST_MAC0=52:54:00:00:00:00
IMAGE0=<ubuntu-image-a>
GUEST_IP1=fe80::5054:ff:fe00:1
GUEST_MAC1=52:54:00:00:00:01
IMAGE1=<ubuntu-image-b>
BOOTARGS0="earlyprintk root=/dev/vda rw console=ttyS0 ip=$GUEST_IP0"
BOOTARGS1="earlyprintk root=/dev/vda rw console=ttyS0 ip=$GUEST_IP1"
EOF
    exit $SKIPPED_CODE
fi

. /etc/bench_conf.sh
. ~/bench_conf.sh

export BENCH_ENV_PID

TESTCONFPATH="/tmp/snabb_nfv_selftest_ports.$$"
FUZZCONFPATH="/tmp/snabb_nfv_selftest_fuzz$$.ports"

# Usage: run_telnet <port> <command> [<sleep>]
# Runs <command> on VM listening on telnet <port>. Waits <sleep> seconds
# for before closing connection. The default of <sleep> is 2.
function run_telnet {
    (echo "$2"; sleep ${3:-2}) \
        | telnet localhost $1 2>&1
}

# Usage: load_config <path>
# Copies <path> to TESTCONFPATH and sleeps for a bit.
function load_config {
    echo "USING $1"
    cp "$1" "$TESTCONFPATH"
    sleep 2
}

function start_bench_env {
    scripts/bench_env/host-nic-snabbnfv-guests.sh $TESTCONFPATH &
    BENCH_ENV_PID=$!

    # Give bench_env time to print its stuff.
    sleep 0.25

    # Wait until VMs are ready.
    wait_vm_up $TELNET_PORT0
    wait_vm_up $TELNET_PORT1

    # Manually set ip addresses.
    run_telnet $TELNET_PORT0 "ifconfig eth0 up" >/dev/null
    run_telnet $TELNET_PORT1 "ifconfig eth0 up" >/dev/null
    run_telnet $TELNET_PORT0 "ip -6 addr add $GUEST_IP0 dev eth0" >/dev/null
    run_telnet $TELNET_PORT1 "ip -6 addr add $GUEST_IP1 dev eth0" >/dev/null

}

function stop_bench_env {
    kill $BENCH_ENV_PID

    # Give VMs and snabbnfv-traffic time to shut down.
    sleep 5

    # Clean up temporary config location.
    rm -f "$TESTCONFPATH"

    exit
}

# Set up graceful `exit'.
trap stop_bench_env EXIT HUP INT QUIT TERM

# Usage: wait_vm_up <port>
# Blocks until ping to 0::0 suceeds.
function wait_vm_up {
    local timeout_counter=0
    local timeout_max=50
    echo -n "Waiting for VM listening on telnet port $1 to get ready..."
    while ( ! (run_telnet $1 "ping6 -c 1 0::0" | grep "1 received" \
        >/dev/null) ); do
        # Time out eventually.
        if [ $timeout_counter -gt $timeout_max ]; then
            echo " [TIMEOUT]"
            exit 1
        fi
        timeout_counter=$(expr $timeout_counter + 1)
        sleep 2
    done
    echo " [OK]"
}

function assert {
    if [ $2 == "0" ]; then echo "$1 succeded."
    else
        echo "$1 failed."
        echo
        echo "qemu ($IMAGE0) log:"
        cat "$(basename "$IMAGE0").log"
        echo
        echo
        echo "qemu ($IMAGE1) log:"
        cat "$(basename "$IMAGE1").log"
        echo
        echo
        echo "snabbnfv log:"
        cat "$SNABB_LOG0"
        exit 1
    fi
}

# Usage: debug_tcpdump <telnet_port> <n>
# Capture <n> packets on eth0 for VM listening in <telnet_port> to
# /eth0.cap.
function debug_tcpdump {
{'fraction_non_alphanumeric': 0.11324717285945073, 'fraction_numerical': 0.036348949919224556, 'mean_word_length': 3.1859364435429343, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1343039', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2374, 'n_words': 640}