From patchwork Sat Nov 19 09:36:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Manish X-Patchwork-Id: 13049668 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 3A0F9C4332F for ; Sat, 19 Nov 2022 09:37:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1owKHl-0000SS-Ds; Sat, 19 Nov 2022 04:37:17 -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 1owKHX-0000HL-Uk for qemu-devel@nongnu.org; Sat, 19 Nov 2022 04:37:10 -0500 Received: from mx0b-002c1b01.pphosted.com ([148.163.155.12]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1owKHV-0007Bb-Bp for qemu-devel@nongnu.org; Sat, 19 Nov 2022 04:37:03 -0500 Received: from pps.filterd (m0127842.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AJ4aY7S018323; Sat, 19 Nov 2022 01:36:59 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : content-transfer-encoding : mime-version; s=proofpoint20171006; bh=xo4whEmOJJBUUfO2hyQGNq0jBWJWtbRZanzlEXknIiU=; b=zZq5TxG7cjplUFiMG0gQ79eGtffpTprAJKzaZcB7PTVPnwML3IK5GQKJg+9xj17+lvZP bVQmGL+AemXpcsI6KNb84fVX46XuQIiPsd4QdybZG+GR4n59RhtGIdeygqtirPoFs8fw 4SJ8ziI2Qi3X6amXLx2J0cbdn4S/d+BcWrx8O5KxbLUWZAjl8583TCZ0Hc9Bnt05hX9f GPa48QKaXDUjyqLNCizkCih6TtuUYdI/Kr0YumeaVuneIKLmmGjSevAOncvmh7UDZDjr cIjHdUT+Qn/gHm/YJ5xVz0nuoNSyc8AN0bW4BALCrK6qztrpplSjuk1Gvl6S9L0A0psS og== Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2104.outbound.protection.outlook.com [104.47.55.104]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 3kxr908c9m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 19 Nov 2022 01:36:58 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Q16xd2TC+3U1wNthRKO6Q3zOI/iREhcrLD5FACClF4ITr4mI2Ou3F3rGLiNesqlmrwxzoS0KoajCz+zlIlSRhdhpo4Bq6dVDbczx5Um5H/y5ZrSi2Luu82GJvY83/wRgk4cKUiY1o2E9qSSItAjwlNtYC3QJeAVXxjLzCmKyg6OF7I9paTSI2A5zCPu6Pf6PN1XEcv/dJAYP5ZXZ5uQMMj6MmIqVMxX2BsgocAos8Qd78tsDvBX55DBmkHtgnCt1V833jwEEy0YrkaLp7BG8pNQ3SUF/e5z/LWClHlF/WhlaFa08xWmLyIvqTmwnIaIFMEKzh6rmJHnjo2pEFqHjNQ== 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=xo4whEmOJJBUUfO2hyQGNq0jBWJWtbRZanzlEXknIiU=; b=Y05eK178/zoEWowPvz/IE8RGXT+yLpRZImjUZ//T8teatjqIADXkunlCmpa7eiKbcCeeVIKT+MJAL4GksfuF1XQ/cKrW5ErqfkdlotGpikjQe4fVFsYvIZIxersLapkIhuxv4XXKJym7qawYs+LiZ9m/ME3pw4VkF4MPmlwy6Tyw20fQbvXqTzD1WtnLAGWrHdV6bqvUdeY6U++3+3QJsoOTBVyFpXN+8E4A43xdIQmALp5GjyRjh2PeVaJKfTN5tzIDF4jz6MxKRx61bXqj4UapR3CALSjsmGgVwcGQuL8Etk/BtYI7QWY+Aj/dxbm+538QCXGgY7u54wRumu6idg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xo4whEmOJJBUUfO2hyQGNq0jBWJWtbRZanzlEXknIiU=; b=JrAbIIqP67e15rVpOBQ3e9d79Sson8WYehshO1jpsrVlVVolD8r707/RrQrKlwgrQVYdHSdWlSStFQgCh3+OW4Fn6OzSEkERlyk0RIr8gYf4fQJ4e0iJ71MPI6gOAAVl5G4MFq7OumFucd9r+e0xO6zVcw6vEHp6sIp1yByXPDh4ko2tVvbFFndXapb63jacN9VrZh69t5odJV5jn5Hy5NGqg4KlZm2unpcUa7G9lOrZTfWS7cQ8cUjrUmicd9OhVgc+nIdc/W5+TupNopQ9ktd2aYxkhVcCSHflwfwjf10K+Cx6xP3hJlRt+MUNMEGeJxbe8tXw37mdziy1GvEjDQ== Received: from PH0PR02MB7384.namprd02.prod.outlook.com (2603:10b6:510:12::12) by PH0PR02MB7479.namprd02.prod.outlook.com (2603:10b6:510:a::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5834.9; Sat, 19 Nov 2022 09:36:56 +0000 Received: from PH0PR02MB7384.namprd02.prod.outlook.com ([fe80::dff2:2d3c:e182:fb66]) by PH0PR02MB7384.namprd02.prod.outlook.com ([fe80::dff2:2d3c:e182:fb66%3]) with mapi id 15.20.5834.009; Sat, 19 Nov 2022 09:36:56 +0000 From: "manish.mishra" To: qemu-devel@nongnu.org Cc: berrange@redhat.com, peterx@redhat.com, prerna.saxena@nutanix.com, quintela@redhat.com, dgilbert@redhat.com, lsoaresp@redhat.com, "manish.mishra" Subject: [PATCH v3 1/2] io: Add support for MSG_PEEK for socket channel Date: Sat, 19 Nov 2022 09:36:14 +0000 Message-Id: <20221119093615.158072-4-manish.mishra@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20221119093615.158072-1-manish.mishra@nutanix.com> References: <20221119093615.158072-1-manish.mishra@nutanix.com> X-ClientProxiedBy: BYAPR01CA0058.prod.exchangelabs.com (2603:10b6:a03:94::35) To PH0PR02MB7384.namprd02.prod.outlook.com (2603:10b6:510:12::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR02MB7384:EE_|PH0PR02MB7479:EE_ X-MS-Office365-Filtering-Correlation-Id: e96ceeb4-558d-476f-a3f8-08daca119944 x-proofpoint-crosstenant: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WZ+IAAWrhDSkySwpKu/mquLdW/DcmRuEokWqa8SObYKNIHvXMkpgxRPGUkeZvBC4RQ+yyKD0Ur+H+obG4ok6gSr1jBWUs1XdHCK9uK9G2RzSKw7jDyos1xNpRNm0clfyp/yWzhBQj8ChEHmgit254CeQxQHaIxcpVhWUw+M0bhCjfNRhCTCjnGodPdneEoDRHhUr71XSg5RYLQTGenwr+AbTXgFmz5DOvB4at3v/QS+un2IuI+BiGD8MmaKWiJjXE4X53UDumcU8ZgApZLKh1R/UGRw0uU0y0pYbQgtW/gCmJcBbtIQkpNhxhk/F9/duOnB6jt0HDQTAoZvUlpbjajTd4pnZL6ZsYz4pgwT7XpKWJLeETPM7UryovloMcQFzm1NP6n5t9WTbtYIqp+d9+gua4aSXji2hMwSTPJEOO2lwFFcFcLnP/ZVze8gBz+c55RqNHadd+9HaHyjzIrcMrTi+VT64v2QSIvG809vjwBSTDsp0svXjabaGg1KXUiNRJS1ye3G3uWIOpmEMhQmtvBNaewmJHUl/zcpgIbjAubRa90wuTCbn2ymW1+a99Jfy/EQgUvxP42Qa/PMvh+0rs2B97mgPw7YQDV+zW++NL7ad1b5r8kju/NIi+0JD27U50g37NQ2PNavXfkt4x/y5bqNjTcB0quirAGbS9lHryUM2pUAynXEV9fnH7UyWRfk1at1eQbpxW1GzZoTQlN08/Q== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PH0PR02MB7384.namprd02.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(39860400002)(396003)(366004)(346002)(136003)(376002)(451199015)(478600001)(6512007)(6506007)(6666004)(6486002)(52116002)(107886003)(86362001)(316002)(6916009)(38350700002)(38100700002)(66476007)(66556008)(66946007)(4326008)(8676002)(36756003)(26005)(2906002)(30864003)(41300700001)(83380400001)(186003)(8936002)(1076003)(5660300002)(2616005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?Ly+wA1jmkDpGdpygvItymw2BW2qy?= =?utf-8?q?4AaG/agKNUTBlrv9hTPr3ZdsU3rYddgTvswmnEK80nNt241C3wIS8/k0Ect3cDiJQ?= =?utf-8?q?UoSPXo/PjUtkeObY555Mxz96X0xr74ENPMv+2iSDwC6Fx2ES7t+BR+JYwYcKXYJrR?= =?utf-8?q?qGAKIzafQvCu9DMvrXO0XdrtOgeZF4ZFXh4WR8pGlTIemqrhTBrMEtQ+fWLpoHl94?= =?utf-8?q?fri9Od3I3txvupmsYQ1/pSum7SJR/H4fvhcYwMskuuBkjq5be100a1ocsLk5MaoyJ?= =?utf-8?q?ccQlzIpDlEevg+g+lrn49aRJ/oxpoidrQlQM7E7Mx+OgebE6Ob1OvHLBvrXMkeWiL?= =?utf-8?q?Sf6nrhultgC6lmuRpk9o5pY0OApIyi+ymLaRXwvK8mKV/4U5EchqFYhgabSE4xNit?= =?utf-8?q?8Ee2cC+Oa9UKfOtqVrcgpVsX/uc0bg5b6hR/RE5RfWViUBd3WAn2B+ItAF15mCYX7?= =?utf-8?q?KRKBW0Ucj0umxHBt0LTRpydbXvsNMMi5vpGKoXYIoHYttzFV34IArqJjomjzI6Ot1?= =?utf-8?q?Kgr55OqETiSIHoI60jwab9IGLxcYt62l4i6PYiFXl3PNLbnFXEm6utnD2Su4ZpRGW?= =?utf-8?q?IK5CIgChx/KokP4CC2nEZenzYAfjWwT+mKqC0cF77XUT4VIccH6joNI4zp1qlSZrH?= =?utf-8?q?5783l2Q3VElUztCSU4YFngbFMqSyje0/3/5a5WxfXfActk9p+D8kMucMtvi5ldKke?= =?utf-8?q?h4Go2uyeN2JpgslNriigVToWX+hkT/IQUz1oOkHdSFQFx9bJjoftwQaUFTzJiAIGA?= =?utf-8?q?Cxiod9uhCp7xE/aBwPNNz7dlDm/1uX79lEoTfAd244vvFZHQCkJzMNcchy/VOHo6m?= =?utf-8?q?NE+4aq05Qi4wroBDuJoAxPXCJ5JzTqhmNPHxLVLCG9tGW1qLF0yhXOqX1b9tNniC/?= =?utf-8?q?jKXKM1TfjFBsiz13vcyffyaSH9bwnPMCR6zWNUqPPWhxXrP9SMP3/kSqTy4hbYsgD?= =?utf-8?q?F2baG6ga9rdOfD0Egicsh1V/V0oC1jiLIT5rUpjCn8s8TUYUqVFgwgyhL4XyRVEQj?= =?utf-8?q?gDiPuce2WjlU2ZZAqcgyv9gAHX+qO6b7bIey0R6JdcjEeG0UfLmcqIuZsDJh4W4p7?= =?utf-8?q?4oKpwmKEM0G62ZEaWiKuI0QL+uCvaCicCJwbLjZPSSMNIQ/7pSbgFresqiJKJg34i?= =?utf-8?q?3CnEsBj5FEKvLz6u8S1b9DqSuULf1lYj7yYFac9NeP+/RiVh+LSEiaL6Hx9xClx5R?= =?utf-8?q?1nqGOt8HyIT5idprCrSeW9ie1ily1K9oFojs+Y4VYD9LuxLQVmNpJYP5jTM78WisS?= =?utf-8?q?P9/DuFcyg8c3tMqFAhdrLKXEm3CAQ+bkEeHi7O3VeJcSFkpXmkbDarZjPNx5l0cey?= =?utf-8?q?9Xah3s94TRPvspRp5YiSSv6T4Zp1ah83eKw9om5VvaUr47ABWHWnO6qtT9F2Pn5wJ?= =?utf-8?q?k+k3CTjSXijS+1Uzxwk7b2NLJFf5+hiMMCYAj7uqP268bUODeC/Om7iDt3+WYMGOb?= =?utf-8?q?+M3lrNJkEoS3PmJVRlf82QFk5baHC7Gq4crpIWlhaOtNwVUDcutbr6BluAyrBH9AM?= =?utf-8?q?yaxtKxv1/qpL9E/ffzekL+os6B/+YeFJgg=3D=3D?= X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: e96ceeb4-558d-476f-a3f8-08daca119944 X-MS-Exchange-CrossTenant-AuthSource: PH0PR02MB7384.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2022 09:36:56.2419 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DqQd5kNVYbyJ5hotRn4J30jjbSjEYogW/5nE2BLIWYI7R1Q8uxpMK56MjdIKOsBLMap8YpEz4JkCaO5kU6zKw+LV3AzyQDINXK869TJWkeU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR02MB7479 X-Proofpoint-GUID: m0jOCht5iZ2zIQa_XgmzQsunwqMIBsTV X-Proofpoint-ORIG-GUID: m0jOCht5iZ2zIQa_XgmzQsunwqMIBsTV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-18_08,2022-11-18_01,2022-06-22_01 X-Proofpoint-Spam-Reason: safe Received-SPF: pass client-ip=148.163.155.12; envelope-from=manish.mishra@nutanix.com; helo=mx0b-002c1b01.pphosted.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, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=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-bounces+qemu-devel=archiver.kernel.org@nongnu.org MSG_PEEK reads from the peek of channel, The data is treated as unread and the next read shall still return this data. This support is currently added only for socket class. Extra parameter 'flags' is added to io_readv calls to pass extra read flags like MSG_PEEK. Suggested-by: Daniel P. Berrangé --- chardev/char-socket.c | 4 +- include/io/channel.h | 83 +++++++++++++++++++++++++++++ io/channel-buffer.c | 1 + io/channel-command.c | 1 + io/channel-file.c | 1 + io/channel-null.c | 1 + io/channel-socket.c | 16 +++++- io/channel-tls.c | 1 + io/channel-websock.c | 1 + io/channel.c | 73 +++++++++++++++++++++++-- migration/channel-block.c | 1 + scsi/qemu-pr-helper.c | 2 +- tests/qtest/tpm-emu.c | 2 +- tests/unit/test-io-channel-socket.c | 1 + util/vhost-user-server.c | 2 +- 15 files changed, 179 insertions(+), 11 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 879564aa8a..5afce9a464 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -283,11 +283,11 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len) if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) { ret = qio_channel_readv_full(s->ioc, &iov, 1, &msgfds, &msgfds_num, - NULL); + 0, NULL); } else { ret = qio_channel_readv_full(s->ioc, &iov, 1, NULL, NULL, - NULL); + 0, NULL); } if (msgfds_num) { diff --git a/include/io/channel.h b/include/io/channel.h index c680ee7480..cbcde4b88f 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -34,6 +34,8 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass, #define QIO_CHANNEL_WRITE_FLAG_ZERO_COPY 0x1 +#define QIO_CHANNEL_READ_FLAG_MSG_PEEK 0x1 + typedef enum QIOChannelFeature QIOChannelFeature; enum QIOChannelFeature { @@ -41,6 +43,7 @@ enum QIOChannelFeature { QIO_CHANNEL_FEATURE_SHUTDOWN, QIO_CHANNEL_FEATURE_LISTEN, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY, + QIO_CHANNEL_FEATURE_READ_MSG_PEEK, }; @@ -114,6 +117,7 @@ struct QIOChannelClass { size_t niov, int **fds, size_t *nfds, + int flags, Error **errp); int (*io_close)(QIOChannel *ioc, Error **errp); @@ -188,6 +192,7 @@ void qio_channel_set_name(QIOChannel *ioc, * @niov: the length of the @iov array * @fds: pointer to an array that will received file handles * @nfds: pointer filled with number of elements in @fds on return + * @flags: read flags (QIO_CHANNEL_READ_FLAG_*) * @errp: pointer to a NULL-initialized error object * * Read data from the IO channel, storing it in the @@ -224,6 +229,7 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp); @@ -300,6 +306,34 @@ int qio_channel_readv_all_eof(QIOChannel *ioc, size_t niov, Error **errp); +/** + * qio_channel_readv_peek_all_eof: + * @ioc: the channel object + * @iov: the array of memory regions to read data into + * @niov: the length of the @iov array + * @errp: pointer to a NULL-initialized error object + * + * Read data from the peek of IO channel without + * actually removing it from channel buffer, storing + * it in the memory regions referenced by @iov. Each + * element in the @iov will be fully populated with + * data before the next one is used. The @niov + * parameter specifies the total number of elements + * in @iov. + * + * The function will wait for all requested data + * to be read, yielding from the current coroutine + * if required. + * + * Returns: 1 if all bytes were read, 0 if end-of-file + * occurs without data, or -1 on error + */ +int qio_channel_readv_peek_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + Error **errp); + + /** * qio_channel_readv_all: * @ioc: the channel object @@ -328,6 +362,34 @@ int qio_channel_readv_all(QIOChannel *ioc, Error **errp); +/** + * qio_channel_readv_peek_all: + * @ioc: the channel object + * @iov: the array of memory regions to read data into + * @niov: the length of the @iov array + * @errp: pointer to a NULL-initialized error object + * + * Read data from the the peek of IO channel without + * removing from channel buffer, storing it in the + * memory regions referenced by @iov. Each element + * in the @iov will be fully populated with data + * before the next one is used. The @niov parameter + * specifies the total number of elements in @iov. + * + * The function will wait for all requested data + * to be read, yielding from the current coroutine + * if required. + * + * If end-of-file occurs before all requested data + * has been read, an error will be reported. + * + * Returns: 0 if all bytes were read, or -1 on error + */ +int qio_channel_readv_peek_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + Error **errp); + /** * qio_channel_writev_all: * @ioc: the channel object @@ -456,6 +518,27 @@ int qio_channel_read_all(QIOChannel *ioc, size_t buflen, Error **errp); +/** + * qio_channel_read_peek_all: + * @ioc: the channel object + * @buf: the memory region to read data into + * @buflen: the number of bytes to @buf + * @errp: pointer to a NULL-initialized error object + * + * Reads @buflen bytes from the peek of channel into @buf without + * removing it from channel buffer, possibly blocking or (if the + * channel is non-blocking) yielding from the current coroutine + * multiple times until the entire content is read. If end-of-file + * occurs it will return an error rather than a short-read. Otherwise + * behaves as qio_channel_read(). + * + * Returns: 0 if all bytes were read, or -1 on error + */ +int qio_channel_read_peek_all(QIOChannel *ioc, + const char *buf, + size_t buflen, + Error **errp); + /** * qio_channel_write_all: * @ioc: the channel object diff --git a/io/channel-buffer.c b/io/channel-buffer.c index bf52011be2..8096180f85 100644 --- a/io/channel-buffer.c +++ b/io/channel-buffer.c @@ -54,6 +54,7 @@ static ssize_t qio_channel_buffer_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc); diff --git a/io/channel-command.c b/io/channel-command.c index 74516252ba..e7edd091af 100644 --- a/io/channel-command.c +++ b/io/channel-command.c @@ -203,6 +203,7 @@ static ssize_t qio_channel_command_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); diff --git a/io/channel-file.c b/io/channel-file.c index b67687c2aa..d76663e6ae 100644 --- a/io/channel-file.c +++ b/io/channel-file.c @@ -86,6 +86,7 @@ static ssize_t qio_channel_file_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); diff --git a/io/channel-null.c b/io/channel-null.c index 75e3781507..4fafdb770d 100644 --- a/io/channel-null.c +++ b/io/channel-null.c @@ -60,6 +60,7 @@ qio_channel_null_readv(QIOChannel *ioc, size_t niov, int **fds G_GNUC_UNUSED, size_t *nfds G_GNUC_UNUSED, + int flags, Error **errp) { QIOChannelNull *nioc = QIO_CHANNEL_NULL(ioc); diff --git a/io/channel-socket.c b/io/channel-socket.c index b76dca9cc1..a06b24766d 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -406,6 +406,8 @@ qio_channel_socket_accept(QIOChannelSocket *ioc, } #endif /* WIN32 */ + qio_channel_set_feature(QIO_CHANNEL(cioc), QIO_CHANNEL_FEATURE_READ_MSG_PEEK); + trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd); return cioc; @@ -496,6 +498,7 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); @@ -517,6 +520,10 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, } + if (flags & QIO_CHANNEL_READ_FLAG_MSG_PEEK) { + sflags |= MSG_PEEK; + } + retry: ret = recvmsg(sioc->fd, &msg, sflags); if (ret < 0) { @@ -624,11 +631,17 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); ssize_t done = 0; ssize_t i; + int sflags = 0; + + if (flags & QIO_CHANNEL_READ_FLAG_MSG_PEEK) { + sflags |= MSG_PEEK; + } for (i = 0; i < niov; i++) { ssize_t ret; @@ -636,7 +649,7 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc, ret = recv(sioc->fd, iov[i].iov_base, iov[i].iov_len, - 0); + sflags); if (ret < 0) { if (errno == EAGAIN) { if (done) { @@ -705,7 +718,6 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, } #endif /* WIN32 */ - #ifdef QEMU_MSG_ZEROCOPY static int qio_channel_socket_flush(QIOChannel *ioc, Error **errp) diff --git a/io/channel-tls.c b/io/channel-tls.c index 4ce890a538..c730cb8ec5 100644 --- a/io/channel-tls.c +++ b/io/channel-tls.c @@ -260,6 +260,7 @@ static ssize_t qio_channel_tls_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); diff --git a/io/channel-websock.c b/io/channel-websock.c index fb4932ade7..a12acc27cf 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -1081,6 +1081,7 @@ static ssize_t qio_channel_websock_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc); diff --git a/io/channel.c b/io/channel.c index 0640941ac5..23c8752918 100644 --- a/io/channel.c +++ b/io/channel.c @@ -52,6 +52,7 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc); @@ -63,7 +64,14 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc, return -1; } - return klass->io_readv(ioc, iov, niov, fds, nfds, errp); + if ((flags & QIO_CHANNEL_READ_FLAG_MSG_PEEK) && + !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) { + error_setg_errno(errp, EINVAL, + "Channel does not support peek read"); + return -1; + } + + return klass->io_readv(ioc, iov, niov, fds, nfds, flags, errp); } @@ -109,6 +117,37 @@ int qio_channel_readv_all_eof(QIOChannel *ioc, return qio_channel_readv_full_all_eof(ioc, iov, niov, NULL, NULL, errp); } +int qio_channel_readv_peek_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + Error **errp) +{ + ssize_t len = 0; + ssize_t total = iov_size(iov, niov); + + while (len < total) { + len = qio_channel_readv_full(ioc, iov, niov, NULL, + NULL, QIO_CHANNEL_READ_FLAG_MSG_PEEK, errp); + + if (len == QIO_CHANNEL_ERR_BLOCK) { + if (qemu_in_coroutine()) { + qio_channel_yield(ioc, G_IO_IN); + } else { + qio_channel_wait(ioc, G_IO_IN); + } + continue; + } + if (len == 0) { + return 0; + } + if (len < 0) { + return -1; + } + } + + return 1; +} + int qio_channel_readv_all(QIOChannel *ioc, const struct iovec *iov, size_t niov, @@ -117,6 +156,24 @@ int qio_channel_readv_all(QIOChannel *ioc, return qio_channel_readv_full_all(ioc, iov, niov, NULL, NULL, errp); } +int qio_channel_readv_peek_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + Error **errp) +{ + int ret = qio_channel_readv_peek_all_eof(ioc, iov, niov, errp); + + if (ret == 0) { + error_setg(errp, "Unexpected end-of-file before all data were read"); + return -1; + } + if (ret == 1) { + return 0; + } + + return ret; +} + int qio_channel_readv_full_all_eof(QIOChannel *ioc, const struct iovec *iov, size_t niov, @@ -146,7 +203,7 @@ int qio_channel_readv_full_all_eof(QIOChannel *ioc, while ((nlocal_iov > 0) || local_fds) { ssize_t len; len = qio_channel_readv_full(ioc, local_iov, nlocal_iov, local_fds, - local_nfds, errp); + local_nfds, 0, errp); if (len == QIO_CHANNEL_ERR_BLOCK) { if (qemu_in_coroutine()) { qio_channel_yield(ioc, G_IO_IN); @@ -284,7 +341,7 @@ ssize_t qio_channel_readv(QIOChannel *ioc, size_t niov, Error **errp) { - return qio_channel_readv_full(ioc, iov, niov, NULL, NULL, errp); + return qio_channel_readv_full(ioc, iov, niov, NULL, NULL, 0, errp); } @@ -303,7 +360,7 @@ ssize_t qio_channel_read(QIOChannel *ioc, Error **errp) { struct iovec iov = { .iov_base = buf, .iov_len = buflen }; - return qio_channel_readv_full(ioc, &iov, 1, NULL, NULL, errp); + return qio_channel_readv_full(ioc, &iov, 1, NULL, NULL, 0, errp); } @@ -336,6 +393,14 @@ int qio_channel_read_all(QIOChannel *ioc, return qio_channel_readv_all(ioc, &iov, 1, errp); } +int qio_channel_read_peek_all(QIOChannel *ioc, + const char *buf, + size_t buflen, + Error **errp) +{ + struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen }; + return qio_channel_readv_peek_all(ioc, &iov, 1, errp); +} int qio_channel_write_all(QIOChannel *ioc, const char *buf, diff --git a/migration/channel-block.c b/migration/channel-block.c index c55c8c93ce..0b0deeb919 100644 --- a/migration/channel-block.c +++ b/migration/channel-block.c @@ -53,6 +53,7 @@ qio_channel_block_readv(QIOChannel *ioc, size_t niov, int **fds, size_t *nfds, + int flags, Error **errp) { QIOChannelBlock *bioc = QIO_CHANNEL_BLOCK(ioc); diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c index 196b78c00d..199227a556 100644 --- a/scsi/qemu-pr-helper.c +++ b/scsi/qemu-pr-helper.c @@ -614,7 +614,7 @@ static int coroutine_fn prh_read(PRHelperClient *client, void *buf, int sz, iov.iov_base = buf; iov.iov_len = sz; n_read = qio_channel_readv_full(QIO_CHANNEL(client->ioc), &iov, 1, - &fds, &nfds, errp); + &fds, &nfds, 0, errp); if (n_read == QIO_CHANNEL_ERR_BLOCK) { qio_channel_yield(QIO_CHANNEL(client->ioc), G_IO_IN); diff --git a/tests/qtest/tpm-emu.c b/tests/qtest/tpm-emu.c index 2994d1cf42..3cf1acaf7d 100644 --- a/tests/qtest/tpm-emu.c +++ b/tests/qtest/tpm-emu.c @@ -106,7 +106,7 @@ void *tpm_emu_ctrl_thread(void *data) int *pfd = NULL; size_t nfd = 0; - qio_channel_readv_full(ioc, &iov, 1, &pfd, &nfd, &error_abort); + qio_channel_readv_full(ioc, &iov, 1, &pfd, &nfd, 0, &error_abort); cmd = be32_to_cpu(cmd); g_assert_cmpint(cmd, ==, CMD_SET_DATAFD); g_assert_cmpint(nfd, ==, 1); diff --git a/tests/unit/test-io-channel-socket.c b/tests/unit/test-io-channel-socket.c index b36a5d972a..b964bb202d 100644 --- a/tests/unit/test-io-channel-socket.c +++ b/tests/unit/test-io-channel-socket.c @@ -460,6 +460,7 @@ static void test_io_channel_unix_fd_pass(void) G_N_ELEMENTS(iorecv), &fdrecv, &nfdrecv, + 0, &error_abort); g_assert(nfdrecv == G_N_ELEMENTS(fdsend)); diff --git a/util/vhost-user-server.c b/util/vhost-user-server.c index 232984ace6..145eb17c08 100644 --- a/util/vhost-user-server.c +++ b/util/vhost-user-server.c @@ -116,7 +116,7 @@ vu_message_read(VuDev *vu_dev, int conn_fd, VhostUserMsg *vmsg) * qio_channel_readv_full may have short reads, keeping calling it * until getting VHOST_USER_HDR_SIZE or 0 bytes in total */ - rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, &local_err); + rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, 0, &local_err); if (rc < 0) { if (rc == QIO_CHANNEL_ERR_BLOCK) { assert(local_err == NULL); From patchwork Sat Nov 19 09:36:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manish X-Patchwork-Id: 13049670 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 7D1E7C433FE for ; Sat, 19 Nov 2022 09:38:18 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1owKHi-0000PL-V3; Sat, 19 Nov 2022 04:37:15 -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 1owKHS-0000Gq-Go for qemu-devel@nongnu.org; Sat, 19 Nov 2022 04:37:04 -0500 Received: from mx0a-002c1b01.pphosted.com ([148.163.151.68]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1owKHO-0007AN-Kx for qemu-devel@nongnu.org; Sat, 19 Nov 2022 04:36:57 -0500 Received: from pps.filterd (m0127840.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AJ9Gd3i011879; Sat, 19 Nov 2022 01:36:51 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=proofpoint20171006; bh=6CuVDQ1+saTVSzAH3SlPIZ6Y/EZOhdGH4CSk2dsCR/k=; b=GH1WB2R2a2reC7EaVbZZS8rbi3rCL3yKZWWtlQ4BYS8iID8kifbZZYAKLDS0PEMd6IVN Z5dR8rpLuMkZxee2Mv0coCKj23X6MVQ/hpf/T7WsLPtQZ7AZMZlkZmQYSTwm0WzrVgyq TmuqPSOP6CjYf8M8LZVifRtX6B9yvjRbIC+J5mosPGZJAQYRk6SQEhsfootQk3G1uEC2 w/H25LWb79CbpKVqJIQ+E0d7qR+gT1ON5FQ42XxKfkxAWjJT4fWd41AijT+5SLIqmhUi Hy3gVwXqFusVtuqbIL2PO1ZK6TUA/iHcR+jOltA8ECq1ndtLM6neIQ9TvgJT8Z0V+QGd gQ== Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2176.outbound.protection.outlook.com [104.47.55.176]) by mx0a-002c1b01.pphosted.com (PPS) with ESMTPS id 3kxvbxg0qy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 19 Nov 2022 01:36:51 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fD49dxPaaZYFBBCCf9rrJUrs95DabN7/nV00iHM0FbGrBdc63ykbIvj+YFsyckQghFIPE94tbWpIeJlIKjoyXS6TX2KA7Qs5YWZPlYo2SMX3Q0BaYAg+MT+//Ph55OimGjP3ffhrhWAMZJDQAg0OkFJSLAc6Fw+CXFMV8mJ1oybUcAU6Ni8nztg2aE4WYUZ6ENgVGBREJuPRVtW6M4JaF8+jXg6aGsmSdihj9coRGCqFtutNaogW0WXhTXxdp/taALjaWbdLAJ9yva59rFlnaPK8qQreQ2DIna3F/Oftr0WxywyZVzAMnYGXSkInv3c5Cz8O+/+ZEvMwdnSsjCSiow== 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=6CuVDQ1+saTVSzAH3SlPIZ6Y/EZOhdGH4CSk2dsCR/k=; b=TdUUDYa3l6MEQN/++IZocGz62CoIh2nfykiwfCm0RhYefXo5w8GXsFJ2WK4RvnIKKHmv7H1iFc+0nHews6CGar5Ym1NRXy4E4h9FNLeoCh/J5fnhobIFRakNB+hjX5em50xkAgXJM0Me4zS9MPN/y1du2Y+5/y+3miUpXRqVhcamVasEAfQIZtET9cb4iHURWxK1Ljw4hsjbJz+UaVOTPUHfH8Mr/mnCvBcWzKZORvWQclDuEWxVIqGcRYogNDfn1PHPAlTmMfBE5rq4HWCu4hC6O3NWP3BnnYpLcC2UgoS3v3I9Kplqblm6RsRDL+FGno+9da9xMmSuCW8h6aBCyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6CuVDQ1+saTVSzAH3SlPIZ6Y/EZOhdGH4CSk2dsCR/k=; b=FYJXHirutbu3zbAPp6AeDXAmEGbbGmlDXliPlArPx3NVccjrl/GOq26wkk7g2yOkxC6ayTUvYL1L0ncJ5wKnfLjF0pPeSJ9tmispnxXqCfgzTVlQLnFQNE+B7FI4LcMNeZL22NoKWSgymn4t8RfqKDcQjmtr1oRsPlMtn4z+WLvxaX2jI3fXAqBwDxjMApJlRFdL9k15qNOfu2uBEa8a7uZYUUsqQJgCtv+7urgYdcgfgnRCx83qLwJYRQZxhKUEFLr1c1UKn1ws79Mao8b9PN72is3yxGb+4cBHa04U24Xc3SgpPTWDDKOS+QCt6I9wjXofM+sOemL2l+qZBm5cmw== Received: from PH0PR02MB7384.namprd02.prod.outlook.com (2603:10b6:510:12::12) by PH0PR02MB7479.namprd02.prod.outlook.com (2603:10b6:510:a::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5834.9; Sat, 19 Nov 2022 09:36:49 +0000 Received: from PH0PR02MB7384.namprd02.prod.outlook.com ([fe80::dff2:2d3c:e182:fb66]) by PH0PR02MB7384.namprd02.prod.outlook.com ([fe80::dff2:2d3c:e182:fb66%3]) with mapi id 15.20.5834.009; Sat, 19 Nov 2022 09:36:49 +0000 From: "manish.mishra" To: qemu-devel@nongnu.org Cc: berrange@redhat.com, peterx@redhat.com, prerna.saxena@nutanix.com, quintela@redhat.com, dgilbert@redhat.com, lsoaresp@redhat.com, "manish.mishra" Subject: [PATCH 2/2] migration: check magic value for deciding the mapping of channels Date: Sat, 19 Nov 2022 09:36:12 +0000 Message-Id: <20221119093615.158072-2-manish.mishra@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20221119093615.158072-1-manish.mishra@nutanix.com> References: <20221119093615.158072-1-manish.mishra@nutanix.com> X-ClientProxiedBy: BYAPR01CA0058.prod.exchangelabs.com (2603:10b6:a03:94::35) To PH0PR02MB7384.namprd02.prod.outlook.com (2603:10b6:510:12::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR02MB7384:EE_|PH0PR02MB7479:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b1e181c-531a-46ef-0081-08daca119537 x-proofpoint-crosstenant: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qRqmgvg+U9OIsCJUNa7vYbJV0pabA7rCJlSlq8FKKbytYL0AspeOQaNYL1FsfQiqSt616YrJ7eE13I55HLV036JwTR5xg5ZZd9tWehnkxhLxEM5/1+gYUn7cXd8YZ7s2H5wof3Y11GX7D3WyvInr4rO7c00OXv79WPRDv6PQogoTVWtMwdpuG/GBUX0YEd6qayRvkcLXypb5M70Nde0zAOOLDjjQ3ZJ/DpPrILbto9xX0/xoYMBXu6LLDd+5Eju3Nentr9E4GaCPb6SlCLnlQ/fdxXvDMjel5GFFeX8Vn96s9u28KWEcQfk7Qx2PyowadvZyPdMyF53J0EURzzu2IIX0ESSiS/str2A+Cc1jIzgmV4JbIvp2l7+wU+4DVtUNYLFrYIxOcbfNDGw4InZwL7Z97hk6aRsTgxC9yL5A0MzzgwRwM003tsyY8RGGslCa9rFBqAMRk1/8ASbpA49Z5NPBUxxWGnTdnKIZySf5Fwx5sZiMp9OMJGIPJjCrntNsmN0oFufQbiCV4Is/ycmE4PtiZJketPmBXDrU6yALc1VAfCmYFfEwxrPgLIT2qaGPTQCU6hnDqZv5EU2mwcYSurwVjBTUH9k2M+iB9GDapDIXD4yITq7sZrSJLUy6I/cAwvYNfKEZE2LAGH3cm4zpIgY3jCXojO14K3vGB5MwfWo4XAVShEZ/aP4hMKOX0WRKe+2glY8Qrr/Q2KNVVQnaMg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PH0PR02MB7384.namprd02.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(39860400002)(396003)(366004)(346002)(136003)(376002)(451199015)(478600001)(6512007)(6506007)(6666004)(6486002)(52116002)(107886003)(86362001)(316002)(6916009)(38350700002)(38100700002)(66476007)(66556008)(66946007)(4326008)(8676002)(36756003)(26005)(2906002)(41300700001)(83380400001)(186003)(8936002)(1076003)(5660300002)(2616005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XVnCD6hhAK6dvQCPeZeltUlRuxRpy8jTymEmnLwe9AjHvZ+/YH1munI+CZ4igdXcay025yHjqwl+wR3lUyA19v4tTNKPpNKLwAEsFuo8U/SFu2xTSXC/xRPJfoL/PVveW+Vt8GdYgoPlMNdM2lFcXPSSOy2CTIBL6XfE8F/E9junWEh5AKV1OkU8CDyWPjZ7c62Ua7KtryrukaSBu6nKokhpdVH0bXCZ/nUq6J2cJ/f5i6PkS+gaRtz1dxf1s8gMCZJmtl86bEuFd5hoeopWJfZN8AF8fonu9TEfiq44STrN48HmevcPwRrAmJ/ktHZopAVOEMBc3uXFmjZM04fGIWdIj5U1ny/GyUiFOuQMSjZmzc2IteVZ2ksfCJv9NVsUltLV1tDcQSHO7Ivrkj6/rKwC3qJYzTc/Vw8RAspgoC6Jw9hEumql/PmDt55nmXzqOPGWrwFPxS+huy3Jc9y9HejgL/fEndaFvASJ+tSqzxR7Os8A2lKOeFB+2ob+LNuI3HbSQoGUicOaHSZpAFvTl3EOOJvb8a2t1KuOlbJDDA3uiaECkk89RIQc0fPzvaA6X/sXaOn9CXE85NuMvvKnfsl2KsU1qKVMGuoKeZLL/M/0PdKL4mCO6nK3kp+p14L3NvOtupy1h73p0KUZQFMNb1Ges7sGxMj0jaYkH6/O8PbH2pau6I/dTz8SkZ2poF/HZZF6ICLJ8tPxkN2mUKj8EhB6vhKd1KFgWC3nLi69ZXUmALwATi+F5HAthwl0Wz3KMUD2kvu6Fx5qv3zWRDQ/2is39oVdmz4cFW/xmVfh4c17v2CTVRnvD+xPqr33V5hJd25M5NsbFrNdatBBpGPDzXYNyhucdNT1pQNl4wH8ACuSjadIicxdvJrdjCss7kfGDZGlnAu6aZM8MOBMWM1jMzwYRp78MmXbI+Gdz3BJudI1MJeYNU5EEP3McH6XhBee32kO5NCgLec4mQ10IZveuaxOfHL+qFg8ZzPQZpb2NWMbssjRKRUPB2hXe7EDUm35Hl2rchg1AMFvWO+Rn/xcw34OmgJGRPFSGd22XzD8Gz9VgiOums/FKc+DvcJRo57JM3XgoBXzHXvfmW3qk7UHHGemMjPzC02eFILq3mG3jYMmW6A9OfbDSA70hQQMHWKum2Ta7XpFuRULloRgqyeDvPz9cG+Fv6INVf2D7xNy/phNnTvYp3N2oZVGq2OLBkbB/Yy8MF+ffy89oS7iLuXwtD/MkkgLS0b5m7KhksICgjBllZPo8NWTWxT0QkxiI4Vz2IBWsnfL9c16Z2HfAOnns9B+LygEPkq5T84KNlznfEGxjDKRqRirnmBfIUsH0PqxkuEAR/OnCbNdazJcDklUkRCe0DM9Bgx+5fkcfm6X+dP/FA7U4UXNUQhsZY7nrN3PWFainiRaSC/sfmckUr3Kcap50dQSJzqnV6fzzUoFo53/qXTVIigqAKL8bK61yUM33pdUoaB86SD1LxiseFrZzYb5xlseoek8jW8FopNUgSJr9/Q3Q1dXhU3MoFjtq+XaXj/9T8Za/g5P4hGAPz8wGOFeaLbF7+oXx8Io0Wo/Ql7Xdz39kgf8QHPvil4uzx7J59Mq9JYJWA7PCNjTHWRA7w== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b1e181c-531a-46ef-0081-08daca119537 X-MS-Exchange-CrossTenant-AuthSource: PH0PR02MB7384.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2022 09:36:49.4416 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hVI1nHxu5lgskiVBH/nnf0T2xqOw4hSCxB9N8NH3xxdWHDibXbdRbhGNszeKLc0Asl3zM/9KgcYvekPGd511TD2/9oDmKuXZcQkBmj1memI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR02MB7479 X-Proofpoint-GUID: 5Py2Nbb2_8xIF1WLKstHagQvLwIPJrsC X-Proofpoint-ORIG-GUID: 5Py2Nbb2_8xIF1WLKstHagQvLwIPJrsC X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-18_08,2022-11-18_01,2022-06-22_01 X-Proofpoint-Spam-Reason: safe Received-SPF: pass client-ip=148.163.151.68; envelope-from=manish.mishra@nutanix.com; helo=mx0a-002c1b01.pphosted.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, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=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-bounces+qemu-devel=archiver.kernel.org@nongnu.org Current logic assumes that channel connections on the destination side are always established in the same order as the source and the first one will always be the main channel followed by the multifid or post-copy preemption channel. This may not be always true, as even if a channel has a connection established on the source side it can be in the pending state on the destination side and a newer connection can be established first. Basically causing out of order mapping of channels on the destination side. Currently, all channels except post-copy preempt send a magic number, this patch uses that magic number to decide the type of channel. This logic is applicable only for precopy(multifd) live migration, as mentioned, the post-copy preempt channel does not send any magic number. Also, tls live migrations already does tls handshake before creating other channels, so this issue is not possible with tls, hence this logic is avoided for tls live migrations. This patch uses read peek to check the magic number of channels so that current data/control stream management remains un-effected. --- migration/migration.c | 44 +++++++++++++++++++++++++++++----------- migration/multifd.c | 12 ++++------- migration/multifd.h | 2 +- migration/postcopy-ram.c | 5 +---- migration/postcopy-ram.h | 2 +- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 739bb683f3..787e678d48 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -733,31 +733,51 @@ void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp) { MigrationIncomingState *mis = migration_incoming_get_current(); Error *local_err = NULL; - bool start_migration; QEMUFile *f; + bool default_channel = true; + uint32_t channel_magic = 0; + int ret = 0; - if (!mis->from_src_file) { - /* The first connection (multifd may have multiple) */ + if (migrate_use_multifd() && !migrate_postcopy_ram() && + qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) { + /* + * With multiple channels, it is possible that we receive channels + * out of order on destination side, causing incorrect mapping of + * source channels on destination side. Check channel MAGIC to + * decide type of channel. Please note this is best effort, postcopy + * preempt channel does not send any magic number so avoid it for + * postcopy live migration. Also tls live migration already does + * tls handshake while initializing main channel so with tls this + * issue is not possible. + */ + ret = qio_channel_read_peek_all(ioc, (void *)&channel_magic, + sizeof(channel_magic), &local_err); + + if (ret != 0) { + error_propagate(errp, local_err); + return; + } + + default_channel = (channel_magic == cpu_to_be32(QEMU_VM_FILE_MAGIC)); + } else { + default_channel = !mis->from_src_file; + } + + if (default_channel) { f = qemu_file_new_input(ioc); if (!migration_incoming_setup(f, errp)) { return; } - - /* - * Common migration only needs one channel, so we can start - * right now. Some features need more than one channel, we wait. - */ - start_migration = !migration_needs_multiple_sockets(); } else { /* Multiple connections */ assert(migration_needs_multiple_sockets()); if (migrate_use_multifd()) { - start_migration = multifd_recv_new_channel(ioc, &local_err); + multifd_recv_new_channel(ioc, &local_err); } else { assert(migrate_postcopy_preempt()); f = qemu_file_new_input(ioc); - start_migration = postcopy_preempt_new_channel(mis, f); + postcopy_preempt_new_channel(mis, f); } if (local_err) { error_propagate(errp, local_err); @@ -765,7 +785,7 @@ void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp) } } - if (start_migration) { + if (migration_has_all_channels()) { /* If it's a recovery, we're done */ if (postcopy_try_recover()) { return; diff --git a/migration/multifd.c b/migration/multifd.c index 586ddc9d65..be86a4d07f 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -1220,11 +1220,9 @@ bool multifd_recv_all_channels_created(void) /* * Try to receive all multifd channels to get ready for the migration. - * - Return true and do not set @errp when correctly receiving all channels; - * - Return false and do not set @errp when correctly receiving the current one; - * - Return false and set @errp when failing to receive the current channel. + * Sets @errp when failing to receive the current channel. */ -bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp) +void multifd_recv_new_channel(QIOChannel *ioc, Error **errp) { MultiFDRecvParams *p; Error *local_err = NULL; @@ -1237,7 +1235,7 @@ bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp) "failed to receive packet" " via multifd channel %d: ", qatomic_read(&multifd_recv_state->count)); - return false; + return; } trace_multifd_recv_new_channel(id); @@ -1247,7 +1245,7 @@ bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp) id); multifd_recv_terminate_threads(local_err); error_propagate(errp, local_err); - return false; + return; } p->c = ioc; object_ref(OBJECT(ioc)); @@ -1258,6 +1256,4 @@ bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp) qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p, QEMU_THREAD_JOINABLE); qatomic_inc(&multifd_recv_state->count); - return qatomic_read(&multifd_recv_state->count) == - migrate_multifd_channels(); } diff --git a/migration/multifd.h b/migration/multifd.h index 519f498643..913e4ba274 100644 --- a/migration/multifd.h +++ b/migration/multifd.h @@ -18,7 +18,7 @@ void multifd_save_cleanup(void); int multifd_load_setup(Error **errp); int multifd_load_cleanup(Error **errp); bool multifd_recv_all_channels_created(void); -bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp); +void multifd_recv_new_channel(QIOChannel *ioc, Error **errp); void multifd_recv_sync_main(void); int multifd_send_sync_main(QEMUFile *f); int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset); diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index b9a37ef255..f84f783ab4 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -1539,7 +1539,7 @@ void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd) } } -bool postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file) +void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file) { /* * The new loading channel has its own threads, so it needs to be @@ -1548,9 +1548,6 @@ bool postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file) qemu_file_set_blocking(file, true); mis->postcopy_qemufile_dst = file; trace_postcopy_preempt_new_channel(); - - /* Start the migration immediately */ - return true; } /* diff --git a/migration/postcopy-ram.h b/migration/postcopy-ram.h index 6147bf7d1d..25881c4127 100644 --- a/migration/postcopy-ram.h +++ b/migration/postcopy-ram.h @@ -190,7 +190,7 @@ enum PostcopyChannels { RAM_CHANNEL_MAX, }; -bool postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file); +void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file); int postcopy_preempt_setup(MigrationState *s, Error **errp); int postcopy_preempt_wait_channel(MigrationState *s);