xref: /qemu/tests/qtest/e1000e-test.c (revision c3bef3b4)
1 /*
2  * QTest testcase for e1000e NIC
3  *
4  * Copyright (c) 2015 Ravello Systems LTD (http://ravellosystems.com)
5  * Developed by Daynix Computing LTD (http://www.daynix.com)
6  *
7  * Authors:
8  * Dmitry Fleytman <dmitry@daynix.com>
9  * Leonid Bloch <leonid@daynix.com>
10  * Yan Vugenfirer <yan@daynix.com>
11  *
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.
16  *
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.
21  *
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/>.
24  */
25 
26 
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"
37 
38 static void e1000e_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
39 {
40     static const char test[] = "TEST";
41     struct e1000_tx_desc descr;
42     char buffer[64];
43     int ret;
44     uint32_t recv_len;
45 
46     /* Prepare test data buffer */
47     uint64_t data = guest_alloc(alloc, sizeof(buffer));
48     memwrite(data, test, sizeof(test));
49 
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));
58 
59     /* Put descriptor to the ring */
60     e1000e_tx_ring_push(d, &descr);
61 
62     /* Wait for TX WB interrupt */
63     e1000e_wait_isr(d, E1000E_TX0_MSG_ID);
64 
65     /* Check DD bit */
66     g_assert_cmphex(le32_to_cpu(descr.upper.data) & E1000_TXD_STAT_DD, ==,
67                     E1000_TXD_STAT_DD);
68 
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);
75 
76     /* Free test data buffer */
77     guest_free(alloc, data);
78 }
79 
80 static void e1000e_receive_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
81 {
82     union e1000_rx_desc_extended descr;
83 
84     char test[] = "TEST";
85     int len = htonl(sizeof(test));
86     struct iovec iov[] = {
87         {
88             .iov_base = &len,
89             .iov_len = sizeof(len),
90         },{
91             .iov_base = test,
92             .iov_len = sizeof(test),
93         },
94     };
95 
96     char buffer[64];
97     int ret;
98 
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));
102 
103     /* Prepare test data buffer */
104     uint64_t data = guest_alloc(alloc, sizeof(buffer));
105 
106     /* Prepare RX descriptor */
107     memset(&descr, 0, sizeof(descr));
108     descr.read.buffer_addr = cpu_to_le64(data);
109 
110     /* Put descriptor to the ring */
111     e1000e_rx_ring_push(d, &descr);
112 
113     /* Wait for TX WB interrupt */
114     e1000e_wait_isr(d, E1000E_RX0_MSG_ID);
115 
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);
119 
120     /* Check data sent to the backend */
121     memread(data, buffer, sizeof(buffer));
122     g_assert_cmpstr(buffer, == , test);
123 
124     /* Free test data buffer */
125     guest_free(alloc, data);
126 }
127 
128 static void test_e1000e_init(void *obj, void *data, QGuestAllocator * alloc)
129 {
130     /* init does nothing */
131 }
132 
133 static void test_e1000e_tx(void *obj, void *data, QGuestAllocator * alloc)
134 {
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");
139 
140     /* FIXME: add spapr support */
141     if (qpci_check_buggy_msi(dev)) {
142         return;
143     }
144 
145     e1000e_send_verify(d, data, alloc);
146 }
147 
148 static void test_e1000e_rx(void *obj, void *data, QGuestAllocator * alloc)
149 {
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");
154 
155     /* FIXME: add spapr support */
156     if (qpci_check_buggy_msi(dev)) {
157         return;
158     }
159 
160     e1000e_receive_verify(d, data, alloc);
161 }
162 
163 static void test_e1000e_multiple_transfers(void *obj, void *data,
164                                            QGuestAllocator *alloc)
165 {
166     static const long iterations = 4 * 1024;
167     long i;
168 
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");
173 
174     /* FIXME: add spapr support */
175     if (qpci_check_buggy_msi(dev)) {
176         return;
177     }
178 
179     for (i = 0; i < iterations; i++) {
180         e1000e_send_verify(d, data, alloc);
181         e1000e_receive_verify(d, data, alloc);
182     }
183 
184 }
185 
186 static void test_e1000e_hotplug(void *obj, void *data, QGuestAllocator * alloc)
187 {
188     QTestState *qts = global_qtest;  /* TODO: get rid of global_qtest here */
189     QE1000E_PCI *dev = obj;
190 
191     if (dev->pci_dev.bus->not_hotpluggable) {
192         g_test_skip("pci bus does not support hotplug");
193         return;
194     }
195 
196     qtest_qmp_device_add(qts, "e1000e", "e1000e_net", "{'addr': '0x06'}");
197     qpci_unplug_acpi_device_test(qts, "e1000e_net", 0x06);
198 }
199 
200 static void data_test_clear(void *sockets)
201 {
202     int *test_sockets = sockets;
203 
204     close(test_sockets[0]);
205     qos_invalidate_command_line();
206     close(test_sockets[1]);
207     g_free(test_sockets);
208 }
209 
210 static void *data_test_init(GString *cmd_line, void *arg)
211 {
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);
215 
216     g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ",
217                            test_sockets[1]);
218 
219     g_test_queue_destroy(data_test_clear, test_sockets);
220     return test_sockets;
221 }
222 
223 static void register_e1000e_test(void)
224 {
225     QOSGraphTestOptions opts = {
226         .before = data_test_init,
227     };
228 
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);
235 }
236 
237 libqos_init(register_e1000e_test);
238