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/>.
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
38 #include <xen/io/xenbus.h>
39 #include <xen/io/netif.h>
43 #include "net/checksum.h"
45 #include "qemu-char.h"
46 #include "xen_backend.h"
48 /* ------------------------------------------------------------- */
51 struct XenDevice xendev
; /* must be first */
56 struct netif_tx_sring
*txs
;
57 struct netif_rx_sring
*rxs
;
58 netif_tx_back_ring_t tx_ring
;
59 netif_rx_back_ring_t rx_ring
;
64 /* ------------------------------------------------------------- */
66 static void net_tx_response(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, int8_t st
)
68 RING_IDX i
= netdev
->tx_ring
.rsp_prod_pvt
;
69 netif_tx_response_t
*resp
;
72 resp
= RING_GET_RESPONSE(&netdev
->tx_ring
, i
);
77 if (txp
->flags
& NETTXF_extra_info
)
78 RING_GET_RESPONSE(&netdev
->tx_ring
, ++i
)->status
= NETIF_RSP_NULL
;
81 netdev
->tx_ring
.rsp_prod_pvt
= ++i
;
82 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->tx_ring
, notify
);
84 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
);
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
);
107 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
109 netdev
->tx_ring
.req_cons
= cons
;
110 netif_schedule_work(netif
);
113 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
117 static void net_tx_packets(struct XenNetDev
*netdev
)
119 netif_tx_request_t txreq
;
125 rc
= netdev
->tx_ring
.req_cons
;
126 rp
= netdev
->tx_ring
.sring
->req_prod
;
127 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
130 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
))
132 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
133 netdev
->tx_ring
.req_cons
= ++rc
;
136 /* should not happen in theory, we don't announce the *
137 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
138 if (txreq
.flags
& NETTXF_extra_info
) {
139 xen_be_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
140 net_tx_error(netdev
, &txreq
, rc
);
143 if (txreq
.flags
& NETTXF_more_data
) {
144 xen_be_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
145 net_tx_error(netdev
, &txreq
, rc
);
150 if (txreq
.size
< 14) {
151 xen_be_printf(&netdev
->xendev
, 0, "bad packet size: %d\n", txreq
.size
);
152 net_tx_error(netdev
, &txreq
, rc
);
156 if ((txreq
.offset
+ txreq
.size
) > XC_PAGE_SIZE
) {
157 xen_be_printf(&netdev
->xendev
, 0, "error: page crossing\n");
158 net_tx_error(netdev
, &txreq
, rc
);
162 xen_be_printf(&netdev
->xendev
, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
163 txreq
.gref
, txreq
.offset
, txreq
.size
, txreq
.flags
,
164 (txreq
.flags
& NETTXF_csum_blank
) ? " csum_blank" : "",
165 (txreq
.flags
& NETTXF_data_validated
) ? " data_validated" : "",
166 (txreq
.flags
& NETTXF_more_data
) ? " more_data" : "",
167 (txreq
.flags
& NETTXF_extra_info
) ? " extra_info" : "");
169 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
171 txreq
.gref
, PROT_READ
);
173 xen_be_printf(&netdev
->xendev
, 0, "error: tx gref dereference failed (%d)\n",
175 net_tx_error(netdev
, &txreq
, rc
);
178 if (txreq
.flags
& NETTXF_csum_blank
) {
179 /* have read-only mapping -> can't fill checksum in-place */
181 tmpbuf
= qemu_malloc(XC_PAGE_SIZE
);
182 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
183 net_checksum_calculate(tmpbuf
, txreq
.size
);
184 qemu_send_packet(&netdev
->nic
->nc
, tmpbuf
, txreq
.size
);
186 qemu_send_packet(&netdev
->nic
->nc
, page
+ txreq
.offset
, txreq
.size
);
188 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
189 net_tx_response(netdev
, &txreq
, NETIF_RSP_OKAY
);
191 if (!netdev
->tx_work
)
198 /* ------------------------------------------------------------- */
200 static void net_rx_response(struct XenNetDev
*netdev
,
201 netif_rx_request_t
*req
, int8_t st
,
202 uint16_t offset
, uint16_t size
,
205 RING_IDX i
= netdev
->rx_ring
.rsp_prod_pvt
;
206 netif_rx_response_t
*resp
;
209 resp
= RING_GET_RESPONSE(&netdev
->rx_ring
, i
);
210 resp
->offset
= offset
;
213 resp
->status
= (int16_t)size
;
215 resp
->status
= (int16_t)st
;
217 xen_be_printf(&netdev
->xendev
, 3, "rx response: idx %d, status %d, flags 0x%x\n",
218 i
, resp
->status
, resp
->flags
);
220 netdev
->rx_ring
.rsp_prod_pvt
= ++i
;
221 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->rx_ring
, notify
);
223 xen_be_send_notify(&netdev
->xendev
);
226 #define NET_IP_ALIGN 2
228 static int net_rx_ok(VLANClientState
*nc
)
230 struct XenNetDev
*netdev
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
233 if (netdev
->xendev
.be_state
!= XenbusStateConnected
)
236 rc
= netdev
->rx_ring
.req_cons
;
237 rp
= netdev
->rx_ring
.sring
->req_prod
;
240 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
241 xen_be_printf(&netdev
->xendev
, 2, "%s: no rx buffers (%d/%d)\n",
242 __FUNCTION__
, rc
, rp
);
248 static ssize_t
net_rx_packet(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
250 struct XenNetDev
*netdev
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
251 netif_rx_request_t rxreq
;
255 if (netdev
->xendev
.be_state
!= XenbusStateConnected
)
258 rc
= netdev
->rx_ring
.req_cons
;
259 rp
= netdev
->rx_ring
.sring
->req_prod
;
260 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
262 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
263 xen_be_printf(&netdev
->xendev
, 2, "no buffer, drop packet\n");
266 if (size
> XC_PAGE_SIZE
- NET_IP_ALIGN
) {
267 xen_be_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
268 (unsigned long)size
, XC_PAGE_SIZE
- NET_IP_ALIGN
);
272 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
273 netdev
->rx_ring
.req_cons
= ++rc
;
275 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
277 rxreq
.gref
, PROT_WRITE
);
279 xen_be_printf(&netdev
->xendev
, 0, "error: rx gref dereference failed (%d)\n",
281 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
284 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
285 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
286 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
291 /* ------------------------------------------------------------- */
293 static NetClientInfo net_xen_info
= {
294 .type
= NET_CLIENT_TYPE_NIC
,
295 .size
= sizeof(NICState
),
296 .can_receive
= net_rx_ok
,
297 .receive
= net_rx_packet
,
300 static int net_init(struct XenDevice
*xendev
)
302 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
304 /* read xenstore entries */
305 if (netdev
->mac
== NULL
)
306 netdev
->mac
= xenstore_read_be_str(&netdev
->xendev
, "mac");
308 /* do we have all we need? */
309 if (netdev
->mac
== NULL
)
312 if (net_parse_macaddr(netdev
->conf
.macaddr
.a
, netdev
->mac
) < 0)
315 netdev
->conf
.vlan
= qemu_find_vlan(netdev
->xendev
.dev
, 1);
316 netdev
->conf
.peer
= NULL
;
318 netdev
->nic
= qemu_new_nic(&net_xen_info
, &netdev
->conf
,
319 "xen", NULL
, netdev
);
321 snprintf(netdev
->nic
->nc
.info_str
, sizeof(netdev
->nic
->nc
.info_str
),
322 "nic: xenbus vif macaddr=%s", netdev
->mac
);
325 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
326 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
331 static int net_connect(struct XenDevice
*xendev
)
333 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
336 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
337 &netdev
->tx_ring_ref
) == -1)
339 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
340 &netdev
->rx_ring_ref
) == -1)
342 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
343 &netdev
->xendev
.remote_port
) == -1)
346 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1)
349 xen_be_printf(&netdev
->xendev
, 0, "frontend doesn't support rx-copy.\n");
353 netdev
->txs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
356 PROT_READ
| PROT_WRITE
);
357 netdev
->rxs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
360 PROT_READ
| PROT_WRITE
);
361 if (!netdev
->txs
|| !netdev
->rxs
)
363 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XC_PAGE_SIZE
);
364 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XC_PAGE_SIZE
);
366 xen_be_bind_evtchn(&netdev
->xendev
);
368 xen_be_printf(&netdev
->xendev
, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
369 "remote port %d, local port %d\n",
370 netdev
->tx_ring_ref
, netdev
->rx_ring_ref
,
371 netdev
->xendev
.remote_port
, netdev
->xendev
.local_port
);
373 net_tx_packets(netdev
);
377 static void net_disconnect(struct XenDevice
*xendev
)
379 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
381 xen_be_unbind_evtchn(&netdev
->xendev
);
384 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
388 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->rxs
, 1);
392 qemu_del_vlan_client(&netdev
->nic
->nc
);
397 static void net_event(struct XenDevice
*xendev
)
399 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
400 net_tx_packets(netdev
);
403 static int net_free(struct XenDevice
*xendev
)
405 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
407 qemu_free(netdev
->mac
);
411 /* ------------------------------------------------------------- */
413 struct XenDevOps xen_netdev_ops
= {
414 .size
= sizeof(struct XenNetDev
),
415 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
417 .connect
= net_connect
,
419 .disconnect
= net_disconnect
,