summaryrefslogtreecommitdiffstats
path: root/tests/qtest/virtio-net-test.c
blob: 8bf74e516cce23117696e4b23396a9244c473588 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * QTest testcase for VirtIO NIC
 *
 * Copyright (c) 2014 SUSE LINUX Products GmbH
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "qemu-common.h"
#include "libqtest-single.h"
#include "qemu/iov.h"
#include "qemu/module.h"
#include "qapi/qmp/qdict.h"
#include "hw/virtio/virtio-net.h"
#include "libqos/qgraph.h"
#include "libqos/virtio-net.h"

#ifndef ETH_P_RARP
#define ETH_P_RARP 0x8035
#endif

#define PCI_SLOT_HP             0x06
#define PCI_SLOT                0x04

#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)

#ifndef _WIN32

static void rx_test(QVirtioDevice *dev,
                    QGuestAllocator *alloc, QVirtQueue *vq,
                    int socket)
{
    QTestState *qts = global_qtest;
    uint64_t req_addr;
    uint32_t free_head;
    char test[] = "TEST";
    char buffer[64];
    int len = htonl(sizeof(test));
    struct iovec iov[] = {
        {
            .iov_base = &len,
            .iov_len = sizeof(len),
        }, {
            .iov_base = test,
            .iov_len = sizeof(test),
        },
    };
    int ret;

    req_addr = guest_alloc(alloc, 64);

    free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
    qvirtqueue_kick(qts, dev, vq, free_head);

    ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
    g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));

    qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
                           QVIRTIO_NET_TIMEOUT_US);
    memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
    g_assert_cmpstr(buffer, ==, "TEST");

    guest_free(alloc, req_addr);
}

static void tx_test(QVirtioDevice *dev,
                    QGuestAllocator *alloc, QVirtQueue *vq,
                    int socket)
{
    QTestState *qts = global_qtest;
    uint64_t req_addr;
    uint32_t free_head;
    uint32_t len;
    char buffer[64];
    int ret;

    req_addr = guest_alloc(alloc, 64);
    memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);

    free_head = qvirtqueue_add(qts, vq, req_addr, 64, false, false);
    qvirtqueue_kick(qts, dev, vq, free_head);

    qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
                           QVIRTIO_NET_TIMEOUT_US);
    guest_free(alloc, req_addr);

    ret = qemu_recv(socket, &len, sizeof(len), 0);
    g_assert_cmpint(ret, ==, sizeof(len));
    len = ntohl(len);

    ret = qemu_recv(socket, buffer, len, 0);
    g_assert_cmpstr(buffer, ==, "TEST");
}

static void rx_stop_cont_test(QVirtioDevice *dev,
                              QGuestAllocator *alloc, QVirtQueue *vq,
                              int socket)
{
    QTestState *qts = global_qtest;
    uint64_t req_addr;
    uint32_t free_head;
    char test[] = "TEST";
    char buffer[64];
    int len = htonl(sizeof(test));
    QDict *rsp;
    struct iovec iov[] = {
        {
            .iov_base = &len,
            .iov_len = sizeof(len),
        }, {
            .iov_base = test,
            .iov_len = sizeof(test),
        },
    };
    int ret;

    req_addr = guest_alloc(alloc, 64);

    free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
    qvirtqueue_kick(qts, dev, vq, free_head);

    rsp = qmp("{ 'execute' : 'stop'}");
    qobject_unref(rsp);

    ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
    g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));

    /* We could check the status, but this command is more importantly to
     * ensure the packet data gets queued in QEMU, before we do 'cont'.
     */
    rsp = qmp("{ 'execute' : 'query-status'}");
    qobject_unref(rsp);
    rsp = qmp("{ 'execute' : 'cont'}");
    qobject_unref(rsp);

    qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
                           QVIRTIO_NET_TIMEOUT_US);
    memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
    g_assert_cmpstr(buffer, ==, "TEST");

    guest_free(alloc, req_addr);
}

static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
{
    QVirtioNet *net_if = obj;
    QVirtioDevice *dev = net_if->vdev;
    QVirtQueue *rx = net_if->queues[0];
    QVirtQueue *tx = net_if->queues[1];
    int *sv = data;

    rx_test(dev, t_alloc, rx, sv[0]);
    tx_test(dev, t_alloc, tx, sv[0]);
}

static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
{
    QVirtioNet *net_if = obj;
    QVirtioDevice *dev = net_if->vdev;
    QVirtQueue *rx = net_if->queues[0];
    int *sv = data;

    rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
}

#endif

static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
{
    QVirtioPCIDevice *dev = obj;
    QTestState *qts = dev->pdev->bus->qts;
    const char *arch = qtest_get_arch();

    qtest_qmp_device_add(qts, "virtio-net-pci", "net1",
                         "{'addr': %s}", stringify(PCI_SLOT_HP));

    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
        qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
    }
}

