2009-04-22 19:19:35 +04:00
|
|
|
/*
|
|
|
|
* xen paravirt network card backend
|
|
|
|
*
|
|
|
|
* (c) Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; under version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2009-07-17 00:47:01 +04:00
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
2012-01-13 20:44:23 +04:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-04-22 19:19:35 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:06 +03:00
|
|
|
#include "qemu/osdep.h"
|
2023-10-17 15:58:03 +03:00
|
|
|
#include "qemu/main-loop.h"
|
|
|
|
#include "qemu/cutils.h"
|
|
|
|
#include "qemu/log.h"
|
|
|
|
#include "qemu/qemu-print.h"
|
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
|
2009-04-22 19:19:35 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2012-10-24 10:43:34 +04:00
|
|
|
#include "net/net.h"
|
2009-10-22 20:49:03 +04:00
|
|
|
#include "net/checksum.h"
|
2009-11-25 21:49:28 +03:00
|
|
|
#include "net/util.h"
|
2023-10-17 15:58:03 +03:00
|
|
|
|
|
|
|
#include "hw/xen/xen-backend.h"
|
|
|
|
#include "hw/xen/xen-bus-helper.h"
|
|
|
|
#include "hw/qdev-properties.h"
|
|
|
|
#include "hw/qdev-properties-system.h"
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2019-06-21 13:54:41 +03:00
|
|
|
#include "hw/xen/interface/io/netif.h"
|
2023-10-17 15:58:03 +03:00
|
|
|
#include "hw/xen/interface/io/xs_wire.h"
|
|
|
|
|
|
|
|
#include "trace.h"
|
2012-06-21 15:43:59 +04:00
|
|
|
|
2009-04-22 19:19:35 +04:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct XenNetDev {
|
2023-10-17 15:58:03 +03:00
|
|
|
struct XenDevice xendev; /* must be first */
|
|
|
|
XenEventChannel *event_channel;
|
|
|
|
int dev;
|
2009-04-22 19:19:35 +04:00
|
|
|
int tx_work;
|
2023-10-17 15:58:03 +03:00
|
|
|
unsigned int tx_ring_ref;
|
|
|
|
unsigned int rx_ring_ref;
|
2009-04-22 19:19:35 +04:00
|
|
|
struct netif_tx_sring *txs;
|
|
|
|
struct netif_rx_sring *rxs;
|
|
|
|
netif_tx_back_ring_t tx_ring;
|
|
|
|
netif_rx_back_ring_t rx_ring;
|
2009-11-25 21:49:28 +03:00
|
|
|
NICConf conf;
|
|
|
|
NICState *nic;
|
2009-04-22 19:19:35 +04:00
|
|
|
};
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
typedef struct XenNetDev XenNetDev;
|
|
|
|
|
|
|
|
#define TYPE_XEN_NET_DEVICE "xen-net-device"
|
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(XenNetDev, XEN_NET_DEVICE)
|
|
|
|
|
2009-04-22 19:19:35 +04:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st)
|
|
|
|
{
|
|
|
|
RING_IDX i = netdev->tx_ring.rsp_prod_pvt;
|
|
|
|
netif_tx_response_t *resp;
|
|
|
|
int notify;
|
|
|
|
|
|
|
|
resp = RING_GET_RESPONSE(&netdev->tx_ring, i);
|
|
|
|
resp->id = txp->id;
|
|
|
|
resp->status = st;
|
|
|
|
|
|
|
|
#if 0
|
2010-09-23 15:28:45 +04:00
|
|
|
if (txp->flags & NETTXF_extra_info) {
|
|
|
|
RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
netdev->tx_ring.rsp_prod_pvt = ++i;
|
|
|
|
RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify);
|
2010-09-23 15:28:45 +04:00
|
|
|
if (notify) {
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_notify_event_channel(XEN_DEVICE(netdev),
|
|
|
|
netdev->event_channel, NULL);
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
|
|
|
|
if (i == netdev->tx_ring.req_cons) {
|
2010-09-23 15:28:45 +04:00
|
|
|
int more_to_do;
|
|
|
|
RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do);
|
|
|
|
if (more_to_do) {
|
|
|
|
netdev->tx_work++;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Hmm, why netback fails everything in the ring?
|
|
|
|
* Should we do that even when not supporting SG and TSO?
|
|
|
|
*/
|
|
|
|
RING_IDX cons = netdev->tx_ring.req_cons;
|
|
|
|
|
|
|
|
do {
|
2010-09-23 15:28:45 +04:00
|
|
|
make_tx_response(netif, txp, NETIF_RSP_ERROR);
|
|
|
|
if (cons >= end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
|
2009-04-22 19:19:35 +04:00
|
|
|
} while (1);
|
|
|
|
netdev->tx_ring.req_cons = cons;
|
|
|
|
netif_schedule_work(netif);
|
|
|
|
netif_put(netif);
|
|
|
|
#else
|
|
|
|
net_tx_response(netdev, txp, NETIF_RSP_ERROR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
static bool net_tx_packets(struct XenNetDev *netdev)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2023-10-17 15:58:03 +03:00
|
|
|
bool done_something = false;
|
2009-04-22 19:19:35 +04:00
|
|
|
netif_tx_request_t txreq;
|
|
|
|
RING_IDX rc, rp;
|
|
|
|
void *page;
|
|
|
|
void *tmpbuf = NULL;
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
assert(qemu_mutex_iothread_locked());
|
|
|
|
|
2009-04-22 19:19:35 +04:00
|
|
|
for (;;) {
|
2010-09-23 15:28:45 +04:00
|
|
|
rc = netdev->tx_ring.req_cons;
|
|
|
|
rp = netdev->tx_ring.sring->req_prod;
|
|
|
|
xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2010-09-23 15:28:45 +04:00
|
|
|
while ((rc != rp)) {
|
|
|
|
if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
|
|
|
|
netdev->tx_ring.req_cons = ++rc;
|
2023-10-17 15:58:03 +03:00
|
|
|
done_something = true;
|
2009-04-22 19:19:35 +04:00
|
|
|
|
|
|
|
#if 1
|
2010-09-23 15:28:45 +04:00
|
|
|
/* should not happen in theory, we don't announce the *
|
|
|
|
* feature-{sg,gso,whatelse} flags in xenstore (yet?) */
|
|
|
|
if (txreq.flags & NETTXF_extra_info) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_UNIMP, "vif%u: FIXME: extra info flag\n",
|
|
|
|
netdev->dev);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_tx_error(netdev, &txreq, rc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (txreq.flags & NETTXF_more_data) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_UNIMP, "vif%u: FIXME: more data flag\n",
|
|
|
|
netdev->dev);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_tx_error(netdev, &txreq, rc);
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
#endif
|
|
|
|
|
2010-09-23 15:28:45 +04:00
|
|
|
if (txreq.size < 14) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "vif%u: bad packet size: %d\n",
|
|
|
|
netdev->dev, txreq.size);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_tx_error(netdev, &txreq, rc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-07 19:47:43 +03:00
|
|
|
if ((txreq.offset + txreq.size) > XEN_PAGE_SIZE) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "vif%u: error: page crossing\n",
|
|
|
|
netdev->dev);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_tx_error(netdev, &txreq, rc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
trace_xen_netdev_tx(netdev->dev, txreq.gref, txreq.offset,
|
|
|
|
txreq.size, txreq.flags,
|
|
|
|
(txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "",
|
|
|
|
(txreq.flags & NETTXF_data_validated) ? " data_validated" : "",
|
|
|
|
(txreq.flags & NETTXF_more_data) ? " more_data" : "",
|
|
|
|
(txreq.flags & NETTXF_extra_info) ? " extra_info" : "");
|
2010-09-23 15:28:45 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
page = xen_device_map_grant_refs(&netdev->xendev, &txreq.gref, 1,
|
|
|
|
PROT_READ, NULL);
|
2010-09-23 15:28:45 +04:00
|
|
|
if (page == NULL) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"vif%u: tx gref dereference failed (%d)\n",
|
|
|
|
netdev->dev, txreq.gref);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_tx_error(netdev, &txreq, rc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (txreq.flags & NETTXF_csum_blank) {
|
2009-04-22 19:19:35 +04:00
|
|
|
/* have read-only mapping -> can't fill checksum in-place */
|
2010-09-23 15:28:45 +04:00
|
|
|
if (!tmpbuf) {
|
2023-01-07 19:47:43 +03:00
|
|
|
tmpbuf = g_malloc(XEN_PAGE_SIZE);
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
memcpy(tmpbuf, page + txreq.offset, txreq.size);
|
2020-12-11 12:35:12 +03:00
|
|
|
net_checksum_calculate(tmpbuf, txreq.size, CSUM_ALL);
|
2013-01-30 15:12:22 +04:00
|
|
|
qemu_send_packet(qemu_get_queue(netdev->nic), tmpbuf,
|
|
|
|
txreq.size);
|
2009-04-22 19:19:35 +04:00
|
|
|
} else {
|
2013-01-30 15:12:22 +04:00
|
|
|
qemu_send_packet(qemu_get_queue(netdev->nic),
|
|
|
|
page + txreq.offset, txreq.size);
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_unmap_grant_refs(&netdev->xendev, page, &txreq.gref, 1,
|
|
|
|
NULL);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
|
|
|
|
}
|
|
|
|
if (!netdev->tx_work) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
netdev->tx_work = 0;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(tmpbuf);
|
2023-10-17 15:58:03 +03:00
|
|
|
return done_something;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void net_rx_response(struct XenNetDev *netdev,
|
2010-09-23 15:28:45 +04:00
|
|
|
netif_rx_request_t *req, int8_t st,
|
|
|
|
uint16_t offset, uint16_t size,
|
|
|
|
uint16_t flags)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
|
|
|
RING_IDX i = netdev->rx_ring.rsp_prod_pvt;
|
|
|
|
netif_rx_response_t *resp;
|
|
|
|
int notify;
|
|
|
|
|
|
|
|
resp = RING_GET_RESPONSE(&netdev->rx_ring, i);
|
|
|
|
resp->offset = offset;
|
|
|
|
resp->flags = flags;
|
|
|
|
resp->id = req->id;
|
|
|
|
resp->status = (int16_t)size;
|
2010-09-23 15:28:45 +04:00
|
|
|
if (st < 0) {
|
|
|
|
resp->status = (int16_t)st;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
trace_xen_netdev_rx(netdev->dev, i, resp->status, resp->flags);
|
2009-04-22 19:19:35 +04:00
|
|
|
|
|
|
|
netdev->rx_ring.rsp_prod_pvt = ++i;
|
|
|
|
RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify);
|
2010-09-23 15:28:45 +04:00
|
|
|
if (notify) {
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_notify_event_channel(XEN_DEVICE(netdev),
|
|
|
|
netdev->event_channel, NULL);
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NET_IP_ALIGN 2
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2013-01-30 15:12:23 +04:00
|
|
|
struct XenNetDev *netdev = qemu_get_nic_opaque(nc);
|
2009-04-22 19:19:35 +04:00
|
|
|
netif_rx_request_t rxreq;
|
|
|
|
RING_IDX rc, rp;
|
|
|
|
void *page;
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
assert(qemu_mutex_iothread_locked());
|
|
|
|
|
|
|
|
if (xen_device_backend_get_state(&netdev->xendev) != XenbusStateConnected) {
|
2010-09-23 15:28:45 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
|
|
|
|
rc = netdev->rx_ring.req_cons;
|
|
|
|
rp = netdev->rx_ring.sring->req_prod;
|
|
|
|
xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
|
|
|
|
|
|
|
if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
|
2015-07-28 12:52:56 +03:00
|
|
|
return 0;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
2023-01-07 19:47:43 +03:00
|
|
|
if (size > XEN_PAGE_SIZE - NET_IP_ALIGN) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "vif%u: packet too big (%lu > %ld)",
|
|
|
|
netdev->dev, (unsigned long)size,
|
|
|
|
XEN_PAGE_SIZE - NET_IP_ALIGN);
|
2010-09-23 15:28:45 +04:00
|
|
|
return -1;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
|
|
|
|
netdev->rx_ring.req_cons = ++rc;
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
page = xen_device_map_grant_refs(&netdev->xendev, &rxreq.gref, 1,
|
|
|
|
PROT_WRITE, NULL);
|
2009-04-22 19:19:35 +04:00
|
|
|
if (page == NULL) {
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"vif%u: rx gref dereference failed (%d)\n",
|
|
|
|
netdev->dev, rxreq.gref);
|
2010-09-23 15:28:45 +04:00
|
|
|
net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0);
|
|
|
|
return -1;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
memcpy(page + NET_IP_ALIGN, buf, size);
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_unmap_grant_refs(&netdev->xendev, page, &rxreq.gref, 1, NULL);
|
2009-04-22 19:19:35 +04:00
|
|
|
net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
|
2009-05-18 16:40:55 +04:00
|
|
|
|
|
|
|
return size;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
2009-11-25 21:49:28 +03:00
|
|
|
static NetClientInfo net_xen_info = {
|
qapi: Change Netdev into a flat union
This is a mostly-mechanical conversion that creates a new flat
union 'Netdev' QAPI type that covers all the branches of the
former 'NetClientOptions' simple union, where the branches are
now listed in a new 'NetClientDriver' enum rather than generated
from the simple union. The existence of a flat union has no
change to the command line syntax accepted for new code, and
will make it possible for a future patch to switch the QMP
command to parse a boxed union for no change to valid QMP; but
it does have some ripple effect on the C code when dealing with
the new types.
While making the conversion, note that the 'NetLegacy' type
remains unchanged: it applies only to legacy command line options,
and will not be ported to QMP, so it should remain a wrapper
around a simple union; to avoid confusion, the type named
'NetClientOptions' is now gone, and we introduce 'NetLegacyOptions'
in its place. Then, in the C code, we convert from NetLegacy to
Netdev as soon as possible, so that the bulk of the net stack
only has to deal with one QAPI type, not two. Note that since
the old legacy code always rejected 'hubport', we can just omit
that branch from the new 'NetLegacyOptions' simple union.
Based on an idea originally by Zoltán Kővágó <DirtY.iCE.hu@gmail.com>:
Message-Id: <01a527fbf1a5de880091f98cf011616a78adeeee.1441627176.git.DirtY.iCE.hu@gmail.com>
although the sed script in that patch no longer applies due to
other changes in the tree since then, and I also did some manual
cleanups (such as fixing whitespace to keep checkpatch happy).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1468468228-27827-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Fixup from Eric squashed in]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-07-14 06:50:23 +03:00
|
|
|
.type = NET_CLIENT_DRIVER_NIC,
|
2009-11-25 21:49:28 +03:00
|
|
|
.size = sizeof(NICState),
|
|
|
|
.receive = net_rx_packet,
|
|
|
|
};
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
static void xen_netdev_realize(XenDevice *xendev, Error **errp)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2023-10-17 15:58:03 +03:00
|
|
|
ERRP_GUARD();
|
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(xendev);
|
|
|
|
NetClientState *nc;
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
qemu_macaddr_default_if_unset(&netdev->conf.macaddr);
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_frontend_printf(xendev, "mac", "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
netdev->conf.macaddr.a[0],
|
|
|
|
netdev->conf.macaddr.a[1],
|
|
|
|
netdev->conf.macaddr.a[2],
|
|
|
|
netdev->conf.macaddr.a[3],
|
|
|
|
netdev->conf.macaddr.a[4],
|
|
|
|
netdev->conf.macaddr.a[5]);
|
2009-11-25 21:49:28 +03:00
|
|
|
|
|
|
|
netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
|
2023-10-17 15:58:03 +03:00
|
|
|
object_get_typename(OBJECT(xendev)),
|
2023-06-01 06:18:58 +03:00
|
|
|
DEVICE(xendev)->id,
|
|
|
|
&xendev->qdev.mem_reentrancy_guard, netdev);
|
2009-11-25 21:49:28 +03:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
nc = qemu_get_queue(netdev->nic);
|
|
|
|
qemu_format_nic_info_str(nc, netdev->conf.macaddr.a);
|
2009-04-22 19:19:35 +04:00
|
|
|
|
|
|
|
/* fill info */
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_backend_printf(xendev, "feature-rx-copy", "%u", 1);
|
|
|
|
xen_device_backend_printf(xendev, "feature-rx-flip", "%u", 0);
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
trace_xen_netdev_realize(netdev->dev, nc->info_str, nc->peer ?
|
|
|
|
nc->peer->name : "(none)");
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
static bool net_event(void *_xendev)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2023-10-17 15:58:03 +03:00
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(_xendev);
|
|
|
|
bool done_something;
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
done_something = net_tx_packets(netdev);
|
|
|
|
qemu_flush_queued_packets(qemu_get_queue(netdev->nic));
|
|
|
|
return done_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xen_netdev_connect(XenDevice *xendev, Error **errp)
|
|
|
|
{
|
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(xendev);
|
|
|
|
unsigned int port, rx_copy;
|
|
|
|
|
|
|
|
assert(qemu_mutex_iothread_locked());
|
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, "tx-ring-ref", "%u",
|
|
|
|
&netdev->tx_ring_ref) != 1) {
|
|
|
|
error_setg(errp, "failed to read tx-ring-ref");
|
|
|
|
return false;
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2023-10-17 15:58:03 +03:00
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, "rx-ring-ref", "%u",
|
|
|
|
&netdev->rx_ring_ref) != 1) {
|
|
|
|
error_setg(errp, "failed to read rx-ring-ref");
|
|
|
|
return false;
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2023-10-17 15:58:03 +03:00
|
|
|
|
|
|
|
if (xen_device_frontend_scanf(xendev, "event-channel", "%u",
|
|
|
|
&port) != 1) {
|
|
|
|
error_setg(errp, "failed to read event-channel");
|
|
|
|
return false;
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
if (xen_device_frontend_scanf(xendev, "request-rx-copy", "%u",
|
|
|
|
&rx_copy) != 1) {
|
2010-09-23 15:28:45 +04:00
|
|
|
rx_copy = 0;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
if (rx_copy == 0) {
|
2023-10-17 15:58:03 +03:00
|
|
|
error_setg(errp, "frontend doesn't support rx-copy");
|
|
|
|
return false;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
netdev->txs = xen_device_map_grant_refs(xendev,
|
|
|
|
&netdev->tx_ring_ref, 1,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
errp);
|
2014-12-16 23:48:54 +03:00
|
|
|
if (!netdev->txs) {
|
2023-10-17 15:58:03 +03:00
|
|
|
error_prepend(errp, "failed to map tx grant ref: ");
|
|
|
|
return false;
|
2014-12-16 23:48:54 +03:00
|
|
|
}
|
2023-10-17 15:58:03 +03:00
|
|
|
|
|
|
|
netdev->rxs = xen_device_map_grant_refs(xendev,
|
|
|
|
&netdev->rx_ring_ref, 1,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
errp);
|
2014-12-16 23:48:54 +03:00
|
|
|
if (!netdev->rxs) {
|
2023-10-17 15:58:03 +03:00
|
|
|
error_prepend(errp, "failed to map rx grant ref: ");
|
|
|
|
return false;
|
2010-09-23 15:28:45 +04:00
|
|
|
}
|
2023-10-17 15:58:03 +03:00
|
|
|
|
2023-01-07 19:47:43 +03:00
|
|
|
BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XEN_PAGE_SIZE);
|
|
|
|
BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XEN_PAGE_SIZE);
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
netdev->event_channel = xen_device_bind_event_channel(xendev, port,
|
|
|
|
net_event,
|
|
|
|
netdev,
|
|
|
|
errp);
|
|
|
|
if (!netdev->event_channel) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
trace_xen_netdev_connect(netdev->dev, netdev->tx_ring_ref,
|
|
|
|
netdev->rx_ring_ref, port);
|
2009-06-11 13:32:51 +04:00
|
|
|
|
|
|
|
net_tx_packets(netdev);
|
2023-10-17 15:58:03 +03:00
|
|
|
return true;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
static void xen_netdev_disconnect(XenDevice *xendev, Error **errp)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2023-10-17 15:58:03 +03:00
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(xendev);
|
|
|
|
|
|
|
|
trace_xen_netdev_disconnect(netdev->dev);
|
|
|
|
|
|
|
|
assert(qemu_mutex_iothread_locked());
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
netdev->tx_ring.sring = NULL;
|
|
|
|
netdev->rx_ring.sring = NULL;
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
if (netdev->event_channel) {
|
|
|
|
xen_device_unbind_event_channel(xendev, netdev->event_channel,
|
|
|
|
errp);
|
|
|
|
netdev->event_channel = NULL;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
if (netdev->txs) {
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_unmap_grant_refs(xendev, netdev->txs,
|
|
|
|
&netdev->tx_ring_ref, 1, errp);
|
2010-09-23 15:28:45 +04:00
|
|
|
netdev->txs = NULL;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
if (netdev->rxs) {
|
2023-10-17 15:58:03 +03:00
|
|
|
xen_device_unmap_grant_refs(xendev, netdev->rxs,
|
|
|
|
&netdev->rx_ring_ref, 1, errp);
|
2010-09-23 15:28:45 +04:00
|
|
|
netdev->rxs = NULL;
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void xen_netdev_frontend_changed(XenDevice *xendev,
|
|
|
|
enum xenbus_state frontend_state,
|
|
|
|
Error **errp)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2023-10-17 15:58:03 +03:00
|
|
|
ERRP_GUARD();
|
|
|
|
enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
|
|
|
|
|
|
|
|
trace_xen_netdev_frontend_changed(xendev->name, frontend_state);
|
|
|
|
|
|
|
|
switch (frontend_state) {
|
|
|
|
case XenbusStateConnected:
|
|
|
|
if (backend_state == XenbusStateConnected) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_netdev_disconnect(xendev, errp);
|
|
|
|
if (*errp) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!xen_netdev_connect(xendev, errp)) {
|
|
|
|
xen_netdev_disconnect(xendev, NULL);
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateClosing);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateConnected);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XenbusStateClosing:
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateClosing);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XenbusStateClosed:
|
|
|
|
case XenbusStateUnknown:
|
|
|
|
xen_netdev_disconnect(xendev, errp);
|
|
|
|
if (*errp) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_device_backend_set_state(xendev, XenbusStateClosed);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XenbusStateInitialised:
|
|
|
|
/*
|
|
|
|
* Linux netback does nothing on the frontend going (back) to
|
|
|
|
* XenbusStateInitialised, so do the same here.
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
static char *xen_netdev_get_name(XenDevice *xendev, Error **errp)
|
2009-04-22 19:19:35 +04:00
|
|
|
{
|
2023-10-17 15:58:03 +03:00
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(xendev);
|
|
|
|
|
|
|
|
if (netdev->dev == -1) {
|
|
|
|
XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
|
|
|
|
char fe_path[XENSTORE_ABS_PATH_MAX + 1];
|
|
|
|
int idx = (xen_mode == XEN_EMULATE) ? 0 : 1;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
/* Theoretically we could go up to INT_MAX here but that's overkill */
|
|
|
|
while (idx < 100) {
|
|
|
|
snprintf(fe_path, sizeof(fe_path),
|
|
|
|
"/local/domain/%u/device/vif/%u",
|
|
|
|
xendev->frontend_id, idx);
|
|
|
|
value = qemu_xen_xs_read(xenbus->xsh, XBT_NULL, fe_path, NULL);
|
|
|
|
if (!value) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
netdev->dev = idx;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
error_setg(errp, "cannot read %s: %s", fe_path,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free(value);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
error_setg(errp, "cannot find device index for netdev device");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
found:
|
|
|
|
return g_strdup_printf("%u", netdev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_netdev_unrealize(XenDevice *xendev)
|
|
|
|
{
|
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(xendev);
|
|
|
|
|
|
|
|
trace_xen_netdev_unrealize(netdev->dev);
|
|
|
|
|
|
|
|
/* Disconnect from the frontend in case this has not already happened */
|
|
|
|
xen_netdev_disconnect(xendev, NULL);
|
2009-04-22 19:19:35 +04:00
|
|
|
|
2014-12-16 23:52:16 +03:00
|
|
|
if (netdev->nic) {
|
|
|
|
qemu_del_nic(netdev->nic);
|
|
|
|
}
|
2009-04-22 19:19:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
2023-10-17 15:58:03 +03:00
|
|
|
static Property xen_netdev_properties[] = {
|
|
|
|
DEFINE_NIC_PROPERTIES(XenNetDev, conf),
|
|
|
|
DEFINE_PROP_INT32("idx", XenNetDev, dev, -1),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_netdev_class_init(ObjectClass *class, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dev_class = DEVICE_CLASS(class);
|
|
|
|
XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
|
|
|
|
|
|
|
|
xendev_class->backend = "qnic";
|
|
|
|
xendev_class->device = "vif";
|
|
|
|
xendev_class->get_name = xen_netdev_get_name;
|
|
|
|
xendev_class->realize = xen_netdev_realize;
|
|
|
|
xendev_class->frontend_changed = xen_netdev_frontend_changed;
|
|
|
|
xendev_class->unrealize = xen_netdev_unrealize;
|
|
|
|
set_bit(DEVICE_CATEGORY_NETWORK, dev_class->categories);
|
|
|
|
dev_class->user_creatable = true;
|
|
|
|
|
|
|
|
device_class_set_props(dev_class, xen_netdev_properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo xen_net_type_info = {
|
|
|
|
.name = TYPE_XEN_NET_DEVICE,
|
|
|
|
.parent = TYPE_XEN_DEVICE,
|
|
|
|
.instance_size = sizeof(XenNetDev),
|
|
|
|
.class_init = xen_netdev_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xen_net_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&xen_net_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(xen_net_register_types)
|
|
|
|
|
|
|
|
/* Called to instantiate a XenNetDev when the backend is detected. */
|
|
|
|
static void xen_net_device_create(XenBackendInstance *backend,
|
|
|
|
QDict *opts, Error **errp)
|
|
|
|
{
|
|
|
|
ERRP_GUARD();
|
|
|
|
XenBus *xenbus = xen_backend_get_bus(backend);
|
|
|
|
const char *name = xen_backend_get_name(backend);
|
|
|
|
XenDevice *xendev = NULL;
|
|
|
|
unsigned long number;
|
|
|
|
const char *macstr;
|
|
|
|
XenNetDev *net;
|
|
|
|
MACAddr mac;
|
|
|
|
|
|
|
|
if (qemu_strtoul(name, NULL, 10, &number) || number >= INT_MAX) {
|
|
|
|
error_setg(errp, "failed to parse name '%s'", name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_xen_netdev_create(number);
|
|
|
|
|
|
|
|
macstr = qdict_get_try_str(opts, "mac");
|
|
|
|
if (macstr == NULL) {
|
|
|
|
error_setg(errp, "no MAC address found");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (net_parse_macaddr(mac.a, macstr) < 0) {
|
|
|
|
error_setg(errp, "failed to parse MAC address");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
xendev = XEN_DEVICE(qdev_new(TYPE_XEN_NET_DEVICE));
|
|
|
|
net = XEN_NET_DEVICE(xendev);
|
|
|
|
|
|
|
|
net->dev = number;
|
|
|
|
memcpy(&net->conf.macaddr, &mac, sizeof(mac));
|
|
|
|
|
|
|
|
if (qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
|
|
|
|
xen_backend_set_device(backend, xendev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_prepend(errp, "realization of net device %lu failed: ",
|
|
|
|
number);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (xendev) {
|
|
|
|
object_unparent(OBJECT(xendev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xen_net_device_destroy(XenBackendInstance *backend,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
ERRP_GUARD();
|
|
|
|
XenDevice *xendev = xen_backend_get_device(backend);
|
|
|
|
XenNetDev *netdev = XEN_NET_DEVICE(xendev);
|
|
|
|
|
|
|
|
trace_xen_netdev_destroy(netdev->dev);
|
|
|
|
|
|
|
|
object_unparent(OBJECT(xendev));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const XenBackendInfo xen_net_backend_info = {
|
|
|
|
.type = "qnic",
|
|
|
|
.create = xen_net_device_create,
|
|
|
|
.destroy = xen_net_device_destroy,
|
2009-04-22 19:19:35 +04:00
|
|
|
};
|
2023-10-17 15:58:03 +03:00
|
|
|
|
|
|
|
static void xen_net_register_backend(void)
|
|
|
|
{
|
|
|
|
xen_backend_register(&xen_net_backend_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
xen_backend_init(xen_net_register_backend);
|