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, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
39 #include <xen/io/xenbus.h>
40 #include <xen/io/netif.h>
44 #include "qemu-char.h"
45 #include "xen_backend.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
;
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
;
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
);
84 if (i
== netdev
->tx_ring
.req_cons
) {
86 RING_FINAL_CHECK_FOR_REQUESTS(&netdev
->tx_ring
, more_to_do
);
92 static void net_tx_error(struct XenNetDev
*netdev
, netif_tx_request_t
*txp
, RING_IDX end
)
96 * Hmm, why netback fails everything in the ring?
97 * Should we do that even when not supporting SG and TSO?
99 RING_IDX cons
= netdev
->tx_ring
.req_cons
;
102 make_tx_response(netif
, txp
, NETIF_RSP_ERROR
);
105 txp
= RING_GET_REQUEST(&netdev
->tx_ring
, cons
++);
107 netdev
->tx_ring
.req_cons
= cons
;
108 netif_schedule_work(netif
);
111 net_tx_response(netdev
, txp
, NETIF_RSP_ERROR
);
115 static void net_tx_packets(struct XenNetDev
*netdev
)
117 netif_tx_request_t txreq
;
123 rc
= netdev
->tx_ring
.req_cons
;
124 rp
= netdev
->tx_ring
.sring
->req_prod
;
125 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
128 if (RING_REQUEST_CONS_OVERFLOW(&netdev
->tx_ring
, rc
))
130 memcpy(&txreq
, RING_GET_REQUEST(&netdev
->tx_ring
, rc
), sizeof(txreq
));
131 netdev
->tx_ring
.req_cons
= ++rc
;
134 /* should not happen in theory, we don't announce the *
135 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
136 if (txreq
.flags
& NETTXF_extra_info
) {
137 xen_be_printf(&netdev
->xendev
, 0, "FIXME: extra info flag\n");
138 net_tx_error(netdev
, &txreq
, rc
);
141 if (txreq
.flags
& NETTXF_more_data
) {
142 xen_be_printf(&netdev
->xendev
, 0, "FIXME: more data flag\n");
143 net_tx_error(netdev
, &txreq
, rc
);
148 if (txreq
.size
< 14) {
149 xen_be_printf(&netdev
->xendev
, 0, "bad packet size: %d\n", txreq
.size
);
150 net_tx_error(netdev
, &txreq
, rc
);
154 if ((txreq
.offset
+ txreq
.size
) > XC_PAGE_SIZE
) {
155 xen_be_printf(&netdev
->xendev
, 0, "error: page crossing\n");
156 net_tx_error(netdev
, &txreq
, rc
);
160 xen_be_printf(&netdev
->xendev
, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
161 txreq
.gref
, txreq
.offset
, txreq
.size
, txreq
.flags
,
162 (txreq
.flags
& NETTXF_csum_blank
) ? " csum_blank" : "",
163 (txreq
.flags
& NETTXF_data_validated
) ? " data_validated" : "",
164 (txreq
.flags
& NETTXF_more_data
) ? " more_data" : "",
165 (txreq
.flags
& NETTXF_extra_info
) ? " extra_info" : "");
167 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
169 txreq
.gref
, PROT_READ
);
171 xen_be_printf(&netdev
->xendev
, 0, "error: tx gref dereference failed (%d)\n",
173 net_tx_error(netdev
, &txreq
, rc
);
176 if (txreq
.flags
& NETTXF_csum_blank
) {
177 /* have read-only mapping -> can't fill checksum in-place */
179 tmpbuf
= qemu_malloc(XC_PAGE_SIZE
);
180 memcpy(tmpbuf
, page
+ txreq
.offset
, txreq
.size
);
181 net_checksum_calculate(tmpbuf
, txreq
.size
);
182 qemu_send_packet(netdev
->vs
, tmpbuf
, txreq
.size
);
184 qemu_send_packet(netdev
->vs
, page
+ txreq
.offset
, txreq
.size
);
186 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
187 net_tx_response(netdev
, &txreq
, NETIF_RSP_OKAY
);
189 if (!netdev
->tx_work
)
196 /* ------------------------------------------------------------- */
198 static void net_rx_response(struct XenNetDev
*netdev
,
199 netif_rx_request_t
*req
, int8_t st
,
200 uint16_t offset
, uint16_t size
,
203 RING_IDX i
= netdev
->rx_ring
.rsp_prod_pvt
;
204 netif_rx_response_t
*resp
;
207 resp
= RING_GET_RESPONSE(&netdev
->rx_ring
, i
);
208 resp
->offset
= offset
;
211 resp
->status
= (int16_t)size
;
213 resp
->status
= (int16_t)st
;
215 xen_be_printf(&netdev
->xendev
, 3, "rx response: idx %d, status %d, flags 0x%x\n",
216 i
, resp
->status
, resp
->flags
);
218 netdev
->rx_ring
.rsp_prod_pvt
= ++i
;
219 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev
->rx_ring
, notify
);
221 xen_be_send_notify(&netdev
->xendev
);
224 #define NET_IP_ALIGN 2
226 static int net_rx_ok(VLANClientState
*vc
)
228 struct XenNetDev
*netdev
= vc
->opaque
;
231 if (netdev
->xendev
.be_state
!= XenbusStateConnected
)
234 rc
= netdev
->rx_ring
.req_cons
;
235 rp
= netdev
->rx_ring
.sring
->req_prod
;
238 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
239 xen_be_printf(&netdev
->xendev
, 2, "%s: no rx buffers (%d/%d)\n",
240 __FUNCTION__
, rc
, rp
);
246 static ssize_t
net_rx_packet(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
248 struct XenNetDev
*netdev
= vc
->opaque
;
249 netif_rx_request_t rxreq
;
253 if (netdev
->xendev
.be_state
!= XenbusStateConnected
)
256 rc
= netdev
->rx_ring
.req_cons
;
257 rp
= netdev
->rx_ring
.sring
->req_prod
;
258 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
260 if (rc
== rp
|| RING_REQUEST_CONS_OVERFLOW(&netdev
->rx_ring
, rc
)) {
261 xen_be_printf(&netdev
->xendev
, 2, "no buffer, drop packet\n");
264 if (size
> XC_PAGE_SIZE
- NET_IP_ALIGN
) {
265 xen_be_printf(&netdev
->xendev
, 0, "packet too big (%lu > %ld)",
266 (unsigned long)size
, XC_PAGE_SIZE
- NET_IP_ALIGN
);
270 memcpy(&rxreq
, RING_GET_REQUEST(&netdev
->rx_ring
, rc
), sizeof(rxreq
));
271 netdev
->rx_ring
.req_cons
= ++rc
;
273 page
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
275 rxreq
.gref
, PROT_WRITE
);
277 xen_be_printf(&netdev
->xendev
, 0, "error: rx gref dereference failed (%d)\n",
279 net_rx_response(netdev
, &rxreq
, NETIF_RSP_ERROR
, 0, 0, 0);
282 memcpy(page
+ NET_IP_ALIGN
, buf
, size
);
283 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, page
, 1);
284 net_rx_response(netdev
, &rxreq
, NETIF_RSP_OKAY
, NET_IP_ALIGN
, size
, 0);
289 /* ------------------------------------------------------------- */
291 static int net_init(struct XenDevice
*xendev
)
293 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
296 /* read xenstore entries */
297 if (netdev
->mac
== NULL
)
298 netdev
->mac
= xenstore_read_be_str(&netdev
->xendev
, "mac");
300 /* do we have all we need? */
301 if (netdev
->mac
== NULL
)
304 vlan
= qemu_find_vlan(netdev
->xendev
.dev
, 1);
305 netdev
->vs
= qemu_new_vlan_client(vlan
, "xen", NULL
,
306 net_rx_ok
, net_rx_packet
, NULL
,
308 snprintf(netdev
->vs
->info_str
, sizeof(netdev
->vs
->info_str
),
309 "nic: xenbus vif macaddr=%s", netdev
->mac
);
312 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-copy", 1);
313 xenstore_write_be_int(&netdev
->xendev
, "feature-rx-flip", 0);
318 static int net_connect(struct XenDevice
*xendev
)
320 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
323 if (xenstore_read_fe_int(&netdev
->xendev
, "tx-ring-ref",
324 &netdev
->tx_ring_ref
) == -1)
326 if (xenstore_read_fe_int(&netdev
->xendev
, "rx-ring-ref",
327 &netdev
->rx_ring_ref
) == -1)
329 if (xenstore_read_fe_int(&netdev
->xendev
, "event-channel",
330 &netdev
->xendev
.remote_port
) == -1)
333 if (xenstore_read_fe_int(&netdev
->xendev
, "request-rx-copy", &rx_copy
) == -1)
336 xen_be_printf(&netdev
->xendev
, 0, "frontend doesn't support rx-copy.\n");
340 netdev
->txs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
343 PROT_READ
| PROT_WRITE
);
344 netdev
->rxs
= xc_gnttab_map_grant_ref(netdev
->xendev
.gnttabdev
,
347 PROT_READ
| PROT_WRITE
);
348 if (!netdev
->txs
|| !netdev
->rxs
)
350 BACK_RING_INIT(&netdev
->tx_ring
, netdev
->txs
, XC_PAGE_SIZE
);
351 BACK_RING_INIT(&netdev
->rx_ring
, netdev
->rxs
, XC_PAGE_SIZE
);
353 xen_be_bind_evtchn(&netdev
->xendev
);
355 xen_be_printf(&netdev
->xendev
, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
356 "remote port %d, local port %d\n",
357 netdev
->tx_ring_ref
, netdev
->rx_ring_ref
,
358 netdev
->xendev
.remote_port
, netdev
->xendev
.local_port
);
360 net_tx_packets(netdev
);
364 static void net_disconnect(struct XenDevice
*xendev
)
366 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
368 xen_be_unbind_evtchn(&netdev
->xendev
);
371 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->txs
, 1);
375 xc_gnttab_munmap(netdev
->xendev
.gnttabdev
, netdev
->rxs
, 1);
379 qemu_del_vlan_client(netdev
->vs
);
384 static void net_event(struct XenDevice
*xendev
)
386 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
387 net_tx_packets(netdev
);
390 static int net_free(struct XenDevice
*xendev
)
392 struct XenNetDev
*netdev
= container_of(xendev
, struct XenNetDev
, xendev
);
394 qemu_free(netdev
->mac
);
398 /* ------------------------------------------------------------- */
400 struct XenDevOps xen_netdev_ops
= {
401 .size
= sizeof(struct XenNetDev
),
402 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
404 .connect
= net_connect
,
406 .disconnect
= net_disconnect
,