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>
28 #include "net/checksum.h"
30 #include "hw/xen/xen-legacy-backend.h"
32 #include "hw/xen/interface/io/netif.h"
34 /* ------------------------------------------------------------- */
37 struct XenLegacyDevice xendev
; /* must be first */
42 struct netif_tx_sring
*txs
;
43 struct netif_rx_sring
*rxs
;
44 netif_tx_back_ring_t tx_ring
;
45 netif_rx_back_ring_t rx_ring
;
50 /* ------------------------------------------------------------- */
52 static void net_tx_response(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, int8_t st
)
54 RING_IDX i
= netdev
->tx_ring
.rsp_prod_pvt
;
55 netif_tx_response_t
*resp
;
58 resp
= RING_GET_RESPONSE(&netdev
->tx_ring
, i
);
63 if (txp
->flags
& NETTXF_extra_info
) {
64 RING_GET_RESPONSE(&netdev
->tx_ring
, ++i
)->status
= NETIF_RSP_NULL
;
68 netdev
->tx_ring
.rsp_prod_pvt
= ++i
;
69 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->tx_ring
, notify
);
71 xen_pv_send_notify(&netdev
->xendev
);
74 if (i
== netdev
->tx_ring
.req_cons
) {
76 RING_FINAL_CHECK_FOR_REQUESTS(&netdev
->tx_ring
, more_to_do
);
83 static void net_tx_error(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, RING_IDX end
)
87 * Hmm, why netback fails everything in the ring?
88 * Should we do that even when not supporting SG and TSO?
90 RING_IDX cons
= netdev
->tx_ring
.req_cons
;
93 make_tx_response(netif
, txp
, NETIF_RSP_ERROR
);
97 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
99 netdev
->tx_ring
.req_cons
= cons
;
100 netif_schedule_work(netif
);
103 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
107 static void net_tx_packets(struct XenNetDev
*netdev
)
109 netif_tx_request_t txreq
;
115 rc
= netdev
->tx_ring
.req_cons
;
116 rp
= netdev
->tx_ring
.sring
->req_prod
;
117 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
120 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
)) {
123 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
124 netdev
->tx_ring
.req_cons
= ++rc
;
127 /* should not happen in theory, we don't announce the *
128 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
129 if (txreq
.flags
& NETTXF_extra_info
) {
130 xen_pv_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
131 net_tx_error(netdev
, &txreq
, rc
);
134 if (txreq
.flags
& NETTXF_more_data
) {
135 xen_pv_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
136 net_tx_error(netdev
, &txreq
, rc
);
141 if (txreq
.size
< 14) {
142 xen_pv_printf(&netdev
->xendev
, 0, "bad packet size: %d\n",
144 net_tx_error(netdev
, &txreq
, rc
);
148 if ((txreq
.offset
+ txreq
.size
) > XEN_PAGE_SIZE
) {
149 xen_pv_printf(&netdev
->xendev
, 0, "error: page crossing\n");
150 net_tx_error(netdev
, &txreq
, rc
);
154 xen_pv_printf(&netdev
->xendev
, 3,
155 "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
= xen_be_map_grant_ref(&netdev
->xendev
, txreq
.gref
,
165 xen_pv_printf(&netdev
->xendev
, 0,
166 "error: tx gref dereference failed (%d)\n",
168 net_tx_error(netdev
, &txreq
, rc
);
171 if (txreq
.flags
& NETTXF_csum_blank
) {
172 /* have read-only mapping -> can't fill checksum in-place */
174 tmpbuf
= g_malloc(XEN_PAGE_SIZE
);
176 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
177 net_checksum_calculate(tmpbuf
, txreq
.size
, CSUM_ALL
);
178 qemu_send_packet(qemu_get_queue(netdev
->nic
), tmpbuf
,
181 qemu_send_packet(qemu_get_queue(netdev
->nic
),
182 page
+ txreq
.offset
, txreq
.size
);
184 xen_be_unmap_grant_ref(&netdev
->xendev
, page
, txreq
.gref
);
185 net_tx_response(netdev
, &txreq
, NETIF_RSP_OKAY
);
187 if (!netdev
->tx_work
) {
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
,
202 RING_IDX i
= netdev
->rx_ring
.rsp_prod_pvt
;
203 netif_rx_response_t
*resp
;
206 resp
= RING_GET_RESPONSE(&netdev
->rx_ring
, i
);
207 resp
->offset
= offset
;
210 resp
->status
= (int16_t)size
;
212 resp
->status
= (int16_t)st
;
215 xen_pv_printf(&netdev
->xendev
, 3,
216 "rx response: idx %d, status %d, flags 0x%x\n",
217 i
, resp
->status
, resp
->flags
);
219 netdev
->rx_ring
.rsp_prod_pvt
= ++i
;
220 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->rx_ring
, notify
);
222 xen_pv_send_notify(&netdev
->xendev
);
226 #define NET_IP_ALIGN 2
228 static ssize_t
net_rx_packet(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
230 struct XenNetDev
*netdev
= qemu_get_nic_opaque(nc
);
231 netif_rx_request_t rxreq
;
235 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
239 rc
= netdev
->rx_ring
.req_cons
;
240 rp
= netdev
->rx_ring
.sring
->req_prod
;
241 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
243 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
246 if (size
> XEN_PAGE_SIZE
- NET_IP_ALIGN
) {
247 xen_pv_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
248 (unsigned long)size
, XEN_PAGE_SIZE
- NET_IP_ALIGN
);
252 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
253 netdev
->rx_ring
.req_cons
= ++rc
;
255 page
= xen_be_map_grant_ref(&netdev
->xendev
, rxreq
.gref
, PROT_WRITE
);
257 xen_pv_printf(&netdev
->xendev
, 0,
258 "error: rx gref dereference failed (%d)\n",
260 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
263 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
264 xen_be_unmap_grant_ref(&netdev
->xendev
, page
, rxreq
.gref
);
265 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
270 /* ------------------------------------------------------------- */
272 static NetClientInfo net_xen_info
= {
273 .type
= NET_CLIENT_DRIVER_NIC
,
274 .size
= sizeof(NICState
),
275 .receive
= net_rx_packet
,
278 static int net_init(struct XenLegacyDevice
*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
) {
292 if (net_parse_macaddr(netdev
->conf
.macaddr
.a
, netdev
->mac
) < 0) {
296 netdev
->nic
= qemu_new_nic(&net_xen_info
, &netdev
->conf
,
297 "xen", NULL
, netdev
);
299 qemu_set_info_str(qemu_get_queue(netdev
->nic
),
300 "nic: xenbus vif macaddr=%s", netdev
->mac
);
303 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
304 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
309 static int net_connect(struct XenLegacyDevice
*xendev
)
311 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
314 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
315 &netdev
->tx_ring_ref
) == -1) {
318 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
319 &netdev
->rx_ring_ref
) == -1) {
322 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
323 &netdev
->xendev
.remote_port
) == -1) {
327 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1) {
331 xen_pv_printf(&netdev
->xendev
, 0,
332 "frontend doesn't support rx-copy.\n");
336 netdev
->txs
= xen_be_map_grant_ref(&netdev
->xendev
,
338 PROT_READ
| PROT_WRITE
);
342 netdev
->rxs
= xen_be_map_grant_ref(&netdev
->xendev
,
344 PROT_READ
| PROT_WRITE
);
346 xen_be_unmap_grant_ref(&netdev
->xendev
, netdev
->txs
,
347 netdev
->tx_ring_ref
);
351 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XEN_PAGE_SIZE
);
352 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XEN_PAGE_SIZE
);
354 xen_be_bind_evtchn(&netdev
->xendev
);
356 xen_pv_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
);
365 static void net_disconnect(struct XenLegacyDevice
*xendev
)
367 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
369 xen_pv_unbind_evtchn(&netdev
->xendev
);
372 xen_be_unmap_grant_ref(&netdev
->xendev
, netdev
->txs
,
373 netdev
->tx_ring_ref
);
377 xen_be_unmap_grant_ref(&netdev
->xendev
, netdev
->rxs
,
378 netdev
->rx_ring_ref
);
383 static void net_event(struct XenLegacyDevice
*xendev
)
385 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
386 net_tx_packets(netdev
);
387 qemu_flush_queued_packets(qemu_get_queue(netdev
->nic
));
390 static int net_free(struct XenLegacyDevice
*xendev
)
392 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
395 qemu_del_nic(netdev
->nic
);
403 /* ------------------------------------------------------------- */
405 struct XenDevOps xen_netdev_ops
= {
406 .size
= sizeof(struct XenNetDev
),
407 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
409 .initialise
= net_connect
,
411 .disconnect
= net_disconnect
,