From patchwork Mon Oct 3 09:39:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexander Ivanov X-Patchwork-Id: 12997417 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 487D0C433FE for ; Mon, 3 Oct 2022 10:03:16 +0000 (UTC) Received: from localhost ([::1]:54018 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ofII7-0007br-8W for qemu-devel@archiver.kernel.org; Mon, 03 Oct 2022 06:03:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39162) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ofHvP-0006XP-OD for qemu-devel@nongnu.org; Mon, 03 Oct 2022 05:39:47 -0400 Received: from mail-eopbgr80119.outbound.protection.outlook.com ([40.107.8.119]:34208 helo=EUR04-VI1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ofHvN-0002U6-O9 for qemu-devel@nongnu.org; Mon, 03 Oct 2022 05:39:47 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c3I6H2bOxjJiAko77L6vGk8zZnYh5Bnk9X6/dUQTVh7RFOr8nNgw2XpxDTOGexSfd3I5E9rAkdN2LMrraRwVtJwoU1xm0HR4sjjcE8UGoBuwVoOCQimfl03wvGNXssqAUY6N/dknGtfDIEcxrjNRVp8HauGTwmNwGI2zRaOKdVl160m1OuSsFNIpzEMbZdO2pTGgp4/75tTwD9qSozpNZKPColh2WvzVoBrZZcDdoaOXS2yrWhPd3WfQeFaDe9rhZglVYE6ewIUIMteDnVdnXdAkGbbqHQJ+n7tTw7rqJkPhTltz+utHmup9BOxaQxKmmq0fI4yZ/WmPhfLovGzTlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GMmb0KLPqmWq8e9j4AHAUqbvFRHTz+zJGmVL75zIHYk=; b=ZA21u4YQcCgG95QEwSl5dOx32RWgpY0whfbEwTuf1VI/l2nF9zNhi+m9jUgp6/r+yHBbYVGobUURsCCGo8pMPsBRNBCG5jzvYyS7LbZsMEoUJznZDV3uRKd5WDH4jNoATImwBhkIgXxOgNnjXp9gBBmJBSOiXHYJgU7eLywzVjpazq7StG/JtM7cmF1wDn1MU7CEQZlgRmxNuvls3NWuCXqLJPoPXxkgSCRurqyI/h3cwzwkP3yBEPq/NuPyJJ/wtyFWjT24nudMaX6dDTFQJkG9bn7W4/HvyFozwiLzaRN8l1BAcTE7PBo7Tuaf1SKju+p3LpKfCq8x6VyEDDTzsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GMmb0KLPqmWq8e9j4AHAUqbvFRHTz+zJGmVL75zIHYk=; b=EeAsdCifkrqDZ98buLhPcYooC5fVzpbRT5glxLffevqWFy1T/SNZxP7WHqIOIGo1d4PZTy2uX+T54lw6ByIxJ5WMuj9kxcek9Gk2sjP1E3okcJRoMGA5eiunUI+wm+2+CAmaZJPPkCpDIZkuOk3NYDG1MljHvaOGkahvdBeEIsianryC/v6WGIAv9Ua2JuVMEYDp3mwWz6sbjLBKHErWmidju+AtcWPlH7schtOmkwORXgr1MGElCkDFp2vGJUEXgV4axA1owoz1Gplr9v4324RqNcrQr6UIo9FEQtIysC37oY5Tk2q6FbRknpMB/9o/g3TGw6HOA5IlXlVUgU9ezA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=virtuozzo.com; Received: from AS8PR08MB7095.eurprd08.prod.outlook.com (2603:10a6:20b:402::11) by DB9PR08MB8203.eurprd08.prod.outlook.com (2603:10a6:10:39e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.17; Mon, 3 Oct 2022 09:39:24 +0000 Received: from AS8PR08MB7095.eurprd08.prod.outlook.com ([fe80::5174:25c7:6df8:741b]) by AS8PR08MB7095.eurprd08.prod.outlook.com ([fe80::5174:25c7:6df8:741b%8]) with mapi id 15.20.5676.028; Mon, 3 Oct 2022 09:39:24 +0000 From: Alexander Ivanov To: qemu-devel@nongnu.org Cc: den@virtuozzo.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@gmail.com Subject: [PATCH v3 6/7] qga: Move HW address getting to a separate function Date: Mon, 3 Oct 2022 11:39:10 +0200 Message-Id: <20221003093911.1844031-7-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221003093911.1844031-1-alexander.ivanov@virtuozzo.com> References: <20221003093911.1844031-1-alexander.ivanov@virtuozzo.com> X-ClientProxiedBy: FR3P281CA0096.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:a1::8) To AS8PR08MB7095.eurprd08.prod.outlook.com (2603:10a6:20b:402::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR08MB7095:EE_|DB9PR08MB8203:EE_ X-MS-Office365-Filtering-Correlation-Id: f26864a1-b788-410e-6655-08daa523283c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z5uTOTTz5aZe0NFHJvLPQ/ZBFVLt4nJHJYdHFT+2Oo5fHwnWNLcXjX5L9pFkWHTdKUN95J02ruKuP7E3Or3UWwy/SrRSYgBvfLAZDXXVrS8xYsYOEPuQZ8Dzix6ynu/5bdWNPI8ejdUhP2aEfdsQKoRt04f+5dwtVITgcoMedzWXuKdlS/rpwdS316iXhGTge3cK2ayu/lYSsJt9jZEfEIak5VRUZKJg6yTfCxkHX6KV++Kpt1mcuTQqjaKAESgzmOQ052n39kP7UCNjfCk+ChFedtJKsd+oryGwTYK67YYFppoxHK3HW5aooHW5SmbNPRUYLt8DyiyLtAKgNYTsMjRcRO5Y6yMuR41NZBFuTGyhhIqZgv5C/oCtiNFKlHgapthB/y9QpUoxfHDtHwtLj03lJ8P+CIq1O9tNLAiiA5PZ1FB4rtEHMZJAQAuG/cNIVuQY3kP3xCqXhKCun5lFRk6itRszwe700DicKQUCy2GZRRXk5MQ8hSz8+KpbVnj/ENoT4hUwWJpem3V9IPH27A1yrb1I7l04OipgieigxSkT4a85tf24aWhfEVsq5hCC7Nn6bjDevspU72Wv2TCiA/xEopMExF5fAueqB/NKX95YTkFVlDOOc/+bGASDNju23ogoaevR6X7dirUtqILdr7XLhWrhCXp63e/vQmgeyXFiiD8O7jV/ffBpv/DZMz32GwJz/61WNJ1f+K/DrwUHYjetJIMx4FdhR3Dek5I1nfEF0u30l3DzuHFNKV9F1ERhRcmySDO7aj5qs3IHvj4PUg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AS8PR08MB7095.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(136003)(346002)(39850400004)(376002)(396003)(451199015)(6666004)(316002)(66476007)(66556008)(26005)(4326008)(8676002)(6512007)(6506007)(52116002)(2616005)(6916009)(478600001)(6486002)(66946007)(36756003)(38350700002)(38100700002)(86362001)(186003)(83380400001)(1076003)(44832011)(41300700001)(8936002)(5660300002)(2906002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?fqOmUHJYNBblA2PsbtCQ/5E85rPC?= =?utf-8?q?0Kogsjmq9KP/xYrwMmP8TT+KKXUAKE4DJ0SYfpDT2B7ak8pZmos/sqGrdw259JseO?= =?utf-8?q?FJ9ElNd7mYjcj8OVjv8xME5pAP+ZMs8n0tR1f/xyK5N3uDiy8KcXL1VnVBEufFdPU?= =?utf-8?q?G5TtAZ1jHicOJp5HuznrccxzXw/0KUkXUhAqx0Fd52QwxFR75iD38ZrkqxbRVXcaW?= =?utf-8?q?D9Gt9QYUVm/UuJj7srWQdhr8H08cGr9DfRiSYFgrO7jNZE2mrdnz9GDC1mB01WKkL?= =?utf-8?q?rPN+LbshMXXBKMOgAB2nCf53B94hst6rYeXdeWCSyHemtjXFIrc7htvkKgxFTRBNL?= =?utf-8?q?QsAjZ2Giu0byF7GIj7cYqa+92g0eRyYzG2gRSVifN7oQ8qPqD1OFXVBcKWTU0Q5IU?= =?utf-8?q?xb00tImcAxejNVHKmPFgPsFD6L2DEAnxVJ9RibqNLHTcSChYfyL+l+XSy3wdEYY0g?= =?utf-8?q?iJc0fShYkjozjkxj6ltaxojNH3e9EwZAuFTeLM12LlT+Bizwp2iZ0o3e45Z1F7KT0?= =?utf-8?q?ZPzDvj/2J1P5HHDjqfsXYiHPOxpuZRBur7tXkWH47UiAbKxrMM9W0kPdjjFLYOZZc?= =?utf-8?q?3Q7UTLK+R4qHHdh4OFgCKFiYz3TLFsXCP296dFcXGnw8g4ZLDlip8gH5TVDP10iT2?= =?utf-8?q?w1nJrgjM3SZ9SrCF0Kp2CM9K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nzkMVoP5Q=3D=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: f26864a1-b788-410e-6655-08daa523283c X-MS-Exchange-CrossTenant-AuthSource: AS8PR08MB7095.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2022 09:39:24.5081 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: z0Tt1jQJO2sJIiPxBCEKBDBFrWeKnMacmKzFkX8GY0OMDjZGNwm6FBdo24za/N0tXqaVwEaqJ/9NN8+xzVKZiraBHubY6PYz9L/RPSEBv64= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR08MB8203 Received-SPF: pass client-ip=40.107.8.119; envelope-from=alexander.ivanov@virtuozzo.com; helo=EUR04-VI1-obe.outbound.protection.outlook.com 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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" In the next patch FreeBSD support for guest-network-get-interfaces will be added. Previously move Linux-specific code of HW address getting to a separate functions and add a dumb function to commands-bsd.c. Signed-off-by: Alexander Ivanov Reviewed-by: Marc-André Lureau Reviewed-by: Konstantin Kostiuk --- qga/commands-bsd.c | 16 +++++++ qga/commands-common.h | 6 +++ qga/commands-posix.c | 100 ++++++++++++++++++++++++------------------ 3 files changed, 80 insertions(+), 42 deletions(-) diff --git a/qga/commands-bsd.c b/qga/commands-bsd.c index ca06692179..40f7ec7600 100644 --- a/qga/commands-bsd.c +++ b/qga/commands-bsd.c @@ -167,3 +167,19 @@ GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) return NULL; } #endif /* CONFIG_FSFREEZE */ + +#ifdef HAVE_GETIFADDRS +/* + * Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a + * buffer with ETHER_ADDR_LEN length at least. + * + * Returns -1 in case of an error, otherwise 0. "obtained" arguument is + * true if a MAC address was obtained successful, otherwise false. + */ +int guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf, + bool *obtained, Error **errp) +{ + *obtained = false; + return 0; +} +#endif /* HAVE_GETIFADDRS */ diff --git a/qga/commands-common.h b/qga/commands-common.h index 2d9878a634..a9cdaf7278 100644 --- a/qga/commands-common.h +++ b/qga/commands-common.h @@ -56,6 +56,12 @@ int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints, int qmp_guest_fsfreeze_do_thaw(Error **errp); #endif /* CONFIG_FSFREEZE */ +#ifdef HAVE_GETIFADDRS +#include +int guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf, + bool *obtained, Error **errp); +#endif + typedef struct GuestFileHandle GuestFileHandle; GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp); diff --git a/qga/commands-posix.c b/qga/commands-posix.c index f5b9e5c530..2a172c6492 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -41,20 +41,12 @@ #endif #endif -#ifdef __FreeBSD__ -/* - * The code under HAVE_GETIFADDRS condition can't be compiled in FreeBSD. - * Fix it in one of the following patches. - */ -#undef HAVE_GETIFADDRS -#endif - #ifdef HAVE_GETIFADDRS #include #include #include +#include #include -#include #ifdef CONFIG_SOLARIS #include #endif @@ -2889,6 +2881,57 @@ static int guest_get_network_stats(const char *name, return -1; } +#ifndef __FreeBSD__ +/* + * Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a + * buffer with ETHER_ADDR_LEN length at least. + * + * Returns -1 in case of an error, otherwise 0. "obtained" arguument is + * true if a MAC address was obtained successful, otherwise false. + */ +int guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf, + bool *obtained, Error **errp) +{ + struct ifreq ifr; + int sock; + + *obtained = false; + + /* we haven't obtained HW address yet */ + sock = socket(PF_INET, SOCK_STREAM, 0); + if (sock == -1) { + error_setg_errno(errp, errno, "failed to create socket"); + return -1; + } + + memset(&ifr, 0, sizeof(ifr)); + pstrcpy(ifr.ifr_name, IF_NAMESIZE, ifa->ifa_name); + if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) { + /* + * We can't get the hw addr of this interface, but that's not a + * fatal error. + */ + if (errno == EADDRNOTAVAIL) { + /* The interface doesn't have a hw addr (e.g. loopback). */ + g_debug("failed to get MAC address of %s: %s", + ifa->ifa_name, strerror(errno)); + } else{ + g_warning("failed to get MAC address of %s: %s", + ifa->ifa_name, strerror(errno)); + } + } else { +#ifdef CONFIG_SOLARIS + memcpy(buf, &ifr.ifr_addr.sa_data, ETHER_ADDR_LEN); +#else + memcpy(buf, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); +#endif + *obtained = true; + } + close(sock); + return 0; +} +#endif /* __FreeBSD__ */ + /* * Build information about guest interfaces */ @@ -2909,9 +2952,9 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) GuestNetworkInterfaceStat *interface_stat = NULL; char addr4[INET_ADDRSTRLEN]; char addr6[INET6_ADDRSTRLEN]; - int sock; - struct ifreq ifr; - unsigned char *mac_addr; + unsigned char mac_addr[ETHER_ADDR_LEN]; + int ret; + bool obtained; void *p; g_debug("Processing %s interface", ifa->ifa_name); @@ -2926,45 +2969,18 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) } if (!info->has_hardware_address) { - /* we haven't obtained HW address yet */ - sock = socket(PF_INET, SOCK_STREAM, 0); - if (sock == -1) { - error_setg_errno(errp, errno, "failed to create socket"); + ret = guest_get_hw_addr(ifa, mac_addr, &obtained, errp); + if (ret == -1) { goto error; } - - memset(&ifr, 0, sizeof(ifr)); - pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->name); - if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) { - /* - * We can't get the hw addr of this interface, but that's not a - * fatal error. Don't set info->hardware_address, but keep - * going. - */ - if (errno == EADDRNOTAVAIL) { - /* The interface doesn't have a hw addr (e.g. loopback). */ - g_debug("failed to get MAC address of %s: %s", - ifa->ifa_name, strerror(errno)); - } else{ - g_warning("failed to get MAC address of %s: %s", - ifa->ifa_name, strerror(errno)); - } - - } else { -#ifdef CONFIG_SOLARIS - mac_addr = (unsigned char *) &ifr.ifr_addr.sa_data; -#else - mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data; -#endif + if (obtained) { info->hardware_address = g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", (int) mac_addr[0], (int) mac_addr[1], (int) mac_addr[2], (int) mac_addr[3], (int) mac_addr[4], (int) mac_addr[5]); - info->has_hardware_address = true; } - close(sock); } if (ifa->ifa_addr &&