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.
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
39 #include "net/checksum.h"
41 #include "hw/xen/xen_backend.h"
43 #include <xen/io/netif.h>
45 /* ------------------------------------------------------------- */
48 struct XenDevice xendev
; /* must be first */
53 struct netif_tx_sring
*txs
;
54 struct netif_rx_sring
*rxs
;
55 netif_tx_back_ring_t tx_ring
;
56 netif_rx_back_ring_t rx_ring
;
61 /* ------------------------------------------------------------- */
63 static void net_tx_response(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, int8_t st
)
65 RING_IDX i
= netdev
->tx_ring
.rsp_prod_pvt
;
66 netif_tx_response_t
*resp
;
69 resp
= RING_GET_RESPONSE(&netdev
->tx_ring
, i
);
74 if (txp
->flags
& NETTXF_extra_info
) {
75 RING_GET_RESPONSE(&netdev
->tx_ring
, ++i
)->status
= NETIF_RSP_NULL
;
79 netdev
->tx_ring
.rsp_prod_pvt
= ++i
;
80 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->tx_ring
, notify
);
82 xen_be_send_notify(&netdev
->xendev
);
85 if (i
== netdev
->tx_ring
.req_cons
) {
87 RING_FINAL_CHECK_FOR_REQUESTS(&netdev
->tx_ring
, more_to_do
);
94 static void net_tx_error(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, RING_IDX end
)
98 * Hmm, why netback fails everything in the ring?
99 * Should we do that even when not supporting SG and TSO?
101 RING_IDX cons
= netdev
->tx_ring
.req_cons
;
104 make_tx_response(netif
, txp
, NETIF_RSP_ERROR
);
108 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
110 netdev
->tx_ring
.req_cons
= cons
;
111 netif_schedule_work(netif
);
114 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
118 static void net_tx_packets(struct XenNetDev
*netdev
)
120 netif_tx_request_t txreq
;
126 rc
= netdev
->tx_ring
.req_cons
;
127 rp
= netdev
->tx_ring
.sring
->req_prod
;
128 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
131 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
)) {
134 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
135 netdev
->tx_ring
.req_cons
= ++rc
;
138 /* should not happen in theory, we don't announce the *
139 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
140 if (txreq
.flags
& NETTXF_extra_info
) {
141 xen_be_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
142 net_tx_error(netdev
, &txreq
, rc
);
145 if (txreq
.flags
& NETTXF_more_data
) {
146 xen_be_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
147 net_tx_error(netdev
, &txreq
, rc
);
152 if (txreq
.size
< 14) {
153 xen_be_printf(&netdev
->xendev
, 0, "bad packet size: %d\n", txreq
.size
);
154 net_tx_error(netdev
, &txreq
, rc
);
158 if ((txreq
.offset
+ txreq
.size
) > XC_PAGE_SIZE
) {
159 xen_be_printf(&netdev
->xendev
, 0, "error: page crossing\n");
160 net_tx_error(netdev
, &txreq
, rc
);
164 xen_be_printf(&netdev
->xendev
, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
165 txreq
.gref
, txreq
.offset
, txreq
.size
, txreq
.flags
,
166 (txreq
.flags
& NETTXF_csum_blank
) ? " csum_blank" : "",
167 (txreq
.flags
& NETTXF_data_validated
) ? " data_validated" : "",
168 (txreq
.flags
& NETTXF_more_data
) ? " more_data" : "",
169 (txreq
.flags
& NETTXF_extra_info
) ? " extra_info" : "");
171 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
173 txreq
.gref
, PROT_READ
);
175 xen_be_printf(&netdev
->xendev
, 0, "error: tx gref dereference failed (%d)\n",
177 net_tx_error(netdev
, &txreq
, rc
);
180 if (txreq
.flags
& NETTXF_csum_blank
) {
181 /* have read-only mapping -> can't fill checksum in-place */
183 tmpbuf
= g_malloc(XC_PAGE_SIZE
);
185 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
186 net_checksum_calculate(tmpbuf
, txreq
.size
);
187 qemu_send_packet(qemu_get_queue(netdev
->nic
), tmpbuf
,
190 qemu_send_packet(qemu_get_queue(netdev
->nic
),
191 page
+ txreq
.offset
, txreq
.size
);
193 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
194 net_tx_response(netdev
, &txreq
, NETIF_RSP_OKAY
);
196 if (!netdev
->tx_work
) {
204 /* ------------------------------------------------------------- */
206 static void net_rx_response(struct XenNetDev
*netdev
,
207 netif_rx_request_t
*req
, int8_t st
,
208 uint16_t offset
, uint16_t size
,
211 RING_IDX i
= netdev
->rx_ring
.rsp_prod_pvt
;
212 netif_rx_response_t
*resp
;
215 resp
= RING_GET_RESPONSE(&netdev
->rx_ring
, i
);
216 resp
->offset
= offset
;
219 resp
->status
= (int16_t)size
;
221 resp
->status
= (int16_t)st
;
224 xen_be_printf(&netdev
->xendev
, 3, "rx response: idx %d, status %d, flags 0x%x\n",
225 i
, resp
->status
, resp
->flags
);
227 netdev
->rx_ring
.rsp_prod_pvt
= ++i
;
228 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->rx_ring
, notify
);
230 xen_be_send_notify(&netdev
->xendev
);
234 #define NET_IP_ALIGN 2
236 static ssize_t
net_rx_packet(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
238 struct XenNetDev
*netdev
= qemu_get_nic_opaque(nc
);
239 netif_rx_request_t rxreq
;
243 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
247 rc
= netdev
->rx_ring
.req_cons
;
248 rp
= netdev
->rx_ring
.sring
->req_prod
;
249 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
251 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
254 if (size
> XC_PAGE_SIZE
- NET_IP_ALIGN
) {
255 xen_be_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
256 (unsigned long)size
, XC_PAGE_SIZE
- NET_IP_ALIGN
);
260 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
261 netdev
->rx_ring
.req_cons
= ++rc
;
263 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
265 rxreq
.gref
, PROT_WRITE
);
267 xen_be_printf(&netdev
->xendev
, 0, "error: rx gref dereference failed (%d)\n",
269 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
272 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
273 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
274 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
279 /* ------------------------------------------------------------- */
281 static NetClientInfo net_xen_info
= {
282 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
283 .size
= sizeof(NICState
),
284 .receive
= net_rx_packet
,
287 static int net_init(struct XenDevice
*xendev
)
289 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
291 /* read xenstore entries */
292 if (netdev
->mac
== NULL
) {
293 netdev
->mac
= xenstore_read_be_str(&netdev
->xendev
, "mac");
296 /* do we have all we need? */
297 if (netdev
->mac
== NULL
) {
301 if (net_parse_macaddr(netdev
->conf
.macaddr
.a
, netdev
->mac
) < 0) {
305 netdev
->nic
= qemu_new_nic(&net_xen_info
, &netdev
->conf
,
306 "xen", NULL
, netdev
);
308 snprintf(qemu_get_queue(netdev
->nic
)->info_str
,
309 sizeof(qemu_get_queue(netdev
->nic
)->info_str
),
310 "nic: xenbus vif macaddr=%s", netdev
->mac
);
313 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
314 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
319 static int net_connect(struct XenDevice
*xendev
)
321 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
324 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
325 &netdev
->tx_ring_ref
) == -1) {
328 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
329 &netdev
->rx_ring_ref
) == -1) {
332 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
333 &netdev
->xendev
.remote_port
) == -1) {
337 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1) {
341 xen_be_printf(&netdev
->xendev
, 0, "frontend doesn't support rx-copy.\n");
345 netdev
->txs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
348 PROT_READ
| PROT_WRITE
);
352 netdev
->rxs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
355 PROT_READ
| PROT_WRITE
);
357 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
361 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XC_PAGE_SIZE
);
362 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XC_PAGE_SIZE
);
364 xen_be_bind_evtchn(&netdev
->xendev
);
366 xen_be_printf(&netdev
->xendev
, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
367 "remote port %d, local port %d\n",
368 netdev
->tx_ring_ref
, netdev
->rx_ring_ref
,
369 netdev
->xendev
.remote_port
, netdev
->xendev
.local_port
);
371 net_tx_packets(netdev
);
375 static void net_disconnect(struct XenDevice
*xendev
)
377 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
379 xen_be_unbind_evtchn(&netdev
->xendev
);
382 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
386 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->rxs
, 1);
391 static void net_event(struct XenDevice
*xendev
)
393 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
394 net_tx_packets(netdev
);
395 qemu_flush_queued_packets(qemu_get_queue(netdev
->nic
));
398 static int net_free(struct XenDevice
*xendev
)
400 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
403 qemu_del_nic(netdev
->nic
);
411 /* ------------------------------------------------------------- */
413 struct XenDevOps xen_netdev_ops
= {
414 .size
= sizeof(struct XenNetDev
),
415 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
417 .initialise
= net_connect
,
419 .disconnect
= net_disconnect
,