From b82fb0ceecbd74106b2aaa89ac29b7311f10a8d7 Mon Sep 17 00:00:00 2001 From: Kostis Papazafeiropoulos Date: Mon, 2 Sep 2024 20:35:14 +0100 Subject: [PATCH] virtio-accel: Add Dockerfile and workflow to build and push image Signed-off-by: Kostis Papazafeiropoulos --- .github/workflows/vaccel-build-and-upload.yml | 118 ++++++++++ subprojects/vaccel/.gitignore | 1 + subprojects/vaccel/docker/Dockerfile | 64 ++++++ subprojects/vaccel/docker/qemu-ifup | 38 +++ subprojects/vaccel/docker/qemu-script.sh | 216 ++++++++++++++++++ subprojects/vaccel/docker/vq-size.patch | 13 ++ 6 files changed, 450 insertions(+) create mode 100644 .github/workflows/vaccel-build-and-upload.yml create mode 100644 subprojects/vaccel/.gitignore create mode 100644 subprojects/vaccel/docker/Dockerfile create mode 100755 subprojects/vaccel/docker/qemu-ifup create mode 100755 subprojects/vaccel/docker/qemu-script.sh create mode 100644 subprojects/vaccel/docker/vq-size.patch diff --git a/.github/workflows/vaccel-build-and-upload.yml b/.github/workflows/vaccel-build-and-upload.yml new file mode 100644 index 00000000000..c8ed44ded30 --- /dev/null +++ b/.github/workflows/vaccel-build-and-upload.yml @@ -0,0 +1,118 @@ +name: Build QEMU+vAccel docker image + +on: + push: + branches: [ '*\+vaccel' ] + + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +env: + REGISTRY: harbor.nbfc.io/nubificus + IMAGE_NAME: qemu-vaccel + APP: qemu-vaccel + +jobs: + build: + name: Build Docker Image + runs-on: [self-hosted, gcc, lite, "${{ matrix.arch }}"] + strategy: + matrix: + arch: [x86_64, aarch64] + outputs: + digest-x86_64: ${{ steps.set-outputs.outputs.digest-x86_64 }} + digest-aarch64: ${{ steps.set-outputs.outputs.digest-aarch64 }} + steps: + - name: Cleanup previous jobs + run: | + echo "Cleaning up previous runs" + sudo rm -rf ${{ github.workspace }}/* + sudo rm -rf ${{ github.workspace }}/.??* + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.HARBOR_USER }} + password: ${{ secrets.HARBOR_PASSWD }} + + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=raw,value=${{ matrix.arch }} + type=sha,prefix=${{ matrix.arch }}- + type=sha,format=long,prefix=${{ matrix.arch }}- + type=ref,event=branch,prefix=${{ matrix.arch }}- + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v6 + with: + context: ./subprojects/vaccel/docker + no-cache: true + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + ARCHTAG=${{ matrix.arch }} + BRANCH=${{ github.event.ref_name || github.ref_name }} + + - name: Set per-arch outputs + id: set-outputs + run: | + # Workaround for https://github.com/actions/runner/issues/2499 + echo "digest-${{ matrix.arch }}=${{ steps.build-and-push.outputs.digest }}" \ + >> "$GITHUB_OUTPUT" + + sign: + name: Sign Docker Image + runs-on: [self-hosted] + needs: [build] + strategy: + matrix: + arch: [x86_64, aarch64] + permissions: + contents: read + id-token: write + + steps: + - name: Install Cosign + uses: sigstore/cosign-installer@v3.6.0 + + - name: Check install + run: cosign version + + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.HARBOR_USER }} + password: ${{ secrets.HARBOR_PASSWD }} + + - name: Sign published Docker image + env: + DIGEST: ${{ needs.build.outputs[format('digest-{0}', matrix.arch)] }} + run: | + cosign sign --yes ${{ env.REGISTRY }}/${{ env.APP }}@${{ env.DIGEST }} \ + -a "repo=${{ github.repository }}" \ + -a "workflow=${{ github.workflow }}" \ + -a "ref=${{ github.sha }}" \ + -a "author=Nubificus LTD" + + - name: Cleanup previous runs + if: ${{ always() }} + run: | + sudo rm -rf ${{ github.workspace }}/* + sudo rm -rf ${{ github.workspace }}/.??* diff --git a/subprojects/vaccel/.gitignore b/subprojects/vaccel/.gitignore new file mode 100644 index 00000000000..69f1bf45309 --- /dev/null +++ b/subprojects/vaccel/.gitignore @@ -0,0 +1 @@ +!*.patch diff --git a/subprojects/vaccel/docker/Dockerfile b/subprojects/vaccel/docker/Dockerfile new file mode 100644 index 00000000000..a7246b802d5 --- /dev/null +++ b/subprojects/vaccel/docker/Dockerfile @@ -0,0 +1,64 @@ +FROM ubuntu:24.04 + +WORKDIR / + +# Install common build utilities +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -yy eatmydata && \ + DEBIAN_FRONTEND=noninteractive eatmydata \ + apt-get install -y --no-install-recommends \ + gcc \ + g++ \ + build-essential \ + libglib2.0-dev \ + libfdt-dev \ + libpixman-1-dev \ + libslirp-dev \ + zlib1g-dev \ + libcap-ng-dev \ + libattr1-dev \ + ninja-build \ + git \ + python3-pip \ + libclang-dev \ + pkg-config \ + iproute2 \ + openssh-client \ + iputils-ping \ + socat \ + vim \ + less \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* \ + && pip install --break-system-packages meson + +# Build & install vAccel +RUN git clone https://github.com/nubificus/vaccel && \ + cd vaccel && \ + meson setup -Dplugins=enabled -Dexamples=enabled build && \ + meson compile -C build && \ + meson install -C build && \ + ldconfig && \ + cd .. && rm -rf vaccel + +ARG BRANCH=master+vaccel +ARG ARCHTAG=x86_64 +ARG DOCKER_DIR=. +COPY ${DOCKER_DIR}/vq-size.patch /vq-size.patch +# Build & install QEMU w/ vAccel backend +RUN git clone -b ${BRANCH} --depth 1 \ + https://github.com/cloudkernels/qemu-vaccel && \ + cd qemu-vaccel && \ + mv /vq-size.patch . && \ + git apply vq-size.patch && \ + mkdir build && cd build && \ + ../configure --target-list=${ARCHTAG}-softmmu --enable-virtfs && \ + make -j$(nproc) && make install && \ + cd ../.. && rm -rf qemu-vaccel + +COPY ${DOCKER_DIR}/qemu-ifup /usr/local/etc/qemu-ifup +COPY ${DOCKER_DIR}/qemu-script.sh /run.sh + +VOLUME /data +WORKDIR /data +ENTRYPOINT ["/run.sh"] diff --git a/subprojects/vaccel/docker/qemu-ifup b/subprojects/vaccel/docker/qemu-ifup new file mode 100755 index 00000000000..6d306aabc7f --- /dev/null +++ b/subprojects/vaccel/docker/qemu-ifup @@ -0,0 +1,38 @@ +#! /bin/sh +# Script to bring a network (tap) device for qemu up. +# The idea is to add the tap device to the same bridge +# as we have default routing to. + +# in order to be able to find brctl +PATH=$PATH:/sbin:/usr/sbin +ip=$(which ip) + +if [ -n "$ip" ]; then + ip link set "$1" up +else + brctl=$(which brctl) + if [ ! "$ip" -o ! "$brctl" ]; then + echo "W: $0: not doing any bridge processing: neither ip nor brctl utility not found" >&2 + exit 0 + fi + ifconfig "$1" 0.0.0.0 up +fi + +switch=virbr0 + +# only add the interface to default-route bridge if we +# have such interface (with default route) and if that +# interface is actually a bridge. +# It is possible to have several default routes too +for br in $switch; do + if [ -d /sys/class/net/$br/bridge/. ]; then + if [ -n "$ip" ]; then + ip link set "$1" master "$br" + else + brctl addif $br "$1" + fi + exit # exit with status of the previous command + fi +done + +echo "W: $0: no bridge for guest interface found" >&2 diff --git a/subprojects/vaccel/docker/qemu-script.sh b/subprojects/vaccel/docker/qemu-script.sh new file mode 100755 index 00000000000..0dd320c7063 --- /dev/null +++ b/subprojects/vaccel/docker/qemu-script.sh @@ -0,0 +1,216 @@ +#!/bin/bash + +set -e + +export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH} +export QEMU_AUDIO_DRV=none +export VACCEL_BACKENDS=${VACCEL_BACKENDS:=libvaccel-noop.so} +export VACCEL_DEBUG_LEVEL=${VACCEL_DEBUG_LEVEL:=4} + +SCRIPT_NAME=$(basename "$0") +RUN_PATH=./run +SSH_HOST=localhost +SSH_PORT_LOCAL=60022 +SSH_PORT_VM=22 + +smp=1 +cpu=host +ram=512 + +machine=pc,accel=kvm +kernel=bzImage +rootfs=rootfs.img +cmdline='rw root=/dev/vda console=ttyS0 ' +stderr=${RUN_PATH}/stderr.log +network="-nic user,model=virtio-net-pci,hostfwd=tcp::${SSH_PORT_LOCAL}-:${SSH_PORT_VM}" + +error() { + local error=${1:-'Unknown error'} + local code=${2:-1} + echo "${SCRIPT_NAME}: $error" >&2 + exit "$code" +} + +setup_qemu_socket() { + [[ -z "$1" ]] && error "'setup_qemu_socket()' requires a non-empty string" + stderr="${RUN_PATH}/${1}-stderr.log" + extra_args+="-serial pipe:${RUN_PATH}/${1}.serial " + extra_args+="-chardev socket,id=monitor,path=${RUN_PATH}/${1}.monitor,server=on,wait=off " + extra_args+='-monitor chardev:monitor ' + mkfifo "${RUN_PATH}/${1}.serial.in" "${RUN_PATH}/${1}.serial.out" +} + +cleanup_qemu_socket() { + [[ -n "$1" ]] && + rm -f "${RUN_PATH}/${1}.serial.in" "${RUN_PATH}/${1}.serial.out" \ + "${RUN_PATH}/${1}.monitor" || + true +} + +cleanup_stderr() { + [[ -n "$1" ]] && [[ -z "$(cat "$1")" ]] && rm -f "$1" || true +} + +run_cmd() { + [[ -z "$1" ]] && error "'run_cmd()' requires a non-empty string" + sleep 2 + ssh -o StrictHostKeyChecking=accept-new -o BatchMode=yes \ + "${SSH_HOST}" -p "${SSH_PORT_LOCAL}" "${1}; poweroff 2>/dev/null" +} + +parse_args() { + short_opts=M:m:r:k:s:n::v::c: + read -r -d '' long_opts <<-EOF || true + machine:,cpu:,dtb:, + vcpus:,ram:,rootfs:,kernel:,cmdline-append:,output-socket:, + network::,vsock::,cmd: + EOF + + if ! getopt=$(getopt -o "${short_opts}" --long "${long_opts}" \ + -n "${SCRIPT_NAME}" -- "$@"); then + echo 'Failed to parse args' >&2 + exit 1 + fi + + eval set -- "$getopt" + unset "$getopt" + + while true; do + case "$1" in + '-M' | '--machine') + # QEMU Machine + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + machine="$2" + shift 2 + ;; + '--cpu') + # QEMU CPU + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + cpu="$2" + shift 2 + ;; + '--dtb') + # QEMU CPU + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + dtb="$2" + extra_args+="-dtb ${dtb} " + shift 2 + ;; + '--vcpus') + # VM vCPUs + [[ "$2" =~ ^[0-9]+$ ]] || error "'$1' requires a number" + smp="$2" + shift 2 + ;; + '-m' | '--memory') + # VM RAM + [[ "$2" =~ ^[0-9]+$ ]] || error "'$1' requires a number" + ram="$2" + shift 2 + ;; + '-r' | '--rootfs') + # VM rootfs + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + rootfs="$2" + shift 2 + ;; + '-k' | '--kernel') + # VM kernel + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + kernel="$2" + shift 2 + ;; + '--cmdline-append') + # VM kernel command line append + cmdline+="$2 " + shift 2 + ;; + '-s' | '--output-socket') + # QEMU output to socket + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + qemu_socket="$2" + shift 2 + ;; + '-n' | '--network') + # VM w/ network + [[ -z "$2" ]] && mac='52:54:00:12:34:01' || mac="$2" + network="-nic tap,model=virtio-net-pci,mac=${mac}" + shift 2 + ;; + '-v' | '--vsock') + # VM w/ vsock + [[ "$2" =~ ^[0-9]+$ ]] || error "'$1' requires a number" + cid="$2" + extra_args+="-device vhost-vsock-pci,id=vhost-vsock-pci0,guest-cid=${cid} " + shift 2 + ;; + '-c' | '--cmd') + # Command to run in the VM + [[ -z "$2" ]] && error "'$1' requires a non-empty string" + cmd="$2" + shift 2 + ;; + --) + shift + break + ;; + *) + echo 'Internal error parsing args' >&2 + exit 1 + ;; + esac + done + cmdline+="mem=${ram}M" +} + +run_qemu() { + TERM=linux qemu-system-"$(uname -m)" \ + -cpu "${cpu}" -m "${ram}" -smp "${smp}" -M "${machine}" -nographic \ + -kernel "${kernel}" -append "${cmdline}" --enable-kvm \ + -drive if=none,id=rootfs,file="${rootfs}",format=raw,cache=none \ + -device virtio-blk,drive=rootfs \ + -fsdev local,id=fsdev0,path=/data/data,security_model=none \ + -device virtio-9p-pci,fsdev=fsdev0,mount_tag=data \ + -device virtio-rng-pci \ + -object acceldev-backend-vaccel,id=rt0 \ + -device virtio-accel-pci,id=accel0,runtime=rt0 \ + ${network} ${extra_args} 2>"${stderr}" +} + +main() { + parse_args "$@" + + cd /data + mkdir -p ${RUN_PATH} data + [[ -n "${qemu_socket}" ]] && setup_qemu_socket "${qemu_socket}" + fsck.ext4 -fy "${rootfs}" 1>/dev/null 2>&1 + + res=0 + + if [[ -z "${cmd}" ]]; then + run_qemu || res="$?" + cleanup_qemu_socket "${qemu_socket}" + cleanup_stderr "${stderr}" + exit "${res}" + fi + + if [[ -z "${qemu_socket}" ]]; then + qemu_socket=qemu-$( + od -vN 3 -An -tx1 /dev/urandom | tr -d " \n" + echo + ) + setup_qemu_socket "${qemu_socket}" + fi + + run_qemu & + pid_qemu=$! + + run_cmd "${cmd}" || res="$?" + wait "${pid_qemu}" || res="${res:-"$?"}" + + cleanup_qemu_socket "${qemu_socket}" + cleanup_stderr "${stderr}" + exit "${res}" +} + +main "$@" diff --git a/subprojects/vaccel/docker/vq-size.patch b/subprojects/vaccel/docker/vq-size.patch new file mode 100644 index 00000000000..27741c18f6a --- /dev/null +++ b/subprojects/vaccel/docker/vq-size.patch @@ -0,0 +1,13 @@ +diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h +index b69d517496..64967d7e3d 100644 +--- a/include/hw/virtio/virtio.h ++++ b/include/hw/virtio/virtio.h +@@ -47,7 +47,7 @@ size_t virtio_feature_get_config_size(VirtIOFeature *features, + + typedef struct VirtQueue VirtQueue; + +-#define VIRTQUEUE_MAX_SIZE 1024 ++#define VIRTQUEUE_MAX_SIZE 8192 + + typedef struct VirtQueueElement + {