Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QTest testcase for VirtIO NIC |
| 3 | * |
| 4 | * Copyright (c) 2014 SUSE LINUX Products GmbH |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
| 9 | |
| 10 | #include <glib.h> |
| 11 | #include <string.h> |
| 12 | #include "libqtest.h" |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 13 | #include "qemu-common.h" |
| 14 | #include "qemu/sockets.h" |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 16 | #include "qemu/iov.h" |
| 17 | #include "libqos/pci-pc.h" |
| 18 | #include "libqos/virtio.h" |
| 19 | #include "libqos/virtio-pci.h" |
| 20 | #include "libqos/malloc.h" |
| 21 | #include "libqos/malloc-pc.h" |
| 22 | #include "libqos/malloc-generic.h" |
| 23 | #include "qemu/bswap.h" |
| 24 | #include "hw/virtio/virtio-net.h" |
Igor Mammedov | 9224709 | 2014-09-26 09:28:10 +0000 | [diff] [blame] | 25 | |
| 26 | #define PCI_SLOT_HP 0x06 |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 27 | #define PCI_SLOT 0x04 |
| 28 | #define PCI_FN 0x00 |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 29 | |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 30 | #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000) |
| 31 | #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf) |
| 32 | |
| 33 | static void test_end(void) |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 34 | { |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 35 | qtest_end(); |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 38 | #ifndef _WIN32 |
| 39 | |
| 40 | static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot) |
| 41 | { |
| 42 | QVirtioPCIDevice *dev; |
| 43 | |
| 44 | dev = qvirtio_pci_device_find(bus, QVIRTIO_NET_DEVICE_ID); |
| 45 | g_assert(dev != NULL); |
| 46 | g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_NET_DEVICE_ID); |
| 47 | |
| 48 | qvirtio_pci_device_enable(dev); |
| 49 | qvirtio_reset(&qvirtio_pci, &dev->vdev); |
| 50 | qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev); |
| 51 | qvirtio_set_driver(&qvirtio_pci, &dev->vdev); |
| 52 | |
| 53 | return dev; |
| 54 | } |
| 55 | |
| 56 | static QPCIBus *pci_test_start(int socket) |
| 57 | { |
| 58 | char *cmdline; |
| 59 | |
| 60 | cmdline = g_strdup_printf("-netdev socket,fd=%d,id=hs0 -device " |
| 61 | "virtio-net-pci,netdev=hs0", socket); |
| 62 | qtest_start(cmdline); |
| 63 | g_free(cmdline); |
| 64 | |
| 65 | return qpci_init_pc(); |
| 66 | } |
| 67 | |
| 68 | static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev) |
| 69 | { |
| 70 | uint32_t features; |
| 71 | |
| 72 | features = qvirtio_get_features(bus, dev); |
| 73 | features = features & ~(QVIRTIO_F_BAD_FEATURE | |
| 74 | QVIRTIO_F_RING_INDIRECT_DESC | |
| 75 | QVIRTIO_F_RING_EVENT_IDX); |
| 76 | qvirtio_set_features(bus, dev, features); |
| 77 | |
| 78 | qvirtio_set_driver_ok(bus, dev); |
| 79 | } |
| 80 | |
| 81 | static void rx_test(const QVirtioBus *bus, QVirtioDevice *dev, |
| 82 | QGuestAllocator *alloc, QVirtQueue *vq, |
| 83 | int socket) |
| 84 | { |
| 85 | uint64_t req_addr; |
| 86 | uint32_t free_head; |
| 87 | char test[] = "TEST"; |
| 88 | char buffer[64]; |
| 89 | int len = htonl(sizeof(test)); |
| 90 | struct iovec iov[] = { |
| 91 | { |
| 92 | .iov_base = &len, |
| 93 | .iov_len = sizeof(len), |
| 94 | }, { |
| 95 | .iov_base = test, |
| 96 | .iov_len = sizeof(test), |
| 97 | }, |
| 98 | }; |
| 99 | int ret; |
| 100 | |
| 101 | req_addr = guest_alloc(alloc, 64); |
| 102 | |
| 103 | free_head = qvirtqueue_add(vq, req_addr, 64, true, false); |
| 104 | qvirtqueue_kick(bus, dev, vq, free_head); |
| 105 | |
| 106 | ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test)); |
| 107 | g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len)); |
| 108 | |
| 109 | qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US); |
| 110 | memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test)); |
| 111 | g_assert_cmpstr(buffer, ==, "TEST"); |
| 112 | |
| 113 | guest_free(alloc, req_addr); |
| 114 | } |
| 115 | |
| 116 | static void tx_test(const QVirtioBus *bus, QVirtioDevice *dev, |
| 117 | QGuestAllocator *alloc, QVirtQueue *vq, |
| 118 | int socket) |
| 119 | { |
| 120 | uint64_t req_addr; |
| 121 | uint32_t free_head; |
| 122 | uint32_t len; |
| 123 | char buffer[64]; |
| 124 | int ret; |
| 125 | |
| 126 | req_addr = guest_alloc(alloc, 64); |
| 127 | memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4); |
| 128 | |
| 129 | free_head = qvirtqueue_add(vq, req_addr, 64, false, false); |
| 130 | qvirtqueue_kick(bus, dev, vq, free_head); |
| 131 | |
| 132 | qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US); |
| 133 | guest_free(alloc, req_addr); |
| 134 | |
| 135 | ret = qemu_recv(socket, &len, sizeof(len), 0); |
| 136 | g_assert_cmpint(ret, ==, sizeof(len)); |
| 137 | len = ntohl(len); |
| 138 | |
| 139 | ret = qemu_recv(socket, buffer, len, 0); |
| 140 | g_assert_cmpstr(buffer, ==, "TEST"); |
| 141 | } |
| 142 | |
Jason Wang | 8887f84 | 2015-07-17 15:25:54 +0800 | [diff] [blame] | 143 | static void rx_stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev, |
| 144 | QGuestAllocator *alloc, QVirtQueue *vq, |
| 145 | int socket) |
| 146 | { |
| 147 | uint64_t req_addr; |
| 148 | uint32_t free_head; |
| 149 | char test[] = "TEST"; |
| 150 | char buffer[64]; |
| 151 | int len = htonl(sizeof(test)); |
| 152 | struct iovec iov[] = { |
| 153 | { |
| 154 | .iov_base = &len, |
| 155 | .iov_len = sizeof(len), |
| 156 | }, { |
| 157 | .iov_base = test, |
| 158 | .iov_len = sizeof(test), |
| 159 | }, |
| 160 | }; |
| 161 | int ret; |
| 162 | |
| 163 | req_addr = guest_alloc(alloc, 64); |
| 164 | |
| 165 | free_head = qvirtqueue_add(vq, req_addr, 64, true, false); |
| 166 | qvirtqueue_kick(bus, dev, vq, free_head); |
| 167 | |
| 168 | qmp("{ 'execute' : 'stop'}"); |
| 169 | |
| 170 | ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test)); |
| 171 | g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len)); |
| 172 | |
| 173 | /* We could check the status, but this command is more importantly to |
| 174 | * ensure the packet data gets queued in QEMU, before we do 'cont'. |
| 175 | */ |
| 176 | qmp("{ 'execute' : 'query-status'}"); |
| 177 | qmp("{ 'execute' : 'cont'}"); |
| 178 | |
| 179 | qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US); |
| 180 | memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test)); |
| 181 | g_assert_cmpstr(buffer, ==, "TEST"); |
| 182 | |
| 183 | guest_free(alloc, req_addr); |
| 184 | } |
| 185 | |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 186 | static void send_recv_test(const QVirtioBus *bus, QVirtioDevice *dev, |
| 187 | QGuestAllocator *alloc, QVirtQueue *rvq, |
| 188 | QVirtQueue *tvq, int socket) |
| 189 | { |
| 190 | rx_test(bus, dev, alloc, rvq, socket); |
| 191 | tx_test(bus, dev, alloc, tvq, socket); |
| 192 | } |
| 193 | |
Jason Wang | 8887f84 | 2015-07-17 15:25:54 +0800 | [diff] [blame] | 194 | static void stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev, |
| 195 | QGuestAllocator *alloc, QVirtQueue *rvq, |
| 196 | QVirtQueue *tvq, int socket) |
| 197 | { |
| 198 | rx_stop_cont_test(bus, dev, alloc, rvq, socket); |
| 199 | } |
| 200 | |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 201 | static void pci_basic(gconstpointer data) |
| 202 | { |
| 203 | QVirtioPCIDevice *dev; |
| 204 | QPCIBus *bus; |
| 205 | QVirtQueuePCI *tx, *rx; |
| 206 | QGuestAllocator *alloc; |
| 207 | void (*func) (const QVirtioBus *bus, |
| 208 | QVirtioDevice *dev, |
| 209 | QGuestAllocator *alloc, |
| 210 | QVirtQueue *rvq, |
| 211 | QVirtQueue *tvq, |
| 212 | int socket) = data; |
| 213 | int sv[2], ret; |
| 214 | |
| 215 | ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv); |
| 216 | g_assert_cmpint(ret, !=, -1); |
| 217 | |
| 218 | bus = pci_test_start(sv[1]); |
| 219 | dev = virtio_net_pci_init(bus, PCI_SLOT); |
| 220 | |
| 221 | alloc = pc_alloc_init(); |
| 222 | rx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev, |
| 223 | alloc, 0); |
| 224 | tx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev, |
| 225 | alloc, 1); |
| 226 | |
| 227 | driver_init(&qvirtio_pci, &dev->vdev); |
| 228 | func(&qvirtio_pci, &dev->vdev, alloc, &rx->vq, &tx->vq, sv[0]); |
| 229 | |
| 230 | /* End test */ |
| 231 | close(sv[0]); |
| 232 | guest_free(alloc, tx->vq.desc); |
| 233 | pc_alloc_uninit(alloc); |
| 234 | qvirtio_pci_device_disable(dev); |
| 235 | g_free(dev); |
| 236 | qpci_free_pc(bus); |
| 237 | test_end(); |
| 238 | } |
| 239 | #endif |
| 240 | |
Igor Mammedov | 9224709 | 2014-09-26 09:28:10 +0000 | [diff] [blame] | 241 | static void hotplug(void) |
| 242 | { |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 243 | qtest_start("-device virtio-net-pci"); |
| 244 | |
Igor Mammedov | 9224709 | 2014-09-26 09:28:10 +0000 | [diff] [blame] | 245 | qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL); |
| 246 | qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP); |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 247 | |
| 248 | test_end(); |
Igor Mammedov | 9224709 | 2014-09-26 09:28:10 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 251 | int main(int argc, char **argv) |
| 252 | { |
| 253 | int ret; |
| 254 | |
| 255 | g_test_init(&argc, &argv, NULL); |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 256 | #ifndef _WIN32 |
| 257 | qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic); |
Jason Wang | 8887f84 | 2015-07-17 15:25:54 +0800 | [diff] [blame] | 258 | qtest_add_data_func("/virtio/net/pci/rx_stop_cont", |
| 259 | stop_cont_test, pci_basic); |
Jason Wang | 2af4025 | 2015-07-17 15:25:53 +0800 | [diff] [blame] | 260 | #endif |
Igor Mammedov | 9224709 | 2014-09-26 09:28:10 +0000 | [diff] [blame] | 261 | qtest_add_func("/virtio/net/pci/hotplug", hotplug); |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 262 | |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 263 | ret = g_test_run(); |
| 264 | |
Andreas Färber | b815ec5 | 2014-02-09 04:13:37 +0100 | [diff] [blame] | 265 | return ret; |
| 266 | } |