Use error_fatal to simplify obvious fatal errors (again)
[qemu/cris-port.git] / hw / net / xen_nic.c
blob7281730d94437bed5d00e680753191b25b8db016
1 /*
2 * xen paravirt network card backend
4 * (c) Gerd Hoffmann <kraxel@redhat.com>
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
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.
22 #include "qemu/osdep.h"
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
28 #include "hw/hw.h"
29 #include "net/net.h"
30 #include "net/checksum.h"
31 #include "net/util.h"
32 #include "hw/xen/xen_backend.h"
34 #include <xen/io/netif.h>
36 /* ------------------------------------------------------------- */
38 struct XenNetDev {
39 struct XenDevice xendev; /* must be first */
40 char *mac;
41 int tx_work;
42 int tx_ring_ref;
43 int rx_ring_ref;
44 struct netif_tx_sring *txs;
45 struct netif_rx_sring *rxs;
46 netif_tx_back_ring_t tx_ring;
47 netif_rx_back_ring_t rx_ring;
48 NICConf conf;
49 NICState *nic;
52 /* ------------------------------------------------------------- */
54 static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st)
56 RING_IDX i = netdev->tx_ring.rsp_prod_pvt;
57 netif_tx_response_t *resp;
58 int notify;
60 resp = RING_GET_RESPONSE(&netdev->tx_ring, i);
61 resp->id = txp->id;
62 resp->status = st;
64 #if 0
65 if (txp->flags & NETTXF_extra_info) {
66 RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
68 #endif
70 netdev->tx_ring.rsp_prod_pvt = ++i;
71 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify);
72 if (notify) {
73 xen_be_send_notify(&netdev->xendev);
76 if (i == netdev->tx_ring.req_cons) {
77 int more_to_do;
78 RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do);
79 if (more_to_do) {
80 netdev->tx_work++;
85 static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end)
87 #if 0
89 * Hmm, why netback fails everything in the ring?
90 * Should we do that even when not supporting SG and TSO?
92 RING_IDX cons = netdev->tx_ring.req_cons;
94 do {
95 make_tx_response(netif, txp, NETIF_RSP_ERROR);
96 if (cons >= end) {
97 break;
99 txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
100 } while (1);
101 netdev->tx_ring.req_cons = cons;
102 netif_schedule_work(netif);
103 netif_put(netif);
104 #else
105 net_tx_response(netdev, txp, NETIF_RSP_ERROR);
106 #endif
109 static void net_tx_packets(struct XenNetDev *netdev)
111 netif_tx_request_t txreq;
112 RING_IDX rc, rp;
113 void *page;
114 void *tmpbuf = NULL;
116 for (;;) {
117 rc = netdev->tx_ring.req_cons;
118 rp = netdev->tx_ring.sring->req_prod;
119 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
121 while ((rc != rp)) {
122 if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) {
123 break;
125 memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
126 netdev->tx_ring.req_cons = ++rc;
128 #if 1
129 /* should not happen in theory, we don't announce the *
130 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
131 if (txreq.flags & NETTXF_extra_info) {
132 xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n");
133 net_tx_error(netdev, &txreq, rc);
134 continue;
136 if (txreq.flags & NETTXF_more_data) {
137 xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n");
138 net_tx_error(netdev, &txreq, rc);
139 continue;
141 #endif
143 if (txreq.size < 14) {
144 xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size);
145 net_tx_error(netdev, &txreq, rc);
146 continue;
149 if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
150 xen_be_printf(&netdev->xendev, 0, "error: page crossing\n");
151 net_tx_error(netdev, &txreq, rc);
152 continue;
155 xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
156 txreq.gref, txreq.offset, txreq.size, txreq.flags,
157 (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "",
158 (txreq.flags & NETTXF_data_validated) ? " data_validated" : "",
159 (txreq.flags & NETTXF_more_data) ? " more_data" : "",
160 (txreq.flags & NETTXF_extra_info) ? " extra_info" : "");
162 page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
163 netdev->xendev.dom,
164 txreq.gref, PROT_READ);
165 if (page == NULL) {
166 xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n",
167 txreq.gref);
168 net_tx_error(netdev, &txreq, rc);
169 continue;
171 if (txreq.flags & NETTXF_csum_blank) {
172 /* have read-only mapping -> can't fill checksum in-place */
173 if (!tmpbuf) {
174 tmpbuf = g_malloc(XC_PAGE_SIZE);
176 memcpy(tmpbuf, page + txreq.offset, txreq.size);
177 net_checksum_calculate(tmpbuf, txreq.size);
178 qemu_send_packet(qemu_get_queue(netdev->nic), tmpbuf,
179 txreq.size);
180 } else {
181 qemu_send_packet(qemu_get_queue(netdev->nic),
182 page + txreq.offset, txreq.size);
184 xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
185 net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
187 if (!netdev->tx_work) {
188 break;
190 netdev->tx_work = 0;
192 g_free(tmpbuf);
195 /* ------------------------------------------------------------- */
197 static void net_rx_response(struct XenNetDev *netdev,
198 netif_rx_request_t *req, int8_t st,
199 uint16_t offset, uint16_t size,
200 uint16_t flags)
202 RING_IDX i = netdev->rx_ring.rsp_prod_pvt;
203 netif_rx_response_t *resp;
204 int notify;
206 resp = RING_GET_RESPONSE(&netdev->rx_ring, i);
207 resp->offset = offset;
208 resp->flags = flags;
209 resp->id = req->id;
210 resp->status = (int16_t)size;
211 if (st < 0) {
212 resp->status = (int16_t)st;
215 xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n",
216 i, resp->status, resp->flags);
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);
225 #define NET_IP_ALIGN 2
227 static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size)
229 struct XenNetDev *netdev = qemu_get_nic_opaque(nc);
230 netif_rx_request_t rxreq;
231 RING_IDX rc, rp;
232 void *page;
234 if (netdev->xendev.be_state != XenbusStateConnected) {
235 return -1;
238 rc = netdev->rx_ring.req_cons;
239 rp = netdev->rx_ring.sring->req_prod;
240 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
242 if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
243 return 0;
245 if (size > XC_PAGE_SIZE - NET_IP_ALIGN) {
246 xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)",
247 (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN);
248 return -1;
251 memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
252 netdev->rx_ring.req_cons = ++rc;
254 page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
255 netdev->xendev.dom,
256 rxreq.gref, PROT_WRITE);
257 if (page == NULL) {
258 xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n",
259 rxreq.gref);
260 net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0);
261 return -1;
263 memcpy(page + NET_IP_ALIGN, buf, size);
264 xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
265 net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
267 return size;
270 /* ------------------------------------------------------------- */
272 static NetClientInfo net_xen_info = {
273 .type = NET_CLIENT_OPTIONS_KIND_NIC,
274 .size = sizeof(NICState),
275 .receive = net_rx_packet,
278 static int net_init(struct XenDevice *xendev)
280 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
282 /* read xenstore entries */
283 if (netdev->mac == NULL) {
284 netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac");
287 /* do we have all we need? */
288 if (netdev->mac == NULL) {
289 return -1;
292 if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) {
293 return -1;
296 netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
297 "xen", NULL, netdev);
299 snprintf(qemu_get_queue(netdev->nic)->info_str,
300 sizeof(qemu_get_queue(netdev->nic)->info_str),
301 "nic: xenbus vif macaddr=%s", netdev->mac);
303 /* fill info */
304 xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1);
305 xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0);
307 return 0;
310 static int net_connect(struct XenDevice *xendev)
312 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
313 int rx_copy;
315 if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref",
316 &netdev->tx_ring_ref) == -1) {
317 return -1;
319 if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref",
320 &netdev->rx_ring_ref) == -1) {
321 return 1;
323 if (xenstore_read_fe_int(&netdev->xendev, "event-channel",
324 &netdev->xendev.remote_port) == -1) {
325 return -1;
328 if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) {
329 rx_copy = 0;
331 if (rx_copy == 0) {
332 xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n");
333 return -1;
336 netdev->txs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
337 netdev->xendev.dom,
338 netdev->tx_ring_ref,
339 PROT_READ | PROT_WRITE);
340 if (!netdev->txs) {
341 return -1;
343 netdev->rxs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
344 netdev->xendev.dom,
345 netdev->rx_ring_ref,
346 PROT_READ | PROT_WRITE);
347 if (!netdev->rxs) {
348 xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
349 netdev->txs = NULL;
350 return -1;
352 BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE);
353 BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE);
355 xen_be_bind_evtchn(&netdev->xendev);
357 xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
358 "remote port %d, local port %d\n",
359 netdev->tx_ring_ref, netdev->rx_ring_ref,
360 netdev->xendev.remote_port, netdev->xendev.local_port);
362 net_tx_packets(netdev);
363 return 0;
366 static void net_disconnect(struct XenDevice *xendev)
368 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
370 xen_be_unbind_evtchn(&netdev->xendev);
372 if (netdev->txs) {
373 xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
374 netdev->txs = NULL;
376 if (netdev->rxs) {
377 xengnttab_unmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
378 netdev->rxs = NULL;
382 static void net_event(struct XenDevice *xendev)
384 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
385 net_tx_packets(netdev);
386 qemu_flush_queued_packets(qemu_get_queue(netdev->nic));
389 static int net_free(struct XenDevice *xendev)
391 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
393 if (netdev->nic) {
394 qemu_del_nic(netdev->nic);
395 netdev->nic = NULL;
397 g_free(netdev->mac);
398 netdev->mac = NULL;
399 return 0;
402 /* ------------------------------------------------------------- */
404 struct XenDevOps xen_netdev_ops = {
405 .size = sizeof(struct XenNetDev),
406 .flags = DEVOPS_FLAG_NEED_GNTDEV,
407 .init = net_init,
408 .initialise = net_connect,
409 .event = net_event,
410 .disconnect = net_disconnect,
411 .free = net_free,