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 "qemu-char.h"
43 #include "xen_backend.h"
45 #include <xen/io/netif.h>
47 /* ------------------------------------------------------------- */
50 struct XenDevice xendev
; /* must be first */
55 struct netif_tx_sring
*txs
;
56 struct netif_rx_sring
*rxs
;
57 netif_tx_back_ring_t tx_ring
;
58 netif_rx_back_ring_t rx_ring
;
63 /* ------------------------------------------------------------- */
65 static void net_tx_response(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, int8_t st
)
67 RING_IDX i
= netdev
->tx_ring
.rsp_prod_pvt
;
68 netif_tx_response_t
*resp
;
71 resp
= RING_GET_RESPONSE(&netdev
->tx_ring
, i
);
76 if (txp
->flags
& NETTXF_extra_info
) {
77 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
);
87 if (i
== netdev
->tx_ring
.req_cons
) {
89 RING_FINAL_CHECK_FOR_REQUESTS(&netdev
->tx_ring
, more_to_do
);
96 static void net_tx_error(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, RING_IDX end
)
100 * Hmm, why netback fails everything in the ring?
101 * Should we do that even when not supporting SG and TSO?
103 RING_IDX cons
= netdev
->tx_ring
.req_cons
;
106 make_tx_response(netif
, txp
, NETIF_RSP_ERROR
);
110 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
112 netdev
->tx_ring
.req_cons
= cons
;
113 netif_schedule_work(netif
);
116 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
120 static void net_tx_packets(struct XenNetDev
*netdev
)
122 netif_tx_request_t txreq
;
128 rc
= netdev
->tx_ring
.req_cons
;
129 rp
= netdev
->tx_ring
.sring
->req_prod
;
130 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
133 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
)) {
136 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
137 netdev
->tx_ring
.req_cons
= ++rc
;
140 /* should not happen in theory, we don't announce the *
141 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
142 if (txreq
.flags
& NETTXF_extra_info
) {
143 xen_be_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
144 net_tx_error(netdev
, &txreq
, rc
);
147 if (txreq
.flags
& NETTXF_more_data
) {
148 xen_be_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
149 net_tx_error(netdev
, &txreq
, rc
);
154 if (txreq
.size
< 14) {
155 xen_be_printf(&netdev
->xendev
, 0, "bad packet size: %d\n", txreq
.size
);
156 net_tx_error(netdev
, &txreq
, rc
);
160 if ((txreq
.offset
+ txreq
.size
) > XC_PAGE_SIZE
) {
161 xen_be_printf(&netdev
->xendev
, 0, "error: page crossing\n");
162 net_tx_error(netdev
, &txreq
, rc
);
166 xen_be_printf(&netdev
->xendev
, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
167 txreq
.gref
, txreq
.offset
, txreq
.size
, txreq
.flags
,
168 (txreq
.flags
& NETTXF_csum_blank
) ? " csum_blank" : "",
169 (txreq
.flags
& NETTXF_data_validated
) ? " data_validated" : "",
170 (txreq
.flags
& NETTXF_more_data
) ? " more_data" : "",
171 (txreq
.flags
& NETTXF_extra_info
) ? " extra_info" : "");
173 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
175 txreq
.gref
, PROT_READ
);
177 xen_be_printf(&netdev
->xendev
, 0, "error: tx gref dereference failed (%d)\n",
179 net_tx_error(netdev
, &txreq
, rc
);
182 if (txreq
.flags
& NETTXF_csum_blank
) {
183 /* have read-only mapping -> can't fill checksum in-place */
185 tmpbuf
= g_malloc(XC_PAGE_SIZE
);
187 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
188 net_checksum_calculate(tmpbuf
, txreq
.size
);
189 qemu_send_packet(&netdev
->nic
->nc
, tmpbuf
, txreq
.size
);
191 qemu_send_packet(&netdev
->nic
->nc
, 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 int net_rx_ok(VLANClientState
*nc
)
238 struct XenNetDev
*netdev
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
241 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
245 rc
= netdev
->rx_ring
.req_cons
;
246 rp
= netdev
->rx_ring
.sring
->req_prod
;
249 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
250 xen_be_printf(&netdev
->xendev
, 2, "%s: no rx buffers (%d/%d)\n",
251 __FUNCTION__
, rc
, rp
);
257 static ssize_t
net_rx_packet(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
259 struct XenNetDev
*netdev
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
260 netif_rx_request_t rxreq
;
264 if (netdev
->xendev
.be_state
!= XenbusStateConnected
) {
268 rc
= netdev
->rx_ring
.req_cons
;
269 rp
= netdev
->rx_ring
.sring
->req_prod
;
270 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
272 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
273 xen_be_printf(&netdev
->xendev
, 2, "no buffer, drop packet\n");
276 if (size
> XC_PAGE_SIZE
- NET_IP_ALIGN
) {
277 xen_be_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
278 (unsigned long)size
, XC_PAGE_SIZE
- NET_IP_ALIGN
);
282 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
283 netdev
->rx_ring
.req_cons
= ++rc
;
285 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
287 rxreq
.gref
, PROT_WRITE
);
289 xen_be_printf(&netdev
->xendev
, 0, "error: rx gref dereference failed (%d)\n",
291 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
294 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
295 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
296 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
301 /* ------------------------------------------------------------- */
303 static NetClientInfo net_xen_info
= {
304 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
305 .size
= sizeof(NICState
),
306 .can_receive
= net_rx_ok
,
307 .receive
= net_rx_packet
,
310 static int net_init(struct XenDevice
*xendev
)
312 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
314 /* read xenstore entries */
315 if (netdev
->mac
== NULL
) {
316 netdev
->mac
= xenstore_read_be_str(&netdev
->xendev
, "mac");
319 /* do we have all we need? */
320 if (netdev
->mac
== NULL
) {
324 if (net_parse_macaddr(netdev
->conf
.macaddr
.a
, netdev
->mac
) < 0) {
328 netdev
->conf
.vlan
= qemu_find_vlan(netdev
->xendev
.dev
, 1);
329 netdev
->conf
.peer
= NULL
;
331 netdev
->nic
= qemu_new_nic(&net_xen_info
, &netdev
->conf
,
332 "xen", NULL
, netdev
);
334 snprintf(netdev
->nic
->nc
.info_str
, sizeof(netdev
->nic
->nc
.info_str
),
335 "nic: xenbus vif macaddr=%s", netdev
->mac
);
338 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
339 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
344 static int net_connect(struct XenDevice
*xendev
)
346 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
349 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
350 &netdev
->tx_ring_ref
) == -1) {
353 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
354 &netdev
->rx_ring_ref
) == -1) {
357 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
358 &netdev
->xendev
.remote_port
) == -1) {
362 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1) {
366 xen_be_printf(&netdev
->xendev
, 0, "frontend doesn't support rx-copy.\n");
370 netdev
->txs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
373 PROT_READ
| PROT_WRITE
);
374 netdev
->rxs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
377 PROT_READ
| PROT_WRITE
);
378 if (!netdev
->txs
|| !netdev
->rxs
) {
381 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XC_PAGE_SIZE
);
382 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XC_PAGE_SIZE
);
384 xen_be_bind_evtchn(&netdev
->xendev
);
386 xen_be_printf(&netdev
->xendev
, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
387 "remote port %d, local port %d\n",
388 netdev
->tx_ring_ref
, netdev
->rx_ring_ref
,
389 netdev
->xendev
.remote_port
, netdev
->xendev
.local_port
);
391 net_tx_packets(netdev
);
395 static void net_disconnect(struct XenDevice
*xendev
)
397 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
399 xen_be_unbind_evtchn(&netdev
->xendev
);
402 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
406 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->rxs
, 1);
410 qemu_del_vlan_client(&netdev
->nic
->nc
);
415 static void net_event(struct XenDevice
*xendev
)
417 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
418 net_tx_packets(netdev
);
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
,