]>
Commit | Line | Data |
---|---|---|
e613b064 AL |
1 | /* |
2 | * xen paravirt network card backend | |
3 | * | |
4 | * (c) Gerd Hoffmann <[email protected]> | |
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 | |
8167ee88 | 16 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
6b620ca3 PB |
17 | * |
18 | * Contributions after 2012-01-13 are licensed under the terms of the | |
19 | * GNU GPL, version 2 or (at your option) any later version. | |
e613b064 AL |
20 | */ |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <stdarg.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <signal.h> | |
28 | #include <inttypes.h> | |
29 | #include <fcntl.h> | |
30 | #include <errno.h> | |
e613b064 AL |
31 | #include <sys/socket.h> |
32 | #include <sys/ioctl.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/mman.h> | |
36 | #include <sys/wait.h> | |
e613b064 AL |
37 | |
38 | #include <xs.h> | |
39 | #include <xenctrl.h> | |
40 | #include <xen/io/xenbus.h> | |
41 | #include <xen/io/netif.h> | |
42 | ||
43 | #include "hw.h" | |
44 | #include "net.h" | |
7200ac3c | 45 | #include "net/checksum.h" |
658788c5 | 46 | #include "net/util.h" |
e613b064 AL |
47 | #include "qemu-char.h" |
48 | #include "xen_backend.h" | |
49 | ||
50 | /* ------------------------------------------------------------- */ | |
51 | ||
52 | struct XenNetDev { | |
53 | struct XenDevice xendev; /* must be first */ | |
54 | char *mac; | |
55 | int tx_work; | |
56 | int tx_ring_ref; | |
57 | int rx_ring_ref; | |
58 | struct netif_tx_sring *txs; | |
59 | struct netif_rx_sring *rxs; | |
60 | netif_tx_back_ring_t tx_ring; | |
61 | netif_rx_back_ring_t rx_ring; | |
658788c5 MM |
62 | NICConf conf; |
63 | NICState *nic; | |
e613b064 AL |
64 | }; |
65 | ||
66 | /* ------------------------------------------------------------- */ | |
67 | ||
68 | static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st) | |
69 | { | |
70 | RING_IDX i = netdev->tx_ring.rsp_prod_pvt; | |
71 | netif_tx_response_t *resp; | |
72 | int notify; | |
73 | ||
74 | resp = RING_GET_RESPONSE(&netdev->tx_ring, i); | |
75 | resp->id = txp->id; | |
76 | resp->status = st; | |
77 | ||
78 | #if 0 | |
209cd7ab AP |
79 | if (txp->flags & NETTXF_extra_info) { |
80 | RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL; | |
81 | } | |
e613b064 AL |
82 | #endif |
83 | ||
84 | netdev->tx_ring.rsp_prod_pvt = ++i; | |
85 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify); | |
209cd7ab AP |
86 | if (notify) { |
87 | xen_be_send_notify(&netdev->xendev); | |
88 | } | |
e613b064 AL |
89 | |
90 | if (i == netdev->tx_ring.req_cons) { | |
209cd7ab AP |
91 | int more_to_do; |
92 | RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do); | |
93 | if (more_to_do) { | |
94 | netdev->tx_work++; | |
95 | } | |
e613b064 AL |
96 | } |
97 | } | |
98 | ||
99 | static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end) | |
100 | { | |
101 | #if 0 | |
102 | /* | |
103 | * Hmm, why netback fails everything in the ring? | |
104 | * Should we do that even when not supporting SG and TSO? | |
105 | */ | |
106 | RING_IDX cons = netdev->tx_ring.req_cons; | |
107 | ||
108 | do { | |
209cd7ab AP |
109 | make_tx_response(netif, txp, NETIF_RSP_ERROR); |
110 | if (cons >= end) { | |
111 | break; | |
112 | } | |
113 | txp = RING_GET_REQUEST(&netdev->tx_ring, cons++); | |
e613b064 AL |
114 | } while (1); |
115 | netdev->tx_ring.req_cons = cons; | |
116 | netif_schedule_work(netif); | |
117 | netif_put(netif); | |
118 | #else | |
119 | net_tx_response(netdev, txp, NETIF_RSP_ERROR); | |
120 | #endif | |
121 | } | |
122 | ||
123 | static void net_tx_packets(struct XenNetDev *netdev) | |
124 | { | |
125 | netif_tx_request_t txreq; | |
126 | RING_IDX rc, rp; | |
127 | void *page; | |
128 | void *tmpbuf = NULL; | |
129 | ||
130 | for (;;) { | |
209cd7ab AP |
131 | rc = netdev->tx_ring.req_cons; |
132 | rp = netdev->tx_ring.sring->req_prod; | |
133 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ | |
e613b064 | 134 | |
209cd7ab AP |
135 | while ((rc != rp)) { |
136 | if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) { | |
137 | break; | |
138 | } | |
139 | memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq)); | |
140 | netdev->tx_ring.req_cons = ++rc; | |
e613b064 AL |
141 | |
142 | #if 1 | |
209cd7ab AP |
143 | /* should not happen in theory, we don't announce the * |
144 | * feature-{sg,gso,whatelse} flags in xenstore (yet?) */ | |
145 | if (txreq.flags & NETTXF_extra_info) { | |
146 | xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n"); | |
147 | net_tx_error(netdev, &txreq, rc); | |
148 | continue; | |
149 | } | |
150 | if (txreq.flags & NETTXF_more_data) { | |
151 | xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n"); | |
152 | net_tx_error(netdev, &txreq, rc); | |
153 | continue; | |
154 | } | |
e613b064 AL |
155 | #endif |
156 | ||
209cd7ab AP |
157 | if (txreq.size < 14) { |
158 | xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size); | |
159 | net_tx_error(netdev, &txreq, rc); | |
160 | continue; | |
161 | } | |
162 | ||
163 | if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) { | |
164 | xen_be_printf(&netdev->xendev, 0, "error: page crossing\n"); | |
165 | net_tx_error(netdev, &txreq, rc); | |
166 | continue; | |
167 | } | |
168 | ||
169 | xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n", | |
170 | txreq.gref, txreq.offset, txreq.size, txreq.flags, | |
171 | (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "", | |
172 | (txreq.flags & NETTXF_data_validated) ? " data_validated" : "", | |
173 | (txreq.flags & NETTXF_more_data) ? " more_data" : "", | |
174 | (txreq.flags & NETTXF_extra_info) ? " extra_info" : ""); | |
175 | ||
176 | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, | |
177 | netdev->xendev.dom, | |
178 | txreq.gref, PROT_READ); | |
179 | if (page == NULL) { | |
180 | xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n", | |
e613b064 | 181 | txreq.gref); |
209cd7ab AP |
182 | net_tx_error(netdev, &txreq, rc); |
183 | continue; | |
184 | } | |
185 | if (txreq.flags & NETTXF_csum_blank) { | |
e613b064 | 186 | /* have read-only mapping -> can't fill checksum in-place */ |
209cd7ab | 187 | if (!tmpbuf) { |
7267c094 | 188 | tmpbuf = g_malloc(XC_PAGE_SIZE); |
209cd7ab | 189 | } |
e613b064 | 190 | memcpy(tmpbuf, page + txreq.offset, txreq.size); |
209cd7ab | 191 | net_checksum_calculate(tmpbuf, txreq.size); |
658788c5 | 192 | qemu_send_packet(&netdev->nic->nc, tmpbuf, txreq.size); |
e613b064 | 193 | } else { |
658788c5 | 194 | qemu_send_packet(&netdev->nic->nc, page + txreq.offset, txreq.size); |
e613b064 | 195 | } |
209cd7ab AP |
196 | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1); |
197 | net_tx_response(netdev, &txreq, NETIF_RSP_OKAY); | |
198 | } | |
199 | if (!netdev->tx_work) { | |
200 | break; | |
201 | } | |
202 | netdev->tx_work = 0; | |
e613b064 | 203 | } |
7267c094 | 204 | g_free(tmpbuf); |
e613b064 AL |
205 | } |
206 | ||
207 | /* ------------------------------------------------------------- */ | |
208 | ||
209 | static void net_rx_response(struct XenNetDev *netdev, | |
209cd7ab AP |
210 | netif_rx_request_t *req, int8_t st, |
211 | uint16_t offset, uint16_t size, | |
212 | uint16_t flags) | |
e613b064 AL |
213 | { |
214 | RING_IDX i = netdev->rx_ring.rsp_prod_pvt; | |
215 | netif_rx_response_t *resp; | |
216 | int notify; | |
217 | ||
218 | resp = RING_GET_RESPONSE(&netdev->rx_ring, i); | |
219 | resp->offset = offset; | |
220 | resp->flags = flags; | |
221 | resp->id = req->id; | |
222 | resp->status = (int16_t)size; | |
209cd7ab AP |
223 | if (st < 0) { |
224 | resp->status = (int16_t)st; | |
225 | } | |
e613b064 AL |
226 | |
227 | xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n", | |
209cd7ab | 228 | i, resp->status, resp->flags); |
e613b064 AL |
229 | |
230 | netdev->rx_ring.rsp_prod_pvt = ++i; | |
231 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify); | |
209cd7ab AP |
232 | if (notify) { |
233 | xen_be_send_notify(&netdev->xendev); | |
234 | } | |
e613b064 AL |
235 | } |
236 | ||
237 | #define NET_IP_ALIGN 2 | |
238 | ||
658788c5 | 239 | static int net_rx_ok(VLANClientState *nc) |
e613b064 | 240 | { |
658788c5 | 241 | struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque; |
e613b064 AL |
242 | RING_IDX rc, rp; |
243 | ||
209cd7ab AP |
244 | if (netdev->xendev.be_state != XenbusStateConnected) { |
245 | return 0; | |
246 | } | |
e613b064 AL |
247 | |
248 | rc = netdev->rx_ring.req_cons; | |
249 | rp = netdev->rx_ring.sring->req_prod; | |
250 | xen_rmb(); | |
251 | ||
252 | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) { | |
209cd7ab AP |
253 | xen_be_printf(&netdev->xendev, 2, "%s: no rx buffers (%d/%d)\n", |
254 | __FUNCTION__, rc, rp); | |
255 | return 0; | |
e613b064 AL |
256 | } |
257 | return 1; | |
258 | } | |
259 | ||
658788c5 | 260 | static ssize_t net_rx_packet(VLANClientState *nc, const uint8_t *buf, size_t size) |
e613b064 | 261 | { |
658788c5 | 262 | struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque; |
e613b064 AL |
263 | netif_rx_request_t rxreq; |
264 | RING_IDX rc, rp; | |
265 | void *page; | |
266 | ||
209cd7ab AP |
267 | if (netdev->xendev.be_state != XenbusStateConnected) { |
268 | return -1; | |
269 | } | |
e613b064 AL |
270 | |
271 | rc = netdev->rx_ring.req_cons; | |
272 | rp = netdev->rx_ring.sring->req_prod; | |
273 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ | |
274 | ||
275 | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) { | |
209cd7ab AP |
276 | xen_be_printf(&netdev->xendev, 2, "no buffer, drop packet\n"); |
277 | return -1; | |
e613b064 AL |
278 | } |
279 | if (size > XC_PAGE_SIZE - NET_IP_ALIGN) { | |
209cd7ab AP |
280 | xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)", |
281 | (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN); | |
282 | return -1; | |
e613b064 AL |
283 | } |
284 | ||
285 | memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq)); | |
286 | netdev->rx_ring.req_cons = ++rc; | |
287 | ||
288 | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, | |
209cd7ab AP |
289 | netdev->xendev.dom, |
290 | rxreq.gref, PROT_WRITE); | |
e613b064 | 291 | if (page == NULL) { |
209cd7ab | 292 | xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n", |
e613b064 | 293 | rxreq.gref); |
209cd7ab AP |
294 | net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0); |
295 | return -1; | |
e613b064 AL |
296 | } |
297 | memcpy(page + NET_IP_ALIGN, buf, size); | |
298 | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1); | |
299 | net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0); | |
4f1c942b MM |
300 | |
301 | return size; | |
e613b064 AL |
302 | } |
303 | ||
304 | /* ------------------------------------------------------------- */ | |
305 | ||
658788c5 MM |
306 | static NetClientInfo net_xen_info = { |
307 | .type = NET_CLIENT_TYPE_NIC, | |
308 | .size = sizeof(NICState), | |
309 | .can_receive = net_rx_ok, | |
310 | .receive = net_rx_packet, | |
311 | }; | |
312 | ||
e613b064 AL |
313 | static int net_init(struct XenDevice *xendev) |
314 | { | |
315 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
e613b064 AL |
316 | |
317 | /* read xenstore entries */ | |
209cd7ab AP |
318 | if (netdev->mac == NULL) { |
319 | netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac"); | |
320 | } | |
e613b064 AL |
321 | |
322 | /* do we have all we need? */ | |
209cd7ab AP |
323 | if (netdev->mac == NULL) { |
324 | return -1; | |
325 | } | |
e613b064 | 326 | |
209cd7ab | 327 | if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) { |
658788c5 | 328 | return -1; |
209cd7ab | 329 | } |
658788c5 MM |
330 | |
331 | netdev->conf.vlan = qemu_find_vlan(netdev->xendev.dev, 1); | |
332 | netdev->conf.peer = NULL; | |
333 | ||
334 | netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf, | |
335 | "xen", NULL, netdev); | |
336 | ||
337 | snprintf(netdev->nic->nc.info_str, sizeof(netdev->nic->nc.info_str), | |
e613b064 AL |
338 | "nic: xenbus vif macaddr=%s", netdev->mac); |
339 | ||
340 | /* fill info */ | |
341 | xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); | |
342 | xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0); | |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
347 | static int net_connect(struct XenDevice *xendev) | |
348 | { | |
349 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
350 | int rx_copy; | |
351 | ||
352 | if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref", | |
209cd7ab AP |
353 | &netdev->tx_ring_ref) == -1) { |
354 | return -1; | |
355 | } | |
e613b064 | 356 | if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref", |
209cd7ab AP |
357 | &netdev->rx_ring_ref) == -1) { |
358 | return 1; | |
359 | } | |
e613b064 | 360 | if (xenstore_read_fe_int(&netdev->xendev, "event-channel", |
209cd7ab AP |
361 | &netdev->xendev.remote_port) == -1) { |
362 | return -1; | |
363 | } | |
e613b064 | 364 | |
209cd7ab AP |
365 | if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) { |
366 | rx_copy = 0; | |
367 | } | |
e613b064 | 368 | if (rx_copy == 0) { |
209cd7ab AP |
369 | xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n"); |
370 | return -1; | |
e613b064 AL |
371 | } |
372 | ||
373 | netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, | |
209cd7ab AP |
374 | netdev->xendev.dom, |
375 | netdev->tx_ring_ref, | |
376 | PROT_READ | PROT_WRITE); | |
e613b064 | 377 | netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
209cd7ab AP |
378 | netdev->xendev.dom, |
379 | netdev->rx_ring_ref, | |
380 | PROT_READ | PROT_WRITE); | |
381 | if (!netdev->txs || !netdev->rxs) { | |
382 | return -1; | |
383 | } | |
e613b064 AL |
384 | BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE); |
385 | BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE); | |
386 | ||
387 | xen_be_bind_evtchn(&netdev->xendev); | |
388 | ||
389 | xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, " | |
209cd7ab AP |
390 | "remote port %d, local port %d\n", |
391 | netdev->tx_ring_ref, netdev->rx_ring_ref, | |
392 | netdev->xendev.remote_port, netdev->xendev.local_port); | |
3e3cabcf GH |
393 | |
394 | net_tx_packets(netdev); | |
e613b064 AL |
395 | return 0; |
396 | } | |
397 | ||
398 | static void net_disconnect(struct XenDevice *xendev) | |
399 | { | |
400 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
401 | ||
402 | xen_be_unbind_evtchn(&netdev->xendev); | |
403 | ||
404 | if (netdev->txs) { | |
209cd7ab AP |
405 | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1); |
406 | netdev->txs = NULL; | |
e613b064 AL |
407 | } |
408 | if (netdev->rxs) { | |
209cd7ab AP |
409 | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1); |
410 | netdev->rxs = NULL; | |
e613b064 | 411 | } |
658788c5 MM |
412 | if (netdev->nic) { |
413 | qemu_del_vlan_client(&netdev->nic->nc); | |
414 | netdev->nic = NULL; | |
e613b064 AL |
415 | } |
416 | } | |
417 | ||
418 | static void net_event(struct XenDevice *xendev) | |
419 | { | |
420 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
421 | net_tx_packets(netdev); | |
422 | } | |
423 | ||
424 | static int net_free(struct XenDevice *xendev) | |
425 | { | |
426 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
427 | ||
7267c094 | 428 | g_free(netdev->mac); |
e613b064 AL |
429 | return 0; |
430 | } | |
431 | ||
432 | /* ------------------------------------------------------------- */ | |
433 | ||
434 | struct XenDevOps xen_netdev_ops = { | |
435 | .size = sizeof(struct XenNetDev), | |
436 | .flags = DEVOPS_FLAG_NEED_GNTDEV, | |
437 | .init = net_init, | |
384087b2 | 438 | .initialise = net_connect, |
e613b064 AL |
439 | .event = net_event, |
440 | .disconnect = net_disconnect, | |
441 | .free = net_free, | |
442 | }; |