root / hw / xen_nic.c @ e3f5ec2b
History | View | Annotate | Download (11.9 kB)
1 |
/*
|
---|---|
2 |
* xen paravirt network card backend
|
3 |
*
|
4 |
* (c) Gerd Hoffmann <kraxel@redhat.com>
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation; under version 2 of the License.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License along
|
16 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
18 |
*/
|
19 |
|
20 |
#include <stdio.h> |
21 |
#include <stdlib.h> |
22 |
#include <stdarg.h> |
23 |
#include <string.h> |
24 |
#include <unistd.h> |
25 |
#include <signal.h> |
26 |
#include <inttypes.h> |
27 |
#include <fcntl.h> |
28 |
#include <errno.h> |
29 |
#include <pthread.h> |
30 |
#include <sys/socket.h> |
31 |
#include <sys/ioctl.h> |
32 |
#include <sys/types.h> |
33 |
#include <sys/stat.h> |
34 |
#include <sys/mman.h> |
35 |
#include <sys/wait.h> |
36 |
|
37 |
#include <xs.h> |
38 |
#include <xenctrl.h> |
39 |
#include <xen/io/xenbus.h> |
40 |
#include <xen/io/netif.h> |
41 |
|
42 |
#include "hw.h" |
43 |
#include "net.h" |
44 |
#include "qemu-char.h" |
45 |
#include "xen_backend.h" |
46 |
|
47 |
/* ------------------------------------------------------------- */
|
48 |
|
49 |
struct XenNetDev {
|
50 |
struct XenDevice xendev; /* must be first */ |
51 |
char *mac;
|
52 |
int tx_work;
|
53 |
int tx_ring_ref;
|
54 |
int rx_ring_ref;
|
55 |
struct netif_tx_sring *txs;
|
56 |
struct netif_rx_sring *rxs;
|
57 |
netif_tx_back_ring_t tx_ring; |
58 |
netif_rx_back_ring_t rx_ring; |
59 |
VLANClientState *vs; |
60 |
}; |
61 |
|
62 |
/* ------------------------------------------------------------- */
|
63 |
|
64 |
static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st) |
65 |
{ |
66 |
RING_IDX i = netdev->tx_ring.rsp_prod_pvt; |
67 |
netif_tx_response_t *resp; |
68 |
int notify;
|
69 |
|
70 |
resp = RING_GET_RESPONSE(&netdev->tx_ring, i); |
71 |
resp->id = txp->id; |
72 |
resp->status = st; |
73 |
|
74 |
#if 0
|
75 |
if (txp->flags & NETTXF_extra_info)
|
76 |
RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
|
77 |
#endif
|
78 |
|
79 |
netdev->tx_ring.rsp_prod_pvt = ++i; |
80 |
RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify); |
81 |
if (notify)
|
82 |
xen_be_send_notify(&netdev->xendev); |
83 |
|
84 |
if (i == netdev->tx_ring.req_cons) {
|
85 |
int more_to_do;
|
86 |
RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do); |
87 |
if (more_to_do)
|
88 |
netdev->tx_work++; |
89 |
} |
90 |
} |
91 |
|
92 |
static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end) |
93 |
{ |
94 |
#if 0
|
95 |
/*
|
96 |
* Hmm, why netback fails everything in the ring?
|
97 |
* Should we do that even when not supporting SG and TSO?
|
98 |
*/
|
99 |
RING_IDX cons = netdev->tx_ring.req_cons;
|
100 |
|
101 |
do {
|
102 |
make_tx_response(netif, txp, NETIF_RSP_ERROR);
|
103 |
if (cons >= end)
|
104 |
break;
|
105 |
txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
|
106 |
} while (1);
|
107 |
netdev->tx_ring.req_cons = cons;
|
108 |
netif_schedule_work(netif);
|
109 |
netif_put(netif);
|
110 |
#else
|
111 |
net_tx_response(netdev, txp, NETIF_RSP_ERROR); |
112 |
#endif
|
113 |
} |
114 |
|
115 |
static void net_tx_packets(struct XenNetDev *netdev) |
116 |
{ |
117 |
netif_tx_request_t txreq; |
118 |
RING_IDX rc, rp; |
119 |
void *page;
|
120 |
void *tmpbuf = NULL; |
121 |
|
122 |
for (;;) {
|
123 |
rc = netdev->tx_ring.req_cons; |
124 |
rp = netdev->tx_ring.sring->req_prod; |
125 |
xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
126 |
|
127 |
while ((rc != rp)) {
|
128 |
if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc))
|
129 |
break;
|
130 |
memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
|
131 |
netdev->tx_ring.req_cons = ++rc; |
132 |
|
133 |
#if 1 |
134 |
/* should not happen in theory, we don't announce the *
|
135 |
* feature-{sg,gso,whatelse} flags in xenstore (yet?) */
|
136 |
if (txreq.flags & NETTXF_extra_info) {
|
137 |
xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n"); |
138 |
net_tx_error(netdev, &txreq, rc); |
139 |
continue;
|
140 |
} |
141 |
if (txreq.flags & NETTXF_more_data) {
|
142 |
xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n"); |
143 |
net_tx_error(netdev, &txreq, rc); |
144 |
continue;
|
145 |
} |
146 |
#endif
|
147 |
|
148 |
if (txreq.size < 14) { |
149 |
xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size); |
150 |
net_tx_error(netdev, &txreq, rc); |
151 |
continue;
|
152 |
} |
153 |
|
154 |
if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
|
155 |
xen_be_printf(&netdev->xendev, 0, "error: page crossing\n"); |
156 |
net_tx_error(netdev, &txreq, rc); |
157 |
continue;
|
158 |
} |
159 |
|
160 |
xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n", |
161 |
txreq.gref, txreq.offset, txreq.size, txreq.flags, |
162 |
(txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "", |
163 |
(txreq.flags & NETTXF_data_validated) ? " data_validated" : "", |
164 |
(txreq.flags & NETTXF_more_data) ? " more_data" : "", |
165 |
(txreq.flags & NETTXF_extra_info) ? " extra_info" : ""); |
166 |
|
167 |
page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
168 |
netdev->xendev.dom, |
169 |
txreq.gref, PROT_READ); |
170 |
if (page == NULL) { |
171 |
xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n", |
172 |
txreq.gref); |
173 |
net_tx_error(netdev, &txreq, rc); |
174 |
continue;
|
175 |
} |
176 |
if (txreq.flags & NETTXF_csum_blank) {
|
177 |
/* have read-only mapping -> can't fill checksum in-place */
|
178 |
if (!tmpbuf)
|
179 |
tmpbuf = qemu_malloc(XC_PAGE_SIZE); |
180 |
memcpy(tmpbuf, page + txreq.offset, txreq.size); |
181 |
net_checksum_calculate(tmpbuf, txreq.size); |
182 |
qemu_send_packet(netdev->vs, tmpbuf, txreq.size); |
183 |
} else {
|
184 |
qemu_send_packet(netdev->vs, page + txreq.offset, txreq.size); |
185 |
} |
186 |
xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
|
187 |
net_tx_response(netdev, &txreq, NETIF_RSP_OKAY); |
188 |
} |
189 |
if (!netdev->tx_work)
|
190 |
break;
|
191 |
netdev->tx_work = 0;
|
192 |
} |
193 |
qemu_free(tmpbuf); |
194 |
} |
195 |
|
196 |
/* ------------------------------------------------------------- */
|
197 |
|
198 |
static void net_rx_response(struct XenNetDev *netdev, |
199 |
netif_rx_request_t *req, int8_t st, |
200 |
uint16_t offset, uint16_t size, |
201 |
uint16_t flags) |
202 |
{ |
203 |
RING_IDX i = netdev->rx_ring.rsp_prod_pvt; |
204 |
netif_rx_response_t *resp; |
205 |
int notify;
|
206 |
|
207 |
resp = RING_GET_RESPONSE(&netdev->rx_ring, i); |
208 |
resp->offset = offset; |
209 |
resp->flags = flags; |
210 |
resp->id = req->id; |
211 |
resp->status = (int16_t)size; |
212 |
if (st < 0) |
213 |
resp->status = (int16_t)st; |
214 |
|
215 |
xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n", |
216 |
i, resp->status, resp->flags); |
217 |
|
218 |
netdev->rx_ring.rsp_prod_pvt = ++i; |
219 |
RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify); |
220 |
if (notify)
|
221 |
xen_be_send_notify(&netdev->xendev); |
222 |
} |
223 |
|
224 |
#define NET_IP_ALIGN 2 |
225 |
|
226 |
static int net_rx_ok(VLANClientState *vc) |
227 |
{ |
228 |
struct XenNetDev *netdev = vc->opaque;
|
229 |
RING_IDX rc, rp; |
230 |
|
231 |
if (netdev->xendev.be_state != XenbusStateConnected)
|
232 |
return 0; |
233 |
|
234 |
rc = netdev->rx_ring.req_cons; |
235 |
rp = netdev->rx_ring.sring->req_prod; |
236 |
xen_rmb(); |
237 |
|
238 |
if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
|
239 |
xen_be_printf(&netdev->xendev, 2, "%s: no rx buffers (%d/%d)\n", |
240 |
__FUNCTION__, rc, rp); |
241 |
return 0; |
242 |
} |
243 |
return 1; |
244 |
} |
245 |
|
246 |
static void net_rx_packet(VLANClientState *vc, const uint8_t *buf, size_t size) |
247 |
{ |
248 |
struct XenNetDev *netdev = vc->opaque;
|
249 |
netif_rx_request_t rxreq; |
250 |
RING_IDX rc, rp; |
251 |
void *page;
|
252 |
|
253 |
if (netdev->xendev.be_state != XenbusStateConnected)
|
254 |
return;
|
255 |
|
256 |
rc = netdev->rx_ring.req_cons; |
257 |
rp = netdev->rx_ring.sring->req_prod; |
258 |
xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
259 |
|
260 |
if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
|
261 |
xen_be_printf(&netdev->xendev, 2, "no buffer, drop packet\n"); |
262 |
return;
|
263 |
} |
264 |
if (size > XC_PAGE_SIZE - NET_IP_ALIGN) {
|
265 |
xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)", |
266 |
(unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN); |
267 |
return;
|
268 |
} |
269 |
|
270 |
memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
|
271 |
netdev->rx_ring.req_cons = ++rc; |
272 |
|
273 |
page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
274 |
netdev->xendev.dom, |
275 |
rxreq.gref, PROT_WRITE); |
276 |
if (page == NULL) { |
277 |
xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n", |
278 |
rxreq.gref); |
279 |
net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0); |
280 |
return;
|
281 |
} |
282 |
memcpy(page + NET_IP_ALIGN, buf, size); |
283 |
xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
|
284 |
net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
|
285 |
} |
286 |
|
287 |
/* ------------------------------------------------------------- */
|
288 |
|
289 |
static int net_init(struct XenDevice *xendev) |
290 |
{ |
291 |
struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
292 |
VLANState *vlan; |
293 |
|
294 |
/* read xenstore entries */
|
295 |
if (netdev->mac == NULL) |
296 |
netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac");
|
297 |
|
298 |
/* do we have all we need? */
|
299 |
if (netdev->mac == NULL) |
300 |
return -1; |
301 |
|
302 |
vlan = qemu_find_vlan(netdev->xendev.dev); |
303 |
netdev->vs = qemu_new_vlan_client(vlan, "xen", NULL, |
304 |
net_rx_ok, net_rx_packet, NULL,
|
305 |
NULL, netdev);
|
306 |
snprintf(netdev->vs->info_str, sizeof(netdev->vs->info_str),
|
307 |
"nic: xenbus vif macaddr=%s", netdev->mac);
|
308 |
|
309 |
/* fill info */
|
310 |
xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); |
311 |
xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0); |
312 |
|
313 |
return 0; |
314 |
} |
315 |
|
316 |
static int net_connect(struct XenDevice *xendev) |
317 |
{ |
318 |
struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
319 |
int rx_copy;
|
320 |
|
321 |
if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref", |
322 |
&netdev->tx_ring_ref) == -1)
|
323 |
return -1; |
324 |
if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref", |
325 |
&netdev->rx_ring_ref) == -1)
|
326 |
return 1; |
327 |
if (xenstore_read_fe_int(&netdev->xendev, "event-channel", |
328 |
&netdev->xendev.remote_port) == -1)
|
329 |
return -1; |
330 |
|
331 |
if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) |
332 |
rx_copy = 0;
|
333 |
if (rx_copy == 0) { |
334 |
xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n"); |
335 |
return -1; |
336 |
} |
337 |
|
338 |
netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
339 |
netdev->xendev.dom, |
340 |
netdev->tx_ring_ref, |
341 |
PROT_READ | PROT_WRITE); |
342 |
netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
343 |
netdev->xendev.dom, |
344 |
netdev->rx_ring_ref, |
345 |
PROT_READ | PROT_WRITE); |
346 |
if (!netdev->txs || !netdev->rxs)
|
347 |
return -1; |
348 |
BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE); |
349 |
BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE); |
350 |
|
351 |
xen_be_bind_evtchn(&netdev->xendev); |
352 |
|
353 |
xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, " |
354 |
"remote port %d, local port %d\n",
|
355 |
netdev->tx_ring_ref, netdev->rx_ring_ref, |
356 |
netdev->xendev.remote_port, netdev->xendev.local_port); |
357 |
return 0; |
358 |
} |
359 |
|
360 |
static void net_disconnect(struct XenDevice *xendev) |
361 |
{ |
362 |
struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
363 |
|
364 |
xen_be_unbind_evtchn(&netdev->xendev); |
365 |
|
366 |
if (netdev->txs) {
|
367 |
xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
|
368 |
netdev->txs = NULL;
|
369 |
} |
370 |
if (netdev->rxs) {
|
371 |
xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
|
372 |
netdev->rxs = NULL;
|
373 |
} |
374 |
if (netdev->vs) {
|
375 |
qemu_del_vlan_client(netdev->vs); |
376 |
netdev->vs = NULL;
|
377 |
} |
378 |
} |
379 |
|
380 |
static void net_event(struct XenDevice *xendev) |
381 |
{ |
382 |
struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
383 |
net_tx_packets(netdev); |
384 |
} |
385 |
|
386 |
static int net_free(struct XenDevice *xendev) |
387 |
{ |
388 |
struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
389 |
|
390 |
qemu_free(netdev->mac); |
391 |
return 0; |
392 |
} |
393 |
|
394 |
/* ------------------------------------------------------------- */
|
395 |
|
396 |
struct XenDevOps xen_netdev_ops = {
|
397 |
.size = sizeof(struct XenNetDev), |
398 |
.flags = DEVOPS_FLAG_NEED_GNTDEV, |
399 |
.init = net_init, |
400 |
.connect = net_connect, |
401 |
.event = net_event, |
402 |
.disconnect = net_disconnect, |
403 |
.free = net_free, |
404 |
}; |