vmnet: stop recieving events when VM is stopped
[qemu/ar7.git] / tests / qtest / e1000e-test.c
blobb63a4d3c91bf1796c18aa3572ed3aa9117288770
1 /*
2 * QTest testcase for e1000e NIC
4 * Copyright (c) 2015 Ravello Systems LTD (http://ravellosystems.com)
5 * Developed by Daynix Computing LTD (http://www.daynix.com)
7 * Authors:
8 * Dmitry Fleytman <dmitry@daynix.com>
9 * Leonid Bloch <leonid@daynix.com>
10 * Yan Vugenfirer <yan@daynix.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #include "qemu/osdep.h"
28 #include "libqtest-single.h"
29 #include "libqos/pci-pc.h"
30 #include "qemu/sockets.h"
31 #include "qemu/iov.h"
32 #include "qemu/module.h"
33 #include "qemu/bitops.h"
34 #include "libqos/libqos-malloc.h"
35 #include "libqos/e1000e.h"
36 #include "hw/net/e1000_regs.h"
38 static void e1000e_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
40 static const char test[] = "TEST";
41 struct e1000_tx_desc descr;
42 char buffer[64];
43 int ret;
44 uint32_t recv_len;
46 /* Prepare test data buffer */
47 uint64_t data = guest_alloc(alloc, sizeof(buffer));
48 memwrite(data, test, sizeof(test));
50 /* Prepare TX descriptor */
51 memset(&descr, 0, sizeof(descr));
52 descr.buffer_addr = cpu_to_le64(data);
53 descr.lower.data = cpu_to_le32(E1000_TXD_CMD_RS |
54 E1000_TXD_CMD_EOP |
55 E1000_TXD_CMD_DEXT |
56 E1000_TXD_DTYP_D |
57 sizeof(buffer));
59 /* Put descriptor to the ring */
60 e1000e_tx_ring_push(d, &descr);
62 /* Wait for TX WB interrupt */
63 e1000e_wait_isr(d, E1000E_TX0_MSG_ID);
65 /* Check DD bit */
66 g_assert_cmphex(le32_to_cpu(descr.upper.data) & E1000_TXD_STAT_DD, ==,
67 E1000_TXD_STAT_DD);
69 /* Check data sent to the backend */
70 ret = recv(test_sockets[0], &recv_len, sizeof(recv_len), 0);
71 g_assert_cmpint(ret, == , sizeof(recv_len));
72 ret = recv(test_sockets[0], buffer, sizeof(buffer), 0);
73 g_assert_cmpint(ret, ==, sizeof(buffer));
74 g_assert_cmpstr(buffer, == , test);
76 /* Free test data buffer */
77 guest_free(alloc, data);
80 static void e1000e_receive_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
82 union e1000_rx_desc_extended descr;
84 char test[] = "TEST";
85 int len = htonl(sizeof(test));
86 struct iovec iov[] = {
88 .iov_base = &len,
89 .iov_len = sizeof(len),
90 },{
91 .iov_base = test,
92 .iov_len = sizeof(test),
96 char buffer[64];
97 int ret;
99 /* Send a dummy packet to device's socket*/
100 ret = iov_send(test_sockets[0], iov, 2, 0, sizeof(len) + sizeof(test));
101 g_assert_cmpint(ret, == , sizeof(test) + sizeof(len));
103 /* Prepare test data buffer */
104 uint64_t data = guest_alloc(alloc, sizeof(buffer));
106 /* Prepare RX descriptor */
107 memset(&descr, 0, sizeof(descr));
108 descr.read.buffer_addr = cpu_to_le64(data);
110 /* Put descriptor to the ring */
111 e1000e_rx_ring_push(d, &descr);
113 /* Wait for TX WB interrupt */
114 e1000e_wait_isr(d, E1000E_RX0_MSG_ID);
116 /* Check DD bit */
117 g_assert_cmphex(le32_to_cpu(descr.wb.upper.status_error) &
118 E1000_RXD_STAT_DD, ==, E1000_RXD_STAT_DD);
120 /* Check data sent to the backend */
121 memread(data, buffer, sizeof(buffer));
122 g_assert_cmpstr(buffer, == , test);
124 /* Free test data buffer */
125 guest_free(alloc, data);
128 static void test_e1000e_init(void *obj, void *data, QGuestAllocator * alloc)
130 /* init does nothing */
133 static void test_e1000e_tx(void *obj, void *data, QGuestAllocator * alloc)
135 QE1000E_PCI *e1000e = obj;
136 QE1000E *d = &e1000e->e1000e;
137 QOSGraphObject *e_object = obj;
138 QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
140 /* FIXME: add spapr support */
141 if (qpci_check_buggy_msi(dev)) {
142 return;
145 e1000e_send_verify(d, data, alloc);
148 static void test_e1000e_rx(void *obj, void *data, QGuestAllocator * alloc)
150 QE1000E_PCI *e1000e = obj;
151 QE1000E *d = &e1000e->e1000e;
152 QOSGraphObject *e_object = obj;
153 QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
155 /* FIXME: add spapr support */
156 if (qpci_check_buggy_msi(dev)) {
157 return;
160 e1000e_receive_verify(d, data, alloc);
163 static void test_e1000e_multiple_transfers(void *obj, void *data,
164 QGuestAllocator *alloc)
166 static const long iterations = 4 * 1024;
167 long i;
169 QE1000E_PCI *e1000e = obj;
170 QE1000E *d = &e1000e->e1000e;
171 QOSGraphObject *e_object = obj;
172 QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
174 /* FIXME: add spapr support */
175 if (qpci_check_buggy_msi(dev)) {
176 return;
179 for (i = 0; i < iterations; i++) {
180 e1000e_send_verify(d, data, alloc);
181 e1000e_receive_verify(d, data, alloc);
186 static void test_e1000e_hotplug(void *obj, void *data, QGuestAllocator * alloc)
188 QTestState *qts = global_qtest; /* TODO: get rid of global_qtest here */
189 QE1000E_PCI *dev = obj;
191 if (dev->pci_dev.bus->not_hotpluggable) {
192 g_test_skip("pci bus does not support hotplug");
193 return;
196 qtest_qmp_device_add(qts, "e1000e", "e1000e_net", "{'addr': '0x06'}");
197 qpci_unplug_acpi_device_test(qts, "e1000e_net", 0x06);
200 static void data_test_clear(void *sockets)
202 int *test_sockets = sockets;
204 close(test_sockets[0]);
205 qos_invalidate_command_line();
206 close(test_sockets[1]);
207 g_free(test_sockets);
210 static void *data_test_init(GString *cmd_line, void *arg)
212 int *test_sockets = g_new(int, 2);
213 int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, test_sockets);
214 g_assert_cmpint(ret, != , -1);
216 g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ",
217 test_sockets[1]);
219 g_test_queue_destroy(data_test_clear, test_sockets);
220 return test_sockets;
223 static void register_e1000e_test(void)
225 QOSGraphTestOptions opts = {
226 .before = data_test_init,
229 qos_add_test("init", "e1000e", test_e1000e_init, &opts);
230 qos_add_test("tx", "e1000e", test_e1000e_tx, &opts);
231 qos_add_test("rx", "e1000e", test_e1000e_rx, &opts);
232 qos_add_test("multiple_transfers", "e1000e",
233 test_e1000e_multiple_transfers, &opts);
234 qos_add_test("hotplug", "e1000e", test_e1000e_hotplug, &opts);
237 libqos_init(register_e1000e_test);