From nobody Tue Nov 26 14:18:07 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 1706806337428419.6493089892514; Thu, 1 Feb 2024 08:52:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rVaCD-0003yQ-5B; Thu, 01 Feb 2024 11:45:51 -0500 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 1rVaB3-0002vA-4p for qemu-devel@nongnu.org; Thu, 01 Feb 2024 11:44:37 -0500 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 1rVaAt-0002Pq-Vq for qemu-devel@nongnu.org; Thu, 01 Feb 2024 11:44:36 -0500 Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVaAe-0000000GIcv-1p9A for qemu-devel@nongnu.org; Thu, 01 Feb 2024 16:44:13 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVaAf-00000003ILy-0mkl for qemu-devel@nongnu.org; Thu, 01 Feb 2024 16:44:13 +0000 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:To: From:Reply-To:Cc:Content-ID:Content-Description; bh=OdxzKrubfC0ZaYycLGHWamGW/MHu3AZpCkbdAJlgMyI=; b=c7Q9gZLKv3gFOjxuzCTKtS4S6H tsXHqR3S92hJu2aPpNyzNdi7/PZ7h/jk0aOmwbrd7NuWpoAoI/elMeOJlJ6uOW+rXsC6mri2wKBsW 7uKpkHe4aF0Y/I9vMemY7eVwaBMY5Ur6L4rur/84mm3b7p7yZ5Rn6pjephA0kh78e4CbSgNX8G0Tj 77UNnJLMAX1m2LzZRxAbJfyNhcqqEqomCYSnkYOWA0orEutz+A4ifyFgSIbONeoTaxsR7BKk8619G N4EPoR9R7EeOJKBH7JHXoe4Uf23ccLnyZboarvtnLYuFZAm/DFdWn4cwhWqcf0IeI2ITBe3hDuIft mEZH/jNg==; From: David Woodhouse To: qemu-devel@nongnu.org Subject: [PULL 03/47] net: add qemu_create_nic_bus_devices() Date: Thu, 1 Feb 2024 16:43:28 +0000 Message-ID: <20240201164412.785520-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240201164412.785520-1-dwmw2@infradead.org> References: <20240201164412.785520-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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+16c2638fe4346fda554c+7466+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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1706806337885100002 From: David Woodhouse This will instantiate any NICs which live on a given bus type. Each bus is allowed *one* substitution (for PCI it's virtio =E2=86=92 virtio-net-pci= , for Xen it's xen =E2=86=92 xen-net-device; no point in overengineering it unles= s we actually want more). Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Reviewed-by: Thomas Huth --- include/net/net.h | 22 ++++++++++++++++++++ net/net.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/net/net.h b/include/net/net.h index dff1872b4d..728ca965af 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -248,6 +248,28 @@ bool qemu_configure_nic_device(DeviceState *dev, bool = match_default, */ DeviceState *qemu_create_nic_device(const char *typename, bool match_defau= lt, const char *alias); + +/* + * qemu_create_nic_bus_devices: Create configured NIC devices for a given = bus + * @bus: Bus on which to create devices + * @parent_type: Object type for devices to be created (e.g. TYPE_PCI_DEVI= CE) + * @default_model: Object type name for default NIC model (or %NULL) + * @alias: Additional model string to replace, for user convenience + * @alias_target: Actual object type name to be used in place of @alias + * + * Instantiate dynamic NICs on a given bus, typically a PCI bus. This scans + * for available NIC configurations which either specify a model which is + * a child type of @parent_type, or which do not specify a model when + * @default_model is non-NULL. Each device is instantiated on the given @b= us. + * + * A single substitution is supported, e.g. "xen" =E2=86=92 "xen-net-devic= e" for the + * Xen bus, or "virtio" =E2=86=92 "virtio-net-pci" for PCI. This allows th= e user to + * specify a more understandable "model=3D" parameter on the command line,= not + * only the real object typename. + */ +void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type, + const char *default_model, + const char *alias, const char *alias_targ= et); void print_net_client(Monitor *mon, NetClientState *nc); void net_socket_rs_init(SocketReadState *rs, SocketReadStateFinalize *finalize, diff --git a/net/net.c b/net/net.c index 962904eaef..4651b3f443 100644 --- a/net/net.c +++ b/net/net.c @@ -1223,6 +1223,59 @@ DeviceState *qemu_create_nic_device(const char *type= name, bool match_default, return dev; } =20 +void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type, + const char *default_model, + const char *alias, const char *alias_targ= et) +{ + GPtrArray *nic_models =3D qemu_get_nic_models(parent_type); + const char *model; + DeviceState *dev; + NICInfo *nd; + int i; + + if (nic_model_help) { + if (alias_target) { + add_nic_model_help(alias_target, alias); + } + for (i =3D 0; i < nic_models->len - 1; i++) { + add_nic_model_help(nic_models->pdata[i], NULL); + } + } + + /* Drop the NULL terminator which would make g_str_equal() unhappy */ + nic_models->len--; + + for (i =3D 0; i < nb_nics; i++) { + nd =3D &nd_table[i]; + + if (!nd->used || nd->instantiated) { + continue; + } + + model =3D nd->model ? nd->model : default_model; + if (!model) { + continue; + } + + /* Each bus type is allowed *one* substitution */ + if (g_str_equal(model, alias)) { + model =3D alias_target; + } + + if (!g_ptr_array_find_with_equal_func(nic_models, model, + g_str_equal, NULL)) { + /* This NIC does not live on this bus. */ + continue; + } + + dev =3D qdev_new(model); + qdev_set_nic_properties(dev, nd); + qdev_realize_and_unref(dev, bus, &error_fatal); + } + + g_ptr_array_free(nic_models, true); +} + static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])( const Netdev *netdev, const char *name, --=20 2.43.0