pc: use new CPU hotplug interface since 2.7 machine type
[qemu/ar7.git] / hw / net / xen_nic.c
blob0b4ddae48cedb984ac1708cada75e47887ec7ac8
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/wait.h>
27 #include "hw/hw.h"
28 #include "net/net.h"
29 #include "net/checksum.h"
30 #include "net/util.h"
31 #include "hw/xen/xen_backend.h"
33 #include <xen/io/netif.h>
35 /* ------------------------------------------------------------- */
37 struct XenNetDev {
38 struct XenDevice xendev; /* must be first */
39 char *mac;
40 int tx_work;
41 int tx_ring_ref;
42 int rx_ring_ref;
43 struct netif_tx_sring *txs;
44 struct netif_rx_sring *rxs;
45 netif_tx_back_ring_t tx_ring;
46 netif_rx_back_ring_t rx_ring;
47 NICConf conf;
48 NICState *nic;
51 /* ------------------------------------------------------------- */
53 static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st)
55 RING_IDX i = netdev->tx_ring.rsp_prod_pvt;
56 netif_tx_response_t *resp;
57 int notify;
59 resp = RING_GET_RESPONSE(&netdev->tx_ring, i);
60 resp->id = txp->id;
61 resp->status = st;
63 #if 0
64 if (txp->flags & NETTXF_extra_info) {
65 RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
67 #endif
69 netdev->tx_ring.rsp_prod_pvt = ++i;
70 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify);
71 if (notify) {
72 xen_be_send_notify(&netdev->xendev);
75 if (i == netdev->tx_ring.req_cons) {
76 int more_to_do;
77 RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do);
78 if (more_to_do) {
79 netdev->tx_work++;
84 static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end)
86 #if 0
88 * Hmm, why netback fails everything in the ring?
89 * Should we do that even when not supporting SG and TSO?
91 RING_IDX cons = netdev->tx_ring.req_cons;
93 do {
94 make_tx_response(netif, txp, NETIF_RSP_ERROR);
95 if (cons >= end) {
96 break;
98 txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
99 } while (1);
100 netdev->tx_ring.req_cons = cons;
101 netif_schedule_work(netif);
102 netif_put(netif);
103 #else
104 net_tx_response(netdev, txp, NETIF_RSP_ERROR);
105 #endif
108 static void net_tx_packets(struct XenNetDev *netdev)
110 netif_tx_request_t txreq;
111 RING_IDX rc, rp;
112 void *page;
113 void *tmpbuf = NULL;
115 for (;;) {
116 rc = netdev->tx_ring.req_cons;
117 rp = netdev->tx_ring.sring->req_prod;
118 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
120 while ((rc != rp)) {
121 if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) {
122 break;
124 memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
125 netdev->tx_ring.req_cons = ++rc;
127 #if 1
128 /* should not happen in theory, we don't announce the *
129 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
130 if (txreq.flags & NETTXF_extra_info) {
131 xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n");
132 net_tx_error(netdev, &txreq, rc);
133 continue;
135 if (txreq.flags & NETTXF_more_data) {
136 xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n");
137 net_tx_error(netdev, &txreq, rc);
138 continue;
140 #endif
142 if (txreq.size < 14) {
143 xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size);
144 net_tx_error(netdev, &txreq, rc);
145 continue;
148 if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
149 xen_be_printf(&netdev->xendev, 0, "error: page crossing\n");
150 net_tx_error(netdev, &txreq, rc);
151 continue;
154 xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
155 txreq.gref, txreq.offset, txreq.size, txreq.flags,
156 (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "",
157 (txreq.flags & NETTXF_data_validated) ? " data_validated" : "",
158 (txreq.flags & NETTXF_more_data) ? " more_data" : "",
159 (txreq.flags & NETTXF_extra_info) ? " extra_info" : "");
161 page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
162 netdev->xendev.dom,
163 txreq.gref, PROT_READ);
164 if (page == NULL) {
165 xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n",
166 txreq.gref);
167 net_tx_error(netdev, &txreq, rc);
168 continue;
170 if (txreq.flags & NETTXF_csum_blank) {
171 /* have read-only mapping -> can't fill checksum in-place */
172 if (!tmpbuf) {
173 tmpbuf = g_malloc(XC_PAGE_SIZE);
175 memcpy(tmpbuf, page + txreq.offset, txreq.size);
176 net_checksum_calculate(tmpbuf, txreq.size);
177 qemu_send_packet(qemu_get_queue(netdev->nic), tmpbuf,
178 txreq.size);
179 } else {
180 qemu_send_packet(qemu_get_queue(netdev->nic),
181 page + txreq.offset, txreq.size);
183 xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
184 net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
186 if (!netdev->tx_work) {
187 break;
189 netdev->tx_work = 0;
191 g_free(tmpbuf);
194 /* ------------------------------------------------------------- */
196 static void net_rx_response(struct XenNetDev *netdev,
197 netif_rx_request_t *req, int8_t st,
198 uint16_t offset, uint16_t size,
199 uint16_t flags)
201 RING_IDX i = netdev->rx_ring.rsp_prod_pvt;
202 netif_rx_response_t *resp;
203 int notify;
205 resp = RING_GET_RESPONSE(&netdev->rx_ring, i);
206 resp->offset = offset;
207 resp->flags = flags;
208 resp->id = req->id;
209 resp->status = (int16_t)size;
210 if (st < 0) {
211 resp->status = (int16_t)st;
214 xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n",
215 i, resp->status, resp->flags);
217 netdev->rx_ring.rsp_prod_pvt = ++i;
218 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify);
219 if (notify) {
220 xen_be_send_notify(&netdev->xendev);
224 #define NET_IP_ALIGN 2
226 static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size)
228 struct XenNetDev *netdev = qemu_get_nic_opaque(nc);
229 netif_rx_request_t rxreq;
230 RING_IDX rc, rp;
231 void *page;
233 if (netdev->xendev.be_state != XenbusStateConnected) {
234 return -1;
237 rc = netdev->rx_ring.req_cons;
238 rp = netdev->rx_ring.sring->req_prod;
239 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
241 if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
242 return 0;
244 if (size > XC_PAGE_SIZE - NET_IP_ALIGN) {
245 xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)",
246 (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN);
247 return -1;
250 memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
251 netdev->rx_ring.req_cons = ++rc;
253 page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
254 netdev->xendev.dom,
255 rxreq.gref, PROT_WRITE);
256 if (page == NULL) {
257 xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n",
258 rxreq.gref);
259 net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0);
260 return -1;
262 memcpy(page + NET_IP_ALIGN, buf, size);
263 xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
264 net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
266 return size;
269 /* ------------------------------------------------------------- */
271 static NetClientInfo net_xen_info = {
272 .type = NET_CLIENT_OPTIONS_KIND_NIC,
273 .size = sizeof(NICState),
274 .receive = net_rx_packet,
277 static int net_init(struct XenDevice *xendev)
279 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
281 /* read xenstore entries */
282 if (netdev->mac == NULL) {
283 netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac");
286 /* do we have all we need? */
287 if (netdev->mac == NULL) {
288 return -1;
291 if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) {
292 return -1;
295 netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
296 "xen", NULL, netdev);
298 snprintf(qemu_get_queue(netdev->nic)->info_str,
299 sizeof(qemu_get_queue(netdev->nic)->info_str),
300 "nic: xenbus vif macaddr=%s", netdev->mac);
302 /* fill info */
303 xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1);
304 xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0);
306 return 0;
309 static int net_connect(struct XenDevice *xendev)
311 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
312 int rx_copy;
314 if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref",
315 &netdev->tx_ring_ref) == -1) {
316 return -1;
318 if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref",
319 &netdev->rx_ring_ref) == -1) {
320 return 1;
322 if (xenstore_read_fe_int(&netdev->xendev, "event-channel",
323 &netdev->xendev.remote_port) == -1) {
324 return -1;
327 if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) {
328 rx_copy = 0;
330 if (rx_copy == 0) {
331 xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n");
332 return -1;
335 netdev->txs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
336 netdev->xendev.dom,
337 netdev->tx_ring_ref,
338 PROT_READ | PROT_WRITE);
339 if (!netdev->txs) {
340 return -1;
342 netdev->rxs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
343 netdev->xendev.dom,
344 netdev->rx_ring_ref,
345 PROT_READ | PROT_WRITE);
346 if (!netdev->rxs) {
347 xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
348 netdev->txs = NULL;
349 return -1;
351 BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE);
352 BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE);
354 xen_be_bind_evtchn(&netdev->xendev);
356 xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
357 "remote port %d, local port %d\n",
358 netdev->tx_ring_ref, netdev->rx_ring_ref,
359 netdev->xendev.remote_port, netdev->xendev.local_port);
361 net_tx_packets(netdev);
362 return 0;
365 static void net_disconnect(struct XenDevice *xendev)
367 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
369 xen_be_unbind_evtchn(&netdev->xendev);
371 if (netdev->txs) {
372 xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
373 netdev->txs = NULL;
375 if (netdev->rxs) {
376 xengnttab_unmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
377 netdev->rxs = NULL;
381 static void net_event(struct XenDevice *xendev)
383 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
384 net_tx_packets(netdev);
385 qemu_flush_queued_packets(qemu_get_queue(netdev->nic));
388 static int net_free(struct XenDevice *xendev)
390 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
392 if (netdev->nic) {
393 qemu_del_nic(netdev->nic);
394 netdev->nic = NULL;
396 g_free(netdev->mac);
397 netdev->mac = NULL;
398 return 0;
401 /* ------------------------------------------------------------- */
403 struct XenDevOps xen_netdev_ops = {
404 .size = sizeof(struct XenNetDev),
405 .flags = DEVOPS_FLAG_NEED_GNTDEV,
406 .init = net_init,
407 .initialise = net_connect,
408 .event = net_event,
409 .disconnect = net_disconnect,
410 .free = net_free,