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.
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
40 #include "net/checksum.h"
42 #include "hw/xen/xen_backend.h"
44 #include <xen/io/netif.h>
46 /* ------------------------------------------------------------- */
49 struct XenDevice xendev
; /* must be first */
54 struct netif_tx_sring
*txs
;
55 struct netif_rx_sring
*rxs
;
56 netif_tx_back_ring_t tx_ring
;
57 netif_rx_back_ring_t rx_ring
;
62 /* ------------------------------------------------------------- */
64 static void net_tx_response(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, int8_t st
)
66 RING_IDX i
= netdev
->tx_ring
.rsp_prod_pvt
;
67 netif_tx_response_t
*resp
;
70 resp
= RING_GET_RESPONSE(&netdev
->tx_ring
, i
);
75 if (txp
->flags
& NETTXF_extra_info
) {
76 RING_GET_RESPONSE(&netdev
->tx_ring
, ++i
)->status
= NETIF_RSP_NULL
;
80 netdev
->tx_ring
.rsp_prod_pvt
= ++i
;
81 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->tx_ring
, notify
);
83 xen_be_send_notify(&netdev
->xendev
);
86 if (i
== netdev
->tx_ring
.req_cons
) {
88 RING_FINAL_CHECK_FOR_REQUESTS(&netdev
->tx_ring
, more_to_do
);
95 static void net_tx_error(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, RING_IDX end
)
99 * Hmm, why netback fails everything in the ring?
100 * Should we do that even when not supporting SG and TSO?
102 RING_IDX cons
= netdev
->tx_ring
.req_cons
;
105 make_tx_response(netif
, txp
, NETIF_RSP_ERROR
);
109 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
111 netdev
->tx_ring
.req_cons
= cons
;
112 netif_schedule_work(netif
);
115 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
119 static void net_tx_packets(struct XenNetDev
*netdev
)
121 netif_tx_request_t txreq
;
127 rc
= netdev
->tx_ring
.req_cons
;
128 rp
= netdev
->tx_ring
.sring
->req_prod
;
129 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
132 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
)) {
135 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
136 netdev
->tx_ring
.req_cons
= ++rc
;
139 /* should not happen in theory, we don't announce the *
140 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
141 if (txreq
.flags
& NETTXF_extra_info
) {
142 xen_be_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
143 net_tx_error(netdev
, &txreq
, rc
);
146 if (txreq
.flags
& NETTXF_more_data
) {
147 xen_be_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
148 net_tx_error(netdev
, &txreq
, rc
);
153 if (txreq
.size
< 14) {
154 xen_be_printf(&netdev
->xendev
, 0, "bad packet size: %d\n", txreq
.size
);
155 net_tx_error(netdev
, &txreq
, rc
);
159 if ((txreq
.offset
+ txreq
.size
) > XC_PAGE_SIZE
) {
160 xen_be_printf(&netdev
->xendev
, 0, "error: page crossing\n");
161 net_tx_error(netdev
, &txreq
, rc
);
165 xen_be_printf(&netdev
->xendev
, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
166 txreq
.gref
, txreq
.offset
, txreq
.size
, txreq
.flags
,
167 (txreq
.flags
& NETTXF_csum_blank
) ? " csum_blank" : "",
168 (txreq
.flags
& NETTXF_data_validated
) ? " data_validated" : "",
169 (txreq
.flags
& NETTXF_more_data
) ? " more_data" : "",
170 (txreq
.flags
& NETTXF_extra_info
) ? " extra_info" : "");
172 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
174 txreq
.gref
, PROT_READ
);
176 xen_be_printf(&netdev
->xendev
, 0, "error: tx gref dereference failed (%d)\n",
178 net_tx_error(netdev
, &txreq
, rc
);
181 if (txreq
.flags
& NETTXF_csum_blank
) {
182 /* have read-only mapping -> can't fill checksum in-place */
184 tmpbuf
= g_malloc(XC_PAGE_SIZE
);
186 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
187 net_checksum_calculate(tmpbuf
, txreq
.size
);
188 qemu_send_packet(qemu_get_queue(netdev
->nic
), tmpbuf
,
191 qemu_send_packet(qemu_get_queue(netdev
->nic
),
192 page
+ txreq
.offset
, txreq
.size
);
194 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
195 net_tx_response(netdev
, &txreq
, NETIF_RSP_OKAY
);
197 if (!netdev
->tx_work
) {
205 /* ------------------------------------------------------------- */
207 static void net_rx_response(struct XenNetDev
*netdev
,
208 netif_rx_request_t
*req
, int8_t st
,
209 uint16_t offset
, uint16_t size
,
212 RING_IDX i
= netdev
->rx_ring
.rsp_prod_pvt
;
213 netif_rx_response_t
*resp
;
216 resp
= RING_GET_RESPONSE(&netdev
->rx_ring
, i
);
217 resp
->offset
= offset
;
220 resp
->status
= (int16_t)size
;
222 resp
->status
= (int16_t)st
;
225 xen_be_printf(&netdev
->xendev
, 3, "rx response: idx %d, status %d, flags 0x%x\n",
226 i
, resp
->status
, resp
->flags
);
228 netdev
->rx_ring
.rsp_prod_pvt
= ++i
;
229 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->rx_ring
, notify
);
231 xen_be_send_notify(&netdev
->xendev
);
235 #define NET_IP_ALIGN 2
237 static int net_rx_ok(NetClientState
*nc
)
239 struct XenNetDev
*netdev
= qemu_get_nic_opaque(nc
);
242 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
246 rc
= netdev
->rx_ring
.req_cons
;
247 rp
= netdev
->rx_ring
.sring
->req_prod
;
250 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
251 xen_be_printf(&netdev
->xendev
, 2, "%s: no rx buffers (%d/%d)\n",
252 __FUNCTION__
, rc
, rp
);
258 static ssize_t
net_rx_packet(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
260 struct XenNetDev
*netdev
= qemu_get_nic_opaque(nc
);
261 netif_rx_request_t rxreq
;
265 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
269 rc
= netdev
->rx_ring
.req_cons
;
270 rp
= netdev
->rx_ring
.sring
->req_prod
;
271 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
273 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
274 xen_be_printf(&netdev
->xendev
, 2, "no buffer, drop packet\n");
277 if (size
> XC_PAGE_SIZE
- NET_IP_ALIGN
) {
278 xen_be_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
279 (unsigned long)size
, XC_PAGE_SIZE
- NET_IP_ALIGN
);
283 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
284 netdev
->rx_ring
.req_cons
= ++rc
;
286 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
288 rxreq
.gref
, PROT_WRITE
);
290 xen_be_printf(&netdev
->xendev
, 0, "error: rx gref dereference failed (%d)\n",
292 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
295 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
296 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
297 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
302 /* ------------------------------------------------------------- */
304 static NetClientInfo net_xen_info
= {
305 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
306 .size
= sizeof(NICState
),
307 .can_receive
= net_rx_ok
,
308 .receive
= net_rx_packet
,
311 static int net_init(struct XenDevice
*xendev
)
313 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
315 /* read xenstore entries */
316 if (netdev
->mac
== NULL
) {
317 netdev
->mac
= xenstore_read_be_str(&netdev
->xendev
, "mac");
320 /* do we have all we need? */
321 if (netdev
->mac
== NULL
) {
325 if (net_parse_macaddr(netdev
->conf
.macaddr
.a
, netdev
->mac
) < 0) {
329 netdev
->nic
= qemu_new_nic(&net_xen_info
, &netdev
->conf
,
330 "xen", NULL
, netdev
);
332 snprintf(qemu_get_queue(netdev
->nic
)->info_str
,
333 sizeof(qemu_get_queue(netdev
->nic
)->info_str
),
334 "nic: xenbus vif macaddr=%s", netdev
->mac
);
337 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
338 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
343 static int net_connect(struct XenDevice
*xendev
)
345 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
348 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
349 &netdev
->tx_ring_ref
) == -1) {
352 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
353 &netdev
->rx_ring_ref
) == -1) {
356 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
357 &netdev
->xendev
.remote_port
) == -1) {
361 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1) {
365 xen_be_printf(&netdev
->xendev
, 0, "frontend doesn't support rx-copy.\n");
369 netdev
->txs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
372 PROT_READ
| PROT_WRITE
);
373 netdev
->rxs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
376 PROT_READ
| PROT_WRITE
);
377 if (!netdev
->txs
|| !netdev
->rxs
) {
380 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XC_PAGE_SIZE
);
381 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XC_PAGE_SIZE
);
383 xen_be_bind_evtchn(&netdev
->xendev
);
385 xen_be_printf(&netdev
->xendev
, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
386 "remote port %d, local port %d\n",
387 netdev
->tx_ring_ref
, netdev
->rx_ring_ref
,
388 netdev
->xendev
.remote_port
, netdev
->xendev
.local_port
);
390 net_tx_packets(netdev
);
394 static void net_disconnect(struct XenDevice
*xendev
)
396 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
398 xen_be_unbind_evtchn(&netdev
->xendev
);
401 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
405 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->rxs
, 1);
409 qemu_del_nic(netdev
->nic
);
414 static void net_event(struct XenDevice
*xendev
)
416 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
417 net_tx_packets(netdev
);
418 qemu_flush_queued_packets(qemu_get_queue(netdev
->nic
));
421 static int net_free(struct XenDevice
*xendev
)
423 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
429 /* ------------------------------------------------------------- */
431 struct XenDevOps xen_netdev_ops
= {
432 .size
= sizeof(struct XenNetDev
),
433 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
435 .initialise
= net_connect
,
437 .disconnect
= net_disconnect
,