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