xref: /qemu/tests/qtest/e1000e-test.c (revision b2a3cbb8)
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     struct e1000_tx_desc descr;
41     static const int data_len = 64;
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, data_len);
48     memwrite(data, "TEST", 5);
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                                    data_len);
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, 64, 0);
73     g_assert_cmpint(ret, >=, 5);
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     static const int data_len = 64;
97     char buffer[64];
98     int ret;
99 
100     /* Send a dummy packet to device's socket*/
101     ret = iov_send(test_sockets[0], iov, 2, 0, sizeof(len) + sizeof(test));
102     g_assert_cmpint(ret, == , sizeof(test) + sizeof(len));
103 
104     /* Prepare test data buffer */
105     uint64_t data = guest_alloc(alloc, data_len);
106 
107     /* Prepare RX descriptor */
108     memset(&descr, 0, sizeof(descr));
109     descr.read.buffer_addr = cpu_to_le64(data);
110 
111     /* Put descriptor to the ring */
112     e1000e_rx_ring_push(d, &descr);
113 
114     /* Wait for TX WB interrupt */
115     e1000e_wait_isr(d, E1000E_RX0_MSG_ID);
116 
117     /* Check DD bit */
118     g_assert_cmphex(le32_to_cpu(descr.wb.upper.status_error) &
119         E1000_RXD_STAT_DD, ==, E1000_RXD_STAT_DD);
120 
121     /* Check data sent to the backend */
122     memread(data, buffer, sizeof(buffer));
123     g_assert_cmpstr(buffer, == , "TEST");
124 
125     /* Free test data buffer */
126     guest_free(alloc, data);
127 }
128 
129 static void test_e1000e_init(void *obj, void *data, QGuestAllocator * alloc)
130 {
131     /* init does nothing */
132 }
133 
134 static void test_e1000e_tx(void *obj, void *data, QGuestAllocator * alloc)
135 {
136     QE1000E_PCI *e1000e = obj;
137     QE1000E *d = &e1000e->e1000e;
138     QOSGraphObject *e_object = obj;
139     QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
140 
141     /* FIXME: add spapr support */
142     if (qpci_check_buggy_msi(dev)) {
143         return;
144     }
145 
146     e1000e_send_verify(d, data, alloc);
147 }
148 
149 static void test_e1000e_rx(void *obj, void *data, QGuestAllocator * alloc)
150 {
151     QE1000E_PCI *e1000e = obj;
152     QE1000E *d = &e1000e->e1000e;
153     QOSGraphObject *e_object = obj;
154     QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
155 
156     /* FIXME: add spapr support */
157     if (qpci_check_buggy_msi(dev)) {
158         return;
159     }
160 
161     e1000e_receive_verify(d, data, alloc);
162 }
163 
164 static void test_e1000e_multiple_transfers(void *obj, void *data,
165                                            QGuestAllocator *alloc)
166 {
167     static const long iterations = 4 * 1024;
168     long i;
169 
170     QE1000E_PCI *e1000e = obj;
171     QE1000E *d = &e1000e->e1000e;
172     QOSGraphObject *e_object = obj;
173     QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
174 
175     /* FIXME: add spapr support */
176     if (qpci_check_buggy_msi(dev)) {
177         return;
178     }
179 
180     for (i = 0; i < iterations; i++) {
181         e1000e_send_verify(d, data, alloc);
182         e1000e_receive_verify(d, data, alloc);
183     }
184 
185 }
186 
187 static void test_e1000e_hotplug(void *obj, void *data, QGuestAllocator * alloc)
188 {
189     QTestState *qts = global_qtest;  /* TODO: get rid of global_qtest here */
190     QE1000E_PCI *dev = obj;
191 
192     if (dev->pci_dev.bus->not_hotpluggable) {
193         g_test_skip("pci bus does not support hotplug");
194         return;
195     }
196 
197     qtest_qmp_device_add(qts, "e1000e", "e1000e_net", "{'addr': '0x06'}");
198     qpci_unplug_acpi_device_test(qts, "e1000e_net", 0x06);
199 }
200 
201 static void data_test_clear(void *sockets)
202 {
203     int *test_sockets = sockets;
204 
205     close(test_sockets[0]);
206     qos_invalidate_command_line();
207     close(test_sockets[1]);
208     g_free(test_sockets);
209 }
210 
211 static void *data_test_init(GString *cmd_line, void *arg)
212 {
213     int *test_sockets = g_new(int, 2);
214     int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, test_sockets);
215     g_assert_cmpint(ret, != , -1);
216 
217     g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ",
218                            test_sockets[1]);
219 
220     g_test_queue_destroy(data_test_clear, test_sockets);
221     return test_sockets;
222 }
223 
224 static void register_e1000e_test(void)
225 {
226     QOSGraphTestOptions opts = {
227         .before = data_test_init,
228     };
229 
230     qos_add_test("init", "e1000e", test_e1000e_init, &opts);
231     qos_add_test("tx", "e1000e", test_e1000e_tx, &opts);
232     qos_add_test("rx", "e1000e", test_e1000e_rx, &opts);
233     qos_add_test("multiple_transfers", "e1000e",
234                       test_e1000e_multiple_transfers, &opts);
235     qos_add_test("hotplug", "e1000e", test_e1000e_hotplug, &opts);
236 }
237 
238 libqos_init(register_e1000e_test);
239