From nobody Tue May 21 09:19:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169756721529427.19447082452575; Tue, 17 Oct 2023 11:26:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qsolp-0006Jq-Pu; Tue, 17 Oct 2023 14:26:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qsoli-0006Gm-AL for qemu-devel@nongnu.org; Tue, 17 Oct 2023 14:26:16 -0400 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qsolT-0001UM-2t for qemu-devel@nongnu.org; Tue, 17 Oct 2023 14:26:14 -0400 Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qsolL-007II3-25; Tue, 17 Oct 2023 18:25:51 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1qsolI-000Pal-25; Tue, 17 Oct 2023 19:25:48 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=ASyx27ABg/WdxWXFMwdeXHtm2HUFOe8n8pbrbgV+R1U=; b=dCsAZYtU5oS750WARgfG1cbHbk bPq+UZ7bR809wKmsw6T/r5Nxzqx94zY+jrblkr53Fbw9QGaa0IdcMIDWC7gMM9f/2wG56EATB16YM 6Cdyn/BVEQT8FmID+F6W4Do+H9HkX8Yx6y3050hlpOfC4WRRmLws8CV1sav8q1c3JvBJ1buZfhQ1b D7SbIvxrk3EDzcEPbzuFIPt/piSKj+wHOSdiGhorqpUF3KFolMfYdOAF71tj/YcAZF/QvMnkDo0kt wNcmRNEUqWWlcIWAgKAiCrsr8NFVyln/wn+h28XuAME3OeO+FgYtDeHIGbHAG+XixHHmENkN4g5m3 E+1fmYTg==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Stefano Stabellini , Anthony Perard , Paul Durrant , Jason Wang , xen-devel@lists.xenproject.org Subject: [PATCH 1/4] hw/xen: only remove peers of PCI NICs on unplug Date: Tue, 17 Oct 2023 19:25:42 +0100 Message-Id: <20231017182545.97973-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231017182545.97973-1-dwmw2@infradead.org> References: <20231017182545.97973-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2001:8b0:10b:1:d65d:64ff:fe57:4e05; envelope-from=BATV+ff42024f62dfd72ba0d0+7359+infradead.org+dwmw2@desiato.srs.infradead.org; helo=desiato.infradead.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1697567216416100001 Content-Type: text/plain; charset="utf-8" From: David Woodhouse When the Xen guest asks to unplug *emulated* NICs, it's kind of unhelpful also to unplug the peer of the *Xen* PV NIC. Signed-off-by: David Woodhouse --- hw/i386/xen/xen_platform.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hw/i386/xen/xen_platform.c b/hw/i386/xen/xen_platform.c index 17457ff3de..e2dd1b536a 100644 --- a/hw/i386/xen/xen_platform.c +++ b/hw/i386/xen/xen_platform.c @@ -140,9 +140,14 @@ static void unplug_nic(PCIBus *b, PCIDevice *d, void *= o) /* Remove the peer of the NIC device. Normally, this would be a tap device= . */ static void del_nic_peer(NICState *nic, void *opaque) { - NetClientState *nc; + NetClientState *nc =3D qemu_get_queue(nic); + ObjectClass *klass =3D module_object_class_by_name(nc->model); + + /* Only delete peers of PCI NICs that we're about to delete */ + if (!klass || !object_class_dynamic_cast(klass, TYPE_PCI_DEVICE)) { + return; + } =20 - nc =3D qemu_get_queue(nic); if (nc->peer) qemu_del_net_client(nc->peer); } --=20 2.40.1 From nobody Tue May 21 09:19:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1697567227978576.3520555239629; Tue, 17 Oct 2023 11:27:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qsolo-0006HD-51; Tue, 17 Oct 2023 14:26:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qsolY-0006F6-MJ for qemu-devel@nongnu.org; Tue, 17 Oct 2023 14:26:08 -0400 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qsolQ-0001UE-J0 for qemu-devel@nongnu.org; Tue, 17 Oct 2023 14:26:01 -0400 Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qsolL-00DrCp-On; Tue, 17 Oct 2023 18:25:51 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1qsolI-000Pao-2A; Tue, 17 Oct 2023 19:25:48 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=soewF1VEeC4Dlpkzvr2uXeIIPaScop4Mpb2XJvzItmY=; b=RkSR5Y5Ot3jJe1d4Q4FDdzIghn QI1NxIjMQyDz+f1OQpK27yB8HhfPT73C9TmxaFKMrAaodavvk2DeQYyT+z6zpFERv3aTi5OzxogQN Ri+bizOAO1LUuNW2k4tI7WfFDQQCNUJoSom9xBE86kI6yIk1lSxuRVCCXTlIYz5nzNTJfzTBllI4b qV/0Wn+6tYmcJ4OgNadm6bg76PsSMOnrDUyrjjcc+UQTdP7wYO26RkOc8Js83mUmGu041vzPhX2M8 taWrsG/UUW5EU18fzdQHVmlV3+3lVA/LfbrWYdz94JanMSEkdDXntw7PopN+qW/4pJyLSx1++kO1N 8w9ZXHfw==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Stefano Stabellini , Anthony Perard , Paul Durrant , Jason Wang , xen-devel@lists.xenproject.org Subject: [PATCH 2/4] hw/xen: update Xen PV NIC to XenDevice model Date: Tue, 17 Oct 2023 19:25:43 +0100 Message-Id: <20231017182545.97973-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231017182545.97973-1-dwmw2@infradead.org> References: <20231017182545.97973-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2001:8b0:10b:1236::1; envelope-from=BATV+e442e50539a6073fd887+7359+infradead.org+dwmw2@casper.srs.infradead.org; helo=casper.infradead.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1697567229915100007 Content-Type: text/plain; charset="utf-8" From: David Woodhouse Signed-off-by: David Woodhouse --- hw/net/meson.build | 2 +- hw/net/trace-events | 9 + hw/net/xen_nic.c | 426 +++++++++++++++++++++++++++++--------- hw/xenpv/xen_machine_pv.c | 1 - 4 files changed, 342 insertions(+), 96 deletions(-) diff --git a/hw/net/meson.build b/hw/net/meson.build index 2632634df3..f64651c467 100644 --- a/hw/net/meson.build +++ b/hw/net/meson.build @@ -1,5 +1,5 @@ system_ss.add(when: 'CONFIG_DP8393X', if_true: files('dp8393x.c')) -system_ss.add(when: 'CONFIG_XEN', if_true: files('xen_nic.c')) +system_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen_nic.c')) system_ss.add(when: 'CONFIG_NE2000_COMMON', if_true: files('ne2000.c')) =20 # PCI network cards diff --git a/hw/net/trace-events b/hw/net/trace-events index 3abfd65e5b..ee56acfbce 100644 --- a/hw/net/trace-events +++ b/hw/net/trace-events @@ -482,3 +482,12 @@ dp8393x_receive_oversize(int size) "oversize packet, p= kt_size is %d" dp8393x_receive_not_netcard(void) "packet not for netcard" dp8393x_receive_packet(int crba) "Receive packet at 0x%"PRIx32 dp8393x_receive_write_status(int crba) "Write status at 0x%"PRIx32 + +# xen_nic.c +xen_netdev_realize(int idx, const char *info) "idx %u info '%s'" +xen_netdev_unrealize(int idx) "idx %u" +xen_netdev_create(int idx) "idx %u" +xen_netdev_destroy(int idx) "idx %u" +xen_netdev_disconnect(int idx) "idx %u" +xen_netdev_connect(int idx, unsigned int tx, unsigned int rx, int port) "i= dx %u tx %u rx %u port %u" +xen_netdev_frontend_changed(const char *idx, int state) "idx %s state %d" diff --git a/hw/net/xen_nic.c b/hw/net/xen_nic.c index 9bbf6599fc..84914c329c 100644 --- a/hw/net/xen_nic.c +++ b/hw/net/xen_nic.c @@ -20,6 +20,11 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/cutils.h" +#include "qemu/qemu-print.h" +#include "qapi/qmp/qdict.h" +#include "qapi/error.h" + #include #include #include @@ -27,18 +32,26 @@ #include "net/net.h" #include "net/checksum.h" #include "net/util.h" -#include "hw/xen/xen-legacy-backend.h" + +#include "hw/xen/xen-backend.h" +#include "hw/xen/xen-bus-helper.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" =20 #include "hw/xen/interface/io/netif.h" +#include "hw/xen/interface/io/xs_wire.h" + +#include "trace.h" =20 /* ------------------------------------------------------------- */ =20 struct XenNetDev { - struct XenLegacyDevice xendev; /* must be first */ - char *mac; + struct XenDevice xendev; /* must be first */ + XenEventChannel *event_channel; + int dev; int tx_work; - int tx_ring_ref; - int rx_ring_ref; + unsigned int tx_ring_ref; + unsigned int rx_ring_ref; struct netif_tx_sring *txs; struct netif_rx_sring *rxs; netif_tx_back_ring_t tx_ring; @@ -47,6 +60,13 @@ struct XenNetDev { NICState *nic; }; =20 +typedef struct XenNetDev XenNetDev; + +#define TYPE_XEN_NET_DEVICE "xen-net-device" +OBJECT_DECLARE_SIMPLE_TYPE(XenNetDev, XEN_NET_DEVICE) + +#define xen_pv_printf(a, n, ...) qemu_printf(__VA_ARGS__) + /* ------------------------------------------------------------- */ =20 static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *= txp, int8_t st) @@ -68,7 +88,8 @@ static void net_tx_response(struct XenNetDev *netdev, net= if_tx_request_t *txp, i netdev->tx_ring.rsp_prod_pvt =3D ++i; RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify); if (notify) { - xen_pv_send_notify(&netdev->xendev); + xen_device_notify_event_channel(XEN_DEVICE(netdev), + netdev->event_channel, NULL); } =20 if (i =3D=3D netdev->tx_ring.req_cons) { @@ -104,8 +125,9 @@ static void net_tx_error(struct XenNetDev *netdev, neti= f_tx_request_t *txp, RING #endif } =20 -static void net_tx_packets(struct XenNetDev *netdev) +static bool net_tx_packets(struct XenNetDev *netdev) { + bool done_something =3D false; netif_tx_request_t txreq; RING_IDX rc, rp; void *page; @@ -122,6 +144,7 @@ static void net_tx_packets(struct XenNetDev *netdev) } memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(= txreq)); netdev->tx_ring.req_cons =3D ++rc; + done_something =3D true; =20 #if 1 /* should not happen in theory, we don't announce the * @@ -151,7 +174,7 @@ static void net_tx_packets(struct XenNetDev *netdev) continue; } =20 - xen_pv_printf(&netdev->xendev, 3, + if (0) qemu_printf(//&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%= s%s%s\n", txreq.gref, txreq.offset, txreq.size, txreq.flag= s, (txreq.flags & NETTXF_csum_blank) ? " csum_b= lank" : "", @@ -159,8 +182,8 @@ static void net_tx_packets(struct XenNetDev *netdev) (txreq.flags & NETTXF_more_data) ? " more_d= ata" : "", (txreq.flags & NETTXF_extra_info) ? " extra_= info" : ""); =20 - page =3D xen_be_map_grant_ref(&netdev->xendev, txreq.gref, - PROT_READ); + page =3D xen_device_map_grant_refs(&netdev->xendev, &txreq.gre= f, 1, + PROT_READ, NULL); if (page =3D=3D NULL) { xen_pv_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n", @@ -181,7 +204,8 @@ static void net_tx_packets(struct XenNetDev *netdev) qemu_send_packet(qemu_get_queue(netdev->nic), page + txreq.offset, txreq.size); } - xen_be_unmap_grant_ref(&netdev->xendev, page, txreq.gref); + xen_device_unmap_grant_refs(&netdev->xendev, page, &txreq.gref= , 1, + NULL); net_tx_response(netdev, &txreq, NETIF_RSP_OKAY); } if (!netdev->tx_work) { @@ -190,6 +214,7 @@ static void net_tx_packets(struct XenNetDev *netdev) netdev->tx_work =3D 0; } g_free(tmpbuf); + return done_something; } =20 /* ------------------------------------------------------------- */ @@ -212,14 +237,15 @@ static void net_rx_response(struct XenNetDev *netdev, resp->status =3D (int16_t)st; } =20 - xen_pv_printf(&netdev->xendev, 3, + if (0) qemu_printf(//&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n", i, resp->status, resp->flags); =20 netdev->rx_ring.rsp_prod_pvt =3D ++i; RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify); if (notify) { - xen_pv_send_notify(&netdev->xendev); + xen_device_notify_event_channel(XEN_DEVICE(netdev), + netdev->event_channel, NULL); } } =20 @@ -232,7 +258,7 @@ static ssize_t net_rx_packet(NetClientState *nc, const = uint8_t *buf, size_t size RING_IDX rc, rp; void *page; =20 - if (netdev->xendev.be_state !=3D XenbusStateConnected) { + if (netdev->rx_ring.sring =3D=3D NULL) { return -1; } =20 @@ -252,7 +278,8 @@ static ssize_t net_rx_packet(NetClientState *nc, const = uint8_t *buf, size_t size memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq)); netdev->rx_ring.req_cons =3D ++rc; =20 - page =3D xen_be_map_grant_ref(&netdev->xendev, rxreq.gref, PROT_WRITE); + page =3D xen_device_map_grant_refs(&netdev->xendev, &rxreq.gref, 1, + PROT_WRITE, NULL); if (page =3D=3D NULL) { xen_pv_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n", @@ -261,7 +288,7 @@ static ssize_t net_rx_packet(NetClientState *nc, const = uint8_t *buf, size_t size return -1; } memcpy(page + NET_IP_ALIGN, buf, size); - xen_be_unmap_grant_ref(&netdev->xendev, page, rxreq.gref); + xen_device_unmap_grant_refs(&netdev->xendev, page, &rxreq.gref, 1, NUL= L); net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0); =20 return size; @@ -275,139 +302,350 @@ static NetClientInfo net_xen_info =3D { .receive =3D net_rx_packet, }; =20 -static int net_init(struct XenLegacyDevice *xendev) +static void xen_netdev_realize(XenDevice *xendev, Error **errp) { - struct XenNetDev *netdev =3D container_of(xendev, struct XenNetDev, xe= ndev); + ERRP_GUARD(); + XenNetDev *netdev =3D XEN_NET_DEVICE(xendev); =20 - /* read xenstore entries */ - if (netdev->mac =3D=3D NULL) { - netdev->mac =3D xenstore_read_be_str(&netdev->xendev, "mac"); - } + qemu_macaddr_default_if_unset(&netdev->conf.macaddr); =20 - /* do we have all we need? */ - if (netdev->mac =3D=3D NULL) { - return -1; - } - - if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) { - return -1; - } + xen_device_frontend_printf(xendev, "mac", "%02x:%02x:%02x:%02x:%02x:%0= 2x", + 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]); =20 netdev->nic =3D qemu_new_nic(&net_xen_info, &netdev->conf, - "xen", NULL, netdev); + object_get_typename(OBJECT(xendev)), + DEVICE(xendev)->id, netdev); =20 - qemu_set_info_str(qemu_get_queue(netdev->nic), - "nic: xenbus vif macaddr=3D%s", netdev->mac); + qemu_format_nic_info_str(qemu_get_queue(netdev->nic), netdev->conf.mac= addr.a); =20 /* fill info */ - xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); - xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0); + xen_device_backend_printf(xendev, "feature-rx-copy", "%u", 1); + xen_device_backend_printf(xendev, "feature-rx-flip", "%u", 0); =20 - return 0; + trace_xen_netdev_realize(netdev->dev, qemu_get_queue(netdev->nic)->inf= o_str); } =20 -static int net_connect(struct XenLegacyDevice *xendev) +static bool net_event(void *_xendev) { - struct XenNetDev *netdev =3D container_of(xendev, struct XenNetDev, xe= ndev); - int rx_copy; + XenNetDev *netdev =3D XEN_NET_DEVICE(_xendev); + bool done_something; =20 - if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref", - &netdev->tx_ring_ref) =3D=3D -1) { - return -1; + done_something =3D 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 =3D XEN_NET_DEVICE(xendev); + unsigned int port, rx_copy; + + if (xen_device_frontend_scanf(xendev, "tx-ring-ref", "%u", + &netdev->tx_ring_ref) !=3D 1) { + error_setg(errp, "failed to read tx-ring-ref"); + return false; } - if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref", - &netdev->rx_ring_ref) =3D=3D -1) { - return 1; + + if (xen_device_frontend_scanf(xendev, "rx-ring-ref", "%u", + &netdev->rx_ring_ref) !=3D 1) { + error_setg(errp, "failed to read rx-ring-ref"); + return false; } - if (xenstore_read_fe_int(&netdev->xendev, "event-channel", - &netdev->xendev.remote_port) =3D=3D -1) { - return -1; + + if (xen_device_frontend_scanf(xendev, "event-channel", "%u", + &port) !=3D 1) { + error_setg(errp, "failed to read event-channel"); + return false; } =20 - if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy)= =3D=3D -1) { + if (xen_device_frontend_scanf(xendev, "request-rx-copy", "%u", + &rx_copy) !=3D 1) { rx_copy =3D 0; } if (rx_copy =3D=3D 0) { - xen_pv_printf(&netdev->xendev, 0, - "frontend doesn't support rx-copy.\n"); - return -1; + error_setg(errp, "frontend doesn't support rx-copy"); + return false; } =20 - netdev->txs =3D xen_be_map_grant_ref(&netdev->xendev, - netdev->tx_ring_ref, - PROT_READ | PROT_WRITE); + netdev->txs =3D xen_device_map_grant_refs(xendev, + &netdev->tx_ring_ref, 1, + PROT_READ | PROT_WRITE, + errp); if (!netdev->txs) { - return -1; + error_prepend(errp, "failed to map tx grant ref: "); + return false; } - netdev->rxs =3D xen_be_map_grant_ref(&netdev->xendev, - netdev->rx_ring_ref, - PROT_READ | PROT_WRITE); + + netdev->rxs =3D xen_device_map_grant_refs(xendev, + &netdev->rx_ring_ref, 1, + PROT_READ | PROT_WRITE, + errp); if (!netdev->rxs) { - xen_be_unmap_grant_ref(&netdev->xendev, netdev->txs, - netdev->tx_ring_ref); - netdev->txs =3D NULL; - return -1; + error_prepend(errp, "failed to map rx grant ref: "); + return false; } + BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XEN_PAGE_SIZE); BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XEN_PAGE_SIZE); =20 - xen_be_bind_evtchn(&netdev->xendev); + netdev->event_channel =3D xen_device_bind_event_channel(xendev, port, + net_event, + netdev, + errp); + if (!netdev->event_channel) { + return false; + } =20 - xen_pv_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d,= " - "remote port %d, local port %d\n", - netdev->tx_ring_ref, netdev->rx_ring_ref, - netdev->xendev.remote_port, netdev->xendev.local_port); + trace_xen_netdev_connect(netdev->dev, netdev->tx_ring_ref, + netdev->rx_ring_ref, port); =20 net_tx_packets(netdev); - return 0; + return true; } =20 -static void net_disconnect(struct XenLegacyDevice *xendev) +static void xen_netdev_disconnect(XenDevice *xendev, Error **errp) { - struct XenNetDev *netdev =3D container_of(xendev, struct XenNetDev, xe= ndev); + XenNetDev *netdev =3D XEN_NET_DEVICE(xendev); + + trace_xen_netdev_disconnect(netdev->dev); =20 - xen_pv_unbind_evtchn(&netdev->xendev); + netdev->tx_ring.sring =3D NULL; + netdev->rx_ring.sring =3D NULL; =20 + if (netdev->event_channel) { + xen_device_unbind_event_channel(xendev, netdev->event_channel, + errp); + netdev->event_channel =3D NULL; + } if (netdev->txs) { - xen_be_unmap_grant_ref(&netdev->xendev, netdev->txs, - netdev->tx_ring_ref); + xen_device_unmap_grant_refs(xendev, netdev->txs, + &netdev->tx_ring_ref, 1, errp); netdev->txs =3D NULL; } if (netdev->rxs) { - xen_be_unmap_grant_ref(&netdev->xendev, netdev->rxs, - netdev->rx_ring_ref); + xen_device_unmap_grant_refs(xendev, netdev->rxs, + &netdev->rx_ring_ref, 1, errp); netdev->rxs =3D NULL; } } =20 -static void net_event(struct XenLegacyDevice *xendev) +/* -------------------------------------------------------------------- */ + + +static void xen_netdev_frontend_changed(XenDevice *xendev, + enum xenbus_state frontend_state, + Error **errp) { - struct XenNetDev *netdev =3D container_of(xendev, struct XenNetDev, xe= ndev); - net_tx_packets(netdev); - qemu_flush_queued_packets(qemu_get_queue(netdev->nic)); + ERRP_GUARD(); + enum xenbus_state backend_state =3D xen_device_backend_get_state(xende= v); + + trace_xen_netdev_frontend_changed(xendev->name, frontend_state); + + switch (frontend_state) { + case XenbusStateInitialised: + case XenbusStateConnected: + if (backend_state =3D=3D 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; + + default: + break; + } } =20 -static int net_free(struct XenLegacyDevice *xendev) +static char *xen_netdev_get_name(XenDevice *xendev, Error **errp) { - struct XenNetDev *netdev =3D container_of(xendev, struct XenNetDev, xe= ndev); + XenNetDev *netdev =3D XEN_NET_DEVICE(xendev); + + if (netdev->dev =3D=3D -1) { + XenBus *xenbus =3D XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); + char fe_path[XENSTORE_ABS_PATH_MAX + 1]; + int idx =3D (xen_mode =3D=3D XEN_EMULATE) ? 0 : 1; + char *value; + + /* Theoretically we could go up to INT_MAX here but that's overkil= l */ + while (idx < 100) { + snprintf(fe_path, sizeof(fe_path), + "/local/domain/%u/device/vif/%u", + xendev->frontend_id, idx); + value =3D qemu_xen_xs_read(xenbus->xsh, XBT_NULL, fe_path, NUL= L); + if (!value) { + if (errno =3D=3D ENOENT) { + netdev->dev =3D 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 =3D 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); =20 if (netdev->nic) { qemu_del_nic(netdev->nic); - netdev->nic =3D NULL; } - g_free(netdev->mac); - netdev->mac =3D NULL; - return 0; } =20 /* ------------------------------------------------------------- */ =20 -struct XenDevOps xen_netdev_ops =3D { - .size =3D sizeof(struct XenNetDev), - .flags =3D DEVOPS_FLAG_NEED_GNTDEV, - .init =3D net_init, - .initialise =3D net_connect, - .event =3D net_event, - .disconnect =3D net_disconnect, - .free =3D net_free, +static Property xen_netdev_properties[] =3D { + 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 =3D DEVICE_CLASS(class); + XenDeviceClass *xendev_class =3D XEN_DEVICE_CLASS(class); + + xendev_class->backend =3D "qnet"; + xendev_class->device =3D "vif"; + xendev_class->get_name =3D xen_netdev_get_name; + xendev_class->realize =3D xen_netdev_realize; + xendev_class->frontend_changed =3D xen_netdev_frontend_changed; + xendev_class->unrealize =3D xen_netdev_unrealize; + set_bit(DEVICE_CATEGORY_NETWORK, dev_class->categories); + dev_class->user_creatable =3D true; + + device_class_set_props(dev_class, xen_netdev_properties); +} + +static const TypeInfo xen_net_type_info =3D { + .name =3D TYPE_XEN_NET_DEVICE, + .parent =3D TYPE_XEN_DEVICE, + .instance_size =3D sizeof(XenNetDev), + .class_init =3D 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 =3D xen_backend_get_bus(backend); + const char *name =3D xen_backend_get_name(backend); + XenDevice *xendev =3D NULL; + unsigned long number; + const char *macstr; + XenNetDev *net; + MACAddr mac; + + if (qemu_strtoul(name, NULL, 10, &number) || number >=3D INT_MAX) { + error_setg(errp, "failed to parse name '%s'", name); + goto fail; + } + + trace_xen_netdev_create(number); + + macstr =3D qdict_get_try_str(opts, "mac"); + if (macstr =3D=3D 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 =3D XEN_DEVICE(qdev_new(TYPE_XEN_NET_DEVICE)); + net =3D XEN_NET_DEVICE(xendev); + + net->dev =3D 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 =3D xen_backend_get_device(backend); + XenNetDev *netdev =3D XEN_NET_DEVICE(xendev); + + trace_xen_netdev_destroy(netdev->dev); + + object_unparent(OBJECT(xendev)); +} + +static const XenBackendInfo xen_net_backend_info =3D { + .type =3D "qnet", + .create =3D xen_net_device_create, + .destroy =3D xen_net_device_destroy, +}; + +static void xen_net_register_backend(void) +{ + xen_backend_register(&xen_net_backend_info); +} + +xen_backend_init(xen_net_register_backend); diff --git a/hw/xenpv/xen_machine_pv.c b/hw/xenpv/xen_machine_pv.c index 17cda5ec13..764ca5c4fa 100644 --- a/hw/xenpv/xen_machine_pv.c +++ b/hw/xenpv/xen_machine_pv.c @@ -55,7 +55,6 @@ static void xen_init_pv(MachineState *machine) } =20 xen_be_register("vfb", &xen_framebuffer_ops); - xen_be_register("qnic", &xen_netdev_ops); =20 /* configure framebuffer */ if (vga_interface_type =3D=3D VGA_XENFB) { --=20 2.40.1 From nobody Tue May 21 09:19:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1697567193; cv=none; d=zohomail.com; s=zohoarc; b=RqsmPNUeHkmzmu2aVgVuhMP8gEBevO5KXZfVScWg7nNFfVYf8hhQ+4cuL4iO9oR83sfSFqOWiZiNUwrR/W1XB/y/7FWPySN2sIDZ2e6bdbI9hOF/ezbIKJfH1mXAM4FclOI5AZenSCoUJfMLDLz/wdWr/w515AhytuCYxrVIOVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1697567193; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mT1XScI7NSJ4weZ5c1NJoTEjfaqAFFggM/PoQY19qlQ=; b=JBH6mUnYIMF4BoS6a7i+GeNXMqlgT74qQRA5QOU2EbPJprLxZuDYtGAJ3K8r8jjU09G8bfECvKRPyeDzZjJpfVc0HkWv6+SNwhhFcsphsV2zkmuf5ABC0gJmFvLET6WisMnOm1BF3OKQMmMbh13yWmmp1H0238vbPpBCesg08Xs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1697567193066530.7812703066795; Tue, 17 Oct 2023 11:26:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.618329.961842 (Exim 4.92) (envelope-from ) id 1qsolc-0005Ff-SW; Tue, 17 Oct 2023 18:26:08 +0000 Received: by outflank-mailman (output) from mailman id 618329.961842; Tue, 17 Oct 2023 18:26:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qsolc-0005FY-PP; Tue, 17 Oct 2023 18:26:08 +0000 Received: by outflank-mailman (input) for mailman id 618329; Tue, 17 Oct 2023 18:26:06 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qsola-0005FN-T8 for xen-devel@lists.xenproject.org; Tue, 17 Oct 2023 18:26:06 +0000 Received: from casper.infradead.org (casper.infradead.org [2001:8b0:10b:1236::1]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a048d7a8-6d1a-11ee-98d4-6d05b1d4d9a1; Tue, 17 Oct 2023 20:26:02 +0200 (CEST) Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qsolL-00DrCq-Pk; Tue, 17 Oct 2023 18:25:51 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1qsolI-000Par-2F; Tue, 17 Oct 2023 19:25:48 +0100 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list X-Inumbo-ID: a048d7a8-6d1a-11ee-98d4-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description; bh=mT1XScI7NSJ4weZ5c1NJoTEjfaqAFFggM/PoQY19qlQ=; b=u2pl0HXyBSCIO8jw1OQWFNhf2O Hj/MN1k4AK95NViqo7qIk/mSIRKYon9a3ezB59j4K+iKbmyMri//ejsICoyYSix0lyRkGFkcyL8Ou vMliReAQ0IrMARgiZES6WA1xMB9twTeVfZaMrm7HvisUfl1T8yHWWDxG1ReTokk08H8//LXKNOliM dUxj2P1UJ++Q69tUfpC1Q86upQsLUc8mxFbFAnIxev/h+kAeEKtWt0+X7iWYdUwPCLGF3kdmr5wMK 7jQS8z9M1Mx5BxrobTWp3WEr1ZawgXQ8MGh4TnVlvCyk5ih0AL87cVI0NjBGyW7h/y/UBLoyHHEI6 10GUyejw==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Stefano Stabellini , Anthony Perard , Paul Durrant , Jason Wang , xen-devel@lists.xenproject.org Subject: [PATCH 3/4] [WTF] avoid qemu_del_nic() in xen_netdev_unrealize() on shutdown Date: Tue, 17 Oct 2023 19:25:44 +0100 Message-Id: <20231017182545.97973-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231017182545.97973-1-dwmw2@infradead.org> References: <20231017182545.97973-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1697567193830100001 From: David Woodhouse When QEMU is exiting, qemu_cleanup() calls net_cleanup(), which deletes the NIC from underneath the xen-net-device. When xen_netdev_unrealize() is later called via the xenbus exit notifier, it crashes. Signed-off-by: David Woodhouse --- hw/net/xen_nic.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hw/net/xen_nic.c b/hw/net/xen_nic.c index 84914c329c..8d25fb3101 100644 --- a/hw/net/xen_nic.c +++ b/hw/net/xen_nic.c @@ -25,6 +25,8 @@ #include "qapi/qmp/qdict.h" #include "qapi/error.h" =20 +#include "sysemu/runstate.h" + #include #include #include @@ -530,7 +532,11 @@ static void xen_netdev_unrealize(XenDevice *xendev) /* Disconnect from the frontend in case this has not already happened = */ xen_netdev_disconnect(xendev, NULL); =20 - if (netdev->nic) { + /* + * WTF? In RUN_STATE_SHUTDOWN, qemu_cleanup()=E2=86=92net_cleanup() al= ready deleted + * our NIC from underneath us! + */ + if (netdev->nic && !runstate_check(RUN_STATE_SHUTDOWN)) { qemu_del_nic(netdev->nic); } } --=20 2.40.1 From nobody Tue May 21 09:19:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1697567198; cv=none; d=zohomail.com; s=zohoarc; b=hsF/PoszThnGXTtLJhzXsSP4wcSUmwwuFE8bwxswPuHIWoU5Pd+9l1UF+YyiP9ie2BBS7o8qYO2dXBPXRf99KB1nBMVIM3Ti5CKkZxeJOA1+n+em4xnwTH7hQKCtx4XnmkyuLbKjq8ywBbZC+toBY/JSbsfWTOvgQmm0rz/HiIs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1697567198; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=i6ZTV9wtaYV9iGMHjBK+i6c16uQy0HhkOiTbhO1beOA=; b=eKBrpYLPaWzgYqjkwnL6NSxxUUJZenyG1sim2pZQoWu6VZTJ6SVJCKG0oHH0MV7ycbCmFHGN3apCzdKGFawbF/NsJ5kf4ckLlwzyrdGLx3Pu1YrSVuYn7UvL75U47AxtdqgG/ZuCCG6+LmKHgrv1OWzXLgWwocOLph67DPTl/yg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 16975671987081018.7962168386969; Tue, 17 Oct 2023 11:26:38 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.618331.961861 (Exim 4.92) (envelope-from ) id 1qsoli-0005kh-9j; Tue, 17 Oct 2023 18:26:14 +0000 Received: by outflank-mailman (output) from mailman id 618331.961861; Tue, 17 Oct 2023 18:26:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qsoli-0005kW-6u; Tue, 17 Oct 2023 18:26:14 +0000 Received: by outflank-mailman (input) for mailman id 618331; Tue, 17 Oct 2023 18:26:12 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qsolg-0005jg-QO for xen-devel@lists.xenproject.org; Tue, 17 Oct 2023 18:26:12 +0000 Received: from casper.infradead.org (casper.infradead.org [2001:8b0:10b:1236::1]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a327ac44-6d1a-11ee-9b0e-b553b5be7939; Tue, 17 Oct 2023 20:26:09 +0200 (CEST) Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qsolL-00DrCo-Or; Tue, 17 Oct 2023 18:25:51 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1qsolI-000Pau-2K; Tue, 17 Oct 2023 19:25:48 +0100 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list X-Inumbo-ID: a327ac44-6d1a-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=i6ZTV9wtaYV9iGMHjBK+i6c16uQy0HhkOiTbhO1beOA=; b=kgUcqSpB3ZA6qV433Y7zjLuzBw WPppw8Z1Zqodeu1n+SgUTO1nCXQZH7RJxqWGZOw0OLAVhBimarKfifrC34uoWrTXlo8lePfvJMqqO XlFT/qsz3HrkkrNS5HTi2kyFe+3cyhTkEVPK1KPMNwEssFHL9gOB3erBlaSavxk3wPso+PnrHcDYE NZDjIHF9pXhcfNVpF1nChNaK1D+6IVaw2lKMonuPIW7EdBOXJR6utSoD5mi/+JDcpJ+UUU6gRM9lQ lT1dkCkvWBJWbdTx15tFxmtYGaGVgoL3NVdKFQ+iWonFAlFHVcvMMwXemBGVjpY6pVkeVnoz14Z6U +T9h+UGA==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Stefano Stabellini , Anthony Perard , Paul Durrant , Jason Wang , xen-devel@lists.xenproject.org Subject: [PATCH 4/4] hw/i386/pc: support '-nic' for xen-net-device Date: Tue, 17 Oct 2023 19:25:45 +0100 Message-Id: <20231017182545.97973-5-dwmw2@infradead.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231017182545.97973-1-dwmw2@infradead.org> References: <20231017182545.97973-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1697567199899100001 Content-Type: text/plain; charset="utf-8" From: David Woodhouse The default NIC creation seems a bit hackish to me. I don't understand why each platform ha to call pci_nic_init_nofail() from a point in the code where it actually has a pointer to the PCI bus, and then we have the special cases for things like ne2k_isa. If qmp_device_add() can *find* the appropriate bus and instantiate the device on it, why can't we just do that from generic code for creating the default NICs too? But that isn't a yak I want to shave today. Add a xenbus field to the PCMachineState so that it can make its way from pc_basic_device_init() to pc_nic_init() and be handled as a special case like ne2k_isa is. Now we can launch emulated Xen guests with '-nic user'. Signed-off-by: David Woodhouse --- hw/i386/pc.c | 11 ++++++++--- hw/i386/pc_piix.c | 2 +- hw/i386/pc_q35.c | 2 +- hw/xen/xen-bus.c | 4 +++- include/hw/i386/pc.h | 4 +++- include/hw/xen/xen-bus.h | 2 +- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index bb3854d1d0..7413ca50c8 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1265,7 +1265,7 @@ void pc_basic_device_init(struct PCMachineState *pcms, if (pcms->bus) { pci_create_simple(pcms->bus, -1, "xen-platform"); } - xen_bus_init(); + pcms->xenbus =3D xen_bus_init(); xen_be_init(); } #endif @@ -1291,7 +1291,8 @@ void pc_basic_device_init(struct PCMachineState *pcms, pcms->vmport !=3D ON_OFF_AUTO_ON); } =20 -void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus) +void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus, + BusState *xen_bus) { MachineClass *mc =3D MACHINE_CLASS(pcmc); int i; @@ -1301,7 +1302,11 @@ void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_b= us, PCIBus *pci_bus) NICInfo *nd =3D &nd_table[i]; const char *model =3D nd->model ? nd->model : mc->default_nic; =20 - if (g_str_equal(model, "ne2k_isa")) { + if (xen_bus && (!nd->model || g_str_equal(model, "xen-net-device")= )) { + DeviceState *dev =3D qdev_new("xen-net-device"); + qdev_set_nic_properties(dev, nd); + qdev_realize_and_unref(dev, xen_bus, &error_fatal); + } else if (g_str_equal(model, "ne2k_isa")) { pc_init_ne2k_isa(isa_bus, nd); } else { pci_nic_init_nofail(nd, pci_bus, model, NULL); diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c index e36a3262b2..90b5ae7258 100644 --- a/hw/i386/pc_piix.c +++ b/hw/i386/pc_piix.c @@ -322,7 +322,7 @@ static void pc_init1(MachineState *machine, pc_basic_device_init(pcms, isa_bus, x86ms->gsi, rtc_state, true, 0x4); =20 - pc_nic_init(pcmc, isa_bus, pci_bus); + pc_nic_init(pcmc, isa_bus, pci_bus, pcms->xenbus); =20 if (pcmc->pci_enabled) { PCIDevice *dev; diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c index a7386f2ca2..2ed0aab2a7 100644 --- a/hw/i386/pc_q35.c +++ b/hw/i386/pc_q35.c @@ -340,7 +340,7 @@ static void pc_q35_init(MachineState *machine) =20 /* the rest devices to which pci devfn is automatically assigned */ pc_vga_init(isa_bus, host_bus); - pc_nic_init(pcmc, isa_bus, host_bus); + pc_nic_init(pcmc, isa_bus, host_bus, pcms->xenbus); =20 if (machine->nvdimms_state->is_enabled) { nvdimm_init_acpi_state(machine->nvdimms_state, system_io, diff --git a/hw/xen/xen-bus.c b/hw/xen/xen-bus.c index 0da2aa219a..d7823964f8 100644 --- a/hw/xen/xen-bus.c +++ b/hw/xen/xen-bus.c @@ -1127,11 +1127,13 @@ static void xen_register_types(void) =20 type_init(xen_register_types) =20 -void xen_bus_init(void) +BusState *xen_bus_init(void) { DeviceState *dev =3D qdev_new(TYPE_XEN_BRIDGE); BusState *bus =3D qbus_new(TYPE_XEN_BUS, dev, NULL); =20 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); qbus_set_bus_hotplug_handler(bus); + + return bus; } diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h index bec38cb92c..feabf3d195 100644 --- a/include/hw/i386/pc.h +++ b/include/hw/i386/pc.h @@ -33,6 +33,7 @@ typedef struct PCMachineState { =20 /* Pointers to devices and objects: */ PCIBus *bus; + BusState *xenbus; I2CBus *smbus; PFlashCFI01 *flash[2]; ISADevice *pcspk; @@ -182,7 +183,8 @@ void pc_basic_device_init(struct PCMachineState *pcms, void pc_cmos_init(PCMachineState *pcms, BusState *ide0, BusState *ide1, ISADevice *s); -void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus); +void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus, + BusState *xen_bus); =20 void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs); =20 diff --git a/include/hw/xen/xen-bus.h b/include/hw/xen/xen-bus.h index eb440880b5..acad871b80 100644 --- a/include/hw/xen/xen-bus.h +++ b/include/hw/xen/xen-bus.h @@ -75,7 +75,7 @@ struct XenBusClass { OBJECT_DECLARE_TYPE(XenBus, XenBusClass, XEN_BUS) =20 -void xen_bus_init(void); +BusState *xen_bus_init(void); =20 void xen_device_backend_set_state(XenDevice *xendev, enum xenbus_state state); --=20 2.40.1