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