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>
29 #include "net/checksum.h"
31 #include "hw/xen/xen_backend.h"
33 #include <xen/io/netif.h>
35 /* ------------------------------------------------------------- */
38 struct XenDevice xendev
; /* must be first */
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
;
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
;
59 resp
= RING_GET_RESPONSE(&netdev
->tx_ring
, i
);
64 if (txp
->flags
& NETTXF_extra_info
) {
65 RING_GET_RESPONSE(&netdev
->tx_ring
, ++i
)->status
= NETIF_RSP_NULL
;
69 netdev
->tx_ring
.rsp_prod_pvt
= ++i
;
70 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->tx_ring
, notify
);
72 xen_pv_send_notify(&netdev
->xendev
);
75 if (i
== netdev
->tx_ring
.req_cons
) {
77 RING_FINAL_CHECK_FOR_REQUESTS(&netdev
->tx_ring
, more_to_do
);
84 static void net_tx_error(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, RING_IDX end
)
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
;
94 make_tx_response(netif
, txp
, NETIF_RSP_ERROR
);
98 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
100 netdev
->tx_ring
.req_cons
= cons
;
101 netif_schedule_work(netif
);
104 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
108 static void net_tx_packets(struct XenNetDev
*netdev
)
110 netif_tx_request_t txreq
;
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'. */
121 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
)) {
124 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
125 netdev
->tx_ring
.req_cons
= ++rc
;
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_pv_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
132 net_tx_error(netdev
, &txreq
, rc
);
135 if (txreq
.flags
& NETTXF_more_data
) {
136 xen_pv_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
137 net_tx_error(netdev
, &txreq
, rc
);
142 if (txreq
.size
< 14) {
143 xen_pv_printf(&netdev
->xendev
, 0, "bad packet size: %d\n",
145 net_tx_error(netdev
, &txreq
, rc
);
149 if ((txreq
.offset
+ txreq
.size
) > XC_PAGE_SIZE
) {
150 xen_pv_printf(&netdev
->xendev
, 0, "error: page crossing\n");
151 net_tx_error(netdev
, &txreq
, rc
);
155 xen_pv_printf(&netdev
->xendev
, 3,
156 "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
157 txreq
.gref
, txreq
.offset
, txreq
.size
, txreq
.flags
,
158 (txreq
.flags
& NETTXF_csum_blank
) ? " csum_blank" : "",
159 (txreq
.flags
& NETTXF_data_validated
) ? " data_validated" : "",
160 (txreq
.flags
& NETTXF_more_data
) ? " more_data" : "",
161 (txreq
.flags
& NETTXF_extra_info
) ? " extra_info" : "");
163 page
= xengnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
165 txreq
.gref
, PROT_READ
);
167 xen_pv_printf(&netdev
->xendev
, 0,
168 "error: tx gref dereference failed (%d)\n",
170 net_tx_error(netdev
, &txreq
, rc
);
173 if (txreq
.flags
& NETTXF_csum_blank
) {
174 /* have read-only mapping -> can't fill checksum in-place */
176 tmpbuf
= g_malloc(XC_PAGE_SIZE
);
178 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
179 net_checksum_calculate(tmpbuf
, txreq
.size
);
180 qemu_send_packet(qemu_get_queue(netdev
->nic
), tmpbuf
,
183 qemu_send_packet(qemu_get_queue(netdev
->nic
),
184 page
+ txreq
.offset
, txreq
.size
);
186 xengnttab_unmap(netdev
->xendev
.gnttabdev
, page
, 1);
187 net_tx_response(netdev
, &txreq
, NETIF_RSP_OKAY
);
189 if (!netdev
->tx_work
) {
197 /* ------------------------------------------------------------- */
199 static void net_rx_response(struct XenNetDev
*netdev
,
200 netif_rx_request_t
*req
, int8_t st
,
201 uint16_t offset
, uint16_t size
,
204 RING_IDX i
= netdev
->rx_ring
.rsp_prod_pvt
;
205 netif_rx_response_t
*resp
;
208 resp
= RING_GET_RESPONSE(&netdev
->rx_ring
, i
);
209 resp
->offset
= offset
;
212 resp
->status
= (int16_t)size
;
214 resp
->status
= (int16_t)st
;
217 xen_pv_printf(&netdev
->xendev
, 3,
218 "rx response: idx %d, status %d, flags 0x%x\n",
219 i
, resp
->status
, resp
->flags
);
221 netdev
->rx_ring
.rsp_prod_pvt
= ++i
;
222 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->rx_ring
, notify
);
224 xen_pv_send_notify(&netdev
->xendev
);
228 #define NET_IP_ALIGN 2
230 static ssize_t
net_rx_packet(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
232 struct XenNetDev
*netdev
= qemu_get_nic_opaque(nc
);
233 netif_rx_request_t rxreq
;
237 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
241 rc
= netdev
->rx_ring
.req_cons
;
242 rp
= netdev
->rx_ring
.sring
->req_prod
;
243 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
245 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
248 if (size
> XC_PAGE_SIZE
- NET_IP_ALIGN
) {
249 xen_pv_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
250 (unsigned long)size
, XC_PAGE_SIZE
- NET_IP_ALIGN
);
254 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
255 netdev
->rx_ring
.req_cons
= ++rc
;
257 page
= xengnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
259 rxreq
.gref
, PROT_WRITE
);
261 xen_pv_printf(&netdev
->xendev
, 0,
262 "error: rx gref dereference failed (%d)\n",
264 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
267 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
268 xengnttab_unmap(netdev
->xendev
.gnttabdev
, page
, 1);
269 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
274 /* ------------------------------------------------------------- */
276 static NetClientInfo net_xen_info
= {
277 .type
= NET_CLIENT_DRIVER_NIC
,
278 .size
= sizeof(NICState
),
279 .receive
= net_rx_packet
,
282 static int net_init(struct XenDevice
*xendev
)
284 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
286 /* read xenstore entries */
287 if (netdev
->mac
== NULL
) {
288 netdev
->mac
= xenstore_read_be_str(&netdev
->xendev
, "mac");
291 /* do we have all we need? */
292 if (netdev
->mac
== NULL
) {
296 if (net_parse_macaddr(netdev
->conf
.macaddr
.a
, netdev
->mac
) < 0) {
300 netdev
->nic
= qemu_new_nic(&net_xen_info
, &netdev
->conf
,
301 "xen", NULL
, netdev
);
303 snprintf(qemu_get_queue(netdev
->nic
)->info_str
,
304 sizeof(qemu_get_queue(netdev
->nic
)->info_str
),
305 "nic: xenbus vif macaddr=%s", netdev
->mac
);
308 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
309 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
314 static int net_connect(struct XenDevice
*xendev
)
316 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
319 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
320 &netdev
->tx_ring_ref
) == -1) {
323 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
324 &netdev
->rx_ring_ref
) == -1) {
327 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
328 &netdev
->xendev
.remote_port
) == -1) {
332 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1) {
336 xen_pv_printf(&netdev
->xendev
, 0,
337 "frontend doesn't support rx-copy.\n");
341 netdev
->txs
= xengnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
344 PROT_READ
| PROT_WRITE
);
348 netdev
->rxs
= xengnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
351 PROT_READ
| PROT_WRITE
);
353 xengnttab_unmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
357 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XC_PAGE_SIZE
);
358 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XC_PAGE_SIZE
);
360 xen_be_bind_evtchn(&netdev
->xendev
);
362 xen_pv_printf(&netdev
->xendev
, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
363 "remote port %d, local port %d\n",
364 netdev
->tx_ring_ref
, netdev
->rx_ring_ref
,
365 netdev
->xendev
.remote_port
, netdev
->xendev
.local_port
);
367 net_tx_packets(netdev
);
371 static void net_disconnect(struct XenDevice
*xendev
)
373 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
375 xen_pv_unbind_evtchn(&netdev
->xendev
);
378 xengnttab_unmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
382 xengnttab_unmap(netdev
->xendev
.gnttabdev
, netdev
->rxs
, 1);
387 static void net_event(struct XenDevice
*xendev
)
389 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
390 net_tx_packets(netdev
);
391 qemu_flush_queued_packets(qemu_get_queue(netdev
->nic
));
394 static int net_free(struct XenDevice
*xendev
)
396 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
399 qemu_del_nic(netdev
->nic
);
407 /* ------------------------------------------------------------- */
409 struct XenDevOps xen_netdev_ops
= {
410 .size
= sizeof(struct XenNetDev
),
411 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
413 .initialise
= net_connect
,
415 .disconnect
= net_disconnect
,