xref: /openbmc/qemu/tests/qemu-iotests/tests/nbd-tls-iothread (revision 2d32a5d2a06a524c5e7967f918c406ec99418a34)
1#!/usr/bin/env bash
2# group: rw quick
3#
4# Test of NBD+TLS+iothread
5#
6# Copyright (C) 2024 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22# creator
23owner=eblake@redhat.com
24
25seq=`basename $0`
26echo "QA output created by $seq"
27
28status=1    # failure is the default!
29
30_cleanup()
31{
32    _cleanup_qemu
33    _cleanup_test_img
34    rm -f "$dst_image"
35    tls_x509_cleanup
36}
37trap "_cleanup; exit \$status" 0 1 2 3 15
38
39# get standard environment, filters and checks
40cd ..
41. ./common.rc
42. ./common.filter
43. ./common.qemu
44. ./common.tls
45. ./common.nbd
46
47_supported_fmt qcow2  # Hardcoded to qcow2 command line and QMP below
48_supported_proto file
49
50# pick_unused_port
51#
52# Picks and returns an "unused" port, setting the global variable
53# $port.
54#
55# This is inherently racy, but we need it because qemu does not currently
56# permit NBD+TLS over a Unix domain socket
57pick_unused_port ()
58{
59    if ! (ss --version) >/dev/null 2>&1; then
60        _notrun "ss utility required, skipped this test"
61    fi
62
63    # Start at a random port to make it less likely that two parallel
64    # tests will conflict.
65    port=$(( 50000 + (RANDOM%15000) ))
66    while ss -ltn | grep -sqE ":$port\b"; do
67        ((port++))
68        if [ $port -eq 65000 ]; then port=50000; fi
69    done
70    echo picked unused port
71}
72
73tls_x509_init
74
75size=1G
76DST_IMG="$TEST_DIR/dst.qcow2"
77
78echo
79echo "== preparing TLS creds and spare port =="
80
81pick_unused_port
82tls_x509_create_root_ca "ca1"
83tls_x509_create_server "ca1" "server1"
84tls_x509_create_client "ca1" "client1"
85tls_obj_base=tls-creds-x509,id=tls0,verify-peer=true,dir="${tls_dir}"
86
87echo
88echo "== preparing image =="
89
90_make_test_img $size
91$QEMU_IMG create -f qcow2 "$DST_IMG" $size | _filter_img_create
92
93echo
94echo === Starting Src QEMU ===
95echo
96
97_launch_qemu -machine q35 \
98    -object iothread,id=iothread0 \
99    -object "${tls_obj_base}"/client1,endpoint=client \
100    -device '{"driver":"pcie-root-port", "id":"root0", "multifunction":true,
101              "bus":"pcie.0"}' \
102    -device '{"driver":"virtio-scsi-pci", "id":"virtio_scsi_pci0",
103              "bus":"root0", "iothread":"iothread0"}' \
104    -device '{"driver":"scsi-hd", "id":"image1", "drive":"drive_image1",
105              "bus":"virtio_scsi_pci0.0"}' \
106    -blockdev '{"driver":"file", "cache":{"direct":true, "no-flush":false},
107                "filename":"'"$TEST_IMG"'", "node-name":"drive_sys1"}' \
108    -blockdev '{"driver":"qcow2", "node-name":"drive_image1",
109                "file":"drive_sys1"}'
110h1=$QEMU_HANDLE
111_send_qemu_cmd $h1 '{"execute": "qmp_capabilities"}' 'return'
112
113echo
114echo === Starting Dst VM2 ===
115echo
116
117_launch_qemu -machine q35 \
118    -object iothread,id=iothread0 \
119    -object "${tls_obj_base}"/server1,endpoint=server \
120    -device '{"driver":"pcie-root-port", "id":"root0", "multifunction":true,
121              "bus":"pcie.0"}' \
122    -device '{"driver":"virtio-scsi-pci", "id":"virtio_scsi_pci0",
123              "bus":"root0", "iothread":"iothread0"}' \
124    -device '{"driver":"scsi-hd", "id":"image1", "drive":"drive_image1",
125              "bus":"virtio_scsi_pci0.0"}' \
126    -blockdev '{"driver":"file", "cache":{"direct":true, "no-flush":false},
127                "filename":"'"$DST_IMG"'", "node-name":"drive_sys1"}' \
128    -blockdev '{"driver":"qcow2", "node-name":"drive_image1",
129                "file":"drive_sys1"}' \
130    -incoming defer
131h2=$QEMU_HANDLE
132_send_qemu_cmd $h2 '{"execute": "qmp_capabilities"}' 'return'
133
134echo
135echo === Dst VM: Enable NBD server for incoming storage migration ===
136echo
137
138_send_qemu_cmd $h2 '{"execute": "nbd-server-start", "arguments":
139    {"addr": {"type": "inet", "data": {"host": "127.0.0.1", "port": "'$port'"}},
140              "tls-creds": "tls0"}}' '{"return": {}}' | sed "s/\"$port\"/PORT/g"
141_send_qemu_cmd $h2 '{"execute": "block-export-add", "arguments":
142    {"node-name": "drive_image1", "type": "nbd", "writable": true,
143      "id": "drive_image1"}}' '{"return": {}}'
144
145echo
146echo === Src VM: Mirror to dst NBD for outgoing storage migration ===
147echo
148
149_send_qemu_cmd $h1 '{"execute": "blockdev-add", "arguments":
150    {"node-name": "mirror", "driver": "nbd",
151     "server": {"type": "inet", "host": "127.0.0.1", "port": "'$port'"},
152     "export": "drive_image1", "tls-creds": "tls0",
153     "tls-hostname": "127.0.0.1"}}' '{"return": {}}' | sed "s/\"$port\"/PORT/g"
154_send_qemu_cmd $h1 '{"execute": "blockdev-mirror", "arguments":
155    {"sync": "full", "device": "drive_image1", "target": "mirror",
156     "job-id": "drive_image1_53"}}' '{"return": {}}'
157_timed_wait_for $h1 '"ready"'
158
159echo
160echo === Cleaning up ===
161echo
162
163_send_qemu_cmd $h1 '{"execute":"quit"}' ''
164_send_qemu_cmd $h2 '{"execute":"quit"}' ''
165
166echo "*** done"
167rm -f $seq.full
168status=0
169