static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
{
    int *sv = data;
    char buffer[60];
    int len;
    QDict *rsp;
    int ret;
    uint16_t *proto = (uint16_t *)&buffer[12];
    size_t total_received = 0;
    uint64_t start, now, last_rxt, deadline;

    /* Send a set of packets over a few second period */
    rsp = qmp("{ 'execute' : 'announce-self', "
                  " 'arguments': {"
                      " 'initial': 20, 'max': 100,"
                      " 'rounds': 300, 'step': 10, 'id': 'bob' } }");
    assert(!qdict_haskey(rsp, "error"));
    qobject_unref(rsp);

    /* Catch the first packet and make sure it's a RARP */
    ret = qemu_recv(sv[0], &len, sizeof(len), 0);
    g_assert_cmpint(ret, ==,  sizeof(len));
    len = ntohl(len);

    ret = qemu_recv(sv[0], buffer, len, 0);
    g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));

    /*
     * Stop the announcment by settings rounds to 0 on the
     * existing timer.
     */
    rsp = qmp("{ 'execute' : 'announce-self', "
                  " 'arguments': {"
                      " 'initial': 20, 'max': 100,"
                      " 'rounds': 0, 'step': 10, 'id': 'bob' } }");
    assert(!qdict_haskey(rsp, "error"));
    qobject_unref(rsp);

    /* Now make sure the packets stop */

    /* Times are in us */
    start = g_get_monotonic_time();
    /* 30 packets, max gap 100ms, * 4 for wiggle */
    deadline = start + 1000 * (100 * 30 * 4);
    last_rxt = start;

    while (true) {
        int saved_err;
        ret = qemu_recv(sv[0], buffer, 60, MSG_DONTWAIT);
        saved_err = errno;
        now = g_get_monotonic_time();
        g_assert_cmpint(now, <, deadline);

        if (ret >= 0) {
            if (ret) {
                last_rxt = now;
            }
            total_received += ret;

            /* Check it's not spewing loads */
            g_assert_cmpint(total_received, <, 60 * 30 * 2);
        } else {
            g_assert_cmpint(saved_err, ==, EAGAIN);

            /* 400ms, i.e. 4 worst case gaps */
            if ((now - last_rxt) > (1000 * 100 * 4)) {
                /* Nothings arrived for a while - must have stopped */
                break;
            };

            /* 100ms */
            g_usleep(1000 * 100);
        }
    };
}

static void virtio_net_test_cleanup(void *sockets)
{
    int *sv = sockets;

    close(sv[0]);
    qos_invalidate_command_line();
    close(sv[1]);
    g_free(sv);
}

static void *virtio_net_test_setup(GString *cmd_line, void *arg)
{
    int ret;
    int *sv = g_new(int, 2);

    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
    g_assert_cmpint(ret, !=, -1);

    g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);

    g_test_queue_destroy(virtio_net_test_cleanup, sv);
    return sv;
}

static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
{
    QVirtioNet *dev = obj;
    QVirtQueue *vq = dev->queues[1];
    uint64_t req_addr;
    uint32_t free_head;
    size_t alloc_size = (size_t)data / 64;
    QTestState *qts = global_qtest;
    int i;

    /* Bypass the limitation by pointing several descriptors to a single
     * smaller area */
    req_addr = guest_alloc(t_alloc, alloc_size);
    free_head = qvirtqueue_add(qts, vq, req_addr, alloc_size, false, true);

    for (i = 0; i < 64; i++) {
        qvirtqueue_add(qts, vq, req_addr, alloc_size, false, i != 63);
    }
    qvirtqueue_kick(qts, dev->vdev, vq, free_head);

    qvirtio_wait_used_elem(qts, dev->vdev, vq, free_head, NULL,
                           QVIRTIO_NET_TIMEOUT_US);
    guest_free(t_alloc, req_addr);
}

static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
{
    g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
    return arg;
}

static void register_virtio_net_test(void)
{
    QOSGraphTestOptions opts = {
        .before = virtio_net_test_setup,
    };

    qos_add_test("hotplug", "virtio-net-pci", hotplug, &opts);
#ifndef _WIN32
    qos_add_test("basic", "virtio-net", send_recv_test, &opts);
    qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
#endif
    qos_add_test("announce-self", "virtio-net", announce_self, &opts);

    /* These tests do not need a loopback backend.  */
    opts.before = virtio_net_test_setup_nosocket;
    opts.arg = (gpointer)UINT_MAX;
    qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
    opts.arg = (gpointer)NET_BUFSIZE;
    qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
}

libqos_init(register_virtio_net_test);